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