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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
41 #include <sys/devicestat.h>
43 #include <sys/sysctl.h>
46 #include <machine/md_var.h>
47 #include <machine/bus.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-disk.h>
51 #include <dev/ata/ata-raid.h>
53 /* device structures */
54 static d_open_t adopen;
55 static d_close_t adclose;
56 static d_strategy_t adstrategy;
57 static d_dump_t addump;
58 static struct cdevsw ad_cdevsw = {
62 /* write */ physwrite,
66 /* strategy */ adstrategy,
73 static struct cdevsw addisk_cdevsw;
76 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
77 static int ad_tagsupported(struct ad_softc *);
78 static void ad_timeout(struct ad_request *);
79 static void ad_free(struct ad_request *);
80 static int ad_version(u_int16_t);
83 #define AD_MAX_RETRIES 3
86 static u_int32_t adp_lun_map = 0;
87 static int ata_dma = 1;
88 static int ata_wc = 1;
89 static int ata_tags = 0;
90 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
91 TUNABLE_INT("hw.ata.wc", &ata_wc);
92 TUNABLE_INT("hw.ata.tags", &ata_tags);
93 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
97 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
98 "ATA disk DMA mode control");
99 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
100 "ATA disk write caching");
101 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
102 "ATA disk tagged queuing support");
105 ad_attach(struct ata_device *atadev)
107 struct ad_softc *adp;
112 if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
113 ata_prtdev(atadev, "failed to allocate driver storage\n");
116 adp->device = atadev;
118 adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
120 adp->lun = ata_get_lun(&adp_lun_map);
122 ata_set_name(atadev, "ad", adp->lun);
123 adp->heads = atadev->param->heads;
124 adp->sectors = atadev->param->sectors;
125 adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;
126 adp->max_iosize = 256 * DEV_BSIZE;
127 bioq_init(&adp->queue);
129 lbasize = (u_int32_t)atadev->param->lba_size_1 |
130 ((u_int32_t)atadev->param->lba_size_2 << 16);
132 /* does this device need oldstyle CHS addressing */
133 if (!ad_version(atadev->param->version_major) ||
134 !(atadev->param->atavalid & ATA_FLAG_54_58) || !lbasize)
135 adp->flags |= AD_F_CHS_USED;
137 /* use the 28bit LBA size if valid */
138 if (atadev->param->cylinders == 16383 && adp->total_secs < lbasize)
139 adp->total_secs = lbasize;
141 lbasize48 = ((u_int64_t)atadev->param->lba_size48_1) |
142 ((u_int64_t)atadev->param->lba_size48_2 << 16) |
143 ((u_int64_t)atadev->param->lba_size48_3 << 32) |
144 ((u_int64_t)atadev->param->lba_size48_4 << 48);
146 /* use the 48bit LBA size if valid */
147 if (atadev->param->support.address48 && lbasize48 > 268435455)
148 adp->total_secs = lbasize48;
150 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, &addisk_cdevsw);
208 dev->si_iosize_max = adp->max_iosize;
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;
231 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
233 struct ad_softc *adp = atadev->driver;
234 struct ad_request *request;
237 atadev->flags |= ATA_D_DETACHING;
238 ata_prtdev(atadev, "removed from configuration\n");
239 ad_invalidatequeue(adp, NULL);
240 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
241 if (request->softc != adp)
243 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
244 biofinish(request->bp, NULL, ENXIO);
248 while ((bp = bioq_first(&adp->queue))) {
249 bioq_remove(&adp->queue, bp);
250 biofinish(bp, NULL, ENXIO);
252 disk_invalidate(&adp->disk);
253 disk_destroy(adp->dev);
254 devstat_remove_entry(&adp->stats);
256 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
257 ata_prtdev(atadev, "flushing cache on detach failed\n");
259 if (adp->flags & AD_F_RAID_SUBDISK)
260 ata_raiddisk_detach(adp);
261 ata_free_name(atadev);
262 ata_free_lun(&adp_lun_map, adp->lun);
263 atadev->driver = NULL;
269 adopen(dev_t dev, int flags, int fmt, struct thread *td)
271 struct ad_softc *adp = dev->si_drv1;
273 if (adp->flags & AD_F_RAID_SUBDISK)
279 adclose(dev_t dev, int flags, int fmt, struct thread *td)
281 struct ad_softc *adp = dev->si_drv1;
283 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
284 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
285 ata_prtdev(adp->device, "flushing cache on close failed\n");
286 ATA_UNLOCK_CH(adp->device->channel);
291 adstrategy(struct bio *bp)
293 struct ad_softc *adp = bp->bio_dev->si_drv1;
296 if (adp->device->flags & ATA_D_DETACHING) {
297 biofinish(bp, NULL, ENXIO);
301 bioqdisksort(&adp->queue, bp);
303 ata_start(adp->device->channel);
307 addump(dev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length)
309 struct ad_softc *adp = dev->si_drv1;
310 struct ad_request request;
317 /* force PIO mode for dumps */
318 adp->device->mode = ATA_PIO;
319 ata_reinit(adp->device->channel);
324 bzero(&request, sizeof(struct ad_request));
326 request.blockaddr = offset / DEV_BSIZE;
327 request.bytecount = length;
328 request.data = virtual;
330 while (request.bytecount > 0) {
331 ad_transfer(&request);
332 if (request.flags & ADR_F_ERROR)
334 request.donecount += request.currentsize;
335 request.bytecount -= request.currentsize;
339 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
340 ata_prtdev(adp->device, "timeout waiting for final ready\n");
346 ad_start(struct ata_device *atadev)
348 struct ad_softc *adp = atadev->driver;
349 struct bio *bp = bioq_first(&adp->queue);
350 struct ad_request *request;
356 /* if tagged queueing enabled get next free tag */
357 if (adp->flags & AD_F_TAG_ENABLED) {
358 while (tag <= adp->num_tags && adp->tags[tag])
360 if (tag > adp->num_tags )
364 if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
365 ata_prtdev(atadev, "out of memory in start\n");
370 request->softc = adp;
372 request->blockaddr = bp->bio_pblkno;
373 request->bytecount = bp->bio_bcount;
374 request->data = bp->bio_data;
376 if (bp->bio_cmd == BIO_READ)
377 request->flags |= ADR_F_READ;
379 if (adp->device->mode >= ATA_DMA && ata_dmaalloc(atadev))
380 adp->device->mode = ATA_PIO;
382 /* insert in tag array */
383 adp->tags[tag] = request;
385 /* remove from drive queue */
386 bioq_remove(&adp->queue, bp);
388 /* link onto controller queue */
389 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
393 ad_transfer(struct ad_request *request)
395 struct ad_softc *adp;
399 int flags = ATA_IMMEDIATE;
401 /* get request params */
402 adp = request->softc;
404 /* calculate transfer details */
405 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
407 /* start timeout for this transfer */
408 if (!request->timeout_handle.callout && !dumping)
409 request->timeout_handle =
410 timeout((timeout_t*)ad_timeout, request, 10 * hz);
412 if (request->donecount == 0) {
414 /* check & setup transfer parameters */
415 if (request->bytecount > adp->max_iosize) {
416 ata_prtdev(adp->device,
417 "%d byte transfers not supported\n", request->bytecount);
418 count = howmany(adp->max_iosize, DEV_BSIZE);
421 count = howmany(request->bytecount, DEV_BSIZE);
423 if (count > (adp->device->param->support.address48 ? 65536 : 256)) {
424 ata_prtdev(adp->device,
425 "%d block transfers not supported\n", count);
426 count = adp->device->param->support.address48 ? 65536 : 256;
429 if (adp->flags & AD_F_CHS_USED) {
430 int sector = (lba % adp->sectors) + 1;
431 int cylinder = lba / (adp->sectors * adp->heads);
432 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
434 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
435 adp->device->flags |= ATA_D_USE_CHS;
438 devstat_start_transaction(&adp->stats);
440 /* does this drive & transfer work with DMA ? */
441 request->flags &= ~ADR_F_DMA_USED;
442 if (adp->device->mode >= ATA_DMA &&
443 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
444 request->flags |= ADR_F_DMA_USED;
445 request->currentsize = request->bytecount;
447 /* do we have tags enabled ? */
448 if (adp->flags & AD_F_TAG_ENABLED) {
449 cmd = (request->flags & ADR_F_READ) ?
450 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
452 if (ata_command(adp->device, cmd, lba,
453 request->tag << 3, count, flags)) {
454 ata_prtdev(adp->device, "error executing command");
455 goto transfer_failed;
457 if (ata_wait(adp->device, ATA_S_READY)) {
458 ata_prtdev(adp->device, "timeout waiting for READY\n");
459 goto transfer_failed;
463 /* if ATA bus RELEASE check for SERVICE */
464 if (adp->flags & AD_F_TAG_ENABLED &&
465 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
467 return ad_service(adp, 1);
470 cmd = (request->flags & ADR_F_READ) ?
471 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
473 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
474 ata_prtdev(adp->device, "error executing command");
475 goto transfer_failed;
479 * wait for data transfer phase
481 * well this should be here acording to specs, but older
482 * promise controllers doesn't like it, they lockup!
484 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
485 ata_prtdev(adp->device, "timeout waiting for data phase\n");
486 goto transfer_failed;
491 /* start transfer, return and wait for interrupt */
492 ata_dmastart(adp->device, request->data, request->bytecount,
493 request->flags & ADR_F_READ);
494 return ATA_OP_CONTINUES;
497 /* does this drive support multi sector transfers ? */
498 if (adp->transfersize > DEV_BSIZE)
499 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
501 /* just plain old single sector transfer */
503 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
505 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
506 ata_prtdev(adp->device, "error executing command");
507 goto transfer_failed;
511 /* calculate this transfer length */
512 request->currentsize = min(request->bytecount, adp->transfersize);
514 /* if this is a PIO read operation, return and wait for interrupt */
515 if (request->flags & ADR_F_READ)
516 return ATA_OP_CONTINUES;
518 /* ready to write PIO data ? */
519 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
520 ata_prtdev(adp->device, "timeout waiting for DRQ");
521 goto transfer_failed;
524 /* output the data */
525 if (adp->device->channel->flags & ATA_USE_16BIT)
526 ATA_OUTSW_STRM(adp->device->channel->r_io, ATA_DATA,
527 (void *)((uintptr_t)request->data + request->donecount),
528 request->currentsize / sizeof(int16_t));
530 ATA_OUTSL_STRM(adp->device->channel->r_io, ATA_DATA,
531 (void *)((uintptr_t)request->data + request->donecount),
532 request->currentsize / sizeof(int32_t));
533 return ATA_OP_CONTINUES;
536 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
537 ad_invalidatequeue(adp, request);
539 /* if retries still permit, reinject this request */
540 if (request->retries++ < AD_MAX_RETRIES)
541 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
543 /* retries all used up, return error */
544 request->bp->bio_error = EIO;
545 request->bp->bio_flags |= BIO_ERROR;
546 request->bp->bio_resid = request->bytecount;
547 biofinish(request->bp, &adp->stats, 0);
550 ata_reinit(adp->device->channel);
551 return ATA_OP_CONTINUES;
555 ad_interrupt(struct ad_request *request)
557 struct ad_softc *adp = request->softc;
560 /* finish DMA transfer */
561 if (request->flags & ADR_F_DMA_USED)
562 dma_stat = ata_dmadone(adp->device);
564 /* do we have a corrected soft error ? */
565 if (adp->device->channel->status & ATA_S_CORR)
566 diskerr(request->bp, "soft error (ECC corrected)",
567 request->blockaddr + (request->donecount / DEV_BSIZE),
570 /* did any real errors happen ? */
571 if ((adp->device->channel->status & ATA_S_ERROR) ||
572 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
573 adp->device->channel->error =
574 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
575 diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
576 "UDMA ICRC error" : "hard error",
577 request->blockaddr + (request->donecount / DEV_BSIZE),
580 /* if this is a UDMA CRC error, reinject request */
581 if (request->flags & ADR_F_DMA_USED &&
582 adp->device->channel->error & ATA_E_ICRC) {
583 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
584 ad_invalidatequeue(adp, request);
586 if (request->retries++ < AD_MAX_RETRIES)
587 printf(" retrying\n");
589 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
590 printf(" falling back to PIO mode\n");
592 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
593 return ATA_OP_FINISHED;
596 /* if using DMA, try once again in PIO mode */
597 if (request->flags & ADR_F_DMA_USED) {
598 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
599 ad_invalidatequeue(adp, request);
600 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
601 request->flags |= ADR_F_FORCE_PIO;
602 printf(" trying PIO mode\n");
603 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
604 return ATA_OP_FINISHED;
607 request->flags |= ADR_F_ERROR;
608 printf(" status=%02x error=%02x\n",
609 adp->device->channel->status, adp->device->channel->error);
612 /* if we arrived here with forced PIO mode, DMA doesn't work right */
613 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
614 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
616 /* if this was a PIO read operation, get the data */
617 if (!(request->flags & ADR_F_DMA_USED) &&
618 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
620 /* ready to receive data? */
621 if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
622 != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
623 ata_prtdev(adp->device, "read interrupt arrived early");
625 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
626 ata_prtdev(adp->device, "read error detected (too) late");
627 request->flags |= ADR_F_ERROR;
630 /* data ready, read in */
631 if (adp->device->channel->flags & ATA_USE_16BIT)
632 ATA_INSW_STRM(adp->device->channel->r_io, ATA_DATA,
633 (void*)((uintptr_t)request->data +
634 request->donecount), request->currentsize /
637 ATA_INSL_STRM(adp->device->channel->r_io, ATA_DATA,
638 (void*)((uintptr_t)request->data +
639 request->donecount), request->currentsize /
644 /* finish up transfer */
645 if (request->flags & ADR_F_ERROR) {
646 request->bp->bio_error = EIO;
647 request->bp->bio_flags |= BIO_ERROR;
650 request->bytecount -= request->currentsize;
651 request->donecount += request->currentsize;
652 if (!(request->flags & ADR_F_DMA_USED) && request->bytecount > 0) {
653 ad_transfer(request);
654 return ATA_OP_CONTINUES;
658 /* disarm timeout for this transfer */
659 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
661 request->bp->bio_resid = request->bytecount;
663 biofinish(request->bp, &adp->stats, 0);
667 /* check for SERVICE */
668 return ad_service(adp, 1);
672 ad_service(struct ad_softc *adp, int change)
674 /* do we have to check the other device on this channel ? */
675 if (adp->device->channel->flags & ATA_QUEUED && change) {
676 int device = adp->device->unit;
678 if (adp->device->unit == ATA_MASTER) {
679 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
680 (adp->device->channel->device[SLAVE].driver) &&
681 ((struct ad_softc *) (adp->device->channel->
682 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
686 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
687 (adp->device->channel->device[MASTER].driver) &&
688 ((struct ad_softc *) (adp->device->channel->
689 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
692 if (device != adp->device->unit &&
694 (adp->device->channel->
695 device[ATA_DEV(device)].driver))->outstanding > 0) {
696 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
697 adp = adp->device->channel->device[ATA_DEV(device)].driver;
701 adp->device->channel->status =
702 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
704 /* do we have a SERVICE request from the drive ? */
705 if (adp->flags & AD_F_TAG_ENABLED &&
706 adp->outstanding > 0 &&
707 adp->device->channel->status & ATA_S_SERVICE) {
708 struct ad_request *request;
711 /* check for error */
712 if (adp->device->channel->status & ATA_S_ERROR) {
713 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
714 adp->device->channel->status,
715 adp->device->channel->error);
716 ad_invalidatequeue(adp, NULL);
717 return ATA_OP_FINISHED;
720 /* issue SERVICE cmd */
721 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
722 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
723 ad_invalidatequeue(adp, NULL);
724 return ATA_OP_FINISHED;
727 /* setup the transfer environment when ready */
728 if (ata_wait(adp->device, ATA_S_READY)) {
729 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
730 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
731 adp->device->channel->status,
732 adp->device->channel->error);
733 ad_invalidatequeue(adp, NULL);
734 return ATA_OP_FINISHED;
736 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
737 if (!(request = adp->tags[tag])) {
738 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
739 ad_invalidatequeue(adp, NULL);
740 return ATA_OP_FINISHED;
742 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
743 adp->device->channel->running = request;
746 /* start DMA transfer when ready */
747 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
748 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
749 adp->device->channel->status,
750 adp->device->channel->error);
751 ad_invalidatequeue(adp, NULL);
752 return ATA_OP_FINISHED;
754 ata_dmastart(adp->device, request->data, request->bytecount,
755 request->flags & ADR_F_READ);
756 return ATA_OP_CONTINUES;
758 return ATA_OP_FINISHED;
762 ad_free(struct ad_request *request)
764 request->softc->tags[request->tag] = NULL;
769 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
771 /* if tags in use invalidate all other outstanding transfers */
772 if (adp->flags & AD_F_TAG_ENABLED) {
773 struct ad_request *tmpreq;
776 ata_prtdev(adp->device, "invalidating queued requests\n");
777 for (tag = 0; tag <= adp->num_tags; tag++) {
778 tmpreq = adp->tags[tag];
779 adp->tags[tag] = NULL;
780 if (tmpreq == request || tmpreq == NULL)
782 untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
783 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
785 adp->outstanding = 0;
786 if (ata_command(adp->device, ATA_C_NOP,
787 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
788 ata_prtdev(adp->device, "flush queue failed\n");
793 ad_tagsupported(struct ad_softc *adp)
795 switch (adp->device->channel->chiptype) {
796 case 0x0d30105a: /* Promises before TX2 doesn't work with tagged queuing */
804 /* check that drive does DMA, has tags enabled, and is one we know works */
805 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
806 adp->device->param->enabled.queued) {
808 /* IBM DTTA series needs transfers <= 64K for tags to work properly */
809 if (!strncmp(adp->device->param->model, "IBM-DTTA", 8)) {
810 adp->max_iosize = 128 * DEV_BSIZE;
814 /* IBM DJNA series has broken tags, corrupts data */
815 if (!strncmp(adp->device->param->model, "IBM-DJNA", 8))
818 /* IBM DPTA & IBM DTLA series supports tags */
819 if (!strncmp(adp->device->param->model, "IBM-DPTA", 8) ||
820 !strncmp(adp->device->param->model, "IBM-DTLA", 8))
823 /* IBM IC series ATA drives supports tags */
824 if (!strncmp(adp->device->param->model, "IC", 2) &&
825 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
826 !strncmp(adp->device->param->model + 8, "AV", 2)))
833 ad_timeout(struct ad_request *request)
835 struct ad_softc *adp = request->softc;
837 adp->device->channel->running = NULL;
838 request->timeout_handle.callout = NULL;
839 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
840 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
841 request->tag, request->serv);
843 if (request->flags & ADR_F_DMA_USED) {
844 ata_dmadone(adp->device);
845 ad_invalidatequeue(adp, request);
846 if (request->retries == AD_MAX_RETRIES) {
847 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
848 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
849 request->retries = 0;
853 /* if retries still permit, reinject this request */
854 if (request->retries++ < AD_MAX_RETRIES) {
855 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
858 /* retries all used up, return error */
859 request->bp->bio_error = EIO;
860 request->bp->bio_flags |= BIO_ERROR;
861 biofinish(request->bp, &adp->stats, 0);
864 ata_reinit(adp->device->channel);
868 ad_reinit(struct ata_device *atadev)
870 struct ad_softc *adp = atadev->driver;
872 /* reinit disk parameters */
873 ad_invalidatequeue(atadev->driver, NULL);
874 ata_command(atadev, ATA_C_SET_MULTI, 0,
875 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
876 if (adp->device->mode >= ATA_DMA)
877 ata_dmainit(atadev, ata_pmode(adp->device->param),
878 ata_wmode(adp->device->param),
879 ata_umode(adp->device->param));
881 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
885 ad_print(struct ad_softc *adp)
888 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
889 adp->device->param->model, adp->device->param->revision,
890 ad_version(adp->device->param->version_major),
891 device_get_unit(adp->device->channel->dev),
892 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
894 ata_prtdev(adp->device,
895 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
896 (unsigned long long)(adp->total_secs /
897 ((1024L*1024L)/DEV_BSIZE)),
898 (unsigned long long)adp->total_secs,
899 (unsigned long long)(adp->total_secs /
900 (adp->heads * adp->sectors)),
901 adp->heads, adp->sectors, DEV_BSIZE);
903 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
904 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
905 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
906 ata_mode2str(adp->device->mode));
908 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
909 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
910 ata_umode(adp->device->param),
911 adp->device->param->hwres_cblid);
915 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
916 (unsigned long long)(adp->total_secs /
917 ((1024L * 1024L) / DEV_BSIZE)),
918 adp->device->param->model,
919 (unsigned long long)(adp->total_secs /
920 (adp->heads * adp->sectors)),
921 adp->heads, adp->sectors,
922 device_get_unit(adp->device->channel->dev),
923 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
924 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
925 ata_mode2str(adp->device->mode));
929 ad_version(u_int16_t version)
933 if (version == 0xffff)
935 for (bit = 15; bit >= 0; bit--)
936 if (version & (1<<bit))