]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-disk.c
This commit was generated by cvs2svn to compensate for changes in r99060,
[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     bioq_init(&adp->queue);
127
128     lbasize = (u_int32_t)atadev->param->lba_size_1 |
129                ((u_int32_t)atadev->param->lba_size_2 << 16);
130
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;
135
136     /* use the 28bit LBA size if valid */
137     if (atadev->param->cylinders == 16383 && adp->total_secs < lbasize)
138         adp->total_secs = lbasize;
139
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);
144
145     /* use the 48bit LBA size if valid */
146     if (atadev->param->support.address48 && lbasize48 > 268435455)
147         adp->total_secs = lbasize48;
148     
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));
154
155         if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
156                          0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
157         adp->transfersize *= secsperint;
158     }
159
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");
164
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");
170     }
171     else {
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");
175     }
176
177     /* use DMA if allowed and if drive/controller supports it */
178     if (ata_dma)
179         ata_dmainit(atadev, ata_pmode(atadev->param),
180                     ata_wmode(atadev->param), ata_umode(atadev->param));
181     else
182         ata_dmainit(atadev, ata_pmode(atadev->param), -1, -1);
183
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");
195     }
196
197     ATA_UNLOCK_CH(atadev->channel);
198
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);
203
204     dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw, &addisk_cdevsw);
205     dev->si_drv1 = adp;
206     dev->si_iosize_max = 256 * DEV_BSIZE;
207     adp->dev = dev;
208
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;
217
218     atadev->driver = adp;
219     atadev->flags = 0;
220
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;
224     else
225         ad_print(adp);
226 }
227
228 void
229 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
230 {
231     struct ad_softc *adp = atadev->driver;
232     struct ad_request *request;
233     struct bio *bp;
234
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)
240             continue;
241         TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
242         biofinish(request->bp, NULL, ENXIO);
243         ad_free(request);
244     }
245     ata_dmafree(atadev);
246     while ((bp = bioq_first(&adp->queue))) {
247         bioq_remove(&adp->queue, bp); 
248         biofinish(bp, NULL, ENXIO);
249     }
250     disk_invalidate(&adp->disk);
251     disk_destroy(adp->dev);
252     devstat_remove_entry(&adp->stats);
253     if (flush) {
254         if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
255             ata_prtdev(atadev, "flushing cache on detach failed\n");
256     }
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;
262     atadev->flags = 0;
263     free(adp, M_AD);
264 }
265
266 static int
267 adopen(dev_t dev, int flags, int fmt, struct thread *td)
268 {
269     struct ad_softc *adp = dev->si_drv1;
270
271     if (adp->flags & AD_F_RAID_SUBDISK)
272         return EBUSY;
273     return 0;
274 }
275
276 static int
277 adclose(dev_t dev, int flags, int fmt, struct thread *td)
278 {
279     struct ad_softc *adp = dev->si_drv1;
280
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);
285     return 0;
286 }
287
288 static void 
289 adstrategy(struct bio *bp)
290 {
291     struct ad_softc *adp = bp->bio_dev->si_drv1;
292     int s;
293
294     if (adp->device->flags & ATA_D_DETACHING) {
295         biofinish(bp, NULL, ENXIO);
296         return;
297     }
298     s = splbio();
299     bioqdisksort(&adp->queue, bp);
300     splx(s);
301     ata_start(adp->device->channel);
302 }
303
304 static int
305 addump(dev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length)
306 {
307     struct ad_softc *adp = dev->si_drv1;
308     struct ad_request request;
309     static int once;
310
311     if (!adp)
312         return ENXIO;
313
314     if (!once) {
315         /* force PIO mode for dumps */
316         adp->device->mode = ATA_PIO;
317         ata_reinit(adp->device->channel);
318         once = 1;
319     }
320
321     if (length > 0) {
322         bzero(&request, sizeof(struct ad_request));
323         request.softc = adp;
324         request.blockaddr = offset / DEV_BSIZE;
325         request.bytecount = length;
326         request.data = virtual;
327
328         while (request.bytecount > 0) {
329             ad_transfer(&request);
330             if (request.flags & ADR_F_ERROR)
331                 return EIO;
332             request.donecount += request.currentsize;
333             request.bytecount -= request.currentsize;
334             DELAY(20);
335         }
336     } else {
337         if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
338             ata_prtdev(adp->device, "timeout waiting for final ready\n");
339     }
340     return 0;
341 }
342
343 void
344 ad_start(struct ata_device *atadev)
345 {
346     struct ad_softc *adp = atadev->driver;
347     struct bio *bp = bioq_first(&adp->queue);
348     struct ad_request *request;
349     int tag = 0;
350
351     if (!bp)
352         return;
353
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])
357             tag++;
358         if (tag > adp->num_tags )
359             return;
360     }
361
362     if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
363         ata_prtdev(atadev, "out of memory in start\n");
364         return;
365     }
366
367     /* setup request */
368     request->softc = adp;
369     request->bp = bp;
370     request->blockaddr = bp->bio_pblkno;
371     request->bytecount = bp->bio_bcount;
372     request->data = bp->bio_data;
373     request->tag = tag;
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;
379     }
380
381     /* insert in tag array */
382     adp->tags[tag] = request;
383
384     /* remove from drive queue */
385     bioq_remove(&adp->queue, bp); 
386
387     /* link onto controller queue */
388     TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
389 }
390
391 int
392 ad_transfer(struct ad_request *request)
393 {
394     struct ad_softc *adp;
395     u_int64_t lba;
396     u_int32_t count, max_count;
397     u_int8_t cmd;
398     int flags = ATA_IMMEDIATE;
399
400     /* get request params */
401     adp = request->softc;
402
403     /* calculate transfer details */
404     lba = request->blockaddr + (request->donecount / DEV_BSIZE);
405    
406     if (request->donecount == 0) {
407
408         /* start timeout for this transfer */
409         if (dumping)
410             request->timeout_handle.callout = NULL;
411         else
412             request->timeout_handle = 
413                 timeout((timeout_t*)ad_timeout, request, 10 * hz);
414
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);
421             count = max_count;
422         }
423
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;
428
429             lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
430             adp->device->flags |= ATA_D_USE_CHS;
431         }
432
433         /* setup first transfer length */
434         request->currentsize = min(request->bytecount, adp->transfersize);
435
436         devstat_start_transaction(&adp->stats);
437
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;
444
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;
449
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;
454                 }
455                 if (ata_wait(adp->device, ATA_S_READY)) {
456                     ata_prtdev(adp->device, "timeout waiting for READY\n");
457                     goto transfer_failed;
458                 }
459                 adp->outstanding++;
460
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) &
464                     ATA_I_RELEASE)
465                     return ad_service(adp, 1);
466             }
467             else {
468                 cmd = (request->flags & ADR_F_READ) ?
469                     ATA_C_READ_DMA : ATA_C_WRITE_DMA;
470
471                 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
472                     ata_prtdev(adp->device, "error executing command");
473                     goto transfer_failed;
474                 }
475 #if 0
476                 /*
477                  * wait for data transfer phase
478                  *
479                  * well this should be here acording to specs, but older
480                  * promise controllers doesn't like it, they lockup!
481                  */
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;
485                 }
486 #endif
487             }
488
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;
493         }
494
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;
498
499         /* just plain old single sector transfer */
500         else
501             cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
502
503         if (ata_command(adp->device, cmd, lba, count, 0, flags)){
504             ata_prtdev(adp->device, "error executing command");
505             goto transfer_failed;
506         }
507     }
508    
509     /* calculate this transfer length */
510     request->currentsize = min(request->bytecount, adp->transfersize);
511
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;
515
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;
520     }
521
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));
527     else
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;
532
533 transfer_failed:
534     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
535     ad_invalidatequeue(adp, request);
536     printf(" - resetting\n");
537
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);
541     else {
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);
547         ad_free(request);
548     }
549     ata_reinit(adp->device->channel);
550     return ATA_OP_CONTINUES;
551 }
552
553 int
554 ad_interrupt(struct ad_request *request)
555 {
556     struct ad_softc *adp = request->softc;
557     int dma_stat = 0;
558
559     /* finish DMA transfer */
560     if (request->flags & ADR_F_DMA_USED)
561         dma_stat = ata_dmadone(adp->device);
562
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),
567                 &adp->disk.d_label);
568
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),
577                 &adp->disk.d_label);
578
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);
584
585             if (request->retries++ < AD_MAX_RETRIES)
586                 printf(" retrying\n");
587             else {
588                 ata_dmainit(adp->device, ata_pmode(adp->device->param), -1, -1);
589                 printf(" falling back to PIO mode\n");
590             }
591             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
592             return ATA_OP_FINISHED;
593         }
594
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;
604         }
605
606         request->flags |= ADR_F_ERROR;
607         printf(" status=%02x error=%02x\n", 
608                adp->device->channel->status, adp->device->channel->error);
609     }
610
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");
614
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) {
618
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");
623
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;
627         }
628         else {
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 /
634                               sizeof(int16_t));
635             else
636                 ATA_INSL_STRM(adp->device->channel->r_io, ATA_DATA,
637                               (void*)((uintptr_t)request->data +
638                               request->donecount), request->currentsize /
639                               sizeof(int32_t));
640         }
641     }
642
643     /* finish up transfer */
644     if (request->flags & ADR_F_ERROR) {
645         request->bp->bio_error = EIO;
646         request->bp->bio_flags |= BIO_ERROR;
647     } 
648     else {
649         request->bytecount -= request->currentsize;
650         request->donecount += request->currentsize;
651         if (request->bytecount > 0) {
652             ad_transfer(request);
653             return ATA_OP_CONTINUES;
654         }
655     }
656
657     /* disarm timeout for this transfer */
658     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
659
660     request->bp->bio_resid = request->bytecount;
661
662     biofinish(request->bp, &adp->stats, 0);
663     ad_free(request);
664     adp->outstanding--;
665
666     /* check for SERVICE (tagged operations only) */
667     return ad_service(adp, 1);
668 }
669
670 int
671 ad_service(struct ad_softc *adp, int change)
672 {
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;
676
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)
682                 device = ATA_SLAVE;
683         }
684         else {
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)
689                 device = ATA_MASTER;
690         }
691         if (device != adp->device->unit &&
692             ((struct ad_softc *)
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;
697             DELAY(1);
698         }
699     }
700     adp->device->channel->status =
701         ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
702  
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;
708         int tag;
709
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;
717         }
718
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;
724         }
725
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;
734         }
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;
740         }
741         adp->device->channel->active = ATA_ACTIVE_ATA;
742         adp->device->channel->running = request;
743         request->serv++;
744
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;
752         }
753         ata_dmastart(adp->device, request->data, request->bytecount,
754                      request->flags & ADR_F_READ);
755         return ATA_OP_CONTINUES;
756     }
757     return ATA_OP_FINISHED;
758 }
759
760 static void
761 ad_free(struct ad_request *request)
762 {
763     request->softc->tags[request->tag] = NULL;
764     free(request, M_AD);
765 }
766
767 static void
768 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
769 {
770     /* if tags used invalidate all other tagged transfers */
771     if (adp->flags & AD_F_TAG_ENABLED) {
772         struct ad_request *tmpreq;
773         int tag;
774
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)
780                 continue;
781             untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
782             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
783         }
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;
788     }
789 }
790
791 static int
792 ad_tagsupported(struct ad_softc *adp)
793 {
794     const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
795     int i = 0;
796
797     switch (adp->device->channel->chiptype) {
798     case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
799     case 0x4d38105a:
800     case 0x0d30105a:
801     case 0x4d30105a:  
802         return 0;
803     }
804
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])))
810                 return 1;
811             i++;
812         }
813         /* 
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)
817          */
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     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);
835
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;
843         }
844     }
845
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);
849     }
850     else {
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);
855         ad_free(request);
856     }
857     ata_reinit(adp->device->channel);
858 }
859
860 void
861 ad_reinit(struct ata_device *atadev)
862 {
863     struct ad_softc *adp = atadev->driver;
864
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));
873     else
874         ata_dmainit(atadev, ata_pmode(adp->device->param), -1, -1);
875 }
876
877 void
878 ad_print(struct ad_softc *adp) 
879 {
880     if (bootverbose) {
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");
886
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);
895
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));
900
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);
905
906     }
907     else
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));
919 }
920
921 static int
922 ad_version(u_int16_t version)
923 {
924     int bit;
925
926     if (version == 0xffff)
927         return 0;
928     for (bit = 15; bit >= 0; bit--)
929         if (version & (1<<bit))
930             return bit;
931     return 0;
932 }