1/*- 2 * Copyright (c) 1999 Jonathan Lemon 3 * Copyright (c) 1999 Michael Smith 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 *
|
39#include <sys/bus.h> 40#include <sys/conf.h> 41#include <sys/devicestat.h> 42#include <sys/disk.h> 43 44#include <machine/bus.h> 45#include <machine/clock.h> 46#include <sys/rman.h> 47 48#include <dev/amr/amrio.h> 49#include <dev/amr/amrreg.h> 50#include <dev/amr/amrvar.h> 51 52#if 0 53#define debug(fmt, args...) printf("%s: " fmt "\n", __FUNCTION__ , ##args) 54#else 55#define debug(fmt, args...) 56#endif 57 58/* prototypes */ 59static int amrd_probe(device_t dev); 60static int amrd_attach(device_t dev); 61static int amrd_detach(device_t dev); 62 63static d_open_t amrd_open; 64static d_close_t amrd_close; 65static d_strategy_t amrd_strategy; 66static d_ioctl_t amrd_ioctl; 67 68#define AMRD_BDEV_MAJOR 35 69#define AMRD_CDEV_MAJOR 133 70 71static struct cdevsw amrd_cdevsw = { 72 /* open */ amrd_open, 73 /* close */ amrd_close, 74 /* read */ physread, 75 /* write */ physwrite, 76 /* ioctl */ amrd_ioctl, 77 /* poll */ nopoll, 78 /* mmap */ nommap, 79 /* strategy */ amrd_strategy, 80 /* name */ "amrd", 81 /* maj */ AMRD_CDEV_MAJOR, 82 /* dump */ nodump, 83 /* psize */ nopsize, 84 /* flags */ D_DISK, 85 /* bmaj */ AMRD_BDEV_MAJOR 86}; 87 88static devclass_t amrd_devclass; 89static struct cdevsw amrddisk_cdevsw; 90static int disks_registered = 0; 91 92static device_method_t amrd_methods[] = { 93 DEVMETHOD(device_probe, amrd_probe), 94 DEVMETHOD(device_attach, amrd_attach), 95 DEVMETHOD(device_detach, amrd_detach), 96 { 0, 0 } 97}; 98 99static driver_t amrd_driver = { 100 "amrd", 101 amrd_methods, 102 sizeof(struct amrd_softc) 103}; 104 105DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0); 106 107static int 108amrd_open(dev_t dev, int flags, int fmt, struct proc *p) 109{ 110 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1; 111 struct disklabel *label; 112 113 debug("called"); 114 115 if (sc == NULL) 116 return (ENXIO); 117 118 /* controller not active? */ 119 if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN) 120 return(ENXIO); 121 122 label = &sc->amrd_disk.d_label; 123 bzero(label, sizeof(*label)); 124 label->d_type = DTYPE_SCSI; 125 label->d_secsize = AMR_BLKSIZE; 126 label->d_nsectors = sc->amrd_drive->al_sectors; 127 label->d_ntracks = sc->amrd_drive->al_heads; 128 label->d_ncylinders = sc->amrd_drive->al_cylinders; 129 label->d_secpercyl = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads; 130 label->d_secperunit = sc->amrd_drive->al_size; 131 132 sc->amrd_flags |= AMRD_OPEN; 133 return (0); 134} 135 136static int 137amrd_close(dev_t dev, int flags, int fmt, struct proc *p) 138{ 139 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1; 140 141 debug("called"); 142 143 if (sc == NULL) 144 return (ENXIO); 145 sc->amrd_flags &= ~AMRD_OPEN; 146 return (0); 147} 148 149static int 150amrd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p) 151{ 152 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1; 153 int error; 154 155 debug("called"); 156 157 if (sc == NULL) 158 return (ENXIO); 159 160 if ((error = amr_submit_ioctl(sc->amrd_controller, sc->amrd_drive, cmd, addr, flag, p)) != ENOIOCTL) { 161 debug("amr_submit_ioctl returned %d\n", error); 162 return(error); 163 } 164 return (ENOTTY); 165} 166 167/* 168 * Read/write routine for a buffer. Finds the proper unit, range checks 169 * arguments, and schedules the transfer. Does not wait for the transfer 170 * to complete. Multi-page transfers are supported. All I/O requests must 171 * be a multiple of a sector in length. 172 */ 173static void 174amrd_strategy(struct bio *bp) 175{ 176 struct amrd_softc *sc = (struct amrd_softc *)bp->bio_dev->si_drv1; 177 178 debug("called to %s %d bytes at b_blkno 0x%x b_pblkno 0x%x", 179 (bp->b_flags & B_READ) ? "read" : "write", bp->bio_bcount, bp->bio_blkno, bp->bio_pblkno); 180 181 /* bogus disk? */ 182 if (sc == NULL) { 183 bp->bio_error = EINVAL; 184 goto bad; 185 } 186 187#if 0 188 /* XXX may only be temporarily offline - sleep? */ 189 if (sc->amrd_drive->ld_state == AMR_SYSD_OFFLINE) { 190 bp->bio_error = ENXIO; 191 goto bad; 192 } 193#endif 194 195 /* do-nothing operation */ 196 if (bp->bio_bcount == 0) 197 goto done; 198 199 devstat_start_transaction(&sc->amrd_stats); 200 amr_submit_buf(sc->amrd_controller, bp); 201 return; 202 203 bad: 204 bp->bio_flags |= BIO_ERROR; 205 206 done: 207 /* 208 * Correctly set the buf to indicate a completed transfer 209 */ 210 bp->bio_resid = bp->bio_bcount; 211 biodone(bp); 212 return; 213} 214 215void 216amrd_intr(void *data) 217{ 218 struct bio *bp = (struct bio *)data; 219 struct amrd_softc *sc = (struct amrd_softc *)bp->bio_dev->si_drv1; 220 221 debug("called"); 222 223 if (bp->bio_flags & BIO_ERROR) { 224 bp->bio_error = EIO; 225 debug("i/o error\n"); 226 } else { 227#if 0 228 int i; 229 for (i = 0; i < 512; i += 16) 230 debug(" %04x %16D", i, bp->bio_data + i, " "); 231#endif 232 bp->bio_resid = 0; 233 } 234 235 devstat_end_transaction_bio(&sc->amrd_stats, bp); 236 biodone(bp); 237} 238 239static int 240amrd_probe(device_t dev) 241{ 242 243 debug("called"); 244 245 device_set_desc(dev, "MegaRAID logical drive"); 246 return (0); 247} 248 249static int 250amrd_attach(device_t dev) 251{ 252 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev); 253 device_t parent; 254 char *state; 255 256 debug("called"); 257 258 parent = device_get_parent(dev); 259 sc->amrd_controller = (struct amr_softc *)device_get_softc(parent); 260 sc->amrd_unit = device_get_unit(dev); 261 sc->amrd_drive = device_get_ivars(dev); 262 sc->amrd_dev = dev; 263 264 switch(sc->amrd_drive->al_state) { 265 case AMRD_OFFLINE: 266 state = "offline"; 267 break; 268 case AMRD_DEGRADED: 269 state = "degraded"; 270 break; 271 case AMRD_OPTIMAL: 272 state = "optimal"; 273 break; 274 case AMRD_DELETED: 275 state = "deleted"; 276 break; 277 default: 278 state = "unknown state"; 279 } 280 281 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n", 282 sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE), 283 sc->amrd_drive->al_size, sc->amrd_drive->al_properties & 0xf, state); 284 285 devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE, 286 DEVSTAT_NO_ORDERED_TAGS, 287 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 288 DEVSTAT_PRIORITY_ARRAY); 289 290 sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_cdevsw, &amrddisk_cdevsw); 291 sc->amrd_dev_t->si_drv1 = sc; 292 disks_registered++; 293 294 /* set maximum I/O size */ 295 /* dsk->si_iosize_max = ??? */; 296 297 return (0); 298} 299 300static int 301amrd_detach(device_t dev) 302{ 303 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev); 304 305 debug("called"); 306 307 devstat_remove_entry(&sc->amrd_stats); 308 disk_destroy(sc->amrd_dev_t); 309 return(0); 310} 311
|