2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(const struct ata_port *ap,
72 unsigned int *xfer_shift_out);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 int atapi_enabled = 0;
78 module_param(atapi_enabled, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81 MODULE_AUTHOR("Jeff Garzik");
82 MODULE_DESCRIPTION("Library module for ATA devices");
83 MODULE_LICENSE("GPL");
84 MODULE_VERSION(DRV_VERSION);
88 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
97 * Inherited from caller.
100 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
119 fis[13] = tf->hob_nsect;
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
137 * Inherited from caller.
140 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
155 tf->hob_nsect = fis[13];
158 static const u8 ata_rw_cmds[] = {
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
185 ATA_CMD_WRITE_FUA_EXT
189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
198 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
204 int index, fua, lba48, write;
206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
212 index = dev->multi_count ? 0 : 8;
213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 8;
218 tf->protocol = ATA_PROT_DMA;
222 cmd = ata_rw_cmds[index + fua + lba48 + write];
230 static const char * const xfer_mode_str[] = {
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
264 static const char *ata_mode_string(unsigned int mask)
268 for (i = 7; i >= 0; i--)
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
281 return xfer_mode_str[i];
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
302 static unsigned int ata_pio_devchk(struct ata_port *ap,
305 struct ata_ioports *ioaddr = &ap->ioaddr;
308 ap->ops->dev_select(ap, device);
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
325 return 0; /* nothing found */
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
346 static unsigned int ata_mmio_devchk(struct ata_port *ap,
349 struct ata_ioports *ioaddr = &ap->ioaddr;
352 ap->ops->dev_select(ap, device);
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
369 return 0; /* nothing found */
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
385 static unsigned int ata_devchk(struct ata_port *ap,
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
409 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
436 * @r_err: Value of error register on completion
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
455 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
457 struct ata_taskfile tf;
461 ap->ops->dev_select(ap, device);
463 memset(&tf, 0, sizeof(tf));
465 ap->ops->tf_read(ap, &tf);
470 /* see if device passed diags */
473 else if ((device == 0) && (err == 0x81))
478 /* determine if device is ATA or ATAPI */
479 class = ata_dev_classify(&tf);
481 if (class == ATA_DEV_UNKNOWN)
483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
489 * ata_id_string - Convert IDENTIFY DEVICE page into string
490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
503 void ata_id_string(const u16 *id, unsigned char *s,
504 unsigned int ofs, unsigned int len)
523 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
524 * @id: IDENTIFY DEVICE results we will examine
525 * @s: string into which data is output
526 * @ofs: offset into identify device page
527 * @len: length of string to return. must be an odd number.
529 * This function is identical to ata_id_string except that it
530 * trims trailing spaces and terminates the resulting string with
531 * null. @len must be actual maximum length (even number) + 1.
536 void ata_id_c_string(const u16 *id, unsigned char *s,
537 unsigned int ofs, unsigned int len)
543 ata_id_string(id, s, ofs, len - 1);
545 p = s + strnlen(s, len - 1);
546 while (p > s && p[-1] == ' ')
551 static u64 ata_id_n_sectors(const u16 *id)
553 if (ata_id_has_lba(id)) {
554 if (ata_id_has_lba48(id))
555 return ata_id_u64(id, 100);
557 return ata_id_u32(id, 60);
559 if (ata_id_current_chs_valid(id))
560 return ata_id_u32(id, 57);
562 return id[1] * id[3] * id[6];
567 * ata_noop_dev_select - Select device 0/1 on ATA bus
568 * @ap: ATA channel to manipulate
569 * @device: ATA device (numbered from zero) to select
571 * This function performs no actual function.
573 * May be used as the dev_select() entry in ata_port_operations.
578 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
584 * ata_std_dev_select - Select device 0/1 on ATA bus
585 * @ap: ATA channel to manipulate
586 * @device: ATA device (numbered from zero) to select
588 * Use the method defined in the ATA specification to
589 * make either device 0, or device 1, active on the
590 * ATA channel. Works with both PIO and MMIO.
592 * May be used as the dev_select() entry in ata_port_operations.
598 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
603 tmp = ATA_DEVICE_OBS;
605 tmp = ATA_DEVICE_OBS | ATA_DEV1;
607 if (ap->flags & ATA_FLAG_MMIO) {
608 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
610 outb(tmp, ap->ioaddr.device_addr);
612 ata_pause(ap); /* needed; also flushes, for mmio */
616 * ata_dev_select - Select device 0/1 on ATA bus
617 * @ap: ATA channel to manipulate
618 * @device: ATA device (numbered from zero) to select
619 * @wait: non-zero to wait for Status register BSY bit to clear
620 * @can_sleep: non-zero if context allows sleeping
622 * Use the method defined in the ATA specification to
623 * make either device 0, or device 1, active on the
626 * This is a high-level version of ata_std_dev_select(),
627 * which additionally provides the services of inserting
628 * the proper pauses and status polling, where needed.
634 void ata_dev_select(struct ata_port *ap, unsigned int device,
635 unsigned int wait, unsigned int can_sleep)
637 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
638 ap->id, device, wait);
643 ap->ops->dev_select(ap, device);
646 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
653 * ata_dump_id - IDENTIFY DEVICE info debugging output
654 * @id: IDENTIFY DEVICE page to dump
656 * Dump selected 16-bit words from the given IDENTIFY DEVICE
663 static inline void ata_dump_id(const u16 *id)
665 DPRINTK("49==0x%04x "
675 DPRINTK("80==0x%04x "
685 DPRINTK("88==0x%04x "
692 * Compute the PIO modes available for this device. This is not as
693 * trivial as it seems if we must consider early devices correctly.
695 * FIXME: pre IDE drive timing (do we care ?).
698 static unsigned int ata_pio_modes(const struct ata_device *adev)
702 /* Usual case. Word 53 indicates word 64 is valid */
703 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
704 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
710 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
711 number for the maximum. Turn it into a mask and return it */
712 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
714 /* But wait.. there's more. Design your standards by committee and
715 you too can get a free iordy field to process. However its the
716 speeds not the modes that are supported... Note drivers using the
717 timing API will get this right anyway */
721 ata_queue_packet_task(struct ata_port *ap)
723 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
724 queue_work(ata_wq, &ap->packet_task);
728 ata_queue_pio_task(struct ata_port *ap)
730 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
731 queue_work(ata_wq, &ap->pio_task);
735 ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
737 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
738 queue_delayed_work(ata_wq, &ap->pio_task, delay);
742 * ata_flush_pio_tasks - Flush pio_task and packet_task
743 * @ap: the target ata_port
745 * After this function completes, pio_task and packet_task are
746 * guranteed not to be running or scheduled.
749 * Kernel thread context (may sleep)
752 static void ata_flush_pio_tasks(struct ata_port *ap)
759 spin_lock_irqsave(&ap->host_set->lock, flags);
760 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
761 spin_unlock_irqrestore(&ap->host_set->lock, flags);
763 DPRINTK("flush #1\n");
764 flush_workqueue(ata_wq);
767 * At this point, if a task is running, it's guaranteed to see
768 * the FLUSH flag; thus, it will never queue pio tasks again.
771 tmp |= cancel_delayed_work(&ap->pio_task);
772 tmp |= cancel_delayed_work(&ap->packet_task);
774 DPRINTK("flush #2\n");
775 flush_workqueue(ata_wq);
778 spin_lock_irqsave(&ap->host_set->lock, flags);
779 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
780 spin_unlock_irqrestore(&ap->host_set->lock, flags);
785 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
787 struct completion *waiting = qc->private_data;
789 qc->ap->ops->tf_read(qc->ap, &qc->tf);
794 * ata_exec_internal - execute libata internal command
795 * @ap: Port to which the command is sent
796 * @dev: Device to which the command is sent
797 * @tf: Taskfile registers for the command and the result
798 * @dma_dir: Data tranfer direction of the command
799 * @buf: Data buffer of the command
800 * @buflen: Length of data buffer
802 * Executes libata internal command with timeout. @tf contains
803 * command on entry and result on return. Timeout and error
804 * conditions are reported via return value. No recovery action
805 * is taken after a command times out. It's caller's duty to
806 * clean up after timeout.
809 * None. Should be called with kernel context, might sleep.
813 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
814 struct ata_taskfile *tf,
815 int dma_dir, void *buf, unsigned int buflen)
817 u8 command = tf->command;
818 struct ata_queued_cmd *qc;
819 DECLARE_COMPLETION(wait);
821 unsigned int err_mask;
823 spin_lock_irqsave(&ap->host_set->lock, flags);
825 qc = ata_qc_new_init(ap, dev);
829 qc->dma_dir = dma_dir;
830 if (dma_dir != DMA_NONE) {
831 ata_sg_init_one(qc, buf, buflen);
832 qc->nsect = buflen / ATA_SECT_SIZE;
835 qc->private_data = &wait;
836 qc->complete_fn = ata_qc_complete_internal;
838 qc->err_mask = ata_qc_issue(qc);
842 spin_unlock_irqrestore(&ap->host_set->lock, flags);
844 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
845 spin_lock_irqsave(&ap->host_set->lock, flags);
847 /* We're racing with irq here. If we lose, the
848 * following test prevents us from completing the qc
849 * again. If completion irq occurs after here but
850 * before the caller cleans up, it will result in a
851 * spurious interrupt. We can live with that.
853 if (qc->flags & ATA_QCFLAG_ACTIVE) {
854 qc->err_mask = AC_ERR_TIMEOUT;
856 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
860 spin_unlock_irqrestore(&ap->host_set->lock, flags);
864 err_mask = qc->err_mask;
872 * ata_pio_need_iordy - check if iordy needed
875 * Check if the current speed of the device requires IORDY. Used
876 * by various controllers for chip configuration.
879 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
882 int speed = adev->pio_mode - XFER_PIO_0;
889 /* If we have no drive specific rule, then PIO 2 is non IORDY */
891 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
892 pio = adev->id[ATA_ID_EIDE_PIO];
893 /* Is the speed faster than the drive allows non IORDY ? */
895 /* This is cycle times not frequency - watch the logic! */
896 if (pio > 240) /* PIO2 is 240nS per cycle */
905 * ata_dev_read_id - Read ID data from the specified device
906 * @ap: port on which target device resides
907 * @dev: target device
908 * @p_class: pointer to class of the target device (may be changed)
909 * @post_reset: is this read ID post-reset?
910 * @id: buffer to fill IDENTIFY page into
912 * Read ID data from the specified device. ATA_CMD_ID_ATA is
913 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
914 * devices. This function also takes care of EDD signature
915 * misreporting (to be removed once EDD support is gone) and
916 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
919 * Kernel thread context (may sleep)
922 * 0 on success, -errno otherwise.
924 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
925 unsigned int *p_class, int post_reset, u16 *id)
927 unsigned int class = *p_class;
928 unsigned int using_edd;
929 struct ata_taskfile tf;
930 unsigned int err_mask = 0;
934 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
936 if (ap->ops->probe_reset ||
937 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
942 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
945 ata_tf_init(ap, &tf, dev->devno);
949 tf.command = ATA_CMD_ID_ATA;
952 tf.command = ATA_CMD_ID_ATAPI;
956 reason = "unsupported class";
960 tf.protocol = ATA_PROT_PIO;
962 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
963 id, sizeof(id[0]) * ATA_ID_WORDS);
967 reason = "I/O error";
969 if (err_mask & ~AC_ERR_DEV)
973 * arg! EDD works for all test cases, but seems to return
974 * the ATA signature for some ATAPI devices. Until the
975 * reason for this is found and fixed, we fix up the mess
976 * here. If IDENTIFY DEVICE returns command aborted
977 * (as ATAPI devices do), then we issue an
978 * IDENTIFY PACKET DEVICE.
980 * ATA software reset (SRST, the default) does not appear
981 * to have this problem.
983 if ((using_edd) && (class == ATA_DEV_ATA)) {
985 if (err & ATA_ABORTED) {
986 class = ATA_DEV_ATAPI;
993 swap_buf_le16(id, ATA_ID_WORDS);
995 /* print device capabilities */
996 printk(KERN_DEBUG "ata%u: dev %u cfg "
997 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
999 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1002 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1004 reason = "device reports illegal type";
1008 if (post_reset && class == ATA_DEV_ATA) {
1010 * The exact sequence expected by certain pre-ATA4 drives is:
1013 * INITIALIZE DEVICE PARAMETERS
1015 * Some drives were very specific about that exact sequence.
1017 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1018 err_mask = ata_dev_init_params(ap, dev);
1021 reason = "INIT_DEV_PARAMS failed";
1025 /* current CHS translation info (id[53-58]) might be
1026 * changed. reread the identify device info.
1037 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1038 ap->id, dev->devno, reason);
1044 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1045 * @ap: port on which device we wish to probe resides
1046 * @device: device bus address, starting at zero
1048 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1049 * command, and read back the 512-byte device information page.
1050 * The device information page is fed to us via the standard
1051 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1052 * using standard PIO-IN paths)
1054 * After reading the device information page, we use several
1055 * bits of information from it to initialize data structures
1056 * that will be used during the lifetime of the ata_device.
1057 * Other data from the info page is used to disqualify certain
1058 * older ATA devices we do not wish to support.
1061 * Inherited from caller. Some functions called by this function
1062 * obtain the host_set lock.
1065 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1067 struct ata_device *dev = &ap->device[device];
1068 unsigned long xfer_modes;
1071 if (!ata_dev_present(dev)) {
1072 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1077 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1079 rc = ata_dev_read_id(ap, dev, &dev->class, 1, dev->id);
1084 * common ATA, ATAPI feature tests
1087 /* we require DMA support (bits 8 of word 49) */
1088 if (!ata_id_has_dma(dev->id)) {
1089 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1093 /* quick-n-dirty find max transfer mode; for printk only */
1094 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1096 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1098 xfer_modes = ata_pio_modes(dev);
1100 ata_dump_id(dev->id);
1102 /* ATA-specific feature tests */
1103 if (dev->class == ATA_DEV_ATA) {
1104 dev->n_sectors = ata_id_n_sectors(dev->id);
1106 if (ata_id_has_lba(dev->id)) {
1107 dev->flags |= ATA_DFLAG_LBA;
1109 if (ata_id_has_lba48(dev->id))
1110 dev->flags |= ATA_DFLAG_LBA48;
1112 /* print device info to dmesg */
1113 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1115 ata_id_major_version(dev->id),
1116 ata_mode_string(xfer_modes),
1117 (unsigned long long)dev->n_sectors,
1118 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1122 /* Default translation */
1123 dev->cylinders = dev->id[1];
1124 dev->heads = dev->id[3];
1125 dev->sectors = dev->id[6];
1127 if (ata_id_current_chs_valid(dev->id)) {
1128 /* Current CHS translation is valid. */
1129 dev->cylinders = dev->id[54];
1130 dev->heads = dev->id[55];
1131 dev->sectors = dev->id[56];
1134 /* print device info to dmesg */
1135 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1137 ata_id_major_version(dev->id),
1138 ata_mode_string(xfer_modes),
1139 (unsigned long long)dev->n_sectors,
1140 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1147 /* ATAPI-specific feature tests */
1148 else if (dev->class == ATA_DEV_ATAPI) {
1149 rc = atapi_cdb_len(dev->id);
1150 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1151 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1154 dev->cdb_len = (unsigned int) rc;
1156 /* print device info to dmesg */
1157 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1159 ata_mode_string(xfer_modes));
1162 ap->host->max_cmd_len = 0;
1163 for (i = 0; i < ATA_MAX_DEVICES; i++)
1164 ap->host->max_cmd_len = max_t(unsigned int,
1165 ap->host->max_cmd_len,
1166 ap->device[i].cdb_len);
1168 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1172 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1175 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1176 DPRINTK("EXIT, err\n");
1180 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1181 struct ata_device *dev)
1183 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1187 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1194 void ata_dev_config(struct ata_port *ap, unsigned int i)
1196 /* limit bridge transfers to udma5, 200 sectors */
1197 if (ata_dev_knobble(ap, &ap->device[i])) {
1198 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1200 ap->udma_mask &= ATA_UDMA5;
1201 ap->device[i].max_sectors = ATA_MAX_SECTORS;
1204 if (ap->ops->dev_config)
1205 ap->ops->dev_config(ap, &ap->device[i]);
1209 * ata_bus_probe - Reset and probe ATA bus
1212 * Master ATA bus probing function. Initiates a hardware-dependent
1213 * bus reset, then attempts to identify any devices found on
1217 * PCI/etc. bus probe sem.
1220 * Zero on success, non-zero on error.
1223 static int ata_bus_probe(struct ata_port *ap)
1225 unsigned int i, found = 0;
1227 if (ap->ops->probe_reset) {
1228 unsigned int classes[ATA_MAX_DEVICES];
1233 rc = ap->ops->probe_reset(ap, classes);
1235 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1236 if (classes[i] == ATA_DEV_UNKNOWN)
1237 classes[i] = ATA_DEV_NONE;
1238 ap->device[i].class = classes[i];
1241 printk(KERN_ERR "ata%u: probe reset failed, "
1242 "disabling port\n", ap->id);
1243 ata_port_disable(ap);
1246 ap->ops->phy_reset(ap);
1248 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1251 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1252 ata_dev_identify(ap, i);
1253 if (ata_dev_present(&ap->device[i])) {
1255 ata_dev_config(ap,i);
1259 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1260 goto err_out_disable;
1263 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1264 goto err_out_disable;
1269 ap->ops->port_disable(ap);
1275 * ata_port_probe - Mark port as enabled
1276 * @ap: Port for which we indicate enablement
1278 * Modify @ap data structure such that the system
1279 * thinks that the entire port is enabled.
1281 * LOCKING: host_set lock, or some other form of
1285 void ata_port_probe(struct ata_port *ap)
1287 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1291 * sata_print_link_status - Print SATA link status
1292 * @ap: SATA port to printk link status about
1294 * This function prints link speed and status of a SATA link.
1299 static void sata_print_link_status(struct ata_port *ap)
1304 if (!ap->ops->scr_read)
1307 sstatus = scr_read(ap, SCR_STATUS);
1309 if (sata_dev_present(ap)) {
1310 tmp = (sstatus >> 4) & 0xf;
1313 else if (tmp & (1 << 1))
1316 speed = "<unknown>";
1317 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1318 ap->id, speed, sstatus);
1320 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1326 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1327 * @ap: SATA port associated with target SATA PHY.
1329 * This function issues commands to standard SATA Sxxx
1330 * PHY registers, to wake up the phy (and device), and
1331 * clear any reset condition.
1334 * PCI/etc. bus probe sem.
1337 void __sata_phy_reset(struct ata_port *ap)
1340 unsigned long timeout = jiffies + (HZ * 5);
1342 if (ap->flags & ATA_FLAG_SATA_RESET) {
1343 /* issue phy wake/reset */
1344 scr_write_flush(ap, SCR_CONTROL, 0x301);
1345 /* Couldn't find anything in SATA I/II specs, but
1346 * AHCI-1.1 10.4.2 says at least 1 ms. */
1349 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1351 /* wait for phy to become ready, if necessary */
1354 sstatus = scr_read(ap, SCR_STATUS);
1355 if ((sstatus & 0xf) != 1)
1357 } while (time_before(jiffies, timeout));
1359 /* print link status */
1360 sata_print_link_status(ap);
1362 /* TODO: phy layer with polling, timeouts, etc. */
1363 if (sata_dev_present(ap))
1366 ata_port_disable(ap);
1368 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1371 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1372 ata_port_disable(ap);
1376 ap->cbl = ATA_CBL_SATA;
1380 * sata_phy_reset - Reset SATA bus.
1381 * @ap: SATA port associated with target SATA PHY.
1383 * This function resets the SATA bus, and then probes
1384 * the bus for devices.
1387 * PCI/etc. bus probe sem.
1390 void sata_phy_reset(struct ata_port *ap)
1392 __sata_phy_reset(ap);
1393 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1399 * ata_port_disable - Disable port.
1400 * @ap: Port to be disabled.
1402 * Modify @ap data structure such that the system
1403 * thinks that the entire port is disabled, and should
1404 * never attempt to probe or communicate with devices
1407 * LOCKING: host_set lock, or some other form of
1411 void ata_port_disable(struct ata_port *ap)
1413 ap->device[0].class = ATA_DEV_NONE;
1414 ap->device[1].class = ATA_DEV_NONE;
1415 ap->flags |= ATA_FLAG_PORT_DISABLED;
1419 * This mode timing computation functionality is ported over from
1420 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1423 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1424 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1425 * for PIO 5, which is a nonstandard extension and UDMA6, which
1426 * is currently supported only by Maxtor drives.
1429 static const struct ata_timing ata_timing[] = {
1431 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1432 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1433 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1434 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1436 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1437 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1438 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1440 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1442 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1443 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1444 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1446 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1447 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1448 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1450 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1451 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1452 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1454 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1455 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1456 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1458 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1463 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1464 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1466 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1468 q->setup = EZ(t->setup * 1000, T);
1469 q->act8b = EZ(t->act8b * 1000, T);
1470 q->rec8b = EZ(t->rec8b * 1000, T);
1471 q->cyc8b = EZ(t->cyc8b * 1000, T);
1472 q->active = EZ(t->active * 1000, T);
1473 q->recover = EZ(t->recover * 1000, T);
1474 q->cycle = EZ(t->cycle * 1000, T);
1475 q->udma = EZ(t->udma * 1000, UT);
1478 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1479 struct ata_timing *m, unsigned int what)
1481 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1482 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1483 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1484 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1485 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1486 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1487 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1488 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1491 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1493 const struct ata_timing *t;
1495 for (t = ata_timing; t->mode != speed; t++)
1496 if (t->mode == 0xFF)
1501 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1502 struct ata_timing *t, int T, int UT)
1504 const struct ata_timing *s;
1505 struct ata_timing p;
1511 if (!(s = ata_timing_find_mode(speed)))
1514 memcpy(t, s, sizeof(*s));
1517 * If the drive is an EIDE drive, it can tell us it needs extended
1518 * PIO/MW_DMA cycle timing.
1521 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1522 memset(&p, 0, sizeof(p));
1523 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1524 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1525 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1526 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1527 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1529 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1533 * Convert the timing to bus clock counts.
1536 ata_timing_quantize(t, t, T, UT);
1539 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1540 * S.M.A.R.T * and some other commands. We have to ensure that the
1541 * DMA cycle timing is slower/equal than the fastest PIO timing.
1544 if (speed > XFER_PIO_4) {
1545 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1546 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1550 * Lengthen active & recovery time so that cycle time is correct.
1553 if (t->act8b + t->rec8b < t->cyc8b) {
1554 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1555 t->rec8b = t->cyc8b - t->act8b;
1558 if (t->active + t->recover < t->cycle) {
1559 t->active += (t->cycle - (t->active + t->recover)) / 2;
1560 t->recover = t->cycle - t->active;
1566 static const struct {
1569 } xfer_mode_classes[] = {
1570 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1571 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1572 { ATA_SHIFT_PIO, XFER_PIO_0 },
1575 static u8 base_from_shift(unsigned int shift)
1579 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1580 if (xfer_mode_classes[i].shift == shift)
1581 return xfer_mode_classes[i].base;
1586 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1591 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1594 if (dev->xfer_shift == ATA_SHIFT_PIO)
1595 dev->flags |= ATA_DFLAG_PIO;
1597 ata_dev_set_xfermode(ap, dev);
1599 base = base_from_shift(dev->xfer_shift);
1600 ofs = dev->xfer_mode - base;
1601 idx = ofs + dev->xfer_shift;
1602 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1604 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1605 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1607 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1608 ap->id, dev->devno, xfer_mode_str[idx]);
1611 static int ata_host_set_pio(struct ata_port *ap)
1617 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1620 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1624 base = base_from_shift(ATA_SHIFT_PIO);
1625 xfer_mode = base + x;
1627 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1628 (int)base, (int)xfer_mode, mask, x);
1630 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1631 struct ata_device *dev = &ap->device[i];
1632 if (ata_dev_present(dev)) {
1633 dev->pio_mode = xfer_mode;
1634 dev->xfer_mode = xfer_mode;
1635 dev->xfer_shift = ATA_SHIFT_PIO;
1636 if (ap->ops->set_piomode)
1637 ap->ops->set_piomode(ap, dev);
1644 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1645 unsigned int xfer_shift)
1649 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1650 struct ata_device *dev = &ap->device[i];
1651 if (ata_dev_present(dev)) {
1652 dev->dma_mode = xfer_mode;
1653 dev->xfer_mode = xfer_mode;
1654 dev->xfer_shift = xfer_shift;
1655 if (ap->ops->set_dmamode)
1656 ap->ops->set_dmamode(ap, dev);
1662 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1663 * @ap: port on which timings will be programmed
1665 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1668 * PCI/etc. bus probe sem.
1670 static void ata_set_mode(struct ata_port *ap)
1672 unsigned int xfer_shift;
1676 /* step 1: always set host PIO timings */
1677 rc = ata_host_set_pio(ap);
1681 /* step 2: choose the best data xfer mode */
1682 xfer_mode = xfer_shift = 0;
1683 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1687 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1688 if (xfer_shift != ATA_SHIFT_PIO)
1689 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1691 /* step 4: update devices' xfer mode */
1692 ata_dev_set_mode(ap, &ap->device[0]);
1693 ata_dev_set_mode(ap, &ap->device[1]);
1695 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1698 if (ap->ops->post_set_mode)
1699 ap->ops->post_set_mode(ap);
1704 ata_port_disable(ap);
1708 * ata_tf_to_host - issue ATA taskfile to host controller
1709 * @ap: port to which command is being issued
1710 * @tf: ATA taskfile register set
1712 * Issues ATA taskfile register set to ATA host controller,
1713 * with proper synchronization with interrupt handler and
1717 * spin_lock_irqsave(host_set lock)
1720 static inline void ata_tf_to_host(struct ata_port *ap,
1721 const struct ata_taskfile *tf)
1723 ap->ops->tf_load(ap, tf);
1724 ap->ops->exec_command(ap, tf);
1728 * ata_busy_sleep - sleep until BSY clears, or timeout
1729 * @ap: port containing status register to be polled
1730 * @tmout_pat: impatience timeout
1731 * @tmout: overall timeout
1733 * Sleep until ATA Status register bit BSY clears,
1734 * or a timeout occurs.
1739 unsigned int ata_busy_sleep (struct ata_port *ap,
1740 unsigned long tmout_pat, unsigned long tmout)
1742 unsigned long timer_start, timeout;
1745 status = ata_busy_wait(ap, ATA_BUSY, 300);
1746 timer_start = jiffies;
1747 timeout = timer_start + tmout_pat;
1748 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1750 status = ata_busy_wait(ap, ATA_BUSY, 3);
1753 if (status & ATA_BUSY)
1754 printk(KERN_WARNING "ata%u is slow to respond, "
1755 "please be patient\n", ap->id);
1757 timeout = timer_start + tmout;
1758 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1760 status = ata_chk_status(ap);
1763 if (status & ATA_BUSY) {
1764 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1765 ap->id, tmout / HZ);
1772 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1774 struct ata_ioports *ioaddr = &ap->ioaddr;
1775 unsigned int dev0 = devmask & (1 << 0);
1776 unsigned int dev1 = devmask & (1 << 1);
1777 unsigned long timeout;
1779 /* if device 0 was found in ata_devchk, wait for its
1783 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1785 /* if device 1 was found in ata_devchk, wait for
1786 * register access, then wait for BSY to clear
1788 timeout = jiffies + ATA_TMOUT_BOOT;
1792 ap->ops->dev_select(ap, 1);
1793 if (ap->flags & ATA_FLAG_MMIO) {
1794 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1795 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1797 nsect = inb(ioaddr->nsect_addr);
1798 lbal = inb(ioaddr->lbal_addr);
1800 if ((nsect == 1) && (lbal == 1))
1802 if (time_after(jiffies, timeout)) {
1806 msleep(50); /* give drive a breather */
1809 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1811 /* is all this really necessary? */
1812 ap->ops->dev_select(ap, 0);
1814 ap->ops->dev_select(ap, 1);
1816 ap->ops->dev_select(ap, 0);
1820 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1821 * @ap: Port to reset and probe
1823 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1824 * probe the bus. Not often used these days.
1827 * PCI/etc. bus probe sem.
1828 * Obtains host_set lock.
1832 static unsigned int ata_bus_edd(struct ata_port *ap)
1834 struct ata_taskfile tf;
1835 unsigned long flags;
1837 /* set up execute-device-diag (bus reset) taskfile */
1838 /* also, take interrupts to a known state (disabled) */
1839 DPRINTK("execute-device-diag\n");
1840 ata_tf_init(ap, &tf, 0);
1842 tf.command = ATA_CMD_EDD;
1843 tf.protocol = ATA_PROT_NODATA;
1846 spin_lock_irqsave(&ap->host_set->lock, flags);
1847 ata_tf_to_host(ap, &tf);
1848 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1850 /* spec says at least 2ms. but who knows with those
1851 * crazy ATAPI devices...
1855 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1858 static unsigned int ata_bus_softreset(struct ata_port *ap,
1859 unsigned int devmask)
1861 struct ata_ioports *ioaddr = &ap->ioaddr;
1863 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1865 /* software reset. causes dev0 to be selected */
1866 if (ap->flags & ATA_FLAG_MMIO) {
1867 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1868 udelay(20); /* FIXME: flush */
1869 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1870 udelay(20); /* FIXME: flush */
1871 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 outb(ap->ctl, ioaddr->ctl_addr);
1875 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1877 outb(ap->ctl, ioaddr->ctl_addr);
1880 /* spec mandates ">= 2ms" before checking status.
1881 * We wait 150ms, because that was the magic delay used for
1882 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1883 * between when the ATA command register is written, and then
1884 * status is checked. Because waiting for "a while" before
1885 * checking status is fine, post SRST, we perform this magic
1886 * delay here as well.
1890 ata_bus_post_reset(ap, devmask);
1896 * ata_bus_reset - reset host port and associated ATA channel
1897 * @ap: port to reset
1899 * This is typically the first time we actually start issuing
1900 * commands to the ATA channel. We wait for BSY to clear, then
1901 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1902 * result. Determine what devices, if any, are on the channel
1903 * by looking at the device 0/1 error register. Look at the signature
1904 * stored in each device's taskfile registers, to determine if
1905 * the device is ATA or ATAPI.
1908 * PCI/etc. bus probe sem.
1909 * Obtains host_set lock.
1912 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1915 void ata_bus_reset(struct ata_port *ap)
1917 struct ata_ioports *ioaddr = &ap->ioaddr;
1918 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1920 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1922 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1924 /* determine if device 0/1 are present */
1925 if (ap->flags & ATA_FLAG_SATA_RESET)
1928 dev0 = ata_devchk(ap, 0);
1930 dev1 = ata_devchk(ap, 1);
1934 devmask |= (1 << 0);
1936 devmask |= (1 << 1);
1938 /* select device 0 again */
1939 ap->ops->dev_select(ap, 0);
1941 /* issue bus reset */
1942 if (ap->flags & ATA_FLAG_SRST)
1943 rc = ata_bus_softreset(ap, devmask);
1944 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1945 /* set up device control */
1946 if (ap->flags & ATA_FLAG_MMIO)
1947 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1949 outb(ap->ctl, ioaddr->ctl_addr);
1950 rc = ata_bus_edd(ap);
1957 * determine by signature whether we have ATA or ATAPI devices
1959 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1960 if ((slave_possible) && (err != 0x81))
1961 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1963 /* re-enable interrupts */
1964 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1967 /* is double-select really necessary? */
1968 if (ap->device[1].class != ATA_DEV_NONE)
1969 ap->ops->dev_select(ap, 1);
1970 if (ap->device[0].class != ATA_DEV_NONE)
1971 ap->ops->dev_select(ap, 0);
1973 /* if no devices were detected, disable this port */
1974 if ((ap->device[0].class == ATA_DEV_NONE) &&
1975 (ap->device[1].class == ATA_DEV_NONE))
1978 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1979 /* set up device control for ATA_FLAG_SATA_RESET */
1980 if (ap->flags & ATA_FLAG_MMIO)
1981 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1983 outb(ap->ctl, ioaddr->ctl_addr);
1990 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1991 ap->ops->port_disable(ap);
1996 static int sata_phy_resume(struct ata_port *ap)
1998 unsigned long timeout = jiffies + (HZ * 5);
2001 scr_write_flush(ap, SCR_CONTROL, 0x300);
2003 /* Wait for phy to become ready, if necessary. */
2006 sstatus = scr_read(ap, SCR_STATUS);
2007 if ((sstatus & 0xf) != 1)
2009 } while (time_before(jiffies, timeout));
2015 * ata_std_probeinit - initialize probing
2016 * @ap: port to be probed
2018 * @ap is about to be probed. Initialize it. This function is
2019 * to be used as standard callback for ata_drive_probe_reset().
2021 * NOTE!!! Do not use this function as probeinit if a low level
2022 * driver implements only hardreset. Just pass NULL as probeinit
2023 * in that case. Using this function is probably okay but doing
2024 * so makes reset sequence different from the original
2025 * ->phy_reset implementation and Jeff nervous. :-P
2027 extern void ata_std_probeinit(struct ata_port *ap)
2029 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2030 sata_phy_resume(ap);
2031 if (sata_dev_present(ap))
2032 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2037 * ata_std_softreset - reset host port via ATA SRST
2038 * @ap: port to reset
2039 * @verbose: fail verbosely
2040 * @classes: resulting classes of attached devices
2042 * Reset host port using ATA SRST. This function is to be used
2043 * as standard callback for ata_drive_*_reset() functions.
2046 * Kernel thread context (may sleep)
2049 * 0 on success, -errno otherwise.
2051 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2053 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2054 unsigned int devmask = 0, err_mask;
2059 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2060 classes[0] = ATA_DEV_NONE;
2064 /* determine if device 0/1 are present */
2065 if (ata_devchk(ap, 0))
2066 devmask |= (1 << 0);
2067 if (slave_possible && ata_devchk(ap, 1))
2068 devmask |= (1 << 1);
2070 /* select device 0 again */
2071 ap->ops->dev_select(ap, 0);
2073 /* issue bus reset */
2074 DPRINTK("about to softreset, devmask=%x\n", devmask);
2075 err_mask = ata_bus_softreset(ap, devmask);
2078 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2081 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2086 /* determine by signature whether we have ATA or ATAPI devices */
2087 classes[0] = ata_dev_try_classify(ap, 0, &err);
2088 if (slave_possible && err != 0x81)
2089 classes[1] = ata_dev_try_classify(ap, 1, &err);
2092 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2097 * sata_std_hardreset - reset host port via SATA phy reset
2098 * @ap: port to reset
2099 * @verbose: fail verbosely
2100 * @class: resulting class of attached device
2102 * SATA phy-reset host port using DET bits of SControl register.
2103 * This function is to be used as standard callback for
2104 * ata_drive_*_reset().
2107 * Kernel thread context (may sleep)
2110 * 0 on success, -errno otherwise.
2112 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2116 /* Issue phy wake/reset */
2117 scr_write_flush(ap, SCR_CONTROL, 0x301);
2120 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2121 * 10.4.2 says at least 1 ms.
2125 /* Bring phy back */
2126 sata_phy_resume(ap);
2128 /* TODO: phy layer with polling, timeouts, etc. */
2129 if (!sata_dev_present(ap)) {
2130 *class = ATA_DEV_NONE;
2131 DPRINTK("EXIT, link offline\n");
2135 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2137 printk(KERN_ERR "ata%u: COMRESET failed "
2138 "(device not ready)\n", ap->id);
2140 DPRINTK("EXIT, device not ready\n");
2144 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2146 *class = ata_dev_try_classify(ap, 0, NULL);
2148 DPRINTK("EXIT, class=%u\n", *class);
2153 * ata_std_postreset - standard postreset callback
2154 * @ap: the target ata_port
2155 * @classes: classes of attached devices
2157 * This function is invoked after a successful reset. Note that
2158 * the device might have been reset more than once using
2159 * different reset methods before postreset is invoked.
2161 * This function is to be used as standard callback for
2162 * ata_drive_*_reset().
2165 * Kernel thread context (may sleep)
2167 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2171 /* set cable type if it isn't already set */
2172 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2173 ap->cbl = ATA_CBL_SATA;
2175 /* print link status */
2176 if (ap->cbl == ATA_CBL_SATA)
2177 sata_print_link_status(ap);
2179 /* re-enable interrupts */
2180 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2183 /* is double-select really necessary? */
2184 if (classes[0] != ATA_DEV_NONE)
2185 ap->ops->dev_select(ap, 1);
2186 if (classes[1] != ATA_DEV_NONE)
2187 ap->ops->dev_select(ap, 0);
2189 /* bail out if no device is present */
2190 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2191 DPRINTK("EXIT, no device\n");
2195 /* set up device control */
2196 if (ap->ioaddr.ctl_addr) {
2197 if (ap->flags & ATA_FLAG_MMIO)
2198 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2200 outb(ap->ctl, ap->ioaddr.ctl_addr);
2207 * ata_std_probe_reset - standard probe reset method
2208 * @ap: prot to perform probe-reset
2209 * @classes: resulting classes of attached devices
2211 * The stock off-the-shelf ->probe_reset method.
2214 * Kernel thread context (may sleep)
2217 * 0 on success, -errno otherwise.
2219 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2221 ata_reset_fn_t hardreset;
2224 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2225 hardreset = sata_std_hardreset;
2227 return ata_drive_probe_reset(ap, ata_std_probeinit,
2228 ata_std_softreset, hardreset,
2229 ata_std_postreset, classes);
2232 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2233 ata_postreset_fn_t postreset,
2234 unsigned int *classes)
2238 for (i = 0; i < ATA_MAX_DEVICES; i++)
2239 classes[i] = ATA_DEV_UNKNOWN;
2241 rc = reset(ap, 0, classes);
2245 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2246 * is complete and convert all ATA_DEV_UNKNOWN to
2249 for (i = 0; i < ATA_MAX_DEVICES; i++)
2250 if (classes[i] != ATA_DEV_UNKNOWN)
2253 if (i < ATA_MAX_DEVICES)
2254 for (i = 0; i < ATA_MAX_DEVICES; i++)
2255 if (classes[i] == ATA_DEV_UNKNOWN)
2256 classes[i] = ATA_DEV_NONE;
2259 postreset(ap, classes);
2261 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2265 * ata_drive_probe_reset - Perform probe reset with given methods
2266 * @ap: port to reset
2267 * @probeinit: probeinit method (can be NULL)
2268 * @softreset: softreset method (can be NULL)
2269 * @hardreset: hardreset method (can be NULL)
2270 * @postreset: postreset method (can be NULL)
2271 * @classes: resulting classes of attached devices
2273 * Reset the specified port and classify attached devices using
2274 * given methods. This function prefers softreset but tries all
2275 * possible reset sequences to reset and classify devices. This
2276 * function is intended to be used for constructing ->probe_reset
2277 * callback by low level drivers.
2279 * Reset methods should follow the following rules.
2281 * - Return 0 on sucess, -errno on failure.
2282 * - If classification is supported, fill classes[] with
2283 * recognized class codes.
2284 * - If classification is not supported, leave classes[] alone.
2285 * - If verbose is non-zero, print error message on failure;
2286 * otherwise, shut up.
2289 * Kernel thread context (may sleep)
2292 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2293 * if classification fails, and any error code from reset
2296 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2297 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2298 ata_postreset_fn_t postreset, unsigned int *classes)
2306 rc = do_probe_reset(ap, softreset, postreset, classes);
2314 rc = do_probe_reset(ap, hardreset, postreset, classes);
2315 if (rc == 0 || rc != -ENODEV)
2319 rc = do_probe_reset(ap, softreset, postreset, classes);
2324 static void ata_pr_blacklisted(const struct ata_port *ap,
2325 const struct ata_device *dev)
2327 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2328 ap->id, dev->devno);
2331 static const char * const ata_dma_blacklist [] = {
2350 "Toshiba CD-ROM XM-6202B",
2351 "TOSHIBA CD-ROM XM-1702BC",
2353 "E-IDE CD-ROM CR-840",
2356 "SAMSUNG CD-ROM SC-148C",
2357 "SAMSUNG CD-ROM SC",
2359 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2363 static int ata_dma_blacklisted(const struct ata_device *dev)
2365 unsigned char model_num[41];
2368 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2370 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2371 if (!strcmp(ata_dma_blacklist[i], model_num))
2377 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2379 const struct ata_device *master, *slave;
2382 master = &ap->device[0];
2383 slave = &ap->device[1];
2385 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2387 if (shift == ATA_SHIFT_UDMA) {
2388 mask = ap->udma_mask;
2389 if (ata_dev_present(master)) {
2390 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2391 if (ata_dma_blacklisted(master)) {
2393 ata_pr_blacklisted(ap, master);
2396 if (ata_dev_present(slave)) {
2397 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2398 if (ata_dma_blacklisted(slave)) {
2400 ata_pr_blacklisted(ap, slave);
2404 else if (shift == ATA_SHIFT_MWDMA) {
2405 mask = ap->mwdma_mask;
2406 if (ata_dev_present(master)) {
2407 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2408 if (ata_dma_blacklisted(master)) {
2410 ata_pr_blacklisted(ap, master);
2413 if (ata_dev_present(slave)) {
2414 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2415 if (ata_dma_blacklisted(slave)) {
2417 ata_pr_blacklisted(ap, slave);
2421 else if (shift == ATA_SHIFT_PIO) {
2422 mask = ap->pio_mask;
2423 if (ata_dev_present(master)) {
2424 /* spec doesn't return explicit support for
2425 * PIO0-2, so we fake it
2427 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2432 if (ata_dev_present(slave)) {
2433 /* spec doesn't return explicit support for
2434 * PIO0-2, so we fake it
2436 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2443 mask = 0xffffffff; /* shut up compiler warning */
2450 /* find greatest bit */
2451 static int fgb(u32 bitmap)
2456 for (i = 0; i < 32; i++)
2457 if (bitmap & (1 << i))
2464 * ata_choose_xfer_mode - attempt to find best transfer mode
2465 * @ap: Port for which an xfer mode will be selected
2466 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2467 * @xfer_shift_out: (output) bit shift that selects this mode
2469 * Based on host and device capabilities, determine the
2470 * maximum transfer mode that is amenable to all.
2473 * PCI/etc. bus probe sem.
2476 * Zero on success, negative on error.
2479 static int ata_choose_xfer_mode(const struct ata_port *ap,
2481 unsigned int *xfer_shift_out)
2483 unsigned int mask, shift;
2486 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2487 shift = xfer_mode_classes[i].shift;
2488 mask = ata_get_mode_mask(ap, shift);
2492 *xfer_mode_out = xfer_mode_classes[i].base + x;
2493 *xfer_shift_out = shift;
2502 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2503 * @ap: Port associated with device @dev
2504 * @dev: Device to which command will be sent
2506 * Issue SET FEATURES - XFER MODE command to device @dev
2510 * PCI/etc. bus probe sem.
2513 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2515 struct ata_taskfile tf;
2517 /* set up set-features taskfile */
2518 DPRINTK("set features - xfer mode\n");
2520 ata_tf_init(ap, &tf, dev->devno);
2521 tf.command = ATA_CMD_SET_FEATURES;
2522 tf.feature = SETFEATURES_XFER;
2523 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2524 tf.protocol = ATA_PROT_NODATA;
2525 tf.nsect = dev->xfer_mode;
2527 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2528 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2530 ata_port_disable(ap);
2537 * ata_dev_init_params - Issue INIT DEV PARAMS command
2538 * @ap: Port associated with device @dev
2539 * @dev: Device to which command will be sent
2542 * Kernel thread context (may sleep)
2545 * 0 on success, AC_ERR_* mask otherwise.
2548 static unsigned int ata_dev_init_params(struct ata_port *ap,
2549 struct ata_device *dev)
2551 struct ata_taskfile tf;
2552 unsigned int err_mask;
2553 u16 sectors = dev->id[6];
2554 u16 heads = dev->id[3];
2556 /* Number of sectors per track 1-255. Number of heads 1-16 */
2557 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2560 /* set up init dev params taskfile */
2561 DPRINTK("init dev params \n");
2563 ata_tf_init(ap, &tf, dev->devno);
2564 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2565 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2566 tf.protocol = ATA_PROT_NODATA;
2568 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2570 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2572 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2577 * ata_sg_clean - Unmap DMA memory associated with command
2578 * @qc: Command containing DMA memory to be released
2580 * Unmap all mapped DMA memory associated with this command.
2583 * spin_lock_irqsave(host_set lock)
2586 static void ata_sg_clean(struct ata_queued_cmd *qc)
2588 struct ata_port *ap = qc->ap;
2589 struct scatterlist *sg = qc->__sg;
2590 int dir = qc->dma_dir;
2591 void *pad_buf = NULL;
2593 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2594 WARN_ON(sg == NULL);
2596 if (qc->flags & ATA_QCFLAG_SINGLE)
2597 WARN_ON(qc->n_elem > 1);
2599 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2601 /* if we padded the buffer out to 32-bit bound, and data
2602 * xfer direction is from-device, we must copy from the
2603 * pad buffer back into the supplied buffer
2605 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2606 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2608 if (qc->flags & ATA_QCFLAG_SG) {
2610 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2611 /* restore last sg */
2612 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2614 struct scatterlist *psg = &qc->pad_sgent;
2615 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2616 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2617 kunmap_atomic(addr, KM_IRQ0);
2621 dma_unmap_single(ap->host_set->dev,
2622 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2625 sg->length += qc->pad_len;
2627 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2628 pad_buf, qc->pad_len);
2631 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2636 * ata_fill_sg - Fill PCI IDE PRD table
2637 * @qc: Metadata associated with taskfile to be transferred
2639 * Fill PCI IDE PRD (scatter-gather) table with segments
2640 * associated with the current disk command.
2643 * spin_lock_irqsave(host_set lock)
2646 static void ata_fill_sg(struct ata_queued_cmd *qc)
2648 struct ata_port *ap = qc->ap;
2649 struct scatterlist *sg;
2652 WARN_ON(qc->__sg == NULL);
2653 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2656 ata_for_each_sg(sg, qc) {
2660 /* determine if physical DMA addr spans 64K boundary.
2661 * Note h/w doesn't support 64-bit, so we unconditionally
2662 * truncate dma_addr_t to u32.
2664 addr = (u32) sg_dma_address(sg);
2665 sg_len = sg_dma_len(sg);
2668 offset = addr & 0xffff;
2670 if ((offset + sg_len) > 0x10000)
2671 len = 0x10000 - offset;
2673 ap->prd[idx].addr = cpu_to_le32(addr);
2674 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2675 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2684 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2687 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2688 * @qc: Metadata associated with taskfile to check
2690 * Allow low-level driver to filter ATA PACKET commands, returning
2691 * a status indicating whether or not it is OK to use DMA for the
2692 * supplied PACKET command.
2695 * spin_lock_irqsave(host_set lock)
2697 * RETURNS: 0 when ATAPI DMA can be used
2700 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2702 struct ata_port *ap = qc->ap;
2703 int rc = 0; /* Assume ATAPI DMA is OK by default */
2705 if (ap->ops->check_atapi_dma)
2706 rc = ap->ops->check_atapi_dma(qc);
2711 * ata_qc_prep - Prepare taskfile for submission
2712 * @qc: Metadata associated with taskfile to be prepared
2714 * Prepare ATA taskfile for submission.
2717 * spin_lock_irqsave(host_set lock)
2719 void ata_qc_prep(struct ata_queued_cmd *qc)
2721 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2728 * ata_sg_init_one - Associate command with memory buffer
2729 * @qc: Command to be associated
2730 * @buf: Memory buffer
2731 * @buflen: Length of memory buffer, in bytes.
2733 * Initialize the data-related elements of queued_cmd @qc
2734 * to point to a single memory buffer, @buf of byte length @buflen.
2737 * spin_lock_irqsave(host_set lock)
2740 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2742 struct scatterlist *sg;
2744 qc->flags |= ATA_QCFLAG_SINGLE;
2746 memset(&qc->sgent, 0, sizeof(qc->sgent));
2747 qc->__sg = &qc->sgent;
2749 qc->orig_n_elem = 1;
2753 sg_init_one(sg, buf, buflen);
2757 * ata_sg_init - Associate command with scatter-gather table.
2758 * @qc: Command to be associated
2759 * @sg: Scatter-gather table.
2760 * @n_elem: Number of elements in s/g table.
2762 * Initialize the data-related elements of queued_cmd @qc
2763 * to point to a scatter-gather table @sg, containing @n_elem
2767 * spin_lock_irqsave(host_set lock)
2770 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2771 unsigned int n_elem)
2773 qc->flags |= ATA_QCFLAG_SG;
2775 qc->n_elem = n_elem;
2776 qc->orig_n_elem = n_elem;
2780 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2781 * @qc: Command with memory buffer to be mapped.
2783 * DMA-map the memory buffer associated with queued_cmd @qc.
2786 * spin_lock_irqsave(host_set lock)
2789 * Zero on success, negative on error.
2792 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2794 struct ata_port *ap = qc->ap;
2795 int dir = qc->dma_dir;
2796 struct scatterlist *sg = qc->__sg;
2797 dma_addr_t dma_address;
2800 /* we must lengthen transfers to end on a 32-bit boundary */
2801 qc->pad_len = sg->length & 3;
2803 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2804 struct scatterlist *psg = &qc->pad_sgent;
2806 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2808 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2810 if (qc->tf.flags & ATA_TFLAG_WRITE)
2811 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2814 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2815 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2817 sg->length -= qc->pad_len;
2818 if (sg->length == 0)
2821 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2822 sg->length, qc->pad_len);
2830 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2832 if (dma_mapping_error(dma_address)) {
2834 sg->length += qc->pad_len;
2838 sg_dma_address(sg) = dma_address;
2839 sg_dma_len(sg) = sg->length;
2842 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2843 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2849 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2850 * @qc: Command with scatter-gather table to be mapped.
2852 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2855 * spin_lock_irqsave(host_set lock)
2858 * Zero on success, negative on error.
2862 static int ata_sg_setup(struct ata_queued_cmd *qc)
2864 struct ata_port *ap = qc->ap;
2865 struct scatterlist *sg = qc->__sg;
2866 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2867 int n_elem, pre_n_elem, dir, trim_sg = 0;
2869 VPRINTK("ENTER, ata%u\n", ap->id);
2870 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2872 /* we must lengthen transfers to end on a 32-bit boundary */
2873 qc->pad_len = lsg->length & 3;
2875 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2876 struct scatterlist *psg = &qc->pad_sgent;
2877 unsigned int offset;
2879 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2881 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2884 * psg->page/offset are used to copy to-be-written
2885 * data in this function or read data in ata_sg_clean.
2887 offset = lsg->offset + lsg->length - qc->pad_len;
2888 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2889 psg->offset = offset_in_page(offset);
2891 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2892 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2893 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2894 kunmap_atomic(addr, KM_IRQ0);
2897 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2898 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2900 lsg->length -= qc->pad_len;
2901 if (lsg->length == 0)
2904 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2905 qc->n_elem - 1, lsg->length, qc->pad_len);
2908 pre_n_elem = qc->n_elem;
2909 if (trim_sg && pre_n_elem)
2918 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2920 /* restore last sg */
2921 lsg->length += qc->pad_len;
2925 DPRINTK("%d sg elements mapped\n", n_elem);
2928 qc->n_elem = n_elem;
2934 * ata_poll_qc_complete - turn irq back on and finish qc
2935 * @qc: Command to complete
2936 * @err_mask: ATA status register content
2939 * None. (grabs host lock)
2942 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2944 struct ata_port *ap = qc->ap;
2945 unsigned long flags;
2947 spin_lock_irqsave(&ap->host_set->lock, flags);
2948 ap->flags &= ~ATA_FLAG_NOINTR;
2950 ata_qc_complete(qc);
2951 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2955 * ata_pio_poll - poll using PIO, depending on current state
2956 * @ap: the target ata_port
2959 * None. (executing in kernel thread context)
2962 * timeout value to use
2965 static unsigned long ata_pio_poll(struct ata_port *ap)
2967 struct ata_queued_cmd *qc;
2969 unsigned int poll_state = HSM_ST_UNKNOWN;
2970 unsigned int reg_state = HSM_ST_UNKNOWN;
2972 qc = ata_qc_from_tag(ap, ap->active_tag);
2973 WARN_ON(qc == NULL);
2975 switch (ap->hsm_task_state) {
2978 poll_state = HSM_ST_POLL;
2982 case HSM_ST_LAST_POLL:
2983 poll_state = HSM_ST_LAST_POLL;
2984 reg_state = HSM_ST_LAST;
2991 status = ata_chk_status(ap);
2992 if (status & ATA_BUSY) {
2993 if (time_after(jiffies, ap->pio_task_timeout)) {
2994 qc->err_mask |= AC_ERR_TIMEOUT;
2995 ap->hsm_task_state = HSM_ST_TMOUT;
2998 ap->hsm_task_state = poll_state;
2999 return ATA_SHORT_PAUSE;
3002 ap->hsm_task_state = reg_state;
3007 * ata_pio_complete - check if drive is busy or idle
3008 * @ap: the target ata_port
3011 * None. (executing in kernel thread context)
3014 * Non-zero if qc completed, zero otherwise.
3017 static int ata_pio_complete (struct ata_port *ap)
3019 struct ata_queued_cmd *qc;
3023 * This is purely heuristic. This is a fast path. Sometimes when
3024 * we enter, BSY will be cleared in a chk-status or two. If not,
3025 * the drive is probably seeking or something. Snooze for a couple
3026 * msecs, then chk-status again. If still busy, fall back to
3027 * HSM_ST_POLL state.
3029 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3030 if (drv_stat & ATA_BUSY) {
3032 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3033 if (drv_stat & ATA_BUSY) {
3034 ap->hsm_task_state = HSM_ST_LAST_POLL;
3035 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3040 qc = ata_qc_from_tag(ap, ap->active_tag);
3041 WARN_ON(qc == NULL);
3043 drv_stat = ata_wait_idle(ap);
3044 if (!ata_ok(drv_stat)) {
3045 qc->err_mask |= __ac_err_mask(drv_stat);
3046 ap->hsm_task_state = HSM_ST_ERR;
3050 ap->hsm_task_state = HSM_ST_IDLE;
3052 WARN_ON(qc->err_mask);
3053 ata_poll_qc_complete(qc);
3055 /* another command may start at this point */
3062 * swap_buf_le16 - swap halves of 16-bit words in place
3063 * @buf: Buffer to swap
3064 * @buf_words: Number of 16-bit words in buffer.
3066 * Swap halves of 16-bit words if needed to convert from
3067 * little-endian byte order to native cpu byte order, or
3071 * Inherited from caller.
3073 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3078 for (i = 0; i < buf_words; i++)
3079 buf[i] = le16_to_cpu(buf[i]);
3080 #endif /* __BIG_ENDIAN */
3084 * ata_mmio_data_xfer - Transfer data by MMIO
3085 * @ap: port to read/write
3087 * @buflen: buffer length
3088 * @write_data: read/write
3090 * Transfer data from/to the device data register by MMIO.
3093 * Inherited from caller.
3096 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3097 unsigned int buflen, int write_data)
3100 unsigned int words = buflen >> 1;
3101 u16 *buf16 = (u16 *) buf;
3102 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3104 /* Transfer multiple of 2 bytes */
3106 for (i = 0; i < words; i++)
3107 writew(le16_to_cpu(buf16[i]), mmio);
3109 for (i = 0; i < words; i++)
3110 buf16[i] = cpu_to_le16(readw(mmio));
3113 /* Transfer trailing 1 byte, if any. */
3114 if (unlikely(buflen & 0x01)) {
3115 u16 align_buf[1] = { 0 };
3116 unsigned char *trailing_buf = buf + buflen - 1;
3119 memcpy(align_buf, trailing_buf, 1);
3120 writew(le16_to_cpu(align_buf[0]), mmio);
3122 align_buf[0] = cpu_to_le16(readw(mmio));
3123 memcpy(trailing_buf, align_buf, 1);
3129 * ata_pio_data_xfer - Transfer data by PIO
3130 * @ap: port to read/write
3132 * @buflen: buffer length
3133 * @write_data: read/write
3135 * Transfer data from/to the device data register by PIO.
3138 * Inherited from caller.
3141 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3142 unsigned int buflen, int write_data)
3144 unsigned int words = buflen >> 1;
3146 /* Transfer multiple of 2 bytes */
3148 outsw(ap->ioaddr.data_addr, buf, words);
3150 insw(ap->ioaddr.data_addr, buf, words);
3152 /* Transfer trailing 1 byte, if any. */
3153 if (unlikely(buflen & 0x01)) {
3154 u16 align_buf[1] = { 0 };
3155 unsigned char *trailing_buf = buf + buflen - 1;
3158 memcpy(align_buf, trailing_buf, 1);
3159 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3161 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3162 memcpy(trailing_buf, align_buf, 1);
3168 * ata_data_xfer - Transfer data from/to the data register.
3169 * @ap: port to read/write
3171 * @buflen: buffer length
3172 * @do_write: read/write
3174 * Transfer data from/to the device data register.
3177 * Inherited from caller.
3180 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3181 unsigned int buflen, int do_write)
3183 /* Make the crap hardware pay the costs not the good stuff */
3184 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3185 unsigned long flags;
3186 local_irq_save(flags);
3187 if (ap->flags & ATA_FLAG_MMIO)
3188 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3190 ata_pio_data_xfer(ap, buf, buflen, do_write);
3191 local_irq_restore(flags);
3193 if (ap->flags & ATA_FLAG_MMIO)
3194 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3196 ata_pio_data_xfer(ap, buf, buflen, do_write);
3201 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3202 * @qc: Command on going
3204 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3207 * Inherited from caller.
3210 static void ata_pio_sector(struct ata_queued_cmd *qc)
3212 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3213 struct scatterlist *sg = qc->__sg;
3214 struct ata_port *ap = qc->ap;
3216 unsigned int offset;
3219 if (qc->cursect == (qc->nsect - 1))
3220 ap->hsm_task_state = HSM_ST_LAST;
3222 page = sg[qc->cursg].page;
3223 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3225 /* get the current page and offset */
3226 page = nth_page(page, (offset >> PAGE_SHIFT));
3227 offset %= PAGE_SIZE;
3229 buf = kmap(page) + offset;
3234 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3239 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3241 /* do the actual data transfer */
3242 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3243 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3249 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3250 * @qc: Command on going
3251 * @bytes: number of bytes
3253 * Transfer Transfer data from/to the ATAPI device.
3256 * Inherited from caller.
3260 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3262 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3263 struct scatterlist *sg = qc->__sg;
3264 struct ata_port *ap = qc->ap;
3267 unsigned int offset, count;
3269 if (qc->curbytes + bytes >= qc->nbytes)
3270 ap->hsm_task_state = HSM_ST_LAST;
3273 if (unlikely(qc->cursg >= qc->n_elem)) {
3275 * The end of qc->sg is reached and the device expects
3276 * more data to transfer. In order not to overrun qc->sg
3277 * and fulfill length specified in the byte count register,
3278 * - for read case, discard trailing data from the device
3279 * - for write case, padding zero data to the device
3281 u16 pad_buf[1] = { 0 };
3282 unsigned int words = bytes >> 1;
3285 if (words) /* warning if bytes > 1 */
3286 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3289 for (i = 0; i < words; i++)
3290 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3292 ap->hsm_task_state = HSM_ST_LAST;
3296 sg = &qc->__sg[qc->cursg];
3299 offset = sg->offset + qc->cursg_ofs;
3301 /* get the current page and offset */
3302 page = nth_page(page, (offset >> PAGE_SHIFT));
3303 offset %= PAGE_SIZE;
3305 /* don't overrun current sg */
3306 count = min(sg->length - qc->cursg_ofs, bytes);
3308 /* don't cross page boundaries */
3309 count = min(count, (unsigned int)PAGE_SIZE - offset);
3311 buf = kmap(page) + offset;
3314 qc->curbytes += count;
3315 qc->cursg_ofs += count;
3317 if (qc->cursg_ofs == sg->length) {
3322 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3324 /* do the actual data transfer */
3325 ata_data_xfer(ap, buf, count, do_write);
3334 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3335 * @qc: Command on going
3337 * Transfer Transfer data from/to the ATAPI device.
3340 * Inherited from caller.
3343 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3345 struct ata_port *ap = qc->ap;
3346 struct ata_device *dev = qc->dev;
3347 unsigned int ireason, bc_lo, bc_hi, bytes;
3348 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3350 ap->ops->tf_read(ap, &qc->tf);
3351 ireason = qc->tf.nsect;
3352 bc_lo = qc->tf.lbam;
3353 bc_hi = qc->tf.lbah;
3354 bytes = (bc_hi << 8) | bc_lo;
3356 /* shall be cleared to zero, indicating xfer of data */
3357 if (ireason & (1 << 0))
3360 /* make sure transfer direction matches expected */
3361 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3362 if (do_write != i_write)
3365 __atapi_pio_bytes(qc, bytes);
3370 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3371 ap->id, dev->devno);
3372 qc->err_mask |= AC_ERR_HSM;
3373 ap->hsm_task_state = HSM_ST_ERR;
3377 * ata_pio_block - start PIO on a block
3378 * @ap: the target ata_port
3381 * None. (executing in kernel thread context)
3384 static void ata_pio_block(struct ata_port *ap)
3386 struct ata_queued_cmd *qc;
3390 * This is purely heuristic. This is a fast path.
3391 * Sometimes when we enter, BSY will be cleared in
3392 * a chk-status or two. If not, the drive is probably seeking
3393 * or something. Snooze for a couple msecs, then
3394 * chk-status again. If still busy, fall back to
3395 * HSM_ST_POLL state.
3397 status = ata_busy_wait(ap, ATA_BUSY, 5);
3398 if (status & ATA_BUSY) {
3400 status = ata_busy_wait(ap, ATA_BUSY, 10);
3401 if (status & ATA_BUSY) {
3402 ap->hsm_task_state = HSM_ST_POLL;
3403 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3408 qc = ata_qc_from_tag(ap, ap->active_tag);
3409 WARN_ON(qc == NULL);
3412 if (status & (ATA_ERR | ATA_DF)) {
3413 qc->err_mask |= AC_ERR_DEV;
3414 ap->hsm_task_state = HSM_ST_ERR;
3418 /* transfer data if any */
3419 if (is_atapi_taskfile(&qc->tf)) {
3420 /* DRQ=0 means no more data to transfer */
3421 if ((status & ATA_DRQ) == 0) {
3422 ap->hsm_task_state = HSM_ST_LAST;
3426 atapi_pio_bytes(qc);
3428 /* handle BSY=0, DRQ=0 as error */
3429 if ((status & ATA_DRQ) == 0) {
3430 qc->err_mask |= AC_ERR_HSM;
3431 ap->hsm_task_state = HSM_ST_ERR;
3439 static void ata_pio_error(struct ata_port *ap)
3441 struct ata_queued_cmd *qc;
3443 qc = ata_qc_from_tag(ap, ap->active_tag);
3444 WARN_ON(qc == NULL);
3446 if (qc->tf.command != ATA_CMD_PACKET)
3447 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3449 /* make sure qc->err_mask is available to
3450 * know what's wrong and recover
3452 WARN_ON(qc->err_mask == 0);
3454 ap->hsm_task_state = HSM_ST_IDLE;
3456 ata_poll_qc_complete(qc);
3459 static void ata_pio_task(void *_data)
3461 struct ata_port *ap = _data;
3462 unsigned long timeout;
3469 switch (ap->hsm_task_state) {
3478 qc_completed = ata_pio_complete(ap);
3482 case HSM_ST_LAST_POLL:
3483 timeout = ata_pio_poll(ap);
3493 ata_queue_delayed_pio_task(ap, timeout);
3494 else if (!qc_completed)
3499 * ata_qc_timeout - Handle timeout of queued command
3500 * @qc: Command that timed out
3502 * Some part of the kernel (currently, only the SCSI layer)
3503 * has noticed that the active command on port @ap has not
3504 * completed after a specified length of time. Handle this
3505 * condition by disabling DMA (if necessary) and completing
3506 * transactions, with error if necessary.
3508 * This also handles the case of the "lost interrupt", where
3509 * for some reason (possibly hardware bug, possibly driver bug)
3510 * an interrupt was not delivered to the driver, even though the
3511 * transaction completed successfully.
3514 * Inherited from SCSI layer (none, can sleep)
3517 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3519 struct ata_port *ap = qc->ap;
3520 struct ata_host_set *host_set = ap->host_set;
3521 u8 host_stat = 0, drv_stat;
3522 unsigned long flags;
3526 ata_flush_pio_tasks(ap);
3527 ap->hsm_task_state = HSM_ST_IDLE;
3529 spin_lock_irqsave(&host_set->lock, flags);
3531 switch (qc->tf.protocol) {
3534 case ATA_PROT_ATAPI_DMA:
3535 host_stat = ap->ops->bmdma_status(ap);
3537 /* before we do anything else, clear DMA-Start bit */
3538 ap->ops->bmdma_stop(qc);
3544 drv_stat = ata_chk_status(ap);
3546 /* ack bmdma irq events */
3547 ap->ops->irq_clear(ap);
3549 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3550 ap->id, qc->tf.command, drv_stat, host_stat);
3552 /* complete taskfile transaction */
3553 qc->err_mask |= ac_err_mask(drv_stat);
3557 spin_unlock_irqrestore(&host_set->lock, flags);
3559 ata_eh_qc_complete(qc);
3565 * ata_eng_timeout - Handle timeout of queued command
3566 * @ap: Port on which timed-out command is active
3568 * Some part of the kernel (currently, only the SCSI layer)
3569 * has noticed that the active command on port @ap has not
3570 * completed after a specified length of time. Handle this
3571 * condition by disabling DMA (if necessary) and completing
3572 * transactions, with error if necessary.
3574 * This also handles the case of the "lost interrupt", where
3575 * for some reason (possibly hardware bug, possibly driver bug)
3576 * an interrupt was not delivered to the driver, even though the
3577 * transaction completed successfully.
3580 * Inherited from SCSI layer (none, can sleep)
3583 void ata_eng_timeout(struct ata_port *ap)
3587 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3593 * ata_qc_new - Request an available ATA command, for queueing
3594 * @ap: Port associated with device @dev
3595 * @dev: Device from whom we request an available command structure
3601 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3603 struct ata_queued_cmd *qc = NULL;
3606 for (i = 0; i < ATA_MAX_QUEUE; i++)
3607 if (!test_and_set_bit(i, &ap->qactive)) {
3608 qc = ata_qc_from_tag(ap, i);
3619 * ata_qc_new_init - Request an available ATA command, and initialize it
3620 * @ap: Port associated with device @dev
3621 * @dev: Device from whom we request an available command structure
3627 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3628 struct ata_device *dev)
3630 struct ata_queued_cmd *qc;
3632 qc = ata_qc_new(ap);
3645 * ata_qc_free - free unused ata_queued_cmd
3646 * @qc: Command to complete
3648 * Designed to free unused ata_queued_cmd object
3649 * in case something prevents using it.
3652 * spin_lock_irqsave(host_set lock)
3654 void ata_qc_free(struct ata_queued_cmd *qc)
3656 struct ata_port *ap = qc->ap;
3659 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3663 if (likely(ata_tag_valid(tag))) {
3664 if (tag == ap->active_tag)
3665 ap->active_tag = ATA_TAG_POISON;
3666 qc->tag = ATA_TAG_POISON;
3667 clear_bit(tag, &ap->qactive);
3671 void __ata_qc_complete(struct ata_queued_cmd *qc)
3673 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3674 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3676 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3679 /* atapi: mark qc as inactive to prevent the interrupt handler
3680 * from completing the command twice later, before the error handler
3681 * is called. (when rc != 0 and atapi request sense is needed)
3683 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3685 /* call completion callback */
3686 qc->complete_fn(qc);
3689 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3691 struct ata_port *ap = qc->ap;
3693 switch (qc->tf.protocol) {
3695 case ATA_PROT_ATAPI_DMA:
3698 case ATA_PROT_ATAPI:
3700 case ATA_PROT_PIO_MULT:
3701 if (ap->flags & ATA_FLAG_PIO_DMA)
3714 * ata_qc_issue - issue taskfile to device
3715 * @qc: command to issue to device
3717 * Prepare an ATA command to submission to device.
3718 * This includes mapping the data into a DMA-able
3719 * area, filling in the S/G table, and finally
3720 * writing the taskfile to hardware, starting the command.
3723 * spin_lock_irqsave(host_set lock)
3726 * Zero on success, AC_ERR_* mask on failure
3729 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3731 struct ata_port *ap = qc->ap;
3733 if (ata_should_dma_map(qc)) {
3734 if (qc->flags & ATA_QCFLAG_SG) {
3735 if (ata_sg_setup(qc))
3737 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3738 if (ata_sg_setup_one(qc))
3742 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3745 ap->ops->qc_prep(qc);
3747 qc->ap->active_tag = qc->tag;
3748 qc->flags |= ATA_QCFLAG_ACTIVE;
3750 return ap->ops->qc_issue(qc);
3753 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3754 return AC_ERR_SYSTEM;
3759 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3760 * @qc: command to issue to device
3762 * Using various libata functions and hooks, this function
3763 * starts an ATA command. ATA commands are grouped into
3764 * classes called "protocols", and issuing each type of protocol
3765 * is slightly different.
3767 * May be used as the qc_issue() entry in ata_port_operations.
3770 * spin_lock_irqsave(host_set lock)
3773 * Zero on success, AC_ERR_* mask on failure
3776 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3778 struct ata_port *ap = qc->ap;
3780 ata_dev_select(ap, qc->dev->devno, 1, 0);
3782 switch (qc->tf.protocol) {
3783 case ATA_PROT_NODATA:
3784 ata_tf_to_host(ap, &qc->tf);
3788 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3789 ap->ops->bmdma_setup(qc); /* set up bmdma */
3790 ap->ops->bmdma_start(qc); /* initiate bmdma */
3793 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3794 ata_qc_set_polling(qc);
3795 ata_tf_to_host(ap, &qc->tf);
3796 ap->hsm_task_state = HSM_ST;
3797 ata_queue_pio_task(ap);
3800 case ATA_PROT_ATAPI:
3801 ata_qc_set_polling(qc);
3802 ata_tf_to_host(ap, &qc->tf);
3803 ata_queue_packet_task(ap);
3806 case ATA_PROT_ATAPI_NODATA:
3807 ap->flags |= ATA_FLAG_NOINTR;
3808 ata_tf_to_host(ap, &qc->tf);
3809 ata_queue_packet_task(ap);
3812 case ATA_PROT_ATAPI_DMA:
3813 ap->flags |= ATA_FLAG_NOINTR;
3814 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3815 ap->ops->bmdma_setup(qc); /* set up bmdma */
3816 ata_queue_packet_task(ap);
3821 return AC_ERR_SYSTEM;
3828 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3829 * @qc: Info associated with this ATA transaction.
3832 * spin_lock_irqsave(host_set lock)
3835 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3837 struct ata_port *ap = qc->ap;
3838 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3840 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3842 /* load PRD table addr. */
3843 mb(); /* make sure PRD table writes are visible to controller */
3844 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3846 /* specify data direction, triple-check start bit is clear */
3847 dmactl = readb(mmio + ATA_DMA_CMD);
3848 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3850 dmactl |= ATA_DMA_WR;
3851 writeb(dmactl, mmio + ATA_DMA_CMD);
3853 /* issue r/w command */
3854 ap->ops->exec_command(ap, &qc->tf);
3858 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3859 * @qc: Info associated with this ATA transaction.
3862 * spin_lock_irqsave(host_set lock)
3865 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3867 struct ata_port *ap = qc->ap;
3868 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3871 /* start host DMA transaction */
3872 dmactl = readb(mmio + ATA_DMA_CMD);
3873 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3875 /* Strictly, one may wish to issue a readb() here, to
3876 * flush the mmio write. However, control also passes
3877 * to the hardware at this point, and it will interrupt
3878 * us when we are to resume control. So, in effect,
3879 * we don't care when the mmio write flushes.
3880 * Further, a read of the DMA status register _immediately_
3881 * following the write may not be what certain flaky hardware
3882 * is expected, so I think it is best to not add a readb()
3883 * without first all the MMIO ATA cards/mobos.
3884 * Or maybe I'm just being paranoid.
3889 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3890 * @qc: Info associated with this ATA transaction.
3893 * spin_lock_irqsave(host_set lock)
3896 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3898 struct ata_port *ap = qc->ap;
3899 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3902 /* load PRD table addr. */
3903 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3905 /* specify data direction, triple-check start bit is clear */
3906 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3907 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3909 dmactl |= ATA_DMA_WR;
3910 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3912 /* issue r/w command */
3913 ap->ops->exec_command(ap, &qc->tf);
3917 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3918 * @qc: Info associated with this ATA transaction.
3921 * spin_lock_irqsave(host_set lock)
3924 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3926 struct ata_port *ap = qc->ap;
3929 /* start host DMA transaction */
3930 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3931 outb(dmactl | ATA_DMA_START,
3932 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3937 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3938 * @qc: Info associated with this ATA transaction.
3940 * Writes the ATA_DMA_START flag to the DMA command register.
3942 * May be used as the bmdma_start() entry in ata_port_operations.
3945 * spin_lock_irqsave(host_set lock)
3947 void ata_bmdma_start(struct ata_queued_cmd *qc)
3949 if (qc->ap->flags & ATA_FLAG_MMIO)
3950 ata_bmdma_start_mmio(qc);
3952 ata_bmdma_start_pio(qc);
3957 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3958 * @qc: Info associated with this ATA transaction.
3960 * Writes address of PRD table to device's PRD Table Address
3961 * register, sets the DMA control register, and calls
3962 * ops->exec_command() to start the transfer.
3964 * May be used as the bmdma_setup() entry in ata_port_operations.
3967 * spin_lock_irqsave(host_set lock)
3969 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3971 if (qc->ap->flags & ATA_FLAG_MMIO)
3972 ata_bmdma_setup_mmio(qc);
3974 ata_bmdma_setup_pio(qc);
3979 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3980 * @ap: Port associated with this ATA transaction.
3982 * Clear interrupt and error flags in DMA status register.
3984 * May be used as the irq_clear() entry in ata_port_operations.
3987 * spin_lock_irqsave(host_set lock)
3990 void ata_bmdma_irq_clear(struct ata_port *ap)
3992 if (ap->flags & ATA_FLAG_MMIO) {
3993 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3994 writeb(readb(mmio), mmio);
3996 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3997 outb(inb(addr), addr);
4004 * ata_bmdma_status - Read PCI IDE BMDMA status
4005 * @ap: Port associated with this ATA transaction.
4007 * Read and return BMDMA status register.
4009 * May be used as the bmdma_status() entry in ata_port_operations.
4012 * spin_lock_irqsave(host_set lock)
4015 u8 ata_bmdma_status(struct ata_port *ap)
4018 if (ap->flags & ATA_FLAG_MMIO) {
4019 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4020 host_stat = readb(mmio + ATA_DMA_STATUS);
4022 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4028 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4029 * @qc: Command we are ending DMA for
4031 * Clears the ATA_DMA_START flag in the dma control register
4033 * May be used as the bmdma_stop() entry in ata_port_operations.
4036 * spin_lock_irqsave(host_set lock)
4039 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4041 struct ata_port *ap = qc->ap;
4042 if (ap->flags & ATA_FLAG_MMIO) {
4043 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4045 /* clear start/stop bit */
4046 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4047 mmio + ATA_DMA_CMD);
4049 /* clear start/stop bit */
4050 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4051 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4054 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4055 ata_altstatus(ap); /* dummy read */
4059 * ata_host_intr - Handle host interrupt for given (port, task)
4060 * @ap: Port on which interrupt arrived (possibly...)
4061 * @qc: Taskfile currently active in engine
4063 * Handle host interrupt for given queued command. Currently,
4064 * only DMA interrupts are handled. All other commands are
4065 * handled via polling with interrupts disabled (nIEN bit).
4068 * spin_lock_irqsave(host_set lock)
4071 * One if interrupt was handled, zero if not (shared irq).
4074 inline unsigned int ata_host_intr (struct ata_port *ap,
4075 struct ata_queued_cmd *qc)
4077 u8 status, host_stat;
4079 switch (qc->tf.protocol) {
4082 case ATA_PROT_ATAPI_DMA:
4083 case ATA_PROT_ATAPI:
4084 /* check status of DMA engine */
4085 host_stat = ap->ops->bmdma_status(ap);
4086 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4088 /* if it's not our irq... */
4089 if (!(host_stat & ATA_DMA_INTR))
4092 /* before we do anything else, clear DMA-Start bit */
4093 ap->ops->bmdma_stop(qc);
4097 case ATA_PROT_ATAPI_NODATA:
4098 case ATA_PROT_NODATA:
4099 /* check altstatus */
4100 status = ata_altstatus(ap);
4101 if (status & ATA_BUSY)
4104 /* check main status, clearing INTRQ */
4105 status = ata_chk_status(ap);
4106 if (unlikely(status & ATA_BUSY))
4108 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4109 ap->id, qc->tf.protocol, status);
4111 /* ack bmdma irq events */
4112 ap->ops->irq_clear(ap);
4114 /* complete taskfile transaction */
4115 qc->err_mask |= ac_err_mask(status);
4116 ata_qc_complete(qc);
4123 return 1; /* irq handled */
4126 ap->stats.idle_irq++;
4129 if ((ap->stats.idle_irq % 1000) == 0) {
4131 ata_irq_ack(ap, 0); /* debug trap */
4132 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4135 return 0; /* irq not handled */
4139 * ata_interrupt - Default ATA host interrupt handler
4140 * @irq: irq line (unused)
4141 * @dev_instance: pointer to our ata_host_set information structure
4144 * Default interrupt handler for PCI IDE devices. Calls
4145 * ata_host_intr() for each port that is not disabled.
4148 * Obtains host_set lock during operation.
4151 * IRQ_NONE or IRQ_HANDLED.
4154 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4156 struct ata_host_set *host_set = dev_instance;
4158 unsigned int handled = 0;
4159 unsigned long flags;
4161 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4162 spin_lock_irqsave(&host_set->lock, flags);
4164 for (i = 0; i < host_set->n_ports; i++) {
4165 struct ata_port *ap;
4167 ap = host_set->ports[i];
4169 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4170 struct ata_queued_cmd *qc;
4172 qc = ata_qc_from_tag(ap, ap->active_tag);
4173 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4174 (qc->flags & ATA_QCFLAG_ACTIVE))
4175 handled |= ata_host_intr(ap, qc);
4179 spin_unlock_irqrestore(&host_set->lock, flags);
4181 return IRQ_RETVAL(handled);
4185 * atapi_packet_task - Write CDB bytes to hardware
4186 * @_data: Port to which ATAPI device is attached.
4188 * When device has indicated its readiness to accept
4189 * a CDB, this function is called. Send the CDB.
4190 * If DMA is to be performed, exit immediately.
4191 * Otherwise, we are in polling mode, so poll
4192 * status under operation succeeds or fails.
4195 * Kernel thread context (may sleep)
4198 static void atapi_packet_task(void *_data)
4200 struct ata_port *ap = _data;
4201 struct ata_queued_cmd *qc;
4204 qc = ata_qc_from_tag(ap, ap->active_tag);
4205 WARN_ON(qc == NULL);
4206 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4208 /* sleep-wait for BSY to clear */
4209 DPRINTK("busy wait\n");
4210 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4211 qc->err_mask |= AC_ERR_TIMEOUT;
4215 /* make sure DRQ is set */
4216 status = ata_chk_status(ap);
4217 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4218 qc->err_mask |= AC_ERR_HSM;
4223 DPRINTK("send cdb\n");
4224 WARN_ON(qc->dev->cdb_len < 12);
4226 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4227 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4228 unsigned long flags;
4230 /* Once we're done issuing command and kicking bmdma,
4231 * irq handler takes over. To not lose irq, we need
4232 * to clear NOINTR flag before sending cdb, but
4233 * interrupt handler shouldn't be invoked before we're
4234 * finished. Hence, the following locking.
4236 spin_lock_irqsave(&ap->host_set->lock, flags);
4237 ap->flags &= ~ATA_FLAG_NOINTR;
4238 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4239 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4240 ap->ops->bmdma_start(qc); /* initiate bmdma */
4241 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4243 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4245 /* PIO commands are handled by polling */
4246 ap->hsm_task_state = HSM_ST;
4247 ata_queue_pio_task(ap);
4253 ata_poll_qc_complete(qc);
4258 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4259 * without filling any other registers
4261 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4264 struct ata_taskfile tf;
4267 ata_tf_init(ap, &tf, dev->devno);
4270 tf.flags |= ATA_TFLAG_DEVICE;
4271 tf.protocol = ATA_PROT_NODATA;
4273 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4275 printk(KERN_ERR "%s: ata command failed: %d\n",
4281 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4285 if (!ata_try_flush_cache(dev))
4288 if (ata_id_has_flush_ext(dev->id))
4289 cmd = ATA_CMD_FLUSH_EXT;
4291 cmd = ATA_CMD_FLUSH;
4293 return ata_do_simple_cmd(ap, dev, cmd);
4296 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4298 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4301 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4303 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4307 * ata_device_resume - wakeup a previously suspended devices
4308 * @ap: port the device is connected to
4309 * @dev: the device to resume
4311 * Kick the drive back into action, by sending it an idle immediate
4312 * command and making sure its transfer mode matches between drive
4316 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4318 if (ap->flags & ATA_FLAG_SUSPENDED) {
4319 ap->flags &= ~ATA_FLAG_SUSPENDED;
4322 if (!ata_dev_present(dev))
4324 if (dev->class == ATA_DEV_ATA)
4325 ata_start_drive(ap, dev);
4331 * ata_device_suspend - prepare a device for suspend
4332 * @ap: port the device is connected to
4333 * @dev: the device to suspend
4335 * Flush the cache on the drive, if appropriate, then issue a
4336 * standbynow command.
4338 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4340 if (!ata_dev_present(dev))
4342 if (dev->class == ATA_DEV_ATA)
4343 ata_flush_cache(ap, dev);
4345 ata_standby_drive(ap, dev);
4346 ap->flags |= ATA_FLAG_SUSPENDED;
4351 * ata_port_start - Set port up for dma.
4352 * @ap: Port to initialize
4354 * Called just after data structures for each port are
4355 * initialized. Allocates space for PRD table.
4357 * May be used as the port_start() entry in ata_port_operations.
4360 * Inherited from caller.
4363 int ata_port_start (struct ata_port *ap)
4365 struct device *dev = ap->host_set->dev;
4368 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4372 rc = ata_pad_alloc(ap, dev);
4374 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4378 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4385 * ata_port_stop - Undo ata_port_start()
4386 * @ap: Port to shut down
4388 * Frees the PRD table.
4390 * May be used as the port_stop() entry in ata_port_operations.
4393 * Inherited from caller.
4396 void ata_port_stop (struct ata_port *ap)
4398 struct device *dev = ap->host_set->dev;
4400 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4401 ata_pad_free(ap, dev);
4404 void ata_host_stop (struct ata_host_set *host_set)
4406 if (host_set->mmio_base)
4407 iounmap(host_set->mmio_base);
4412 * ata_host_remove - Unregister SCSI host structure with upper layers
4413 * @ap: Port to unregister
4414 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4417 * Inherited from caller.
4420 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4422 struct Scsi_Host *sh = ap->host;
4427 scsi_remove_host(sh);
4429 ap->ops->port_stop(ap);
4433 * ata_host_init - Initialize an ata_port structure
4434 * @ap: Structure to initialize
4435 * @host: associated SCSI mid-layer structure
4436 * @host_set: Collection of hosts to which @ap belongs
4437 * @ent: Probe information provided by low-level driver
4438 * @port_no: Port number associated with this ata_port
4440 * Initialize a new ata_port structure, and its associated
4444 * Inherited from caller.
4447 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4448 struct ata_host_set *host_set,
4449 const struct ata_probe_ent *ent, unsigned int port_no)
4455 host->max_channel = 1;
4456 host->unique_id = ata_unique_id++;
4457 host->max_cmd_len = 12;
4459 ap->flags = ATA_FLAG_PORT_DISABLED;
4460 ap->id = host->unique_id;
4462 ap->ctl = ATA_DEVCTL_OBS;
4463 ap->host_set = host_set;
4464 ap->port_no = port_no;
4466 ent->legacy_mode ? ent->hard_port_no : port_no;
4467 ap->pio_mask = ent->pio_mask;
4468 ap->mwdma_mask = ent->mwdma_mask;
4469 ap->udma_mask = ent->udma_mask;
4470 ap->flags |= ent->host_flags;
4471 ap->ops = ent->port_ops;
4472 ap->cbl = ATA_CBL_NONE;
4473 ap->active_tag = ATA_TAG_POISON;
4474 ap->last_ctl = 0xFF;
4476 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4477 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4478 INIT_LIST_HEAD(&ap->eh_done_q);
4480 for (i = 0; i < ATA_MAX_DEVICES; i++)
4481 ap->device[i].devno = i;
4484 ap->stats.unhandled_irq = 1;
4485 ap->stats.idle_irq = 1;
4488 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4492 * ata_host_add - Attach low-level ATA driver to system
4493 * @ent: Information provided by low-level driver
4494 * @host_set: Collections of ports to which we add
4495 * @port_no: Port number associated with this host
4497 * Attach low-level ATA driver to system.
4500 * PCI/etc. bus probe sem.
4503 * New ata_port on success, for NULL on error.
4506 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4507 struct ata_host_set *host_set,
4508 unsigned int port_no)
4510 struct Scsi_Host *host;
4511 struct ata_port *ap;
4515 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4519 ap = (struct ata_port *) &host->hostdata[0];
4521 ata_host_init(ap, host, host_set, ent, port_no);
4523 rc = ap->ops->port_start(ap);
4530 scsi_host_put(host);
4535 * ata_device_add - Register hardware device with ATA and SCSI layers
4536 * @ent: Probe information describing hardware device to be registered
4538 * This function processes the information provided in the probe
4539 * information struct @ent, allocates the necessary ATA and SCSI
4540 * host information structures, initializes them, and registers
4541 * everything with requisite kernel subsystems.
4543 * This function requests irqs, probes the ATA bus, and probes
4547 * PCI/etc. bus probe sem.
4550 * Number of ports registered. Zero on error (no ports registered).
4553 int ata_device_add(const struct ata_probe_ent *ent)
4555 unsigned int count = 0, i;
4556 struct device *dev = ent->dev;
4557 struct ata_host_set *host_set;
4560 /* alloc a container for our list of ATA ports (buses) */
4561 host_set = kzalloc(sizeof(struct ata_host_set) +
4562 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4565 spin_lock_init(&host_set->lock);
4567 host_set->dev = dev;
4568 host_set->n_ports = ent->n_ports;
4569 host_set->irq = ent->irq;
4570 host_set->mmio_base = ent->mmio_base;
4571 host_set->private_data = ent->private_data;
4572 host_set->ops = ent->port_ops;
4574 /* register each port bound to this device */
4575 for (i = 0; i < ent->n_ports; i++) {
4576 struct ata_port *ap;
4577 unsigned long xfer_mode_mask;
4579 ap = ata_host_add(ent, host_set, i);
4583 host_set->ports[i] = ap;
4584 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4585 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4586 (ap->pio_mask << ATA_SHIFT_PIO);
4588 /* print per-port info to dmesg */
4589 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4590 "bmdma 0x%lX irq %lu\n",
4592 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4593 ata_mode_string(xfer_mode_mask),
4594 ap->ioaddr.cmd_addr,
4595 ap->ioaddr.ctl_addr,
4596 ap->ioaddr.bmdma_addr,
4600 host_set->ops->irq_clear(ap);
4607 /* obtain irq, that is shared between channels */
4608 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4609 DRV_NAME, host_set))
4612 /* perform each probe synchronously */
4613 DPRINTK("probe begin\n");
4614 for (i = 0; i < count; i++) {
4615 struct ata_port *ap;
4618 ap = host_set->ports[i];
4620 DPRINTK("ata%u: bus probe begin\n", ap->id);
4621 rc = ata_bus_probe(ap);
4622 DPRINTK("ata%u: bus probe end\n", ap->id);
4625 /* FIXME: do something useful here?
4626 * Current libata behavior will
4627 * tear down everything when
4628 * the module is removed
4629 * or the h/w is unplugged.
4633 rc = scsi_add_host(ap->host, dev);
4635 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4637 /* FIXME: do something useful here */
4638 /* FIXME: handle unconditional calls to
4639 * scsi_scan_host and ata_host_remove, below,
4645 /* probes are done, now scan each port's disk(s) */
4646 DPRINTK("host probe begin\n");
4647 for (i = 0; i < count; i++) {
4648 struct ata_port *ap = host_set->ports[i];
4650 ata_scsi_scan_host(ap);
4653 dev_set_drvdata(dev, host_set);
4655 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4656 return ent->n_ports; /* success */
4659 for (i = 0; i < count; i++) {
4660 ata_host_remove(host_set->ports[i], 1);
4661 scsi_host_put(host_set->ports[i]->host);
4665 VPRINTK("EXIT, returning 0\n");
4670 * ata_host_set_remove - PCI layer callback for device removal
4671 * @host_set: ATA host set that was removed
4673 * Unregister all objects associated with this host set. Free those
4677 * Inherited from calling layer (may sleep).
4680 void ata_host_set_remove(struct ata_host_set *host_set)
4682 struct ata_port *ap;
4685 for (i = 0; i < host_set->n_ports; i++) {
4686 ap = host_set->ports[i];
4687 scsi_remove_host(ap->host);
4690 free_irq(host_set->irq, host_set);
4692 for (i = 0; i < host_set->n_ports; i++) {
4693 ap = host_set->ports[i];
4695 ata_scsi_release(ap->host);
4697 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4698 struct ata_ioports *ioaddr = &ap->ioaddr;
4700 if (ioaddr->cmd_addr == 0x1f0)
4701 release_region(0x1f0, 8);
4702 else if (ioaddr->cmd_addr == 0x170)
4703 release_region(0x170, 8);
4706 scsi_host_put(ap->host);
4709 if (host_set->ops->host_stop)
4710 host_set->ops->host_stop(host_set);
4716 * ata_scsi_release - SCSI layer callback hook for host unload
4717 * @host: libata host to be unloaded
4719 * Performs all duties necessary to shut down a libata port...
4720 * Kill port kthread, disable port, and release resources.
4723 * Inherited from SCSI layer.
4729 int ata_scsi_release(struct Scsi_Host *host)
4731 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4735 ap->ops->port_disable(ap);
4736 ata_host_remove(ap, 0);
4743 * ata_std_ports - initialize ioaddr with standard port offsets.
4744 * @ioaddr: IO address structure to be initialized
4746 * Utility function which initializes data_addr, error_addr,
4747 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4748 * device_addr, status_addr, and command_addr to standard offsets
4749 * relative to cmd_addr.
4751 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4754 void ata_std_ports(struct ata_ioports *ioaddr)
4756 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4757 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4758 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4759 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4760 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4761 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4762 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4763 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4764 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4765 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4771 void ata_pci_host_stop (struct ata_host_set *host_set)
4773 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4775 pci_iounmap(pdev, host_set->mmio_base);
4779 * ata_pci_remove_one - PCI layer callback for device removal
4780 * @pdev: PCI device that was removed
4782 * PCI layer indicates to libata via this hook that
4783 * hot-unplug or module unload event has occurred.
4784 * Handle this by unregistering all objects associated
4785 * with this PCI device. Free those objects. Then finally
4786 * release PCI resources and disable device.
4789 * Inherited from PCI layer (may sleep).
4792 void ata_pci_remove_one (struct pci_dev *pdev)
4794 struct device *dev = pci_dev_to_dev(pdev);
4795 struct ata_host_set *host_set = dev_get_drvdata(dev);
4797 ata_host_set_remove(host_set);
4798 pci_release_regions(pdev);
4799 pci_disable_device(pdev);
4800 dev_set_drvdata(dev, NULL);
4803 /* move to PCI subsystem */
4804 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4806 unsigned long tmp = 0;
4808 switch (bits->width) {
4811 pci_read_config_byte(pdev, bits->reg, &tmp8);
4817 pci_read_config_word(pdev, bits->reg, &tmp16);
4823 pci_read_config_dword(pdev, bits->reg, &tmp32);
4834 return (tmp == bits->val) ? 1 : 0;
4837 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4839 pci_save_state(pdev);
4840 pci_disable_device(pdev);
4841 pci_set_power_state(pdev, PCI_D3hot);
4845 int ata_pci_device_resume(struct pci_dev *pdev)
4847 pci_set_power_state(pdev, PCI_D0);
4848 pci_restore_state(pdev);
4849 pci_enable_device(pdev);
4850 pci_set_master(pdev);
4853 #endif /* CONFIG_PCI */
4856 static int __init ata_init(void)
4858 ata_wq = create_workqueue("ata");
4862 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4866 static void __exit ata_exit(void)
4868 destroy_workqueue(ata_wq);
4871 module_init(ata_init);
4872 module_exit(ata_exit);
4874 static unsigned long ratelimit_time;
4875 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4877 int ata_ratelimit(void)
4880 unsigned long flags;
4882 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4884 if (time_after(jiffies, ratelimit_time)) {
4886 ratelimit_time = jiffies + (HZ/5);
4890 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4896 * libata is essentially a library of internal helper functions for
4897 * low-level ATA host controller drivers. As such, the API/ABI is
4898 * likely to change as new drivers are added and updated.
4899 * Do not depend on ABI/API stability.
4902 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4903 EXPORT_SYMBOL_GPL(ata_std_ports);
4904 EXPORT_SYMBOL_GPL(ata_device_add);
4905 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4906 EXPORT_SYMBOL_GPL(ata_sg_init);
4907 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4908 EXPORT_SYMBOL_GPL(__ata_qc_complete);
4909 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4910 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4911 EXPORT_SYMBOL_GPL(ata_tf_load);
4912 EXPORT_SYMBOL_GPL(ata_tf_read);
4913 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4914 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4915 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4916 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4917 EXPORT_SYMBOL_GPL(ata_check_status);
4918 EXPORT_SYMBOL_GPL(ata_altstatus);
4919 EXPORT_SYMBOL_GPL(ata_exec_command);
4920 EXPORT_SYMBOL_GPL(ata_port_start);
4921 EXPORT_SYMBOL_GPL(ata_port_stop);
4922 EXPORT_SYMBOL_GPL(ata_host_stop);
4923 EXPORT_SYMBOL_GPL(ata_interrupt);
4924 EXPORT_SYMBOL_GPL(ata_qc_prep);
4925 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4926 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4927 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4928 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4929 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4930 EXPORT_SYMBOL_GPL(ata_port_probe);
4931 EXPORT_SYMBOL_GPL(sata_phy_reset);
4932 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4933 EXPORT_SYMBOL_GPL(ata_bus_reset);
4934 EXPORT_SYMBOL_GPL(ata_std_probeinit);
4935 EXPORT_SYMBOL_GPL(ata_std_softreset);
4936 EXPORT_SYMBOL_GPL(sata_std_hardreset);
4937 EXPORT_SYMBOL_GPL(ata_std_postreset);
4938 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
4939 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
4940 EXPORT_SYMBOL_GPL(ata_port_disable);
4941 EXPORT_SYMBOL_GPL(ata_ratelimit);
4942 EXPORT_SYMBOL_GPL(ata_busy_sleep);
4943 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4944 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4945 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
4946 EXPORT_SYMBOL_GPL(ata_scsi_error);
4947 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4948 EXPORT_SYMBOL_GPL(ata_scsi_release);
4949 EXPORT_SYMBOL_GPL(ata_host_intr);
4950 EXPORT_SYMBOL_GPL(ata_dev_classify);
4951 EXPORT_SYMBOL_GPL(ata_id_string);
4952 EXPORT_SYMBOL_GPL(ata_id_c_string);
4953 EXPORT_SYMBOL_GPL(ata_dev_config);
4954 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4955 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4956 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
4958 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
4959 EXPORT_SYMBOL_GPL(ata_timing_compute);
4960 EXPORT_SYMBOL_GPL(ata_timing_merge);
4963 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4964 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4965 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4966 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4967 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4968 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4969 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
4970 #endif /* CONFIG_PCI */
4972 EXPORT_SYMBOL_GPL(ata_device_suspend);
4973 EXPORT_SYMBOL_GPL(ata_device_resume);
4974 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4975 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);