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.23 2004/09/23 11:50:03 asmodai 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>
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;
64 static struct cdevsw ad_cdevsw = {
74 /* write */ physwrite,
78 /* strategy */ adstrategy,
84 static void ad_requeue(struct ata_channel *, struct ad_request *);
85 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
86 static int ad_tagsupported(struct ad_softc *);
87 static void ad_timeout(struct ad_request *);
88 static void ad_free(struct ad_request *);
89 static int ad_version(u_int16_t);
92 #define AD_MAX_RETRIES 3
95 static u_int32_t adp_lun_map = 0;
96 static int ata_dma = 1;
97 static int ata_wc = 1;
98 static int ata_tags = 0;
99 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
100 TUNABLE_INT("hw.ata.wc", &ata_wc);
101 TUNABLE_INT("hw.ata.tags", &ata_tags);
102 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
105 SYSCTL_DECL(_hw_ata);
106 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
107 "ATA disk DMA mode control");
108 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
109 "ATA disk write caching");
110 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
111 "ATA disk tagged queuing support");
114 ad_attach(struct ata_device *atadev, int alreadylocked)
116 struct ad_softc *adp;
119 adp = malloc(sizeof(struct ad_softc), M_AD, M_WAITOK | M_ZERO);
121 KKASSERT(atadev->channel->req_mpipe.max_count != 0);
123 adp->device = atadev;
125 adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
127 adp->lun = ata_get_lun(&adp_lun_map);
129 ata_set_name(atadev, "ad", adp->lun);
130 adp->heads = atadev->param->heads;
131 adp->sectors = atadev->param->sectors;
132 adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;
133 bufq_init(&adp->queue);
135 /* does this device need oldstyle CHS addressing */
136 if (!ad_version(atadev->param->version_major) ||
137 !(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
138 adp->flags |= AD_F_CHS_USED;
140 /* use the 28bit LBA size if valid */
141 if (atadev->param->cylinders == 16383 &&
142 adp->total_secs < atadev->param->lba_size)
143 adp->total_secs = atadev->param->lba_size;
145 /* use the 48bit LBA size if valid */
146 if (atadev->param->support.address48 &&
147 atadev->param->lba_size48 > 268435455)
148 adp->total_secs = atadev->param->lba_size48;
151 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
152 /* use multiple sectors/interrupt if device supports it */
153 adp->transfersize = DEV_BSIZE;
154 if (ad_version(atadev->param->version_major)) {
155 int secsperint = max(1, min(atadev->param->sectors_intr, 16));
157 if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
158 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
159 adp->transfersize *= secsperint;
162 /* enable read caching if not default on device */
163 if (ata_command(atadev, ATA_C_SETFEATURES,
164 0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
165 ata_prtdev(atadev, "enabling readahead cache failed\n");
167 /* enable write caching if allowed and not default on device */
168 if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
169 if (ata_command(atadev, ATA_C_SETFEATURES,
170 0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
171 ata_prtdev(atadev, "enabling write cache failed\n");
174 if (ata_command(atadev, ATA_C_SETFEATURES,
175 0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
176 ata_prtdev(atadev, "disabling write cache failed\n");
179 /* use DMA if allowed and if drive/controller supports it */
181 ata_dmainit(atadev, ata_pmode(atadev->param),
182 ata_wmode(atadev->param), ata_umode(atadev->param));
184 ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
186 /* use tagged queueing if allowed and supported */
187 if (ata_tags && ad_tagsupported(adp)) {
188 adp->num_tags = atadev->param->queuelen;
189 adp->flags |= AD_F_TAG_ENABLED;
190 adp->device->channel->flags |= ATA_QUEUED;
191 if (ata_command(atadev, ATA_C_SETFEATURES,
192 0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
193 ata_prtdev(atadev, "disabling release interrupt failed\n");
194 if (ata_command(atadev, ATA_C_SETFEATURES,
195 0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
196 ata_prtdev(atadev, "disabling service interrupt failed\n");
199 ATA_UNLOCK_CH(atadev->channel);
201 devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
202 DEVSTAT_NO_ORDERED_TAGS,
203 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
204 DEVSTAT_PRIORITY_DISK);
206 dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw);
208 dev->si_iosize_max = 256 * DEV_BSIZE;
211 /* construct the disklabel */
212 bzero(&adp->disk.d_label, sizeof(struct disklabel));
213 adp->disk.d_label.d_secsize = DEV_BSIZE;
214 adp->disk.d_label.d_nsectors = adp->sectors;
215 adp->disk.d_label.d_ntracks = adp->heads;
216 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
217 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
218 adp->disk.d_label.d_secperunit = adp->total_secs;
220 atadev->driver = adp;
223 /* if this disk belongs to an ATA RAID dont print the probe */
224 if (ata_raiddisk_attach(adp))
225 adp->flags |= AD_F_RAID_SUBDISK;
227 if (atadev->driver) {
229 ata_enclosure_print(atadev);
235 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
237 struct ad_softc *adp = atadev->driver;
238 struct ad_request *request;
241 atadev->flags |= ATA_D_DETACHING;
242 ata_prtdev(atadev, "removed from configuration\n");
243 ad_invalidatequeue(adp, NULL);
244 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
245 if (request->softc != adp)
247 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
248 request->bp->b_error = ENXIO;
249 request->bp->b_flags |= B_ERROR;
250 biodone(request->bp);
254 while ((bp = bufq_first(&adp->queue))) {
255 bufq_remove(&adp->queue, bp);
257 bp->b_flags |= B_ERROR;
260 disk_invalidate(&adp->disk);
261 devstat_remove_entry(&adp->stats);
262 disk_destroy(&adp->disk);
264 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
265 ata_prtdev(atadev, "flushing cache on detach failed\n");
267 if (adp->flags & AD_F_RAID_SUBDISK)
268 ata_raiddisk_detach(adp);
269 ata_free_name(atadev);
270 ata_free_lun(&adp_lun_map, adp->lun);
271 atadev->driver = NULL;
277 adopen(dev_t dev, int flags, int fmt, struct thread *td)
279 struct ad_softc *adp = dev->si_drv1;
281 if (adp->flags & AD_F_RAID_SUBDISK)
287 adclose(dev_t dev, int flags, int fmt, struct thread *td)
289 struct ad_softc *adp = dev->si_drv1;
292 s = splbio(); /* interlock non-atomic channel lock */
293 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
294 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
295 ata_prtdev(adp->device, "flushing cache on close failed\n");
296 ATA_UNLOCK_CH(adp->device->channel);
302 * note: always use the passed device rather then bp->b_dev, as the bp
303 * may have been translated through several layers.
306 adstrategy(struct buf *bp)
308 struct ad_softc *adp = bp->b_dev->si_drv1;
311 if (adp->device->flags & ATA_D_DETACHING) {
313 bp->b_flags |= B_ERROR;
318 bufqdisksort(&adp->queue, bp);
320 ata_start(adp->device->channel);
324 addump(dev_t dev, u_int count, u_int blkno, u_int secsize)
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, secsize);
346 if ((count / blkcnt) < dumppages)
347 dumppages = 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 = 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)count * DEV_BSIZE) < 0)
376 blkno += blkcnt * dumppages;
377 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 ad_start(struct ata_device *atadev)
389 struct ad_softc *adp = atadev->driver;
390 struct buf *bp = bufq_first(&adp->queue);
391 struct ad_request *request;
397 /* if tagged queueing enabled get next free tag */
398 if (adp->flags & AD_F_TAG_ENABLED) {
399 while (tag <= adp->num_tags && adp->tags[tag])
401 if (tag > adp->num_tags )
406 * Allocate a request. The allocation can only fail if the pipeline
407 * is full, in which case the request will be picked up later when
408 * ad_start() is called after another request completes.
410 request = mpipe_alloc_nowait(&atadev->channel->req_mpipe);
411 if (request == NULL) {
412 ata_prtdev(atadev, "pipeline full allocating request in ad_start\n");
417 request->softc = adp;
419 request->blockaddr = bp->b_pblkno;
420 request->bytecount = bp->b_bcount;
421 request->data = bp->b_data;
423 callout_init(&request->callout);
424 if (bp->b_flags & B_READ)
425 request->flags |= ADR_F_READ;
426 if (adp->device->mode >= ATA_DMA) {
427 if (ata_dmaalloc(atadev, M_NOWAIT) != 0) {
428 mpipe_free(&atadev->channel->req_mpipe, request);
429 ata_prtdev(atadev, "pipeline full allocated dmabuf in ad_start\n");
430 /* do not revert to PIO, wait for ad_start after I/O completion */
435 /* insert in tag array */
436 adp->tags[tag] = request;
438 /* remove from drive queue */
439 bufq_remove(&adp->queue, bp);
441 /* link onto controller queue */
442 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
446 ad_requeue(struct ata_channel *chan, struct ad_request *req)
448 if (req->donecount) {
450 "WARNING: resetting donecount %u for retry\n",
452 req->bytecount += req->donecount;
455 TAILQ_INSERT_HEAD(&chan->ata_queue, req, chain);
459 ad_transfer(struct ad_request *request)
461 struct ad_softc *adp;
463 u_int32_t count, max_count;
465 int flags = ATA_IMMEDIATE;
467 /* get request params */
468 adp = request->softc;
470 /* calculate transfer details */
471 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
473 if (request->donecount == 0) {
475 /* start timeout for this transfer */
477 callout_stop(&request->callout);
479 callout_reset(&request->callout, 10 * hz,
480 (void *)ad_timeout, request);
483 /* setup transfer parameters */
484 count = howmany(request->bytecount, DEV_BSIZE);
485 max_count = adp->device->param->support.address48 ? 65536 : 256;
486 if (count > max_count) {
487 ata_prtdev(adp->device,
488 "count %d size transfers not supported\n", count);
492 if (adp->flags & AD_F_CHS_USED) {
493 int sector = (lba % adp->sectors) + 1;
494 int cylinder = lba / (adp->sectors * adp->heads);
495 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
497 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
498 adp->device->flags |= ATA_D_USE_CHS;
501 /* setup first transfer length */
502 request->currentsize = min(request->bytecount, adp->transfersize);
504 devstat_start_transaction(&adp->stats);
506 /* does this drive & transfer work with DMA ? */
507 request->flags &= ~ADR_F_DMA_USED;
508 if (adp->device->mode >= ATA_DMA &&
509 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
510 request->flags |= ADR_F_DMA_USED;
511 request->currentsize = request->bytecount;
513 /* do we have tags enabled ? */
514 if (adp->flags & AD_F_TAG_ENABLED) {
515 cmd = (request->flags & ADR_F_READ) ?
516 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
518 if (ata_command(adp->device, cmd, lba,
519 request->tag << 3, count, flags)) {
520 ata_prtdev(adp->device, "error executing command");
521 goto transfer_failed;
523 if (ata_wait(adp->device, ATA_S_READY)) {
524 ata_prtdev(adp->device, "timeout waiting for READY\n");
525 goto transfer_failed;
529 /* if ATA bus RELEASE check for SERVICE */
530 if (adp->flags & AD_F_TAG_ENABLED &&
531 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
533 return ad_service(adp, 1);
536 cmd = (request->flags & ADR_F_READ) ?
537 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
539 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
540 ata_prtdev(adp->device, "error executing command");
541 goto transfer_failed;
545 * wait for data transfer phase
547 * well this should be here acording to specs, but older
548 * promise controllers doesn't like it, they lockup!
550 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
551 ata_prtdev(adp->device, "timeout waiting for data phase\n");
552 goto transfer_failed;
557 /* start transfer, return and wait for interrupt */
558 ata_dmastart(adp->device, request->data, request->bytecount,
559 request->flags & ADR_F_READ);
560 return ATA_OP_CONTINUES;
563 /* does this drive support multi sector transfers ? */
564 if (request->currentsize > DEV_BSIZE)
565 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
567 /* just plain old single sector transfer */
569 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
571 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
572 ata_prtdev(adp->device, "error executing command");
573 goto transfer_failed;
577 /* calculate this transfer length */
578 request->currentsize = min(request->bytecount, adp->transfersize);
580 /* if this is a PIO read operation, return and wait for interrupt */
581 if (request->flags & ADR_F_READ)
582 return ATA_OP_CONTINUES;
584 /* ready to write PIO data ? */
585 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
586 ata_prtdev(adp->device, "timeout waiting for DRQ");
587 goto transfer_failed;
590 /* output the data */
591 if (adp->device->channel->flags & ATA_USE_16BIT)
592 ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
593 (void *)((uintptr_t)request->data + request->donecount),
594 request->currentsize / sizeof(int16_t));
596 ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
597 (void *)((uintptr_t)request->data + request->donecount),
598 request->currentsize / sizeof(int32_t));
599 return ATA_OP_CONTINUES;
602 callout_stop(&request->callout);
603 ad_invalidatequeue(adp, request);
604 printf(" - resetting\n");
606 /* if retries still permit, reinject this request */
607 if (request->retries++ < AD_MAX_RETRIES)
608 ad_requeue(adp->device->channel, request);
610 /* retries all used up, return error */
611 request->bp->b_error = EIO;
612 request->bp->b_flags |= B_ERROR;
613 request->bp->b_resid = request->bytecount;
614 devstat_end_transaction_buf(&adp->stats, request->bp);
615 biodone(request->bp);
618 ata_reinit(adp->device->channel);
619 return ATA_OP_CONTINUES;
623 ad_interrupt(struct ad_request *request)
625 struct ad_softc *adp = request->softc;
628 /* finish DMA transfer */
629 if (request->flags & ADR_F_DMA_USED)
630 dma_stat = ata_dmadone(adp->device);
632 /* do we have a corrected soft error ? */
633 if (adp->device->channel->status & ATA_S_CORR)
634 diskerr(request->bp, request->bp->b_dev,
635 "soft error (ECC corrected)", LOG_PRINTF,
636 request->blockaddr + (request->donecount / DEV_BSIZE),
639 /* did any real errors happen ? */
640 if ((adp->device->channel->status & ATA_S_ERROR) ||
641 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
642 adp->device->channel->error =
643 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
644 diskerr(request->bp, request->bp->b_dev,
645 (adp->device->channel->error & ATA_E_ICRC) ?
646 "UDMA ICRC error" : "hard error", LOG_PRINTF,
647 request->blockaddr + (request->donecount / DEV_BSIZE),
650 /* if this is a UDMA CRC error, reinject request */
651 if (request->flags & ADR_F_DMA_USED &&
652 adp->device->channel->error & ATA_E_ICRC) {
653 callout_stop(&request->callout);
654 ad_invalidatequeue(adp, request);
656 if (request->retries++ < AD_MAX_RETRIES)
657 printf(" retrying\n");
659 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
660 printf(" falling back to PIO mode\n");
662 ad_requeue(adp->device->channel, request);
663 return ATA_OP_FINISHED;
666 /* if using DMA, try once again in PIO mode */
667 if (request->flags & ADR_F_DMA_USED) {
668 callout_stop(&request->callout);
669 ad_invalidatequeue(adp, request);
670 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
671 request->flags |= ADR_F_FORCE_PIO;
672 printf(" trying PIO mode\n");
673 ad_requeue(adp->device->channel, request);
674 return ATA_OP_FINISHED;
677 request->flags |= ADR_F_ERROR;
678 printf(" status=%02x error=%02x\n",
679 adp->device->channel->status, adp->device->channel->error);
682 /* if we arrived here with forced PIO mode, DMA doesn't work right */
683 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
684 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
686 /* if this was a PIO read operation, get the data */
687 if (!(request->flags & ADR_F_DMA_USED) &&
688 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
690 /* ready to receive data? */
691 if ((adp->device->channel->status & ATA_S_READY) == 0)
692 ata_prtdev(adp->device, "read interrupt arrived early");
694 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
695 ata_prtdev(adp->device, "read error detected (too) late");
696 request->flags |= ADR_F_ERROR;
699 /* data ready, read in */
700 if (adp->device->channel->flags & ATA_USE_16BIT)
701 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
702 (void*)((uintptr_t)request->data + request->donecount),
703 request->currentsize / sizeof(int16_t));
705 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
706 (void*)((uintptr_t)request->data + request->donecount),
707 request->currentsize / sizeof(int32_t));
711 /* finish up transfer */
712 if (request->flags & ADR_F_ERROR) {
713 request->bp->b_error = EIO;
714 request->bp->b_flags |= B_ERROR;
717 request->bytecount -= request->currentsize;
718 request->donecount += request->currentsize;
719 if (request->bytecount > 0) {
720 ad_transfer(request);
721 return ATA_OP_CONTINUES;
725 /* disarm timeout for this transfer */
726 callout_stop(&request->callout);
728 request->bp->b_resid = request->bytecount;
730 devstat_end_transaction_buf(&adp->stats, request->bp);
731 biodone(request->bp);
735 /* check for SERVICE (tagged operations only) */
736 return ad_service(adp, 1);
740 ad_service(struct ad_softc *adp, int change)
742 /* do we have to check the other device on this channel ? */
743 if (adp->device->channel->flags & ATA_QUEUED && change) {
744 int device = adp->device->unit;
746 if (adp->device->unit == ATA_MASTER) {
747 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
748 (adp->device->channel->device[SLAVE].driver) &&
749 ((struct ad_softc *) (adp->device->channel->
750 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
754 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
755 (adp->device->channel->device[MASTER].driver) &&
756 ((struct ad_softc *) (adp->device->channel->
757 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
760 if (device != adp->device->unit &&
762 (adp->device->channel->
763 device[ATA_DEV(device)].driver))->outstanding > 0) {
764 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
765 adp = adp->device->channel->device[ATA_DEV(device)].driver;
769 adp->device->channel->status =
770 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
772 /* do we have a SERVICE request from the drive ? */
773 if (adp->flags & AD_F_TAG_ENABLED &&
774 adp->outstanding > 0 &&
775 adp->device->channel->status & ATA_S_SERVICE) {
776 struct ad_request *request;
779 /* check for error */
780 if (adp->device->channel->status & ATA_S_ERROR) {
781 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
782 adp->device->channel->status,
783 adp->device->channel->error);
784 ad_invalidatequeue(adp, NULL);
785 return ATA_OP_FINISHED;
788 /* issue SERVICE cmd */
789 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
790 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
791 ad_invalidatequeue(adp, NULL);
792 return ATA_OP_FINISHED;
795 /* setup the transfer environment when ready */
796 if (ata_wait(adp->device, ATA_S_READY)) {
797 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
798 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
799 adp->device->channel->status,
800 adp->device->channel->error);
801 ad_invalidatequeue(adp, NULL);
802 return ATA_OP_FINISHED;
804 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
805 if (!(request = adp->tags[tag])) {
806 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
807 ad_invalidatequeue(adp, NULL);
808 return ATA_OP_FINISHED;
810 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
811 adp->device->channel->running = request;
814 /* start DMA transfer when ready */
815 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
816 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
817 adp->device->channel->status,
818 adp->device->channel->error);
819 ad_invalidatequeue(adp, NULL);
820 return ATA_OP_FINISHED;
822 ata_dmastart(adp->device, request->data, request->bytecount,
823 request->flags & ADR_F_READ);
824 return ATA_OP_CONTINUES;
826 return ATA_OP_FINISHED;
830 ad_free(struct ad_request *request)
833 ata_dmafree(request->softc->device);
834 request->softc->tags[request->tag] = NULL;
835 mpipe_free(&request->softc->device->channel->req_mpipe, request);
840 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
842 /* if tags used invalidate all other tagged transfers */
843 if (adp->flags & AD_F_TAG_ENABLED) {
844 struct ad_request *tmpreq;
847 ata_prtdev(adp->device, "invalidating queued requests\n");
848 for (tag = 0; tag <= adp->num_tags; tag++) {
849 tmpreq = adp->tags[tag];
850 adp->tags[tag] = NULL;
851 if (tmpreq == request || tmpreq == NULL)
853 callout_stop(&request->callout);
854 ad_requeue(adp->device->channel, tmpreq);
856 if (ata_command(adp->device, ATA_C_NOP,
857 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
858 ata_prtdev(adp->device, "flush queue failed\n");
859 adp->outstanding = 0;
864 ad_tagsupported(struct ad_softc *adp)
866 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
869 switch (adp->device->channel->chiptype) {
870 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
877 /* check that drive does DMA, has tags enabled, and is one we know works */
878 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
879 adp->device->param->enabled.queued) {
880 while (good[i] != NULL) {
881 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
886 * check IBM's new obscure way of naming drives
887 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
888 * but doesn't care about the other info (size, capacity etc)
890 if (!strncmp(adp->device->param->model, "IC", 2) &&
891 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
892 !strncmp(adp->device->param->model + 8, "AV", 2)))
899 ad_timeout(struct ad_request *request)
901 struct ad_softc *adp = request->softc;
903 adp->device->channel->running = NULL;
904 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
905 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
906 request->tag, request->serv);
908 if (request->flags & ADR_F_DMA_USED) {
909 ata_dmadone(adp->device);
910 ad_invalidatequeue(adp, request);
911 if (request->retries == AD_MAX_RETRIES) {
912 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
913 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
914 request->retries = 0;
918 /* if retries still permit, reinject this request */
919 if (request->retries++ < AD_MAX_RETRIES) {
920 ad_requeue(adp->device->channel, request);
923 /* retries all used up, return error */
924 request->bp->b_error = EIO;
925 request->bp->b_flags |= B_ERROR;
926 devstat_end_transaction_buf(&adp->stats, request->bp);
927 biodone(request->bp);
930 ata_reinit(adp->device->channel);
934 ad_reinit(struct ata_device *atadev)
936 struct ad_softc *adp = atadev->driver;
938 /* reinit disk parameters */
939 ad_invalidatequeue(atadev->driver, NULL);
940 ata_command(atadev, ATA_C_SET_MULTI, 0,
941 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
942 if (adp->device->mode >= ATA_DMA)
943 ata_dmainit(atadev, ata_pmode(adp->device->param),
944 ata_wmode(adp->device->param),
945 ata_umode(adp->device->param));
947 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
951 ad_print(struct ad_softc *adp)
954 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
955 adp->device->param->model, adp->device->param->revision,
956 ad_version(adp->device->param->version_major),
957 device_get_unit(adp->device->channel->dev),
958 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
960 ata_prtdev(adp->device,
961 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
962 (unsigned long long)(adp->total_secs /
963 ((1024L*1024L)/DEV_BSIZE)),
964 (unsigned long long) adp->total_secs,
965 (unsigned long long) (adp->total_secs /
966 (adp->heads * adp->sectors)),
967 adp->heads, adp->sectors, DEV_BSIZE);
969 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
970 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
971 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
972 ata_mode2str(adp->device->mode));
974 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
975 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
976 ata_umode(adp->device->param),
977 adp->device->param->hwres_cblid);
981 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
982 (unsigned long long)(adp->total_secs /
983 ((1024L * 1024L) / DEV_BSIZE)),
984 adp->device->param->model,
985 (unsigned long long)(adp->total_secs /
986 (adp->heads*adp->sectors)),
987 adp->heads, adp->sectors,
988 device_get_unit(adp->device->channel->dev),
989 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
990 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
991 ata_mode2str(adp->device->mode));
995 ad_version(u_int16_t version)
999 if (version == 0xffff)
1001 for (bit = 15; bit >= 0; bit--)
1002 if (version & (1<<bit))