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