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_dma_complete(struct ata_queued_cmd *qc, u8 host_stat);
54 static void ata_host_set_pio(struct ata_port *ap);
55 static void ata_host_set_udma(struct ata_port *ap);
56 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device);
57 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device);
58 static void ata_set_mode(struct ata_port *ap);
59 static int ata_qc_issue_prot(struct ata_queued_cmd *qc);
61 static unsigned int ata_unique_id = 1;
62 static struct workqueue_struct *ata_wq;
64 MODULE_AUTHOR("Jeff Garzik");
65 MODULE_DESCRIPTION("Library module for ATA devices");
66 MODULE_LICENSE("GPL");
68 static const char * thr_state_name[] = {
79 * ata_thr_state_name - convert thread state enum to string
80 * @thr_state: thread state to be converted to string
82 * Converts the specified thread state id to a constant C string.
88 * The THR_xxx-prefixed string naming the specified thread
89 * state id, or the string "<invalid THR_xxx state>".
92 static const char *ata_thr_state_name(unsigned int thr_state)
94 if (thr_state < ARRAY_SIZE(thr_state_name))
95 return thr_state_name[thr_state];
96 return "<invalid THR_xxx state>";
100 * ata_tf_load_pio - send taskfile registers to host controller
101 * @ap: Port to which output is sent
102 * @tf: ATA taskfile register set
104 * Outputs ATA taskfile to standard ATA host controller using PIO.
107 * Inherited from caller.
110 void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
112 struct ata_ioports *ioaddr = &ap->ioaddr;
113 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
115 if (tf->ctl != ap->last_ctl) {
116 outb(tf->ctl, ioaddr->ctl_addr);
117 ap->last_ctl = tf->ctl;
121 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
122 outb(tf->hob_feature, ioaddr->feature_addr);
123 outb(tf->hob_nsect, ioaddr->nsect_addr);
124 outb(tf->hob_lbal, ioaddr->lbal_addr);
125 outb(tf->hob_lbam, ioaddr->lbam_addr);
126 outb(tf->hob_lbah, ioaddr->lbah_addr);
127 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
136 outb(tf->feature, ioaddr->feature_addr);
137 outb(tf->nsect, ioaddr->nsect_addr);
138 outb(tf->lbal, ioaddr->lbal_addr);
139 outb(tf->lbam, ioaddr->lbam_addr);
140 outb(tf->lbah, ioaddr->lbah_addr);
141 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
149 if (tf->flags & ATA_TFLAG_DEVICE) {
150 outb(tf->device, ioaddr->device_addr);
151 VPRINTK("device 0x%X\n", tf->device);
158 * ata_tf_load_mmio - send taskfile registers to host controller
159 * @ap: Port to which output is sent
160 * @tf: ATA taskfile register set
162 * Outputs ATA taskfile to standard ATA host controller using MMIO.
165 * Inherited from caller.
168 void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
170 struct ata_ioports *ioaddr = &ap->ioaddr;
171 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
173 if (tf->ctl != ap->last_ctl) {
174 writeb(tf->ctl, ap->ioaddr.ctl_addr);
175 ap->last_ctl = tf->ctl;
179 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
180 writeb(tf->hob_feature, (void *) ioaddr->feature_addr);
181 writeb(tf->hob_nsect, (void *) ioaddr->nsect_addr);
182 writeb(tf->hob_lbal, (void *) ioaddr->lbal_addr);
183 writeb(tf->hob_lbam, (void *) ioaddr->lbam_addr);
184 writeb(tf->hob_lbah, (void *) ioaddr->lbah_addr);
185 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
194 writeb(tf->feature, (void *) ioaddr->feature_addr);
195 writeb(tf->nsect, (void *) ioaddr->nsect_addr);
196 writeb(tf->lbal, (void *) ioaddr->lbal_addr);
197 writeb(tf->lbam, (void *) ioaddr->lbam_addr);
198 writeb(tf->lbah, (void *) ioaddr->lbah_addr);
199 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
207 if (tf->flags & ATA_TFLAG_DEVICE) {
208 writeb(tf->device, (void *) ioaddr->device_addr);
209 VPRINTK("device 0x%X\n", tf->device);
216 * ata_exec_command_pio - issue ATA command to host controller
217 * @ap: port to which command is being issued
218 * @tf: ATA taskfile register set
220 * Issues PIO write to ATA command register, with proper
221 * synchronization with interrupt handler / other threads.
224 * spin_lock_irqsave(host_set lock)
227 void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
229 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
231 outb(tf->command, ap->ioaddr.command_addr);
237 * ata_exec_command_mmio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues MMIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
245 * spin_lock_irqsave(host_set lock)
248 void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252 writeb(tf->command, (void *) ap->ioaddr.command_addr);
257 * ata_exec - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
261 * Issues PIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
265 * Obtains host_set lock.
268 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273 spin_lock_irqsave(&ap->host_set->lock, flags);
274 ap->ops->exec_command(ap, tf);
275 spin_unlock_irqrestore(&ap->host_set->lock, flags);
279 * ata_tf_to_host - issue ATA taskfile to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues ATA taskfile register set to ATA host controller,
284 * via PIO, with proper synchronization with interrupt handler and
288 * Obtains host_set lock.
291 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
293 ap->ops->tf_load(ap, tf);
299 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
303 * Issues ATA taskfile register set to ATA host controller,
304 * via PIO, with proper synchronization with interrupt handler and
308 * spin_lock_irqsave(host_set lock)
311 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
322 * Reads ATA taskfile registers for currently-selected device
326 * Inherited from caller.
329 void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331 struct ata_ioports *ioaddr = &ap->ioaddr;
333 tf->nsect = inb(ioaddr->nsect_addr);
334 tf->lbal = inb(ioaddr->lbal_addr);
335 tf->lbam = inb(ioaddr->lbam_addr);
336 tf->lbah = inb(ioaddr->lbah_addr);
337 tf->device = inb(ioaddr->device_addr);
339 if (tf->flags & ATA_TFLAG_LBA48) {
340 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
341 tf->hob_feature = inb(ioaddr->error_addr);
342 tf->hob_nsect = inb(ioaddr->nsect_addr);
343 tf->hob_lbal = inb(ioaddr->lbal_addr);
344 tf->hob_lbam = inb(ioaddr->lbam_addr);
345 tf->hob_lbah = inb(ioaddr->lbah_addr);
350 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
351 * @ap: Port from which input is read
352 * @tf: ATA taskfile register set for storing input
354 * Reads ATA taskfile registers for currently-selected device
358 * Inherited from caller.
361 void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
363 struct ata_ioports *ioaddr = &ap->ioaddr;
365 tf->nsect = readb((void *)ioaddr->nsect_addr);
366 tf->lbal = readb((void *)ioaddr->lbal_addr);
367 tf->lbam = readb((void *)ioaddr->lbam_addr);
368 tf->lbah = readb((void *)ioaddr->lbah_addr);
369 tf->device = readb((void *)ioaddr->device_addr);
371 if (tf->flags & ATA_TFLAG_LBA48) {
372 writeb(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr);
373 tf->hob_feature = readb((void *)ioaddr->error_addr);
374 tf->hob_nsect = readb((void *)ioaddr->nsect_addr);
375 tf->hob_lbal = readb((void *)ioaddr->lbal_addr);
376 tf->hob_lbam = readb((void *)ioaddr->lbam_addr);
377 tf->hob_lbah = readb((void *)ioaddr->lbah_addr);
382 * ata_check_status_pio - Read device status reg & clear interrupt
383 * @ap: port where the device is
385 * Reads ATA taskfile status register for currently-selected device
386 * via PIO and return it's value. This also clears pending interrupts
390 * Inherited from caller.
392 u8 ata_check_status_pio(struct ata_port *ap)
394 return inb(ap->ioaddr.status_addr);
398 * ata_check_status_mmio - Read device status reg & clear interrupt
399 * @ap: port where the device is
401 * Reads ATA taskfile status register for currently-selected device
402 * via MMIO and return it's value. This also clears pending interrupts
406 * Inherited from caller.
408 u8 ata_check_status_mmio(struct ata_port *ap)
410 return readb((void *) ap->ioaddr.status_addr);
414 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
415 * @tf: Taskfile to convert
416 * @fis: Buffer into which data will output
417 * @pmp: Port multiplier port
419 * Converts a standard ATA taskfile to a Serial ATA
420 * FIS structure (Register - Host to Device).
423 * Inherited from caller.
426 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
428 fis[0] = 0x27; /* Register - Host to Device FIS */
429 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
430 bit 7 indicates Command FIS */
431 fis[2] = tf->command;
432 fis[3] = tf->feature;
439 fis[8] = tf->hob_lbal;
440 fis[9] = tf->hob_lbam;
441 fis[10] = tf->hob_lbah;
442 fis[11] = tf->hob_feature;
445 fis[13] = tf->hob_nsect;
456 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
457 * @fis: Buffer from which data will be input
458 * @tf: Taskfile to output
460 * Converts a standard ATA taskfile to a Serial ATA
461 * FIS structure (Register - Host to Device).
464 * Inherited from caller.
467 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
469 tf->command = fis[2]; /* status */
470 tf->feature = fis[3]; /* error */
477 tf->hob_lbal = fis[8];
478 tf->hob_lbam = fis[9];
479 tf->hob_lbah = fis[10];
482 tf->hob_nsect = fis[13];
486 * ata_prot_to_cmd - determine which read/write opcodes to use
487 * @protocol: ATA_PROT_xxx taskfile protocol
488 * @lba48: true is lba48 is present
490 * Given necessary input, determine which read/write commands
491 * to use to transfer data.
496 static int ata_prot_to_cmd(int protocol, int lba48)
498 int rcmd = 0, wcmd = 0;
503 rcmd = ATA_CMD_PIO_READ_EXT;
504 wcmd = ATA_CMD_PIO_WRITE_EXT;
506 rcmd = ATA_CMD_PIO_READ;
507 wcmd = ATA_CMD_PIO_WRITE;
513 rcmd = ATA_CMD_READ_EXT;
514 wcmd = ATA_CMD_WRITE_EXT;
517 wcmd = ATA_CMD_WRITE;
525 return rcmd | (wcmd << 8);
529 * ata_dev_set_protocol - set taskfile protocol and r/w commands
530 * @dev: device to examine and configure
532 * Examine the device configuration, after we have
533 * read the identify-device page and configured the
534 * data transfer mode. Set internal state related to
535 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
536 * and calculate the proper read/write commands to use.
541 static void ata_dev_set_protocol(struct ata_device *dev)
543 int pio = (dev->flags & ATA_DFLAG_PIO);
544 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
548 proto = dev->xfer_protocol = ATA_PROT_PIO;
550 proto = dev->xfer_protocol = ATA_PROT_DMA;
552 cmd = ata_prot_to_cmd(proto, lba48);
556 dev->read_cmd = cmd & 0xff;
557 dev->write_cmd = (cmd >> 8) & 0xff;
560 static const char * udma_str[] = {
572 * ata_udma_string - convert UDMA bit offset to string
573 * @udma_mask: mask of bits supported; only highest bit counts.
575 * Determine string which represents the highest speed
576 * (highest bit in @udma_mask).
582 * Constant C string representing highest speed listed in
583 * @udma_mask, or the constant C string "<n/a>".
586 static const char *ata_udma_string(unsigned int udma_mask)
590 for (i = 7; i >= 0; i--) {
591 if (udma_mask & (1 << i))
599 * ata_pio_devchk - PATA device presence detection
600 * @ap: ATA channel to examine
601 * @device: Device to examine (starting at zero)
603 * This technique was originally described in
604 * Hale Landis's ATADRVR (www.ata-atapi.com), and
605 * later found its way into the ATA/ATAPI spec.
607 * Write a pattern to the ATA shadow registers,
608 * and if a device is present, it will respond by
609 * correctly storing and echoing back the
610 * ATA shadow register contents.
616 static unsigned int ata_pio_devchk(struct ata_port *ap,
619 struct ata_ioports *ioaddr = &ap->ioaddr;
622 __ata_dev_select(ap, device);
624 outb(0x55, ioaddr->nsect_addr);
625 outb(0xaa, ioaddr->lbal_addr);
627 outb(0xaa, ioaddr->nsect_addr);
628 outb(0x55, ioaddr->lbal_addr);
630 outb(0x55, ioaddr->nsect_addr);
631 outb(0xaa, ioaddr->lbal_addr);
633 nsect = inb(ioaddr->nsect_addr);
634 lbal = inb(ioaddr->lbal_addr);
636 if ((nsect == 0x55) && (lbal == 0xaa))
637 return 1; /* we found a device */
639 return 0; /* nothing found */
643 * ata_mmio_devchk - PATA device presence detection
644 * @ap: ATA channel to examine
645 * @device: Device to examine (starting at zero)
647 * This technique was originally described in
648 * Hale Landis's ATADRVR (www.ata-atapi.com), and
649 * later found its way into the ATA/ATAPI spec.
651 * Write a pattern to the ATA shadow registers,
652 * and if a device is present, it will respond by
653 * correctly storing and echoing back the
654 * ATA shadow register contents.
660 static unsigned int ata_mmio_devchk(struct ata_port *ap,
663 struct ata_ioports *ioaddr = &ap->ioaddr;
666 __ata_dev_select(ap, device);
668 writeb(0x55, (void *) ioaddr->nsect_addr);
669 writeb(0xaa, (void *) ioaddr->lbal_addr);
671 writeb(0xaa, (void *) ioaddr->nsect_addr);
672 writeb(0x55, (void *) ioaddr->lbal_addr);
674 writeb(0x55, (void *) ioaddr->nsect_addr);
675 writeb(0xaa, (void *) ioaddr->lbal_addr);
677 nsect = readb((void *) ioaddr->nsect_addr);
678 lbal = readb((void *) ioaddr->lbal_addr);
680 if ((nsect == 0x55) && (lbal == 0xaa))
681 return 1; /* we found a device */
683 return 0; /* nothing found */
687 * ata_dev_devchk - PATA device presence detection
688 * @ap: ATA channel to examine
689 * @device: Device to examine (starting at zero)
691 * Dispatch ATA device presence detection, depending
692 * on whether we are using PIO or MMIO to talk to the
693 * ATA shadow registers.
699 static unsigned int ata_dev_devchk(struct ata_port *ap,
702 if (ap->flags & ATA_FLAG_MMIO)
703 return ata_mmio_devchk(ap, device);
704 return ata_pio_devchk(ap, device);
708 * ata_dev_classify - determine device type based on ATA-spec signature
709 * @tf: ATA taskfile register set for device to be identified
711 * Determine from taskfile register contents whether a device is
712 * ATA or ATAPI, as per "Signature and persistence" section
713 * of ATA/PI spec (volume 1, sect 5.14).
719 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
720 * the event of failure.
723 static unsigned int ata_dev_classify(struct ata_taskfile *tf)
725 /* Apple's open source Darwin code hints that some devices only
726 * put a proper signature into the LBA mid/high registers,
727 * So, we only check those. It's sufficient for uniqueness.
730 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
731 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
732 DPRINTK("found ATA device by sig\n");
736 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
737 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
738 DPRINTK("found ATAPI device by sig\n");
739 return ATA_DEV_ATAPI;
742 DPRINTK("unknown device\n");
743 return ATA_DEV_UNKNOWN;
747 * ata_dev_try_classify - Parse returned ATA device signature
748 * @ap: ATA channel to examine
749 * @device: Device to examine (starting at zero)
751 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
752 * an ATA/ATAPI-defined set of values is placed in the ATA
753 * shadow registers, indicating the results of device detection
756 * Select the ATA device, and read the values from the ATA shadow
757 * registers. Then parse according to the Error register value,
758 * and the spec-defined values examined by ata_dev_classify().
764 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
766 struct ata_device *dev = &ap->device[device];
767 struct ata_taskfile tf;
771 __ata_dev_select(ap, device);
773 memset(&tf, 0, sizeof(tf));
775 err = ata_chk_err(ap);
776 ap->ops->tf_read(ap, &tf);
778 dev->class = ATA_DEV_NONE;
780 /* see if device passed diags */
783 else if ((device == 0) && (err == 0x81))
788 /* determine if device if ATA or ATAPI */
789 class = ata_dev_classify(&tf);
790 if (class == ATA_DEV_UNKNOWN)
792 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
801 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
802 * @dev: Device whose IDENTIFY DEVICE results we will examine
803 * @s: string into which data is output
804 * @ofs: offset into identify device page
805 * @len: length of string to return. must be an even number.
807 * The strings in the IDENTIFY DEVICE page are broken up into
808 * 16-bit chunks. Run through the string, and output each
809 * 8-bit chunk linearly, regardless of platform.
815 void ata_dev_id_string(struct ata_device *dev, unsigned char *s,
816 unsigned int ofs, unsigned int len)
821 c = dev->id[ofs] >> 8;
825 c = dev->id[ofs] & 0xff;
835 * __ata_dev_select - Select device 0/1 on ATA bus
836 * @ap: ATA channel to manipulate
837 * @device: ATA device (numbered from zero) to select
839 * Use the method defined in the ATA specification to
840 * make either device 0, or device 1, active on the
847 static void __ata_dev_select (struct ata_port *ap, unsigned int device)
852 tmp = ATA_DEVICE_OBS;
854 tmp = ATA_DEVICE_OBS | ATA_DEV1;
856 if (ap->flags & ATA_FLAG_MMIO) {
857 writeb(tmp, (void *) ap->ioaddr.device_addr);
859 outb(tmp, ap->ioaddr.device_addr);
861 ata_pause(ap); /* needed; also flushes, for mmio */
865 * ata_dev_select - Select device 0/1 on ATA bus
866 * @ap: ATA channel to manipulate
867 * @device: ATA device (numbered from zero) to select
868 * @wait: non-zero to wait for Status register BSY bit to clear
869 * @can_sleep: non-zero if context allows sleeping
871 * Use the method defined in the ATA specification to
872 * make either device 0, or device 1, active on the
875 * This is a high-level version of __ata_dev_select(),
876 * which additionally provides the services of inserting
877 * the proper pauses and status polling, where needed.
883 void ata_dev_select(struct ata_port *ap, unsigned int device,
884 unsigned int wait, unsigned int can_sleep)
886 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
887 ap->id, device, wait);
892 __ata_dev_select(ap, device);
895 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
902 * ata_dump_id - IDENTIFY DEVICE info debugging output
903 * @dev: Device whose IDENTIFY DEVICE page we will dump
905 * Dump selected 16-bit words from a detected device's
906 * IDENTIFY PAGE page.
912 static inline void ata_dump_id(struct ata_device *dev)
914 DPRINTK("49==0x%04x "
924 DPRINTK("80==0x%04x "
934 DPRINTK("88==0x%04x "
941 * ata_dev_identify - obtain IDENTIFY x DEVICE page
942 * @ap: port on which device we wish to probe resides
943 * @device: device bus address, starting at zero
945 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
946 * command, and read back the 512-byte device information page.
947 * The device information page is fed to us via the standard
948 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
949 * using standard PIO-IN paths)
951 * After reading the device information page, we use several
952 * bits of information from it to initialize data structures
953 * that will be used during the lifetime of the ata_device.
954 * Other data from the info page is used to disqualify certain
955 * older ATA devices we do not wish to support.
958 * Inherited from caller. Some functions called by this function
959 * obtain the host_set lock.
962 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
964 struct ata_device *dev = &ap->device[device];
968 struct ata_taskfile tf;
969 unsigned int using_edd;
971 if (!ata_dev_present(dev)) {
972 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
977 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
982 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
984 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
985 dev->class == ATA_DEV_NONE);
987 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
990 ata_tf_init(ap, &tf, device);
992 tf.protocol = ATA_PROT_PIO;
994 if (dev->class == ATA_DEV_ATA) {
995 tf.command = ATA_CMD_ID_ATA;
996 DPRINTK("do ATA identify\n");
998 tf.command = ATA_CMD_ID_ATAPI;
999 DPRINTK("do ATAPI identify\n");
1002 ata_tf_to_host(ap, &tf);
1004 /* crazy ATAPI devices... */
1005 if (dev->class == ATA_DEV_ATAPI)
1008 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT))
1011 status = ata_chk_status(ap);
1012 if (status & ATA_ERR) {
1014 * arg! EDD works for all test cases, but seems to return
1015 * the ATA signature for some ATAPI devices. Until the
1016 * reason for this is found and fixed, we fix up the mess
1017 * here. If IDENTIFY DEVICE returns command aborted
1018 * (as ATAPI devices do), then we issue an
1019 * IDENTIFY PACKET DEVICE.
1021 * ATA software reset (SRST, the default) does not appear
1022 * to have this problem.
1024 if ((using_edd) && (tf.command == ATA_CMD_ID_ATA)) {
1025 u8 err = ata_chk_err(ap);
1026 if (err & ATA_ABORTED) {
1027 dev->class = ATA_DEV_ATAPI;
1034 /* make sure we have BSY=0, DRQ=1 */
1035 if ((status & ATA_DRQ) == 0) {
1036 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) not returning id page (0x%x)\n",
1038 dev->class == ATA_DEV_ATA ? "" : "PI",
1043 /* read IDENTIFY [X] DEVICE page */
1044 if (ap->flags & ATA_FLAG_MMIO) {
1045 for (i = 0; i < ATA_ID_WORDS; i++)
1046 dev->id[i] = readw((void *)ap->ioaddr.data_addr);
1048 for (i = 0; i < ATA_ID_WORDS; i++)
1049 dev->id[i] = inw(ap->ioaddr.data_addr);
1051 /* wait for host_idle */
1052 status = ata_wait_idle(ap);
1053 if (status & (ATA_BUSY | ATA_DRQ)) {
1054 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) error after id page (0x%x)\n",
1056 dev->class == ATA_DEV_ATA ? "" : "PI",
1061 ata_irq_on(ap); /* re-enable interrupts */
1063 /* print device capabilities */
1064 printk(KERN_DEBUG "ata%u: dev %u cfg "
1065 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1066 ap->id, device, dev->id[49],
1067 dev->id[82], dev->id[83], dev->id[84],
1068 dev->id[85], dev->id[86], dev->id[87],
1072 * common ATA, ATAPI feature tests
1075 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1076 if (!ata_id_has_dma(dev) || !ata_id_has_lba(dev)) {
1077 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1081 /* we require UDMA support */
1083 tmp = dev->id[ATA_ID_UDMA_MODES];
1084 if ((tmp & 0xff) == 0) {
1085 printk(KERN_DEBUG "ata%u: no udma\n", ap->id);
1091 /* ATA-specific feature tests */
1092 if (dev->class == ATA_DEV_ATA) {
1093 if (!ata_id_is_ata(dev)) /* sanity check */
1096 tmp = dev->id[ATA_ID_MAJOR_VER];
1097 for (i = 14; i >= 1; i--)
1101 /* we require at least ATA-3 */
1103 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1107 if (ata_id_has_lba48(dev)) {
1108 dev->flags |= ATA_DFLAG_LBA48;
1109 dev->n_sectors = ata_id_u64(dev, 100);
1111 dev->n_sectors = ata_id_u32(dev, 60);
1114 ap->host->max_cmd_len = 16;
1116 /* print device info to dmesg */
1117 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1119 ata_udma_string(udma_modes),
1120 (unsigned long long)dev->n_sectors,
1121 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1124 /* ATAPI-specific feature tests */
1126 if (ata_id_is_ata(dev)) /* sanity check */
1129 /* see if 16-byte commands supported */
1130 tmp = dev->id[0] & 0x3;
1132 ap->host->max_cmd_len = 16;
1134 /* print device info to dmesg */
1135 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1137 ata_udma_string(udma_modes));
1140 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1144 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1147 ata_irq_on(ap); /* re-enable interrupts */
1148 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1149 DPRINTK("EXIT, err\n");
1159 static void ata_port_reset(struct ata_port *ap)
1161 unsigned int i, found = 0;
1163 ap->ops->phy_reset(ap);
1164 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1167 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1168 ata_dev_identify(ap, i);
1169 if (ata_dev_present(&ap->device[i])) {
1171 if (ap->ops->dev_config)
1172 ap->ops->dev_config(ap, &ap->device[i]);
1176 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1177 goto err_out_disable;
1180 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1181 goto err_out_disable;
1183 ap->thr_state = THR_PROBE_SUCCESS;
1188 ap->ops->port_disable(ap);
1190 ap->thr_state = THR_PROBE_FAILED;
1200 void ata_port_probe(struct ata_port *ap)
1202 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1212 void sata_phy_reset(struct ata_port *ap)
1215 unsigned long timeout = jiffies + (HZ * 5);
1217 if (ap->flags & ATA_FLAG_SATA_RESET) {
1218 scr_write(ap, SCR_CONTROL, 0x301); /* issue phy wake/reset */
1219 scr_read(ap, SCR_STATUS); /* dummy read; flush */
1220 udelay(400); /* FIXME: a guess */
1222 scr_write(ap, SCR_CONTROL, 0x300); /* issue phy wake/clear reset */
1224 /* wait for phy to become ready, if necessary */
1227 sstatus = scr_read(ap, SCR_STATUS);
1228 if ((sstatus & 0xf) != 1)
1230 } while (time_before(jiffies, timeout));
1232 /* TODO: phy layer with polling, timeouts, etc. */
1233 if (sata_dev_present(ap))
1236 sstatus = scr_read(ap, SCR_STATUS);
1237 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1239 ata_port_disable(ap);
1242 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1245 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1246 ata_port_disable(ap);
1254 * ata_port_disable -
1260 void ata_port_disable(struct ata_port *ap)
1262 ap->device[0].class = ATA_DEV_NONE;
1263 ap->device[1].class = ATA_DEV_NONE;
1264 ap->flags |= ATA_FLAG_PORT_DISABLED;
1268 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1269 * @ap: port on which timings will be programmed
1274 static void ata_set_mode(struct ata_port *ap)
1276 unsigned int force_pio, i;
1278 ata_host_set_pio(ap);
1279 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1282 ata_host_set_udma(ap);
1283 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1286 #ifdef ATA_FORCE_PIO
1293 ata_dev_set_pio(ap, 0);
1294 ata_dev_set_pio(ap, 1);
1296 ata_dev_set_udma(ap, 0);
1297 ata_dev_set_udma(ap, 1);
1300 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1303 if (ap->ops->post_set_mode)
1304 ap->ops->post_set_mode(ap);
1306 for (i = 0; i < 2; i++) {
1307 struct ata_device *dev = &ap->device[i];
1308 ata_dev_set_protocol(dev);
1313 * ata_busy_sleep - sleep until BSY clears, or timeout
1314 * @ap: port containing status register to be polled
1315 * @tmout_pat: impatience timeout
1316 * @tmout: overall timeout
1322 static unsigned int ata_busy_sleep (struct ata_port *ap,
1323 unsigned long tmout_pat,
1324 unsigned long tmout)
1326 unsigned long timer_start, timeout;
1329 status = ata_busy_wait(ap, ATA_BUSY, 300);
1330 timer_start = jiffies;
1331 timeout = timer_start + tmout_pat;
1332 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1334 status = ata_busy_wait(ap, ATA_BUSY, 3);
1337 if (status & ATA_BUSY)
1338 printk(KERN_WARNING "ata%u is slow to respond, "
1339 "please be patient\n", ap->id);
1341 timeout = timer_start + tmout;
1342 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1344 status = ata_chk_status(ap);
1347 if (status & ATA_BUSY) {
1348 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1349 ap->id, tmout / HZ);
1356 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1358 struct ata_ioports *ioaddr = &ap->ioaddr;
1359 unsigned int dev0 = devmask & (1 << 0);
1360 unsigned int dev1 = devmask & (1 << 1);
1361 unsigned long timeout;
1363 /* if device 0 was found in ata_dev_devchk, wait for its
1367 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1369 /* if device 1 was found in ata_dev_devchk, wait for
1370 * register access, then wait for BSY to clear
1372 timeout = jiffies + ATA_TMOUT_BOOT;
1376 __ata_dev_select(ap, 1);
1377 if (ap->flags & ATA_FLAG_MMIO) {
1378 nsect = readb((void *) ioaddr->nsect_addr);
1379 lbal = readb((void *) ioaddr->lbal_addr);
1381 nsect = inb(ioaddr->nsect_addr);
1382 lbal = inb(ioaddr->lbal_addr);
1384 if ((nsect == 1) && (lbal == 1))
1386 if (time_after(jiffies, timeout)) {
1390 msleep(50); /* give drive a breather */
1393 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1395 /* is all this really necessary? */
1396 __ata_dev_select(ap, 0);
1398 __ata_dev_select(ap, 1);
1400 __ata_dev_select(ap, 0);
1411 static unsigned int ata_bus_edd(struct ata_port *ap)
1413 struct ata_taskfile tf;
1415 /* set up execute-device-diag (bus reset) taskfile */
1416 /* also, take interrupts to a known state (disabled) */
1417 DPRINTK("execute-device-diag\n");
1418 ata_tf_init(ap, &tf, 0);
1420 tf.command = ATA_CMD_EDD;
1421 tf.protocol = ATA_PROT_NODATA;
1424 ata_tf_to_host(ap, &tf);
1426 /* spec says at least 2ms. but who knows with those
1427 * crazy ATAPI devices...
1431 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1434 static unsigned int ata_bus_softreset(struct ata_port *ap,
1435 unsigned int devmask)
1437 struct ata_ioports *ioaddr = &ap->ioaddr;
1439 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1441 /* software reset. causes dev0 to be selected */
1442 if (ap->flags & ATA_FLAG_MMIO) {
1443 writeb(ap->ctl, ioaddr->ctl_addr);
1444 udelay(20); /* FIXME: flush */
1445 writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1446 udelay(20); /* FIXME: flush */
1447 writeb(ap->ctl, ioaddr->ctl_addr);
1449 outb(ap->ctl, ioaddr->ctl_addr);
1451 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1453 outb(ap->ctl, ioaddr->ctl_addr);
1456 /* spec mandates ">= 2ms" before checking status.
1457 * We wait 150ms, because that was the magic delay used for
1458 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1459 * between when the ATA command register is written, and then
1460 * status is checked. Because waiting for "a while" before
1461 * checking status is fine, post SRST, we perform this magic
1462 * delay here as well.
1466 ata_bus_post_reset(ap, devmask);
1472 * ata_bus_reset - reset host port and associated ATA channel
1473 * @ap: port to reset
1475 * This is typically the first time we actually start issuing
1476 * commands to the ATA channel. We wait for BSY to clear, then
1477 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1478 * result. Determine what devices, if any, are on the channel
1479 * by looking at the device 0/1 error register. Look at the signature
1480 * stored in each device's taskfile registers, to determine if
1481 * the device is ATA or ATAPI.
1484 * Inherited from caller. Some functions called by this function
1485 * obtain the host_set lock.
1488 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1491 void ata_bus_reset(struct ata_port *ap)
1493 struct ata_ioports *ioaddr = &ap->ioaddr;
1494 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1496 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1498 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1500 /* determine if device 0/1 are present */
1501 if (ap->flags & ATA_FLAG_SATA_RESET)
1504 dev0 = ata_dev_devchk(ap, 0);
1506 dev1 = ata_dev_devchk(ap, 1);
1510 devmask |= (1 << 0);
1512 devmask |= (1 << 1);
1514 /* select device 0 again */
1515 __ata_dev_select(ap, 0);
1517 /* issue bus reset */
1518 if (ap->flags & ATA_FLAG_SRST)
1519 rc = ata_bus_softreset(ap, devmask);
1520 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1521 /* set up device control */
1522 if (ap->flags & ATA_FLAG_MMIO)
1523 writeb(ap->ctl, ioaddr->ctl_addr);
1525 outb(ap->ctl, ioaddr->ctl_addr);
1526 rc = ata_bus_edd(ap);
1533 * determine by signature whether we have ATA or ATAPI devices
1535 err = ata_dev_try_classify(ap, 0);
1536 if ((slave_possible) && (err != 0x81))
1537 ata_dev_try_classify(ap, 1);
1539 /* re-enable interrupts */
1542 /* is double-select really necessary? */
1543 if (ap->device[1].class != ATA_DEV_NONE)
1544 __ata_dev_select(ap, 1);
1545 if (ap->device[0].class != ATA_DEV_NONE)
1546 __ata_dev_select(ap, 0);
1548 /* if no devices were detected, disable this port */
1549 if ((ap->device[0].class == ATA_DEV_NONE) &&
1550 (ap->device[1].class == ATA_DEV_NONE))
1553 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1554 /* set up device control for ATA_FLAG_SATA_RESET */
1555 if (ap->flags & ATA_FLAG_MMIO)
1556 writeb(ap->ctl, ioaddr->ctl_addr);
1558 outb(ap->ctl, ioaddr->ctl_addr);
1565 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1566 ap->ops->port_disable(ap);
1572 * ata_host_set_pio -
1578 static void ata_host_set_pio(struct ata_port *ap)
1580 struct ata_device *master, *slave;
1581 unsigned int pio, i;
1584 master = &ap->device[0];
1585 slave = &ap->device[1];
1587 assert (ata_dev_present(master) || ata_dev_present(slave));
1589 mask = ap->pio_mask;
1590 if (ata_dev_present(master))
1591 mask &= (master->id[ATA_ID_PIO_MODES] & 0x03);
1592 if (ata_dev_present(slave))
1593 mask &= (slave->id[ATA_ID_PIO_MODES] & 0x03);
1595 /* require pio mode 3 or 4 support for host and all devices */
1597 printk(KERN_WARNING "ata%u: no PIO3/4 support, ignoring\n",
1602 pio = (mask & ATA_ID_PIO4) ? 4 : 3;
1603 for (i = 0; i < ATA_MAX_DEVICES; i++)
1604 if (ata_dev_present(&ap->device[i])) {
1605 ap->device[i].pio_mode = (pio == 3) ?
1606 XFER_PIO_3 : XFER_PIO_4;
1607 if (ap->ops->set_piomode)
1608 ap->ops->set_piomode(ap, &ap->device[i], pio);
1614 ap->ops->port_disable(ap);
1618 * ata_host_set_udma -
1624 static void ata_host_set_udma(struct ata_port *ap)
1626 struct ata_device *master, *slave;
1631 master = &ap->device[0];
1632 slave = &ap->device[1];
1634 assert (ata_dev_present(master) || ata_dev_present(slave));
1635 assert ((ap->flags & ATA_FLAG_PORT_DISABLED) == 0);
1637 DPRINTK("udma masks: host 0x%X, master 0x%X, slave 0x%X\n",
1639 (!ata_dev_present(master)) ? 0xff :
1640 (master->id[ATA_ID_UDMA_MODES] & 0xff),
1641 (!ata_dev_present(slave)) ? 0xff :
1642 (slave->id[ATA_ID_UDMA_MODES] & 0xff));
1644 mask = ap->udma_mask;
1645 if (ata_dev_present(master))
1646 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1647 if (ata_dev_present(slave))
1648 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1651 while (i >= XFER_UDMA_0) {
1652 j = i - XFER_UDMA_0;
1653 DPRINTK("mask 0x%X i 0x%X j %u\n", mask, i, j);
1654 if (mask & (1 << j)) {
1662 /* require udma for host and all attached devices */
1663 if (udma_mode < 0) {
1664 printk(KERN_WARNING "ata%u: no UltraDMA support, ignoring\n",
1669 for (i = 0; i < ATA_MAX_DEVICES; i++)
1670 if (ata_dev_present(&ap->device[i])) {
1671 ap->device[i].udma_mode = udma_mode;
1672 if (ap->ops->set_udmamode)
1673 ap->ops->set_udmamode(ap, &ap->device[i],
1680 ap->ops->port_disable(ap);
1684 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1685 * @ap: Port associated with device @dev
1686 * @dev: Device to which command will be sent
1691 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1693 struct ata_taskfile tf;
1695 /* set up set-features taskfile */
1696 DPRINTK("set features - xfer mode\n");
1697 ata_tf_init(ap, &tf, dev->devno);
1699 tf.command = ATA_CMD_SET_FEATURES;
1700 tf.feature = SETFEATURES_XFER;
1701 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1702 tf.protocol = ATA_PROT_NODATA;
1703 if (dev->flags & ATA_DFLAG_PIO)
1704 tf.nsect = dev->pio_mode;
1706 tf.nsect = dev->udma_mode;
1709 ata_tf_to_host(ap, &tf);
1711 /* crazy ATAPI devices... */
1712 if (dev->class == ATA_DEV_ATAPI)
1715 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1717 ata_irq_on(ap); /* re-enable interrupts */
1725 * ata_dev_set_udma - Set ATA device's transfer mode to Ultra DMA
1726 * @ap: Port associated with device @dev
1727 * @device: Device whose mode will be set
1732 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device)
1734 struct ata_device *dev = &ap->device[device];
1736 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1739 ata_dev_set_xfermode(ap, dev);
1741 assert((dev->udma_mode >= XFER_UDMA_0) &&
1742 (dev->udma_mode <= XFER_UDMA_7));
1743 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1745 udma_str[dev->udma_mode - XFER_UDMA_0]);
1749 * ata_dev_set_pio - Set ATA device's transfer mode to PIO
1750 * @ap: Port associated with device @dev
1751 * @device: Device whose mode will be set
1756 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device)
1758 struct ata_device *dev = &ap->device[device];
1760 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1763 /* force PIO mode */
1764 dev->flags |= ATA_DFLAG_PIO;
1766 ata_dev_set_xfermode(ap, dev);
1768 assert((dev->pio_mode >= XFER_PIO_3) &&
1769 (dev->pio_mode <= XFER_PIO_4));
1770 printk(KERN_INFO "ata%u: dev %u configured for PIO%c\n",
1772 dev->pio_mode == 3 ? '3' : '4');
1782 static void ata_sg_clean(struct ata_queued_cmd *qc)
1784 struct ata_port *ap = qc->ap;
1785 struct scsi_cmnd *cmd = qc->scsicmd;
1786 struct scatterlist *sg = qc->sg;
1787 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1789 assert(dir == SCSI_DATA_READ || dir == SCSI_DATA_WRITE);
1790 assert(qc->flags & ATA_QCFLAG_SG);
1794 assert(qc->n_elem == 1);
1796 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
1799 pci_unmap_sg(ap->host_set->pdev, sg, qc->n_elem, dir);
1801 pci_unmap_single(ap->host_set->pdev, sg_dma_address(&sg[0]),
1802 sg_dma_len(&sg[0]), dir);
1804 qc->flags &= ~ATA_QCFLAG_SG;
1815 void ata_fill_sg(struct ata_queued_cmd *qc)
1817 struct scatterlist *sg = qc->sg;
1818 struct ata_port *ap = qc->ap;
1819 unsigned int idx, nelem;
1822 assert(qc->n_elem > 0);
1825 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
1829 /* determine if physical DMA addr spans 64K boundary.
1830 * Note h/w doesn't support 64-bit, so we unconditionally
1831 * truncate dma_addr_t to u32.
1833 addr = (u32) sg_dma_address(sg);
1834 sg_len = sg_dma_len(sg);
1837 boundary = (addr & ~0xffff) + (0xffff + 1);
1839 if ((addr + sg_len) > boundary)
1840 len = boundary - addr;
1842 ap->prd[idx].addr = cpu_to_le32(addr);
1843 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
1844 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
1853 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
1857 * ata_sg_setup_one -
1861 * spin_lock_irqsave(host_set lock)
1867 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
1869 struct ata_port *ap = qc->ap;
1870 struct scsi_cmnd *cmd = qc->scsicmd;
1871 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1872 struct scatterlist *sg = qc->sg;
1873 unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1874 dma_addr_t dma_address;
1876 assert(sg == &qc->sgent);
1877 assert(qc->n_elem == 1);
1879 sg->page = virt_to_page(cmd->request_buffer);
1880 sg->offset = (unsigned long) cmd->request_buffer & ~PAGE_MASK;
1881 sg_dma_len(sg) = cmd->request_bufflen;
1886 dma_address = pci_map_single(ap->host_set->pdev, cmd->request_buffer,
1887 cmd->request_bufflen, dir);
1888 if (pci_dma_mapping_error(dma_address))
1891 sg_dma_address(sg) = dma_address;
1893 DPRINTK("mapped buffer of %d bytes for %s\n", cmd->request_bufflen,
1894 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1904 * spin_lock_irqsave(host_set lock)
1910 static int ata_sg_setup(struct ata_queued_cmd *qc)
1912 struct ata_port *ap = qc->ap;
1913 struct scsi_cmnd *cmd = qc->scsicmd;
1914 struct scatterlist *sg;
1916 unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1918 VPRINTK("ENTER, ata%u, use_sg %d\n", ap->id, cmd->use_sg);
1919 assert(cmd->use_sg > 0);
1921 sg = (struct scatterlist *)cmd->request_buffer;
1923 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1924 n_elem = pci_map_sg(ap->host_set->pdev, sg, cmd->use_sg, dir);
1927 DPRINTK("%d sg elements mapped\n", n_elem);
1929 n_elem = cmd->use_sg;
1931 qc->n_elem = n_elem;
1946 static unsigned long ata_pio_poll(struct ata_port *ap)
1949 unsigned int poll_state = PIO_ST_UNKNOWN;
1950 unsigned int reg_state = PIO_ST_UNKNOWN;
1951 const unsigned int tmout_state = PIO_ST_TMOUT;
1953 switch (ap->pio_task_state) {
1956 poll_state = PIO_ST_POLL;
1960 case PIO_ST_LAST_POLL:
1961 poll_state = PIO_ST_LAST_POLL;
1962 reg_state = PIO_ST_LAST;
1969 status = ata_chk_status(ap);
1970 if (status & ATA_BUSY) {
1971 if (time_after(jiffies, ap->pio_task_timeout)) {
1972 ap->pio_task_state = tmout_state;
1975 ap->pio_task_state = poll_state;
1976 return ATA_SHORT_PAUSE;
1979 ap->pio_task_state = reg_state;
1984 * ata_pio_complete -
1990 static void ata_pio_complete (struct ata_port *ap)
1992 struct ata_queued_cmd *qc;
1996 * This is purely hueristic. This is a fast path.
1997 * Sometimes when we enter, BSY will be cleared in
1998 * a chk-status or two. If not, the drive is probably seeking
1999 * or something. Snooze for a couple msecs, then
2000 * chk-status again. If still busy, fall back to
2001 * PIO_ST_POLL state.
2003 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2004 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2006 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2007 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2008 ap->pio_task_state = PIO_ST_LAST_POLL;
2009 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2014 drv_stat = ata_wait_idle(ap);
2015 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2016 ap->pio_task_state = PIO_ST_ERR;
2020 qc = ata_qc_from_tag(ap, ap->active_tag);
2023 ap->pio_task_state = PIO_ST_IDLE;
2027 ata_qc_complete(qc, drv_stat);
2037 static void ata_pio_sector(struct ata_port *ap)
2039 struct ata_queued_cmd *qc;
2040 struct scatterlist *sg;
2041 struct scsi_cmnd *cmd;
2046 * This is purely hueristic. This is a fast path.
2047 * Sometimes when we enter, BSY will be cleared in
2048 * a chk-status or two. If not, the drive is probably seeking
2049 * or something. Snooze for a couple msecs, then
2050 * chk-status again. If still busy, fall back to
2051 * PIO_ST_POLL state.
2053 status = ata_busy_wait(ap, ATA_BUSY, 5);
2054 if (status & ATA_BUSY) {
2056 status = ata_busy_wait(ap, ATA_BUSY, 10);
2057 if (status & ATA_BUSY) {
2058 ap->pio_task_state = PIO_ST_POLL;
2059 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2064 /* handle BSY=0, DRQ=0 as error */
2065 if ((status & ATA_DRQ) == 0) {
2066 ap->pio_task_state = PIO_ST_ERR;
2070 qc = ata_qc_from_tag(ap, ap->active_tag);
2076 if (qc->cursect == (qc->nsect - 1))
2077 ap->pio_task_state = PIO_ST_LAST;
2079 buf = kmap(sg[qc->cursg].page) +
2080 sg[qc->cursg].offset + (qc->cursg_ofs * ATA_SECT_SIZE);
2086 if ((qc->cursg_ofs * ATA_SECT_SIZE) == sg_dma_len(&sg[qc->cursg])) {
2091 DPRINTK("data %s, drv_stat 0x%X\n",
2092 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read",
2095 /* do the actual data transfer */
2096 /* FIXME: mmio-ize */
2097 if (qc->tf.flags & ATA_TFLAG_WRITE)
2098 outsl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2100 insl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2102 kunmap(sg[qc->cursg].page);
2105 static void ata_pio_task(void *_data)
2107 struct ata_port *ap = _data;
2108 unsigned long timeout = 0;
2110 switch (ap->pio_task_state) {
2116 ata_pio_complete(ap);
2120 case PIO_ST_LAST_POLL:
2121 timeout = ata_pio_poll(ap);
2125 printk(KERN_ERR "ata%d: FIXME: PIO_ST_TMOUT\n", /* FIXME */
2131 printk(KERN_ERR "ata%d: FIXME: PIO_ST_ERR\n", /* FIXME */
2137 if ((ap->pio_task_state != PIO_ST_IDLE) &&
2138 (ap->pio_task_state != PIO_ST_TMOUT) &&
2139 (ap->pio_task_state != PIO_ST_ERR)) {
2141 queue_delayed_work(ata_wq, &ap->pio_task,
2144 queue_work(ata_wq, &ap->pio_task);
2149 * ata_eng_timeout - Handle timeout of queued command
2150 * @ap: Port on which timed-out command is active
2152 * Some part of the kernel (currently, only the SCSI layer)
2153 * has noticed that the active command on port @ap has not
2154 * completed after a specified length of time. Handle this
2155 * condition by disabling DMA (if necessary) and completing
2156 * transactions, with error if necessary.
2158 * This also handles the case of the "lost interrupt", where
2159 * for some reason (possibly hardware bug, possibly driver bug)
2160 * an interrupt was not delivered to the driver, even though the
2161 * transaction completed successfully.
2164 * Inherited from SCSI layer (none, can sleep)
2167 void ata_eng_timeout(struct ata_port *ap)
2169 u8 host_stat, drv_stat;
2170 struct ata_queued_cmd *qc;
2174 qc = ata_qc_from_tag(ap, ap->active_tag);
2176 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2181 /* hack alert! We cannot use the supplied completion
2182 * function from inside the ->eh_strategy_handler() thread.
2183 * libata is the only user of ->eh_strategy_handler() in
2184 * any kernel, so the default scsi_done() assumes it is
2185 * not being called from the SCSI EH.
2187 qc->scsidone = scsi_finish_command;
2189 switch (qc->tf.protocol) {
2191 if (ap->flags & ATA_FLAG_MMIO) {
2192 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2193 host_stat = readb(mmio + ATA_DMA_STATUS);
2195 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2197 printk(KERN_ERR "ata%u: DMA timeout, stat 0x%x\n",
2200 ata_dma_complete(qc, host_stat);
2203 case ATA_PROT_NODATA:
2204 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2206 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x\n",
2207 ap->id, qc->tf.command, drv_stat);
2209 ata_qc_complete(qc, drv_stat);
2213 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2215 printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
2216 ap->id, qc->tf.command, drv_stat);
2218 ata_qc_complete(qc, drv_stat);
2227 * ata_qc_new - Request an available ATA command, for queueing
2228 * @ap: Port associated with device @dev
2229 * @dev: Device from whom we request an available command structure
2234 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2236 struct ata_queued_cmd *qc = NULL;
2239 for (i = 0; i < ATA_MAX_QUEUE; i++)
2240 if (!test_and_set_bit(i, &ap->qactive)) {
2241 qc = ata_qc_from_tag(ap, i);
2252 * ata_qc_new_init - Request an available ATA command, and initialize it
2253 * @ap: Port associated with device @dev
2254 * @dev: Device from whom we request an available command structure
2259 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2260 struct ata_device *dev)
2262 struct ata_queued_cmd *qc;
2264 qc = ata_qc_new(ap);
2271 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2274 ata_tf_init(ap, &qc->tf, dev->devno);
2276 if (likely((dev->flags & ATA_DFLAG_PIO) == 0))
2277 qc->flags |= ATA_QCFLAG_DMA;
2278 if (dev->flags & ATA_DFLAG_LBA48)
2279 qc->tf.flags |= ATA_TFLAG_LBA48;
2286 * ata_qc_complete - Complete an active ATA command
2287 * @qc: Command to complete
2288 * @drv_stat: ATA status register contents
2294 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2296 struct ata_port *ap = qc->ap;
2297 struct scsi_cmnd *cmd = qc->scsicmd;
2298 unsigned int tag, do_clear = 0;
2300 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2301 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2303 if (likely(qc->flags & ATA_QCFLAG_SG))
2307 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
2308 if (is_atapi_taskfile(&qc->tf))
2309 cmd->result = SAM_STAT_CHECK_CONDITION;
2311 ata_to_sense_error(qc);
2313 cmd->result = SAM_STAT_GOOD;
2319 qc->flags &= ~ATA_QCFLAG_ACTIVE;
2321 if (likely(ata_tag_valid(tag))) {
2322 if (tag == ap->active_tag)
2323 ap->active_tag = ATA_TAG_POISON;
2324 qc->tag = ATA_TAG_POISON;
2329 complete(qc->waiting);
2331 if (likely(do_clear))
2332 clear_bit(tag, &ap->qactive);
2336 * ata_qc_issue - issue taskfile to device
2337 * @qc: command to issue to device
2339 * Prepare an ATA command to submission to device.
2340 * This includes mapping the data into a DMA-able
2341 * area, filling in the S/G table, and finally
2342 * writing the taskfile to hardware, starting the command.
2345 * spin_lock_irqsave(host_set lock)
2348 * Zero on success, negative on error.
2351 int ata_qc_issue(struct ata_queued_cmd *qc)
2353 struct ata_port *ap = qc->ap;
2354 struct scsi_cmnd *cmd = qc->scsicmd;
2356 if (qc->flags & ATA_QCFLAG_SG) {
2357 /* set up SG table */
2359 if (ata_sg_setup(qc))
2362 if (ata_sg_setup_one(qc))
2366 ap->ops->fill_sg(qc);
2369 qc->ap->active_tag = qc->tag;
2370 qc->flags |= ATA_QCFLAG_ACTIVE;
2372 return ata_qc_issue_prot(qc);
2379 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
2380 * @qc: command to issue to device
2382 * Using various libata functions and hooks, this function
2383 * starts an ATA command. ATA commands are grouped into
2384 * classes called "protocols", and issuing each type of protocol
2385 * is slightly different.
2388 * spin_lock_irqsave(host_set lock)
2391 * Zero on success, negative on error.
2394 static int ata_qc_issue_prot(struct ata_queued_cmd *qc)
2396 struct ata_port *ap = qc->ap;
2398 ata_dev_select(ap, qc->dev->devno, 1, 0);
2400 switch (qc->tf.protocol) {
2401 case ATA_PROT_NODATA:
2402 ata_tf_to_host_nolock(ap, &qc->tf);
2406 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2407 ap->ops->bmdma_setup(qc); /* set up bmdma */
2408 ap->ops->bmdma_start(qc); /* initiate bmdma */
2411 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
2412 ata_qc_set_polling(qc);
2413 ata_tf_to_host_nolock(ap, &qc->tf);
2414 ap->pio_task_state = PIO_ST;
2415 queue_work(ata_wq, &ap->pio_task);
2418 case ATA_PROT_ATAPI:
2419 ata_tf_to_host_nolock(ap, &qc->tf);
2420 queue_work(ata_wq, &ap->packet_task);
2423 case ATA_PROT_ATAPI_DMA:
2424 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2425 ap->ops->bmdma_setup(qc); /* set up bmdma */
2426 queue_work(ata_wq, &ap->packet_task);
2438 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction (MMIO)
2439 * @qc: Info associated with this ATA transaction.
2442 * spin_lock_irqsave(host_set lock)
2445 void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
2447 struct ata_port *ap = qc->ap;
2448 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2449 u8 host_stat, dmactl;
2450 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2452 /* load PRD table addr. */
2453 mb(); /* make sure PRD table writes are visible to controller */
2454 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
2456 /* specify data direction, triple-check start bit is clear */
2457 dmactl = readb(mmio + ATA_DMA_CMD);
2458 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2460 dmactl |= ATA_DMA_WR;
2461 writeb(dmactl, mmio + ATA_DMA_CMD);
2463 /* clear interrupt, error bits */
2464 host_stat = readb(mmio + ATA_DMA_STATUS);
2465 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR, mmio + ATA_DMA_STATUS);
2467 /* issue r/w command */
2468 ap->ops->exec_command(ap, &qc->tf);
2472 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction (MMIO)
2473 * @qc: Info associated with this ATA transaction.
2476 * spin_lock_irqsave(host_set lock)
2479 void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
2481 struct ata_port *ap = qc->ap;
2482 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2485 /* start host DMA transaction */
2486 dmactl = readb(mmio + ATA_DMA_CMD);
2487 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
2489 /* Strictly, one may wish to issue a readb() here, to
2490 * flush the mmio write. However, control also passes
2491 * to the hardware at this point, and it will interrupt
2492 * us when we are to resume control. So, in effect,
2493 * we don't care when the mmio write flushes.
2494 * Further, a read of the DMA status register _immediately_
2495 * following the write may not be what certain flaky hardware
2496 * is expected, so I think it is best to not add a readb()
2497 * without first all the MMIO ATA cards/mobos.
2498 * Or maybe I'm just being paranoid.
2503 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
2504 * @qc: Info associated with this ATA transaction.
2507 * spin_lock_irqsave(host_set lock)
2510 void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
2512 struct ata_port *ap = qc->ap;
2513 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2514 u8 host_stat, dmactl;
2516 /* load PRD table addr. */
2517 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2519 /* specify data direction, triple-check start bit is clear */
2520 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2521 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2523 dmactl |= ATA_DMA_WR;
2524 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2526 /* clear interrupt, error bits */
2527 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2528 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2529 ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2531 /* issue r/w command */
2532 ap->ops->exec_command(ap, &qc->tf);
2536 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
2537 * @qc: Info associated with this ATA transaction.
2540 * spin_lock_irqsave(host_set lock)
2543 void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
2545 struct ata_port *ap = qc->ap;
2548 /* start host DMA transaction */
2549 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2550 outb(dmactl | ATA_DMA_START,
2551 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2555 * ata_dma_complete - Complete an active ATA BMDMA command
2556 * @qc: Command to complete
2557 * @host_stat: BMDMA status register contents
2562 static void ata_dma_complete(struct ata_queued_cmd *qc, u8 host_stat)
2564 struct ata_port *ap = qc->ap;
2567 if (ap->flags & ATA_FLAG_MMIO) {
2568 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2570 /* clear start/stop bit */
2571 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2572 mmio + ATA_DMA_CMD);
2574 /* ack intr, err bits */
2575 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2576 mmio + ATA_DMA_STATUS);
2578 /* clear start/stop bit */
2579 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
2580 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2582 /* ack intr, err bits */
2583 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2584 ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2588 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2589 ata_altstatus(ap); /* dummy read */
2591 DPRINTK("host %u, host_stat==0x%X, drv_stat==0x%X\n",
2592 ap->id, (u32) host_stat, (u32) ata_chk_status(ap));
2594 /* get drive status; clear intr; complete txn */
2595 ata_qc_complete(qc, ata_wait_idle(ap));
2599 * ata_host_intr - Handle host interrupt for given (port, task)
2600 * @ap: Port on which interrupt arrived (possibly...)
2601 * @qc: Taskfile currently active in engine
2603 * Handle host interrupt for given queued command. Currently,
2604 * only DMA interrupts are handled. All other commands are
2605 * handled via polling with interrupts disabled (nIEN bit).
2608 * spin_lock_irqsave(host_set lock)
2611 * One if interrupt was handled, zero if not (shared irq).
2614 inline unsigned int ata_host_intr (struct ata_port *ap,
2615 struct ata_queued_cmd *qc)
2617 u8 status, host_stat;
2618 unsigned int handled = 0;
2620 switch (qc->tf.protocol) {
2622 /* BMDMA completion */
2624 case ATA_PROT_ATAPI_DMA:
2625 if (ap->flags & ATA_FLAG_MMIO) {
2626 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2627 host_stat = readb(mmio + ATA_DMA_STATUS);
2629 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2630 VPRINTK("BUS_DMA (host_stat 0x%X)\n", host_stat);
2632 if (!(host_stat & ATA_DMA_INTR)) {
2633 ap->stats.idle_irq++;
2637 ata_dma_complete(qc, host_stat);
2641 /* command completion, but no data xfer */
2642 /* FIXME: a shared interrupt _will_ cause a non-data command
2643 * to be completed prematurely, with an error.
2645 * This doesn't matter right now, since we aren't sending
2646 * non-data commands down this pipe except in development
2649 case ATA_PROT_ATAPI:
2650 case ATA_PROT_NODATA:
2651 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2652 DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
2653 ata_qc_complete(qc, status);
2658 ap->stats.idle_irq++;
2661 if ((ap->stats.idle_irq % 1000) == 0) {
2663 ata_irq_ack(ap, 0); /* debug trap */
2664 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
2674 * ata_interrupt - Default ATA host interrupt handler
2676 * @dev_instance: pointer to our host information structure
2685 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
2687 struct ata_host_set *host_set = dev_instance;
2689 unsigned int handled = 0;
2690 unsigned long flags;
2692 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
2693 spin_lock_irqsave(&host_set->lock, flags);
2695 for (i = 0; i < host_set->n_ports; i++) {
2696 struct ata_port *ap;
2698 ap = host_set->ports[i];
2699 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
2700 struct ata_queued_cmd *qc;
2702 qc = ata_qc_from_tag(ap, ap->active_tag);
2703 if (qc && (!(qc->tf.ctl & ATA_NIEN)))
2704 handled += ata_host_intr(ap, qc);
2708 spin_unlock_irqrestore(&host_set->lock, flags);
2710 return IRQ_RETVAL(handled);
2723 static unsigned long ata_thread_iter(struct ata_port *ap)
2727 DPRINTK("ata%u: thr_state %s\n",
2728 ap->id, ata_thr_state_name(ap->thr_state));
2730 switch (ap->thr_state) {
2732 ap->thr_state = THR_PORT_RESET;
2735 case THR_PROBE_START:
2736 ap->thr_state = THR_PORT_RESET;
2739 case THR_PORT_RESET:
2743 case THR_PROBE_SUCCESS:
2745 ap->thr_state = THR_IDLE;
2748 case THR_PROBE_FAILED:
2750 ap->thr_state = THR_AWAIT_DEATH;
2753 case THR_AWAIT_DEATH:
2759 printk(KERN_DEBUG "ata%u: unknown thr state %s\n",
2760 ap->id, ata_thr_state_name(ap->thr_state));
2764 DPRINTK("ata%u: new thr_state %s, returning %ld\n",
2765 ap->id, ata_thr_state_name(ap->thr_state), timeout);
2770 * atapi_packet_task - Write CDB bytes to hardware
2771 * @_data: Port to which ATAPI device is attached.
2773 * When device has indicated its readiness to accept
2774 * a CDB, this function is called. Send the CDB.
2775 * If DMA is to be performed, exit immediately.
2776 * Otherwise, we are in polling mode, so poll
2777 * status under operation succeeds or fails.
2780 * Kernel thread context (may sleep)
2783 static void atapi_packet_task(void *_data)
2785 struct ata_port *ap = _data;
2786 struct ata_queued_cmd *qc;
2789 qc = ata_qc_from_tag(ap, ap->active_tag);
2791 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2793 /* sleep-wait for BSY to clear */
2794 DPRINTK("busy wait\n");
2795 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
2798 /* make sure DRQ is set */
2799 status = ata_chk_status(ap);
2800 if ((status & ATA_DRQ) == 0)
2804 /* FIXME: mmio-ize */
2805 DPRINTK("send cdb\n");
2806 outsl(ap->ioaddr.data_addr,
2807 qc->scsicmd->cmnd, ap->host->max_cmd_len / 4);
2809 /* if we are DMA'ing, irq handler takes over from here */
2810 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
2811 ap->ops->bmdma_start(qc); /* initiate bmdma */
2813 /* non-data commands are also handled via irq */
2814 else if (qc->scsicmd->sc_data_direction == SCSI_DATA_NONE) {
2818 /* PIO commands are handled by polling */
2820 ap->pio_task_state = PIO_ST;
2821 queue_work(ata_wq, &ap->pio_task);
2827 ata_qc_complete(qc, ATA_ERR);
2830 int ata_port_start (struct ata_port *ap)
2832 struct pci_dev *pdev = ap->host_set->pdev;
2834 ap->prd = pci_alloc_consistent(pdev, ATA_PRD_TBL_SZ, &ap->prd_dma);
2838 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
2843 void ata_port_stop (struct ata_port *ap)
2845 struct pci_dev *pdev = ap->host_set->pdev;
2847 pci_free_consistent(pdev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
2850 static void ata_probe_task(void *_data)
2852 struct ata_port *ap = _data;
2855 timeout = ata_thread_iter(ap);
2860 queue_delayed_work(ata_wq, &ap->probe_task, timeout);
2862 queue_work(ata_wq, &ap->probe_task);
2866 * ata_host_remove - Unregister SCSI host structure with upper layers
2867 * @ap: Port to unregister
2868 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
2873 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
2875 struct Scsi_Host *sh = ap->host;
2880 scsi_remove_host(sh);
2882 ap->ops->port_stop(ap);
2886 * ata_host_init - Initialize an ata_port structure
2887 * @ap: Structure to initialize
2888 * @host: associated SCSI mid-layer structure
2889 * @host_set: Collection of hosts to which @ap belongs
2890 * @ent: Probe information provided by low-level driver
2891 * @port_no: Port number associated with this ata_port
2897 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
2898 struct ata_host_set *host_set,
2899 struct ata_probe_ent *ent, unsigned int port_no)
2905 host->max_channel = 1;
2906 host->unique_id = ata_unique_id++;
2907 host->max_cmd_len = 12;
2908 scsi_set_device(host, &ent->pdev->dev);
2909 scsi_assign_lock(host, &host_set->lock);
2911 ap->flags = ATA_FLAG_PORT_DISABLED;
2912 ap->id = host->unique_id;
2914 ap->ctl = ATA_DEVCTL_OBS;
2915 ap->host_set = host_set;
2916 ap->port_no = port_no;
2917 ap->pio_mask = ent->pio_mask;
2918 ap->udma_mask = ent->udma_mask;
2919 ap->flags |= ent->host_flags;
2920 ap->ops = ent->port_ops;
2921 ap->thr_state = THR_PROBE_START;
2922 ap->cbl = ATA_CBL_NONE;
2923 ap->device[0].flags = ATA_DFLAG_MASTER;
2924 ap->active_tag = ATA_TAG_POISON;
2925 ap->last_ctl = 0xFF;
2927 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
2928 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
2929 INIT_WORK(&ap->probe_task, ata_probe_task, ap);
2931 for (i = 0; i < ATA_MAX_DEVICES; i++)
2932 ap->device[i].devno = i;
2934 init_MUTEX_LOCKED(&ap->probe_sem);
2937 ap->stats.unhandled_irq = 1;
2938 ap->stats.idle_irq = 1;
2941 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
2945 * ata_host_add - Attach low-level ATA driver to system
2946 * @ent: Information provided by low-level driver
2947 * @host_set: Collections of ports to which we add
2948 * @port_no: Port number associated with this host
2956 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
2957 struct ata_host_set *host_set,
2958 unsigned int port_no)
2960 struct Scsi_Host *host;
2961 struct ata_port *ap;
2965 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
2969 ap = (struct ata_port *) &host->hostdata[0];
2971 ata_host_init(ap, host, host_set, ent, port_no);
2973 rc = ap->ops->port_start(ap);
2980 scsi_host_put(host);
2994 int ata_device_add(struct ata_probe_ent *ent)
2996 unsigned int count = 0, i;
2997 struct pci_dev *pdev = ent->pdev;
2998 struct ata_host_set *host_set;
3001 /* alloc a container for our list of ATA ports (buses) */
3002 host_set = kmalloc(sizeof(struct ata_host_set) +
3003 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
3006 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
3007 spin_lock_init(&host_set->lock);
3009 host_set->pdev = pdev;
3010 host_set->n_ports = ent->n_ports;
3011 host_set->irq = ent->irq;
3012 host_set->mmio_base = ent->mmio_base;
3013 host_set->private_data = ent->private_data;
3015 /* register each port bound to this device */
3016 for (i = 0; i < ent->n_ports; i++) {
3017 struct ata_port *ap;
3019 ap = ata_host_add(ent, host_set, i);
3023 host_set->ports[i] = ap;
3025 /* print per-port info to dmesg */
3026 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3027 "bmdma 0x%lX irq %lu\n",
3029 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
3030 ata_udma_string(ent->udma_mask),
3031 ap->ioaddr.cmd_addr,
3032 ap->ioaddr.ctl_addr,
3033 ap->ioaddr.bmdma_addr,
3044 /* obtain irq, that is shared between channels */
3045 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
3046 DRV_NAME, host_set))
3049 /* perform each probe synchronously */
3050 DPRINTK("probe begin\n");
3051 for (i = 0; i < count; i++) {
3052 struct ata_port *ap;
3055 ap = host_set->ports[i];
3057 DPRINTK("ata%u: probe begin\n", ap->id);
3058 queue_work(ata_wq, &ap->probe_task); /* start probe */
3060 DPRINTK("ata%u: probe-wait begin\n", ap->id);
3061 down(&ap->probe_sem); /* wait for end */
3063 DPRINTK("ata%u: probe-wait end\n", ap->id);
3065 rc = scsi_add_host(ap->host, &pdev->dev);
3067 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
3069 /* FIXME: do something useful here */
3070 /* FIXME: handle unconditional calls to
3071 * scsi_scan_host and ata_host_remove, below,
3077 /* probes are done, now scan each port's disk(s) */
3078 DPRINTK("probe begin\n");
3079 for (i = 0; i < count; i++) {
3080 struct ata_port *ap = host_set->ports[i];
3082 scsi_scan_host(ap->host);
3085 pci_set_drvdata(pdev, host_set);
3087 VPRINTK("EXIT, returning %u\n", ent->n_ports);
3088 return ent->n_ports; /* success */
3091 for (i = 0; i < count; i++) {
3092 ata_host_remove(host_set->ports[i], 1);
3093 scsi_host_put(host_set->ports[i]->host);
3096 VPRINTK("EXIT, returning 0\n");
3101 * ata_scsi_release - SCSI layer callback hook for host unload
3102 * @host: libata host to be unloaded
3104 * Performs all duties necessary to shut down a libata port...
3105 * Kill port kthread, disable port, and release resources.
3108 * Inherited from SCSI layer.
3114 int ata_scsi_release(struct Scsi_Host *host)
3116 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3120 ap->ops->port_disable(ap);
3121 ata_host_remove(ap, 0);
3128 * ata_std_ports - initialize ioaddr with standard port offsets.
3129 * @ioaddr: IO address structure to be initialized
3131 void ata_std_ports(struct ata_ioports *ioaddr)
3133 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3134 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3135 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3136 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3137 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3138 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3139 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3140 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3141 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3142 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3146 * ata_pci_init_one - Initialize/register PCI IDE host controller
3147 * @pdev: Controller to be initialized
3148 * @port_info: Information from low-level host driver
3149 * @n_ports: Number of ports attached to host controller
3152 * Inherited from PCI layer (may sleep).
3158 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3159 unsigned int n_ports)
3161 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3162 struct ata_port_info *port0, *port1;
3164 unsigned int legacy_mode = 0;
3169 port0 = port_info[0];
3171 port1 = port_info[1];
3175 if ((port0->host_flags & ATA_FLAG_NO_LEGACY) == 0) {
3176 /* TODO: support transitioning to native mode? */
3177 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3178 mask = (1 << 2) | (1 << 0);
3179 if ((tmp8 & mask) != mask)
3180 legacy_mode = (1 << 3);
3184 if ((!legacy_mode) && (n_ports > 1)) {
3185 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3189 rc = pci_enable_device(pdev);
3193 rc = pci_request_regions(pdev, DRV_NAME);
3198 if (!request_region(0x1f0, 8, "libata")) {
3199 struct resource *conflict, res;
3201 res.end = 0x1f0 + 8 - 1;
3202 conflict = ____request_resource(&ioport_resource, &res);
3203 if (!strcmp(conflict->name, "libata"))
3204 legacy_mode |= (1 << 0);
3206 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3208 legacy_mode |= (1 << 0);
3210 if (!request_region(0x170, 8, "libata")) {
3211 struct resource *conflict, res;
3213 res.end = 0x170 + 8 - 1;
3214 conflict = ____request_resource(&ioport_resource, &res);
3215 if (!strcmp(conflict->name, "libata"))
3216 legacy_mode |= (1 << 1);
3218 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3220 legacy_mode |= (1 << 1);
3223 /* we have legacy mode, but all ports are unavailable */
3224 if (legacy_mode == (1 << 3)) {
3226 goto err_out_regions;
3229 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3231 goto err_out_regions;
3232 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3234 goto err_out_regions;
3236 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3239 goto err_out_regions;
3242 memset(probe_ent, 0, sizeof(*probe_ent));
3243 probe_ent->pdev = pdev;
3244 INIT_LIST_HEAD(&probe_ent->node);
3247 probe_ent2 = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3250 goto err_out_free_ent;
3253 memset(probe_ent2, 0, sizeof(*probe_ent));
3254 probe_ent2->pdev = pdev;
3255 INIT_LIST_HEAD(&probe_ent2->node);
3258 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3259 probe_ent->sht = port0->sht;
3260 probe_ent->host_flags = port0->host_flags;
3261 probe_ent->pio_mask = port0->pio_mask;
3262 probe_ent->udma_mask = port0->udma_mask;
3263 probe_ent->port_ops = port0->port_ops;
3266 probe_ent->port[0].cmd_addr = 0x1f0;
3267 probe_ent->port[0].altstatus_addr =
3268 probe_ent->port[0].ctl_addr = 0x3f6;
3269 probe_ent->n_ports = 1;
3270 probe_ent->irq = 14;
3271 ata_std_ports(&probe_ent->port[0]);
3273 probe_ent2->port[0].cmd_addr = 0x170;
3274 probe_ent2->port[0].altstatus_addr =
3275 probe_ent2->port[0].ctl_addr = 0x376;
3276 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3277 probe_ent2->n_ports = 1;
3278 probe_ent2->irq = 15;
3279 ata_std_ports(&probe_ent2->port[0]);
3281 probe_ent2->sht = port1->sht;
3282 probe_ent2->host_flags = port1->host_flags;
3283 probe_ent2->pio_mask = port1->pio_mask;
3284 probe_ent2->udma_mask = port1->udma_mask;
3285 probe_ent2->port_ops = port1->port_ops;
3287 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3288 ata_std_ports(&probe_ent->port[0]);
3289 probe_ent->port[0].altstatus_addr =
3290 probe_ent->port[0].ctl_addr =
3291 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3293 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3294 ata_std_ports(&probe_ent->port[1]);
3295 probe_ent->port[1].altstatus_addr =
3296 probe_ent->port[1].ctl_addr =
3297 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3298 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3300 probe_ent->n_ports = 2;
3301 probe_ent->irq = pdev->irq;
3302 probe_ent->irq_flags = SA_SHIRQ;
3305 pci_set_master(pdev);
3307 /* FIXME: check ata_device_add return */
3309 if (legacy_mode & (1 << 0))
3310 ata_device_add(probe_ent);
3311 if (legacy_mode & (1 << 1))
3312 ata_device_add(probe_ent2);
3315 ata_device_add(probe_ent);
3316 assert(probe_ent2 == NULL);
3325 if (legacy_mode & (1 << 0))
3326 release_region(0x1f0, 8);
3327 if (legacy_mode & (1 << 1))
3328 release_region(0x170, 8);
3329 pci_release_regions(pdev);
3331 pci_disable_device(pdev);
3336 * ata_pci_remove_one - PCI layer callback for device removal
3337 * @pdev: PCI device that was removed
3339 * PCI layer indicates to libata via this hook that
3340 * hot-unplug or module unload event has occured.
3341 * Handle this by unregistering all objects associated
3342 * with this PCI device. Free those objects. Then finally
3343 * release PCI resources and disable device.
3346 * Inherited from PCI layer (may sleep).
3349 void ata_pci_remove_one (struct pci_dev *pdev)
3351 struct ata_host_set *host_set = pci_get_drvdata(pdev);
3352 struct ata_port *ap;
3355 for (i = 0; i < host_set->n_ports; i++) {
3356 ap = host_set->ports[i];
3358 scsi_remove_host(ap->host);
3361 free_irq(host_set->irq, host_set);
3362 if (host_set->mmio_base)
3363 iounmap(host_set->mmio_base);
3364 if (host_set->ports[0]->ops->host_stop)
3365 host_set->ports[0]->ops->host_stop(host_set);
3367 for (i = 0; i < host_set->n_ports; i++) {
3368 ap = host_set->ports[i];
3370 ata_scsi_release(ap->host);
3371 scsi_host_put(ap->host);
3374 pci_release_regions(pdev);
3376 for (i = 0; i < host_set->n_ports; i++) {
3377 struct ata_ioports *ioaddr;
3379 ap = host_set->ports[i];
3380 ioaddr = &ap->ioaddr;
3382 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
3383 if (ioaddr->cmd_addr == 0x1f0)
3384 release_region(0x1f0, 8);
3385 else if (ioaddr->cmd_addr == 0x170)
3386 release_region(0x170, 8);
3391 pci_disable_device(pdev);
3392 pci_set_drvdata(pdev, NULL);
3395 /* move to PCI subsystem */
3396 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
3398 unsigned long tmp = 0;
3400 switch (bits->width) {
3403 pci_read_config_byte(pdev, bits->reg, &tmp8);
3409 pci_read_config_word(pdev, bits->reg, &tmp16);
3415 pci_read_config_dword(pdev, bits->reg, &tmp32);
3426 return (tmp == bits->val) ? 1 : 0;
3439 static int __init ata_init(void)
3441 ata_wq = create_workqueue("ata");
3445 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
3449 static void __exit ata_exit(void)
3451 destroy_workqueue(ata_wq);
3454 module_init(ata_init);
3455 module_exit(ata_exit);
3458 * libata is essentially a library of internal helper functions for
3459 * low-level ATA host controller drivers. As such, the API/ABI is
3460 * likely to change as new drivers are added and updated.
3461 * Do not depend on ABI/API stability.
3464 EXPORT_SYMBOL_GPL(pci_test_config_bits);
3465 EXPORT_SYMBOL_GPL(ata_std_bios_param);
3466 EXPORT_SYMBOL_GPL(ata_std_ports);
3467 EXPORT_SYMBOL_GPL(ata_device_add);
3468 EXPORT_SYMBOL_GPL(ata_qc_complete);
3469 EXPORT_SYMBOL_GPL(ata_eng_timeout);
3470 EXPORT_SYMBOL_GPL(ata_tf_load_pio);
3471 EXPORT_SYMBOL_GPL(ata_tf_load_mmio);
3472 EXPORT_SYMBOL_GPL(ata_tf_read_pio);
3473 EXPORT_SYMBOL_GPL(ata_tf_read_mmio);
3474 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
3475 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
3476 EXPORT_SYMBOL_GPL(ata_check_status_pio);
3477 EXPORT_SYMBOL_GPL(ata_check_status_mmio);
3478 EXPORT_SYMBOL_GPL(ata_exec_command_pio);
3479 EXPORT_SYMBOL_GPL(ata_exec_command_mmio);
3480 EXPORT_SYMBOL_GPL(ata_port_start);
3481 EXPORT_SYMBOL_GPL(ata_port_stop);
3482 EXPORT_SYMBOL_GPL(ata_interrupt);
3483 EXPORT_SYMBOL_GPL(ata_fill_sg);
3484 EXPORT_SYMBOL_GPL(ata_bmdma_setup_pio);
3485 EXPORT_SYMBOL_GPL(ata_bmdma_start_pio);
3486 EXPORT_SYMBOL_GPL(ata_bmdma_setup_mmio);
3487 EXPORT_SYMBOL_GPL(ata_bmdma_start_mmio);
3488 EXPORT_SYMBOL_GPL(ata_port_probe);
3489 EXPORT_SYMBOL_GPL(sata_phy_reset);
3490 EXPORT_SYMBOL_GPL(ata_bus_reset);
3491 EXPORT_SYMBOL_GPL(ata_port_disable);
3492 EXPORT_SYMBOL_GPL(ata_pci_init_one);
3493 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
3494 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
3495 EXPORT_SYMBOL_GPL(ata_scsi_error);
3496 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
3497 EXPORT_SYMBOL_GPL(ata_scsi_release);
3498 EXPORT_SYMBOL_GPL(ata_host_intr);
3499 EXPORT_SYMBOL_GPL(ata_dev_id_string);