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