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/suspend.h>
38 #include <scsi/scsi.h>
41 #include <linux/libata.h>
43 #include <asm/semaphore.h>
47 static void atapi_cdb_send(struct ata_port *ap);
48 static unsigned int ata_busy_sleep (struct ata_port *ap,
49 unsigned long tmout_pat,
51 static void __ata_dev_select (struct ata_port *ap, unsigned int device);
52 #if 0 /* to be used eventually */
53 static void ata_qc_push (struct ata_queued_cmd *qc, unsigned int append);
55 static void ata_dma_complete(struct ata_port *ap, u8 host_stat,
56 unsigned int done_late);
57 static void ata_host_set_pio(struct ata_port *ap);
58 static void ata_host_set_udma(struct ata_port *ap);
59 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device);
60 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device);
61 static void ata_set_mode(struct ata_port *ap);
63 static unsigned int ata_unique_id = 1;
65 MODULE_AUTHOR("Jeff Garzik");
66 MODULE_DESCRIPTION("Library module for ATA devices");
67 MODULE_LICENSE("GPL");
69 static const char * thr_state_name[] = {
87 * ata_thr_state_name - convert thread state enum to string
88 * @thr_state: thread state to be converted to string
90 * Converts the specified thread state id to a constant C string.
96 * The THR_xxx-prefixed string naming the specified thread
97 * state id, or the string "<invalid THR_xxx state>".
100 static const char *ata_thr_state_name(unsigned int thr_state)
102 if (thr_state < ARRAY_SIZE(thr_state_name))
103 return thr_state_name[thr_state];
104 return "<invalid THR_xxx state>";
108 * msleep - sleep for a number of milliseconds
109 * @msecs: number of milliseconds to sleep
111 * Issues schedule_timeout call for the specified number
118 static void msleep(unsigned long msecs)
120 set_current_state(TASK_UNINTERRUPTIBLE);
121 schedule_timeout(msecs_to_jiffies(msecs) + 1);
125 * ata_tf_load_pio - send taskfile registers to host controller
126 * @ioaddr: set of IO ports to which output is sent
127 * @tf: ATA taskfile register set
129 * Outputs ATA taskfile to standard ATA host controller using PIO.
132 * Inherited from caller.
135 void ata_tf_load_pio(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 outb(tf->ctl, ioaddr->ctl_addr);
142 ap->last_ctl = tf->ctl;
146 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
147 outb(tf->hob_feature, ioaddr->feature_addr);
148 outb(tf->hob_nsect, ioaddr->nsect_addr);
149 outb(tf->hob_lbal, ioaddr->lbal_addr);
150 outb(tf->hob_lbam, ioaddr->lbam_addr);
151 outb(tf->hob_lbah, ioaddr->lbah_addr);
152 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
161 outb(tf->feature, ioaddr->feature_addr);
162 outb(tf->nsect, ioaddr->nsect_addr);
163 outb(tf->lbal, ioaddr->lbal_addr);
164 outb(tf->lbam, ioaddr->lbam_addr);
165 outb(tf->lbah, ioaddr->lbah_addr);
166 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
174 if (tf->flags & ATA_TFLAG_DEVICE) {
175 outb(tf->device, ioaddr->device_addr);
176 VPRINTK("device 0x%X\n", tf->device);
183 * ata_tf_load_mmio - send taskfile registers to host controller
184 * @ioaddr: set of IO ports to which output is sent
185 * @tf: ATA taskfile register set
187 * Outputs ATA taskfile to standard ATA host controller using MMIO.
190 * Inherited from caller.
193 void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
195 struct ata_ioports *ioaddr = &ap->ioaddr;
196 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
198 if (tf->ctl != ap->last_ctl) {
199 writeb(tf->ctl, ap->ioaddr.ctl_addr);
200 ap->last_ctl = tf->ctl;
204 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
205 writeb(tf->hob_feature, (void *) ioaddr->feature_addr);
206 writeb(tf->hob_nsect, (void *) ioaddr->nsect_addr);
207 writeb(tf->hob_lbal, (void *) ioaddr->lbal_addr);
208 writeb(tf->hob_lbam, (void *) ioaddr->lbam_addr);
209 writeb(tf->hob_lbah, (void *) ioaddr->lbah_addr);
210 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
219 writeb(tf->feature, (void *) ioaddr->feature_addr);
220 writeb(tf->nsect, (void *) ioaddr->nsect_addr);
221 writeb(tf->lbal, (void *) ioaddr->lbal_addr);
222 writeb(tf->lbam, (void *) ioaddr->lbam_addr);
223 writeb(tf->lbah, (void *) ioaddr->lbah_addr);
224 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
232 if (tf->flags & ATA_TFLAG_DEVICE) {
233 writeb(tf->device, (void *) ioaddr->device_addr);
234 VPRINTK("device 0x%X\n", tf->device);
241 * ata_exec_command_pio - issue ATA command to host controller
242 * @ap: port to which command is being issued
243 * @tf: ATA taskfile register set
245 * Issues PIO write to ATA command register, with proper
246 * synchronization with interrupt handler / other threads.
249 * spin_lock_irqsave(host_set lock)
252 void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
254 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
256 outb(tf->command, ap->ioaddr.command_addr);
262 * ata_exec_command_mmio - issue ATA command to host controller
263 * @ap: port to which command is being issued
264 * @tf: ATA taskfile register set
266 * Issues MMIO write to ATA command register, with proper
267 * synchronization with interrupt handler / other threads.
270 * spin_lock_irqsave(host_set lock)
273 void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
275 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
277 writeb(tf->command, (void *) ap->ioaddr.command_addr);
282 * ata_exec - issue ATA command to host controller
283 * @ap: port to which command is being issued
284 * @tf: ATA taskfile register set
286 * Issues PIO write to ATA command register, with proper
287 * synchronization with interrupt handler / other threads.
290 * Obtains host_set lock.
293 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
297 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
298 spin_lock_irqsave(&ap->host_set->lock, flags);
299 ap->ops->exec_command(ap, tf);
300 spin_unlock_irqrestore(&ap->host_set->lock, flags);
304 * ata_tf_to_host - issue ATA taskfile to host controller
305 * @ap: port to which command is being issued
306 * @tf: ATA taskfile register set
308 * Issues ATA taskfile register set to ATA host controller,
309 * via PIO, with proper synchronization with interrupt handler and
313 * Obtains host_set lock.
316 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
318 init_MUTEX_LOCKED(&ap->sem);
320 ap->ops->tf_load(ap, tf);
326 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
327 * @ap: port to which command is being issued
328 * @tf: ATA taskfile register set
330 * Issues ATA taskfile register set to ATA host controller,
331 * via PIO, with proper synchronization with interrupt handler and
335 * spin_lock_irqsave(host_set lock)
338 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
340 init_MUTEX_LOCKED(&ap->sem);
342 ap->ops->tf_load(ap, tf);
343 ap->ops->exec_command(ap, tf);
347 * ata_tf_read_pio - input device's ATA taskfile shadow registers
348 * @ioaddr: set of IO ports from which input is read
349 * @tf: ATA taskfile register set for storing input
351 * Reads ATA taskfile registers for currently-selected device
355 * Inherited from caller.
358 void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
360 struct ata_ioports *ioaddr = &ap->ioaddr;
362 tf->nsect = inb(ioaddr->nsect_addr);
363 tf->lbal = inb(ioaddr->lbal_addr);
364 tf->lbam = inb(ioaddr->lbam_addr);
365 tf->lbah = inb(ioaddr->lbah_addr);
366 tf->device = inb(ioaddr->device_addr);
368 if (tf->flags & ATA_TFLAG_LBA48) {
369 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
370 tf->hob_feature = inb(ioaddr->error_addr);
371 tf->hob_nsect = inb(ioaddr->nsect_addr);
372 tf->hob_lbal = inb(ioaddr->lbal_addr);
373 tf->hob_lbam = inb(ioaddr->lbam_addr);
374 tf->hob_lbah = inb(ioaddr->lbah_addr);
379 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
380 * @ioaddr: set of IO ports from which input is read
381 * @tf: ATA taskfile register set for storing input
383 * Reads ATA taskfile registers for currently-selected device
387 * Inherited from caller.
390 void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
392 struct ata_ioports *ioaddr = &ap->ioaddr;
394 tf->nsect = readb((void *)ioaddr->nsect_addr);
395 tf->lbal = readb((void *)ioaddr->lbal_addr);
396 tf->lbam = readb((void *)ioaddr->lbam_addr);
397 tf->lbah = readb((void *)ioaddr->lbah_addr);
398 tf->device = readb((void *)ioaddr->device_addr);
400 if (tf->flags & ATA_TFLAG_LBA48) {
401 writeb(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr);
402 tf->hob_feature = readb((void *)ioaddr->error_addr);
403 tf->hob_nsect = readb((void *)ioaddr->nsect_addr);
404 tf->hob_lbal = readb((void *)ioaddr->lbal_addr);
405 tf->hob_lbam = readb((void *)ioaddr->lbam_addr);
406 tf->hob_lbah = readb((void *)ioaddr->lbah_addr);
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
414 * Reads ATA taskfile status register for currently-selected device
415 * via PIO and return it's value. This also clears pending interrupts
419 * Inherited from caller.
421 u8 ata_check_status_pio(struct ata_port *ap)
423 return inb(ap->ioaddr.status_addr);
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
430 * Reads ATA taskfile status register for currently-selected device
431 * via MMIO and return it's value. This also clears pending interrupts
435 * Inherited from caller.
437 u8 ata_check_status_mmio(struct ata_port *ap)
439 return readb((void *) ap->ioaddr.status_addr);
443 * ata_prot_to_cmd - determine which read/write opcodes to use
444 * @protocol: ATA_PROT_xxx taskfile protocol
445 * @lba48: true is lba48 is present
447 * Given necessary input, determine which read/write commands
448 * to use to transfer data.
453 static int ata_prot_to_cmd(int protocol, int lba48)
455 int rcmd = 0, wcmd = 0;
460 rcmd = ATA_CMD_PIO_READ_EXT;
461 wcmd = ATA_CMD_PIO_WRITE_EXT;
463 rcmd = ATA_CMD_PIO_READ;
464 wcmd = ATA_CMD_PIO_WRITE;
470 rcmd = ATA_CMD_READ_EXT;
471 wcmd = ATA_CMD_WRITE_EXT;
474 wcmd = ATA_CMD_WRITE;
482 return rcmd | (wcmd << 8);
486 * ata_dev_set_protocol - set taskfile protocol and r/w commands
487 * @dev: device to examine and configure
489 * Examine the device configuration, after we have
490 * read the identify-device page and configured the
491 * data transfer mode. Set internal state related to
492 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
493 * and calculate the proper read/write commands to use.
498 static void ata_dev_set_protocol(struct ata_device *dev)
500 int pio = (dev->flags & ATA_DFLAG_PIO);
501 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
505 proto = dev->xfer_protocol = ATA_PROT_PIO;
507 proto = dev->xfer_protocol = ATA_PROT_DMA;
509 cmd = ata_prot_to_cmd(proto, lba48);
513 dev->read_cmd = cmd & 0xff;
514 dev->write_cmd = (cmd >> 8) & 0xff;
517 static const char * udma_str[] = {
529 * ata_udma_string - convert UDMA bit offset to string
530 * @udma_mask: mask of bits supported; only highest bit counts.
532 * Determine string which represents the highest speed
533 * (highest bit in @udma_mask).
539 * Constant C string representing highest speed listed in
540 * @udma_mask, or the constant C string "<n/a>".
543 static const char *ata_udma_string(unsigned int udma_mask)
547 for (i = 7; i >= 0; i--) {
548 if (udma_mask & (1 << i))
556 * ata_pio_devchk - PATA device presence detection
557 * @ap: ATA channel to examine
558 * @device: Device to examine (starting at zero)
560 * This technique was originally described in
561 * Hale Landis's ATADRVR (www.ata-atapi.com), and
562 * later found its way into the ATA/ATAPI spec.
564 * Write a pattern to the ATA shadow registers,
565 * and if a device is present, it will respond by
566 * correctly storing and echoing back the
567 * ATA shadow register contents.
573 static unsigned int ata_pio_devchk(struct ata_port *ap,
576 struct ata_ioports *ioaddr = &ap->ioaddr;
579 __ata_dev_select(ap, device);
581 outb(0x55, ioaddr->nsect_addr);
582 outb(0xaa, ioaddr->lbal_addr);
584 outb(0xaa, ioaddr->nsect_addr);
585 outb(0x55, ioaddr->lbal_addr);
587 outb(0x55, ioaddr->nsect_addr);
588 outb(0xaa, ioaddr->lbal_addr);
590 nsect = inb(ioaddr->nsect_addr);
591 lbal = inb(ioaddr->lbal_addr);
593 if ((nsect == 0x55) && (lbal == 0xaa))
594 return 1; /* we found a device */
596 return 0; /* nothing found */
600 * ata_mmio_devchk - PATA device presence detection
601 * @ap: ATA channel to examine
602 * @device: Device to examine (starting at zero)
604 * This technique was originally described in
605 * Hale Landis's ATADRVR (www.ata-atapi.com), and
606 * later found its way into the ATA/ATAPI spec.
608 * Write a pattern to the ATA shadow registers,
609 * and if a device is present, it will respond by
610 * correctly storing and echoing back the
611 * ATA shadow register contents.
617 static unsigned int ata_mmio_devchk(struct ata_port *ap,
620 struct ata_ioports *ioaddr = &ap->ioaddr;
623 __ata_dev_select(ap, device);
625 writeb(0x55, (void *) ioaddr->nsect_addr);
626 writeb(0xaa, (void *) ioaddr->lbal_addr);
628 writeb(0xaa, (void *) ioaddr->nsect_addr);
629 writeb(0x55, (void *) ioaddr->lbal_addr);
631 writeb(0x55, (void *) ioaddr->nsect_addr);
632 writeb(0xaa, (void *) ioaddr->lbal_addr);
634 nsect = readb((void *) ioaddr->nsect_addr);
635 lbal = readb((void *) ioaddr->lbal_addr);
637 if ((nsect == 0x55) && (lbal == 0xaa))
638 return 1; /* we found a device */
640 return 0; /* nothing found */
644 * ata_dev_devchk - PATA device presence detection
645 * @ap: ATA channel to examine
646 * @device: Device to examine (starting at zero)
648 * Dispatch ATA device presence detection, depending
649 * on whether we are using PIO or MMIO to talk to the
650 * ATA shadow registers.
656 static unsigned int ata_dev_devchk(struct ata_port *ap,
659 if (ap->flags & ATA_FLAG_MMIO)
660 return ata_mmio_devchk(ap, device);
661 return ata_pio_devchk(ap, device);
665 * ata_dev_classify - determine device type based on ATA-spec signature
666 * @tf: ATA taskfile register set for device to be identified
668 * Determine from taskfile register contents whether a device is
669 * ATA or ATAPI, as per "Signature and persistence" section
670 * of ATA/PI spec (volume 1, sect 5.14).
676 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
677 * the event of failure.
680 static unsigned int ata_dev_classify(struct ata_taskfile *tf)
682 /* Apple's open source Darwin code hints that some devices only
683 * put a proper signature into the LBA mid/high registers,
684 * So, we only check those. It's sufficient for uniqueness.
687 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
688 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
689 DPRINTK("found ATA device by sig\n");
693 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
694 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
695 DPRINTK("found ATAPI device by sig\n");
696 return ATA_DEV_ATAPI;
699 DPRINTK("unknown device\n");
700 return ATA_DEV_UNKNOWN;
704 * ata_dev_try_classify - Parse returned ATA device signature
705 * @ap: ATA channel to examine
706 * @device: Device to examine (starting at zero)
708 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
709 * an ATA/ATAPI-defined set of values is placed in the ATA
710 * shadow registers, indicating the results of device detection
713 * Select the ATA device, and read the values from the ATA shadow
714 * registers. Then parse according to the Error register value,
715 * and the spec-defined values examined by ata_dev_classify().
721 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
723 struct ata_device *dev = &ap->device[device];
724 struct ata_taskfile tf;
728 __ata_dev_select(ap, device);
730 memset(&tf, 0, sizeof(tf));
732 err = ata_chk_err(ap);
733 ap->ops->tf_read(ap, &tf);
735 dev->class = ATA_DEV_NONE;
737 /* see if device passed diags */
740 else if ((device == 0) && (err == 0x81))
745 /* determine if device if ATA or ATAPI */
746 class = ata_dev_classify(&tf);
747 if (class == ATA_DEV_UNKNOWN)
749 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
758 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
759 * @dev: Device whose IDENTIFY DEVICE results we will examine
760 * @s: string into which data is output
761 * @ofs: offset into identify device page
762 * @len: length of string to return
764 * The strings in the IDENTIFY DEVICE page are broken up into
765 * 16-bit chunks. Run through the string, and output each
766 * 8-bit chunk linearly, regardless of platform.
772 void ata_dev_id_string(struct ata_device *dev, unsigned char *s,
773 unsigned int ofs, unsigned int len)
778 c = dev->id[ofs] >> 8;
782 c = dev->id[ofs] & 0xff;
792 * ata_dev_parse_strings - Store useful IDENTIFY DEVICE page strings
793 * @dev: Device whose IDENTIFY DEVICE page info we use
795 * We store 'vendor' and 'product' strings read from the device,
796 * for later use in the SCSI simulator's INQUIRY data.
798 * Set these strings here, in the case of 'product', using
799 * data read from the ATA IDENTIFY DEVICE page.
805 static void ata_dev_parse_strings(struct ata_device *dev)
807 assert (dev->class == ATA_DEV_ATA);
808 memcpy(dev->vendor, "ATA ", 8);
810 ata_dev_id_string(dev, dev->product, ATA_ID_PROD_OFS,
811 sizeof(dev->product));
815 * __ata_dev_select - Select device 0/1 on ATA bus
816 * @ap: ATA channel to manipulate
817 * @device: ATA device (numbered from zero) to select
819 * Use the method defined in the ATA specification to
820 * make either device 0, or device 1, active on the
827 static void __ata_dev_select (struct ata_port *ap, unsigned int device)
832 tmp = ATA_DEVICE_OBS;
834 tmp = ATA_DEVICE_OBS | ATA_DEV1;
836 if (ap->flags & ATA_FLAG_MMIO) {
837 writeb(tmp, (void *) ap->ioaddr.device_addr);
839 outb(tmp, ap->ioaddr.device_addr);
841 ata_pause(ap); /* needed; also flushes, for mmio */
845 * ata_dev_select - Select device 0/1 on ATA bus
846 * @ap: ATA channel to manipulate
847 * @device: ATA device (numbered from zero) to select
848 * @wait: non-zero to wait for Status register BSY bit to clear
849 * @can_sleep: non-zero if context allows sleeping
851 * Use the method defined in the ATA specification to
852 * make either device 0, or device 1, active on the
855 * This is a high-level version of __ata_dev_select(),
856 * which additionally provides the services of inserting
857 * the proper pauses and status polling, where needed.
863 void ata_dev_select(struct ata_port *ap, unsigned int device,
864 unsigned int wait, unsigned int can_sleep)
866 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
867 ap->id, device, wait);
872 __ata_dev_select(ap, device);
875 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
882 * ata_dump_id - IDENTIFY DEVICE info debugging output
883 * @dev: Device whose IDENTIFY DEVICE page we will dump
885 * Dump selected 16-bit words from a detected device's
886 * IDENTIFY PAGE page.
892 static inline void ata_dump_id(struct ata_device *dev)
894 DPRINTK("49==0x%04x "
904 DPRINTK("80==0x%04x "
914 DPRINTK("88==0x%04x "
921 * ata_dev_identify - obtain IDENTIFY x DEVICE page
922 * @ap: port on which device we wish to probe resides
923 * @device: device bus address, starting at zero
925 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
926 * command, and read back the 512-byte device information page.
927 * The device information page is fed to us via the standard
928 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
929 * using standard PIO-IN paths)
931 * After reading the device information page, we use several
932 * bits of information from it to initialize data structures
933 * that will be used during the lifetime of the ata_device.
934 * Other data from the info page is used to disqualify certain
935 * older ATA devices we do not wish to support.
938 * Inherited from caller. Some functions called by this function
939 * obtain the host_set lock.
942 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
944 struct ata_device *dev = &ap->device[device];
948 struct ata_taskfile tf;
949 unsigned int using_edd;
951 if (!ata_dev_present(dev)) {
952 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
957 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
962 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
964 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
965 dev->class == ATA_DEV_NONE);
967 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
970 ata_tf_init(ap, &tf, device);
972 tf.protocol = ATA_PROT_PIO;
974 if (dev->class == ATA_DEV_ATA) {
975 tf.command = ATA_CMD_ID_ATA;
976 DPRINTK("do ATA identify\n");
978 tf.command = ATA_CMD_ID_ATAPI;
979 DPRINTK("do ATAPI identify\n");
982 ata_tf_to_host(ap, &tf);
984 /* crazy ATAPI devices... */
985 if (dev->class == ATA_DEV_ATAPI)
988 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT))
991 status = ata_chk_status(ap);
992 if (status & ATA_ERR) {
994 * arg! EDD works for all test cases, but seems to return
995 * the ATA signature for some ATAPI devices. Until the
996 * reason for this is found and fixed, we fix up the mess
997 * here. If IDENTIFY DEVICE returns command aborted
998 * (as ATAPI devices do), then we issue an
999 * IDENTIFY PACKET DEVICE.
1001 * ATA software reset (SRST, the default) does not appear
1002 * to have this problem.
1004 if ((using_edd) && (tf.command == ATA_CMD_ID_ATA)) {
1005 u8 err = ata_chk_err(ap);
1006 if (err & ATA_ABORTED) {
1007 dev->class = ATA_DEV_ATAPI;
1014 /* make sure we have BSY=0, DRQ=1 */
1015 if ((status & ATA_DRQ) == 0) {
1016 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) not returning id page (0x%x)\n",
1018 dev->class == ATA_DEV_ATA ? "" : "PI",
1023 /* read IDENTIFY [X] DEVICE page */
1024 if (ap->flags & ATA_FLAG_MMIO) {
1025 for (i = 0; i < ATA_ID_WORDS; i++)
1026 dev->id[i] = readw((void *)ap->ioaddr.data_addr);
1028 for (i = 0; i < ATA_ID_WORDS; i++)
1029 dev->id[i] = inw(ap->ioaddr.data_addr);
1031 /* wait for host_idle */
1032 status = ata_wait_idle(ap);
1033 if (status & (ATA_BUSY | ATA_DRQ)) {
1034 printk(KERN_WARNING "ata%u: dev %u (ATA%s?) error after id page (0x%x)\n",
1036 dev->class == ATA_DEV_ATA ? "" : "PI",
1041 ata_irq_on(ap); /* re-enable interrupts */
1043 /* print device capabilities */
1044 printk(KERN_DEBUG "ata%u: dev %u cfg "
1045 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1046 ap->id, device, dev->id[49],
1047 dev->id[82], dev->id[83], dev->id[84],
1048 dev->id[85], dev->id[86], dev->id[87],
1052 * common ATA, ATAPI feature tests
1055 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1056 if (!ata_id_has_dma(dev) || !ata_id_has_lba(dev)) {
1057 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1061 /* we require UDMA support */
1063 tmp = dev->id[ATA_ID_UDMA_MODES];
1064 if ((tmp & 0xff) == 0) {
1065 printk(KERN_DEBUG "ata%u: no udma\n", ap->id);
1071 ata_dev_parse_strings(dev);
1073 /* ATA-specific feature tests */
1074 if (dev->class == ATA_DEV_ATA) {
1075 if (!ata_id_is_ata(dev)) /* sanity check */
1078 tmp = dev->id[ATA_ID_MAJOR_VER];
1079 for (i = 14; i >= 1; i--)
1083 /* we require at least ATA-3 */
1085 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1089 if (ata_id_has_lba48(dev)) {
1090 dev->flags |= ATA_DFLAG_LBA48;
1091 dev->n_sectors = ata_id_u64(dev, 100);
1093 dev->n_sectors = ata_id_u32(dev, 60);
1096 ap->host->max_cmd_len = 16;
1098 /* print device info to dmesg */
1099 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors%s\n",
1101 ata_udma_string(udma_modes),
1102 (unsigned long long)dev->n_sectors,
1103 dev->flags & ATA_DFLAG_LBA48 ? " (lba48)" : "");
1106 /* ATAPI-specific feature tests */
1108 if (ata_id_is_ata(dev)) /* sanity check */
1111 /* see if 16-byte commands supported */
1112 tmp = dev->id[0] & 0x3;
1114 ap->host->max_cmd_len = 16;
1116 /* print device info to dmesg */
1117 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1119 ata_udma_string(udma_modes));
1122 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1126 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1129 ata_irq_on(ap); /* re-enable interrupts */
1130 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1131 DPRINTK("EXIT, err\n");
1141 static void ata_port_reset(struct ata_port *ap)
1143 unsigned int i, found = 0;
1145 ap->ops->phy_reset(ap);
1146 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1149 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1150 ata_dev_identify(ap, i);
1151 if (ata_dev_present(&ap->device[i])) {
1153 if (ap->ops->dev_config)
1154 ap->ops->dev_config(ap, &ap->device[i]);
1158 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1159 goto err_out_disable;
1162 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1163 goto err_out_disable;
1165 ap->thr_state = THR_PROBE_SUCCESS;
1170 ap->ops->port_disable(ap);
1172 ap->thr_state = THR_PROBE_FAILED;
1182 void ata_port_probe(struct ata_port *ap)
1184 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1194 void sata_phy_reset(struct ata_port *ap)
1197 unsigned long timeout = jiffies + (HZ * 5);
1199 if (ap->flags & ATA_FLAG_SATA_RESET) {
1200 scr_write(ap, SCR_CONTROL, 0x301); /* issue phy wake/reset */
1201 scr_read(ap, SCR_STATUS); /* dummy read; flush */
1202 udelay(400); /* FIXME: a guess */
1204 scr_write(ap, SCR_CONTROL, 0x300); /* issue phy wake/clear reset */
1206 /* wait for phy to become ready, if necessary */
1209 sstatus = scr_read(ap, SCR_STATUS);
1210 if ((sstatus & 0xf) != 1)
1212 } while (time_before(jiffies, timeout));
1214 /* TODO: phy layer with polling, timeouts, etc. */
1215 if (sata_dev_present(ap))
1218 sstatus = scr_read(ap, SCR_STATUS);
1219 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1221 ata_port_disable(ap);
1224 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1227 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1228 ata_port_disable(ap);
1236 * ata_port_disable -
1242 void ata_port_disable(struct ata_port *ap)
1244 ap->device[0].class = ATA_DEV_NONE;
1245 ap->device[1].class = ATA_DEV_NONE;
1246 ap->flags |= ATA_FLAG_PORT_DISABLED;
1250 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1251 * @ap: port on which timings will be programmed
1256 static void ata_set_mode(struct ata_port *ap)
1258 unsigned int force_pio, i;
1260 ata_host_set_pio(ap);
1261 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1264 ata_host_set_udma(ap);
1265 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1268 #ifdef ATA_FORCE_PIO
1275 ata_dev_set_pio(ap, 0);
1276 ata_dev_set_pio(ap, 1);
1278 ata_dev_set_udma(ap, 0);
1279 ata_dev_set_udma(ap, 1);
1282 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1285 if (ap->ops->post_set_mode)
1286 ap->ops->post_set_mode(ap);
1288 for (i = 0; i < 2; i++) {
1289 struct ata_device *dev = &ap->device[i];
1290 ata_dev_set_protocol(dev);
1295 * ata_busy_sleep - sleep until BSY clears, or timeout
1296 * @ap: port containing status register to be polled
1297 * @tmout_pat: impatience timeout
1298 * @tmout: overall timeout
1304 static unsigned int ata_busy_sleep (struct ata_port *ap,
1305 unsigned long tmout_pat,
1306 unsigned long tmout)
1308 unsigned long timer_start, timeout;
1311 status = ata_busy_wait(ap, ATA_BUSY, 300);
1312 timer_start = jiffies;
1313 timeout = timer_start + tmout_pat;
1314 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1316 status = ata_busy_wait(ap, ATA_BUSY, 3);
1319 if (status & ATA_BUSY)
1320 printk(KERN_WARNING "ata%u is slow to respond, "
1321 "please be patient\n", ap->id);
1323 timeout = timer_start + tmout;
1324 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1326 status = ata_chk_status(ap);
1329 if (status & ATA_BUSY) {
1330 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1331 ap->id, tmout / HZ);
1338 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1340 struct ata_ioports *ioaddr = &ap->ioaddr;
1341 unsigned int dev0 = devmask & (1 << 0);
1342 unsigned int dev1 = devmask & (1 << 1);
1343 unsigned long timeout;
1345 /* if device 0 was found in ata_dev_devchk, wait for its
1349 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1351 /* if device 1 was found in ata_dev_devchk, wait for
1352 * register access, then wait for BSY to clear
1354 timeout = jiffies + ATA_TMOUT_BOOT;
1358 __ata_dev_select(ap, 1);
1359 if (ap->flags & ATA_FLAG_MMIO) {
1360 nsect = readb((void *) ioaddr->nsect_addr);
1361 lbal = readb((void *) ioaddr->lbal_addr);
1363 nsect = inb(ioaddr->nsect_addr);
1364 lbal = inb(ioaddr->lbal_addr);
1366 if ((nsect == 1) && (lbal == 1))
1368 if (time_after(jiffies, timeout)) {
1372 msleep(50); /* give drive a breather */
1375 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1377 /* is all this really necessary? */
1378 __ata_dev_select(ap, 0);
1380 __ata_dev_select(ap, 1);
1382 __ata_dev_select(ap, 0);
1393 static unsigned int ata_bus_edd(struct ata_port *ap)
1395 struct ata_taskfile tf;
1397 /* set up execute-device-diag (bus reset) taskfile */
1398 /* also, take interrupts to a known state (disabled) */
1399 DPRINTK("execute-device-diag\n");
1400 ata_tf_init(ap, &tf, 0);
1402 tf.command = ATA_CMD_EDD;
1403 tf.protocol = ATA_PROT_NODATA;
1406 ata_tf_to_host(ap, &tf);
1408 /* spec says at least 2ms. but who knows with those
1409 * crazy ATAPI devices...
1413 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1416 static unsigned int ata_bus_softreset(struct ata_port *ap,
1417 unsigned int devmask)
1419 struct ata_ioports *ioaddr = &ap->ioaddr;
1421 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1423 /* software reset. causes dev0 to be selected */
1424 if (ap->flags & ATA_FLAG_MMIO) {
1425 writeb(ap->ctl, ioaddr->ctl_addr);
1426 udelay(20); /* FIXME: flush */
1427 writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1428 udelay(20); /* FIXME: flush */
1429 writeb(ap->ctl, ioaddr->ctl_addr);
1431 outb(ap->ctl, ioaddr->ctl_addr);
1433 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1435 outb(ap->ctl, ioaddr->ctl_addr);
1438 /* spec mandates ">= 2ms" before checking status.
1439 * We wait 150ms, because that was the magic delay used for
1440 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1441 * between when the ATA command register is written, and then
1442 * status is checked. Because waiting for "a while" before
1443 * checking status is fine, post SRST, we perform this magic
1444 * delay here as well.
1448 ata_bus_post_reset(ap, devmask);
1454 * ata_bus_reset - reset host port and associated ATA channel
1455 * @ap: port to reset
1457 * This is typically the first time we actually start issuing
1458 * commands to the ATA channel. We wait for BSY to clear, then
1459 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1460 * result. Determine what devices, if any, are on the channel
1461 * by looking at the device 0/1 error register. Look at the signature
1462 * stored in each device's taskfile registers, to determine if
1463 * the device is ATA or ATAPI.
1466 * Inherited from caller. Some functions called by this function
1467 * obtain the host_set lock.
1470 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1473 void ata_bus_reset(struct ata_port *ap)
1475 struct ata_ioports *ioaddr = &ap->ioaddr;
1476 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1478 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1480 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1482 /* determine if device 0/1 are present */
1483 if (ap->flags & ATA_FLAG_SATA_RESET)
1486 dev0 = ata_dev_devchk(ap, 0);
1488 dev1 = ata_dev_devchk(ap, 1);
1492 devmask |= (1 << 0);
1494 devmask |= (1 << 1);
1496 /* select device 0 again */
1497 __ata_dev_select(ap, 0);
1499 /* issue bus reset */
1500 if (ap->flags & ATA_FLAG_SRST)
1501 rc = ata_bus_softreset(ap, devmask);
1502 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1503 /* set up device control */
1504 if (ap->flags & ATA_FLAG_MMIO)
1505 writeb(ap->ctl, ioaddr->ctl_addr);
1507 outb(ap->ctl, ioaddr->ctl_addr);
1508 rc = ata_bus_edd(ap);
1515 * determine by signature whether we have ATA or ATAPI devices
1517 err = ata_dev_try_classify(ap, 0);
1518 if ((slave_possible) && (err != 0x81))
1519 ata_dev_try_classify(ap, 1);
1521 /* re-enable interrupts */
1524 /* is double-select really necessary? */
1525 if (ap->device[1].class != ATA_DEV_NONE)
1526 __ata_dev_select(ap, 1);
1527 if (ap->device[0].class != ATA_DEV_NONE)
1528 __ata_dev_select(ap, 0);
1530 /* if no devices were detected, disable this port */
1531 if ((ap->device[0].class == ATA_DEV_NONE) &&
1532 (ap->device[1].class == ATA_DEV_NONE))
1535 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1536 /* set up device control for ATA_FLAG_SATA_RESET */
1537 if (ap->flags & ATA_FLAG_MMIO)
1538 writeb(ap->ctl, ioaddr->ctl_addr);
1540 outb(ap->ctl, ioaddr->ctl_addr);
1547 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1548 ap->ops->port_disable(ap);
1554 * ata_host_set_pio -
1560 static void ata_host_set_pio(struct ata_port *ap)
1562 struct ata_device *master, *slave;
1563 unsigned int pio, i;
1566 master = &ap->device[0];
1567 slave = &ap->device[1];
1569 assert (ata_dev_present(master) || ata_dev_present(slave));
1571 mask = ap->pio_mask;
1572 if (ata_dev_present(master))
1573 mask &= (master->id[ATA_ID_PIO_MODES] & 0x03);
1574 if (ata_dev_present(slave))
1575 mask &= (slave->id[ATA_ID_PIO_MODES] & 0x03);
1577 /* require pio mode 3 or 4 support for host and all devices */
1579 printk(KERN_WARNING "ata%u: no PIO3/4 support, ignoring\n",
1584 pio = (mask & ATA_ID_PIO4) ? 4 : 3;
1585 for (i = 0; i < ATA_MAX_DEVICES; i++)
1586 if (ata_dev_present(&ap->device[i])) {
1587 ap->device[i].pio_mode = (pio == 3) ?
1588 XFER_PIO_3 : XFER_PIO_4;
1589 if (ap->ops->set_piomode)
1590 ap->ops->set_piomode(ap, &ap->device[i], pio);
1596 ap->ops->port_disable(ap);
1600 * ata_host_set_udma -
1606 static void ata_host_set_udma(struct ata_port *ap)
1608 struct ata_device *master, *slave;
1613 master = &ap->device[0];
1614 slave = &ap->device[1];
1616 assert (ata_dev_present(master) || ata_dev_present(slave));
1617 assert ((ap->flags & ATA_FLAG_PORT_DISABLED) == 0);
1619 DPRINTK("udma masks: host 0x%X, master 0x%X, slave 0x%X\n",
1621 (!ata_dev_present(master)) ? 0xff :
1622 (master->id[ATA_ID_UDMA_MODES] & 0xff),
1623 (!ata_dev_present(slave)) ? 0xff :
1624 (slave->id[ATA_ID_UDMA_MODES] & 0xff));
1626 mask = ap->udma_mask;
1627 if (ata_dev_present(master))
1628 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1629 if (ata_dev_present(slave))
1630 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1633 while (i >= XFER_UDMA_0) {
1634 j = i - XFER_UDMA_0;
1635 DPRINTK("mask 0x%X i 0x%X j %u\n", mask, i, j);
1636 if (mask & (1 << j)) {
1644 /* require udma for host and all attached devices */
1645 if (udma_mode < 0) {
1646 printk(KERN_WARNING "ata%u: no UltraDMA support, ignoring\n",
1651 for (i = 0; i < ATA_MAX_DEVICES; i++)
1652 if (ata_dev_present(&ap->device[i])) {
1653 ap->device[i].udma_mode = udma_mode;
1654 if (ap->ops->set_udmamode)
1655 ap->ops->set_udmamode(ap, &ap->device[i],
1662 ap->ops->port_disable(ap);
1666 * ata_dev_set_xfermode -
1673 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1675 struct ata_taskfile tf;
1677 /* set up set-features taskfile */
1678 DPRINTK("set features - xfer mode\n");
1679 ata_tf_init(ap, &tf, dev->devno);
1681 tf.command = ATA_CMD_SET_FEATURES;
1682 tf.feature = SETFEATURES_XFER;
1683 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1684 tf.protocol = ATA_PROT_NODATA;
1685 if (dev->flags & ATA_DFLAG_PIO)
1686 tf.nsect = dev->pio_mode;
1688 tf.nsect = dev->udma_mode;
1691 ata_tf_to_host(ap, &tf);
1693 /* crazy ATAPI devices... */
1694 if (dev->class == ATA_DEV_ATAPI)
1697 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1699 ata_irq_on(ap); /* re-enable interrupts */
1707 * ata_dev_set_udma -
1714 static void ata_dev_set_udma(struct ata_port *ap, unsigned int device)
1716 struct ata_device *dev = &ap->device[device];
1718 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1721 ata_dev_set_xfermode(ap, dev);
1723 assert((dev->udma_mode >= XFER_UDMA_0) &&
1724 (dev->udma_mode <= XFER_UDMA_7));
1725 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1727 udma_str[dev->udma_mode - XFER_UDMA_0]);
1738 static void ata_dev_set_pio(struct ata_port *ap, unsigned int device)
1740 struct ata_device *dev = &ap->device[device];
1742 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1745 /* force PIO mode */
1746 dev->flags |= ATA_DFLAG_PIO;
1748 ata_dev_set_xfermode(ap, dev);
1750 assert((dev->pio_mode >= XFER_PIO_3) &&
1751 (dev->pio_mode <= XFER_PIO_4));
1752 printk(KERN_INFO "ata%u: dev %u configured for PIO%c\n",
1754 dev->pio_mode == 3 ? '3' : '4');
1764 static void ata_sg_clean(struct ata_queued_cmd *qc)
1766 struct ata_port *ap = qc->ap;
1767 struct scsi_cmnd *cmd = qc->scsicmd;
1768 struct scatterlist *sg = qc->sg;
1769 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1771 assert(dir == SCSI_DATA_READ || dir == SCSI_DATA_WRITE);
1772 assert(qc->flags & ATA_QCFLAG_SG);
1776 assert(qc->n_elem == 1);
1778 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
1781 pci_unmap_sg(ap->host_set->pdev, sg, qc->n_elem, dir);
1783 pci_unmap_single(ap->host_set->pdev, sg_dma_address(&sg[0]),
1784 sg_dma_len(&sg[0]), dir);
1786 qc->flags &= ~ATA_QCFLAG_SG;
1797 void ata_fill_sg(struct ata_queued_cmd *qc)
1799 struct scatterlist *sg = qc->sg;
1800 struct ata_port *ap = qc->ap;
1801 unsigned int idx, nelem;
1804 assert(qc->n_elem > 0);
1807 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
1811 /* determine if physical DMA addr spans 64K boundary.
1812 * Note h/w doesn't support 64-bit, so we unconditionally
1813 * truncate dma_addr_t to u32.
1815 addr = (u32) sg_dma_address(sg);
1816 sg_len = sg_dma_len(sg);
1819 boundary = (addr & ~0xffff) + (0xffff + 1);
1821 if ((addr + sg_len) > boundary)
1822 len = boundary - addr;
1824 ap->prd[idx].addr = cpu_to_le32(addr);
1825 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
1826 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
1835 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
1839 * ata_sg_setup_one -
1843 * spin_lock_irqsave(host_set lock)
1849 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
1851 struct ata_port *ap = qc->ap;
1852 struct scsi_cmnd *cmd = qc->scsicmd;
1853 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1854 struct scatterlist *sg = qc->sg;
1855 unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1856 dma_addr_t dma_address;
1858 assert(sg == &qc->sgent);
1859 assert(qc->n_elem == 1);
1861 sg->page = virt_to_page(cmd->request_buffer);
1862 sg->offset = (unsigned long) cmd->request_buffer & ~PAGE_MASK;
1863 sg_dma_len(sg) = cmd->request_bufflen;
1868 dma_address = pci_map_single(ap->host_set->pdev, cmd->request_buffer,
1869 cmd->request_bufflen, dir);
1870 if (pci_dma_mapping_error(dma_address))
1873 sg_dma_address(sg) = dma_address;
1875 DPRINTK("mapped buffer of %d bytes for %s\n", cmd->request_bufflen,
1876 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1886 * spin_lock_irqsave(host_set lock)
1892 static int ata_sg_setup(struct ata_queued_cmd *qc)
1894 struct ata_port *ap = qc->ap;
1895 struct scsi_cmnd *cmd = qc->scsicmd;
1896 struct scatterlist *sg;
1898 unsigned int have_sg = (qc->flags & ATA_QCFLAG_SG);
1900 VPRINTK("ENTER, ata%u, use_sg %d\n", ap->id, cmd->use_sg);
1901 assert(cmd->use_sg > 0);
1903 sg = (struct scatterlist *)cmd->request_buffer;
1905 int dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1906 n_elem = pci_map_sg(ap->host_set->pdev, sg, cmd->use_sg, dir);
1909 DPRINTK("%d sg elements mapped\n", n_elem);
1911 n_elem = cmd->use_sg;
1913 qc->n_elem = n_elem;
1928 static unsigned long ata_pio_poll(struct ata_port *ap)
1931 unsigned int poll_state = THR_UNKNOWN;
1932 unsigned int reg_state = THR_UNKNOWN;
1933 const unsigned int tmout_state = THR_PIO_TMOUT;
1935 switch (ap->thr_state) {
1938 poll_state = THR_PIO_POLL;
1939 reg_state = THR_PIO;
1942 case THR_PIO_LAST_POLL:
1943 poll_state = THR_PIO_LAST_POLL;
1944 reg_state = THR_PIO_LAST;
1951 status = ata_chk_status(ap);
1952 if (status & ATA_BUSY) {
1953 if (time_after(jiffies, ap->thr_timeout)) {
1954 ap->thr_state = tmout_state;
1957 ap->thr_state = poll_state;
1958 return ATA_SHORT_PAUSE;
1961 ap->thr_state = reg_state;
1970 * spin_lock_irqsave(host_set lock)
1973 static void ata_pio_start (struct ata_queued_cmd *qc)
1975 struct ata_port *ap = qc->ap;
1977 assert(qc->tf.protocol == ATA_PROT_PIO);
1979 qc->flags |= ATA_QCFLAG_POLL;
1980 qc->tf.ctl |= ATA_NIEN; /* disable interrupts */
1981 ata_tf_to_host_nolock(ap, &qc->tf);
1982 ata_thread_wake(ap, THR_PIO);
1986 * ata_pio_complete -
1992 static void ata_pio_complete (struct ata_port *ap)
1994 struct ata_queued_cmd *qc;
1995 unsigned long flags;
1999 * This is purely hueristic. This is a fast path.
2000 * Sometimes when we enter, BSY will be cleared in
2001 * a chk-status or two. If not, the drive is probably seeking
2002 * or something. Snooze for a couple msecs, then
2003 * chk-status again. If still busy, fall back to
2004 * THR_PIO_POLL state.
2006 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2007 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2009 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2010 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2011 ap->thr_state = THR_PIO_LAST_POLL;
2012 ap->thr_timeout = jiffies + ATA_TMOUT_PIO;
2017 drv_stat = ata_wait_idle(ap);
2018 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2019 ap->thr_state = THR_PIO_ERR;
2023 qc = ata_qc_from_tag(ap, ap->active_tag);
2026 spin_lock_irqsave(&ap->host_set->lock, flags);
2027 ap->thr_state = THR_IDLE;
2028 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2032 ata_qc_complete(qc, drv_stat, 0);
2042 static void ata_pio_sector(struct ata_port *ap)
2044 struct ata_queued_cmd *qc;
2045 struct scatterlist *sg;
2046 struct scsi_cmnd *cmd;
2051 * This is purely hueristic. This is a fast path.
2052 * Sometimes when we enter, BSY will be cleared in
2053 * a chk-status or two. If not, the drive is probably seeking
2054 * or something. Snooze for a couple msecs, then
2055 * chk-status again. If still busy, fall back to
2056 * THR_PIO_POLL state.
2058 status = ata_busy_wait(ap, ATA_BUSY, 5);
2059 if (status & ATA_BUSY) {
2061 status = ata_busy_wait(ap, ATA_BUSY, 10);
2062 if (status & ATA_BUSY) {
2063 ap->thr_state = THR_PIO_POLL;
2064 ap->thr_timeout = jiffies + ATA_TMOUT_PIO;
2069 /* handle BSY=0, DRQ=0 as error */
2070 if ((status & ATA_DRQ) == 0) {
2071 ap->thr_state = THR_PIO_ERR;
2075 qc = ata_qc_from_tag(ap, ap->active_tag);
2081 if (qc->cursect == (qc->nsect - 1))
2082 ap->thr_state = THR_PIO_LAST;
2084 buf = kmap(sg[qc->cursg].page) +
2085 sg[qc->cursg].offset + (qc->cursg_ofs * ATA_SECT_SIZE);
2091 if ((qc->cursg_ofs * ATA_SECT_SIZE) == sg_dma_len(&sg[qc->cursg])) {
2096 DPRINTK("data %s, drv_stat 0x%X\n",
2097 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read",
2100 /* do the actual data transfer */
2101 /* FIXME: mmio-ize */
2102 if (qc->tf.flags & ATA_TFLAG_WRITE)
2103 outsl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2105 insl(ap->ioaddr.data_addr, buf, ATA_SECT_DWORDS);
2107 kunmap(sg[qc->cursg].page);
2110 #if 0 /* to be used eventually */
2112 * ata_eng_schedule - run an iteration of the pio/dma/whatever engine
2113 * @ap: port on which activity will occur
2114 * @eng: instance of engine
2117 * spin_lock_irqsave(host_set lock)
2119 static void ata_eng_schedule (struct ata_port *ap, struct ata_engine *eng)
2126 * ata_eng_timeout - Handle timeout of queued command
2127 * @ap: Port on which timed-out command is active
2129 * Some part of the kernel (currently, only the SCSI layer)
2130 * has noticed that the active command on port @ap has not
2131 * completed after a specified length of time. Handle this
2132 * condition by disabling DMA (if necessary) and completing
2133 * transactions, with error if necessary.
2135 * This also handles the case of the "lost interrupt", where
2136 * for some reason (possibly hardware bug, possibly driver bug)
2137 * an interrupt was not delivered to the driver, even though the
2138 * transaction completed successfully.
2141 * Inherited from SCSI layer (none, can sleep)
2144 void ata_eng_timeout(struct ata_port *ap)
2146 u8 host_stat, drv_stat;
2147 struct ata_queued_cmd *qc;
2151 qc = ata_qc_from_tag(ap, ap->active_tag);
2153 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2158 /* hack alert! We cannot use the supplied completion
2159 * function from inside the ->eh_strategy_handler() thread.
2160 * libata is the only user of ->eh_strategy_handler() in
2161 * any kernel, so the default scsi_done() assumes it is
2162 * not being called from the SCSI EH.
2164 qc->scsidone = scsi_finish_command;
2166 switch (qc->tf.protocol) {
2168 if (ap->flags & ATA_FLAG_MMIO) {
2169 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2170 host_stat = readb(mmio + ATA_DMA_STATUS);
2172 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2174 printk(KERN_ERR "ata%u: DMA timeout, stat 0x%x\n",
2177 ata_dma_complete(ap, host_stat, 1);
2180 case ATA_PROT_NODATA:
2181 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2183 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x\n",
2184 ap->id, qc->tf.command, drv_stat);
2186 ata_qc_complete(qc, drv_stat, 1);
2190 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2192 printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
2193 ap->id, qc->tf.command, drv_stat);
2195 ata_qc_complete(qc, drv_stat, 1);
2211 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2213 struct ata_queued_cmd *qc = NULL;
2216 for (i = 0; i < ATA_MAX_QUEUE; i++)
2217 if (!test_and_set_bit(i, &ap->qactive)) {
2218 qc = ata_qc_from_tag(ap, i);
2236 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2237 struct ata_device *dev)
2239 struct ata_queued_cmd *qc;
2241 qc = ata_qc_new(ap);
2248 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2249 INIT_LIST_HEAD(&qc->node);
2250 init_MUTEX_LOCKED(&qc->sem);
2252 ata_tf_init(ap, &qc->tf, dev->devno);
2254 if (likely((dev->flags & ATA_DFLAG_PIO) == 0))
2255 qc->flags |= ATA_QCFLAG_DMA;
2256 if (dev->flags & ATA_DFLAG_LBA48)
2257 qc->tf.flags |= ATA_TFLAG_LBA48;
2273 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat, unsigned int done_late)
2275 struct ata_port *ap = qc->ap;
2276 struct scsi_cmnd *cmd = qc->scsicmd;
2277 unsigned int tag, do_clear = 0;
2279 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2280 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2282 if (likely(qc->flags & ATA_QCFLAG_SG))
2286 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
2287 if (qc->flags & ATA_QCFLAG_ATAPI)
2288 cmd->result = SAM_STAT_CHECK_CONDITION;
2290 ata_to_sense_error(qc);
2292 cmd->result = SAM_STAT_GOOD;
2298 qc->flags &= ~ATA_QCFLAG_ACTIVE;
2300 if (likely(ata_tag_valid(tag))) {
2301 if (tag == ap->active_tag)
2302 ap->active_tag = ATA_TAG_POISON;
2303 qc->tag = ATA_TAG_POISON;
2309 if (likely(do_clear))
2310 clear_bit(tag, &ap->qactive);
2313 #if 0 /* to be used eventually */
2320 * spin_lock_irqsave(host_set lock)
2322 static void ata_qc_push (struct ata_queued_cmd *qc, unsigned int append)
2324 struct ata_port *ap = qc->ap;
2325 struct ata_engine *eng = &ap->eng;
2328 list_add_tail(&qc->node, &eng->q);
2330 list_add(&qc->node, &eng->q);
2332 if (!test_and_set_bit(ATA_EFLG_ACTIVE, &eng->flags))
2333 ata_eng_schedule(ap, eng);
2346 int ata_qc_issue(struct ata_queued_cmd *qc)
2348 struct ata_port *ap = qc->ap;
2349 struct scsi_cmnd *cmd = qc->scsicmd;
2350 unsigned int dma = qc->flags & ATA_QCFLAG_DMA;
2352 ata_dev_select(ap, qc->dev->devno, 1, 0);
2354 /* set up SG table */
2356 if (ata_sg_setup(qc))
2359 if (ata_sg_setup_one(qc))
2363 ap->ops->fill_sg(qc);
2365 qc->ap->active_tag = qc->tag;
2366 qc->flags |= ATA_QCFLAG_ACTIVE;
2369 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2370 ap->ops->bmdma_start(qc); /* initiate bmdma */
2372 /* load tf registers, initiate polling pio */
2382 * ata_bmdma_start_mmio -
2386 * spin_lock_irqsave(host_set lock)
2389 void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
2391 struct ata_port *ap = qc->ap;
2392 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2393 u8 host_stat, dmactl;
2394 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2396 /* load PRD table addr. */
2397 mb(); /* make sure PRD table writes are visible to controller */
2398 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
2400 /* specify data direction, triple-check start bit is clear */
2401 dmactl = readb(mmio + ATA_DMA_CMD);
2402 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2404 dmactl |= ATA_DMA_WR;
2405 writeb(dmactl, mmio + ATA_DMA_CMD);
2407 /* clear interrupt, error bits */
2408 host_stat = readb(mmio + ATA_DMA_STATUS);
2409 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR, mmio + ATA_DMA_STATUS);
2411 /* issue r/w command */
2412 ap->ops->exec_command(ap, &qc->tf);
2414 /* start host DMA transaction */
2415 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
2417 /* Strictly, one may wish to issue a readb() here, to
2418 * flush the mmio write. However, control also passes
2419 * to the hardware at this point, and it will interrupt
2420 * us when we are to resume control. So, in effect,
2421 * we don't care when the mmio write flushes.
2422 * Further, a read of the DMA status register _immediately_
2423 * following the write may not be what certain flaky hardware
2424 * is expected, so I think it is best to not add a readb()
2425 * without first all the MMIO ATA cards/mobos.
2426 * Or maybe I'm just being paranoid.
2431 * ata_bmdma_start_pio -
2435 * spin_lock_irqsave(host_set lock)
2438 void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
2440 struct ata_port *ap = qc->ap;
2441 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2442 u8 host_stat, dmactl;
2444 /* load PRD table addr. */
2445 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2447 /* specify data direction, triple-check start bit is clear */
2448 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2449 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2451 dmactl |= ATA_DMA_WR;
2452 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2454 /* clear interrupt, error bits */
2455 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2456 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2457 ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2459 /* issue r/w command */
2460 ap->ops->exec_command(ap, &qc->tf);
2462 /* start host DMA transaction */
2463 outb(dmactl | ATA_DMA_START,
2464 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2468 * ata_dma_complete -
2476 static void ata_dma_complete(struct ata_port *ap, u8 host_stat,
2477 unsigned int done_late)
2481 if (ap->flags & ATA_FLAG_MMIO) {
2482 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2484 /* clear start/stop bit */
2485 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2486 mmio + ATA_DMA_CMD);
2488 /* ack intr, err bits */
2489 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2490 mmio + ATA_DMA_STATUS);
2492 /* clear start/stop bit */
2493 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
2494 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2496 /* ack intr, err bits */
2497 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
2498 ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2502 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2503 ata_altstatus(ap); /* dummy read */
2505 DPRINTK("host %u, host_stat==0x%X, drv_stat==0x%X\n",
2506 ap->id, (u32) host_stat, (u32) ata_chk_status(ap));
2508 /* get drive status; clear intr; complete txn */
2509 ata_qc_complete(ata_qc_from_tag(ap, ap->active_tag),
2510 ata_wait_idle(ap), done_late);
2514 * ata_host_intr - Handle host interrupt for given (port, task)
2515 * @ap: Port on which interrupt arrived (possibly...)
2516 * @qc: Taskfile currently active in engine
2518 * Handle host interrupt for given queued command. Currently,
2519 * only DMA interrupts are handled. All other commands are
2520 * handled via polling with interrupts disabled (nIEN bit).
2523 * spin_lock_irqsave(host_set lock)
2526 * One if interrupt was handled, zero if not (shared irq).
2529 inline unsigned int ata_host_intr (struct ata_port *ap,
2530 struct ata_queued_cmd *qc)
2532 u8 status, host_stat;
2533 unsigned int handled = 0;
2535 switch (qc->tf.protocol) {
2537 if (ap->flags & ATA_FLAG_MMIO) {
2538 void *mmio = (void *) ap->ioaddr.bmdma_addr;
2539 host_stat = readb(mmio + ATA_DMA_STATUS);
2541 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2542 VPRINTK("BUS_DMA (host_stat 0x%X)\n", host_stat);
2544 if (!(host_stat & ATA_DMA_INTR)) {
2545 ap->stats.idle_irq++;
2549 ata_dma_complete(ap, host_stat, 0);
2553 case ATA_PROT_NODATA: /* command completion, but no data xfer */
2554 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2555 DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
2556 ata_qc_complete(qc, status, 0);
2561 ap->stats.idle_irq++;
2564 if ((ap->stats.idle_irq % 1000) == 0) {
2566 ata_irq_ack(ap, 0); /* debug trap */
2567 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
2588 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
2590 struct ata_host_set *host_set = dev_instance;
2592 unsigned int handled = 0;
2593 unsigned long flags;
2595 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
2596 spin_lock_irqsave(&host_set->lock, flags);
2598 for (i = 0; i < host_set->n_ports; i++) {
2599 struct ata_port *ap;
2601 ap = host_set->ports[i];
2602 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
2603 struct ata_queued_cmd *qc;
2605 qc = ata_qc_from_tag(ap, ap->active_tag);
2606 if (qc && ((qc->flags & ATA_QCFLAG_POLL) == 0))
2607 handled += ata_host_intr(ap, qc);
2611 spin_unlock_irqrestore(&host_set->lock, flags);
2613 return IRQ_RETVAL(handled);
2622 * spin_lock_irqsave(host_set lock)
2625 void ata_thread_wake(struct ata_port *ap, unsigned int thr_state)
2627 assert(ap->thr_state == THR_IDLE);
2628 ap->thr_state = thr_state;
2633 * ata_thread_timer -
2639 static void ata_thread_timer(unsigned long opaque)
2641 struct ata_port *ap = (struct ata_port *) opaque;
2656 static unsigned long ata_thread_iter(struct ata_port *ap)
2660 DPRINTK("ata%u: thr_state %s\n",
2661 ap->id, ata_thr_state_name(ap->thr_state));
2663 switch (ap->thr_state) {
2665 ap->thr_state = THR_PORT_RESET;
2668 case THR_PROBE_START:
2670 ap->thr_state = THR_PORT_RESET;
2673 case THR_PORT_RESET:
2677 case THR_PROBE_SUCCESS:
2679 ap->thr_state = THR_IDLE;
2682 case THR_PROBE_FAILED:
2684 ap->thr_state = THR_AWAIT_DEATH;
2687 case THR_AWAIT_DEATH:
2700 ata_pio_complete(ap);
2704 case THR_PIO_LAST_POLL:
2705 timeout = ata_pio_poll(ap);
2709 printk(KERN_ERR "ata%d: FIXME: THR_PIO_TMOUT\n", /* FIXME */
2715 printk(KERN_ERR "ata%d: FIXME: THR_PIO_ERR\n", /* FIXME */
2725 printk(KERN_DEBUG "ata%u: unknown thr state %s\n",
2726 ap->id, ata_thr_state_name(ap->thr_state));
2730 DPRINTK("ata%u: new thr_state %s, returning %ld\n",
2731 ap->id, ata_thr_state_name(ap->thr_state), timeout);
2745 static int ata_thread (void *data)
2747 struct ata_port *ap = data;
2750 daemonize ("katad-%u", ap->id);
2751 allow_signal(SIGTERM);
2756 timeout = ata_thread_iter(ap);
2758 if (signal_pending (current))
2759 flush_signals(current);
2761 if (current->flags & PF_FREEZE)
2762 refrigerator(PF_FREEZE);
2765 if ((timeout < 0) || (ap->time_to_die))
2768 /* note sleeping for full timeout not guaranteed (that's ok) */
2770 mod_timer(&ap->thr_timer, jiffies + timeout);
2771 down_interruptible(&ap->thr_sem);
2773 if (signal_pending (current))
2774 flush_signals(current);
2776 if (ap->time_to_die)
2781 printk(KERN_DEBUG "ata%u: thread exiting\n", ap->id);
2783 del_timer_sync(&ap->thr_timer);
2784 complete_and_exit (&ap->thr_exited, 0);
2788 * ata_thread_kill - kill per-port kernel thread
2789 * @ap: port those thread is to be killed
2795 static int ata_thread_kill(struct ata_port *ap)
2799 if (ap->thr_pid >= 0) {
2800 ap->time_to_die = 1;
2802 ret = kill_proc(ap->thr_pid, SIGTERM, 1);
2804 printk(KERN_ERR "ata%d: unable to kill kernel thread\n",
2807 wait_for_completion(&ap->thr_exited);
2814 * atapi_cdb_send - Write CDB bytes to hardware
2815 * @ap: Port to which ATAPI device is attached.
2817 * When device has indicated its readiness to accept
2818 * a CDB, this function is called. Send the CDB.
2819 * If DMA is to be performed, exit immediately.
2820 * Otherwise, we are in polling mode, so poll
2821 * status under operation succeeds or fails.
2824 * Kernel thread context (may sleep)
2827 static void atapi_cdb_send(struct ata_port *ap)
2829 struct ata_queued_cmd *qc;
2832 qc = ata_qc_from_tag(ap, ap->active_tag);
2834 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2836 /* sleep-wait for BSY to clear */
2837 DPRINTK("busy wait\n");
2838 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
2841 /* make sure DRQ is set */
2842 status = ata_chk_status(ap);
2843 if ((status & ATA_DRQ) == 0)
2847 /* FIXME: mmio-ize */
2848 DPRINTK("send cdb\n");
2849 outsl(ap->ioaddr.data_addr,
2850 qc->scsicmd->cmnd, ap->host->max_cmd_len / 4);
2852 /* if we are DMA'ing, irq handler takes over from here */
2853 if (qc->tf.feature == ATAPI_PKT_DMA)
2856 /* sleep-wait for BSY to clear */
2857 DPRINTK("busy wait 2\n");
2858 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
2861 /* wait for BSY,DRQ to clear */
2862 status = ata_wait_idle(ap);
2863 if (status & (ATA_BUSY | ATA_DRQ))
2866 /* transaction completed, indicate such to scsi stack */
2867 ata_qc_complete(qc, status, 0);
2871 ap->thr_state = THR_IDLE;
2875 ata_qc_complete(qc, ATA_ERR, 0);
2879 int ata_port_start (struct ata_port *ap)
2881 struct pci_dev *pdev = ap->host_set->pdev;
2883 ap->prd = pci_alloc_consistent(pdev, ATA_PRD_TBL_SZ, &ap->prd_dma);
2887 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
2892 void ata_port_stop (struct ata_port *ap)
2894 struct pci_dev *pdev = ap->host_set->pdev;
2896 pci_free_consistent(pdev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
2907 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
2909 struct Scsi_Host *sh = ap->host;
2914 scsi_remove_host(sh);
2916 ata_thread_kill(ap); /* FIXME: check return val */
2918 ap->ops->port_stop(ap);
2931 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
2932 struct ata_host_set *host_set,
2933 struct ata_probe_ent *ent, unsigned int port_no)
2939 host->max_channel = 1;
2940 host->unique_id = ata_unique_id++;
2941 host->max_cmd_len = 12;
2942 scsi_set_device(host, &ent->pdev->dev);
2943 scsi_assign_lock(host, &host_set->lock);
2945 ap->flags = ATA_FLAG_PORT_DISABLED;
2946 ap->id = host->unique_id;
2948 ap->ctl = ATA_DEVCTL_OBS;
2949 ap->host_set = host_set;
2950 ap->port_no = port_no;
2951 ap->pio_mask = ent->pio_mask;
2952 ap->udma_mask = ent->udma_mask;
2953 ap->flags |= ent->host_flags;
2954 ap->ops = ent->port_ops;
2955 ap->thr_state = THR_PROBE_START;
2956 ap->cbl = ATA_CBL_NONE;
2957 ap->device[0].flags = ATA_DFLAG_MASTER;
2958 ap->active_tag = ATA_TAG_POISON;
2959 ap->last_ctl = 0xFF;
2961 /* ata_engine init */
2963 INIT_LIST_HEAD(&ap->eng.q);
2965 for (i = 0; i < ATA_MAX_DEVICES; i++)
2966 ap->device[i].devno = i;
2968 init_completion(&ap->thr_exited);
2969 init_MUTEX_LOCKED(&ap->probe_sem);
2970 init_MUTEX_LOCKED(&ap->sem);
2971 init_MUTEX_LOCKED(&ap->thr_sem);
2973 init_timer(&ap->thr_timer);
2974 ap->thr_timer.function = ata_thread_timer;
2975 ap->thr_timer.data = (unsigned long) ap;
2978 ap->stats.unhandled_irq = 1;
2979 ap->stats.idle_irq = 1;
2982 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
2997 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
2998 struct ata_host_set *host_set,
2999 unsigned int port_no)
3001 struct Scsi_Host *host;
3002 struct ata_port *ap;
3006 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
3010 ap = (struct ata_port *) &host->hostdata[0];
3012 ata_host_init(ap, host, host_set, ent, port_no);
3014 rc = ap->ops->port_start(ap);
3018 ap->thr_pid = kernel_thread(ata_thread, ap, CLONE_FS | CLONE_FILES);
3019 if (ap->thr_pid < 0) {
3020 printk(KERN_ERR "ata%d: unable to start kernel thread\n",
3028 ap->ops->port_stop(ap);
3031 scsi_host_put(host);
3045 int ata_device_add(struct ata_probe_ent *ent)
3047 unsigned int count = 0, i;
3048 struct pci_dev *pdev = ent->pdev;
3049 struct ata_host_set *host_set;
3052 /* alloc a container for our list of ATA ports (buses) */
3053 host_set = kmalloc(sizeof(struct ata_host_set) +
3054 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
3057 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
3058 spin_lock_init(&host_set->lock);
3060 host_set->pdev = pdev;
3061 host_set->n_ports = ent->n_ports;
3062 host_set->irq = ent->irq;
3063 host_set->mmio_base = ent->mmio_base;
3064 host_set->private_data = ent->private_data;
3066 /* register each port bound to this device */
3067 for (i = 0; i < ent->n_ports; i++) {
3068 struct ata_port *ap;
3070 ap = ata_host_add(ent, host_set, i);
3074 host_set->ports[i] = ap;
3076 /* print per-port info to dmesg */
3077 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3078 "bmdma 0x%lX irq %lu\n",
3080 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
3081 ata_udma_string(ent->udma_mask),
3082 ap->ioaddr.cmd_addr,
3083 ap->ioaddr.ctl_addr,
3084 ap->ioaddr.bmdma_addr,
3095 /* obtain irq, that is shared between channels */
3096 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
3097 DRV_NAME, host_set))
3100 /* perform each probe synchronously */
3101 DPRINTK("probe begin\n");
3102 for (i = 0; i < count; i++) {
3103 struct ata_port *ap;
3106 ap = host_set->ports[i];
3108 DPRINTK("ata%u: probe begin\n", ap->id);
3109 up(&ap->sem); /* start probe */
3111 DPRINTK("ata%u: probe-wait begin\n", ap->id);
3112 down(&ap->probe_sem); /* wait for end */
3114 DPRINTK("ata%u: probe-wait end\n", ap->id);
3116 rc = scsi_add_host(ap->host, &pdev->dev);
3118 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
3120 /* FIXME: do something useful here */
3121 /* FIXME: handle unconditional calls to
3122 * scsi_scan_host and ata_host_remove, below,
3128 /* probes are done, now scan each port's disk(s) */
3129 DPRINTK("probe begin\n");
3130 for (i = 0; i < count; i++) {
3131 struct ata_port *ap = host_set->ports[i];
3133 scsi_scan_host(ap->host);
3136 pci_set_drvdata(pdev, host_set);
3138 VPRINTK("EXIT, returning %u\n", ent->n_ports);
3139 return ent->n_ports; /* success */
3142 for (i = 0; i < count; i++) {
3143 ata_host_remove(host_set->ports[i], 1);
3144 scsi_host_put(host_set->ports[i]->host);
3147 VPRINTK("EXIT, returning 0\n");
3152 * ata_scsi_release - SCSI layer callback hook for host unload
3153 * @host: libata host to be unloaded
3155 * Performs all duties necessary to shut down a libata port:
3156 * Kill port kthread, disable port, and release resources.
3159 * Inherited from SCSI layer.
3165 int ata_scsi_release(struct Scsi_Host *host)
3167 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3171 ap->ops->port_disable(ap);
3172 ata_host_remove(ap, 0);
3179 * ata_std_ports - initialize ioaddr with standard port offsets.
3182 void ata_std_ports(struct ata_ioports *ioaddr)
3184 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3185 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3186 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3187 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3188 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3189 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3190 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3191 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3192 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3193 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3197 * ata_pci_init_one -
3203 * Inherited from PCI layer (may sleep).
3209 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3210 unsigned int n_ports)
3212 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3213 struct ata_port_info *port0, *port1;
3215 unsigned int legacy_mode = 0;
3220 port0 = port_info[0];
3222 port1 = port_info[1];
3226 if ((port0->host_flags & ATA_FLAG_NO_LEGACY) == 0) {
3227 /* TODO: support transitioning to native mode? */
3228 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3229 mask = (1 << 2) | (1 << 0);
3230 if ((tmp8 & mask) != mask)
3231 legacy_mode = (1 << 3);
3235 if ((!legacy_mode) && (n_ports > 1)) {
3236 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3240 rc = pci_enable_device(pdev);
3244 rc = pci_request_regions(pdev, DRV_NAME);
3249 if (!request_region(0x1f0, 8, "libata")) {
3250 struct resource *conflict, res;
3252 res.end = 0x1f0 + 8 - 1;
3253 conflict = ____request_resource(&ioport_resource, &res);
3254 if (!strcmp(conflict->name, "libata"))
3255 legacy_mode |= (1 << 0);
3257 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3259 legacy_mode |= (1 << 0);
3261 if (!request_region(0x170, 8, "libata")) {
3262 struct resource *conflict, res;
3264 res.end = 0x170 + 8 - 1;
3265 conflict = ____request_resource(&ioport_resource, &res);
3266 if (!strcmp(conflict->name, "libata"))
3267 legacy_mode |= (1 << 1);
3269 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3271 legacy_mode |= (1 << 1);
3274 /* we have legacy mode, but all ports are unavailable */
3275 if (legacy_mode == (1 << 3)) {
3277 goto err_out_regions;
3280 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3282 goto err_out_regions;
3283 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3285 goto err_out_regions;
3287 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3290 goto err_out_regions;
3293 memset(probe_ent, 0, sizeof(*probe_ent));
3294 probe_ent->pdev = pdev;
3295 INIT_LIST_HEAD(&probe_ent->node);
3298 probe_ent2 = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3301 goto err_out_free_ent;
3304 memset(probe_ent2, 0, sizeof(*probe_ent));
3305 probe_ent2->pdev = pdev;
3306 INIT_LIST_HEAD(&probe_ent2->node);
3309 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3310 probe_ent->sht = port0->sht;
3311 probe_ent->host_flags = port0->host_flags;
3312 probe_ent->pio_mask = port0->pio_mask;
3313 probe_ent->udma_mask = port0->udma_mask;
3314 probe_ent->port_ops = port0->port_ops;
3317 probe_ent->port[0].cmd_addr = 0x1f0;
3318 probe_ent->port[0].altstatus_addr =
3319 probe_ent->port[0].ctl_addr = 0x3f6;
3320 probe_ent->n_ports = 1;
3321 probe_ent->irq = 14;
3322 ata_std_ports(&probe_ent->port[0]);
3324 probe_ent2->port[0].cmd_addr = 0x170;
3325 probe_ent2->port[0].altstatus_addr =
3326 probe_ent2->port[0].ctl_addr = 0x376;
3327 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3328 probe_ent2->n_ports = 1;
3329 probe_ent2->irq = 15;
3330 ata_std_ports(&probe_ent2->port[0]);
3332 probe_ent2->sht = port1->sht;
3333 probe_ent2->host_flags = port1->host_flags;
3334 probe_ent2->pio_mask = port1->pio_mask;
3335 probe_ent2->udma_mask = port1->udma_mask;
3336 probe_ent2->port_ops = port1->port_ops;
3338 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3339 ata_std_ports(&probe_ent->port[0]);
3340 probe_ent->port[0].altstatus_addr =
3341 probe_ent->port[0].ctl_addr =
3342 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3344 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3345 ata_std_ports(&probe_ent->port[1]);
3346 probe_ent->port[1].altstatus_addr =
3347 probe_ent->port[1].ctl_addr =
3348 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3349 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3351 probe_ent->n_ports = 2;
3352 probe_ent->irq = pdev->irq;
3353 probe_ent->irq_flags = SA_SHIRQ;
3356 pci_set_master(pdev);
3358 /* FIXME: check ata_device_add return */
3360 if (legacy_mode & (1 << 0))
3361 ata_device_add(probe_ent);
3362 if (legacy_mode & (1 << 1))
3363 ata_device_add(probe_ent2);
3366 ata_device_add(probe_ent);
3367 assert(probe_ent2 == NULL);
3376 if (legacy_mode & (1 << 0))
3377 release_region(0x1f0, 8);
3378 if (legacy_mode & (1 << 1))
3379 release_region(0x170, 8);
3380 pci_release_regions(pdev);
3382 pci_disable_device(pdev);
3387 * ata_pci_remove_one - PCI layer callback for device removal
3388 * @pdev: PCI device that was removed
3390 * PCI layer indicates to libata via this hook that
3391 * hot-unplug or module unload event has occured.
3392 * Handle this by unregistering all objects associated
3393 * with this PCI device. Free those objects. Then finally
3394 * release PCI resources and disable device.
3397 * Inherited from PCI layer (may sleep).
3400 void ata_pci_remove_one (struct pci_dev *pdev)
3402 struct ata_host_set *host_set = pci_get_drvdata(pdev);
3403 struct ata_port *ap;
3406 for (i = 0; i < host_set->n_ports; i++) {
3407 ap = host_set->ports[i];
3409 scsi_remove_host(ap->host);
3412 free_irq(host_set->irq, host_set);
3413 if (host_set->mmio_base)
3414 iounmap(host_set->mmio_base);
3415 if (host_set->ports[0]->ops->host_stop)
3416 host_set->ports[0]->ops->host_stop(host_set);
3418 for (i = 0; i < host_set->n_ports; i++) {
3419 ap = host_set->ports[i];
3421 ata_scsi_release(ap->host);
3422 scsi_host_put(ap->host);
3425 pci_release_regions(pdev);
3427 for (i = 0; i < host_set->n_ports; i++) {
3428 struct ata_ioports *ioaddr;
3430 ap = host_set->ports[i];
3431 ioaddr = &ap->ioaddr;
3433 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
3434 if (ioaddr->cmd_addr == 0x1f0)
3435 release_region(0x1f0, 8);
3436 else if (ioaddr->cmd_addr == 0x170)
3437 release_region(0x170, 8);
3442 pci_disable_device(pdev);
3443 pci_set_drvdata(pdev, NULL);
3446 /* move to PCI subsystem */
3447 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
3449 unsigned long tmp = 0;
3451 switch (bits->width) {
3454 pci_read_config_byte(pdev, bits->reg, &tmp8);
3460 pci_read_config_word(pdev, bits->reg, &tmp16);
3466 pci_read_config_dword(pdev, bits->reg, &tmp32);
3477 return (tmp == bits->val) ? 1 : 0;
3490 static int __init ata_init(void)
3492 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
3496 module_init(ata_init);
3499 * libata is essentially a library of internal helper functions for
3500 * low-level ATA host controller drivers. As such, the API/ABI is
3501 * likely to change as new drivers are added and updated.
3502 * Do not depend on ABI/API stability.
3505 EXPORT_SYMBOL_GPL(pci_test_config_bits);
3506 EXPORT_SYMBOL_GPL(ata_std_bios_param);
3507 EXPORT_SYMBOL_GPL(ata_std_ports);
3508 EXPORT_SYMBOL_GPL(ata_device_add);
3509 EXPORT_SYMBOL_GPL(ata_qc_complete);
3510 EXPORT_SYMBOL_GPL(ata_eng_timeout);
3511 EXPORT_SYMBOL_GPL(ata_tf_load_pio);
3512 EXPORT_SYMBOL_GPL(ata_tf_load_mmio);
3513 EXPORT_SYMBOL_GPL(ata_tf_read_pio);
3514 EXPORT_SYMBOL_GPL(ata_tf_read_mmio);
3515 EXPORT_SYMBOL_GPL(ata_check_status_pio);
3516 EXPORT_SYMBOL_GPL(ata_check_status_mmio);
3517 EXPORT_SYMBOL_GPL(ata_exec_command_pio);
3518 EXPORT_SYMBOL_GPL(ata_exec_command_mmio);
3519 EXPORT_SYMBOL_GPL(ata_port_start);
3520 EXPORT_SYMBOL_GPL(ata_port_stop);
3521 EXPORT_SYMBOL_GPL(ata_interrupt);
3522 EXPORT_SYMBOL_GPL(ata_fill_sg);
3523 EXPORT_SYMBOL_GPL(ata_bmdma_start_pio);
3524 EXPORT_SYMBOL_GPL(ata_bmdma_start_mmio);
3525 EXPORT_SYMBOL_GPL(ata_port_probe);
3526 EXPORT_SYMBOL_GPL(sata_phy_reset);
3527 EXPORT_SYMBOL_GPL(ata_bus_reset);
3528 EXPORT_SYMBOL_GPL(ata_port_disable);
3529 EXPORT_SYMBOL_GPL(ata_pci_init_one);
3530 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
3531 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
3532 EXPORT_SYMBOL_GPL(ata_scsi_error);
3533 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
3534 EXPORT_SYMBOL_GPL(ata_scsi_release);
3535 EXPORT_SYMBOL_GPL(ata_host_intr);