]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/scsi/libata-core.c
f1942545a20d6cb3134e243c9a4df5856f44e0c3
[linux-2.6.git] / drivers / scsi / libata-core.c
1 /*
2  *  libata-core.c - helper library for ATA
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Please ALWAYS copy linux-ide@vger.kernel.org
6  *                  on emails.
7  *
8  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9  *  Copyright 2003-2004 Jeff Garzik
10  *
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; see the file COPYING.  If not, write to
24  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  *
27  *  libata documentation is available via 'make {ps|pdf}docs',
28  *  as Documentation/DocBook/libata.*
29  *
30  *  Hardware documentation available from http://www.t13.org/ and
31  *  http://www.sata-io.org/
32  *
33  */
34
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/mm.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
58 #include <asm/io.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
61
62 #include "libata.h"
63
64 static unsigned int ata_busy_sleep (struct ata_port *ap,
65                                     unsigned long tmout_pat,
66                                     unsigned long tmout);
67 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69 static void ata_set_mode(struct ata_port *ap);
70 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
72 static int fgb(u32 bitmap);
73 static int ata_choose_xfer_mode(const struct ata_port *ap,
74                                 u8 *xfer_mode_out,
75                                 unsigned int *xfer_shift_out);
76 static void __ata_qc_complete(struct ata_queued_cmd *qc);
77
78 static unsigned int ata_unique_id = 1;
79 static struct workqueue_struct *ata_wq;
80
81 int atapi_enabled = 0;
82 module_param(atapi_enabled, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
89
90 /**
91  *      ata_tf_load_pio - send taskfile registers to host controller
92  *      @ap: Port to which output is sent
93  *      @tf: ATA taskfile register set
94  *
95  *      Outputs ATA taskfile to standard ATA host controller.
96  *
97  *      LOCKING:
98  *      Inherited from caller.
99  */
100
101 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
102 {
103         struct ata_ioports *ioaddr = &ap->ioaddr;
104         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105
106         if (tf->ctl != ap->last_ctl) {
107                 outb(tf->ctl, ioaddr->ctl_addr);
108                 ap->last_ctl = tf->ctl;
109                 ata_wait_idle(ap);
110         }
111
112         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113                 outb(tf->hob_feature, ioaddr->feature_addr);
114                 outb(tf->hob_nsect, ioaddr->nsect_addr);
115                 outb(tf->hob_lbal, ioaddr->lbal_addr);
116                 outb(tf->hob_lbam, ioaddr->lbam_addr);
117                 outb(tf->hob_lbah, ioaddr->lbah_addr);
118                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
119                         tf->hob_feature,
120                         tf->hob_nsect,
121                         tf->hob_lbal,
122                         tf->hob_lbam,
123                         tf->hob_lbah);
124         }
125
126         if (is_addr) {
127                 outb(tf->feature, ioaddr->feature_addr);
128                 outb(tf->nsect, ioaddr->nsect_addr);
129                 outb(tf->lbal, ioaddr->lbal_addr);
130                 outb(tf->lbam, ioaddr->lbam_addr);
131                 outb(tf->lbah, ioaddr->lbah_addr);
132                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133                         tf->feature,
134                         tf->nsect,
135                         tf->lbal,
136                         tf->lbam,
137                         tf->lbah);
138         }
139
140         if (tf->flags & ATA_TFLAG_DEVICE) {
141                 outb(tf->device, ioaddr->device_addr);
142                 VPRINTK("device 0x%X\n", tf->device);
143         }
144
145         ata_wait_idle(ap);
146 }
147
148 /**
149  *      ata_tf_load_mmio - send taskfile registers to host controller
150  *      @ap: Port to which output is sent
151  *      @tf: ATA taskfile register set
152  *
153  *      Outputs ATA taskfile to standard ATA host controller using MMIO.
154  *
155  *      LOCKING:
156  *      Inherited from caller.
157  */
158
159 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
160 {
161         struct ata_ioports *ioaddr = &ap->ioaddr;
162         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163
164         if (tf->ctl != ap->last_ctl) {
165                 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166                 ap->last_ctl = tf->ctl;
167                 ata_wait_idle(ap);
168         }
169
170         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171                 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172                 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173                 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174                 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175                 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
177                         tf->hob_feature,
178                         tf->hob_nsect,
179                         tf->hob_lbal,
180                         tf->hob_lbam,
181                         tf->hob_lbah);
182         }
183
184         if (is_addr) {
185                 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186                 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187                 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188                 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189                 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191                         tf->feature,
192                         tf->nsect,
193                         tf->lbal,
194                         tf->lbam,
195                         tf->lbah);
196         }
197
198         if (tf->flags & ATA_TFLAG_DEVICE) {
199                 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200                 VPRINTK("device 0x%X\n", tf->device);
201         }
202
203         ata_wait_idle(ap);
204 }
205
206
207 /**
208  *      ata_tf_load - send taskfile registers to host controller
209  *      @ap: Port to which output is sent
210  *      @tf: ATA taskfile register set
211  *
212  *      Outputs ATA taskfile to standard ATA host controller using MMIO
213  *      or PIO as indicated by the ATA_FLAG_MMIO flag.
214  *      Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215  *      Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216  *      hob_lbal, hob_lbam, and hob_lbah.
217  *
218  *      This function waits for idle (!BUSY and !DRQ) after writing
219  *      registers.  If the control register has a new value, this
220  *      function also waits for idle after writing control and before
221  *      writing the remaining registers.
222  *
223  *      May be used as the tf_load() entry in ata_port_operations.
224  *
225  *      LOCKING:
226  *      Inherited from caller.
227  */
228 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
229 {
230         if (ap->flags & ATA_FLAG_MMIO)
231                 ata_tf_load_mmio(ap, tf);
232         else
233                 ata_tf_load_pio(ap, tf);
234 }
235
236 /**
237  *      ata_exec_command_pio - issue ATA command to host controller
238  *      @ap: port to which command is being issued
239  *      @tf: ATA taskfile register set
240  *
241  *      Issues PIO write to ATA command register, with proper
242  *      synchronization with interrupt handler / other threads.
243  *
244  *      LOCKING:
245  *      spin_lock_irqsave(host_set lock)
246  */
247
248 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
249 {
250         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251
252         outb(tf->command, ap->ioaddr.command_addr);
253         ata_pause(ap);
254 }
255
256
257 /**
258  *      ata_exec_command_mmio - issue ATA command to host controller
259  *      @ap: port to which command is being issued
260  *      @tf: ATA taskfile register set
261  *
262  *      Issues MMIO write to ATA command register, with proper
263  *      synchronization with interrupt handler / other threads.
264  *
265  *      LOCKING:
266  *      spin_lock_irqsave(host_set lock)
267  */
268
269 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
270 {
271         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272
273         writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
274         ata_pause(ap);
275 }
276
277
278 /**
279  *      ata_exec_command - issue ATA command to host controller
280  *      @ap: port to which command is being issued
281  *      @tf: ATA taskfile register set
282  *
283  *      Issues PIO/MMIO write to ATA command register, with proper
284  *      synchronization with interrupt handler / other threads.
285  *
286  *      LOCKING:
287  *      spin_lock_irqsave(host_set lock)
288  */
289 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
290 {
291         if (ap->flags & ATA_FLAG_MMIO)
292                 ata_exec_command_mmio(ap, tf);
293         else
294                 ata_exec_command_pio(ap, tf);
295 }
296
297 /**
298  *      ata_tf_to_host - issue ATA taskfile to host controller
299  *      @ap: port to which command is being issued
300  *      @tf: ATA taskfile register set
301  *
302  *      Issues ATA taskfile register set to ATA host controller,
303  *      with proper synchronization with interrupt handler and
304  *      other threads.
305  *
306  *      LOCKING:
307  *      spin_lock_irqsave(host_set lock)
308  */
309
310 static inline void ata_tf_to_host(struct ata_port *ap,
311                                   const struct ata_taskfile *tf)
312 {
313         ap->ops->tf_load(ap, tf);
314         ap->ops->exec_command(ap, tf);
315 }
316
317 /**
318  *      ata_tf_read_pio - input device's ATA taskfile shadow registers
319  *      @ap: Port from which input is read
320  *      @tf: ATA taskfile register set for storing input
321  *
322  *      Reads ATA taskfile registers for currently-selected device
323  *      into @tf.
324  *
325  *      LOCKING:
326  *      Inherited from caller.
327  */
328
329 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
330 {
331         struct ata_ioports *ioaddr = &ap->ioaddr;
332
333         tf->command = ata_check_status(ap);
334         tf->feature = inb(ioaddr->error_addr);
335         tf->nsect = inb(ioaddr->nsect_addr);
336         tf->lbal = inb(ioaddr->lbal_addr);
337         tf->lbam = inb(ioaddr->lbam_addr);
338         tf->lbah = inb(ioaddr->lbah_addr);
339         tf->device = inb(ioaddr->device_addr);
340
341         if (tf->flags & ATA_TFLAG_LBA48) {
342                 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343                 tf->hob_feature = inb(ioaddr->error_addr);
344                 tf->hob_nsect = inb(ioaddr->nsect_addr);
345                 tf->hob_lbal = inb(ioaddr->lbal_addr);
346                 tf->hob_lbam = inb(ioaddr->lbam_addr);
347                 tf->hob_lbah = inb(ioaddr->lbah_addr);
348         }
349 }
350
351 /**
352  *      ata_tf_read_mmio - input device's ATA taskfile shadow registers
353  *      @ap: Port from which input is read
354  *      @tf: ATA taskfile register set for storing input
355  *
356  *      Reads ATA taskfile registers for currently-selected device
357  *      into @tf via MMIO.
358  *
359  *      LOCKING:
360  *      Inherited from caller.
361  */
362
363 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
364 {
365         struct ata_ioports *ioaddr = &ap->ioaddr;
366
367         tf->command = ata_check_status(ap);
368         tf->feature = readb((void __iomem *)ioaddr->error_addr);
369         tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370         tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371         tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372         tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373         tf->device = readb((void __iomem *)ioaddr->device_addr);
374
375         if (tf->flags & ATA_TFLAG_LBA48) {
376                 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377                 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378                 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379                 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380                 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381                 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
382         }
383 }
384
385
386 /**
387  *      ata_tf_read - input device's ATA taskfile shadow registers
388  *      @ap: Port from which input is read
389  *      @tf: ATA taskfile register set for storing input
390  *
391  *      Reads ATA taskfile registers for currently-selected device
392  *      into @tf.
393  *
394  *      Reads nsect, lbal, lbam, lbah, and device.  If ATA_TFLAG_LBA48
395  *      is set, also reads the hob registers.
396  *
397  *      May be used as the tf_read() entry in ata_port_operations.
398  *
399  *      LOCKING:
400  *      Inherited from caller.
401  */
402 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
403 {
404         if (ap->flags & ATA_FLAG_MMIO)
405                 ata_tf_read_mmio(ap, tf);
406         else
407                 ata_tf_read_pio(ap, tf);
408 }
409
410 /**
411  *      ata_check_status_pio - Read device status reg & clear interrupt
412  *      @ap: port where the device is
413  *
414  *      Reads ATA taskfile status register for currently-selected device
415  *      and return its value. This also clears pending interrupts
416  *      from this device
417  *
418  *      LOCKING:
419  *      Inherited from caller.
420  */
421 static u8 ata_check_status_pio(struct ata_port *ap)
422 {
423         return inb(ap->ioaddr.status_addr);
424 }
425
426 /**
427  *      ata_check_status_mmio - Read device status reg & clear interrupt
428  *      @ap: port where the device is
429  *
430  *      Reads ATA taskfile status register for currently-selected device
431  *      via MMIO and return its value. This also clears pending interrupts
432  *      from this device
433  *
434  *      LOCKING:
435  *      Inherited from caller.
436  */
437 static u8 ata_check_status_mmio(struct ata_port *ap)
438 {
439         return readb((void __iomem *) ap->ioaddr.status_addr);
440 }
441
442
443 /**
444  *      ata_check_status - Read device status reg & clear interrupt
445  *      @ap: port where the device is
446  *
447  *      Reads ATA taskfile status register for currently-selected device
448  *      and return its value. This also clears pending interrupts
449  *      from this device
450  *
451  *      May be used as the check_status() entry in ata_port_operations.
452  *
453  *      LOCKING:
454  *      Inherited from caller.
455  */
456 u8 ata_check_status(struct ata_port *ap)
457 {
458         if (ap->flags & ATA_FLAG_MMIO)
459                 return ata_check_status_mmio(ap);
460         return ata_check_status_pio(ap);
461 }
462
463
464 /**
465  *      ata_altstatus - Read device alternate status reg
466  *      @ap: port where the device is
467  *
468  *      Reads ATA taskfile alternate status register for
469  *      currently-selected device and return its value.
470  *
471  *      Note: may NOT be used as the check_altstatus() entry in
472  *      ata_port_operations.
473  *
474  *      LOCKING:
475  *      Inherited from caller.
476  */
477 u8 ata_altstatus(struct ata_port *ap)
478 {
479         if (ap->ops->check_altstatus)
480                 return ap->ops->check_altstatus(ap);
481
482         if (ap->flags & ATA_FLAG_MMIO)
483                 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484         return inb(ap->ioaddr.altstatus_addr);
485 }
486
487
488 /**
489  *      ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490  *      @tf: Taskfile to convert
491  *      @fis: Buffer into which data will output
492  *      @pmp: Port multiplier port
493  *
494  *      Converts a standard ATA taskfile to a Serial ATA
495  *      FIS structure (Register - Host to Device).
496  *
497  *      LOCKING:
498  *      Inherited from caller.
499  */
500
501 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
502 {
503         fis[0] = 0x27;  /* Register - Host to Device FIS */
504         fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505                                             bit 7 indicates Command FIS */
506         fis[2] = tf->command;
507         fis[3] = tf->feature;
508
509         fis[4] = tf->lbal;
510         fis[5] = tf->lbam;
511         fis[6] = tf->lbah;
512         fis[7] = tf->device;
513
514         fis[8] = tf->hob_lbal;
515         fis[9] = tf->hob_lbam;
516         fis[10] = tf->hob_lbah;
517         fis[11] = tf->hob_feature;
518
519         fis[12] = tf->nsect;
520         fis[13] = tf->hob_nsect;
521         fis[14] = 0;
522         fis[15] = tf->ctl;
523
524         fis[16] = 0;
525         fis[17] = 0;
526         fis[18] = 0;
527         fis[19] = 0;
528 }
529
530 /**
531  *      ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532  *      @fis: Buffer from which data will be input
533  *      @tf: Taskfile to output
534  *
535  *      Converts a standard ATA taskfile to a Serial ATA
536  *      FIS structure (Register - Host to Device).
537  *
538  *      LOCKING:
539  *      Inherited from caller.
540  */
541
542 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
543 {
544         tf->command     = fis[2];       /* status */
545         tf->feature     = fis[3];       /* error */
546
547         tf->lbal        = fis[4];
548         tf->lbam        = fis[5];
549         tf->lbah        = fis[6];
550         tf->device      = fis[7];
551
552         tf->hob_lbal    = fis[8];
553         tf->hob_lbam    = fis[9];
554         tf->hob_lbah    = fis[10];
555
556         tf->nsect       = fis[12];
557         tf->hob_nsect   = fis[13];
558 }
559
560 static const u8 ata_rw_cmds[] = {
561         /* pio multi */
562         ATA_CMD_READ_MULTI,
563         ATA_CMD_WRITE_MULTI,
564         ATA_CMD_READ_MULTI_EXT,
565         ATA_CMD_WRITE_MULTI_EXT,
566         /* pio */
567         ATA_CMD_PIO_READ,
568         ATA_CMD_PIO_WRITE,
569         ATA_CMD_PIO_READ_EXT,
570         ATA_CMD_PIO_WRITE_EXT,
571         /* dma */
572         ATA_CMD_READ,
573         ATA_CMD_WRITE,
574         ATA_CMD_READ_EXT,
575         ATA_CMD_WRITE_EXT
576 };
577
578 /**
579  *      ata_rwcmd_protocol - set taskfile r/w commands and protocol
580  *      @qc: command to examine and configure
581  *
582  *      Examine the device configuration and tf->flags to calculate 
583  *      the proper read/write commands and protocol to use.
584  *
585  *      LOCKING:
586  *      caller.
587  */
588 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
589 {
590         struct ata_taskfile *tf = &qc->tf;
591         struct ata_device *dev = qc->dev;
592
593         int index, lba48, write;
594  
595         lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596         write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
597
598         if (dev->flags & ATA_DFLAG_PIO) {
599                 tf->protocol = ATA_PROT_PIO;
600                 index = dev->multi_count ? 0 : 4;
601         } else {
602                 tf->protocol = ATA_PROT_DMA;
603                 index = 8;
604         }
605
606         tf->command = ata_rw_cmds[index + lba48 + write];
607 }
608
609 static const char * xfer_mode_str[] = {
610         "UDMA/16",
611         "UDMA/25",
612         "UDMA/33",
613         "UDMA/44",
614         "UDMA/66",
615         "UDMA/100",
616         "UDMA/133",
617         "UDMA7",
618         "MWDMA0",
619         "MWDMA1",
620         "MWDMA2",
621         "PIO0",
622         "PIO1",
623         "PIO2",
624         "PIO3",
625         "PIO4",
626 };
627
628 /**
629  *      ata_udma_string - convert UDMA bit offset to string
630  *      @mask: mask of bits supported; only highest bit counts.
631  *
632  *      Determine string which represents the highest speed
633  *      (highest bit in @udma_mask).
634  *
635  *      LOCKING:
636  *      None.
637  *
638  *      RETURNS:
639  *      Constant C string representing highest speed listed in
640  *      @udma_mask, or the constant C string "<n/a>".
641  */
642
643 static const char *ata_mode_string(unsigned int mask)
644 {
645         int i;
646
647         for (i = 7; i >= 0; i--)
648                 if (mask & (1 << i))
649                         goto out;
650         for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
651                 if (mask & (1 << i))
652                         goto out;
653         for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
654                 if (mask & (1 << i))
655                         goto out;
656
657         return "<n/a>";
658
659 out:
660         return xfer_mode_str[i];
661 }
662
663 /**
664  *      ata_pio_devchk - PATA device presence detection
665  *      @ap: ATA channel to examine
666  *      @device: Device to examine (starting at zero)
667  *
668  *      This technique was originally described in
669  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
670  *      later found its way into the ATA/ATAPI spec.
671  *
672  *      Write a pattern to the ATA shadow registers,
673  *      and if a device is present, it will respond by
674  *      correctly storing and echoing back the
675  *      ATA shadow register contents.
676  *
677  *      LOCKING:
678  *      caller.
679  */
680
681 static unsigned int ata_pio_devchk(struct ata_port *ap,
682                                    unsigned int device)
683 {
684         struct ata_ioports *ioaddr = &ap->ioaddr;
685         u8 nsect, lbal;
686
687         ap->ops->dev_select(ap, device);
688
689         outb(0x55, ioaddr->nsect_addr);
690         outb(0xaa, ioaddr->lbal_addr);
691
692         outb(0xaa, ioaddr->nsect_addr);
693         outb(0x55, ioaddr->lbal_addr);
694
695         outb(0x55, ioaddr->nsect_addr);
696         outb(0xaa, ioaddr->lbal_addr);
697
698         nsect = inb(ioaddr->nsect_addr);
699         lbal = inb(ioaddr->lbal_addr);
700
701         if ((nsect == 0x55) && (lbal == 0xaa))
702                 return 1;       /* we found a device */
703
704         return 0;               /* nothing found */
705 }
706
707 /**
708  *      ata_mmio_devchk - PATA device presence detection
709  *      @ap: ATA channel to examine
710  *      @device: Device to examine (starting at zero)
711  *
712  *      This technique was originally described in
713  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
714  *      later found its way into the ATA/ATAPI spec.
715  *
716  *      Write a pattern to the ATA shadow registers,
717  *      and if a device is present, it will respond by
718  *      correctly storing and echoing back the
719  *      ATA shadow register contents.
720  *
721  *      LOCKING:
722  *      caller.
723  */
724
725 static unsigned int ata_mmio_devchk(struct ata_port *ap,
726                                     unsigned int device)
727 {
728         struct ata_ioports *ioaddr = &ap->ioaddr;
729         u8 nsect, lbal;
730
731         ap->ops->dev_select(ap, device);
732
733         writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
734         writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
735
736         writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
737         writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
738
739         writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
740         writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
741
742         nsect = readb((void __iomem *) ioaddr->nsect_addr);
743         lbal = readb((void __iomem *) ioaddr->lbal_addr);
744
745         if ((nsect == 0x55) && (lbal == 0xaa))
746                 return 1;       /* we found a device */
747
748         return 0;               /* nothing found */
749 }
750
751 /**
752  *      ata_devchk - PATA device presence detection
753  *      @ap: ATA channel to examine
754  *      @device: Device to examine (starting at zero)
755  *
756  *      Dispatch ATA device presence detection, depending
757  *      on whether we are using PIO or MMIO to talk to the
758  *      ATA shadow registers.
759  *
760  *      LOCKING:
761  *      caller.
762  */
763
764 static unsigned int ata_devchk(struct ata_port *ap,
765                                     unsigned int device)
766 {
767         if (ap->flags & ATA_FLAG_MMIO)
768                 return ata_mmio_devchk(ap, device);
769         return ata_pio_devchk(ap, device);
770 }
771
772 /**
773  *      ata_dev_classify - determine device type based on ATA-spec signature
774  *      @tf: ATA taskfile register set for device to be identified
775  *
776  *      Determine from taskfile register contents whether a device is
777  *      ATA or ATAPI, as per "Signature and persistence" section
778  *      of ATA/PI spec (volume 1, sect 5.14).
779  *
780  *      LOCKING:
781  *      None.
782  *
783  *      RETURNS:
784  *      Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
785  *      the event of failure.
786  */
787
788 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
789 {
790         /* Apple's open source Darwin code hints that some devices only
791          * put a proper signature into the LBA mid/high registers,
792          * So, we only check those.  It's sufficient for uniqueness.
793          */
794
795         if (((tf->lbam == 0) && (tf->lbah == 0)) ||
796             ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
797                 DPRINTK("found ATA device by sig\n");
798                 return ATA_DEV_ATA;
799         }
800
801         if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
802             ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
803                 DPRINTK("found ATAPI device by sig\n");
804                 return ATA_DEV_ATAPI;
805         }
806
807         DPRINTK("unknown device\n");
808         return ATA_DEV_UNKNOWN;
809 }
810
811 /**
812  *      ata_dev_try_classify - Parse returned ATA device signature
813  *      @ap: ATA channel to examine
814  *      @device: Device to examine (starting at zero)
815  *
816  *      After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
817  *      an ATA/ATAPI-defined set of values is placed in the ATA
818  *      shadow registers, indicating the results of device detection
819  *      and diagnostics.
820  *
821  *      Select the ATA device, and read the values from the ATA shadow
822  *      registers.  Then parse according to the Error register value,
823  *      and the spec-defined values examined by ata_dev_classify().
824  *
825  *      LOCKING:
826  *      caller.
827  */
828
829 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
830 {
831         struct ata_device *dev = &ap->device[device];
832         struct ata_taskfile tf;
833         unsigned int class;
834         u8 err;
835
836         ap->ops->dev_select(ap, device);
837
838         memset(&tf, 0, sizeof(tf));
839
840         ap->ops->tf_read(ap, &tf);
841         err = tf.feature;
842
843         dev->class = ATA_DEV_NONE;
844
845         /* see if device passed diags */
846         if (err == 1)
847                 /* do nothing */ ;
848         else if ((device == 0) && (err == 0x81))
849                 /* do nothing */ ;
850         else
851                 return err;
852
853         /* determine if device if ATA or ATAPI */
854         class = ata_dev_classify(&tf);
855         if (class == ATA_DEV_UNKNOWN)
856                 return err;
857         if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
858                 return err;
859
860         dev->class = class;
861
862         return err;
863 }
864
865 /**
866  *      ata_dev_id_string - Convert IDENTIFY DEVICE page into string
867  *      @id: IDENTIFY DEVICE results we will examine
868  *      @s: string into which data is output
869  *      @ofs: offset into identify device page
870  *      @len: length of string to return. must be an even number.
871  *
872  *      The strings in the IDENTIFY DEVICE page are broken up into
873  *      16-bit chunks.  Run through the string, and output each
874  *      8-bit chunk linearly, regardless of platform.
875  *
876  *      LOCKING:
877  *      caller.
878  */
879
880 void ata_dev_id_string(const u16 *id, unsigned char *s,
881                        unsigned int ofs, unsigned int len)
882 {
883         unsigned int c;
884
885         while (len > 0) {
886                 c = id[ofs] >> 8;
887                 *s = c;
888                 s++;
889
890                 c = id[ofs] & 0xff;
891                 *s = c;
892                 s++;
893
894                 ofs++;
895                 len -= 2;
896         }
897 }
898
899
900 /**
901  *      ata_noop_dev_select - Select device 0/1 on ATA bus
902  *      @ap: ATA channel to manipulate
903  *      @device: ATA device (numbered from zero) to select
904  *
905  *      This function performs no actual function.
906  *
907  *      May be used as the dev_select() entry in ata_port_operations.
908  *
909  *      LOCKING:
910  *      caller.
911  */
912 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
913 {
914 }
915
916
917 /**
918  *      ata_std_dev_select - Select device 0/1 on ATA bus
919  *      @ap: ATA channel to manipulate
920  *      @device: ATA device (numbered from zero) to select
921  *
922  *      Use the method defined in the ATA specification to
923  *      make either device 0, or device 1, active on the
924  *      ATA channel.  Works with both PIO and MMIO.
925  *
926  *      May be used as the dev_select() entry in ata_port_operations.
927  *
928  *      LOCKING:
929  *      caller.
930  */
931
932 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
933 {
934         u8 tmp;
935
936         if (device == 0)
937                 tmp = ATA_DEVICE_OBS;
938         else
939                 tmp = ATA_DEVICE_OBS | ATA_DEV1;
940
941         if (ap->flags & ATA_FLAG_MMIO) {
942                 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
943         } else {
944                 outb(tmp, ap->ioaddr.device_addr);
945         }
946         ata_pause(ap);          /* needed; also flushes, for mmio */
947 }
948
949 /**
950  *      ata_dev_select - Select device 0/1 on ATA bus
951  *      @ap: ATA channel to manipulate
952  *      @device: ATA device (numbered from zero) to select
953  *      @wait: non-zero to wait for Status register BSY bit to clear
954  *      @can_sleep: non-zero if context allows sleeping
955  *
956  *      Use the method defined in the ATA specification to
957  *      make either device 0, or device 1, active on the
958  *      ATA channel.
959  *
960  *      This is a high-level version of ata_std_dev_select(),
961  *      which additionally provides the services of inserting
962  *      the proper pauses and status polling, where needed.
963  *
964  *      LOCKING:
965  *      caller.
966  */
967
968 void ata_dev_select(struct ata_port *ap, unsigned int device,
969                            unsigned int wait, unsigned int can_sleep)
970 {
971         VPRINTK("ENTER, ata%u: device %u, wait %u\n",
972                 ap->id, device, wait);
973
974         if (wait)
975                 ata_wait_idle(ap);
976
977         ap->ops->dev_select(ap, device);
978
979         if (wait) {
980                 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
981                         msleep(150);
982                 ata_wait_idle(ap);
983         }
984 }
985
986 /**
987  *      ata_dump_id - IDENTIFY DEVICE info debugging output
988  *      @dev: Device whose IDENTIFY DEVICE page we will dump
989  *
990  *      Dump selected 16-bit words from a detected device's
991  *      IDENTIFY PAGE page.
992  *
993  *      LOCKING:
994  *      caller.
995  */
996
997 static inline void ata_dump_id(const struct ata_device *dev)
998 {
999         DPRINTK("49==0x%04x  "
1000                 "53==0x%04x  "
1001                 "63==0x%04x  "
1002                 "64==0x%04x  "
1003                 "75==0x%04x  \n",
1004                 dev->id[49],
1005                 dev->id[53],
1006                 dev->id[63],
1007                 dev->id[64],
1008                 dev->id[75]);
1009         DPRINTK("80==0x%04x  "
1010                 "81==0x%04x  "
1011                 "82==0x%04x  "
1012                 "83==0x%04x  "
1013                 "84==0x%04x  \n",
1014                 dev->id[80],
1015                 dev->id[81],
1016                 dev->id[82],
1017                 dev->id[83],
1018                 dev->id[84]);
1019         DPRINTK("88==0x%04x  "
1020                 "93==0x%04x\n",
1021                 dev->id[88],
1022                 dev->id[93]);
1023 }
1024
1025 /*
1026  *      Compute the PIO modes available for this device. This is not as
1027  *      trivial as it seems if we must consider early devices correctly.
1028  *
1029  *      FIXME: pre IDE drive timing (do we care ?). 
1030  */
1031
1032 static unsigned int ata_pio_modes(const struct ata_device *adev)
1033 {
1034         u16 modes;
1035
1036         /* Usual case. Word 53 indicates word 88 is valid */
1037         if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1038                 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1039                 modes <<= 3;
1040                 modes |= 0x7;
1041                 return modes;
1042         }
1043
1044         /* If word 88 isn't valid then Word 51 holds the PIO timing number
1045            for the maximum. Turn it into a mask and return it */
1046         modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1047         return modes;
1048 }
1049
1050 /**
1051  *      ata_dev_identify - obtain IDENTIFY x DEVICE page
1052  *      @ap: port on which device we wish to probe resides
1053  *      @device: device bus address, starting at zero
1054  *
1055  *      Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1056  *      command, and read back the 512-byte device information page.
1057  *      The device information page is fed to us via the standard
1058  *      PIO-IN protocol, but we hand-code it here. (TODO: investigate
1059  *      using standard PIO-IN paths)
1060  *
1061  *      After reading the device information page, we use several
1062  *      bits of information from it to initialize data structures
1063  *      that will be used during the lifetime of the ata_device.
1064  *      Other data from the info page is used to disqualify certain
1065  *      older ATA devices we do not wish to support.
1066  *
1067  *      LOCKING:
1068  *      Inherited from caller.  Some functions called by this function
1069  *      obtain the host_set lock.
1070  */
1071
1072 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1073 {
1074         struct ata_device *dev = &ap->device[device];
1075         unsigned int major_version;
1076         u16 tmp;
1077         unsigned long xfer_modes;
1078         unsigned int using_edd;
1079         DECLARE_COMPLETION(wait);
1080         struct ata_queued_cmd *qc;
1081         unsigned long flags;
1082         int rc;
1083
1084         if (!ata_dev_present(dev)) {
1085                 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1086                         ap->id, device);
1087                 return;
1088         }
1089
1090         if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1091                 using_edd = 0;
1092         else
1093                 using_edd = 1;
1094
1095         DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1096
1097         assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1098                 dev->class == ATA_DEV_NONE);
1099
1100         ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1101
1102         qc = ata_qc_new_init(ap, dev);
1103         BUG_ON(qc == NULL);
1104
1105         ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1106         qc->dma_dir = DMA_FROM_DEVICE;
1107         qc->tf.protocol = ATA_PROT_PIO;
1108         qc->nsect = 1;
1109
1110 retry:
1111         if (dev->class == ATA_DEV_ATA) {
1112                 qc->tf.command = ATA_CMD_ID_ATA;
1113                 DPRINTK("do ATA identify\n");
1114         } else {
1115                 qc->tf.command = ATA_CMD_ID_ATAPI;
1116                 DPRINTK("do ATAPI identify\n");
1117         }
1118
1119         qc->waiting = &wait;
1120         qc->complete_fn = ata_qc_complete_noop;
1121
1122         spin_lock_irqsave(&ap->host_set->lock, flags);
1123         rc = ata_qc_issue(qc);
1124         spin_unlock_irqrestore(&ap->host_set->lock, flags);
1125
1126         if (rc)
1127                 goto err_out;
1128         else
1129                 wait_for_completion(&wait);
1130
1131         spin_lock_irqsave(&ap->host_set->lock, flags);
1132         ap->ops->tf_read(ap, &qc->tf);
1133         spin_unlock_irqrestore(&ap->host_set->lock, flags);
1134
1135         if (qc->tf.command & ATA_ERR) {
1136                 /*
1137                  * arg!  EDD works for all test cases, but seems to return
1138                  * the ATA signature for some ATAPI devices.  Until the
1139                  * reason for this is found and fixed, we fix up the mess
1140                  * here.  If IDENTIFY DEVICE returns command aborted
1141                  * (as ATAPI devices do), then we issue an
1142                  * IDENTIFY PACKET DEVICE.
1143                  *
1144                  * ATA software reset (SRST, the default) does not appear
1145                  * to have this problem.
1146                  */
1147                 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1148                         u8 err = qc->tf.feature;
1149                         if (err & ATA_ABORTED) {
1150                                 dev->class = ATA_DEV_ATAPI;
1151                                 qc->cursg = 0;
1152                                 qc->cursg_ofs = 0;
1153                                 qc->cursect = 0;
1154                                 qc->nsect = 1;
1155                                 goto retry;
1156                         }
1157                 }
1158                 goto err_out;
1159         }
1160
1161         swap_buf_le16(dev->id, ATA_ID_WORDS);
1162
1163         /* print device capabilities */
1164         printk(KERN_DEBUG "ata%u: dev %u cfg "
1165                "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1166                ap->id, device, dev->id[49],
1167                dev->id[82], dev->id[83], dev->id[84],
1168                dev->id[85], dev->id[86], dev->id[87],
1169                dev->id[88]);
1170
1171         /*
1172          * common ATA, ATAPI feature tests
1173          */
1174
1175         /* we require DMA support (bits 8 of word 49) */
1176         if (!ata_id_has_dma(dev->id)) {
1177                 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1178                 goto err_out_nosup;
1179         }
1180
1181         /* quick-n-dirty find max transfer mode; for printk only */
1182         xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1183         if (!xfer_modes)
1184                 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1185         if (!xfer_modes)
1186                 xfer_modes = ata_pio_modes(dev);
1187
1188         ata_dump_id(dev);
1189
1190         /* ATA-specific feature tests */
1191         if (dev->class == ATA_DEV_ATA) {
1192                 if (!ata_id_is_ata(dev->id))    /* sanity check */
1193                         goto err_out_nosup;
1194
1195                 /* get major version */
1196                 tmp = dev->id[ATA_ID_MAJOR_VER];
1197                 for (major_version = 14; major_version >= 1; major_version--)
1198                         if (tmp & (1 << major_version))
1199                                 break;
1200
1201                 /*
1202                  * The exact sequence expected by certain pre-ATA4 drives is:
1203                  * SRST RESET
1204                  * IDENTIFY
1205                  * INITIALIZE DEVICE PARAMETERS
1206                  * anything else..
1207                  * Some drives were very specific about that exact sequence.
1208                  */
1209                 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1210                         ata_dev_init_params(ap, dev);
1211
1212                         /* current CHS translation info (id[53-58]) might be
1213                          * changed. reread the identify device info.
1214                          */
1215                         ata_dev_reread_id(ap, dev);
1216                 }
1217
1218                 if (ata_id_has_lba(dev->id)) {
1219                         dev->flags |= ATA_DFLAG_LBA;
1220
1221                         if (ata_id_has_lba48(dev->id)) {
1222                                 dev->flags |= ATA_DFLAG_LBA48;
1223                                 dev->n_sectors = ata_id_u64(dev->id, 100);
1224                         } else {
1225                                 dev->n_sectors = ata_id_u32(dev->id, 60);
1226                         }
1227
1228                         /* print device info to dmesg */
1229                         printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1230                                ap->id, device,
1231                                major_version,
1232                                ata_mode_string(xfer_modes),
1233                                (unsigned long long)dev->n_sectors,
1234                                dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1235                 } else { 
1236                         /* CHS */
1237
1238                         /* Default translation */
1239                         dev->cylinders  = dev->id[1];
1240                         dev->heads      = dev->id[3];
1241                         dev->sectors    = dev->id[6];
1242                         dev->n_sectors  = dev->cylinders * dev->heads * dev->sectors;
1243
1244                         if (ata_id_current_chs_valid(dev->id)) {
1245                                 /* Current CHS translation is valid. */
1246                                 dev->cylinders = dev->id[54];
1247                                 dev->heads     = dev->id[55];
1248                                 dev->sectors   = dev->id[56];
1249                                 
1250                                 dev->n_sectors = ata_id_u32(dev->id, 57);
1251                         }
1252
1253                         /* print device info to dmesg */
1254                         printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1255                                ap->id, device,
1256                                major_version,
1257                                ata_mode_string(xfer_modes),
1258                                (unsigned long long)dev->n_sectors,
1259                                (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1260
1261                 }
1262
1263                 ap->host->max_cmd_len = 16;
1264         }
1265
1266         /* ATAPI-specific feature tests */
1267         else {
1268                 if (ata_id_is_ata(dev->id))             /* sanity check */
1269                         goto err_out_nosup;
1270
1271                 rc = atapi_cdb_len(dev->id);
1272                 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1273                         printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1274                         goto err_out_nosup;
1275                 }
1276                 ap->cdb_len = (unsigned int) rc;
1277                 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1278
1279                 /* print device info to dmesg */
1280                 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1281                        ap->id, device,
1282                        ata_mode_string(xfer_modes));
1283         }
1284
1285         DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1286         return;
1287
1288 err_out_nosup:
1289         printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1290                ap->id, device);
1291 err_out:
1292         dev->class++;   /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1293         DPRINTK("EXIT, err\n");
1294 }
1295
1296
1297 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1298 {
1299         return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1300 }
1301
1302 /**
1303  *      ata_dev_config - Run device specific handlers and check for
1304  *                       SATA->PATA bridges
1305  *      @ap: Bus
1306  *      @i:  Device
1307  *
1308  *      LOCKING:
1309  */
1310
1311 void ata_dev_config(struct ata_port *ap, unsigned int i)
1312 {
1313         /* limit bridge transfers to udma5, 200 sectors */
1314         if (ata_dev_knobble(ap)) {
1315                 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1316                         ap->id, ap->device->devno);
1317                 ap->udma_mask &= ATA_UDMA5;
1318                 ap->host->max_sectors = ATA_MAX_SECTORS;
1319                 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1320                 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1321         }
1322
1323         if (ap->ops->dev_config)
1324                 ap->ops->dev_config(ap, &ap->device[i]);
1325 }
1326
1327 /**
1328  *      ata_bus_probe - Reset and probe ATA bus
1329  *      @ap: Bus to probe
1330  *
1331  *      Master ATA bus probing function.  Initiates a hardware-dependent
1332  *      bus reset, then attempts to identify any devices found on
1333  *      the bus.
1334  *
1335  *      LOCKING:
1336  *      PCI/etc. bus probe sem.
1337  *
1338  *      RETURNS:
1339  *      Zero on success, non-zero on error.
1340  */
1341
1342 static int ata_bus_probe(struct ata_port *ap)
1343 {
1344         unsigned int i, found = 0;
1345
1346         ap->ops->phy_reset(ap);
1347         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1348                 goto err_out;
1349
1350         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1351                 ata_dev_identify(ap, i);
1352                 if (ata_dev_present(&ap->device[i])) {
1353                         found = 1;
1354                         ata_dev_config(ap,i);
1355                 }
1356         }
1357
1358         if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1359                 goto err_out_disable;
1360
1361         ata_set_mode(ap);
1362         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1363                 goto err_out_disable;
1364
1365         return 0;
1366
1367 err_out_disable:
1368         ap->ops->port_disable(ap);
1369 err_out:
1370         return -1;
1371 }
1372
1373 /**
1374  *      ata_port_probe - Mark port as enabled
1375  *      @ap: Port for which we indicate enablement
1376  *
1377  *      Modify @ap data structure such that the system
1378  *      thinks that the entire port is enabled.
1379  *
1380  *      LOCKING: host_set lock, or some other form of
1381  *      serialization.
1382  */
1383
1384 void ata_port_probe(struct ata_port *ap)
1385 {
1386         ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1387 }
1388
1389 /**
1390  *      __sata_phy_reset - Wake/reset a low-level SATA PHY
1391  *      @ap: SATA port associated with target SATA PHY.
1392  *
1393  *      This function issues commands to standard SATA Sxxx
1394  *      PHY registers, to wake up the phy (and device), and
1395  *      clear any reset condition.
1396  *
1397  *      LOCKING:
1398  *      PCI/etc. bus probe sem.
1399  *
1400  */
1401 void __sata_phy_reset(struct ata_port *ap)
1402 {
1403         u32 sstatus;
1404         unsigned long timeout = jiffies + (HZ * 5);
1405
1406         if (ap->flags & ATA_FLAG_SATA_RESET) {
1407                 /* issue phy wake/reset */
1408                 scr_write_flush(ap, SCR_CONTROL, 0x301);
1409                 /* Couldn't find anything in SATA I/II specs, but
1410                  * AHCI-1.1 10.4.2 says at least 1 ms. */
1411                 mdelay(1);
1412         }
1413         scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1414
1415         /* wait for phy to become ready, if necessary */
1416         do {
1417                 msleep(200);
1418                 sstatus = scr_read(ap, SCR_STATUS);
1419                 if ((sstatus & 0xf) != 1)
1420                         break;
1421         } while (time_before(jiffies, timeout));
1422
1423         /* TODO: phy layer with polling, timeouts, etc. */
1424         if (sata_dev_present(ap))
1425                 ata_port_probe(ap);
1426         else {
1427                 sstatus = scr_read(ap, SCR_STATUS);
1428                 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1429                        ap->id, sstatus);
1430                 ata_port_disable(ap);
1431         }
1432
1433         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1434                 return;
1435
1436         if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1437                 ata_port_disable(ap);
1438                 return;
1439         }
1440
1441         ap->cbl = ATA_CBL_SATA;
1442 }
1443
1444 /**
1445  *      sata_phy_reset - Reset SATA bus.
1446  *      @ap: SATA port associated with target SATA PHY.
1447  *
1448  *      This function resets the SATA bus, and then probes
1449  *      the bus for devices.
1450  *
1451  *      LOCKING:
1452  *      PCI/etc. bus probe sem.
1453  *
1454  */
1455 void sata_phy_reset(struct ata_port *ap)
1456 {
1457         __sata_phy_reset(ap);
1458         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1459                 return;
1460         ata_bus_reset(ap);
1461 }
1462
1463 /**
1464  *      ata_port_disable - Disable port.
1465  *      @ap: Port to be disabled.
1466  *
1467  *      Modify @ap data structure such that the system
1468  *      thinks that the entire port is disabled, and should
1469  *      never attempt to probe or communicate with devices
1470  *      on this port.
1471  *
1472  *      LOCKING: host_set lock, or some other form of
1473  *      serialization.
1474  */
1475
1476 void ata_port_disable(struct ata_port *ap)
1477 {
1478         ap->device[0].class = ATA_DEV_NONE;
1479         ap->device[1].class = ATA_DEV_NONE;
1480         ap->flags |= ATA_FLAG_PORT_DISABLED;
1481 }
1482
1483 /*
1484  * This mode timing computation functionality is ported over from
1485  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1486  */
1487 /*
1488  * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1489  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1490  * for PIO 5, which is a nonstandard extension and UDMA6, which
1491  * is currently supported only by Maxtor drives. 
1492  */
1493
1494 static const struct ata_timing ata_timing[] = {
1495
1496         { XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
1497         { XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
1498         { XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
1499         { XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
1500
1501         { XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
1502         { XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
1503         { XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
1504
1505 /*      { XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
1506                                           
1507         { XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
1508         { XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
1509         { XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
1510                                           
1511         { XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
1512         { XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
1513         { XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
1514
1515 /*      { XFER_PIO_5,     20,  50,  30, 100,  50,  30, 100,   0 }, */
1516         { XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
1517         { XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
1518
1519         { XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
1520         { XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
1521         { XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
1522
1523 /*      { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
1524
1525         { 0xFF }
1526 };
1527
1528 #define ENOUGH(v,unit)          (((v)-1)/(unit)+1)
1529 #define EZ(v,unit)              ((v)?ENOUGH(v,unit):0)
1530
1531 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1532 {
1533         q->setup   = EZ(t->setup   * 1000,  T);
1534         q->act8b   = EZ(t->act8b   * 1000,  T);
1535         q->rec8b   = EZ(t->rec8b   * 1000,  T);
1536         q->cyc8b   = EZ(t->cyc8b   * 1000,  T);
1537         q->active  = EZ(t->active  * 1000,  T);
1538         q->recover = EZ(t->recover * 1000,  T);
1539         q->cycle   = EZ(t->cycle   * 1000,  T);
1540         q->udma    = EZ(t->udma    * 1000, UT);
1541 }
1542
1543 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1544                       struct ata_timing *m, unsigned int what)
1545 {
1546         if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
1547         if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
1548         if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
1549         if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
1550         if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
1551         if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1552         if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
1553         if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
1554 }
1555
1556 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1557 {
1558         const struct ata_timing *t;
1559
1560         for (t = ata_timing; t->mode != speed; t++)
1561                 if (t->mode == 0xFF)
1562                         return NULL;
1563         return t; 
1564 }
1565
1566 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1567                        struct ata_timing *t, int T, int UT)
1568 {
1569         const struct ata_timing *s;
1570         struct ata_timing p;
1571
1572         /*
1573          * Find the mode. 
1574         */
1575
1576         if (!(s = ata_timing_find_mode(speed)))
1577                 return -EINVAL;
1578
1579         /*
1580          * If the drive is an EIDE drive, it can tell us it needs extended
1581          * PIO/MW_DMA cycle timing.
1582          */
1583
1584         if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1585                 memset(&p, 0, sizeof(p));
1586                 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1587                         if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1588                                             else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1589                 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1590                         p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1591                 }
1592                 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1593         }
1594
1595         /*
1596          * Convert the timing to bus clock counts.
1597          */
1598
1599         ata_timing_quantize(s, t, T, UT);
1600
1601         /*
1602          * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1603          * and some other commands. We have to ensure that the DMA cycle timing is
1604          * slower/equal than the fastest PIO timing.
1605          */
1606
1607         if (speed > XFER_PIO_4) {
1608                 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1609                 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1610         }
1611
1612         /*
1613          * Lenghten active & recovery time so that cycle time is correct.
1614          */
1615
1616         if (t->act8b + t->rec8b < t->cyc8b) {
1617                 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1618                 t->rec8b = t->cyc8b - t->act8b;
1619         }
1620
1621         if (t->active + t->recover < t->cycle) {
1622                 t->active += (t->cycle - (t->active + t->recover)) / 2;
1623                 t->recover = t->cycle - t->active;
1624         }
1625
1626         return 0;
1627 }
1628
1629 static const struct {
1630         unsigned int shift;
1631         u8 base;
1632 } xfer_mode_classes[] = {
1633         { ATA_SHIFT_UDMA,       XFER_UDMA_0 },
1634         { ATA_SHIFT_MWDMA,      XFER_MW_DMA_0 },
1635         { ATA_SHIFT_PIO,        XFER_PIO_0 },
1636 };
1637
1638 static inline u8 base_from_shift(unsigned int shift)
1639 {
1640         int i;
1641
1642         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1643                 if (xfer_mode_classes[i].shift == shift)
1644                         return xfer_mode_classes[i].base;
1645
1646         return 0xff;
1647 }
1648
1649 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1650 {
1651         int ofs, idx;
1652         u8 base;
1653
1654         if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1655                 return;
1656
1657         if (dev->xfer_shift == ATA_SHIFT_PIO)
1658                 dev->flags |= ATA_DFLAG_PIO;
1659
1660         ata_dev_set_xfermode(ap, dev);
1661
1662         base = base_from_shift(dev->xfer_shift);
1663         ofs = dev->xfer_mode - base;
1664         idx = ofs + dev->xfer_shift;
1665         WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1666
1667         DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1668                 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1669
1670         printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1671                 ap->id, dev->devno, xfer_mode_str[idx]);
1672 }
1673
1674 static int ata_host_set_pio(struct ata_port *ap)
1675 {
1676         unsigned int mask;
1677         int x, i;
1678         u8 base, xfer_mode;
1679
1680         mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1681         x = fgb(mask);
1682         if (x < 0) {
1683                 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1684                 return -1;
1685         }
1686
1687         base = base_from_shift(ATA_SHIFT_PIO);
1688         xfer_mode = base + x;
1689
1690         DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1691                 (int)base, (int)xfer_mode, mask, x);
1692
1693         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1694                 struct ata_device *dev = &ap->device[i];
1695                 if (ata_dev_present(dev)) {
1696                         dev->pio_mode = xfer_mode;
1697                         dev->xfer_mode = xfer_mode;
1698                         dev->xfer_shift = ATA_SHIFT_PIO;
1699                         if (ap->ops->set_piomode)
1700                                 ap->ops->set_piomode(ap, dev);
1701                 }
1702         }
1703
1704         return 0;
1705 }
1706
1707 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1708                             unsigned int xfer_shift)
1709 {
1710         int i;
1711
1712         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1713                 struct ata_device *dev = &ap->device[i];
1714                 if (ata_dev_present(dev)) {
1715                         dev->dma_mode = xfer_mode;
1716                         dev->xfer_mode = xfer_mode;
1717                         dev->xfer_shift = xfer_shift;
1718                         if (ap->ops->set_dmamode)
1719                                 ap->ops->set_dmamode(ap, dev);
1720                 }
1721         }
1722 }
1723
1724 /**
1725  *      ata_set_mode - Program timings and issue SET FEATURES - XFER
1726  *      @ap: port on which timings will be programmed
1727  *
1728  *      Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1729  *
1730  *      LOCKING:
1731  *      PCI/etc. bus probe sem.
1732  *
1733  */
1734 static void ata_set_mode(struct ata_port *ap)
1735 {
1736         unsigned int xfer_shift;
1737         u8 xfer_mode;
1738         int rc;
1739
1740         /* step 1: always set host PIO timings */
1741         rc = ata_host_set_pio(ap);
1742         if (rc)
1743                 goto err_out;
1744
1745         /* step 2: choose the best data xfer mode */
1746         xfer_mode = xfer_shift = 0;
1747         rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1748         if (rc)
1749                 goto err_out;
1750
1751         /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1752         if (xfer_shift != ATA_SHIFT_PIO)
1753                 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1754
1755         /* step 4: update devices' xfer mode */
1756         ata_dev_set_mode(ap, &ap->device[0]);
1757         ata_dev_set_mode(ap, &ap->device[1]);
1758
1759         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1760                 return;
1761
1762         if (ap->ops->post_set_mode)
1763                 ap->ops->post_set_mode(ap);
1764
1765         return;
1766
1767 err_out:
1768         ata_port_disable(ap);
1769 }
1770
1771 /**
1772  *      ata_busy_sleep - sleep until BSY clears, or timeout
1773  *      @ap: port containing status register to be polled
1774  *      @tmout_pat: impatience timeout
1775  *      @tmout: overall timeout
1776  *
1777  *      Sleep until ATA Status register bit BSY clears,
1778  *      or a timeout occurs.
1779  *
1780  *      LOCKING: None.
1781  *
1782  */
1783
1784 static unsigned int ata_busy_sleep (struct ata_port *ap,
1785                                     unsigned long tmout_pat,
1786                                     unsigned long tmout)
1787 {
1788         unsigned long timer_start, timeout;
1789         u8 status;
1790
1791         status = ata_busy_wait(ap, ATA_BUSY, 300);
1792         timer_start = jiffies;
1793         timeout = timer_start + tmout_pat;
1794         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1795                 msleep(50);
1796                 status = ata_busy_wait(ap, ATA_BUSY, 3);
1797         }
1798
1799         if (status & ATA_BUSY)
1800                 printk(KERN_WARNING "ata%u is slow to respond, "
1801                        "please be patient\n", ap->id);
1802
1803         timeout = timer_start + tmout;
1804         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1805                 msleep(50);
1806                 status = ata_chk_status(ap);
1807         }
1808
1809         if (status & ATA_BUSY) {
1810                 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1811                        ap->id, tmout / HZ);
1812                 return 1;
1813         }
1814
1815         return 0;
1816 }
1817
1818 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1819 {
1820         struct ata_ioports *ioaddr = &ap->ioaddr;
1821         unsigned int dev0 = devmask & (1 << 0);
1822         unsigned int dev1 = devmask & (1 << 1);
1823         unsigned long timeout;
1824
1825         /* if device 0 was found in ata_devchk, wait for its
1826          * BSY bit to clear
1827          */
1828         if (dev0)
1829                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1830
1831         /* if device 1 was found in ata_devchk, wait for
1832          * register access, then wait for BSY to clear
1833          */
1834         timeout = jiffies + ATA_TMOUT_BOOT;
1835         while (dev1) {
1836                 u8 nsect, lbal;
1837
1838                 ap->ops->dev_select(ap, 1);
1839                 if (ap->flags & ATA_FLAG_MMIO) {
1840                         nsect = readb((void __iomem *) ioaddr->nsect_addr);
1841                         lbal = readb((void __iomem *) ioaddr->lbal_addr);
1842                 } else {
1843                         nsect = inb(ioaddr->nsect_addr);
1844                         lbal = inb(ioaddr->lbal_addr);
1845                 }
1846                 if ((nsect == 1) && (lbal == 1))
1847                         break;
1848                 if (time_after(jiffies, timeout)) {
1849                         dev1 = 0;
1850                         break;
1851                 }
1852                 msleep(50);     /* give drive a breather */
1853         }
1854         if (dev1)
1855                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1856
1857         /* is all this really necessary? */
1858         ap->ops->dev_select(ap, 0);
1859         if (dev1)
1860                 ap->ops->dev_select(ap, 1);
1861         if (dev0)
1862                 ap->ops->dev_select(ap, 0);
1863 }
1864
1865 /**
1866  *      ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1867  *      @ap: Port to reset and probe
1868  *
1869  *      Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1870  *      probe the bus.  Not often used these days.
1871  *
1872  *      LOCKING:
1873  *      PCI/etc. bus probe sem.
1874  *      Obtains host_set lock.
1875  *
1876  */
1877
1878 static unsigned int ata_bus_edd(struct ata_port *ap)
1879 {
1880         struct ata_taskfile tf;
1881         unsigned long flags;
1882
1883         /* set up execute-device-diag (bus reset) taskfile */
1884         /* also, take interrupts to a known state (disabled) */
1885         DPRINTK("execute-device-diag\n");
1886         ata_tf_init(ap, &tf, 0);
1887         tf.ctl |= ATA_NIEN;
1888         tf.command = ATA_CMD_EDD;
1889         tf.protocol = ATA_PROT_NODATA;
1890
1891         /* do bus reset */
1892         spin_lock_irqsave(&ap->host_set->lock, flags);
1893         ata_tf_to_host(ap, &tf);
1894         spin_unlock_irqrestore(&ap->host_set->lock, flags);
1895
1896         /* spec says at least 2ms.  but who knows with those
1897          * crazy ATAPI devices...
1898          */
1899         msleep(150);
1900
1901         return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1902 }
1903
1904 static unsigned int ata_bus_softreset(struct ata_port *ap,
1905                                       unsigned int devmask)
1906 {
1907         struct ata_ioports *ioaddr = &ap->ioaddr;
1908
1909         DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1910
1911         /* software reset.  causes dev0 to be selected */
1912         if (ap->flags & ATA_FLAG_MMIO) {
1913                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1914                 udelay(20);     /* FIXME: flush */
1915                 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1916                 udelay(20);     /* FIXME: flush */
1917                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1918         } else {
1919                 outb(ap->ctl, ioaddr->ctl_addr);
1920                 udelay(10);
1921                 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1922                 udelay(10);
1923                 outb(ap->ctl, ioaddr->ctl_addr);
1924         }
1925
1926         /* spec mandates ">= 2ms" before checking status.
1927          * We wait 150ms, because that was the magic delay used for
1928          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1929          * between when the ATA command register is written, and then
1930          * status is checked.  Because waiting for "a while" before
1931          * checking status is fine, post SRST, we perform this magic
1932          * delay here as well.
1933          */
1934         msleep(150);
1935
1936         ata_bus_post_reset(ap, devmask);
1937
1938         return 0;
1939 }
1940
1941 /**
1942  *      ata_bus_reset - reset host port and associated ATA channel
1943  *      @ap: port to reset
1944  *
1945  *      This is typically the first time we actually start issuing
1946  *      commands to the ATA channel.  We wait for BSY to clear, then
1947  *      issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1948  *      result.  Determine what devices, if any, are on the channel
1949  *      by looking at the device 0/1 error register.  Look at the signature
1950  *      stored in each device's taskfile registers, to determine if
1951  *      the device is ATA or ATAPI.
1952  *
1953  *      LOCKING:
1954  *      PCI/etc. bus probe sem.
1955  *      Obtains host_set lock.
1956  *
1957  *      SIDE EFFECTS:
1958  *      Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1959  */
1960
1961 void ata_bus_reset(struct ata_port *ap)
1962 {
1963         struct ata_ioports *ioaddr = &ap->ioaddr;
1964         unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1965         u8 err;
1966         unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1967
1968         DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1969
1970         /* determine if device 0/1 are present */
1971         if (ap->flags & ATA_FLAG_SATA_RESET)
1972                 dev0 = 1;
1973         else {
1974                 dev0 = ata_devchk(ap, 0);
1975                 if (slave_possible)
1976                         dev1 = ata_devchk(ap, 1);
1977         }
1978
1979         if (dev0)
1980                 devmask |= (1 << 0);
1981         if (dev1)
1982                 devmask |= (1 << 1);
1983
1984         /* select device 0 again */
1985         ap->ops->dev_select(ap, 0);
1986
1987         /* issue bus reset */
1988         if (ap->flags & ATA_FLAG_SRST)
1989                 rc = ata_bus_softreset(ap, devmask);
1990         else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1991                 /* set up device control */
1992                 if (ap->flags & ATA_FLAG_MMIO)
1993                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1994                 else
1995                         outb(ap->ctl, ioaddr->ctl_addr);
1996                 rc = ata_bus_edd(ap);
1997         }
1998
1999         if (rc)
2000                 goto err_out;
2001
2002         /*
2003          * determine by signature whether we have ATA or ATAPI devices
2004          */
2005         err = ata_dev_try_classify(ap, 0);
2006         if ((slave_possible) && (err != 0x81))
2007                 ata_dev_try_classify(ap, 1);
2008
2009         /* re-enable interrupts */
2010         if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
2011                 ata_irq_on(ap);
2012
2013         /* is double-select really necessary? */
2014         if (ap->device[1].class != ATA_DEV_NONE)
2015                 ap->ops->dev_select(ap, 1);
2016         if (ap->device[0].class != ATA_DEV_NONE)
2017                 ap->ops->dev_select(ap, 0);
2018
2019         /* if no devices were detected, disable this port */
2020         if ((ap->device[0].class == ATA_DEV_NONE) &&
2021             (ap->device[1].class == ATA_DEV_NONE))
2022                 goto err_out;
2023
2024         if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2025                 /* set up device control for ATA_FLAG_SATA_RESET */
2026                 if (ap->flags & ATA_FLAG_MMIO)
2027                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2028                 else
2029                         outb(ap->ctl, ioaddr->ctl_addr);
2030         }
2031
2032         DPRINTK("EXIT\n");
2033         return;
2034
2035 err_out:
2036         printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2037         ap->ops->port_disable(ap);
2038
2039         DPRINTK("EXIT\n");
2040 }
2041
2042 static void ata_pr_blacklisted(const struct ata_port *ap,
2043                                const struct ata_device *dev)
2044 {
2045         printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2046                 ap->id, dev->devno);
2047 }
2048
2049 static const char * ata_dma_blacklist [] = {
2050         "WDC AC11000H",
2051         "WDC AC22100H",
2052         "WDC AC32500H",
2053         "WDC AC33100H",
2054         "WDC AC31600H",
2055         "WDC AC32100H",
2056         "WDC AC23200L",
2057         "Compaq CRD-8241B",
2058         "CRD-8400B",
2059         "CRD-8480B",
2060         "CRD-8482B",
2061         "CRD-84",
2062         "SanDisk SDP3B",
2063         "SanDisk SDP3B-64",
2064         "SANYO CD-ROM CRD",
2065         "HITACHI CDR-8",
2066         "HITACHI CDR-8335",
2067         "HITACHI CDR-8435",
2068         "Toshiba CD-ROM XM-6202B",
2069         "TOSHIBA CD-ROM XM-1702BC",
2070         "CD-532E-A",
2071         "E-IDE CD-ROM CR-840",
2072         "CD-ROM Drive/F5A",
2073         "WPI CDD-820",
2074         "SAMSUNG CD-ROM SC-148C",
2075         "SAMSUNG CD-ROM SC",
2076         "SanDisk SDP3B-64",
2077         "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2078         "_NEC DV5800A",
2079 };
2080
2081 static int ata_dma_blacklisted(const struct ata_device *dev)
2082 {
2083         unsigned char model_num[40];
2084         char *s;
2085         unsigned int len;
2086         int i;
2087
2088         ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2089                           sizeof(model_num));
2090         s = &model_num[0];
2091         len = strnlen(s, sizeof(model_num));
2092
2093         /* ATAPI specifies that empty space is blank-filled; remove blanks */
2094         while ((len > 0) && (s[len - 1] == ' ')) {
2095                 len--;
2096                 s[len] = 0;
2097         }
2098
2099         for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2100                 if (!strncmp(ata_dma_blacklist[i], s, len))
2101                         return 1;
2102
2103         return 0;
2104 }
2105
2106 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2107 {
2108         const struct ata_device *master, *slave;
2109         unsigned int mask;
2110
2111         master = &ap->device[0];
2112         slave = &ap->device[1];
2113
2114         assert (ata_dev_present(master) || ata_dev_present(slave));
2115
2116         if (shift == ATA_SHIFT_UDMA) {
2117                 mask = ap->udma_mask;
2118                 if (ata_dev_present(master)) {
2119                         mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2120                         if (ata_dma_blacklisted(master)) {
2121                                 mask = 0;
2122                                 ata_pr_blacklisted(ap, master);
2123                         }
2124                 }
2125                 if (ata_dev_present(slave)) {
2126                         mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2127                         if (ata_dma_blacklisted(slave)) {
2128                                 mask = 0;
2129                                 ata_pr_blacklisted(ap, slave);
2130                         }
2131                 }
2132         }
2133         else if (shift == ATA_SHIFT_MWDMA) {
2134                 mask = ap->mwdma_mask;
2135                 if (ata_dev_present(master)) {
2136                         mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2137                         if (ata_dma_blacklisted(master)) {
2138                                 mask = 0;
2139                                 ata_pr_blacklisted(ap, master);
2140                         }
2141                 }
2142                 if (ata_dev_present(slave)) {
2143                         mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2144                         if (ata_dma_blacklisted(slave)) {
2145                                 mask = 0;
2146                                 ata_pr_blacklisted(ap, slave);
2147                         }
2148                 }
2149         }
2150         else if (shift == ATA_SHIFT_PIO) {
2151                 mask = ap->pio_mask;
2152                 if (ata_dev_present(master)) {
2153                         /* spec doesn't return explicit support for
2154                          * PIO0-2, so we fake it
2155                          */
2156                         u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2157                         tmp_mode <<= 3;
2158                         tmp_mode |= 0x7;
2159                         mask &= tmp_mode;
2160                 }
2161                 if (ata_dev_present(slave)) {
2162                         /* spec doesn't return explicit support for
2163                          * PIO0-2, so we fake it
2164                          */
2165                         u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2166                         tmp_mode <<= 3;
2167                         tmp_mode |= 0x7;
2168                         mask &= tmp_mode;
2169                 }
2170         }
2171         else {
2172                 mask = 0xffffffff; /* shut up compiler warning */
2173                 BUG();
2174         }
2175
2176         return mask;
2177 }
2178
2179 /* find greatest bit */
2180 static int fgb(u32 bitmap)
2181 {
2182         unsigned int i;
2183         int x = -1;
2184
2185         for (i = 0; i < 32; i++)
2186                 if (bitmap & (1 << i))
2187                         x = i;
2188
2189         return x;
2190 }
2191
2192 /**
2193  *      ata_choose_xfer_mode - attempt to find best transfer mode
2194  *      @ap: Port for which an xfer mode will be selected
2195  *      @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2196  *      @xfer_shift_out: (output) bit shift that selects this mode
2197  *
2198  *      Based on host and device capabilities, determine the
2199  *      maximum transfer mode that is amenable to all.
2200  *
2201  *      LOCKING:
2202  *      PCI/etc. bus probe sem.
2203  *
2204  *      RETURNS:
2205  *      Zero on success, negative on error.
2206  */
2207
2208 static int ata_choose_xfer_mode(const struct ata_port *ap,
2209                                 u8 *xfer_mode_out,
2210                                 unsigned int *xfer_shift_out)
2211 {
2212         unsigned int mask, shift;
2213         int x, i;
2214
2215         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2216                 shift = xfer_mode_classes[i].shift;
2217                 mask = ata_get_mode_mask(ap, shift);
2218
2219                 x = fgb(mask);
2220                 if (x >= 0) {
2221                         *xfer_mode_out = xfer_mode_classes[i].base + x;
2222                         *xfer_shift_out = shift;
2223                         return 0;
2224                 }
2225         }
2226
2227         return -1;
2228 }
2229
2230 /**
2231  *      ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2232  *      @ap: Port associated with device @dev
2233  *      @dev: Device to which command will be sent
2234  *
2235  *      Issue SET FEATURES - XFER MODE command to device @dev
2236  *      on port @ap.
2237  *
2238  *      LOCKING:
2239  *      PCI/etc. bus probe sem.
2240  */
2241
2242 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2243 {
2244         DECLARE_COMPLETION(wait);
2245         struct ata_queued_cmd *qc;
2246         int rc;
2247         unsigned long flags;
2248
2249         /* set up set-features taskfile */
2250         DPRINTK("set features - xfer mode\n");
2251
2252         qc = ata_qc_new_init(ap, dev);
2253         BUG_ON(qc == NULL);
2254
2255         qc->tf.command = ATA_CMD_SET_FEATURES;
2256         qc->tf.feature = SETFEATURES_XFER;
2257         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2258         qc->tf.protocol = ATA_PROT_NODATA;
2259         qc->tf.nsect = dev->xfer_mode;
2260
2261         qc->waiting = &wait;
2262         qc->complete_fn = ata_qc_complete_noop;
2263
2264         spin_lock_irqsave(&ap->host_set->lock, flags);
2265         rc = ata_qc_issue(qc);
2266         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2267
2268         if (rc)
2269                 ata_port_disable(ap);
2270         else
2271                 wait_for_completion(&wait);
2272
2273         DPRINTK("EXIT\n");
2274 }
2275
2276 /**
2277  *      ata_dev_reread_id - Reread the device identify device info
2278  *      @ap: port where the device is
2279  *      @dev: device to reread the identify device info
2280  *
2281  *      LOCKING:
2282  */
2283
2284 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2285 {
2286         DECLARE_COMPLETION(wait);
2287         struct ata_queued_cmd *qc;
2288         unsigned long flags;
2289         int rc;
2290
2291         qc = ata_qc_new_init(ap, dev);
2292         BUG_ON(qc == NULL);
2293
2294         ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2295         qc->dma_dir = DMA_FROM_DEVICE;
2296
2297         if (dev->class == ATA_DEV_ATA) {
2298                 qc->tf.command = ATA_CMD_ID_ATA;
2299                 DPRINTK("do ATA identify\n");
2300         } else {
2301                 qc->tf.command = ATA_CMD_ID_ATAPI;
2302                 DPRINTK("do ATAPI identify\n");
2303         }
2304
2305         qc->tf.flags |= ATA_TFLAG_DEVICE;
2306         qc->tf.protocol = ATA_PROT_PIO;
2307         qc->nsect = 1;
2308
2309         qc->waiting = &wait;
2310         qc->complete_fn = ata_qc_complete_noop;
2311
2312         spin_lock_irqsave(&ap->host_set->lock, flags);
2313         rc = ata_qc_issue(qc);
2314         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2315
2316         if (rc)
2317                 goto err_out;
2318
2319         wait_for_completion(&wait);
2320
2321         swap_buf_le16(dev->id, ATA_ID_WORDS);
2322
2323         ata_dump_id(dev);
2324
2325         DPRINTK("EXIT\n");
2326
2327         return;
2328 err_out:
2329         ata_port_disable(ap);
2330 }
2331
2332 /**
2333  *      ata_dev_init_params - Issue INIT DEV PARAMS command
2334  *      @ap: Port associated with device @dev
2335  *      @dev: Device to which command will be sent
2336  *
2337  *      LOCKING:
2338  */
2339
2340 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2341 {
2342         DECLARE_COMPLETION(wait);
2343         struct ata_queued_cmd *qc;
2344         int rc;
2345         unsigned long flags;
2346         u16 sectors = dev->id[6];
2347         u16 heads   = dev->id[3];
2348
2349         /* Number of sectors per track 1-255. Number of heads 1-16 */
2350         if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2351                 return;
2352
2353         /* set up init dev params taskfile */
2354         DPRINTK("init dev params \n");
2355
2356         qc = ata_qc_new_init(ap, dev);
2357         BUG_ON(qc == NULL);
2358
2359         qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2360         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2361         qc->tf.protocol = ATA_PROT_NODATA;
2362         qc->tf.nsect = sectors;
2363         qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2364
2365         qc->waiting = &wait;
2366         qc->complete_fn = ata_qc_complete_noop;
2367
2368         spin_lock_irqsave(&ap->host_set->lock, flags);
2369         rc = ata_qc_issue(qc);
2370         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2371
2372         if (rc)
2373                 ata_port_disable(ap);
2374         else
2375                 wait_for_completion(&wait);
2376
2377         DPRINTK("EXIT\n");
2378 }
2379
2380 /**
2381  *      ata_sg_clean - Unmap DMA memory associated with command
2382  *      @qc: Command containing DMA memory to be released
2383  *
2384  *      Unmap all mapped DMA memory associated with this command.
2385  *
2386  *      LOCKING:
2387  *      spin_lock_irqsave(host_set lock)
2388  */
2389
2390 static void ata_sg_clean(struct ata_queued_cmd *qc)
2391 {
2392         struct ata_port *ap = qc->ap;
2393         struct scatterlist *sg = qc->__sg;
2394         int dir = qc->dma_dir;
2395         void *pad_buf = NULL;
2396
2397         assert(qc->flags & ATA_QCFLAG_DMAMAP);
2398         assert(sg != NULL);
2399
2400         if (qc->flags & ATA_QCFLAG_SINGLE)
2401                 assert(qc->n_elem == 1);
2402
2403         DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2404
2405         /* if we padded the buffer out to 32-bit bound, and data
2406          * xfer direction is from-device, we must copy from the
2407          * pad buffer back into the supplied buffer
2408          */
2409         if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2410                 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2411
2412         if (qc->flags & ATA_QCFLAG_SG) {
2413                 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2414                 /* restore last sg */
2415                 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2416                 if (pad_buf) {
2417                         struct scatterlist *psg = &qc->pad_sgent;
2418                         void *addr = kmap_atomic(psg->page, KM_IRQ0);
2419                         memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2420                         kunmap_atomic(psg->page, KM_IRQ0);
2421                 }
2422         } else {
2423                 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2424                                  sg_dma_len(&sg[0]), dir);
2425                 /* restore sg */
2426                 sg->length += qc->pad_len;
2427                 if (pad_buf)
2428                         memcpy(qc->buf_virt + sg->length - qc->pad_len,
2429                                pad_buf, qc->pad_len);
2430         }
2431
2432         qc->flags &= ~ATA_QCFLAG_DMAMAP;
2433         qc->__sg = NULL;
2434 }
2435
2436 /**
2437  *      ata_fill_sg - Fill PCI IDE PRD table
2438  *      @qc: Metadata associated with taskfile to be transferred
2439  *
2440  *      Fill PCI IDE PRD (scatter-gather) table with segments
2441  *      associated with the current disk command.
2442  *
2443  *      LOCKING:
2444  *      spin_lock_irqsave(host_set lock)
2445  *
2446  */
2447 static void ata_fill_sg(struct ata_queued_cmd *qc)
2448 {
2449         struct ata_port *ap = qc->ap;
2450         struct scatterlist *sg;
2451         unsigned int idx;
2452
2453         assert(qc->__sg != NULL);
2454         assert(qc->n_elem > 0);
2455
2456         idx = 0;
2457         ata_for_each_sg(sg, qc) {
2458                 u32 addr, offset;
2459                 u32 sg_len, len;
2460
2461                 /* determine if physical DMA addr spans 64K boundary.
2462                  * Note h/w doesn't support 64-bit, so we unconditionally
2463                  * truncate dma_addr_t to u32.
2464                  */
2465                 addr = (u32) sg_dma_address(sg);
2466                 sg_len = sg_dma_len(sg);
2467
2468                 while (sg_len) {
2469                         offset = addr & 0xffff;
2470                         len = sg_len;
2471                         if ((offset + sg_len) > 0x10000)
2472                                 len = 0x10000 - offset;
2473
2474                         ap->prd[idx].addr = cpu_to_le32(addr);
2475                         ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2476                         VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2477
2478                         idx++;
2479                         sg_len -= len;
2480                         addr += len;
2481                 }
2482         }
2483
2484         if (idx)
2485                 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2486 }
2487 /**
2488  *      ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2489  *      @qc: Metadata associated with taskfile to check
2490  *
2491  *      Allow low-level driver to filter ATA PACKET commands, returning
2492  *      a status indicating whether or not it is OK to use DMA for the
2493  *      supplied PACKET command.
2494  *
2495  *      LOCKING:
2496  *      spin_lock_irqsave(host_set lock)
2497  *
2498  *      RETURNS: 0 when ATAPI DMA can be used
2499  *               nonzero otherwise
2500  */
2501 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2502 {
2503         struct ata_port *ap = qc->ap;
2504         int rc = 0; /* Assume ATAPI DMA is OK by default */
2505
2506         if (ap->ops->check_atapi_dma)
2507                 rc = ap->ops->check_atapi_dma(qc);
2508
2509         return rc;
2510 }
2511 /**
2512  *      ata_qc_prep - Prepare taskfile for submission
2513  *      @qc: Metadata associated with taskfile to be prepared
2514  *
2515  *      Prepare ATA taskfile for submission.
2516  *
2517  *      LOCKING:
2518  *      spin_lock_irqsave(host_set lock)
2519  */
2520 void ata_qc_prep(struct ata_queued_cmd *qc)
2521 {
2522         if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2523                 return;
2524
2525         ata_fill_sg(qc);
2526 }
2527
2528 /**
2529  *      ata_sg_init_one - Associate command with memory buffer
2530  *      @qc: Command to be associated
2531  *      @buf: Memory buffer
2532  *      @buflen: Length of memory buffer, in bytes.
2533  *
2534  *      Initialize the data-related elements of queued_cmd @qc
2535  *      to point to a single memory buffer, @buf of byte length @buflen.
2536  *
2537  *      LOCKING:
2538  *      spin_lock_irqsave(host_set lock)
2539  */
2540
2541 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2542 {
2543         struct scatterlist *sg;
2544
2545         qc->flags |= ATA_QCFLAG_SINGLE;
2546
2547         memset(&qc->sgent, 0, sizeof(qc->sgent));
2548         qc->__sg = &qc->sgent;
2549         qc->n_elem = 1;
2550         qc->orig_n_elem = 1;
2551         qc->buf_virt = buf;
2552
2553         sg = qc->__sg;
2554         sg_init_one(sg, buf, buflen);
2555 }
2556
2557 /**
2558  *      ata_sg_init - Associate command with scatter-gather table.
2559  *      @qc: Command to be associated
2560  *      @sg: Scatter-gather table.
2561  *      @n_elem: Number of elements in s/g table.
2562  *
2563  *      Initialize the data-related elements of queued_cmd @qc
2564  *      to point to a scatter-gather table @sg, containing @n_elem
2565  *      elements.
2566  *
2567  *      LOCKING:
2568  *      spin_lock_irqsave(host_set lock)
2569  */
2570
2571 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2572                  unsigned int n_elem)
2573 {
2574         qc->flags |= ATA_QCFLAG_SG;
2575         qc->__sg = sg;
2576         qc->n_elem = n_elem;
2577         qc->orig_n_elem = n_elem;
2578 }
2579
2580 /**
2581  *      ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2582  *      @qc: Command with memory buffer to be mapped.
2583  *
2584  *      DMA-map the memory buffer associated with queued_cmd @qc.
2585  *
2586  *      LOCKING:
2587  *      spin_lock_irqsave(host_set lock)
2588  *
2589  *      RETURNS:
2590  *      Zero on success, negative on error.
2591  */
2592
2593 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2594 {
2595         struct ata_port *ap = qc->ap;
2596         int dir = qc->dma_dir;
2597         struct scatterlist *sg = qc->__sg;
2598         dma_addr_t dma_address;
2599
2600         /* we must lengthen transfers to end on a 32-bit boundary */
2601         qc->pad_len = sg->length & 3;
2602         if (qc->pad_len) {
2603                 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2604                 struct scatterlist *psg = &qc->pad_sgent;
2605
2606                 assert(qc->dev->class == ATA_DEV_ATAPI);
2607
2608                 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2609
2610                 if (qc->tf.flags & ATA_TFLAG_WRITE)
2611                         memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2612                                qc->pad_len);
2613
2614                 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2615                 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2616                 /* trim sg */
2617                 sg->length -= qc->pad_len;
2618
2619                 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2620                         sg->length, qc->pad_len);
2621         }
2622
2623         dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2624                                      sg->length, dir);
2625         if (dma_mapping_error(dma_address)) {
2626                 /* restore sg */
2627                 sg->length += qc->pad_len;
2628                 return -1;
2629         }
2630
2631         sg_dma_address(sg) = dma_address;
2632         sg_dma_len(sg) = sg->length;
2633
2634         DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2635                 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2636
2637         return 0;
2638 }
2639
2640 /**
2641  *      ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2642  *      @qc: Command with scatter-gather table to be mapped.
2643  *
2644  *      DMA-map the scatter-gather table associated with queued_cmd @qc.
2645  *
2646  *      LOCKING:
2647  *      spin_lock_irqsave(host_set lock)
2648  *
2649  *      RETURNS:
2650  *      Zero on success, negative on error.
2651  *
2652  */
2653
2654 static int ata_sg_setup(struct ata_queued_cmd *qc)
2655 {
2656         struct ata_port *ap = qc->ap;
2657         struct scatterlist *sg = qc->__sg;
2658         struct scatterlist *lsg = &sg[qc->n_elem - 1];
2659         int n_elem, dir;
2660
2661         VPRINTK("ENTER, ata%u\n", ap->id);
2662         assert(qc->flags & ATA_QCFLAG_SG);
2663
2664         /* we must lengthen transfers to end on a 32-bit boundary */
2665         qc->pad_len = lsg->length & 3;
2666         if (qc->pad_len) {
2667                 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2668                 struct scatterlist *psg = &qc->pad_sgent;
2669                 unsigned int offset;
2670
2671                 assert(qc->dev->class == ATA_DEV_ATAPI);
2672
2673                 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2674
2675                 /*
2676                  * psg->page/offset are used to copy to-be-written
2677                  * data in this function or read data in ata_sg_clean.
2678                  */
2679                 offset = lsg->offset + lsg->length - qc->pad_len;
2680                 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2681                 psg->offset = offset_in_page(offset);
2682
2683                 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2684                         void *addr = kmap_atomic(psg->page, KM_IRQ0);
2685                         memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2686                         kunmap_atomic(psg->page, KM_IRQ0);
2687                 }
2688
2689                 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2690                 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2691                 /* trim last sg */
2692                 lsg->length -= qc->pad_len;
2693
2694                 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2695                         qc->n_elem - 1, lsg->length, qc->pad_len);
2696         }
2697
2698         dir = qc->dma_dir;
2699         n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2700         if (n_elem < 1) {
2701                 /* restore last sg */
2702                 lsg->length += qc->pad_len;
2703                 return -1;
2704         }
2705
2706         DPRINTK("%d sg elements mapped\n", n_elem);
2707
2708         qc->n_elem = n_elem;
2709
2710         return 0;
2711 }
2712
2713 /**
2714  *      ata_poll_qc_complete - turn irq back on and finish qc
2715  *      @qc: Command to complete
2716  *      @err_mask: ATA status register content
2717  *
2718  *      LOCKING:
2719  *      None.  (grabs host lock)
2720  */
2721
2722 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2723 {
2724         struct ata_port *ap = qc->ap;
2725         unsigned long flags;
2726
2727         spin_lock_irqsave(&ap->host_set->lock, flags);
2728         ap->flags &= ~ATA_FLAG_NOINTR;
2729         ata_irq_on(ap);
2730         ata_qc_complete(qc, err_mask);
2731         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2732 }
2733
2734 /**
2735  *      ata_pio_poll -
2736  *      @ap: the target ata_port
2737  *
2738  *      LOCKING:
2739  *      None.  (executing in kernel thread context)
2740  *
2741  *      RETURNS:
2742  *      timeout value to use
2743  */
2744
2745 static unsigned long ata_pio_poll(struct ata_port *ap)
2746 {
2747         u8 status;
2748         unsigned int poll_state = HSM_ST_UNKNOWN;
2749         unsigned int reg_state = HSM_ST_UNKNOWN;
2750         const unsigned int tmout_state = HSM_ST_TMOUT;
2751
2752         switch (ap->hsm_task_state) {
2753         case HSM_ST:
2754         case HSM_ST_POLL:
2755                 poll_state = HSM_ST_POLL;
2756                 reg_state = HSM_ST;
2757                 break;
2758         case HSM_ST_LAST:
2759         case HSM_ST_LAST_POLL:
2760                 poll_state = HSM_ST_LAST_POLL;
2761                 reg_state = HSM_ST_LAST;
2762                 break;
2763         default:
2764                 BUG();
2765                 break;
2766         }
2767
2768         status = ata_chk_status(ap);
2769         if (status & ATA_BUSY) {
2770                 if (time_after(jiffies, ap->pio_task_timeout)) {
2771                         ap->hsm_task_state = tmout_state;
2772                         return 0;
2773                 }
2774                 ap->hsm_task_state = poll_state;
2775                 return ATA_SHORT_PAUSE;
2776         }
2777
2778         ap->hsm_task_state = reg_state;
2779         return 0;
2780 }
2781
2782 /**
2783  *      ata_pio_complete - check if drive is busy or idle
2784  *      @ap: the target ata_port
2785  *
2786  *      LOCKING:
2787  *      None.  (executing in kernel thread context)
2788  *
2789  *      RETURNS:
2790  *      Non-zero if qc completed, zero otherwise.
2791  */
2792
2793 static int ata_pio_complete (struct ata_port *ap)
2794 {
2795         struct ata_queued_cmd *qc;
2796         u8 drv_stat;
2797
2798         /*
2799          * This is purely heuristic.  This is a fast path.  Sometimes when
2800          * we enter, BSY will be cleared in a chk-status or two.  If not,
2801          * the drive is probably seeking or something.  Snooze for a couple
2802          * msecs, then chk-status again.  If still busy, fall back to
2803          * HSM_ST_POLL state.
2804          */
2805         drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2806         if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2807                 msleep(2);
2808                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2809                 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2810                         ap->hsm_task_state = HSM_ST_LAST_POLL;
2811                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2812                         return 0;
2813                 }
2814         }
2815
2816         drv_stat = ata_wait_idle(ap);
2817         if (!ata_ok(drv_stat)) {
2818                 ap->hsm_task_state = HSM_ST_ERR;
2819                 return 0;
2820         }
2821
2822         qc = ata_qc_from_tag(ap, ap->active_tag);
2823         assert(qc != NULL);
2824
2825         ap->hsm_task_state = HSM_ST_IDLE;
2826
2827         ata_poll_qc_complete(qc, 0);
2828
2829         /* another command may start at this point */
2830
2831         return 1;
2832 }
2833
2834
2835 /**
2836  *      swap_buf_le16 - swap halves of 16-words in place
2837  *      @buf:  Buffer to swap
2838  *      @buf_words:  Number of 16-bit words in buffer.
2839  *
2840  *      Swap halves of 16-bit words if needed to convert from
2841  *      little-endian byte order to native cpu byte order, or
2842  *      vice-versa.
2843  *
2844  *      LOCKING:
2845  *      Inherited from caller.
2846  */
2847 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2848 {
2849 #ifdef __BIG_ENDIAN
2850         unsigned int i;
2851
2852         for (i = 0; i < buf_words; i++)
2853                 buf[i] = le16_to_cpu(buf[i]);
2854 #endif /* __BIG_ENDIAN */
2855 }
2856
2857 /**
2858  *      ata_mmio_data_xfer - Transfer data by MMIO
2859  *      @ap: port to read/write
2860  *      @buf: data buffer
2861  *      @buflen: buffer length
2862  *      @write_data: read/write
2863  *
2864  *      Transfer data from/to the device data register by MMIO.
2865  *
2866  *      LOCKING:
2867  *      Inherited from caller.
2868  */
2869
2870 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2871                                unsigned int buflen, int write_data)
2872 {
2873         unsigned int i;
2874         unsigned int words = buflen >> 1;
2875         u16 *buf16 = (u16 *) buf;
2876         void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2877
2878         /* Transfer multiple of 2 bytes */
2879         if (write_data) {
2880                 for (i = 0; i < words; i++)
2881                         writew(le16_to_cpu(buf16[i]), mmio);
2882         } else {
2883                 for (i = 0; i < words; i++)
2884                         buf16[i] = cpu_to_le16(readw(mmio));
2885         }
2886
2887         /* Transfer trailing 1 byte, if any. */
2888         if (unlikely(buflen & 0x01)) {
2889                 u16 align_buf[1] = { 0 };
2890                 unsigned char *trailing_buf = buf + buflen - 1;
2891
2892                 if (write_data) {
2893                         memcpy(align_buf, trailing_buf, 1);
2894                         writew(le16_to_cpu(align_buf[0]), mmio);
2895                 } else {
2896                         align_buf[0] = cpu_to_le16(readw(mmio));
2897                         memcpy(trailing_buf, align_buf, 1);
2898                 }
2899         }
2900 }
2901
2902 /**
2903  *      ata_pio_data_xfer - Transfer data by PIO
2904  *      @ap: port to read/write
2905  *      @buf: data buffer
2906  *      @buflen: buffer length
2907  *      @write_data: read/write
2908  *
2909  *      Transfer data from/to the device data register by PIO.
2910  *
2911  *      LOCKING:
2912  *      Inherited from caller.
2913  */
2914
2915 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2916                               unsigned int buflen, int write_data)
2917 {
2918         unsigned int words = buflen >> 1;
2919
2920         /* Transfer multiple of 2 bytes */
2921         if (write_data)
2922                 outsw(ap->ioaddr.data_addr, buf, words);
2923         else
2924                 insw(ap->ioaddr.data_addr, buf, words);
2925
2926         /* Transfer trailing 1 byte, if any. */
2927         if (unlikely(buflen & 0x01)) {
2928                 u16 align_buf[1] = { 0 };
2929                 unsigned char *trailing_buf = buf + buflen - 1;
2930
2931                 if (write_data) {
2932                         memcpy(align_buf, trailing_buf, 1);
2933                         outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2934                 } else {
2935                         align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2936                         memcpy(trailing_buf, align_buf, 1);
2937                 }
2938         }
2939 }
2940
2941 /**
2942  *      ata_data_xfer - Transfer data from/to the data register.
2943  *      @ap: port to read/write
2944  *      @buf: data buffer
2945  *      @buflen: buffer length
2946  *      @do_write: read/write
2947  *
2948  *      Transfer data from/to the device data register.
2949  *
2950  *      LOCKING:
2951  *      Inherited from caller.
2952  */
2953
2954 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2955                           unsigned int buflen, int do_write)
2956 {
2957         if (ap->flags & ATA_FLAG_MMIO)
2958                 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2959         else
2960                 ata_pio_data_xfer(ap, buf, buflen, do_write);
2961 }
2962
2963 /**
2964  *      ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2965  *      @qc: Command on going
2966  *
2967  *      Transfer ATA_SECT_SIZE of data from/to the ATA device.
2968  *
2969  *      LOCKING:
2970  *      Inherited from caller.
2971  */
2972
2973 static void ata_pio_sector(struct ata_queued_cmd *qc)
2974 {
2975         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2976         struct scatterlist *sg = qc->__sg;
2977         struct ata_port *ap = qc->ap;
2978         struct page *page;
2979         unsigned int offset;
2980         unsigned char *buf;
2981
2982         if (qc->cursect == (qc->nsect - 1))
2983                 ap->hsm_task_state = HSM_ST_LAST;
2984
2985         page = sg[qc->cursg].page;
2986         offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2987
2988         /* get the current page and offset */
2989         page = nth_page(page, (offset >> PAGE_SHIFT));
2990         offset %= PAGE_SIZE;
2991
2992         buf = kmap(page) + offset;
2993
2994         qc->cursect++;
2995         qc->cursg_ofs++;
2996
2997         if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2998                 qc->cursg++;
2999                 qc->cursg_ofs = 0;
3000         }
3001
3002         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3003
3004         /* do the actual data transfer */
3005         do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3006         ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3007
3008         kunmap(page);
3009 }
3010
3011 /**
3012  *      __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3013  *      @qc: Command on going
3014  *      @bytes: number of bytes
3015  *
3016  *      Transfer Transfer data from/to the ATAPI device.
3017  *
3018  *      LOCKING:
3019  *      Inherited from caller.
3020  *
3021  */
3022
3023 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3024 {
3025         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3026         struct scatterlist *sg = qc->__sg;
3027         struct ata_port *ap = qc->ap;
3028         struct page *page;
3029         unsigned char *buf;
3030         unsigned int offset, count;
3031
3032         if (qc->curbytes + bytes >= qc->nbytes)
3033                 ap->hsm_task_state = HSM_ST_LAST;
3034
3035 next_sg:
3036         if (unlikely(qc->cursg >= qc->n_elem)) {
3037                 /*
3038                  * The end of qc->sg is reached and the device expects
3039                  * more data to transfer. In order not to overrun qc->sg
3040                  * and fulfill length specified in the byte count register,
3041                  *    - for read case, discard trailing data from the device
3042                  *    - for write case, padding zero data to the device
3043                  */
3044                 u16 pad_buf[1] = { 0 };
3045                 unsigned int words = bytes >> 1;
3046                 unsigned int i;
3047
3048                 if (words) /* warning if bytes > 1 */
3049                         printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3050                                ap->id, bytes);
3051
3052                 for (i = 0; i < words; i++)
3053                         ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3054
3055                 ap->hsm_task_state = HSM_ST_LAST;
3056                 return;
3057         }
3058
3059         sg = &qc->__sg[qc->cursg];
3060
3061         page = sg->page;
3062         offset = sg->offset + qc->cursg_ofs;
3063
3064         /* get the current page and offset */
3065         page = nth_page(page, (offset >> PAGE_SHIFT));
3066         offset %= PAGE_SIZE;
3067
3068         /* don't overrun current sg */
3069         count = min(sg->length - qc->cursg_ofs, bytes);
3070
3071         /* don't cross page boundaries */
3072         count = min(count, (unsigned int)PAGE_SIZE - offset);
3073
3074         buf = kmap(page) + offset;
3075
3076         bytes -= count;
3077         qc->curbytes += count;
3078         qc->cursg_ofs += count;
3079
3080         if (qc->cursg_ofs == sg->length) {
3081                 qc->cursg++;
3082                 qc->cursg_ofs = 0;
3083         }
3084
3085         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3086
3087         /* do the actual data transfer */
3088         ata_data_xfer(ap, buf, count, do_write);
3089
3090         kunmap(page);
3091
3092         if (bytes)
3093                 goto next_sg;
3094 }
3095
3096 /**
3097  *      atapi_pio_bytes - Transfer data from/to the ATAPI device.
3098  *      @qc: Command on going
3099  *
3100  *      Transfer Transfer data from/to the ATAPI device.
3101  *
3102  *      LOCKING:
3103  *      Inherited from caller.
3104  */
3105
3106 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3107 {
3108         struct ata_port *ap = qc->ap;
3109         struct ata_device *dev = qc->dev;
3110         unsigned int ireason, bc_lo, bc_hi, bytes;
3111         int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3112
3113         ap->ops->tf_read(ap, &qc->tf);
3114         ireason = qc->tf.nsect;
3115         bc_lo = qc->tf.lbam;
3116         bc_hi = qc->tf.lbah;
3117         bytes = (bc_hi << 8) | bc_lo;
3118
3119         /* shall be cleared to zero, indicating xfer of data */
3120         if (ireason & (1 << 0))
3121                 goto err_out;
3122
3123         /* make sure transfer direction matches expected */
3124         i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3125         if (do_write != i_write)
3126                 goto err_out;
3127
3128         __atapi_pio_bytes(qc, bytes);
3129
3130         return;
3131
3132 err_out:
3133         printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3134               ap->id, dev->devno);
3135         ap->hsm_task_state = HSM_ST_ERR;
3136 }
3137
3138 /**
3139  *      ata_pio_block - start PIO on a block
3140  *      @ap: the target ata_port
3141  *
3142  *      LOCKING:
3143  *      None.  (executing in kernel thread context)
3144  */
3145
3146 static void ata_pio_block(struct ata_port *ap)
3147 {
3148         struct ata_queued_cmd *qc;
3149         u8 status;
3150
3151         /*
3152          * This is purely heuristic.  This is a fast path.
3153          * Sometimes when we enter, BSY will be cleared in
3154          * a chk-status or two.  If not, the drive is probably seeking
3155          * or something.  Snooze for a couple msecs, then
3156          * chk-status again.  If still busy, fall back to
3157          * HSM_ST_POLL state.
3158          */
3159         status = ata_busy_wait(ap, ATA_BUSY, 5);
3160         if (status & ATA_BUSY) {
3161                 msleep(2);
3162                 status = ata_busy_wait(ap, ATA_BUSY, 10);
3163                 if (status & ATA_BUSY) {
3164                         ap->hsm_task_state = HSM_ST_POLL;
3165                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3166                         return;
3167                 }
3168         }
3169
3170         qc = ata_qc_from_tag(ap, ap->active_tag);
3171         assert(qc != NULL);
3172
3173         if (is_atapi_taskfile(&qc->tf)) {
3174                 /* no more data to transfer or unsupported ATAPI command */
3175                 if ((status & ATA_DRQ) == 0) {
3176                         ap->hsm_task_state = HSM_ST_LAST;
3177                         return;
3178                 }
3179
3180                 atapi_pio_bytes(qc);
3181         } else {
3182                 /* handle BSY=0, DRQ=0 as error */
3183                 if ((status & ATA_DRQ) == 0) {
3184                         ap->hsm_task_state = HSM_ST_ERR;
3185                         return;
3186                 }
3187
3188                 ata_pio_sector(qc);
3189         }
3190 }
3191
3192 static void ata_pio_error(struct ata_port *ap)
3193 {
3194         struct ata_queued_cmd *qc;
3195
3196         printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3197
3198         qc = ata_qc_from_tag(ap, ap->active_tag);
3199         assert(qc != NULL);
3200
3201         ap->hsm_task_state = HSM_ST_IDLE;
3202
3203         ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3204 }
3205
3206 static void ata_pio_task(void *_data)
3207 {
3208         struct ata_port *ap = _data;
3209         unsigned long timeout;
3210         int qc_completed;
3211
3212 fsm_start:
3213         timeout = 0;
3214         qc_completed = 0;
3215
3216         switch (ap->hsm_task_state) {
3217         case HSM_ST_IDLE:
3218                 return;
3219
3220         case HSM_ST:
3221                 ata_pio_block(ap);
3222                 break;
3223
3224         case HSM_ST_LAST:
3225                 qc_completed = ata_pio_complete(ap);
3226                 break;
3227
3228         case HSM_ST_POLL:
3229         case HSM_ST_LAST_POLL:
3230                 timeout = ata_pio_poll(ap);
3231                 break;
3232
3233         case HSM_ST_TMOUT:
3234         case HSM_ST_ERR:
3235                 ata_pio_error(ap);
3236                 return;
3237         }
3238
3239         if (timeout)
3240                 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3241         else if (!qc_completed)
3242                 goto fsm_start;
3243 }
3244
3245 /**
3246  *      ata_qc_timeout - Handle timeout of queued command
3247  *      @qc: Command that timed out
3248  *
3249  *      Some part of the kernel (currently, only the SCSI layer)
3250  *      has noticed that the active command on port @ap has not
3251  *      completed after a specified length of time.  Handle this
3252  *      condition by disabling DMA (if necessary) and completing
3253  *      transactions, with error if necessary.
3254  *
3255  *      This also handles the case of the "lost interrupt", where
3256  *      for some reason (possibly hardware bug, possibly driver bug)
3257  *      an interrupt was not delivered to the driver, even though the
3258  *      transaction completed successfully.
3259  *
3260  *      LOCKING:
3261  *      Inherited from SCSI layer (none, can sleep)
3262  */
3263
3264 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3265 {
3266         struct ata_port *ap = qc->ap;
3267         struct ata_host_set *host_set = ap->host_set;
3268         struct ata_device *dev = qc->dev;
3269         u8 host_stat = 0, drv_stat;
3270         unsigned long flags;
3271
3272         DPRINTK("ENTER\n");
3273
3274         /* FIXME: doesn't this conflict with timeout handling? */
3275         if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3276                 struct scsi_cmnd *cmd = qc->scsicmd;
3277
3278                 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3279
3280                         /* finish completing original command */
3281                         spin_lock_irqsave(&host_set->lock, flags);
3282                         __ata_qc_complete(qc);
3283                         spin_unlock_irqrestore(&host_set->lock, flags);
3284
3285                         atapi_request_sense(ap, dev, cmd);
3286
3287                         cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3288                         scsi_finish_command(cmd);
3289
3290                         goto out;
3291                 }
3292         }
3293
3294         spin_lock_irqsave(&host_set->lock, flags);
3295
3296         /* hack alert!  We cannot use the supplied completion
3297          * function from inside the ->eh_strategy_handler() thread.
3298          * libata is the only user of ->eh_strategy_handler() in
3299          * any kernel, so the default scsi_done() assumes it is
3300          * not being called from the SCSI EH.
3301          */
3302         qc->scsidone = scsi_finish_command;
3303
3304         switch (qc->tf.protocol) {
3305
3306         case ATA_PROT_DMA:
3307         case ATA_PROT_ATAPI_DMA:
3308                 host_stat = ap->ops->bmdma_status(ap);
3309
3310                 /* before we do anything else, clear DMA-Start bit */
3311                 ap->ops->bmdma_stop(qc);
3312
3313                 /* fall through */
3314
3315         default:
3316                 ata_altstatus(ap);
3317                 drv_stat = ata_chk_status(ap);
3318
3319                 /* ack bmdma irq events */
3320                 ap->ops->irq_clear(ap);
3321
3322                 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3323                        ap->id, qc->tf.command, drv_stat, host_stat);
3324
3325                 /* complete taskfile transaction */
3326                 ata_qc_complete(qc, ac_err_mask(drv_stat));
3327                 break;
3328         }
3329
3330         spin_unlock_irqrestore(&host_set->lock, flags);
3331
3332 out:
3333         DPRINTK("EXIT\n");
3334 }
3335
3336 /**
3337  *      ata_eng_timeout - Handle timeout of queued command
3338  *      @ap: Port on which timed-out command is active
3339  *
3340  *      Some part of the kernel (currently, only the SCSI layer)
3341  *      has noticed that the active command on port @ap has not
3342  *      completed after a specified length of time.  Handle this
3343  *      condition by disabling DMA (if necessary) and completing
3344  *      transactions, with error if necessary.
3345  *
3346  *      This also handles the case of the "lost interrupt", where
3347  *      for some reason (possibly hardware bug, possibly driver bug)
3348  *      an interrupt was not delivered to the driver, even though the
3349  *      transaction completed successfully.
3350  *
3351  *      LOCKING:
3352  *      Inherited from SCSI layer (none, can sleep)
3353  */
3354
3355 void ata_eng_timeout(struct ata_port *ap)
3356 {
3357         struct ata_queued_cmd *qc;
3358
3359         DPRINTK("ENTER\n");
3360
3361         qc = ata_qc_from_tag(ap, ap->active_tag);
3362         if (qc)
3363                 ata_qc_timeout(qc);
3364         else {
3365                 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3366                        ap->id);
3367                 goto out;
3368         }
3369
3370 out:
3371         DPRINTK("EXIT\n");
3372 }
3373
3374 /**
3375  *      ata_qc_new - Request an available ATA command, for queueing
3376  *      @ap: Port associated with device @dev
3377  *      @dev: Device from whom we request an available command structure
3378  *
3379  *      LOCKING:
3380  *      None.
3381  */
3382
3383 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3384 {
3385         struct ata_queued_cmd *qc = NULL;
3386         unsigned int i;
3387
3388         for (i = 0; i < ATA_MAX_QUEUE; i++)
3389                 if (!test_and_set_bit(i, &ap->qactive)) {
3390                         qc = ata_qc_from_tag(ap, i);
3391                         break;
3392                 }
3393
3394         if (qc)
3395                 qc->tag = i;
3396
3397         return qc;
3398 }
3399
3400 /**
3401  *      ata_qc_new_init - Request an available ATA command, and initialize it
3402  *      @ap: Port associated with device @dev
3403  *      @dev: Device from whom we request an available command structure
3404  *
3405  *      LOCKING:
3406  *      None.
3407  */
3408
3409 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3410                                       struct ata_device *dev)
3411 {
3412         struct ata_queued_cmd *qc;
3413
3414         qc = ata_qc_new(ap);
3415         if (qc) {