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/
33 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
49 #include <linux/spinlock.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
52 #include <linux/timer.h>
53 #include <linux/interrupt.h>
54 #include <linux/completion.h>
55 #include <linux/suspend.h>
56 #include <linux/workqueue.h>
57 #include <linux/jiffies.h>
58 #include <linux/scatterlist.h>
60 #include <scsi/scsi.h>
61 #include <scsi/scsi_cmnd.h>
62 #include <scsi/scsi_host.h>
63 #include <linux/libata.h>
64 #include <asm/semaphore.h>
65 #include <asm/byteorder.h>
66 #include <linux/cdrom.h>
71 /* debounce timing parameters in msecs { interval, duration, timeout } */
72 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
73 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
74 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
76 const struct ata_port_operations ata_base_port_ops = {
77 .irq_clear = ata_noop_irq_clear,
78 .prereset = ata_std_prereset,
79 .hardreset = sata_std_hardreset,
80 .postreset = ata_std_postreset,
81 .error_handler = ata_std_error_handler,
84 const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
87 .qc_defer = ata_std_qc_defer,
88 .dev_select = ata_noop_dev_select,
91 const struct ata_port_operations sata_pmp_port_ops = {
92 .inherits = &sata_port_ops,
94 .pmp_prereset = sata_pmp_std_prereset,
95 .pmp_hardreset = sata_pmp_std_hardreset,
96 .pmp_postreset = sata_pmp_std_postreset,
97 .error_handler = sata_pmp_error_handler,
100 static unsigned int ata_dev_init_params(struct ata_device *dev,
101 u16 heads, u16 sectors);
102 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
103 static unsigned int ata_dev_set_feature(struct ata_device *dev,
104 u8 enable, u8 feature);
105 static void ata_dev_xfermask(struct ata_device *dev);
106 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
108 unsigned int ata_print_id = 1;
109 static struct workqueue_struct *ata_wq;
111 struct workqueue_struct *ata_aux_wq;
113 struct ata_force_param {
117 unsigned long xfer_mask;
118 unsigned int horkage_on;
119 unsigned int horkage_off;
122 struct ata_force_ent {
125 struct ata_force_param param;
128 static struct ata_force_ent *ata_force_tbl;
129 static int ata_force_tbl_size;
131 static char ata_force_param_buf[PAGE_SIZE] __initdata;
132 /* param_buf is thrown away after initialization, disallow read */
133 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
134 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
136 int atapi_enabled = 1;
137 module_param(atapi_enabled, int, 0444);
138 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
140 static int atapi_dmadir = 0;
141 module_param(atapi_dmadir, int, 0444);
142 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
144 int atapi_passthru16 = 1;
145 module_param(atapi_passthru16, int, 0444);
146 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
149 module_param_named(fua, libata_fua, int, 0444);
150 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
152 static int ata_ignore_hpa;
153 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
154 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
156 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
157 module_param_named(dma, libata_dma_mask, int, 0444);
158 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
160 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
161 module_param(ata_probe_timeout, int, 0444);
162 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
164 int libata_noacpi = 0;
165 module_param_named(noacpi, libata_noacpi, int, 0444);
166 MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
168 int libata_allow_tpm = 0;
169 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
170 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
172 MODULE_AUTHOR("Jeff Garzik");
173 MODULE_DESCRIPTION("Library module for ATA devices");
174 MODULE_LICENSE("GPL");
175 MODULE_VERSION(DRV_VERSION);
179 * ata_force_cbl - force cable type according to libata.force
180 * @ap: ATA port of interest
182 * Force cable type according to libata.force and whine about it.
183 * The last entry which has matching port number is used, so it
184 * can be specified as part of device force parameters. For
185 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
191 void ata_force_cbl(struct ata_port *ap)
195 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
196 const struct ata_force_ent *fe = &ata_force_tbl[i];
198 if (fe->port != -1 && fe->port != ap->print_id)
201 if (fe->param.cbl == ATA_CBL_NONE)
204 ap->cbl = fe->param.cbl;
205 ata_port_printk(ap, KERN_NOTICE,
206 "FORCE: cable set to %s\n", fe->param.name);
212 * ata_force_spd_limit - force SATA spd limit according to libata.force
213 * @link: ATA link of interest
215 * Force SATA spd limit according to libata.force and whine about
216 * it. When only the port part is specified (e.g. 1:), the limit
217 * applies to all links connected to both the host link and all
218 * fan-out ports connected via PMP. If the device part is
219 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
220 * link not the host link. Device number 15 always points to the
221 * host link whether PMP is attached or not.
226 static void ata_force_spd_limit(struct ata_link *link)
230 if (ata_is_host_link(link))
235 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
236 const struct ata_force_ent *fe = &ata_force_tbl[i];
238 if (fe->port != -1 && fe->port != link->ap->print_id)
241 if (fe->device != -1 && fe->device != linkno)
244 if (!fe->param.spd_limit)
247 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
248 ata_link_printk(link, KERN_NOTICE,
249 "FORCE: PHY spd limit set to %s\n", fe->param.name);
255 * ata_force_xfermask - force xfermask according to libata.force
256 * @dev: ATA device of interest
258 * Force xfer_mask according to libata.force and whine about it.
259 * For consistency with link selection, device number 15 selects
260 * the first device connected to the host link.
265 static void ata_force_xfermask(struct ata_device *dev)
267 int devno = dev->link->pmp + dev->devno;
268 int alt_devno = devno;
271 /* allow n.15 for the first device attached to host port */
272 if (ata_is_host_link(dev->link) && devno == 0)
275 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
276 const struct ata_force_ent *fe = &ata_force_tbl[i];
277 unsigned long pio_mask, mwdma_mask, udma_mask;
279 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
282 if (fe->device != -1 && fe->device != devno &&
283 fe->device != alt_devno)
286 if (!fe->param.xfer_mask)
289 ata_unpack_xfermask(fe->param.xfer_mask,
290 &pio_mask, &mwdma_mask, &udma_mask);
292 dev->udma_mask = udma_mask;
293 else if (mwdma_mask) {
295 dev->mwdma_mask = mwdma_mask;
299 dev->pio_mask = pio_mask;
302 ata_dev_printk(dev, KERN_NOTICE,
303 "FORCE: xfer_mask set to %s\n", fe->param.name);
309 * ata_force_horkage - force horkage according to libata.force
310 * @dev: ATA device of interest
312 * Force horkage according to libata.force and whine about it.
313 * For consistency with link selection, device number 15 selects
314 * the first device connected to the host link.
319 static void ata_force_horkage(struct ata_device *dev)
321 int devno = dev->link->pmp + dev->devno;
322 int alt_devno = devno;
325 /* allow n.15 for the first device attached to host port */
326 if (ata_is_host_link(dev->link) && devno == 0)
329 for (i = 0; i < ata_force_tbl_size; i++) {
330 const struct ata_force_ent *fe = &ata_force_tbl[i];
332 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
335 if (fe->device != -1 && fe->device != devno &&
336 fe->device != alt_devno)
339 if (!(~dev->horkage & fe->param.horkage_on) &&
340 !(dev->horkage & fe->param.horkage_off))
343 dev->horkage |= fe->param.horkage_on;
344 dev->horkage &= ~fe->param.horkage_off;
346 ata_dev_printk(dev, KERN_NOTICE,
347 "FORCE: horkage modified (%s)\n", fe->param.name);
352 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
353 * @opcode: SCSI opcode
355 * Determine ATAPI command type from @opcode.
361 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
363 int atapi_cmd_type(u8 opcode)
372 case GPCMD_WRITE_AND_VERIFY_10:
376 case GPCMD_READ_CD_MSF:
377 return ATAPI_READ_CD;
381 if (atapi_passthru16)
382 return ATAPI_PASS_THRU;
390 * ata_noop_irq_clear - Noop placeholder for irq_clear
391 * @ap: Port associated with this ATA transaction.
393 void ata_noop_irq_clear(struct ata_port *ap)
398 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
399 * @tf: Taskfile to convert
400 * @pmp: Port multiplier port
401 * @is_cmd: This FIS is for command
402 * @fis: Buffer into which data will output
404 * Converts a standard ATA taskfile to a Serial ATA
405 * FIS structure (Register - Host to Device).
408 * Inherited from caller.
410 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
412 fis[0] = 0x27; /* Register - Host to Device FIS */
413 fis[1] = pmp & 0xf; /* Port multiplier number*/
415 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
417 fis[2] = tf->command;
418 fis[3] = tf->feature;
425 fis[8] = tf->hob_lbal;
426 fis[9] = tf->hob_lbam;
427 fis[10] = tf->hob_lbah;
428 fis[11] = tf->hob_feature;
431 fis[13] = tf->hob_nsect;
442 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
443 * @fis: Buffer from which data will be input
444 * @tf: Taskfile to output
446 * Converts a serial ATA FIS structure to a standard ATA taskfile.
449 * Inherited from caller.
452 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
454 tf->command = fis[2]; /* status */
455 tf->feature = fis[3]; /* error */
462 tf->hob_lbal = fis[8];
463 tf->hob_lbam = fis[9];
464 tf->hob_lbah = fis[10];
467 tf->hob_nsect = fis[13];
470 static const u8 ata_rw_cmds[] = {
474 ATA_CMD_READ_MULTI_EXT,
475 ATA_CMD_WRITE_MULTI_EXT,
479 ATA_CMD_WRITE_MULTI_FUA_EXT,
483 ATA_CMD_PIO_READ_EXT,
484 ATA_CMD_PIO_WRITE_EXT,
497 ATA_CMD_WRITE_FUA_EXT
501 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
502 * @tf: command to examine and configure
503 * @dev: device tf belongs to
505 * Examine the device configuration and tf->flags to calculate
506 * the proper read/write commands and protocol to use.
511 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
515 int index, fua, lba48, write;
517 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
518 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
519 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
521 if (dev->flags & ATA_DFLAG_PIO) {
522 tf->protocol = ATA_PROT_PIO;
523 index = dev->multi_count ? 0 : 8;
524 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
525 /* Unable to use DMA due to host limitation */
526 tf->protocol = ATA_PROT_PIO;
527 index = dev->multi_count ? 0 : 8;
529 tf->protocol = ATA_PROT_DMA;
533 cmd = ata_rw_cmds[index + fua + lba48 + write];
542 * ata_tf_read_block - Read block address from ATA taskfile
543 * @tf: ATA taskfile of interest
544 * @dev: ATA device @tf belongs to
549 * Read block address from @tf. This function can handle all
550 * three address formats - LBA, LBA48 and CHS. tf->protocol and
551 * flags select the address format to use.
554 * Block address read from @tf.
556 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
560 if (tf->flags & ATA_TFLAG_LBA) {
561 if (tf->flags & ATA_TFLAG_LBA48) {
562 block |= (u64)tf->hob_lbah << 40;
563 block |= (u64)tf->hob_lbam << 32;
564 block |= tf->hob_lbal << 24;
566 block |= (tf->device & 0xf) << 24;
568 block |= tf->lbah << 16;
569 block |= tf->lbam << 8;
574 cyl = tf->lbam | (tf->lbah << 8);
575 head = tf->device & 0xf;
578 block = (cyl * dev->heads + head) * dev->sectors + sect;
585 * ata_build_rw_tf - Build ATA taskfile for given read/write request
586 * @tf: Target ATA taskfile
587 * @dev: ATA device @tf belongs to
588 * @block: Block address
589 * @n_block: Number of blocks
590 * @tf_flags: RW/FUA etc...
596 * Build ATA taskfile @tf for read/write request described by
597 * @block, @n_block, @tf_flags and @tag on @dev.
601 * 0 on success, -ERANGE if the request is too large for @dev,
602 * -EINVAL if the request is invalid.
604 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
605 u64 block, u32 n_block, unsigned int tf_flags,
608 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
609 tf->flags |= tf_flags;
611 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
613 if (!lba_48_ok(block, n_block))
616 tf->protocol = ATA_PROT_NCQ;
617 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
619 if (tf->flags & ATA_TFLAG_WRITE)
620 tf->command = ATA_CMD_FPDMA_WRITE;
622 tf->command = ATA_CMD_FPDMA_READ;
624 tf->nsect = tag << 3;
625 tf->hob_feature = (n_block >> 8) & 0xff;
626 tf->feature = n_block & 0xff;
628 tf->hob_lbah = (block >> 40) & 0xff;
629 tf->hob_lbam = (block >> 32) & 0xff;
630 tf->hob_lbal = (block >> 24) & 0xff;
631 tf->lbah = (block >> 16) & 0xff;
632 tf->lbam = (block >> 8) & 0xff;
633 tf->lbal = block & 0xff;
636 if (tf->flags & ATA_TFLAG_FUA)
637 tf->device |= 1 << 7;
638 } else if (dev->flags & ATA_DFLAG_LBA) {
639 tf->flags |= ATA_TFLAG_LBA;
641 if (lba_28_ok(block, n_block)) {
643 tf->device |= (block >> 24) & 0xf;
644 } else if (lba_48_ok(block, n_block)) {
645 if (!(dev->flags & ATA_DFLAG_LBA48))
649 tf->flags |= ATA_TFLAG_LBA48;
651 tf->hob_nsect = (n_block >> 8) & 0xff;
653 tf->hob_lbah = (block >> 40) & 0xff;
654 tf->hob_lbam = (block >> 32) & 0xff;
655 tf->hob_lbal = (block >> 24) & 0xff;
657 /* request too large even for LBA48 */
660 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
663 tf->nsect = n_block & 0xff;
665 tf->lbah = (block >> 16) & 0xff;
666 tf->lbam = (block >> 8) & 0xff;
667 tf->lbal = block & 0xff;
669 tf->device |= ATA_LBA;
672 u32 sect, head, cyl, track;
674 /* The request -may- be too large for CHS addressing. */
675 if (!lba_28_ok(block, n_block))
678 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
681 /* Convert LBA to CHS */
682 track = (u32)block / dev->sectors;
683 cyl = track / dev->heads;
684 head = track % dev->heads;
685 sect = (u32)block % dev->sectors + 1;
687 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
688 (u32)block, track, cyl, head, sect);
690 /* Check whether the converted CHS can fit.
694 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
697 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
708 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
709 * @pio_mask: pio_mask
710 * @mwdma_mask: mwdma_mask
711 * @udma_mask: udma_mask
713 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
714 * unsigned int xfer_mask.
722 unsigned long ata_pack_xfermask(unsigned long pio_mask,
723 unsigned long mwdma_mask,
724 unsigned long udma_mask)
726 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
727 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
728 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
732 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
733 * @xfer_mask: xfer_mask to unpack
734 * @pio_mask: resulting pio_mask
735 * @mwdma_mask: resulting mwdma_mask
736 * @udma_mask: resulting udma_mask
738 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
739 * Any NULL distination masks will be ignored.
741 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
742 unsigned long *mwdma_mask, unsigned long *udma_mask)
745 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
747 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
749 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
752 static const struct ata_xfer_ent {
756 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
757 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
758 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
763 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
764 * @xfer_mask: xfer_mask of interest
766 * Return matching XFER_* value for @xfer_mask. Only the highest
767 * bit of @xfer_mask is considered.
773 * Matching XFER_* value, 0xff if no match found.
775 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
777 int highbit = fls(xfer_mask) - 1;
778 const struct ata_xfer_ent *ent;
780 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
781 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
782 return ent->base + highbit - ent->shift;
787 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
788 * @xfer_mode: XFER_* of interest
790 * Return matching xfer_mask for @xfer_mode.
796 * Matching xfer_mask, 0 if no match found.
798 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
800 const struct ata_xfer_ent *ent;
802 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
803 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
804 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
805 & ~((1 << ent->shift) - 1);
810 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
811 * @xfer_mode: XFER_* of interest
813 * Return matching xfer_shift for @xfer_mode.
819 * Matching xfer_shift, -1 if no match found.
821 int ata_xfer_mode2shift(unsigned long xfer_mode)
823 const struct ata_xfer_ent *ent;
825 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
826 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
832 * ata_mode_string - convert xfer_mask to string
833 * @xfer_mask: mask of bits supported; only highest bit counts.
835 * Determine string which represents the highest speed
836 * (highest bit in @modemask).
842 * Constant C string representing highest speed listed in
843 * @mode_mask, or the constant C string "<n/a>".
845 const char *ata_mode_string(unsigned long xfer_mask)
847 static const char * const xfer_mode_str[] = {
871 highbit = fls(xfer_mask) - 1;
872 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
873 return xfer_mode_str[highbit];
877 static const char *sata_spd_string(unsigned int spd)
879 static const char * const spd_str[] = {
884 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
886 return spd_str[spd - 1];
889 void ata_dev_disable(struct ata_device *dev)
891 if (ata_dev_enabled(dev)) {
892 if (ata_msg_drv(dev->link->ap))
893 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
894 ata_acpi_on_disable(dev);
895 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
901 static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
903 struct ata_link *link = dev->link;
904 struct ata_port *ap = link->ap;
906 unsigned int err_mask;
910 * disallow DIPM for drivers which haven't set
911 * ATA_FLAG_IPM. This is because when DIPM is enabled,
912 * phy ready will be set in the interrupt status on
913 * state changes, which will cause some drivers to
914 * think there are errors - additionally drivers will
915 * need to disable hot plug.
917 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
918 ap->pm_policy = NOT_AVAILABLE;
923 * For DIPM, we will only enable it for the
926 * Why? Because Disks are too stupid to know that
927 * If the host rejects a request to go to SLUMBER
928 * they should retry at PARTIAL, and instead it
929 * just would give up. So, for medium_power to
930 * work at all, we need to only allow HIPM.
932 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
938 /* no restrictions on IPM transitions */
939 scontrol &= ~(0x3 << 8);
940 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
945 if (dev->flags & ATA_DFLAG_DIPM)
946 err_mask = ata_dev_set_feature(dev,
947 SETFEATURES_SATA_ENABLE, SATA_DIPM);
950 /* allow IPM to PARTIAL */
951 scontrol &= ~(0x1 << 8);
952 scontrol |= (0x2 << 8);
953 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
958 * we don't have to disable DIPM since IPM flags
959 * disallow transitions to SLUMBER, which effectively
960 * disable DIPM if it does not support PARTIAL
964 case MAX_PERFORMANCE:
965 /* disable all IPM transitions */
966 scontrol |= (0x3 << 8);
967 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
972 * we don't have to disable DIPM since IPM flags
973 * disallow all transitions which effectively
974 * disable DIPM anyway.
979 /* FIXME: handle SET FEATURES failure */
986 * ata_dev_enable_pm - enable SATA interface power management
987 * @dev: device to enable power management
988 * @policy: the link power management policy
990 * Enable SATA Interface power management. This will enable
991 * Device Interface Power Management (DIPM) for min_power
992 * policy, and then call driver specific callbacks for
993 * enabling Host Initiated Power management.
996 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
998 void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1001 struct ata_port *ap = dev->link->ap;
1003 /* set HIPM first, then DIPM */
1004 if (ap->ops->enable_pm)
1005 rc = ap->ops->enable_pm(ap, policy);
1008 rc = ata_dev_set_dipm(dev, policy);
1012 ap->pm_policy = MAX_PERFORMANCE;
1014 ap->pm_policy = policy;
1015 return /* rc */; /* hopefully we can use 'rc' eventually */
1020 * ata_dev_disable_pm - disable SATA interface power management
1021 * @dev: device to disable power management
1023 * Disable SATA Interface power management. This will disable
1024 * Device Interface Power Management (DIPM) without changing
1025 * policy, call driver specific callbacks for disabling Host
1026 * Initiated Power management.
1031 static void ata_dev_disable_pm(struct ata_device *dev)
1033 struct ata_port *ap = dev->link->ap;
1035 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1036 if (ap->ops->disable_pm)
1037 ap->ops->disable_pm(ap);
1039 #endif /* CONFIG_PM */
1041 void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1043 ap->pm_policy = policy;
1044 ap->link.eh_info.action |= ATA_EH_LPM;
1045 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1046 ata_port_schedule_eh(ap);
1050 static void ata_lpm_enable(struct ata_host *host)
1052 struct ata_link *link;
1053 struct ata_port *ap;
1054 struct ata_device *dev;
1057 for (i = 0; i < host->n_ports; i++) {
1058 ap = host->ports[i];
1059 ata_port_for_each_link(link, ap) {
1060 ata_link_for_each_dev(dev, link)
1061 ata_dev_disable_pm(dev);
1066 static void ata_lpm_disable(struct ata_host *host)
1070 for (i = 0; i < host->n_ports; i++) {
1071 struct ata_port *ap = host->ports[i];
1072 ata_lpm_schedule(ap, ap->pm_policy);
1075 #endif /* CONFIG_PM */
1078 * ata_dev_classify - determine device type based on ATA-spec signature
1079 * @tf: ATA taskfile register set for device to be identified
1081 * Determine from taskfile register contents whether a device is
1082 * ATA or ATAPI, as per "Signature and persistence" section
1083 * of ATA/PI spec (volume 1, sect 5.14).
1089 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1090 * %ATA_DEV_UNKNOWN the event of failure.
1092 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1094 /* Apple's open source Darwin code hints that some devices only
1095 * put a proper signature into the LBA mid/high registers,
1096 * So, we only check those. It's sufficient for uniqueness.
1098 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1099 * signatures for ATA and ATAPI devices attached on SerialATA,
1100 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1101 * spec has never mentioned about using different signatures
1102 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1103 * Multiplier specification began to use 0x69/0x96 to identify
1104 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1105 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1106 * 0x69/0x96 shortly and described them as reserved for
1109 * We follow the current spec and consider that 0x69/0x96
1110 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1112 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1113 DPRINTK("found ATA device by sig\n");
1117 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1118 DPRINTK("found ATAPI device by sig\n");
1119 return ATA_DEV_ATAPI;
1122 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1123 DPRINTK("found PMP device by sig\n");
1127 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1128 printk(KERN_INFO "ata: SEMB device ignored\n");
1129 return ATA_DEV_SEMB_UNSUP; /* not yet */
1132 DPRINTK("unknown device\n");
1133 return ATA_DEV_UNKNOWN;
1137 * ata_id_string - Convert IDENTIFY DEVICE page into string
1138 * @id: IDENTIFY DEVICE results we will examine
1139 * @s: string into which data is output
1140 * @ofs: offset into identify device page
1141 * @len: length of string to return. must be an even number.
1143 * The strings in the IDENTIFY DEVICE page are broken up into
1144 * 16-bit chunks. Run through the string, and output each
1145 * 8-bit chunk linearly, regardless of platform.
1151 void ata_id_string(const u16 *id, unsigned char *s,
1152 unsigned int ofs, unsigned int len)
1171 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1172 * @id: IDENTIFY DEVICE results we will examine
1173 * @s: string into which data is output
1174 * @ofs: offset into identify device page
1175 * @len: length of string to return. must be an odd number.
1177 * This function is identical to ata_id_string except that it
1178 * trims trailing spaces and terminates the resulting string with
1179 * null. @len must be actual maximum length (even number) + 1.
1184 void ata_id_c_string(const u16 *id, unsigned char *s,
1185 unsigned int ofs, unsigned int len)
1189 WARN_ON(!(len & 1));
1191 ata_id_string(id, s, ofs, len - 1);
1193 p = s + strnlen(s, len - 1);
1194 while (p > s && p[-1] == ' ')
1199 static u64 ata_id_n_sectors(const u16 *id)
1201 if (ata_id_has_lba(id)) {
1202 if (ata_id_has_lba48(id))
1203 return ata_id_u64(id, 100);
1205 return ata_id_u32(id, 60);
1207 if (ata_id_current_chs_valid(id))
1208 return ata_id_u32(id, 57);
1210 return id[1] * id[3] * id[6];
1214 static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1218 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1219 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1220 sectors |= (tf->hob_lbal & 0xff) << 24;
1221 sectors |= (tf->lbah & 0xff) << 16;
1222 sectors |= (tf->lbam & 0xff) << 8;
1223 sectors |= (tf->lbal & 0xff);
1228 static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1232 sectors |= (tf->device & 0x0f) << 24;
1233 sectors |= (tf->lbah & 0xff) << 16;
1234 sectors |= (tf->lbam & 0xff) << 8;
1235 sectors |= (tf->lbal & 0xff);
1241 * ata_read_native_max_address - Read native max address
1242 * @dev: target device
1243 * @max_sectors: out parameter for the result native max address
1245 * Perform an LBA48 or LBA28 native size query upon the device in
1249 * 0 on success, -EACCES if command is aborted by the drive.
1250 * -EIO on other errors.
1252 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1254 unsigned int err_mask;
1255 struct ata_taskfile tf;
1256 int lba48 = ata_id_has_lba48(dev->id);
1258 ata_tf_init(dev, &tf);
1260 /* always clear all address registers */
1261 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1264 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1265 tf.flags |= ATA_TFLAG_LBA48;
1267 tf.command = ATA_CMD_READ_NATIVE_MAX;
1269 tf.protocol |= ATA_PROT_NODATA;
1270 tf.device |= ATA_LBA;
1272 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1274 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1275 "max address (err_mask=0x%x)\n", err_mask);
1276 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1282 *max_sectors = ata_tf_to_lba48(&tf);
1284 *max_sectors = ata_tf_to_lba(&tf);
1285 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1291 * ata_set_max_sectors - Set max sectors
1292 * @dev: target device
1293 * @new_sectors: new max sectors value to set for the device
1295 * Set max sectors of @dev to @new_sectors.
1298 * 0 on success, -EACCES if command is aborted or denied (due to
1299 * previous non-volatile SET_MAX) by the drive. -EIO on other
1302 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1304 unsigned int err_mask;
1305 struct ata_taskfile tf;
1306 int lba48 = ata_id_has_lba48(dev->id);
1310 ata_tf_init(dev, &tf);
1312 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1315 tf.command = ATA_CMD_SET_MAX_EXT;
1316 tf.flags |= ATA_TFLAG_LBA48;
1318 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1319 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1320 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1322 tf.command = ATA_CMD_SET_MAX;
1324 tf.device |= (new_sectors >> 24) & 0xf;
1327 tf.protocol |= ATA_PROT_NODATA;
1328 tf.device |= ATA_LBA;
1330 tf.lbal = (new_sectors >> 0) & 0xff;
1331 tf.lbam = (new_sectors >> 8) & 0xff;
1332 tf.lbah = (new_sectors >> 16) & 0xff;
1334 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1336 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1337 "max address (err_mask=0x%x)\n", err_mask);
1338 if (err_mask == AC_ERR_DEV &&
1339 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1348 * ata_hpa_resize - Resize a device with an HPA set
1349 * @dev: Device to resize
1351 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1352 * it if required to the full size of the media. The caller must check
1353 * the drive has the HPA feature set enabled.
1356 * 0 on success, -errno on failure.
1358 static int ata_hpa_resize(struct ata_device *dev)
1360 struct ata_eh_context *ehc = &dev->link->eh_context;
1361 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1362 u64 sectors = ata_id_n_sectors(dev->id);
1366 /* do we need to do it? */
1367 if (dev->class != ATA_DEV_ATA ||
1368 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1369 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1372 /* read native max address */
1373 rc = ata_read_native_max_address(dev, &native_sectors);
1375 /* If device aborted the command or HPA isn't going to
1376 * be unlocked, skip HPA resizing.
1378 if (rc == -EACCES || !ata_ignore_hpa) {
1379 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1380 "broken, skipping HPA handling\n");
1381 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1383 /* we can continue if device aborted the command */
1391 /* nothing to do? */
1392 if (native_sectors <= sectors || !ata_ignore_hpa) {
1393 if (!print_info || native_sectors == sectors)
1396 if (native_sectors > sectors)
1397 ata_dev_printk(dev, KERN_INFO,
1398 "HPA detected: current %llu, native %llu\n",
1399 (unsigned long long)sectors,
1400 (unsigned long long)native_sectors);
1401 else if (native_sectors < sectors)
1402 ata_dev_printk(dev, KERN_WARNING,
1403 "native sectors (%llu) is smaller than "
1405 (unsigned long long)native_sectors,
1406 (unsigned long long)sectors);
1410 /* let's unlock HPA */
1411 rc = ata_set_max_sectors(dev, native_sectors);
1412 if (rc == -EACCES) {
1413 /* if device aborted the command, skip HPA resizing */
1414 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1415 "(%llu -> %llu), skipping HPA handling\n",
1416 (unsigned long long)sectors,
1417 (unsigned long long)native_sectors);
1418 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1423 /* re-read IDENTIFY data */
1424 rc = ata_dev_reread_id(dev, 0);
1426 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1427 "data after HPA resizing\n");
1432 u64 new_sectors = ata_id_n_sectors(dev->id);
1433 ata_dev_printk(dev, KERN_INFO,
1434 "HPA unlocked: %llu -> %llu, native %llu\n",
1435 (unsigned long long)sectors,
1436 (unsigned long long)new_sectors,
1437 (unsigned long long)native_sectors);
1444 * ata_noop_dev_select - Select device 0/1 on ATA bus
1445 * @ap: ATA channel to manipulate
1446 * @device: ATA device (numbered from zero) to select
1448 * This function performs no actual function.
1450 * May be used as the dev_select() entry in ata_port_operations.
1455 void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
1460 * ata_dump_id - IDENTIFY DEVICE info debugging output
1461 * @id: IDENTIFY DEVICE page to dump
1463 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1470 static inline void ata_dump_id(const u16 *id)
1472 DPRINTK("49==0x%04x "
1482 DPRINTK("80==0x%04x "
1492 DPRINTK("88==0x%04x "
1499 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1500 * @id: IDENTIFY data to compute xfer mask from
1502 * Compute the xfermask for this device. This is not as trivial
1503 * as it seems if we must consider early devices correctly.
1505 * FIXME: pre IDE drive timing (do we care ?).
1513 unsigned long ata_id_xfermask(const u16 *id)
1515 unsigned long pio_mask, mwdma_mask, udma_mask;
1517 /* Usual case. Word 53 indicates word 64 is valid */
1518 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1519 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1523 /* If word 64 isn't valid then Word 51 high byte holds
1524 * the PIO timing number for the maximum. Turn it into
1527 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1528 if (mode < 5) /* Valid PIO range */
1529 pio_mask = (2 << mode) - 1;
1533 /* But wait.. there's more. Design your standards by
1534 * committee and you too can get a free iordy field to
1535 * process. However its the speeds not the modes that
1536 * are supported... Note drivers using the timing API
1537 * will get this right anyway
1541 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1543 if (ata_id_is_cfa(id)) {
1545 * Process compact flash extended modes
1547 int pio = id[163] & 0x7;
1548 int dma = (id[163] >> 3) & 7;
1551 pio_mask |= (1 << 5);
1553 pio_mask |= (1 << 6);
1555 mwdma_mask |= (1 << 3);
1557 mwdma_mask |= (1 << 4);
1561 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1562 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1564 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1568 * ata_pio_queue_task - Queue port_task
1569 * @ap: The ata_port to queue port_task for
1570 * @fn: workqueue function to be scheduled
1571 * @data: data for @fn to use
1572 * @delay: delay time for workqueue function
1574 * Schedule @fn(@data) for execution after @delay jiffies using
1575 * port_task. There is one port_task per port and it's the
1576 * user(low level driver)'s responsibility to make sure that only
1577 * one task is active at any given time.
1579 * libata core layer takes care of synchronization between
1580 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1584 * Inherited from caller.
1586 void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1588 ap->port_task_data = data;
1590 /* may fail if ata_port_flush_task() in progress */
1591 queue_delayed_work(ata_wq, &ap->port_task, delay);
1595 * ata_port_flush_task - Flush port_task
1596 * @ap: The ata_port to flush port_task for
1598 * After this function completes, port_task is guranteed not to
1599 * be running or scheduled.
1602 * Kernel thread context (may sleep)
1604 void ata_port_flush_task(struct ata_port *ap)
1608 cancel_rearming_delayed_work(&ap->port_task);
1610 if (ata_msg_ctl(ap))
1611 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1614 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1616 struct completion *waiting = qc->private_data;
1622 * ata_exec_internal_sg - execute libata internal command
1623 * @dev: Device to which the command is sent
1624 * @tf: Taskfile registers for the command and the result
1625 * @cdb: CDB for packet command
1626 * @dma_dir: Data tranfer direction of the command
1627 * @sgl: sg list for the data buffer of the command
1628 * @n_elem: Number of sg entries
1629 * @timeout: Timeout in msecs (0 for default)
1631 * Executes libata internal command with timeout. @tf contains
1632 * command on entry and result on return. Timeout and error
1633 * conditions are reported via return value. No recovery action
1634 * is taken after a command times out. It's caller's duty to
1635 * clean up after timeout.
1638 * None. Should be called with kernel context, might sleep.
1641 * Zero on success, AC_ERR_* mask on failure
1643 unsigned ata_exec_internal_sg(struct ata_device *dev,
1644 struct ata_taskfile *tf, const u8 *cdb,
1645 int dma_dir, struct scatterlist *sgl,
1646 unsigned int n_elem, unsigned long timeout)
1648 struct ata_link *link = dev->link;
1649 struct ata_port *ap = link->ap;
1650 u8 command = tf->command;
1651 struct ata_queued_cmd *qc;
1652 unsigned int tag, preempted_tag;
1653 u32 preempted_sactive, preempted_qc_active;
1654 int preempted_nr_active_links;
1655 DECLARE_COMPLETION_ONSTACK(wait);
1656 unsigned long flags;
1657 unsigned int err_mask;
1660 spin_lock_irqsave(ap->lock, flags);
1662 /* no internal command while frozen */
1663 if (ap->pflags & ATA_PFLAG_FROZEN) {
1664 spin_unlock_irqrestore(ap->lock, flags);
1665 return AC_ERR_SYSTEM;
1668 /* initialize internal qc */
1670 /* XXX: Tag 0 is used for drivers with legacy EH as some
1671 * drivers choke if any other tag is given. This breaks
1672 * ata_tag_internal() test for those drivers. Don't use new
1673 * EH stuff without converting to it.
1675 if (ap->ops->error_handler)
1676 tag = ATA_TAG_INTERNAL;
1680 if (test_and_set_bit(tag, &ap->qc_allocated))
1682 qc = __ata_qc_from_tag(ap, tag);
1690 preempted_tag = link->active_tag;
1691 preempted_sactive = link->sactive;
1692 preempted_qc_active = ap->qc_active;
1693 preempted_nr_active_links = ap->nr_active_links;
1694 link->active_tag = ATA_TAG_POISON;
1697 ap->nr_active_links = 0;
1699 /* prepare & issue qc */
1702 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1703 qc->flags |= ATA_QCFLAG_RESULT_TF;
1704 qc->dma_dir = dma_dir;
1705 if (dma_dir != DMA_NONE) {
1706 unsigned int i, buflen = 0;
1707 struct scatterlist *sg;
1709 for_each_sg(sgl, sg, n_elem, i)
1710 buflen += sg->length;
1712 ata_sg_init(qc, sgl, n_elem);
1713 qc->nbytes = buflen;
1716 qc->private_data = &wait;
1717 qc->complete_fn = ata_qc_complete_internal;
1721 spin_unlock_irqrestore(ap->lock, flags);
1724 timeout = ata_probe_timeout * 1000 / HZ;
1726 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1728 ata_port_flush_task(ap);
1731 spin_lock_irqsave(ap->lock, flags);
1733 /* We're racing with irq here. If we lose, the
1734 * following test prevents us from completing the qc
1735 * twice. If we win, the port is frozen and will be
1736 * cleaned up by ->post_internal_cmd().
1738 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1739 qc->err_mask |= AC_ERR_TIMEOUT;
1741 if (ap->ops->error_handler)
1742 ata_port_freeze(ap);
1744 ata_qc_complete(qc);
1746 if (ata_msg_warn(ap))
1747 ata_dev_printk(dev, KERN_WARNING,
1748 "qc timeout (cmd 0x%x)\n", command);
1751 spin_unlock_irqrestore(ap->lock, flags);
1754 /* do post_internal_cmd */
1755 if (ap->ops->post_internal_cmd)
1756 ap->ops->post_internal_cmd(qc);
1758 /* perform minimal error analysis */
1759 if (qc->flags & ATA_QCFLAG_FAILED) {
1760 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1761 qc->err_mask |= AC_ERR_DEV;
1764 qc->err_mask |= AC_ERR_OTHER;
1766 if (qc->err_mask & ~AC_ERR_OTHER)
1767 qc->err_mask &= ~AC_ERR_OTHER;
1771 spin_lock_irqsave(ap->lock, flags);
1773 *tf = qc->result_tf;
1774 err_mask = qc->err_mask;
1777 link->active_tag = preempted_tag;
1778 link->sactive = preempted_sactive;
1779 ap->qc_active = preempted_qc_active;
1780 ap->nr_active_links = preempted_nr_active_links;
1782 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1783 * Until those drivers are fixed, we detect the condition
1784 * here, fail the command with AC_ERR_SYSTEM and reenable the
1787 * Note that this doesn't change any behavior as internal
1788 * command failure results in disabling the device in the
1789 * higher layer for LLDDs without new reset/EH callbacks.
1791 * Kill the following code as soon as those drivers are fixed.
1793 if (ap->flags & ATA_FLAG_DISABLED) {
1794 err_mask |= AC_ERR_SYSTEM;
1798 spin_unlock_irqrestore(ap->lock, flags);
1804 * ata_exec_internal - execute libata internal command
1805 * @dev: Device to which the command is sent
1806 * @tf: Taskfile registers for the command and the result
1807 * @cdb: CDB for packet command
1808 * @dma_dir: Data tranfer direction of the command
1809 * @buf: Data buffer of the command
1810 * @buflen: Length of data buffer
1811 * @timeout: Timeout in msecs (0 for default)
1813 * Wrapper around ata_exec_internal_sg() which takes simple
1814 * buffer instead of sg list.
1817 * None. Should be called with kernel context, might sleep.
1820 * Zero on success, AC_ERR_* mask on failure
1822 unsigned ata_exec_internal(struct ata_device *dev,
1823 struct ata_taskfile *tf, const u8 *cdb,
1824 int dma_dir, void *buf, unsigned int buflen,
1825 unsigned long timeout)
1827 struct scatterlist *psg = NULL, sg;
1828 unsigned int n_elem = 0;
1830 if (dma_dir != DMA_NONE) {
1832 sg_init_one(&sg, buf, buflen);
1837 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1842 * ata_do_simple_cmd - execute simple internal command
1843 * @dev: Device to which the command is sent
1844 * @cmd: Opcode to execute
1846 * Execute a 'simple' command, that only consists of the opcode
1847 * 'cmd' itself, without filling any other registers
1850 * Kernel thread context (may sleep).
1853 * Zero on success, AC_ERR_* mask on failure
1855 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1857 struct ata_taskfile tf;
1859 ata_tf_init(dev, &tf);
1862 tf.flags |= ATA_TFLAG_DEVICE;
1863 tf.protocol = ATA_PROT_NODATA;
1865 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1869 * ata_pio_need_iordy - check if iordy needed
1872 * Check if the current speed of the device requires IORDY. Used
1873 * by various controllers for chip configuration.
1876 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1878 /* Controller doesn't support IORDY. Probably a pointless check
1879 as the caller should know this */
1880 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1882 /* PIO3 and higher it is mandatory */
1883 if (adev->pio_mode > XFER_PIO_2)
1885 /* We turn it on when possible */
1886 if (ata_id_has_iordy(adev->id))
1892 * ata_pio_mask_no_iordy - Return the non IORDY mask
1895 * Compute the highest mode possible if we are not using iordy. Return
1896 * -1 if no iordy mode is available.
1899 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1901 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1902 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1903 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1904 /* Is the speed faster than the drive allows non IORDY ? */
1906 /* This is cycle times not frequency - watch the logic! */
1907 if (pio > 240) /* PIO2 is 240nS per cycle */
1908 return 3 << ATA_SHIFT_PIO;
1909 return 7 << ATA_SHIFT_PIO;
1912 return 3 << ATA_SHIFT_PIO;
1916 * ata_dev_read_id - Read ID data from the specified device
1917 * @dev: target device
1918 * @p_class: pointer to class of the target device (may be changed)
1919 * @flags: ATA_READID_* flags
1920 * @id: buffer to read IDENTIFY data into
1922 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1923 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1924 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1925 * for pre-ATA4 drives.
1927 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1928 * now we abort if we hit that case.
1931 * Kernel thread context (may sleep)
1934 * 0 on success, -errno otherwise.
1936 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1937 unsigned int flags, u16 *id)
1939 struct ata_port *ap = dev->link->ap;
1940 unsigned int class = *p_class;
1941 struct ata_taskfile tf;
1942 unsigned int err_mask = 0;
1944 int may_fallback = 1, tried_spinup = 0;
1947 if (ata_msg_ctl(ap))
1948 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
1951 ata_tf_init(dev, &tf);
1955 tf.command = ATA_CMD_ID_ATA;
1958 tf.command = ATA_CMD_ID_ATAPI;
1962 reason = "unsupported class";
1966 tf.protocol = ATA_PROT_PIO;
1968 /* Some devices choke if TF registers contain garbage. Make
1969 * sure those are properly initialized.
1971 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1973 /* Device presence detection is unreliable on some
1974 * controllers. Always poll IDENTIFY if available.
1976 tf.flags |= ATA_TFLAG_POLLING;
1978 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1979 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1981 if (err_mask & AC_ERR_NODEV_HINT) {
1982 ata_dev_printk(dev, KERN_DEBUG,
1983 "NODEV after polling detection\n");
1987 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1988 /* Device or controller might have reported
1989 * the wrong device class. Give a shot at the
1990 * other IDENTIFY if the current one is
1991 * aborted by the device.
1996 if (class == ATA_DEV_ATA)
1997 class = ATA_DEV_ATAPI;
1999 class = ATA_DEV_ATA;
2003 /* Control reaches here iff the device aborted
2004 * both flavors of IDENTIFYs which happens
2005 * sometimes with phantom devices.
2007 ata_dev_printk(dev, KERN_DEBUG,
2008 "both IDENTIFYs aborted, assuming NODEV\n");
2013 reason = "I/O error";
2017 /* Falling back doesn't make sense if ID data was read
2018 * successfully at least once.
2022 swap_buf_le16(id, ATA_ID_WORDS);
2026 reason = "device reports invalid type";
2028 if (class == ATA_DEV_ATA) {
2029 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2032 if (ata_id_is_ata(id))
2036 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2039 * Drive powered-up in standby mode, and requires a specific
2040 * SET_FEATURES spin-up subcommand before it will accept
2041 * anything other than the original IDENTIFY command.
2043 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2044 if (err_mask && id[2] != 0x738c) {
2046 reason = "SPINUP failed";
2050 * If the drive initially returned incomplete IDENTIFY info,
2051 * we now must reissue the IDENTIFY command.
2053 if (id[2] == 0x37c8)
2057 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2059 * The exact sequence expected by certain pre-ATA4 drives is:
2061 * IDENTIFY (optional in early ATA)
2062 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2064 * Some drives were very specific about that exact sequence.
2066 * Note that ATA4 says lba is mandatory so the second check
2067 * shoud never trigger.
2069 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2070 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2073 reason = "INIT_DEV_PARAMS failed";
2077 /* current CHS translation info (id[53-58]) might be
2078 * changed. reread the identify device info.
2080 flags &= ~ATA_READID_POSTRESET;
2090 if (ata_msg_warn(ap))
2091 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2092 "(%s, err_mask=0x%x)\n", reason, err_mask);
2096 static inline u8 ata_dev_knobble(struct ata_device *dev)
2098 struct ata_port *ap = dev->link->ap;
2099 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2102 static void ata_dev_config_ncq(struct ata_device *dev,
2103 char *desc, size_t desc_sz)
2105 struct ata_port *ap = dev->link->ap;
2106 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2108 if (!ata_id_has_ncq(dev->id)) {
2112 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2113 snprintf(desc, desc_sz, "NCQ (not used)");
2116 if (ap->flags & ATA_FLAG_NCQ) {
2117 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2118 dev->flags |= ATA_DFLAG_NCQ;
2121 if (hdepth >= ddepth)
2122 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2124 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2128 * ata_dev_configure - Configure the specified ATA/ATAPI device
2129 * @dev: Target device to configure
2131 * Configure @dev according to @dev->id. Generic and low-level
2132 * driver specific fixups are also applied.
2135 * Kernel thread context (may sleep)
2138 * 0 on success, -errno otherwise
2140 int ata_dev_configure(struct ata_device *dev)
2142 struct ata_port *ap = dev->link->ap;
2143 struct ata_eh_context *ehc = &dev->link->eh_context;
2144 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2145 const u16 *id = dev->id;
2146 unsigned long xfer_mask;
2147 char revbuf[7]; /* XYZ-99\0 */
2148 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2149 char modelbuf[ATA_ID_PROD_LEN+1];
2152 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2153 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
2158 if (ata_msg_probe(ap))
2159 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2162 dev->horkage |= ata_dev_blacklisted(dev);
2163 ata_force_horkage(dev);
2165 /* let ACPI work its magic */
2166 rc = ata_acpi_on_devcfg(dev);
2170 /* massage HPA, do it early as it might change IDENTIFY data */
2171 rc = ata_hpa_resize(dev);
2175 /* print device capabilities */
2176 if (ata_msg_probe(ap))
2177 ata_dev_printk(dev, KERN_DEBUG,
2178 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2179 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2181 id[49], id[82], id[83], id[84],
2182 id[85], id[86], id[87], id[88]);
2184 /* initialize to-be-configured parameters */
2185 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2186 dev->max_sectors = 0;
2194 * common ATA, ATAPI feature tests
2197 /* find max transfer mode; for printk only */
2198 xfer_mask = ata_id_xfermask(id);
2200 if (ata_msg_probe(ap))
2203 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2204 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2207 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2210 /* ATA-specific feature tests */
2211 if (dev->class == ATA_DEV_ATA) {
2212 if (ata_id_is_cfa(id)) {
2213 if (id[162] & 1) /* CPRM may make this media unusable */
2214 ata_dev_printk(dev, KERN_WARNING,
2215 "supports DRM functions and may "
2216 "not be fully accessable.\n");
2217 snprintf(revbuf, 7, "CFA");
2219 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2220 /* Warn the user if the device has TPM extensions */
2221 if (ata_id_has_tpm(id))
2222 ata_dev_printk(dev, KERN_WARNING,
2223 "supports DRM functions and may "
2224 "not be fully accessable.\n");
2227 dev->n_sectors = ata_id_n_sectors(id);
2229 if (dev->id[59] & 0x100)
2230 dev->multi_count = dev->id[59] & 0xff;
2232 if (ata_id_has_lba(id)) {
2233 const char *lba_desc;
2237 dev->flags |= ATA_DFLAG_LBA;
2238 if (ata_id_has_lba48(id)) {
2239 dev->flags |= ATA_DFLAG_LBA48;
2242 if (dev->n_sectors >= (1UL << 28) &&
2243 ata_id_has_flush_ext(id))
2244 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2248 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2250 /* print device info to dmesg */
2251 if (ata_msg_drv(ap) && print_info) {
2252 ata_dev_printk(dev, KERN_INFO,
2253 "%s: %s, %s, max %s\n",
2254 revbuf, modelbuf, fwrevbuf,
2255 ata_mode_string(xfer_mask));
2256 ata_dev_printk(dev, KERN_INFO,
2257 "%Lu sectors, multi %u: %s %s\n",
2258 (unsigned long long)dev->n_sectors,
2259 dev->multi_count, lba_desc, ncq_desc);
2264 /* Default translation */
2265 dev->cylinders = id[1];
2267 dev->sectors = id[6];
2269 if (ata_id_current_chs_valid(id)) {
2270 /* Current CHS translation is valid. */
2271 dev->cylinders = id[54];
2272 dev->heads = id[55];
2273 dev->sectors = id[56];
2276 /* print device info to dmesg */
2277 if (ata_msg_drv(ap) && print_info) {
2278 ata_dev_printk(dev, KERN_INFO,
2279 "%s: %s, %s, max %s\n",
2280 revbuf, modelbuf, fwrevbuf,
2281 ata_mode_string(xfer_mask));
2282 ata_dev_printk(dev, KERN_INFO,
2283 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2284 (unsigned long long)dev->n_sectors,
2285 dev->multi_count, dev->cylinders,
2286 dev->heads, dev->sectors);
2293 /* ATAPI-specific feature tests */
2294 else if (dev->class == ATA_DEV_ATAPI) {
2295 const char *cdb_intr_string = "";
2296 const char *atapi_an_string = "";
2297 const char *dma_dir_string = "";
2300 rc = atapi_cdb_len(id);
2301 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2302 if (ata_msg_warn(ap))
2303 ata_dev_printk(dev, KERN_WARNING,
2304 "unsupported CDB len\n");
2308 dev->cdb_len = (unsigned int) rc;
2310 /* Enable ATAPI AN if both the host and device have
2311 * the support. If PMP is attached, SNTF is required
2312 * to enable ATAPI AN to discern between PHY status
2313 * changed notifications and ATAPI ANs.
2315 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2316 (!ap->nr_pmp_links ||
2317 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2318 unsigned int err_mask;
2320 /* issue SET feature command to turn this on */
2321 err_mask = ata_dev_set_feature(dev,
2322 SETFEATURES_SATA_ENABLE, SATA_AN);
2324 ata_dev_printk(dev, KERN_ERR,
2325 "failed to enable ATAPI AN "
2326 "(err_mask=0x%x)\n", err_mask);
2328 dev->flags |= ATA_DFLAG_AN;
2329 atapi_an_string = ", ATAPI AN";
2333 if (ata_id_cdb_intr(dev->id)) {
2334 dev->flags |= ATA_DFLAG_CDB_INTR;
2335 cdb_intr_string = ", CDB intr";
2338 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2339 dev->flags |= ATA_DFLAG_DMADIR;
2340 dma_dir_string = ", DMADIR";
2343 /* print device info to dmesg */
2344 if (ata_msg_drv(ap) && print_info)
2345 ata_dev_printk(dev, KERN_INFO,
2346 "ATAPI: %s, %s, max %s%s%s%s\n",
2348 ata_mode_string(xfer_mask),
2349 cdb_intr_string, atapi_an_string,
2353 /* determine max_sectors */
2354 dev->max_sectors = ATA_MAX_SECTORS;
2355 if (dev->flags & ATA_DFLAG_LBA48)
2356 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2358 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2359 if (ata_id_has_hipm(dev->id))
2360 dev->flags |= ATA_DFLAG_HIPM;
2361 if (ata_id_has_dipm(dev->id))
2362 dev->flags |= ATA_DFLAG_DIPM;
2365 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2367 if (ata_dev_knobble(dev)) {
2368 if (ata_msg_drv(ap) && print_info)
2369 ata_dev_printk(dev, KERN_INFO,
2370 "applying bridge limits\n");
2371 dev->udma_mask &= ATA_UDMA5;
2372 dev->max_sectors = ATA_MAX_SECTORS;
2375 if ((dev->class == ATA_DEV_ATAPI) &&
2376 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2377 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2378 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2381 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2382 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2385 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2386 dev->horkage |= ATA_HORKAGE_IPM;
2388 /* reset link pm_policy for this port to no pm */
2389 ap->pm_policy = MAX_PERFORMANCE;
2392 if (ap->ops->dev_config)
2393 ap->ops->dev_config(dev);
2395 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2396 /* Let the user know. We don't want to disallow opens for
2397 rescue purposes, or in case the vendor is just a blithering
2398 idiot. Do this after the dev_config call as some controllers
2399 with buggy firmware may want to avoid reporting false device
2403 ata_dev_printk(dev, KERN_WARNING,
2404 "Drive reports diagnostics failure. This may indicate a drive\n");
2405 ata_dev_printk(dev, KERN_WARNING,
2406 "fault or invalid emulation. Contact drive vendor for information.\n");
2413 if (ata_msg_probe(ap))
2414 ata_dev_printk(dev, KERN_DEBUG,
2415 "%s: EXIT, err\n", __func__);
2420 * ata_cable_40wire - return 40 wire cable type
2423 * Helper method for drivers which want to hardwire 40 wire cable
2427 int ata_cable_40wire(struct ata_port *ap)
2429 return ATA_CBL_PATA40;
2433 * ata_cable_80wire - return 80 wire cable type
2436 * Helper method for drivers which want to hardwire 80 wire cable
2440 int ata_cable_80wire(struct ata_port *ap)
2442 return ATA_CBL_PATA80;
2446 * ata_cable_unknown - return unknown PATA cable.
2449 * Helper method for drivers which have no PATA cable detection.
2452 int ata_cable_unknown(struct ata_port *ap)
2454 return ATA_CBL_PATA_UNK;
2458 * ata_cable_ignore - return ignored PATA cable.
2461 * Helper method for drivers which don't use cable type to limit
2464 int ata_cable_ignore(struct ata_port *ap)
2466 return ATA_CBL_PATA_IGN;
2470 * ata_cable_sata - return SATA cable type
2473 * Helper method for drivers which have SATA cables
2476 int ata_cable_sata(struct ata_port *ap)
2478 return ATA_CBL_SATA;
2482 * ata_bus_probe - Reset and probe ATA bus
2485 * Master ATA bus probing function. Initiates a hardware-dependent
2486 * bus reset, then attempts to identify any devices found on
2490 * PCI/etc. bus probe sem.
2493 * Zero on success, negative errno otherwise.
2496 int ata_bus_probe(struct ata_port *ap)
2498 unsigned int classes[ATA_MAX_DEVICES];
2499 int tries[ATA_MAX_DEVICES];
2501 struct ata_device *dev;
2505 ata_link_for_each_dev(dev, &ap->link)
2506 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2509 ata_link_for_each_dev(dev, &ap->link) {
2510 /* If we issue an SRST then an ATA drive (not ATAPI)
2511 * may change configuration and be in PIO0 timing. If
2512 * we do a hard reset (or are coming from power on)
2513 * this is true for ATA or ATAPI. Until we've set a
2514 * suitable controller mode we should not touch the
2515 * bus as we may be talking too fast.
2517 dev->pio_mode = XFER_PIO_0;
2519 /* If the controller has a pio mode setup function
2520 * then use it to set the chipset to rights. Don't
2521 * touch the DMA setup as that will be dealt with when
2522 * configuring devices.
2524 if (ap->ops->set_piomode)
2525 ap->ops->set_piomode(ap, dev);
2528 /* reset and determine device classes */
2529 ap->ops->phy_reset(ap);
2531 ata_link_for_each_dev(dev, &ap->link) {
2532 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2533 dev->class != ATA_DEV_UNKNOWN)
2534 classes[dev->devno] = dev->class;
2536 classes[dev->devno] = ATA_DEV_NONE;
2538 dev->class = ATA_DEV_UNKNOWN;
2543 /* read IDENTIFY page and configure devices. We have to do the identify
2544 specific sequence bass-ackwards so that PDIAG- is released by
2547 ata_link_for_each_dev_reverse(dev, &ap->link) {
2548 if (tries[dev->devno])
2549 dev->class = classes[dev->devno];
2551 if (!ata_dev_enabled(dev))
2554 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2560 /* Now ask for the cable type as PDIAG- should have been released */
2561 if (ap->ops->cable_detect)
2562 ap->cbl = ap->ops->cable_detect(ap);
2564 /* We may have SATA bridge glue hiding here irrespective of the
2565 reported cable types and sensed types */
2566 ata_link_for_each_dev(dev, &ap->link) {
2567 if (!ata_dev_enabled(dev))
2569 /* SATA drives indicate we have a bridge. We don't know which
2570 end of the link the bridge is which is a problem */
2571 if (ata_id_is_sata(dev->id))
2572 ap->cbl = ATA_CBL_SATA;
2575 /* After the identify sequence we can now set up the devices. We do
2576 this in the normal order so that the user doesn't get confused */
2578 ata_link_for_each_dev(dev, &ap->link) {
2579 if (!ata_dev_enabled(dev))
2582 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2583 rc = ata_dev_configure(dev);
2584 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2589 /* configure transfer mode */
2590 rc = ata_set_mode(&ap->link, &dev);
2594 ata_link_for_each_dev(dev, &ap->link)
2595 if (ata_dev_enabled(dev))
2598 /* no device present, disable port */
2599 ata_port_disable(ap);
2603 tries[dev->devno]--;
2607 /* eeek, something went very wrong, give up */
2608 tries[dev->devno] = 0;
2612 /* give it just one more chance */
2613 tries[dev->devno] = min(tries[dev->devno], 1);
2615 if (tries[dev->devno] == 1) {
2616 /* This is the last chance, better to slow
2617 * down than lose it.
2619 sata_down_spd_limit(&ap->link);
2620 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2624 if (!tries[dev->devno])
2625 ata_dev_disable(dev);
2631 * ata_port_probe - Mark port as enabled
2632 * @ap: Port for which we indicate enablement
2634 * Modify @ap data structure such that the system
2635 * thinks that the entire port is enabled.
2637 * LOCKING: host lock, or some other form of
2641 void ata_port_probe(struct ata_port *ap)
2643 ap->flags &= ~ATA_FLAG_DISABLED;
2647 * sata_print_link_status - Print SATA link status
2648 * @link: SATA link to printk link status about
2650 * This function prints link speed and status of a SATA link.
2655 void sata_print_link_status(struct ata_link *link)
2657 u32 sstatus, scontrol, tmp;
2659 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2661 sata_scr_read(link, SCR_CONTROL, &scontrol);
2663 if (ata_link_online(link)) {
2664 tmp = (sstatus >> 4) & 0xf;
2665 ata_link_printk(link, KERN_INFO,
2666 "SATA link up %s (SStatus %X SControl %X)\n",
2667 sata_spd_string(tmp), sstatus, scontrol);
2669 ata_link_printk(link, KERN_INFO,
2670 "SATA link down (SStatus %X SControl %X)\n",
2676 * ata_dev_pair - return other device on cable
2679 * Obtain the other device on the same cable, or if none is
2680 * present NULL is returned
2683 struct ata_device *ata_dev_pair(struct ata_device *adev)
2685 struct ata_link *link = adev->link;
2686 struct ata_device *pair = &link->device[1 - adev->devno];
2687 if (!ata_dev_enabled(pair))
2693 * ata_port_disable - Disable port.
2694 * @ap: Port to be disabled.
2696 * Modify @ap data structure such that the system
2697 * thinks that the entire port is disabled, and should
2698 * never attempt to probe or communicate with devices
2701 * LOCKING: host lock, or some other form of
2705 void ata_port_disable(struct ata_port *ap)
2707 ap->link.device[0].class = ATA_DEV_NONE;
2708 ap->link.device[1].class = ATA_DEV_NONE;
2709 ap->flags |= ATA_FLAG_DISABLED;
2713 * sata_down_spd_limit - adjust SATA spd limit downward
2714 * @link: Link to adjust SATA spd limit for
2716 * Adjust SATA spd limit of @link downward. Note that this
2717 * function only adjusts the limit. The change must be applied
2718 * using sata_set_spd().
2721 * Inherited from caller.
2724 * 0 on success, negative errno on failure
2726 int sata_down_spd_limit(struct ata_link *link)
2728 u32 sstatus, spd, mask;
2731 if (!sata_scr_valid(link))
2734 /* If SCR can be read, use it to determine the current SPD.
2735 * If not, use cached value in link->sata_spd.
2737 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2739 spd = (sstatus >> 4) & 0xf;
2741 spd = link->sata_spd;
2743 mask = link->sata_spd_limit;
2747 /* unconditionally mask off the highest bit */
2748 highbit = fls(mask) - 1;
2749 mask &= ~(1 << highbit);
2751 /* Mask off all speeds higher than or equal to the current
2752 * one. Force 1.5Gbps if current SPD is not available.
2755 mask &= (1 << (spd - 1)) - 1;
2759 /* were we already at the bottom? */
2763 link->sata_spd_limit = mask;
2765 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2766 sata_spd_string(fls(mask)));
2771 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2773 struct ata_link *host_link = &link->ap->link;
2774 u32 limit, target, spd;
2776 limit = link->sata_spd_limit;
2778 /* Don't configure downstream link faster than upstream link.
2779 * It doesn't speed up anything and some PMPs choke on such
2782 if (!ata_is_host_link(link) && host_link->sata_spd)
2783 limit &= (1 << host_link->sata_spd) - 1;
2785 if (limit == UINT_MAX)
2788 target = fls(limit);
2790 spd = (*scontrol >> 4) & 0xf;
2791 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2793 return spd != target;
2797 * sata_set_spd_needed - is SATA spd configuration needed
2798 * @link: Link in question
2800 * Test whether the spd limit in SControl matches
2801 * @link->sata_spd_limit. This function is used to determine
2802 * whether hardreset is necessary to apply SATA spd
2806 * Inherited from caller.
2809 * 1 if SATA spd configuration is needed, 0 otherwise.
2811 int sata_set_spd_needed(struct ata_link *link)
2815 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2818 return __sata_set_spd_needed(link, &scontrol);
2822 * sata_set_spd - set SATA spd according to spd limit
2823 * @link: Link to set SATA spd for
2825 * Set SATA spd of @link according to sata_spd_limit.
2828 * Inherited from caller.
2831 * 0 if spd doesn't need to be changed, 1 if spd has been
2832 * changed. Negative errno if SCR registers are inaccessible.
2834 int sata_set_spd(struct ata_link *link)
2839 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2842 if (!__sata_set_spd_needed(link, &scontrol))
2845 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2852 * This mode timing computation functionality is ported over from
2853 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2856 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2857 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2858 * for UDMA6, which is currently supported only by Maxtor drives.
2860 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2863 static const struct ata_timing ata_timing[] = {
2864 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2865 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2866 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2867 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2868 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2869 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2870 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2871 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2873 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2874 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2875 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2877 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2878 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2879 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2880 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
2881 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2883 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2884 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2885 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2886 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2887 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2888 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2889 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2890 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2895 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
2896 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
2898 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2900 q->setup = EZ(t->setup * 1000, T);
2901 q->act8b = EZ(t->act8b * 1000, T);
2902 q->rec8b = EZ(t->rec8b * 1000, T);
2903 q->cyc8b = EZ(t->cyc8b * 1000, T);
2904 q->active = EZ(t->active * 1000, T);
2905 q->recover = EZ(t->recover * 1000, T);
2906 q->cycle = EZ(t->cycle * 1000, T);
2907 q->udma = EZ(t->udma * 1000, UT);
2910 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2911 struct ata_timing *m, unsigned int what)
2913 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2914 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2915 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2916 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2917 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2918 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2919 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2920 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2923 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2925 const struct ata_timing *t = ata_timing;
2927 while (xfer_mode > t->mode)
2930 if (xfer_mode == t->mode)
2935 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2936 struct ata_timing *t, int T, int UT)
2938 const struct ata_timing *s;
2939 struct ata_timing p;
2945 if (!(s = ata_timing_find_mode(speed)))
2948 memcpy(t, s, sizeof(*s));
2951 * If the drive is an EIDE drive, it can tell us it needs extended
2952 * PIO/MW_DMA cycle timing.
2955 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2956 memset(&p, 0, sizeof(p));
2957 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2958 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2959 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2960 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2961 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2963 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2967 * Convert the timing to bus clock counts.
2970 ata_timing_quantize(t, t, T, UT);
2973 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2974 * S.M.A.R.T * and some other commands. We have to ensure that the
2975 * DMA cycle timing is slower/equal than the fastest PIO timing.
2978 if (speed > XFER_PIO_6) {
2979 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2980 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2984 * Lengthen active & recovery time so that cycle time is correct.
2987 if (t->act8b + t->rec8b < t->cyc8b) {
2988 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2989 t->rec8b = t->cyc8b - t->act8b;
2992 if (t->active + t->recover < t->cycle) {
2993 t->active += (t->cycle - (t->active + t->recover)) / 2;
2994 t->recover = t->cycle - t->active;
2997 /* In a few cases quantisation may produce enough errors to
2998 leave t->cycle too low for the sum of active and recovery
2999 if so we must correct this */
3000 if (t->active + t->recover > t->cycle)
3001 t->cycle = t->active + t->recover;
3007 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3008 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3009 * @cycle: cycle duration in ns
3011 * Return matching xfer mode for @cycle. The returned mode is of
3012 * the transfer type specified by @xfer_shift. If @cycle is too
3013 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3014 * than the fastest known mode, the fasted mode is returned.
3020 * Matching xfer_mode, 0xff if no match found.
3022 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3024 u8 base_mode = 0xff, last_mode = 0xff;
3025 const struct ata_xfer_ent *ent;
3026 const struct ata_timing *t;
3028 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3029 if (ent->shift == xfer_shift)
3030 base_mode = ent->base;
3032 for (t = ata_timing_find_mode(base_mode);
3033 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3034 unsigned short this_cycle;
3036 switch (xfer_shift) {
3038 case ATA_SHIFT_MWDMA:
3039 this_cycle = t->cycle;
3041 case ATA_SHIFT_UDMA:
3042 this_cycle = t->udma;
3048 if (cycle > this_cycle)
3051 last_mode = t->mode;
3058 * ata_down_xfermask_limit - adjust dev xfer masks downward
3059 * @dev: Device to adjust xfer masks
3060 * @sel: ATA_DNXFER_* selector
3062 * Adjust xfer masks of @dev downward. Note that this function
3063 * does not apply the change. Invoking ata_set_mode() afterwards
3064 * will apply the limit.
3067 * Inherited from caller.
3070 * 0 on success, negative errno on failure
3072 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3075 unsigned long orig_mask, xfer_mask;
3076 unsigned long pio_mask, mwdma_mask, udma_mask;
3079 quiet = !!(sel & ATA_DNXFER_QUIET);
3080 sel &= ~ATA_DNXFER_QUIET;
3082 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3085 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3088 case ATA_DNXFER_PIO:
3089 highbit = fls(pio_mask) - 1;
3090 pio_mask &= ~(1 << highbit);
3093 case ATA_DNXFER_DMA:
3095 highbit = fls(udma_mask) - 1;
3096 udma_mask &= ~(1 << highbit);
3099 } else if (mwdma_mask) {
3100 highbit = fls(mwdma_mask) - 1;
3101 mwdma_mask &= ~(1 << highbit);
3107 case ATA_DNXFER_40C:
3108 udma_mask &= ATA_UDMA_MASK_40C;
3111 case ATA_DNXFER_FORCE_PIO0:
3113 case ATA_DNXFER_FORCE_PIO:
3122 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3124 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3128 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3129 snprintf(buf, sizeof(buf), "%s:%s",
3130 ata_mode_string(xfer_mask),
3131 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3133 snprintf(buf, sizeof(buf), "%s",
3134 ata_mode_string(xfer_mask));
3136 ata_dev_printk(dev, KERN_WARNING,
3137 "limiting speed to %s\n", buf);
3140 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3146 static int ata_dev_set_mode(struct ata_device *dev)
3148 struct ata_eh_context *ehc = &dev->link->eh_context;
3149 const char *dev_err_whine = "";
3150 int ign_dev_err = 0;
3151 unsigned int err_mask;
3154 dev->flags &= ~ATA_DFLAG_PIO;
3155 if (dev->xfer_shift == ATA_SHIFT_PIO)
3156 dev->flags |= ATA_DFLAG_PIO;
3158 err_mask = ata_dev_set_xfermode(dev);
3160 if (err_mask & ~AC_ERR_DEV)
3164 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3165 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3166 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3170 /* Old CFA may refuse this command, which is just fine */
3171 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
3174 /* Some very old devices and some bad newer ones fail any kind of
3175 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3176 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3177 dev->pio_mode <= XFER_PIO_2)
3180 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3181 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3182 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3183 dev->dma_mode == XFER_MW_DMA_0 &&
3184 (dev->id[63] >> 8) & 1)
3187 /* if the device is actually configured correctly, ignore dev err */
3188 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3191 if (err_mask & AC_ERR_DEV) {
3195 dev_err_whine = " (device error ignored)";
3198 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3199 dev->xfer_shift, (int)dev->xfer_mode);
3201 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3202 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3208 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3209 "(err_mask=0x%x)\n", err_mask);
3214 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3215 * @link: link on which timings will be programmed
3216 * @r_failed_dev: out parameter for failed device
3218 * Standard implementation of the function used to tune and set
3219 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3220 * ata_dev_set_mode() fails, pointer to the failing device is
3221 * returned in @r_failed_dev.
3224 * PCI/etc. bus probe sem.
3227 * 0 on success, negative errno otherwise
3230 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3232 struct ata_port *ap = link->ap;
3233 struct ata_device *dev;
3234 int rc = 0, used_dma = 0, found = 0;
3236 /* step 1: calculate xfer_mask */
3237 ata_link_for_each_dev(dev, link) {
3238 unsigned long pio_mask, dma_mask;
3239 unsigned int mode_mask;
3241 if (!ata_dev_enabled(dev))
3244 mode_mask = ATA_DMA_MASK_ATA;
3245 if (dev->class == ATA_DEV_ATAPI)
3246 mode_mask = ATA_DMA_MASK_ATAPI;
3247 else if (ata_id_is_cfa(dev->id))
3248 mode_mask = ATA_DMA_MASK_CFA;
3250 ata_dev_xfermask(dev);
3251 ata_force_xfermask(dev);
3253 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3254 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3256 if (libata_dma_mask & mode_mask)
3257 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3261 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3262 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3265 if (dev->dma_mode != 0xff)
3271 /* step 2: always set host PIO timings */
3272 ata_link_for_each_dev(dev, link) {
3273 if (!ata_dev_enabled(dev))
3276 if (dev->pio_mode == 0xff) {
3277 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3282 dev->xfer_mode = dev->pio_mode;
3283 dev->xfer_shift = ATA_SHIFT_PIO;
3284 if (ap->ops->set_piomode)
3285 ap->ops->set_piomode(ap, dev);
3288 /* step 3: set host DMA timings */
3289 ata_link_for_each_dev(dev, link) {
3290 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
3293 dev->xfer_mode = dev->dma_mode;
3294 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3295 if (ap->ops->set_dmamode)
3296 ap->ops->set_dmamode(ap, dev);
3299 /* step 4: update devices' xfer mode */
3300 ata_link_for_each_dev(dev, link) {
3301 /* don't update suspended devices' xfer mode */
3302 if (!ata_dev_enabled(dev))
3305 rc = ata_dev_set_mode(dev);
3310 /* Record simplex status. If we selected DMA then the other
3311 * host channels are not permitted to do so.
3313 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3314 ap->host->simplex_claimed = ap;
3318 *r_failed_dev = dev;
3323 * sata_link_debounce - debounce SATA phy status
3324 * @link: ATA link to debounce SATA phy status for
3325 * @params: timing parameters { interval, duratinon, timeout } in msec
3326 * @deadline: deadline jiffies for the operation
3328 * Make sure SStatus of @link reaches stable state, determined by
3329 * holding the same value where DET is not 1 for @duration polled
3330 * every @interval, before @timeout. Timeout constraints the
3331 * beginning of the stable state. Because DET gets stuck at 1 on
3332 * some controllers after hot unplugging, this functions waits
3333 * until timeout then returns 0 if DET is stable at 1.
3335 * @timeout is further limited by @deadline. The sooner of the
3339 * Kernel thread context (may sleep)
3342 * 0 on success, -errno on failure.
3344 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3345 unsigned long deadline)
3347 unsigned long interval_msec = params[0];
3348 unsigned long duration = msecs_to_jiffies(params[1]);
3349 unsigned long last_jiffies, t;
3353 t = jiffies + msecs_to_jiffies(params[2]);
3354 if (time_before(t, deadline))
3357 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3362 last_jiffies = jiffies;
3365 msleep(interval_msec);
3366 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3372 if (cur == 1 && time_before(jiffies, deadline))
3374 if (time_after(jiffies, last_jiffies + duration))
3379 /* unstable, start over */
3381 last_jiffies = jiffies;
3383 /* Check deadline. If debouncing failed, return
3384 * -EPIPE to tell upper layer to lower link speed.
3386 if (time_after(jiffies, deadline))
3392 * sata_link_resume - resume SATA link
3393 * @link: ATA link to resume SATA
3394 * @params: timing parameters { interval, duratinon, timeout } in msec
3395 * @deadline: deadline jiffies for the operation
3397 * Resume SATA phy @link and debounce it.
3400 * Kernel thread context (may sleep)
3403 * 0 on success, -errno on failure.
3405 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3406 unsigned long deadline)
3411 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3414 scontrol = (scontrol & 0x0f0) | 0x300;
3416 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3419 /* Some PHYs react badly if SStatus is pounded immediately
3420 * after resuming. Delay 200ms before debouncing.
3424 return sata_link_debounce(link, params, deadline);
3428 * ata_std_prereset - prepare for reset
3429 * @link: ATA link to be reset
3430 * @deadline: deadline jiffies for the operation
3432 * @link is about to be reset. Initialize it. Failure from
3433 * prereset makes libata abort whole reset sequence and give up
3434 * that port, so prereset should be best-effort. It does its
3435 * best to prepare for reset sequence but if things go wrong, it
3436 * should just whine, not fail.
3439 * Kernel thread context (may sleep)
3442 * 0 on success, -errno otherwise.
3444 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3446 struct ata_port *ap = link->ap;
3447 struct ata_eh_context *ehc = &link->eh_context;
3448 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3451 /* if we're about to do hardreset, nothing more to do */
3452 if (ehc->i.action & ATA_EH_HARDRESET)
3455 /* if SATA, resume link */
3456 if (ap->flags & ATA_FLAG_SATA) {
3457 rc = sata_link_resume(link, timing, deadline);
3458 /* whine about phy resume failure but proceed */
3459 if (rc && rc != -EOPNOTSUPP)
3460 ata_link_printk(link, KERN_WARNING, "failed to resume "
3461 "link for reset (errno=%d)\n", rc);
3464 /* wait for !BSY if we don't know that no device is attached */
3465 if (!ata_link_offline(link)) {
3466 rc = ata_wait_ready(ap, deadline);
3467 if (rc && rc != -ENODEV) {
3468 ata_link_printk(link, KERN_WARNING, "device not ready "
3469 "(errno=%d), forcing hardreset\n", rc);
3470 ehc->i.action |= ATA_EH_HARDRESET;
3478 * sata_link_hardreset - reset link via SATA phy reset
3479 * @link: link to reset
3480 * @timing: timing parameters { interval, duratinon, timeout } in msec
3481 * @deadline: deadline jiffies for the operation
3483 * SATA phy-reset @link using DET bits of SControl register.
3486 * Kernel thread context (may sleep)
3489 * 0 on success, -errno otherwise.
3491 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3492 unsigned long deadline)
3499 if (sata_set_spd_needed(link)) {
3500 /* SATA spec says nothing about how to reconfigure
3501 * spd. To be on the safe side, turn off phy during
3502 * reconfiguration. This works for at least ICH7 AHCI
3505 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3508 scontrol = (scontrol & 0x0f0) | 0x304;
3510 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3516 /* issue phy wake/reset */
3517 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3520 scontrol = (scontrol & 0x0f0) | 0x301;
3522 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3525 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3526 * 10.4.2 says at least 1 ms.
3530 /* bring link back */
3531 rc = sata_link_resume(link, timing, deadline);
3533 DPRINTK("EXIT, rc=%d\n", rc);
3538 * ata_std_postreset - standard postreset callback
3539 * @link: the target ata_link
3540 * @classes: classes of attached devices
3542 * This function is invoked after a successful reset. Note that
3543 * the device might have been reset more than once using
3544 * different reset methods before postreset is invoked.
3547 * Kernel thread context (may sleep)
3549 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3551 struct ata_port *ap = link->ap;
3556 /* print link status */
3557 sata_print_link_status(link);
3560 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3561 sata_scr_write(link, SCR_ERROR, serror);
3562 link->eh_info.serror = 0;
3564 /* is double-select really necessary? */
3565 if (classes[0] != ATA_DEV_NONE)
3566 ap->ops->dev_select(ap, 1);
3567 if (classes[1] != ATA_DEV_NONE)
3568 ap->ops->dev_select(ap, 0);
3570 /* bail out if no device is present */
3571 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3572 DPRINTK("EXIT, no device\n");
3576 /* set up device control */
3577 if (ap->ioaddr.ctl_addr)
3578 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
3584 * ata_dev_same_device - Determine whether new ID matches configured device
3585 * @dev: device to compare against
3586 * @new_class: class of the new device
3587 * @new_id: IDENTIFY page of the new device
3589 * Compare @new_class and @new_id against @dev and determine
3590 * whether @dev is the device indicated by @new_class and
3597 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3599 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3602 const u16 *old_id = dev->id;
3603 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3604 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3606 if (dev->class != new_class) {
3607 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3608 dev->class, new_class);
3612 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3613 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3614 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3615 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3617 if (strcmp(model[0], model[1])) {
3618 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3619 "'%s' != '%s'\n", model[0], model[1]);
3623 if (strcmp(serial[0], serial[1])) {
3624 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3625 "'%s' != '%s'\n", serial[0], serial[1]);
3633 * ata_dev_reread_id - Re-read IDENTIFY data
3634 * @dev: target ATA device
3635 * @readid_flags: read ID flags
3637 * Re-read IDENTIFY page and make sure @dev is still attached to
3641 * Kernel thread context (may sleep)
3644 * 0 on success, negative errno otherwise
3646 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3648 unsigned int class = dev->class;
3649 u16 *id = (void *)dev->link->ap->sector_buf;
3653 rc = ata_dev_read_id(dev, &class, readid_flags, id);
3657 /* is the device still there? */
3658 if (!ata_dev_same_device(dev, class, id))
3661 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3666 * ata_dev_revalidate - Revalidate ATA device
3667 * @dev: device to revalidate
3668 * @new_class: new class code
3669 * @readid_flags: read ID flags
3671 * Re-read IDENTIFY page, make sure @dev is still attached to the
3672 * port and reconfigure it according to the new IDENTIFY page.
3675 * Kernel thread context (may sleep)
3678 * 0 on success, negative errno otherwise
3680 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3681 unsigned int readid_flags)
3683 u64 n_sectors = dev->n_sectors;
3686 if (!ata_dev_enabled(dev))
3689 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3690 if (ata_class_enabled(new_class) &&
3691 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3692 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3693 dev->class, new_class);
3699 rc = ata_dev_reread_id(dev, readid_flags);
3703 /* configure device according to the new ID */
3704 rc = ata_dev_configure(dev);
3708 /* verify n_sectors hasn't changed */
3709 if (dev->class == ATA_DEV_ATA && n_sectors &&
3710 dev->n_sectors != n_sectors) {
3711 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3713 (unsigned long long)n_sectors,
3714 (unsigned long long)dev->n_sectors);
3716 /* restore original n_sectors */
3717 dev->n_sectors = n_sectors;
3726 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
3730 struct ata_blacklist_entry {
3731 const char *model_num;
3732 const char *model_rev;
3733 unsigned long horkage;
3736 static const struct ata_blacklist_entry ata_device_blacklist [] = {
3737 /* Devices with DMA related problems under Linux */
3738 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3739 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3740 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3741 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3742 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3743 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3744 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3745 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3746 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3747 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3748 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3749 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3750 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3751 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3752 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3753 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3754 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3755 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3756 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3757 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3758 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3759 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3760 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3761 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3762 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3763 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
3764 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3765 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3766 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
3767 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
3768 /* Odd clown on sil3726/4726 PMPs */
3769 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
3770 ATA_HORKAGE_SKIP_PM },
3772 /* Weird ATAPI devices */
3773 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
3775 /* Devices we expect to fail diagnostics */
3777 /* Devices where NCQ should be avoided */
3779 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
3780 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
3781 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3782 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
3784 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
3785 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
3786 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
3787 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
3789 /* Blacklist entries taken from Silicon Image 3124/3132
3790 Windows driver .inf file - also several Linux problem reports */
3791 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3792 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3793 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
3795 /* devices which puke on READ_NATIVE_MAX */
3796 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3797 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3798 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3799 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
3801 /* Devices which report 1 sector over size HPA */
3802 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3803 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
3804 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
3806 /* Devices which get the IVB wrong */
3807 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
3808 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
3809 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
3810 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
3811 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
3817 static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3823 * check for trailing wildcard: *\0
3825 p = strchr(patt, wildchar);
3826 if (p && ((*(p + 1)) == 0))
3837 return strncmp(patt, name, len);
3840 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
3842 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3843 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
3844 const struct ata_blacklist_entry *ad = ata_device_blacklist;
3846 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3847 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
3849 while (ad->model_num) {
3850 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
3851 if (ad->model_rev == NULL)
3853 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
3861 static int ata_dma_blacklisted(const struct ata_device *dev)
3863 /* We don't support polling DMA.
3864 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3865 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3867 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
3868 (dev->flags & ATA_DFLAG_CDB_INTR))
3870 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
3874 * ata_is_40wire - check drive side detection
3877 * Perform drive side detection decoding, allowing for device vendors
3878 * who can't follow the documentation.
3881 static int ata_is_40wire(struct ata_device *dev)
3883 if (dev->horkage & ATA_HORKAGE_IVB)
3884 return ata_drive_40wire_relaxed(dev->id);
3885 return ata_drive_40wire(dev->id);
3889 * cable_is_40wire - 40/80/SATA decider
3890 * @ap: port to consider
3892 * This function encapsulates the policy for speed management
3893 * in one place. At the moment we don't cache the result but
3894 * there is a good case for setting ap->cbl to the result when
3895 * we are called with unknown cables (and figuring out if it
3896 * impacts hotplug at all).
3898 * Return 1 if the cable appears to be 40 wire.
3901 static int cable_is_40wire(struct ata_port *ap)
3903 struct ata_link *link;
3904 struct ata_device *dev;
3906 /* If the controller thinks we are 40 wire, we are */
3907 if (ap->cbl == ATA_CBL_PATA40)
3909 /* If the controller thinks we are 80 wire, we are */
3910 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
3912 /* If the controller doesn't know we scan
3914 - Note: We look for all 40 wire detects at this point.
3915 Any 80 wire detect is taken to be 80 wire cable
3917 - In many setups only the one drive (slave if present)
3918 will give a valid detect
3919 - If you have a non detect capable drive you don't
3920 want it to colour the choice
3922 ata_port_for_each_link(link, ap) {
3923 ata_link_for_each_dev(dev, link) {
3924 if (!ata_is_40wire(dev))
3932 * ata_dev_xfermask - Compute supported xfermask of the given device
3933 * @dev: Device to compute xfermask for
3935 * Compute supported xfermask of @dev and store it in
3936 * dev->*_mask. This function is responsible for applying all
3937 * known limits including host controller limits, device
3943 static void ata_dev_xfermask(struct ata_device *dev)
3945 struct ata_link *link = dev->link;
3946 struct ata_port *ap = link->ap;
3947 struct ata_host *host = ap->host;
3948 unsigned long xfer_mask;
3950 /* controller modes available */
3951 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3952 ap->mwdma_mask, ap->udma_mask);
3954 /* drive modes available */
3955 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3956 dev->mwdma_mask, dev->udma_mask);
3957 xfer_mask &= ata_id_xfermask(dev->id);
3960 * CFA Advanced TrueIDE timings are not allowed on a shared
3963 if (ata_dev_pair(dev)) {
3964 /* No PIO5 or PIO6 */
3965 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3966 /* No MWDMA3 or MWDMA 4 */
3967 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3970 if (ata_dma_blacklisted(dev)) {
3971 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3972 ata_dev_printk(dev, KERN_WARNING,
3973 "device is on DMA blacklist, disabling DMA\n");
3976 if ((host->flags & ATA_HOST_SIMPLEX) &&
3977 host->simplex_claimed && host->simplex_claimed != ap) {
3978 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3979 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3980 "other device, disabling DMA\n");
3983 if (ap->flags & ATA_FLAG_NO_IORDY)
3984 xfer_mask &= ata_pio_mask_no_iordy(dev);
3986 if (ap->ops->mode_filter)
3987 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
3989 /* Apply cable rule here. Don't apply it early because when
3990 * we handle hot plug the cable type can itself change.
3991 * Check this last so that we know if the transfer rate was
3992 * solely limited by the cable.
3993 * Unknown or 80 wire cables reported host side are checked
3994 * drive side as well. Cases where we know a 40wire cable
3995 * is used safely for 80 are not checked here.
3997 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
3998 /* UDMA/44 or higher would be available */
3999 if (cable_is_40wire(ap)) {
4000 ata_dev_printk(dev, KERN_WARNING,
4001 "limited to UDMA/33 due to 40-wire cable\n");
4002 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4005 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4006 &dev->mwdma_mask, &dev->udma_mask);
4010 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4011 * @dev: Device to which command will be sent
4013 * Issue SET FEATURES - XFER MODE command to device @dev
4017 * PCI/etc. bus probe sem.
4020 * 0 on success, AC_ERR_* mask otherwise.
4023 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4025 struct ata_taskfile tf;
4026 unsigned int err_mask;
4028 /* set up set-features taskfile */
4029 DPRINTK("set features - xfer mode\n");
4031 /* Some controllers and ATAPI devices show flaky interrupt
4032 * behavior after setting xfer mode. Use polling instead.
4034 ata_tf_init(dev, &tf);
4035 tf.command = ATA_CMD_SET_FEATURES;
4036 tf.feature = SETFEATURES_XFER;
4037 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4038 tf.protocol = ATA_PROT_NODATA;
4039 /* If we are using IORDY we must send the mode setting command */
4040 if (ata_pio_need_iordy(dev))
4041 tf.nsect = dev->xfer_mode;
4042 /* If the device has IORDY and the controller does not - turn it off */
4043 else if (ata_id_has_iordy(dev->id))
4045 else /* In the ancient relic department - skip all of this */
4048 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4050 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4054 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4055 * @dev: Device to which command will be sent
4056 * @enable: Whether to enable or disable the feature
4057 * @feature: The sector count represents the feature to set
4059 * Issue SET FEATURES - SATA FEATURES command to device @dev
4060 * on port @ap with sector count
4063 * PCI/etc. bus probe sem.
4066 * 0 on success, AC_ERR_* mask otherwise.
4068 static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4071 struct ata_taskfile tf;
4072 unsigned int err_mask;
4074 /* set up set-features taskfile */
4075 DPRINTK("set features - SATA features\n");
4077 ata_tf_init(dev, &tf);
4078 tf.command = ATA_CMD_SET_FEATURES;
4079 tf.feature = enable;
4080 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4081 tf.protocol = ATA_PROT_NODATA;
4084 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4086 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4091 * ata_dev_init_params - Issue INIT DEV PARAMS command
4092 * @dev: Device to which command will be sent
4093 * @heads: Number of heads (taskfile parameter)
4094 * @sectors: Number of sectors (taskfile parameter)
4097 * Kernel thread context (may sleep)
4100 * 0 on success, AC_ERR_* mask otherwise.
4102 static unsigned int ata_dev_init_params(struct ata_device *dev,
4103 u16 heads, u16 sectors)
4105 struct ata_taskfile tf;
4106 unsigned int err_mask;
4108 /* Number of sectors per track 1-255. Number of heads 1-16 */
4109 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4110 return AC_ERR_INVALID;
4112 /* set up init dev params taskfile */
4113 DPRINTK("init dev params \n");
4115 ata_tf_init(dev, &tf);
4116 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4117 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4118 tf.protocol = ATA_PROT_NODATA;
4120 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4122 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4123 /* A clean abort indicates an original or just out of spec drive
4124 and we should continue as we issue the setup based on the
4125 drive reported working geometry */
4126 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4129 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4134 * ata_sg_clean - Unmap DMA memory associated with command
4135 * @qc: Command containing DMA memory to be released
4137 * Unmap all mapped DMA memory associated with this command.
4140 * spin_lock_irqsave(host lock)
4142 void ata_sg_clean(struct ata_queued_cmd *qc)
4144 struct ata_port *ap = qc->ap;
4145 struct scatterlist *sg = qc->sg;
4146 int dir = qc->dma_dir;
4148 WARN_ON(sg == NULL);
4150 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4153 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4155 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4160 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4161 * @qc: Metadata associated with taskfile to check
4163 * Allow low-level driver to filter ATA PACKET commands, returning
4164 * a status indicating whether or not it is OK to use DMA for the
4165 * supplied PACKET command.
4168 * spin_lock_irqsave(host lock)
4170 * RETURNS: 0 when ATAPI DMA can be used
4173 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4175 struct ata_port *ap = qc->ap;
4177 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4178 * few ATAPI devices choke on such DMA requests.
4180 if (unlikely(qc->nbytes & 15))
4183 if (ap->ops->check_atapi_dma)
4184 return ap->ops->check_atapi_dma(qc);
4190 * ata_std_qc_defer - Check whether a qc needs to be deferred
4191 * @qc: ATA command in question
4193 * Non-NCQ commands cannot run with any other command, NCQ or
4194 * not. As upper layer only knows the queue depth, we are
4195 * responsible for maintaining exclusion. This function checks
4196 * whether a new command @qc can be issued.
4199 * spin_lock_irqsave(host lock)
4202 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4204 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4206 struct ata_link *link = qc->dev->link;
4208 if (qc->tf.protocol == ATA_PROT_NCQ) {
4209 if (!ata_tag_valid(link->active_tag))
4212 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4216 return ATA_DEFER_LINK;
4219 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4222 * ata_sg_init - Associate command with scatter-gather table.
4223 * @qc: Command to be associated
4224 * @sg: Scatter-gather table.
4225 * @n_elem: Number of elements in s/g table.
4227 * Initialize the data-related elements of queued_cmd @qc
4228 * to point to a scatter-gather table @sg, containing @n_elem
4232 * spin_lock_irqsave(host lock)
4234 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4235 unsigned int n_elem)
4238 qc->n_elem = n_elem;
4243 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4244 * @qc: Command with scatter-gather table to be mapped.
4246 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4249 * spin_lock_irqsave(host lock)
4252 * Zero on success, negative on error.
4255 static int ata_sg_setup(struct ata_queued_cmd *qc)
4257 struct ata_port *ap = qc->ap;
4258 unsigned int n_elem;
4260 VPRINTK("ENTER, ata%u\n", ap->print_id);
4262 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4266 DPRINTK("%d sg elements mapped\n", n_elem);
4268 qc->n_elem = n_elem;
4269 qc->flags |= ATA_QCFLAG_DMAMAP;
4275 * swap_buf_le16 - swap halves of 16-bit words in place
4276 * @buf: Buffer to swap
4277 * @buf_words: Number of 16-bit words in buffer.
4279 * Swap halves of 16-bit words if needed to convert from
4280 * little-endian byte order to native cpu byte order, or
4284 * Inherited from caller.
4286 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4291 for (i = 0; i < buf_words; i++)
4292 buf[i] = le16_to_cpu(buf[i]);
4293 #endif /* __BIG_ENDIAN */
4297 * ata_qc_new - Request an available ATA command, for queueing
4298 * @ap: Port associated with device @dev
4299 * @dev: Device from whom we request an available command structure
4305 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4307 struct ata_queued_cmd *qc = NULL;
4310 /* no command while frozen */
4311 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4314 /* the last tag is reserved for internal command. */
4315 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
4316 if (!test_and_set_bit(i, &ap->qc_allocated)) {
4317 qc = __ata_qc_from_tag(ap, i);
4328 * ata_qc_new_init - Request an available ATA command, and initialize it
4329 * @dev: Device from whom we request an available command structure
4335 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4337 struct ata_port *ap = dev->link->ap;
4338 struct ata_queued_cmd *qc;
4340 qc = ata_qc_new(ap);
4353 * ata_qc_free - free unused ata_queued_cmd
4354 * @qc: Command to complete
4356 * Designed to free unused ata_queued_cmd object
4357 * in case something prevents using it.
4360 * spin_lock_irqsave(host lock)
4362 void ata_qc_free(struct ata_queued_cmd *qc)
4364 struct ata_port *ap = qc->ap;
4367 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4371 if (likely(ata_tag_valid(tag))) {
4372 qc->tag = ATA_TAG_POISON;
4373 clear_bit(tag, &ap->qc_allocated);
4377 void __ata_qc_complete(struct ata_queued_cmd *qc)
4379 struct ata_port *ap = qc->ap;
4380 struct ata_link *link = qc->dev->link;
4382 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4383 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4385 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4388 /* command should be marked inactive atomically with qc completion */
4389 if (qc->tf.protocol == ATA_PROT_NCQ) {
4390 link->sactive &= ~(1 << qc->tag);
4392 ap->nr_active_links--;
4394 link->active_tag = ATA_TAG_POISON;
4395 ap->nr_active_links--;
4398 /* clear exclusive status */
4399 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4400 ap->excl_link == link))
4401 ap->excl_link = NULL;
4403 /* atapi: mark qc as inactive to prevent the interrupt handler
4404 * from completing the command twice later, before the error handler
4405 * is called. (when rc != 0 and atapi request sense is needed)
4407 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4408 ap->qc_active &= ~(1 << qc->tag);
4410 /* call completion callback */
4411 qc->complete_fn(qc);
4414 static void fill_result_tf(struct ata_queued_cmd *qc)
4416 struct ata_port *ap = qc->ap;
4418 qc->result_tf.flags = qc->tf.flags;
4419 ap->ops->tf_read(ap, &qc->result_tf);
4422 static void ata_verify_xfer(struct ata_queued_cmd *qc)
4424 struct ata_device *dev = qc->dev;
4426 if (ata_tag_internal(qc->tag))
4429 if (ata_is_nodata(qc->tf.protocol))
4432 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4435 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4439 * ata_qc_complete - Complete an active ATA command
4440 * @qc: Command to complete
4441 * @err_mask: ATA Status register contents
4443 * Indicate to the mid and upper layers that an ATA
4444 * command has completed, with either an ok or not-ok status.
4447 * spin_lock_irqsave(host lock)
4449 void ata_qc_complete(struct ata_queued_cmd *qc)
4451 struct ata_port *ap = qc->ap;
4453 /* XXX: New EH and old EH use different mechanisms to
4454 * synchronize EH with regular execution path.
4456 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4457 * Normal execution path is responsible for not accessing a
4458 * failed qc. libata core enforces the rule by returning NULL
4459 * from ata_qc_from_tag() for failed qcs.
4461 * Old EH depends on ata_qc_complete() nullifying completion
4462 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4463 * not synchronize with interrupt handler. Only PIO task is
4466 if (ap->ops->error_handler) {
4467 struct ata_device *dev = qc->dev;
4468 struct ata_eh_info *ehi = &dev->link->eh_info;
4470 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
4472 if (unlikely(qc->err_mask))
4473 qc->flags |= ATA_QCFLAG_FAILED;
4475 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4476 if (!ata_tag_internal(qc->tag)) {
4477 /* always fill result TF for failed qc */
4479 ata_qc_schedule_eh(qc);
4484 /* read result TF if requested */
4485 if (qc->flags & ATA_QCFLAG_RESULT_TF)
4488 /* Some commands need post-processing after successful
4491 switch (qc->tf.command) {
4492 case ATA_CMD_SET_FEATURES:
4493 if (qc->tf.feature != SETFEATURES_WC_ON &&
4494 qc->tf.feature != SETFEATURES_WC_OFF)
4497 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4498 case ATA_CMD_SET_MULTI: /* multi_count changed */
4499 /* revalidate device */
4500 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4501 ata_port_schedule_eh(ap);
4505 dev->flags |= ATA_DFLAG_SLEEPING;
4509 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4510 ata_verify_xfer(qc);
4512 __ata_qc_complete(qc);
4514 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4517 /* read result TF if failed or requested */
4518 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
4521 __ata_qc_complete(qc);
4526 * ata_qc_complete_multiple - Complete multiple qcs successfully
4527 * @ap: port in question
4528 * @qc_active: new qc_active mask
4529 * @finish_qc: LLDD callback invoked before completing a qc
4531 * Complete in-flight commands. This functions is meant to be
4532 * called from low-level driver's interrupt routine to complete
4533 * requests normally. ap->qc_active and @qc_active is compared
4534 * and commands are completed accordingly.
4537 * spin_lock_irqsave(host lock)
4540 * Number of completed commands on success, -errno otherwise.
4542 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4543 void (*finish_qc)(struct ata_queued_cmd *))
4549 done_mask = ap->qc_active ^ qc_active;
4551 if (unlikely(done_mask & qc_active)) {
4552 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4553 "(%08x->%08x)\n", ap->qc_active, qc_active);
4557 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4558 struct ata_queued_cmd *qc;
4560 if (!(done_mask & (1 << i)))
4563 if ((qc = ata_qc_from_tag(ap, i))) {
4566 ata_qc_complete(qc);
4575 * ata_qc_issue - issue taskfile to device
4576 * @qc: command to issue to device
4578 * Prepare an ATA command to submission to device.
4579 * This includes mapping the data into a DMA-able
4580 * area, filling in the S/G table, and finally
4581 * writing the taskfile to hardware, starting the command.
4584 * spin_lock_irqsave(host lock)
4586 void ata_qc_issue(struct ata_queued_cmd *qc)
4588 struct ata_port *ap = qc->ap;
4589 struct ata_link *link = qc->dev->link;
4590 u8 prot = qc->tf.protocol;
4592 /* Make sure only one non-NCQ command is outstanding. The
4593 * check is skipped for old EH because it reuses active qc to
4594 * request ATAPI sense.
4596 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4598 if (ata_is_ncq(prot)) {
4599 WARN_ON(link->sactive & (1 << qc->tag));
4602 ap->nr_active_links++;
4603 link->sactive |= 1 << qc->tag;
4605 WARN_ON(link->sactive);
4607 ap->nr_active_links++;
4608 link->active_tag = qc->tag;
4611 qc->flags |= ATA_QCFLAG_ACTIVE;
4612 ap->qc_active |= 1 << qc->tag;
4614 /* We guarantee to LLDs that they will have at least one
4615 * non-zero sg if the command is a data command.
4617 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
4619 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4620 (ap->flags & ATA_FLAG_PIO_DMA)))
4621 if (ata_sg_setup(qc))
4624 /* if device is sleeping, schedule reset and abort the link */
4625 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4626 link->eh_info.action |= ATA_EH_RESET;
4627 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4628 ata_link_abort(link);
4632 ap->ops->qc_prep(qc);
4634 qc->err_mask |= ap->ops->qc_issue(qc);
4635 if (unlikely(qc->err_mask))
4640 qc->err_mask |= AC_ERR_SYSTEM;
4642 ata_qc_complete(qc);
4646 * sata_scr_valid - test whether SCRs are accessible
4647 * @link: ATA link to test SCR accessibility for
4649 * Test whether SCRs are accessible for @link.
4655 * 1 if SCRs are accessible, 0 otherwise.
4657 int sata_scr_valid(struct ata_link *link)
4659 struct ata_port *ap = link->ap;
4661 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
4665 * sata_scr_read - read SCR register of the specified port
4666 * @link: ATA link to read SCR for
4668 * @val: Place to store read value
4670 * Read SCR register @reg of @link into *@val. This function is
4671 * guaranteed to succeed if @link is ap->link, the cable type of
4672 * the port is SATA and the port implements ->scr_read.
4675 * None if @link is ap->link. Kernel thread context otherwise.
4678 * 0 on success, negative errno on failure.
4680 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
4682 if (ata_is_host_link(link)) {
4683 struct ata_port *ap = link->ap;
4685 if (sata_scr_valid(link))
4686 return ap->ops->scr_read(ap, reg, val);
4690 return sata_pmp_scr_read(link, reg, val);
4694 * sata_scr_write - write SCR register of the specified port
4695 * @link: ATA link to write SCR for
4696 * @reg: SCR to write
4697 * @val: value to write
4699 * Write @val to SCR register @reg of @link. This function is
4700 * guaranteed to succeed if @link is ap->link, the cable type of
4701 * the port is SATA and the port implements ->scr_read.
4704 * None if @link is ap->link. Kernel thread context otherwise.
4707 * 0 on success, negative errno on failure.
4709 int sata_scr_write(struct ata_link *link, int reg, u32 val)
4711 if (ata_is_host_link(link)) {
4712 struct ata_port *ap = link->ap;
4714 if (sata_scr_valid(link))
4715 return ap->ops->scr_write(ap, reg, val);
4719 return sata_pmp_scr_write(link, reg, val);
4723 * sata_scr_write_flush - write SCR register of the specified port and flush
4724 * @link: ATA link to write SCR for
4725 * @reg: SCR to write
4726 * @val: value to write
4728 * This function is identical to sata_scr_write() except that this
4729 * function performs flush after writing to the register.
4732 * None if @link is ap->link. Kernel thread context otherwise.
4735 * 0 on success, negative errno on failure.
4737 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
4739 if (ata_is_host_link(link)) {
4740 struct ata_port *ap = link->ap;
4743 if (sata_scr_valid(link)) {
4744 rc = ap->ops->scr_write(ap, reg, val);
4746 rc = ap->ops->scr_read(ap, reg, &val);
4752 return sata_pmp_scr_write(link, reg, val);
4756 * ata_link_online - test whether the given link is online
4757 * @link: ATA link to test
4759 * Test whether @link is online. Note that this function returns
4760 * 0 if online status of @link cannot be obtained, so
4761 * ata_link_online(link) != !ata_link_offline(link).
4767 * 1 if the port online status is available and online.
4769 int ata_link_online(struct ata_link *link)
4773 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4774 (sstatus & 0xf) == 0x3)
4780 * ata_link_offline - test whether the given link is offline
4781 * @link: ATA link to test
4783 * Test whether @link is offline. Note that this function
4784 * returns 0 if offline status of @link cannot be obtained, so
4785 * ata_link_online(link) != !ata_link_offline(link).
4791 * 1 if the port offline status is available and offline.
4793 int ata_link_offline(struct ata_link *link)
4797 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4798 (sstatus & 0xf) != 0x3)
4803 int ata_flush_cache(struct ata_device *dev)
4805 unsigned int err_mask;
4808 if (!ata_try_flush_cache(dev))
4811 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
4812 cmd = ATA_CMD_FLUSH_EXT;
4814 cmd = ATA_CMD_FLUSH;
4816 /* This is wrong. On a failed flush we get back the LBA of the lost
4817 sector and we should (assuming it wasn't aborted as unknown) issue
4818 a further flush command to continue the writeback until it
4820 err_mask = ata_do_simple_cmd(dev, cmd);
4822 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
4830 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
4831 unsigned int action, unsigned int ehi_flags,
4834 unsigned long flags;
4837 for (i = 0; i < host->n_ports; i++) {
4838 struct ata_port *ap = host->ports[i];
4839 struct ata_link *link;
4841 /* Previous resume operation might still be in
4842 * progress. Wait for PM_PENDING to clear.
4844 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4845 ata_port_wait_eh(ap);
4846 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4849 /* request PM ops to EH */
4850 spin_lock_irqsave(ap->lock, flags);
4855 ap->pm_result = &rc;
4858 ap->pflags |= ATA_PFLAG_PM_PENDING;
4859 __ata_port_for_each_link(link, ap) {
4860 link->eh_info.action |= action;
4861 link->eh_info.flags |= ehi_flags;
4864 ata_port_schedule_eh(ap);
4866 spin_unlock_irqrestore(ap->lock, flags);
4868 /* wait and check result */
4870 ata_port_wait_eh(ap);
4871 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4881 * ata_host_suspend - suspend host
4882 * @host: host to suspend
4885 * Suspend @host. Actual operation is performed by EH. This
4886 * function requests EH to perform PM operations and waits for EH
4890 * Kernel thread context (may sleep).
4893 * 0 on success, -errno on failure.
4895 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
4900 * disable link pm on all ports before requesting
4903 ata_lpm_enable(host);
4905 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
4907 host->dev->power.power_state = mesg;
4912 * ata_host_resume - resume host
4913 * @host: host to resume
4915 * Resume @host. Actual operation is performed by EH. This
4916 * function requests EH to perform PM operations and returns.
4917 * Note that all resume operations are performed parallely.
4920 * Kernel thread context (may sleep).
4922 void ata_host_resume(struct ata_host *host)
4924 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
4925 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
4926 host->dev->power.power_state = PMSG_ON;
4928 /* reenable link pm */
4929 ata_lpm_disable(host);
4934 * ata_port_start - Set port up for dma.
4935 * @ap: Port to initialize
4937 * Called just after data structures for each port are
4938 * initialized. Allocates space for PRD table.
4940 * May be used as the port_start() entry in ata_port_operations.
4943 * Inherited from caller.
4945 int ata_port_start(struct ata_port *ap)
4947 struct device *dev = ap->dev;
4949 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
4958 * ata_dev_init - Initialize an ata_device structure
4959 * @dev: Device structure to initialize
4961 * Initialize @dev in preparation for probing.
4964 * Inherited from caller.
4966 void ata_dev_init(struct ata_device *dev)
4968 struct ata_link *link = dev->link;
4969 struct ata_port *ap = link->ap;
4970 unsigned long flags;
4972 /* SATA spd limit is bound to the first device */
4973 link->sata_spd_limit = link->hw_sata_spd_limit;
4976 /* High bits of dev->flags are used to record warm plug
4977 * requests which occur asynchronously. Synchronize using
4980 spin_lock_irqsave(ap->lock, flags);
4981 dev->flags &= ~ATA_DFLAG_INIT_MASK;
4983 spin_unlock_irqrestore(ap->lock, flags);
4985 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
4986 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
4987 dev->pio_mask = UINT_MAX;
4988 dev->mwdma_mask = UINT_MAX;
4989 dev->udma_mask = UINT_MAX;
4993 * ata_link_init - Initialize an ata_link structure
4994 * @ap: ATA port link is attached to
4995 * @link: Link structure to initialize
4996 * @pmp: Port multiplier port number
5001 * Kernel thread context (may sleep)
5003 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
5007 /* clear everything except for devices */
5008 memset(link, 0, offsetof(struct ata_link, device[0]));
5012 link->active_tag = ATA_TAG_POISON;
5013 link->hw_sata_spd_limit = UINT_MAX;
5015 /* can't use iterator, ap isn't initialized yet */
5016 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5017 struct ata_device *dev = &link->device[i];
5020 dev->devno = dev - link->device;
5026 * sata_link_init_spd - Initialize link->sata_spd_limit
5027 * @link: Link to configure sata_spd_limit for
5029 * Initialize @link->[hw_]sata_spd_limit to the currently
5033 * Kernel thread context (may sleep).
5036 * 0 on success, -errno on failure.
5038 int sata_link_init_spd(struct ata_link *link)
5044 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
5048 spd = (scontrol >> 4) & 0xf;
5050 link->hw_sata_spd_limit &= (1 << spd) - 1;
5052 ata_force_spd_limit(link);
5054 link->sata_spd_limit = link->hw_sata_spd_limit;
5060 * ata_port_alloc - allocate and initialize basic ATA port resources
5061 * @host: ATA host this allocated port belongs to
5063 * Allocate and initialize basic ATA port resources.
5066 * Allocate ATA port on success, NULL on failure.
5069 * Inherited from calling layer (may sleep).
5071 struct ata_port *ata_port_alloc(struct ata_host *host)
5073 struct ata_port *ap;
5077 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5081 ap->pflags |= ATA_PFLAG_INITIALIZING;
5082 ap->lock = &host->lock;
5083 ap->flags = ATA_FLAG_DISABLED;
5085 ap->ctl = ATA_DEVCTL_OBS;
5087 ap->dev = host->dev;
5088 ap->last_ctl = 0xFF;
5090 #if defined(ATA_VERBOSE_DEBUG)
5091 /* turn on all debugging levels */
5092 ap->msg_enable = 0x00FF;
5093 #elif defined(ATA_DEBUG)
5094 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5096 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5099 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5100 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5101 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5102 INIT_LIST_HEAD(&ap->eh_done_q);
5103 init_waitqueue_head(&ap->eh_wait_q);
5104 init_timer_deferrable(&ap->fastdrain_timer);
5105 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5106 ap->fastdrain_timer.data = (unsigned long)ap;
5108 ap->cbl = ATA_CBL_NONE;
5110 ata_link_init(ap, &ap->link, 0);
5113 ap->stats.unhandled_irq = 1;
5114 ap->stats.idle_irq = 1;
5119 static void ata_host_release(struct device *gendev, void *res)
5121 struct ata_host *host = dev_get_drvdata(gendev);
5124 for (i = 0; i < host->n_ports; i++) {
5125 struct ata_port *ap = host->ports[i];
5131 scsi_host_put(ap->scsi_host);
5133 kfree(ap->pmp_link);
5135 host->ports[i] = NULL;
5138 dev_set_drvdata(gendev, NULL);
5142 * ata_host_alloc - allocate and init basic ATA host resources
5143 * @dev: generic device this host is associated with
5144 * @max_ports: maximum number of ATA ports associated with this host
5146 * Allocate and initialize basic ATA host resources. LLD calls
5147 * this function to allocate a host, initializes it fully and
5148 * attaches it using ata_host_register().
5150 * @max_ports ports are allocated and host->n_ports is
5151 * initialized to @max_ports. The caller is allowed to decrease
5152 * host->n_ports before calling ata_host_register(). The unused
5153 * ports will be automatically freed on registration.
5156 * Allocate ATA host on success, NULL on failure.
5159 * Inherited from calling layer (may sleep).
5161 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5163 struct ata_host *host;
5169 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5172 /* alloc a container for our list of ATA ports (buses) */
5173 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5174 /* alloc a container for our list of ATA ports (buses) */
5175 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5179 devres_add(dev, host);
5180 dev_set_drvdata(dev, host);
5182 spin_lock_init(&host->lock);
5184 host->n_ports = max_ports;
5186 /* allocate ports bound to this host */
5187 for (i = 0; i < max_ports; i++) {
5188 struct ata_port *ap;
5190 ap = ata_port_alloc(host);
5195 host->ports[i] = ap;
5198 devres_remove_group(dev, NULL);
5202 devres_release_group(dev, NULL);
5207 * ata_host_alloc_pinfo - alloc host and init with port_info array
5208 * @dev: generic device this host is associated with
5209 * @ppi: array of ATA port_info to initialize host with
5210 * @n_ports: number of ATA ports attached to this host
5212 * Allocate ATA host and initialize with info from @ppi. If NULL
5213 * terminated, @ppi may contain fewer entries than @n_ports. The
5214 * last entry will be used for the remaining ports.
5217 * Allocate ATA host on success, NULL on failure.
5220 * Inherited from calling layer (may sleep).
5222 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5223 const struct ata_port_info * const * ppi,
5226 const struct ata_port_info *pi;
5227 struct ata_host *host;
5230 host = ata_host_alloc(dev, n_ports);
5234 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5235 struct ata_port *ap = host->ports[i];
5240 ap->pio_mask = pi->pio_mask;
5241 ap->mwdma_mask = pi->mwdma_mask;
5242 ap->udma_mask = pi->udma_mask;
5243 ap->flags |= pi->flags;
5244 ap->link.flags |= pi->link_flags;
5245 ap->ops = pi->port_ops;
5247 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5248 host->ops = pi->port_ops;
5254 static void ata_host_stop(struct device *gendev, void *res)
5256 struct ata_host *host = dev_get_drvdata(gendev);
5259 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5261 for (i = 0; i < host->n_ports; i++) {
5262 struct ata_port *ap = host->ports[i];
5264 if (ap->ops->port_stop)
5265 ap->ops->port_stop(ap);
5268 if (host->ops->host_stop)
5269 host->ops->host_stop(host);
5273 * ata_finalize_port_ops - finalize ata_port_operations
5274 * @ops: ata_port_operations to finalize
5276 * An ata_port_operations can inherit from another ops and that
5277 * ops can again inherit from another. This can go on as many
5278 * times as necessary as long as there is no loop in the
5279 * inheritance chain.
5281 * Ops tables are finalized when the host is started. NULL or
5282 * unspecified entries are inherited from the closet ancestor
5283 * which has the method and the entry is populated with it.
5284 * After finalization, the ops table directly points to all the
5285 * methods and ->inherits is no longer necessary and cleared.
5287 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5292 static void ata_finalize_port_ops(struct ata_port_operations *ops)
5294 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
5295 const struct ata_port_operations *cur;
5296 void **begin = (void **)ops;
5297 void **end = (void **)&ops->inherits;
5300 if (!ops || !ops->inherits)
5305 for (cur = ops->inherits; cur; cur = cur->inherits) {
5306 void **inherit = (void **)cur;
5308 for (pp = begin; pp < end; pp++, inherit++)
5313 for (pp = begin; pp < end; pp++)
5317 ops->inherits = NULL;
5323 * ata_host_start - start and freeze ports of an ATA host
5324 * @host: ATA host to start ports for
5326 * Start and then freeze ports of @host. Started status is
5327 * recorded in host->flags, so this function can be called
5328 * multiple times. Ports are guaranteed to get started only
5329 * once. If host->ops isn't initialized yet, its set to the
5330 * first non-dummy port ops.
5333 * Inherited from calling layer (may sleep).
5336 * 0 if all ports are started successfully, -errno otherwise.
5338 int ata_host_start(struct ata_host *host)
5341 void *start_dr = NULL;
5344 if (host->flags & ATA_HOST_STARTED)
5347 ata_finalize_port_ops(host->ops);
5349 for (i = 0; i < host->n_ports; i++) {
5350 struct ata_port *ap = host->ports[i];
5352 ata_finalize_port_ops(ap->ops);
5354 if (!host->ops && !ata_port_is_dummy(ap))
5355 host->ops = ap->ops;
5357 if (ap->ops->port_stop)
5361 if (host->ops->host_stop)
5365 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5370 for (i = 0; i < host->n_ports; i++) {
5371 struct ata_port *ap = host->ports[i];
5373 if (ap->ops->port_start) {
5374 rc = ap->ops->port_start(ap);
5377 dev_printk(KERN_ERR, host->dev,
5378 "failed to start port %d "
5379 "(errno=%d)\n", i, rc);
5383 ata_eh_freeze_port(ap);
5387 devres_add(host->dev, start_dr);
5388 host->flags |= ATA_HOST_STARTED;
5393 struct ata_port *ap = host->ports[i];
5395 if (ap->ops->port_stop)
5396 ap->ops->port_stop(ap);
5398 devres_free(start_dr);
5403 * ata_sas_host_init - Initialize a host struct
5404 * @host: host to initialize
5405 * @dev: device host is attached to
5406 * @flags: host flags
5410 * PCI/etc. bus probe sem.
5413 /* KILLME - the only user left is ipr */
5414 void ata_host_init(struct ata_host *host, struct device *dev,
5415 unsigned long flags, struct ata_port_operations *ops)
5417 spin_lock_init(&host->lock);
5419 host->flags = flags;
5424 * ata_host_register - register initialized ATA host
5425 * @host: ATA host to register
5426 * @sht: template for SCSI host
5428 * Register initialized ATA host. @host is allocated using
5429 * ata_host_alloc() and fully initialized by LLD. This function
5430 * starts ports, registers @host with ATA and SCSI layers and
5431 * probe registered devices.
5434 * Inherited from calling layer (may sleep).
5437 * 0 on success, -errno otherwise.
5439 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5443 /* host must have been started */
5444 if (!(host->flags & ATA_HOST_STARTED)) {
5445 dev_printk(KERN_ERR, host->dev,
5446 "BUG: trying to register unstarted host\n");
5451 /* Blow away unused ports. This happens when LLD can't
5452 * determine the exact number of ports to allocate at
5455 for (i = host->n_ports; host->ports[i]; i++)
5456 kfree(host->ports[i]);
5458 /* give ports names and add SCSI hosts */
5459 for (i = 0; i < host->n_ports; i++)
5460 host->ports[i]->print_id = ata_print_id++;
5462 rc = ata_scsi_add_hosts(host, sht);
5466 /* associate with ACPI nodes */
5467 ata_acpi_associate(host);
5469 /* set cable, sata_spd_limit and report */
5470 for (i = 0; i < host->n_ports; i++) {
5471 struct ata_port *ap = host->ports[i];
5472 unsigned long xfer_mask;
5474 /* set SATA cable type if still unset */
5475 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5476 ap->cbl = ATA_CBL_SATA;
5478 /* init sata_spd_limit to the current value */
5479 sata_link_init_spd(&ap->link);
5481 /* print per-port info to dmesg */
5482 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5485 if (!ata_port_is_dummy(ap)) {
5486 ata_port_printk(ap, KERN_INFO,
5487 "%cATA max %s %s\n",
5488 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5489 ata_mode_string(xfer_mask),
5490 ap->link.eh_info.desc);
5491 ata_ehi_clear_desc(&ap->link.eh_info);
5493 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5496 /* perform each probe synchronously */
5497 DPRINTK("probe begin\n");
5498 for (i = 0; i < host->n_ports; i++) {
5499 struct ata_port *ap = host->ports[i];
5502 if (ap->ops->error_handler) {
5503 struct ata_eh_info *ehi = &ap->link.eh_info;
5504 unsigned long flags;
5508 /* kick EH for boot probing */
5509 spin_lock_irqsave(ap->lock, flags);
5511 ehi->probe_mask |= ATA_ALL_DEVICES;
5512 ehi->action |= ATA_EH_RESET;
5513 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5515 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5516 ap->pflags |= ATA_PFLAG_LOADING;
5517 ata_port_schedule_eh(ap);
5519 spin_unlock_irqrestore(ap->lock, flags);
5521 /* wait for EH to finish */
5522 ata_port_wait_eh(ap);
5524 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5525 rc = ata_bus_probe(ap);
5526 DPRINTK("ata%u: bus probe end\n", ap->print_id);
5529 /* FIXME: do something useful here?
5530 * Current libata behavior will
5531 * tear down everything when
5532 * the module is removed
5533 * or the h/w is unplugged.
5539 /* probes are done, now scan each port's disk(s) */
5540 DPRINTK("host probe begin\n");
5541 for (i = 0; i < host->n_ports; i++) {
5542 struct ata_port *ap = host->ports[i];
5544 ata_scsi_scan_host(ap, 1);
5545 ata_lpm_schedule(ap, ap->pm_policy);
5552 * ata_host_activate - start host, request IRQ and register it
5553 * @host: target ATA host
5554 * @irq: IRQ to request
5555 * @irq_handler: irq_handler used when requesting IRQ
5556 * @irq_flags: irq_flags used when requesting IRQ
5557 * @sht: scsi_host_template to use when registering the host
5559 * After allocating an ATA host and initializing it, most libata
5560 * LLDs perform three steps to activate the host - start host,
5561 * request IRQ and register it. This helper takes necessasry
5562 * arguments and performs the three steps in one go.
5564 * An invalid IRQ skips the IRQ registration and expects the host to
5565 * have set polling mode on the port. In this case, @irq_handler
5569 * Inherited from calling layer (may sleep).
5572 * 0 on success, -errno otherwise.
5574 int ata_host_activate(struct ata_host *host, int irq,
5575 irq_handler_t irq_handler, unsigned long irq_flags,
5576 struct scsi_host_template *sht)
5580 rc = ata_host_start(host);
5584 /* Special case for polling mode */
5586 WARN_ON(irq_handler);
5587 return ata_host_register(host, sht);
5590 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5591 dev_driver_string(host->dev), host);
5595 for (i = 0; i < host->n_ports; i++)
5596 ata_port_desc(host->ports[i], "irq %d", irq);
5598 rc = ata_host_register(host, sht);
5599 /* if failed, just free the IRQ and leave ports alone */
5601 devm_free_irq(host->dev, irq, host);
5607 * ata_port_detach - Detach ATA port in prepration of device removal
5608 * @ap: ATA port to be detached
5610 * Detach all ATA devices and the associated SCSI devices of @ap;
5611 * then, remove the associated SCSI host. @ap is guaranteed to
5612 * be quiescent on return from this function.
5615 * Kernel thread context (may sleep).
5617 static void ata_port_detach(struct ata_port *ap)
5619 unsigned long flags;
5620 struct ata_link *link;
5621 struct ata_device *dev;
5623 if (!ap->ops->error_handler)
5626 /* tell EH we're leaving & flush EH */
5627 spin_lock_irqsave(ap->lock, flags);
5628 ap->pflags |= ATA_PFLAG_UNLOADING;
5629 spin_unlock_irqrestore(ap->lock, flags);
5631 ata_port_wait_eh(ap);
5633 /* EH is now guaranteed to see UNLOADING - EH context belongs
5634 * to us. Disable all existing devices.
5636 ata_port_for_each_link(link, ap) {
5637 ata_link_for_each_dev(dev, link)
5638 ata_dev_disable(dev);
5641 /* Final freeze & EH. All in-flight commands are aborted. EH
5642 * will be skipped and retrials will be terminated with bad
5645 spin_lock_irqsave(ap->lock, flags);
5646 ata_port_freeze(ap); /* won't be thawed */
5647 spin_unlock_irqrestore(ap->lock, flags);
5649 ata_port_wait_eh(ap);
5650 cancel_rearming_delayed_work(&ap->hotplug_task);
5653 /* remove the associated SCSI host */
5654 scsi_remove_host(ap->scsi_host);
5658 * ata_host_detach - Detach all ports of an ATA host
5659 * @host: Host to detach
5661 * Detach all ports of @host.
5664 * Kernel thread context (may sleep).
5666 void ata_host_detach(struct ata_host *host)
5670 for (i = 0; i < host->n_ports; i++)
5671 ata_port_detach(host->ports[i]);
5673 /* the host is dead now, dissociate ACPI */
5674 ata_acpi_dissociate(host);
5680 * ata_pci_remove_one - PCI layer callback for device removal
5681 * @pdev: PCI device that was removed
5683 * PCI layer indicates to libata via this hook that hot-unplug or
5684 * module unload event has occurred. Detach all ports. Resource
5685 * release is handled via devres.
5688 * Inherited from PCI layer (may sleep).
5690 void ata_pci_remove_one(struct pci_dev *pdev)
5692 struct device *dev = &pdev->dev;
5693 struct ata_host *host = dev_get_drvdata(dev);
5695 ata_host_detach(host);
5698 /* move to PCI subsystem */
5699 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5701 unsigned long tmp = 0;
5703 switch (bits->width) {
5706 pci_read_config_byte(pdev, bits->reg, &tmp8);
5712 pci_read_config_word(pdev, bits->reg, &tmp16);
5718 pci_read_config_dword(pdev, bits->reg, &tmp32);
5729 return (tmp == bits->val) ? 1 : 0;
5733 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
5735 pci_save_state(pdev);
5736 pci_disable_device(pdev);
5738 if (mesg.event & PM_EVENT_SLEEP)
5739 pci_set_power_state(pdev, PCI_D3hot);
5742 int ata_pci_device_do_resume(struct pci_dev *pdev)
5746 pci_set_power_state(pdev, PCI_D0);
5747 pci_restore_state(pdev);
5749 rc = pcim_enable_device(pdev);
5751 dev_printk(KERN_ERR, &pdev->dev,
5752 "failed to enable device after resume (%d)\n", rc);
5756 pci_set_master(pdev);
5760 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
5762 struct ata_host *host = dev_get_drvdata(&pdev->dev);
5765 rc = ata_host_suspend(host, mesg);
5769 ata_pci_device_do_suspend(pdev, mesg);
5774 int ata_pci_device_resume(struct pci_dev *pdev)
5776 struct ata_host *host = dev_get_drvdata(&pdev->dev);
5779 rc = ata_pci_device_do_resume(pdev);
5781 ata_host_resume(host);
5784 #endif /* CONFIG_PM */
5786 #endif /* CONFIG_PCI */
5788 static int __init ata_parse_force_one(char **cur,
5789 struct ata_force_ent *force_ent,
5790 const char **reason)
5792 /* FIXME: Currently, there's no way to tag init const data and
5793 * using __initdata causes build failure on some versions of
5794 * gcc. Once __initdataconst is implemented, add const to the
5795 * following structure.
5797 static struct ata_force_param force_tbl[] __initdata = {
5798 { "40c", .cbl = ATA_CBL_PATA40 },
5799 { "80c", .cbl = ATA_CBL_PATA80 },
5800 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
5801 { "unk", .cbl = ATA_CBL_PATA_UNK },
5802 { "ign", .cbl = ATA_CBL_PATA_IGN },
5803 { "sata", .cbl = ATA_CBL_SATA },
5804 { "1.5Gbps", .spd_limit = 1 },
5805 { "3.0Gbps", .spd_limit = 2 },
5806 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
5807 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
5808 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
5809 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
5810 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
5811 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
5812 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
5813 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
5814 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
5815 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
5816 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
5817 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
5818 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
5819 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
5820 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5821 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5822 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5823 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5824 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5825 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5826 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5827 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5828 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5829 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5830 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5831 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5832 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5833 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5834 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5835 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5836 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5837 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5838 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5839 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5840 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5841 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
5843 char *start = *cur, *p = *cur;
5844 char *id, *val, *endp;
5845 const struct ata_force_param *match_fp = NULL;
5846 int nr_matches = 0, i;
5848 /* find where this param ends and update *cur */
5849 while (*p != '\0' && *p != ',')
5860 p = strchr(start, ':');
5862 val = strstrip(start);
5867 id = strstrip(start);
5868 val = strstrip(p + 1);
5871 p = strchr(id, '.');
5874 force_ent->device = simple_strtoul(p, &endp, 10);
5875 if (p == endp || *endp != '\0') {
5876 *reason = "invalid device";
5881 force_ent->port = simple_strtoul(id, &endp, 10);
5882 if (p == endp || *endp != '\0') {
5883 *reason = "invalid port/link";
5888 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
5889 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
5890 const struct ata_force_param *fp = &force_tbl[i];
5892 if (strncasecmp(val, fp->name, strlen(val)))
5898 if (strcasecmp(val, fp->name) == 0) {
5905 *reason = "unknown value";
5908 if (nr_matches > 1) {
5909 *reason = "ambigious value";
5913 force_ent->param = *match_fp;
5918 static void __init ata_parse_force_param(void)
5920 int idx = 0, size = 1;
5921 int last_port = -1, last_device = -1;
5922 char *p, *cur, *next;
5924 /* calculate maximum number of params and allocate force_tbl */
5925 for (p = ata_force_param_buf; *p; p++)
5929 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
5930 if (!ata_force_tbl) {
5931 printk(KERN_WARNING "ata: failed to extend force table, "
5932 "libata.force ignored\n");
5936 /* parse and populate the table */
5937 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
5938 const char *reason = "";
5939 struct ata_force_ent te = { .port = -1, .device = -1 };
5942 if (ata_parse_force_one(&next, &te, &reason)) {
5943 printk(KERN_WARNING "ata: failed to parse force "
5944 "parameter \"%s\" (%s)\n",
5949 if (te.port == -1) {
5950 te.port = last_port;
5951 te.device = last_device;
5954 ata_force_tbl[idx++] = te;
5956 last_port = te.port;
5957 last_device = te.device;
5960 ata_force_tbl_size = idx;
5963 static int __init ata_init(void)
5965 ata_probe_timeout *= HZ;
5967 ata_parse_force_param();
5969 ata_wq = create_workqueue("ata");
5973 ata_aux_wq = create_singlethread_workqueue("ata_aux");
5975 destroy_workqueue(ata_wq);
5979 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5983 static void __exit ata_exit(void)
5985 kfree(ata_force_tbl);
5986 destroy_workqueue(ata_wq);
5987 destroy_workqueue(ata_aux_wq);
5990 subsys_initcall(ata_init);
5991 module_exit(ata_exit);
5993 static unsigned long ratelimit_time;
5994 static DEFINE_SPINLOCK(ata_ratelimit_lock);
5996 int ata_ratelimit(void)
5999 unsigned long flags;
6001 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6003 if (time_after(jiffies, ratelimit_time)) {
6005 ratelimit_time = jiffies + (HZ/5);
6009 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6015 * ata_wait_register - wait until register value changes
6016 * @reg: IO-mapped register
6017 * @mask: Mask to apply to read register value
6018 * @val: Wait condition
6019 * @interval_msec: polling interval in milliseconds
6020 * @timeout_msec: timeout in milliseconds
6022 * Waiting for some bits of register to change is a common
6023 * operation for ATA controllers. This function reads 32bit LE
6024 * IO-mapped register @reg and tests for the following condition.
6026 * (*@reg & mask) != val
6028 * If the condition is met, it returns; otherwise, the process is
6029 * repeated after @interval_msec until timeout.
6032 * Kernel thread context (may sleep)
6035 * The final register value.
6037 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6038 unsigned long interval_msec,
6039 unsigned long timeout_msec)
6041 unsigned long timeout;
6044 tmp = ioread32(reg);
6046 /* Calculate timeout _after_ the first read to make sure
6047 * preceding writes reach the controller before starting to
6048 * eat away the timeout.
6050 timeout = jiffies + (timeout_msec * HZ) / 1000;
6052 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6053 msleep(interval_msec);
6054 tmp = ioread32(reg);
6063 static void ata_dummy_noret(struct ata_port *ap) { }
6064 static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6065 static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6067 static u8 ata_dummy_check_status(struct ata_port *ap)
6072 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6074 return AC_ERR_SYSTEM;
6077 struct ata_port_operations ata_dummy_port_ops = {
6078 .check_status = ata_dummy_check_status,
6079 .check_altstatus = ata_dummy_check_status,
6080 .dev_select = ata_noop_dev_select,
6081 .qc_prep = ata_noop_qc_prep,
6082 .qc_issue = ata_dummy_qc_issue,
6083 .freeze = ata_dummy_noret,
6084 .thaw = ata_dummy_noret,
6085 .error_handler = ata_dummy_noret,
6086 .post_internal_cmd = ata_dummy_qc_noret,
6087 .irq_clear = ata_dummy_noret,
6088 .port_start = ata_dummy_ret0,
6089 .port_stop = ata_dummy_noret,
6092 const struct ata_port_info ata_dummy_port_info = {
6093 .port_ops = &ata_dummy_port_ops,
6097 * libata is essentially a library of internal helper functions for
6098 * low-level ATA host controller drivers. As such, the API/ABI is
6099 * likely to change as new drivers are added and updated.
6100 * Do not depend on ABI/API stability.
6102 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6103 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6104 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6105 EXPORT_SYMBOL_GPL(ata_base_port_ops);
6106 EXPORT_SYMBOL_GPL(sata_port_ops);
6107 EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
6108 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6109 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6110 EXPORT_SYMBOL_GPL(ata_std_bios_param);
6111 EXPORT_SYMBOL_GPL(ata_host_init);
6112 EXPORT_SYMBOL_GPL(ata_host_alloc);
6113 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6114 EXPORT_SYMBOL_GPL(ata_host_start);
6115 EXPORT_SYMBOL_GPL(ata_host_register);
6116 EXPORT_SYMBOL_GPL(ata_host_activate);
6117 EXPORT_SYMBOL_GPL(ata_host_detach);
6118 EXPORT_SYMBOL_GPL(ata_sg_init);
6119 EXPORT_SYMBOL_GPL(ata_qc_complete);
6120 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6121 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
6122 EXPORT_SYMBOL_GPL(sata_print_link_status);
6123 EXPORT_SYMBOL_GPL(atapi_cmd_type);
6124 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6125 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6126 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6127 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6128 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6129 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6130 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6131 EXPORT_SYMBOL_GPL(ata_mode_string);
6132 EXPORT_SYMBOL_GPL(ata_id_xfermask);
6133 EXPORT_SYMBOL_GPL(ata_port_start);
6134 EXPORT_SYMBOL_GPL(ata_do_set_mode);
6135 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6136 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6137 EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
6138 EXPORT_SYMBOL_GPL(ata_port_probe);
6139 EXPORT_SYMBOL_GPL(ata_dev_disable);
6140 EXPORT_SYMBOL_GPL(sata_set_spd);
6141 EXPORT_SYMBOL_GPL(sata_link_debounce);
6142 EXPORT_SYMBOL_GPL(sata_link_resume);
6143 EXPORT_SYMBOL_GPL(sata_link_hardreset);
6144 EXPORT_SYMBOL_GPL(ata_dev_classify);
6145 EXPORT_SYMBOL_GPL(ata_dev_pair);
6146 EXPORT_SYMBOL_GPL(ata_port_disable);
6147 EXPORT_SYMBOL_GPL(ata_ratelimit);
6148 EXPORT_SYMBOL_GPL(ata_wait_register);
6149 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6150 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6151 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6152 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6153 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6154 EXPORT_SYMBOL_GPL(sata_scr_valid);
6155 EXPORT_SYMBOL_GPL(sata_scr_read);
6156 EXPORT_SYMBOL_GPL(sata_scr_write);
6157 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6158 EXPORT_SYMBOL_GPL(ata_link_online);
6159 EXPORT_SYMBOL_GPL(ata_link_offline);
6161 EXPORT_SYMBOL_GPL(ata_host_suspend);
6162 EXPORT_SYMBOL_GPL(ata_host_resume);
6163 #endif /* CONFIG_PM */
6164 EXPORT_SYMBOL_GPL(ata_id_string);
6165 EXPORT_SYMBOL_GPL(ata_id_c_string);
6166 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6168 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
6169 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6170 EXPORT_SYMBOL_GPL(ata_timing_compute);
6171 EXPORT_SYMBOL_GPL(ata_timing_merge);
6172 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6175 EXPORT_SYMBOL_GPL(pci_test_config_bits);
6176 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6178 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6179 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6180 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6181 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6182 #endif /* CONFIG_PM */
6183 #endif /* CONFIG_PCI */
6185 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
6186 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
6187 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
6188 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
6189 EXPORT_SYMBOL_GPL(sata_pmp_error_handler);
6191 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6192 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6193 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6194 EXPORT_SYMBOL_GPL(ata_port_desc);
6196 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6197 #endif /* CONFIG_PCI */
6198 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6199 EXPORT_SYMBOL_GPL(ata_link_abort);
6200 EXPORT_SYMBOL_GPL(ata_port_abort);
6201 EXPORT_SYMBOL_GPL(ata_port_freeze);
6202 EXPORT_SYMBOL_GPL(sata_async_notification);
6203 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6204 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6205 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6206 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
6207 EXPORT_SYMBOL_GPL(ata_do_eh);
6208 EXPORT_SYMBOL_GPL(ata_std_error_handler);
6210 EXPORT_SYMBOL_GPL(ata_cable_40wire);
6211 EXPORT_SYMBOL_GPL(ata_cable_80wire);
6212 EXPORT_SYMBOL_GPL(ata_cable_unknown);
6213 EXPORT_SYMBOL_GPL(ata_cable_ignore);
6214 EXPORT_SYMBOL_GPL(ata_cable_sata);