]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-disk.c
Get rid of the advertising clause in the copyright.
[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     device_t dev = dp->d_drv1;
296     struct ata_device *atadev = device_get_softc(dev);
297     struct ad_softc *adp = device_get_ivars(dev);
298     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
299     struct ata_request request;
300
301     if (!adp)
302         return ENXIO;
303
304     bzero(&request, sizeof(struct ata_request));
305     request.dev = dev;
306
307     if (length) {
308         request.data = virtual;
309         request.bytecount = length;
310         request.transfersize = min(length, atadev->max_iosize);
311         request.flags = ATA_R_WRITE;
312         if (atadev->max_iosize > DEV_BSIZE)
313             request.u.ata.command = ATA_WRITE_MUL;
314         else
315             request.u.ata.command = ATA_WRITE;
316         request.u.ata.lba = offset / DEV_BSIZE;
317         request.u.ata.count = request.bytecount / DEV_BSIZE;
318     }
319     else {
320         request.u.ata.command = ATA_FLUSHCACHE;
321         request.flags = ATA_R_CONTROL;
322     }
323     if (ch->hw.begin_transaction(&request) == ATA_OP_CONTINUES) {
324         do {
325             DELAY(20);
326         } while (ch->hw.end_transaction(&request) == ATA_OP_CONTINUES);
327     }
328     if (request.status & ATA_S_ERROR)
329         return EIO;
330     return 0;
331 }
332
333 static void
334 ad_init(device_t dev)
335 {
336     struct ata_device *atadev = device_get_softc(dev);
337
338     ATA_SETMODE(device_get_parent(dev), dev);
339
340     /* enable readahead caching */
341     if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
342         ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
343
344     /* enable write caching if supported and configured */
345     if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
346         if (ata_wc)
347             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
348         else
349             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
350     }
351
352     /* use multiple sectors/interrupt if device supports it */
353     if (ad_version(atadev->param.version_major)) {
354         int secsperint = max(1, min(atadev->param.sectors_intr, 16));
355
356         if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
357             atadev->max_iosize = secsperint * DEV_BSIZE;
358     }
359     else
360         atadev->max_iosize = DEV_BSIZE;
361 }
362
363 void
364 ad_describe(device_t dev)
365 {
366     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
367     struct ata_device *atadev = device_get_softc(dev);
368     struct ad_softc *adp = device_get_ivars(dev);
369     u_int8_t *marker, vendor[64], product[64];
370
371     /* try to seperate the ATA model string into vendor and model parts */
372     if ((marker = index(atadev->param.model, ' ')) ||
373         (marker = index(atadev->param.model, '-'))) {
374         int len = (marker - atadev->param.model);
375
376         strncpy(vendor, atadev->param.model, len);
377         vendor[len++] = 0;
378         strcat(vendor, " ");
379         strncpy(product, atadev->param.model + len, 40 - len);
380         vendor[40 - len] = 0;
381     }
382     else {
383         if (!strncmp(atadev->param.model, "ST", 2))
384             strcpy(vendor, "Seagate ");
385         else
386             strcpy(vendor, "");
387         strncpy(product, atadev->param.model, 40);
388     }
389
390     device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n",
391                   (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)),
392                   vendor, product, atadev->param.revision,
393                   device_get_unit(ch->dev),
394                   (atadev->unit == ATA_MASTER) ? "master" : "slave",
395                   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
396                   ata_mode2str(atadev->mode));
397     if (bootverbose) {
398         device_printf(dev, "%llu sectors [%lldC/%dH/%dS] "
399                       "%d sectors/interrupt %d depth queue\n",
400                       (unsigned long long)adp->total_secs,
401                       (unsigned long long)(adp->total_secs /
402                                            (adp->heads * adp->sectors)),
403                       adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
404                       adp->num_tags + 1);
405     }
406 }
407
408 static int
409 ad_version(u_int16_t version)
410 {
411     int bit;
412
413     if (version == 0xffff)
414         return 0;
415     for (bit = 15; bit >= 0; bit--)
416         if (version & (1<<bit))
417             return bit;
418     return 0;
419 }
420
421 static device_method_t ad_methods[] = {
422     /* device interface */
423     DEVMETHOD(device_probe,     ad_probe),
424     DEVMETHOD(device_attach,    ad_attach),
425     DEVMETHOD(device_detach,    ad_detach),
426     DEVMETHOD(device_shutdown,  ad_shutdown),
427
428     /* ATA methods */
429     DEVMETHOD(ata_reinit,       ad_reinit),
430
431     { 0, 0 }
432 };
433
434 static driver_t ad_driver = {
435     "ad",
436     ad_methods,
437     0,
438 };
439
440 devclass_t ad_devclass;
441
442 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
443 MODULE_VERSION(ad, 1);
444 MODULE_DEPEND(ad, ata, 1, 1, 1);