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.1 2006/12/04 14:40:37 tgen 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>
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 },
62 .d_strategy = afd_strategy,
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);
73 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
76 afd_probe(device_t dev)
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)
87 afd_attach(device_t dev)
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;
94 /* XXX TGEN We're not in interrupt context, so we can M_WAITOK and remove
96 if (!(fdp = kmalloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO))) {
97 device_printf(dev, "out of memory\n");
100 device_set_ivars(dev, fdp);
101 ATA_SETMODE(device_get_parent(dev), dev);
103 if (afd_sense(dev)) {
104 device_set_ivars(dev, NULL);
108 atadev->flags |= ATA_D_MEDIA_CHANGED;
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);
117 cdev->si_iosize_max = ch->dma->max_iosize;
119 cdev->si_iosize_max = DFLTPHYS;
122 /* announce we are here */
128 afd_detach(device_t dev)
130 struct afd_softc *fdp = device_get_ivars(dev);
132 /* check that we have a valid device to detach */
133 if (!device_get_ivars(dev))
136 /* detroy disk from the system so we dont get any further requests */
137 disk_invalidate(&fdp->disk);
138 disk_destroy(&fdp->disk);
140 /* fail requests on the queue and any thats "in flight" for this device */
141 ata_fail_requests(dev);
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);
152 afd_shutdown(device_t dev)
154 struct ata_device *atadev = device_get_softc(dev);
156 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
157 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
161 afd_reinit(device_t dev)
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);
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);
173 ATA_SETMODE(device_get_parent(dev), dev);
178 afd_open(struct dev_open_args *ap)
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;
187 if (!device_is_attached(dev))
191 afd_prevent_allow(dev, 1);
194 device_printf(dev, "sense media type failed\n");
195 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
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;
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));
262 if (bbp->b_cmd == BUF_CMD_READ)
263 ccb[0] = ATAPI_READ_BIG;
265 ccb[0] = ATAPI_WRITE_BIG;
274 if (!(request = ata_alloc_request())) {
275 bbp->b_flags |= B_ERROR;
276 bbp->b_error = ENOMEM;
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) {
293 request->flags = (ATA_R_ATAPI | ATA_R_READ);
296 request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
299 device_printf(dev, "unknown BUF operation\n");
300 ata_free_request(request);
301 bbp->b_flags |= B_ERROR;
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);
315 afd_done(struct ata_request *request)
317 struct afd_softc *fdp = device_get_ivars(request->dev);
318 struct bio *bp = request->bio;
319 struct buf *bbp = bp->bio_buf;
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);
327 ata_free_request(request);
331 afd_sense(device_t dev)
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 };
351 /* wait for device to get ready */
352 while ((error = afd_test_ready(dev)) && timeout--) {
358 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
359 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
362 fdp->mediasize = 39441 * 1024;
363 fdp->sectorsize = 512;
368 /* get drive capacity */
369 if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
370 sizeof(struct afd_capacity), ATA_R_READ, 30)) {
373 fdp->sectorsize = be32toh(capacity.blocksize);
374 fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize;
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)) {
385 fdp->sectorsize = be32toh(capacity_big.blocksize);
386 fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
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)
410 afd_prevent_allow(device_t dev, int lock)
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 };
416 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
418 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
422 afd_test_ready(device_t dev)
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 };
427 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
431 afd_describe(device_t dev)
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);
438 if (fdp->mediasize > 1048576 * 5)
439 sprintf(sizestring, "%lluMB", (unsigned long long)
440 (fdp->mediasize / 1048576));
441 else if (fdp->mediasize)
442 sprintf(sizestring, "%lluKB", (unsigned long long)
443 (fdp->mediasize / 1024));
445 strcpy(sizestring, "(no media)");
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));
453 device_printf(dev, "%llu sectors [%lluC/%dH/%dS]\n",
454 (unsigned long long)(fdp->mediasize / fdp->sectorsize),
456 (fdp->mediasize/(fdp->sectorsize*fdp->sectors*fdp->heads)),
457 fdp->heads, fdp->sectors);
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),
469 DEVMETHOD(ata_reinit, afd_reinit),
474 static driver_t afd_driver = {
480 static devclass_t afd_devclass;
482 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
483 MODULE_VERSION(afd, 1);
484 MODULE_DEPEND(afd, ata, 1, 1, 1);