2 * Copyright (c) 1998,1999,2000,2001,2002 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/ata/ata-disk.c,v 1.60.2.24 2003/01/30 07:19:59 sos Exp $
29 * $DragonFly: src/sys/dev/disk/ata/ata-disk.c,v 1.33 2006/12/22 23:26:15 swildner Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
42 #include <sys/devicestat.h>
44 #include <sys/sysctl.h>
45 #include <sys/syslog.h>
49 #include <sys/thread2.h>
54 #include <machine/md_var.h>
55 #include <machine/clock.h>
61 /* device structures */
62 static d_open_t adopen;
63 static d_close_t adclose;
64 static d_strategy_t adstrategy;
65 static d_dump_t addump;
67 static struct dev_ops ad_ops = {
68 { "ad", 116, D_DISK },
73 .d_strategy = adstrategy,
78 static void ad_requeue(struct ata_channel *, struct ad_request *);
79 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
80 static int ad_tagsupported(struct ad_softc *);
81 static void ad_timeout(struct ad_request *);
82 static void ad_free(struct ad_request *);
83 static int ad_version(u_int16_t);
86 #define AD_MAX_RETRIES 3
89 static u_int32_t adp_lun_map = 0;
90 static int ata_dma = 1;
91 static int ata_wc = 1;
92 static int ata_tags = 0;
93 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
94 TUNABLE_INT("hw.ata.wc", &ata_wc);
95 TUNABLE_INT("hw.ata.tags", &ata_tags);
96 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
100 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
101 "ATA disk DMA mode control");
102 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
103 "ATA disk write caching");
104 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
105 "ATA disk tagged queuing support");
108 ad_attach(struct ata_device *atadev, int alreadylocked)
110 struct ad_softc *adp;
113 adp = kmalloc(sizeof(struct ad_softc), M_AD, M_WAITOK | M_ZERO);
115 KKASSERT(atadev->channel->req_mpipe.max_count != 0);
117 adp->device = atadev;
119 adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
121 adp->lun = ata_get_lun(&adp_lun_map);
123 ata_set_name(atadev, "ad", adp->lun);
124 adp->heads = atadev->param->heads;
125 adp->sectors = atadev->param->sectors;
126 adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;
127 bioq_init(&adp->bio_queue);
129 /* does this device need oldstyle CHS addressing */
130 if (!ad_version(atadev->param->version_major) ||
131 !(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
132 adp->flags |= AD_F_CHS_USED;
134 /* use the 28bit LBA size if valid */
135 if (atadev->param->cylinders == 16383 &&
136 adp->total_secs < atadev->param->lba_size)
137 adp->total_secs = atadev->param->lba_size;
139 /* use the 48bit LBA size if valid */
140 if (atadev->param->support.address48 &&
141 atadev->param->lba_size48 > 268435455)
142 adp->total_secs = atadev->param->lba_size48;
145 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
146 /* use multiple sectors/interrupt if device supports it */
147 adp->transfersize = DEV_BSIZE;
148 if (ad_version(atadev->param->version_major)) {
149 int secsperint = max(1, min(atadev->param->sectors_intr, 16));
151 if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
152 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
153 adp->transfersize *= secsperint;
156 /* enable read caching if not default on device */
157 if (ata_command(atadev, ATA_C_SETFEATURES,
158 0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
159 ata_prtdev(atadev, "enabling readahead cache failed\n");
161 /* enable write caching if allowed and not default on device */
162 if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
163 if (ata_command(atadev, ATA_C_SETFEATURES,
164 0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
165 ata_prtdev(atadev, "enabling write cache failed\n");
168 if (ata_command(atadev, ATA_C_SETFEATURES,
169 0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
170 ata_prtdev(atadev, "disabling write cache failed\n");
173 /* use DMA if allowed and if drive/controller supports it */
175 ata_dmainit(atadev, ata_pmode(atadev->param),
176 ata_wmode(atadev->param), ata_umode(atadev->param));
178 ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
180 /* use tagged queueing if allowed and supported */
181 if (ata_tags && ad_tagsupported(adp)) {
182 adp->num_tags = atadev->param->queuelen;
183 adp->flags |= AD_F_TAG_ENABLED;
184 adp->device->channel->flags |= ATA_QUEUED;
185 if (ata_command(atadev, ATA_C_SETFEATURES,
186 0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
187 ata_prtdev(atadev, "disabling release interrupt failed\n");
188 if (ata_command(atadev, ATA_C_SETFEATURES,
189 0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
190 ata_prtdev(atadev, "disabling service interrupt failed\n");
193 ATA_UNLOCK_CH(atadev->channel);
195 devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
196 DEVSTAT_NO_ORDERED_TAGS,
197 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
198 DEVSTAT_PRIORITY_DISK);
200 dev = disk_create(adp->lun, &adp->disk, 0, &ad_ops);
202 dev->si_iosize_max = 256 * DEV_BSIZE;
205 /* construct the disklabel */
206 bzero(&adp->disk.d_label, sizeof(struct disklabel));
207 adp->disk.d_label.d_secsize = DEV_BSIZE;
208 adp->disk.d_label.d_nsectors = adp->sectors;
209 adp->disk.d_label.d_ntracks = adp->heads;
210 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
211 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
212 adp->disk.d_label.d_secperunit = adp->total_secs;
214 atadev->driver = adp;
217 /* if this disk belongs to an ATA RAID dont print the probe */
218 if (ata_raiddisk_attach(adp))
219 adp->flags |= AD_F_RAID_SUBDISK;
221 if (atadev->driver) {
223 ata_enclosure_print(atadev);
229 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
231 struct ad_softc *adp = atadev->driver;
232 struct ad_request *request;
236 atadev->flags |= ATA_D_DETACHING;
237 ata_prtdev(atadev, "removed from configuration\n");
238 ad_invalidatequeue(adp, NULL);
239 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
240 if (request->softc != adp)
242 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
243 request->bio->bio_buf->b_error = ENXIO;
244 request->bio->bio_buf->b_flags |= B_ERROR;
245 biodone(request->bio);
249 while ((bio = bioq_first(&adp->bio_queue))) {
250 bioq_remove(&adp->bio_queue, bio);
253 bp->b_flags |= B_ERROR;
256 disk_invalidate(&adp->disk);
257 devstat_remove_entry(&adp->stats);
258 disk_destroy(&adp->disk);
260 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
261 ata_prtdev(atadev, "flushing cache on detach failed\n");
263 if (adp->flags & AD_F_RAID_SUBDISK)
264 ata_raiddisk_detach(adp);
265 ata_free_name(atadev);
266 ata_free_lun(&adp_lun_map, adp->lun);
267 atadev->driver = NULL;
273 adopen(struct dev_open_args *ap)
275 struct ad_softc *adp = ap->a_head.a_dev->si_drv1;
277 if (adp->flags & AD_F_RAID_SUBDISK)
283 adclose(struct dev_close_args *ap)
285 struct ad_softc *adp = ap->a_head.a_dev->si_drv1;
287 crit_enter(); /* interlock non-atomic channel lock */
288 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
289 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
290 ata_prtdev(adp->device, "flushing cache on close failed\n");
291 ATA_UNLOCK_CH(adp->device->channel);
297 * note: always use the passed device rather then bp->b_dev, as the bp
298 * may have been translated through several layers.
301 adstrategy(struct dev_strategy_args *ap)
303 cdev_t dev = ap->a_head.a_dev;
304 struct bio *bio = ap->a_bio;
305 struct buf *bp = bio->bio_buf;
306 struct ad_softc *adp = dev->si_drv1;
308 if (adp->device->flags & ATA_D_DETACHING) {
310 bp->b_flags |= B_ERROR;
314 bio->bio_driver_info = dev;
316 bioqdisksort(&adp->bio_queue, bio);
318 ata_start(adp->device->channel);
323 addump(struct dev_dump_args *ap)
325 cdev_t dev = ap->a_head.a_dev;
326 struct ad_softc *adp = dev->si_drv1;
327 struct ad_request request;
330 int dumppages = MAXDUMPPGS;
336 /* force PIO mode for dumps */
337 adp->device->mode = ATA_PIO;
338 ata_reinit(adp->device->channel);
340 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
342 while (ap->a_count > 0) {
346 if ((ap->a_count / blkcnt) < dumppages)
347 dumppages = ap->a_count / blkcnt;
349 for (i = 0; i < dumppages; ++i) {
350 vm_paddr_t a = addr + (i * PAGE_SIZE);
351 if (is_physical_memory(a))
352 va = pmap_kenter_temporary(trunc_page(a), i);
354 va = pmap_kenter_temporary(trunc_page(0), i);
357 bzero(&request, sizeof(struct ad_request));
359 request.blockaddr = ap->a_blkno;
360 request.bytecount = PAGE_SIZE * dumppages;
362 callout_init(&request.callout);
364 while (request.bytecount > 0) {
365 ad_transfer(&request);
366 if (request.flags & ADR_F_ERROR)
368 request.donecount += request.currentsize;
369 request.bytecount -= request.currentsize;
373 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
376 ap->a_blkno += blkcnt * dumppages;
377 ap->a_count -= blkcnt * dumppages;
378 addr += PAGE_SIZE * dumppages;
381 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
382 ata_prtdev(adp->device, "timeout waiting for final ready\n");
387 * Critical section is held when this function is called
391 ad_start(struct ata_device *atadev)
393 struct ad_softc *adp = atadev->driver;
394 struct bio *bio = bioq_first(&adp->bio_queue);
396 struct ad_request *request;
403 /* if tagged queueing enabled get next free tag */
404 if (adp->flags & AD_F_TAG_ENABLED) {
405 while (tag <= adp->num_tags && adp->tags[tag])
407 if (tag > adp->num_tags )
412 * Allocate a request. The allocation can only fail if the pipeline
413 * is full, in which case the request will be picked up later when
414 * ad_start() is called after another request completes.
416 request = mpipe_alloc_nowait(&atadev->channel->req_mpipe);
417 if (request == NULL) {
418 ata_prtdev(atadev, "pipeline full allocating request in ad_start\n");
422 KKASSERT((bio->bio_offset & DEV_BMASK) == 0);
425 request->softc = adp;
427 request->blockaddr = (u_int32_t)(bio->bio_offset >> DEV_BSHIFT);
428 request->bytecount = bp->b_bcount;
429 request->data = bp->b_data;
431 callout_init(&request->callout);
432 if (bp->b_cmd == BUF_CMD_READ)
433 request->flags |= ADR_F_READ;
434 if (adp->device->mode >= ATA_DMA) {
435 if (ata_dmaalloc(atadev, M_NOWAIT) != 0) {
436 mpipe_free(&atadev->channel->req_mpipe, request);
437 ata_prtdev(atadev, "pipeline full allocated dmabuf in ad_start\n");
438 /* do not revert to PIO, wait for ad_start after I/O completion */
443 /* insert in tag array */
444 adp->tags[tag] = request;
446 /* remove from drive queue */
447 bioq_remove(&adp->bio_queue, bio);
449 /* link onto controller queue */
450 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
454 ad_requeue(struct ata_channel *chan, struct ad_request *req)
456 if (req->donecount) {
458 "WARNING: resetting donecount %u for retry\n",
460 req->bytecount += req->donecount;
463 TAILQ_INSERT_HEAD(&chan->ata_queue, req, chain);
467 ad_transfer(struct ad_request *request)
469 struct ad_softc *adp;
471 u_int32_t count, max_count;
473 int flags = ATA_IMMEDIATE;
475 /* get request params */
476 adp = request->softc;
478 /* calculate transfer details */
479 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
481 if (request->donecount == 0) {
483 /* start timeout for this transfer */
485 callout_stop(&request->callout);
487 callout_reset(&request->callout, 10 * hz,
488 (void *)ad_timeout, request);
491 /* setup transfer parameters */
492 count = howmany(request->bytecount, DEV_BSIZE);
493 max_count = adp->device->param->support.address48 ? 65536 : 256;
494 if (count > max_count) {
495 ata_prtdev(adp->device,
496 "count %d size transfers not supported\n", count);
500 if (adp->flags & AD_F_CHS_USED) {
501 int sector = (lba % adp->sectors) + 1;
502 int cylinder = lba / (adp->sectors * adp->heads);
503 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
505 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
506 adp->device->flags |= ATA_D_USE_CHS;
509 /* setup first transfer length */
510 request->currentsize = min(request->bytecount, adp->transfersize);
512 devstat_start_transaction(&adp->stats);
514 /* does this drive & transfer work with DMA ? */
515 request->flags &= ~ADR_F_DMA_USED;
516 if (adp->device->mode >= ATA_DMA &&
517 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
518 request->flags |= ADR_F_DMA_USED;
519 request->currentsize = request->bytecount;
521 /* do we have tags enabled ? */
522 if (adp->flags & AD_F_TAG_ENABLED) {
523 cmd = (request->flags & ADR_F_READ) ?
524 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
526 if (ata_command(adp->device, cmd, lba,
527 request->tag << 3, count, flags)) {
528 ata_prtdev(adp->device, "error executing command");
529 goto transfer_failed;
531 if (ata_wait(adp->device, ATA_S_READY)) {
532 ata_prtdev(adp->device, "timeout waiting for READY\n");
533 goto transfer_failed;
537 /* if ATA bus RELEASE check for SERVICE */
538 if (adp->flags & AD_F_TAG_ENABLED &&
539 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
541 return ad_service(adp, 1);
544 cmd = (request->flags & ADR_F_READ) ?
545 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
547 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
548 ata_prtdev(adp->device, "error executing command");
549 goto transfer_failed;
553 * wait for data transfer phase
555 * well this should be here acording to specs, but older
556 * promise controllers doesn't like it, they lockup!
558 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
559 ata_prtdev(adp->device, "timeout waiting for data phase\n");
560 goto transfer_failed;
565 /* start transfer, return and wait for interrupt */
566 ata_dmastart(adp->device, request->data, request->bytecount,
567 request->flags & ADR_F_READ);
568 return ATA_OP_CONTINUES;
571 /* does this drive support multi sector transfers ? */
572 if (request->currentsize > DEV_BSIZE)
573 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
575 /* just plain old single sector transfer */
577 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
579 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
580 ata_prtdev(adp->device, "error executing command");
581 goto transfer_failed;
585 /* calculate this transfer length */
586 request->currentsize = min(request->bytecount, adp->transfersize);
588 /* if this is a PIO read operation, return and wait for interrupt */
589 if (request->flags & ADR_F_READ)
590 return ATA_OP_CONTINUES;
592 /* ready to write PIO data ? */
593 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
594 ata_prtdev(adp->device, "timeout waiting for DRQ");
595 goto transfer_failed;
598 /* output the data */
599 if (adp->device->channel->flags & ATA_USE_16BIT)
600 ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
601 (void *)((uintptr_t)request->data + request->donecount),
602 request->currentsize / sizeof(int16_t));
604 ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
605 (void *)((uintptr_t)request->data + request->donecount),
606 request->currentsize / sizeof(int32_t));
607 return ATA_OP_CONTINUES;
610 callout_stop(&request->callout);
611 ad_invalidatequeue(adp, request);
612 kprintf(" - resetting\n");
614 /* if retries still permit, reinject this request */
615 if (request->retries++ < AD_MAX_RETRIES)
616 ad_requeue(adp->device->channel, request);
618 /* retries all used up, return error */
619 request->bio->bio_buf->b_error = EIO;
620 request->bio->bio_buf->b_flags |= B_ERROR;
621 request->bio->bio_buf->b_resid = request->bytecount;
622 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
623 biodone(request->bio);
626 ata_reinit(adp->device->channel);
627 return ATA_OP_CONTINUES;
631 ad_interrupt(struct ad_request *request)
633 struct ad_softc *adp = request->softc;
637 /* finish DMA transfer */
638 if (request->flags & ADR_F_DMA_USED)
639 dma_stat = ata_dmadone(adp->device);
641 dev = request->bio->bio_driver_info;
642 /* do we have a corrected soft error ? */
643 if (adp->device->channel->status & ATA_S_CORR)
644 diskerr(request->bio, dev,
645 "soft error (ECC corrected)", LOG_PRINTF,
646 request->donecount, &adp->disk.d_label);
648 /* did any real errors happen ? */
649 if ((adp->device->channel->status & ATA_S_ERROR) ||
650 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
651 adp->device->channel->error =
652 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
653 diskerr(request->bio, dev,
654 (adp->device->channel->error & ATA_E_ICRC) ?
655 "UDMA ICRC error" : "hard error", LOG_PRINTF,
656 request->donecount, &adp->disk.d_label);
658 /* if this is a UDMA CRC error, reinject request */
659 if (request->flags & ADR_F_DMA_USED &&
660 adp->device->channel->error & ATA_E_ICRC) {
661 callout_stop(&request->callout);
662 ad_invalidatequeue(adp, request);
664 if (request->retries++ < AD_MAX_RETRIES)
665 kprintf(" retrying\n");
667 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
668 kprintf(" falling back to PIO mode\n");
670 ad_requeue(adp->device->channel, request);
671 return ATA_OP_FINISHED;
674 /* if using DMA, try once again in PIO mode */
675 if (request->flags & ADR_F_DMA_USED) {
676 callout_stop(&request->callout);
677 ad_invalidatequeue(adp, request);
678 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
679 request->flags |= ADR_F_FORCE_PIO;
680 kprintf(" trying PIO mode\n");
681 ad_requeue(adp->device->channel, request);
682 return ATA_OP_FINISHED;
685 request->flags |= ADR_F_ERROR;
686 kprintf(" status=%02x error=%02x\n",
687 adp->device->channel->status, adp->device->channel->error);
690 /* if we arrived here with forced PIO mode, DMA doesn't work right */
691 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
692 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
694 /* if this was a PIO read operation, get the data */
695 if (!(request->flags & ADR_F_DMA_USED) &&
696 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
698 /* ready to receive data? */
699 if ((adp->device->channel->status & ATA_S_READY) == 0)
700 ata_prtdev(adp->device, "read interrupt arrived early");
702 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
703 ata_prtdev(adp->device, "read error detected (too) late");
704 request->flags |= ADR_F_ERROR;
707 /* data ready, read in */
708 if (adp->device->channel->flags & ATA_USE_16BIT)
709 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
710 (void*)((uintptr_t)request->data + request->donecount),
711 request->currentsize / sizeof(int16_t));
713 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
714 (void*)((uintptr_t)request->data + request->donecount),
715 request->currentsize / sizeof(int32_t));
719 /* finish up transfer */
720 if (request->flags & ADR_F_ERROR) {
721 request->bio->bio_buf->b_error = EIO;
722 request->bio->bio_buf->b_flags |= B_ERROR;
725 request->bytecount -= request->currentsize;
726 request->donecount += request->currentsize;
727 if (request->bytecount > 0) {
728 ad_transfer(request);
729 return ATA_OP_CONTINUES;
733 /* disarm timeout for this transfer */
734 callout_stop(&request->callout);
736 request->bio->bio_buf->b_resid = request->bytecount;
738 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
739 biodone(request->bio);
743 /* check for SERVICE (tagged operations only) */
744 return ad_service(adp, 1);
748 ad_service(struct ad_softc *adp, int change)
750 /* do we have to check the other device on this channel ? */
751 if (adp->device->channel->flags & ATA_QUEUED && change) {
752 int device = adp->device->unit;
754 if (adp->device->unit == ATA_MASTER) {
755 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
756 (adp->device->channel->device[SLAVE].driver) &&
757 ((struct ad_softc *) (adp->device->channel->
758 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
762 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
763 (adp->device->channel->device[MASTER].driver) &&
764 ((struct ad_softc *) (adp->device->channel->
765 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
768 if (device != adp->device->unit &&
770 (adp->device->channel->
771 device[ATA_DEV(device)].driver))->outstanding > 0) {
772 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
773 adp = adp->device->channel->device[ATA_DEV(device)].driver;
777 adp->device->channel->status =
778 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
780 /* do we have a SERVICE request from the drive ? */
781 if (adp->flags & AD_F_TAG_ENABLED &&
782 adp->outstanding > 0 &&
783 adp->device->channel->status & ATA_S_SERVICE) {
784 struct ad_request *request;
787 /* check for error */
788 if (adp->device->channel->status & ATA_S_ERROR) {
789 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
790 adp->device->channel->status,
791 adp->device->channel->error);
792 ad_invalidatequeue(adp, NULL);
793 return ATA_OP_FINISHED;
796 /* issue SERVICE cmd */
797 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
798 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
799 ad_invalidatequeue(adp, NULL);
800 return ATA_OP_FINISHED;
803 /* setup the transfer environment when ready */
804 if (ata_wait(adp->device, ATA_S_READY)) {
805 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
806 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
807 adp->device->channel->status,
808 adp->device->channel->error);
809 ad_invalidatequeue(adp, NULL);
810 return ATA_OP_FINISHED;
812 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
813 if (!(request = adp->tags[tag])) {
814 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
815 ad_invalidatequeue(adp, NULL);
816 return ATA_OP_FINISHED;
818 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
819 adp->device->channel->running = request;
822 /* start DMA transfer when ready */
823 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
824 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
825 adp->device->channel->status,
826 adp->device->channel->error);
827 ad_invalidatequeue(adp, NULL);
828 return ATA_OP_FINISHED;
830 ata_dmastart(adp->device, request->data, request->bytecount,
831 request->flags & ADR_F_READ);
832 return ATA_OP_CONTINUES;
834 return ATA_OP_FINISHED;
838 ad_free(struct ad_request *request)
841 ata_dmafree(request->softc->device);
842 request->softc->tags[request->tag] = NULL;
843 mpipe_free(&request->softc->device->channel->req_mpipe, request);
848 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
850 /* if tags used invalidate all other tagged transfers */
851 if (adp->flags & AD_F_TAG_ENABLED) {
852 struct ad_request *tmpreq;
855 ata_prtdev(adp->device, "invalidating queued requests\n");
856 for (tag = 0; tag <= adp->num_tags; tag++) {
857 tmpreq = adp->tags[tag];
858 adp->tags[tag] = NULL;
859 if (tmpreq == request || tmpreq == NULL)
861 callout_stop(&request->callout);
862 ad_requeue(adp->device->channel, tmpreq);
864 if (ata_command(adp->device, ATA_C_NOP,
865 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
866 ata_prtdev(adp->device, "flush queue failed\n");
867 adp->outstanding = 0;
872 ad_tagsupported(struct ad_softc *adp)
874 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
877 switch (adp->device->channel->chiptype) {
878 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
885 /* check that drive does DMA, has tags enabled, and is one we know works */
886 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
887 adp->device->param->enabled.queued) {
888 while (good[i] != NULL) {
889 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
894 * check IBM's new obscure way of naming drives
895 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
896 * but doesn't care about the other info (size, capacity etc)
898 if (!strncmp(adp->device->param->model, "IC", 2) &&
899 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
900 !strncmp(adp->device->param->model + 8, "AV", 2)))
907 ad_timeout(struct ad_request *request)
909 struct ad_softc *adp = request->softc;
911 adp->device->channel->running = NULL;
912 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
913 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
914 request->tag, request->serv);
916 if (request->flags & ADR_F_DMA_USED) {
917 ata_dmadone(adp->device);
918 ad_invalidatequeue(adp, request);
919 if (request->retries == AD_MAX_RETRIES) {
920 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
921 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
922 request->retries = 0;
926 /* if retries still permit, reinject this request */
927 if (request->retries++ < AD_MAX_RETRIES) {
928 ad_requeue(adp->device->channel, request);
931 /* retries all used up, return error */
932 request->bio->bio_buf->b_error = EIO;
933 request->bio->bio_buf->b_flags |= B_ERROR;
934 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
935 biodone(request->bio);
938 ata_reinit(adp->device->channel);
942 ad_reinit(struct ata_device *atadev)
944 struct ad_softc *adp = atadev->driver;
946 /* reinit disk parameters */
947 ad_invalidatequeue(atadev->driver, NULL);
948 ata_command(atadev, ATA_C_SET_MULTI, 0,
949 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
950 if (adp->device->mode >= ATA_DMA)
951 ata_dmainit(atadev, ata_pmode(adp->device->param),
952 ata_wmode(adp->device->param),
953 ata_umode(adp->device->param));
955 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
959 ad_print(struct ad_softc *adp)
962 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
963 adp->device->param->model, adp->device->param->revision,
964 ad_version(adp->device->param->version_major),
965 device_get_unit(adp->device->channel->dev),
966 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
968 ata_prtdev(adp->device,
969 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
970 (unsigned long long)(adp->total_secs /
971 ((1024L*1024L)/DEV_BSIZE)),
972 (unsigned long long) adp->total_secs,
973 (unsigned long long) (adp->total_secs /
974 (adp->heads * adp->sectors)),
975 adp->heads, adp->sectors, DEV_BSIZE);
977 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
978 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
979 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
980 ata_mode2str(adp->device->mode));
982 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
983 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
984 ata_umode(adp->device->param),
985 adp->device->param->hwres_cblid);
989 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
990 (unsigned long long)(adp->total_secs /
991 ((1024L * 1024L) / DEV_BSIZE)),
992 adp->device->param->model,
993 (unsigned long long)(adp->total_secs /
994 (adp->heads*adp->sectors)),
995 adp->heads, adp->sectors,
996 device_get_unit(adp->device->channel->dev),
997 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
998 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
999 ata_mode2str(adp->device->mode));
1003 ad_version(u_int16_t version)
1007 if (version == 0xffff)
1009 for (bit = 15; bit >= 0; bit--)
1010 if (version & (1<<bit))