xref: /dflybsd-src/sys/dev/disk/nata/atapi-fd.c (revision 2458a87a718a37261418254ad47f5f9adc3366d4)
1c1b3d7c5SThomas E. Spanjaard /*-
2*2458a87aSzrj  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3c1b3d7c5SThomas E. Spanjaard  * All rights reserved.
4c1b3d7c5SThomas E. Spanjaard  *
5c1b3d7c5SThomas E. Spanjaard  * Redistribution and use in source and binary forms, with or without
6c1b3d7c5SThomas E. Spanjaard  * modification, are permitted provided that the following conditions
7c1b3d7c5SThomas E. Spanjaard  * are met:
8c1b3d7c5SThomas E. Spanjaard  * 1. Redistributions of source code must retain the above copyright
9c1b3d7c5SThomas E. Spanjaard  *    notice, this list of conditions and the following disclaimer,
10c1b3d7c5SThomas E. Spanjaard  *    without modification, immediately at the beginning of the file.
11c1b3d7c5SThomas E. Spanjaard  * 2. Redistributions in binary form must reproduce the above copyright
12c1b3d7c5SThomas E. Spanjaard  *    notice, this list of conditions and the following disclaimer in the
13c1b3d7c5SThomas E. Spanjaard  *    documentation and/or other materials provided with the distribution.
14c1b3d7c5SThomas E. Spanjaard  *
15c1b3d7c5SThomas E. Spanjaard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c1b3d7c5SThomas E. Spanjaard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c1b3d7c5SThomas E. Spanjaard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c1b3d7c5SThomas E. Spanjaard  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c1b3d7c5SThomas E. Spanjaard  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c1b3d7c5SThomas E. Spanjaard  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c1b3d7c5SThomas E. Spanjaard  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c1b3d7c5SThomas E. Spanjaard  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c1b3d7c5SThomas E. Spanjaard  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c1b3d7c5SThomas E. Spanjaard  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c1b3d7c5SThomas E. Spanjaard  *
26c1b3d7c5SThomas E. Spanjaard  * $FreeBSD: src/sys/dev/ata/atapi-fd.c,v 1.109 2006/03/30 05:29:57 marcel Exp $
27c1b3d7c5SThomas E. Spanjaard  */
28c1b3d7c5SThomas E. Spanjaard 
29c1b3d7c5SThomas E. Spanjaard #include <sys/param.h>
30c1b3d7c5SThomas E. Spanjaard #include <sys/bio.h>
31c1b3d7c5SThomas E. Spanjaard #include <sys/buf.h>
32c1b3d7c5SThomas E. Spanjaard #include <sys/bus.h>
33c1b3d7c5SThomas E. Spanjaard #include <sys/conf.h>
34c1b3d7c5SThomas E. Spanjaard #include <sys/device.h>
35c1b3d7c5SThomas E. Spanjaard #include <sys/devicestat.h>
36c1b3d7c5SThomas E. Spanjaard #include <sys/disk.h>
37c1b3d7c5SThomas E. Spanjaard #include <sys/endian.h>
38c1b3d7c5SThomas E. Spanjaard #include <sys/kernel.h>
39c1b3d7c5SThomas E. Spanjaard #include <sys/libkern.h>
40c1b3d7c5SThomas E. Spanjaard #include <sys/malloc.h>
41c1b3d7c5SThomas E. Spanjaard #include <sys/module.h>
42c1b3d7c5SThomas E. Spanjaard #include <sys/nata.h>
43c1b3d7c5SThomas E. Spanjaard #include <sys/systm.h>
44f5d8307cSAlex Hornung #include <sys/udev.h>
45c1b3d7c5SThomas E. Spanjaard 
46c1b3d7c5SThomas E. Spanjaard #include "ata-all.h"
47c1b3d7c5SThomas E. Spanjaard #include "atapi-fd.h"
48c1b3d7c5SThomas E. Spanjaard #include "ata_if.h"
49c1b3d7c5SThomas E. Spanjaard 
50bb15467aSzrj /* local implementation, to trigger a warning */
51bb15467aSzrj static inline void
biofinish(struct bio * bp,struct bio * x __unused,int error)52bb15467aSzrj biofinish(struct bio *bp, struct bio *x __unused, int error)
53bb15467aSzrj {
54bb15467aSzrj 	struct buf *bbp = bp->bio_buf;
55bb15467aSzrj 
56bb15467aSzrj 	bbp->b_flags |= B_ERROR;
57bb15467aSzrj 	bbp->b_error = error;
58bb15467aSzrj 	biodone(bp);
59bb15467aSzrj }
60bb15467aSzrj 
61c1b3d7c5SThomas E. Spanjaard /* device structure */
62c1b3d7c5SThomas E. Spanjaard static	d_open_t	afd_open;
63c1b3d7c5SThomas E. Spanjaard static	d_close_t	afd_close;
64c1b3d7c5SThomas E. Spanjaard static	d_ioctl_t	afd_ioctl;
65c1b3d7c5SThomas E. Spanjaard static	d_strategy_t	afd_strategy;
66c1b3d7c5SThomas E. Spanjaard static struct dev_ops afd_ops = {
67c1b3d7c5SThomas E. Spanjaard 	{ "afd", 118, D_DISK | D_TRACKCLOSE },
68c1b3d7c5SThomas E. Spanjaard 	.d_open =	afd_open,
69c1b3d7c5SThomas E. Spanjaard 	.d_close =	afd_close,
70c1b3d7c5SThomas E. Spanjaard 	.d_read =	physread,
71c1b3d7c5SThomas E. Spanjaard 	.d_write =	physwrite,
72c1b3d7c5SThomas E. Spanjaard 	.d_ioctl =	afd_ioctl,
73c1b3d7c5SThomas E. Spanjaard 	.d_strategy =	afd_strategy,
74c1b3d7c5SThomas E. Spanjaard };
75c1b3d7c5SThomas E. Spanjaard 
76c1b3d7c5SThomas E. Spanjaard /* prototypes */
77c1b3d7c5SThomas E. Spanjaard static int afd_sense(device_t);
78c1b3d7c5SThomas E. Spanjaard static void afd_describe(device_t);
79c1b3d7c5SThomas E. Spanjaard static void afd_done(struct ata_request *);
80c1b3d7c5SThomas E. Spanjaard static int afd_prevent_allow(device_t, int);
81c1b3d7c5SThomas E. Spanjaard static int afd_test_ready(device_t);
82c1b3d7c5SThomas E. Spanjaard 
83c1b3d7c5SThomas E. Spanjaard /* internal vars */
84c1b3d7c5SThomas E. Spanjaard static MALLOC_DEFINE(M_AFD, "afd_driver", "ATAPI floppy driver buffers");
85c1b3d7c5SThomas E. Spanjaard 
86c1b3d7c5SThomas E. Spanjaard static int
afd_probe(device_t dev)87c1b3d7c5SThomas E. Spanjaard afd_probe(device_t dev)
88c1b3d7c5SThomas E. Spanjaard {
89c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
90c1b3d7c5SThomas E. Spanjaard     if ((atadev->param.config & ATA_PROTO_ATAPI) &&
91c1b3d7c5SThomas E. Spanjaard 	(atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_DIRECT)
92c1b3d7c5SThomas E. Spanjaard 	return 0;
93c1b3d7c5SThomas E. Spanjaard     else
94c1b3d7c5SThomas E. Spanjaard 	return ENXIO;
95c1b3d7c5SThomas E. Spanjaard }
96c1b3d7c5SThomas E. Spanjaard 
97c1b3d7c5SThomas E. Spanjaard static int
afd_attach(device_t dev)98c1b3d7c5SThomas E. Spanjaard afd_attach(device_t dev)
99c1b3d7c5SThomas E. Spanjaard {
100c1b3d7c5SThomas E. Spanjaard     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
101c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
102c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp;
103c1b3d7c5SThomas E. Spanjaard     cdev_t cdev;
104c1b3d7c5SThomas E. Spanjaard 
105a01741bbSMatthew Dillon     fdp = kmalloc(sizeof(struct afd_softc), M_AFD, M_WAITOK | M_ZERO);
106c1b3d7c5SThomas E. Spanjaard     device_set_ivars(dev, fdp);
107c1b3d7c5SThomas E. Spanjaard     ATA_SETMODE(device_get_parent(dev), dev);
108c1b3d7c5SThomas E. Spanjaard 
109c1b3d7c5SThomas E. Spanjaard     if (afd_sense(dev)) {
110c1b3d7c5SThomas E. Spanjaard 	device_set_ivars(dev, NULL);
111c1b3d7c5SThomas E. Spanjaard 	kfree(fdp, M_AFD);
112c1b3d7c5SThomas E. Spanjaard 	return ENXIO;
113c1b3d7c5SThomas E. Spanjaard     }
114c1b3d7c5SThomas E. Spanjaard     atadev->flags |= ATA_D_MEDIA_CHANGED;
115c1b3d7c5SThomas E. Spanjaard 
116c1b3d7c5SThomas E. Spanjaard     /* create the disk device */
117c1b3d7c5SThomas E. Spanjaard     devstat_add_entry(&fdp->stats, "afd", device_get_unit(dev), DEV_BSIZE,
118c1b3d7c5SThomas E. Spanjaard 		      DEVSTAT_NO_ORDERED_TAGS, DEVSTAT_TYPE_DIRECT |
119c1b3d7c5SThomas E. Spanjaard 		      DEVSTAT_TYPE_IF_IDE, DEVSTAT_PRIORITY_WFD);
120a688b15cSMatthew Dillon     cdev = disk_create(device_get_unit(dev), &fdp->disk, &afd_ops);
121f5d8307cSAlex Hornung     disk_setdisktype(&fdp->disk, "floppy");
122c1b3d7c5SThomas E. Spanjaard     cdev->si_drv1 = dev;
123c1b3d7c5SThomas E. Spanjaard     if (ch->dma)
124c1b3d7c5SThomas E. Spanjaard 	cdev->si_iosize_max = ch->dma->max_iosize;
125c1b3d7c5SThomas E. Spanjaard     else
126d83666e0SFrançois Tigeot 	cdev->si_iosize_max = min(MAXPHYS,64*1024);
127c1b3d7c5SThomas E. Spanjaard     fdp->cdev = cdev;
128c1b3d7c5SThomas E. Spanjaard 
129c1b3d7c5SThomas E. Spanjaard     /* announce we are here */
130c1b3d7c5SThomas E. Spanjaard     afd_describe(dev);
131c1b3d7c5SThomas E. Spanjaard     return 0;
132c1b3d7c5SThomas E. Spanjaard }
133c1b3d7c5SThomas E. Spanjaard 
134c1b3d7c5SThomas E. Spanjaard static int
afd_detach(device_t dev)135c1b3d7c5SThomas E. Spanjaard afd_detach(device_t dev)
136c1b3d7c5SThomas E. Spanjaard {
137c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
138c1b3d7c5SThomas E. Spanjaard 
139c1b3d7c5SThomas E. Spanjaard     /* check that we have a valid device to detach */
140c1b3d7c5SThomas E. Spanjaard     if (!device_get_ivars(dev))
141c1b3d7c5SThomas E. Spanjaard         return ENXIO;
142c1b3d7c5SThomas E. Spanjaard 
143c1b3d7c5SThomas E. Spanjaard     /* detroy disk from the system so we dont get any further requests */
144c1b3d7c5SThomas E. Spanjaard     disk_invalidate(&fdp->disk);
145c1b3d7c5SThomas E. Spanjaard     disk_destroy(&fdp->disk);
146c1b3d7c5SThomas E. Spanjaard 
147c1b3d7c5SThomas E. Spanjaard     /* fail requests on the queue and any thats "in flight" for this device */
148c1b3d7c5SThomas E. Spanjaard     ata_fail_requests(dev);
149c1b3d7c5SThomas E. Spanjaard 
150c1b3d7c5SThomas E. Spanjaard     /* dont leave anything behind */
151c1b3d7c5SThomas E. Spanjaard     /* disk_destroy() already took care of the dev_ops */
152c1b3d7c5SThomas E. Spanjaard     devstat_remove_entry(&fdp->stats);
153c1b3d7c5SThomas E. Spanjaard     device_set_ivars(dev, NULL);
154c1b3d7c5SThomas E. Spanjaard     kfree(fdp, M_AFD);
155c1b3d7c5SThomas E. Spanjaard     return 0;
156c1b3d7c5SThomas E. Spanjaard }
157c1b3d7c5SThomas E. Spanjaard 
158c1b3d7c5SThomas E. Spanjaard static void
afd_shutdown(device_t dev)159c1b3d7c5SThomas E. Spanjaard afd_shutdown(device_t dev)
160c1b3d7c5SThomas E. Spanjaard {
161c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
162c1b3d7c5SThomas E. Spanjaard 
163c1b3d7c5SThomas E. Spanjaard     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
164c1b3d7c5SThomas E. Spanjaard 	ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
165c1b3d7c5SThomas E. Spanjaard }
166c1b3d7c5SThomas E. Spanjaard 
167c1b3d7c5SThomas E. Spanjaard static int
afd_reinit(device_t dev)168c1b3d7c5SThomas E. Spanjaard afd_reinit(device_t dev)
169c1b3d7c5SThomas E. Spanjaard {
170c1b3d7c5SThomas E. Spanjaard     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
171c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
172c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
173c1b3d7c5SThomas E. Spanjaard 
174c1b3d7c5SThomas E. Spanjaard     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
175c1b3d7c5SThomas E. Spanjaard 	((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
176c1b3d7c5SThomas E. Spanjaard 	device_set_ivars(dev, NULL);
177c1b3d7c5SThomas E. Spanjaard 	kfree(fdp, M_AFD);
178c1b3d7c5SThomas E. Spanjaard 	return 1;
179c1b3d7c5SThomas E. Spanjaard     }
180c1b3d7c5SThomas E. Spanjaard     ATA_SETMODE(device_get_parent(dev), dev);
181c1b3d7c5SThomas E. Spanjaard     return 0;
182c1b3d7c5SThomas E. Spanjaard }
183c1b3d7c5SThomas E. Spanjaard 
184c1b3d7c5SThomas E. Spanjaard static int
afd_open(struct dev_open_args * ap)185c1b3d7c5SThomas E. Spanjaard afd_open(struct dev_open_args *ap)
186c1b3d7c5SThomas E. Spanjaard {
187c1b3d7c5SThomas E. Spanjaard     device_t dev = ap->a_head.a_dev->si_drv1;
188c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
189c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
190a688b15cSMatthew Dillon     struct disk_info info;
191c1b3d7c5SThomas E. Spanjaard 
192c1b3d7c5SThomas E. Spanjaard     if (!fdp)
193c1b3d7c5SThomas E. Spanjaard 	return ENXIO;
194c1b3d7c5SThomas E. Spanjaard     if (!device_is_attached(dev))
195c1b3d7c5SThomas E. Spanjaard 	return EBUSY;
196c1b3d7c5SThomas E. Spanjaard 
197c1b3d7c5SThomas E. Spanjaard     afd_test_ready(dev);
198c1b3d7c5SThomas E. Spanjaard     afd_prevent_allow(dev, 1);
199c1b3d7c5SThomas E. Spanjaard 
200c1b3d7c5SThomas E. Spanjaard     if (afd_sense(dev))
201c1b3d7c5SThomas E. Spanjaard 	device_printf(dev, "sense media type failed\n");
202c1b3d7c5SThomas E. Spanjaard     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
203c1b3d7c5SThomas E. Spanjaard 
204c1b3d7c5SThomas E. Spanjaard     if (!fdp->mediasize)
205c1b3d7c5SThomas E. Spanjaard 	return ENXIO;
206c1b3d7c5SThomas E. Spanjaard 
207a688b15cSMatthew Dillon     bzero(&info, sizeof(info));
208a688b15cSMatthew Dillon     info.d_media_blksize = fdp->sectorsize;	/* mandatory */
209a688b15cSMatthew Dillon     info.d_media_size = fdp->mediasize;		/* (this is in bytes) */
210a688b15cSMatthew Dillon 
211a688b15cSMatthew Dillon     info.d_secpertrack = fdp->sectors;		/* optional */
212a688b15cSMatthew Dillon     info.d_nheads = fdp->heads;
213a688b15cSMatthew Dillon     info.d_ncylinders =
214c1b3d7c5SThomas E. Spanjaard 	   ((fdp->mediasize/fdp->sectorsize)/fdp->sectors)/fdp->heads;
215a688b15cSMatthew Dillon     info.d_secpercyl = fdp->sectors * fdp->heads;
216a688b15cSMatthew Dillon 
217a688b15cSMatthew Dillon     disk_setdiskinfo(&fdp->disk, &info);
218c1b3d7c5SThomas E. Spanjaard     return 0;
219c1b3d7c5SThomas E. Spanjaard }
220c1b3d7c5SThomas E. Spanjaard 
221c1b3d7c5SThomas E. Spanjaard static int
afd_close(struct dev_close_args * ap)222c1b3d7c5SThomas E. Spanjaard afd_close(struct dev_close_args *ap)
223c1b3d7c5SThomas E. Spanjaard {
224c1b3d7c5SThomas E. Spanjaard     device_t dev = ap->a_head.a_dev->si_drv1;
225c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
226c1b3d7c5SThomas E. Spanjaard 
227c1b3d7c5SThomas E. Spanjaard     if (count_dev(fdp->cdev) == 1)
228c1b3d7c5SThomas E. Spanjaard 	afd_prevent_allow(dev, 0);
229c1b3d7c5SThomas E. Spanjaard     return 0;
230c1b3d7c5SThomas E. Spanjaard }
231c1b3d7c5SThomas E. Spanjaard 
232c1b3d7c5SThomas E. Spanjaard static int
afd_strategy(struct dev_strategy_args * ap)233c1b3d7c5SThomas E. Spanjaard afd_strategy(struct dev_strategy_args *ap)
234c1b3d7c5SThomas E. Spanjaard {
235c1b3d7c5SThomas E. Spanjaard     device_t dev = ap->a_head.a_dev->si_drv1;
236c1b3d7c5SThomas E. Spanjaard     struct bio *bp = ap->a_bio;
237c1b3d7c5SThomas E. Spanjaard     struct buf *bbp = bp->bio_buf;
238c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
239c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
240c1b3d7c5SThomas E. Spanjaard     struct ata_request *request;
241c1b3d7c5SThomas E. Spanjaard     u_int32_t lba;
242c1b3d7c5SThomas E. Spanjaard     u_int16_t count;
243c1b3d7c5SThomas E. Spanjaard     int8_t ccb[16];
244c1b3d7c5SThomas E. Spanjaard 
245c1b3d7c5SThomas E. Spanjaard     /* if it's a null transfer, return immediatly. */
246c1b3d7c5SThomas E. Spanjaard     if (bbp->b_bcount == 0) {
247c1b3d7c5SThomas E. Spanjaard 	bbp->b_resid = 0;
248c1b3d7c5SThomas E. Spanjaard 	biodone(bp);
249c1b3d7c5SThomas E. Spanjaard 	return 0;
250c1b3d7c5SThomas E. Spanjaard     }
251c1b3d7c5SThomas E. Spanjaard 
252c1b3d7c5SThomas E. Spanjaard     /* should reject all queued entries if media have changed. */
253c1b3d7c5SThomas E. Spanjaard     if (atadev->flags & ATA_D_MEDIA_CHANGED) {
254bb15467aSzrj 	biofinish(bp, NULL, EIO);
255c1b3d7c5SThomas E. Spanjaard 	return 0;
256c1b3d7c5SThomas E. Spanjaard     }
257c1b3d7c5SThomas E. Spanjaard 
258c1b3d7c5SThomas E. Spanjaard     lba = bp->bio_offset / fdp->sectorsize;
259c1b3d7c5SThomas E. Spanjaard     count = bbp->b_bcount / fdp->sectorsize;
260c1b3d7c5SThomas E. Spanjaard     bbp->b_resid = bbp->b_bcount;
261c1b3d7c5SThomas E. Spanjaard 
262c1b3d7c5SThomas E. Spanjaard     bzero(ccb, sizeof(ccb));
263c1b3d7c5SThomas E. Spanjaard 
264b106cb48SMatthew Dillon     switch(bbp->b_cmd) {
265b106cb48SMatthew Dillon     case BUF_CMD_READ:
266c1b3d7c5SThomas E. Spanjaard 	ccb[0] = ATAPI_READ_BIG;
267b106cb48SMatthew Dillon 	break;
268b106cb48SMatthew Dillon     case BUF_CMD_WRITE:
269c1b3d7c5SThomas E. Spanjaard 	ccb[0] = ATAPI_WRITE_BIG;
270b106cb48SMatthew Dillon 	break;
271b106cb48SMatthew Dillon     default:
272b106cb48SMatthew Dillon 	device_printf(dev, "unknown BUF operation\n");
273bb15467aSzrj 	biofinish(bp, NULL, EIO);
274b106cb48SMatthew Dillon 	return 0;
275b106cb48SMatthew Dillon     }
276c1b3d7c5SThomas E. Spanjaard 
277c1b3d7c5SThomas E. Spanjaard     ccb[2] = lba >> 24;
278c1b3d7c5SThomas E. Spanjaard     ccb[3] = lba >> 16;
279c1b3d7c5SThomas E. Spanjaard     ccb[4] = lba >> 8;
280c1b3d7c5SThomas E. Spanjaard     ccb[5] = lba;
281c1b3d7c5SThomas E. Spanjaard     ccb[7] = count>>8;
282c1b3d7c5SThomas E. Spanjaard     ccb[8] = count;
283c1b3d7c5SThomas E. Spanjaard 
284c1b3d7c5SThomas E. Spanjaard     if (!(request = ata_alloc_request())) {
285bb15467aSzrj 	biofinish(bp, NULL, ENOMEM);
286c1b3d7c5SThomas E. Spanjaard 	return 0;
287c1b3d7c5SThomas E. Spanjaard     }
288c1b3d7c5SThomas E. Spanjaard     request->dev = dev;
289c1b3d7c5SThomas E. Spanjaard     request->bio = bp;
290c1b3d7c5SThomas E. Spanjaard     bcopy(ccb, request->u.atapi.ccb,
291c1b3d7c5SThomas E. Spanjaard 	  (atadev->param.config & ATA_PROTO_MASK) ==
292c1b3d7c5SThomas E. Spanjaard 	  ATA_PROTO_ATAPI_12 ? 16 : 12);
293c1b3d7c5SThomas E. Spanjaard     request->data = bbp->b_data;
294c1b3d7c5SThomas E. Spanjaard     request->bytecount = count * fdp->sectorsize;
295c1b3d7c5SThomas E. Spanjaard     request->transfersize = min(request->bytecount, 65534);
296c1b3d7c5SThomas E. Spanjaard     request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 60 : 30;
297c1b3d7c5SThomas E. Spanjaard     request->retries = 2;
298c1b3d7c5SThomas E. Spanjaard     request->callback = afd_done;
299b106cb48SMatthew Dillon 
300c1b3d7c5SThomas E. Spanjaard     switch (bbp->b_cmd) {
301c1b3d7c5SThomas E. Spanjaard     case BUF_CMD_READ:
302c1b3d7c5SThomas E. Spanjaard 	request->flags = (ATA_R_ATAPI | ATA_R_READ);
303c1b3d7c5SThomas E. Spanjaard 	break;
304c1b3d7c5SThomas E. Spanjaard     case BUF_CMD_WRITE:
305c1b3d7c5SThomas E. Spanjaard 	request->flags = (ATA_R_ATAPI | ATA_R_WRITE);
306c1b3d7c5SThomas E. Spanjaard 	break;
307c1b3d7c5SThomas E. Spanjaard     default:
308b106cb48SMatthew Dillon 	panic("bbp->b_cmd");
309c1b3d7c5SThomas E. Spanjaard     }
310c1b3d7c5SThomas E. Spanjaard     if (atadev->mode >= ATA_DMA)
311c1b3d7c5SThomas E. Spanjaard 	request->flags |= ATA_R_DMA;
312c1b3d7c5SThomas E. Spanjaard     request->flags |= ATA_R_ORDERED;
313c1b3d7c5SThomas E. Spanjaard     devstat_start_transaction(&fdp->stats);
314c1b3d7c5SThomas E. Spanjaard     ata_queue_request(request);
315c1b3d7c5SThomas E. Spanjaard     return 0;
316c1b3d7c5SThomas E. Spanjaard }
317c1b3d7c5SThomas E. Spanjaard 
318c1b3d7c5SThomas E. Spanjaard static void
afd_done(struct ata_request * request)319c1b3d7c5SThomas E. Spanjaard afd_done(struct ata_request *request)
320c1b3d7c5SThomas E. Spanjaard {
321c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(request->dev);
322c1b3d7c5SThomas E. Spanjaard     struct bio *bp = request->bio;
323c1b3d7c5SThomas E. Spanjaard     struct buf *bbp = bp->bio_buf;
324c1b3d7c5SThomas E. Spanjaard 
325c1b3d7c5SThomas E. Spanjaard     /* finish up transfer */
326c1b3d7c5SThomas E. Spanjaard     if ((bbp->b_error = request->result))
327c1b3d7c5SThomas E. Spanjaard 	bbp->b_flags |= B_ERROR;
328c1b3d7c5SThomas E. Spanjaard     bbp->b_resid = bbp->b_bcount - request->donecount;
329c1b3d7c5SThomas E. Spanjaard     devstat_end_transaction_buf(&fdp->stats, bbp);
330c1b3d7c5SThomas E. Spanjaard     biodone(bp);
331c1b3d7c5SThomas E. Spanjaard     ata_free_request(request);
332c1b3d7c5SThomas E. Spanjaard }
333c1b3d7c5SThomas E. Spanjaard 
334c1b3d7c5SThomas E. Spanjaard static int
afd_ioctl(struct dev_ioctl_args * ap)335bb15467aSzrj afd_ioctl(struct dev_ioctl_args *ap)
336bb15467aSzrj {
337bb15467aSzrj     return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data);
338bb15467aSzrj }
339bb15467aSzrj 
340bb15467aSzrj static int
afd_sense(device_t dev)341c1b3d7c5SThomas E. Spanjaard afd_sense(device_t dev)
342c1b3d7c5SThomas E. Spanjaard {
343c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
344c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
345c1b3d7c5SThomas E. Spanjaard     struct afd_capacity capacity;
346c1b3d7c5SThomas E. Spanjaard     struct afd_capacity_big capacity_big;
347c1b3d7c5SThomas E. Spanjaard     struct afd_capabilities capabilities;
348c1b3d7c5SThomas E. Spanjaard     int8_t ccb1[16] = { ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0,
349c1b3d7c5SThomas E. Spanjaard                         0, 0, 0, 0, 0, 0, 0, 0 };
350d55cd0cbSMatthew Dillon     int8_t ccb2[16] = { ATAPI_READ_CAPACITY_16, 0x10, 0, 0, 0, 0, 0, 0, 0, 0,
351c1b3d7c5SThomas E. Spanjaard 			0, 0, 0, sizeof(struct afd_capacity_big) & 0xff, 0, 0 };
352c1b3d7c5SThomas E. Spanjaard     int8_t ccb3[16] = { ATAPI_MODE_SENSE_BIG, 0, ATAPI_REWRITEABLE_CAP_PAGE,
353c1b3d7c5SThomas E. Spanjaard 		        0, 0, 0, 0, sizeof(struct afd_capabilities) >> 8,
354c1b3d7c5SThomas E. Spanjaard 		        sizeof(struct afd_capabilities) & 0xff,
355c1b3d7c5SThomas E. Spanjaard 			0, 0, 0, 0, 0, 0, 0 };
356c1b3d7c5SThomas E. Spanjaard     int timeout = 20;
357c1b3d7c5SThomas E. Spanjaard     int error, count;
358c1b3d7c5SThomas E. Spanjaard 
359c1b3d7c5SThomas E. Spanjaard     fdp->mediasize = 0;
360c1b3d7c5SThomas E. Spanjaard 
361c1b3d7c5SThomas E. Spanjaard     /* wait for device to get ready */
362c1b3d7c5SThomas E. Spanjaard     while ((error = afd_test_ready(dev)) && timeout--) {
363c1b3d7c5SThomas E. Spanjaard 	DELAY(100000);
364c1b3d7c5SThomas E. Spanjaard     }
365c1b3d7c5SThomas E. Spanjaard     if (error == EBUSY)
366c1b3d7c5SThomas E. Spanjaard 	return 1;
367c1b3d7c5SThomas E. Spanjaard 
368c1b3d7c5SThomas E. Spanjaard     /* The IOMEGA Clik! doesn't support reading the cap page, fake it */
369c1b3d7c5SThomas E. Spanjaard     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12)) {
370c1b3d7c5SThomas E. Spanjaard 	fdp->heads = 1;
371c1b3d7c5SThomas E. Spanjaard 	fdp->sectors = 2;
372c1b3d7c5SThomas E. Spanjaard 	fdp->mediasize = 39441 * 1024;
373c1b3d7c5SThomas E. Spanjaard 	fdp->sectorsize = 512;
374c1b3d7c5SThomas E. Spanjaard 	afd_test_ready(dev);
375c1b3d7c5SThomas E. Spanjaard 	return 0;
376c1b3d7c5SThomas E. Spanjaard     }
377c1b3d7c5SThomas E. Spanjaard 
378c1b3d7c5SThomas E. Spanjaard     /* get drive capacity */
379c1b3d7c5SThomas E. Spanjaard     if (!ata_atapicmd(dev, ccb1, (caddr_t)&capacity,
380c1b3d7c5SThomas E. Spanjaard 		      sizeof(struct afd_capacity), ATA_R_READ, 30)) {
381c1b3d7c5SThomas E. Spanjaard 	fdp->heads = 16;
382c1b3d7c5SThomas E. Spanjaard 	fdp->sectors = 63;
383c1b3d7c5SThomas E. Spanjaard 	fdp->sectorsize = be32toh(capacity.blocksize);
384c1b3d7c5SThomas E. Spanjaard 	fdp->mediasize = (u_int64_t)be32toh(capacity.capacity)*fdp->sectorsize;
385c1b3d7c5SThomas E. Spanjaard 	afd_test_ready(dev);
386c1b3d7c5SThomas E. Spanjaard 	return 0;
387c1b3d7c5SThomas E. Spanjaard     }
388c1b3d7c5SThomas E. Spanjaard 
389c1b3d7c5SThomas E. Spanjaard     /* get drive capacity big */
390c1b3d7c5SThomas E. Spanjaard     if (!ata_atapicmd(dev, ccb2, (caddr_t)&capacity_big,
391c1b3d7c5SThomas E. Spanjaard 		      sizeof(struct afd_capacity_big),
392c1b3d7c5SThomas E. Spanjaard 		      ATA_R_READ | ATA_R_QUIET, 30)) {
393c1b3d7c5SThomas E. Spanjaard 	fdp->heads = 16;
394c1b3d7c5SThomas E. Spanjaard 	fdp->sectors = 63;
395c1b3d7c5SThomas E. Spanjaard 	fdp->sectorsize = be32toh(capacity_big.blocksize);
396c1b3d7c5SThomas E. Spanjaard 	fdp->mediasize = be64toh(capacity_big.capacity)*fdp->sectorsize;
397c1b3d7c5SThomas E. Spanjaard 	afd_test_ready(dev);
398c1b3d7c5SThomas E. Spanjaard 	return 0;
399c1b3d7c5SThomas E. Spanjaard     }
400c1b3d7c5SThomas E. Spanjaard 
401c1b3d7c5SThomas E. Spanjaard     /* get drive capabilities, some bugridden drives needs this repeated */
402c1b3d7c5SThomas E. Spanjaard     for (count = 0 ; count < 5 ; count++) {
403c1b3d7c5SThomas E. Spanjaard 	if (!ata_atapicmd(dev, ccb3, (caddr_t)&capabilities,
404c1b3d7c5SThomas E. Spanjaard 			  sizeof(struct afd_capabilities), ATA_R_READ, 30) &&
405c1b3d7c5SThomas E. Spanjaard 	    capabilities.page_code == ATAPI_REWRITEABLE_CAP_PAGE) {
406c1b3d7c5SThomas E. Spanjaard 	    fdp->heads = capabilities.heads;
407c1b3d7c5SThomas E. Spanjaard 	    fdp->sectors = capabilities.sectors;
408c1b3d7c5SThomas E. Spanjaard 	    fdp->sectorsize = be16toh(capabilities.sector_size);
409c1b3d7c5SThomas E. Spanjaard 	    fdp->mediasize = be16toh(capabilities.cylinders) *
410c1b3d7c5SThomas E. Spanjaard 			     fdp->heads * fdp->sectors * fdp->sectorsize;
411c1b3d7c5SThomas E. Spanjaard 	    if (!capabilities.medium_type)
412c1b3d7c5SThomas E. Spanjaard 		fdp->mediasize = 0;
413c1b3d7c5SThomas E. Spanjaard 	    return 0;
414c1b3d7c5SThomas E. Spanjaard 	}
415c1b3d7c5SThomas E. Spanjaard     }
416c1b3d7c5SThomas E. Spanjaard     return 1;
417c1b3d7c5SThomas E. Spanjaard }
418c1b3d7c5SThomas E. Spanjaard 
419c1b3d7c5SThomas E. Spanjaard static int
afd_prevent_allow(device_t dev,int lock)420c1b3d7c5SThomas E. Spanjaard afd_prevent_allow(device_t dev, int lock)
421c1b3d7c5SThomas E. Spanjaard {
422c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
423c1b3d7c5SThomas E. Spanjaard     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
424c1b3d7c5SThomas E. Spanjaard 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
425c1b3d7c5SThomas E. Spanjaard 
426c1b3d7c5SThomas E. Spanjaard     if (!strncmp(atadev->param.model, "IOMEGA Clik!", 12))
427c1b3d7c5SThomas E. Spanjaard 	return 0;
428c1b3d7c5SThomas E. Spanjaard     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
429c1b3d7c5SThomas E. Spanjaard }
430c1b3d7c5SThomas E. Spanjaard 
431c1b3d7c5SThomas E. Spanjaard static int
afd_test_ready(device_t dev)432c1b3d7c5SThomas E. Spanjaard afd_test_ready(device_t dev)
433c1b3d7c5SThomas E. Spanjaard {
434c1b3d7c5SThomas E. Spanjaard     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
435c1b3d7c5SThomas E. Spanjaard 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
436c1b3d7c5SThomas E. Spanjaard 
437c1b3d7c5SThomas E. Spanjaard     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
438c1b3d7c5SThomas E. Spanjaard }
439c1b3d7c5SThomas E. Spanjaard 
440c1b3d7c5SThomas E. Spanjaard static void
afd_describe(device_t dev)441c1b3d7c5SThomas E. Spanjaard afd_describe(device_t dev)
442c1b3d7c5SThomas E. Spanjaard {
443c1b3d7c5SThomas E. Spanjaard     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
444c1b3d7c5SThomas E. Spanjaard     struct ata_device *atadev = device_get_softc(dev);
445c1b3d7c5SThomas E. Spanjaard     struct afd_softc *fdp = device_get_ivars(dev);
446c1b3d7c5SThomas E. Spanjaard     char sizestring[16];
447c1b3d7c5SThomas E. Spanjaard 
448c1b3d7c5SThomas E. Spanjaard     if (fdp->mediasize > 1048576 * 5)
449bb15467aSzrj 	ksprintf(sizestring, "%juMB", fdp->mediasize / 1048576);
450c1b3d7c5SThomas E. Spanjaard     else if (fdp->mediasize)
451bb15467aSzrj 	ksprintf(sizestring, "%juKB", fdp->mediasize / 1024);
452c1b3d7c5SThomas E. Spanjaard     else
453c1b3d7c5SThomas E. Spanjaard 	strcpy(sizestring, "(no media)");
454c1b3d7c5SThomas E. Spanjaard 
455c1b3d7c5SThomas E. Spanjaard     device_printf(dev, "%s <%.40s %.8s> at ata%d-%s %s\n",
456c1b3d7c5SThomas E. Spanjaard 		  sizestring, atadev->param.model, atadev->param.revision,
457cbf684e5Szrj 		  device_get_unit(ch->dev), ata_unit2str(atadev),
458c1b3d7c5SThomas E. Spanjaard 		  ata_mode2str(atadev->mode));
459c1b3d7c5SThomas E. Spanjaard     if (bootverbose) {
460bb15467aSzrj 	device_printf(dev, "%ju sectors [%juC/%dH/%dS]\n",
461bb15467aSzrj 		      fdp->mediasize / fdp->sectorsize,
462bb15467aSzrj 		     fdp->mediasize/(fdp->sectorsize*fdp->sectors*fdp->heads),
463c1b3d7c5SThomas E. Spanjaard 		      fdp->heads, fdp->sectors);
464c1b3d7c5SThomas E. Spanjaard     }
465c1b3d7c5SThomas E. Spanjaard }
466c1b3d7c5SThomas E. Spanjaard 
467c1b3d7c5SThomas E. Spanjaard static device_method_t afd_methods[] = {
468c1b3d7c5SThomas E. Spanjaard     /* device interface */
469c1b3d7c5SThomas E. Spanjaard     DEVMETHOD(device_probe,     afd_probe),
470c1b3d7c5SThomas E. Spanjaard     DEVMETHOD(device_attach,    afd_attach),
471c1b3d7c5SThomas E. Spanjaard     DEVMETHOD(device_detach,    afd_detach),
472c1b3d7c5SThomas E. Spanjaard     DEVMETHOD(device_shutdown,  afd_shutdown),
473c1b3d7c5SThomas E. Spanjaard 
474c1b3d7c5SThomas E. Spanjaard     /* ATA methods */
475c1b3d7c5SThomas E. Spanjaard     DEVMETHOD(ata_reinit,       afd_reinit),
476c1b3d7c5SThomas E. Spanjaard 
477d3c9c58eSSascha Wildner     DEVMETHOD_END
478c1b3d7c5SThomas E. Spanjaard };
479c1b3d7c5SThomas E. Spanjaard 
480c1b3d7c5SThomas E. Spanjaard static driver_t afd_driver = {
481c1b3d7c5SThomas E. Spanjaard     "afd",
482c1b3d7c5SThomas E. Spanjaard     afd_methods,
483c1b3d7c5SThomas E. Spanjaard     0,
484c1b3d7c5SThomas E. Spanjaard };
485c1b3d7c5SThomas E. Spanjaard 
486c1b3d7c5SThomas E. Spanjaard static devclass_t afd_devclass;
487c1b3d7c5SThomas E. Spanjaard 
488c1b3d7c5SThomas E. Spanjaard DRIVER_MODULE(afd, ata, afd_driver, afd_devclass, NULL, NULL);
489c1b3d7c5SThomas E. Spanjaard MODULE_VERSION(afd, 1);
490c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(afd, ata, 1, 1, 1);
491