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.
26 * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.197 2006/03/31 08:09:04 sos Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-disk.c,v 1.1 2006/12/04 14:40:37 tgen Exp $
32 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/devicestat.h>
39 #include <sys/libkern.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <sys/systm.h>
47 #include <machine/md_var.h>
53 /* device structure */
54 static d_open_t ad_open;
55 static d_close_t ad_close;
56 static d_ioctl_t ad_ioctl;
57 static d_strategy_t ad_strategy;
58 static d_dump_t ad_dump;
59 static struct dev_ops ad_ops = {
60 { "ad", 116, D_DISK },
66 .d_strategy = ad_strategy,
71 static void ad_init(device_t);
72 static void ad_done(struct ata_request *);
73 static void ad_describe(device_t dev);
74 static int ad_version(u_int16_t);
77 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
80 ad_probe(device_t dev)
82 struct ata_device *atadev = device_get_softc(dev);
84 if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
85 (atadev->param.config == ATA_CFA_MAGIC1) ||
86 (atadev->param.config == ATA_CFA_MAGIC2))
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);
102 /* check that we have a virgin disk to attach */
103 if (device_get_ivars(dev))
106 /* XXX TGEN We're not in interrupt context, so we can M_WAITOK and remove
108 if (!(adp = kmalloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
109 device_printf(dev, "out of memory\n");
112 device_set_ivars(dev, adp);
114 if (atadev->param.atavalid & ATA_FLAG_54_58) {
115 adp->heads = atadev->param.current_heads;
116 adp->sectors = atadev->param.current_sectors;
117 adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
118 ((u_int32_t)atadev->param.current_size_2 << 16);
121 adp->heads = atadev->param.heads;
122 adp->sectors = atadev->param.sectors;
123 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;
125 lbasize = (u_int32_t)atadev->param.lba_size_1 |
126 ((u_int32_t)atadev->param.lba_size_2 << 16);
128 /* does this device need oldstyle CHS addressing */
129 if (!ad_version(atadev->param.version_major) || !lbasize)
130 atadev->flags |= ATA_D_USE_CHS;
132 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
133 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
134 adp->total_secs = lbasize;
136 /* use the 48bit LBA size if valid */
137 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
138 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
139 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
140 ((u_int64_t)atadev->param.lba_size48_4 << 48);
141 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
142 lbasize48 > ATA_MAX_28BIT_LBA)
143 adp->total_secs = lbasize48;
145 /* init device parameters */
148 /* create the disk device */
149 /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
150 DEVSTAT_PRIORITY_MAX. */
151 devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE,
152 DEVSTAT_NO_ORDERED_TAGS,
153 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
154 DEVSTAT_PRIORITY_DISK);
155 cdev = disk_create(device_get_unit(dev), &adp->disk, 0, &ad_ops);
158 cdev->si_iosize_max = ch->dma->max_iosize;
160 cdev->si_iosize_max = DFLTPHYS;
162 bzero(&adp->disk.d_label, sizeof(struct disklabel));
163 adp->disk.d_label.d_secsize = DEV_BSIZE;
164 adp->disk.d_label.d_nsectors = adp->sectors;
165 adp->disk.d_label.d_ntracks = adp->heads;
166 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
167 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
168 adp->disk.d_label.d_secperunit = adp->total_secs;
169 device_add_child(dev, "subdisk", device_get_unit(dev));
170 bus_generic_attach(dev);
172 /* announce we are here */
178 ad_detach(device_t dev)
180 struct ad_softc *adp = device_get_ivars(dev);
184 /* check that we have a valid disk to detach */
188 #if 0 /* XXX TGEN Probably useless, we fail the queue below. */
189 /* check that the disk is closed */
190 if (adp->ad_flags & AD_DISK_OPEN)
194 /* detach & delete all children */
195 if (!device_get_children(dev, &children, &nchildren)) {
196 for (i = 0; i < nchildren; i++)
198 device_delete_child(dev, children[i]);
199 kfree(children, M_TEMP);
202 /* detroy disk from the system so we dont get any further requests */
203 disk_invalidate(&adp->disk);
204 disk_destroy(&adp->disk);
206 /* fail requests on the queue and any thats "in flight" for this device */
207 ata_fail_requests(dev);
209 /* dont leave anything behind */
210 /* disk_destroy() already took care of the dev_ops */
211 devstat_remove_entry(&adp->stats);
212 device_set_ivars(dev, NULL);
218 ad_shutdown(device_t dev)
220 struct ata_device *atadev = device_get_softc(dev);
222 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
223 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
227 ad_reinit(device_t dev)
229 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
230 struct ata_device *atadev = device_get_softc(dev);
232 /* if detach pending, return error */
233 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
234 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
242 ad_open(struct dev_open_args *ap)
244 device_t dev = ap->a_head.a_dev->si_drv1;
245 struct ad_softc *adp = device_get_ivars(dev);
247 if (!adp || adp->cdev == NULL)
249 if(!device_is_attached(dev))
252 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
253 adp->ad_flags &= AD_DISK_OPEN;
259 ad_close(struct dev_close_args *ap)
261 device_t dev = ap->a_head.a_dev->si_drv1;
262 struct ad_softc *adp = device_get_ivars(dev);
264 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
265 adp->ad_flags |= AD_DISK_OPEN;
271 ad_ioctl(struct dev_ioctl_args *ap)
273 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data);
277 ad_strategy(struct dev_strategy_args *ap)
279 device_t dev = ap->a_head.a_dev->si_drv1;
280 struct bio *bp = ap->a_bio;
281 struct buf *bbp = bp->bio_buf;
282 struct ata_device *atadev = device_get_softc(dev);
283 struct ata_request *request;
284 struct ad_softc *adp = device_get_ivars(dev);
286 if (!(request = ata_alloc_request())) {
287 device_printf(dev, "FAILURE - out of memory in strategy\n");
288 bbp->b_flags |= B_ERROR;
289 bbp->b_error = ENOMEM;
297 request->callback = ad_done;
298 request->timeout = 5;
299 request->retries = 2;
300 request->data = bbp->b_data;
301 request->bytecount = bbp->b_bcount;
302 /* lba is block granularity, convert byte granularity bio_offset */
303 request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT);
304 request->u.ata.count = request->bytecount / DEV_BSIZE;
305 request->transfersize = min(bbp->b_bcount, atadev->max_iosize);
307 switch (bbp->b_cmd) {
309 request->flags = ATA_R_READ;
310 if (atadev->mode >= ATA_DMA) {
311 request->u.ata.command = ATA_READ_DMA;
312 request->flags |= ATA_R_DMA;
314 else if (request->transfersize > DEV_BSIZE)
315 request->u.ata.command = ATA_READ_MUL;
317 request->u.ata.command = ATA_READ;
320 request->flags = ATA_R_WRITE;
321 if (atadev->mode >= ATA_DMA) {
322 request->u.ata.command = ATA_WRITE_DMA;
323 request->flags |= ATA_R_DMA;
325 else if (request->transfersize > DEV_BSIZE)
326 request->u.ata.command = ATA_WRITE_MUL;
328 request->u.ata.command = ATA_WRITE;
331 device_printf(dev, "FAILURE - unknown BUF operation\n");
332 ata_free_request(request);
333 bbp->b_flags |= B_ERROR;
338 request->flags |= ATA_R_ORDERED;
339 devstat_start_transaction(&adp->stats);
340 ata_queue_request(request);
345 ad_done(struct ata_request *request)
347 struct ad_softc *adp = device_get_ivars(request->dev);
348 struct bio *bp = request->bio;
349 struct buf *bbp = bp->bio_buf;
351 /* finish up transfer */
352 if ((bbp->b_error = request->result))
353 bbp->b_flags |= B_ERROR;
354 bbp->b_resid = bbp->b_bcount - request->donecount;
355 devstat_end_transaction_buf(&adp->stats, bbp);
357 ata_free_request(request);
361 ad_dump(struct dev_dump_args *ap)
363 device_t dev = ap->a_head.a_dev->si_drv1;
364 struct ata_device *atadev = device_get_softc(dev);
367 struct dev_strategy_args dsap;
370 int dumppages = MAXDUMPPGS;
375 /* XXX TGEN Figure out if we need to support this as well. */
376 /* XXX TGEN Seems like we don't, ad_dump isn't called recursively. */
377 /* length zero is special and really means flush buffers to media */
379 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
380 error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
385 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
387 while (ap->a_count > 0) {
390 if ((ap->a_count /blkcnt) < dumppages)
391 dumppages = ap->a_count / blkcnt;
393 for (i = 0; i < dumppages; ++i) {
394 vm_paddr_t a = addr + (i * PAGE_SIZE);
395 if (is_physical_memory(a))
396 va = pmap_kenter_temporary(trunc_page(a), i);
398 va = pmap_kenter_temporary(trunc_page(0), i);
401 bzero(&bp, sizeof(struct bio));
402 bzero(&bbp, sizeof(struct buf));
403 bzero(&dsap, sizeof(struct dev_strategy_args));
405 /* bio_offset is byte granularity, convert block granularity a_blkno */
406 bp.bio_offset = (off_t)(ap->a_blkno << DEV_BSHIFT);
407 bbp.b_bcount = PAGE_SIZE * dumppages;
409 bbp.b_cmd = BUF_CMD_WRITE;
410 /* XXX TGEN With the previous bzero probably not efficient. */
411 dsap.a_head = ap->a_head;
417 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
420 ap->a_blkno += blkcnt * dumppages;
421 ap->a_count -= blkcnt * dumppages;
422 addr += PAGE_SIZE * dumppages;
425 /* flush buffers to media */
426 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
427 error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
432 ad_init(device_t dev)
434 struct ata_device *atadev = device_get_softc(dev);
436 ATA_SETMODE(device_get_parent(dev), dev);
438 /* enable readahead caching */
439 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
440 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
442 /* enable write caching if supported and configured */
443 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
445 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
447 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
450 /* use multiple sectors/interrupt if device supports it */
451 if (ad_version(atadev->param.version_major)) {
452 int secsperint = max(1, min(atadev->param.sectors_intr, 16));
454 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
455 atadev->max_iosize = secsperint * DEV_BSIZE;
458 atadev->max_iosize = DEV_BSIZE;
462 ad_describe(device_t dev)
464 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
465 struct ata_device *atadev = device_get_softc(dev);
466 struct ad_softc *adp = device_get_ivars(dev);
467 u_int8_t *marker, vendor[64], product[64];
469 /* try to seperate the ATA model string into vendor and model parts */
470 if ((marker = index(atadev->param.model, ' ')) ||
471 (marker = index(atadev->param.model, '-'))) {
472 int len = (marker - atadev->param.model);
474 strncpy(vendor, atadev->param.model, len);
477 strncpy(product, atadev->param.model + len, 40 - len);
478 vendor[40 - len] = 0;
481 if (!strncmp(atadev->param.model, "ST", 2))
482 strcpy(vendor, "Seagate ");
485 strncpy(product, atadev->param.model, 40);
488 device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n",
489 (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)),
490 vendor, product, atadev->param.revision,
491 device_get_unit(ch->dev),
492 (atadev->unit == ATA_MASTER) ? "master" : "slave",
493 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
494 ata_mode2str(atadev->mode));
496 device_printf(dev, "%llu sectors [%lluC/%dH/%dS] "
497 "%d sectors/interrupt %d depth queue\n",
498 (unsigned long long)adp->total_secs,(unsigned long long)(
499 adp->total_secs / (adp->heads * adp->sectors)),
500 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
506 ad_version(u_int16_t version)
510 if (version == 0xffff)
512 for (bit = 15; bit >= 0; bit--)
513 if (version & (1<<bit))
518 static device_method_t ad_methods[] = {
519 /* device interface */
520 DEVMETHOD(device_probe, ad_probe),
521 DEVMETHOD(device_attach, ad_attach),
522 DEVMETHOD(device_detach, ad_detach),
523 DEVMETHOD(device_shutdown, ad_shutdown),
526 DEVMETHOD(ata_reinit, ad_reinit),
531 static driver_t ad_driver = {
537 devclass_t ad_devclass;
539 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
540 MODULE_VERSION(ad, 1);
541 MODULE_DEPEND(ad, ata, 1, 1, 1);