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