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