2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
46 #include <machine/md_var.h>
47 #include <machine/bus.h>
49 #include <geom/geom_disk.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/ata/ata-pci.h>
52 #include <dev/ata/ata-disk.h>
53 #include <dev/ata/ata-raid.h>
57 static void ad_init(device_t);
58 static void ad_done(struct ata_request *);
59 static void ad_describe(device_t dev);
60 static int ad_version(u_int16_t);
61 static disk_strategy_t ad_strategy;
62 static disk_ioctl_t ad_ioctl;
63 static dumper_t ad_dump;
66 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
69 ad_probe(device_t dev)
71 struct ata_device *atadev = device_get_softc(dev);
73 if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
74 (atadev->param.config == ATA_CFA_MAGIC))
81 ad_attach(device_t dev)
83 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
84 struct ata_device *atadev = device_get_softc(dev);
89 /* check that we have a virgin disk to attach */
90 if (device_get_ivars(dev))
93 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
94 device_printf(dev, "out of memory\n");
97 device_set_ivars(dev, adp);
99 if (atadev->param.atavalid & ATA_FLAG_54_58) {
100 adp->heads = atadev->param.current_heads;
101 adp->sectors = atadev->param.current_sectors;
102 adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
103 ((u_int32_t)atadev->param.current_size_2 << 16);
106 adp->heads = atadev->param.heads;
107 adp->sectors = atadev->param.sectors;
108 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;
110 lbasize = (u_int32_t)atadev->param.lba_size_1 |
111 ((u_int32_t)atadev->param.lba_size_2 << 16);
113 /* does this device need oldstyle CHS addressing */
114 if (!ad_version(atadev->param.version_major) || !lbasize)
115 atadev->flags |= ATA_D_USE_CHS;
117 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
118 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
119 adp->total_secs = lbasize;
121 /* use the 48bit LBA size if valid */
122 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
123 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
124 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
125 ((u_int64_t)atadev->param.lba_size48_4 << 48);
126 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
127 lbasize48 > ATA_MAX_28BIT_LBA)
128 adp->total_secs = lbasize48;
130 /* init device parameters */
133 /* announce we are here */
136 /* create the disk device */
137 adp->disk = disk_alloc();
138 adp->disk->d_strategy = ad_strategy;
139 adp->disk->d_ioctl = ad_ioctl;
140 adp->disk->d_dump = ad_dump;
141 adp->disk->d_name = "ad";
142 adp->disk->d_drv1 = dev;
144 adp->disk->d_maxsize = ch->dma->max_iosize;
146 adp->disk->d_maxsize = DFLTPHYS;
147 adp->disk->d_sectorsize = DEV_BSIZE;
148 adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
149 adp->disk->d_fwsectors = adp->sectors;
150 adp->disk->d_fwheads = adp->heads;
151 adp->disk->d_unit = device_get_unit(dev);
152 disk_create(adp->disk, DISK_VERSION);
153 device_add_child(dev, "subdisk", device_get_unit(dev));
154 bus_generic_attach(dev);
159 ad_detach(device_t dev)
161 struct ad_softc *adp = device_get_ivars(dev);
165 /* check that we have a valid disk to detach */
166 if (!device_get_ivars(dev))
169 /* detach & delete all children */
170 if (!device_get_children(dev, &children, &nchildren)) {
171 for (i = 0; i < nchildren; i++)
173 device_delete_child(dev, children[i]);
174 free(children, M_TEMP);
177 /* detroy disk from the system so we dont get any further requests */
178 disk_destroy(adp->disk);
180 /* fail requests on the queue and any thats "in flight" for this device */
181 ata_fail_requests(dev);
183 /* dont leave anything behind */
184 device_set_ivars(dev, NULL);
190 ad_shutdown(device_t dev)
192 struct ata_device *atadev = device_get_softc(dev);
194 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
195 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
199 ad_reinit(device_t dev)
201 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
202 struct ata_device *atadev = device_get_softc(dev);
204 /* if detach pending, return error */
205 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
206 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
214 ad_strategy(struct bio *bp)
216 device_t dev = bp->bio_disk->d_drv1;
217 struct ata_device *atadev = device_get_softc(dev);
218 struct ata_request *request;
220 if (!(request = ata_alloc_request())) {
221 device_printf(dev, "FAILURE - out of memory in start\n");
222 biofinish(bp, NULL, ENOMEM);
229 request->callback = ad_done;
230 request->timeout = 5;
231 request->retries = 2;
232 request->data = bp->bio_data;
233 request->bytecount = bp->bio_bcount;
234 request->u.ata.lba = bp->bio_pblkno;
235 request->u.ata.count = request->bytecount / DEV_BSIZE;
236 request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
238 switch (bp->bio_cmd) {
240 request->flags = ATA_R_READ;
241 if (atadev->mode >= ATA_DMA) {
242 request->u.ata.command = ATA_READ_DMA;
243 request->flags |= ATA_R_DMA;
245 else if (request->transfersize > DEV_BSIZE)
246 request->u.ata.command = ATA_READ_MUL;
248 request->u.ata.command = ATA_READ;
251 request->flags = ATA_R_WRITE;
252 if (atadev->mode >= ATA_DMA) {
253 request->u.ata.command = ATA_WRITE_DMA;
254 request->flags |= ATA_R_DMA;
256 else if (request->transfersize > DEV_BSIZE)
257 request->u.ata.command = ATA_WRITE_MUL;
259 request->u.ata.command = ATA_WRITE;
262 device_printf(dev, "FAILURE - unknown BIO operation\n");
263 ata_free_request(request);
264 biofinish(bp, NULL, EIO);
267 request->flags |= ATA_R_ORDERED;
268 ata_queue_request(request);
272 ad_done(struct ata_request *request)
274 struct bio *bp = request->bio;
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;
281 ata_free_request(request);
285 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
287 return ata_device_ioctl(disk->d_drv1, cmd, data);
291 ad_dump(void *arg, void *virtual, vm_offset_t physical,
292 off_t offset, size_t length)
294 struct disk *dp = arg;
295 device_t dev = dp->d_drv1;
296 struct ata_device *atadev = device_get_softc(dev);
297 struct ad_softc *adp = device_get_ivars(dev);
298 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
299 struct ata_request request;
304 bzero(&request, sizeof(struct ata_request));
308 request.data = virtual;
309 request.bytecount = length;
310 request.transfersize = min(length, atadev->max_iosize);
311 request.flags = ATA_R_WRITE;
312 if (atadev->max_iosize > DEV_BSIZE)
313 request.u.ata.command = ATA_WRITE_MUL;
315 request.u.ata.command = ATA_WRITE;
316 request.u.ata.lba = offset / DEV_BSIZE;
317 request.u.ata.count = request.bytecount / DEV_BSIZE;
320 request.u.ata.command = ATA_FLUSHCACHE;
321 request.flags = ATA_R_CONTROL;
323 if (ch->hw.begin_transaction(&request) == ATA_OP_CONTINUES) {
326 } while (ch->hw.end_transaction(&request) == ATA_OP_CONTINUES);
328 if (request.status & ATA_S_ERROR)
334 ad_init(device_t dev)
336 struct ata_device *atadev = device_get_softc(dev);
338 ATA_SETMODE(device_get_parent(dev), dev);
340 /* enable readahead caching */
341 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
342 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
344 /* enable write caching if supported and configured */
345 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
347 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
349 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
352 /* use multiple sectors/interrupt if device supports it */
353 if (ad_version(atadev->param.version_major)) {
354 int secsperint = max(1, min(atadev->param.sectors_intr, 16));
356 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
357 atadev->max_iosize = secsperint * DEV_BSIZE;
360 atadev->max_iosize = DEV_BSIZE;
364 ad_describe(device_t dev)
366 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
367 struct ata_device *atadev = device_get_softc(dev);
368 struct ad_softc *adp = device_get_ivars(dev);
369 u_int8_t *marker, vendor[64], product[64];
371 /* try to seperate the ATA model string into vendor and model parts */
372 if ((marker = index(atadev->param.model, ' ')) ||
373 (marker = index(atadev->param.model, '-'))) {
374 int len = (marker - atadev->param.model);
376 strncpy(vendor, atadev->param.model, len);
379 strncpy(product, atadev->param.model + len, 40 - len);
380 vendor[40 - len] = 0;
383 if (!strncmp(atadev->param.model, "ST", 2))
384 strcpy(vendor, "Seagate ");
387 strncpy(product, atadev->param.model, 40);
390 device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n",
391 (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)),
392 vendor, product, atadev->param.revision,
393 device_get_unit(ch->dev),
394 (atadev->unit == ATA_MASTER) ? "master" : "slave",
395 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
396 ata_mode2str(atadev->mode));
398 device_printf(dev, "%llu sectors [%lldC/%dH/%dS] "
399 "%d sectors/interrupt %d depth queue\n",
400 (unsigned long long)adp->total_secs,
401 (unsigned long long)(adp->total_secs /
402 (adp->heads * adp->sectors)),
403 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
409 ad_version(u_int16_t version)
413 if (version == 0xffff)
415 for (bit = 15; bit >= 0; bit--)
416 if (version & (1<<bit))
421 static device_method_t ad_methods[] = {
422 /* device interface */
423 DEVMETHOD(device_probe, ad_probe),
424 DEVMETHOD(device_attach, ad_attach),
425 DEVMETHOD(device_detach, ad_detach),
426 DEVMETHOD(device_shutdown, ad_shutdown),
429 DEVMETHOD(ata_reinit, ad_reinit),
434 static driver_t ad_driver = {
440 devclass_t ad_devclass;
442 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
443 MODULE_VERSION(ad, 1);
444 MODULE_DEPEND(ad, ata, 1, 1, 1);