35 #include <linux/kernel.h>
37 #include <linux/module.h>
43 #include <linux/device.h>
50 static int ahci_skip_host_reset;
55 MODULE_PARM_DESC(skip_host_reset,
"skip global host reset (0=don't skip, 1=skip)");
58 MODULE_PARM_DESC(ignore_sss,
"Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
74 static int ahci_port_start(
struct ata_port *ap);
75 static void ahci_port_stop(
struct ata_port *ap);
78 static void ahci_freeze(
struct ata_port *ap);
79 static void ahci_thaw(
struct ata_port *ap);
80 static void ahci_set_aggressive_devslp(
struct ata_port *ap,
bool sleep);
81 static void ahci_enable_fbs(
struct ata_port *ap);
82 static void ahci_disable_fbs(
struct ata_port *ap);
83 static void ahci_pmp_attach(
struct ata_port *ap);
84 static void ahci_pmp_detach(
struct ata_port *ap);
85 static int ahci_softreset(
struct ata_link *
link,
unsigned int *
class,
86 unsigned long deadline);
87 static int ahci_pmp_retry_softreset(
struct ata_link *
link,
unsigned int *
class,
88 unsigned long deadline);
89 static int ahci_hardreset(
struct ata_link *
link,
unsigned int *
class,
90 unsigned long deadline);
91 static void ahci_postreset(
struct ata_link *
link,
unsigned int *
class);
92 static void ahci_error_handler(
struct ata_port *ap);
101 static void ahci_init_sw_activity(
struct ata_link *
link);
124 ahci_read_em_buffer, ahci_store_em_buffer);
131 &dev_attr_ahci_host_caps,
132 &dev_attr_ahci_host_cap2,
133 &dev_attr_ahci_host_version,
134 &dev_attr_ahci_port_cmd,
136 &dev_attr_em_message_supported,
151 .qc_defer = ahci_pmp_qc_defer,
152 .qc_prep = ahci_qc_prep,
153 .qc_issue = ahci_qc_issue,
154 .qc_fill_rtf = ahci_qc_fill_rtf,
156 .freeze = ahci_freeze,
158 .softreset = ahci_softreset,
159 .hardreset = ahci_hardreset,
160 .postreset = ahci_postreset,
161 .pmp_softreset = ahci_softreset,
162 .error_handler = ahci_error_handler,
163 .post_internal_cmd = ahci_post_internal_cmd,
164 .dev_config = ahci_dev_config,
166 .scr_read = ahci_scr_read,
167 .scr_write = ahci_scr_write,
168 .pmp_attach = ahci_pmp_attach,
169 .pmp_detach = ahci_pmp_detach,
171 .set_lpm = ahci_set_lpm,
172 .em_show = ahci_led_show,
173 .em_store = ahci_led_store,
174 .sw_activity_show = ahci_activity_show,
175 .sw_activity_store = ahci_activity_store,
177 .port_suspend = ahci_port_suspend,
180 .port_start = ahci_port_start,
181 .port_stop = ahci_port_stop,
187 .softreset = ahci_pmp_retry_softreset,
196 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
202 static void ahci_enable_ahci(
void __iomem *mmio)
215 for (i = 0; i < 5; i++) {
219 if (tmp & HOST_AHCI_EN)
231 struct ata_port *ap = ata_shost_to_port(shost);
241 struct ata_port *ap = ata_shost_to_port(shost);
251 struct ata_port *ap = ata_shost_to_port(shost);
262 struct ata_port *ap = ata_shost_to_port(shost);
263 void __iomem *port_mmio = ahci_port_base(ap);
272 struct ata_port *ap = ata_shost_to_port(shost);
286 spin_unlock_irqrestore(ap->
lock, flags);
291 spin_unlock_irqrestore(ap->
lock, flags);
302 if (printk_ratelimit())
304 "EM read buffer size too large: "
305 "buffer size %u, page size %lu\n",
310 for (i = 0; i <
count; i += 4) {
311 msg =
readl(em_mmio + i);
313 buf[i + 1] = (msg >> 8) & 0xff;
314 buf[i + 2] = (msg >> 16) & 0xff;
315 buf[i + 3] = (msg >> 24) & 0xff;
318 spin_unlock_irqrestore(ap->
lock, flags);
325 const char *buf,
size_t size)
328 struct ata_port *ap = ata_shost_to_port(shost);
347 spin_unlock_irqrestore(ap->
lock, flags);
351 for (i = 0; i <
size; i += 4) {
352 msg = msg_buf[
i] | msg_buf[i + 1] << 8 |
353 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
359 spin_unlock_irqrestore(ap->
lock, flags);
368 struct ata_port *ap = ata_shost_to_port(shost);
375 return sprintf(buf,
"%s%s%s%s\n",
401 unsigned int force_port_map,
402 unsigned int mask_port_map)
409 ahci_enable_ahci(mmio);
419 if ((vers >> 16) > 1 ||
420 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
427 dev_info(dev,
"controller can't do 64bit DMA, forcing 32bit\n");
432 dev_info(dev,
"controller can't do NCQ, turning off CAP_NCQ\n");
433 cap &= ~HOST_CAP_NCQ;
437 dev_info(dev,
"controller can do NCQ, turning on CAP_NCQ\n");
442 dev_info(dev,
"controller can't do PMP, turning off CAP_PMP\n");
443 cap &= ~HOST_CAP_PMP;
448 "controller can't do SNTF, turning off CAP_SNTF\n");
449 cap &= ~HOST_CAP_SNTF;
453 dev_info(dev,
"controller can do FBS, turning on CAP_FBS\n");
457 if (force_port_map && port_map != force_port_map) {
458 dev_info(dev,
"forcing port_map 0x%x -> 0x%x\n",
459 port_map, force_port_map);
460 port_map = force_port_map;
464 dev_warn(dev,
"masking port_map 0x%x -> 0x%x\n",
466 port_map & mask_port_map);
467 port_map &= mask_port_map;
475 if (port_map & (1 << i))
481 if (map_ports > ahci_nr_ports(cap)) {
483 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
484 port_map, ahci_nr_ports(cap));
491 port_map = (1 << ahci_nr_ports(cap)) - 1;
492 dev_warn(dev,
"forcing PORTS_IMPL to 0x%x\n", port_map);
514 static void ahci_restore_initial_config(
struct ata_host *
host)
526 static unsigned ahci_scr_offset(
struct ata_port *ap,
unsigned int sc_reg)
528 static const int offset[] = {
539 return offset[sc_reg];
545 void __iomem *port_mmio = ahci_port_base(link->
ap);
546 int offset = ahci_scr_offset(link->
ap, sc_reg);
549 *val =
readl(port_mmio + offset);
555 static int ahci_scr_write(
struct ata_link *link,
unsigned int sc_reg,
u32 val)
557 void __iomem *port_mmio = ahci_port_base(link->
ap);
558 int offset = ahci_scr_offset(link->
ap, sc_reg);
561 writel(val, port_mmio + offset);
569 void __iomem *port_mmio = ahci_port_base(ap);
582 void __iomem *port_mmio = ahci_port_base(ap);
605 static void ahci_start_fis_rx(
struct ata_port *ap)
607 void __iomem *port_mmio = ahci_port_base(ap);
632 static int ahci_stop_fis_rx(
struct ata_port *ap)
634 void __iomem *port_mmio = ahci_port_base(ap);
651 static void ahci_power_up(
struct ata_port *ap)
654 void __iomem *port_mmio = ahci_port_base(ap);
675 void __iomem *port_mmio = ahci_port_base(ap);
716 ahci_set_aggressive_devslp(ap,
true);
718 ahci_set_aggressive_devslp(ap,
false);
733 static void ahci_power_down(
struct ata_port *ap)
736 void __iomem *port_mmio = ahci_port_base(ap);
754 static void ahci_start_port(
struct ata_port *ap)
764 ahci_start_fis_rx(ap);
777 rc = ahci_transmit_led_message(ap,
790 ahci_init_sw_activity(link);
801 *emsg =
"failed to stop engine";
806 rc = ahci_stop_fis_rx(ap);
808 *emsg =
"failed stop FIS RX";
824 ahci_enable_ahci(mmio);
827 if (!ahci_skip_host_reset) {
841 HOST_RESET, 10, 1000);
843 if (tmp & HOST_RESET) {
844 dev_err(host->
dev,
"controller reset failed (0x%x)\n",
850 ahci_enable_ahci(mmio);
855 ahci_restore_initial_config(host);
857 dev_info(host->
dev,
"skipping global host reset\n");
863 static void ahci_sw_activity(
struct ata_link *link)
873 if (!timer_pending(&emp->
timer))
877 static void ahci_sw_activity_blink(
unsigned long arg)
883 unsigned long led_message = emp->
led_state;
884 u32 activity_led_state;
888 led_message |= ap->
port_no | (link->
pmp << 8);
900 if (activity_led_state)
901 activity_led_state = 0;
903 activity_led_state = 1;
909 led_message |= (activity_led_state << 16);
915 led_message |= (1 << 16);
917 spin_unlock_irqrestore(ap->
lock, flags);
918 ahci_transmit_led_message(ap, led_message, 4);
921 static void ahci_init_sw_activity(
struct ata_link *link)
943 if ((em_ctl & EM_CTL_TM) || (em_ctl &
EM_CTL_RST))
977 if (em_ctl & EM_CTL_TM) {
978 spin_unlock_irqrestore(ap->
lock, flags);
987 message[0] |= (4 << 8);
1005 spin_unlock_irqrestore(ap->
lock, flags);
1047 return ahci_transmit_led_message(ap, state, size);
1065 port_led_state |= (ap->
port_no | (link->
pmp << 8));
1066 ahci_transmit_led_message(ap, port_led_state, 4);
1072 port_led_state |= (ap->
port_no | (link->
pmp << 8));
1074 ahci_transmit_led_message(ap, port_led_state, 4);
1094 static void ahci_port_init(
struct device *dev,
struct ata_port *ap,
1098 const char *emsg =
NULL;
1103 rc = ahci_deinit_port(ap, &emsg);
1105 dev_warn(dev,
"%s (%d)\n", emsg, rc);
1109 VPRINTK(
"PORT_SCR_ERR 0x%x\n", tmp);
1114 VPRINTK(
"PORT_IRQ_STAT 0x%x\n", tmp);
1129 for (i = 0; i < host->
n_ports; i++) {
1132 port_mmio = ahci_port_base(ap);
1133 if (ata_port_is_dummy(ap))
1136 ahci_port_init(host->
dev, ap, i, mmio, port_mmio);
1140 VPRINTK(
"HOST_CTL 0x%x\n", tmp);
1143 VPRINTK(
"HOST_CTL 0x%x\n", tmp);
1147 static void ahci_dev_config(
struct ata_device *dev)
1154 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1160 void __iomem *port_mmio = ahci_port_base(ap);
1165 tf.
lbah = (tmp >> 24) & 0xff;
1166 tf.
lbam = (tmp >> 16) & 0xff;
1167 tf.
lbal = (tmp >> 8) & 0xff;
1190 void __iomem *port_mmio = ahci_port_base(ap);
1205 if (!busy && !sata_pmp_attached(ap)) {
1233 static int ahci_exec_polled_cmd(
struct ata_port *ap,
int pmp,
1235 unsigned long timeout_msec)
1237 const u32 cmd_fis_len = 5;
1239 void __iomem *port_mmio = ahci_port_base(ap);
1252 0x1, 0x1, 1, timeout_msec);
1264 int pmp,
unsigned long deadline,
1265 int (*check_ready)(
struct ata_link *link))
1270 unsigned long now, msecs;
1279 ata_link_warn(link,
"failed to reset engine (errno=%d)\n", rc);
1281 ata_tf_init(link->
device, &tf);
1290 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1293 reason =
"1st FIS failed";
1302 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1312 ata_link_info(link,
"device not ready, treating as offline\n");
1316 reason =
"device not ready";
1319 *
class = ahci_dev_classify(ap);
1321 DPRINTK(
"EXIT, class=%u\n", *
class);
1331 void __iomem *port_mmio = ahci_port_base(link->
ap);
1334 return ata_check_ready(status);
1338 static int ahci_softreset(
struct ata_link *link,
unsigned int *
class,
1339 unsigned long deadline)
1341 int pmp = sata_srst_pmp(link);
1349 static int ahci_bad_pmp_check_ready(
struct ata_link *link)
1351 void __iomem *port_mmio = ahci_port_base(link->
ap);
1362 return ata_check_ready(status);
1365 int ahci_pmp_retry_softreset(
struct ata_link *link,
unsigned int *
class,
1366 unsigned long deadline)
1369 void __iomem *port_mmio = ahci_port_base(ap);
1370 int pmp = sata_srst_pmp(link);
1377 ahci_bad_pmp_check_ready);
1388 "applying PMP SRST workaround "
1398 static int ahci_hardreset(
struct ata_link *link,
unsigned int *
class,
1399 unsigned long deadline)
1414 ata_tf_init(link->
device, &tf);
1424 *
class = ahci_dev_classify(ap);
1426 DPRINTK(
"EXIT, rc=%d, class=%u\n", rc, *
class);
1430 static void ahci_postreset(
struct ata_link *link,
unsigned int *
class)
1433 void __iomem *port_mmio = ahci_port_base(ap);
1444 if (new_tmp != tmp) {
1450 static unsigned int ahci_fill_sg(
struct ata_queued_cmd *qc,
void *cmd_tbl)
1488 int is_atapi = ata_is_atapi(qc->
tf.protocol);
1491 const u32 cmd_fis_len = 5;
1492 unsigned int n_elem;
1508 n_elem = ahci_fill_sg(qc, cmd_tbl);
1513 opts = cmd_fis_len | n_elem << 16 | (qc->
dev->link->pmp << 12);
1522 static void ahci_fbs_dec_intr(
struct ata_port *ap)
1525 void __iomem *port_mmio = ahci_port_base(ap);
1542 if (fbs & PORT_FBS_DEC)
1543 dev_err(ap->
host->dev,
"failed to clear device error\n");
1546 static void ahci_error_intr(
struct ata_port *ap,
u32 irq_stat)
1554 bool fbs_need_dec =
false;
1559 void __iomem *port_mmio = ahci_port_base(ap);
1563 if ((fbs &
PORT_FBS_SDE) && (pmp < ap->nr_pmp_links) &&
1566 fbs_need_dec =
true;
1571 if (ata_link_active(link))
1577 active_qc = ata_qc_from_tag(ap, link->active_tag);
1578 active_ehi = &link->eh_info;
1585 ahci_scr_read(&ap->link,
SCR_ERROR, &serror);
1586 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1587 host_ehi->serror |= serror;
1613 "unknown FIS %08x %08x %08x %08x" ,
1614 unk[0], unk[1], unk[2], unk[3]);
1629 if (irq_stat & PORT_IRQ_IF_ERR) {
1641 ata_ehi_hotplugged(host_ehi);
1644 "connection status changed" :
"PHY RDY changed");
1651 else if (fbs_need_dec) {
1653 ahci_fbs_dec_intr(ap);
1658 static void ahci_port_intr(
struct ata_port *ap)
1660 void __iomem *port_mmio = ahci_port_base(ap);
1673 status &= ~PORT_IRQ_BAD_PMP;
1682 ahci_error_intr(ap, status);
1739 if (
unlikely(rc < 0 && !resetting)) {
1748 struct ata_host *host = dev_instance;
1750 unsigned int i, handled = 0;
1764 irq_masked = irq_stat & hpriv->
port_map;
1766 spin_lock(&host->
lock);
1768 for (i = 0; i < host->
n_ports; i++) {
1771 if (!(irq_masked & (1 << i)))
1779 VPRINTK(
"port %u (no irq)\n", i);
1782 "interrupt on disabled port %u\n", i);
1799 spin_unlock(&host->
lock);
1810 void __iomem *port_mmio = ahci_port_base(ap);
1832 ahci_sw_activity(qc->
dev->link);
1861 static void ahci_freeze(
struct ata_port *ap)
1863 void __iomem *port_mmio = ahci_port_base(ap);
1869 static void ahci_thaw(
struct ata_port *ap)
1873 void __iomem *port_mmio = ahci_port_base(ap);
1886 static void ahci_error_handler(
struct ata_port *ap)
1896 if (!ata_dev_enabled(ap->
link.device))
1909 static void ahci_set_aggressive_devslp(
struct ata_port *ap,
bool sleep)
1911 void __iomem *port_mmio = ahci_port_base(ap);
1913 u32 devslp, dm, dito, mdat, deto;
1915 unsigned int err_mask;
1919 dev_err(ap->
host->dev,
"port does not support device sleep\n");
1926 writel(devslp & ~PORT_DEVSLP_ADSE,
1938 if (devslp & PORT_DEVSLP_ADSE)
1984 static void ahci_enable_fbs(
struct ata_port *ap)
1987 void __iomem *port_mmio = ahci_port_base(ap);
2007 if (fbs & PORT_FBS_EN) {
2017 static void ahci_disable_fbs(
struct ata_port *ap)
2020 void __iomem *port_mmio = ahci_port_base(ap);
2028 if ((fbs & PORT_FBS_EN) == 0) {
2039 if (fbs & PORT_FBS_EN)
2040 dev_err(ap->
host->dev,
"Failed to disable FBS\n");
2049 static void ahci_pmp_attach(
struct ata_port *ap)
2051 void __iomem *port_mmio = ahci_port_base(ap);
2059 ahci_enable_fbs(ap);
2075 static void ahci_pmp_detach(
struct ata_port *ap)
2077 void __iomem *port_mmio = ahci_port_base(ap);
2081 ahci_disable_fbs(ap);
2097 ahci_start_port(ap);
2099 if (sata_pmp_attached(ap))
2100 ahci_pmp_attach(ap);
2102 ahci_pmp_detach(ap);
2111 const char *emsg =
NULL;
2114 rc = ahci_deinit_port(ap, &emsg);
2116 ahci_power_down(ap);
2126 static int ahci_port_start(
struct ata_port *ap)
2133 size_t dma_sz, rx_fis_sz;
2141 void __iomem *port_mmio = ahci_port_base(ap);
2146 dev_info(dev,
"port %d can do FBS, forcing FBSCP\n",
2150 dev_warn(dev,
"port %d is not capable of FBS\n",
2184 mem_dma += rx_fis_sz;
2205 static void ahci_port_stop(
struct ata_port *ap)
2207 const char *emsg =
NULL;
2211 rc = ahci_deinit_port(ap, &emsg);
2221 const char *speed_s;
2228 speed = (cap >> 20) & 0xf;
2231 else if (speed == 2)
2233 else if (speed == 3)
2239 "AHCI %02x%02x.%02x%02x "
2240 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2243 (vers >> 24) & 0xff,
2244 (vers >> 16) & 0xff,
2248 ((cap >> 8) & 0x1f) + 1,
2304 hpriv->
em_loc = ((em_loc >> 16) * 4);
2305 hpriv->
em_buf_sz = ((em_loc & 0xff) * 4);