]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/atapi-fd.c
This commit was generated by cvs2svn to compensate for changes in r156230,
[FreeBSD/FreeBSD.git] / sys / dev / ata / 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/ata.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/bio.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/cdio.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/bus.h>
44 #include <geom/geom_disk.h>
45 #include <dev/ata/ata-all.h>
46 #include <dev/ata/atapi-fd.h>
47 #include <ata_if.h>
48
49
50 /* prototypes */
51 static disk_open_t afd_open;
52 static disk_close_t afd_close;
53 static disk_strategy_t afd_strategy;
54 static disk_ioctl_t afd_ioctl;
55 static int afd_sense(device_t);
56 static void afd_describe(device_t);
57 static void afd_done(struct ata_request *);
58 static int afd_prevent_allow(device_t, int);
59 static int afd_test_ready(device_t);
60
61 /* internal vars */
62 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
63
64 static int 
65 afd_probe(device_t dev)
66 {
67     struct ata_device *atadev = device_get_softc(dev);
68
69     if ((atadev->param.config & ATA_PROTO_ATAPI) &&
70         (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT)
71         return 0;  
72     else
73         return ENXIO;
74 }
75
76 static int 
77 afd_attach(device_t dev)
78 {
79     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
80     struct ata_device *atadev = device_get_softc(dev);
81     struct afd_softc *fdp;
82
83     if (!(fdp = malloc(sizeof(struct afd_softc), M_AFD, M_NOWAIT | M_ZERO))) {
84         device_printf(dev, "out of memory\n");
85         return ENOMEM;
86     }
87     device_set_ivars(dev, fdp);
88     ATA_SETMODE(device_get_parent(dev), dev);
89
90     if (afd_sense(dev)) {
91         device_set_ivars(dev, NULL);
92         free(fdp, M_AFD);
93         return ENXIO;
94     }
95     atadev->flags |= ATA_D_MEDIA_CHANGED;
96
97     /* announce we are here */
98     afd_describe(dev);
99
100     /* create the disk device */
101     fdp->disk = disk_alloc();
102     fdp->disk->d_open = afd_open;
103     fdp->disk->d_close = afd_close;
104     fdp->disk->d_strategy = afd_strategy;
105     fdp->disk->d_ioctl = afd_ioctl;
106     fdp->disk->d_name = "afd";
107     fdp->disk->d_drv1 = dev;
108     if (ch->dma)
109         fdp->disk->d_maxsize = ch->dma->max_iosize;
110     else
111         fdp->disk->d_maxsize = DFLTPHYS;
112     fdp->disk->d_unit = device_get_unit(dev);
113     disk_create(fdp->disk, DISK_VERSION);
114     return 0;
115 }
116
117 static int
118 afd_detach(device_t dev)
119 {   
120     struct afd_softc *fdp = device_get_ivars(dev);
121     
122     /* detroy disk from the system so we dont get any further requests */
123     disk_destroy(fdp->disk);
124
125     /* fail requests on the queue and any thats "in flight" for this device */
126     ata_fail_requests(dev);
127
128     /* dont leave anything behind */
129     device_set_ivars(dev, NULL);
130     free(fdp, M_AFD);
131     return 0;
132 }
133
134 static void
135 afd_shutdown(device_t dev)
136 {
137     struct ata_device *atadev = device_get_softc(dev);
138
139     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
140         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
141 }
142
143 static int
144 afd_reinit(device_t dev)
145 {
146     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
147     struct ata_device *atadev = device_get_softc(dev);
148     struct afd_softc *fdp = device_get_ivars(dev);
149     
150     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
151         ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
152         device_set_ivars(dev, NULL);
153         free(fdp, M_AFD);
154         return 1;
155     }
156     ATA_SETMODE(device_get_parent(dev), dev);
157     return 0;
158 }
159
160 static int
161 afd_open(struct disk *dp)
162 {
163     device_t dev = dp->d_drv1;
164     struct ata_device *atadev = device_get_softc(dev);
165     struct afd_softc *fdp = device_get_ivars(dev);
166
167     if (!fdp) 
168         return ENXIO;
169     if (!device_is_attached(dev))
170         return EBUSY;
171
172     afd_test_ready(dev);
173     afd_prevent_allow(dev, 1);
174
175     if (afd_sense(dev))
176         device_printf(dev, "sense media type failed\n");
177     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
178
179     fdp->disk->d_sectorsize = fdp->cap.sector_size;
180     fdp->disk->d_mediasize = (off_t)fdp->cap.sector_size * fdp->cap.sectors *
181         fdp->cap.heads * fdp->cap.cylinders;
182     fdp->disk->d_fwsectors = fdp->cap.sectors;
183     fdp->disk->d_fwheads = fdp->cap.heads;
184   
185     return 0;
186 }
187
188 static int 
189 afd_close(struct disk *dp)
190 {
191     device_t dev = dp->d_drv1;
192
193     afd_prevent_allow(dev, 0); 
194     return 0;
195 }
196
197 static void 
198 afd_strategy(struct bio *bp)
199 {
200     device_t dev = bp->bio_disk->d_drv1;
201     struct ata_device *atadev = device_get_softc(dev);
202     struct afd_softc *fdp = device_get_ivars(dev);
203     struct ata_request *request;
204     u_int16_t count;
205     int8_t ccb[16];
206
207     /* if it's a null transfer, return immediatly. */
208     if (bp->bio_bcount == 0) {
209         bp->bio_resid = 0;
210         biodone(bp);
211         return;
212     }
213
214     /* should reject all queued entries if media have changed. */
215     if (atadev->flags & ATA_D_MEDIA_CHANGED) {
216         biofinish(bp, NULL, EIO);
217         return;
218     }
219
220     count = bp->bio_bcount / fdp->cap.sector_size;
221     bp->bio_resid = bp->bio_bcount; 
222
223     bzero(ccb, sizeof(ccb));
224
225     if (bp->bio_cmd == BIO_READ)
226         ccb[0] = ATAPI_READ_BIG;
227     else
228         ccb[0] = ATAPI_WRITE_BIG;
229
230     ccb[2] = bp->bio_pblkno >> 24;
231     ccb[3] = bp->bio_pblkno >> 16;
232     ccb[4] = bp->bio_pblkno >> 8;
233     ccb[5] = bp->bio_pblkno;
234     ccb[7] = count>>8;
235     ccb[8] = count;
236
237     if (!(request = ata_alloc_request())) {
238         biofinish(bp, NULL, ENOMEM);
239         return;
240     }
241     request->dev = dev;
242     request->bio = bp;
243     bcopy(ccb, request->u.atapi.ccb,
244           (atadev->param.config & ATA_PROTO_MASK) == 
245           ATA_PROTO_ATAPI_12 ? 16 : 12);
246     request->data = bp->bio_data;
247     request->bytecount = count * fdp->cap.sector_size;
248     request->transfersize = min(request->bytecount, 65534);
249     request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
250     request->retries = 2;
251     request->callback = afd_done;
252     switch (bp->bio_cmd) {
253     case BIO_READ:
254         request->flags = (ATA_R_ATAPI | ATA_R_READ);
255         break;
256     case BIO_WRITE:
257         request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
258         break;
259     default:
260         device_printf(dev, "unknown BIO operation\n");
261         ata_free_request(request);
262         biofinish(bp, NULL, EIO);
263         return;
264     }
265     if (atadev->mode >= ATA_DMA)
266         request->flags |= ATA_R_DMA;
267     request->flags |= ATA_R_ORDERED;
268     ata_queue_request(request);
269 }
270
271 static void 
272 afd_done(struct ata_request *request)
273 {
274     struct bio *bp = request->bio;
275
276     /* finish up transfer */
277     if ((bp->bio_error = request->result))
278         bp->bio_flags |= BIO_ERROR;
279     bp->bio_resid = bp->bio_bcount - request->donecount;
280     biodone(bp);
281     ata_free_request(request);
282 }
283
284 static int
285 afd_ioctl(struct disk *disk, u_long cmd, void *data, int flag,struct thread *td)
286 {
287     return ata_device_ioctl(disk->d_drv1, cmd, data);
288 }
289
290 static int 
291 afd_sense(device_t dev)
292 {
293     struct ata_device *atadev = device_get_softc(dev);
294     struct afd_softc *fdp = device_get_ivars(dev);
295     int8_t ccb[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
296                        0, 0, 0, 0, sizeof(struct afd_cappage) >> 8,
297                        sizeof(struct afd_cappage) & 0xff, 0, 0, 0, 0, 0, 0, 0 };
298     int count;
299
300     /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
301     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
302         fdp->cap.transfer_rate = 500;
303         fdp->cap.heads = 1;
304         fdp->cap.sectors = 2;
305         fdp->cap.cylinders = 39441;
306         fdp->cap.sector_size = 512;
307         afd_test_ready(dev);
308         return 0;
309     }
310
311     /* get drive capabilities, some bugridden drives needs this repeated */
312     for (count = 0 ; count < 5 ; count++) {
313         if (!ata_atapicmd(dev, ccb, (caddr_t)&fdp->cap,
314                           sizeof(struct afd_cappage), ATA_R_READ, 30) &&
315             fdp->cap.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
316             fdp->cap.cylinders = ntohs(fdp->cap.cylinders);
317             fdp->cap.sector_size = ntohs(fdp->cap.sector_size);
318             fdp->cap.transfer_rate = ntohs(fdp->cap.transfer_rate);
319             return 0;
320         }
321     }
322     return 1;
323 }
324
325 static int
326 afd_prevent_allow(device_t dev, int lock)
327 {
328     struct ata_device *atadev = device_get_softc(dev);
329     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
330                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
331     
332     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
333         return 0;
334     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
335 }
336
337 static int
338 afd_test_ready(device_t dev)
339 {
340     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
341                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
342
343     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
344 }
345
346 static void 
347 afd_describe(device_t dev)
348 {
349     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
350     struct ata_device *atadev = device_get_softc(dev);
351     struct afd_softc *fdp = device_get_ivars(dev);
352
353     if (bootverbose) {
354         device_printf(dev, "<%.40s/%.8s> removable drive at ata%d as %s\n",
355                       atadev->param.model, atadev->param.revision,
356                       device_get_unit(ch->dev),
357                       (atadev->unit == ATA_MASTER) ? "master" : "slave");
358         device_printf(dev,
359                       "%luMB (%u sectors), %u cyls, %u heads, %u S/T, %u B/S\n",
360                       (fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors) /
361                       ((1024L * 1024L) / fdp->cap.sector_size),
362                       fdp->cap.cylinders * fdp->cap.heads * fdp->cap.sectors,
363                       fdp->cap.cylinders, fdp->cap.heads, fdp->cap.sectors,
364                       fdp->cap.sector_size);
365         device_printf(dev, "%dKB/s,", fdp->cap.transfer_rate / 8);
366         printf(" %s\n", ata_mode2str(atadev->mode));
367         if (fdp->cap.medium_type) {
368             device_printf(dev, "Medium: ");
369             switch (fdp->cap.medium_type) {
370             case MFD_2DD:
371                 printf("720KB DD disk"); break;
372
373             case MFD_HD_12:
374                 printf("1.2MB HD disk"); break;
375
376             case MFD_HD_144:
377                 printf("1.44MB HD disk"); break;
378
379             case MFD_UHD: 
380                 printf("120MB UHD disk"); break;
381
382             default:
383                 printf("Unknown (0x%x)", fdp->cap.medium_type);
384             }
385             if (fdp->cap.wp) printf(", writeprotected");
386             printf("\n");
387         }
388     }
389     else {
390         device_printf(dev, "REMOVABLE <%.40s/%.8s> at ata%d-%s %s\n",
391                       atadev->param.model, atadev->param.revision,
392                       device_get_unit(ch->dev),
393                       (atadev->unit == ATA_MASTER) ? "master" : "slave",
394                       ata_mode2str(atadev->mode));
395     }
396 }
397
398 static device_method_t afd_methods[] = {
399     /* device interface */
400     DEVMETHOD(device_probe,     afd_probe),
401     DEVMETHOD(device_attach,    afd_attach),
402     DEVMETHOD(device_detach,    afd_detach),
403     DEVMETHOD(device_shutdown,  afd_shutdown),
404     
405     /* ATA methods */
406     DEVMETHOD(ata_reinit,       afd_reinit),
407     
408     { 0, 0 }
409 };
410     
411 static driver_t afd_driver = {
412     "afd",
413     afd_methods,
414     0,
415 };
416
417 static devclass_t afd_devclass;
418
419 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
420 MODULE_VERSION(afd, 1);
421 MODULE_DEPEND(afd, ata, 1, 1, 1);
422