kernel: Use DEVMETHOD_END in the drivers.
[dragonfly.git] / sys / dev / disk / nata / 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  * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.199 2006/09/14 19:12:29 sos Exp $
27  */
28
29 #include "opt_ata.h"
30
31 #include <sys/param.h>
32 #include <sys/bio.h>
33 #include <sys/buf.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/devicestat.h>
37 #include <sys/disk.h>
38 #include <sys/libkern.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/nata.h>
42 #include <sys/systm.h>
43
44 #include <vm/pmap.h>
45
46 #include <machine/md_var.h>
47
48 #include "ata-all.h"
49 #include "ata-disk.h"
50 #include "ata_if.h"
51
52 /* device structure */
53 static  d_open_t        ad_open;
54 static  d_close_t       ad_close;
55 static  d_ioctl_t       ad_ioctl;
56 static  d_strategy_t    ad_strategy;
57 static  d_dump_t        ad_dump;
58 static struct dev_ops ad_ops = {
59         { "ad", 0, D_DISK },
60         .d_open =       ad_open,
61         .d_close =      ad_close,
62         .d_read =       physread,
63         .d_write =      physwrite,
64         .d_ioctl =      ad_ioctl,
65         .d_strategy =   ad_strategy,
66         .d_dump =       ad_dump,
67 };
68
69 /* prototypes */
70 static void ad_init(device_t);
71 static void ad_done(struct ata_request *);
72 static void ad_describe(device_t dev);
73 static int ad_version(u_int16_t);
74
75 /* local vars */
76 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
77
78 static int
79 ad_probe(device_t dev)
80 {
81     struct ata_device *atadev = device_get_softc(dev);
82
83     if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
84         (atadev->param.config == ATA_CFA_MAGIC1) ||
85         (atadev->param.config == ATA_CFA_MAGIC2) ||
86         (atadev->param.config == ATA_CFA_MAGIC3))
87         return 0;
88     else
89         return ENXIO;
90 }
91
92 static int
93 ad_attach(device_t dev)
94 {
95     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
96     struct ata_device *atadev = device_get_softc(dev);
97     struct disk_info info;
98     struct ad_softc *adp;
99     cdev_t cdev;
100     u_int32_t lbasize;
101     u_int64_t lbasize48;
102
103     /* check that we have a virgin disk to attach */
104     if (device_get_ivars(dev))
105         return EEXIST;
106
107     adp = kmalloc(sizeof(struct ad_softc), M_AD, M_INTWAIT | M_ZERO);
108     device_set_ivars(dev, adp);
109
110     if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
111         atadev->param.current_heads && atadev->param.current_sectors) {
112         adp->heads = atadev->param.current_heads;
113         adp->sectors = atadev->param.current_sectors;
114         adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
115                           ((u_int32_t)atadev->param.current_size_2 << 16);
116     }
117     else {
118         adp->heads = atadev->param.heads;
119         adp->sectors = atadev->param.sectors;
120         adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;  
121     }
122     lbasize = (u_int32_t)atadev->param.lba_size_1 |
123               ((u_int32_t)atadev->param.lba_size_2 << 16);
124
125     /* does this device need oldstyle CHS addressing */
126     if (!ad_version(atadev->param.version_major) || !lbasize)
127         atadev->flags |= ATA_D_USE_CHS;
128
129     /* use the 28bit LBA size if valid or bigger than the CHS mapping */
130     if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
131         adp->total_secs = lbasize;
132
133     /* use the 48bit LBA size if valid */
134     lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
135                 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
136                 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
137                 ((u_int64_t)atadev->param.lba_size48_4 << 48);
138     if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
139         lbasize48 > ATA_MAX_28BIT_LBA)
140         adp->total_secs = lbasize48;
141
142     /* init device parameters */
143     ad_init(dev);
144
145     /* create the disk device */
146     /* XXX TGEN Maybe use DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
147        DEVSTAT_PRIORITY_MAX. */
148     devstat_add_entry(&adp->stats, "ad", device_get_unit(dev), DEV_BSIZE,
149                       DEVSTAT_NO_ORDERED_TAGS,
150                       DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
151                       DEVSTAT_PRIORITY_DISK);
152     cdev = disk_create(device_get_unit(dev), &adp->disk, &ad_ops);
153     cdev->si_drv1 = dev;
154     if (ch->dma)
155         cdev->si_iosize_max = ch->dma->max_iosize;
156     else
157         cdev->si_iosize_max = min(MAXPHYS,64*1024);
158     adp->cdev = cdev;
159
160     bzero(&info, sizeof(info));
161     info.d_media_blksize = DEV_BSIZE;           /* mandatory */
162     info.d_media_blocks = adp->total_secs;
163
164     info.d_secpertrack = adp->sectors;          /* optional */
165     info.d_nheads = adp->heads;
166     info.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
167     info.d_secpercyl = adp->sectors * adp->heads;
168     info.d_serialno = atadev->param.serial;
169
170     device_add_child(dev, "subdisk", device_get_unit(dev));
171     bus_generic_attach(dev);
172
173     /* announce we are here */
174     ad_describe(dev);
175
176     disk_setdiskinfo(&adp->disk, &info);
177
178     return 0;
179 }
180
181 static int
182 ad_detach(device_t dev)
183 {
184     struct ad_softc *adp = device_get_ivars(dev);
185     device_t *children;
186     int nchildren, i;
187
188     /* check that we have a valid disk to detach */
189     if (!adp)
190         return ENXIO;
191
192 #if 0 /* XXX TGEN Probably useless, we fail the queue below. */
193     /* check that the disk is closed */
194     if (adp->ad_flags & AD_DISK_OPEN)
195         return EBUSY;
196 #endif /* 0 */
197     
198     /* detach & delete all children */
199     if (!device_get_children(dev, &children, &nchildren)) {
200         for (i = 0; i < nchildren; i++)
201             if (children[i])
202                 device_delete_child(dev, children[i]);
203         kfree(children, M_TEMP);
204     }
205
206     /* detroy disk from the system so we dont get any further requests */
207     disk_invalidate(&adp->disk);
208     disk_destroy(&adp->disk);
209
210     /* fail requests on the queue and any thats "in flight" for this device */
211     ata_fail_requests(dev);
212
213     /* dont leave anything behind */
214     /* disk_destroy() already took care of the dev_ops */
215     devstat_remove_entry(&adp->stats);
216     device_set_ivars(dev, NULL);
217     kfree(adp, M_AD);
218     return 0;
219 }
220
221 static void
222 ad_shutdown(device_t dev)
223 {
224     struct ata_device *atadev = device_get_softc(dev);
225
226     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
227         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
228 }
229
230 static int
231 ad_reinit(device_t dev)
232 {
233     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
234     struct ata_device *atadev = device_get_softc(dev);
235
236     /* if detach pending, return error */
237     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
238         ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
239         return 1;
240     }
241     ad_init(dev);
242     return 0;
243 }
244
245 static int
246 ad_open(struct dev_open_args *ap)
247 {
248     device_t dev = ap->a_head.a_dev->si_drv1;
249     struct ad_softc *adp = device_get_ivars(dev);
250
251     if (!adp || adp->cdev == NULL)
252         return ENXIO;
253     if(!device_is_attached(dev))
254         return EBUSY;
255
256 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
257     adp->ad_flags &= AD_DISK_OPEN;
258 #endif /* 0 */
259     return 0;
260 }
261
262 static int
263 ad_close(struct dev_close_args *ap)
264 {
265 #if 0 /* XXX TGEN Probably useless, queue will be failed on detach. */
266     device_t dev = ap->a_head.a_dev->si_drv1;
267     struct ad_softc *adp = device_get_ivars(dev);
268     adp->ad_flags |= AD_DISK_OPEN;
269 #endif /* 0 */
270     return 0;
271 }
272
273 static int
274 ad_ioctl(struct dev_ioctl_args *ap)
275 {
276     return ata_device_ioctl(ap->a_head.a_dev->si_drv1, ap->a_cmd, ap->a_data);
277 }
278
279 static int
280 ad_strategy(struct dev_strategy_args *ap)
281 {
282     device_t dev =  ap->a_head.a_dev->si_drv1;
283     struct bio *bp = ap->a_bio;
284     struct buf *bbp = bp->bio_buf;
285     struct ata_device *atadev = device_get_softc(dev);
286     struct ata_request *request;
287     struct ad_softc *adp = device_get_ivars(dev);
288
289     if (!(request = ata_alloc_request())) {
290         device_printf(dev, "FAILURE - out of memory in strategy\n");
291         bbp->b_flags |= B_ERROR;
292         bbp->b_error = ENOMEM;
293         biodone(bp);
294         return(0);
295     }
296
297     /* setup request */
298     request->dev = dev;
299     request->bio = bp;
300     request->callback = ad_done;
301     request->timeout = ATA_DEFAULT_TIMEOUT;
302     request->retries = 2;
303     request->data = bbp->b_data;
304     request->bytecount = bbp->b_bcount;
305     /* lba is block granularity, convert byte granularity bio_offset */
306     request->u.ata.lba = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT);
307     request->u.ata.count = request->bytecount / DEV_BSIZE;
308     request->transfersize = min(bbp->b_bcount, atadev->max_iosize);
309
310     switch (bbp->b_cmd) {
311     case BUF_CMD_READ:
312         request->flags = ATA_R_READ;
313         if (atadev->mode >= ATA_DMA) {
314             request->u.ata.command = ATA_READ_DMA;
315             request->flags |= ATA_R_DMA;
316         }
317         else if (request->transfersize > DEV_BSIZE)
318             request->u.ata.command = ATA_READ_MUL;
319         else
320             request->u.ata.command = ATA_READ;
321         break;
322     case BUF_CMD_WRITE:
323         request->flags = ATA_R_WRITE;
324         if (atadev->mode >= ATA_DMA) {
325             request->u.ata.command = ATA_WRITE_DMA;
326             request->flags |= ATA_R_DMA;
327         }
328         else if (request->transfersize > DEV_BSIZE)
329             request->u.ata.command = ATA_WRITE_MUL;
330         else
331             request->u.ata.command = ATA_WRITE;
332         break;
333     case BUF_CMD_FLUSH:
334         request->u.ata.lba = 0;
335         request->u.ata.count = 0;
336         request->u.ata.feature = 0;
337         request->bytecount = 0;
338         request->transfersize = 0;
339         request->flags = ATA_R_CONTROL;
340         request->u.ata.command = ATA_FLUSHCACHE;
341         /* ATA FLUSHCACHE requests may take up to 30 sec to timeout */
342         request->timeout = 30;
343         break;
344     default:
345         device_printf(dev, "FAILURE - unknown BUF operation\n");
346         ata_free_request(request);
347         bbp->b_flags |= B_ERROR;
348         bbp->b_error = EIO;
349         biodone(bp);
350         return(0);
351     }
352     request->flags |= ATA_R_ORDERED;
353     devstat_start_transaction(&adp->stats);
354     ata_queue_request(request);
355     return(0);
356 }
357
358 static void
359 ad_done(struct ata_request *request)
360 {
361     struct ad_softc *adp = device_get_ivars(request->dev);
362     struct bio *bp = request->bio;
363     struct buf *bbp = bp->bio_buf;
364
365     /* finish up transfer */
366     if ((bbp->b_error = request->result))
367         bbp->b_flags |= B_ERROR;
368     bbp->b_resid = bbp->b_bcount - request->donecount;
369     devstat_end_transaction_buf(&adp->stats, bbp);
370     biodone(bp);
371     ata_free_request(request);
372 }
373
374 static int
375 ad_dump(struct dev_dump_args *ap)
376 {
377         device_t dev = ap->a_head.a_dev->si_drv1;
378         struct ata_device *atadev = device_get_softc(dev);
379         struct ata_request request;
380
381         ata_drop_requests(dev);
382         /*
383          * 0 length means flush buffers and return
384          */
385         if (ap->a_length == 0) {
386                 /* flush buffers to media */
387                 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
388                         return ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
389                 else
390                         return ENXIO;
391         }
392
393         bzero(&request, sizeof(struct ata_request));
394         request.dev = dev;
395
396         request.data = ap->a_virtual;
397         request.bytecount = ap->a_length;
398         request.transfersize = min(request.bytecount, atadev->max_iosize);
399         request.flags = ATA_R_WRITE;
400
401         if (atadev->mode >= ATA_DMA) {
402                 request.u.ata.command = ATA_WRITE_DMA;
403                 request.flags |= ATA_DMA;
404         } else if (request.transfersize > DEV_BSIZE)
405                 request.u.ata.command = ATA_WRITE_MUL;
406         else
407                 request.u.ata.command = ATA_WRITE;
408         request.u.ata.lba = ap->a_offset / DEV_BSIZE;
409         request.u.ata.count = request.bytecount / DEV_BSIZE;
410
411         request.timeout = ATA_DEFAULT_TIMEOUT;
412         request.retries = 2;
413
414         ata_queue_request(&request);
415         return request.result;
416 }
417
418 static void
419 ad_init(device_t dev)
420 {
421     struct ata_device *atadev = device_get_softc(dev);
422
423     ATA_SETMODE(device_get_parent(dev), dev);
424
425     /* enable readahead caching */
426     if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
427         ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
428
429     /* enable write caching if supported and configured */
430     if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
431         if (ata_wc)
432             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
433         else
434             ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
435     }
436
437     /* use multiple sectors/interrupt if device supports it */
438     if (ad_version(atadev->param.version_major)) {
439         int secsperint = max(1, min(atadev->param.sectors_intr & 0xff, 16));
440
441         if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
442             atadev->max_iosize = secsperint * DEV_BSIZE;
443     }
444     else
445         atadev->max_iosize = DEV_BSIZE;
446 }
447
448 void
449 ad_describe(device_t dev)
450 {
451     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
452     struct ata_device *atadev = device_get_softc(dev);
453     struct ad_softc *adp = device_get_ivars(dev);
454     u_int8_t *marker, vendor[64], product[64];
455
456     /* try to seperate the ATA model string into vendor and model parts */
457     if ((marker = index(atadev->param.model, ' ')) ||
458         (marker = index(atadev->param.model, '-'))) {
459         int len = (marker - atadev->param.model);
460
461         strncpy(vendor, atadev->param.model, len);
462         vendor[len++] = 0;
463         strcat(vendor, " ");
464         strncpy(product, atadev->param.model + len, 40 - len);
465         vendor[40 - len] = 0;
466     }
467     else {
468         if (!strncmp(atadev->param.model, "ST", 2))
469             strcpy(vendor, "Seagate ");
470         else if (!strncmp(atadev->param.model, "HDS", 3))
471             strcpy(vendor, "Hitachi ");
472         else
473             strcpy(vendor, "");
474         strncpy(product, atadev->param.model, 40);
475     }
476
477     device_printf(dev, "%lluMB <%s%s %.8s> at ata%d-%s %s%s\n",
478                   (unsigned long long)(adp->total_secs / (1048576 / DEV_BSIZE)),
479                   vendor, product, atadev->param.revision,
480                   device_get_unit(ch->dev),
481                   (atadev->unit == ATA_MASTER) ? "master" : "slave",
482                   (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
483                   ata_mode2str(atadev->mode));
484     if (bootverbose) {
485         device_printf(dev, "%llu sectors [%lluC/%dH/%dS] "
486                       "%d sectors/interrupt %d depth queue\n",
487                       (unsigned long long)adp->total_secs,(unsigned long long)(
488                       adp->total_secs / (adp->heads * adp->sectors)),
489                       adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
490                       adp->num_tags + 1);
491     }
492 }
493
494 static int
495 ad_version(u_int16_t version)
496 {
497     int bit;
498
499     if (version == 0xffff)
500         return 0;
501     for (bit = 15; bit >= 0; bit--)
502         if (version & (1<<bit))
503             return bit;
504     return 0;
505 }
506
507 static device_method_t ad_methods[] = {
508     /* device interface */
509     DEVMETHOD(device_probe,     ad_probe),
510     DEVMETHOD(device_attach,    ad_attach),
511     DEVMETHOD(device_detach,    ad_detach),
512     DEVMETHOD(device_shutdown,  ad_shutdown),
513
514     /* ATA methods */
515     DEVMETHOD(ata_reinit,       ad_reinit),
516
517     DEVMETHOD_END
518 };
519
520 static driver_t ad_driver = {
521     "ad",
522     ad_methods,
523     0,
524 };
525
526 devclass_t ad_devclass;
527
528 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
529 MODULE_VERSION(ad, 1);
530 MODULE_DEPEND(ad, ata, 1, 1, 1);