]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-disk.c
This commit was generated by cvs2svn to compensate for changes in r104756,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-disk.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
27  *
28  * $FreeBSD$
29  */
30
31 #include "opt_ata.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ata.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/bio.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/disk.h>
41 #include <sys/devicestat.h>
42 #include <sys/cons.h>
43 #include <sys/sysctl.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <machine/md_var.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-disk.h>
51 #include <dev/ata/ata-raid.h>
52
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 = {
59         /* open */      adopen,
60         /* close */     adclose,
61         /* read */      physread,
62         /* write */     physwrite,
63         /* ioctl */     noioctl,
64         /* poll */      nopoll,
65         /* mmap */      nommap,
66         /* strategy */  adstrategy,
67         /* name */      "ad",
68         /* maj */       116,
69         /* dump */      addump,
70         /* psize */     nopsize,
71         /* flags */     D_DISK,
72 };
73 static struct cdevsw addisk_cdevsw;
74
75 /* prototypes */
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);
81
82 /* misc defines */
83 #define AD_MAX_RETRIES  3
84
85 /* internal vars */
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");
94
95 /* sysctl vars */
96 SYSCTL_DECL(_hw_ata);
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");
103
104 void
105 ad_attach(struct ata_device *atadev)
106 {
107     struct ad_softc *adp;
108     dev_t dev;
109     u_int32_t lbasize;
110     u_int64_t lbasize48;
111
112     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
113         ata_prtdev(atadev, "failed to allocate driver storage\n");
114         return;
115     }
116     adp->device = atadev;
117 #ifdef ATA_STATIC_ID
118     adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
119 #else
120     adp->lun = ata_get_lun(&adp_lun_map);
121 #endif
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);
128
129     lbasize = (u_int32_t)atadev->param->lba_size_1 |
130                ((u_int32_t)atadev->param->lba_size_2 << 16);
131
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;
136
137     /* use the 28bit LBA size if valid */
138     if (atadev->param->cylinders == 16383 && adp->total_secs < lbasize)
139         adp->total_secs = lbasize;
140
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);
145
146     /* use the 48bit LBA size if valid */
147     if (atadev->param->support.address48 && lbasize48 > 268435455)
148         adp->total_secs = lbasize48;
149     
150     ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
151
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));
156
157         if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
158                          0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
159         adp->transfersize *= secsperint;
160     }
161
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");
166
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");
172     }
173     else {
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");
177     }
178
179     /* use DMA if allowed and if drive/controller supports it */
180     if (ata_dma)
181         ata_dmainit(atadev, ata_pmode(atadev->param),
182                     ata_wmode(atadev->param), ata_umode(atadev->param));
183     else
184         ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
185
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");
197     }
198
199     ATA_UNLOCK_CH(atadev->channel);
200
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);
205
206     dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw, &addisk_cdevsw);
207     dev->si_drv1 = adp;
208     dev->si_iosize_max = adp->max_iosize;
209     adp->dev = dev;
210
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;
215
216     atadev->driver = adp;
217     atadev->flags = 0;
218
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;
222     else
223         ad_print(adp);
224 }
225
226 void
227 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
228 {
229     struct ad_softc *adp = atadev->driver;
230     struct ad_request *request;
231     struct bio *bp;
232
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)
238             continue;
239         TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
240         biofinish(request->bp, NULL, ENXIO);
241         ad_free(request);
242     }
243     ata_dmafree(atadev);
244     while ((bp = bioq_first(&adp->queue))) {
245         bioq_remove(&adp->queue, bp); 
246         biofinish(bp, NULL, ENXIO);
247     }
248     disk_invalidate(&adp->disk);
249     disk_destroy(adp->dev);
250     devstat_remove_entry(&adp->stats);
251     if (flush) {
252         if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
253             ata_prtdev(atadev, "flushing cache on detach failed\n");
254     }
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;
260     atadev->flags = 0;
261     free(adp, M_AD);
262 }
263
264 static int
265 adopen(dev_t dev, int flags, int fmt, struct thread *td)
266 {
267     struct ad_softc *adp = dev->si_drv1;
268
269     if (adp->flags & AD_F_RAID_SUBDISK)
270         return EBUSY;
271     return 0;
272 }
273
274 static int
275 adclose(dev_t dev, int flags, int fmt, struct thread *td)
276 {
277     struct ad_softc *adp = dev->si_drv1;
278
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);
283     return 0;
284 }
285
286 static void 
287 adstrategy(struct bio *bp)
288 {
289     struct ad_softc *adp = bp->bio_dev->si_drv1;
290     int s;
291
292     if (adp->device->flags & ATA_D_DETACHING) {
293         biofinish(bp, NULL, ENXIO);
294         return;
295     }
296     s = splbio();
297     bioqdisksort(&adp->queue, bp);
298     splx(s);
299     ata_start(adp->device->channel);
300 }
301
302 static int
303 addump(dev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length)
304 {
305     struct ad_softc *adp = dev->si_drv1;
306     struct ad_request request;
307     static int once;
308
309     if (!adp)
310         return ENXIO;
311
312     if (!once) {
313         /* force PIO mode for dumps */
314         adp->device->mode = ATA_PIO;
315         ata_reinit(adp->device->channel);
316         once = 1;
317     }
318
319     if (length > 0) {
320         bzero(&request, sizeof(struct ad_request));
321         request.softc = adp;
322         request.blockaddr = offset / DEV_BSIZE;
323         request.bytecount = length;
324         request.data = virtual;
325
326         while (request.bytecount > 0) {
327             ad_transfer(&request);
328             if (request.flags & ADR_F_ERROR)
329                 return EIO;
330             request.donecount += request.currentsize;
331             request.bytecount -= request.currentsize;
332             DELAY(20);
333         }
334     } else {
335         if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
336             ata_prtdev(adp->device, "timeout waiting for final ready\n");
337     }
338     return 0;
339 }
340
341 void
342 ad_start(struct ata_device *atadev)
343 {
344     struct ad_softc *adp = atadev->driver;
345     struct bio *bp = bioq_first(&adp->queue);
346     struct ad_request *request;
347     int tag = 0;
348
349     if (!bp)
350         return;
351
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])
355             tag++;
356         if (tag > adp->num_tags )
357             return;
358     }
359
360     if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
361         ata_prtdev(atadev, "out of memory in start\n");
362         return;
363     }
364
365     /* setup request */
366     request->softc = adp;
367     request->bp = bp;
368     request->blockaddr = bp->bio_pblkno;
369     request->bytecount = bp->bio_bcount;
370     request->data = bp->bio_data;
371     request->tag = tag;
372     if (bp->bio_cmd == BIO_READ) 
373         request->flags |= ADR_F_READ;
374
375     if (adp->device->mode >= ATA_DMA && ata_dmaalloc(atadev))
376         adp->device->mode = ATA_PIO;
377
378     /* insert in tag array */
379     adp->tags[tag] = request;
380
381     /* remove from drive queue */
382     bioq_remove(&adp->queue, bp); 
383
384     /* link onto controller queue */
385     TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
386 }
387
388 int
389 ad_transfer(struct ad_request *request)
390 {
391     struct ad_softc *adp;
392     u_int64_t lba;
393     u_int32_t count;
394     u_int8_t cmd;
395     int flags = ATA_IMMEDIATE;
396
397     /* get request params */
398     adp = request->softc;
399
400     /* calculate transfer details */
401     lba = request->blockaddr + (request->donecount / DEV_BSIZE);
402
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);
407    
408     if (request->donecount == 0) {
409
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);
415         }
416         else
417             count = howmany(request->bytecount, DEV_BSIZE);
418
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;
423         }
424
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;
429
430             lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
431             adp->device->flags |= ATA_D_USE_CHS;
432         }
433
434         devstat_start_transaction(&adp->stats);
435
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;
442
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;
447
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;
452                 }
453                 if (ata_wait(adp->device, ATA_S_READY)) {
454                     ata_prtdev(adp->device, "timeout waiting for READY\n");
455                     goto transfer_failed;
456                 }
457                 adp->outstanding++;
458
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) &
462                     ATA_I_RELEASE)
463                     return ad_service(adp, 1);
464             }
465             else {
466                 cmd = (request->flags & ADR_F_READ) ?
467                     ATA_C_READ_DMA : ATA_C_WRITE_DMA;
468
469                 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
470                     ata_prtdev(adp->device, "error executing command");
471                     goto transfer_failed;
472                 }
473 #if 0
474                 /*
475                  * wait for data transfer phase
476                  *
477                  * well this should be here acording to specs, but older
478                  * promise controllers doesn't like it, they lockup!
479                  */
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;
483                 }
484 #endif
485             }
486
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;
491         }
492
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;
496
497         /* just plain old single sector transfer */
498         else
499             cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
500
501         if (ata_command(adp->device, cmd, lba, count, 0, flags)){
502             ata_prtdev(adp->device, "error executing command");
503             goto transfer_failed;
504         }
505     }
506    
507     /* calculate this transfer length */
508     request->currentsize = min(request->bytecount, adp->transfersize);
509
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;
513
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;
518     }
519
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));
525     else
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;
530
531 transfer_failed:
532     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
533     ad_invalidatequeue(adp, request);
534
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);
538     else {
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);
544         ad_free(request);
545     }
546     ata_reinit(adp->device->channel);
547     return ATA_OP_CONTINUES;
548 }
549
550 int
551 ad_interrupt(struct ad_request *request)
552 {
553     struct ad_softc *adp = request->softc;
554     int dma_stat = 0;
555
556     /* finish DMA transfer */
557     if (request->flags & ADR_F_DMA_USED)
558         dma_stat = ata_dmadone(adp->device);
559
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);
564
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);
573
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);
579
580             if (request->retries++ < AD_MAX_RETRIES)
581                 printf(" retrying\n");
582             else {
583                 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
584                 printf(" falling back to PIO mode\n");
585             }
586             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
587             return ATA_OP_FINISHED;
588         }
589
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;
599         }
600
601         request->flags |= ADR_F_ERROR;
602         printf(" status=%02x error=%02x\n", 
603                adp->device->channel->status, adp->device->channel->error);
604     }
605
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");
609
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) {
613
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");
618
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;
622         }
623         else {
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 /
629                               sizeof(int16_t));
630             else
631                 ATA_INSL_STRM(adp->device->channel->r_io, ATA_DATA,
632                               (void*)((uintptr_t)request->data +
633                               request->donecount), request->currentsize /
634                               sizeof(int32_t));
635         }
636     }
637
638     /* finish up transfer */
639     if (request->flags & ADR_F_ERROR) {
640         request->bp->bio_error = EIO;
641         request->bp->bio_flags |= BIO_ERROR;
642     } 
643     else {
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;
649         }
650     }
651
652     /* disarm timeout for this transfer */
653     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
654
655     request->bp->bio_resid = request->bytecount;
656
657     biofinish(request->bp, &adp->stats, 0);
658     ad_free(request);
659     adp->outstanding--;
660
661     /* check for SERVICE */
662     return ad_service(adp, 1);
663 }
664
665 int
666 ad_service(struct ad_softc *adp, int change)
667 {
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;
671
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)
677                 device = ATA_SLAVE;
678         }
679         else {
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)
684                 device = ATA_MASTER;
685         }
686         if (device != adp->device->unit &&
687             ((struct ad_softc *)
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;
692             DELAY(1);
693         }
694     }
695     adp->device->channel->status =
696         ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
697  
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;
703         int tag;
704
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;
712         }
713
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;
719         }
720
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;
729         }
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;
735         }
736         ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
737         adp->device->channel->running = request;
738         request->serv++;
739
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;
747         }
748         ata_dmastart(adp->device, request->data, request->bytecount,
749                      request->flags & ADR_F_READ);
750         return ATA_OP_CONTINUES;
751     }
752     return ATA_OP_FINISHED;
753 }
754
755 static void
756 ad_free(struct ad_request *request)
757 {
758     request->softc->tags[request->tag] = NULL;
759     free(request, M_AD);
760 }
761
762 static void
763 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
764 {
765     /* if tags in use invalidate all other outstanding transfers */
766     if (adp->flags & AD_F_TAG_ENABLED) {
767         struct ad_request *tmpreq;
768         int tag;
769
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)
775                 continue;
776             untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
777             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
778         }
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");
783     }
784 }
785
786 static int
787 ad_tagsupported(struct ad_softc *adp)
788 {
789     switch (adp->device->channel->chiptype) {
790     case 0x0d30105a: /* Promises before TX2 doesn't work with tagged queuing */
791     case 0x0d38105a:
792     case 0x4d30105a:  
793     case 0x4d33105a:
794     case 0x4d38105a:
795         return 0;
796     }
797
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) {
801
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;
805             return 1;
806         }
807
808         /* IBM DJNA series has broken tags, corrupts data */
809         if (!strncmp(adp->device->param->model, "IBM-DJNA", 8)) 
810             return 0;
811
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))
815             return 1;
816
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)))
821                 return 1;
822     }
823     return 0;
824 }
825
826 static void
827 ad_timeout(struct ad_request *request)
828 {
829     struct ad_softc *adp = request->softc;
830
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);
836
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;
844         }
845     }
846
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);
850     } 
851     else {
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);
856         ad_free(request);
857     }
858     ata_reinit(adp->device->channel);
859 }
860
861 void
862 ad_reinit(struct ata_device *atadev)
863 {
864     struct ad_softc *adp = atadev->driver;
865
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));
874     else
875         ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
876 }
877
878 void
879 ad_print(struct ad_softc *adp) 
880 {
881     if (bootverbose) {
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");
887
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);
896
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));
901
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);
906
907     }
908     else
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));
920 }
921
922 static int
923 ad_version(u_int16_t version)
924 {
925     int bit;
926
927     if (version == 0xffff)
928         return 0;
929     for (bit = 15; bit >= 0; bit--)
930         if (version & (1<<bit))
931             return bit;
932     return 0;
933 }