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 bioq_init(&adp->queue);
128 lbasize = (u_int32_t)atadev->param->lba_size_1 |
129 ((u_int32_t)atadev->param->lba_size_2 << 16);
131 /* does this device need oldstyle CHS addressing */
132 if (!ad_version(atadev->param->version_major) ||
133 !(atadev->param->atavalid & ATA_FLAG_54_58) || !lbasize)
134 adp->flags |= AD_F_CHS_USED;
136 /* use the 28bit LBA size if valid */
137 if (atadev->param->cylinders == 16383 && adp->total_secs < lbasize)
138 adp->total_secs = lbasize;
140 lbasize48 = ((u_int64_t)atadev->param->lba_size48_1) |
141 ((u_int64_t)atadev->param->lba_size48_2 << 16) |
142 ((u_int64_t)atadev->param->lba_size48_3 << 32) |
143 ((u_int64_t)atadev->param->lba_size48_4 << 48);
145 /* use the 48bit LBA size if valid */
146 if (atadev->param->support.address48 && lbasize48 > 268435455)
147 adp->total_secs = lbasize48;
149 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
150 /* use multiple sectors/interrupt if device supports it */
151 adp->transfersize = DEV_BSIZE;
152 if (ad_version(atadev->param->version_major)) {
153 int secsperint = max(1, min(atadev->param->sectors_intr, 16));
155 if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
156 0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
157 adp->transfersize *= secsperint;
160 /* enable read caching if not default on device */
161 if (ata_command(atadev, ATA_C_SETFEATURES,
162 0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
163 ata_prtdev(atadev, "enabling readahead cache failed\n");
165 /* enable write caching if allowed and not default on device */
166 if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
167 if (ata_command(atadev, ATA_C_SETFEATURES,
168 0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
169 ata_prtdev(atadev, "enabling write cache failed\n");
172 if (ata_command(atadev, ATA_C_SETFEATURES,
173 0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
174 ata_prtdev(atadev, "disabling write cache failed\n");
177 /* use DMA if allowed and if drive/controller supports it */
179 ata_dmainit(atadev, ata_pmode(atadev->param),
180 ata_wmode(atadev->param), ata_umode(atadev->param));
182 ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
184 /* use tagged queueing if allowed and supported */
185 if (ata_tags && ad_tagsupported(adp)) {
186 adp->num_tags = atadev->param->queuelen;
187 adp->flags |= AD_F_TAG_ENABLED;
188 adp->device->channel->flags |= ATA_QUEUED;
189 if (ata_command(atadev, ATA_C_SETFEATURES,
190 0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
191 ata_prtdev(atadev, "disabling release interrupt failed\n");
192 if (ata_command(atadev, ATA_C_SETFEATURES,
193 0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
194 ata_prtdev(atadev, "disabling service interrupt failed\n");
197 ATA_UNLOCK_CH(atadev->channel);
199 devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
200 DEVSTAT_NO_ORDERED_TAGS,
201 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
202 DEVSTAT_PRIORITY_DISK);
204 dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw, &addisk_cdevsw);
206 dev->si_iosize_max = 256 * DEV_BSIZE;
209 /* construct the disklabel */
210 bzero(&adp->disk.d_label, sizeof(struct disklabel));
211 adp->disk.d_label.d_secsize = DEV_BSIZE;
212 adp->disk.d_label.d_nsectors = adp->sectors;
213 adp->disk.d_label.d_ntracks = adp->heads;
214 adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
215 adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
216 adp->disk.d_label.d_secperunit = adp->total_secs;
218 atadev->driver = adp;
221 /* if this disk belongs to an ATA RAID dont print the probe */
222 if (ata_raiddisk_attach(adp))
223 adp->flags |= AD_F_RAID_SUBDISK;
229 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
231 struct ad_softc *adp = atadev->driver;
232 struct ad_request *request;
235 atadev->flags |= ATA_D_DETACHING;
236 ata_prtdev(atadev, "removed from configuration\n");
237 ad_invalidatequeue(adp, NULL);
238 TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
239 if (request->softc != adp)
241 TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
242 biofinish(request->bp, NULL, ENXIO);
246 while ((bp = bioq_first(&adp->queue))) {
247 bioq_remove(&adp->queue, bp);
248 biofinish(bp, NULL, ENXIO);
250 disk_invalidate(&adp->disk);
251 disk_destroy(adp->dev);
252 devstat_remove_entry(&adp->stats);
254 if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
255 ata_prtdev(atadev, "flushing cache on detach failed\n");
257 if (adp->flags & AD_F_RAID_SUBDISK)
258 ata_raiddisk_detach(adp);
259 ata_free_name(atadev);
260 ata_free_lun(&adp_lun_map, adp->lun);
261 atadev->driver = NULL;
267 adopen(dev_t dev, int flags, int fmt, struct thread *td)
269 struct ad_softc *adp = dev->si_drv1;
271 if (adp->flags & AD_F_RAID_SUBDISK)
277 adclose(dev_t dev, int flags, int fmt, struct thread *td)
279 struct ad_softc *adp = dev->si_drv1;
281 ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
282 if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
283 ata_prtdev(adp->device, "flushing cache on close failed\n");
284 ATA_UNLOCK_CH(adp->device->channel);
289 adstrategy(struct bio *bp)
291 struct ad_softc *adp = bp->bio_dev->si_drv1;
294 if (adp->device->flags & ATA_D_DETACHING) {
295 biofinish(bp, NULL, ENXIO);
299 bioqdisksort(&adp->queue, bp);
301 ata_start(adp->device->channel);
305 addump(dev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length)
307 struct ad_softc *adp = dev->si_drv1;
308 struct ad_request request;
315 /* force PIO mode for dumps */
316 adp->device->mode = ATA_PIO;
317 ata_reinit(adp->device->channel);
322 bzero(&request, sizeof(struct ad_request));
324 request.blockaddr = offset / DEV_BSIZE;
325 request.bytecount = length;
326 request.data = virtual;
328 while (request.bytecount > 0) {
329 ad_transfer(&request);
330 if (request.flags & ADR_F_ERROR)
332 request.donecount += request.currentsize;
333 request.bytecount -= request.currentsize;
337 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
338 ata_prtdev(adp->device, "timeout waiting for final ready\n");
344 ad_start(struct ata_device *atadev)
346 struct ad_softc *adp = atadev->driver;
347 struct bio *bp = bioq_first(&adp->queue);
348 struct ad_request *request;
354 /* if tagged queueing enabled get next free tag */
355 if (adp->flags & AD_F_TAG_ENABLED) {
356 while (tag <= adp->num_tags && adp->tags[tag])
358 if (tag > adp->num_tags )
362 if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
363 ata_prtdev(atadev, "out of memory in start\n");
368 request->softc = adp;
370 request->blockaddr = bp->bio_pblkno;
371 request->bytecount = bp->bio_bcount;
372 request->data = bp->bio_data;
374 if (bp->bio_cmd == BIO_READ)
375 request->flags |= ADR_F_READ;
376 if (adp->device->mode >= ATA_DMA) {
377 if (ata_dmaalloc(atadev))
378 adp->device->mode = ATA_PIO;
381 /* insert in tag array */
382 adp->tags[tag] = request;
384 /* remove from drive queue */
385 bioq_remove(&adp->queue, bp);
387 /* link onto controller queue */
388 TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
392 ad_transfer(struct ad_request *request)
394 struct ad_softc *adp;
396 u_int32_t count, max_count;
398 int flags = ATA_IMMEDIATE;
400 /* get request params */
401 adp = request->softc;
403 /* calculate transfer details */
404 lba = request->blockaddr + (request->donecount / DEV_BSIZE);
406 if (request->donecount == 0) {
408 /* start timeout for this transfer */
410 request->timeout_handle.callout = NULL;
412 request->timeout_handle =
413 timeout((timeout_t*)ad_timeout, request, 10 * hz);
415 /* setup transfer parameters */
416 count = howmany(request->bytecount, DEV_BSIZE);
417 max_count = adp->device->param->support.address48 ? 65536 : 256;
418 if (count > max_count) {
419 ata_prtdev(adp->device,
420 "count %d size transfers not supported\n", count);
424 if (adp->flags & AD_F_CHS_USED) {
425 int sector = (lba % adp->sectors) + 1;
426 int cylinder = lba / (adp->sectors * adp->heads);
427 int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
429 lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
430 adp->device->flags |= ATA_D_USE_CHS;
433 /* setup first transfer length */
434 request->currentsize = min(request->bytecount, adp->transfersize);
436 devstat_start_transaction(&adp->stats);
438 /* does this drive & transfer work with DMA ? */
439 request->flags &= ~ADR_F_DMA_USED;
440 if (adp->device->mode >= ATA_DMA &&
441 !ata_dmasetup(adp->device, request->data, request->bytecount)) {
442 request->flags |= ADR_F_DMA_USED;
443 request->currentsize = request->bytecount;
445 /* do we have tags enabled ? */
446 if (adp->flags & AD_F_TAG_ENABLED) {
447 cmd = (request->flags & ADR_F_READ) ?
448 ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
450 if (ata_command(adp->device, cmd, lba,
451 request->tag << 3, count, flags)) {
452 ata_prtdev(adp->device, "error executing command");
453 goto transfer_failed;
455 if (ata_wait(adp->device, ATA_S_READY)) {
456 ata_prtdev(adp->device, "timeout waiting for READY\n");
457 goto transfer_failed;
461 /* if ATA bus RELEASE check for SERVICE */
462 if (adp->flags & AD_F_TAG_ENABLED &&
463 ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
465 return ad_service(adp, 1);
468 cmd = (request->flags & ADR_F_READ) ?
469 ATA_C_READ_DMA : ATA_C_WRITE_DMA;
471 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
472 ata_prtdev(adp->device, "error executing command");
473 goto transfer_failed;
477 * wait for data transfer phase
479 * well this should be here acording to specs, but older
480 * promise controllers doesn't like it, they lockup!
482 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
483 ata_prtdev(adp->device, "timeout waiting for data phase\n");
484 goto transfer_failed;
489 /* start transfer, return and wait for interrupt */
490 ata_dmastart(adp->device, request->data, request->bytecount,
491 request->flags & ADR_F_READ);
492 return ATA_OP_CONTINUES;
495 /* does this drive support multi sector transfers ? */
496 if (request->currentsize > DEV_BSIZE)
497 cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
499 /* just plain old single sector transfer */
501 cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
503 if (ata_command(adp->device, cmd, lba, count, 0, flags)){
504 ata_prtdev(adp->device, "error executing command");
505 goto transfer_failed;
509 /* calculate this transfer length */
510 request->currentsize = min(request->bytecount, adp->transfersize);
512 /* if this is a PIO read operation, return and wait for interrupt */
513 if (request->flags & ADR_F_READ)
514 return ATA_OP_CONTINUES;
516 /* ready to write PIO data ? */
517 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
518 ata_prtdev(adp->device, "timeout waiting for DRQ");
519 goto transfer_failed;
522 /* output the data */
523 if (adp->device->channel->flags & ATA_USE_16BIT)
524 ATA_OUTSW_STRM(adp->device->channel->r_io, ATA_DATA,
525 (void *)((uintptr_t)request->data + request->donecount),
526 request->currentsize / sizeof(int16_t));
528 ATA_OUTSL_STRM(adp->device->channel->r_io, ATA_DATA,
529 (void *)((uintptr_t)request->data + request->donecount),
530 request->currentsize / sizeof(int32_t));
531 return ATA_OP_CONTINUES;
534 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
535 ad_invalidatequeue(adp, request);
536 printf(" - resetting\n");
538 /* if retries still permit, reinject this request */
539 if (request->retries++ < AD_MAX_RETRIES)
540 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
542 /* retries all used up, return error */
543 request->bp->bio_error = EIO;
544 request->bp->bio_flags |= BIO_ERROR;
545 request->bp->bio_resid = request->bytecount;
546 biofinish(request->bp, &adp->stats, 0);
549 ata_reinit(adp->device->channel);
550 return ATA_OP_CONTINUES;
554 ad_interrupt(struct ad_request *request)
556 struct ad_softc *adp = request->softc;
559 /* finish DMA transfer */
560 if (request->flags & ADR_F_DMA_USED)
561 dma_stat = ata_dmadone(adp->device);
563 /* do we have a corrected soft error ? */
564 if (adp->device->channel->status & ATA_S_CORR)
565 diskerr(request->bp, "soft error (ECC corrected)",
566 request->blockaddr + (request->donecount / DEV_BSIZE),
569 /* did any real errors happen ? */
570 if ((adp->device->channel->status & ATA_S_ERROR) ||
571 (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
572 adp->device->channel->error =
573 ATA_INB(adp->device->channel->r_io, ATA_ERROR);
574 diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
575 "UDMA ICRC error" : "hard error",
576 request->blockaddr + (request->donecount / DEV_BSIZE),
579 /* if this is a UDMA CRC error, reinject request */
580 if (request->flags & ADR_F_DMA_USED &&
581 adp->device->channel->error & ATA_E_ICRC) {
582 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
583 ad_invalidatequeue(adp, request);
585 if (request->retries++ < AD_MAX_RETRIES)
586 printf(" retrying\n");
588 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
589 printf(" falling back to PIO mode\n");
591 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
592 return ATA_OP_FINISHED;
595 /* if using DMA, try once again in PIO mode */
596 if (request->flags & ADR_F_DMA_USED) {
597 untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
598 ad_invalidatequeue(adp, request);
599 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
600 request->flags |= ADR_F_FORCE_PIO;
601 printf(" trying PIO mode\n");
602 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
603 return ATA_OP_FINISHED;
606 request->flags |= ADR_F_ERROR;
607 printf(" status=%02x error=%02x\n",
608 adp->device->channel->status, adp->device->channel->error);
611 /* if we arrived here with forced PIO mode, DMA doesn't work right */
612 if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
613 ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
615 /* if this was a PIO read operation, get the data */
616 if (!(request->flags & ADR_F_DMA_USED) &&
617 (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
619 /* ready to receive data? */
620 if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
621 != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
622 ata_prtdev(adp->device, "read interrupt arrived early");
624 if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
625 ata_prtdev(adp->device, "read error detected (too) late");
626 request->flags |= ADR_F_ERROR;
629 /* data ready, read in */
630 if (adp->device->channel->flags & ATA_USE_16BIT)
631 ATA_INSW_STRM(adp->device->channel->r_io, ATA_DATA,
632 (void*)((uintptr_t)request->data +
633 request->donecount), request->currentsize /
636 ATA_INSL_STRM(adp->device->channel->r_io, ATA_DATA,
637 (void*)((uintptr_t)request->data +
638 request->donecount), request->currentsize /
643 /* finish up transfer */
644 if (request->flags & ADR_F_ERROR) {
645 request->bp->bio_error = EIO;
646 request->bp->bio_flags |= BIO_ERROR;
649 request->bytecount -= request->currentsize;
650 request->donecount += request->currentsize;
651 if (request->bytecount > 0) {
652 ad_transfer(request);
653 return ATA_OP_CONTINUES;
657 /* disarm timeout for this transfer */
658 untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
660 request->bp->bio_resid = request->bytecount;
662 biofinish(request->bp, &adp->stats, 0);
666 /* check for SERVICE (tagged operations only) */
667 return ad_service(adp, 1);
671 ad_service(struct ad_softc *adp, int change)
673 /* do we have to check the other device on this channel ? */
674 if (adp->device->channel->flags & ATA_QUEUED && change) {
675 int device = adp->device->unit;
677 if (adp->device->unit == ATA_MASTER) {
678 if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
679 (adp->device->channel->device[SLAVE].driver) &&
680 ((struct ad_softc *) (adp->device->channel->
681 device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
685 if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
686 (adp->device->channel->device[MASTER].driver) &&
687 ((struct ad_softc *) (adp->device->channel->
688 device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
691 if (device != adp->device->unit &&
693 (adp->device->channel->
694 device[ATA_DEV(device)].driver))->outstanding > 0) {
695 ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
696 adp = adp->device->channel->device[ATA_DEV(device)].driver;
700 adp->device->channel->status =
701 ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
703 /* do we have a SERVICE request from the drive ? */
704 if (adp->flags & AD_F_TAG_ENABLED &&
705 adp->outstanding > 0 &&
706 adp->device->channel->status & ATA_S_SERVICE) {
707 struct ad_request *request;
710 /* check for error */
711 if (adp->device->channel->status & ATA_S_ERROR) {
712 ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
713 adp->device->channel->status,
714 adp->device->channel->error);
715 ad_invalidatequeue(adp, NULL);
716 return ATA_OP_FINISHED;
719 /* issue SERVICE cmd */
720 if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
721 ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
722 ad_invalidatequeue(adp, NULL);
723 return ATA_OP_FINISHED;
726 /* setup the transfer environment when ready */
727 if (ata_wait(adp->device, ATA_S_READY)) {
728 ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
729 ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
730 adp->device->channel->status,
731 adp->device->channel->error);
732 ad_invalidatequeue(adp, NULL);
733 return ATA_OP_FINISHED;
735 tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
736 if (!(request = adp->tags[tag])) {
737 ata_prtdev(adp->device, "no request for tag=%d\n", tag);
738 ad_invalidatequeue(adp, NULL);
739 return ATA_OP_FINISHED;
741 adp->device->channel->active = ATA_ACTIVE_ATA;
742 adp->device->channel->running = request;
745 /* start DMA transfer when ready */
746 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
747 ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
748 adp->device->channel->status,
749 adp->device->channel->error);
750 ad_invalidatequeue(adp, NULL);
751 return ATA_OP_FINISHED;
753 ata_dmastart(adp->device, request->data, request->bytecount,
754 request->flags & ADR_F_READ);
755 return ATA_OP_CONTINUES;
757 return ATA_OP_FINISHED;
761 ad_free(struct ad_request *request)
763 request->softc->tags[request->tag] = NULL;
768 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
770 /* if tags used invalidate all other tagged transfers */
771 if (adp->flags & AD_F_TAG_ENABLED) {
772 struct ad_request *tmpreq;
775 ata_prtdev(adp->device, "invalidating queued requests\n");
776 for (tag = 0; tag <= adp->num_tags; tag++) {
777 tmpreq = adp->tags[tag];
778 adp->tags[tag] = NULL;
779 if (tmpreq == request || tmpreq == NULL)
781 untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
782 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
784 if (ata_command(adp->device, ATA_C_NOP,
785 0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
786 ata_prtdev(adp->device, "flush queue failed\n");
787 adp->outstanding = 0;
792 ad_tagsupported(struct ad_softc *adp)
794 const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
797 switch (adp->device->channel->chiptype) {
798 case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
805 /* check that drive does DMA, has tags enabled, and is one we know works */
806 if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued &&
807 adp->device->param->enabled.queued) {
808 while (good[i] != NULL) {
809 if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
814 * check IBM's new obscure way of naming drives
815 * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
816 * but doesn't care about the other info (size, capacity etc)
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 ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
833 (request->flags & ADR_F_READ) ? "READ" : "WRITE",
834 request->tag, request->serv);
836 if (request->flags & ADR_F_DMA_USED) {
837 ata_dmadone(adp->device);
838 ad_invalidatequeue(adp, request);
839 if (request->retries == AD_MAX_RETRIES) {
840 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
841 ata_prtdev(adp->device, "trying fallback to PIO mode\n");
842 request->retries = 0;
846 /* if retries still permit, reinject this request */
847 if (request->retries++ < AD_MAX_RETRIES) {
848 TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
851 /* retries all used up, return error */
852 request->bp->bio_error = EIO;
853 request->bp->bio_flags |= BIO_ERROR;
854 biofinish(request->bp, &adp->stats, 0);
857 ata_reinit(adp->device->channel);
861 ad_reinit(struct ata_device *atadev)
863 struct ad_softc *adp = atadev->driver;
865 /* reinit disk parameters */
866 ad_invalidatequeue(atadev->driver, NULL);
867 ata_command(atadev, ATA_C_SET_MULTI, 0,
868 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
869 if (adp->device->mode >= ATA_DMA)
870 ata_dmainit(atadev, ata_pmode(adp->device->param),
871 ata_wmode(adp->device->param),
872 ata_umode(adp->device->param));
874 ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
878 ad_print(struct ad_softc *adp)
881 ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n",
882 adp->device->param->model, adp->device->param->revision,
883 ad_version(adp->device->param->version_major),
884 device_get_unit(adp->device->channel->dev),
885 (adp->device->unit == ATA_MASTER) ? "master" : "slave");
887 ata_prtdev(adp->device,
888 "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
889 (unsigned long long)(adp->total_secs /
890 ((1024L*1024L)/DEV_BSIZE)),
891 (unsigned long long) adp->total_secs,
892 (unsigned long long) (adp->total_secs /
893 (adp->heads * adp->sectors)),
894 adp->heads, adp->sectors, DEV_BSIZE);
896 ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n",
897 adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
898 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
899 ata_mode2str(adp->device->mode));
901 ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
902 ata_pmode(adp->device->param), ata_wmode(adp->device->param),
903 ata_umode(adp->device->param),
904 adp->device->param->hwres_cblid);
908 ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
909 (unsigned long long)(adp->total_secs /
910 ((1024L * 1024L) / DEV_BSIZE)),
911 adp->device->param->model,
912 (unsigned long long)(adp->total_secs /
913 (adp->heads*adp->sectors)),
914 adp->heads, adp->sectors,
915 device_get_unit(adp->device->channel->dev),
916 (adp->device->unit == ATA_MASTER) ? "master" : "slave",
917 (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
918 ata_mode2str(adp->device->mode));
922 ad_version(u_int16_t version)
926 if (version == 0xffff)
928 for (bit = 15; bit >= 0; bit--)
929 if (version & (1<<bit))