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