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