1 /*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2001 Scott Long 4 * Copyright (c) 2000 BSDi 5 * Copyright (c) 2001 Adaptec, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/aac/aac_disk.c,v 1.3.2.8 2003/01/11 18:39:39 scottl Exp $ 30 * $DragonFly: src/sys/dev/raid/aac/aac_disk.c,v 1.16 2006/12/22 23:26:23 swildner Exp $ 31 */ 32 33 #include "opt_aac.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/sysctl.h> 39 40 #include "aac_compat.h" 41 #include <sys/bus.h> 42 #include <sys/conf.h> 43 #include <sys/devicestat.h> 44 #include <sys/disk.h> 45 #include <sys/rman.h> 46 47 #include <vm/vm.h> 48 #include <vm/pmap.h> 49 50 #include <machine/md_var.h> 51 52 #include "aacreg.h" 53 #include "aac_ioctl.h" 54 #include "aacvar.h" 55 56 /* 57 * Interface to parent. 58 */ 59 static int aac_disk_probe(device_t dev); 60 static int aac_disk_attach(device_t dev); 61 static int aac_disk_detach(device_t dev); 62 63 /* 64 * Interface to the device switch. 65 */ 66 static d_open_t aac_disk_open; 67 static d_close_t aac_disk_close; 68 static d_strategy_t aac_disk_strategy; 69 static d_dump_t aac_disk_dump; 70 71 #define AAC_DISK_CDEV_MAJOR 151 72 73 static struct dev_ops aac_disk_ops = { 74 { "aacd", AAC_DISK_CDEV_MAJOR, D_DISK }, 75 .d_open = aac_disk_open, 76 .d_close = aac_disk_close, 77 .d_read = physread, 78 .d_write = physwrite, 79 .d_strategy = aac_disk_strategy, 80 .d_dump = aac_disk_dump, 81 }; 82 83 devclass_t aac_disk_devclass; 84 85 static device_method_t aac_disk_methods[] = { 86 DEVMETHOD(device_probe, aac_disk_probe), 87 DEVMETHOD(device_attach, aac_disk_attach), 88 DEVMETHOD(device_detach, aac_disk_detach), 89 { 0, 0 } 90 }; 91 92 static driver_t aac_disk_driver = { 93 "aacd", 94 aac_disk_methods, 95 sizeof(struct aac_disk) 96 }; 97 98 #define AAC_MAXIO 65536 99 100 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0); 101 102 /* sysctl tunables */ 103 static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */ 104 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max); 105 106 SYSCTL_DECL(_hw_aac); 107 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0, 108 "Max I/O size per transfer to an array"); 109 110 /* 111 * Handle open from generic layer. 112 * 113 * This is called by the diskslice code on first open in order to get the 114 * basic device geometry paramters. 115 */ 116 static int 117 aac_disk_open(struct dev_open_args *ap) 118 { 119 cdev_t dev = ap->a_head.a_dev; 120 struct aac_disk *sc; 121 struct disklabel *label; 122 123 debug_called(0); 124 125 sc = (struct aac_disk *)dev->si_drv1; 126 127 if (sc == NULL) { 128 kprintf("aac_disk_open: No Softc\n"); 129 return (ENXIO); 130 } 131 132 /* check that the controller is up and running */ 133 if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) { 134 kprintf("Controller Suspended controller state = 0x%x\n", 135 sc->ad_controller->aac_state); 136 return(ENXIO); 137 } 138 139 /* build synthetic label */ 140 label = &sc->ad_disk.d_label; 141 bzero(label, sizeof(*label)); 142 label->d_type = DTYPE_ESDI; 143 label->d_secsize = AAC_BLOCK_SIZE; 144 label->d_nsectors = sc->ad_sectors; 145 label->d_ntracks = sc->ad_heads; 146 label->d_ncylinders = sc->ad_cylinders; 147 label->d_secpercyl = sc->ad_sectors * sc->ad_heads; 148 label->d_secperunit = sc->ad_size; 149 150 sc->ad_flags |= AAC_DISK_OPEN; 151 return (0); 152 } 153 154 /* 155 * Handle last close of the disk device. 156 */ 157 static int 158 aac_disk_close(struct dev_close_args *ap) 159 { 160 cdev_t dev = ap->a_head.a_dev; 161 struct aac_disk *sc; 162 163 debug_called(0); 164 165 sc = (struct aac_disk *)dev->si_drv1; 166 167 if (sc == NULL) 168 return (ENXIO); 169 170 sc->ad_flags &= ~AAC_DISK_OPEN; 171 return (0); 172 } 173 174 /* 175 * Handle an I/O request. 176 */ 177 static int 178 aac_disk_strategy(struct dev_strategy_args *ap) 179 { 180 cdev_t dev = ap->a_head.a_dev; 181 struct bio *bio = ap->a_bio; 182 struct buf *bp = bio->bio_buf; 183 struct aac_disk *sc; 184 185 debug_called(4); 186 187 sc = (struct aac_disk *)dev->si_drv1; 188 189 /* bogus disk? */ 190 if (sc == NULL) { 191 bp->b_flags |= B_ERROR; 192 bp->b_error = EINVAL; 193 biodone(bio); 194 return(0); 195 } 196 197 /* do-nothing operation? */ 198 if (bp->b_bcount == 0) { 199 bp->b_resid = bp->b_bcount; 200 biodone(bio); 201 return(0); 202 } 203 204 /* perform accounting */ 205 devstat_start_transaction(&sc->ad_stats); 206 207 /* pass the bio to the controller - it can work out who we are */ 208 aac_submit_bio(sc, bio); 209 return(0); 210 } 211 212 /* 213 * Dump memory out to an array 214 * 215 * This queues blocks of memory of size AAC_MAXIO to the controller and waits 216 * for the controller to complete the requests. 217 */ 218 static int 219 aac_disk_dump(struct dev_dump_args *ap) 220 { 221 cdev_t dev = ap->a_head.a_dev; 222 struct aac_disk *ad; 223 struct aac_softc *sc; 224 vm_offset_t addr; 225 long blkcnt; 226 int dumppages; 227 int i, error; 228 229 ad = dev->si_drv1; 230 addr = 0; 231 dumppages = AAC_MAXIO / PAGE_SIZE; 232 233 if (ad == NULL) 234 return (ENXIO); 235 236 sc= ad->ad_controller; 237 238 blkcnt = howmany(PAGE_SIZE, ap->a_secsize); 239 240 while (ap->a_count > 0) { 241 caddr_t va = NULL; 242 243 if ((ap->a_count / blkcnt) < dumppages) 244 dumppages = ap->a_count / blkcnt; 245 246 for (i = 0; i < dumppages; ++i) { 247 vm_offset_t a = addr + (i * PAGE_SIZE); 248 if (is_physical_memory(a)) { 249 va = pmap_kenter_temporary(trunc_page(a), i); 250 } else { 251 va = pmap_kenter_temporary(trunc_page(0), i); 252 } 253 } 254 255 retry: 256 /* 257 * Queue the block to the controller. If the queue is full, 258 * EBUSY will be returned. 259 */ 260 error = aac_dump_enqueue(ad, ap->a_blkno, va, dumppages); 261 if (error && (error != EBUSY)) 262 return (error); 263 264 if (!error) { 265 if (dumpstatus(addr, (off_t)(ap->a_count * DEV_BSIZE)) < 0) 266 return (EINTR); 267 268 ap->a_blkno += blkcnt * dumppages; 269 ap->a_count -= blkcnt * dumppages; 270 addr += PAGE_SIZE * dumppages; 271 if (ap->a_count > 0) 272 continue; 273 } 274 275 /* 276 * Either the queue was full on the last attemp, or we have no 277 * more data to dump. Let the queue drain out and retry the 278 * block if the queue was full. 279 */ 280 aac_dump_complete(sc); 281 282 if (error == EBUSY) 283 goto retry; 284 } 285 286 return (0); 287 } 288 289 /* 290 * Handle completion of an I/O request. 291 */ 292 void 293 aac_biodone(struct bio *bio, const char *code) 294 { 295 struct buf *bp = bio->bio_buf; 296 struct aac_disk *sc; 297 298 debug_called(4); 299 300 sc = (struct aac_disk *)bio->bio_driver_info; 301 302 devstat_end_transaction_buf(&sc->ad_stats, bp); 303 if (bp->b_flags & B_ERROR) { 304 diskerr(bio, sc->ad_dev_t, 305 code, 0, 0, &sc->ad_label); 306 } 307 biodone(bio); 308 } 309 310 /* 311 * Stub only. 312 */ 313 static int 314 aac_disk_probe(device_t dev) 315 { 316 317 debug_called(2); 318 319 return (0); 320 } 321 322 /* 323 * Attach a unit to the controller. 324 */ 325 static int 326 aac_disk_attach(device_t dev) 327 { 328 struct aac_disk *sc; 329 330 debug_called(0); 331 332 sc = (struct aac_disk *)device_get_softc(dev); 333 334 /* initialise our softc */ 335 sc->ad_controller = 336 (struct aac_softc *)device_get_softc(device_get_parent(dev)); 337 sc->ad_container = device_get_ivars(dev); 338 sc->ad_dev = dev; 339 340 /* 341 * require that extended translation be enabled - other drivers read the 342 * disk! 343 */ 344 sc->ad_size = sc->ad_container->co_mntobj.Capacity; 345 if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */ 346 sc->ad_heads = 255; 347 sc->ad_sectors = 63; 348 } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */ 349 sc->ad_heads = 128; 350 sc->ad_sectors = 32; 351 } else { 352 sc->ad_heads = 64; 353 sc->ad_sectors = 32; 354 } 355 sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors)); 356 357 device_printf(dev, "%uMB (%u sectors)\n", 358 sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE), 359 sc->ad_size); 360 361 devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev), 362 AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS, 363 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 364 DEVSTAT_PRIORITY_ARRAY); 365 366 /* attach a generic disk device to ourselves */ 367 sc->ad_dev_t = disk_create(device_get_unit(dev), &sc->ad_disk, 0, 368 &aac_disk_ops); 369 sc->ad_dev_t->si_drv1 = sc; 370 371 sc->ad_dev_t->si_iosize_max = aac_iosize_max; 372 sc->unit = device_get_unit(dev); 373 374 return (0); 375 } 376 377 /* 378 * Disconnect ourselves from the system. 379 */ 380 static int 381 aac_disk_detach(device_t dev) 382 { 383 struct aac_disk *sc; 384 385 debug_called(2); 386 387 sc = (struct aac_disk *)device_get_softc(dev); 388 389 if (sc->ad_flags & AAC_DISK_OPEN) 390 return(EBUSY); 391 392 devstat_remove_entry(&sc->ad_stats); 393 disk_destroy(&sc->ad_disk); 394 return(0); 395 } 396