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