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