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