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.199 2006/09/14 19:12:29 sos Exp $ 27 * $DragonFly: src/sys/dev/disk/nata/ata-disk.c,v 1.6 2007/06/05 18:30:40 swildner 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 adp = kmalloc(sizeof(struct ad_softc), M_AD, M_INTWAIT | M_ZERO); 108 device_set_ivars(dev, adp); 109 110 if ((atadev->param.atavalid & ATA_FLAG_54_58) && 111 atadev->param.current_heads && atadev->param.current_sectors) { 112 adp->heads = atadev->param.current_heads; 113 adp->sectors = atadev->param.current_sectors; 114 adp->total_secs = (u_int32_t)atadev->param.current_size_1 | 115 ((u_int32_t)atadev->param.current_size_2 << 16); 116 } 117 else { 118 adp->heads = atadev->param.heads; 119 adp->sectors = atadev->param.sectors; 120 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors; 121 } 122 lbasize = (u_int32_t)atadev->param.lba_size_1 | 123 ((u_int32_t)atadev->param.lba_size_2 << 16); 124 125 /* does this device need oldstyle CHS addressing */ 126 if (!ad_version(atadev->param.version_major) || !lbasize) 127 atadev->flags |= ATA_D_USE_CHS; 128 129 /* use the 28bit LBA size if valid or bigger than the CHS mapping */ 130 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize) 131 adp->total_secs = lbasize; 132 133 /* use the 48bit LBA size if valid */ 134 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) | 135 ((u_int64_t)atadev->param.lba_size48_2 << 16) | 136 ((u_int64_t)atadev->param.lba_size48_3 << 32) | 137 ((u_int64_t)atadev->param.lba_size48_4 << 48); 138 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) && 139 lbasize48 > ATA_MAX_28BIT_LBA) 140 adp->total_secs = lbasize48; 141 142 /* init device parameters */ 143 ad_init(dev); 144 145 /* create the disk device */ 146 /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, 147 DEVSTAT_PRIORITY_MAX. */ 148 devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE, 149 DEVSTAT_NO_ORDERED_TAGS, 150 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE, 151 DEVSTAT_PRIORITY_DISK); 152 cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops); 153 cdev->si_drv1 = dev; 154 if (ch->dma) 155 cdev->si_iosize_max = ch->dma->max_iosize; 156 else 157 cdev->si_iosize_max = DFLTPHYS; 158 adp->cdev = cdev; 159 160 bzero(&info, sizeof(info)); 161 info.d_media_blksize = DEV_BSIZE; /* mandatory */ 162 info.d_media_blocks = adp->total_secs; 163 164 info.d_secpertrack = adp->sectors; /* optional */ 165 info.d_nheads = adp->heads; 166 info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors); 167 info.d_secpercyl = adp->sectors * adp->heads; 168 169 device_add_child(dev, "subdisk", device_get_unit(dev)); 170 bus_generic_attach(dev); 171 172 /* announce we are here */ 173 ad_describe(dev); 174 175 disk_setdiskinfo(&adp->disk, &info); 176 177 return 0; 178 } 179 180 static int 181 ad_detach(device_t dev) 182 { 183 struct ad_softc *adp = device_get_ivars(dev); 184 device_t *children; 185 int nchildren, i; 186 187 /* check that we have a valid disk to detach */ 188 if (!adp) 189 return ENXIO; 190 191 #if 0 /* XXX TGEN Probably useless, we fail the queue below. */ 192 /* check that the disk is closed */ 193 if (adp->ad_flags & AD_DISK_OPEN) 194 return EBUSY; 195 #endif /* 0 */ 196 197 /* detach & delete all children */ 198 if (!device_get_children(dev, &children, &nchildren)) { 199 for (i = 0; i < nchildren; i++) 200 if (children[i]) 201 device_delete_child(dev, children[i]); 202 kfree(children, M_TEMP); 203 } 204 205 /* detroy disk from the system so we dont get any further requests */ 206 disk_invalidate(&adp->disk); 207 disk_destroy(&adp->disk); 208 209 /* fail requests on the queue and any thats "in flight" for this device */ 210 ata_fail_requests(dev); 211 212 /* dont leave anything behind */ 213 /* disk_destroy() already took care of the dev_ops */ 214 devstat_remove_entry(&adp->stats); 215 device_set_ivars(dev, NULL); 216 kfree(adp, M_AD); 217 return 0; 218 } 219 220 static void 221 ad_shutdown(device_t dev) 222 { 223 struct ata_device *atadev = device_get_softc(dev); 224 225 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 226 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 227 } 228 229 static int 230 ad_reinit(device_t dev) 231 { 232 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 233 struct ata_device *atadev = device_get_softc(dev); 234 235 /* if detach pending, return error */ 236 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) || 237 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) { 238 return 1; 239 } 240 ad_init(dev); 241 return 0; 242 } 243 244 static int 245 ad_open(struct dev_open_args *ap) 246 { 247 device_t dev = ap->a_head.a_dev->si_drv1; 248 struct ad_softc *adp = device_get_ivars(dev); 249 250 if (!adp || adp->cdev == NULL) 251 return ENXIO; 252 if(!device_is_attached(dev)) 253 return EBUSY; 254 255 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 256 adp->ad_flags &= AD_DISK_OPEN; 257 #endif /* 0 */ 258 return 0; 259 } 260 261 static int 262 ad_close(struct dev_close_args *ap) 263 { 264 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */ 265 device_t dev = ap->a_head.a_dev->si_drv1; 266 struct ad_softc *adp = device_get_ivars(dev); 267 adp->ad_flags |= AD_DISK_OPEN; 268 #endif /* 0 */ 269 return 0; 270 } 271 272 static int 273 ad_ioctl(struct dev_ioctl_args *ap) 274 { 275 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data); 276 } 277 278 static int 279 ad_strategy(struct dev_strategy_args *ap) 280 { 281 device_t dev = ap->a_head.a_dev->si_drv1; 282 struct bio *bp = ap->a_bio; 283 struct buf *bbp = bp->bio_buf; 284 struct ata_device *atadev = device_get_softc(dev); 285 struct ata_request *request; 286 struct ad_softc *adp = device_get_ivars(dev); 287 288 if (!(request = ata_alloc_request())) { 289 device_printf(dev, "FAILURE - out of memory in strategy\n"); 290 bbp->b_flags |= B_ERROR; 291 bbp->b_error = ENOMEM; 292 biodone(bp); 293 return(0); 294 } 295 296 /* setup request */ 297 request->dev = dev; 298 request->bio = bp; 299 request->callback = ad_done; 300 request->timeout = 5; 301 request->retries = 2; 302 request->data = bbp->b_data; 303 request->bytecount = bbp->b_bcount; 304 /* lba is block granularity, convert byte granularity bio_offset */ 305 request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT); 306 request->u.ata.count = request->bytecount / DEV_BSIZE; 307 request->transfersize = min(bbp->b_bcount, atadev->max_iosize); 308 309 switch (bbp->b_cmd) { 310 case BUF_CMD_READ: 311 request->flags = ATA_R_READ; 312 if (atadev->mode >= ATA_DMA) { 313 request->u.ata.command = ATA_READ_DMA; 314 request->flags |= ATA_R_DMA; 315 } 316 else if (request->transfersize > DEV_BSIZE) 317 request->u.ata.command = ATA_READ_MUL; 318 else 319 request->u.ata.command = ATA_READ; 320 break; 321 case BUF_CMD_WRITE: 322 request->flags = ATA_R_WRITE; 323 if (atadev->mode >= ATA_DMA) { 324 request->u.ata.command = ATA_WRITE_DMA; 325 request->flags |= ATA_R_DMA; 326 } 327 else if (request->transfersize > DEV_BSIZE) 328 request->u.ata.command = ATA_WRITE_MUL; 329 else 330 request->u.ata.command = ATA_WRITE; 331 break; 332 #if 0 /* NOT YET */ 333 case BUF_CMD_FLUSH: 334 request->u.ata.lba = 0; 335 request->u.ata.count = 0; 336 request->u.ata.feature = 0; 337 request->bytecount = 0; 338 request->transfersize = 0; 339 request->flags = ATA_R_CONTROL; 340 request->u.ata.command = ATA_FLUSHCACHE; 341 break; 342 #endif 343 default: 344 device_printf(dev, "FAILURE - unknown BUF operation\n"); 345 ata_free_request(request); 346 bbp->b_flags |= B_ERROR; 347 bbp->b_error = EIO; 348 biodone(bp); 349 return(0); 350 } 351 request->flags |= ATA_R_ORDERED; 352 devstat_start_transaction(&adp->stats); 353 ata_queue_request(request); 354 return(0); 355 } 356 357 static void 358 ad_done(struct ata_request *request) 359 { 360 struct ad_softc *adp = device_get_ivars(request->dev); 361 struct bio *bp = request->bio; 362 struct buf *bbp = bp->bio_buf; 363 364 /* finish up transfer */ 365 if ((bbp->b_error = request->result)) 366 bbp->b_flags |= B_ERROR; 367 bbp->b_resid = bbp->b_bcount - request->donecount; 368 devstat_end_transaction_buf(&adp->stats, bbp); 369 biodone(bp); 370 ata_free_request(request); 371 } 372 373 static int 374 ad_dump(struct dev_dump_args *ap) 375 { 376 device_t dev = ap->a_head.a_dev->si_drv1; 377 struct ata_device *atadev = device_get_softc(dev); 378 struct ata_request request; 379 vm_paddr_t addr = 0; 380 long blkcnt; 381 int dumppages = MAXDUMPPGS; 382 int error = 0; 383 int i; 384 385 blkcnt = howmany(PAGE_SIZE, ap->a_secsize); 386 387 while (ap->a_count > 0) { 388 caddr_t va = NULL; 389 390 if ((ap->a_count /blkcnt) < dumppages) 391 dumppages = ap->a_count / blkcnt; 392 393 for (i = 0; i < dumppages; ++i) { 394 vm_paddr_t a = addr + (i * PAGE_SIZE); 395 if (is_physical_memory(a)) 396 va = pmap_kenter_temporary(trunc_page(a), i); 397 else 398 va = pmap_kenter_temporary(trunc_page(0), i); 399 } 400 401 bzero(&request, sizeof(struct ata_request)); 402 request.dev = dev; 403 /* request.bio = NULL; */ 404 request.timeout = 5; 405 request.retries = 2; 406 request.data = va; 407 request.bytecount = dumppages * PAGE_SIZE; 408 request.u.ata.lba = ap->a_blkno; 409 request.u.ata.count = request.bytecount / DEV_BSIZE; 410 request.transfersize = min(request.bytecount, atadev->max_iosize); 411 request.flags = ATA_R_WRITE | ATA_R_AT_HEAD; 412 if (atadev->mode >= ATA_DMA) { 413 request.u.ata.command = ATA_WRITE_DMA; 414 request.flags |= ATA_DMA; 415 } else if (request.transfersize > DEV_BSIZE) 416 request.u.ata.command = ATA_WRITE_MUL; 417 else 418 request.u.ata.command = ATA_WRITE; 419 ata_queue_request(&request); 420 if (request.result) 421 return request.result; 422 423 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0) 424 return EINTR; 425 426 ap->a_blkno += blkcnt * dumppages; 427 ap->a_count -= blkcnt * dumppages; 428 addr += PAGE_SIZE * dumppages; 429 } 430 431 /* flush buffers to media */ 432 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 433 error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 434 return error; 435 } 436 437 static void 438 ad_init(device_t dev) 439 { 440 struct ata_device *atadev = device_get_softc(dev); 441 442 ATA_SETMODE(device_get_parent(dev), dev); 443 444 /* enable readahead caching */ 445 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD) 446 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0); 447 448 /* enable write caching if supported and configured */ 449 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) { 450 if (ata_wc) 451 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0); 452 else 453 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0); 454 } 455 456 /* use multiple sectors/interrupt if device supports it */ 457 if (ad_version(atadev->param.version_major)) { 458 int secsperint = max(1, min(atadev->param.sectors_intr, 16)); 459 460 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint)) 461 atadev->max_iosize = secsperint * DEV_BSIZE; 462 } 463 else 464 atadev->max_iosize = DEV_BSIZE; 465 } 466 467 void 468 ad_describe(device_t dev) 469 { 470 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 471 struct ata_device *atadev = device_get_softc(dev); 472 struct ad_softc *adp = device_get_ivars(dev); 473 u_int8_t *marker, vendor[64], product[64]; 474 475 /* try to seperate the ATA model string into vendor and model parts */ 476 if ((marker = index(atadev->param.model, ' ')) || 477 (marker = index(atadev->param.model, '-'))) { 478 int len = (marker - atadev->param.model); 479 480 strncpy(vendor, atadev->param.model, len); 481 vendor[len++] = 0; 482 strcat(vendor, " "); 483 strncpy(product, atadev->param.model + len, 40 - len); 484 vendor[40 - len] = 0; 485 } 486 else { 487 if (!strncmp(atadev->param.model, "ST", 2)) 488 strcpy(vendor, "Seagate "); 489 else 490 strcpy(vendor, ""); 491 strncpy(product, atadev->param.model, 40); 492 } 493 494 device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n", 495 (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)), 496 vendor, product, atadev->param.revision, 497 device_get_unit(ch->dev), 498 (atadev->unit == ATA_MASTER) ? "master" : "slave", 499 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "", 500 ata_mode2str(atadev->mode)); 501 if (bootverbose) { 502 device_printf(dev, "%llu sectors [%lluC/%dH/%dS] " 503 "%d sectors/interrupt %d depth queue\n", 504 (unsigned long long)adp->total_secs,(unsigned long long)( 505 adp->total_secs / (adp->heads * adp->sectors)), 506 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE, 507 adp->num_tags + 1); 508 } 509 } 510 511 static int 512 ad_version(u_int16_t version) 513 { 514 int bit; 515 516 if (version == 0xffff) 517 return 0; 518 for (bit = 15; bit >= 0; bit--) 519 if (version & (1<<bit)) 520 return bit; 521 return 0; 522 } 523 524 static device_method_t ad_methods[] = { 525 /* device interface */ 526 DEVMETHOD(device_probe, ad_probe), 527 DEVMETHOD(device_attach, ad_attach), 528 DEVMETHOD(device_detach, ad_detach), 529 DEVMETHOD(device_shutdown, ad_shutdown), 530 531 /* ATA methods */ 532 DEVMETHOD(ata_reinit, ad_reinit), 533 534 { 0, 0 } 535 }; 536 537 static driver_t ad_driver = { 538 "ad", 539 ad_methods, 540 0, 541 }; 542 543 devclass_t ad_devclass; 544 545 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL); 546 MODULE_VERSION(ad, 1); 547 MODULE_DEPEND(ad, ata, 1, 1, 1); 548