Revert "ALSA: hda: Flush interrupts on disabling"
[linux/fpc-iii.git] / drivers / ata / libata-core.c
blobda1a987c622a448144b3e6f535f1c17c5b96b5fc
1 /*
2 * libata-core.c - helper library for ATA
4 * Maintained by: Tejun Heo <tj@kernel.org>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
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)
15 * any later version.
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>
48 #include <linux/mm.h>
49 #include <linux/spinlock.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
52 #include <linux/timer.h>
53 #include <linux/time.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/scatterlist.h>
59 #include <linux/io.h>
60 #include <linux/async.h>
61 #include <linux/log2.h>
62 #include <linux/slab.h>
63 #include <linux/glob.h>
64 #include <scsi/scsi.h>
65 #include <scsi/scsi_cmnd.h>
66 #include <scsi/scsi_host.h>
67 #include <linux/libata.h>
68 #include <asm/byteorder.h>
69 #include <asm/unaligned.h>
70 #include <linux/cdrom.h>
71 #include <linux/ratelimit.h>
72 #include <linux/leds.h>
73 #include <linux/pm_runtime.h>
74 #include <linux/platform_device.h>
76 #define CREATE_TRACE_POINTS
77 #include <trace/events/libata.h>
79 #include "libata.h"
80 #include "libata-transport.h"
82 /* debounce timing parameters in msecs { interval, duration, timeout } */
83 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
84 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
85 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
87 const struct ata_port_operations ata_base_port_ops = {
88 .prereset = ata_std_prereset,
89 .postreset = ata_std_postreset,
90 .error_handler = ata_std_error_handler,
91 .sched_eh = ata_std_sched_eh,
92 .end_eh = ata_std_end_eh,
95 const struct ata_port_operations sata_port_ops = {
96 .inherits = &ata_base_port_ops,
98 .qc_defer = ata_std_qc_defer,
99 .hardreset = sata_std_hardreset,
102 static unsigned int ata_dev_init_params(struct ata_device *dev,
103 u16 heads, u16 sectors);
104 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
105 static void ata_dev_xfermask(struct ata_device *dev);
106 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
108 atomic_t ata_print_id = ATOMIC_INIT(0);
110 struct ata_force_param {
111 const char *name;
112 unsigned int cbl;
113 int spd_limit;
114 unsigned long xfer_mask;
115 unsigned int horkage_on;
116 unsigned int horkage_off;
117 unsigned int lflags;
120 struct ata_force_ent {
121 int port;
122 int device;
123 struct ata_force_param param;
126 static struct ata_force_ent *ata_force_tbl;
127 static int ata_force_tbl_size;
129 static char ata_force_param_buf[PAGE_SIZE] __initdata;
130 /* param_buf is thrown away after initialization, disallow read */
131 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
132 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
134 static int atapi_enabled = 1;
135 module_param(atapi_enabled, int, 0444);
136 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
138 static int atapi_dmadir = 0;
139 module_param(atapi_dmadir, int, 0444);
140 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
142 int atapi_passthru16 = 1;
143 module_param(atapi_passthru16, int, 0444);
144 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
146 int libata_fua = 0;
147 module_param_named(fua, libata_fua, int, 0444);
148 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
150 static int ata_ignore_hpa;
151 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
152 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
154 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
155 module_param_named(dma, libata_dma_mask, int, 0444);
156 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
158 static int ata_probe_timeout;
159 module_param(ata_probe_timeout, int, 0444);
160 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
162 int libata_noacpi = 0;
163 module_param_named(noacpi, libata_noacpi, int, 0444);
164 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
166 int libata_allow_tpm = 0;
167 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
168 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
170 static int atapi_an;
171 module_param(atapi_an, int, 0444);
172 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
174 MODULE_AUTHOR("Jeff Garzik");
175 MODULE_DESCRIPTION("Library module for ATA devices");
176 MODULE_LICENSE("GPL");
177 MODULE_VERSION(DRV_VERSION);
180 static bool ata_sstatus_online(u32 sstatus)
182 return (sstatus & 0xf) == 0x3;
186 * ata_link_next - link iteration helper
187 * @link: the previous link, NULL to start
188 * @ap: ATA port containing links to iterate
189 * @mode: iteration mode, one of ATA_LITER_*
191 * LOCKING:
192 * Host lock or EH context.
194 * RETURNS:
195 * Pointer to the next link.
197 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
198 enum ata_link_iter_mode mode)
200 BUG_ON(mode != ATA_LITER_EDGE &&
201 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
203 /* NULL link indicates start of iteration */
204 if (!link)
205 switch (mode) {
206 case ATA_LITER_EDGE:
207 case ATA_LITER_PMP_FIRST:
208 if (sata_pmp_attached(ap))
209 return ap->pmp_link;
210 /* fall through */
211 case ATA_LITER_HOST_FIRST:
212 return &ap->link;
215 /* we just iterated over the host link, what's next? */
216 if (link == &ap->link)
217 switch (mode) {
218 case ATA_LITER_HOST_FIRST:
219 if (sata_pmp_attached(ap))
220 return ap->pmp_link;
221 /* fall through */
222 case ATA_LITER_PMP_FIRST:
223 if (unlikely(ap->slave_link))
224 return ap->slave_link;
225 /* fall through */
226 case ATA_LITER_EDGE:
227 return NULL;
230 /* slave_link excludes PMP */
231 if (unlikely(link == ap->slave_link))
232 return NULL;
234 /* we were over a PMP link */
235 if (++link < ap->pmp_link + ap->nr_pmp_links)
236 return link;
238 if (mode == ATA_LITER_PMP_FIRST)
239 return &ap->link;
241 return NULL;
245 * ata_dev_next - device iteration helper
246 * @dev: the previous device, NULL to start
247 * @link: ATA link containing devices to iterate
248 * @mode: iteration mode, one of ATA_DITER_*
250 * LOCKING:
251 * Host lock or EH context.
253 * RETURNS:
254 * Pointer to the next device.
256 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
257 enum ata_dev_iter_mode mode)
259 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
260 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
262 /* NULL dev indicates start of iteration */
263 if (!dev)
264 switch (mode) {
265 case ATA_DITER_ENABLED:
266 case ATA_DITER_ALL:
267 dev = link->device;
268 goto check;
269 case ATA_DITER_ENABLED_REVERSE:
270 case ATA_DITER_ALL_REVERSE:
271 dev = link->device + ata_link_max_devices(link) - 1;
272 goto check;
275 next:
276 /* move to the next one */
277 switch (mode) {
278 case ATA_DITER_ENABLED:
279 case ATA_DITER_ALL:
280 if (++dev < link->device + ata_link_max_devices(link))
281 goto check;
282 return NULL;
283 case ATA_DITER_ENABLED_REVERSE:
284 case ATA_DITER_ALL_REVERSE:
285 if (--dev >= link->device)
286 goto check;
287 return NULL;
290 check:
291 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
292 !ata_dev_enabled(dev))
293 goto next;
294 return dev;
298 * ata_dev_phys_link - find physical link for a device
299 * @dev: ATA device to look up physical link for
301 * Look up physical link which @dev is attached to. Note that
302 * this is different from @dev->link only when @dev is on slave
303 * link. For all other cases, it's the same as @dev->link.
305 * LOCKING:
306 * Don't care.
308 * RETURNS:
309 * Pointer to the found physical link.
311 struct ata_link *ata_dev_phys_link(struct ata_device *dev)
313 struct ata_port *ap = dev->link->ap;
315 if (!ap->slave_link)
316 return dev->link;
317 if (!dev->devno)
318 return &ap->link;
319 return ap->slave_link;
323 * ata_force_cbl - force cable type according to libata.force
324 * @ap: ATA port of interest
326 * Force cable type according to libata.force and whine about it.
327 * The last entry which has matching port number is used, so it
328 * can be specified as part of device force parameters. For
329 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
330 * same effect.
332 * LOCKING:
333 * EH context.
335 void ata_force_cbl(struct ata_port *ap)
337 int i;
339 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
340 const struct ata_force_ent *fe = &ata_force_tbl[i];
342 if (fe->port != -1 && fe->port != ap->print_id)
343 continue;
345 if (fe->param.cbl == ATA_CBL_NONE)
346 continue;
348 ap->cbl = fe->param.cbl;
349 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
350 return;
355 * ata_force_link_limits - force link limits according to libata.force
356 * @link: ATA link of interest
358 * Force link flags and SATA spd limit according to libata.force
359 * and whine about it. When only the port part is specified
360 * (e.g. 1:), the limit applies to all links connected to both
361 * the host link and all fan-out ports connected via PMP. If the
362 * device part is specified as 0 (e.g. 1.00:), it specifies the
363 * first fan-out link not the host link. Device number 15 always
364 * points to the host link whether PMP is attached or not. If the
365 * controller has slave link, device number 16 points to it.
367 * LOCKING:
368 * EH context.
370 static void ata_force_link_limits(struct ata_link *link)
372 bool did_spd = false;
373 int linkno = link->pmp;
374 int i;
376 if (ata_is_host_link(link))
377 linkno += 15;
379 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
380 const struct ata_force_ent *fe = &ata_force_tbl[i];
382 if (fe->port != -1 && fe->port != link->ap->print_id)
383 continue;
385 if (fe->device != -1 && fe->device != linkno)
386 continue;
388 /* only honor the first spd limit */
389 if (!did_spd && fe->param.spd_limit) {
390 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
391 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
392 fe->param.name);
393 did_spd = true;
396 /* let lflags stack */
397 if (fe->param.lflags) {
398 link->flags |= fe->param.lflags;
399 ata_link_notice(link,
400 "FORCE: link flag 0x%x forced -> 0x%x\n",
401 fe->param.lflags, link->flags);
407 * ata_force_xfermask - force xfermask according to libata.force
408 * @dev: ATA device of interest
410 * Force xfer_mask according to libata.force and whine about it.
411 * For consistency with link selection, device number 15 selects
412 * the first device connected to the host link.
414 * LOCKING:
415 * EH context.
417 static void ata_force_xfermask(struct ata_device *dev)
419 int devno = dev->link->pmp + dev->devno;
420 int alt_devno = devno;
421 int i;
423 /* allow n.15/16 for devices attached to host port */
424 if (ata_is_host_link(dev->link))
425 alt_devno += 15;
427 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
428 const struct ata_force_ent *fe = &ata_force_tbl[i];
429 unsigned long pio_mask, mwdma_mask, udma_mask;
431 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
432 continue;
434 if (fe->device != -1 && fe->device != devno &&
435 fe->device != alt_devno)
436 continue;
438 if (!fe->param.xfer_mask)
439 continue;
441 ata_unpack_xfermask(fe->param.xfer_mask,
442 &pio_mask, &mwdma_mask, &udma_mask);
443 if (udma_mask)
444 dev->udma_mask = udma_mask;
445 else if (mwdma_mask) {
446 dev->udma_mask = 0;
447 dev->mwdma_mask = mwdma_mask;
448 } else {
449 dev->udma_mask = 0;
450 dev->mwdma_mask = 0;
451 dev->pio_mask = pio_mask;
454 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
455 fe->param.name);
456 return;
461 * ata_force_horkage - force horkage according to libata.force
462 * @dev: ATA device of interest
464 * Force horkage according to libata.force and whine about it.
465 * For consistency with link selection, device number 15 selects
466 * the first device connected to the host link.
468 * LOCKING:
469 * EH context.
471 static void ata_force_horkage(struct ata_device *dev)
473 int devno = dev->link->pmp + dev->devno;
474 int alt_devno = devno;
475 int i;
477 /* allow n.15/16 for devices attached to host port */
478 if (ata_is_host_link(dev->link))
479 alt_devno += 15;
481 for (i = 0; i < ata_force_tbl_size; i++) {
482 const struct ata_force_ent *fe = &ata_force_tbl[i];
484 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
485 continue;
487 if (fe->device != -1 && fe->device != devno &&
488 fe->device != alt_devno)
489 continue;
491 if (!(~dev->horkage & fe->param.horkage_on) &&
492 !(dev->horkage & fe->param.horkage_off))
493 continue;
495 dev->horkage |= fe->param.horkage_on;
496 dev->horkage &= ~fe->param.horkage_off;
498 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
499 fe->param.name);
504 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
505 * @opcode: SCSI opcode
507 * Determine ATAPI command type from @opcode.
509 * LOCKING:
510 * None.
512 * RETURNS:
513 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
515 int atapi_cmd_type(u8 opcode)
517 switch (opcode) {
518 case GPCMD_READ_10:
519 case GPCMD_READ_12:
520 return ATAPI_READ;
522 case GPCMD_WRITE_10:
523 case GPCMD_WRITE_12:
524 case GPCMD_WRITE_AND_VERIFY_10:
525 return ATAPI_WRITE;
527 case GPCMD_READ_CD:
528 case GPCMD_READ_CD_MSF:
529 return ATAPI_READ_CD;
531 case ATA_16:
532 case ATA_12:
533 if (atapi_passthru16)
534 return ATAPI_PASS_THRU;
535 /* fall thru */
536 default:
537 return ATAPI_MISC;
542 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
543 * @tf: Taskfile to convert
544 * @pmp: Port multiplier port
545 * @is_cmd: This FIS is for command
546 * @fis: Buffer into which data will output
548 * Converts a standard ATA taskfile to a Serial ATA
549 * FIS structure (Register - Host to Device).
551 * LOCKING:
552 * Inherited from caller.
554 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
556 fis[0] = 0x27; /* Register - Host to Device FIS */
557 fis[1] = pmp & 0xf; /* Port multiplier number*/
558 if (is_cmd)
559 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
561 fis[2] = tf->command;
562 fis[3] = tf->feature;
564 fis[4] = tf->lbal;
565 fis[5] = tf->lbam;
566 fis[6] = tf->lbah;
567 fis[7] = tf->device;
569 fis[8] = tf->hob_lbal;
570 fis[9] = tf->hob_lbam;
571 fis[10] = tf->hob_lbah;
572 fis[11] = tf->hob_feature;
574 fis[12] = tf->nsect;
575 fis[13] = tf->hob_nsect;
576 fis[14] = 0;
577 fis[15] = tf->ctl;
579 fis[16] = tf->auxiliary & 0xff;
580 fis[17] = (tf->auxiliary >> 8) & 0xff;
581 fis[18] = (tf->auxiliary >> 16) & 0xff;
582 fis[19] = (tf->auxiliary >> 24) & 0xff;
586 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
587 * @fis: Buffer from which data will be input
588 * @tf: Taskfile to output
590 * Converts a serial ATA FIS structure to a standard ATA taskfile.
592 * LOCKING:
593 * Inherited from caller.
596 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
598 tf->command = fis[2]; /* status */
599 tf->feature = fis[3]; /* error */
601 tf->lbal = fis[4];
602 tf->lbam = fis[5];
603 tf->lbah = fis[6];
604 tf->device = fis[7];
606 tf->hob_lbal = fis[8];
607 tf->hob_lbam = fis[9];
608 tf->hob_lbah = fis[10];
610 tf->nsect = fis[12];
611 tf->hob_nsect = fis[13];
614 static const u8 ata_rw_cmds[] = {
615 /* pio multi */
616 ATA_CMD_READ_MULTI,
617 ATA_CMD_WRITE_MULTI,
618 ATA_CMD_READ_MULTI_EXT,
619 ATA_CMD_WRITE_MULTI_EXT,
623 ATA_CMD_WRITE_MULTI_FUA_EXT,
624 /* pio */
625 ATA_CMD_PIO_READ,
626 ATA_CMD_PIO_WRITE,
627 ATA_CMD_PIO_READ_EXT,
628 ATA_CMD_PIO_WRITE_EXT,
633 /* dma */
634 ATA_CMD_READ,
635 ATA_CMD_WRITE,
636 ATA_CMD_READ_EXT,
637 ATA_CMD_WRITE_EXT,
641 ATA_CMD_WRITE_FUA_EXT
645 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
646 * @tf: command to examine and configure
647 * @dev: device tf belongs to
649 * Examine the device configuration and tf->flags to calculate
650 * the proper read/write commands and protocol to use.
652 * LOCKING:
653 * caller.
655 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
657 u8 cmd;
659 int index, fua, lba48, write;
661 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
662 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
663 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
665 if (dev->flags & ATA_DFLAG_PIO) {
666 tf->protocol = ATA_PROT_PIO;
667 index = dev->multi_count ? 0 : 8;
668 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
669 /* Unable to use DMA due to host limitation */
670 tf->protocol = ATA_PROT_PIO;
671 index = dev->multi_count ? 0 : 8;
672 } else {
673 tf->protocol = ATA_PROT_DMA;
674 index = 16;
677 cmd = ata_rw_cmds[index + fua + lba48 + write];
678 if (cmd) {
679 tf->command = cmd;
680 return 0;
682 return -1;
686 * ata_tf_read_block - Read block address from ATA taskfile
687 * @tf: ATA taskfile of interest
688 * @dev: ATA device @tf belongs to
690 * LOCKING:
691 * None.
693 * Read block address from @tf. This function can handle all
694 * three address formats - LBA, LBA48 and CHS. tf->protocol and
695 * flags select the address format to use.
697 * RETURNS:
698 * Block address read from @tf.
700 u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
702 u64 block = 0;
704 if (tf->flags & ATA_TFLAG_LBA) {
705 if (tf->flags & ATA_TFLAG_LBA48) {
706 block |= (u64)tf->hob_lbah << 40;
707 block |= (u64)tf->hob_lbam << 32;
708 block |= (u64)tf->hob_lbal << 24;
709 } else
710 block |= (tf->device & 0xf) << 24;
712 block |= tf->lbah << 16;
713 block |= tf->lbam << 8;
714 block |= tf->lbal;
715 } else {
716 u32 cyl, head, sect;
718 cyl = tf->lbam | (tf->lbah << 8);
719 head = tf->device & 0xf;
720 sect = tf->lbal;
722 if (!sect) {
723 ata_dev_warn(dev,
724 "device reported invalid CHS sector 0\n");
725 return U64_MAX;
728 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
731 return block;
735 * ata_build_rw_tf - Build ATA taskfile for given read/write request
736 * @tf: Target ATA taskfile
737 * @dev: ATA device @tf belongs to
738 * @block: Block address
739 * @n_block: Number of blocks
740 * @tf_flags: RW/FUA etc...
741 * @tag: tag
743 * LOCKING:
744 * None.
746 * Build ATA taskfile @tf for read/write request described by
747 * @block, @n_block, @tf_flags and @tag on @dev.
749 * RETURNS:
751 * 0 on success, -ERANGE if the request is too large for @dev,
752 * -EINVAL if the request is invalid.
754 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
755 u64 block, u32 n_block, unsigned int tf_flags,
756 unsigned int tag)
758 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
759 tf->flags |= tf_flags;
761 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
762 /* yay, NCQ */
763 if (!lba_48_ok(block, n_block))
764 return -ERANGE;
766 tf->protocol = ATA_PROT_NCQ;
767 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
769 if (tf->flags & ATA_TFLAG_WRITE)
770 tf->command = ATA_CMD_FPDMA_WRITE;
771 else
772 tf->command = ATA_CMD_FPDMA_READ;
774 tf->nsect = tag << 3;
775 tf->hob_feature = (n_block >> 8) & 0xff;
776 tf->feature = n_block & 0xff;
778 tf->hob_lbah = (block >> 40) & 0xff;
779 tf->hob_lbam = (block >> 32) & 0xff;
780 tf->hob_lbal = (block >> 24) & 0xff;
781 tf->lbah = (block >> 16) & 0xff;
782 tf->lbam = (block >> 8) & 0xff;
783 tf->lbal = block & 0xff;
785 tf->device = ATA_LBA;
786 if (tf->flags & ATA_TFLAG_FUA)
787 tf->device |= 1 << 7;
788 } else if (dev->flags & ATA_DFLAG_LBA) {
789 tf->flags |= ATA_TFLAG_LBA;
791 if (lba_28_ok(block, n_block)) {
792 /* use LBA28 */
793 tf->device |= (block >> 24) & 0xf;
794 } else if (lba_48_ok(block, n_block)) {
795 if (!(dev->flags & ATA_DFLAG_LBA48))
796 return -ERANGE;
798 /* use LBA48 */
799 tf->flags |= ATA_TFLAG_LBA48;
801 tf->hob_nsect = (n_block >> 8) & 0xff;
803 tf->hob_lbah = (block >> 40) & 0xff;
804 tf->hob_lbam = (block >> 32) & 0xff;
805 tf->hob_lbal = (block >> 24) & 0xff;
806 } else
807 /* request too large even for LBA48 */
808 return -ERANGE;
810 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
811 return -EINVAL;
813 tf->nsect = n_block & 0xff;
815 tf->lbah = (block >> 16) & 0xff;
816 tf->lbam = (block >> 8) & 0xff;
817 tf->lbal = block & 0xff;
819 tf->device |= ATA_LBA;
820 } else {
821 /* CHS */
822 u32 sect, head, cyl, track;
824 /* The request -may- be too large for CHS addressing. */
825 if (!lba_28_ok(block, n_block))
826 return -ERANGE;
828 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
829 return -EINVAL;
831 /* Convert LBA to CHS */
832 track = (u32)block / dev->sectors;
833 cyl = track / dev->heads;
834 head = track % dev->heads;
835 sect = (u32)block % dev->sectors + 1;
837 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
838 (u32)block, track, cyl, head, sect);
840 /* Check whether the converted CHS can fit.
841 Cylinder: 0-65535
842 Head: 0-15
843 Sector: 1-255*/
844 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
845 return -ERANGE;
847 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
848 tf->lbal = sect;
849 tf->lbam = cyl;
850 tf->lbah = cyl >> 8;
851 tf->device |= head;
854 return 0;
858 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
859 * @pio_mask: pio_mask
860 * @mwdma_mask: mwdma_mask
861 * @udma_mask: udma_mask
863 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
864 * unsigned int xfer_mask.
866 * LOCKING:
867 * None.
869 * RETURNS:
870 * Packed xfer_mask.
872 unsigned long ata_pack_xfermask(unsigned long pio_mask,
873 unsigned long mwdma_mask,
874 unsigned long udma_mask)
876 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
877 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
878 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
882 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
883 * @xfer_mask: xfer_mask to unpack
884 * @pio_mask: resulting pio_mask
885 * @mwdma_mask: resulting mwdma_mask
886 * @udma_mask: resulting udma_mask
888 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
889 * Any NULL destination masks will be ignored.
891 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
892 unsigned long *mwdma_mask, unsigned long *udma_mask)
894 if (pio_mask)
895 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
896 if (mwdma_mask)
897 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
898 if (udma_mask)
899 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
902 static const struct ata_xfer_ent {
903 int shift, bits;
904 u8 base;
905 } ata_xfer_tbl[] = {
906 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
907 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
908 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
909 { -1, },
913 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
914 * @xfer_mask: xfer_mask of interest
916 * Return matching XFER_* value for @xfer_mask. Only the highest
917 * bit of @xfer_mask is considered.
919 * LOCKING:
920 * None.
922 * RETURNS:
923 * Matching XFER_* value, 0xff if no match found.
925 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
927 int highbit = fls(xfer_mask) - 1;
928 const struct ata_xfer_ent *ent;
930 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
931 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
932 return ent->base + highbit - ent->shift;
933 return 0xff;
937 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
938 * @xfer_mode: XFER_* of interest
940 * Return matching xfer_mask for @xfer_mode.
942 * LOCKING:
943 * None.
945 * RETURNS:
946 * Matching xfer_mask, 0 if no match found.
948 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
950 const struct ata_xfer_ent *ent;
952 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
953 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
954 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
955 & ~((1 << ent->shift) - 1);
956 return 0;
960 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
961 * @xfer_mode: XFER_* of interest
963 * Return matching xfer_shift for @xfer_mode.
965 * LOCKING:
966 * None.
968 * RETURNS:
969 * Matching xfer_shift, -1 if no match found.
971 int ata_xfer_mode2shift(unsigned long xfer_mode)
973 const struct ata_xfer_ent *ent;
975 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
976 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
977 return ent->shift;
978 return -1;
982 * ata_mode_string - convert xfer_mask to string
983 * @xfer_mask: mask of bits supported; only highest bit counts.
985 * Determine string which represents the highest speed
986 * (highest bit in @modemask).
988 * LOCKING:
989 * None.
991 * RETURNS:
992 * Constant C string representing highest speed listed in
993 * @mode_mask, or the constant C string "<n/a>".
995 const char *ata_mode_string(unsigned long xfer_mask)
997 static const char * const xfer_mode_str[] = {
998 "PIO0",
999 "PIO1",
1000 "PIO2",
1001 "PIO3",
1002 "PIO4",
1003 "PIO5",
1004 "PIO6",
1005 "MWDMA0",
1006 "MWDMA1",
1007 "MWDMA2",
1008 "MWDMA3",
1009 "MWDMA4",
1010 "UDMA/16",
1011 "UDMA/25",
1012 "UDMA/33",
1013 "UDMA/44",
1014 "UDMA/66",
1015 "UDMA/100",
1016 "UDMA/133",
1017 "UDMA7",
1019 int highbit;
1021 highbit = fls(xfer_mask) - 1;
1022 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1023 return xfer_mode_str[highbit];
1024 return "<n/a>";
1027 const char *sata_spd_string(unsigned int spd)
1029 static const char * const spd_str[] = {
1030 "1.5 Gbps",
1031 "3.0 Gbps",
1032 "6.0 Gbps",
1035 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1036 return "<unknown>";
1037 return spd_str[spd - 1];
1041 * ata_dev_classify - determine device type based on ATA-spec signature
1042 * @tf: ATA taskfile register set for device to be identified
1044 * Determine from taskfile register contents whether a device is
1045 * ATA or ATAPI, as per "Signature and persistence" section
1046 * of ATA/PI spec (volume 1, sect 5.14).
1048 * LOCKING:
1049 * None.
1051 * RETURNS:
1052 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
1053 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
1055 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1057 /* Apple's open source Darwin code hints that some devices only
1058 * put a proper signature into the LBA mid/high registers,
1059 * So, we only check those. It's sufficient for uniqueness.
1061 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1062 * signatures for ATA and ATAPI devices attached on SerialATA,
1063 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1064 * spec has never mentioned about using different signatures
1065 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1066 * Multiplier specification began to use 0x69/0x96 to identify
1067 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1068 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1069 * 0x69/0x96 shortly and described them as reserved for
1070 * SerialATA.
1072 * We follow the current spec and consider that 0x69/0x96
1073 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1074 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1075 * SEMB signature. This is worked around in
1076 * ata_dev_read_id().
1078 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1079 DPRINTK("found ATA device by sig\n");
1080 return ATA_DEV_ATA;
1083 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1084 DPRINTK("found ATAPI device by sig\n");
1085 return ATA_DEV_ATAPI;
1088 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1089 DPRINTK("found PMP device by sig\n");
1090 return ATA_DEV_PMP;
1093 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1094 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1095 return ATA_DEV_SEMB;
1098 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1099 DPRINTK("found ZAC device by sig\n");
1100 return ATA_DEV_ZAC;
1103 DPRINTK("unknown device\n");
1104 return ATA_DEV_UNKNOWN;
1108 * ata_id_string - Convert IDENTIFY DEVICE page into string
1109 * @id: IDENTIFY DEVICE results we will examine
1110 * @s: string into which data is output
1111 * @ofs: offset into identify device page
1112 * @len: length of string to return. must be an even number.
1114 * The strings in the IDENTIFY DEVICE page are broken up into
1115 * 16-bit chunks. Run through the string, and output each
1116 * 8-bit chunk linearly, regardless of platform.
1118 * LOCKING:
1119 * caller.
1122 void ata_id_string(const u16 *id, unsigned char *s,
1123 unsigned int ofs, unsigned int len)
1125 unsigned int c;
1127 BUG_ON(len & 1);
1129 while (len > 0) {
1130 c = id[ofs] >> 8;
1131 *s = c;
1132 s++;
1134 c = id[ofs] & 0xff;
1135 *s = c;
1136 s++;
1138 ofs++;
1139 len -= 2;
1144 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1145 * @id: IDENTIFY DEVICE results we will examine
1146 * @s: string into which data is output
1147 * @ofs: offset into identify device page
1148 * @len: length of string to return. must be an odd number.
1150 * This function is identical to ata_id_string except that it
1151 * trims trailing spaces and terminates the resulting string with
1152 * null. @len must be actual maximum length (even number) + 1.
1154 * LOCKING:
1155 * caller.
1157 void ata_id_c_string(const u16 *id, unsigned char *s,
1158 unsigned int ofs, unsigned int len)
1160 unsigned char *p;
1162 ata_id_string(id, s, ofs, len - 1);
1164 p = s + strnlen(s, len - 1);
1165 while (p > s && p[-1] == ' ')
1166 p--;
1167 *p = '\0';
1170 static u64 ata_id_n_sectors(const u16 *id)
1172 if (ata_id_has_lba(id)) {
1173 if (ata_id_has_lba48(id))
1174 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1175 else
1176 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1177 } else {
1178 if (ata_id_current_chs_valid(id))
1179 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1180 id[ATA_ID_CUR_SECTORS];
1181 else
1182 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1183 id[ATA_ID_SECTORS];
1187 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1189 u64 sectors = 0;
1191 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1192 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1193 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1194 sectors |= (tf->lbah & 0xff) << 16;
1195 sectors |= (tf->lbam & 0xff) << 8;
1196 sectors |= (tf->lbal & 0xff);
1198 return sectors;
1201 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1203 u64 sectors = 0;
1205 sectors |= (tf->device & 0x0f) << 24;
1206 sectors |= (tf->lbah & 0xff) << 16;
1207 sectors |= (tf->lbam & 0xff) << 8;
1208 sectors |= (tf->lbal & 0xff);
1210 return sectors;
1214 * ata_read_native_max_address - Read native max address
1215 * @dev: target device
1216 * @max_sectors: out parameter for the result native max address
1218 * Perform an LBA48 or LBA28 native size query upon the device in
1219 * question.
1221 * RETURNS:
1222 * 0 on success, -EACCES if command is aborted by the drive.
1223 * -EIO on other errors.
1225 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1227 unsigned int err_mask;
1228 struct ata_taskfile tf;
1229 int lba48 = ata_id_has_lba48(dev->id);
1231 ata_tf_init(dev, &tf);
1233 /* always clear all address registers */
1234 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1236 if (lba48) {
1237 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1238 tf.flags |= ATA_TFLAG_LBA48;
1239 } else
1240 tf.command = ATA_CMD_READ_NATIVE_MAX;
1242 tf.protocol = ATA_PROT_NODATA;
1243 tf.device |= ATA_LBA;
1245 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1246 if (err_mask) {
1247 ata_dev_warn(dev,
1248 "failed to read native max address (err_mask=0x%x)\n",
1249 err_mask);
1250 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1251 return -EACCES;
1252 return -EIO;
1255 if (lba48)
1256 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1257 else
1258 *max_sectors = ata_tf_to_lba(&tf) + 1;
1259 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1260 (*max_sectors)--;
1261 return 0;
1265 * ata_set_max_sectors - Set max sectors
1266 * @dev: target device
1267 * @new_sectors: new max sectors value to set for the device
1269 * Set max sectors of @dev to @new_sectors.
1271 * RETURNS:
1272 * 0 on success, -EACCES if command is aborted or denied (due to
1273 * previous non-volatile SET_MAX) by the drive. -EIO on other
1274 * errors.
1276 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1278 unsigned int err_mask;
1279 struct ata_taskfile tf;
1280 int lba48 = ata_id_has_lba48(dev->id);
1282 new_sectors--;
1284 ata_tf_init(dev, &tf);
1286 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1288 if (lba48) {
1289 tf.command = ATA_CMD_SET_MAX_EXT;
1290 tf.flags |= ATA_TFLAG_LBA48;
1292 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1293 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1294 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1295 } else {
1296 tf.command = ATA_CMD_SET_MAX;
1298 tf.device |= (new_sectors >> 24) & 0xf;
1301 tf.protocol = ATA_PROT_NODATA;
1302 tf.device |= ATA_LBA;
1304 tf.lbal = (new_sectors >> 0) & 0xff;
1305 tf.lbam = (new_sectors >> 8) & 0xff;
1306 tf.lbah = (new_sectors >> 16) & 0xff;
1308 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1309 if (err_mask) {
1310 ata_dev_warn(dev,
1311 "failed to set max address (err_mask=0x%x)\n",
1312 err_mask);
1313 if (err_mask == AC_ERR_DEV &&
1314 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1315 return -EACCES;
1316 return -EIO;
1319 return 0;
1323 * ata_hpa_resize - Resize a device with an HPA set
1324 * @dev: Device to resize
1326 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1327 * it if required to the full size of the media. The caller must check
1328 * the drive has the HPA feature set enabled.
1330 * RETURNS:
1331 * 0 on success, -errno on failure.
1333 static int ata_hpa_resize(struct ata_device *dev)
1335 struct ata_eh_context *ehc = &dev->link->eh_context;
1336 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1337 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1338 u64 sectors = ata_id_n_sectors(dev->id);
1339 u64 native_sectors;
1340 int rc;
1342 /* do we need to do it? */
1343 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
1344 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1345 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1346 return 0;
1348 /* read native max address */
1349 rc = ata_read_native_max_address(dev, &native_sectors);
1350 if (rc) {
1351 /* If device aborted the command or HPA isn't going to
1352 * be unlocked, skip HPA resizing.
1354 if (rc == -EACCES || !unlock_hpa) {
1355 ata_dev_warn(dev,
1356 "HPA support seems broken, skipping HPA handling\n");
1357 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1359 /* we can continue if device aborted the command */
1360 if (rc == -EACCES)
1361 rc = 0;
1364 return rc;
1366 dev->n_native_sectors = native_sectors;
1368 /* nothing to do? */
1369 if (native_sectors <= sectors || !unlock_hpa) {
1370 if (!print_info || native_sectors == sectors)
1371 return 0;
1373 if (native_sectors > sectors)
1374 ata_dev_info(dev,
1375 "HPA detected: current %llu, native %llu\n",
1376 (unsigned long long)sectors,
1377 (unsigned long long)native_sectors);
1378 else if (native_sectors < sectors)
1379 ata_dev_warn(dev,
1380 "native sectors (%llu) is smaller than sectors (%llu)\n",
1381 (unsigned long long)native_sectors,
1382 (unsigned long long)sectors);
1383 return 0;
1386 /* let's unlock HPA */
1387 rc = ata_set_max_sectors(dev, native_sectors);
1388 if (rc == -EACCES) {
1389 /* if device aborted the command, skip HPA resizing */
1390 ata_dev_warn(dev,
1391 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1392 (unsigned long long)sectors,
1393 (unsigned long long)native_sectors);
1394 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1395 return 0;
1396 } else if (rc)
1397 return rc;
1399 /* re-read IDENTIFY data */
1400 rc = ata_dev_reread_id(dev, 0);
1401 if (rc) {
1402 ata_dev_err(dev,
1403 "failed to re-read IDENTIFY data after HPA resizing\n");
1404 return rc;
1407 if (print_info) {
1408 u64 new_sectors = ata_id_n_sectors(dev->id);
1409 ata_dev_info(dev,
1410 "HPA unlocked: %llu -> %llu, native %llu\n",
1411 (unsigned long long)sectors,
1412 (unsigned long long)new_sectors,
1413 (unsigned long long)native_sectors);
1416 return 0;
1420 * ata_dump_id - IDENTIFY DEVICE info debugging output
1421 * @id: IDENTIFY DEVICE page to dump
1423 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1424 * page.
1426 * LOCKING:
1427 * caller.
1430 static inline void ata_dump_id(const u16 *id)
1432 DPRINTK("49==0x%04x "
1433 "53==0x%04x "
1434 "63==0x%04x "
1435 "64==0x%04x "
1436 "75==0x%04x \n",
1437 id[49],
1438 id[53],
1439 id[63],
1440 id[64],
1441 id[75]);
1442 DPRINTK("80==0x%04x "
1443 "81==0x%04x "
1444 "82==0x%04x "
1445 "83==0x%04x "
1446 "84==0x%04x \n",
1447 id[80],
1448 id[81],
1449 id[82],
1450 id[83],
1451 id[84]);
1452 DPRINTK("88==0x%04x "
1453 "93==0x%04x\n",
1454 id[88],
1455 id[93]);
1459 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1460 * @id: IDENTIFY data to compute xfer mask from
1462 * Compute the xfermask for this device. This is not as trivial
1463 * as it seems if we must consider early devices correctly.
1465 * FIXME: pre IDE drive timing (do we care ?).
1467 * LOCKING:
1468 * None.
1470 * RETURNS:
1471 * Computed xfermask
1473 unsigned long ata_id_xfermask(const u16 *id)
1475 unsigned long pio_mask, mwdma_mask, udma_mask;
1477 /* Usual case. Word 53 indicates word 64 is valid */
1478 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1479 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1480 pio_mask <<= 3;
1481 pio_mask |= 0x7;
1482 } else {
1483 /* If word 64 isn't valid then Word 51 high byte holds
1484 * the PIO timing number for the maximum. Turn it into
1485 * a mask.
1487 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1488 if (mode < 5) /* Valid PIO range */
1489 pio_mask = (2 << mode) - 1;
1490 else
1491 pio_mask = 1;
1493 /* But wait.. there's more. Design your standards by
1494 * committee and you too can get a free iordy field to
1495 * process. However its the speeds not the modes that
1496 * are supported... Note drivers using the timing API
1497 * will get this right anyway
1501 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1503 if (ata_id_is_cfa(id)) {
1505 * Process compact flash extended modes
1507 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1508 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1510 if (pio)
1511 pio_mask |= (1 << 5);
1512 if (pio > 1)
1513 pio_mask |= (1 << 6);
1514 if (dma)
1515 mwdma_mask |= (1 << 3);
1516 if (dma > 1)
1517 mwdma_mask |= (1 << 4);
1520 udma_mask = 0;
1521 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1522 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1524 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1527 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1529 struct completion *waiting = qc->private_data;
1531 complete(waiting);
1535 * ata_exec_internal_sg - execute libata internal command
1536 * @dev: Device to which the command is sent
1537 * @tf: Taskfile registers for the command and the result
1538 * @cdb: CDB for packet command
1539 * @dma_dir: Data transfer direction of the command
1540 * @sgl: sg list for the data buffer of the command
1541 * @n_elem: Number of sg entries
1542 * @timeout: Timeout in msecs (0 for default)
1544 * Executes libata internal command with timeout. @tf contains
1545 * command on entry and result on return. Timeout and error
1546 * conditions are reported via return value. No recovery action
1547 * is taken after a command times out. It's caller's duty to
1548 * clean up after timeout.
1550 * LOCKING:
1551 * None. Should be called with kernel context, might sleep.
1553 * RETURNS:
1554 * Zero on success, AC_ERR_* mask on failure
1556 unsigned ata_exec_internal_sg(struct ata_device *dev,
1557 struct ata_taskfile *tf, const u8 *cdb,
1558 int dma_dir, struct scatterlist *sgl,
1559 unsigned int n_elem, unsigned long timeout)
1561 struct ata_link *link = dev->link;
1562 struct ata_port *ap = link->ap;
1563 u8 command = tf->command;
1564 int auto_timeout = 0;
1565 struct ata_queued_cmd *qc;
1566 unsigned int tag, preempted_tag;
1567 u32 preempted_sactive, preempted_qc_active;
1568 int preempted_nr_active_links;
1569 DECLARE_COMPLETION_ONSTACK(wait);
1570 unsigned long flags;
1571 unsigned int err_mask;
1572 int rc;
1574 spin_lock_irqsave(ap->lock, flags);
1576 /* no internal command while frozen */
1577 if (ap->pflags & ATA_PFLAG_FROZEN) {
1578 spin_unlock_irqrestore(ap->lock, flags);
1579 return AC_ERR_SYSTEM;
1582 /* initialize internal qc */
1584 /* XXX: Tag 0 is used for drivers with legacy EH as some
1585 * drivers choke if any other tag is given. This breaks
1586 * ata_tag_internal() test for those drivers. Don't use new
1587 * EH stuff without converting to it.
1589 if (ap->ops->error_handler)
1590 tag = ATA_TAG_INTERNAL;
1591 else
1592 tag = 0;
1594 qc = __ata_qc_from_tag(ap, tag);
1596 qc->tag = tag;
1597 qc->scsicmd = NULL;
1598 qc->ap = ap;
1599 qc->dev = dev;
1600 ata_qc_reinit(qc);
1602 preempted_tag = link->active_tag;
1603 preempted_sactive = link->sactive;
1604 preempted_qc_active = ap->qc_active;
1605 preempted_nr_active_links = ap->nr_active_links;
1606 link->active_tag = ATA_TAG_POISON;
1607 link->sactive = 0;
1608 ap->qc_active = 0;
1609 ap->nr_active_links = 0;
1611 /* prepare & issue qc */
1612 qc->tf = *tf;
1613 if (cdb)
1614 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1616 /* some SATA bridges need us to indicate data xfer direction */
1617 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1618 dma_dir == DMA_FROM_DEVICE)
1619 qc->tf.feature |= ATAPI_DMADIR;
1621 qc->flags |= ATA_QCFLAG_RESULT_TF;
1622 qc->dma_dir = dma_dir;
1623 if (dma_dir != DMA_NONE) {
1624 unsigned int i, buflen = 0;
1625 struct scatterlist *sg;
1627 for_each_sg(sgl, sg, n_elem, i)
1628 buflen += sg->length;
1630 ata_sg_init(qc, sgl, n_elem);
1631 qc->nbytes = buflen;
1634 qc->private_data = &wait;
1635 qc->complete_fn = ata_qc_complete_internal;
1637 ata_qc_issue(qc);
1639 spin_unlock_irqrestore(ap->lock, flags);
1641 if (!timeout) {
1642 if (ata_probe_timeout)
1643 timeout = ata_probe_timeout * 1000;
1644 else {
1645 timeout = ata_internal_cmd_timeout(dev, command);
1646 auto_timeout = 1;
1650 if (ap->ops->error_handler)
1651 ata_eh_release(ap);
1653 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1655 if (ap->ops->error_handler)
1656 ata_eh_acquire(ap);
1658 ata_sff_flush_pio_task(ap);
1660 if (!rc) {
1661 spin_lock_irqsave(ap->lock, flags);
1663 /* We're racing with irq here. If we lose, the
1664 * following test prevents us from completing the qc
1665 * twice. If we win, the port is frozen and will be
1666 * cleaned up by ->post_internal_cmd().
1668 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1669 qc->err_mask |= AC_ERR_TIMEOUT;
1671 if (ap->ops->error_handler)
1672 ata_port_freeze(ap);
1673 else
1674 ata_qc_complete(qc);
1676 if (ata_msg_warn(ap))
1677 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1678 command);
1681 spin_unlock_irqrestore(ap->lock, flags);
1684 /* do post_internal_cmd */
1685 if (ap->ops->post_internal_cmd)
1686 ap->ops->post_internal_cmd(qc);
1688 /* perform minimal error analysis */
1689 if (qc->flags & ATA_QCFLAG_FAILED) {
1690 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1691 qc->err_mask |= AC_ERR_DEV;
1693 if (!qc->err_mask)
1694 qc->err_mask |= AC_ERR_OTHER;
1696 if (qc->err_mask & ~AC_ERR_OTHER)
1697 qc->err_mask &= ~AC_ERR_OTHER;
1698 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1699 qc->result_tf.command |= ATA_SENSE;
1702 /* finish up */
1703 spin_lock_irqsave(ap->lock, flags);
1705 *tf = qc->result_tf;
1706 err_mask = qc->err_mask;
1708 ata_qc_free(qc);
1709 link->active_tag = preempted_tag;
1710 link->sactive = preempted_sactive;
1711 ap->qc_active = preempted_qc_active;
1712 ap->nr_active_links = preempted_nr_active_links;
1714 spin_unlock_irqrestore(ap->lock, flags);
1716 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1717 ata_internal_cmd_timed_out(dev, command);
1719 return err_mask;
1723 * ata_exec_internal - execute libata internal command
1724 * @dev: Device to which the command is sent
1725 * @tf: Taskfile registers for the command and the result
1726 * @cdb: CDB for packet command
1727 * @dma_dir: Data transfer direction of the command
1728 * @buf: Data buffer of the command
1729 * @buflen: Length of data buffer
1730 * @timeout: Timeout in msecs (0 for default)
1732 * Wrapper around ata_exec_internal_sg() which takes simple
1733 * buffer instead of sg list.
1735 * LOCKING:
1736 * None. Should be called with kernel context, might sleep.
1738 * RETURNS:
1739 * Zero on success, AC_ERR_* mask on failure
1741 unsigned ata_exec_internal(struct ata_device *dev,
1742 struct ata_taskfile *tf, const u8 *cdb,
1743 int dma_dir, void *buf, unsigned int buflen,
1744 unsigned long timeout)
1746 struct scatterlist *psg = NULL, sg;
1747 unsigned int n_elem = 0;
1749 if (dma_dir != DMA_NONE) {
1750 WARN_ON(!buf);
1751 sg_init_one(&sg, buf, buflen);
1752 psg = &sg;
1753 n_elem++;
1756 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1757 timeout);
1761 * ata_pio_need_iordy - check if iordy needed
1762 * @adev: ATA device
1764 * Check if the current speed of the device requires IORDY. Used
1765 * by various controllers for chip configuration.
1767 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1769 /* Don't set IORDY if we're preparing for reset. IORDY may
1770 * lead to controller lock up on certain controllers if the
1771 * port is not occupied. See bko#11703 for details.
1773 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1774 return 0;
1775 /* Controller doesn't support IORDY. Probably a pointless
1776 * check as the caller should know this.
1778 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1779 return 0;
1780 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1781 if (ata_id_is_cfa(adev->id)
1782 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1783 return 0;
1784 /* PIO3 and higher it is mandatory */
1785 if (adev->pio_mode > XFER_PIO_2)
1786 return 1;
1787 /* We turn it on when possible */
1788 if (ata_id_has_iordy(adev->id))
1789 return 1;
1790 return 0;
1794 * ata_pio_mask_no_iordy - Return the non IORDY mask
1795 * @adev: ATA device
1797 * Compute the highest mode possible if we are not using iordy. Return
1798 * -1 if no iordy mode is available.
1800 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1802 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1803 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1804 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1805 /* Is the speed faster than the drive allows non IORDY ? */
1806 if (pio) {
1807 /* This is cycle times not frequency - watch the logic! */
1808 if (pio > 240) /* PIO2 is 240nS per cycle */
1809 return 3 << ATA_SHIFT_PIO;
1810 return 7 << ATA_SHIFT_PIO;
1813 return 3 << ATA_SHIFT_PIO;
1817 * ata_do_dev_read_id - default ID read method
1818 * @dev: device
1819 * @tf: proposed taskfile
1820 * @id: data buffer
1822 * Issue the identify taskfile and hand back the buffer containing
1823 * identify data. For some RAID controllers and for pre ATA devices
1824 * this function is wrapped or replaced by the driver
1826 unsigned int ata_do_dev_read_id(struct ata_device *dev,
1827 struct ata_taskfile *tf, u16 *id)
1829 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1830 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1834 * ata_dev_read_id - Read ID data from the specified device
1835 * @dev: target device
1836 * @p_class: pointer to class of the target device (may be changed)
1837 * @flags: ATA_READID_* flags
1838 * @id: buffer to read IDENTIFY data into
1840 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1841 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1842 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1843 * for pre-ATA4 drives.
1845 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1846 * now we abort if we hit that case.
1848 * LOCKING:
1849 * Kernel thread context (may sleep)
1851 * RETURNS:
1852 * 0 on success, -errno otherwise.
1854 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1855 unsigned int flags, u16 *id)
1857 struct ata_port *ap = dev->link->ap;
1858 unsigned int class = *p_class;
1859 struct ata_taskfile tf;
1860 unsigned int err_mask = 0;
1861 const char *reason;
1862 bool is_semb = class == ATA_DEV_SEMB;
1863 int may_fallback = 1, tried_spinup = 0;
1864 int rc;
1866 if (ata_msg_ctl(ap))
1867 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1869 retry:
1870 ata_tf_init(dev, &tf);
1872 switch (class) {
1873 case ATA_DEV_SEMB:
1874 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
1875 case ATA_DEV_ATA:
1876 case ATA_DEV_ZAC:
1877 tf.command = ATA_CMD_ID_ATA;
1878 break;
1879 case ATA_DEV_ATAPI:
1880 tf.command = ATA_CMD_ID_ATAPI;
1881 break;
1882 default:
1883 rc = -ENODEV;
1884 reason = "unsupported class";
1885 goto err_out;
1888 tf.protocol = ATA_PROT_PIO;
1890 /* Some devices choke if TF registers contain garbage. Make
1891 * sure those are properly initialized.
1893 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1895 /* Device presence detection is unreliable on some
1896 * controllers. Always poll IDENTIFY if available.
1898 tf.flags |= ATA_TFLAG_POLLING;
1900 if (ap->ops->read_id)
1901 err_mask = ap->ops->read_id(dev, &tf, id);
1902 else
1903 err_mask = ata_do_dev_read_id(dev, &tf, id);
1905 if (err_mask) {
1906 if (err_mask & AC_ERR_NODEV_HINT) {
1907 ata_dev_dbg(dev, "NODEV after polling detection\n");
1908 return -ENOENT;
1911 if (is_semb) {
1912 ata_dev_info(dev,
1913 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1914 /* SEMB is not supported yet */
1915 *p_class = ATA_DEV_SEMB_UNSUP;
1916 return 0;
1919 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1920 /* Device or controller might have reported
1921 * the wrong device class. Give a shot at the
1922 * other IDENTIFY if the current one is
1923 * aborted by the device.
1925 if (may_fallback) {
1926 may_fallback = 0;
1928 if (class == ATA_DEV_ATA)
1929 class = ATA_DEV_ATAPI;
1930 else
1931 class = ATA_DEV_ATA;
1932 goto retry;
1935 /* Control reaches here iff the device aborted
1936 * both flavors of IDENTIFYs which happens
1937 * sometimes with phantom devices.
1939 ata_dev_dbg(dev,
1940 "both IDENTIFYs aborted, assuming NODEV\n");
1941 return -ENOENT;
1944 rc = -EIO;
1945 reason = "I/O error";
1946 goto err_out;
1949 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1950 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1951 "class=%d may_fallback=%d tried_spinup=%d\n",
1952 class, may_fallback, tried_spinup);
1953 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1954 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1957 /* Falling back doesn't make sense if ID data was read
1958 * successfully at least once.
1960 may_fallback = 0;
1962 swap_buf_le16(id, ATA_ID_WORDS);
1964 /* sanity check */
1965 rc = -EINVAL;
1966 reason = "device reports invalid type";
1968 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
1969 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1970 goto err_out;
1971 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1972 ata_id_is_ata(id)) {
1973 ata_dev_dbg(dev,
1974 "host indicates ignore ATA devices, ignored\n");
1975 return -ENOENT;
1977 } else {
1978 if (ata_id_is_ata(id))
1979 goto err_out;
1982 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1983 tried_spinup = 1;
1985 * Drive powered-up in standby mode, and requires a specific
1986 * SET_FEATURES spin-up subcommand before it will accept
1987 * anything other than the original IDENTIFY command.
1989 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1990 if (err_mask && id[2] != 0x738c) {
1991 rc = -EIO;
1992 reason = "SPINUP failed";
1993 goto err_out;
1996 * If the drive initially returned incomplete IDENTIFY info,
1997 * we now must reissue the IDENTIFY command.
1999 if (id[2] == 0x37c8)
2000 goto retry;
2003 if ((flags & ATA_READID_POSTRESET) &&
2004 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
2006 * The exact sequence expected by certain pre-ATA4 drives is:
2007 * SRST RESET
2008 * IDENTIFY (optional in early ATA)
2009 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2010 * anything else..
2011 * Some drives were very specific about that exact sequence.
2013 * Note that ATA4 says lba is mandatory so the second check
2014 * should never trigger.
2016 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2017 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2018 if (err_mask) {
2019 rc = -EIO;
2020 reason = "INIT_DEV_PARAMS failed";
2021 goto err_out;
2024 /* current CHS translation info (id[53-58]) might be
2025 * changed. reread the identify device info.
2027 flags &= ~ATA_READID_POSTRESET;
2028 goto retry;
2032 *p_class = class;
2034 return 0;
2036 err_out:
2037 if (ata_msg_warn(ap))
2038 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2039 reason, err_mask);
2040 return rc;
2043 static int ata_do_link_spd_horkage(struct ata_device *dev)
2045 struct ata_link *plink = ata_dev_phys_link(dev);
2046 u32 target, target_limit;
2048 if (!sata_scr_valid(plink))
2049 return 0;
2051 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2052 target = 1;
2053 else
2054 return 0;
2056 target_limit = (1 << target) - 1;
2058 /* if already on stricter limit, no need to push further */
2059 if (plink->sata_spd_limit <= target_limit)
2060 return 0;
2062 plink->sata_spd_limit = target_limit;
2064 /* Request another EH round by returning -EAGAIN if link is
2065 * going faster than the target speed. Forward progress is
2066 * guaranteed by setting sata_spd_limit to target_limit above.
2068 if (plink->sata_spd > target) {
2069 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2070 sata_spd_string(target));
2071 return -EAGAIN;
2073 return 0;
2076 static inline u8 ata_dev_knobble(struct ata_device *dev)
2078 struct ata_port *ap = dev->link->ap;
2080 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2081 return 0;
2083 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2086 static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2088 struct ata_port *ap = dev->link->ap;
2089 unsigned int err_mask;
2090 int log_index = ATA_LOG_NCQ_SEND_RECV * 2;
2091 u16 log_pages;
2093 err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
2094 0, ap->sector_buf, 1);
2095 if (err_mask) {
2096 ata_dev_dbg(dev,
2097 "failed to get Log Directory Emask 0x%x\n",
2098 err_mask);
2099 return;
2101 log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
2102 if (!log_pages) {
2103 ata_dev_warn(dev,
2104 "NCQ Send/Recv Log not supported\n");
2105 return;
2107 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2108 0, ap->sector_buf, 1);
2109 if (err_mask) {
2110 ata_dev_dbg(dev,
2111 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2112 err_mask);
2113 } else {
2114 u8 *cmds = dev->ncq_send_recv_cmds;
2116 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2117 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2119 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2120 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2121 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2122 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2127 static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2129 struct ata_port *ap = dev->link->ap;
2130 unsigned int err_mask;
2131 int log_index = ATA_LOG_NCQ_NON_DATA * 2;
2132 u16 log_pages;
2134 err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
2135 0, ap->sector_buf, 1);
2136 if (err_mask) {
2137 ata_dev_dbg(dev,
2138 "failed to get Log Directory Emask 0x%x\n",
2139 err_mask);
2140 return;
2142 log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
2143 if (!log_pages) {
2144 ata_dev_warn(dev,
2145 "NCQ Send/Recv Log not supported\n");
2146 return;
2148 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2149 0, ap->sector_buf, 1);
2150 if (err_mask) {
2151 ata_dev_dbg(dev,
2152 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2153 err_mask);
2154 } else {
2155 u8 *cmds = dev->ncq_non_data_cmds;
2157 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2161 static int ata_dev_config_ncq(struct ata_device *dev,
2162 char *desc, size_t desc_sz)
2164 struct ata_port *ap = dev->link->ap;
2165 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2166 unsigned int err_mask;
2167 char *aa_desc = "";
2169 if (!ata_id_has_ncq(dev->id)) {
2170 desc[0] = '\0';
2171 return 0;
2173 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2174 snprintf(desc, desc_sz, "NCQ (not used)");
2175 return 0;
2177 if (ap->flags & ATA_FLAG_NCQ) {
2178 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2179 dev->flags |= ATA_DFLAG_NCQ;
2182 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2183 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2184 ata_id_has_fpdma_aa(dev->id)) {
2185 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2186 SATA_FPDMA_AA);
2187 if (err_mask) {
2188 ata_dev_err(dev,
2189 "failed to enable AA (error_mask=0x%x)\n",
2190 err_mask);
2191 if (err_mask != AC_ERR_DEV) {
2192 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2193 return -EIO;
2195 } else
2196 aa_desc = ", AA";
2199 if (hdepth >= ddepth)
2200 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2201 else
2202 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2203 ddepth, aa_desc);
2205 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2206 if (ata_id_has_ncq_send_and_recv(dev->id))
2207 ata_dev_config_ncq_send_recv(dev);
2208 if (ata_id_has_ncq_non_data(dev->id))
2209 ata_dev_config_ncq_non_data(dev);
2212 return 0;
2215 static void ata_dev_config_sense_reporting(struct ata_device *dev)
2217 unsigned int err_mask;
2219 if (!ata_id_has_sense_reporting(dev->id))
2220 return;
2222 if (ata_id_sense_reporting_enabled(dev->id))
2223 return;
2225 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2226 if (err_mask) {
2227 ata_dev_dbg(dev,
2228 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2229 err_mask);
2233 static void ata_dev_config_zac(struct ata_device *dev)
2235 struct ata_port *ap = dev->link->ap;
2236 unsigned int err_mask;
2237 u8 *identify_buf = ap->sector_buf;
2238 int log_index = ATA_LOG_SATA_ID_DEV_DATA * 2, i, found = 0;
2239 u16 log_pages;
2241 dev->zac_zones_optimal_open = U32_MAX;
2242 dev->zac_zones_optimal_nonseq = U32_MAX;
2243 dev->zac_zones_max_open = U32_MAX;
2246 * Always set the 'ZAC' flag for Host-managed devices.
2248 if (dev->class == ATA_DEV_ZAC)
2249 dev->flags |= ATA_DFLAG_ZAC;
2250 else if (ata_id_zoned_cap(dev->id) == 0x01)
2252 * Check for host-aware devices.
2254 dev->flags |= ATA_DFLAG_ZAC;
2256 if (!(dev->flags & ATA_DFLAG_ZAC))
2257 return;
2260 * Read Log Directory to figure out if IDENTIFY DEVICE log
2261 * is supported.
2263 err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
2264 0, ap->sector_buf, 1);
2265 if (err_mask) {
2266 ata_dev_info(dev,
2267 "failed to get Log Directory Emask 0x%x\n",
2268 err_mask);
2269 return;
2271 log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
2272 if (log_pages == 0) {
2273 ata_dev_warn(dev,
2274 "ATA Identify Device Log not supported\n");
2275 return;
2278 * Read IDENTIFY DEVICE data log, page 0, to figure out
2279 * if page 9 is supported.
2281 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_ID_DEV_DATA, 0,
2282 identify_buf, 1);
2283 if (err_mask) {
2284 ata_dev_info(dev,
2285 "failed to get Device Identify Log Emask 0x%x\n",
2286 err_mask);
2287 return;
2289 log_pages = identify_buf[8];
2290 for (i = 0; i < log_pages; i++) {
2291 if (identify_buf[9 + i] == ATA_LOG_ZONED_INFORMATION) {
2292 found++;
2293 break;
2296 if (!found) {
2297 ata_dev_warn(dev,
2298 "ATA Zoned Information Log not supported\n");
2299 return;
2303 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2305 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_ID_DEV_DATA,
2306 ATA_LOG_ZONED_INFORMATION,
2307 identify_buf, 1);
2308 if (!err_mask) {
2309 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2311 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2312 if ((zoned_cap >> 63))
2313 dev->zac_zoned_cap = (zoned_cap & 1);
2314 opt_open = get_unaligned_le64(&identify_buf[24]);
2315 if ((opt_open >> 63))
2316 dev->zac_zones_optimal_open = (u32)opt_open;
2317 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2318 if ((opt_nonseq >> 63))
2319 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2320 max_open = get_unaligned_le64(&identify_buf[40]);
2321 if ((max_open >> 63))
2322 dev->zac_zones_max_open = (u32)max_open;
2327 * ata_dev_configure - Configure the specified ATA/ATAPI device
2328 * @dev: Target device to configure
2330 * Configure @dev according to @dev->id. Generic and low-level
2331 * driver specific fixups are also applied.
2333 * LOCKING:
2334 * Kernel thread context (may sleep)
2336 * RETURNS:
2337 * 0 on success, -errno otherwise
2339 int ata_dev_configure(struct ata_device *dev)
2341 struct ata_port *ap = dev->link->ap;
2342 struct ata_eh_context *ehc = &dev->link->eh_context;
2343 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2344 const u16 *id = dev->id;
2345 unsigned long xfer_mask;
2346 unsigned int err_mask;
2347 char revbuf[7]; /* XYZ-99\0 */
2348 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2349 char modelbuf[ATA_ID_PROD_LEN+1];
2350 int rc;
2352 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2353 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2354 return 0;
2357 if (ata_msg_probe(ap))
2358 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2360 /* set horkage */
2361 dev->horkage |= ata_dev_blacklisted(dev);
2362 ata_force_horkage(dev);
2364 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2365 ata_dev_info(dev, "unsupported device, disabling\n");
2366 ata_dev_disable(dev);
2367 return 0;
2370 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2371 dev->class == ATA_DEV_ATAPI) {
2372 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2373 atapi_enabled ? "not supported with this driver"
2374 : "disabled");
2375 ata_dev_disable(dev);
2376 return 0;
2379 rc = ata_do_link_spd_horkage(dev);
2380 if (rc)
2381 return rc;
2383 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2384 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2385 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2386 dev->horkage |= ATA_HORKAGE_NOLPM;
2388 if (ap->flags & ATA_FLAG_NO_LPM)
2389 dev->horkage |= ATA_HORKAGE_NOLPM;
2391 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2392 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2393 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2396 /* let ACPI work its magic */
2397 rc = ata_acpi_on_devcfg(dev);
2398 if (rc)
2399 return rc;
2401 /* massage HPA, do it early as it might change IDENTIFY data */
2402 rc = ata_hpa_resize(dev);
2403 if (rc)
2404 return rc;
2406 /* print device capabilities */
2407 if (ata_msg_probe(ap))
2408 ata_dev_dbg(dev,
2409 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2410 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2411 __func__,
2412 id[49], id[82], id[83], id[84],
2413 id[85], id[86], id[87], id[88]);
2415 /* initialize to-be-configured parameters */
2416 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2417 dev->max_sectors = 0;
2418 dev->cdb_len = 0;
2419 dev->n_sectors = 0;
2420 dev->cylinders = 0;
2421 dev->heads = 0;
2422 dev->sectors = 0;
2423 dev->multi_count = 0;
2426 * common ATA, ATAPI feature tests
2429 /* find max transfer mode; for printk only */
2430 xfer_mask = ata_id_xfermask(id);
2432 if (ata_msg_probe(ap))
2433 ata_dump_id(id);
2435 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2436 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2437 sizeof(fwrevbuf));
2439 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2440 sizeof(modelbuf));
2442 /* ATA-specific feature tests */
2443 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2444 if (ata_id_is_cfa(id)) {
2445 /* CPRM may make this media unusable */
2446 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2447 ata_dev_warn(dev,
2448 "supports DRM functions and may not be fully accessible\n");
2449 snprintf(revbuf, 7, "CFA");
2450 } else {
2451 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2452 /* Warn the user if the device has TPM extensions */
2453 if (ata_id_has_tpm(id))
2454 ata_dev_warn(dev,
2455 "supports DRM functions and may not be fully accessible\n");
2458 dev->n_sectors = ata_id_n_sectors(id);
2460 /* get current R/W Multiple count setting */
2461 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2462 unsigned int max = dev->id[47] & 0xff;
2463 unsigned int cnt = dev->id[59] & 0xff;
2464 /* only recognize/allow powers of two here */
2465 if (is_power_of_2(max) && is_power_of_2(cnt))
2466 if (cnt <= max)
2467 dev->multi_count = cnt;
2470 if (ata_id_has_lba(id)) {
2471 const char *lba_desc;
2472 char ncq_desc[24];
2474 lba_desc = "LBA";
2475 dev->flags |= ATA_DFLAG_LBA;
2476 if (ata_id_has_lba48(id)) {
2477 dev->flags |= ATA_DFLAG_LBA48;
2478 lba_desc = "LBA48";
2480 if (dev->n_sectors >= (1UL << 28) &&
2481 ata_id_has_flush_ext(id))
2482 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2485 /* config NCQ */
2486 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2487 if (rc)
2488 return rc;
2490 /* print device info to dmesg */
2491 if (ata_msg_drv(ap) && print_info) {
2492 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2493 revbuf, modelbuf, fwrevbuf,
2494 ata_mode_string(xfer_mask));
2495 ata_dev_info(dev,
2496 "%llu sectors, multi %u: %s %s\n",
2497 (unsigned long long)dev->n_sectors,
2498 dev->multi_count, lba_desc, ncq_desc);
2500 } else {
2501 /* CHS */
2503 /* Default translation */
2504 dev->cylinders = id[1];
2505 dev->heads = id[3];
2506 dev->sectors = id[6];
2508 if (ata_id_current_chs_valid(id)) {
2509 /* Current CHS translation is valid. */
2510 dev->cylinders = id[54];
2511 dev->heads = id[55];
2512 dev->sectors = id[56];
2515 /* print device info to dmesg */
2516 if (ata_msg_drv(ap) && print_info) {
2517 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2518 revbuf, modelbuf, fwrevbuf,
2519 ata_mode_string(xfer_mask));
2520 ata_dev_info(dev,
2521 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2522 (unsigned long long)dev->n_sectors,
2523 dev->multi_count, dev->cylinders,
2524 dev->heads, dev->sectors);
2528 /* Check and mark DevSlp capability. Get DevSlp timing variables
2529 * from SATA Settings page of Identify Device Data Log.
2531 if (ata_id_has_devslp(dev->id)) {
2532 u8 *sata_setting = ap->sector_buf;
2533 int i, j;
2535 dev->flags |= ATA_DFLAG_DEVSLP;
2536 err_mask = ata_read_log_page(dev,
2537 ATA_LOG_SATA_ID_DEV_DATA,
2538 ATA_LOG_SATA_SETTINGS,
2539 sata_setting,
2541 if (err_mask)
2542 ata_dev_dbg(dev,
2543 "failed to get Identify Device Data, Emask 0x%x\n",
2544 err_mask);
2545 else
2546 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2547 j = ATA_LOG_DEVSLP_OFFSET + i;
2548 dev->devslp_timing[i] = sata_setting[j];
2551 ata_dev_config_sense_reporting(dev);
2552 ata_dev_config_zac(dev);
2553 dev->cdb_len = 16;
2556 /* ATAPI-specific feature tests */
2557 else if (dev->class == ATA_DEV_ATAPI) {
2558 const char *cdb_intr_string = "";
2559 const char *atapi_an_string = "";
2560 const char *dma_dir_string = "";
2561 u32 sntf;
2563 rc = atapi_cdb_len(id);
2564 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2565 if (ata_msg_warn(ap))
2566 ata_dev_warn(dev, "unsupported CDB len\n");
2567 rc = -EINVAL;
2568 goto err_out_nosup;
2570 dev->cdb_len = (unsigned int) rc;
2572 /* Enable ATAPI AN if both the host and device have
2573 * the support. If PMP is attached, SNTF is required
2574 * to enable ATAPI AN to discern between PHY status
2575 * changed notifications and ATAPI ANs.
2577 if (atapi_an &&
2578 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2579 (!sata_pmp_attached(ap) ||
2580 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2581 /* issue SET feature command to turn this on */
2582 err_mask = ata_dev_set_feature(dev,
2583 SETFEATURES_SATA_ENABLE, SATA_AN);
2584 if (err_mask)
2585 ata_dev_err(dev,
2586 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2587 err_mask);
2588 else {
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 || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
2600 dev->flags |= ATA_DFLAG_DMADIR;
2601 dma_dir_string = ", DMADIR";
2604 if (ata_id_has_da(dev->id)) {
2605 dev->flags |= ATA_DFLAG_DA;
2606 zpodd_init(dev);
2609 /* print device info to dmesg */
2610 if (ata_msg_drv(ap) && print_info)
2611 ata_dev_info(dev,
2612 "ATAPI: %s, %s, max %s%s%s%s\n",
2613 modelbuf, fwrevbuf,
2614 ata_mode_string(xfer_mask),
2615 cdb_intr_string, atapi_an_string,
2616 dma_dir_string);
2619 /* determine max_sectors */
2620 dev->max_sectors = ATA_MAX_SECTORS;
2621 if (dev->flags & ATA_DFLAG_LBA48)
2622 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2624 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2625 200 sectors */
2626 if (ata_dev_knobble(dev)) {
2627 if (ata_msg_drv(ap) && print_info)
2628 ata_dev_info(dev, "applying bridge limits\n");
2629 dev->udma_mask &= ATA_UDMA5;
2630 dev->max_sectors = ATA_MAX_SECTORS;
2633 if ((dev->class == ATA_DEV_ATAPI) &&
2634 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2635 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2636 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2639 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2640 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2641 dev->max_sectors);
2643 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2644 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2645 dev->max_sectors);
2647 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2648 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2650 if (ap->ops->dev_config)
2651 ap->ops->dev_config(dev);
2653 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2654 /* Let the user know. We don't want to disallow opens for
2655 rescue purposes, or in case the vendor is just a blithering
2656 idiot. Do this after the dev_config call as some controllers
2657 with buggy firmware may want to avoid reporting false device
2658 bugs */
2660 if (print_info) {
2661 ata_dev_warn(dev,
2662 "Drive reports diagnostics failure. This may indicate a drive\n");
2663 ata_dev_warn(dev,
2664 "fault or invalid emulation. Contact drive vendor for information.\n");
2668 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2669 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2670 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
2673 return 0;
2675 err_out_nosup:
2676 if (ata_msg_probe(ap))
2677 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2678 return rc;
2682 * ata_cable_40wire - return 40 wire cable type
2683 * @ap: port
2685 * Helper method for drivers which want to hardwire 40 wire cable
2686 * detection.
2689 int ata_cable_40wire(struct ata_port *ap)
2691 return ATA_CBL_PATA40;
2695 * ata_cable_80wire - return 80 wire cable type
2696 * @ap: port
2698 * Helper method for drivers which want to hardwire 80 wire cable
2699 * detection.
2702 int ata_cable_80wire(struct ata_port *ap)
2704 return ATA_CBL_PATA80;
2708 * ata_cable_unknown - return unknown PATA cable.
2709 * @ap: port
2711 * Helper method for drivers which have no PATA cable detection.
2714 int ata_cable_unknown(struct ata_port *ap)
2716 return ATA_CBL_PATA_UNK;
2720 * ata_cable_ignore - return ignored PATA cable.
2721 * @ap: port
2723 * Helper method for drivers which don't use cable type to limit
2724 * transfer mode.
2726 int ata_cable_ignore(struct ata_port *ap)
2728 return ATA_CBL_PATA_IGN;
2732 * ata_cable_sata - return SATA cable type
2733 * @ap: port
2735 * Helper method for drivers which have SATA cables
2738 int ata_cable_sata(struct ata_port *ap)
2740 return ATA_CBL_SATA;
2744 * ata_bus_probe - Reset and probe ATA bus
2745 * @ap: Bus to probe
2747 * Master ATA bus probing function. Initiates a hardware-dependent
2748 * bus reset, then attempts to identify any devices found on
2749 * the bus.
2751 * LOCKING:
2752 * PCI/etc. bus probe sem.
2754 * RETURNS:
2755 * Zero on success, negative errno otherwise.
2758 int ata_bus_probe(struct ata_port *ap)
2760 unsigned int classes[ATA_MAX_DEVICES];
2761 int tries[ATA_MAX_DEVICES];
2762 int rc;
2763 struct ata_device *dev;
2765 ata_for_each_dev(dev, &ap->link, ALL)
2766 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2768 retry:
2769 ata_for_each_dev(dev, &ap->link, ALL) {
2770 /* If we issue an SRST then an ATA drive (not ATAPI)
2771 * may change configuration and be in PIO0 timing. If
2772 * we do a hard reset (or are coming from power on)
2773 * this is true for ATA or ATAPI. Until we've set a
2774 * suitable controller mode we should not touch the
2775 * bus as we may be talking too fast.
2777 dev->pio_mode = XFER_PIO_0;
2778 dev->dma_mode = 0xff;
2780 /* If the controller has a pio mode setup function
2781 * then use it to set the chipset to rights. Don't
2782 * touch the DMA setup as that will be dealt with when
2783 * configuring devices.
2785 if (ap->ops->set_piomode)
2786 ap->ops->set_piomode(ap, dev);
2789 /* reset and determine device classes */
2790 ap->ops->phy_reset(ap);
2792 ata_for_each_dev(dev, &ap->link, ALL) {
2793 if (dev->class != ATA_DEV_UNKNOWN)
2794 classes[dev->devno] = dev->class;
2795 else
2796 classes[dev->devno] = ATA_DEV_NONE;
2798 dev->class = ATA_DEV_UNKNOWN;
2801 /* read IDENTIFY page and configure devices. We have to do the identify
2802 specific sequence bass-ackwards so that PDIAG- is released by
2803 the slave device */
2805 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2806 if (tries[dev->devno])
2807 dev->class = classes[dev->devno];
2809 if (!ata_dev_enabled(dev))
2810 continue;
2812 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2813 dev->id);
2814 if (rc)
2815 goto fail;
2818 /* Now ask for the cable type as PDIAG- should have been released */
2819 if (ap->ops->cable_detect)
2820 ap->cbl = ap->ops->cable_detect(ap);
2822 /* We may have SATA bridge glue hiding here irrespective of
2823 * the reported cable types and sensed types. When SATA
2824 * drives indicate we have a bridge, we don't know which end
2825 * of the link the bridge is which is a problem.
2827 ata_for_each_dev(dev, &ap->link, ENABLED)
2828 if (ata_id_is_sata(dev->id))
2829 ap->cbl = ATA_CBL_SATA;
2831 /* After the identify sequence we can now set up the devices. We do
2832 this in the normal order so that the user doesn't get confused */
2834 ata_for_each_dev(dev, &ap->link, ENABLED) {
2835 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2836 rc = ata_dev_configure(dev);
2837 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2838 if (rc)
2839 goto fail;
2842 /* configure transfer mode */
2843 rc = ata_set_mode(&ap->link, &dev);
2844 if (rc)
2845 goto fail;
2847 ata_for_each_dev(dev, &ap->link, ENABLED)
2848 return 0;
2850 return -ENODEV;
2852 fail:
2853 tries[dev->devno]--;
2855 switch (rc) {
2856 case -EINVAL:
2857 /* eeek, something went very wrong, give up */
2858 tries[dev->devno] = 0;
2859 break;
2861 case -ENODEV:
2862 /* give it just one more chance */
2863 tries[dev->devno] = min(tries[dev->devno], 1);
2864 case -EIO:
2865 if (tries[dev->devno] == 1) {
2866 /* This is the last chance, better to slow
2867 * down than lose it.
2869 sata_down_spd_limit(&ap->link, 0);
2870 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2874 if (!tries[dev->devno])
2875 ata_dev_disable(dev);
2877 goto retry;
2881 * sata_print_link_status - Print SATA link status
2882 * @link: SATA link to printk link status about
2884 * This function prints link speed and status of a SATA link.
2886 * LOCKING:
2887 * None.
2889 static void sata_print_link_status(struct ata_link *link)
2891 u32 sstatus, scontrol, tmp;
2893 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2894 return;
2895 sata_scr_read(link, SCR_CONTROL, &scontrol);
2897 if (ata_phys_link_online(link)) {
2898 tmp = (sstatus >> 4) & 0xf;
2899 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2900 sata_spd_string(tmp), sstatus, scontrol);
2901 } else {
2902 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2903 sstatus, scontrol);
2908 * ata_dev_pair - return other device on cable
2909 * @adev: device
2911 * Obtain the other device on the same cable, or if none is
2912 * present NULL is returned
2915 struct ata_device *ata_dev_pair(struct ata_device *adev)
2917 struct ata_link *link = adev->link;
2918 struct ata_device *pair = &link->device[1 - adev->devno];
2919 if (!ata_dev_enabled(pair))
2920 return NULL;
2921 return pair;
2925 * sata_down_spd_limit - adjust SATA spd limit downward
2926 * @link: Link to adjust SATA spd limit for
2927 * @spd_limit: Additional limit
2929 * Adjust SATA spd limit of @link downward. Note that this
2930 * function only adjusts the limit. The change must be applied
2931 * using sata_set_spd().
2933 * If @spd_limit is non-zero, the speed is limited to equal to or
2934 * lower than @spd_limit if such speed is supported. If
2935 * @spd_limit is slower than any supported speed, only the lowest
2936 * supported speed is allowed.
2938 * LOCKING:
2939 * Inherited from caller.
2941 * RETURNS:
2942 * 0 on success, negative errno on failure
2944 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2946 u32 sstatus, spd, mask;
2947 int rc, bit;
2949 if (!sata_scr_valid(link))
2950 return -EOPNOTSUPP;
2952 /* If SCR can be read, use it to determine the current SPD.
2953 * If not, use cached value in link->sata_spd.
2955 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2956 if (rc == 0 && ata_sstatus_online(sstatus))
2957 spd = (sstatus >> 4) & 0xf;
2958 else
2959 spd = link->sata_spd;
2961 mask = link->sata_spd_limit;
2962 if (mask <= 1)
2963 return -EINVAL;
2965 /* unconditionally mask off the highest bit */
2966 bit = fls(mask) - 1;
2967 mask &= ~(1 << bit);
2969 /* Mask off all speeds higher than or equal to the current
2970 * one. Force 1.5Gbps if current SPD is not available.
2972 if (spd > 1)
2973 mask &= (1 << (spd - 1)) - 1;
2974 else
2975 mask &= 1;
2977 /* were we already at the bottom? */
2978 if (!mask)
2979 return -EINVAL;
2981 if (spd_limit) {
2982 if (mask & ((1 << spd_limit) - 1))
2983 mask &= (1 << spd_limit) - 1;
2984 else {
2985 bit = ffs(mask) - 1;
2986 mask = 1 << bit;
2990 link->sata_spd_limit = mask;
2992 ata_link_warn(link, "limiting SATA link speed to %s\n",
2993 sata_spd_string(fls(mask)));
2995 return 0;
2998 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3000 struct ata_link *host_link = &link->ap->link;
3001 u32 limit, target, spd;
3003 limit = link->sata_spd_limit;
3005 /* Don't configure downstream link faster than upstream link.
3006 * It doesn't speed up anything and some PMPs choke on such
3007 * configuration.
3009 if (!ata_is_host_link(link) && host_link->sata_spd)
3010 limit &= (1 << host_link->sata_spd) - 1;
3012 if (limit == UINT_MAX)
3013 target = 0;
3014 else
3015 target = fls(limit);
3017 spd = (*scontrol >> 4) & 0xf;
3018 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3020 return spd != target;
3024 * sata_set_spd_needed - is SATA spd configuration needed
3025 * @link: Link in question
3027 * Test whether the spd limit in SControl matches
3028 * @link->sata_spd_limit. This function is used to determine
3029 * whether hardreset is necessary to apply SATA spd
3030 * configuration.
3032 * LOCKING:
3033 * Inherited from caller.
3035 * RETURNS:
3036 * 1 if SATA spd configuration is needed, 0 otherwise.
3038 static int sata_set_spd_needed(struct ata_link *link)
3040 u32 scontrol;
3042 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3043 return 1;
3045 return __sata_set_spd_needed(link, &scontrol);
3049 * sata_set_spd - set SATA spd according to spd limit
3050 * @link: Link to set SATA spd for
3052 * Set SATA spd of @link according to sata_spd_limit.
3054 * LOCKING:
3055 * Inherited from caller.
3057 * RETURNS:
3058 * 0 if spd doesn't need to be changed, 1 if spd has been
3059 * changed. Negative errno if SCR registers are inaccessible.
3061 int sata_set_spd(struct ata_link *link)
3063 u32 scontrol;
3064 int rc;
3066 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3067 return rc;
3069 if (!__sata_set_spd_needed(link, &scontrol))
3070 return 0;
3072 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3073 return rc;
3075 return 1;
3079 * This mode timing computation functionality is ported over from
3080 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3083 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3084 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3085 * for UDMA6, which is currently supported only by Maxtor drives.
3087 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3090 static const struct ata_timing ata_timing[] = {
3091 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3092 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3093 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3094 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3095 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3096 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3097 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3098 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
3100 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3101 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3102 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
3104 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3105 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3106 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3107 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3108 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
3110 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3111 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3112 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3113 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3114 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3115 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3116 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3117 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
3119 { 0xFF }
3122 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3123 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
3125 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3127 q->setup = EZ(t->setup * 1000, T);
3128 q->act8b = EZ(t->act8b * 1000, T);
3129 q->rec8b = EZ(t->rec8b * 1000, T);
3130 q->cyc8b = EZ(t->cyc8b * 1000, T);
3131 q->active = EZ(t->active * 1000, T);
3132 q->recover = EZ(t->recover * 1000, T);
3133 q->dmack_hold = EZ(t->dmack_hold * 1000, T);
3134 q->cycle = EZ(t->cycle * 1000, T);
3135 q->udma = EZ(t->udma * 1000, UT);
3138 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3139 struct ata_timing *m, unsigned int what)
3141 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3142 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3143 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3144 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3145 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3146 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3147 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3148 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3149 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3152 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3154 const struct ata_timing *t = ata_timing;
3156 while (xfer_mode > t->mode)
3157 t++;
3159 if (xfer_mode == t->mode)
3160 return t;
3162 WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3163 __func__, xfer_mode);
3165 return NULL;
3168 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3169 struct ata_timing *t, int T, int UT)
3171 const u16 *id = adev->id;
3172 const struct ata_timing *s;
3173 struct ata_timing p;
3176 * Find the mode.
3179 if (!(s = ata_timing_find_mode(speed)))
3180 return -EINVAL;
3182 memcpy(t, s, sizeof(*s));
3185 * If the drive is an EIDE drive, it can tell us it needs extended
3186 * PIO/MW_DMA cycle timing.
3189 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
3190 memset(&p, 0, sizeof(p));
3192 if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
3193 if (speed <= XFER_PIO_2)
3194 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3195 else if ((speed <= XFER_PIO_4) ||
3196 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3197 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3198 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3199 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3201 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3205 * Convert the timing to bus clock counts.
3208 ata_timing_quantize(t, t, T, UT);
3211 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3212 * S.M.A.R.T * and some other commands. We have to ensure that the
3213 * DMA cycle timing is slower/equal than the fastest PIO timing.
3216 if (speed > XFER_PIO_6) {
3217 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3218 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3222 * Lengthen active & recovery time so that cycle time is correct.
3225 if (t->act8b + t->rec8b < t->cyc8b) {
3226 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3227 t->rec8b = t->cyc8b - t->act8b;
3230 if (t->active + t->recover < t->cycle) {
3231 t->active += (t->cycle - (t->active + t->recover)) / 2;
3232 t->recover = t->cycle - t->active;
3235 /* In a few cases quantisation may produce enough errors to
3236 leave t->cycle too low for the sum of active and recovery
3237 if so we must correct this */
3238 if (t->active + t->recover > t->cycle)
3239 t->cycle = t->active + t->recover;
3241 return 0;
3245 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3246 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3247 * @cycle: cycle duration in ns
3249 * Return matching xfer mode for @cycle. The returned mode is of
3250 * the transfer type specified by @xfer_shift. If @cycle is too
3251 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3252 * than the fastest known mode, the fasted mode is returned.
3254 * LOCKING:
3255 * None.
3257 * RETURNS:
3258 * Matching xfer_mode, 0xff if no match found.
3260 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3262 u8 base_mode = 0xff, last_mode = 0xff;
3263 const struct ata_xfer_ent *ent;
3264 const struct ata_timing *t;
3266 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3267 if (ent->shift == xfer_shift)
3268 base_mode = ent->base;
3270 for (t = ata_timing_find_mode(base_mode);
3271 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3272 unsigned short this_cycle;
3274 switch (xfer_shift) {
3275 case ATA_SHIFT_PIO:
3276 case ATA_SHIFT_MWDMA:
3277 this_cycle = t->cycle;
3278 break;
3279 case ATA_SHIFT_UDMA:
3280 this_cycle = t->udma;
3281 break;
3282 default:
3283 return 0xff;
3286 if (cycle > this_cycle)
3287 break;
3289 last_mode = t->mode;
3292 return last_mode;
3296 * ata_down_xfermask_limit - adjust dev xfer masks downward
3297 * @dev: Device to adjust xfer masks
3298 * @sel: ATA_DNXFER_* selector
3300 * Adjust xfer masks of @dev downward. Note that this function
3301 * does not apply the change. Invoking ata_set_mode() afterwards
3302 * will apply the limit.
3304 * LOCKING:
3305 * Inherited from caller.
3307 * RETURNS:
3308 * 0 on success, negative errno on failure
3310 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3312 char buf[32];
3313 unsigned long orig_mask, xfer_mask;
3314 unsigned long pio_mask, mwdma_mask, udma_mask;
3315 int quiet, highbit;
3317 quiet = !!(sel & ATA_DNXFER_QUIET);
3318 sel &= ~ATA_DNXFER_QUIET;
3320 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3321 dev->mwdma_mask,
3322 dev->udma_mask);
3323 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3325 switch (sel) {
3326 case ATA_DNXFER_PIO:
3327 highbit = fls(pio_mask) - 1;
3328 pio_mask &= ~(1 << highbit);
3329 break;
3331 case ATA_DNXFER_DMA:
3332 if (udma_mask) {
3333 highbit = fls(udma_mask) - 1;
3334 udma_mask &= ~(1 << highbit);
3335 if (!udma_mask)
3336 return -ENOENT;
3337 } else if (mwdma_mask) {
3338 highbit = fls(mwdma_mask) - 1;
3339 mwdma_mask &= ~(1 << highbit);
3340 if (!mwdma_mask)
3341 return -ENOENT;
3343 break;
3345 case ATA_DNXFER_40C:
3346 udma_mask &= ATA_UDMA_MASK_40C;
3347 break;
3349 case ATA_DNXFER_FORCE_PIO0:
3350 pio_mask &= 1;
3351 case ATA_DNXFER_FORCE_PIO:
3352 mwdma_mask = 0;
3353 udma_mask = 0;
3354 break;
3356 default:
3357 BUG();
3360 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3362 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3363 return -ENOENT;
3365 if (!quiet) {
3366 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3367 snprintf(buf, sizeof(buf), "%s:%s",
3368 ata_mode_string(xfer_mask),
3369 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3370 else
3371 snprintf(buf, sizeof(buf), "%s",
3372 ata_mode_string(xfer_mask));
3374 ata_dev_warn(dev, "limiting speed to %s\n", buf);
3377 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3378 &dev->udma_mask);
3380 return 0;
3383 static int ata_dev_set_mode(struct ata_device *dev)
3385 struct ata_port *ap = dev->link->ap;
3386 struct ata_eh_context *ehc = &dev->link->eh_context;
3387 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3388 const char *dev_err_whine = "";
3389 int ign_dev_err = 0;
3390 unsigned int err_mask = 0;
3391 int rc;
3393 dev->flags &= ~ATA_DFLAG_PIO;
3394 if (dev->xfer_shift == ATA_SHIFT_PIO)
3395 dev->flags |= ATA_DFLAG_PIO;
3397 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3398 dev_err_whine = " (SET_XFERMODE skipped)";
3399 else {
3400 if (nosetxfer)
3401 ata_dev_warn(dev,
3402 "NOSETXFER but PATA detected - can't "
3403 "skip SETXFER, might malfunction\n");
3404 err_mask = ata_dev_set_xfermode(dev);
3407 if (err_mask & ~AC_ERR_DEV)
3408 goto fail;
3410 /* revalidate */
3411 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3412 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3413 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3414 if (rc)
3415 return rc;
3417 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3418 /* Old CFA may refuse this command, which is just fine */
3419 if (ata_id_is_cfa(dev->id))
3420 ign_dev_err = 1;
3421 /* Catch several broken garbage emulations plus some pre
3422 ATA devices */
3423 if (ata_id_major_version(dev->id) == 0 &&
3424 dev->pio_mode <= XFER_PIO_2)
3425 ign_dev_err = 1;
3426 /* Some very old devices and some bad newer ones fail
3427 any kind of SET_XFERMODE request but support PIO0-2
3428 timings and no IORDY */
3429 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3430 ign_dev_err = 1;
3432 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3433 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3434 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3435 dev->dma_mode == XFER_MW_DMA_0 &&
3436 (dev->id[63] >> 8) & 1)
3437 ign_dev_err = 1;
3439 /* if the device is actually configured correctly, ignore dev err */
3440 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3441 ign_dev_err = 1;
3443 if (err_mask & AC_ERR_DEV) {
3444 if (!ign_dev_err)
3445 goto fail;
3446 else
3447 dev_err_whine = " (device error ignored)";
3450 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3451 dev->xfer_shift, (int)dev->xfer_mode);
3453 ata_dev_info(dev, "configured for %s%s\n",
3454 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3455 dev_err_whine);
3457 return 0;
3459 fail:
3460 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
3461 return -EIO;
3465 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3466 * @link: link on which timings will be programmed
3467 * @r_failed_dev: out parameter for failed device
3469 * Standard implementation of the function used to tune and set
3470 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3471 * ata_dev_set_mode() fails, pointer to the failing device is
3472 * returned in @r_failed_dev.
3474 * LOCKING:
3475 * PCI/etc. bus probe sem.
3477 * RETURNS:
3478 * 0 on success, negative errno otherwise
3481 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3483 struct ata_port *ap = link->ap;
3484 struct ata_device *dev;
3485 int rc = 0, used_dma = 0, found = 0;
3487 /* step 1: calculate xfer_mask */
3488 ata_for_each_dev(dev, link, ENABLED) {
3489 unsigned long pio_mask, dma_mask;
3490 unsigned int mode_mask;
3492 mode_mask = ATA_DMA_MASK_ATA;
3493 if (dev->class == ATA_DEV_ATAPI)
3494 mode_mask = ATA_DMA_MASK_ATAPI;
3495 else if (ata_id_is_cfa(dev->id))
3496 mode_mask = ATA_DMA_MASK_CFA;
3498 ata_dev_xfermask(dev);
3499 ata_force_xfermask(dev);
3501 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3503 if (libata_dma_mask & mode_mask)
3504 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3505 dev->udma_mask);
3506 else
3507 dma_mask = 0;
3509 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3510 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3512 found = 1;
3513 if (ata_dma_enabled(dev))
3514 used_dma = 1;
3516 if (!found)
3517 goto out;
3519 /* step 2: always set host PIO timings */
3520 ata_for_each_dev(dev, link, ENABLED) {
3521 if (dev->pio_mode == 0xff) {
3522 ata_dev_warn(dev, "no PIO support\n");
3523 rc = -EINVAL;
3524 goto out;
3527 dev->xfer_mode = dev->pio_mode;
3528 dev->xfer_shift = ATA_SHIFT_PIO;
3529 if (ap->ops->set_piomode)
3530 ap->ops->set_piomode(ap, dev);
3533 /* step 3: set host DMA timings */
3534 ata_for_each_dev(dev, link, ENABLED) {
3535 if (!ata_dma_enabled(dev))
3536 continue;
3538 dev->xfer_mode = dev->dma_mode;
3539 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3540 if (ap->ops->set_dmamode)
3541 ap->ops->set_dmamode(ap, dev);
3544 /* step 4: update devices' xfer mode */
3545 ata_for_each_dev(dev, link, ENABLED) {
3546 rc = ata_dev_set_mode(dev);
3547 if (rc)
3548 goto out;
3551 /* Record simplex status. If we selected DMA then the other
3552 * host channels are not permitted to do so.
3554 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3555 ap->host->simplex_claimed = ap;
3557 out:
3558 if (rc)
3559 *r_failed_dev = dev;
3560 return rc;
3564 * ata_wait_ready - wait for link to become ready
3565 * @link: link to be waited on
3566 * @deadline: deadline jiffies for the operation
3567 * @check_ready: callback to check link readiness
3569 * Wait for @link to become ready. @check_ready should return
3570 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3571 * link doesn't seem to be occupied, other errno for other error
3572 * conditions.
3574 * Transient -ENODEV conditions are allowed for
3575 * ATA_TMOUT_FF_WAIT.
3577 * LOCKING:
3578 * EH context.
3580 * RETURNS:
3581 * 0 if @link is ready before @deadline; otherwise, -errno.
3583 int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3584 int (*check_ready)(struct ata_link *link))
3586 unsigned long start = jiffies;
3587 unsigned long nodev_deadline;
3588 int warned = 0;
3590 /* choose which 0xff timeout to use, read comment in libata.h */
3591 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3592 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3593 else
3594 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3596 /* Slave readiness can't be tested separately from master. On
3597 * M/S emulation configuration, this function should be called
3598 * only on the master and it will handle both master and slave.
3600 WARN_ON(link == link->ap->slave_link);
3602 if (time_after(nodev_deadline, deadline))
3603 nodev_deadline = deadline;
3605 while (1) {
3606 unsigned long now = jiffies;
3607 int ready, tmp;
3609 ready = tmp = check_ready(link);
3610 if (ready > 0)
3611 return 0;
3614 * -ENODEV could be transient. Ignore -ENODEV if link
3615 * is online. Also, some SATA devices take a long
3616 * time to clear 0xff after reset. Wait for
3617 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3618 * offline.
3620 * Note that some PATA controllers (pata_ali) explode
3621 * if status register is read more than once when
3622 * there's no device attached.
3624 if (ready == -ENODEV) {
3625 if (ata_link_online(link))
3626 ready = 0;
3627 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3628 !ata_link_offline(link) &&
3629 time_before(now, nodev_deadline))
3630 ready = 0;
3633 if (ready)
3634 return ready;
3635 if (time_after(now, deadline))
3636 return -EBUSY;
3638 if (!warned && time_after(now, start + 5 * HZ) &&
3639 (deadline - now > 3 * HZ)) {
3640 ata_link_warn(link,
3641 "link is slow to respond, please be patient "
3642 "(ready=%d)\n", tmp);
3643 warned = 1;
3646 ata_msleep(link->ap, 50);
3651 * ata_wait_after_reset - wait for link to become ready after reset
3652 * @link: link to be waited on
3653 * @deadline: deadline jiffies for the operation
3654 * @check_ready: callback to check link readiness
3656 * Wait for @link to become ready after reset.
3658 * LOCKING:
3659 * EH context.
3661 * RETURNS:
3662 * 0 if @link is ready before @deadline; otherwise, -errno.
3664 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3665 int (*check_ready)(struct ata_link *link))
3667 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3669 return ata_wait_ready(link, deadline, check_ready);
3673 * sata_link_debounce - debounce SATA phy status
3674 * @link: ATA link to debounce SATA phy status for
3675 * @params: timing parameters { interval, duration, timeout } in msec
3676 * @deadline: deadline jiffies for the operation
3678 * Make sure SStatus of @link reaches stable state, determined by
3679 * holding the same value where DET is not 1 for @duration polled
3680 * every @interval, before @timeout. Timeout constraints the
3681 * beginning of the stable state. Because DET gets stuck at 1 on
3682 * some controllers after hot unplugging, this functions waits
3683 * until timeout then returns 0 if DET is stable at 1.
3685 * @timeout is further limited by @deadline. The sooner of the
3686 * two is used.
3688 * LOCKING:
3689 * Kernel thread context (may sleep)
3691 * RETURNS:
3692 * 0 on success, -errno on failure.
3694 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3695 unsigned long deadline)
3697 unsigned long interval = params[0];
3698 unsigned long duration = params[1];
3699 unsigned long last_jiffies, t;
3700 u32 last, cur;
3701 int rc;
3703 t = ata_deadline(jiffies, params[2]);
3704 if (time_before(t, deadline))
3705 deadline = t;
3707 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3708 return rc;
3709 cur &= 0xf;
3711 last = cur;
3712 last_jiffies = jiffies;
3714 while (1) {
3715 ata_msleep(link->ap, interval);
3716 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3717 return rc;
3718 cur &= 0xf;
3720 /* DET stable? */
3721 if (cur == last) {
3722 if (cur == 1 && time_before(jiffies, deadline))
3723 continue;
3724 if (time_after(jiffies,
3725 ata_deadline(last_jiffies, duration)))
3726 return 0;
3727 continue;
3730 /* unstable, start over */
3731 last = cur;
3732 last_jiffies = jiffies;
3734 /* Check deadline. If debouncing failed, return
3735 * -EPIPE to tell upper layer to lower link speed.
3737 if (time_after(jiffies, deadline))
3738 return -EPIPE;
3743 * sata_link_resume - resume SATA link
3744 * @link: ATA link to resume SATA
3745 * @params: timing parameters { interval, duration, timeout } in msec
3746 * @deadline: deadline jiffies for the operation
3748 * Resume SATA phy @link and debounce it.
3750 * LOCKING:
3751 * Kernel thread context (may sleep)
3753 * RETURNS:
3754 * 0 on success, -errno on failure.
3756 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3757 unsigned long deadline)
3759 int tries = ATA_LINK_RESUME_TRIES;
3760 u32 scontrol, serror;
3761 int rc;
3763 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3764 return rc;
3767 * Writes to SControl sometimes get ignored under certain
3768 * controllers (ata_piix SIDPR). Make sure DET actually is
3769 * cleared.
3771 do {
3772 scontrol = (scontrol & 0x0f0) | 0x300;
3773 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3774 return rc;
3776 * Some PHYs react badly if SStatus is pounded
3777 * immediately after resuming. Delay 200ms before
3778 * debouncing.
3780 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
3781 ata_msleep(link->ap, 200);
3783 /* is SControl restored correctly? */
3784 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3785 return rc;
3786 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3788 if ((scontrol & 0xf0f) != 0x300) {
3789 ata_link_warn(link, "failed to resume link (SControl %X)\n",
3790 scontrol);
3791 return 0;
3794 if (tries < ATA_LINK_RESUME_TRIES)
3795 ata_link_warn(link, "link resume succeeded after %d retries\n",
3796 ATA_LINK_RESUME_TRIES - tries);
3798 if ((rc = sata_link_debounce(link, params, deadline)))
3799 return rc;
3801 /* clear SError, some PHYs require this even for SRST to work */
3802 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3803 rc = sata_scr_write(link, SCR_ERROR, serror);
3805 return rc != -EINVAL ? rc : 0;
3809 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
3810 * @link: ATA link to manipulate SControl for
3811 * @policy: LPM policy to configure
3812 * @spm_wakeup: initiate LPM transition to active state
3814 * Manipulate the IPM field of the SControl register of @link
3815 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
3816 * @spm_wakeup is %true, the SPM field is manipulated to wake up
3817 * the link. This function also clears PHYRDY_CHG before
3818 * returning.
3820 * LOCKING:
3821 * EH context.
3823 * RETURNS:
3824 * 0 on success, -errno otherwise.
3826 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3827 bool spm_wakeup)
3829 struct ata_eh_context *ehc = &link->eh_context;
3830 bool woken_up = false;
3831 u32 scontrol;
3832 int rc;
3834 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
3835 if (rc)
3836 return rc;
3838 switch (policy) {
3839 case ATA_LPM_MAX_POWER:
3840 /* disable all LPM transitions */
3841 scontrol |= (0x7 << 8);
3842 /* initiate transition to active state */
3843 if (spm_wakeup) {
3844 scontrol |= (0x4 << 12);
3845 woken_up = true;
3847 break;
3848 case ATA_LPM_MED_POWER:
3849 /* allow LPM to PARTIAL */
3850 scontrol &= ~(0x1 << 8);
3851 scontrol |= (0x6 << 8);
3852 break;
3853 case ATA_LPM_MIN_POWER:
3854 if (ata_link_nr_enabled(link) > 0)
3855 /* no restrictions on LPM transitions */
3856 scontrol &= ~(0x7 << 8);
3857 else {
3858 /* empty port, power off */
3859 scontrol &= ~0xf;
3860 scontrol |= (0x1 << 2);
3862 break;
3863 default:
3864 WARN_ON(1);
3867 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
3868 if (rc)
3869 return rc;
3871 /* give the link time to transit out of LPM state */
3872 if (woken_up)
3873 msleep(10);
3875 /* clear PHYRDY_CHG from SError */
3876 ehc->i.serror &= ~SERR_PHYRDY_CHG;
3877 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
3881 * ata_std_prereset - prepare for reset
3882 * @link: ATA link to be reset
3883 * @deadline: deadline jiffies for the operation
3885 * @link is about to be reset. Initialize it. Failure from
3886 * prereset makes libata abort whole reset sequence and give up
3887 * that port, so prereset should be best-effort. It does its
3888 * best to prepare for reset sequence but if things go wrong, it
3889 * should just whine, not fail.
3891 * LOCKING:
3892 * Kernel thread context (may sleep)
3894 * RETURNS:
3895 * 0 on success, -errno otherwise.
3897 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3899 struct ata_port *ap = link->ap;
3900 struct ata_eh_context *ehc = &link->eh_context;
3901 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3902 int rc;
3904 /* if we're about to do hardreset, nothing more to do */
3905 if (ehc->i.action & ATA_EH_HARDRESET)
3906 return 0;
3908 /* if SATA, resume link */
3909 if (ap->flags & ATA_FLAG_SATA) {
3910 rc = sata_link_resume(link, timing, deadline);
3911 /* whine about phy resume failure but proceed */
3912 if (rc && rc != -EOPNOTSUPP)
3913 ata_link_warn(link,
3914 "failed to resume link for reset (errno=%d)\n",
3915 rc);
3918 /* no point in trying softreset on offline link */
3919 if (ata_phys_link_offline(link))
3920 ehc->i.action &= ~ATA_EH_SOFTRESET;
3922 return 0;
3926 * sata_link_hardreset - reset link via SATA phy reset
3927 * @link: link to reset
3928 * @timing: timing parameters { interval, duration, timeout } in msec
3929 * @deadline: deadline jiffies for the operation
3930 * @online: optional out parameter indicating link onlineness
3931 * @check_ready: optional callback to check link readiness
3933 * SATA phy-reset @link using DET bits of SControl register.
3934 * After hardreset, link readiness is waited upon using
3935 * ata_wait_ready() if @check_ready is specified. LLDs are
3936 * allowed to not specify @check_ready and wait itself after this
3937 * function returns. Device classification is LLD's
3938 * responsibility.
3940 * *@online is set to one iff reset succeeded and @link is online
3941 * after reset.
3943 * LOCKING:
3944 * Kernel thread context (may sleep)
3946 * RETURNS:
3947 * 0 on success, -errno otherwise.
3949 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3950 unsigned long deadline,
3951 bool *online, int (*check_ready)(struct ata_link *))
3953 u32 scontrol;
3954 int rc;
3956 DPRINTK("ENTER\n");
3958 if (online)
3959 *online = false;
3961 if (sata_set_spd_needed(link)) {
3962 /* SATA spec says nothing about how to reconfigure
3963 * spd. To be on the safe side, turn off phy during
3964 * reconfiguration. This works for at least ICH7 AHCI
3965 * and Sil3124.
3967 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3968 goto out;
3970 scontrol = (scontrol & 0x0f0) | 0x304;
3972 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3973 goto out;
3975 sata_set_spd(link);
3978 /* issue phy wake/reset */
3979 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3980 goto out;
3982 scontrol = (scontrol & 0x0f0) | 0x301;
3984 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3985 goto out;
3987 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3988 * 10.4.2 says at least 1 ms.
3990 ata_msleep(link->ap, 1);
3992 /* bring link back */
3993 rc = sata_link_resume(link, timing, deadline);
3994 if (rc)
3995 goto out;
3996 /* if link is offline nothing more to do */
3997 if (ata_phys_link_offline(link))
3998 goto out;
4000 /* Link is online. From this point, -ENODEV too is an error. */
4001 if (online)
4002 *online = true;
4004 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
4005 /* If PMP is supported, we have to do follow-up SRST.
4006 * Some PMPs don't send D2H Reg FIS after hardreset if
4007 * the first port is empty. Wait only for
4008 * ATA_TMOUT_PMP_SRST_WAIT.
4010 if (check_ready) {
4011 unsigned long pmp_deadline;
4013 pmp_deadline = ata_deadline(jiffies,
4014 ATA_TMOUT_PMP_SRST_WAIT);
4015 if (time_after(pmp_deadline, deadline))
4016 pmp_deadline = deadline;
4017 ata_wait_ready(link, pmp_deadline, check_ready);
4019 rc = -EAGAIN;
4020 goto out;
4023 rc = 0;
4024 if (check_ready)
4025 rc = ata_wait_ready(link, deadline, check_ready);
4026 out:
4027 if (rc && rc != -EAGAIN) {
4028 /* online is set iff link is online && reset succeeded */
4029 if (online)
4030 *online = false;
4031 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
4033 DPRINTK("EXIT, rc=%d\n", rc);
4034 return rc;
4038 * sata_std_hardreset - COMRESET w/o waiting or classification
4039 * @link: link to reset
4040 * @class: resulting class of attached device
4041 * @deadline: deadline jiffies for the operation
4043 * Standard SATA COMRESET w/o waiting or classification.
4045 * LOCKING:
4046 * Kernel thread context (may sleep)
4048 * RETURNS:
4049 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4051 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4052 unsigned long deadline)
4054 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4055 bool online;
4056 int rc;
4058 /* do hardreset */
4059 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
4060 return online ? -EAGAIN : rc;
4064 * ata_std_postreset - standard postreset callback
4065 * @link: the target ata_link
4066 * @classes: classes of attached devices
4068 * This function is invoked after a successful reset. Note that
4069 * the device might have been reset more than once using
4070 * different reset methods before postreset is invoked.
4072 * LOCKING:
4073 * Kernel thread context (may sleep)
4075 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4077 u32 serror;
4079 DPRINTK("ENTER\n");
4081 /* reset complete, clear SError */
4082 if (!sata_scr_read(link, SCR_ERROR, &serror))
4083 sata_scr_write(link, SCR_ERROR, serror);
4085 /* print link status */
4086 sata_print_link_status(link);
4088 DPRINTK("EXIT\n");
4092 * ata_dev_same_device - Determine whether new ID matches configured device
4093 * @dev: device to compare against
4094 * @new_class: class of the new device
4095 * @new_id: IDENTIFY page of the new device
4097 * Compare @new_class and @new_id against @dev and determine
4098 * whether @dev is the device indicated by @new_class and
4099 * @new_id.
4101 * LOCKING:
4102 * None.
4104 * RETURNS:
4105 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4107 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4108 const u16 *new_id)
4110 const u16 *old_id = dev->id;
4111 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4112 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4114 if (dev->class != new_class) {
4115 ata_dev_info(dev, "class mismatch %d != %d\n",
4116 dev->class, new_class);
4117 return 0;
4120 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4121 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4122 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4123 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4125 if (strcmp(model[0], model[1])) {
4126 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4127 model[0], model[1]);
4128 return 0;
4131 if (strcmp(serial[0], serial[1])) {
4132 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4133 serial[0], serial[1]);
4134 return 0;
4137 return 1;
4141 * ata_dev_reread_id - Re-read IDENTIFY data
4142 * @dev: target ATA device
4143 * @readid_flags: read ID flags
4145 * Re-read IDENTIFY page and make sure @dev is still attached to
4146 * the port.
4148 * LOCKING:
4149 * Kernel thread context (may sleep)
4151 * RETURNS:
4152 * 0 on success, negative errno otherwise
4154 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4156 unsigned int class = dev->class;
4157 u16 *id = (void *)dev->link->ap->sector_buf;
4158 int rc;
4160 /* read ID data */
4161 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4162 if (rc)
4163 return rc;
4165 /* is the device still there? */
4166 if (!ata_dev_same_device(dev, class, id))
4167 return -ENODEV;
4169 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4170 return 0;
4174 * ata_dev_revalidate - Revalidate ATA device
4175 * @dev: device to revalidate
4176 * @new_class: new class code
4177 * @readid_flags: read ID flags
4179 * Re-read IDENTIFY page, make sure @dev is still attached to the
4180 * port and reconfigure it according to the new IDENTIFY page.
4182 * LOCKING:
4183 * Kernel thread context (may sleep)
4185 * RETURNS:
4186 * 0 on success, negative errno otherwise
4188 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4189 unsigned int readid_flags)
4191 u64 n_sectors = dev->n_sectors;
4192 u64 n_native_sectors = dev->n_native_sectors;
4193 int rc;
4195 if (!ata_dev_enabled(dev))
4196 return -ENODEV;
4198 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4199 if (ata_class_enabled(new_class) &&
4200 new_class != ATA_DEV_ATA &&
4201 new_class != ATA_DEV_ATAPI &&
4202 new_class != ATA_DEV_ZAC &&
4203 new_class != ATA_DEV_SEMB) {
4204 ata_dev_info(dev, "class mismatch %u != %u\n",
4205 dev->class, new_class);
4206 rc = -ENODEV;
4207 goto fail;
4210 /* re-read ID */
4211 rc = ata_dev_reread_id(dev, readid_flags);
4212 if (rc)
4213 goto fail;
4215 /* configure device according to the new ID */
4216 rc = ata_dev_configure(dev);
4217 if (rc)
4218 goto fail;
4220 /* verify n_sectors hasn't changed */
4221 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4222 dev->n_sectors == n_sectors)
4223 return 0;
4225 /* n_sectors has changed */
4226 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
4227 (unsigned long long)n_sectors,
4228 (unsigned long long)dev->n_sectors);
4231 * Something could have caused HPA to be unlocked
4232 * involuntarily. If n_native_sectors hasn't changed and the
4233 * new size matches it, keep the device.
4235 if (dev->n_native_sectors == n_native_sectors &&
4236 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4237 ata_dev_warn(dev,
4238 "new n_sectors matches native, probably "
4239 "late HPA unlock, n_sectors updated\n");
4240 /* use the larger n_sectors */
4241 return 0;
4245 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4246 * unlocking HPA in those cases.
4248 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4250 if (dev->n_native_sectors == n_native_sectors &&
4251 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4252 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4253 ata_dev_warn(dev,
4254 "old n_sectors matches native, probably "
4255 "late HPA lock, will try to unlock HPA\n");
4256 /* try unlocking HPA */
4257 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4258 rc = -EIO;
4259 } else
4260 rc = -ENODEV;
4262 /* restore original n_[native_]sectors and fail */
4263 dev->n_native_sectors = n_native_sectors;
4264 dev->n_sectors = n_sectors;
4265 fail:
4266 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4267 return rc;
4270 struct ata_blacklist_entry {
4271 const char *model_num;
4272 const char *model_rev;
4273 unsigned long horkage;
4276 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4277 /* Devices with DMA related problems under Linux */
4278 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4279 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4280 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4281 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4282 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4283 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4284 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4285 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4286 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4287 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
4288 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4289 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4290 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4291 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4292 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4293 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
4294 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4295 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4296 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4297 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4298 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4299 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4300 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4301 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4302 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4303 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4304 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4305 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4306 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
4307 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
4308 /* Odd clown on sil3726/4726 PMPs */
4309 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
4311 /* Weird ATAPI devices */
4312 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4313 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
4314 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4315 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4318 * Causes silent data corruption with higher max sects.
4319 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4321 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
4324 * These devices time out with higher max sects.
4325 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
4327 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4328 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4330 /* Devices we expect to fail diagnostics */
4332 /* Devices where NCQ should be avoided */
4333 /* NCQ is slow */
4334 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4335 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4336 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4337 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4338 /* NCQ is broken */
4339 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4340 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4341 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4342 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4343 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
4345 /* Seagate NCQ + FLUSH CACHE firmware bug */
4346 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4347 ATA_HORKAGE_FIRMWARE_WARN },
4349 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4350 ATA_HORKAGE_FIRMWARE_WARN },
4352 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4353 ATA_HORKAGE_FIRMWARE_WARN },
4355 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4356 ATA_HORKAGE_FIRMWARE_WARN },
4358 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
4359 the ST disks also have LPM issues */
4360 { "ST1000LM024 HN-M101MBB", "2AR10001", ATA_HORKAGE_BROKEN_FPDMA_AA |
4361 ATA_HORKAGE_NOLPM, },
4362 { "ST1000LM024 HN-M101MBB", "2BA30001", ATA_HORKAGE_BROKEN_FPDMA_AA |
4363 ATA_HORKAGE_NOLPM, },
4364 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
4366 /* Blacklist entries taken from Silicon Image 3124/3132
4367 Windows driver .inf file - also several Linux problem reports */
4368 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4369 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4370 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4372 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4373 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4375 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
4376 SD7SN6S256G and SD8SN8U256G */
4377 { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
4379 /* devices which puke on READ_NATIVE_MAX */
4380 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4381 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4382 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4383 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4385 /* this one allows HPA unlocking but fails IOs on the area */
4386 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4388 /* Devices which report 1 sector over size HPA */
4389 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4390 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4391 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4393 /* Devices which get the IVB wrong */
4394 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4395 /* Maybe we should just blacklist TSSTcorp... */
4396 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
4398 /* Devices that do not need bridging limits applied */
4399 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4400 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
4402 /* Devices which aren't very happy with higher link speeds */
4403 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4404 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
4407 * Devices which choke on SETXFER. Applies only if both the
4408 * device and controller are SATA.
4410 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
4411 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4412 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
4413 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4414 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
4416 /* Crucial BX100 SSD 500GB has broken LPM support */
4417 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
4419 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4420 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4421 ATA_HORKAGE_ZERO_AFTER_TRIM |
4422 ATA_HORKAGE_NOLPM, },
4423 /* 512GB MX100 with newer firmware has only LPM issues */
4424 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4425 ATA_HORKAGE_NOLPM, },
4427 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4428 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4429 ATA_HORKAGE_ZERO_AFTER_TRIM |
4430 ATA_HORKAGE_NOLPM, },
4431 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4432 ATA_HORKAGE_ZERO_AFTER_TRIM |
4433 ATA_HORKAGE_NOLPM, },
4435 /* devices that don't properly handle queued TRIM commands */
4436 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4437 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4438 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4439 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4440 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4441 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4442 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4443 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4444 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4445 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4446 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4447 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4448 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4449 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4450 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4451 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4452 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4453 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4455 /* devices that don't properly handle TRIM commands */
4456 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4459 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4460 * (Return Zero After Trim) flags in the ATA Command Set are
4461 * unreliable in the sense that they only define what happens if
4462 * the device successfully executed the DSM TRIM command. TRIM
4463 * is only advisory, however, and the device is free to silently
4464 * ignore all or parts of the request.
4466 * Whitelist drives that are known to reliably return zeroes
4467 * after TRIM.
4471 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4472 * that model before whitelisting all other intel SSDs.
4474 { "INTEL*SSDSC2MH*", NULL, 0, },
4476 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4477 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4478 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4479 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4480 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4481 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4482 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4483 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4486 * Some WD SATA-I drives spin up and down erratically when the link
4487 * is put into the slumber mode. We don't have full list of the
4488 * affected devices. Disable LPM if the device matches one of the
4489 * known prefixes and is SATA-1. As a side effect LPM partial is
4490 * lost too.
4492 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4494 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4495 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4496 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4497 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4498 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4499 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4500 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4502 /* End Marker */
4506 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4508 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4509 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4510 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4512 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4513 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4515 while (ad->model_num) {
4516 if (glob_match(ad->model_num, model_num)) {
4517 if (ad->model_rev == NULL)
4518 return ad->horkage;
4519 if (glob_match(ad->model_rev, model_rev))
4520 return ad->horkage;
4522 ad++;
4524 return 0;
4527 static int ata_dma_blacklisted(const struct ata_device *dev)
4529 /* We don't support polling DMA.
4530 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4531 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4533 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4534 (dev->flags & ATA_DFLAG_CDB_INTR))
4535 return 1;
4536 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4540 * ata_is_40wire - check drive side detection
4541 * @dev: device
4543 * Perform drive side detection decoding, allowing for device vendors
4544 * who can't follow the documentation.
4547 static int ata_is_40wire(struct ata_device *dev)
4549 if (dev->horkage & ATA_HORKAGE_IVB)
4550 return ata_drive_40wire_relaxed(dev->id);
4551 return ata_drive_40wire(dev->id);
4555 * cable_is_40wire - 40/80/SATA decider
4556 * @ap: port to consider
4558 * This function encapsulates the policy for speed management
4559 * in one place. At the moment we don't cache the result but
4560 * there is a good case for setting ap->cbl to the result when
4561 * we are called with unknown cables (and figuring out if it
4562 * impacts hotplug at all).
4564 * Return 1 if the cable appears to be 40 wire.
4567 static int cable_is_40wire(struct ata_port *ap)
4569 struct ata_link *link;
4570 struct ata_device *dev;
4572 /* If the controller thinks we are 40 wire, we are. */
4573 if (ap->cbl == ATA_CBL_PATA40)
4574 return 1;
4576 /* If the controller thinks we are 80 wire, we are. */
4577 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4578 return 0;
4580 /* If the system is known to be 40 wire short cable (eg
4581 * laptop), then we allow 80 wire modes even if the drive
4582 * isn't sure.
4584 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4585 return 0;
4587 /* If the controller doesn't know, we scan.
4589 * Note: We look for all 40 wire detects at this point. Any
4590 * 80 wire detect is taken to be 80 wire cable because
4591 * - in many setups only the one drive (slave if present) will
4592 * give a valid detect
4593 * - if you have a non detect capable drive you don't want it
4594 * to colour the choice
4596 ata_for_each_link(link, ap, EDGE) {
4597 ata_for_each_dev(dev, link, ENABLED) {
4598 if (!ata_is_40wire(dev))
4599 return 0;
4602 return 1;
4606 * ata_dev_xfermask - Compute supported xfermask of the given device
4607 * @dev: Device to compute xfermask for
4609 * Compute supported xfermask of @dev and store it in
4610 * dev->*_mask. This function is responsible for applying all
4611 * known limits including host controller limits, device
4612 * blacklist, etc...
4614 * LOCKING:
4615 * None.
4617 static void ata_dev_xfermask(struct ata_device *dev)
4619 struct ata_link *link = dev->link;
4620 struct ata_port *ap = link->ap;
4621 struct ata_host *host = ap->host;
4622 unsigned long xfer_mask;
4624 /* controller modes available */
4625 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4626 ap->mwdma_mask, ap->udma_mask);
4628 /* drive modes available */
4629 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4630 dev->mwdma_mask, dev->udma_mask);
4631 xfer_mask &= ata_id_xfermask(dev->id);
4634 * CFA Advanced TrueIDE timings are not allowed on a shared
4635 * cable
4637 if (ata_dev_pair(dev)) {
4638 /* No PIO5 or PIO6 */
4639 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4640 /* No MWDMA3 or MWDMA 4 */
4641 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4644 if (ata_dma_blacklisted(dev)) {
4645 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4646 ata_dev_warn(dev,
4647 "device is on DMA blacklist, disabling DMA\n");
4650 if ((host->flags & ATA_HOST_SIMPLEX) &&
4651 host->simplex_claimed && host->simplex_claimed != ap) {
4652 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4653 ata_dev_warn(dev,
4654 "simplex DMA is claimed by other device, disabling DMA\n");
4657 if (ap->flags & ATA_FLAG_NO_IORDY)
4658 xfer_mask &= ata_pio_mask_no_iordy(dev);
4660 if (ap->ops->mode_filter)
4661 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4663 /* Apply cable rule here. Don't apply it early because when
4664 * we handle hot plug the cable type can itself change.
4665 * Check this last so that we know if the transfer rate was
4666 * solely limited by the cable.
4667 * Unknown or 80 wire cables reported host side are checked
4668 * drive side as well. Cases where we know a 40wire cable
4669 * is used safely for 80 are not checked here.
4671 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4672 /* UDMA/44 or higher would be available */
4673 if (cable_is_40wire(ap)) {
4674 ata_dev_warn(dev,
4675 "limited to UDMA/33 due to 40-wire cable\n");
4676 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4679 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4680 &dev->mwdma_mask, &dev->udma_mask);
4684 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4685 * @dev: Device to which command will be sent
4687 * Issue SET FEATURES - XFER MODE command to device @dev
4688 * on port @ap.
4690 * LOCKING:
4691 * PCI/etc. bus probe sem.
4693 * RETURNS:
4694 * 0 on success, AC_ERR_* mask otherwise.
4697 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4699 struct ata_taskfile tf;
4700 unsigned int err_mask;
4702 /* set up set-features taskfile */
4703 DPRINTK("set features - xfer mode\n");
4705 /* Some controllers and ATAPI devices show flaky interrupt
4706 * behavior after setting xfer mode. Use polling instead.
4708 ata_tf_init(dev, &tf);
4709 tf.command = ATA_CMD_SET_FEATURES;
4710 tf.feature = SETFEATURES_XFER;
4711 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4712 tf.protocol = ATA_PROT_NODATA;
4713 /* If we are using IORDY we must send the mode setting command */
4714 if (ata_pio_need_iordy(dev))
4715 tf.nsect = dev->xfer_mode;
4716 /* If the device has IORDY and the controller does not - turn it off */
4717 else if (ata_id_has_iordy(dev->id))
4718 tf.nsect = 0x01;
4719 else /* In the ancient relic department - skip all of this */
4720 return 0;
4722 /* On some disks, this command causes spin-up, so we need longer timeout */
4723 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4725 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4726 return err_mask;
4730 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4731 * @dev: Device to which command will be sent
4732 * @enable: Whether to enable or disable the feature
4733 * @feature: The sector count represents the feature to set
4735 * Issue SET FEATURES - SATA FEATURES command to device @dev
4736 * on port @ap with sector count
4738 * LOCKING:
4739 * PCI/etc. bus probe sem.
4741 * RETURNS:
4742 * 0 on success, AC_ERR_* mask otherwise.
4744 unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
4746 struct ata_taskfile tf;
4747 unsigned int err_mask;
4748 unsigned long timeout = 0;
4750 /* set up set-features taskfile */
4751 DPRINTK("set features - SATA features\n");
4753 ata_tf_init(dev, &tf);
4754 tf.command = ATA_CMD_SET_FEATURES;
4755 tf.feature = enable;
4756 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4757 tf.protocol = ATA_PROT_NODATA;
4758 tf.nsect = feature;
4760 if (enable == SETFEATURES_SPINUP)
4761 timeout = ata_probe_timeout ?
4762 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4763 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
4765 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4766 return err_mask;
4768 EXPORT_SYMBOL_GPL(ata_dev_set_feature);
4771 * ata_dev_init_params - Issue INIT DEV PARAMS command
4772 * @dev: Device to which command will be sent
4773 * @heads: Number of heads (taskfile parameter)
4774 * @sectors: Number of sectors (taskfile parameter)
4776 * LOCKING:
4777 * Kernel thread context (may sleep)
4779 * RETURNS:
4780 * 0 on success, AC_ERR_* mask otherwise.
4782 static unsigned int ata_dev_init_params(struct ata_device *dev,
4783 u16 heads, u16 sectors)
4785 struct ata_taskfile tf;
4786 unsigned int err_mask;
4788 /* Number of sectors per track 1-255. Number of heads 1-16 */
4789 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4790 return AC_ERR_INVALID;
4792 /* set up init dev params taskfile */
4793 DPRINTK("init dev params \n");
4795 ata_tf_init(dev, &tf);
4796 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4797 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4798 tf.protocol = ATA_PROT_NODATA;
4799 tf.nsect = sectors;
4800 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4802 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4803 /* A clean abort indicates an original or just out of spec drive
4804 and we should continue as we issue the setup based on the
4805 drive reported working geometry */
4806 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4807 err_mask = 0;
4809 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4810 return err_mask;
4814 * ata_sg_clean - Unmap DMA memory associated with command
4815 * @qc: Command containing DMA memory to be released
4817 * Unmap all mapped DMA memory associated with this command.
4819 * LOCKING:
4820 * spin_lock_irqsave(host lock)
4822 void ata_sg_clean(struct ata_queued_cmd *qc)
4824 struct ata_port *ap = qc->ap;
4825 struct scatterlist *sg = qc->sg;
4826 int dir = qc->dma_dir;
4828 WARN_ON_ONCE(sg == NULL);
4830 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4832 if (qc->n_elem)
4833 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4835 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4836 qc->sg = NULL;
4840 * atapi_check_dma - Check whether ATAPI DMA can be supported
4841 * @qc: Metadata associated with taskfile to check
4843 * Allow low-level driver to filter ATA PACKET commands, returning
4844 * a status indicating whether or not it is OK to use DMA for the
4845 * supplied PACKET command.
4847 * LOCKING:
4848 * spin_lock_irqsave(host lock)
4850 * RETURNS: 0 when ATAPI DMA can be used
4851 * nonzero otherwise
4853 int atapi_check_dma(struct ata_queued_cmd *qc)
4855 struct ata_port *ap = qc->ap;
4857 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4858 * few ATAPI devices choke on such DMA requests.
4860 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4861 unlikely(qc->nbytes & 15))
4862 return 1;
4864 if (ap->ops->check_atapi_dma)
4865 return ap->ops->check_atapi_dma(qc);
4867 return 0;
4871 * ata_std_qc_defer - Check whether a qc needs to be deferred
4872 * @qc: ATA command in question
4874 * Non-NCQ commands cannot run with any other command, NCQ or
4875 * not. As upper layer only knows the queue depth, we are
4876 * responsible for maintaining exclusion. This function checks
4877 * whether a new command @qc can be issued.
4879 * LOCKING:
4880 * spin_lock_irqsave(host lock)
4882 * RETURNS:
4883 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4885 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4887 struct ata_link *link = qc->dev->link;
4889 if (ata_is_ncq(qc->tf.protocol)) {
4890 if (!ata_tag_valid(link->active_tag))
4891 return 0;
4892 } else {
4893 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4894 return 0;
4897 return ATA_DEFER_LINK;
4900 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4903 * ata_sg_init - Associate command with scatter-gather table.
4904 * @qc: Command to be associated
4905 * @sg: Scatter-gather table.
4906 * @n_elem: Number of elements in s/g table.
4908 * Initialize the data-related elements of queued_cmd @qc
4909 * to point to a scatter-gather table @sg, containing @n_elem
4910 * elements.
4912 * LOCKING:
4913 * spin_lock_irqsave(host lock)
4915 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4916 unsigned int n_elem)
4918 qc->sg = sg;
4919 qc->n_elem = n_elem;
4920 qc->cursg = qc->sg;
4924 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4925 * @qc: Command with scatter-gather table to be mapped.
4927 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4929 * LOCKING:
4930 * spin_lock_irqsave(host lock)
4932 * RETURNS:
4933 * Zero on success, negative on error.
4936 static int ata_sg_setup(struct ata_queued_cmd *qc)
4938 struct ata_port *ap = qc->ap;
4939 unsigned int n_elem;
4941 VPRINTK("ENTER, ata%u\n", ap->print_id);
4943 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4944 if (n_elem < 1)
4945 return -1;
4947 DPRINTK("%d sg elements mapped\n", n_elem);
4948 qc->orig_n_elem = qc->n_elem;
4949 qc->n_elem = n_elem;
4950 qc->flags |= ATA_QCFLAG_DMAMAP;
4952 return 0;
4956 * swap_buf_le16 - swap halves of 16-bit words in place
4957 * @buf: Buffer to swap
4958 * @buf_words: Number of 16-bit words in buffer.
4960 * Swap halves of 16-bit words if needed to convert from
4961 * little-endian byte order to native cpu byte order, or
4962 * vice-versa.
4964 * LOCKING:
4965 * Inherited from caller.
4967 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4969 #ifdef __BIG_ENDIAN
4970 unsigned int i;
4972 for (i = 0; i < buf_words; i++)
4973 buf[i] = le16_to_cpu(buf[i]);
4974 #endif /* __BIG_ENDIAN */
4978 * ata_qc_new_init - Request an available ATA command, and initialize it
4979 * @dev: Device from whom we request an available command structure
4980 * @tag: tag
4982 * LOCKING:
4983 * None.
4986 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4988 struct ata_port *ap = dev->link->ap;
4989 struct ata_queued_cmd *qc;
4991 /* no command while frozen */
4992 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4993 return NULL;
4995 /* libsas case */
4996 if (ap->flags & ATA_FLAG_SAS_HOST) {
4997 tag = ata_sas_allocate_tag(ap);
4998 if (tag < 0)
4999 return NULL;
5002 qc = __ata_qc_from_tag(ap, tag);
5003 qc->tag = tag;
5004 qc->scsicmd = NULL;
5005 qc->ap = ap;
5006 qc->dev = dev;
5008 ata_qc_reinit(qc);
5010 return qc;
5014 * ata_qc_free - free unused ata_queued_cmd
5015 * @qc: Command to complete
5017 * Designed to free unused ata_queued_cmd object
5018 * in case something prevents using it.
5020 * LOCKING:
5021 * spin_lock_irqsave(host lock)
5023 void ata_qc_free(struct ata_queued_cmd *qc)
5025 struct ata_port *ap;
5026 unsigned int tag;
5028 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5029 ap = qc->ap;
5031 qc->flags = 0;
5032 tag = qc->tag;
5033 if (likely(ata_tag_valid(tag))) {
5034 qc->tag = ATA_TAG_POISON;
5035 if (ap->flags & ATA_FLAG_SAS_HOST)
5036 ata_sas_free_tag(tag, ap);
5040 void __ata_qc_complete(struct ata_queued_cmd *qc)
5042 struct ata_port *ap;
5043 struct ata_link *link;
5045 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5046 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5047 ap = qc->ap;
5048 link = qc->dev->link;
5050 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5051 ata_sg_clean(qc);
5053 /* command should be marked inactive atomically with qc completion */
5054 if (ata_is_ncq(qc->tf.protocol)) {
5055 link->sactive &= ~(1 << qc->tag);
5056 if (!link->sactive)
5057 ap->nr_active_links--;
5058 } else {
5059 link->active_tag = ATA_TAG_POISON;
5060 ap->nr_active_links--;
5063 /* clear exclusive status */
5064 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5065 ap->excl_link == link))
5066 ap->excl_link = NULL;
5068 /* atapi: mark qc as inactive to prevent the interrupt handler
5069 * from completing the command twice later, before the error handler
5070 * is called. (when rc != 0 and atapi request sense is needed)
5072 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5073 ap->qc_active &= ~(1 << qc->tag);
5075 /* call completion callback */
5076 qc->complete_fn(qc);
5079 static void fill_result_tf(struct ata_queued_cmd *qc)
5081 struct ata_port *ap = qc->ap;
5083 qc->result_tf.flags = qc->tf.flags;
5084 ap->ops->qc_fill_rtf(qc);
5087 static void ata_verify_xfer(struct ata_queued_cmd *qc)
5089 struct ata_device *dev = qc->dev;
5091 if (!ata_is_data(qc->tf.protocol))
5092 return;
5094 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5095 return;
5097 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5101 * ata_qc_complete - Complete an active ATA command
5102 * @qc: Command to complete
5104 * Indicate to the mid and upper layers that an ATA command has
5105 * completed, with either an ok or not-ok status.
5107 * Refrain from calling this function multiple times when
5108 * successfully completing multiple NCQ commands.
5109 * ata_qc_complete_multiple() should be used instead, which will
5110 * properly update IRQ expect state.
5112 * LOCKING:
5113 * spin_lock_irqsave(host lock)
5115 void ata_qc_complete(struct ata_queued_cmd *qc)
5117 struct ata_port *ap = qc->ap;
5119 /* Trigger the LED (if available) */
5120 ledtrig_disk_activity();
5122 /* XXX: New EH and old EH use different mechanisms to
5123 * synchronize EH with regular execution path.
5125 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5126 * Normal execution path is responsible for not accessing a
5127 * failed qc. libata core enforces the rule by returning NULL
5128 * from ata_qc_from_tag() for failed qcs.
5130 * Old EH depends on ata_qc_complete() nullifying completion
5131 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5132 * not synchronize with interrupt handler. Only PIO task is
5133 * taken care of.
5135 if (ap->ops->error_handler) {
5136 struct ata_device *dev = qc->dev;
5137 struct ata_eh_info *ehi = &dev->link->eh_info;
5139 if (unlikely(qc->err_mask))
5140 qc->flags |= ATA_QCFLAG_FAILED;
5143 * Finish internal commands without any further processing
5144 * and always with the result TF filled.
5146 if (unlikely(ata_tag_internal(qc->tag))) {
5147 fill_result_tf(qc);
5148 trace_ata_qc_complete_internal(qc);
5149 __ata_qc_complete(qc);
5150 return;
5154 * Non-internal qc has failed. Fill the result TF and
5155 * summon EH.
5157 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5158 fill_result_tf(qc);
5159 trace_ata_qc_complete_failed(qc);
5160 ata_qc_schedule_eh(qc);
5161 return;
5164 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5166 /* read result TF if requested */
5167 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5168 fill_result_tf(qc);
5170 trace_ata_qc_complete_done(qc);
5171 /* Some commands need post-processing after successful
5172 * completion.
5174 switch (qc->tf.command) {
5175 case ATA_CMD_SET_FEATURES:
5176 if (qc->tf.feature != SETFEATURES_WC_ON &&
5177 qc->tf.feature != SETFEATURES_WC_OFF &&
5178 qc->tf.feature != SETFEATURES_RA_ON &&
5179 qc->tf.feature != SETFEATURES_RA_OFF)
5180 break;
5181 /* fall through */
5182 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5183 case ATA_CMD_SET_MULTI: /* multi_count changed */
5184 /* revalidate device */
5185 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5186 ata_port_schedule_eh(ap);
5187 break;
5189 case ATA_CMD_SLEEP:
5190 dev->flags |= ATA_DFLAG_SLEEPING;
5191 break;
5194 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5195 ata_verify_xfer(qc);
5197 __ata_qc_complete(qc);
5198 } else {
5199 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5200 return;
5202 /* read result TF if failed or requested */
5203 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5204 fill_result_tf(qc);
5206 __ata_qc_complete(qc);
5211 * ata_qc_complete_multiple - Complete multiple qcs successfully
5212 * @ap: port in question
5213 * @qc_active: new qc_active mask
5215 * Complete in-flight commands. This functions is meant to be
5216 * called from low-level driver's interrupt routine to complete
5217 * requests normally. ap->qc_active and @qc_active is compared
5218 * and commands are completed accordingly.
5220 * Always use this function when completing multiple NCQ commands
5221 * from IRQ handlers instead of calling ata_qc_complete()
5222 * multiple times to keep IRQ expect status properly in sync.
5224 * LOCKING:
5225 * spin_lock_irqsave(host lock)
5227 * RETURNS:
5228 * Number of completed commands on success, -errno otherwise.
5230 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5232 int nr_done = 0;
5233 u32 done_mask;
5235 done_mask = ap->qc_active ^ qc_active;
5237 if (unlikely(done_mask & qc_active)) {
5238 ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
5239 ap->qc_active, qc_active);
5240 return -EINVAL;
5243 while (done_mask) {
5244 struct ata_queued_cmd *qc;
5245 unsigned int tag = __ffs(done_mask);
5247 qc = ata_qc_from_tag(ap, tag);
5248 if (qc) {
5249 ata_qc_complete(qc);
5250 nr_done++;
5252 done_mask &= ~(1 << tag);
5255 return nr_done;
5259 * ata_qc_issue - issue taskfile to device
5260 * @qc: command to issue to device
5262 * Prepare an ATA command to submission to device.
5263 * This includes mapping the data into a DMA-able
5264 * area, filling in the S/G table, and finally
5265 * writing the taskfile to hardware, starting the command.
5267 * LOCKING:
5268 * spin_lock_irqsave(host lock)
5270 void ata_qc_issue(struct ata_queued_cmd *qc)
5272 struct ata_port *ap = qc->ap;
5273 struct ata_link *link = qc->dev->link;
5274 u8 prot = qc->tf.protocol;
5276 /* Make sure only one non-NCQ command is outstanding. The
5277 * check is skipped for old EH because it reuses active qc to
5278 * request ATAPI sense.
5280 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5282 if (ata_is_ncq(prot)) {
5283 WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5285 if (!link->sactive)
5286 ap->nr_active_links++;
5287 link->sactive |= 1 << qc->tag;
5288 } else {
5289 WARN_ON_ONCE(link->sactive);
5291 ap->nr_active_links++;
5292 link->active_tag = qc->tag;
5295 qc->flags |= ATA_QCFLAG_ACTIVE;
5296 ap->qc_active |= 1 << qc->tag;
5299 * We guarantee to LLDs that they will have at least one
5300 * non-zero sg if the command is a data command.
5302 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
5303 goto sys_err;
5305 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5306 (ap->flags & ATA_FLAG_PIO_DMA)))
5307 if (ata_sg_setup(qc))
5308 goto sys_err;
5310 /* if device is sleeping, schedule reset and abort the link */
5311 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5312 link->eh_info.action |= ATA_EH_RESET;
5313 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5314 ata_link_abort(link);
5315 return;
5318 ap->ops->qc_prep(qc);
5319 trace_ata_qc_issue(qc);
5320 qc->err_mask |= ap->ops->qc_issue(qc);
5321 if (unlikely(qc->err_mask))
5322 goto err;
5323 return;
5325 sys_err:
5326 qc->err_mask |= AC_ERR_SYSTEM;
5327 err:
5328 ata_qc_complete(qc);
5332 * sata_scr_valid - test whether SCRs are accessible
5333 * @link: ATA link to test SCR accessibility for
5335 * Test whether SCRs are accessible for @link.
5337 * LOCKING:
5338 * None.
5340 * RETURNS:
5341 * 1 if SCRs are accessible, 0 otherwise.
5343 int sata_scr_valid(struct ata_link *link)
5345 struct ata_port *ap = link->ap;
5347 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5351 * sata_scr_read - read SCR register of the specified port
5352 * @link: ATA link to read SCR for
5353 * @reg: SCR to read
5354 * @val: Place to store read value
5356 * Read SCR register @reg of @link into *@val. This function is
5357 * guaranteed to succeed if @link is ap->link, the cable type of
5358 * the port is SATA and the port implements ->scr_read.
5360 * LOCKING:
5361 * None if @link is ap->link. Kernel thread context otherwise.
5363 * RETURNS:
5364 * 0 on success, negative errno on failure.
5366 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5368 if (ata_is_host_link(link)) {
5369 if (sata_scr_valid(link))
5370 return link->ap->ops->scr_read(link, reg, val);
5371 return -EOPNOTSUPP;
5374 return sata_pmp_scr_read(link, reg, val);
5378 * sata_scr_write - write SCR register of the specified port
5379 * @link: ATA link to write SCR for
5380 * @reg: SCR to write
5381 * @val: value to write
5383 * Write @val to SCR register @reg of @link. This function is
5384 * guaranteed to succeed if @link is ap->link, the cable type of
5385 * the port is SATA and the port implements ->scr_read.
5387 * LOCKING:
5388 * None if @link is ap->link. Kernel thread context otherwise.
5390 * RETURNS:
5391 * 0 on success, negative errno on failure.
5393 int sata_scr_write(struct ata_link *link, int reg, u32 val)
5395 if (ata_is_host_link(link)) {
5396 if (sata_scr_valid(link))
5397 return link->ap->ops->scr_write(link, reg, val);
5398 return -EOPNOTSUPP;
5401 return sata_pmp_scr_write(link, reg, val);
5405 * sata_scr_write_flush - write SCR register of the specified port and flush
5406 * @link: ATA link to write SCR for
5407 * @reg: SCR to write
5408 * @val: value to write
5410 * This function is identical to sata_scr_write() except that this
5411 * function performs flush after writing to the register.
5413 * LOCKING:
5414 * None if @link is ap->link. Kernel thread context otherwise.
5416 * RETURNS:
5417 * 0 on success, negative errno on failure.
5419 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5421 if (ata_is_host_link(link)) {
5422 int rc;
5424 if (sata_scr_valid(link)) {
5425 rc = link->ap->ops->scr_write(link, reg, val);
5426 if (rc == 0)
5427 rc = link->ap->ops->scr_read(link, reg, &val);
5428 return rc;
5430 return -EOPNOTSUPP;
5433 return sata_pmp_scr_write(link, reg, val);
5437 * ata_phys_link_online - test whether the given link is online
5438 * @link: ATA link to test
5440 * Test whether @link is online. Note that this function returns
5441 * 0 if online status of @link cannot be obtained, so
5442 * ata_link_online(link) != !ata_link_offline(link).
5444 * LOCKING:
5445 * None.
5447 * RETURNS:
5448 * True if the port online status is available and online.
5450 bool ata_phys_link_online(struct ata_link *link)
5452 u32 sstatus;
5454 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5455 ata_sstatus_online(sstatus))
5456 return true;
5457 return false;
5461 * ata_phys_link_offline - test whether the given link is offline
5462 * @link: ATA link to test
5464 * Test whether @link is offline. Note that this function
5465 * returns 0 if offline status of @link cannot be obtained, so
5466 * ata_link_online(link) != !ata_link_offline(link).
5468 * LOCKING:
5469 * None.
5471 * RETURNS:
5472 * True if the port offline status is available and offline.
5474 bool ata_phys_link_offline(struct ata_link *link)
5476 u32 sstatus;
5478 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5479 !ata_sstatus_online(sstatus))
5480 return true;
5481 return false;
5485 * ata_link_online - test whether the given link is online
5486 * @link: ATA link to test
5488 * Test whether @link is online. This is identical to
5489 * ata_phys_link_online() when there's no slave link. When
5490 * there's a slave link, this function should only be called on
5491 * the master link and will return true if any of M/S links is
5492 * online.
5494 * LOCKING:
5495 * None.
5497 * RETURNS:
5498 * True if the port online status is available and online.
5500 bool ata_link_online(struct ata_link *link)
5502 struct ata_link *slave = link->ap->slave_link;
5504 WARN_ON(link == slave); /* shouldn't be called on slave link */
5506 return ata_phys_link_online(link) ||
5507 (slave && ata_phys_link_online(slave));
5511 * ata_link_offline - test whether the given link is offline
5512 * @link: ATA link to test
5514 * Test whether @link is offline. This is identical to
5515 * ata_phys_link_offline() when there's no slave link. When
5516 * there's a slave link, this function should only be called on
5517 * the master link and will return true if both M/S links are
5518 * offline.
5520 * LOCKING:
5521 * None.
5523 * RETURNS:
5524 * True if the port offline status is available and offline.
5526 bool ata_link_offline(struct ata_link *link)
5528 struct ata_link *slave = link->ap->slave_link;
5530 WARN_ON(link == slave); /* shouldn't be called on slave link */
5532 return ata_phys_link_offline(link) &&
5533 (!slave || ata_phys_link_offline(slave));
5536 #ifdef CONFIG_PM
5537 static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5538 unsigned int action, unsigned int ehi_flags,
5539 bool async)
5541 struct ata_link *link;
5542 unsigned long flags;
5544 /* Previous resume operation might still be in
5545 * progress. Wait for PM_PENDING to clear.
5547 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5548 ata_port_wait_eh(ap);
5549 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5552 /* request PM ops to EH */
5553 spin_lock_irqsave(ap->lock, flags);
5555 ap->pm_mesg = mesg;
5556 ap->pflags |= ATA_PFLAG_PM_PENDING;
5557 ata_for_each_link(link, ap, HOST_FIRST) {
5558 link->eh_info.action |= action;
5559 link->eh_info.flags |= ehi_flags;
5562 ata_port_schedule_eh(ap);
5564 spin_unlock_irqrestore(ap->lock, flags);
5566 if (!async) {
5567 ata_port_wait_eh(ap);
5568 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5573 * On some hardware, device fails to respond after spun down for suspend. As
5574 * the device won't be used before being resumed, we don't need to touch the
5575 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5577 * http://thread.gmane.org/gmane.linux.ide/46764
5579 static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5580 | ATA_EHI_NO_AUTOPSY
5581 | ATA_EHI_NO_RECOVERY;
5583 static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5585 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
5588 static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5590 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5593 static int ata_port_pm_suspend(struct device *dev)
5595 struct ata_port *ap = to_ata_port(dev);
5597 if (pm_runtime_suspended(dev))
5598 return 0;
5600 ata_port_suspend(ap, PMSG_SUSPEND);
5601 return 0;
5604 static int ata_port_pm_freeze(struct device *dev)
5606 struct ata_port *ap = to_ata_port(dev);
5608 if (pm_runtime_suspended(dev))
5609 return 0;
5611 ata_port_suspend(ap, PMSG_FREEZE);
5612 return 0;
5615 static int ata_port_pm_poweroff(struct device *dev)
5617 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5618 return 0;
5621 static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5622 | ATA_EHI_QUIET;
5624 static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5626 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5629 static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5631 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5634 static int ata_port_pm_resume(struct device *dev)
5636 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5637 pm_runtime_disable(dev);
5638 pm_runtime_set_active(dev);
5639 pm_runtime_enable(dev);
5640 return 0;
5644 * For ODDs, the upper layer will poll for media change every few seconds,
5645 * which will make it enter and leave suspend state every few seconds. And
5646 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5647 * is very little and the ODD may malfunction after constantly being reset.
5648 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5649 * ODD is attached to the port.
5651 static int ata_port_runtime_idle(struct device *dev)
5653 struct ata_port *ap = to_ata_port(dev);
5654 struct ata_link *link;
5655 struct ata_device *adev;
5657 ata_for_each_link(link, ap, HOST_FIRST) {
5658 ata_for_each_dev(adev, link, ENABLED)
5659 if (adev->class == ATA_DEV_ATAPI &&
5660 !zpodd_dev_enabled(adev))
5661 return -EBUSY;
5664 return 0;
5667 static int ata_port_runtime_suspend(struct device *dev)
5669 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5670 return 0;
5673 static int ata_port_runtime_resume(struct device *dev)
5675 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5676 return 0;
5679 static const struct dev_pm_ops ata_port_pm_ops = {
5680 .suspend = ata_port_pm_suspend,
5681 .resume = ata_port_pm_resume,
5682 .freeze = ata_port_pm_freeze,
5683 .thaw = ata_port_pm_resume,
5684 .poweroff = ata_port_pm_poweroff,
5685 .restore = ata_port_pm_resume,
5687 .runtime_suspend = ata_port_runtime_suspend,
5688 .runtime_resume = ata_port_runtime_resume,
5689 .runtime_idle = ata_port_runtime_idle,
5692 /* sas ports don't participate in pm runtime management of ata_ports,
5693 * and need to resume ata devices at the domain level, not the per-port
5694 * level. sas suspend/resume is async to allow parallel port recovery
5695 * since sas has multiple ata_port instances per Scsi_Host.
5697 void ata_sas_port_suspend(struct ata_port *ap)
5699 ata_port_suspend_async(ap, PMSG_SUSPEND);
5701 EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
5703 void ata_sas_port_resume(struct ata_port *ap)
5705 ata_port_resume_async(ap, PMSG_RESUME);
5707 EXPORT_SYMBOL_GPL(ata_sas_port_resume);
5710 * ata_host_suspend - suspend host
5711 * @host: host to suspend
5712 * @mesg: PM message
5714 * Suspend @host. Actual operation is performed by port suspend.
5716 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5718 host->dev->power.power_state = mesg;
5719 return 0;
5723 * ata_host_resume - resume host
5724 * @host: host to resume
5726 * Resume @host. Actual operation is performed by port resume.
5728 void ata_host_resume(struct ata_host *host)
5730 host->dev->power.power_state = PMSG_ON;
5732 #endif
5734 struct device_type ata_port_type = {
5735 .name = "ata_port",
5736 #ifdef CONFIG_PM
5737 .pm = &ata_port_pm_ops,
5738 #endif
5742 * ata_dev_init - Initialize an ata_device structure
5743 * @dev: Device structure to initialize
5745 * Initialize @dev in preparation for probing.
5747 * LOCKING:
5748 * Inherited from caller.
5750 void ata_dev_init(struct ata_device *dev)
5752 struct ata_link *link = ata_dev_phys_link(dev);
5753 struct ata_port *ap = link->ap;
5754 unsigned long flags;
5756 /* SATA spd limit is bound to the attached device, reset together */
5757 link->sata_spd_limit = link->hw_sata_spd_limit;
5758 link->sata_spd = 0;
5760 /* High bits of dev->flags are used to record warm plug
5761 * requests which occur asynchronously. Synchronize using
5762 * host lock.
5764 spin_lock_irqsave(ap->lock, flags);
5765 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5766 dev->horkage = 0;
5767 spin_unlock_irqrestore(ap->lock, flags);
5769 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5770 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5771 dev->pio_mask = UINT_MAX;
5772 dev->mwdma_mask = UINT_MAX;
5773 dev->udma_mask = UINT_MAX;
5777 * ata_link_init - Initialize an ata_link structure
5778 * @ap: ATA port link is attached to
5779 * @link: Link structure to initialize
5780 * @pmp: Port multiplier port number
5782 * Initialize @link.
5784 * LOCKING:
5785 * Kernel thread context (may sleep)
5787 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
5789 int i;
5791 /* clear everything except for devices */
5792 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5793 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
5795 link->ap = ap;
5796 link->pmp = pmp;
5797 link->active_tag = ATA_TAG_POISON;
5798 link->hw_sata_spd_limit = UINT_MAX;
5800 /* can't use iterator, ap isn't initialized yet */
5801 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5802 struct ata_device *dev = &link->device[i];
5804 dev->link = link;
5805 dev->devno = dev - link->device;
5806 #ifdef CONFIG_ATA_ACPI
5807 dev->gtf_filter = ata_acpi_gtf_filter;
5808 #endif
5809 ata_dev_init(dev);
5814 * sata_link_init_spd - Initialize link->sata_spd_limit
5815 * @link: Link to configure sata_spd_limit for
5817 * Initialize @link->[hw_]sata_spd_limit to the currently
5818 * configured value.
5820 * LOCKING:
5821 * Kernel thread context (may sleep).
5823 * RETURNS:
5824 * 0 on success, -errno on failure.
5826 int sata_link_init_spd(struct ata_link *link)
5828 u8 spd;
5829 int rc;
5831 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
5832 if (rc)
5833 return rc;
5835 spd = (link->saved_scontrol >> 4) & 0xf;
5836 if (spd)
5837 link->hw_sata_spd_limit &= (1 << spd) - 1;
5839 ata_force_link_limits(link);
5841 link->sata_spd_limit = link->hw_sata_spd_limit;
5843 return 0;
5847 * ata_port_alloc - allocate and initialize basic ATA port resources
5848 * @host: ATA host this allocated port belongs to
5850 * Allocate and initialize basic ATA port resources.
5852 * RETURNS:
5853 * Allocate ATA port on success, NULL on failure.
5855 * LOCKING:
5856 * Inherited from calling layer (may sleep).
5858 struct ata_port *ata_port_alloc(struct ata_host *host)
5860 struct ata_port *ap;
5862 DPRINTK("ENTER\n");
5864 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5865 if (!ap)
5866 return NULL;
5868 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5869 ap->lock = &host->lock;
5870 ap->print_id = -1;
5871 ap->local_port_no = -1;
5872 ap->host = host;
5873 ap->dev = host->dev;
5875 #if defined(ATA_VERBOSE_DEBUG)
5876 /* turn on all debugging levels */
5877 ap->msg_enable = 0x00FF;
5878 #elif defined(ATA_DEBUG)
5879 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5880 #else
5881 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5882 #endif
5884 mutex_init(&ap->scsi_scan_mutex);
5885 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5886 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5887 INIT_LIST_HEAD(&ap->eh_done_q);
5888 init_waitqueue_head(&ap->eh_wait_q);
5889 init_completion(&ap->park_req_pending);
5890 init_timer_deferrable(&ap->fastdrain_timer);
5891 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5892 ap->fastdrain_timer.data = (unsigned long)ap;
5894 ap->cbl = ATA_CBL_NONE;
5896 ata_link_init(ap, &ap->link, 0);
5898 #ifdef ATA_IRQ_TRAP
5899 ap->stats.unhandled_irq = 1;
5900 ap->stats.idle_irq = 1;
5901 #endif
5902 ata_sff_port_init(ap);
5904 return ap;
5907 static void ata_host_release(struct device *gendev, void *res)
5909 struct ata_host *host = dev_get_drvdata(gendev);
5910 int i;
5912 for (i = 0; i < host->n_ports; i++) {
5913 struct ata_port *ap = host->ports[i];
5915 if (!ap)
5916 continue;
5918 if (ap->scsi_host)
5919 scsi_host_put(ap->scsi_host);
5921 kfree(ap->pmp_link);
5922 kfree(ap->slave_link);
5923 kfree(ap);
5924 host->ports[i] = NULL;
5927 dev_set_drvdata(gendev, NULL);
5931 * ata_host_alloc - allocate and init basic ATA host resources
5932 * @dev: generic device this host is associated with
5933 * @max_ports: maximum number of ATA ports associated with this host
5935 * Allocate and initialize basic ATA host resources. LLD calls
5936 * this function to allocate a host, initializes it fully and
5937 * attaches it using ata_host_register().
5939 * @max_ports ports are allocated and host->n_ports is
5940 * initialized to @max_ports. The caller is allowed to decrease
5941 * host->n_ports before calling ata_host_register(). The unused
5942 * ports will be automatically freed on registration.
5944 * RETURNS:
5945 * Allocate ATA host on success, NULL on failure.
5947 * LOCKING:
5948 * Inherited from calling layer (may sleep).
5950 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5952 struct ata_host *host;
5953 size_t sz;
5954 int i;
5956 DPRINTK("ENTER\n");
5958 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5959 return NULL;
5961 /* alloc a container for our list of ATA ports (buses) */
5962 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5963 /* alloc a container for our list of ATA ports (buses) */
5964 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5965 if (!host)
5966 goto err_out;
5968 devres_add(dev, host);
5969 dev_set_drvdata(dev, host);
5971 spin_lock_init(&host->lock);
5972 mutex_init(&host->eh_mutex);
5973 host->dev = dev;
5974 host->n_ports = max_ports;
5976 /* allocate ports bound to this host */
5977 for (i = 0; i < max_ports; i++) {
5978 struct ata_port *ap;
5980 ap = ata_port_alloc(host);
5981 if (!ap)
5982 goto err_out;
5984 ap->port_no = i;
5985 host->ports[i] = ap;
5988 devres_remove_group(dev, NULL);
5989 return host;
5991 err_out:
5992 devres_release_group(dev, NULL);
5993 return NULL;
5997 * ata_host_alloc_pinfo - alloc host and init with port_info array
5998 * @dev: generic device this host is associated with
5999 * @ppi: array of ATA port_info to initialize host with
6000 * @n_ports: number of ATA ports attached to this host
6002 * Allocate ATA host and initialize with info from @ppi. If NULL
6003 * terminated, @ppi may contain fewer entries than @n_ports. The
6004 * last entry will be used for the remaining ports.
6006 * RETURNS:
6007 * Allocate ATA host on success, NULL on failure.
6009 * LOCKING:
6010 * Inherited from calling layer (may sleep).
6012 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6013 const struct ata_port_info * const * ppi,
6014 int n_ports)
6016 const struct ata_port_info *pi;
6017 struct ata_host *host;
6018 int i, j;
6020 host = ata_host_alloc(dev, n_ports);
6021 if (!host)
6022 return NULL;
6024 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6025 struct ata_port *ap = host->ports[i];
6027 if (ppi[j])
6028 pi = ppi[j++];
6030 ap->pio_mask = pi->pio_mask;
6031 ap->mwdma_mask = pi->mwdma_mask;
6032 ap->udma_mask = pi->udma_mask;
6033 ap->flags |= pi->flags;
6034 ap->link.flags |= pi->link_flags;
6035 ap->ops = pi->port_ops;
6037 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6038 host->ops = pi->port_ops;
6041 return host;
6045 * ata_slave_link_init - initialize slave link
6046 * @ap: port to initialize slave link for
6048 * Create and initialize slave link for @ap. This enables slave
6049 * link handling on the port.
6051 * In libata, a port contains links and a link contains devices.
6052 * There is single host link but if a PMP is attached to it,
6053 * there can be multiple fan-out links. On SATA, there's usually
6054 * a single device connected to a link but PATA and SATA
6055 * controllers emulating TF based interface can have two - master
6056 * and slave.
6058 * However, there are a few controllers which don't fit into this
6059 * abstraction too well - SATA controllers which emulate TF
6060 * interface with both master and slave devices but also have
6061 * separate SCR register sets for each device. These controllers
6062 * need separate links for physical link handling
6063 * (e.g. onlineness, link speed) but should be treated like a
6064 * traditional M/S controller for everything else (e.g. command
6065 * issue, softreset).
6067 * slave_link is libata's way of handling this class of
6068 * controllers without impacting core layer too much. For
6069 * anything other than physical link handling, the default host
6070 * link is used for both master and slave. For physical link
6071 * handling, separate @ap->slave_link is used. All dirty details
6072 * are implemented inside libata core layer. From LLD's POV, the
6073 * only difference is that prereset, hardreset and postreset are
6074 * called once more for the slave link, so the reset sequence
6075 * looks like the following.
6077 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6078 * softreset(M) -> postreset(M) -> postreset(S)
6080 * Note that softreset is called only for the master. Softreset
6081 * resets both M/S by definition, so SRST on master should handle
6082 * both (the standard method will work just fine).
6084 * LOCKING:
6085 * Should be called before host is registered.
6087 * RETURNS:
6088 * 0 on success, -errno on failure.
6090 int ata_slave_link_init(struct ata_port *ap)
6092 struct ata_link *link;
6094 WARN_ON(ap->slave_link);
6095 WARN_ON(ap->flags & ATA_FLAG_PMP);
6097 link = kzalloc(sizeof(*link), GFP_KERNEL);
6098 if (!link)
6099 return -ENOMEM;
6101 ata_link_init(ap, link, 1);
6102 ap->slave_link = link;
6103 return 0;
6106 static void ata_host_stop(struct device *gendev, void *res)
6108 struct ata_host *host = dev_get_drvdata(gendev);
6109 int i;
6111 WARN_ON(!(host->flags & ATA_HOST_STARTED));
6113 for (i = 0; i < host->n_ports; i++) {
6114 struct ata_port *ap = host->ports[i];
6116 if (ap->ops->port_stop)
6117 ap->ops->port_stop(ap);
6120 if (host->ops->host_stop)
6121 host->ops->host_stop(host);
6125 * ata_finalize_port_ops - finalize ata_port_operations
6126 * @ops: ata_port_operations to finalize
6128 * An ata_port_operations can inherit from another ops and that
6129 * ops can again inherit from another. This can go on as many
6130 * times as necessary as long as there is no loop in the
6131 * inheritance chain.
6133 * Ops tables are finalized when the host is started. NULL or
6134 * unspecified entries are inherited from the closet ancestor
6135 * which has the method and the entry is populated with it.
6136 * After finalization, the ops table directly points to all the
6137 * methods and ->inherits is no longer necessary and cleared.
6139 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6141 * LOCKING:
6142 * None.
6144 static void ata_finalize_port_ops(struct ata_port_operations *ops)
6146 static DEFINE_SPINLOCK(lock);
6147 const struct ata_port_operations *cur;
6148 void **begin = (void **)ops;
6149 void **end = (void **)&ops->inherits;
6150 void **pp;
6152 if (!ops || !ops->inherits)
6153 return;
6155 spin_lock(&lock);
6157 for (cur = ops->inherits; cur; cur = cur->inherits) {
6158 void **inherit = (void **)cur;
6160 for (pp = begin; pp < end; pp++, inherit++)
6161 if (!*pp)
6162 *pp = *inherit;
6165 for (pp = begin; pp < end; pp++)
6166 if (IS_ERR(*pp))
6167 *pp = NULL;
6169 ops->inherits = NULL;
6171 spin_unlock(&lock);
6175 * ata_host_start - start and freeze ports of an ATA host
6176 * @host: ATA host to start ports for
6178 * Start and then freeze ports of @host. Started status is
6179 * recorded in host->flags, so this function can be called
6180 * multiple times. Ports are guaranteed to get started only
6181 * once. If host->ops isn't initialized yet, its set to the
6182 * first non-dummy port ops.
6184 * LOCKING:
6185 * Inherited from calling layer (may sleep).
6187 * RETURNS:
6188 * 0 if all ports are started successfully, -errno otherwise.
6190 int ata_host_start(struct ata_host *host)
6192 int have_stop = 0;
6193 void *start_dr = NULL;
6194 int i, rc;
6196 if (host->flags & ATA_HOST_STARTED)
6197 return 0;
6199 ata_finalize_port_ops(host->ops);
6201 for (i = 0; i < host->n_ports; i++) {
6202 struct ata_port *ap = host->ports[i];
6204 ata_finalize_port_ops(ap->ops);
6206 if (!host->ops && !ata_port_is_dummy(ap))
6207 host->ops = ap->ops;
6209 if (ap->ops->port_stop)
6210 have_stop = 1;
6213 if (host->ops->host_stop)
6214 have_stop = 1;
6216 if (have_stop) {
6217 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6218 if (!start_dr)
6219 return -ENOMEM;
6222 for (i = 0; i < host->n_ports; i++) {
6223 struct ata_port *ap = host->ports[i];
6225 if (ap->ops->port_start) {
6226 rc = ap->ops->port_start(ap);
6227 if (rc) {
6228 if (rc != -ENODEV)
6229 dev_err(host->dev,
6230 "failed to start port %d (errno=%d)\n",
6231 i, rc);
6232 goto err_out;
6235 ata_eh_freeze_port(ap);
6238 if (start_dr)
6239 devres_add(host->dev, start_dr);
6240 host->flags |= ATA_HOST_STARTED;
6241 return 0;
6243 err_out:
6244 while (--i >= 0) {
6245 struct ata_port *ap = host->ports[i];
6247 if (ap->ops->port_stop)
6248 ap->ops->port_stop(ap);
6250 devres_free(start_dr);
6251 return rc;
6255 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6256 * @host: host to initialize
6257 * @dev: device host is attached to
6258 * @ops: port_ops
6261 void ata_host_init(struct ata_host *host, struct device *dev,
6262 struct ata_port_operations *ops)
6264 spin_lock_init(&host->lock);
6265 mutex_init(&host->eh_mutex);
6266 host->n_tags = ATA_MAX_QUEUE - 1;
6267 host->dev = dev;
6268 host->ops = ops;
6271 void __ata_port_probe(struct ata_port *ap)
6273 struct ata_eh_info *ehi = &ap->link.eh_info;
6274 unsigned long flags;
6276 /* kick EH for boot probing */
6277 spin_lock_irqsave(ap->lock, flags);
6279 ehi->probe_mask |= ATA_ALL_DEVICES;
6280 ehi->action |= ATA_EH_RESET;
6281 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6283 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6284 ap->pflags |= ATA_PFLAG_LOADING;
6285 ata_port_schedule_eh(ap);
6287 spin_unlock_irqrestore(ap->lock, flags);
6290 int ata_port_probe(struct ata_port *ap)
6292 int rc = 0;
6294 if (ap->ops->error_handler) {
6295 __ata_port_probe(ap);
6296 ata_port_wait_eh(ap);
6297 } else {
6298 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6299 rc = ata_bus_probe(ap);
6300 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6302 return rc;
6306 static void async_port_probe(void *data, async_cookie_t cookie)
6308 struct ata_port *ap = data;
6311 * If we're not allowed to scan this host in parallel,
6312 * we need to wait until all previous scans have completed
6313 * before going further.
6314 * Jeff Garzik says this is only within a controller, so we
6315 * don't need to wait for port 0, only for later ports.
6317 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6318 async_synchronize_cookie(cookie);
6320 (void)ata_port_probe(ap);
6322 /* in order to keep device order, we need to synchronize at this point */
6323 async_synchronize_cookie(cookie);
6325 ata_scsi_scan_host(ap, 1);
6329 * ata_host_register - register initialized ATA host
6330 * @host: ATA host to register
6331 * @sht: template for SCSI host
6333 * Register initialized ATA host. @host is allocated using
6334 * ata_host_alloc() and fully initialized by LLD. This function
6335 * starts ports, registers @host with ATA and SCSI layers and
6336 * probe registered devices.
6338 * LOCKING:
6339 * Inherited from calling layer (may sleep).
6341 * RETURNS:
6342 * 0 on success, -errno otherwise.
6344 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6346 int i, rc;
6348 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE - 1);
6350 /* host must have been started */
6351 if (!(host->flags & ATA_HOST_STARTED)) {
6352 dev_err(host->dev, "BUG: trying to register unstarted host\n");
6353 WARN_ON(1);
6354 return -EINVAL;
6357 /* Blow away unused ports. This happens when LLD can't
6358 * determine the exact number of ports to allocate at
6359 * allocation time.
6361 for (i = host->n_ports; host->ports[i]; i++)
6362 kfree(host->ports[i]);
6364 /* give ports names and add SCSI hosts */
6365 for (i = 0; i < host->n_ports; i++) {
6366 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6367 host->ports[i]->local_port_no = i + 1;
6370 /* Create associated sysfs transport objects */
6371 for (i = 0; i < host->n_ports; i++) {
6372 rc = ata_tport_add(host->dev,host->ports[i]);
6373 if (rc) {
6374 goto err_tadd;
6378 rc = ata_scsi_add_hosts(host, sht);
6379 if (rc)
6380 goto err_tadd;
6382 /* set cable, sata_spd_limit and report */
6383 for (i = 0; i < host->n_ports; i++) {
6384 struct ata_port *ap = host->ports[i];
6385 unsigned long xfer_mask;
6387 /* set SATA cable type if still unset */
6388 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6389 ap->cbl = ATA_CBL_SATA;
6391 /* init sata_spd_limit to the current value */
6392 sata_link_init_spd(&ap->link);
6393 if (ap->slave_link)
6394 sata_link_init_spd(ap->slave_link);
6396 /* print per-port info to dmesg */
6397 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6398 ap->udma_mask);
6400 if (!ata_port_is_dummy(ap)) {
6401 ata_port_info(ap, "%cATA max %s %s\n",
6402 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6403 ata_mode_string(xfer_mask),
6404 ap->link.eh_info.desc);
6405 ata_ehi_clear_desc(&ap->link.eh_info);
6406 } else
6407 ata_port_info(ap, "DUMMY\n");
6410 /* perform each probe asynchronously */
6411 for (i = 0; i < host->n_ports; i++) {
6412 struct ata_port *ap = host->ports[i];
6413 async_schedule(async_port_probe, ap);
6416 return 0;
6418 err_tadd:
6419 while (--i >= 0) {
6420 ata_tport_delete(host->ports[i]);
6422 return rc;
6427 * ata_host_activate - start host, request IRQ and register it
6428 * @host: target ATA host
6429 * @irq: IRQ to request
6430 * @irq_handler: irq_handler used when requesting IRQ
6431 * @irq_flags: irq_flags used when requesting IRQ
6432 * @sht: scsi_host_template to use when registering the host
6434 * After allocating an ATA host and initializing it, most libata
6435 * LLDs perform three steps to activate the host - start host,
6436 * request IRQ and register it. This helper takes necessary
6437 * arguments and performs the three steps in one go.
6439 * An invalid IRQ skips the IRQ registration and expects the host to
6440 * have set polling mode on the port. In this case, @irq_handler
6441 * should be NULL.
6443 * LOCKING:
6444 * Inherited from calling layer (may sleep).
6446 * RETURNS:
6447 * 0 on success, -errno otherwise.
6449 int ata_host_activate(struct ata_host *host, int irq,
6450 irq_handler_t irq_handler, unsigned long irq_flags,
6451 struct scsi_host_template *sht)
6453 int i, rc;
6454 char *irq_desc;
6456 rc = ata_host_start(host);
6457 if (rc)
6458 return rc;
6460 /* Special case for polling mode */
6461 if (!irq) {
6462 WARN_ON(irq_handler);
6463 return ata_host_register(host, sht);
6466 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
6467 dev_driver_string(host->dev),
6468 dev_name(host->dev));
6469 if (!irq_desc)
6470 return -ENOMEM;
6472 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6473 irq_desc, host);
6474 if (rc)
6475 return rc;
6477 for (i = 0; i < host->n_ports; i++)
6478 ata_port_desc(host->ports[i], "irq %d", irq);
6480 rc = ata_host_register(host, sht);
6481 /* if failed, just free the IRQ and leave ports alone */
6482 if (rc)
6483 devm_free_irq(host->dev, irq, host);
6485 return rc;
6489 * ata_port_detach - Detach ATA port in preparation of device removal
6490 * @ap: ATA port to be detached
6492 * Detach all ATA devices and the associated SCSI devices of @ap;
6493 * then, remove the associated SCSI host. @ap is guaranteed to
6494 * be quiescent on return from this function.
6496 * LOCKING:
6497 * Kernel thread context (may sleep).
6499 static void ata_port_detach(struct ata_port *ap)
6501 unsigned long flags;
6502 struct ata_link *link;
6503 struct ata_device *dev;
6505 if (!ap->ops->error_handler)
6506 goto skip_eh;
6508 /* tell EH we're leaving & flush EH */
6509 spin_lock_irqsave(ap->lock, flags);
6510 ap->pflags |= ATA_PFLAG_UNLOADING;
6511 ata_port_schedule_eh(ap);
6512 spin_unlock_irqrestore(ap->lock, flags);
6514 /* wait till EH commits suicide */
6515 ata_port_wait_eh(ap);
6517 /* it better be dead now */
6518 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6520 cancel_delayed_work_sync(&ap->hotplug_task);
6522 skip_eh:
6523 /* clean up zpodd on port removal */
6524 ata_for_each_link(link, ap, HOST_FIRST) {
6525 ata_for_each_dev(dev, link, ALL) {
6526 if (zpodd_dev_enabled(dev))
6527 zpodd_exit(dev);
6530 if (ap->pmp_link) {
6531 int i;
6532 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6533 ata_tlink_delete(&ap->pmp_link[i]);
6535 /* remove the associated SCSI host */
6536 scsi_remove_host(ap->scsi_host);
6537 ata_tport_delete(ap);
6541 * ata_host_detach - Detach all ports of an ATA host
6542 * @host: Host to detach
6544 * Detach all ports of @host.
6546 * LOCKING:
6547 * Kernel thread context (may sleep).
6549 void ata_host_detach(struct ata_host *host)
6551 int i;
6553 for (i = 0; i < host->n_ports; i++)
6554 ata_port_detach(host->ports[i]);
6556 /* the host is dead now, dissociate ACPI */
6557 ata_acpi_dissociate(host);
6560 #ifdef CONFIG_PCI
6563 * ata_pci_remove_one - PCI layer callback for device removal
6564 * @pdev: PCI device that was removed
6566 * PCI layer indicates to libata via this hook that hot-unplug or
6567 * module unload event has occurred. Detach all ports. Resource
6568 * release is handled via devres.
6570 * LOCKING:
6571 * Inherited from PCI layer (may sleep).
6573 void ata_pci_remove_one(struct pci_dev *pdev)
6575 struct ata_host *host = pci_get_drvdata(pdev);
6577 ata_host_detach(host);
6580 /* move to PCI subsystem */
6581 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6583 unsigned long tmp = 0;
6585 switch (bits->width) {
6586 case 1: {
6587 u8 tmp8 = 0;
6588 pci_read_config_byte(pdev, bits->reg, &tmp8);
6589 tmp = tmp8;
6590 break;
6592 case 2: {
6593 u16 tmp16 = 0;
6594 pci_read_config_word(pdev, bits->reg, &tmp16);
6595 tmp = tmp16;
6596 break;
6598 case 4: {
6599 u32 tmp32 = 0;
6600 pci_read_config_dword(pdev, bits->reg, &tmp32);
6601 tmp = tmp32;
6602 break;
6605 default:
6606 return -EINVAL;
6609 tmp &= bits->mask;
6611 return (tmp == bits->val) ? 1 : 0;
6614 #ifdef CONFIG_PM
6615 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6617 pci_save_state(pdev);
6618 pci_disable_device(pdev);
6620 if (mesg.event & PM_EVENT_SLEEP)
6621 pci_set_power_state(pdev, PCI_D3hot);
6624 int ata_pci_device_do_resume(struct pci_dev *pdev)
6626 int rc;
6628 pci_set_power_state(pdev, PCI_D0);
6629 pci_restore_state(pdev);
6631 rc = pcim_enable_device(pdev);
6632 if (rc) {
6633 dev_err(&pdev->dev,
6634 "failed to enable device after resume (%d)\n", rc);
6635 return rc;
6638 pci_set_master(pdev);
6639 return 0;
6642 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6644 struct ata_host *host = pci_get_drvdata(pdev);
6645 int rc = 0;
6647 rc = ata_host_suspend(host, mesg);
6648 if (rc)
6649 return rc;
6651 ata_pci_device_do_suspend(pdev, mesg);
6653 return 0;
6656 int ata_pci_device_resume(struct pci_dev *pdev)
6658 struct ata_host *host = pci_get_drvdata(pdev);
6659 int rc;
6661 rc = ata_pci_device_do_resume(pdev);
6662 if (rc == 0)
6663 ata_host_resume(host);
6664 return rc;
6666 #endif /* CONFIG_PM */
6668 #endif /* CONFIG_PCI */
6671 * ata_platform_remove_one - Platform layer callback for device removal
6672 * @pdev: Platform device that was removed
6674 * Platform layer indicates to libata via this hook that hot-unplug or
6675 * module unload event has occurred. Detach all ports. Resource
6676 * release is handled via devres.
6678 * LOCKING:
6679 * Inherited from platform layer (may sleep).
6681 int ata_platform_remove_one(struct platform_device *pdev)
6683 struct ata_host *host = platform_get_drvdata(pdev);
6685 ata_host_detach(host);
6687 return 0;
6690 static int __init ata_parse_force_one(char **cur,
6691 struct ata_force_ent *force_ent,
6692 const char **reason)
6694 static const struct ata_force_param force_tbl[] __initconst = {
6695 { "40c", .cbl = ATA_CBL_PATA40 },
6696 { "80c", .cbl = ATA_CBL_PATA80 },
6697 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6698 { "unk", .cbl = ATA_CBL_PATA_UNK },
6699 { "ign", .cbl = ATA_CBL_PATA_IGN },
6700 { "sata", .cbl = ATA_CBL_SATA },
6701 { "1.5Gbps", .spd_limit = 1 },
6702 { "3.0Gbps", .spd_limit = 2 },
6703 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6704 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
6705 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6706 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
6707 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
6708 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6709 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6710 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6711 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6712 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6713 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6714 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6715 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6716 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6717 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6718 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6719 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6720 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6721 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6722 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6723 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6724 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6725 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6726 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6727 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6728 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6729 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6730 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6731 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6732 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6733 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6734 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6735 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6736 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6737 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6738 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6739 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6740 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6741 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
6742 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6743 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6744 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6745 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
6746 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
6747 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
6749 char *start = *cur, *p = *cur;
6750 char *id, *val, *endp;
6751 const struct ata_force_param *match_fp = NULL;
6752 int nr_matches = 0, i;
6754 /* find where this param ends and update *cur */
6755 while (*p != '\0' && *p != ',')
6756 p++;
6758 if (*p == '\0')
6759 *cur = p;
6760 else
6761 *cur = p + 1;
6763 *p = '\0';
6765 /* parse */
6766 p = strchr(start, ':');
6767 if (!p) {
6768 val = strstrip(start);
6769 goto parse_val;
6771 *p = '\0';
6773 id = strstrip(start);
6774 val = strstrip(p + 1);
6776 /* parse id */
6777 p = strchr(id, '.');
6778 if (p) {
6779 *p++ = '\0';
6780 force_ent->device = simple_strtoul(p, &endp, 10);
6781 if (p == endp || *endp != '\0') {
6782 *reason = "invalid device";
6783 return -EINVAL;
6787 force_ent->port = simple_strtoul(id, &endp, 10);
6788 if (id == endp || *endp != '\0') {
6789 *reason = "invalid port/link";
6790 return -EINVAL;
6793 parse_val:
6794 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6795 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6796 const struct ata_force_param *fp = &force_tbl[i];
6798 if (strncasecmp(val, fp->name, strlen(val)))
6799 continue;
6801 nr_matches++;
6802 match_fp = fp;
6804 if (strcasecmp(val, fp->name) == 0) {
6805 nr_matches = 1;
6806 break;
6810 if (!nr_matches) {
6811 *reason = "unknown value";
6812 return -EINVAL;
6814 if (nr_matches > 1) {
6815 *reason = "ambigious value";
6816 return -EINVAL;
6819 force_ent->param = *match_fp;
6821 return 0;
6824 static void __init ata_parse_force_param(void)
6826 int idx = 0, size = 1;
6827 int last_port = -1, last_device = -1;
6828 char *p, *cur, *next;
6830 /* calculate maximum number of params and allocate force_tbl */
6831 for (p = ata_force_param_buf; *p; p++)
6832 if (*p == ',')
6833 size++;
6835 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
6836 if (!ata_force_tbl) {
6837 printk(KERN_WARNING "ata: failed to extend force table, "
6838 "libata.force ignored\n");
6839 return;
6842 /* parse and populate the table */
6843 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6844 const char *reason = "";
6845 struct ata_force_ent te = { .port = -1, .device = -1 };
6847 next = cur;
6848 if (ata_parse_force_one(&next, &te, &reason)) {
6849 printk(KERN_WARNING "ata: failed to parse force "
6850 "parameter \"%s\" (%s)\n",
6851 cur, reason);
6852 continue;
6855 if (te.port == -1) {
6856 te.port = last_port;
6857 te.device = last_device;
6860 ata_force_tbl[idx++] = te;
6862 last_port = te.port;
6863 last_device = te.device;
6866 ata_force_tbl_size = idx;
6869 static int __init ata_init(void)
6871 int rc;
6873 ata_parse_force_param();
6875 rc = ata_sff_init();
6876 if (rc) {
6877 kfree(ata_force_tbl);
6878 return rc;
6881 libata_transport_init();
6882 ata_scsi_transport_template = ata_attach_transport();
6883 if (!ata_scsi_transport_template) {
6884 ata_sff_exit();
6885 rc = -ENOMEM;
6886 goto err_out;
6889 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6890 return 0;
6892 err_out:
6893 return rc;
6896 static void __exit ata_exit(void)
6898 ata_release_transport(ata_scsi_transport_template);
6899 libata_transport_exit();
6900 ata_sff_exit();
6901 kfree(ata_force_tbl);
6904 subsys_initcall(ata_init);
6905 module_exit(ata_exit);
6907 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6909 int ata_ratelimit(void)
6911 return __ratelimit(&ratelimit);
6915 * ata_msleep - ATA EH owner aware msleep
6916 * @ap: ATA port to attribute the sleep to
6917 * @msecs: duration to sleep in milliseconds
6919 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6920 * ownership is released before going to sleep and reacquired
6921 * after the sleep is complete. IOW, other ports sharing the
6922 * @ap->host will be allowed to own the EH while this task is
6923 * sleeping.
6925 * LOCKING:
6926 * Might sleep.
6928 void ata_msleep(struct ata_port *ap, unsigned int msecs)
6930 bool owns_eh = ap && ap->host->eh_owner == current;
6932 if (owns_eh)
6933 ata_eh_release(ap);
6935 if (msecs < 20) {
6936 unsigned long usecs = msecs * USEC_PER_MSEC;
6937 usleep_range(usecs, usecs + 50);
6938 } else {
6939 msleep(msecs);
6942 if (owns_eh)
6943 ata_eh_acquire(ap);
6947 * ata_wait_register - wait until register value changes
6948 * @ap: ATA port to wait register for, can be NULL
6949 * @reg: IO-mapped register
6950 * @mask: Mask to apply to read register value
6951 * @val: Wait condition
6952 * @interval: polling interval in milliseconds
6953 * @timeout: timeout in milliseconds
6955 * Waiting for some bits of register to change is a common
6956 * operation for ATA controllers. This function reads 32bit LE
6957 * IO-mapped register @reg and tests for the following condition.
6959 * (*@reg & mask) != val
6961 * If the condition is met, it returns; otherwise, the process is
6962 * repeated after @interval_msec until timeout.
6964 * LOCKING:
6965 * Kernel thread context (may sleep)
6967 * RETURNS:
6968 * The final register value.
6970 u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6971 unsigned long interval, unsigned long timeout)
6973 unsigned long deadline;
6974 u32 tmp;
6976 tmp = ioread32(reg);
6978 /* Calculate timeout _after_ the first read to make sure
6979 * preceding writes reach the controller before starting to
6980 * eat away the timeout.
6982 deadline = ata_deadline(jiffies, timeout);
6984 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6985 ata_msleep(ap, interval);
6986 tmp = ioread32(reg);
6989 return tmp;
6993 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
6994 * @link: Link receiving the event
6996 * Test whether the received PHY event has to be ignored or not.
6998 * LOCKING:
6999 * None:
7001 * RETURNS:
7002 * True if the event has to be ignored.
7004 bool sata_lpm_ignore_phy_events(struct ata_link *link)
7006 unsigned long lpm_timeout = link->last_lpm_change +
7007 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
7009 /* if LPM is enabled, PHYRDY doesn't mean anything */
7010 if (link->lpm_policy > ATA_LPM_MAX_POWER)
7011 return true;
7013 /* ignore the first PHY event after the LPM policy changed
7014 * as it is might be spurious
7016 if ((link->flags & ATA_LFLAG_CHANGED) &&
7017 time_before(jiffies, lpm_timeout))
7018 return true;
7020 return false;
7022 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
7025 * Dummy port_ops
7027 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7029 return AC_ERR_SYSTEM;
7032 static void ata_dummy_error_handler(struct ata_port *ap)
7034 /* truly dummy */
7037 struct ata_port_operations ata_dummy_port_ops = {
7038 .qc_prep = ata_noop_qc_prep,
7039 .qc_issue = ata_dummy_qc_issue,
7040 .error_handler = ata_dummy_error_handler,
7041 .sched_eh = ata_std_sched_eh,
7042 .end_eh = ata_std_end_eh,
7045 const struct ata_port_info ata_dummy_port_info = {
7046 .port_ops = &ata_dummy_port_ops,
7050 * Utility print functions
7052 void ata_port_printk(const struct ata_port *ap, const char *level,
7053 const char *fmt, ...)
7055 struct va_format vaf;
7056 va_list args;
7058 va_start(args, fmt);
7060 vaf.fmt = fmt;
7061 vaf.va = &args;
7063 printk("%sata%u: %pV", level, ap->print_id, &vaf);
7065 va_end(args);
7067 EXPORT_SYMBOL(ata_port_printk);
7069 void ata_link_printk(const struct ata_link *link, const char *level,
7070 const char *fmt, ...)
7072 struct va_format vaf;
7073 va_list args;
7075 va_start(args, fmt);
7077 vaf.fmt = fmt;
7078 vaf.va = &args;
7080 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7081 printk("%sata%u.%02u: %pV",
7082 level, link->ap->print_id, link->pmp, &vaf);
7083 else
7084 printk("%sata%u: %pV",
7085 level, link->ap->print_id, &vaf);
7087 va_end(args);
7089 EXPORT_SYMBOL(ata_link_printk);
7091 void ata_dev_printk(const struct ata_device *dev, const char *level,
7092 const char *fmt, ...)
7094 struct va_format vaf;
7095 va_list args;
7097 va_start(args, fmt);
7099 vaf.fmt = fmt;
7100 vaf.va = &args;
7102 printk("%sata%u.%02u: %pV",
7103 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7104 &vaf);
7106 va_end(args);
7108 EXPORT_SYMBOL(ata_dev_printk);
7110 void ata_print_version(const struct device *dev, const char *version)
7112 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
7114 EXPORT_SYMBOL(ata_print_version);
7117 * libata is essentially a library of internal helper functions for
7118 * low-level ATA host controller drivers. As such, the API/ABI is
7119 * likely to change as new drivers are added and updated.
7120 * Do not depend on ABI/API stability.
7122 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7123 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7124 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7125 EXPORT_SYMBOL_GPL(ata_base_port_ops);
7126 EXPORT_SYMBOL_GPL(sata_port_ops);
7127 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
7128 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
7129 EXPORT_SYMBOL_GPL(ata_link_next);
7130 EXPORT_SYMBOL_GPL(ata_dev_next);
7131 EXPORT_SYMBOL_GPL(ata_std_bios_param);
7132 EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7133 EXPORT_SYMBOL_GPL(ata_host_init);
7134 EXPORT_SYMBOL_GPL(ata_host_alloc);
7135 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7136 EXPORT_SYMBOL_GPL(ata_slave_link_init);
7137 EXPORT_SYMBOL_GPL(ata_host_start);
7138 EXPORT_SYMBOL_GPL(ata_host_register);
7139 EXPORT_SYMBOL_GPL(ata_host_activate);
7140 EXPORT_SYMBOL_GPL(ata_host_detach);
7141 EXPORT_SYMBOL_GPL(ata_sg_init);
7142 EXPORT_SYMBOL_GPL(ata_qc_complete);
7143 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7144 EXPORT_SYMBOL_GPL(atapi_cmd_type);
7145 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7146 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7147 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7148 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7149 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7150 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7151 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7152 EXPORT_SYMBOL_GPL(ata_mode_string);
7153 EXPORT_SYMBOL_GPL(ata_id_xfermask);
7154 EXPORT_SYMBOL_GPL(ata_do_set_mode);
7155 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7156 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
7157 EXPORT_SYMBOL_GPL(ata_dev_disable);
7158 EXPORT_SYMBOL_GPL(sata_set_spd);
7159 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7160 EXPORT_SYMBOL_GPL(sata_link_debounce);
7161 EXPORT_SYMBOL_GPL(sata_link_resume);
7162 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
7163 EXPORT_SYMBOL_GPL(ata_std_prereset);
7164 EXPORT_SYMBOL_GPL(sata_link_hardreset);
7165 EXPORT_SYMBOL_GPL(sata_std_hardreset);
7166 EXPORT_SYMBOL_GPL(ata_std_postreset);
7167 EXPORT_SYMBOL_GPL(ata_dev_classify);
7168 EXPORT_SYMBOL_GPL(ata_dev_pair);
7169 EXPORT_SYMBOL_GPL(ata_ratelimit);
7170 EXPORT_SYMBOL_GPL(ata_msleep);
7171 EXPORT_SYMBOL_GPL(ata_wait_register);
7172 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7173 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7174 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7175 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7176 EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7177 EXPORT_SYMBOL_GPL(sata_scr_valid);
7178 EXPORT_SYMBOL_GPL(sata_scr_read);
7179 EXPORT_SYMBOL_GPL(sata_scr_write);
7180 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7181 EXPORT_SYMBOL_GPL(ata_link_online);
7182 EXPORT_SYMBOL_GPL(ata_link_offline);
7183 #ifdef CONFIG_PM
7184 EXPORT_SYMBOL_GPL(ata_host_suspend);
7185 EXPORT_SYMBOL_GPL(ata_host_resume);
7186 #endif /* CONFIG_PM */
7187 EXPORT_SYMBOL_GPL(ata_id_string);
7188 EXPORT_SYMBOL_GPL(ata_id_c_string);
7189 EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7190 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7192 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7193 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7194 EXPORT_SYMBOL_GPL(ata_timing_compute);
7195 EXPORT_SYMBOL_GPL(ata_timing_merge);
7196 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7198 #ifdef CONFIG_PCI
7199 EXPORT_SYMBOL_GPL(pci_test_config_bits);
7200 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7201 #ifdef CONFIG_PM
7202 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7203 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7204 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7205 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
7206 #endif /* CONFIG_PM */
7207 #endif /* CONFIG_PCI */
7209 EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7211 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7212 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7213 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7214 EXPORT_SYMBOL_GPL(ata_port_desc);
7215 #ifdef CONFIG_PCI
7216 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7217 #endif /* CONFIG_PCI */
7218 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7219 EXPORT_SYMBOL_GPL(ata_link_abort);
7220 EXPORT_SYMBOL_GPL(ata_port_abort);
7221 EXPORT_SYMBOL_GPL(ata_port_freeze);
7222 EXPORT_SYMBOL_GPL(sata_async_notification);
7223 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7224 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7225 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7226 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
7227 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7228 EXPORT_SYMBOL_GPL(ata_do_eh);
7229 EXPORT_SYMBOL_GPL(ata_std_error_handler);
7231 EXPORT_SYMBOL_GPL(ata_cable_40wire);
7232 EXPORT_SYMBOL_GPL(ata_cable_80wire);
7233 EXPORT_SYMBOL_GPL(ata_cable_unknown);
7234 EXPORT_SYMBOL_GPL(ata_cable_ignore);
7235 EXPORT_SYMBOL_GPL(ata_cable_sata);