1 /*- 2 * Copyright (c) 1999,2000 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/kernel.h> 36 37 #include <sys/bio.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/cons.h> 41 #include <sys/devicestat.h> 42 #include <sys/disk.h> 43 44 #include <machine/bus_memio.h> 45 #include <machine/bus_pio.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 49 #include <vm/vm.h> 50 #include <vm/pmap.h> 51 #include <machine/md_var.h> 52 53 #include <dev/ida/idareg.h> 54 #include <dev/ida/idavar.h> 55 56 /* prototypes */ 57 static int idad_probe(device_t dev); 58 static int idad_attach(device_t dev); 59 static int idad_detach(device_t dev); 60 61 static d_open_t idad_open; 62 static d_close_t idad_close; 63 static d_strategy_t idad_strategy; 64 static d_dump_t idad_dump; 65 66 #define IDAD_CDEV_MAJOR 109 67 68 static struct cdevsw id_cdevsw = { 69 /* open */ idad_open, 70 /* close */ idad_close, 71 /* read */ physread, 72 /* write */ physwrite, 73 /* ioctl */ noioctl, 74 /* poll */ nopoll, 75 /* mmap */ nommap, 76 /* strategy */ idad_strategy, 77 /* name */ "idad", 78 /* maj */ IDAD_CDEV_MAJOR, 79 /* dump */ idad_dump, 80 /* psize */ nopsize, 81 /* flags */ D_DISK, 82 }; 83 84 static devclass_t idad_devclass; 85 static struct cdevsw idaddisk_cdevsw; 86 static int disks_registered = 0; 87 88 static device_method_t idad_methods[] = { 89 DEVMETHOD(device_probe, idad_probe), 90 DEVMETHOD(device_attach, idad_attach), 91 DEVMETHOD(device_detach, idad_detach), 92 { 0, 0 } 93 }; 94 95 static driver_t idad_driver = { 96 "idad", 97 idad_methods, 98 sizeof(struct idad_softc) 99 }; 100 101 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0); 102 103 static __inline struct idad_softc * 104 idad_getsoftc(dev_t dev) 105 { 106 107 return ((struct idad_softc *)dev->si_drv1); 108 } 109 110 static int 111 idad_open(dev_t dev, int flags, int fmt, struct thread *td) 112 { 113 struct idad_softc *drv; 114 struct disklabel *label; 115 116 drv = idad_getsoftc(dev); 117 if (drv == NULL) 118 return (ENXIO); 119 120 label = &drv->disk.d_label; 121 bzero(label, sizeof(*label)); 122 label->d_type = DTYPE_SCSI; 123 label->d_secsize = drv->secsize; 124 label->d_nsectors = drv->sectors; 125 label->d_ntracks = drv->heads; 126 label->d_ncylinders = drv->cylinders; 127 label->d_secpercyl = drv->sectors * drv->heads; 128 label->d_secperunit = drv->secperunit; 129 130 return (0); 131 } 132 133 static int 134 idad_close(dev_t dev, int flags, int fmt, struct thread *td) 135 { 136 struct idad_softc *drv; 137 138 drv = idad_getsoftc(dev); 139 if (drv == NULL) 140 return (ENXIO); 141 return (0); 142 } 143 144 /* 145 * Read/write routine for a buffer. Finds the proper unit, range checks 146 * arguments, and schedules the transfer. Does not wait for the transfer 147 * to complete. Multi-page transfers are supported. All I/O requests must 148 * be a multiple of a sector in length. 149 */ 150 static void 151 idad_strategy(struct bio *bp) 152 { 153 struct idad_softc *drv; 154 int s; 155 156 drv = idad_getsoftc(bp->bio_dev); 157 if (drv == NULL) { 158 bp->bio_error = EINVAL; 159 goto bad; 160 } 161 162 /* 163 * software write protect check 164 */ 165 if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) { 166 bp->bio_error = EROFS; 167 goto bad; 168 } 169 170 bp->bio_driver1 = drv; 171 s = splbio(); 172 devstat_start_transaction(&drv->stats); 173 ida_submit_buf(drv->controller, bp); 174 splx(s); 175 return; 176 177 bad: 178 bp->bio_flags |= BIO_ERROR; 179 180 /* 181 * Correctly set the buf to indicate a completed transfer 182 */ 183 bp->bio_resid = bp->bio_bcount; 184 biodone(bp); 185 return; 186 } 187 188 static int 189 idad_dump(dev_t dev) 190 { 191 struct idad_softc *drv; 192 u_int count, blkno, secsize; 193 long blkcnt; 194 int i, error, dumppages; 195 caddr_t va; 196 vm_offset_t addr, a; 197 198 if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize))) 199 return (error); 200 201 drv = idad_getsoftc(dev); 202 if (drv == NULL) 203 return (ENXIO); 204 205 addr = 0; 206 blkcnt = howmany(PAGE_SIZE, secsize); 207 208 while (count > 0) { 209 va = NULL; 210 211 dumppages = imin(count / blkcnt, MAXDUMPPGS); 212 213 for (i = 0; i < dumppages; i++) { 214 a = addr + (i * PAGE_SIZE); 215 if (is_physical_memory(a)) 216 va = pmap_kenter_temporary(trunc_page(a), i); 217 else 218 va = pmap_kenter_temporary(trunc_page(0), i); 219 } 220 221 error = ida_command(drv->controller, CMD_WRITE, va, 222 PAGE_SIZE * dumppages, drv->drive, blkno, DMA_DATA_OUT); 223 if (error) 224 return (error); 225 226 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0) 227 return (EINTR); 228 229 blkno += blkcnt * dumppages; 230 count -= blkcnt * dumppages; 231 addr += PAGE_SIZE * dumppages; 232 } 233 return (0); 234 } 235 236 void 237 idad_intr(struct bio *bp) 238 { 239 struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1; 240 241 if (bp->bio_flags & BIO_ERROR) 242 bp->bio_error = EIO; 243 else 244 bp->bio_resid = 0; 245 246 biofinish(bp, &drv->stats, 0); 247 } 248 249 static int 250 idad_probe(device_t dev) 251 { 252 253 device_set_desc(dev, "Compaq Logical Drive"); 254 return (0); 255 } 256 257 static int 258 idad_attach(device_t dev) 259 { 260 struct ida_drive_info dinfo; 261 struct idad_softc *drv; 262 device_t parent; 263 dev_t dsk; 264 int error; 265 266 drv = (struct idad_softc *)device_get_softc(dev); 267 parent = device_get_parent(dev); 268 drv->controller = (struct ida_softc *)device_get_softc(parent); 269 drv->unit = device_get_unit(dev); 270 drv->drive = drv->controller->num_drives; 271 drv->controller->num_drives++; 272 273 error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO, 274 &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN); 275 if (error) { 276 device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n"); 277 return (ENXIO); 278 } 279 280 drv->cylinders = dinfo.ncylinders; 281 drv->heads = dinfo.nheads; 282 drv->sectors = dinfo.nsectors; 283 drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize; 284 drv->secperunit = dinfo.secperunit; 285 286 /* XXX 287 * other initialization 288 */ 289 device_printf(dev, "%uMB (%u sectors), blocksize=%d\n", 290 drv->secperunit / ((1024 * 1024) / drv->secsize), 291 drv->secperunit, drv->secsize); 292 293 devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize, 294 DEVSTAT_NO_ORDERED_TAGS, 295 DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER, 296 DEVSTAT_PRIORITY_ARRAY); 297 298 dsk = disk_create(drv->unit, &drv->disk, 0, 299 &id_cdevsw, &idaddisk_cdevsw); 300 301 dsk->si_drv1 = drv; 302 dsk->si_iosize_max = DFLTPHYS; /* XXX guess? */ 303 disks_registered++; 304 305 return (0); 306 } 307 308 static int 309 idad_detach(device_t dev) 310 { 311 struct idad_softc *drv; 312 313 drv = (struct idad_softc *)device_get_softc(dev); 314 devstat_remove_entry(&drv->stats); 315 316 if (--disks_registered == 0) 317 cdevsw_remove(&idaddisk_cdevsw); 318 return (0); 319 } 320