Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / disk / 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: src/sys/dev/ata/ata-disk.c,v 1.60.2.24 2003/01/30 07:19:59 sos Exp $
29  * $DragonFly: src/sys/dev/disk/ata/ata-disk.c,v 1.2 2003/06/17 04:28:22 dillon Exp $
30  */
31
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ata.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/buf.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/disk.h>
42 #include <sys/devicestat.h>
43 #include <sys/cons.h>
44 #include <sys/sysctl.h>
45 #include <sys/syslog.h>
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <machine/md_var.h>
49 #include <machine/bus.h>
50 #include <machine/clock.h>
51 #include <sys/rman.h>
52 #include <dev/ata/ata-all.h>
53 #include <dev/ata/ata-disk.h>
54 #include <dev/ata/ata-raid.h>
55
56 /* device structures */
57 static d_open_t         adopen;
58 static d_close_t        adclose;
59 static d_strategy_t     adstrategy;
60 static d_dump_t         addump;
61 static struct cdevsw ad_cdevsw = {
62         /* open */      adopen,
63         /* close */     adclose,
64         /* read */      physread,
65         /* write */     physwrite,
66         /* ioctl */     noioctl,
67         /* poll */      nopoll,
68         /* mmap */      nommap,
69         /* strategy */  adstrategy,
70         /* name */      "ad",
71         /* maj */       116,
72         /* dump */      addump,
73         /* psize */     nopsize,
74         /* flags */     D_DISK,
75         /* bmaj */      30
76 };
77 static struct cdevsw addisk_cdevsw;
78
79 /* prototypes */
80 static void ad_invalidatequeue(struct ad_softc *, struct ad_request *);
81 static int ad_tagsupported(struct ad_softc *);
82 static void ad_timeout(struct ad_request *);
83 static void ad_free(struct ad_request *);
84 static int ad_version(u_int16_t);
85
86 /* misc defines */
87 #define AD_MAX_RETRIES  3
88
89 /* internal vars */
90 static u_int32_t adp_lun_map = 0;
91 static int ata_dma = 1;
92 static int ata_wc = 1;
93 static int ata_tags = 0; 
94 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
95 TUNABLE_INT("hw.ata.wc", &ata_wc);
96 TUNABLE_INT("hw.ata.tags", &ata_tags);
97 static MALLOC_DEFINE(M_AD, "AD driver", "ATA disk driver");
98
99 /* sysctl vars */
100 SYSCTL_DECL(_hw_ata);
101 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RD, &ata_dma, 0,
102            "ATA disk DMA mode control");
103 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RD, &ata_wc, 0,
104            "ATA disk write caching");
105 SYSCTL_INT(_hw_ata, OID_AUTO, tags, CTLFLAG_RD, &ata_tags, 0,
106            "ATA disk tagged queuing support");
107
108 void
109 ad_attach(struct ata_device *atadev)
110 {
111     struct ad_softc *adp;
112     dev_t dev;
113
114     if (!(adp = malloc(sizeof(struct ad_softc), M_AD, M_NOWAIT | M_ZERO))) {
115         ata_prtdev(atadev, "failed to allocate driver storage\n");
116         return;
117     }
118     adp->device = atadev;
119 #ifdef ATA_STATIC_ID
120     adp->lun = (device_get_unit(atadev->channel->dev)<<1)+ATA_DEV(atadev->unit);
121 #else
122     adp->lun = ata_get_lun(&adp_lun_map);
123 #endif
124     ata_set_name(atadev, "ad", adp->lun);
125     adp->heads = atadev->param->heads;
126     adp->sectors = atadev->param->sectors;
127     adp->total_secs = atadev->param->cylinders * adp->heads * adp->sectors;     
128     bufq_init(&adp->queue);
129
130     /* does this device need oldstyle CHS addressing */
131     if (!ad_version(atadev->param->version_major) || 
132         !(atadev->param->atavalid & ATA_FLAG_54_58) || !atadev->param->lba_size)
133         adp->flags |= AD_F_CHS_USED;
134
135     /* use the 28bit LBA size if valid */
136     if (atadev->param->cylinders == 16383 &&
137         adp->total_secs < atadev->param->lba_size)
138         adp->total_secs = atadev->param->lba_size;
139
140     /* use the 48bit LBA size if valid */
141     if (atadev->param->support.address48 &&
142         atadev->param->lba_size48 > 268435455)
143         adp->total_secs = atadev->param->lba_size48;
144     
145     ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
146     /* use multiple sectors/interrupt if device supports it */
147     adp->transfersize = DEV_BSIZE;
148     if (ad_version(atadev->param->version_major)) {
149         int secsperint = max(1, min(atadev->param->sectors_intr, 16));
150
151         if (!ata_command(atadev, ATA_C_SET_MULTI, 0, secsperint,
152                          0, ATA_WAIT_INTR) && !ata_wait(atadev, 0))
153         adp->transfersize *= secsperint;
154     }
155
156     /* enable read caching if not default on device */
157     if (ata_command(atadev, ATA_C_SETFEATURES,
158                     0, 0, ATA_C_F_ENAB_RCACHE, ATA_WAIT_INTR))
159         ata_prtdev(atadev, "enabling readahead cache failed\n");
160
161     /* enable write caching if allowed and not default on device */
162     if (ata_wc || (ata_tags && ad_tagsupported(adp))) {
163         if (ata_command(atadev, ATA_C_SETFEATURES,
164                         0, 0, ATA_C_F_ENAB_WCACHE, ATA_WAIT_INTR))
165             ata_prtdev(atadev, "enabling write cache failed\n");
166     }
167     else {
168         if (ata_command(atadev, ATA_C_SETFEATURES,
169                         0, 0, ATA_C_F_DIS_WCACHE, ATA_WAIT_INTR))
170             ata_prtdev(atadev, "disabling write cache failed\n");
171     }
172
173     /* use DMA if allowed and if drive/controller supports it */
174     if (ata_dma)
175         ata_dmainit(atadev->channel, atadev->unit, ata_pmode(atadev->param), 
176                     ata_wmode(atadev->param), ata_umode(atadev->param));
177     else
178         ata_dmainit(atadev->channel, atadev->unit,
179                     ata_pmode(atadev->param), -1, -1);
180
181     /* use tagged queueing if allowed and supported */
182     if (ata_tags && ad_tagsupported(adp)) {
183         adp->num_tags = atadev->param->queuelen;
184         adp->flags |= AD_F_TAG_ENABLED;
185         adp->device->channel->flags |= ATA_QUEUED;
186         if (ata_command(atadev, ATA_C_SETFEATURES,
187                         0, 0, ATA_C_F_DIS_RELIRQ, ATA_WAIT_INTR))
188             ata_prtdev(atadev, "disabling release interrupt failed\n");
189         if (ata_command(atadev, ATA_C_SETFEATURES,
190                         0, 0, ATA_C_F_DIS_SRVIRQ, ATA_WAIT_INTR))
191             ata_prtdev(atadev, "disabling service interrupt failed\n");
192     }
193
194     ATA_UNLOCK_CH(atadev->channel);
195
196     devstat_add_entry(&adp->stats, "ad", adp->lun, DEV_BSIZE,
197                       DEVSTAT_NO_ORDERED_TAGS,
198                       DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
199                       DEVSTAT_PRIORITY_DISK);
200
201     dev = disk_create(adp->lun, &adp->disk, 0, &ad_cdevsw, &addisk_cdevsw);
202     dev->si_drv1 = adp;
203     dev->si_iosize_max = 256 * DEV_BSIZE;
204     adp->dev = dev;
205
206     /* construct the disklabel */
207     bzero(&adp->disk.d_label, sizeof(struct disklabel));
208     adp->disk.d_label.d_secsize = DEV_BSIZE;
209     adp->disk.d_label.d_nsectors = adp->sectors;
210     adp->disk.d_label.d_ntracks = adp->heads;
211     adp->disk.d_label.d_ncylinders = adp->total_secs/(adp->heads*adp->sectors);
212     adp->disk.d_label.d_secpercyl = adp->sectors * adp->heads;
213     adp->disk.d_label.d_secperunit = adp->total_secs;
214
215     atadev->driver = adp;
216     atadev->flags = 0;
217
218     /* if this disk belongs to an ATA RAID dont print the probe */
219     if (ata_raiddisk_attach(adp))
220         adp->flags |= AD_F_RAID_SUBDISK;
221     else {
222         if (atadev->driver) {
223             ad_print(adp);
224             ata_enclosure_print(atadev);
225         }
226     }
227 }
228
229 void
230 ad_detach(struct ata_device *atadev, int flush) /* get rid of flush XXX SOS */
231 {
232     struct ad_softc *adp = atadev->driver;
233     struct ad_request *request;
234     struct buf *bp;
235
236     atadev->flags |= ATA_D_DETACHING;
237     ata_prtdev(atadev, "removed from configuration\n");
238     ad_invalidatequeue(adp, NULL);
239     TAILQ_FOREACH(request, &atadev->channel->ata_queue, chain) {
240         if (request->softc != adp)
241             continue;
242         TAILQ_REMOVE(&atadev->channel->ata_queue, request, chain);
243         request->bp->b_error = ENXIO;
244         request->bp->b_flags |= B_ERROR;
245         biodone(request->bp);
246         ad_free(request);
247     }
248     while ((bp = bufq_first(&adp->queue))) {
249         bufq_remove(&adp->queue, bp); 
250         bp->b_error = ENXIO;
251         bp->b_flags |= B_ERROR;
252         biodone(bp);
253     }
254     disk_invalidate(&adp->disk);
255     disk_destroy(adp->dev);
256     devstat_remove_entry(&adp->stats);
257     if (flush) {
258         if (ata_command(atadev, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
259             ata_prtdev(atadev, "flushing cache on detach failed\n");
260     }
261     if (adp->flags & AD_F_RAID_SUBDISK)
262         ata_raiddisk_detach(adp);
263     ata_free_name(atadev);
264     ata_free_lun(&adp_lun_map, adp->lun);
265     atadev->driver = NULL;
266     atadev->flags = 0;
267     free(adp, M_AD);
268 }
269
270 static int
271 adopen(dev_t dev, int flags, int fmt, struct proc *p)
272 {
273     struct ad_softc *adp = dev->si_drv1;
274
275     if (adp->flags & AD_F_RAID_SUBDISK)
276         return EBUSY;
277     return 0;
278 }
279
280 static int
281 adclose(dev_t dev, int flags, int fmt, struct proc *p)
282 {
283     struct ad_softc *adp = dev->si_drv1;
284
285     ATA_SLEEPLOCK_CH(adp->device->channel, ATA_CONTROL);
286     if (ata_command(adp->device, ATA_C_FLUSHCACHE, 0, 0, 0, ATA_WAIT_READY))
287         ata_prtdev(adp->device, "flushing cache on close failed\n");
288     ATA_UNLOCK_CH(adp->device->channel);
289     return 0;
290 }
291
292 static void 
293 adstrategy(struct buf *bp)
294 {
295     struct ad_softc *adp = bp->b_dev->si_drv1;
296     int s;
297
298     if (adp->device->flags & ATA_D_DETACHING) {
299         bp->b_error = ENXIO;
300         bp->b_flags |= B_ERROR;
301         biodone(bp);
302         return;
303     }
304     s = splbio();
305     bufqdisksort(&adp->queue, bp);
306     splx(s);
307     ata_start(adp->device->channel);
308 }
309
310 int
311 addump(dev_t dev)
312 {
313     struct ad_softc *adp = dev->si_drv1;
314     struct ad_request request;
315     u_int count, blkno, secsize;
316     vm_offset_t addr = 0;
317     long blkcnt;
318     int dumppages = MAXDUMPPGS;
319     int error;
320     int i;
321
322     if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
323         return error;
324         
325     if (!adp)
326         return ENXIO;
327
328     /* force PIO mode for dumps */
329     adp->device->mode = ATA_PIO;
330     ata_reinit(adp->device->channel);
331
332     blkcnt = howmany(PAGE_SIZE, secsize);
333
334     while (count > 0) {
335         caddr_t va = NULL;
336         DELAY(1000);
337
338         if ((count / blkcnt) < dumppages)
339             dumppages = count / blkcnt;
340
341         for (i = 0; i < dumppages; ++i) {
342             vm_offset_t a = addr + (i * PAGE_SIZE);
343             if (is_physical_memory(a))
344                 va = pmap_kenter_temporary(trunc_page(a), i);
345             else
346                 va = pmap_kenter_temporary(trunc_page(0), i);
347         }
348
349         bzero(&request, sizeof(struct ad_request));
350         request.softc = adp;
351         request.blockaddr = blkno;
352         request.bytecount = PAGE_SIZE * dumppages;
353         request.data = va;
354
355         while (request.bytecount > 0) {
356             ad_transfer(&request);
357             if (request.flags & ADR_F_ERROR)
358                 return EIO;
359             request.donecount += request.currentsize;
360             request.bytecount -= request.currentsize;
361             DELAY(20);
362         }
363
364         if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
365             return EINTR;
366
367         blkno += blkcnt * dumppages;
368         count -= blkcnt * dumppages;
369         addr += PAGE_SIZE * dumppages;
370     }
371
372     if (ata_wait(adp->device, ATA_S_READY | ATA_S_DSC) < 0)
373         ata_prtdev(adp->device, "timeout waiting for final ready\n");
374     return 0;
375 }
376
377 void
378 ad_start(struct ata_device *atadev)
379 {
380     struct ad_softc *adp = atadev->driver;
381     struct buf *bp = bufq_first(&adp->queue);
382     struct ad_request *request;
383     int tag = 0;
384
385     if (!bp)
386         return;
387
388     /* if tagged queueing enabled get next free tag */
389     if (adp->flags & AD_F_TAG_ENABLED) {
390         while (tag <= adp->num_tags && adp->tags[tag])
391             tag++;
392         if (tag > adp->num_tags )
393             return;
394     }
395
396     if (!(request = malloc(sizeof(struct ad_request), M_AD, M_NOWAIT|M_ZERO))) {
397         ata_prtdev(atadev, "out of memory in start\n");
398         return;
399     }
400
401     /* setup request */
402     request->softc = adp;
403     request->bp = bp;
404     request->blockaddr = bp->b_pblkno;
405     request->bytecount = bp->b_bcount;
406     request->data = bp->b_data;
407     request->tag = tag;
408     if (bp->b_flags & B_READ) 
409         request->flags |= ADR_F_READ;
410     if (adp->device->mode >= ATA_DMA) {
411         if (!(request->dmatab = ata_dmaalloc(atadev->channel, atadev->unit)))
412             adp->device->mode = ATA_PIO;
413     }
414
415     /* insert in tag array */
416     adp->tags[tag] = request;
417
418     /* remove from drive queue */
419     bufq_remove(&adp->queue, bp); 
420
421     /* link onto controller queue */
422     TAILQ_INSERT_TAIL(&atadev->channel->ata_queue, request, chain);
423 }
424
425 int
426 ad_transfer(struct ad_request *request)
427 {
428     struct ad_softc *adp;
429     u_int64_t lba;
430     u_int32_t count, max_count;
431     u_int8_t cmd;
432     int flags = ATA_IMMEDIATE;
433
434     /* get request params */
435     adp = request->softc;
436
437     /* calculate transfer details */
438     lba = request->blockaddr + (request->donecount / DEV_BSIZE);
439    
440     if (request->donecount == 0) {
441
442         /* start timeout for this transfer */
443         if (dumping)
444             request->timeout_handle.callout = NULL;
445         else
446             request->timeout_handle = 
447                 timeout((timeout_t*)ad_timeout, request, 10 * hz);
448
449         /* setup transfer parameters */
450         count = howmany(request->bytecount, DEV_BSIZE);
451         max_count = adp->device->param->support.address48 ? 65536 : 256;
452         if (count > max_count) {
453             ata_prtdev(adp->device,
454                        "count %d size transfers not supported\n", count);
455             count = max_count;
456         }
457
458         if (adp->flags & AD_F_CHS_USED) {
459             int sector = (lba % adp->sectors) + 1;
460             int cylinder = lba / (adp->sectors * adp->heads);
461             int head = (lba % (adp->sectors * adp->heads)) / adp->sectors;
462
463             lba = (sector&0xff) | ((cylinder&0xffff)<<8) | ((head&0xf)<<24);
464             adp->device->flags |= ATA_D_USE_CHS;
465         }
466
467         /* setup first transfer length */
468         request->currentsize = min(request->bytecount, adp->transfersize);
469
470         devstat_start_transaction(&adp->stats);
471
472         /* does this drive & transfer work with DMA ? */
473         request->flags &= ~ADR_F_DMA_USED;
474         if (adp->device->mode >= ATA_DMA &&
475             !ata_dmasetup(adp->device->channel, adp->device->unit,
476                           request->dmatab, request->data, request->bytecount)) {
477             request->flags |= ADR_F_DMA_USED;
478             request->currentsize = request->bytecount;
479
480             /* do we have tags enabled ? */
481             if (adp->flags & AD_F_TAG_ENABLED) {
482                 cmd = (request->flags & ADR_F_READ) ?
483                     ATA_C_READ_DMA_QUEUED : ATA_C_WRITE_DMA_QUEUED;
484
485                 if (ata_command(adp->device, cmd, lba,
486                                 request->tag << 3, count, flags)) {
487                     ata_prtdev(adp->device, "error executing command");
488                     goto transfer_failed;
489                 }
490                 if (ata_wait(adp->device, ATA_S_READY)) {
491                     ata_prtdev(adp->device, "timeout waiting for READY\n");
492                     goto transfer_failed;
493                 }
494                 adp->outstanding++;
495
496                 /* if ATA bus RELEASE check for SERVICE */
497                 if (adp->flags & AD_F_TAG_ENABLED &&
498                     ATA_INB(adp->device->channel->r_io, ATA_IREASON) &
499                     ATA_I_RELEASE)
500                     return ad_service(adp, 1);
501             }
502             else {
503                 cmd = (request->flags & ADR_F_READ) ?
504                     ATA_C_READ_DMA : ATA_C_WRITE_DMA;
505
506                 if (ata_command(adp->device, cmd, lba, count, 0, flags)) {
507                     ata_prtdev(adp->device, "error executing command");
508                     goto transfer_failed;
509                 }
510 #if 0
511                 /*
512                  * wait for data transfer phase
513                  *
514                  * well this should be here acording to specs, but older
515                  * promise controllers doesn't like it, they lockup!
516                  */
517                 if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
518                     ata_prtdev(adp->device, "timeout waiting for data phase\n");
519                     goto transfer_failed;
520                 }
521 #endif
522             }
523
524             /* start transfer, return and wait for interrupt */
525             ata_dmastart(adp->device->channel, adp->device->unit,
526                          request->dmatab, request->flags & ADR_F_READ);
527             return ATA_OP_CONTINUES;
528         }
529
530         /* does this drive support multi sector transfers ? */
531         if (request->currentsize > DEV_BSIZE)
532             cmd = request->flags&ADR_F_READ ? ATA_C_READ_MUL : ATA_C_WRITE_MUL;
533
534         /* just plain old single sector transfer */
535         else
536             cmd = request->flags&ADR_F_READ ? ATA_C_READ : ATA_C_WRITE;
537
538         if (ata_command(adp->device, cmd, lba, count, 0, flags)){
539             ata_prtdev(adp->device, "error executing command");
540             goto transfer_failed;
541         }
542     }
543    
544     /* calculate this transfer length */
545     request->currentsize = min(request->bytecount, adp->transfersize);
546
547     /* if this is a PIO read operation, return and wait for interrupt */
548     if (request->flags & ADR_F_READ)
549         return ATA_OP_CONTINUES;
550
551     /* ready to write PIO data ? */
552     if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) < 0) {
553         ata_prtdev(adp->device, "timeout waiting for DRQ");
554         goto transfer_failed;
555     }
556
557     /* output the data */
558     if (adp->device->channel->flags & ATA_USE_16BIT)
559         ATA_OUTSW(adp->device->channel->r_io, ATA_DATA,
560                   (void *)((uintptr_t)request->data + request->donecount),
561                   request->currentsize / sizeof(int16_t));
562     else
563         ATA_OUTSL(adp->device->channel->r_io, ATA_DATA,
564                   (void *)((uintptr_t)request->data + request->donecount),
565                   request->currentsize / sizeof(int32_t));
566     return ATA_OP_CONTINUES;
567
568 transfer_failed:
569     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
570     ad_invalidatequeue(adp, request);
571     printf(" - resetting\n");
572
573     /* if retries still permit, reinject this request */
574     if (request->retries++ < AD_MAX_RETRIES)
575         TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
576     else {
577         /* retries all used up, return error */
578         request->bp->b_error = EIO;
579         request->bp->b_flags |= B_ERROR;
580         request->bp->b_resid = request->bytecount;
581         devstat_end_transaction_buf(&adp->stats, request->bp);
582         biodone(request->bp);
583         ad_free(request);
584     }
585     ata_reinit(adp->device->channel);
586     return ATA_OP_CONTINUES;
587 }
588
589 int
590 ad_interrupt(struct ad_request *request)
591 {
592     struct ad_softc *adp = request->softc;
593     int dma_stat = 0;
594
595     /* finish DMA transfer */
596     if (request->flags & ADR_F_DMA_USED)
597         dma_stat = ata_dmadone(adp->device->channel);
598
599     /* do we have a corrected soft error ? */
600     if (adp->device->channel->status & ATA_S_CORR)
601         diskerr(request->bp, "soft error (ECC corrected)", LOG_PRINTF,
602                 request->blockaddr + (request->donecount / DEV_BSIZE),
603                 &adp->disk.d_label);
604
605     /* did any real errors happen ? */
606     if ((adp->device->channel->status & ATA_S_ERROR) ||
607         (request->flags & ADR_F_DMA_USED && dma_stat & ATA_BMSTAT_ERROR)) {
608         adp->device->channel->error =
609             ATA_INB(adp->device->channel->r_io, ATA_ERROR);
610         diskerr(request->bp, (adp->device->channel->error & ATA_E_ICRC) ?
611                 "UDMA ICRC error" : "hard error", LOG_PRINTF,
612                 request->blockaddr + (request->donecount / DEV_BSIZE),
613                 &adp->disk.d_label);
614
615         /* if this is a UDMA CRC error, reinject request */
616         if (request->flags & ADR_F_DMA_USED &&
617             adp->device->channel->error & ATA_E_ICRC) {
618             untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
619             ad_invalidatequeue(adp, request);
620
621             if (request->retries++ < AD_MAX_RETRIES)
622                 printf(" retrying\n");
623             else {
624                 ata_dmainit(adp->device->channel, adp->device->unit, 
625                             ata_pmode(adp->device->param), -1, -1);
626                 printf(" falling back to PIO mode\n");
627             }
628             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
629             return ATA_OP_FINISHED;
630         }
631
632         /* if using DMA, try once again in PIO mode */
633         if (request->flags & ADR_F_DMA_USED) {
634             untimeout((timeout_t *)ad_timeout, request,request->timeout_handle);
635             ad_invalidatequeue(adp, request);
636             ata_dmainit(adp->device->channel, adp->device->unit,
637                         ata_pmode(adp->device->param), -1, -1);
638             request->flags |= ADR_F_FORCE_PIO;
639             printf(" trying PIO mode\n");
640             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
641             return ATA_OP_FINISHED;
642         }
643
644         request->flags |= ADR_F_ERROR;
645         printf(" status=%02x error=%02x\n", 
646                adp->device->channel->status, adp->device->channel->error);
647     }
648
649     /* if we arrived here with forced PIO mode, DMA doesn't work right */
650     if (request->flags & ADR_F_FORCE_PIO && !(request->flags & ADR_F_ERROR))
651         ata_prtdev(adp->device, "DMA problem fallback to PIO mode\n");
652
653     /* if this was a PIO read operation, get the data */
654     if (!(request->flags & ADR_F_DMA_USED) &&
655         (request->flags & (ADR_F_READ | ADR_F_ERROR)) == ADR_F_READ) {
656
657         /* ready to receive data? */
658         if ((adp->device->channel->status & (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
659             != (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))
660             ata_prtdev(adp->device, "read interrupt arrived early");
661
662         if (ata_wait(adp->device, (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ)) != 0) {
663             ata_prtdev(adp->device, "read error detected (too) late");
664             request->flags |= ADR_F_ERROR;
665         }
666         else {
667             /* data ready, read in */
668             if (adp->device->channel->flags & ATA_USE_16BIT)
669                 ATA_INSW(adp->device->channel->r_io, ATA_DATA,
670                          (void*)((uintptr_t)request->data + request->donecount),
671                          request->currentsize / sizeof(int16_t));
672             else
673                 ATA_INSL(adp->device->channel->r_io, ATA_DATA,
674                          (void*)((uintptr_t)request->data + request->donecount),
675                          request->currentsize / sizeof(int32_t));
676         }
677     }
678
679     /* finish up transfer */
680     if (request->flags & ADR_F_ERROR) {
681         request->bp->b_error = EIO;
682         request->bp->b_flags |= B_ERROR;
683     } 
684     else {
685         request->bytecount -= request->currentsize;
686         request->donecount += request->currentsize;
687         if (request->bytecount > 0) {
688             ad_transfer(request);
689             return ATA_OP_CONTINUES;
690         }
691     }
692
693     /* disarm timeout for this transfer */
694     untimeout((timeout_t *)ad_timeout, request, request->timeout_handle);
695
696     request->bp->b_resid = request->bytecount;
697
698     devstat_end_transaction_buf(&adp->stats, request->bp);
699     biodone(request->bp);
700     ad_free(request);
701     adp->outstanding--;
702
703     /* check for SERVICE (tagged operations only) */
704     return ad_service(adp, 1);
705 }
706
707 int
708 ad_service(struct ad_softc *adp, int change)
709 {
710     /* do we have to check the other device on this channel ? */
711     if (adp->device->channel->flags & ATA_QUEUED && change) {
712         int device = adp->device->unit;
713
714         if (adp->device->unit == ATA_MASTER) {
715             if ((adp->device->channel->devices & ATA_ATA_SLAVE) &&
716                 (adp->device->channel->device[SLAVE].driver) &&
717                 ((struct ad_softc *) (adp->device->channel->
718                  device[SLAVE].driver))->flags & AD_F_TAG_ENABLED)
719                 device = ATA_SLAVE;
720         }
721         else {
722             if ((adp->device->channel->devices & ATA_ATA_MASTER) &&
723                 (adp->device->channel->device[MASTER].driver) &&
724                 ((struct ad_softc *) (adp->device->channel->
725                  device[MASTER].driver))->flags & AD_F_TAG_ENABLED)
726                 device = ATA_MASTER;
727         }
728         if (device != adp->device->unit &&
729             ((struct ad_softc *)
730              (adp->device->channel->
731               device[ATA_DEV(device)].driver))->outstanding > 0) {
732             ATA_OUTB(adp->device->channel->r_io, ATA_DRIVE, ATA_D_IBM | device);
733             adp = adp->device->channel->device[ATA_DEV(device)].driver;
734             DELAY(1);
735         }
736     }
737     adp->device->channel->status =
738         ATA_INB(adp->device->channel->r_altio, ATA_ALTSTAT);
739  
740     /* do we have a SERVICE request from the drive ? */
741     if (adp->flags & AD_F_TAG_ENABLED &&
742         adp->outstanding > 0 &&
743         adp->device->channel->status & ATA_S_SERVICE) {
744         struct ad_request *request;
745         int tag;
746
747         /* check for error */
748         if (adp->device->channel->status & ATA_S_ERROR) {
749             ata_prtdev(adp->device, "Oops! controller says s=0x%02x e=0x%02x\n",
750                        adp->device->channel->status,
751                        adp->device->channel->error);
752             ad_invalidatequeue(adp, NULL);
753             return ATA_OP_FINISHED;
754         }
755
756         /* issue SERVICE cmd */
757         if (ata_command(adp->device, ATA_C_SERVICE, 0, 0, 0, ATA_IMMEDIATE)) {
758             ata_prtdev(adp->device, "problem executing SERVICE cmd\n");
759             ad_invalidatequeue(adp, NULL);
760             return ATA_OP_FINISHED;
761         }
762
763         /* setup the transfer environment when ready */
764         if (ata_wait(adp->device, ATA_S_READY)) {
765             ata_prtdev(adp->device, "SERVICE timeout tag=%d s=%02x e=%02x\n",
766                        ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3,
767                        adp->device->channel->status,
768                        adp->device->channel->error);
769             ad_invalidatequeue(adp, NULL);
770             return ATA_OP_FINISHED;
771         }
772         tag = ATA_INB(adp->device->channel->r_io, ATA_COUNT) >> 3;
773         if (!(request = adp->tags[tag])) {
774             ata_prtdev(adp->device, "no request for tag=%d\n", tag);    
775             ad_invalidatequeue(adp, NULL);
776             return ATA_OP_FINISHED;
777         }
778         ATA_FORCELOCK_CH(adp->device->channel, ATA_ACTIVE_ATA);
779         adp->device->channel->running = request;
780         request->serv++;
781
782         /* start DMA transfer when ready */
783         if (ata_wait(adp->device, ATA_S_READY | ATA_S_DRQ)) {
784             ata_prtdev(adp->device, "timeout starting DMA s=%02x e=%02x\n",
785                        adp->device->channel->status,
786                        adp->device->channel->error);
787             ad_invalidatequeue(adp, NULL);
788             return ATA_OP_FINISHED;
789         }
790         ata_dmastart(adp->device->channel, adp->device->unit,
791                      request->dmatab, request->flags & ADR_F_READ);
792         return ATA_OP_CONTINUES;
793     }
794     return ATA_OP_FINISHED;
795 }
796
797 static void
798 ad_free(struct ad_request *request)
799 {
800     int s = splbio();
801
802     if (request->dmatab)
803         free(request->dmatab, M_DEVBUF);
804     request->softc->tags[request->tag] = NULL;
805     free(request, M_AD);
806     splx(s);
807 }
808
809 static void
810 ad_invalidatequeue(struct ad_softc *adp, struct ad_request *request)
811 {
812     /* if tags used invalidate all other tagged transfers */
813     if (adp->flags & AD_F_TAG_ENABLED) {
814         struct ad_request *tmpreq;
815         int tag;
816
817         ata_prtdev(adp->device, "invalidating queued requests\n");
818         for (tag = 0; tag <= adp->num_tags; tag++) {
819             tmpreq = adp->tags[tag];
820             adp->tags[tag] = NULL;
821             if (tmpreq == request || tmpreq == NULL)
822                 continue;
823             untimeout((timeout_t *)ad_timeout, tmpreq, tmpreq->timeout_handle);
824             TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, tmpreq, chain);
825         }
826         if (ata_command(adp->device, ATA_C_NOP,
827                         0, 0, ATA_C_F_FLUSHQUEUE, ATA_WAIT_READY))
828             ata_prtdev(adp->device, "flush queue failed\n");
829         adp->outstanding = 0;
830     }
831 }
832
833 static int
834 ad_tagsupported(struct ad_softc *adp)
835 {
836     const char *good[] = {"IBM-DPTA", "IBM-DTLA", NULL};
837     int i = 0;
838
839     switch (adp->device->channel->chiptype) {
840     case 0x4d33105a: /* Promises before TX2 doesn't work with tagged queuing */
841     case 0x4d38105a:
842     case 0x0d30105a:
843     case 0x4d30105a:  
844         return 0;
845     }
846
847     /* check that drive does DMA, has tags enabled, and is one we know works */
848     if (adp->device->mode >= ATA_DMA && adp->device->param->support.queued && 
849         adp->device->param->enabled.queued) {
850         while (good[i] != NULL) {
851             if (!strncmp(adp->device->param->model, good[i], strlen(good[i])))
852                 return 1;
853             i++;
854         }
855         /* 
856          * check IBM's new obscure way of naming drives 
857          * we want "IC" (IBM CORP) and "AT" or "AV" (ATA interface)
858          * but doesn't care about the other info (size, capacity etc)
859          */
860         if (!strncmp(adp->device->param->model, "IC", 2) &&
861             (!strncmp(adp->device->param->model + 8, "AT", 2) ||
862              !strncmp(adp->device->param->model + 8, "AV", 2)))
863                 return 1;
864     }
865     return 0;
866 }
867
868 static void
869 ad_timeout(struct ad_request *request)
870 {
871     struct ad_softc *adp = request->softc;
872
873     adp->device->channel->running = NULL;
874     ata_prtdev(adp->device, "%s command timeout tag=%d serv=%d - resetting\n",
875                (request->flags & ADR_F_READ) ? "READ" : "WRITE",
876                request->tag, request->serv);
877
878     if (request->flags & ADR_F_DMA_USED) {
879         ata_dmadone(adp->device->channel);
880         ad_invalidatequeue(adp, request);
881         if (request->retries == AD_MAX_RETRIES) {
882             ata_dmainit(adp->device->channel, adp->device->unit,
883                         ata_pmode(adp->device->param), -1, -1);
884             ata_prtdev(adp->device, "trying fallback to PIO mode\n");
885             request->retries = 0;
886         }
887     }
888
889     /* if retries still permit, reinject this request */
890     if (request->retries++ < AD_MAX_RETRIES) {
891         TAILQ_INSERT_HEAD(&adp->device->channel->ata_queue, request, chain);
892     }
893     else {
894         /* retries all used up, return error */
895         request->bp->b_error = EIO;
896         request->bp->b_flags |= B_ERROR;
897         devstat_end_transaction_buf(&adp->stats, request->bp);
898         biodone(request->bp);
899         ad_free(request);
900     }
901     ata_reinit(adp->device->channel);
902 }
903
904 void
905 ad_reinit(struct ata_device *atadev)
906 {
907     struct ad_softc *adp = atadev->driver;
908
909     /* reinit disk parameters */
910     ad_invalidatequeue(atadev->driver, NULL);
911     ata_command(atadev, ATA_C_SET_MULTI, 0,
912                 adp->transfersize / DEV_BSIZE, 0, ATA_WAIT_READY);
913     if (adp->device->mode >= ATA_DMA)
914         ata_dmainit(atadev->channel, atadev->unit,
915                     ata_pmode(adp->device->param),
916                     ata_wmode(adp->device->param),
917                     ata_umode(adp->device->param));
918     else
919         ata_dmainit(atadev->channel, atadev->unit,
920                     ata_pmode(adp->device->param), -1, -1);
921 }
922
923 void
924 ad_print(struct ad_softc *adp) 
925 {
926     if (bootverbose) {
927         ata_prtdev(adp->device, "<%.40s/%.8s> ATA-%d disk at ata%d-%s\n", 
928                    adp->device->param->model, adp->device->param->revision,
929                    ad_version(adp->device->param->version_major), 
930                    device_get_unit(adp->device->channel->dev),
931                    (adp->device->unit == ATA_MASTER) ? "master" : "slave");
932
933         ata_prtdev(adp->device,
934                    "%lluMB (%llu sectors), %llu C, %u H, %u S, %u B\n",
935                    (unsigned long long)(adp->total_secs /
936                    ((1024L*1024L)/DEV_BSIZE)),
937                    (unsigned long long) adp->total_secs,
938                    (unsigned long long) (adp->total_secs /
939                     (adp->heads * adp->sectors)),
940                    adp->heads, adp->sectors, DEV_BSIZE);
941
942         ata_prtdev(adp->device, "%d secs/int, %d depth queue, %s%s\n", 
943                    adp->transfersize / DEV_BSIZE, adp->num_tags + 1,
944                    (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
945                    ata_mode2str(adp->device->mode));
946
947         ata_prtdev(adp->device, "piomode=%d dmamode=%d udmamode=%d cblid=%d\n",
948                    ata_pmode(adp->device->param), ata_wmode(adp->device->param),
949                    ata_umode(adp->device->param), 
950                    adp->device->param->hwres_cblid);
951
952     }
953     else
954         ata_prtdev(adp->device,"%lluMB <%.40s> [%lld/%d/%d] at ata%d-%s %s%s\n",
955                    (unsigned long long)(adp->total_secs /
956                    ((1024L * 1024L) / DEV_BSIZE)),
957                    adp->device->param->model,
958                    (unsigned long long)(adp->total_secs /
959                     (adp->heads*adp->sectors)),
960                    adp->heads, adp->sectors,
961                    device_get_unit(adp->device->channel->dev),
962                    (adp->device->unit == ATA_MASTER) ? "master" : "slave",
963                    (adp->flags & AD_F_TAG_ENABLED) ? "tagged " : "",
964                    ata_mode2str(adp->device->mode));
965 }
966
967 static int
968 ad_version(u_int16_t version)
969 {
970     int bit;
971
972     if (version == 0xffff)
973         return 0;
974     for (bit = 15; bit >= 0; bit--)
975         if (version & (1<<bit))
976             return bit;
977     return 0;
978 }