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