patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / libata-core.c
1 /*
2    libata-core.c - helper library for ATA
3
4    Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
5    Copyright 2003-2004 Jeff Garzik
6
7    The contents of this file are subject to the Open
8    Software License version 1.1 that can be found at
9    http://www.opensource.org/licenses/osl-1.1.txt and is included herein
10    by reference.
11
12    Alternatively, the contents of this file may be used under the terms
13    of the GNU General Public License version 2 (the "GPL") as distributed
14    in the kernel source COPYING file, in which case the provisions of
15    the GPL are applicable instead of the above.  If you wish to allow
16    the use of your version of this file only under the terms of the
17    GPL and not to allow others to use your version of this file under
18    the OSL, indicate your decision by deleting the provisions above and
19    replace them with the notice and other provisions required by the GPL.
20    If you do not delete the provisions above, a recipient may use your
21    version of this file under either the OSL or the GPL.
22
23  */
24
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/highmem.h>
32 #include <linux/spinlock.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/timer.h>
36 #include <linux/interrupt.h>
37 #include <linux/completion.h>
38 #include <linux/suspend.h>
39 #include <linux/workqueue.h>
40 #include <scsi/scsi.h>
41 #include "scsi.h"
42 #include <scsi/scsi_host.h>
43 #include <linux/libata.h>
44 #include <asm/io.h>
45 #include <asm/semaphore.h>
46
47 #include "libata.h"
48
49 static unsigned int ata_busy_sleep (struct ata_port *ap,
50                                     unsigned long tmout_pat,
51                                     unsigned long tmout);
52 static void __ata_dev_select (struct ata_port *ap, unsigned int device);
53 static void ata_dma_complete(struct ata_queued_cmd *qc, u8 host_stat);
54 static void ata_host_set_pio(struct ata_port *ap);
55 static void ata_host_set_udma(struct ata_port *ap);
56 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device);
57 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device);
58 static void ata_set_mode(struct ata_port *ap);
59 static int ata_qc_issue_prot(struct ata_queued_cmd *qc);
60
61 static unsigned int ata_unique_id = 1;
62 static struct workqueue_struct *ata_wq;
63
64 MODULE_AUTHOR("Jeff Garzik");
65 MODULE_DESCRIPTION("Library module for ATA devices");
66 MODULE_LICENSE("GPL");
67
68 static const char * thr_state_name[] = {
69         "THR_UNKNOWN",
70         "THR_PORT_RESET",
71         "THR_AWAIT_DEATH",
72         "THR_PROBE_FAILED",
73         "THR_IDLE",
74         "THR_PROBE_SUCCESS",
75         "THR_PROBE_START",
76 };
77
78 /**
79  *      ata_thr_state_name - convert thread state enum to string
80  *      @thr_state: thread state to be converted to string
81  *
82  *      Converts the specified thread state id to a constant C string.
83  *
84  *      LOCKING:
85  *      None.
86  *
87  *      RETURNS:
88  *      The THR_xxx-prefixed string naming the specified thread
89  *      state id, or the string "<invalid THR_xxx state>".
90  */
91
92 static const char *ata_thr_state_name(unsigned int thr_state)
93 {
94         if (thr_state < ARRAY_SIZE(thr_state_name))
95                 return thr_state_name[thr_state];
96         return "<invalid THR_xxx state>";
97 }
98
99 /**
100  *      ata_tf_load_pio - send taskfile registers to host controller
101  *      @ap: Port to which output is sent
102  *      @tf: ATA taskfile register set
103  *
104  *      Outputs ATA taskfile to standard ATA host controller using PIO.
105  *
106  *      LOCKING:
107  *      Inherited from caller.
108  */
109
110 void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
111 {
112         struct ata_ioports *ioaddr = &ap->ioaddr;
113         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
114
115         if (tf->ctl != ap->last_ctl) {
116                 outb(tf->ctl, ioaddr->ctl_addr);
117                 ap->last_ctl = tf->ctl;
118                 ata_wait_idle(ap);
119         }
120
121         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
122                 outb(tf->hob_feature, ioaddr->feature_addr);
123                 outb(tf->hob_nsect, ioaddr->nsect_addr);
124                 outb(tf->hob_lbal, ioaddr->lbal_addr);
125                 outb(tf->hob_lbam, ioaddr->lbam_addr);
126                 outb(tf->hob_lbah, ioaddr->lbah_addr);
127                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
128                         tf->hob_feature,
129                         tf->hob_nsect,
130                         tf->hob_lbal,
131                         tf->hob_lbam,
132                         tf->hob_lbah);
133         }
134
135         if (is_addr) {
136                 outb(tf->feature, ioaddr->feature_addr);
137                 outb(tf->nsect, ioaddr->nsect_addr);
138                 outb(tf->lbal, ioaddr->lbal_addr);
139                 outb(tf->lbam, ioaddr->lbam_addr);
140                 outb(tf->lbah, ioaddr->lbah_addr);
141                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
142                         tf->feature,
143                         tf->nsect,
144                         tf->lbal,
145                         tf->lbam,
146                         tf->lbah);
147         }
148
149         if (tf->flags & ATA_TFLAG_DEVICE) {
150                 outb(tf->device, ioaddr->device_addr);
151                 VPRINTK("device 0x%X\n", tf->device);
152         }
153
154         ata_wait_idle(ap);
155 }
156
157 /**
158  *      ata_tf_load_mmio - send taskfile registers to host controller
159  *      @ap: Port to which output is sent
160  *      @tf: ATA taskfile register set
161  *
162  *      Outputs ATA taskfile to standard ATA host controller using MMIO.
163  *
164  *      LOCKING:
165  *      Inherited from caller.
166  */
167
168 void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
169 {
170         struct ata_ioports *ioaddr = &ap->ioaddr;
171         unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
172
173         if (tf->ctl != ap->last_ctl) {
174                 writeb(tf->ctl, ap->ioaddr.ctl_addr);
175                 ap->last_ctl = tf->ctl;
176                 ata_wait_idle(ap);
177         }
178
179         if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
180                 writeb(tf->hob_feature, (void *) ioaddr->feature_addr);
181                 writeb(tf->hob_nsect, (void *) ioaddr->nsect_addr);
182                 writeb(tf->hob_lbal, (void *) ioaddr->lbal_addr);
183                 writeb(tf->hob_lbam, (void *) ioaddr->lbam_addr);
184                 writeb(tf->hob_lbah, (void *) ioaddr->lbah_addr);
185                 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
186                         tf->hob_feature,
187                         tf->hob_nsect,
188                         tf->hob_lbal,
189                         tf->hob_lbam,
190                         tf->hob_lbah);
191         }
192
193         if (is_addr) {
194                 writeb(tf->feature, (void *) ioaddr->feature_addr);
195                 writeb(tf->nsect, (void *) ioaddr->nsect_addr);
196                 writeb(tf->lbal, (void *) ioaddr->lbal_addr);
197                 writeb(tf->lbam, (void *) ioaddr->lbam_addr);
198                 writeb(tf->lbah, (void *) ioaddr->lbah_addr);
199                 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
200                         tf->feature,
201                         tf->nsect,
202                         tf->lbal,
203                         tf->lbam,
204                         tf->lbah);
205         }
206
207         if (tf->flags & ATA_TFLAG_DEVICE) {
208                 writeb(tf->device, (void *) ioaddr->device_addr);
209                 VPRINTK("device 0x%X\n", tf->device);
210         }
211
212         ata_wait_idle(ap);
213 }
214
215 /**
216  *      ata_exec_command_pio - issue ATA command to host controller
217  *      @ap: port to which command is being issued
218  *      @tf: ATA taskfile register set
219  *
220  *      Issues PIO write to ATA command register, with proper
221  *      synchronization with interrupt handler / other threads.
222  *
223  *      LOCKING:
224  *      spin_lock_irqsave(host_set lock)
225  */
226
227 void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
228 {
229         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
230
231         outb(tf->command, ap->ioaddr.command_addr);
232         ata_pause(ap);
233 }
234
235
236 /**
237  *      ata_exec_command_mmio - issue ATA command to host controller
238  *      @ap: port to which command is being issued
239  *      @tf: ATA taskfile register set
240  *
241  *      Issues MMIO write to ATA command register, with proper
242  *      synchronization with interrupt handler / other threads.
243  *
244  *      LOCKING:
245  *      spin_lock_irqsave(host_set lock)
246  */
247
248 void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
249 {
250         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251
252         writeb(tf->command, (void *) ap->ioaddr.command_addr);
253         ata_pause(ap);
254 }
255
256 /**
257  *      ata_exec - issue ATA command to host controller
258  *      @ap: port to which command is being issued
259  *      @tf: ATA taskfile register set
260  *
261  *      Issues PIO write to ATA command register, with proper
262  *      synchronization with interrupt handler / other threads.
263  *
264  *      LOCKING:
265  *      Obtains host_set lock.
266  */
267
268 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
269 {
270         unsigned long flags;
271
272         DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273         spin_lock_irqsave(&ap->host_set->lock, flags);
274         ap->ops->exec_command(ap, tf);
275         spin_unlock_irqrestore(&ap->host_set->lock, flags);
276 }
277
278 /**
279  *      ata_tf_to_host - issue ATA taskfile to host controller
280  *      @ap: port to which command is being issued
281  *      @tf: ATA taskfile register set
282  *
283  *      Issues ATA taskfile register set to ATA host controller,
284  *      via PIO, with proper synchronization with interrupt handler and
285  *      other threads.
286  *
287  *      LOCKING:
288  *      Obtains host_set lock.
289  */
290
291 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
292 {
293         ap->ops->tf_load(ap, tf);
294
295         ata_exec(ap, tf);
296 }
297
298 /**
299  *      ata_tf_to_host_nolock - issue ATA taskfile to host controller
300  *      @ap: port to which command is being issued
301  *      @tf: ATA taskfile register set
302  *
303  *      Issues ATA taskfile register set to ATA host controller,
304  *      via PIO, with proper synchronization with interrupt handler and
305  *      other threads.
306  *
307  *      LOCKING:
308  *      spin_lock_irqsave(host_set lock)
309  */
310
311 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
312 {
313         ap->ops->tf_load(ap, tf);
314         ap->ops->exec_command(ap, tf);
315 }
316
317 /**
318  *      ata_tf_read_pio - input device's ATA taskfile shadow registers
319  *      @ap: Port from which input is read
320  *      @tf: ATA taskfile register set for storing input
321  *
322  *      Reads ATA taskfile registers for currently-selected device
323  *      into @tf via PIO.
324  *
325  *      LOCKING:
326  *      Inherited from caller.
327  */
328
329 void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
330 {
331         struct ata_ioports *ioaddr = &ap->ioaddr;
332
333         tf->nsect = inb(ioaddr->nsect_addr);
334         tf->lbal = inb(ioaddr->lbal_addr);
335         tf->lbam = inb(ioaddr->lbam_addr);
336         tf->lbah = inb(ioaddr->lbah_addr);
337         tf->device = inb(ioaddr->device_addr);
338
339         if (tf->flags & ATA_TFLAG_LBA48) {
340                 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
341                 tf->hob_feature = inb(ioaddr->error_addr);
342                 tf->hob_nsect = inb(ioaddr->nsect_addr);
343                 tf->hob_lbal = inb(ioaddr->lbal_addr);
344                 tf->hob_lbam = inb(ioaddr->lbam_addr);
345                 tf->hob_lbah = inb(ioaddr->lbah_addr);
346         }
347 }
348
349 /**
350  *      ata_tf_read_mmio - input device's ATA taskfile shadow registers
351  *      @ap: Port from which input is read
352  *      @tf: ATA taskfile register set for storing input
353  *
354  *      Reads ATA taskfile registers for currently-selected device
355  *      into @tf via MMIO.
356  *
357  *      LOCKING:
358  *      Inherited from caller.
359  */
360
361 void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
362 {
363         struct ata_ioports *ioaddr = &ap->ioaddr;
364
365         tf->nsect = readb((void *)ioaddr->nsect_addr);
366         tf->lbal = readb((void *)ioaddr->lbal_addr);
367         tf->lbam = readb((void *)ioaddr->lbam_addr);
368         tf->lbah = readb((void *)ioaddr->lbah_addr);
369         tf->device = readb((void *)ioaddr->device_addr);
370
371         if (tf->flags & ATA_TFLAG_LBA48) {
372                 writeb(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr);
373                 tf->hob_feature = readb((void *)ioaddr->error_addr);
374                 tf->hob_nsect = readb((void *)ioaddr->nsect_addr);
375                 tf->hob_lbal = readb((void *)ioaddr->lbal_addr);
376                 tf->hob_lbam = readb((void *)ioaddr->lbam_addr);
377                 tf->hob_lbah = readb((void *)ioaddr->lbah_addr);
378         }
379 }
380
381 /**
382  *      ata_check_status_pio - Read device status reg & clear interrupt
383  *      @ap: port where the device is
384  *
385  *      Reads ATA taskfile status register for currently-selected device
386  *      via PIO and return it's value. This also clears pending interrupts
387  *      from this device
388  *
389  *      LOCKING:
390  *      Inherited from caller.
391  */
392 u8 ata_check_status_pio(struct ata_port *ap)
393 {
394         return inb(ap->ioaddr.status_addr);
395 }
396
397 /**
398  *      ata_check_status_mmio - Read device status reg & clear interrupt
399  *      @ap: port where the device is
400  *
401  *      Reads ATA taskfile status register for currently-selected device
402  *      via MMIO and return it's value. This also clears pending interrupts
403  *      from this device
404  *
405  *      LOCKING:
406  *      Inherited from caller.
407  */
408 u8 ata_check_status_mmio(struct ata_port *ap)
409 {
410         return readb((void *) ap->ioaddr.status_addr);
411 }
412
413 /**
414  *      ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
415  *      @tf: Taskfile to convert
416  *      @fis: Buffer into which data will output
417  *      @pmp: Port multiplier port
418  *
419  *      Converts a standard ATA taskfile to a Serial ATA
420  *      FIS structure (Register - Host to Device).
421  *
422  *      LOCKING:
423  *      Inherited from caller.
424  */
425
426 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
427 {
428         fis[0] = 0x27;  /* Register - Host to Device FIS */
429         fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
430                                             bit 7 indicates Command FIS */
431         fis[2] = tf->command;
432         fis[3] = tf->feature;
433
434         fis[4] = tf->lbal;
435         fis[5] = tf->lbam;
436         fis[6] = tf->lbah;
437         fis[7] = tf->device;
438
439         fis[8] = tf->hob_lbal;
440         fis[9] = tf->hob_lbam;
441         fis[10] = tf->hob_lbah;
442         fis[11] = tf->hob_feature;
443
444         fis[12] = tf->nsect;
445         fis[13] = tf->hob_nsect;
446         fis[14] = 0;
447         fis[15] = tf->ctl;
448
449         fis[16] = 0;
450         fis[17] = 0;
451         fis[18] = 0;
452         fis[19] = 0;
453 }
454
455 /**
456  *      ata_tf_from_fis - Convert SATA FIS to ATA taskfile
457  *      @fis: Buffer from which data will be input
458  *      @tf: Taskfile to output
459  *
460  *      Converts a standard ATA taskfile to a Serial ATA
461  *      FIS structure (Register - Host to Device).
462  *
463  *      LOCKING:
464  *      Inherited from caller.
465  */
466
467 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
468 {
469         tf->command     = fis[2];       /* status */
470         tf->feature     = fis[3];       /* error */
471
472         tf->lbal        = fis[4];
473         tf->lbam        = fis[5];
474         tf->lbah        = fis[6];
475         tf->device      = fis[7];
476
477         tf->hob_lbal    = fis[8];
478         tf->hob_lbam    = fis[9];
479         tf->hob_lbah    = fis[10];
480
481         tf->nsect       = fis[12];
482         tf->hob_nsect   = fis[13];
483 }
484
485 /**
486  *      ata_prot_to_cmd - determine which read/write opcodes to use
487  *      @protocol: ATA_PROT_xxx taskfile protocol
488  *      @lba48: true is lba48 is present
489  *
490  *      Given necessary input, determine which read/write commands
491  *      to use to transfer data.
492  *
493  *      LOCKING:
494  *      None.
495  */
496 static int ata_prot_to_cmd(int protocol, int lba48)
497 {
498         int rcmd = 0, wcmd = 0;
499
500         switch (protocol) {
501         case ATA_PROT_PIO:
502                 if (lba48) {
503                         rcmd = ATA_CMD_PIO_READ_EXT;
504                         wcmd = ATA_CMD_PIO_WRITE_EXT;
505                 } else {
506                         rcmd = ATA_CMD_PIO_READ;
507                         wcmd = ATA_CMD_PIO_WRITE;
508                 }
509                 break;
510
511         case ATA_PROT_DMA:
512                 if (lba48) {
513                         rcmd = ATA_CMD_READ_EXT;
514                         wcmd = ATA_CMD_WRITE_EXT;
515                 } else {
516                         rcmd = ATA_CMD_READ;
517                         wcmd = ATA_CMD_WRITE;
518                 }
519                 break;
520
521         default:
522                 return -1;
523         }
524
525         return rcmd | (wcmd << 8);
526 }
527
528 /**
529  *      ata_dev_set_protocol - set taskfile protocol and r/w commands
530  *      @dev: device to examine and configure
531  *
532  *      Examine the device configuration, after we have
533  *      read the identify-device page and configured the
534  *      data transfer mode.  Set internal state related to
535  *      the ATA taskfile protocol (pio, pio mult, dma, etc.)
536  *      and calculate the proper read/write commands to use.
537  *
538  *      LOCKING:
539  *      caller.
540  */
541 static void ata_dev_set_protocol(struct ata_device *dev)
542 {
543         int pio = (dev->flags & ATA_DFLAG_PIO);
544         int lba48 = (dev->flags & ATA_DFLAG_LBA48);
545         int proto, cmd;
546
547         if (pio)
548                 proto = dev->xfer_protocol = ATA_PROT_PIO;
549         else
550                 proto = dev->xfer_protocol = ATA_PROT_DMA;
551
552         cmd = ata_prot_to_cmd(proto, lba48);
553         if (cmd < 0)
554                 BUG();
555
556         dev->read_cmd = cmd & 0xff;
557         dev->write_cmd = (cmd >> 8) & 0xff;
558 }
559
560 static const char * udma_str[] = {
561         "UDMA/16",
562         "UDMA/25",
563         "UDMA/33",
564         "UDMA/44",
565         "UDMA/66",
566         "UDMA/100",
567         "UDMA/133",
568         "UDMA7",
569 };
570
571 /**
572  *      ata_udma_string - convert UDMA bit offset to string
573  *      @udma_mask: mask of bits supported; only highest bit counts.
574  *
575  *      Determine string which represents the highest speed
576  *      (highest bit in @udma_mask).
577  *
578  *      LOCKING:
579  *      None.
580  *
581  *      RETURNS:
582  *      Constant C string representing highest speed listed in
583  *      @udma_mask, or the constant C string "<n/a>".
584  */
585
586 static const char *ata_udma_string(unsigned int udma_mask)
587 {
588         int i;
589
590         for (i = 7; i >= 0; i--) {
591                 if (udma_mask & (1 << i))
592                         return udma_str[i];
593         }
594
595         return "<n/a>";
596 }
597
598 /**
599  *      ata_pio_devchk - PATA device presence detection
600  *      @ap: ATA channel to examine
601  *      @device: Device to examine (starting at zero)
602  *
603  *      This technique was originally described in
604  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
605  *      later found its way into the ATA/ATAPI spec.
606  *
607  *      Write a pattern to the ATA shadow registers,
608  *      and if a device is present, it will respond by
609  *      correctly storing and echoing back the
610  *      ATA shadow register contents.
611  *
612  *      LOCKING:
613  *      caller.
614  */
615
616 static unsigned int ata_pio_devchk(struct ata_port *ap,
617                                    unsigned int device)
618 {
619         struct ata_ioports *ioaddr = &ap->ioaddr;
620         u8 nsect, lbal;
621
622         __ata_dev_select(ap, device);
623
624         outb(0x55, ioaddr->nsect_addr);
625         outb(0xaa, ioaddr->lbal_addr);
626
627         outb(0xaa, ioaddr->nsect_addr);
628         outb(0x55, ioaddr->lbal_addr);
629
630         outb(0x55, ioaddr->nsect_addr);
631         outb(0xaa, ioaddr->lbal_addr);
632
633         nsect = inb(ioaddr->nsect_addr);
634         lbal = inb(ioaddr->lbal_addr);
635
636         if ((nsect == 0x55) && (lbal == 0xaa))
637                 return 1;       /* we found a device */
638
639         return 0;               /* nothing found */
640 }
641
642 /**
643  *      ata_mmio_devchk - PATA device presence detection
644  *      @ap: ATA channel to examine
645  *      @device: Device to examine (starting at zero)
646  *
647  *      This technique was originally described in
648  *      Hale Landis's ATADRVR (www.ata-atapi.com), and
649  *      later found its way into the ATA/ATAPI spec.
650  *
651  *      Write a pattern to the ATA shadow registers,
652  *      and if a device is present, it will respond by
653  *      correctly storing and echoing back the
654  *      ATA shadow register contents.
655  *
656  *      LOCKING:
657  *      caller.
658  */
659
660 static unsigned int ata_mmio_devchk(struct ata_port *ap,
661                                     unsigned int device)
662 {
663         struct ata_ioports *ioaddr = &ap->ioaddr;
664         u8 nsect, lbal;
665
666         __ata_dev_select(ap, device);
667
668         writeb(0x55, (void *) ioaddr->nsect_addr);
669         writeb(0xaa, (void *) ioaddr->lbal_addr);
670
671         writeb(0xaa, (void *) ioaddr->nsect_addr);
672         writeb(0x55, (void *) ioaddr->lbal_addr);
673
674         writeb(0x55, (void *) ioaddr->nsect_addr);
675         writeb(0xaa, (void *) ioaddr->lbal_addr);
676
677         nsect = readb((void *) ioaddr->nsect_addr);
678         lbal = readb((void *) ioaddr->lbal_addr);
679
680         if ((nsect == 0x55) && (lbal == 0xaa))
681                 return 1;       /* we found a device */
682
683         return 0;               /* nothing found */
684 }
685
686 /**
687  *      ata_dev_devchk - PATA device presence detection
688  *      @ap: ATA channel to examine
689  *      @device: Device to examine (starting at zero)
690  *
691  *      Dispatch ATA device presence detection, depending
692  *      on whether we are using PIO or MMIO to talk to the
693  *      ATA shadow registers.
694  *
695  *      LOCKING:
696  *      caller.
697  */
698
699 static unsigned int ata_dev_devchk(struct ata_port *ap,
700                                     unsigned int device)
701 {
702         if (ap->flags & ATA_FLAG_MMIO)
703                 return ata_mmio_devchk(ap, device);
704         return ata_pio_devchk(ap, device);
705 }
706
707 /**
708  *      ata_dev_classify - determine device type based on ATA-spec signature
709  *      @tf: ATA taskfile register set for device to be identified
710  *
711  *      Determine from taskfile register contents whether a device is
712  *      ATA or ATAPI, as per "Signature and persistence" section
713  *      of ATA/PI spec (volume 1, sect 5.14).
714  *
715  *      LOCKING:
716  *      None.
717  *
718  *      RETURNS:
719  *      Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
720  *      the event of failure.
721  */
722
723 static unsigned int ata_dev_classify(struct ata_taskfile *tf)
724 {
725         /* Apple's open source Darwin code hints that some devices only
726          * put a proper signature into the LBA mid/high registers,
727          * So, we only check those.  It's sufficient for uniqueness.
728          */
729
730         if (((tf->lbam == 0) && (tf->lbah == 0)) ||
731             ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
732                 DPRINTK("found ATA device by sig\n");
733                 return ATA_DEV_ATA;
734         }
735
736         if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
737             ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
738                 DPRINTK("found ATAPI device by sig\n");
739                 return ATA_DEV_ATAPI;
740         }
741
742         DPRINTK("unknown device\n");
743         return ATA_DEV_UNKNOWN;
744 }
745
746 /**
747  *      ata_dev_try_classify - Parse returned ATA device signature
748  *      @ap: ATA channel to examine
749  *      @device: Device to examine (starting at zero)
750  *
751  *      After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
752  *      an ATA/ATAPI-defined set of values is placed in the ATA
753  *      shadow registers, indicating the results of device detection
754  *      and diagnostics.
755  *
756  *      Select the ATA device, and read the values from the ATA shadow
757  *      registers.  Then parse according to the Error register value,
758  *      and the spec-defined values examined by ata_dev_classify().
759  *
760  *      LOCKING:
761  *      caller.
762  */
763
764 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
765 {
766         struct ata_device *dev = &ap->device[device];
767         struct ata_taskfile tf;
768         unsigned int class;
769         u8 err;
770
771         __ata_dev_select(ap, device);
772
773         memset(&tf, 0, sizeof(tf));
774
775         err = ata_chk_err(ap);
776         ap->ops->tf_read(ap, &tf);
777
778         dev->class = ATA_DEV_NONE;
779
780         /* see if device passed diags */
781         if (err == 1)
782                 /* do nothing */ ;
783         else if ((device == 0) && (err == 0x81))
784                 /* do nothing */ ;
785         else
786                 return err;
787
788         /* determine if device if ATA or ATAPI */
789         class = ata_dev_classify(&tf);
790         if (class == ATA_DEV_UNKNOWN)
791                 return err;
792         if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
793                 return err;
794
795         dev->class = class;
796
797         return err;
798 }
799
800 /**
801  *      ata_dev_id_string - Convert IDENTIFY DEVICE page into string
802  *      @dev: Device whose IDENTIFY DEVICE results we will examine
803  *      @s: string into which data is output
804  *      @ofs: offset into identify device page
805  *      @len: length of string to return. must be an even number.
806  *
807  *      The strings in the IDENTIFY DEVICE page are broken up into
808  *      16-bit chunks.  Run through the string, and output each
809  *      8-bit chunk linearly, regardless of platform.
810  *
811  *      LOCKING:
812  *      caller.
813  */
814
815 void ata_dev_id_string(struct ata_device *dev, unsigned char *s,
816                        unsigned int ofs, unsigned int len)
817 {
818         unsigned int c;
819
820         while (len > 0) {
821                 c = dev->id[ofs] >> 8;
822                 *s = c;
823                 s++;
824
825                 c = dev->id[ofs] & 0xff;
826                 *s = c;
827                 s++;
828
829                 ofs++;
830                 len -= 2;
831         }
832 }
833
834 /**
835  *      __ata_dev_select - Select device 0/1 on ATA bus
836  *      @ap: ATA channel to manipulate
837  *      @device: ATA device (numbered from zero) to select
838  *
839  *      Use the method defined in the ATA specification to
840  *      make either device 0, or device 1, active on the
841  *      ATA channel.
842  *
843  *      LOCKING:
844  *      caller.
845  */
846
847 static void __ata_dev_select (struct ata_port *ap, unsigned int device)
848 {
849         u8 tmp;
850
851         if (device == 0)
852                 tmp = ATA_DEVICE_OBS;
853         else
854                 tmp = ATA_DEVICE_OBS | ATA_DEV1;
855
856         if (ap->flags & ATA_FLAG_MMIO) {
857                 writeb(tmp, (void *) ap->ioaddr.device_addr);
858         } else {
859                 outb(tmp, ap->ioaddr.device_addr);
860         }
861         ata_pause(ap);          /* needed; also flushes, for mmio */
862 }
863
864 /**
865  *      ata_dev_select - Select device 0/1 on ATA bus
866  *      @ap: ATA channel to manipulate
867  *      @device: ATA device (numbered from zero) to select
868  *      @wait: non-zero to wait for Status register BSY bit to clear
869  *      @can_sleep: non-zero if context allows sleeping
870  *
871  *      Use the method defined in the ATA specification to
872  *      make either device 0, or device 1, active on the
873  *      ATA channel.
874  *
875  *      This is a high-level version of __ata_dev_select(),
876  *      which additionally provides the services of inserting
877  *      the proper pauses and status polling, where needed.
878  *
879  *      LOCKING:
880  *      caller.
881  */
882
883 void ata_dev_select(struct ata_port *ap, unsigned int device,
884                            unsigned int wait, unsigned int can_sleep)
885 {
886         VPRINTK("ENTER, ata%u: device %u, wait %u\n",
887                 ap->id, device, wait);
888
889         if (wait)
890                 ata_wait_idle(ap);
891
892         __ata_dev_select(ap, device);
893
894         if (wait) {
895                 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
896                         msleep(150);
897                 ata_wait_idle(ap);
898         }
899 }
900
901 /**
902  *      ata_dump_id - IDENTIFY DEVICE info debugging output
903  *      @dev: Device whose IDENTIFY DEVICE page we will dump
904  *
905  *      Dump selected 16-bit words from a detected device's
906  *      IDENTIFY PAGE page.
907  *
908  *      LOCKING:
909  *      caller.
910  */
911
912 static inline void ata_dump_id(struct ata_device *dev)
913 {
914         DPRINTK("49==0x%04x  "
915                 "53==0x%04x  "
916                 "63==0x%04x  "
917                 "64==0x%04x  "
918                 "75==0x%04x  \n",
919                 dev->id[49],
920                 dev->id[53],
921                 dev->id[63],
922                 dev->id[64],
923                 dev->id[75]);
924         DPRINTK("80==0x%04x  "
925                 "81==0x%04x  "
926                 "82==0x%04x  "
927                 "83==0x%04x  "
928                 "84==0x%04x  \n",
929                 dev->id[80],
930                 dev->id[81],
931                 dev->id[82],
932                 dev->id[83],
933                 dev->id[84]);
934         DPRINTK("88==0x%04x  "
935                 "93==0x%04x\n",
936                 dev->id[88],
937                 dev->id[93]);
938 }
939
940 /**
941  *      ata_dev_identify - obtain IDENTIFY x DEVICE page
942  *      @ap: port on which device we wish to probe resides
943  *      @device: device bus address, starting at zero
944  *
945  *      Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
946  *      command, and read back the 512-byte device information page.
947  *      The device information page is fed to us via the standard
948  *      PIO-IN protocol, but we hand-code it here. (TODO: investigate
949  *      using standard PIO-IN paths)
950  *
951  *      After reading the device information page, we use several
952  *      bits of information from it to initialize data structures
953  *      that will be used during the lifetime of the ata_device.
954  *      Other data from the info page is used to disqualify certain
955  *      older ATA devices we do not wish to support.
956  *
957  *      LOCKING:
958  *      Inherited from caller.  Some functions called by this function
959  *      obtain the host_set lock.
960  */
961
962 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
963 {
964         struct ata_device *dev = &ap->device[device];
965         unsigned int i;
966         u16 tmp, udma_modes;
967         u8 status;
968         struct ata_taskfile tf;
969         unsigned int using_edd;
970
971         if (!ata_dev_present(dev)) {
972                 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
973                         ap->id, device);
974                 return;
975         }
976
977         if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
978                 using_edd = 0;
979         else
980                 using_edd = 1;
981
982         DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
983
984         assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
985                 dev->class == ATA_DEV_NONE);
986
987         ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
988
989 retry:
990         ata_tf_init(ap, &tf, device);
991         tf.ctl |= ATA_NIEN;
992         tf.protocol = ATA_PROT_PIO;
993
994         if (dev->class == ATA_DEV_ATA) {
995                 tf.command = ATA_CMD_ID_ATA;
996                 DPRINTK("do ATA identify\n");
997         } else {
998                 tf.command = ATA_CMD_ID_ATAPI;
999                 DPRINTK("do ATAPI identify\n");
1000         }
1001
1002         ata_tf_to_host(ap, &tf);
1003
1004         /* crazy ATAPI devices... */
1005         if (dev->class == ATA_DEV_ATAPI)
1006                 msleep(150);
1007
1008         if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT))
1009                 goto err_out;
1010
1011         status = ata_chk_status(ap);
1012         if (status & ATA_ERR) {
1013                 /*
1014                  * arg!  EDD works for all test cases, but seems to return
1015                  * the ATA signature for some ATAPI devices.  Until the
1016                  * reason for this is found and fixed, we fix up the mess
1017                  * here.  If IDENTIFY DEVICE returns command aborted
1018                  * (as ATAPI devices do), then we issue an
1019                  * IDENTIFY PACKET DEVICE.
1020                  *
1021                  * ATA software reset (SRST, the default) does not appear
1022                  * to have this problem.
1023                  */
1024                 if ((using_edd) && (tf.command == ATA_CMD_ID_ATA)) {
1025                         u8 err = ata_chk_err(ap);
1026                         if (err & ATA_ABORTED) {
1027                                 dev->class = ATA_DEV_ATAPI;
1028                                 goto retry;
1029                         }
1030                 }
1031                 goto err_out;
1032         }
1033
1034         /* make sure we have BSY=0, DRQ=1 */
1035         if ((status & ATA_DRQ) == 0) {
1036                 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) not returning id page (0x%x)\n",
1037                        ap->id, device,
1038                        dev->class == ATA_DEV_ATA ? "" : "PI",
1039                        status);
1040                 goto err_out;
1041         }
1042
1043         /* read IDENTIFY [X] DEVICE page */
1044         if (ap->flags & ATA_FLAG_MMIO) {
1045                 for (i = 0; i < ATA_ID_WORDS; i++)
1046                         dev->id[i] = readw((void *)ap->ioaddr.data_addr);
1047         } else
1048                 for (i = 0; i < ATA_ID_WORDS; i++)
1049                         dev->id[i] = inw(ap->ioaddr.data_addr);
1050
1051         /* wait for host_idle */
1052         status = ata_wait_idle(ap);
1053         if (status & (ATA_BUSY | ATA_DRQ)) {
1054                 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) error after id page (0x%x)\n",
1055                        ap->id, device,
1056                        dev->class == ATA_DEV_ATA ? "" : "PI",
1057                        status);
1058                 goto err_out;
1059         }
1060
1061         ata_irq_on(ap); /* re-enable interrupts */
1062
1063         /* print device capabilities */
1064         printk(KERN_DEBUG "ata%u: dev %u cfg "
1065                "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1066                ap->id, device, dev->id[49],
1067                dev->id[82], dev->id[83], dev->id[84],
1068                dev->id[85], dev->id[86], dev->id[87],
1069                dev->id[88]);
1070
1071         /*
1072          * common ATA, ATAPI feature tests
1073          */
1074
1075         /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1076         if (!ata_id_has_dma(dev) || !ata_id_has_lba(dev)) {
1077                 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1078                 goto err_out_nosup;
1079         }
1080
1081         /* we require UDMA support */
1082         udma_modes =
1083         tmp = dev->id[ATA_ID_UDMA_MODES];
1084         if ((tmp & 0xff) == 0) {
1085                 printk(KERN_DEBUG "ata%u: no udma\n", ap->id);
1086                 goto err_out_nosup;
1087         }
1088
1089         ata_dump_id(dev);
1090
1091         /* ATA-specific feature tests */
1092         if (dev->class == ATA_DEV_ATA) {
1093                 if (!ata_id_is_ata(dev))        /* sanity check */
1094                         goto err_out_nosup;
1095
1096                 tmp = dev->id[ATA_ID_MAJOR_VER];
1097                 for (i = 14; i >= 1; i--)
1098                         if (tmp & (1 << i))
1099                                 break;
1100
1101                 /* we require at least ATA-3 */
1102                 if (i < 3) {
1103                         printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1104                         goto err_out_nosup;
1105                 }
1106
1107                 if (ata_id_has_lba48(dev)) {
1108                         dev->flags |= ATA_DFLAG_LBA48;
1109                         dev->n_sectors = ata_id_u64(dev, 100);
1110                 } else {
1111                         dev->n_sectors = ata_id_u32(dev, 60);
1112                 }
1113
1114                 ap->host->max_cmd_len = 16;
1115
1116                 /* print device info to dmesg */
1117                 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1118                        ap->id, device,
1119                        ata_udma_string(udma_modes),
1120                        (unsigned long long)dev->n_sectors,
1121                        dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1122         }
1123
1124         /* ATAPI-specific feature tests */
1125         else {
1126                 if (ata_id_is_ata(dev))         /* sanity check */
1127                         goto err_out_nosup;
1128
1129                 /* see if 16-byte commands supported */
1130                 tmp = dev->id[0] & 0x3;
1131                 if (tmp == 1)
1132                         ap->host->max_cmd_len = 16;
1133
1134                 /* print device info to dmesg */
1135                 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1136                        ap->id, device,
1137                        ata_udma_string(udma_modes));
1138         }
1139
1140         DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1141         return;
1142
1143 err_out_nosup:
1144         printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1145                ap->id, device);
1146 err_out:
1147         ata_irq_on(ap); /* re-enable interrupts */
1148         dev->class++;   /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1149         DPRINTK("EXIT, err\n");
1150 }
1151
1152 /**
1153  *      ata_port_reset -
1154  *      @ap:
1155  *
1156  *      LOCKING:
1157  */
1158
1159 static void ata_port_reset(struct ata_port *ap)
1160 {
1161         unsigned int i, found = 0;
1162
1163         ap->ops->phy_reset(ap);
1164         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1165                 goto err_out;
1166
1167         for (i = 0; i < ATA_MAX_DEVICES; i++) {
1168                 ata_dev_identify(ap, i);
1169                 if (ata_dev_present(&ap->device[i])) {
1170                         found = 1;
1171                         if (ap->ops->dev_config)
1172                                 ap->ops->dev_config(ap, &ap->device[i]);
1173                 }
1174         }
1175
1176         if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1177                 goto err_out_disable;
1178
1179         ata_set_mode(ap);
1180         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1181                 goto err_out_disable;
1182
1183         ap->thr_state = THR_PROBE_SUCCESS;
1184
1185         return;
1186
1187 err_out_disable:
1188         ap->ops->port_disable(ap);
1189 err_out:
1190         ap->thr_state = THR_PROBE_FAILED;
1191 }
1192
1193 /**
1194  *      ata_port_probe -
1195  *      @ap:
1196  *
1197  *      LOCKING:
1198  */
1199
1200 void ata_port_probe(struct ata_port *ap)
1201 {
1202         ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1203 }
1204
1205 /**
1206  *      sata_phy_reset -
1207  *      @ap:
1208  *
1209  *      LOCKING:
1210  *
1211  */
1212 void sata_phy_reset(struct ata_port *ap)
1213 {
1214         u32 sstatus;
1215         unsigned long timeout = jiffies + (HZ * 5);
1216
1217         if (ap->flags & ATA_FLAG_SATA_RESET) {
1218                 scr_write(ap, SCR_CONTROL, 0x301); /* issue phy wake/reset */
1219                 scr_read(ap, SCR_STATUS);       /* dummy read; flush */
1220                 udelay(400);                    /* FIXME: a guess */
1221         }
1222         scr_write(ap, SCR_CONTROL, 0x300);      /* issue phy wake/clear reset */
1223
1224         /* wait for phy to become ready, if necessary */
1225         do {
1226                 msleep(200);
1227                 sstatus = scr_read(ap, SCR_STATUS);
1228                 if ((sstatus & 0xf) != 1)
1229                         break;
1230         } while (time_before(jiffies, timeout));
1231
1232         /* TODO: phy layer with polling, timeouts, etc. */
1233         if (sata_dev_present(ap))
1234                 ata_port_probe(ap);
1235         else {
1236                 sstatus = scr_read(ap, SCR_STATUS);
1237                 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1238                        ap->id, sstatus);
1239                 ata_port_disable(ap);
1240         }
1241
1242         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1243                 return;
1244
1245         if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1246                 ata_port_disable(ap);
1247                 return;
1248         }
1249
1250         ata_bus_reset(ap);
1251 }
1252
1253 /**
1254  *      ata_port_disable -
1255  *      @ap:
1256  *
1257  *      LOCKING:
1258  */
1259
1260 void ata_port_disable(struct ata_port *ap)
1261 {
1262         ap->device[0].class = ATA_DEV_NONE;
1263         ap->device[1].class = ATA_DEV_NONE;
1264         ap->flags |= ATA_FLAG_PORT_DISABLED;
1265 }
1266
1267 /**
1268  *      ata_set_mode - Program timings and issue SET FEATURES - XFER
1269  *      @ap: port on which timings will be programmed
1270  *
1271  *      LOCKING:
1272  *
1273  */
1274 static void ata_set_mode(struct ata_port *ap)
1275 {
1276         unsigned int force_pio, i;
1277
1278         ata_host_set_pio(ap);
1279         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1280                 return;
1281
1282         ata_host_set_udma(ap);
1283         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1284                 return;
1285
1286 #ifdef ATA_FORCE_PIO
1287         force_pio = 1;
1288 #else
1289         force_pio = 0;
1290 #endif
1291
1292         if (force_pio) {
1293                 ata_dev_set_pio(ap, 0);
1294                 ata_dev_set_pio(ap, 1);
1295         } else {
1296                 ata_dev_set_udma(ap, 0);
1297                 ata_dev_set_udma(ap, 1);
1298         }
1299
1300         if (ap->flags & ATA_FLAG_PORT_DISABLED)
1301                 return;
1302
1303         if (ap->ops->post_set_mode)
1304                 ap->ops->post_set_mode(ap);
1305
1306         for (i = 0; i < 2; i++) {
1307                 struct ata_device *dev = &ap->device[i];
1308                 ata_dev_set_protocol(dev);
1309         }
1310 }
1311
1312 /**
1313  *      ata_busy_sleep - sleep until BSY clears, or timeout
1314  *      @ap: port containing status register to be polled
1315  *      @tmout_pat: impatience timeout
1316  *      @tmout: overall timeout
1317  *
1318  *      LOCKING:
1319  *
1320  */
1321
1322 static unsigned int ata_busy_sleep (struct ata_port *ap,
1323                                     unsigned long tmout_pat,
1324                                     unsigned long tmout)
1325 {
1326         unsigned long timer_start, timeout;
1327         u8 status;
1328
1329         status = ata_busy_wait(ap, ATA_BUSY, 300);
1330         timer_start = jiffies;
1331         timeout = timer_start + tmout_pat;
1332         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1333                 msleep(50);
1334                 status = ata_busy_wait(ap, ATA_BUSY, 3);
1335         }
1336
1337         if (status & ATA_BUSY)
1338                 printk(KERN_WARNING "ata%u is slow to respond, "
1339                        "please be patient\n", ap->id);
1340
1341         timeout = timer_start + tmout;
1342         while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1343                 msleep(50);
1344                 status = ata_chk_status(ap);
1345         }
1346
1347         if (status & ATA_BUSY) {
1348                 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1349                        ap->id, tmout / HZ);
1350                 return 1;
1351         }
1352
1353         return 0;
1354 }
1355
1356 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1357 {
1358         struct ata_ioports *ioaddr = &ap->ioaddr;
1359         unsigned int dev0 = devmask & (1 << 0);
1360         unsigned int dev1 = devmask & (1 << 1);
1361         unsigned long timeout;
1362
1363         /* if device 0 was found in ata_dev_devchk, wait for its
1364          * BSY bit to clear
1365          */
1366         if (dev0)
1367                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1368
1369         /* if device 1 was found in ata_dev_devchk, wait for
1370          * register access, then wait for BSY to clear
1371          */
1372         timeout = jiffies + ATA_TMOUT_BOOT;
1373         while (dev1) {
1374                 u8 nsect, lbal;
1375
1376                 __ata_dev_select(ap, 1);
1377                 if (ap->flags & ATA_FLAG_MMIO) {
1378                         nsect = readb((void *) ioaddr->nsect_addr);
1379                         lbal = readb((void *) ioaddr->lbal_addr);
1380                 } else {
1381                         nsect = inb(ioaddr->nsect_addr);
1382                         lbal = inb(ioaddr->lbal_addr);
1383                 }
1384                 if ((nsect == 1) && (lbal == 1))
1385                         break;
1386                 if (time_after(jiffies, timeout)) {
1387                         dev1 = 0;
1388                         break;
1389                 }
1390                 msleep(50);     /* give drive a breather */
1391         }
1392         if (dev1)
1393                 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1394
1395         /* is all this really necessary? */
1396         __ata_dev_select(ap, 0);
1397         if (dev1)
1398                 __ata_dev_select(ap, 1);
1399         if (dev0)
1400                 __ata_dev_select(ap, 0);
1401 }
1402
1403 /**
1404  *      ata_bus_edd -
1405  *      @ap:
1406  *
1407  *      LOCKING:
1408  *
1409  */
1410
1411 static unsigned int ata_bus_edd(struct ata_port *ap)
1412 {
1413         struct ata_taskfile tf;
1414
1415         /* set up execute-device-diag (bus reset) taskfile */
1416         /* also, take interrupts to a known state (disabled) */
1417         DPRINTK("execute-device-diag\n");
1418         ata_tf_init(ap, &tf, 0);
1419         tf.ctl |= ATA_NIEN;
1420         tf.command = ATA_CMD_EDD;
1421         tf.protocol = ATA_PROT_NODATA;
1422
1423         /* do bus reset */
1424         ata_tf_to_host(ap, &tf);
1425
1426         /* spec says at least 2ms.  but who knows with those
1427          * crazy ATAPI devices...
1428          */
1429         msleep(150);
1430
1431         return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1432 }
1433
1434 static unsigned int ata_bus_softreset(struct ata_port *ap,
1435                                       unsigned int devmask)
1436 {
1437         struct ata_ioports *ioaddr = &ap->ioaddr;
1438
1439         DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1440
1441         /* software reset.  causes dev0 to be selected */
1442         if (ap->flags & ATA_FLAG_MMIO) {
1443                 writeb(ap->ctl, ioaddr->ctl_addr);
1444                 udelay(20);     /* FIXME: flush */
1445                 writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1446                 udelay(20);     /* FIXME: flush */
1447                 writeb(ap->ctl, ioaddr->ctl_addr);
1448         } else {
1449                 outb(ap->ctl, ioaddr->ctl_addr);
1450                 udelay(10);
1451                 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1452                 udelay(10);
1453                 outb(ap->ctl, ioaddr->ctl_addr);
1454         }
1455
1456         /* spec mandates ">= 2ms" before checking status.
1457          * We wait 150ms, because that was the magic delay used for
1458          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1459          * between when the ATA command register is written, and then
1460          * status is checked.  Because waiting for "a while" before
1461          * checking status is fine, post SRST, we perform this magic
1462          * delay here as well.
1463          */
1464         msleep(150);
1465
1466         ata_bus_post_reset(ap, devmask);
1467
1468         return 0;
1469 }
1470
1471 /**
1472  *      ata_bus_reset - reset host port and associated ATA channel
1473  *      @ap: port to reset
1474  *
1475  *      This is typically the first time we actually start issuing
1476  *      commands to the ATA channel.  We wait for BSY to clear, then
1477  *      issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1478  *      result.  Determine what devices, if any, are on the channel
1479  *      by looking at the device 0/1 error register.  Look at the signature
1480  *      stored in each device's taskfile registers, to determine if
1481  *      the device is ATA or ATAPI.
1482  *
1483  *      LOCKING:
1484  *      Inherited from caller.  Some functions called by this function
1485  *      obtain the host_set lock.
1486  *
1487  *      SIDE EFFECTS:
1488  *      Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1489  */
1490
1491 void ata_bus_reset(struct ata_port *ap)
1492 {
1493         struct ata_ioports *ioaddr = &ap->ioaddr;
1494         unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1495         u8 err;
1496         unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1497
1498         DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1499
1500         /* determine if device 0/1 are present */
1501         if (ap->flags & ATA_FLAG_SATA_RESET)
1502                 dev0 = 1;
1503         else {
1504                 dev0 = ata_dev_devchk(ap, 0);
1505                 if (slave_possible)
1506                         dev1 = ata_dev_devchk(ap, 1);
1507         }
1508
1509         if (dev0)
1510                 devmask |= (1 << 0);
1511         if (dev1)
1512                 devmask |= (1 << 1);
1513
1514         /* select device 0 again */
1515         __ata_dev_select(ap, 0);
1516
1517         /* issue bus reset */
1518         if (ap->flags & ATA_FLAG_SRST)
1519                 rc = ata_bus_softreset(ap, devmask);
1520         else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1521                 /* set up device control */
1522                 if (ap->flags & ATA_FLAG_MMIO)
1523                         writeb(ap->ctl, ioaddr->ctl_addr);
1524                 else
1525                         outb(ap->ctl, ioaddr->ctl_addr);
1526                 rc = ata_bus_edd(ap);
1527         }
1528
1529         if (rc)
1530                 goto err_out;
1531
1532         /*
1533          * determine by signature whether we have ATA or ATAPI devices
1534          */
1535         err = ata_dev_try_classify(ap, 0);
1536         if ((slave_possible) && (err != 0x81))
1537                 ata_dev_try_classify(ap, 1);
1538
1539         /* re-enable interrupts */
1540         ata_irq_on(ap);
1541
1542         /* is double-select really necessary? */
1543         if (ap->device[1].class != ATA_DEV_NONE)
1544                 __ata_dev_select(ap, 1);
1545         if (ap->device[0].class != ATA_DEV_NONE)
1546                 __ata_dev_select(ap, 0);
1547
1548         /* if no devices were detected, disable this port */
1549         if ((ap->device[0].class == ATA_DEV_NONE) &&
1550             (ap->device[1].class == ATA_DEV_NONE))
1551                 goto err_out;
1552
1553         if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1554                 /* set up device control for ATA_FLAG_SATA_RESET */
1555                 if (ap->flags & ATA_FLAG_MMIO)
1556                         writeb(ap->ctl, ioaddr->ctl_addr);
1557                 else
1558                         outb(ap->ctl, ioaddr->ctl_addr);
1559         }
1560
1561         DPRINTK("EXIT\n");
1562         return;
1563
1564 err_out:
1565         printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1566         ap->ops->port_disable(ap);
1567
1568         DPRINTK("EXIT\n");
1569 }
1570
1571 /**
1572  *      ata_host_set_pio -
1573  *      @ap:
1574  *
1575  *      LOCKING:
1576  */
1577
1578 static void ata_host_set_pio(struct ata_port *ap)
1579 {
1580         struct ata_device *master, *slave;
1581         unsigned int pio, i;
1582         u16 mask;
1583
1584         master = &ap->device[0];
1585         slave = &ap->device[1];
1586
1587         assert (ata_dev_present(master) || ata_dev_present(slave));
1588
1589         mask = ap->pio_mask;
1590         if (ata_dev_present(master))
1591                 mask &= (master->id[ATA_ID_PIO_MODES] & 0x03);
1592         if (ata_dev_present(slave))
1593                 mask &= (slave->id[ATA_ID_PIO_MODES] & 0x03);
1594
1595         /* require pio mode 3 or 4 support for host and all devices */
1596         if (mask == 0) {
1597                 printk(KERN_WARNING "ata%u: no PIO3/4 support, ignoring\n",
1598                        ap->id);
1599                 goto err_out;
1600         }
1601
1602         pio = (mask & ATA_ID_PIO4) ? 4 : 3;
1603         for (i = 0; i < ATA_MAX_DEVICES; i++)
1604                 if (ata_dev_present(&ap->device[i])) {
1605                         ap->device[i].pio_mode = (pio == 3) ?
1606                                 XFER_PIO_3 : XFER_PIO_4;
1607                         if (ap->ops->set_piomode)
1608                                 ap->ops->set_piomode(ap, &ap->device[i], pio);
1609                 }
1610
1611         return;
1612
1613 err_out:
1614         ap->ops->port_disable(ap);
1615 }
1616
1617 /**
1618  *      ata_host_set_udma -
1619  *      @ap:
1620  *
1621  *      LOCKING:
1622  */
1623
1624 static void ata_host_set_udma(struct ata_port *ap)
1625 {
1626         struct ata_device *master, *slave;
1627         u16 mask;
1628         unsigned int i, j;
1629         int udma_mode = -1;
1630
1631         master = &ap->device[0];
1632         slave = &ap->device[1];
1633
1634         assert (ata_dev_present(master) || ata_dev_present(slave));
1635         assert ((ap->flags & ATA_FLAG_PORT_DISABLED) == 0);
1636
1637         DPRINTK("udma masks: host 0x%X, master 0x%X, slave 0x%X\n",
1638                 ap->udma_mask,
1639                 (!ata_dev_present(master)) ? 0xff :
1640                         (master->id[ATA_ID_UDMA_MODES] & 0xff),
1641                 (!ata_dev_present(slave)) ? 0xff :
1642                         (slave->id[ATA_ID_UDMA_MODES] & 0xff));
1643
1644         mask = ap->udma_mask;
1645         if (ata_dev_present(master))
1646                 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1647         if (ata_dev_present(slave))
1648                 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1649
1650         i = XFER_UDMA_7;
1651         while (i >= XFER_UDMA_0) {
1652                 j = i - XFER_UDMA_0;
1653                 DPRINTK("mask 0x%X i 0x%X j %u\n", mask, i, j);
1654                 if (mask & (1 << j)) {
1655                         udma_mode = i;
1656                         break;
1657                 }
1658
1659                 i--;
1660         }
1661
1662         /* require udma for host and all attached devices */
1663         if (udma_mode < 0) {
1664                 printk(KERN_WARNING "ata%u: no UltraDMA support, ignoring\n",
1665                        ap->id);
1666                 goto err_out;
1667         }
1668
1669         for (i = 0; i < ATA_MAX_DEVICES; i++)
1670                 if (ata_dev_present(&ap->device[i])) {
1671                         ap->device[i].udma_mode = udma_mode;
1672                         if (ap->ops->set_udmamode)
1673                                 ap->ops->set_udmamode(ap, &ap->device[i],
1674                                                       udma_mode);
1675                 }
1676
1677         return;
1678
1679 err_out:
1680         ap->ops->port_disable(ap);
1681 }
1682
1683 /**
1684  *      ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1685  *      @ap: Port associated with device @dev
1686  *      @dev: Device to which command will be sent
1687  *
1688  *      LOCKING:
1689  */
1690
1691 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1692 {
1693         struct ata_taskfile tf;
1694
1695         /* set up set-features taskfile */
1696         DPRINTK("set features - xfer mode\n");
1697         ata_tf_init(ap, &tf, dev->devno);
1698         tf.ctl |= ATA_NIEN;
1699         tf.command = ATA_CMD_SET_FEATURES;
1700         tf.feature = SETFEATURES_XFER;
1701         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1702         tf.protocol = ATA_PROT_NODATA;
1703         if (dev->flags & ATA_DFLAG_PIO)
1704                 tf.nsect = dev->pio_mode;
1705         else
1706                 tf.nsect = dev->udma_mode;
1707
1708         /* do bus reset */
1709         ata_tf_to_host(ap, &tf);
1710
1711         /* crazy ATAPI devices... */
1712         if (dev->class == ATA_DEV_ATAPI)
1713                 msleep(150);
1714
1715         ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1716
1717         ata_irq_on(ap); /* re-enable interrupts */
1718
1719         ata_wait_idle(ap);
1720
1721         DPRINTK("EXIT\n");
1722 }
1723
1724 /**
1725  *      ata_dev_set_udma - Set ATA device's transfer mode to Ultra DMA
1726  *      @ap: Port associated with device @dev
1727  *      @device: Device whose mode will be set
1728  *
1729  *      LOCKING:
1730  */
1731
1732 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device)
1733 {
1734         struct ata_device *dev = &ap->device[device];
1735
1736         if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1737                 return;
1738
1739         ata_dev_set_xfermode(ap, dev);
1740
1741         assert((dev->udma_mode >= XFER_UDMA_0) &&
1742                (dev->udma_mode <= XFER_UDMA_7));
1743         printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1744                ap->id, device,
1745                udma_str[dev->udma_mode - XFER_UDMA_0]);
1746 }
1747
1748 /**
1749  *      ata_dev_set_pio - Set ATA device's transfer mode to PIO
1750  *      @ap: Port associated with device @dev
1751  *      @device: Device whose mode will be set
1752  *
1753  *      LOCKING:
1754  */
1755
1756 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device)
1757 {
1758         struct ata_device *dev = &ap->device[device];
1759
1760         if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1761                 return;
1762
1763         /* force PIO mode */
1764         dev->flags |= ATA_DFLAG_PIO;
1765
1766         ata_dev_set_xfermode(ap, dev);
1767
1768         assert((dev->pio_mode >= XFER_PIO_3) &&
1769                (dev->pio_mode <= XFER_PIO_4));
1770         printk(KERN_INFO "ata%u: dev %u configured for PIO%c\n",
1771                ap->id, device,
1772                dev->pio_mode == 3 ? '3' : '4');
1773 }
1774
1775 /**
1776  *      ata_sg_clean -
1777  *      @qc:
1778  *
1779  *      LOCKING:
1780  */
1781
1782 static void ata_sg_clean(struct ata_queued_cmd *qc)
1783 {
1784         struct ata_port *ap = qc->ap;
1785         struct scsi_cmnd *cmd = qc->scsicmd;
1786         struct scatterlist *sg = qc->sg;
1787         int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1788
1789         assert(dir == SCSI_DATA_READ || dir == SCSI_DATA_WRITE);
1790         assert(qc->flags & ATA_QCFLAG_SG);
1791         assert(sg != NULL);
1792
1793         if (!cmd->use_sg)
1794                 assert(qc->n_elem == 1);
1795
1796         DPRINTK("unmapping %u sg elements\n", qc->n_elem);
1797
1798         if (cmd->use_sg)
1799                 pci_unmap_sg(ap->host_set->pdev, sg, qc->n_elem, dir);
1800         else
1801                 pci_unmap_single(ap->host_set->pdev, sg_dma_address(&sg[0]),
1802                                  sg_dma_len(&sg[0]), dir);
1803
1804         qc->flags &= ~ATA_QCFLAG_SG;
1805         qc->sg = NULL;
1806 }
1807
1808 /**
1809  *      ata_fill_sg -
1810  *      @qc:
1811  *
1812  *      LOCKING:
1813  *
1814  */
1815 void ata_fill_sg(struct ata_queued_cmd *qc)
1816 {
1817         struct scatterlist *sg = qc->sg;
1818         struct ata_port *ap = qc->ap;
1819         unsigned int idx, nelem;
1820
1821         assert(sg != NULL);
1822         assert(qc->n_elem > 0);
1823
1824         idx = 0;
1825         for (nelem = qc->n_elem; nelem; nelem--,sg++) {
1826                 u32 addr, boundary;
1827                 u32 sg_len, len;
1828
1829                 /* determine if physical DMA addr spans 64K boundary.
1830                  * Note h/w doesn't support 64-bit, so we unconditionally
1831                  * truncate dma_addr_t to u32.
1832                  */
1833                 addr = (u32) sg_dma_address(sg);
1834                 sg_len = sg_dma_len(sg);
1835
1836                 while (sg_len) {
1837                         boundary = (addr & ~0xffff) + (0xffff + 1);
1838                         len = sg_len;
1839                         if ((addr + sg_len) > boundary)
1840                                 len = boundary - addr;
1841
1842                         ap->prd[idx].addr = cpu_to_le32(addr);
1843                         ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
1844                         VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
1845
1846                         idx++;
1847                         sg_len -= len;
1848                         addr += len;
1849                 }
1850         }
1851
1852         if (idx)
1853                 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
1854 }
1855
1856 /**
1857  *      ata_sg_setup_one -
1858  *      @qc:
1859  *
1860  *      LOCKING:
1861  *      spin_lock_irqsave(host_set lock)
1862  *
1863  *      RETURNS:
1864  *
1865  */
1866
1867 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
1868 {
1869         struct ata_port *ap = qc->ap;
1870         struct scsi_cmnd *cmd = qc->scsicmd;
1871         int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1872         struct scatterlist *sg = qc->sg;
1873         unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1874         dma_addr_t dma_address;
1875
1876         assert(sg == &qc->sgent);
1877         assert(qc->n_elem == 1);
1878
1879         sg->page = virt_to_page(cmd->request_buffer);
1880         sg->offset = (unsigned long) cmd->request_buffer & ~PAGE_MASK;
1881         sg_dma_len(sg) = cmd->request_bufflen;
1882
1883         if (!have_sg)
1884                 return 0;
1885
1886         dma_address = pci_map_single(ap->host_set->pdev, cmd->request_buffer,
1887                                      cmd->request_bufflen, dir);
1888         if (pci_dma_mapping_error(dma_address))
1889                 return -1;
1890
1891         sg_dma_address(sg) = dma_address;
1892
1893         DPRINTK("mapped buffer of %d bytes for %s\n", cmd->request_bufflen,
1894                 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1895
1896         return 0;
1897 }
1898
1899 /**
1900  *      ata_sg_setup -
1901  *      @qc:
1902  *
1903  *      LOCKING:
1904  *      spin_lock_irqsave(host_set lock)
1905  *
1906  *      RETURNS:
1907  *
1908  */
1909
1910 static int ata_sg_setup(struct ata_queued_cmd *qc)
1911 {
1912         struct ata_port *ap = qc->ap;
1913         struct scsi_cmnd *cmd = qc->scsicmd;
1914         struct scatterlist *sg;
1915         int n_elem;
1916         unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1917
1918         VPRINTK("ENTER, ata%u, use_sg %d\n", ap->id, cmd->use_sg);
1919         assert(cmd->use_sg > 0);
1920
1921         sg = (struct scatterlist *)cmd->request_buffer;
1922         if (have_sg) {
1923                 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1924                 n_elem = pci_map_sg(ap->host_set->pdev, sg, cmd->use_sg, dir);
1925                 if (n_elem < 1)
1926                         return -1;
1927                 DPRINTK("%d sg elements mapped\n", n_elem);
1928         } else {
1929                 n_elem = cmd->use_sg;
1930         }
1931         qc->n_elem = n_elem;
1932
1933         return 0;
1934 }
1935
1936 /**
1937  *      ata_pio_poll -
1938  *      @ap:
1939  *
1940  *      LOCKING:
1941  *
1942  *      RETURNS:
1943  *
1944  */
1945
1946 static unsigned long ata_pio_poll(struct ata_port *ap)
1947 {
1948         u8 status;
1949         unsigned int poll_state = PIO_ST_UNKNOWN;
1950         unsigned int reg_state = PIO_ST_UNKNOWN;
1951         const unsigned int tmout_state = PIO_ST_TMOUT;
1952
1953         switch (ap->pio_task_state) {
1954         case PIO_ST:
1955         case PIO_ST_POLL:
1956                 poll_state = PIO_ST_POLL;
1957                 reg_state = PIO_ST;
1958                 break;
1959         case PIO_ST_LAST:
1960         case PIO_ST_LAST_POLL:
1961                 poll_state = PIO_ST_LAST_POLL;
1962                 reg_state = PIO_ST_LAST;
1963                 break;
1964         default:
1965                 BUG();
1966                 break;
1967         }
1968
1969         status = ata_chk_status(ap);
1970         if (status & ATA_BUSY) {
1971                 if (time_after(jiffies, ap->pio_task_timeout)) {
1972                         ap->pio_task_state = tmout_state;
1973                         return 0;
1974                 }
1975                 ap->pio_task_state = poll_state;
1976                 return ATA_SHORT_PAUSE;
1977         }
1978
1979         ap->pio_task_state = reg_state;
1980         return 0;
1981 }
1982
1983 /**
1984  *      ata_pio_complete -
1985  *      @ap:
1986  *
1987  *      LOCKING:
1988  */
1989
1990 static void ata_pio_complete (struct ata_port *ap)
1991 {
1992         struct ata_queued_cmd *qc;
1993         u8 drv_stat;
1994
1995         /*
1996          * This is purely hueristic.  This is a fast path.
1997          * Sometimes when we enter, BSY will be cleared in
1998          * a chk-status or two.  If not, the drive is probably seeking
1999          * or something.  Snooze for a couple msecs, then
2000          * chk-status again.  If still busy, fall back to
2001          * PIO_ST_POLL state.
2002          */
2003         drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2004         if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2005                 msleep(2);
2006                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2007                 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2008                         ap->pio_task_state = PIO_ST_LAST_POLL;
2009                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2010                         return;
2011                 }
2012         }
2013
2014         drv_stat = ata_wait_idle(ap);
2015         if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2016                 ap->pio_task_state = PIO_ST_ERR;
2017                 return;
2018         }
2019
2020         qc = ata_qc_from_tag(ap, ap->active_tag);
2021         assert(qc != NULL);
2022
2023         ap->pio_task_state = PIO_ST_IDLE;
2024
2025         ata_irq_on(ap);
2026
2027         ata_qc_complete(qc, drv_stat);
2028 }
2029
2030 /**
2031  *      ata_pio_sector -
2032  *      @ap:
2033  *
2034  *      LOCKING:
2035  */
2036
2037 static void ata_pio_sector(struct ata_port *ap)
2038 {
2039         struct ata_queued_cmd *qc;
2040         struct scatterlist *sg;
2041         struct scsi_cmnd *cmd;
2042         unsigned char *buf;
2043         u8 status;
2044
2045         /*
2046          * This is purely hueristic.  This is a fast path.
2047          * Sometimes when we enter, BSY will be cleared in
2048          * a chk-status or two.  If not, the drive is probably seeking
2049          * or something.  Snooze for a couple msecs, then
2050          * chk-status again.  If still busy, fall back to
2051          * PIO_ST_POLL state.
2052          */
2053         status = ata_busy_wait(ap, ATA_BUSY, 5);
2054         if (status & ATA_BUSY) {
2055                 msleep(2);
2056                 status = ata_busy_wait(ap, ATA_BUSY, 10);
2057                 if (status & ATA_BUSY) {
2058                         ap->pio_task_state = PIO_ST_POLL;
2059                         ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2060                         return;
2061                 }
2062         }
2063
2064         /* handle BSY=0, DRQ=0 as error */
2065         if ((status & ATA_DRQ) == 0) {
2066                 ap->pio_task_state = PIO_ST_ERR;
2067                 return;
2068         }
2069
2070         qc = ata_qc_from_tag(ap, ap->active_tag);
2071         assert(qc != NULL);
2072
2073         cmd = qc->scsicmd;
2074         sg = qc->sg;
2075
2076         if (qc->cursect == (qc->nsect - 1))
2077                 ap->pio_task_state = PIO_ST_LAST;
2078
2079         buf = kmap(sg[qc->cursg].page) +
2080               sg[qc->cursg].offset + (qc->cursg_ofs * ATA_SECT_SIZE);
2081
2082         qc->cursect++;
2083         qc->cursg_ofs++;
2084
2085         if (cmd->use_sg)
2086                 if ((qc->cursg_ofs * ATA_SECT_SIZE) == sg_dma_len(&sg[qc->cursg])) {
2087                         qc->cursg++;
2088                         qc->cursg_ofs = 0;
2089                 }
2090
2091         DPRINTK("data %s, drv_stat 0x%X\n",
2092                 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read",
2093                 status);
2094
2095         /* do the actual data transfer */
2096         /* FIXME: mmio-ize */
2097         if (qc->tf.flags & ATA_TFLAG_WRITE)
2098                 outsl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2099         else
2100                 insl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2101
2102         kunmap(sg[qc->cursg].page);
2103 }
2104
2105 static void ata_pio_task(void *_data)
2106 {
2107         struct ata_port *ap = _data;
2108         unsigned long timeout = 0;
2109
2110         switch (ap->pio_task_state) {
2111         case PIO_ST:
2112                 ata_pio_sector(ap);
2113                 break;
2114
2115         case PIO_ST_LAST:
2116                 ata_pio_complete(ap);
2117                 break;
2118
2119         case PIO_ST_POLL:
2120         case PIO_ST_LAST_POLL:
2121                 timeout = ata_pio_poll(ap);
2122                 break;
2123
2124         case PIO_ST_TMOUT:
2125                 printk(KERN_ERR "ata%d: FIXME: PIO_ST_TMOUT\n", /* FIXME */
2126                        ap->id);
2127                 timeout = 11 * HZ;
2128                 break;
2129
2130         case PIO_ST_ERR:
2131                 printk(KERN_ERR "ata%d: FIXME: PIO_ST_ERR\n", /* FIXME */
2132                        ap->id);
2133                 timeout = 11 * HZ;
2134                 break;
2135         }
2136
2137         if ((ap->pio_task_state != PIO_ST_IDLE) &&
2138             (ap->pio_task_state != PIO_ST_TMOUT) &&
2139             (ap->pio_task_state != PIO_ST_ERR)) {
2140                 if (timeout)
2141                         queue_delayed_work(ata_wq, &ap->pio_task,
2142                                            timeout);
2143                 else
2144                         queue_work(ata_wq, &ap->pio_task);
2145         }
2146 }
2147
2148 /**
2149  *      ata_eng_timeout - Handle timeout of queued command
2150  *      @ap: Port on which timed-out command is active
2151  *
2152  *      Some part of the kernel (currently, only the SCSI layer)
2153  *      has noticed that the active command on port @ap has not
2154  *      completed after a specified length of time.  Handle this
2155  *      condition by disabling DMA (if necessary) and completing
2156  *      transactions, with error if necessary.
2157  *
2158  *      This also handles the case of the "lost interrupt", where
2159  *      for some reason (possibly hardware bug, possibly driver bug)
2160  *      an interrupt was not delivered to the driver, even though the
2161  *      transaction completed successfully.
2162  *
2163  *      LOCKING:
2164  *      Inherited from SCSI layer (none, can sleep)
2165  */
2166
2167 void ata_eng_timeout(struct ata_port *ap)
2168 {
2169         u8 host_stat, drv_stat;
2170         struct ata_queued_cmd *qc;
2171
2172         DPRINTK("ENTER\n");
2173
2174         qc = ata_qc_from_tag(ap, ap->active_tag);
2175         if (!qc) {
2176                 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2177                        ap->id);
2178                 goto out;
2179         }
2180
2181         /* hack alert!  We cannot use the supplied completion
2182          * function from inside the ->eh_strategy_handler() thread.
2183          * libata is the only user of ->eh_strategy_handler() in
2184          * any kernel, so the default scsi_done() assumes it is
2185          * not being called from the SCSI EH.
2186          */
2187         qc->scsidone = scsi_finish_command;
2188
2189         switch (qc->tf.protocol) {
2190         case ATA_PROT_DMA:
2191                 if (ap->flags & ATA_FLAG_MMIO) {
2192                         void *mmio = (void *) ap->ioaddr.bmdma_addr;
2193                         host_stat = readb(mmio + ATA_DMA_STATUS);
2194                 } else
2195                         host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2196
2197                 printk(KERN_ERR "ata%u: DMA timeout, stat 0x%x\n",
2198                        ap->id, host_stat);
2199
2200                 ata_dma_complete(qc, host_stat);
2201                 break;
2202
2203         case ATA_PROT_NODATA:
2204                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2205
2206                 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x\n",
2207                        ap->id, qc->tf.command, drv_stat);
2208
2209                 ata_qc_complete(qc, drv_stat);
2210                 break;
2211
2212         default:
2213                 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2214
2215                 printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
2216                        ap->id, qc->tf.command, drv_stat);
2217
2218                 ata_qc_complete(qc, drv_stat);
2219                 break;
2220         }
2221
2222 out:
2223         DPRINTK("EXIT\n");
2224 }
2225
2226 /**
2227  *      ata_qc_new - Request an available ATA command, for queueing
2228  *      @ap: Port associated with device @dev
2229  *      @dev: Device from whom we request an available command structure
2230  *
2231  *      LOCKING:
2232  */
2233
2234 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2235 {
2236         struct ata_queued_cmd *qc = NULL;
2237         unsigned int i;
2238
2239         for (i = 0; i < ATA_MAX_QUEUE; i++)
2240                 if (!test_and_set_bit(i, &ap->qactive)) {
2241                         qc = ata_qc_from_tag(ap, i);
2242                         break;
2243                 }
2244
2245         if (qc)
2246                 qc->tag = i;
2247
2248         return qc;
2249 }
2250
2251 /**
2252  *      ata_qc_new_init - Request an available ATA command, and initialize it
2253  *      @ap: Port associated with device @dev
2254  *      @dev: Device from whom we request an available command structure
2255  *
2256  *      LOCKING:
2257  */
2258
2259 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2260                                       struct ata_device *dev)
2261 {
2262         struct ata_queued_cmd *qc;
2263
2264         qc = ata_qc_new(ap);
2265         if (qc) {
2266                 qc->sg = NULL;
2267                 qc->flags = 0;
2268                 qc->scsicmd = NULL;
2269                 qc->ap = ap;
2270                 qc->dev = dev;
2271                 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2272                 qc->nsect = 0;
2273
2274                 ata_tf_init(ap, &qc->tf, dev->devno);
2275
2276                 if (likely((dev->flags & ATA_DFLAG_PIO) == 0))
2277                         qc->flags |= ATA_QCFLAG_DMA;
2278                 if (dev->flags & ATA_DFLAG_LBA48)
2279                         qc->tf.flags |= ATA_TFLAG_LBA48;
2280         }
2281
2282         return qc;
2283 }
2284
2285 /**
2286  *      ata_qc_complete - Complete an active ATA command
2287  *      @qc: Command to complete
2288  *      @drv_stat: ATA status register contents
2289  *
2290  *      LOCKING:
2291  *
2292  */
2293
2294 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2295 {
2296         struct ata_port *ap = qc->ap;
2297         struct scsi_cmnd *cmd = qc->scsicmd;
2298         unsigned int tag, do_clear = 0;
2299
2300         assert(qc != NULL);     /* ata_qc_from_tag _might_ return NULL */
2301         assert(qc->flags & ATA_QCFLAG_ACTIVE);
2302
2303         if (likely(qc->flags & ATA_QCFLAG_SG))
2304                 ata_sg_clean(qc);
2305
2306         if (cmd) {
2307                 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
2308                         if (is_atapi_taskfile(&qc->tf))
2309                                 cmd->result = SAM_STAT_CHECK_CONDITION;
2310                         else
2311                                 ata_to_sense_error(qc);
2312                 } else {
2313                         cmd->result = SAM_STAT_GOOD;
2314                 }
2315
2316                 qc->scsidone(cmd);
2317         }
2318
2319         qc->flags &= ~ATA_QCFLAG_ACTIVE;
2320         tag = qc->tag;
2321         if (likely(ata_tag_valid(tag))) {
2322                 if (tag == ap->active_tag)
2323                         ap->active_tag = ATA_TAG_POISON;
2324                 qc->tag = ATA_TAG_POISON;
2325                 do_clear = 1;
2326         }
2327
2328         if (qc->waiting)
2329                 complete(qc->waiting);
2330
2331         if (likely(do_clear))
2332                 clear_bit(tag, &ap->qactive);
2333 }
2334
2335 /**
2336  *      ata_qc_issue - issue taskfile to device
2337  *      @qc: command to issue to device
2338  *
2339  *      Prepare an ATA command to submission to device.
2340  *      This includes mapping the data into a DMA-able
2341  *      area, filling in the S/G table, and finally
2342  *      writing the taskfile to hardware, starting the command.
2343  *
2344  *      LOCKING:
2345  *      spin_lock_irqsave(host_set lock)
2346  *
2347  *      RETURNS:
2348  *      Zero on success, negative on error.
2349  */
2350
2351 int ata_qc_issue(struct ata_queued_cmd *qc)
2352 {
2353         struct ata_port *ap = qc->ap;
2354         struct scsi_cmnd *cmd = qc->scsicmd;
2355
2356         if (qc->flags & ATA_QCFLAG_SG) {
2357                 /* set up SG table */
2358                 if (cmd->use_sg) {
2359                         if (ata_sg_setup(qc))
2360                                 goto err_out;
2361                 } else {
2362                         if (ata_sg_setup_one(qc))
2363                                 goto err_out;
2364                 }
2365
2366                 ap->ops->fill_sg(qc);
2367         }
2368
2369         qc->ap->active_tag = qc->tag;
2370         qc->flags |= ATA_QCFLAG_ACTIVE;
2371
2372         return ata_qc_issue_prot(qc);
2373
2374 err_out:
2375         return -1;
2376 }
2377
2378 /**
2379  *      ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
2380  *      @qc: command to issue to device
2381  *
2382  *      Using various libata functions and hooks, this function
2383  *      starts an ATA command.  ATA commands are grouped into
2384  *      classes called "protocols", and issuing each type of protocol
2385  *      is slightly different.
2386  *
2387  *      LOCKING:
2388  *      spin_lock_irqsave(host_set lock)
2389  *
2390  *      RETURNS:
2391  *      Zero on success, negative on error.
2392  */
2393
2394 static int ata_qc_issue_prot(struct ata_queued_cmd *qc)
2395 {
2396         struct ata_port *ap = qc->ap;
2397
2398         ata_dev_select(ap, qc->dev->devno, 1, 0);
2399
2400         switch (qc->tf.protocol) {
2401         case ATA_PROT_NODATA:
2402                 ata_tf_to_host_nolock(ap, &qc->tf);
2403                 break;
2404
2405         case ATA_PROT_DMA:
2406                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
2407                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
2408                 ap->ops->bmdma_start(qc);           /* initiate bmdma */
2409                 break;
2410
2411         case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
2412                 ata_qc_set_polling(qc);
2413                 ata_tf_to_host_nolock(ap, &qc->tf);
2414                 ap->pio_task_state = PIO_ST;
2415                 queue_work(ata_wq, &ap->pio_task);
2416                 break;
2417
2418         case ATA_PROT_ATAPI:
2419                 ata_tf_to_host_nolock(ap, &qc->tf);
2420                 queue_work(ata_wq, &ap->packet_task);
2421                 break;
2422
2423         case ATA_PROT_ATAPI_DMA:
2424                 ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
2425                 ap->ops->bmdma_setup(qc);           /* set up bmdma */
2426                 queue_work(ata_wq, &ap->packet_task);
2427                 break;
2428
2429         default:
2430                 WARN_ON(1);
2431                 return -1;
2432         }
2433
2434         return 0;
2435 }
2436
2437 /**
2438  *      ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction (MMIO)
2439  *      @qc: Info associated with this ATA transaction.
2440  *
2441  *      LOCKING:
2442  *      spin_lock_irqsave(host_set lock)
2443  */
2444
2445 void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
2446 {
2447         struct ata_port *ap = qc->ap;
2448         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2449         u8 host_stat, dmactl;
2450         void *mmio = (void *) ap->ioaddr.bmdma_addr;
2451
2452         /* load PRD table addr. */
2453         mb();   /* make sure PRD table writes are visible to controller */
2454         writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
2455
2456         /* specify data direction, triple-check start bit is clear */
2457         dmactl = readb(mmio + ATA_DMA_CMD);
2458         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2459         if (!rw)
2460                 dmactl |= ATA_DMA_WR;
2461         writeb(dmactl, mmio + ATA_DMA_CMD);
2462
2463         /* clear interrupt, error bits */
2464         host_stat = readb(mmio + ATA_DMA_STATUS);
2465         writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR, mmio + ATA_DMA_STATUS);
2466
2467         /* issue r/w command */
2468         ap->ops->exec_command(ap, &qc->tf);
2469 }
2470
2471 /**
2472  *      ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction (MMIO)
2473  *      @qc: Info associated with this ATA transaction.
2474  *
2475  *      LOCKING:
2476  *      spin_lock_irqsave(host_set lock)
2477  */
2478
2479 void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
2480 {
2481         struct ata_port *ap = qc->ap;
2482         void *mmio = (void *) ap->ioaddr.bmdma_addr;
2483         u8 dmactl;
2484
2485         /* start host DMA transaction */
2486         dmactl = readb(mmio + ATA_DMA_CMD);
2487         writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
2488
2489         /* Strictly, one may wish to issue a readb() here, to
2490          * flush the mmio write.  However, control also passes
2491          * to the hardware at this point, and it will interrupt
2492          * us when we are to resume control.  So, in effect,
2493          * we don't care when the mmio write flushes.
2494          * Further, a read of the DMA status register _immediately_
2495          * following the write may not be what certain flaky hardware
2496          * is expected, so I think it is best to not add a readb()
2497          * without first all the MMIO ATA cards/mobos.
2498          * Or maybe I'm just being paranoid.
2499          */
2500 }
2501
2502 /**
2503  *      ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
2504  *      @qc: Info associated with this ATA transaction.
2505  *
2506  *      LOCKING:
2507  *      spin_lock_irqsave(host_set lock)
2508  */
2509
2510 void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
2511 {
2512         struct ata_port *ap = qc->ap;
2513         unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2514         u8 host_stat, dmactl;
2515
2516         /* load PRD table addr. */
2517         outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2518
2519         /* specify data direction, triple-check start bit is clear */
2520         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2521         dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2522         if (!rw)
2523                 dmactl |= ATA_DMA_WR;
2524         outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2525
2526         /* clear interrupt, error bits */
2527         host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2528         outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2529              ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2530
2531         /* issue r/w command */
2532         ap->ops->exec_command(ap, &qc->tf);
2533 }
2534
2535 /**
2536  *      ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
2537  *      @qc: Info associated with this ATA transaction.
2538  *
2539  *      LOCKING:
2540  *      spin_lock_irqsave(host_set lock)
2541  */
2542
2543 void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
2544 {
2545         struct ata_port *ap = qc->ap;
2546         u8 dmactl;
2547
2548         /* start host DMA transaction */
2549         dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2550         outb(dmactl | ATA_DMA_START,
2551              ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2552 }
2553
2554 /**
2555  *      ata_dma_complete - Complete an active ATA BMDMA command
2556  *      @qc: Command to complete
2557  *      @host_stat: BMDMA status register contents
2558  *
2559  *      LOCKING:
2560  */
2561
2562 static void ata_dma_complete(struct ata_queued_cmd *qc, u8 host_stat)
2563 {
2564         struct ata_port *ap = qc->ap;
2565         VPRINTK("ENTER\n");
2566
2567         if (ap->flags & ATA_FLAG_MMIO) {
2568                 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2569
2570                 /* clear start/stop bit */
2571                 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2572                        mmio + ATA_DMA_CMD);
2573
2574                 /* ack intr, err bits */
2575                 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2576                        mmio + ATA_DMA_STATUS);
2577         } else {
2578                 /* clear start/stop bit */
2579                 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
2580                      ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2581
2582                 /* ack intr, err bits */
2583                 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2584                      ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2585         }
2586
2587
2588         /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2589         ata_altstatus(ap);              /* dummy read */
2590
2591         DPRINTK("host %u, host_stat==0x%X, drv_stat==0x%X\n",
2592                 ap->id, (u32) host_stat, (u32) ata_chk_status(ap));
2593
2594         /* get drive status; clear intr; complete txn */
2595         ata_qc_complete(qc, ata_wait_idle(ap));
2596 }
2597
2598 /**
2599  *      ata_host_intr - Handle host interrupt for given (port, task)
2600  *      @ap: Port on which interrupt arrived (possibly...)
2601  *      @qc: Taskfile currently active in engine
2602  *
2603  *      Handle host interrupt for given queued command.  Currently,
2604  *      only DMA interrupts are handled.  All other commands are
2605  *      handled via polling with interrupts disabled (nIEN bit).
2606  *
2607  *      LOCKING:
2608  *      spin_lock_irqsave(host_set lock)
2609  *
2610  *      RETURNS:
2611  *      One if interrupt was handled, zero if not (shared irq).
2612  */
2613
2614 inline unsigned int ata_host_intr (struct ata_port *ap,
2615                                    struct ata_queued_cmd *qc)
2616 {
2617         u8 status, host_stat;
2618         unsigned int handled = 0;
2619
2620         switch (qc->tf.protocol) {
2621
2622         /* BMDMA completion */
2623         case ATA_PROT_DMA:
2624         case ATA_PROT_ATAPI_DMA:
2625                 if (ap->flags & ATA_FLAG_MMIO) {
2626                         void *mmio = (void *) ap->ioaddr.bmdma_addr;
2627                         host_stat = readb(mmio + ATA_DMA_STATUS);
2628                 } else
2629                         host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2630                 VPRINTK("BUS_DMA (host_stat 0x%X)\n", host_stat);
2631
2632                 if (!(host_stat & ATA_DMA_INTR)) {
2633                         ap->stats.idle_irq++;
2634                         break;
2635                 }
2636
2637                 ata_dma_complete(qc, host_stat);
2638                 handled = 1;
2639                 break;
2640
2641         /* command completion, but no data xfer */
2642         /* FIXME: a shared interrupt _will_ cause a non-data command
2643          * to be completed prematurely, with an error.
2644          *
2645          * This doesn't matter right now, since we aren't sending
2646          * non-data commands down this pipe except in development
2647          * situations.
2648          */
2649         case ATA_PROT_ATAPI:
2650         case ATA_PROT_NODATA:
2651                 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2652                 DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
2653                 ata_qc_complete(qc, status);
2654                 handled = 1;
2655                 break;
2656
2657         default:
2658                 ap->stats.idle_irq++;
2659
2660 #ifdef ATA_IRQ_TRAP
2661                 if ((ap->stats.idle_irq % 1000) == 0) {
2662                         handled = 1;
2663                         ata_irq_ack(ap, 0); /* debug trap */
2664                         printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
2665                 }
2666 #endif
2667                 break;
2668         }
2669
2670         return handled;
2671 }
2672
2673 /**
2674  *      ata_interrupt - Default ATA host interrupt handler
2675  *      @irq: irq line
2676  *      @dev_instance: pointer to our host information structure
2677  *      @regs: unused
2678  *
2679  *      LOCKING:
2680  *
2681  *      RETURNS:
2682  *
2683  */
2684
2685 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
2686 {
2687         struct ata_host_set *host_set = dev_instance;
2688         unsigned int i;
2689         unsigned int handled = 0;
2690         unsigned long flags;
2691
2692         /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
2693         spin_lock_irqsave(&host_set->lock, flags);
2694
2695         for (i = 0; i < host_set->n_ports; i++) {
2696                 struct ata_port *ap;
2697
2698                 ap = host_set->ports[i];
2699                 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
2700                         struct ata_queued_cmd *qc;
2701
2702                         qc = ata_qc_from_tag(ap, ap->active_tag);
2703                         if (qc && (!(qc->tf.ctl & ATA_NIEN)))
2704                                 handled += ata_host_intr(ap, qc);
2705                 }
2706         }
2707
2708         spin_unlock_irqrestore(&host_set->lock, flags);
2709
2710         return IRQ_RETVAL(handled);
2711 }
2712
2713 /**
2714  *      ata_thread_iter -
2715  *      @ap:
2716  *
2717  *      LOCKING:
2718  *
2719  *      RETURNS:
2720  *
2721  */
2722
2723 static unsigned long ata_thread_iter(struct ata_port *ap)
2724 {
2725         long timeout = 0;
2726
2727         DPRINTK("ata%u: thr_state %s\n",
2728                 ap->id, ata_thr_state_name(ap->thr_state));
2729
2730         switch (ap->thr_state) {
2731         case THR_UNKNOWN:
2732                 ap->thr_state = THR_PORT_RESET;
2733                 break;
2734
2735         case THR_PROBE_START:
2736                 ap->thr_state = THR_PORT_RESET;
2737                 break;
2738
2739         case THR_PORT_RESET:
2740                 ata_port_reset(ap);
2741                 break;
2742
2743         case THR_PROBE_SUCCESS:
2744                 up(&ap->probe_sem);
2745                 ap->thr_state = THR_IDLE;
2746                 break;
2747
2748         case THR_PROBE_FAILED:
2749                 up(&ap->probe_sem);
2750                 ap->thr_state = THR_AWAIT_DEATH;
2751                 break;
2752
2753         case THR_AWAIT_DEATH:
2754         case THR_IDLE:
2755                 timeout = -1;
2756                 break;
2757
2758         default:
2759                 printk(KERN_DEBUG "ata%u: unknown thr state %s\n",
2760                        ap->id, ata_thr_state_name(ap->thr_state));
2761                 break;
2762         }
2763
2764         DPRINTK("ata%u: new thr_state %s, returning %ld\n",
2765                 ap->id, ata_thr_state_name(ap->thr_state), timeout);
2766         return timeout;
2767 }
2768
2769 /**
2770  *      atapi_packet_task - Write CDB bytes to hardware
2771  *      @_data: Port to which ATAPI device is attached.
2772  *
2773  *      When device has indicated its readiness to accept
2774  *      a CDB, this function is called.  Send the CDB.
2775  *      If DMA is to be performed, exit immediately.
2776  *      Otherwise, we are in polling mode, so poll
2777  *      status under operation succeeds or fails.
2778  *
2779  *      LOCKING:
2780  *      Kernel thread context (may sleep)
2781  */
2782
2783 static void atapi_packet_task(void *_data)
2784 {
2785         struct ata_port *ap = _data;
2786         struct ata_queued_cmd *qc;
2787         u8 status;
2788
2789         qc = ata_qc_from_tag(ap, ap->active_tag);
2790         assert(qc != NULL);
2791         assert(qc->flags & ATA_QCFLAG_ACTIVE);
2792
2793         /* sleep-wait for BSY to clear */
2794         DPRINTK("busy wait\n");
2795         if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
2796                 goto err_out;
2797
2798         /* make sure DRQ is set */
2799         status = ata_chk_status(ap);
2800         if ((status & ATA_DRQ) == 0)
2801                 goto err_out;
2802
2803         /* send SCSI cdb */
2804         /* FIXME: mmio-ize */
2805         DPRINTK("send cdb\n");
2806         outsl(ap->ioaddr.data_addr,
2807               qc->scsicmd->cmnd, ap->host->max_cmd_len / 4);
2808
2809         /* if we are DMA'ing, irq handler takes over from here */
2810         if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
2811                 ap->ops->bmdma_start(qc);           /* initiate bmdma */
2812
2813         /* non-data commands are also handled via irq */
2814         else if (qc->scsicmd->sc_data_direction == SCSI_DATA_NONE) {
2815                 /* do nothing */
2816         }
2817
2818         /* PIO commands are handled by polling */
2819         else {
2820                 ap->pio_task_state = PIO_ST;
2821                 queue_work(ata_wq, &ap->pio_task);
2822         }
2823
2824         return;
2825
2826 err_out:
2827         ata_qc_complete(qc, ATA_ERR);
2828 }
2829
2830 int ata_port_start (struct ata_port *ap)
2831 {
2832         struct pci_dev *pdev = ap->host_set->pdev;
2833
2834         ap->prd = pci_alloc_consistent(pdev, ATA_PRD_TBL_SZ, &ap->prd_dma);
2835         if (!ap->prd)
2836                 return -ENOMEM;
2837
2838         DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
2839
2840         return 0;
2841 }
2842
2843 void ata_port_stop (struct ata_port *ap)
2844 {
2845         struct pci_dev *pdev = ap->host_set->pdev;
2846
2847         pci_free_consistent(pdev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
2848 }
2849
2850 static void ata_probe_task(void *_data)
2851 {
2852         struct ata_port *ap = _data;
2853         long timeout;
2854
2855         timeout = ata_thread_iter(ap);
2856         if (timeout < 0)
2857                 return;
2858
2859         if (timeout > 0)
2860                 queue_delayed_work(ata_wq, &ap->probe_task, timeout);
2861         else
2862                 queue_work(ata_wq, &ap->probe_task);
2863 }
2864
2865 /**
2866  *      ata_host_remove - Unregister SCSI host structure with upper layers
2867  *      @ap: Port to unregister
2868  *      @do_unregister: 1 if we fully unregister, 0 to just stop the port
2869  *
2870  *      LOCKING:
2871  */
2872
2873 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
2874 {
2875         struct Scsi_Host *sh = ap->host;
2876
2877         DPRINTK("ENTER\n");
2878
2879         if (do_unregister)
2880                 scsi_remove_host(sh);
2881
2882         ap->ops->port_stop(ap);
2883 }
2884
2885 /**
2886  *      ata_host_init - Initialize an ata_port structure
2887  *      @ap: Structure to initialize
2888  *      @host: associated SCSI mid-layer structure
2889  *      @host_set: Collection of hosts to which @ap belongs
2890  *      @ent: Probe information provided by low-level driver
2891  *      @port_no: Port number associated with this ata_port
2892  *
2893  *      LOCKING:
2894  *
2895  */
2896
2897 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
2898                           struct ata_host_set *host_set,
2899                           struct ata_probe_ent *ent, unsigned int port_no)
2900 {
2901         unsigned int i;
2902
2903         host->max_id = 16;
2904         host->max_lun = 1;
2905         host->max_channel = 1;
2906         host->unique_id = ata_unique_id++;
2907         host->max_cmd_len = 12;
2908         scsi_set_device(host, &ent->pdev->dev);
2909         scsi_assign_lock(host, &host_set->lock);
2910
2911         ap->flags = ATA_FLAG_PORT_DISABLED;
2912         ap->id = host->unique_id;
2913         ap->host = host;
2914         ap->ctl = ATA_DEVCTL_OBS;
2915         ap->host_set = host_set;
2916         ap->port_no = port_no;
2917         ap->pio_mask = ent->pio_mask;
2918         ap->udma_mask = ent->udma_mask;
2919         ap->flags |= ent->host_flags;
2920         ap->ops = ent->port_ops;
2921         ap->thr_state = THR_PROBE_START;
2922         ap->cbl = ATA_CBL_NONE;
2923         ap->device[0].flags = ATA_DFLAG_MASTER;
2924         ap->active_tag = ATA_TAG_POISON;
2925         ap->last_ctl = 0xFF;
2926
2927         INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
2928         INIT_WORK(&ap->pio_task, ata_pio_task, ap);
2929         INIT_WORK(&ap->probe_task, ata_probe_task, ap);
2930
2931         for (i = 0; i < ATA_MAX_DEVICES; i++)
2932                 ap->device[i].devno = i;
2933
2934         init_MUTEX_LOCKED(&ap->probe_sem);
2935
2936 #ifdef ATA_IRQ_TRAP
2937         ap->stats.unhandled_irq = 1;
2938         ap->stats.idle_irq = 1;
2939 #endif
2940
2941         memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
2942 }
2943
2944 /**
2945  *      ata_host_add - Attach low-level ATA driver to system
2946  *      @ent: Information provided by low-level driver
2947  *      @host_set: Collections of ports to which we add
2948  *      @port_no: Port number associated with this host
2949  *
2950  *      LOCKING:
2951  *
2952  *      RETURNS:
2953  *
2954  */
2955
2956 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
2957                                       struct ata_host_set *host_set,
2958                                       unsigned int port_no)
2959 {
2960         struct Scsi_Host *host;
2961         struct ata_port *ap;
2962         int rc;
2963
2964         DPRINTK("ENTER\n");
2965         host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
2966         if (!host)
2967                 return NULL;
2968
2969         ap = (struct ata_port *) &host->hostdata[0];
2970
2971         ata_host_init(ap, host, host_set, ent, port_no);
2972
2973         rc = ap->ops->port_start(ap);
2974         if (rc)
2975                 goto err_out;
2976
2977         return ap;
2978
2979 err_out:
2980         scsi_host_put(host);
2981         return NULL;
2982 }
2983
2984 /**
2985  *      ata_device_add -
2986  *      @ent:
2987  *
2988  *      LOCKING:
2989  *
2990  *      RETURNS:
2991  *
2992  */
2993
2994 int ata_device_add(struct ata_probe_ent *ent)
2995 {
2996         unsigned int count = 0, i;
2997         struct pci_dev *pdev = ent->pdev;
2998         struct ata_host_set *host_set;
2999
3000         DPRINTK("ENTER\n");
3001         /* alloc a container for our list of ATA ports (buses) */
3002         host_set = kmalloc(sizeof(struct ata_host_set) +
3003                            (ent->n_ports * sizeof(void *)), GFP_KERNEL);
3004         if (!host_set)
3005                 return 0;
3006         memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
3007         spin_lock_init(&host_set->lock);
3008
3009         host_set->pdev = pdev;
3010         host_set->n_ports = ent->n_ports;
3011         host_set->irq = ent->irq;
3012         host_set->mmio_base = ent->mmio_base;
3013         host_set->private_data = ent->private_data;
3014
3015         /* register each port bound to this device */
3016         for (i = 0; i < ent->n_ports; i++) {
3017                 struct ata_port *ap;
3018
3019                 ap = ata_host_add(ent, host_set, i);
3020                 if (!ap)
3021                         goto err_out;
3022
3023                 host_set->ports[i] = ap;
3024
3025                 /* print per-port info to dmesg */
3026                 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3027                                  "bmdma 0x%lX irq %lu\n",
3028                         ap->id,
3029                         ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
3030                         ata_udma_string(ent->udma_mask),
3031                         ap->ioaddr.cmd_addr,
3032                         ap->ioaddr.ctl_addr,
3033                         ap->ioaddr.bmdma_addr,
3034                         ent->irq);
3035
3036                 count++;
3037         }
3038
3039         if (!count) {
3040                 kfree(host_set);
3041                 return 0;
3042         }
3043
3044         /* obtain irq, that is shared between channels */
3045         if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
3046                         DRV_NAME, host_set))
3047                 goto err_out;
3048
3049         /* perform each probe synchronously */
3050         DPRINTK("probe begin\n");
3051         for (i = 0; i < count; i++) {
3052                 struct ata_port *ap;
3053                 int rc;
3054
3055                 ap = host_set->ports[i];
3056
3057                 DPRINTK("ata%u: probe begin\n", ap->id);
3058                 queue_work(ata_wq, &ap->probe_task);    /* start probe */
3059
3060                 DPRINTK("ata%u: probe-wait begin\n", ap->id);
3061                 down(&ap->probe_sem);   /* wait for end */
3062
3063                 DPRINTK("ata%u: probe-wait end\n", ap->id);
3064
3065                 rc = scsi_add_host(ap->host, &pdev->dev);
3066                 if (rc) {
3067                         printk(KERN_ERR "ata%u: scsi_add_host failed\n",
3068                                ap->id);
3069                         /* FIXME: do something useful here */
3070                         /* FIXME: handle unconditional calls to
3071                          * scsi_scan_host and ata_host_remove, below,
3072                          * at the very least
3073                          */
3074                 }
3075         }
3076
3077         /* probes are done, now scan each port's disk(s) */
3078         DPRINTK("probe begin\n");
3079         for (i = 0; i < count; i++) {
3080                 struct ata_port *ap = host_set->ports[i];
3081
3082                 scsi_scan_host(ap->host);
3083         }
3084
3085         pci_set_drvdata(pdev, host_set);
3086
3087         VPRINTK("EXIT, returning %u\n", ent->n_ports);
3088         return ent->n_ports; /* success */
3089
3090 err_out:
3091         for (i = 0; i < count; i++) {
3092                 ata_host_remove(host_set->ports[i], 1);
3093                 scsi_host_put(host_set->ports[i]->host);
3094         }
3095         kfree(host_set);
3096         VPRINTK("EXIT, returning 0\n");
3097         return 0;
3098 }
3099
3100 /**
3101  *      ata_scsi_release - SCSI layer callback hook for host unload
3102  *      @host: libata host to be unloaded
3103  *
3104  *      Performs all duties necessary to shut down a libata port...
3105  *      Kill port kthread, disable port, and release resources.
3106  *
3107  *      LOCKING:
3108  *      Inherited from SCSI layer.
3109  *
3110  *      RETURNS:
3111  *      One.
3112  */
3113
3114 int ata_scsi_release(struct Scsi_Host *host)
3115 {
3116         struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3117
3118         DPRINTK("ENTER\n");
3119
3120         ap->ops->port_disable(ap);
3121         ata_host_remove(ap, 0);
3122
3123         DPRINTK("EXIT\n");
3124         return 1;
3125 }
3126
3127 /**
3128  *      ata_std_ports - initialize ioaddr with standard port offsets.
3129  *      @ioaddr: IO address structure to be initialized
3130  */
3131 void ata_std_ports(struct ata_ioports *ioaddr)
3132 {
3133         ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3134         ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3135         ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3136         ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3137         ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3138         ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3139         ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3140         ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3141         ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3142         ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3143 }
3144
3145 /**
3146  *      ata_pci_init_one - Initialize/register PCI IDE host controller
3147  *      @pdev: Controller to be initialized
3148  *      @port_info: Information from low-level host driver
3149  *      @n_ports: Number of ports attached to host controller
3150  *
3151  *      LOCKING:
3152  *      Inherited from PCI layer (may sleep).
3153  *
3154  *      RETURNS:
3155  *
3156  */
3157
3158 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3159                       unsigned int n_ports)
3160 {
3161         struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3162         struct ata_port_info *port0, *port1;
3163         u8 tmp8, mask;
3164         unsigned int legacy_mode = 0;
3165         int rc;
3166
3167         DPRINTK("ENTER\n");
3168
3169         port0 = port_info[0];
3170         if (n_ports > 1)
3171                 port1 = port_info[1];
3172         else
3173                 port1 = port0;
3174
3175         if ((port0->host_flags & ATA_FLAG_NO_LEGACY) == 0) {
3176                 /* TODO: support transitioning to native mode? */
3177                 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3178                 mask = (1 << 2) | (1 << 0);
3179                 if ((tmp8 & mask) != mask)
3180                         legacy_mode = (1 << 3);
3181         }
3182
3183         /* FIXME... */
3184         if ((!legacy_mode) && (n_ports > 1)) {
3185                 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3186                 return -EINVAL;
3187         }
3188
3189         rc = pci_enable_device(pdev);
3190         if (rc)
3191                 return rc;
3192
3193         rc = pci_request_regions(pdev, DRV_NAME);
3194         if (rc)
3195                 goto err_out;
3196
3197         if (legacy_mode) {
3198                 if (!request_region(0x1f0, 8, "libata")) {
3199                         struct resource *conflict, res;
3200                         res.start = 0x1f0;
3201                         res.end = 0x1f0 + 8 - 1;
3202                         conflict = ____request_resource(&ioport_resource, &res);
3203                         if (!strcmp(conflict->name, "libata"))
3204                                 legacy_mode |= (1 << 0);
3205                         else
3206                                 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3207                 } else
3208                         legacy_mode |= (1 << 0);
3209
3210                 if (!request_region(0x170, 8, "libata")) {
3211                         struct resource *conflict, res;
3212                         res.start = 0x170;
3213                         res.end = 0x170 + 8 - 1;
3214                         conflict = ____request_resource(&ioport_resource, &res);
3215                         if (!strcmp(conflict->name, "libata"))
3216                                 legacy_mode |= (1 << 1);
3217                         else
3218                                 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3219                 } else
3220                         legacy_mode |= (1 << 1);
3221         }
3222
3223         /* we have legacy mode, but all ports are unavailable */
3224         if (legacy_mode == (1 << 3)) {
3225                 rc = -EBUSY;
3226                 goto err_out_regions;
3227         }
3228
3229         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3230         if (rc)
3231                 goto err_out_regions;
3232         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3233         if (rc)
3234                 goto err_out_regions;
3235
3236         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3237         if (!probe_ent) {
3238                 rc = -ENOMEM;
3239                 goto err_out_regions;
3240         }
3241
3242         memset(probe_ent, 0, sizeof(*probe_ent));
3243         probe_ent->pdev = pdev;
3244         INIT_LIST_HEAD(&probe_ent->node);
3245
3246         if (legacy_mode) {
3247                 probe_ent2 = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3248                 if (!probe_ent2) {
3249                         rc = -ENOMEM;
3250                         goto err_out_free_ent;
3251                 }
3252
3253                 memset(probe_ent2, 0, sizeof(*probe_ent));
3254                 probe_ent2->pdev = pdev;
3255                 INIT_LIST_HEAD(&probe_ent2->node);
3256         }
3257
3258         probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3259         probe_ent->sht = port0->sht;
3260         probe_ent->host_flags = port0->host_flags;
3261         probe_ent->pio_mask = port0->pio_mask;
3262         probe_ent->udma_mask = port0->udma_mask;
3263         probe_ent->port_ops = port0->port_ops;
3264
3265         if (legacy_mode) {
3266                 probe_ent->port[0].cmd_addr = 0x1f0;
3267                 probe_ent->port[0].altstatus_addr =
3268                 probe_ent->port[0].ctl_addr = 0x3f6;
3269                 probe_ent->n_ports = 1;
3270                 probe_ent->irq = 14;
3271                 ata_std_ports(&probe_ent->port[0]);
3272
3273                 probe_ent2->port[0].cmd_addr = 0x170;
3274                 probe_ent2->port[0].altstatus_addr =
3275                 probe_ent2->port[0].ctl_addr = 0x376;
3276                 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3277                 probe_ent2->n_ports = 1;
3278                 probe_ent2->irq = 15;
3279                 ata_std_ports(&probe_ent2->port[0]);
3280
3281                 probe_ent2->sht = port1->sht;
3282                 probe_ent2->host_flags = port1->host_flags;
3283                 probe_ent2->pio_mask = port1->pio_mask;
3284                 probe_ent2->udma_mask = port1->udma_mask;
3285                 probe_ent2->port_ops = port1->port_ops;
3286         } else {
3287                 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3288                 ata_std_ports(&probe_ent->port[0]);
3289                 probe_ent->port[0].altstatus_addr =
3290                 probe_ent->port[0].ctl_addr =
3291                         pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3292
3293                 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3294                 ata_std_ports(&probe_ent->port[1]);
3295                 probe_ent->port[1].altstatus_addr =
3296                 probe_ent->port[1].ctl_addr =
3297                         pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3298                 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3299
3300                 probe_ent->n_ports = 2;
3301                 probe_ent->irq = pdev->irq;
3302                 probe_ent->irq_flags = SA_SHIRQ;
3303         }
3304
3305         pci_set_master(pdev);
3306
3307         /* FIXME: check ata_device_add return */
3308         if (legacy_mode) {
3309                 if (legacy_mode & (1 << 0))
3310                         ata_device_add(probe_ent);
3311                 if (legacy_mode & (1 << 1))
3312                         ata_device_add(probe_ent2);
3313                 kfree(probe_ent2);
3314         } else {
3315                 ata_device_add(probe_ent);
3316                 assert(probe_ent2 == NULL);
3317         }
3318         kfree(probe_ent);
3319
3320         return 0;
3321
3322 err_out_free_ent:
3323         kfree(probe_ent);
3324 err_out_regions:
3325         if (legacy_mode & (1 << 0))
3326                 release_region(0x1f0, 8);
3327         if (legacy_mode & (1 << 1))
3328                 release_region(0x170, 8);
3329         pci_release_regions(pdev);
3330 err_out:
3331         pci_disable_device(pdev);
3332         return rc;
3333 }
3334
3335 /**
3336  *      ata_pci_remove_one - PCI layer callback for device removal
3337  *      @pdev: PCI device that was removed
3338  *
3339  *      PCI layer indicates to libata via this hook that
3340  *      hot-unplug or module unload event has occured.
3341  *      Handle this by unregistering all objects associated
3342  *      with this PCI device.  Free those objects.  Then finally
3343  *      release PCI resources and disable device.
3344  *
3345  *      LOCKING:
3346  *      Inherited from PCI layer (may sleep).
3347  */
3348
3349 void ata_pci_remove_one (struct pci_dev *pdev)
3350 {
3351         struct ata_host_set *host_set = pci_get_drvdata(pdev);
3352         struct ata_port *ap;
3353         unsigned int i;
3354
3355         for (i = 0; i < host_set->n_ports; i++) {
3356                 ap = host_set->ports[i];
3357
3358                 scsi_remove_host(ap->host);
3359         }
3360
3361         free_irq(host_set->irq, host_set);
3362         if (host_set->mmio_base)
3363                 iounmap(host_set->mmio_base);
3364         if (host_set->ports[0]->ops->host_stop)
3365                 host_set->ports[0]->ops->host_stop(host_set);
3366
3367         for (i = 0; i < host_set->n_ports; i++) {
3368                 ap = host_set->ports[i];
3369
3370                 ata_scsi_release(ap->host);
3371                 scsi_host_put(ap->host);
3372         }
3373
3374         pci_release_regions(pdev);
3375
3376         for (i = 0; i < host_set->n_ports; i++) {
3377                 struct ata_ioports *ioaddr;
3378
3379                 ap = host_set->ports[i];
3380                 ioaddr = &ap->ioaddr;
3381
3382                 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
3383                         if (ioaddr->cmd_addr == 0x1f0)
3384                                 release_region(0x1f0, 8);
3385                         else if (ioaddr->cmd_addr == 0x170)
3386                                 release_region(0x170, 8);
3387                 }
3388         }
3389
3390         kfree(host_set);
3391         pci_disable_device(pdev);
3392         pci_set_drvdata(pdev, NULL);
3393 }
3394
3395 /* move to PCI subsystem */
3396 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
3397 {
3398         unsigned long tmp = 0;
3399
3400         switch (bits->width) {
3401         case 1: {
3402                 u8 tmp8 = 0;
3403                 pci_read_config_byte(pdev, bits->reg, &tmp8);
3404                 tmp = tmp8;
3405                 break;
3406         }
3407         case 2: {
3408                 u16 tmp16 = 0;
3409                 pci_read_config_word(pdev, bits->reg, &tmp16);
3410                 tmp = tmp16;
3411                 break;
3412         }
3413         case 4: {
3414                 u32 tmp32 = 0;
3415                 pci_read_config_dword(pdev, bits->reg, &tmp32);
3416                 tmp = tmp32;
3417                 break;
3418         }
3419
3420         default:
3421                 return -EINVAL;
3422         }
3423
3424         tmp &= bits->mask;
3425
3426         return (tmp == bits->val) ? 1 : 0;
3427 }
3428
3429
3430 /**
3431  *      ata_init -
3432  *
3433  *      LOCKING:
3434  *
3435  *      RETURNS:
3436  *
3437  */
3438
3439 static int __init ata_init(void)
3440 {
3441         ata_wq = create_workqueue("ata");
3442         if (!ata_wq)
3443                 return -ENOMEM;
3444
3445         printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
3446         return 0;
3447 }
3448
3449 static void __exit ata_exit(void)
3450 {
3451         destroy_workqueue(ata_wq);
3452 }
3453
3454 module_init(ata_init);
3455 module_exit(ata_exit);
3456
3457 /*
3458  * libata is essentially a library of internal helper functions for
3459  * low-level ATA host controller drivers.  As such, the API/ABI is
3460  * likely to change as new drivers are added and updated.
3461  * Do not depend on ABI/API stability.
3462  */
3463
3464 EXPORT_SYMBOL_GPL(pci_test_config_bits);
3465 EXPORT_SYMBOL_GPL(ata_std_bios_param);
3466 EXPORT_SYMBOL_GPL(ata_std_ports);
3467 EXPORT_SYMBOL_GPL(ata_device_add);
3468 EXPORT_SYMBOL_GPL(ata_qc_complete);
3469 EXPORT_SYMBOL_GPL(ata_eng_timeout);
3470 EXPORT_SYMBOL_GPL(ata_tf_load_pio);
3471 EXPORT_SYMBOL_GPL(ata_tf_load_mmio);
3472 EXPORT_SYMBOL_GPL(ata_tf_read_pio);
3473 EXPORT_SYMBOL_GPL(ata_tf_read_mmio);
3474 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
3475 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
3476 EXPORT_SYMBOL_GPL(ata_check_status_pio);
3477 EXPORT_SYMBOL_GPL(ata_check_status_mmio);
3478 EXPORT_SYMBOL_GPL(ata_exec_command_pio);
3479 EXPORT_SYMBOL_GPL(ata_exec_command_mmio);
3480 EXPORT_SYMBOL_GPL(ata_port_start);
3481 EXPORT_SYMBOL_GPL(ata_port_stop);
3482 EXPORT_SYMBOL_GPL(ata_interrupt);
3483 EXPORT_SYMBOL_GPL(ata_fill_sg);
3484 EXPORT_SYMBOL_GPL(ata_bmdma_setup_pio);
3485 EXPORT_SYMBOL_GPL(ata_bmdma_start_pio);
3486 EXPORT_SYMBOL_GPL(ata_bmdma_setup_mmio);
3487 EXPORT_SYMBOL_GPL(ata_bmdma_start_mmio);
3488 EXPORT_SYMBOL_GPL(ata_port_probe);
3489 EXPORT_SYMBOL_GPL(sata_phy_reset);
3490 EXPORT_SYMBOL_GPL(ata_bus_reset);
3491 EXPORT_SYMBOL_GPL(ata_port_disable);
3492 EXPORT_SYMBOL_GPL(ata_pci_init_one);
3493 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
3494 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
3495 EXPORT_SYMBOL_GPL(ata_scsi_error);
3496 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
3497 EXPORT_SYMBOL_GPL(ata_scsi_release);
3498 EXPORT_SYMBOL_GPL(ata_host_intr);
3499 EXPORT_SYMBOL_GPL(ata_dev_id_string);