2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(const struct ata_port *ap,
72 unsigned int *xfer_shift_out);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 int atapi_enabled = 0;
78 module_param(atapi_enabled, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82 module_param_named(fua, libata_fua, int, 0444);
83 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
92 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
101 * Inherited from caller.
104 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
123 fis[13] = tf->hob_nsect;
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
141 * Inherited from caller.
144 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
159 tf->hob_nsect = fis[13];
162 static const u8 ata_rw_cmds[] = {
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
189 ATA_CMD_WRITE_FUA_EXT
193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
202 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
208 int index, fua, lba48, write;
210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 8;
217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
220 index = dev->multi_count ? 0 : 8;
222 tf->protocol = ATA_PROT_DMA;
226 cmd = ata_rw_cmds[index + fua + lba48 + write];
234 static const char * const xfer_mode_str[] = {
254 * ata_udma_string - convert UDMA bit offset to string
255 * @mask: mask of bits supported; only highest bit counts.
257 * Determine string which represents the highest speed
258 * (highest bit in @udma_mask).
264 * Constant C string representing highest speed listed in
265 * @udma_mask, or the constant C string "<n/a>".
268 static const char *ata_mode_string(unsigned int mask)
272 for (i = 7; i >= 0; i--)
275 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
278 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
285 return xfer_mode_str[i];
289 * ata_pio_devchk - PATA device presence detection
290 * @ap: ATA channel to examine
291 * @device: Device to examine (starting at zero)
293 * This technique was originally described in
294 * Hale Landis's ATADRVR (www.ata-atapi.com), and
295 * later found its way into the ATA/ATAPI spec.
297 * Write a pattern to the ATA shadow registers,
298 * and if a device is present, it will respond by
299 * correctly storing and echoing back the
300 * ATA shadow register contents.
306 static unsigned int ata_pio_devchk(struct ata_port *ap,
309 struct ata_ioports *ioaddr = &ap->ioaddr;
312 ap->ops->dev_select(ap, device);
314 outb(0x55, ioaddr->nsect_addr);
315 outb(0xaa, ioaddr->lbal_addr);
317 outb(0xaa, ioaddr->nsect_addr);
318 outb(0x55, ioaddr->lbal_addr);
320 outb(0x55, ioaddr->nsect_addr);
321 outb(0xaa, ioaddr->lbal_addr);
323 nsect = inb(ioaddr->nsect_addr);
324 lbal = inb(ioaddr->lbal_addr);
326 if ((nsect == 0x55) && (lbal == 0xaa))
327 return 1; /* we found a device */
329 return 0; /* nothing found */
333 * ata_mmio_devchk - PATA device presence detection
334 * @ap: ATA channel to examine
335 * @device: Device to examine (starting at zero)
337 * This technique was originally described in
338 * Hale Landis's ATADRVR (www.ata-atapi.com), and
339 * later found its way into the ATA/ATAPI spec.
341 * Write a pattern to the ATA shadow registers,
342 * and if a device is present, it will respond by
343 * correctly storing and echoing back the
344 * ATA shadow register contents.
350 static unsigned int ata_mmio_devchk(struct ata_port *ap,
353 struct ata_ioports *ioaddr = &ap->ioaddr;
356 ap->ops->dev_select(ap, device);
358 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
364 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
365 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
367 nsect = readb((void __iomem *) ioaddr->nsect_addr);
368 lbal = readb((void __iomem *) ioaddr->lbal_addr);
370 if ((nsect == 0x55) && (lbal == 0xaa))
371 return 1; /* we found a device */
373 return 0; /* nothing found */
377 * ata_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
381 * Dispatch ATA device presence detection, depending
382 * on whether we are using PIO or MMIO to talk to the
383 * ATA shadow registers.
389 static unsigned int ata_devchk(struct ata_port *ap,
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_mmio_devchk(ap, device);
394 return ata_pio_devchk(ap, device);
398 * ata_dev_classify - determine device type based on ATA-spec signature
399 * @tf: ATA taskfile register set for device to be identified
401 * Determine from taskfile register contents whether a device is
402 * ATA or ATAPI, as per "Signature and persistence" section
403 * of ATA/PI spec (volume 1, sect 5.14).
409 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
410 * the event of failure.
413 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
415 /* Apple's open source Darwin code hints that some devices only
416 * put a proper signature into the LBA mid/high registers,
417 * So, we only check those. It's sufficient for uniqueness.
420 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
421 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
422 DPRINTK("found ATA device by sig\n");
426 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
427 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
428 DPRINTK("found ATAPI device by sig\n");
429 return ATA_DEV_ATAPI;
432 DPRINTK("unknown device\n");
433 return ATA_DEV_UNKNOWN;
437 * ata_dev_try_classify - Parse returned ATA device signature
438 * @ap: ATA channel to examine
439 * @device: Device to examine (starting at zero)
440 * @r_err: Value of error register on completion
442 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
443 * an ATA/ATAPI-defined set of values is placed in the ATA
444 * shadow registers, indicating the results of device detection
447 * Select the ATA device, and read the values from the ATA shadow
448 * registers. Then parse according to the Error register value,
449 * and the spec-defined values examined by ata_dev_classify().
455 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
459 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
461 struct ata_taskfile tf;
465 ap->ops->dev_select(ap, device);
467 memset(&tf, 0, sizeof(tf));
469 ap->ops->tf_read(ap, &tf);
474 /* see if device passed diags */
477 else if ((device == 0) && (err == 0x81))
482 /* determine if device is ATA or ATAPI */
483 class = ata_dev_classify(&tf);
485 if (class == ATA_DEV_UNKNOWN)
487 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
493 * ata_id_string - Convert IDENTIFY DEVICE page into string
494 * @id: IDENTIFY DEVICE results we will examine
495 * @s: string into which data is output
496 * @ofs: offset into identify device page
497 * @len: length of string to return. must be an even number.
499 * The strings in the IDENTIFY DEVICE page are broken up into
500 * 16-bit chunks. Run through the string, and output each
501 * 8-bit chunk linearly, regardless of platform.
507 void ata_id_string(const u16 *id, unsigned char *s,
508 unsigned int ofs, unsigned int len)
527 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
528 * @id: IDENTIFY DEVICE results we will examine
529 * @s: string into which data is output
530 * @ofs: offset into identify device page
531 * @len: length of string to return. must be an odd number.
533 * This function is identical to ata_id_string except that it
534 * trims trailing spaces and terminates the resulting string with
535 * null. @len must be actual maximum length (even number) + 1.
540 void ata_id_c_string(const u16 *id, unsigned char *s,
541 unsigned int ofs, unsigned int len)
547 ata_id_string(id, s, ofs, len - 1);
549 p = s + strnlen(s, len - 1);
550 while (p > s && p[-1] == ' ')
555 static u64 ata_id_n_sectors(const u16 *id)
557 if (ata_id_has_lba(id)) {
558 if (ata_id_has_lba48(id))
559 return ata_id_u64(id, 100);
561 return ata_id_u32(id, 60);
563 if (ata_id_current_chs_valid(id))
564 return ata_id_u32(id, 57);
566 return id[1] * id[3] * id[6];
571 * ata_noop_dev_select - Select device 0/1 on ATA bus
572 * @ap: ATA channel to manipulate
573 * @device: ATA device (numbered from zero) to select
575 * This function performs no actual function.
577 * May be used as the dev_select() entry in ata_port_operations.
582 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
588 * ata_std_dev_select - Select device 0/1 on ATA bus
589 * @ap: ATA channel to manipulate
590 * @device: ATA device (numbered from zero) to select
592 * Use the method defined in the ATA specification to
593 * make either device 0, or device 1, active on the
594 * ATA channel. Works with both PIO and MMIO.
596 * May be used as the dev_select() entry in ata_port_operations.
602 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
607 tmp = ATA_DEVICE_OBS;
609 tmp = ATA_DEVICE_OBS | ATA_DEV1;
611 if (ap->flags & ATA_FLAG_MMIO) {
612 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
614 outb(tmp, ap->ioaddr.device_addr);
616 ata_pause(ap); /* needed; also flushes, for mmio */
620 * ata_dev_select - Select device 0/1 on ATA bus
621 * @ap: ATA channel to manipulate
622 * @device: ATA device (numbered from zero) to select
623 * @wait: non-zero to wait for Status register BSY bit to clear
624 * @can_sleep: non-zero if context allows sleeping
626 * Use the method defined in the ATA specification to
627 * make either device 0, or device 1, active on the
630 * This is a high-level version of ata_std_dev_select(),
631 * which additionally provides the services of inserting
632 * the proper pauses and status polling, where needed.
638 void ata_dev_select(struct ata_port *ap, unsigned int device,
639 unsigned int wait, unsigned int can_sleep)
641 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
642 ap->id, device, wait);
647 ap->ops->dev_select(ap, device);
650 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
657 * ata_dump_id - IDENTIFY DEVICE info debugging output
658 * @id: IDENTIFY DEVICE page to dump
660 * Dump selected 16-bit words from the given IDENTIFY DEVICE
667 static inline void ata_dump_id(const u16 *id)
669 DPRINTK("49==0x%04x "
679 DPRINTK("80==0x%04x "
689 DPRINTK("88==0x%04x "
696 * Compute the PIO modes available for this device. This is not as
697 * trivial as it seems if we must consider early devices correctly.
699 * FIXME: pre IDE drive timing (do we care ?).
702 static unsigned int ata_pio_modes(const struct ata_device *adev)
706 /* Usual case. Word 53 indicates word 64 is valid */
707 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
708 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
714 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
715 number for the maximum. Turn it into a mask and return it */
716 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
718 /* But wait.. there's more. Design your standards by committee and
719 you too can get a free iordy field to process. However its the
720 speeds not the modes that are supported... Note drivers using the
721 timing API will get this right anyway */
725 ata_queue_packet_task(struct ata_port *ap)
727 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
728 queue_work(ata_wq, &ap->packet_task);
732 ata_queue_pio_task(struct ata_port *ap)
734 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
735 queue_work(ata_wq, &ap->pio_task);
739 ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
741 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
742 queue_delayed_work(ata_wq, &ap->pio_task, delay);
746 * ata_flush_pio_tasks - Flush pio_task and packet_task
747 * @ap: the target ata_port
749 * After this function completes, pio_task and packet_task are
750 * guranteed not to be running or scheduled.
753 * Kernel thread context (may sleep)
756 static void ata_flush_pio_tasks(struct ata_port *ap)
763 spin_lock_irqsave(&ap->host_set->lock, flags);
764 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
765 spin_unlock_irqrestore(&ap->host_set->lock, flags);
767 DPRINTK("flush #1\n");
768 flush_workqueue(ata_wq);
771 * At this point, if a task is running, it's guaranteed to see
772 * the FLUSH flag; thus, it will never queue pio tasks again.
775 tmp |= cancel_delayed_work(&ap->pio_task);
776 tmp |= cancel_delayed_work(&ap->packet_task);
778 DPRINTK("flush #2\n");
779 flush_workqueue(ata_wq);
782 spin_lock_irqsave(&ap->host_set->lock, flags);
783 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
784 spin_unlock_irqrestore(&ap->host_set->lock, flags);
789 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
791 struct completion *waiting = qc->private_data;
793 qc->ap->ops->tf_read(qc->ap, &qc->tf);
798 * ata_exec_internal - execute libata internal command
799 * @ap: Port to which the command is sent
800 * @dev: Device to which the command is sent
801 * @tf: Taskfile registers for the command and the result
802 * @dma_dir: Data tranfer direction of the command
803 * @buf: Data buffer of the command
804 * @buflen: Length of data buffer
806 * Executes libata internal command with timeout. @tf contains
807 * command on entry and result on return. Timeout and error
808 * conditions are reported via return value. No recovery action
809 * is taken after a command times out. It's caller's duty to
810 * clean up after timeout.
813 * None. Should be called with kernel context, might sleep.
817 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
818 struct ata_taskfile *tf,
819 int dma_dir, void *buf, unsigned int buflen)
821 u8 command = tf->command;
822 struct ata_queued_cmd *qc;
823 DECLARE_COMPLETION(wait);
825 unsigned int err_mask;
827 spin_lock_irqsave(&ap->host_set->lock, flags);
829 qc = ata_qc_new_init(ap, dev);
833 qc->dma_dir = dma_dir;
834 if (dma_dir != DMA_NONE) {
835 ata_sg_init_one(qc, buf, buflen);
836 qc->nsect = buflen / ATA_SECT_SIZE;
839 qc->private_data = &wait;
840 qc->complete_fn = ata_qc_complete_internal;
842 qc->err_mask = ata_qc_issue(qc);
846 spin_unlock_irqrestore(&ap->host_set->lock, flags);
848 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
849 spin_lock_irqsave(&ap->host_set->lock, flags);
851 /* We're racing with irq here. If we lose, the
852 * following test prevents us from completing the qc
853 * again. If completion irq occurs after here but
854 * before the caller cleans up, it will result in a
855 * spurious interrupt. We can live with that.
857 if (qc->flags & ATA_QCFLAG_ACTIVE) {
858 qc->err_mask = AC_ERR_TIMEOUT;
860 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
864 spin_unlock_irqrestore(&ap->host_set->lock, flags);
868 err_mask = qc->err_mask;
876 * ata_pio_need_iordy - check if iordy needed
879 * Check if the current speed of the device requires IORDY. Used
880 * by various controllers for chip configuration.
883 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
886 int speed = adev->pio_mode - XFER_PIO_0;
893 /* If we have no drive specific rule, then PIO 2 is non IORDY */
895 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
896 pio = adev->id[ATA_ID_EIDE_PIO];
897 /* Is the speed faster than the drive allows non IORDY ? */
899 /* This is cycle times not frequency - watch the logic! */
900 if (pio > 240) /* PIO2 is 240nS per cycle */
909 * ata_dev_read_id - Read ID data from the specified device
910 * @ap: port on which target device resides
911 * @dev: target device
912 * @p_class: pointer to class of the target device (may be changed)
913 * @post_reset: is this read ID post-reset?
914 * @p_id: read IDENTIFY page (newly allocated)
916 * Read ID data from the specified device. ATA_CMD_ID_ATA is
917 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
918 * devices. This function also takes care of EDD signature
919 * misreporting (to be removed once EDD support is gone) and
920 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
923 * Kernel thread context (may sleep)
926 * 0 on success, -errno otherwise.
928 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
929 unsigned int *p_class, int post_reset, u16 **p_id)
931 unsigned int class = *p_class;
932 unsigned int using_edd;
933 struct ata_taskfile tf;
934 unsigned int err_mask = 0;
939 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
941 if (ap->ops->probe_reset ||
942 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
947 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
949 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
952 reason = "out of memory";
957 ata_tf_init(ap, &tf, dev->devno);
961 tf.command = ATA_CMD_ID_ATA;
964 tf.command = ATA_CMD_ID_ATAPI;
968 reason = "unsupported class";
972 tf.protocol = ATA_PROT_PIO;
974 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
975 id, sizeof(id[0]) * ATA_ID_WORDS);
979 reason = "I/O error";
981 if (err_mask & ~AC_ERR_DEV)
985 * arg! EDD works for all test cases, but seems to return
986 * the ATA signature for some ATAPI devices. Until the
987 * reason for this is found and fixed, we fix up the mess
988 * here. If IDENTIFY DEVICE returns command aborted
989 * (as ATAPI devices do), then we issue an
990 * IDENTIFY PACKET DEVICE.
992 * ATA software reset (SRST, the default) does not appear
993 * to have this problem.
995 if ((using_edd) && (class == ATA_DEV_ATA)) {
997 if (err & ATA_ABORTED) {
998 class = ATA_DEV_ATAPI;
1005 swap_buf_le16(id, ATA_ID_WORDS);
1007 /* print device capabilities */
1008 printk(KERN_DEBUG "ata%u: dev %u cfg "
1009 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1011 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1014 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1016 reason = "device reports illegal type";
1020 if (post_reset && class == ATA_DEV_ATA) {
1022 * The exact sequence expected by certain pre-ATA4 drives is:
1025 * INITIALIZE DEVICE PARAMETERS
1027 * Some drives were very specific about that exact sequence.
1029 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1030 err_mask = ata_dev_init_params(ap, dev);
1033 reason = "INIT_DEV_PARAMS failed";
1037 /* current CHS translation info (id[53-58]) might be
1038 * changed. reread the identify device info.
1050 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1051 ap->id, dev->devno, reason);
1056 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1057 struct ata_device *dev)
1059 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1063 * ata_dev_configure - Configure the specified ATA/ATAPI device
1064 * @ap: Port on which target device resides
1065 * @dev: Target device to configure
1066 * @print_info: Enable device info printout
1068 * Configure @dev according to @dev->id. Generic and low-level
1069 * driver specific fixups are also applied.
1072 * Kernel thread context (may sleep)
1075 * 0 on success, -errno otherwise
1077 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1080 unsigned long xfer_modes;
1083 if (!ata_dev_present(dev)) {
1084 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1085 ap->id, dev->devno);
1089 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1091 /* initialize to-be-configured parameters */
1093 dev->max_sectors = 0;
1101 * common ATA, ATAPI feature tests
1104 /* we require DMA support (bits 8 of word 49) */
1105 if (!ata_id_has_dma(dev->id)) {
1106 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1111 /* quick-n-dirty find max transfer mode; for printk only */
1112 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1114 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1116 xfer_modes = ata_pio_modes(dev);
1118 ata_dump_id(dev->id);
1120 /* ATA-specific feature tests */
1121 if (dev->class == ATA_DEV_ATA) {
1122 dev->n_sectors = ata_id_n_sectors(dev->id);
1124 if (ata_id_has_lba(dev->id)) {
1125 const char *lba_desc;
1128 dev->flags |= ATA_DFLAG_LBA;
1129 if (ata_id_has_lba48(dev->id)) {
1130 dev->flags |= ATA_DFLAG_LBA48;
1134 /* print device info to dmesg */
1136 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1137 "max %s, %Lu sectors: %s\n",
1139 ata_id_major_version(dev->id),
1140 ata_mode_string(xfer_modes),
1141 (unsigned long long)dev->n_sectors,
1146 /* Default translation */
1147 dev->cylinders = dev->id[1];
1148 dev->heads = dev->id[3];
1149 dev->sectors = dev->id[6];
1151 if (ata_id_current_chs_valid(dev->id)) {
1152 /* Current CHS translation is valid. */
1153 dev->cylinders = dev->id[54];
1154 dev->heads = dev->id[55];
1155 dev->sectors = dev->id[56];
1158 /* print device info to dmesg */
1160 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1161 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1163 ata_id_major_version(dev->id),
1164 ata_mode_string(xfer_modes),
1165 (unsigned long long)dev->n_sectors,
1166 dev->cylinders, dev->heads, dev->sectors);
1172 /* ATAPI-specific feature tests */
1173 else if (dev->class == ATA_DEV_ATAPI) {
1174 rc = atapi_cdb_len(dev->id);
1175 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1176 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1180 dev->cdb_len = (unsigned int) rc;
1182 /* print device info to dmesg */
1184 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1185 ap->id, dev->devno, ata_mode_string(xfer_modes));
1188 ap->host->max_cmd_len = 0;
1189 for (i = 0; i < ATA_MAX_DEVICES; i++)
1190 ap->host->max_cmd_len = max_t(unsigned int,
1191 ap->host->max_cmd_len,
1192 ap->device[i].cdb_len);
1194 /* limit bridge transfers to udma5, 200 sectors */
1195 if (ata_dev_knobble(ap, dev)) {
1197 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1198 ap->id, dev->devno);
1199 ap->udma_mask &= ATA_UDMA5;
1200 dev->max_sectors = ATA_MAX_SECTORS;
1203 if (ap->ops->dev_config)
1204 ap->ops->dev_config(ap, dev);
1206 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1210 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1211 ap->id, dev->devno);
1212 DPRINTK("EXIT, err\n");
1217 * ata_bus_probe - Reset and probe ATA bus
1220 * Master ATA bus probing function. Initiates a hardware-dependent
1221 * bus reset, then attempts to identify any devices found on
1225 * PCI/etc. bus probe sem.
1228 * Zero on success, non-zero on error.
1231 static int ata_bus_probe(struct ata_port *ap)
1233 unsigned int classes[ATA_MAX_DEVICES];
1234 unsigned int i, rc, found = 0;
1239 if (ap->ops->probe_reset) {
1240 rc = ap->ops->probe_reset(ap, classes);
1242 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1246 for (i = 0; i < ATA_MAX_DEVICES; i++)
1247 if (classes[i] == ATA_DEV_UNKNOWN)
1248 classes[i] = ATA_DEV_NONE;
1250 ap->ops->phy_reset(ap);
1252 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1253 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1254 classes[i] = ap->device[i].class;
1256 ap->device[i].class = ATA_DEV_UNKNOWN;
1261 /* read IDENTIFY page and configure devices */
1262 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1263 struct ata_device *dev = &ap->device[i];
1265 dev->class = classes[i];
1267 if (!ata_dev_present(dev))
1270 WARN_ON(dev->id != NULL);
1271 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1272 dev->class = ATA_DEV_NONE;
1276 if (ata_dev_configure(ap, dev, 1)) {
1277 dev->class++; /* disable device */
1285 goto err_out_disable;
1288 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1289 goto err_out_disable;
1294 ap->ops->port_disable(ap);
1299 * ata_port_probe - Mark port as enabled
1300 * @ap: Port for which we indicate enablement
1302 * Modify @ap data structure such that the system
1303 * thinks that the entire port is enabled.
1305 * LOCKING: host_set lock, or some other form of
1309 void ata_port_probe(struct ata_port *ap)
1311 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1315 * sata_print_link_status - Print SATA link status
1316 * @ap: SATA port to printk link status about
1318 * This function prints link speed and status of a SATA link.
1323 static void sata_print_link_status(struct ata_port *ap)
1328 if (!ap->ops->scr_read)
1331 sstatus = scr_read(ap, SCR_STATUS);
1333 if (sata_dev_present(ap)) {
1334 tmp = (sstatus >> 4) & 0xf;
1337 else if (tmp & (1 << 1))
1340 speed = "<unknown>";
1341 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1342 ap->id, speed, sstatus);
1344 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1350 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1351 * @ap: SATA port associated with target SATA PHY.
1353 * This function issues commands to standard SATA Sxxx
1354 * PHY registers, to wake up the phy (and device), and
1355 * clear any reset condition.
1358 * PCI/etc. bus probe sem.
1361 void __sata_phy_reset(struct ata_port *ap)
1364 unsigned long timeout = jiffies + (HZ * 5);
1366 if (ap->flags & ATA_FLAG_SATA_RESET) {
1367 /* issue phy wake/reset */
1368 scr_write_flush(ap, SCR_CONTROL, 0x301);
1369 /* Couldn't find anything in SATA I/II specs, but
1370 * AHCI-1.1 10.4.2 says at least 1 ms. */
1373 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1375 /* wait for phy to become ready, if necessary */
1378 sstatus = scr_read(ap, SCR_STATUS);
1379 if ((sstatus & 0xf) != 1)
1381 } while (time_before(jiffies, timeout));
1383 /* print link status */
1384 sata_print_link_status(ap);
1386 /* TODO: phy layer with polling, timeouts, etc. */
1387 if (sata_dev_present(ap))
1390 ata_port_disable(ap);
1392 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1395 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1396 ata_port_disable(ap);
1400 ap->cbl = ATA_CBL_SATA;
1404 * sata_phy_reset - Reset SATA bus.
1405 * @ap: SATA port associated with target SATA PHY.
1407 * This function resets the SATA bus, and then probes
1408 * the bus for devices.
1411 * PCI/etc. bus probe sem.
1414 void sata_phy_reset(struct ata_port *ap)
1416 __sata_phy_reset(ap);
1417 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1423 * ata_port_disable - Disable port.
1424 * @ap: Port to be disabled.
1426 * Modify @ap data structure such that the system
1427 * thinks that the entire port is disabled, and should
1428 * never attempt to probe or communicate with devices
1431 * LOCKING: host_set lock, or some other form of
1435 void ata_port_disable(struct ata_port *ap)
1437 ap->device[0].class = ATA_DEV_NONE;
1438 ap->device[1].class = ATA_DEV_NONE;
1439 ap->flags |= ATA_FLAG_PORT_DISABLED;
1443 * This mode timing computation functionality is ported over from
1444 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1447 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1448 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1449 * for PIO 5, which is a nonstandard extension and UDMA6, which
1450 * is currently supported only by Maxtor drives.
1453 static const struct ata_timing ata_timing[] = {
1455 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1456 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1457 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1458 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1460 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1461 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1462 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1464 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1466 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1467 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1468 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1470 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1471 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1472 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1474 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1475 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1476 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1478 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1479 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1480 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1482 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1487 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1488 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1490 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1492 q->setup = EZ(t->setup * 1000, T);
1493 q->act8b = EZ(t->act8b * 1000, T);
1494 q->rec8b = EZ(t->rec8b * 1000, T);
1495 q->cyc8b = EZ(t->cyc8b * 1000, T);
1496 q->active = EZ(t->active * 1000, T);
1497 q->recover = EZ(t->recover * 1000, T);
1498 q->cycle = EZ(t->cycle * 1000, T);
1499 q->udma = EZ(t->udma * 1000, UT);
1502 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1503 struct ata_timing *m, unsigned int what)
1505 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1506 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1507 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1508 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1509 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1510 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1511 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1512 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1515 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1517 const struct ata_timing *t;
1519 for (t = ata_timing; t->mode != speed; t++)
1520 if (t->mode == 0xFF)
1525 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1526 struct ata_timing *t, int T, int UT)
1528 const struct ata_timing *s;
1529 struct ata_timing p;
1535 if (!(s = ata_timing_find_mode(speed)))
1538 memcpy(t, s, sizeof(*s));
1541 * If the drive is an EIDE drive, it can tell us it needs extended
1542 * PIO/MW_DMA cycle timing.
1545 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1546 memset(&p, 0, sizeof(p));
1547 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1548 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1549 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1550 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1551 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1553 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1557 * Convert the timing to bus clock counts.
1560 ata_timing_quantize(t, t, T, UT);
1563 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1564 * S.M.A.R.T * and some other commands. We have to ensure that the
1565 * DMA cycle timing is slower/equal than the fastest PIO timing.
1568 if (speed > XFER_PIO_4) {
1569 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1570 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1574 * Lengthen active & recovery time so that cycle time is correct.
1577 if (t->act8b + t->rec8b < t->cyc8b) {
1578 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1579 t->rec8b = t->cyc8b - t->act8b;
1582 if (t->active + t->recover < t->cycle) {
1583 t->active += (t->cycle - (t->active + t->recover)) / 2;
1584 t->recover = t->cycle - t->active;
1590 static const struct {
1593 } xfer_mode_classes[] = {
1594 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1595 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1596 { ATA_SHIFT_PIO, XFER_PIO_0 },
1599 static u8 base_from_shift(unsigned int shift)
1603 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1604 if (xfer_mode_classes[i].shift == shift)
1605 return xfer_mode_classes[i].base;
1610 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1615 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1618 if (dev->xfer_shift == ATA_SHIFT_PIO)
1619 dev->flags |= ATA_DFLAG_PIO;
1621 ata_dev_set_xfermode(ap, dev);
1623 base = base_from_shift(dev->xfer_shift);
1624 ofs = dev->xfer_mode - base;
1625 idx = ofs + dev->xfer_shift;
1626 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1628 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1629 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1631 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1632 ap->id, dev->devno, xfer_mode_str[idx]);
1635 static int ata_host_set_pio(struct ata_port *ap)
1641 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1644 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1648 base = base_from_shift(ATA_SHIFT_PIO);
1649 xfer_mode = base + x;
1651 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1652 (int)base, (int)xfer_mode, mask, x);
1654 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1655 struct ata_device *dev = &ap->device[i];
1656 if (ata_dev_present(dev)) {
1657 dev->pio_mode = xfer_mode;
1658 dev->xfer_mode = xfer_mode;
1659 dev->xfer_shift = ATA_SHIFT_PIO;
1660 if (ap->ops->set_piomode)
1661 ap->ops->set_piomode(ap, dev);
1668 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1669 unsigned int xfer_shift)
1673 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1674 struct ata_device *dev = &ap->device[i];
1675 if (ata_dev_present(dev)) {
1676 dev->dma_mode = xfer_mode;
1677 dev->xfer_mode = xfer_mode;
1678 dev->xfer_shift = xfer_shift;
1679 if (ap->ops->set_dmamode)
1680 ap->ops->set_dmamode(ap, dev);
1686 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1687 * @ap: port on which timings will be programmed
1689 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1692 * PCI/etc. bus probe sem.
1694 static void ata_set_mode(struct ata_port *ap)
1696 unsigned int xfer_shift;
1700 /* step 1: always set host PIO timings */
1701 rc = ata_host_set_pio(ap);
1705 /* step 2: choose the best data xfer mode */
1706 xfer_mode = xfer_shift = 0;
1707 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1711 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1712 if (xfer_shift != ATA_SHIFT_PIO)
1713 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1715 /* step 4: update devices' xfer mode */
1716 ata_dev_set_mode(ap, &ap->device[0]);
1717 ata_dev_set_mode(ap, &ap->device[1]);
1719 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1722 if (ap->ops->post_set_mode)
1723 ap->ops->post_set_mode(ap);
1728 ata_port_disable(ap);
1732 * ata_tf_to_host - issue ATA taskfile to host controller
1733 * @ap: port to which command is being issued
1734 * @tf: ATA taskfile register set
1736 * Issues ATA taskfile register set to ATA host controller,
1737 * with proper synchronization with interrupt handler and
1741 * spin_lock_irqsave(host_set lock)
1744 static inline void ata_tf_to_host(struct ata_port *ap,
1745 const struct ata_taskfile *tf)
1747 ap->ops->tf_load(ap, tf);
1748 ap->ops->exec_command(ap, tf);
1752 * ata_busy_sleep - sleep until BSY clears, or timeout
1753 * @ap: port containing status register to be polled
1754 * @tmout_pat: impatience timeout
1755 * @tmout: overall timeout
1757 * Sleep until ATA Status register bit BSY clears,
1758 * or a timeout occurs.
1763 unsigned int ata_busy_sleep (struct ata_port *ap,
1764 unsigned long tmout_pat, unsigned long tmout)
1766 unsigned long timer_start, timeout;
1769 status = ata_busy_wait(ap, ATA_BUSY, 300);
1770 timer_start = jiffies;
1771 timeout = timer_start + tmout_pat;
1772 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1774 status = ata_busy_wait(ap, ATA_BUSY, 3);
1777 if (status & ATA_BUSY)
1778 printk(KERN_WARNING "ata%u is slow to respond, "
1779 "please be patient\n", ap->id);
1781 timeout = timer_start + tmout;
1782 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1784 status = ata_chk_status(ap);
1787 if (status & ATA_BUSY) {
1788 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1789 ap->id, tmout / HZ);
1796 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1798 struct ata_ioports *ioaddr = &ap->ioaddr;
1799 unsigned int dev0 = devmask & (1 << 0);
1800 unsigned int dev1 = devmask & (1 << 1);
1801 unsigned long timeout;
1803 /* if device 0 was found in ata_devchk, wait for its
1807 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1809 /* if device 1 was found in ata_devchk, wait for
1810 * register access, then wait for BSY to clear
1812 timeout = jiffies + ATA_TMOUT_BOOT;
1816 ap->ops->dev_select(ap, 1);
1817 if (ap->flags & ATA_FLAG_MMIO) {
1818 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1819 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1821 nsect = inb(ioaddr->nsect_addr);
1822 lbal = inb(ioaddr->lbal_addr);
1824 if ((nsect == 1) && (lbal == 1))
1826 if (time_after(jiffies, timeout)) {
1830 msleep(50); /* give drive a breather */
1833 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1835 /* is all this really necessary? */
1836 ap->ops->dev_select(ap, 0);
1838 ap->ops->dev_select(ap, 1);
1840 ap->ops->dev_select(ap, 0);
1844 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1845 * @ap: Port to reset and probe
1847 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1848 * probe the bus. Not often used these days.
1851 * PCI/etc. bus probe sem.
1852 * Obtains host_set lock.
1856 static unsigned int ata_bus_edd(struct ata_port *ap)
1858 struct ata_taskfile tf;
1859 unsigned long flags;
1861 /* set up execute-device-diag (bus reset) taskfile */
1862 /* also, take interrupts to a known state (disabled) */
1863 DPRINTK("execute-device-diag\n");
1864 ata_tf_init(ap, &tf, 0);
1866 tf.command = ATA_CMD_EDD;
1867 tf.protocol = ATA_PROT_NODATA;
1870 spin_lock_irqsave(&ap->host_set->lock, flags);
1871 ata_tf_to_host(ap, &tf);
1872 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1874 /* spec says at least 2ms. but who knows with those
1875 * crazy ATAPI devices...
1879 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1882 static unsigned int ata_bus_softreset(struct ata_port *ap,
1883 unsigned int devmask)
1885 struct ata_ioports *ioaddr = &ap->ioaddr;
1887 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1889 /* software reset. causes dev0 to be selected */
1890 if (ap->flags & ATA_FLAG_MMIO) {
1891 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1892 udelay(20); /* FIXME: flush */
1893 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1894 udelay(20); /* FIXME: flush */
1895 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1897 outb(ap->ctl, ioaddr->ctl_addr);
1899 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1901 outb(ap->ctl, ioaddr->ctl_addr);
1904 /* spec mandates ">= 2ms" before checking status.
1905 * We wait 150ms, because that was the magic delay used for
1906 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1907 * between when the ATA command register is written, and then
1908 * status is checked. Because waiting for "a while" before
1909 * checking status is fine, post SRST, we perform this magic
1910 * delay here as well.
1914 ata_bus_post_reset(ap, devmask);
1920 * ata_bus_reset - reset host port and associated ATA channel
1921 * @ap: port to reset
1923 * This is typically the first time we actually start issuing
1924 * commands to the ATA channel. We wait for BSY to clear, then
1925 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1926 * result. Determine what devices, if any, are on the channel
1927 * by looking at the device 0/1 error register. Look at the signature
1928 * stored in each device's taskfile registers, to determine if
1929 * the device is ATA or ATAPI.
1932 * PCI/etc. bus probe sem.
1933 * Obtains host_set lock.
1936 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1939 void ata_bus_reset(struct ata_port *ap)
1941 struct ata_ioports *ioaddr = &ap->ioaddr;
1942 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1944 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1946 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1948 /* determine if device 0/1 are present */
1949 if (ap->flags & ATA_FLAG_SATA_RESET)
1952 dev0 = ata_devchk(ap, 0);
1954 dev1 = ata_devchk(ap, 1);
1958 devmask |= (1 << 0);
1960 devmask |= (1 << 1);
1962 /* select device 0 again */
1963 ap->ops->dev_select(ap, 0);
1965 /* issue bus reset */
1966 if (ap->flags & ATA_FLAG_SRST)
1967 rc = ata_bus_softreset(ap, devmask);
1968 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1969 /* set up device control */
1970 if (ap->flags & ATA_FLAG_MMIO)
1971 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1973 outb(ap->ctl, ioaddr->ctl_addr);
1974 rc = ata_bus_edd(ap);
1981 * determine by signature whether we have ATA or ATAPI devices
1983 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1984 if ((slave_possible) && (err != 0x81))
1985 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1987 /* re-enable interrupts */
1988 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1991 /* is double-select really necessary? */
1992 if (ap->device[1].class != ATA_DEV_NONE)
1993 ap->ops->dev_select(ap, 1);
1994 if (ap->device[0].class != ATA_DEV_NONE)
1995 ap->ops->dev_select(ap, 0);
1997 /* if no devices were detected, disable this port */
1998 if ((ap->device[0].class == ATA_DEV_NONE) &&
1999 (ap->device[1].class == ATA_DEV_NONE))
2002 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2003 /* set up device control for ATA_FLAG_SATA_RESET */
2004 if (ap->flags & ATA_FLAG_MMIO)
2005 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2007 outb(ap->ctl, ioaddr->ctl_addr);
2014 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2015 ap->ops->port_disable(ap);
2020 static int sata_phy_resume(struct ata_port *ap)
2022 unsigned long timeout = jiffies + (HZ * 5);
2025 scr_write_flush(ap, SCR_CONTROL, 0x300);
2027 /* Wait for phy to become ready, if necessary. */
2030 sstatus = scr_read(ap, SCR_STATUS);
2031 if ((sstatus & 0xf) != 1)
2033 } while (time_before(jiffies, timeout));
2039 * ata_std_probeinit - initialize probing
2040 * @ap: port to be probed
2042 * @ap is about to be probed. Initialize it. This function is
2043 * to be used as standard callback for ata_drive_probe_reset().
2045 * NOTE!!! Do not use this function as probeinit if a low level
2046 * driver implements only hardreset. Just pass NULL as probeinit
2047 * in that case. Using this function is probably okay but doing
2048 * so makes reset sequence different from the original
2049 * ->phy_reset implementation and Jeff nervous. :-P
2051 extern void ata_std_probeinit(struct ata_port *ap)
2053 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2054 sata_phy_resume(ap);
2055 if (sata_dev_present(ap))
2056 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2061 * ata_std_softreset - reset host port via ATA SRST
2062 * @ap: port to reset
2063 * @verbose: fail verbosely
2064 * @classes: resulting classes of attached devices
2066 * Reset host port using ATA SRST. This function is to be used
2067 * as standard callback for ata_drive_*_reset() functions.
2070 * Kernel thread context (may sleep)
2073 * 0 on success, -errno otherwise.
2075 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2077 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2078 unsigned int devmask = 0, err_mask;
2083 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2084 classes[0] = ATA_DEV_NONE;
2088 /* determine if device 0/1 are present */
2089 if (ata_devchk(ap, 0))
2090 devmask |= (1 << 0);
2091 if (slave_possible && ata_devchk(ap, 1))
2092 devmask |= (1 << 1);
2094 /* select device 0 again */
2095 ap->ops->dev_select(ap, 0);
2097 /* issue bus reset */
2098 DPRINTK("about to softreset, devmask=%x\n", devmask);
2099 err_mask = ata_bus_softreset(ap, devmask);
2102 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2105 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2110 /* determine by signature whether we have ATA or ATAPI devices */
2111 classes[0] = ata_dev_try_classify(ap, 0, &err);
2112 if (slave_possible && err != 0x81)
2113 classes[1] = ata_dev_try_classify(ap, 1, &err);
2116 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2121 * sata_std_hardreset - reset host port via SATA phy reset
2122 * @ap: port to reset
2123 * @verbose: fail verbosely
2124 * @class: resulting class of attached device
2126 * SATA phy-reset host port using DET bits of SControl register.
2127 * This function is to be used as standard callback for
2128 * ata_drive_*_reset().
2131 * Kernel thread context (may sleep)
2134 * 0 on success, -errno otherwise.
2136 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2140 /* Issue phy wake/reset */
2141 scr_write_flush(ap, SCR_CONTROL, 0x301);
2144 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2145 * 10.4.2 says at least 1 ms.
2149 /* Bring phy back */
2150 sata_phy_resume(ap);
2152 /* TODO: phy layer with polling, timeouts, etc. */
2153 if (!sata_dev_present(ap)) {
2154 *class = ATA_DEV_NONE;
2155 DPRINTK("EXIT, link offline\n");
2159 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2161 printk(KERN_ERR "ata%u: COMRESET failed "
2162 "(device not ready)\n", ap->id);
2164 DPRINTK("EXIT, device not ready\n");
2168 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2170 *class = ata_dev_try_classify(ap, 0, NULL);
2172 DPRINTK("EXIT, class=%u\n", *class);
2177 * ata_std_postreset - standard postreset callback
2178 * @ap: the target ata_port
2179 * @classes: classes of attached devices
2181 * This function is invoked after a successful reset. Note that
2182 * the device might have been reset more than once using
2183 * different reset methods before postreset is invoked.
2185 * This function is to be used as standard callback for
2186 * ata_drive_*_reset().
2189 * Kernel thread context (may sleep)
2191 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2195 /* set cable type if it isn't already set */
2196 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2197 ap->cbl = ATA_CBL_SATA;
2199 /* print link status */
2200 if (ap->cbl == ATA_CBL_SATA)
2201 sata_print_link_status(ap);
2203 /* re-enable interrupts */
2204 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2207 /* is double-select really necessary? */
2208 if (classes[0] != ATA_DEV_NONE)
2209 ap->ops->dev_select(ap, 1);
2210 if (classes[1] != ATA_DEV_NONE)
2211 ap->ops->dev_select(ap, 0);
2213 /* bail out if no device is present */
2214 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2215 DPRINTK("EXIT, no device\n");
2219 /* set up device control */
2220 if (ap->ioaddr.ctl_addr) {
2221 if (ap->flags & ATA_FLAG_MMIO)
2222 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2224 outb(ap->ctl, ap->ioaddr.ctl_addr);
2231 * ata_std_probe_reset - standard probe reset method
2232 * @ap: prot to perform probe-reset
2233 * @classes: resulting classes of attached devices
2235 * The stock off-the-shelf ->probe_reset method.
2238 * Kernel thread context (may sleep)
2241 * 0 on success, -errno otherwise.
2243 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2245 ata_reset_fn_t hardreset;
2248 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2249 hardreset = sata_std_hardreset;
2251 return ata_drive_probe_reset(ap, ata_std_probeinit,
2252 ata_std_softreset, hardreset,
2253 ata_std_postreset, classes);
2256 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2257 ata_postreset_fn_t postreset,
2258 unsigned int *classes)
2262 for (i = 0; i < ATA_MAX_DEVICES; i++)
2263 classes[i] = ATA_DEV_UNKNOWN;
2265 rc = reset(ap, 0, classes);
2269 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2270 * is complete and convert all ATA_DEV_UNKNOWN to
2273 for (i = 0; i < ATA_MAX_DEVICES; i++)
2274 if (classes[i] != ATA_DEV_UNKNOWN)
2277 if (i < ATA_MAX_DEVICES)
2278 for (i = 0; i < ATA_MAX_DEVICES; i++)
2279 if (classes[i] == ATA_DEV_UNKNOWN)
2280 classes[i] = ATA_DEV_NONE;
2283 postreset(ap, classes);
2285 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2289 * ata_drive_probe_reset - Perform probe reset with given methods
2290 * @ap: port to reset
2291 * @probeinit: probeinit method (can be NULL)
2292 * @softreset: softreset method (can be NULL)
2293 * @hardreset: hardreset method (can be NULL)
2294 * @postreset: postreset method (can be NULL)
2295 * @classes: resulting classes of attached devices
2297 * Reset the specified port and classify attached devices using
2298 * given methods. This function prefers softreset but tries all
2299 * possible reset sequences to reset and classify devices. This
2300 * function is intended to be used for constructing ->probe_reset
2301 * callback by low level drivers.
2303 * Reset methods should follow the following rules.
2305 * - Return 0 on sucess, -errno on failure.
2306 * - If classification is supported, fill classes[] with
2307 * recognized class codes.
2308 * - If classification is not supported, leave classes[] alone.
2309 * - If verbose is non-zero, print error message on failure;
2310 * otherwise, shut up.
2313 * Kernel thread context (may sleep)
2316 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2317 * if classification fails, and any error code from reset
2320 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2321 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2322 ata_postreset_fn_t postreset, unsigned int *classes)
2330 rc = do_probe_reset(ap, softreset, postreset, classes);
2338 rc = do_probe_reset(ap, hardreset, postreset, classes);
2339 if (rc == 0 || rc != -ENODEV)
2343 rc = do_probe_reset(ap, softreset, postreset, classes);
2349 * ata_dev_same_device - Determine whether new ID matches configured device
2350 * @ap: port on which the device to compare against resides
2351 * @dev: device to compare against
2352 * @new_class: class of the new device
2353 * @new_id: IDENTIFY page of the new device
2355 * Compare @new_class and @new_id against @dev and determine
2356 * whether @dev is the device indicated by @new_class and
2363 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2365 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2366 unsigned int new_class, const u16 *new_id)
2368 const u16 *old_id = dev->id;
2369 unsigned char model[2][41], serial[2][21];
2372 if (dev->class != new_class) {
2374 "ata%u: dev %u class mismatch %d != %d\n",
2375 ap->id, dev->devno, dev->class, new_class);
2379 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2380 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2381 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2382 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2383 new_n_sectors = ata_id_n_sectors(new_id);
2385 if (strcmp(model[0], model[1])) {
2387 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2388 ap->id, dev->devno, model[0], model[1]);
2392 if (strcmp(serial[0], serial[1])) {
2394 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2395 ap->id, dev->devno, serial[0], serial[1]);
2399 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2401 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2402 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2403 (unsigned long long)new_n_sectors);
2411 * ata_dev_revalidate - Revalidate ATA device
2412 * @ap: port on which the device to revalidate resides
2413 * @dev: device to revalidate
2414 * @post_reset: is this revalidation after reset?
2416 * Re-read IDENTIFY page and make sure @dev is still attached to
2420 * Kernel thread context (may sleep)
2423 * 0 on success, negative errno otherwise
2425 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2432 if (!ata_dev_present(dev))
2438 /* allocate & read ID data */
2439 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2443 /* is the device still there? */
2444 if (!ata_dev_same_device(ap, dev, class, id)) {
2452 /* configure device according to the new ID */
2453 return ata_dev_configure(ap, dev, 0);
2456 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2457 ap->id, dev->devno, rc);
2462 static void ata_pr_blacklisted(const struct ata_port *ap,
2463 const struct ata_device *dev)
2465 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2466 ap->id, dev->devno);
2469 static const char * const ata_dma_blacklist [] = {
2488 "Toshiba CD-ROM XM-6202B",
2489 "TOSHIBA CD-ROM XM-1702BC",
2491 "E-IDE CD-ROM CR-840",
2494 "SAMSUNG CD-ROM SC-148C",
2495 "SAMSUNG CD-ROM SC",
2497 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2501 static int ata_dma_blacklisted(const struct ata_device *dev)
2503 unsigned char model_num[41];
2506 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2508 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2509 if (!strcmp(ata_dma_blacklist[i], model_num))
2515 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2517 const struct ata_device *master, *slave;
2520 master = &ap->device[0];
2521 slave = &ap->device[1];
2523 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2525 if (shift == ATA_SHIFT_UDMA) {
2526 mask = ap->udma_mask;
2527 if (ata_dev_present(master)) {
2528 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2529 if (ata_dma_blacklisted(master)) {
2531 ata_pr_blacklisted(ap, master);
2534 if (ata_dev_present(slave)) {
2535 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2536 if (ata_dma_blacklisted(slave)) {
2538 ata_pr_blacklisted(ap, slave);
2542 else if (shift == ATA_SHIFT_MWDMA) {
2543 mask = ap->mwdma_mask;
2544 if (ata_dev_present(master)) {
2545 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2546 if (ata_dma_blacklisted(master)) {
2548 ata_pr_blacklisted(ap, master);
2551 if (ata_dev_present(slave)) {
2552 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2553 if (ata_dma_blacklisted(slave)) {
2555 ata_pr_blacklisted(ap, slave);
2559 else if (shift == ATA_SHIFT_PIO) {
2560 mask = ap->pio_mask;
2561 if (ata_dev_present(master)) {
2562 /* spec doesn't return explicit support for
2563 * PIO0-2, so we fake it
2565 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2570 if (ata_dev_present(slave)) {
2571 /* spec doesn't return explicit support for
2572 * PIO0-2, so we fake it
2574 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2581 mask = 0xffffffff; /* shut up compiler warning */
2588 /* find greatest bit */
2589 static int fgb(u32 bitmap)
2594 for (i = 0; i < 32; i++)
2595 if (bitmap & (1 << i))
2602 * ata_choose_xfer_mode - attempt to find best transfer mode
2603 * @ap: Port for which an xfer mode will be selected
2604 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2605 * @xfer_shift_out: (output) bit shift that selects this mode
2607 * Based on host and device capabilities, determine the
2608 * maximum transfer mode that is amenable to all.
2611 * PCI/etc. bus probe sem.
2614 * Zero on success, negative on error.
2617 static int ata_choose_xfer_mode(const struct ata_port *ap,
2619 unsigned int *xfer_shift_out)
2621 unsigned int mask, shift;
2624 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2625 shift = xfer_mode_classes[i].shift;
2626 mask = ata_get_mode_mask(ap, shift);
2630 *xfer_mode_out = xfer_mode_classes[i].base + x;
2631 *xfer_shift_out = shift;
2640 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2641 * @ap: Port associated with device @dev
2642 * @dev: Device to which command will be sent
2644 * Issue SET FEATURES - XFER MODE command to device @dev
2648 * PCI/etc. bus probe sem.
2651 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2653 struct ata_taskfile tf;
2655 /* set up set-features taskfile */
2656 DPRINTK("set features - xfer mode\n");
2658 ata_tf_init(ap, &tf, dev->devno);
2659 tf.command = ATA_CMD_SET_FEATURES;
2660 tf.feature = SETFEATURES_XFER;
2661 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2662 tf.protocol = ATA_PROT_NODATA;
2663 tf.nsect = dev->xfer_mode;
2665 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2666 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2668 ata_port_disable(ap);
2675 * ata_dev_init_params - Issue INIT DEV PARAMS command
2676 * @ap: Port associated with device @dev
2677 * @dev: Device to which command will be sent
2680 * Kernel thread context (may sleep)
2683 * 0 on success, AC_ERR_* mask otherwise.
2686 static unsigned int ata_dev_init_params(struct ata_port *ap,
2687 struct ata_device *dev)
2689 struct ata_taskfile tf;
2690 unsigned int err_mask;
2691 u16 sectors = dev->id[6];
2692 u16 heads = dev->id[3];
2694 /* Number of sectors per track 1-255. Number of heads 1-16 */
2695 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2698 /* set up init dev params taskfile */
2699 DPRINTK("init dev params \n");
2701 ata_tf_init(ap, &tf, dev->devno);
2702 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2703 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2704 tf.protocol = ATA_PROT_NODATA;
2706 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2708 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2710 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2715 * ata_sg_clean - Unmap DMA memory associated with command
2716 * @qc: Command containing DMA memory to be released
2718 * Unmap all mapped DMA memory associated with this command.
2721 * spin_lock_irqsave(host_set lock)
2724 static void ata_sg_clean(struct ata_queued_cmd *qc)
2726 struct ata_port *ap = qc->ap;
2727 struct scatterlist *sg = qc->__sg;
2728 int dir = qc->dma_dir;
2729 void *pad_buf = NULL;
2731 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2732 WARN_ON(sg == NULL);
2734 if (qc->flags & ATA_QCFLAG_SINGLE)
2735 WARN_ON(qc->n_elem > 1);
2737 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2739 /* if we padded the buffer out to 32-bit bound, and data
2740 * xfer direction is from-device, we must copy from the
2741 * pad buffer back into the supplied buffer
2743 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2744 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2746 if (qc->flags & ATA_QCFLAG_SG) {
2748 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2749 /* restore last sg */
2750 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2752 struct scatterlist *psg = &qc->pad_sgent;
2753 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2754 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2755 kunmap_atomic(addr, KM_IRQ0);
2759 dma_unmap_single(ap->host_set->dev,
2760 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2763 sg->length += qc->pad_len;
2765 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2766 pad_buf, qc->pad_len);
2769 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2774 * ata_fill_sg - Fill PCI IDE PRD table
2775 * @qc: Metadata associated with taskfile to be transferred
2777 * Fill PCI IDE PRD (scatter-gather) table with segments
2778 * associated with the current disk command.
2781 * spin_lock_irqsave(host_set lock)
2784 static void ata_fill_sg(struct ata_queued_cmd *qc)
2786 struct ata_port *ap = qc->ap;
2787 struct scatterlist *sg;
2790 WARN_ON(qc->__sg == NULL);
2791 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2794 ata_for_each_sg(sg, qc) {
2798 /* determine if physical DMA addr spans 64K boundary.
2799 * Note h/w doesn't support 64-bit, so we unconditionally
2800 * truncate dma_addr_t to u32.
2802 addr = (u32) sg_dma_address(sg);
2803 sg_len = sg_dma_len(sg);
2806 offset = addr & 0xffff;
2808 if ((offset + sg_len) > 0x10000)
2809 len = 0x10000 - offset;
2811 ap->prd[idx].addr = cpu_to_le32(addr);
2812 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2813 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2822 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2825 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2826 * @qc: Metadata associated with taskfile to check
2828 * Allow low-level driver to filter ATA PACKET commands, returning
2829 * a status indicating whether or not it is OK to use DMA for the
2830 * supplied PACKET command.
2833 * spin_lock_irqsave(host_set lock)
2835 * RETURNS: 0 when ATAPI DMA can be used
2838 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2840 struct ata_port *ap = qc->ap;
2841 int rc = 0; /* Assume ATAPI DMA is OK by default */
2843 if (ap->ops->check_atapi_dma)
2844 rc = ap->ops->check_atapi_dma(qc);
2849 * ata_qc_prep - Prepare taskfile for submission
2850 * @qc: Metadata associated with taskfile to be prepared
2852 * Prepare ATA taskfile for submission.
2855 * spin_lock_irqsave(host_set lock)
2857 void ata_qc_prep(struct ata_queued_cmd *qc)
2859 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2866 * ata_sg_init_one - Associate command with memory buffer
2867 * @qc: Command to be associated
2868 * @buf: Memory buffer
2869 * @buflen: Length of memory buffer, in bytes.
2871 * Initialize the data-related elements of queued_cmd @qc
2872 * to point to a single memory buffer, @buf of byte length @buflen.
2875 * spin_lock_irqsave(host_set lock)
2878 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2880 struct scatterlist *sg;
2882 qc->flags |= ATA_QCFLAG_SINGLE;
2884 memset(&qc->sgent, 0, sizeof(qc->sgent));
2885 qc->__sg = &qc->sgent;
2887 qc->orig_n_elem = 1;
2891 sg_init_one(sg, buf, buflen);
2895 * ata_sg_init - Associate command with scatter-gather table.
2896 * @qc: Command to be associated
2897 * @sg: Scatter-gather table.
2898 * @n_elem: Number of elements in s/g table.
2900 * Initialize the data-related elements of queued_cmd @qc
2901 * to point to a scatter-gather table @sg, containing @n_elem
2905 * spin_lock_irqsave(host_set lock)
2908 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2909 unsigned int n_elem)
2911 qc->flags |= ATA_QCFLAG_SG;
2913 qc->n_elem = n_elem;
2914 qc->orig_n_elem = n_elem;
2918 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2919 * @qc: Command with memory buffer to be mapped.
2921 * DMA-map the memory buffer associated with queued_cmd @qc.
2924 * spin_lock_irqsave(host_set lock)
2927 * Zero on success, negative on error.
2930 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2932 struct ata_port *ap = qc->ap;
2933 int dir = qc->dma_dir;
2934 struct scatterlist *sg = qc->__sg;
2935 dma_addr_t dma_address;
2938 /* we must lengthen transfers to end on a 32-bit boundary */
2939 qc->pad_len = sg->length & 3;
2941 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2942 struct scatterlist *psg = &qc->pad_sgent;
2944 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2946 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2948 if (qc->tf.flags & ATA_TFLAG_WRITE)
2949 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2952 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2953 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2955 sg->length -= qc->pad_len;
2956 if (sg->length == 0)
2959 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2960 sg->length, qc->pad_len);
2968 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2970 if (dma_mapping_error(dma_address)) {
2972 sg->length += qc->pad_len;
2976 sg_dma_address(sg) = dma_address;
2977 sg_dma_len(sg) = sg->length;
2980 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2981 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2987 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2988 * @qc: Command with scatter-gather table to be mapped.
2990 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2993 * spin_lock_irqsave(host_set lock)
2996 * Zero on success, negative on error.
3000 static int ata_sg_setup(struct ata_queued_cmd *qc)
3002 struct ata_port *ap = qc->ap;
3003 struct scatterlist *sg = qc->__sg;
3004 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3005 int n_elem, pre_n_elem, dir, trim_sg = 0;
3007 VPRINTK("ENTER, ata%u\n", ap->id);
3008 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3010 /* we must lengthen transfers to end on a 32-bit boundary */
3011 qc->pad_len = lsg->length & 3;
3013 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3014 struct scatterlist *psg = &qc->pad_sgent;
3015 unsigned int offset;
3017 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3019 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3022 * psg->page/offset are used to copy to-be-written
3023 * data in this function or read data in ata_sg_clean.
3025 offset = lsg->offset + lsg->length - qc->pad_len;
3026 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3027 psg->offset = offset_in_page(offset);
3029 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3030 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3031 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3032 kunmap_atomic(addr, KM_IRQ0);
3035 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3036 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3038 lsg->length -= qc->pad_len;
3039 if (lsg->length == 0)
3042 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3043 qc->n_elem - 1, lsg->length, qc->pad_len);
3046 pre_n_elem = qc->n_elem;
3047 if (trim_sg && pre_n_elem)
3056 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
3058 /* restore last sg */
3059 lsg->length += qc->pad_len;
3063 DPRINTK("%d sg elements mapped\n", n_elem);
3066 qc->n_elem = n_elem;
3072 * ata_poll_qc_complete - turn irq back on and finish qc
3073 * @qc: Command to complete
3074 * @err_mask: ATA status register content
3077 * None. (grabs host lock)
3080 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3082 struct ata_port *ap = qc->ap;
3083 unsigned long flags;
3085 spin_lock_irqsave(&ap->host_set->lock, flags);
3086 ap->flags &= ~ATA_FLAG_NOINTR;
3088 ata_qc_complete(qc);
3089 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3093 * ata_pio_poll - poll using PIO, depending on current state
3094 * @ap: the target ata_port
3097 * None. (executing in kernel thread context)
3100 * timeout value to use
3103 static unsigned long ata_pio_poll(struct ata_port *ap)
3105 struct ata_queued_cmd *qc;
3107 unsigned int poll_state = HSM_ST_UNKNOWN;
3108 unsigned int reg_state = HSM_ST_UNKNOWN;
3110 qc = ata_qc_from_tag(ap, ap->active_tag);
3111 WARN_ON(qc == NULL);
3113 switch (ap->hsm_task_state) {
3116 poll_state = HSM_ST_POLL;
3120 case HSM_ST_LAST_POLL:
3121 poll_state = HSM_ST_LAST_POLL;
3122 reg_state = HSM_ST_LAST;
3129 status = ata_chk_status(ap);
3130 if (status & ATA_BUSY) {
3131 if (time_after(jiffies, ap->pio_task_timeout)) {
3132 qc->err_mask |= AC_ERR_TIMEOUT;
3133 ap->hsm_task_state = HSM_ST_TMOUT;
3136 ap->hsm_task_state = poll_state;
3137 return ATA_SHORT_PAUSE;
3140 ap->hsm_task_state = reg_state;
3145 * ata_pio_complete - check if drive is busy or idle
3146 * @ap: the target ata_port
3149 * None. (executing in kernel thread context)
3152 * Non-zero if qc completed, zero otherwise.
3155 static int ata_pio_complete (struct ata_port *ap)
3157 struct ata_queued_cmd *qc;
3161 * This is purely heuristic. This is a fast path. Sometimes when
3162 * we enter, BSY will be cleared in a chk-status or two. If not,
3163 * the drive is probably seeking or something. Snooze for a couple
3164 * msecs, then chk-status again. If still busy, fall back to
3165 * HSM_ST_POLL state.
3167 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3168 if (drv_stat & ATA_BUSY) {
3170 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3171 if (drv_stat & ATA_BUSY) {
3172 ap->hsm_task_state = HSM_ST_LAST_POLL;
3173 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3178 qc = ata_qc_from_tag(ap, ap->active_tag);
3179 WARN_ON(qc == NULL);
3181 drv_stat = ata_wait_idle(ap);
3182 if (!ata_ok(drv_stat)) {
3183 qc->err_mask |= __ac_err_mask(drv_stat);
3184 ap->hsm_task_state = HSM_ST_ERR;
3188 ap->hsm_task_state = HSM_ST_IDLE;
3190 WARN_ON(qc->err_mask);
3191 ata_poll_qc_complete(qc);
3193 /* another command may start at this point */
3200 * swap_buf_le16 - swap halves of 16-bit words in place
3201 * @buf: Buffer to swap
3202 * @buf_words: Number of 16-bit words in buffer.
3204 * Swap halves of 16-bit words if needed to convert from
3205 * little-endian byte order to native cpu byte order, or
3209 * Inherited from caller.
3211 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3216 for (i = 0; i < buf_words; i++)
3217 buf[i] = le16_to_cpu(buf[i]);
3218 #endif /* __BIG_ENDIAN */
3222 * ata_mmio_data_xfer - Transfer data by MMIO
3223 * @ap: port to read/write
3225 * @buflen: buffer length
3226 * @write_data: read/write
3228 * Transfer data from/to the device data register by MMIO.
3231 * Inherited from caller.
3234 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3235 unsigned int buflen, int write_data)
3238 unsigned int words = buflen >> 1;
3239 u16 *buf16 = (u16 *) buf;
3240 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3242 /* Transfer multiple of 2 bytes */
3244 for (i = 0; i < words; i++)
3245 writew(le16_to_cpu(buf16[i]), mmio);
3247 for (i = 0; i < words; i++)
3248 buf16[i] = cpu_to_le16(readw(mmio));
3251 /* Transfer trailing 1 byte, if any. */
3252 if (unlikely(buflen & 0x01)) {
3253 u16 align_buf[1] = { 0 };
3254 unsigned char *trailing_buf = buf + buflen - 1;
3257 memcpy(align_buf, trailing_buf, 1);
3258 writew(le16_to_cpu(align_buf[0]), mmio);
3260 align_buf[0] = cpu_to_le16(readw(mmio));
3261 memcpy(trailing_buf, align_buf, 1);
3267 * ata_pio_data_xfer - Transfer data by PIO
3268 * @ap: port to read/write
3270 * @buflen: buffer length
3271 * @write_data: read/write
3273 * Transfer data from/to the device data register by PIO.
3276 * Inherited from caller.
3279 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3280 unsigned int buflen, int write_data)
3282 unsigned int words = buflen >> 1;
3284 /* Transfer multiple of 2 bytes */
3286 outsw(ap->ioaddr.data_addr, buf, words);
3288 insw(ap->ioaddr.data_addr, buf, words);
3290 /* Transfer trailing 1 byte, if any. */
3291 if (unlikely(buflen & 0x01)) {
3292 u16 align_buf[1] = { 0 };
3293 unsigned char *trailing_buf = buf + buflen - 1;
3296 memcpy(align_buf, trailing_buf, 1);
3297 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3299 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3300 memcpy(trailing_buf, align_buf, 1);
3306 * ata_data_xfer - Transfer data from/to the data register.
3307 * @ap: port to read/write
3309 * @buflen: buffer length
3310 * @do_write: read/write
3312 * Transfer data from/to the device data register.
3315 * Inherited from caller.
3318 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3319 unsigned int buflen, int do_write)
3321 /* Make the crap hardware pay the costs not the good stuff */
3322 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3323 unsigned long flags;
3324 local_irq_save(flags);
3325 if (ap->flags & ATA_FLAG_MMIO)
3326 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3328 ata_pio_data_xfer(ap, buf, buflen, do_write);
3329 local_irq_restore(flags);
3331 if (ap->flags & ATA_FLAG_MMIO)
3332 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3334 ata_pio_data_xfer(ap, buf, buflen, do_write);
3339 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3340 * @qc: Command on going
3342 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3345 * Inherited from caller.
3348 static void ata_pio_sector(struct ata_queued_cmd *qc)
3350 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3351 struct scatterlist *sg = qc->__sg;
3352 struct ata_port *ap = qc->ap;
3354 unsigned int offset;
3357 if (qc->cursect == (qc->nsect - 1))
3358 ap->hsm_task_state = HSM_ST_LAST;
3360 page = sg[qc->cursg].page;
3361 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3363 /* get the current page and offset */
3364 page = nth_page(page, (offset >> PAGE_SHIFT));
3365 offset %= PAGE_SIZE;
3367 buf = kmap(page) + offset;
3372 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3377 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3379 /* do the actual data transfer */
3380 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3381 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3387 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3388 * @qc: Command on going
3389 * @bytes: number of bytes
3391 * Transfer Transfer data from/to the ATAPI device.
3394 * Inherited from caller.
3398 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3400 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3401 struct scatterlist *sg = qc->__sg;
3402 struct ata_port *ap = qc->ap;
3405 unsigned int offset, count;
3407 if (qc->curbytes + bytes >= qc->nbytes)
3408 ap->hsm_task_state = HSM_ST_LAST;
3411 if (unlikely(qc->cursg >= qc->n_elem)) {
3413 * The end of qc->sg is reached and the device expects
3414 * more data to transfer. In order not to overrun qc->sg
3415 * and fulfill length specified in the byte count register,
3416 * - for read case, discard trailing data from the device
3417 * - for write case, padding zero data to the device
3419 u16 pad_buf[1] = { 0 };
3420 unsigned int words = bytes >> 1;
3423 if (words) /* warning if bytes > 1 */
3424 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3427 for (i = 0; i < words; i++)
3428 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3430 ap->hsm_task_state = HSM_ST_LAST;
3434 sg = &qc->__sg[qc->cursg];
3437 offset = sg->offset + qc->cursg_ofs;
3439 /* get the current page and offset */
3440 page = nth_page(page, (offset >> PAGE_SHIFT));
3441 offset %= PAGE_SIZE;
3443 /* don't overrun current sg */
3444 count = min(sg->length - qc->cursg_ofs, bytes);
3446 /* don't cross page boundaries */
3447 count = min(count, (unsigned int)PAGE_SIZE - offset);
3449 buf = kmap(page) + offset;
3452 qc->curbytes += count;
3453 qc->cursg_ofs += count;
3455 if (qc->cursg_ofs == sg->length) {
3460 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3462 /* do the actual data transfer */
3463 ata_data_xfer(ap, buf, count, do_write);
3472 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3473 * @qc: Command on going
3475 * Transfer Transfer data from/to the ATAPI device.
3478 * Inherited from caller.
3481 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3483 struct ata_port *ap = qc->ap;
3484 struct ata_device *dev = qc->dev;
3485 unsigned int ireason, bc_lo, bc_hi, bytes;
3486 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3488 ap->ops->tf_read(ap, &qc->tf);
3489 ireason = qc->tf.nsect;
3490 bc_lo = qc->tf.lbam;
3491 bc_hi = qc->tf.lbah;
3492 bytes = (bc_hi << 8) | bc_lo;
3494 /* shall be cleared to zero, indicating xfer of data */
3495 if (ireason & (1 << 0))
3498 /* make sure transfer direction matches expected */
3499 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3500 if (do_write != i_write)
3503 __atapi_pio_bytes(qc, bytes);
3508 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3509 ap->id, dev->devno);
3510 qc->err_mask |= AC_ERR_HSM;
3511 ap->hsm_task_state = HSM_ST_ERR;
3515 * ata_pio_block - start PIO on a block
3516 * @ap: the target ata_port
3519 * None. (executing in kernel thread context)
3522 static void ata_pio_block(struct ata_port *ap)
3524 struct ata_queued_cmd *qc;
3528 * This is purely heuristic. This is a fast path.
3529 * Sometimes when we enter, BSY will be cleared in
3530 * a chk-status or two. If not, the drive is probably seeking
3531 * or something. Snooze for a couple msecs, then
3532 * chk-status again. If still busy, fall back to
3533 * HSM_ST_POLL state.
3535 status = ata_busy_wait(ap, ATA_BUSY, 5);
3536 if (status & ATA_BUSY) {
3538 status = ata_busy_wait(ap, ATA_BUSY, 10);
3539 if (status & ATA_BUSY) {
3540 ap->hsm_task_state = HSM_ST_POLL;
3541 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3546 qc = ata_qc_from_tag(ap, ap->active_tag);
3547 WARN_ON(qc == NULL);
3550 if (status & (ATA_ERR | ATA_DF)) {
3551 qc->err_mask |= AC_ERR_DEV;
3552 ap->hsm_task_state = HSM_ST_ERR;
3556 /* transfer data if any */
3557 if (is_atapi_taskfile(&qc->tf)) {
3558 /* DRQ=0 means no more data to transfer */
3559 if ((status & ATA_DRQ) == 0) {
3560 ap->hsm_task_state = HSM_ST_LAST;
3564 atapi_pio_bytes(qc);
3566 /* handle BSY=0, DRQ=0 as error */
3567 if ((status & ATA_DRQ) == 0) {
3568 qc->err_mask |= AC_ERR_HSM;
3569 ap->hsm_task_state = HSM_ST_ERR;
3577 static void ata_pio_error(struct ata_port *ap)
3579 struct ata_queued_cmd *qc;
3581 qc = ata_qc_from_tag(ap, ap->active_tag);
3582 WARN_ON(qc == NULL);
3584 if (qc->tf.command != ATA_CMD_PACKET)
3585 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3587 /* make sure qc->err_mask is available to
3588 * know what's wrong and recover
3590 WARN_ON(qc->err_mask == 0);
3592 ap->hsm_task_state = HSM_ST_IDLE;
3594 ata_poll_qc_complete(qc);
3597 static void ata_pio_task(void *_data)
3599 struct ata_port *ap = _data;
3600 unsigned long timeout;
3607 switch (ap->hsm_task_state) {
3616 qc_completed = ata_pio_complete(ap);
3620 case HSM_ST_LAST_POLL:
3621 timeout = ata_pio_poll(ap);
3631 ata_queue_delayed_pio_task(ap, timeout);
3632 else if (!qc_completed)
3637 * ata_qc_timeout - Handle timeout of queued command
3638 * @qc: Command that timed out
3640 * Some part of the kernel (currently, only the SCSI layer)
3641 * has noticed that the active command on port @ap has not
3642 * completed after a specified length of time. Handle this
3643 * condition by disabling DMA (if necessary) and completing
3644 * transactions, with error if necessary.
3646 * This also handles the case of the "lost interrupt", where
3647 * for some reason (possibly hardware bug, possibly driver bug)
3648 * an interrupt was not delivered to the driver, even though the
3649 * transaction completed successfully.
3652 * Inherited from SCSI layer (none, can sleep)
3655 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3657 struct ata_port *ap = qc->ap;
3658 struct ata_host_set *host_set = ap->host_set;
3659 u8 host_stat = 0, drv_stat;
3660 unsigned long flags;
3664 ata_flush_pio_tasks(ap);
3665 ap->hsm_task_state = HSM_ST_IDLE;
3667 spin_lock_irqsave(&host_set->lock, flags);
3669 switch (qc->tf.protocol) {
3672 case ATA_PROT_ATAPI_DMA:
3673 host_stat = ap->ops->bmdma_status(ap);
3675 /* before we do anything else, clear DMA-Start bit */
3676 ap->ops->bmdma_stop(qc);
3682 drv_stat = ata_chk_status(ap);
3684 /* ack bmdma irq events */
3685 ap->ops->irq_clear(ap);
3687 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3688 ap->id, qc->tf.command, drv_stat, host_stat);
3690 /* complete taskfile transaction */
3691 qc->err_mask |= ac_err_mask(drv_stat);
3695 spin_unlock_irqrestore(&host_set->lock, flags);
3697 ata_eh_qc_complete(qc);
3703 * ata_eng_timeout - Handle timeout of queued command
3704 * @ap: Port on which timed-out command is active
3706 * Some part of the kernel (currently, only the SCSI layer)
3707 * has noticed that the active command on port @ap has not
3708 * completed after a specified length of time. Handle this
3709 * condition by disabling DMA (if necessary) and completing
3710 * transactions, with error if necessary.
3712 * This also handles the case of the "lost interrupt", where
3713 * for some reason (possibly hardware bug, possibly driver bug)
3714 * an interrupt was not delivered to the driver, even though the
3715 * transaction completed successfully.
3718 * Inherited from SCSI layer (none, can sleep)
3721 void ata_eng_timeout(struct ata_port *ap)
3725 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3731 * ata_qc_new - Request an available ATA command, for queueing
3732 * @ap: Port associated with device @dev
3733 * @dev: Device from whom we request an available command structure
3739 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3741 struct ata_queued_cmd *qc = NULL;
3744 for (i = 0; i < ATA_MAX_QUEUE; i++)
3745 if (!test_and_set_bit(i, &ap->qactive)) {
3746 qc = ata_qc_from_tag(ap, i);
3757 * ata_qc_new_init - Request an available ATA command, and initialize it
3758 * @ap: Port associated with device @dev
3759 * @dev: Device from whom we request an available command structure
3765 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3766 struct ata_device *dev)
3768 struct ata_queued_cmd *qc;
3770 qc = ata_qc_new(ap);
3783 * ata_qc_free - free unused ata_queued_cmd
3784 * @qc: Command to complete
3786 * Designed to free unused ata_queued_cmd object
3787 * in case something prevents using it.
3790 * spin_lock_irqsave(host_set lock)
3792 void ata_qc_free(struct ata_queued_cmd *qc)
3794 struct ata_port *ap = qc->ap;
3797 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3801 if (likely(ata_tag_valid(tag))) {
3802 if (tag == ap->active_tag)
3803 ap->active_tag = ATA_TAG_POISON;
3804 qc->tag = ATA_TAG_POISON;
3805 clear_bit(tag, &ap->qactive);
3809 void __ata_qc_complete(struct ata_queued_cmd *qc)
3811 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3812 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3814 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3817 /* atapi: mark qc as inactive to prevent the interrupt handler
3818 * from completing the command twice later, before the error handler
3819 * is called. (when rc != 0 and atapi request sense is needed)
3821 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3823 /* call completion callback */
3824 qc->complete_fn(qc);
3827 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3829 struct ata_port *ap = qc->ap;
3831 switch (qc->tf.protocol) {
3833 case ATA_PROT_ATAPI_DMA:
3836 case ATA_PROT_ATAPI:
3838 case ATA_PROT_PIO_MULT:
3839 if (ap->flags & ATA_FLAG_PIO_DMA)
3852 * ata_qc_issue - issue taskfile to device
3853 * @qc: command to issue to device
3855 * Prepare an ATA command to submission to device.
3856 * This includes mapping the data into a DMA-able
3857 * area, filling in the S/G table, and finally
3858 * writing the taskfile to hardware, starting the command.
3861 * spin_lock_irqsave(host_set lock)
3864 * Zero on success, AC_ERR_* mask on failure
3867 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3869 struct ata_port *ap = qc->ap;
3871 if (ata_should_dma_map(qc)) {
3872 if (qc->flags & ATA_QCFLAG_SG) {
3873 if (ata_sg_setup(qc))
3875 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3876 if (ata_sg_setup_one(qc))
3880 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3883 ap->ops->qc_prep(qc);
3885 qc->ap->active_tag = qc->tag;
3886 qc->flags |= ATA_QCFLAG_ACTIVE;
3888 return ap->ops->qc_issue(qc);
3891 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3892 return AC_ERR_SYSTEM;
3897 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3898 * @qc: command to issue to device
3900 * Using various libata functions and hooks, this function
3901 * starts an ATA command. ATA commands are grouped into
3902 * classes called "protocols", and issuing each type of protocol
3903 * is slightly different.
3905 * May be used as the qc_issue() entry in ata_port_operations.
3908 * spin_lock_irqsave(host_set lock)
3911 * Zero on success, AC_ERR_* mask on failure
3914 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3916 struct ata_port *ap = qc->ap;
3918 ata_dev_select(ap, qc->dev->devno, 1, 0);
3920 switch (qc->tf.protocol) {
3921 case ATA_PROT_NODATA:
3922 ata_tf_to_host(ap, &qc->tf);
3926 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3927 ap->ops->bmdma_setup(qc); /* set up bmdma */
3928 ap->ops->bmdma_start(qc); /* initiate bmdma */
3931 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3932 ata_qc_set_polling(qc);
3933 ata_tf_to_host(ap, &qc->tf);
3934 ap->hsm_task_state = HSM_ST;
3935 ata_queue_pio_task(ap);
3938 case ATA_PROT_ATAPI:
3939 ata_qc_set_polling(qc);
3940 ata_tf_to_host(ap, &qc->tf);
3941 ata_queue_packet_task(ap);
3944 case ATA_PROT_ATAPI_NODATA:
3945 ap->flags |= ATA_FLAG_NOINTR;
3946 ata_tf_to_host(ap, &qc->tf);
3947 ata_queue_packet_task(ap);
3950 case ATA_PROT_ATAPI_DMA:
3951 ap->flags |= ATA_FLAG_NOINTR;
3952 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3953 ap->ops->bmdma_setup(qc); /* set up bmdma */
3954 ata_queue_packet_task(ap);
3959 return AC_ERR_SYSTEM;
3966 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3967 * @qc: Info associated with this ATA transaction.
3970 * spin_lock_irqsave(host_set lock)
3973 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3975 struct ata_port *ap = qc->ap;
3976 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3978 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3980 /* load PRD table addr. */
3981 mb(); /* make sure PRD table writes are visible to controller */
3982 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3984 /* specify data direction, triple-check start bit is clear */
3985 dmactl = readb(mmio + ATA_DMA_CMD);
3986 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3988 dmactl |= ATA_DMA_WR;
3989 writeb(dmactl, mmio + ATA_DMA_CMD);
3991 /* issue r/w command */
3992 ap->ops->exec_command(ap, &qc->tf);
3996 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3997 * @qc: Info associated with this ATA transaction.
4000 * spin_lock_irqsave(host_set lock)
4003 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4005 struct ata_port *ap = qc->ap;
4006 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4009 /* start host DMA transaction */
4010 dmactl = readb(mmio + ATA_DMA_CMD);
4011 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4013 /* Strictly, one may wish to issue a readb() here, to
4014 * flush the mmio write. However, control also passes
4015 * to the hardware at this point, and it will interrupt
4016 * us when we are to resume control. So, in effect,
4017 * we don't care when the mmio write flushes.
4018 * Further, a read of the DMA status register _immediately_
4019 * following the write may not be what certain flaky hardware
4020 * is expected, so I think it is best to not add a readb()
4021 * without first all the MMIO ATA cards/mobos.
4022 * Or maybe I'm just being paranoid.
4027 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4028 * @qc: Info associated with this ATA transaction.
4031 * spin_lock_irqsave(host_set lock)
4034 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4036 struct ata_port *ap = qc->ap;
4037 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4040 /* load PRD table addr. */
4041 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4043 /* specify data direction, triple-check start bit is clear */
4044 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4045 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4047 dmactl |= ATA_DMA_WR;
4048 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4050 /* issue r/w command */
4051 ap->ops->exec_command(ap, &qc->tf);
4055 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4056 * @qc: Info associated with this ATA transaction.
4059 * spin_lock_irqsave(host_set lock)
4062 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4064 struct ata_port *ap = qc->ap;
4067 /* start host DMA transaction */
4068 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4069 outb(dmactl | ATA_DMA_START,
4070 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4075 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4076 * @qc: Info associated with this ATA transaction.
4078 * Writes the ATA_DMA_START flag to the DMA command register.
4080 * May be used as the bmdma_start() entry in ata_port_operations.
4083 * spin_lock_irqsave(host_set lock)
4085 void ata_bmdma_start(struct ata_queued_cmd *qc)
4087 if (qc->ap->flags & ATA_FLAG_MMIO)
4088 ata_bmdma_start_mmio(qc);
4090 ata_bmdma_start_pio(qc);
4095 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4096 * @qc: Info associated with this ATA transaction.
4098 * Writes address of PRD table to device's PRD Table Address
4099 * register, sets the DMA control register, and calls
4100 * ops->exec_command() to start the transfer.
4102 * May be used as the bmdma_setup() entry in ata_port_operations.
4105 * spin_lock_irqsave(host_set lock)
4107 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4109 if (qc->ap->flags & ATA_FLAG_MMIO)
4110 ata_bmdma_setup_mmio(qc);
4112 ata_bmdma_setup_pio(qc);
4117 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4118 * @ap: Port associated with this ATA transaction.
4120 * Clear interrupt and error flags in DMA status register.
4122 * May be used as the irq_clear() entry in ata_port_operations.
4125 * spin_lock_irqsave(host_set lock)
4128 void ata_bmdma_irq_clear(struct ata_port *ap)
4130 if (ap->flags & ATA_FLAG_MMIO) {
4131 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4132 writeb(readb(mmio), mmio);
4134 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4135 outb(inb(addr), addr);
4142 * ata_bmdma_status - Read PCI IDE BMDMA status
4143 * @ap: Port associated with this ATA transaction.
4145 * Read and return BMDMA status register.
4147 * May be used as the bmdma_status() entry in ata_port_operations.
4150 * spin_lock_irqsave(host_set lock)
4153 u8 ata_bmdma_status(struct ata_port *ap)
4156 if (ap->flags & ATA_FLAG_MMIO) {
4157 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4158 host_stat = readb(mmio + ATA_DMA_STATUS);
4160 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4166 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4167 * @qc: Command we are ending DMA for
4169 * Clears the ATA_DMA_START flag in the dma control register
4171 * May be used as the bmdma_stop() entry in ata_port_operations.
4174 * spin_lock_irqsave(host_set lock)
4177 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4179 struct ata_port *ap = qc->ap;
4180 if (ap->flags & ATA_FLAG_MMIO) {
4181 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4183 /* clear start/stop bit */
4184 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4185 mmio + ATA_DMA_CMD);
4187 /* clear start/stop bit */
4188 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4189 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4192 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4193 ata_altstatus(ap); /* dummy read */
4197 * ata_host_intr - Handle host interrupt for given (port, task)
4198 * @ap: Port on which interrupt arrived (possibly...)
4199 * @qc: Taskfile currently active in engine
4201 * Handle host interrupt for given queued command. Currently,
4202 * only DMA interrupts are handled. All other commands are
4203 * handled via polling with interrupts disabled (nIEN bit).
4206 * spin_lock_irqsave(host_set lock)
4209 * One if interrupt was handled, zero if not (shared irq).
4212 inline unsigned int ata_host_intr (struct ata_port *ap,
4213 struct ata_queued_cmd *qc)
4215 u8 status, host_stat;
4217 switch (qc->tf.protocol) {
4220 case ATA_PROT_ATAPI_DMA:
4221 case ATA_PROT_ATAPI:
4222 /* check status of DMA engine */
4223 host_stat = ap->ops->bmdma_status(ap);
4224 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4226 /* if it's not our irq... */
4227 if (!(host_stat & ATA_DMA_INTR))
4230 /* before we do anything else, clear DMA-Start bit */
4231 ap->ops->bmdma_stop(qc);
4235 case ATA_PROT_ATAPI_NODATA:
4236 case ATA_PROT_NODATA:
4237 /* check altstatus */
4238 status = ata_altstatus(ap);
4239 if (status & ATA_BUSY)
4242 /* check main status, clearing INTRQ */
4243 status = ata_chk_status(ap);
4244 if (unlikely(status & ATA_BUSY))
4246 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4247 ap->id, qc->tf.protocol, status);
4249 /* ack bmdma irq events */
4250 ap->ops->irq_clear(ap);
4252 /* complete taskfile transaction */
4253 qc->err_mask |= ac_err_mask(status);
4254 ata_qc_complete(qc);
4261 return 1; /* irq handled */
4264 ap->stats.idle_irq++;
4267 if ((ap->stats.idle_irq % 1000) == 0) {
4269 ata_irq_ack(ap, 0); /* debug trap */
4270 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4273 return 0; /* irq not handled */
4277 * ata_interrupt - Default ATA host interrupt handler
4278 * @irq: irq line (unused)
4279 * @dev_instance: pointer to our ata_host_set information structure
4282 * Default interrupt handler for PCI IDE devices. Calls
4283 * ata_host_intr() for each port that is not disabled.
4286 * Obtains host_set lock during operation.
4289 * IRQ_NONE or IRQ_HANDLED.
4292 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4294 struct ata_host_set *host_set = dev_instance;
4296 unsigned int handled = 0;
4297 unsigned long flags;
4299 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4300 spin_lock_irqsave(&host_set->lock, flags);
4302 for (i = 0; i < host_set->n_ports; i++) {
4303 struct ata_port *ap;
4305 ap = host_set->ports[i];
4307 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4308 struct ata_queued_cmd *qc;
4310 qc = ata_qc_from_tag(ap, ap->active_tag);
4311 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4312 (qc->flags & ATA_QCFLAG_ACTIVE))
4313 handled |= ata_host_intr(ap, qc);
4317 spin_unlock_irqrestore(&host_set->lock, flags);
4319 return IRQ_RETVAL(handled);
4323 * atapi_packet_task - Write CDB bytes to hardware
4324 * @_data: Port to which ATAPI device is attached.
4326 * When device has indicated its readiness to accept
4327 * a CDB, this function is called. Send the CDB.
4328 * If DMA is to be performed, exit immediately.
4329 * Otherwise, we are in polling mode, so poll
4330 * status under operation succeeds or fails.
4333 * Kernel thread context (may sleep)
4336 static void atapi_packet_task(void *_data)
4338 struct ata_port *ap = _data;
4339 struct ata_queued_cmd *qc;
4342 qc = ata_qc_from_tag(ap, ap->active_tag);
4343 WARN_ON(qc == NULL);
4344 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4346 /* sleep-wait for BSY to clear */
4347 DPRINTK("busy wait\n");
4348 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4349 qc->err_mask |= AC_ERR_TIMEOUT;
4353 /* make sure DRQ is set */
4354 status = ata_chk_status(ap);
4355 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4356 qc->err_mask |= AC_ERR_HSM;
4361 DPRINTK("send cdb\n");
4362 WARN_ON(qc->dev->cdb_len < 12);
4364 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4365 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4366 unsigned long flags;
4368 /* Once we're done issuing command and kicking bmdma,
4369 * irq handler takes over. To not lose irq, we need
4370 * to clear NOINTR flag before sending cdb, but
4371 * interrupt handler shouldn't be invoked before we're
4372 * finished. Hence, the following locking.
4374 spin_lock_irqsave(&ap->host_set->lock, flags);
4375 ap->flags &= ~ATA_FLAG_NOINTR;
4376 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4377 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4378 ap->ops->bmdma_start(qc); /* initiate bmdma */
4379 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4381 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4383 /* PIO commands are handled by polling */
4384 ap->hsm_task_state = HSM_ST;
4385 ata_queue_pio_task(ap);
4391 ata_poll_qc_complete(qc);
4396 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4397 * without filling any other registers
4399 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4402 struct ata_taskfile tf;
4405 ata_tf_init(ap, &tf, dev->devno);
4408 tf.flags |= ATA_TFLAG_DEVICE;
4409 tf.protocol = ATA_PROT_NODATA;
4411 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4413 printk(KERN_ERR "%s: ata command failed: %d\n",
4419 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4423 if (!ata_try_flush_cache(dev))
4426 if (ata_id_has_flush_ext(dev->id))
4427 cmd = ATA_CMD_FLUSH_EXT;
4429 cmd = ATA_CMD_FLUSH;
4431 return ata_do_simple_cmd(ap, dev, cmd);
4434 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4436 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4439 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4441 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4445 * ata_device_resume - wakeup a previously suspended devices
4446 * @ap: port the device is connected to
4447 * @dev: the device to resume
4449 * Kick the drive back into action, by sending it an idle immediate
4450 * command and making sure its transfer mode matches between drive
4454 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4456 if (ap->flags & ATA_FLAG_SUSPENDED) {
4457 ap->flags &= ~ATA_FLAG_SUSPENDED;
4460 if (!ata_dev_present(dev))
4462 if (dev->class == ATA_DEV_ATA)
4463 ata_start_drive(ap, dev);
4469 * ata_device_suspend - prepare a device for suspend
4470 * @ap: port the device is connected to
4471 * @dev: the device to suspend
4473 * Flush the cache on the drive, if appropriate, then issue a
4474 * standbynow command.
4476 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4478 if (!ata_dev_present(dev))
4480 if (dev->class == ATA_DEV_ATA)
4481 ata_flush_cache(ap, dev);
4483 ata_standby_drive(ap, dev);
4484 ap->flags |= ATA_FLAG_SUSPENDED;
4489 * ata_port_start - Set port up for dma.
4490 * @ap: Port to initialize
4492 * Called just after data structures for each port are
4493 * initialized. Allocates space for PRD table.
4495 * May be used as the port_start() entry in ata_port_operations.
4498 * Inherited from caller.
4501 int ata_port_start (struct ata_port *ap)
4503 struct device *dev = ap->host_set->dev;
4506 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4510 rc = ata_pad_alloc(ap, dev);
4512 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4516 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4523 * ata_port_stop - Undo ata_port_start()
4524 * @ap: Port to shut down
4526 * Frees the PRD table.
4528 * May be used as the port_stop() entry in ata_port_operations.
4531 * Inherited from caller.
4534 void ata_port_stop (struct ata_port *ap)
4536 struct device *dev = ap->host_set->dev;
4538 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4539 ata_pad_free(ap, dev);
4542 void ata_host_stop (struct ata_host_set *host_set)
4544 if (host_set->mmio_base)
4545 iounmap(host_set->mmio_base);
4550 * ata_host_remove - Unregister SCSI host structure with upper layers
4551 * @ap: Port to unregister
4552 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4555 * Inherited from caller.
4558 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4560 struct Scsi_Host *sh = ap->host;
4565 scsi_remove_host(sh);
4567 ap->ops->port_stop(ap);
4571 * ata_host_init - Initialize an ata_port structure
4572 * @ap: Structure to initialize
4573 * @host: associated SCSI mid-layer structure
4574 * @host_set: Collection of hosts to which @ap belongs
4575 * @ent: Probe information provided by low-level driver
4576 * @port_no: Port number associated with this ata_port
4578 * Initialize a new ata_port structure, and its associated
4582 * Inherited from caller.
4585 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4586 struct ata_host_set *host_set,
4587 const struct ata_probe_ent *ent, unsigned int port_no)
4593 host->max_channel = 1;
4594 host->unique_id = ata_unique_id++;
4595 host->max_cmd_len = 12;
4597 ap->flags = ATA_FLAG_PORT_DISABLED;
4598 ap->id = host->unique_id;
4600 ap->ctl = ATA_DEVCTL_OBS;
4601 ap->host_set = host_set;
4602 ap->port_no = port_no;
4604 ent->legacy_mode ? ent->hard_port_no : port_no;
4605 ap->pio_mask = ent->pio_mask;
4606 ap->mwdma_mask = ent->mwdma_mask;
4607 ap->udma_mask = ent->udma_mask;
4608 ap->flags |= ent->host_flags;
4609 ap->ops = ent->port_ops;
4610 ap->cbl = ATA_CBL_NONE;
4611 ap->active_tag = ATA_TAG_POISON;
4612 ap->last_ctl = 0xFF;
4614 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4615 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4616 INIT_LIST_HEAD(&ap->eh_done_q);
4618 for (i = 0; i < ATA_MAX_DEVICES; i++)
4619 ap->device[i].devno = i;
4622 ap->stats.unhandled_irq = 1;
4623 ap->stats.idle_irq = 1;
4626 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4630 * ata_host_add - Attach low-level ATA driver to system
4631 * @ent: Information provided by low-level driver
4632 * @host_set: Collections of ports to which we add
4633 * @port_no: Port number associated with this host
4635 * Attach low-level ATA driver to system.
4638 * PCI/etc. bus probe sem.
4641 * New ata_port on success, for NULL on error.
4644 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4645 struct ata_host_set *host_set,
4646 unsigned int port_no)
4648 struct Scsi_Host *host;
4649 struct ata_port *ap;
4653 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4657 ap = (struct ata_port *) &host->hostdata[0];
4659 ata_host_init(ap, host, host_set, ent, port_no);
4661 rc = ap->ops->port_start(ap);
4668 scsi_host_put(host);
4673 * ata_device_add - Register hardware device with ATA and SCSI layers
4674 * @ent: Probe information describing hardware device to be registered
4676 * This function processes the information provided in the probe
4677 * information struct @ent, allocates the necessary ATA and SCSI
4678 * host information structures, initializes them, and registers
4679 * everything with requisite kernel subsystems.
4681 * This function requests irqs, probes the ATA bus, and probes
4685 * PCI/etc. bus probe sem.
4688 * Number of ports registered. Zero on error (no ports registered).
4691 int ata_device_add(const struct ata_probe_ent *ent)
4693 unsigned int count = 0, i;
4694 struct device *dev = ent->dev;
4695 struct ata_host_set *host_set;
4698 /* alloc a container for our list of ATA ports (buses) */
4699 host_set = kzalloc(sizeof(struct ata_host_set) +
4700 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4703 spin_lock_init(&host_set->lock);
4705 host_set->dev = dev;
4706 host_set->n_ports = ent->n_ports;
4707 host_set->irq = ent->irq;
4708 host_set->mmio_base = ent->mmio_base;
4709 host_set->private_data = ent->private_data;
4710 host_set->ops = ent->port_ops;
4712 /* register each port bound to this device */
4713 for (i = 0; i < ent->n_ports; i++) {
4714 struct ata_port *ap;
4715 unsigned long xfer_mode_mask;
4717 ap = ata_host_add(ent, host_set, i);
4721 host_set->ports[i] = ap;
4722 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4723 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4724 (ap->pio_mask << ATA_SHIFT_PIO);
4726 /* print per-port info to dmesg */
4727 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4728 "bmdma 0x%lX irq %lu\n",
4730 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4731 ata_mode_string(xfer_mode_mask),
4732 ap->ioaddr.cmd_addr,
4733 ap->ioaddr.ctl_addr,
4734 ap->ioaddr.bmdma_addr,
4738 host_set->ops->irq_clear(ap);
4745 /* obtain irq, that is shared between channels */
4746 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4747 DRV_NAME, host_set))
4750 /* perform each probe synchronously */
4751 DPRINTK("probe begin\n");
4752 for (i = 0; i < count; i++) {
4753 struct ata_port *ap;
4756 ap = host_set->ports[i];
4758 DPRINTK("ata%u: bus probe begin\n", ap->id);
4759 rc = ata_bus_probe(ap);
4760 DPRINTK("ata%u: bus probe end\n", ap->id);
4763 /* FIXME: do something useful here?
4764 * Current libata behavior will
4765 * tear down everything when
4766 * the module is removed
4767 * or the h/w is unplugged.
4771 rc = scsi_add_host(ap->host, dev);
4773 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4775 /* FIXME: do something useful here */
4776 /* FIXME: handle unconditional calls to
4777 * scsi_scan_host and ata_host_remove, below,
4783 /* probes are done, now scan each port's disk(s) */
4784 DPRINTK("host probe begin\n");
4785 for (i = 0; i < count; i++) {
4786 struct ata_port *ap = host_set->ports[i];
4788 ata_scsi_scan_host(ap);
4791 dev_set_drvdata(dev, host_set);
4793 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4794 return ent->n_ports; /* success */
4797 for (i = 0; i < count; i++) {
4798 ata_host_remove(host_set->ports[i], 1);
4799 scsi_host_put(host_set->ports[i]->host);
4803 VPRINTK("EXIT, returning 0\n");
4808 * ata_host_set_remove - PCI layer callback for device removal
4809 * @host_set: ATA host set that was removed
4811 * Unregister all objects associated with this host set. Free those
4815 * Inherited from calling layer (may sleep).
4818 void ata_host_set_remove(struct ata_host_set *host_set)
4820 struct ata_port *ap;
4823 for (i = 0; i < host_set->n_ports; i++) {
4824 ap = host_set->ports[i];
4825 scsi_remove_host(ap->host);
4828 free_irq(host_set->irq, host_set);
4830 for (i = 0; i < host_set->n_ports; i++) {
4831 ap = host_set->ports[i];
4833 ata_scsi_release(ap->host);
4835 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4836 struct ata_ioports *ioaddr = &ap->ioaddr;
4838 if (ioaddr->cmd_addr == 0x1f0)
4839 release_region(0x1f0, 8);
4840 else if (ioaddr->cmd_addr == 0x170)
4841 release_region(0x170, 8);
4844 scsi_host_put(ap->host);
4847 if (host_set->ops->host_stop)
4848 host_set->ops->host_stop(host_set);
4854 * ata_scsi_release - SCSI layer callback hook for host unload
4855 * @host: libata host to be unloaded
4857 * Performs all duties necessary to shut down a libata port...
4858 * Kill port kthread, disable port, and release resources.
4861 * Inherited from SCSI layer.
4867 int ata_scsi_release(struct Scsi_Host *host)
4869 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4874 ap->ops->port_disable(ap);
4875 ata_host_remove(ap, 0);
4876 for (i = 0; i < ATA_MAX_DEVICES; i++)
4877 kfree(ap->device[i].id);
4884 * ata_std_ports - initialize ioaddr with standard port offsets.
4885 * @ioaddr: IO address structure to be initialized
4887 * Utility function which initializes data_addr, error_addr,
4888 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4889 * device_addr, status_addr, and command_addr to standard offsets
4890 * relative to cmd_addr.
4892 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4895 void ata_std_ports(struct ata_ioports *ioaddr)
4897 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4898 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4899 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4900 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4901 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4902 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4903 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4904 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4905 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4906 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4912 void ata_pci_host_stop (struct ata_host_set *host_set)
4914 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4916 pci_iounmap(pdev, host_set->mmio_base);
4920 * ata_pci_remove_one - PCI layer callback for device removal
4921 * @pdev: PCI device that was removed
4923 * PCI layer indicates to libata via this hook that
4924 * hot-unplug or module unload event has occurred.
4925 * Handle this by unregistering all objects associated
4926 * with this PCI device. Free those objects. Then finally
4927 * release PCI resources and disable device.
4930 * Inherited from PCI layer (may sleep).
4933 void ata_pci_remove_one (struct pci_dev *pdev)
4935 struct device *dev = pci_dev_to_dev(pdev);
4936 struct ata_host_set *host_set = dev_get_drvdata(dev);
4938 ata_host_set_remove(host_set);
4939 pci_release_regions(pdev);
4940 pci_disable_device(pdev);
4941 dev_set_drvdata(dev, NULL);
4944 /* move to PCI subsystem */
4945 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4947 unsigned long tmp = 0;
4949 switch (bits->width) {
4952 pci_read_config_byte(pdev, bits->reg, &tmp8);
4958 pci_read_config_word(pdev, bits->reg, &tmp16);
4964 pci_read_config_dword(pdev, bits->reg, &tmp32);
4975 return (tmp == bits->val) ? 1 : 0;
4978 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4980 pci_save_state(pdev);
4981 pci_disable_device(pdev);
4982 pci_set_power_state(pdev, PCI_D3hot);
4986 int ata_pci_device_resume(struct pci_dev *pdev)
4988 pci_set_power_state(pdev, PCI_D0);
4989 pci_restore_state(pdev);
4990 pci_enable_device(pdev);
4991 pci_set_master(pdev);
4994 #endif /* CONFIG_PCI */
4997 static int __init ata_init(void)
4999 ata_wq = create_workqueue("ata");
5003 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5007 static void __exit ata_exit(void)
5009 destroy_workqueue(ata_wq);
5012 module_init(ata_init);
5013 module_exit(ata_exit);
5015 static unsigned long ratelimit_time;
5016 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5018 int ata_ratelimit(void)
5021 unsigned long flags;
5023 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5025 if (time_after(jiffies, ratelimit_time)) {
5027 ratelimit_time = jiffies + (HZ/5);
5031 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5037 * libata is essentially a library of internal helper functions for
5038 * low-level ATA host controller drivers. As such, the API/ABI is
5039 * likely to change as new drivers are added and updated.
5040 * Do not depend on ABI/API stability.
5043 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5044 EXPORT_SYMBOL_GPL(ata_std_ports);
5045 EXPORT_SYMBOL_GPL(ata_device_add);
5046 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5047 EXPORT_SYMBOL_GPL(ata_sg_init);
5048 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5049 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5050 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5051 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5052 EXPORT_SYMBOL_GPL(ata_tf_load);
5053 EXPORT_SYMBOL_GPL(ata_tf_read);
5054 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5055 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5056 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5057 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5058 EXPORT_SYMBOL_GPL(ata_check_status);
5059 EXPORT_SYMBOL_GPL(ata_altstatus);
5060 EXPORT_SYMBOL_GPL(ata_exec_command);
5061 EXPORT_SYMBOL_GPL(ata_port_start);
5062 EXPORT_SYMBOL_GPL(ata_port_stop);
5063 EXPORT_SYMBOL_GPL(ata_host_stop);
5064 EXPORT_SYMBOL_GPL(ata_interrupt);
5065 EXPORT_SYMBOL_GPL(ata_qc_prep);
5066 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5067 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5068 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5069 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5070 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5071 EXPORT_SYMBOL_GPL(ata_port_probe);
5072 EXPORT_SYMBOL_GPL(sata_phy_reset);
5073 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5074 EXPORT_SYMBOL_GPL(ata_bus_reset);
5075 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5076 EXPORT_SYMBOL_GPL(ata_std_softreset);
5077 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5078 EXPORT_SYMBOL_GPL(ata_std_postreset);
5079 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5080 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5081 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5082 EXPORT_SYMBOL_GPL(ata_port_disable);
5083 EXPORT_SYMBOL_GPL(ata_ratelimit);
5084 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5085 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5086 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5087 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
5088 EXPORT_SYMBOL_GPL(ata_scsi_error);
5089 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5090 EXPORT_SYMBOL_GPL(ata_scsi_release);
5091 EXPORT_SYMBOL_GPL(ata_host_intr);
5092 EXPORT_SYMBOL_GPL(ata_dev_classify);
5093 EXPORT_SYMBOL_GPL(ata_id_string);
5094 EXPORT_SYMBOL_GPL(ata_id_c_string);
5095 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5096 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5097 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5099 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5100 EXPORT_SYMBOL_GPL(ata_timing_compute);
5101 EXPORT_SYMBOL_GPL(ata_timing_merge);
5104 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5105 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5106 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5107 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5108 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5109 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5110 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5111 #endif /* CONFIG_PCI */
5113 EXPORT_SYMBOL_GPL(ata_device_suspend);
5114 EXPORT_SYMBOL_GPL(ata_device_resume);
5115 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5116 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);