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>
73 /* debounce timing parameters in msecs { interval, duration, timeout } */
74 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
75 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
76 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
78 const struct ata_port_operations ata_base_port_ops = {
79 .prereset = ata_std_prereset,
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 .hardreset = sata_std_hardreset,
91 static unsigned int ata_dev_init_params(struct ata_device *dev,
92 u16 heads, u16 sectors);
93 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
94 static unsigned int ata_dev_set_feature(struct ata_device *dev,
95 u8 enable, u8 feature);
96 static void ata_dev_xfermask(struct ata_device *dev);
97 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
99 unsigned int ata_print_id = 1;
100 static struct workqueue_struct *ata_wq;
102 struct workqueue_struct *ata_aux_wq;
104 struct ata_force_param {
108 unsigned long xfer_mask;
109 unsigned int horkage_on;
110 unsigned int horkage_off;
114 struct ata_force_ent {
117 struct ata_force_param param;
120 static struct ata_force_ent *ata_force_tbl;
121 static int ata_force_tbl_size;
123 static char ata_force_param_buf[PAGE_SIZE] __initdata;
124 /* param_buf is thrown away after initialization, disallow read */
125 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
126 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
128 static int atapi_enabled = 1;
129 module_param(atapi_enabled, int, 0444);
130 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
132 static int atapi_dmadir = 0;
133 module_param(atapi_dmadir, int, 0444);
134 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
136 int atapi_passthru16 = 1;
137 module_param(atapi_passthru16, int, 0444);
138 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
141 module_param_named(fua, libata_fua, int, 0444);
142 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
144 static int ata_ignore_hpa;
145 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
146 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
148 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
149 module_param_named(dma, libata_dma_mask, int, 0444);
150 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
152 static int ata_probe_timeout;
153 module_param(ata_probe_timeout, int, 0444);
154 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
156 int libata_noacpi = 0;
157 module_param_named(noacpi, libata_noacpi, int, 0444);
158 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
160 int libata_allow_tpm = 0;
161 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
162 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [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_printk(ap, KERN_NOTICE,
340 "FORCE: cable set to %s\n", fe->param.name);
346 * ata_force_link_limits - force link limits according to libata.force
347 * @link: ATA link of interest
349 * Force link flags and SATA spd limit according to libata.force
350 * and whine about it. When only the port part is specified
351 * (e.g. 1:), the limit applies to all links connected to both
352 * the host link and all fan-out ports connected via PMP. If the
353 * device part is specified as 0 (e.g. 1.00:), it specifies the
354 * first fan-out link not the host link. Device number 15 always
355 * points to the host link whether PMP is attached or not. If the
356 * controller has slave link, device number 16 points to it.
361 static void ata_force_link_limits(struct ata_link *link)
363 bool did_spd = false;
364 int linkno = link->pmp;
367 if (ata_is_host_link(link))
370 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
371 const struct ata_force_ent *fe = &ata_force_tbl[i];
373 if (fe->port != -1 && fe->port != link->ap->print_id)
376 if (fe->device != -1 && fe->device != linkno)
379 /* only honor the first spd limit */
380 if (!did_spd && fe->param.spd_limit) {
381 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
382 ata_link_printk(link, KERN_NOTICE,
383 "FORCE: PHY spd limit set to %s\n",
388 /* let lflags stack */
389 if (fe->param.lflags) {
390 link->flags |= fe->param.lflags;
391 ata_link_printk(link, KERN_NOTICE,
392 "FORCE: link flag 0x%x forced -> 0x%x\n",
393 fe->param.lflags, link->flags);
399 * ata_force_xfermask - force xfermask according to libata.force
400 * @dev: ATA device of interest
402 * Force xfer_mask according to libata.force and whine about it.
403 * For consistency with link selection, device number 15 selects
404 * the first device connected to the host link.
409 static void ata_force_xfermask(struct ata_device *dev)
411 int devno = dev->link->pmp + dev->devno;
412 int alt_devno = devno;
415 /* allow n.15/16 for devices attached to host port */
416 if (ata_is_host_link(dev->link))
419 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
420 const struct ata_force_ent *fe = &ata_force_tbl[i];
421 unsigned long pio_mask, mwdma_mask, udma_mask;
423 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
426 if (fe->device != -1 && fe->device != devno &&
427 fe->device != alt_devno)
430 if (!fe->param.xfer_mask)
433 ata_unpack_xfermask(fe->param.xfer_mask,
434 &pio_mask, &mwdma_mask, &udma_mask);
436 dev->udma_mask = udma_mask;
437 else if (mwdma_mask) {
439 dev->mwdma_mask = mwdma_mask;
443 dev->pio_mask = pio_mask;
446 ata_dev_printk(dev, KERN_NOTICE,
447 "FORCE: xfer_mask set to %s\n", fe->param.name);
453 * ata_force_horkage - force horkage according to libata.force
454 * @dev: ATA device of interest
456 * Force horkage according to libata.force and whine about it.
457 * For consistency with link selection, device number 15 selects
458 * the first device connected to the host link.
463 static void ata_force_horkage(struct ata_device *dev)
465 int devno = dev->link->pmp + dev->devno;
466 int alt_devno = devno;
469 /* allow n.15/16 for devices attached to host port */
470 if (ata_is_host_link(dev->link))
473 for (i = 0; i < ata_force_tbl_size; i++) {
474 const struct ata_force_ent *fe = &ata_force_tbl[i];
476 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
479 if (fe->device != -1 && fe->device != devno &&
480 fe->device != alt_devno)
483 if (!(~dev->horkage & fe->param.horkage_on) &&
484 !(dev->horkage & fe->param.horkage_off))
487 dev->horkage |= fe->param.horkage_on;
488 dev->horkage &= ~fe->param.horkage_off;
490 ata_dev_printk(dev, KERN_NOTICE,
491 "FORCE: horkage modified (%s)\n", fe->param.name);
496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
499 * Determine ATAPI command type from @opcode.
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
507 int atapi_cmd_type(u8 opcode)
516 case GPCMD_WRITE_AND_VERIFY_10:
520 case GPCMD_READ_CD_MSF:
521 return ATAPI_READ_CD;
525 if (atapi_passthru16)
526 return ATAPI_PASS_THRU;
534 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
535 * @tf: Taskfile to convert
536 * @pmp: Port multiplier port
537 * @is_cmd: This FIS is for command
538 * @fis: Buffer into which data will output
540 * Converts a standard ATA taskfile to a Serial ATA
541 * FIS structure (Register - Host to Device).
544 * Inherited from caller.
546 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
548 fis[0] = 0x27; /* Register - Host to Device FIS */
549 fis[1] = pmp & 0xf; /* Port multiplier number*/
551 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
553 fis[2] = tf->command;
554 fis[3] = tf->feature;
561 fis[8] = tf->hob_lbal;
562 fis[9] = tf->hob_lbam;
563 fis[10] = tf->hob_lbah;
564 fis[11] = tf->hob_feature;
567 fis[13] = tf->hob_nsect;
578 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
579 * @fis: Buffer from which data will be input
580 * @tf: Taskfile to output
582 * Converts a serial ATA FIS structure to a standard ATA taskfile.
585 * Inherited from caller.
588 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
590 tf->command = fis[2]; /* status */
591 tf->feature = fis[3]; /* error */
598 tf->hob_lbal = fis[8];
599 tf->hob_lbam = fis[9];
600 tf->hob_lbah = fis[10];
603 tf->hob_nsect = fis[13];
606 static const u8 ata_rw_cmds[] = {
610 ATA_CMD_READ_MULTI_EXT,
611 ATA_CMD_WRITE_MULTI_EXT,
615 ATA_CMD_WRITE_MULTI_FUA_EXT,
619 ATA_CMD_PIO_READ_EXT,
620 ATA_CMD_PIO_WRITE_EXT,
633 ATA_CMD_WRITE_FUA_EXT
637 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
638 * @tf: command to examine and configure
639 * @dev: device tf belongs to
641 * Examine the device configuration and tf->flags to calculate
642 * the proper read/write commands and protocol to use.
647 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
651 int index, fua, lba48, write;
653 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
654 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
655 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
657 if (dev->flags & ATA_DFLAG_PIO) {
658 tf->protocol = ATA_PROT_PIO;
659 index = dev->multi_count ? 0 : 8;
660 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
661 /* Unable to use DMA due to host limitation */
662 tf->protocol = ATA_PROT_PIO;
663 index = dev->multi_count ? 0 : 8;
665 tf->protocol = ATA_PROT_DMA;
669 cmd = ata_rw_cmds[index + fua + lba48 + write];
678 * ata_tf_read_block - Read block address from ATA taskfile
679 * @tf: ATA taskfile of interest
680 * @dev: ATA device @tf belongs to
685 * Read block address from @tf. This function can handle all
686 * three address formats - LBA, LBA48 and CHS. tf->protocol and
687 * flags select the address format to use.
690 * Block address read from @tf.
692 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
696 if (tf->flags & ATA_TFLAG_LBA) {
697 if (tf->flags & ATA_TFLAG_LBA48) {
698 block |= (u64)tf->hob_lbah << 40;
699 block |= (u64)tf->hob_lbam << 32;
700 block |= (u64)tf->hob_lbal << 24;
702 block |= (tf->device & 0xf) << 24;
704 block |= tf->lbah << 16;
705 block |= tf->lbam << 8;
710 cyl = tf->lbam | (tf->lbah << 8);
711 head = tf->device & 0xf;
715 ata_dev_printk(dev, KERN_WARNING, "device reported "
716 "invalid CHS sector 0\n");
717 sect = 1; /* oh well */
720 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
727 * ata_build_rw_tf - Build ATA taskfile for given read/write request
728 * @tf: Target ATA taskfile
729 * @dev: ATA device @tf belongs to
730 * @block: Block address
731 * @n_block: Number of blocks
732 * @tf_flags: RW/FUA etc...
738 * Build ATA taskfile @tf for read/write request described by
739 * @block, @n_block, @tf_flags and @tag on @dev.
743 * 0 on success, -ERANGE if the request is too large for @dev,
744 * -EINVAL if the request is invalid.
746 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
747 u64 block, u32 n_block, unsigned int tf_flags,
750 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
751 tf->flags |= tf_flags;
753 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
755 if (!lba_48_ok(block, n_block))
758 tf->protocol = ATA_PROT_NCQ;
759 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
761 if (tf->flags & ATA_TFLAG_WRITE)
762 tf->command = ATA_CMD_FPDMA_WRITE;
764 tf->command = ATA_CMD_FPDMA_READ;
766 tf->nsect = tag << 3;
767 tf->hob_feature = (n_block >> 8) & 0xff;
768 tf->feature = n_block & 0xff;
770 tf->hob_lbah = (block >> 40) & 0xff;
771 tf->hob_lbam = (block >> 32) & 0xff;
772 tf->hob_lbal = (block >> 24) & 0xff;
773 tf->lbah = (block >> 16) & 0xff;
774 tf->lbam = (block >> 8) & 0xff;
775 tf->lbal = block & 0xff;
778 if (tf->flags & ATA_TFLAG_FUA)
779 tf->device |= 1 << 7;
780 } else if (dev->flags & ATA_DFLAG_LBA) {
781 tf->flags |= ATA_TFLAG_LBA;
783 if (lba_28_ok(block, n_block)) {
785 tf->device |= (block >> 24) & 0xf;
786 } else if (lba_48_ok(block, n_block)) {
787 if (!(dev->flags & ATA_DFLAG_LBA48))
791 tf->flags |= ATA_TFLAG_LBA48;
793 tf->hob_nsect = (n_block >> 8) & 0xff;
795 tf->hob_lbah = (block >> 40) & 0xff;
796 tf->hob_lbam = (block >> 32) & 0xff;
797 tf->hob_lbal = (block >> 24) & 0xff;
799 /* request too large even for LBA48 */
802 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
805 tf->nsect = n_block & 0xff;
807 tf->lbah = (block >> 16) & 0xff;
808 tf->lbam = (block >> 8) & 0xff;
809 tf->lbal = block & 0xff;
811 tf->device |= ATA_LBA;
814 u32 sect, head, cyl, track;
816 /* The request -may- be too large for CHS addressing. */
817 if (!lba_28_ok(block, n_block))
820 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
823 /* Convert LBA to CHS */
824 track = (u32)block / dev->sectors;
825 cyl = track / dev->heads;
826 head = track % dev->heads;
827 sect = (u32)block % dev->sectors + 1;
829 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
830 (u32)block, track, cyl, head, sect);
832 /* Check whether the converted CHS can fit.
836 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
839 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
850 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
851 * @pio_mask: pio_mask
852 * @mwdma_mask: mwdma_mask
853 * @udma_mask: udma_mask
855 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
856 * unsigned int xfer_mask.
864 unsigned long ata_pack_xfermask(unsigned long pio_mask,
865 unsigned long mwdma_mask,
866 unsigned long udma_mask)
868 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
869 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
870 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
874 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
875 * @xfer_mask: xfer_mask to unpack
876 * @pio_mask: resulting pio_mask
877 * @mwdma_mask: resulting mwdma_mask
878 * @udma_mask: resulting udma_mask
880 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
881 * Any NULL distination masks will be ignored.
883 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
884 unsigned long *mwdma_mask, unsigned long *udma_mask)
887 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
889 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
891 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
894 static const struct ata_xfer_ent {
898 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
899 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
900 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
905 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
906 * @xfer_mask: xfer_mask of interest
908 * Return matching XFER_* value for @xfer_mask. Only the highest
909 * bit of @xfer_mask is considered.
915 * Matching XFER_* value, 0xff if no match found.
917 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
919 int highbit = fls(xfer_mask) - 1;
920 const struct ata_xfer_ent *ent;
922 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
923 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
924 return ent->base + highbit - ent->shift;
929 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
930 * @xfer_mode: XFER_* of interest
932 * Return matching xfer_mask for @xfer_mode.
938 * Matching xfer_mask, 0 if no match found.
940 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
942 const struct ata_xfer_ent *ent;
944 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
945 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
946 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
947 & ~((1 << ent->shift) - 1);
952 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
953 * @xfer_mode: XFER_* of interest
955 * Return matching xfer_shift for @xfer_mode.
961 * Matching xfer_shift, -1 if no match found.
963 int ata_xfer_mode2shift(unsigned long xfer_mode)
965 const struct ata_xfer_ent *ent;
967 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
968 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
974 * ata_mode_string - convert xfer_mask to string
975 * @xfer_mask: mask of bits supported; only highest bit counts.
977 * Determine string which represents the highest speed
978 * (highest bit in @modemask).
984 * Constant C string representing highest speed listed in
985 * @mode_mask, or the constant C string "<n/a>".
987 const char *ata_mode_string(unsigned long xfer_mask)
989 static const char * const xfer_mode_str[] = {
1013 highbit = fls(xfer_mask) - 1;
1014 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1015 return xfer_mode_str[highbit];
1019 static const char *sata_spd_string(unsigned int spd)
1021 static const char * const spd_str[] = {
1027 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1029 return spd_str[spd - 1];
1032 static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1034 struct ata_link *link = dev->link;
1035 struct ata_port *ap = link->ap;
1037 unsigned int err_mask;
1041 * disallow DIPM for drivers which haven't set
1042 * ATA_FLAG_IPM. This is because when DIPM is enabled,
1043 * phy ready will be set in the interrupt status on
1044 * state changes, which will cause some drivers to
1045 * think there are errors - additionally drivers will
1046 * need to disable hot plug.
1048 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1049 ap->pm_policy = NOT_AVAILABLE;
1054 * For DIPM, we will only enable it for the
1055 * min_power setting.
1057 * Why? Because Disks are too stupid to know that
1058 * If the host rejects a request to go to SLUMBER
1059 * they should retry at PARTIAL, and instead it
1060 * just would give up. So, for medium_power to
1061 * work at all, we need to only allow HIPM.
1063 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1069 /* no restrictions on IPM transitions */
1070 scontrol &= ~(0x3 << 8);
1071 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1076 if (dev->flags & ATA_DFLAG_DIPM)
1077 err_mask = ata_dev_set_feature(dev,
1078 SETFEATURES_SATA_ENABLE, SATA_DIPM);
1081 /* allow IPM to PARTIAL */
1082 scontrol &= ~(0x1 << 8);
1083 scontrol |= (0x2 << 8);
1084 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1089 * we don't have to disable DIPM since IPM flags
1090 * disallow transitions to SLUMBER, which effectively
1091 * disable DIPM if it does not support PARTIAL
1095 case MAX_PERFORMANCE:
1096 /* disable all IPM transitions */
1097 scontrol |= (0x3 << 8);
1098 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1103 * we don't have to disable DIPM since IPM flags
1104 * disallow all transitions which effectively
1105 * disable DIPM anyway.
1110 /* FIXME: handle SET FEATURES failure */
1117 * ata_dev_enable_pm - enable SATA interface power management
1118 * @dev: device to enable power management
1119 * @policy: the link power management policy
1121 * Enable SATA Interface power management. This will enable
1122 * Device Interface Power Management (DIPM) for min_power
1123 * policy, and then call driver specific callbacks for
1124 * enabling Host Initiated Power management.
1127 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
1129 void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1132 struct ata_port *ap = dev->link->ap;
1134 /* set HIPM first, then DIPM */
1135 if (ap->ops->enable_pm)
1136 rc = ap->ops->enable_pm(ap, policy);
1139 rc = ata_dev_set_dipm(dev, policy);
1143 ap->pm_policy = MAX_PERFORMANCE;
1145 ap->pm_policy = policy;
1146 return /* rc */; /* hopefully we can use 'rc' eventually */
1151 * ata_dev_disable_pm - disable SATA interface power management
1152 * @dev: device to disable power management
1154 * Disable SATA Interface power management. This will disable
1155 * Device Interface Power Management (DIPM) without changing
1156 * policy, call driver specific callbacks for disabling Host
1157 * Initiated Power management.
1162 static void ata_dev_disable_pm(struct ata_device *dev)
1164 struct ata_port *ap = dev->link->ap;
1166 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1167 if (ap->ops->disable_pm)
1168 ap->ops->disable_pm(ap);
1170 #endif /* CONFIG_PM */
1172 void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1174 ap->pm_policy = policy;
1175 ap->link.eh_info.action |= ATA_EH_LPM;
1176 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1177 ata_port_schedule_eh(ap);
1181 static void ata_lpm_enable(struct ata_host *host)
1183 struct ata_link *link;
1184 struct ata_port *ap;
1185 struct ata_device *dev;
1188 for (i = 0; i < host->n_ports; i++) {
1189 ap = host->ports[i];
1190 ata_for_each_link(link, ap, EDGE) {
1191 ata_for_each_dev(dev, link, ALL)
1192 ata_dev_disable_pm(dev);
1197 static void ata_lpm_disable(struct ata_host *host)
1201 for (i = 0; i < host->n_ports; i++) {
1202 struct ata_port *ap = host->ports[i];
1203 ata_lpm_schedule(ap, ap->pm_policy);
1206 #endif /* CONFIG_PM */
1209 * ata_dev_classify - determine device type based on ATA-spec signature
1210 * @tf: ATA taskfile register set for device to be identified
1212 * Determine from taskfile register contents whether a device is
1213 * ATA or ATAPI, as per "Signature and persistence" section
1214 * of ATA/PI spec (volume 1, sect 5.14).
1220 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1221 * %ATA_DEV_UNKNOWN the event of failure.
1223 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1225 /* Apple's open source Darwin code hints that some devices only
1226 * put a proper signature into the LBA mid/high registers,
1227 * So, we only check those. It's sufficient for uniqueness.
1229 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1230 * signatures for ATA and ATAPI devices attached on SerialATA,
1231 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1232 * spec has never mentioned about using different signatures
1233 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1234 * Multiplier specification began to use 0x69/0x96 to identify
1235 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1236 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1237 * 0x69/0x96 shortly and described them as reserved for
1240 * We follow the current spec and consider that 0x69/0x96
1241 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1242 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1243 * SEMB signature. This is worked around in
1244 * ata_dev_read_id().
1246 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1247 DPRINTK("found ATA device by sig\n");
1251 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1252 DPRINTK("found ATAPI device by sig\n");
1253 return ATA_DEV_ATAPI;
1256 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1257 DPRINTK("found PMP device by sig\n");
1261 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1262 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1263 return ATA_DEV_SEMB;
1266 DPRINTK("unknown device\n");
1267 return ATA_DEV_UNKNOWN;
1271 * ata_id_string - Convert IDENTIFY DEVICE page into string
1272 * @id: IDENTIFY DEVICE results we will examine
1273 * @s: string into which data is output
1274 * @ofs: offset into identify device page
1275 * @len: length of string to return. must be an even number.
1277 * The strings in the IDENTIFY DEVICE page are broken up into
1278 * 16-bit chunks. Run through the string, and output each
1279 * 8-bit chunk linearly, regardless of platform.
1285 void ata_id_string(const u16 *id, unsigned char *s,
1286 unsigned int ofs, unsigned int len)
1307 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1308 * @id: IDENTIFY DEVICE results we will examine
1309 * @s: string into which data is output
1310 * @ofs: offset into identify device page
1311 * @len: length of string to return. must be an odd number.
1313 * This function is identical to ata_id_string except that it
1314 * trims trailing spaces and terminates the resulting string with
1315 * null. @len must be actual maximum length (even number) + 1.
1320 void ata_id_c_string(const u16 *id, unsigned char *s,
1321 unsigned int ofs, unsigned int len)
1325 ata_id_string(id, s, ofs, len - 1);
1327 p = s + strnlen(s, len - 1);
1328 while (p > s && p[-1] == ' ')
1333 static u64 ata_id_n_sectors(const u16 *id)
1335 if (ata_id_has_lba(id)) {
1336 if (ata_id_has_lba48(id))
1337 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1339 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1341 if (ata_id_current_chs_valid(id))
1342 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1343 id[ATA_ID_CUR_SECTORS];
1345 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1350 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1354 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1355 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1356 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1357 sectors |= (tf->lbah & 0xff) << 16;
1358 sectors |= (tf->lbam & 0xff) << 8;
1359 sectors |= (tf->lbal & 0xff);
1364 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1368 sectors |= (tf->device & 0x0f) << 24;
1369 sectors |= (tf->lbah & 0xff) << 16;
1370 sectors |= (tf->lbam & 0xff) << 8;
1371 sectors |= (tf->lbal & 0xff);
1377 * ata_read_native_max_address - Read native max address
1378 * @dev: target device
1379 * @max_sectors: out parameter for the result native max address
1381 * Perform an LBA48 or LBA28 native size query upon the device in
1385 * 0 on success, -EACCES if command is aborted by the drive.
1386 * -EIO on other errors.
1388 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1390 unsigned int err_mask;
1391 struct ata_taskfile tf;
1392 int lba48 = ata_id_has_lba48(dev->id);
1394 ata_tf_init(dev, &tf);
1396 /* always clear all address registers */
1397 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1400 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1401 tf.flags |= ATA_TFLAG_LBA48;
1403 tf.command = ATA_CMD_READ_NATIVE_MAX;
1405 tf.protocol |= ATA_PROT_NODATA;
1406 tf.device |= ATA_LBA;
1408 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1410 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1411 "max address (err_mask=0x%x)\n", err_mask);
1412 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1418 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1420 *max_sectors = ata_tf_to_lba(&tf) + 1;
1421 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1427 * ata_set_max_sectors - Set max sectors
1428 * @dev: target device
1429 * @new_sectors: new max sectors value to set for the device
1431 * Set max sectors of @dev to @new_sectors.
1434 * 0 on success, -EACCES if command is aborted or denied (due to
1435 * previous non-volatile SET_MAX) by the drive. -EIO on other
1438 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1440 unsigned int err_mask;
1441 struct ata_taskfile tf;
1442 int lba48 = ata_id_has_lba48(dev->id);
1446 ata_tf_init(dev, &tf);
1448 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1451 tf.command = ATA_CMD_SET_MAX_EXT;
1452 tf.flags |= ATA_TFLAG_LBA48;
1454 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1455 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1456 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1458 tf.command = ATA_CMD_SET_MAX;
1460 tf.device |= (new_sectors >> 24) & 0xf;
1463 tf.protocol |= ATA_PROT_NODATA;
1464 tf.device |= ATA_LBA;
1466 tf.lbal = (new_sectors >> 0) & 0xff;
1467 tf.lbam = (new_sectors >> 8) & 0xff;
1468 tf.lbah = (new_sectors >> 16) & 0xff;
1470 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1472 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1473 "max address (err_mask=0x%x)\n", err_mask);
1474 if (err_mask == AC_ERR_DEV &&
1475 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1484 * ata_hpa_resize - Resize a device with an HPA set
1485 * @dev: Device to resize
1487 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1488 * it if required to the full size of the media. The caller must check
1489 * the drive has the HPA feature set enabled.
1492 * 0 on success, -errno on failure.
1494 static int ata_hpa_resize(struct ata_device *dev)
1496 struct ata_eh_context *ehc = &dev->link->eh_context;
1497 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1498 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1499 u64 sectors = ata_id_n_sectors(dev->id);
1503 /* do we need to do it? */
1504 if (dev->class != ATA_DEV_ATA ||
1505 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1506 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1509 /* read native max address */
1510 rc = ata_read_native_max_address(dev, &native_sectors);
1512 /* If device aborted the command or HPA isn't going to
1513 * be unlocked, skip HPA resizing.
1515 if (rc == -EACCES || !unlock_hpa) {
1516 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1517 "broken, skipping HPA handling\n");
1518 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1520 /* we can continue if device aborted the command */
1527 dev->n_native_sectors = native_sectors;
1529 /* nothing to do? */
1530 if (native_sectors <= sectors || !unlock_hpa) {
1531 if (!print_info || native_sectors == sectors)
1534 if (native_sectors > sectors)
1535 ata_dev_printk(dev, KERN_INFO,
1536 "HPA detected: current %llu, native %llu\n",
1537 (unsigned long long)sectors,
1538 (unsigned long long)native_sectors);
1539 else if (native_sectors < sectors)
1540 ata_dev_printk(dev, KERN_WARNING,
1541 "native sectors (%llu) is smaller than "
1543 (unsigned long long)native_sectors,
1544 (unsigned long long)sectors);
1548 /* let's unlock HPA */
1549 rc = ata_set_max_sectors(dev, native_sectors);
1550 if (rc == -EACCES) {
1551 /* if device aborted the command, skip HPA resizing */
1552 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1553 "(%llu -> %llu), skipping HPA handling\n",
1554 (unsigned long long)sectors,
1555 (unsigned long long)native_sectors);
1556 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1561 /* re-read IDENTIFY data */
1562 rc = ata_dev_reread_id(dev, 0);
1564 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1565 "data after HPA resizing\n");
1570 u64 new_sectors = ata_id_n_sectors(dev->id);
1571 ata_dev_printk(dev, KERN_INFO,
1572 "HPA unlocked: %llu -> %llu, native %llu\n",
1573 (unsigned long long)sectors,
1574 (unsigned long long)new_sectors,
1575 (unsigned long long)native_sectors);
1582 * ata_dump_id - IDENTIFY DEVICE info debugging output
1583 * @id: IDENTIFY DEVICE page to dump
1585 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1592 static inline void ata_dump_id(const u16 *id)
1594 DPRINTK("49==0x%04x "
1604 DPRINTK("80==0x%04x "
1614 DPRINTK("88==0x%04x "
1621 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1622 * @id: IDENTIFY data to compute xfer mask from
1624 * Compute the xfermask for this device. This is not as trivial
1625 * as it seems if we must consider early devices correctly.
1627 * FIXME: pre IDE drive timing (do we care ?).
1635 unsigned long ata_id_xfermask(const u16 *id)
1637 unsigned long pio_mask, mwdma_mask, udma_mask;
1639 /* Usual case. Word 53 indicates word 64 is valid */
1640 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1641 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1645 /* If word 64 isn't valid then Word 51 high byte holds
1646 * the PIO timing number for the maximum. Turn it into
1649 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1650 if (mode < 5) /* Valid PIO range */
1651 pio_mask = (2 << mode) - 1;
1655 /* But wait.. there's more. Design your standards by
1656 * committee and you too can get a free iordy field to
1657 * process. However its the speeds not the modes that
1658 * are supported... Note drivers using the timing API
1659 * will get this right anyway
1663 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1665 if (ata_id_is_cfa(id)) {
1667 * Process compact flash extended modes
1669 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1670 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1673 pio_mask |= (1 << 5);
1675 pio_mask |= (1 << 6);
1677 mwdma_mask |= (1 << 3);
1679 mwdma_mask |= (1 << 4);
1683 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1684 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1686 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1690 * ata_pio_queue_task - Queue port_task
1691 * @ap: The ata_port to queue port_task for
1692 * @data: data for @fn to use
1693 * @delay: delay time in msecs for workqueue function
1695 * Schedule @fn(@data) for execution after @delay jiffies using
1696 * port_task. There is one port_task per port and it's the
1697 * user(low level driver)'s responsibility to make sure that only
1698 * one task is active at any given time.
1700 * libata core layer takes care of synchronization between
1701 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1705 * Inherited from caller.
1707 void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1709 ap->port_task_data = data;
1711 /* may fail if ata_port_flush_task() in progress */
1712 queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1716 * ata_port_flush_task - Flush port_task
1717 * @ap: The ata_port to flush port_task for
1719 * After this function completes, port_task is guranteed not to
1720 * be running or scheduled.
1723 * Kernel thread context (may sleep)
1725 void ata_port_flush_task(struct ata_port *ap)
1729 cancel_rearming_delayed_work(&ap->port_task);
1731 if (ata_msg_ctl(ap))
1732 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1735 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1737 struct completion *waiting = qc->private_data;
1743 * ata_exec_internal_sg - execute libata internal command
1744 * @dev: Device to which the command is sent
1745 * @tf: Taskfile registers for the command and the result
1746 * @cdb: CDB for packet command
1747 * @dma_dir: Data tranfer direction of the command
1748 * @sgl: sg list for the data buffer of the command
1749 * @n_elem: Number of sg entries
1750 * @timeout: Timeout in msecs (0 for default)
1752 * Executes libata internal command with timeout. @tf contains
1753 * command on entry and result on return. Timeout and error
1754 * conditions are reported via return value. No recovery action
1755 * is taken after a command times out. It's caller's duty to
1756 * clean up after timeout.
1759 * None. Should be called with kernel context, might sleep.
1762 * Zero on success, AC_ERR_* mask on failure
1764 unsigned ata_exec_internal_sg(struct ata_device *dev,
1765 struct ata_taskfile *tf, const u8 *cdb,
1766 int dma_dir, struct scatterlist *sgl,
1767 unsigned int n_elem, unsigned long timeout)
1769 struct ata_link *link = dev->link;
1770 struct ata_port *ap = link->ap;
1771 u8 command = tf->command;
1772 int auto_timeout = 0;
1773 struct ata_queued_cmd *qc;
1774 unsigned int tag, preempted_tag;
1775 u32 preempted_sactive, preempted_qc_active;
1776 int preempted_nr_active_links;
1777 DECLARE_COMPLETION_ONSTACK(wait);
1778 unsigned long flags;
1779 unsigned int err_mask;
1782 spin_lock_irqsave(ap->lock, flags);
1784 /* no internal command while frozen */
1785 if (ap->pflags & ATA_PFLAG_FROZEN) {
1786 spin_unlock_irqrestore(ap->lock, flags);
1787 return AC_ERR_SYSTEM;
1790 /* initialize internal qc */
1792 /* XXX: Tag 0 is used for drivers with legacy EH as some
1793 * drivers choke if any other tag is given. This breaks
1794 * ata_tag_internal() test for those drivers. Don't use new
1795 * EH stuff without converting to it.
1797 if (ap->ops->error_handler)
1798 tag = ATA_TAG_INTERNAL;
1802 if (test_and_set_bit(tag, &ap->qc_allocated))
1804 qc = __ata_qc_from_tag(ap, tag);
1812 preempted_tag = link->active_tag;
1813 preempted_sactive = link->sactive;
1814 preempted_qc_active = ap->qc_active;
1815 preempted_nr_active_links = ap->nr_active_links;
1816 link->active_tag = ATA_TAG_POISON;
1819 ap->nr_active_links = 0;
1821 /* prepare & issue qc */
1824 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1825 qc->flags |= ATA_QCFLAG_RESULT_TF;
1826 qc->dma_dir = dma_dir;
1827 if (dma_dir != DMA_NONE) {
1828 unsigned int i, buflen = 0;
1829 struct scatterlist *sg;
1831 for_each_sg(sgl, sg, n_elem, i)
1832 buflen += sg->length;
1834 ata_sg_init(qc, sgl, n_elem);
1835 qc->nbytes = buflen;
1838 qc->private_data = &wait;
1839 qc->complete_fn = ata_qc_complete_internal;
1843 spin_unlock_irqrestore(ap->lock, flags);
1846 if (ata_probe_timeout)
1847 timeout = ata_probe_timeout * 1000;
1849 timeout = ata_internal_cmd_timeout(dev, command);
1854 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1856 ata_port_flush_task(ap);
1859 spin_lock_irqsave(ap->lock, flags);
1861 /* We're racing with irq here. If we lose, the
1862 * following test prevents us from completing the qc
1863 * twice. If we win, the port is frozen and will be
1864 * cleaned up by ->post_internal_cmd().
1866 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1867 qc->err_mask |= AC_ERR_TIMEOUT;
1869 if (ap->ops->error_handler)
1870 ata_port_freeze(ap);
1872 ata_qc_complete(qc);
1874 if (ata_msg_warn(ap))
1875 ata_dev_printk(dev, KERN_WARNING,
1876 "qc timeout (cmd 0x%x)\n", command);
1879 spin_unlock_irqrestore(ap->lock, flags);
1882 /* do post_internal_cmd */
1883 if (ap->ops->post_internal_cmd)
1884 ap->ops->post_internal_cmd(qc);
1886 /* perform minimal error analysis */
1887 if (qc->flags & ATA_QCFLAG_FAILED) {
1888 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1889 qc->err_mask |= AC_ERR_DEV;
1892 qc->err_mask |= AC_ERR_OTHER;
1894 if (qc->err_mask & ~AC_ERR_OTHER)
1895 qc->err_mask &= ~AC_ERR_OTHER;
1899 spin_lock_irqsave(ap->lock, flags);
1901 *tf = qc->result_tf;
1902 err_mask = qc->err_mask;
1905 link->active_tag = preempted_tag;
1906 link->sactive = preempted_sactive;
1907 ap->qc_active = preempted_qc_active;
1908 ap->nr_active_links = preempted_nr_active_links;
1910 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1911 * Until those drivers are fixed, we detect the condition
1912 * here, fail the command with AC_ERR_SYSTEM and reenable the
1915 * Note that this doesn't change any behavior as internal
1916 * command failure results in disabling the device in the
1917 * higher layer for LLDDs without new reset/EH callbacks.
1919 * Kill the following code as soon as those drivers are fixed.
1921 if (ap->flags & ATA_FLAG_DISABLED) {
1922 err_mask |= AC_ERR_SYSTEM;
1926 spin_unlock_irqrestore(ap->lock, flags);
1928 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1929 ata_internal_cmd_timed_out(dev, command);
1935 * ata_exec_internal - execute libata internal command
1936 * @dev: Device to which the command is sent
1937 * @tf: Taskfile registers for the command and the result
1938 * @cdb: CDB for packet command
1939 * @dma_dir: Data tranfer direction of the command
1940 * @buf: Data buffer of the command
1941 * @buflen: Length of data buffer
1942 * @timeout: Timeout in msecs (0 for default)
1944 * Wrapper around ata_exec_internal_sg() which takes simple
1945 * buffer instead of sg list.
1948 * None. Should be called with kernel context, might sleep.
1951 * Zero on success, AC_ERR_* mask on failure
1953 unsigned ata_exec_internal(struct ata_device *dev,
1954 struct ata_taskfile *tf, const u8 *cdb,
1955 int dma_dir, void *buf, unsigned int buflen,
1956 unsigned long timeout)
1958 struct scatterlist *psg = NULL, sg;
1959 unsigned int n_elem = 0;
1961 if (dma_dir != DMA_NONE) {
1963 sg_init_one(&sg, buf, buflen);
1968 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1973 * ata_do_simple_cmd - execute simple internal command
1974 * @dev: Device to which the command is sent
1975 * @cmd: Opcode to execute
1977 * Execute a 'simple' command, that only consists of the opcode
1978 * 'cmd' itself, without filling any other registers
1981 * Kernel thread context (may sleep).
1984 * Zero on success, AC_ERR_* mask on failure
1986 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1988 struct ata_taskfile tf;
1990 ata_tf_init(dev, &tf);
1993 tf.flags |= ATA_TFLAG_DEVICE;
1994 tf.protocol = ATA_PROT_NODATA;
1996 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2000 * ata_pio_need_iordy - check if iordy needed
2003 * Check if the current speed of the device requires IORDY. Used
2004 * by various controllers for chip configuration.
2006 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2008 /* Don't set IORDY if we're preparing for reset. IORDY may
2009 * lead to controller lock up on certain controllers if the
2010 * port is not occupied. See bko#11703 for details.
2012 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
2014 /* Controller doesn't support IORDY. Probably a pointless
2015 * check as the caller should know this.
2017 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
2019 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
2020 if (ata_id_is_cfa(adev->id)
2021 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
2023 /* PIO3 and higher it is mandatory */
2024 if (adev->pio_mode > XFER_PIO_2)
2026 /* We turn it on when possible */
2027 if (ata_id_has_iordy(adev->id))
2033 * ata_pio_mask_no_iordy - Return the non IORDY mask
2036 * Compute the highest mode possible if we are not using iordy. Return
2037 * -1 if no iordy mode is available.
2039 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2041 /* If we have no drive specific rule, then PIO 2 is non IORDY */
2042 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
2043 u16 pio = adev->id[ATA_ID_EIDE_PIO];
2044 /* Is the speed faster than the drive allows non IORDY ? */
2046 /* This is cycle times not frequency - watch the logic! */
2047 if (pio > 240) /* PIO2 is 240nS per cycle */
2048 return 3 << ATA_SHIFT_PIO;
2049 return 7 << ATA_SHIFT_PIO;
2052 return 3 << ATA_SHIFT_PIO;
2056 * ata_do_dev_read_id - default ID read method
2058 * @tf: proposed taskfile
2061 * Issue the identify taskfile and hand back the buffer containing
2062 * identify data. For some RAID controllers and for pre ATA devices
2063 * this function is wrapped or replaced by the driver
2065 unsigned int ata_do_dev_read_id(struct ata_device *dev,
2066 struct ata_taskfile *tf, u16 *id)
2068 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2069 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2073 * ata_dev_read_id - Read ID data from the specified device
2074 * @dev: target device
2075 * @p_class: pointer to class of the target device (may be changed)
2076 * @flags: ATA_READID_* flags
2077 * @id: buffer to read IDENTIFY data into
2079 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2080 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2081 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2082 * for pre-ATA4 drives.
2084 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2085 * now we abort if we hit that case.
2088 * Kernel thread context (may sleep)
2091 * 0 on success, -errno otherwise.
2093 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2094 unsigned int flags, u16 *id)
2096 struct ata_port *ap = dev->link->ap;
2097 unsigned int class = *p_class;
2098 struct ata_taskfile tf;
2099 unsigned int err_mask = 0;
2101 bool is_semb = class == ATA_DEV_SEMB;
2102 int may_fallback = 1, tried_spinup = 0;
2105 if (ata_msg_ctl(ap))
2106 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2109 ata_tf_init(dev, &tf);
2113 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
2115 tf.command = ATA_CMD_ID_ATA;
2118 tf.command = ATA_CMD_ID_ATAPI;
2122 reason = "unsupported class";
2126 tf.protocol = ATA_PROT_PIO;
2128 /* Some devices choke if TF registers contain garbage. Make
2129 * sure those are properly initialized.
2131 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2133 /* Device presence detection is unreliable on some
2134 * controllers. Always poll IDENTIFY if available.
2136 tf.flags |= ATA_TFLAG_POLLING;
2138 if (ap->ops->read_id)
2139 err_mask = ap->ops->read_id(dev, &tf, id);
2141 err_mask = ata_do_dev_read_id(dev, &tf, id);
2144 if (err_mask & AC_ERR_NODEV_HINT) {
2145 ata_dev_printk(dev, KERN_DEBUG,
2146 "NODEV after polling detection\n");
2151 ata_dev_printk(dev, KERN_INFO, "IDENTIFY failed on "
2152 "device w/ SEMB sig, disabled\n");
2153 /* SEMB is not supported yet */
2154 *p_class = ATA_DEV_SEMB_UNSUP;
2158 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
2159 /* Device or controller might have reported
2160 * the wrong device class. Give a shot at the
2161 * other IDENTIFY if the current one is
2162 * aborted by the device.
2167 if (class == ATA_DEV_ATA)
2168 class = ATA_DEV_ATAPI;
2170 class = ATA_DEV_ATA;
2174 /* Control reaches here iff the device aborted
2175 * both flavors of IDENTIFYs which happens
2176 * sometimes with phantom devices.
2178 ata_dev_printk(dev, KERN_DEBUG,
2179 "both IDENTIFYs aborted, assuming NODEV\n");
2184 reason = "I/O error";
2188 /* Falling back doesn't make sense if ID data was read
2189 * successfully at least once.
2193 swap_buf_le16(id, ATA_ID_WORDS);
2197 reason = "device reports invalid type";
2199 if (class == ATA_DEV_ATA) {
2200 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2203 if (ata_id_is_ata(id))
2207 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2210 * Drive powered-up in standby mode, and requires a specific
2211 * SET_FEATURES spin-up subcommand before it will accept
2212 * anything other than the original IDENTIFY command.
2214 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2215 if (err_mask && id[2] != 0x738c) {
2217 reason = "SPINUP failed";
2221 * If the drive initially returned incomplete IDENTIFY info,
2222 * we now must reissue the IDENTIFY command.
2224 if (id[2] == 0x37c8)
2228 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2230 * The exact sequence expected by certain pre-ATA4 drives is:
2232 * IDENTIFY (optional in early ATA)
2233 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2235 * Some drives were very specific about that exact sequence.
2237 * Note that ATA4 says lba is mandatory so the second check
2238 * should never trigger.
2240 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2241 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2244 reason = "INIT_DEV_PARAMS failed";
2248 /* current CHS translation info (id[53-58]) might be
2249 * changed. reread the identify device info.
2251 flags &= ~ATA_READID_POSTRESET;
2261 if (ata_msg_warn(ap))
2262 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2263 "(%s, err_mask=0x%x)\n", reason, err_mask);
2267 static int ata_do_link_spd_horkage(struct ata_device *dev)
2269 struct ata_link *plink = ata_dev_phys_link(dev);
2270 u32 target, target_limit;
2272 if (!sata_scr_valid(plink))
2275 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2280 target_limit = (1 << target) - 1;
2282 /* if already on stricter limit, no need to push further */
2283 if (plink->sata_spd_limit <= target_limit)
2286 plink->sata_spd_limit = target_limit;
2288 /* Request another EH round by returning -EAGAIN if link is
2289 * going faster than the target speed. Forward progress is
2290 * guaranteed by setting sata_spd_limit to target_limit above.
2292 if (plink->sata_spd > target) {
2293 ata_dev_printk(dev, KERN_INFO,
2294 "applying link speed limit horkage to %s\n",
2295 sata_spd_string(target));
2301 static inline u8 ata_dev_knobble(struct ata_device *dev)
2303 struct ata_port *ap = dev->link->ap;
2305 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2308 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2311 static int ata_dev_config_ncq(struct ata_device *dev,
2312 char *desc, size_t desc_sz)
2314 struct ata_port *ap = dev->link->ap;
2315 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2316 unsigned int err_mask;
2319 if (!ata_id_has_ncq(dev->id)) {
2323 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2324 snprintf(desc, desc_sz, "NCQ (not used)");
2327 if (ap->flags & ATA_FLAG_NCQ) {
2328 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2329 dev->flags |= ATA_DFLAG_NCQ;
2332 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2333 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2334 ata_id_has_fpdma_aa(dev->id)) {
2335 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2338 ata_dev_printk(dev, KERN_ERR, "failed to enable AA"
2339 "(error_mask=0x%x)\n", err_mask);
2340 if (err_mask != AC_ERR_DEV) {
2341 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2348 if (hdepth >= ddepth)
2349 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2351 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2357 * ata_dev_configure - Configure the specified ATA/ATAPI device
2358 * @dev: Target device to configure
2360 * Configure @dev according to @dev->id. Generic and low-level
2361 * driver specific fixups are also applied.
2364 * Kernel thread context (may sleep)
2367 * 0 on success, -errno otherwise
2369 int ata_dev_configure(struct ata_device *dev)
2371 struct ata_port *ap = dev->link->ap;
2372 struct ata_eh_context *ehc = &dev->link->eh_context;
2373 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2374 const u16 *id = dev->id;
2375 unsigned long xfer_mask;
2376 char revbuf[7]; /* XYZ-99\0 */
2377 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2378 char modelbuf[ATA_ID_PROD_LEN+1];
2381 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2382 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
2387 if (ata_msg_probe(ap))
2388 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2391 dev->horkage |= ata_dev_blacklisted(dev);
2392 ata_force_horkage(dev);
2394 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2395 ata_dev_printk(dev, KERN_INFO,
2396 "unsupported device, disabling\n");
2397 ata_dev_disable(dev);
2401 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2402 dev->class == ATA_DEV_ATAPI) {
2403 ata_dev_printk(dev, KERN_WARNING,
2404 "WARNING: ATAPI is %s, device ignored.\n",
2405 atapi_enabled ? "not supported with this driver"
2407 ata_dev_disable(dev);
2411 rc = ata_do_link_spd_horkage(dev);
2415 /* let ACPI work its magic */
2416 rc = ata_acpi_on_devcfg(dev);
2420 /* massage HPA, do it early as it might change IDENTIFY data */
2421 rc = ata_hpa_resize(dev);
2425 /* print device capabilities */
2426 if (ata_msg_probe(ap))
2427 ata_dev_printk(dev, KERN_DEBUG,
2428 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2429 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2431 id[49], id[82], id[83], id[84],
2432 id[85], id[86], id[87], id[88]);
2434 /* initialize to-be-configured parameters */
2435 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2436 dev->max_sectors = 0;
2442 dev->multi_count = 0;
2445 * common ATA, ATAPI feature tests
2448 /* find max transfer mode; for printk only */
2449 xfer_mask = ata_id_xfermask(id);
2451 if (ata_msg_probe(ap))
2454 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2455 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2458 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2461 /* ATA-specific feature tests */
2462 if (dev->class == ATA_DEV_ATA) {
2463 if (ata_id_is_cfa(id)) {
2464 /* CPRM may make this media unusable */
2465 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2466 ata_dev_printk(dev, KERN_WARNING,
2467 "supports DRM functions and may "
2468 "not be fully accessable.\n");
2469 snprintf(revbuf, 7, "CFA");
2471 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2472 /* Warn the user if the device has TPM extensions */
2473 if (ata_id_has_tpm(id))
2474 ata_dev_printk(dev, KERN_WARNING,
2475 "supports DRM functions and may "
2476 "not be fully accessable.\n");
2479 dev->n_sectors = ata_id_n_sectors(id);
2481 /* get current R/W Multiple count setting */
2482 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2483 unsigned int max = dev->id[47] & 0xff;
2484 unsigned int cnt = dev->id[59] & 0xff;
2485 /* only recognize/allow powers of two here */
2486 if (is_power_of_2(max) && is_power_of_2(cnt))
2488 dev->multi_count = cnt;
2491 if (ata_id_has_lba(id)) {
2492 const char *lba_desc;
2496 dev->flags |= ATA_DFLAG_LBA;
2497 if (ata_id_has_lba48(id)) {
2498 dev->flags |= ATA_DFLAG_LBA48;
2501 if (dev->n_sectors >= (1UL << 28) &&
2502 ata_id_has_flush_ext(id))
2503 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2507 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2511 /* print device info to dmesg */
2512 if (ata_msg_drv(ap) && print_info) {
2513 ata_dev_printk(dev, KERN_INFO,
2514 "%s: %s, %s, max %s\n",
2515 revbuf, modelbuf, fwrevbuf,
2516 ata_mode_string(xfer_mask));
2517 ata_dev_printk(dev, KERN_INFO,
2518 "%Lu sectors, multi %u: %s %s\n",
2519 (unsigned long long)dev->n_sectors,
2520 dev->multi_count, lba_desc, ncq_desc);
2525 /* Default translation */
2526 dev->cylinders = id[1];
2528 dev->sectors = id[6];
2530 if (ata_id_current_chs_valid(id)) {
2531 /* Current CHS translation is valid. */
2532 dev->cylinders = id[54];
2533 dev->heads = id[55];
2534 dev->sectors = id[56];
2537 /* print device info to dmesg */
2538 if (ata_msg_drv(ap) && print_info) {
2539 ata_dev_printk(dev, KERN_INFO,
2540 "%s: %s, %s, max %s\n",
2541 revbuf, modelbuf, fwrevbuf,
2542 ata_mode_string(xfer_mask));
2543 ata_dev_printk(dev, KERN_INFO,
2544 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2545 (unsigned long long)dev->n_sectors,
2546 dev->multi_count, dev->cylinders,
2547 dev->heads, dev->sectors);
2554 /* ATAPI-specific feature tests */
2555 else if (dev->class == ATA_DEV_ATAPI) {
2556 const char *cdb_intr_string = "";
2557 const char *atapi_an_string = "";
2558 const char *dma_dir_string = "";
2561 rc = atapi_cdb_len(id);
2562 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2563 if (ata_msg_warn(ap))
2564 ata_dev_printk(dev, KERN_WARNING,
2565 "unsupported CDB len\n");
2569 dev->cdb_len = (unsigned int) rc;
2571 /* Enable ATAPI AN if both the host and device have
2572 * the support. If PMP is attached, SNTF is required
2573 * to enable ATAPI AN to discern between PHY status
2574 * changed notifications and ATAPI ANs.
2576 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2577 (!sata_pmp_attached(ap) ||
2578 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2579 unsigned int err_mask;
2581 /* issue SET feature command to turn this on */
2582 err_mask = ata_dev_set_feature(dev,
2583 SETFEATURES_SATA_ENABLE, SATA_AN);
2585 ata_dev_printk(dev, KERN_ERR,
2586 "failed to enable ATAPI AN "
2587 "(err_mask=0x%x)\n", err_mask);
2589 dev->flags |= ATA_DFLAG_AN;
2590 atapi_an_string = ", ATAPI AN";
2594 if (ata_id_cdb_intr(dev->id)) {
2595 dev->flags |= ATA_DFLAG_CDB_INTR;
2596 cdb_intr_string = ", CDB intr";
2599 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2600 dev->flags |= ATA_DFLAG_DMADIR;
2601 dma_dir_string = ", DMADIR";
2604 /* print device info to dmesg */
2605 if (ata_msg_drv(ap) && print_info)
2606 ata_dev_printk(dev, KERN_INFO,
2607 "ATAPI: %s, %s, max %s%s%s%s\n",
2609 ata_mode_string(xfer_mask),
2610 cdb_intr_string, atapi_an_string,
2614 /* determine max_sectors */
2615 dev->max_sectors = ATA_MAX_SECTORS;
2616 if (dev->flags & ATA_DFLAG_LBA48)
2617 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2619 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2620 if (ata_id_has_hipm(dev->id))
2621 dev->flags |= ATA_DFLAG_HIPM;
2622 if (ata_id_has_dipm(dev->id))
2623 dev->flags |= ATA_DFLAG_DIPM;
2626 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2628 if (ata_dev_knobble(dev)) {
2629 if (ata_msg_drv(ap) && print_info)
2630 ata_dev_printk(dev, KERN_INFO,
2631 "applying bridge limits\n");
2632 dev->udma_mask &= ATA_UDMA5;
2633 dev->max_sectors = ATA_MAX_SECTORS;
2636 if ((dev->class == ATA_DEV_ATAPI) &&
2637 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2638 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2639 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2642 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2643 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2646 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2647 dev->horkage |= ATA_HORKAGE_IPM;
2649 /* reset link pm_policy for this port to no pm */
2650 ap->pm_policy = MAX_PERFORMANCE;
2653 if (ap->ops->dev_config)
2654 ap->ops->dev_config(dev);
2656 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2657 /* Let the user know. We don't want to disallow opens for
2658 rescue purposes, or in case the vendor is just a blithering
2659 idiot. Do this after the dev_config call as some controllers
2660 with buggy firmware may want to avoid reporting false device
2664 ata_dev_printk(dev, KERN_WARNING,
2665 "Drive reports diagnostics failure. This may indicate a drive\n");
2666 ata_dev_printk(dev, KERN_WARNING,
2667 "fault or invalid emulation. Contact drive vendor for information.\n");
2671 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2672 ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2673 "firmware update to be fully functional.\n");
2674 ata_dev_printk(dev, KERN_WARNING, " contact the vendor "
2675 "or visit http://ata.wiki.kernel.org.\n");
2681 if (ata_msg_probe(ap))
2682 ata_dev_printk(dev, KERN_DEBUG,
2683 "%s: EXIT, err\n", __func__);
2688 * ata_cable_40wire - return 40 wire cable type
2691 * Helper method for drivers which want to hardwire 40 wire cable
2695 int ata_cable_40wire(struct ata_port *ap)
2697 return ATA_CBL_PATA40;
2701 * ata_cable_80wire - return 80 wire cable type
2704 * Helper method for drivers which want to hardwire 80 wire cable
2708 int ata_cable_80wire(struct ata_port *ap)
2710 return ATA_CBL_PATA80;
2714 * ata_cable_unknown - return unknown PATA cable.
2717 * Helper method for drivers which have no PATA cable detection.
2720 int ata_cable_unknown(struct ata_port *ap)
2722 return ATA_CBL_PATA_UNK;
2726 * ata_cable_ignore - return ignored PATA cable.
2729 * Helper method for drivers which don't use cable type to limit
2732 int ata_cable_ignore(struct ata_port *ap)
2734 return ATA_CBL_PATA_IGN;
2738 * ata_cable_sata - return SATA cable type
2741 * Helper method for drivers which have SATA cables
2744 int ata_cable_sata(struct ata_port *ap)
2746 return ATA_CBL_SATA;
2750 * ata_bus_probe - Reset and probe ATA bus
2753 * Master ATA bus probing function. Initiates a hardware-dependent
2754 * bus reset, then attempts to identify any devices found on
2758 * PCI/etc. bus probe sem.
2761 * Zero on success, negative errno otherwise.
2764 int ata_bus_probe(struct ata_port *ap)
2766 unsigned int classes[ATA_MAX_DEVICES];
2767 int tries[ATA_MAX_DEVICES];
2769 struct ata_device *dev;
2773 ata_for_each_dev(dev, &ap->link, ALL)
2774 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2777 ata_for_each_dev(dev, &ap->link, ALL) {
2778 /* If we issue an SRST then an ATA drive (not ATAPI)
2779 * may change configuration and be in PIO0 timing. If
2780 * we do a hard reset (or are coming from power on)
2781 * this is true for ATA or ATAPI. Until we've set a
2782 * suitable controller mode we should not touch the
2783 * bus as we may be talking too fast.
2785 dev->pio_mode = XFER_PIO_0;
2787 /* If the controller has a pio mode setup function
2788 * then use it to set the chipset to rights. Don't
2789 * touch the DMA setup as that will be dealt with when
2790 * configuring devices.
2792 if (ap->ops->set_piomode)
2793 ap->ops->set_piomode(ap, dev);
2796 /* reset and determine device classes */
2797 ap->ops->phy_reset(ap);
2799 ata_for_each_dev(dev, &ap->link, ALL) {
2800 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2801 dev->class != ATA_DEV_UNKNOWN)
2802 classes[dev->devno] = dev->class;
2804 classes[dev->devno] = ATA_DEV_NONE;
2806 dev->class = ATA_DEV_UNKNOWN;
2811 /* read IDENTIFY page and configure devices. We have to do the identify
2812 specific sequence bass-ackwards so that PDIAG- is released by
2815 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2816 if (tries[dev->devno])
2817 dev->class = classes[dev->devno];
2819 if (!ata_dev_enabled(dev))
2822 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2828 /* Now ask for the cable type as PDIAG- should have been released */
2829 if (ap->ops->cable_detect)
2830 ap->cbl = ap->ops->cable_detect(ap);
2832 /* We may have SATA bridge glue hiding here irrespective of
2833 * the reported cable types and sensed types. When SATA
2834 * drives indicate we have a bridge, we don't know which end
2835 * of the link the bridge is which is a problem.
2837 ata_for_each_dev(dev, &ap->link, ENABLED)
2838 if (ata_id_is_sata(dev->id))
2839 ap->cbl = ATA_CBL_SATA;
2841 /* After the identify sequence we can now set up the devices. We do
2842 this in the normal order so that the user doesn't get confused */
2844 ata_for_each_dev(dev, &ap->link, ENABLED) {
2845 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2846 rc = ata_dev_configure(dev);
2847 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2852 /* configure transfer mode */
2853 rc = ata_set_mode(&ap->link, &dev);
2857 ata_for_each_dev(dev, &ap->link, ENABLED)
2860 /* no device present, disable port */
2861 ata_port_disable(ap);
2865 tries[dev->devno]--;
2869 /* eeek, something went very wrong, give up */
2870 tries[dev->devno] = 0;
2874 /* give it just one more chance */
2875 tries[dev->devno] = min(tries[dev->devno], 1);
2877 if (tries[dev->devno] == 1) {
2878 /* This is the last chance, better to slow
2879 * down than lose it.
2881 sata_down_spd_limit(&ap->link, 0);
2882 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2886 if (!tries[dev->devno])
2887 ata_dev_disable(dev);
2893 * ata_port_probe - Mark port as enabled
2894 * @ap: Port for which we indicate enablement
2896 * Modify @ap data structure such that the system
2897 * thinks that the entire port is enabled.
2899 * LOCKING: host lock, or some other form of
2903 void ata_port_probe(struct ata_port *ap)
2905 ap->flags &= ~ATA_FLAG_DISABLED;
2909 * sata_print_link_status - Print SATA link status
2910 * @link: SATA link to printk link status about
2912 * This function prints link speed and status of a SATA link.
2917 static void sata_print_link_status(struct ata_link *link)
2919 u32 sstatus, scontrol, tmp;
2921 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2923 sata_scr_read(link, SCR_CONTROL, &scontrol);
2925 if (ata_phys_link_online(link)) {
2926 tmp = (sstatus >> 4) & 0xf;
2927 ata_link_printk(link, KERN_INFO,
2928 "SATA link up %s (SStatus %X SControl %X)\n",
2929 sata_spd_string(tmp), sstatus, scontrol);
2931 ata_link_printk(link, KERN_INFO,
2932 "SATA link down (SStatus %X SControl %X)\n",
2938 * ata_dev_pair - return other device on cable
2941 * Obtain the other device on the same cable, or if none is
2942 * present NULL is returned
2945 struct ata_device *ata_dev_pair(struct ata_device *adev)
2947 struct ata_link *link = adev->link;
2948 struct ata_device *pair = &link->device[1 - adev->devno];
2949 if (!ata_dev_enabled(pair))
2955 * ata_port_disable - Disable port.
2956 * @ap: Port to be disabled.
2958 * Modify @ap data structure such that the system
2959 * thinks that the entire port is disabled, and should
2960 * never attempt to probe or communicate with devices
2963 * LOCKING: host lock, or some other form of
2967 void ata_port_disable(struct ata_port *ap)
2969 ap->link.device[0].class = ATA_DEV_NONE;
2970 ap->link.device[1].class = ATA_DEV_NONE;
2971 ap->flags |= ATA_FLAG_DISABLED;
2975 * sata_down_spd_limit - adjust SATA spd limit downward
2976 * @link: Link to adjust SATA spd limit for
2977 * @spd_limit: Additional limit
2979 * Adjust SATA spd limit of @link downward. Note that this
2980 * function only adjusts the limit. The change must be applied
2981 * using sata_set_spd().
2983 * If @spd_limit is non-zero, the speed is limited to equal to or
2984 * lower than @spd_limit if such speed is supported. If
2985 * @spd_limit is slower than any supported speed, only the lowest
2986 * supported speed is allowed.
2989 * Inherited from caller.
2992 * 0 on success, negative errno on failure
2994 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2996 u32 sstatus, spd, mask;
2999 if (!sata_scr_valid(link))
3002 /* If SCR can be read, use it to determine the current SPD.
3003 * If not, use cached value in link->sata_spd.
3005 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
3006 if (rc == 0 && ata_sstatus_online(sstatus))
3007 spd = (sstatus >> 4) & 0xf;
3009 spd = link->sata_spd;
3011 mask = link->sata_spd_limit;
3015 /* unconditionally mask off the highest bit */
3016 bit = fls(mask) - 1;
3017 mask &= ~(1 << bit);
3019 /* Mask off all speeds higher than or equal to the current
3020 * one. Force 1.5Gbps if current SPD is not available.
3023 mask &= (1 << (spd - 1)) - 1;
3027 /* were we already at the bottom? */
3032 if (mask & ((1 << spd_limit) - 1))
3033 mask &= (1 << spd_limit) - 1;
3035 bit = ffs(mask) - 1;
3040 link->sata_spd_limit = mask;
3042 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
3043 sata_spd_string(fls(mask)));
3048 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3050 struct ata_link *host_link = &link->ap->link;
3051 u32 limit, target, spd;
3053 limit = link->sata_spd_limit;
3055 /* Don't configure downstream link faster than upstream link.
3056 * It doesn't speed up anything and some PMPs choke on such
3059 if (!ata_is_host_link(link) && host_link->sata_spd)
3060 limit &= (1 << host_link->sata_spd) - 1;
3062 if (limit == UINT_MAX)
3065 target = fls(limit);
3067 spd = (*scontrol >> 4) & 0xf;
3068 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3070 return spd != target;
3074 * sata_set_spd_needed - is SATA spd configuration needed
3075 * @link: Link in question
3077 * Test whether the spd limit in SControl matches
3078 * @link->sata_spd_limit. This function is used to determine
3079 * whether hardreset is necessary to apply SATA spd
3083 * Inherited from caller.
3086 * 1 if SATA spd configuration is needed, 0 otherwise.
3088 static int sata_set_spd_needed(struct ata_link *link)
3092 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3095 return __sata_set_spd_needed(link, &scontrol);
3099 * sata_set_spd - set SATA spd according to spd limit
3100 * @link: Link to set SATA spd for
3102 * Set SATA spd of @link according to sata_spd_limit.
3105 * Inherited from caller.
3108 * 0 if spd doesn't need to be changed, 1 if spd has been
3109 * changed. Negative errno if SCR registers are inaccessible.
3111 int sata_set_spd(struct ata_link *link)
3116 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3119 if (!__sata_set_spd_needed(link, &scontrol))
3122 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3129 * This mode timing computation functionality is ported over from
3130 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3133 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3134 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3135 * for UDMA6, which is currently supported only by Maxtor drives.
3137 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3140 static const struct ata_timing ata_timing[] = {
3141 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3142 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3143 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3144 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3145 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3146 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3147 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3148 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
3150 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3151 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3152 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
3154 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3155 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3156 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3157 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3158 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
3160 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3161 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3162 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3163 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3164 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3165 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3166 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3167 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
3172 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3173 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
3175 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3177 q->setup = EZ(t->setup * 1000, T);
3178 q->act8b = EZ(t->act8b * 1000, T);
3179 q->rec8b = EZ(t->rec8b * 1000, T);
3180 q->cyc8b = EZ(t->cyc8b * 1000, T);
3181 q->active = EZ(t->active * 1000, T);
3182 q->recover = EZ(t->recover * 1000, T);
3183 q->dmack_hold = EZ(t->dmack_hold * 1000, T);
3184 q->cycle = EZ(t->cycle * 1000, T);
3185 q->udma = EZ(t->udma * 1000, UT);
3188 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3189 struct ata_timing *m, unsigned int what)
3191 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3192 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3193 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3194 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3195 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3196 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3197 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3198 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3199 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3202 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3204 const struct ata_timing *t = ata_timing;
3206 while (xfer_mode > t->mode)
3209 if (xfer_mode == t->mode)
3214 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3215 struct ata_timing *t, int T, int UT)
3217 const u16 *id = adev->id;
3218 const struct ata_timing *s;
3219 struct ata_timing p;
3225 if (!(s = ata_timing_find_mode(speed)))
3228 memcpy(t, s, sizeof(*s));
3231 * If the drive is an EIDE drive, it can tell us it needs extended
3232 * PIO/MW_DMA cycle timing.
3235 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
3236 memset(&p, 0, sizeof(p));
3238 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3239 if (speed <= XFER_PIO_2)
3240 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3241 else if ((speed <= XFER_PIO_4) ||
3242 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3243 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3244 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3245 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3247 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3251 * Convert the timing to bus clock counts.
3254 ata_timing_quantize(t, t, T, UT);
3257 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3258 * S.M.A.R.T * and some other commands. We have to ensure that the
3259 * DMA cycle timing is slower/equal than the fastest PIO timing.
3262 if (speed > XFER_PIO_6) {
3263 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3264 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3268 * Lengthen active & recovery time so that cycle time is correct.
3271 if (t->act8b + t->rec8b < t->cyc8b) {
3272 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3273 t->rec8b = t->cyc8b - t->act8b;
3276 if (t->active + t->recover < t->cycle) {
3277 t->active += (t->cycle - (t->active + t->recover)) / 2;
3278 t->recover = t->cycle - t->active;
3281 /* In a few cases quantisation may produce enough errors to
3282 leave t->cycle too low for the sum of active and recovery
3283 if so we must correct this */
3284 if (t->active + t->recover > t->cycle)
3285 t->cycle = t->active + t->recover;
3291 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3292 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3293 * @cycle: cycle duration in ns
3295 * Return matching xfer mode for @cycle. The returned mode is of
3296 * the transfer type specified by @xfer_shift. If @cycle is too
3297 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3298 * than the fastest known mode, the fasted mode is returned.
3304 * Matching xfer_mode, 0xff if no match found.
3306 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3308 u8 base_mode = 0xff, last_mode = 0xff;
3309 const struct ata_xfer_ent *ent;
3310 const struct ata_timing *t;
3312 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3313 if (ent->shift == xfer_shift)
3314 base_mode = ent->base;
3316 for (t = ata_timing_find_mode(base_mode);
3317 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3318 unsigned short this_cycle;
3320 switch (xfer_shift) {
3322 case ATA_SHIFT_MWDMA:
3323 this_cycle = t->cycle;
3325 case ATA_SHIFT_UDMA:
3326 this_cycle = t->udma;
3332 if (cycle > this_cycle)
3335 last_mode = t->mode;
3342 * ata_down_xfermask_limit - adjust dev xfer masks downward
3343 * @dev: Device to adjust xfer masks
3344 * @sel: ATA_DNXFER_* selector
3346 * Adjust xfer masks of @dev downward. Note that this function
3347 * does not apply the change. Invoking ata_set_mode() afterwards
3348 * will apply the limit.
3351 * Inherited from caller.
3354 * 0 on success, negative errno on failure
3356 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3359 unsigned long orig_mask, xfer_mask;
3360 unsigned long pio_mask, mwdma_mask, udma_mask;
3363 quiet = !!(sel & ATA_DNXFER_QUIET);
3364 sel &= ~ATA_DNXFER_QUIET;
3366 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3369 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3372 case ATA_DNXFER_PIO:
3373 highbit = fls(pio_mask) - 1;
3374 pio_mask &= ~(1 << highbit);
3377 case ATA_DNXFER_DMA:
3379 highbit = fls(udma_mask) - 1;
3380 udma_mask &= ~(1 << highbit);
3383 } else if (mwdma_mask) {
3384 highbit = fls(mwdma_mask) - 1;
3385 mwdma_mask &= ~(1 << highbit);
3391 case ATA_DNXFER_40C:
3392 udma_mask &= ATA_UDMA_MASK_40C;
3395 case ATA_DNXFER_FORCE_PIO0:
3397 case ATA_DNXFER_FORCE_PIO:
3406 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3408 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3412 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3413 snprintf(buf, sizeof(buf), "%s:%s",
3414 ata_mode_string(xfer_mask),
3415 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3417 snprintf(buf, sizeof(buf), "%s",
3418 ata_mode_string(xfer_mask));
3420 ata_dev_printk(dev, KERN_WARNING,
3421 "limiting speed to %s\n", buf);
3424 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3430 static int ata_dev_set_mode(struct ata_device *dev)
3432 struct ata_port *ap = dev->link->ap;
3433 struct ata_eh_context *ehc = &dev->link->eh_context;
3434 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3435 const char *dev_err_whine = "";
3436 int ign_dev_err = 0;
3437 unsigned int err_mask = 0;
3440 dev->flags &= ~ATA_DFLAG_PIO;
3441 if (dev->xfer_shift == ATA_SHIFT_PIO)
3442 dev->flags |= ATA_DFLAG_PIO;
3444 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3445 dev_err_whine = " (SET_XFERMODE skipped)";
3448 ata_dev_printk(dev, KERN_WARNING,
3449 "NOSETXFER but PATA detected - can't "
3450 "skip SETXFER, might malfunction\n");
3451 err_mask = ata_dev_set_xfermode(dev);
3454 if (err_mask & ~AC_ERR_DEV)
3458 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3459 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3460 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3464 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3465 /* Old CFA may refuse this command, which is just fine */
3466 if (ata_id_is_cfa(dev->id))
3468 /* Catch several broken garbage emulations plus some pre
3470 if (ata_id_major_version(dev->id) == 0 &&
3471 dev->pio_mode <= XFER_PIO_2)
3473 /* Some very old devices and some bad newer ones fail
3474 any kind of SET_XFERMODE request but support PIO0-2
3475 timings and no IORDY */
3476 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3479 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3480 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3481 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3482 dev->dma_mode == XFER_MW_DMA_0 &&
3483 (dev->id[63] >> 8) & 1)
3486 /* if the device is actually configured correctly, ignore dev err */
3487 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3490 if (err_mask & AC_ERR_DEV) {
3494 dev_err_whine = " (device error ignored)";
3497 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3498 dev->xfer_shift, (int)dev->xfer_mode);
3500 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3501 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3507 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3508 "(err_mask=0x%x)\n", err_mask);
3513 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3514 * @link: link on which timings will be programmed
3515 * @r_failed_dev: out parameter for failed device
3517 * Standard implementation of the function used to tune and set
3518 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3519 * ata_dev_set_mode() fails, pointer to the failing device is
3520 * returned in @r_failed_dev.
3523 * PCI/etc. bus probe sem.
3526 * 0 on success, negative errno otherwise
3529 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3531 struct ata_port *ap = link->ap;
3532 struct ata_device *dev;
3533 int rc = 0, used_dma = 0, found = 0;
3535 /* step 1: calculate xfer_mask */
3536 ata_for_each_dev(dev, link, ENABLED) {
3537 unsigned long pio_mask, dma_mask;
3538 unsigned int mode_mask;
3540 mode_mask = ATA_DMA_MASK_ATA;
3541 if (dev->class == ATA_DEV_ATAPI)
3542 mode_mask = ATA_DMA_MASK_ATAPI;
3543 else if (ata_id_is_cfa(dev->id))
3544 mode_mask = ATA_DMA_MASK_CFA;
3546 ata_dev_xfermask(dev);
3547 ata_force_xfermask(dev);
3549 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3550 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3552 if (libata_dma_mask & mode_mask)
3553 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3557 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3558 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3561 if (ata_dma_enabled(dev))
3567 /* step 2: always set host PIO timings */
3568 ata_for_each_dev(dev, link, ENABLED) {
3569 if (dev->pio_mode == 0xff) {
3570 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3575 dev->xfer_mode = dev->pio_mode;
3576 dev->xfer_shift = ATA_SHIFT_PIO;
3577 if (ap->ops->set_piomode)
3578 ap->ops->set_piomode(ap, dev);
3581 /* step 3: set host DMA timings */
3582 ata_for_each_dev(dev, link, ENABLED) {
3583 if (!ata_dma_enabled(dev))
3586 dev->xfer_mode = dev->dma_mode;
3587 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3588 if (ap->ops->set_dmamode)
3589 ap->ops->set_dmamode(ap, dev);
3592 /* step 4: update devices' xfer mode */
3593 ata_for_each_dev(dev, link, ENABLED) {
3594 rc = ata_dev_set_mode(dev);
3599 /* Record simplex status. If we selected DMA then the other
3600 * host channels are not permitted to do so.
3602 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3603 ap->host->simplex_claimed = ap;
3607 *r_failed_dev = dev;
3612 * ata_wait_ready - wait for link to become ready
3613 * @link: link to be waited on
3614 * @deadline: deadline jiffies for the operation
3615 * @check_ready: callback to check link readiness
3617 * Wait for @link to become ready. @check_ready should return
3618 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3619 * link doesn't seem to be occupied, other errno for other error
3622 * Transient -ENODEV conditions are allowed for
3623 * ATA_TMOUT_FF_WAIT.
3629 * 0 if @linke is ready before @deadline; otherwise, -errno.
3631 int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3632 int (*check_ready)(struct ata_link *link))
3634 unsigned long start = jiffies;
3635 unsigned long nodev_deadline;
3638 /* choose which 0xff timeout to use, read comment in libata.h */
3639 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3640 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3642 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3644 /* Slave readiness can't be tested separately from master. On
3645 * M/S emulation configuration, this function should be called
3646 * only on the master and it will handle both master and slave.
3648 WARN_ON(link == link->ap->slave_link);
3650 if (time_after(nodev_deadline, deadline))
3651 nodev_deadline = deadline;
3654 unsigned long now = jiffies;
3657 ready = tmp = check_ready(link);
3662 * -ENODEV could be transient. Ignore -ENODEV if link
3663 * is online. Also, some SATA devices take a long
3664 * time to clear 0xff after reset. Wait for
3665 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3668 * Note that some PATA controllers (pata_ali) explode
3669 * if status register is read more than once when
3670 * there's no device attached.
3672 if (ready == -ENODEV) {
3673 if (ata_link_online(link))
3675 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3676 !ata_link_offline(link) &&
3677 time_before(now, nodev_deadline))
3683 if (time_after(now, deadline))
3686 if (!warned && time_after(now, start + 5 * HZ) &&
3687 (deadline - now > 3 * HZ)) {
3688 ata_link_printk(link, KERN_WARNING,
3689 "link is slow to respond, please be patient "
3690 "(ready=%d)\n", tmp);
3699 * ata_wait_after_reset - wait for link to become ready after reset
3700 * @link: link to be waited on
3701 * @deadline: deadline jiffies for the operation
3702 * @check_ready: callback to check link readiness
3704 * Wait for @link to become ready after reset.
3710 * 0 if @linke is ready before @deadline; otherwise, -errno.
3712 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3713 int (*check_ready)(struct ata_link *link))
3715 msleep(ATA_WAIT_AFTER_RESET);
3717 return ata_wait_ready(link, deadline, check_ready);
3721 * sata_link_debounce - debounce SATA phy status
3722 * @link: ATA link to debounce SATA phy status for
3723 * @params: timing parameters { interval, duratinon, timeout } in msec
3724 * @deadline: deadline jiffies for the operation
3726 * Make sure SStatus of @link reaches stable state, determined by
3727 * holding the same value where DET is not 1 for @duration polled
3728 * every @interval, before @timeout. Timeout constraints the
3729 * beginning of the stable state. Because DET gets stuck at 1 on
3730 * some controllers after hot unplugging, this functions waits
3731 * until timeout then returns 0 if DET is stable at 1.
3733 * @timeout is further limited by @deadline. The sooner of the
3737 * Kernel thread context (may sleep)
3740 * 0 on success, -errno on failure.
3742 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3743 unsigned long deadline)
3745 unsigned long interval = params[0];
3746 unsigned long duration = params[1];
3747 unsigned long last_jiffies, t;
3751 t = ata_deadline(jiffies, params[2]);
3752 if (time_before(t, deadline))
3755 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3760 last_jiffies = jiffies;
3764 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3770 if (cur == 1 && time_before(jiffies, deadline))
3772 if (time_after(jiffies,
3773 ata_deadline(last_jiffies, duration)))
3778 /* unstable, start over */
3780 last_jiffies = jiffies;
3782 /* Check deadline. If debouncing failed, return
3783 * -EPIPE to tell upper layer to lower link speed.
3785 if (time_after(jiffies, deadline))
3791 * sata_link_resume - resume SATA link
3792 * @link: ATA link to resume SATA
3793 * @params: timing parameters { interval, duratinon, timeout } in msec
3794 * @deadline: deadline jiffies for the operation
3796 * Resume SATA phy @link and debounce it.
3799 * Kernel thread context (may sleep)
3802 * 0 on success, -errno on failure.
3804 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3805 unsigned long deadline)
3807 int tries = ATA_LINK_RESUME_TRIES;
3808 u32 scontrol, serror;
3811 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3815 * Writes to SControl sometimes get ignored under certain
3816 * controllers (ata_piix SIDPR). Make sure DET actually is
3820 scontrol = (scontrol & 0x0f0) | 0x300;
3821 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3824 * Some PHYs react badly if SStatus is pounded
3825 * immediately after resuming. Delay 200ms before
3830 /* is SControl restored correctly? */
3831 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3833 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3835 if ((scontrol & 0xf0f) != 0x300) {
3836 ata_link_printk(link, KERN_ERR,
3837 "failed to resume link (SControl %X)\n",
3842 if (tries < ATA_LINK_RESUME_TRIES)
3843 ata_link_printk(link, KERN_WARNING,
3844 "link resume succeeded after %d retries\n",
3845 ATA_LINK_RESUME_TRIES - tries);
3847 if ((rc = sata_link_debounce(link, params, deadline)))
3850 /* clear SError, some PHYs require this even for SRST to work */
3851 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3852 rc = sata_scr_write(link, SCR_ERROR, serror);
3854 return rc != -EINVAL ? rc : 0;
3858 * ata_std_prereset - prepare for reset
3859 * @link: ATA link to be reset
3860 * @deadline: deadline jiffies for the operation
3862 * @link is about to be reset. Initialize it. Failure from
3863 * prereset makes libata abort whole reset sequence and give up
3864 * that port, so prereset should be best-effort. It does its
3865 * best to prepare for reset sequence but if things go wrong, it
3866 * should just whine, not fail.
3869 * Kernel thread context (may sleep)
3872 * 0 on success, -errno otherwise.
3874 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3876 struct ata_port *ap = link->ap;
3877 struct ata_eh_context *ehc = &link->eh_context;
3878 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3881 /* if we're about to do hardreset, nothing more to do */
3882 if (ehc->i.action & ATA_EH_HARDRESET)
3885 /* if SATA, resume link */
3886 if (ap->flags & ATA_FLAG_SATA) {
3887 rc = sata_link_resume(link, timing, deadline);
3888 /* whine about phy resume failure but proceed */
3889 if (rc && rc != -EOPNOTSUPP)
3890 ata_link_printk(link, KERN_WARNING, "failed to resume "
3891 "link for reset (errno=%d)\n", rc);
3894 /* no point in trying softreset on offline link */
3895 if (ata_phys_link_offline(link))
3896 ehc->i.action &= ~ATA_EH_SOFTRESET;
3902 * sata_link_hardreset - reset link via SATA phy reset
3903 * @link: link to reset
3904 * @timing: timing parameters { interval, duratinon, timeout } in msec
3905 * @deadline: deadline jiffies for the operation
3906 * @online: optional out parameter indicating link onlineness
3907 * @check_ready: optional callback to check link readiness
3909 * SATA phy-reset @link using DET bits of SControl register.
3910 * After hardreset, link readiness is waited upon using
3911 * ata_wait_ready() if @check_ready is specified. LLDs are
3912 * allowed to not specify @check_ready and wait itself after this
3913 * function returns. Device classification is LLD's
3916 * *@online is set to one iff reset succeeded and @link is online
3920 * Kernel thread context (may sleep)
3923 * 0 on success, -errno otherwise.
3925 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3926 unsigned long deadline,
3927 bool *online, int (*check_ready)(struct ata_link *))
3937 if (sata_set_spd_needed(link)) {
3938 /* SATA spec says nothing about how to reconfigure
3939 * spd. To be on the safe side, turn off phy during
3940 * reconfiguration. This works for at least ICH7 AHCI
3943 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3946 scontrol = (scontrol & 0x0f0) | 0x304;
3948 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3954 /* issue phy wake/reset */
3955 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3958 scontrol = (scontrol & 0x0f0) | 0x301;
3960 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3963 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3964 * 10.4.2 says at least 1 ms.
3968 /* bring link back */
3969 rc = sata_link_resume(link, timing, deadline);
3972 /* if link is offline nothing more to do */
3973 if (ata_phys_link_offline(link))
3976 /* Link is online. From this point, -ENODEV too is an error. */
3980 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
3981 /* If PMP is supported, we have to do follow-up SRST.
3982 * Some PMPs don't send D2H Reg FIS after hardreset if
3983 * the first port is empty. Wait only for
3984 * ATA_TMOUT_PMP_SRST_WAIT.
3987 unsigned long pmp_deadline;
3989 pmp_deadline = ata_deadline(jiffies,
3990 ATA_TMOUT_PMP_SRST_WAIT);
3991 if (time_after(pmp_deadline, deadline))
3992 pmp_deadline = deadline;
3993 ata_wait_ready(link, pmp_deadline, check_ready);
4001 rc = ata_wait_ready(link, deadline, check_ready);
4003 if (rc && rc != -EAGAIN) {
4004 /* online is set iff link is online && reset succeeded */
4007 ata_link_printk(link, KERN_ERR,
4008 "COMRESET failed (errno=%d)\n", rc);
4010 DPRINTK("EXIT, rc=%d\n", rc);
4015 * sata_std_hardreset - COMRESET w/o waiting or classification
4016 * @link: link to reset
4017 * @class: resulting class of attached device
4018 * @deadline: deadline jiffies for the operation
4020 * Standard SATA COMRESET w/o waiting or classification.
4023 * Kernel thread context (may sleep)
4026 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4028 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4029 unsigned long deadline)
4031 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4036 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
4037 return online ? -EAGAIN : rc;
4041 * ata_std_postreset - standard postreset callback
4042 * @link: the target ata_link
4043 * @classes: classes of attached devices
4045 * This function is invoked after a successful reset. Note that
4046 * the device might have been reset more than once using
4047 * different reset methods before postreset is invoked.
4050 * Kernel thread context (may sleep)
4052 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4058 /* reset complete, clear SError */
4059 if (!sata_scr_read(link, SCR_ERROR, &serror))
4060 sata_scr_write(link, SCR_ERROR, serror);
4062 /* print link status */
4063 sata_print_link_status(link);
4069 * ata_dev_same_device - Determine whether new ID matches configured device
4070 * @dev: device to compare against
4071 * @new_class: class of the new device
4072 * @new_id: IDENTIFY page of the new device
4074 * Compare @new_class and @new_id against @dev and determine
4075 * whether @dev is the device indicated by @new_class and
4082 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4084 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4087 const u16 *old_id = dev->id;
4088 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4089 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4091 if (dev->class != new_class) {
4092 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4093 dev->class, new_class);
4097 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4098 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4099 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4100 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4102 if (strcmp(model[0], model[1])) {
4103 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4104 "'%s' != '%s'\n", model[0], model[1]);
4108 if (strcmp(serial[0], serial[1])) {
4109 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4110 "'%s' != '%s'\n", serial[0], serial[1]);
4118 * ata_dev_reread_id - Re-read IDENTIFY data
4119 * @dev: target ATA device
4120 * @readid_flags: read ID flags
4122 * Re-read IDENTIFY page and make sure @dev is still attached to
4126 * Kernel thread context (may sleep)
4129 * 0 on success, negative errno otherwise
4131 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4133 unsigned int class = dev->class;
4134 u16 *id = (void *)dev->link->ap->sector_buf;
4138 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4142 /* is the device still there? */
4143 if (!ata_dev_same_device(dev, class, id))
4146 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4151 * ata_dev_revalidate - Revalidate ATA device
4152 * @dev: device to revalidate
4153 * @new_class: new class code
4154 * @readid_flags: read ID flags
4156 * Re-read IDENTIFY page, make sure @dev is still attached to the
4157 * port and reconfigure it according to the new IDENTIFY page.
4160 * Kernel thread context (may sleep)
4163 * 0 on success, negative errno otherwise
4165 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4166 unsigned int readid_flags)
4168 u64 n_sectors = dev->n_sectors;
4169 u64 n_native_sectors = dev->n_native_sectors;
4172 if (!ata_dev_enabled(dev))
4175 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4176 if (ata_class_enabled(new_class) &&
4177 new_class != ATA_DEV_ATA &&
4178 new_class != ATA_DEV_ATAPI &&
4179 new_class != ATA_DEV_SEMB) {
4180 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4181 dev->class, new_class);
4187 rc = ata_dev_reread_id(dev, readid_flags);
4191 /* configure device according to the new ID */
4192 rc = ata_dev_configure(dev);
4196 /* verify n_sectors hasn't changed */
4197 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4198 dev->n_sectors == n_sectors)
4201 /* n_sectors has changed */
4202 ata_dev_printk(dev, KERN_WARNING, "n_sectors mismatch %llu != %llu\n",
4203 (unsigned long long)n_sectors,
4204 (unsigned long long)dev->n_sectors);
4207 * Something could have caused HPA to be unlocked
4208 * involuntarily. If n_native_sectors hasn't changed and the
4209 * new size matches it, keep the device.
4211 if (dev->n_native_sectors == n_native_sectors &&
4212 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4213 ata_dev_printk(dev, KERN_WARNING,
4214 "new n_sectors matches native, probably "
4215 "late HPA unlock, continuing\n");
4216 /* keep using the old n_sectors */
4217 dev->n_sectors = n_sectors;
4222 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4223 * unlocking HPA in those cases.
4225 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4227 if (dev->n_native_sectors == n_native_sectors &&
4228 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4229 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4230 ata_dev_printk(dev, KERN_WARNING,
4231 "old n_sectors matches native, probably "
4232 "late HPA lock, will try to unlock HPA\n");
4233 /* try unlocking HPA */
4234 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4239 /* restore original n_[native_]sectors and fail */
4240 dev->n_native_sectors = n_native_sectors;
4241 dev->n_sectors = n_sectors;
4243 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4247 struct ata_blacklist_entry {
4248 const char *model_num;
4249 const char *model_rev;
4250 unsigned long horkage;
4253 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4254 /* Devices with DMA related problems under Linux */
4255 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4256 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4257 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4258 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4259 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4260 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4261 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4262 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4263 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4264 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
4265 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
4266 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4267 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4268 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4269 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4270 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4271 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
4272 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
4273 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4274 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4275 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4276 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4277 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4278 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4279 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4280 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4281 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4282 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4283 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4284 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4285 /* Odd clown on sil3726/4726 PMPs */
4286 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
4288 /* Weird ATAPI devices */
4289 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4290 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
4292 /* Devices we expect to fail diagnostics */
4294 /* Devices where NCQ should be avoided */
4296 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4297 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4298 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4299 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4301 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4302 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4303 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4304 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4305 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
4307 /* Seagate NCQ + FLUSH CACHE firmware bug */
4308 { "ST31500341AS", "SD15", ATA_HORKAGE_NONCQ |
4309 ATA_HORKAGE_FIRMWARE_WARN },
4310 { "ST31500341AS", "SD16", ATA_HORKAGE_NONCQ |
4311 ATA_HORKAGE_FIRMWARE_WARN },
4312 { "ST31500341AS", "SD17", ATA_HORKAGE_NONCQ |
4313 ATA_HORKAGE_FIRMWARE_WARN },
4314 { "ST31500341AS", "SD18", ATA_HORKAGE_NONCQ |
4315 ATA_HORKAGE_FIRMWARE_WARN },
4316 { "ST31500341AS", "SD19", ATA_HORKAGE_NONCQ |
4317 ATA_HORKAGE_FIRMWARE_WARN },
4319 { "ST31000333AS", "SD15", ATA_HORKAGE_NONCQ |
4320 ATA_HORKAGE_FIRMWARE_WARN },
4321 { "ST31000333AS", "SD16", ATA_HORKAGE_NONCQ |
4322 ATA_HORKAGE_FIRMWARE_WARN },
4323 { "ST31000333AS", "SD17", ATA_HORKAGE_NONCQ |
4324 ATA_HORKAGE_FIRMWARE_WARN },
4325 { "ST31000333AS", "SD18", ATA_HORKAGE_NONCQ |
4326 ATA_HORKAGE_FIRMWARE_WARN },
4327 { "ST31000333AS", "SD19", ATA_HORKAGE_NONCQ |
4328 ATA_HORKAGE_FIRMWARE_WARN },
4330 { "ST3640623AS", "SD15", ATA_HORKAGE_NONCQ |
4331 ATA_HORKAGE_FIRMWARE_WARN },
4332 { "ST3640623AS", "SD16", ATA_HORKAGE_NONCQ |
4333 ATA_HORKAGE_FIRMWARE_WARN },
4334 { "ST3640623AS", "SD17", ATA_HORKAGE_NONCQ |
4335 ATA_HORKAGE_FIRMWARE_WARN },
4336 { "ST3640623AS", "SD18", ATA_HORKAGE_NONCQ |
4337 ATA_HORKAGE_FIRMWARE_WARN },
4338 { "ST3640623AS", "SD19", ATA_HORKAGE_NONCQ |
4339 ATA_HORKAGE_FIRMWARE_WARN },
4341 { "ST3640323AS", "SD15", ATA_HORKAGE_NONCQ |
4342 ATA_HORKAGE_FIRMWARE_WARN },
4343 { "ST3640323AS", "SD16", ATA_HORKAGE_NONCQ |
4344 ATA_HORKAGE_FIRMWARE_WARN },
4345 { "ST3640323AS", "SD17", ATA_HORKAGE_NONCQ |
4346 ATA_HORKAGE_FIRMWARE_WARN },
4347 { "ST3640323AS", "SD18", ATA_HORKAGE_NONCQ |
4348 ATA_HORKAGE_FIRMWARE_WARN },
4349 { "ST3640323AS", "SD19", ATA_HORKAGE_NONCQ |
4350 ATA_HORKAGE_FIRMWARE_WARN },
4352 { "ST3320813AS", "SD15", ATA_HORKAGE_NONCQ |
4353 ATA_HORKAGE_FIRMWARE_WARN },
4354 { "ST3320813AS", "SD16", ATA_HORKAGE_NONCQ |
4355 ATA_HORKAGE_FIRMWARE_WARN },
4356 { "ST3320813AS", "SD17", ATA_HORKAGE_NONCQ |
4357 ATA_HORKAGE_FIRMWARE_WARN },
4358 { "ST3320813AS", "SD18", ATA_HORKAGE_NONCQ |
4359 ATA_HORKAGE_FIRMWARE_WARN },
4360 { "ST3320813AS", "SD19", ATA_HORKAGE_NONCQ |
4361 ATA_HORKAGE_FIRMWARE_WARN },
4363 { "ST3320613AS", "SD15", ATA_HORKAGE_NONCQ |
4364 ATA_HORKAGE_FIRMWARE_WARN },
4365 { "ST3320613AS", "SD16", ATA_HORKAGE_NONCQ |
4366 ATA_HORKAGE_FIRMWARE_WARN },
4367 { "ST3320613AS", "SD17", ATA_HORKAGE_NONCQ |
4368 ATA_HORKAGE_FIRMWARE_WARN },
4369 { "ST3320613AS", "SD18", ATA_HORKAGE_NONCQ |
4370 ATA_HORKAGE_FIRMWARE_WARN },
4371 { "ST3320613AS", "SD19", ATA_HORKAGE_NONCQ |
4372 ATA_HORKAGE_FIRMWARE_WARN },
4374 /* Blacklist entries taken from Silicon Image 3124/3132
4375 Windows driver .inf file - also several Linux problem reports */
4376 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4377 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4378 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4380 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4381 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4383 /* devices which puke on READ_NATIVE_MAX */
4384 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4385 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4386 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4387 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4389 /* this one allows HPA unlocking but fails IOs on the area */
4390 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4392 /* Devices which report 1 sector over size HPA */
4393 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4394 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4395 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4397 /* Devices which get the IVB wrong */
4398 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4399 /* Maybe we should just blacklist TSSTcorp... */
4400 { "TSSTcorp CDDVDW SH-S202H", "SB00", ATA_HORKAGE_IVB, },
4401 { "TSSTcorp CDDVDW SH-S202H", "SB01", ATA_HORKAGE_IVB, },
4402 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
4403 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
4404 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
4405 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
4407 /* Devices that do not need bridging limits applied */
4408 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4410 /* Devices which aren't very happy with higher link speeds */
4411 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4414 * Devices which choke on SETXFER. Applies only if both the
4415 * device and controller are SATA.
4417 { "PIONEER DVD-RW DVRTD08", "1.00", ATA_HORKAGE_NOSETXFER },
4423 static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4429 * check for trailing wildcard: *\0
4431 p = strchr(patt, wildchar);
4432 if (p && ((*(p + 1)) == 0))
4443 return strncmp(patt, name, len);
4446 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4448 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4449 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4450 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4452 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4453 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4455 while (ad->model_num) {
4456 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
4457 if (ad->model_rev == NULL)
4459 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
4467 static int ata_dma_blacklisted(const struct ata_device *dev)
4469 /* We don't support polling DMA.
4470 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4471 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4473 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4474 (dev->flags & ATA_DFLAG_CDB_INTR))
4476 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4480 * ata_is_40wire - check drive side detection
4483 * Perform drive side detection decoding, allowing for device vendors
4484 * who can't follow the documentation.
4487 static int ata_is_40wire(struct ata_device *dev)
4489 if (dev->horkage & ATA_HORKAGE_IVB)
4490 return ata_drive_40wire_relaxed(dev->id);
4491 return ata_drive_40wire(dev->id);
4495 * cable_is_40wire - 40/80/SATA decider
4496 * @ap: port to consider
4498 * This function encapsulates the policy for speed management
4499 * in one place. At the moment we don't cache the result but
4500 * there is a good case for setting ap->cbl to the result when
4501 * we are called with unknown cables (and figuring out if it
4502 * impacts hotplug at all).
4504 * Return 1 if the cable appears to be 40 wire.
4507 static int cable_is_40wire(struct ata_port *ap)
4509 struct ata_link *link;
4510 struct ata_device *dev;
4512 /* If the controller thinks we are 40 wire, we are. */
4513 if (ap->cbl == ATA_CBL_PATA40)
4516 /* If the controller thinks we are 80 wire, we are. */
4517 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4520 /* If the system is known to be 40 wire short cable (eg
4521 * laptop), then we allow 80 wire modes even if the drive
4524 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4527 /* If the controller doesn't know, we scan.
4529 * Note: We look for all 40 wire detects at this point. Any
4530 * 80 wire detect is taken to be 80 wire cable because
4531 * - in many setups only the one drive (slave if present) will
4532 * give a valid detect
4533 * - if you have a non detect capable drive you don't want it
4534 * to colour the choice
4536 ata_for_each_link(link, ap, EDGE) {
4537 ata_for_each_dev(dev, link, ENABLED) {
4538 if (!ata_is_40wire(dev))
4546 * ata_dev_xfermask - Compute supported xfermask of the given device
4547 * @dev: Device to compute xfermask for
4549 * Compute supported xfermask of @dev and store it in
4550 * dev->*_mask. This function is responsible for applying all
4551 * known limits including host controller limits, device
4557 static void ata_dev_xfermask(struct ata_device *dev)
4559 struct ata_link *link = dev->link;
4560 struct ata_port *ap = link->ap;
4561 struct ata_host *host = ap->host;
4562 unsigned long xfer_mask;
4564 /* controller modes available */
4565 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4566 ap->mwdma_mask, ap->udma_mask);
4568 /* drive modes available */
4569 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4570 dev->mwdma_mask, dev->udma_mask);
4571 xfer_mask &= ata_id_xfermask(dev->id);
4574 * CFA Advanced TrueIDE timings are not allowed on a shared
4577 if (ata_dev_pair(dev)) {
4578 /* No PIO5 or PIO6 */
4579 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4580 /* No MWDMA3 or MWDMA 4 */
4581 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4584 if (ata_dma_blacklisted(dev)) {
4585 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4586 ata_dev_printk(dev, KERN_WARNING,
4587 "device is on DMA blacklist, disabling DMA\n");
4590 if ((host->flags & ATA_HOST_SIMPLEX) &&
4591 host->simplex_claimed && host->simplex_claimed != ap) {
4592 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4593 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4594 "other device, disabling DMA\n");
4597 if (ap->flags & ATA_FLAG_NO_IORDY)
4598 xfer_mask &= ata_pio_mask_no_iordy(dev);
4600 if (ap->ops->mode_filter)
4601 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4603 /* Apply cable rule here. Don't apply it early because when
4604 * we handle hot plug the cable type can itself change.
4605 * Check this last so that we know if the transfer rate was
4606 * solely limited by the cable.
4607 * Unknown or 80 wire cables reported host side are checked
4608 * drive side as well. Cases where we know a 40wire cable
4609 * is used safely for 80 are not checked here.
4611 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4612 /* UDMA/44 or higher would be available */
4613 if (cable_is_40wire(ap)) {
4614 ata_dev_printk(dev, KERN_WARNING,
4615 "limited to UDMA/33 due to 40-wire cable\n");
4616 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4619 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4620 &dev->mwdma_mask, &dev->udma_mask);
4624 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4625 * @dev: Device to which command will be sent
4627 * Issue SET FEATURES - XFER MODE command to device @dev
4631 * PCI/etc. bus probe sem.
4634 * 0 on success, AC_ERR_* mask otherwise.
4637 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4639 struct ata_taskfile tf;
4640 unsigned int err_mask;
4642 /* set up set-features taskfile */
4643 DPRINTK("set features - xfer mode\n");
4645 /* Some controllers and ATAPI devices show flaky interrupt
4646 * behavior after setting xfer mode. Use polling instead.
4648 ata_tf_init(dev, &tf);
4649 tf.command = ATA_CMD_SET_FEATURES;
4650 tf.feature = SETFEATURES_XFER;
4651 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4652 tf.protocol = ATA_PROT_NODATA;
4653 /* If we are using IORDY we must send the mode setting command */
4654 if (ata_pio_need_iordy(dev))
4655 tf.nsect = dev->xfer_mode;
4656 /* If the device has IORDY and the controller does not - turn it off */
4657 else if (ata_id_has_iordy(dev->id))
4659 else /* In the ancient relic department - skip all of this */
4662 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4664 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4668 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4669 * @dev: Device to which command will be sent
4670 * @enable: Whether to enable or disable the feature
4671 * @feature: The sector count represents the feature to set
4673 * Issue SET FEATURES - SATA FEATURES command to device @dev
4674 * on port @ap with sector count
4677 * PCI/etc. bus probe sem.
4680 * 0 on success, AC_ERR_* mask otherwise.
4682 static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4685 struct ata_taskfile tf;
4686 unsigned int err_mask;
4688 /* set up set-features taskfile */
4689 DPRINTK("set features - SATA features\n");
4691 ata_tf_init(dev, &tf);
4692 tf.command = ATA_CMD_SET_FEATURES;
4693 tf.feature = enable;
4694 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4695 tf.protocol = ATA_PROT_NODATA;
4698 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4700 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4705 * ata_dev_init_params - Issue INIT DEV PARAMS command
4706 * @dev: Device to which command will be sent
4707 * @heads: Number of heads (taskfile parameter)
4708 * @sectors: Number of sectors (taskfile parameter)
4711 * Kernel thread context (may sleep)
4714 * 0 on success, AC_ERR_* mask otherwise.
4716 static unsigned int ata_dev_init_params(struct ata_device *dev,
4717 u16 heads, u16 sectors)
4719 struct ata_taskfile tf;
4720 unsigned int err_mask;
4722 /* Number of sectors per track 1-255. Number of heads 1-16 */
4723 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4724 return AC_ERR_INVALID;
4726 /* set up init dev params taskfile */
4727 DPRINTK("init dev params \n");
4729 ata_tf_init(dev, &tf);
4730 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4731 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4732 tf.protocol = ATA_PROT_NODATA;
4734 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4736 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4737 /* A clean abort indicates an original or just out of spec drive
4738 and we should continue as we issue the setup based on the
4739 drive reported working geometry */
4740 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4743 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4748 * ata_sg_clean - Unmap DMA memory associated with command
4749 * @qc: Command containing DMA memory to be released
4751 * Unmap all mapped DMA memory associated with this command.
4754 * spin_lock_irqsave(host lock)
4756 void ata_sg_clean(struct ata_queued_cmd *qc)
4758 struct ata_port *ap = qc->ap;
4759 struct scatterlist *sg = qc->sg;
4760 int dir = qc->dma_dir;
4762 WARN_ON_ONCE(sg == NULL);
4764 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4767 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4769 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4774 * atapi_check_dma - Check whether ATAPI DMA can be supported
4775 * @qc: Metadata associated with taskfile to check
4777 * Allow low-level driver to filter ATA PACKET commands, returning
4778 * a status indicating whether or not it is OK to use DMA for the
4779 * supplied PACKET command.
4782 * spin_lock_irqsave(host lock)
4784 * RETURNS: 0 when ATAPI DMA can be used
4787 int atapi_check_dma(struct ata_queued_cmd *qc)
4789 struct ata_port *ap = qc->ap;
4791 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4792 * few ATAPI devices choke on such DMA requests.
4794 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4795 unlikely(qc->nbytes & 15))
4798 if (ap->ops->check_atapi_dma)
4799 return ap->ops->check_atapi_dma(qc);
4805 * ata_std_qc_defer - Check whether a qc needs to be deferred
4806 * @qc: ATA command in question
4808 * Non-NCQ commands cannot run with any other command, NCQ or
4809 * not. As upper layer only knows the queue depth, we are
4810 * responsible for maintaining exclusion. This function checks
4811 * whether a new command @qc can be issued.
4814 * spin_lock_irqsave(host lock)
4817 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4819 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4821 struct ata_link *link = qc->dev->link;
4823 if (qc->tf.protocol == ATA_PROT_NCQ) {
4824 if (!ata_tag_valid(link->active_tag))
4827 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4831 return ATA_DEFER_LINK;
4834 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4837 * ata_sg_init - Associate command with scatter-gather table.
4838 * @qc: Command to be associated
4839 * @sg: Scatter-gather table.
4840 * @n_elem: Number of elements in s/g table.
4842 * Initialize the data-related elements of queued_cmd @qc
4843 * to point to a scatter-gather table @sg, containing @n_elem
4847 * spin_lock_irqsave(host lock)
4849 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4850 unsigned int n_elem)
4853 qc->n_elem = n_elem;
4858 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4859 * @qc: Command with scatter-gather table to be mapped.
4861 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4864 * spin_lock_irqsave(host lock)
4867 * Zero on success, negative on error.
4870 static int ata_sg_setup(struct ata_queued_cmd *qc)
4872 struct ata_port *ap = qc->ap;
4873 unsigned int n_elem;
4875 VPRINTK("ENTER, ata%u\n", ap->print_id);
4877 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4881 DPRINTK("%d sg elements mapped\n", n_elem);
4882 qc->orig_n_elem = qc->n_elem;
4883 qc->n_elem = n_elem;
4884 qc->flags |= ATA_QCFLAG_DMAMAP;
4890 * swap_buf_le16 - swap halves of 16-bit words in place
4891 * @buf: Buffer to swap
4892 * @buf_words: Number of 16-bit words in buffer.
4894 * Swap halves of 16-bit words if needed to convert from
4895 * little-endian byte order to native cpu byte order, or
4899 * Inherited from caller.
4901 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4906 for (i = 0; i < buf_words; i++)
4907 buf[i] = le16_to_cpu(buf[i]);
4908 #endif /* __BIG_ENDIAN */
4912 * ata_qc_new - Request an available ATA command, for queueing
4919 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4921 struct ata_queued_cmd *qc = NULL;
4924 /* no command while frozen */
4925 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4928 /* the last tag is reserved for internal command. */
4929 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
4930 if (!test_and_set_bit(i, &ap->qc_allocated)) {
4931 qc = __ata_qc_from_tag(ap, i);
4942 * ata_qc_new_init - Request an available ATA command, and initialize it
4943 * @dev: Device from whom we request an available command structure
4949 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4951 struct ata_port *ap = dev->link->ap;
4952 struct ata_queued_cmd *qc;
4954 qc = ata_qc_new(ap);
4967 * ata_qc_free - free unused ata_queued_cmd
4968 * @qc: Command to complete
4970 * Designed to free unused ata_queued_cmd object
4971 * in case something prevents using it.
4974 * spin_lock_irqsave(host lock)
4976 void ata_qc_free(struct ata_queued_cmd *qc)
4978 struct ata_port *ap;
4981 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4986 if (likely(ata_tag_valid(tag))) {
4987 qc->tag = ATA_TAG_POISON;
4988 clear_bit(tag, &ap->qc_allocated);
4992 void __ata_qc_complete(struct ata_queued_cmd *qc)
4994 struct ata_port *ap;
4995 struct ata_link *link;
4997 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4998 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5000 link = qc->dev->link;
5002 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5005 /* command should be marked inactive atomically with qc completion */
5006 if (qc->tf.protocol == ATA_PROT_NCQ) {
5007 link->sactive &= ~(1 << qc->tag);
5009 ap->nr_active_links--;
5011 link->active_tag = ATA_TAG_POISON;
5012 ap->nr_active_links--;
5015 /* clear exclusive status */
5016 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5017 ap->excl_link == link))
5018 ap->excl_link = NULL;
5020 /* atapi: mark qc as inactive to prevent the interrupt handler
5021 * from completing the command twice later, before the error handler
5022 * is called. (when rc != 0 and atapi request sense is needed)
5024 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5025 ap->qc_active &= ~(1 << qc->tag);
5027 /* call completion callback */
5028 qc->complete_fn(qc);
5031 static void fill_result_tf(struct ata_queued_cmd *qc)
5033 struct ata_port *ap = qc->ap;
5035 qc->result_tf.flags = qc->tf.flags;
5036 ap->ops->qc_fill_rtf(qc);
5039 static void ata_verify_xfer(struct ata_queued_cmd *qc)
5041 struct ata_device *dev = qc->dev;
5043 if (ata_tag_internal(qc->tag))
5046 if (ata_is_nodata(qc->tf.protocol))
5049 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5052 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5056 * ata_qc_complete - Complete an active ATA command
5057 * @qc: Command to complete
5059 * Indicate to the mid and upper layers that an ATA
5060 * command has completed, with either an ok or not-ok status.
5063 * spin_lock_irqsave(host lock)
5065 void ata_qc_complete(struct ata_queued_cmd *qc)
5067 struct ata_port *ap = qc->ap;
5069 /* XXX: New EH and old EH use different mechanisms to
5070 * synchronize EH with regular execution path.
5072 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5073 * Normal execution path is responsible for not accessing a
5074 * failed qc. libata core enforces the rule by returning NULL
5075 * from ata_qc_from_tag() for failed qcs.
5077 * Old EH depends on ata_qc_complete() nullifying completion
5078 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5079 * not synchronize with interrupt handler. Only PIO task is
5082 if (ap->ops->error_handler) {
5083 struct ata_device *dev = qc->dev;
5084 struct ata_eh_info *ehi = &dev->link->eh_info;
5086 if (unlikely(qc->err_mask))
5087 qc->flags |= ATA_QCFLAG_FAILED;
5089 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5090 /* always fill result TF for failed qc */
5093 if (!ata_tag_internal(qc->tag))
5094 ata_qc_schedule_eh(qc);
5096 __ata_qc_complete(qc);
5100 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5102 /* read result TF if requested */
5103 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5106 /* Some commands need post-processing after successful
5109 switch (qc->tf.command) {
5110 case ATA_CMD_SET_FEATURES:
5111 if (qc->tf.feature != SETFEATURES_WC_ON &&
5112 qc->tf.feature != SETFEATURES_WC_OFF)
5115 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5116 case ATA_CMD_SET_MULTI: /* multi_count changed */
5117 /* revalidate device */
5118 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5119 ata_port_schedule_eh(ap);
5123 dev->flags |= ATA_DFLAG_SLEEPING;
5127 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5128 ata_verify_xfer(qc);
5130 __ata_qc_complete(qc);
5132 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5135 /* read result TF if failed or requested */
5136 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5139 __ata_qc_complete(qc);
5144 * ata_qc_complete_multiple - Complete multiple qcs successfully
5145 * @ap: port in question
5146 * @qc_active: new qc_active mask
5148 * Complete in-flight commands. This functions is meant to be
5149 * called from low-level driver's interrupt routine to complete
5150 * requests normally. ap->qc_active and @qc_active is compared
5151 * and commands are completed accordingly.
5154 * spin_lock_irqsave(host lock)
5157 * Number of completed commands on success, -errno otherwise.
5159 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5164 done_mask = ap->qc_active ^ qc_active;
5166 if (unlikely(done_mask & qc_active)) {
5167 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5168 "(%08x->%08x)\n", ap->qc_active, qc_active);
5173 struct ata_queued_cmd *qc;
5174 unsigned int tag = __ffs(done_mask);
5176 qc = ata_qc_from_tag(ap, tag);
5178 ata_qc_complete(qc);
5181 done_mask &= ~(1 << tag);
5188 * ata_qc_issue - issue taskfile to device
5189 * @qc: command to issue to device
5191 * Prepare an ATA command to submission to device.
5192 * This includes mapping the data into a DMA-able
5193 * area, filling in the S/G table, and finally
5194 * writing the taskfile to hardware, starting the command.
5197 * spin_lock_irqsave(host lock)
5199 void ata_qc_issue(struct ata_queued_cmd *qc)
5201 struct ata_port *ap = qc->ap;
5202 struct ata_link *link = qc->dev->link;
5203 u8 prot = qc->tf.protocol;
5205 /* Make sure only one non-NCQ command is outstanding. The
5206 * check is skipped for old EH because it reuses active qc to
5207 * request ATAPI sense.
5209 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5211 if (ata_is_ncq(prot)) {
5212 WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5215 ap->nr_active_links++;
5216 link->sactive |= 1 << qc->tag;
5218 WARN_ON_ONCE(link->sactive);
5220 ap->nr_active_links++;
5221 link->active_tag = qc->tag;
5224 qc->flags |= ATA_QCFLAG_ACTIVE;
5225 ap->qc_active |= 1 << qc->tag;
5227 /* We guarantee to LLDs that they will have at least one
5228 * non-zero sg if the command is a data command.
5230 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
5232 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5233 (ap->flags & ATA_FLAG_PIO_DMA)))
5234 if (ata_sg_setup(qc))
5237 /* if device is sleeping, schedule reset and abort the link */
5238 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5239 link->eh_info.action |= ATA_EH_RESET;
5240 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5241 ata_link_abort(link);
5245 ap->ops->qc_prep(qc);
5247 qc->err_mask |= ap->ops->qc_issue(qc);
5248 if (unlikely(qc->err_mask))
5253 qc->err_mask |= AC_ERR_SYSTEM;
5255 ata_qc_complete(qc);
5259 * sata_scr_valid - test whether SCRs are accessible
5260 * @link: ATA link to test SCR accessibility for
5262 * Test whether SCRs are accessible for @link.
5268 * 1 if SCRs are accessible, 0 otherwise.
5270 int sata_scr_valid(struct ata_link *link)
5272 struct ata_port *ap = link->ap;
5274 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5278 * sata_scr_read - read SCR register of the specified port
5279 * @link: ATA link to read SCR for
5281 * @val: Place to store read value
5283 * Read SCR register @reg of @link into *@val. This function is
5284 * guaranteed to succeed if @link is ap->link, the cable type of
5285 * the port is SATA and the port implements ->scr_read.
5288 * None if @link is ap->link. Kernel thread context otherwise.
5291 * 0 on success, negative errno on failure.
5293 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5295 if (ata_is_host_link(link)) {
5296 if (sata_scr_valid(link))
5297 return link->ap->ops->scr_read(link, reg, val);
5301 return sata_pmp_scr_read(link, reg, val);
5305 * sata_scr_write - write SCR register of the specified port
5306 * @link: ATA link to write SCR for
5307 * @reg: SCR to write
5308 * @val: value to write
5310 * Write @val to SCR register @reg of @link. This function is
5311 * guaranteed to succeed if @link is ap->link, the cable type of
5312 * the port is SATA and the port implements ->scr_read.
5315 * None if @link is ap->link. Kernel thread context otherwise.
5318 * 0 on success, negative errno on failure.
5320 int sata_scr_write(struct ata_link *link, int reg, u32 val)
5322 if (ata_is_host_link(link)) {
5323 if (sata_scr_valid(link))
5324 return link->ap->ops->scr_write(link, reg, val);
5328 return sata_pmp_scr_write(link, reg, val);
5332 * sata_scr_write_flush - write SCR register of the specified port and flush
5333 * @link: ATA link to write SCR for
5334 * @reg: SCR to write
5335 * @val: value to write
5337 * This function is identical to sata_scr_write() except that this
5338 * function performs flush after writing to the register.
5341 * None if @link is ap->link. Kernel thread context otherwise.
5344 * 0 on success, negative errno on failure.
5346 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5348 if (ata_is_host_link(link)) {
5351 if (sata_scr_valid(link)) {
5352 rc = link->ap->ops->scr_write(link, reg, val);
5354 rc = link->ap->ops->scr_read(link, reg, &val);
5360 return sata_pmp_scr_write(link, reg, val);
5364 * ata_phys_link_online - test whether the given link is online
5365 * @link: ATA link to test
5367 * Test whether @link is online. Note that this function returns
5368 * 0 if online status of @link cannot be obtained, so
5369 * ata_link_online(link) != !ata_link_offline(link).
5375 * True if the port online status is available and online.
5377 bool ata_phys_link_online(struct ata_link *link)
5381 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5382 ata_sstatus_online(sstatus))
5388 * ata_phys_link_offline - test whether the given link is offline
5389 * @link: ATA link to test
5391 * Test whether @link is offline. Note that this function
5392 * returns 0 if offline status of @link cannot be obtained, so
5393 * ata_link_online(link) != !ata_link_offline(link).
5399 * True if the port offline status is available and offline.
5401 bool ata_phys_link_offline(struct ata_link *link)
5405 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5406 !ata_sstatus_online(sstatus))
5412 * ata_link_online - test whether the given link is online
5413 * @link: ATA link to test
5415 * Test whether @link is online. This is identical to
5416 * ata_phys_link_online() when there's no slave link. When
5417 * there's a slave link, this function should only be called on
5418 * the master link and will return true if any of M/S links is
5425 * True if the port online status is available and online.
5427 bool ata_link_online(struct ata_link *link)
5429 struct ata_link *slave = link->ap->slave_link;
5431 WARN_ON(link == slave); /* shouldn't be called on slave link */
5433 return ata_phys_link_online(link) ||
5434 (slave && ata_phys_link_online(slave));
5438 * ata_link_offline - test whether the given link is offline
5439 * @link: ATA link to test
5441 * Test whether @link is offline. This is identical to
5442 * ata_phys_link_offline() when there's no slave link. When
5443 * there's a slave link, this function should only be called on
5444 * the master link and will return true if both M/S links are
5451 * True if the port offline status is available and offline.
5453 bool ata_link_offline(struct ata_link *link)
5455 struct ata_link *slave = link->ap->slave_link;
5457 WARN_ON(link == slave); /* shouldn't be called on slave link */
5459 return ata_phys_link_offline(link) &&
5460 (!slave || ata_phys_link_offline(slave));
5464 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5465 unsigned int action, unsigned int ehi_flags,
5468 unsigned long flags;
5471 for (i = 0; i < host->n_ports; i++) {
5472 struct ata_port *ap = host->ports[i];
5473 struct ata_link *link;
5475 /* Previous resume operation might still be in
5476 * progress. Wait for PM_PENDING to clear.
5478 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5479 ata_port_wait_eh(ap);
5480 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5483 /* request PM ops to EH */
5484 spin_lock_irqsave(ap->lock, flags);
5489 ap->pm_result = &rc;
5492 ap->pflags |= ATA_PFLAG_PM_PENDING;
5493 ata_for_each_link(link, ap, HOST_FIRST) {
5494 link->eh_info.action |= action;
5495 link->eh_info.flags |= ehi_flags;
5498 ata_port_schedule_eh(ap);
5500 spin_unlock_irqrestore(ap->lock, flags);
5502 /* wait and check result */
5504 ata_port_wait_eh(ap);
5505 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5515 * ata_host_suspend - suspend host
5516 * @host: host to suspend
5519 * Suspend @host. Actual operation is performed by EH. This
5520 * function requests EH to perform PM operations and waits for EH
5524 * Kernel thread context (may sleep).
5527 * 0 on success, -errno on failure.
5529 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5534 * disable link pm on all ports before requesting
5537 ata_lpm_enable(host);
5539 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
5541 host->dev->power.power_state = mesg;
5546 * ata_host_resume - resume host
5547 * @host: host to resume
5549 * Resume @host. Actual operation is performed by EH. This
5550 * function requests EH to perform PM operations and returns.
5551 * Note that all resume operations are performed parallely.
5554 * Kernel thread context (may sleep).
5556 void ata_host_resume(struct ata_host *host)
5558 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5559 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
5560 host->dev->power.power_state = PMSG_ON;
5562 /* reenable link pm */
5563 ata_lpm_disable(host);
5568 * ata_port_start - Set port up for dma.
5569 * @ap: Port to initialize
5571 * Called just after data structures for each port are
5572 * initialized. Allocates space for PRD table.
5574 * May be used as the port_start() entry in ata_port_operations.
5577 * Inherited from caller.
5579 int ata_port_start(struct ata_port *ap)
5581 struct device *dev = ap->dev;
5583 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5592 * ata_dev_init - Initialize an ata_device structure
5593 * @dev: Device structure to initialize
5595 * Initialize @dev in preparation for probing.
5598 * Inherited from caller.
5600 void ata_dev_init(struct ata_device *dev)
5602 struct ata_link *link = ata_dev_phys_link(dev);
5603 struct ata_port *ap = link->ap;
5604 unsigned long flags;
5606 /* SATA spd limit is bound to the attached device, reset together */
5607 link->sata_spd_limit = link->hw_sata_spd_limit;
5610 /* High bits of dev->flags are used to record warm plug
5611 * requests which occur asynchronously. Synchronize using
5614 spin_lock_irqsave(ap->lock, flags);
5615 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5617 spin_unlock_irqrestore(ap->lock, flags);
5619 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5620 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5621 dev->pio_mask = UINT_MAX;
5622 dev->mwdma_mask = UINT_MAX;
5623 dev->udma_mask = UINT_MAX;
5627 * ata_link_init - Initialize an ata_link structure
5628 * @ap: ATA port link is attached to
5629 * @link: Link structure to initialize
5630 * @pmp: Port multiplier port number
5635 * Kernel thread context (may sleep)
5637 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
5641 /* clear everything except for devices */
5642 memset(link, 0, offsetof(struct ata_link, device[0]));
5646 link->active_tag = ATA_TAG_POISON;
5647 link->hw_sata_spd_limit = UINT_MAX;
5649 /* can't use iterator, ap isn't initialized yet */
5650 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5651 struct ata_device *dev = &link->device[i];
5654 dev->devno = dev - link->device;
5655 #ifdef CONFIG_ATA_ACPI
5656 dev->gtf_filter = ata_acpi_gtf_filter;
5663 * sata_link_init_spd - Initialize link->sata_spd_limit
5664 * @link: Link to configure sata_spd_limit for
5666 * Initialize @link->[hw_]sata_spd_limit to the currently
5670 * Kernel thread context (may sleep).
5673 * 0 on success, -errno on failure.
5675 int sata_link_init_spd(struct ata_link *link)
5680 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
5684 spd = (link->saved_scontrol >> 4) & 0xf;
5686 link->hw_sata_spd_limit &= (1 << spd) - 1;
5688 ata_force_link_limits(link);
5690 link->sata_spd_limit = link->hw_sata_spd_limit;
5696 * ata_port_alloc - allocate and initialize basic ATA port resources
5697 * @host: ATA host this allocated port belongs to
5699 * Allocate and initialize basic ATA port resources.
5702 * Allocate ATA port on success, NULL on failure.
5705 * Inherited from calling layer (may sleep).
5707 struct ata_port *ata_port_alloc(struct ata_host *host)
5709 struct ata_port *ap;
5713 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5717 ap->pflags |= ATA_PFLAG_INITIALIZING;
5718 ap->lock = &host->lock;
5719 ap->flags = ATA_FLAG_DISABLED;
5721 ap->ctl = ATA_DEVCTL_OBS;
5723 ap->dev = host->dev;
5724 ap->last_ctl = 0xFF;
5726 #if defined(ATA_VERBOSE_DEBUG)
5727 /* turn on all debugging levels */
5728 ap->msg_enable = 0x00FF;
5729 #elif defined(ATA_DEBUG)
5730 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5732 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5735 #ifdef CONFIG_ATA_SFF
5736 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5738 INIT_DELAYED_WORK(&ap->port_task, NULL);
5740 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5741 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5742 INIT_LIST_HEAD(&ap->eh_done_q);
5743 init_waitqueue_head(&ap->eh_wait_q);
5744 init_completion(&ap->park_req_pending);
5745 init_timer_deferrable(&ap->fastdrain_timer);
5746 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5747 ap->fastdrain_timer.data = (unsigned long)ap;
5749 ap->cbl = ATA_CBL_NONE;
5751 ata_link_init(ap, &ap->link, 0);
5754 ap->stats.unhandled_irq = 1;
5755 ap->stats.idle_irq = 1;
5760 static void ata_host_release(struct device *gendev, void *res)
5762 struct ata_host *host = dev_get_drvdata(gendev);
5765 for (i = 0; i < host->n_ports; i++) {
5766 struct ata_port *ap = host->ports[i];
5772 scsi_host_put(ap->scsi_host);
5774 kfree(ap->pmp_link);
5775 kfree(ap->slave_link);
5777 host->ports[i] = NULL;
5780 dev_set_drvdata(gendev, NULL);
5784 * ata_host_alloc - allocate and init basic ATA host resources
5785 * @dev: generic device this host is associated with
5786 * @max_ports: maximum number of ATA ports associated with this host
5788 * Allocate and initialize basic ATA host resources. LLD calls
5789 * this function to allocate a host, initializes it fully and
5790 * attaches it using ata_host_register().
5792 * @max_ports ports are allocated and host->n_ports is
5793 * initialized to @max_ports. The caller is allowed to decrease
5794 * host->n_ports before calling ata_host_register(). The unused
5795 * ports will be automatically freed on registration.
5798 * Allocate ATA host on success, NULL on failure.
5801 * Inherited from calling layer (may sleep).
5803 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5805 struct ata_host *host;
5811 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5814 /* alloc a container for our list of ATA ports (buses) */
5815 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5816 /* alloc a container for our list of ATA ports (buses) */
5817 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5821 devres_add(dev, host);
5822 dev_set_drvdata(dev, host);
5824 spin_lock_init(&host->lock);
5826 host->n_ports = max_ports;
5828 /* allocate ports bound to this host */
5829 for (i = 0; i < max_ports; i++) {
5830 struct ata_port *ap;
5832 ap = ata_port_alloc(host);
5837 host->ports[i] = ap;
5840 devres_remove_group(dev, NULL);
5844 devres_release_group(dev, NULL);
5849 * ata_host_alloc_pinfo - alloc host and init with port_info array
5850 * @dev: generic device this host is associated with
5851 * @ppi: array of ATA port_info to initialize host with
5852 * @n_ports: number of ATA ports attached to this host
5854 * Allocate ATA host and initialize with info from @ppi. If NULL
5855 * terminated, @ppi may contain fewer entries than @n_ports. The
5856 * last entry will be used for the remaining ports.
5859 * Allocate ATA host on success, NULL on failure.
5862 * Inherited from calling layer (may sleep).
5864 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5865 const struct ata_port_info * const * ppi,
5868 const struct ata_port_info *pi;
5869 struct ata_host *host;
5872 host = ata_host_alloc(dev, n_ports);
5876 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5877 struct ata_port *ap = host->ports[i];
5882 ap->pio_mask = pi->pio_mask;
5883 ap->mwdma_mask = pi->mwdma_mask;
5884 ap->udma_mask = pi->udma_mask;
5885 ap->flags |= pi->flags;
5886 ap->link.flags |= pi->link_flags;
5887 ap->ops = pi->port_ops;
5889 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5890 host->ops = pi->port_ops;
5897 * ata_slave_link_init - initialize slave link
5898 * @ap: port to initialize slave link for
5900 * Create and initialize slave link for @ap. This enables slave
5901 * link handling on the port.
5903 * In libata, a port contains links and a link contains devices.
5904 * There is single host link but if a PMP is attached to it,
5905 * there can be multiple fan-out links. On SATA, there's usually
5906 * a single device connected to a link but PATA and SATA
5907 * controllers emulating TF based interface can have two - master
5910 * However, there are a few controllers which don't fit into this
5911 * abstraction too well - SATA controllers which emulate TF
5912 * interface with both master and slave devices but also have
5913 * separate SCR register sets for each device. These controllers
5914 * need separate links for physical link handling
5915 * (e.g. onlineness, link speed) but should be treated like a
5916 * traditional M/S controller for everything else (e.g. command
5917 * issue, softreset).
5919 * slave_link is libata's way of handling this class of
5920 * controllers without impacting core layer too much. For
5921 * anything other than physical link handling, the default host
5922 * link is used for both master and slave. For physical link
5923 * handling, separate @ap->slave_link is used. All dirty details
5924 * are implemented inside libata core layer. From LLD's POV, the
5925 * only difference is that prereset, hardreset and postreset are
5926 * called once more for the slave link, so the reset sequence
5927 * looks like the following.
5929 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5930 * softreset(M) -> postreset(M) -> postreset(S)
5932 * Note that softreset is called only for the master. Softreset
5933 * resets both M/S by definition, so SRST on master should handle
5934 * both (the standard method will work just fine).
5937 * Should be called before host is registered.
5940 * 0 on success, -errno on failure.
5942 int ata_slave_link_init(struct ata_port *ap)
5944 struct ata_link *link;
5946 WARN_ON(ap->slave_link);
5947 WARN_ON(ap->flags & ATA_FLAG_PMP);
5949 link = kzalloc(sizeof(*link), GFP_KERNEL);
5953 ata_link_init(ap, link, 1);
5954 ap->slave_link = link;
5958 static void ata_host_stop(struct device *gendev, void *res)
5960 struct ata_host *host = dev_get_drvdata(gendev);
5963 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5965 for (i = 0; i < host->n_ports; i++) {
5966 struct ata_port *ap = host->ports[i];
5968 if (ap->ops->port_stop)
5969 ap->ops->port_stop(ap);
5972 if (host->ops->host_stop)
5973 host->ops->host_stop(host);
5977 * ata_finalize_port_ops - finalize ata_port_operations
5978 * @ops: ata_port_operations to finalize
5980 * An ata_port_operations can inherit from another ops and that
5981 * ops can again inherit from another. This can go on as many
5982 * times as necessary as long as there is no loop in the
5983 * inheritance chain.
5985 * Ops tables are finalized when the host is started. NULL or
5986 * unspecified entries are inherited from the closet ancestor
5987 * which has the method and the entry is populated with it.
5988 * After finalization, the ops table directly points to all the
5989 * methods and ->inherits is no longer necessary and cleared.
5991 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5996 static void ata_finalize_port_ops(struct ata_port_operations *ops)
5998 static DEFINE_SPINLOCK(lock);
5999 const struct ata_port_operations *cur;
6000 void **begin = (void **)ops;
6001 void **end = (void **)&ops->inherits;
6004 if (!ops || !ops->inherits)
6009 for (cur = ops->inherits; cur; cur = cur->inherits) {
6010 void **inherit = (void **)cur;
6012 for (pp = begin; pp < end; pp++, inherit++)
6017 for (pp = begin; pp < end; pp++)
6021 ops->inherits = NULL;
6027 * ata_host_start - start and freeze ports of an ATA host
6028 * @host: ATA host to start ports for
6030 * Start and then freeze ports of @host. Started status is
6031 * recorded in host->flags, so this function can be called
6032 * multiple times. Ports are guaranteed to get started only
6033 * once. If host->ops isn't initialized yet, its set to the
6034 * first non-dummy port ops.
6037 * Inherited from calling layer (may sleep).
6040 * 0 if all ports are started successfully, -errno otherwise.
6042 int ata_host_start(struct ata_host *host)
6045 void *start_dr = NULL;
6048 if (host->flags & ATA_HOST_STARTED)
6051 ata_finalize_port_ops(host->ops);
6053 for (i = 0; i < host->n_ports; i++) {
6054 struct ata_port *ap = host->ports[i];
6056 ata_finalize_port_ops(ap->ops);
6058 if (!host->ops && !ata_port_is_dummy(ap))
6059 host->ops = ap->ops;
6061 if (ap->ops->port_stop)
6065 if (host->ops->host_stop)
6069 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6074 for (i = 0; i < host->n_ports; i++) {
6075 struct ata_port *ap = host->ports[i];
6077 if (ap->ops->port_start) {
6078 rc = ap->ops->port_start(ap);
6081 dev_printk(KERN_ERR, host->dev,
6082 "failed to start port %d "
6083 "(errno=%d)\n", i, rc);
6087 ata_eh_freeze_port(ap);
6091 devres_add(host->dev, start_dr);
6092 host->flags |= ATA_HOST_STARTED;
6097 struct ata_port *ap = host->ports[i];
6099 if (ap->ops->port_stop)
6100 ap->ops->port_stop(ap);
6102 devres_free(start_dr);
6107 * ata_sas_host_init - Initialize a host struct
6108 * @host: host to initialize
6109 * @dev: device host is attached to
6110 * @flags: host flags
6114 * PCI/etc. bus probe sem.
6117 /* KILLME - the only user left is ipr */
6118 void ata_host_init(struct ata_host *host, struct device *dev,
6119 unsigned long flags, struct ata_port_operations *ops)
6121 spin_lock_init(&host->lock);
6123 host->flags = flags;
6128 static void async_port_probe(void *data, async_cookie_t cookie)
6131 struct ata_port *ap = data;
6134 * If we're not allowed to scan this host in parallel,
6135 * we need to wait until all previous scans have completed
6136 * before going further.
6137 * Jeff Garzik says this is only within a controller, so we
6138 * don't need to wait for port 0, only for later ports.
6140 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6141 async_synchronize_cookie(cookie);
6144 if (ap->ops->error_handler) {
6145 struct ata_eh_info *ehi = &ap->link.eh_info;
6146 unsigned long flags;
6150 /* kick EH for boot probing */
6151 spin_lock_irqsave(ap->lock, flags);
6153 ehi->probe_mask |= ATA_ALL_DEVICES;
6154 ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
6155 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6157 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6158 ap->pflags |= ATA_PFLAG_LOADING;
6159 ata_port_schedule_eh(ap);
6161 spin_unlock_irqrestore(ap->lock, flags);
6163 /* wait for EH to finish */
6164 ata_port_wait_eh(ap);
6166 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6167 rc = ata_bus_probe(ap);
6168 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6171 /* FIXME: do something useful here?
6172 * Current libata behavior will
6173 * tear down everything when
6174 * the module is removed
6175 * or the h/w is unplugged.
6180 /* in order to keep device order, we need to synchronize at this point */
6181 async_synchronize_cookie(cookie);
6183 ata_scsi_scan_host(ap, 1);
6187 * ata_host_register - register initialized ATA host
6188 * @host: ATA host to register
6189 * @sht: template for SCSI host
6191 * Register initialized ATA host. @host is allocated using
6192 * ata_host_alloc() and fully initialized by LLD. This function
6193 * starts ports, registers @host with ATA and SCSI layers and
6194 * probe registered devices.
6197 * Inherited from calling layer (may sleep).
6200 * 0 on success, -errno otherwise.
6202 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6206 /* host must have been started */
6207 if (!(host->flags & ATA_HOST_STARTED)) {
6208 dev_printk(KERN_ERR, host->dev,
6209 "BUG: trying to register unstarted host\n");
6214 /* Blow away unused ports. This happens when LLD can't
6215 * determine the exact number of ports to allocate at
6218 for (i = host->n_ports; host->ports[i]; i++)
6219 kfree(host->ports[i]);
6221 /* give ports names and add SCSI hosts */
6222 for (i = 0; i < host->n_ports; i++)
6223 host->ports[i]->print_id = ata_print_id++;
6225 rc = ata_scsi_add_hosts(host, sht);
6229 /* associate with ACPI nodes */
6230 ata_acpi_associate(host);
6232 /* set cable, sata_spd_limit and report */
6233 for (i = 0; i < host->n_ports; i++) {
6234 struct ata_port *ap = host->ports[i];
6235 unsigned long xfer_mask;
6237 /* set SATA cable type if still unset */
6238 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6239 ap->cbl = ATA_CBL_SATA;
6241 /* init sata_spd_limit to the current value */
6242 sata_link_init_spd(&ap->link);
6244 sata_link_init_spd(ap->slave_link);
6246 /* print per-port info to dmesg */
6247 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6250 if (!ata_port_is_dummy(ap)) {
6251 ata_port_printk(ap, KERN_INFO,
6252 "%cATA max %s %s\n",
6253 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6254 ata_mode_string(xfer_mask),
6255 ap->link.eh_info.desc);
6256 ata_ehi_clear_desc(&ap->link.eh_info);
6258 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6261 /* perform each probe asynchronously */
6262 for (i = 0; i < host->n_ports; i++) {
6263 struct ata_port *ap = host->ports[i];
6264 async_schedule(async_port_probe, ap);
6271 * ata_host_activate - start host, request IRQ and register it
6272 * @host: target ATA host
6273 * @irq: IRQ to request
6274 * @irq_handler: irq_handler used when requesting IRQ
6275 * @irq_flags: irq_flags used when requesting IRQ
6276 * @sht: scsi_host_template to use when registering the host
6278 * After allocating an ATA host and initializing it, most libata
6279 * LLDs perform three steps to activate the host - start host,
6280 * request IRQ and register it. This helper takes necessasry
6281 * arguments and performs the three steps in one go.
6283 * An invalid IRQ skips the IRQ registration and expects the host to
6284 * have set polling mode on the port. In this case, @irq_handler
6288 * Inherited from calling layer (may sleep).
6291 * 0 on success, -errno otherwise.
6293 int ata_host_activate(struct ata_host *host, int irq,
6294 irq_handler_t irq_handler, unsigned long irq_flags,
6295 struct scsi_host_template *sht)
6299 rc = ata_host_start(host);
6303 /* Special case for polling mode */
6305 WARN_ON(irq_handler);
6306 return ata_host_register(host, sht);
6309 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6310 dev_driver_string(host->dev), host);
6314 for (i = 0; i < host->n_ports; i++)
6315 ata_port_desc(host->ports[i], "irq %d", irq);
6317 rc = ata_host_register(host, sht);
6318 /* if failed, just free the IRQ and leave ports alone */
6320 devm_free_irq(host->dev, irq, host);
6326 * ata_port_detach - Detach ATA port in prepration of device removal
6327 * @ap: ATA port to be detached
6329 * Detach all ATA devices and the associated SCSI devices of @ap;
6330 * then, remove the associated SCSI host. @ap is guaranteed to
6331 * be quiescent on return from this function.
6334 * Kernel thread context (may sleep).
6336 static void ata_port_detach(struct ata_port *ap)
6338 unsigned long flags;
6340 if (!ap->ops->error_handler)
6343 /* tell EH we're leaving & flush EH */
6344 spin_lock_irqsave(ap->lock, flags);
6345 ap->pflags |= ATA_PFLAG_UNLOADING;
6346 ata_port_schedule_eh(ap);
6347 spin_unlock_irqrestore(ap->lock, flags);
6349 /* wait till EH commits suicide */
6350 ata_port_wait_eh(ap);
6352 /* it better be dead now */
6353 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6355 cancel_rearming_delayed_work(&ap->hotplug_task);
6358 /* remove the associated SCSI host */
6359 scsi_remove_host(ap->scsi_host);
6363 * ata_host_detach - Detach all ports of an ATA host
6364 * @host: Host to detach
6366 * Detach all ports of @host.
6369 * Kernel thread context (may sleep).
6371 void ata_host_detach(struct ata_host *host)
6375 for (i = 0; i < host->n_ports; i++)
6376 ata_port_detach(host->ports[i]);
6378 /* the host is dead now, dissociate ACPI */
6379 ata_acpi_dissociate(host);
6385 * ata_pci_remove_one - PCI layer callback for device removal
6386 * @pdev: PCI device that was removed
6388 * PCI layer indicates to libata via this hook that hot-unplug or
6389 * module unload event has occurred. Detach all ports. Resource
6390 * release is handled via devres.
6393 * Inherited from PCI layer (may sleep).
6395 void ata_pci_remove_one(struct pci_dev *pdev)
6397 struct device *dev = &pdev->dev;
6398 struct ata_host *host = dev_get_drvdata(dev);
6400 ata_host_detach(host);
6403 /* move to PCI subsystem */
6404 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6406 unsigned long tmp = 0;
6408 switch (bits->width) {
6411 pci_read_config_byte(pdev, bits->reg, &tmp8);
6417 pci_read_config_word(pdev, bits->reg, &tmp16);
6423 pci_read_config_dword(pdev, bits->reg, &tmp32);
6434 return (tmp == bits->val) ? 1 : 0;
6438 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6440 pci_save_state(pdev);
6441 pci_disable_device(pdev);
6443 if (mesg.event & PM_EVENT_SLEEP)
6444 pci_set_power_state(pdev, PCI_D3hot);
6447 int ata_pci_device_do_resume(struct pci_dev *pdev)
6451 pci_set_power_state(pdev, PCI_D0);
6452 pci_restore_state(pdev);
6454 rc = pcim_enable_device(pdev);
6456 dev_printk(KERN_ERR, &pdev->dev,
6457 "failed to enable device after resume (%d)\n", rc);
6461 pci_set_master(pdev);
6465 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6467 struct ata_host *host = dev_get_drvdata(&pdev->dev);
6470 rc = ata_host_suspend(host, mesg);
6474 ata_pci_device_do_suspend(pdev, mesg);
6479 int ata_pci_device_resume(struct pci_dev *pdev)
6481 struct ata_host *host = dev_get_drvdata(&pdev->dev);
6484 rc = ata_pci_device_do_resume(pdev);
6486 ata_host_resume(host);
6489 #endif /* CONFIG_PM */
6491 #endif /* CONFIG_PCI */
6493 static int __init ata_parse_force_one(char **cur,
6494 struct ata_force_ent *force_ent,
6495 const char **reason)
6497 /* FIXME: Currently, there's no way to tag init const data and
6498 * using __initdata causes build failure on some versions of
6499 * gcc. Once __initdataconst is implemented, add const to the
6500 * following structure.
6502 static struct ata_force_param force_tbl[] __initdata = {
6503 { "40c", .cbl = ATA_CBL_PATA40 },
6504 { "80c", .cbl = ATA_CBL_PATA80 },
6505 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6506 { "unk", .cbl = ATA_CBL_PATA_UNK },
6507 { "ign", .cbl = ATA_CBL_PATA_IGN },
6508 { "sata", .cbl = ATA_CBL_SATA },
6509 { "1.5Gbps", .spd_limit = 1 },
6510 { "3.0Gbps", .spd_limit = 2 },
6511 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6512 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
6513 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6514 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6515 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6516 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6517 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6518 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6519 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6520 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6521 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6522 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6523 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6524 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6525 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6526 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6527 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6528 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6529 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6530 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6531 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6532 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6533 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6534 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6535 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6536 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6537 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6538 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6539 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6540 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6541 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6542 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6543 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6544 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6545 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6546 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
6547 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6548 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6549 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6551 char *start = *cur, *p = *cur;
6552 char *id, *val, *endp;
6553 const struct ata_force_param *match_fp = NULL;
6554 int nr_matches = 0, i;
6556 /* find where this param ends and update *cur */
6557 while (*p != '\0' && *p != ',')
6568 p = strchr(start, ':');
6570 val = strstrip(start);
6575 id = strstrip(start);
6576 val = strstrip(p + 1);
6579 p = strchr(id, '.');
6582 force_ent->device = simple_strtoul(p, &endp, 10);
6583 if (p == endp || *endp != '\0') {
6584 *reason = "invalid device";
6589 force_ent->port = simple_strtoul(id, &endp, 10);
6590 if (p == endp || *endp != '\0') {
6591 *reason = "invalid port/link";
6596 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6597 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6598 const struct ata_force_param *fp = &force_tbl[i];
6600 if (strncasecmp(val, fp->name, strlen(val)))
6606 if (strcasecmp(val, fp->name) == 0) {
6613 *reason = "unknown value";
6616 if (nr_matches > 1) {
6617 *reason = "ambigious value";
6621 force_ent->param = *match_fp;
6626 static void __init ata_parse_force_param(void)
6628 int idx = 0, size = 1;
6629 int last_port = -1, last_device = -1;
6630 char *p, *cur, *next;
6632 /* calculate maximum number of params and allocate force_tbl */
6633 for (p = ata_force_param_buf; *p; p++)
6637 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
6638 if (!ata_force_tbl) {
6639 printk(KERN_WARNING "ata: failed to extend force table, "
6640 "libata.force ignored\n");
6644 /* parse and populate the table */
6645 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6646 const char *reason = "";
6647 struct ata_force_ent te = { .port = -1, .device = -1 };
6650 if (ata_parse_force_one(&next, &te, &reason)) {
6651 printk(KERN_WARNING "ata: failed to parse force "
6652 "parameter \"%s\" (%s)\n",
6657 if (te.port == -1) {
6658 te.port = last_port;
6659 te.device = last_device;
6662 ata_force_tbl[idx++] = te;
6664 last_port = te.port;
6665 last_device = te.device;
6668 ata_force_tbl_size = idx;
6671 static int __init ata_init(void)
6673 ata_parse_force_param();
6676 * FIXME: In UP case, there is only one workqueue thread and if you
6677 * have more than one PIO device, latency is bloody awful, with
6678 * occasional multi-second "hiccups" as one PIO device waits for
6679 * another. It's an ugly wart that users DO occasionally complain
6680 * about; luckily most users have at most one PIO polled device.
6682 ata_wq = create_workqueue("ata");
6684 goto free_force_tbl;
6686 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6690 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6694 destroy_workqueue(ata_wq);
6696 kfree(ata_force_tbl);
6700 static void __exit ata_exit(void)
6702 kfree(ata_force_tbl);
6703 destroy_workqueue(ata_wq);
6704 destroy_workqueue(ata_aux_wq);
6707 subsys_initcall(ata_init);
6708 module_exit(ata_exit);
6710 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6712 int ata_ratelimit(void)
6714 return __ratelimit(&ratelimit);
6718 * ata_wait_register - wait until register value changes
6719 * @reg: IO-mapped register
6720 * @mask: Mask to apply to read register value
6721 * @val: Wait condition
6722 * @interval: polling interval in milliseconds
6723 * @timeout: timeout in milliseconds
6725 * Waiting for some bits of register to change is a common
6726 * operation for ATA controllers. This function reads 32bit LE
6727 * IO-mapped register @reg and tests for the following condition.
6729 * (*@reg & mask) != val
6731 * If the condition is met, it returns; otherwise, the process is
6732 * repeated after @interval_msec until timeout.
6735 * Kernel thread context (may sleep)
6738 * The final register value.
6740 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6741 unsigned long interval, unsigned long timeout)
6743 unsigned long deadline;
6746 tmp = ioread32(reg);
6748 /* Calculate timeout _after_ the first read to make sure
6749 * preceding writes reach the controller before starting to
6750 * eat away the timeout.
6752 deadline = ata_deadline(jiffies, timeout);
6754 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6756 tmp = ioread32(reg);
6765 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6767 return AC_ERR_SYSTEM;
6770 static void ata_dummy_error_handler(struct ata_port *ap)
6775 struct ata_port_operations ata_dummy_port_ops = {
6776 .qc_prep = ata_noop_qc_prep,
6777 .qc_issue = ata_dummy_qc_issue,
6778 .error_handler = ata_dummy_error_handler,
6781 const struct ata_port_info ata_dummy_port_info = {
6782 .port_ops = &ata_dummy_port_ops,
6786 * libata is essentially a library of internal helper functions for
6787 * low-level ATA host controller drivers. As such, the API/ABI is
6788 * likely to change as new drivers are added and updated.
6789 * Do not depend on ABI/API stability.
6791 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6792 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6793 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6794 EXPORT_SYMBOL_GPL(ata_base_port_ops);
6795 EXPORT_SYMBOL_GPL(sata_port_ops);
6796 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6797 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6798 EXPORT_SYMBOL_GPL(ata_link_next);
6799 EXPORT_SYMBOL_GPL(ata_dev_next);
6800 EXPORT_SYMBOL_GPL(ata_std_bios_param);
6801 EXPORT_SYMBOL_GPL(ata_host_init);
6802 EXPORT_SYMBOL_GPL(ata_host_alloc);
6803 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6804 EXPORT_SYMBOL_GPL(ata_slave_link_init);
6805 EXPORT_SYMBOL_GPL(ata_host_start);
6806 EXPORT_SYMBOL_GPL(ata_host_register);
6807 EXPORT_SYMBOL_GPL(ata_host_activate);
6808 EXPORT_SYMBOL_GPL(ata_host_detach);
6809 EXPORT_SYMBOL_GPL(ata_sg_init);
6810 EXPORT_SYMBOL_GPL(ata_qc_complete);
6811 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6812 EXPORT_SYMBOL_GPL(atapi_cmd_type);
6813 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6814 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6815 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6816 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6817 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6818 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6819 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6820 EXPORT_SYMBOL_GPL(ata_mode_string);
6821 EXPORT_SYMBOL_GPL(ata_id_xfermask);
6822 EXPORT_SYMBOL_GPL(ata_port_start);
6823 EXPORT_SYMBOL_GPL(ata_do_set_mode);
6824 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6825 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6826 EXPORT_SYMBOL_GPL(ata_port_probe);
6827 EXPORT_SYMBOL_GPL(ata_dev_disable);
6828 EXPORT_SYMBOL_GPL(sata_set_spd);
6829 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6830 EXPORT_SYMBOL_GPL(sata_link_debounce);
6831 EXPORT_SYMBOL_GPL(sata_link_resume);
6832 EXPORT_SYMBOL_GPL(ata_std_prereset);
6833 EXPORT_SYMBOL_GPL(sata_link_hardreset);
6834 EXPORT_SYMBOL_GPL(sata_std_hardreset);
6835 EXPORT_SYMBOL_GPL(ata_std_postreset);
6836 EXPORT_SYMBOL_GPL(ata_dev_classify);
6837 EXPORT_SYMBOL_GPL(ata_dev_pair);
6838 EXPORT_SYMBOL_GPL(ata_port_disable);
6839 EXPORT_SYMBOL_GPL(ata_ratelimit);
6840 EXPORT_SYMBOL_GPL(ata_wait_register);
6841 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6842 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6843 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6844 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6845 EXPORT_SYMBOL_GPL(sata_scr_valid);
6846 EXPORT_SYMBOL_GPL(sata_scr_read);
6847 EXPORT_SYMBOL_GPL(sata_scr_write);
6848 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6849 EXPORT_SYMBOL_GPL(ata_link_online);
6850 EXPORT_SYMBOL_GPL(ata_link_offline);
6852 EXPORT_SYMBOL_GPL(ata_host_suspend);
6853 EXPORT_SYMBOL_GPL(ata_host_resume);
6854 #endif /* CONFIG_PM */
6855 EXPORT_SYMBOL_GPL(ata_id_string);
6856 EXPORT_SYMBOL_GPL(ata_id_c_string);
6857 EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6858 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6860 EXPORT_SYMBOL_GPL(ata_pio_queue_task);
6861 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
6862 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6863 EXPORT_SYMBOL_GPL(ata_timing_compute);
6864 EXPORT_SYMBOL_GPL(ata_timing_merge);
6865 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6868 EXPORT_SYMBOL_GPL(pci_test_config_bits);
6869 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6871 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6872 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6873 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6874 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6875 #endif /* CONFIG_PM */
6876 #endif /* CONFIG_PCI */
6878 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6879 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6880 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6881 EXPORT_SYMBOL_GPL(ata_port_desc);
6883 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6884 #endif /* CONFIG_PCI */
6885 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6886 EXPORT_SYMBOL_GPL(ata_link_abort);
6887 EXPORT_SYMBOL_GPL(ata_port_abort);
6888 EXPORT_SYMBOL_GPL(ata_port_freeze);
6889 EXPORT_SYMBOL_GPL(sata_async_notification);
6890 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6891 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6892 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6893 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
6894 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6895 EXPORT_SYMBOL_GPL(ata_do_eh);
6896 EXPORT_SYMBOL_GPL(ata_std_error_handler);
6898 EXPORT_SYMBOL_GPL(ata_cable_40wire);
6899 EXPORT_SYMBOL_GPL(ata_cable_80wire);
6900 EXPORT_SYMBOL_GPL(ata_cable_unknown);
6901 EXPORT_SYMBOL_GPL(ata_cable_ignore);
6902 EXPORT_SYMBOL_GPL(ata_cable_sata);