2 * Copyright (c) 1998 - 2008 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>
43 #include <sys/taskqueue.h>
45 #include <machine/md_var.h>
46 #include <machine/bus.h>
48 #include <geom/geom_disk.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
51 #include <dev/ata/ata-disk.h>
52 #include <dev/ata/ata-raid.h>
53 #include <dev/pci/pcivar.h>
57 static void ad_init(device_t dev);
58 static int ad_get_geometry(device_t dev);
59 static void ad_set_geometry(device_t dev);
60 static void ad_done(struct ata_request *request);
61 static void ad_describe(device_t dev);
62 static int ad_version(u_int16_t version);
63 static disk_strategy_t ad_strategy;
64 static disk_ioctl_t ad_ioctl;
65 static dumper_t ad_dump;
68 * Most platforms map firmware geom to actual, but some don't. If
69 * not overridden, default to nothing.
71 #ifndef ata_disk_firmware_geom_adjust
72 #define ata_disk_firmware_geom_adjust(disk)
76 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
79 ad_probe(device_t dev)
81 struct ata_device *atadev = device_get_softc(dev);
83 if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
84 (atadev->param.config == ATA_CFA_MAGIC1) ||
85 (atadev->param.config == ATA_CFA_MAGIC2) ||
86 (atadev->param.config == ATA_CFA_MAGIC3))
93 ad_attach(device_t dev)
95 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
96 struct ata_device *atadev = device_get_softc(dev);
100 /* check that we have a virgin disk to attach */
101 if (device_get_ivars(dev))
104 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
105 device_printf(dev, "out of memory\n");
108 device_set_ivars(dev, adp);
110 /* get device geometry into internal structs */
111 if (ad_get_geometry(dev))
114 /* set the max size if configured */
116 ad_set_geometry(dev);
118 /* init device parameters */
121 /* announce we are here */
124 /* create the disk device */
125 adp->disk = disk_alloc();
126 adp->disk->d_strategy = ad_strategy;
127 adp->disk->d_ioctl = ad_ioctl;
128 adp->disk->d_dump = ad_dump;
129 adp->disk->d_name = "ad";
130 adp->disk->d_drv1 = dev;
131 adp->disk->d_maxsize = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
132 if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48)
133 adp->disk->d_maxsize = min(adp->disk->d_maxsize, 65536 * DEV_BSIZE);
134 else /* 28bit ATA command limit */
135 adp->disk->d_maxsize = min(adp->disk->d_maxsize, 256 * DEV_BSIZE);
136 adp->disk->d_sectorsize = DEV_BSIZE;
137 adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
138 adp->disk->d_fwsectors = adp->sectors;
139 adp->disk->d_fwheads = adp->heads;
140 adp->disk->d_unit = device_get_unit(dev);
141 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
142 adp->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
143 if ((atadev->param.support.command2 & ATA_SUPPORT_CFA) ||
144 atadev->param.config == ATA_PROTO_CFA)
145 adp->disk->d_flags |= DISKFLAG_CANDELETE;
146 strlcpy(adp->disk->d_ident, atadev->param.serial,
147 sizeof(adp->disk->d_ident));
148 strlcpy(adp->disk->d_descr, atadev->param.model,
149 sizeof(adp->disk->d_descr));
150 parent = device_get_parent(ch->dev);
151 if (parent != NULL && device_get_parent(parent) != NULL &&
152 (device_get_devclass(parent) ==
153 devclass_find("atapci") ||
154 device_get_devclass(device_get_parent(parent)) ==
155 devclass_find("pci"))) {
156 adp->disk->d_hba_vendor = pci_get_vendor(parent);
157 adp->disk->d_hba_device = pci_get_device(parent);
158 adp->disk->d_hba_subvendor = pci_get_subvendor(parent);
159 adp->disk->d_hba_subdevice = pci_get_subdevice(parent);
161 ata_disk_firmware_geom_adjust(adp->disk);
162 disk_create(adp->disk, DISK_VERSION);
163 device_add_child(dev, "subdisk", device_get_unit(dev));
164 bus_generic_attach(dev);
166 callout_init(&atadev->spindown_timer, 1);
171 ad_detach(device_t dev)
173 struct ad_softc *adp = device_get_ivars(dev);
174 struct ata_device *atadev = device_get_softc(dev);
176 /* check that we have a valid disk to detach */
177 if (!device_get_ivars(dev))
180 /* destroy the power timeout */
181 callout_drain(&atadev->spindown_timer);
183 /* detach & delete all children */
184 device_delete_children(dev);
186 /* destroy disk from the system so we don't get any further requests */
187 disk_destroy(adp->disk);
189 /* fail requests on the queue and any that's "in flight" for this device */
190 ata_fail_requests(dev);
192 /* don't leave anything behind */
193 device_set_ivars(dev, NULL);
199 ad_shutdown(device_t dev)
201 struct ata_device *atadev = device_get_softc(dev);
203 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
204 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
209 ad_reinit(device_t dev)
211 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
212 struct ata_device *atadev = device_get_softc(dev);
214 /* if detach pending, return error */
215 if (!(ch->devices & (ATA_ATA_MASTER << atadev->unit)))
223 ad_power_callback(struct ata_request *request)
225 device_printf(request->dev, "drive spun down.\n");
226 ata_free_request(request);
230 ad_spindown(void *priv)
233 struct ata_device *atadev = device_get_softc(dev);
234 struct ata_request *request;
236 if (!atadev->spindown)
238 device_printf(dev, "Idle, spin down\n");
239 atadev->spindown_state = 1;
240 if (!(request = ata_alloc_request())) {
241 device_printf(dev, "FAILURE - out of memory in ad_spindown\n");
245 request->flags = ATA_R_CONTROL;
246 request->timeout = ATA_REQUEST_TIMEOUT;
247 request->retries = 1;
248 request->callback = ad_power_callback;
249 request->u.ata.command = ATA_STANDBY_IMMEDIATE;
250 ata_queue_request(request);
255 ad_strategy(struct bio *bp)
257 device_t dev = bp->bio_disk->d_drv1;
258 struct ata_device *atadev = device_get_softc(dev);
259 struct ata_request *request;
261 if (atadev->spindown)
262 callout_reset(&atadev->spindown_timer, hz * atadev->spindown,
265 if (!(request = ata_alloc_request())) {
266 device_printf(dev, "FAILURE - out of memory in start\n");
267 biofinish(bp, NULL, ENOMEM);
274 request->callback = ad_done;
275 if (atadev->spindown_state) {
276 device_printf(dev, "request while spun down, starting.\n");
277 atadev->spindown_state = 0;
278 request->timeout = MAX(ATA_REQUEST_TIMEOUT, 31);
281 request->timeout = ATA_REQUEST_TIMEOUT;
283 request->retries = 2;
284 request->data = bp->bio_data;
285 request->bytecount = bp->bio_bcount;
286 request->u.ata.lba = bp->bio_pblkno;
287 request->u.ata.count = request->bytecount / DEV_BSIZE;
288 request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
290 switch (bp->bio_cmd) {
292 request->flags = ATA_R_READ;
293 if (atadev->mode >= ATA_DMA) {
294 request->u.ata.command = ATA_READ_DMA;
295 request->flags |= ATA_R_DMA;
297 else if (request->transfersize > DEV_BSIZE)
298 request->u.ata.command = ATA_READ_MUL;
300 request->u.ata.command = ATA_READ;
303 request->flags = ATA_R_WRITE;
304 if (atadev->mode >= ATA_DMA) {
305 request->u.ata.command = ATA_WRITE_DMA;
306 request->flags |= ATA_R_DMA;
308 else if (request->transfersize > DEV_BSIZE)
309 request->u.ata.command = ATA_WRITE_MUL;
311 request->u.ata.command = ATA_WRITE;
314 request->flags = ATA_R_CONTROL;
315 request->u.ata.command = ATA_CFA_ERASE;
316 request->transfersize = 0;
317 request->donecount = bp->bio_bcount;
320 request->u.ata.lba = 0;
321 request->u.ata.count = 0;
322 request->u.ata.feature = 0;
323 request->bytecount = 0;
324 request->transfersize = 0;
325 request->flags = ATA_R_CONTROL;
326 request->u.ata.command = ATA_FLUSHCACHE;
329 device_printf(dev, "FAILURE - unknown BIO operation\n");
330 ata_free_request(request);
331 biofinish(bp, NULL, EIO);
334 request->flags |= ATA_R_ORDERED;
335 ata_queue_request(request);
339 ad_done(struct ata_request *request)
341 struct bio *bp = request->bio;
343 /* finish up transfer */
344 if ((bp->bio_error = request->result))
345 bp->bio_flags |= BIO_ERROR;
346 bp->bio_resid = bp->bio_bcount - request->donecount;
348 ata_free_request(request);
352 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
354 return ata_device_ioctl(disk->d_drv1, cmd, data);
358 ad_dump(void *arg, void *virtual, vm_offset_t physical,
359 off_t offset, size_t length)
361 struct disk *dp = arg;
362 device_t dev = dp->d_drv1;
365 /* XXX: Drop pre-dump request queue. Long request queue processing
366 * causes stack overflow in ATA working in dumping (interruptless) mode.
367 * Conter-XXX: To make dump coherent we should avoid doing anything
368 * else while dumping.
370 ata_drop_requests(dev);
372 /* length zero is special and really means flush buffers to media */
374 struct ata_device *atadev = device_get_softc(dev);
377 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
378 error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
382 bzero(&bp, sizeof(struct bio));
384 bp.bio_pblkno = offset / DEV_BSIZE;
385 bp.bio_bcount = length;
386 bp.bio_data = virtual;
387 bp.bio_cmd = BIO_WRITE;
393 ad_init(device_t dev)
395 struct ata_device *atadev = device_get_softc(dev);
399 /* enable readahead caching */
400 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
401 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
403 /* enable write caching if supported and configured */
404 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
406 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
408 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
411 /* use multiple sectors/interrupt if device supports it */
412 if (ad_version(atadev->param.version_major)) {
413 int secsperint = max(1, min(atadev->param.sectors_intr & 0xff, 16));
415 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
416 atadev->max_iosize = secsperint * DEV_BSIZE;
418 atadev->max_iosize = DEV_BSIZE;
421 atadev->max_iosize = DEV_BSIZE;
425 ad_get_geometry(device_t dev)
427 struct ata_device *atadev = device_get_softc(dev);
428 struct ad_softc *adp = device_get_ivars(dev);
432 if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
433 atadev->param.current_heads && atadev->param.current_sectors) {
434 adp->heads = atadev->param.current_heads;
435 adp->sectors = atadev->param.current_sectors;
436 adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
437 ((u_int32_t)atadev->param.current_size_2 << 16);
440 adp->heads = atadev->param.heads;
441 adp->sectors = atadev->param.sectors;
442 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;
444 lbasize = (u_int32_t)atadev->param.lba_size_1 |
445 ((u_int32_t)atadev->param.lba_size_2 << 16);
446 /* This device exists, but has no size. Filter out this bogus device. */
447 if (!lbasize && !adp->total_secs)
450 /* does this device need oldstyle CHS addressing */
451 if (!ad_version(atadev->param.version_major) || !lbasize)
452 atadev->flags |= ATA_D_USE_CHS;
454 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
455 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
456 adp->total_secs = lbasize;
458 /* use the 48bit LBA size if valid */
459 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
460 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
461 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
462 ((u_int64_t)atadev->param.lba_size48_4 << 48);
463 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
464 lbasize48 > ATA_MAX_28BIT_LBA)
465 adp->total_secs = lbasize48;
470 ad_set_geometry(device_t dev)
472 struct ad_softc *adp = device_get_ivars(dev);
473 struct ata_request *request;
476 device_printf(dev, "ORG %ju sectors [%juC/%dH/%dS]\n", adp->total_secs,
477 adp->total_secs / (adp->heads * adp->sectors),
478 adp->heads, adp->sectors);
480 if (!(request = ata_alloc_request()))
483 /* get the max native size the device supports */
485 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS;
486 request->u.ata.lba = 0;
487 request->u.ata.count = 0;
488 request->u.ata.feature = 0;
489 request->flags = ATA_R_CONTROL | ATA_R_QUIET;
490 request->timeout = ATA_REQUEST_TIMEOUT;
491 request->retries = 0;
492 ata_queue_request(request);
493 if (request->status & ATA_S_ERROR)
497 device_printf(dev, "MAX %ju sectors\n", request->u.ata.lba + 1);
499 /* if original size equals max size nothing more todo */
500 if (adp->total_secs >= request->u.ata.lba)
503 /* set the max native size to its max */
505 request->u.ata.command = ATA_SET_MAX_ADDRESS;
506 request->u.ata.count = 1;
507 request->u.ata.feature = 0;
508 request->flags = ATA_R_CONTROL;
509 request->timeout = ATA_REQUEST_TIMEOUT;
510 request->retries = 0;
511 ata_queue_request(request);
512 if (request->status & ATA_S_ERROR)
515 /* refresh geometry from drive */
516 ata_getparam(device_get_softc(dev), 0);
517 ad_get_geometry(dev);
519 device_printf(dev, "NEW %ju sectors [%juC/%dH/%dS]\n", adp->total_secs,
520 adp->total_secs / (adp->heads * adp->sectors),
521 adp->heads, adp->sectors);
523 ata_free_request(request);
527 ad_describe(device_t dev)
529 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
530 struct ata_device *atadev = device_get_softc(dev);
531 struct ad_softc *adp = device_get_ivars(dev);
532 u_int8_t *marker, vendor[64], product[64];
534 /* try to separate the ATA model string into vendor and model parts */
535 if ((marker = index(atadev->param.model, ' ')) ||
536 (marker = index(atadev->param.model, '-'))) {
537 int len = (marker - atadev->param.model);
539 strncpy(vendor, atadev->param.model, len);
542 strncpy(product, atadev->param.model + len, 40 - len);
543 vendor[40 - len] = 0;
546 if (!strncmp(atadev->param.model, "ST", 2))
547 strcpy(vendor, "Seagate ");
548 else if (!strncmp(atadev->param.model, "HDS", 3))
549 strcpy(vendor, "Hitachi ");
552 strncpy(product, atadev->param.model, 40);
555 device_printf(dev, "%juMB <%s%s %.8s> at ata%d-%s %s%s %s\n",
556 adp->total_secs / (1048576 / DEV_BSIZE),
557 vendor, product, atadev->param.revision,
558 device_get_unit(ch->dev), ata_unit2str(atadev),
559 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
560 ata_mode2str(atadev->mode),
561 ata_satarev2str(ATA_GETREV(device_get_parent(dev), atadev->unit)));
563 device_printf(dev, "%ju sectors [%juC/%dH/%dS] "
564 "%d sectors/interrupt %d depth queue\n", adp->total_secs,
565 adp->total_secs / (adp->heads * adp->sectors),
566 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
572 ad_version(u_int16_t version)
576 if (version == 0xffff)
578 for (bit = 15; bit >= 0; bit--)
579 if (version & (1<<bit))
584 static device_method_t ad_methods[] = {
585 /* device interface */
586 DEVMETHOD(device_probe, ad_probe),
587 DEVMETHOD(device_attach, ad_attach),
588 DEVMETHOD(device_detach, ad_detach),
589 DEVMETHOD(device_shutdown, ad_shutdown),
592 DEVMETHOD(ata_reinit, ad_reinit),
597 static driver_t ad_driver = {
603 devclass_t ad_devclass;
605 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
606 MODULE_VERSION(ad, 1);
607 MODULE_DEPEND(ad, ata, 1, 1, 1);