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