FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/atapi-fd.c
1 /*-
2 * Copyright (c) 1998 - 2007 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/endian.h>
40 #include <sys/cdio.h>
41 #include <sys/sema.h>
42 #include <sys/taskqueue.h>
43 #include <vm/uma.h>
44 #include <machine/bus.h>
45 #include <geom/geom_disk.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/atapi-fd.h>
48 #include <ata_if.h>
49
50
51 /* prototypes */
52 static disk_open_t afd_open;
53 static disk_close_t afd_close;
54 static disk_strategy_t afd_strategy;
55 static disk_ioctl_t afd_ioctl;
56 static int afd_sense(device_t);
57 static void afd_describe(device_t);
58 static void afd_done(struct ata_request *);
59 static int afd_prevent_allow(device_t, int);
60 static int afd_test_ready(device_t);
61
62 /* internal vars */
63 static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
64
65 static int
66 afd_probe(device_t dev)
67 {
68 struct ata_device *atadev = device_get_softc(dev);
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 /* check that we have a valid device to detach */
123 if (!device_get_ivars(dev))
124 return ENXIO;
125
126 /* detroy disk from the system so we dont get any further requests */
127 disk_destroy(fdp->disk);
128
129 /* fail requests on the queue and any thats "in flight" for this device */
130 ata_fail_requests(dev);
131
132 /* dont leave anything behind */
133 device_set_ivars(dev, NULL);
134 free(fdp, M_AFD);
135 return 0;
136 }
137
138 static void
139 afd_shutdown(device_t dev)
140 {
141 struct ata_device *atadev = device_get_softc(dev);
142
143 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
144 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
145 }
146
147 static int
148 afd_reinit(device_t dev)
149 {
150 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
151 struct ata_device *atadev = device_get_softc(dev);
152
153 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
154 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
155 return 1;
156 }
157 ATA_SETMODE(device_get_parent(dev), dev);
158 return 0;
159 }
160
161 static int
162 afd_open(struct disk *dp)
163 {
164 device_t dev = dp->d_drv1;
165 struct ata_device *atadev = device_get_softc(dev);
166 struct afd_softc *fdp = device_get_ivars(dev);
167
168 if (!fdp)
169 return ENXIO;
170 if (!device_is_attached(dev))
171 return EBUSY;
172
173 afd_test_ready(dev);
174 afd_prevent_allow(dev, 1);
175
176 if (afd_sense(dev))
177 device_printf(dev, "sense media type failed\n");
178 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
179
180 if (!fdp->mediasize)
181 return ENXIO;
182
183 fdp->disk->d_sectorsize = fdp->sectorsize;
184 fdp->disk->d_mediasize = fdp->mediasize;
185 fdp->disk->d_fwsectors = fdp->sectors;
186 fdp->disk->d_fwheads = fdp->heads;
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->sectorsize;
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->sectorsize;
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 struct afd_capacity capacity;
298 struct afd_capacity_big capacity_big;
299 struct afd_capabilities capabilities;
300 int8_t ccb1[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
301 0, 0, 0, 0, 0, 0, 0, 0 };
302 int8_t ccb2[16] = { ATAPI_SERVICE_ACTION_IN, 0x10, 0, 0, 0, 0, 0, 0, 0, 0,
303 0, 0, 0, sizeof(struct afd_capacity_big) & 0xff, 0, 0 };
304 int8_t ccb3[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
305 0, 0, 0, 0, sizeof(struct afd_capabilities) >> 8,
306 sizeof(struct afd_capabilities) & 0xff,
307 0, 0, 0, 0, 0, 0, 0 };
308 int timeout = 20;
309 int error, count;
310
311 fdp->mediasize = 0;
312
313 /* wait for device to get ready */
314 while ((error = afd_test_ready(dev)) && timeout--) {
315 DELAY(100000);
316 }
317 if (error == EBUSY)
318 return 1;
319
320 /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
321 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
322 fdp->heads = 1;
323 fdp->sectors = 2;
324 fdp->mediasize = 39441 * 1024;
325 fdp->sectorsize = 512;
326 afd_test_ready(dev);
327 return 0;
328 }
329
330 /* get drive capacity */
331 if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
332 sizeof(struct afd_capacity), ATA_R_READ, 30)) {
333 fdp->heads = 16;
334 fdp->sectors = 63;
335 fdp->sectorsize = be32toh(capacity.blocksize);
336 fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize;
337 afd_test_ready(dev);
338 return 0;
339 }
340
341 /* get drive capacity big */
342 if (!ata_atapicmd(dev, ccb2, (caddr_t)&capacity_big,
343 sizeof(struct afd_capacity_big),
344 ATA_R_READ | ATA_R_QUIET, 30)) {
345 fdp->heads = 16;
346 fdp->sectors = 63;
347 fdp->sectorsize = be32toh(capacity_big.blocksize);
348 fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
349 afd_test_ready(dev);
350 return 0;
351 }
352
353 /* get drive capabilities, some bugridden drives needs this repeated */
354 for (count = 0 ; count < 5 ; count++) {
355 if (!ata_atapicmd(dev, ccb3, (caddr_t)&capabilities,
356 sizeof(struct afd_capabilities), ATA_R_READ, 30) &&
357 capabilities.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
358 fdp->heads = capabilities.heads;
359 fdp->sectors = capabilities.sectors;
360 fdp->sectorsize = be16toh(capabilities.sector_size);
361 fdp->mediasize = be16toh(capabilities.cylinders) *
362 fdp->heads * fdp->sectors * fdp->sectorsize;
363 if (!capabilities.medium_type)
364 fdp->mediasize = 0;
365 return 0;
366 }
367 }
368 return 1;
369 }
370
371 static int
372 afd_prevent_allow(device_t dev, int lock)
373 {
374 struct ata_device *atadev = device_get_softc(dev);
375 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
376 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
377
378 if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
379 return 0;
380 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
381 }
382
383 static int
384 afd_test_ready(device_t dev)
385 {
386 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
388
389 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
390 }
391
392 static void
393 afd_describe(device_t dev)
394 {
395 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
396 struct ata_device *atadev = device_get_softc(dev);
397 struct afd_softc *fdp = device_get_ivars(dev);
398 char sizestring[16];
399
400 if (fdp->mediasize > 1048576 * 5)
401 sprintf(sizestring, "%juMB", fdp->mediasize / 1048576);
402 else if (fdp->mediasize)
403 sprintf(sizestring, "%juKB", fdp->mediasize / 1024);
404 else
405 strcpy(sizestring, "(no media)");
406
407 device_printf(dev, "%s <%.40s %.8s> at ata%d-%s %s\n",
408 sizestring, atadev->param.model, atadev->param.revision,
409 device_get_unit(ch->dev),
410 (atadev->unit == ATA_MASTER) ? "master" : "slave",
411 ata_mode2str(atadev->mode));
412 if (bootverbose) {
413 device_printf(dev, "%ju sectors [%juC/%dH/%dS]\n",
414 fdp->mediasize / fdp->sectorsize,
415 fdp->mediasize /(fdp->sectorsize*fdp->sectors*fdp->heads),
416 fdp->heads, fdp->sectors);
417 }
418 }
419
420 static device_method_t afd_methods[] = {
421 /* device interface */
422 DEVMETHOD(device_probe, afd_probe),
423 DEVMETHOD(device_attach, afd_attach),
424 DEVMETHOD(device_detach, afd_detach),
425 DEVMETHOD(device_shutdown, afd_shutdown),
426
427 /* ATA methods */
428 DEVMETHOD(ata_reinit, afd_reinit),
429
430 { 0, 0 }
431 };
432
433 static driver_t afd_driver = {
434 "afd",
435 afd_methods,
436 0,
437 };
438
439 static devclass_t afd_devclass;
440
441 DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
442 MODULE_VERSION(afd, 1);
443 MODULE_DEPEND(afd, ata, 1, 1, 1);
444
Cache object: 121cf1b2b92ffbfce728a93b62d1b113
|