1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libahci.c - Common AHCI SATA low-level routines
5 * Maintained by: Tejun Heo <tj@kernel.org>
6 * Please ALWAYS copy linux-ide@vger.kernel.org
9 * Copyright 2004-2005 Red Hat, Inc.
11 * libata documentation is available via 'make {ps|pdf}docs',
12 * as Documentation/driver-api/libata.rst
14 * AHCI hardware documentation:
15 * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
16 * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
19 #include <linux/kernel.h>
20 #include <linux/gfp.h>
21 #include <linux/module.h>
22 #include <linux/nospec.h>
23 #include <linux/blkdev.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <linux/libata.h>
31 #include <linux/pci.h>
35 static int ahci_skip_host_reset;
37 EXPORT_SYMBOL_GPL(ahci_ignore_sss);
39 module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
40 MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
42 module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
43 MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
45 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
47 static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
48 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
50 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
55 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
56 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
57 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
58 static int ahci_port_start(struct ata_port *ap);
59 static void ahci_port_stop(struct ata_port *ap);
60 static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc);
61 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
62 static void ahci_freeze(struct ata_port *ap);
63 static void ahci_thaw(struct ata_port *ap);
64 static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
65 static void ahci_enable_fbs(struct ata_port *ap);
66 static void ahci_disable_fbs(struct ata_port *ap);
67 static void ahci_pmp_attach(struct ata_port *ap);
68 static void ahci_pmp_detach(struct ata_port *ap);
69 static int ahci_softreset(struct ata_link *link, unsigned int *class,
70 unsigned long deadline);
71 static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
72 unsigned long deadline);
73 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
74 unsigned long deadline);
75 static void ahci_postreset(struct ata_link *link, unsigned int *class);
76 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
77 static void ahci_dev_config(struct ata_device *dev);
79 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
81 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
82 static ssize_t ahci_activity_store(struct ata_device *dev,
83 enum sw_activity val);
84 static void ahci_init_sw_activity(struct ata_link *link);
86 static ssize_t ahci_show_host_caps(struct device *dev,
87 struct device_attribute *attr, char *buf);
88 static ssize_t ahci_show_host_cap2(struct device *dev,
89 struct device_attribute *attr, char *buf);
90 static ssize_t ahci_show_host_version(struct device *dev,
91 struct device_attribute *attr, char *buf);
92 static ssize_t ahci_show_port_cmd(struct device *dev,
93 struct device_attribute *attr, char *buf);
94 static ssize_t ahci_read_em_buffer(struct device *dev,
95 struct device_attribute *attr, char *buf);
96 static ssize_t ahci_store_em_buffer(struct device *dev,
97 struct device_attribute *attr,
98 const char *buf, size_t size);
99 static ssize_t ahci_show_em_supported(struct device *dev,
100 struct device_attribute *attr, char *buf);
101 static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
103 static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
104 static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
105 static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
106 static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
107 static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
108 ahci_read_em_buffer, ahci_store_em_buffer);
109 static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
111 struct device_attribute *ahci_shost_attrs[] = {
112 &dev_attr_link_power_management_policy,
113 &dev_attr_em_message_type,
114 &dev_attr_em_message,
115 &dev_attr_ahci_host_caps,
116 &dev_attr_ahci_host_cap2,
117 &dev_attr_ahci_host_version,
118 &dev_attr_ahci_port_cmd,
120 &dev_attr_em_message_supported,
123 EXPORT_SYMBOL_GPL(ahci_shost_attrs);
125 struct device_attribute *ahci_sdev_attrs[] = {
126 &dev_attr_sw_activity,
127 &dev_attr_unload_heads,
128 &dev_attr_ncq_prio_enable,
131 EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
133 struct ata_port_operations ahci_ops = {
134 .inherits = &sata_pmp_port_ops,
136 .qc_defer = ahci_pmp_qc_defer,
137 .qc_prep = ahci_qc_prep,
138 .qc_issue = ahci_qc_issue,
139 .qc_fill_rtf = ahci_qc_fill_rtf,
141 .freeze = ahci_freeze,
143 .softreset = ahci_softreset,
144 .hardreset = ahci_hardreset,
145 .postreset = ahci_postreset,
146 .pmp_softreset = ahci_softreset,
147 .error_handler = ahci_error_handler,
148 .post_internal_cmd = ahci_post_internal_cmd,
149 .dev_config = ahci_dev_config,
151 .scr_read = ahci_scr_read,
152 .scr_write = ahci_scr_write,
153 .pmp_attach = ahci_pmp_attach,
154 .pmp_detach = ahci_pmp_detach,
156 .set_lpm = ahci_set_lpm,
157 .em_show = ahci_led_show,
158 .em_store = ahci_led_store,
159 .sw_activity_show = ahci_activity_show,
160 .sw_activity_store = ahci_activity_store,
161 .transmit_led_message = ahci_transmit_led_message,
163 .port_suspend = ahci_port_suspend,
164 .port_resume = ahci_port_resume,
166 .port_start = ahci_port_start,
167 .port_stop = ahci_port_stop,
169 EXPORT_SYMBOL_GPL(ahci_ops);
171 struct ata_port_operations ahci_pmp_retry_srst_ops = {
172 .inherits = &ahci_ops,
173 .softreset = ahci_pmp_retry_softreset,
175 EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
177 static bool ahci_em_messages __read_mostly = true;
178 module_param(ahci_em_messages, bool, 0444);
179 /* add other LED protocol types when they become supported */
180 MODULE_PARM_DESC(ahci_em_messages,
181 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
183 /* device sleep idle timeout in ms */
184 static int devslp_idle_timeout __read_mostly = 1000;
185 module_param(devslp_idle_timeout, int, 0644);
186 MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
188 static void ahci_enable_ahci(void __iomem *mmio)
193 /* turn on AHCI_EN */
194 tmp = readl(mmio + HOST_CTL);
195 if (tmp & HOST_AHCI_EN)
198 /* Some controllers need AHCI_EN to be written multiple times.
199 * Try a few times before giving up.
201 for (i = 0; i < 5; i++) {
203 writel(tmp, mmio + HOST_CTL);
204 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
205 if (tmp & HOST_AHCI_EN)
214 * ahci_rpm_get_port - Make sure the port is powered on
215 * @ap: Port to power on
217 * Whenever there is need to access the AHCI host registers outside of
218 * normal execution paths, call this function to make sure the host is
219 * actually powered on.
221 static int ahci_rpm_get_port(struct ata_port *ap)
223 return pm_runtime_get_sync(ap->dev);
227 * ahci_rpm_put_port - Undoes ahci_rpm_get_port()
228 * @ap: Port to power down
230 * Undoes ahci_rpm_get_port() and possibly powers down the AHCI host
231 * if it has no more active users.
233 static void ahci_rpm_put_port(struct ata_port *ap)
235 pm_runtime_put(ap->dev);
238 static ssize_t ahci_show_host_caps(struct device *dev,
239 struct device_attribute *attr, char *buf)
241 struct Scsi_Host *shost = class_to_shost(dev);
242 struct ata_port *ap = ata_shost_to_port(shost);
243 struct ahci_host_priv *hpriv = ap->host->private_data;
245 return sprintf(buf, "%x\n", hpriv->cap);
248 static ssize_t ahci_show_host_cap2(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct Scsi_Host *shost = class_to_shost(dev);
252 struct ata_port *ap = ata_shost_to_port(shost);
253 struct ahci_host_priv *hpriv = ap->host->private_data;
255 return sprintf(buf, "%x\n", hpriv->cap2);
258 static ssize_t ahci_show_host_version(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct Scsi_Host *shost = class_to_shost(dev);
262 struct ata_port *ap = ata_shost_to_port(shost);
263 struct ahci_host_priv *hpriv = ap->host->private_data;
265 return sprintf(buf, "%x\n", hpriv->version);
268 static ssize_t ahci_show_port_cmd(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct Scsi_Host *shost = class_to_shost(dev);
272 struct ata_port *ap = ata_shost_to_port(shost);
273 void __iomem *port_mmio = ahci_port_base(ap);
276 ahci_rpm_get_port(ap);
277 ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
278 ahci_rpm_put_port(ap);
283 static ssize_t ahci_read_em_buffer(struct device *dev,
284 struct device_attribute *attr, char *buf)
286 struct Scsi_Host *shost = class_to_shost(dev);
287 struct ata_port *ap = ata_shost_to_port(shost);
288 struct ahci_host_priv *hpriv = ap->host->private_data;
289 void __iomem *mmio = hpriv->mmio;
290 void __iomem *em_mmio = mmio + hpriv->em_loc;
296 ahci_rpm_get_port(ap);
297 spin_lock_irqsave(ap->lock, flags);
299 em_ctl = readl(mmio + HOST_EM_CTL);
300 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
301 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
302 spin_unlock_irqrestore(ap->lock, flags);
303 ahci_rpm_put_port(ap);
307 if (!(em_ctl & EM_CTL_MR)) {
308 spin_unlock_irqrestore(ap->lock, flags);
309 ahci_rpm_put_port(ap);
313 if (!(em_ctl & EM_CTL_SMB))
314 em_mmio += hpriv->em_buf_sz;
316 count = hpriv->em_buf_sz;
318 /* the count should not be larger than PAGE_SIZE */
319 if (count > PAGE_SIZE) {
320 if (printk_ratelimit())
322 "EM read buffer size too large: "
323 "buffer size %u, page size %lu\n",
324 hpriv->em_buf_sz, PAGE_SIZE);
328 for (i = 0; i < count; i += 4) {
329 msg = readl(em_mmio + i);
331 buf[i + 1] = (msg >> 8) & 0xff;
332 buf[i + 2] = (msg >> 16) & 0xff;
333 buf[i + 3] = (msg >> 24) & 0xff;
336 spin_unlock_irqrestore(ap->lock, flags);
337 ahci_rpm_put_port(ap);
342 static ssize_t ahci_store_em_buffer(struct device *dev,
343 struct device_attribute *attr,
344 const char *buf, size_t size)
346 struct Scsi_Host *shost = class_to_shost(dev);
347 struct ata_port *ap = ata_shost_to_port(shost);
348 struct ahci_host_priv *hpriv = ap->host->private_data;
349 void __iomem *mmio = hpriv->mmio;
350 void __iomem *em_mmio = mmio + hpriv->em_loc;
351 const unsigned char *msg_buf = buf;
356 /* check size validity */
357 if (!(ap->flags & ATA_FLAG_EM) ||
358 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
359 size % 4 || size > hpriv->em_buf_sz)
362 ahci_rpm_get_port(ap);
363 spin_lock_irqsave(ap->lock, flags);
365 em_ctl = readl(mmio + HOST_EM_CTL);
366 if (em_ctl & EM_CTL_TM) {
367 spin_unlock_irqrestore(ap->lock, flags);
368 ahci_rpm_put_port(ap);
372 for (i = 0; i < size; i += 4) {
373 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
374 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
375 writel(msg, em_mmio + i);
378 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
380 spin_unlock_irqrestore(ap->lock, flags);
381 ahci_rpm_put_port(ap);
386 static ssize_t ahci_show_em_supported(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 struct Scsi_Host *shost = class_to_shost(dev);
390 struct ata_port *ap = ata_shost_to_port(shost);
391 struct ahci_host_priv *hpriv = ap->host->private_data;
392 void __iomem *mmio = hpriv->mmio;
395 ahci_rpm_get_port(ap);
396 em_ctl = readl(mmio + HOST_EM_CTL);
397 ahci_rpm_put_port(ap);
399 return sprintf(buf, "%s%s%s%s\n",
400 em_ctl & EM_CTL_LED ? "led " : "",
401 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
402 em_ctl & EM_CTL_SES ? "ses-2 " : "",
403 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
407 * ahci_save_initial_config - Save and fixup initial config values
408 * @dev: target AHCI device
409 * @hpriv: host private area to store config values
411 * Some registers containing configuration info might be setup by
412 * BIOS and might be cleared on reset. This function saves the
413 * initial values of those registers into @hpriv such that they
414 * can be restored after controller reset.
416 * If inconsistent, config values are fixed up by this function.
418 * If it is not set already this function sets hpriv->start_engine to
424 void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
426 void __iomem *mmio = hpriv->mmio;
427 u32 cap, cap2, vers, port_map;
430 /* make sure AHCI mode is enabled before accessing CAP */
431 ahci_enable_ahci(mmio);
433 /* Values prefixed with saved_ are written back to host after
434 * reset. Values without are used for driver operation.
436 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
437 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
439 /* CAP2 register is only defined for AHCI 1.2 and later */
440 vers = readl(mmio + HOST_VERSION);
441 if ((vers >> 16) > 1 ||
442 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
443 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
445 hpriv->saved_cap2 = cap2 = 0;
447 /* some chips have errata preventing 64bit use */
448 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
449 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
453 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
454 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
455 cap &= ~HOST_CAP_NCQ;
458 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
459 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
463 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
464 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
465 cap &= ~HOST_CAP_PMP;
468 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
470 "controller can't do SNTF, turning off CAP_SNTF\n");
471 cap &= ~HOST_CAP_SNTF;
474 if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
476 "controller can't do DEVSLP, turning off\n");
477 cap2 &= ~HOST_CAP2_SDS;
478 cap2 &= ~HOST_CAP2_SADM;
481 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
482 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
486 if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
487 dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
488 cap &= ~HOST_CAP_FBS;
491 if (!(cap & HOST_CAP_ALPM) && (hpriv->flags & AHCI_HFLAG_YES_ALPM)) {
492 dev_info(dev, "controller can do ALPM, turning on CAP_ALPM\n");
493 cap |= HOST_CAP_ALPM;
496 if ((cap & HOST_CAP_SXS) && (hpriv->flags & AHCI_HFLAG_NO_SXS)) {
497 dev_info(dev, "controller does not support SXS, disabling CAP_SXS\n");
498 cap &= ~HOST_CAP_SXS;
501 if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
502 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
503 port_map, hpriv->force_port_map);
504 port_map = hpriv->force_port_map;
505 hpriv->saved_port_map = port_map;
508 if (hpriv->mask_port_map) {
509 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
511 port_map & hpriv->mask_port_map);
512 port_map &= hpriv->mask_port_map;
515 /* cross check port_map and cap.n_ports */
519 for (i = 0; i < AHCI_MAX_PORTS; i++)
520 if (port_map & (1 << i))
523 /* If PI has more ports than n_ports, whine, clear
524 * port_map and let it be generated from n_ports.
526 if (map_ports > ahci_nr_ports(cap)) {
528 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
529 port_map, ahci_nr_ports(cap));
534 /* fabricate port_map from cap.nr_ports for < AHCI 1.3 */
535 if (!port_map && vers < 0x10300) {
536 port_map = (1 << ahci_nr_ports(cap)) - 1;
537 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
539 /* write the fixed up value to the PI register */
540 hpriv->saved_port_map = port_map;
543 /* record values to use during operation */
546 hpriv->version = readl(mmio + HOST_VERSION);
547 hpriv->port_map = port_map;
549 if (!hpriv->start_engine)
550 hpriv->start_engine = ahci_start_engine;
552 if (!hpriv->stop_engine)
553 hpriv->stop_engine = ahci_stop_engine;
555 if (!hpriv->irq_handler)
556 hpriv->irq_handler = ahci_single_level_irq_intr;
558 EXPORT_SYMBOL_GPL(ahci_save_initial_config);
561 * ahci_restore_initial_config - Restore initial config
562 * @host: target ATA host
564 * Restore initial config stored by ahci_save_initial_config().
569 static void ahci_restore_initial_config(struct ata_host *host)
571 struct ahci_host_priv *hpriv = host->private_data;
572 void __iomem *mmio = hpriv->mmio;
574 writel(hpriv->saved_cap, mmio + HOST_CAP);
575 if (hpriv->saved_cap2)
576 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
577 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
578 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
581 static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
583 static const int offset[] = {
584 [SCR_STATUS] = PORT_SCR_STAT,
585 [SCR_CONTROL] = PORT_SCR_CTL,
586 [SCR_ERROR] = PORT_SCR_ERR,
587 [SCR_ACTIVE] = PORT_SCR_ACT,
588 [SCR_NOTIFICATION] = PORT_SCR_NTF,
590 struct ahci_host_priv *hpriv = ap->host->private_data;
592 if (sc_reg < ARRAY_SIZE(offset) &&
593 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
594 return offset[sc_reg];
598 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
600 void __iomem *port_mmio = ahci_port_base(link->ap);
601 int offset = ahci_scr_offset(link->ap, sc_reg);
604 *val = readl(port_mmio + offset);
610 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
612 void __iomem *port_mmio = ahci_port_base(link->ap);
613 int offset = ahci_scr_offset(link->ap, sc_reg);
616 writel(val, port_mmio + offset);
622 void ahci_start_engine(struct ata_port *ap)
624 void __iomem *port_mmio = ahci_port_base(ap);
628 tmp = readl(port_mmio + PORT_CMD);
629 tmp |= PORT_CMD_START;
630 writel(tmp, port_mmio + PORT_CMD);
631 readl(port_mmio + PORT_CMD); /* flush */
633 EXPORT_SYMBOL_GPL(ahci_start_engine);
635 int ahci_stop_engine(struct ata_port *ap)
637 void __iomem *port_mmio = ahci_port_base(ap);
638 struct ahci_host_priv *hpriv = ap->host->private_data;
642 * On some controllers, stopping a port's DMA engine while the port
643 * is in ALPM state (partial or slumber) results in failures on
644 * subsequent DMA engine starts. For those controllers, put the
645 * port back in active state before stopping its DMA engine.
647 if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
648 (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
649 ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
650 dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
654 tmp = readl(port_mmio + PORT_CMD);
656 /* check if the HBA is idle */
657 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
661 * Don't try to issue commands but return with ENODEV if the
662 * AHCI controller not available anymore (e.g. due to PCIe hot
663 * unplugging). Otherwise a 500ms delay for each port is added.
665 if (tmp == 0xffffffff) {
666 dev_err(ap->host->dev, "AHCI controller unavailable!\n");
670 /* setting HBA to idle */
671 tmp &= ~PORT_CMD_START;
672 writel(tmp, port_mmio + PORT_CMD);
674 /* wait for engine to stop. This could be as long as 500 msec */
675 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
676 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
677 if (tmp & PORT_CMD_LIST_ON)
682 EXPORT_SYMBOL_GPL(ahci_stop_engine);
684 void ahci_start_fis_rx(struct ata_port *ap)
686 void __iomem *port_mmio = ahci_port_base(ap);
687 struct ahci_host_priv *hpriv = ap->host->private_data;
688 struct ahci_port_priv *pp = ap->private_data;
691 /* set FIS registers */
692 if (hpriv->cap & HOST_CAP_64)
693 writel((pp->cmd_slot_dma >> 16) >> 16,
694 port_mmio + PORT_LST_ADDR_HI);
695 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
697 if (hpriv->cap & HOST_CAP_64)
698 writel((pp->rx_fis_dma >> 16) >> 16,
699 port_mmio + PORT_FIS_ADDR_HI);
700 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
702 /* enable FIS reception */
703 tmp = readl(port_mmio + PORT_CMD);
704 tmp |= PORT_CMD_FIS_RX;
705 writel(tmp, port_mmio + PORT_CMD);
708 readl(port_mmio + PORT_CMD);
710 EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
712 static int ahci_stop_fis_rx(struct ata_port *ap)
714 void __iomem *port_mmio = ahci_port_base(ap);
717 /* disable FIS reception */
718 tmp = readl(port_mmio + PORT_CMD);
719 tmp &= ~PORT_CMD_FIS_RX;
720 writel(tmp, port_mmio + PORT_CMD);
722 /* wait for completion, spec says 500ms, give it 1000 */
723 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
724 PORT_CMD_FIS_ON, 10, 1000);
725 if (tmp & PORT_CMD_FIS_ON)
731 static void ahci_power_up(struct ata_port *ap)
733 struct ahci_host_priv *hpriv = ap->host->private_data;
734 void __iomem *port_mmio = ahci_port_base(ap);
737 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
740 if (hpriv->cap & HOST_CAP_SSS) {
741 cmd |= PORT_CMD_SPIN_UP;
742 writel(cmd, port_mmio + PORT_CMD);
746 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
749 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
752 struct ata_port *ap = link->ap;
753 struct ahci_host_priv *hpriv = ap->host->private_data;
754 struct ahci_port_priv *pp = ap->private_data;
755 void __iomem *port_mmio = ahci_port_base(ap);
757 if (policy != ATA_LPM_MAX_POWER) {
758 /* wakeup flag only applies to the max power policy */
759 hints &= ~ATA_LPM_WAKE_ONLY;
762 * Disable interrupts on Phy Ready. This keeps us from
763 * getting woken up due to spurious phy ready
766 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
767 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
769 sata_link_scr_lpm(link, policy, false);
772 if (hpriv->cap & HOST_CAP_ALPM) {
773 u32 cmd = readl(port_mmio + PORT_CMD);
775 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
776 if (!(hints & ATA_LPM_WAKE_ONLY))
777 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
778 cmd |= PORT_CMD_ICC_ACTIVE;
780 writel(cmd, port_mmio + PORT_CMD);
781 readl(port_mmio + PORT_CMD);
783 /* wait 10ms to be sure we've come out of LPM state */
786 if (hints & ATA_LPM_WAKE_ONLY)
789 cmd |= PORT_CMD_ALPE;
790 if (policy == ATA_LPM_MIN_POWER)
792 else if (policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
793 cmd &= ~PORT_CMD_ASP;
795 /* write out new cmd value */
796 writel(cmd, port_mmio + PORT_CMD);
800 /* set aggressive device sleep */
801 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
802 (hpriv->cap2 & HOST_CAP2_SADM) &&
803 (link->device->flags & ATA_DFLAG_DEVSLP)) {
804 if (policy == ATA_LPM_MIN_POWER ||
805 policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
806 ahci_set_aggressive_devslp(ap, true);
808 ahci_set_aggressive_devslp(ap, false);
811 if (policy == ATA_LPM_MAX_POWER) {
812 sata_link_scr_lpm(link, policy, false);
814 /* turn PHYRDY IRQ back on */
815 pp->intr_mask |= PORT_IRQ_PHYRDY;
816 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
823 static void ahci_power_down(struct ata_port *ap)
825 struct ahci_host_priv *hpriv = ap->host->private_data;
826 void __iomem *port_mmio = ahci_port_base(ap);
829 if (!(hpriv->cap & HOST_CAP_SSS))
832 /* put device into listen mode, first set PxSCTL.DET to 0 */
833 scontrol = readl(port_mmio + PORT_SCR_CTL);
835 writel(scontrol, port_mmio + PORT_SCR_CTL);
837 /* then set PxCMD.SUD to 0 */
838 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
839 cmd &= ~PORT_CMD_SPIN_UP;
840 writel(cmd, port_mmio + PORT_CMD);
844 static void ahci_start_port(struct ata_port *ap)
846 struct ahci_host_priv *hpriv = ap->host->private_data;
847 struct ahci_port_priv *pp = ap->private_data;
848 struct ata_link *link;
849 struct ahci_em_priv *emp;
853 /* enable FIS reception */
854 ahci_start_fis_rx(ap);
857 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
858 hpriv->start_engine(ap);
861 if (ap->flags & ATA_FLAG_EM) {
862 ata_for_each_link(link, ap, EDGE) {
863 emp = &pp->em_priv[link->pmp];
865 /* EM Transmit bit maybe busy during init */
866 for (i = 0; i < EM_MAX_RETRY; i++) {
867 rc = ap->ops->transmit_led_message(ap,
871 * If busy, give a breather but do not
872 * release EH ownership by using msleep()
873 * instead of ata_msleep(). EM Transmit
874 * bit is busy for the whole host and
875 * releasing ownership will cause other
876 * ports to fail the same way.
886 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
887 ata_for_each_link(link, ap, EDGE)
888 ahci_init_sw_activity(link);
892 static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
895 struct ahci_host_priv *hpriv = ap->host->private_data;
898 rc = hpriv->stop_engine(ap);
900 *emsg = "failed to stop engine";
904 /* disable FIS reception */
905 rc = ahci_stop_fis_rx(ap);
907 *emsg = "failed stop FIS RX";
914 int ahci_reset_controller(struct ata_host *host)
916 struct ahci_host_priv *hpriv = host->private_data;
917 void __iomem *mmio = hpriv->mmio;
920 /* we must be in AHCI mode, before using anything
921 * AHCI-specific, such as HOST_RESET.
923 ahci_enable_ahci(mmio);
925 /* global controller reset */
926 if (!ahci_skip_host_reset) {
927 tmp = readl(mmio + HOST_CTL);
928 if ((tmp & HOST_RESET) == 0) {
929 writel(tmp | HOST_RESET, mmio + HOST_CTL);
930 readl(mmio + HOST_CTL); /* flush */
934 * to perform host reset, OS should set HOST_RESET
935 * and poll until this bit is read to be "0".
936 * reset must complete within 1 second, or
937 * the hardware should be considered fried.
939 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
940 HOST_RESET, 10, 1000);
942 if (tmp & HOST_RESET) {
943 dev_err(host->dev, "controller reset failed (0x%x)\n",
948 /* turn on AHCI mode */
949 ahci_enable_ahci(mmio);
951 /* Some registers might be cleared on reset. Restore
954 if (!(hpriv->flags & AHCI_HFLAG_NO_WRITE_TO_RO))
955 ahci_restore_initial_config(host);
957 dev_info(host->dev, "skipping global host reset\n");
961 EXPORT_SYMBOL_GPL(ahci_reset_controller);
963 static void ahci_sw_activity(struct ata_link *link)
965 struct ata_port *ap = link->ap;
966 struct ahci_port_priv *pp = ap->private_data;
967 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
969 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
973 if (!timer_pending(&emp->timer))
974 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
977 static void ahci_sw_activity_blink(struct timer_list *t)
979 struct ahci_em_priv *emp = from_timer(emp, t, timer);
980 struct ata_link *link = emp->link;
981 struct ata_port *ap = link->ap;
983 unsigned long led_message = emp->led_state;
984 u32 activity_led_state;
987 led_message &= EM_MSG_LED_VALUE;
988 led_message |= ap->port_no | (link->pmp << 8);
990 /* check to see if we've had activity. If so,
991 * toggle state of LED and reset timer. If not,
992 * turn LED to desired idle state.
994 spin_lock_irqsave(ap->lock, flags);
995 if (emp->saved_activity != emp->activity) {
996 emp->saved_activity = emp->activity;
997 /* get the current LED state */
998 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
1000 if (activity_led_state)
1001 activity_led_state = 0;
1003 activity_led_state = 1;
1005 /* clear old state */
1006 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1009 led_message |= (activity_led_state << 16);
1010 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1012 /* switch to idle */
1013 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1014 if (emp->blink_policy == BLINK_OFF)
1015 led_message |= (1 << 16);
1017 spin_unlock_irqrestore(ap->lock, flags);
1018 ap->ops->transmit_led_message(ap, led_message, 4);
1021 static void ahci_init_sw_activity(struct ata_link *link)
1023 struct ata_port *ap = link->ap;
1024 struct ahci_port_priv *pp = ap->private_data;
1025 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1027 /* init activity stats, setup timer */
1028 emp->saved_activity = emp->activity = 0;
1030 timer_setup(&emp->timer, ahci_sw_activity_blink, 0);
1032 /* check our blink policy and set flag for link if it's enabled */
1033 if (emp->blink_policy)
1034 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1037 int ahci_reset_em(struct ata_host *host)
1039 struct ahci_host_priv *hpriv = host->private_data;
1040 void __iomem *mmio = hpriv->mmio;
1043 em_ctl = readl(mmio + HOST_EM_CTL);
1044 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1047 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1050 EXPORT_SYMBOL_GPL(ahci_reset_em);
1052 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1055 struct ahci_host_priv *hpriv = ap->host->private_data;
1056 struct ahci_port_priv *pp = ap->private_data;
1057 void __iomem *mmio = hpriv->mmio;
1059 u32 message[] = {0, 0};
1060 unsigned long flags;
1062 struct ahci_em_priv *emp;
1064 /* get the slot number from the message */
1065 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1066 if (pmp < EM_MAX_SLOTS)
1067 emp = &pp->em_priv[pmp];
1071 ahci_rpm_get_port(ap);
1072 spin_lock_irqsave(ap->lock, flags);
1075 * if we are still busy transmitting a previous message,
1078 em_ctl = readl(mmio + HOST_EM_CTL);
1079 if (em_ctl & EM_CTL_TM) {
1080 spin_unlock_irqrestore(ap->lock, flags);
1081 ahci_rpm_put_port(ap);
1085 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1087 * create message header - this is all zero except for
1088 * the message size, which is 4 bytes.
1090 message[0] |= (4 << 8);
1092 /* ignore 0:4 of byte zero, fill in port info yourself */
1093 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
1095 /* write message to EM_LOC */
1096 writel(message[0], mmio + hpriv->em_loc);
1097 writel(message[1], mmio + hpriv->em_loc+4);
1100 * tell hardware to transmit the message
1102 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1105 /* save off new led state for port/slot */
1106 emp->led_state = state;
1108 spin_unlock_irqrestore(ap->lock, flags);
1109 ahci_rpm_put_port(ap);
1114 static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1116 struct ahci_port_priv *pp = ap->private_data;
1117 struct ata_link *link;
1118 struct ahci_em_priv *emp;
1121 ata_for_each_link(link, ap, EDGE) {
1122 emp = &pp->em_priv[link->pmp];
1123 rc += sprintf(buf, "%lx\n", emp->led_state);
1128 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1133 struct ahci_port_priv *pp = ap->private_data;
1134 struct ahci_em_priv *emp;
1136 if (kstrtouint(buf, 0, &state) < 0)
1139 /* get the slot number from the message */
1140 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1141 if (pmp < EM_MAX_SLOTS) {
1142 pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
1143 emp = &pp->em_priv[pmp];
1148 /* mask off the activity bits if we are in sw_activity
1149 * mode, user should turn off sw_activity before setting
1150 * activity led through em_message
1152 if (emp->blink_policy)
1153 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1155 return ap->ops->transmit_led_message(ap, state, size);
1158 static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1160 struct ata_link *link = dev->link;
1161 struct ata_port *ap = link->ap;
1162 struct ahci_port_priv *pp = ap->private_data;
1163 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1164 u32 port_led_state = emp->led_state;
1166 /* save the desired Activity LED behavior */
1169 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1171 /* set the LED to OFF */
1172 port_led_state &= EM_MSG_LED_VALUE_OFF;
1173 port_led_state |= (ap->port_no | (link->pmp << 8));
1174 ap->ops->transmit_led_message(ap, port_led_state, 4);
1176 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1177 if (val == BLINK_OFF) {
1178 /* set LED to ON for idle */
1179 port_led_state &= EM_MSG_LED_VALUE_OFF;
1180 port_led_state |= (ap->port_no | (link->pmp << 8));
1181 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
1182 ap->ops->transmit_led_message(ap, port_led_state, 4);
1185 emp->blink_policy = val;
1189 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1191 struct ata_link *link = dev->link;
1192 struct ata_port *ap = link->ap;
1193 struct ahci_port_priv *pp = ap->private_data;
1194 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1196 /* display the saved value of activity behavior for this
1199 return sprintf(buf, "%d\n", emp->blink_policy);
1202 static void ahci_port_clear_pending_irq(struct ata_port *ap)
1204 struct ahci_host_priv *hpriv = ap->host->private_data;
1205 void __iomem *port_mmio = ahci_port_base(ap);
1209 tmp = readl(port_mmio + PORT_SCR_ERR);
1210 dev_dbg(ap->host->dev, "PORT_SCR_ERR 0x%x\n", tmp);
1211 writel(tmp, port_mmio + PORT_SCR_ERR);
1213 /* clear port IRQ */
1214 tmp = readl(port_mmio + PORT_IRQ_STAT);
1215 dev_dbg(ap->host->dev, "PORT_IRQ_STAT 0x%x\n", tmp);
1217 writel(tmp, port_mmio + PORT_IRQ_STAT);
1219 writel(1 << ap->port_no, hpriv->mmio + HOST_IRQ_STAT);
1222 static void ahci_port_init(struct device *dev, struct ata_port *ap,
1223 int port_no, void __iomem *mmio,
1224 void __iomem *port_mmio)
1226 struct ahci_host_priv *hpriv = ap->host->private_data;
1227 const char *emsg = NULL;
1231 /* make sure port is not active */
1232 rc = ahci_deinit_port(ap, &emsg);
1234 dev_warn(dev, "%s (%d)\n", emsg, rc);
1236 ahci_port_clear_pending_irq(ap);
1238 /* mark esata ports */
1239 tmp = readl(port_mmio + PORT_CMD);
1240 if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
1241 ap->pflags |= ATA_PFLAG_EXTERNAL;
1244 void ahci_init_controller(struct ata_host *host)
1246 struct ahci_host_priv *hpriv = host->private_data;
1247 void __iomem *mmio = hpriv->mmio;
1249 void __iomem *port_mmio;
1252 for (i = 0; i < host->n_ports; i++) {
1253 struct ata_port *ap = host->ports[i];
1255 port_mmio = ahci_port_base(ap);
1256 if (ata_port_is_dummy(ap))
1259 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1262 tmp = readl(mmio + HOST_CTL);
1263 dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
1264 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1265 tmp = readl(mmio + HOST_CTL);
1266 dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
1268 EXPORT_SYMBOL_GPL(ahci_init_controller);
1270 static void ahci_dev_config(struct ata_device *dev)
1272 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1274 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1275 dev->max_sectors = 255;
1277 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1281 unsigned int ahci_dev_classify(struct ata_port *ap)
1283 void __iomem *port_mmio = ahci_port_base(ap);
1284 struct ata_taskfile tf;
1287 tmp = readl(port_mmio + PORT_SIG);
1288 tf.lbah = (tmp >> 24) & 0xff;
1289 tf.lbam = (tmp >> 16) & 0xff;
1290 tf.lbal = (tmp >> 8) & 0xff;
1291 tf.nsect = (tmp) & 0xff;
1293 return ata_dev_classify(&tf);
1295 EXPORT_SYMBOL_GPL(ahci_dev_classify);
1297 void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1300 dma_addr_t cmd_tbl_dma;
1302 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1304 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1305 pp->cmd_slot[tag].status = 0;
1306 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1307 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1309 EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
1311 int ahci_kick_engine(struct ata_port *ap)
1313 void __iomem *port_mmio = ahci_port_base(ap);
1314 struct ahci_host_priv *hpriv = ap->host->private_data;
1315 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1320 rc = hpriv->stop_engine(ap);
1325 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1327 busy = status & (ATA_BUSY | ATA_DRQ);
1328 if (!busy && !sata_pmp_attached(ap)) {
1333 if (!(hpriv->cap & HOST_CAP_CLO)) {
1339 tmp = readl(port_mmio + PORT_CMD);
1340 tmp |= PORT_CMD_CLO;
1341 writel(tmp, port_mmio + PORT_CMD);
1344 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
1345 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1346 if (tmp & PORT_CMD_CLO)
1349 /* restart engine */
1351 hpriv->start_engine(ap);
1354 EXPORT_SYMBOL_GPL(ahci_kick_engine);
1356 static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1357 struct ata_taskfile *tf, int is_cmd, u16 flags,
1358 unsigned long timeout_msec)
1360 const u32 cmd_fis_len = 5; /* five dwords */
1361 struct ahci_port_priv *pp = ap->private_data;
1362 void __iomem *port_mmio = ahci_port_base(ap);
1363 u8 *fis = pp->cmd_tbl;
1366 /* prep the command */
1367 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1368 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1370 /* set port value for softreset of Port Multiplier */
1371 if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1372 tmp = readl(port_mmio + PORT_FBS);
1373 tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1374 tmp |= pmp << PORT_FBS_DEV_OFFSET;
1375 writel(tmp, port_mmio + PORT_FBS);
1376 pp->fbs_last_dev = pmp;
1380 writel(1, port_mmio + PORT_CMD_ISSUE);
1383 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1384 0x1, 0x1, 1, timeout_msec);
1386 ahci_kick_engine(ap);
1390 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1395 int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1396 int pmp, unsigned long deadline,
1397 int (*check_ready)(struct ata_link *link))
1399 struct ata_port *ap = link->ap;
1400 struct ahci_host_priv *hpriv = ap->host->private_data;
1401 struct ahci_port_priv *pp = ap->private_data;
1402 const char *reason = NULL;
1403 unsigned long now, msecs;
1404 struct ata_taskfile tf;
1405 bool fbs_disabled = false;
1410 /* prepare for SRST (AHCI-1.1 10.4.1) */
1411 rc = ahci_kick_engine(ap);
1412 if (rc && rc != -EOPNOTSUPP)
1413 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
1416 * According to AHCI-1.2 9.3.9: if FBS is enable, software shall
1417 * clear PxFBS.EN to '0' prior to issuing software reset to devices
1418 * that is attached to port multiplier.
1420 if (!ata_is_host_link(link) && pp->fbs_enabled) {
1421 ahci_disable_fbs(ap);
1422 fbs_disabled = true;
1425 ata_tf_init(link->device, &tf);
1427 /* issue the first H2D Register FIS */
1430 if (time_after(deadline, now))
1431 msecs = jiffies_to_msecs(deadline - now);
1434 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1435 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1437 reason = "1st FIS failed";
1441 /* spec says at least 5us, but be generous and sleep for 1ms */
1444 /* issue the second H2D Register FIS */
1445 tf.ctl &= ~ATA_SRST;
1446 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1448 /* wait for link to become ready */
1449 rc = ata_wait_after_reset(link, deadline, check_ready);
1450 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1452 * Workaround for cases where link online status can't
1453 * be trusted. Treat device readiness timeout as link
1456 ata_link_info(link, "device not ready, treating as offline\n");
1457 *class = ATA_DEV_NONE;
1459 /* link occupied, -ENODEV too is an error */
1460 reason = "device not ready";
1463 *class = ahci_dev_classify(ap);
1465 /* re-enable FBS if disabled before */
1467 ahci_enable_fbs(ap);
1469 DPRINTK("EXIT, class=%u\n", *class);
1473 ata_link_err(link, "softreset failed (%s)\n", reason);
1477 int ahci_check_ready(struct ata_link *link)
1479 void __iomem *port_mmio = ahci_port_base(link->ap);
1480 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1482 return ata_check_ready(status);
1484 EXPORT_SYMBOL_GPL(ahci_check_ready);
1486 static int ahci_softreset(struct ata_link *link, unsigned int *class,
1487 unsigned long deadline)
1489 int pmp = sata_srst_pmp(link);
1493 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1495 EXPORT_SYMBOL_GPL(ahci_do_softreset);
1497 static int ahci_bad_pmp_check_ready(struct ata_link *link)
1499 void __iomem *port_mmio = ahci_port_base(link->ap);
1500 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1501 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1504 * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1505 * which can save timeout delay.
1507 if (irq_status & PORT_IRQ_BAD_PMP)
1510 return ata_check_ready(status);
1513 static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1514 unsigned long deadline)
1516 struct ata_port *ap = link->ap;
1517 void __iomem *port_mmio = ahci_port_base(ap);
1518 int pmp = sata_srst_pmp(link);
1524 rc = ahci_do_softreset(link, class, pmp, deadline,
1525 ahci_bad_pmp_check_ready);
1528 * Soft reset fails with IPMS set when PMP is enabled but
1529 * SATA HDD/ODD is connected to SATA port, do soft reset
1533 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1534 if (irq_sts & PORT_IRQ_BAD_PMP) {
1536 "applying PMP SRST workaround "
1538 rc = ahci_do_softreset(link, class, 0, deadline,
1546 int ahci_do_hardreset(struct ata_link *link, unsigned int *class,
1547 unsigned long deadline, bool *online)
1549 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1550 struct ata_port *ap = link->ap;
1551 struct ahci_port_priv *pp = ap->private_data;
1552 struct ahci_host_priv *hpriv = ap->host->private_data;
1553 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1554 struct ata_taskfile tf;
1559 hpriv->stop_engine(ap);
1561 /* clear D2H reception area to properly wait for D2H FIS */
1562 ata_tf_init(link->device, &tf);
1563 tf.command = ATA_BUSY;
1564 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1566 ahci_port_clear_pending_irq(ap);
1568 rc = sata_link_hardreset(link, timing, deadline, online,
1571 hpriv->start_engine(ap);
1574 *class = ahci_dev_classify(ap);
1576 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1579 EXPORT_SYMBOL_GPL(ahci_do_hardreset);
1581 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1582 unsigned long deadline)
1586 return ahci_do_hardreset(link, class, deadline, &online);
1589 static void ahci_postreset(struct ata_link *link, unsigned int *class)
1591 struct ata_port *ap = link->ap;
1592 void __iomem *port_mmio = ahci_port_base(ap);
1595 ata_std_postreset(link, class);
1597 /* Make sure port's ATAPI bit is set appropriately */
1598 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1599 if (*class == ATA_DEV_ATAPI)
1600 new_tmp |= PORT_CMD_ATAPI;
1602 new_tmp &= ~PORT_CMD_ATAPI;
1603 if (new_tmp != tmp) {
1604 writel(new_tmp, port_mmio + PORT_CMD);
1605 readl(port_mmio + PORT_CMD); /* flush */
1609 static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1611 struct scatterlist *sg;
1612 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1618 * Next, the S/G list.
1620 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1621 dma_addr_t addr = sg_dma_address(sg);
1622 u32 sg_len = sg_dma_len(sg);
1624 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1625 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1626 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1632 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1634 struct ata_port *ap = qc->ap;
1635 struct ahci_port_priv *pp = ap->private_data;
1637 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1638 return ata_std_qc_defer(qc);
1640 return sata_pmp_qc_defer_cmd_switch(qc);
1643 static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc)
1645 struct ata_port *ap = qc->ap;
1646 struct ahci_port_priv *pp = ap->private_data;
1647 int is_atapi = ata_is_atapi(qc->tf.protocol);
1650 const u32 cmd_fis_len = 5; /* five dwords */
1651 unsigned int n_elem;
1654 * Fill in command table information. First, the header,
1655 * a SATA Register - Host to Device command FIS.
1657 cmd_tbl = pp->cmd_tbl + qc->hw_tag * AHCI_CMD_TBL_SZ;
1659 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1661 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1662 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1666 if (qc->flags & ATA_QCFLAG_DMAMAP)
1667 n_elem = ahci_fill_sg(qc, cmd_tbl);
1670 * Fill in command slot information.
1672 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1673 if (qc->tf.flags & ATA_TFLAG_WRITE)
1674 opts |= AHCI_CMD_WRITE;
1676 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1678 ahci_fill_cmd_slot(pp, qc->hw_tag, opts);
1683 static void ahci_fbs_dec_intr(struct ata_port *ap)
1685 struct ahci_port_priv *pp = ap->private_data;
1686 void __iomem *port_mmio = ahci_port_base(ap);
1687 u32 fbs = readl(port_mmio + PORT_FBS);
1691 BUG_ON(!pp->fbs_enabled);
1693 /* time to wait for DEC is not specified by AHCI spec,
1694 * add a retry loop for safety.
1696 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1697 fbs = readl(port_mmio + PORT_FBS);
1698 while ((fbs & PORT_FBS_DEC) && retries--) {
1700 fbs = readl(port_mmio + PORT_FBS);
1703 if (fbs & PORT_FBS_DEC)
1704 dev_err(ap->host->dev, "failed to clear device error\n");
1707 static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1709 struct ahci_host_priv *hpriv = ap->host->private_data;
1710 struct ahci_port_priv *pp = ap->private_data;
1711 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1712 struct ata_link *link = NULL;
1713 struct ata_queued_cmd *active_qc;
1714 struct ata_eh_info *active_ehi;
1715 bool fbs_need_dec = false;
1718 /* determine active link with error */
1719 if (pp->fbs_enabled) {
1720 void __iomem *port_mmio = ahci_port_base(ap);
1721 u32 fbs = readl(port_mmio + PORT_FBS);
1722 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1724 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
1725 link = &ap->pmp_link[pmp];
1726 fbs_need_dec = true;
1730 ata_for_each_link(link, ap, EDGE)
1731 if (ata_link_active(link))
1737 active_qc = ata_qc_from_tag(ap, link->active_tag);
1738 active_ehi = &link->eh_info;
1740 /* record irq stat */
1741 ata_ehi_clear_desc(host_ehi);
1742 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1744 /* AHCI needs SError cleared; otherwise, it might lock up */
1745 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1746 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1747 host_ehi->serror |= serror;
1749 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1750 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1751 irq_stat &= ~PORT_IRQ_IF_ERR;
1753 if (irq_stat & PORT_IRQ_TF_ERR) {
1754 /* If qc is active, charge it; otherwise, the active
1755 * link. There's no active qc on NCQ errors. It will
1756 * be determined by EH by reading log page 10h.
1759 active_qc->err_mask |= AC_ERR_DEV;
1761 active_ehi->err_mask |= AC_ERR_DEV;
1763 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1764 host_ehi->serror &= ~SERR_INTERNAL;
1767 if (irq_stat & PORT_IRQ_UNK_FIS) {
1768 u32 *unk = pp->rx_fis + RX_FIS_UNK;
1770 active_ehi->err_mask |= AC_ERR_HSM;
1771 active_ehi->action |= ATA_EH_RESET;
1772 ata_ehi_push_desc(active_ehi,
1773 "unknown FIS %08x %08x %08x %08x" ,
1774 unk[0], unk[1], unk[2], unk[3]);
1777 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1778 active_ehi->err_mask |= AC_ERR_HSM;
1779 active_ehi->action |= ATA_EH_RESET;
1780 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1783 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1784 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1785 host_ehi->action |= ATA_EH_RESET;
1786 ata_ehi_push_desc(host_ehi, "host bus error");
1789 if (irq_stat & PORT_IRQ_IF_ERR) {
1791 active_ehi->err_mask |= AC_ERR_DEV;
1793 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1794 host_ehi->action |= ATA_EH_RESET;
1797 ata_ehi_push_desc(host_ehi, "interface fatal error");
1800 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1801 ata_ehi_hotplugged(host_ehi);
1802 ata_ehi_push_desc(host_ehi, "%s",
1803 irq_stat & PORT_IRQ_CONNECT ?
1804 "connection status changed" : "PHY RDY changed");
1807 /* okay, let's hand over to EH */
1809 if (irq_stat & PORT_IRQ_FREEZE)
1810 ata_port_freeze(ap);
1811 else if (fbs_need_dec) {
1812 ata_link_abort(link);
1813 ahci_fbs_dec_intr(ap);
1818 static void ahci_handle_port_interrupt(struct ata_port *ap,
1819 void __iomem *port_mmio, u32 status)
1821 struct ata_eh_info *ehi = &ap->link.eh_info;
1822 struct ahci_port_priv *pp = ap->private_data;
1823 struct ahci_host_priv *hpriv = ap->host->private_data;
1824 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1828 /* ignore BAD_PMP while resetting */
1829 if (unlikely(resetting))
1830 status &= ~PORT_IRQ_BAD_PMP;
1832 if (sata_lpm_ignore_phy_events(&ap->link)) {
1833 status &= ~PORT_IRQ_PHYRDY;
1834 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
1837 if (unlikely(status & PORT_IRQ_ERROR)) {
1838 ahci_error_intr(ap, status);
1842 if (status & PORT_IRQ_SDB_FIS) {
1843 /* If SNotification is available, leave notification
1844 * handling to sata_async_notification(). If not,
1845 * emulate it by snooping SDB FIS RX area.
1847 * Snooping FIS RX area is probably cheaper than
1848 * poking SNotification but some constrollers which
1849 * implement SNotification, ICH9 for example, don't
1850 * store AN SDB FIS into receive area.
1852 if (hpriv->cap & HOST_CAP_SNTF)
1853 sata_async_notification(ap);
1855 /* If the 'N' bit in word 0 of the FIS is set,
1856 * we just received asynchronous notification.
1857 * Tell libata about it.
1859 * Lack of SNotification should not appear in
1860 * ahci 1.2, so the workaround is unnecessary
1861 * when FBS is enabled.
1863 if (pp->fbs_enabled)
1866 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1867 u32 f0 = le32_to_cpu(f[0]);
1869 sata_async_notification(ap);
1874 /* pp->active_link is not reliable once FBS is enabled, both
1875 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1876 * NCQ and non-NCQ commands may be in flight at the same time.
1878 if (pp->fbs_enabled) {
1879 if (ap->qc_active) {
1880 qc_active = readl(port_mmio + PORT_SCR_ACT);
1881 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1884 /* pp->active_link is valid iff any command is in flight */
1885 if (ap->qc_active && pp->active_link->sactive)
1886 qc_active = readl(port_mmio + PORT_SCR_ACT);
1888 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1892 rc = ata_qc_complete_multiple(ap, qc_active);
1894 /* while resetting, invalid completions are expected */
1895 if (unlikely(rc < 0 && !resetting)) {
1896 ehi->err_mask |= AC_ERR_HSM;
1897 ehi->action |= ATA_EH_RESET;
1898 ata_port_freeze(ap);
1902 static void ahci_port_intr(struct ata_port *ap)
1904 void __iomem *port_mmio = ahci_port_base(ap);
1907 status = readl(port_mmio + PORT_IRQ_STAT);
1908 writel(status, port_mmio + PORT_IRQ_STAT);
1910 ahci_handle_port_interrupt(ap, port_mmio, status);
1913 static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
1915 struct ata_port *ap = dev_instance;
1916 void __iomem *port_mmio = ahci_port_base(ap);
1919 status = readl(port_mmio + PORT_IRQ_STAT);
1920 writel(status, port_mmio + PORT_IRQ_STAT);
1922 spin_lock(ap->lock);
1923 ahci_handle_port_interrupt(ap, port_mmio, status);
1924 spin_unlock(ap->lock);
1929 u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
1931 unsigned int i, handled = 0;
1933 for (i = 0; i < host->n_ports; i++) {
1934 struct ata_port *ap;
1936 if (!(irq_masked & (1 << i)))
1939 ap = host->ports[i];
1943 if (ata_ratelimit())
1945 "interrupt on disabled port %u\n", i);
1953 EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
1955 static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1957 struct ata_host *host = dev_instance;
1958 struct ahci_host_priv *hpriv;
1959 unsigned int rc = 0;
1961 u32 irq_stat, irq_masked;
1963 hpriv = host->private_data;
1966 /* sigh. 0xffffffff is a valid return from h/w */
1967 irq_stat = readl(mmio + HOST_IRQ_STAT);
1971 irq_masked = irq_stat & hpriv->port_map;
1973 spin_lock(&host->lock);
1975 rc = ahci_handle_port_intr(host, irq_masked);
1977 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1978 * it should be cleared after all the port events are cleared;
1979 * otherwise, it will raise a spurious interrupt after each
1980 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1983 * Also, use the unmasked value to clear interrupt as spurious
1984 * pending event on a dummy port might cause screaming IRQ.
1986 writel(irq_stat, mmio + HOST_IRQ_STAT);
1988 spin_unlock(&host->lock);
1990 return IRQ_RETVAL(rc);
1993 unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1995 struct ata_port *ap = qc->ap;
1996 void __iomem *port_mmio = ahci_port_base(ap);
1997 struct ahci_port_priv *pp = ap->private_data;
1999 /* Keep track of the currently active link. It will be used
2000 * in completion path to determine whether NCQ phase is in
2003 pp->active_link = qc->dev->link;
2005 if (ata_is_ncq(qc->tf.protocol))
2006 writel(1 << qc->hw_tag, port_mmio + PORT_SCR_ACT);
2008 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
2009 u32 fbs = readl(port_mmio + PORT_FBS);
2010 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
2011 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
2012 writel(fbs, port_mmio + PORT_FBS);
2013 pp->fbs_last_dev = qc->dev->link->pmp;
2016 writel(1 << qc->hw_tag, port_mmio + PORT_CMD_ISSUE);
2018 ahci_sw_activity(qc->dev->link);
2022 EXPORT_SYMBOL_GPL(ahci_qc_issue);
2024 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2026 struct ahci_port_priv *pp = qc->ap->private_data;
2027 u8 *rx_fis = pp->rx_fis;
2029 if (pp->fbs_enabled)
2030 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
2033 * After a successful execution of an ATA PIO data-in command,
2034 * the device doesn't send D2H Reg FIS to update the TF and
2035 * the host should take TF and E_Status from the preceding PIO
2038 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
2039 !(qc->flags & ATA_QCFLAG_FAILED)) {
2040 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
2041 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
2043 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
2048 static void ahci_freeze(struct ata_port *ap)
2050 void __iomem *port_mmio = ahci_port_base(ap);
2053 writel(0, port_mmio + PORT_IRQ_MASK);
2056 static void ahci_thaw(struct ata_port *ap)
2058 struct ahci_host_priv *hpriv = ap->host->private_data;
2059 void __iomem *mmio = hpriv->mmio;
2060 void __iomem *port_mmio = ahci_port_base(ap);
2062 struct ahci_port_priv *pp = ap->private_data;
2065 tmp = readl(port_mmio + PORT_IRQ_STAT);
2066 writel(tmp, port_mmio + PORT_IRQ_STAT);
2067 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2069 /* turn IRQ back on */
2070 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2073 void ahci_error_handler(struct ata_port *ap)
2075 struct ahci_host_priv *hpriv = ap->host->private_data;
2077 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2078 /* restart engine */
2079 hpriv->stop_engine(ap);
2080 hpriv->start_engine(ap);
2083 sata_pmp_error_handler(ap);
2085 if (!ata_dev_enabled(ap->link.device))
2086 hpriv->stop_engine(ap);
2088 EXPORT_SYMBOL_GPL(ahci_error_handler);
2090 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2092 struct ata_port *ap = qc->ap;
2094 /* make DMA engine forget about the failed command */
2095 if (qc->flags & ATA_QCFLAG_FAILED)
2096 ahci_kick_engine(ap);
2099 static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2101 struct ahci_host_priv *hpriv = ap->host->private_data;
2102 void __iomem *port_mmio = ahci_port_base(ap);
2103 struct ata_device *dev = ap->link.device;
2104 u32 devslp, dm, dito, mdat, deto, dito_conf;
2106 unsigned int err_mask;
2108 devslp = readl(port_mmio + PORT_DEVSLP);
2109 if (!(devslp & PORT_DEVSLP_DSP)) {
2110 dev_info(ap->host->dev, "port does not support device sleep\n");
2114 /* disable device sleep */
2116 if (devslp & PORT_DEVSLP_ADSE) {
2117 writel(devslp & ~PORT_DEVSLP_ADSE,
2118 port_mmio + PORT_DEVSLP);
2119 err_mask = ata_dev_set_feature(dev,
2120 SETFEATURES_SATA_DISABLE,
2122 if (err_mask && err_mask != AC_ERR_DEV)
2123 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2128 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2129 dito = devslp_idle_timeout / (dm + 1);
2133 dito_conf = (devslp >> PORT_DEVSLP_DITO_OFFSET) & 0x3FF;
2135 /* device sleep was already enabled and same dito */
2136 if ((devslp & PORT_DEVSLP_ADSE) && (dito_conf == dito))
2139 /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
2140 rc = hpriv->stop_engine(ap);
2144 /* Use the nominal value 10 ms if the read MDAT is zero,
2145 * the nominal value of DETO is 20 ms.
2147 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
2148 ATA_LOG_DEVSLP_VALID_MASK) {
2149 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
2150 ATA_LOG_DEVSLP_MDAT_MASK;
2153 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
2161 /* Make dito, mdat, deto bits to 0s */
2162 devslp &= ~GENMASK_ULL(24, 2);
2163 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2164 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2165 (deto << PORT_DEVSLP_DETO_OFFSET) |
2167 writel(devslp, port_mmio + PORT_DEVSLP);
2169 hpriv->start_engine(ap);
2171 /* enable device sleep feature for the drive */
2172 err_mask = ata_dev_set_feature(dev,
2173 SETFEATURES_SATA_ENABLE,
2175 if (err_mask && err_mask != AC_ERR_DEV)
2176 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2179 static void ahci_enable_fbs(struct ata_port *ap)
2181 struct ahci_host_priv *hpriv = ap->host->private_data;
2182 struct ahci_port_priv *pp = ap->private_data;
2183 void __iomem *port_mmio = ahci_port_base(ap);
2187 if (!pp->fbs_supported)
2190 fbs = readl(port_mmio + PORT_FBS);
2191 if (fbs & PORT_FBS_EN) {
2192 pp->fbs_enabled = true;
2193 pp->fbs_last_dev = -1; /* initialization */
2197 rc = hpriv->stop_engine(ap);
2201 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2202 fbs = readl(port_mmio + PORT_FBS);
2203 if (fbs & PORT_FBS_EN) {
2204 dev_info(ap->host->dev, "FBS is enabled\n");
2205 pp->fbs_enabled = true;
2206 pp->fbs_last_dev = -1; /* initialization */
2208 dev_err(ap->host->dev, "Failed to enable FBS\n");
2210 hpriv->start_engine(ap);
2213 static void ahci_disable_fbs(struct ata_port *ap)
2215 struct ahci_host_priv *hpriv = ap->host->private_data;
2216 struct ahci_port_priv *pp = ap->private_data;
2217 void __iomem *port_mmio = ahci_port_base(ap);
2221 if (!pp->fbs_supported)
2224 fbs = readl(port_mmio + PORT_FBS);
2225 if ((fbs & PORT_FBS_EN) == 0) {
2226 pp->fbs_enabled = false;
2230 rc = hpriv->stop_engine(ap);
2234 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2235 fbs = readl(port_mmio + PORT_FBS);
2236 if (fbs & PORT_FBS_EN)
2237 dev_err(ap->host->dev, "Failed to disable FBS\n");
2239 dev_info(ap->host->dev, "FBS is disabled\n");
2240 pp->fbs_enabled = false;
2243 hpriv->start_engine(ap);
2246 static void ahci_pmp_attach(struct ata_port *ap)
2248 void __iomem *port_mmio = ahci_port_base(ap);
2249 struct ahci_port_priv *pp = ap->private_data;
2252 cmd = readl(port_mmio + PORT_CMD);
2253 cmd |= PORT_CMD_PMP;
2254 writel(cmd, port_mmio + PORT_CMD);
2256 ahci_enable_fbs(ap);
2258 pp->intr_mask |= PORT_IRQ_BAD_PMP;
2261 * We must not change the port interrupt mask register if the
2262 * port is marked frozen, the value in pp->intr_mask will be
2263 * restored later when the port is thawed.
2265 * Note that during initialization, the port is marked as
2266 * frozen since the irq handler is not yet registered.
2268 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2269 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2272 static void ahci_pmp_detach(struct ata_port *ap)
2274 void __iomem *port_mmio = ahci_port_base(ap);
2275 struct ahci_port_priv *pp = ap->private_data;
2278 ahci_disable_fbs(ap);
2280 cmd = readl(port_mmio + PORT_CMD);
2281 cmd &= ~PORT_CMD_PMP;
2282 writel(cmd, port_mmio + PORT_CMD);
2284 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2286 /* see comment above in ahci_pmp_attach() */
2287 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2288 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2291 int ahci_port_resume(struct ata_port *ap)
2293 ahci_rpm_get_port(ap);
2296 ahci_start_port(ap);
2298 if (sata_pmp_attached(ap))
2299 ahci_pmp_attach(ap);
2301 ahci_pmp_detach(ap);
2305 EXPORT_SYMBOL_GPL(ahci_port_resume);
2308 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2310 const char *emsg = NULL;
2313 rc = ahci_deinit_port(ap, &emsg);
2315 ahci_power_down(ap);
2317 ata_port_err(ap, "%s (%d)\n", emsg, rc);
2318 ata_port_freeze(ap);
2321 ahci_rpm_put_port(ap);
2326 static int ahci_port_start(struct ata_port *ap)
2328 struct ahci_host_priv *hpriv = ap->host->private_data;
2329 struct device *dev = ap->host->dev;
2330 struct ahci_port_priv *pp;
2333 size_t dma_sz, rx_fis_sz;
2335 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2339 if (ap->host->n_ports > 1) {
2340 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2341 if (!pp->irq_desc) {
2342 devm_kfree(dev, pp);
2345 snprintf(pp->irq_desc, 8,
2346 "%s%d", dev_driver_string(dev), ap->port_no);
2349 /* check FBS capability */
2350 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2351 void __iomem *port_mmio = ahci_port_base(ap);
2352 u32 cmd = readl(port_mmio + PORT_CMD);
2353 if (cmd & PORT_CMD_FBSCP)
2354 pp->fbs_supported = true;
2355 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
2356 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2358 pp->fbs_supported = true;
2360 dev_warn(dev, "port %d is not capable of FBS\n",
2364 if (pp->fbs_supported) {
2365 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2366 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2368 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2369 rx_fis_sz = AHCI_RX_FIS_SZ;
2372 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2377 * First item in chunk of DMA memory: 32-slot command table,
2378 * 32 bytes each in size
2381 pp->cmd_slot_dma = mem_dma;
2383 mem += AHCI_CMD_SLOT_SZ;
2384 mem_dma += AHCI_CMD_SLOT_SZ;
2387 * Second item: Received-FIS area
2390 pp->rx_fis_dma = mem_dma;
2393 mem_dma += rx_fis_sz;
2396 * Third item: data area for storing a single command
2397 * and its scatter-gather table
2400 pp->cmd_tbl_dma = mem_dma;
2403 * Save off initial list of interrupts to be enabled.
2404 * This could be changed later
2406 pp->intr_mask = DEF_PORT_IRQ;
2409 * Switch to per-port locking in case each port has its own MSI vector.
2411 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2412 spin_lock_init(&pp->lock);
2413 ap->lock = &pp->lock;
2416 ap->private_data = pp;
2418 /* engage engines, captain */
2419 return ahci_port_resume(ap);
2422 static void ahci_port_stop(struct ata_port *ap)
2424 const char *emsg = NULL;
2425 struct ahci_host_priv *hpriv = ap->host->private_data;
2426 void __iomem *host_mmio = hpriv->mmio;
2429 /* de-initialize port */
2430 rc = ahci_deinit_port(ap, &emsg);
2432 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
2435 * Clear GHC.IS to prevent stuck INTx after disabling MSI and
2438 writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
2440 ahci_rpm_put_port(ap);
2443 void ahci_print_info(struct ata_host *host, const char *scc_s)
2445 struct ahci_host_priv *hpriv = host->private_data;
2446 u32 vers, cap, cap2, impl, speed;
2447 const char *speed_s;
2449 vers = hpriv->version;
2452 impl = hpriv->port_map;
2454 speed = (cap >> 20) & 0xf;
2457 else if (speed == 2)
2459 else if (speed == 3)
2465 "AHCI %02x%02x.%02x%02x "
2466 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2469 (vers >> 24) & 0xff,
2470 (vers >> 16) & 0xff,
2474 ((cap >> 8) & 0x1f) + 1,
2488 cap & HOST_CAP_64 ? "64bit " : "",
2489 cap & HOST_CAP_NCQ ? "ncq " : "",
2490 cap & HOST_CAP_SNTF ? "sntf " : "",
2491 cap & HOST_CAP_MPS ? "ilck " : "",
2492 cap & HOST_CAP_SSS ? "stag " : "",
2493 cap & HOST_CAP_ALPM ? "pm " : "",
2494 cap & HOST_CAP_LED ? "led " : "",
2495 cap & HOST_CAP_CLO ? "clo " : "",
2496 cap & HOST_CAP_ONLY ? "only " : "",
2497 cap & HOST_CAP_PMP ? "pmp " : "",
2498 cap & HOST_CAP_FBS ? "fbs " : "",
2499 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2500 cap & HOST_CAP_SSC ? "slum " : "",
2501 cap & HOST_CAP_PART ? "part " : "",
2502 cap & HOST_CAP_CCC ? "ccc " : "",
2503 cap & HOST_CAP_EMS ? "ems " : "",
2504 cap & HOST_CAP_SXS ? "sxs " : "",
2505 cap2 & HOST_CAP2_DESO ? "deso " : "",
2506 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2507 cap2 & HOST_CAP2_SDS ? "sds " : "",
2508 cap2 & HOST_CAP2_APST ? "apst " : "",
2509 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2510 cap2 & HOST_CAP2_BOH ? "boh " : ""
2513 EXPORT_SYMBOL_GPL(ahci_print_info);
2515 void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2516 struct ata_port_info *pi)
2519 void __iomem *mmio = hpriv->mmio;
2520 u32 em_loc = readl(mmio + HOST_EM_LOC);
2521 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2523 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2526 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2530 hpriv->em_loc = ((em_loc >> 16) * 4);
2531 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
2532 hpriv->em_msg_type = messages;
2533 pi->flags |= ATA_FLAG_EM;
2534 if (!(em_ctl & EM_CTL_ALHD))
2535 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2538 EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2540 static int ahci_host_activate_multi_irqs(struct ata_host *host,
2541 struct scsi_host_template *sht)
2543 struct ahci_host_priv *hpriv = host->private_data;
2546 rc = ata_host_start(host);
2550 * Requests IRQs according to AHCI-1.1 when multiple MSIs were
2551 * allocated. That is one MSI per port, starting from @irq.
2553 for (i = 0; i < host->n_ports; i++) {
2554 struct ahci_port_priv *pp = host->ports[i]->private_data;
2555 int irq = hpriv->get_irq_vector(host, i);
2557 /* Do not receive interrupts sent by dummy ports */
2563 rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2564 0, pp->irq_desc, host->ports[i]);
2568 ata_port_desc(host->ports[i], "irq %d", irq);
2571 return ata_host_register(host, sht);
2575 * ahci_host_activate - start AHCI host, request IRQs and register it
2576 * @host: target ATA host
2577 * @sht: scsi_host_template to use when registering the host
2580 * Inherited from calling layer (may sleep).
2583 * 0 on success, -errno otherwise.
2585 int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
2587 struct ahci_host_priv *hpriv = host->private_data;
2588 int irq = hpriv->irq;
2591 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2592 if (hpriv->irq_handler &&
2593 hpriv->irq_handler != ahci_single_level_irq_intr)
2595 "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
2596 if (!hpriv->get_irq_vector) {
2598 "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
2602 rc = ahci_host_activate_multi_irqs(host, sht);
2604 rc = ata_host_activate(host, irq, hpriv->irq_handler,
2611 EXPORT_SYMBOL_GPL(ahci_host_activate);
2613 MODULE_AUTHOR("Jeff Garzik");
2614 MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2615 MODULE_LICENSE("GPL");