blob: 1610fff19bb39dfa5ad9f00187e44801836f7633 [file] [log] [blame]
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001/*
2 * libahci.c - Common AHCI SATA low-level routines
3 *
Tejun Heo8c3d3d42013-05-14 11:09:50 -07004 * Maintained by: Tejun Heo <tj@kernel.org>
Anton Vorontsov365cfa12010-03-28 00:22:14 -04005 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2004-2005 Red Hat, Inc.
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *
26 * libata documentation is available via 'make {ps|pdf}docs',
27 * as Documentation/DocBook/libata.*
28 *
29 * AHCI hardware documentation:
30 * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
31 * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
32 *
33 */
34
35#include <linux/kernel.h>
Tejun Heofbaf6662010-03-30 02:52:43 +090036#include <linux/gfp.h>
Anton Vorontsov365cfa12010-03-28 00:22:14 -040037#include <linux/module.h>
John Garry5600d612018-06-08 18:26:33 +080038#include <linux/nospec.h>
Anton Vorontsov365cfa12010-03-28 00:22:14 -040039#include <linux/blkdev.h>
40#include <linux/delay.h>
41#include <linux/interrupt.h>
42#include <linux/dma-mapping.h>
43#include <linux/device.h>
44#include <scsi/scsi_host.h>
45#include <scsi/scsi_cmnd.h>
46#include <linux/libata.h>
Dan Williamsd684a902015-11-11 16:27:33 -080047#include <linux/pci.h>
Anton Vorontsov365cfa12010-03-28 00:22:14 -040048#include "ahci.h"
Shane Huang65fe1f02012-09-07 22:40:01 +080049#include "libata.h"
Anton Vorontsov365cfa12010-03-28 00:22:14 -040050
51static int ahci_skip_host_reset;
52int ahci_ignore_sss;
53EXPORT_SYMBOL_GPL(ahci_ignore_sss);
54
55module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
56MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
57
58module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
59MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
60
Tejun Heo6b7ae952010-09-01 17:50:06 +020061static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
62 unsigned hints);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040063static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
64static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
65 size_t size);
66static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
67 ssize_t size);
68
69
70
71static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
72static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040073static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
74static int ahci_port_start(struct ata_port *ap);
75static void ahci_port_stop(struct ata_port *ap);
76static void ahci_qc_prep(struct ata_queued_cmd *qc);
77static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
78static void ahci_freeze(struct ata_port *ap);
79static void ahci_thaw(struct ata_port *ap);
Shane Huang65fe1f02012-09-07 22:40:01 +080080static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040081static void ahci_enable_fbs(struct ata_port *ap);
82static void ahci_disable_fbs(struct ata_port *ap);
83static void ahci_pmp_attach(struct ata_port *ap);
84static void ahci_pmp_detach(struct ata_port *ap);
85static int ahci_softreset(struct ata_link *link, unsigned int *class,
86 unsigned long deadline);
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +080087static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
88 unsigned long deadline);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040089static int ahci_hardreset(struct ata_link *link, unsigned int *class,
90 unsigned long deadline);
91static void ahci_postreset(struct ata_link *link, unsigned int *class);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040092static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040093static void ahci_dev_config(struct ata_device *dev);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040094#ifdef CONFIG_PM
95static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
96#endif
97static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
98static ssize_t ahci_activity_store(struct ata_device *dev,
99 enum sw_activity val);
100static void ahci_init_sw_activity(struct ata_link *link);
101
102static ssize_t ahci_show_host_caps(struct device *dev,
103 struct device_attribute *attr, char *buf);
104static ssize_t ahci_show_host_cap2(struct device *dev,
105 struct device_attribute *attr, char *buf);
106static ssize_t ahci_show_host_version(struct device *dev,
107 struct device_attribute *attr, char *buf);
108static ssize_t ahci_show_port_cmd(struct device *dev,
109 struct device_attribute *attr, char *buf);
Harry Zhangc0623162010-04-23 17:28:38 +0800110static ssize_t ahci_read_em_buffer(struct device *dev,
111 struct device_attribute *attr, char *buf);
112static ssize_t ahci_store_em_buffer(struct device *dev,
113 struct device_attribute *attr,
114 const char *buf, size_t size);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100115static ssize_t ahci_show_em_supported(struct device *dev,
116 struct device_attribute *attr, char *buf);
Suman Tripathif070d672016-02-06 11:25:22 +0530117static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400118
119static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
120static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
121static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
122static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
Harry Zhangc0623162010-04-23 17:28:38 +0800123static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
124 ahci_read_em_buffer, ahci_store_em_buffer);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100125static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400126
Tejun Heofad16e72010-09-21 09:25:48 +0200127struct device_attribute *ahci_shost_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400128 &dev_attr_link_power_management_policy,
129 &dev_attr_em_message_type,
130 &dev_attr_em_message,
131 &dev_attr_ahci_host_caps,
132 &dev_attr_ahci_host_cap2,
133 &dev_attr_ahci_host_version,
134 &dev_attr_ahci_port_cmd,
Harry Zhangc0623162010-04-23 17:28:38 +0800135 &dev_attr_em_buffer,
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100136 &dev_attr_em_message_supported,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400137 NULL
138};
Tejun Heofad16e72010-09-21 09:25:48 +0200139EXPORT_SYMBOL_GPL(ahci_shost_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400140
Tejun Heofad16e72010-09-21 09:25:48 +0200141struct device_attribute *ahci_sdev_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400142 &dev_attr_sw_activity,
143 &dev_attr_unload_heads,
144 NULL
145};
Tejun Heofad16e72010-09-21 09:25:48 +0200146EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400147
148struct ata_port_operations ahci_ops = {
149 .inherits = &sata_pmp_port_ops,
150
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,
155
156 .freeze = ahci_freeze,
157 .thaw = ahci_thaw,
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,
165
166 .scr_read = ahci_scr_read,
167 .scr_write = ahci_scr_write,
168 .pmp_attach = ahci_pmp_attach,
169 .pmp_detach = ahci_pmp_detach,
170
Tejun Heo6b7ae952010-09-01 17:50:06 +0200171 .set_lpm = ahci_set_lpm,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400172 .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,
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500176 .transmit_led_message = ahci_transmit_led_message,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400177#ifdef CONFIG_PM
178 .port_suspend = ahci_port_suspend,
179 .port_resume = ahci_port_resume,
180#endif
181 .port_start = ahci_port_start,
182 .port_stop = ahci_port_stop,
183};
184EXPORT_SYMBOL_GPL(ahci_ops);
185
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +0800186struct ata_port_operations ahci_pmp_retry_srst_ops = {
187 .inherits = &ahci_ops,
188 .softreset = ahci_pmp_retry_softreset,
189};
190EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
191
Chuansheng Liued08d402013-09-18 20:21:49 +0800192static bool ahci_em_messages __read_mostly = true;
Chuansheng Liued08d402013-09-18 20:21:49 +0800193module_param(ahci_em_messages, bool, 0444);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400194/* add other LED protocol types when they become supported */
195MODULE_PARM_DESC(ahci_em_messages,
Harry Zhang008dbd62010-04-23 17:27:19 +0800196 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400197
Chuansheng Liued08d402013-09-18 20:21:49 +0800198/* device sleep idle timeout in ms */
199static int devslp_idle_timeout __read_mostly = 1000;
Shane Huang65fe1f02012-09-07 22:40:01 +0800200module_param(devslp_idle_timeout, int, 0644);
201MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
202
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400203static void ahci_enable_ahci(void __iomem *mmio)
204{
205 int i;
206 u32 tmp;
207
208 /* turn on AHCI_EN */
209 tmp = readl(mmio + HOST_CTL);
210 if (tmp & HOST_AHCI_EN)
211 return;
212
213 /* Some controllers need AHCI_EN to be written multiple times.
214 * Try a few times before giving up.
215 */
216 for (i = 0; i < 5; i++) {
217 tmp |= HOST_AHCI_EN;
218 writel(tmp, mmio + HOST_CTL);
219 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
220 if (tmp & HOST_AHCI_EN)
221 return;
222 msleep(10);
223 }
224
225 WARN_ON(1);
226}
227
Mika Westerbergbb03c642016-02-18 10:54:16 +0200228/**
229 * ahci_rpm_get_port - Make sure the port is powered on
230 * @ap: Port to power on
231 *
232 * Whenever there is need to access the AHCI host registers outside of
233 * normal execution paths, call this function to make sure the host is
234 * actually powered on.
235 */
236static int ahci_rpm_get_port(struct ata_port *ap)
237{
238 return pm_runtime_get_sync(ap->dev);
239}
240
241/**
242 * ahci_rpm_put_port - Undoes ahci_rpm_get_port()
243 * @ap: Port to power down
244 *
245 * Undoes ahci_rpm_get_port() and possibly powers down the AHCI host
246 * if it has no more active users.
247 */
248static void ahci_rpm_put_port(struct ata_port *ap)
249{
250 pm_runtime_put(ap->dev);
251}
252
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400253static ssize_t ahci_show_host_caps(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256 struct Scsi_Host *shost = class_to_shost(dev);
257 struct ata_port *ap = ata_shost_to_port(shost);
258 struct ahci_host_priv *hpriv = ap->host->private_data;
259
260 return sprintf(buf, "%x\n", hpriv->cap);
261}
262
263static ssize_t ahci_show_host_cap2(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
266 struct Scsi_Host *shost = class_to_shost(dev);
267 struct ata_port *ap = ata_shost_to_port(shost);
268 struct ahci_host_priv *hpriv = ap->host->private_data;
269
270 return sprintf(buf, "%x\n", hpriv->cap2);
271}
272
273static ssize_t ahci_show_host_version(struct device *dev,
274 struct device_attribute *attr, char *buf)
275{
276 struct Scsi_Host *shost = class_to_shost(dev);
277 struct ata_port *ap = ata_shost_to_port(shost);
278 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400279
Mika Westerberg8ea909c2016-02-18 10:54:14 +0200280 return sprintf(buf, "%x\n", hpriv->version);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400281}
282
283static ssize_t ahci_show_port_cmd(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
286 struct Scsi_Host *shost = class_to_shost(dev);
287 struct ata_port *ap = ata_shost_to_port(shost);
288 void __iomem *port_mmio = ahci_port_base(ap);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200289 ssize_t ret;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400290
Mika Westerbergbb03c642016-02-18 10:54:16 +0200291 ahci_rpm_get_port(ap);
292 ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
293 ahci_rpm_put_port(ap);
294
295 return ret;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400296}
297
Harry Zhangc0623162010-04-23 17:28:38 +0800298static ssize_t ahci_read_em_buffer(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct Scsi_Host *shost = class_to_shost(dev);
302 struct ata_port *ap = ata_shost_to_port(shost);
303 struct ahci_host_priv *hpriv = ap->host->private_data;
304 void __iomem *mmio = hpriv->mmio;
305 void __iomem *em_mmio = mmio + hpriv->em_loc;
306 u32 em_ctl, msg;
307 unsigned long flags;
308 size_t count;
309 int i;
310
Mika Westerbergbb03c642016-02-18 10:54:16 +0200311 ahci_rpm_get_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800312 spin_lock_irqsave(ap->lock, flags);
313
314 em_ctl = readl(mmio + HOST_EM_CTL);
315 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
316 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
317 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200318 ahci_rpm_put_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800319 return -EINVAL;
320 }
321
322 if (!(em_ctl & EM_CTL_MR)) {
323 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200324 ahci_rpm_put_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800325 return -EAGAIN;
326 }
327
328 if (!(em_ctl & EM_CTL_SMB))
329 em_mmio += hpriv->em_buf_sz;
330
331 count = hpriv->em_buf_sz;
332
333 /* the count should not be larger than PAGE_SIZE */
334 if (count > PAGE_SIZE) {
335 if (printk_ratelimit())
Joe Perchesa9a79df2011-04-15 15:51:59 -0700336 ata_port_warn(ap,
337 "EM read buffer size too large: "
338 "buffer size %u, page size %lu\n",
339 hpriv->em_buf_sz, PAGE_SIZE);
Harry Zhangc0623162010-04-23 17:28:38 +0800340 count = PAGE_SIZE;
341 }
342
343 for (i = 0; i < count; i += 4) {
344 msg = readl(em_mmio + i);
345 buf[i] = msg & 0xff;
346 buf[i + 1] = (msg >> 8) & 0xff;
347 buf[i + 2] = (msg >> 16) & 0xff;
348 buf[i + 3] = (msg >> 24) & 0xff;
349 }
350
351 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200352 ahci_rpm_put_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800353
354 return i;
355}
356
357static ssize_t ahci_store_em_buffer(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t size)
360{
361 struct Scsi_Host *shost = class_to_shost(dev);
362 struct ata_port *ap = ata_shost_to_port(shost);
363 struct ahci_host_priv *hpriv = ap->host->private_data;
364 void __iomem *mmio = hpriv->mmio;
365 void __iomem *em_mmio = mmio + hpriv->em_loc;
Harry Zhangf9ce8892010-06-24 11:34:23 +0800366 const unsigned char *msg_buf = buf;
Harry Zhangc0623162010-04-23 17:28:38 +0800367 u32 em_ctl, msg;
368 unsigned long flags;
369 int i;
370
371 /* check size validity */
372 if (!(ap->flags & ATA_FLAG_EM) ||
373 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
374 size % 4 || size > hpriv->em_buf_sz)
375 return -EINVAL;
376
Mika Westerbergbb03c642016-02-18 10:54:16 +0200377 ahci_rpm_get_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800378 spin_lock_irqsave(ap->lock, flags);
379
380 em_ctl = readl(mmio + HOST_EM_CTL);
381 if (em_ctl & EM_CTL_TM) {
382 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200383 ahci_rpm_put_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800384 return -EBUSY;
385 }
386
387 for (i = 0; i < size; i += 4) {
Harry Zhangf9ce8892010-06-24 11:34:23 +0800388 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
389 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
Harry Zhangc0623162010-04-23 17:28:38 +0800390 writel(msg, em_mmio + i);
391 }
392
393 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
394
395 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200396 ahci_rpm_put_port(ap);
Harry Zhangc0623162010-04-23 17:28:38 +0800397
398 return size;
399}
400
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100401static ssize_t ahci_show_em_supported(struct device *dev,
402 struct device_attribute *attr, char *buf)
403{
404 struct Scsi_Host *shost = class_to_shost(dev);
405 struct ata_port *ap = ata_shost_to_port(shost);
406 struct ahci_host_priv *hpriv = ap->host->private_data;
407 void __iomem *mmio = hpriv->mmio;
408 u32 em_ctl;
409
Mika Westerbergbb03c642016-02-18 10:54:16 +0200410 ahci_rpm_get_port(ap);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100411 em_ctl = readl(mmio + HOST_EM_CTL);
Mika Westerbergbb03c642016-02-18 10:54:16 +0200412 ahci_rpm_put_port(ap);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100413
414 return sprintf(buf, "%s%s%s%s\n",
415 em_ctl & EM_CTL_LED ? "led " : "",
416 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
417 em_ctl & EM_CTL_SES ? "ses-2 " : "",
418 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
419}
420
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400421/**
422 * ahci_save_initial_config - Save and fixup initial config values
423 * @dev: target AHCI device
424 * @hpriv: host private area to store config values
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400425 *
426 * Some registers containing configuration info might be setup by
427 * BIOS and might be cleared on reset. This function saves the
428 * initial values of those registers into @hpriv such that they
429 * can be restored after controller reset.
430 *
431 * If inconsistent, config values are fixed up by this function.
432 *
Hans de Goede039ece32014-02-22 16:53:30 +0100433 * If it is not set already this function sets hpriv->start_engine to
434 * ahci_start_engine.
435 *
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400436 * LOCKING:
437 * None.
438 */
Antoine Ténart725c7b52014-07-30 20:13:56 +0200439void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400440{
441 void __iomem *mmio = hpriv->mmio;
442 u32 cap, cap2, vers, port_map;
443 int i;
444
445 /* make sure AHCI mode is enabled before accessing CAP */
446 ahci_enable_ahci(mmio);
447
448 /* Values prefixed with saved_ are written back to host after
449 * reset. Values without are used for driver operation.
450 */
451 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
452 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
453
454 /* CAP2 register is only defined for AHCI 1.2 and later */
455 vers = readl(mmio + HOST_VERSION);
456 if ((vers >> 16) > 1 ||
457 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
458 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
459 else
460 hpriv->saved_cap2 = cap2 = 0;
461
462 /* some chips have errata preventing 64bit use */
463 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700464 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400465 cap &= ~HOST_CAP_64;
466 }
467
468 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700469 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400470 cap &= ~HOST_CAP_NCQ;
471 }
472
473 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700474 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400475 cap |= HOST_CAP_NCQ;
476 }
477
478 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700479 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400480 cap &= ~HOST_CAP_PMP;
481 }
482
483 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700484 dev_info(dev,
485 "controller can't do SNTF, turning off CAP_SNTF\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400486 cap &= ~HOST_CAP_SNTF;
487 }
488
Jacob Pan0cf4a7d2014-04-15 22:27:11 -0700489 if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
490 dev_info(dev,
491 "controller can't do DEVSLP, turning off\n");
492 cap2 &= ~HOST_CAP2_SDS;
493 cap2 &= ~HOST_CAP2_SADM;
494 }
495
Tejun Heo5f173102010-07-24 16:53:48 +0200496 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700497 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
Tejun Heo5f173102010-07-24 16:53:48 +0200498 cap |= HOST_CAP_FBS;
499 }
500
Kefeng Wang888d91a2014-05-14 14:13:40 +0800501 if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
502 dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
503 cap &= ~HOST_CAP_FBS;
504 }
505
Antoine Ténart725c7b52014-07-30 20:13:56 +0200506 if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700507 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
Antoine Ténart725c7b52014-07-30 20:13:56 +0200508 port_map, hpriv->force_port_map);
509 port_map = hpriv->force_port_map;
Srinivas Kandagatla2fd0f462016-04-01 08:52:56 +0100510 hpriv->saved_port_map = port_map;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400511 }
512
Antoine Ténart725c7b52014-07-30 20:13:56 +0200513 if (hpriv->mask_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700514 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
515 port_map,
Antoine Ténart725c7b52014-07-30 20:13:56 +0200516 port_map & hpriv->mask_port_map);
517 port_map &= hpriv->mask_port_map;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400518 }
519
520 /* cross check port_map and cap.n_ports */
521 if (port_map) {
522 int map_ports = 0;
523
524 for (i = 0; i < AHCI_MAX_PORTS; i++)
525 if (port_map & (1 << i))
526 map_ports++;
527
528 /* If PI has more ports than n_ports, whine, clear
529 * port_map and let it be generated from n_ports.
530 */
531 if (map_ports > ahci_nr_ports(cap)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700532 dev_warn(dev,
533 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
534 port_map, ahci_nr_ports(cap));
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400535 port_map = 0;
536 }
537 }
538
Tejun Heo566d1822016-01-15 15:13:05 -0500539 /* fabricate port_map from cap.nr_ports for < AHCI 1.3 */
540 if (!port_map && vers < 0x10300) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400541 port_map = (1 << ahci_nr_ports(cap)) - 1;
Joe Perchesa44fec12011-04-15 15:51:58 -0700542 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400543
544 /* write the fixed up value to the PI register */
545 hpriv->saved_port_map = port_map;
546 }
547
548 /* record values to use during operation */
549 hpriv->cap = cap;
550 hpriv->cap2 = cap2;
Mika Westerberg8ea909c2016-02-18 10:54:14 +0200551 hpriv->version = readl(mmio + HOST_VERSION);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400552 hpriv->port_map = port_map;
Hans de Goede039ece32014-02-22 16:53:30 +0100553
554 if (!hpriv->start_engine)
555 hpriv->start_engine = ahci_start_engine;
Suman Tripathif070d672016-02-06 11:25:22 +0530556
557 if (!hpriv->irq_handler)
Suman Tripathid867b952016-02-06 11:25:23 +0530558 hpriv->irq_handler = ahci_single_level_irq_intr;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400559}
560EXPORT_SYMBOL_GPL(ahci_save_initial_config);
561
562/**
563 * ahci_restore_initial_config - Restore initial config
564 * @host: target ATA host
565 *
566 * Restore initial config stored by ahci_save_initial_config().
567 *
568 * LOCKING:
569 * None.
570 */
571static void ahci_restore_initial_config(struct ata_host *host)
572{
573 struct ahci_host_priv *hpriv = host->private_data;
574 void __iomem *mmio = hpriv->mmio;
575
576 writel(hpriv->saved_cap, mmio + HOST_CAP);
577 if (hpriv->saved_cap2)
578 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
579 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
580 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
581}
582
583static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
584{
585 static const int offset[] = {
586 [SCR_STATUS] = PORT_SCR_STAT,
587 [SCR_CONTROL] = PORT_SCR_CTL,
588 [SCR_ERROR] = PORT_SCR_ERR,
589 [SCR_ACTIVE] = PORT_SCR_ACT,
590 [SCR_NOTIFICATION] = PORT_SCR_NTF,
591 };
592 struct ahci_host_priv *hpriv = ap->host->private_data;
593
594 if (sc_reg < ARRAY_SIZE(offset) &&
595 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
596 return offset[sc_reg];
597 return 0;
598}
599
600static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
601{
602 void __iomem *port_mmio = ahci_port_base(link->ap);
603 int offset = ahci_scr_offset(link->ap, sc_reg);
604
605 if (offset) {
606 *val = readl(port_mmio + offset);
607 return 0;
608 }
609 return -EINVAL;
610}
611
612static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
613{
614 void __iomem *port_mmio = ahci_port_base(link->ap);
615 int offset = ahci_scr_offset(link->ap, sc_reg);
616
617 if (offset) {
618 writel(val, port_mmio + offset);
619 return 0;
620 }
621 return -EINVAL;
622}
623
624void ahci_start_engine(struct ata_port *ap)
625{
626 void __iomem *port_mmio = ahci_port_base(ap);
627 u32 tmp;
628
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400629 /* start DMA */
630 tmp = readl(port_mmio + PORT_CMD);
631 tmp |= PORT_CMD_START;
632 writel(tmp, port_mmio + PORT_CMD);
633 readl(port_mmio + PORT_CMD); /* flush */
634}
635EXPORT_SYMBOL_GPL(ahci_start_engine);
636
637int ahci_stop_engine(struct ata_port *ap)
638{
639 void __iomem *port_mmio = ahci_port_base(ap);
Danesh Petigarafb329632016-01-11 13:22:26 -0800640 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400641 u32 tmp;
642
Danesh Petigarafb329632016-01-11 13:22:26 -0800643 /*
644 * On some controllers, stopping a port's DMA engine while the port
645 * is in ALPM state (partial or slumber) results in failures on
646 * subsequent DMA engine starts. For those controllers, put the
647 * port back in active state before stopping its DMA engine.
648 */
649 if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
650 (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
651 ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
652 dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
653 return -EIO;
654 }
655
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400656 tmp = readl(port_mmio + PORT_CMD);
657
658 /* check if the HBA is idle */
659 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
660 return 0;
661
662 /* setting HBA to idle */
663 tmp &= ~PORT_CMD_START;
664 writel(tmp, port_mmio + PORT_CMD);
665
666 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo97750ce2010-09-06 17:56:29 +0200667 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400668 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
669 if (tmp & PORT_CMD_LIST_ON)
670 return -EIO;
671
672 return 0;
673}
674EXPORT_SYMBOL_GPL(ahci_stop_engine);
675
Suman Tripathi39e0ee92014-07-07 22:33:04 +0530676void ahci_start_fis_rx(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400677{
678 void __iomem *port_mmio = ahci_port_base(ap);
679 struct ahci_host_priv *hpriv = ap->host->private_data;
680 struct ahci_port_priv *pp = ap->private_data;
681 u32 tmp;
682
683 /* set FIS registers */
684 if (hpriv->cap & HOST_CAP_64)
685 writel((pp->cmd_slot_dma >> 16) >> 16,
686 port_mmio + PORT_LST_ADDR_HI);
687 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
688
689 if (hpriv->cap & HOST_CAP_64)
690 writel((pp->rx_fis_dma >> 16) >> 16,
691 port_mmio + PORT_FIS_ADDR_HI);
692 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
693
694 /* enable FIS reception */
695 tmp = readl(port_mmio + PORT_CMD);
696 tmp |= PORT_CMD_FIS_RX;
697 writel(tmp, port_mmio + PORT_CMD);
698
699 /* flush */
700 readl(port_mmio + PORT_CMD);
701}
Suman Tripathi39e0ee92014-07-07 22:33:04 +0530702EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400703
704static int ahci_stop_fis_rx(struct ata_port *ap)
705{
706 void __iomem *port_mmio = ahci_port_base(ap);
707 u32 tmp;
708
709 /* disable FIS reception */
710 tmp = readl(port_mmio + PORT_CMD);
711 tmp &= ~PORT_CMD_FIS_RX;
712 writel(tmp, port_mmio + PORT_CMD);
713
714 /* wait for completion, spec says 500ms, give it 1000 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200715 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400716 PORT_CMD_FIS_ON, 10, 1000);
717 if (tmp & PORT_CMD_FIS_ON)
718 return -EBUSY;
719
720 return 0;
721}
722
723static void ahci_power_up(struct ata_port *ap)
724{
725 struct ahci_host_priv *hpriv = ap->host->private_data;
726 void __iomem *port_mmio = ahci_port_base(ap);
727 u32 cmd;
728
729 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
730
731 /* spin up device */
732 if (hpriv->cap & HOST_CAP_SSS) {
733 cmd |= PORT_CMD_SPIN_UP;
734 writel(cmd, port_mmio + PORT_CMD);
735 }
736
737 /* wake up link */
738 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
739}
740
Tejun Heo6b7ae952010-09-01 17:50:06 +0200741static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
742 unsigned int hints)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400743{
Tejun Heo6b7ae952010-09-01 17:50:06 +0200744 struct ata_port *ap = link->ap;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400745 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400746 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400747 void __iomem *port_mmio = ahci_port_base(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400748
Tejun Heo6b7ae952010-09-01 17:50:06 +0200749 if (policy != ATA_LPM_MAX_POWER) {
Danesh Petigarafb329632016-01-11 13:22:26 -0800750 /* wakeup flag only applies to the max power policy */
751 hints &= ~ATA_LPM_WAKE_ONLY;
752
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400753 /*
Tejun Heo6b7ae952010-09-01 17:50:06 +0200754 * Disable interrupts on Phy Ready. This keeps us from
755 * getting woken up due to spurious phy ready
756 * interrupts.
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400757 */
Tejun Heo6b7ae952010-09-01 17:50:06 +0200758 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
759 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
760
761 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400762 }
763
Tejun Heo6b7ae952010-09-01 17:50:06 +0200764 if (hpriv->cap & HOST_CAP_ALPM) {
765 u32 cmd = readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400766
Tejun Heo6b7ae952010-09-01 17:50:06 +0200767 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
Danesh Petigarafb329632016-01-11 13:22:26 -0800768 if (!(hints & ATA_LPM_WAKE_ONLY))
769 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
Tejun Heo6b7ae952010-09-01 17:50:06 +0200770 cmd |= PORT_CMD_ICC_ACTIVE;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400771
Tejun Heo6b7ae952010-09-01 17:50:06 +0200772 writel(cmd, port_mmio + PORT_CMD);
773 readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400774
Tejun Heo6b7ae952010-09-01 17:50:06 +0200775 /* wait 10ms to be sure we've come out of LPM state */
Tejun Heo97750ce2010-09-06 17:56:29 +0200776 ata_msleep(ap, 10);
Danesh Petigarafb329632016-01-11 13:22:26 -0800777
778 if (hints & ATA_LPM_WAKE_ONLY)
779 return 0;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200780 } else {
781 cmd |= PORT_CMD_ALPE;
782 if (policy == ATA_LPM_MIN_POWER)
783 cmd |= PORT_CMD_ASP;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400784
Tejun Heo6b7ae952010-09-01 17:50:06 +0200785 /* write out new cmd value */
786 writel(cmd, port_mmio + PORT_CMD);
787 }
788 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400789
Shane Huang65fe1f02012-09-07 22:40:01 +0800790 /* set aggressive device sleep */
791 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
792 (hpriv->cap2 & HOST_CAP2_SADM) &&
793 (link->device->flags & ATA_DFLAG_DEVSLP)) {
794 if (policy == ATA_LPM_MIN_POWER)
795 ahci_set_aggressive_devslp(ap, true);
796 else
797 ahci_set_aggressive_devslp(ap, false);
798 }
799
Tejun Heo6b7ae952010-09-01 17:50:06 +0200800 if (policy == ATA_LPM_MAX_POWER) {
801 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400802
Tejun Heo6b7ae952010-09-01 17:50:06 +0200803 /* turn PHYRDY IRQ back on */
804 pp->intr_mask |= PORT_IRQ_PHYRDY;
805 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
806 }
807
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400808 return 0;
809}
810
811#ifdef CONFIG_PM
812static void ahci_power_down(struct ata_port *ap)
813{
814 struct ahci_host_priv *hpriv = ap->host->private_data;
815 void __iomem *port_mmio = ahci_port_base(ap);
816 u32 cmd, scontrol;
817
818 if (!(hpriv->cap & HOST_CAP_SSS))
819 return;
820
821 /* put device into listen mode, first set PxSCTL.DET to 0 */
822 scontrol = readl(port_mmio + PORT_SCR_CTL);
823 scontrol &= ~0xf;
824 writel(scontrol, port_mmio + PORT_SCR_CTL);
825
826 /* then set PxCMD.SUD to 0 */
827 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
828 cmd &= ~PORT_CMD_SPIN_UP;
829 writel(cmd, port_mmio + PORT_CMD);
830}
831#endif
832
833static void ahci_start_port(struct ata_port *ap)
834{
Brian Norris66583c92012-02-21 10:38:42 -0800835 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400836 struct ahci_port_priv *pp = ap->private_data;
837 struct ata_link *link;
838 struct ahci_em_priv *emp;
839 ssize_t rc;
840 int i;
841
842 /* enable FIS reception */
843 ahci_start_fis_rx(ap);
844
Brian Norris66583c92012-02-21 10:38:42 -0800845 /* enable DMA */
846 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
Hans de Goede039ece32014-02-22 16:53:30 +0100847 hpriv->start_engine(ap);
Brian Norris66583c92012-02-21 10:38:42 -0800848
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400849 /* turn on LEDs */
850 if (ap->flags & ATA_FLAG_EM) {
851 ata_for_each_link(link, ap, EDGE) {
852 emp = &pp->em_priv[link->pmp];
853
854 /* EM Transmit bit maybe busy during init */
855 for (i = 0; i < EM_MAX_RETRY; i++) {
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500856 rc = ap->ops->transmit_led_message(ap,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400857 emp->led_state,
858 4);
Lukasz Doraufa070ee2013-10-14 18:18:53 +0200859 /*
860 * If busy, give a breather but do not
861 * release EH ownership by using msleep()
862 * instead of ata_msleep(). EM Transmit
863 * bit is busy for the whole host and
864 * releasing ownership will cause other
865 * ports to fail the same way.
866 */
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400867 if (rc == -EBUSY)
Lukasz Doraufa070ee2013-10-14 18:18:53 +0200868 msleep(1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400869 else
870 break;
871 }
872 }
873 }
874
875 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
876 ata_for_each_link(link, ap, EDGE)
877 ahci_init_sw_activity(link);
878
879}
880
881static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
882{
883 int rc;
884
885 /* disable DMA */
886 rc = ahci_stop_engine(ap);
887 if (rc) {
888 *emsg = "failed to stop engine";
889 return rc;
890 }
891
892 /* disable FIS reception */
893 rc = ahci_stop_fis_rx(ap);
894 if (rc) {
895 *emsg = "failed stop FIS RX";
896 return rc;
897 }
898
899 return 0;
900}
901
902int ahci_reset_controller(struct ata_host *host)
903{
904 struct ahci_host_priv *hpriv = host->private_data;
905 void __iomem *mmio = hpriv->mmio;
906 u32 tmp;
907
908 /* we must be in AHCI mode, before using anything
909 * AHCI-specific, such as HOST_RESET.
910 */
911 ahci_enable_ahci(mmio);
912
913 /* global controller reset */
914 if (!ahci_skip_host_reset) {
915 tmp = readl(mmio + HOST_CTL);
916 if ((tmp & HOST_RESET) == 0) {
917 writel(tmp | HOST_RESET, mmio + HOST_CTL);
918 readl(mmio + HOST_CTL); /* flush */
919 }
920
921 /*
922 * to perform host reset, OS should set HOST_RESET
923 * and poll until this bit is read to be "0".
924 * reset must complete within 1 second, or
925 * the hardware should be considered fried.
926 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200927 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400928 HOST_RESET, 10, 1000);
929
930 if (tmp & HOST_RESET) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700931 dev_err(host->dev, "controller reset failed (0x%x)\n",
932 tmp);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400933 return -EIO;
934 }
935
936 /* turn on AHCI mode */
937 ahci_enable_ahci(mmio);
938
939 /* Some registers might be cleared on reset. Restore
940 * initial values.
941 */
942 ahci_restore_initial_config(host);
943 } else
Joe Perchesa44fec12011-04-15 15:51:58 -0700944 dev_info(host->dev, "skipping global host reset\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400945
946 return 0;
947}
948EXPORT_SYMBOL_GPL(ahci_reset_controller);
949
950static void ahci_sw_activity(struct ata_link *link)
951{
952 struct ata_port *ap = link->ap;
953 struct ahci_port_priv *pp = ap->private_data;
954 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
955
956 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
957 return;
958
959 emp->activity++;
960 if (!timer_pending(&emp->timer))
961 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
962}
963
964static void ahci_sw_activity_blink(unsigned long arg)
965{
966 struct ata_link *link = (struct ata_link *)arg;
967 struct ata_port *ap = link->ap;
968 struct ahci_port_priv *pp = ap->private_data;
969 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
970 unsigned long led_message = emp->led_state;
971 u32 activity_led_state;
972 unsigned long flags;
973
974 led_message &= EM_MSG_LED_VALUE;
975 led_message |= ap->port_no | (link->pmp << 8);
976
977 /* check to see if we've had activity. If so,
978 * toggle state of LED and reset timer. If not,
979 * turn LED to desired idle state.
980 */
981 spin_lock_irqsave(ap->lock, flags);
982 if (emp->saved_activity != emp->activity) {
983 emp->saved_activity = emp->activity;
984 /* get the current LED state */
985 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
986
987 if (activity_led_state)
988 activity_led_state = 0;
989 else
990 activity_led_state = 1;
991
992 /* clear old state */
993 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
994
995 /* toggle state */
996 led_message |= (activity_led_state << 16);
997 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
998 } else {
999 /* switch to idle */
1000 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1001 if (emp->blink_policy == BLINK_OFF)
1002 led_message |= (1 << 16);
1003 }
1004 spin_unlock_irqrestore(ap->lock, flags);
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001005 ap->ops->transmit_led_message(ap, led_message, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001006}
1007
1008static void ahci_init_sw_activity(struct ata_link *link)
1009{
1010 struct ata_port *ap = link->ap;
1011 struct ahci_port_priv *pp = ap->private_data;
1012 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1013
1014 /* init activity stats, setup timer */
1015 emp->saved_activity = emp->activity = 0;
1016 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
1017
1018 /* check our blink policy and set flag for link if it's enabled */
1019 if (emp->blink_policy)
1020 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1021}
1022
1023int ahci_reset_em(struct ata_host *host)
1024{
1025 struct ahci_host_priv *hpriv = host->private_data;
1026 void __iomem *mmio = hpriv->mmio;
1027 u32 em_ctl;
1028
1029 em_ctl = readl(mmio + HOST_EM_CTL);
1030 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1031 return -EINVAL;
1032
1033 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1034 return 0;
1035}
1036EXPORT_SYMBOL_GPL(ahci_reset_em);
1037
1038static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1039 ssize_t size)
1040{
1041 struct ahci_host_priv *hpriv = ap->host->private_data;
1042 struct ahci_port_priv *pp = ap->private_data;
1043 void __iomem *mmio = hpriv->mmio;
1044 u32 em_ctl;
1045 u32 message[] = {0, 0};
1046 unsigned long flags;
1047 int pmp;
1048 struct ahci_em_priv *emp;
1049
1050 /* get the slot number from the message */
1051 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1052 if (pmp < EM_MAX_SLOTS)
1053 emp = &pp->em_priv[pmp];
1054 else
1055 return -EINVAL;
1056
Mika Westerbergbb03c642016-02-18 10:54:16 +02001057 ahci_rpm_get_port(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001058 spin_lock_irqsave(ap->lock, flags);
1059
1060 /*
1061 * if we are still busy transmitting a previous message,
1062 * do not allow
1063 */
1064 em_ctl = readl(mmio + HOST_EM_CTL);
1065 if (em_ctl & EM_CTL_TM) {
1066 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +02001067 ahci_rpm_put_port(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001068 return -EBUSY;
1069 }
1070
Harry Zhang008dbd62010-04-23 17:27:19 +08001071 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1072 /*
1073 * create message header - this is all zero except for
1074 * the message size, which is 4 bytes.
1075 */
1076 message[0] |= (4 << 8);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001077
Harry Zhang008dbd62010-04-23 17:27:19 +08001078 /* ignore 0:4 of byte zero, fill in port info yourself */
1079 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001080
Harry Zhang008dbd62010-04-23 17:27:19 +08001081 /* write message to EM_LOC */
1082 writel(message[0], mmio + hpriv->em_loc);
1083 writel(message[1], mmio + hpriv->em_loc+4);
1084
1085 /*
1086 * tell hardware to transmit the message
1087 */
1088 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1089 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001090
1091 /* save off new led state for port/slot */
1092 emp->led_state = state;
1093
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001094 spin_unlock_irqrestore(ap->lock, flags);
Mika Westerbergbb03c642016-02-18 10:54:16 +02001095 ahci_rpm_put_port(ap);
1096
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001097 return size;
1098}
1099
1100static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1101{
1102 struct ahci_port_priv *pp = ap->private_data;
1103 struct ata_link *link;
1104 struct ahci_em_priv *emp;
1105 int rc = 0;
1106
1107 ata_for_each_link(link, ap, EDGE) {
1108 emp = &pp->em_priv[link->pmp];
1109 rc += sprintf(buf, "%lx\n", emp->led_state);
1110 }
1111 return rc;
1112}
1113
1114static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1115 size_t size)
1116{
Daeseok Younb2a52b62014-02-20 08:28:45 +09001117 unsigned int state;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001118 int pmp;
1119 struct ahci_port_priv *pp = ap->private_data;
1120 struct ahci_em_priv *emp;
1121
Daeseok Younb2a52b62014-02-20 08:28:45 +09001122 if (kstrtouint(buf, 0, &state) < 0)
1123 return -EINVAL;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001124
1125 /* get the slot number from the message */
1126 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
John Garry5600d612018-06-08 18:26:33 +08001127 if (pmp < EM_MAX_SLOTS) {
1128 pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001129 emp = &pp->em_priv[pmp];
John Garry5600d612018-06-08 18:26:33 +08001130 } else {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001131 return -EINVAL;
John Garry5600d612018-06-08 18:26:33 +08001132 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001133
1134 /* mask off the activity bits if we are in sw_activity
1135 * mode, user should turn off sw_activity before setting
1136 * activity led through em_message
1137 */
1138 if (emp->blink_policy)
1139 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1140
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001141 return ap->ops->transmit_led_message(ap, state, size);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001142}
1143
1144static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1145{
1146 struct ata_link *link = dev->link;
1147 struct ata_port *ap = link->ap;
1148 struct ahci_port_priv *pp = ap->private_data;
1149 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1150 u32 port_led_state = emp->led_state;
1151
1152 /* save the desired Activity LED behavior */
1153 if (val == OFF) {
1154 /* clear LFLAG */
1155 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1156
1157 /* set the LED to OFF */
1158 port_led_state &= EM_MSG_LED_VALUE_OFF;
1159 port_led_state |= (ap->port_no | (link->pmp << 8));
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001160 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001161 } else {
1162 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1163 if (val == BLINK_OFF) {
1164 /* set LED to ON for idle */
1165 port_led_state &= EM_MSG_LED_VALUE_OFF;
1166 port_led_state |= (ap->port_no | (link->pmp << 8));
1167 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001168 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001169 }
1170 }
1171 emp->blink_policy = val;
1172 return 0;
1173}
1174
1175static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1176{
1177 struct ata_link *link = dev->link;
1178 struct ata_port *ap = link->ap;
1179 struct ahci_port_priv *pp = ap->private_data;
1180 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1181
1182 /* display the saved value of activity behavior for this
1183 * disk.
1184 */
1185 return sprintf(buf, "%d\n", emp->blink_policy);
1186}
1187
1188static void ahci_port_init(struct device *dev, struct ata_port *ap,
1189 int port_no, void __iomem *mmio,
1190 void __iomem *port_mmio)
1191{
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02001192 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001193 const char *emsg = NULL;
1194 int rc;
1195 u32 tmp;
1196
1197 /* make sure port is not active */
1198 rc = ahci_deinit_port(ap, &emsg);
1199 if (rc)
1200 dev_warn(dev, "%s (%d)\n", emsg, rc);
1201
1202 /* clear SError */
1203 tmp = readl(port_mmio + PORT_SCR_ERR);
1204 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1205 writel(tmp, port_mmio + PORT_SCR_ERR);
1206
1207 /* clear port IRQ */
1208 tmp = readl(port_mmio + PORT_IRQ_STAT);
1209 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1210 if (tmp)
1211 writel(tmp, port_mmio + PORT_IRQ_STAT);
1212
1213 writel(1 << port_no, mmio + HOST_IRQ_STAT);
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02001214
1215 /* mark esata ports */
1216 tmp = readl(port_mmio + PORT_CMD);
Manuel Laussdc8b4af2016-02-27 16:10:05 +01001217 if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02001218 ap->pflags |= ATA_PFLAG_EXTERNAL;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001219}
1220
1221void ahci_init_controller(struct ata_host *host)
1222{
1223 struct ahci_host_priv *hpriv = host->private_data;
1224 void __iomem *mmio = hpriv->mmio;
1225 int i;
1226 void __iomem *port_mmio;
1227 u32 tmp;
1228
1229 for (i = 0; i < host->n_ports; i++) {
1230 struct ata_port *ap = host->ports[i];
1231
1232 port_mmio = ahci_port_base(ap);
1233 if (ata_port_is_dummy(ap))
1234 continue;
1235
1236 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1237 }
1238
1239 tmp = readl(mmio + HOST_CTL);
1240 VPRINTK("HOST_CTL 0x%x\n", tmp);
1241 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1242 tmp = readl(mmio + HOST_CTL);
1243 VPRINTK("HOST_CTL 0x%x\n", tmp);
1244}
1245EXPORT_SYMBOL_GPL(ahci_init_controller);
1246
1247static void ahci_dev_config(struct ata_device *dev)
1248{
1249 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1250
1251 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1252 dev->max_sectors = 255;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001253 ata_dev_info(dev,
1254 "SB600 AHCI: limiting to 255 sectors per cmd\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001255 }
1256}
1257
Rob Herringbbb4ab42012-08-17 09:51:50 -05001258unsigned int ahci_dev_classify(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001259{
1260 void __iomem *port_mmio = ahci_port_base(ap);
1261 struct ata_taskfile tf;
1262 u32 tmp;
1263
1264 tmp = readl(port_mmio + PORT_SIG);
1265 tf.lbah = (tmp >> 24) & 0xff;
1266 tf.lbam = (tmp >> 16) & 0xff;
1267 tf.lbal = (tmp >> 8) & 0xff;
1268 tf.nsect = (tmp) & 0xff;
1269
1270 return ata_dev_classify(&tf);
1271}
Rob Herringbbb4ab42012-08-17 09:51:50 -05001272EXPORT_SYMBOL_GPL(ahci_dev_classify);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001273
David Milburn02cdfcf2010-11-12 15:38:21 -06001274void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1275 u32 opts)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001276{
1277 dma_addr_t cmd_tbl_dma;
1278
1279 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1280
1281 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1282 pp->cmd_slot[tag].status = 0;
1283 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1284 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1285}
David Milburn02cdfcf2010-11-12 15:38:21 -06001286EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001287
1288int ahci_kick_engine(struct ata_port *ap)
1289{
1290 void __iomem *port_mmio = ahci_port_base(ap);
1291 struct ahci_host_priv *hpriv = ap->host->private_data;
1292 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1293 u32 tmp;
1294 int busy, rc;
1295
1296 /* stop engine */
1297 rc = ahci_stop_engine(ap);
1298 if (rc)
1299 goto out_restart;
1300
1301 /* need to do CLO?
1302 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1303 */
1304 busy = status & (ATA_BUSY | ATA_DRQ);
1305 if (!busy && !sata_pmp_attached(ap)) {
1306 rc = 0;
1307 goto out_restart;
1308 }
1309
1310 if (!(hpriv->cap & HOST_CAP_CLO)) {
1311 rc = -EOPNOTSUPP;
1312 goto out_restart;
1313 }
1314
1315 /* perform CLO */
1316 tmp = readl(port_mmio + PORT_CMD);
1317 tmp |= PORT_CMD_CLO;
1318 writel(tmp, port_mmio + PORT_CMD);
1319
1320 rc = 0;
Tejun Heo97750ce2010-09-06 17:56:29 +02001321 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001322 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1323 if (tmp & PORT_CMD_CLO)
1324 rc = -EIO;
1325
1326 /* restart engine */
1327 out_restart:
Hans de Goede039ece32014-02-22 16:53:30 +01001328 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001329 return rc;
1330}
1331EXPORT_SYMBOL_GPL(ahci_kick_engine);
1332
1333static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1334 struct ata_taskfile *tf, int is_cmd, u16 flags,
1335 unsigned long timeout_msec)
1336{
1337 const u32 cmd_fis_len = 5; /* five dwords */
1338 struct ahci_port_priv *pp = ap->private_data;
1339 void __iomem *port_mmio = ahci_port_base(ap);
1340 u8 *fis = pp->cmd_tbl;
1341 u32 tmp;
1342
1343 /* prep the command */
1344 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1345 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1346
Xiangliang Yu023113d2015-11-26 20:27:02 +08001347 /* set port value for softreset of Port Multiplier */
1348 if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1349 tmp = readl(port_mmio + PORT_FBS);
1350 tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1351 tmp |= pmp << PORT_FBS_DEV_OFFSET;
1352 writel(tmp, port_mmio + PORT_FBS);
1353 pp->fbs_last_dev = pmp;
1354 }
1355
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001356 /* issue & wait */
1357 writel(1, port_mmio + PORT_CMD_ISSUE);
1358
1359 if (timeout_msec) {
Tejun Heo97750ce2010-09-06 17:56:29 +02001360 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1361 0x1, 0x1, 1, timeout_msec);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001362 if (tmp & 0x1) {
1363 ahci_kick_engine(ap);
1364 return -EBUSY;
1365 }
1366 } else
1367 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1368
1369 return 0;
1370}
1371
1372int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1373 int pmp, unsigned long deadline,
1374 int (*check_ready)(struct ata_link *link))
1375{
1376 struct ata_port *ap = link->ap;
1377 struct ahci_host_priv *hpriv = ap->host->private_data;
xiangliang yu89dafa22013-10-27 08:03:04 -04001378 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001379 const char *reason = NULL;
1380 unsigned long now, msecs;
1381 struct ata_taskfile tf;
xiangliang yu89dafa22013-10-27 08:03:04 -04001382 bool fbs_disabled = false;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001383 int rc;
1384
1385 DPRINTK("ENTER\n");
1386
1387 /* prepare for SRST (AHCI-1.1 10.4.1) */
1388 rc = ahci_kick_engine(ap);
1389 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001390 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001391
xiangliang yu89dafa22013-10-27 08:03:04 -04001392 /*
1393 * According to AHCI-1.2 9.3.9: if FBS is enable, software shall
1394 * clear PxFBS.EN to '0' prior to issuing software reset to devices
1395 * that is attached to port multiplier.
1396 */
1397 if (!ata_is_host_link(link) && pp->fbs_enabled) {
1398 ahci_disable_fbs(ap);
1399 fbs_disabled = true;
1400 }
1401
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001402 ata_tf_init(link->device, &tf);
1403
1404 /* issue the first D2H Register FIS */
1405 msecs = 0;
1406 now = jiffies;
Tejun Heof1f5a802010-08-27 11:09:15 +02001407 if (time_after(deadline, now))
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001408 msecs = jiffies_to_msecs(deadline - now);
1409
1410 tf.ctl |= ATA_SRST;
1411 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1412 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1413 rc = -EIO;
1414 reason = "1st FIS failed";
1415 goto fail;
1416 }
1417
1418 /* spec says at least 5us, but be generous and sleep for 1ms */
Tejun Heo97750ce2010-09-06 17:56:29 +02001419 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001420
1421 /* issue the second D2H Register FIS */
1422 tf.ctl &= ~ATA_SRST;
1423 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1424
1425 /* wait for link to become ready */
1426 rc = ata_wait_after_reset(link, deadline, check_ready);
1427 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1428 /*
1429 * Workaround for cases where link online status can't
1430 * be trusted. Treat device readiness timeout as link
1431 * offline.
1432 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001433 ata_link_info(link, "device not ready, treating as offline\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001434 *class = ATA_DEV_NONE;
1435 } else if (rc) {
1436 /* link occupied, -ENODEV too is an error */
1437 reason = "device not ready";
1438 goto fail;
1439 } else
1440 *class = ahci_dev_classify(ap);
1441
xiangliang yu89dafa22013-10-27 08:03:04 -04001442 /* re-enable FBS if disabled before */
1443 if (fbs_disabled)
1444 ahci_enable_fbs(ap);
1445
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001446 DPRINTK("EXIT, class=%u\n", *class);
1447 return 0;
1448
1449 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07001450 ata_link_err(link, "softreset failed (%s)\n", reason);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001451 return rc;
1452}
1453
1454int ahci_check_ready(struct ata_link *link)
1455{
1456 void __iomem *port_mmio = ahci_port_base(link->ap);
1457 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1458
1459 return ata_check_ready(status);
1460}
1461EXPORT_SYMBOL_GPL(ahci_check_ready);
1462
1463static int ahci_softreset(struct ata_link *link, unsigned int *class,
1464 unsigned long deadline)
1465{
1466 int pmp = sata_srst_pmp(link);
1467
1468 DPRINTK("ENTER\n");
1469
1470 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1471}
1472EXPORT_SYMBOL_GPL(ahci_do_softreset);
1473
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001474static int ahci_bad_pmp_check_ready(struct ata_link *link)
1475{
1476 void __iomem *port_mmio = ahci_port_base(link->ap);
1477 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1478 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1479
1480 /*
1481 * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1482 * which can save timeout delay.
1483 */
1484 if (irq_status & PORT_IRQ_BAD_PMP)
1485 return -EIO;
1486
1487 return ata_check_ready(status);
1488}
1489
Daeseok Youn35186d02014-02-20 08:34:27 +09001490static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1491 unsigned long deadline)
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001492{
1493 struct ata_port *ap = link->ap;
1494 void __iomem *port_mmio = ahci_port_base(ap);
1495 int pmp = sata_srst_pmp(link);
1496 int rc;
1497 u32 irq_sts;
1498
1499 DPRINTK("ENTER\n");
1500
1501 rc = ahci_do_softreset(link, class, pmp, deadline,
1502 ahci_bad_pmp_check_ready);
1503
1504 /*
1505 * Soft reset fails with IPMS set when PMP is enabled but
1506 * SATA HDD/ODD is connected to SATA port, do soft reset
1507 * again to port 0.
1508 */
1509 if (rc == -EIO) {
1510 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1511 if (irq_sts & PORT_IRQ_BAD_PMP) {
Wei Yongjun39f80ac2012-12-03 23:39:31 -05001512 ata_link_warn(link,
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001513 "applying PMP SRST workaround "
1514 "and retrying\n");
1515 rc = ahci_do_softreset(link, class, 0, deadline,
1516 ahci_check_ready);
1517 }
1518 }
1519
1520 return rc;
1521}
1522
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001523static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1524 unsigned long deadline)
1525{
1526 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1527 struct ata_port *ap = link->ap;
1528 struct ahci_port_priv *pp = ap->private_data;
Hans de Goede039ece32014-02-22 16:53:30 +01001529 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001530 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1531 struct ata_taskfile tf;
1532 bool online;
1533 int rc;
1534
1535 DPRINTK("ENTER\n");
1536
1537 ahci_stop_engine(ap);
1538
1539 /* clear D2H reception area to properly wait for D2H FIS */
1540 ata_tf_init(link->device, &tf);
Sergei Shtylyov9bbb1b02013-06-23 01:39:39 +04001541 tf.command = ATA_BUSY;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001542 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1543
1544 rc = sata_link_hardreset(link, timing, deadline, &online,
1545 ahci_check_ready);
1546
Hans de Goede039ece32014-02-22 16:53:30 +01001547 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001548
1549 if (online)
1550 *class = ahci_dev_classify(ap);
1551
1552 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1553 return rc;
1554}
1555
1556static void ahci_postreset(struct ata_link *link, unsigned int *class)
1557{
1558 struct ata_port *ap = link->ap;
1559 void __iomem *port_mmio = ahci_port_base(ap);
1560 u32 new_tmp, tmp;
1561
1562 ata_std_postreset(link, class);
1563
1564 /* Make sure port's ATAPI bit is set appropriately */
1565 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1566 if (*class == ATA_DEV_ATAPI)
1567 new_tmp |= PORT_CMD_ATAPI;
1568 else
1569 new_tmp &= ~PORT_CMD_ATAPI;
1570 if (new_tmp != tmp) {
1571 writel(new_tmp, port_mmio + PORT_CMD);
1572 readl(port_mmio + PORT_CMD); /* flush */
1573 }
1574}
1575
1576static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1577{
1578 struct scatterlist *sg;
1579 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1580 unsigned int si;
1581
1582 VPRINTK("ENTER\n");
1583
1584 /*
1585 * Next, the S/G list.
1586 */
1587 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1588 dma_addr_t addr = sg_dma_address(sg);
1589 u32 sg_len = sg_dma_len(sg);
1590
1591 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1592 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1593 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1594 }
1595
1596 return si;
1597}
1598
1599static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1600{
1601 struct ata_port *ap = qc->ap;
1602 struct ahci_port_priv *pp = ap->private_data;
1603
1604 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1605 return ata_std_qc_defer(qc);
1606 else
1607 return sata_pmp_qc_defer_cmd_switch(qc);
1608}
1609
1610static void ahci_qc_prep(struct ata_queued_cmd *qc)
1611{
1612 struct ata_port *ap = qc->ap;
1613 struct ahci_port_priv *pp = ap->private_data;
1614 int is_atapi = ata_is_atapi(qc->tf.protocol);
1615 void *cmd_tbl;
1616 u32 opts;
1617 const u32 cmd_fis_len = 5; /* five dwords */
1618 unsigned int n_elem;
1619
1620 /*
1621 * Fill in command table information. First, the header,
1622 * a SATA Register - Host to Device command FIS.
1623 */
1624 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1625
1626 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1627 if (is_atapi) {
1628 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1629 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1630 }
1631
1632 n_elem = 0;
1633 if (qc->flags & ATA_QCFLAG_DMAMAP)
1634 n_elem = ahci_fill_sg(qc, cmd_tbl);
1635
1636 /*
1637 * Fill in command slot information.
1638 */
1639 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1640 if (qc->tf.flags & ATA_TFLAG_WRITE)
1641 opts |= AHCI_CMD_WRITE;
1642 if (is_atapi)
1643 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1644
1645 ahci_fill_cmd_slot(pp, qc->tag, opts);
1646}
1647
1648static void ahci_fbs_dec_intr(struct ata_port *ap)
1649{
1650 struct ahci_port_priv *pp = ap->private_data;
1651 void __iomem *port_mmio = ahci_port_base(ap);
1652 u32 fbs = readl(port_mmio + PORT_FBS);
1653 int retries = 3;
1654
1655 DPRINTK("ENTER\n");
1656 BUG_ON(!pp->fbs_enabled);
1657
1658 /* time to wait for DEC is not specified by AHCI spec,
1659 * add a retry loop for safety.
1660 */
1661 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1662 fbs = readl(port_mmio + PORT_FBS);
1663 while ((fbs & PORT_FBS_DEC) && retries--) {
1664 udelay(1);
1665 fbs = readl(port_mmio + PORT_FBS);
1666 }
1667
1668 if (fbs & PORT_FBS_DEC)
Joe Perchesa44fec12011-04-15 15:51:58 -07001669 dev_err(ap->host->dev, "failed to clear device error\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001670}
1671
1672static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1673{
1674 struct ahci_host_priv *hpriv = ap->host->private_data;
1675 struct ahci_port_priv *pp = ap->private_data;
1676 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1677 struct ata_link *link = NULL;
1678 struct ata_queued_cmd *active_qc;
1679 struct ata_eh_info *active_ehi;
1680 bool fbs_need_dec = false;
1681 u32 serror;
1682
1683 /* determine active link with error */
1684 if (pp->fbs_enabled) {
1685 void __iomem *port_mmio = ahci_port_base(ap);
1686 u32 fbs = readl(port_mmio + PORT_FBS);
1687 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1688
Shane Huang912b9ac2013-06-08 16:00:16 +08001689 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001690 link = &ap->pmp_link[pmp];
1691 fbs_need_dec = true;
1692 }
1693
1694 } else
1695 ata_for_each_link(link, ap, EDGE)
1696 if (ata_link_active(link))
1697 break;
1698
1699 if (!link)
1700 link = &ap->link;
1701
1702 active_qc = ata_qc_from_tag(ap, link->active_tag);
1703 active_ehi = &link->eh_info;
1704
1705 /* record irq stat */
1706 ata_ehi_clear_desc(host_ehi);
1707 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1708
1709 /* AHCI needs SError cleared; otherwise, it might lock up */
1710 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1711 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1712 host_ehi->serror |= serror;
1713
1714 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1715 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1716 irq_stat &= ~PORT_IRQ_IF_ERR;
1717
1718 if (irq_stat & PORT_IRQ_TF_ERR) {
1719 /* If qc is active, charge it; otherwise, the active
1720 * link. There's no active qc on NCQ errors. It will
1721 * be determined by EH by reading log page 10h.
1722 */
1723 if (active_qc)
1724 active_qc->err_mask |= AC_ERR_DEV;
1725 else
1726 active_ehi->err_mask |= AC_ERR_DEV;
1727
1728 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1729 host_ehi->serror &= ~SERR_INTERNAL;
1730 }
1731
1732 if (irq_stat & PORT_IRQ_UNK_FIS) {
Joe Perchesd5185d62014-03-26 09:34:49 -07001733 u32 *unk = pp->rx_fis + RX_FIS_UNK;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001734
1735 active_ehi->err_mask |= AC_ERR_HSM;
1736 active_ehi->action |= ATA_EH_RESET;
1737 ata_ehi_push_desc(active_ehi,
1738 "unknown FIS %08x %08x %08x %08x" ,
1739 unk[0], unk[1], unk[2], unk[3]);
1740 }
1741
1742 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1743 active_ehi->err_mask |= AC_ERR_HSM;
1744 active_ehi->action |= ATA_EH_RESET;
1745 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1746 }
1747
1748 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1749 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1750 host_ehi->action |= ATA_EH_RESET;
1751 ata_ehi_push_desc(host_ehi, "host bus error");
1752 }
1753
1754 if (irq_stat & PORT_IRQ_IF_ERR) {
1755 if (fbs_need_dec)
1756 active_ehi->err_mask |= AC_ERR_DEV;
1757 else {
1758 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1759 host_ehi->action |= ATA_EH_RESET;
1760 }
1761
1762 ata_ehi_push_desc(host_ehi, "interface fatal error");
1763 }
1764
1765 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1766 ata_ehi_hotplugged(host_ehi);
1767 ata_ehi_push_desc(host_ehi, "%s",
1768 irq_stat & PORT_IRQ_CONNECT ?
1769 "connection status changed" : "PHY RDY changed");
1770 }
1771
1772 /* okay, let's hand over to EH */
1773
1774 if (irq_stat & PORT_IRQ_FREEZE)
1775 ata_port_freeze(ap);
1776 else if (fbs_need_dec) {
1777 ata_link_abort(link);
1778 ahci_fbs_dec_intr(ap);
1779 } else
1780 ata_port_abort(ap);
1781}
1782
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001783static void ahci_handle_port_interrupt(struct ata_port *ap,
1784 void __iomem *port_mmio, u32 status)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001785{
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001786 struct ata_eh_info *ehi = &ap->link.eh_info;
1787 struct ahci_port_priv *pp = ap->private_data;
1788 struct ahci_host_priv *hpriv = ap->host->private_data;
1789 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001790 u32 qc_active = 0;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001791 int rc;
1792
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001793 /* ignore BAD_PMP while resetting */
1794 if (unlikely(resetting))
1795 status &= ~PORT_IRQ_BAD_PMP;
1796
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02001797 if (sata_lpm_ignore_phy_events(&ap->link)) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001798 status &= ~PORT_IRQ_PHYRDY;
Tejun Heo6b7ae952010-09-01 17:50:06 +02001799 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001800 }
1801
1802 if (unlikely(status & PORT_IRQ_ERROR)) {
1803 ahci_error_intr(ap, status);
1804 return;
1805 }
1806
1807 if (status & PORT_IRQ_SDB_FIS) {
1808 /* If SNotification is available, leave notification
1809 * handling to sata_async_notification(). If not,
1810 * emulate it by snooping SDB FIS RX area.
1811 *
1812 * Snooping FIS RX area is probably cheaper than
1813 * poking SNotification but some constrollers which
1814 * implement SNotification, ICH9 for example, don't
1815 * store AN SDB FIS into receive area.
1816 */
1817 if (hpriv->cap & HOST_CAP_SNTF)
1818 sata_async_notification(ap);
1819 else {
1820 /* If the 'N' bit in word 0 of the FIS is set,
1821 * we just received asynchronous notification.
1822 * Tell libata about it.
1823 *
1824 * Lack of SNotification should not appear in
1825 * ahci 1.2, so the workaround is unnecessary
1826 * when FBS is enabled.
1827 */
1828 if (pp->fbs_enabled)
1829 WARN_ON_ONCE(1);
1830 else {
1831 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1832 u32 f0 = le32_to_cpu(f[0]);
1833 if (f0 & (1 << 15))
1834 sata_async_notification(ap);
1835 }
1836 }
1837 }
1838
1839 /* pp->active_link is not reliable once FBS is enabled, both
1840 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1841 * NCQ and non-NCQ commands may be in flight at the same time.
1842 */
1843 if (pp->fbs_enabled) {
1844 if (ap->qc_active) {
1845 qc_active = readl(port_mmio + PORT_SCR_ACT);
1846 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1847 }
1848 } else {
1849 /* pp->active_link is valid iff any command is in flight */
1850 if (ap->qc_active && pp->active_link->sactive)
1851 qc_active = readl(port_mmio + PORT_SCR_ACT);
1852 else
1853 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1854 }
1855
1856
1857 rc = ata_qc_complete_multiple(ap, qc_active);
1858
1859 /* while resetting, invalid completions are expected */
1860 if (unlikely(rc < 0 && !resetting)) {
1861 ehi->err_mask |= AC_ERR_HSM;
1862 ehi->action |= ATA_EH_RESET;
1863 ata_port_freeze(ap);
1864 }
1865}
1866
Tejun Heo7865f832014-10-27 09:50:36 -04001867static void ahci_port_intr(struct ata_port *ap)
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001868{
1869 void __iomem *port_mmio = ahci_port_base(ap);
1870 u32 status;
1871
1872 status = readl(port_mmio + PORT_IRQ_STAT);
1873 writel(status, port_mmio + PORT_IRQ_STAT);
1874
Tejun Heo7865f832014-10-27 09:50:36 -04001875 ahci_handle_port_interrupt(ap, port_mmio, status);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001876}
1877
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001878static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001879{
Alexander Gordeev227dfb42014-09-29 18:26:01 +02001880 struct ata_port *ap = dev_instance;
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001881 void __iomem *port_mmio = ahci_port_base(ap);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001882 u32 status;
1883
Alexander Gordeev227dfb42014-09-29 18:26:01 +02001884 VPRINTK("ENTER\n");
1885
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001886 status = readl(port_mmio + PORT_IRQ_STAT);
1887 writel(status, port_mmio + PORT_IRQ_STAT);
1888
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001889 spin_lock(ap->lock);
1890 ahci_handle_port_interrupt(ap, port_mmio, status);
1891 spin_unlock(ap->lock);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001892
1893 VPRINTK("EXIT\n");
1894
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001895 return IRQ_HANDLED;
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001896}
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001897
Suman Tripathif070d672016-02-06 11:25:22 +05301898u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001899{
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001900 unsigned int i, handled = 0;
Tejun Heo03e83cb2014-10-27 12:00:01 -04001901
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001902 for (i = 0; i < host->n_ports; i++) {
1903 struct ata_port *ap;
1904
1905 if (!(irq_masked & (1 << i)))
1906 continue;
1907
1908 ap = host->ports[i];
1909 if (ap) {
Tejun Heo7865f832014-10-27 09:50:36 -04001910 ahci_port_intr(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001911 VPRINTK("port %u\n", i);
1912 } else {
1913 VPRINTK("port %u (no irq)\n", i);
1914 if (ata_ratelimit())
Joe Perchesa44fec12011-04-15 15:51:58 -07001915 dev_warn(host->dev,
1916 "interrupt on disabled port %u\n", i);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001917 }
1918
1919 handled = 1;
1920 }
1921
Suman Tripathia129db82015-05-06 00:51:10 +05301922 return handled;
1923}
Suman Tripathif070d672016-02-06 11:25:22 +05301924EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
Suman Tripathia129db82015-05-06 00:51:10 +05301925
1926static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1927{
1928 struct ata_host *host = dev_instance;
1929 struct ahci_host_priv *hpriv;
1930 unsigned int rc = 0;
1931 void __iomem *mmio;
1932 u32 irq_stat, irq_masked;
1933
1934 VPRINTK("ENTER\n");
1935
1936 hpriv = host->private_data;
1937 mmio = hpriv->mmio;
1938
1939 /* sigh. 0xffffffff is a valid return from h/w */
1940 irq_stat = readl(mmio + HOST_IRQ_STAT);
1941 if (!irq_stat)
1942 return IRQ_NONE;
1943
1944 irq_masked = irq_stat & hpriv->port_map;
1945
1946 spin_lock(&host->lock);
1947
1948 rc = ahci_handle_port_intr(host, irq_masked);
1949
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001950 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1951 * it should be cleared after all the port events are cleared;
1952 * otherwise, it will raise a spurious interrupt after each
1953 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1954 * information.
1955 *
1956 * Also, use the unmasked value to clear interrupt as spurious
1957 * pending event on a dummy port might cause screaming IRQ.
1958 */
1959 writel(irq_stat, mmio + HOST_IRQ_STAT);
1960
Tejun Heo03e83cb2014-10-27 12:00:01 -04001961 spin_unlock(&host->lock);
1962
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001963 VPRINTK("EXIT\n");
1964
Suman Tripathia129db82015-05-06 00:51:10 +05301965 return IRQ_RETVAL(rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001966}
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001967
Suman Tripathi39e0ee92014-07-07 22:33:04 +05301968unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001969{
1970 struct ata_port *ap = qc->ap;
1971 void __iomem *port_mmio = ahci_port_base(ap);
1972 struct ahci_port_priv *pp = ap->private_data;
1973
1974 /* Keep track of the currently active link. It will be used
1975 * in completion path to determine whether NCQ phase is in
1976 * progress.
1977 */
1978 pp->active_link = qc->dev->link;
1979
Hannes Reinecke179b3102016-07-14 09:05:43 +09001980 if (ata_is_ncq(qc->tf.protocol))
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001981 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1982
1983 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
1984 u32 fbs = readl(port_mmio + PORT_FBS);
1985 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1986 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
1987 writel(fbs, port_mmio + PORT_FBS);
1988 pp->fbs_last_dev = qc->dev->link->pmp;
1989 }
1990
1991 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
1992
1993 ahci_sw_activity(qc->dev->link);
1994
1995 return 0;
1996}
Suman Tripathi39e0ee92014-07-07 22:33:04 +05301997EXPORT_SYMBOL_GPL(ahci_qc_issue);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001998
1999static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2000{
2001 struct ahci_port_priv *pp = qc->ap->private_data;
Tejun Heo6ad60192010-10-15 11:00:08 +02002002 u8 *rx_fis = pp->rx_fis;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002003
2004 if (pp->fbs_enabled)
Tejun Heo6ad60192010-10-15 11:00:08 +02002005 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002006
Tejun Heo6ad60192010-10-15 11:00:08 +02002007 /*
2008 * After a successful execution of an ATA PIO data-in command,
2009 * the device doesn't send D2H Reg FIS to update the TF and
2010 * the host should take TF and E_Status from the preceding PIO
2011 * Setup FIS.
2012 */
2013 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
2014 !(qc->flags & ATA_QCFLAG_FAILED)) {
2015 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
2016 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
2017 } else
2018 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
2019
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002020 return true;
2021}
2022
2023static void ahci_freeze(struct ata_port *ap)
2024{
2025 void __iomem *port_mmio = ahci_port_base(ap);
2026
2027 /* turn IRQ off */
2028 writel(0, port_mmio + PORT_IRQ_MASK);
2029}
2030
2031static void ahci_thaw(struct ata_port *ap)
2032{
2033 struct ahci_host_priv *hpriv = ap->host->private_data;
2034 void __iomem *mmio = hpriv->mmio;
2035 void __iomem *port_mmio = ahci_port_base(ap);
2036 u32 tmp;
2037 struct ahci_port_priv *pp = ap->private_data;
2038
2039 /* clear IRQ */
2040 tmp = readl(port_mmio + PORT_IRQ_STAT);
2041 writel(tmp, port_mmio + PORT_IRQ_STAT);
2042 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2043
2044 /* turn IRQ back on */
2045 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2046}
2047
Richard Zhu8b789d82013-10-15 10:44:54 +08002048void ahci_error_handler(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002049{
Hans de Goede039ece32014-02-22 16:53:30 +01002050 struct ahci_host_priv *hpriv = ap->host->private_data;
2051
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002052 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2053 /* restart engine */
2054 ahci_stop_engine(ap);
Hans de Goede039ece32014-02-22 16:53:30 +01002055 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002056 }
2057
2058 sata_pmp_error_handler(ap);
Tejun Heo0ee71952010-06-07 15:15:08 +02002059
2060 if (!ata_dev_enabled(ap->link.device))
2061 ahci_stop_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002062}
Richard Zhu8b789d82013-10-15 10:44:54 +08002063EXPORT_SYMBOL_GPL(ahci_error_handler);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002064
2065static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2066{
2067 struct ata_port *ap = qc->ap;
2068
2069 /* make DMA engine forget about the failed command */
2070 if (qc->flags & ATA_QCFLAG_FAILED)
2071 ahci_kick_engine(ap);
2072}
2073
Shane Huang65fe1f02012-09-07 22:40:01 +08002074static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2075{
Hans de Goede039ece32014-02-22 16:53:30 +01002076 struct ahci_host_priv *hpriv = ap->host->private_data;
Shane Huang65fe1f02012-09-07 22:40:01 +08002077 void __iomem *port_mmio = ahci_port_base(ap);
2078 struct ata_device *dev = ap->link.device;
2079 u32 devslp, dm, dito, mdat, deto;
2080 int rc;
2081 unsigned int err_mask;
2082
2083 devslp = readl(port_mmio + PORT_DEVSLP);
2084 if (!(devslp & PORT_DEVSLP_DSP)) {
Gabriele Mazzotta95bbbe92015-01-08 19:41:34 +01002085 dev_info(ap->host->dev, "port does not support device sleep\n");
Shane Huang65fe1f02012-09-07 22:40:01 +08002086 return;
2087 }
2088
2089 /* disable device sleep */
2090 if (!sleep) {
2091 if (devslp & PORT_DEVSLP_ADSE) {
2092 writel(devslp & ~PORT_DEVSLP_ADSE,
2093 port_mmio + PORT_DEVSLP);
2094 err_mask = ata_dev_set_feature(dev,
2095 SETFEATURES_SATA_DISABLE,
2096 SATA_DEVSLP);
2097 if (err_mask && err_mask != AC_ERR_DEV)
2098 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2099 }
2100 return;
2101 }
2102
2103 /* device sleep was already enabled */
2104 if (devslp & PORT_DEVSLP_ADSE)
2105 return;
2106
2107 /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
2108 rc = ahci_stop_engine(ap);
2109 if (rc)
2110 return;
2111
2112 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2113 dito = devslp_idle_timeout / (dm + 1);
2114 if (dito > 0x3ff)
2115 dito = 0x3ff;
2116
2117 /* Use the nominal value 10 ms if the read MDAT is zero,
2118 * the nominal value of DETO is 20 ms.
2119 */
Shane Huang803739d2012-12-17 23:18:59 +08002120 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002121 ATA_LOG_DEVSLP_VALID_MASK) {
Shane Huang803739d2012-12-17 23:18:59 +08002122 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002123 ATA_LOG_DEVSLP_MDAT_MASK;
2124 if (!mdat)
2125 mdat = 10;
Shane Huang803739d2012-12-17 23:18:59 +08002126 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
Shane Huang65fe1f02012-09-07 22:40:01 +08002127 if (!deto)
2128 deto = 20;
2129 } else {
2130 mdat = 10;
2131 deto = 20;
2132 }
2133
Srinivas Pandruvadac9b2ba42018-07-02 12:01:53 -07002134 /* Make dito, mdat, deto bits to 0s */
2135 devslp &= ~GENMASK_ULL(24, 2);
Shane Huang65fe1f02012-09-07 22:40:01 +08002136 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2137 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2138 (deto << PORT_DEVSLP_DETO_OFFSET) |
2139 PORT_DEVSLP_ADSE);
2140 writel(devslp, port_mmio + PORT_DEVSLP);
2141
Hans de Goede039ece32014-02-22 16:53:30 +01002142 hpriv->start_engine(ap);
Shane Huang65fe1f02012-09-07 22:40:01 +08002143
2144 /* enable device sleep feature for the drive */
2145 err_mask = ata_dev_set_feature(dev,
2146 SETFEATURES_SATA_ENABLE,
2147 SATA_DEVSLP);
2148 if (err_mask && err_mask != AC_ERR_DEV)
2149 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2150}
2151
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002152static void ahci_enable_fbs(struct ata_port *ap)
2153{
Hans de Goede039ece32014-02-22 16:53:30 +01002154 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002155 struct ahci_port_priv *pp = ap->private_data;
2156 void __iomem *port_mmio = ahci_port_base(ap);
2157 u32 fbs;
2158 int rc;
2159
2160 if (!pp->fbs_supported)
2161 return;
2162
2163 fbs = readl(port_mmio + PORT_FBS);
2164 if (fbs & PORT_FBS_EN) {
2165 pp->fbs_enabled = true;
2166 pp->fbs_last_dev = -1; /* initialization */
2167 return;
2168 }
2169
2170 rc = ahci_stop_engine(ap);
2171 if (rc)
2172 return;
2173
2174 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2175 fbs = readl(port_mmio + PORT_FBS);
2176 if (fbs & PORT_FBS_EN) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002177 dev_info(ap->host->dev, "FBS is enabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002178 pp->fbs_enabled = true;
2179 pp->fbs_last_dev = -1; /* initialization */
2180 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002181 dev_err(ap->host->dev, "Failed to enable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002182
Hans de Goede039ece32014-02-22 16:53:30 +01002183 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002184}
2185
2186static void ahci_disable_fbs(struct ata_port *ap)
2187{
Hans de Goede039ece32014-02-22 16:53:30 +01002188 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002189 struct ahci_port_priv *pp = ap->private_data;
2190 void __iomem *port_mmio = ahci_port_base(ap);
2191 u32 fbs;
2192 int rc;
2193
2194 if (!pp->fbs_supported)
2195 return;
2196
2197 fbs = readl(port_mmio + PORT_FBS);
2198 if ((fbs & PORT_FBS_EN) == 0) {
2199 pp->fbs_enabled = false;
2200 return;
2201 }
2202
2203 rc = ahci_stop_engine(ap);
2204 if (rc)
2205 return;
2206
2207 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2208 fbs = readl(port_mmio + PORT_FBS);
2209 if (fbs & PORT_FBS_EN)
Joe Perchesa44fec12011-04-15 15:51:58 -07002210 dev_err(ap->host->dev, "Failed to disable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002211 else {
Joe Perchesa44fec12011-04-15 15:51:58 -07002212 dev_info(ap->host->dev, "FBS is disabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002213 pp->fbs_enabled = false;
2214 }
2215
Hans de Goede039ece32014-02-22 16:53:30 +01002216 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002217}
2218
2219static void ahci_pmp_attach(struct ata_port *ap)
2220{
2221 void __iomem *port_mmio = ahci_port_base(ap);
2222 struct ahci_port_priv *pp = ap->private_data;
2223 u32 cmd;
2224
2225 cmd = readl(port_mmio + PORT_CMD);
2226 cmd |= PORT_CMD_PMP;
2227 writel(cmd, port_mmio + PORT_CMD);
2228
2229 ahci_enable_fbs(ap);
2230
2231 pp->intr_mask |= PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002232
2233 /*
2234 * We must not change the port interrupt mask register if the
2235 * port is marked frozen, the value in pp->intr_mask will be
2236 * restored later when the port is thawed.
2237 *
2238 * Note that during initialization, the port is marked as
2239 * frozen since the irq handler is not yet registered.
2240 */
2241 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2242 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002243}
2244
2245static void ahci_pmp_detach(struct ata_port *ap)
2246{
2247 void __iomem *port_mmio = ahci_port_base(ap);
2248 struct ahci_port_priv *pp = ap->private_data;
2249 u32 cmd;
2250
2251 ahci_disable_fbs(ap);
2252
2253 cmd = readl(port_mmio + PORT_CMD);
2254 cmd &= ~PORT_CMD_PMP;
2255 writel(cmd, port_mmio + PORT_CMD);
2256
2257 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002258
2259 /* see comment above in ahci_pmp_attach() */
2260 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2261 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002262}
2263
David Milburn02cdfcf2010-11-12 15:38:21 -06002264int ahci_port_resume(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002265{
Mika Westerbergbb03c642016-02-18 10:54:16 +02002266 ahci_rpm_get_port(ap);
2267
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002268 ahci_power_up(ap);
2269 ahci_start_port(ap);
2270
2271 if (sata_pmp_attached(ap))
2272 ahci_pmp_attach(ap);
2273 else
2274 ahci_pmp_detach(ap);
2275
2276 return 0;
2277}
David Milburn02cdfcf2010-11-12 15:38:21 -06002278EXPORT_SYMBOL_GPL(ahci_port_resume);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002279
2280#ifdef CONFIG_PM
2281static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2282{
2283 const char *emsg = NULL;
2284 int rc;
2285
2286 rc = ahci_deinit_port(ap, &emsg);
2287 if (rc == 0)
2288 ahci_power_down(ap);
2289 else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002290 ata_port_err(ap, "%s (%d)\n", emsg, rc);
Tejun Heo7faa33d2011-07-22 11:41:26 +02002291 ata_port_freeze(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002292 }
2293
Mika Westerbergbb03c642016-02-18 10:54:16 +02002294 ahci_rpm_put_port(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002295 return rc;
2296}
2297#endif
2298
2299static int ahci_port_start(struct ata_port *ap)
2300{
2301 struct ahci_host_priv *hpriv = ap->host->private_data;
2302 struct device *dev = ap->host->dev;
2303 struct ahci_port_priv *pp;
2304 void *mem;
2305 dma_addr_t mem_dma;
2306 size_t dma_sz, rx_fis_sz;
2307
2308 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2309 if (!pp)
2310 return -ENOMEM;
2311
Alexander Gordeevb29900e2013-05-22 08:53:48 +09002312 if (ap->host->n_ports > 1) {
2313 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2314 if (!pp->irq_desc) {
2315 devm_kfree(dev, pp);
2316 return -ENOMEM;
2317 }
2318 snprintf(pp->irq_desc, 8,
2319 "%s%d", dev_driver_string(dev), ap->port_no);
2320 }
2321
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002322 /* check FBS capability */
2323 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2324 void __iomem *port_mmio = ahci_port_base(ap);
2325 u32 cmd = readl(port_mmio + PORT_CMD);
2326 if (cmd & PORT_CMD_FBSCP)
2327 pp->fbs_supported = true;
Tejun Heo5f173102010-07-24 16:53:48 +02002328 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002329 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2330 ap->port_no);
Tejun Heo5f173102010-07-24 16:53:48 +02002331 pp->fbs_supported = true;
2332 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002333 dev_warn(dev, "port %d is not capable of FBS\n",
2334 ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002335 }
2336
2337 if (pp->fbs_supported) {
2338 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2339 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2340 } else {
2341 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2342 rx_fis_sz = AHCI_RX_FIS_SZ;
2343 }
2344
2345 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2346 if (!mem)
2347 return -ENOMEM;
2348 memset(mem, 0, dma_sz);
2349
2350 /*
2351 * First item in chunk of DMA memory: 32-slot command table,
2352 * 32 bytes each in size
2353 */
2354 pp->cmd_slot = mem;
2355 pp->cmd_slot_dma = mem_dma;
2356
2357 mem += AHCI_CMD_SLOT_SZ;
2358 mem_dma += AHCI_CMD_SLOT_SZ;
2359
2360 /*
2361 * Second item: Received-FIS area
2362 */
2363 pp->rx_fis = mem;
2364 pp->rx_fis_dma = mem_dma;
2365
2366 mem += rx_fis_sz;
2367 mem_dma += rx_fis_sz;
2368
2369 /*
2370 * Third item: data area for storing a single command
2371 * and its scatter-gather table
2372 */
2373 pp->cmd_tbl = mem;
2374 pp->cmd_tbl_dma = mem_dma;
2375
2376 /*
2377 * Save off initial list of interrupts to be enabled.
2378 * This could be changed later
2379 */
2380 pp->intr_mask = DEF_PORT_IRQ;
2381
Tejun Heo7865f832014-10-27 09:50:36 -04002382 /*
2383 * Switch to per-port locking in case each port has its own MSI vector.
2384 */
Christoph Hellwig0b9e29882016-09-05 17:21:45 +02002385 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
Tejun Heo7865f832014-10-27 09:50:36 -04002386 spin_lock_init(&pp->lock);
2387 ap->lock = &pp->lock;
2388 }
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01002389
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002390 ap->private_data = pp;
2391
2392 /* engage engines, captain */
2393 return ahci_port_resume(ap);
2394}
2395
2396static void ahci_port_stop(struct ata_port *ap)
2397{
2398 const char *emsg = NULL;
Pang Raymond05169002016-07-20 12:13:46 +00002399 struct ahci_host_priv *hpriv = ap->host->private_data;
2400 void __iomem *host_mmio = hpriv->mmio;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002401 int rc;
2402
2403 /* de-initialize port */
2404 rc = ahci_deinit_port(ap, &emsg);
2405 if (rc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002406 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
Pang Raymond05169002016-07-20 12:13:46 +00002407
2408 /*
2409 * Clear GHC.IS to prevent stuck INTx after disabling MSI and
2410 * re-enabling INTx.
2411 */
2412 writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002413}
2414
2415void ahci_print_info(struct ata_host *host, const char *scc_s)
2416{
2417 struct ahci_host_priv *hpriv = host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002418 u32 vers, cap, cap2, impl, speed;
2419 const char *speed_s;
2420
Mika Westerberg8ea909c2016-02-18 10:54:14 +02002421 vers = hpriv->version;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002422 cap = hpriv->cap;
2423 cap2 = hpriv->cap2;
2424 impl = hpriv->port_map;
2425
2426 speed = (cap >> 20) & 0xf;
2427 if (speed == 1)
2428 speed_s = "1.5";
2429 else if (speed == 2)
2430 speed_s = "3";
2431 else if (speed == 3)
2432 speed_s = "6";
2433 else
2434 speed_s = "?";
2435
2436 dev_info(host->dev,
2437 "AHCI %02x%02x.%02x%02x "
2438 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2439 ,
2440
2441 (vers >> 24) & 0xff,
2442 (vers >> 16) & 0xff,
2443 (vers >> 8) & 0xff,
2444 vers & 0xff,
2445
2446 ((cap >> 8) & 0x1f) + 1,
2447 (cap & 0x1f) + 1,
2448 speed_s,
2449 impl,
2450 scc_s);
2451
2452 dev_info(host->dev,
2453 "flags: "
2454 "%s%s%s%s%s%s%s"
2455 "%s%s%s%s%s%s%s"
Shane Huang65fe1f02012-09-07 22:40:01 +08002456 "%s%s%s%s%s%s%s"
2457 "%s%s\n"
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002458 ,
2459
2460 cap & HOST_CAP_64 ? "64bit " : "",
2461 cap & HOST_CAP_NCQ ? "ncq " : "",
2462 cap & HOST_CAP_SNTF ? "sntf " : "",
2463 cap & HOST_CAP_MPS ? "ilck " : "",
2464 cap & HOST_CAP_SSS ? "stag " : "",
2465 cap & HOST_CAP_ALPM ? "pm " : "",
2466 cap & HOST_CAP_LED ? "led " : "",
2467 cap & HOST_CAP_CLO ? "clo " : "",
2468 cap & HOST_CAP_ONLY ? "only " : "",
2469 cap & HOST_CAP_PMP ? "pmp " : "",
2470 cap & HOST_CAP_FBS ? "fbs " : "",
2471 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2472 cap & HOST_CAP_SSC ? "slum " : "",
2473 cap & HOST_CAP_PART ? "part " : "",
2474 cap & HOST_CAP_CCC ? "ccc " : "",
2475 cap & HOST_CAP_EMS ? "ems " : "",
2476 cap & HOST_CAP_SXS ? "sxs " : "",
Shane Huang65fe1f02012-09-07 22:40:01 +08002477 cap2 & HOST_CAP2_DESO ? "deso " : "",
2478 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2479 cap2 & HOST_CAP2_SDS ? "sds " : "",
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002480 cap2 & HOST_CAP2_APST ? "apst " : "",
2481 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2482 cap2 & HOST_CAP2_BOH ? "boh " : ""
2483 );
2484}
2485EXPORT_SYMBOL_GPL(ahci_print_info);
2486
2487void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2488 struct ata_port_info *pi)
2489{
2490 u8 messages;
2491 void __iomem *mmio = hpriv->mmio;
2492 u32 em_loc = readl(mmio + HOST_EM_LOC);
2493 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2494
2495 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2496 return;
2497
2498 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2499
Harry Zhang008dbd62010-04-23 17:27:19 +08002500 if (messages) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002501 /* store em_loc */
2502 hpriv->em_loc = ((em_loc >> 16) * 4);
Harry Zhangc0623162010-04-23 17:28:38 +08002503 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
Harry Zhang008dbd62010-04-23 17:27:19 +08002504 hpriv->em_msg_type = messages;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002505 pi->flags |= ATA_FLAG_EM;
2506 if (!(em_ctl & EM_CTL_ALHD))
2507 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2508 }
2509}
2510EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2511
Dan Williamsd684a902015-11-11 16:27:33 -08002512static int ahci_host_activate_multi_irqs(struct ata_host *host,
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002513 struct scsi_host_template *sht)
Alexander Gordeev1c628542014-09-29 18:25:58 +02002514{
Dan Williamsd684a902015-11-11 16:27:33 -08002515 struct ahci_host_priv *hpriv = host->private_data;
Alexander Gordeev1c628542014-09-29 18:25:58 +02002516 int i, rc;
2517
2518 rc = ata_host_start(host);
2519 if (rc)
2520 return rc;
Robert Richter21bfd1a2015-05-31 13:55:18 +02002521 /*
2522 * Requests IRQs according to AHCI-1.1 when multiple MSIs were
2523 * allocated. That is one MSI per port, starting from @irq.
2524 */
Alexander Gordeev1c628542014-09-29 18:25:58 +02002525 for (i = 0; i < host->n_ports; i++) {
2526 struct ahci_port_priv *pp = host->ports[i]->private_data;
Christoph Hellwig0b9e29882016-09-05 17:21:45 +02002527 int irq = hpriv->get_irq_vector(host, i);
Alexander Gordeev1c628542014-09-29 18:25:58 +02002528
2529 /* Do not receive interrupts sent by dummy ports */
2530 if (!pp) {
Christoph Hellwig9b4b3f62016-08-11 07:26:01 -07002531 disable_irq(irq);
Alexander Gordeev1c628542014-09-29 18:25:58 +02002532 continue;
2533 }
2534
Dan Williamsa6b7fb72015-11-11 16:27:38 -08002535 rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2536 0, pp->irq_desc, host->ports[i]);
2537
Alexander Gordeev1c628542014-09-29 18:25:58 +02002538 if (rc)
Dan Williams0a142b22015-10-30 17:09:35 -04002539 return rc;
Dan Williamsd684a902015-11-11 16:27:33 -08002540 ata_port_desc(host->ports[i], "irq %d", irq);
Dan Williams0a142b22015-10-30 17:09:35 -04002541 }
Dan Williamsd684a902015-11-11 16:27:33 -08002542
Dan Williams0a142b22015-10-30 17:09:35 -04002543 return ata_host_register(host, sht);
Alexander Gordeev1c628542014-09-29 18:25:58 +02002544}
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002545
2546/**
2547 * ahci_host_activate - start AHCI host, request IRQs and register it
2548 * @host: target ATA host
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002549 * @sht: scsi_host_template to use when registering the host
2550 *
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002551 * LOCKING:
2552 * Inherited from calling layer (may sleep).
2553 *
2554 * RETURNS:
2555 * 0 on success, -errno otherwise.
2556 */
Robert Richter21bfd1a2015-05-31 13:55:18 +02002557int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002558{
2559 struct ahci_host_priv *hpriv = host->private_data;
Robert Richter21bfd1a2015-05-31 13:55:18 +02002560 int irq = hpriv->irq;
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002561 int rc;
2562
Christoph Hellwig0b9e29882016-09-05 17:21:45 +02002563 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
Suman Tripathif070d672016-02-06 11:25:22 +05302564 if (hpriv->irq_handler)
Sander Eikelenboomd991c872016-03-20 22:27:06 +01002565 dev_warn(host->dev,
2566 "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
Christoph Hellwig0b9e29882016-09-05 17:21:45 +02002567 if (!hpriv->get_irq_vector) {
2568 dev_err(host->dev,
2569 "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
2570 return -EIO;
2571 }
Suman Tripathif070d672016-02-06 11:25:22 +05302572
Dan Williamsd684a902015-11-11 16:27:33 -08002573 rc = ahci_host_activate_multi_irqs(host, sht);
Suman Tripathif070d672016-02-06 11:25:22 +05302574 } else {
2575 rc = ata_host_activate(host, irq, hpriv->irq_handler,
Suman Tripathi5903b162015-05-06 00:51:11 +05302576 IRQF_SHARED, sht);
Suman Tripathif070d672016-02-06 11:25:22 +05302577 }
2578
2579
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002580 return rc;
2581}
Alexander Gordeev1c628542014-09-29 18:25:58 +02002582EXPORT_SYMBOL_GPL(ahci_host_activate);
2583
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002584MODULE_AUTHOR("Jeff Garzik");
2585MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2586MODULE_LICENSE("GPL");