blob: 513b3fa74d7886949419448cc18a591cf4bcd4cd [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>
Anton Vorontsov365cfa12010-03-28 00:22:14 -040038#include <linux/blkdev.h>
39#include <linux/delay.h>
40#include <linux/interrupt.h>
41#include <linux/dma-mapping.h>
42#include <linux/device.h>
43#include <scsi/scsi_host.h>
44#include <scsi/scsi_cmnd.h>
45#include <linux/libata.h>
Dan Williamsd684a902015-11-11 16:27:33 -080046#include <linux/pci.h>
Anton Vorontsov365cfa12010-03-28 00:22:14 -040047#include "ahci.h"
Shane Huang65fe1f02012-09-07 22:40:01 +080048#include "libata.h"
Anton Vorontsov365cfa12010-03-28 00:22:14 -040049
50static int ahci_skip_host_reset;
51int ahci_ignore_sss;
52EXPORT_SYMBOL_GPL(ahci_ignore_sss);
53
54module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
55MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
56
57module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
58MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
59
Tejun Heo6b7ae952010-09-01 17:50:06 +020060static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
61 unsigned hints);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040062static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
63static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
64 size_t size);
65static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
66 ssize_t size);
67
68
69
70static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
71static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040072static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
73static int ahci_port_start(struct ata_port *ap);
74static void ahci_port_stop(struct ata_port *ap);
75static void ahci_qc_prep(struct ata_queued_cmd *qc);
76static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
77static void ahci_freeze(struct ata_port *ap);
78static void ahci_thaw(struct ata_port *ap);
Shane Huang65fe1f02012-09-07 22:40:01 +080079static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040080static void ahci_enable_fbs(struct ata_port *ap);
81static void ahci_disable_fbs(struct ata_port *ap);
82static void ahci_pmp_attach(struct ata_port *ap);
83static void ahci_pmp_detach(struct ata_port *ap);
84static int ahci_softreset(struct ata_link *link, unsigned int *class,
85 unsigned long deadline);
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +080086static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
87 unsigned long deadline);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040088static int ahci_hardreset(struct ata_link *link, unsigned int *class,
89 unsigned long deadline);
90static void ahci_postreset(struct ata_link *link, unsigned int *class);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040091static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040092static void ahci_dev_config(struct ata_device *dev);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040093#ifdef CONFIG_PM
94static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
95#endif
96static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
97static ssize_t ahci_activity_store(struct ata_device *dev,
98 enum sw_activity val);
99static void ahci_init_sw_activity(struct ata_link *link);
100
101static ssize_t ahci_show_host_caps(struct device *dev,
102 struct device_attribute *attr, char *buf);
103static ssize_t ahci_show_host_cap2(struct device *dev,
104 struct device_attribute *attr, char *buf);
105static ssize_t ahci_show_host_version(struct device *dev,
106 struct device_attribute *attr, char *buf);
107static ssize_t ahci_show_port_cmd(struct device *dev,
108 struct device_attribute *attr, char *buf);
Harry Zhangc0623162010-04-23 17:28:38 +0800109static ssize_t ahci_read_em_buffer(struct device *dev,
110 struct device_attribute *attr, char *buf);
111static ssize_t ahci_store_em_buffer(struct device *dev,
112 struct device_attribute *attr,
113 const char *buf, size_t size);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100114static ssize_t ahci_show_em_supported(struct device *dev,
115 struct device_attribute *attr, char *buf);
Suman Tripathif070d672016-02-06 11:25:22 +0530116static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400117
118static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
119static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
120static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
121static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
Harry Zhangc0623162010-04-23 17:28:38 +0800122static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
123 ahci_read_em_buffer, ahci_store_em_buffer);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100124static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400125
Tejun Heofad16e72010-09-21 09:25:48 +0200126struct device_attribute *ahci_shost_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400127 &dev_attr_link_power_management_policy,
128 &dev_attr_em_message_type,
129 &dev_attr_em_message,
130 &dev_attr_ahci_host_caps,
131 &dev_attr_ahci_host_cap2,
132 &dev_attr_ahci_host_version,
133 &dev_attr_ahci_port_cmd,
Harry Zhangc0623162010-04-23 17:28:38 +0800134 &dev_attr_em_buffer,
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100135 &dev_attr_em_message_supported,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400136 NULL
137};
Tejun Heofad16e72010-09-21 09:25:48 +0200138EXPORT_SYMBOL_GPL(ahci_shost_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400139
Tejun Heofad16e72010-09-21 09:25:48 +0200140struct device_attribute *ahci_sdev_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400141 &dev_attr_sw_activity,
142 &dev_attr_unload_heads,
143 NULL
144};
Tejun Heofad16e72010-09-21 09:25:48 +0200145EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400146
147struct ata_port_operations ahci_ops = {
148 .inherits = &sata_pmp_port_ops,
149
150 .qc_defer = ahci_pmp_qc_defer,
151 .qc_prep = ahci_qc_prep,
152 .qc_issue = ahci_qc_issue,
153 .qc_fill_rtf = ahci_qc_fill_rtf,
154
155 .freeze = ahci_freeze,
156 .thaw = ahci_thaw,
157 .softreset = ahci_softreset,
158 .hardreset = ahci_hardreset,
159 .postreset = ahci_postreset,
160 .pmp_softreset = ahci_softreset,
161 .error_handler = ahci_error_handler,
162 .post_internal_cmd = ahci_post_internal_cmd,
163 .dev_config = ahci_dev_config,
164
165 .scr_read = ahci_scr_read,
166 .scr_write = ahci_scr_write,
167 .pmp_attach = ahci_pmp_attach,
168 .pmp_detach = ahci_pmp_detach,
169
Tejun Heo6b7ae952010-09-01 17:50:06 +0200170 .set_lpm = ahci_set_lpm,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400171 .em_show = ahci_led_show,
172 .em_store = ahci_led_store,
173 .sw_activity_show = ahci_activity_show,
174 .sw_activity_store = ahci_activity_store,
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500175 .transmit_led_message = ahci_transmit_led_message,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400176#ifdef CONFIG_PM
177 .port_suspend = ahci_port_suspend,
178 .port_resume = ahci_port_resume,
179#endif
180 .port_start = ahci_port_start,
181 .port_stop = ahci_port_stop,
182};
183EXPORT_SYMBOL_GPL(ahci_ops);
184
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +0800185struct ata_port_operations ahci_pmp_retry_srst_ops = {
186 .inherits = &ahci_ops,
187 .softreset = ahci_pmp_retry_softreset,
188};
189EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
190
Chuansheng Liued08d402013-09-18 20:21:49 +0800191static bool ahci_em_messages __read_mostly = true;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400192EXPORT_SYMBOL_GPL(ahci_em_messages);
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
228static ssize_t ahci_show_host_caps(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 struct Scsi_Host *shost = class_to_shost(dev);
232 struct ata_port *ap = ata_shost_to_port(shost);
233 struct ahci_host_priv *hpriv = ap->host->private_data;
234
235 return sprintf(buf, "%x\n", hpriv->cap);
236}
237
238static ssize_t ahci_show_host_cap2(struct device *dev,
239 struct device_attribute *attr, char *buf)
240{
241 struct Scsi_Host *shost = class_to_shost(dev);
242 struct ata_port *ap = ata_shost_to_port(shost);
243 struct ahci_host_priv *hpriv = ap->host->private_data;
244
245 return sprintf(buf, "%x\n", hpriv->cap2);
246}
247
248static ssize_t ahci_show_host_version(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 struct Scsi_Host *shost = class_to_shost(dev);
252 struct ata_port *ap = ata_shost_to_port(shost);
253 struct ahci_host_priv *hpriv = ap->host->private_data;
254 void __iomem *mmio = hpriv->mmio;
255
256 return sprintf(buf, "%x\n", readl(mmio + HOST_VERSION));
257}
258
259static ssize_t ahci_show_port_cmd(struct device *dev,
260 struct device_attribute *attr, char *buf)
261{
262 struct Scsi_Host *shost = class_to_shost(dev);
263 struct ata_port *ap = ata_shost_to_port(shost);
264 void __iomem *port_mmio = ahci_port_base(ap);
265
266 return sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
267}
268
Harry Zhangc0623162010-04-23 17:28:38 +0800269static ssize_t ahci_read_em_buffer(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
272 struct Scsi_Host *shost = class_to_shost(dev);
273 struct ata_port *ap = ata_shost_to_port(shost);
274 struct ahci_host_priv *hpriv = ap->host->private_data;
275 void __iomem *mmio = hpriv->mmio;
276 void __iomem *em_mmio = mmio + hpriv->em_loc;
277 u32 em_ctl, msg;
278 unsigned long flags;
279 size_t count;
280 int i;
281
282 spin_lock_irqsave(ap->lock, flags);
283
284 em_ctl = readl(mmio + HOST_EM_CTL);
285 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
286 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
287 spin_unlock_irqrestore(ap->lock, flags);
288 return -EINVAL;
289 }
290
291 if (!(em_ctl & EM_CTL_MR)) {
292 spin_unlock_irqrestore(ap->lock, flags);
293 return -EAGAIN;
294 }
295
296 if (!(em_ctl & EM_CTL_SMB))
297 em_mmio += hpriv->em_buf_sz;
298
299 count = hpriv->em_buf_sz;
300
301 /* the count should not be larger than PAGE_SIZE */
302 if (count > PAGE_SIZE) {
303 if (printk_ratelimit())
Joe Perchesa9a79df2011-04-15 15:51:59 -0700304 ata_port_warn(ap,
305 "EM read buffer size too large: "
306 "buffer size %u, page size %lu\n",
307 hpriv->em_buf_sz, PAGE_SIZE);
Harry Zhangc0623162010-04-23 17:28:38 +0800308 count = PAGE_SIZE;
309 }
310
311 for (i = 0; i < count; i += 4) {
312 msg = readl(em_mmio + i);
313 buf[i] = msg & 0xff;
314 buf[i + 1] = (msg >> 8) & 0xff;
315 buf[i + 2] = (msg >> 16) & 0xff;
316 buf[i + 3] = (msg >> 24) & 0xff;
317 }
318
319 spin_unlock_irqrestore(ap->lock, flags);
320
321 return i;
322}
323
324static ssize_t ahci_store_em_buffer(struct device *dev,
325 struct device_attribute *attr,
326 const char *buf, size_t size)
327{
328 struct Scsi_Host *shost = class_to_shost(dev);
329 struct ata_port *ap = ata_shost_to_port(shost);
330 struct ahci_host_priv *hpriv = ap->host->private_data;
331 void __iomem *mmio = hpriv->mmio;
332 void __iomem *em_mmio = mmio + hpriv->em_loc;
Harry Zhangf9ce8892010-06-24 11:34:23 +0800333 const unsigned char *msg_buf = buf;
Harry Zhangc0623162010-04-23 17:28:38 +0800334 u32 em_ctl, msg;
335 unsigned long flags;
336 int i;
337
338 /* check size validity */
339 if (!(ap->flags & ATA_FLAG_EM) ||
340 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
341 size % 4 || size > hpriv->em_buf_sz)
342 return -EINVAL;
343
344 spin_lock_irqsave(ap->lock, flags);
345
346 em_ctl = readl(mmio + HOST_EM_CTL);
347 if (em_ctl & EM_CTL_TM) {
348 spin_unlock_irqrestore(ap->lock, flags);
349 return -EBUSY;
350 }
351
352 for (i = 0; i < size; i += 4) {
Harry Zhangf9ce8892010-06-24 11:34:23 +0800353 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
354 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
Harry Zhangc0623162010-04-23 17:28:38 +0800355 writel(msg, em_mmio + i);
356 }
357
358 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
359
360 spin_unlock_irqrestore(ap->lock, flags);
361
362 return size;
363}
364
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100365static ssize_t ahci_show_em_supported(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
368 struct Scsi_Host *shost = class_to_shost(dev);
369 struct ata_port *ap = ata_shost_to_port(shost);
370 struct ahci_host_priv *hpriv = ap->host->private_data;
371 void __iomem *mmio = hpriv->mmio;
372 u32 em_ctl;
373
374 em_ctl = readl(mmio + HOST_EM_CTL);
375
376 return sprintf(buf, "%s%s%s%s\n",
377 em_ctl & EM_CTL_LED ? "led " : "",
378 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
379 em_ctl & EM_CTL_SES ? "ses-2 " : "",
380 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
381}
382
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400383/**
384 * ahci_save_initial_config - Save and fixup initial config values
385 * @dev: target AHCI device
386 * @hpriv: host private area to store config values
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400387 *
388 * Some registers containing configuration info might be setup by
389 * BIOS and might be cleared on reset. This function saves the
390 * initial values of those registers into @hpriv such that they
391 * can be restored after controller reset.
392 *
393 * If inconsistent, config values are fixed up by this function.
394 *
Hans de Goede039ece32014-02-22 16:53:30 +0100395 * If it is not set already this function sets hpriv->start_engine to
396 * ahci_start_engine.
397 *
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400398 * LOCKING:
399 * None.
400 */
Antoine Ténart725c7b52014-07-30 20:13:56 +0200401void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400402{
403 void __iomem *mmio = hpriv->mmio;
404 u32 cap, cap2, vers, port_map;
405 int i;
406
407 /* make sure AHCI mode is enabled before accessing CAP */
408 ahci_enable_ahci(mmio);
409
410 /* Values prefixed with saved_ are written back to host after
411 * reset. Values without are used for driver operation.
412 */
413 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
414 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
415
416 /* CAP2 register is only defined for AHCI 1.2 and later */
417 vers = readl(mmio + HOST_VERSION);
418 if ((vers >> 16) > 1 ||
419 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
420 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
421 else
422 hpriv->saved_cap2 = cap2 = 0;
423
424 /* some chips have errata preventing 64bit use */
425 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700426 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400427 cap &= ~HOST_CAP_64;
428 }
429
430 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700431 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400432 cap &= ~HOST_CAP_NCQ;
433 }
434
435 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700436 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400437 cap |= HOST_CAP_NCQ;
438 }
439
440 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700441 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400442 cap &= ~HOST_CAP_PMP;
443 }
444
445 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700446 dev_info(dev,
447 "controller can't do SNTF, turning off CAP_SNTF\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400448 cap &= ~HOST_CAP_SNTF;
449 }
450
Jacob Pan0cf4a7d2014-04-15 22:27:11 -0700451 if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
452 dev_info(dev,
453 "controller can't do DEVSLP, turning off\n");
454 cap2 &= ~HOST_CAP2_SDS;
455 cap2 &= ~HOST_CAP2_SADM;
456 }
457
Tejun Heo5f173102010-07-24 16:53:48 +0200458 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700459 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
Tejun Heo5f173102010-07-24 16:53:48 +0200460 cap |= HOST_CAP_FBS;
461 }
462
Kefeng Wang888d91a2014-05-14 14:13:40 +0800463 if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
464 dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
465 cap &= ~HOST_CAP_FBS;
466 }
467
Antoine Ténart725c7b52014-07-30 20:13:56 +0200468 if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700469 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
Antoine Ténart725c7b52014-07-30 20:13:56 +0200470 port_map, hpriv->force_port_map);
471 port_map = hpriv->force_port_map;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400472 }
473
Antoine Ténart725c7b52014-07-30 20:13:56 +0200474 if (hpriv->mask_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700475 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
476 port_map,
Antoine Ténart725c7b52014-07-30 20:13:56 +0200477 port_map & hpriv->mask_port_map);
478 port_map &= hpriv->mask_port_map;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400479 }
480
481 /* cross check port_map and cap.n_ports */
482 if (port_map) {
483 int map_ports = 0;
484
485 for (i = 0; i < AHCI_MAX_PORTS; i++)
486 if (port_map & (1 << i))
487 map_ports++;
488
489 /* If PI has more ports than n_ports, whine, clear
490 * port_map and let it be generated from n_ports.
491 */
492 if (map_ports > ahci_nr_ports(cap)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700493 dev_warn(dev,
494 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
495 port_map, ahci_nr_ports(cap));
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400496 port_map = 0;
497 }
498 }
499
Tejun Heo566d1822016-01-15 15:13:05 -0500500 /* fabricate port_map from cap.nr_ports for < AHCI 1.3 */
501 if (!port_map && vers < 0x10300) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400502 port_map = (1 << ahci_nr_ports(cap)) - 1;
Joe Perchesa44fec12011-04-15 15:51:58 -0700503 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400504
505 /* write the fixed up value to the PI register */
506 hpriv->saved_port_map = port_map;
507 }
508
509 /* record values to use during operation */
510 hpriv->cap = cap;
511 hpriv->cap2 = cap2;
512 hpriv->port_map = port_map;
Hans de Goede039ece32014-02-22 16:53:30 +0100513
514 if (!hpriv->start_engine)
515 hpriv->start_engine = ahci_start_engine;
Suman Tripathif070d672016-02-06 11:25:22 +0530516
517 if (!hpriv->irq_handler)
Suman Tripathid867b952016-02-06 11:25:23 +0530518 hpriv->irq_handler = ahci_single_level_irq_intr;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400519}
520EXPORT_SYMBOL_GPL(ahci_save_initial_config);
521
522/**
523 * ahci_restore_initial_config - Restore initial config
524 * @host: target ATA host
525 *
526 * Restore initial config stored by ahci_save_initial_config().
527 *
528 * LOCKING:
529 * None.
530 */
531static void ahci_restore_initial_config(struct ata_host *host)
532{
533 struct ahci_host_priv *hpriv = host->private_data;
534 void __iomem *mmio = hpriv->mmio;
535
536 writel(hpriv->saved_cap, mmio + HOST_CAP);
537 if (hpriv->saved_cap2)
538 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
539 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
540 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
541}
542
543static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
544{
545 static const int offset[] = {
546 [SCR_STATUS] = PORT_SCR_STAT,
547 [SCR_CONTROL] = PORT_SCR_CTL,
548 [SCR_ERROR] = PORT_SCR_ERR,
549 [SCR_ACTIVE] = PORT_SCR_ACT,
550 [SCR_NOTIFICATION] = PORT_SCR_NTF,
551 };
552 struct ahci_host_priv *hpriv = ap->host->private_data;
553
554 if (sc_reg < ARRAY_SIZE(offset) &&
555 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
556 return offset[sc_reg];
557 return 0;
558}
559
560static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
561{
562 void __iomem *port_mmio = ahci_port_base(link->ap);
563 int offset = ahci_scr_offset(link->ap, sc_reg);
564
565 if (offset) {
566 *val = readl(port_mmio + offset);
567 return 0;
568 }
569 return -EINVAL;
570}
571
572static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
573{
574 void __iomem *port_mmio = ahci_port_base(link->ap);
575 int offset = ahci_scr_offset(link->ap, sc_reg);
576
577 if (offset) {
578 writel(val, port_mmio + offset);
579 return 0;
580 }
581 return -EINVAL;
582}
583
584void ahci_start_engine(struct ata_port *ap)
585{
586 void __iomem *port_mmio = ahci_port_base(ap);
587 u32 tmp;
588
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400589 /* start DMA */
590 tmp = readl(port_mmio + PORT_CMD);
591 tmp |= PORT_CMD_START;
592 writel(tmp, port_mmio + PORT_CMD);
593 readl(port_mmio + PORT_CMD); /* flush */
594}
595EXPORT_SYMBOL_GPL(ahci_start_engine);
596
597int ahci_stop_engine(struct ata_port *ap)
598{
599 void __iomem *port_mmio = ahci_port_base(ap);
Danesh Petigarafb329632016-01-11 13:22:26 -0800600 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400601 u32 tmp;
602
Danesh Petigarafb329632016-01-11 13:22:26 -0800603 /*
604 * On some controllers, stopping a port's DMA engine while the port
605 * is in ALPM state (partial or slumber) results in failures on
606 * subsequent DMA engine starts. For those controllers, put the
607 * port back in active state before stopping its DMA engine.
608 */
609 if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
610 (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
611 ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
612 dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
613 return -EIO;
614 }
615
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400616 tmp = readl(port_mmio + PORT_CMD);
617
618 /* check if the HBA is idle */
619 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
620 return 0;
621
622 /* setting HBA to idle */
623 tmp &= ~PORT_CMD_START;
624 writel(tmp, port_mmio + PORT_CMD);
625
626 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo97750ce2010-09-06 17:56:29 +0200627 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400628 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
629 if (tmp & PORT_CMD_LIST_ON)
630 return -EIO;
631
632 return 0;
633}
634EXPORT_SYMBOL_GPL(ahci_stop_engine);
635
Suman Tripathi39e0ee92014-07-07 22:33:04 +0530636void ahci_start_fis_rx(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400637{
638 void __iomem *port_mmio = ahci_port_base(ap);
639 struct ahci_host_priv *hpriv = ap->host->private_data;
640 struct ahci_port_priv *pp = ap->private_data;
641 u32 tmp;
642
643 /* set FIS registers */
644 if (hpriv->cap & HOST_CAP_64)
645 writel((pp->cmd_slot_dma >> 16) >> 16,
646 port_mmio + PORT_LST_ADDR_HI);
647 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
648
649 if (hpriv->cap & HOST_CAP_64)
650 writel((pp->rx_fis_dma >> 16) >> 16,
651 port_mmio + PORT_FIS_ADDR_HI);
652 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
653
654 /* enable FIS reception */
655 tmp = readl(port_mmio + PORT_CMD);
656 tmp |= PORT_CMD_FIS_RX;
657 writel(tmp, port_mmio + PORT_CMD);
658
659 /* flush */
660 readl(port_mmio + PORT_CMD);
661}
Suman Tripathi39e0ee92014-07-07 22:33:04 +0530662EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400663
664static int ahci_stop_fis_rx(struct ata_port *ap)
665{
666 void __iomem *port_mmio = ahci_port_base(ap);
667 u32 tmp;
668
669 /* disable FIS reception */
670 tmp = readl(port_mmio + PORT_CMD);
671 tmp &= ~PORT_CMD_FIS_RX;
672 writel(tmp, port_mmio + PORT_CMD);
673
674 /* wait for completion, spec says 500ms, give it 1000 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200675 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400676 PORT_CMD_FIS_ON, 10, 1000);
677 if (tmp & PORT_CMD_FIS_ON)
678 return -EBUSY;
679
680 return 0;
681}
682
683static void ahci_power_up(struct ata_port *ap)
684{
685 struct ahci_host_priv *hpriv = ap->host->private_data;
686 void __iomem *port_mmio = ahci_port_base(ap);
687 u32 cmd;
688
689 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
690
691 /* spin up device */
692 if (hpriv->cap & HOST_CAP_SSS) {
693 cmd |= PORT_CMD_SPIN_UP;
694 writel(cmd, port_mmio + PORT_CMD);
695 }
696
697 /* wake up link */
698 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
699}
700
Tejun Heo6b7ae952010-09-01 17:50:06 +0200701static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
702 unsigned int hints)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400703{
Tejun Heo6b7ae952010-09-01 17:50:06 +0200704 struct ata_port *ap = link->ap;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400705 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400706 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400707 void __iomem *port_mmio = ahci_port_base(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400708
Tejun Heo6b7ae952010-09-01 17:50:06 +0200709 if (policy != ATA_LPM_MAX_POWER) {
Danesh Petigarafb329632016-01-11 13:22:26 -0800710 /* wakeup flag only applies to the max power policy */
711 hints &= ~ATA_LPM_WAKE_ONLY;
712
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400713 /*
Tejun Heo6b7ae952010-09-01 17:50:06 +0200714 * Disable interrupts on Phy Ready. This keeps us from
715 * getting woken up due to spurious phy ready
716 * interrupts.
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400717 */
Tejun Heo6b7ae952010-09-01 17:50:06 +0200718 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
719 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
720
721 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400722 }
723
Tejun Heo6b7ae952010-09-01 17:50:06 +0200724 if (hpriv->cap & HOST_CAP_ALPM) {
725 u32 cmd = readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400726
Tejun Heo6b7ae952010-09-01 17:50:06 +0200727 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
Danesh Petigarafb329632016-01-11 13:22:26 -0800728 if (!(hints & ATA_LPM_WAKE_ONLY))
729 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
Tejun Heo6b7ae952010-09-01 17:50:06 +0200730 cmd |= PORT_CMD_ICC_ACTIVE;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400731
Tejun Heo6b7ae952010-09-01 17:50:06 +0200732 writel(cmd, port_mmio + PORT_CMD);
733 readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400734
Tejun Heo6b7ae952010-09-01 17:50:06 +0200735 /* wait 10ms to be sure we've come out of LPM state */
Tejun Heo97750ce2010-09-06 17:56:29 +0200736 ata_msleep(ap, 10);
Danesh Petigarafb329632016-01-11 13:22:26 -0800737
738 if (hints & ATA_LPM_WAKE_ONLY)
739 return 0;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200740 } else {
741 cmd |= PORT_CMD_ALPE;
742 if (policy == ATA_LPM_MIN_POWER)
743 cmd |= PORT_CMD_ASP;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400744
Tejun Heo6b7ae952010-09-01 17:50:06 +0200745 /* write out new cmd value */
746 writel(cmd, port_mmio + PORT_CMD);
747 }
748 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400749
Shane Huang65fe1f02012-09-07 22:40:01 +0800750 /* set aggressive device sleep */
751 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
752 (hpriv->cap2 & HOST_CAP2_SADM) &&
753 (link->device->flags & ATA_DFLAG_DEVSLP)) {
754 if (policy == ATA_LPM_MIN_POWER)
755 ahci_set_aggressive_devslp(ap, true);
756 else
757 ahci_set_aggressive_devslp(ap, false);
758 }
759
Tejun Heo6b7ae952010-09-01 17:50:06 +0200760 if (policy == ATA_LPM_MAX_POWER) {
761 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400762
Tejun Heo6b7ae952010-09-01 17:50:06 +0200763 /* turn PHYRDY IRQ back on */
764 pp->intr_mask |= PORT_IRQ_PHYRDY;
765 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
766 }
767
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400768 return 0;
769}
770
771#ifdef CONFIG_PM
772static void ahci_power_down(struct ata_port *ap)
773{
774 struct ahci_host_priv *hpriv = ap->host->private_data;
775 void __iomem *port_mmio = ahci_port_base(ap);
776 u32 cmd, scontrol;
777
778 if (!(hpriv->cap & HOST_CAP_SSS))
779 return;
780
781 /* put device into listen mode, first set PxSCTL.DET to 0 */
782 scontrol = readl(port_mmio + PORT_SCR_CTL);
783 scontrol &= ~0xf;
784 writel(scontrol, port_mmio + PORT_SCR_CTL);
785
786 /* then set PxCMD.SUD to 0 */
787 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
788 cmd &= ~PORT_CMD_SPIN_UP;
789 writel(cmd, port_mmio + PORT_CMD);
790}
791#endif
792
793static void ahci_start_port(struct ata_port *ap)
794{
Brian Norris66583c92012-02-21 10:38:42 -0800795 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400796 struct ahci_port_priv *pp = ap->private_data;
797 struct ata_link *link;
798 struct ahci_em_priv *emp;
799 ssize_t rc;
800 int i;
801
802 /* enable FIS reception */
803 ahci_start_fis_rx(ap);
804
Brian Norris66583c92012-02-21 10:38:42 -0800805 /* enable DMA */
806 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
Hans de Goede039ece32014-02-22 16:53:30 +0100807 hpriv->start_engine(ap);
Brian Norris66583c92012-02-21 10:38:42 -0800808
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400809 /* turn on LEDs */
810 if (ap->flags & ATA_FLAG_EM) {
811 ata_for_each_link(link, ap, EDGE) {
812 emp = &pp->em_priv[link->pmp];
813
814 /* EM Transmit bit maybe busy during init */
815 for (i = 0; i < EM_MAX_RETRY; i++) {
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500816 rc = ap->ops->transmit_led_message(ap,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400817 emp->led_state,
818 4);
Lukasz Doraufa070ee2013-10-14 18:18:53 +0200819 /*
820 * If busy, give a breather but do not
821 * release EH ownership by using msleep()
822 * instead of ata_msleep(). EM Transmit
823 * bit is busy for the whole host and
824 * releasing ownership will cause other
825 * ports to fail the same way.
826 */
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400827 if (rc == -EBUSY)
Lukasz Doraufa070ee2013-10-14 18:18:53 +0200828 msleep(1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400829 else
830 break;
831 }
832 }
833 }
834
835 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
836 ata_for_each_link(link, ap, EDGE)
837 ahci_init_sw_activity(link);
838
839}
840
841static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
842{
843 int rc;
844
845 /* disable DMA */
846 rc = ahci_stop_engine(ap);
847 if (rc) {
848 *emsg = "failed to stop engine";
849 return rc;
850 }
851
852 /* disable FIS reception */
853 rc = ahci_stop_fis_rx(ap);
854 if (rc) {
855 *emsg = "failed stop FIS RX";
856 return rc;
857 }
858
859 return 0;
860}
861
862int ahci_reset_controller(struct ata_host *host)
863{
864 struct ahci_host_priv *hpriv = host->private_data;
865 void __iomem *mmio = hpriv->mmio;
866 u32 tmp;
867
868 /* we must be in AHCI mode, before using anything
869 * AHCI-specific, such as HOST_RESET.
870 */
871 ahci_enable_ahci(mmio);
872
873 /* global controller reset */
874 if (!ahci_skip_host_reset) {
875 tmp = readl(mmio + HOST_CTL);
876 if ((tmp & HOST_RESET) == 0) {
877 writel(tmp | HOST_RESET, mmio + HOST_CTL);
878 readl(mmio + HOST_CTL); /* flush */
879 }
880
881 /*
882 * to perform host reset, OS should set HOST_RESET
883 * and poll until this bit is read to be "0".
884 * reset must complete within 1 second, or
885 * the hardware should be considered fried.
886 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200887 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400888 HOST_RESET, 10, 1000);
889
890 if (tmp & HOST_RESET) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700891 dev_err(host->dev, "controller reset failed (0x%x)\n",
892 tmp);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400893 return -EIO;
894 }
895
896 /* turn on AHCI mode */
897 ahci_enable_ahci(mmio);
898
899 /* Some registers might be cleared on reset. Restore
900 * initial values.
901 */
902 ahci_restore_initial_config(host);
903 } else
Joe Perchesa44fec12011-04-15 15:51:58 -0700904 dev_info(host->dev, "skipping global host reset\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400905
906 return 0;
907}
908EXPORT_SYMBOL_GPL(ahci_reset_controller);
909
910static void ahci_sw_activity(struct ata_link *link)
911{
912 struct ata_port *ap = link->ap;
913 struct ahci_port_priv *pp = ap->private_data;
914 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
915
916 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
917 return;
918
919 emp->activity++;
920 if (!timer_pending(&emp->timer))
921 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
922}
923
924static void ahci_sw_activity_blink(unsigned long arg)
925{
926 struct ata_link *link = (struct ata_link *)arg;
927 struct ata_port *ap = link->ap;
928 struct ahci_port_priv *pp = ap->private_data;
929 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
930 unsigned long led_message = emp->led_state;
931 u32 activity_led_state;
932 unsigned long flags;
933
934 led_message &= EM_MSG_LED_VALUE;
935 led_message |= ap->port_no | (link->pmp << 8);
936
937 /* check to see if we've had activity. If so,
938 * toggle state of LED and reset timer. If not,
939 * turn LED to desired idle state.
940 */
941 spin_lock_irqsave(ap->lock, flags);
942 if (emp->saved_activity != emp->activity) {
943 emp->saved_activity = emp->activity;
944 /* get the current LED state */
945 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
946
947 if (activity_led_state)
948 activity_led_state = 0;
949 else
950 activity_led_state = 1;
951
952 /* clear old state */
953 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
954
955 /* toggle state */
956 led_message |= (activity_led_state << 16);
957 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
958 } else {
959 /* switch to idle */
960 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
961 if (emp->blink_policy == BLINK_OFF)
962 led_message |= (1 << 16);
963 }
964 spin_unlock_irqrestore(ap->lock, flags);
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500965 ap->ops->transmit_led_message(ap, led_message, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400966}
967
968static void ahci_init_sw_activity(struct ata_link *link)
969{
970 struct ata_port *ap = link->ap;
971 struct ahci_port_priv *pp = ap->private_data;
972 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
973
974 /* init activity stats, setup timer */
975 emp->saved_activity = emp->activity = 0;
976 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
977
978 /* check our blink policy and set flag for link if it's enabled */
979 if (emp->blink_policy)
980 link->flags |= ATA_LFLAG_SW_ACTIVITY;
981}
982
983int ahci_reset_em(struct ata_host *host)
984{
985 struct ahci_host_priv *hpriv = host->private_data;
986 void __iomem *mmio = hpriv->mmio;
987 u32 em_ctl;
988
989 em_ctl = readl(mmio + HOST_EM_CTL);
990 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
991 return -EINVAL;
992
993 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
994 return 0;
995}
996EXPORT_SYMBOL_GPL(ahci_reset_em);
997
998static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
999 ssize_t size)
1000{
1001 struct ahci_host_priv *hpriv = ap->host->private_data;
1002 struct ahci_port_priv *pp = ap->private_data;
1003 void __iomem *mmio = hpriv->mmio;
1004 u32 em_ctl;
1005 u32 message[] = {0, 0};
1006 unsigned long flags;
1007 int pmp;
1008 struct ahci_em_priv *emp;
1009
1010 /* get the slot number from the message */
1011 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1012 if (pmp < EM_MAX_SLOTS)
1013 emp = &pp->em_priv[pmp];
1014 else
1015 return -EINVAL;
1016
1017 spin_lock_irqsave(ap->lock, flags);
1018
1019 /*
1020 * if we are still busy transmitting a previous message,
1021 * do not allow
1022 */
1023 em_ctl = readl(mmio + HOST_EM_CTL);
1024 if (em_ctl & EM_CTL_TM) {
1025 spin_unlock_irqrestore(ap->lock, flags);
1026 return -EBUSY;
1027 }
1028
Harry Zhang008dbd62010-04-23 17:27:19 +08001029 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1030 /*
1031 * create message header - this is all zero except for
1032 * the message size, which is 4 bytes.
1033 */
1034 message[0] |= (4 << 8);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001035
Harry Zhang008dbd62010-04-23 17:27:19 +08001036 /* ignore 0:4 of byte zero, fill in port info yourself */
1037 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001038
Harry Zhang008dbd62010-04-23 17:27:19 +08001039 /* write message to EM_LOC */
1040 writel(message[0], mmio + hpriv->em_loc);
1041 writel(message[1], mmio + hpriv->em_loc+4);
1042
1043 /*
1044 * tell hardware to transmit the message
1045 */
1046 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1047 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001048
1049 /* save off new led state for port/slot */
1050 emp->led_state = state;
1051
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001052 spin_unlock_irqrestore(ap->lock, flags);
1053 return size;
1054}
1055
1056static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1057{
1058 struct ahci_port_priv *pp = ap->private_data;
1059 struct ata_link *link;
1060 struct ahci_em_priv *emp;
1061 int rc = 0;
1062
1063 ata_for_each_link(link, ap, EDGE) {
1064 emp = &pp->em_priv[link->pmp];
1065 rc += sprintf(buf, "%lx\n", emp->led_state);
1066 }
1067 return rc;
1068}
1069
1070static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1071 size_t size)
1072{
Daeseok Younb2a52b62014-02-20 08:28:45 +09001073 unsigned int state;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001074 int pmp;
1075 struct ahci_port_priv *pp = ap->private_data;
1076 struct ahci_em_priv *emp;
1077
Daeseok Younb2a52b62014-02-20 08:28:45 +09001078 if (kstrtouint(buf, 0, &state) < 0)
1079 return -EINVAL;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001080
1081 /* get the slot number from the message */
1082 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1083 if (pmp < EM_MAX_SLOTS)
1084 emp = &pp->em_priv[pmp];
1085 else
1086 return -EINVAL;
1087
1088 /* mask off the activity bits if we are in sw_activity
1089 * mode, user should turn off sw_activity before setting
1090 * activity led through em_message
1091 */
1092 if (emp->blink_policy)
1093 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1094
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001095 return ap->ops->transmit_led_message(ap, state, size);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001096}
1097
1098static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1099{
1100 struct ata_link *link = dev->link;
1101 struct ata_port *ap = link->ap;
1102 struct ahci_port_priv *pp = ap->private_data;
1103 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1104 u32 port_led_state = emp->led_state;
1105
1106 /* save the desired Activity LED behavior */
1107 if (val == OFF) {
1108 /* clear LFLAG */
1109 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1110
1111 /* set the LED to OFF */
1112 port_led_state &= EM_MSG_LED_VALUE_OFF;
1113 port_led_state |= (ap->port_no | (link->pmp << 8));
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001114 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001115 } else {
1116 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1117 if (val == BLINK_OFF) {
1118 /* set LED to ON for idle */
1119 port_led_state &= EM_MSG_LED_VALUE_OFF;
1120 port_led_state |= (ap->port_no | (link->pmp << 8));
1121 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001122 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001123 }
1124 }
1125 emp->blink_policy = val;
1126 return 0;
1127}
1128
1129static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1130{
1131 struct ata_link *link = dev->link;
1132 struct ata_port *ap = link->ap;
1133 struct ahci_port_priv *pp = ap->private_data;
1134 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1135
1136 /* display the saved value of activity behavior for this
1137 * disk.
1138 */
1139 return sprintf(buf, "%d\n", emp->blink_policy);
1140}
1141
1142static void ahci_port_init(struct device *dev, struct ata_port *ap,
1143 int port_no, void __iomem *mmio,
1144 void __iomem *port_mmio)
1145{
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02001146 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001147 const char *emsg = NULL;
1148 int rc;
1149 u32 tmp;
1150
1151 /* make sure port is not active */
1152 rc = ahci_deinit_port(ap, &emsg);
1153 if (rc)
1154 dev_warn(dev, "%s (%d)\n", emsg, rc);
1155
1156 /* clear SError */
1157 tmp = readl(port_mmio + PORT_SCR_ERR);
1158 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1159 writel(tmp, port_mmio + PORT_SCR_ERR);
1160
1161 /* clear port IRQ */
1162 tmp = readl(port_mmio + PORT_IRQ_STAT);
1163 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1164 if (tmp)
1165 writel(tmp, port_mmio + PORT_IRQ_STAT);
1166
1167 writel(1 << port_no, mmio + HOST_IRQ_STAT);
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02001168
1169 /* mark esata ports */
1170 tmp = readl(port_mmio + PORT_CMD);
1171 if ((tmp & PORT_CMD_HPCP) ||
1172 ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS)))
1173 ap->pflags |= ATA_PFLAG_EXTERNAL;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001174}
1175
1176void ahci_init_controller(struct ata_host *host)
1177{
1178 struct ahci_host_priv *hpriv = host->private_data;
1179 void __iomem *mmio = hpriv->mmio;
1180 int i;
1181 void __iomem *port_mmio;
1182 u32 tmp;
1183
1184 for (i = 0; i < host->n_ports; i++) {
1185 struct ata_port *ap = host->ports[i];
1186
1187 port_mmio = ahci_port_base(ap);
1188 if (ata_port_is_dummy(ap))
1189 continue;
1190
1191 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1192 }
1193
1194 tmp = readl(mmio + HOST_CTL);
1195 VPRINTK("HOST_CTL 0x%x\n", tmp);
1196 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1197 tmp = readl(mmio + HOST_CTL);
1198 VPRINTK("HOST_CTL 0x%x\n", tmp);
1199}
1200EXPORT_SYMBOL_GPL(ahci_init_controller);
1201
1202static void ahci_dev_config(struct ata_device *dev)
1203{
1204 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1205
1206 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1207 dev->max_sectors = 255;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001208 ata_dev_info(dev,
1209 "SB600 AHCI: limiting to 255 sectors per cmd\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001210 }
1211}
1212
Rob Herringbbb4ab42012-08-17 09:51:50 -05001213unsigned int ahci_dev_classify(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001214{
1215 void __iomem *port_mmio = ahci_port_base(ap);
1216 struct ata_taskfile tf;
1217 u32 tmp;
1218
1219 tmp = readl(port_mmio + PORT_SIG);
1220 tf.lbah = (tmp >> 24) & 0xff;
1221 tf.lbam = (tmp >> 16) & 0xff;
1222 tf.lbal = (tmp >> 8) & 0xff;
1223 tf.nsect = (tmp) & 0xff;
1224
1225 return ata_dev_classify(&tf);
1226}
Rob Herringbbb4ab42012-08-17 09:51:50 -05001227EXPORT_SYMBOL_GPL(ahci_dev_classify);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001228
David Milburn02cdfcf2010-11-12 15:38:21 -06001229void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1230 u32 opts)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001231{
1232 dma_addr_t cmd_tbl_dma;
1233
1234 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1235
1236 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1237 pp->cmd_slot[tag].status = 0;
1238 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1239 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1240}
David Milburn02cdfcf2010-11-12 15:38:21 -06001241EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001242
1243int ahci_kick_engine(struct ata_port *ap)
1244{
1245 void __iomem *port_mmio = ahci_port_base(ap);
1246 struct ahci_host_priv *hpriv = ap->host->private_data;
1247 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1248 u32 tmp;
1249 int busy, rc;
1250
1251 /* stop engine */
1252 rc = ahci_stop_engine(ap);
1253 if (rc)
1254 goto out_restart;
1255
1256 /* need to do CLO?
1257 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1258 */
1259 busy = status & (ATA_BUSY | ATA_DRQ);
1260 if (!busy && !sata_pmp_attached(ap)) {
1261 rc = 0;
1262 goto out_restart;
1263 }
1264
1265 if (!(hpriv->cap & HOST_CAP_CLO)) {
1266 rc = -EOPNOTSUPP;
1267 goto out_restart;
1268 }
1269
1270 /* perform CLO */
1271 tmp = readl(port_mmio + PORT_CMD);
1272 tmp |= PORT_CMD_CLO;
1273 writel(tmp, port_mmio + PORT_CMD);
1274
1275 rc = 0;
Tejun Heo97750ce2010-09-06 17:56:29 +02001276 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001277 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1278 if (tmp & PORT_CMD_CLO)
1279 rc = -EIO;
1280
1281 /* restart engine */
1282 out_restart:
Hans de Goede039ece32014-02-22 16:53:30 +01001283 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001284 return rc;
1285}
1286EXPORT_SYMBOL_GPL(ahci_kick_engine);
1287
1288static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1289 struct ata_taskfile *tf, int is_cmd, u16 flags,
1290 unsigned long timeout_msec)
1291{
1292 const u32 cmd_fis_len = 5; /* five dwords */
1293 struct ahci_port_priv *pp = ap->private_data;
1294 void __iomem *port_mmio = ahci_port_base(ap);
1295 u8 *fis = pp->cmd_tbl;
1296 u32 tmp;
1297
1298 /* prep the command */
1299 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1300 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1301
Xiangliang Yu023113d2015-11-26 20:27:02 +08001302 /* set port value for softreset of Port Multiplier */
1303 if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1304 tmp = readl(port_mmio + PORT_FBS);
1305 tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1306 tmp |= pmp << PORT_FBS_DEV_OFFSET;
1307 writel(tmp, port_mmio + PORT_FBS);
1308 pp->fbs_last_dev = pmp;
1309 }
1310
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001311 /* issue & wait */
1312 writel(1, port_mmio + PORT_CMD_ISSUE);
1313
1314 if (timeout_msec) {
Tejun Heo97750ce2010-09-06 17:56:29 +02001315 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1316 0x1, 0x1, 1, timeout_msec);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001317 if (tmp & 0x1) {
1318 ahci_kick_engine(ap);
1319 return -EBUSY;
1320 }
1321 } else
1322 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1323
1324 return 0;
1325}
1326
1327int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1328 int pmp, unsigned long deadline,
1329 int (*check_ready)(struct ata_link *link))
1330{
1331 struct ata_port *ap = link->ap;
1332 struct ahci_host_priv *hpriv = ap->host->private_data;
xiangliang yu89dafa22013-10-27 08:03:04 -04001333 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001334 const char *reason = NULL;
1335 unsigned long now, msecs;
1336 struct ata_taskfile tf;
xiangliang yu89dafa22013-10-27 08:03:04 -04001337 bool fbs_disabled = false;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001338 int rc;
1339
1340 DPRINTK("ENTER\n");
1341
1342 /* prepare for SRST (AHCI-1.1 10.4.1) */
1343 rc = ahci_kick_engine(ap);
1344 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001345 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001346
xiangliang yu89dafa22013-10-27 08:03:04 -04001347 /*
1348 * According to AHCI-1.2 9.3.9: if FBS is enable, software shall
1349 * clear PxFBS.EN to '0' prior to issuing software reset to devices
1350 * that is attached to port multiplier.
1351 */
1352 if (!ata_is_host_link(link) && pp->fbs_enabled) {
1353 ahci_disable_fbs(ap);
1354 fbs_disabled = true;
1355 }
1356
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001357 ata_tf_init(link->device, &tf);
1358
1359 /* issue the first D2H Register FIS */
1360 msecs = 0;
1361 now = jiffies;
Tejun Heof1f5a802010-08-27 11:09:15 +02001362 if (time_after(deadline, now))
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001363 msecs = jiffies_to_msecs(deadline - now);
1364
1365 tf.ctl |= ATA_SRST;
1366 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1367 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1368 rc = -EIO;
1369 reason = "1st FIS failed";
1370 goto fail;
1371 }
1372
1373 /* spec says at least 5us, but be generous and sleep for 1ms */
Tejun Heo97750ce2010-09-06 17:56:29 +02001374 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001375
1376 /* issue the second D2H Register FIS */
1377 tf.ctl &= ~ATA_SRST;
1378 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1379
1380 /* wait for link to become ready */
1381 rc = ata_wait_after_reset(link, deadline, check_ready);
1382 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1383 /*
1384 * Workaround for cases where link online status can't
1385 * be trusted. Treat device readiness timeout as link
1386 * offline.
1387 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001388 ata_link_info(link, "device not ready, treating as offline\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001389 *class = ATA_DEV_NONE;
1390 } else if (rc) {
1391 /* link occupied, -ENODEV too is an error */
1392 reason = "device not ready";
1393 goto fail;
1394 } else
1395 *class = ahci_dev_classify(ap);
1396
xiangliang yu89dafa22013-10-27 08:03:04 -04001397 /* re-enable FBS if disabled before */
1398 if (fbs_disabled)
1399 ahci_enable_fbs(ap);
1400
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001401 DPRINTK("EXIT, class=%u\n", *class);
1402 return 0;
1403
1404 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07001405 ata_link_err(link, "softreset failed (%s)\n", reason);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001406 return rc;
1407}
1408
1409int ahci_check_ready(struct ata_link *link)
1410{
1411 void __iomem *port_mmio = ahci_port_base(link->ap);
1412 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1413
1414 return ata_check_ready(status);
1415}
1416EXPORT_SYMBOL_GPL(ahci_check_ready);
1417
1418static int ahci_softreset(struct ata_link *link, unsigned int *class,
1419 unsigned long deadline)
1420{
1421 int pmp = sata_srst_pmp(link);
1422
1423 DPRINTK("ENTER\n");
1424
1425 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1426}
1427EXPORT_SYMBOL_GPL(ahci_do_softreset);
1428
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001429static int ahci_bad_pmp_check_ready(struct ata_link *link)
1430{
1431 void __iomem *port_mmio = ahci_port_base(link->ap);
1432 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1433 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1434
1435 /*
1436 * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1437 * which can save timeout delay.
1438 */
1439 if (irq_status & PORT_IRQ_BAD_PMP)
1440 return -EIO;
1441
1442 return ata_check_ready(status);
1443}
1444
Daeseok Youn35186d02014-02-20 08:34:27 +09001445static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1446 unsigned long deadline)
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001447{
1448 struct ata_port *ap = link->ap;
1449 void __iomem *port_mmio = ahci_port_base(ap);
1450 int pmp = sata_srst_pmp(link);
1451 int rc;
1452 u32 irq_sts;
1453
1454 DPRINTK("ENTER\n");
1455
1456 rc = ahci_do_softreset(link, class, pmp, deadline,
1457 ahci_bad_pmp_check_ready);
1458
1459 /*
1460 * Soft reset fails with IPMS set when PMP is enabled but
1461 * SATA HDD/ODD is connected to SATA port, do soft reset
1462 * again to port 0.
1463 */
1464 if (rc == -EIO) {
1465 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1466 if (irq_sts & PORT_IRQ_BAD_PMP) {
Wei Yongjun39f80ac2012-12-03 23:39:31 -05001467 ata_link_warn(link,
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001468 "applying PMP SRST workaround "
1469 "and retrying\n");
1470 rc = ahci_do_softreset(link, class, 0, deadline,
1471 ahci_check_ready);
1472 }
1473 }
1474
1475 return rc;
1476}
1477
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001478static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1479 unsigned long deadline)
1480{
1481 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1482 struct ata_port *ap = link->ap;
1483 struct ahci_port_priv *pp = ap->private_data;
Hans de Goede039ece32014-02-22 16:53:30 +01001484 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001485 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1486 struct ata_taskfile tf;
1487 bool online;
1488 int rc;
1489
1490 DPRINTK("ENTER\n");
1491
1492 ahci_stop_engine(ap);
1493
1494 /* clear D2H reception area to properly wait for D2H FIS */
1495 ata_tf_init(link->device, &tf);
Sergei Shtylyov9bbb1b02013-06-23 01:39:39 +04001496 tf.command = ATA_BUSY;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001497 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1498
1499 rc = sata_link_hardreset(link, timing, deadline, &online,
1500 ahci_check_ready);
1501
Hans de Goede039ece32014-02-22 16:53:30 +01001502 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001503
1504 if (online)
1505 *class = ahci_dev_classify(ap);
1506
1507 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1508 return rc;
1509}
1510
1511static void ahci_postreset(struct ata_link *link, unsigned int *class)
1512{
1513 struct ata_port *ap = link->ap;
1514 void __iomem *port_mmio = ahci_port_base(ap);
1515 u32 new_tmp, tmp;
1516
1517 ata_std_postreset(link, class);
1518
1519 /* Make sure port's ATAPI bit is set appropriately */
1520 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1521 if (*class == ATA_DEV_ATAPI)
1522 new_tmp |= PORT_CMD_ATAPI;
1523 else
1524 new_tmp &= ~PORT_CMD_ATAPI;
1525 if (new_tmp != tmp) {
1526 writel(new_tmp, port_mmio + PORT_CMD);
1527 readl(port_mmio + PORT_CMD); /* flush */
1528 }
1529}
1530
1531static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1532{
1533 struct scatterlist *sg;
1534 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1535 unsigned int si;
1536
1537 VPRINTK("ENTER\n");
1538
1539 /*
1540 * Next, the S/G list.
1541 */
1542 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1543 dma_addr_t addr = sg_dma_address(sg);
1544 u32 sg_len = sg_dma_len(sg);
1545
1546 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1547 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1548 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1549 }
1550
1551 return si;
1552}
1553
1554static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1555{
1556 struct ata_port *ap = qc->ap;
1557 struct ahci_port_priv *pp = ap->private_data;
1558
1559 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1560 return ata_std_qc_defer(qc);
1561 else
1562 return sata_pmp_qc_defer_cmd_switch(qc);
1563}
1564
1565static void ahci_qc_prep(struct ata_queued_cmd *qc)
1566{
1567 struct ata_port *ap = qc->ap;
1568 struct ahci_port_priv *pp = ap->private_data;
1569 int is_atapi = ata_is_atapi(qc->tf.protocol);
1570 void *cmd_tbl;
1571 u32 opts;
1572 const u32 cmd_fis_len = 5; /* five dwords */
1573 unsigned int n_elem;
1574
1575 /*
1576 * Fill in command table information. First, the header,
1577 * a SATA Register - Host to Device command FIS.
1578 */
1579 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1580
1581 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1582 if (is_atapi) {
1583 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1584 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1585 }
1586
1587 n_elem = 0;
1588 if (qc->flags & ATA_QCFLAG_DMAMAP)
1589 n_elem = ahci_fill_sg(qc, cmd_tbl);
1590
1591 /*
1592 * Fill in command slot information.
1593 */
1594 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1595 if (qc->tf.flags & ATA_TFLAG_WRITE)
1596 opts |= AHCI_CMD_WRITE;
1597 if (is_atapi)
1598 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1599
1600 ahci_fill_cmd_slot(pp, qc->tag, opts);
1601}
1602
1603static void ahci_fbs_dec_intr(struct ata_port *ap)
1604{
1605 struct ahci_port_priv *pp = ap->private_data;
1606 void __iomem *port_mmio = ahci_port_base(ap);
1607 u32 fbs = readl(port_mmio + PORT_FBS);
1608 int retries = 3;
1609
1610 DPRINTK("ENTER\n");
1611 BUG_ON(!pp->fbs_enabled);
1612
1613 /* time to wait for DEC is not specified by AHCI spec,
1614 * add a retry loop for safety.
1615 */
1616 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1617 fbs = readl(port_mmio + PORT_FBS);
1618 while ((fbs & PORT_FBS_DEC) && retries--) {
1619 udelay(1);
1620 fbs = readl(port_mmio + PORT_FBS);
1621 }
1622
1623 if (fbs & PORT_FBS_DEC)
Joe Perchesa44fec12011-04-15 15:51:58 -07001624 dev_err(ap->host->dev, "failed to clear device error\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001625}
1626
1627static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1628{
1629 struct ahci_host_priv *hpriv = ap->host->private_data;
1630 struct ahci_port_priv *pp = ap->private_data;
1631 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1632 struct ata_link *link = NULL;
1633 struct ata_queued_cmd *active_qc;
1634 struct ata_eh_info *active_ehi;
1635 bool fbs_need_dec = false;
1636 u32 serror;
1637
1638 /* determine active link with error */
1639 if (pp->fbs_enabled) {
1640 void __iomem *port_mmio = ahci_port_base(ap);
1641 u32 fbs = readl(port_mmio + PORT_FBS);
1642 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1643
Shane Huang912b9ac2013-06-08 16:00:16 +08001644 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001645 link = &ap->pmp_link[pmp];
1646 fbs_need_dec = true;
1647 }
1648
1649 } else
1650 ata_for_each_link(link, ap, EDGE)
1651 if (ata_link_active(link))
1652 break;
1653
1654 if (!link)
1655 link = &ap->link;
1656
1657 active_qc = ata_qc_from_tag(ap, link->active_tag);
1658 active_ehi = &link->eh_info;
1659
1660 /* record irq stat */
1661 ata_ehi_clear_desc(host_ehi);
1662 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1663
1664 /* AHCI needs SError cleared; otherwise, it might lock up */
1665 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1666 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1667 host_ehi->serror |= serror;
1668
1669 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1670 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1671 irq_stat &= ~PORT_IRQ_IF_ERR;
1672
1673 if (irq_stat & PORT_IRQ_TF_ERR) {
1674 /* If qc is active, charge it; otherwise, the active
1675 * link. There's no active qc on NCQ errors. It will
1676 * be determined by EH by reading log page 10h.
1677 */
1678 if (active_qc)
1679 active_qc->err_mask |= AC_ERR_DEV;
1680 else
1681 active_ehi->err_mask |= AC_ERR_DEV;
1682
1683 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1684 host_ehi->serror &= ~SERR_INTERNAL;
1685 }
1686
1687 if (irq_stat & PORT_IRQ_UNK_FIS) {
Joe Perchesd5185d62014-03-26 09:34:49 -07001688 u32 *unk = pp->rx_fis + RX_FIS_UNK;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001689
1690 active_ehi->err_mask |= AC_ERR_HSM;
1691 active_ehi->action |= ATA_EH_RESET;
1692 ata_ehi_push_desc(active_ehi,
1693 "unknown FIS %08x %08x %08x %08x" ,
1694 unk[0], unk[1], unk[2], unk[3]);
1695 }
1696
1697 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1698 active_ehi->err_mask |= AC_ERR_HSM;
1699 active_ehi->action |= ATA_EH_RESET;
1700 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1701 }
1702
1703 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1704 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1705 host_ehi->action |= ATA_EH_RESET;
1706 ata_ehi_push_desc(host_ehi, "host bus error");
1707 }
1708
1709 if (irq_stat & PORT_IRQ_IF_ERR) {
1710 if (fbs_need_dec)
1711 active_ehi->err_mask |= AC_ERR_DEV;
1712 else {
1713 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1714 host_ehi->action |= ATA_EH_RESET;
1715 }
1716
1717 ata_ehi_push_desc(host_ehi, "interface fatal error");
1718 }
1719
1720 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1721 ata_ehi_hotplugged(host_ehi);
1722 ata_ehi_push_desc(host_ehi, "%s",
1723 irq_stat & PORT_IRQ_CONNECT ?
1724 "connection status changed" : "PHY RDY changed");
1725 }
1726
1727 /* okay, let's hand over to EH */
1728
1729 if (irq_stat & PORT_IRQ_FREEZE)
1730 ata_port_freeze(ap);
1731 else if (fbs_need_dec) {
1732 ata_link_abort(link);
1733 ahci_fbs_dec_intr(ap);
1734 } else
1735 ata_port_abort(ap);
1736}
1737
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001738static void ahci_handle_port_interrupt(struct ata_port *ap,
1739 void __iomem *port_mmio, u32 status)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001740{
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001741 struct ata_eh_info *ehi = &ap->link.eh_info;
1742 struct ahci_port_priv *pp = ap->private_data;
1743 struct ahci_host_priv *hpriv = ap->host->private_data;
1744 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001745 u32 qc_active = 0;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001746 int rc;
1747
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001748 /* ignore BAD_PMP while resetting */
1749 if (unlikely(resetting))
1750 status &= ~PORT_IRQ_BAD_PMP;
1751
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02001752 if (sata_lpm_ignore_phy_events(&ap->link)) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001753 status &= ~PORT_IRQ_PHYRDY;
Tejun Heo6b7ae952010-09-01 17:50:06 +02001754 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001755 }
1756
1757 if (unlikely(status & PORT_IRQ_ERROR)) {
1758 ahci_error_intr(ap, status);
1759 return;
1760 }
1761
1762 if (status & PORT_IRQ_SDB_FIS) {
1763 /* If SNotification is available, leave notification
1764 * handling to sata_async_notification(). If not,
1765 * emulate it by snooping SDB FIS RX area.
1766 *
1767 * Snooping FIS RX area is probably cheaper than
1768 * poking SNotification but some constrollers which
1769 * implement SNotification, ICH9 for example, don't
1770 * store AN SDB FIS into receive area.
1771 */
1772 if (hpriv->cap & HOST_CAP_SNTF)
1773 sata_async_notification(ap);
1774 else {
1775 /* If the 'N' bit in word 0 of the FIS is set,
1776 * we just received asynchronous notification.
1777 * Tell libata about it.
1778 *
1779 * Lack of SNotification should not appear in
1780 * ahci 1.2, so the workaround is unnecessary
1781 * when FBS is enabled.
1782 */
1783 if (pp->fbs_enabled)
1784 WARN_ON_ONCE(1);
1785 else {
1786 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1787 u32 f0 = le32_to_cpu(f[0]);
1788 if (f0 & (1 << 15))
1789 sata_async_notification(ap);
1790 }
1791 }
1792 }
1793
1794 /* pp->active_link is not reliable once FBS is enabled, both
1795 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1796 * NCQ and non-NCQ commands may be in flight at the same time.
1797 */
1798 if (pp->fbs_enabled) {
1799 if (ap->qc_active) {
1800 qc_active = readl(port_mmio + PORT_SCR_ACT);
1801 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1802 }
1803 } else {
1804 /* pp->active_link is valid iff any command is in flight */
1805 if (ap->qc_active && pp->active_link->sactive)
1806 qc_active = readl(port_mmio + PORT_SCR_ACT);
1807 else
1808 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1809 }
1810
1811
1812 rc = ata_qc_complete_multiple(ap, qc_active);
1813
1814 /* while resetting, invalid completions are expected */
1815 if (unlikely(rc < 0 && !resetting)) {
1816 ehi->err_mask |= AC_ERR_HSM;
1817 ehi->action |= ATA_EH_RESET;
1818 ata_port_freeze(ap);
1819 }
1820}
1821
Tejun Heo7865f832014-10-27 09:50:36 -04001822static void ahci_port_intr(struct ata_port *ap)
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001823{
1824 void __iomem *port_mmio = ahci_port_base(ap);
1825 u32 status;
1826
1827 status = readl(port_mmio + PORT_IRQ_STAT);
1828 writel(status, port_mmio + PORT_IRQ_STAT);
1829
Tejun Heo7865f832014-10-27 09:50:36 -04001830 ahci_handle_port_interrupt(ap, port_mmio, status);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001831}
1832
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001833static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001834{
Alexander Gordeev227dfb42014-09-29 18:26:01 +02001835 struct ata_port *ap = dev_instance;
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001836 void __iomem *port_mmio = ahci_port_base(ap);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001837 u32 status;
1838
Alexander Gordeev227dfb42014-09-29 18:26:01 +02001839 VPRINTK("ENTER\n");
1840
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001841 status = readl(port_mmio + PORT_IRQ_STAT);
1842 writel(status, port_mmio + PORT_IRQ_STAT);
1843
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001844 spin_lock(ap->lock);
1845 ahci_handle_port_interrupt(ap, port_mmio, status);
1846 spin_unlock(ap->lock);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001847
1848 VPRINTK("EXIT\n");
1849
Dan Williamsa6b7fb72015-11-11 16:27:38 -08001850 return IRQ_HANDLED;
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001851}
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001852
Suman Tripathif070d672016-02-06 11:25:22 +05301853u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001854{
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001855 unsigned int i, handled = 0;
Tejun Heo03e83cb2014-10-27 12:00:01 -04001856
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001857 for (i = 0; i < host->n_ports; i++) {
1858 struct ata_port *ap;
1859
1860 if (!(irq_masked & (1 << i)))
1861 continue;
1862
1863 ap = host->ports[i];
1864 if (ap) {
Tejun Heo7865f832014-10-27 09:50:36 -04001865 ahci_port_intr(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001866 VPRINTK("port %u\n", i);
1867 } else {
1868 VPRINTK("port %u (no irq)\n", i);
1869 if (ata_ratelimit())
Joe Perchesa44fec12011-04-15 15:51:58 -07001870 dev_warn(host->dev,
1871 "interrupt on disabled port %u\n", i);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001872 }
1873
1874 handled = 1;
1875 }
1876
Suman Tripathia129db82015-05-06 00:51:10 +05301877 return handled;
1878}
Suman Tripathif070d672016-02-06 11:25:22 +05301879EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
Suman Tripathia129db82015-05-06 00:51:10 +05301880
1881static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1882{
1883 struct ata_host *host = dev_instance;
1884 struct ahci_host_priv *hpriv;
1885 unsigned int rc = 0;
1886 void __iomem *mmio;
1887 u32 irq_stat, irq_masked;
1888
1889 VPRINTK("ENTER\n");
1890
1891 hpriv = host->private_data;
1892 mmio = hpriv->mmio;
1893
1894 /* sigh. 0xffffffff is a valid return from h/w */
1895 irq_stat = readl(mmio + HOST_IRQ_STAT);
1896 if (!irq_stat)
1897 return IRQ_NONE;
1898
1899 irq_masked = irq_stat & hpriv->port_map;
1900
1901 spin_lock(&host->lock);
1902
1903 rc = ahci_handle_port_intr(host, irq_masked);
1904
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001905 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1906 * it should be cleared after all the port events are cleared;
1907 * otherwise, it will raise a spurious interrupt after each
1908 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1909 * information.
1910 *
1911 * Also, use the unmasked value to clear interrupt as spurious
1912 * pending event on a dummy port might cause screaming IRQ.
1913 */
1914 writel(irq_stat, mmio + HOST_IRQ_STAT);
1915
Tejun Heo03e83cb2014-10-27 12:00:01 -04001916 spin_unlock(&host->lock);
1917
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001918 VPRINTK("EXIT\n");
1919
Suman Tripathia129db82015-05-06 00:51:10 +05301920 return IRQ_RETVAL(rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001921}
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001922
Suman Tripathi39e0ee92014-07-07 22:33:04 +05301923unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001924{
1925 struct ata_port *ap = qc->ap;
1926 void __iomem *port_mmio = ahci_port_base(ap);
1927 struct ahci_port_priv *pp = ap->private_data;
1928
1929 /* Keep track of the currently active link. It will be used
1930 * in completion path to determine whether NCQ phase is in
1931 * progress.
1932 */
1933 pp->active_link = qc->dev->link;
1934
1935 if (qc->tf.protocol == ATA_PROT_NCQ)
1936 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1937
1938 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
1939 u32 fbs = readl(port_mmio + PORT_FBS);
1940 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1941 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
1942 writel(fbs, port_mmio + PORT_FBS);
1943 pp->fbs_last_dev = qc->dev->link->pmp;
1944 }
1945
1946 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
1947
1948 ahci_sw_activity(qc->dev->link);
1949
1950 return 0;
1951}
Suman Tripathi39e0ee92014-07-07 22:33:04 +05301952EXPORT_SYMBOL_GPL(ahci_qc_issue);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001953
1954static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
1955{
1956 struct ahci_port_priv *pp = qc->ap->private_data;
Tejun Heo6ad60192010-10-15 11:00:08 +02001957 u8 *rx_fis = pp->rx_fis;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001958
1959 if (pp->fbs_enabled)
Tejun Heo6ad60192010-10-15 11:00:08 +02001960 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001961
Tejun Heo6ad60192010-10-15 11:00:08 +02001962 /*
1963 * After a successful execution of an ATA PIO data-in command,
1964 * the device doesn't send D2H Reg FIS to update the TF and
1965 * the host should take TF and E_Status from the preceding PIO
1966 * Setup FIS.
1967 */
1968 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
1969 !(qc->flags & ATA_QCFLAG_FAILED)) {
1970 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
1971 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
1972 } else
1973 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
1974
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001975 return true;
1976}
1977
1978static void ahci_freeze(struct ata_port *ap)
1979{
1980 void __iomem *port_mmio = ahci_port_base(ap);
1981
1982 /* turn IRQ off */
1983 writel(0, port_mmio + PORT_IRQ_MASK);
1984}
1985
1986static void ahci_thaw(struct ata_port *ap)
1987{
1988 struct ahci_host_priv *hpriv = ap->host->private_data;
1989 void __iomem *mmio = hpriv->mmio;
1990 void __iomem *port_mmio = ahci_port_base(ap);
1991 u32 tmp;
1992 struct ahci_port_priv *pp = ap->private_data;
1993
1994 /* clear IRQ */
1995 tmp = readl(port_mmio + PORT_IRQ_STAT);
1996 writel(tmp, port_mmio + PORT_IRQ_STAT);
1997 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
1998
1999 /* turn IRQ back on */
2000 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2001}
2002
Richard Zhu8b789d82013-10-15 10:44:54 +08002003void ahci_error_handler(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002004{
Hans de Goede039ece32014-02-22 16:53:30 +01002005 struct ahci_host_priv *hpriv = ap->host->private_data;
2006
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002007 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2008 /* restart engine */
2009 ahci_stop_engine(ap);
Hans de Goede039ece32014-02-22 16:53:30 +01002010 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002011 }
2012
2013 sata_pmp_error_handler(ap);
Tejun Heo0ee71952010-06-07 15:15:08 +02002014
2015 if (!ata_dev_enabled(ap->link.device))
2016 ahci_stop_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002017}
Richard Zhu8b789d82013-10-15 10:44:54 +08002018EXPORT_SYMBOL_GPL(ahci_error_handler);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002019
2020static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2021{
2022 struct ata_port *ap = qc->ap;
2023
2024 /* make DMA engine forget about the failed command */
2025 if (qc->flags & ATA_QCFLAG_FAILED)
2026 ahci_kick_engine(ap);
2027}
2028
Shane Huang65fe1f02012-09-07 22:40:01 +08002029static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2030{
Hans de Goede039ece32014-02-22 16:53:30 +01002031 struct ahci_host_priv *hpriv = ap->host->private_data;
Shane Huang65fe1f02012-09-07 22:40:01 +08002032 void __iomem *port_mmio = ahci_port_base(ap);
2033 struct ata_device *dev = ap->link.device;
2034 u32 devslp, dm, dito, mdat, deto;
2035 int rc;
2036 unsigned int err_mask;
2037
2038 devslp = readl(port_mmio + PORT_DEVSLP);
2039 if (!(devslp & PORT_DEVSLP_DSP)) {
Gabriele Mazzotta95bbbe92015-01-08 19:41:34 +01002040 dev_info(ap->host->dev, "port does not support device sleep\n");
Shane Huang65fe1f02012-09-07 22:40:01 +08002041 return;
2042 }
2043
2044 /* disable device sleep */
2045 if (!sleep) {
2046 if (devslp & PORT_DEVSLP_ADSE) {
2047 writel(devslp & ~PORT_DEVSLP_ADSE,
2048 port_mmio + PORT_DEVSLP);
2049 err_mask = ata_dev_set_feature(dev,
2050 SETFEATURES_SATA_DISABLE,
2051 SATA_DEVSLP);
2052 if (err_mask && err_mask != AC_ERR_DEV)
2053 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2054 }
2055 return;
2056 }
2057
2058 /* device sleep was already enabled */
2059 if (devslp & PORT_DEVSLP_ADSE)
2060 return;
2061
2062 /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
2063 rc = ahci_stop_engine(ap);
2064 if (rc)
2065 return;
2066
2067 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2068 dito = devslp_idle_timeout / (dm + 1);
2069 if (dito > 0x3ff)
2070 dito = 0x3ff;
2071
2072 /* Use the nominal value 10 ms if the read MDAT is zero,
2073 * the nominal value of DETO is 20 ms.
2074 */
Shane Huang803739d2012-12-17 23:18:59 +08002075 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002076 ATA_LOG_DEVSLP_VALID_MASK) {
Shane Huang803739d2012-12-17 23:18:59 +08002077 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002078 ATA_LOG_DEVSLP_MDAT_MASK;
2079 if (!mdat)
2080 mdat = 10;
Shane Huang803739d2012-12-17 23:18:59 +08002081 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
Shane Huang65fe1f02012-09-07 22:40:01 +08002082 if (!deto)
2083 deto = 20;
2084 } else {
2085 mdat = 10;
2086 deto = 20;
2087 }
2088
2089 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2090 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2091 (deto << PORT_DEVSLP_DETO_OFFSET) |
2092 PORT_DEVSLP_ADSE);
2093 writel(devslp, port_mmio + PORT_DEVSLP);
2094
Hans de Goede039ece32014-02-22 16:53:30 +01002095 hpriv->start_engine(ap);
Shane Huang65fe1f02012-09-07 22:40:01 +08002096
2097 /* enable device sleep feature for the drive */
2098 err_mask = ata_dev_set_feature(dev,
2099 SETFEATURES_SATA_ENABLE,
2100 SATA_DEVSLP);
2101 if (err_mask && err_mask != AC_ERR_DEV)
2102 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2103}
2104
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002105static void ahci_enable_fbs(struct ata_port *ap)
2106{
Hans de Goede039ece32014-02-22 16:53:30 +01002107 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002108 struct ahci_port_priv *pp = ap->private_data;
2109 void __iomem *port_mmio = ahci_port_base(ap);
2110 u32 fbs;
2111 int rc;
2112
2113 if (!pp->fbs_supported)
2114 return;
2115
2116 fbs = readl(port_mmio + PORT_FBS);
2117 if (fbs & PORT_FBS_EN) {
2118 pp->fbs_enabled = true;
2119 pp->fbs_last_dev = -1; /* initialization */
2120 return;
2121 }
2122
2123 rc = ahci_stop_engine(ap);
2124 if (rc)
2125 return;
2126
2127 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2128 fbs = readl(port_mmio + PORT_FBS);
2129 if (fbs & PORT_FBS_EN) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002130 dev_info(ap->host->dev, "FBS is enabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002131 pp->fbs_enabled = true;
2132 pp->fbs_last_dev = -1; /* initialization */
2133 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002134 dev_err(ap->host->dev, "Failed to enable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002135
Hans de Goede039ece32014-02-22 16:53:30 +01002136 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002137}
2138
2139static void ahci_disable_fbs(struct ata_port *ap)
2140{
Hans de Goede039ece32014-02-22 16:53:30 +01002141 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002142 struct ahci_port_priv *pp = ap->private_data;
2143 void __iomem *port_mmio = ahci_port_base(ap);
2144 u32 fbs;
2145 int rc;
2146
2147 if (!pp->fbs_supported)
2148 return;
2149
2150 fbs = readl(port_mmio + PORT_FBS);
2151 if ((fbs & PORT_FBS_EN) == 0) {
2152 pp->fbs_enabled = false;
2153 return;
2154 }
2155
2156 rc = ahci_stop_engine(ap);
2157 if (rc)
2158 return;
2159
2160 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2161 fbs = readl(port_mmio + PORT_FBS);
2162 if (fbs & PORT_FBS_EN)
Joe Perchesa44fec12011-04-15 15:51:58 -07002163 dev_err(ap->host->dev, "Failed to disable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002164 else {
Joe Perchesa44fec12011-04-15 15:51:58 -07002165 dev_info(ap->host->dev, "FBS is disabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002166 pp->fbs_enabled = false;
2167 }
2168
Hans de Goede039ece32014-02-22 16:53:30 +01002169 hpriv->start_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002170}
2171
2172static void ahci_pmp_attach(struct ata_port *ap)
2173{
2174 void __iomem *port_mmio = ahci_port_base(ap);
2175 struct ahci_port_priv *pp = ap->private_data;
2176 u32 cmd;
2177
2178 cmd = readl(port_mmio + PORT_CMD);
2179 cmd |= PORT_CMD_PMP;
2180 writel(cmd, port_mmio + PORT_CMD);
2181
2182 ahci_enable_fbs(ap);
2183
2184 pp->intr_mask |= PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002185
2186 /*
2187 * We must not change the port interrupt mask register if the
2188 * port is marked frozen, the value in pp->intr_mask will be
2189 * restored later when the port is thawed.
2190 *
2191 * Note that during initialization, the port is marked as
2192 * frozen since the irq handler is not yet registered.
2193 */
2194 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2195 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002196}
2197
2198static void ahci_pmp_detach(struct ata_port *ap)
2199{
2200 void __iomem *port_mmio = ahci_port_base(ap);
2201 struct ahci_port_priv *pp = ap->private_data;
2202 u32 cmd;
2203
2204 ahci_disable_fbs(ap);
2205
2206 cmd = readl(port_mmio + PORT_CMD);
2207 cmd &= ~PORT_CMD_PMP;
2208 writel(cmd, port_mmio + PORT_CMD);
2209
2210 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002211
2212 /* see comment above in ahci_pmp_attach() */
2213 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2214 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002215}
2216
David Milburn02cdfcf2010-11-12 15:38:21 -06002217int ahci_port_resume(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002218{
2219 ahci_power_up(ap);
2220 ahci_start_port(ap);
2221
2222 if (sata_pmp_attached(ap))
2223 ahci_pmp_attach(ap);
2224 else
2225 ahci_pmp_detach(ap);
2226
2227 return 0;
2228}
David Milburn02cdfcf2010-11-12 15:38:21 -06002229EXPORT_SYMBOL_GPL(ahci_port_resume);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002230
2231#ifdef CONFIG_PM
2232static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2233{
2234 const char *emsg = NULL;
2235 int rc;
2236
2237 rc = ahci_deinit_port(ap, &emsg);
2238 if (rc == 0)
2239 ahci_power_down(ap);
2240 else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002241 ata_port_err(ap, "%s (%d)\n", emsg, rc);
Tejun Heo7faa33d2011-07-22 11:41:26 +02002242 ata_port_freeze(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002243 }
2244
2245 return rc;
2246}
2247#endif
2248
2249static int ahci_port_start(struct ata_port *ap)
2250{
2251 struct ahci_host_priv *hpriv = ap->host->private_data;
2252 struct device *dev = ap->host->dev;
2253 struct ahci_port_priv *pp;
2254 void *mem;
2255 dma_addr_t mem_dma;
2256 size_t dma_sz, rx_fis_sz;
2257
2258 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2259 if (!pp)
2260 return -ENOMEM;
2261
Alexander Gordeevb29900e2013-05-22 08:53:48 +09002262 if (ap->host->n_ports > 1) {
2263 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2264 if (!pp->irq_desc) {
2265 devm_kfree(dev, pp);
2266 return -ENOMEM;
2267 }
2268 snprintf(pp->irq_desc, 8,
2269 "%s%d", dev_driver_string(dev), ap->port_no);
2270 }
2271
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002272 /* check FBS capability */
2273 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2274 void __iomem *port_mmio = ahci_port_base(ap);
2275 u32 cmd = readl(port_mmio + PORT_CMD);
2276 if (cmd & PORT_CMD_FBSCP)
2277 pp->fbs_supported = true;
Tejun Heo5f173102010-07-24 16:53:48 +02002278 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002279 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2280 ap->port_no);
Tejun Heo5f173102010-07-24 16:53:48 +02002281 pp->fbs_supported = true;
2282 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002283 dev_warn(dev, "port %d is not capable of FBS\n",
2284 ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002285 }
2286
2287 if (pp->fbs_supported) {
2288 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2289 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2290 } else {
2291 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2292 rx_fis_sz = AHCI_RX_FIS_SZ;
2293 }
2294
2295 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2296 if (!mem)
2297 return -ENOMEM;
2298 memset(mem, 0, dma_sz);
2299
2300 /*
2301 * First item in chunk of DMA memory: 32-slot command table,
2302 * 32 bytes each in size
2303 */
2304 pp->cmd_slot = mem;
2305 pp->cmd_slot_dma = mem_dma;
2306
2307 mem += AHCI_CMD_SLOT_SZ;
2308 mem_dma += AHCI_CMD_SLOT_SZ;
2309
2310 /*
2311 * Second item: Received-FIS area
2312 */
2313 pp->rx_fis = mem;
2314 pp->rx_fis_dma = mem_dma;
2315
2316 mem += rx_fis_sz;
2317 mem_dma += rx_fis_sz;
2318
2319 /*
2320 * Third item: data area for storing a single command
2321 * and its scatter-gather table
2322 */
2323 pp->cmd_tbl = mem;
2324 pp->cmd_tbl_dma = mem_dma;
2325
2326 /*
2327 * Save off initial list of interrupts to be enabled.
2328 * This could be changed later
2329 */
2330 pp->intr_mask = DEF_PORT_IRQ;
2331
Tejun Heo7865f832014-10-27 09:50:36 -04002332 /*
2333 * Switch to per-port locking in case each port has its own MSI vector.
2334 */
Robert Richter21bfd1a2015-05-31 13:55:18 +02002335 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
Tejun Heo7865f832014-10-27 09:50:36 -04002336 spin_lock_init(&pp->lock);
2337 ap->lock = &pp->lock;
2338 }
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01002339
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002340 ap->private_data = pp;
2341
2342 /* engage engines, captain */
2343 return ahci_port_resume(ap);
2344}
2345
2346static void ahci_port_stop(struct ata_port *ap)
2347{
2348 const char *emsg = NULL;
2349 int rc;
2350
2351 /* de-initialize port */
2352 rc = ahci_deinit_port(ap, &emsg);
2353 if (rc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002354 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002355}
2356
2357void ahci_print_info(struct ata_host *host, const char *scc_s)
2358{
2359 struct ahci_host_priv *hpriv = host->private_data;
2360 void __iomem *mmio = hpriv->mmio;
2361 u32 vers, cap, cap2, impl, speed;
2362 const char *speed_s;
2363
2364 vers = readl(mmio + HOST_VERSION);
2365 cap = hpriv->cap;
2366 cap2 = hpriv->cap2;
2367 impl = hpriv->port_map;
2368
2369 speed = (cap >> 20) & 0xf;
2370 if (speed == 1)
2371 speed_s = "1.5";
2372 else if (speed == 2)
2373 speed_s = "3";
2374 else if (speed == 3)
2375 speed_s = "6";
2376 else
2377 speed_s = "?";
2378
2379 dev_info(host->dev,
2380 "AHCI %02x%02x.%02x%02x "
2381 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2382 ,
2383
2384 (vers >> 24) & 0xff,
2385 (vers >> 16) & 0xff,
2386 (vers >> 8) & 0xff,
2387 vers & 0xff,
2388
2389 ((cap >> 8) & 0x1f) + 1,
2390 (cap & 0x1f) + 1,
2391 speed_s,
2392 impl,
2393 scc_s);
2394
2395 dev_info(host->dev,
2396 "flags: "
2397 "%s%s%s%s%s%s%s"
2398 "%s%s%s%s%s%s%s"
Shane Huang65fe1f02012-09-07 22:40:01 +08002399 "%s%s%s%s%s%s%s"
2400 "%s%s\n"
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002401 ,
2402
2403 cap & HOST_CAP_64 ? "64bit " : "",
2404 cap & HOST_CAP_NCQ ? "ncq " : "",
2405 cap & HOST_CAP_SNTF ? "sntf " : "",
2406 cap & HOST_CAP_MPS ? "ilck " : "",
2407 cap & HOST_CAP_SSS ? "stag " : "",
2408 cap & HOST_CAP_ALPM ? "pm " : "",
2409 cap & HOST_CAP_LED ? "led " : "",
2410 cap & HOST_CAP_CLO ? "clo " : "",
2411 cap & HOST_CAP_ONLY ? "only " : "",
2412 cap & HOST_CAP_PMP ? "pmp " : "",
2413 cap & HOST_CAP_FBS ? "fbs " : "",
2414 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2415 cap & HOST_CAP_SSC ? "slum " : "",
2416 cap & HOST_CAP_PART ? "part " : "",
2417 cap & HOST_CAP_CCC ? "ccc " : "",
2418 cap & HOST_CAP_EMS ? "ems " : "",
2419 cap & HOST_CAP_SXS ? "sxs " : "",
Shane Huang65fe1f02012-09-07 22:40:01 +08002420 cap2 & HOST_CAP2_DESO ? "deso " : "",
2421 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2422 cap2 & HOST_CAP2_SDS ? "sds " : "",
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002423 cap2 & HOST_CAP2_APST ? "apst " : "",
2424 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2425 cap2 & HOST_CAP2_BOH ? "boh " : ""
2426 );
2427}
2428EXPORT_SYMBOL_GPL(ahci_print_info);
2429
2430void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2431 struct ata_port_info *pi)
2432{
2433 u8 messages;
2434 void __iomem *mmio = hpriv->mmio;
2435 u32 em_loc = readl(mmio + HOST_EM_LOC);
2436 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2437
2438 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2439 return;
2440
2441 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2442
Harry Zhang008dbd62010-04-23 17:27:19 +08002443 if (messages) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002444 /* store em_loc */
2445 hpriv->em_loc = ((em_loc >> 16) * 4);
Harry Zhangc0623162010-04-23 17:28:38 +08002446 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
Harry Zhang008dbd62010-04-23 17:27:19 +08002447 hpriv->em_msg_type = messages;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002448 pi->flags |= ATA_FLAG_EM;
2449 if (!(em_ctl & EM_CTL_ALHD))
2450 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2451 }
2452}
2453EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2454
Dan Williamsd684a902015-11-11 16:27:33 -08002455static int ahci_host_activate_multi_irqs(struct ata_host *host,
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002456 struct scsi_host_template *sht)
Alexander Gordeev1c628542014-09-29 18:25:58 +02002457{
Dan Williamsd684a902015-11-11 16:27:33 -08002458 struct ahci_host_priv *hpriv = host->private_data;
Alexander Gordeev1c628542014-09-29 18:25:58 +02002459 int i, rc;
2460
2461 rc = ata_host_start(host);
2462 if (rc)
2463 return rc;
Robert Richter21bfd1a2015-05-31 13:55:18 +02002464 /*
2465 * Requests IRQs according to AHCI-1.1 when multiple MSIs were
2466 * allocated. That is one MSI per port, starting from @irq.
2467 */
Alexander Gordeev1c628542014-09-29 18:25:58 +02002468 for (i = 0; i < host->n_ports; i++) {
2469 struct ahci_port_priv *pp = host->ports[i]->private_data;
Dan Williamsf8931802015-12-05 16:18:44 -08002470 int irq = ahci_irq_vector(hpriv, i);
Alexander Gordeev1c628542014-09-29 18:25:58 +02002471
2472 /* Do not receive interrupts sent by dummy ports */
2473 if (!pp) {
2474 disable_irq(irq + i);
2475 continue;
2476 }
2477
Dan Williamsa6b7fb72015-11-11 16:27:38 -08002478 rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2479 0, pp->irq_desc, host->ports[i]);
2480
Alexander Gordeev1c628542014-09-29 18:25:58 +02002481 if (rc)
Dan Williams0a142b22015-10-30 17:09:35 -04002482 return rc;
Dan Williamsd684a902015-11-11 16:27:33 -08002483 ata_port_desc(host->ports[i], "irq %d", irq);
Dan Williams0a142b22015-10-30 17:09:35 -04002484 }
Dan Williamsd684a902015-11-11 16:27:33 -08002485
Dan Williams0a142b22015-10-30 17:09:35 -04002486 return ata_host_register(host, sht);
Alexander Gordeev1c628542014-09-29 18:25:58 +02002487}
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002488
2489/**
2490 * ahci_host_activate - start AHCI host, request IRQs and register it
2491 * @host: target ATA host
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002492 * @sht: scsi_host_template to use when registering the host
2493 *
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002494 * LOCKING:
2495 * Inherited from calling layer (may sleep).
2496 *
2497 * RETURNS:
2498 * 0 on success, -errno otherwise.
2499 */
Robert Richter21bfd1a2015-05-31 13:55:18 +02002500int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002501{
2502 struct ahci_host_priv *hpriv = host->private_data;
Robert Richter21bfd1a2015-05-31 13:55:18 +02002503 int irq = hpriv->irq;
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002504 int rc;
2505
Suman Tripathif070d672016-02-06 11:25:22 +05302506 if (hpriv->flags & (AHCI_HFLAG_MULTI_MSI | AHCI_HFLAG_MULTI_MSIX)) {
2507 if (hpriv->irq_handler)
2508 dev_warn(host->dev, "both AHCI_HFLAG_MULTI_MSI flag set \
2509 and custom irq handler implemented\n");
2510
Dan Williamsd684a902015-11-11 16:27:33 -08002511 rc = ahci_host_activate_multi_irqs(host, sht);
Suman Tripathif070d672016-02-06 11:25:22 +05302512 } else {
2513 rc = ata_host_activate(host, irq, hpriv->irq_handler,
Suman Tripathi5903b162015-05-06 00:51:11 +05302514 IRQF_SHARED, sht);
Suman Tripathif070d672016-02-06 11:25:22 +05302515 }
2516
2517
Alexander Gordeevd1028e22014-09-29 18:25:59 +02002518 return rc;
2519}
Alexander Gordeev1c628542014-09-29 18:25:58 +02002520EXPORT_SYMBOL_GPL(ahci_host_activate);
2521
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002522MODULE_AUTHOR("Jeff Garzik");
2523MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2524MODULE_LICENSE("GPL");