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