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.2 2003/06/17 04:28:22 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>
48 #include <machine/md_var.h>
49 #include <machine/bus.h>
50 #include <machine/clock.h>
52 #include <dev/ata/ata-all.h>
53 #include <dev/ata/ata-disk.h>
54 #include <dev/ata/ata-raid.h>
56 /* device structures */
57 static d_open_t adopen;
58 static d_close_t adclose;
59 static d_strategy_t adstrategy;
60 static d_dump_t addump;
61 static struct cdevsw ad_cdevsw = {
65 /* write */ physwrite,
69 /* strategy */ adstrategy,
77 static struct cdevsw addisk_cdevsw;
80 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
81 static int ad_tagsupported(struct ad_softc *);
82 static void ad_timeout(struct ad_request *);
83 static void ad_free(struct ad_request *);
84 static int ad_version(u_int16_t);
87 #define AD_MAX_RETRIES 3
90 static u_int32_t adp_lun_map = 0;
91 static int ata_dma = 1;
92 static int ata_wc = 1;
93 static int ata_tags = 0;
94 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
95 TUNABLE_INT("hw.ata.wc", &ata_wc);
96 TUNABLE_INT("hw.ata.tags", &ata_tags);
97 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
100 SYSCTL_DECL(_hw_ata);
101 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
102 "ATA disk DMA mode control");
103 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
104 "ATA disk write caching");
105 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
106 "ATA disk tagged queuing support");
109 ad_attach(struct ata_device *atadev)
111 struct ad_softc *adp;
114 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
115 ata_prtdev(atadev, "failed to allocate driver storage\n");
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 bufq_init(&adp->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;
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->channel, atadev->unit, ata_pmode(atadev->param),
176 ata_wmode(atadev->param), ata_umode(atadev->param));
178 ata_dmainit(atadev->channel, atadev->unit,
179 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, 0, &ad_cdevsw, &addisk_cdevsw);
203 dev->si_iosize_max = 256 * DEV_BSIZE;
206 /* construct the disklabel */
207 bzero(&adp->disk.d_label, sizeof(struct disklabel));
208 adp->disk.d_label.d_secsize = DEV_BSIZE;
209 adp->disk.d_label.d_nsectors = adp->sectors;
210 adp->disk.d_label.d_ntracks = adp->heads;
211 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
212 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
213 adp->disk.d_label.d_secperunit = adp->total_secs;
215 atadev->driver = adp;
218 /* if this disk belongs to an ATA RAID dont print the probe */
219 if (ata_raiddisk_attach(adp))
220 adp->flags |= AD_F_RAID_SUBDISK;
222 if (atadev->driver) {
224 ata_enclosure_print(atadev);
230 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
232 struct ad_softc *adp = atadev->driver;
233 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->bp->b_error = ENXIO;
244 request->bp->b_flags |= B_ERROR;
245 biodone(request->bp);
248 while ((bp = bufq_first(&adp->queue))) {
249 bufq_remove(&adp->queue, bp);
251 bp->b_flags |= B_ERROR;
254 disk_invalidate(&adp->disk);
255 disk_destroy(adp->dev);
256 devstat_remove_entry(&adp->stats);
258 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
259 ata_prtdev(atadev, "flushing cache on detach failed\n");
261 if (adp->flags & AD_F_RAID_SUBDISK)
262 ata_raiddisk_detach(adp);
263 ata_free_name(atadev);
264 ata_free_lun(&adp_lun_map, adp->lun);
265 atadev->driver = NULL;
271 adopen(dev_t dev, int flags, int fmt, struct proc *p)
273 struct ad_softc *adp = dev->si_drv1;
275 if (adp->flags & AD_F_RAID_SUBDISK)
281 adclose(dev_t dev, int flags, int fmt, struct proc *p)
283 struct ad_softc *adp = dev->si_drv1;
285 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
286 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
287 ata_prtdev(adp->device, "flushing cache on close failed\n");
288 ATA_UNLOCK_CH(adp->device->channel);
293 adstrategy(struct buf *bp)
295 struct ad_softc *adp = bp->b_dev->si_drv1;
298 if (adp->device->flags & ATA_D_DETACHING) {
300 bp->b_flags |= B_ERROR;
305 bufqdisksort(&adp->queue, bp);
307 ata_start(adp->device->channel);
313 struct ad_softc *adp = dev->si_drv1;
314 struct ad_request request;
315 u_int count, blkno, secsize;
316 vm_offset_t addr = 0;
318 int dumppages = MAXDUMPPGS;
322 if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
328 /* force PIO mode for dumps */
329 adp->device->mode = ATA_PIO;
330 ata_reinit(adp->device->channel);
332 blkcnt = howmany(PAGE_SIZE, secsize);
338 if ((count / blkcnt) < dumppages)
339 dumppages = count / blkcnt;
341 for (i = 0; i < dumppages; ++i) {
342 vm_offset_t a = addr + (i * PAGE_SIZE);
343 if (is_physical_memory(a))
344 va = pmap_kenter_temporary(trunc_page(a), i);
346 va = pmap_kenter_temporary(trunc_page(0), i);
349 bzero(&request, sizeof(struct ad_request));
351 request.blockaddr = blkno;
352 request.bytecount = PAGE_SIZE * dumppages;
355 while (request.bytecount > 0) {
356 ad_transfer(&request);
357 if (request.flags & ADR_F_ERROR)
359 request.donecount += request.currentsize;
360 request.bytecount -= request.currentsize;
364 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
367 blkno += blkcnt * dumppages;
368 count -= blkcnt * dumppages;
369 addr += PAGE_SIZE * dumppages;
372 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
373 ata_prtdev(adp->device, "timeout waiting for final ready\n");
378 ad_start(struct ata_device *atadev)
380 struct ad_softc *adp = atadev->driver;
381 struct buf *bp = bufq_first(&adp->queue);
382 struct ad_request *request;
388 /* if tagged queueing enabled get next free tag */
389 if (adp->flags & AD_F_TAG_ENABLED) {
390 while (tag <= adp->num_tags && adp->tags[tag])
392 if (tag > adp->num_tags )
396 if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
397 ata_prtdev(atadev, "out of memory in start\n");
402 request->softc = adp;
404 request->blockaddr = bp->b_pblkno;
405 request->bytecount = bp->b_bcount;
406 request->data = bp->b_data;
408 if (bp->b_flags & B_READ)
409 request->flags |= ADR_F_READ;
410 if (adp->device->mode >= ATA_DMA) {
411 if (!(request->dmatab = ata_dmaalloc(atadev->channel, atadev->unit)))
412 adp->device->mode = ATA_PIO;
415 /* insert in tag array */
416 adp->tags[tag] = request;
418 /* remove from drive queue */
419 bufq_remove(&adp->queue, bp);
421 /* link onto controller queue */
422 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
426 ad_transfer(struct ad_request *request)
428 struct ad_softc *adp;
430 u_int32_t count, max_count;
432 int flags = ATA_IMMEDIATE;
434 /* get request params */
435 adp = request->softc;
437 /* calculate transfer details */
438 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
440 if (request->donecount == 0) {
442 /* start timeout for this transfer */
444 request->timeout_handle.callout = NULL;
446 request->timeout_handle =
447 timeout((timeout_t*)ad_timeout, request, 10 * hz);
449 /* setup transfer parameters */
450 count = howmany(request->bytecount, DEV_BSIZE);
451 max_count = adp->device->param->support.address48 ? 65536 : 256;
452 if (count > max_count) {
453 ata_prtdev(adp->device,
454 "count %d size transfers not supported\n", count);
458 if (adp->flags & AD_F_CHS_USED) {
459 int sector = (lba % adp->sectors) + 1;
460 int cylinder = lba / (adp->sectors * adp->heads);
461 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
463 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
464 adp->device->flags |= ATA_D_USE_CHS;
467 /* setup first transfer length */
468 request->currentsize = min(request->bytecount, adp->transfersize);
470 devstat_start_transaction(&adp->stats);
472 /* does this drive & transfer work with DMA ? */
473 request->flags &= ~ADR_F_DMA_USED;
474 if (adp->device->mode >= ATA_DMA &&
475 !ata_dmasetup(adp->device->channel, adp->device->unit,
476 request->dmatab, request->data, request->bytecount)) {
477 request->flags |= ADR_F_DMA_USED;
478 request->currentsize = request->bytecount;
480 /* do we have tags enabled ? */
481 if (adp->flags & AD_F_TAG_ENABLED) {
482 cmd = (request->flags & ADR_F_READ) ?
483 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
485 if (ata_command(adp->device, cmd, lba,
486 request->tag << 3, count, flags)) {
487 ata_prtdev(adp->device, "error executing command");
488 goto transfer_failed;
490 if (ata_wait(adp->device, ATA_S_READY)) {
491 ata_prtdev(adp->device, "timeout waiting for READY\n");
492 goto transfer_failed;
496 /* if ATA bus RELEASE check for SERVICE */
497 if (adp->flags & AD_F_TAG_ENABLED &&
498 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
500 return ad_service(adp, 1);
503 cmd = (request->flags & ADR_F_READ) ?
504 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
506 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
507 ata_prtdev(adp->device, "error executing command");
508 goto transfer_failed;
512 * wait for data transfer phase
514 * well this should be here acording to specs, but older
515 * promise controllers doesn't like it, they lockup!
517 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
518 ata_prtdev(adp->device, "timeout waiting for data phase\n");
519 goto transfer_failed;
524 /* start transfer, return and wait for interrupt */
525 ata_dmastart(adp->device->channel, adp->device->unit,
526 request->dmatab, request->flags & ADR_F_READ);
527 return ATA_OP_CONTINUES;
530 /* does this drive support multi sector transfers ? */
531 if (request->currentsize > DEV_BSIZE)
532 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
534 /* just plain old single sector transfer */
536 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
538 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
539 ata_prtdev(adp->device, "error executing command");
540 goto transfer_failed;
544 /* calculate this transfer length */
545 request->currentsize = min(request->bytecount, adp->transfersize);
547 /* if this is a PIO read operation, return and wait for interrupt */
548 if (request->flags & ADR_F_READ)
549 return ATA_OP_CONTINUES;
551 /* ready to write PIO data ? */
552 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
553 ata_prtdev(adp->device, "timeout waiting for DRQ");
554 goto transfer_failed;
557 /* output the data */
558 if (adp->device->channel->flags & ATA_USE_16BIT)
559 ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
560 (void *)((uintptr_t)request->data + request->donecount),
561 request->currentsize / sizeof(int16_t));
563 ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
564 (void *)((uintptr_t)request->data + request->donecount),
565 request->currentsize / sizeof(int32_t));
566 return ATA_OP_CONTINUES;
569 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
570 ad_invalidatequeue(adp, request);
571 printf(" - resetting\n");
573 /* if retries still permit, reinject this request */
574 if (request->retries++ < AD_MAX_RETRIES)
575 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
577 /* retries all used up, return error */
578 request->bp->b_error = EIO;
579 request->bp->b_flags |= B_ERROR;
580 request->bp->b_resid = request->bytecount;
581 devstat_end_transaction_buf(&adp->stats, request->bp);
582 biodone(request->bp);
585 ata_reinit(adp->device->channel);
586 return ATA_OP_CONTINUES;
590 ad_interrupt(struct ad_request *request)
592 struct ad_softc *adp = request->softc;
595 /* finish DMA transfer */
596 if (request->flags & ADR_F_DMA_USED)
597 dma_stat = ata_dmadone(adp->device->channel);
599 /* do we have a corrected soft error ? */
600 if (adp->device->channel->status & ATA_S_CORR)
601 diskerr(request->bp, "soft error (ECC corrected)", LOG_PRINTF,
602 request->blockaddr + (request->donecount / DEV_BSIZE),
605 /* did any real errors happen ? */
606 if ((adp->device->channel->status & ATA_S_ERROR) ||
607 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
608 adp->device->channel->error =
609 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
610 diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
611 "UDMA ICRC error" : "hard error", LOG_PRINTF,
612 request->blockaddr + (request->donecount / DEV_BSIZE),
615 /* if this is a UDMA CRC error, reinject request */
616 if (request->flags & ADR_F_DMA_USED &&
617 adp->device->channel->error & ATA_E_ICRC) {
618 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
619 ad_invalidatequeue(adp, request);
621 if (request->retries++ < AD_MAX_RETRIES)
622 printf(" retrying\n");
624 ata_dmainit(adp->device->channel, adp->device->unit,
625 ata_pmode(adp->device->param), -1, -1);
626 printf(" falling back to PIO mode\n");
628 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
629 return ATA_OP_FINISHED;
632 /* if using DMA, try once again in PIO mode */
633 if (request->flags & ADR_F_DMA_USED) {
634 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
635 ad_invalidatequeue(adp, request);
636 ata_dmainit(adp->device->channel, adp->device->unit,
637 ata_pmode(adp->device->param), -1, -1);
638 request->flags |= ADR_F_FORCE_PIO;
639 printf(" trying PIO mode\n");
640 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
641 return ATA_OP_FINISHED;
644 request->flags |= ADR_F_ERROR;
645 printf(" status=%02x error=%02x\n",
646 adp->device->channel->status, adp->device->channel->error);
649 /* if we arrived here with forced PIO mode, DMA doesn't work right */
650 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
651 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
653 /* if this was a PIO read operation, get the data */
654 if (!(request->flags & ADR_F_DMA_USED) &&
655 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
657 /* ready to receive data? */
658 if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
659 != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
660 ata_prtdev(adp->device, "read interrupt arrived early");
662 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
663 ata_prtdev(adp->device, "read error detected (too) late");
664 request->flags |= ADR_F_ERROR;
667 /* data ready, read in */
668 if (adp->device->channel->flags & ATA_USE_16BIT)
669 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
670 (void*)((uintptr_t)request->data + request->donecount),
671 request->currentsize / sizeof(int16_t));
673 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
674 (void*)((uintptr_t)request->data + request->donecount),
675 request->currentsize / sizeof(int32_t));
679 /* finish up transfer */
680 if (request->flags & ADR_F_ERROR) {
681 request->bp->b_error = EIO;
682 request->bp->b_flags |= B_ERROR;
685 request->bytecount -= request->currentsize;
686 request->donecount += request->currentsize;
687 if (request->bytecount > 0) {
688 ad_transfer(request);
689 return ATA_OP_CONTINUES;
693 /* disarm timeout for this transfer */
694 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
696 request->bp->b_resid = request->bytecount;
698 devstat_end_transaction_buf(&adp->stats, request->bp);
699 biodone(request->bp);
703 /* check for SERVICE (tagged operations only) */
704 return ad_service(adp, 1);
708 ad_service(struct ad_softc *adp, int change)
710 /* do we have to check the other device on this channel ? */
711 if (adp->device->channel->flags & ATA_QUEUED && change) {
712 int device = adp->device->unit;
714 if (adp->device->unit == ATA_MASTER) {
715 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
716 (adp->device->channel->device[SLAVE].driver) &&
717 ((struct ad_softc *) (adp->device->channel->
718 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
722 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
723 (adp->device->channel->device[MASTER].driver) &&
724 ((struct ad_softc *) (adp->device->channel->
725 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
728 if (device != adp->device->unit &&
730 (adp->device->channel->
731 device[ATA_DEV(device)].driver))->outstanding > 0) {
732 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
733 adp = adp->device->channel->device[ATA_DEV(device)].driver;
737 adp->device->channel->status =
738 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
740 /* do we have a SERVICE request from the drive ? */
741 if (adp->flags & AD_F_TAG_ENABLED &&
742 adp->outstanding > 0 &&
743 adp->device->channel->status & ATA_S_SERVICE) {
744 struct ad_request *request;
747 /* check for error */
748 if (adp->device->channel->status & ATA_S_ERROR) {
749 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
750 adp->device->channel->status,
751 adp->device->channel->error);
752 ad_invalidatequeue(adp, NULL);
753 return ATA_OP_FINISHED;
756 /* issue SERVICE cmd */
757 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
758 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
759 ad_invalidatequeue(adp, NULL);
760 return ATA_OP_FINISHED;
763 /* setup the transfer environment when ready */
764 if (ata_wait(adp->device, ATA_S_READY)) {
765 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
766 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
767 adp->device->channel->status,
768 adp->device->channel->error);
769 ad_invalidatequeue(adp, NULL);
770 return ATA_OP_FINISHED;
772 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
773 if (!(request = adp->tags[tag])) {
774 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
775 ad_invalidatequeue(adp, NULL);
776 return ATA_OP_FINISHED;
778 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
779 adp->device->channel->running = request;
782 /* start DMA transfer when ready */
783 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
784 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
785 adp->device->channel->status,
786 adp->device->channel->error);
787 ad_invalidatequeue(adp, NULL);
788 return ATA_OP_FINISHED;
790 ata_dmastart(adp->device->channel, adp->device->unit,
791 request->dmatab, request->flags & ADR_F_READ);
792 return ATA_OP_CONTINUES;
794 return ATA_OP_FINISHED;
798 ad_free(struct ad_request *request)
803 free(request->dmatab, M_DEVBUF);
804 request->softc->tags[request->tag] = NULL;
810 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
812 /* if tags used invalidate all other tagged transfers */
813 if (adp->flags & AD_F_TAG_ENABLED) {
814 struct ad_request *tmpreq;
817 ata_prtdev(adp->device, "invalidating queued requests\n");
818 for (tag = 0; tag <= adp->num_tags; tag++) {
819 tmpreq = adp->tags[tag];
820 adp->tags[tag] = NULL;
821 if (tmpreq == request || tmpreq == NULL)
823 untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
824 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
826 if (ata_command(adp->device, ATA_C_NOP,
827 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
828 ata_prtdev(adp->device, "flush queue failed\n");
829 adp->outstanding = 0;
834 ad_tagsupported(struct ad_softc *adp)
836 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
839 switch (adp->device->channel->chiptype) {
840 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
847 /* check that drive does DMA, has tags enabled, and is one we know works */
848 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
849 adp->device->param->enabled.queued) {
850 while (good[i] != NULL) {
851 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
856 * check IBM's new obscure way of naming drives
857 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
858 * but doesn't care about the other info (size, capacity etc)
860 if (!strncmp(adp->device->param->model, "IC", 2) &&
861 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
862 !strncmp(adp->device->param->model + 8, "AV", 2)))
869 ad_timeout(struct ad_request *request)
871 struct ad_softc *adp = request->softc;
873 adp->device->channel->running = NULL;
874 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
875 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
876 request->tag, request->serv);
878 if (request->flags & ADR_F_DMA_USED) {
879 ata_dmadone(adp->device->channel);
880 ad_invalidatequeue(adp, request);
881 if (request->retries == AD_MAX_RETRIES) {
882 ata_dmainit(adp->device->channel, adp->device->unit,
883 ata_pmode(adp->device->param), -1, -1);
884 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
885 request->retries = 0;
889 /* if retries still permit, reinject this request */
890 if (request->retries++ < AD_MAX_RETRIES) {
891 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
894 /* retries all used up, return error */
895 request->bp->b_error = EIO;
896 request->bp->b_flags |= B_ERROR;
897 devstat_end_transaction_buf(&adp->stats, request->bp);
898 biodone(request->bp);
901 ata_reinit(adp->device->channel);
905 ad_reinit(struct ata_device *atadev)
907 struct ad_softc *adp = atadev->driver;
909 /* reinit disk parameters */
910 ad_invalidatequeue(atadev->driver, NULL);
911 ata_command(atadev, ATA_C_SET_MULTI, 0,
912 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
913 if (adp->device->mode >= ATA_DMA)
914 ata_dmainit(atadev->channel, atadev->unit,
915 ata_pmode(adp->device->param),
916 ata_wmode(adp->device->param),
917 ata_umode(adp->device->param));
919 ata_dmainit(atadev->channel, atadev->unit,
920 ata_pmode(adp->device->param), -1, -1);
924 ad_print(struct ad_softc *adp)
927 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
928 adp->device->param->model, adp->device->param->revision,
929 ad_version(adp->device->param->version_major),
930 device_get_unit(adp->device->channel->dev),
931 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
933 ata_prtdev(adp->device,
934 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
935 (unsigned long long)(adp->total_secs /
936 ((1024L*1024L)/DEV_BSIZE)),
937 (unsigned long long) adp->total_secs,
938 (unsigned long long) (adp->total_secs /
939 (adp->heads * adp->sectors)),
940 adp->heads, adp->sectors, DEV_BSIZE);
942 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
943 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
944 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
945 ata_mode2str(adp->device->mode));
947 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
948 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
949 ata_umode(adp->device->param),
950 adp->device->param->hwres_cblid);
954 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
955 (unsigned long long)(adp->total_secs /
956 ((1024L * 1024L) / DEV_BSIZE)),
957 adp->device->param->model,
958 (unsigned long long)(adp->total_secs /
959 (adp->heads*adp->sectors)),
960 adp->heads, adp->sectors,
961 device_get_unit(adp->device->channel->dev),
962 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
963 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
964 ata_mode2str(adp->device->mode));
968 ad_version(u_int16_t version)
972 if (version == 0xffff)
974 for (bit = 15; bit >= 0; bit--)
975 if (version & (1<<bit))