gold: Fix hardcoded library search path
[dragonfly.git] / sys / dev / disk / nata / atapi-fd.c
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.5 2008/08/30 02:56:11 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 #include <sys/udev.h>
46
47 #include "ata-all.h"
48 #include "atapi-fd.h"
49 #include "ata_if.h"
50
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 },
58         .d_open =       afd_open,
59         .d_close =      afd_close,
60         .d_read =       physread,
61         .d_write =      physwrite,
62         .d_ioctl =      afd_ioctl,
63         .d_strategy =   afd_strategy,
64 };
65
66 /* prototypes */
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);
72
73 /* internal vars */
74 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
75
76 static int 
77 afd_probe(device_t dev)
78 {
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)
82         return 0;  
83     else
84         return ENXIO;
85 }
86
87 static int 
88 afd_attach(device_t dev)
89 {
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;
93     cdev_t cdev;
94
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);
98
99     if (afd_sense(dev)) {
100         device_set_ivars(dev, NULL);
101         kfree(fdp, M_AFD);
102         return ENXIO;
103     }
104     atadev->flags |= ATA_D_MEDIA_CHANGED;
105
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");
112     cdev->si_drv1 = dev;
113     if (ch->dma)
114         cdev->si_iosize_max = ch->dma->max_iosize;
115     else
116         cdev->si_iosize_max = DFLTPHYS;
117     fdp->cdev = cdev;
118
119     /* announce we are here */
120     afd_describe(dev);
121     return 0;
122 }
123
124 static int
125 afd_detach(device_t dev)
126 {   
127     struct afd_softc *fdp = device_get_ivars(dev);
128
129     /* check that we have a valid device to detach */
130     if (!device_get_ivars(dev))
131         return ENXIO;
132     
133     /* detroy disk from the system so we dont get any further requests */
134     disk_invalidate(&fdp->disk);
135     disk_destroy(&fdp->disk);
136
137     /* fail requests on the queue and any thats "in flight" for this device */
138     ata_fail_requests(dev);
139
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);
144     kfree(fdp, M_AFD);
145     return 0;
146 }
147
148 static void
149 afd_shutdown(device_t dev)
150 {
151     struct ata_device *atadev = device_get_softc(dev);
152
153     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
154         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
155 }
156
157 static int
158 afd_reinit(device_t dev)
159 {
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);
163     
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);
167         kfree(fdp, M_AFD);
168         return 1;
169     }
170     ATA_SETMODE(device_get_parent(dev), dev);
171     return 0;
172 }
173
174 static int
175 afd_open(struct dev_open_args *ap)
176 {
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;
181
182     if (!fdp) 
183         return ENXIO;
184     if (!device_is_attached(dev))
185         return EBUSY;
186
187     afd_test_ready(dev);
188     afd_prevent_allow(dev, 1);
189
190     if (afd_sense(dev))
191         device_printf(dev, "sense media type failed\n");
192     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
193
194     if (!fdp->mediasize)
195         return ENXIO;
196
197     bzero(&info, sizeof(info));
198     info.d_media_blksize = fdp->sectorsize;     /* mandatory */
199     info.d_media_size = fdp->mediasize;         /* (this is in bytes) */
200
201     info.d_secpertrack = fdp->sectors;          /* optional */
202     info.d_nheads = fdp->heads;
203     info.d_ncylinders =
204            ((fdp->mediasize/fdp->sectorsize)/fdp->sectors)/fdp->heads;
205     info.d_secpercyl = fdp->sectors * fdp->heads;
206
207     disk_setdiskinfo(&fdp->disk, &info);
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     switch(bbp->b_cmd) {
263     case BUF_CMD_READ:
264         ccb[0] = ATAPI_READ_BIG;
265         break;
266     case BUF_CMD_WRITE:
267         ccb[0] = ATAPI_WRITE_BIG;
268         break;
269     default:
270         device_printf(dev, "unknown BUF operation\n");
271         bbp->b_flags |= B_ERROR;
272         bbp->b_error = EIO;
273         biodone(bp);
274         return 0;
275     }
276
277     ccb[2] = lba >> 24;
278     ccb[3] = lba >> 16;
279     ccb[4] = lba >> 8;
280     ccb[5] = lba;
281     ccb[7] = count>>8;
282     ccb[8] = count;
283
284     if (!(request = ata_alloc_request())) {
285         bbp->b_flags |= B_ERROR;
286         bbp->b_error = ENOMEM;
287         biodone(bp);
288         return 0;
289     }
290     request->dev = dev;
291     request->bio = bp;
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;
301
302     switch (bbp->b_cmd) {
303     case BUF_CMD_READ:
304         request->flags = (ATA_R_ATAPI | ATA_R_READ);
305         break;
306     case BUF_CMD_WRITE:
307         request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
308         break;
309     default:
310         panic("bbp->b_cmd");
311     }
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);
317     return 0;
318 }
319
320 static void 
321 afd_done(struct ata_request *request)
322 {
323     struct afd_softc *fdp = device_get_ivars(request->dev);
324     struct bio *bp = request->bio;
325     struct buf *bbp = bp->bio_buf;
326
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);
332     biodone(bp);
333     ata_free_request(request);
334 }
335
336 static int 
337 afd_sense(device_t dev)
338 {
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 };
352     int timeout = 20;
353     int error, count;
354
355     fdp->mediasize = 0;
356
357     /* wait for device to get ready */
358     while ((error = afd_test_ready(dev)) && timeout--) {
359         DELAY(100000);
360     }
361     if (error == EBUSY)
362         return 1;
363
364     /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
365     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
366         fdp->heads = 1;
367         fdp->sectors = 2;
368         fdp->mediasize = 39441 * 1024;
369         fdp->sectorsize = 512;
370         afd_test_ready(dev);
371         return 0;
372     }
373
374     /* get drive capacity */
375     if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
376                       sizeof(struct afd_capacity), ATA_R_READ, 30)) {
377         fdp->heads = 16;
378         fdp->sectors = 63;
379         fdp->sectorsize = be32toh(capacity.blocksize);
380         fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize; 
381         afd_test_ready(dev);
382         return 0;
383     }
384
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)) {
389         fdp->heads = 16;
390         fdp->sectors = 63;
391         fdp->sectorsize = be32toh(capacity_big.blocksize);
392         fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
393         afd_test_ready(dev);
394         return 0;
395     }
396
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)
408                 fdp->mediasize = 0;
409             return 0;
410         }
411     }
412     return 1;
413 }
414
415 static int
416 afd_prevent_allow(device_t dev, int lock)
417 {
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 };
421     
422     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
423         return 0;
424     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
425 }
426
427 static int
428 afd_test_ready(device_t dev)
429 {
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 };
432
433     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
434 }
435
436 static void 
437 afd_describe(device_t dev)
438 {
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);
442     char sizestring[16];
443
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));
450     else
451         strcpy(sizestring, "(no media)");
452  
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));
458     if (bootverbose) {
459         device_printf(dev, "%llu sectors [%lluC/%dH/%dS]\n",
460                       (unsigned long long)(fdp->mediasize / fdp->sectorsize),
461                       (unsigned long long)
462                      (fdp->mediasize/(fdp->sectorsize*fdp->sectors*fdp->heads)),
463                       fdp->heads, fdp->sectors);
464     }
465 }
466
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),
473     
474     /* ATA methods */
475     DEVMETHOD(ata_reinit,       afd_reinit),
476     
477     { 0, 0 }
478 };
479     
480 static driver_t afd_driver = {
481     "afd",
482     afd_methods,
483     0,
484 };
485
486 static devclass_t afd_devclass;
487
488 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
489 MODULE_VERSION(afd, 1);
490 MODULE_DEPEND(afd, ata, 1, 1, 1);