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