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 void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
65 static void ata_dev_init_params(struct ata_port *ap, 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 : 4;
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_dev_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_dev_id_string(const u16 *id, unsigned char *s,
504 unsigned int ofs, unsigned int len)
523 * ata_dev_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_dev_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_dev_id_c_string(const u16 *id, unsigned char *s,
537 unsigned int ofs, unsigned int len)
543 ata_dev_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_identify - obtain IDENTIFY x DEVICE page
906 * @ap: port on which device we wish to probe resides
907 * @device: device bus address, starting at zero
909 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
910 * command, and read back the 512-byte device information page.
911 * The device information page is fed to us via the standard
912 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
913 * using standard PIO-IN paths)
915 * After reading the device information page, we use several
916 * bits of information from it to initialize data structures
917 * that will be used during the lifetime of the ata_device.
918 * Other data from the info page is used to disqualify certain
919 * older ATA devices we do not wish to support.
922 * Inherited from caller. Some functions called by this function
923 * obtain the host_set lock.
926 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
928 struct ata_device *dev = &ap->device[device];
929 unsigned int major_version;
930 unsigned long xfer_modes;
931 unsigned int using_edd;
932 struct ata_taskfile tf;
933 unsigned int err_mask;
936 if (!ata_dev_present(dev)) {
937 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
942 if (ap->ops->probe_reset ||
943 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
948 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
950 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
951 dev->class != ATA_DEV_NONE);
953 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
956 ata_tf_init(ap, &tf, device);
958 if (dev->class == ATA_DEV_ATA) {
959 tf.command = ATA_CMD_ID_ATA;
960 DPRINTK("do ATA identify\n");
962 tf.command = ATA_CMD_ID_ATAPI;
963 DPRINTK("do ATAPI identify\n");
966 tf.protocol = ATA_PROT_PIO;
968 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
969 dev->id, sizeof(dev->id));
972 if (err_mask & ~AC_ERR_DEV)
976 * arg! EDD works for all test cases, but seems to return
977 * the ATA signature for some ATAPI devices. Until the
978 * reason for this is found and fixed, we fix up the mess
979 * here. If IDENTIFY DEVICE returns command aborted
980 * (as ATAPI devices do), then we issue an
981 * IDENTIFY PACKET DEVICE.
983 * ATA software reset (SRST, the default) does not appear
984 * to have this problem.
986 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
988 if (err & ATA_ABORTED) {
989 dev->class = ATA_DEV_ATAPI;
996 swap_buf_le16(dev->id, ATA_ID_WORDS);
998 /* print device capabilities */
999 printk(KERN_DEBUG "ata%u: dev %u cfg "
1000 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1001 ap->id, device, dev->id[49],
1002 dev->id[82], dev->id[83], dev->id[84],
1003 dev->id[85], dev->id[86], dev->id[87],
1007 * common ATA, ATAPI feature tests
1010 /* we require DMA support (bits 8 of word 49) */
1011 if (!ata_id_has_dma(dev->id)) {
1012 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1016 /* quick-n-dirty find max transfer mode; for printk only */
1017 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1019 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1021 xfer_modes = ata_pio_modes(dev);
1023 ata_dump_id(dev->id);
1025 /* ATA-specific feature tests */
1026 if (dev->class == ATA_DEV_ATA) {
1027 dev->n_sectors = ata_id_n_sectors(dev->id);
1029 if (!ata_id_is_ata(dev->id)) /* sanity check */
1032 /* get major version */
1033 major_version = ata_id_major_version(dev->id);
1036 * The exact sequence expected by certain pre-ATA4 drives is:
1039 * INITIALIZE DEVICE PARAMETERS
1041 * Some drives were very specific about that exact sequence.
1043 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1044 ata_dev_init_params(ap, dev);
1046 /* current CHS translation info (id[53-58]) might be
1047 * changed. reread the identify device info.
1049 ata_dev_reread_id(ap, dev);
1052 if (ata_id_has_lba(dev->id)) {
1053 dev->flags |= ATA_DFLAG_LBA;
1055 if (ata_id_has_lba48(dev->id))
1056 dev->flags |= ATA_DFLAG_LBA48;
1058 /* print device info to dmesg */
1059 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1062 ata_mode_string(xfer_modes),
1063 (unsigned long long)dev->n_sectors,
1064 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1068 /* Default translation */
1069 dev->cylinders = dev->id[1];
1070 dev->heads = dev->id[3];
1071 dev->sectors = dev->id[6];
1073 if (ata_id_current_chs_valid(dev->id)) {
1074 /* Current CHS translation is valid. */
1075 dev->cylinders = dev->id[54];
1076 dev->heads = dev->id[55];
1077 dev->sectors = dev->id[56];
1080 /* print device info to dmesg */
1081 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1084 ata_mode_string(xfer_modes),
1085 (unsigned long long)dev->n_sectors,
1086 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1090 ap->host->max_cmd_len = 16;
1093 /* ATAPI-specific feature tests */
1094 else if (dev->class == ATA_DEV_ATAPI) {
1095 if (ata_id_is_ata(dev->id)) /* sanity check */
1098 rc = atapi_cdb_len(dev->id);
1099 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1100 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1103 ap->cdb_len = (unsigned int) rc;
1104 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1106 /* print device info to dmesg */
1107 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1109 ata_mode_string(xfer_modes));
1112 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1116 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1119 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1120 DPRINTK("EXIT, err\n");
1124 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1125 struct ata_device *dev)
1127 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1131 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1138 void ata_dev_config(struct ata_port *ap, unsigned int i)
1140 /* limit bridge transfers to udma5, 200 sectors */
1141 if (ata_dev_knobble(ap, &ap->device[i])) {
1142 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1144 ap->udma_mask &= ATA_UDMA5;
1145 ap->host->max_sectors = ATA_MAX_SECTORS;
1146 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1147 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
1150 if (ap->ops->dev_config)
1151 ap->ops->dev_config(ap, &ap->device[i]);
1155 * ata_bus_probe - Reset and probe ATA bus
1158 * Master ATA bus probing function. Initiates a hardware-dependent
1159 * bus reset, then attempts to identify any devices found on
1163 * PCI/etc. bus probe sem.
1166 * Zero on success, non-zero on error.
1169 static int ata_bus_probe(struct ata_port *ap)
1171 unsigned int i, found = 0;
1173 if (ap->ops->probe_reset) {
1174 unsigned int classes[ATA_MAX_DEVICES];
1179 rc = ap->ops->probe_reset(ap, classes);
1181 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1182 if (classes[i] == ATA_DEV_UNKNOWN)
1183 classes[i] = ATA_DEV_NONE;
1184 ap->device[i].class = classes[i];
1187 printk(KERN_ERR "ata%u: probe reset failed, "
1188 "disabling port\n", ap->id);
1189 ata_port_disable(ap);
1192 ap->ops->phy_reset(ap);
1194 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1197 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1198 ata_dev_identify(ap, i);
1199 if (ata_dev_present(&ap->device[i])) {
1201 ata_dev_config(ap,i);
1205 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1206 goto err_out_disable;
1209 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1210 goto err_out_disable;
1215 ap->ops->port_disable(ap);
1221 * ata_port_probe - Mark port as enabled
1222 * @ap: Port for which we indicate enablement
1224 * Modify @ap data structure such that the system
1225 * thinks that the entire port is enabled.
1227 * LOCKING: host_set lock, or some other form of
1231 void ata_port_probe(struct ata_port *ap)
1233 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1237 * sata_print_link_status - Print SATA link status
1238 * @ap: SATA port to printk link status about
1240 * This function prints link speed and status of a SATA link.
1245 static void sata_print_link_status(struct ata_port *ap)
1250 if (!ap->ops->scr_read)
1253 sstatus = scr_read(ap, SCR_STATUS);
1255 if (sata_dev_present(ap)) {
1256 tmp = (sstatus >> 4) & 0xf;
1259 else if (tmp & (1 << 1))
1262 speed = "<unknown>";
1263 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1264 ap->id, speed, sstatus);
1266 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1272 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1273 * @ap: SATA port associated with target SATA PHY.
1275 * This function issues commands to standard SATA Sxxx
1276 * PHY registers, to wake up the phy (and device), and
1277 * clear any reset condition.
1280 * PCI/etc. bus probe sem.
1283 void __sata_phy_reset(struct ata_port *ap)
1286 unsigned long timeout = jiffies + (HZ * 5);
1288 if (ap->flags & ATA_FLAG_SATA_RESET) {
1289 /* issue phy wake/reset */
1290 scr_write_flush(ap, SCR_CONTROL, 0x301);
1291 /* Couldn't find anything in SATA I/II specs, but
1292 * AHCI-1.1 10.4.2 says at least 1 ms. */
1295 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1297 /* wait for phy to become ready, if necessary */
1300 sstatus = scr_read(ap, SCR_STATUS);
1301 if ((sstatus & 0xf) != 1)
1303 } while (time_before(jiffies, timeout));
1305 /* print link status */
1306 sata_print_link_status(ap);
1308 /* TODO: phy layer with polling, timeouts, etc. */
1309 if (sata_dev_present(ap))
1312 ata_port_disable(ap);
1314 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1317 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1318 ata_port_disable(ap);
1322 ap->cbl = ATA_CBL_SATA;
1326 * sata_phy_reset - Reset SATA bus.
1327 * @ap: SATA port associated with target SATA PHY.
1329 * This function resets the SATA bus, and then probes
1330 * the bus for devices.
1333 * PCI/etc. bus probe sem.
1336 void sata_phy_reset(struct ata_port *ap)
1338 __sata_phy_reset(ap);
1339 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1345 * ata_port_disable - Disable port.
1346 * @ap: Port to be disabled.
1348 * Modify @ap data structure such that the system
1349 * thinks that the entire port is disabled, and should
1350 * never attempt to probe or communicate with devices
1353 * LOCKING: host_set lock, or some other form of
1357 void ata_port_disable(struct ata_port *ap)
1359 ap->device[0].class = ATA_DEV_NONE;
1360 ap->device[1].class = ATA_DEV_NONE;
1361 ap->flags |= ATA_FLAG_PORT_DISABLED;
1365 * This mode timing computation functionality is ported over from
1366 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1369 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1370 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1371 * for PIO 5, which is a nonstandard extension and UDMA6, which
1372 * is currently supported only by Maxtor drives.
1375 static const struct ata_timing ata_timing[] = {
1377 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1378 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1379 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1380 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1382 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1383 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1384 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1386 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1388 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1389 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1390 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1392 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1393 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1394 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1396 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1397 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1398 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1400 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1401 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1402 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1404 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1409 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1410 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1412 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1414 q->setup = EZ(t->setup * 1000, T);
1415 q->act8b = EZ(t->act8b * 1000, T);
1416 q->rec8b = EZ(t->rec8b * 1000, T);
1417 q->cyc8b = EZ(t->cyc8b * 1000, T);
1418 q->active = EZ(t->active * 1000, T);
1419 q->recover = EZ(t->recover * 1000, T);
1420 q->cycle = EZ(t->cycle * 1000, T);
1421 q->udma = EZ(t->udma * 1000, UT);
1424 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1425 struct ata_timing *m, unsigned int what)
1427 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1428 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1429 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1430 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1431 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1432 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1433 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1434 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1437 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1439 const struct ata_timing *t;
1441 for (t = ata_timing; t->mode != speed; t++)
1442 if (t->mode == 0xFF)
1447 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1448 struct ata_timing *t, int T, int UT)
1450 const struct ata_timing *s;
1451 struct ata_timing p;
1457 if (!(s = ata_timing_find_mode(speed)))
1460 memcpy(t, s, sizeof(*s));
1463 * If the drive is an EIDE drive, it can tell us it needs extended
1464 * PIO/MW_DMA cycle timing.
1467 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1468 memset(&p, 0, sizeof(p));
1469 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1470 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1471 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1472 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1473 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1475 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1479 * Convert the timing to bus clock counts.
1482 ata_timing_quantize(t, t, T, UT);
1485 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1486 * S.M.A.R.T * and some other commands. We have to ensure that the
1487 * DMA cycle timing is slower/equal than the fastest PIO timing.
1490 if (speed > XFER_PIO_4) {
1491 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1492 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1496 * Lengthen active & recovery time so that cycle time is correct.
1499 if (t->act8b + t->rec8b < t->cyc8b) {
1500 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1501 t->rec8b = t->cyc8b - t->act8b;
1504 if (t->active + t->recover < t->cycle) {
1505 t->active += (t->cycle - (t->active + t->recover)) / 2;
1506 t->recover = t->cycle - t->active;
1512 static const struct {
1515 } xfer_mode_classes[] = {
1516 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1517 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1518 { ATA_SHIFT_PIO, XFER_PIO_0 },
1521 static u8 base_from_shift(unsigned int shift)
1525 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1526 if (xfer_mode_classes[i].shift == shift)
1527 return xfer_mode_classes[i].base;
1532 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1537 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1540 if (dev->xfer_shift == ATA_SHIFT_PIO)
1541 dev->flags |= ATA_DFLAG_PIO;
1543 ata_dev_set_xfermode(ap, dev);
1545 base = base_from_shift(dev->xfer_shift);
1546 ofs = dev->xfer_mode - base;
1547 idx = ofs + dev->xfer_shift;
1548 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1550 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1551 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1553 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1554 ap->id, dev->devno, xfer_mode_str[idx]);
1557 static int ata_host_set_pio(struct ata_port *ap)
1563 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1566 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1570 base = base_from_shift(ATA_SHIFT_PIO);
1571 xfer_mode = base + x;
1573 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1574 (int)base, (int)xfer_mode, mask, x);
1576 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1577 struct ata_device *dev = &ap->device[i];
1578 if (ata_dev_present(dev)) {
1579 dev->pio_mode = xfer_mode;
1580 dev->xfer_mode = xfer_mode;
1581 dev->xfer_shift = ATA_SHIFT_PIO;
1582 if (ap->ops->set_piomode)
1583 ap->ops->set_piomode(ap, dev);
1590 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1591 unsigned int xfer_shift)
1595 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1596 struct ata_device *dev = &ap->device[i];
1597 if (ata_dev_present(dev)) {
1598 dev->dma_mode = xfer_mode;
1599 dev->xfer_mode = xfer_mode;
1600 dev->xfer_shift = xfer_shift;
1601 if (ap->ops->set_dmamode)
1602 ap->ops->set_dmamode(ap, dev);
1608 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1609 * @ap: port on which timings will be programmed
1611 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1614 * PCI/etc. bus probe sem.
1616 static void ata_set_mode(struct ata_port *ap)
1618 unsigned int xfer_shift;
1622 /* step 1: always set host PIO timings */
1623 rc = ata_host_set_pio(ap);
1627 /* step 2: choose the best data xfer mode */
1628 xfer_mode = xfer_shift = 0;
1629 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1633 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1634 if (xfer_shift != ATA_SHIFT_PIO)
1635 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1637 /* step 4: update devices' xfer mode */
1638 ata_dev_set_mode(ap, &ap->device[0]);
1639 ata_dev_set_mode(ap, &ap->device[1]);
1641 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1644 if (ap->ops->post_set_mode)
1645 ap->ops->post_set_mode(ap);
1650 ata_port_disable(ap);
1654 * ata_tf_to_host - issue ATA taskfile to host controller
1655 * @ap: port to which command is being issued
1656 * @tf: ATA taskfile register set
1658 * Issues ATA taskfile register set to ATA host controller,
1659 * with proper synchronization with interrupt handler and
1663 * spin_lock_irqsave(host_set lock)
1666 static inline void ata_tf_to_host(struct ata_port *ap,
1667 const struct ata_taskfile *tf)
1669 ap->ops->tf_load(ap, tf);
1670 ap->ops->exec_command(ap, tf);
1674 * ata_busy_sleep - sleep until BSY clears, or timeout
1675 * @ap: port containing status register to be polled
1676 * @tmout_pat: impatience timeout
1677 * @tmout: overall timeout
1679 * Sleep until ATA Status register bit BSY clears,
1680 * or a timeout occurs.
1685 unsigned int ata_busy_sleep (struct ata_port *ap,
1686 unsigned long tmout_pat, unsigned long tmout)
1688 unsigned long timer_start, timeout;
1691 status = ata_busy_wait(ap, ATA_BUSY, 300);
1692 timer_start = jiffies;
1693 timeout = timer_start + tmout_pat;
1694 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1696 status = ata_busy_wait(ap, ATA_BUSY, 3);
1699 if (status & ATA_BUSY)
1700 printk(KERN_WARNING "ata%u is slow to respond, "
1701 "please be patient\n", ap->id);
1703 timeout = timer_start + tmout;
1704 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1706 status = ata_chk_status(ap);
1709 if (status & ATA_BUSY) {
1710 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1711 ap->id, tmout / HZ);
1718 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1720 struct ata_ioports *ioaddr = &ap->ioaddr;
1721 unsigned int dev0 = devmask & (1 << 0);
1722 unsigned int dev1 = devmask & (1 << 1);
1723 unsigned long timeout;
1725 /* if device 0 was found in ata_devchk, wait for its
1729 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1731 /* if device 1 was found in ata_devchk, wait for
1732 * register access, then wait for BSY to clear
1734 timeout = jiffies + ATA_TMOUT_BOOT;
1738 ap->ops->dev_select(ap, 1);
1739 if (ap->flags & ATA_FLAG_MMIO) {
1740 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1741 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1743 nsect = inb(ioaddr->nsect_addr);
1744 lbal = inb(ioaddr->lbal_addr);
1746 if ((nsect == 1) && (lbal == 1))
1748 if (time_after(jiffies, timeout)) {
1752 msleep(50); /* give drive a breather */
1755 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1757 /* is all this really necessary? */
1758 ap->ops->dev_select(ap, 0);
1760 ap->ops->dev_select(ap, 1);
1762 ap->ops->dev_select(ap, 0);
1766 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1767 * @ap: Port to reset and probe
1769 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1770 * probe the bus. Not often used these days.
1773 * PCI/etc. bus probe sem.
1774 * Obtains host_set lock.
1778 static unsigned int ata_bus_edd(struct ata_port *ap)
1780 struct ata_taskfile tf;
1781 unsigned long flags;
1783 /* set up execute-device-diag (bus reset) taskfile */
1784 /* also, take interrupts to a known state (disabled) */
1785 DPRINTK("execute-device-diag\n");
1786 ata_tf_init(ap, &tf, 0);
1788 tf.command = ATA_CMD_EDD;
1789 tf.protocol = ATA_PROT_NODATA;
1792 spin_lock_irqsave(&ap->host_set->lock, flags);
1793 ata_tf_to_host(ap, &tf);
1794 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1796 /* spec says at least 2ms. but who knows with those
1797 * crazy ATAPI devices...
1801 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1804 static unsigned int ata_bus_softreset(struct ata_port *ap,
1805 unsigned int devmask)
1807 struct ata_ioports *ioaddr = &ap->ioaddr;
1809 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1811 /* software reset. causes dev0 to be selected */
1812 if (ap->flags & ATA_FLAG_MMIO) {
1813 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1814 udelay(20); /* FIXME: flush */
1815 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1816 udelay(20); /* FIXME: flush */
1817 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1819 outb(ap->ctl, ioaddr->ctl_addr);
1821 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1823 outb(ap->ctl, ioaddr->ctl_addr);
1826 /* spec mandates ">= 2ms" before checking status.
1827 * We wait 150ms, because that was the magic delay used for
1828 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1829 * between when the ATA command register is written, and then
1830 * status is checked. Because waiting for "a while" before
1831 * checking status is fine, post SRST, we perform this magic
1832 * delay here as well.
1836 ata_bus_post_reset(ap, devmask);
1842 * ata_bus_reset - reset host port and associated ATA channel
1843 * @ap: port to reset
1845 * This is typically the first time we actually start issuing
1846 * commands to the ATA channel. We wait for BSY to clear, then
1847 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1848 * result. Determine what devices, if any, are on the channel
1849 * by looking at the device 0/1 error register. Look at the signature
1850 * stored in each device's taskfile registers, to determine if
1851 * the device is ATA or ATAPI.
1854 * PCI/etc. bus probe sem.
1855 * Obtains host_set lock.
1858 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1861 void ata_bus_reset(struct ata_port *ap)
1863 struct ata_ioports *ioaddr = &ap->ioaddr;
1864 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1866 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1868 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1870 /* determine if device 0/1 are present */
1871 if (ap->flags & ATA_FLAG_SATA_RESET)
1874 dev0 = ata_devchk(ap, 0);
1876 dev1 = ata_devchk(ap, 1);
1880 devmask |= (1 << 0);
1882 devmask |= (1 << 1);
1884 /* select device 0 again */
1885 ap->ops->dev_select(ap, 0);
1887 /* issue bus reset */
1888 if (ap->flags & ATA_FLAG_SRST)
1889 rc = ata_bus_softreset(ap, devmask);
1890 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1891 /* set up device control */
1892 if (ap->flags & ATA_FLAG_MMIO)
1893 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1895 outb(ap->ctl, ioaddr->ctl_addr);
1896 rc = ata_bus_edd(ap);
1903 * determine by signature whether we have ATA or ATAPI devices
1905 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1906 if ((slave_possible) && (err != 0x81))
1907 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1909 /* re-enable interrupts */
1910 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1913 /* is double-select really necessary? */
1914 if (ap->device[1].class != ATA_DEV_NONE)
1915 ap->ops->dev_select(ap, 1);
1916 if (ap->device[0].class != ATA_DEV_NONE)
1917 ap->ops->dev_select(ap, 0);
1919 /* if no devices were detected, disable this port */
1920 if ((ap->device[0].class == ATA_DEV_NONE) &&
1921 (ap->device[1].class == ATA_DEV_NONE))
1924 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1925 /* set up device control for ATA_FLAG_SATA_RESET */
1926 if (ap->flags & ATA_FLAG_MMIO)
1927 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1929 outb(ap->ctl, ioaddr->ctl_addr);
1936 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1937 ap->ops->port_disable(ap);
1942 static int sata_phy_resume(struct ata_port *ap)
1944 unsigned long timeout = jiffies + (HZ * 5);
1947 scr_write_flush(ap, SCR_CONTROL, 0x300);
1949 /* Wait for phy to become ready, if necessary. */
1952 sstatus = scr_read(ap, SCR_STATUS);
1953 if ((sstatus & 0xf) != 1)
1955 } while (time_before(jiffies, timeout));
1961 * ata_std_probeinit - initialize probing
1962 * @ap: port to be probed
1964 * @ap is about to be probed. Initialize it. This function is
1965 * to be used as standard callback for ata_drive_probe_reset().
1967 * NOTE!!! Do not use this function as probeinit if a low level
1968 * driver implements only hardreset. Just pass NULL as probeinit
1969 * in that case. Using this function is probably okay but doing
1970 * so makes reset sequence different from the original
1971 * ->phy_reset implementation and Jeff nervous. :-P
1973 extern void ata_std_probeinit(struct ata_port *ap)
1975 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
1976 sata_phy_resume(ap);
1977 if (sata_dev_present(ap))
1978 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1983 * ata_std_softreset - reset host port via ATA SRST
1984 * @ap: port to reset
1985 * @verbose: fail verbosely
1986 * @classes: resulting classes of attached devices
1988 * Reset host port using ATA SRST. This function is to be used
1989 * as standard callback for ata_drive_*_reset() functions.
1992 * Kernel thread context (may sleep)
1995 * 0 on success, -errno otherwise.
1997 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
1999 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2000 unsigned int devmask = 0, err_mask;
2005 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2006 classes[0] = ATA_DEV_NONE;
2010 /* determine if device 0/1 are present */
2011 if (ata_devchk(ap, 0))
2012 devmask |= (1 << 0);
2013 if (slave_possible && ata_devchk(ap, 1))
2014 devmask |= (1 << 1);
2016 /* select device 0 again */
2017 ap->ops->dev_select(ap, 0);
2019 /* issue bus reset */
2020 DPRINTK("about to softreset, devmask=%x\n", devmask);
2021 err_mask = ata_bus_softreset(ap, devmask);
2024 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2027 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2032 /* determine by signature whether we have ATA or ATAPI devices */
2033 classes[0] = ata_dev_try_classify(ap, 0, &err);
2034 if (slave_possible && err != 0x81)
2035 classes[1] = ata_dev_try_classify(ap, 1, &err);
2038 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2043 * sata_std_hardreset - reset host port via SATA phy reset
2044 * @ap: port to reset
2045 * @verbose: fail verbosely
2046 * @class: resulting class of attached device
2048 * SATA phy-reset host port using DET bits of SControl register.
2049 * This function is to be used as standard callback for
2050 * ata_drive_*_reset().
2053 * Kernel thread context (may sleep)
2056 * 0 on success, -errno otherwise.
2058 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2062 /* Issue phy wake/reset */
2063 scr_write_flush(ap, SCR_CONTROL, 0x301);
2066 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2067 * 10.4.2 says at least 1 ms.
2071 /* Bring phy back */
2072 sata_phy_resume(ap);
2074 /* TODO: phy layer with polling, timeouts, etc. */
2075 if (!sata_dev_present(ap)) {
2076 *class = ATA_DEV_NONE;
2077 DPRINTK("EXIT, link offline\n");
2081 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2083 printk(KERN_ERR "ata%u: COMRESET failed "
2084 "(device not ready)\n", ap->id);
2086 DPRINTK("EXIT, device not ready\n");
2090 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2092 *class = ata_dev_try_classify(ap, 0, NULL);
2094 DPRINTK("EXIT, class=%u\n", *class);
2099 * ata_std_postreset - standard postreset callback
2100 * @ap: the target ata_port
2101 * @classes: classes of attached devices
2103 * This function is invoked after a successful reset. Note that
2104 * the device might have been reset more than once using
2105 * different reset methods before postreset is invoked.
2106 * postreset is also reponsible for setting cable type.
2108 * This function is to be used as standard callback for
2109 * ata_drive_*_reset().
2112 * Kernel thread context (may sleep)
2114 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2118 /* set cable type */
2119 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2120 ap->cbl = ATA_CBL_SATA;
2122 /* print link status */
2123 if (ap->cbl == ATA_CBL_SATA)
2124 sata_print_link_status(ap);
2126 /* re-enable interrupts */
2127 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2130 /* is double-select really necessary? */
2131 if (classes[0] != ATA_DEV_NONE)
2132 ap->ops->dev_select(ap, 1);
2133 if (classes[1] != ATA_DEV_NONE)
2134 ap->ops->dev_select(ap, 0);
2136 /* bail out if no device is present */
2137 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2138 DPRINTK("EXIT, no device\n");
2142 /* set up device control */
2143 if (ap->ioaddr.ctl_addr) {
2144 if (ap->flags & ATA_FLAG_MMIO)
2145 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2147 outb(ap->ctl, ap->ioaddr.ctl_addr);
2154 * ata_std_probe_reset - standard probe reset method
2155 * @ap: prot to perform probe-reset
2156 * @classes: resulting classes of attached devices
2158 * The stock off-the-shelf ->probe_reset method.
2161 * Kernel thread context (may sleep)
2164 * 0 on success, -errno otherwise.
2166 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2168 ata_reset_fn_t hardreset;
2171 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2172 hardreset = sata_std_hardreset;
2174 return ata_drive_probe_reset(ap, ata_std_probeinit,
2175 ata_std_softreset, hardreset,
2176 ata_std_postreset, classes);
2179 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2180 ata_postreset_fn_t postreset,
2181 unsigned int *classes)
2185 for (i = 0; i < ATA_MAX_DEVICES; i++)
2186 classes[i] = ATA_DEV_UNKNOWN;
2188 rc = reset(ap, 0, classes);
2192 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2193 * is complete and convert all ATA_DEV_UNKNOWN to
2196 for (i = 0; i < ATA_MAX_DEVICES; i++)
2197 if (classes[i] != ATA_DEV_UNKNOWN)
2200 if (i < ATA_MAX_DEVICES)
2201 for (i = 0; i < ATA_MAX_DEVICES; i++)
2202 if (classes[i] == ATA_DEV_UNKNOWN)
2203 classes[i] = ATA_DEV_NONE;
2206 postreset(ap, classes);
2208 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2212 * ata_drive_probe_reset - Perform probe reset with given methods
2213 * @ap: port to reset
2214 * @probeinit: probeinit method (can be NULL)
2215 * @softreset: softreset method (can be NULL)
2216 * @hardreset: hardreset method (can be NULL)
2217 * @postreset: postreset method (can be NULL)
2218 * @classes: resulting classes of attached devices
2220 * Reset the specified port and classify attached devices using
2221 * given methods. This function prefers softreset but tries all
2222 * possible reset sequences to reset and classify devices. This
2223 * function is intended to be used for constructing ->probe_reset
2224 * callback by low level drivers.
2226 * Reset methods should follow the following rules.
2228 * - Return 0 on sucess, -errno on failure.
2229 * - If classification is supported, fill classes[] with
2230 * recognized class codes.
2231 * - If classification is not supported, leave classes[] alone.
2232 * - If verbose is non-zero, print error message on failure;
2233 * otherwise, shut up.
2236 * Kernel thread context (may sleep)
2239 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2240 * if classification fails, and any error code from reset
2243 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2244 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2245 ata_postreset_fn_t postreset, unsigned int *classes)
2253 rc = do_probe_reset(ap, softreset, postreset, classes);
2261 rc = do_probe_reset(ap, hardreset, postreset, classes);
2262 if (rc == 0 || rc != -ENODEV)
2266 rc = do_probe_reset(ap, softreset, postreset, classes);
2271 static void ata_pr_blacklisted(const struct ata_port *ap,
2272 const struct ata_device *dev)
2274 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2275 ap->id, dev->devno);
2278 static const char * const ata_dma_blacklist [] = {
2297 "Toshiba CD-ROM XM-6202B",
2298 "TOSHIBA CD-ROM XM-1702BC",
2300 "E-IDE CD-ROM CR-840",
2303 "SAMSUNG CD-ROM SC-148C",
2304 "SAMSUNG CD-ROM SC",
2306 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2310 static int ata_dma_blacklisted(const struct ata_device *dev)
2312 unsigned char model_num[41];
2315 ata_dev_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS,
2318 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2319 if (!strcmp(ata_dma_blacklist[i], model_num))
2325 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2327 const struct ata_device *master, *slave;
2330 master = &ap->device[0];
2331 slave = &ap->device[1];
2333 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2335 if (shift == ATA_SHIFT_UDMA) {
2336 mask = ap->udma_mask;
2337 if (ata_dev_present(master)) {
2338 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2339 if (ata_dma_blacklisted(master)) {
2341 ata_pr_blacklisted(ap, master);
2344 if (ata_dev_present(slave)) {
2345 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2346 if (ata_dma_blacklisted(slave)) {
2348 ata_pr_blacklisted(ap, slave);
2352 else if (shift == ATA_SHIFT_MWDMA) {
2353 mask = ap->mwdma_mask;
2354 if (ata_dev_present(master)) {
2355 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2356 if (ata_dma_blacklisted(master)) {
2358 ata_pr_blacklisted(ap, master);
2361 if (ata_dev_present(slave)) {
2362 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2363 if (ata_dma_blacklisted(slave)) {
2365 ata_pr_blacklisted(ap, slave);
2369 else if (shift == ATA_SHIFT_PIO) {
2370 mask = ap->pio_mask;
2371 if (ata_dev_present(master)) {
2372 /* spec doesn't return explicit support for
2373 * PIO0-2, so we fake it
2375 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2380 if (ata_dev_present(slave)) {
2381 /* spec doesn't return explicit support for
2382 * PIO0-2, so we fake it
2384 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2391 mask = 0xffffffff; /* shut up compiler warning */
2398 /* find greatest bit */
2399 static int fgb(u32 bitmap)
2404 for (i = 0; i < 32; i++)
2405 if (bitmap & (1 << i))
2412 * ata_choose_xfer_mode - attempt to find best transfer mode
2413 * @ap: Port for which an xfer mode will be selected
2414 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2415 * @xfer_shift_out: (output) bit shift that selects this mode
2417 * Based on host and device capabilities, determine the
2418 * maximum transfer mode that is amenable to all.
2421 * PCI/etc. bus probe sem.
2424 * Zero on success, negative on error.
2427 static int ata_choose_xfer_mode(const struct ata_port *ap,
2429 unsigned int *xfer_shift_out)
2431 unsigned int mask, shift;
2434 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2435 shift = xfer_mode_classes[i].shift;
2436 mask = ata_get_mode_mask(ap, shift);
2440 *xfer_mode_out = xfer_mode_classes[i].base + x;
2441 *xfer_shift_out = shift;
2450 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2451 * @ap: Port associated with device @dev
2452 * @dev: Device to which command will be sent
2454 * Issue SET FEATURES - XFER MODE command to device @dev
2458 * PCI/etc. bus probe sem.
2461 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2463 struct ata_taskfile tf;
2465 /* set up set-features taskfile */
2466 DPRINTK("set features - xfer mode\n");
2468 ata_tf_init(ap, &tf, dev->devno);
2469 tf.command = ATA_CMD_SET_FEATURES;
2470 tf.feature = SETFEATURES_XFER;
2471 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2472 tf.protocol = ATA_PROT_NODATA;
2473 tf.nsect = dev->xfer_mode;
2475 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2476 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2478 ata_port_disable(ap);
2485 * ata_dev_reread_id - Reread the device identify device info
2486 * @ap: port where the device is
2487 * @dev: device to reread the identify device info
2492 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2494 struct ata_taskfile tf;
2496 ata_tf_init(ap, &tf, dev->devno);
2498 if (dev->class == ATA_DEV_ATA) {
2499 tf.command = ATA_CMD_ID_ATA;
2500 DPRINTK("do ATA identify\n");
2502 tf.command = ATA_CMD_ID_ATAPI;
2503 DPRINTK("do ATAPI identify\n");
2506 tf.flags |= ATA_TFLAG_DEVICE;
2507 tf.protocol = ATA_PROT_PIO;
2509 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2510 dev->id, sizeof(dev->id)))
2513 swap_buf_le16(dev->id, ATA_ID_WORDS);
2515 ata_dump_id(dev->id);
2521 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
2522 ata_port_disable(ap);
2526 * ata_dev_init_params - Issue INIT DEV PARAMS command
2527 * @ap: Port associated with device @dev
2528 * @dev: Device to which command will be sent
2533 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2535 struct ata_taskfile tf;
2536 u16 sectors = dev->id[6];
2537 u16 heads = dev->id[3];
2539 /* Number of sectors per track 1-255. Number of heads 1-16 */
2540 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2543 /* set up init dev params taskfile */
2544 DPRINTK("init dev params \n");
2546 ata_tf_init(ap, &tf, dev->devno);
2547 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2548 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2549 tf.protocol = ATA_PROT_NODATA;
2551 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2553 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2554 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2556 ata_port_disable(ap);
2563 * ata_sg_clean - Unmap DMA memory associated with command
2564 * @qc: Command containing DMA memory to be released
2566 * Unmap all mapped DMA memory associated with this command.
2569 * spin_lock_irqsave(host_set lock)
2572 static void ata_sg_clean(struct ata_queued_cmd *qc)
2574 struct ata_port *ap = qc->ap;
2575 struct scatterlist *sg = qc->__sg;
2576 int dir = qc->dma_dir;
2577 void *pad_buf = NULL;
2579 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2580 WARN_ON(sg == NULL);
2582 if (qc->flags & ATA_QCFLAG_SINGLE)
2583 WARN_ON(qc->n_elem != 1);
2585 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2587 /* if we padded the buffer out to 32-bit bound, and data
2588 * xfer direction is from-device, we must copy from the
2589 * pad buffer back into the supplied buffer
2591 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2592 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2594 if (qc->flags & ATA_QCFLAG_SG) {
2596 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2597 /* restore last sg */
2598 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2600 struct scatterlist *psg = &qc->pad_sgent;
2601 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2602 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2603 kunmap_atomic(addr, KM_IRQ0);
2606 if (sg_dma_len(&sg[0]) > 0)
2607 dma_unmap_single(ap->host_set->dev,
2608 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2611 sg->length += qc->pad_len;
2613 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2614 pad_buf, qc->pad_len);
2617 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2622 * ata_fill_sg - Fill PCI IDE PRD table
2623 * @qc: Metadata associated with taskfile to be transferred
2625 * Fill PCI IDE PRD (scatter-gather) table with segments
2626 * associated with the current disk command.
2629 * spin_lock_irqsave(host_set lock)
2632 static void ata_fill_sg(struct ata_queued_cmd *qc)
2634 struct ata_port *ap = qc->ap;
2635 struct scatterlist *sg;
2638 WARN_ON(qc->__sg == NULL);
2639 WARN_ON(qc->n_elem == 0);
2642 ata_for_each_sg(sg, qc) {
2646 /* determine if physical DMA addr spans 64K boundary.
2647 * Note h/w doesn't support 64-bit, so we unconditionally
2648 * truncate dma_addr_t to u32.
2650 addr = (u32) sg_dma_address(sg);
2651 sg_len = sg_dma_len(sg);
2654 offset = addr & 0xffff;
2656 if ((offset + sg_len) > 0x10000)
2657 len = 0x10000 - offset;
2659 ap->prd[idx].addr = cpu_to_le32(addr);
2660 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2661 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2670 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2673 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2674 * @qc: Metadata associated with taskfile to check
2676 * Allow low-level driver to filter ATA PACKET commands, returning
2677 * a status indicating whether or not it is OK to use DMA for the
2678 * supplied PACKET command.
2681 * spin_lock_irqsave(host_set lock)
2683 * RETURNS: 0 when ATAPI DMA can be used
2686 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2688 struct ata_port *ap = qc->ap;
2689 int rc = 0; /* Assume ATAPI DMA is OK by default */
2691 if (ap->ops->check_atapi_dma)
2692 rc = ap->ops->check_atapi_dma(qc);
2697 * ata_qc_prep - Prepare taskfile for submission
2698 * @qc: Metadata associated with taskfile to be prepared
2700 * Prepare ATA taskfile for submission.
2703 * spin_lock_irqsave(host_set lock)
2705 void ata_qc_prep(struct ata_queued_cmd *qc)
2707 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2714 * ata_sg_init_one - Associate command with memory buffer
2715 * @qc: Command to be associated
2716 * @buf: Memory buffer
2717 * @buflen: Length of memory buffer, in bytes.
2719 * Initialize the data-related elements of queued_cmd @qc
2720 * to point to a single memory buffer, @buf of byte length @buflen.
2723 * spin_lock_irqsave(host_set lock)
2726 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2728 struct scatterlist *sg;
2730 qc->flags |= ATA_QCFLAG_SINGLE;
2732 memset(&qc->sgent, 0, sizeof(qc->sgent));
2733 qc->__sg = &qc->sgent;
2735 qc->orig_n_elem = 1;
2739 sg_init_one(sg, buf, buflen);
2743 * ata_sg_init - Associate command with scatter-gather table.
2744 * @qc: Command to be associated
2745 * @sg: Scatter-gather table.
2746 * @n_elem: Number of elements in s/g table.
2748 * Initialize the data-related elements of queued_cmd @qc
2749 * to point to a scatter-gather table @sg, containing @n_elem
2753 * spin_lock_irqsave(host_set lock)
2756 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2757 unsigned int n_elem)
2759 qc->flags |= ATA_QCFLAG_SG;
2761 qc->n_elem = n_elem;
2762 qc->orig_n_elem = n_elem;
2766 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2767 * @qc: Command with memory buffer to be mapped.
2769 * DMA-map the memory buffer associated with queued_cmd @qc.
2772 * spin_lock_irqsave(host_set lock)
2775 * Zero on success, negative on error.
2778 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2780 struct ata_port *ap = qc->ap;
2781 int dir = qc->dma_dir;
2782 struct scatterlist *sg = qc->__sg;
2783 dma_addr_t dma_address;
2785 /* we must lengthen transfers to end on a 32-bit boundary */
2786 qc->pad_len = sg->length & 3;
2788 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2789 struct scatterlist *psg = &qc->pad_sgent;
2791 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2793 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2795 if (qc->tf.flags & ATA_TFLAG_WRITE)
2796 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2799 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2800 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2802 sg->length -= qc->pad_len;
2804 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2805 sg->length, qc->pad_len);
2809 sg_dma_address(sg) = 0;
2813 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2815 if (dma_mapping_error(dma_address)) {
2817 sg->length += qc->pad_len;
2821 sg_dma_address(sg) = dma_address;
2823 sg_dma_len(sg) = sg->length;
2825 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2826 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2832 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2833 * @qc: Command with scatter-gather table to be mapped.
2835 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2838 * spin_lock_irqsave(host_set lock)
2841 * Zero on success, negative on error.
2845 static int ata_sg_setup(struct ata_queued_cmd *qc)
2847 struct ata_port *ap = qc->ap;
2848 struct scatterlist *sg = qc->__sg;
2849 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2850 int n_elem, pre_n_elem, dir, trim_sg = 0;
2852 VPRINTK("ENTER, ata%u\n", ap->id);
2853 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2855 /* we must lengthen transfers to end on a 32-bit boundary */
2856 qc->pad_len = lsg->length & 3;
2858 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2859 struct scatterlist *psg = &qc->pad_sgent;
2860 unsigned int offset;
2862 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2864 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2867 * psg->page/offset are used to copy to-be-written
2868 * data in this function or read data in ata_sg_clean.
2870 offset = lsg->offset + lsg->length - qc->pad_len;
2871 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2872 psg->offset = offset_in_page(offset);
2874 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2875 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2876 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2877 kunmap_atomic(addr, KM_IRQ0);
2880 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2881 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2883 lsg->length -= qc->pad_len;
2884 if (lsg->length == 0)
2887 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2888 qc->n_elem - 1, lsg->length, qc->pad_len);
2891 pre_n_elem = qc->n_elem;
2892 if (trim_sg && pre_n_elem)
2901 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2903 /* restore last sg */
2904 lsg->length += qc->pad_len;
2908 DPRINTK("%d sg elements mapped\n", n_elem);
2911 qc->n_elem = n_elem;
2917 * ata_poll_qc_complete - turn irq back on and finish qc
2918 * @qc: Command to complete
2919 * @err_mask: ATA status register content
2922 * None. (grabs host lock)
2925 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2927 struct ata_port *ap = qc->ap;
2928 unsigned long flags;
2930 spin_lock_irqsave(&ap->host_set->lock, flags);
2931 ap->flags &= ~ATA_FLAG_NOINTR;
2933 ata_qc_complete(qc);
2934 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2938 * ata_pio_poll - poll using PIO, depending on current state
2939 * @ap: the target ata_port
2942 * None. (executing in kernel thread context)
2945 * timeout value to use
2948 static unsigned long ata_pio_poll(struct ata_port *ap)
2950 struct ata_queued_cmd *qc;
2952 unsigned int poll_state = HSM_ST_UNKNOWN;
2953 unsigned int reg_state = HSM_ST_UNKNOWN;
2955 qc = ata_qc_from_tag(ap, ap->active_tag);
2956 WARN_ON(qc == NULL);
2958 switch (ap->hsm_task_state) {
2961 poll_state = HSM_ST_POLL;
2965 case HSM_ST_LAST_POLL:
2966 poll_state = HSM_ST_LAST_POLL;
2967 reg_state = HSM_ST_LAST;
2974 status = ata_chk_status(ap);
2975 if (status & ATA_BUSY) {
2976 if (time_after(jiffies, ap->pio_task_timeout)) {
2977 qc->err_mask |= AC_ERR_TIMEOUT;
2978 ap->hsm_task_state = HSM_ST_TMOUT;
2981 ap->hsm_task_state = poll_state;
2982 return ATA_SHORT_PAUSE;
2985 ap->hsm_task_state = reg_state;
2990 * ata_pio_complete - check if drive is busy or idle
2991 * @ap: the target ata_port
2994 * None. (executing in kernel thread context)
2997 * Non-zero if qc completed, zero otherwise.
3000 static int ata_pio_complete (struct ata_port *ap)
3002 struct ata_queued_cmd *qc;
3006 * This is purely heuristic. This is a fast path. Sometimes when
3007 * we enter, BSY will be cleared in a chk-status or two. If not,
3008 * the drive is probably seeking or something. Snooze for a couple
3009 * msecs, then chk-status again. If still busy, fall back to
3010 * HSM_ST_POLL state.
3012 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3013 if (drv_stat & ATA_BUSY) {
3015 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3016 if (drv_stat & ATA_BUSY) {
3017 ap->hsm_task_state = HSM_ST_LAST_POLL;
3018 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3023 qc = ata_qc_from_tag(ap, ap->active_tag);
3024 WARN_ON(qc == NULL);
3026 drv_stat = ata_wait_idle(ap);
3027 if (!ata_ok(drv_stat)) {
3028 qc->err_mask |= __ac_err_mask(drv_stat);
3029 ap->hsm_task_state = HSM_ST_ERR;
3033 ap->hsm_task_state = HSM_ST_IDLE;
3035 WARN_ON(qc->err_mask);
3036 ata_poll_qc_complete(qc);
3038 /* another command may start at this point */
3045 * swap_buf_le16 - swap halves of 16-bit words in place
3046 * @buf: Buffer to swap
3047 * @buf_words: Number of 16-bit words in buffer.
3049 * Swap halves of 16-bit words if needed to convert from
3050 * little-endian byte order to native cpu byte order, or
3054 * Inherited from caller.
3056 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3061 for (i = 0; i < buf_words; i++)
3062 buf[i] = le16_to_cpu(buf[i]);
3063 #endif /* __BIG_ENDIAN */
3067 * ata_mmio_data_xfer - Transfer data by MMIO
3068 * @ap: port to read/write
3070 * @buflen: buffer length
3071 * @write_data: read/write
3073 * Transfer data from/to the device data register by MMIO.
3076 * Inherited from caller.
3079 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3080 unsigned int buflen, int write_data)
3083 unsigned int words = buflen >> 1;
3084 u16 *buf16 = (u16 *) buf;
3085 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3087 /* Transfer multiple of 2 bytes */
3089 for (i = 0; i < words; i++)
3090 writew(le16_to_cpu(buf16[i]), mmio);
3092 for (i = 0; i < words; i++)
3093 buf16[i] = cpu_to_le16(readw(mmio));
3096 /* Transfer trailing 1 byte, if any. */
3097 if (unlikely(buflen & 0x01)) {
3098 u16 align_buf[1] = { 0 };
3099 unsigned char *trailing_buf = buf + buflen - 1;
3102 memcpy(align_buf, trailing_buf, 1);
3103 writew(le16_to_cpu(align_buf[0]), mmio);
3105 align_buf[0] = cpu_to_le16(readw(mmio));
3106 memcpy(trailing_buf, align_buf, 1);
3112 * ata_pio_data_xfer - Transfer data by PIO
3113 * @ap: port to read/write
3115 * @buflen: buffer length
3116 * @write_data: read/write
3118 * Transfer data from/to the device data register by PIO.
3121 * Inherited from caller.
3124 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3125 unsigned int buflen, int write_data)
3127 unsigned int words = buflen >> 1;
3129 /* Transfer multiple of 2 bytes */
3131 outsw(ap->ioaddr.data_addr, buf, words);
3133 insw(ap->ioaddr.data_addr, buf, words);
3135 /* Transfer trailing 1 byte, if any. */
3136 if (unlikely(buflen & 0x01)) {
3137 u16 align_buf[1] = { 0 };
3138 unsigned char *trailing_buf = buf + buflen - 1;
3141 memcpy(align_buf, trailing_buf, 1);
3142 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3144 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3145 memcpy(trailing_buf, align_buf, 1);
3151 * ata_data_xfer - Transfer data from/to the data register.
3152 * @ap: port to read/write
3154 * @buflen: buffer length
3155 * @do_write: read/write
3157 * Transfer data from/to the device data register.
3160 * Inherited from caller.
3163 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3164 unsigned int buflen, int do_write)
3166 /* Make the crap hardware pay the costs not the good stuff */
3167 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3168 unsigned long flags;
3169 local_irq_save(flags);
3170 if (ap->flags & ATA_FLAG_MMIO)
3171 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3173 ata_pio_data_xfer(ap, buf, buflen, do_write);
3174 local_irq_restore(flags);
3176 if (ap->flags & ATA_FLAG_MMIO)
3177 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3179 ata_pio_data_xfer(ap, buf, buflen, do_write);
3184 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3185 * @qc: Command on going
3187 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3190 * Inherited from caller.
3193 static void ata_pio_sector(struct ata_queued_cmd *qc)
3195 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3196 struct scatterlist *sg = qc->__sg;
3197 struct ata_port *ap = qc->ap;
3199 unsigned int offset;
3202 if (qc->cursect == (qc->nsect - 1))
3203 ap->hsm_task_state = HSM_ST_LAST;
3205 page = sg[qc->cursg].page;
3206 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3208 /* get the current page and offset */
3209 page = nth_page(page, (offset >> PAGE_SHIFT));
3210 offset %= PAGE_SIZE;
3212 buf = kmap(page) + offset;
3217 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3222 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3224 /* do the actual data transfer */
3225 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3226 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3232 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3233 * @qc: Command on going
3234 * @bytes: number of bytes
3236 * Transfer Transfer data from/to the ATAPI device.
3239 * Inherited from caller.
3243 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3245 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3246 struct scatterlist *sg = qc->__sg;
3247 struct ata_port *ap = qc->ap;
3250 unsigned int offset, count;
3252 if (qc->curbytes + bytes >= qc->nbytes)
3253 ap->hsm_task_state = HSM_ST_LAST;
3256 if (unlikely(qc->cursg >= qc->n_elem)) {
3258 * The end of qc->sg is reached and the device expects
3259 * more data to transfer. In order not to overrun qc->sg
3260 * and fulfill length specified in the byte count register,
3261 * - for read case, discard trailing data from the device
3262 * - for write case, padding zero data to the device
3264 u16 pad_buf[1] = { 0 };
3265 unsigned int words = bytes >> 1;
3268 if (words) /* warning if bytes > 1 */
3269 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3272 for (i = 0; i < words; i++)
3273 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3275 ap->hsm_task_state = HSM_ST_LAST;
3279 sg = &qc->__sg[qc->cursg];
3282 offset = sg->offset + qc->cursg_ofs;
3284 /* get the current page and offset */
3285 page = nth_page(page, (offset >> PAGE_SHIFT));
3286 offset %= PAGE_SIZE;
3288 /* don't overrun current sg */
3289 count = min(sg->length - qc->cursg_ofs, bytes);
3291 /* don't cross page boundaries */
3292 count = min(count, (unsigned int)PAGE_SIZE - offset);
3294 buf = kmap(page) + offset;
3297 qc->curbytes += count;
3298 qc->cursg_ofs += count;
3300 if (qc->cursg_ofs == sg->length) {
3305 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3307 /* do the actual data transfer */
3308 ata_data_xfer(ap, buf, count, do_write);
3317 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3318 * @qc: Command on going
3320 * Transfer Transfer data from/to the ATAPI device.
3323 * Inherited from caller.
3326 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3328 struct ata_port *ap = qc->ap;
3329 struct ata_device *dev = qc->dev;
3330 unsigned int ireason, bc_lo, bc_hi, bytes;
3331 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3333 ap->ops->tf_read(ap, &qc->tf);
3334 ireason = qc->tf.nsect;
3335 bc_lo = qc->tf.lbam;
3336 bc_hi = qc->tf.lbah;
3337 bytes = (bc_hi << 8) | bc_lo;
3339 /* shall be cleared to zero, indicating xfer of data */
3340 if (ireason & (1 << 0))
3343 /* make sure transfer direction matches expected */
3344 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3345 if (do_write != i_write)
3348 __atapi_pio_bytes(qc, bytes);
3353 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3354 ap->id, dev->devno);
3355 qc->err_mask |= AC_ERR_HSM;
3356 ap->hsm_task_state = HSM_ST_ERR;
3360 * ata_pio_block - start PIO on a block
3361 * @ap: the target ata_port
3364 * None. (executing in kernel thread context)
3367 static void ata_pio_block(struct ata_port *ap)
3369 struct ata_queued_cmd *qc;
3373 * This is purely heuristic. This is a fast path.
3374 * Sometimes when we enter, BSY will be cleared in
3375 * a chk-status or two. If not, the drive is probably seeking
3376 * or something. Snooze for a couple msecs, then
3377 * chk-status again. If still busy, fall back to
3378 * HSM_ST_POLL state.
3380 status = ata_busy_wait(ap, ATA_BUSY, 5);
3381 if (status & ATA_BUSY) {
3383 status = ata_busy_wait(ap, ATA_BUSY, 10);
3384 if (status & ATA_BUSY) {
3385 ap->hsm_task_state = HSM_ST_POLL;
3386 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3391 qc = ata_qc_from_tag(ap, ap->active_tag);
3392 WARN_ON(qc == NULL);
3395 if (status & (ATA_ERR | ATA_DF)) {
3396 qc->err_mask |= AC_ERR_DEV;
3397 ap->hsm_task_state = HSM_ST_ERR;
3401 /* transfer data if any */
3402 if (is_atapi_taskfile(&qc->tf)) {
3403 /* DRQ=0 means no more data to transfer */
3404 if ((status & ATA_DRQ) == 0) {
3405 ap->hsm_task_state = HSM_ST_LAST;
3409 atapi_pio_bytes(qc);
3411 /* handle BSY=0, DRQ=0 as error */
3412 if ((status & ATA_DRQ) == 0) {
3413 qc->err_mask |= AC_ERR_HSM;
3414 ap->hsm_task_state = HSM_ST_ERR;
3422 static void ata_pio_error(struct ata_port *ap)
3424 struct ata_queued_cmd *qc;
3426 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3428 qc = ata_qc_from_tag(ap, ap->active_tag);
3429 WARN_ON(qc == NULL);
3431 /* make sure qc->err_mask is available to
3432 * know what's wrong and recover
3434 WARN_ON(qc->err_mask == 0);
3436 ap->hsm_task_state = HSM_ST_IDLE;
3438 ata_poll_qc_complete(qc);
3441 static void ata_pio_task(void *_data)
3443 struct ata_port *ap = _data;
3444 unsigned long timeout;
3451 switch (ap->hsm_task_state) {
3460 qc_completed = ata_pio_complete(ap);
3464 case HSM_ST_LAST_POLL:
3465 timeout = ata_pio_poll(ap);
3475 ata_queue_delayed_pio_task(ap, timeout);
3476 else if (!qc_completed)
3481 * ata_qc_timeout - Handle timeout of queued command
3482 * @qc: Command that timed out
3484 * Some part of the kernel (currently, only the SCSI layer)
3485 * has noticed that the active command on port @ap has not
3486 * completed after a specified length of time. Handle this
3487 * condition by disabling DMA (if necessary) and completing
3488 * transactions, with error if necessary.
3490 * This also handles the case of the "lost interrupt", where
3491 * for some reason (possibly hardware bug, possibly driver bug)
3492 * an interrupt was not delivered to the driver, even though the
3493 * transaction completed successfully.
3496 * Inherited from SCSI layer (none, can sleep)
3499 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3501 struct ata_port *ap = qc->ap;
3502 struct ata_host_set *host_set = ap->host_set;
3503 u8 host_stat = 0, drv_stat;
3504 unsigned long flags;
3508 ata_flush_pio_tasks(ap);
3509 ap->hsm_task_state = HSM_ST_IDLE;
3511 spin_lock_irqsave(&host_set->lock, flags);
3513 switch (qc->tf.protocol) {
3516 case ATA_PROT_ATAPI_DMA:
3517 host_stat = ap->ops->bmdma_status(ap);
3519 /* before we do anything else, clear DMA-Start bit */
3520 ap->ops->bmdma_stop(qc);
3526 drv_stat = ata_chk_status(ap);
3528 /* ack bmdma irq events */
3529 ap->ops->irq_clear(ap);
3531 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3532 ap->id, qc->tf.command, drv_stat, host_stat);
3534 /* complete taskfile transaction */
3535 qc->err_mask |= ac_err_mask(drv_stat);
3539 spin_unlock_irqrestore(&host_set->lock, flags);
3541 ata_eh_qc_complete(qc);
3547 * ata_eng_timeout - Handle timeout of queued command
3548 * @ap: Port on which timed-out command is active
3550 * Some part of the kernel (currently, only the SCSI layer)
3551 * has noticed that the active command on port @ap has not
3552 * completed after a specified length of time. Handle this
3553 * condition by disabling DMA (if necessary) and completing
3554 * transactions, with error if necessary.
3556 * This also handles the case of the "lost interrupt", where
3557 * for some reason (possibly hardware bug, possibly driver bug)
3558 * an interrupt was not delivered to the driver, even though the
3559 * transaction completed successfully.
3562 * Inherited from SCSI layer (none, can sleep)
3565 void ata_eng_timeout(struct ata_port *ap)
3569 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3575 * ata_qc_new - Request an available ATA command, for queueing
3576 * @ap: Port associated with device @dev
3577 * @dev: Device from whom we request an available command structure
3583 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3585 struct ata_queued_cmd *qc = NULL;
3588 for (i = 0; i < ATA_MAX_QUEUE; i++)
3589 if (!test_and_set_bit(i, &ap->qactive)) {
3590 qc = ata_qc_from_tag(ap, i);
3601 * ata_qc_new_init - Request an available ATA command, and initialize it
3602 * @ap: Port associated with device @dev
3603 * @dev: Device from whom we request an available command structure
3609 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3610 struct ata_device *dev)
3612 struct ata_queued_cmd *qc;
3614 qc = ata_qc_new(ap);
3627 * ata_qc_free - free unused ata_queued_cmd
3628 * @qc: Command to complete
3630 * Designed to free unused ata_queued_cmd object
3631 * in case something prevents using it.
3634 * spin_lock_irqsave(host_set lock)
3636 void ata_qc_free(struct ata_queued_cmd *qc)
3638 struct ata_port *ap = qc->ap;
3641 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3645 if (likely(ata_tag_valid(tag))) {
3646 if (tag == ap->active_tag)
3647 ap->active_tag = ATA_TAG_POISON;
3648 qc->tag = ATA_TAG_POISON;
3649 clear_bit(tag, &ap->qactive);
3653 void __ata_qc_complete(struct ata_queued_cmd *qc)
3655 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3656 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3658 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3661 /* atapi: mark qc as inactive to prevent the interrupt handler
3662 * from completing the command twice later, before the error handler
3663 * is called. (when rc != 0 and atapi request sense is needed)
3665 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3667 /* call completion callback */
3668 qc->complete_fn(qc);
3671 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3673 struct ata_port *ap = qc->ap;
3675 switch (qc->tf.protocol) {
3677 case ATA_PROT_ATAPI_DMA:
3680 case ATA_PROT_ATAPI:
3682 case ATA_PROT_PIO_MULT:
3683 if (ap->flags & ATA_FLAG_PIO_DMA)
3696 * ata_qc_issue - issue taskfile to device
3697 * @qc: command to issue to device
3699 * Prepare an ATA command to submission to device.
3700 * This includes mapping the data into a DMA-able
3701 * area, filling in the S/G table, and finally
3702 * writing the taskfile to hardware, starting the command.
3705 * spin_lock_irqsave(host_set lock)
3708 * Zero on success, AC_ERR_* mask on failure
3711 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3713 struct ata_port *ap = qc->ap;
3715 if (ata_should_dma_map(qc)) {
3716 if (qc->flags & ATA_QCFLAG_SG) {
3717 if (ata_sg_setup(qc))
3719 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3720 if (ata_sg_setup_one(qc))
3724 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3727 ap->ops->qc_prep(qc);
3729 qc->ap->active_tag = qc->tag;
3730 qc->flags |= ATA_QCFLAG_ACTIVE;
3732 return ap->ops->qc_issue(qc);
3735 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3736 return AC_ERR_SYSTEM;
3741 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3742 * @qc: command to issue to device
3744 * Using various libata functions and hooks, this function
3745 * starts an ATA command. ATA commands are grouped into
3746 * classes called "protocols", and issuing each type of protocol
3747 * is slightly different.
3749 * May be used as the qc_issue() entry in ata_port_operations.
3752 * spin_lock_irqsave(host_set lock)
3755 * Zero on success, AC_ERR_* mask on failure
3758 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3760 struct ata_port *ap = qc->ap;
3762 ata_dev_select(ap, qc->dev->devno, 1, 0);
3764 switch (qc->tf.protocol) {
3765 case ATA_PROT_NODATA:
3766 ata_tf_to_host(ap, &qc->tf);
3770 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3771 ap->ops->bmdma_setup(qc); /* set up bmdma */
3772 ap->ops->bmdma_start(qc); /* initiate bmdma */
3775 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3776 ata_qc_set_polling(qc);
3777 ata_tf_to_host(ap, &qc->tf);
3778 ap->hsm_task_state = HSM_ST;
3779 ata_queue_pio_task(ap);
3782 case ATA_PROT_ATAPI:
3783 ata_qc_set_polling(qc);
3784 ata_tf_to_host(ap, &qc->tf);
3785 ata_queue_packet_task(ap);
3788 case ATA_PROT_ATAPI_NODATA:
3789 ap->flags |= ATA_FLAG_NOINTR;
3790 ata_tf_to_host(ap, &qc->tf);
3791 ata_queue_packet_task(ap);
3794 case ATA_PROT_ATAPI_DMA:
3795 ap->flags |= ATA_FLAG_NOINTR;
3796 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3797 ap->ops->bmdma_setup(qc); /* set up bmdma */
3798 ata_queue_packet_task(ap);
3803 return AC_ERR_SYSTEM;
3810 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3811 * @qc: Info associated with this ATA transaction.
3814 * spin_lock_irqsave(host_set lock)
3817 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3819 struct ata_port *ap = qc->ap;
3820 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3822 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3824 /* load PRD table addr. */
3825 mb(); /* make sure PRD table writes are visible to controller */
3826 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3828 /* specify data direction, triple-check start bit is clear */
3829 dmactl = readb(mmio + ATA_DMA_CMD);
3830 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3832 dmactl |= ATA_DMA_WR;
3833 writeb(dmactl, mmio + ATA_DMA_CMD);
3835 /* issue r/w command */
3836 ap->ops->exec_command(ap, &qc->tf);
3840 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3841 * @qc: Info associated with this ATA transaction.
3844 * spin_lock_irqsave(host_set lock)
3847 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3849 struct ata_port *ap = qc->ap;
3850 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3853 /* start host DMA transaction */
3854 dmactl = readb(mmio + ATA_DMA_CMD);
3855 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3857 /* Strictly, one may wish to issue a readb() here, to
3858 * flush the mmio write. However, control also passes
3859 * to the hardware at this point, and it will interrupt
3860 * us when we are to resume control. So, in effect,
3861 * we don't care when the mmio write flushes.
3862 * Further, a read of the DMA status register _immediately_
3863 * following the write may not be what certain flaky hardware
3864 * is expected, so I think it is best to not add a readb()
3865 * without first all the MMIO ATA cards/mobos.
3866 * Or maybe I'm just being paranoid.
3871 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3872 * @qc: Info associated with this ATA transaction.
3875 * spin_lock_irqsave(host_set lock)
3878 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3880 struct ata_port *ap = qc->ap;
3881 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3884 /* load PRD table addr. */
3885 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3887 /* specify data direction, triple-check start bit is clear */
3888 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3889 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3891 dmactl |= ATA_DMA_WR;
3892 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3894 /* issue r/w command */
3895 ap->ops->exec_command(ap, &qc->tf);
3899 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3900 * @qc: Info associated with this ATA transaction.
3903 * spin_lock_irqsave(host_set lock)
3906 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3908 struct ata_port *ap = qc->ap;
3911 /* start host DMA transaction */
3912 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3913 outb(dmactl | ATA_DMA_START,
3914 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3919 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3920 * @qc: Info associated with this ATA transaction.
3922 * Writes the ATA_DMA_START flag to the DMA command register.
3924 * May be used as the bmdma_start() entry in ata_port_operations.
3927 * spin_lock_irqsave(host_set lock)
3929 void ata_bmdma_start(struct ata_queued_cmd *qc)
3931 if (qc->ap->flags & ATA_FLAG_MMIO)
3932 ata_bmdma_start_mmio(qc);
3934 ata_bmdma_start_pio(qc);
3939 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3940 * @qc: Info associated with this ATA transaction.
3942 * Writes address of PRD table to device's PRD Table Address
3943 * register, sets the DMA control register, and calls
3944 * ops->exec_command() to start the transfer.
3946 * May be used as the bmdma_setup() entry in ata_port_operations.
3949 * spin_lock_irqsave(host_set lock)
3951 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3953 if (qc->ap->flags & ATA_FLAG_MMIO)
3954 ata_bmdma_setup_mmio(qc);
3956 ata_bmdma_setup_pio(qc);
3961 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3962 * @ap: Port associated with this ATA transaction.
3964 * Clear interrupt and error flags in DMA status register.
3966 * May be used as the irq_clear() entry in ata_port_operations.
3969 * spin_lock_irqsave(host_set lock)
3972 void ata_bmdma_irq_clear(struct ata_port *ap)
3974 if (ap->flags & ATA_FLAG_MMIO) {
3975 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3976 writeb(readb(mmio), mmio);
3978 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3979 outb(inb(addr), addr);
3986 * ata_bmdma_status - Read PCI IDE BMDMA status
3987 * @ap: Port associated with this ATA transaction.
3989 * Read and return BMDMA status register.
3991 * May be used as the bmdma_status() entry in ata_port_operations.
3994 * spin_lock_irqsave(host_set lock)
3997 u8 ata_bmdma_status(struct ata_port *ap)
4000 if (ap->flags & ATA_FLAG_MMIO) {
4001 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4002 host_stat = readb(mmio + ATA_DMA_STATUS);
4004 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4010 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4011 * @qc: Command we are ending DMA for
4013 * Clears the ATA_DMA_START flag in the dma control register
4015 * May be used as the bmdma_stop() entry in ata_port_operations.
4018 * spin_lock_irqsave(host_set lock)
4021 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4023 struct ata_port *ap = qc->ap;
4024 if (ap->flags & ATA_FLAG_MMIO) {
4025 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4027 /* clear start/stop bit */
4028 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4029 mmio + ATA_DMA_CMD);
4031 /* clear start/stop bit */
4032 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4033 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4036 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4037 ata_altstatus(ap); /* dummy read */
4041 * ata_host_intr - Handle host interrupt for given (port, task)
4042 * @ap: Port on which interrupt arrived (possibly...)
4043 * @qc: Taskfile currently active in engine
4045 * Handle host interrupt for given queued command. Currently,
4046 * only DMA interrupts are handled. All other commands are
4047 * handled via polling with interrupts disabled (nIEN bit).
4050 * spin_lock_irqsave(host_set lock)
4053 * One if interrupt was handled, zero if not (shared irq).
4056 inline unsigned int ata_host_intr (struct ata_port *ap,
4057 struct ata_queued_cmd *qc)
4059 u8 status, host_stat;
4061 switch (qc->tf.protocol) {
4064 case ATA_PROT_ATAPI_DMA:
4065 case ATA_PROT_ATAPI:
4066 /* check status of DMA engine */
4067 host_stat = ap->ops->bmdma_status(ap);
4068 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4070 /* if it's not our irq... */
4071 if (!(host_stat & ATA_DMA_INTR))
4074 /* before we do anything else, clear DMA-Start bit */
4075 ap->ops->bmdma_stop(qc);
4079 case ATA_PROT_ATAPI_NODATA:
4080 case ATA_PROT_NODATA:
4081 /* check altstatus */
4082 status = ata_altstatus(ap);
4083 if (status & ATA_BUSY)
4086 /* check main status, clearing INTRQ */
4087 status = ata_chk_status(ap);
4088 if (unlikely(status & ATA_BUSY))
4090 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4091 ap->id, qc->tf.protocol, status);
4093 /* ack bmdma irq events */
4094 ap->ops->irq_clear(ap);
4096 /* complete taskfile transaction */
4097 qc->err_mask |= ac_err_mask(status);
4098 ata_qc_complete(qc);
4105 return 1; /* irq handled */
4108 ap->stats.idle_irq++;
4111 if ((ap->stats.idle_irq % 1000) == 0) {
4113 ata_irq_ack(ap, 0); /* debug trap */
4114 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4117 return 0; /* irq not handled */
4121 * ata_interrupt - Default ATA host interrupt handler
4122 * @irq: irq line (unused)
4123 * @dev_instance: pointer to our ata_host_set information structure
4126 * Default interrupt handler for PCI IDE devices. Calls
4127 * ata_host_intr() for each port that is not disabled.
4130 * Obtains host_set lock during operation.
4133 * IRQ_NONE or IRQ_HANDLED.
4136 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4138 struct ata_host_set *host_set = dev_instance;
4140 unsigned int handled = 0;
4141 unsigned long flags;
4143 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4144 spin_lock_irqsave(&host_set->lock, flags);
4146 for (i = 0; i < host_set->n_ports; i++) {
4147 struct ata_port *ap;
4149 ap = host_set->ports[i];
4151 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4152 struct ata_queued_cmd *qc;
4154 qc = ata_qc_from_tag(ap, ap->active_tag);
4155 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4156 (qc->flags & ATA_QCFLAG_ACTIVE))
4157 handled |= ata_host_intr(ap, qc);
4161 spin_unlock_irqrestore(&host_set->lock, flags);
4163 return IRQ_RETVAL(handled);
4167 * atapi_packet_task - Write CDB bytes to hardware
4168 * @_data: Port to which ATAPI device is attached.
4170 * When device has indicated its readiness to accept
4171 * a CDB, this function is called. Send the CDB.
4172 * If DMA is to be performed, exit immediately.
4173 * Otherwise, we are in polling mode, so poll
4174 * status under operation succeeds or fails.
4177 * Kernel thread context (may sleep)
4180 static void atapi_packet_task(void *_data)
4182 struct ata_port *ap = _data;
4183 struct ata_queued_cmd *qc;
4186 qc = ata_qc_from_tag(ap, ap->active_tag);
4187 WARN_ON(qc == NULL);
4188 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4190 /* sleep-wait for BSY to clear */
4191 DPRINTK("busy wait\n");
4192 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4193 qc->err_mask |= AC_ERR_TIMEOUT;
4197 /* make sure DRQ is set */
4198 status = ata_chk_status(ap);
4199 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4200 qc->err_mask |= AC_ERR_HSM;
4205 DPRINTK("send cdb\n");
4206 WARN_ON(ap->cdb_len < 12);
4208 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4209 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4210 unsigned long flags;
4212 /* Once we're done issuing command and kicking bmdma,
4213 * irq handler takes over. To not lose irq, we need
4214 * to clear NOINTR flag before sending cdb, but
4215 * interrupt handler shouldn't be invoked before we're
4216 * finished. Hence, the following locking.
4218 spin_lock_irqsave(&ap->host_set->lock, flags);
4219 ap->flags &= ~ATA_FLAG_NOINTR;
4220 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4221 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4222 ap->ops->bmdma_start(qc); /* initiate bmdma */
4223 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4225 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4227 /* PIO commands are handled by polling */
4228 ap->hsm_task_state = HSM_ST;
4229 ata_queue_pio_task(ap);
4235 ata_poll_qc_complete(qc);
4240 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4241 * without filling any other registers
4243 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4246 struct ata_taskfile tf;
4249 ata_tf_init(ap, &tf, dev->devno);
4252 tf.flags |= ATA_TFLAG_DEVICE;
4253 tf.protocol = ATA_PROT_NODATA;
4255 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4257 printk(KERN_ERR "%s: ata command failed: %d\n",
4263 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4267 if (!ata_try_flush_cache(dev))
4270 if (ata_id_has_flush_ext(dev->id))
4271 cmd = ATA_CMD_FLUSH_EXT;
4273 cmd = ATA_CMD_FLUSH;
4275 return ata_do_simple_cmd(ap, dev, cmd);
4278 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4280 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4283 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4285 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4289 * ata_device_resume - wakeup a previously suspended devices
4290 * @ap: port the device is connected to
4291 * @dev: the device to resume
4293 * Kick the drive back into action, by sending it an idle immediate
4294 * command and making sure its transfer mode matches between drive
4298 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4300 if (ap->flags & ATA_FLAG_SUSPENDED) {
4301 ap->flags &= ~ATA_FLAG_SUSPENDED;
4304 if (!ata_dev_present(dev))
4306 if (dev->class == ATA_DEV_ATA)
4307 ata_start_drive(ap, dev);
4313 * ata_device_suspend - prepare a device for suspend
4314 * @ap: port the device is connected to
4315 * @dev: the device to suspend
4317 * Flush the cache on the drive, if appropriate, then issue a
4318 * standbynow command.
4320 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4322 if (!ata_dev_present(dev))
4324 if (dev->class == ATA_DEV_ATA)
4325 ata_flush_cache(ap, dev);
4327 ata_standby_drive(ap, dev);
4328 ap->flags |= ATA_FLAG_SUSPENDED;
4333 * ata_port_start - Set port up for dma.
4334 * @ap: Port to initialize
4336 * Called just after data structures for each port are
4337 * initialized. Allocates space for PRD table.
4339 * May be used as the port_start() entry in ata_port_operations.
4342 * Inherited from caller.
4345 int ata_port_start (struct ata_port *ap)
4347 struct device *dev = ap->host_set->dev;
4350 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4354 rc = ata_pad_alloc(ap, dev);
4356 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4360 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4367 * ata_port_stop - Undo ata_port_start()
4368 * @ap: Port to shut down
4370 * Frees the PRD table.
4372 * May be used as the port_stop() entry in ata_port_operations.
4375 * Inherited from caller.
4378 void ata_port_stop (struct ata_port *ap)
4380 struct device *dev = ap->host_set->dev;
4382 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4383 ata_pad_free(ap, dev);
4386 void ata_host_stop (struct ata_host_set *host_set)
4388 if (host_set->mmio_base)
4389 iounmap(host_set->mmio_base);
4394 * ata_host_remove - Unregister SCSI host structure with upper layers
4395 * @ap: Port to unregister
4396 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4399 * Inherited from caller.
4402 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4404 struct Scsi_Host *sh = ap->host;
4409 scsi_remove_host(sh);
4411 ap->ops->port_stop(ap);
4415 * ata_host_init - Initialize an ata_port structure
4416 * @ap: Structure to initialize
4417 * @host: associated SCSI mid-layer structure
4418 * @host_set: Collection of hosts to which @ap belongs
4419 * @ent: Probe information provided by low-level driver
4420 * @port_no: Port number associated with this ata_port
4422 * Initialize a new ata_port structure, and its associated
4426 * Inherited from caller.
4429 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4430 struct ata_host_set *host_set,
4431 const struct ata_probe_ent *ent, unsigned int port_no)
4437 host->max_channel = 1;
4438 host->unique_id = ata_unique_id++;
4439 host->max_cmd_len = 12;
4441 ap->flags = ATA_FLAG_PORT_DISABLED;
4442 ap->id = host->unique_id;
4444 ap->ctl = ATA_DEVCTL_OBS;
4445 ap->host_set = host_set;
4446 ap->port_no = port_no;
4448 ent->legacy_mode ? ent->hard_port_no : port_no;
4449 ap->pio_mask = ent->pio_mask;
4450 ap->mwdma_mask = ent->mwdma_mask;
4451 ap->udma_mask = ent->udma_mask;
4452 ap->flags |= ent->host_flags;
4453 ap->ops = ent->port_ops;
4454 ap->cbl = ATA_CBL_NONE;
4455 ap->active_tag = ATA_TAG_POISON;
4456 ap->last_ctl = 0xFF;
4458 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4459 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4460 INIT_LIST_HEAD(&ap->eh_done_q);
4462 for (i = 0; i < ATA_MAX_DEVICES; i++)
4463 ap->device[i].devno = i;
4466 ap->stats.unhandled_irq = 1;
4467 ap->stats.idle_irq = 1;
4470 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4474 * ata_host_add - Attach low-level ATA driver to system
4475 * @ent: Information provided by low-level driver
4476 * @host_set: Collections of ports to which we add
4477 * @port_no: Port number associated with this host
4479 * Attach low-level ATA driver to system.
4482 * PCI/etc. bus probe sem.
4485 * New ata_port on success, for NULL on error.
4488 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4489 struct ata_host_set *host_set,
4490 unsigned int port_no)
4492 struct Scsi_Host *host;
4493 struct ata_port *ap;
4497 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4501 ap = (struct ata_port *) &host->hostdata[0];
4503 ata_host_init(ap, host, host_set, ent, port_no);
4505 rc = ap->ops->port_start(ap);
4512 scsi_host_put(host);
4517 * ata_device_add - Register hardware device with ATA and SCSI layers
4518 * @ent: Probe information describing hardware device to be registered
4520 * This function processes the information provided in the probe
4521 * information struct @ent, allocates the necessary ATA and SCSI
4522 * host information structures, initializes them, and registers
4523 * everything with requisite kernel subsystems.
4525 * This function requests irqs, probes the ATA bus, and probes
4529 * PCI/etc. bus probe sem.
4532 * Number of ports registered. Zero on error (no ports registered).
4535 int ata_device_add(const struct ata_probe_ent *ent)
4537 unsigned int count = 0, i;
4538 struct device *dev = ent->dev;
4539 struct ata_host_set *host_set;
4542 /* alloc a container for our list of ATA ports (buses) */
4543 host_set = kzalloc(sizeof(struct ata_host_set) +
4544 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4547 spin_lock_init(&host_set->lock);
4549 host_set->dev = dev;
4550 host_set->n_ports = ent->n_ports;
4551 host_set->irq = ent->irq;
4552 host_set->mmio_base = ent->mmio_base;
4553 host_set->private_data = ent->private_data;
4554 host_set->ops = ent->port_ops;
4556 /* register each port bound to this device */
4557 for (i = 0; i < ent->n_ports; i++) {
4558 struct ata_port *ap;
4559 unsigned long xfer_mode_mask;
4561 ap = ata_host_add(ent, host_set, i);
4565 host_set->ports[i] = ap;
4566 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4567 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4568 (ap->pio_mask << ATA_SHIFT_PIO);
4570 /* print per-port info to dmesg */
4571 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4572 "bmdma 0x%lX irq %lu\n",
4574 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4575 ata_mode_string(xfer_mode_mask),
4576 ap->ioaddr.cmd_addr,
4577 ap->ioaddr.ctl_addr,
4578 ap->ioaddr.bmdma_addr,
4582 host_set->ops->irq_clear(ap);
4589 /* obtain irq, that is shared between channels */
4590 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4591 DRV_NAME, host_set))
4594 /* perform each probe synchronously */
4595 DPRINTK("probe begin\n");
4596 for (i = 0; i < count; i++) {
4597 struct ata_port *ap;
4600 ap = host_set->ports[i];
4602 DPRINTK("ata%u: bus probe begin\n", ap->id);
4603 rc = ata_bus_probe(ap);
4604 DPRINTK("ata%u: bus probe end\n", ap->id);
4607 /* FIXME: do something useful here?
4608 * Current libata behavior will
4609 * tear down everything when
4610 * the module is removed
4611 * or the h/w is unplugged.
4615 rc = scsi_add_host(ap->host, dev);
4617 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4619 /* FIXME: do something useful here */
4620 /* FIXME: handle unconditional calls to
4621 * scsi_scan_host and ata_host_remove, below,
4627 /* probes are done, now scan each port's disk(s) */
4628 DPRINTK("host probe begin\n");
4629 for (i = 0; i < count; i++) {
4630 struct ata_port *ap = host_set->ports[i];
4632 ata_scsi_scan_host(ap);
4635 dev_set_drvdata(dev, host_set);
4637 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4638 return ent->n_ports; /* success */
4641 for (i = 0; i < count; i++) {
4642 ata_host_remove(host_set->ports[i], 1);
4643 scsi_host_put(host_set->ports[i]->host);
4647 VPRINTK("EXIT, returning 0\n");
4652 * ata_host_set_remove - PCI layer callback for device removal
4653 * @host_set: ATA host set that was removed
4655 * Unregister all objects associated with this host set. Free those
4659 * Inherited from calling layer (may sleep).
4662 void ata_host_set_remove(struct ata_host_set *host_set)
4664 struct ata_port *ap;
4667 for (i = 0; i < host_set->n_ports; i++) {
4668 ap = host_set->ports[i];
4669 scsi_remove_host(ap->host);
4672 free_irq(host_set->irq, host_set);
4674 for (i = 0; i < host_set->n_ports; i++) {
4675 ap = host_set->ports[i];
4677 ata_scsi_release(ap->host);
4679 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4680 struct ata_ioports *ioaddr = &ap->ioaddr;
4682 if (ioaddr->cmd_addr == 0x1f0)
4683 release_region(0x1f0, 8);
4684 else if (ioaddr->cmd_addr == 0x170)
4685 release_region(0x170, 8);
4688 scsi_host_put(ap->host);
4691 if (host_set->ops->host_stop)
4692 host_set->ops->host_stop(host_set);
4698 * ata_scsi_release - SCSI layer callback hook for host unload
4699 * @host: libata host to be unloaded
4701 * Performs all duties necessary to shut down a libata port...
4702 * Kill port kthread, disable port, and release resources.
4705 * Inherited from SCSI layer.
4711 int ata_scsi_release(struct Scsi_Host *host)
4713 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4717 ap->ops->port_disable(ap);
4718 ata_host_remove(ap, 0);
4725 * ata_std_ports - initialize ioaddr with standard port offsets.
4726 * @ioaddr: IO address structure to be initialized
4728 * Utility function which initializes data_addr, error_addr,
4729 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4730 * device_addr, status_addr, and command_addr to standard offsets
4731 * relative to cmd_addr.
4733 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4736 void ata_std_ports(struct ata_ioports *ioaddr)
4738 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4739 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4740 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4741 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4742 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4743 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4744 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4745 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4746 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4747 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4753 void ata_pci_host_stop (struct ata_host_set *host_set)
4755 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4757 pci_iounmap(pdev, host_set->mmio_base);
4761 * ata_pci_remove_one - PCI layer callback for device removal
4762 * @pdev: PCI device that was removed
4764 * PCI layer indicates to libata via this hook that
4765 * hot-unplug or module unload event has occurred.
4766 * Handle this by unregistering all objects associated
4767 * with this PCI device. Free those objects. Then finally
4768 * release PCI resources and disable device.
4771 * Inherited from PCI layer (may sleep).
4774 void ata_pci_remove_one (struct pci_dev *pdev)
4776 struct device *dev = pci_dev_to_dev(pdev);
4777 struct ata_host_set *host_set = dev_get_drvdata(dev);
4779 ata_host_set_remove(host_set);
4780 pci_release_regions(pdev);
4781 pci_disable_device(pdev);
4782 dev_set_drvdata(dev, NULL);
4785 /* move to PCI subsystem */
4786 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4788 unsigned long tmp = 0;
4790 switch (bits->width) {
4793 pci_read_config_byte(pdev, bits->reg, &tmp8);
4799 pci_read_config_word(pdev, bits->reg, &tmp16);
4805 pci_read_config_dword(pdev, bits->reg, &tmp32);
4816 return (tmp == bits->val) ? 1 : 0;
4819 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4821 pci_save_state(pdev);
4822 pci_disable_device(pdev);
4823 pci_set_power_state(pdev, PCI_D3hot);
4827 int ata_pci_device_resume(struct pci_dev *pdev)
4829 pci_set_power_state(pdev, PCI_D0);
4830 pci_restore_state(pdev);
4831 pci_enable_device(pdev);
4832 pci_set_master(pdev);
4835 #endif /* CONFIG_PCI */
4838 static int __init ata_init(void)
4840 ata_wq = create_workqueue("ata");
4844 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4848 static void __exit ata_exit(void)
4850 destroy_workqueue(ata_wq);
4853 module_init(ata_init);
4854 module_exit(ata_exit);
4856 static unsigned long ratelimit_time;
4857 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4859 int ata_ratelimit(void)
4862 unsigned long flags;
4864 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4866 if (time_after(jiffies, ratelimit_time)) {
4868 ratelimit_time = jiffies + (HZ/5);
4872 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4878 * libata is essentially a library of internal helper functions for
4879 * low-level ATA host controller drivers. As such, the API/ABI is
4880 * likely to change as new drivers are added and updated.
4881 * Do not depend on ABI/API stability.
4884 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4885 EXPORT_SYMBOL_GPL(ata_std_ports);
4886 EXPORT_SYMBOL_GPL(ata_device_add);
4887 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4888 EXPORT_SYMBOL_GPL(ata_sg_init);
4889 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4890 EXPORT_SYMBOL_GPL(__ata_qc_complete);
4891 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4892 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4893 EXPORT_SYMBOL_GPL(ata_tf_load);
4894 EXPORT_SYMBOL_GPL(ata_tf_read);
4895 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4896 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4897 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4898 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4899 EXPORT_SYMBOL_GPL(ata_check_status);
4900 EXPORT_SYMBOL_GPL(ata_altstatus);
4901 EXPORT_SYMBOL_GPL(ata_exec_command);
4902 EXPORT_SYMBOL_GPL(ata_port_start);
4903 EXPORT_SYMBOL_GPL(ata_port_stop);
4904 EXPORT_SYMBOL_GPL(ata_host_stop);
4905 EXPORT_SYMBOL_GPL(ata_interrupt);
4906 EXPORT_SYMBOL_GPL(ata_qc_prep);
4907 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4908 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4909 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4910 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4911 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4912 EXPORT_SYMBOL_GPL(ata_port_probe);
4913 EXPORT_SYMBOL_GPL(sata_phy_reset);
4914 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4915 EXPORT_SYMBOL_GPL(ata_bus_reset);
4916 EXPORT_SYMBOL_GPL(ata_std_probeinit);
4917 EXPORT_SYMBOL_GPL(ata_std_softreset);
4918 EXPORT_SYMBOL_GPL(sata_std_hardreset);
4919 EXPORT_SYMBOL_GPL(ata_std_postreset);
4920 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
4921 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
4922 EXPORT_SYMBOL_GPL(ata_port_disable);
4923 EXPORT_SYMBOL_GPL(ata_ratelimit);
4924 EXPORT_SYMBOL_GPL(ata_busy_sleep);
4925 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4926 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4927 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
4928 EXPORT_SYMBOL_GPL(ata_scsi_error);
4929 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4930 EXPORT_SYMBOL_GPL(ata_scsi_release);
4931 EXPORT_SYMBOL_GPL(ata_host_intr);
4932 EXPORT_SYMBOL_GPL(ata_dev_classify);
4933 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4934 EXPORT_SYMBOL_GPL(ata_dev_id_c_string);
4935 EXPORT_SYMBOL_GPL(ata_dev_config);
4936 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4937 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4938 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
4940 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
4941 EXPORT_SYMBOL_GPL(ata_timing_compute);
4942 EXPORT_SYMBOL_GPL(ata_timing_merge);
4945 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4946 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4947 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4948 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4949 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4950 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4951 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
4952 #endif /* CONFIG_PCI */
4954 EXPORT_SYMBOL_GPL(ata_device_suspend);
4955 EXPORT_SYMBOL_GPL(ata_device_resume);
4956 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4957 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);