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