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 adp->disk.d_sectorsize = DEV_BSIZE;
212 adp->disk.d_mediasize = DEV_BSIZE * (off_t)adp->total_secs;
213 adp->disk.d_fwsectors = adp->sectors;
214 adp->disk.d_fwheads = adp->heads;
216 atadev->driver = adp;
219 /* if this disk belongs to an ATA RAID dont print the probe */
220 if (ata_raiddisk_attach(adp))
221 adp->flags |= AD_F_RAID_SUBDISK;
227 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
229 struct ad_softc *adp = atadev->driver;
230 struct ad_request *request;
233 atadev->flags |= ATA_D_DETACHING;
234 ata_prtdev(atadev, "removed from configuration\n");
235 ad_invalidatequeue(adp, NULL);
236 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
237 if (request->softc != adp)
239 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
240 biofinish(request->bp, NULL, ENXIO);
244 while ((bp = bioq_first(&adp->queue))) {
245 bioq_remove(&adp->queue, bp);
246 biofinish(bp, NULL, ENXIO);
248 disk_invalidate(&adp->disk);
249 disk_destroy(adp->dev);
250 devstat_remove_entry(&adp->stats);
252 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
253 ata_prtdev(atadev, "flushing cache on detach failed\n");
255 if (adp->flags & AD_F_RAID_SUBDISK)
256 ata_raiddisk_detach(adp);
257 ata_free_name(atadev);
258 ata_free_lun(&adp_lun_map, adp->lun);
259 atadev->driver = NULL;
265 adopen(dev_t dev, int flags, int fmt, struct thread *td)
267 struct ad_softc *adp = dev->si_drv1;
269 if (adp->flags & AD_F_RAID_SUBDISK)
275 adclose(dev_t dev, int flags, int fmt, struct thread *td)
277 struct ad_softc *adp = dev->si_drv1;
279 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
280 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
281 ata_prtdev(adp->device, "flushing cache on close failed\n");
282 ATA_UNLOCK_CH(adp->device->channel);
287 adstrategy(struct bio *bp)
289 struct ad_softc *adp = bp->bio_dev->si_drv1;
292 if (adp->device->flags & ATA_D_DETACHING) {
293 biofinish(bp, NULL, ENXIO);
297 bioqdisksort(&adp->queue, bp);
299 ata_start(adp->device->channel);
303 addump(dev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length)
305 struct ad_softc *adp = dev->si_drv1;
306 struct ad_request request;
313 /* force PIO mode for dumps */
314 adp->device->mode = ATA_PIO;
315 ata_reinit(adp->device->channel);
320 bzero(&request, sizeof(struct ad_request));
322 request.blockaddr = offset / DEV_BSIZE;
323 request.bytecount = length;
324 request.data = virtual;
326 while (request.bytecount > 0) {
327 ad_transfer(&request);
328 if (request.flags & ADR_F_ERROR)
330 request.donecount += request.currentsize;
331 request.bytecount -= request.currentsize;
335 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
336 ata_prtdev(adp->device, "timeout waiting for final ready\n");
342 ad_start(struct ata_device *atadev)
344 struct ad_softc *adp = atadev->driver;
345 struct bio *bp = bioq_first(&adp->queue);
346 struct ad_request *request;
352 /* if tagged queueing enabled get next free tag */
353 if (adp->flags & AD_F_TAG_ENABLED) {
354 while (tag <= adp->num_tags && adp->tags[tag])
356 if (tag > adp->num_tags )
360 if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
361 ata_prtdev(atadev, "out of memory in start\n");
366 request->softc = adp;
368 request->blockaddr = bp->bio_pblkno;
369 request->bytecount = bp->bio_bcount;
370 request->data = bp->bio_data;
372 if (bp->bio_cmd == BIO_READ)
373 request->flags |= ADR_F_READ;
375 if (adp->device->mode >= ATA_DMA && ata_dmaalloc(atadev))
376 adp->device->mode = ATA_PIO;
378 /* insert in tag array */
379 adp->tags[tag] = request;
381 /* remove from drive queue */
382 bioq_remove(&adp->queue, bp);
384 /* link onto controller queue */
385 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
389 ad_transfer(struct ad_request *request)
391 struct ad_softc *adp;
395 int flags = ATA_IMMEDIATE;
397 /* get request params */
398 adp = request->softc;
400 /* calculate transfer details */
401 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
403 /* start timeout for this transfer */
404 if (!request->timeout_handle.callout && !dumping)
405 request->timeout_handle =
406 timeout((timeout_t*)ad_timeout, request, 10 * hz);
408 if (request->donecount == 0) {
410 /* check & setup transfer parameters */
411 if (request->bytecount > adp->max_iosize) {
412 ata_prtdev(adp->device,
413 "%d byte transfers not supported\n", request->bytecount);
414 count = howmany(adp->max_iosize, DEV_BSIZE);
417 count = howmany(request->bytecount, DEV_BSIZE);
419 if (count > (adp->device->param->support.address48 ? 65536 : 256)) {
420 ata_prtdev(adp->device,
421 "%d block transfers not supported\n", count);
422 count = adp->device->param->support.address48 ? 65536 : 256;
425 if (adp->flags & AD_F_CHS_USED) {
426 int sector = (lba % adp->sectors) + 1;
427 int cylinder = lba / (adp->sectors * adp->heads);
428 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
430 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
431 adp->device->flags |= ATA_D_USE_CHS;
434 devstat_start_transaction(&adp->stats);
436 /* does this drive & transfer work with DMA ? */
437 request->flags &= ~ADR_F_DMA_USED;
438 if (adp->device->mode >= ATA_DMA &&
439 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
440 request->flags |= ADR_F_DMA_USED;
441 request->currentsize = request->bytecount;
443 /* do we have tags enabled ? */
444 if (adp->flags & AD_F_TAG_ENABLED) {
445 cmd = (request->flags & ADR_F_READ) ?
446 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
448 if (ata_command(adp->device, cmd, lba,
449 request->tag << 3, count, flags)) {
450 ata_prtdev(adp->device, "error executing command");
451 goto transfer_failed;
453 if (ata_wait(adp->device, ATA_S_READY)) {
454 ata_prtdev(adp->device, "timeout waiting for READY\n");
455 goto transfer_failed;
459 /* if ATA bus RELEASE check for SERVICE */
460 if (adp->flags & AD_F_TAG_ENABLED &&
461 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
463 return ad_service(adp, 1);
466 cmd = (request->flags & ADR_F_READ) ?
467 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
469 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
470 ata_prtdev(adp->device, "error executing command");
471 goto transfer_failed;
475 * wait for data transfer phase
477 * well this should be here acording to specs, but older
478 * promise controllers doesn't like it, they lockup!
480 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
481 ata_prtdev(adp->device, "timeout waiting for data phase\n");
482 goto transfer_failed;
487 /* start transfer, return and wait for interrupt */
488 ata_dmastart(adp->device, request->data, request->bytecount,
489 request->flags & ADR_F_READ);
490 return ATA_OP_CONTINUES;
493 /* does this drive support multi sector transfers ? */
494 if (adp->transfersize > DEV_BSIZE)
495 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
497 /* just plain old single sector transfer */
499 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
501 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
502 ata_prtdev(adp->device, "error executing command");
503 goto transfer_failed;
507 /* calculate this transfer length */
508 request->currentsize = min(request->bytecount, adp->transfersize);
510 /* if this is a PIO read operation, return and wait for interrupt */
511 if (request->flags & ADR_F_READ)
512 return ATA_OP_CONTINUES;
514 /* ready to write PIO data ? */
515 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
516 ata_prtdev(adp->device, "timeout waiting for DRQ");
517 goto transfer_failed;
520 /* output the data */
521 if (adp->device->channel->flags & ATA_USE_16BIT)
522 ATA_OUTSW_STRM(adp->device->channel->r_io, ATA_DATA,
523 (void *)((uintptr_t)request->data + request->donecount),
524 request->currentsize / sizeof(int16_t));
526 ATA_OUTSL_STRM(adp->device->channel->r_io, ATA_DATA,
527 (void *)((uintptr_t)request->data + request->donecount),
528 request->currentsize / sizeof(int32_t));
529 return ATA_OP_CONTINUES;
532 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
533 ad_invalidatequeue(adp, request);
535 /* if retries still permit, reinject this request */
536 if (request->retries++ < AD_MAX_RETRIES)
537 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
539 /* retries all used up, return error */
540 request->bp->bio_error = EIO;
541 request->bp->bio_flags |= BIO_ERROR;
542 request->bp->bio_resid = request->bytecount;
543 biofinish(request->bp, &adp->stats, 0);
546 ata_reinit(adp->device->channel);
547 return ATA_OP_CONTINUES;
551 ad_interrupt(struct ad_request *request)
553 struct ad_softc *adp = request->softc;
556 /* finish DMA transfer */
557 if (request->flags & ADR_F_DMA_USED)
558 dma_stat = ata_dmadone(adp->device);
560 /* do we have a corrected soft error ? */
561 if (adp->device->channel->status & ATA_S_CORR)
562 disk_err(request->bp, "soft error (ECC corrected)",
563 request->blockaddr + (request->donecount / DEV_BSIZE), 1);
565 /* did any real errors happen ? */
566 if ((adp->device->channel->status & ATA_S_ERROR) ||
567 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
568 adp->device->channel->error =
569 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
570 disk_err(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
571 "UDMA ICRC error" : "hard error",
572 request->blockaddr + (request->donecount / DEV_BSIZE), 1);
574 /* if this is a UDMA CRC error, reinject request */
575 if (request->flags & ADR_F_DMA_USED &&
576 adp->device->channel->error & ATA_E_ICRC) {
577 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
578 ad_invalidatequeue(adp, request);
580 if (request->retries++ < AD_MAX_RETRIES)
581 printf(" retrying\n");
583 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
584 printf(" falling back to PIO mode\n");
586 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
587 return ATA_OP_FINISHED;
590 /* if using DMA, try once again in PIO mode */
591 if (request->flags & ADR_F_DMA_USED) {
592 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
593 ad_invalidatequeue(adp, request);
594 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
595 request->flags |= ADR_F_FORCE_PIO;
596 printf(" trying PIO mode\n");
597 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
598 return ATA_OP_FINISHED;
601 request->flags |= ADR_F_ERROR;
602 printf(" status=%02x error=%02x\n",
603 adp->device->channel->status, adp->device->channel->error);
606 /* if we arrived here with forced PIO mode, DMA doesn't work right */
607 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
608 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
610 /* if this was a PIO read operation, get the data */
611 if (!(request->flags & ADR_F_DMA_USED) &&
612 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
614 /* ready to receive data? */
615 if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
616 != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
617 ata_prtdev(adp->device, "read interrupt arrived early");
619 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
620 ata_prtdev(adp->device, "read error detected (too) late");
621 request->flags |= ADR_F_ERROR;
624 /* data ready, read in */
625 if (adp->device->channel->flags & ATA_USE_16BIT)
626 ATA_INSW_STRM(adp->device->channel->r_io, ATA_DATA,
627 (void*)((uintptr_t)request->data +
628 request->donecount), request->currentsize /
631 ATA_INSL_STRM(adp->device->channel->r_io, ATA_DATA,
632 (void*)((uintptr_t)request->data +
633 request->donecount), request->currentsize /
638 /* finish up transfer */
639 if (request->flags & ADR_F_ERROR) {
640 request->bp->bio_error = EIO;
641 request->bp->bio_flags |= BIO_ERROR;
644 request->bytecount -= request->currentsize;
645 request->donecount += request->currentsize;
646 if (!(request->flags & ADR_F_DMA_USED) && request->bytecount > 0) {
647 ad_transfer(request);
648 return ATA_OP_CONTINUES;
652 /* disarm timeout for this transfer */
653 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
655 request->bp->bio_resid = request->bytecount;
657 biofinish(request->bp, &adp->stats, 0);
661 /* check for SERVICE */
662 return ad_service(adp, 1);
666 ad_service(struct ad_softc *adp, int change)
668 /* do we have to check the other device on this channel ? */
669 if (adp->device->channel->flags & ATA_QUEUED && change) {
670 int device = adp->device->unit;
672 if (adp->device->unit == ATA_MASTER) {
673 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
674 (adp->device->channel->device[SLAVE].driver) &&
675 ((struct ad_softc *) (adp->device->channel->
676 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
680 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
681 (adp->device->channel->device[MASTER].driver) &&
682 ((struct ad_softc *) (adp->device->channel->
683 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
686 if (device != adp->device->unit &&
688 (adp->device->channel->
689 device[ATA_DEV(device)].driver))->outstanding > 0) {
690 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
691 adp = adp->device->channel->device[ATA_DEV(device)].driver;
695 adp->device->channel->status =
696 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
698 /* do we have a SERVICE request from the drive ? */
699 if (adp->flags & AD_F_TAG_ENABLED &&
700 adp->outstanding > 0 &&
701 adp->device->channel->status & ATA_S_SERVICE) {
702 struct ad_request *request;
705 /* check for error */
706 if (adp->device->channel->status & ATA_S_ERROR) {
707 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
708 adp->device->channel->status,
709 adp->device->channel->error);
710 ad_invalidatequeue(adp, NULL);
711 return ATA_OP_FINISHED;
714 /* issue SERVICE cmd */
715 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
716 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
717 ad_invalidatequeue(adp, NULL);
718 return ATA_OP_FINISHED;
721 /* setup the transfer environment when ready */
722 if (ata_wait(adp->device, ATA_S_READY)) {
723 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
724 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
725 adp->device->channel->status,
726 adp->device->channel->error);
727 ad_invalidatequeue(adp, NULL);
728 return ATA_OP_FINISHED;
730 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
731 if (!(request = adp->tags[tag])) {
732 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
733 ad_invalidatequeue(adp, NULL);
734 return ATA_OP_FINISHED;
736 ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
737 adp->device->channel->running = request;
740 /* start DMA transfer when ready */
741 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
742 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
743 adp->device->channel->status,
744 adp->device->channel->error);
745 ad_invalidatequeue(adp, NULL);
746 return ATA_OP_FINISHED;
748 ata_dmastart(adp->device, request->data, request->bytecount,
749 request->flags & ADR_F_READ);
750 return ATA_OP_CONTINUES;
752 return ATA_OP_FINISHED;
756 ad_free(struct ad_request *request)
758 request->softc->tags[request->tag] = NULL;
763 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
765 /* if tags in use invalidate all other outstanding transfers */
766 if (adp->flags & AD_F_TAG_ENABLED) {
767 struct ad_request *tmpreq;
770 ata_prtdev(adp->device, "invalidating queued requests\n");
771 for (tag = 0; tag <= adp->num_tags; tag++) {
772 tmpreq = adp->tags[tag];
773 adp->tags[tag] = NULL;
774 if (tmpreq == request || tmpreq == NULL)
776 untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
777 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
779 adp->outstanding = 0;
780 if (ata_command(adp->device, ATA_C_NOP,
781 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
782 ata_prtdev(adp->device, "flush queue failed\n");
787 ad_tagsupported(struct ad_softc *adp)
789 switch (adp->device->channel->chiptype) {
790 case 0x0d30105a: /* Promises before TX2 doesn't work with tagged queuing */
798 /* check that drive does DMA, has tags enabled, and is one we know works */
799 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
800 adp->device->param->enabled.queued) {
802 /* IBM DTTA series needs transfers <= 64K for tags to work properly */
803 if (!strncmp(adp->device->param->model, "IBM-DTTA", 8)) {
804 adp->max_iosize = 128 * DEV_BSIZE;
808 /* IBM DJNA series has broken tags, corrupts data */
809 if (!strncmp(adp->device->param->model, "IBM-DJNA", 8))
812 /* IBM DPTA & IBM DTLA series supports tags */
813 if (!strncmp(adp->device->param->model, "IBM-DPTA", 8) ||
814 !strncmp(adp->device->param->model, "IBM-DTLA", 8))
817 /* IBM IC series ATA drives supports tags */
818 if (!strncmp(adp->device->param->model, "IC", 2) &&
819 (!strncmp(adp->device->param->model + 8, "AT", 2) ||
820 !strncmp(adp->device->param->model + 8, "AV", 2)))
827 ad_timeout(struct ad_request *request)
829 struct ad_softc *adp = request->softc;
831 adp->device->channel->running = NULL;
832 request->timeout_handle.callout = NULL;
833 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
834 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
835 request->tag, request->serv);
837 if (request->flags & ADR_F_DMA_USED) {
838 ata_dmadone(adp->device);
839 ad_invalidatequeue(adp, request);
840 if (request->retries == AD_MAX_RETRIES) {
841 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
842 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
843 request->retries = 0;
847 /* if retries still permit, reinject this request */
848 if (request->retries++ < AD_MAX_RETRIES) {
849 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
852 /* retries all used up, return error */
853 request->bp->bio_error = EIO;
854 request->bp->bio_flags |= BIO_ERROR;
855 biofinish(request->bp, &adp->stats, 0);
858 ata_reinit(adp->device->channel);
862 ad_reinit(struct ata_device *atadev)
864 struct ad_softc *adp = atadev->driver;
866 /* reinit disk parameters */
867 ad_invalidatequeue(atadev->driver, NULL);
868 ata_command(atadev, ATA_C_SET_MULTI, 0,
869 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
870 if (adp->device->mode >= ATA_DMA)
871 ata_dmainit(atadev, ata_pmode(adp->device->param),
872 ata_wmode(adp->device->param),
873 ata_umode(adp->device->param));
875 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
879 ad_print(struct ad_softc *adp)
882 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
883 adp->device->param->model, adp->device->param->revision,
884 ad_version(adp->device->param->version_major),
885 device_get_unit(adp->device->channel->dev),
886 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
888 ata_prtdev(adp->device,
889 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
890 (unsigned long long)(adp->total_secs /
891 ((1024L*1024L)/DEV_BSIZE)),
892 (unsigned long long)adp->total_secs,
893 (unsigned long long)(adp->total_secs /
894 (adp->heads * adp->sectors)),
895 adp->heads, adp->sectors, DEV_BSIZE);
897 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
898 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
899 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
900 ata_mode2str(adp->device->mode));
902 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
903 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
904 ata_umode(adp->device->param),
905 adp->device->param->hwres_cblid);
909 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
910 (unsigned long long)(adp->total_secs /
911 ((1024L * 1024L) / DEV_BSIZE)),
912 adp->device->param->model,
913 (unsigned long long)(adp->total_secs /
914 (adp->heads * adp->sectors)),
915 adp->heads, adp->sectors,
916 device_get_unit(adp->device->channel->dev),
917 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
918 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
919 ata_mode2str(adp->device->mode));
923 ad_version(u_int16_t version)
927 if (version == 0xffff)
929 for (bit = 15; bit >= 0; bit--)
930 if (version & (1<<bit))