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