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: src/sys/dev/ida/ida_disk.c,v 1.12.2.6 2001/11/27 20:21:02 ps Exp $ 27 * $DragonFly: src/sys/dev/raid/ida/ida_disk.c,v 1.14 2006/10/25 20:56:01 dillon Exp $ 28 */ 29 30 /* 31 * Disk driver for Compaq SMART 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/cons.h> 43 #include <sys/devicestat.h> 44 #include <sys/disk.h> 45 #include <sys/rman.h> 46 #include <sys/thread2.h> 47 48 #include <machine/clock.h> 49 #include <machine/md_var.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 54 #include "idareg.h" 55 #include "idavar.h" 56 57 /* prototypes */ 58 static int idad_probe(device_t dev); 59 static int idad_attach(device_t dev); 60 static int idad_detach(device_t dev); 61 62 static d_open_t idad_open; 63 static d_close_t idad_close; 64 static d_strategy_t idad_strategy; 65 static d_dump_t idad_dump; 66 67 #define IDAD_BDEV_MAJOR 29 68 #define IDAD_CDEV_MAJOR 109 69 70 static struct dev_ops id_ops = { 71 { "idad", IDAD_CDEV_MAJOR, D_DISK }, 72 .d_open = idad_open, 73 .d_close = idad_close, 74 .d_read = physread, 75 .d_write = physwrite, 76 .d_strategy = idad_strategy, 77 .d_dump = idad_dump, 78 }; 79 80 static devclass_t idad_devclass; 81 82 static device_method_t idad_methods[] = { 83 DEVMETHOD(device_probe, idad_probe), 84 DEVMETHOD(device_attach, idad_attach), 85 DEVMETHOD(device_detach, idad_detach), 86 { 0, 0 } 87 }; 88 89 static driver_t idad_driver = { 90 "idad", 91 idad_methods, 92 sizeof(struct idad_softc) 93 }; 94 95 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0); 96 97 static __inline struct idad_softc * 98 idad_getsoftc(cdev_t dev) 99 { 100 101 return ((struct idad_softc *)dev->si_drv1); 102 } 103 104 static int 105 idad_open(struct dev_open_args *ap) 106 { 107 cdev_t dev = ap->a_head.a_dev; 108 struct idad_softc *drv; 109 struct disklabel *label; 110 111 drv = idad_getsoftc(dev); 112 if (drv == NULL) 113 return (ENXIO); 114 115 label = &drv->disk.d_label; 116 bzero(label, sizeof(*label)); 117 label->d_type = DTYPE_SCSI; 118 label->d_secsize = drv->secsize; 119 label->d_nsectors = drv->sectors; 120 label->d_ntracks = drv->heads; 121 label->d_ncylinders = drv->cylinders; 122 label->d_secpercyl = drv->sectors * drv->heads; 123 label->d_secperunit = drv->secperunit; 124 125 return (0); 126 } 127 128 static int 129 idad_close(struct dev_close_args *ap) 130 { 131 cdev_t dev = ap->a_head.a_dev; 132 struct idad_softc *drv; 133 134 drv = idad_getsoftc(dev); 135 if (drv == NULL) 136 return (ENXIO); 137 return (0); 138 } 139 140 /* 141 * Read/write routine for a buffer. Finds the proper unit, range checks 142 * arguments, and schedules the transfer. Does not wait for the transfer 143 * to complete. Multi-page transfers are supported. All I/O requests must 144 * be a multiple of a sector in length. 145 */ 146 static int 147 idad_strategy(struct dev_strategy_args *ap) 148 { 149 cdev_t dev = ap->a_head.a_dev; 150 struct bio *bio = ap->a_bio; 151 struct buf *bp = bio->bio_buf; 152 struct idad_softc *drv; 153 154 drv = idad_getsoftc(dev); 155 if (drv == NULL) { 156 bp->b_error = EINVAL; 157 goto bad; 158 } 159 160 /* 161 * software write protect check 162 */ 163 if ((drv->flags & DRV_WRITEPROT) && bp->b_cmd != BUF_CMD_READ) { 164 bp->b_error = EROFS; 165 goto bad; 166 } 167 168 /* 169 * If it's a null transfer, return immediately 170 */ 171 if (bp->b_bcount == 0) 172 goto done; 173 174 bio->bio_driver_info = drv; 175 crit_enter(); 176 devstat_start_transaction(&drv->stats); 177 ida_submit_buf(drv->controller, bio); 178 crit_exit(); 179 return(0); 180 181 bad: 182 bp->b_flags |= B_ERROR; 183 184 done: 185 /* 186 * Correctly set the buf to indicate a completed transfer 187 */ 188 bp->b_resid = bp->b_bcount; 189 biodone(bio); 190 return(0); 191 } 192 193 static int 194 idad_dump(struct dev_dump_args *ap) 195 { 196 cdev_t dev = ap->a_head.a_dev; 197 struct idad_softc *drv; 198 long blkcnt; 199 int i, error, dumppages; 200 caddr_t va; 201 vm_offset_t addr, a; 202 203 drv = idad_getsoftc(dev); 204 if (drv == NULL) 205 return (ENXIO); 206 207 addr = 0; 208 blkcnt = howmany(PAGE_SIZE, ap->a_secsize); 209 210 while (ap->a_count > 0) { 211 va = NULL; 212 213 dumppages = imin(ap->a_count / blkcnt, MAXDUMPPGS); 214 215 for (i = 0; i < dumppages; i++) { 216 a = addr + (i * PAGE_SIZE); 217 if (is_physical_memory(a)) 218 va = pmap_kenter_temporary(trunc_page(a), i); 219 else 220 va = pmap_kenter_temporary(trunc_page(0), i); 221 } 222 223 error = ida_command(drv->controller, CMD_WRITE, va, 224 PAGE_SIZE * dumppages, drv->drive, ap->a_blkno, DMA_DATA_OUT); 225 if (error) 226 return (error); 227 228 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0) 229 return (EINTR); 230 231 ap->a_blkno += blkcnt * dumppages; 232 ap->a_count -= blkcnt * dumppages; 233 addr += PAGE_SIZE * dumppages; 234 } 235 return (0); 236 } 237 238 void 239 idad_intr(struct bio *bio) 240 { 241 struct idad_softc *drv = (struct idad_softc *)bio->bio_driver_info; 242 struct buf *bp = bio->bio_buf; 243 244 if (bp->b_flags & B_ERROR) 245 bp->b_error = EIO; 246 else 247 bp->b_resid = 0; 248 249 devstat_end_transaction_buf(&drv->stats, bp); 250 biodone(bio); 251 } 252 253 static int 254 idad_probe(device_t dev) 255 { 256 257 device_set_desc(dev, "Compaq Logical Drive"); 258 return (0); 259 } 260 261 static int 262 idad_attach(device_t dev) 263 { 264 struct ida_drive_info dinfo; 265 struct idad_softc *drv; 266 device_t parent; 267 cdev_t dsk; 268 int error; 269 270 drv = (struct idad_softc *)device_get_softc(dev); 271 parent = device_get_parent(dev); 272 drv->controller = (struct ida_softc *)device_get_softc(parent); 273 drv->unit = device_get_unit(dev); 274 drv->drive = drv->controller->num_drives; 275 drv->controller->num_drives++; 276 277 error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO, 278 &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN); 279 if (error) { 280 device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n"); 281 return (ENXIO); 282 } 283 284 drv->cylinders = dinfo.ncylinders; 285 drv->heads = dinfo.nheads; 286 drv->sectors = dinfo.nsectors; 287 drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize; 288 drv->secperunit = dinfo.secperunit; 289 290 /* XXX 291 * other initialization 292 */ 293 device_printf(dev, "%uMB (%u sectors), blocksize=%d\n", 294 drv->secperunit / ((1024 * 1024) / drv->secsize), 295 drv->secperunit, drv->secsize); 296 297 devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize, 298 DEVSTAT_NO_ORDERED_TAGS, 299 DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER, 300 DEVSTAT_PRIORITY_ARRAY); 301 302 dsk = disk_create(drv->unit, &drv->disk, 0, &id_ops); 303 304 dsk->si_drv1 = drv; 305 dsk->si_iosize_max = DFLTPHYS; /* XXX guess? */ 306 307 return (0); 308 } 309 310 static int 311 idad_detach(device_t dev) 312 { 313 struct idad_softc *drv; 314 315 drv = (struct idad_softc *)device_get_softc(dev); 316 devstat_remove_entry(&drv->stats); 317 disk_destroy(&drv->disk); 318 return (0); 319 } 320