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/atapi-fd.c,v 1.109 2006/03/30 05:29:57 marcel Exp $ 27 * $DragonFly: src/sys/dev/disk/nata/atapi-fd.c,v 1.2 2006/12/20 18:14:38 dillon Exp $ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/buf.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/device.h> 36 #include <sys/devicestat.h> 37 #include <sys/disk.h> 38 #include <sys/endian.h> 39 #include <sys/kernel.h> 40 #include <sys/libkern.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/nata.h> 44 #include <sys/systm.h> 45 46 #include "ata-all.h" 47 #include "atapi-fd.h" 48 #include "ata_if.h" 49 50 /* device structure */ 51 static d_open_t afd_open; 52 static d_close_t afd_close; 53 static d_ioctl_t afd_ioctl; 54 static d_strategy_t afd_strategy; 55 static struct dev_ops afd_ops = { 56 { "afd", 118, D_DISK | D_TRACKCLOSE }, 57 .d_open = afd_open, 58 .d_close = afd_close, 59 .d_read = physread, 60 .d_write = physwrite, 61 .d_ioctl = afd_ioctl, 62 .d_strategy = afd_strategy, 63 }; 64 65 /* prototypes */ 66 static int afd_sense(device_t); 67 static void afd_describe(device_t); 68 static void afd_done(struct ata_request *); 69 static int afd_prevent_allow(device_t, int); 70 static int afd_test_ready(device_t); 71 72 /* internal vars */ 73 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers"); 74 75 static int 76 afd_probe(device_t dev) 77 { 78 struct ata_device *atadev = device_get_softc(dev); 79 if ((atadev->param.config & ATA_PROTO_ATAPI) && 80 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT) 81 return 0; 82 else 83 return ENXIO; 84 } 85 86 static int 87 afd_attach(device_t dev) 88 { 89 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 90 struct ata_device *atadev = device_get_softc(dev); 91 struct afd_softc *fdp; 92 cdev_t cdev; 93 94 /* XXX TGEN We're not in interrupt context, so we can M_WAITOK and remove 95 the OOM check. */ 96 if (!(fdp = kmalloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO))) { 97 device_printf(dev, "out of memory\n"); 98 return ENOMEM; 99 } 100 device_set_ivars(dev, fdp); 101 ATA_SETMODE(device_get_parent(dev), dev); 102 103 if (afd_sense(dev)) { 104 device_set_ivars(dev, NULL); 105 kfree(fdp, M_AFD); 106 return ENXIO; 107 } 108 atadev->flags |= ATA_D_MEDIA_CHANGED; 109 110 /* create the disk device */ 111 devstat_add_entry(&fdp->stats, "afd", device_get_unit(dev), DEV_BSIZE, 112 DEVSTAT_NO_ORDERED_TAGS, DEVSTAT_TYPE_DIRECT | 113 DEVSTAT_TYPE_IF_IDE, DEVSTAT_PRIORITY_WFD); 114 cdev = disk_create(device_get_unit(dev), &fdp->disk, 0, &afd_ops); 115 cdev->si_drv1 = dev; 116 if (ch->dma) 117 cdev->si_iosize_max = ch->dma->max_iosize; 118 else 119 cdev->si_iosize_max = DFLTPHYS; 120 fdp->cdev = cdev; 121 122 /* announce we are here */ 123 afd_describe(dev); 124 return 0; 125 } 126 127 static int 128 afd_detach(device_t dev) 129 { 130 struct afd_softc *fdp = device_get_ivars(dev); 131 132 /* check that we have a valid device to detach */ 133 if (!device_get_ivars(dev)) 134 return ENXIO; 135 136 /* detroy disk from the system so we dont get any further requests */ 137 disk_invalidate(&fdp->disk); 138 disk_destroy(&fdp->disk); 139 140 /* fail requests on the queue and any thats "in flight" for this device */ 141 ata_fail_requests(dev); 142 143 /* dont leave anything behind */ 144 /* disk_destroy() already took care of the dev_ops */ 145 devstat_remove_entry(&fdp->stats); 146 device_set_ivars(dev, NULL); 147 kfree(fdp, M_AFD); 148 return 0; 149 } 150 151 static void 152 afd_shutdown(device_t dev) 153 { 154 struct ata_device *atadev = device_get_softc(dev); 155 156 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE) 157 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0); 158 } 159 160 static int 161 afd_reinit(device_t dev) 162 { 163 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 164 struct ata_device *atadev = device_get_softc(dev); 165 struct afd_softc *fdp = device_get_ivars(dev); 166 167 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) || 168 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) { 169 device_set_ivars(dev, NULL); 170 kfree(fdp, M_AFD); 171 return 1; 172 } 173 ATA_SETMODE(device_get_parent(dev), dev); 174 return 0; 175 } 176 177 static int 178 afd_open(struct dev_open_args *ap) 179 { 180 device_t dev = ap->a_head.a_dev->si_drv1; 181 struct ata_device *atadev = device_get_softc(dev); 182 struct afd_softc *fdp = device_get_ivars(dev); 183 struct disklabel *label = &fdp->disk.d_label; 184 185 if (!fdp) 186 return ENXIO; 187 if (!device_is_attached(dev)) 188 return EBUSY; 189 190 afd_test_ready(dev); 191 afd_prevent_allow(dev, 1); 192 193 if (afd_sense(dev)) 194 device_printf(dev, "sense media type failed\n"); 195 atadev->flags &= ~ATA_D_MEDIA_CHANGED; 196 197 if (!fdp->mediasize) 198 return ENXIO; 199 200 bzero(label, sizeof(*label)); 201 label->d_secsize = fdp->sectorsize; 202 label->d_nsectors = fdp->sectors; 203 label->d_ntracks = fdp->heads; 204 label->d_ncylinders = 205 ((fdp->mediasize/fdp->sectorsize)/fdp->sectors)/fdp->heads; 206 label->d_secpercyl = fdp->sectors * fdp->heads; 207 label->d_secperunit = fdp->mediasize/fdp->sectorsize; 208 return 0; 209 } 210 211 static int 212 afd_close(struct dev_close_args *ap) 213 { 214 device_t dev = ap->a_head.a_dev->si_drv1; 215 struct afd_softc *fdp = device_get_ivars(dev); 216 217 if (count_dev(fdp->cdev) == 1) 218 afd_prevent_allow(dev, 0); 219 return 0; 220 } 221 222 static int 223 afd_ioctl(struct dev_ioctl_args *ap) 224 { 225 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data); 226 } 227 228 static int 229 afd_strategy(struct dev_strategy_args *ap) 230 { 231 device_t dev = ap->a_head.a_dev->si_drv1; 232 struct bio *bp = ap->a_bio; 233 struct buf *bbp = bp->bio_buf; 234 struct ata_device *atadev = device_get_softc(dev); 235 struct afd_softc *fdp = device_get_ivars(dev); 236 struct ata_request *request; 237 u_int32_t lba; 238 u_int16_t count; 239 int8_t ccb[16]; 240 241 /* if it's a null transfer, return immediatly. */ 242 if (bbp->b_bcount == 0) { 243 bbp->b_resid = 0; 244 biodone(bp); 245 return 0; 246 } 247 248 /* should reject all queued entries if media have changed. */ 249 if (atadev->flags & ATA_D_MEDIA_CHANGED) { 250 bbp->b_flags |= B_ERROR; 251 bbp->b_error = EIO; 252 biodone(bp); 253 return 0; 254 } 255 256 lba = bp->bio_offset / fdp->sectorsize; 257 count = bbp->b_bcount / fdp->sectorsize; 258 bbp->b_resid = bbp->b_bcount; 259 260 bzero(ccb, sizeof(ccb)); 261 262 if (bbp->b_cmd == BUF_CMD_READ) 263 ccb[0] = ATAPI_READ_BIG; 264 else 265 ccb[0] = ATAPI_WRITE_BIG; 266 267 ccb[2] = lba >> 24; 268 ccb[3] = lba >> 16; 269 ccb[4] = lba >> 8; 270 ccb[5] = lba; 271 ccb[7] = count>>8; 272 ccb[8] = count; 273 274 if (!(request = ata_alloc_request())) { 275 bbp->b_flags |= B_ERROR; 276 bbp->b_error = ENOMEM; 277 biodone(bp); 278 return 0; 279 } 280 request->dev = dev; 281 request->bio = bp; 282 bcopy(ccb, request->u.atapi.ccb, 283 (atadev->param.config & ATA_PROTO_MASK) == 284 ATA_PROTO_ATAPI_12 ? 16 : 12); 285 request->data = bbp->b_data; 286 request->bytecount = count * fdp->sectorsize; 287 request->transfersize = min(request->bytecount, 65534); 288 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30; 289 request->retries = 2; 290 request->callback = afd_done; 291 switch (bbp->b_cmd) { 292 case BUF_CMD_READ: 293 request->flags = (ATA_R_ATAPI | ATA_R_READ); 294 break; 295 case BUF_CMD_WRITE: 296 request->flags = (ATA_R_ATAPI | ATA_R_WRITE); 297 break; 298 default: 299 device_printf(dev, "unknown BUF operation\n"); 300 ata_free_request(request); 301 bbp->b_flags |= B_ERROR; 302 bbp->b_error = EIO; 303 biodone(bp); 304 return 0; 305 } 306 if (atadev->mode >= ATA_DMA) 307 request->flags |= ATA_R_DMA; 308 request->flags |= ATA_R_ORDERED; 309 devstat_start_transaction(&fdp->stats); 310 ata_queue_request(request); 311 return 0; 312 } 313 314 static void 315 afd_done(struct ata_request *request) 316 { 317 struct afd_softc *fdp = device_get_ivars(request->dev); 318 struct bio *bp = request->bio; 319 struct buf *bbp = bp->bio_buf; 320 321 /* finish up transfer */ 322 if ((bbp->b_error = request->result)) 323 bbp->b_flags |= B_ERROR; 324 bbp->b_resid = bbp->b_bcount - request->donecount; 325 devstat_end_transaction_buf(&fdp->stats, bbp); 326 biodone(bp); 327 ata_free_request(request); 328 } 329 330 static int 331 afd_sense(device_t dev) 332 { 333 struct ata_device *atadev = device_get_softc(dev); 334 struct afd_softc *fdp = device_get_ivars(dev); 335 struct afd_capacity capacity; 336 struct afd_capacity_big capacity_big; 337 struct afd_capabilities capabilities; 338 int8_t ccb1[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 339 0, 0, 0, 0, 0, 0, 0, 0 }; 340 int8_t ccb2[16] = { ATAPI_SERVICE_ACTION_IN, 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 341 0, 0, 0, sizeof(struct afd_capacity_big) & 0xff, 0, 0 }; 342 int8_t ccb3[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE, 343 0, 0, 0, 0, sizeof(struct afd_capabilities) >> 8, 344 sizeof(struct afd_capabilities) & 0xff, 345 0, 0, 0, 0, 0, 0, 0 }; 346 int timeout = 20; 347 int error, count; 348 349 fdp->mediasize = 0; 350 351 /* wait for device to get ready */ 352 while ((error = afd_test_ready(dev)) && timeout--) { 353 DELAY(100000); 354 } 355 if (error == EBUSY) 356 return 1; 357 358 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */ 359 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) { 360 fdp->heads = 1; 361 fdp->sectors = 2; 362 fdp->mediasize = 39441 * 1024; 363 fdp->sectorsize = 512; 364 afd_test_ready(dev); 365 return 0; 366 } 367 368 /* get drive capacity */ 369 if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity, 370 sizeof(struct afd_capacity), ATA_R_READ, 30)) { 371 fdp->heads = 16; 372 fdp->sectors = 63; 373 fdp->sectorsize = be32toh(capacity.blocksize); 374 fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize; 375 afd_test_ready(dev); 376 return 0; 377 } 378 379 /* get drive capacity big */ 380 if (!ata_atapicmd(dev, ccb2, (caddr_t)&capacity_big, 381 sizeof(struct afd_capacity_big), 382 ATA_R_READ | ATA_R_QUIET, 30)) { 383 fdp->heads = 16; 384 fdp->sectors = 63; 385 fdp->sectorsize = be32toh(capacity_big.blocksize); 386 fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize; 387 afd_test_ready(dev); 388 return 0; 389 } 390 391 /* get drive capabilities, some bugridden drives needs this repeated */ 392 for (count = 0 ; count < 5 ; count++) { 393 if (!ata_atapicmd(dev, ccb3, (caddr_t)&capabilities, 394 sizeof(struct afd_capabilities), ATA_R_READ, 30) && 395 capabilities.page_code == ATAPI_REWRITEABLE_CAP_PAGE) { 396 fdp->heads = capabilities.heads; 397 fdp->sectors = capabilities.sectors; 398 fdp->sectorsize = be16toh(capabilities.sector_size); 399 fdp->mediasize = be16toh(capabilities.cylinders) * 400 fdp->heads * fdp->sectors * fdp->sectorsize; 401 if (!capabilities.medium_type) 402 fdp->mediasize = 0; 403 return 0; 404 } 405 } 406 return 1; 407 } 408 409 static int 410 afd_prevent_allow(device_t dev, int lock) 411 { 412 struct ata_device *atadev = device_get_softc(dev); 413 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock, 414 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 415 416 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) 417 return 0; 418 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30); 419 } 420 421 static int 422 afd_test_ready(device_t dev) 423 { 424 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0, 425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 426 427 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30); 428 } 429 430 static void 431 afd_describe(device_t dev) 432 { 433 struct ata_channel *ch = device_get_softc(device_get_parent(dev)); 434 struct ata_device *atadev = device_get_softc(dev); 435 struct afd_softc *fdp = device_get_ivars(dev); 436 char sizestring[16]; 437 438 if (fdp->mediasize > 1048576 * 5) 439 ksprintf(sizestring, "%lluMB", (unsigned long long) 440 (fdp->mediasize / 1048576)); 441 else if (fdp->mediasize) 442 ksprintf(sizestring, "%lluKB", (unsigned long long) 443 (fdp->mediasize / 1024)); 444 else 445 strcpy(sizestring, "(no media)"); 446 447 device_printf(dev, "%s <%.40s %.8s> at ata%d-%s %s\n", 448 sizestring, atadev->param.model, atadev->param.revision, 449 device_get_unit(ch->dev), 450 (atadev->unit == ATA_MASTER) ? "master" : "slave", 451 ata_mode2str(atadev->mode)); 452 if (bootverbose) { 453 device_printf(dev, "%llu sectors [%lluC/%dH/%dS]\n", 454 (unsigned long long)(fdp->mediasize / fdp->sectorsize), 455 (unsigned long long) 456 (fdp->mediasize/(fdp->sectorsize*fdp->sectors*fdp->heads)), 457 fdp->heads, fdp->sectors); 458 } 459 } 460 461 static device_method_t afd_methods[] = { 462 /* device interface */ 463 DEVMETHOD(device_probe, afd_probe), 464 DEVMETHOD(device_attach, afd_attach), 465 DEVMETHOD(device_detach, afd_detach), 466 DEVMETHOD(device_shutdown, afd_shutdown), 467 468 /* ATA methods */ 469 DEVMETHOD(ata_reinit, afd_reinit), 470 471 { 0, 0 } 472 }; 473 474 static driver_t afd_driver = { 475 "afd", 476 afd_methods, 477 0, 478 }; 479 480 static devclass_t afd_devclass; 481 482 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL); 483 MODULE_VERSION(afd, 1); 484 MODULE_DEPEND(afd, ata, 1, 1, 1); 485