kernel/nata: Return more data for natacontrol(8).
[dragonfly.git] / sys / dev / disk / nata / ata-disk.c
blob3666a425c5ef486a298d38cb642cdf29730250ec
1 /*-
2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
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.
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.199 2006/09/14 19:12:29 sos Exp $
29 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/bio.h>
33 #include <sys/buf.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/devicestat.h>
37 #include <sys/disk.h>
38 #include <sys/libkern.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/nata.h>
42 #include <sys/systm.h>
44 #include <vm/pmap.h>
46 #include <machine/md_var.h>
48 #include "ata-all.h"
49 #include "ata-disk.h"
50 #include "ata_if.h"
52 /* device structure */
53 static d_open_t ad_open;
54 static d_close_t ad_close;
55 static d_ioctl_t ad_ioctl;
56 static d_strategy_t ad_strategy;
57 static d_dump_t ad_dump;
58 static struct dev_ops ad_ops = {
59 { "ad", 0, D_DISK },
60 .d_open = ad_open,
61 .d_close = ad_close,
62 .d_read = physread,
63 .d_write = physwrite,
64 .d_ioctl = ad_ioctl,
65 .d_strategy = ad_strategy,
66 .d_dump = ad_dump,
69 /* prototypes */
70 static void ad_init(device_t);
71 static void ad_done(struct ata_request *);
72 static void ad_describe(device_t dev);
73 static int ad_version(u_int16_t);
75 /* local vars */
76 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
78 static int
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))
87 return 0;
88 else
89 return ENXIO;
92 static int
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);
97 struct disk_info info;
98 struct ad_softc *adp;
99 cdev_t cdev;
100 u_int32_t lbasize;
101 u_int64_t lbasize48;
103 /* check that we have a virgin disk to attach */
104 if (device_get_ivars(dev))
105 return EEXIST;
107 adp = kmalloc(sizeof(struct ad_softc), M_AD, M_INTWAIT | M_ZERO);
108 device_set_ivars(dev, adp);
110 if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
111 atadev->param.current_heads && atadev->param.current_sectors) {
112 adp->heads = atadev->param.current_heads;
113 adp->sectors = atadev->param.current_sectors;
114 adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
115 ((u_int32_t)atadev->param.current_size_2 << 16);
117 else {
118 adp->heads = atadev->param.heads;
119 adp->sectors = atadev->param.sectors;
120 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;
122 lbasize = (u_int32_t)atadev->param.lba_size_1 |
123 ((u_int32_t)atadev->param.lba_size_2 << 16);
125 /* does this device need oldstyle CHS addressing */
126 if (!ad_version(atadev->param.version_major) || !lbasize)
127 atadev->flags |= ATA_D_USE_CHS;
129 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
130 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
131 adp->total_secs = lbasize;
133 /* use the 48bit LBA size if valid */
134 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
135 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
136 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
137 ((u_int64_t)atadev->param.lba_size48_4 << 48);
138 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
139 lbasize48 > ATA_MAX_28BIT_LBA)
140 adp->total_secs = lbasize48;
142 /* init device parameters */
143 ad_init(dev);
145 /* create the disk device */
146 /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
147 DEVSTAT_PRIORITY_MAX. */
148 devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE,
149 DEVSTAT_NO_ORDERED_TAGS,
150 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
151 DEVSTAT_PRIORITY_DISK);
152 cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops);
153 cdev->si_drv1 = dev;
154 if (ch->dma)
155 cdev->si_iosize_max = ch->dma->max_iosize;
156 else
157 cdev->si_iosize_max = min(MAXPHYS,64*1024);
158 adp->cdev = cdev;
160 bzero(&info, sizeof(info));
161 info.d_media_blksize = DEV_BSIZE; /* mandatory */
162 info.d_media_blocks = adp->total_secs;
164 info.d_secpertrack = adp->sectors; /* optional */
165 info.d_nheads = adp->heads;
166 info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
167 info.d_secpercyl = adp->sectors * adp->heads;
168 info.d_serialno = atadev->param.serial;
170 device_add_child(dev, "subdisk", device_get_unit(dev));
171 bus_generic_attach(dev);
173 /* announce we are here */
174 ad_describe(dev);
176 disk_setdiskinfo(&adp->disk, &info);
178 #if defined(__DragonFly__)
179 callout_init_mp(&atadev->spindown_timer);
180 #else
181 callout_init(&atadev->spindown_timer, 1);
182 #endif
183 return 0;
186 static int
187 ad_detach(device_t dev)
189 struct ad_softc *adp = device_get_ivars(dev);
190 struct ata_device *atadev = device_get_softc(dev);
191 device_t *children;
192 int nchildren, i;
194 /* check that we have a valid disk to detach */
195 if (!adp)
196 return ENXIO;
198 /* destroy the power timeout */
199 callout_drain(&atadev->spindown_timer);
201 /* detach & delete all children */
202 if (!device_get_children(dev, &children, &nchildren)) {
203 for (i = 0; i < nchildren; i++)
204 if (children[i])
205 device_delete_child(dev, children[i]);
206 kfree(children, M_TEMP);
209 /* detroy disk from the system so we dont get any further requests */
210 disk_invalidate(&adp->disk);
211 disk_destroy(&adp->disk);
213 /* fail requests on the queue and any thats "in flight" for this device */
214 ata_fail_requests(dev);
216 /* dont leave anything behind */
217 /* disk_destroy() already took care of the dev_ops */
218 devstat_remove_entry(&adp->stats);
219 device_set_ivars(dev, NULL);
220 kfree(adp, M_AD);
221 return 0;
224 static void
225 ad_shutdown(device_t dev)
227 struct ata_device *atadev = device_get_softc(dev);
229 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
230 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
233 static int
234 ad_reinit(device_t dev)
236 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
237 struct ata_device *atadev = device_get_softc(dev);
239 /* if detach pending, return error */
240 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
241 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
242 return 1;
244 ad_init(dev);
245 return 0;
248 static void
249 ad_power_callback(struct ata_request *request)
251 device_printf(request->dev, "drive spun down.\n");
252 ata_free_request(request);
255 static void
256 ad_spindown(void *priv)
258 device_t dev = priv;
259 struct ata_device *atadev = device_get_softc(dev);
260 struct ata_request *request;
262 if (!atadev->spindown)
263 return;
264 device_printf(dev, "Idle, spin down\n");
265 atadev->spindown_state = 1;
266 if (!(request = ata_alloc_request())) {
267 device_printf(dev, "FAILURE - out of memory in ad_spindown\n");
268 return;
270 request->dev = dev;
271 request->flags = ATA_R_CONTROL;
272 request->timeout = ATA_DEFAULT_TIMEOUT;
273 request->retries = 1;
274 request->callback = ad_power_callback;
275 request->u.ata.command = ATA_STANDBY_IMMEDIATE;
276 ata_queue_request(request);
279 static int
280 ad_open(struct dev_open_args *ap)
282 device_t dev = ap->a_head.a_dev->si_drv1;
283 struct ad_softc *adp = device_get_ivars(dev);
285 if (!adp || adp->cdev == NULL)
286 return ENXIO;
287 if(!device_is_attached(dev))
288 return EBUSY;
290 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
291 adp->ad_flags &= AD_DISK_OPEN;
292 #endif /* 0 */
293 return 0;
296 static int
297 ad_close(struct dev_close_args *ap)
299 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
300 device_t dev = ap->a_head.a_dev->si_drv1;
301 struct ad_softc *adp = device_get_ivars(dev);
302 adp->ad_flags |= AD_DISK_OPEN;
303 #endif /* 0 */
304 return 0;
307 static int
308 ad_ioctl(struct dev_ioctl_args *ap)
310 return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data);
313 static int
314 ad_strategy(struct dev_strategy_args *ap)
316 device_t dev = ap->a_head.a_dev->si_drv1;
317 struct bio *bp = ap->a_bio;
318 struct buf *bbp = bp->bio_buf;
319 struct ata_device *atadev = device_get_softc(dev);
320 struct ata_request *request;
321 struct ad_softc *adp = device_get_ivars(dev);
323 if (atadev->spindown)
324 callout_reset(&atadev->spindown_timer, hz * atadev->spindown,
325 ad_spindown, dev);
327 if (!(request = ata_alloc_request())) {
328 device_printf(dev, "FAILURE - out of memory in strategy\n");
329 bbp->b_flags |= B_ERROR;
330 bbp->b_error = ENOMEM;
331 biodone(bp);
332 return(0);
335 /* setup request */
336 request->dev = dev;
337 request->bio = bp;
338 request->callback = ad_done;
339 if (atadev->spindown_state) {
340 device_printf(dev, "request while spun down, starting.\n");
341 atadev->spindown_state = 0;
342 request->timeout = MAX(ATA_DEFAULT_TIMEOUT, 31);
343 } else {
344 request->timeout = ATA_DEFAULT_TIMEOUT;
346 request->retries = 2;
347 request->data = bbp->b_data;
348 request->bytecount = bbp->b_bcount;
349 /* lba is block granularity, convert byte granularity bio_offset */
350 request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT);
351 request->u.ata.count = request->bytecount / DEV_BSIZE;
352 request->transfersize = min(bbp->b_bcount, atadev->max_iosize);
354 switch (bbp->b_cmd) {
355 case BUF_CMD_READ:
356 request->flags = ATA_R_READ;
357 if (atadev->mode >= ATA_DMA) {
358 request->u.ata.command = ATA_READ_DMA;
359 request->flags |= ATA_R_DMA;
361 else if (request->transfersize > DEV_BSIZE)
362 request->u.ata.command = ATA_READ_MUL;
363 else
364 request->u.ata.command = ATA_READ;
365 break;
366 case BUF_CMD_WRITE:
367 request->flags = ATA_R_WRITE;
368 if (atadev->mode >= ATA_DMA) {
369 request->u.ata.command = ATA_WRITE_DMA;
370 request->flags |= ATA_R_DMA;
372 else if (request->transfersize > DEV_BSIZE)
373 request->u.ata.command = ATA_WRITE_MUL;
374 else
375 request->u.ata.command = ATA_WRITE;
376 break;
377 case BUF_CMD_FLUSH:
378 request->u.ata.lba = 0;
379 request->u.ata.count = 0;
380 request->u.ata.feature = 0;
381 request->bytecount = 0;
382 request->transfersize = 0;
383 request->flags = ATA_R_CONTROL;
384 request->u.ata.command = ATA_FLUSHCACHE;
385 /* ATA FLUSHCACHE requests may take up to 30 sec to timeout */
386 request->timeout = 30;
387 break;
388 default:
389 device_printf(dev, "FAILURE - unknown BUF operation\n");
390 ata_free_request(request);
391 bbp->b_flags |= B_ERROR;
392 bbp->b_error = EIO;
393 biodone(bp);
394 return(0);
396 request->flags |= ATA_R_ORDERED;
397 devstat_start_transaction(&adp->stats);
398 ata_queue_request(request);
399 return(0);
402 static void
403 ad_done(struct ata_request *request)
405 struct ad_softc *adp = device_get_ivars(request->dev);
406 struct bio *bp = request->bio;
407 struct buf *bbp = bp->bio_buf;
409 /* finish up transfer */
410 if ((bbp->b_error = request->result))
411 bbp->b_flags |= B_ERROR;
412 bbp->b_resid = bbp->b_bcount - request->donecount;
413 devstat_end_transaction_buf(&adp->stats, bbp);
414 biodone(bp);
415 ata_free_request(request);
418 static int
419 ad_dump(struct dev_dump_args *ap)
421 device_t dev = ap->a_head.a_dev->si_drv1;
422 struct ata_device *atadev = device_get_softc(dev);
423 struct ata_request request;
425 ata_drop_requests(dev);
427 * 0 length means flush buffers and return
429 if (ap->a_length == 0) {
430 /* flush buffers to media */
431 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
432 return ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
433 else
434 return ENXIO;
437 bzero(&request, sizeof(struct ata_request));
438 request.dev = dev;
440 request.data = ap->a_virtual;
441 request.bytecount = ap->a_length;
442 request.transfersize = min(request.bytecount, atadev->max_iosize);
443 request.flags = ATA_R_WRITE;
445 if (atadev->mode >= ATA_DMA) {
446 request.u.ata.command = ATA_WRITE_DMA;
447 request.flags |= ATA_DMA;
448 } else if (request.transfersize > DEV_BSIZE)
449 request.u.ata.command = ATA_WRITE_MUL;
450 else
451 request.u.ata.command = ATA_WRITE;
452 request.u.ata.lba = ap->a_offset / DEV_BSIZE;
453 request.u.ata.count = request.bytecount / DEV_BSIZE;
455 request.timeout = ATA_DEFAULT_TIMEOUT;
456 request.retries = 2;
458 ata_queue_request(&request);
459 return request.result;
462 static void
463 ad_init(device_t dev)
465 struct ata_device *atadev = device_get_softc(dev);
467 ATA_SETMODE(device_get_parent(dev), dev);
469 /* enable readahead caching */
470 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
471 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
473 /* enable write caching if supported and configured */
474 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
475 if (ata_wc)
476 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
477 else
478 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
481 /* use multiple sectors/interrupt if device supports it */
482 if (ad_version(atadev->param.version_major)) {
483 int secsperint = max(1, min(atadev->param.sectors_intr & 0xff, 16));
485 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
486 atadev->max_iosize = secsperint * DEV_BSIZE;
488 else
489 atadev->max_iosize = DEV_BSIZE;
492 static void
493 ad_describe(device_t dev)
495 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
496 struct ata_device *atadev = device_get_softc(dev);
497 struct ad_softc *adp = device_get_ivars(dev);
498 u_int8_t *marker, vendor[64], product[64];
500 /* try to seperate the ATA model string into vendor and model parts */
501 if ((marker = index(atadev->param.model, ' ')) ||
502 (marker = index(atadev->param.model, '-'))) {
503 int len = (marker - atadev->param.model);
505 strncpy(vendor, atadev->param.model, len);
506 vendor[len++] = 0;
507 strcat(vendor, " ");
508 strncpy(product, atadev->param.model + len, 40 - len);
509 vendor[40 - len] = 0;
511 else {
512 if (!strncmp(atadev->param.model, "ST", 2))
513 strcpy(vendor, "Seagate ");
514 else if (!strncmp(atadev->param.model, "HDS", 3))
515 strcpy(vendor, "Hitachi ");
516 else
517 strcpy(vendor, "");
518 strncpy(product, atadev->param.model, 40);
521 device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n",
522 (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)),
523 vendor, product, atadev->param.revision,
524 device_get_unit(ch->dev),
525 (atadev->unit == ATA_MASTER) ? "master" : "slave",
526 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
527 ata_mode2str(atadev->mode));
528 if (bootverbose) {
529 device_printf(dev, "%llu sectors [%lluC/%dH/%dS] "
530 "%d sectors/interrupt %d depth queue\n",
531 (unsigned long long)adp->total_secs,(unsigned long long)(
532 adp->total_secs / (adp->heads * adp->sectors)),
533 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
534 adp->num_tags + 1);
538 static int
539 ad_version(u_int16_t version)
541 int bit;
543 if (version == 0xffff)
544 return 0;
545 for (bit = 15; bit >= 0; bit--)
546 if (version & (1<<bit))
547 return bit;
548 return 0;
551 static device_method_t ad_methods[] = {
552 /* device interface */
553 DEVMETHOD(device_probe, ad_probe),
554 DEVMETHOD(device_attach, ad_attach),
555 DEVMETHOD(device_detach, ad_detach),
556 DEVMETHOD(device_shutdown, ad_shutdown),
558 /* ATA methods */
559 DEVMETHOD(ata_reinit, ad_reinit),
561 DEVMETHOD_END
564 static driver_t ad_driver = {
565 "ad",
566 ad_methods,
570 devclass_t ad_devclass;
572 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
573 MODULE_VERSION(ad, 1);
574 MODULE_DEPEND(ad, ata, 1, 1, 1);