]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-disk.c
This commit was generated by cvs2svn to compensate for changes in r169695,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-disk.c
1 /*-
2  * Copyright (c) 1998 - 2007 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/sysctl.h>
43 #include <sys/sema.h>
44 #include <sys/taskqueue.h>
45 #include <vm/uma.h>
46 #include <machine/md_var.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <geom/geom_disk.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/ata/ata-pci.h>
52 #include <dev/ata/ata-disk.h>
53 #include <dev/ata/ata-raid.h>
54 #include <ata_if.h>
55
56 /* prototypes */
57 static void ad_init(device_t);
58 static void ad_done(struct ata_request *);
59 static void ad_describe(device_t dev);
60 static int ad_version(u_int16_t);
61 static disk_strategy_t ad_strategy;
62 static disk_ioctl_t ad_ioctl;
63 static dumper_t ad_dump;
64
65 /*
66  * Most platforms map firmware geom to actual, but some don't.  If
67  * not overridden, default to nothing.
68  */
69 #ifndef ad_firmware_geom_adjust
70 #define ad_firmware_geom_adjust(dev, disk)
71 #endif
72
73 /* local vars */
74 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
75
76 static int
77 ad_probe(device_t dev)
78 {
79     struct ata_device *atadev = device_get_softc(dev);
80
81     if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
82         (atadev->param.config == ATA_CFA_MAGIC1) ||
83         (atadev->param.config == ATA_CFA_MAGIC2))
84         return 0;
85     else
86         return ENXIO;
87 }
88
89 static int
90 ad_attach(device_t dev)
91 {
92     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
93     struct ata_device *atadev = device_get_softc(dev);
94     struct ad_softc *adp;
95     u_int32_t lbasize;
96     u_int64_t lbasize48;
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     if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
109         atadev->param.current_heads && atadev->param.current_sectors) {
110         adp->heads = atadev->param.current_heads;
111         adp->sectors = atadev->param.current_sectors;
112         adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
113                           ((u_int32_t)atadev->param.current_size_2 << 16);
114     }
115     else {
116         adp->heads = atadev->param.heads;
117         adp->sectors = atadev->param.sectors;
118         adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;  
119     }
120     lbasize = (u_int32_t)atadev->param.lba_size_1 |
121               ((u_int32_t)atadev->param.lba_size_2 << 16);
122
123     /* does this device need oldstyle CHS addressing */
124     if (!ad_version(atadev->param.version_major) || !lbasize)
125         atadev->flags |= ATA_D_USE_CHS;
126
127     /* use the 28bit LBA size if valid or bigger than the CHS mapping */
128     if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
129         adp->total_secs = lbasize;
130
131     /* use the 48bit LBA size if valid */
132     lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
133                 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
134                 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
135                 ((u_int64_t)atadev->param.lba_size48_4 << 48);
136     if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
137         lbasize48 > ATA_MAX_28BIT_LBA)
138         adp->total_secs = lbasize48;
139
140     /* init device parameters */
141     ad_init(dev);
142
143     /* announce we are here */
144     ad_describe(dev);
145
146     /* create the disk device */
147     adp->disk = disk_alloc();
148     adp->disk->d_strategy = ad_strategy;
149     adp->disk->d_ioctl = ad_ioctl;
150     adp->disk->d_dump = ad_dump;
151     adp->disk->d_name = "ad";
152     adp->disk->d_drv1 = dev;
153     if (ch->dma)
154         adp->disk->d_maxsize = ch->dma->max_iosize;
155     else
156         adp->disk->d_maxsize = DFLTPHYS;
157     adp->disk->d_sectorsize = DEV_BSIZE;
158     adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
159     adp->disk->d_fwsectors = adp->sectors;
160     adp->disk->d_fwheads = adp->heads;
161     adp->disk->d_unit = device_get_unit(dev);
162     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
163         adp->disk->d_flags = DISKFLAG_CANFLUSHCACHE;
164     snprintf(adp->disk->d_ident, sizeof(adp->disk->d_ident), "ad:%s",
165         atadev->param.serial);
166     disk_create(adp->disk, DISK_VERSION);
167     device_add_child(dev, "subdisk", device_get_unit(dev));
168     ad_firmware_geom_adjust(dev, adp->disk);
169     bus_generic_attach(dev);
170     return 0;
171 }
172
173 static int
174 ad_detach(device_t dev)
175 {
176     struct ad_softc *adp = device_get_ivars(dev);
177     device_t *children;
178     int nchildren, i;
179
180     /* check that we have a valid disk to detach */
181     if (!device_get_ivars(dev))
182         return ENXIO;
183     
184     /* detach & delete all children */
185     if (!device_get_children(dev, &children, &nchildren)) {
186         for (i = 0; i < nchildren; i++)
187             if (children[i])
188                 device_delete_child(dev, children[i]);
189         free(children, M_TEMP);
190     }
191
192     /* detroy disk from the system so we dont get any further requests */
193     disk_destroy(adp->disk);
194
195     /* fail requests on the queue and any thats "in flight" for this device */
196     ata_fail_requests(dev);
197
198     /* dont leave anything behind */
199     device_set_ivars(dev, NULL);
200     free(adp, M_AD);
201     return 0;
202 }
203
204 static void
205 ad_shutdown(device_t dev)
206 {
207     struct ata_device *atadev = device_get_softc(dev);
208
209     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
210         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
211 }
212
213 static int
214 ad_reinit(device_t dev)
215 {
216     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
217     struct ata_device *atadev = device_get_softc(dev);
218
219     /* if detach pending, return error */
220     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
221         ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
222         return 1;
223     }
224     ad_init(dev);
225     return 0;
226 }
227
228 static void 
229 ad_strategy(struct bio *bp)
230 {
231     device_t dev =  bp->bio_disk->d_drv1;
232     struct ata_device *atadev = device_get_softc(dev);
233     struct ata_request *request;
234
235     if (!(request = ata_alloc_request())) {
236         device_printf(dev, "FAILURE - out of memory in start\n");
237         biofinish(bp, NULL, ENOMEM);
238         return;
239     }
240
241     /* setup request */
242     request->dev = dev;
243     request->bio = bp;
244     request->callback = ad_done;
245     request->timeout = 5;
246     request->retries = 2;
247     request->data = bp->bio_data;
248     request->bytecount = bp->bio_bcount;
249     request->u.ata.lba = bp->bio_pblkno;
250     request->u.ata.count = request->bytecount / DEV_BSIZE;
251     request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
252
253     switch (bp->bio_cmd) {
254     case BIO_READ:
255         request->flags = ATA_R_READ;
256         if (atadev->mode >= ATA_DMA) {
257             request->u.ata.command = ATA_READ_DMA;
258             request->flags |= ATA_R_DMA;
259         }
260         else if (request->transfersize > DEV_BSIZE)
261             request->u.ata.command = ATA_READ_MUL;
262         else
263             request->u.ata.command = ATA_READ;
264         break;
265     case BIO_WRITE:
266         request->flags = ATA_R_WRITE;
267         if (atadev->mode >= ATA_DMA) {
268             request->u.ata.command = ATA_WRITE_DMA;
269             request->flags |= ATA_R_DMA;
270         }
271         else if (request->transfersize > DEV_BSIZE)
272             request->u.ata.command = ATA_WRITE_MUL;
273         else
274             request->u.ata.command = ATA_WRITE;
275         break;
276     case BIO_FLUSH:
277         request->u.ata.lba = 0;
278         request->u.ata.count = 0;
279         request->u.ata.feature = 0;
280         request->bytecount = 0;
281         request->transfersize = 0;
282         request->flags = ATA_R_CONTROL;
283         request->u.ata.command = ATA_FLUSHCACHE;
284         break;
285     default:
286         device_printf(dev, "FAILURE - unknown BIO operation\n");
287         ata_free_request(request);
288         biofinish(bp, NULL, EIO);
289         return;
290     }
291     request->flags |= ATA_R_ORDERED;
292     ata_queue_request(request);
293 }
294
295 static void
296 ad_done(struct ata_request *request)
297 {
298     struct bio *bp = request->bio;
299
300     /* finish up transfer */
301     if ((bp->bio_error = request->result))
302         bp->bio_flags |= BIO_ERROR;
303     bp->bio_resid = bp->bio_bcount - request->donecount;
304     biodone(bp);
305     ata_free_request(request);
306 }
307
308 static int
309 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
310 {
311     return ata_device_ioctl(disk->d_drv1, cmd, data);
312 }
313
314 static int
315 ad_dump(void *arg, void *virtual, vm_offset_t physical,
316         off_t offset, size_t length)
317 {
318     struct disk *dp = arg;
319     struct bio bp;
320
321     /* length zero is special and really means flush buffers to media */
322     if (!length) {
323         struct ata_device *atadev = device_get_softc(dp->d_drv1);
324         int error = 0;
325
326         if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
327             error = ata_controlcmd(dp->d_drv1, ATA_FLUSHCACHE, 0, 0, 0);
328         return error;
329     }
330
331     bzero(&bp, sizeof(struct bio));
332     bp.bio_disk = dp;
333     bp.bio_pblkno = offset / DEV_BSIZE;
334     bp.bio_bcount = length;
335     bp.bio_data = virtual;
336     bp.bio_cmd = BIO_WRITE;
337     ad_strategy(&bp);
338     return bp.bio_error;
339 }
340
341 static void
342 ad_init(device_t dev)
343 {
344     struct ata_device *atadev = device_get_softc(dev);
345
346     ATA_SETMODE(device_get_parent(dev), dev);
347
348     /* enable readahead caching */
349     if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
350         ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
351
352     /* enable write caching if supported and configured */
353     if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
354         if (ata_wc)
355             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
356         else
357             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
358     }
359
360     /* use multiple sectors/interrupt if device supports it */
361     if (ad_version(atadev->param.version_major)) {
362         int secsperint = max(1, min(atadev->param.sectors_intr, 16));
363
364         if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
365             atadev->max_iosize = secsperint * DEV_BSIZE;
366     }
367     else
368         atadev->max_iosize = DEV_BSIZE;
369 }
370
371 void
372 ad_describe(device_t dev)
373 {
374     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
375     struct ata_device *atadev = device_get_softc(dev);
376     struct ad_softc *adp = device_get_ivars(dev);
377     u_int8_t *marker, vendor[64], product[64];
378
379     /* try to seperate the ATA model string into vendor and model parts */
380     if ((marker = index(atadev->param.model, ' ')) ||
381         (marker = index(atadev->param.model, '-'))) {
382         int len = (marker - atadev->param.model);
383
384         strncpy(vendor, atadev->param.model, len);
385         vendor[len++] = 0;
386         strcat(vendor, " ");
387         strncpy(product, atadev->param.model + len, 40 - len);
388         vendor[40 - len] = 0;
389     }
390     else {
391         if (!strncmp(atadev->param.model, "ST", 2))
392             strcpy(vendor, "Seagate ");
393         else
394             strcpy(vendor, "");
395         strncpy(product, atadev->param.model, 40);
396     }
397
398     device_printf(dev, "%juMB <%s%s %.8s> at ata%d-%s %s%s\n",
399                   adp->total_secs / (1048576 / DEV_BSIZE),
400                   vendor, product, atadev->param.revision,
401                   device_get_unit(ch->dev),
402                   (atadev->unit == ATA_MASTER) ? "master" : "slave",
403                   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
404                   ata_mode2str(atadev->mode));
405     if (bootverbose) {
406         device_printf(dev, "%ju sectors [%juC/%dH/%dS] "
407                       "%d sectors/interrupt %d depth queue\n", adp->total_secs,
408                       adp->total_secs / (adp->heads * adp->sectors),
409                       adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
410                       adp->num_tags + 1);
411     }
412 }
413
414 static int
415 ad_version(u_int16_t version)
416 {
417     int bit;
418
419     if (version == 0xffff)
420         return 0;
421     for (bit = 15; bit >= 0; bit--)
422         if (version & (1<<bit))
423             return bit;
424     return 0;
425 }
426
427 static device_method_t ad_methods[] = {
428     /* device interface */
429     DEVMETHOD(device_probe,     ad_probe),
430     DEVMETHOD(device_attach,    ad_attach),
431     DEVMETHOD(device_detach,    ad_detach),
432     DEVMETHOD(device_shutdown,  ad_shutdown),
433
434     /* ATA methods */
435     DEVMETHOD(ata_reinit,       ad_reinit),
436
437     { 0, 0 }
438 };
439
440 static driver_t ad_driver = {
441     "ad",
442     ad_methods,
443     0,
444 };
445
446 devclass_t ad_devclass;
447
448 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
449 MODULE_VERSION(ad, 1);
450 MODULE_DEPEND(ad, ata, 1, 1, 1);