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