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