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 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Disk driver for Mylex DAC960 RAID adapters. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 38 #include <sys/bio.h> 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 <sys/rman.h> 46 47 #include <dev/mlx/mlxio.h> 48 #include <dev/mlx/mlxvar.h> 49 #include <dev/mlx/mlxreg.h> 50 51 /* prototypes */ 52 static int mlxd_probe(device_t dev); 53 static int mlxd_attach(device_t dev); 54 static int mlxd_detach(device_t dev); 55 56 static d_open_t mlxd_open; 57 static d_close_t mlxd_close; 58 static d_strategy_t mlxd_strategy; 59 static d_ioctl_t mlxd_ioctl; 60 61 #define MLXD_BDEV_MAJOR 27 62 #define MLXD_CDEV_MAJOR 131 63 64 static struct cdevsw mlxd_cdevsw = { 65 /* open */ mlxd_open, 66 /* close */ mlxd_close, 67 /* read */ physread, 68 /* write */ physwrite, 69 /* ioctl */ mlxd_ioctl, 70 /* poll */ nopoll, 71 /* mmap */ nommap, 72 /* strategy */ mlxd_strategy, 73 /* name */ "mlxd", 74 /* maj */ MLXD_CDEV_MAJOR, 75 /* dump */ nodump, 76 /* psize */ nopsize, 77 /* flags */ D_DISK, 78 /* bmaj */ MLXD_BDEV_MAJOR 79 }; 80 81 devclass_t mlxd_devclass; 82 static struct cdevsw mlxddisk_cdevsw; 83 84 static device_method_t mlxd_methods[] = { 85 DEVMETHOD(device_probe, mlxd_probe), 86 DEVMETHOD(device_attach, mlxd_attach), 87 DEVMETHOD(device_detach, mlxd_detach), 88 { 0, 0 } 89 }; 90 91 static driver_t mlxd_driver = { 92 "mlxd", 93 mlxd_methods, 94 sizeof(struct mlxd_softc) 95 }; 96 97 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0); 98 99 static int 100 mlxd_open(dev_t dev, int flags, int fmt, struct proc *p) 101 { 102 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1; 103 struct disklabel *label; 104 105 debug_called(1); 106 107 if (sc == NULL) 108 return (ENXIO); 109 110 /* controller not active? */ 111 if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN) 112 return(ENXIO); 113 114 label = &sc->mlxd_disk.d_label; 115 bzero(label, sizeof(*label)); 116 label->d_type = DTYPE_SCSI; 117 label->d_secsize = MLX_BLKSIZE; 118 label->d_nsectors = sc->mlxd_drive->ms_sectors; 119 label->d_ntracks = sc->mlxd_drive->ms_heads; 120 label->d_ncylinders = sc->mlxd_drive->ms_cylinders; 121 label->d_secpercyl = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads; 122 label->d_secperunit = sc->mlxd_drive->ms_size; 123 124 sc->mlxd_flags |= MLXD_OPEN; 125 return (0); 126 } 127 128 static int 129 mlxd_close(dev_t dev, int flags, int fmt, struct proc *p) 130 { 131 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1; 132 133 debug_called(1); 134 135 if (sc == NULL) 136 return (ENXIO); 137 sc->mlxd_flags &= ~MLXD_OPEN; 138 return (0); 139 } 140 141 static int 142 mlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p) 143 { 144 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1; 145 int error; 146 147 debug_called(1); 148 149 if (sc == NULL) 150 return (ENXIO); 151 152 if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) { 153 debug(0, "mlx_submit_ioctl returned %d\n", error); 154 return(error); 155 } 156 return (ENOTTY); 157 } 158 159 /* 160 * Read/write routine for a buffer. Finds the proper unit, range checks 161 * arguments, and schedules the transfer. Does not wait for the transfer 162 * to complete. Multi-page transfers are supported. All I/O requests must 163 * be a multiple of a sector in length. 164 */ 165 static void 166 mlxd_strategy(struct bio *bp) 167 { 168 struct mlxd_softc *sc = (struct mlxd_softc *)bp->bio_dev->si_drv1; 169 170 debug_called(1); 171 172 /* bogus disk? */ 173 if (sc == NULL) { 174 bp->bio_error = EINVAL; 175 goto bad; 176 } 177 178 /* XXX may only be temporarily offline - sleep? */ 179 if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) { 180 bp->bio_error = ENXIO; 181 goto bad; 182 } 183 184 /* do-nothing operation */ 185 if (bp->bio_bcount == 0) 186 goto done; 187 188 devstat_start_transaction(&sc->mlxd_stats); 189 mlx_submit_buf(sc->mlxd_controller, bp); 190 return; 191 192 bad: 193 bp->bio_flags |= BIO_ERROR; 194 195 done: 196 /* 197 * Correctly set the buf to indicate a completed transfer 198 */ 199 bp->bio_resid = bp->bio_bcount; 200 biodone(bp); 201 return; 202 } 203 204 void 205 mlxd_intr(void *data) 206 { 207 struct bio *bp = (struct bio *)data; 208 struct mlxd_softc *sc = (struct mlxd_softc *)bp->bio_dev->si_drv1; 209 210 debug_called(1); 211 212 if (bp->bio_flags & BIO_ERROR) 213 bp->bio_error = EIO; 214 else 215 bp->bio_resid = 0; 216 217 devstat_end_transaction_bio(&sc->mlxd_stats, bp); 218 biodone(bp); 219 } 220 221 static int 222 mlxd_probe(device_t dev) 223 { 224 225 debug_called(1); 226 227 device_set_desc(dev, "Mylex System Drive"); 228 return (0); 229 } 230 231 static int 232 mlxd_attach(device_t dev) 233 { 234 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev); 235 device_t parent; 236 char *state; 237 dev_t dsk; 238 int s1, s2; 239 240 debug_called(1); 241 242 parent = device_get_parent(dev); 243 sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent); 244 sc->mlxd_unit = device_get_unit(dev); 245 sc->mlxd_drive = device_get_ivars(dev); 246 sc->mlxd_dev = dev; 247 248 switch(sc->mlxd_drive->ms_state) { 249 case MLX_SYSD_ONLINE: 250 state = "online"; 251 break; 252 case MLX_SYSD_CRITICAL: 253 state = "critical"; 254 break; 255 case MLX_SYSD_OFFLINE: 256 state = "offline"; 257 break; 258 default: 259 state = "unknown state"; 260 } 261 262 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n", 263 sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE), 264 sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state); 265 266 devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE, 267 DEVSTAT_NO_ORDERED_TAGS, 268 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 269 DEVSTAT_PRIORITY_ARRAY); 270 271 dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw); 272 dsk->si_drv1 = sc; 273 sc->mlxd_dev_t = dsk; 274 275 /* 276 * Set maximum I/O size to the lesser of the recommended maximum and the practical 277 * maximum. 278 */ 279 s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE; 280 s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE; 281 dsk->si_iosize_max = imin(s1, s2); 282 283 return (0); 284 } 285 286 static int 287 mlxd_detach(device_t dev) 288 { 289 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev); 290 291 debug_called(1); 292 293 devstat_remove_entry(&sc->mlxd_stats); 294 disk_destroy(sc->mlxd_dev_t); 295 296 return(0); 297 } 298 299