]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-disk.c
This commit was generated by cvs2svn to compensate for changes in r156369,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-disk.c
1 /*-
2  * Copyright (c) 1998 - 2006 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 /* local vars */
66 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
67
68 static int
69 ad_probe(device_t dev)
70 {
71     struct ata_device *atadev = device_get_softc(dev);
72
73     if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
74         (atadev->param.config == ATA_CFA_MAGIC))
75         return 0;
76     else
77         return ENXIO;
78 }
79
80 static int
81 ad_attach(device_t dev)
82 {
83     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
84     struct ata_device *atadev = device_get_softc(dev);
85     struct ad_softc *adp;
86     u_int32_t lbasize;
87     u_int64_t lbasize48;
88
89     /* check that we have a virgin disk to attach */
90     if (device_get_ivars(dev))
91         return EEXIST;
92
93     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
94         device_printf(dev, "out of memory\n");
95         return ENOMEM;
96     }
97     device_set_ivars(dev, adp);
98
99     if (atadev->param.atavalid & ATA_FLAG_54_58) {
100         adp->heads = atadev->param.current_heads;
101         adp->sectors = atadev->param.current_sectors;
102         adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
103                           ((u_int32_t)atadev->param.current_size_2 << 16);
104     }
105     else {
106         adp->heads = atadev->param.heads;
107         adp->sectors = atadev->param.sectors;
108         adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;  
109     }
110     lbasize = (u_int32_t)atadev->param.lba_size_1 |
111               ((u_int32_t)atadev->param.lba_size_2 << 16);
112
113     /* does this device need oldstyle CHS addressing */
114     if (!ad_version(atadev->param.version_major) || !lbasize)
115         atadev->flags |= ATA_D_USE_CHS;
116
117     /* use the 28bit LBA size if valid or bigger than the CHS mapping */
118     if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
119         adp->total_secs = lbasize;
120
121     /* use the 48bit LBA size if valid */
122     lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
123                 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
124                 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
125                 ((u_int64_t)atadev->param.lba_size48_4 << 48);
126     if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
127         lbasize48 > ATA_MAX_28BIT_LBA)
128         adp->total_secs = lbasize48;
129
130     /* init device parameters */
131     ad_init(dev);
132
133     /* announce we are here */
134     ad_describe(dev);
135
136     /* create the disk device */
137     adp->disk = disk_alloc();
138     adp->disk->d_strategy = ad_strategy;
139     adp->disk->d_ioctl = ad_ioctl;
140     adp->disk->d_dump = ad_dump;
141     adp->disk->d_name = "ad";
142     adp->disk->d_drv1 = dev;
143     if (ch->dma)
144         adp->disk->d_maxsize = ch->dma->max_iosize;
145     else
146         adp->disk->d_maxsize = DFLTPHYS;
147     adp->disk->d_sectorsize = DEV_BSIZE;
148     adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
149     adp->disk->d_fwsectors = adp->sectors;
150     adp->disk->d_fwheads = adp->heads;
151     adp->disk->d_unit = device_get_unit(dev);
152     disk_create(adp->disk, DISK_VERSION);
153     device_add_child(dev, "subdisk", device_get_unit(dev));
154     bus_generic_attach(dev);
155     return 0;
156 }
157
158 static int
159 ad_detach(device_t dev)
160 {
161     struct ad_softc *adp = device_get_ivars(dev);
162     device_t *children;
163     int nchildren, i;
164
165     /* check that we have a valid disk to detach */
166     if (!device_get_ivars(dev))
167         return ENXIO;
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 void
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 }
197
198 static int
199 ad_reinit(device_t dev)
200 {
201     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
202     struct ata_device *atadev = device_get_softc(dev);
203
204     /* if detach pending, return error */
205     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
206         ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
207         return 1;
208     }
209     ad_init(dev);
210     return 0;
211 }
212
213 static void 
214 ad_strategy(struct bio *bp)
215 {
216     device_t dev =  bp->bio_disk->d_drv1;
217     struct ata_device *atadev = device_get_softc(dev);
218     struct ata_request *request;
219
220     if (!(request = ata_alloc_request())) {
221         device_printf(dev, "FAILURE - out of memory in start\n");
222         biofinish(bp, NULL, ENOMEM);
223         return;
224     }
225
226     /* setup request */
227     request->dev = dev;
228     request->bio = bp;
229     request->callback = ad_done;
230     request->timeout = 5;
231     request->retries = 2;
232     request->data = bp->bio_data;
233     request->bytecount = bp->bio_bcount;
234     request->u.ata.lba = bp->bio_pblkno;
235     request->u.ata.count = request->bytecount / DEV_BSIZE;
236     request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
237
238     switch (bp->bio_cmd) {
239     case BIO_READ:
240         request->flags = ATA_R_READ;
241         if (atadev->mode >= ATA_DMA) {
242             request->u.ata.command = ATA_READ_DMA;
243             request->flags |= ATA_R_DMA;
244         }
245         else if (request->transfersize > DEV_BSIZE)
246             request->u.ata.command = ATA_READ_MUL;
247         else
248             request->u.ata.command = ATA_READ;
249         break;
250     case BIO_WRITE:
251         request->flags = ATA_R_WRITE;
252         if (atadev->mode >= ATA_DMA) {
253             request->u.ata.command = ATA_WRITE_DMA;
254             request->flags |= ATA_R_DMA;
255         }
256         else if (request->transfersize > DEV_BSIZE)
257             request->u.ata.command = ATA_WRITE_MUL;
258         else
259             request->u.ata.command = ATA_WRITE;
260         break;
261     default:
262         device_printf(dev, "FAILURE - unknown BIO operation\n");
263         ata_free_request(request);
264         biofinish(bp, NULL, EIO);
265         return;
266     }
267     request->flags |= ATA_R_ORDERED;
268     ata_queue_request(request);
269 }
270
271 static void
272 ad_done(struct ata_request *request)
273 {
274     struct bio *bp = request->bio;
275
276     /* finish up transfer */
277     if ((bp->bio_error = request->result))
278         bp->bio_flags |= BIO_ERROR;
279     bp->bio_resid = bp->bio_bcount - request->donecount;
280     biodone(bp);
281     ata_free_request(request);
282 }
283
284 static int
285 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
286 {
287     return ata_device_ioctl(disk->d_drv1, cmd, data);
288 }
289
290 static int
291 ad_dump(void *arg, void *virtual, vm_offset_t physical,
292         off_t offset, size_t length)
293 {
294     struct disk *dp = arg;
295     struct bio bp;
296
297     /* length zero is special and really means flush buffers to media */
298     if (!length)
299         return ata_controlcmd(dp->d_drv1, ATA_FLUSHCACHE, 0, 0, 0);
300
301     bzero(&bp, sizeof(struct bio));
302     bp.bio_disk = dp;
303     bp.bio_pblkno = offset / DEV_BSIZE;
304     bp.bio_bcount = length;
305     bp.bio_data = virtual;
306     bp.bio_cmd = BIO_WRITE;
307     ad_strategy(&bp);
308     return bp.bio_error;
309 }
310
311 static void
312 ad_init(device_t dev)
313 {
314     struct ata_device *atadev = device_get_softc(dev);
315
316     ATA_SETMODE(device_get_parent(dev), dev);
317
318     /* enable readahead caching */
319     if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
320         ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
321
322     /* enable write caching if supported and configured */
323     if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
324         if (ata_wc)
325             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
326         else
327             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
328     }
329
330     /* use multiple sectors/interrupt if device supports it */
331     if (ad_version(atadev->param.version_major)) {
332         int secsperint = max(1, min(atadev->param.sectors_intr, 16));
333
334         if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
335             atadev->max_iosize = secsperint * DEV_BSIZE;
336     }
337     else
338         atadev->max_iosize = DEV_BSIZE;
339 }
340
341 void
342 ad_describe(device_t dev)
343 {
344     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
345     struct ata_device *atadev = device_get_softc(dev);
346     struct ad_softc *adp = device_get_ivars(dev);
347     u_int8_t *marker, vendor[64], product[64];
348
349     /* try to seperate the ATA model string into vendor and model parts */
350     if ((marker = index(atadev->param.model, ' ')) ||
351         (marker = index(atadev->param.model, '-'))) {
352         int len = (marker - atadev->param.model);
353
354         strncpy(vendor, atadev->param.model, len);
355         vendor[len++] = 0;
356         strcat(vendor, " ");
357         strncpy(product, atadev->param.model + len, 40 - len);
358         vendor[40 - len] = 0;
359     }
360     else {
361         if (!strncmp(atadev->param.model, "ST", 2))
362             strcpy(vendor, "Seagate ");
363         else
364             strcpy(vendor, "");
365         strncpy(product, atadev->param.model, 40);
366     }
367
368     device_printf(dev, "%juMB <%s%s %.8s> at ata%d-%s %s%s\n",
369                   (uintmax_t)(adp->total_secs / (1048576 / DEV_BSIZE)),
370                   vendor, product, atadev->param.revision,
371                   device_get_unit(ch->dev),
372                   (atadev->unit == ATA_MASTER) ? "master" : "slave",
373                   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
374                   ata_mode2str(atadev->mode));
375     if (bootverbose) {
376         device_printf(dev, "%ju sectors [%juC/%dH/%dS] "
377                       "%d sectors/interrupt %d depth queue\n",
378                       (uintmax_t)adp->total_secs,
379                       (uintmax_t)(adp->total_secs /
380                                            (adp->heads * adp->sectors)),
381                       adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
382                       adp->num_tags + 1);
383     }
384 }
385
386 static int
387 ad_version(u_int16_t version)
388 {
389     int bit;
390
391     if (version == 0xffff)
392         return 0;
393     for (bit = 15; bit >= 0; bit--)
394         if (version & (1<<bit))
395             return bit;
396     return 0;
397 }
398
399 static device_method_t ad_methods[] = {
400     /* device interface */
401     DEVMETHOD(device_probe,     ad_probe),
402     DEVMETHOD(device_attach,    ad_attach),
403     DEVMETHOD(device_detach,    ad_detach),
404     DEVMETHOD(device_shutdown,  ad_shutdown),
405
406     /* ATA methods */
407     DEVMETHOD(ata_reinit,       ad_reinit),
408
409     { 0, 0 }
410 };
411
412 static driver_t ad_driver = {
413     "ad",
414     ad_methods,
415     0,
416 };
417
418 devclass_t ad_devclass;
419
420 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
421 MODULE_VERSION(ad, 1);
422 MODULE_DEPEND(ad, ata, 1, 1, 1);