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.36 2007/05/19 02:39:02 dillon 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;
111 struct disk_info info;
114 adp = kmalloc(sizeof(struct ad_softc), M_AD, M_WAITOK | M_ZERO);
116 KKASSERT(atadev->channel->req_mpipe.max_count != 0);
118 adp->device = atadev;
120 adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
122 adp->lun = ata_get_lun(&adp_lun_map);
124 ata_set_name(atadev, "ad", adp->lun);
125 adp->heads = atadev->param->heads;
126 adp->sectors = atadev->param->sectors;
127 adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;
128 bioq_init(&adp->bio_queue);
130 /* does this device need oldstyle CHS addressing */
131 if (!ad_version(atadev->param->version_major) ||
132 !(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
133 adp->flags |= AD_F_CHS_USED;
135 /* use the 28bit LBA size if valid */
136 if (atadev->param->cylinders == 16383 &&
137 adp->total_secs < atadev->param->lba_size)
138 adp->total_secs = atadev->param->lba_size;
140 /* use the 48bit LBA size if valid */
141 if (atadev->param->support.address48 &&
142 atadev->param->lba_size48 > 268435455)
143 adp->total_secs = atadev->param->lba_size48;
146 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
147 /* use multiple sectors/interrupt if device supports it */
148 adp->transfersize = DEV_BSIZE;
149 if (ad_version(atadev->param->version_major)) {
150 int secsperint = max(1, min(atadev->param->sectors_intr, 16));
152 if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
153 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
154 adp->transfersize *= secsperint;
157 /* enable read caching if not default on device */
158 if (ata_command(atadev, ATA_C_SETFEATURES,
159 0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
160 ata_prtdev(atadev, "enabling readahead cache failed\n");
162 /* enable write caching if allowed and not default on device */
163 if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
164 if (ata_command(atadev, ATA_C_SETFEATURES,
165 0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
166 ata_prtdev(atadev, "enabling write cache failed\n");
169 if (ata_command(atadev, ATA_C_SETFEATURES,
170 0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
171 ata_prtdev(atadev, "disabling write cache failed\n");
174 /* use DMA if allowed and if drive/controller supports it */
176 ata_dmainit(atadev, ata_pmode(atadev->param),
177 ata_wmode(atadev->param), ata_umode(atadev->param));
179 ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
181 /* use tagged queueing if allowed and supported */
182 if (ata_tags && ad_tagsupported(adp)) {
183 adp->num_tags = atadev->param->queuelen;
184 adp->flags |= AD_F_TAG_ENABLED;
185 adp->device->channel->flags |= ATA_QUEUED;
186 if (ata_command(atadev, ATA_C_SETFEATURES,
187 0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
188 ata_prtdev(atadev, "disabling release interrupt failed\n");
189 if (ata_command(atadev, ATA_C_SETFEATURES,
190 0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
191 ata_prtdev(atadev, "disabling service interrupt failed\n");
194 ATA_UNLOCK_CH(atadev->channel);
196 devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
197 DEVSTAT_NO_ORDERED_TAGS,
198 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
199 DEVSTAT_PRIORITY_DISK);
201 dev = disk_create(adp->lun, &adp->disk, &ad_ops);
203 dev->si_iosize_max = 256 * DEV_BSIZE;
206 /* construct the disk_info */
207 bzero(&info, sizeof(info));
208 info.d_media_blksize = DEV_BSIZE;
209 info.d_media_blocks = adp->total_secs;
210 info.d_nheads = adp->heads;
211 info.d_secpertrack = adp->sectors;
212 info.d_ncylinders = adp->total_secs /
213 (info.d_nheads * info.d_secpertrack);
214 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
215 disk_setdiskinfo(&adp->disk, &info);
217 atadev->driver = adp;
220 /* if this disk belongs to an ATA RAID dont print the probe */
221 if (ata_raiddisk_attach(adp))
222 adp->flags |= AD_F_RAID_SUBDISK;
224 if (atadev->driver) {
226 ata_enclosure_print(atadev);
232 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
234 struct ad_softc *adp = atadev->driver;
235 struct ad_request *request;
239 atadev->flags |= ATA_D_DETACHING;
240 ata_prtdev(atadev, "removed from configuration\n");
241 ad_invalidatequeue(adp, NULL);
242 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
243 if (request->softc != adp)
245 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
246 request->bio->bio_buf->b_error = ENXIO;
247 request->bio->bio_buf->b_flags |= B_ERROR;
248 biodone(request->bio);
252 while ((bio = bioq_first(&adp->bio_queue))) {
253 bioq_remove(&adp->bio_queue, bio);
256 bp->b_flags |= B_ERROR;
259 disk_invalidate(&adp->disk);
260 devstat_remove_entry(&adp->stats);
261 disk_destroy(&adp->disk);
263 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
264 ata_prtdev(atadev, "flushing cache on detach failed\n");
266 if (adp->flags & AD_F_RAID_SUBDISK)
267 ata_raiddisk_detach(adp);
268 ata_free_name(atadev);
269 ata_free_lun(&adp_lun_map, adp->lun);
270 atadev->driver = NULL;
276 adopen(struct dev_open_args *ap)
278 struct ad_softc *adp = ap->a_head.a_dev->si_drv1;
280 if (adp->flags & AD_F_RAID_SUBDISK)
286 adclose(struct dev_close_args *ap)
288 struct ad_softc *adp = ap->a_head.a_dev->si_drv1;
290 crit_enter(); /* interlock non-atomic channel lock */
291 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
292 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
293 ata_prtdev(adp->device, "flushing cache on close failed\n");
294 ATA_UNLOCK_CH(adp->device->channel);
300 * note: always use the passed device rather then bp->b_dev, as the bp
301 * may have been translated through several layers.
304 adstrategy(struct dev_strategy_args *ap)
306 cdev_t dev = ap->a_head.a_dev;
307 struct bio *bio = ap->a_bio;
308 struct buf *bp = bio->bio_buf;
309 struct ad_softc *adp = dev->si_drv1;
311 if (adp->device->flags & ATA_D_DETACHING) {
313 bp->b_flags |= B_ERROR;
317 bio->bio_driver_info = dev;
319 bioqdisksort(&adp->bio_queue, bio);
321 ata_start(adp->device->channel);
326 addump(struct dev_dump_args *ap)
328 cdev_t dev = ap->a_head.a_dev;
329 struct ad_softc *adp = dev->si_drv1;
330 struct ad_request request;
335 /* force PIO mode for dumps */
336 adp->device->mode = ATA_PIO;
337 ata_reinit(adp->device->channel);
340 bzero(&request, sizeof(struct ad_request));
342 request.blockaddr = ap->a_offset / DEV_BSIZE;
343 request.bytecount = ap->a_length;
344 request.data = ap->a_virtual;
345 callout_init(&request.callout);
346 while (request.bytecount > 0) {
347 ad_transfer(&request);
348 if (request.flags & ADR_F_ERROR)
350 request.donecount += request.currentsize;
351 request.bytecount -= request.currentsize;
355 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
356 ata_prtdev(adp->device, "timeout waiting for final ready\n");
362 * Critical section is held when this function is called
366 ad_start(struct ata_device *atadev)
368 struct ad_softc *adp = atadev->driver;
369 struct bio *bio = bioq_first(&adp->bio_queue);
371 struct ad_request *request;
378 /* if tagged queueing enabled get next free tag */
379 if (adp->flags & AD_F_TAG_ENABLED) {
380 while (tag <= adp->num_tags && adp->tags[tag])
382 if (tag > adp->num_tags )
387 * Allocate a request. The allocation can only fail if the pipeline
388 * is full, in which case the request will be picked up later when
389 * ad_start() is called after another request completes.
391 request = mpipe_alloc_nowait(&atadev->channel->req_mpipe);
392 if (request == NULL) {
393 ata_prtdev(atadev, "pipeline full allocating request in ad_start\n");
397 KASSERT((bio->bio_offset & DEV_BMASK) == 0,
398 ("bio_offset not on sector boundary %08llx", bio->bio_offset));
401 request->softc = adp;
403 request->blockaddr = (u_int64_t)(bio->bio_offset >> DEV_BSHIFT);
404 request->bytecount = bp->b_bcount;
405 request->data = bp->b_data;
407 callout_init(&request->callout);
408 if (bp->b_cmd == BUF_CMD_READ)
409 request->flags |= ADR_F_READ;
410 if (adp->device->mode >= ATA_DMA) {
411 if (ata_dmaalloc(atadev, M_NOWAIT) != 0) {
412 mpipe_free(&atadev->channel->req_mpipe, request);
413 ata_prtdev(atadev, "pipeline full allocated dmabuf in ad_start\n");
414 /* do not revert to PIO, wait for ad_start after I/O completion */
419 /* insert in tag array */
420 adp->tags[tag] = request;
422 /* remove from drive queue */
423 bioq_remove(&adp->bio_queue, bio);
425 /* link onto controller queue */
426 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
430 ad_requeue(struct ata_channel *chan, struct ad_request *req)
432 if (req->donecount) {
434 "WARNING: resetting donecount %u for retry\n",
436 req->bytecount += req->donecount;
439 TAILQ_INSERT_HEAD(&chan->ata_queue, req, chain);
443 ad_transfer(struct ad_request *request)
445 struct ad_softc *adp;
447 u_int32_t count, max_count;
449 int flags = ATA_IMMEDIATE;
451 /* get request params */
452 adp = request->softc;
454 /* calculate transfer details */
455 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
457 if (request->donecount == 0) {
459 /* start timeout for this transfer */
461 callout_stop(&request->callout);
463 callout_reset(&request->callout, 10 * hz,
464 (void *)ad_timeout, request);
467 /* setup transfer parameters */
468 count = howmany(request->bytecount, DEV_BSIZE);
469 max_count = adp->device->param->support.address48 ? 65536 : 256;
470 if (count > max_count) {
471 ata_prtdev(adp->device,
472 "count %d size transfers not supported\n", count);
476 if (adp->flags & AD_F_CHS_USED) {
477 int sector = (lba % adp->sectors) + 1;
478 int cylinder = lba / (adp->sectors * adp->heads);
479 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
481 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
482 adp->device->flags |= ATA_D_USE_CHS;
485 /* setup first transfer length */
486 request->currentsize = min(request->bytecount, adp->transfersize);
488 devstat_start_transaction(&adp->stats);
490 /* does this drive & transfer work with DMA ? */
491 request->flags &= ~ADR_F_DMA_USED;
492 if (adp->device->mode >= ATA_DMA &&
493 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
494 request->flags |= ADR_F_DMA_USED;
495 request->currentsize = request->bytecount;
497 /* do we have tags enabled ? */
498 if (adp->flags & AD_F_TAG_ENABLED) {
499 cmd = (request->flags & ADR_F_READ) ?
500 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
502 if (ata_command(adp->device, cmd, lba,
503 request->tag << 3, count, flags)) {
504 ata_prtdev(adp->device, "error executing command");
505 goto transfer_failed;
507 if (ata_wait(adp->device, ATA_S_READY)) {
508 ata_prtdev(adp->device, "timeout waiting for READY\n");
509 goto transfer_failed;
513 /* if ATA bus RELEASE check for SERVICE */
514 if (adp->flags & AD_F_TAG_ENABLED &&
515 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
517 return ad_service(adp, 1);
520 cmd = (request->flags & ADR_F_READ) ?
521 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
523 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
524 ata_prtdev(adp->device, "error executing command");
525 goto transfer_failed;
529 * wait for data transfer phase
531 * well this should be here acording to specs, but older
532 * promise controllers doesn't like it, they lockup!
534 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
535 ata_prtdev(adp->device, "timeout waiting for data phase\n");
536 goto transfer_failed;
541 /* start transfer, return and wait for interrupt */
542 ata_dmastart(adp->device, request->data, request->bytecount,
543 request->flags & ADR_F_READ);
544 return ATA_OP_CONTINUES;
547 /* does this drive support multi sector transfers ? */
548 if (request->currentsize > DEV_BSIZE)
549 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
551 /* just plain old single sector transfer */
553 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
555 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
556 ata_prtdev(adp->device, "error executing command");
557 goto transfer_failed;
561 /* calculate this transfer length */
562 request->currentsize = min(request->bytecount, adp->transfersize);
564 /* if this is a PIO read operation, return and wait for interrupt */
565 if (request->flags & ADR_F_READ)
566 return ATA_OP_CONTINUES;
568 /* ready to write PIO data ? */
569 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
570 ata_prtdev(adp->device, "timeout waiting for DRQ");
571 goto transfer_failed;
574 /* output the data */
575 if (adp->device->channel->flags & ATA_USE_16BIT)
576 ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
577 (void *)((uintptr_t)request->data + request->donecount),
578 request->currentsize / sizeof(int16_t));
580 ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
581 (void *)((uintptr_t)request->data + request->donecount),
582 request->currentsize / sizeof(int32_t));
583 return ATA_OP_CONTINUES;
586 callout_stop(&request->callout);
587 ad_invalidatequeue(adp, request);
588 kprintf(" - resetting\n");
590 /* if retries still permit, reinject this request */
591 if (request->retries++ < AD_MAX_RETRIES)
592 ad_requeue(adp->device->channel, request);
594 /* retries all used up, return error */
595 request->bio->bio_buf->b_error = EIO;
596 request->bio->bio_buf->b_flags |= B_ERROR;
597 request->bio->bio_buf->b_resid = request->bytecount;
598 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
599 biodone(request->bio);
602 ata_reinit(adp->device->channel);
603 return ATA_OP_CONTINUES;
607 ad_interrupt(struct ad_request *request)
609 struct ad_softc *adp = request->softc;
613 /* finish DMA transfer */
614 if (request->flags & ADR_F_DMA_USED)
615 dma_stat = ata_dmadone(adp->device);
617 dev = request->bio->bio_driver_info;
618 /* do we have a corrected soft error ? */
619 if (adp->device->channel->status & ATA_S_CORR)
620 diskerr(request->bio, dev,
621 "soft error (ECC corrected)", LOG_PRINTF,
624 /* did any real errors happen ? */
625 if ((adp->device->channel->status & ATA_S_ERROR) ||
626 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
627 adp->device->channel->error =
628 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
629 diskerr(request->bio, dev,
630 (adp->device->channel->error & ATA_E_ICRC) ?
631 "UDMA ICRC error" : "hard error", LOG_PRINTF,
634 /* if this is a UDMA CRC error, reinject request */
635 if (request->flags & ADR_F_DMA_USED &&
636 adp->device->channel->error & ATA_E_ICRC) {
637 callout_stop(&request->callout);
638 ad_invalidatequeue(adp, request);
640 if (request->retries++ < AD_MAX_RETRIES)
641 kprintf(" retrying\n");
643 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
644 kprintf(" falling back to PIO mode\n");
646 ad_requeue(adp->device->channel, request);
647 return ATA_OP_FINISHED;
650 /* if using DMA, try once again in PIO mode */
651 if (request->flags & ADR_F_DMA_USED) {
652 callout_stop(&request->callout);
653 ad_invalidatequeue(adp, request);
654 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
655 request->flags |= ADR_F_FORCE_PIO;
656 kprintf(" trying PIO mode\n");
657 ad_requeue(adp->device->channel, request);
658 return ATA_OP_FINISHED;
661 request->flags |= ADR_F_ERROR;
662 kprintf(" status=%02x error=%02x\n",
663 adp->device->channel->status, adp->device->channel->error);
666 /* if we arrived here with forced PIO mode, DMA doesn't work right */
667 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
668 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
670 /* if this was a PIO read operation, get the data */
671 if (!(request->flags & ADR_F_DMA_USED) &&
672 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
674 /* ready to receive data? */
675 if ((adp->device->channel->status & ATA_S_READY) == 0)
676 ata_prtdev(adp->device, "read interrupt arrived early");
678 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
679 ata_prtdev(adp->device, "read error detected (too) late");
680 request->flags |= ADR_F_ERROR;
683 /* data ready, read in */
684 if (adp->device->channel->flags & ATA_USE_16BIT)
685 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
686 (void*)((uintptr_t)request->data + request->donecount),
687 request->currentsize / sizeof(int16_t));
689 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
690 (void*)((uintptr_t)request->data + request->donecount),
691 request->currentsize / sizeof(int32_t));
695 /* finish up transfer */
696 if (request->flags & ADR_F_ERROR) {
697 request->bio->bio_buf->b_error = EIO;
698 request->bio->bio_buf->b_flags |= B_ERROR;
701 request->bytecount -= request->currentsize;
702 request->donecount += request->currentsize;
703 if (request->bytecount > 0) {
704 ad_transfer(request);
705 return ATA_OP_CONTINUES;
709 /* disarm timeout for this transfer */
710 callout_stop(&request->callout);
712 request->bio->bio_buf->b_resid = request->bytecount;
714 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
715 biodone(request->bio);
719 /* check for SERVICE (tagged operations only) */
720 return ad_service(adp, 1);
724 ad_service(struct ad_softc *adp, int change)
726 /* do we have to check the other device on this channel ? */
727 if (adp->device->channel->flags & ATA_QUEUED && change) {
728 int device = adp->device->unit;
730 if (adp->device->unit == ATA_MASTER) {
731 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
732 (adp->device->channel->device[SLAVE].driver) &&
733 ((struct ad_softc *) (adp->device->channel->
734 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
738 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
739 (adp->device->channel->device[MASTER].driver) &&
740 ((struct ad_softc *) (adp->device->channel->
741 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
744 if (device != adp->device->unit &&
746 (adp->device->channel->
747 device[ATA_DEV(device)].driver))->outstanding > 0) {
748 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
749 adp = adp->device->channel->device[ATA_DEV(device)].driver;
753 adp->device->channel->status =
754 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
756 /* do we have a SERVICE request from the drive ? */
757 if (adp->flags & AD_F_TAG_ENABLED &&
758 adp->outstanding > 0 &&
759 adp->device->channel->status & ATA_S_SERVICE) {
760 struct ad_request *request;
763 /* check for error */
764 if (adp->device->channel->status & ATA_S_ERROR) {
765 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
766 adp->device->channel->status,
767 adp->device->channel->error);
768 ad_invalidatequeue(adp, NULL);
769 return ATA_OP_FINISHED;
772 /* issue SERVICE cmd */
773 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
774 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
775 ad_invalidatequeue(adp, NULL);
776 return ATA_OP_FINISHED;
779 /* setup the transfer environment when ready */
780 if (ata_wait(adp->device, ATA_S_READY)) {
781 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
782 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
783 adp->device->channel->status,
784 adp->device->channel->error);
785 ad_invalidatequeue(adp, NULL);
786 return ATA_OP_FINISHED;
788 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
789 if (!(request = adp->tags[tag])) {
790 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
791 ad_invalidatequeue(adp, NULL);
792 return ATA_OP_FINISHED;
794 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
795 adp->device->channel->running = request;
798 /* start DMA transfer when ready */
799 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
800 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
801 adp->device->channel->status,
802 adp->device->channel->error);
803 ad_invalidatequeue(adp, NULL);
804 return ATA_OP_FINISHED;
806 ata_dmastart(adp->device, request->data, request->bytecount,
807 request->flags & ADR_F_READ);
808 return ATA_OP_CONTINUES;
810 return ATA_OP_FINISHED;
814 ad_free(struct ad_request *request)
817 ata_dmafree(request->softc->device);
818 request->softc->tags[request->tag] = NULL;
819 mpipe_free(&request->softc->device->channel->req_mpipe, request);
824 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
826 /* if tags used invalidate all other tagged transfers */
827 if (adp->flags & AD_F_TAG_ENABLED) {
828 struct ad_request *tmpreq;
831 ata_prtdev(adp->device, "invalidating queued requests\n");
832 for (tag = 0; tag <= adp->num_tags; tag++) {
833 tmpreq = adp->tags[tag];
834 adp->tags[tag] = NULL;
835 if (tmpreq == request || tmpreq == NULL)
837 callout_stop(&request->callout);
838 ad_requeue(adp->device->channel, tmpreq);
840 if (ata_command(adp->device, ATA_C_NOP,
841 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
842 ata_prtdev(adp->device, "flush queue failed\n");
843 adp->outstanding = 0;
848 ad_tagsupported(struct ad_softc *adp)
850 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
853 switch (adp->device->channel->chiptype) {
854 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
861 /* check that drive does DMA, has tags enabled, and is one we know works */
862 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
863 adp->device->param->enabled.queued) {
864 while (good[i] != NULL) {
865 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
870 * check IBM's new obscure way of naming drives
871 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
872 * but doesn't care about the other info (size, capacity etc)
874 if (!strncmp(adp->device->param->model, "IC", 2) &&
875 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
876 !strncmp(adp->device->param->model + 8, "AV", 2)))
883 ad_timeout(struct ad_request *request)
885 struct ad_softc *adp = request->softc;
887 adp->device->channel->running = NULL;
888 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
889 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
890 request->tag, request->serv);
892 if (request->flags & ADR_F_DMA_USED) {
893 ata_dmadone(adp->device);
894 ad_invalidatequeue(adp, request);
895 if (request->retries == AD_MAX_RETRIES) {
896 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
897 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
898 request->retries = 0;
902 /* if retries still permit, reinject this request */
903 if (request->retries++ < AD_MAX_RETRIES) {
904 ad_requeue(adp->device->channel, request);
907 /* retries all used up, return error */
908 request->bio->bio_buf->b_error = EIO;
909 request->bio->bio_buf->b_flags |= B_ERROR;
910 devstat_end_transaction_buf(&adp->stats, request->bio->bio_buf);
911 biodone(request->bio);
914 ata_reinit(adp->device->channel);
918 ad_reinit(struct ata_device *atadev)
920 struct ad_softc *adp = atadev->driver;
922 /* reinit disk parameters */
923 ad_invalidatequeue(atadev->driver, NULL);
924 ata_command(atadev, ATA_C_SET_MULTI, 0,
925 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
926 if (adp->device->mode >= ATA_DMA)
927 ata_dmainit(atadev, ata_pmode(adp->device->param),
928 ata_wmode(adp->device->param),
929 ata_umode(adp->device->param));
931 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
935 ad_print(struct ad_softc *adp)
938 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
939 adp->device->param->model, adp->device->param->revision,
940 ad_version(adp->device->param->version_major),
941 device_get_unit(adp->device->channel->dev),
942 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
944 ata_prtdev(adp->device,
945 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
946 (unsigned long long)(adp->total_secs /
947 ((1024L*1024L)/DEV_BSIZE)),
948 (unsigned long long) adp->total_secs,
949 (unsigned long long) (adp->total_secs /
950 (adp->heads * adp->sectors)),
951 adp->heads, adp->sectors, DEV_BSIZE);
953 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
954 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
955 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
956 ata_mode2str(adp->device->mode));
958 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
959 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
960 ata_umode(adp->device->param),
961 adp->device->param->hwres_cblid);
965 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
966 (unsigned long long)(adp->total_secs /
967 ((1024L * 1024L) / DEV_BSIZE)),
968 adp->device->param->model,
969 (unsigned long long)(adp->total_secs /
970 (adp->heads*adp->sectors)),
971 adp->heads, adp->sectors,
972 device_get_unit(adp->device->channel->dev),
973 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
974 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
975 ata_mode2str(adp->device->mode));
979 ad_version(u_int16_t version)
983 if (version == 0xffff)
985 for (bit = 15; bit >= 0; bit--)
986 if (version & (1<<bit))