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>
31 #include <linux/highmem.h>
32 #include <linux/spinlock.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/timer.h>
36 #include <linux/interrupt.h>
37 #include <linux/completion.h>
38 #include <linux/suspend.h>
39 #include <linux/workqueue.h>
40 #include <scsi/scsi.h>
42 #include <scsi/scsi_host.h>
43 #include <linux/libata.h>
45 #include <asm/semaphore.h>
49 static unsigned int ata_busy_sleep (struct ata_port *ap,
50 unsigned long tmout_pat,
52 static void __ata_dev_select (struct ata_port *ap, unsigned int device);
53 static void ata_host_set_pio(struct ata_port *ap);
54 static void ata_host_set_udma(struct ata_port *ap);
55 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device);
56 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device);
57 static void ata_set_mode(struct ata_port *ap);
59 static unsigned int ata_unique_id = 1;
60 static struct workqueue_struct *ata_wq;
62 MODULE_AUTHOR("Jeff Garzik");
63 MODULE_DESCRIPTION("Library module for ATA devices");
64 MODULE_LICENSE("GPL");
67 * ata_tf_load_pio - send taskfile registers to host controller
68 * @ap: Port to which output is sent
69 * @tf: ATA taskfile register set
71 * Outputs ATA taskfile to standard ATA host controller using PIO.
74 * Inherited from caller.
77 void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
79 struct ata_ioports *ioaddr = &ap->ioaddr;
80 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
82 if (tf->ctl != ap->last_ctl) {
83 outb(tf->ctl, ioaddr->ctl_addr);
84 ap->last_ctl = tf->ctl;
88 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
89 outb(tf->hob_feature, ioaddr->feature_addr);
90 outb(tf->hob_nsect, ioaddr->nsect_addr);
91 outb(tf->hob_lbal, ioaddr->lbal_addr);
92 outb(tf->hob_lbam, ioaddr->lbam_addr);
93 outb(tf->hob_lbah, ioaddr->lbah_addr);
94 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
103 outb(tf->feature, ioaddr->feature_addr);
104 outb(tf->nsect, ioaddr->nsect_addr);
105 outb(tf->lbal, ioaddr->lbal_addr);
106 outb(tf->lbam, ioaddr->lbam_addr);
107 outb(tf->lbah, ioaddr->lbah_addr);
108 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
116 if (tf->flags & ATA_TFLAG_DEVICE) {
117 outb(tf->device, ioaddr->device_addr);
118 VPRINTK("device 0x%X\n", tf->device);
125 * ata_tf_load_mmio - send taskfile registers to host controller
126 * @ap: Port to which output is sent
127 * @tf: ATA taskfile register set
129 * Outputs ATA taskfile to standard ATA host controller using MMIO.
132 * Inherited from caller.
135 void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
137 struct ata_ioports *ioaddr = &ap->ioaddr;
138 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
140 if (tf->ctl != ap->last_ctl) {
141 writeb(tf->ctl, ap->ioaddr.ctl_addr);
142 ap->last_ctl = tf->ctl;
146 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
147 writeb(tf->hob_feature, (void *) ioaddr->feature_addr);
148 writeb(tf->hob_nsect, (void *) ioaddr->nsect_addr);
149 writeb(tf->hob_lbal, (void *) ioaddr->lbal_addr);
150 writeb(tf->hob_lbam, (void *) ioaddr->lbam_addr);
151 writeb(tf->hob_lbah, (void *) ioaddr->lbah_addr);
152 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
161 writeb(tf->feature, (void *) ioaddr->feature_addr);
162 writeb(tf->nsect, (void *) ioaddr->nsect_addr);
163 writeb(tf->lbal, (void *) ioaddr->lbal_addr);
164 writeb(tf->lbam, (void *) ioaddr->lbam_addr);
165 writeb(tf->lbah, (void *) ioaddr->lbah_addr);
166 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
174 if (tf->flags & ATA_TFLAG_DEVICE) {
175 writeb(tf->device, (void *) ioaddr->device_addr);
176 VPRINTK("device 0x%X\n", tf->device);
183 * ata_exec_command_pio - issue ATA command to host controller
184 * @ap: port to which command is being issued
185 * @tf: ATA taskfile register set
187 * Issues PIO write to ATA command register, with proper
188 * synchronization with interrupt handler / other threads.
191 * spin_lock_irqsave(host_set lock)
194 void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
196 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
198 outb(tf->command, ap->ioaddr.command_addr);
204 * ata_exec_command_mmio - issue ATA command to host controller
205 * @ap: port to which command is being issued
206 * @tf: ATA taskfile register set
208 * Issues MMIO write to ATA command register, with proper
209 * synchronization with interrupt handler / other threads.
212 * spin_lock_irqsave(host_set lock)
215 void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
217 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
219 writeb(tf->command, (void *) ap->ioaddr.command_addr);
224 * ata_exec - issue ATA command to host controller
225 * @ap: port to which command is being issued
226 * @tf: ATA taskfile register set
228 * Issues PIO write to ATA command register, with proper
229 * synchronization with interrupt handler / other threads.
232 * Obtains host_set lock.
235 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
239 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
240 spin_lock_irqsave(&ap->host_set->lock, flags);
241 ap->ops->exec_command(ap, tf);
242 spin_unlock_irqrestore(&ap->host_set->lock, flags);
246 * ata_tf_to_host - issue ATA taskfile to host controller
247 * @ap: port to which command is being issued
248 * @tf: ATA taskfile register set
250 * Issues ATA taskfile register set to ATA host controller,
251 * via PIO, with proper synchronization with interrupt handler and
255 * Obtains host_set lock.
258 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
260 ap->ops->tf_load(ap, tf);
266 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
267 * @ap: port to which command is being issued
268 * @tf: ATA taskfile register set
270 * Issues ATA taskfile register set to ATA host controller,
271 * via PIO, with proper synchronization with interrupt handler and
275 * spin_lock_irqsave(host_set lock)
278 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
280 ap->ops->tf_load(ap, tf);
281 ap->ops->exec_command(ap, tf);
285 * ata_tf_read_pio - input device's ATA taskfile shadow registers
286 * @ap: Port from which input is read
287 * @tf: ATA taskfile register set for storing input
289 * Reads ATA taskfile registers for currently-selected device
293 * Inherited from caller.
296 void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
298 struct ata_ioports *ioaddr = &ap->ioaddr;
300 tf->nsect = inb(ioaddr->nsect_addr);
301 tf->lbal = inb(ioaddr->lbal_addr);
302 tf->lbam = inb(ioaddr->lbam_addr);
303 tf->lbah = inb(ioaddr->lbah_addr);
304 tf->device = inb(ioaddr->device_addr);
306 if (tf->flags & ATA_TFLAG_LBA48) {
307 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
308 tf->hob_feature = inb(ioaddr->error_addr);
309 tf->hob_nsect = inb(ioaddr->nsect_addr);
310 tf->hob_lbal = inb(ioaddr->lbal_addr);
311 tf->hob_lbam = inb(ioaddr->lbam_addr);
312 tf->hob_lbah = inb(ioaddr->lbah_addr);
317 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
318 * @ap: Port from which input is read
319 * @tf: ATA taskfile register set for storing input
321 * Reads ATA taskfile registers for currently-selected device
325 * Inherited from caller.
328 void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
330 struct ata_ioports *ioaddr = &ap->ioaddr;
332 tf->nsect = readb((void *)ioaddr->nsect_addr);
333 tf->lbal = readb((void *)ioaddr->lbal_addr);
334 tf->lbam = readb((void *)ioaddr->lbam_addr);
335 tf->lbah = readb((void *)ioaddr->lbah_addr);
336 tf->device = readb((void *)ioaddr->device_addr);
338 if (tf->flags & ATA_TFLAG_LBA48) {
339 writeb(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr);
340 tf->hob_feature = readb((void *)ioaddr->error_addr);
341 tf->hob_nsect = readb((void *)ioaddr->nsect_addr);
342 tf->hob_lbal = readb((void *)ioaddr->lbal_addr);
343 tf->hob_lbam = readb((void *)ioaddr->lbam_addr);
344 tf->hob_lbah = readb((void *)ioaddr->lbah_addr);
349 * ata_check_status_pio - Read device status reg & clear interrupt
350 * @ap: port where the device is
352 * Reads ATA taskfile status register for currently-selected device
353 * via PIO and return it's value. This also clears pending interrupts
357 * Inherited from caller.
359 u8 ata_check_status_pio(struct ata_port *ap)
361 return inb(ap->ioaddr.status_addr);
365 * ata_check_status_mmio - Read device status reg & clear interrupt
366 * @ap: port where the device is
368 * Reads ATA taskfile status register for currently-selected device
369 * via MMIO and return it's value. This also clears pending interrupts
373 * Inherited from caller.
375 u8 ata_check_status_mmio(struct ata_port *ap)
377 return readb((void *) ap->ioaddr.status_addr);
381 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
382 * @tf: Taskfile to convert
383 * @fis: Buffer into which data will output
384 * @pmp: Port multiplier port
386 * Converts a standard ATA taskfile to a Serial ATA
387 * FIS structure (Register - Host to Device).
390 * Inherited from caller.
393 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
395 fis[0] = 0x27; /* Register - Host to Device FIS */
396 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
397 bit 7 indicates Command FIS */
398 fis[2] = tf->command;
399 fis[3] = tf->feature;
406 fis[8] = tf->hob_lbal;
407 fis[9] = tf->hob_lbam;
408 fis[10] = tf->hob_lbah;
409 fis[11] = tf->hob_feature;
412 fis[13] = tf->hob_nsect;
423 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
424 * @fis: Buffer from which data will be input
425 * @tf: Taskfile to output
427 * Converts a standard ATA taskfile to a Serial ATA
428 * FIS structure (Register - Host to Device).
431 * Inherited from caller.
434 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
436 tf->command = fis[2]; /* status */
437 tf->feature = fis[3]; /* error */
444 tf->hob_lbal = fis[8];
445 tf->hob_lbam = fis[9];
446 tf->hob_lbah = fis[10];
449 tf->hob_nsect = fis[13];
453 * ata_prot_to_cmd - determine which read/write opcodes to use
454 * @protocol: ATA_PROT_xxx taskfile protocol
455 * @lba48: true is lba48 is present
457 * Given necessary input, determine which read/write commands
458 * to use to transfer data.
463 static int ata_prot_to_cmd(int protocol, int lba48)
465 int rcmd = 0, wcmd = 0;
470 rcmd = ATA_CMD_PIO_READ_EXT;
471 wcmd = ATA_CMD_PIO_WRITE_EXT;
473 rcmd = ATA_CMD_PIO_READ;
474 wcmd = ATA_CMD_PIO_WRITE;
480 rcmd = ATA_CMD_READ_EXT;
481 wcmd = ATA_CMD_WRITE_EXT;
484 wcmd = ATA_CMD_WRITE;
492 return rcmd | (wcmd << 8);
496 * ata_dev_set_protocol - set taskfile protocol and r/w commands
497 * @dev: device to examine and configure
499 * Examine the device configuration, after we have
500 * read the identify-device page and configured the
501 * data transfer mode. Set internal state related to
502 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
503 * and calculate the proper read/write commands to use.
508 static void ata_dev_set_protocol(struct ata_device *dev)
510 int pio = (dev->flags & ATA_DFLAG_PIO);
511 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
515 proto = dev->xfer_protocol = ATA_PROT_PIO;
517 proto = dev->xfer_protocol = ATA_PROT_DMA;
519 cmd = ata_prot_to_cmd(proto, lba48);
523 dev->read_cmd = cmd & 0xff;
524 dev->write_cmd = (cmd >> 8) & 0xff;
527 static const char * udma_str[] = {
539 * ata_udma_string - convert UDMA bit offset to string
540 * @udma_mask: mask of bits supported; only highest bit counts.
542 * Determine string which represents the highest speed
543 * (highest bit in @udma_mask).
549 * Constant C string representing highest speed listed in
550 * @udma_mask, or the constant C string "<n/a>".
553 static const char *ata_udma_string(unsigned int udma_mask)
557 for (i = 7; i >= 0; i--) {
558 if (udma_mask & (1 << i))
566 * ata_pio_devchk - PATA device presence detection
567 * @ap: ATA channel to examine
568 * @device: Device to examine (starting at zero)
570 * This technique was originally described in
571 * Hale Landis's ATADRVR (www.ata-atapi.com), and
572 * later found its way into the ATA/ATAPI spec.
574 * Write a pattern to the ATA shadow registers,
575 * and if a device is present, it will respond by
576 * correctly storing and echoing back the
577 * ATA shadow register contents.
583 static unsigned int ata_pio_devchk(struct ata_port *ap,
586 struct ata_ioports *ioaddr = &ap->ioaddr;
589 __ata_dev_select(ap, device);
591 outb(0x55, ioaddr->nsect_addr);
592 outb(0xaa, ioaddr->lbal_addr);
594 outb(0xaa, ioaddr->nsect_addr);
595 outb(0x55, ioaddr->lbal_addr);
597 outb(0x55, ioaddr->nsect_addr);
598 outb(0xaa, ioaddr->lbal_addr);
600 nsect = inb(ioaddr->nsect_addr);
601 lbal = inb(ioaddr->lbal_addr);
603 if ((nsect == 0x55) && (lbal == 0xaa))
604 return 1; /* we found a device */
606 return 0; /* nothing found */
610 * ata_mmio_devchk - PATA device presence detection
611 * @ap: ATA channel to examine
612 * @device: Device to examine (starting at zero)
614 * This technique was originally described in
615 * Hale Landis's ATADRVR (www.ata-atapi.com), and
616 * later found its way into the ATA/ATAPI spec.
618 * Write a pattern to the ATA shadow registers,
619 * and if a device is present, it will respond by
620 * correctly storing and echoing back the
621 * ATA shadow register contents.
627 static unsigned int ata_mmio_devchk(struct ata_port *ap,
630 struct ata_ioports *ioaddr = &ap->ioaddr;
633 __ata_dev_select(ap, device);
635 writeb(0x55, (void *) ioaddr->nsect_addr);
636 writeb(0xaa, (void *) ioaddr->lbal_addr);
638 writeb(0xaa, (void *) ioaddr->nsect_addr);
639 writeb(0x55, (void *) ioaddr->lbal_addr);
641 writeb(0x55, (void *) ioaddr->nsect_addr);
642 writeb(0xaa, (void *) ioaddr->lbal_addr);
644 nsect = readb((void *) ioaddr->nsect_addr);
645 lbal = readb((void *) ioaddr->lbal_addr);
647 if ((nsect == 0x55) && (lbal == 0xaa))
648 return 1; /* we found a device */
650 return 0; /* nothing found */
654 * ata_dev_devchk - PATA device presence detection
655 * @ap: ATA channel to examine
656 * @device: Device to examine (starting at zero)
658 * Dispatch ATA device presence detection, depending
659 * on whether we are using PIO or MMIO to talk to the
660 * ATA shadow registers.
666 static unsigned int ata_dev_devchk(struct ata_port *ap,
669 if (ap->flags & ATA_FLAG_MMIO)
670 return ata_mmio_devchk(ap, device);
671 return ata_pio_devchk(ap, device);
675 * ata_dev_classify - determine device type based on ATA-spec signature
676 * @tf: ATA taskfile register set for device to be identified
678 * Determine from taskfile register contents whether a device is
679 * ATA or ATAPI, as per "Signature and persistence" section
680 * of ATA/PI spec (volume 1, sect 5.14).
686 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
687 * the event of failure.
690 static unsigned int ata_dev_classify(struct ata_taskfile *tf)
692 /* Apple's open source Darwin code hints that some devices only
693 * put a proper signature into the LBA mid/high registers,
694 * So, we only check those. It's sufficient for uniqueness.
697 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
698 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
699 DPRINTK("found ATA device by sig\n");
703 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
704 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
705 DPRINTK("found ATAPI device by sig\n");
706 return ATA_DEV_ATAPI;
709 DPRINTK("unknown device\n");
710 return ATA_DEV_UNKNOWN;
714 * ata_dev_try_classify - Parse returned ATA device signature
715 * @ap: ATA channel to examine
716 * @device: Device to examine (starting at zero)
718 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
719 * an ATA/ATAPI-defined set of values is placed in the ATA
720 * shadow registers, indicating the results of device detection
723 * Select the ATA device, and read the values from the ATA shadow
724 * registers. Then parse according to the Error register value,
725 * and the spec-defined values examined by ata_dev_classify().
731 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
733 struct ata_device *dev = &ap->device[device];
734 struct ata_taskfile tf;
738 __ata_dev_select(ap, device);
740 memset(&tf, 0, sizeof(tf));
742 err = ata_chk_err(ap);
743 ap->ops->tf_read(ap, &tf);
745 dev->class = ATA_DEV_NONE;
747 /* see if device passed diags */
750 else if ((device == 0) && (err == 0x81))
755 /* determine if device if ATA or ATAPI */
756 class = ata_dev_classify(&tf);
757 if (class == ATA_DEV_UNKNOWN)
759 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
768 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
769 * @dev: Device whose IDENTIFY DEVICE results we will examine
770 * @s: string into which data is output
771 * @ofs: offset into identify device page
772 * @len: length of string to return. must be an even number.
774 * The strings in the IDENTIFY DEVICE page are broken up into
775 * 16-bit chunks. Run through the string, and output each
776 * 8-bit chunk linearly, regardless of platform.
782 void ata_dev_id_string(struct ata_device *dev, unsigned char *s,
783 unsigned int ofs, unsigned int len)
788 c = dev->id[ofs] >> 8;
792 c = dev->id[ofs] & 0xff;
802 * __ata_dev_select - Select device 0/1 on ATA bus
803 * @ap: ATA channel to manipulate
804 * @device: ATA device (numbered from zero) to select
806 * Use the method defined in the ATA specification to
807 * make either device 0, or device 1, active on the
814 static void __ata_dev_select (struct ata_port *ap, unsigned int device)
819 tmp = ATA_DEVICE_OBS;
821 tmp = ATA_DEVICE_OBS | ATA_DEV1;
823 if (ap->flags & ATA_FLAG_MMIO) {
824 writeb(tmp, (void *) ap->ioaddr.device_addr);
826 outb(tmp, ap->ioaddr.device_addr);
828 ata_pause(ap); /* needed; also flushes, for mmio */
832 * ata_dev_select - Select device 0/1 on ATA bus
833 * @ap: ATA channel to manipulate
834 * @device: ATA device (numbered from zero) to select
835 * @wait: non-zero to wait for Status register BSY bit to clear
836 * @can_sleep: non-zero if context allows sleeping
838 * Use the method defined in the ATA specification to
839 * make either device 0, or device 1, active on the
842 * This is a high-level version of __ata_dev_select(),
843 * which additionally provides the services of inserting
844 * the proper pauses and status polling, where needed.
850 void ata_dev_select(struct ata_port *ap, unsigned int device,
851 unsigned int wait, unsigned int can_sleep)
853 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
854 ap->id, device, wait);
859 __ata_dev_select(ap, device);
862 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
869 * ata_dump_id - IDENTIFY DEVICE info debugging output
870 * @dev: Device whose IDENTIFY DEVICE page we will dump
872 * Dump selected 16-bit words from a detected device's
873 * IDENTIFY PAGE page.
879 static inline void ata_dump_id(struct ata_device *dev)
881 DPRINTK("49==0x%04x "
891 DPRINTK("80==0x%04x "
901 DPRINTK("88==0x%04x "
908 * ata_dev_identify - obtain IDENTIFY x DEVICE page
909 * @ap: port on which device we wish to probe resides
910 * @device: device bus address, starting at zero
912 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
913 * command, and read back the 512-byte device information page.
914 * The device information page is fed to us via the standard
915 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
916 * using standard PIO-IN paths)
918 * After reading the device information page, we use several
919 * bits of information from it to initialize data structures
920 * that will be used during the lifetime of the ata_device.
921 * Other data from the info page is used to disqualify certain
922 * older ATA devices we do not wish to support.
925 * Inherited from caller. Some functions called by this function
926 * obtain the host_set lock.
929 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
931 struct ata_device *dev = &ap->device[device];
935 struct ata_taskfile tf;
936 unsigned int using_edd;
938 if (!ata_dev_present(dev)) {
939 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
944 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
949 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
951 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
952 dev->class == ATA_DEV_NONE);
954 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
957 ata_tf_init(ap, &tf, device);
959 tf.protocol = ATA_PROT_PIO;
961 if (dev->class == ATA_DEV_ATA) {
962 tf.command = ATA_CMD_ID_ATA;
963 DPRINTK("do ATA identify\n");
965 tf.command = ATA_CMD_ID_ATAPI;
966 DPRINTK("do ATAPI identify\n");
969 ata_tf_to_host(ap, &tf);
971 /* crazy ATAPI devices... */
972 if (dev->class == ATA_DEV_ATAPI)
975 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT))
978 status = ata_chk_status(ap);
979 if (status & ATA_ERR) {
981 * arg! EDD works for all test cases, but seems to return
982 * the ATA signature for some ATAPI devices. Until the
983 * reason for this is found and fixed, we fix up the mess
984 * here. If IDENTIFY DEVICE returns command aborted
985 * (as ATAPI devices do), then we issue an
986 * IDENTIFY PACKET DEVICE.
988 * ATA software reset (SRST, the default) does not appear
989 * to have this problem.
991 if ((using_edd) && (tf.command == ATA_CMD_ID_ATA)) {
992 u8 err = ata_chk_err(ap);
993 if (err & ATA_ABORTED) {
994 dev->class = ATA_DEV_ATAPI;
1001 /* make sure we have BSY=0, DRQ=1 */
1002 if ((status & ATA_DRQ) == 0) {
1003 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) not returning id page (0x%x)\n",
1005 dev->class == ATA_DEV_ATA ? "" : "PI",
1010 /* read IDENTIFY [X] DEVICE page */
1011 if (ap->flags & ATA_FLAG_MMIO) {
1012 for (i = 0; i < ATA_ID_WORDS; i++)
1013 dev->id[i] = readw((void *)ap->ioaddr.data_addr);
1015 for (i = 0; i < ATA_ID_WORDS; i++)
1016 dev->id[i] = inw(ap->ioaddr.data_addr);
1018 /* wait for host_idle */
1019 status = ata_wait_idle(ap);
1020 if (status & (ATA_BUSY | ATA_DRQ)) {
1021 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) error after id page (0x%x)\n",
1023 dev->class == ATA_DEV_ATA ? "" : "PI",
1028 ata_irq_on(ap); /* re-enable interrupts */
1030 /* print device capabilities */
1031 printk(KERN_DEBUG "ata%u: dev %u cfg "
1032 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1033 ap->id, device, dev->id[49],
1034 dev->id[82], dev->id[83], dev->id[84],
1035 dev->id[85], dev->id[86], dev->id[87],
1039 * common ATA, ATAPI feature tests
1042 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1043 if (!ata_id_has_dma(dev) || !ata_id_has_lba(dev)) {
1044 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1048 /* we require UDMA support */
1050 tmp = dev->id[ATA_ID_UDMA_MODES];
1051 if ((tmp & 0xff) == 0) {
1052 printk(KERN_DEBUG "ata%u: no udma\n", ap->id);
1058 /* ATA-specific feature tests */
1059 if (dev->class == ATA_DEV_ATA) {
1060 if (!ata_id_is_ata(dev)) /* sanity check */
1063 tmp = dev->id[ATA_ID_MAJOR_VER];
1064 for (i = 14; i >= 1; i--)
1068 /* we require at least ATA-3 */
1070 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1074 if (ata_id_has_lba48(dev)) {
1075 dev->flags |= ATA_DFLAG_LBA48;
1076 dev->n_sectors = ata_id_u64(dev, 100);
1078 dev->n_sectors = ata_id_u32(dev, 60);
1081 ap->host->max_cmd_len = 16;
1083 /* print device info to dmesg */
1084 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1086 ata_udma_string(udma_modes),
1087 (unsigned long long)dev->n_sectors,
1088 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1091 /* ATAPI-specific feature tests */
1093 if (ata_id_is_ata(dev)) /* sanity check */
1096 /* see if 16-byte commands supported */
1097 tmp = dev->id[0] & 0x3;
1099 ap->host->max_cmd_len = 16;
1101 /* print device info to dmesg */
1102 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1104 ata_udma_string(udma_modes));
1107 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1111 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1114 ata_irq_on(ap); /* re-enable interrupts */
1115 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1116 DPRINTK("EXIT, err\n");
1120 * ata_bus_probe - Reset and probe ATA bus
1126 * Zero on success, non-zero on error.
1129 static int ata_bus_probe(struct ata_port *ap)
1131 unsigned int i, found = 0;
1133 ap->ops->phy_reset(ap);
1134 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1137 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1138 ata_dev_identify(ap, i);
1139 if (ata_dev_present(&ap->device[i])) {
1141 if (ap->ops->dev_config)
1142 ap->ops->dev_config(ap, &ap->device[i]);
1146 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1147 goto err_out_disable;
1150 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1151 goto err_out_disable;
1156 ap->ops->port_disable(ap);
1168 void ata_port_probe(struct ata_port *ap)
1170 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1180 void sata_phy_reset(struct ata_port *ap)
1183 unsigned long timeout = jiffies + (HZ * 5);
1185 if (ap->flags & ATA_FLAG_SATA_RESET) {
1186 scr_write(ap, SCR_CONTROL, 0x301); /* issue phy wake/reset */
1187 scr_read(ap, SCR_STATUS); /* dummy read; flush */
1188 udelay(400); /* FIXME: a guess */
1190 scr_write(ap, SCR_CONTROL, 0x300); /* issue phy wake/clear reset */
1192 /* wait for phy to become ready, if necessary */
1195 sstatus = scr_read(ap, SCR_STATUS);
1196 if ((sstatus & 0xf) != 1)
1198 } while (time_before(jiffies, timeout));
1200 /* TODO: phy layer with polling, timeouts, etc. */
1201 if (sata_dev_present(ap))
1204 sstatus = scr_read(ap, SCR_STATUS);
1205 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1207 ata_port_disable(ap);
1210 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1213 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1214 ata_port_disable(ap);
1222 * ata_port_disable -
1228 void ata_port_disable(struct ata_port *ap)
1230 ap->device[0].class = ATA_DEV_NONE;
1231 ap->device[1].class = ATA_DEV_NONE;
1232 ap->flags |= ATA_FLAG_PORT_DISABLED;
1236 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1237 * @ap: port on which timings will be programmed
1242 static void ata_set_mode(struct ata_port *ap)
1244 unsigned int force_pio, i;
1246 ata_host_set_pio(ap);
1247 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1250 ata_host_set_udma(ap);
1251 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1254 #ifdef ATA_FORCE_PIO
1261 ata_dev_set_pio(ap, 0);
1262 ata_dev_set_pio(ap, 1);
1264 ata_dev_set_udma(ap, 0);
1265 ata_dev_set_udma(ap, 1);
1268 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1271 if (ap->ops->post_set_mode)
1272 ap->ops->post_set_mode(ap);
1274 for (i = 0; i < 2; i++) {
1275 struct ata_device *dev = &ap->device[i];
1276 ata_dev_set_protocol(dev);
1281 * ata_busy_sleep - sleep until BSY clears, or timeout
1282 * @ap: port containing status register to be polled
1283 * @tmout_pat: impatience timeout
1284 * @tmout: overall timeout
1290 static unsigned int ata_busy_sleep (struct ata_port *ap,
1291 unsigned long tmout_pat,
1292 unsigned long tmout)
1294 unsigned long timer_start, timeout;
1297 status = ata_busy_wait(ap, ATA_BUSY, 300);
1298 timer_start = jiffies;
1299 timeout = timer_start + tmout_pat;
1300 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1302 status = ata_busy_wait(ap, ATA_BUSY, 3);
1305 if (status & ATA_BUSY)
1306 printk(KERN_WARNING "ata%u is slow to respond, "
1307 "please be patient\n", ap->id);
1309 timeout = timer_start + tmout;
1310 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1312 status = ata_chk_status(ap);
1315 if (status & ATA_BUSY) {
1316 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1317 ap->id, tmout / HZ);
1324 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1326 struct ata_ioports *ioaddr = &ap->ioaddr;
1327 unsigned int dev0 = devmask & (1 << 0);
1328 unsigned int dev1 = devmask & (1 << 1);
1329 unsigned long timeout;
1331 /* if device 0 was found in ata_dev_devchk, wait for its
1335 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1337 /* if device 1 was found in ata_dev_devchk, wait for
1338 * register access, then wait for BSY to clear
1340 timeout = jiffies + ATA_TMOUT_BOOT;
1344 __ata_dev_select(ap, 1);
1345 if (ap->flags & ATA_FLAG_MMIO) {
1346 nsect = readb((void *) ioaddr->nsect_addr);
1347 lbal = readb((void *) ioaddr->lbal_addr);
1349 nsect = inb(ioaddr->nsect_addr);
1350 lbal = inb(ioaddr->lbal_addr);
1352 if ((nsect == 1) && (lbal == 1))
1354 if (time_after(jiffies, timeout)) {
1358 msleep(50); /* give drive a breather */
1361 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1363 /* is all this really necessary? */
1364 __ata_dev_select(ap, 0);
1366 __ata_dev_select(ap, 1);
1368 __ata_dev_select(ap, 0);
1379 static unsigned int ata_bus_edd(struct ata_port *ap)
1381 struct ata_taskfile tf;
1383 /* set up execute-device-diag (bus reset) taskfile */
1384 /* also, take interrupts to a known state (disabled) */
1385 DPRINTK("execute-device-diag\n");
1386 ata_tf_init(ap, &tf, 0);
1388 tf.command = ATA_CMD_EDD;
1389 tf.protocol = ATA_PROT_NODATA;
1392 ata_tf_to_host(ap, &tf);
1394 /* spec says at least 2ms. but who knows with those
1395 * crazy ATAPI devices...
1399 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1402 static unsigned int ata_bus_softreset(struct ata_port *ap,
1403 unsigned int devmask)
1405 struct ata_ioports *ioaddr = &ap->ioaddr;
1407 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1409 /* software reset. causes dev0 to be selected */
1410 if (ap->flags & ATA_FLAG_MMIO) {
1411 writeb(ap->ctl, ioaddr->ctl_addr);
1412 udelay(20); /* FIXME: flush */
1413 writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1414 udelay(20); /* FIXME: flush */
1415 writeb(ap->ctl, ioaddr->ctl_addr);
1417 outb(ap->ctl, ioaddr->ctl_addr);
1419 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1421 outb(ap->ctl, ioaddr->ctl_addr);
1424 /* spec mandates ">= 2ms" before checking status.
1425 * We wait 150ms, because that was the magic delay used for
1426 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1427 * between when the ATA command register is written, and then
1428 * status is checked. Because waiting for "a while" before
1429 * checking status is fine, post SRST, we perform this magic
1430 * delay here as well.
1434 ata_bus_post_reset(ap, devmask);
1440 * ata_bus_reset - reset host port and associated ATA channel
1441 * @ap: port to reset
1443 * This is typically the first time we actually start issuing
1444 * commands to the ATA channel. We wait for BSY to clear, then
1445 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1446 * result. Determine what devices, if any, are on the channel
1447 * by looking at the device 0/1 error register. Look at the signature
1448 * stored in each device's taskfile registers, to determine if
1449 * the device is ATA or ATAPI.
1452 * Inherited from caller. Some functions called by this function
1453 * obtain the host_set lock.
1456 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1459 void ata_bus_reset(struct ata_port *ap)
1461 struct ata_ioports *ioaddr = &ap->ioaddr;
1462 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1464 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1466 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1468 /* determine if device 0/1 are present */
1469 if (ap->flags & ATA_FLAG_SATA_RESET)
1472 dev0 = ata_dev_devchk(ap, 0);
1474 dev1 = ata_dev_devchk(ap, 1);
1478 devmask |= (1 << 0);
1480 devmask |= (1 << 1);
1482 /* select device 0 again */
1483 __ata_dev_select(ap, 0);
1485 /* issue bus reset */
1486 if (ap->flags & ATA_FLAG_SRST)
1487 rc = ata_bus_softreset(ap, devmask);
1488 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1489 /* set up device control */
1490 if (ap->flags & ATA_FLAG_MMIO)
1491 writeb(ap->ctl, ioaddr->ctl_addr);
1493 outb(ap->ctl, ioaddr->ctl_addr);
1494 rc = ata_bus_edd(ap);
1501 * determine by signature whether we have ATA or ATAPI devices
1503 err = ata_dev_try_classify(ap, 0);
1504 if ((slave_possible) && (err != 0x81))
1505 ata_dev_try_classify(ap, 1);
1507 /* re-enable interrupts */
1510 /* is double-select really necessary? */
1511 if (ap->device[1].class != ATA_DEV_NONE)
1512 __ata_dev_select(ap, 1);
1513 if (ap->device[0].class != ATA_DEV_NONE)
1514 __ata_dev_select(ap, 0);
1516 /* if no devices were detected, disable this port */
1517 if ((ap->device[0].class == ATA_DEV_NONE) &&
1518 (ap->device[1].class == ATA_DEV_NONE))
1521 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1522 /* set up device control for ATA_FLAG_SATA_RESET */
1523 if (ap->flags & ATA_FLAG_MMIO)
1524 writeb(ap->ctl, ioaddr->ctl_addr);
1526 outb(ap->ctl, ioaddr->ctl_addr);
1533 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1534 ap->ops->port_disable(ap);
1540 * ata_host_set_pio -
1546 static void ata_host_set_pio(struct ata_port *ap)
1548 struct ata_device *master, *slave;
1549 unsigned int pio, i;
1552 master = &ap->device[0];
1553 slave = &ap->device[1];
1555 assert (ata_dev_present(master) || ata_dev_present(slave));
1557 mask = ap->pio_mask;
1558 if (ata_dev_present(master))
1559 mask &= (master->id[ATA_ID_PIO_MODES] & 0x03);
1560 if (ata_dev_present(slave))
1561 mask &= (slave->id[ATA_ID_PIO_MODES] & 0x03);
1563 /* require pio mode 3 or 4 support for host and all devices */
1565 printk(KERN_WARNING "ata%u: no PIO3/4 support, ignoring\n",
1570 pio = (mask & ATA_ID_PIO4) ? 4 : 3;
1571 for (i = 0; i < ATA_MAX_DEVICES; i++)
1572 if (ata_dev_present(&ap->device[i])) {
1573 ap->device[i].pio_mode = (pio == 3) ?
1574 XFER_PIO_3 : XFER_PIO_4;
1575 if (ap->ops->set_piomode)
1576 ap->ops->set_piomode(ap, &ap->device[i], pio);
1582 ap->ops->port_disable(ap);
1586 * ata_host_set_udma -
1592 static void ata_host_set_udma(struct ata_port *ap)
1594 struct ata_device *master, *slave;
1599 master = &ap->device[0];
1600 slave = &ap->device[1];
1602 assert (ata_dev_present(master) || ata_dev_present(slave));
1603 assert ((ap->flags & ATA_FLAG_PORT_DISABLED) == 0);
1605 DPRINTK("udma masks: host 0x%X, master 0x%X, slave 0x%X\n",
1607 (!ata_dev_present(master)) ? 0xff :
1608 (master->id[ATA_ID_UDMA_MODES] & 0xff),
1609 (!ata_dev_present(slave)) ? 0xff :
1610 (slave->id[ATA_ID_UDMA_MODES] & 0xff));
1612 mask = ap->udma_mask;
1613 if (ata_dev_present(master))
1614 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1615 if (ata_dev_present(slave))
1616 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1619 while (i >= XFER_UDMA_0) {
1620 j = i - XFER_UDMA_0;
1621 DPRINTK("mask 0x%X i 0x%X j %u\n", mask, i, j);
1622 if (mask & (1 << j)) {
1630 /* require udma for host and all attached devices */
1631 if (udma_mode < 0) {
1632 printk(KERN_WARNING "ata%u: no UltraDMA support, ignoring\n",
1637 for (i = 0; i < ATA_MAX_DEVICES; i++)
1638 if (ata_dev_present(&ap->device[i])) {
1639 ap->device[i].udma_mode = udma_mode;
1640 if (ap->ops->set_udmamode)
1641 ap->ops->set_udmamode(ap, &ap->device[i],
1648 ap->ops->port_disable(ap);
1652 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1653 * @ap: Port associated with device @dev
1654 * @dev: Device to which command will be sent
1659 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1661 struct ata_taskfile tf;
1663 /* set up set-features taskfile */
1664 DPRINTK("set features - xfer mode\n");
1665 ata_tf_init(ap, &tf, dev->devno);
1667 tf.command = ATA_CMD_SET_FEATURES;
1668 tf.feature = SETFEATURES_XFER;
1669 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1670 tf.protocol = ATA_PROT_NODATA;
1671 if (dev->flags & ATA_DFLAG_PIO)
1672 tf.nsect = dev->pio_mode;
1674 tf.nsect = dev->udma_mode;
1677 ata_tf_to_host(ap, &tf);
1679 /* crazy ATAPI devices... */
1680 if (dev->class == ATA_DEV_ATAPI)
1683 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1685 ata_irq_on(ap); /* re-enable interrupts */
1693 * ata_dev_set_udma - Set ATA device's transfer mode to Ultra DMA
1694 * @ap: Port associated with device @dev
1695 * @device: Device whose mode will be set
1700 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device)
1702 struct ata_device *dev = &ap->device[device];
1704 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1707 ata_dev_set_xfermode(ap, dev);
1709 assert((dev->udma_mode >= XFER_UDMA_0) &&
1710 (dev->udma_mode <= XFER_UDMA_7));
1711 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1713 udma_str[dev->udma_mode - XFER_UDMA_0]);
1717 * ata_dev_set_pio - Set ATA device's transfer mode to PIO
1718 * @ap: Port associated with device @dev
1719 * @device: Device whose mode will be set
1724 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device)
1726 struct ata_device *dev = &ap->device[device];
1728 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1731 /* force PIO mode */
1732 dev->flags |= ATA_DFLAG_PIO;
1734 ata_dev_set_xfermode(ap, dev);
1736 assert((dev->pio_mode >= XFER_PIO_3) &&
1737 (dev->pio_mode <= XFER_PIO_4));
1738 printk(KERN_INFO "ata%u: dev %u configured for PIO%c\n",
1740 dev->pio_mode == 3 ? '3' : '4');
1750 static void ata_sg_clean(struct ata_queued_cmd *qc)
1752 struct ata_port *ap = qc->ap;
1753 struct scatterlist *sg = qc->sg;
1754 int dir = qc->pci_dma_dir;
1756 assert(qc->flags & ATA_QCFLAG_DMAMAP);
1759 if (qc->flags & ATA_QCFLAG_SINGLE)
1760 assert(qc->n_elem == 1);
1762 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
1764 if (qc->flags & ATA_QCFLAG_SG)
1765 pci_unmap_sg(ap->host_set->pdev, sg, qc->n_elem, dir);
1767 pci_unmap_single(ap->host_set->pdev, sg_dma_address(&sg[0]),
1768 sg_dma_len(&sg[0]), dir);
1770 qc->flags &= ~ATA_QCFLAG_DMAMAP;
1775 * ata_fill_sg - Fill PCI IDE PRD table
1776 * @qc: Metadata associated with taskfile to be transferred
1781 static void ata_fill_sg(struct ata_queued_cmd *qc)
1783 struct scatterlist *sg = qc->sg;
1784 struct ata_port *ap = qc->ap;
1785 unsigned int idx, nelem;
1788 assert(qc->n_elem > 0);
1791 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
1795 /* determine if physical DMA addr spans 64K boundary.
1796 * Note h/w doesn't support 64-bit, so we unconditionally
1797 * truncate dma_addr_t to u32.
1799 addr = (u32) sg_dma_address(sg);
1800 sg_len = sg_dma_len(sg);
1803 boundary = (addr & ~0xffff) + (0xffff + 1);
1805 if ((addr + sg_len) > boundary)
1806 len = boundary - addr;
1808 ap->prd[idx].addr = cpu_to_le32(addr);
1809 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
1810 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
1819 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
1823 * ata_qc_prep - Prepare taskfile for submission
1824 * @qc: Metadata associated with taskfile to be prepared
1827 * spin_lock_irqsave(host_set lock)
1829 void ata_qc_prep(struct ata_queued_cmd *qc)
1831 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1837 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
1839 struct scatterlist *sg;
1841 qc->flags |= ATA_QCFLAG_SINGLE;
1843 memset(&qc->sgent, 0, sizeof(qc->sgent));
1844 qc->sg = &qc->sgent;
1849 sg->page = virt_to_page(buf);
1850 sg->offset = (unsigned long) buf & ~PAGE_MASK;
1851 sg_dma_len(sg) = buflen;
1853 WARN_ON(buflen > PAGE_SIZE);
1856 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
1857 unsigned int n_elem)
1859 qc->flags |= ATA_QCFLAG_SG;
1861 qc->n_elem = n_elem;
1865 * ata_sg_setup_one -
1869 * spin_lock_irqsave(host_set lock)
1875 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
1877 struct ata_port *ap = qc->ap;
1878 int dir = qc->pci_dma_dir;
1879 struct scatterlist *sg = qc->sg;
1880 dma_addr_t dma_address;
1882 dma_address = pci_map_single(ap->host_set->pdev, qc->buf_virt,
1883 sg_dma_len(sg), dir);
1884 if (pci_dma_mapping_error(dma_address))
1887 sg_dma_address(sg) = dma_address;
1889 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
1890 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1900 * spin_lock_irqsave(host_set lock)
1906 static int ata_sg_setup(struct ata_queued_cmd *qc)
1908 struct ata_port *ap = qc->ap;
1909 struct scatterlist *sg = qc->sg;
1912 VPRINTK("ENTER, ata%u\n", ap->id);
1913 assert(qc->flags & ATA_QCFLAG_SG);
1915 dir = qc->pci_dma_dir;
1916 n_elem = pci_map_sg(ap->host_set->pdev, sg, qc->n_elem, dir);
1920 DPRINTK("%d sg elements mapped\n", n_elem);
1922 qc->n_elem = n_elem;
1937 static unsigned long ata_pio_poll(struct ata_port *ap)
1940 unsigned int poll_state = PIO_ST_UNKNOWN;
1941 unsigned int reg_state = PIO_ST_UNKNOWN;
1942 const unsigned int tmout_state = PIO_ST_TMOUT;
1944 switch (ap->pio_task_state) {
1947 poll_state = PIO_ST_POLL;
1951 case PIO_ST_LAST_POLL:
1952 poll_state = PIO_ST_LAST_POLL;
1953 reg_state = PIO_ST_LAST;
1960 status = ata_chk_status(ap);
1961 if (status & ATA_BUSY) {
1962 if (time_after(jiffies, ap->pio_task_timeout)) {
1963 ap->pio_task_state = tmout_state;
1966 ap->pio_task_state = poll_state;
1967 return ATA_SHORT_PAUSE;
1970 ap->pio_task_state = reg_state;
1975 * ata_pio_complete -
1981 static void ata_pio_complete (struct ata_port *ap)
1983 struct ata_queued_cmd *qc;
1987 * This is purely hueristic. This is a fast path.
1988 * Sometimes when we enter, BSY will be cleared in
1989 * a chk-status or two. If not, the drive is probably seeking
1990 * or something. Snooze for a couple msecs, then
1991 * chk-status again. If still busy, fall back to
1992 * PIO_ST_POLL state.
1994 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
1995 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
1997 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
1998 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
1999 ap->pio_task_state = PIO_ST_LAST_POLL;
2000 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2005 drv_stat = ata_wait_idle(ap);
2006 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2007 ap->pio_task_state = PIO_ST_ERR;
2011 qc = ata_qc_from_tag(ap, ap->active_tag);
2014 ap->pio_task_state = PIO_ST_IDLE;
2018 ata_qc_complete(qc, drv_stat);
2028 static void ata_pio_sector(struct ata_port *ap)
2030 struct ata_queued_cmd *qc;
2031 struct scatterlist *sg;
2037 * This is purely hueristic. This is a fast path.
2038 * Sometimes when we enter, BSY will be cleared in
2039 * a chk-status or two. If not, the drive is probably seeking
2040 * or something. Snooze for a couple msecs, then
2041 * chk-status again. If still busy, fall back to
2042 * PIO_ST_POLL state.
2044 status = ata_busy_wait(ap, ATA_BUSY, 5);
2045 if (status & ATA_BUSY) {
2047 status = ata_busy_wait(ap, ATA_BUSY, 10);
2048 if (status & ATA_BUSY) {
2049 ap->pio_task_state = PIO_ST_POLL;
2050 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2055 /* handle BSY=0, DRQ=0 as error */
2056 if ((status & ATA_DRQ) == 0) {
2057 ap->pio_task_state = PIO_ST_ERR;
2061 qc = ata_qc_from_tag(ap, ap->active_tag);
2066 if (qc->cursect == (qc->nsect - 1))
2067 ap->pio_task_state = PIO_ST_LAST;
2069 page = sg[qc->cursg].page;
2071 sg[qc->cursg].offset + (qc->cursg_ofs * ATA_SECT_SIZE);
2076 if (qc->flags & ATA_QCFLAG_SG)
2077 if ((qc->cursg_ofs * ATA_SECT_SIZE) == sg_dma_len(&sg[qc->cursg])) {
2082 DPRINTK("data %s, drv_stat 0x%X\n",
2083 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read",
2086 /* do the actual data transfer */
2087 /* FIXME: mmio-ize */
2088 if (qc->tf.flags & ATA_TFLAG_WRITE)
2089 outsl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2091 insl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2096 static void ata_pio_task(void *_data)
2098 struct ata_port *ap = _data;
2099 unsigned long timeout = 0;
2101 switch (ap->pio_task_state) {
2107 ata_pio_complete(ap);
2111 case PIO_ST_LAST_POLL:
2112 timeout = ata_pio_poll(ap);
2116 printk(KERN_ERR "ata%d: FIXME: PIO_ST_TMOUT\n", /* FIXME */
2122 printk(KERN_ERR "ata%d: FIXME: PIO_ST_ERR\n", /* FIXME */
2128 if ((ap->pio_task_state != PIO_ST_IDLE) &&
2129 (ap->pio_task_state != PIO_ST_TMOUT) &&
2130 (ap->pio_task_state != PIO_ST_ERR)) {
2132 queue_delayed_work(ata_wq, &ap->pio_task,
2135 queue_work(ata_wq, &ap->pio_task);
2140 * ata_qc_timeout - Handle timeout of queued command
2141 * @qc: Command that timed out
2143 * Some part of the kernel (currently, only the SCSI layer)
2144 * has noticed that the active command on port @ap has not
2145 * completed after a specified length of time. Handle this
2146 * condition by disabling DMA (if necessary) and completing
2147 * transactions, with error if necessary.
2149 * This also handles the case of the "lost interrupt", where
2150 * for some reason (possibly hardware bug, possibly driver bug)
2151 * an interrupt was not delivered to the driver, even though the
2152 * transaction completed successfully.
2157 static void ata_qc_timeout(struct ata_queued_cmd *qc)
2159 struct ata_port *ap = qc->ap;
2160 u8 host_stat = 0, drv_stat;
2164 /* hack alert! We cannot use the supplied completion
2165 * function from inside the ->eh_strategy_handler() thread.
2166 * libata is the only user of ->eh_strategy_handler() in
2167 * any kernel, so the default scsi_done() assumes it is
2168 * not being called from the SCSI EH.
2170 qc->scsidone = scsi_finish_command;
2172 switch (qc->tf.protocol) {
2175 case ATA_PROT_ATAPI_DMA:
2176 host_stat = ata_bmdma_status(ap);
2178 /* before we do anything else, clear DMA-Start bit */
2183 case ATA_PROT_NODATA:
2186 drv_stat = ata_chk_status(ap);
2188 /* ack bmdma irq events */
2189 ata_bmdma_ack_irq(ap);
2191 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
2192 ap->id, qc->tf.command, drv_stat, host_stat);
2194 /* complete taskfile transaction */
2195 ata_qc_complete(qc, drv_stat);
2203 * ata_eng_timeout - Handle timeout of queued command
2204 * @ap: Port on which timed-out command is active
2206 * Some part of the kernel (currently, only the SCSI layer)
2207 * has noticed that the active command on port @ap has not
2208 * completed after a specified length of time. Handle this
2209 * condition by disabling DMA (if necessary) and completing
2210 * transactions, with error if necessary.
2212 * This also handles the case of the "lost interrupt", where
2213 * for some reason (possibly hardware bug, possibly driver bug)
2214 * an interrupt was not delivered to the driver, even though the
2215 * transaction completed successfully.
2218 * Inherited from SCSI layer (none, can sleep)
2221 void ata_eng_timeout(struct ata_port *ap)
2223 struct ata_queued_cmd *qc;
2227 qc = ata_qc_from_tag(ap, ap->active_tag);
2229 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2241 * ata_qc_new - Request an available ATA command, for queueing
2242 * @ap: Port associated with device @dev
2243 * @dev: Device from whom we request an available command structure
2248 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2250 struct ata_queued_cmd *qc = NULL;
2253 for (i = 0; i < ATA_MAX_QUEUE; i++)
2254 if (!test_and_set_bit(i, &ap->qactive)) {
2255 qc = ata_qc_from_tag(ap, i);
2266 * ata_qc_new_init - Request an available ATA command, and initialize it
2267 * @ap: Port associated with device @dev
2268 * @dev: Device from whom we request an available command structure
2273 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2274 struct ata_device *dev)
2276 struct ata_queued_cmd *qc;
2278 qc = ata_qc_new(ap);
2285 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2288 ata_tf_init(ap, &qc->tf, dev->devno);
2290 if (likely((dev->flags & ATA_DFLAG_PIO) == 0))
2291 qc->flags |= ATA_QCFLAG_DMA;
2292 if (dev->flags & ATA_DFLAG_LBA48)
2293 qc->tf.flags |= ATA_TFLAG_LBA48;
2300 * ata_qc_complete - Complete an active ATA command
2301 * @qc: Command to complete
2302 * @drv_stat: ATA status register contents
2308 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2310 struct ata_port *ap = qc->ap;
2311 unsigned int tag, do_clear = 0;
2314 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2315 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2317 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
2320 /* call completion callback */
2321 rc = qc->complete_fn(qc, drv_stat);
2323 /* if callback indicates not to complete command (non-zero),
2324 * return immediately
2331 if (likely(ata_tag_valid(tag))) {
2332 if (tag == ap->active_tag)
2333 ap->active_tag = ATA_TAG_POISON;
2334 qc->tag = ATA_TAG_POISON;
2339 complete(qc->waiting);
2341 if (likely(do_clear))
2342 clear_bit(tag, &ap->qactive);
2346 * ata_qc_issue - issue taskfile to device
2347 * @qc: command to issue to device
2349 * Prepare an ATA command to submission to device.
2350 * This includes mapping the data into a DMA-able
2351 * area, filling in the S/G table, and finally
2352 * writing the taskfile to hardware, starting the command.
2355 * spin_lock_irqsave(host_set lock)
2358 * Zero on success, negative on error.
2361 int ata_qc_issue(struct ata_queued_cmd *qc)
2363 struct ata_port *ap = qc->ap;
2365 if (qc->flags & ATA_QCFLAG_SG) {
2366 if (ata_sg_setup(qc))
2368 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
2369 if (ata_sg_setup_one(qc))
2373 ap->ops->qc_prep(qc);
2375 qc->ap->active_tag = qc->tag;
2376 qc->flags |= ATA_QCFLAG_ACTIVE;
2378 return ap->ops->qc_issue(qc);
2385 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
2386 * @qc: command to issue to device
2388 * Using various libata functions and hooks, this function
2389 * starts an ATA command. ATA commands are grouped into
2390 * classes called "protocols", and issuing each type of protocol
2391 * is slightly different.
2394 * spin_lock_irqsave(host_set lock)
2397 * Zero on success, negative on error.
2400 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
2402 struct ata_port *ap = qc->ap;
2404 ata_dev_select(ap, qc->dev->devno, 1, 0);
2406 switch (qc->tf.protocol) {
2407 case ATA_PROT_NODATA:
2408 ata_tf_to_host_nolock(ap, &qc->tf);
2412 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2413 ap->ops->bmdma_setup(qc); /* set up bmdma */
2414 ap->ops->bmdma_start(qc); /* initiate bmdma */
2417 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
2418 ata_qc_set_polling(qc);
2419 ata_tf_to_host_nolock(ap, &qc->tf);
2420 ap->pio_task_state = PIO_ST;
2421 queue_work(ata_wq, &ap->pio_task);
2424 case ATA_PROT_ATAPI:
2425 ata_tf_to_host_nolock(ap, &qc->tf);
2426 queue_work(ata_wq, &ap->packet_task);
2429 case ATA_PROT_ATAPI_DMA:
2430 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2431 ap->ops->bmdma_setup(qc); /* set up bmdma */
2432 queue_work(ata_wq, &ap->packet_task);
2444 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction (MMIO)
2445 * @qc: Info associated with this ATA transaction.
2448 * spin_lock_irqsave(host_set lock)
2451 void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
2453 struct ata_port *ap = qc->ap;
2454 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2456 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2458 /* load PRD table addr. */
2459 mb(); /* make sure PRD table writes are visible to controller */
2460 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
2462 /* specify data direction, triple-check start bit is clear */
2463 dmactl = readb(mmio + ATA_DMA_CMD);
2464 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2466 dmactl |= ATA_DMA_WR;
2467 writeb(dmactl, mmio + ATA_DMA_CMD);
2469 /* issue r/w command */
2470 ap->ops->exec_command(ap, &qc->tf);
2474 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction (MMIO)
2475 * @qc: Info associated with this ATA transaction.
2478 * spin_lock_irqsave(host_set lock)
2481 void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
2483 struct ata_port *ap = qc->ap;
2484 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2487 /* start host DMA transaction */
2488 dmactl = readb(mmio + ATA_DMA_CMD);
2489 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
2491 /* Strictly, one may wish to issue a readb() here, to
2492 * flush the mmio write. However, control also passes
2493 * to the hardware at this point, and it will interrupt
2494 * us when we are to resume control. So, in effect,
2495 * we don't care when the mmio write flushes.
2496 * Further, a read of the DMA status register _immediately_
2497 * following the write may not be what certain flaky hardware
2498 * is expected, so I think it is best to not add a readb()
2499 * without first all the MMIO ATA cards/mobos.
2500 * Or maybe I'm just being paranoid.
2505 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
2506 * @qc: Info associated with this ATA transaction.
2509 * spin_lock_irqsave(host_set lock)
2512 void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
2514 struct ata_port *ap = qc->ap;
2515 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2518 /* load PRD table addr. */
2519 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2521 /* specify data direction, triple-check start bit is clear */
2522 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2523 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2525 dmactl |= ATA_DMA_WR;
2526 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2528 /* issue r/w command */
2529 ap->ops->exec_command(ap, &qc->tf);
2533 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
2534 * @qc: Info associated with this ATA transaction.
2537 * spin_lock_irqsave(host_set lock)
2540 void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
2542 struct ata_port *ap = qc->ap;
2545 /* start host DMA transaction */
2546 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2547 outb(dmactl | ATA_DMA_START,
2548 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2551 void ata_bmdma_irq_clear(struct ata_port *ap)
2553 ata_bmdma_ack_irq(ap);
2557 * ata_host_intr - Handle host interrupt for given (port, task)
2558 * @ap: Port on which interrupt arrived (possibly...)
2559 * @qc: Taskfile currently active in engine
2561 * Handle host interrupt for given queued command. Currently,
2562 * only DMA interrupts are handled. All other commands are
2563 * handled via polling with interrupts disabled (nIEN bit).
2566 * spin_lock_irqsave(host_set lock)
2569 * One if interrupt was handled, zero if not (shared irq).
2572 inline unsigned int ata_host_intr (struct ata_port *ap,
2573 struct ata_queued_cmd *qc)
2575 u8 status, host_stat;
2577 switch (qc->tf.protocol) {
2580 case ATA_PROT_ATAPI_DMA:
2581 case ATA_PROT_ATAPI:
2582 /* check status of DMA engine */
2583 host_stat = ata_bmdma_status(ap);
2584 VPRINTK("BUS_DMA (host_stat 0x%X)\n", host_stat);
2586 /* if it's not our irq... */
2587 if (!(host_stat & ATA_DMA_INTR))
2590 /* before we do anything else, clear DMA-Start bit */
2595 case ATA_PROT_NODATA:
2596 /* check altstatus */
2597 status = ata_altstatus(ap);
2598 if (status & ATA_BUSY)
2601 /* check main status, clearing INTRQ */
2602 status = ata_chk_status(ap);
2603 if (unlikely(status & ATA_BUSY))
2605 DPRINTK("BUS_NODATA (dev_stat 0x%X)\n", status);
2607 /* ack bmdma irq events */
2608 ata_bmdma_ack_irq(ap);
2610 /* complete taskfile transaction */
2611 ata_qc_complete(qc, status);
2618 return 1; /* irq handled */
2621 ap->stats.idle_irq++;
2624 if ((ap->stats.idle_irq % 1000) == 0) {
2626 ata_irq_ack(ap, 0); /* debug trap */
2627 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
2630 return 0; /* irq not handled */
2634 * ata_interrupt - Default ATA host interrupt handler
2636 * @dev_instance: pointer to our host information structure
2645 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
2647 struct ata_host_set *host_set = dev_instance;
2649 unsigned int handled = 0;
2650 unsigned long flags;
2652 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
2653 spin_lock_irqsave(&host_set->lock, flags);
2655 for (i = 0; i < host_set->n_ports; i++) {
2656 struct ata_port *ap;
2658 ap = host_set->ports[i];
2659 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
2660 struct ata_queued_cmd *qc;
2662 qc = ata_qc_from_tag(ap, ap->active_tag);
2663 if (qc && (!(qc->tf.ctl & ATA_NIEN)))
2664 handled |= ata_host_intr(ap, qc);
2668 spin_unlock_irqrestore(&host_set->lock, flags);
2670 return IRQ_RETVAL(handled);
2674 * atapi_packet_task - Write CDB bytes to hardware
2675 * @_data: Port to which ATAPI device is attached.
2677 * When device has indicated its readiness to accept
2678 * a CDB, this function is called. Send the CDB.
2679 * If DMA is to be performed, exit immediately.
2680 * Otherwise, we are in polling mode, so poll
2681 * status under operation succeeds or fails.
2684 * Kernel thread context (may sleep)
2687 static void atapi_packet_task(void *_data)
2689 struct ata_port *ap = _data;
2690 struct ata_queued_cmd *qc;
2693 qc = ata_qc_from_tag(ap, ap->active_tag);
2695 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2697 /* sleep-wait for BSY to clear */
2698 DPRINTK("busy wait\n");
2699 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
2702 /* make sure DRQ is set */
2703 status = ata_chk_status(ap);
2704 if ((status & ATA_DRQ) == 0)
2708 /* FIXME: mmio-ize */
2709 DPRINTK("send cdb\n");
2710 outsl(ap->ioaddr.data_addr,
2711 qc->scsicmd->cmnd, ap->host->max_cmd_len / 4);
2713 /* if we are DMA'ing, irq handler takes over from here */
2714 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
2715 ap->ops->bmdma_start(qc); /* initiate bmdma */
2717 /* non-data commands are also handled via irq */
2718 else if (qc->scsicmd->sc_data_direction == SCSI_DATA_NONE) {
2722 /* PIO commands are handled by polling */
2724 ap->pio_task_state = PIO_ST;
2725 queue_work(ata_wq, &ap->pio_task);
2731 ata_qc_complete(qc, ATA_ERR);
2734 int ata_port_start (struct ata_port *ap)
2736 struct pci_dev *pdev = ap->host_set->pdev;
2738 ap->prd = pci_alloc_consistent(pdev, ATA_PRD_TBL_SZ, &ap->prd_dma);
2742 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
2747 void ata_port_stop (struct ata_port *ap)
2749 struct pci_dev *pdev = ap->host_set->pdev;
2751 pci_free_consistent(pdev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
2755 * ata_host_remove - Unregister SCSI host structure with upper layers
2756 * @ap: Port to unregister
2757 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
2762 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
2764 struct Scsi_Host *sh = ap->host;
2769 scsi_remove_host(sh);
2771 ap->ops->port_stop(ap);
2775 * ata_host_init - Initialize an ata_port structure
2776 * @ap: Structure to initialize
2777 * @host: associated SCSI mid-layer structure
2778 * @host_set: Collection of hosts to which @ap belongs
2779 * @ent: Probe information provided by low-level driver
2780 * @port_no: Port number associated with this ata_port
2786 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
2787 struct ata_host_set *host_set,
2788 struct ata_probe_ent *ent, unsigned int port_no)
2794 host->max_channel = 1;
2795 host->unique_id = ata_unique_id++;
2796 host->max_cmd_len = 12;
2797 scsi_set_device(host, &ent->pdev->dev);
2798 scsi_assign_lock(host, &host_set->lock);
2800 ap->flags = ATA_FLAG_PORT_DISABLED;
2801 ap->id = host->unique_id;
2803 ap->ctl = ATA_DEVCTL_OBS;
2804 ap->host_set = host_set;
2805 ap->port_no = port_no;
2806 ap->pio_mask = ent->pio_mask;
2807 ap->udma_mask = ent->udma_mask;
2808 ap->flags |= ent->host_flags;
2809 ap->ops = ent->port_ops;
2810 ap->cbl = ATA_CBL_NONE;
2811 ap->device[0].flags = ATA_DFLAG_MASTER;
2812 ap->active_tag = ATA_TAG_POISON;
2813 ap->last_ctl = 0xFF;
2815 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
2816 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
2818 for (i = 0; i < ATA_MAX_DEVICES; i++)
2819 ap->device[i].devno = i;
2822 ap->stats.unhandled_irq = 1;
2823 ap->stats.idle_irq = 1;
2826 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
2830 * ata_host_add - Attach low-level ATA driver to system
2831 * @ent: Information provided by low-level driver
2832 * @host_set: Collections of ports to which we add
2833 * @port_no: Port number associated with this host
2841 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
2842 struct ata_host_set *host_set,
2843 unsigned int port_no)
2845 struct Scsi_Host *host;
2846 struct ata_port *ap;
2850 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
2854 ap = (struct ata_port *) &host->hostdata[0];
2856 ata_host_init(ap, host, host_set, ent, port_no);
2858 rc = ap->ops->port_start(ap);
2865 scsi_host_put(host);
2879 int ata_device_add(struct ata_probe_ent *ent)
2881 unsigned int count = 0, i;
2882 struct pci_dev *pdev = ent->pdev;
2883 struct ata_host_set *host_set;
2886 /* alloc a container for our list of ATA ports (buses) */
2887 host_set = kmalloc(sizeof(struct ata_host_set) +
2888 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
2891 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
2892 spin_lock_init(&host_set->lock);
2894 host_set->pdev = pdev;
2895 host_set->n_ports = ent->n_ports;
2896 host_set->irq = ent->irq;
2897 host_set->mmio_base = ent->mmio_base;
2898 host_set->private_data = ent->private_data;
2899 host_set->ops = ent->port_ops;
2901 /* register each port bound to this device */
2902 for (i = 0; i < ent->n_ports; i++) {
2903 struct ata_port *ap;
2905 ap = ata_host_add(ent, host_set, i);
2909 host_set->ports[i] = ap;
2911 /* print per-port info to dmesg */
2912 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
2913 "bmdma 0x%lX irq %lu\n",
2915 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
2916 ata_udma_string(ent->udma_mask),
2917 ap->ioaddr.cmd_addr,
2918 ap->ioaddr.ctl_addr,
2919 ap->ioaddr.bmdma_addr,
2923 host_set->ops->irq_clear(ap);
2932 /* obtain irq, that is shared between channels */
2933 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
2934 DRV_NAME, host_set))
2937 /* perform each probe synchronously */
2938 DPRINTK("probe begin\n");
2939 for (i = 0; i < count; i++) {
2940 struct ata_port *ap;
2943 ap = host_set->ports[i];
2945 DPRINTK("ata%u: probe begin\n", ap->id);
2946 rc = ata_bus_probe(ap);
2947 DPRINTK("ata%u: probe end\n", ap->id);
2950 /* FIXME: do something useful here?
2951 * Current libata behavior will
2952 * tear down everything when
2953 * the module is removed
2954 * or the h/w is unplugged.
2958 rc = scsi_add_host(ap->host, &pdev->dev);
2960 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
2962 /* FIXME: do something useful here */
2963 /* FIXME: handle unconditional calls to
2964 * scsi_scan_host and ata_host_remove, below,
2970 /* probes are done, now scan each port's disk(s) */
2971 DPRINTK("probe begin\n");
2972 for (i = 0; i < count; i++) {
2973 struct ata_port *ap = host_set->ports[i];
2975 scsi_scan_host(ap->host);
2978 pci_set_drvdata(pdev, host_set);
2980 VPRINTK("EXIT, returning %u\n", ent->n_ports);
2981 return ent->n_ports; /* success */
2984 for (i = 0; i < count; i++) {
2985 ata_host_remove(host_set->ports[i], 1);
2986 scsi_host_put(host_set->ports[i]->host);
2989 VPRINTK("EXIT, returning 0\n");
2994 * ata_scsi_release - SCSI layer callback hook for host unload
2995 * @host: libata host to be unloaded
2997 * Performs all duties necessary to shut down a libata port...
2998 * Kill port kthread, disable port, and release resources.
3001 * Inherited from SCSI layer.
3007 int ata_scsi_release(struct Scsi_Host *host)
3009 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3013 ap->ops->port_disable(ap);
3014 ata_host_remove(ap, 0);
3021 * ata_std_ports - initialize ioaddr with standard port offsets.
3022 * @ioaddr: IO address structure to be initialized
3024 void ata_std_ports(struct ata_ioports *ioaddr)
3026 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3027 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3028 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3029 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3030 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3031 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3032 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3033 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3034 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3035 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3039 * ata_pci_init_one - Initialize/register PCI IDE host controller
3040 * @pdev: Controller to be initialized
3041 * @port_info: Information from low-level host driver
3042 * @n_ports: Number of ports attached to host controller
3045 * Inherited from PCI layer (may sleep).
3051 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3052 unsigned int n_ports)
3054 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3055 struct ata_port_info *port0, *port1;
3057 unsigned int legacy_mode = 0;
3062 port0 = port_info[0];
3064 port1 = port_info[1];
3068 if ((port0->host_flags & ATA_FLAG_NO_LEGACY) == 0) {
3069 /* TODO: support transitioning to native mode? */
3070 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3071 mask = (1 << 2) | (1 << 0);
3072 if ((tmp8 & mask) != mask)
3073 legacy_mode = (1 << 3);
3077 if ((!legacy_mode) && (n_ports > 1)) {
3078 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3082 rc = pci_enable_device(pdev);
3086 rc = pci_request_regions(pdev, DRV_NAME);
3091 if (!request_region(0x1f0, 8, "libata")) {
3092 struct resource *conflict, res;
3094 res.end = 0x1f0 + 8 - 1;
3095 conflict = ____request_resource(&ioport_resource, &res);
3096 if (!strcmp(conflict->name, "libata"))
3097 legacy_mode |= (1 << 0);
3099 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3101 legacy_mode |= (1 << 0);
3103 if (!request_region(0x170, 8, "libata")) {
3104 struct resource *conflict, res;
3106 res.end = 0x170 + 8 - 1;
3107 conflict = ____request_resource(&ioport_resource, &res);
3108 if (!strcmp(conflict->name, "libata"))
3109 legacy_mode |= (1 << 1);
3111 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3113 legacy_mode |= (1 << 1);
3116 /* we have legacy mode, but all ports are unavailable */
3117 if (legacy_mode == (1 << 3)) {
3119 goto err_out_regions;
3122 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3124 goto err_out_regions;
3125 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3127 goto err_out_regions;
3129 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3132 goto err_out_regions;
3135 memset(probe_ent, 0, sizeof(*probe_ent));
3136 probe_ent->pdev = pdev;
3137 INIT_LIST_HEAD(&probe_ent->node);
3140 probe_ent2 = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3143 goto err_out_free_ent;
3146 memset(probe_ent2, 0, sizeof(*probe_ent));
3147 probe_ent2->pdev = pdev;
3148 INIT_LIST_HEAD(&probe_ent2->node);
3151 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3152 probe_ent->sht = port0->sht;
3153 probe_ent->host_flags = port0->host_flags;
3154 probe_ent->pio_mask = port0->pio_mask;
3155 probe_ent->udma_mask = port0->udma_mask;
3156 probe_ent->port_ops = port0->port_ops;
3159 probe_ent->port[0].cmd_addr = 0x1f0;
3160 probe_ent->port[0].altstatus_addr =
3161 probe_ent->port[0].ctl_addr = 0x3f6;
3162 probe_ent->n_ports = 1;
3163 probe_ent->irq = 14;
3164 ata_std_ports(&probe_ent->port[0]);
3166 probe_ent2->port[0].cmd_addr = 0x170;
3167 probe_ent2->port[0].altstatus_addr =
3168 probe_ent2->port[0].ctl_addr = 0x376;
3169 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3170 probe_ent2->n_ports = 1;
3171 probe_ent2->irq = 15;
3172 ata_std_ports(&probe_ent2->port[0]);
3174 probe_ent2->sht = port1->sht;
3175 probe_ent2->host_flags = port1->host_flags;
3176 probe_ent2->pio_mask = port1->pio_mask;
3177 probe_ent2->udma_mask = port1->udma_mask;
3178 probe_ent2->port_ops = port1->port_ops;
3180 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3181 ata_std_ports(&probe_ent->port[0]);
3182 probe_ent->port[0].altstatus_addr =
3183 probe_ent->port[0].ctl_addr =
3184 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3186 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3187 ata_std_ports(&probe_ent->port[1]);
3188 probe_ent->port[1].altstatus_addr =
3189 probe_ent->port[1].ctl_addr =
3190 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3191 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3193 probe_ent->n_ports = 2;
3194 probe_ent->irq = pdev->irq;
3195 probe_ent->irq_flags = SA_SHIRQ;
3198 pci_set_master(pdev);
3200 /* FIXME: check ata_device_add return */
3202 if (legacy_mode & (1 << 0))
3203 ata_device_add(probe_ent);
3204 if (legacy_mode & (1 << 1))
3205 ata_device_add(probe_ent2);
3208 ata_device_add(probe_ent);
3209 assert(probe_ent2 == NULL);
3218 if (legacy_mode & (1 << 0))
3219 release_region(0x1f0, 8);
3220 if (legacy_mode & (1 << 1))
3221 release_region(0x170, 8);
3222 pci_release_regions(pdev);
3224 pci_disable_device(pdev);
3229 * ata_pci_remove_one - PCI layer callback for device removal
3230 * @pdev: PCI device that was removed
3232 * PCI layer indicates to libata via this hook that
3233 * hot-unplug or module unload event has occured.
3234 * Handle this by unregistering all objects associated
3235 * with this PCI device. Free those objects. Then finally
3236 * release PCI resources and disable device.
3239 * Inherited from PCI layer (may sleep).
3242 void ata_pci_remove_one (struct pci_dev *pdev)
3244 struct ata_host_set *host_set = pci_get_drvdata(pdev);
3245 struct ata_port *ap;
3248 for (i = 0; i < host_set->n_ports; i++) {
3249 ap = host_set->ports[i];
3251 scsi_remove_host(ap->host);
3254 free_irq(host_set->irq, host_set);
3255 if (host_set->ops->host_stop)
3256 host_set->ops->host_stop(host_set);
3257 if (host_set->mmio_base)
3258 iounmap(host_set->mmio_base);
3260 for (i = 0; i < host_set->n_ports; i++) {
3261 ap = host_set->ports[i];
3263 ata_scsi_release(ap->host);
3264 scsi_host_put(ap->host);
3267 pci_release_regions(pdev);
3269 for (i = 0; i < host_set->n_ports; i++) {
3270 struct ata_ioports *ioaddr;
3272 ap = host_set->ports[i];
3273 ioaddr = &ap->ioaddr;
3275 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
3276 if (ioaddr->cmd_addr == 0x1f0)
3277 release_region(0x1f0, 8);
3278 else if (ioaddr->cmd_addr == 0x170)
3279 release_region(0x170, 8);
3284 pci_disable_device(pdev);
3285 pci_set_drvdata(pdev, NULL);
3288 /* move to PCI subsystem */
3289 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
3291 unsigned long tmp = 0;
3293 switch (bits->width) {
3296 pci_read_config_byte(pdev, bits->reg, &tmp8);
3302 pci_read_config_word(pdev, bits->reg, &tmp16);
3308 pci_read_config_dword(pdev, bits->reg, &tmp32);
3319 return (tmp == bits->val) ? 1 : 0;
3332 static int __init ata_init(void)
3334 ata_wq = create_workqueue("ata");
3338 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
3342 static void __exit ata_exit(void)
3344 destroy_workqueue(ata_wq);
3347 module_init(ata_init);
3348 module_exit(ata_exit);
3351 * libata is essentially a library of internal helper functions for
3352 * low-level ATA host controller drivers. As such, the API/ABI is
3353 * likely to change as new drivers are added and updated.
3354 * Do not depend on ABI/API stability.
3357 EXPORT_SYMBOL_GPL(pci_test_config_bits);
3358 EXPORT_SYMBOL_GPL(ata_std_bios_param);
3359 EXPORT_SYMBOL_GPL(ata_std_ports);
3360 EXPORT_SYMBOL_GPL(ata_device_add);
3361 EXPORT_SYMBOL_GPL(ata_sg_init);
3362 EXPORT_SYMBOL_GPL(ata_sg_init_one);
3363 EXPORT_SYMBOL_GPL(ata_qc_complete);
3364 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
3365 EXPORT_SYMBOL_GPL(ata_eng_timeout);
3366 EXPORT_SYMBOL_GPL(ata_tf_load_pio);
3367 EXPORT_SYMBOL_GPL(ata_tf_load_mmio);
3368 EXPORT_SYMBOL_GPL(ata_tf_read_pio);
3369 EXPORT_SYMBOL_GPL(ata_tf_read_mmio);
3370 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
3371 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
3372 EXPORT_SYMBOL_GPL(ata_check_status_pio);
3373 EXPORT_SYMBOL_GPL(ata_check_status_mmio);
3374 EXPORT_SYMBOL_GPL(ata_exec_command_pio);
3375 EXPORT_SYMBOL_GPL(ata_exec_command_mmio);
3376 EXPORT_SYMBOL_GPL(ata_port_start);
3377 EXPORT_SYMBOL_GPL(ata_port_stop);
3378 EXPORT_SYMBOL_GPL(ata_interrupt);
3379 EXPORT_SYMBOL_GPL(ata_qc_prep);
3380 EXPORT_SYMBOL_GPL(ata_bmdma_setup_pio);
3381 EXPORT_SYMBOL_GPL(ata_bmdma_start_pio);
3382 EXPORT_SYMBOL_GPL(ata_bmdma_setup_mmio);
3383 EXPORT_SYMBOL_GPL(ata_bmdma_start_mmio);
3384 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
3385 EXPORT_SYMBOL_GPL(ata_port_probe);
3386 EXPORT_SYMBOL_GPL(sata_phy_reset);
3387 EXPORT_SYMBOL_GPL(ata_bus_reset);
3388 EXPORT_SYMBOL_GPL(ata_port_disable);
3389 EXPORT_SYMBOL_GPL(ata_pci_init_one);
3390 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
3391 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
3392 EXPORT_SYMBOL_GPL(ata_scsi_error);
3393 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
3394 EXPORT_SYMBOL_GPL(ata_scsi_release);
3395 EXPORT_SYMBOL_GPL(ata_host_intr);
3396 EXPORT_SYMBOL_GPL(ata_dev_id_string);