1 /*- 2 * Copyright (c) 1998 - 2006 S�ren Schmidt <sos@FreeBSD.org> 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 * without modification, immediately at the beginning of the file. 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 ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.197 2006/03/31 08:09:04 sos Exp $ 27 * $DragonFly: src/sys/dev/disk/nata/ata-disk.c,v 1.4 2007/06/01 00:31:14 dillon Exp $ 28 */ 29 30 #include "opt_ata.h" 31 32 #include <sys/param.h> 33 #include <sys/bio.h> 34 #include <sys/buf.h> 35 #include <sys/bus.h> 36 #include <sys/device.h> 37 #include <sys/devicestat.h> 38 #include <sys/disk.h> 39 #include <sys/libkern.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/nata.h> 43 #include <sys/systm.h> 44 45 #include <vm/pmap.h> 46 47 #include <machine/md_var.h> 48 49 #include "ata-all.h" 50 #include "ata-disk.h" 51 #include "ata_if.h" 52 53 /* device structure */ 54 static d_open_t ad_open; 55 static d_close_t ad_close; 56 static d_ioctl_t ad_ioctl; 57 static d_strategy_t ad_strategy; 58 static d_dump_t ad_dump; 59 static struct dev_ops ad_ops = { 60 { "ad", 116, D_DISK }, 61 .d_open = ad_open, 62 .d_close = ad_close, 63 .d_read = physread, 64 .d_write = physwrite, 65 .d_ioctl = ad_ioctl, 66 .d_strategy = ad_strategy, 67 .d_dump = ad_dump, 68 }; 69 70 /* prototypes */ 71 static void ad_init(device_t); 72 static void ad_done(struct ata_request *); 73 static void ad_describe(device_t dev); 74 static int ad_version(u_int16_t); 75 76 /* local vars */ 77 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver"); 78 79 static int 80 ad_probe(device_t dev) 81 { 82 struct ata_device *atadev = device_get_softc(dev); 83 84 if (!(atadev->param.config & ATA_PROTO_ATAPI) || 85 (atadev->param.config == ATA_CFA_MAGIC1) || 86 (atadev->param.config == ATA_CFA_MAGIC2)) 87 return 0; 88 else 89 return ENXIO; 90 } 91 92 static int 93 ad_attach(device_t dev) 94 { 95 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 96 struct ata_device *atadev = device_get_softc(dev); 97 struct disk_info info; 98 struct ad_softc *adp; 99 cdev_t cdev; 100 u_int32_t lbasize; 101 u_int64_t lbasize48; 102 103 /* check that we have a virgin disk to attach */ 104 if (device_get_ivars(dev)) 105 return EEXIST; 106 107 /* XXX TGEN We're not in interrupt context, so we can M_WAITOK and remove 108 the OOM check. */ 109 if (!(adp = kmalloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) { 110 device_printf(dev, "out of memory\n"); 111 return ENOMEM; 112 } 113 device_set_ivars(dev, adp); 114 115 if ((atadev->param.atavalid & ATA_FLAG_54_58) && 116 atadev->param.current_heads && atadev->param.current_sectors) { 117 adp->heads = atadev->param.current_heads; 118 adp->sectors = atadev->param.current_sectors; 119 adp->total_secs = (u_int32_t)atadev->param.current_size_1 | 120 ((u_int32_t)atadev->param.current_size_2 << 16); 121 } 122 else { 123 adp->heads = atadev->param.heads; 124 adp->sectors = atadev->param.sectors; 125 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors; 126 } 127 lbasize = (u_int32_t)atadev->param.lba_size_1 | 128 ((u_int32_t)atadev->param.lba_size_2 << 16); 129 130 /* does this device need oldstyle CHS addressing */ 131 if (!ad_version(atadev->param.version_major) || !lbasize) 132 atadev->flags |= ATA_D_USE_CHS; 133 134 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 135 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize) 136 adp->total_secs = lbasize; 137 138 /* use the 48bit LBA size if valid */ 139 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) | 140 ((u_int64_t)atadev->param.lba_size48_2 << 16) | 141 ((u_int64_t)atadev->param.lba_size48_3 << 32) | 142 ((u_int64_t)atadev->param.lba_size48_4 << 48); 143 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) && 144 lbasize48 > ATA_MAX_28BIT_LBA) 145 adp->total_secs = lbasize48; 146 147 /* init device parameters */ 148 ad_init(dev); 149 150 /* create the disk device */ 151 /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, 152 DEVSTAT_PRIORITY_MAX. */ 153 devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE, 154 DEVSTAT_NO_ORDERED_TAGS, 155 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE, 156 DEVSTAT_PRIORITY_DISK); 157 cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops); 158 cdev->si_drv1 = dev; 159 if (ch->dma) 160 cdev->si_iosize_max = ch->dma->max_iosize; 161 else 162 cdev->si_iosize_max = DFLTPHYS; 163 adp->cdev = cdev; 164 165 bzero(&info, sizeof(info)); 166 info.d_media_blksize = DEV_BSIZE; /* mandatory */ 167 info.d_media_blocks = adp->total_secs; 168 169 info.d_secpertrack = adp->sectors; /* optional */ 170 info.d_nheads = adp->heads; 171 info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors); 172 info.d_secpercyl = adp->sectors * adp->heads; 173 174 device_add_child(dev, "subdisk", device_get_unit(dev)); 175 bus_generic_attach(dev); 176 177 /* announce we are here */ 178 ad_describe(dev); 179 180 disk_setdiskinfo(&adp->disk, &info); 181 182 return 0; 183 } 184 185 static int 186 ad_detach(device_t dev) 187 { 188 struct ad_softc *adp = device_get_ivars(dev); 189 device_t *children; 190 int nchildren, i; 191 192 /* check that we have a valid disk to detach */ 193 if (!adp) 194 return ENXIO; 195 196 #if 0 /* XXX TGEN Probably useless, we fail the queue below. */ 197 /* check that the disk is closed */ 198 if (adp->ad_flags & AD_DISK_OPEN) 199 return EBUSY; 200 #endif /* 0 */ 201 202 /* detach & delete all children */ 203 if (!device_get_children(dev, &children, &nchildren)) { 204 for (i = 0; i < nchildren; i++) 205 if (children[i]) 206 device_delete_child(dev, children[i]); 207 kfree(children, M_TEMP); 208 } 209 210 /* detroy disk from the system so we dont get any further requests */ 211 disk_invalidate(&adp->disk); 212 disk_destroy(&adp->disk); 213 214 /* fail requests on the queue and any thats "in flight" for this device */ 215 ata_fail_requests(dev); 216 217 /* dont leave anything behind */ 218 /* disk_destroy() already took care of the dev_ops */ 219 devstat_remove_entry(&adp->stats); 220 device_set_ivars(dev, NULL); 221 kfree(adp, M_AD); 222 return 0; 223 } 224 225 static void 226 ad_shutdown(device_t dev) 227 { 228 struct ata_device *atadev = device_get_softc(dev); 229 230 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 231 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 232 } 233 234 static int 235 ad_reinit(device_t dev) 236 { 237 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 238 struct ata_device *atadev = device_get_softc(dev); 239 240 /* if detach pending, return error */ 241 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) || 242 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) { 243 return 1; 244 } 245 ad_init(dev); 246 return 0; 247 } 248 249 static int 250 ad_open(struct dev_open_args *ap) 251 { 252 device_t dev = ap->a_head.a_dev->si_drv1; 253 struct ad_softc *adp = device_get_ivars(dev); 254 255 if (!adp || adp->cdev == NULL) 256 return ENXIO; 257 if(!device_is_attached(dev)) 258 return EBUSY; 259 260 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 261 adp->ad_flags &= AD_DISK_OPEN; 262 #endif /* 0 */ 263 return 0; 264 } 265 266 static int 267 ad_close(struct dev_close_args *ap) 268 { 269 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 270 device_t dev = ap->a_head.a_dev->si_drv1; 271 struct ad_softc *adp = device_get_ivars(dev); 272 adp->ad_flags |= AD_DISK_OPEN; 273 #endif /* 0 */ 274 return 0; 275 } 276 277 static int 278 ad_ioctl(struct dev_ioctl_args *ap) 279 { 280 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data); 281 } 282 283 static int 284 ad_strategy(struct dev_strategy_args *ap) 285 { 286 device_t dev = ap->a_head.a_dev->si_drv1; 287 struct bio *bp = ap->a_bio; 288 struct buf *bbp = bp->bio_buf; 289 struct ata_device *atadev = device_get_softc(dev); 290 struct ata_request *request; 291 struct ad_softc *adp = device_get_ivars(dev); 292 293 if (!(request = ata_alloc_request())) { 294 device_printf(dev, "FAILURE - out of memory in strategy\n"); 295 bbp->b_flags |= B_ERROR; 296 bbp->b_error = ENOMEM; 297 biodone(bp); 298 return(0); 299 } 300 301 /* setup request */ 302 request->dev = dev; 303 request->bio = bp; 304 request->callback = ad_done; 305 request->timeout = 5; 306 request->retries = 2; 307 request->data = bbp->b_data; 308 request->bytecount = bbp->b_bcount; 309 /* lba is block granularity, convert byte granularity bio_offset */ 310 request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT); 311 request->u.ata.count = request->bytecount / DEV_BSIZE; 312 request->transfersize = min(bbp->b_bcount, atadev->max_iosize); 313 314 switch (bbp->b_cmd) { 315 case BUF_CMD_READ: 316 request->flags = ATA_R_READ; 317 if (atadev->mode >= ATA_DMA) { 318 request->u.ata.command = ATA_READ_DMA; 319 request->flags |= ATA_R_DMA; 320 } 321 else if (request->transfersize > DEV_BSIZE) 322 request->u.ata.command = ATA_READ_MUL; 323 else 324 request->u.ata.command = ATA_READ; 325 break; 326 case BUF_CMD_WRITE: 327 request->flags = ATA_R_WRITE; 328 if (atadev->mode >= ATA_DMA) { 329 request->u.ata.command = ATA_WRITE_DMA; 330 request->flags |= ATA_R_DMA; 331 } 332 else if (request->transfersize > DEV_BSIZE) 333 request->u.ata.command = ATA_WRITE_MUL; 334 else 335 request->u.ata.command = ATA_WRITE; 336 break; 337 #if 0 /* NOT YET */ 338 case BUF_CMD_FLUSH: 339 request->u.ata.lba = 0; 340 request->u.ata.count = 0; 341 request->u.ata.feature = 0; 342 request->bytecount = 0; 343 request->transfersize = 0; 344 request->flags = ATA_R_CONTROL; 345 request->u.ata.command = ATA_FLUSHCACHE; 346 break; 347 #endif 348 default: 349 device_printf(dev, "FAILURE - unknown BUF operation\n"); 350 ata_free_request(request); 351 bbp->b_flags |= B_ERROR; 352 bbp->b_error = EIO; 353 biodone(bp); 354 return(0); 355 } 356 request->flags |= ATA_R_ORDERED; 357 devstat_start_transaction(&adp->stats); 358 ata_queue_request(request); 359 return(0); 360 } 361 362 static void 363 ad_done(struct ata_request *request) 364 { 365 struct ad_softc *adp = device_get_ivars(request->dev); 366 struct bio *bp = request->bio; 367 struct buf *bbp = bp->bio_buf; 368 369 /* finish up transfer */ 370 if ((bbp->b_error = request->result)) 371 bbp->b_flags |= B_ERROR; 372 bbp->b_resid = bbp->b_bcount - request->donecount; 373 devstat_end_transaction_buf(&adp->stats, bbp); 374 biodone(bp); 375 ata_free_request(request); 376 } 377 378 static int 379 ad_dump(struct dev_dump_args *ap) 380 { 381 device_t dev = ap->a_head.a_dev->si_drv1; 382 struct ata_device *atadev = device_get_softc(dev); 383 struct ata_request request; 384 vm_paddr_t addr = 0; 385 long blkcnt; 386 int dumppages = MAXDUMPPGS; 387 int error = 0; 388 int i; 389 390 blkcnt = howmany(PAGE_SIZE, ap->a_secsize); 391 392 while (ap->a_count > 0) { 393 caddr_t va = NULL; 394 395 if ((ap->a_count /blkcnt) < dumppages) 396 dumppages = ap->a_count / blkcnt; 397 398 for (i = 0; i < dumppages; ++i) { 399 vm_paddr_t a = addr + (i * PAGE_SIZE); 400 if (is_physical_memory(a)) 401 va = pmap_kenter_temporary(trunc_page(a), i); 402 else 403 va = pmap_kenter_temporary(trunc_page(0), i); 404 } 405 406 bzero(&request, sizeof(struct ata_request)); 407 request.dev = dev; 408 /* request.bio = NULL; */ 409 request.timeout = 5; 410 request.retries = 2; 411 request.data = va; 412 request.bytecount = dumppages * PAGE_SIZE; 413 request.u.ata.lba = ap->a_blkno; 414 request.u.ata.count = request.bytecount / DEV_BSIZE; 415 request.transfersize = min(request.bytecount, atadev->max_iosize); 416 request.flags = ATA_R_WRITE | ATA_R_AT_HEAD; 417 if (atadev->mode >= ATA_DMA) { 418 request.u.ata.command = ATA_WRITE_DMA; 419 request.flags |= ATA_DMA; 420 } else if (request.transfersize > DEV_BSIZE) 421 request.u.ata.command = ATA_WRITE_MUL; 422 else 423 request.u.ata.command = ATA_WRITE; 424 ata_queue_request(&request); 425 if (request.result) 426 return request.result; 427 428 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0) 429 return EINTR; 430 431 ap->a_blkno += blkcnt * dumppages; 432 ap->a_count -= blkcnt * dumppages; 433 addr += PAGE_SIZE * dumppages; 434 } 435 436 /* flush buffers to media */ 437 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 438 error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 439 return error; 440 } 441 442 static void 443 ad_init(device_t dev) 444 { 445 struct ata_device *atadev = device_get_softc(dev); 446 447 ATA_SETMODE(device_get_parent(dev), dev); 448 449 /* enable readahead caching */ 450 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD) 451 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0); 452 453 /* enable write caching if supported and configured */ 454 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) { 455 if (ata_wc) 456 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0); 457 else 458 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0); 459 } 460 461 /* use multiple sectors/interrupt if device supports it */ 462 if (ad_version(atadev->param.version_major)) { 463 int secsperint = max(1, min(atadev->param.sectors_intr, 16)); 464 465 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint)) 466 atadev->max_iosize = secsperint * DEV_BSIZE; 467 } 468 else 469 atadev->max_iosize = DEV_BSIZE; 470 } 471 472 void 473 ad_describe(device_t dev) 474 { 475 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 476 struct ata_device *atadev = device_get_softc(dev); 477 struct ad_softc *adp = device_get_ivars(dev); 478 u_int8_t *marker, vendor[64], product[64]; 479 480 /* try to seperate the ATA model string into vendor and model parts */ 481 if ((marker = index(atadev->param.model, ' ')) || 482 (marker = index(atadev->param.model, '-'))) { 483 int len = (marker - atadev->param.model); 484 485 strncpy(vendor, atadev->param.model, len); 486 vendor[len++] = 0; 487 strcat(vendor, " "); 488 strncpy(product, atadev->param.model + len, 40 - len); 489 vendor[40 - len] = 0; 490 } 491 else { 492 if (!strncmp(atadev->param.model, "ST", 2)) 493 strcpy(vendor, "Seagate "); 494 else 495 strcpy(vendor, ""); 496 strncpy(product, atadev->param.model, 40); 497 } 498 499 device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n", 500 (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)), 501 vendor, product, atadev->param.revision, 502 device_get_unit(ch->dev), 503 (atadev->unit == ATA_MASTER) ? "master" : "slave", 504 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "", 505 ata_mode2str(atadev->mode)); 506 if (bootverbose) { 507 device_printf(dev, "%llu sectors [%lluC/%dH/%dS] " 508 "%d sectors/interrupt %d depth queue\n", 509 (unsigned long long)adp->total_secs,(unsigned long long)( 510 adp->total_secs / (adp->heads * adp->sectors)), 511 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE, 512 adp->num_tags + 1); 513 } 514 } 515 516 static int 517 ad_version(u_int16_t version) 518 { 519 int bit; 520 521 if (version == 0xffff) 522 return 0; 523 for (bit = 15; bit >= 0; bit--) 524 if (version & (1<<bit)) 525 return bit; 526 return 0; 527 } 528 529 static device_method_t ad_methods[] = { 530 /* device interface */ 531 DEVMETHOD(device_probe, ad_probe), 532 DEVMETHOD(device_attach, ad_attach), 533 DEVMETHOD(device_detach, ad_detach), 534 DEVMETHOD(device_shutdown, ad_shutdown), 535 536 /* ATA methods */ 537 DEVMETHOD(ata_reinit, ad_reinit), 538 539 { 0, 0 } 540 }; 541 542 static driver_t ad_driver = { 543 "ad", 544 ad_methods, 545 0, 546 }; 547 548 devclass_t ad_devclass; 549 550 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL); 551 MODULE_VERSION(ad, 1); 552 MODULE_DEPEND(ad, ata, 1, 1, 1); 553