2 libata-core.c - helper library for ATA
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
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
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.
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>
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>
43 #include "scsi_priv.h"
44 #include <scsi/scsi_host.h>
45 #include <linux/libata.h>
47 #include <asm/semaphore.h>
48 #include <asm/byteorder.h>
52 static unsigned int ata_busy_sleep (struct ata_port *ap,
53 unsigned long tmout_pat,
55 static void ata_set_mode(struct ata_port *ap);
56 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
57 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
58 static int fgb(u32 bitmap);
59 static int ata_choose_xfer_mode(struct ata_port *ap,
61 unsigned int *xfer_shift_out);
62 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
63 static void __ata_qc_complete(struct ata_queued_cmd *qc);
65 static unsigned int ata_unique_id = 1;
66 static struct workqueue_struct *ata_wq;
68 MODULE_AUTHOR("Jeff Garzik");
69 MODULE_DESCRIPTION("Library module for ATA devices");
70 MODULE_LICENSE("GPL");
71 MODULE_VERSION(DRV_VERSION);
74 * ata_tf_load - send taskfile registers to host controller
75 * @ap: Port to which output is sent
76 * @tf: ATA taskfile register set
78 * Outputs ATA taskfile to standard ATA host controller.
81 * Inherited from caller.
84 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
86 struct ata_ioports *ioaddr = &ap->ioaddr;
87 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
89 if (tf->ctl != ap->last_ctl) {
90 outb(tf->ctl, ioaddr->ctl_addr);
91 ap->last_ctl = tf->ctl;
95 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
96 outb(tf->hob_feature, ioaddr->feature_addr);
97 outb(tf->hob_nsect, ioaddr->nsect_addr);
98 outb(tf->hob_lbal, ioaddr->lbal_addr);
99 outb(tf->hob_lbam, ioaddr->lbam_addr);
100 outb(tf->hob_lbah, ioaddr->lbah_addr);
101 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
110 outb(tf->feature, ioaddr->feature_addr);
111 outb(tf->nsect, ioaddr->nsect_addr);
112 outb(tf->lbal, ioaddr->lbal_addr);
113 outb(tf->lbam, ioaddr->lbam_addr);
114 outb(tf->lbah, ioaddr->lbah_addr);
115 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
123 if (tf->flags & ATA_TFLAG_DEVICE) {
124 outb(tf->device, ioaddr->device_addr);
125 VPRINTK("device 0x%X\n", tf->device);
132 * ata_tf_load_mmio - send taskfile registers to host controller
133 * @ap: Port to which output is sent
134 * @tf: ATA taskfile register set
136 * Outputs ATA taskfile to standard ATA host controller using MMIO.
139 * Inherited from caller.
142 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
144 struct ata_ioports *ioaddr = &ap->ioaddr;
145 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
147 if (tf->ctl != ap->last_ctl) {
148 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
149 ap->last_ctl = tf->ctl;
153 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
154 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
155 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
156 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
157 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
158 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
159 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
168 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
181 if (tf->flags & ATA_TFLAG_DEVICE) {
182 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
183 VPRINTK("device 0x%X\n", tf->device);
189 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
191 if (ap->flags & ATA_FLAG_MMIO)
192 ata_tf_load_mmio(ap, tf);
194 ata_tf_load_pio(ap, tf);
198 * ata_exec_command - issue ATA command to host controller
199 * @ap: port to which command is being issued
200 * @tf: ATA taskfile register set
202 * Issues PIO/MMIO write to ATA command register, with proper
203 * synchronization with interrupt handler / other threads.
206 * spin_lock_irqsave(host_set lock)
209 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
211 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
213 outb(tf->command, ap->ioaddr.command_addr);
219 * ata_exec_command_mmio - issue ATA command to host controller
220 * @ap: port to which command is being issued
221 * @tf: ATA taskfile register set
223 * Issues MMIO write to ATA command register, with proper
224 * synchronization with interrupt handler / other threads.
227 * spin_lock_irqsave(host_set lock)
230 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
232 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
234 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
238 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
240 if (ap->flags & ATA_FLAG_MMIO)
241 ata_exec_command_mmio(ap, tf);
243 ata_exec_command_pio(ap, tf);
247 * ata_exec - issue ATA command to host controller
248 * @ap: port to which command is being issued
249 * @tf: ATA taskfile register set
251 * Issues PIO/MMIO write to ATA command register, with proper
252 * synchronization with interrupt handler / other threads.
255 * Obtains host_set lock.
258 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
262 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
263 spin_lock_irqsave(&ap->host_set->lock, flags);
264 ap->ops->exec_command(ap, tf);
265 spin_unlock_irqrestore(&ap->host_set->lock, flags);
269 * ata_tf_to_host - issue ATA taskfile to host controller
270 * @ap: port to which command is being issued
271 * @tf: ATA taskfile register set
273 * Issues ATA taskfile register set to ATA host controller,
274 * with proper synchronization with interrupt handler and
278 * Obtains host_set lock.
281 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
283 ap->ops->tf_load(ap, tf);
289 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
290 * @ap: port to which command is being issued
291 * @tf: ATA taskfile register set
293 * Issues ATA taskfile register set to ATA host controller,
294 * with proper synchronization with interrupt handler and
298 * spin_lock_irqsave(host_set lock)
301 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
303 ap->ops->tf_load(ap, tf);
304 ap->ops->exec_command(ap, tf);
308 * ata_tf_read - input device's ATA taskfile shadow registers
309 * @ap: Port from which input is read
310 * @tf: ATA taskfile register set for storing input
312 * Reads ATA taskfile registers for currently-selected device
316 * Inherited from caller.
319 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
321 struct ata_ioports *ioaddr = &ap->ioaddr;
323 tf->nsect = inb(ioaddr->nsect_addr);
324 tf->lbal = inb(ioaddr->lbal_addr);
325 tf->lbam = inb(ioaddr->lbam_addr);
326 tf->lbah = inb(ioaddr->lbah_addr);
327 tf->device = inb(ioaddr->device_addr);
329 if (tf->flags & ATA_TFLAG_LBA48) {
330 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
331 tf->hob_feature = inb(ioaddr->error_addr);
332 tf->hob_nsect = inb(ioaddr->nsect_addr);
333 tf->hob_lbal = inb(ioaddr->lbal_addr);
334 tf->hob_lbam = inb(ioaddr->lbam_addr);
335 tf->hob_lbah = inb(ioaddr->lbah_addr);
340 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
341 * @ap: Port from which input is read
342 * @tf: ATA taskfile register set for storing input
344 * Reads ATA taskfile registers for currently-selected device
348 * Inherited from caller.
351 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
353 struct ata_ioports *ioaddr = &ap->ioaddr;
355 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
356 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
357 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
358 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
359 tf->device = readb((void __iomem *)ioaddr->device_addr);
361 if (tf->flags & ATA_TFLAG_LBA48) {
362 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
363 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
364 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
365 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
366 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
367 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
371 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
373 if (ap->flags & ATA_FLAG_MMIO)
374 ata_tf_read_mmio(ap, tf);
376 ata_tf_read_pio(ap, tf);
380 * ata_check_status_pio - Read device status reg & clear interrupt
381 * @ap: port where the device is
383 * Reads ATA taskfile status register for currently-selected device
384 * and return it's value. This also clears pending interrupts
388 * Inherited from caller.
390 static u8 ata_check_status_pio(struct ata_port *ap)
392 return inb(ap->ioaddr.status_addr);
396 * ata_check_status_mmio - Read device status reg & clear interrupt
397 * @ap: port where the device is
399 * Reads ATA taskfile status register for currently-selected device
400 * via MMIO and return it's value. This also clears pending interrupts
404 * Inherited from caller.
406 static u8 ata_check_status_mmio(struct ata_port *ap)
408 return readb((void __iomem *) ap->ioaddr.status_addr);
411 u8 ata_check_status(struct ata_port *ap)
413 if (ap->flags & ATA_FLAG_MMIO)
414 return ata_check_status_mmio(ap);
415 return ata_check_status_pio(ap);
418 u8 ata_altstatus(struct ata_port *ap)
420 if (ap->ops->check_altstatus)
421 return ap->ops->check_altstatus(ap);
423 if (ap->flags & ATA_FLAG_MMIO)
424 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
425 return inb(ap->ioaddr.altstatus_addr);
428 u8 ata_chk_err(struct ata_port *ap)
430 if (ap->ops->check_err)
431 return ap->ops->check_err(ap);
433 if (ap->flags & ATA_FLAG_MMIO) {
434 return readb((void __iomem *) ap->ioaddr.error_addr);
436 return inb(ap->ioaddr.error_addr);
440 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
441 * @tf: Taskfile to convert
442 * @fis: Buffer into which data will output
443 * @pmp: Port multiplier port
445 * Converts a standard ATA taskfile to a Serial ATA
446 * FIS structure (Register - Host to Device).
449 * Inherited from caller.
452 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
454 fis[0] = 0x27; /* Register - Host to Device FIS */
455 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
456 bit 7 indicates Command FIS */
457 fis[2] = tf->command;
458 fis[3] = tf->feature;
465 fis[8] = tf->hob_lbal;
466 fis[9] = tf->hob_lbam;
467 fis[10] = tf->hob_lbah;
468 fis[11] = tf->hob_feature;
471 fis[13] = tf->hob_nsect;
482 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
483 * @fis: Buffer from which data will be input
484 * @tf: Taskfile to output
486 * Converts a standard ATA taskfile to a Serial ATA
487 * FIS structure (Register - Host to Device).
490 * Inherited from caller.
493 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
495 tf->command = fis[2]; /* status */
496 tf->feature = fis[3]; /* error */
503 tf->hob_lbal = fis[8];
504 tf->hob_lbam = fis[9];
505 tf->hob_lbah = fis[10];
508 tf->hob_nsect = fis[13];
512 * ata_prot_to_cmd - determine which read/write opcodes to use
513 * @protocol: ATA_PROT_xxx taskfile protocol
514 * @lba48: true is lba48 is present
516 * Given necessary input, determine which read/write commands
517 * to use to transfer data.
522 static int ata_prot_to_cmd(int protocol, int lba48)
524 int rcmd = 0, wcmd = 0;
529 rcmd = ATA_CMD_PIO_READ_EXT;
530 wcmd = ATA_CMD_PIO_WRITE_EXT;
532 rcmd = ATA_CMD_PIO_READ;
533 wcmd = ATA_CMD_PIO_WRITE;
539 rcmd = ATA_CMD_READ_EXT;
540 wcmd = ATA_CMD_WRITE_EXT;
543 wcmd = ATA_CMD_WRITE;
551 return rcmd | (wcmd << 8);
555 * ata_dev_set_protocol - set taskfile protocol and r/w commands
556 * @dev: device to examine and configure
558 * Examine the device configuration, after we have
559 * read the identify-device page and configured the
560 * data transfer mode. Set internal state related to
561 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
562 * and calculate the proper read/write commands to use.
567 static void ata_dev_set_protocol(struct ata_device *dev)
569 int pio = (dev->flags & ATA_DFLAG_PIO);
570 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
574 proto = dev->xfer_protocol = ATA_PROT_PIO;
576 proto = dev->xfer_protocol = ATA_PROT_DMA;
578 cmd = ata_prot_to_cmd(proto, lba48);
582 dev->read_cmd = cmd & 0xff;
583 dev->write_cmd = (cmd >> 8) & 0xff;
586 static const char * xfer_mode_str[] = {
606 * ata_udma_string - convert UDMA bit offset to string
607 * @mask: mask of bits supported; only highest bit counts.
609 * Determine string which represents the highest speed
610 * (highest bit in @udma_mask).
616 * Constant C string representing highest speed listed in
617 * @udma_mask, or the constant C string "<n/a>".
620 static const char *ata_mode_string(unsigned int mask)
624 for (i = 7; i >= 0; i--)
627 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
630 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
637 return xfer_mode_str[i];
641 * ata_pio_devchk - PATA device presence detection
642 * @ap: ATA channel to examine
643 * @device: Device to examine (starting at zero)
645 * This technique was originally described in
646 * Hale Landis's ATADRVR (www.ata-atapi.com), and
647 * later found its way into the ATA/ATAPI spec.
649 * Write a pattern to the ATA shadow registers,
650 * and if a device is present, it will respond by
651 * correctly storing and echoing back the
652 * ATA shadow register contents.
658 static unsigned int ata_pio_devchk(struct ata_port *ap,
661 struct ata_ioports *ioaddr = &ap->ioaddr;
664 ap->ops->dev_select(ap, device);
666 outb(0x55, ioaddr->nsect_addr);
667 outb(0xaa, ioaddr->lbal_addr);
669 outb(0xaa, ioaddr->nsect_addr);
670 outb(0x55, ioaddr->lbal_addr);
672 outb(0x55, ioaddr->nsect_addr);
673 outb(0xaa, ioaddr->lbal_addr);
675 nsect = inb(ioaddr->nsect_addr);
676 lbal = inb(ioaddr->lbal_addr);
678 if ((nsect == 0x55) && (lbal == 0xaa))
679 return 1; /* we found a device */
681 return 0; /* nothing found */
685 * ata_mmio_devchk - PATA device presence detection
686 * @ap: ATA channel to examine
687 * @device: Device to examine (starting at zero)
689 * This technique was originally described in
690 * Hale Landis's ATADRVR (www.ata-atapi.com), and
691 * later found its way into the ATA/ATAPI spec.
693 * Write a pattern to the ATA shadow registers,
694 * and if a device is present, it will respond by
695 * correctly storing and echoing back the
696 * ATA shadow register contents.
702 static unsigned int ata_mmio_devchk(struct ata_port *ap,
705 struct ata_ioports *ioaddr = &ap->ioaddr;
708 ap->ops->dev_select(ap, device);
710 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
711 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
713 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
714 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
716 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
717 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
719 nsect = readb((void __iomem *) ioaddr->nsect_addr);
720 lbal = readb((void __iomem *) ioaddr->lbal_addr);
722 if ((nsect == 0x55) && (lbal == 0xaa))
723 return 1; /* we found a device */
725 return 0; /* nothing found */
729 * ata_devchk - PATA device presence detection
730 * @ap: ATA channel to examine
731 * @device: Device to examine (starting at zero)
733 * Dispatch ATA device presence detection, depending
734 * on whether we are using PIO or MMIO to talk to the
735 * ATA shadow registers.
741 static unsigned int ata_devchk(struct ata_port *ap,
744 if (ap->flags & ATA_FLAG_MMIO)
745 return ata_mmio_devchk(ap, device);
746 return ata_pio_devchk(ap, device);
750 * ata_dev_classify - determine device type based on ATA-spec signature
751 * @tf: ATA taskfile register set for device to be identified
753 * Determine from taskfile register contents whether a device is
754 * ATA or ATAPI, as per "Signature and persistence" section
755 * of ATA/PI spec (volume 1, sect 5.14).
761 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
762 * the event of failure.
765 unsigned int ata_dev_classify(struct ata_taskfile *tf)
767 /* Apple's open source Darwin code hints that some devices only
768 * put a proper signature into the LBA mid/high registers,
769 * So, we only check those. It's sufficient for uniqueness.
772 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
773 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
774 DPRINTK("found ATA device by sig\n");
778 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
779 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
780 DPRINTK("found ATAPI device by sig\n");
781 return ATA_DEV_ATAPI;
784 DPRINTK("unknown device\n");
785 return ATA_DEV_UNKNOWN;
789 * ata_dev_try_classify - Parse returned ATA device signature
790 * @ap: ATA channel to examine
791 * @device: Device to examine (starting at zero)
793 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
794 * an ATA/ATAPI-defined set of values is placed in the ATA
795 * shadow registers, indicating the results of device detection
798 * Select the ATA device, and read the values from the ATA shadow
799 * registers. Then parse according to the Error register value,
800 * and the spec-defined values examined by ata_dev_classify().
806 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
808 struct ata_device *dev = &ap->device[device];
809 struct ata_taskfile tf;
813 ap->ops->dev_select(ap, device);
815 memset(&tf, 0, sizeof(tf));
817 err = ata_chk_err(ap);
818 ap->ops->tf_read(ap, &tf);
820 dev->class = ATA_DEV_NONE;
822 /* see if device passed diags */
825 else if ((device == 0) && (err == 0x81))
830 /* determine if device if ATA or ATAPI */
831 class = ata_dev_classify(&tf);
832 if (class == ATA_DEV_UNKNOWN)
834 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
843 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
844 * @id: IDENTIFY DEVICE results we will examine
845 * @s: string into which data is output
846 * @ofs: offset into identify device page
847 * @len: length of string to return. must be an even number.
849 * The strings in the IDENTIFY DEVICE page are broken up into
850 * 16-bit chunks. Run through the string, and output each
851 * 8-bit chunk linearly, regardless of platform.
857 void ata_dev_id_string(u16 *id, unsigned char *s,
858 unsigned int ofs, unsigned int len)
876 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
881 * ata_std_dev_select - Select device 0/1 on ATA bus
882 * @ap: ATA channel to manipulate
883 * @device: ATA device (numbered from zero) to select
885 * Use the method defined in the ATA specification to
886 * make either device 0, or device 1, active on the
893 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
898 tmp = ATA_DEVICE_OBS;
900 tmp = ATA_DEVICE_OBS | ATA_DEV1;
902 if (ap->flags & ATA_FLAG_MMIO) {
903 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
905 outb(tmp, ap->ioaddr.device_addr);
907 ata_pause(ap); /* needed; also flushes, for mmio */
911 * ata_dev_select - Select device 0/1 on ATA bus
912 * @ap: ATA channel to manipulate
913 * @device: ATA device (numbered from zero) to select
914 * @wait: non-zero to wait for Status register BSY bit to clear
915 * @can_sleep: non-zero if context allows sleeping
917 * Use the method defined in the ATA specification to
918 * make either device 0, or device 1, active on the
921 * This is a high-level version of ata_std_dev_select(),
922 * which additionally provides the services of inserting
923 * the proper pauses and status polling, where needed.
929 void ata_dev_select(struct ata_port *ap, unsigned int device,
930 unsigned int wait, unsigned int can_sleep)
932 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
933 ap->id, device, wait);
938 ap->ops->dev_select(ap, device);
941 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
948 * ata_dump_id - IDENTIFY DEVICE info debugging output
949 * @dev: Device whose IDENTIFY DEVICE page we will dump
951 * Dump selected 16-bit words from a detected device's
952 * IDENTIFY PAGE page.
958 static inline void ata_dump_id(struct ata_device *dev)
960 DPRINTK("49==0x%04x "
970 DPRINTK("80==0x%04x "
980 DPRINTK("88==0x%04x "
987 * ata_dev_identify - obtain IDENTIFY x DEVICE page
988 * @ap: port on which device we wish to probe resides
989 * @device: device bus address, starting at zero
991 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
992 * command, and read back the 512-byte device information page.
993 * The device information page is fed to us via the standard
994 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
995 * using standard PIO-IN paths)
997 * After reading the device information page, we use several
998 * bits of information from it to initialize data structures
999 * that will be used during the lifetime of the ata_device.
1000 * Other data from the info page is used to disqualify certain
1001 * older ATA devices we do not wish to support.
1004 * Inherited from caller. Some functions called by this function
1005 * obtain the host_set lock.
1008 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1010 struct ata_device *dev = &ap->device[device];
1013 unsigned long xfer_modes;
1015 unsigned int using_edd;
1016 DECLARE_COMPLETION(wait);
1017 struct ata_queued_cmd *qc;
1018 unsigned long flags;
1021 if (!ata_dev_present(dev)) {
1022 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1027 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1032 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1034 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1035 dev->class == ATA_DEV_NONE);
1037 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1039 qc = ata_qc_new_init(ap, dev);
1042 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1043 qc->dma_dir = DMA_FROM_DEVICE;
1044 qc->tf.protocol = ATA_PROT_PIO;
1048 if (dev->class == ATA_DEV_ATA) {
1049 qc->tf.command = ATA_CMD_ID_ATA;
1050 DPRINTK("do ATA identify\n");
1052 qc->tf.command = ATA_CMD_ID_ATAPI;
1053 DPRINTK("do ATAPI identify\n");
1056 qc->waiting = &wait;
1057 qc->complete_fn = ata_qc_complete_noop;
1059 spin_lock_irqsave(&ap->host_set->lock, flags);
1060 rc = ata_qc_issue(qc);
1061 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1066 wait_for_completion(&wait);
1068 status = ata_chk_status(ap);
1069 if (status & ATA_ERR) {
1071 * arg! EDD works for all test cases, but seems to return
1072 * the ATA signature for some ATAPI devices. Until the
1073 * reason for this is found and fixed, we fix up the mess
1074 * here. If IDENTIFY DEVICE returns command aborted
1075 * (as ATAPI devices do), then we issue an
1076 * IDENTIFY PACKET DEVICE.
1078 * ATA software reset (SRST, the default) does not appear
1079 * to have this problem.
1081 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1082 u8 err = ata_chk_err(ap);
1083 if (err & ATA_ABORTED) {
1084 dev->class = ATA_DEV_ATAPI;
1095 swap_buf_le16(dev->id, ATA_ID_WORDS);
1097 /* print device capabilities */
1098 printk(KERN_DEBUG "ata%u: dev %u cfg "
1099 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1100 ap->id, device, dev->id[49],
1101 dev->id[82], dev->id[83], dev->id[84],
1102 dev->id[85], dev->id[86], dev->id[87],
1106 * common ATA, ATAPI feature tests
1109 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1110 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1111 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1115 /* quick-n-dirty find max transfer mode; for printk only */
1116 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1118 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1120 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1121 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1126 /* ATA-specific feature tests */
1127 if (dev->class == ATA_DEV_ATA) {
1128 if (!ata_id_is_ata(dev->id)) /* sanity check */
1131 tmp = dev->id[ATA_ID_MAJOR_VER];
1132 for (i = 14; i >= 1; i--)
1136 /* we require at least ATA-3 */
1138 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1142 if (ata_id_has_lba48(dev->id)) {
1143 dev->flags |= ATA_DFLAG_LBA48;
1144 dev->n_sectors = ata_id_u64(dev->id, 100);
1146 dev->n_sectors = ata_id_u32(dev->id, 60);
1149 ap->host->max_cmd_len = 16;
1151 /* print device info to dmesg */
1152 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1154 ata_mode_string(xfer_modes),
1155 (unsigned long long)dev->n_sectors,
1156 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1159 /* ATAPI-specific feature tests */
1161 if (ata_id_is_ata(dev->id)) /* sanity check */
1164 rc = atapi_cdb_len(dev->id);
1165 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1166 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1169 ap->cdb_len = (unsigned int) rc;
1170 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1172 /* print device info to dmesg */
1173 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1175 ata_mode_string(xfer_modes));
1178 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1182 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1185 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1186 DPRINTK("EXIT, err\n");
1190 static inline u8 ata_dev_knobble(struct ata_port *ap)
1192 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1196 * ata_dev_config - Run device specific handlers and check for
1197 * SATA->PATA bridges
1204 void ata_dev_config(struct ata_port *ap, unsigned int i)
1206 /* limit bridge transfers to udma5, 200 sectors */
1207 if (ata_dev_knobble(ap)) {
1208 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1209 ap->id, ap->device->devno);
1210 ap->udma_mask &= ATA_UDMA5;
1211 ap->host->max_sectors = ATA_MAX_SECTORS;
1212 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1213 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1216 if (ap->ops->dev_config)
1217 ap->ops->dev_config(ap, &ap->device[i]);
1221 * ata_bus_probe - Reset and probe ATA bus
1227 * Zero on success, non-zero on error.
1230 static int ata_bus_probe(struct ata_port *ap)
1232 unsigned int i, found = 0;
1234 ap->ops->phy_reset(ap);
1235 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1238 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1239 ata_dev_identify(ap, i);
1240 if (ata_dev_present(&ap->device[i])) {
1242 ata_dev_config(ap,i);
1246 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1247 goto err_out_disable;
1250 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1251 goto err_out_disable;
1256 ap->ops->port_disable(ap);
1268 void ata_port_probe(struct ata_port *ap)
1270 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1274 * __sata_phy_reset -
1280 void __sata_phy_reset(struct ata_port *ap)
1283 unsigned long timeout = jiffies + (HZ * 5);
1285 if (ap->flags & ATA_FLAG_SATA_RESET) {
1286 scr_write(ap, SCR_CONTROL, 0x301); /* issue phy wake/reset */
1287 scr_read(ap, SCR_STATUS); /* dummy read; flush */
1288 udelay(400); /* FIXME: a guess */
1290 scr_write(ap, SCR_CONTROL, 0x300); /* issue phy wake/clear reset */
1292 /* wait for phy to become ready, if necessary */
1295 sstatus = scr_read(ap, SCR_STATUS);
1296 if ((sstatus & 0xf) != 1)
1298 } while (time_before(jiffies, timeout));
1300 /* TODO: phy layer with polling, timeouts, etc. */
1301 if (sata_dev_present(ap))
1304 sstatus = scr_read(ap, SCR_STATUS);
1305 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1307 ata_port_disable(ap);
1310 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1313 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1314 ata_port_disable(ap);
1318 ap->cbl = ATA_CBL_SATA;
1322 * __sata_phy_reset -
1328 void sata_phy_reset(struct ata_port *ap)
1330 __sata_phy_reset(ap);
1331 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1337 * ata_port_disable -
1343 void ata_port_disable(struct ata_port *ap)
1345 ap->device[0].class = ATA_DEV_NONE;
1346 ap->device[1].class = ATA_DEV_NONE;
1347 ap->flags |= ATA_FLAG_PORT_DISABLED;
1353 } xfer_mode_classes[] = {
1354 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1355 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1356 { ATA_SHIFT_PIO, XFER_PIO_0 },
1359 static inline u8 base_from_shift(unsigned int shift)
1363 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1364 if (xfer_mode_classes[i].shift == shift)
1365 return xfer_mode_classes[i].base;
1370 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1375 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1378 if (dev->xfer_shift == ATA_SHIFT_PIO)
1379 dev->flags |= ATA_DFLAG_PIO;
1381 ata_dev_set_xfermode(ap, dev);
1383 base = base_from_shift(dev->xfer_shift);
1384 ofs = dev->xfer_mode - base;
1385 idx = ofs + dev->xfer_shift;
1386 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1388 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1389 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1391 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1392 ap->id, dev->devno, xfer_mode_str[idx]);
1395 static int ata_host_set_pio(struct ata_port *ap)
1401 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1404 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1408 base = base_from_shift(ATA_SHIFT_PIO);
1409 xfer_mode = base + x;
1411 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1412 (int)base, (int)xfer_mode, mask, x);
1414 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1415 struct ata_device *dev = &ap->device[i];
1416 if (ata_dev_present(dev)) {
1417 dev->pio_mode = xfer_mode;
1418 dev->xfer_mode = xfer_mode;
1419 dev->xfer_shift = ATA_SHIFT_PIO;
1420 if (ap->ops->set_piomode)
1421 ap->ops->set_piomode(ap, dev);
1428 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1429 unsigned int xfer_shift)
1433 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1434 struct ata_device *dev = &ap->device[i];
1435 if (ata_dev_present(dev)) {
1436 dev->dma_mode = xfer_mode;
1437 dev->xfer_mode = xfer_mode;
1438 dev->xfer_shift = xfer_shift;
1439 if (ap->ops->set_dmamode)
1440 ap->ops->set_dmamode(ap, dev);
1446 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1447 * @ap: port on which timings will be programmed
1452 static void ata_set_mode(struct ata_port *ap)
1454 unsigned int i, xfer_shift;
1458 /* step 1: always set host PIO timings */
1459 rc = ata_host_set_pio(ap);
1463 /* step 2: choose the best data xfer mode */
1464 xfer_mode = xfer_shift = 0;
1465 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1469 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1470 if (xfer_shift != ATA_SHIFT_PIO)
1471 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1473 /* step 4: update devices' xfer mode */
1474 ata_dev_set_mode(ap, &ap->device[0]);
1475 ata_dev_set_mode(ap, &ap->device[1]);
1477 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1480 if (ap->ops->post_set_mode)
1481 ap->ops->post_set_mode(ap);
1483 for (i = 0; i < 2; i++) {
1484 struct ata_device *dev = &ap->device[i];
1485 ata_dev_set_protocol(dev);
1491 ata_port_disable(ap);
1495 * ata_busy_sleep - sleep until BSY clears, or timeout
1496 * @ap: port containing status register to be polled
1497 * @tmout_pat: impatience timeout
1498 * @tmout: overall timeout
1504 static unsigned int ata_busy_sleep (struct ata_port *ap,
1505 unsigned long tmout_pat,
1506 unsigned long tmout)
1508 unsigned long timer_start, timeout;
1511 status = ata_busy_wait(ap, ATA_BUSY, 300);
1512 timer_start = jiffies;
1513 timeout = timer_start + tmout_pat;
1514 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1516 status = ata_busy_wait(ap, ATA_BUSY, 3);
1519 if (status & ATA_BUSY)
1520 printk(KERN_WARNING "ata%u is slow to respond, "
1521 "please be patient\n", ap->id);
1523 timeout = timer_start + tmout;
1524 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1526 status = ata_chk_status(ap);
1529 if (status & ATA_BUSY) {
1530 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1531 ap->id, tmout / HZ);
1538 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1540 struct ata_ioports *ioaddr = &ap->ioaddr;
1541 unsigned int dev0 = devmask & (1 << 0);
1542 unsigned int dev1 = devmask & (1 << 1);
1543 unsigned long timeout;
1545 /* if device 0 was found in ata_devchk, wait for its
1549 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1551 /* if device 1 was found in ata_devchk, wait for
1552 * register access, then wait for BSY to clear
1554 timeout = jiffies + ATA_TMOUT_BOOT;
1558 ap->ops->dev_select(ap, 1);
1559 if (ap->flags & ATA_FLAG_MMIO) {
1560 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1561 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1563 nsect = inb(ioaddr->nsect_addr);
1564 lbal = inb(ioaddr->lbal_addr);
1566 if ((nsect == 1) && (lbal == 1))
1568 if (time_after(jiffies, timeout)) {
1572 msleep(50); /* give drive a breather */
1575 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1577 /* is all this really necessary? */
1578 ap->ops->dev_select(ap, 0);
1580 ap->ops->dev_select(ap, 1);
1582 ap->ops->dev_select(ap, 0);
1593 static unsigned int ata_bus_edd(struct ata_port *ap)
1595 struct ata_taskfile tf;
1597 /* set up execute-device-diag (bus reset) taskfile */
1598 /* also, take interrupts to a known state (disabled) */
1599 DPRINTK("execute-device-diag\n");
1600 ata_tf_init(ap, &tf, 0);
1602 tf.command = ATA_CMD_EDD;
1603 tf.protocol = ATA_PROT_NODATA;
1606 ata_tf_to_host(ap, &tf);
1608 /* spec says at least 2ms. but who knows with those
1609 * crazy ATAPI devices...
1613 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1616 static unsigned int ata_bus_softreset(struct ata_port *ap,
1617 unsigned int devmask)
1619 struct ata_ioports *ioaddr = &ap->ioaddr;
1621 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1623 /* software reset. causes dev0 to be selected */
1624 if (ap->flags & ATA_FLAG_MMIO) {
1625 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1626 udelay(20); /* FIXME: flush */
1627 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1628 udelay(20); /* FIXME: flush */
1629 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1631 outb(ap->ctl, ioaddr->ctl_addr);
1633 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1635 outb(ap->ctl, ioaddr->ctl_addr);
1638 /* spec mandates ">= 2ms" before checking status.
1639 * We wait 150ms, because that was the magic delay used for
1640 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1641 * between when the ATA command register is written, and then
1642 * status is checked. Because waiting for "a while" before
1643 * checking status is fine, post SRST, we perform this magic
1644 * delay here as well.
1648 ata_bus_post_reset(ap, devmask);
1654 * ata_bus_reset - reset host port and associated ATA channel
1655 * @ap: port to reset
1657 * This is typically the first time we actually start issuing
1658 * commands to the ATA channel. We wait for BSY to clear, then
1659 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1660 * result. Determine what devices, if any, are on the channel
1661 * by looking at the device 0/1 error register. Look at the signature
1662 * stored in each device's taskfile registers, to determine if
1663 * the device is ATA or ATAPI.
1666 * Inherited from caller. Some functions called by this function
1667 * obtain the host_set lock.
1670 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1673 void ata_bus_reset(struct ata_port *ap)
1675 struct ata_ioports *ioaddr = &ap->ioaddr;
1676 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1678 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1680 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1682 /* determine if device 0/1 are present */
1683 if (ap->flags & ATA_FLAG_SATA_RESET)
1686 dev0 = ata_devchk(ap, 0);
1688 dev1 = ata_devchk(ap, 1);
1692 devmask |= (1 << 0);
1694 devmask |= (1 << 1);
1696 /* select device 0 again */
1697 ap->ops->dev_select(ap, 0);
1699 /* issue bus reset */
1700 if (ap->flags & ATA_FLAG_SRST)
1701 rc = ata_bus_softreset(ap, devmask);
1702 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1703 /* set up device control */
1704 if (ap->flags & ATA_FLAG_MMIO)
1705 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1707 outb(ap->ctl, ioaddr->ctl_addr);
1708 rc = ata_bus_edd(ap);
1715 * determine by signature whether we have ATA or ATAPI devices
1717 err = ata_dev_try_classify(ap, 0);
1718 if ((slave_possible) && (err != 0x81))
1719 ata_dev_try_classify(ap, 1);
1721 /* re-enable interrupts */
1722 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1725 /* is double-select really necessary? */
1726 if (ap->device[1].class != ATA_DEV_NONE)
1727 ap->ops->dev_select(ap, 1);
1728 if (ap->device[0].class != ATA_DEV_NONE)
1729 ap->ops->dev_select(ap, 0);
1731 /* if no devices were detected, disable this port */
1732 if ((ap->device[0].class == ATA_DEV_NONE) &&
1733 (ap->device[1].class == ATA_DEV_NONE))
1736 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1737 /* set up device control for ATA_FLAG_SATA_RESET */
1738 if (ap->flags & ATA_FLAG_MMIO)
1739 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1741 outb(ap->ctl, ioaddr->ctl_addr);
1748 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1749 ap->ops->port_disable(ap);
1754 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1756 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1757 ap->id, dev->devno);
1760 static const char * ata_dma_blacklist [] = {
1779 "Toshiba CD-ROM XM-6202B",
1781 "E-IDE CD-ROM CR-840",
1784 "SAMSUNG CD-ROM SC-148C",
1785 "SAMSUNG CD-ROM SC",
1787 "SAMSUNG CD-ROM SN-124",
1788 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1792 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1794 unsigned char model_num[40];
1799 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1802 len = strnlen(s, sizeof(model_num));
1804 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1805 while ((len > 0) && (s[len - 1] == ' ')) {
1810 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1811 if (!strncmp(ata_dma_blacklist[i], s, len))
1817 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1819 struct ata_device *master, *slave;
1822 master = &ap->device[0];
1823 slave = &ap->device[1];
1825 assert (ata_dev_present(master) || ata_dev_present(slave));
1827 if (shift == ATA_SHIFT_UDMA) {
1828 mask = ap->udma_mask;
1829 if (ata_dev_present(master)) {
1830 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1831 if (ata_dma_blacklisted(ap, master)) {
1833 ata_pr_blacklisted(ap, master);
1836 if (ata_dev_present(slave)) {
1837 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1838 if (ata_dma_blacklisted(ap, slave)) {
1840 ata_pr_blacklisted(ap, slave);
1844 else if (shift == ATA_SHIFT_MWDMA) {
1845 mask = ap->mwdma_mask;
1846 if (ata_dev_present(master)) {
1847 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
1848 if (ata_dma_blacklisted(ap, master)) {
1850 ata_pr_blacklisted(ap, master);
1853 if (ata_dev_present(slave)) {
1854 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
1855 if (ata_dma_blacklisted(ap, slave)) {
1857 ata_pr_blacklisted(ap, slave);
1861 else if (shift == ATA_SHIFT_PIO) {
1862 mask = ap->pio_mask;
1863 if (ata_dev_present(master)) {
1864 /* spec doesn't return explicit support for
1865 * PIO0-2, so we fake it
1867 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
1872 if (ata_dev_present(slave)) {
1873 /* spec doesn't return explicit support for
1874 * PIO0-2, so we fake it
1876 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
1883 mask = 0xffffffff; /* shut up compiler warning */
1890 /* find greatest bit */
1891 static int fgb(u32 bitmap)
1896 for (i = 0; i < 32; i++)
1897 if (bitmap & (1 << i))
1904 * ata_choose_xfer_mode - attempt to find best transfer mode
1905 * @ap: Port for which an xfer mode will be selected
1906 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
1907 * @xfer_shift_out: (output) bit shift that selects this mode
1912 * Zero on success, negative on error.
1915 static int ata_choose_xfer_mode(struct ata_port *ap,
1917 unsigned int *xfer_shift_out)
1919 unsigned int mask, shift;
1922 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
1923 shift = xfer_mode_classes[i].shift;
1924 mask = ata_get_mode_mask(ap, shift);
1928 *xfer_mode_out = xfer_mode_classes[i].base + x;
1929 *xfer_shift_out = shift;
1938 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1939 * @ap: Port associated with device @dev
1940 * @dev: Device to which command will be sent
1945 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1947 DECLARE_COMPLETION(wait);
1948 struct ata_queued_cmd *qc;
1950 unsigned long flags;
1952 /* set up set-features taskfile */
1953 DPRINTK("set features - xfer mode\n");
1955 qc = ata_qc_new_init(ap, dev);
1958 qc->tf.command = ATA_CMD_SET_FEATURES;
1959 qc->tf.feature = SETFEATURES_XFER;
1960 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1961 qc->tf.protocol = ATA_PROT_NODATA;
1962 qc->tf.nsect = dev->xfer_mode;
1964 qc->waiting = &wait;
1965 qc->complete_fn = ata_qc_complete_noop;
1967 spin_lock_irqsave(&ap->host_set->lock, flags);
1968 rc = ata_qc_issue(qc);
1969 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1972 ata_port_disable(ap);
1974 wait_for_completion(&wait);
1986 static void ata_sg_clean(struct ata_queued_cmd *qc)
1988 struct ata_port *ap = qc->ap;
1989 struct scatterlist *sg = qc->sg;
1990 int dir = qc->dma_dir;
1992 assert(qc->flags & ATA_QCFLAG_DMAMAP);
1995 if (qc->flags & ATA_QCFLAG_SINGLE)
1996 assert(qc->n_elem == 1);
1998 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2000 if (qc->flags & ATA_QCFLAG_SG)
2001 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2003 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2004 sg_dma_len(&sg[0]), dir);
2006 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2011 * ata_fill_sg - Fill PCI IDE PRD table
2012 * @qc: Metadata associated with taskfile to be transferred
2017 static void ata_fill_sg(struct ata_queued_cmd *qc)
2019 struct scatterlist *sg = qc->sg;
2020 struct ata_port *ap = qc->ap;
2021 unsigned int idx, nelem;
2024 assert(qc->n_elem > 0);
2027 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2031 /* determine if physical DMA addr spans 64K boundary.
2032 * Note h/w doesn't support 64-bit, so we unconditionally
2033 * truncate dma_addr_t to u32.
2035 addr = (u32) sg_dma_address(sg);
2036 sg_len = sg_dma_len(sg);
2039 offset = addr & 0xffff;
2041 if ((offset + sg_len) > 0x10000)
2042 len = 0x10000 - offset;
2044 ap->prd[idx].addr = cpu_to_le32(addr);
2045 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2046 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2055 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2058 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2059 * @qc: Metadata associated with taskfile to check
2062 * RETURNS: 0 when ATAPI DMA can be used
2065 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2067 struct ata_port *ap = qc->ap;
2068 int rc = 0; /* Assume ATAPI DMA is OK by default */
2070 if (ap->ops->check_atapi_dma)
2071 rc = ap->ops->check_atapi_dma(qc);
2076 * ata_qc_prep - Prepare taskfile for submission
2077 * @qc: Metadata associated with taskfile to be prepared
2080 * spin_lock_irqsave(host_set lock)
2082 void ata_qc_prep(struct ata_queued_cmd *qc)
2084 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2090 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2092 struct scatterlist *sg;
2094 qc->flags |= ATA_QCFLAG_SINGLE;
2096 memset(&qc->sgent, 0, sizeof(qc->sgent));
2097 qc->sg = &qc->sgent;
2102 sg->page = virt_to_page(buf);
2103 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2104 sg_dma_len(sg) = buflen;
2107 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2108 unsigned int n_elem)
2110 qc->flags |= ATA_QCFLAG_SG;
2112 qc->n_elem = n_elem;
2116 * ata_sg_setup_one -
2120 * spin_lock_irqsave(host_set lock)
2126 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2128 struct ata_port *ap = qc->ap;
2129 int dir = qc->dma_dir;
2130 struct scatterlist *sg = qc->sg;
2131 dma_addr_t dma_address;
2133 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2134 sg_dma_len(sg), dir);
2135 if (dma_mapping_error(dma_address))
2138 sg_dma_address(sg) = dma_address;
2140 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2141 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2151 * spin_lock_irqsave(host_set lock)
2157 static int ata_sg_setup(struct ata_queued_cmd *qc)
2159 struct ata_port *ap = qc->ap;
2160 struct scatterlist *sg = qc->sg;
2163 VPRINTK("ENTER, ata%u\n", ap->id);
2164 assert(qc->flags & ATA_QCFLAG_SG);
2167 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2171 DPRINTK("%d sg elements mapped\n", n_elem);
2173 qc->n_elem = n_elem;
2188 static unsigned long ata_pio_poll(struct ata_port *ap)
2191 unsigned int poll_state = PIO_ST_UNKNOWN;
2192 unsigned int reg_state = PIO_ST_UNKNOWN;
2193 const unsigned int tmout_state = PIO_ST_TMOUT;
2195 switch (ap->pio_task_state) {
2198 poll_state = PIO_ST_POLL;
2202 case PIO_ST_LAST_POLL:
2203 poll_state = PIO_ST_LAST_POLL;
2204 reg_state = PIO_ST_LAST;
2211 status = ata_chk_status(ap);
2212 if (status & ATA_BUSY) {
2213 if (time_after(jiffies, ap->pio_task_timeout)) {
2214 ap->pio_task_state = tmout_state;
2217 ap->pio_task_state = poll_state;
2218 return ATA_SHORT_PAUSE;
2221 ap->pio_task_state = reg_state;
2226 * ata_pio_complete -
2232 static void ata_pio_complete (struct ata_port *ap)
2234 struct ata_queued_cmd *qc;
2238 * This is purely hueristic. This is a fast path.
2239 * Sometimes when we enter, BSY will be cleared in
2240 * a chk-status or two. If not, the drive is probably seeking
2241 * or something. Snooze for a couple msecs, then
2242 * chk-status again. If still busy, fall back to
2243 * PIO_ST_POLL state.
2245 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2246 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2248 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2249 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2250 ap->pio_task_state = PIO_ST_LAST_POLL;
2251 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2256 drv_stat = ata_wait_idle(ap);
2257 if (!ata_ok(drv_stat)) {
2258 ap->pio_task_state = PIO_ST_ERR;
2262 qc = ata_qc_from_tag(ap, ap->active_tag);
2265 ap->pio_task_state = PIO_ST_IDLE;
2269 ata_qc_complete(qc, drv_stat);
2272 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2277 for (i = 0; i < buf_words; i++)
2278 buf[i] = le16_to_cpu(buf[i]);
2279 #endif /* __BIG_ENDIAN */
2282 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2283 unsigned int buflen, int write_data)
2286 unsigned int words = buflen >> 1;
2287 u16 *buf16 = (u16 *) buf;
2288 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2291 for (i = 0; i < words; i++)
2292 writew(le16_to_cpu(buf16[i]), mmio);
2294 for (i = 0; i < words; i++)
2295 buf16[i] = cpu_to_le16(readw(mmio));
2299 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2300 unsigned int buflen, int write_data)
2302 unsigned int dwords = buflen >> 1;
2305 outsw(ap->ioaddr.data_addr, buf, dwords);
2307 insw(ap->ioaddr.data_addr, buf, dwords);
2310 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2311 unsigned int buflen, int do_write)
2313 if (ap->flags & ATA_FLAG_MMIO)
2314 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2316 ata_pio_data_xfer(ap, buf, buflen, do_write);
2319 static void ata_pio_sector(struct ata_queued_cmd *qc)
2321 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2322 struct scatterlist *sg = qc->sg;
2323 struct ata_port *ap = qc->ap;
2325 unsigned int offset;
2328 if (qc->cursect == (qc->nsect - 1))
2329 ap->pio_task_state = PIO_ST_LAST;
2331 page = sg[qc->cursg].page;
2332 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2334 /* get the current page and offset */
2335 page = nth_page(page, (offset >> PAGE_SHIFT));
2336 offset %= PAGE_SIZE;
2338 buf = kmap(page) + offset;
2343 if ((qc->cursg_ofs * ATA_SECT_SIZE) == sg_dma_len(&sg[qc->cursg])) {
2348 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2350 /* do the actual data transfer */
2351 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2352 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2357 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2359 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2360 struct scatterlist *sg = qc->sg;
2361 struct ata_port *ap = qc->ap;
2364 unsigned int offset, count;
2366 if (qc->curbytes == qc->nbytes - bytes)
2367 ap->pio_task_state = PIO_ST_LAST;
2370 sg = &qc->sg[qc->cursg];
2374 offset = sg->offset + qc->cursg_ofs;
2376 /* get the current page and offset */
2377 page = nth_page(page, (offset >> PAGE_SHIFT));
2378 offset %= PAGE_SIZE;
2380 count = min(sg_dma_len(sg) - qc->cursg_ofs, bytes);
2382 /* don't cross page boundaries */
2383 count = min(count, (unsigned int)PAGE_SIZE - offset);
2385 buf = kmap(page) + offset;
2388 qc->curbytes += count;
2389 qc->cursg_ofs += count;
2391 if (qc->cursg_ofs == sg_dma_len(sg)) {
2396 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2398 /* do the actual data transfer */
2399 ata_data_xfer(ap, buf, count, do_write);
2404 if (qc->cursg_ofs < sg_dma_len(sg))
2410 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2412 struct ata_port *ap = qc->ap;
2413 struct ata_device *dev = qc->dev;
2414 unsigned int ireason, bc_lo, bc_hi, bytes;
2415 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2417 ap->ops->tf_read(ap, &qc->tf);
2418 ireason = qc->tf.nsect;
2419 bc_lo = qc->tf.lbam;
2420 bc_hi = qc->tf.lbah;
2421 bytes = (bc_hi << 8) | bc_lo;
2423 /* shall be cleared to zero, indicating xfer of data */
2424 if (ireason & (1 << 0))
2427 /* make sure transfer direction matches expected */
2428 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2429 if (do_write != i_write)
2432 __atapi_pio_bytes(qc, bytes);
2437 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2438 ap->id, dev->devno);
2439 ap->pio_task_state = PIO_ST_ERR;
2449 static void ata_pio_block(struct ata_port *ap)
2451 struct ata_queued_cmd *qc;
2455 * This is purely hueristic. This is a fast path.
2456 * Sometimes when we enter, BSY will be cleared in
2457 * a chk-status or two. If not, the drive is probably seeking
2458 * or something. Snooze for a couple msecs, then
2459 * chk-status again. If still busy, fall back to
2460 * PIO_ST_POLL state.
2462 status = ata_busy_wait(ap, ATA_BUSY, 5);
2463 if (status & ATA_BUSY) {
2465 status = ata_busy_wait(ap, ATA_BUSY, 10);
2466 if (status & ATA_BUSY) {
2467 ap->pio_task_state = PIO_ST_POLL;
2468 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2473 qc = ata_qc_from_tag(ap, ap->active_tag);
2476 if (is_atapi_taskfile(&qc->tf)) {
2477 /* no more data to transfer or unsupported ATAPI command */
2478 if ((status & ATA_DRQ) == 0) {
2479 ap->pio_task_state = PIO_ST_IDLE;
2483 ata_qc_complete(qc, status);
2487 atapi_pio_bytes(qc);
2489 /* handle BSY=0, DRQ=0 as error */
2490 if ((status & ATA_DRQ) == 0) {
2491 ap->pio_task_state = PIO_ST_ERR;
2499 static void ata_pio_error(struct ata_port *ap)
2501 struct ata_queued_cmd *qc;
2504 qc = ata_qc_from_tag(ap, ap->active_tag);
2507 drv_stat = ata_chk_status(ap);
2508 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2511 ap->pio_task_state = PIO_ST_IDLE;
2515 ata_qc_complete(qc, drv_stat | ATA_ERR);
2518 static void ata_pio_task(void *_data)
2520 struct ata_port *ap = _data;
2521 unsigned long timeout = 0;
2523 switch (ap->pio_task_state) {
2532 ata_pio_complete(ap);
2536 case PIO_ST_LAST_POLL:
2537 timeout = ata_pio_poll(ap);
2547 queue_delayed_work(ata_wq, &ap->pio_task,
2550 queue_work(ata_wq, &ap->pio_task);
2553 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2554 struct scsi_cmnd *cmd)
2556 DECLARE_COMPLETION(wait);
2557 struct ata_queued_cmd *qc;
2558 unsigned long flags;
2561 DPRINTK("ATAPI request sense\n");
2563 qc = ata_qc_new_init(ap, dev);
2566 /* FIXME: is this needed? */
2567 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2569 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2570 qc->dma_dir = DMA_FROM_DEVICE;
2572 memset(&qc->cdb, 0, sizeof(ap->cdb_len));
2573 qc->cdb[0] = REQUEST_SENSE;
2574 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2576 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2577 qc->tf.command = ATA_CMD_PACKET;
2579 qc->tf.protocol = ATA_PROT_ATAPI;
2580 qc->tf.lbam = (8 * 1024) & 0xff;
2581 qc->tf.lbah = (8 * 1024) >> 8;
2582 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2584 qc->waiting = &wait;
2585 qc->complete_fn = ata_qc_complete_noop;
2587 spin_lock_irqsave(&ap->host_set->lock, flags);
2588 rc = ata_qc_issue(qc);
2589 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2592 ata_port_disable(ap);
2594 wait_for_completion(&wait);
2600 * ata_qc_timeout - Handle timeout of queued command
2601 * @qc: Command that timed out
2603 * Some part of the kernel (currently, only the SCSI layer)
2604 * has noticed that the active command on port @ap has not
2605 * completed after a specified length of time. Handle this
2606 * condition by disabling DMA (if necessary) and completing
2607 * transactions, with error if necessary.
2609 * This also handles the case of the "lost interrupt", where
2610 * for some reason (possibly hardware bug, possibly driver bug)
2611 * an interrupt was not delivered to the driver, even though the
2612 * transaction completed successfully.
2617 static void ata_qc_timeout(struct ata_queued_cmd *qc)
2619 struct ata_port *ap = qc->ap;
2620 struct ata_device *dev = qc->dev;
2621 u8 host_stat = 0, drv_stat;
2625 /* FIXME: doesn't this conflict with timeout handling? */
2626 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
2627 struct scsi_cmnd *cmd = qc->scsicmd;
2629 if (!scsi_eh_eflags_chk(cmd, SCSI_EH_CANCEL_CMD)) {
2631 /* finish completing original command */
2632 __ata_qc_complete(qc);
2634 atapi_request_sense(ap, dev, cmd);
2636 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
2637 scsi_finish_command(cmd);
2643 /* hack alert! We cannot use the supplied completion
2644 * function from inside the ->eh_strategy_handler() thread.
2645 * libata is the only user of ->eh_strategy_handler() in
2646 * any kernel, so the default scsi_done() assumes it is
2647 * not being called from the SCSI EH.
2649 qc->scsidone = scsi_finish_command;
2651 switch (qc->tf.protocol) {
2654 case ATA_PROT_ATAPI_DMA:
2655 host_stat = ap->ops->bmdma_status(ap);
2657 /* before we do anything else, clear DMA-Start bit */
2658 ap->ops->bmdma_stop(ap);
2664 drv_stat = ata_chk_status(ap);
2666 /* ack bmdma irq events */
2667 ap->ops->irq_clear(ap);
2669 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
2670 ap->id, qc->tf.command, drv_stat, host_stat);
2672 /* complete taskfile transaction */
2673 ata_qc_complete(qc, drv_stat);
2681 * ata_eng_timeout - Handle timeout of queued command
2682 * @ap: Port on which timed-out command is active
2684 * Some part of the kernel (currently, only the SCSI layer)
2685 * has noticed that the active command on port @ap has not
2686 * completed after a specified length of time. Handle this
2687 * condition by disabling DMA (if necessary) and completing
2688 * transactions, with error if necessary.
2690 * This also handles the case of the "lost interrupt", where
2691 * for some reason (possibly hardware bug, possibly driver bug)
2692 * an interrupt was not delivered to the driver, even though the
2693 * transaction completed successfully.
2696 * Inherited from SCSI layer (none, can sleep)
2699 void ata_eng_timeout(struct ata_port *ap)
2701 struct ata_queued_cmd *qc;
2705 qc = ata_qc_from_tag(ap, ap->active_tag);
2707 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2719 * ata_qc_new - Request an available ATA command, for queueing
2720 * @ap: Port associated with device @dev
2721 * @dev: Device from whom we request an available command structure
2726 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2728 struct ata_queued_cmd *qc = NULL;
2731 for (i = 0; i < ATA_MAX_QUEUE; i++)
2732 if (!test_and_set_bit(i, &ap->qactive)) {
2733 qc = ata_qc_from_tag(ap, i);
2744 * ata_qc_new_init - Request an available ATA command, and initialize it
2745 * @ap: Port associated with device @dev
2746 * @dev: Device from whom we request an available command structure
2751 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2752 struct ata_device *dev)
2754 struct ata_queued_cmd *qc;
2756 qc = ata_qc_new(ap);
2763 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2765 qc->nbytes = qc->curbytes = 0;
2767 ata_tf_init(ap, &qc->tf, dev->devno);
2769 if (dev->flags & ATA_DFLAG_LBA48)
2770 qc->tf.flags |= ATA_TFLAG_LBA48;
2776 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
2781 static void __ata_qc_complete(struct ata_queued_cmd *qc)
2783 struct ata_port *ap = qc->ap;
2784 unsigned int tag, do_clear = 0;
2788 if (likely(ata_tag_valid(tag))) {
2789 if (tag == ap->active_tag)
2790 ap->active_tag = ATA_TAG_POISON;
2791 qc->tag = ATA_TAG_POISON;
2796 struct completion *waiting = qc->waiting;
2801 if (likely(do_clear))
2802 clear_bit(tag, &ap->qactive);
2806 * ata_qc_free - free unused ata_queued_cmd
2807 * @qc: Command to complete
2809 * Designed to free unused ata_queued_cmd object
2810 * in case something prevents using it.
2815 void ata_qc_free(struct ata_queued_cmd *qc)
2817 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2818 assert(qc->waiting == NULL); /* nothing should be waiting */
2820 __ata_qc_complete(qc);
2824 * ata_qc_complete - Complete an active ATA command
2825 * @qc: Command to complete
2826 * @drv_stat: ATA status register contents
2832 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2836 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2837 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2839 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
2842 /* call completion callback */
2843 rc = qc->complete_fn(qc, drv_stat);
2845 /* if callback indicates not to complete command (non-zero),
2846 * return immediately
2851 __ata_qc_complete(qc);
2856 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
2858 struct ata_port *ap = qc->ap;
2860 switch (qc->tf.protocol) {
2862 case ATA_PROT_ATAPI_DMA:
2865 case ATA_PROT_ATAPI:
2867 case ATA_PROT_PIO_MULT:
2868 if (ap->flags & ATA_FLAG_PIO_DMA)
2881 * ata_qc_issue - issue taskfile to device
2882 * @qc: command to issue to device
2884 * Prepare an ATA command to submission to device.
2885 * This includes mapping the data into a DMA-able
2886 * area, filling in the S/G table, and finally
2887 * writing the taskfile to hardware, starting the command.
2890 * spin_lock_irqsave(host_set lock)
2893 * Zero on success, negative on error.
2896 int ata_qc_issue(struct ata_queued_cmd *qc)
2898 struct ata_port *ap = qc->ap;
2900 if (ata_should_dma_map(qc)) {
2901 if (qc->flags & ATA_QCFLAG_SG) {
2902 if (ata_sg_setup(qc))
2904 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
2905 if (ata_sg_setup_one(qc))
2909 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2912 ap->ops->qc_prep(qc);
2914 qc->ap->active_tag = qc->tag;
2915 qc->flags |= ATA_QCFLAG_ACTIVE;
2917 return ap->ops->qc_issue(qc);
2924 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
2925 * @qc: command to issue to device
2927 * Using various libata functions and hooks, this function
2928 * starts an ATA command. ATA commands are grouped into
2929 * classes called "protocols", and issuing each type of protocol
2930 * is slightly different.
2933 * spin_lock_irqsave(host_set lock)
2936 * Zero on success, negative on error.
2939 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
2941 struct ata_port *ap = qc->ap;
2943 ata_dev_select(ap, qc->dev->devno, 1, 0);
2945 switch (qc->tf.protocol) {
2946 case ATA_PROT_NODATA:
2947 ata_tf_to_host_nolock(ap, &qc->tf);
2951 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2952 ap->ops->bmdma_setup(qc); /* set up bmdma */
2953 ap->ops->bmdma_start(qc); /* initiate bmdma */
2956 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
2957 ata_qc_set_polling(qc);
2958 ata_tf_to_host_nolock(ap, &qc->tf);
2959 ap->pio_task_state = PIO_ST;
2960 queue_work(ata_wq, &ap->pio_task);
2963 case ATA_PROT_ATAPI:
2964 ata_qc_set_polling(qc);
2965 ata_tf_to_host_nolock(ap, &qc->tf);
2966 queue_work(ata_wq, &ap->packet_task);
2969 case ATA_PROT_ATAPI_NODATA:
2970 ata_tf_to_host_nolock(ap, &qc->tf);
2971 queue_work(ata_wq, &ap->packet_task);
2974 case ATA_PROT_ATAPI_DMA:
2975 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2976 ap->ops->bmdma_setup(qc); /* set up bmdma */
2977 queue_work(ata_wq, &ap->packet_task);
2989 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2990 * @qc: Info associated with this ATA transaction.
2993 * spin_lock_irqsave(host_set lock)
2996 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
2998 struct ata_port *ap = qc->ap;
2999 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3001 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3003 /* load PRD table addr. */
3004 mb(); /* make sure PRD table writes are visible to controller */
3005 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3007 /* specify data direction, triple-check start bit is clear */
3008 dmactl = readb(mmio + ATA_DMA_CMD);
3009 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3011 dmactl |= ATA_DMA_WR;
3012 writeb(dmactl, mmio + ATA_DMA_CMD);
3014 /* issue r/w command */
3015 ap->ops->exec_command(ap, &qc->tf);
3019 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3020 * @qc: Info associated with this ATA transaction.
3023 * spin_lock_irqsave(host_set lock)
3026 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3028 struct ata_port *ap = qc->ap;
3029 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3032 /* start host DMA transaction */
3033 dmactl = readb(mmio + ATA_DMA_CMD);
3034 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3036 /* Strictly, one may wish to issue a readb() here, to
3037 * flush the mmio write. However, control also passes
3038 * to the hardware at this point, and it will interrupt
3039 * us when we are to resume control. So, in effect,
3040 * we don't care when the mmio write flushes.
3041 * Further, a read of the DMA status register _immediately_
3042 * following the write may not be what certain flaky hardware
3043 * is expected, so I think it is best to not add a readb()
3044 * without first all the MMIO ATA cards/mobos.
3045 * Or maybe I'm just being paranoid.
3050 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3051 * @qc: Info associated with this ATA transaction.
3054 * spin_lock_irqsave(host_set lock)
3057 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3059 struct ata_port *ap = qc->ap;
3060 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3063 /* load PRD table addr. */
3064 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3066 /* specify data direction, triple-check start bit is clear */
3067 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3068 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3070 dmactl |= ATA_DMA_WR;
3071 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3073 /* issue r/w command */
3074 ap->ops->exec_command(ap, &qc->tf);
3078 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3079 * @qc: Info associated with this ATA transaction.
3082 * spin_lock_irqsave(host_set lock)
3085 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3087 struct ata_port *ap = qc->ap;
3090 /* start host DMA transaction */
3091 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3092 outb(dmactl | ATA_DMA_START,
3093 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3096 void ata_bmdma_start(struct ata_queued_cmd *qc)
3098 if (qc->ap->flags & ATA_FLAG_MMIO)
3099 ata_bmdma_start_mmio(qc);
3101 ata_bmdma_start_pio(qc);
3104 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3106 if (qc->ap->flags & ATA_FLAG_MMIO)
3107 ata_bmdma_setup_mmio(qc);
3109 ata_bmdma_setup_pio(qc);
3112 void ata_bmdma_irq_clear(struct ata_port *ap)
3114 if (ap->flags & ATA_FLAG_MMIO) {
3115 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3116 writeb(readb(mmio), mmio);
3118 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3119 outb(inb(addr), addr);
3124 u8 ata_bmdma_status(struct ata_port *ap)
3127 if (ap->flags & ATA_FLAG_MMIO) {
3128 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3129 host_stat = readb(mmio + ATA_DMA_STATUS);
3131 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3135 void ata_bmdma_stop(struct ata_port *ap)
3137 if (ap->flags & ATA_FLAG_MMIO) {
3138 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3140 /* clear start/stop bit */
3141 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3142 mmio + ATA_DMA_CMD);
3144 /* clear start/stop bit */
3145 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3146 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3149 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3150 ata_altstatus(ap); /* dummy read */
3154 * ata_host_intr - Handle host interrupt for given (port, task)
3155 * @ap: Port on which interrupt arrived (possibly...)
3156 * @qc: Taskfile currently active in engine
3158 * Handle host interrupt for given queued command. Currently,
3159 * only DMA interrupts are handled. All other commands are
3160 * handled via polling with interrupts disabled (nIEN bit).
3163 * spin_lock_irqsave(host_set lock)
3166 * One if interrupt was handled, zero if not (shared irq).
3169 inline unsigned int ata_host_intr (struct ata_port *ap,
3170 struct ata_queued_cmd *qc)
3172 u8 status, host_stat;
3174 switch (qc->tf.protocol) {
3177 case ATA_PROT_ATAPI_DMA:
3178 case ATA_PROT_ATAPI:
3179 /* check status of DMA engine */
3180 host_stat = ap->ops->bmdma_status(ap);
3181 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3183 /* if it's not our irq... */
3184 if (!(host_stat & ATA_DMA_INTR))
3187 /* before we do anything else, clear DMA-Start bit */
3188 ap->ops->bmdma_stop(ap);
3192 case ATA_PROT_ATAPI_NODATA:
3193 case ATA_PROT_NODATA:
3194 /* check altstatus */
3195 status = ata_altstatus(ap);
3196 if (status & ATA_BUSY)
3199 /* check main status, clearing INTRQ */
3200 status = ata_chk_status(ap);
3201 if (unlikely(status & ATA_BUSY))
3203 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3204 ap->id, qc->tf.protocol, status);
3206 /* ack bmdma irq events */
3207 ap->ops->irq_clear(ap);
3209 /* complete taskfile transaction */
3210 ata_qc_complete(qc, status);
3217 return 1; /* irq handled */
3220 ap->stats.idle_irq++;
3223 if ((ap->stats.idle_irq % 1000) == 0) {
3225 ata_irq_ack(ap, 0); /* debug trap */
3226 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3229 return 0; /* irq not handled */
3233 * ata_interrupt - Default ATA host interrupt handler
3235 * @dev_instance: pointer to our host information structure
3244 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3246 struct ata_host_set *host_set = dev_instance;
3248 unsigned int handled = 0;
3249 unsigned long flags;
3251 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3252 spin_lock_irqsave(&host_set->lock, flags);
3254 for (i = 0; i < host_set->n_ports; i++) {
3255 struct ata_port *ap;
3257 ap = host_set->ports[i];
3258 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
3259 struct ata_queued_cmd *qc;
3261 qc = ata_qc_from_tag(ap, ap->active_tag);
3262 if (qc && (!(qc->tf.ctl & ATA_NIEN)))
3263 handled |= ata_host_intr(ap, qc);
3267 spin_unlock_irqrestore(&host_set->lock, flags);
3269 return IRQ_RETVAL(handled);
3273 * atapi_packet_task - Write CDB bytes to hardware
3274 * @_data: Port to which ATAPI device is attached.
3276 * When device has indicated its readiness to accept
3277 * a CDB, this function is called. Send the CDB.
3278 * If DMA is to be performed, exit immediately.
3279 * Otherwise, we are in polling mode, so poll
3280 * status under operation succeeds or fails.
3283 * Kernel thread context (may sleep)
3286 static void atapi_packet_task(void *_data)
3288 struct ata_port *ap = _data;
3289 struct ata_queued_cmd *qc;
3292 qc = ata_qc_from_tag(ap, ap->active_tag);
3294 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3296 /* sleep-wait for BSY to clear */
3297 DPRINTK("busy wait\n");
3298 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3301 /* make sure DRQ is set */
3302 status = ata_chk_status(ap);
3303 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3307 DPRINTK("send cdb\n");
3308 assert(ap->cdb_len >= 12);
3309 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3311 /* if we are DMA'ing, irq handler takes over from here */
3312 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3313 ap->ops->bmdma_start(qc); /* initiate bmdma */
3315 /* non-data commands are also handled via irq */
3316 else if (qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3320 /* PIO commands are handled by polling */
3322 ap->pio_task_state = PIO_ST;
3323 queue_work(ata_wq, &ap->pio_task);
3329 ata_qc_complete(qc, ATA_ERR);
3332 int ata_port_start (struct ata_port *ap)
3334 struct device *dev = ap->host_set->dev;
3336 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3340 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3345 void ata_port_stop (struct ata_port *ap)
3347 struct device *dev = ap->host_set->dev;
3349 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3353 * ata_host_remove - Unregister SCSI host structure with upper layers
3354 * @ap: Port to unregister
3355 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3360 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3362 struct Scsi_Host *sh = ap->host;
3367 scsi_remove_host(sh);
3369 ap->ops->port_stop(ap);
3373 * ata_host_init - Initialize an ata_port structure
3374 * @ap: Structure to initialize
3375 * @host: associated SCSI mid-layer structure
3376 * @host_set: Collection of hosts to which @ap belongs
3377 * @ent: Probe information provided by low-level driver
3378 * @port_no: Port number associated with this ata_port
3384 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3385 struct ata_host_set *host_set,
3386 struct ata_probe_ent *ent, unsigned int port_no)
3392 host->max_channel = 1;
3393 host->unique_id = ata_unique_id++;
3394 host->max_cmd_len = 12;
3395 scsi_set_device(host, ent->dev);
3396 scsi_assign_lock(host, &host_set->lock);
3398 ap->flags = ATA_FLAG_PORT_DISABLED;
3399 ap->id = host->unique_id;
3401 ap->ctl = ATA_DEVCTL_OBS;
3402 ap->host_set = host_set;
3403 ap->port_no = port_no;
3405 ent->legacy_mode ? ent->hard_port_no : port_no;
3406 ap->pio_mask = ent->pio_mask;
3407 ap->mwdma_mask = ent->mwdma_mask;
3408 ap->udma_mask = ent->udma_mask;
3409 ap->flags |= ent->host_flags;
3410 ap->ops = ent->port_ops;
3411 ap->cbl = ATA_CBL_NONE;
3412 ap->active_tag = ATA_TAG_POISON;
3413 ap->last_ctl = 0xFF;
3415 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
3416 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
3418 for (i = 0; i < ATA_MAX_DEVICES; i++)
3419 ap->device[i].devno = i;
3422 ap->stats.unhandled_irq = 1;
3423 ap->stats.idle_irq = 1;
3426 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
3430 * ata_host_add - Attach low-level ATA driver to system
3431 * @ent: Information provided by low-level driver
3432 * @host_set: Collections of ports to which we add
3433 * @port_no: Port number associated with this host
3441 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
3442 struct ata_host_set *host_set,
3443 unsigned int port_no)
3445 struct Scsi_Host *host;
3446 struct ata_port *ap;
3450 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
3454 ap = (struct ata_port *) &host->hostdata[0];
3456 ata_host_init(ap, host, host_set, ent, port_no);
3458 rc = ap->ops->port_start(ap);
3465 scsi_host_put(host);
3479 int ata_device_add(struct ata_probe_ent *ent)
3481 unsigned int count = 0, i;
3482 struct device *dev = ent->dev;
3483 struct ata_host_set *host_set;
3486 /* alloc a container for our list of ATA ports (buses) */
3487 host_set = kmalloc(sizeof(struct ata_host_set) +
3488 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
3491 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
3492 spin_lock_init(&host_set->lock);
3494 host_set->dev = dev;
3495 host_set->n_ports = ent->n_ports;
3496 host_set->irq = ent->irq;
3497 host_set->mmio_base = ent->mmio_base;
3498 host_set->private_data = ent->private_data;
3499 host_set->ops = ent->port_ops;
3501 /* register each port bound to this device */
3502 for (i = 0; i < ent->n_ports; i++) {
3503 struct ata_port *ap;
3504 unsigned long xfer_mode_mask;
3506 ap = ata_host_add(ent, host_set, i);
3510 host_set->ports[i] = ap;
3511 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
3512 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
3513 (ap->pio_mask << ATA_SHIFT_PIO);
3515 /* print per-port info to dmesg */
3516 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3517 "bmdma 0x%lX irq %lu\n",
3519 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
3520 ata_mode_string(xfer_mode_mask),
3521 ap->ioaddr.cmd_addr,
3522 ap->ioaddr.ctl_addr,
3523 ap->ioaddr.bmdma_addr,
3527 host_set->ops->irq_clear(ap);
3536 /* obtain irq, that is shared between channels */
3537 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
3538 DRV_NAME, host_set))
3541 /* perform each probe synchronously */
3542 DPRINTK("probe begin\n");
3543 for (i = 0; i < count; i++) {
3544 struct ata_port *ap;
3547 ap = host_set->ports[i];
3549 DPRINTK("ata%u: probe begin\n", ap->id);
3550 rc = ata_bus_probe(ap);
3551 DPRINTK("ata%u: probe end\n", ap->id);
3554 /* FIXME: do something useful here?
3555 * Current libata behavior will
3556 * tear down everything when
3557 * the module is removed
3558 * or the h/w is unplugged.
3562 rc = scsi_add_host(ap->host, dev);
3564 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
3566 /* FIXME: do something useful here */
3567 /* FIXME: handle unconditional calls to
3568 * scsi_scan_host and ata_host_remove, below,
3574 /* probes are done, now scan each port's disk(s) */
3575 DPRINTK("probe begin\n");
3576 for (i = 0; i < count; i++) {
3577 struct ata_port *ap = host_set->ports[i];
3579 scsi_scan_host(ap->host);
3582 dev_set_drvdata(dev, host_set);
3584 VPRINTK("EXIT, returning %u\n", ent->n_ports);
3585 return ent->n_ports; /* success */
3588 for (i = 0; i < count; i++) {
3589 ata_host_remove(host_set->ports[i], 1);
3590 scsi_host_put(host_set->ports[i]->host);
3593 VPRINTK("EXIT, returning 0\n");
3598 * ata_scsi_release - SCSI layer callback hook for host unload
3599 * @host: libata host to be unloaded
3601 * Performs all duties necessary to shut down a libata port...
3602 * Kill port kthread, disable port, and release resources.
3605 * Inherited from SCSI layer.
3611 int ata_scsi_release(struct Scsi_Host *host)
3613 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3617 ap->ops->port_disable(ap);
3618 ata_host_remove(ap, 0);
3625 * ata_std_ports - initialize ioaddr with standard port offsets.
3626 * @ioaddr: IO address structure to be initialized
3628 void ata_std_ports(struct ata_ioports *ioaddr)
3630 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3631 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3632 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3633 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3634 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3635 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3636 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3637 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3638 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3639 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3642 static struct ata_probe_ent *
3643 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
3645 struct ata_probe_ent *probe_ent;
3647 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3649 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
3650 kobject_name(&(dev->kobj)));
3654 memset(probe_ent, 0, sizeof(*probe_ent));
3656 INIT_LIST_HEAD(&probe_ent->node);
3657 probe_ent->dev = dev;
3659 probe_ent->sht = port->sht;
3660 probe_ent->host_flags = port->host_flags;
3661 probe_ent->pio_mask = port->pio_mask;
3662 probe_ent->mwdma_mask = port->mwdma_mask;
3663 probe_ent->udma_mask = port->udma_mask;
3664 probe_ent->port_ops = port->port_ops;
3670 struct ata_probe_ent *
3671 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
3673 struct ata_probe_ent *probe_ent =
3674 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
3678 probe_ent->n_ports = 2;
3679 probe_ent->irq = pdev->irq;
3680 probe_ent->irq_flags = SA_SHIRQ;
3682 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3683 probe_ent->port[0].altstatus_addr =
3684 probe_ent->port[0].ctl_addr =
3685 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3686 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3688 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3689 probe_ent->port[1].altstatus_addr =
3690 probe_ent->port[1].ctl_addr =
3691 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3692 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3694 ata_std_ports(&probe_ent->port[0]);
3695 ata_std_ports(&probe_ent->port[1]);
3700 static struct ata_probe_ent *
3701 ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
3702 struct ata_probe_ent **ppe2)
3704 struct ata_probe_ent *probe_ent, *probe_ent2;
3706 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
3709 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
3715 probe_ent->n_ports = 1;
3716 probe_ent->irq = 14;
3718 probe_ent->hard_port_no = 0;
3719 probe_ent->legacy_mode = 1;
3721 probe_ent2->n_ports = 1;
3722 probe_ent2->irq = 15;
3724 probe_ent2->hard_port_no = 1;
3725 probe_ent2->legacy_mode = 1;
3727 probe_ent->port[0].cmd_addr = 0x1f0;
3728 probe_ent->port[0].altstatus_addr =
3729 probe_ent->port[0].ctl_addr = 0x3f6;
3730 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3732 probe_ent2->port[0].cmd_addr = 0x170;
3733 probe_ent2->port[0].altstatus_addr =
3734 probe_ent2->port[0].ctl_addr = 0x376;
3735 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3737 ata_std_ports(&probe_ent->port[0]);
3738 ata_std_ports(&probe_ent2->port[0]);
3745 * ata_pci_init_one - Initialize/register PCI IDE host controller
3746 * @pdev: Controller to be initialized
3747 * @port_info: Information from low-level host driver
3748 * @n_ports: Number of ports attached to host controller
3751 * Inherited from PCI layer (may sleep).
3757 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3758 unsigned int n_ports)
3760 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3761 struct ata_port_info *port[2];
3763 unsigned int legacy_mode = 0;
3764 int disable_dev_on_err = 1;
3769 port[0] = port_info[0];
3771 port[1] = port_info[1];
3775 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
3776 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
3777 /* TODO: support transitioning to native mode? */
3778 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3779 mask = (1 << 2) | (1 << 0);
3780 if ((tmp8 & mask) != mask)
3781 legacy_mode = (1 << 3);
3785 if ((!legacy_mode) && (n_ports > 1)) {
3786 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3790 rc = pci_enable_device(pdev);
3794 rc = pci_request_regions(pdev, DRV_NAME);
3796 disable_dev_on_err = 0;
3801 if (!request_region(0x1f0, 8, "libata")) {
3802 struct resource *conflict, res;
3804 res.end = 0x1f0 + 8 - 1;
3805 conflict = ____request_resource(&ioport_resource, &res);
3806 if (!strcmp(conflict->name, "libata"))
3807 legacy_mode |= (1 << 0);
3809 disable_dev_on_err = 0;
3810 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3813 legacy_mode |= (1 << 0);
3815 if (!request_region(0x170, 8, "libata")) {
3816 struct resource *conflict, res;
3818 res.end = 0x170 + 8 - 1;
3819 conflict = ____request_resource(&ioport_resource, &res);
3820 if (!strcmp(conflict->name, "libata"))
3821 legacy_mode |= (1 << 1);
3823 disable_dev_on_err = 0;
3824 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3827 legacy_mode |= (1 << 1);
3830 /* we have legacy mode, but all ports are unavailable */
3831 if (legacy_mode == (1 << 3)) {
3833 goto err_out_regions;
3836 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3838 goto err_out_regions;
3839 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3841 goto err_out_regions;
3844 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
3846 probe_ent = ata_pci_init_native_mode(pdev, port);
3849 goto err_out_regions;
3852 pci_set_master(pdev);
3854 /* FIXME: check ata_device_add return */
3856 if (legacy_mode & (1 << 0))
3857 ata_device_add(probe_ent);
3858 if (legacy_mode & (1 << 1))
3859 ata_device_add(probe_ent2);
3861 ata_device_add(probe_ent);
3869 if (legacy_mode & (1 << 0))
3870 release_region(0x1f0, 8);
3871 if (legacy_mode & (1 << 1))
3872 release_region(0x170, 8);
3873 pci_release_regions(pdev);
3875 if (disable_dev_on_err)
3876 pci_disable_device(pdev);
3881 * ata_pci_remove_one - PCI layer callback for device removal
3882 * @pdev: PCI device that was removed
3884 * PCI layer indicates to libata via this hook that
3885 * hot-unplug or module unload event has occured.
3886 * Handle this by unregistering all objects associated
3887 * with this PCI device. Free those objects. Then finally
3888 * release PCI resources and disable device.
3891 * Inherited from PCI layer (may sleep).
3894 void ata_pci_remove_one (struct pci_dev *pdev)
3896 struct device *dev = pci_dev_to_dev(pdev);
3897 struct ata_host_set *host_set = dev_get_drvdata(dev);
3898 struct ata_port *ap;
3901 for (i = 0; i < host_set->n_ports; i++) {
3902 ap = host_set->ports[i];
3904 scsi_remove_host(ap->host);
3907 free_irq(host_set->irq, host_set);
3908 if (host_set->ops->host_stop)
3909 host_set->ops->host_stop(host_set);
3910 if (host_set->mmio_base)
3911 iounmap(host_set->mmio_base);
3913 for (i = 0; i < host_set->n_ports; i++) {
3914 ap = host_set->ports[i];
3916 ata_scsi_release(ap->host);
3918 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
3919 struct ata_ioports *ioaddr = &ap->ioaddr;
3921 if (ioaddr->cmd_addr == 0x1f0)
3922 release_region(0x1f0, 8);
3923 else if (ioaddr->cmd_addr == 0x170)
3924 release_region(0x170, 8);
3927 scsi_host_put(ap->host);
3932 pci_release_regions(pdev);
3933 pci_disable_device(pdev);
3934 dev_set_drvdata(dev, NULL);
3937 /* move to PCI subsystem */
3938 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
3940 unsigned long tmp = 0;
3942 switch (bits->width) {
3945 pci_read_config_byte(pdev, bits->reg, &tmp8);
3951 pci_read_config_word(pdev, bits->reg, &tmp16);
3957 pci_read_config_dword(pdev, bits->reg, &tmp32);
3968 return (tmp == bits->val) ? 1 : 0;
3970 #endif /* CONFIG_PCI */
3982 static int __init ata_init(void)
3984 ata_wq = create_workqueue("ata");
3988 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
3992 static void __exit ata_exit(void)
3994 destroy_workqueue(ata_wq);
3997 module_init(ata_init);
3998 module_exit(ata_exit);
4001 * libata is essentially a library of internal helper functions for
4002 * low-level ATA host controller drivers. As such, the API/ABI is
4003 * likely to change as new drivers are added and updated.
4004 * Do not depend on ABI/API stability.
4007 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4008 EXPORT_SYMBOL_GPL(ata_std_ports);
4009 EXPORT_SYMBOL_GPL(ata_device_add);
4010 EXPORT_SYMBOL_GPL(ata_sg_init);
4011 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4012 EXPORT_SYMBOL_GPL(ata_qc_complete);
4013 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4014 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4015 EXPORT_SYMBOL_GPL(ata_tf_load);
4016 EXPORT_SYMBOL_GPL(ata_tf_read);
4017 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4018 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4019 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4020 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4021 EXPORT_SYMBOL_GPL(ata_check_status);
4022 EXPORT_SYMBOL_GPL(ata_altstatus);
4023 EXPORT_SYMBOL_GPL(ata_chk_err);
4024 EXPORT_SYMBOL_GPL(ata_exec_command);
4025 EXPORT_SYMBOL_GPL(ata_port_start);
4026 EXPORT_SYMBOL_GPL(ata_port_stop);
4027 EXPORT_SYMBOL_GPL(ata_interrupt);
4028 EXPORT_SYMBOL_GPL(ata_qc_prep);
4029 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4030 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4031 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4032 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4033 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4034 EXPORT_SYMBOL_GPL(ata_port_probe);
4035 EXPORT_SYMBOL_GPL(sata_phy_reset);
4036 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4037 EXPORT_SYMBOL_GPL(ata_bus_reset);
4038 EXPORT_SYMBOL_GPL(ata_port_disable);
4039 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4040 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4041 EXPORT_SYMBOL_GPL(ata_scsi_error);
4042 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4043 EXPORT_SYMBOL_GPL(ata_scsi_release);
4044 EXPORT_SYMBOL_GPL(ata_host_intr);
4045 EXPORT_SYMBOL_GPL(ata_dev_classify);
4046 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4047 EXPORT_SYMBOL_GPL(ata_dev_config);
4048 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4051 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4052 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4053 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4054 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4055 #endif /* CONFIG_PCI */