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/scatterlist.h>
59 #include <linux/async.h>
60 #include <linux/log2.h>
61 #include <linux/slab.h>
62 #include <scsi/scsi.h>
63 #include <scsi/scsi_cmnd.h>
64 #include <scsi/scsi_host.h>
65 #include <linux/libata.h>
66 #include <asm/byteorder.h>
67 #include <linux/cdrom.h>
68 #include <linux/ratelimit.h>
69 #include <linux/pm_runtime.h>
72 #include "libata-transport.h"
74 /* debounce timing parameters in msecs { interval, duration, timeout } */
75 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
76 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
77 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
79 const struct ata_port_operations ata_base_port_ops = {
80 .prereset = ata_std_prereset,
81 .postreset = ata_std_postreset,
82 .error_handler = ata_std_error_handler,
85 const struct ata_port_operations sata_port_ops = {
86 .inherits = &ata_base_port_ops,
88 .qc_defer = ata_std_qc_defer,
89 .hardreset = sata_std_hardreset,
92 static unsigned int ata_dev_init_params(struct ata_device *dev,
93 u16 heads, u16 sectors);
94 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
95 static void ata_dev_xfermask(struct ata_device *dev);
96 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
98 unsigned int ata_print_id = 1;
100 struct ata_force_param {
104 unsigned long xfer_mask;
105 unsigned int horkage_on;
106 unsigned int horkage_off;
110 struct ata_force_ent {
113 struct ata_force_param param;
116 static struct ata_force_ent *ata_force_tbl;
117 static int ata_force_tbl_size;
119 static char ata_force_param_buf[PAGE_SIZE] __initdata;
120 /* param_buf is thrown away after initialization, disallow read */
121 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
122 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
124 static int atapi_enabled = 1;
125 module_param(atapi_enabled, int, 0444);
126 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
128 static int atapi_dmadir = 0;
129 module_param(atapi_dmadir, int, 0444);
130 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
132 int atapi_passthru16 = 1;
133 module_param(atapi_passthru16, int, 0444);
134 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
137 module_param_named(fua, libata_fua, int, 0444);
138 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
140 static int ata_ignore_hpa;
141 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
142 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
144 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
145 module_param_named(dma, libata_dma_mask, int, 0444);
146 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
148 static int ata_probe_timeout;
149 module_param(ata_probe_timeout, int, 0444);
150 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
152 int libata_noacpi = 0;
153 module_param_named(noacpi, libata_noacpi, int, 0444);
154 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
156 int libata_allow_tpm = 0;
157 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
158 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
161 module_param(atapi_an, int, 0444);
162 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
164 MODULE_AUTHOR("Jeff Garzik");
165 MODULE_DESCRIPTION("Library module for ATA devices");
166 MODULE_LICENSE("GPL");
167 MODULE_VERSION(DRV_VERSION);
170 static bool ata_sstatus_online(u32 sstatus)
172 return (sstatus & 0xf) == 0x3;
176 * ata_link_next - link iteration helper
177 * @link: the previous link, NULL to start
178 * @ap: ATA port containing links to iterate
179 * @mode: iteration mode, one of ATA_LITER_*
182 * Host lock or EH context.
185 * Pointer to the next link.
187 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
193 /* NULL link indicates start of iteration */
197 case ATA_LITER_PMP_FIRST:
198 if (sata_pmp_attached(ap))
201 case ATA_LITER_HOST_FIRST:
205 /* we just iterated over the host link, what's next? */
206 if (link == &ap->link)
208 case ATA_LITER_HOST_FIRST:
209 if (sata_pmp_attached(ap))
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
214 return ap->slave_link;
220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
224 /* we were over a PMP link */
225 if (++link < ap->pmp_link + ap->nr_pmp_links)
228 if (mode == ATA_LITER_PMP_FIRST)
235 * ata_dev_next - device iteration helper
236 * @dev: the previous device, NULL to start
237 * @link: ATA link containing devices to iterate
238 * @mode: iteration mode, one of ATA_DITER_*
241 * Host lock or EH context.
244 * Pointer to the next device.
246 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
247 enum ata_dev_iter_mode mode)
249 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
250 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
252 /* NULL dev indicates start of iteration */
255 case ATA_DITER_ENABLED:
259 case ATA_DITER_ENABLED_REVERSE:
260 case ATA_DITER_ALL_REVERSE:
261 dev = link->device + ata_link_max_devices(link) - 1;
266 /* move to the next one */
268 case ATA_DITER_ENABLED:
270 if (++dev < link->device + ata_link_max_devices(link))
273 case ATA_DITER_ENABLED_REVERSE:
274 case ATA_DITER_ALL_REVERSE:
275 if (--dev >= link->device)
281 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
282 !ata_dev_enabled(dev))
288 * ata_dev_phys_link - find physical link for a device
289 * @dev: ATA device to look up physical link for
291 * Look up physical link which @dev is attached to. Note that
292 * this is different from @dev->link only when @dev is on slave
293 * link. For all other cases, it's the same as @dev->link.
299 * Pointer to the found physical link.
301 struct ata_link *ata_dev_phys_link(struct ata_device *dev)
303 struct ata_port *ap = dev->link->ap;
309 return ap->slave_link;
313 * ata_force_cbl - force cable type according to libata.force
314 * @ap: ATA port of interest
316 * Force cable type according to libata.force and whine about it.
317 * The last entry which has matching port number is used, so it
318 * can be specified as part of device force parameters. For
319 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
325 void ata_force_cbl(struct ata_port *ap)
329 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
330 const struct ata_force_ent *fe = &ata_force_tbl[i];
332 if (fe->port != -1 && fe->port != ap->print_id)
335 if (fe->param.cbl == ATA_CBL_NONE)
338 ap->cbl = fe->param.cbl;
339 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
345 * ata_force_link_limits - force link limits according to libata.force
346 * @link: ATA link of interest
348 * Force link flags and SATA spd limit according to libata.force
349 * and whine about it. When only the port part is specified
350 * (e.g. 1:), the limit applies to all links connected to both
351 * the host link and all fan-out ports connected via PMP. If the
352 * device part is specified as 0 (e.g. 1.00:), it specifies the
353 * first fan-out link not the host link. Device number 15 always
354 * points to the host link whether PMP is attached or not. If the
355 * controller has slave link, device number 16 points to it.
360 static void ata_force_link_limits(struct ata_link *link)
362 bool did_spd = false;
363 int linkno = link->pmp;
366 if (ata_is_host_link(link))
369 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
370 const struct ata_force_ent *fe = &ata_force_tbl[i];
372 if (fe->port != -1 && fe->port != link->ap->print_id)
375 if (fe->device != -1 && fe->device != linkno)
378 /* only honor the first spd limit */
379 if (!did_spd && fe->param.spd_limit) {
380 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
381 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
386 /* let lflags stack */
387 if (fe->param.lflags) {
388 link->flags |= fe->param.lflags;
389 ata_link_notice(link,
390 "FORCE: link flag 0x%x forced -> 0x%x\n",
391 fe->param.lflags, link->flags);
397 * ata_force_xfermask - force xfermask according to libata.force
398 * @dev: ATA device of interest
400 * Force xfer_mask according to libata.force and whine about it.
401 * For consistency with link selection, device number 15 selects
402 * the first device connected to the host link.
407 static void ata_force_xfermask(struct ata_device *dev)
409 int devno = dev->link->pmp + dev->devno;
410 int alt_devno = devno;
413 /* allow n.15/16 for devices attached to host port */
414 if (ata_is_host_link(dev->link))
417 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
418 const struct ata_force_ent *fe = &ata_force_tbl[i];
419 unsigned long pio_mask, mwdma_mask, udma_mask;
421 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
424 if (fe->device != -1 && fe->device != devno &&
425 fe->device != alt_devno)
428 if (!fe->param.xfer_mask)
431 ata_unpack_xfermask(fe->param.xfer_mask,
432 &pio_mask, &mwdma_mask, &udma_mask);
434 dev->udma_mask = udma_mask;
435 else if (mwdma_mask) {
437 dev->mwdma_mask = mwdma_mask;
441 dev->pio_mask = pio_mask;
444 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
451 * ata_force_horkage - force horkage according to libata.force
452 * @dev: ATA device of interest
454 * Force horkage according to libata.force and whine about it.
455 * For consistency with link selection, device number 15 selects
456 * the first device connected to the host link.
461 static void ata_force_horkage(struct ata_device *dev)
463 int devno = dev->link->pmp + dev->devno;
464 int alt_devno = devno;
467 /* allow n.15/16 for devices attached to host port */
468 if (ata_is_host_link(dev->link))
471 for (i = 0; i < ata_force_tbl_size; i++) {
472 const struct ata_force_ent *fe = &ata_force_tbl[i];
474 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
477 if (fe->device != -1 && fe->device != devno &&
478 fe->device != alt_devno)
481 if (!(~dev->horkage & fe->param.horkage_on) &&
482 !(dev->horkage & fe->param.horkage_off))
485 dev->horkage |= fe->param.horkage_on;
486 dev->horkage &= ~fe->param.horkage_off;
488 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
494 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
495 * @opcode: SCSI opcode
497 * Determine ATAPI command type from @opcode.
503 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
505 int atapi_cmd_type(u8 opcode)
514 case GPCMD_WRITE_AND_VERIFY_10:
518 case GPCMD_READ_CD_MSF:
519 return ATAPI_READ_CD;
523 if (atapi_passthru16)
524 return ATAPI_PASS_THRU;
532 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
533 * @tf: Taskfile to convert
534 * @pmp: Port multiplier port
535 * @is_cmd: This FIS is for command
536 * @fis: Buffer into which data will output
538 * Converts a standard ATA taskfile to a Serial ATA
539 * FIS structure (Register - Host to Device).
542 * Inherited from caller.
544 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
546 fis[0] = 0x27; /* Register - Host to Device FIS */
547 fis[1] = pmp & 0xf; /* Port multiplier number*/
549 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
551 fis[2] = tf->command;
552 fis[3] = tf->feature;
559 fis[8] = tf->hob_lbal;
560 fis[9] = tf->hob_lbam;
561 fis[10] = tf->hob_lbah;
562 fis[11] = tf->hob_feature;
565 fis[13] = tf->hob_nsect;
576 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
577 * @fis: Buffer from which data will be input
578 * @tf: Taskfile to output
580 * Converts a serial ATA FIS structure to a standard ATA taskfile.
583 * Inherited from caller.
586 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
588 tf->command = fis[2]; /* status */
589 tf->feature = fis[3]; /* error */
596 tf->hob_lbal = fis[8];
597 tf->hob_lbam = fis[9];
598 tf->hob_lbah = fis[10];
601 tf->hob_nsect = fis[13];
604 static const u8 ata_rw_cmds[] = {
608 ATA_CMD_READ_MULTI_EXT,
609 ATA_CMD_WRITE_MULTI_EXT,
613 ATA_CMD_WRITE_MULTI_FUA_EXT,
617 ATA_CMD_PIO_READ_EXT,
618 ATA_CMD_PIO_WRITE_EXT,
631 ATA_CMD_WRITE_FUA_EXT
635 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
636 * @tf: command to examine and configure
637 * @dev: device tf belongs to
639 * Examine the device configuration and tf->flags to calculate
640 * the proper read/write commands and protocol to use.
645 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
649 int index, fua, lba48, write;
651 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
652 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
653 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
655 if (dev->flags & ATA_DFLAG_PIO) {
656 tf->protocol = ATA_PROT_PIO;
657 index = dev->multi_count ? 0 : 8;
658 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
659 /* Unable to use DMA due to host limitation */
660 tf->protocol = ATA_PROT_PIO;
661 index = dev->multi_count ? 0 : 8;
663 tf->protocol = ATA_PROT_DMA;
667 cmd = ata_rw_cmds[index + fua + lba48 + write];
676 * ata_tf_read_block - Read block address from ATA taskfile
677 * @tf: ATA taskfile of interest
678 * @dev: ATA device @tf belongs to
683 * Read block address from @tf. This function can handle all
684 * three address formats - LBA, LBA48 and CHS. tf->protocol and
685 * flags select the address format to use.
688 * Block address read from @tf.
690 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
694 if (tf->flags & ATA_TFLAG_LBA) {
695 if (tf->flags & ATA_TFLAG_LBA48) {
696 block |= (u64)tf->hob_lbah << 40;
697 block |= (u64)tf->hob_lbam << 32;
698 block |= (u64)tf->hob_lbal << 24;
700 block |= (tf->device & 0xf) << 24;
702 block |= tf->lbah << 16;
703 block |= tf->lbam << 8;
708 cyl = tf->lbam | (tf->lbah << 8);
709 head = tf->device & 0xf;
714 "device reported invalid CHS sector 0\n");
715 sect = 1; /* oh well */
718 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
725 * ata_build_rw_tf - Build ATA taskfile for given read/write request
726 * @tf: Target ATA taskfile
727 * @dev: ATA device @tf belongs to
728 * @block: Block address
729 * @n_block: Number of blocks
730 * @tf_flags: RW/FUA etc...
736 * Build ATA taskfile @tf for read/write request described by
737 * @block, @n_block, @tf_flags and @tag on @dev.
741 * 0 on success, -ERANGE if the request is too large for @dev,
742 * -EINVAL if the request is invalid.
744 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
745 u64 block, u32 n_block, unsigned int tf_flags,
748 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
749 tf->flags |= tf_flags;
751 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
753 if (!lba_48_ok(block, n_block))
756 tf->protocol = ATA_PROT_NCQ;
757 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
759 if (tf->flags & ATA_TFLAG_WRITE)
760 tf->command = ATA_CMD_FPDMA_WRITE;
762 tf->command = ATA_CMD_FPDMA_READ;
764 tf->nsect = tag << 3;
765 tf->hob_feature = (n_block >> 8) & 0xff;
766 tf->feature = n_block & 0xff;
768 tf->hob_lbah = (block >> 40) & 0xff;
769 tf->hob_lbam = (block >> 32) & 0xff;
770 tf->hob_lbal = (block >> 24) & 0xff;
771 tf->lbah = (block >> 16) & 0xff;
772 tf->lbam = (block >> 8) & 0xff;
773 tf->lbal = block & 0xff;
776 if (tf->flags & ATA_TFLAG_FUA)
777 tf->device |= 1 << 7;
778 } else if (dev->flags & ATA_DFLAG_LBA) {
779 tf->flags |= ATA_TFLAG_LBA;
781 if (lba_28_ok(block, n_block)) {
783 tf->device |= (block >> 24) & 0xf;
784 } else if (lba_48_ok(block, n_block)) {
785 if (!(dev->flags & ATA_DFLAG_LBA48))
789 tf->flags |= ATA_TFLAG_LBA48;
791 tf->hob_nsect = (n_block >> 8) & 0xff;
793 tf->hob_lbah = (block >> 40) & 0xff;
794 tf->hob_lbam = (block >> 32) & 0xff;
795 tf->hob_lbal = (block >> 24) & 0xff;
797 /* request too large even for LBA48 */
800 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
803 tf->nsect = n_block & 0xff;
805 tf->lbah = (block >> 16) & 0xff;
806 tf->lbam = (block >> 8) & 0xff;
807 tf->lbal = block & 0xff;
809 tf->device |= ATA_LBA;
812 u32 sect, head, cyl, track;
814 /* The request -may- be too large for CHS addressing. */
815 if (!lba_28_ok(block, n_block))
818 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
821 /* Convert LBA to CHS */
822 track = (u32)block / dev->sectors;
823 cyl = track / dev->heads;
824 head = track % dev->heads;
825 sect = (u32)block % dev->sectors + 1;
827 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
828 (u32)block, track, cyl, head, sect);
830 /* Check whether the converted CHS can fit.
834 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
837 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
848 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
849 * @pio_mask: pio_mask
850 * @mwdma_mask: mwdma_mask
851 * @udma_mask: udma_mask
853 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
854 * unsigned int xfer_mask.
862 unsigned long ata_pack_xfermask(unsigned long pio_mask,
863 unsigned long mwdma_mask,
864 unsigned long udma_mask)
866 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
867 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
868 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
872 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
873 * @xfer_mask: xfer_mask to unpack
874 * @pio_mask: resulting pio_mask
875 * @mwdma_mask: resulting mwdma_mask
876 * @udma_mask: resulting udma_mask
878 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
879 * Any NULL distination masks will be ignored.
881 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
882 unsigned long *mwdma_mask, unsigned long *udma_mask)
885 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
887 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
889 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
892 static const struct ata_xfer_ent {
896 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
897 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
898 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
903 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
904 * @xfer_mask: xfer_mask of interest
906 * Return matching XFER_* value for @xfer_mask. Only the highest
907 * bit of @xfer_mask is considered.
913 * Matching XFER_* value, 0xff if no match found.
915 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
917 int highbit = fls(xfer_mask) - 1;
918 const struct ata_xfer_ent *ent;
920 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
921 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
922 return ent->base + highbit - ent->shift;
927 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
928 * @xfer_mode: XFER_* of interest
930 * Return matching xfer_mask for @xfer_mode.
936 * Matching xfer_mask, 0 if no match found.
938 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
940 const struct ata_xfer_ent *ent;
942 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
943 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
944 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
945 & ~((1 << ent->shift) - 1);
950 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
951 * @xfer_mode: XFER_* of interest
953 * Return matching xfer_shift for @xfer_mode.
959 * Matching xfer_shift, -1 if no match found.
961 int ata_xfer_mode2shift(unsigned long xfer_mode)
963 const struct ata_xfer_ent *ent;
965 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
966 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
972 * ata_mode_string - convert xfer_mask to string
973 * @xfer_mask: mask of bits supported; only highest bit counts.
975 * Determine string which represents the highest speed
976 * (highest bit in @modemask).
982 * Constant C string representing highest speed listed in
983 * @mode_mask, or the constant C string "<n/a>".
985 const char *ata_mode_string(unsigned long xfer_mask)
987 static const char * const xfer_mode_str[] = {
1011 highbit = fls(xfer_mask) - 1;
1012 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1013 return xfer_mode_str[highbit];
1017 const char *sata_spd_string(unsigned int spd)
1019 static const char * const spd_str[] = {
1025 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1027 return spd_str[spd - 1];
1031 * ata_dev_classify - determine device type based on ATA-spec signature
1032 * @tf: ATA taskfile register set for device to be identified
1034 * Determine from taskfile register contents whether a device is
1035 * ATA or ATAPI, as per "Signature and persistence" section
1036 * of ATA/PI spec (volume 1, sect 5.14).
1042 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1043 * %ATA_DEV_UNKNOWN the event of failure.
1045 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1047 /* Apple's open source Darwin code hints that some devices only
1048 * put a proper signature into the LBA mid/high registers,
1049 * So, we only check those. It's sufficient for uniqueness.
1051 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1052 * signatures for ATA and ATAPI devices attached on SerialATA,
1053 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1054 * spec has never mentioned about using different signatures
1055 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1056 * Multiplier specification began to use 0x69/0x96 to identify
1057 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1058 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1059 * 0x69/0x96 shortly and described them as reserved for
1062 * We follow the current spec and consider that 0x69/0x96
1063 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1064 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1065 * SEMB signature. This is worked around in
1066 * ata_dev_read_id().
1068 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1069 DPRINTK("found ATA device by sig\n");
1073 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1074 DPRINTK("found ATAPI device by sig\n");
1075 return ATA_DEV_ATAPI;
1078 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1079 DPRINTK("found PMP device by sig\n");
1083 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1084 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1085 return ATA_DEV_SEMB;
1088 DPRINTK("unknown device\n");
1089 return ATA_DEV_UNKNOWN;
1093 * ata_id_string - Convert IDENTIFY DEVICE page into string
1094 * @id: IDENTIFY DEVICE results we will examine
1095 * @s: string into which data is output
1096 * @ofs: offset into identify device page
1097 * @len: length of string to return. must be an even number.
1099 * The strings in the IDENTIFY DEVICE page are broken up into
1100 * 16-bit chunks. Run through the string, and output each
1101 * 8-bit chunk linearly, regardless of platform.
1107 void ata_id_string(const u16 *id, unsigned char *s,
1108 unsigned int ofs, unsigned int len)
1129 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1130 * @id: IDENTIFY DEVICE results we will examine
1131 * @s: string into which data is output
1132 * @ofs: offset into identify device page
1133 * @len: length of string to return. must be an odd number.
1135 * This function is identical to ata_id_string except that it
1136 * trims trailing spaces and terminates the resulting string with
1137 * null. @len must be actual maximum length (even number) + 1.
1142 void ata_id_c_string(const u16 *id, unsigned char *s,
1143 unsigned int ofs, unsigned int len)
1147 ata_id_string(id, s, ofs, len - 1);
1149 p = s + strnlen(s, len - 1);
1150 while (p > s && p[-1] == ' ')
1155 static u64 ata_id_n_sectors(const u16 *id)
1157 if (ata_id_has_lba(id)) {
1158 if (ata_id_has_lba48(id))
1159 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1161 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1163 if (ata_id_current_chs_valid(id))
1164 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1165 id[ATA_ID_CUR_SECTORS];
1167 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1172 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1176 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1177 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1178 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1179 sectors |= (tf->lbah & 0xff) << 16;
1180 sectors |= (tf->lbam & 0xff) << 8;
1181 sectors |= (tf->lbal & 0xff);
1186 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1190 sectors |= (tf->device & 0x0f) << 24;
1191 sectors |= (tf->lbah & 0xff) << 16;
1192 sectors |= (tf->lbam & 0xff) << 8;
1193 sectors |= (tf->lbal & 0xff);
1199 * ata_read_native_max_address - Read native max address
1200 * @dev: target device
1201 * @max_sectors: out parameter for the result native max address
1203 * Perform an LBA48 or LBA28 native size query upon the device in
1207 * 0 on success, -EACCES if command is aborted by the drive.
1208 * -EIO on other errors.
1210 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1212 unsigned int err_mask;
1213 struct ata_taskfile tf;
1214 int lba48 = ata_id_has_lba48(dev->id);
1216 ata_tf_init(dev, &tf);
1218 /* always clear all address registers */
1219 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1222 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1223 tf.flags |= ATA_TFLAG_LBA48;
1225 tf.command = ATA_CMD_READ_NATIVE_MAX;
1227 tf.protocol |= ATA_PROT_NODATA;
1228 tf.device |= ATA_LBA;
1230 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1233 "failed to read native max address (err_mask=0x%x)\n",
1235 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1241 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1243 *max_sectors = ata_tf_to_lba(&tf) + 1;
1244 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1250 * ata_set_max_sectors - Set max sectors
1251 * @dev: target device
1252 * @new_sectors: new max sectors value to set for the device
1254 * Set max sectors of @dev to @new_sectors.
1257 * 0 on success, -EACCES if command is aborted or denied (due to
1258 * previous non-volatile SET_MAX) by the drive. -EIO on other
1261 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1263 unsigned int err_mask;
1264 struct ata_taskfile tf;
1265 int lba48 = ata_id_has_lba48(dev->id);
1269 ata_tf_init(dev, &tf);
1271 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1274 tf.command = ATA_CMD_SET_MAX_EXT;
1275 tf.flags |= ATA_TFLAG_LBA48;
1277 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1278 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1279 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1281 tf.command = ATA_CMD_SET_MAX;
1283 tf.device |= (new_sectors >> 24) & 0xf;
1286 tf.protocol |= ATA_PROT_NODATA;
1287 tf.device |= ATA_LBA;
1289 tf.lbal = (new_sectors >> 0) & 0xff;
1290 tf.lbam = (new_sectors >> 8) & 0xff;
1291 tf.lbah = (new_sectors >> 16) & 0xff;
1293 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1296 "failed to set max address (err_mask=0x%x)\n",
1298 if (err_mask == AC_ERR_DEV &&
1299 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1308 * ata_hpa_resize - Resize a device with an HPA set
1309 * @dev: Device to resize
1311 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1312 * it if required to the full size of the media. The caller must check
1313 * the drive has the HPA feature set enabled.
1316 * 0 on success, -errno on failure.
1318 static int ata_hpa_resize(struct ata_device *dev)
1320 struct ata_eh_context *ehc = &dev->link->eh_context;
1321 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1322 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1323 u64 sectors = ata_id_n_sectors(dev->id);
1327 /* do we need to do it? */
1328 if (dev->class != ATA_DEV_ATA ||
1329 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1330 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1333 /* read native max address */
1334 rc = ata_read_native_max_address(dev, &native_sectors);
1336 /* If device aborted the command or HPA isn't going to
1337 * be unlocked, skip HPA resizing.
1339 if (rc == -EACCES || !unlock_hpa) {
1341 "HPA support seems broken, skipping HPA handling\n");
1342 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1344 /* we can continue if device aborted the command */
1351 dev->n_native_sectors = native_sectors;
1353 /* nothing to do? */
1354 if (native_sectors <= sectors || !unlock_hpa) {
1355 if (!print_info || native_sectors == sectors)
1358 if (native_sectors > sectors)
1360 "HPA detected: current %llu, native %llu\n",
1361 (unsigned long long)sectors,
1362 (unsigned long long)native_sectors);
1363 else if (native_sectors < sectors)
1365 "native sectors (%llu) is smaller than sectors (%llu)\n",
1366 (unsigned long long)native_sectors,
1367 (unsigned long long)sectors);
1371 /* let's unlock HPA */
1372 rc = ata_set_max_sectors(dev, native_sectors);
1373 if (rc == -EACCES) {
1374 /* if device aborted the command, skip HPA resizing */
1376 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1377 (unsigned long long)sectors,
1378 (unsigned long long)native_sectors);
1379 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1384 /* re-read IDENTIFY data */
1385 rc = ata_dev_reread_id(dev, 0);
1388 "failed to re-read IDENTIFY data after HPA resizing\n");
1393 u64 new_sectors = ata_id_n_sectors(dev->id);
1395 "HPA unlocked: %llu -> %llu, native %llu\n",
1396 (unsigned long long)sectors,
1397 (unsigned long long)new_sectors,
1398 (unsigned long long)native_sectors);
1405 * ata_dump_id - IDENTIFY DEVICE info debugging output
1406 * @id: IDENTIFY DEVICE page to dump
1408 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1415 static inline void ata_dump_id(const u16 *id)
1417 DPRINTK("49==0x%04x "
1427 DPRINTK("80==0x%04x "
1437 DPRINTK("88==0x%04x "
1444 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1445 * @id: IDENTIFY data to compute xfer mask from
1447 * Compute the xfermask for this device. This is not as trivial
1448 * as it seems if we must consider early devices correctly.
1450 * FIXME: pre IDE drive timing (do we care ?).
1458 unsigned long ata_id_xfermask(const u16 *id)
1460 unsigned long pio_mask, mwdma_mask, udma_mask;
1462 /* Usual case. Word 53 indicates word 64 is valid */
1463 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1464 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1468 /* If word 64 isn't valid then Word 51 high byte holds
1469 * the PIO timing number for the maximum. Turn it into
1472 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1473 if (mode < 5) /* Valid PIO range */
1474 pio_mask = (2 << mode) - 1;
1478 /* But wait.. there's more. Design your standards by
1479 * committee and you too can get a free iordy field to
1480 * process. However its the speeds not the modes that
1481 * are supported... Note drivers using the timing API
1482 * will get this right anyway
1486 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1488 if (ata_id_is_cfa(id)) {
1490 * Process compact flash extended modes
1492 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1493 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1496 pio_mask |= (1 << 5);
1498 pio_mask |= (1 << 6);
1500 mwdma_mask |= (1 << 3);
1502 mwdma_mask |= (1 << 4);
1506 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1507 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1509 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1512 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1514 struct completion *waiting = qc->private_data;
1520 * ata_exec_internal_sg - execute libata internal command
1521 * @dev: Device to which the command is sent
1522 * @tf: Taskfile registers for the command and the result
1523 * @cdb: CDB for packet command
1524 * @dma_dir: Data tranfer direction of the command
1525 * @sgl: sg list for the data buffer of the command
1526 * @n_elem: Number of sg entries
1527 * @timeout: Timeout in msecs (0 for default)
1529 * Executes libata internal command with timeout. @tf contains
1530 * command on entry and result on return. Timeout and error
1531 * conditions are reported via return value. No recovery action
1532 * is taken after a command times out. It's caller's duty to
1533 * clean up after timeout.
1536 * None. Should be called with kernel context, might sleep.
1539 * Zero on success, AC_ERR_* mask on failure
1541 unsigned ata_exec_internal_sg(struct ata_device *dev,
1542 struct ata_taskfile *tf, const u8 *cdb,
1543 int dma_dir, struct scatterlist *sgl,
1544 unsigned int n_elem, unsigned long timeout)
1546 struct ata_link *link = dev->link;
1547 struct ata_port *ap = link->ap;
1548 u8 command = tf->command;
1549 int auto_timeout = 0;
1550 struct ata_queued_cmd *qc;
1551 unsigned int tag, preempted_tag;
1552 u32 preempted_sactive, preempted_qc_active;
1553 int preempted_nr_active_links;
1554 DECLARE_COMPLETION_ONSTACK(wait);
1555 unsigned long flags;
1556 unsigned int err_mask;
1559 spin_lock_irqsave(ap->lock, flags);
1561 /* no internal command while frozen */
1562 if (ap->pflags & ATA_PFLAG_FROZEN) {
1563 spin_unlock_irqrestore(ap->lock, flags);
1564 return AC_ERR_SYSTEM;
1567 /* initialize internal qc */
1569 /* XXX: Tag 0 is used for drivers with legacy EH as some
1570 * drivers choke if any other tag is given. This breaks
1571 * ata_tag_internal() test for those drivers. Don't use new
1572 * EH stuff without converting to it.
1574 if (ap->ops->error_handler)
1575 tag = ATA_TAG_INTERNAL;
1579 if (test_and_set_bit(tag, &ap->qc_allocated))
1581 qc = __ata_qc_from_tag(ap, tag);
1589 preempted_tag = link->active_tag;
1590 preempted_sactive = link->sactive;
1591 preempted_qc_active = ap->qc_active;
1592 preempted_nr_active_links = ap->nr_active_links;
1593 link->active_tag = ATA_TAG_POISON;
1596 ap->nr_active_links = 0;
1598 /* prepare & issue qc */
1601 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1602 qc->flags |= ATA_QCFLAG_RESULT_TF;
1603 qc->dma_dir = dma_dir;
1604 if (dma_dir != DMA_NONE) {
1605 unsigned int i, buflen = 0;
1606 struct scatterlist *sg;
1608 for_each_sg(sgl, sg, n_elem, i)
1609 buflen += sg->length;
1611 ata_sg_init(qc, sgl, n_elem);
1612 qc->nbytes = buflen;
1615 qc->private_data = &wait;
1616 qc->complete_fn = ata_qc_complete_internal;
1620 spin_unlock_irqrestore(ap->lock, flags);
1623 if (ata_probe_timeout)
1624 timeout = ata_probe_timeout * 1000;
1626 timeout = ata_internal_cmd_timeout(dev, command);
1631 if (ap->ops->error_handler)
1634 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1636 if (ap->ops->error_handler)
1639 ata_sff_flush_pio_task(ap);
1642 spin_lock_irqsave(ap->lock, flags);
1644 /* We're racing with irq here. If we lose, the
1645 * following test prevents us from completing the qc
1646 * twice. If we win, the port is frozen and will be
1647 * cleaned up by ->post_internal_cmd().
1649 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1650 qc->err_mask |= AC_ERR_TIMEOUT;
1652 if (ap->ops->error_handler)
1653 ata_port_freeze(ap);
1655 ata_qc_complete(qc);
1657 if (ata_msg_warn(ap))
1658 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1662 spin_unlock_irqrestore(ap->lock, flags);
1665 /* do post_internal_cmd */
1666 if (ap->ops->post_internal_cmd)
1667 ap->ops->post_internal_cmd(qc);
1669 /* perform minimal error analysis */
1670 if (qc->flags & ATA_QCFLAG_FAILED) {
1671 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1672 qc->err_mask |= AC_ERR_DEV;
1675 qc->err_mask |= AC_ERR_OTHER;
1677 if (qc->err_mask & ~AC_ERR_OTHER)
1678 qc->err_mask &= ~AC_ERR_OTHER;
1682 spin_lock_irqsave(ap->lock, flags);
1684 *tf = qc->result_tf;
1685 err_mask = qc->err_mask;
1688 link->active_tag = preempted_tag;
1689 link->sactive = preempted_sactive;
1690 ap->qc_active = preempted_qc_active;
1691 ap->nr_active_links = preempted_nr_active_links;
1693 spin_unlock_irqrestore(ap->lock, flags);
1695 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1696 ata_internal_cmd_timed_out(dev, command);
1702 * ata_exec_internal - execute libata internal command
1703 * @dev: Device to which the command is sent
1704 * @tf: Taskfile registers for the command and the result
1705 * @cdb: CDB for packet command
1706 * @dma_dir: Data tranfer direction of the command
1707 * @buf: Data buffer of the command
1708 * @buflen: Length of data buffer
1709 * @timeout: Timeout in msecs (0 for default)
1711 * Wrapper around ata_exec_internal_sg() which takes simple
1712 * buffer instead of sg list.
1715 * None. Should be called with kernel context, might sleep.
1718 * Zero on success, AC_ERR_* mask on failure
1720 unsigned ata_exec_internal(struct ata_device *dev,
1721 struct ata_taskfile *tf, const u8 *cdb,
1722 int dma_dir, void *buf, unsigned int buflen,
1723 unsigned long timeout)
1725 struct scatterlist *psg = NULL, sg;
1726 unsigned int n_elem = 0;
1728 if (dma_dir != DMA_NONE) {
1730 sg_init_one(&sg, buf, buflen);
1735 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1740 * ata_do_simple_cmd - execute simple internal command
1741 * @dev: Device to which the command is sent
1742 * @cmd: Opcode to execute
1744 * Execute a 'simple' command, that only consists of the opcode
1745 * 'cmd' itself, without filling any other registers
1748 * Kernel thread context (may sleep).
1751 * Zero on success, AC_ERR_* mask on failure
1753 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1755 struct ata_taskfile tf;
1757 ata_tf_init(dev, &tf);
1760 tf.flags |= ATA_TFLAG_DEVICE;
1761 tf.protocol = ATA_PROT_NODATA;
1763 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1767 * ata_pio_need_iordy - check if iordy needed
1770 * Check if the current speed of the device requires IORDY. Used
1771 * by various controllers for chip configuration.
1773 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1775 /* Don't set IORDY if we're preparing for reset. IORDY may
1776 * lead to controller lock up on certain controllers if the
1777 * port is not occupied. See bko#11703 for details.
1779 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1781 /* Controller doesn't support IORDY. Probably a pointless
1782 * check as the caller should know this.
1784 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1786 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1787 if (ata_id_is_cfa(adev->id)
1788 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1790 /* PIO3 and higher it is mandatory */
1791 if (adev->pio_mode > XFER_PIO_2)
1793 /* We turn it on when possible */
1794 if (ata_id_has_iordy(adev->id))
1800 * ata_pio_mask_no_iordy - Return the non IORDY mask
1803 * Compute the highest mode possible if we are not using iordy. Return
1804 * -1 if no iordy mode is available.
1806 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1808 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1809 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1810 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1811 /* Is the speed faster than the drive allows non IORDY ? */
1813 /* This is cycle times not frequency - watch the logic! */
1814 if (pio > 240) /* PIO2 is 240nS per cycle */
1815 return 3 << ATA_SHIFT_PIO;
1816 return 7 << ATA_SHIFT_PIO;
1819 return 3 << ATA_SHIFT_PIO;
1823 * ata_do_dev_read_id - default ID read method
1825 * @tf: proposed taskfile
1828 * Issue the identify taskfile and hand back the buffer containing
1829 * identify data. For some RAID controllers and for pre ATA devices
1830 * this function is wrapped or replaced by the driver
1832 unsigned int ata_do_dev_read_id(struct ata_device *dev,
1833 struct ata_taskfile *tf, u16 *id)
1835 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1836 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1840 * ata_dev_read_id - Read ID data from the specified device
1841 * @dev: target device
1842 * @p_class: pointer to class of the target device (may be changed)
1843 * @flags: ATA_READID_* flags
1844 * @id: buffer to read IDENTIFY data into
1846 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1847 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1848 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1849 * for pre-ATA4 drives.
1851 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1852 * now we abort if we hit that case.
1855 * Kernel thread context (may sleep)
1858 * 0 on success, -errno otherwise.
1860 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1861 unsigned int flags, u16 *id)
1863 struct ata_port *ap = dev->link->ap;
1864 unsigned int class = *p_class;
1865 struct ata_taskfile tf;
1866 unsigned int err_mask = 0;
1868 bool is_semb = class == ATA_DEV_SEMB;
1869 int may_fallback = 1, tried_spinup = 0;
1872 if (ata_msg_ctl(ap))
1873 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1876 ata_tf_init(dev, &tf);
1880 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
1882 tf.command = ATA_CMD_ID_ATA;
1885 tf.command = ATA_CMD_ID_ATAPI;
1889 reason = "unsupported class";
1893 tf.protocol = ATA_PROT_PIO;
1895 /* Some devices choke if TF registers contain garbage. Make
1896 * sure those are properly initialized.
1898 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1900 /* Device presence detection is unreliable on some
1901 * controllers. Always poll IDENTIFY if available.
1903 tf.flags |= ATA_TFLAG_POLLING;
1905 if (ap->ops->read_id)
1906 err_mask = ap->ops->read_id(dev, &tf, id);
1908 err_mask = ata_do_dev_read_id(dev, &tf, id);
1911 if (err_mask & AC_ERR_NODEV_HINT) {
1912 ata_dev_dbg(dev, "NODEV after polling detection\n");
1918 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1919 /* SEMB is not supported yet */
1920 *p_class = ATA_DEV_SEMB_UNSUP;
1924 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1925 /* Device or controller might have reported
1926 * the wrong device class. Give a shot at the
1927 * other IDENTIFY if the current one is
1928 * aborted by the device.
1933 if (class == ATA_DEV_ATA)
1934 class = ATA_DEV_ATAPI;
1936 class = ATA_DEV_ATA;
1940 /* Control reaches here iff the device aborted
1941 * both flavors of IDENTIFYs which happens
1942 * sometimes with phantom devices.
1945 "both IDENTIFYs aborted, assuming NODEV\n");
1950 reason = "I/O error";
1954 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1955 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1956 "class=%d may_fallback=%d tried_spinup=%d\n",
1957 class, may_fallback, tried_spinup);
1958 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1959 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1962 /* Falling back doesn't make sense if ID data was read
1963 * successfully at least once.
1967 swap_buf_le16(id, ATA_ID_WORDS);
1971 reason = "device reports invalid type";
1973 if (class == ATA_DEV_ATA) {
1974 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1977 if (ata_id_is_ata(id))
1981 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1984 * Drive powered-up in standby mode, and requires a specific
1985 * SET_FEATURES spin-up subcommand before it will accept
1986 * anything other than the original IDENTIFY command.
1988 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1989 if (err_mask && id[2] != 0x738c) {
1991 reason = "SPINUP failed";
1995 * If the drive initially returned incomplete IDENTIFY info,
1996 * we now must reissue the IDENTIFY command.
1998 if (id[2] == 0x37c8)
2002 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2004 * The exact sequence expected by certain pre-ATA4 drives is:
2006 * IDENTIFY (optional in early ATA)
2007 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2009 * Some drives were very specific about that exact sequence.
2011 * Note that ATA4 says lba is mandatory so the second check
2012 * should never trigger.
2014 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2015 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2018 reason = "INIT_DEV_PARAMS failed";
2022 /* current CHS translation info (id[53-58]) might be
2023 * changed. reread the identify device info.
2025 flags &= ~ATA_READID_POSTRESET;
2035 if (ata_msg_warn(ap))
2036 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2041 static int ata_do_link_spd_horkage(struct ata_device *dev)
2043 struct ata_link *plink = ata_dev_phys_link(dev);
2044 u32 target, target_limit;
2046 if (!sata_scr_valid(plink))
2049 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2054 target_limit = (1 << target) - 1;
2056 /* if already on stricter limit, no need to push further */
2057 if (plink->sata_spd_limit <= target_limit)
2060 plink->sata_spd_limit = target_limit;
2062 /* Request another EH round by returning -EAGAIN if link is
2063 * going faster than the target speed. Forward progress is
2064 * guaranteed by setting sata_spd_limit to target_limit above.
2066 if (plink->sata_spd > target) {
2067 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2068 sata_spd_string(target));
2074 static inline u8 ata_dev_knobble(struct ata_device *dev)
2076 struct ata_port *ap = dev->link->ap;
2078 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2081 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2084 static int ata_dev_config_ncq(struct ata_device *dev,
2085 char *desc, size_t desc_sz)
2087 struct ata_port *ap = dev->link->ap;
2088 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2089 unsigned int err_mask;
2092 if (!ata_id_has_ncq(dev->id)) {
2096 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2097 snprintf(desc, desc_sz, "NCQ (not used)");
2100 if (ap->flags & ATA_FLAG_NCQ) {
2101 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2102 dev->flags |= ATA_DFLAG_NCQ;
2105 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2106 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2107 ata_id_has_fpdma_aa(dev->id)) {
2108 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2112 "failed to enable AA (error_mask=0x%x)\n",
2114 if (err_mask != AC_ERR_DEV) {
2115 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2122 if (hdepth >= ddepth)
2123 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2125 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2131 * ata_dev_configure - Configure the specified ATA/ATAPI device
2132 * @dev: Target device to configure
2134 * Configure @dev according to @dev->id. Generic and low-level
2135 * driver specific fixups are also applied.
2138 * Kernel thread context (may sleep)
2141 * 0 on success, -errno otherwise
2143 int ata_dev_configure(struct ata_device *dev)
2145 struct ata_port *ap = dev->link->ap;
2146 struct ata_eh_context *ehc = &dev->link->eh_context;
2147 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2148 const u16 *id = dev->id;
2149 unsigned long xfer_mask;
2150 char revbuf[7]; /* XYZ-99\0 */
2151 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2152 char modelbuf[ATA_ID_PROD_LEN+1];
2155 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2156 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2160 if (ata_msg_probe(ap))
2161 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2164 dev->horkage |= ata_dev_blacklisted(dev);
2165 ata_force_horkage(dev);
2167 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2168 ata_dev_info(dev, "unsupported device, disabling\n");
2169 ata_dev_disable(dev);
2173 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2174 dev->class == ATA_DEV_ATAPI) {
2175 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2176 atapi_enabled ? "not supported with this driver"
2178 ata_dev_disable(dev);
2182 rc = ata_do_link_spd_horkage(dev);
2186 /* let ACPI work its magic */
2187 rc = ata_acpi_on_devcfg(dev);
2191 /* massage HPA, do it early as it might change IDENTIFY data */
2192 rc = ata_hpa_resize(dev);
2196 /* print device capabilities */
2197 if (ata_msg_probe(ap))
2199 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2200 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2202 id[49], id[82], id[83], id[84],
2203 id[85], id[86], id[87], id[88]);
2205 /* initialize to-be-configured parameters */
2206 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2207 dev->max_sectors = 0;
2213 dev->multi_count = 0;
2216 * common ATA, ATAPI feature tests
2219 /* find max transfer mode; for printk only */
2220 xfer_mask = ata_id_xfermask(id);
2222 if (ata_msg_probe(ap))
2225 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2226 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2229 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2232 /* ATA-specific feature tests */
2233 if (dev->class == ATA_DEV_ATA) {
2234 if (ata_id_is_cfa(id)) {
2235 /* CPRM may make this media unusable */
2236 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2238 "supports DRM functions and may not be fully accessible\n");
2239 snprintf(revbuf, 7, "CFA");
2241 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2242 /* Warn the user if the device has TPM extensions */
2243 if (ata_id_has_tpm(id))
2245 "supports DRM functions and may not be fully accessible\n");
2248 dev->n_sectors = ata_id_n_sectors(id);
2250 /* get current R/W Multiple count setting */
2251 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2252 unsigned int max = dev->id[47] & 0xff;
2253 unsigned int cnt = dev->id[59] & 0xff;
2254 /* only recognize/allow powers of two here */
2255 if (is_power_of_2(max) && is_power_of_2(cnt))
2257 dev->multi_count = cnt;
2260 if (ata_id_has_lba(id)) {
2261 const char *lba_desc;
2265 dev->flags |= ATA_DFLAG_LBA;
2266 if (ata_id_has_lba48(id)) {
2267 dev->flags |= ATA_DFLAG_LBA48;
2270 if (dev->n_sectors >= (1UL << 28) &&
2271 ata_id_has_flush_ext(id))
2272 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2276 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2280 /* print device info to dmesg */
2281 if (ata_msg_drv(ap) && print_info) {
2282 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2283 revbuf, modelbuf, fwrevbuf,
2284 ata_mode_string(xfer_mask));
2286 "%llu sectors, multi %u: %s %s\n",
2287 (unsigned long long)dev->n_sectors,
2288 dev->multi_count, lba_desc, ncq_desc);
2293 /* Default translation */
2294 dev->cylinders = id[1];
2296 dev->sectors = id[6];
2298 if (ata_id_current_chs_valid(id)) {
2299 /* Current CHS translation is valid. */
2300 dev->cylinders = id[54];
2301 dev->heads = id[55];
2302 dev->sectors = id[56];
2305 /* print device info to dmesg */
2306 if (ata_msg_drv(ap) && print_info) {
2307 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2308 revbuf, modelbuf, fwrevbuf,
2309 ata_mode_string(xfer_mask));
2311 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2312 (unsigned long long)dev->n_sectors,
2313 dev->multi_count, dev->cylinders,
2314 dev->heads, dev->sectors);
2321 /* ATAPI-specific feature tests */
2322 else if (dev->class == ATA_DEV_ATAPI) {
2323 const char *cdb_intr_string = "";
2324 const char *atapi_an_string = "";
2325 const char *dma_dir_string = "";
2328 rc = atapi_cdb_len(id);
2329 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2330 if (ata_msg_warn(ap))
2331 ata_dev_warn(dev, "unsupported CDB len\n");
2335 dev->cdb_len = (unsigned int) rc;
2337 /* Enable ATAPI AN if both the host and device have
2338 * the support. If PMP is attached, SNTF is required
2339 * to enable ATAPI AN to discern between PHY status
2340 * changed notifications and ATAPI ANs.
2343 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2344 (!sata_pmp_attached(ap) ||
2345 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2346 unsigned int err_mask;
2348 /* issue SET feature command to turn this on */
2349 err_mask = ata_dev_set_feature(dev,
2350 SETFEATURES_SATA_ENABLE, SATA_AN);
2353 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2356 dev->flags |= ATA_DFLAG_AN;
2357 atapi_an_string = ", ATAPI AN";
2361 if (ata_id_cdb_intr(dev->id)) {
2362 dev->flags |= ATA_DFLAG_CDB_INTR;
2363 cdb_intr_string = ", CDB intr";
2366 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2367 dev->flags |= ATA_DFLAG_DMADIR;
2368 dma_dir_string = ", DMADIR";
2371 /* print device info to dmesg */
2372 if (ata_msg_drv(ap) && print_info)
2374 "ATAPI: %s, %s, max %s%s%s%s\n",
2376 ata_mode_string(xfer_mask),
2377 cdb_intr_string, atapi_an_string,
2381 /* determine max_sectors */
2382 dev->max_sectors = ATA_MAX_SECTORS;
2383 if (dev->flags & ATA_DFLAG_LBA48)
2384 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2386 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2388 if (ata_dev_knobble(dev)) {
2389 if (ata_msg_drv(ap) && print_info)
2390 ata_dev_info(dev, "applying bridge limits\n");
2391 dev->udma_mask &= ATA_UDMA5;
2392 dev->max_sectors = ATA_MAX_SECTORS;
2395 if ((dev->class == ATA_DEV_ATAPI) &&
2396 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2397 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2398 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2401 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2402 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2405 if (ap->ops->dev_config)
2406 ap->ops->dev_config(dev);
2408 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2409 /* Let the user know. We don't want to disallow opens for
2410 rescue purposes, or in case the vendor is just a blithering
2411 idiot. Do this after the dev_config call as some controllers
2412 with buggy firmware may want to avoid reporting false device
2417 "Drive reports diagnostics failure. This may indicate a drive\n");
2419 "fault or invalid emulation. Contact drive vendor for information.\n");
2423 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2424 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2425 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
2431 if (ata_msg_probe(ap))
2432 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2437 * ata_cable_40wire - return 40 wire cable type
2440 * Helper method for drivers which want to hardwire 40 wire cable
2444 int ata_cable_40wire(struct ata_port *ap)
2446 return ATA_CBL_PATA40;
2450 * ata_cable_80wire - return 80 wire cable type
2453 * Helper method for drivers which want to hardwire 80 wire cable
2457 int ata_cable_80wire(struct ata_port *ap)
2459 return ATA_CBL_PATA80;
2463 * ata_cable_unknown - return unknown PATA cable.
2466 * Helper method for drivers which have no PATA cable detection.
2469 int ata_cable_unknown(struct ata_port *ap)
2471 return ATA_CBL_PATA_UNK;
2475 * ata_cable_ignore - return ignored PATA cable.
2478 * Helper method for drivers which don't use cable type to limit
2481 int ata_cable_ignore(struct ata_port *ap)
2483 return ATA_CBL_PATA_IGN;
2487 * ata_cable_sata - return SATA cable type
2490 * Helper method for drivers which have SATA cables
2493 int ata_cable_sata(struct ata_port *ap)
2495 return ATA_CBL_SATA;
2499 * ata_bus_probe - Reset and probe ATA bus
2502 * Master ATA bus probing function. Initiates a hardware-dependent
2503 * bus reset, then attempts to identify any devices found on
2507 * PCI/etc. bus probe sem.
2510 * Zero on success, negative errno otherwise.
2513 int ata_bus_probe(struct ata_port *ap)
2515 unsigned int classes[ATA_MAX_DEVICES];
2516 int tries[ATA_MAX_DEVICES];
2518 struct ata_device *dev;
2520 ata_for_each_dev(dev, &ap->link, ALL)
2521 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2524 ata_for_each_dev(dev, &ap->link, ALL) {
2525 /* If we issue an SRST then an ATA drive (not ATAPI)
2526 * may change configuration and be in PIO0 timing. If
2527 * we do a hard reset (or are coming from power on)
2528 * this is true for ATA or ATAPI. Until we've set a
2529 * suitable controller mode we should not touch the
2530 * bus as we may be talking too fast.
2532 dev->pio_mode = XFER_PIO_0;
2534 /* If the controller has a pio mode setup function
2535 * then use it to set the chipset to rights. Don't
2536 * touch the DMA setup as that will be dealt with when
2537 * configuring devices.
2539 if (ap->ops->set_piomode)
2540 ap->ops->set_piomode(ap, dev);
2543 /* reset and determine device classes */
2544 ap->ops->phy_reset(ap);
2546 ata_for_each_dev(dev, &ap->link, ALL) {
2547 if (dev->class != ATA_DEV_UNKNOWN)
2548 classes[dev->devno] = dev->class;
2550 classes[dev->devno] = ATA_DEV_NONE;
2552 dev->class = ATA_DEV_UNKNOWN;
2555 /* read IDENTIFY page and configure devices. We have to do the identify
2556 specific sequence bass-ackwards so that PDIAG- is released by
2559 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2560 if (tries[dev->devno])
2561 dev->class = classes[dev->devno];
2563 if (!ata_dev_enabled(dev))
2566 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2572 /* Now ask for the cable type as PDIAG- should have been released */
2573 if (ap->ops->cable_detect)
2574 ap->cbl = ap->ops->cable_detect(ap);
2576 /* We may have SATA bridge glue hiding here irrespective of
2577 * the reported cable types and sensed types. When SATA
2578 * drives indicate we have a bridge, we don't know which end
2579 * of the link the bridge is which is a problem.
2581 ata_for_each_dev(dev, &ap->link, ENABLED)
2582 if (ata_id_is_sata(dev->id))
2583 ap->cbl = ATA_CBL_SATA;
2585 /* After the identify sequence we can now set up the devices. We do
2586 this in the normal order so that the user doesn't get confused */
2588 ata_for_each_dev(dev, &ap->link, ENABLED) {
2589 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2590 rc = ata_dev_configure(dev);
2591 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2596 /* configure transfer mode */
2597 rc = ata_set_mode(&ap->link, &dev);
2601 ata_for_each_dev(dev, &ap->link, ENABLED)
2607 tries[dev->devno]--;
2611 /* eeek, something went very wrong, give up */
2612 tries[dev->devno] = 0;
2616 /* give it just one more chance */
2617 tries[dev->devno] = min(tries[dev->devno], 1);
2619 if (tries[dev->devno] == 1) {
2620 /* This is the last chance, better to slow
2621 * down than lose it.
2623 sata_down_spd_limit(&ap->link, 0);
2624 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2628 if (!tries[dev->devno])
2629 ata_dev_disable(dev);
2635 * sata_print_link_status - Print SATA link status
2636 * @link: SATA link to printk link status about
2638 * This function prints link speed and status of a SATA link.
2643 static void sata_print_link_status(struct ata_link *link)
2645 u32 sstatus, scontrol, tmp;
2647 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2649 sata_scr_read(link, SCR_CONTROL, &scontrol);
2651 if (ata_phys_link_online(link)) {
2652 tmp = (sstatus >> 4) & 0xf;
2653 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2654 sata_spd_string(tmp), sstatus, scontrol);
2656 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2662 * ata_dev_pair - return other device on cable
2665 * Obtain the other device on the same cable, or if none is
2666 * present NULL is returned
2669 struct ata_device *ata_dev_pair(struct ata_device *adev)
2671 struct ata_link *link = adev->link;
2672 struct ata_device *pair = &link->device[1 - adev->devno];
2673 if (!ata_dev_enabled(pair))
2679 * sata_down_spd_limit - adjust SATA spd limit downward
2680 * @link: Link to adjust SATA spd limit for
2681 * @spd_limit: Additional limit
2683 * Adjust SATA spd limit of @link downward. Note that this
2684 * function only adjusts the limit. The change must be applied
2685 * using sata_set_spd().
2687 * If @spd_limit is non-zero, the speed is limited to equal to or
2688 * lower than @spd_limit if such speed is supported. If
2689 * @spd_limit is slower than any supported speed, only the lowest
2690 * supported speed is allowed.
2693 * Inherited from caller.
2696 * 0 on success, negative errno on failure
2698 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2700 u32 sstatus, spd, mask;
2703 if (!sata_scr_valid(link))
2706 /* If SCR can be read, use it to determine the current SPD.
2707 * If not, use cached value in link->sata_spd.
2709 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2710 if (rc == 0 && ata_sstatus_online(sstatus))
2711 spd = (sstatus >> 4) & 0xf;
2713 spd = link->sata_spd;
2715 mask = link->sata_spd_limit;
2719 /* unconditionally mask off the highest bit */
2720 bit = fls(mask) - 1;
2721 mask &= ~(1 << bit);
2723 /* Mask off all speeds higher than or equal to the current
2724 * one. Force 1.5Gbps if current SPD is not available.
2727 mask &= (1 << (spd - 1)) - 1;
2731 /* were we already at the bottom? */
2736 if (mask & ((1 << spd_limit) - 1))
2737 mask &= (1 << spd_limit) - 1;
2739 bit = ffs(mask) - 1;
2744 link->sata_spd_limit = mask;
2746 ata_link_warn(link, "limiting SATA link speed to %s\n",
2747 sata_spd_string(fls(mask)));
2752 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2754 struct ata_link *host_link = &link->ap->link;
2755 u32 limit, target, spd;
2757 limit = link->sata_spd_limit;
2759 /* Don't configure downstream link faster than upstream link.
2760 * It doesn't speed up anything and some PMPs choke on such
2763 if (!ata_is_host_link(link) && host_link->sata_spd)
2764 limit &= (1 << host_link->sata_spd) - 1;
2766 if (limit == UINT_MAX)
2769 target = fls(limit);
2771 spd = (*scontrol >> 4) & 0xf;
2772 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2774 return spd != target;
2778 * sata_set_spd_needed - is SATA spd configuration needed
2779 * @link: Link in question
2781 * Test whether the spd limit in SControl matches
2782 * @link->sata_spd_limit. This function is used to determine
2783 * whether hardreset is necessary to apply SATA spd
2787 * Inherited from caller.
2790 * 1 if SATA spd configuration is needed, 0 otherwise.
2792 static int sata_set_spd_needed(struct ata_link *link)
2796 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2799 return __sata_set_spd_needed(link, &scontrol);
2803 * sata_set_spd - set SATA spd according to spd limit
2804 * @link: Link to set SATA spd for
2806 * Set SATA spd of @link according to sata_spd_limit.
2809 * Inherited from caller.
2812 * 0 if spd doesn't need to be changed, 1 if spd has been
2813 * changed. Negative errno if SCR registers are inaccessible.
2815 int sata_set_spd(struct ata_link *link)
2820 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2823 if (!__sata_set_spd_needed(link, &scontrol))
2826 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2833 * This mode timing computation functionality is ported over from
2834 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2837 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2838 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2839 * for UDMA6, which is currently supported only by Maxtor drives.
2841 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2844 static const struct ata_timing ata_timing[] = {
2845 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
2846 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
2847 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
2848 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
2849 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
2850 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
2851 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
2852 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
2854 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
2855 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
2856 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
2858 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
2859 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
2860 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
2861 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
2862 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
2864 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2865 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
2866 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
2867 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
2868 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
2869 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
2870 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
2871 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
2876 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
2877 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
2879 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2881 q->setup = EZ(t->setup * 1000, T);
2882 q->act8b = EZ(t->act8b * 1000, T);
2883 q->rec8b = EZ(t->rec8b * 1000, T);
2884 q->cyc8b = EZ(t->cyc8b * 1000, T);
2885 q->active = EZ(t->active * 1000, T);
2886 q->recover = EZ(t->recover * 1000, T);
2887 q->dmack_hold = EZ(t->dmack_hold * 1000, T);
2888 q->cycle = EZ(t->cycle * 1000, T);
2889 q->udma = EZ(t->udma * 1000, UT);
2892 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2893 struct ata_timing *m, unsigned int what)
2895 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2896 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2897 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2898 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2899 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2900 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2901 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
2902 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2903 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2906 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2908 const struct ata_timing *t = ata_timing;
2910 while (xfer_mode > t->mode)
2913 if (xfer_mode == t->mode)
2918 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2919 struct ata_timing *t, int T, int UT)
2921 const u16 *id = adev->id;
2922 const struct ata_timing *s;
2923 struct ata_timing p;
2929 if (!(s = ata_timing_find_mode(speed)))
2932 memcpy(t, s, sizeof(*s));
2935 * If the drive is an EIDE drive, it can tell us it needs extended
2936 * PIO/MW_DMA cycle timing.
2939 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2940 memset(&p, 0, sizeof(p));
2942 if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
2943 if (speed <= XFER_PIO_2)
2944 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
2945 else if ((speed <= XFER_PIO_4) ||
2946 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
2947 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
2948 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
2949 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
2951 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2955 * Convert the timing to bus clock counts.
2958 ata_timing_quantize(t, t, T, UT);
2961 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2962 * S.M.A.R.T * and some other commands. We have to ensure that the
2963 * DMA cycle timing is slower/equal than the fastest PIO timing.
2966 if (speed > XFER_PIO_6) {
2967 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2968 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2972 * Lengthen active & recovery time so that cycle time is correct.
2975 if (t->act8b + t->rec8b < t->cyc8b) {
2976 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2977 t->rec8b = t->cyc8b - t->act8b;
2980 if (t->active + t->recover < t->cycle) {
2981 t->active += (t->cycle - (t->active + t->recover)) / 2;
2982 t->recover = t->cycle - t->active;
2985 /* In a few cases quantisation may produce enough errors to
2986 leave t->cycle too low for the sum of active and recovery
2987 if so we must correct this */
2988 if (t->active + t->recover > t->cycle)
2989 t->cycle = t->active + t->recover;
2995 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
2996 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
2997 * @cycle: cycle duration in ns
2999 * Return matching xfer mode for @cycle. The returned mode is of
3000 * the transfer type specified by @xfer_shift. If @cycle is too
3001 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3002 * than the fastest known mode, the fasted mode is returned.
3008 * Matching xfer_mode, 0xff if no match found.
3010 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3012 u8 base_mode = 0xff, last_mode = 0xff;
3013 const struct ata_xfer_ent *ent;
3014 const struct ata_timing *t;
3016 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3017 if (ent->shift == xfer_shift)
3018 base_mode = ent->base;
3020 for (t = ata_timing_find_mode(base_mode);
3021 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3022 unsigned short this_cycle;
3024 switch (xfer_shift) {
3026 case ATA_SHIFT_MWDMA:
3027 this_cycle = t->cycle;
3029 case ATA_SHIFT_UDMA:
3030 this_cycle = t->udma;
3036 if (cycle > this_cycle)
3039 last_mode = t->mode;
3046 * ata_down_xfermask_limit - adjust dev xfer masks downward
3047 * @dev: Device to adjust xfer masks
3048 * @sel: ATA_DNXFER_* selector
3050 * Adjust xfer masks of @dev downward. Note that this function
3051 * does not apply the change. Invoking ata_set_mode() afterwards
3052 * will apply the limit.
3055 * Inherited from caller.
3058 * 0 on success, negative errno on failure
3060 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3063 unsigned long orig_mask, xfer_mask;
3064 unsigned long pio_mask, mwdma_mask, udma_mask;
3067 quiet = !!(sel & ATA_DNXFER_QUIET);
3068 sel &= ~ATA_DNXFER_QUIET;
3070 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3073 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3076 case ATA_DNXFER_PIO:
3077 highbit = fls(pio_mask) - 1;
3078 pio_mask &= ~(1 << highbit);
3081 case ATA_DNXFER_DMA:
3083 highbit = fls(udma_mask) - 1;
3084 udma_mask &= ~(1 << highbit);
3087 } else if (mwdma_mask) {
3088 highbit = fls(mwdma_mask) - 1;
3089 mwdma_mask &= ~(1 << highbit);
3095 case ATA_DNXFER_40C:
3096 udma_mask &= ATA_UDMA_MASK_40C;
3099 case ATA_DNXFER_FORCE_PIO0:
3101 case ATA_DNXFER_FORCE_PIO:
3110 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3112 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3116 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3117 snprintf(buf, sizeof(buf), "%s:%s",
3118 ata_mode_string(xfer_mask),
3119 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3121 snprintf(buf, sizeof(buf), "%s",
3122 ata_mode_string(xfer_mask));
3124 ata_dev_warn(dev, "limiting speed to %s\n", buf);
3127 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3133 static int ata_dev_set_mode(struct ata_device *dev)
3135 struct ata_port *ap = dev->link->ap;
3136 struct ata_eh_context *ehc = &dev->link->eh_context;
3137 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3138 const char *dev_err_whine = "";
3139 int ign_dev_err = 0;
3140 unsigned int err_mask = 0;
3143 dev->flags &= ~ATA_DFLAG_PIO;
3144 if (dev->xfer_shift == ATA_SHIFT_PIO)
3145 dev->flags |= ATA_DFLAG_PIO;
3147 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3148 dev_err_whine = " (SET_XFERMODE skipped)";
3152 "NOSETXFER but PATA detected - can't "
3153 "skip SETXFER, might malfunction\n");
3154 err_mask = ata_dev_set_xfermode(dev);
3157 if (err_mask & ~AC_ERR_DEV)
3161 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3162 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3163 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3167 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3168 /* Old CFA may refuse this command, which is just fine */
3169 if (ata_id_is_cfa(dev->id))
3171 /* Catch several broken garbage emulations plus some pre
3173 if (ata_id_major_version(dev->id) == 0 &&
3174 dev->pio_mode <= XFER_PIO_2)
3176 /* Some very old devices and some bad newer ones fail
3177 any kind of SET_XFERMODE request but support PIO0-2
3178 timings and no IORDY */
3179 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3182 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3183 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3184 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3185 dev->dma_mode == XFER_MW_DMA_0 &&
3186 (dev->id[63] >> 8) & 1)
3189 /* if the device is actually configured correctly, ignore dev err */
3190 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3193 if (err_mask & AC_ERR_DEV) {
3197 dev_err_whine = " (device error ignored)";
3200 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3201 dev->xfer_shift, (int)dev->xfer_mode);
3203 ata_dev_info(dev, "configured for %s%s\n",
3204 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3210 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
3215 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3216 * @link: link on which timings will be programmed
3217 * @r_failed_dev: out parameter for failed device
3219 * Standard implementation of the function used to tune and set
3220 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3221 * ata_dev_set_mode() fails, pointer to the failing device is
3222 * returned in @r_failed_dev.
3225 * PCI/etc. bus probe sem.
3228 * 0 on success, negative errno otherwise
3231 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3233 struct ata_port *ap = link->ap;
3234 struct ata_device *dev;
3235 int rc = 0, used_dma = 0, found = 0;
3237 /* step 1: calculate xfer_mask */
3238 ata_for_each_dev(dev, link, ENABLED) {
3239 unsigned long pio_mask, dma_mask;
3240 unsigned int mode_mask;
3242 mode_mask = ATA_DMA_MASK_ATA;
3243 if (dev->class == ATA_DEV_ATAPI)
3244 mode_mask = ATA_DMA_MASK_ATAPI;
3245 else if (ata_id_is_cfa(dev->id))
3246 mode_mask = ATA_DMA_MASK_CFA;
3248 ata_dev_xfermask(dev);
3249 ata_force_xfermask(dev);
3251 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3253 if (libata_dma_mask & mode_mask)
3254 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3259 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3260 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3263 if (ata_dma_enabled(dev))
3269 /* step 2: always set host PIO timings */
3270 ata_for_each_dev(dev, link, ENABLED) {
3271 if (dev->pio_mode == 0xff) {
3272 ata_dev_warn(dev, "no PIO support\n");
3277 dev->xfer_mode = dev->pio_mode;
3278 dev->xfer_shift = ATA_SHIFT_PIO;
3279 if (ap->ops->set_piomode)
3280 ap->ops->set_piomode(ap, dev);
3283 /* step 3: set host DMA timings */
3284 ata_for_each_dev(dev, link, ENABLED) {
3285 if (!ata_dma_enabled(dev))
3288 dev->xfer_mode = dev->dma_mode;
3289 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3290 if (ap->ops->set_dmamode)
3291 ap->ops->set_dmamode(ap, dev);
3294 /* step 4: update devices' xfer mode */
3295 ata_for_each_dev(dev, link, ENABLED) {