]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/ata/ata-disk.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / dev / ata / ata-disk.c
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ata.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/bio.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/disk.h>
41 #include <sys/cons.h>
42 #include <sys/sema.h>
43 #include <sys/taskqueue.h>
44 #include <vm/uma.h>
45 #include <machine/md_var.h>
46 #include <machine/bus.h>
47 #include <sys/rman.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 <ata_if.h>
54
55 /* prototypes */
56 static void ad_init(device_t dev);
57 static int ad_get_geometry(device_t dev);
58 static void ad_set_geometry(device_t dev);
59 static void ad_done(struct ata_request *request);
60 static void ad_describe(device_t dev);
61 static int ad_version(u_int16_t version);
62 static disk_strategy_t ad_strategy;
63 static disk_ioctl_t ad_ioctl;
64 static dumper_t ad_dump;
65
66 /*
67  * Most platforms map firmware geom to actual, but some don't.  If
68  * not overridden, default to nothing.
69  */
70 #ifndef ata_disk_firmware_geom_adjust
71 #define ata_disk_firmware_geom_adjust(disk)
72 #endif
73
74 /* local vars */
75 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
76
77 static int
78 ad_probe(device_t dev)
79 {
80     struct ata_device *atadev = device_get_softc(dev);
81
82     if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
83         (atadev->param.config == ATA_CFA_MAGIC1) ||
84         (atadev->param.config == ATA_CFA_MAGIC2) ||
85         (atadev->param.config == ATA_CFA_MAGIC3))
86         return 0;
87     else
88         return ENXIO;
89 }
90
91 static int
92 ad_attach(device_t dev)
93 {
94     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
95     struct ata_device *atadev = device_get_softc(dev);
96     struct ad_softc *adp;
97
98     /* check that we have a virgin disk to attach */
99     if (device_get_ivars(dev))
100         return EEXIST;
101
102     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
103         device_printf(dev, "out of memory\n");
104         return ENOMEM;
105     }
106     device_set_ivars(dev, adp);
107
108     /* get device geometry into internal structs */
109     if (ad_get_geometry(dev))
110         return ENXIO;
111
112     /* set the max size if configured */
113     if (ata_setmax)
114         ad_set_geometry(dev);
115
116     /* init device parameters */
117     ad_init(dev);
118
119     /* announce we are here */
120     ad_describe(dev);
121
122     /* create the disk device */
123     adp->disk = disk_alloc();
124     adp->disk->d_strategy = ad_strategy;
125     adp->disk->d_ioctl = ad_ioctl;
126     adp->disk->d_dump = ad_dump;
127     adp->disk->d_name = "ad";
128     adp->disk->d_drv1 = dev;
129     adp->disk->d_maxsize = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
130     if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48)
131         adp->disk->d_maxsize = min(adp->disk->d_maxsize, 65536 * DEV_BSIZE);
132     else                                        /* 28bit ATA command limit */
133         adp->disk->d_maxsize = min(adp->disk->d_maxsize, 256 * DEV_BSIZE);
134     adp->disk->d_sectorsize = DEV_BSIZE;
135     adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
136     adp->disk->d_fwsectors = adp->sectors;
137     adp->disk->d_fwheads = adp->heads;
138     adp->disk->d_unit = device_get_unit(dev);
139     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
140         adp->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
141     if ((atadev->param.support.command2 & ATA_SUPPORT_CFA) ||
142         atadev->param.config == ATA_PROTO_CFA)
143         adp->disk->d_flags |= DISKFLAG_CANDELETE;
144     strlcpy(adp->disk->d_ident, atadev->param.serial,
145         sizeof(adp->disk->d_ident));
146     ata_disk_firmware_geom_adjust(adp->disk);
147     disk_create(adp->disk, DISK_VERSION);
148     device_add_child(dev, "subdisk", device_get_unit(dev));
149     bus_generic_attach(dev);
150
151     callout_init(&atadev->spindown_timer, 1);
152     return 0;
153 }
154
155 static int
156 ad_detach(device_t dev)
157 {
158     struct ad_softc *adp = device_get_ivars(dev);
159     struct ata_device *atadev = device_get_softc(dev);
160     device_t *children;
161     int nchildren, i;
162
163     /* check that we have a valid disk to detach */
164     if (!device_get_ivars(dev))
165         return ENXIO;
166     
167     /* destroy the power timeout */
168     callout_drain(&atadev->spindown_timer);
169
170     /* detach & delete all children */
171     if (!device_get_children(dev, &children, &nchildren)) {
172         for (i = 0; i < nchildren; i++)
173             if (children[i])
174                 device_delete_child(dev, children[i]);
175         free(children, M_TEMP);
176     }
177
178     /* destroy disk from the system so we don't get any further requests */
179     disk_destroy(adp->disk);
180
181     /* fail requests on the queue and any that's "in flight" for this device */
182     ata_fail_requests(dev);
183
184     /* don't leave anything behind */
185     device_set_ivars(dev, NULL);
186     free(adp, M_AD);
187     return 0;
188 }
189
190 static int
191 ad_shutdown(device_t dev)
192 {
193     struct ata_device *atadev = device_get_softc(dev);
194
195     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
196         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
197     return 0;
198 }
199
200 static int
201 ad_reinit(device_t dev)
202 {
203     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
204     struct ata_device *atadev = device_get_softc(dev);
205
206     /* if detach pending, return error */
207     if (!(ch->devices & (ATA_ATA_MASTER << atadev->unit)))
208         return 1;
209
210     ad_init(dev);
211     return 0;
212 }
213
214 static void
215 ad_power_callback(struct ata_request *request)
216 {
217     device_printf(request->dev, "drive spun down.\n");
218     ata_free_request(request);
219 }
220
221 static void
222 ad_spindown(void *priv)
223 {
224     device_t dev = priv;
225     struct ata_device *atadev = device_get_softc(dev);
226     struct ata_request *request;
227
228     if (!atadev->spindown)
229         return;
230     device_printf(dev, "Idle, spin down\n");
231     atadev->spindown_state = 1;
232     if (!(request = ata_alloc_request())) {
233         device_printf(dev, "FAILURE - out of memory in ad_spindown\n");
234         return;
235     }
236     request->dev = dev;
237     request->flags = ATA_R_CONTROL;
238     request->timeout = ATA_REQUEST_TIMEOUT;
239     request->retries = 1;
240     request->callback = ad_power_callback;
241     request->u.ata.command = ATA_STANDBY_IMMEDIATE;
242     ata_queue_request(request);
243 }
244
245
246 static void 
247 ad_strategy(struct bio *bp)
248 {
249     device_t dev =  bp->bio_disk->d_drv1;
250     struct ata_device *atadev = device_get_softc(dev);
251     struct ata_request *request;
252
253     if (atadev->spindown)
254         callout_reset(&atadev->spindown_timer, hz * atadev->spindown,
255                       ad_spindown, dev);
256
257     if (!(request = ata_alloc_request())) {
258         device_printf(dev, "FAILURE - out of memory in start\n");
259         biofinish(bp, NULL, ENOMEM);
260         return;
261     }
262
263     /* setup request */
264     request->dev = dev;
265     request->bio = bp;
266     request->callback = ad_done;
267     if (atadev->spindown_state) {
268         device_printf(dev, "request while spun down, starting.\n");
269         atadev->spindown_state = 0;
270         request->timeout = MAX(ATA_REQUEST_TIMEOUT, 31);
271     }
272     else {
273         request->timeout = ATA_REQUEST_TIMEOUT;
274     }
275     request->retries = 2;
276     request->data = bp->bio_data;
277     request->bytecount = bp->bio_bcount;
278     request->u.ata.lba = bp->bio_pblkno;
279     request->u.ata.count = request->bytecount / DEV_BSIZE;
280     request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
281
282     switch (bp->bio_cmd) {
283     case BIO_READ:
284         request->flags = ATA_R_READ;
285         if (atadev->mode >= ATA_DMA) {
286             request->u.ata.command = ATA_READ_DMA;
287             request->flags |= ATA_R_DMA;
288         }
289         else if (request->transfersize > DEV_BSIZE)
290             request->u.ata.command = ATA_READ_MUL;
291         else
292             request->u.ata.command = ATA_READ;
293         break;
294     case BIO_WRITE:
295         request->flags = ATA_R_WRITE;
296         if (atadev->mode >= ATA_DMA) {
297             request->u.ata.command = ATA_WRITE_DMA;
298             request->flags |= ATA_R_DMA;
299         }
300         else if (request->transfersize > DEV_BSIZE)
301             request->u.ata.command = ATA_WRITE_MUL;
302         else
303             request->u.ata.command = ATA_WRITE;
304         break;
305     case BIO_DELETE:
306         request->flags = ATA_R_CONTROL;
307         request->u.ata.command = ATA_CFA_ERASE;
308         request->transfersize = 0;
309         request->donecount = bp->bio_bcount;
310         break;
311     case BIO_FLUSH:
312         request->u.ata.lba = 0;
313         request->u.ata.count = 0;
314         request->u.ata.feature = 0;
315         request->bytecount = 0;
316         request->transfersize = 0;
317         request->flags = ATA_R_CONTROL;
318         request->u.ata.command = ATA_FLUSHCACHE;
319         break;
320     default:
321         device_printf(dev, "FAILURE - unknown BIO operation\n");
322         ata_free_request(request);
323         biofinish(bp, NULL, EIO);
324         return;
325     }
326     request->flags |= ATA_R_ORDERED;
327     ata_queue_request(request);
328 }
329
330 static void
331 ad_done(struct ata_request *request)
332 {
333     struct bio *bp = request->bio;
334
335     /* finish up transfer */
336     if ((bp->bio_error = request->result))
337         bp->bio_flags |= BIO_ERROR;
338     bp->bio_resid = bp->bio_bcount - request->donecount;
339     biodone(bp);
340     ata_free_request(request);
341 }
342
343 static int
344 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
345 {
346     return ata_device_ioctl(disk->d_drv1, cmd, data);
347 }
348
349 static int
350 ad_dump(void *arg, void *virtual, vm_offset_t physical,
351         off_t offset, size_t length)
352 {
353     struct disk *dp = arg;
354     device_t dev = dp->d_drv1;
355     struct bio bp;
356
357     /* XXX: Drop pre-dump request queue. Long request queue processing
358      * causes stack overflow in ATA working in dumping (interruptless) mode.
359      * Conter-XXX: To make dump coherent we should avoid doing anything
360      * else while dumping.
361      */
362     ata_drop_requests(dev);
363
364     /* length zero is special and really means flush buffers to media */
365     if (!length) {
366         struct ata_device *atadev = device_get_softc(dev);
367         int error = 0;
368
369         if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
370             error = ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
371         return error;
372     }
373
374     bzero(&bp, sizeof(struct bio));
375     bp.bio_disk = dp;
376     bp.bio_pblkno = offset / DEV_BSIZE;
377     bp.bio_bcount = length;
378     bp.bio_data = virtual;
379     bp.bio_cmd = BIO_WRITE;
380     ad_strategy(&bp);
381     return bp.bio_error;
382 }
383
384 static void
385 ad_init(device_t dev)
386 {
387     struct ata_device *atadev = device_get_softc(dev);
388
389     ata_setmode(dev);
390
391     /* enable readahead caching */
392     if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
393         ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
394
395     /* enable write caching if supported and configured */
396     if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
397         if (ata_wc)
398             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
399         else
400             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
401     }
402
403     /* use multiple sectors/interrupt if device supports it */
404     if (ad_version(atadev->param.version_major)) {
405         int secsperint = max(1, min(atadev->param.sectors_intr & 0xff, 16));
406
407         if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
408             atadev->max_iosize = secsperint * DEV_BSIZE;
409         else
410             atadev->max_iosize = DEV_BSIZE;
411     }
412     else
413         atadev->max_iosize = DEV_BSIZE;
414 }
415
416 static int
417 ad_get_geometry(device_t dev)
418 {
419     struct ata_device *atadev = device_get_softc(dev);
420     struct ad_softc *adp = device_get_ivars(dev);
421     u_int64_t lbasize48;
422     u_int32_t lbasize;
423
424     if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
425         atadev->param.current_heads && atadev->param.current_sectors) {
426         adp->heads = atadev->param.current_heads;
427         adp->sectors = atadev->param.current_sectors;
428         adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
429                           ((u_int32_t)atadev->param.current_size_2 << 16);
430     }
431     else {
432         adp->heads = atadev->param.heads;
433         adp->sectors = atadev->param.sectors;
434         adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;  
435     }
436     lbasize = (u_int32_t)atadev->param.lba_size_1 |
437               ((u_int32_t)atadev->param.lba_size_2 << 16);
438     /* This device exists, but has no size.  Filter out this bogus device. */
439     if (!lbasize && !adp->total_secs)
440         return ENXIO;
441
442     /* does this device need oldstyle CHS addressing */
443     if (!ad_version(atadev->param.version_major) || !lbasize)
444         atadev->flags |= ATA_D_USE_CHS;
445
446     /* use the 28bit LBA size if valid or bigger than the CHS mapping */
447     if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
448         adp->total_secs = lbasize;
449
450     /* use the 48bit LBA size if valid */
451     lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
452                 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
453                 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
454                 ((u_int64_t)atadev->param.lba_size48_4 << 48);
455     if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
456         lbasize48 > ATA_MAX_28BIT_LBA)
457         adp->total_secs = lbasize48;
458     return 0;
459 }
460
461 static void
462 ad_set_geometry(device_t dev)
463 {
464     struct ad_softc *adp = device_get_ivars(dev);
465     struct ata_request *request;
466
467     if (1 | bootverbose)
468         device_printf(dev, "ORG %ju sectors [%juC/%dH/%dS]\n", adp->total_secs,
469                       adp->total_secs / (adp->heads * adp->sectors),
470                       adp->heads, adp->sectors);
471
472     if (!(request = ata_alloc_request()))
473         return;
474
475     /* get the max native size the device supports */
476     request->dev = dev;
477     request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS;
478     request->u.ata.lba = 0;
479     request->u.ata.count = 0;
480     request->u.ata.feature = 0;
481     request->flags = ATA_R_CONTROL | ATA_R_QUIET;
482     request->timeout = ATA_REQUEST_TIMEOUT;
483     request->retries = 0;
484     ata_queue_request(request);
485     if (request->status & ATA_S_ERROR)
486         goto out;
487
488     if (1 | bootverbose)
489         device_printf(dev, "MAX %ju sectors\n", request->u.ata.lba + 1);
490
491     /* if original size equals max size nothing more todo */
492     if (adp->total_secs >= request->u.ata.lba)
493         goto out;
494
495     /* set the max native size to its max */
496     request->dev = dev;
497     request->u.ata.command = ATA_SET_MAX_ADDRESS;
498     request->u.ata.count = 1;
499     request->u.ata.feature = 0;
500     request->flags = ATA_R_CONTROL;
501     request->timeout = ATA_REQUEST_TIMEOUT;
502     request->retries = 0;
503     ata_queue_request(request);
504     if (request->status & ATA_S_ERROR)
505         goto out;
506
507     /* refresh geometry from drive */
508     ata_getparam(device_get_softc(dev), 0);
509     ad_get_geometry(dev);
510     if (1 | bootverbose)
511         device_printf(dev, "NEW %ju sectors [%juC/%dH/%dS]\n", adp->total_secs,
512                       adp->total_secs / (adp->heads * adp->sectors),
513                       adp->heads, adp->sectors);
514 out:
515     ata_free_request(request);
516 }
517
518 static void
519 ad_describe(device_t dev)
520 {
521     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
522     struct ata_device *atadev = device_get_softc(dev);
523     struct ad_softc *adp = device_get_ivars(dev);
524     u_int8_t *marker, vendor[64], product[64];
525
526     /* try to separate the ATA model string into vendor and model parts */
527     if ((marker = index(atadev->param.model, ' ')) ||
528         (marker = index(atadev->param.model, '-'))) {
529         int len = (marker - atadev->param.model);
530
531         strncpy(vendor, atadev->param.model, len);
532         vendor[len++] = 0;
533         strcat(vendor, " ");
534         strncpy(product, atadev->param.model + len, 40 - len);
535         vendor[40 - len] = 0;
536     }
537     else {
538         if (!strncmp(atadev->param.model, "ST", 2))
539             strcpy(vendor, "Seagate ");
540         else if (!strncmp(atadev->param.model, "HDS", 3))
541             strcpy(vendor, "Hitachi ");
542         else
543             strcpy(vendor, "");
544         strncpy(product, atadev->param.model, 40);
545     }
546
547     device_printf(dev, "%juMB <%s%s %.8s> at ata%d-%s %s%s %s\n",
548                   adp->total_secs / (1048576 / DEV_BSIZE),
549                   vendor, product, atadev->param.revision,
550                   device_get_unit(ch->dev), ata_unit2str(atadev),
551                   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
552                   ata_mode2str(atadev->mode),
553                   ata_satarev2str(ATA_GETREV(device_get_parent(dev), atadev->unit)));
554     if (bootverbose) {
555         device_printf(dev, "%ju sectors [%juC/%dH/%dS] "
556                       "%d sectors/interrupt %d depth queue\n", adp->total_secs,
557                       adp->total_secs / (adp->heads * adp->sectors),
558                       adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
559                       adp->num_tags + 1);
560     }
561 }
562
563 static int
564 ad_version(u_int16_t version)
565 {
566     int bit;
567
568     if (version == 0xffff)
569         return 0;
570     for (bit = 15; bit >= 0; bit--)
571         if (version & (1<<bit))
572             return bit;
573     return 0;
574 }
575
576 static device_method_t ad_methods[] = {
577     /* device interface */
578     DEVMETHOD(device_probe,     ad_probe),
579     DEVMETHOD(device_attach,    ad_attach),
580     DEVMETHOD(device_detach,    ad_detach),
581     DEVMETHOD(device_shutdown,  ad_shutdown),
582
583     /* ATA methods */
584     DEVMETHOD(ata_reinit,       ad_reinit),
585
586     { 0, 0 }
587 };
588
589 static driver_t ad_driver = {
590     "ad",
591     ad_methods,
592     0,
593 };
594
595 devclass_t ad_devclass;
596
597 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
598 MODULE_VERSION(ad, 1);
599 MODULE_DEPEND(ad, ata, 1, 1, 1);