2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.5 2008/08/30 02:56:11 dillon Exp $
30 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/devicestat.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>
44 #include <sys/systm.h>
51 /* device structure */
52 static d_open_t afd_open;
53 static d_close_t afd_close;
54 static d_ioctl_t afd_ioctl;
55 static d_strategy_t afd_strategy;
56 static struct dev_ops afd_ops = {
57 { "afd", 118, D_DISK | D_TRACKCLOSE },
63 .d_strategy = afd_strategy,
67 static int afd_sense(device_t);
68 static void afd_describe(device_t);
69 static void afd_done(struct ata_request *);
70 static int afd_prevent_allow(device_t, int);
71 static int afd_test_ready(device_t);
74 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
77 afd_probe(device_t dev)
79 struct ata_device *atadev = device_get_softc(dev);
80 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
81 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT)
88 afd_attach(device_t dev)
90 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
91 struct ata_device *atadev = device_get_softc(dev);
92 struct afd_softc *fdp;
95 fdp = kmalloc(sizeof(struct afd_softc), M_AFD, M_WAITOK | M_ZERO);
96 device_set_ivars(dev, fdp);
97 ATA_SETMODE(device_get_parent(dev), dev);
100 device_set_ivars(dev, NULL);
104 atadev->flags |= ATA_D_MEDIA_CHANGED;
106 /* create the disk device */
107 devstat_add_entry(&fdp->stats, "afd", device_get_unit(dev), DEV_BSIZE,
108 DEVSTAT_NO_ORDERED_TAGS, DEVSTAT_TYPE_DIRECT |
109 DEVSTAT_TYPE_IF_IDE, DEVSTAT_PRIORITY_WFD);
110 cdev = disk_create(device_get_unit(dev), &fdp->disk, &afd_ops);
111 disk_setdisktype(&fdp->disk, "floppy");
114 cdev->si_iosize_max = ch->dma->max_iosize;
116 cdev->si_iosize_max = DFLTPHYS;
119 /* announce we are here */
125 afd_detach(device_t dev)
127 struct afd_softc *fdp = device_get_ivars(dev);
129 /* check that we have a valid device to detach */
130 if (!device_get_ivars(dev))
133 /* detroy disk from the system so we dont get any further requests */
134 disk_invalidate(&fdp->disk);
135 disk_destroy(&fdp->disk);
137 /* fail requests on the queue and any thats "in flight" for this device */
138 ata_fail_requests(dev);
140 /* dont leave anything behind */
141 /* disk_destroy() already took care of the dev_ops */
142 devstat_remove_entry(&fdp->stats);
143 device_set_ivars(dev, NULL);
149 afd_shutdown(device_t dev)
151 struct ata_device *atadev = device_get_softc(dev);
153 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
154 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
158 afd_reinit(device_t dev)
160 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
161 struct ata_device *atadev = device_get_softc(dev);
162 struct afd_softc *fdp = device_get_ivars(dev);
164 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
165 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
166 device_set_ivars(dev, NULL);
170 ATA_SETMODE(device_get_parent(dev), dev);
175 afd_open(struct dev_open_args *ap)
177 device_t dev = ap->a_head.a_dev->si_drv1;
178 struct ata_device *atadev = device_get_softc(dev);
179 struct afd_softc *fdp = device_get_ivars(dev);
180 struct disk_info info;
184 if (!device_is_attached(dev))
188 afd_prevent_allow(dev, 1);
191 device_printf(dev, "sense media type failed\n");
192 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
197 bzero(&info, sizeof(info));
198 info.d_media_blksize = fdp->sectorsize; /* mandatory */
199 info.d_media_size = fdp->mediasize; /* (this is in bytes) */
201 info.d_secpertrack = fdp->sectors; /* optional */
202 info.d_nheads = fdp->heads;
204 ((fdp->mediasize/fdp->sectorsize)/fdp->sectors)/fdp->heads;
205 info.d_secpercyl = fdp->sectors * fdp->heads;
207 disk_setdiskinfo(&fdp->disk, &info);
212 afd_close(struct dev_close_args *ap)
214 device_t dev = ap->a_head.a_dev->si_drv1;
215 struct afd_softc *fdp = device_get_ivars(dev);
217 if (count_dev(fdp->cdev) == 1)
218 afd_prevent_allow(dev, 0);
223 afd_ioctl(struct dev_ioctl_args *ap)
225 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data);
229 afd_strategy(struct dev_strategy_args *ap)
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;
241 /* if it's a null transfer, return immediatly. */
242 if (bbp->b_bcount == 0) {
248 /* should reject all queued entries if media have changed. */
249 if (atadev->flags & ATA_D_MEDIA_CHANGED) {
250 bbp->b_flags |= B_ERROR;
256 lba = bp->bio_offset / fdp->sectorsize;
257 count = bbp->b_bcount / fdp->sectorsize;
258 bbp->b_resid = bbp->b_bcount;
260 bzero(ccb, sizeof(ccb));
264 ccb[0] = ATAPI_READ_BIG;
267 ccb[0] = ATAPI_WRITE_BIG;
270 device_printf(dev, "unknown BUF operation\n");
271 bbp->b_flags |= B_ERROR;
284 if (!(request = ata_alloc_request())) {
285 bbp->b_flags |= B_ERROR;
286 bbp->b_error = ENOMEM;
292 bcopy(ccb, request->u.atapi.ccb,
293 (atadev->param.config & ATA_PROTO_MASK) ==
294 ATA_PROTO_ATAPI_12 ? 16 : 12);
295 request->data = bbp->b_data;
296 request->bytecount = count * fdp->sectorsize;
297 request->transfersize = min(request->bytecount, 65534);
298 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
299 request->retries = 2;
300 request->callback = afd_done;
302 switch (bbp->b_cmd) {
304 request->flags = (ATA_R_ATAPI | ATA_R_READ);
307 request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
312 if (atadev->mode >= ATA_DMA)
313 request->flags |= ATA_R_DMA;
314 request->flags |= ATA_R_ORDERED;
315 devstat_start_transaction(&fdp->stats);
316 ata_queue_request(request);
321 afd_done(struct ata_request *request)
323 struct afd_softc *fdp = device_get_ivars(request->dev);
324 struct bio *bp = request->bio;
325 struct buf *bbp = bp->bio_buf;
327 /* finish up transfer */
328 if ((bbp->b_error = request->result))
329 bbp->b_flags |= B_ERROR;
330 bbp->b_resid = bbp->b_bcount - request->donecount;
331 devstat_end_transaction_buf(&fdp->stats, bbp);
333 ata_free_request(request);
337 afd_sense(device_t dev)
339 struct ata_device *atadev = device_get_softc(dev);
340 struct afd_softc *fdp = device_get_ivars(dev);
341 struct afd_capacity capacity;
342 struct afd_capacity_big capacity_big;
343 struct afd_capabilities capabilities;
344 int8_t ccb1[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
345 0, 0, 0, 0, 0, 0, 0, 0 };
346 int8_t ccb2[16] = { ATAPI_READ_CAPACITY_16, 0x10, 0, 0, 0, 0, 0, 0, 0, 0,
347 0, 0, 0, sizeof(struct afd_capacity_big) & 0xff, 0, 0 };
348 int8_t ccb3[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
349 0, 0, 0, 0, sizeof(struct afd_capabilities) >> 8,
350 sizeof(struct afd_capabilities) & 0xff,
351 0, 0, 0, 0, 0, 0, 0 };
357 /* wait for device to get ready */
358 while ((error = afd_test_ready(dev)) && timeout--) {
364 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
365 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
368 fdp->mediasize = 39441 * 1024;
369 fdp->sectorsize = 512;
374 /* get drive capacity */
375 if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
376 sizeof(struct afd_capacity), ATA_R_READ, 30)) {
379 fdp->sectorsize = be32toh(capacity.blocksize);
380 fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize;
385 /* get drive capacity big */
386 if (!ata_atapicmd(dev, ccb2, (caddr_t)&capacity_big,
387 sizeof(struct afd_capacity_big),
388 ATA_R_READ | ATA_R_QUIET, 30)) {
391 fdp->sectorsize = be32toh(capacity_big.blocksize);
392 fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
397 /* get drive capabilities, some bugridden drives needs this repeated */
398 for (count = 0 ; count < 5 ; count++) {
399 if (!ata_atapicmd(dev, ccb3, (caddr_t)&capabilities,
400 sizeof(struct afd_capabilities), ATA_R_READ, 30) &&
401 capabilities.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
402 fdp->heads = capabilities.heads;
403 fdp->sectors = capabilities.sectors;
404 fdp->sectorsize = be16toh(capabilities.sector_size);
405 fdp->mediasize = be16toh(capabilities.cylinders) *
406 fdp->heads * fdp->sectors * fdp->sectorsize;
407 if (!capabilities.medium_type)
416 afd_prevent_allow(device_t dev, int lock)
418 struct ata_device *atadev = device_get_softc(dev);
419 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
422 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
424 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
428 afd_test_ready(device_t dev)
430 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
431 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
433 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
437 afd_describe(device_t dev)
439 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
440 struct ata_device *atadev = device_get_softc(dev);
441 struct afd_softc *fdp = device_get_ivars(dev);
444 if (fdp->mediasize > 1048576 * 5)
445 ksprintf(sizestring, "%lluMB", (unsigned long long)
446 (fdp->mediasize / 1048576));
447 else if (fdp->mediasize)
448 ksprintf(sizestring, "%lluKB", (unsigned long long)
449 (fdp->mediasize / 1024));
451 strcpy(sizestring, "(no media)");
453 device_printf(dev, "%s <%.40s %.8s> at ata%d-%s %s\n",
454 sizestring, atadev->param.model, atadev->param.revision,
455 device_get_unit(ch->dev),
456 (atadev->unit == ATA_MASTER) ? "master" : "slave",
457 ata_mode2str(atadev->mode));
459 device_printf(dev, "%llu sectors [%lluC/%dH/%dS]\n",
460 (unsigned long long)(fdp->mediasize / fdp->sectorsize),
462 (fdp->mediasize/(fdp->sectorsize*fdp->sectors*fdp->heads)),
463 fdp->heads, fdp->sectors);
467 static device_method_t afd_methods[] = {
468 /* device interface */
469 DEVMETHOD(device_probe, afd_probe),
470 DEVMETHOD(device_attach, afd_attach),
471 DEVMETHOD(device_detach, afd_detach),
472 DEVMETHOD(device_shutdown, afd_shutdown),
475 DEVMETHOD(ata_reinit, afd_reinit),
480 static driver_t afd_driver = {
486 static devclass_t afd_devclass;
488 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
489 MODULE_VERSION(afd, 1);
490 MODULE_DEPEND(afd, ata, 1, 1, 1);