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