1 /*- 2 * Copyright (c) 1999 Jonathan Lemon 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Disk driver for Compaq SMART RAID adapters. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/kernel.h> 37 38 #include <sys/buf.h> 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/device.h> 42 #include <sys/devicestat.h> 43 #include <sys/disklabel.h> 44 #include <sys/diskslice.h> 45 46 #if NPCI > 0 47 #include <machine/bus_memio.h> 48 #endif 49 #include <machine/bus_pio.h> 50 #include <machine/bus.h> 51 #include <machine/clock.h> 52 #include <sys/rman.h> 53 54 #include <dev/ida/idareg.h> 55 #include <dev/ida/idavar.h> 56 57 /* prototypes */ 58 static void id_drvinit(void); 59 static int idprobe(device_t dev); 60 static int idattach(device_t dev); 61 62 static d_open_t idopen; 63 static d_close_t idclose; 64 static d_strategy_t idstrategy; 65 static d_ioctl_t idioctl; 66 static d_psize_t idsize; 67 68 #define ID_BDEV_MAJOR 29 69 #define ID_CDEV_MAJOR 109 70 71 #define WD_BDEV_MAJOR 0 72 #define WD_CDEV_MAJOR 3 73 74 static struct cdevsw id_cdevsw = { 75 /* open */ idopen, 76 /* close */ idclose, 77 /* read */ physread, 78 /* write */ physwrite, 79 /* ioctl */ idioctl, 80 /* stop */ nostop, 81 /* reset */ noreset, 82 /* devtotty */ nodevtotty, 83 /* poll */ nopoll, 84 /* mmap */ nommap, 85 /* strategy */ idstrategy, 86 /* name */ "id", 87 /* parms */ noparms, 88 /* maj */ ID_CDEV_MAJOR, 89 /* dump */ nodump, 90 /* psize */ idsize, 91 /* flags */ D_DISK, 92 /* maxio */ 0, 93 /* bmaj */ ID_BDEV_MAJOR 94 }; 95 static struct cdevsw stolen_cdevsw; 96 97 static devclass_t id_devclass; 98 99 static device_method_t id_methods[] = { 100 DEVMETHOD(device_probe, idprobe), 101 DEVMETHOD(device_attach, idattach), 102 { 0, 0 } 103 }; 104 105 static driver_t id_driver = { 106 "id", 107 id_methods, 108 sizeof(struct id_softc) 109 }; 110 111 static __inline struct id_softc * 112 idgetsoftc(dev_t dev) 113 { 114 int unit; 115 116 unit = dkunit(dev); 117 return ((struct id_softc *)devclass_get_softc(id_devclass, unit)); 118 } 119 120 static int 121 idopen(dev_t dev, int flags, int fmt, struct proc *p) 122 { 123 struct id_softc *drv; 124 struct disklabel label; 125 int error; 126 127 drv = idgetsoftc(dev); 128 if (drv == NULL) 129 return (ENXIO); 130 131 /* XXX block against race where > 1 person is reading label? */ 132 133 bzero(&label, sizeof(label)); 134 label.d_type = DTYPE_SCSI; /* XXX should this be DTYPE_RAID? */ 135 /* 136 strncpy(label.d_typename, ... 137 strncpy(label.d_packname, ... 138 */ 139 label.d_secsize = drv->secsize; 140 label.d_nsectors = drv->sectors; 141 label.d_ntracks = drv->heads; 142 label.d_ncylinders = drv->cylinders; 143 label.d_secpercyl = drv->sectors * drv->heads; 144 label.d_secperunit = drv->secperunit; 145 146 /* Initialize slice tables. */ 147 error = dsopen("id", dev, fmt, 0, &drv->slices, &label); 148 149 return (error); 150 } 151 152 static int 153 idclose(dev_t dev, int flags, int fmt, struct proc *p) 154 { 155 struct id_softc *drv; 156 157 drv = idgetsoftc(dev); 158 if (drv == NULL) 159 return (ENXIO); 160 dsclose(dev, fmt, drv->slices); 161 return (0); 162 } 163 164 static int 165 idioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p) 166 { 167 struct id_softc *drv; 168 int error; 169 170 drv = idgetsoftc(dev); 171 if (drv == NULL) 172 return (ENXIO); 173 174 error = dsioctl("id", dev, cmd, addr, flag, &drv->slices); 175 176 if (error == ENOIOCTL) 177 return (ENOTTY); 178 179 return (error); 180 } 181 182 static int 183 idsize(dev_t dev) 184 { 185 struct id_softc *drv; 186 187 drv = idgetsoftc(dev); 188 if (drv == NULL) 189 return (ENXIO); 190 return (dssize(dev, &drv->slices)); 191 } 192 193 /* 194 * Read/write routine for a buffer. Finds the proper unit, range checks 195 * arguments, and schedules the transfer. Does not wait for the transfer 196 * to complete. Multi-page transfers are supported. All I/O requests must 197 * be a multiple of a sector in length. 198 */ 199 static void 200 idstrategy(struct buf *bp) 201 { 202 struct id_softc *drv; 203 int s; 204 205 drv = idgetsoftc(bp->b_dev); 206 if (drv == NULL) { 207 bp->b_error = EINVAL; 208 goto bad; 209 } 210 211 if (dscheck(bp, drv->slices) <= 0) 212 goto done; 213 214 /* 215 * software write protect check 216 */ 217 if (drv->flags & DRV_WRITEPROT && (bp->b_flags & B_READ) == 0) { 218 bp->b_error = EROFS; 219 goto bad; 220 } 221 222 /* 223 * If it's a null transfer, return immediately 224 */ 225 if (bp->b_bcount == 0) 226 goto done; 227 228 bp->b_driver1 = drv; 229 s = splbio(); 230 devstat_start_transaction(&drv->stats); 231 ida_submit_buf(drv->controller, bp); 232 splx(s); 233 return; 234 235 bad: 236 bp->b_flags |= B_ERROR; 237 238 done: 239 /* 240 * Correctly set the buf to indicate a completed transfer 241 */ 242 bp->b_resid = bp->b_bcount; 243 biodone(bp); 244 return; 245 } 246 247 void 248 id_intr(struct buf *bp) 249 { 250 struct id_softc *drv = (struct id_softc *)bp->b_driver1; 251 252 if (bp->b_flags & B_ERROR) 253 bp->b_error = EIO; 254 else 255 bp->b_resid = 0; 256 257 biodone(bp); 258 devstat_end_transaction(&drv->stats, 259 bp->b_bcount - bp->b_resid, DEVSTAT_TAG_NONE, 260 (bp->b_flags & B_READ) ? DEVSTAT_READ : DEVSTAT_WRITE); 261 } 262 263 static void 264 id_drvinit(void) 265 { 266 static int devsw_installed = 0; 267 268 if (devsw_installed) 269 return; /* XXX is this needed? */ 270 271 cdevsw_add(&id_cdevsw); 272 stolen_cdevsw = id_cdevsw; 273 stolen_cdevsw.d_maj = WD_CDEV_MAJOR; 274 stolen_cdevsw.d_bmaj = WD_BDEV_MAJOR; 275 cdevsw_add(&stolen_cdevsw); 276 devsw_installed = 1; 277 } 278 279 static int 280 idprobe(device_t dev) 281 { 282 283 id_drvinit(); 284 device_set_desc(dev, "Compaq Logical Drive"); 285 return (0); 286 } 287 288 static int 289 idattach(device_t dev) 290 { 291 struct ida_drive_info dinfo; 292 struct id_softc *drv; 293 device_t parent; 294 int error; 295 296 drv = (struct id_softc *)device_get_softc(dev); 297 parent = device_get_parent(dev); 298 drv->controller = (struct ida_softc *)device_get_softc(parent); 299 drv->unit = device_get_unit(dev); 300 301 error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO, 302 &dinfo, sizeof(dinfo), drv->unit, DMA_DATA_IN); 303 if (error) { 304 device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n"); 305 return (ENXIO); 306 } 307 308 drv->cylinders = dinfo.ncylinders; 309 drv->heads = dinfo.nheads; 310 drv->sectors = dinfo.nsectors; 311 drv->secsize = dinfo.secsize; 312 drv->secperunit = dinfo.secperunit; 313 314 /* XXX 315 * other initialization 316 */ 317 device_printf(dev, "%uMB (%u sectors), blocksize=%d\n", 318 drv->secperunit / ((1024 * 1024) / drv->secsize), 319 drv->secperunit, drv->secsize); 320 321 devstat_add_entry(&drv->stats, "id", drv->unit, drv->secsize, 322 DEVSTAT_NO_ORDERED_TAGS, 323 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, DEVSTAT_PRIORITY_DA); 324 325 return (0); 326 } 327 328 DEV_DRIVER_MODULE(id, ida, id_driver, id_devclass, id_cdevsw, 0, 0); 329