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