2 * Copyright (c) 1998 - 2007 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
46 #include <machine/md_var.h>
47 #include <machine/bus.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>
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;
66 * Most platforms map firmware geom to actual, but some don't. If
67 * not overridden, default to nothing.
69 #ifndef ad_firmware_geom_adjust
70 #define ad_firmware_geom_adjust(dev, disk)
74 static MALLOC_DEFINE(M_AD, "ad_driver", "ATA disk driver");
77 ad_probe(device_t dev)
79 struct ata_device *atadev = device_get_softc(dev);
81 if (!(atadev->param.config & ATA_PROTO_ATAPI) ||
82 (atadev->param.config == ATA_CFA_MAGIC1) ||
83 (atadev->param.config == ATA_CFA_MAGIC2) ||
84 (atadev->param.config == ATA_CFA_MAGIC3))
91 ad_attach(device_t dev)
93 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
94 struct ata_device *atadev = device_get_softc(dev);
99 /* check that we have a virgin disk to attach */
100 if (device_get_ivars(dev))
103 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
104 device_printf(dev, "out of memory\n");
107 device_set_ivars(dev, adp);
109 if ((atadev->param.atavalid & ATA_FLAG_54_58) &&
110 atadev->param.current_heads && atadev->param.current_sectors) {
111 adp->heads = atadev->param.current_heads;
112 adp->sectors = atadev->param.current_sectors;
113 adp->total_secs = (u_int32_t)atadev->param.current_size_1 |
114 ((u_int32_t)atadev->param.current_size_2 << 16);
117 adp->heads = atadev->param.heads;
118 adp->sectors = atadev->param.sectors;
119 adp->total_secs = atadev->param.cylinders * adp->heads * adp->sectors;
121 lbasize = (u_int32_t)atadev->param.lba_size_1 |
122 ((u_int32_t)atadev->param.lba_size_2 << 16);
124 /* does this device need oldstyle CHS addressing */
125 if (!ad_version(atadev->param.version_major) || !lbasize)
126 atadev->flags |= ATA_D_USE_CHS;
128 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
129 if (atadev->param.cylinders == 16383 || adp->total_secs < lbasize)
130 adp->total_secs = lbasize;
132 /* use the 48bit LBA size if valid */
133 lbasize48 = ((u_int64_t)atadev->param.lba_size48_1) |
134 ((u_int64_t)atadev->param.lba_size48_2 << 16) |
135 ((u_int64_t)atadev->param.lba_size48_3 << 32) |
136 ((u_int64_t)atadev->param.lba_size48_4 << 48);
137 if ((atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) &&
138 lbasize48 > ATA_MAX_28BIT_LBA)
139 adp->total_secs = lbasize48;
141 /* init device parameters */
144 /* announce we are here */
147 /* create the disk device */
148 adp->disk = disk_alloc();
149 adp->disk->d_strategy = ad_strategy;
150 adp->disk->d_ioctl = ad_ioctl;
151 adp->disk->d_dump = ad_dump;
152 adp->disk->d_name = "ad";
153 adp->disk->d_drv1 = dev;
155 adp->disk->d_maxsize = ch->dma->max_iosize;
157 adp->disk->d_maxsize = DFLTPHYS;
158 adp->disk->d_sectorsize = DEV_BSIZE;
159 adp->disk->d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
160 adp->disk->d_fwsectors = adp->sectors;
161 adp->disk->d_fwheads = adp->heads;
162 adp->disk->d_unit = device_get_unit(dev);
163 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
164 adp->disk->d_flags = DISKFLAG_CANFLUSHCACHE;
165 if ((atadev->param.support.command2 & ATA_SUPPORT_CFA) ||
166 atadev->param.config == ATA_PROTO_CFA)
167 adp->disk->d_flags = DISKFLAG_CANDELETE;
168 snprintf(adp->disk->d_ident, sizeof(adp->disk->d_ident), "ad:%s",
169 atadev->param.serial);
170 disk_create(adp->disk, DISK_VERSION);
171 device_add_child(dev, "subdisk", device_get_unit(dev));
172 ad_firmware_geom_adjust(dev, adp->disk);
173 bus_generic_attach(dev);
178 ad_detach(device_t dev)
180 struct ad_softc *adp = device_get_ivars(dev);
184 /* check that we have a valid disk to detach */
185 if (!device_get_ivars(dev))
188 /* detach & delete all children */
189 if (!device_get_children(dev, &children, &nchildren)) {
190 for (i = 0; i < nchildren; i++)
192 device_delete_child(dev, children[i]);
193 free(children, M_TEMP);
196 /* detroy disk from the system so we dont get any further requests */
197 disk_destroy(adp->disk);
199 /* fail requests on the queue and any thats "in flight" for this device */
200 ata_fail_requests(dev);
202 /* dont leave anything behind */
203 device_set_ivars(dev, NULL);
209 ad_shutdown(device_t dev)
211 struct ata_device *atadev = device_get_softc(dev);
213 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
214 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
218 ad_reinit(device_t dev)
220 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
221 struct ata_device *atadev = device_get_softc(dev);
223 /* if detach pending, return error */
224 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATA_MASTER)) ||
225 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATA_SLAVE))) {
233 ad_strategy(struct bio *bp)
235 device_t dev = bp->bio_disk->d_drv1;
236 struct ata_device *atadev = device_get_softc(dev);
237 struct ata_request *request;
239 if (!(request = ata_alloc_request())) {
240 device_printf(dev, "FAILURE - out of memory in start\n");
241 biofinish(bp, NULL, ENOMEM);
248 request->callback = ad_done;
249 request->timeout = 5;
250 request->retries = 2;
251 request->data = bp->bio_data;
252 request->bytecount = bp->bio_bcount;
253 request->u.ata.lba = bp->bio_pblkno;
254 request->u.ata.count = request->bytecount / DEV_BSIZE;
255 request->transfersize = min(bp->bio_bcount, atadev->max_iosize);
257 switch (bp->bio_cmd) {
259 request->flags = ATA_R_READ;
260 if (atadev->mode >= ATA_DMA) {
261 request->u.ata.command = ATA_READ_DMA;
262 request->flags |= ATA_R_DMA;
264 else if (request->transfersize > DEV_BSIZE)
265 request->u.ata.command = ATA_READ_MUL;
267 request->u.ata.command = ATA_READ;
270 request->flags = ATA_R_WRITE;
271 if (atadev->mode >= ATA_DMA) {
272 request->u.ata.command = ATA_WRITE_DMA;
273 request->flags |= ATA_R_DMA;
275 else if (request->transfersize > DEV_BSIZE)
276 request->u.ata.command = ATA_WRITE_MUL;
278 request->u.ata.command = ATA_WRITE;
281 request->flags = ATA_R_CONTROL;
282 request->u.ata.command = ATA_CFA_ERASE;
283 request->transfersize = 0;
284 request->donecount = bp->bio_bcount;
287 request->u.ata.lba = 0;
288 request->u.ata.count = 0;
289 request->u.ata.feature = 0;
290 request->bytecount = 0;
291 request->transfersize = 0;
292 request->flags = ATA_R_CONTROL;
293 request->u.ata.command = ATA_FLUSHCACHE;
296 device_printf(dev, "FAILURE - unknown BIO operation\n");
297 ata_free_request(request);
298 biofinish(bp, NULL, EIO);
301 request->flags |= ATA_R_ORDERED;
302 ata_queue_request(request);
306 ad_done(struct ata_request *request)
308 struct bio *bp = request->bio;
310 /* finish up transfer */
311 if ((bp->bio_error = request->result))
312 bp->bio_flags |= BIO_ERROR;
313 bp->bio_resid = bp->bio_bcount - request->donecount;
315 ata_free_request(request);
319 ad_ioctl(struct disk *disk, u_long cmd, void *data, int flag, struct thread *td)
321 return ata_device_ioctl(disk->d_drv1, cmd, data);
325 ad_dump(void *arg, void *virtual, vm_offset_t physical,
326 off_t offset, size_t length)
328 struct disk *dp = arg;
331 /* length zero is special and really means flush buffers to media */
333 struct ata_device *atadev = device_get_softc(dp->d_drv1);
336 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
337 error = ata_controlcmd(dp->d_drv1, ATA_FLUSHCACHE, 0, 0, 0);
341 bzero(&bp, sizeof(struct bio));
343 bp.bio_pblkno = offset / DEV_BSIZE;
344 bp.bio_bcount = length;
345 bp.bio_data = virtual;
346 bp.bio_cmd = BIO_WRITE;
352 ad_init(device_t dev)
354 struct ata_device *atadev = device_get_softc(dev);
356 ATA_SETMODE(device_get_parent(dev), dev);
358 /* enable readahead caching */
359 if (atadev->param.support.command1 & ATA_SUPPORT_LOOKAHEAD)
360 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_RCACHE, 0, 0);
362 /* enable write caching if supported and configured */
363 if (atadev->param.support.command1 & ATA_SUPPORT_WRITECACHE) {
365 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_ENAB_WCACHE, 0, 0);
367 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_DIS_WCACHE, 0, 0);
370 /* use multiple sectors/interrupt if device supports it */
371 if (ad_version(atadev->param.version_major)) {
372 int secsperint = max(1, min(atadev->param.sectors_intr, 16));
374 if (!ata_controlcmd(dev, ATA_SET_MULTI, 0, 0, secsperint))
375 atadev->max_iosize = secsperint * DEV_BSIZE;
378 atadev->max_iosize = DEV_BSIZE;
382 ad_describe(device_t dev)
384 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
385 struct ata_device *atadev = device_get_softc(dev);
386 struct ad_softc *adp = device_get_ivars(dev);
387 u_int8_t *marker, vendor[64], product[64];
389 /* try to seperate the ATA model string into vendor and model parts */
390 if ((marker = index(atadev->param.model, ' ')) ||
391 (marker = index(atadev->param.model, '-'))) {
392 int len = (marker - atadev->param.model);
394 strncpy(vendor, atadev->param.model, len);
397 strncpy(product, atadev->param.model + len, 40 - len);
398 vendor[40 - len] = 0;
401 if (!strncmp(atadev->param.model, "ST", 2))
402 strcpy(vendor, "Seagate ");
403 else if (!strncmp(atadev->param.model, "HDS", 3))
404 strcpy(vendor, "Hitachi ");
407 strncpy(product, atadev->param.model, 40);
410 device_printf(dev, "%juMB <%s%s %.8s> at ata%d-%s %s%s\n",
411 adp->total_secs / (1048576 / DEV_BSIZE),
412 vendor, product, atadev->param.revision,
413 device_get_unit(ch->dev),
414 (atadev->unit == ATA_MASTER) ? "master" : "slave",
415 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
416 ata_mode2str(atadev->mode));
418 device_printf(dev, "%ju sectors [%juC/%dH/%dS] "
419 "%d sectors/interrupt %d depth queue\n", adp->total_secs,
420 adp->total_secs / (adp->heads * adp->sectors),
421 adp->heads, adp->sectors, atadev->max_iosize / DEV_BSIZE,
427 ad_version(u_int16_t version)
431 if (version == 0xffff)
433 for (bit = 15; bit >= 0; bit--)
434 if (version & (1<<bit))
439 static device_method_t ad_methods[] = {
440 /* device interface */
441 DEVMETHOD(device_probe, ad_probe),
442 DEVMETHOD(device_attach, ad_attach),
443 DEVMETHOD(device_detach, ad_detach),
444 DEVMETHOD(device_shutdown, ad_shutdown),
447 DEVMETHOD(ata_reinit, ad_reinit),
452 static driver_t ad_driver = {
458 devclass_t ad_devclass;
460 DRIVER_MODULE(ad, ata, ad_driver, ad_devclass, NULL, NULL);
461 MODULE_VERSION(ad, 1);
462 MODULE_DEPEND(ad, ata, 1, 1, 1);