]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/libata-core.c
Merge branch 'upstream'
[karo-tx-linux.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         sstatus = scr_read(ap, SCR_STATUS);
1458         if (sata_dev_present(ap)) {
1459                 const char *speed;
1460                 u32 tmp;
1461
1462                 tmp = (sstatus >> 4) & 0xf;
1463                 if (tmp & (1 << 0))
1464                         speed = "1.5";
1465                 else if (tmp & (1 << 1))
1466                         speed = "3.0";
1467                 else
1468                         speed = "<unknown>";
1469                 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1470                        ap->id, speed, sstatus);
1471                 ata_port_probe(ap);
1472         } else {
1473                 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1474                        ap->id, sstatus);
1475                 ata_port_disable(ap);
1476         }
1477
1478         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1479                 return;
1480
1481         if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1482                 ata_port_disable(ap);
1483                 return;
1484         }
1485
1486         ap->cbl = ATA_CBL_SATA;
1487 }
1488
1489 /**
1490  *      sata_phy_reset - Reset SATA bus.
1491  *      @ap: SATA port associated with target SATA PHY.
1492  *
1493  *      This function resets the SATA bus, and then probes
1494  *      the bus for devices.
1495  *
1496  *      LOCKING:
1497  *      PCI/etc. bus probe sem.
1498  *
1499  */
1500 void sata_phy_reset(struct ata_port *ap)
1501 {
1502         __sata_phy_reset(ap);
1503         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1504                 return;
1505         ata_bus_reset(ap);
1506 }
1507
1508 /**
1509  *      ata_port_disable - Disable port.
1510  *      @ap: Port to be disabled.
1511  *
1512  *      Modify @ap data structure such that the system
1513  *      thinks that the entire port is disabled, and should
1514  *      never attempt to probe or communicate with devices
1515  *      on this port.
1516  *
1517  *      LOCKING: host_set lock, or some other form of
1518  *      serialization.
1519  */
1520
1521 void ata_port_disable(struct ata_port *ap)
1522 {
1523         ap->device[0].class = ATA_DEV_NONE;
1524         ap->device[1].class = ATA_DEV_NONE;
1525         ap->flags |= ATA_FLAG_PORT_DISABLED;
1526 }
1527
1528 /*
1529  * This mode timing computation functionality is ported over from
1530  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1531  */
1532 /*
1533  * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1534  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1535  * for PIO 5, which is a nonstandard extension and UDMA6, which
1536  * is currently supported only by Maxtor drives. 
1537  */
1538
1539 static const struct ata_timing ata_timing[] = {
1540
1541         { XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
1542         { XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
1543         { XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
1544         { XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
1545
1546         { XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
1547         { XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
1548         { XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
1549
1550 /*      { XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
1551                                           
1552         { XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
1553         { XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
1554         { XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
1555                                           
1556         { XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
1557         { XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
1558         { XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
1559
1560 /*      { XFER_PIO_5,     20,  50,  30, 100,  50,  30, 100,   0 }, */
1561         { XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
1562         { XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
1563
1564         { XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
1565         { XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
1566         { XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
1567
1568 /*      { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
1569
1570         { 0xFF }
1571 };
1572
1573 #define ENOUGH(v,unit)          (((v)-1)/(unit)+1)
1574 #define EZ(v,unit)              ((v)?ENOUGH(v,unit):0)
1575
1576 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1577 {
1578         q->setup   = EZ(t->setup   * 1000,  T);
1579         q->act8b   = EZ(t->act8b   * 1000,  T);
1580         q->rec8b   = EZ(t->rec8b   * 1000,  T);
1581         q->cyc8b   = EZ(t->cyc8b   * 1000,  T);
1582         q->active  = EZ(t->active  * 1000,  T);
1583         q->recover = EZ(t->recover * 1000,  T);
1584         q->cycle   = EZ(t->cycle   * 1000,  T);
1585         q->udma    = EZ(t->udma    * 1000, UT);
1586 }
1587
1588 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1589                       struct ata_timing *m, unsigned int what)
1590 {
1591         if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
1592         if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
1593         if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
1594         if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
1595         if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
1596         if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1597         if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
1598         if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
1599 }
1600
1601 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1602 {
1603         const struct ata_timing *t;
1604
1605         for (t = ata_timing; t->mode != speed; t++)
1606                 if (t->mode == 0xFF)
1607                         return NULL;
1608         return t; 
1609 }
1610
1611 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1612                        struct ata_timing *t, int T, int UT)
1613 {
1614         const struct ata_timing *s;
1615         struct ata_timing p;
1616
1617         /*
1618          * Find the mode. 
1619          */
1620
1621         if (!(s = ata_timing_find_mode(speed)))
1622                 return -EINVAL;
1623
1624         memcpy(t, s, sizeof(*s));
1625
1626         /*
1627          * If the drive is an EIDE drive, it can tell us it needs extended
1628          * PIO/MW_DMA cycle timing.
1629          */
1630
1631         if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1632                 memset(&p, 0, sizeof(p));
1633                 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1634                         if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1635                                             else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1636                 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1637                         p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1638                 }
1639                 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1640         }
1641
1642         /*
1643          * Convert the timing to bus clock counts.
1644          */
1645
1646         ata_timing_quantize(t, t, T, UT);
1647
1648         /*
1649          * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1650          * and some other commands. We have to ensure that the DMA cycle timing is
1651          * slower/equal than the fastest PIO timing.
1652          */
1653
1654         if (speed > XFER_PIO_4) {
1655                 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1656                 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1657         }
1658
1659         /*
1660          * Lenghten active & recovery time so that cycle time is correct.
1661          */
1662
1663         if (t->act8b + t->rec8b < t->cyc8b) {
1664                 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1665                 t->rec8b = t->cyc8b - t->act8b;
1666         }
1667
1668         if (t->active + t->recover < t->cycle) {
1669                 t->active += (t->cycle - (t->active + t->recover)) / 2;
1670                 t->recover = t->cycle - t->active;
1671         }
1672
1673         return 0;
1674 }
1675
1676 static const struct {
1677         unsigned int shift;
1678         u8 base;
1679 } xfer_mode_classes[] = {
1680         { ATA_SHIFT_UDMA,       XFER_UDMA_0 },
1681         { ATA_SHIFT_MWDMA,      XFER_MW_DMA_0 },
1682         { ATA_SHIFT_PIO,        XFER_PIO_0 },
1683 };
1684
1685 static inline u8 base_from_shift(unsigned int shift)
1686 {
1687         int i;
1688
1689         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1690                 if (xfer_mode_classes[i].shift == shift)
1691                         return xfer_mode_classes[i].base;
1692
1693         return 0xff;
1694 }
1695
1696 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1697 {
1698         int ofs, idx;
1699         u8 base;
1700
1701         if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1702                 return;
1703
1704         if (dev->xfer_shift == ATA_SHIFT_PIO)
1705                 dev->flags |= ATA_DFLAG_PIO;
1706
1707         ata_dev_set_xfermode(ap, dev);
1708
1709         base = base_from_shift(dev->xfer_shift);
1710         ofs = dev->xfer_mode - base;
1711         idx = ofs + dev->xfer_shift;
1712         WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1713
1714         DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1715                 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1716
1717         printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1718                 ap->id, dev->devno, xfer_mode_str[idx]);
1719 }
1720
1721 static int ata_host_set_pio(struct ata_port *ap)
1722 {
1723         unsigned int mask;
1724         int x, i;
1725         u8 base, xfer_mode;
1726
1727         mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1728         x = fgb(mask);
1729         if (x < 0) {
1730                 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1731                 return -1;
1732         }
1733
1734         base = base_from_shift(ATA_SHIFT_PIO);
1735         xfer_mode = base + x;
1736
1737         DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1738                 (int)base, (int)xfer_mode, mask, x);
1739
1740         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1741                 struct ata_device *dev = &ap->device[i];
1742                 if (ata_dev_present(dev)) {
1743                         dev->pio_mode = xfer_mode;
1744                         dev->xfer_mode = xfer_mode;
1745                         dev->xfer_shift = ATA_SHIFT_PIO;
1746                         if (ap->ops->set_piomode)
1747                                 ap->ops->set_piomode(ap, dev);
1748                 }
1749         }
1750
1751         return 0;
1752 }
1753
1754 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1755                             unsigned int xfer_shift)
1756 {
1757         int i;
1758
1759         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1760                 struct ata_device *dev = &ap->device[i];
1761                 if (ata_dev_present(dev)) {
1762                         dev->dma_mode = xfer_mode;
1763                         dev->xfer_mode = xfer_mode;
1764                         dev->xfer_shift = xfer_shift;
1765                         if (ap->ops->set_dmamode)
1766                                 ap->ops->set_dmamode(ap, dev);
1767                 }
1768         }
1769 }
1770
1771 /**
1772  *      ata_set_mode - Program timings and issue SET FEATURES - XFER
1773  *      @ap: port on which timings will be programmed
1774  *
1775  *      Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1776  *
1777  *      LOCKING:
1778  *      PCI/etc. bus probe sem.
1779  *
1780  */
1781 static void ata_set_mode(struct ata_port *ap)
1782 {
1783         unsigned int xfer_shift;
1784         u8 xfer_mode;
1785         int rc;
1786
1787         /* step 1: always set host PIO timings */
1788         rc = ata_host_set_pio(ap);
1789         if (rc)
1790                 goto err_out;
1791
1792         /* step 2: choose the best data xfer mode */
1793         xfer_mode = xfer_shift = 0;
1794         rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1795         if (rc)
1796                 goto err_out;
1797
1798         /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1799         if (xfer_shift != ATA_SHIFT_PIO)
1800                 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1801
1802         /* step 4: update devices' xfer mode */
1803         ata_dev_set_mode(ap, &ap->device[0]);
1804         ata_dev_set_mode(ap, &ap->device[1]);
1805
1806         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1807                 return;
1808
1809         if (ap->ops->post_set_mode)
1810                 ap->ops->post_set_mode(ap);
1811
1812         return;
1813
1814 err_out:
1815         ata_port_disable(ap);
1816 }
1817
1818 /**
1819  *      ata_busy_sleep - sleep until BSY clears, or timeout
1820  *      @ap: port containing status register to be polled
1821  *      @tmout_pat: impatience timeout
1822  *      @tmout: overall timeout
1823  *
1824  *      Sleep until ATA Status register bit BSY clears,
1825  *      or a timeout occurs.
1826  *
1827  *      LOCKING: None.
1828  *
1829  */
1830
1831 static unsigned int ata_busy_sleep (struct ata_port *ap,
1832                                     unsigned long tmout_pat,
1833                                     unsigned long tmout)
1834 {
1835         unsigned long timer_start, timeout;
1836         u8 status;
1837
1838         status = ata_busy_wait(ap, ATA_BUSY, 300);
1839         timer_start = jiffies;
1840         timeout = timer_start + tmout_pat;
1841         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1842                 msleep(50);
1843                 status = ata_busy_wait(ap, ATA_BUSY, 3);
1844         }
1845
1846         if (status & ATA_BUSY)
1847                 printk(KERN_WARNING "ata%u is slow to respond, "
1848                        "please be patient\n", ap->id);
1849
1850         timeout = timer_start + tmout;
1851         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1852                 msleep(50);
1853                 status = ata_chk_status(ap);
1854         }
1855
1856         if (status & ATA_BUSY) {
1857                 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1858                        ap->id, tmout / HZ);
1859                 return 1;
1860         }
1861
1862         return 0;
1863 }
1864
1865 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1866 {
1867         struct ata_ioports *ioaddr = &ap->ioaddr;
1868         unsigned int dev0 = devmask & (1 << 0);
1869         unsigned int dev1 = devmask & (1 << 1);
1870         unsigned long timeout;
1871
1872         /* if device 0 was found in ata_devchk, wait for its
1873          * BSY bit to clear
1874          */
1875         if (dev0)
1876                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1877
1878         /* if device 1 was found in ata_devchk, wait for
1879          * register access, then wait for BSY to clear
1880          */
1881         timeout = jiffies + ATA_TMOUT_BOOT;
1882         while (dev1) {
1883                 u8 nsect, lbal;
1884
1885                 ap->ops->dev_select(ap, 1);
1886                 if (ap->flags & ATA_FLAG_MMIO) {
1887                         nsect = readb((void __iomem *) ioaddr->nsect_addr);
1888                         lbal = readb((void __iomem *) ioaddr->lbal_addr);
1889                 } else {
1890                         nsect = inb(ioaddr->nsect_addr);
1891                         lbal = inb(ioaddr->lbal_addr);
1892                 }
1893                 if ((nsect == 1) && (lbal == 1))
1894                         break;
1895                 if (time_after(jiffies, timeout)) {
1896                         dev1 = 0;
1897                         break;
1898                 }
1899                 msleep(50);     /* give drive a breather */
1900         }
1901         if (dev1)
1902                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1903
1904         /* is all this really necessary? */
1905         ap->ops->dev_select(ap, 0);
1906         if (dev1)
1907                 ap->ops->dev_select(ap, 1);
1908         if (dev0)
1909                 ap->ops->dev_select(ap, 0);
1910 }
1911
1912 /**
1913  *      ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1914  *      @ap: Port to reset and probe
1915  *
1916  *      Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1917  *      probe the bus.  Not often used these days.
1918  *
1919  *      LOCKING:
1920  *      PCI/etc. bus probe sem.
1921  *      Obtains host_set lock.
1922  *
1923  */
1924
1925 static unsigned int ata_bus_edd(struct ata_port *ap)
1926 {
1927         struct ata_taskfile tf;
1928         unsigned long flags;
1929
1930         /* set up execute-device-diag (bus reset) taskfile */
1931         /* also, take interrupts to a known state (disabled) */
1932         DPRINTK("execute-device-diag\n");
1933         ata_tf_init(ap, &tf, 0);
1934         tf.ctl |= ATA_NIEN;
1935         tf.command = ATA_CMD_EDD;
1936         tf.protocol = ATA_PROT_NODATA;
1937
1938         /* do bus reset */
1939         spin_lock_irqsave(&ap->host_set->lock, flags);
1940         ata_tf_to_host(ap, &tf);
1941         spin_unlock_irqrestore(&ap->host_set->lock, flags);
1942
1943         /* spec says at least 2ms.  but who knows with those
1944          * crazy ATAPI devices...
1945          */
1946         msleep(150);
1947
1948         return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1949 }
1950
1951 static unsigned int ata_bus_softreset(struct ata_port *ap,
1952                                       unsigned int devmask)
1953 {
1954         struct ata_ioports *ioaddr = &ap->ioaddr;
1955
1956         DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1957
1958         /* software reset.  causes dev0 to be selected */
1959         if (ap->flags & ATA_FLAG_MMIO) {
1960                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1961                 udelay(20);     /* FIXME: flush */
1962                 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1963                 udelay(20);     /* FIXME: flush */
1964                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1965         } else {
1966                 outb(ap->ctl, ioaddr->ctl_addr);
1967                 udelay(10);
1968                 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1969                 udelay(10);
1970                 outb(ap->ctl, ioaddr->ctl_addr);
1971         }
1972
1973         /* spec mandates ">= 2ms" before checking status.
1974          * We wait 150ms, because that was the magic delay used for
1975          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1976          * between when the ATA command register is written, and then
1977          * status is checked.  Because waiting for "a while" before
1978          * checking status is fine, post SRST, we perform this magic
1979          * delay here as well.
1980          */
1981         msleep(150);
1982
1983         ata_bus_post_reset(ap, devmask);
1984
1985         return 0;
1986 }
1987
1988 /**
1989  *      ata_bus_reset - reset host port and associated ATA channel
1990  *      @ap: port to reset
1991  *
1992  *      This is typically the first time we actually start issuing
1993  *      commands to the ATA channel.  We wait for BSY to clear, then
1994  *      issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1995  *      result.  Determine what devices, if any, are on the channel
1996  *      by looking at the device 0/1 error register.  Look at the signature
1997  *      stored in each device's taskfile registers, to determine if
1998  *      the device is ATA or ATAPI.
1999  *
2000  *      LOCKING:
2001  *      PCI/etc. bus probe sem.
2002  *      Obtains host_set lock.
2003  *
2004  *      SIDE EFFECTS:
2005  *      Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2006  */
2007
2008 void ata_bus_reset(struct ata_port *ap)
2009 {
2010         struct ata_ioports *ioaddr = &ap->ioaddr;
2011         unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2012         u8 err;
2013         unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2014
2015         DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2016
2017         /* determine if device 0/1 are present */
2018         if (ap->flags & ATA_FLAG_SATA_RESET)
2019                 dev0 = 1;
2020         else {
2021                 dev0 = ata_devchk(ap, 0);
2022                 if (slave_possible)
2023                         dev1 = ata_devchk(ap, 1);
2024         }
2025
2026         if (dev0)
2027                 devmask |= (1 << 0);
2028         if (dev1)
2029                 devmask |= (1 << 1);
2030
2031         /* select device 0 again */
2032         ap->ops->dev_select(ap, 0);
2033
2034         /* issue bus reset */
2035         if (ap->flags & ATA_FLAG_SRST)
2036                 rc = ata_bus_softreset(ap, devmask);
2037         else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2038                 /* set up device control */
2039                 if (ap->flags & ATA_FLAG_MMIO)
2040                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2041                 else
2042                         outb(ap->ctl, ioaddr->ctl_addr);
2043                 rc = ata_bus_edd(ap);
2044         }
2045
2046         if (rc)
2047                 goto err_out;
2048
2049         /*
2050          * determine by signature whether we have ATA or ATAPI devices
2051          */
2052         err = ata_dev_try_classify(ap, 0);
2053         if ((slave_possible) && (err != 0x81))
2054                 ata_dev_try_classify(ap, 1);
2055
2056         /* re-enable interrupts */
2057         if (ap->ioaddr.ctl_addr)        /* FIXME: hack. create a hook instead */
2058                 ata_irq_on(ap);
2059
2060         /* is double-select really necessary? */
2061         if (ap->device[1].class != ATA_DEV_NONE)
2062                 ap->ops->dev_select(ap, 1);
2063         if (ap->device[0].class != ATA_DEV_NONE)
2064                 ap->ops->dev_select(ap, 0);
2065
2066         /* if no devices were detected, disable this port */
2067         if ((ap->device[0].class == ATA_DEV_NONE) &&
2068             (ap->device[1].class == ATA_DEV_NONE))
2069                 goto err_out;
2070
2071         if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2072                 /* set up device control for ATA_FLAG_SATA_RESET */
2073                 if (ap->flags & ATA_FLAG_MMIO)
2074                         writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2075                 else
2076                         outb(ap->ctl, ioaddr->ctl_addr);
2077         }
2078
2079         DPRINTK("EXIT\n");
2080         return;
2081
2082 err_out:
2083         printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2084         ap->ops->port_disable(ap);
2085
2086         DPRINTK("EXIT\n");
2087 }
2088
2089 static void ata_pr_blacklisted(const struct ata_port *ap,
2090                                const struct ata_device *dev)
2091 {
2092         printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2093                 ap->id, dev->devno);
2094 }
2095
2096 static const char * ata_dma_blacklist [] = {
2097         "WDC AC11000H",
2098         "WDC AC22100H",
2099         "WDC AC32500H",
2100         "WDC AC33100H",
2101         "WDC AC31600H",
2102         "WDC AC32100H",
2103         "WDC AC23200L",
2104         "Compaq CRD-8241B",
2105         "CRD-8400B",
2106         "CRD-8480B",
2107         "CRD-8482B",
2108         "CRD-84",
2109         "SanDisk SDP3B",
2110         "SanDisk SDP3B-64",
2111         "SANYO CD-ROM CRD",
2112         "HITACHI CDR-8",
2113         "HITACHI CDR-8335",
2114         "HITACHI CDR-8435",
2115         "Toshiba CD-ROM XM-6202B",
2116         "TOSHIBA CD-ROM XM-1702BC",
2117         "CD-532E-A",
2118         "E-IDE CD-ROM CR-840",
2119         "CD-ROM Drive/F5A",
2120         "WPI CDD-820",
2121         "SAMSUNG CD-ROM SC-148C",
2122         "SAMSUNG CD-ROM SC",
2123         "SanDisk SDP3B-64",
2124         "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2125         "_NEC DV5800A",
2126 };
2127
2128 static int ata_dma_blacklisted(const struct ata_device *dev)
2129 {
2130         unsigned char model_num[40];
2131         char *s;
2132         unsigned int len;
2133         int i;
2134
2135         ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2136                           sizeof(model_num));
2137         s = &model_num[0];
2138         len = strnlen(s, sizeof(model_num));
2139
2140         /* ATAPI specifies that empty space is blank-filled; remove blanks */
2141         while ((len > 0) && (s[len - 1] == ' ')) {
2142                 len--;
2143                 s[len] = 0;
2144         }
2145
2146         for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2147                 if (!strncmp(ata_dma_blacklist[i], s, len))
2148                         return 1;
2149
2150         return 0;
2151 }
2152
2153 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2154 {
2155         const struct ata_device *master, *slave;
2156         unsigned int mask;
2157
2158         master = &ap->device[0];
2159         slave = &ap->device[1];
2160
2161         assert (ata_dev_present(master) || ata_dev_present(slave));
2162
2163         if (shift == ATA_SHIFT_UDMA) {
2164                 mask = ap->udma_mask;
2165                 if (ata_dev_present(master)) {
2166                         mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2167                         if (ata_dma_blacklisted(master)) {
2168                                 mask = 0;
2169                                 ata_pr_blacklisted(ap, master);
2170                         }
2171                 }
2172                 if (ata_dev_present(slave)) {
2173                         mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2174                         if (ata_dma_blacklisted(slave)) {
2175                                 mask = 0;
2176                                 ata_pr_blacklisted(ap, slave);
2177                         }
2178                 }
2179         }
2180         else if (shift == ATA_SHIFT_MWDMA) {
2181                 mask = ap->mwdma_mask;
2182                 if (ata_dev_present(master)) {
2183                         mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2184                         if (ata_dma_blacklisted(master)) {
2185                                 mask = 0;
2186                                 ata_pr_blacklisted(ap, master);
2187                         }
2188                 }
2189                 if (ata_dev_present(slave)) {
2190                         mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2191                         if (ata_dma_blacklisted(slave)) {
2192                                 mask = 0;
2193                                 ata_pr_blacklisted(ap, slave);
2194                         }
2195                 }
2196         }
2197         else if (shift == ATA_SHIFT_PIO) {
2198                 mask = ap->pio_mask;
2199                 if (ata_dev_present(master)) {
2200                         /* spec doesn't return explicit support for
2201                          * PIO0-2, so we fake it
2202                          */
2203                         u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2204                         tmp_mode <<= 3;
2205                         tmp_mode |= 0x7;
2206                         mask &= tmp_mode;
2207                 }
2208                 if (ata_dev_present(slave)) {
2209                         /* spec doesn't return explicit support for
2210                          * PIO0-2, so we fake it
2211                          */
2212                         u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2213                         tmp_mode <<= 3;
2214                         tmp_mode |= 0x7;
2215                         mask &= tmp_mode;
2216                 }
2217         }
2218         else {
2219                 mask = 0xffffffff; /* shut up compiler warning */
2220                 BUG();
2221         }
2222
2223         return mask;
2224 }
2225
2226 /* find greatest bit */
2227 static int fgb(u32 bitmap)
2228 {
2229         unsigned int i;
2230         int x = -1;
2231
2232         for (i = 0; i < 32; i++)
2233                 if (bitmap & (1 << i))
2234                         x = i;
2235
2236         return x;
2237 }
2238
2239 /**
2240  *      ata_choose_xfer_mode - attempt to find best transfer mode
2241  *      @ap: Port for which an xfer mode will be selected
2242  *      @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2243  *      @xfer_shift_out: (output) bit shift that selects this mode
2244  *
2245  *      Based on host and device capabilities, determine the
2246  *      maximum transfer mode that is amenable to all.
2247  *
2248  *      LOCKING:
2249  *      PCI/etc. bus probe sem.
2250  *
2251  *      RETURNS:
2252  *      Zero on success, negative on error.
2253  */
2254
2255 static int ata_choose_xfer_mode(const struct ata_port *ap,
2256                                 u8 *xfer_mode_out,
2257                                 unsigned int *xfer_shift_out)
2258 {
2259         unsigned int mask, shift;
2260         int x, i;
2261
2262         for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2263                 shift = xfer_mode_classes[i].shift;
2264                 mask = ata_get_mode_mask(ap, shift);
2265
2266                 x = fgb(mask);
2267                 if (x >= 0) {
2268                         *xfer_mode_out = xfer_mode_classes[i].base + x;
2269                         *xfer_shift_out = shift;
2270                         return 0;
2271                 }
2272         }
2273
2274         return -1;
2275 }
2276
2277 /**
2278  *      ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2279  *      @ap: Port associated with device @dev
2280  *      @dev: Device to which command will be sent
2281  *
2282  *      Issue SET FEATURES - XFER MODE command to device @dev
2283  *      on port @ap.
2284  *
2285  *      LOCKING:
2286  *      PCI/etc. bus probe sem.
2287  */
2288
2289 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2290 {
2291         DECLARE_COMPLETION(wait);
2292         struct ata_queued_cmd *qc;
2293         int rc;
2294         unsigned long flags;
2295
2296         /* set up set-features taskfile */
2297         DPRINTK("set features - xfer mode\n");
2298
2299         qc = ata_qc_new_init(ap, dev);
2300         BUG_ON(qc == NULL);
2301
2302         qc->tf.command = ATA_CMD_SET_FEATURES;
2303         qc->tf.feature = SETFEATURES_XFER;
2304         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2305         qc->tf.protocol = ATA_PROT_NODATA;
2306         qc->tf.nsect = dev->xfer_mode;
2307
2308         qc->waiting = &wait;
2309         qc->complete_fn = ata_qc_complete_noop;
2310
2311         spin_lock_irqsave(&ap->host_set->lock, flags);
2312         rc = ata_qc_issue(qc);
2313         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2314
2315         if (rc)
2316                 ata_port_disable(ap);
2317         else
2318                 ata_qc_wait_err(qc, &wait);
2319
2320         DPRINTK("EXIT\n");
2321 }
2322
2323 /**
2324  *      ata_dev_reread_id - Reread the device identify device info
2325  *      @ap: port where the device is
2326  *      @dev: device to reread the identify device info
2327  *
2328  *      LOCKING:
2329  */
2330
2331 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2332 {
2333         DECLARE_COMPLETION(wait);
2334         struct ata_queued_cmd *qc;
2335         unsigned long flags;
2336         int rc;
2337
2338         qc = ata_qc_new_init(ap, dev);
2339         BUG_ON(qc == NULL);
2340
2341         ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2342         qc->dma_dir = DMA_FROM_DEVICE;
2343
2344         if (dev->class == ATA_DEV_ATA) {
2345                 qc->tf.command = ATA_CMD_ID_ATA;
2346                 DPRINTK("do ATA identify\n");
2347         } else {
2348                 qc->tf.command = ATA_CMD_ID_ATAPI;
2349                 DPRINTK("do ATAPI identify\n");
2350         }
2351
2352         qc->tf.flags |= ATA_TFLAG_DEVICE;
2353         qc->tf.protocol = ATA_PROT_PIO;
2354         qc->nsect = 1;
2355
2356         qc->waiting = &wait;
2357         qc->complete_fn = ata_qc_complete_noop;
2358
2359         spin_lock_irqsave(&ap->host_set->lock, flags);
2360         rc = ata_qc_issue(qc);
2361         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2362
2363         if (rc)
2364                 goto err_out;
2365
2366         ata_qc_wait_err(qc, &wait);
2367
2368         swap_buf_le16(dev->id, ATA_ID_WORDS);
2369
2370         ata_dump_id(dev);
2371
2372         DPRINTK("EXIT\n");
2373
2374         return;
2375 err_out:
2376         ata_port_disable(ap);
2377 }
2378
2379 /**
2380  *      ata_dev_init_params - Issue INIT DEV PARAMS command
2381  *      @ap: Port associated with device @dev
2382  *      @dev: Device to which command will be sent
2383  *
2384  *      LOCKING:
2385  */
2386
2387 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2388 {
2389         DECLARE_COMPLETION(wait);
2390         struct ata_queued_cmd *qc;
2391         int rc;
2392         unsigned long flags;
2393         u16 sectors = dev->id[6];
2394         u16 heads   = dev->id[3];
2395
2396         /* Number of sectors per track 1-255. Number of heads 1-16 */
2397         if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2398                 return;
2399
2400         /* set up init dev params taskfile */
2401         DPRINTK("init dev params \n");
2402
2403         qc = ata_qc_new_init(ap, dev);
2404         BUG_ON(qc == NULL);
2405
2406         qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2407         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2408         qc->tf.protocol = ATA_PROT_NODATA;
2409         qc->tf.nsect = sectors;
2410         qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2411
2412         qc->waiting = &wait;
2413         qc->complete_fn = ata_qc_complete_noop;
2414
2415         spin_lock_irqsave(&ap->host_set->lock, flags);
2416         rc = ata_qc_issue(qc);
2417         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2418
2419         if (rc)
2420                 ata_port_disable(ap);
2421         else
2422                 ata_qc_wait_err(qc, &wait);
2423
2424         DPRINTK("EXIT\n");
2425 }
2426
2427 /**
2428  *      ata_sg_clean - Unmap DMA memory associated with command
2429  *      @qc: Command containing DMA memory to be released
2430  *
2431  *      Unmap all mapped DMA memory associated with this command.
2432  *
2433  *      LOCKING:
2434  *      spin_lock_irqsave(host_set lock)
2435  */
2436
2437 static void ata_sg_clean(struct ata_queued_cmd *qc)
2438 {
2439         struct ata_port *ap = qc->ap;
2440         struct scatterlist *sg = qc->__sg;
2441         int dir = qc->dma_dir;
2442         void *pad_buf = NULL;
2443
2444         assert(qc->flags & ATA_QCFLAG_DMAMAP);
2445         assert(sg != NULL);
2446
2447         if (qc->flags & ATA_QCFLAG_SINGLE)
2448                 assert(qc->n_elem == 1);
2449
2450         VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2451
2452         /* if we padded the buffer out to 32-bit bound, and data
2453          * xfer direction is from-device, we must copy from the
2454          * pad buffer back into the supplied buffer
2455          */
2456         if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2457                 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2458
2459         if (qc->flags & ATA_QCFLAG_SG) {
2460                 if (qc->n_elem)
2461                         dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2462                 /* restore last sg */
2463                 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2464                 if (pad_buf) {
2465                         struct scatterlist *psg = &qc->pad_sgent;
2466                         void *addr = kmap_atomic(psg->page, KM_IRQ0);
2467                         memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2468                         kunmap_atomic(psg->page, KM_IRQ0);
2469                 }
2470         } else {
2471                 if (sg_dma_len(&sg[0]) > 0)
2472                         dma_unmap_single(ap->host_set->dev,
2473                                 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2474                                 dir);
2475                 /* restore sg */
2476                 sg->length += qc->pad_len;
2477                 if (pad_buf)
2478                         memcpy(qc->buf_virt + sg->length - qc->pad_len,
2479                                pad_buf, qc->pad_len);
2480         }
2481
2482         qc->flags &= ~ATA_QCFLAG_DMAMAP;
2483         qc->__sg = NULL;
2484 }
2485
2486 /**
2487  *      ata_fill_sg - Fill PCI IDE PRD table
2488  *      @qc: Metadata associated with taskfile to be transferred
2489  *
2490  *      Fill PCI IDE PRD (scatter-gather) table with segments
2491  *      associated with the current disk command.
2492  *
2493  *      LOCKING:
2494  *      spin_lock_irqsave(host_set lock)
2495  *
2496  */
2497 static void ata_fill_sg(struct ata_queued_cmd *qc)
2498 {
2499         struct ata_port *ap = qc->ap;
2500         struct scatterlist *sg;
2501         unsigned int idx;
2502
2503         assert(qc->__sg != NULL);
2504         assert(qc->n_elem > 0);
2505
2506         idx = 0;
2507         ata_for_each_sg(sg, qc) {
2508                 u32 addr, offset;
2509                 u32 sg_len, len;
2510
2511                 /* determine if physical DMA addr spans 64K boundary.
2512                  * Note h/w doesn't support 64-bit, so we unconditionally
2513                  * truncate dma_addr_t to u32.
2514                  */
2515                 addr = (u32) sg_dma_address(sg);
2516                 sg_len = sg_dma_len(sg);
2517
2518                 while (sg_len) {
2519                         offset = addr & 0xffff;
2520                         len = sg_len;
2521                         if ((offset + sg_len) > 0x10000)
2522                                 len = 0x10000 - offset;
2523
2524                         ap->prd[idx].addr = cpu_to_le32(addr);
2525                         ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2526                         VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2527
2528                         idx++;
2529                         sg_len -= len;
2530                         addr += len;
2531                 }
2532         }
2533
2534         if (idx)
2535                 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2536 }
2537 /**
2538  *      ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2539  *      @qc: Metadata associated with taskfile to check
2540  *
2541  *      Allow low-level driver to filter ATA PACKET commands, returning
2542  *      a status indicating whether or not it is OK to use DMA for the
2543  *      supplied PACKET command.
2544  *
2545  *      LOCKING:
2546  *      spin_lock_irqsave(host_set lock)
2547  *
2548  *      RETURNS: 0 when ATAPI DMA can be used
2549  *               nonzero otherwise
2550  */
2551 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2552 {
2553         struct ata_port *ap = qc->ap;
2554         int rc = 0; /* Assume ATAPI DMA is OK by default */
2555
2556         if (ap->ops->check_atapi_dma)
2557                 rc = ap->ops->check_atapi_dma(qc);
2558
2559         return rc;
2560 }
2561 /**
2562  *      ata_qc_prep - Prepare taskfile for submission
2563  *      @qc: Metadata associated with taskfile to be prepared
2564  *
2565  *      Prepare ATA taskfile for submission.
2566  *
2567  *      LOCKING:
2568  *      spin_lock_irqsave(host_set lock)
2569  */
2570 void ata_qc_prep(struct ata_queued_cmd *qc)
2571 {
2572         if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2573                 return;
2574
2575         ata_fill_sg(qc);
2576 }
2577
2578 /**
2579  *      ata_sg_init_one - Associate command with memory buffer
2580  *      @qc: Command to be associated
2581  *      @buf: Memory buffer
2582  *      @buflen: Length of memory buffer, in bytes.
2583  *
2584  *      Initialize the data-related elements of queued_cmd @qc
2585  *      to point to a single memory buffer, @buf of byte length @buflen.
2586  *
2587  *      LOCKING:
2588  *      spin_lock_irqsave(host_set lock)
2589  */
2590
2591 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2592 {
2593         struct scatterlist *sg;
2594
2595         qc->flags |= ATA_QCFLAG_SINGLE;
2596
2597         memset(&qc->sgent, 0, sizeof(qc->sgent));
2598         qc->__sg = &qc->sgent;
2599         qc->n_elem = 1;
2600         qc->orig_n_elem = 1;
2601         qc->buf_virt = buf;
2602
2603         sg = qc->__sg;
2604         sg_init_one(sg, buf, buflen);
2605 }
2606
2607 /**
2608  *      ata_sg_init - Associate command with scatter-gather table.
2609  *      @qc: Command to be associated
2610  *      @sg: Scatter-gather table.
2611  *      @n_elem: Number of elements in s/g table.
2612  *
2613  *      Initialize the data-related elements of queued_cmd @qc
2614  *      to point to a scatter-gather table @sg, containing @n_elem
2615  *      elements.
2616  *
2617  *      LOCKING:
2618  *      spin_lock_irqsave(host_set lock)
2619  */
2620
2621 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2622                  unsigned int n_elem)
2623 {
2624         qc->flags |= ATA_QCFLAG_SG;
2625         qc->__sg = sg;
2626         qc->n_elem = n_elem;
2627         qc->orig_n_elem = n_elem;
2628 }
2629
2630 /**
2631  *      ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2632  *      @qc: Command with memory buffer to be mapped.
2633  *
2634  *      DMA-map the memory buffer associated with queued_cmd @qc.
2635  *
2636  *      LOCKING:
2637  *      spin_lock_irqsave(host_set lock)
2638  *
2639  *      RETURNS:
2640  *      Zero on success, negative on error.
2641  */
2642
2643 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2644 {
2645         struct ata_port *ap = qc->ap;
2646         int dir = qc->dma_dir;
2647         struct scatterlist *sg = qc->__sg;
2648         dma_addr_t dma_address;
2649
2650         /* we must lengthen transfers to end on a 32-bit boundary */
2651         qc->pad_len = sg->length & 3;
2652         if (qc->pad_len) {
2653                 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2654                 struct scatterlist *psg = &qc->pad_sgent;
2655
2656                 assert(qc->dev->class == ATA_DEV_ATAPI);
2657
2658                 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2659
2660                 if (qc->tf.flags & ATA_TFLAG_WRITE)
2661                         memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2662                                qc->pad_len);
2663
2664                 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2665                 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2666                 /* trim sg */
2667                 sg->length -= qc->pad_len;
2668
2669                 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2670                         sg->length, qc->pad_len);
2671         }
2672
2673         if (!sg->length) {
2674                 sg_dma_address(sg) = 0;
2675                 goto skip_map;
2676         }
2677
2678         dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2679                                      sg->length, dir);
2680         if (dma_mapping_error(dma_address)) {
2681                 /* restore sg */
2682                 sg->length += qc->pad_len;
2683                 return -1;
2684         }
2685
2686         sg_dma_address(sg) = dma_address;
2687 skip_map:
2688         sg_dma_len(sg) = sg->length;
2689
2690         DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2691                 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2692
2693         return 0;
2694 }
2695
2696 /**
2697  *      ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2698  *      @qc: Command with scatter-gather table to be mapped.
2699  *
2700  *      DMA-map the scatter-gather table associated with queued_cmd @qc.
2701  *
2702  *      LOCKING:
2703  *      spin_lock_irqsave(host_set lock)
2704  *
2705  *      RETURNS:
2706  *      Zero on success, negative on error.
2707  *
2708  */
2709
2710 static int ata_sg_setup(struct ata_queued_cmd *qc)
2711 {
2712         struct ata_port *ap = qc->ap;
2713         struct scatterlist *sg = qc->__sg;
2714         struct scatterlist *lsg = &sg[qc->n_elem - 1];
2715         int n_elem, pre_n_elem, dir, trim_sg = 0;
2716
2717         VPRINTK("ENTER, ata%u\n", ap->id);
2718         assert(qc->flags & ATA_QCFLAG_SG);
2719
2720         /* we must lengthen transfers to end on a 32-bit boundary */
2721         qc->pad_len = lsg->length & 3;
2722         if (qc->pad_len) {
2723                 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2724                 struct scatterlist *psg = &qc->pad_sgent;
2725                 unsigned int offset;
2726
2727                 assert(qc->dev->class == ATA_DEV_ATAPI);
2728
2729                 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2730
2731                 /*
2732                  * psg->page/offset are used to copy to-be-written
2733                  * data in this function or read data in ata_sg_clean.
2734                  */
2735                 offset = lsg->offset + lsg->length - qc->pad_len;
2736                 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2737                 psg->offset = offset_in_page(offset);
2738
2739                 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2740                         void *addr = kmap_atomic(psg->page, KM_IRQ0);
2741                         memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2742                         kunmap_atomic(psg->page, KM_IRQ0);
2743                 }
2744
2745                 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2746                 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2747                 /* trim last sg */
2748                 lsg->length -= qc->pad_len;
2749                 if (lsg->length == 0)
2750                         trim_sg = 1;
2751
2752                 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2753                         qc->n_elem - 1, lsg->length, qc->pad_len);
2754         }
2755
2756         pre_n_elem = qc->n_elem;
2757         if (trim_sg && pre_n_elem)
2758                 pre_n_elem--;
2759
2760         if (!pre_n_elem) {
2761                 n_elem = 0;
2762                 goto skip_map;
2763         }
2764
2765         dir = qc->dma_dir;
2766         n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2767         if (n_elem < 1) {
2768                 /* restore last sg */
2769                 lsg->length += qc->pad_len;
2770                 return -1;
2771         }
2772
2773         DPRINTK("%d sg elements mapped\n", n_elem);
2774
2775 skip_map:
2776         qc->n_elem = n_elem;
2777
2778         return 0;
2779 }
2780
2781 /**
2782  *      ata_poll_qc_complete - turn irq back on and finish qc
2783  *      @qc: Command to complete
2784  *      @err_mask: ATA status register content
2785  *
2786  *      LOCKING:
2787  *      None.  (grabs host lock)
2788  */
2789
2790 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2791 {
2792         struct ata_port *ap = qc->ap;
2793         unsigned long flags;
2794
2795         spin_lock_irqsave(&ap->host_set->lock, flags);
2796         ata_irq_on(ap);
2797         ata_qc_complete(qc, err_mask);
2798         spin_unlock_irqrestore(&ap->host_set->lock, flags);
2799 }
2800
2801 /**
2802  *      ata_pio_poll -
2803  *      @ap: the target ata_port
2804  *
2805  *      LOCKING:
2806  *      None.  (executing in kernel thread context)
2807  *
2808  *      RETURNS:
2809  *      timeout value to use
2810  */
2811
2812 static unsigned long ata_pio_poll(struct ata_port *ap)
2813 {
2814         u8 status;
2815         unsigned int poll_state = HSM_ST_UNKNOWN;
2816         unsigned int reg_state = HSM_ST_UNKNOWN;
2817
2818         switch (ap->hsm_task_state) {
2819         case HSM_ST:
2820         case HSM_ST_POLL:
2821                 poll_state = HSM_ST_POLL;
2822                 reg_state = HSM_ST;
2823                 break;
2824         case HSM_ST_LAST:
2825         case HSM_ST_LAST_POLL:
2826                 poll_state = HSM_ST_LAST_POLL;
2827                 reg_state = HSM_ST_LAST;
2828                 break;
2829         default:
2830                 BUG();
2831                 break;
2832         }
2833
2834         status = ata_chk_status(ap);
2835         if (status & ATA_BUSY) {
2836                 if (time_after(jiffies, ap->pio_task_timeout)) {
2837                         ap->hsm_task_state = HSM_ST_TMOUT;
2838                         return 0;
2839                 }
2840                 ap->hsm_task_state = poll_state;
2841                 return ATA_SHORT_PAUSE;
2842         }
2843
2844         ap->hsm_task_state = reg_state;
2845         return 0;
2846 }
2847
2848 /**
2849  *      ata_pio_complete - check if drive is busy or idle
2850  *      @ap: the target ata_port
2851  *
2852  *      LOCKING:
2853  *      None.  (executing in kernel thread context)
2854  *
2855  *      RETURNS:
2856  *      Zero if qc completed.
2857  *      Non-zero if has next.
2858  */
2859
2860 static int ata_pio_complete (struct ata_port *ap)
2861 {
2862         struct ata_queued_cmd *qc;
2863         u8 drv_stat;
2864
2865         /*
2866          * This is purely heuristic.  This is a fast path.  Sometimes when
2867          * we enter, BSY will be cleared in a chk-status or two.  If not,
2868          * the drive is probably seeking or something.  Snooze for a couple
2869          * msecs, then chk-status again.  If still busy, fall back to
2870          * HSM_ST_LAST_POLL state.
2871          */
2872         drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2873         if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2874                 msleep(2);
2875                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2876                 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2877                         ap->hsm_task_state = HSM_ST_LAST_POLL;
2878                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2879                         return 1;
2880                 }
2881         }
2882
2883         drv_stat = ata_wait_idle(ap);
2884         if (!ata_ok(drv_stat)) {
2885                 ap->hsm_task_state = HSM_ST_ERR;
2886                 return 1;
2887         }
2888
2889         qc = ata_qc_from_tag(ap, ap->active_tag);
2890         assert(qc != NULL);
2891
2892         ap->hsm_task_state = HSM_ST_IDLE;
2893
2894         ata_poll_qc_complete(qc, 0);
2895
2896         /* another command may start at this point */
2897
2898         return 0;
2899 }
2900
2901
2902 /**
2903  *      swap_buf_le16 - swap halves of 16-words in place
2904  *      @buf:  Buffer to swap
2905  *      @buf_words:  Number of 16-bit words in buffer.
2906  *
2907  *      Swap halves of 16-bit words if needed to convert from
2908  *      little-endian byte order to native cpu byte order, or
2909  *      vice-versa.
2910  *
2911  *      LOCKING:
2912  *      Inherited from caller.
2913  */
2914 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2915 {
2916 #ifdef __BIG_ENDIAN
2917         unsigned int i;
2918
2919         for (i = 0; i < buf_words; i++)
2920                 buf[i] = le16_to_cpu(buf[i]);
2921 #endif /* __BIG_ENDIAN */
2922 }
2923
2924 /**
2925  *      ata_mmio_data_xfer - Transfer data by MMIO
2926  *      @ap: port to read/write
2927  *      @buf: data buffer
2928  *      @buflen: buffer length
2929  *      @write_data: read/write
2930  *
2931  *      Transfer data from/to the device data register by MMIO.
2932  *
2933  *      LOCKING:
2934  *      Inherited from caller.
2935  */
2936
2937 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2938                                unsigned int buflen, int write_data)
2939 {
2940         unsigned int i;
2941         unsigned int words = buflen >> 1;
2942         u16 *buf16 = (u16 *) buf;
2943         void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2944
2945         /* Transfer multiple of 2 bytes */
2946         if (write_data) {
2947                 for (i = 0; i < words; i++)
2948                         writew(le16_to_cpu(buf16[i]), mmio);
2949         } else {
2950                 for (i = 0; i < words; i++)
2951                         buf16[i] = cpu_to_le16(readw(mmio));
2952         }
2953
2954         /* Transfer trailing 1 byte, if any. */
2955         if (unlikely(buflen & 0x01)) {
2956                 u16 align_buf[1] = { 0 };
2957                 unsigned char *trailing_buf = buf + buflen - 1;
2958
2959                 if (write_data) {
2960                         memcpy(align_buf, trailing_buf, 1);
2961                         writew(le16_to_cpu(align_buf[0]), mmio);
2962                 } else {
2963                         align_buf[0] = cpu_to_le16(readw(mmio));
2964                         memcpy(trailing_buf, align_buf, 1);
2965                 }
2966         }
2967 }
2968
2969 /**
2970  *      ata_pio_data_xfer - Transfer data by PIO
2971  *      @ap: port to read/write
2972  *      @buf: data buffer
2973  *      @buflen: buffer length
2974  *      @write_data: read/write
2975  *
2976  *      Transfer data from/to the device data register by PIO.
2977  *
2978  *      LOCKING:
2979  *      Inherited from caller.
2980  */
2981
2982 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2983                               unsigned int buflen, int write_data)
2984 {
2985         unsigned int words = buflen >> 1;
2986
2987         /* Transfer multiple of 2 bytes */
2988         if (write_data)
2989                 outsw(ap->ioaddr.data_addr, buf, words);
2990         else
2991                 insw(ap->ioaddr.data_addr, buf, words);
2992
2993         /* Transfer trailing 1 byte, if any. */
2994         if (unlikely(buflen & 0x01)) {
2995                 u16 align_buf[1] = { 0 };
2996                 unsigned char *trailing_buf = buf + buflen - 1;
2997
2998                 if (write_data) {
2999                         memcpy(align_buf, trailing_buf, 1);
3000                         outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3001                 } else {
3002                         align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3003                         memcpy(trailing_buf, align_buf, 1);
3004                 }
3005         }
3006 }
3007
3008 /**
3009  *      ata_data_xfer - Transfer data from/to the data register.
3010  *      @ap: port to read/write
3011  *      @buf: data buffer
3012  *      @buflen: buffer length
3013  *      @do_write: read/write
3014  *
3015  *      Transfer data from/to the device data register.
3016  *
3017  *      LOCKING:
3018  *      Inherited from caller.
3019  */
3020
3021 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3022                           unsigned int buflen, int do_write)
3023 {
3024         if (ap->flags & ATA_FLAG_MMIO)
3025                 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3026         else
3027                 ata_pio_data_xfer(ap, buf, buflen, do_write);
3028 }
3029
3030 /**
3031  *      ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3032  *      @qc: Command on going
3033  *
3034  *      Transfer ATA_SECT_SIZE of data from/to the ATA device.
3035  *
3036  *      LOCKING:
3037  *      Inherited from caller.
3038  */
3039
3040 static void ata_pio_sector(struct ata_queued_cmd *qc)
3041 {
3042         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3043         struct scatterlist *sg = qc->__sg;
3044         struct ata_port *ap = qc->ap;
3045         struct page *page;
3046         unsigned int offset;
3047         unsigned char *buf;
3048
3049         if (qc->cursect == (qc->nsect - 1))
3050                 ap->hsm_task_state = HSM_ST_LAST;
3051
3052         page = sg[qc->cursg].page;
3053         offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3054
3055         /* get the current page and offset */
3056         page = nth_page(page, (offset >> PAGE_SHIFT));
3057         offset %= PAGE_SIZE;
3058
3059         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3060
3061         if (PageHighMem(page)) {
3062                 unsigned long flags;
3063
3064                 local_irq_save(flags);
3065                 buf = kmap_atomic(page, KM_IRQ0);
3066
3067                 /* do the actual data transfer */
3068                 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3069
3070                 kunmap_atomic(buf, KM_IRQ0);
3071                 local_irq_restore(flags);
3072         } else {
3073                 buf = page_address(page);
3074                 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3075         }
3076
3077         qc->cursect++;
3078         qc->cursg_ofs++;
3079
3080         if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3081                 qc->cursg++;
3082                 qc->cursg_ofs = 0;
3083         }
3084 }
3085
3086 /**
3087  *      ata_pio_sectors - Transfer one or many 512-byte sectors.
3088  *      @qc: Command on going
3089  *
3090  *      Transfer one or many ATA_SECT_SIZE of data from/to the 
3091  *      ATA device for the DRQ request.
3092  *
3093  *      LOCKING:
3094  *      Inherited from caller.
3095  */
3096
3097 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3098 {
3099         if (is_multi_taskfile(&qc->tf)) {
3100                 /* READ/WRITE MULTIPLE */
3101                 unsigned int nsect;
3102
3103                 assert(qc->dev->multi_count);
3104
3105                 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3106                 while (nsect--)
3107                         ata_pio_sector(qc);
3108         } else
3109                 ata_pio_sector(qc);
3110 }
3111
3112 /**
3113  *      atapi_send_cdb - Write CDB bytes to hardware
3114  *      @ap: Port to which ATAPI device is attached.
3115  *      @qc: Taskfile currently active
3116  *
3117  *      When device has indicated its readiness to accept
3118  *      a CDB, this function is called.  Send the CDB.
3119  *
3120  *      LOCKING:
3121  *      caller.
3122  */
3123
3124 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3125 {
3126         /* send SCSI cdb */
3127         DPRINTK("send cdb\n");
3128         assert(ap->cdb_len >= 12);
3129
3130         ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3131         ata_altstatus(ap); /* flush */
3132
3133         switch (qc->tf.protocol) {
3134         case ATA_PROT_ATAPI:
3135                 ap->hsm_task_state = HSM_ST;
3136                 break;
3137         case ATA_PROT_ATAPI_NODATA:
3138                 ap->hsm_task_state = HSM_ST_LAST;
3139                 break;
3140         case ATA_PROT_ATAPI_DMA:
3141                 ap->hsm_task_state = HSM_ST_LAST;
3142                 /* initiate bmdma */
3143                 ap->ops->bmdma_start(qc);
3144                 break;
3145         }
3146 }
3147
3148 /**
3149  *      ata_pio_first_block - Write first data block to hardware
3150  *      @ap: Port to which ATA/ATAPI device is attached.
3151  *
3152  *      When device has indicated its readiness to accept
3153  *      the data, this function sends out the CDB or 
3154  *      the first data block by PIO.
3155  *      After this, 
3156  *        - If polling, ata_pio_task() handles the rest.
3157  *        - Otherwise, interrupt handler takes over.
3158  *
3159  *      LOCKING:
3160  *      Kernel thread context (may sleep)
3161  *
3162  *      RETURNS:
3163  *      Zero if irq handler takes over
3164  *      Non-zero if has next (polling).
3165  */
3166
3167 static int ata_pio_first_block(struct ata_port *ap)
3168 {
3169         struct ata_queued_cmd *qc;
3170         u8 status;
3171         unsigned long flags;
3172         int has_next;
3173
3174         qc = ata_qc_from_tag(ap, ap->active_tag);
3175         assert(qc != NULL);
3176         assert(qc->flags & ATA_QCFLAG_ACTIVE);
3177
3178         /* if polling, we will stay in the work queue after sending the data.
3179          * otherwise, interrupt handler takes over after sending the data.
3180          */
3181         has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3182
3183         /* sleep-wait for BSY to clear */
3184         DPRINTK("busy wait\n");
3185         if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3186                 ap->hsm_task_state = HSM_ST_TMOUT;
3187                 goto err_out;
3188         }
3189
3190         /* make sure DRQ is set */
3191         status = ata_chk_status(ap);
3192         if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3193                 /* device status error */
3194                 ap->hsm_task_state = HSM_ST_ERR;
3195                 goto err_out;
3196         }
3197
3198         /* Send the CDB (atapi) or the first data block (ata pio out).
3199          * During the state transition, interrupt handler shouldn't
3200          * be invoked before the data transfer is complete and
3201          * hsm_task_state is changed. Hence, the following locking.
3202          */
3203         spin_lock_irqsave(&ap->host_set->lock, flags);
3204
3205         if (qc->tf.protocol == ATA_PROT_PIO) {
3206                 /* PIO data out protocol.
3207                  * send first data block.
3208                  */
3209
3210                 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3211                  * so, the state is changed here before ata_pio_sectors().
3212                  */
3213                 ap->hsm_task_state = HSM_ST;
3214                 ata_pio_sectors(qc);
3215                 ata_altstatus(ap); /* flush */
3216         } else
3217                 /* send CDB */
3218                 atapi_send_cdb(ap, qc);
3219
3220         spin_unlock_irqrestore(&ap->host_set->lock, flags);
3221
3222         /* if polling, ata_pio_task() handles the rest.
3223          * otherwise, interrupt handler takes over from here.
3224          */
3225         return has_next;
3226
3227 err_out:
3228         return 1; /* has next */
3229 }
3230
3231 /**
3232  *      __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3233  *      @qc: Command on going
3234  *      @bytes: number of bytes
3235  *
3236  *      Transfer Transfer data from/to the ATAPI device.
3237  *
3238  *      LOCKING:
3239  *      Inherited from caller.
3240  *
3241  */
3242
3243 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3244 {
3245         int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3246         struct scatterlist *sg = qc->__sg;
3247         struct ata_port *ap = qc->ap;
3248         struct page *page;
3249         unsigned char *buf;
3250         unsigned int offset, count;
3251
3252         if (qc->curbytes + bytes >= qc->nbytes)
3253                 ap->hsm_task_state = HSM_ST_LAST;
3254
3255 next_sg:
3256         if (unlikely(qc->cursg >= qc->n_elem)) {
3257                 /*
3258                  * The end of qc->sg is reached and the device expects
3259                  * more data to transfer. In order not to overrun qc->sg
3260                  * and fulfill length specified in the byte count register,
3261                  *    - for read case, discard trailing data from the device
3262                  *    - for write case, padding zero data to the device
3263                  */
3264                 u16 pad_buf[1] = { 0 };
3265                 unsigned int words = bytes >> 1;
3266                 unsigned int i;
3267
3268                 if (words) /* warning if bytes > 1 */
3269                         printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3270                                ap->id, bytes);
3271
3272                 for (i = 0; i < words; i++)
3273                         ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3274
3275                 ap->hsm_task_state = HSM_ST_LAST;
3276                 return;
3277         }
3278
3279         sg = &qc->__sg[qc->cursg];
3280
3281         page = sg->page;
3282         offset = sg->offset + qc->cursg_ofs;
3283
3284         /* get the current page and offset */
3285         page = nth_page(page, (offset >> PAGE_SHIFT));
3286         offset %= PAGE_SIZE;
3287
3288         /* don't overrun current sg */
3289         count = min(sg->length - qc->cursg_ofs, bytes);
3290
3291         /* don't cross page boundaries */
3292         count = min(count, (unsigned int)PAGE_SIZE - offset);
3293
3294         DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3295
3296         if (PageHighMem(page)) {
3297                 unsigned long flags;
3298
3299                 local_irq_save(flags);
3300                 buf = kmap_atomic(page, KM_IRQ0);
3301
3302                 /* do the actual data transfer */
3303                 ata_data_xfer(ap, buf + offset, count, do_write);
3304
3305                 kunmap_atomic(buf, KM_IRQ0);
3306                 local_irq_restore(flags);
3307         } else {
3308                 buf = page_address(page);
3309                 ata_data_xfer(ap, buf + offset, count, do_write);
3310         }
3311
3312         bytes -= count;
3313         qc->curbytes += count;
3314         qc->cursg_ofs += count;
3315
3316         if (qc->cursg_ofs == sg->length) {
3317                 qc->cursg++;
3318                 qc->cursg_ofs = 0;
3319         }
3320
3321         if (bytes)
3322                 goto next_sg;
3323 }
3324
3325 /**
3326  *      atapi_pio_bytes - Transfer data from/to the ATAPI device.
3327  *      @qc: Command on going
3328  *
3329  *      Transfer Transfer data from/to the ATAPI device.
3330  *
3331  *      LOCKING:
3332  *      Inherited from caller.
3333  */
3334
3335 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3336 {
3337         struct ata_port *ap = qc->ap;
3338         struct ata_device *dev = qc->dev;
3339         unsigned int ireason, bc_lo, bc_hi, bytes;
3340         int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3341
3342         ap->ops->tf_read(ap, &qc->tf);
3343         ireason = qc->tf.nsect;
3344         bc_lo = qc->tf.lbam;
3345         bc_hi = qc->tf.lbah;
3346         bytes = (bc_hi << 8) | bc_lo;
3347
3348         /* shall be cleared to zero, indicating xfer of data */
3349         if (ireason & (1 << 0))
3350                 goto err_out;
3351
3352         /* make sure transfer direction matches expected */
3353         i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3354         if (do_write != i_write)
3355                 goto err_out;
3356
3357         VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3358
3359         __atapi_pio_bytes(qc, bytes);
3360
3361         return;
3362
3363 err_out:
3364         printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3365               ap->id, dev->devno);
3366         ap->hsm_task_state = HSM_ST_ERR;
3367 }
3368
3369 /**
3370  *      ata_pio_block - start PIO on a block
3371  *      @ap: the target ata_port
3372  *
3373  *      LOCKING:
3374  *      None.  (executing in kernel thread context)
3375  */
3376
3377 static void ata_pio_block(struct ata_port *ap)
3378 {
3379         struct ata_queued_cmd *qc;
3380         u8 status;
3381
3382         /*
3383          * This is purely heuristic.  This is a fast path.
3384          * Sometimes when we enter, BSY will be cleared in
3385          * a chk-status or two.  If not, the drive is probably seeking
3386          * or something.  Snooze for a couple msecs, then
3387          * chk-status again.  If still busy, fall back to
3388          * HSM_ST_POLL state.
3389          */
3390         status = ata_busy_wait(ap, ATA_BUSY, 5);
3391         if (status & ATA_BUSY) {
3392                 msleep(2);
3393                 status = ata_busy_wait(ap, ATA_BUSY, 10);
3394                 if (status & ATA_BUSY) {
3395                         ap->hsm_task_state = HSM_ST_POLL;
3396                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3397                         return;
3398                 }
3399         }
3400
3401         qc = ata_qc_from_tag(ap, ap->active_tag);
3402         assert(qc != NULL);
3403
3404         if (is_atapi_taskfile(&qc->tf)) {
3405                 /* no more data to transfer or unsupported ATAPI command */
3406                 if ((status & ATA_DRQ) == 0) {
3407                         ap->hsm_task_state = HSM_ST_LAST;
3408                         return;
3409                 }
3410
3411                 atapi_pio_bytes(qc);
3412         } else {
3413                 /* handle BSY=0, DRQ=0 as error */
3414                 if ((status & ATA_DRQ) == 0) {
3415                         ap->hsm_task_state = HSM_ST_ERR;
3416                         return;
3417                 }
3418
3419                 ata_pio_sectors(qc);
3420         }
3421
3422         ata_altstatus(ap); /* flush */
3423 }
3424
3425 static void ata_pio_error(struct ata_port *ap)
3426 {
3427         struct ata_queued_cmd *qc;
3428
3429         printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3430
3431         qc = ata_qc_from_tag(ap, ap->active_tag);
3432         assert(qc != NULL);
3433
3434         ap->hsm_task_state = HSM_ST_IDLE;
3435
3436         ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3437 }
3438
3439 static void ata_pio_task(void *_data)
3440 {
3441         struct ata_port *ap = _data;
3442         unsigned long timeout;
3443         int has_next;
3444
3445 fsm_start:
3446         timeout = 0;
3447         has_next = 1;
3448
3449         switch (ap->hsm_task_state) {
3450         case HSM_ST_FIRST:
3451                 has_next = ata_pio_first_block(ap);
3452                 break;
3453
3454         case HSM_ST:
3455                 ata_pio_block(ap);
3456                 break;
3457
3458         case HSM_ST_LAST:
3459                 has_next = ata_pio_complete(ap);
3460                 break;
3461
3462         case HSM_ST_POLL:
3463         case HSM_ST_LAST_POLL:
3464                 timeout = ata_pio_poll(ap);
3465                 break;
3466
3467         case HSM_ST_TMOUT:
3468         case HSM_ST_ERR:
3469                 ata_pio_error(ap);
3470                 return;
3471
3472         default:
3473                 BUG();
3474                 return;
3475         }
3476
3477         if (timeout)
3478                 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3479         else if (has_next)
3480                 goto fsm_start;
3481 }
3482
3483 /**
3484  *      ata_qc_timeout - Handle timeout of queued command
3485  *      @qc: Command that timed out
3486  *
3487  *      Some part of the kernel (currently, only the SCSI layer)
3488  *      has noticed that the active command on port @ap has not
3489  *      completed after a specified length of time.  Handle this
3490  *      condition by disabling DMA (if necessary) and completing
3491  *      transactions, with error if necessary.
3492  *
3493  *      This also handles the case of the "lost interrupt", where
3494  *      for some reason (possibly hardware bug, possibly driver bug)
3495  *      an interrupt was not delivered to the driver, even though the
3496  *      transaction completed successfully.
3497  *
3498  *      LOCKING:
3499  *      Inherited from SCSI layer (none, can sleep)
3500  */
3501
3502 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3503 {
3504         struct ata_port *ap = qc->ap;
3505         struct ata_host_set *host_set = ap->host_set;
3506         u8 host_stat = 0, drv_stat;
3507         unsigned long flags;
3508
3509         DPRINTK("ENTER\n");
3510
3511         spin_lock_irqsave(&host_set->lock, flags);
3512
3513         /* hack alert!  We cannot use the supplied completion
3514          * function from inside the ->eh_strategy_handler() thread.
3515          * libata is the only user of ->eh_strategy_handler() in
3516          * any kernel, so the default scsi_done() assumes it is
3517          * not being called from the SCSI EH.
3518          */
3519         qc->scsidone = scsi_finish_command;
3520
3521         switch (qc->tf.protocol) {
3522
3523         case ATA_PROT_DMA:
3524         case ATA_PROT_ATAPI_DMA:
3525                 host_stat = ap->ops->bmdma_status(ap);
3526
3527                 /* before we do anything else, clear DMA-Start bit */
3528                 ap->ops->bmdma_stop(qc);
3529
3530                 /* fall through */
3531
3532         default:
3533                 ata_altstatus(ap);
3534                 drv_stat = ata_chk_status(ap);
3535
3536                 /* ack bmdma irq events */
3537                 ap->ops->irq_clear(ap);
3538
3539                 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3540                        ap->id, qc->tf.command, drv_stat, host_stat);
3541
3542                 ap->hsm_task_state = HSM_ST_IDLE;
3543
3544                 /* complete taskfile transaction */
3545                 ata_qc_complete(qc, ac_err_mask(drv_stat));
3546                 break;
3547         }
3548
3549         spin_unlock_irqrestore(&host_set->lock, flags);
3550
3551         DPRINTK("EXIT\n");
3552 }
3553
3554 /**
3555  *      ata_eng_timeout - Handle timeout of queued command
3556  *      @ap: Port on which timed-out command is active
3557  *
3558  *      Some part of the kernel (currently, only the SCSI layer)
3559  *      has noticed that the active command on port @ap has not
3560  *      completed after a specified length of time.  Handle this
3561  *      condition by disabling DMA (if necessary) and completing
3562  *      transactions, with error if necessary.
3563  *
3564  *      This also handles the case of the "lost interrupt", where
3565  *      for some reason (possibly hardware bug, possibly driver bug)
3566  *      an interrupt was not delivered to the driver, even though the
3567  *      transaction completed successfully.
3568  *
3569  *      LOCKING:
3570  *      Inherited from SCSI layer (none, can sleep)
3571  */
3572
3573 void ata_eng_timeout(struct ata_port *ap)
3574 {
3575         struct ata_queued_cmd *qc;
3576
3577         DPRINTK("ENTER\n");
3578
3579         qc = ata_qc_from_tag(ap, ap->active_tag);
3580         if (qc)
3581                 ata_qc_timeout(qc);
3582         else {
3583                 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3584                        ap->id);
3585                 goto out;
3586         }
3587
3588 out:
3589         DPRINTK("EXIT\n");
3590 }
3591
3592 /**
3593  *      ata_qc_new - Request an available ATA command, for queueing
3594  *      @ap: Port associated with device @dev
3595  *      @dev: Device from whom we request an available command structure
3596  *
3597  *      LOCKING:
3598  *      None.
3599  */
3600
3601 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3602 {
3603         struct ata_queued_cmd *qc = NULL;
3604         unsigned int i;
3605
3606         for (i = 0; i < ATA_MAX_QUEUE; i++)
3607                 if (!test_and_set_bit(i, &ap->qactive)) {
3608                         qc = ata_qc_from_tag(ap, i);
3609                         break;
3610                 }
3611
3612         if (qc)
3613                 qc->tag = i;
3614
3615         return qc;
3616 }
3617
3618 /**
3619  *      ata_qc_new_init - Request an available ATA command, and initialize it
3620  *      @ap: Port associated with device @dev
3621  *      @dev: Device from whom we request an available command structure
3622  *
3623  *      LOCKING:
3624  *      None.
3625  */
3626
3627 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3628                                       struct ata_device *dev)
3629 {
3630         struct ata_queued_cmd *qc;
3631
3632         qc = ata_qc_new(ap);
3633         if (qc) {
3634                 qc->scsicmd = NULL;
3635                 qc->ap = ap;
3636                 qc->dev = dev;
3637
3638                 ata_qc_reinit(qc);
3639         }
3640
3641         return qc;
3642 }
3643
3644 int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
3645 {
3646         return 0;
3647 }
3648
3649 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3650 {
3651         struct ata_port *ap = qc->ap;
3652         unsigned int tag, do_clear = 0;
3653
3654         qc->flags = 0;
3655         tag = qc->tag;
3656         if (likely(ata_tag_valid(tag))) {
3657                 if (tag == ap->active_tag)
3658                         ap->active_tag = ATA_TAG_POISON;
3659                 qc->tag = ATA_TAG_POISON;
3660                 do_clear = 1;
3661         }
3662
3663         if (qc->waiting) {
3664                 struct completion *waiting = qc->waiting;
3665                 qc->waiting = NULL;
3666                 complete(waiting);
3667         }
3668
3669         if (likely(do_clear))
3670                 clear_bit(tag, &ap->qactive);
3671 }
3672
3673 /**
3674  *      ata_qc_free - free unused ata_queued_cmd
3675  *      @qc: Command to complete
3676  *
3677  *      Designed to free unused ata_queued_cmd object
3678  *      in case something prevents using it.
3679  *
3680  *      LOCKING:
3681  *      spin_lock_irqsave(host_set lock)
3682  */
3683 void ata_qc_free(struct ata_queued_cmd *qc)
3684 {
3685         assert(qc != NULL);     /* ata_qc_from_tag _might_ return NULL */
3686         assert(qc->waiting == NULL);    /* nothing should be waiting */
3687
3688         __ata_qc_complete(qc);
3689 }
3690
3691 /**
3692  *      ata_qc_complete - Complete an active ATA command
3693  *      @qc: Command to complete
3694  *      @err_mask: ATA Status register contents
3695  *
3696  *      Indicate to the mid and upper layers that an ATA
3697  *      command has completed, with either an ok or not-ok status.
3698  *
3699  *      LOCKING:
3700  *      spin_lock_irqsave(host_set lock)
3701  */
3702
3703 void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
3704 {
3705         int rc;
3706
3707         assert(qc != NULL);     /* ata_qc_from_tag _might_ return NULL */
3708         assert(qc->flags & ATA_QCFLAG_ACTIVE);
3709
3710         if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3711                 ata_sg_clean(qc);
3712
3713         /* atapi: mark qc as inactive to prevent the interrupt handler
3714          * from completing the command twice later, before the error handler
3715          * is called. (when rc != 0 and atapi request sense is needed)
3716          */
3717         qc->flags &= ~ATA_QCFLAG_ACTIVE;
3718
3719         /* call completion callback */
3720         rc = qc->complete_fn(qc, err_mask);
3721
3722         /* if callback indicates not to complete command (non-zero),
3723          * return immediately
3724          */
3725         if (rc != 0)
3726                 return;
3727
3728         __ata_qc_complete(qc);
3729
3730         VPRINTK("EXIT\n");
3731 }
3732
3733 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3734 {
3735         struct ata_port *ap = qc->ap;
3736
3737         switch (qc->tf.protocol) {
3738         case ATA_PROT_DMA:
3739         case ATA_PROT_ATAPI_DMA:
3740                 return 1;
3741
3742         case ATA_PROT_ATAPI:
3743         case ATA_PROT_PIO:
3744         case ATA_PROT_PIO_MULT:
3745                 if (ap->flags & ATA_FLAG_PIO_DMA)
3746                         return 1;
3747
3748                 /* fall through */
3749
3750         default:
3751                 return 0;
3752         }
3753
3754         /* never reached */
3755 }
3756
3757 /**
3758  *      ata_qc_issue - issue taskfile to device
3759  *      @qc: command to issue to device
3760  *
3761  *      Prepare an ATA command to submission to device.
3762  *      This includes mapping the data into a DMA-able
3763  *      area, filling in the S/G table, and finally
3764  *      writing the taskfile to hardware, starting the command.
3765  *
3766  *      LOCKING:
3767  *      spin_lock_irqsave(host_set lock)
3768  *
3769  *      RETURNS:
3770  *      Zero on success, negative on error.
3771  */
3772
3773 int ata_qc_issue(struct ata_queued_cmd *qc)
3774 {
3775         struct ata_port *ap = qc->ap;
3776
3777         if (ata_should_dma_map(qc)) {
3778                 if (qc->flags & ATA_QCFLAG_SG) {
3779                         if (ata_sg_setup(qc))
3780                                 goto err_out;
3781                 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3782                         if (ata_sg_setup_one(qc))
3783                                 goto err_out;
3784                 }
3785         } else {
3786                 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3787         }
3788
3789         ap->ops->qc_prep(qc);
3790
3791         qc->ap->active_tag = qc->tag;
3792         qc->flags |= ATA_QCFLAG_ACTIVE;
3793
3794         return ap->ops->qc_issue(qc);
3795
3796 err_out:
3797         return -1;
3798 }
3799
3800
3801 /**
3802  *      ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3803  *      @qc: command to issue to device
3804  *
3805  *      Using various libata functions and hooks, this function
3806  *      starts an ATA command.  ATA commands are grouped into
3807  *      classes called "protocols", and issuing each type of protocol
3808  *      is slightly different.
3809  *
3810  *      May be used as the qc_issue() entry in ata_port_operations.
3811  *
3812  *      LOCKING:
3813  *      spin_lock_irqsave(host_set lock)
3814  *
3815  *      RETURNS:
3816  *      Zero on success, negative on error.
3817  */
3818
3819 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3820 {
3821         struct ata_port *ap = qc->ap;
3822
3823         /* Use polling pio if the LLD doesn't handle
3824          * interrupt driven pio and atapi CDB interrupt.
3825          */
3826         if (ap->flags & ATA_FLAG_PIO_POLLING) {
3827                 switch (qc->tf.protocol) {
3828                 case ATA_PROT_PIO:
3829                 case ATA_PROT_ATAPI:
3830                 case ATA_PROT_ATAPI_NODATA:
3831                         qc->tf.flags |= ATA_TFLAG_POLLING;
3832                         break;
3833                 case ATA_PROT_ATAPI_DMA:
3834                         if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3835                                 BUG();
3836                         break;
3837                 default:
3838                         break;
3839                 }
3840         }
3841
3842         /* select the device */
3843         ata_dev_select(ap, qc->dev->devno, 1, 0);
3844
3845         /* start the command */
3846         switch (qc->tf.protocol) {
3847         case ATA_PROT_NODATA:
3848                 if (qc->tf.flags & ATA_TFLAG_POLLING)
3849                         ata_qc_set_polling(qc);
3850
3851                 ata_tf_to_host(ap, &qc->tf);
3852                 ap->hsm_task_state = HSM_ST_LAST;
3853
3854                 if (qc->tf.flags & ATA_TFLAG_POLLING)
3855                         queue_work(ata_wq, &ap->pio_task);
3856
3857                 break;
3858
3859         case ATA_PROT_DMA:
3860                 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3861
3862                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
3863                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
3864                 ap->ops->bmdma_start(qc);           /* initiate bmdma */
3865                 ap->hsm_task_state = HSM_ST_LAST;
3866                 break;
3867
3868         case ATA_PROT_PIO:
3869                 if (qc->tf.flags & ATA_TFLAG_POLLING)
3870                         ata_qc_set_polling(qc);
3871
3872                 ata_tf_to_host(ap, &qc->tf);
3873
3874                 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3875                         /* PIO data out protocol */
3876                         ap->hsm_task_state = HSM_ST_FIRST;
3877                         queue_work(ata_wq, &ap->pio_task);
3878
3879                         /* always send first data block using
3880                          * the ata_pio_task() codepath.
3881                          */
3882                 } else {
3883                         /* PIO data in protocol */
3884                         ap->hsm_task_state = HSM_ST;
3885
3886                         if (qc->tf.flags & ATA_TFLAG_POLLING)
3887                                 queue_work(ata_wq, &ap->pio_task);
3888
3889                         /* if polling, ata_pio_task() handles the rest.
3890                          * otherwise, interrupt handler takes over from here.
3891                          */
3892                 }
3893
3894                 break;
3895
3896         case ATA_PROT_ATAPI:
3897         case ATA_PROT_ATAPI_NODATA:
3898                 if (qc->tf.flags & ATA_TFLAG_POLLING)
3899                         ata_qc_set_polling(qc);
3900
3901                 ata_tf_to_host(ap, &qc->tf);
3902                 ap->hsm_task_state = HSM_ST_FIRST;
3903
3904                 /* send cdb by polling if no cdb interrupt */
3905                 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3906                     (qc->tf.flags & ATA_TFLAG_POLLING))
3907                         queue_work(ata_wq, &ap->pio_task);
3908                 break;
3909
3910         case ATA_PROT_ATAPI_DMA:
3911                 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3912
3913                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
3914                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
3915                 ap->hsm_task_state = HSM_ST_FIRST;
3916
3917                 /* send cdb by polling if no cdb interrupt */
3918                 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3919                         queue_work(ata_wq, &ap->pio_task);
3920                 break;
3921
3922         default:
3923                 WARN_ON(1);
3924                 return -1;
3925         }
3926
3927         return 0;
3928 }
3929
3930 /**
3931  *      ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3932  *      @qc: Info associated with this ATA transaction.
3933  *
3934  *      LOCKING:
3935  *      spin_lock_irqsave(host_set lock)
3936  */
3937
3938 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3939 {
3940         struct ata_port *ap = qc->ap;
3941         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3942         u8 dmactl;
3943         void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3944
3945         /* load PRD table addr. */
3946         mb();   /* make sure PRD table writes are visible to controller */
3947         writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3948
3949         /* specify data direction, triple-check start bit is clear */
3950         dmactl = readb(mmio + ATA_DMA_CMD);
3951         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3952         if (!rw)
3953                 dmactl |= ATA_DMA_WR;
3954         writeb(dmactl, mmio + ATA_DMA_CMD);
3955
3956         /* issue r/w command */
3957         ap->ops->exec_command(ap, &qc->tf);
3958 }
3959
3960 /**
3961  *      ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3962  *      @qc: Info associated with this ATA transaction.
3963  *
3964  *      LOCKING:
3965  *      spin_lock_irqsave(host_set lock)
3966  */
3967
3968 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3969 {
3970         struct ata_port *ap = qc->ap;
3971         void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3972         u8 dmactl;
3973
3974         /* start host DMA transaction */
3975         dmactl = readb(mmio + ATA_DMA_CMD);
3976         writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3977
3978         /* Strictly, one may wish to issue a readb() here, to
3979          * flush the mmio write.  However, control also passes
3980          * to the hardware at this point, and it will interrupt
3981          * us when we are to resume control.  So, in effect,
3982          * we don't care when the mmio write flushes.
3983          * Further, a read of the DMA status register _immediately_
3984          * following the write may not be what certain flaky hardware
3985          * is expected, so I think it is best to not add a readb()
3986          * without first all the MMIO ATA cards/mobos.
3987          * Or maybe I'm just being paranoid.
3988          */
3989 }
3990
3991 /**
3992  *      ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3993  *      @qc: Info associated with this ATA transaction.
3994  *
3995  *      LOCKING:
3996  *      spin_lock_irqsave(host_set lock)
3997  */
3998
3999 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4000 {
4001         struct ata_port *ap = qc->ap;
4002         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4003         u8 dmactl;
4004
4005         /* load PRD table addr. */
4006         outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4007
4008         /* specify data direction, triple-check start bit is clear */
4009         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4010         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4011         if (!rw)
4012                 dmactl |= ATA_DMA_WR;
4013         outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4014
4015         /* issue r/w command */
4016         ap->ops->exec_command(ap, &qc->tf);
4017 }
4018
4019 /**
4020  *      ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4021  *      @qc: Info associated with this ATA transaction.
4022  *
4023  *      LOCKING:
4024  *      spin_lock_irqsave(host_set lock)
4025  */
4026
4027 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4028 {
4029         struct ata_port *ap = qc->ap;
4030         u8 dmactl;
4031
4032         /* start host DMA transaction */
4033         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4034         outb(dmactl | ATA_DMA_START,
4035              ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4036 }
4037
4038
4039 /**
4040  *      ata_bmdma_start - Start a PCI IDE BMDMA transaction
4041  *      @qc: Info associated with this ATA transaction.
4042  *
4043  *      Writes the ATA_DMA_START flag to the DMA command register.
4044  *
4045  *      May be used as the bmdma_start() entry in ata_port_operations.
4046  *
4047  *      LOCKING:
4048  *      spin_lock_irqsave(host_set lock)
4049  */
4050 void ata_bmdma_start(struct ata_queued_cmd *qc)
4051 {
4052         if (qc->ap->flags & ATA_FLAG_MMIO)
4053                 ata_bmdma_start_mmio(qc);
4054         else
4055                 ata_bmdma_start_pio(qc);
4056 }
4057
4058
4059 /**
4060  *      ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4061  *      @qc: Info associated with this ATA transaction.
4062  *
4063  *      Writes address of PRD table to device's PRD Table Address
4064  *      register, sets the DMA control register, and calls
4065  *      ops->exec_command() to start the transfer.
4066  *
4067  *      May be used as the bmdma_setup() entry in ata_port_operations.
4068  *
4069  *      LOCKING:
4070  *      spin_lock_irqsave(host_set lock)
4071  */
4072 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4073 {
4074         if (qc->ap->flags & ATA_FLAG_MMIO)
4075                 ata_bmdma_setup_mmio(qc);
4076         else
4077                 ata_bmdma_setup_pio(qc);
4078 }
4079
4080
4081 /**
4082  *      ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4083  *      @ap: Port associated with this ATA transaction.
4084  *
4085  *      Clear interrupt and error flags in DMA status register.
4086  *
4087  *      May be used as the irq_clear() entry in ata_port_operations.
4088  *
4089  *      LOCKING:
4090  *      spin_lock_irqsave(host_set lock)
4091  */
4092
4093 void ata_bmdma_irq_clear(struct ata_port *ap)
4094 {
4095     if (ap->flags & ATA_FLAG_MMIO) {
4096         void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4097         writeb(readb(mmio), mmio);
4098     } else {
4099         unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4100         outb(inb(addr), addr);
4101     }
4102
4103 }
4104
4105
4106 /**
4107  *      ata_bmdma_status - Read PCI IDE BMDMA status
4108  *      @ap: Port associated with this ATA transaction.
4109  *
4110  *      Read and return BMDMA status register.
4111  *
4112  *      May be used as the bmdma_status() entry in ata_port_operations.
4113  *
4114  *      LOCKING:
4115  *      spin_lock_irqsave(host_set lock)
4116  */
4117
4118 u8 ata_bmdma_status(struct ata_port *ap)
4119 {
4120         u8 host_stat;
4121         if (ap->flags & ATA_FLAG_MMIO) {
4122                 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4123                 host_stat = readb(mmio + ATA_DMA_STATUS);
4124         } else
4125                 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4126         return host_stat;
4127 }
4128
4129
4130 /**
4131  *      ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4132  *      @qc: Command we are ending DMA for
4133  *
4134  *      Clears the ATA_DMA_START flag in the dma control register
4135  *
4136  *      May be used as the bmdma_stop() entry in ata_port_operations.
4137  *
4138  *      LOCKING:
4139  *      spin_lock_irqsave(host_set lock)
4140  */
4141
4142 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4143 {
4144         struct ata_port *ap = qc->ap;
4145         if (ap->flags & ATA_FLAG_MMIO) {
4146                 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4147
4148                 /* clear start/stop bit */
4149                 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4150                         mmio + ATA_DMA_CMD);
4151         } else {
4152                 /* clear start/stop bit */
4153                 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4154                         ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4155         }
4156
4157         /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4158         ata_altstatus(ap);        /* dummy read */
4159 }
4160
4161 /**
4162  *      ata_host_intr - Handle host interrupt for given (port, task)
4163  *      @ap: Port on which interrupt arrived (possibly...)
4164  *      @qc: Taskfile currently active in engine
4165  *
4166  *      Handle host interrupt for given queued command.  Currently,
4167  *      only DMA interrupts are handled.  All other commands are
4168  *      handled via polling with interrupts disabled (nIEN bit).
4169  *
4170  *      LOCKING:
4171  *      spin_lock_irqsave(host_set lock)
4172  *
4173  *      RETURNS:
4174  *      One if interrupt was handled, zero if not (shared irq).
4175  */
4176
4177 inline unsigned int ata_host_intr (struct ata_port *ap,
4178                                    struct ata_queued_cmd *qc)
4179 {
4180         u8 status, host_stat = 0;
4181
4182         VPRINTK("ata%u: protocol %d task_state %d\n",
4183                 ap->id, qc->tf.protocol, ap->hsm_task_state);
4184
4185         /* Check whether we are expecting interrupt in this state */
4186         switch (ap->hsm_task_state) {
4187         case HSM_ST_FIRST:
4188                 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4189                  * The flag was turned on only for atapi devices.
4190                  * No need to check is_atapi_taskfile(&qc->tf) again.
4191                  */
4192                 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4193                         goto idle_irq;
4194                 break;
4195         case HSM_ST_LAST:
4196                 if (qc->tf.protocol == ATA_PROT_DMA ||
4197                     qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4198                         /* check status of DMA engine */
4199                         host_stat = ap->ops->bmdma_status(ap);
4200                         VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4201
4202                         /* if it's not our irq... */
4203                         if (!(host_stat & ATA_DMA_INTR))
4204                                 goto idle_irq;
4205
4206                         /* before we do anything else, clear DMA-Start bit */
4207                         ap->ops->bmdma_stop(qc);
4208                 }
4209                 break;
4210         case HSM_ST:
4211                 break;
4212         default:
4213                 goto idle_irq;
4214         }
4215
4216         /* check altstatus */
4217         status = ata_altstatus(ap);
4218         if (status & ATA_BUSY)
4219                 goto idle_irq;
4220
4221         /* check main status, clearing INTRQ */
4222         status = ata_chk_status(ap);
4223         if (unlikely(status & ATA_BUSY))
4224                 goto idle_irq;
4225
4226         DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4227                 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4228
4229         /* ack bmdma irq events */
4230         ap->ops->irq_clear(ap);
4231
4232         /* check error */
4233         if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4234                 ap->hsm_task_state = HSM_ST_ERR;
4235
4236 fsm_start:
4237         switch (ap->hsm_task_state) {
4238         case HSM_ST_FIRST:
4239                 /* Some pre-ATAPI-4 devices assert INTRQ 
4240                  * at this state when ready to receive CDB.
4241                  */
4242
4243                 /* check device status */
4244                 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4245                         /* Wrong status. Let EH handle this */
4246                         ap->hsm_task_state = HSM_ST_ERR;
4247                         goto fsm_start;
4248                 }
4249
4250                 atapi_send_cdb(ap, qc);
4251
4252                 break;
4253
4254         case HSM_ST:
4255                 /* complete command or read/write the data register */
4256                 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4257                         /* ATAPI PIO protocol */
4258                         if ((status & ATA_DRQ) == 0) {
4259                                 /* no more data to transfer */
4260                                 ap->hsm_task_state = HSM_ST_LAST;
4261                                 goto fsm_start;
4262                         }
4263                         
4264                         atapi_pio_bytes(qc);
4265
4266                         if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4267                                 /* bad ireason reported by device */
4268                                 goto fsm_start;
4269
4270                 } else {
4271                         /* ATA PIO protocol */
4272                         if (unlikely((status & ATA_DRQ) == 0)) {
4273                                 /* handle BSY=0, DRQ=0 as error */
4274                                 ap->hsm_task_state = HSM_ST_ERR;
4275                                 goto fsm_start;
4276                         }
4277
4278                         ata_pio_sectors(qc);
4279
4280                         if (ap->hsm_task_state == HSM_ST_LAST &&
4281                             (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4282                                 /* all data read */
4283                                 ata_altstatus(ap);
4284                                 status = ata_chk_status(ap);
4285                                 goto fsm_start;
4286                         }
4287                 }
4288
4289                 ata_altstatus(ap); /* flush */
4290                 break;
4291
4292         case HSM_ST_LAST:
4293                 if (unlikely(status & ATA_DRQ)) {
4294                         /* handle DRQ=1 as error */
4295                         ap->hsm_task_state = HSM_ST_ERR;
4296                         goto fsm_start;
4297                 }
4298
4299                 /* no more data to transfer */
4300                 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4301                         ap->id, status);
4302
4303                 ap->hsm_task_state = HSM_ST_IDLE;
4304
4305                 /* complete taskfile transaction */
4306                 ata_qc_complete(qc, ac_err_mask(status));
4307                 break;
4308
4309         case HSM_ST_ERR:
4310                 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4311                        ap->id, status, host_stat);
4312
4313                 ap->hsm_task_state = HSM_ST_IDLE;
4314                 ata_qc_complete(qc, status | ATA_ERR);
4315                 break;
4316         default:
4317                 goto idle_irq;
4318         }
4319
4320         return 1;       /* irq handled */
4321
4322 idle_irq:
4323         ap->stats.idle_irq++;
4324
4325 #ifdef ATA_IRQ_TRAP
4326         if ((ap->stats.idle_irq % 1000) == 0) {
4327                 handled = 1;
4328                 ata_irq_ack(ap, 0); /* debug trap */
4329                 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4330         }
4331 #endif
4332         return 0;       /* irq not handled */
4333 }
4334
4335 /**
4336  *      ata_interrupt - Default ATA host interrupt handler
4337  *      @irq: irq line (unused)
4338  *      @dev_instance: pointer to our ata_host_set information structure
4339  *      @regs: unused
4340  *
4341  *      Default interrupt handler for PCI IDE devices.  Calls
4342  *      ata_host_intr() for each port that is not disabled.
4343  *
4344  *      LOCKING:
4345  *      Obtains host_set lock during operation.
4346  *
4347  *      RETURNS:
4348  *      IRQ_NONE or IRQ_HANDLED.
4349  */
4350
4351 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4352 {
4353         struct ata_host_set *host_set = dev_instance;
4354         unsigned int i;
4355         unsigned int handled = 0;
4356         unsigned long flags;
4357
4358         /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4359         spin_lock_irqsave(&host_set->lock, flags);
4360
4361         for (i = 0; i < host_set->n_ports; i++) {
4362                 struct ata_port *ap;
4363
4364                 ap = host_set->ports[i];
4365                 if (ap &&
4366                     !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4367                         struct ata_queued_cmd *qc;
4368
4369                         qc = ata_qc_from_tag(ap, ap->active_tag);
4370                         if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4371                             (qc->flags & ATA_QCFLAG_ACTIVE))
4372                                 handled |= ata_host_intr(ap, qc);
4373                 }
4374         }
4375
4376         spin_unlock_irqrestore(&host_set->lock, flags);
4377
4378         return IRQ_RETVAL(handled);
4379 }
4380
4381 /**
4382  *      ata_port_start - Set port up for dma.
4383  *      @ap: Port to initialize
4384  *
4385  *      Called just after data structures for each port are
4386  *      initialized.  Allocates space for PRD table.
4387  *
4388  *      May be used as the port_start() entry in ata_port_operations.
4389  *
4390  *      LOCKING:
4391  *      Inherited from caller.
4392  */
4393
4394 int ata_port_start (struct ata_port *ap)
4395 {
4396         struct device *dev = ap->host_set->dev;
4397         int rc;
4398
4399         ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4400         if (!ap->prd)
4401                 return -ENOMEM;
4402
4403         rc = ata_pad_alloc(ap, dev);
4404         if (rc) {
4405                 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4406                 return rc;
4407         }
4408
4409         DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4410
4411         return 0;
4412 }
4413
4414
4415 /**
4416  *      ata_port_stop - Undo ata_port_start()
4417  *      @ap: Port to shut down
4418  *
4419  *      Frees the PRD table.
4420  *
4421  *      May be used as the port_stop() entry in ata_port_operations.
4422  *
4423  *      LOCKING:
4424  *      Inherited from caller.
4425  */
4426
4427 void ata_port_stop (struct ata_port *ap)
4428 {
4429         struct device *dev = ap->host_set->dev;
4430
4431         dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4432         ata_pad_free(ap, dev);
4433 }
4434
4435 void ata_host_stop (struct ata_host_set *host_set)
4436 {
4437         if (host_set->mmio_base)
4438                 iounmap(host_set->mmio_base);
4439 }
4440
4441
4442 /**
4443  *      ata_host_remove - Unregister SCSI host structure with upper layers
4444  *      @ap: Port to unregister
4445  *      @do_unregister: 1 if we fully unregister, 0 to just stop the port
4446  *
4447  *      LOCKING:
4448  *      Inherited from caller.
4449  */
4450
4451 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4452 {
4453         struct Scsi_Host *sh = ap->host;
4454
4455         DPRINTK("ENTER\n");
4456
4457         if (do_unregister)
4458                 scsi_remove_host(sh);
4459
4460         ap->ops->port_stop(ap);
4461 }
4462
4463 /**
4464  *      ata_host_init - Initialize an ata_port structure
4465  *      @ap: Structure to initialize
4466  *      @host: associated SCSI mid-layer structure
4467  *      @host_set: Collection of hosts to which @ap belongs
4468  *      @ent: Probe information provided by low-level driver
4469  *      @port_no: Port number associated with this ata_port
4470  *
4471  *      Initialize a new ata_port structure, and its associated
4472  *      scsi_host.
4473  *
4474  *      LOCKING:
4475  *      Inherited from caller.
4476  */
4477
4478 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4479                           struct ata_host_set *host_set,
4480                           const struct ata_probe_ent *ent, unsigned int port_no)
4481 {
4482         unsigned int i;
4483
4484         host->max_id = 16;
4485         host->max_lun = 1;
4486         host->max_channel = 1;
4487         host->unique_id = ata_unique_id++;
4488         host->max_cmd_len = 12;
4489
4490         ap->flags = ATA_FLAG_PORT_DISABLED;
4491         ap->id = host->unique_id;
4492         ap->host = host;
4493         ap->ctl = ATA_DEVCTL_OBS;
4494         ap->host_set = host_set;
4495         ap->port_no = port_no;
4496         ap->hard_port_no =
4497                 ent->legacy_mode ? ent->hard_port_no : port_no;
4498         ap->pio_mask = ent->pio_mask;
4499         ap->mwdma_mask = ent->mwdma_mask;
4500         ap->udma_mask = ent->udma_mask;
4501         ap->flags |= ent->host_flags;
4502         ap->ops = ent->port_ops;
4503         ap->cbl = ATA_CBL_NONE;
4504         ap->active_tag = ATA_TAG_POISON;
4505         ap->last_ctl = 0xFF;
4506
4507         INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4508
4509         for (i = 0; i < ATA_MAX_DEVICES; i++)
4510                 ap->device[i].devno = i;
4511
4512 #ifdef ATA_IRQ_TRAP
4513         ap->stats.unhandled_irq = 1;
4514         ap->stats.idle_irq = 1;
4515 #endif
4516
4517         memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4518 }
4519
4520 /**
4521  *      ata_host_add - Attach low-level ATA driver to system
4522  *      @ent: Information provided by low-level driver
4523  *      @host_set: Collections of ports to which we add
4524  *      @port_no: Port number associated with this host
4525  *
4526  *      Attach low-level ATA driver to system.
4527  *
4528  *      LOCKING:
4529  *      PCI/etc. bus probe sem.
4530  *
4531  *      RETURNS:
4532  *      New ata_port on success, for NULL on error.
4533  */
4534
4535 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4536                                       struct ata_host_set *host_set,
4537                                       unsigned int port_no)
4538 {
4539         struct Scsi_Host *host;
4540         struct ata_port *ap;
4541         int rc;
4542
4543         DPRINTK("ENTER\n");
4544         host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4545         if (!host)
4546                 return NULL;
4547
4548         ap = (struct ata_port *) &host->hostdata[0];
4549
4550         ata_host_init(ap, host, host_set, ent, port_no);
4551
4552         rc = ap->ops->port_start(ap);
4553         if (rc)
4554                 goto err_out;
4555
4556         return ap;
4557
4558 err_out:
4559         scsi_host_put(host);
4560         return NULL;
4561 }
4562
4563 /**
4564  *      ata_device_add - Register hardware device with ATA and SCSI layers
4565  *      @ent: Probe information describing hardware device to be registered
4566  *
4567  *      This function processes the information provided in the probe
4568  *      information struct @ent, allocates the necessary ATA and SCSI
4569  *      host information structures, initializes them, and registers
4570  *      everything with requisite kernel subsystems.
4571  *
4572  *      This function requests irqs, probes the ATA bus, and probes
4573  *      the SCSI bus.
4574  *
4575  *      LOCKING:
4576  *      PCI/etc. bus probe sem.
4577  *
4578  *      RETURNS:
4579  *      Number of ports registered.  Zero on error (no ports registered).
4580  */
4581
4582 int ata_device_add(const struct ata_probe_ent *ent)
4583 {
4584         unsigned int count = 0, i;
4585         struct device *dev = ent->dev;
4586         struct ata_host_set *host_set;
4587
4588         DPRINTK("ENTER\n");
4589         /* alloc a container for our list of ATA ports (buses) */
4590         host_set = kzalloc(sizeof(struct ata_host_set) +
4591                            (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4592         if (!host_set)
4593                 return 0;
4594         spin_lock_init(&host_set->lock);
4595
4596         host_set->dev = dev;
4597         host_set->n_ports = ent->n_ports;
4598         host_set->irq = ent->irq;
4599         host_set->mmio_base = ent->mmio_base;
4600         host_set->private_data = ent->private_data;
4601         host_set->ops = ent->port_ops;
4602
4603         /* register each port bound to this device */
4604         for (i = 0; i < ent->n_ports; i++) {
4605                 struct ata_port *ap;
4606                 unsigned long xfer_mode_mask;
4607
4608                 ap = ata_host_add(ent, host_set, i);
4609                 if (!ap)
4610                         goto err_out;
4611
4612                 host_set->ports[i] = ap;
4613                 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4614                                 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4615                                 (ap->pio_mask << ATA_SHIFT_PIO);
4616
4617                 /* print per-port info to dmesg */
4618                 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4619                                  "bmdma 0x%lX irq %lu\n",
4620                         ap->id,
4621                         ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4622                         ata_mode_string(xfer_mode_mask),
4623                         ap->ioaddr.cmd_addr,
4624                         ap->ioaddr.ctl_addr,
4625                         ap->ioaddr.bmdma_addr,
4626                         ent->irq);
4627
4628                 ata_chk_status(ap);
4629                 host_set->ops->irq_clear(ap);
4630                 count++;
4631         }
4632
4633         if (!count)
4634                 goto err_free_ret;
4635
4636         /* obtain irq, that is shared between channels */
4637         if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4638                         DRV_NAME, host_set))
4639                 goto err_out;
4640
4641         /* perform each probe synchronously */
4642         DPRINTK("probe begin\n");
4643         for (i = 0; i < count; i++) {
4644                 struct ata_port *ap;
4645                 int rc;
4646
4647                 ap = host_set->ports[i];
4648
4649                 DPRINTK("ata%u: probe begin\n", ap->id);
4650                 rc = ata_bus_probe(ap);
4651                 DPRINTK("ata%u: probe end\n", ap->id);
4652
4653                 if (rc) {
4654                         /* FIXME: do something useful here?
4655                          * Current libata behavior will
4656                          * tear down everything when
4657                          * the module is removed
4658                          * or the h/w is unplugged.
4659                          */
4660                 }
4661
4662                 rc = scsi_add_host(ap->host, dev);
4663                 if (rc) {
4664                         printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4665                                ap->id);
4666                         /* FIXME: do something useful here */
4667                         /* FIXME: handle unconditional calls to
4668                          * scsi_scan_host and ata_host_remove, below,
4669                          * at the very least
4670                          */
4671                 }
4672         }
4673
4674         /* probes are done, now scan each port's disk(s) */
4675         DPRINTK("probe begin\n");
4676         for (i = 0; i < count; i++) {
4677                 struct ata_port *ap = host_set->ports[i];
4678
4679                 ata_scsi_scan_host(ap);
4680         }
4681
4682         dev_set_drvdata(dev, host_set);
4683
4684         VPRINTK("EXIT, returning %u\n", ent->n_ports);
4685         return ent->n_ports; /* success */
4686
4687 err_out:
4688         for (i = 0; i < count; i++) {
4689                 ata_host_remove(host_set->ports[i], 1);
4690                 scsi_host_put(host_set->ports[i]->host);
4691         }
4692 err_free_ret:
4693         kfree(host_set);
4694         VPRINTK("EXIT, returning 0\n");
4695         return 0;
4696 }
4697
4698 /**
4699  *      ata_host_set_remove - PCI layer callback for device removal
4700  *      @host_set: ATA host set that was removed
4701  *
4702  *      Unregister all objects associated with this host set. Free those 
4703  *      objects.
4704  *
4705  *      LOCKING:
4706  *      Inherited from calling layer (may sleep).
4707  */
4708
4709 void ata_host_set_remove(struct ata_host_set *host_set)
4710 {
4711         struct ata_port *ap;
4712         unsigned int i;
4713
4714         for (i = 0; i < host_set->n_ports; i++) {
4715                 ap = host_set->ports[i];
4716                 scsi_remove_host(ap->host);
4717         }
4718
4719         free_irq(host_set->irq, host_set);
4720
4721         for (i = 0; i < host_set->n_ports; i++) {
4722                 ap = host_set->ports[i];
4723
4724                 ata_scsi_release(ap->host);
4725
4726                 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4727                         struct ata_ioports *ioaddr = &ap->ioaddr;
4728
4729                         if (ioaddr->cmd_addr == 0x1f0)
4730                                 release_region(0x1f0, 8);
4731                         else if (ioaddr->cmd_addr == 0x170)
4732                                 release_region(0x170, 8);
4733                 }
4734
4735                 scsi_host_put(ap->host);
4736         }
4737
4738         if (host_set->ops->host_stop)
4739                 host_set->ops->host_stop(host_set);
4740
4741         kfree(host_set);
4742 }
4743
4744 /**
4745  *      ata_scsi_release - SCSI layer callback hook for host unload
4746  *      @host: libata host to be unloaded
4747  *
4748  *      Performs all duties necessary to shut down a libata port...
4749  *      Kill port kthread, disable port, and release resources.
4750  *
4751  *      LOCKING:
4752  *      Inherited from SCSI layer.
4753  *
4754  *      RETURNS:
4755  *      One.
4756  */
4757
4758 int ata_scsi_release(struct Scsi_Host *host)
4759 {
4760         struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4761
4762         DPRINTK("ENTER\n");
4763
4764         ap->ops->port_disable(ap);
4765         ata_host_remove(ap, 0);
4766
4767         DPRINTK("EXIT\n");
4768         return 1;
4769 }
4770
4771 /**
4772  *      ata_std_ports - initialize ioaddr with standard port offsets.
4773  *      @ioaddr: IO address structure to be initialized
4774  *
4775  *      Utility function which initializes data_addr, error_addr,
4776  *      feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4777  *      device_addr, status_addr, and command_addr to standard offsets
4778  *      relative to cmd_addr.
4779  *
4780  *      Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4781  */
4782
4783 void ata_std_ports(struct ata_ioports *ioaddr)
4784 {
4785         ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4786         ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4787         ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4788         ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4789         ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4790         ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4791         ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4792         ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4793         ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4794         ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4795 }
4796
4797 static struct ata_probe_ent *
4798 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4799 {
4800         struct ata_probe_ent *probe_ent;
4801
4802         probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4803         if (!probe_ent) {
4804                 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4805                        kobject_name(&(dev->kobj)));
4806                 return NULL;
4807         }
4808
4809         INIT_LIST_HEAD(&probe_ent->node);
4810         probe_ent->dev = dev;
4811
4812         probe_ent->sht = port->sht;
4813         probe_ent->host_flags = port->host_flags;
4814         probe_ent->pio_mask = port->pio_mask;
4815         probe_ent->mwdma_mask = port->mwdma_mask;
4816         probe_ent->udma_mask = port->udma_mask;
4817         probe_ent->port_ops = port->port_ops;
4818
4819         return probe_ent;
4820 }
4821
4822
4823
4824 #ifdef CONFIG_PCI
4825
4826 void ata_pci_host_stop (struct ata_host_set *host_set)
4827 {
4828         struct pci_dev *pdev = to_pci_dev(host_set->dev);
4829
4830         pci_iounmap(pdev, host_set->mmio_base);
4831 }
4832
4833 /**
4834  *      ata_pci_init_native_mode - Initialize native-mode driver
4835  *      @pdev:  pci device to be initialized
4836  *      @port:  array[2] of pointers to port info structures.
4837  *      @ports: bitmap of ports present
4838  *
4839  *      Utility function which allocates and initializes an
4840  *      ata_probe_ent structure for a standard dual-port
4841  *      PIO-based IDE controller.  The returned ata_probe_ent
4842  *      structure can be passed to ata_device_add().  The returned
4843  *      ata_probe_ent structure should then be freed with kfree().
4844  *
4845  *      The caller need only pass the address of the primary port, the
4846  *      secondary will be deduced automatically. If the device has non
4847  *      standard secondary port mappings this function can be called twice,
4848  *      once for each interface.
4849  */
4850
4851 struct ata_probe_ent *
4852 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4853 {
4854         struct ata_probe_ent *probe_ent =
4855                 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4856         int p = 0;
4857
4858         if (!probe_ent)
4859                 return NULL;
4860
4861         probe_ent->irq = pdev->irq;
4862         probe_ent->irq_flags = SA_SHIRQ;
4863         probe_ent->private_data = port[0]->private_data;
4864
4865         if (ports & ATA_PORT_PRIMARY) {
4866                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4867                 probe_ent->port[p].altstatus_addr =
4868                 probe_ent->port[p].ctl_addr =
4869                         pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4870                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4871                 ata_std_ports(&probe_ent->port[p]);
4872                 p++;
4873         }
4874
4875         if (ports & ATA_PORT_SECONDARY) {
4876                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4877                 probe_ent->port[p].altstatus_addr =
4878                 probe_ent->port[p].ctl_addr =
4879                         pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4880                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4881                 ata_std_ports(&probe_ent->port[p]);
4882                 p++;
4883         }
4884
4885         probe_ent->n_ports = p;
4886         return probe_ent;
4887 }
4888
4889 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4890 {
4891         struct ata_probe_ent *probe_ent;
4892
4893         probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4894         if (!probe_ent)
4895                 return NULL;
4896
4897         probe_ent->legacy_mode = 1;
4898         probe_ent->n_ports = 1;
4899         probe_ent->hard_port_no = port_num;
4900         probe_ent->private_data = port->private_data;
4901
4902         switch(port_num)
4903         {
4904                 case 0:
4905                         probe_ent->irq = 14;
4906                         probe_ent->port[0].cmd_addr = 0x1f0;
4907                         probe_ent->port[0].altstatus_addr =
4908                         probe_ent->port[0].ctl_addr = 0x3f6;
4909                         break;
4910                 case 1:
4911                         probe_ent->irq = 15;
4912                         probe_ent->port[0].cmd_addr = 0x170;
4913                         probe_ent->port[0].altstatus_addr =
4914                         probe_ent->port[0].ctl_addr = 0x376;
4915                         break;
4916         }
4917         probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4918         ata_std_ports(&probe_ent->port[0]);
4919         return probe_ent;
4920 }
4921
4922 /**
4923  *      ata_pci_init_one - Initialize/register PCI IDE host controller
4924  *      @pdev: Controller to be initialized
4925  *      @port_info: Information from low-level host driver
4926  *      @n_ports: Number of ports attached to host controller
4927  *
4928  *      This is a helper function which can be called from a driver's
4929  *      xxx_init_one() probe function if the hardware uses traditional
4930  *      IDE taskfile registers.
4931  *
4932  *      This function calls pci_enable_device(), reserves its register
4933  *      regions, sets the dma mask, enables bus master mode, and calls
4934  *      ata_device_add()
4935  *
4936  *      LOCKING:
4937  *      Inherited from PCI layer (may sleep).
4938  *
4939  *      RETURNS:
4940  *      Zero on success, negative on errno-based value on error.
4941  */
4942
4943 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4944                       unsigned int n_ports)
4945 {
4946         struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4947         struct ata_port_info *port[2];
4948         u8 tmp8, mask;
4949         unsigned int legacy_mode = 0;
4950         int disable_dev_on_err = 1;
4951         int rc;
4952
4953         DPRINTK("ENTER\n");
4954
4955         port[0] = port_info[0];
4956         if (n_ports > 1)
4957                 port[1] = port_info[1];
4958         else
4959                 port[1] = port[0];
4960
4961         if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4962             && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4963                 /* TODO: What if one channel is in native mode ... */
4964                 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4965                 mask = (1 << 2) | (1 << 0);
4966                 if ((tmp8 & mask) != mask)
4967                         legacy_mode = (1 << 3);
4968         }
4969
4970         /* FIXME... */
4971         if ((!legacy_mode) && (n_ports > 2)) {
4972                 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4973                 n_ports = 2;
4974                 /* For now */
4975         }
4976
4977         /* FIXME: Really for ATA it isn't safe because the device may be
4978            multi-purpose and we want to leave it alone if it was already
4979            enabled. Secondly for shared use as Arjan says we want refcounting
4980            
4981            Checking dev->is_enabled is insufficient as this is not set at
4982            boot for the primary video which is BIOS enabled
4983          */
4984          
4985         rc = pci_enable_device(pdev);
4986         if (rc)
4987                 return rc;
4988
4989         rc = pci_request_regions(pdev, DRV_NAME);
4990         if (rc) {
4991                 disable_dev_on_err = 0;
4992                 goto err_out;
4993         }
4994
4995         /* FIXME: Should use platform specific mappers for legacy port ranges */
4996         if (legacy_mode) {
4997                 if (!request_region(0x1f0, 8, "libata")) {
4998                         struct resource *conflict, res;
4999                         res.start = 0x1f0;
5000                         res.end = 0x1f0 + 8 - 1;
5001                         conflict = ____request_resource(&ioport_resource, &res);
5002                         if (!strcmp(conflict->name, "libata"))
5003                                 legacy_mode |= (1 << 0);
5004                         else {
5005                                 disable_dev_on_err = 0;
5006                                 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5007                         }
5008                 } else
5009                         legacy_mode |= (1 << 0);
5010
5011                 if (!request_region(0x170, 8, "libata")) {
5012                         struct resource *conflict, res;
5013                         res.start = 0x170;
5014                         res.end = 0x170 + 8 - 1;
5015                         conflict = ____request_resource(&ioport_resource, &res);
5016                         if (!strcmp(conflict->name, "libata"))
5017                                 legacy_mode |= (1 << 1);
5018                         else {
5019                                 disable_dev_on_err = 0;
5020                                 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5021                         }
5022                 } else
5023                         legacy_mode |= (1 << 1);
5024         }
5025
5026         /* we have legacy mode, but all ports are unavailable */
5027         if (legacy_mode == (1 << 3)) {
5028                 rc = -EBUSY;
5029                 goto err_out_regions;
5030         }
5031
5032         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5033         if (rc)
5034                 goto err_out_regions;
5035         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5036         if (rc)
5037                 goto err_out_regions;
5038
5039         if (legacy_mode) {
5040                 if (legacy_mode & (1 << 0))
5041                         probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
5042                 if (legacy_mode & (1 << 1))
5043                         probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
5044         } else {
5045                 if (n_ports == 2)
5046                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5047                 else
5048                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5049         }
5050         if (!probe_ent && !probe_ent2) {
5051                 rc = -ENOMEM;
5052                 goto err_out_regions;
5053         }
5054
5055         pci_set_master(pdev);
5056
5057         /* FIXME: check ata_device_add return */
5058         if (legacy_mode) {
5059                 if (legacy_mode & (1 << 0))
5060                         ata_device_add(probe_ent);
5061                 if (legacy_mode & (1 << 1))
5062                         ata_device_add(probe_ent2);
5063         } else
5064                 ata_device_add(probe_ent);
5065
5066         kfree(probe_ent);
5067         kfree(probe_ent2);
5068
5069         return 0;
5070
5071 err_out_regions:
5072         if (legacy_mode & (1 << 0))
5073                 release_region(0x1f0, 8);
5074         if (legacy_mode & (1 << 1))
5075                 release_region(0x170, 8);
5076         pci_release_regions(pdev);
5077 err_out:
5078         if (disable_dev_on_err)
5079                 pci_disable_device(pdev);
5080         return rc;
5081 }
5082
5083 /**
5084  *      ata_pci_remove_one - PCI layer callback for device removal
5085  *      @pdev: PCI device that was removed
5086  *
5087  *      PCI layer indicates to libata via this hook that
5088  *      hot-unplug or module unload event has occurred.
5089  *      Handle this by unregistering all objects associated
5090  *      with this PCI device.  Free those objects.  Then finally
5091  *      release PCI resources and disable device.
5092  *
5093  *      LOCKING:
5094  *      Inherited from PCI layer (may sleep).
5095  */
5096
5097 void ata_pci_remove_one (struct pci_dev *pdev)
5098 {
5099         struct device *dev = pci_dev_to_dev(pdev);
5100         struct ata_host_set *host_set = dev_get_drvdata(dev);
5101
5102         ata_host_set_remove(host_set);
5103         pci_release_regions(pdev);
5104         pci_disable_device(pdev);
5105         dev_set_drvdata(dev, NULL);
5106 }
5107
5108 /* move to PCI subsystem */
5109 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5110 {
5111         unsigned long tmp = 0;
5112
5113         switch (bits->width) {
5114         case 1: {
5115                 u8 tmp8 = 0;
5116                 pci_read_config_byte(pdev, bits->reg, &tmp8);
5117                 tmp = tmp8;
5118                 break;
5119         }
5120         case 2: {
5121                 u16 tmp16 = 0;
5122                 pci_read_config_word(pdev, bits->reg, &tmp16);
5123                 tmp = tmp16;
5124                 break;
5125         }
5126         case 4: {
5127                 u32 tmp32 = 0;
5128                 pci_read_config_dword(pdev, bits->reg, &tmp32);
5129                 tmp = tmp32;
5130                 break;
5131         }
5132
5133         default:
5134                 return -EINVAL;
5135         }
5136
5137         tmp &= bits->mask;
5138
5139         return (tmp == bits->val) ? 1 : 0;
5140 }
5141 #endif /* CONFIG_PCI */
5142
5143
5144 static int __init ata_init(void)
5145 {
5146         ata_wq = create_workqueue("ata");
5147         if (!ata_wq)
5148                 return -ENOMEM;
5149
5150         printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5151         return 0;
5152 }
5153
5154 static void __exit ata_exit(void)
5155 {
5156         destroy_workqueue(ata_wq);
5157 }
5158
5159 module_init(ata_init);
5160 module_exit(ata_exit);
5161
5162 static unsigned long ratelimit_time;
5163 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5164
5165 int ata_ratelimit(void)
5166 {
5167         int rc;
5168         unsigned long flags;
5169
5170         spin_lock_irqsave(&ata_ratelimit_lock, flags);
5171
5172         if (time_after(jiffies, ratelimit_time)) {
5173                 rc = 1;
5174                 ratelimit_time = jiffies + (HZ/5);
5175         } else
5176                 rc = 0;
5177
5178         spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5179
5180         return rc;
5181 }
5182
5183 /*
5184  * libata is essentially a library of internal helper functions for
5185  * low-level ATA host controller drivers.  As such, the API/ABI is
5186  * likely to change as new drivers are added and updated.
5187  * Do not depend on ABI/API stability.
5188  */
5189
5190 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5191 EXPORT_SYMBOL_GPL(ata_std_ports);
5192 EXPORT_SYMBOL_GPL(ata_device_add);
5193 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5194 EXPORT_SYMBOL_GPL(ata_sg_init);
5195 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5196 EXPORT_SYMBOL_GPL(ata_qc_complete);
5197 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5198 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5199 EXPORT_SYMBOL_GPL(ata_tf_load);
5200 EXPORT_SYMBOL_GPL(ata_tf_read);
5201 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5202 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5203 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5204 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5205 EXPORT_SYMBOL_GPL(ata_check_status);
5206 EXPORT_SYMBOL_GPL(ata_altstatus);
5207 EXPORT_SYMBOL_GPL(ata_exec_command);
5208 EXPORT_SYMBOL_GPL(ata_port_start);
5209 EXPORT_SYMBOL_GPL(ata_port_stop);
5210 EXPORT_SYMBOL_GPL(ata_host_stop);
5211 EXPORT_SYMBOL_GPL(ata_interrupt);
5212 EXPORT_SYMBOL_GPL(ata_qc_prep);
5213 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5214 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5215 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5216 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5217 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5218 EXPORT_SYMBOL_GPL(ata_port_probe);
5219 EXPORT_SYMBOL_GPL(sata_phy_reset);
5220 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5221 EXPORT_SYMBOL_GPL(ata_bus_reset);
5222 EXPORT_SYMBOL_GPL(ata_port_disable);
5223 EXPORT_SYMBOL_GPL(ata_ratelimit);
5224 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5225 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5226 EXPORT_SYMBOL_GPL(ata_scsi_error);
5227 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5228 EXPORT_SYMBOL_GPL(ata_scsi_release);
5229 EXPORT_SYMBOL_GPL(ata_host_intr);
5230 EXPORT_SYMBOL_GPL(ata_dev_classify);
5231 EXPORT_SYMBOL_GPL(ata_dev_id_string);
5232 EXPORT_SYMBOL_GPL(ata_dev_config);
5233 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5234
5235 EXPORT_SYMBOL_GPL(ata_timing_compute);
5236 EXPORT_SYMBOL_GPL(ata_timing_merge);
5237
5238 #ifdef CONFIG_PCI
5239 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5240 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5241 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5242 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5243 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5244 #endif /* CONFIG_PCI */