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