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
1067 * Configure @dev according to @dev->id. Generic and low-level
1068 * driver specific fixups are also applied.
1071 * Kernel thread context (may sleep)
1074 * 0 on success, -errno otherwise
1076 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev)
1078 unsigned long xfer_modes;
1081 if (!ata_dev_present(dev)) {
1082 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1083 ap->id, dev->devno);
1087 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1089 /* initialize to-be-configured parameters */
1091 dev->max_sectors = 0;
1099 * common ATA, ATAPI feature tests
1102 /* we require DMA support (bits 8 of word 49) */
1103 if (!ata_id_has_dma(dev->id)) {
1104 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1109 /* quick-n-dirty find max transfer mode; for printk only */
1110 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1112 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1114 xfer_modes = ata_pio_modes(dev);
1116 ata_dump_id(dev->id);
1118 /* ATA-specific feature tests */
1119 if (dev->class == ATA_DEV_ATA) {
1120 dev->n_sectors = ata_id_n_sectors(dev->id);
1122 if (ata_id_has_lba(dev->id)) {
1123 dev->flags |= ATA_DFLAG_LBA;
1125 if (ata_id_has_lba48(dev->id))
1126 dev->flags |= ATA_DFLAG_LBA48;
1128 /* print device info to dmesg */
1129 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1131 ata_id_major_version(dev->id),
1132 ata_mode_string(xfer_modes),
1133 (unsigned long long)dev->n_sectors,
1134 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1138 /* Default translation */
1139 dev->cylinders = dev->id[1];
1140 dev->heads = dev->id[3];
1141 dev->sectors = dev->id[6];
1143 if (ata_id_current_chs_valid(dev->id)) {
1144 /* Current CHS translation is valid. */
1145 dev->cylinders = dev->id[54];
1146 dev->heads = dev->id[55];
1147 dev->sectors = dev->id[56];
1150 /* print device info to dmesg */
1151 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1153 ata_id_major_version(dev->id),
1154 ata_mode_string(xfer_modes),
1155 (unsigned long long)dev->n_sectors,
1156 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1163 /* ATAPI-specific feature tests */
1164 else if (dev->class == ATA_DEV_ATAPI) {
1165 rc = atapi_cdb_len(dev->id);
1166 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1167 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1171 dev->cdb_len = (unsigned int) rc;
1173 /* print device info to dmesg */
1174 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1176 ata_mode_string(xfer_modes));
1179 ap->host->max_cmd_len = 0;
1180 for (i = 0; i < ATA_MAX_DEVICES; i++)
1181 ap->host->max_cmd_len = max_t(unsigned int,
1182 ap->host->max_cmd_len,
1183 ap->device[i].cdb_len);
1185 /* limit bridge transfers to udma5, 200 sectors */
1186 if (ata_dev_knobble(ap, dev)) {
1187 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1188 ap->id, dev->devno);
1189 ap->udma_mask &= ATA_UDMA5;
1190 dev->max_sectors = ATA_MAX_SECTORS;
1193 if (ap->ops->dev_config)
1194 ap->ops->dev_config(ap, dev);
1196 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1200 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1201 ap->id, dev->devno);
1202 DPRINTK("EXIT, err\n");
1207 * ata_bus_probe - Reset and probe ATA bus
1210 * Master ATA bus probing function. Initiates a hardware-dependent
1211 * bus reset, then attempts to identify any devices found on
1215 * PCI/etc. bus probe sem.
1218 * Zero on success, non-zero on error.
1221 static int ata_bus_probe(struct ata_port *ap)
1223 unsigned int classes[ATA_MAX_DEVICES];
1224 unsigned int i, rc, found = 0;
1229 if (ap->ops->probe_reset) {
1230 rc = ap->ops->probe_reset(ap, classes);
1232 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1236 for (i = 0; i < ATA_MAX_DEVICES; i++)
1237 if (classes[i] == ATA_DEV_UNKNOWN)
1238 classes[i] = ATA_DEV_NONE;
1240 ap->ops->phy_reset(ap);
1242 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1243 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1244 classes[i] = ap->device[i].class;
1246 ap->device[i].class = ATA_DEV_UNKNOWN;
1251 /* read IDENTIFY page and configure devices */
1252 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1253 struct ata_device *dev = &ap->device[i];
1255 dev->class = classes[i];
1257 if (!ata_dev_present(dev))
1260 WARN_ON(dev->id != NULL);
1261 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1262 dev->class = ATA_DEV_NONE;
1266 if (ata_dev_configure(ap, dev)) {
1267 dev->class++; /* disable device */
1275 goto err_out_disable;
1278 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1279 goto err_out_disable;
1284 ap->ops->port_disable(ap);
1289 * ata_port_probe - Mark port as enabled
1290 * @ap: Port for which we indicate enablement
1292 * Modify @ap data structure such that the system
1293 * thinks that the entire port is enabled.
1295 * LOCKING: host_set lock, or some other form of
1299 void ata_port_probe(struct ata_port *ap)
1301 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1305 * sata_print_link_status - Print SATA link status
1306 * @ap: SATA port to printk link status about
1308 * This function prints link speed and status of a SATA link.
1313 static void sata_print_link_status(struct ata_port *ap)
1318 if (!ap->ops->scr_read)
1321 sstatus = scr_read(ap, SCR_STATUS);
1323 if (sata_dev_present(ap)) {
1324 tmp = (sstatus >> 4) & 0xf;
1327 else if (tmp & (1 << 1))
1330 speed = "<unknown>";
1331 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1332 ap->id, speed, sstatus);
1334 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1340 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1341 * @ap: SATA port associated with target SATA PHY.
1343 * This function issues commands to standard SATA Sxxx
1344 * PHY registers, to wake up the phy (and device), and
1345 * clear any reset condition.
1348 * PCI/etc. bus probe sem.
1351 void __sata_phy_reset(struct ata_port *ap)
1354 unsigned long timeout = jiffies + (HZ * 5);
1356 if (ap->flags & ATA_FLAG_SATA_RESET) {
1357 /* issue phy wake/reset */
1358 scr_write_flush(ap, SCR_CONTROL, 0x301);
1359 /* Couldn't find anything in SATA I/II specs, but
1360 * AHCI-1.1 10.4.2 says at least 1 ms. */
1363 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1365 /* wait for phy to become ready, if necessary */
1368 sstatus = scr_read(ap, SCR_STATUS);
1369 if ((sstatus & 0xf) != 1)
1371 } while (time_before(jiffies, timeout));
1373 /* print link status */
1374 sata_print_link_status(ap);
1376 /* TODO: phy layer with polling, timeouts, etc. */
1377 if (sata_dev_present(ap))
1380 ata_port_disable(ap);
1382 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1385 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1386 ata_port_disable(ap);
1390 ap->cbl = ATA_CBL_SATA;
1394 * sata_phy_reset - Reset SATA bus.
1395 * @ap: SATA port associated with target SATA PHY.
1397 * This function resets the SATA bus, and then probes
1398 * the bus for devices.
1401 * PCI/etc. bus probe sem.
1404 void sata_phy_reset(struct ata_port *ap)
1406 __sata_phy_reset(ap);
1407 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1413 * ata_port_disable - Disable port.
1414 * @ap: Port to be disabled.
1416 * Modify @ap data structure such that the system
1417 * thinks that the entire port is disabled, and should
1418 * never attempt to probe or communicate with devices
1421 * LOCKING: host_set lock, or some other form of
1425 void ata_port_disable(struct ata_port *ap)
1427 ap->device[0].class = ATA_DEV_NONE;
1428 ap->device[1].class = ATA_DEV_NONE;
1429 ap->flags |= ATA_FLAG_PORT_DISABLED;
1433 * This mode timing computation functionality is ported over from
1434 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1437 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1438 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1439 * for PIO 5, which is a nonstandard extension and UDMA6, which
1440 * is currently supported only by Maxtor drives.
1443 static const struct ata_timing ata_timing[] = {
1445 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1446 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1447 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1448 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1450 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1451 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1452 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1454 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1456 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1457 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1458 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1460 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1461 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1462 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1464 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1465 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1466 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1468 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1469 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1470 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1472 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1477 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1478 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1480 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1482 q->setup = EZ(t->setup * 1000, T);
1483 q->act8b = EZ(t->act8b * 1000, T);
1484 q->rec8b = EZ(t->rec8b * 1000, T);
1485 q->cyc8b = EZ(t->cyc8b * 1000, T);
1486 q->active = EZ(t->active * 1000, T);
1487 q->recover = EZ(t->recover * 1000, T);
1488 q->cycle = EZ(t->cycle * 1000, T);
1489 q->udma = EZ(t->udma * 1000, UT);
1492 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1493 struct ata_timing *m, unsigned int what)
1495 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1496 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1497 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1498 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1499 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1500 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1501 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1502 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1505 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1507 const struct ata_timing *t;
1509 for (t = ata_timing; t->mode != speed; t++)
1510 if (t->mode == 0xFF)
1515 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1516 struct ata_timing *t, int T, int UT)
1518 const struct ata_timing *s;
1519 struct ata_timing p;
1525 if (!(s = ata_timing_find_mode(speed)))
1528 memcpy(t, s, sizeof(*s));
1531 * If the drive is an EIDE drive, it can tell us it needs extended
1532 * PIO/MW_DMA cycle timing.
1535 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1536 memset(&p, 0, sizeof(p));
1537 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1538 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1539 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1540 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1541 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1543 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1547 * Convert the timing to bus clock counts.
1550 ata_timing_quantize(t, t, T, UT);
1553 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1554 * S.M.A.R.T * and some other commands. We have to ensure that the
1555 * DMA cycle timing is slower/equal than the fastest PIO timing.
1558 if (speed > XFER_PIO_4) {
1559 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1560 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1564 * Lengthen active & recovery time so that cycle time is correct.
1567 if (t->act8b + t->rec8b < t->cyc8b) {
1568 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1569 t->rec8b = t->cyc8b - t->act8b;
1572 if (t->active + t->recover < t->cycle) {
1573 t->active += (t->cycle - (t->active + t->recover)) / 2;
1574 t->recover = t->cycle - t->active;
1580 static const struct {
1583 } xfer_mode_classes[] = {
1584 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1585 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1586 { ATA_SHIFT_PIO, XFER_PIO_0 },
1589 static u8 base_from_shift(unsigned int shift)
1593 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1594 if (xfer_mode_classes[i].shift == shift)
1595 return xfer_mode_classes[i].base;
1600 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1605 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1608 if (dev->xfer_shift == ATA_SHIFT_PIO)
1609 dev->flags |= ATA_DFLAG_PIO;
1611 ata_dev_set_xfermode(ap, dev);
1613 base = base_from_shift(dev->xfer_shift);
1614 ofs = dev->xfer_mode - base;
1615 idx = ofs + dev->xfer_shift;
1616 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1618 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1619 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1621 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1622 ap->id, dev->devno, xfer_mode_str[idx]);
1625 static int ata_host_set_pio(struct ata_port *ap)
1631 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1634 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1638 base = base_from_shift(ATA_SHIFT_PIO);
1639 xfer_mode = base + x;
1641 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1642 (int)base, (int)xfer_mode, mask, x);
1644 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1645 struct ata_device *dev = &ap->device[i];
1646 if (ata_dev_present(dev)) {
1647 dev->pio_mode = xfer_mode;
1648 dev->xfer_mode = xfer_mode;
1649 dev->xfer_shift = ATA_SHIFT_PIO;
1650 if (ap->ops->set_piomode)
1651 ap->ops->set_piomode(ap, dev);
1658 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1659 unsigned int xfer_shift)
1663 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1664 struct ata_device *dev = &ap->device[i];
1665 if (ata_dev_present(dev)) {
1666 dev->dma_mode = xfer_mode;
1667 dev->xfer_mode = xfer_mode;
1668 dev->xfer_shift = xfer_shift;
1669 if (ap->ops->set_dmamode)
1670 ap->ops->set_dmamode(ap, dev);
1676 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1677 * @ap: port on which timings will be programmed
1679 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1682 * PCI/etc. bus probe sem.
1684 static void ata_set_mode(struct ata_port *ap)
1686 unsigned int xfer_shift;
1690 /* step 1: always set host PIO timings */
1691 rc = ata_host_set_pio(ap);
1695 /* step 2: choose the best data xfer mode */
1696 xfer_mode = xfer_shift = 0;
1697 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1701 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1702 if (xfer_shift != ATA_SHIFT_PIO)
1703 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1705 /* step 4: update devices' xfer mode */
1706 ata_dev_set_mode(ap, &ap->device[0]);
1707 ata_dev_set_mode(ap, &ap->device[1]);
1709 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1712 if (ap->ops->post_set_mode)
1713 ap->ops->post_set_mode(ap);
1718 ata_port_disable(ap);
1722 * ata_tf_to_host - issue ATA taskfile to host controller
1723 * @ap: port to which command is being issued
1724 * @tf: ATA taskfile register set
1726 * Issues ATA taskfile register set to ATA host controller,
1727 * with proper synchronization with interrupt handler and
1731 * spin_lock_irqsave(host_set lock)
1734 static inline void ata_tf_to_host(struct ata_port *ap,
1735 const struct ata_taskfile *tf)
1737 ap->ops->tf_load(ap, tf);
1738 ap->ops->exec_command(ap, tf);
1742 * ata_busy_sleep - sleep until BSY clears, or timeout
1743 * @ap: port containing status register to be polled
1744 * @tmout_pat: impatience timeout
1745 * @tmout: overall timeout
1747 * Sleep until ATA Status register bit BSY clears,
1748 * or a timeout occurs.
1753 unsigned int ata_busy_sleep (struct ata_port *ap,
1754 unsigned long tmout_pat, unsigned long tmout)
1756 unsigned long timer_start, timeout;
1759 status = ata_busy_wait(ap, ATA_BUSY, 300);
1760 timer_start = jiffies;
1761 timeout = timer_start + tmout_pat;
1762 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1764 status = ata_busy_wait(ap, ATA_BUSY, 3);
1767 if (status & ATA_BUSY)
1768 printk(KERN_WARNING "ata%u is slow to respond, "
1769 "please be patient\n", ap->id);
1771 timeout = timer_start + tmout;
1772 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1774 status = ata_chk_status(ap);
1777 if (status & ATA_BUSY) {
1778 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1779 ap->id, tmout / HZ);
1786 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1788 struct ata_ioports *ioaddr = &ap->ioaddr;
1789 unsigned int dev0 = devmask & (1 << 0);
1790 unsigned int dev1 = devmask & (1 << 1);
1791 unsigned long timeout;
1793 /* if device 0 was found in ata_devchk, wait for its
1797 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1799 /* if device 1 was found in ata_devchk, wait for
1800 * register access, then wait for BSY to clear
1802 timeout = jiffies + ATA_TMOUT_BOOT;
1806 ap->ops->dev_select(ap, 1);
1807 if (ap->flags & ATA_FLAG_MMIO) {
1808 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1809 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1811 nsect = inb(ioaddr->nsect_addr);
1812 lbal = inb(ioaddr->lbal_addr);
1814 if ((nsect == 1) && (lbal == 1))
1816 if (time_after(jiffies, timeout)) {
1820 msleep(50); /* give drive a breather */
1823 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1825 /* is all this really necessary? */
1826 ap->ops->dev_select(ap, 0);
1828 ap->ops->dev_select(ap, 1);
1830 ap->ops->dev_select(ap, 0);
1834 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1835 * @ap: Port to reset and probe
1837 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1838 * probe the bus. Not often used these days.
1841 * PCI/etc. bus probe sem.
1842 * Obtains host_set lock.
1846 static unsigned int ata_bus_edd(struct ata_port *ap)
1848 struct ata_taskfile tf;
1849 unsigned long flags;
1851 /* set up execute-device-diag (bus reset) taskfile */
1852 /* also, take interrupts to a known state (disabled) */
1853 DPRINTK("execute-device-diag\n");
1854 ata_tf_init(ap, &tf, 0);
1856 tf.command = ATA_CMD_EDD;
1857 tf.protocol = ATA_PROT_NODATA;
1860 spin_lock_irqsave(&ap->host_set->lock, flags);
1861 ata_tf_to_host(ap, &tf);
1862 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1864 /* spec says at least 2ms. but who knows with those
1865 * crazy ATAPI devices...
1869 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1872 static unsigned int ata_bus_softreset(struct ata_port *ap,
1873 unsigned int devmask)
1875 struct ata_ioports *ioaddr = &ap->ioaddr;
1877 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1879 /* software reset. causes dev0 to be selected */
1880 if (ap->flags & ATA_FLAG_MMIO) {
1881 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1882 udelay(20); /* FIXME: flush */
1883 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1884 udelay(20); /* FIXME: flush */
1885 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1887 outb(ap->ctl, ioaddr->ctl_addr);
1889 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1891 outb(ap->ctl, ioaddr->ctl_addr);
1894 /* spec mandates ">= 2ms" before checking status.
1895 * We wait 150ms, because that was the magic delay used for
1896 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1897 * between when the ATA command register is written, and then
1898 * status is checked. Because waiting for "a while" before
1899 * checking status is fine, post SRST, we perform this magic
1900 * delay here as well.
1904 ata_bus_post_reset(ap, devmask);
1910 * ata_bus_reset - reset host port and associated ATA channel
1911 * @ap: port to reset
1913 * This is typically the first time we actually start issuing
1914 * commands to the ATA channel. We wait for BSY to clear, then
1915 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1916 * result. Determine what devices, if any, are on the channel
1917 * by looking at the device 0/1 error register. Look at the signature
1918 * stored in each device's taskfile registers, to determine if
1919 * the device is ATA or ATAPI.
1922 * PCI/etc. bus probe sem.
1923 * Obtains host_set lock.
1926 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1929 void ata_bus_reset(struct ata_port *ap)
1931 struct ata_ioports *ioaddr = &ap->ioaddr;
1932 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1934 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1936 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1938 /* determine if device 0/1 are present */
1939 if (ap->flags & ATA_FLAG_SATA_RESET)
1942 dev0 = ata_devchk(ap, 0);
1944 dev1 = ata_devchk(ap, 1);
1948 devmask |= (1 << 0);
1950 devmask |= (1 << 1);
1952 /* select device 0 again */
1953 ap->ops->dev_select(ap, 0);
1955 /* issue bus reset */
1956 if (ap->flags & ATA_FLAG_SRST)
1957 rc = ata_bus_softreset(ap, devmask);
1958 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1959 /* set up device control */
1960 if (ap->flags & ATA_FLAG_MMIO)
1961 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1963 outb(ap->ctl, ioaddr->ctl_addr);
1964 rc = ata_bus_edd(ap);
1971 * determine by signature whether we have ATA or ATAPI devices
1973 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1974 if ((slave_possible) && (err != 0x81))
1975 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1977 /* re-enable interrupts */
1978 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1981 /* is double-select really necessary? */
1982 if (ap->device[1].class != ATA_DEV_NONE)
1983 ap->ops->dev_select(ap, 1);
1984 if (ap->device[0].class != ATA_DEV_NONE)
1985 ap->ops->dev_select(ap, 0);
1987 /* if no devices were detected, disable this port */
1988 if ((ap->device[0].class == ATA_DEV_NONE) &&
1989 (ap->device[1].class == ATA_DEV_NONE))
1992 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1993 /* set up device control for ATA_FLAG_SATA_RESET */
1994 if (ap->flags & ATA_FLAG_MMIO)
1995 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1997 outb(ap->ctl, ioaddr->ctl_addr);
2004 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2005 ap->ops->port_disable(ap);
2010 static int sata_phy_resume(struct ata_port *ap)
2012 unsigned long timeout = jiffies + (HZ * 5);
2015 scr_write_flush(ap, SCR_CONTROL, 0x300);
2017 /* Wait for phy to become ready, if necessary. */
2020 sstatus = scr_read(ap, SCR_STATUS);
2021 if ((sstatus & 0xf) != 1)
2023 } while (time_before(jiffies, timeout));
2029 * ata_std_probeinit - initialize probing
2030 * @ap: port to be probed
2032 * @ap is about to be probed. Initialize it. This function is
2033 * to be used as standard callback for ata_drive_probe_reset().
2035 * NOTE!!! Do not use this function as probeinit if a low level
2036 * driver implements only hardreset. Just pass NULL as probeinit
2037 * in that case. Using this function is probably okay but doing
2038 * so makes reset sequence different from the original
2039 * ->phy_reset implementation and Jeff nervous. :-P
2041 extern void ata_std_probeinit(struct ata_port *ap)
2043 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2044 sata_phy_resume(ap);
2045 if (sata_dev_present(ap))
2046 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2051 * ata_std_softreset - reset host port via ATA SRST
2052 * @ap: port to reset
2053 * @verbose: fail verbosely
2054 * @classes: resulting classes of attached devices
2056 * Reset host port using ATA SRST. This function is to be used
2057 * as standard callback for ata_drive_*_reset() functions.
2060 * Kernel thread context (may sleep)
2063 * 0 on success, -errno otherwise.
2065 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2067 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2068 unsigned int devmask = 0, err_mask;
2073 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2074 classes[0] = ATA_DEV_NONE;
2078 /* determine if device 0/1 are present */
2079 if (ata_devchk(ap, 0))
2080 devmask |= (1 << 0);
2081 if (slave_possible && ata_devchk(ap, 1))
2082 devmask |= (1 << 1);
2084 /* select device 0 again */
2085 ap->ops->dev_select(ap, 0);
2087 /* issue bus reset */
2088 DPRINTK("about to softreset, devmask=%x\n", devmask);
2089 err_mask = ata_bus_softreset(ap, devmask);
2092 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2095 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2100 /* determine by signature whether we have ATA or ATAPI devices */
2101 classes[0] = ata_dev_try_classify(ap, 0, &err);
2102 if (slave_possible && err != 0x81)
2103 classes[1] = ata_dev_try_classify(ap, 1, &err);
2106 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2111 * sata_std_hardreset - reset host port via SATA phy reset
2112 * @ap: port to reset
2113 * @verbose: fail verbosely
2114 * @class: resulting class of attached device
2116 * SATA phy-reset host port using DET bits of SControl register.
2117 * This function is to be used as standard callback for
2118 * ata_drive_*_reset().
2121 * Kernel thread context (may sleep)
2124 * 0 on success, -errno otherwise.
2126 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2130 /* Issue phy wake/reset */
2131 scr_write_flush(ap, SCR_CONTROL, 0x301);
2134 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2135 * 10.4.2 says at least 1 ms.
2139 /* Bring phy back */
2140 sata_phy_resume(ap);
2142 /* TODO: phy layer with polling, timeouts, etc. */
2143 if (!sata_dev_present(ap)) {
2144 *class = ATA_DEV_NONE;
2145 DPRINTK("EXIT, link offline\n");
2149 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2151 printk(KERN_ERR "ata%u: COMRESET failed "
2152 "(device not ready)\n", ap->id);
2154 DPRINTK("EXIT, device not ready\n");
2158 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2160 *class = ata_dev_try_classify(ap, 0, NULL);
2162 DPRINTK("EXIT, class=%u\n", *class);
2167 * ata_std_postreset - standard postreset callback
2168 * @ap: the target ata_port
2169 * @classes: classes of attached devices
2171 * This function is invoked after a successful reset. Note that
2172 * the device might have been reset more than once using
2173 * different reset methods before postreset is invoked.
2175 * This function is to be used as standard callback for
2176 * ata_drive_*_reset().
2179 * Kernel thread context (may sleep)
2181 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2185 /* set cable type if it isn't already set */
2186 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2187 ap->cbl = ATA_CBL_SATA;
2189 /* print link status */
2190 if (ap->cbl == ATA_CBL_SATA)
2191 sata_print_link_status(ap);
2193 /* re-enable interrupts */
2194 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2197 /* is double-select really necessary? */
2198 if (classes[0] != ATA_DEV_NONE)
2199 ap->ops->dev_select(ap, 1);
2200 if (classes[1] != ATA_DEV_NONE)
2201 ap->ops->dev_select(ap, 0);
2203 /* bail out if no device is present */
2204 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2205 DPRINTK("EXIT, no device\n");
2209 /* set up device control */
2210 if (ap->ioaddr.ctl_addr) {
2211 if (ap->flags & ATA_FLAG_MMIO)
2212 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2214 outb(ap->ctl, ap->ioaddr.ctl_addr);
2221 * ata_std_probe_reset - standard probe reset method
2222 * @ap: prot to perform probe-reset
2223 * @classes: resulting classes of attached devices
2225 * The stock off-the-shelf ->probe_reset method.
2228 * Kernel thread context (may sleep)
2231 * 0 on success, -errno otherwise.
2233 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2235 ata_reset_fn_t hardreset;
2238 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2239 hardreset = sata_std_hardreset;
2241 return ata_drive_probe_reset(ap, ata_std_probeinit,
2242 ata_std_softreset, hardreset,
2243 ata_std_postreset, classes);
2246 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2247 ata_postreset_fn_t postreset,
2248 unsigned int *classes)
2252 for (i = 0; i < ATA_MAX_DEVICES; i++)
2253 classes[i] = ATA_DEV_UNKNOWN;
2255 rc = reset(ap, 0, classes);
2259 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2260 * is complete and convert all ATA_DEV_UNKNOWN to
2263 for (i = 0; i < ATA_MAX_DEVICES; i++)
2264 if (classes[i] != ATA_DEV_UNKNOWN)
2267 if (i < ATA_MAX_DEVICES)
2268 for (i = 0; i < ATA_MAX_DEVICES; i++)
2269 if (classes[i] == ATA_DEV_UNKNOWN)
2270 classes[i] = ATA_DEV_NONE;
2273 postreset(ap, classes);
2275 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2279 * ata_drive_probe_reset - Perform probe reset with given methods
2280 * @ap: port to reset
2281 * @probeinit: probeinit method (can be NULL)
2282 * @softreset: softreset method (can be NULL)
2283 * @hardreset: hardreset method (can be NULL)
2284 * @postreset: postreset method (can be NULL)
2285 * @classes: resulting classes of attached devices
2287 * Reset the specified port and classify attached devices using
2288 * given methods. This function prefers softreset but tries all
2289 * possible reset sequences to reset and classify devices. This
2290 * function is intended to be used for constructing ->probe_reset
2291 * callback by low level drivers.
2293 * Reset methods should follow the following rules.
2295 * - Return 0 on sucess, -errno on failure.
2296 * - If classification is supported, fill classes[] with
2297 * recognized class codes.
2298 * - If classification is not supported, leave classes[] alone.
2299 * - If verbose is non-zero, print error message on failure;
2300 * otherwise, shut up.
2303 * Kernel thread context (may sleep)
2306 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2307 * if classification fails, and any error code from reset
2310 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2311 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2312 ata_postreset_fn_t postreset, unsigned int *classes)
2320 rc = do_probe_reset(ap, softreset, postreset, classes);
2328 rc = do_probe_reset(ap, hardreset, postreset, classes);
2329 if (rc == 0 || rc != -ENODEV)
2333 rc = do_probe_reset(ap, softreset, postreset, classes);
2338 static void ata_pr_blacklisted(const struct ata_port *ap,
2339 const struct ata_device *dev)
2341 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2342 ap->id, dev->devno);
2345 static const char * const ata_dma_blacklist [] = {
2364 "Toshiba CD-ROM XM-6202B",
2365 "TOSHIBA CD-ROM XM-1702BC",
2367 "E-IDE CD-ROM CR-840",
2370 "SAMSUNG CD-ROM SC-148C",
2371 "SAMSUNG CD-ROM SC",
2373 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2377 static int ata_dma_blacklisted(const struct ata_device *dev)
2379 unsigned char model_num[41];
2382 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2384 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2385 if (!strcmp(ata_dma_blacklist[i], model_num))
2391 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2393 const struct ata_device *master, *slave;
2396 master = &ap->device[0];
2397 slave = &ap->device[1];
2399 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2401 if (shift == ATA_SHIFT_UDMA) {
2402 mask = ap->udma_mask;
2403 if (ata_dev_present(master)) {
2404 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2405 if (ata_dma_blacklisted(master)) {
2407 ata_pr_blacklisted(ap, master);
2410 if (ata_dev_present(slave)) {
2411 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2412 if (ata_dma_blacklisted(slave)) {
2414 ata_pr_blacklisted(ap, slave);
2418 else if (shift == ATA_SHIFT_MWDMA) {
2419 mask = ap->mwdma_mask;
2420 if (ata_dev_present(master)) {
2421 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2422 if (ata_dma_blacklisted(master)) {
2424 ata_pr_blacklisted(ap, master);
2427 if (ata_dev_present(slave)) {
2428 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2429 if (ata_dma_blacklisted(slave)) {
2431 ata_pr_blacklisted(ap, slave);
2435 else if (shift == ATA_SHIFT_PIO) {
2436 mask = ap->pio_mask;
2437 if (ata_dev_present(master)) {
2438 /* spec doesn't return explicit support for
2439 * PIO0-2, so we fake it
2441 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2446 if (ata_dev_present(slave)) {
2447 /* spec doesn't return explicit support for
2448 * PIO0-2, so we fake it
2450 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2457 mask = 0xffffffff; /* shut up compiler warning */
2464 /* find greatest bit */
2465 static int fgb(u32 bitmap)
2470 for (i = 0; i < 32; i++)
2471 if (bitmap & (1 << i))
2478 * ata_choose_xfer_mode - attempt to find best transfer mode
2479 * @ap: Port for which an xfer mode will be selected
2480 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2481 * @xfer_shift_out: (output) bit shift that selects this mode
2483 * Based on host and device capabilities, determine the
2484 * maximum transfer mode that is amenable to all.
2487 * PCI/etc. bus probe sem.
2490 * Zero on success, negative on error.
2493 static int ata_choose_xfer_mode(const struct ata_port *ap,
2495 unsigned int *xfer_shift_out)
2497 unsigned int mask, shift;
2500 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2501 shift = xfer_mode_classes[i].shift;
2502 mask = ata_get_mode_mask(ap, shift);
2506 *xfer_mode_out = xfer_mode_classes[i].base + x;
2507 *xfer_shift_out = shift;
2516 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2517 * @ap: Port associated with device @dev
2518 * @dev: Device to which command will be sent
2520 * Issue SET FEATURES - XFER MODE command to device @dev
2524 * PCI/etc. bus probe sem.
2527 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2529 struct ata_taskfile tf;
2531 /* set up set-features taskfile */
2532 DPRINTK("set features - xfer mode\n");
2534 ata_tf_init(ap, &tf, dev->devno);
2535 tf.command = ATA_CMD_SET_FEATURES;
2536 tf.feature = SETFEATURES_XFER;
2537 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2538 tf.protocol = ATA_PROT_NODATA;
2539 tf.nsect = dev->xfer_mode;
2541 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2542 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2544 ata_port_disable(ap);
2551 * ata_dev_init_params - Issue INIT DEV PARAMS command
2552 * @ap: Port associated with device @dev
2553 * @dev: Device to which command will be sent
2556 * Kernel thread context (may sleep)
2559 * 0 on success, AC_ERR_* mask otherwise.
2562 static unsigned int ata_dev_init_params(struct ata_port *ap,
2563 struct ata_device *dev)
2565 struct ata_taskfile tf;
2566 unsigned int err_mask;
2567 u16 sectors = dev->id[6];
2568 u16 heads = dev->id[3];
2570 /* Number of sectors per track 1-255. Number of heads 1-16 */
2571 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2574 /* set up init dev params taskfile */
2575 DPRINTK("init dev params \n");
2577 ata_tf_init(ap, &tf, dev->devno);
2578 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2579 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2580 tf.protocol = ATA_PROT_NODATA;
2582 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2584 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2586 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2591 * ata_sg_clean - Unmap DMA memory associated with command
2592 * @qc: Command containing DMA memory to be released
2594 * Unmap all mapped DMA memory associated with this command.
2597 * spin_lock_irqsave(host_set lock)
2600 static void ata_sg_clean(struct ata_queued_cmd *qc)
2602 struct ata_port *ap = qc->ap;
2603 struct scatterlist *sg = qc->__sg;
2604 int dir = qc->dma_dir;
2605 void *pad_buf = NULL;
2607 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2608 WARN_ON(sg == NULL);
2610 if (qc->flags & ATA_QCFLAG_SINGLE)
2611 WARN_ON(qc->n_elem > 1);
2613 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2615 /* if we padded the buffer out to 32-bit bound, and data
2616 * xfer direction is from-device, we must copy from the
2617 * pad buffer back into the supplied buffer
2619 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2620 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2622 if (qc->flags & ATA_QCFLAG_SG) {
2624 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2625 /* restore last sg */
2626 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2628 struct scatterlist *psg = &qc->pad_sgent;
2629 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2630 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2631 kunmap_atomic(addr, KM_IRQ0);
2635 dma_unmap_single(ap->host_set->dev,
2636 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2639 sg->length += qc->pad_len;
2641 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2642 pad_buf, qc->pad_len);
2645 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2650 * ata_fill_sg - Fill PCI IDE PRD table
2651 * @qc: Metadata associated with taskfile to be transferred
2653 * Fill PCI IDE PRD (scatter-gather) table with segments
2654 * associated with the current disk command.
2657 * spin_lock_irqsave(host_set lock)
2660 static void ata_fill_sg(struct ata_queued_cmd *qc)
2662 struct ata_port *ap = qc->ap;
2663 struct scatterlist *sg;
2666 WARN_ON(qc->__sg == NULL);
2667 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2670 ata_for_each_sg(sg, qc) {
2674 /* determine if physical DMA addr spans 64K boundary.
2675 * Note h/w doesn't support 64-bit, so we unconditionally
2676 * truncate dma_addr_t to u32.
2678 addr = (u32) sg_dma_address(sg);
2679 sg_len = sg_dma_len(sg);
2682 offset = addr & 0xffff;
2684 if ((offset + sg_len) > 0x10000)
2685 len = 0x10000 - offset;
2687 ap->prd[idx].addr = cpu_to_le32(addr);
2688 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2689 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2698 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2701 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2702 * @qc: Metadata associated with taskfile to check
2704 * Allow low-level driver to filter ATA PACKET commands, returning
2705 * a status indicating whether or not it is OK to use DMA for the
2706 * supplied PACKET command.
2709 * spin_lock_irqsave(host_set lock)
2711 * RETURNS: 0 when ATAPI DMA can be used
2714 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2716 struct ata_port *ap = qc->ap;
2717 int rc = 0; /* Assume ATAPI DMA is OK by default */
2719 if (ap->ops->check_atapi_dma)
2720 rc = ap->ops->check_atapi_dma(qc);
2725 * ata_qc_prep - Prepare taskfile for submission
2726 * @qc: Metadata associated with taskfile to be prepared
2728 * Prepare ATA taskfile for submission.
2731 * spin_lock_irqsave(host_set lock)
2733 void ata_qc_prep(struct ata_queued_cmd *qc)
2735 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2742 * ata_sg_init_one - Associate command with memory buffer
2743 * @qc: Command to be associated
2744 * @buf: Memory buffer
2745 * @buflen: Length of memory buffer, in bytes.
2747 * Initialize the data-related elements of queued_cmd @qc
2748 * to point to a single memory buffer, @buf of byte length @buflen.
2751 * spin_lock_irqsave(host_set lock)
2754 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2756 struct scatterlist *sg;
2758 qc->flags |= ATA_QCFLAG_SINGLE;
2760 memset(&qc->sgent, 0, sizeof(qc->sgent));
2761 qc->__sg = &qc->sgent;
2763 qc->orig_n_elem = 1;
2767 sg_init_one(sg, buf, buflen);
2771 * ata_sg_init - Associate command with scatter-gather table.
2772 * @qc: Command to be associated
2773 * @sg: Scatter-gather table.
2774 * @n_elem: Number of elements in s/g table.
2776 * Initialize the data-related elements of queued_cmd @qc
2777 * to point to a scatter-gather table @sg, containing @n_elem
2781 * spin_lock_irqsave(host_set lock)
2784 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2785 unsigned int n_elem)
2787 qc->flags |= ATA_QCFLAG_SG;
2789 qc->n_elem = n_elem;
2790 qc->orig_n_elem = n_elem;
2794 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2795 * @qc: Command with memory buffer to be mapped.
2797 * DMA-map the memory buffer associated with queued_cmd @qc.
2800 * spin_lock_irqsave(host_set lock)
2803 * Zero on success, negative on error.
2806 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2808 struct ata_port *ap = qc->ap;
2809 int dir = qc->dma_dir;
2810 struct scatterlist *sg = qc->__sg;
2811 dma_addr_t dma_address;
2814 /* we must lengthen transfers to end on a 32-bit boundary */
2815 qc->pad_len = sg->length & 3;
2817 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2818 struct scatterlist *psg = &qc->pad_sgent;
2820 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2822 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2824 if (qc->tf.flags & ATA_TFLAG_WRITE)
2825 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2828 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2829 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2831 sg->length -= qc->pad_len;
2832 if (sg->length == 0)
2835 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2836 sg->length, qc->pad_len);
2844 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2846 if (dma_mapping_error(dma_address)) {
2848 sg->length += qc->pad_len;
2852 sg_dma_address(sg) = dma_address;
2853 sg_dma_len(sg) = sg->length;
2856 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2857 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2863 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2864 * @qc: Command with scatter-gather table to be mapped.
2866 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2869 * spin_lock_irqsave(host_set lock)
2872 * Zero on success, negative on error.
2876 static int ata_sg_setup(struct ata_queued_cmd *qc)
2878 struct ata_port *ap = qc->ap;
2879 struct scatterlist *sg = qc->__sg;
2880 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2881 int n_elem, pre_n_elem, dir, trim_sg = 0;
2883 VPRINTK("ENTER, ata%u\n", ap->id);
2884 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2886 /* we must lengthen transfers to end on a 32-bit boundary */
2887 qc->pad_len = lsg->length & 3;
2889 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2890 struct scatterlist *psg = &qc->pad_sgent;
2891 unsigned int offset;
2893 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2895 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2898 * psg->page/offset are used to copy to-be-written
2899 * data in this function or read data in ata_sg_clean.
2901 offset = lsg->offset + lsg->length - qc->pad_len;
2902 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2903 psg->offset = offset_in_page(offset);
2905 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2906 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2907 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2908 kunmap_atomic(addr, KM_IRQ0);
2911 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2912 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2914 lsg->length -= qc->pad_len;
2915 if (lsg->length == 0)
2918 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2919 qc->n_elem - 1, lsg->length, qc->pad_len);
2922 pre_n_elem = qc->n_elem;
2923 if (trim_sg && pre_n_elem)
2932 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2934 /* restore last sg */
2935 lsg->length += qc->pad_len;
2939 DPRINTK("%d sg elements mapped\n", n_elem);
2942 qc->n_elem = n_elem;
2948 * ata_poll_qc_complete - turn irq back on and finish qc
2949 * @qc: Command to complete
2950 * @err_mask: ATA status register content
2953 * None. (grabs host lock)
2956 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2958 struct ata_port *ap = qc->ap;
2959 unsigned long flags;
2961 spin_lock_irqsave(&ap->host_set->lock, flags);
2962 ap->flags &= ~ATA_FLAG_NOINTR;
2964 ata_qc_complete(qc);
2965 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2969 * ata_pio_poll - poll using PIO, depending on current state
2970 * @ap: the target ata_port
2973 * None. (executing in kernel thread context)
2976 * timeout value to use
2979 static unsigned long ata_pio_poll(struct ata_port *ap)
2981 struct ata_queued_cmd *qc;
2983 unsigned int poll_state = HSM_ST_UNKNOWN;
2984 unsigned int reg_state = HSM_ST_UNKNOWN;
2986 qc = ata_qc_from_tag(ap, ap->active_tag);
2987 WARN_ON(qc == NULL);
2989 switch (ap->hsm_task_state) {
2992 poll_state = HSM_ST_POLL;
2996 case HSM_ST_LAST_POLL:
2997 poll_state = HSM_ST_LAST_POLL;
2998 reg_state = HSM_ST_LAST;
3005 status = ata_chk_status(ap);
3006 if (status & ATA_BUSY) {
3007 if (time_after(jiffies, ap->pio_task_timeout)) {
3008 qc->err_mask |= AC_ERR_TIMEOUT;
3009 ap->hsm_task_state = HSM_ST_TMOUT;
3012 ap->hsm_task_state = poll_state;
3013 return ATA_SHORT_PAUSE;
3016 ap->hsm_task_state = reg_state;
3021 * ata_pio_complete - check if drive is busy or idle
3022 * @ap: the target ata_port
3025 * None. (executing in kernel thread context)
3028 * Non-zero if qc completed, zero otherwise.
3031 static int ata_pio_complete (struct ata_port *ap)
3033 struct ata_queued_cmd *qc;
3037 * This is purely heuristic. This is a fast path. Sometimes when
3038 * we enter, BSY will be cleared in a chk-status or two. If not,
3039 * the drive is probably seeking or something. Snooze for a couple
3040 * msecs, then chk-status again. If still busy, fall back to
3041 * HSM_ST_POLL state.
3043 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3044 if (drv_stat & ATA_BUSY) {
3046 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3047 if (drv_stat & ATA_BUSY) {
3048 ap->hsm_task_state = HSM_ST_LAST_POLL;
3049 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3054 qc = ata_qc_from_tag(ap, ap->active_tag);
3055 WARN_ON(qc == NULL);
3057 drv_stat = ata_wait_idle(ap);
3058 if (!ata_ok(drv_stat)) {
3059 qc->err_mask |= __ac_err_mask(drv_stat);
3060 ap->hsm_task_state = HSM_ST_ERR;
3064 ap->hsm_task_state = HSM_ST_IDLE;
3066 WARN_ON(qc->err_mask);
3067 ata_poll_qc_complete(qc);
3069 /* another command may start at this point */
3076 * swap_buf_le16 - swap halves of 16-bit words in place
3077 * @buf: Buffer to swap
3078 * @buf_words: Number of 16-bit words in buffer.
3080 * Swap halves of 16-bit words if needed to convert from
3081 * little-endian byte order to native cpu byte order, or
3085 * Inherited from caller.
3087 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3092 for (i = 0; i < buf_words; i++)
3093 buf[i] = le16_to_cpu(buf[i]);
3094 #endif /* __BIG_ENDIAN */
3098 * ata_mmio_data_xfer - Transfer data by MMIO
3099 * @ap: port to read/write
3101 * @buflen: buffer length
3102 * @write_data: read/write
3104 * Transfer data from/to the device data register by MMIO.
3107 * Inherited from caller.
3110 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3111 unsigned int buflen, int write_data)
3114 unsigned int words = buflen >> 1;
3115 u16 *buf16 = (u16 *) buf;
3116 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3118 /* Transfer multiple of 2 bytes */
3120 for (i = 0; i < words; i++)
3121 writew(le16_to_cpu(buf16[i]), mmio);
3123 for (i = 0; i < words; i++)
3124 buf16[i] = cpu_to_le16(readw(mmio));
3127 /* Transfer trailing 1 byte, if any. */
3128 if (unlikely(buflen & 0x01)) {
3129 u16 align_buf[1] = { 0 };
3130 unsigned char *trailing_buf = buf + buflen - 1;
3133 memcpy(align_buf, trailing_buf, 1);
3134 writew(le16_to_cpu(align_buf[0]), mmio);
3136 align_buf[0] = cpu_to_le16(readw(mmio));
3137 memcpy(trailing_buf, align_buf, 1);
3143 * ata_pio_data_xfer - Transfer data by PIO
3144 * @ap: port to read/write
3146 * @buflen: buffer length
3147 * @write_data: read/write
3149 * Transfer data from/to the device data register by PIO.
3152 * Inherited from caller.
3155 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3156 unsigned int buflen, int write_data)
3158 unsigned int words = buflen >> 1;
3160 /* Transfer multiple of 2 bytes */
3162 outsw(ap->ioaddr.data_addr, buf, words);
3164 insw(ap->ioaddr.data_addr, buf, words);
3166 /* Transfer trailing 1 byte, if any. */
3167 if (unlikely(buflen & 0x01)) {
3168 u16 align_buf[1] = { 0 };
3169 unsigned char *trailing_buf = buf + buflen - 1;
3172 memcpy(align_buf, trailing_buf, 1);
3173 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3175 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3176 memcpy(trailing_buf, align_buf, 1);
3182 * ata_data_xfer - Transfer data from/to the data register.
3183 * @ap: port to read/write
3185 * @buflen: buffer length
3186 * @do_write: read/write
3188 * Transfer data from/to the device data register.
3191 * Inherited from caller.
3194 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3195 unsigned int buflen, int do_write)
3197 /* Make the crap hardware pay the costs not the good stuff */
3198 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3199 unsigned long flags;
3200 local_irq_save(flags);
3201 if (ap->flags & ATA_FLAG_MMIO)
3202 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3204 ata_pio_data_xfer(ap, buf, buflen, do_write);
3205 local_irq_restore(flags);
3207 if (ap->flags & ATA_FLAG_MMIO)
3208 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3210 ata_pio_data_xfer(ap, buf, buflen, do_write);
3215 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3216 * @qc: Command on going
3218 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3221 * Inherited from caller.
3224 static void ata_pio_sector(struct ata_queued_cmd *qc)
3226 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3227 struct scatterlist *sg = qc->__sg;
3228 struct ata_port *ap = qc->ap;
3230 unsigned int offset;
3233 if (qc->cursect == (qc->nsect - 1))
3234 ap->hsm_task_state = HSM_ST_LAST;
3236 page = sg[qc->cursg].page;
3237 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3239 /* get the current page and offset */
3240 page = nth_page(page, (offset >> PAGE_SHIFT));
3241 offset %= PAGE_SIZE;
3243 buf = kmap(page) + offset;
3248 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3253 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3255 /* do the actual data transfer */
3256 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3257 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3263 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3264 * @qc: Command on going
3265 * @bytes: number of bytes
3267 * Transfer Transfer data from/to the ATAPI device.
3270 * Inherited from caller.
3274 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3276 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3277 struct scatterlist *sg = qc->__sg;
3278 struct ata_port *ap = qc->ap;
3281 unsigned int offset, count;
3283 if (qc->curbytes + bytes >= qc->nbytes)
3284 ap->hsm_task_state = HSM_ST_LAST;
3287 if (unlikely(qc->cursg >= qc->n_elem)) {
3289 * The end of qc->sg is reached and the device expects
3290 * more data to transfer. In order not to overrun qc->sg
3291 * and fulfill length specified in the byte count register,
3292 * - for read case, discard trailing data from the device
3293 * - for write case, padding zero data to the device
3295 u16 pad_buf[1] = { 0 };
3296 unsigned int words = bytes >> 1;
3299 if (words) /* warning if bytes > 1 */
3300 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3303 for (i = 0; i < words; i++)
3304 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3306 ap->hsm_task_state = HSM_ST_LAST;
3310 sg = &qc->__sg[qc->cursg];
3313 offset = sg->offset + qc->cursg_ofs;
3315 /* get the current page and offset */
3316 page = nth_page(page, (offset >> PAGE_SHIFT));
3317 offset %= PAGE_SIZE;
3319 /* don't overrun current sg */
3320 count = min(sg->length - qc->cursg_ofs, bytes);
3322 /* don't cross page boundaries */
3323 count = min(count, (unsigned int)PAGE_SIZE - offset);
3325 buf = kmap(page) + offset;
3328 qc->curbytes += count;
3329 qc->cursg_ofs += count;
3331 if (qc->cursg_ofs == sg->length) {
3336 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3338 /* do the actual data transfer */
3339 ata_data_xfer(ap, buf, count, do_write);
3348 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3349 * @qc: Command on going
3351 * Transfer Transfer data from/to the ATAPI device.
3354 * Inherited from caller.
3357 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3359 struct ata_port *ap = qc->ap;
3360 struct ata_device *dev = qc->dev;
3361 unsigned int ireason, bc_lo, bc_hi, bytes;
3362 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3364 ap->ops->tf_read(ap, &qc->tf);
3365 ireason = qc->tf.nsect;
3366 bc_lo = qc->tf.lbam;
3367 bc_hi = qc->tf.lbah;
3368 bytes = (bc_hi << 8) | bc_lo;
3370 /* shall be cleared to zero, indicating xfer of data */
3371 if (ireason & (1 << 0))
3374 /* make sure transfer direction matches expected */
3375 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3376 if (do_write != i_write)
3379 __atapi_pio_bytes(qc, bytes);
3384 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3385 ap->id, dev->devno);
3386 qc->err_mask |= AC_ERR_HSM;
3387 ap->hsm_task_state = HSM_ST_ERR;
3391 * ata_pio_block - start PIO on a block
3392 * @ap: the target ata_port
3395 * None. (executing in kernel thread context)