Merge branch 'master'
[pandora-kernel.git] / drivers / scsi / libata-core.c
1 /*
2  *  libata-core.c - helper library for ATA
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Please ALWAYS copy linux-ide@vger.kernel.org
6  *                  on emails.
7  *
8  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9  *  Copyright 2003-2004 Jeff Garzik
10  *
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; see the file COPYING.  If not, write to
24  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  *
27  *  libata documentation is available via 'make {ps|pdf}docs',
28  *  as Documentation/DocBook/libata.*
29  *
30  *  Hardware documentation available from http://www.t13.org/ and
31  *  http://www.sata-io.org/
32  *
33  */
34
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/mm.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <scsi/scsi.h>
53 #include "scsi.h"
54 #include "scsi_priv.h"
55 #include <scsi/scsi_host.h>
56 #include <linux/libata.h>
57 #include <asm/io.h>
58 #include <asm/semaphore.h>
59 #include <asm/byteorder.h>
60
61 #include "libata.h"
62
63 static unsigned int ata_busy_sleep (struct ata_port *ap,
64                                     unsigned long tmout_pat,
65                                     unsigned long tmout);
66 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
67 static void ata_set_mode(struct ata_port *ap);
68 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
69 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
70 static int fgb(u32 bitmap);
71 static int ata_choose_xfer_mode(struct ata_port *ap,
72                                 u8 *xfer_mode_out,
73                                 unsigned int *xfer_shift_out);
74 static void __ata_qc_complete(struct ata_queued_cmd *qc);
75
76 static unsigned int ata_unique_id = 1;
77 static struct workqueue_struct *ata_wq;
78
79 int atapi_enabled = 0;
80 module_param(atapi_enabled, int, 0444);
81 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION);
87
88 /**
89  *      ata_tf_load - send taskfile registers to host controller
90  *      @ap: Port to which output is sent
91  *      @tf: ATA taskfile register set
92  *
93  *      Outputs ATA taskfile to standard ATA host controller.
94  *
95  *      LOCKING:
96  *      Inherited from caller.
97  */
98
99 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
100 {
101         struct ata_ioports *ioaddr = &ap->ioaddr;
102         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
103
104         if (tf->ctl != ap->last_ctl) {
105                 outb(tf->ctl, ioaddr->ctl_addr);
106                 ap->last_ctl = tf->ctl;
107                 ata_wait_idle(ap);
108         }
109
110         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
111                 outb(tf->hob_feature, ioaddr->feature_addr);
112                 outb(tf->hob_nsect, ioaddr->nsect_addr);
113                 outb(tf->hob_lbal, ioaddr->lbal_addr);
114                 outb(tf->hob_lbam, ioaddr->lbam_addr);
115                 outb(tf->hob_lbah, ioaddr->lbah_addr);
116                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
117                         tf->hob_feature,
118                         tf->hob_nsect,
119                         tf->hob_lbal,
120                         tf->hob_lbam,
121                         tf->hob_lbah);
122         }
123
124         if (is_addr) {
125                 outb(tf->feature, ioaddr->feature_addr);
126                 outb(tf->nsect, ioaddr->nsect_addr);
127                 outb(tf->lbal, ioaddr->lbal_addr);
128                 outb(tf->lbam, ioaddr->lbam_addr);
129                 outb(tf->lbah, ioaddr->lbah_addr);
130                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
131                         tf->feature,
132                         tf->nsect,
133                         tf->lbal,
134                         tf->lbam,
135                         tf->lbah);
136         }
137
138         if (tf->flags & ATA_TFLAG_DEVICE) {
139                 outb(tf->device, ioaddr->device_addr);
140                 VPRINTK("device 0x%X\n", tf->device);
141         }
142
143         ata_wait_idle(ap);
144 }
145
146 /**
147  *      ata_tf_load_mmio - send taskfile registers to host controller
148  *      @ap: Port to which output is sent
149  *      @tf: ATA taskfile register set
150  *
151  *      Outputs ATA taskfile to standard ATA host controller using MMIO.
152  *
153  *      LOCKING:
154  *      Inherited from caller.
155  */
156
157 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
158 {
159         struct ata_ioports *ioaddr = &ap->ioaddr;
160         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
161
162         if (tf->ctl != ap->last_ctl) {
163                 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
164                 ap->last_ctl = tf->ctl;
165                 ata_wait_idle(ap);
166         }
167
168         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
169                 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
170                 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
171                 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
172                 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
173                 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
174                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
175                         tf->hob_feature,
176                         tf->hob_nsect,
177                         tf->hob_lbal,
178                         tf->hob_lbam,
179                         tf->hob_lbah);
180         }
181
182         if (is_addr) {
183                 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
184                 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
185                 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
186                 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
187                 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
188                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
189                         tf->feature,
190                         tf->nsect,
191                         tf->lbal,
192                         tf->lbam,
193                         tf->lbah);
194         }
195
196         if (tf->flags & ATA_TFLAG_DEVICE) {
197                 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
198                 VPRINTK("device 0x%X\n", tf->device);
199         }
200
201         ata_wait_idle(ap);
202 }
203
204
205 /**
206  *      ata_tf_load - send taskfile registers to host controller
207  *      @ap: Port to which output is sent
208  *      @tf: ATA taskfile register set
209  *
210  *      Outputs ATA taskfile to standard ATA host controller using MMIO
211  *      or PIO as indicated by the ATA_FLAG_MMIO flag.
212  *      Writes the control, feature, nsect, lbal, lbam, and lbah registers.
213  *      Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
214  *      hob_lbal, hob_lbam, and hob_lbah.
215  *
216  *      This function waits for idle (!BUSY and !DRQ) after writing
217  *      registers.  If the control register has a new value, this
218  *      function also waits for idle after writing control and before
219  *      writing the remaining registers.
220  *
221  *      May be used as the tf_load() entry in ata_port_operations.
222  *
223  *      LOCKING:
224  *      Inherited from caller.
225  */
226 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
227 {
228         if (ap->flags & ATA_FLAG_MMIO)
229                 ata_tf_load_mmio(ap, tf);
230         else
231                 ata_tf_load_pio(ap, tf);
232 }
233
234 /**
235  *      ata_exec_command_pio - issue ATA command to host controller
236  *      @ap: port to which command is being issued
237  *      @tf: ATA taskfile register set
238  *
239  *      Issues PIO write to ATA command register, with proper
240  *      synchronization with interrupt handler / other threads.
241  *
242  *      LOCKING:
243  *      spin_lock_irqsave(host_set lock)
244  */
245
246 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
247 {
248         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
249
250         outb(tf->command, ap->ioaddr.command_addr);
251         ata_pause(ap);
252 }
253
254
255 /**
256  *      ata_exec_command_mmio - issue ATA command to host controller
257  *      @ap: port to which command is being issued
258  *      @tf: ATA taskfile register set
259  *
260  *      Issues MMIO write to ATA command register, with proper
261  *      synchronization with interrupt handler / other threads.
262  *
263  *      LOCKING:
264  *      spin_lock_irqsave(host_set lock)
265  */
266
267 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
268 {
269         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
270
271         writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
272         ata_pause(ap);
273 }
274
275
276 /**
277  *      ata_exec_command - issue ATA command to host controller
278  *      @ap: port to which command is being issued
279  *      @tf: ATA taskfile register set
280  *
281  *      Issues PIO/MMIO write to ATA command register, with proper
282  *      synchronization with interrupt handler / other threads.
283  *
284  *      LOCKING:
285  *      spin_lock_irqsave(host_set lock)
286  */
287 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
288 {
289         if (ap->flags & ATA_FLAG_MMIO)
290                 ata_exec_command_mmio(ap, tf);
291         else
292                 ata_exec_command_pio(ap, tf);
293 }
294
295 /**
296  *      ata_exec - issue ATA command to host controller
297  *      @ap: port to which command is being issued
298  *      @tf: ATA taskfile register set
299  *
300  *      Issues PIO/MMIO write to ATA command register, with proper
301  *      synchronization with interrupt handler / other threads.
302  *
303  *      LOCKING:
304  *      Obtains host_set lock.
305  */
306
307 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
308 {
309         unsigned long flags;
310
311         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
312         spin_lock_irqsave(&ap->host_set->lock, flags);
313         ap->ops->exec_command(ap, tf);
314         spin_unlock_irqrestore(&ap->host_set->lock, flags);
315 }
316
317 /**
318  *      ata_tf_to_host - issue ATA taskfile to host controller
319  *      @ap: port to which command is being issued
320  *      @tf: ATA taskfile register set
321  *
322  *      Issues ATA taskfile register set to ATA host controller,
323  *      with proper synchronization with interrupt handler and
324  *      other threads.
325  *
326  *      LOCKING:
327  *      Obtains host_set lock.
328  */
329
330 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
331 {
332         ap->ops->tf_load(ap, tf);
333
334         ata_exec(ap, tf);
335 }
336
337 /**
338  *      ata_tf_to_host_nolock - issue ATA taskfile to host controller
339  *      @ap: port to which command is being issued
340  *      @tf: ATA taskfile register set
341  *
342  *      Issues ATA taskfile register set to ATA host controller,
343  *      with proper synchronization with interrupt handler and
344  *      other threads.
345  *
346  *      LOCKING:
347  *      spin_lock_irqsave(host_set lock)
348  */
349
350 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
351 {
352         ap->ops->tf_load(ap, tf);
353         ap->ops->exec_command(ap, tf);
354 }
355
356 /**
357  *      ata_tf_read_pio - input device's ATA taskfile shadow registers
358  *      @ap: Port from which input is read
359  *      @tf: ATA taskfile register set for storing input
360  *
361  *      Reads ATA taskfile registers for currently-selected device
362  *      into @tf.
363  *
364  *      LOCKING:
365  *      Inherited from caller.
366  */
367
368 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
369 {
370         struct ata_ioports *ioaddr = &ap->ioaddr;
371
372         tf->nsect = inb(ioaddr->nsect_addr);
373         tf->lbal = inb(ioaddr->lbal_addr);
374         tf->lbam = inb(ioaddr->lbam_addr);
375         tf->lbah = inb(ioaddr->lbah_addr);
376         tf->device = inb(ioaddr->device_addr);
377
378         if (tf->flags & ATA_TFLAG_LBA48) {
379                 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
380                 tf->hob_feature = inb(ioaddr->error_addr);
381                 tf->hob_nsect = inb(ioaddr->nsect_addr);
382                 tf->hob_lbal = inb(ioaddr->lbal_addr);
383                 tf->hob_lbam = inb(ioaddr->lbam_addr);
384                 tf->hob_lbah = inb(ioaddr->lbah_addr);
385         }
386 }
387
388 /**
389  *      ata_tf_read_mmio - input device's ATA taskfile shadow registers
390  *      @ap: Port from which input is read
391  *      @tf: ATA taskfile register set for storing input
392  *
393  *      Reads ATA taskfile registers for currently-selected device
394  *      into @tf via MMIO.
395  *
396  *      LOCKING:
397  *      Inherited from caller.
398  */
399
400 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
401 {
402         struct ata_ioports *ioaddr = &ap->ioaddr;
403
404         tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
405         tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
406         tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
407         tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
408         tf->device = readb((void __iomem *)ioaddr->device_addr);
409
410         if (tf->flags & ATA_TFLAG_LBA48) {
411                 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
412                 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
413                 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
414                 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
415                 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
416                 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
417         }
418 }
419
420
421 /**
422  *      ata_tf_read - input device's ATA taskfile shadow registers
423  *      @ap: Port from which input is read
424  *      @tf: ATA taskfile register set for storing input
425  *
426  *      Reads ATA taskfile registers for currently-selected device
427  *      into @tf.
428  *
429  *      Reads nsect, lbal, lbam, lbah, and device.  If ATA_TFLAG_LBA48
430  *      is set, also reads the hob registers.
431  *
432  *      May be used as the tf_read() entry in ata_port_operations.
433  *
434  *      LOCKING:
435  *      Inherited from caller.
436  */
437 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
438 {
439         if (ap->flags & ATA_FLAG_MMIO)
440                 ata_tf_read_mmio(ap, tf);
441         else
442                 ata_tf_read_pio(ap, tf);
443 }
444
445 /**
446  *      ata_check_status_pio - Read device status reg & clear interrupt
447  *      @ap: port where the device is
448  *
449  *      Reads ATA taskfile status register for currently-selected device
450  *      and return its value. This also clears pending interrupts
451  *      from this device
452  *
453  *      LOCKING:
454  *      Inherited from caller.
455  */
456 static u8 ata_check_status_pio(struct ata_port *ap)
457 {
458         return inb(ap->ioaddr.status_addr);
459 }
460
461 /**
462  *      ata_check_status_mmio - Read device status reg & clear interrupt
463  *      @ap: port where the device is
464  *
465  *      Reads ATA taskfile status register for currently-selected device
466  *      via MMIO and return its value. This also clears pending interrupts
467  *      from this device
468  *
469  *      LOCKING:
470  *      Inherited from caller.
471  */
472 static u8 ata_check_status_mmio(struct ata_port *ap)
473 {
474         return readb((void __iomem *) ap->ioaddr.status_addr);
475 }
476
477
478 /**
479  *      ata_check_status - Read device status reg & clear interrupt
480  *      @ap: port where the device is
481  *
482  *      Reads ATA taskfile status register for currently-selected device
483  *      and return its value. This also clears pending interrupts
484  *      from this device
485  *
486  *      May be used as the check_status() entry in ata_port_operations.
487  *
488  *      LOCKING:
489  *      Inherited from caller.
490  */
491 u8 ata_check_status(struct ata_port *ap)
492 {
493         if (ap->flags & ATA_FLAG_MMIO)
494                 return ata_check_status_mmio(ap);
495         return ata_check_status_pio(ap);
496 }
497
498
499 /**
500  *      ata_altstatus - Read device alternate status reg
501  *      @ap: port where the device is
502  *
503  *      Reads ATA taskfile alternate status register for
504  *      currently-selected device and return its value.
505  *
506  *      Note: may NOT be used as the check_altstatus() entry in
507  *      ata_port_operations.
508  *
509  *      LOCKING:
510  *      Inherited from caller.
511  */
512 u8 ata_altstatus(struct ata_port *ap)
513 {
514         if (ap->ops->check_altstatus)
515                 return ap->ops->check_altstatus(ap);
516
517         if (ap->flags & ATA_FLAG_MMIO)
518                 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
519         return inb(ap->ioaddr.altstatus_addr);
520 }
521
522
523 /**
524  *      ata_chk_err - Read device error reg
525  *      @ap: port where the device is
526  *
527  *      Reads ATA taskfile error register for
528  *      currently-selected device and return its value.
529  *
530  *      Note: may NOT be used as the check_err() entry in
531  *      ata_port_operations.
532  *
533  *      LOCKING:
534  *      Inherited from caller.
535  */
536 u8 ata_chk_err(struct ata_port *ap)
537 {
538         if (ap->ops->check_err)
539                 return ap->ops->check_err(ap);
540
541         if (ap->flags & ATA_FLAG_MMIO) {
542                 return readb((void __iomem *) ap->ioaddr.error_addr);
543         }
544         return inb(ap->ioaddr.error_addr);
545 }
546
547 /**
548  *      ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
549  *      @tf: Taskfile to convert
550  *      @fis: Buffer into which data will output
551  *      @pmp: Port multiplier port
552  *
553  *      Converts a standard ATA taskfile to a Serial ATA
554  *      FIS structure (Register - Host to Device).
555  *
556  *      LOCKING:
557  *      Inherited from caller.
558  */
559
560 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
561 {
562         fis[0] = 0x27;  /* Register - Host to Device FIS */
563         fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
564                                             bit 7 indicates Command FIS */
565         fis[2] = tf->command;
566         fis[3] = tf->feature;
567
568         fis[4] = tf->lbal;
569         fis[5] = tf->lbam;
570         fis[6] = tf->lbah;
571         fis[7] = tf->device;
572
573         fis[8] = tf->hob_lbal;
574         fis[9] = tf->hob_lbam;
575         fis[10] = tf->hob_lbah;
576         fis[11] = tf->hob_feature;
577
578         fis[12] = tf->nsect;
579         fis[13] = tf->hob_nsect;
580         fis[14] = 0;
581         fis[15] = tf->ctl;
582
583         fis[16] = 0;
584         fis[17] = 0;
585         fis[18] = 0;
586         fis[19] = 0;
587 }
588
589 /**
590  *      ata_tf_from_fis - Convert SATA FIS to ATA taskfile
591  *      @fis: Buffer from which data will be input
592  *      @tf: Taskfile to output
593  *
594  *      Converts a standard ATA taskfile to a Serial ATA
595  *      FIS structure (Register - Host to Device).
596  *
597  *      LOCKING:
598  *      Inherited from caller.
599  */
600
601 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
602 {
603         tf->command     = fis[2];       /* status */
604         tf->feature     = fis[3];       /* error */
605
606         tf->lbal        = fis[4];
607         tf->lbam        = fis[5];
608         tf->lbah        = fis[6];
609         tf->device      = fis[7];
610
611         tf->hob_lbal    = fis[8];
612         tf->hob_lbam    = fis[9];
613         tf->hob_lbah    = fis[10];
614
615         tf->nsect       = fis[12];
616         tf->hob_nsect   = fis[13];
617 }
618
619 /**
620  *      ata_prot_to_cmd - determine which read/write opcodes to use
621  *      @protocol: ATA_PROT_xxx taskfile protocol
622  *      @lba48: true is lba48 is present
623  *
624  *      Given necessary input, determine which read/write commands
625  *      to use to transfer data.
626  *
627  *      LOCKING:
628  *      None.
629  */
630 static int ata_prot_to_cmd(int protocol, int lba48)
631 {
632         int rcmd = 0, wcmd = 0;
633
634         switch (protocol) {
635         case ATA_PROT_PIO:
636                 if (lba48) {
637                         rcmd = ATA_CMD_PIO_READ_EXT;
638                         wcmd = ATA_CMD_PIO_WRITE_EXT;
639                 } else {
640                         rcmd = ATA_CMD_PIO_READ;
641                         wcmd = ATA_CMD_PIO_WRITE;
642                 }
643                 break;
644
645         case ATA_PROT_DMA:
646                 if (lba48) {
647                         rcmd = ATA_CMD_READ_EXT;
648                         wcmd = ATA_CMD_WRITE_EXT;
649                 } else {
650                         rcmd = ATA_CMD_READ;
651                         wcmd = ATA_CMD_WRITE;
652                 }
653                 break;
654
655         default:
656                 return -1;
657         }
658
659         return rcmd | (wcmd << 8);
660 }
661
662 /**
663  *      ata_dev_set_protocol - set taskfile protocol and r/w commands
664  *      @dev: device to examine and configure
665  *
666  *      Examine the device configuration, after we have
667  *      read the identify-device page and configured the
668  *      data transfer mode.  Set internal state related to
669  *      the ATA taskfile protocol (pio, pio mult, dma, etc.)
670  *      and calculate the proper read/write commands to use.
671  *
672  *      LOCKING:
673  *      caller.
674  */
675 static void ata_dev_set_protocol(struct ata_device *dev)
676 {
677         int pio = (dev->flags & ATA_DFLAG_PIO);
678         int lba48 = (dev->flags & ATA_DFLAG_LBA48);
679         int proto, cmd;
680
681         if (pio)
682                 proto = dev->xfer_protocol = ATA_PROT_PIO;
683         else
684                 proto = dev->xfer_protocol = ATA_PROT_DMA;
685
686         cmd = ata_prot_to_cmd(proto, lba48);
687         if (cmd < 0)
688                 BUG();
689
690         dev->read_cmd = cmd & 0xff;
691         dev->write_cmd = (cmd >> 8) & 0xff;
692 }
693
694 static const char * xfer_mode_str[] = {
695         "UDMA/16",
696         "UDMA/25",
697         "UDMA/33",
698         "UDMA/44",
699         "UDMA/66",
700         "UDMA/100",
701         "UDMA/133",
702         "UDMA7",
703         "MWDMA0",
704         "MWDMA1",
705         "MWDMA2",
706         "PIO0",
707         "PIO1",
708         "PIO2",
709         "PIO3",
710         "PIO4",
711 };
712
713 /**
714  *      ata_udma_string - convert UDMA bit offset to string
715  *      @mask: mask of bits supported; only highest bit counts.
716  *
717  *      Determine string which represents the highest speed
718  *      (highest bit in @udma_mask).
719  *
720  *      LOCKING:
721  *      None.
722  *
723  *      RETURNS:
724  *      Constant C string representing highest speed listed in
725  *      @udma_mask, or the constant C string "<n/a>".
726  */
727
728 static const char *ata_mode_string(unsigned int mask)
729 {
730         int i;
731
732         for (i = 7; i >= 0; i--)
733                 if (mask & (1 << i))
734                         goto out;
735         for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
736                 if (mask & (1 << i))
737                         goto out;
738         for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
739                 if (mask & (1 << i))
740                         goto out;
741
742         return "<n/a>";
743
744 out:
745         return xfer_mode_str[i];
746 }
747
748 /**
749  *      ata_pio_devchk - PATA device presence detection
750  *      @ap: ATA channel to examine
751  *      @device: Device to examine (starting at zero)
752  *
753  *      This technique was originally described in
754  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
755  *      later found its way into the ATA/ATAPI spec.
756  *
757  *      Write a pattern to the ATA shadow registers,
758  *      and if a device is present, it will respond by
759  *      correctly storing and echoing back the
760  *      ATA shadow register contents.
761  *
762  *      LOCKING:
763  *      caller.
764  */
765
766 static unsigned int ata_pio_devchk(struct ata_port *ap,
767                                    unsigned int device)
768 {
769         struct ata_ioports *ioaddr = &ap->ioaddr;
770         u8 nsect, lbal;
771
772         ap->ops->dev_select(ap, device);
773
774         outb(0x55, ioaddr->nsect_addr);
775         outb(0xaa, ioaddr->lbal_addr);
776
777         outb(0xaa, ioaddr->nsect_addr);
778         outb(0x55, ioaddr->lbal_addr);
779
780         outb(0x55, ioaddr->nsect_addr);
781         outb(0xaa, ioaddr->lbal_addr);
782
783         nsect = inb(ioaddr->nsect_addr);
784         lbal = inb(ioaddr->lbal_addr);
785
786         if ((nsect == 0x55) && (lbal == 0xaa))
787                 return 1;       /* we found a device */
788
789         return 0;               /* nothing found */
790 }
791
792 /**
793  *      ata_mmio_devchk - PATA device presence detection
794  *      @ap: ATA channel to examine
795  *      @device: Device to examine (starting at zero)
796  *
797  *      This technique was originally described in
798  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
799  *      later found its way into the ATA/ATAPI spec.
800  *
801  *      Write a pattern to the ATA shadow registers,
802  *      and if a device is present, it will respond by
803  *      correctly storing and echoing back the
804  *      ATA shadow register contents.
805  *
806  *      LOCKING:
807  *      caller.
808  */
809
810 static unsigned int ata_mmio_devchk(struct ata_port *ap,
811                                     unsigned int device)
812 {
813         struct ata_ioports *ioaddr = &ap->ioaddr;
814         u8 nsect, lbal;
815
816         ap->ops->dev_select(ap, device);
817
818         writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
819         writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
820
821         writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
822         writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
823
824         writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
825         writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
826
827         nsect = readb((void __iomem *) ioaddr->nsect_addr);
828         lbal = readb((void __iomem *) ioaddr->lbal_addr);
829
830         if ((nsect == 0x55) && (lbal == 0xaa))
831                 return 1;       /* we found a device */
832
833         return 0;               /* nothing found */
834 }
835
836 /**
837  *      ata_devchk - PATA device presence detection
838  *      @ap: ATA channel to examine
839  *      @device: Device to examine (starting at zero)
840  *
841  *      Dispatch ATA device presence detection, depending
842  *      on whether we are using PIO or MMIO to talk to the
843  *      ATA shadow registers.
844  *
845  *      LOCKING:
846  *      caller.
847  */
848
849 static unsigned int ata_devchk(struct ata_port *ap,
850                                     unsigned int device)
851 {
852         if (ap->flags & ATA_FLAG_MMIO)
853                 return ata_mmio_devchk(ap, device);
854         return ata_pio_devchk(ap, device);
855 }
856
857 /**
858  *      ata_dev_classify - determine device type based on ATA-spec signature
859  *      @tf: ATA taskfile register set for device to be identified
860  *
861  *      Determine from taskfile register contents whether a device is
862  *      ATA or ATAPI, as per "Signature and persistence" section
863  *      of ATA/PI spec (volume 1, sect 5.14).
864  *
865  *      LOCKING:
866  *      None.
867  *
868  *      RETURNS:
869  *      Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
870  *      the event of failure.
871  */
872
873 unsigned int ata_dev_classify(struct ata_taskfile *tf)
874 {
875         /* Apple's open source Darwin code hints that some devices only
876          * put a proper signature into the LBA mid/high registers,
877          * So, we only check those.  It's sufficient for uniqueness.
878          */
879
880         if (((tf->lbam == 0) && (tf->lbah == 0)) ||
881             ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
882                 DPRINTK("found ATA device by sig\n");
883                 return ATA_DEV_ATA;
884         }
885
886         if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
887             ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
888                 DPRINTK("found ATAPI device by sig\n");
889                 return ATA_DEV_ATAPI;
890         }
891
892         DPRINTK("unknown device\n");
893         return ATA_DEV_UNKNOWN;
894 }
895
896 /**
897  *      ata_dev_try_classify - Parse returned ATA device signature
898  *      @ap: ATA channel to examine
899  *      @device: Device to examine (starting at zero)
900  *
901  *      After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
902  *      an ATA/ATAPI-defined set of values is placed in the ATA
903  *      shadow registers, indicating the results of device detection
904  *      and diagnostics.
905  *
906  *      Select the ATA device, and read the values from the ATA shadow
907  *      registers.  Then parse according to the Error register value,
908  *      and the spec-defined values examined by ata_dev_classify().
909  *
910  *      LOCKING:
911  *      caller.
912  */
913
914 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
915 {
916         struct ata_device *dev = &ap->device[device];
917         struct ata_taskfile tf;
918         unsigned int class;
919         u8 err;
920
921         ap->ops->dev_select(ap, device);
922
923         memset(&tf, 0, sizeof(tf));
924
925         err = ata_chk_err(ap);
926         ap->ops->tf_read(ap, &tf);
927
928         dev->class = ATA_DEV_NONE;
929
930         /* see if device passed diags */
931         if (err == 1)
932                 /* do nothing */ ;
933         else if ((device == 0) && (err == 0x81))
934                 /* do nothing */ ;
935         else
936                 return err;
937
938         /* determine if device if ATA or ATAPI */
939         class = ata_dev_classify(&tf);
940         if (class == ATA_DEV_UNKNOWN)
941                 return err;
942         if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
943                 return err;
944
945         dev->class = class;
946
947         return err;
948 }
949
950 /**
951  *      ata_dev_id_string - Convert IDENTIFY DEVICE page into string
952  *      @id: IDENTIFY DEVICE results we will examine
953  *      @s: string into which data is output
954  *      @ofs: offset into identify device page
955  *      @len: length of string to return. must be an even number.
956  *
957  *      The strings in the IDENTIFY DEVICE page are broken up into
958  *      16-bit chunks.  Run through the string, and output each
959  *      8-bit chunk linearly, regardless of platform.
960  *
961  *      LOCKING:
962  *      caller.
963  */
964
965 void ata_dev_id_string(u16 *id, unsigned char *s,
966                        unsigned int ofs, unsigned int len)
967 {
968         unsigned int c;
969
970         while (len > 0) {
971                 c = id[ofs] >> 8;
972                 *s = c;
973                 s++;
974
975                 c = id[ofs] & 0xff;
976                 *s = c;
977                 s++;
978
979                 ofs++;
980                 len -= 2;
981         }
982 }
983
984
985 /**
986  *      ata_noop_dev_select - Select device 0/1 on ATA bus
987  *      @ap: ATA channel to manipulate
988  *      @device: ATA device (numbered from zero) to select
989  *
990  *      This function performs no actual function.
991  *
992  *      May be used as the dev_select() entry in ata_port_operations.
993  *
994  *      LOCKING:
995  *      caller.
996  */
997 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
998 {
999 }
1000
1001
1002 /**
1003  *      ata_std_dev_select - Select device 0/1 on ATA bus
1004  *      @ap: ATA channel to manipulate
1005  *      @device: ATA device (numbered from zero) to select
1006  *
1007  *      Use the method defined in the ATA specification to
1008  *      make either device 0, or device 1, active on the
1009  *      ATA channel.  Works with both PIO and MMIO.
1010  *
1011  *      May be used as the dev_select() entry in ata_port_operations.
1012  *
1013  *      LOCKING:
1014  *      caller.
1015  */
1016
1017 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1018 {
1019         u8 tmp;
1020
1021         if (device == 0)
1022                 tmp = ATA_DEVICE_OBS;
1023         else
1024                 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1025
1026         if (ap->flags & ATA_FLAG_MMIO) {
1027                 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1028         } else {
1029                 outb(tmp, ap->ioaddr.device_addr);
1030         }
1031         ata_pause(ap);          /* needed; also flushes, for mmio */
1032 }
1033
1034 /**
1035  *      ata_dev_select - Select device 0/1 on ATA bus
1036  *      @ap: ATA channel to manipulate
1037  *      @device: ATA device (numbered from zero) to select
1038  *      @wait: non-zero to wait for Status register BSY bit to clear
1039  *      @can_sleep: non-zero if context allows sleeping
1040  *
1041  *      Use the method defined in the ATA specification to
1042  *      make either device 0, or device 1, active on the
1043  *      ATA channel.
1044  *
1045  *      This is a high-level version of ata_std_dev_select(),
1046  *      which additionally provides the services of inserting
1047  *      the proper pauses and status polling, where needed.
1048  *
1049  *      LOCKING:
1050  *      caller.
1051  */
1052
1053 void ata_dev_select(struct ata_port *ap, unsigned int device,
1054                            unsigned int wait, unsigned int can_sleep)
1055 {
1056         VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1057                 ap->id, device, wait);
1058
1059         if (wait)
1060                 ata_wait_idle(ap);
1061
1062         ap->ops->dev_select(ap, device);
1063
1064         if (wait) {
1065                 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1066                         msleep(150);
1067                 ata_wait_idle(ap);
1068         }
1069 }
1070
1071 /**
1072  *      ata_dump_id - IDENTIFY DEVICE info debugging output
1073  *      @dev: Device whose IDENTIFY DEVICE page we will dump
1074  *
1075  *      Dump selected 16-bit words from a detected device's
1076  *      IDENTIFY PAGE page.
1077  *
1078  *      LOCKING:
1079  *      caller.
1080  */
1081
1082 static inline void ata_dump_id(struct ata_device *dev)
1083 {
1084         DPRINTK("49==0x%04x  "
1085                 "53==0x%04x  "
1086                 "63==0x%04x  "
1087                 "64==0x%04x  "
1088                 "75==0x%04x  \n",
1089                 dev->id[49],
1090                 dev->id[53],
1091                 dev->id[63],
1092                 dev->id[64],
1093                 dev->id[75]);
1094         DPRINTK("80==0x%04x  "
1095                 "81==0x%04x  "
1096                 "82==0x%04x  "
1097                 "83==0x%04x  "
1098                 "84==0x%04x  \n",
1099                 dev->id[80],
1100                 dev->id[81],
1101                 dev->id[82],
1102                 dev->id[83],
1103                 dev->id[84]);
1104         DPRINTK("88==0x%04x  "
1105                 "93==0x%04x\n",
1106                 dev->id[88],
1107                 dev->id[93]);
1108 }
1109
1110 /**
1111  *      ata_dev_identify - obtain IDENTIFY x DEVICE page
1112  *      @ap: port on which device we wish to probe resides
1113  *      @device: device bus address, starting at zero
1114  *
1115  *      Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1116  *      command, and read back the 512-byte device information page.
1117  *      The device information page is fed to us via the standard
1118  *      PIO-IN protocol, but we hand-code it here. (TODO: investigate
1119  *      using standard PIO-IN paths)
1120  *
1121  *      After reading the device information page, we use several
1122  *      bits of information from it to initialize data structures
1123  *      that will be used during the lifetime of the ata_device.
1124  *      Other data from the info page is used to disqualify certain
1125  *      older ATA devices we do not wish to support.
1126  *
1127  *      LOCKING:
1128  *      Inherited from caller.  Some functions called by this function
1129  *      obtain the host_set lock.
1130  */
1131
1132 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1133 {
1134         struct ata_device *dev = &ap->device[device];
1135         unsigned int major_version;
1136         u16 tmp;
1137         unsigned long xfer_modes;
1138         u8 status;
1139         unsigned int using_edd;
1140         DECLARE_COMPLETION(wait);
1141         struct ata_queued_cmd *qc;
1142         unsigned long flags;
1143         int rc;
1144
1145         if (!ata_dev_present(dev)) {
1146                 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1147                         ap->id, device);
1148                 return;
1149         }
1150
1151         if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1152                 using_edd = 0;
1153         else
1154                 using_edd = 1;
1155
1156         DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1157
1158         assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1159                 dev->class == ATA_DEV_NONE);
1160
1161         ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1162
1163         qc = ata_qc_new_init(ap, dev);
1164         BUG_ON(qc == NULL);
1165
1166         ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1167         qc->dma_dir = DMA_FROM_DEVICE;
1168         qc->tf.protocol = ATA_PROT_PIO;
1169         qc->nsect = 1;
1170
1171 retry:
1172         if (dev->class == ATA_DEV_ATA) {
1173                 qc->tf.command = ATA_CMD_ID_ATA;
1174                 DPRINTK("do ATA identify\n");
1175         } else {
1176                 qc->tf.command = ATA_CMD_ID_ATAPI;
1177                 DPRINTK("do ATAPI identify\n");
1178         }
1179
1180         qc->waiting = &wait;
1181         qc->complete_fn = ata_qc_complete_noop;
1182
1183         spin_lock_irqsave(&ap->host_set->lock, flags);
1184         rc = ata_qc_issue(qc);
1185         spin_unlock_irqrestore(&ap->host_set->lock, flags);
1186
1187         if (rc)
1188                 goto err_out;
1189         else
1190                 wait_for_completion(&wait);
1191
1192         status = ata_chk_status(ap);
1193         if (status & ATA_ERR) {
1194                 /*
1195                  * arg!  EDD works for all test cases, but seems to return
1196                  * the ATA signature for some ATAPI devices.  Until the
1197                  * reason for this is found and fixed, we fix up the mess
1198                  * here.  If IDENTIFY DEVICE returns command aborted
1199                  * (as ATAPI devices do), then we issue an
1200                  * IDENTIFY PACKET DEVICE.
1201                  *
1202                  * ATA software reset (SRST, the default) does not appear
1203                  * to have this problem.
1204                  */
1205                 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1206                         u8 err = ata_chk_err(ap);
1207                         if (err & ATA_ABORTED) {
1208                                 dev->class = ATA_DEV_ATAPI;
1209                                 qc->cursg = 0;
1210                                 qc->cursg_ofs = 0;
1211                                 qc->cursect = 0;
1212                                 qc->nsect = 1;
1213                                 goto retry;
1214                         }
1215                 }
1216                 goto err_out;
1217         }
1218
1219         swap_buf_le16(dev->id, ATA_ID_WORDS);
1220
1221         /* print device capabilities */
1222         printk(KERN_DEBUG "ata%u: dev %u cfg "
1223                "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1224                ap->id, device, dev->id[49],
1225                dev->id[82], dev->id[83], dev->id[84],
1226                dev->id[85], dev->id[86], dev->id[87],
1227                dev->id[88]);
1228
1229         /*
1230          * common ATA, ATAPI feature tests
1231          */
1232
1233         /* we require DMA support (bits 8 of word 49) */
1234         if (!ata_id_has_dma(dev->id)) {
1235                 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1236                 goto err_out_nosup;
1237         }
1238
1239         /* quick-n-dirty find max transfer mode; for printk only */
1240         xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1241         if (!xfer_modes)
1242                 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1243         if (!xfer_modes) {
1244                 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1245                 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1246         }
1247
1248         ata_dump_id(dev);
1249
1250         /* ATA-specific feature tests */
1251         if (dev->class == ATA_DEV_ATA) {
1252                 if (!ata_id_is_ata(dev->id))    /* sanity check */
1253                         goto err_out_nosup;
1254
1255                 /* get major version */
1256                 tmp = dev->id[ATA_ID_MAJOR_VER];
1257                 for (major_version = 14; major_version >= 1; major_version--)
1258                         if (tmp & (1 << major_version))
1259                                 break;
1260
1261                 /*
1262                  * The exact sequence expected by certain pre-ATA4 drives is:
1263                  * SRST RESET
1264                  * IDENTIFY
1265                  * INITIALIZE DEVICE PARAMETERS
1266                  * anything else..
1267                  * Some drives were very specific about that exact sequence.
1268                  */
1269                 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1270                         ata_dev_init_params(ap, dev);
1271
1272                 if (ata_id_has_lba(dev->id)) {
1273                         dev->flags |= ATA_DFLAG_LBA;
1274
1275                         if (ata_id_has_lba48(dev->id)) {
1276                                 dev->flags |= ATA_DFLAG_LBA48;
1277                                 dev->n_sectors = ata_id_u64(dev->id, 100);
1278                         } else {
1279                                 dev->n_sectors = ata_id_u32(dev->id, 60);
1280                         }
1281
1282                         /* print device info to dmesg */
1283                         printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1284                                ap->id, device,
1285                                major_version,
1286                                ata_mode_string(xfer_modes),
1287                                (unsigned long long)dev->n_sectors,
1288                                dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1289                 } else { 
1290                         /* CHS */
1291
1292                         /* Default translation */
1293                         dev->cylinders  = dev->id[1];
1294                         dev->heads      = dev->id[3];
1295                         dev->sectors    = dev->id[6];
1296                         dev->n_sectors  = dev->cylinders * dev->heads * dev->sectors;
1297
1298                         if (ata_id_current_chs_valid(dev->id)) {
1299                                 /* Current CHS translation is valid. */
1300                                 dev->cylinders = dev->id[54];
1301                                 dev->heads     = dev->id[55];
1302                                 dev->sectors   = dev->id[56];
1303                                 
1304                                 dev->n_sectors = ata_id_u32(dev->id, 57);
1305                         }
1306
1307                         /* print device info to dmesg */
1308                         printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1309                                ap->id, device,
1310                                major_version,
1311                                ata_mode_string(xfer_modes),
1312                                (unsigned long long)dev->n_sectors,
1313                                (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1314
1315                 }
1316
1317                 ap->host->max_cmd_len = 16;
1318         }
1319
1320         /* ATAPI-specific feature tests */
1321         else {
1322                 if (ata_id_is_ata(dev->id))             /* sanity check */
1323                         goto err_out_nosup;
1324
1325                 rc = atapi_cdb_len(dev->id);
1326                 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1327                         printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1328                         goto err_out_nosup;
1329                 }
1330                 ap->cdb_len = (unsigned int) rc;
1331                 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1332
1333                 /* print device info to dmesg */
1334                 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1335                        ap->id, device,
1336                        ata_mode_string(xfer_modes));
1337         }
1338
1339         DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1340         return;
1341
1342 err_out_nosup:
1343         printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1344                ap->id, device);
1345 err_out:
1346         dev->class++;   /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1347         DPRINTK("EXIT, err\n");
1348 }
1349
1350
1351 static inline u8 ata_dev_knobble(struct ata_port *ap)
1352 {
1353         return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1354 }
1355
1356 /**
1357  *      ata_dev_config - Run device specific handlers and check for
1358  *                       SATA->PATA bridges
1359  *      @ap: Bus
1360  *      @i:  Device
1361  *
1362  *      LOCKING:
1363  */
1364
1365 void ata_dev_config(struct ata_port *ap, unsigned int i)
1366 {
1367         /* limit bridge transfers to udma5, 200 sectors */
1368         if (ata_dev_knobble(ap)) {
1369                 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1370                         ap->id, ap->device->devno);
1371                 ap->udma_mask &= ATA_UDMA5;
1372                 ap->host->max_sectors = ATA_MAX_SECTORS;
1373                 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1374                 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1375         }
1376
1377         if (ap->ops->dev_config)
1378                 ap->ops->dev_config(ap, &ap->device[i]);
1379 }
1380
1381 /**
1382  *      ata_bus_probe - Reset and probe ATA bus
1383  *      @ap: Bus to probe
1384  *
1385  *      Master ATA bus probing function.  Initiates a hardware-dependent
1386  *      bus reset, then attempts to identify any devices found on
1387  *      the bus.
1388  *
1389  *      LOCKING:
1390  *      PCI/etc. bus probe sem.
1391  *
1392  *      RETURNS:
1393  *      Zero on success, non-zero on error.
1394  */
1395
1396 static int ata_bus_probe(struct ata_port *ap)
1397 {
1398         unsigned int i, found = 0;
1399
1400         ap->ops->phy_reset(ap);
1401         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1402                 goto err_out;
1403
1404         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1405                 ata_dev_identify(ap, i);
1406                 if (ata_dev_present(&ap->device[i])) {
1407                         found = 1;
1408                         ata_dev_config(ap,i);
1409                 }
1410         }
1411
1412         if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1413                 goto err_out_disable;
1414
1415         ata_set_mode(ap);
1416         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1417                 goto err_out_disable;
1418
1419         return 0;
1420
1421 err_out_disable:
1422         ap->ops->port_disable(ap);
1423 err_out:
1424         return -1;
1425 }
1426
1427 /**
1428  *      ata_port_probe - Mark port as enabled
1429  *      @ap: Port for which we indicate enablement
1430  *
1431  *      Modify @ap data structure such that the system
1432  *      thinks that the entire port is enabled.
1433  *
1434  *      LOCKING: host_set lock, or some other form of
1435  *      serialization.
1436  */
1437
1438 void ata_port_probe(struct ata_port *ap)
1439 {
1440         ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1441 }
1442
1443 /**
1444  *      __sata_phy_reset - Wake/reset a low-level SATA PHY
1445  *      @ap: SATA port associated with target SATA PHY.
1446  *
1447  *      This function issues commands to standard SATA Sxxx
1448  *      PHY registers, to wake up the phy (and device), and
1449  *      clear any reset condition.
1450  *
1451  *      LOCKING:
1452  *      PCI/etc. bus probe sem.
1453  *
1454  */
1455 void __sata_phy_reset(struct ata_port *ap)
1456 {
1457         u32 sstatus;
1458         unsigned long timeout = jiffies + (HZ * 5);
1459
1460         if (ap->flags & ATA_FLAG_SATA_RESET) {
1461                 /* issue phy wake/reset */
1462                 scr_write_flush(ap, SCR_CONTROL, 0x301);
1463                 /* Couldn't find anything in SATA I/II specs, but
1464                  * AHCI-1.1 10.4.2 says at least 1 ms. */
1465                 mdelay(1);
1466         }
1467         scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1468
1469         /* wait for phy to become ready, if necessary */
1470         do {
1471                 msleep(200);
1472                 sstatus = scr_read(ap, SCR_STATUS);
1473                 if ((sstatus & 0xf) != 1)
1474                         break;
1475         } while (time_before(jiffies, timeout));
1476
1477         /* TODO: phy layer with polling, timeouts, etc. */
1478         if (sata_dev_present(ap))
1479                 ata_port_probe(ap);
1480         else {
1481                 sstatus = scr_read(ap, SCR_STATUS);
1482                 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1483                        ap->id, sstatus);
1484                 ata_port_disable(ap);
1485         }
1486
1487         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1488                 return;
1489
1490         if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1491                 ata_port_disable(ap);
1492                 return;
1493         }
1494
1495         ap->cbl = ATA_CBL_SATA;
1496 }
1497
1498 /**
1499  *      sata_phy_reset - Reset SATA bus.
1500  *      @ap: SATA port associated with target SATA PHY.
1501  *
1502  *      This function resets the SATA bus, and then probes
1503  *      the bus for devices.
1504  *
1505  *      LOCKING:
1506  *      PCI/etc. bus probe sem.
1507  *
1508  */
1509 void sata_phy_reset(struct ata_port *ap)
1510 {
1511         __sata_phy_reset(ap);
1512         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1513                 return;
1514         ata_bus_reset(ap);
1515 }
1516
1517 /**
1518  *      ata_port_disable - Disable port.
1519  *      @ap: Port to be disabled.
1520  *
1521  *      Modify @ap data structure such that the system
1522  *      thinks that the entire port is disabled, and should
1523  *      never attempt to probe or communicate with devices
1524  *      on this port.
1525  *
1526  *      LOCKING: host_set lock, or some other form of
1527  *      serialization.
1528  */
1529
1530 void ata_port_disable(struct ata_port *ap)
1531 {
1532         ap->device[0].class = ATA_DEV_NONE;
1533         ap->device[1].class = ATA_DEV_NONE;
1534         ap->flags |= ATA_FLAG_PORT_DISABLED;
1535 }
1536
1537 static struct {
1538         unsigned int shift;
1539         u8 base;
1540 } xfer_mode_classes[] = {
1541         { ATA_SHIFT_UDMA,       XFER_UDMA_0 },
1542         { ATA_SHIFT_MWDMA,      XFER_MW_DMA_0 },
1543         { ATA_SHIFT_PIO,        XFER_PIO_0 },
1544 };
1545
1546 static inline u8 base_from_shift(unsigned int shift)
1547 {
1548         int i;
1549
1550         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1551                 if (xfer_mode_classes[i].shift == shift)
1552                         return xfer_mode_classes[i].base;
1553
1554         return 0xff;
1555 }
1556
1557 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1558 {
1559         int ofs, idx;
1560         u8 base;
1561
1562         if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1563                 return;
1564
1565         if (dev->xfer_shift == ATA_SHIFT_PIO)
1566                 dev->flags |= ATA_DFLAG_PIO;
1567
1568         ata_dev_set_xfermode(ap, dev);
1569
1570         base = base_from_shift(dev->xfer_shift);
1571         ofs = dev->xfer_mode - base;
1572         idx = ofs + dev->xfer_shift;
1573         WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1574
1575         DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1576                 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1577
1578         printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1579                 ap->id, dev->devno, xfer_mode_str[idx]);
1580 }
1581
1582 static int ata_host_set_pio(struct ata_port *ap)
1583 {
1584         unsigned int mask;
1585         int x, i;
1586         u8 base, xfer_mode;
1587
1588         mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1589         x = fgb(mask);
1590         if (x < 0) {
1591                 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1592                 return -1;
1593         }
1594
1595         base = base_from_shift(ATA_SHIFT_PIO);
1596         xfer_mode = base + x;
1597
1598         DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1599                 (int)base, (int)xfer_mode, mask, x);
1600
1601         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1602                 struct ata_device *dev = &ap->device[i];
1603                 if (ata_dev_present(dev)) {
1604                         dev->pio_mode = xfer_mode;
1605                         dev->xfer_mode = xfer_mode;
1606                         dev->xfer_shift = ATA_SHIFT_PIO;
1607                         if (ap->ops->set_piomode)
1608                                 ap->ops->set_piomode(ap, dev);
1609                 }
1610         }
1611
1612         return 0;
1613 }
1614
1615 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1616                             unsigned int xfer_shift)
1617 {
1618         int i;
1619
1620         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1621                 struct ata_device *dev = &ap->device[i];
1622                 if (ata_dev_present(dev)) {
1623                         dev->dma_mode = xfer_mode;
1624                         dev->xfer_mode = xfer_mode;
1625                         dev->xfer_shift = xfer_shift;
1626                         if (ap->ops->set_dmamode)
1627                                 ap->ops->set_dmamode(ap, dev);
1628                 }
1629         }
1630 }
1631
1632 /**
1633  *      ata_set_mode - Program timings and issue SET FEATURES - XFER
1634  *      @ap: port on which timings will be programmed
1635  *
1636  *      Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1637  *
1638  *      LOCKING:
1639  *      PCI/etc. bus probe sem.
1640  *
1641  */
1642 static void ata_set_mode(struct ata_port *ap)
1643 {
1644         unsigned int i, xfer_shift;
1645         u8 xfer_mode;
1646         int rc;
1647
1648         /* step 1: always set host PIO timings */
1649         rc = ata_host_set_pio(ap);
1650         if (rc)
1651                 goto err_out;
1652
1653         /* step 2: choose the best data xfer mode */
1654         xfer_mode = xfer_shift = 0;
1655         rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1656         if (rc)
1657                 goto err_out;
1658
1659         /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1660         if (xfer_shift != ATA_SHIFT_PIO)
1661                 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1662
1663         /* step 4: update devices' xfer mode */
1664         ata_dev_set_mode(ap, &ap->device[0]);
1665         ata_dev_set_mode(ap, &ap->device[1]);
1666
1667         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1668                 return;
1669
1670         if (ap->ops->post_set_mode)
1671                 ap->ops->post_set_mode(ap);
1672
1673         for (i = 0; i < 2; i++) {
1674                 struct ata_device *dev = &ap->device[i];
1675                 ata_dev_set_protocol(dev);
1676         }
1677
1678         return;
1679
1680 err_out:
1681         ata_port_disable(ap);
1682 }
1683
1684 /**
1685  *      ata_busy_sleep - sleep until BSY clears, or timeout
1686  *      @ap: port containing status register to be polled
1687  *      @tmout_pat: impatience timeout
1688  *      @tmout: overall timeout
1689  *
1690  *      Sleep until ATA Status register bit BSY clears,
1691  *      or a timeout occurs.
1692  *
1693  *      LOCKING: None.
1694  *
1695  */
1696
1697 static unsigned int ata_busy_sleep (struct ata_port *ap,
1698                                     unsigned long tmout_pat,
1699                                     unsigned long tmout)
1700 {
1701         unsigned long timer_start, timeout;
1702         u8 status;
1703
1704         status = ata_busy_wait(ap, ATA_BUSY, 300);
1705         timer_start = jiffies;
1706         timeout = timer_start + tmout_pat;
1707         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1708                 msleep(50);
1709                 status = ata_busy_wait(ap, ATA_BUSY, 3);
1710         }
1711
1712         if (status & ATA_BUSY)
1713                 printk(KERN_WARNING "ata%u is slow to respond, "
1714                        "please be patient\n", ap->id);
1715
1716         timeout = timer_start + tmout;
1717         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1718                 msleep(50);
1719                 status = ata_chk_status(ap);
1720         }
1721
1722         if (status & ATA_BUSY) {
1723                 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1724                        ap->id, tmout / HZ);
1725                 return 1;
1726         }
1727
1728         return 0;
1729 }
1730
1731 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1732 {
1733         struct ata_ioports *ioaddr = &ap->ioaddr;
1734         unsigned int dev0 = devmask & (1 << 0);
1735         unsigned int dev1 = devmask & (1 << 1);
1736         unsigned long timeout;
1737
1738         /* if device 0 was found in ata_devchk, wait for its
1739          * BSY bit to clear
1740          */
1741         if (dev0)
1742                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1743
1744         /* if device 1 was found in ata_devchk, wait for
1745          * register access, then wait for BSY to clear
1746          */
1747         timeout = jiffies + ATA_TMOUT_BOOT;
1748         while (dev1) {
1749                 u8 nsect, lbal;
1750
1751                 ap->ops->dev_select(ap, 1);
1752                 if (ap->flags & ATA_FLAG_MMIO) {
1753                         nsect = readb((void __iomem *) ioaddr->nsect_addr);
1754                         lbal = readb((void __iomem *) ioaddr->lbal_addr);
1755                 } else {
1756                         nsect = inb(ioaddr->nsect_addr);
1757                         lbal = inb(ioaddr->lbal_addr);
1758                 }
1759                 if ((nsect == 1) && (lbal == 1))
1760                         break;
1761                 if (time_after(jiffies, timeout)) {
1762                         dev1 = 0;
1763                         break;
1764                 }
1765                 msleep(50);     /* give drive a breather */
1766         }
1767         if (dev1)
1768                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1769
1770         /* is all this really necessary? */
1771         ap->ops->dev_select(ap, 0);
1772         if (dev1)
1773                 ap->ops->dev_select(ap, 1);
1774         if (dev0)
1775                 ap->ops->dev_select(ap, 0);
1776 }
1777
1778 /**
1779  *      ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1780  *      @ap: Port to reset and probe
1781  *
1782  *      Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1783  *      probe the bus.  Not often used these days.
1784  *
1785  *      LOCKING:
1786  *      PCI/etc. bus probe sem.
1787  *
1788  */
1789
1790 static unsigned int ata_bus_edd(struct ata_port *ap)
1791 {
1792         struct ata_taskfile tf;
1793
1794         /* set up execute-device-diag (bus reset) taskfile */
1795         /* also, take interrupts to a known state (disabled) */
1796         DPRINTK("execute-device-diag\n");
1797         ata_tf_init(ap, &tf, 0);
1798         tf.ctl |= ATA_NIEN;
1799         tf.command = ATA_CMD_EDD;
1800         tf.protocol = ATA_PROT_NODATA;
1801
1802         /* do bus reset */
1803         ata_tf_to_host(ap, &tf);
1804
1805         /* spec says at least 2ms.  but who knows with those
1806          * crazy ATAPI devices...
1807          */
1808         msleep(150);
1809
1810         return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1811 }
1812
1813 static unsigned int ata_bus_softreset(struct ata_port *ap,
1814                                       unsigned int devmask)
1815 {
1816         struct ata_ioports *ioaddr = &ap->ioaddr;
1817
1818         DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1819
1820         /* software reset.  causes dev0 to be selected */
1821         if (ap->flags & ATA_FLAG_MMIO) {
1822                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1823                 udelay(20);     /* FIXME: flush */
1824                 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1825                 udelay(20);     /* FIXME: flush */
1826                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1827         } else {
1828                 outb(ap->ctl, ioaddr->ctl_addr);
1829                 udelay(10);
1830                 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1831                 udelay(10);
1832                 outb(ap->ctl, ioaddr->ctl_addr);
1833         }
1834
1835         /* spec mandates ">= 2ms" before checking status.
1836          * We wait 150ms, because that was the magic delay used for
1837          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1838          * between when the ATA command register is written, and then
1839          * status is checked.  Because waiting for "a while" before
1840          * checking status is fine, post SRST, we perform this magic
1841          * delay here as well.
1842          */
1843         msleep(150);
1844
1845         ata_bus_post_reset(ap, devmask);
1846
1847         return 0;
1848 }
1849
1850 /**
1851  *      ata_bus_reset - reset host port and associated ATA channel
1852  *      @ap: port to reset
1853  *
1854  *      This is typically the first time we actually start issuing
1855  *      commands to the ATA channel.  We wait for BSY to clear, then
1856  *      issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1857  *      result.  Determine what devices, if any, are on the channel
1858  *      by looking at the device 0/1 error register.  Look at the signature
1859  *      stored in each device's taskfile registers, to determine if
1860  *      the device is ATA or ATAPI.
1861  *
1862  *      LOCKING:
1863  *      PCI/etc. bus probe sem.
1864  *      Obtains host_set lock.
1865  *
1866  *      SIDE EFFECTS:
1867  *      Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1868  */
1869
1870 void ata_bus_reset(struct ata_port *ap)
1871 {
1872         struct ata_ioports *ioaddr = &ap->ioaddr;
1873         unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1874         u8 err;
1875         unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1876
1877         DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1878
1879         /* determine if device 0/1 are present */
1880         if (ap->flags & ATA_FLAG_SATA_RESET)
1881                 dev0 = 1;
1882         else {
1883                 dev0 = ata_devchk(ap, 0);
1884                 if (slave_possible)
1885                         dev1 = ata_devchk(ap, 1);
1886         }
1887
1888         if (dev0)
1889                 devmask |= (1 << 0);
1890         if (dev1)
1891                 devmask |= (1 << 1);
1892
1893         /* select device 0 again */
1894         ap->ops->dev_select(ap, 0);
1895
1896         /* issue bus reset */
1897         if (ap->flags & ATA_FLAG_SRST)
1898                 rc = ata_bus_softreset(ap, devmask);
1899         else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1900                 /* set up device control */
1901                 if (ap->flags & ATA_FLAG_MMIO)
1902                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1903                 else
1904                         outb(ap->ctl, ioaddr->ctl_addr);
1905                 rc = ata_bus_edd(ap);
1906         }
1907
1908         if (rc)
1909                 goto err_out;
1910
1911         /*
1912          * determine by signature whether we have ATA or ATAPI devices
1913          */
1914         err = ata_dev_try_classify(ap, 0);
1915         if ((slave_possible) && (err != 0x81))
1916                 ata_dev_try_classify(ap, 1);
1917
1918         /* re-enable interrupts */
1919         if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
1920                 ata_irq_on(ap);
1921
1922         /* is double-select really necessary? */
1923         if (ap->device[1].class != ATA_DEV_NONE)
1924                 ap->ops->dev_select(ap, 1);
1925         if (ap->device[0].class != ATA_DEV_NONE)
1926                 ap->ops->dev_select(ap, 0);
1927
1928         /* if no devices were detected, disable this port */
1929         if ((ap->device[0].class == ATA_DEV_NONE) &&
1930             (ap->device[1].class == ATA_DEV_NONE))
1931                 goto err_out;
1932
1933         if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1934                 /* set up device control for ATA_FLAG_SATA_RESET */
1935                 if (ap->flags & ATA_FLAG_MMIO)
1936                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1937                 else
1938                         outb(ap->ctl, ioaddr->ctl_addr);
1939         }
1940
1941         DPRINTK("EXIT\n");
1942         return;
1943
1944 err_out:
1945         printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1946         ap->ops->port_disable(ap);
1947
1948         DPRINTK("EXIT\n");
1949 }
1950
1951 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1952 {
1953         printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1954                 ap->id, dev->devno);
1955 }
1956
1957 static const char * ata_dma_blacklist [] = {
1958         "WDC AC11000H",
1959         "WDC AC22100H",
1960         "WDC AC32500H",
1961         "WDC AC33100H",
1962         "WDC AC31600H",
1963         "WDC AC32100H",
1964         "WDC AC23200L",
1965         "Compaq CRD-8241B",
1966         "CRD-8400B",
1967         "CRD-8480B",
1968         "CRD-8482B",
1969         "CRD-84",
1970         "SanDisk SDP3B",
1971         "SanDisk SDP3B-64",
1972         "SANYO CD-ROM CRD",
1973         "HITACHI CDR-8",
1974         "HITACHI CDR-8335",
1975         "HITACHI CDR-8435",
1976         "Toshiba CD-ROM XM-6202B",
1977         "TOSHIBA CD-ROM XM-1702BC",
1978         "CD-532E-A",
1979         "E-IDE CD-ROM CR-840",
1980         "CD-ROM Drive/F5A",
1981         "WPI CDD-820",
1982         "SAMSUNG CD-ROM SC-148C",
1983         "SAMSUNG CD-ROM SC",
1984         "SanDisk SDP3B-64",
1985         "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1986         "_NEC DV5800A",
1987 };
1988
1989 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1990 {
1991         unsigned char model_num[40];
1992         char *s;
1993         unsigned int len;
1994         int i;
1995
1996         ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1997                           sizeof(model_num));
1998         s = &model_num[0];
1999         len = strnlen(s, sizeof(model_num));
2000
2001         /* ATAPI specifies that empty space is blank-filled; remove blanks */
2002         while ((len > 0) && (s[len - 1] == ' ')) {
2003                 len--;
2004                 s[len] = 0;
2005         }
2006
2007         for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2008                 if (!strncmp(ata_dma_blacklist[i], s, len))
2009                         return 1;
2010
2011         return 0;
2012 }
2013
2014 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2015 {
2016         struct ata_device *master, *slave;
2017         unsigned int mask;
2018
2019         master = &ap->device[0];
2020         slave = &ap->device[1];
2021
2022         assert (ata_dev_present(master) || ata_dev_present(slave));
2023
2024         if (shift == ATA_SHIFT_UDMA) {
2025                 mask = ap->udma_mask;
2026                 if (ata_dev_present(master)) {
2027                         mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2028                         if (ata_dma_blacklisted(ap, master)) {
2029                                 mask = 0;
2030                                 ata_pr_blacklisted(ap, master);
2031                         }
2032                 }
2033                 if (ata_dev_present(slave)) {
2034                         mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2035                         if (ata_dma_blacklisted(ap, slave)) {
2036                                 mask = 0;
2037                                 ata_pr_blacklisted(ap, slave);
2038                         }
2039                 }
2040         }
2041         else if (shift == ATA_SHIFT_MWDMA) {
2042                 mask = ap->mwdma_mask;
2043                 if (ata_dev_present(master)) {
2044                         mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2045                         if (ata_dma_blacklisted(ap, master)) {
2046                                 mask = 0;
2047                                 ata_pr_blacklisted(ap, master);
2048                         }
2049                 }
2050                 if (ata_dev_present(slave)) {
2051                         mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2052                         if (ata_dma_blacklisted(ap, slave)) {
2053                                 mask = 0;
2054                                 ata_pr_blacklisted(ap, slave);
2055                         }
2056                 }
2057         }
2058         else if (shift == ATA_SHIFT_PIO) {
2059                 mask = ap->pio_mask;
2060                 if (ata_dev_present(master)) {
2061                         /* spec doesn't return explicit support for
2062                          * PIO0-2, so we fake it
2063                          */
2064                         u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2065                         tmp_mode <<= 3;
2066                         tmp_mode |= 0x7;
2067                         mask &= tmp_mode;
2068                 }
2069                 if (ata_dev_present(slave)) {
2070                         /* spec doesn't return explicit support for
2071                          * PIO0-2, so we fake it
2072                          */
2073                         u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2074                         tmp_mode <<= 3;
2075                         tmp_mode |= 0x7;
2076                         mask &= tmp_mode;
2077                 }
2078         }
2079         else {
2080                 mask = 0xffffffff; /* shut up compiler warning */
2081                 BUG();
2082         }
2083
2084         return mask;
2085 }
2086
2087 /* find greatest bit */
2088 static int fgb(u32 bitmap)
2089 {
2090         unsigned int i;
2091         int x = -1;
2092
2093         for (i = 0; i < 32; i++)
2094                 if (bitmap & (1 << i))
2095                         x = i;
2096
2097         return x;
2098 }
2099
2100 /**
2101  *      ata_choose_xfer_mode - attempt to find best transfer mode
2102  *      @ap: Port for which an xfer mode will be selected
2103  *      @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2104  *      @xfer_shift_out: (output) bit shift that selects this mode
2105  *
2106  *      Based on host and device capabilities, determine the
2107  *      maximum transfer mode that is amenable to all.
2108  *
2109  *      LOCKING:
2110  *      PCI/etc. bus probe sem.
2111  *
2112  *      RETURNS:
2113  *      Zero on success, negative on error.
2114  */
2115
2116 static int ata_choose_xfer_mode(struct ata_port *ap,
2117                                 u8 *xfer_mode_out,
2118                                 unsigned int *xfer_shift_out)
2119 {
2120         unsigned int mask, shift;
2121         int x, i;
2122
2123         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2124                 shift = xfer_mode_classes[i].shift;
2125                 mask = ata_get_mode_mask(ap, shift);
2126
2127                 x = fgb(mask);
2128                 if (x >= 0) {
2129                         *xfer_mode_out = xfer_mode_classes[i].base + x;
2130                         *xfer_shift_out = shift;
2131                         return 0;
2132                 }
2133         }
2134
2135         return -1;
2136 }
2137
2138 /**
2139  *      ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2140  *      @ap: Port associated with device @dev
2141  *      @dev: Device to which command will be sent
2142  *
2143  *      Issue SET FEATURES - XFER MODE command to device @dev
2144  *      on port @ap.
2145  *
2146  *      LOCKING:
2147  *      PCI/etc. bus probe sem.
2148  */
2149
2150 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2151 {
2152         DECLARE_COMPLETION(wait);
2153         struct ata_queued_cmd *qc;
2154         int rc;
2155         unsigned long flags;
2156
2157         /* set up set-features taskfile */
2158         DPRINTK("set features - xfer mode\n");
2159
2160         qc = ata_qc_new_init(ap, dev);
2161         BUG_ON(qc == NULL);
2162
2163         qc->tf.command = ATA_CMD_SET_FEATURES;
2164         qc->tf.feature = SETFEATURES_XFER;
2165         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2166         qc->tf.protocol = ATA_PROT_NODATA;
2167         qc->tf.nsect = dev->xfer_mode;
2168
2169         qc->waiting = &wait;
2170         qc->complete_fn = ata_qc_complete_noop;
2171
2172         spin_lock_irqsave(&ap->host_set->lock, flags);
2173         rc = ata_qc_issue(qc);
2174         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2175
2176         if (rc)
2177                 ata_port_disable(ap);
2178         else
2179                 wait_for_completion(&wait);
2180
2181         DPRINTK("EXIT\n");
2182 }
2183
2184 /**
2185  *      ata_dev_init_params - Issue INIT DEV PARAMS command
2186  *      @ap: Port associated with device @dev
2187  *      @dev: Device to which command will be sent
2188  *
2189  *      LOCKING:
2190  */
2191
2192 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2193 {
2194         DECLARE_COMPLETION(wait);
2195         struct ata_queued_cmd *qc;
2196         int rc;
2197         unsigned long flags;
2198         u16 sectors = dev->id[6];
2199         u16 heads   = dev->id[3];
2200
2201         /* Number of sectors per track 1-255. Number of heads 1-16 */
2202         if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2203                 return;
2204
2205         /* set up init dev params taskfile */
2206         DPRINTK("init dev params \n");
2207
2208         qc = ata_qc_new_init(ap, dev);
2209         BUG_ON(qc == NULL);
2210
2211         qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2212         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2213         qc->tf.protocol = ATA_PROT_NODATA;
2214         qc->tf.nsect = sectors;
2215         qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2216
2217         qc->waiting = &wait;
2218         qc->complete_fn = ata_qc_complete_noop;
2219
2220         spin_lock_irqsave(&ap->host_set->lock, flags);
2221         rc = ata_qc_issue(qc);
2222         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2223
2224         if (rc)
2225                 ata_port_disable(ap);
2226         else
2227                 wait_for_completion(&wait);
2228
2229         DPRINTK("EXIT\n");
2230 }
2231
2232 /**
2233  *      ata_sg_clean - Unmap DMA memory associated with command
2234  *      @qc: Command containing DMA memory to be released
2235  *
2236  *      Unmap all mapped DMA memory associated with this command.
2237  *
2238  *      LOCKING:
2239  *      spin_lock_irqsave(host_set lock)
2240  */
2241
2242 static void ata_sg_clean(struct ata_queued_cmd *qc)
2243 {
2244         struct ata_port *ap = qc->ap;
2245         struct scatterlist *sg = qc->sg;
2246         int dir = qc->dma_dir;
2247
2248         assert(qc->flags & ATA_QCFLAG_DMAMAP);
2249         assert(sg != NULL);
2250
2251         if (qc->flags & ATA_QCFLAG_SINGLE)
2252                 assert(qc->n_elem == 1);
2253
2254         DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2255
2256         if (qc->flags & ATA_QCFLAG_SG)
2257                 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2258         else
2259                 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2260                                  sg_dma_len(&sg[0]), dir);
2261
2262         qc->flags &= ~ATA_QCFLAG_DMAMAP;
2263         qc->sg = NULL;
2264 }
2265
2266 /**
2267  *      ata_fill_sg - Fill PCI IDE PRD table
2268  *      @qc: Metadata associated with taskfile to be transferred
2269  *
2270  *      Fill PCI IDE PRD (scatter-gather) table with segments
2271  *      associated with the current disk command.
2272  *
2273  *      LOCKING:
2274  *      spin_lock_irqsave(host_set lock)
2275  *
2276  */
2277 static void ata_fill_sg(struct ata_queued_cmd *qc)
2278 {
2279         struct scatterlist *sg = qc->sg;
2280         struct ata_port *ap = qc->ap;
2281         unsigned int idx, nelem;
2282
2283         assert(sg != NULL);
2284         assert(qc->n_elem > 0);
2285
2286         idx = 0;
2287         for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2288                 u32 addr, offset;
2289                 u32 sg_len, len;
2290
2291                 /* determine if physical DMA addr spans 64K boundary.
2292                  * Note h/w doesn't support 64-bit, so we unconditionally
2293                  * truncate dma_addr_t to u32.
2294                  */
2295                 addr = (u32) sg_dma_address(sg);
2296                 sg_len = sg_dma_len(sg);
2297
2298                 while (sg_len) {
2299                         offset = addr & 0xffff;
2300                         len = sg_len;
2301                         if ((offset + sg_len) > 0x10000)
2302                                 len = 0x10000 - offset;
2303
2304                         ap->prd[idx].addr = cpu_to_le32(addr);
2305                         ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2306                         VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2307
2308                         idx++;
2309                         sg_len -= len;
2310                         addr += len;
2311                 }
2312         }
2313
2314         if (idx)
2315                 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2316 }
2317 /**
2318  *      ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2319  *      @qc: Metadata associated with taskfile to check
2320  *
2321  *      Allow low-level driver to filter ATA PACKET commands, returning
2322  *      a status indicating whether or not it is OK to use DMA for the
2323  *      supplied PACKET command.
2324  *
2325  *      LOCKING:
2326  *      spin_lock_irqsave(host_set lock)
2327  *
2328  *      RETURNS: 0 when ATAPI DMA can be used
2329  *               nonzero otherwise
2330  */
2331 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2332 {
2333         struct ata_port *ap = qc->ap;
2334         int rc = 0; /* Assume ATAPI DMA is OK by default */
2335
2336         if (ap->ops->check_atapi_dma)
2337                 rc = ap->ops->check_atapi_dma(qc);
2338
2339         return rc;
2340 }
2341 /**
2342  *      ata_qc_prep - Prepare taskfile for submission
2343  *      @qc: Metadata associated with taskfile to be prepared
2344  *
2345  *      Prepare ATA taskfile for submission.
2346  *
2347  *      LOCKING:
2348  *      spin_lock_irqsave(host_set lock)
2349  */
2350 void ata_qc_prep(struct ata_queued_cmd *qc)
2351 {
2352         if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2353                 return;
2354
2355         ata_fill_sg(qc);
2356 }
2357
2358 /**
2359  *      ata_sg_init_one - Associate command with memory buffer
2360  *      @qc: Command to be associated
2361  *      @buf: Memory buffer
2362  *      @buflen: Length of memory buffer, in bytes.
2363  *
2364  *      Initialize the data-related elements of queued_cmd @qc
2365  *      to point to a single memory buffer, @buf of byte length @buflen.
2366  *
2367  *      LOCKING:
2368  *      spin_lock_irqsave(host_set lock)
2369  */
2370
2371 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2372 {
2373         struct scatterlist *sg;
2374
2375         qc->flags |= ATA_QCFLAG_SINGLE;
2376
2377         memset(&qc->sgent, 0, sizeof(qc->sgent));
2378         qc->sg = &qc->sgent;
2379         qc->n_elem = 1;
2380         qc->buf_virt = buf;
2381
2382         sg = qc->sg;
2383         sg->page = virt_to_page(buf);
2384         sg->offset = (unsigned long) buf & ~PAGE_MASK;
2385         sg->length = buflen;
2386 }
2387
2388 /**
2389  *      ata_sg_init - Associate command with scatter-gather table.
2390  *      @qc: Command to be associated
2391  *      @sg: Scatter-gather table.
2392  *      @n_elem: Number of elements in s/g table.
2393  *
2394  *      Initialize the data-related elements of queued_cmd @qc
2395  *      to point to a scatter-gather table @sg, containing @n_elem
2396  *      elements.
2397  *
2398  *      LOCKING:
2399  *      spin_lock_irqsave(host_set lock)
2400  */
2401
2402 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2403                  unsigned int n_elem)
2404 {
2405         qc->flags |= ATA_QCFLAG_SG;
2406         qc->sg = sg;
2407         qc->n_elem = n_elem;
2408 }
2409
2410 /**
2411  *      ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2412  *      @qc: Command with memory buffer to be mapped.
2413  *
2414  *      DMA-map the memory buffer associated with queued_cmd @qc.
2415  *
2416  *      LOCKING:
2417  *      spin_lock_irqsave(host_set lock)
2418  *
2419  *      RETURNS:
2420  *      Zero on success, negative on error.
2421  */
2422
2423 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2424 {
2425         struct ata_port *ap = qc->ap;
2426         int dir = qc->dma_dir;
2427         struct scatterlist *sg = qc->sg;
2428         dma_addr_t dma_address;
2429
2430         dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2431                                      sg->length, dir);
2432         if (dma_mapping_error(dma_address))
2433                 return -1;
2434
2435         sg_dma_address(sg) = dma_address;
2436         sg_dma_len(sg) = sg->length;
2437
2438         DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2439                 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2440
2441         return 0;
2442 }
2443
2444 /**
2445  *      ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2446  *      @qc: Command with scatter-gather table to be mapped.
2447  *
2448  *      DMA-map the scatter-gather table associated with queued_cmd @qc.
2449  *
2450  *      LOCKING:
2451  *      spin_lock_irqsave(host_set lock)
2452  *
2453  *      RETURNS:
2454  *      Zero on success, negative on error.
2455  *
2456  */
2457
2458 static int ata_sg_setup(struct ata_queued_cmd *qc)
2459 {
2460         struct ata_port *ap = qc->ap;
2461         struct scatterlist *sg = qc->sg;
2462         int n_elem, dir;
2463
2464         VPRINTK("ENTER, ata%u\n", ap->id);
2465         assert(qc->flags & ATA_QCFLAG_SG);
2466
2467         dir = qc->dma_dir;
2468         n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2469         if (n_elem < 1)
2470                 return -1;
2471
2472         DPRINTK("%d sg elements mapped\n", n_elem);
2473
2474         qc->n_elem = n_elem;
2475
2476         return 0;
2477 }
2478
2479 /**
2480  *      ata_poll_qc_complete - turn irq back on and finish qc
2481  *      @qc: Command to complete
2482  *      @drv_stat: ATA status register content
2483  *
2484  *      LOCKING:
2485  *      None.  (grabs host lock)
2486  */
2487
2488 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2489 {
2490         struct ata_port *ap = qc->ap;
2491         unsigned long flags;
2492
2493         spin_lock_irqsave(&ap->host_set->lock, flags);
2494         ap->flags &= ~ATA_FLAG_NOINTR;
2495         ata_irq_on(ap);
2496         ata_qc_complete(qc, drv_stat);
2497         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2498 }
2499
2500 /**
2501  *      ata_pio_poll -
2502  *      @ap:
2503  *
2504  *      LOCKING:
2505  *      None.  (executing in kernel thread context)
2506  *
2507  *      RETURNS:
2508  *
2509  */
2510
2511 static unsigned long ata_pio_poll(struct ata_port *ap)
2512 {
2513         u8 status;
2514         unsigned int poll_state = HSM_ST_UNKNOWN;
2515         unsigned int reg_state = HSM_ST_UNKNOWN;
2516         const unsigned int tmout_state = HSM_ST_TMOUT;
2517
2518         switch (ap->hsm_task_state) {
2519         case HSM_ST:
2520         case HSM_ST_POLL:
2521                 poll_state = HSM_ST_POLL;
2522                 reg_state = HSM_ST;
2523                 break;
2524         case HSM_ST_LAST:
2525         case HSM_ST_LAST_POLL:
2526                 poll_state = HSM_ST_LAST_POLL;
2527                 reg_state = HSM_ST_LAST;
2528                 break;
2529         default:
2530                 BUG();
2531                 break;
2532         }
2533
2534         status = ata_chk_status(ap);
2535         if (status & ATA_BUSY) {
2536                 if (time_after(jiffies, ap->pio_task_timeout)) {
2537                         ap->hsm_task_state = tmout_state;
2538                         return 0;
2539                 }
2540                 ap->hsm_task_state = poll_state;
2541                 return ATA_SHORT_PAUSE;
2542         }
2543
2544         ap->hsm_task_state = reg_state;
2545         return 0;
2546 }
2547
2548 /**
2549  *      ata_pio_complete -
2550  *      @ap:
2551  *
2552  *      LOCKING:
2553  *      None.  (executing in kernel thread context)
2554  *
2555  *      RETURNS:
2556  *      Non-zero if qc completed, zero otherwise.
2557  */
2558
2559 static int ata_pio_complete (struct ata_port *ap)
2560 {
2561         struct ata_queued_cmd *qc;
2562         u8 drv_stat;
2563
2564         /*
2565          * This is purely heuristic.  This is a fast path.  Sometimes when
2566          * we enter, BSY will be cleared in a chk-status or two.  If not,
2567          * the drive is probably seeking or something.  Snooze for a couple
2568          * msecs, then chk-status again.  If still busy, fall back to
2569          * HSM_ST_POLL state.
2570          */
2571         drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2572         if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2573                 msleep(2);
2574                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2575                 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2576                         ap->hsm_task_state = HSM_ST_LAST_POLL;
2577                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2578                         return 0;
2579                 }
2580         }
2581
2582         drv_stat = ata_wait_idle(ap);
2583         if (!ata_ok(drv_stat)) {
2584                 ap->hsm_task_state = HSM_ST_ERR;
2585                 return 0;
2586         }
2587
2588         qc = ata_qc_from_tag(ap, ap->active_tag);
2589         assert(qc != NULL);
2590
2591         ap->hsm_task_state = HSM_ST_IDLE;
2592
2593         ata_poll_qc_complete(qc, drv_stat);
2594
2595         /* another command may start at this point */
2596
2597         return 1;
2598 }
2599
2600
2601 /**
2602  *      swap_buf_le16 -
2603  *      @buf:  Buffer to swap
2604  *      @buf_words:  Number of 16-bit words in buffer.
2605  *
2606  *      Swap halves of 16-bit words if needed to convert from
2607  *      little-endian byte order to native cpu byte order, or
2608  *      vice-versa.
2609  *
2610  *      LOCKING:
2611  */
2612 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2613 {
2614 #ifdef __BIG_ENDIAN
2615         unsigned int i;
2616
2617         for (i = 0; i < buf_words; i++)
2618                 buf[i] = le16_to_cpu(buf[i]);
2619 #endif /* __BIG_ENDIAN */
2620 }
2621
2622 /**
2623  *      ata_mmio_data_xfer - Transfer data by MMIO
2624  *      @ap: port to read/write
2625  *      @buf: data buffer
2626  *      @buflen: buffer length
2627  *      @write_data: read/write
2628  *
2629  *      Transfer data from/to the device data register by MMIO.
2630  *
2631  *      LOCKING:
2632  *      Inherited from caller.
2633  *
2634  */
2635
2636 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2637                                unsigned int buflen, int write_data)
2638 {
2639         unsigned int i;
2640         unsigned int words = buflen >> 1;
2641         u16 *buf16 = (u16 *) buf;
2642         void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2643
2644         /* Transfer multiple of 2 bytes */
2645         if (write_data) {
2646                 for (i = 0; i < words; i++)
2647                         writew(le16_to_cpu(buf16[i]), mmio);
2648         } else {
2649                 for (i = 0; i < words; i++)
2650                         buf16[i] = cpu_to_le16(readw(mmio));
2651         }
2652
2653         /* Transfer trailing 1 byte, if any. */
2654         if (unlikely(buflen & 0x01)) {
2655                 u16 align_buf[1] = { 0 };
2656                 unsigned char *trailing_buf = buf + buflen - 1;
2657
2658                 if (write_data) {
2659                         memcpy(align_buf, trailing_buf, 1);
2660                         writew(le16_to_cpu(align_buf[0]), mmio);
2661                 } else {
2662                         align_buf[0] = cpu_to_le16(readw(mmio));
2663                         memcpy(trailing_buf, align_buf, 1);
2664                 }
2665         }
2666 }
2667
2668 /**
2669  *      ata_pio_data_xfer - Transfer data by PIO
2670  *      @ap: port to read/write
2671  *      @buf: data buffer
2672  *      @buflen: buffer length
2673  *      @write_data: read/write
2674  *
2675  *      Transfer data from/to the device data register by PIO.
2676  *
2677  *      LOCKING:
2678  *      Inherited from caller.
2679  *
2680  */
2681
2682 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2683                               unsigned int buflen, int write_data)
2684 {
2685         unsigned int words = buflen >> 1;
2686
2687         /* Transfer multiple of 2 bytes */
2688         if (write_data)
2689                 outsw(ap->ioaddr.data_addr, buf, words);
2690         else
2691                 insw(ap->ioaddr.data_addr, buf, words);
2692
2693         /* Transfer trailing 1 byte, if any. */
2694         if (unlikely(buflen & 0x01)) {
2695                 u16 align_buf[1] = { 0 };
2696                 unsigned char *trailing_buf = buf + buflen - 1;
2697
2698                 if (write_data) {
2699                         memcpy(align_buf, trailing_buf, 1);
2700                         outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2701                 } else {
2702                         align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2703                         memcpy(trailing_buf, align_buf, 1);
2704                 }
2705         }
2706 }
2707
2708 /**
2709  *      ata_data_xfer - Transfer data from/to the data register.
2710  *      @ap: port to read/write
2711  *      @buf: data buffer
2712  *      @buflen: buffer length
2713  *      @do_write: read/write
2714  *
2715  *      Transfer data from/to the device data register.
2716  *
2717  *      LOCKING:
2718  *      Inherited from caller.
2719  *
2720  */
2721
2722 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2723                           unsigned int buflen, int do_write)
2724 {
2725         if (ap->flags & ATA_FLAG_MMIO)
2726                 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2727         else
2728                 ata_pio_data_xfer(ap, buf, buflen, do_write);
2729 }
2730
2731 /**
2732  *      ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2733  *      @qc: Command on going
2734  *
2735  *      Transfer ATA_SECT_SIZE of data from/to the ATA device.
2736  *
2737  *      LOCKING:
2738  *      Inherited from caller.
2739  */
2740
2741 static void ata_pio_sector(struct ata_queued_cmd *qc)
2742 {
2743         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2744         struct scatterlist *sg = qc->sg;
2745         struct ata_port *ap = qc->ap;
2746         struct page *page;
2747         unsigned int offset;
2748         unsigned char *buf;
2749
2750         if (qc->cursect == (qc->nsect - 1))
2751                 ap->hsm_task_state = HSM_ST_LAST;
2752
2753         page = sg[qc->cursg].page;
2754         offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2755
2756         /* get the current page and offset */
2757         page = nth_page(page, (offset >> PAGE_SHIFT));
2758         offset %= PAGE_SIZE;
2759
2760         buf = kmap(page) + offset;
2761
2762         qc->cursect++;
2763         qc->cursg_ofs++;
2764
2765         if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2766                 qc->cursg++;
2767                 qc->cursg_ofs = 0;
2768         }
2769
2770         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2771
2772         /* do the actual data transfer */
2773         do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2774         ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2775
2776         kunmap(page);
2777 }
2778
2779 /**
2780  *      __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2781  *      @qc: Command on going
2782  *      @bytes: number of bytes
2783  *
2784  *      Transfer Transfer data from/to the ATAPI device.
2785  *
2786  *      LOCKING:
2787  *      Inherited from caller.
2788  *
2789  */
2790
2791 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2792 {
2793         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2794         struct scatterlist *sg = qc->sg;
2795         struct ata_port *ap = qc->ap;
2796         struct page *page;
2797         unsigned char *buf;
2798         unsigned int offset, count;
2799
2800         if (qc->curbytes + bytes >= qc->nbytes)
2801                 ap->hsm_task_state = HSM_ST_LAST;
2802
2803 next_sg:
2804         if (unlikely(qc->cursg >= qc->n_elem)) {
2805                 /*
2806                  * The end of qc->sg is reached and the device expects
2807                  * more data to transfer. In order not to overrun qc->sg
2808                  * and fulfill length specified in the byte count register,
2809                  *    - for read case, discard trailing data from the device
2810                  *    - for write case, padding zero data to the device
2811                  */
2812                 u16 pad_buf[1] = { 0 };
2813                 unsigned int words = bytes >> 1;
2814                 unsigned int i;
2815
2816                 if (words) /* warning if bytes > 1 */
2817                         printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2818                                ap->id, bytes);
2819
2820                 for (i = 0; i < words; i++)
2821                         ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2822
2823                 ap->hsm_task_state = HSM_ST_LAST;
2824                 return;
2825         }
2826
2827         sg = &qc->sg[qc->cursg];
2828
2829         page = sg->page;
2830         offset = sg->offset + qc->cursg_ofs;
2831
2832         /* get the current page and offset */
2833         page = nth_page(page, (offset >> PAGE_SHIFT));
2834         offset %= PAGE_SIZE;
2835
2836         /* don't overrun current sg */
2837         count = min(sg->length - qc->cursg_ofs, bytes);
2838
2839         /* don't cross page boundaries */
2840         count = min(count, (unsigned int)PAGE_SIZE - offset);
2841
2842         buf = kmap(page) + offset;
2843
2844         bytes -= count;
2845         qc->curbytes += count;
2846         qc->cursg_ofs += count;
2847
2848         if (qc->cursg_ofs == sg->length) {
2849                 qc->cursg++;
2850                 qc->cursg_ofs = 0;
2851         }
2852
2853         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2854
2855         /* do the actual data transfer */
2856         ata_data_xfer(ap, buf, count, do_write);
2857
2858         kunmap(page);
2859
2860         if (bytes)
2861                 goto next_sg;
2862 }
2863
2864 /**
2865  *      atapi_pio_bytes - Transfer data from/to the ATAPI device.
2866  *      @qc: Command on going
2867  *
2868  *      Transfer Transfer data from/to the ATAPI device.
2869  *
2870  *      LOCKING:
2871  *      Inherited from caller.
2872  *
2873  */
2874
2875 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2876 {
2877         struct ata_port *ap = qc->ap;
2878         struct ata_device *dev = qc->dev;
2879         unsigned int ireason, bc_lo, bc_hi, bytes;
2880         int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2881
2882         ap->ops->tf_read(ap, &qc->tf);
2883         ireason = qc->tf.nsect;
2884         bc_lo = qc->tf.lbam;
2885         bc_hi = qc->tf.lbah;
2886         bytes = (bc_hi << 8) | bc_lo;
2887
2888         /* shall be cleared to zero, indicating xfer of data */
2889         if (ireason & (1 << 0))
2890                 goto err_out;
2891
2892         /* make sure transfer direction matches expected */
2893         i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2894         if (do_write != i_write)
2895                 goto err_out;
2896
2897         __atapi_pio_bytes(qc, bytes);
2898
2899         return;
2900
2901 err_out:
2902         printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2903               ap->id, dev->devno);
2904         ap->hsm_task_state = HSM_ST_ERR;
2905 }
2906
2907 /**
2908  *      ata_pio_sector -
2909  *      @ap:
2910  *
2911  *      LOCKING:
2912  *      None.  (executing in kernel thread context)
2913  */
2914
2915 static void ata_pio_block(struct ata_port *ap)
2916 {
2917         struct ata_queued_cmd *qc;
2918         u8 status;
2919
2920         /*
2921          * This is purely hueristic.  This is a fast path.
2922          * Sometimes when we enter, BSY will be cleared in
2923          * a chk-status or two.  If not, the drive is probably seeking
2924          * or something.  Snooze for a couple msecs, then
2925          * chk-status again.  If still busy, fall back to
2926          * HSM_ST_POLL state.
2927          */
2928         status = ata_busy_wait(ap, ATA_BUSY, 5);
2929         if (status & ATA_BUSY) {
2930                 msleep(2);
2931                 status = ata_busy_wait(ap, ATA_BUSY, 10);
2932                 if (status & ATA_BUSY) {
2933                         ap->hsm_task_state = HSM_ST_POLL;
2934                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2935                         return;
2936                 }
2937         }
2938
2939         qc = ata_qc_from_tag(ap, ap->active_tag);
2940         assert(qc != NULL);
2941
2942         if (is_atapi_taskfile(&qc->tf)) {
2943                 /* no more data to transfer or unsupported ATAPI command */
2944                 if ((status & ATA_DRQ) == 0) {
2945                         ap->hsm_task_state = HSM_ST_LAST;
2946                         return;
2947                 }
2948
2949                 atapi_pio_bytes(qc);
2950         } else {
2951                 /* handle BSY=0, DRQ=0 as error */
2952                 if ((status & ATA_DRQ) == 0) {
2953                         ap->hsm_task_state = HSM_ST_ERR;
2954                         return;
2955                 }
2956
2957                 ata_pio_sector(qc);
2958         }
2959 }
2960
2961 static void ata_pio_error(struct ata_port *ap)
2962 {
2963         struct ata_queued_cmd *qc;
2964         u8 drv_stat;
2965
2966         qc = ata_qc_from_tag(ap, ap->active_tag);
2967         assert(qc != NULL);
2968
2969         drv_stat = ata_chk_status(ap);
2970         printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2971                ap->id, drv_stat);
2972
2973         ap->hsm_task_state = HSM_ST_IDLE;
2974
2975         ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2976 }
2977
2978 static void ata_pio_task(void *_data)
2979 {
2980         struct ata_port *ap = _data;
2981         unsigned long timeout;
2982         int qc_completed;
2983
2984 fsm_start:
2985         timeout = 0;
2986         qc_completed = 0;
2987
2988         switch (ap->hsm_task_state) {
2989         case HSM_ST_IDLE:
2990                 return;
2991
2992         case HSM_ST:
2993                 ata_pio_block(ap);
2994                 break;
2995
2996         case HSM_ST_LAST:
2997                 qc_completed = ata_pio_complete(ap);
2998                 break;
2999
3000         case HSM_ST_POLL:
3001         case HSM_ST_LAST_POLL:
3002                 timeout = ata_pio_poll(ap);
3003                 break;
3004
3005         case HSM_ST_TMOUT:
3006         case HSM_ST_ERR:
3007                 ata_pio_error(ap);
3008                 return;
3009         }
3010
3011         if (timeout)
3012                 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3013         else if (!qc_completed)
3014                 goto fsm_start;
3015 }
3016
3017 /**
3018  *      ata_qc_timeout - Handle timeout of queued command
3019  *      @qc: Command that timed out
3020  *
3021  *      Some part of the kernel (currently, only the SCSI layer)
3022  *      has noticed that the active command on port @ap has not
3023  *      completed after a specified length of time.  Handle this
3024  *      condition by disabling DMA (if necessary) and completing
3025  *      transactions, with error if necessary.
3026  *
3027  *      This also handles the case of the "lost interrupt", where
3028  *      for some reason (possibly hardware bug, possibly driver bug)
3029  *      an interrupt was not delivered to the driver, even though the
3030  *      transaction completed successfully.
3031  *
3032  *      LOCKING:
3033  *      Inherited from SCSI layer (none, can sleep)
3034  */
3035
3036 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3037 {
3038         struct ata_port *ap = qc->ap;
3039         struct ata_host_set *host_set = ap->host_set;
3040         struct ata_device *dev = qc->dev;
3041         u8 host_stat = 0, drv_stat;
3042         unsigned long flags;
3043
3044         DPRINTK("ENTER\n");
3045
3046         /* FIXME: doesn't this conflict with timeout handling? */
3047         if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3048                 struct scsi_cmnd *cmd = qc->scsicmd;
3049
3050                 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3051
3052                         /* finish completing original command */
3053                         spin_lock_irqsave(&host_set->lock, flags);
3054                         __ata_qc_complete(qc);
3055                         spin_unlock_irqrestore(&host_set->lock, flags);
3056
3057                         atapi_request_sense(ap, dev, cmd);
3058
3059                         cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3060                         scsi_finish_command(cmd);
3061
3062                         goto out;
3063                 }
3064         }
3065
3066         spin_lock_irqsave(&host_set->lock, flags);
3067
3068         /* hack alert!  We cannot use the supplied completion
3069          * function from inside the ->eh_strategy_handler() thread.
3070          * libata is the only user of ->eh_strategy_handler() in
3071          * any kernel, so the default scsi_done() assumes it is
3072          * not being called from the SCSI EH.
3073          */
3074         qc->scsidone = scsi_finish_command;
3075
3076         switch (qc->tf.protocol) {
3077
3078         case ATA_PROT_DMA:
3079         case ATA_PROT_ATAPI_DMA:
3080                 host_stat = ap->ops->bmdma_status(ap);
3081
3082                 /* before we do anything else, clear DMA-Start bit */
3083                 ap->ops->bmdma_stop(qc);
3084
3085                 /* fall through */
3086
3087         default:
3088                 ata_altstatus(ap);
3089                 drv_stat = ata_chk_status(ap);
3090
3091                 /* ack bmdma irq events */
3092                 ap->ops->irq_clear(ap);
3093
3094                 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3095                        ap->id, qc->tf.command, drv_stat, host_stat);
3096
3097                 /* complete taskfile transaction */
3098                 ata_qc_complete(qc, drv_stat);
3099                 break;
3100         }
3101
3102         spin_unlock_irqrestore(&host_set->lock, flags);
3103
3104 out:
3105         DPRINTK("EXIT\n");
3106 }
3107
3108 /**
3109  *      ata_eng_timeout - Handle timeout of queued command
3110  *      @ap: Port on which timed-out command is active
3111  *
3112  *      Some part of the kernel (currently, only the SCSI layer)
3113  *      has noticed that the active command on port @ap has not
3114  *      completed after a specified length of time.  Handle this
3115  *      condition by disabling DMA (if necessary) and completing
3116  *      transactions, with error if necessary.
3117  *
3118  *      This also handles the case of the "lost interrupt", where
3119  *      for some reason (possibly hardware bug, possibly driver bug)
3120  *      an interrupt was not delivered to the driver, even though the
3121  *      transaction completed successfully.
3122  *
3123  *      LOCKING:
3124  *      Inherited from SCSI layer (none, can sleep)
3125  */
3126
3127 void ata_eng_timeout(struct ata_port *ap)
3128 {
3129         struct ata_queued_cmd *qc;
3130
3131         DPRINTK("ENTER\n");
3132
3133         qc = ata_qc_from_tag(ap, ap->active_tag);
3134         if (qc)
3135                 ata_qc_timeout(qc);
3136         else {
3137                 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3138                        ap->id);
3139                 goto out;
3140         }
3141
3142 out:
3143         DPRINTK("EXIT\n");
3144 }
3145
3146 /**
3147  *      ata_qc_new - Request an available ATA command, for queueing
3148  *      @ap: Port associated with device @dev
3149  *      @dev: Device from whom we request an available command structure
3150  *
3151  *      LOCKING:
3152  *      None.
3153  */
3154
3155 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3156 {
3157         struct ata_queued_cmd *qc = NULL;
3158         unsigned int i;
3159
3160         for (i = 0; i < ATA_MAX_QUEUE; i++)
3161                 if (!test_and_set_bit(i, &ap->qactive)) {
3162                         qc = ata_qc_from_tag(ap, i);
3163                         break;
3164                 }
3165
3166         if (qc)
3167                 qc->tag = i;
3168
3169         return qc;
3170 }
3171
3172 /**
3173  *      ata_qc_new_init - Request an available ATA command, and initialize it
3174  *      @ap: Port associated with device @dev
3175  *      @dev: Device from whom we request an available command structure
3176  *
3177  *      LOCKING:
3178  *      None.
3179  */
3180
3181 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3182                                       struct ata_device *dev)
3183 {
3184         struct ata_queued_cmd *qc;
3185
3186         qc = ata_qc_new(ap);
3187         if (qc) {
3188                 qc->sg = NULL;
3189                 qc->flags = 0;
3190                 qc->scsicmd = NULL;
3191                 qc->ap = ap;
3192                 qc->dev = dev;
3193                 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3194                 qc->nsect = 0;
3195                 qc->nbytes = qc->curbytes = 0;
3196
3197                 ata_tf_init(ap, &qc->tf, dev->devno);
3198
3199                 if (dev->flags & ATA_DFLAG_LBA) {
3200                         qc->tf.flags |= ATA_TFLAG_LBA;
3201
3202                         if (dev->flags & ATA_DFLAG_LBA48)
3203                                 qc->tf.flags |= ATA_TFLAG_LBA48;
3204                 }
3205         }
3206
3207         return qc;
3208 }
3209
3210 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3211 {
3212         return 0;
3213 }
3214
3215 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3216 {
3217         struct ata_port *ap = qc->ap;
3218         unsigned int tag, do_clear = 0;
3219
3220         qc->flags = 0;
3221         tag = qc->tag;
3222         if (likely(ata_tag_valid(tag))) {
3223                 if (tag == ap->active_tag)
3224                         ap->active_tag = ATA_TAG_POISON;
3225                 qc->tag = ATA_TAG_POISON;
3226                 do_clear = 1;
3227         }
3228
3229         if (qc->waiting) {
3230                 struct completion *waiting = qc->waiting;
3231                 qc->waiting = NULL;
3232                 complete(waiting);
3233         }
3234
3235         if (likely(do_clear))
3236                 clear_bit(tag, &ap->qactive);
3237 }
3238
3239 /**
3240  *      ata_qc_free - free unused ata_queued_cmd
3241  *      @qc: Command to complete
3242  *
3243  *      Designed to free unused ata_queued_cmd object
3244  *      in case something prevents using it.
3245  *
3246  *      LOCKING:
3247  *      spin_lock_irqsave(host_set lock)
3248  *
3249  */
3250 void ata_qc_free(struct ata_queued_cmd *qc)
3251 {
3252         assert(qc != NULL);     /* ata_qc_from_tag _might_ return NULL */
3253         assert(qc->waiting == NULL);    /* nothing should be waiting */
3254
3255         __ata_qc_complete(qc);
3256 }
3257
3258 /**
3259  *      ata_qc_complete - Complete an active ATA command
3260  *      @qc: Command to complete
3261  *      @drv_stat: ATA Status register contents
3262  *
3263  *      Indicate to the mid and upper layers that an ATA
3264  *      command has completed, with either an ok or not-ok status.
3265  *
3266  *      LOCKING:
3267  *      spin_lock_irqsave(host_set lock)
3268  *
3269  */
3270
3271 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3272 {
3273         int rc;
3274
3275         assert(qc != NULL);     /* ata_qc_from_tag _might_ return NULL */
3276         assert(qc->flags & ATA_QCFLAG_ACTIVE);
3277
3278         if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3279                 ata_sg_clean(qc);
3280
3281         /* atapi: mark qc as inactive to prevent the interrupt handler
3282          * from completing the command twice later, before the error handler
3283          * is called. (when rc != 0 and atapi request sense is needed)
3284          */
3285         qc->flags &= ~ATA_QCFLAG_ACTIVE;
3286
3287         /* call completion callback */
3288         rc = qc->complete_fn(qc, drv_stat);
3289
3290         /* if callback indicates not to complete command (non-zero),
3291          * return immediately
3292          */
3293         if (rc != 0)
3294                 return;
3295
3296         __ata_qc_complete(qc);
3297
3298         VPRINTK("EXIT\n");
3299 }
3300
3301 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3302 {
3303         struct ata_port *ap = qc->ap;
3304
3305         switch (qc->tf.protocol) {
3306         case ATA_PROT_DMA:
3307         case ATA_PROT_ATAPI_DMA:
3308                 return 1;
3309
3310         case ATA_PROT_ATAPI:
3311         case ATA_PROT_PIO:
3312         case ATA_PROT_PIO_MULT:
3313                 if (ap->flags & ATA_FLAG_PIO_DMA)
3314                         return 1;
3315
3316                 /* fall through */
3317
3318         default:
3319                 return 0;
3320         }
3321
3322         /* never reached */
3323 }
3324
3325 /**
3326  *      ata_qc_issue - issue taskfile to device
3327  *      @qc: command to issue to device
3328  *
3329  *      Prepare an ATA command to submission to device.
3330  *      This includes mapping the data into a DMA-able
3331  *      area, filling in the S/G table, and finally
3332  *      writing the taskfile to hardware, starting the command.
3333  *
3334  *      LOCKING:
3335  *      spin_lock_irqsave(host_set lock)
3336  *
3337  *      RETURNS:
3338  *      Zero on success, negative on error.
3339  */
3340
3341 int ata_qc_issue(struct ata_queued_cmd *qc)
3342 {
3343         struct ata_port *ap = qc->ap;
3344
3345         if (ata_should_dma_map(qc)) {
3346                 if (qc->flags & ATA_QCFLAG_SG) {
3347                         if (ata_sg_setup(qc))
3348                                 goto err_out;
3349                 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3350                         if (ata_sg_setup_one(qc))
3351                                 goto err_out;
3352                 }
3353         } else {
3354                 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3355         }
3356
3357         ap->ops->qc_prep(qc);
3358
3359         qc->ap->active_tag = qc->tag;
3360         qc->flags |= ATA_QCFLAG_ACTIVE;
3361
3362         return ap->ops->qc_issue(qc);
3363
3364 err_out:
3365         return -1;
3366 }
3367
3368
3369 /**
3370  *      ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3371  *      @qc: command to issue to device
3372  *
3373  *      Using various libata functions and hooks, this function
3374  *      starts an ATA command.  ATA commands are grouped into
3375  *      classes called "protocols", and issuing each type of protocol
3376  *      is slightly different.
3377  *
3378  *      May be used as the qc_issue() entry in ata_port_operations.
3379  *
3380  *      LOCKING:
3381  *      spin_lock_irqsave(host_set lock)
3382  *
3383  *      RETURNS:
3384  *      Zero on success, negative on error.
3385  */
3386
3387 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3388 {
3389         struct ata_port *ap = qc->ap;
3390
3391         ata_dev_select(ap, qc->dev->devno, 1, 0);
3392
3393         switch (qc->tf.protocol) {
3394         case ATA_PROT_NODATA:
3395                 ata_tf_to_host_nolock(ap, &qc->tf);
3396                 break;
3397
3398         case ATA_PROT_DMA:
3399                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
3400                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
3401                 ap->ops->bmdma_start(qc);           /* initiate bmdma */
3402                 break;
3403
3404         case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3405                 ata_qc_set_polling(qc);
3406                 ata_tf_to_host_nolock(ap, &qc->tf);
3407                 ap->hsm_task_state = HSM_ST;
3408                 queue_work(ata_wq, &ap->pio_task);
3409                 break;
3410
3411         case ATA_PROT_ATAPI:
3412                 ata_qc_set_polling(qc);
3413                 ata_tf_to_host_nolock(ap, &qc->tf);
3414                 queue_work(ata_wq, &ap->packet_task);
3415                 break;
3416
3417         case ATA_PROT_ATAPI_NODATA:
3418                 ap->flags |= ATA_FLAG_NOINTR;
3419                 ata_tf_to_host_nolock(ap, &qc->tf);
3420                 queue_work(ata_wq, &ap->packet_task);
3421                 break;
3422
3423         case ATA_PROT_ATAPI_DMA:
3424                 ap->flags |= ATA_FLAG_NOINTR;
3425                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
3426                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
3427                 queue_work(ata_wq, &ap->packet_task);
3428                 break;
3429
3430         default:
3431                 WARN_ON(1);
3432                 return -1;
3433         }
3434
3435         return 0;
3436 }
3437
3438 /**
3439  *      ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3440  *      @qc: Info associated with this ATA transaction.
3441  *
3442  *      LOCKING:
3443  *      spin_lock_irqsave(host_set lock)
3444  */
3445
3446 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3447 {
3448         struct ata_port *ap = qc->ap;
3449         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3450         u8 dmactl;
3451         void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3452
3453         /* load PRD table addr. */
3454         mb();   /* make sure PRD table writes are visible to controller */
3455         writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3456
3457         /* specify data direction, triple-check start bit is clear */
3458         dmactl = readb(mmio + ATA_DMA_CMD);
3459         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3460         if (!rw)
3461                 dmactl |= ATA_DMA_WR;
3462         writeb(dmactl, mmio + ATA_DMA_CMD);
3463
3464         /* issue r/w command */
3465         ap->ops->exec_command(ap, &qc->tf);
3466 }
3467
3468 /**
3469  *      ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3470  *      @qc: Info associated with this ATA transaction.
3471  *
3472  *      LOCKING:
3473  *      spin_lock_irqsave(host_set lock)
3474  */
3475
3476 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3477 {
3478         struct ata_port *ap = qc->ap;
3479         void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3480         u8 dmactl;
3481
3482         /* start host DMA transaction */
3483         dmactl = readb(mmio + ATA_DMA_CMD);
3484         writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3485
3486         /* Strictly, one may wish to issue a readb() here, to
3487          * flush the mmio write.  However, control also passes
3488          * to the hardware at this point, and it will interrupt
3489          * us when we are to resume control.  So, in effect,
3490          * we don't care when the mmio write flushes.
3491          * Further, a read of the DMA status register _immediately_
3492          * following the write may not be what certain flaky hardware
3493          * is expected, so I think it is best to not add a readb()
3494          * without first all the MMIO ATA cards/mobos.
3495          * Or maybe I'm just being paranoid.
3496          */
3497 }
3498
3499 /**
3500  *      ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3501  *      @qc: Info associated with this ATA transaction.
3502  *
3503  *      LOCKING:
3504  *      spin_lock_irqsave(host_set lock)
3505  */
3506
3507 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3508 {
3509         struct ata_port *ap = qc->ap;
3510         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3511         u8 dmactl;
3512
3513         /* load PRD table addr. */
3514         outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3515
3516         /* specify data direction, triple-check start bit is clear */
3517         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3518         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3519         if (!rw)
3520                 dmactl |= ATA_DMA_WR;
3521         outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3522
3523         /* issue r/w command */
3524         ap->ops->exec_command(ap, &qc->tf);
3525 }
3526
3527 /**
3528  *      ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3529  *      @qc: Info associated with this ATA transaction.
3530  *
3531  *      LOCKING:
3532  *      spin_lock_irqsave(host_set lock)
3533  */
3534
3535 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3536 {
3537         struct ata_port *ap = qc->ap;
3538         u8 dmactl;
3539
3540         /* start host DMA transaction */
3541         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3542         outb(dmactl | ATA_DMA_START,
3543              ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3544 }
3545
3546
3547 /**
3548  *      ata_bmdma_start - Start a PCI IDE BMDMA transaction
3549  *      @qc: Info associated with this ATA transaction.
3550  *
3551  *      Writes the ATA_DMA_START flag to the DMA command register.
3552  *
3553  *      May be used as the bmdma_start() entry in ata_port_operations.
3554  *
3555  *      LOCKING:
3556  *      spin_lock_irqsave(host_set lock)
3557  */
3558 void ata_bmdma_start(struct ata_queued_cmd *qc)
3559 {
3560         if (qc->ap->flags & ATA_FLAG_MMIO)
3561                 ata_bmdma_start_mmio(qc);
3562         else
3563                 ata_bmdma_start_pio(qc);
3564 }
3565
3566
3567 /**
3568  *      ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3569  *      @qc: Info associated with this ATA transaction.
3570  *
3571  *      Writes address of PRD table to device's PRD Table Address
3572  *      register, sets the DMA control register, and calls
3573  *      ops->exec_command() to start the transfer.
3574  *
3575  *      May be used as the bmdma_setup() entry in ata_port_operations.
3576  *
3577  *      LOCKING:
3578  *      spin_lock_irqsave(host_set lock)
3579  */
3580 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3581 {
3582         if (qc->ap->flags & ATA_FLAG_MMIO)
3583                 ata_bmdma_setup_mmio(qc);
3584         else
3585                 ata_bmdma_setup_pio(qc);
3586 }
3587
3588
3589 /**
3590  *      ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3591  *      @ap: Port associated with this ATA transaction.
3592  *
3593  *      Clear interrupt and error flags in DMA status register.
3594  *
3595  *      May be used as the irq_clear() entry in ata_port_operations.
3596  *
3597  *      LOCKING:
3598  *      spin_lock_irqsave(host_set lock)
3599  */
3600
3601 void ata_bmdma_irq_clear(struct ata_port *ap)
3602 {
3603     if (ap->flags & ATA_FLAG_MMIO) {
3604         void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3605         writeb(readb(mmio), mmio);
3606     } else {
3607         unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3608         outb(inb(addr), addr);
3609     }
3610
3611 }
3612
3613
3614 /**
3615  *      ata_bmdma_status - Read PCI IDE BMDMA status
3616  *      @ap: Port associated with this ATA transaction.
3617  *
3618  *      Read and return BMDMA status register.
3619  *
3620  *      May be used as the bmdma_status() entry in ata_port_operations.
3621  *
3622  *      LOCKING:
3623  *      spin_lock_irqsave(host_set lock)
3624  */
3625
3626 u8 ata_bmdma_status(struct ata_port *ap)
3627 {
3628         u8 host_stat;
3629         if (ap->flags & ATA_FLAG_MMIO) {
3630                 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3631                 host_stat = readb(mmio + ATA_DMA_STATUS);
3632         } else
3633                 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3634         return host_stat;
3635 }
3636
3637
3638 /**
3639  *      ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3640  *      @qc: Command we are ending DMA for
3641  *
3642  *      Clears the ATA_DMA_START flag in the dma control register
3643  *
3644  *      May be used as the bmdma_stop() entry in ata_port_operations.
3645  *
3646  *      LOCKING:
3647  *      spin_lock_irqsave(host_set lock)
3648  */
3649
3650 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3651 {
3652         struct ata_port *ap = qc->ap;
3653         if (ap->flags & ATA_FLAG_MMIO) {
3654                 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3655
3656                 /* clear start/stop bit */
3657                 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3658                         mmio + ATA_DMA_CMD);
3659         } else {
3660                 /* clear start/stop bit */
3661                 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3662                         ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3663         }
3664
3665         /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3666         ata_altstatus(ap);        /* dummy read */
3667 }
3668
3669 /**
3670  *      ata_host_intr - Handle host interrupt for given (port, task)
3671  *      @ap: Port on which interrupt arrived (possibly...)
3672  *      @qc: Taskfile currently active in engine
3673  *
3674  *      Handle host interrupt for given queued command.  Currently,
3675  *      only DMA interrupts are handled.  All other commands are
3676  *      handled via polling with interrupts disabled (nIEN bit).
3677  *
3678  *      LOCKING:
3679  *      spin_lock_irqsave(host_set lock)
3680  *
3681  *      RETURNS:
3682  *      One if interrupt was handled, zero if not (shared irq).
3683  */
3684
3685 inline unsigned int ata_host_intr (struct ata_port *ap,
3686                                    struct ata_queued_cmd *qc)
3687 {
3688         u8 status, host_stat;
3689
3690         switch (qc->tf.protocol) {
3691
3692         case ATA_PROT_DMA:
3693         case ATA_PROT_ATAPI_DMA:
3694         case ATA_PROT_ATAPI:
3695                 /* check status of DMA engine */
3696                 host_stat = ap->ops->bmdma_status(ap);
3697                 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3698
3699                 /* if it's not our irq... */
3700                 if (!(host_stat & ATA_DMA_INTR))
3701                         goto idle_irq;
3702
3703                 /* before we do anything else, clear DMA-Start bit */
3704                 ap->ops->bmdma_stop(qc);
3705
3706                 /* fall through */
3707
3708         case ATA_PROT_ATAPI_NODATA:
3709         case ATA_PROT_NODATA:
3710                 /* check altstatus */
3711                 status = ata_altstatus(ap);
3712                 if (status & ATA_BUSY)
3713                         goto idle_irq;
3714
3715                 /* check main status, clearing INTRQ */
3716                 status = ata_chk_status(ap);
3717                 if (unlikely(status & ATA_BUSY))
3718                         goto idle_irq;
3719                 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3720                         ap->id, qc->tf.protocol, status);
3721
3722                 /* ack bmdma irq events */
3723                 ap->ops->irq_clear(ap);
3724
3725                 /* complete taskfile transaction */
3726                 ata_qc_complete(qc, status);
3727                 break;
3728
3729         default:
3730                 goto idle_irq;
3731         }
3732
3733         return 1;       /* irq handled */
3734
3735 idle_irq:
3736         ap->stats.idle_irq++;
3737
3738 #ifdef ATA_IRQ_TRAP
3739         if ((ap->stats.idle_irq % 1000) == 0) {
3740                 handled = 1;
3741                 ata_irq_ack(ap, 0); /* debug trap */
3742                 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3743         }
3744 #endif
3745         return 0;       /* irq not handled */
3746 }
3747
3748 /**
3749  *      ata_interrupt - Default ATA host interrupt handler
3750  *      @irq: irq line (unused)
3751  *      @dev_instance: pointer to our ata_host_set information structure
3752  *      @regs: unused
3753  *
3754  *      Default interrupt handler for PCI IDE devices.  Calls
3755  *      ata_host_intr() for each port that is not disabled.
3756  *
3757  *      LOCKING:
3758  *      Obtains host_set lock during operation.
3759  *
3760  *      RETURNS:
3761  *      IRQ_NONE or IRQ_HANDLED.
3762  *
3763  */
3764
3765 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3766 {
3767         struct ata_host_set *host_set = dev_instance;
3768         unsigned int i;
3769         unsigned int handled = 0;
3770         unsigned long flags;
3771
3772         /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3773         spin_lock_irqsave(&host_set->lock, flags);
3774
3775         for (i = 0; i < host_set->n_ports; i++) {
3776                 struct ata_port *ap;
3777
3778                 ap = host_set->ports[i];
3779                 if (ap &&
3780                     !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3781                         struct ata_queued_cmd *qc;
3782
3783                         qc = ata_qc_from_tag(ap, ap->active_tag);
3784                         if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3785                             (qc->flags & ATA_QCFLAG_ACTIVE))
3786                                 handled |= ata_host_intr(ap, qc);
3787                 }
3788         }
3789
3790         spin_unlock_irqrestore(&host_set->lock, flags);
3791
3792         return IRQ_RETVAL(handled);
3793 }
3794
3795 /**
3796  *      atapi_packet_task - Write CDB bytes to hardware
3797  *      @_data: Port to which ATAPI device is attached.
3798  *
3799  *      When device has indicated its readiness to accept
3800  *      a CDB, this function is called.  Send the CDB.
3801  *      If DMA is to be performed, exit immediately.
3802  *      Otherwise, we are in polling mode, so poll
3803  *      status under operation succeeds or fails.
3804  *
3805  *      LOCKING:
3806  *      Kernel thread context (may sleep)
3807  */
3808
3809 static void atapi_packet_task(void *_data)
3810 {
3811         struct ata_port *ap = _data;
3812         struct ata_queued_cmd *qc;
3813         u8 status;
3814
3815         qc = ata_qc_from_tag(ap, ap->active_tag);
3816         assert(qc != NULL);
3817         assert(qc->flags & ATA_QCFLAG_ACTIVE);
3818
3819         /* sleep-wait for BSY to clear */
3820         DPRINTK("busy wait\n");
3821         if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3822                 goto err_out;
3823
3824         /* make sure DRQ is set */
3825         status = ata_chk_status(ap);
3826         if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3827                 goto err_out;
3828
3829         /* send SCSI cdb */
3830         DPRINTK("send cdb\n");
3831         assert(ap->cdb_len >= 12);
3832
3833         if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3834             qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3835                 unsigned long flags;
3836
3837                 /* Once we're done issuing command and kicking bmdma,
3838                  * irq handler takes over.  To not lose irq, we need
3839                  * to clear NOINTR flag before sending cdb, but
3840                  * interrupt handler shouldn't be invoked before we're
3841                  * finished.  Hence, the following locking.
3842                  */
3843                 spin_lock_irqsave(&ap->host_set->lock, flags);
3844                 ap->flags &= ~ATA_FLAG_NOINTR;
3845                 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3846                 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3847                         ap->ops->bmdma_start(qc);       /* initiate bmdma */
3848                 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3849         } else {
3850                 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3851
3852                 /* PIO commands are handled by polling */
3853                 ap->hsm_task_state = HSM_ST;
3854                 queue_work(ata_wq, &ap->pio_task);
3855         }
3856
3857         return;
3858
3859 err_out:
3860         ata_poll_qc_complete(qc, ATA_ERR);
3861 }
3862
3863
3864 /**
3865  *      ata_port_start - Set port up for dma.
3866  *      @ap: Port to initialize
3867  *
3868  *      Called just after data structures for each port are
3869  *      initialized.  Allocates space for PRD table.
3870  *
3871  *      May be used as the port_start() entry in ata_port_operations.
3872  *
3873  *      LOCKING:
3874  */
3875
3876 int ata_port_start (struct ata_port *ap)
3877 {
3878         struct device *dev = ap->host_set->dev;
3879
3880         ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3881         if (!ap->prd)
3882                 return -ENOMEM;
3883
3884         DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3885
3886         return 0;
3887 }
3888
3889
3890 /**
3891  *      ata_port_stop - Undo ata_port_start()
3892  *      @ap: Port to shut down
3893  *
3894  *      Frees the PRD table.
3895  *
3896  *      May be used as the port_stop() entry in ata_port_operations.
3897  *
3898  *      LOCKING:
3899  */
3900
3901 void ata_port_stop (struct ata_port *ap)
3902 {
3903         struct device *dev = ap->host_set->dev;
3904
3905         dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3906 }
3907
3908 void ata_host_stop (struct ata_host_set *host_set)
3909 {
3910         if (host_set->mmio_base)
3911                 iounmap(host_set->mmio_base);
3912 }
3913
3914
3915 /**
3916  *      ata_host_remove - Unregister SCSI host structure with upper layers
3917  *      @ap: Port to unregister
3918  *      @do_unregister: 1 if we fully unregister, 0 to just stop the port
3919  *
3920  *      LOCKING:
3921  */
3922
3923 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3924 {
3925         struct Scsi_Host *sh = ap->host;
3926
3927         DPRINTK("ENTER\n");
3928
3929         if (do_unregister)
3930                 scsi_remove_host(sh);
3931
3932         ap->ops->port_stop(ap);
3933 }
3934
3935 /**
3936  *      ata_host_init - Initialize an ata_port structure
3937  *      @ap: Structure to initialize
3938  *      @host: associated SCSI mid-layer structure
3939  *      @host_set: Collection of hosts to which @ap belongs
3940  *      @ent: Probe information provided by low-level driver
3941  *      @port_no: Port number associated with this ata_port
3942  *
3943  *      Initialize a new ata_port structure, and its associated
3944  *      scsi_host.
3945  *
3946  *      LOCKING:
3947  *      Inherited from caller.
3948  *
3949  */
3950
3951 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3952                           struct ata_host_set *host_set,
3953                           struct ata_probe_ent *ent, unsigned int port_no)
3954 {
3955         unsigned int i;
3956
3957         host->max_id = 16;
3958         host->max_lun = 1;
3959         host->max_channel = 1;
3960         host->unique_id = ata_unique_id++;
3961         host->max_cmd_len = 12;
3962
3963         scsi_assign_lock(host, &host_set->lock);
3964
3965         ap->flags = ATA_FLAG_PORT_DISABLED;
3966         ap->id = host->unique_id;
3967         ap->host = host;
3968         ap->ctl = ATA_DEVCTL_OBS;
3969         ap->host_set = host_set;
3970         ap->port_no = port_no;
3971         ap->hard_port_no =
3972                 ent->legacy_mode ? ent->hard_port_no : port_no;
3973         ap->pio_mask = ent->pio_mask;
3974         ap->mwdma_mask = ent->mwdma_mask;
3975         ap->udma_mask = ent->udma_mask;
3976         ap->flags |= ent->host_flags;
3977         ap->ops = ent->port_ops;
3978         ap->cbl = ATA_CBL_NONE;
3979         ap->active_tag = ATA_TAG_POISON;
3980         ap->last_ctl = 0xFF;
3981
3982         INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
3983         INIT_WORK(&ap->pio_task, ata_pio_task, ap);
3984
3985         for (i = 0; i < ATA_MAX_DEVICES; i++)
3986                 ap->device[i].devno = i;
3987
3988 #ifdef ATA_IRQ_TRAP
3989         ap->stats.unhandled_irq = 1;
3990         ap->stats.idle_irq = 1;
3991 #endif
3992
3993         memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
3994 }
3995
3996 /**
3997  *      ata_host_add - Attach low-level ATA driver to system
3998  *      @ent: Information provided by low-level driver
3999  *      @host_set: Collections of ports to which we add
4000  *      @port_no: Port number associated with this host
4001  *
4002  *      Attach low-level ATA driver to system.
4003  *
4004  *      LOCKING:
4005  *      PCI/etc. bus probe sem.
4006  *
4007  *      RETURNS:
4008  *      New ata_port on success, for NULL on error.
4009  *
4010  */
4011
4012 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4013                                       struct ata_host_set *host_set,
4014                                       unsigned int port_no)
4015 {
4016         struct Scsi_Host *host;
4017         struct ata_port *ap;
4018         int rc;
4019
4020         DPRINTK("ENTER\n");
4021         host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4022         if (!host)
4023                 return NULL;
4024
4025         ap = (struct ata_port *) &host->hostdata[0];
4026
4027         ata_host_init(ap, host, host_set, ent, port_no);
4028
4029         rc = ap->ops->port_start(ap);
4030         if (rc)
4031                 goto err_out;
4032
4033         return ap;
4034
4035 err_out:
4036         scsi_host_put(host);
4037         return NULL;
4038 }
4039
4040 /**
4041  *      ata_device_add - Register hardware device with ATA and SCSI layers
4042  *      @ent: Probe information describing hardware device to be registered
4043  *
4044  *      This function processes the information provided in the probe
4045  *      information struct @ent, allocates the necessary ATA and SCSI
4046  *      host information structures, initializes them, and registers
4047  *      everything with requisite kernel subsystems.
4048  *
4049  *      This function requests irqs, probes the ATA bus, and probes
4050  *      the SCSI bus.
4051  *
4052  *      LOCKING:
4053  *      PCI/etc. bus probe sem.
4054  *
4055  *      RETURNS:
4056  *      Number of ports registered.  Zero on error (no ports registered).
4057  *
4058  */
4059
4060 int ata_device_add(struct ata_probe_ent *ent)
4061 {
4062         unsigned int count = 0, i;
4063         struct device *dev = ent->dev;
4064         struct ata_host_set *host_set;
4065
4066         DPRINTK("ENTER\n");
4067         /* alloc a container for our list of ATA ports (buses) */
4068         host_set = kmalloc(sizeof(struct ata_host_set) +
4069                            (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4070         if (!host_set)
4071                 return 0;
4072         memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4073         spin_lock_init(&host_set->lock);
4074
4075         host_set->dev = dev;
4076         host_set->n_ports = ent->n_ports;
4077         host_set->irq = ent->irq;
4078         host_set->mmio_base = ent->mmio_base;
4079         host_set->private_data = ent->private_data;
4080         host_set->ops = ent->port_ops;
4081
4082         /* register each port bound to this device */
4083         for (i = 0; i < ent->n_ports; i++) {
4084                 struct ata_port *ap;
4085                 unsigned long xfer_mode_mask;
4086
4087                 ap = ata_host_add(ent, host_set, i);
4088                 if (!ap)
4089                         goto err_out;
4090
4091                 host_set->ports[i] = ap;
4092                 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4093                                 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4094                                 (ap->pio_mask << ATA_SHIFT_PIO);
4095
4096                 /* print per-port info to dmesg */
4097                 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4098                                  "bmdma 0x%lX irq %lu\n",
4099                         ap->id,
4100                         ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4101                         ata_mode_string(xfer_mode_mask),
4102                         ap->ioaddr.cmd_addr,
4103                         ap->ioaddr.ctl_addr,
4104                         ap->ioaddr.bmdma_addr,
4105                         ent->irq);
4106
4107                 ata_chk_status(ap);
4108                 host_set->ops->irq_clear(ap);
4109                 count++;
4110         }
4111
4112         if (!count) {
4113                 kfree(host_set);
4114                 return 0;
4115         }
4116
4117         /* obtain irq, that is shared between channels */
4118         if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4119                         DRV_NAME, host_set))
4120                 goto err_out;
4121
4122         /* perform each probe synchronously */
4123         DPRINTK("probe begin\n");
4124         for (i = 0; i < count; i++) {
4125                 struct ata_port *ap;
4126                 int rc;
4127
4128                 ap = host_set->ports[i];
4129
4130                 DPRINTK("ata%u: probe begin\n", ap->id);
4131                 rc = ata_bus_probe(ap);
4132                 DPRINTK("ata%u: probe end\n", ap->id);
4133
4134                 if (rc) {
4135                         /* FIXME: do something useful here?
4136                          * Current libata behavior will
4137                          * tear down everything when
4138                          * the module is removed
4139                          * or the h/w is unplugged.
4140                          */
4141                 }
4142
4143                 rc = scsi_add_host(ap->host, dev);
4144                 if (rc) {
4145                         printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4146                                ap->id);
4147                         /* FIXME: do something useful here */
4148                         /* FIXME: handle unconditional calls to
4149                          * scsi_scan_host and ata_host_remove, below,
4150                          * at the very least
4151                          */
4152                 }
4153         }
4154
4155         /* probes are done, now scan each port's disk(s) */
4156         DPRINTK("probe begin\n");
4157         for (i = 0; i < count; i++) {
4158                 struct ata_port *ap = host_set->ports[i];
4159
4160                 ata_scsi_scan_host(ap);
4161         }
4162
4163         dev_set_drvdata(dev, host_set);
4164
4165         VPRINTK("EXIT, returning %u\n", ent->n_ports);
4166         return ent->n_ports; /* success */
4167
4168 err_out:
4169         for (i = 0; i < count; i++) {
4170                 ata_host_remove(host_set->ports[i], 1);
4171                 scsi_host_put(host_set->ports[i]->host);
4172         }
4173         kfree(host_set);
4174         VPRINTK("EXIT, returning 0\n");
4175         return 0;
4176 }
4177
4178 /**
4179  *      ata_host_set_remove - PCI layer callback for device removal
4180  *      @host_set: ATA host set that was removed
4181  *
4182  *      Unregister all objects associated with this host set. Free those 
4183  *      objects.
4184  *
4185  *      LOCKING:
4186  *      Inherited from calling layer (may sleep).
4187  */
4188
4189
4190 void ata_host_set_remove(struct ata_host_set *host_set)
4191 {
4192         struct ata_port *ap;
4193         unsigned int i;
4194
4195         for (i = 0; i < host_set->n_ports; i++) {
4196                 ap = host_set->ports[i];
4197                 scsi_remove_host(ap->host);
4198         }
4199
4200         free_irq(host_set->irq, host_set);
4201
4202         for (i = 0; i < host_set->n_ports; i++) {
4203                 ap = host_set->ports[i];
4204
4205                 ata_scsi_release(ap->host);
4206
4207                 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4208                         struct ata_ioports *ioaddr = &ap->ioaddr;
4209
4210                         if (ioaddr->cmd_addr == 0x1f0)
4211                                 release_region(0x1f0, 8);
4212                         else if (ioaddr->cmd_addr == 0x170)
4213                                 release_region(0x170, 8);
4214                 }
4215
4216                 scsi_host_put(ap->host);
4217         }
4218
4219         if (host_set->ops->host_stop)
4220                 host_set->ops->host_stop(host_set);
4221
4222         kfree(host_set);
4223 }
4224
4225 /**
4226  *      ata_scsi_release - SCSI layer callback hook for host unload
4227  *      @host: libata host to be unloaded
4228  *
4229  *      Performs all duties necessary to shut down a libata port...
4230  *      Kill port kthread, disable port, and release resources.
4231  *
4232  *      LOCKING:
4233  *      Inherited from SCSI layer.
4234  *
4235  *      RETURNS:
4236  *      One.
4237  */
4238
4239 int ata_scsi_release(struct Scsi_Host *host)
4240 {
4241         struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4242
4243         DPRINTK("ENTER\n");
4244
4245         ap->ops->port_disable(ap);
4246         ata_host_remove(ap, 0);
4247
4248         DPRINTK("EXIT\n");
4249         return 1;
4250 }
4251
4252 /**
4253  *      ata_std_ports - initialize ioaddr with standard port offsets.
4254  *      @ioaddr: IO address structure to be initialized
4255  *
4256  *      Utility function which initializes data_addr, error_addr,
4257  *      feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4258  *      device_addr, status_addr, and command_addr to standard offsets
4259  *      relative to cmd_addr.
4260  *
4261  *      Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4262  */
4263
4264 void ata_std_ports(struct ata_ioports *ioaddr)
4265 {
4266         ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4267         ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4268         ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4269         ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4270         ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4271         ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4272         ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4273         ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4274         ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4275         ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4276 }
4277
4278 static struct ata_probe_ent *
4279 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4280 {
4281         struct ata_probe_ent *probe_ent;
4282
4283         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4284         if (!probe_ent) {
4285                 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4286                        kobject_name(&(dev->kobj)));
4287                 return NULL;
4288         }
4289
4290         memset(probe_ent, 0, sizeof(*probe_ent));
4291
4292         INIT_LIST_HEAD(&probe_ent->node);
4293         probe_ent->dev = dev;
4294
4295         probe_ent->sht = port->sht;
4296         probe_ent->host_flags = port->host_flags;
4297         probe_ent->pio_mask = port->pio_mask;
4298         probe_ent->mwdma_mask = port->mwdma_mask;
4299         probe_ent->udma_mask = port->udma_mask;
4300         probe_ent->port_ops = port->port_ops;
4301
4302         return probe_ent;
4303 }
4304
4305
4306
4307 #ifdef CONFIG_PCI
4308
4309 void ata_pci_host_stop (struct ata_host_set *host_set)
4310 {
4311         struct pci_dev *pdev = to_pci_dev(host_set->dev);
4312
4313         pci_iounmap(pdev, host_set->mmio_base);
4314 }
4315
4316 /**
4317  *      ata_pci_init_native_mode - Initialize native-mode driver
4318  *      @pdev:  pci device to be initialized
4319  *      @port:  array[2] of pointers to port info structures.
4320  *      @ports: bitmap of ports present
4321  *
4322  *      Utility function which allocates and initializes an
4323  *      ata_probe_ent structure for a standard dual-port
4324  *      PIO-based IDE controller.  The returned ata_probe_ent
4325  *      structure can be passed to ata_device_add().  The returned
4326  *      ata_probe_ent structure should then be freed with kfree().
4327  *
4328  *      The caller need only pass the address of the primary port, the
4329  *      secondary will be deduced automatically. If the device has non
4330  *      standard secondary port mappings this function can be called twice,
4331  *      once for each interface.
4332  */
4333
4334 struct ata_probe_ent *
4335 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4336 {
4337         struct ata_probe_ent *probe_ent =
4338                 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4339         int p = 0;
4340
4341         if (!probe_ent)
4342                 return NULL;
4343
4344         probe_ent->irq = pdev->irq;
4345         probe_ent->irq_flags = SA_SHIRQ;
4346
4347         if (ports & ATA_PORT_PRIMARY) {
4348                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4349                 probe_ent->port[p].altstatus_addr =
4350                 probe_ent->port[p].ctl_addr =
4351                         pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4352                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4353                 ata_std_ports(&probe_ent->port[p]);
4354                 p++;
4355         }
4356
4357         if (ports & ATA_PORT_SECONDARY) {
4358                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4359                 probe_ent->port[p].altstatus_addr =
4360                 probe_ent->port[p].ctl_addr =
4361                         pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4362                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4363                 ata_std_ports(&probe_ent->port[p]);
4364                 p++;
4365         }
4366
4367         probe_ent->n_ports = p;
4368         return probe_ent;
4369 }
4370
4371 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4372 {
4373         struct ata_probe_ent *probe_ent;
4374
4375         probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4376         if (!probe_ent)
4377                 return NULL;
4378
4379         
4380         probe_ent->legacy_mode = 1;
4381         probe_ent->n_ports = 1;
4382         probe_ent->hard_port_no = port_num;
4383
4384         switch(port_num)
4385         {
4386                 case 0:
4387                         probe_ent->irq = 14;
4388                         probe_ent->port[0].cmd_addr = 0x1f0;
4389                         probe_ent->port[0].altstatus_addr =
4390                         probe_ent->port[0].ctl_addr = 0x3f6;
4391                         break;
4392                 case 1:
4393                         probe_ent->irq = 15;
4394                         probe_ent->port[0].cmd_addr = 0x170;
4395                         probe_ent->port[0].altstatus_addr =
4396                         probe_ent->port[0].ctl_addr = 0x376;
4397                         break;
4398         }
4399         probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4400         ata_std_ports(&probe_ent->port[0]);
4401         return probe_ent;
4402 }
4403
4404 /**
4405  *      ata_pci_init_one - Initialize/register PCI IDE host controller
4406  *      @pdev: Controller to be initialized
4407  *      @port_info: Information from low-level host driver
4408  *      @n_ports: Number of ports attached to host controller
4409  *
4410  *      This is a helper function which can be called from a driver's
4411  *      xxx_init_one() probe function if the hardware uses traditional
4412  *      IDE taskfile registers.
4413  *
4414  *      This function calls pci_enable_device(), reserves its register
4415  *      regions, sets the dma mask, enables bus master mode, and calls
4416  *      ata_device_add()
4417  *
4418  *      LOCKING:
4419  *      Inherited from PCI layer (may sleep).
4420  *
4421  *      RETURNS:
4422  *      Zero on success, negative on errno-based value on error.
4423  *
4424  */
4425
4426 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4427                       unsigned int n_ports)
4428 {
4429         struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4430         struct ata_port_info *port[2];
4431         u8 tmp8, mask;
4432         unsigned int legacy_mode = 0;
4433         int disable_dev_on_err = 1;
4434         int rc;
4435
4436         DPRINTK("ENTER\n");
4437
4438         port[0] = port_info[0];
4439         if (n_ports > 1)
4440                 port[1] = port_info[1];
4441         else
4442                 port[1] = port[0];
4443
4444         if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4445             && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4446                 /* TODO: What if one channel is in native mode ... */
4447                 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4448                 mask = (1 << 2) | (1 << 0);
4449                 if ((tmp8 & mask) != mask)
4450                         legacy_mode = (1 << 3);
4451         }
4452
4453         /* FIXME... */
4454         if ((!legacy_mode) && (n_ports > 2)) {
4455                 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4456                 n_ports = 2;
4457                 /* For now */
4458         }
4459
4460         /* FIXME: Really for ATA it isn't safe because the device may be
4461            multi-purpose and we want to leave it alone if it was already
4462            enabled. Secondly for shared use as Arjan says we want refcounting
4463            
4464            Checking dev->is_enabled is insufficient as this is not set at
4465            boot for the primary video which is BIOS enabled
4466          */
4467          
4468         rc = pci_enable_device(pdev);
4469         if (rc)
4470                 return rc;
4471
4472         rc = pci_request_regions(pdev, DRV_NAME);
4473         if (rc) {
4474                 disable_dev_on_err = 0;
4475                 goto err_out;
4476         }
4477
4478         /* FIXME: Should use platform specific mappers for legacy port ranges */
4479         if (legacy_mode) {
4480                 if (!request_region(0x1f0, 8, "libata")) {
4481                         struct resource *conflict, res;
4482                         res.start = 0x1f0;
4483                         res.end = 0x1f0 + 8 - 1;
4484                         conflict = ____request_resource(&ioport_resource, &res);
4485                         if (!strcmp(conflict->name, "libata"))
4486                                 legacy_mode |= (1 << 0);
4487                         else {
4488                                 disable_dev_on_err = 0;
4489                                 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4490                         }
4491                 } else
4492                         legacy_mode |= (1 << 0);
4493
4494                 if (!request_region(0x170, 8, "libata")) {
4495                         struct resource *conflict, res;
4496                         res.start = 0x170;
4497                         res.end = 0x170 + 8 - 1;
4498                         conflict = ____request_resource(&ioport_resource, &res);
4499                         if (!strcmp(conflict->name, "libata"))
4500                                 legacy_mode |= (1 << 1);
4501                         else {
4502                                 disable_dev_on_err = 0;
4503                                 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4504                         }
4505                 } else
4506                         legacy_mode |= (1 << 1);
4507         }
4508
4509         /* we have legacy mode, but all ports are unavailable */
4510         if (legacy_mode == (1 << 3)) {
4511                 rc = -EBUSY;
4512                 goto err_out_regions;
4513         }
4514
4515         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4516         if (rc)
4517                 goto err_out_regions;
4518         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4519         if (rc)
4520                 goto err_out_regions;
4521
4522         if (legacy_mode) {
4523                 if (legacy_mode & (1 << 0))
4524                         probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4525                 if (legacy_mode & (1 << 1))
4526                         probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4527         } else {
4528                 if (n_ports == 2)
4529                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4530                 else
4531                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4532         }
4533         if (!probe_ent && !probe_ent2) {
4534                 rc = -ENOMEM;
4535                 goto err_out_regions;
4536         }
4537
4538         pci_set_master(pdev);
4539
4540         /* FIXME: check ata_device_add return */
4541         if (legacy_mode) {
4542                 if (legacy_mode & (1 << 0))
4543                         ata_device_add(probe_ent);
4544                 if (legacy_mode & (1 << 1))
4545                         ata_device_add(probe_ent2);
4546         } else
4547                 ata_device_add(probe_ent);
4548
4549         kfree(probe_ent);
4550         kfree(probe_ent2);
4551
4552         return 0;
4553
4554 err_out_regions:
4555         if (legacy_mode & (1 << 0))
4556                 release_region(0x1f0, 8);
4557         if (legacy_mode & (1 << 1))
4558                 release_region(0x170, 8);
4559         pci_release_regions(pdev);
4560 err_out:
4561         if (disable_dev_on_err)
4562                 pci_disable_device(pdev);
4563         return rc;
4564 }
4565
4566 /**
4567  *      ata_pci_remove_one - PCI layer callback for device removal
4568  *      @pdev: PCI device that was removed
4569  *
4570  *      PCI layer indicates to libata via this hook that
4571  *      hot-unplug or module unload event has occured.
4572  *      Handle this by unregistering all objects associated
4573  *      with this PCI device.  Free those objects.  Then finally
4574  *      release PCI resources and disable device.
4575  *
4576  *      LOCKING:
4577  *      Inherited from PCI layer (may sleep).
4578  */
4579
4580 void ata_pci_remove_one (struct pci_dev *pdev)
4581 {
4582         struct device *dev = pci_dev_to_dev(pdev);
4583         struct ata_host_set *host_set = dev_get_drvdata(dev);
4584
4585         ata_host_set_remove(host_set);
4586         pci_release_regions(pdev);
4587         pci_disable_device(pdev);
4588         dev_set_drvdata(dev, NULL);
4589 }
4590
4591 /* move to PCI subsystem */
4592 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4593 {
4594         unsigned long tmp = 0;
4595
4596         switch (bits->width) {
4597         case 1: {
4598                 u8 tmp8 = 0;
4599                 pci_read_config_byte(pdev, bits->reg, &tmp8);
4600                 tmp = tmp8;
4601                 break;
4602         }
4603         case 2: {
4604                 u16 tmp16 = 0;
4605                 pci_read_config_word(pdev, bits->reg, &tmp16);
4606                 tmp = tmp16;
4607                 break;
4608         }
4609         case 4: {
4610                 u32 tmp32 = 0;
4611                 pci_read_config_dword(pdev, bits->reg, &tmp32);
4612                 tmp = tmp32;
4613                 break;
4614         }
4615
4616         default:
4617                 return -EINVAL;
4618         }
4619
4620         tmp &= bits->mask;
4621
4622         return (tmp == bits->val) ? 1 : 0;
4623 }
4624 #endif /* CONFIG_PCI */
4625
4626
4627 static int __init ata_init(void)
4628 {
4629         ata_wq = create_workqueue("ata");
4630         if (!ata_wq)
4631                 return -ENOMEM;
4632
4633         printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4634         return 0;
4635 }
4636
4637 static void __exit ata_exit(void)
4638 {
4639         destroy_workqueue(ata_wq);
4640 }
4641
4642 module_init(ata_init);
4643 module_exit(ata_exit);
4644
4645 static unsigned long ratelimit_time;
4646 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4647
4648 int ata_ratelimit(void)
4649 {
4650         int rc;
4651         unsigned long flags;
4652
4653         spin_lock_irqsave(&ata_ratelimit_lock, flags);
4654
4655         if (time_after(jiffies, ratelimit_time)) {
4656                 rc = 1;
4657                 ratelimit_time = jiffies + (HZ/5);
4658         } else
4659                 rc = 0;
4660
4661         spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4662
4663         return rc;
4664 }
4665
4666 /*
4667  * libata is essentially a library of internal helper functions for
4668  * low-level ATA host controller drivers.  As such, the API/ABI is
4669  * likely to change as new drivers are added and updated.
4670  * Do not depend on ABI/API stability.
4671  */
4672
4673 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4674 EXPORT_SYMBOL_GPL(ata_std_ports);
4675 EXPORT_SYMBOL_GPL(ata_device_add);
4676 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4677 EXPORT_SYMBOL_GPL(ata_sg_init);
4678 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4679 EXPORT_SYMBOL_GPL(ata_qc_complete);
4680 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4681 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4682 EXPORT_SYMBOL_GPL(ata_tf_load);
4683 EXPORT_SYMBOL_GPL(ata_tf_read);
4684 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4685 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4686 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4687 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4688 EXPORT_SYMBOL_GPL(ata_check_status);
4689 EXPORT_SYMBOL_GPL(ata_altstatus);
4690 EXPORT_SYMBOL_GPL(ata_chk_err);
4691 EXPORT_SYMBOL_GPL(ata_exec_command);
4692 EXPORT_SYMBOL_GPL(ata_port_start);
4693 EXPORT_SYMBOL_GPL(ata_port_stop);
4694 EXPORT_SYMBOL_GPL(ata_host_stop);
4695 EXPORT_SYMBOL_GPL(ata_interrupt);
4696 EXPORT_SYMBOL_GPL(ata_qc_prep);
4697 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4698 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4699 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4700 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4701 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4702 EXPORT_SYMBOL_GPL(ata_port_probe);
4703 EXPORT_SYMBOL_GPL(sata_phy_reset);
4704 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4705 EXPORT_SYMBOL_GPL(ata_bus_reset);
4706 EXPORT_SYMBOL_GPL(ata_port_disable);
4707 EXPORT_SYMBOL_GPL(ata_ratelimit);
4708 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4709 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4710 EXPORT_SYMBOL_GPL(ata_scsi_error);
4711 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4712 EXPORT_SYMBOL_GPL(ata_scsi_release);
4713 EXPORT_SYMBOL_GPL(ata_host_intr);
4714 EXPORT_SYMBOL_GPL(ata_dev_classify);
4715 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4716 EXPORT_SYMBOL_GPL(ata_dev_config);
4717 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4718
4719 #ifdef CONFIG_PCI
4720 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4721 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4722 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4723 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4724 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4725 #endif /* CONFIG_PCI */