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.4 2003/06/23 17:55:29 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>
58 /* device structures */
59 static d_open_t adopen;
60 static d_close_t adclose;
61 static d_strategy_t adstrategy;
62 static d_dump_t addump;
63 static struct cdevsw ad_cdevsw = {
67 /* write */ physwrite,
71 /* strategy */ adstrategy,
79 static struct cdevsw addisk_cdevsw;
82 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
83 static int ad_tagsupported(struct ad_softc *);
84 static void ad_timeout(struct ad_request *);
85 static void ad_free(struct ad_request *);
86 static int ad_version(u_int16_t);
89 #define AD_MAX_RETRIES 3
92 static u_int32_t adp_lun_map = 0;
93 static int ata_dma = 1;
94 static int ata_wc = 1;
95 static int ata_tags = 0;
96 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
97 TUNABLE_INT("hw.ata.wc", &ata_wc);
98 TUNABLE_INT("hw.ata.tags", &ata_tags);
99 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
102 SYSCTL_DECL(_hw_ata);
103 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
104 "ATA disk DMA mode control");
105 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
106 "ATA disk write caching");
107 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
108 "ATA disk tagged queuing support");
111 ad_attach(struct ata_device *atadev)
113 struct ad_softc *adp;
116 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
117 ata_prtdev(atadev, "failed to allocate driver storage\n");
120 adp->device = atadev;
122 adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
124 adp->lun = ata_get_lun(&adp_lun_map);
126 ata_set_name(atadev, "ad", adp->lun);
127 adp->heads = atadev->param->heads;
128 adp->sectors = atadev->param->sectors;
129 adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;
130 bufq_init(&adp->queue);
132 /* does this device need oldstyle CHS addressing */
133 if (!ad_version(atadev->param->version_major) ||
134 !(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
135 adp->flags |= AD_F_CHS_USED;
137 /* use the 28bit LBA size if valid */
138 if (atadev->param->cylinders == 16383 &&
139 adp->total_secs < atadev->param->lba_size)
140 adp->total_secs = atadev->param->lba_size;
142 /* use the 48bit LBA size if valid */
143 if (atadev->param->support.address48 &&
144 atadev->param->lba_size48 > 268435455)
145 adp->total_secs = atadev->param->lba_size48;
147 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
148 /* use multiple sectors/interrupt if device supports it */
149 adp->transfersize = DEV_BSIZE;
150 if (ad_version(atadev->param->version_major)) {
151 int secsperint = max(1, min(atadev->param->sectors_intr, 16));
153 if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
154 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
155 adp->transfersize *= secsperint;
158 /* enable read caching if not default on device */
159 if (ata_command(atadev, ATA_C_SETFEATURES,
160 0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
161 ata_prtdev(atadev, "enabling readahead cache failed\n");
163 /* enable write caching if allowed and not default on device */
164 if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
165 if (ata_command(atadev, ATA_C_SETFEATURES,
166 0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
167 ata_prtdev(atadev, "enabling write cache failed\n");
170 if (ata_command(atadev, ATA_C_SETFEATURES,
171 0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
172 ata_prtdev(atadev, "disabling write cache failed\n");
175 /* use DMA if allowed and if drive/controller supports it */
177 ata_dmainit(atadev->channel, atadev->unit, ata_pmode(atadev->param),
178 ata_wmode(atadev->param), ata_umode(atadev->param));
180 ata_dmainit(atadev->channel, atadev->unit,
181 ata_pmode(atadev->param), -1, -1);
183 /* use tagged queueing if allowed and supported */
184 if (ata_tags && ad_tagsupported(adp)) {
185 adp->num_tags = atadev->param->queuelen;
186 adp->flags |= AD_F_TAG_ENABLED;
187 adp->device->channel->flags |= ATA_QUEUED;
188 if (ata_command(atadev, ATA_C_SETFEATURES,
189 0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
190 ata_prtdev(atadev, "disabling release interrupt failed\n");
191 if (ata_command(atadev, ATA_C_SETFEATURES,
192 0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
193 ata_prtdev(atadev, "disabling service interrupt failed\n");
196 ATA_UNLOCK_CH(atadev->channel);
198 devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
199 DEVSTAT_NO_ORDERED_TAGS,
200 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
201 DEVSTAT_PRIORITY_DISK);
203 dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw, &addisk_cdevsw);
205 dev->si_iosize_max = 256 * DEV_BSIZE;
208 /* construct the disklabel */
209 bzero(&adp->disk.d_label, sizeof(struct disklabel));
210 adp->disk.d_label.d_secsize = DEV_BSIZE;
211 adp->disk.d_label.d_nsectors = adp->sectors;
212 adp->disk.d_label.d_ntracks = adp->heads;
213 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
214 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
215 adp->disk.d_label.d_secperunit = adp->total_secs;
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;
238 atadev->flags |= ATA_D_DETACHING;
239 ata_prtdev(atadev, "removed from configuration\n");
240 ad_invalidatequeue(adp, NULL);
241 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
242 if (request->softc != adp)
244 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
245 request->bp->b_error = ENXIO;
246 request->bp->b_flags |= B_ERROR;
247 biodone(request->bp);
250 while ((bp = bufq_first(&adp->queue))) {
251 bufq_remove(&adp->queue, bp);
253 bp->b_flags |= B_ERROR;
256 disk_invalidate(&adp->disk);
257 disk_destroy(adp->dev);
258 devstat_remove_entry(&adp->stats);
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(dev_t dev, int flags, int fmt, struct thread *td)
275 struct ad_softc *adp = dev->si_drv1;
277 if (adp->flags & AD_F_RAID_SUBDISK)
283 adclose(dev_t dev, int flags, int fmt, struct thread *td)
285 struct ad_softc *adp = dev->si_drv1;
287 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
288 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
289 ata_prtdev(adp->device, "flushing cache on close failed\n");
290 ATA_UNLOCK_CH(adp->device->channel);
295 adstrategy(struct buf *bp)
297 struct ad_softc *adp = bp->b_dev->si_drv1;
300 if (adp->device->flags & ATA_D_DETACHING) {
302 bp->b_flags |= B_ERROR;
307 bufqdisksort(&adp->queue, bp);
309 ata_start(adp->device->channel);
315 struct ad_softc *adp = dev->si_drv1;
316 struct ad_request request;
317 u_int count, blkno, secsize;
318 vm_offset_t addr = 0;
320 int dumppages = MAXDUMPPGS;
324 if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
330 /* force PIO mode for dumps */
331 adp->device->mode = ATA_PIO;
332 ata_reinit(adp->device->channel);
334 blkcnt = howmany(PAGE_SIZE, secsize);
340 if ((count / blkcnt) < dumppages)
341 dumppages = count / blkcnt;
343 for (i = 0; i < dumppages; ++i) {
344 vm_offset_t a = addr + (i * PAGE_SIZE);
345 if (is_physical_memory(a))
346 va = pmap_kenter_temporary(trunc_page(a), i);
348 va = pmap_kenter_temporary(trunc_page(0), i);
351 bzero(&request, sizeof(struct ad_request));
353 request.blockaddr = blkno;
354 request.bytecount = PAGE_SIZE * dumppages;
357 while (request.bytecount > 0) {
358 ad_transfer(&request);
359 if (request.flags & ADR_F_ERROR)
361 request.donecount += request.currentsize;
362 request.bytecount -= request.currentsize;
366 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
369 blkno += blkcnt * dumppages;
370 count -= blkcnt * dumppages;
371 addr += PAGE_SIZE * dumppages;
374 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
375 ata_prtdev(adp->device, "timeout waiting for final ready\n");
380 ad_start(struct ata_device *atadev)
382 struct ad_softc *adp = atadev->driver;
383 struct buf *bp = bufq_first(&adp->queue);
384 struct ad_request *request;
390 /* if tagged queueing enabled get next free tag */
391 if (adp->flags & AD_F_TAG_ENABLED) {
392 while (tag <= adp->num_tags && adp->tags[tag])
394 if (tag > adp->num_tags )
398 if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
399 ata_prtdev(atadev, "out of memory in start\n");
404 request->softc = adp;
406 request->blockaddr = bp->b_pblkno;
407 request->bytecount = bp->b_bcount;
408 request->data = bp->b_data;
410 if (bp->b_flags & B_READ)
411 request->flags |= ADR_F_READ;
412 if (adp->device->mode >= ATA_DMA) {
413 if (!(request->dmatab = ata_dmaalloc(atadev->channel, atadev->unit)))
414 adp->device->mode = ATA_PIO;
417 /* insert in tag array */
418 adp->tags[tag] = request;
420 /* remove from drive queue */
421 bufq_remove(&adp->queue, bp);
423 /* link onto controller queue */
424 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
428 ad_transfer(struct ad_request *request)
430 struct ad_softc *adp;
432 u_int32_t count, max_count;
434 int flags = ATA_IMMEDIATE;
436 /* get request params */
437 adp = request->softc;
439 /* calculate transfer details */
440 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
442 if (request->donecount == 0) {
444 /* start timeout for this transfer */
446 request->timeout_handle.callout = NULL;
448 request->timeout_handle =
449 timeout((timeout_t*)ad_timeout, request, 10 * hz);
451 /* setup transfer parameters */
452 count = howmany(request->bytecount, DEV_BSIZE);
453 max_count = adp->device->param->support.address48 ? 65536 : 256;
454 if (count > max_count) {
455 ata_prtdev(adp->device,
456 "count %d size transfers not supported\n", count);
460 if (adp->flags & AD_F_CHS_USED) {
461 int sector = (lba % adp->sectors) + 1;
462 int cylinder = lba / (adp->sectors * adp->heads);
463 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
465 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
466 adp->device->flags |= ATA_D_USE_CHS;
469 /* setup first transfer length */
470 request->currentsize = min(request->bytecount, adp->transfersize);
472 devstat_start_transaction(&adp->stats);
474 /* does this drive & transfer work with DMA ? */
475 request->flags &= ~ADR_F_DMA_USED;
476 if (adp->device->mode >= ATA_DMA &&
477 !ata_dmasetup(adp->device->channel, adp->device->unit,
478 request->dmatab, request->data, request->bytecount)) {
479 request->flags |= ADR_F_DMA_USED;
480 request->currentsize = request->bytecount;
482 /* do we have tags enabled ? */
483 if (adp->flags & AD_F_TAG_ENABLED) {
484 cmd = (request->flags & ADR_F_READ) ?
485 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
487 if (ata_command(adp->device, cmd, lba,
488 request->tag << 3, count, flags)) {
489 ata_prtdev(adp->device, "error executing command");
490 goto transfer_failed;
492 if (ata_wait(adp->device, ATA_S_READY)) {
493 ata_prtdev(adp->device, "timeout waiting for READY\n");
494 goto transfer_failed;
498 /* if ATA bus RELEASE check for SERVICE */
499 if (adp->flags & AD_F_TAG_ENABLED &&
500 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
502 return ad_service(adp, 1);
505 cmd = (request->flags & ADR_F_READ) ?
506 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
508 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
509 ata_prtdev(adp->device, "error executing command");
510 goto transfer_failed;
514 * wait for data transfer phase
516 * well this should be here acording to specs, but older
517 * promise controllers doesn't like it, they lockup!
519 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
520 ata_prtdev(adp->device, "timeout waiting for data phase\n");
521 goto transfer_failed;
526 /* start transfer, return and wait for interrupt */
527 ata_dmastart(adp->device->channel, adp->device->unit,
528 request->dmatab, request->flags & ADR_F_READ);
529 return ATA_OP_CONTINUES;
532 /* does this drive support multi sector transfers ? */
533 if (request->currentsize > DEV_BSIZE)
534 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
536 /* just plain old single sector transfer */
538 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
540 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
541 ata_prtdev(adp->device, "error executing command");
542 goto transfer_failed;
546 /* calculate this transfer length */
547 request->currentsize = min(request->bytecount, adp->transfersize);
549 /* if this is a PIO read operation, return and wait for interrupt */
550 if (request->flags & ADR_F_READ)
551 return ATA_OP_CONTINUES;
553 /* ready to write PIO data ? */
554 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
555 ata_prtdev(adp->device, "timeout waiting for DRQ");
556 goto transfer_failed;
559 /* output the data */
560 if (adp->device->channel->flags & ATA_USE_16BIT)
561 ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
562 (void *)((uintptr_t)request->data + request->donecount),
563 request->currentsize / sizeof(int16_t));
565 ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
566 (void *)((uintptr_t)request->data + request->donecount),
567 request->currentsize / sizeof(int32_t));
568 return ATA_OP_CONTINUES;
571 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
572 ad_invalidatequeue(adp, request);
573 printf(" - resetting\n");
575 /* if retries still permit, reinject this request */
576 if (request->retries++ < AD_MAX_RETRIES)
577 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
579 /* retries all used up, return error */
580 request->bp->b_error = EIO;
581 request->bp->b_flags |= B_ERROR;
582 request->bp->b_resid = request->bytecount;
583 devstat_end_transaction_buf(&adp->stats, request->bp);
584 biodone(request->bp);
587 ata_reinit(adp->device->channel);
588 return ATA_OP_CONTINUES;
592 ad_interrupt(struct ad_request *request)
594 struct ad_softc *adp = request->softc;
597 /* finish DMA transfer */
598 if (request->flags & ADR_F_DMA_USED)
599 dma_stat = ata_dmadone(adp->device->channel);
601 /* do we have a corrected soft error ? */
602 if (adp->device->channel->status & ATA_S_CORR)
603 diskerr(request->bp, "soft error (ECC corrected)", LOG_PRINTF,
604 request->blockaddr + (request->donecount / DEV_BSIZE),
607 /* did any real errors happen ? */
608 if ((adp->device->channel->status & ATA_S_ERROR) ||
609 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
610 adp->device->channel->error =
611 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
612 diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
613 "UDMA ICRC error" : "hard error", LOG_PRINTF,
614 request->blockaddr + (request->donecount / DEV_BSIZE),
617 /* if this is a UDMA CRC error, reinject request */
618 if (request->flags & ADR_F_DMA_USED &&
619 adp->device->channel->error & ATA_E_ICRC) {
620 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
621 ad_invalidatequeue(adp, request);
623 if (request->retries++ < AD_MAX_RETRIES)
624 printf(" retrying\n");
626 ata_dmainit(adp->device->channel, adp->device->unit,
627 ata_pmode(adp->device->param), -1, -1);
628 printf(" falling back to PIO mode\n");
630 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
631 return ATA_OP_FINISHED;
634 /* if using DMA, try once again in PIO mode */
635 if (request->flags & ADR_F_DMA_USED) {
636 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
637 ad_invalidatequeue(adp, request);
638 ata_dmainit(adp->device->channel, adp->device->unit,
639 ata_pmode(adp->device->param), -1, -1);
640 request->flags |= ADR_F_FORCE_PIO;
641 printf(" trying PIO mode\n");
642 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
643 return ATA_OP_FINISHED;
646 request->flags |= ADR_F_ERROR;
647 printf(" status=%02x error=%02x\n",
648 adp->device->channel->status, adp->device->channel->error);
651 /* if we arrived here with forced PIO mode, DMA doesn't work right */
652 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
653 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
655 /* if this was a PIO read operation, get the data */
656 if (!(request->flags & ADR_F_DMA_USED) &&
657 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
659 /* ready to receive data? */
660 if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
661 != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
662 ata_prtdev(adp->device, "read interrupt arrived early");
664 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
665 ata_prtdev(adp->device, "read error detected (too) late");
666 request->flags |= ADR_F_ERROR;
669 /* data ready, read in */
670 if (adp->device->channel->flags & ATA_USE_16BIT)
671 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
672 (void*)((uintptr_t)request->data + request->donecount),
673 request->currentsize / sizeof(int16_t));
675 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
676 (void*)((uintptr_t)request->data + request->donecount),
677 request->currentsize / sizeof(int32_t));
681 /* finish up transfer */
682 if (request->flags & ADR_F_ERROR) {
683 request->bp->b_error = EIO;
684 request->bp->b_flags |= B_ERROR;
687 request->bytecount -= request->currentsize;
688 request->donecount += request->currentsize;
689 if (request->bytecount > 0) {
690 ad_transfer(request);
691 return ATA_OP_CONTINUES;
695 /* disarm timeout for this transfer */
696 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
698 request->bp->b_resid = request->bytecount;
700 devstat_end_transaction_buf(&adp->stats, request->bp);
701 biodone(request->bp);
705 /* check for SERVICE (tagged operations only) */
706 return ad_service(adp, 1);
710 ad_service(struct ad_softc *adp, int change)
712 /* do we have to check the other device on this channel ? */
713 if (adp->device->channel->flags & ATA_QUEUED && change) {
714 int device = adp->device->unit;
716 if (adp->device->unit == ATA_MASTER) {
717 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
718 (adp->device->channel->device[SLAVE].driver) &&
719 ((struct ad_softc *) (adp->device->channel->
720 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
724 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
725 (adp->device->channel->device[MASTER].driver) &&
726 ((struct ad_softc *) (adp->device->channel->
727 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
730 if (device != adp->device->unit &&
732 (adp->device->channel->
733 device[ATA_DEV(device)].driver))->outstanding > 0) {
734 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
735 adp = adp->device->channel->device[ATA_DEV(device)].driver;
739 adp->device->channel->status =
740 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
742 /* do we have a SERVICE request from the drive ? */
743 if (adp->flags & AD_F_TAG_ENABLED &&
744 adp->outstanding > 0 &&
745 adp->device->channel->status & ATA_S_SERVICE) {
746 struct ad_request *request;
749 /* check for error */
750 if (adp->device->channel->status & ATA_S_ERROR) {
751 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
752 adp->device->channel->status,
753 adp->device->channel->error);
754 ad_invalidatequeue(adp, NULL);
755 return ATA_OP_FINISHED;
758 /* issue SERVICE cmd */
759 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
760 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
761 ad_invalidatequeue(adp, NULL);
762 return ATA_OP_FINISHED;
765 /* setup the transfer environment when ready */
766 if (ata_wait(adp->device, ATA_S_READY)) {
767 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
768 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
769 adp->device->channel->status,
770 adp->device->channel->error);
771 ad_invalidatequeue(adp, NULL);
772 return ATA_OP_FINISHED;
774 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
775 if (!(request = adp->tags[tag])) {
776 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
777 ad_invalidatequeue(adp, NULL);
778 return ATA_OP_FINISHED;
780 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
781 adp->device->channel->running = request;
784 /* start DMA transfer when ready */
785 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
786 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
787 adp->device->channel->status,
788 adp->device->channel->error);
789 ad_invalidatequeue(adp, NULL);
790 return ATA_OP_FINISHED;
792 ata_dmastart(adp->device->channel, adp->device->unit,
793 request->dmatab, request->flags & ADR_F_READ);
794 return ATA_OP_CONTINUES;
796 return ATA_OP_FINISHED;
800 ad_free(struct ad_request *request)
805 free(request->dmatab, M_DEVBUF);
806 request->softc->tags[request->tag] = NULL;
812 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
814 /* if tags used invalidate all other tagged transfers */
815 if (adp->flags & AD_F_TAG_ENABLED) {
816 struct ad_request *tmpreq;
819 ata_prtdev(adp->device, "invalidating queued requests\n");
820 for (tag = 0; tag <= adp->num_tags; tag++) {
821 tmpreq = adp->tags[tag];
822 adp->tags[tag] = NULL;
823 if (tmpreq == request || tmpreq == NULL)
825 untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
826 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
828 if (ata_command(adp->device, ATA_C_NOP,
829 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
830 ata_prtdev(adp->device, "flush queue failed\n");
831 adp->outstanding = 0;
836 ad_tagsupported(struct ad_softc *adp)
838 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
841 switch (adp->device->channel->chiptype) {
842 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
849 /* check that drive does DMA, has tags enabled, and is one we know works */
850 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
851 adp->device->param->enabled.queued) {
852 while (good[i] != NULL) {
853 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
858 * check IBM's new obscure way of naming drives
859 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
860 * but doesn't care about the other info (size, capacity etc)
862 if (!strncmp(adp->device->param->model, "IC", 2) &&
863 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
864 !strncmp(adp->device->param->model + 8, "AV", 2)))
871 ad_timeout(struct ad_request *request)
873 struct ad_softc *adp = request->softc;
875 adp->device->channel->running = NULL;
876 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
877 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
878 request->tag, request->serv);
880 if (request->flags & ADR_F_DMA_USED) {
881 ata_dmadone(adp->device->channel);
882 ad_invalidatequeue(adp, request);
883 if (request->retries == AD_MAX_RETRIES) {
884 ata_dmainit(adp->device->channel, adp->device->unit,
885 ata_pmode(adp->device->param), -1, -1);
886 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
887 request->retries = 0;
891 /* if retries still permit, reinject this request */
892 if (request->retries++ < AD_MAX_RETRIES) {
893 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
896 /* retries all used up, return error */
897 request->bp->b_error = EIO;
898 request->bp->b_flags |= B_ERROR;
899 devstat_end_transaction_buf(&adp->stats, request->bp);
900 biodone(request->bp);
903 ata_reinit(adp->device->channel);
907 ad_reinit(struct ata_device *atadev)
909 struct ad_softc *adp = atadev->driver;
911 /* reinit disk parameters */
912 ad_invalidatequeue(atadev->driver, NULL);
913 ata_command(atadev, ATA_C_SET_MULTI, 0,
914 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
915 if (adp->device->mode >= ATA_DMA)
916 ata_dmainit(atadev->channel, atadev->unit,
917 ata_pmode(adp->device->param),
918 ata_wmode(adp->device->param),
919 ata_umode(adp->device->param));
921 ata_dmainit(atadev->channel, atadev->unit,
922 ata_pmode(adp->device->param), -1, -1);
926 ad_print(struct ad_softc *adp)
929 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
930 adp->device->param->model, adp->device->param->revision,
931 ad_version(adp->device->param->version_major),
932 device_get_unit(adp->device->channel->dev),
933 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
935 ata_prtdev(adp->device,
936 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
937 (unsigned long long)(adp->total_secs /
938 ((1024L*1024L)/DEV_BSIZE)),
939 (unsigned long long) adp->total_secs,
940 (unsigned long long) (adp->total_secs /
941 (adp->heads * adp->sectors)),
942 adp->heads, adp->sectors, DEV_BSIZE);
944 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
945 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
946 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
947 ata_mode2str(adp->device->mode));
949 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
950 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
951 ata_umode(adp->device->param),
952 adp->device->param->hwres_cblid);
956 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
957 (unsigned long long)(adp->total_secs /
958 ((1024L * 1024L) / DEV_BSIZE)),
959 adp->device->param->model,
960 (unsigned long long)(adp->total_secs /
961 (adp->heads*adp->sectors)),
962 adp->heads, adp->sectors,
963 device_get_unit(adp->device->channel->dev),
964 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
965 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
966 ata_mode2str(adp->device->mode));
970 ad_version(u_int16_t version)
974 if (version == 0xffff)
976 for (bit = 15; bit >= 0; bit--)
977 if (version & (1<<bit))