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 * @id: buffer to fill IDENTIFY page into
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 *id)
931 unsigned int class = *p_class;
932 unsigned int using_edd;
933 struct ata_taskfile tf;
934 unsigned int err_mask = 0;
938 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
940 if (ap->ops->probe_reset ||
941 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
946 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
949 ata_tf_init(ap, &tf, dev->devno);
953 tf.command = ATA_CMD_ID_ATA;
956 tf.command = ATA_CMD_ID_ATAPI;
960 reason = "unsupported class";
964 tf.protocol = ATA_PROT_PIO;
966 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
967 id, sizeof(id[0]) * ATA_ID_WORDS);
971 reason = "I/O error";
973 if (err_mask & ~AC_ERR_DEV)
977 * arg! EDD works for all test cases, but seems to return
978 * the ATA signature for some ATAPI devices. Until the
979 * reason for this is found and fixed, we fix up the mess
980 * here. If IDENTIFY DEVICE returns command aborted
981 * (as ATAPI devices do), then we issue an
982 * IDENTIFY PACKET DEVICE.
984 * ATA software reset (SRST, the default) does not appear
985 * to have this problem.
987 if ((using_edd) && (class == ATA_DEV_ATA)) {
989 if (err & ATA_ABORTED) {
990 class = ATA_DEV_ATAPI;
997 swap_buf_le16(id, ATA_ID_WORDS);
999 /* print device capabilities */
1000 printk(KERN_DEBUG "ata%u: dev %u cfg "
1001 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1003 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1006 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1008 reason = "device reports illegal type";
1012 if (post_reset && class == ATA_DEV_ATA) {
1014 * The exact sequence expected by certain pre-ATA4 drives is:
1017 * INITIALIZE DEVICE PARAMETERS
1019 * Some drives were very specific about that exact sequence.
1021 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1022 err_mask = ata_dev_init_params(ap, dev);
1025 reason = "INIT_DEV_PARAMS failed";
1029 /* current CHS translation info (id[53-58]) might be
1030 * changed. reread the identify device info.
1041 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1042 ap->id, dev->devno, reason);
1047 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1048 * @ap: port on which device we wish to probe resides
1049 * @device: device bus address, starting at zero
1051 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1052 * command, and read back the 512-byte device information page.
1053 * The device information page is fed to us via the standard
1054 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1055 * using standard PIO-IN paths)
1057 * After reading the device information page, we use several
1058 * bits of information from it to initialize data structures
1059 * that will be used during the lifetime of the ata_device.
1060 * Other data from the info page is used to disqualify certain
1061 * older ATA devices we do not wish to support.
1064 * Inherited from caller. Some functions called by this function
1065 * obtain the host_set lock.
1068 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1070 struct ata_device *dev = &ap->device[device];
1071 unsigned long xfer_modes;
1074 if (!ata_dev_present(dev)) {
1075 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1080 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1082 rc = ata_dev_read_id(ap, dev, &dev->class, 1, dev->id);
1087 * common ATA, ATAPI feature tests
1090 /* we require DMA support (bits 8 of word 49) */
1091 if (!ata_id_has_dma(dev->id)) {
1092 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1096 /* quick-n-dirty find max transfer mode; for printk only */
1097 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1099 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1101 xfer_modes = ata_pio_modes(dev);
1103 ata_dump_id(dev->id);
1105 /* ATA-specific feature tests */
1106 if (dev->class == ATA_DEV_ATA) {
1107 dev->n_sectors = ata_id_n_sectors(dev->id);
1109 if (ata_id_has_lba(dev->id)) {
1110 dev->flags |= ATA_DFLAG_LBA;
1112 if (ata_id_has_lba48(dev->id))
1113 dev->flags |= ATA_DFLAG_LBA48;
1115 /* print device info to dmesg */
1116 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1118 ata_id_major_version(dev->id),
1119 ata_mode_string(xfer_modes),
1120 (unsigned long long)dev->n_sectors,
1121 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1125 /* Default translation */
1126 dev->cylinders = dev->id[1];
1127 dev->heads = dev->id[3];
1128 dev->sectors = dev->id[6];
1130 if (ata_id_current_chs_valid(dev->id)) {
1131 /* Current CHS translation is valid. */
1132 dev->cylinders = dev->id[54];
1133 dev->heads = dev->id[55];
1134 dev->sectors = dev->id[56];
1137 /* print device info to dmesg */
1138 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1140 ata_id_major_version(dev->id),
1141 ata_mode_string(xfer_modes),
1142 (unsigned long long)dev->n_sectors,
1143 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1150 /* ATAPI-specific feature tests */
1151 else if (dev->class == ATA_DEV_ATAPI) {
1152 rc = atapi_cdb_len(dev->id);
1153 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1154 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1157 dev->cdb_len = (unsigned int) rc;
1159 /* print device info to dmesg */
1160 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1162 ata_mode_string(xfer_modes));
1165 ap->host->max_cmd_len = 0;
1166 for (i = 0; i < ATA_MAX_DEVICES; i++)
1167 ap->host->max_cmd_len = max_t(unsigned int,
1168 ap->host->max_cmd_len,
1169 ap->device[i].cdb_len);
1171 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1175 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1178 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1179 DPRINTK("EXIT, err\n");
1183 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1184 struct ata_device *dev)
1186 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1190 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1197 void ata_dev_config(struct ata_port *ap, unsigned int i)
1199 /* limit bridge transfers to udma5, 200 sectors */
1200 if (ata_dev_knobble(ap, &ap->device[i])) {
1201 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1203 ap->udma_mask &= ATA_UDMA5;
1204 ap->device[i].max_sectors = ATA_MAX_SECTORS;
1207 if (ap->ops->dev_config)
1208 ap->ops->dev_config(ap, &ap->device[i]);
1212 * ata_bus_probe - Reset and probe ATA bus
1215 * Master ATA bus probing function. Initiates a hardware-dependent
1216 * bus reset, then attempts to identify any devices found on
1220 * PCI/etc. bus probe sem.
1223 * Zero on success, non-zero on error.
1226 static int ata_bus_probe(struct ata_port *ap)
1228 unsigned int i, found = 0;
1230 if (ap->ops->probe_reset) {
1231 unsigned int classes[ATA_MAX_DEVICES];
1236 rc = ap->ops->probe_reset(ap, classes);
1238 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1239 if (classes[i] == ATA_DEV_UNKNOWN)
1240 classes[i] = ATA_DEV_NONE;
1241 ap->device[i].class = classes[i];
1244 printk(KERN_ERR "ata%u: probe reset failed, "
1245 "disabling port\n", ap->id);
1246 ata_port_disable(ap);
1249 ap->ops->phy_reset(ap);
1251 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1254 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1255 ata_dev_identify(ap, i);
1256 if (ata_dev_present(&ap->device[i])) {
1258 ata_dev_config(ap,i);
1262 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1263 goto err_out_disable;
1266 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1267 goto err_out_disable;
1272 ap->ops->port_disable(ap);
1278 * ata_port_probe - Mark port as enabled
1279 * @ap: Port for which we indicate enablement
1281 * Modify @ap data structure such that the system
1282 * thinks that the entire port is enabled.
1284 * LOCKING: host_set lock, or some other form of
1288 void ata_port_probe(struct ata_port *ap)
1290 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1294 * sata_print_link_status - Print SATA link status
1295 * @ap: SATA port to printk link status about
1297 * This function prints link speed and status of a SATA link.
1302 static void sata_print_link_status(struct ata_port *ap)
1307 if (!ap->ops->scr_read)
1310 sstatus = scr_read(ap, SCR_STATUS);
1312 if (sata_dev_present(ap)) {
1313 tmp = (sstatus >> 4) & 0xf;
1316 else if (tmp & (1 << 1))
1319 speed = "<unknown>";
1320 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1321 ap->id, speed, sstatus);
1323 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1329 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1330 * @ap: SATA port associated with target SATA PHY.
1332 * This function issues commands to standard SATA Sxxx
1333 * PHY registers, to wake up the phy (and device), and
1334 * clear any reset condition.
1337 * PCI/etc. bus probe sem.
1340 void __sata_phy_reset(struct ata_port *ap)
1343 unsigned long timeout = jiffies + (HZ * 5);
1345 if (ap->flags & ATA_FLAG_SATA_RESET) {
1346 /* issue phy wake/reset */
1347 scr_write_flush(ap, SCR_CONTROL, 0x301);
1348 /* Couldn't find anything in SATA I/II specs, but
1349 * AHCI-1.1 10.4.2 says at least 1 ms. */
1352 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1354 /* wait for phy to become ready, if necessary */
1357 sstatus = scr_read(ap, SCR_STATUS);
1358 if ((sstatus & 0xf) != 1)
1360 } while (time_before(jiffies, timeout));
1362 /* print link status */
1363 sata_print_link_status(ap);
1365 /* TODO: phy layer with polling, timeouts, etc. */
1366 if (sata_dev_present(ap))
1369 ata_port_disable(ap);
1371 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1374 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1375 ata_port_disable(ap);
1379 ap->cbl = ATA_CBL_SATA;
1383 * sata_phy_reset - Reset SATA bus.
1384 * @ap: SATA port associated with target SATA PHY.
1386 * This function resets the SATA bus, and then probes
1387 * the bus for devices.
1390 * PCI/etc. bus probe sem.
1393 void sata_phy_reset(struct ata_port *ap)
1395 __sata_phy_reset(ap);
1396 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1402 * ata_port_disable - Disable port.
1403 * @ap: Port to be disabled.
1405 * Modify @ap data structure such that the system
1406 * thinks that the entire port is disabled, and should
1407 * never attempt to probe or communicate with devices
1410 * LOCKING: host_set lock, or some other form of
1414 void ata_port_disable(struct ata_port *ap)
1416 ap->device[0].class = ATA_DEV_NONE;
1417 ap->device[1].class = ATA_DEV_NONE;
1418 ap->flags |= ATA_FLAG_PORT_DISABLED;
1422 * This mode timing computation functionality is ported over from
1423 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1426 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1427 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1428 * for PIO 5, which is a nonstandard extension and UDMA6, which
1429 * is currently supported only by Maxtor drives.
1432 static const struct ata_timing ata_timing[] = {
1434 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1435 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1436 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1437 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1439 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1440 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1441 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1443 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1445 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1446 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1447 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1449 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1450 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1451 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1453 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1454 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1455 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1457 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1458 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1459 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1461 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1466 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1467 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1469 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1471 q->setup = EZ(t->setup * 1000, T);
1472 q->act8b = EZ(t->act8b * 1000, T);
1473 q->rec8b = EZ(t->rec8b * 1000, T);
1474 q->cyc8b = EZ(t->cyc8b * 1000, T);
1475 q->active = EZ(t->active * 1000, T);
1476 q->recover = EZ(t->recover * 1000, T);
1477 q->cycle = EZ(t->cycle * 1000, T);
1478 q->udma = EZ(t->udma * 1000, UT);
1481 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1482 struct ata_timing *m, unsigned int what)
1484 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1485 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1486 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1487 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1488 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1489 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1490 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1491 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1494 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1496 const struct ata_timing *t;
1498 for (t = ata_timing; t->mode != speed; t++)
1499 if (t->mode == 0xFF)
1504 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1505 struct ata_timing *t, int T, int UT)
1507 const struct ata_timing *s;
1508 struct ata_timing p;
1514 if (!(s = ata_timing_find_mode(speed)))
1517 memcpy(t, s, sizeof(*s));
1520 * If the drive is an EIDE drive, it can tell us it needs extended
1521 * PIO/MW_DMA cycle timing.
1524 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1525 memset(&p, 0, sizeof(p));
1526 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1527 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1528 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1529 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1530 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1532 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1536 * Convert the timing to bus clock counts.
1539 ata_timing_quantize(t, t, T, UT);
1542 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1543 * S.M.A.R.T * and some other commands. We have to ensure that the
1544 * DMA cycle timing is slower/equal than the fastest PIO timing.
1547 if (speed > XFER_PIO_4) {
1548 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1549 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1553 * Lengthen active & recovery time so that cycle time is correct.
1556 if (t->act8b + t->rec8b < t->cyc8b) {
1557 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1558 t->rec8b = t->cyc8b - t->act8b;
1561 if (t->active + t->recover < t->cycle) {
1562 t->active += (t->cycle - (t->active + t->recover)) / 2;
1563 t->recover = t->cycle - t->active;
1569 static const struct {
1572 } xfer_mode_classes[] = {
1573 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1574 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1575 { ATA_SHIFT_PIO, XFER_PIO_0 },
1578 static u8 base_from_shift(unsigned int shift)
1582 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1583 if (xfer_mode_classes[i].shift == shift)
1584 return xfer_mode_classes[i].base;
1589 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1594 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1597 if (dev->xfer_shift == ATA_SHIFT_PIO)
1598 dev->flags |= ATA_DFLAG_PIO;
1600 ata_dev_set_xfermode(ap, dev);
1602 base = base_from_shift(dev->xfer_shift);
1603 ofs = dev->xfer_mode - base;
1604 idx = ofs + dev->xfer_shift;
1605 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1607 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1608 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1610 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1611 ap->id, dev->devno, xfer_mode_str[idx]);
1614 static int ata_host_set_pio(struct ata_port *ap)
1620 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1623 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1627 base = base_from_shift(ATA_SHIFT_PIO);
1628 xfer_mode = base + x;
1630 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1631 (int)base, (int)xfer_mode, mask, x);
1633 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1634 struct ata_device *dev = &ap->device[i];
1635 if (ata_dev_present(dev)) {
1636 dev->pio_mode = xfer_mode;
1637 dev->xfer_mode = xfer_mode;
1638 dev->xfer_shift = ATA_SHIFT_PIO;
1639 if (ap->ops->set_piomode)
1640 ap->ops->set_piomode(ap, dev);
1647 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1648 unsigned int xfer_shift)
1652 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1653 struct ata_device *dev = &ap->device[i];
1654 if (ata_dev_present(dev)) {
1655 dev->dma_mode = xfer_mode;
1656 dev->xfer_mode = xfer_mode;
1657 dev->xfer_shift = xfer_shift;
1658 if (ap->ops->set_dmamode)
1659 ap->ops->set_dmamode(ap, dev);
1665 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1666 * @ap: port on which timings will be programmed
1668 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1671 * PCI/etc. bus probe sem.
1673 static void ata_set_mode(struct ata_port *ap)
1675 unsigned int xfer_shift;
1679 /* step 1: always set host PIO timings */
1680 rc = ata_host_set_pio(ap);
1684 /* step 2: choose the best data xfer mode */
1685 xfer_mode = xfer_shift = 0;
1686 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1690 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1691 if (xfer_shift != ATA_SHIFT_PIO)
1692 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1694 /* step 4: update devices' xfer mode */
1695 ata_dev_set_mode(ap, &ap->device[0]);
1696 ata_dev_set_mode(ap, &ap->device[1]);
1698 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1701 if (ap->ops->post_set_mode)
1702 ap->ops->post_set_mode(ap);
1707 ata_port_disable(ap);
1711 * ata_tf_to_host - issue ATA taskfile to host controller
1712 * @ap: port to which command is being issued
1713 * @tf: ATA taskfile register set
1715 * Issues ATA taskfile register set to ATA host controller,
1716 * with proper synchronization with interrupt handler and
1720 * spin_lock_irqsave(host_set lock)
1723 static inline void ata_tf_to_host(struct ata_port *ap,
1724 const struct ata_taskfile *tf)
1726 ap->ops->tf_load(ap, tf);
1727 ap->ops->exec_command(ap, tf);
1731 * ata_busy_sleep - sleep until BSY clears, or timeout
1732 * @ap: port containing status register to be polled
1733 * @tmout_pat: impatience timeout
1734 * @tmout: overall timeout
1736 * Sleep until ATA Status register bit BSY clears,
1737 * or a timeout occurs.
1742 unsigned int ata_busy_sleep (struct ata_port *ap,
1743 unsigned long tmout_pat, unsigned long tmout)
1745 unsigned long timer_start, timeout;
1748 status = ata_busy_wait(ap, ATA_BUSY, 300);
1749 timer_start = jiffies;
1750 timeout = timer_start + tmout_pat;
1751 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1753 status = ata_busy_wait(ap, ATA_BUSY, 3);
1756 if (status & ATA_BUSY)
1757 printk(KERN_WARNING "ata%u is slow to respond, "
1758 "please be patient\n", ap->id);
1760 timeout = timer_start + tmout;
1761 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1763 status = ata_chk_status(ap);
1766 if (status & ATA_BUSY) {
1767 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1768 ap->id, tmout / HZ);
1775 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1777 struct ata_ioports *ioaddr = &ap->ioaddr;
1778 unsigned int dev0 = devmask & (1 << 0);
1779 unsigned int dev1 = devmask & (1 << 1);
1780 unsigned long timeout;
1782 /* if device 0 was found in ata_devchk, wait for its
1786 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1788 /* if device 1 was found in ata_devchk, wait for
1789 * register access, then wait for BSY to clear
1791 timeout = jiffies + ATA_TMOUT_BOOT;
1795 ap->ops->dev_select(ap, 1);
1796 if (ap->flags & ATA_FLAG_MMIO) {
1797 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1798 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1800 nsect = inb(ioaddr->nsect_addr);
1801 lbal = inb(ioaddr->lbal_addr);
1803 if ((nsect == 1) && (lbal == 1))
1805 if (time_after(jiffies, timeout)) {
1809 msleep(50); /* give drive a breather */
1812 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1814 /* is all this really necessary? */
1815 ap->ops->dev_select(ap, 0);
1817 ap->ops->dev_select(ap, 1);
1819 ap->ops->dev_select(ap, 0);
1823 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1824 * @ap: Port to reset and probe
1826 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1827 * probe the bus. Not often used these days.
1830 * PCI/etc. bus probe sem.
1831 * Obtains host_set lock.
1835 static unsigned int ata_bus_edd(struct ata_port *ap)
1837 struct ata_taskfile tf;
1838 unsigned long flags;
1840 /* set up execute-device-diag (bus reset) taskfile */
1841 /* also, take interrupts to a known state (disabled) */
1842 DPRINTK("execute-device-diag\n");
1843 ata_tf_init(ap, &tf, 0);
1845 tf.command = ATA_CMD_EDD;
1846 tf.protocol = ATA_PROT_NODATA;
1849 spin_lock_irqsave(&ap->host_set->lock, flags);
1850 ata_tf_to_host(ap, &tf);
1851 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1853 /* spec says at least 2ms. but who knows with those
1854 * crazy ATAPI devices...
1858 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1861 static unsigned int ata_bus_softreset(struct ata_port *ap,
1862 unsigned int devmask)
1864 struct ata_ioports *ioaddr = &ap->ioaddr;
1866 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1868 /* software reset. causes dev0 to be selected */
1869 if (ap->flags & ATA_FLAG_MMIO) {
1870 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1871 udelay(20); /* FIXME: flush */
1872 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1873 udelay(20); /* FIXME: flush */
1874 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1876 outb(ap->ctl, ioaddr->ctl_addr);
1878 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1880 outb(ap->ctl, ioaddr->ctl_addr);
1883 /* spec mandates ">= 2ms" before checking status.
1884 * We wait 150ms, because that was the magic delay used for
1885 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1886 * between when the ATA command register is written, and then
1887 * status is checked. Because waiting for "a while" before
1888 * checking status is fine, post SRST, we perform this magic
1889 * delay here as well.
1893 ata_bus_post_reset(ap, devmask);
1899 * ata_bus_reset - reset host port and associated ATA channel
1900 * @ap: port to reset
1902 * This is typically the first time we actually start issuing
1903 * commands to the ATA channel. We wait for BSY to clear, then
1904 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1905 * result. Determine what devices, if any, are on the channel
1906 * by looking at the device 0/1 error register. Look at the signature
1907 * stored in each device's taskfile registers, to determine if
1908 * the device is ATA or ATAPI.
1911 * PCI/etc. bus probe sem.
1912 * Obtains host_set lock.
1915 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1918 void ata_bus_reset(struct ata_port *ap)
1920 struct ata_ioports *ioaddr = &ap->ioaddr;
1921 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1923 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1925 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1927 /* determine if device 0/1 are present */
1928 if (ap->flags & ATA_FLAG_SATA_RESET)
1931 dev0 = ata_devchk(ap, 0);
1933 dev1 = ata_devchk(ap, 1);
1937 devmask |= (1 << 0);
1939 devmask |= (1 << 1);
1941 /* select device 0 again */
1942 ap->ops->dev_select(ap, 0);
1944 /* issue bus reset */
1945 if (ap->flags & ATA_FLAG_SRST)
1946 rc = ata_bus_softreset(ap, devmask);
1947 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1948 /* set up device control */
1949 if (ap->flags & ATA_FLAG_MMIO)
1950 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1952 outb(ap->ctl, ioaddr->ctl_addr);
1953 rc = ata_bus_edd(ap);
1960 * determine by signature whether we have ATA or ATAPI devices
1962 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1963 if ((slave_possible) && (err != 0x81))
1964 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1966 /* re-enable interrupts */
1967 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1970 /* is double-select really necessary? */
1971 if (ap->device[1].class != ATA_DEV_NONE)
1972 ap->ops->dev_select(ap, 1);
1973 if (ap->device[0].class != ATA_DEV_NONE)
1974 ap->ops->dev_select(ap, 0);
1976 /* if no devices were detected, disable this port */
1977 if ((ap->device[0].class == ATA_DEV_NONE) &&
1978 (ap->device[1].class == ATA_DEV_NONE))
1981 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1982 /* set up device control for ATA_FLAG_SATA_RESET */
1983 if (ap->flags & ATA_FLAG_MMIO)
1984 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1986 outb(ap->ctl, ioaddr->ctl_addr);
1993 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1994 ap->ops->port_disable(ap);
1999 static int sata_phy_resume(struct ata_port *ap)
2001 unsigned long timeout = jiffies + (HZ * 5);
2004 scr_write_flush(ap, SCR_CONTROL, 0x300);
2006 /* Wait for phy to become ready, if necessary. */
2009 sstatus = scr_read(ap, SCR_STATUS);
2010 if ((sstatus & 0xf) != 1)
2012 } while (time_before(jiffies, timeout));
2018 * ata_std_probeinit - initialize probing
2019 * @ap: port to be probed
2021 * @ap is about to be probed. Initialize it. This function is
2022 * to be used as standard callback for ata_drive_probe_reset().
2024 * NOTE!!! Do not use this function as probeinit if a low level
2025 * driver implements only hardreset. Just pass NULL as probeinit
2026 * in that case. Using this function is probably okay but doing
2027 * so makes reset sequence different from the original
2028 * ->phy_reset implementation and Jeff nervous. :-P
2030 extern void ata_std_probeinit(struct ata_port *ap)
2032 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2033 sata_phy_resume(ap);
2034 if (sata_dev_present(ap))
2035 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2040 * ata_std_softreset - reset host port via ATA SRST
2041 * @ap: port to reset
2042 * @verbose: fail verbosely
2043 * @classes: resulting classes of attached devices
2045 * Reset host port using ATA SRST. This function is to be used
2046 * as standard callback for ata_drive_*_reset() functions.
2049 * Kernel thread context (may sleep)
2052 * 0 on success, -errno otherwise.
2054 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2056 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2057 unsigned int devmask = 0, err_mask;
2062 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2063 classes[0] = ATA_DEV_NONE;
2067 /* determine if device 0/1 are present */
2068 if (ata_devchk(ap, 0))
2069 devmask |= (1 << 0);
2070 if (slave_possible && ata_devchk(ap, 1))
2071 devmask |= (1 << 1);
2073 /* select device 0 again */
2074 ap->ops->dev_select(ap, 0);
2076 /* issue bus reset */
2077 DPRINTK("about to softreset, devmask=%x\n", devmask);
2078 err_mask = ata_bus_softreset(ap, devmask);
2081 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2084 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2089 /* determine by signature whether we have ATA or ATAPI devices */
2090 classes[0] = ata_dev_try_classify(ap, 0, &err);
2091 if (slave_possible && err != 0x81)
2092 classes[1] = ata_dev_try_classify(ap, 1, &err);
2095 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2100 * sata_std_hardreset - reset host port via SATA phy reset
2101 * @ap: port to reset
2102 * @verbose: fail verbosely
2103 * @class: resulting class of attached device
2105 * SATA phy-reset host port using DET bits of SControl register.
2106 * This function is to be used as standard callback for
2107 * ata_drive_*_reset().
2110 * Kernel thread context (may sleep)
2113 * 0 on success, -errno otherwise.
2115 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2119 /* Issue phy wake/reset */
2120 scr_write_flush(ap, SCR_CONTROL, 0x301);
2123 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2124 * 10.4.2 says at least 1 ms.
2128 /* Bring phy back */
2129 sata_phy_resume(ap);
2131 /* TODO: phy layer with polling, timeouts, etc. */
2132 if (!sata_dev_present(ap)) {
2133 *class = ATA_DEV_NONE;
2134 DPRINTK("EXIT, link offline\n");
2138 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2140 printk(KERN_ERR "ata%u: COMRESET failed "
2141 "(device not ready)\n", ap->id);
2143 DPRINTK("EXIT, device not ready\n");
2147 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2149 *class = ata_dev_try_classify(ap, 0, NULL);
2151 DPRINTK("EXIT, class=%u\n", *class);
2156 * ata_std_postreset - standard postreset callback
2157 * @ap: the target ata_port
2158 * @classes: classes of attached devices
2160 * This function is invoked after a successful reset. Note that
2161 * the device might have been reset more than once using
2162 * different reset methods before postreset is invoked.
2164 * This function is to be used as standard callback for
2165 * ata_drive_*_reset().
2168 * Kernel thread context (may sleep)
2170 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2174 /* set cable type if it isn't already set */
2175 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2176 ap->cbl = ATA_CBL_SATA;
2178 /* print link status */
2179 if (ap->cbl == ATA_CBL_SATA)
2180 sata_print_link_status(ap);
2182 /* re-enable interrupts */
2183 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2186 /* is double-select really necessary? */
2187 if (classes[0] != ATA_DEV_NONE)
2188 ap->ops->dev_select(ap, 1);
2189 if (classes[1] != ATA_DEV_NONE)
2190 ap->ops->dev_select(ap, 0);
2192 /* bail out if no device is present */
2193 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2194 DPRINTK("EXIT, no device\n");
2198 /* set up device control */
2199 if (ap->ioaddr.ctl_addr) {
2200 if (ap->flags & ATA_FLAG_MMIO)
2201 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2203 outb(ap->ctl, ap->ioaddr.ctl_addr);
2210 * ata_std_probe_reset - standard probe reset method
2211 * @ap: prot to perform probe-reset
2212 * @classes: resulting classes of attached devices
2214 * The stock off-the-shelf ->probe_reset method.
2217 * Kernel thread context (may sleep)
2220 * 0 on success, -errno otherwise.
2222 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2224 ata_reset_fn_t hardreset;
2227 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2228 hardreset = sata_std_hardreset;
2230 return ata_drive_probe_reset(ap, ata_std_probeinit,
2231 ata_std_softreset, hardreset,
2232 ata_std_postreset, classes);
2235 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2236 ata_postreset_fn_t postreset,
2237 unsigned int *classes)
2241 for (i = 0; i < ATA_MAX_DEVICES; i++)
2242 classes[i] = ATA_DEV_UNKNOWN;
2244 rc = reset(ap, 0, classes);
2248 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2249 * is complete and convert all ATA_DEV_UNKNOWN to
2252 for (i = 0; i < ATA_MAX_DEVICES; i++)
2253 if (classes[i] != ATA_DEV_UNKNOWN)
2256 if (i < ATA_MAX_DEVICES)
2257 for (i = 0; i < ATA_MAX_DEVICES; i++)
2258 if (classes[i] == ATA_DEV_UNKNOWN)
2259 classes[i] = ATA_DEV_NONE;
2262 postreset(ap, classes);
2264 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2268 * ata_drive_probe_reset - Perform probe reset with given methods
2269 * @ap: port to reset
2270 * @probeinit: probeinit method (can be NULL)
2271 * @softreset: softreset method (can be NULL)
2272 * @hardreset: hardreset method (can be NULL)
2273 * @postreset: postreset method (can be NULL)
2274 * @classes: resulting classes of attached devices
2276 * Reset the specified port and classify attached devices using
2277 * given methods. This function prefers softreset but tries all
2278 * possible reset sequences to reset and classify devices. This
2279 * function is intended to be used for constructing ->probe_reset
2280 * callback by low level drivers.
2282 * Reset methods should follow the following rules.
2284 * - Return 0 on sucess, -errno on failure.
2285 * - If classification is supported, fill classes[] with
2286 * recognized class codes.
2287 * - If classification is not supported, leave classes[] alone.
2288 * - If verbose is non-zero, print error message on failure;
2289 * otherwise, shut up.
2292 * Kernel thread context (may sleep)
2295 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2296 * if classification fails, and any error code from reset
2299 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2300 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2301 ata_postreset_fn_t postreset, unsigned int *classes)
2309 rc = do_probe_reset(ap, softreset, postreset, classes);
2317 rc = do_probe_reset(ap, hardreset, postreset, classes);
2318 if (rc == 0 || rc != -ENODEV)
2322 rc = do_probe_reset(ap, softreset, postreset, classes);
2327 static void ata_pr_blacklisted(const struct ata_port *ap,
2328 const struct ata_device *dev)
2330 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2331 ap->id, dev->devno);
2334 static const char * const ata_dma_blacklist [] = {
2353 "Toshiba CD-ROM XM-6202B",
2354 "TOSHIBA CD-ROM XM-1702BC",
2356 "E-IDE CD-ROM CR-840",
2359 "SAMSUNG CD-ROM SC-148C",
2360 "SAMSUNG CD-ROM SC",
2362 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2366 static int ata_dma_blacklisted(const struct ata_device *dev)
2368 unsigned char model_num[41];
2371 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2373 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2374 if (!strcmp(ata_dma_blacklist[i], model_num))
2380 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2382 const struct ata_device *master, *slave;
2385 master = &ap->device[0];
2386 slave = &ap->device[1];
2388 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2390 if (shift == ATA_SHIFT_UDMA) {
2391 mask = ap->udma_mask;
2392 if (ata_dev_present(master)) {
2393 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2394 if (ata_dma_blacklisted(master)) {
2396 ata_pr_blacklisted(ap, master);
2399 if (ata_dev_present(slave)) {
2400 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2401 if (ata_dma_blacklisted(slave)) {
2403 ata_pr_blacklisted(ap, slave);
2407 else if (shift == ATA_SHIFT_MWDMA) {
2408 mask = ap->mwdma_mask;
2409 if (ata_dev_present(master)) {
2410 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2411 if (ata_dma_blacklisted(master)) {
2413 ata_pr_blacklisted(ap, master);
2416 if (ata_dev_present(slave)) {
2417 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2418 if (ata_dma_blacklisted(slave)) {
2420 ata_pr_blacklisted(ap, slave);
2424 else if (shift == ATA_SHIFT_PIO) {
2425 mask = ap->pio_mask;
2426 if (ata_dev_present(master)) {
2427 /* spec doesn't return explicit support for
2428 * PIO0-2, so we fake it
2430 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2435 if (ata_dev_present(slave)) {
2436 /* spec doesn't return explicit support for
2437 * PIO0-2, so we fake it
2439 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2446 mask = 0xffffffff; /* shut up compiler warning */
2453 /* find greatest bit */
2454 static int fgb(u32 bitmap)
2459 for (i = 0; i < 32; i++)
2460 if (bitmap & (1 << i))
2467 * ata_choose_xfer_mode - attempt to find best transfer mode
2468 * @ap: Port for which an xfer mode will be selected
2469 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2470 * @xfer_shift_out: (output) bit shift that selects this mode
2472 * Based on host and device capabilities, determine the
2473 * maximum transfer mode that is amenable to all.
2476 * PCI/etc. bus probe sem.
2479 * Zero on success, negative on error.
2482 static int ata_choose_xfer_mode(const struct ata_port *ap,
2484 unsigned int *xfer_shift_out)
2486 unsigned int mask, shift;
2489 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2490 shift = xfer_mode_classes[i].shift;
2491 mask = ata_get_mode_mask(ap, shift);
2495 *xfer_mode_out = xfer_mode_classes[i].base + x;
2496 *xfer_shift_out = shift;
2505 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2506 * @ap: Port associated with device @dev
2507 * @dev: Device to which command will be sent
2509 * Issue SET FEATURES - XFER MODE command to device @dev
2513 * PCI/etc. bus probe sem.
2516 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2518 struct ata_taskfile tf;
2520 /* set up set-features taskfile */
2521 DPRINTK("set features - xfer mode\n");
2523 ata_tf_init(ap, &tf, dev->devno);
2524 tf.command = ATA_CMD_SET_FEATURES;
2525 tf.feature = SETFEATURES_XFER;
2526 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2527 tf.protocol = ATA_PROT_NODATA;
2528 tf.nsect = dev->xfer_mode;
2530 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2531 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2533 ata_port_disable(ap);
2540 * ata_dev_init_params - Issue INIT DEV PARAMS command
2541 * @ap: Port associated with device @dev
2542 * @dev: Device to which command will be sent
2545 * Kernel thread context (may sleep)
2548 * 0 on success, AC_ERR_* mask otherwise.
2551 static unsigned int ata_dev_init_params(struct ata_port *ap,
2552 struct ata_device *dev)
2554 struct ata_taskfile tf;
2555 unsigned int err_mask;
2556 u16 sectors = dev->id[6];
2557 u16 heads = dev->id[3];
2559 /* Number of sectors per track 1-255. Number of heads 1-16 */
2560 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2563 /* set up init dev params taskfile */
2564 DPRINTK("init dev params \n");
2566 ata_tf_init(ap, &tf, dev->devno);
2567 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2568 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2569 tf.protocol = ATA_PROT_NODATA;
2571 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2573 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2575 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2580 * ata_sg_clean - Unmap DMA memory associated with command
2581 * @qc: Command containing DMA memory to be released
2583 * Unmap all mapped DMA memory associated with this command.
2586 * spin_lock_irqsave(host_set lock)
2589 static void ata_sg_clean(struct ata_queued_cmd *qc)
2591 struct ata_port *ap = qc->ap;
2592 struct scatterlist *sg = qc->__sg;
2593 int dir = qc->dma_dir;
2594 void *pad_buf = NULL;
2596 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2597 WARN_ON(sg == NULL);
2599 if (qc->flags & ATA_QCFLAG_SINGLE)
2600 WARN_ON(qc->n_elem > 1);
2602 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2604 /* if we padded the buffer out to 32-bit bound, and data
2605 * xfer direction is from-device, we must copy from the
2606 * pad buffer back into the supplied buffer
2608 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2609 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2611 if (qc->flags & ATA_QCFLAG_SG) {
2613 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2614 /* restore last sg */
2615 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2617 struct scatterlist *psg = &qc->pad_sgent;
2618 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2619 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2620 kunmap_atomic(addr, KM_IRQ0);
2624 dma_unmap_single(ap->host_set->dev,
2625 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2628 sg->length += qc->pad_len;
2630 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2631 pad_buf, qc->pad_len);
2634 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2639 * ata_fill_sg - Fill PCI IDE PRD table
2640 * @qc: Metadata associated with taskfile to be transferred
2642 * Fill PCI IDE PRD (scatter-gather) table with segments
2643 * associated with the current disk command.
2646 * spin_lock_irqsave(host_set lock)
2649 static void ata_fill_sg(struct ata_queued_cmd *qc)
2651 struct ata_port *ap = qc->ap;
2652 struct scatterlist *sg;
2655 WARN_ON(qc->__sg == NULL);
2656 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2659 ata_for_each_sg(sg, qc) {
2663 /* determine if physical DMA addr spans 64K boundary.
2664 * Note h/w doesn't support 64-bit, so we unconditionally
2665 * truncate dma_addr_t to u32.
2667 addr = (u32) sg_dma_address(sg);
2668 sg_len = sg_dma_len(sg);
2671 offset = addr & 0xffff;
2673 if ((offset + sg_len) > 0x10000)
2674 len = 0x10000 - offset;
2676 ap->prd[idx].addr = cpu_to_le32(addr);
2677 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2678 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2687 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2690 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2691 * @qc: Metadata associated with taskfile to check
2693 * Allow low-level driver to filter ATA PACKET commands, returning
2694 * a status indicating whether or not it is OK to use DMA for the
2695 * supplied PACKET command.
2698 * spin_lock_irqsave(host_set lock)
2700 * RETURNS: 0 when ATAPI DMA can be used
2703 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2705 struct ata_port *ap = qc->ap;
2706 int rc = 0; /* Assume ATAPI DMA is OK by default */
2708 if (ap->ops->check_atapi_dma)
2709 rc = ap->ops->check_atapi_dma(qc);
2714 * ata_qc_prep - Prepare taskfile for submission
2715 * @qc: Metadata associated with taskfile to be prepared
2717 * Prepare ATA taskfile for submission.
2720 * spin_lock_irqsave(host_set lock)
2722 void ata_qc_prep(struct ata_queued_cmd *qc)
2724 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2731 * ata_sg_init_one - Associate command with memory buffer
2732 * @qc: Command to be associated
2733 * @buf: Memory buffer
2734 * @buflen: Length of memory buffer, in bytes.
2736 * Initialize the data-related elements of queued_cmd @qc
2737 * to point to a single memory buffer, @buf of byte length @buflen.
2740 * spin_lock_irqsave(host_set lock)
2743 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2745 struct scatterlist *sg;
2747 qc->flags |= ATA_QCFLAG_SINGLE;
2749 memset(&qc->sgent, 0, sizeof(qc->sgent));
2750 qc->__sg = &qc->sgent;
2752 qc->orig_n_elem = 1;
2756 sg_init_one(sg, buf, buflen);
2760 * ata_sg_init - Associate command with scatter-gather table.
2761 * @qc: Command to be associated
2762 * @sg: Scatter-gather table.
2763 * @n_elem: Number of elements in s/g table.
2765 * Initialize the data-related elements of queued_cmd @qc
2766 * to point to a scatter-gather table @sg, containing @n_elem
2770 * spin_lock_irqsave(host_set lock)
2773 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2774 unsigned int n_elem)
2776 qc->flags |= ATA_QCFLAG_SG;
2778 qc->n_elem = n_elem;
2779 qc->orig_n_elem = n_elem;
2783 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2784 * @qc: Command with memory buffer to be mapped.
2786 * DMA-map the memory buffer associated with queued_cmd @qc.
2789 * spin_lock_irqsave(host_set lock)
2792 * Zero on success, negative on error.
2795 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2797 struct ata_port *ap = qc->ap;
2798 int dir = qc->dma_dir;
2799 struct scatterlist *sg = qc->__sg;
2800 dma_addr_t dma_address;
2803 /* we must lengthen transfers to end on a 32-bit boundary */
2804 qc->pad_len = sg->length & 3;
2806 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2807 struct scatterlist *psg = &qc->pad_sgent;
2809 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2811 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2813 if (qc->tf.flags & ATA_TFLAG_WRITE)
2814 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2817 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2818 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2820 sg->length -= qc->pad_len;
2821 if (sg->length == 0)
2824 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2825 sg->length, qc->pad_len);
2833 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2835 if (dma_mapping_error(dma_address)) {
2837 sg->length += qc->pad_len;
2841 sg_dma_address(sg) = dma_address;
2842 sg_dma_len(sg) = sg->length;
2845 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2846 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2852 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2853 * @qc: Command with scatter-gather table to be mapped.
2855 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2858 * spin_lock_irqsave(host_set lock)
2861 * Zero on success, negative on error.
2865 static int ata_sg_setup(struct ata_queued_cmd *qc)
2867 struct ata_port *ap = qc->ap;
2868 struct scatterlist *sg = qc->__sg;
2869 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2870 int n_elem, pre_n_elem, dir, trim_sg = 0;
2872 VPRINTK("ENTER, ata%u\n", ap->id);
2873 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2875 /* we must lengthen transfers to end on a 32-bit boundary */
2876 qc->pad_len = lsg->length & 3;
2878 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2879 struct scatterlist *psg = &qc->pad_sgent;
2880 unsigned int offset;
2882 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2884 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2887 * psg->page/offset are used to copy to-be-written
2888 * data in this function or read data in ata_sg_clean.
2890 offset = lsg->offset + lsg->length - qc->pad_len;
2891 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2892 psg->offset = offset_in_page(offset);
2894 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2895 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2896 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2897 kunmap_atomic(addr, KM_IRQ0);
2900 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2901 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2903 lsg->length -= qc->pad_len;
2904 if (lsg->length == 0)
2907 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2908 qc->n_elem - 1, lsg->length, qc->pad_len);
2911 pre_n_elem = qc->n_elem;
2912 if (trim_sg && pre_n_elem)
2921 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2923 /* restore last sg */
2924 lsg->length += qc->pad_len;
2928 DPRINTK("%d sg elements mapped\n", n_elem);
2931 qc->n_elem = n_elem;
2937 * ata_poll_qc_complete - turn irq back on and finish qc
2938 * @qc: Command to complete
2939 * @err_mask: ATA status register content
2942 * None. (grabs host lock)
2945 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2947 struct ata_port *ap = qc->ap;
2948 unsigned long flags;
2950 spin_lock_irqsave(&ap->host_set->lock, flags);
2951 ap->flags &= ~ATA_FLAG_NOINTR;
2953 ata_qc_complete(qc);
2954 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2958 * ata_pio_poll - poll using PIO, depending on current state
2959 * @ap: the target ata_port
2962 * None. (executing in kernel thread context)
2965 * timeout value to use
2968 static unsigned long ata_pio_poll(struct ata_port *ap)
2970 struct ata_queued_cmd *qc;
2972 unsigned int poll_state = HSM_ST_UNKNOWN;
2973 unsigned int reg_state = HSM_ST_UNKNOWN;
2975 qc = ata_qc_from_tag(ap, ap->active_tag);
2976 WARN_ON(qc == NULL);
2978 switch (ap->hsm_task_state) {
2981 poll_state = HSM_ST_POLL;
2985 case HSM_ST_LAST_POLL:
2986 poll_state = HSM_ST_LAST_POLL;
2987 reg_state = HSM_ST_LAST;
2994 status = ata_chk_status(ap);
2995 if (status & ATA_BUSY) {
2996 if (time_after(jiffies, ap->pio_task_timeout)) {
2997 qc->err_mask |= AC_ERR_TIMEOUT;
2998 ap->hsm_task_state = HSM_ST_TMOUT;
3001 ap->hsm_task_state = poll_state;
3002 return ATA_SHORT_PAUSE;
3005 ap->hsm_task_state = reg_state;
3010 * ata_pio_complete - check if drive is busy or idle
3011 * @ap: the target ata_port
3014 * None. (executing in kernel thread context)
3017 * Non-zero if qc completed, zero otherwise.
3020 static int ata_pio_complete (struct ata_port *ap)
3022 struct ata_queued_cmd *qc;
3026 * This is purely heuristic. This is a fast path. Sometimes when
3027 * we enter, BSY will be cleared in a chk-status or two. If not,
3028 * the drive is probably seeking or something. Snooze for a couple
3029 * msecs, then chk-status again. If still busy, fall back to
3030 * HSM_ST_POLL state.
3032 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3033 if (drv_stat & ATA_BUSY) {
3035 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3036 if (drv_stat & ATA_BUSY) {
3037 ap->hsm_task_state = HSM_ST_LAST_POLL;
3038 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3043 qc = ata_qc_from_tag(ap, ap->active_tag);
3044 WARN_ON(qc == NULL);
3046 drv_stat = ata_wait_idle(ap);
3047 if (!ata_ok(drv_stat)) {
3048 qc->err_mask |= __ac_err_mask(drv_stat);
3049 ap->hsm_task_state = HSM_ST_ERR;
3053 ap->hsm_task_state = HSM_ST_IDLE;
3055 WARN_ON(qc->err_mask);
3056 ata_poll_qc_complete(qc);
3058 /* another command may start at this point */
3065 * swap_buf_le16 - swap halves of 16-bit words in place
3066 * @buf: Buffer to swap
3067 * @buf_words: Number of 16-bit words in buffer.
3069 * Swap halves of 16-bit words if needed to convert from
3070 * little-endian byte order to native cpu byte order, or
3074 * Inherited from caller.
3076 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3081 for (i = 0; i < buf_words; i++)
3082 buf[i] = le16_to_cpu(buf[i]);
3083 #endif /* __BIG_ENDIAN */
3087 * ata_mmio_data_xfer - Transfer data by MMIO
3088 * @ap: port to read/write
3090 * @buflen: buffer length
3091 * @write_data: read/write
3093 * Transfer data from/to the device data register by MMIO.
3096 * Inherited from caller.
3099 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3100 unsigned int buflen, int write_data)
3103 unsigned int words = buflen >> 1;
3104 u16 *buf16 = (u16 *) buf;
3105 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3107 /* Transfer multiple of 2 bytes */
3109 for (i = 0; i < words; i++)
3110 writew(le16_to_cpu(buf16[i]), mmio);
3112 for (i = 0; i < words; i++)
3113 buf16[i] = cpu_to_le16(readw(mmio));
3116 /* Transfer trailing 1 byte, if any. */
3117 if (unlikely(buflen & 0x01)) {
3118 u16 align_buf[1] = { 0 };
3119 unsigned char *trailing_buf = buf + buflen - 1;
3122 memcpy(align_buf, trailing_buf, 1);
3123 writew(le16_to_cpu(align_buf[0]), mmio);
3125 align_buf[0] = cpu_to_le16(readw(mmio));
3126 memcpy(trailing_buf, align_buf, 1);
3132 * ata_pio_data_xfer - Transfer data by PIO
3133 * @ap: port to read/write
3135 * @buflen: buffer length
3136 * @write_data: read/write
3138 * Transfer data from/to the device data register by PIO.
3141 * Inherited from caller.
3144 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3145 unsigned int buflen, int write_data)
3147 unsigned int words = buflen >> 1;
3149 /* Transfer multiple of 2 bytes */
3151 outsw(ap->ioaddr.data_addr, buf, words);
3153 insw(ap->ioaddr.data_addr, buf, words);
3155 /* Transfer trailing 1 byte, if any. */
3156 if (unlikely(buflen & 0x01)) {
3157 u16 align_buf[1] = { 0 };
3158 unsigned char *trailing_buf = buf + buflen - 1;
3161 memcpy(align_buf, trailing_buf, 1);
3162 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3164 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3165 memcpy(trailing_buf, align_buf, 1);
3171 * ata_data_xfer - Transfer data from/to the data register.
3172 * @ap: port to read/write
3174 * @buflen: buffer length
3175 * @do_write: read/write
3177 * Transfer data from/to the device data register.
3180 * Inherited from caller.
3183 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3184 unsigned int buflen, int do_write)
3186 /* Make the crap hardware pay the costs not the good stuff */
3187 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3188 unsigned long flags;
3189 local_irq_save(flags);
3190 if (ap->flags & ATA_FLAG_MMIO)
3191 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3193 ata_pio_data_xfer(ap, buf, buflen, do_write);
3194 local_irq_restore(flags);
3196 if (ap->flags & ATA_FLAG_MMIO)
3197 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3199 ata_pio_data_xfer(ap, buf, buflen, do_write);
3204 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3205 * @qc: Command on going
3207 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3210 * Inherited from caller.
3213 static void ata_pio_sector(struct ata_queued_cmd *qc)
3215 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3216 struct scatterlist *sg = qc->__sg;
3217 struct ata_port *ap = qc->ap;
3219 unsigned int offset;
3222 if (qc->cursect == (qc->nsect - 1))
3223 ap->hsm_task_state = HSM_ST_LAST;
3225 page = sg[qc->cursg].page;
3226 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3228 /* get the current page and offset */
3229 page = nth_page(page, (offset >> PAGE_SHIFT));
3230 offset %= PAGE_SIZE;
3232 buf = kmap(page) + offset;
3237 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3242 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3244 /* do the actual data transfer */
3245 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3246 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3252 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3253 * @qc: Command on going
3254 * @bytes: number of bytes
3256 * Transfer Transfer data from/to the ATAPI device.
3259 * Inherited from caller.
3263 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3265 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3266 struct scatterlist *sg = qc->__sg;
3267 struct ata_port *ap = qc->ap;
3270 unsigned int offset, count;
3272 if (qc->curbytes + bytes >= qc->nbytes)
3273 ap->hsm_task_state = HSM_ST_LAST;
3276 if (unlikely(qc->cursg >= qc->n_elem)) {
3278 * The end of qc->sg is reached and the device expects
3279 * more data to transfer. In order not to overrun qc->sg
3280 * and fulfill length specified in the byte count register,
3281 * - for read case, discard trailing data from the device
3282 * - for write case, padding zero data to the device
3284 u16 pad_buf[1] = { 0 };
3285 unsigned int words = bytes >> 1;
3288 if (words) /* warning if bytes > 1 */
3289 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3292 for (i = 0; i < words; i++)
3293 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3295 ap->hsm_task_state = HSM_ST_LAST;
3299 sg = &qc->__sg[qc->cursg];
3302 offset = sg->offset + qc->cursg_ofs;
3304 /* get the current page and offset */
3305 page = nth_page(page, (offset >> PAGE_SHIFT));
3306 offset %= PAGE_SIZE;
3308 /* don't overrun current sg */
3309 count = min(sg->length - qc->cursg_ofs, bytes);
3311 /* don't cross page boundaries */
3312 count = min(count, (unsigned int)PAGE_SIZE - offset);
3314 buf = kmap(page) + offset;
3317 qc->curbytes += count;
3318 qc->cursg_ofs += count;
3320 if (qc->cursg_ofs == sg->length) {
3325 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3327 /* do the actual data transfer */
3328 ata_data_xfer(ap, buf, count, do_write);
3337 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3338 * @qc: Command on going
3340 * Transfer Transfer data from/to the ATAPI device.
3343 * Inherited from caller.
3346 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3348 struct ata_port *ap = qc->ap;
3349 struct ata_device *dev = qc->dev;
3350 unsigned int ireason, bc_lo, bc_hi, bytes;
3351 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3353 ap->ops->tf_read(ap, &qc->tf);
3354 ireason = qc->tf.nsect;
3355 bc_lo = qc->tf.lbam;
3356 bc_hi = qc->tf.lbah;
3357 bytes = (bc_hi << 8) | bc_lo;
3359 /* shall be cleared to zero, indicating xfer of data */
3360 if (ireason & (1 << 0))
3363 /* make sure transfer direction matches expected */
3364 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3365 if (do_write != i_write)
3368 __atapi_pio_bytes(qc, bytes);
3373 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3374 ap->id, dev->devno);
3375 qc->err_mask |= AC_ERR_HSM;
3376 ap->hsm_task_state = HSM_ST_ERR;
3380 * ata_pio_block - start PIO on a block
3381 * @ap: the target ata_port
3384 * None. (executing in kernel thread context)
3387 static void ata_pio_block(struct ata_port *ap)
3389 struct ata_queued_cmd *qc;
3393 * This is purely heuristic. This is a fast path.
3394 * Sometimes when we enter, BSY will be cleared in
3395 * a chk-status or two. If not, the drive is probably seeking
3396 * or something. Snooze for a couple msecs, then
3397 * chk-status again. If still busy, fall back to
3398 * HSM_ST_POLL state.