blob: b67086fde1f1890ab639a8322f7e1d20eb5615bc [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>
38#include <linux/init.h>
39#include <linux/blkdev.h>
40#include <linux/delay.h>
41#include <linux/interrupt.h>
42#include <linux/dma-mapping.h>
43#include <linux/device.h>
44#include <scsi/scsi_host.h>
45#include <scsi/scsi_cmnd.h>
46#include <linux/libata.h>
47#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);
72static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
73static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
74static int ahci_port_start(struct ata_port *ap);
75static void ahci_port_stop(struct ata_port *ap);
76static void ahci_qc_prep(struct ata_queued_cmd *qc);
77static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
78static void ahci_freeze(struct ata_port *ap);
79static void ahci_thaw(struct ata_port *ap);
Shane Huang65fe1f02012-09-07 22:40:01 +080080static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040081static void ahci_enable_fbs(struct ata_port *ap);
82static void ahci_disable_fbs(struct ata_port *ap);
83static void ahci_pmp_attach(struct ata_port *ap);
84static void ahci_pmp_detach(struct ata_port *ap);
85static int ahci_softreset(struct ata_link *link, unsigned int *class,
86 unsigned long deadline);
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +080087static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
88 unsigned long deadline);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040089static int ahci_hardreset(struct ata_link *link, unsigned int *class,
90 unsigned long deadline);
91static void ahci_postreset(struct ata_link *link, unsigned int *class);
92static void ahci_error_handler(struct ata_port *ap);
93static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040094static void ahci_dev_config(struct ata_device *dev);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040095#ifdef CONFIG_PM
96static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
97#endif
98static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
99static ssize_t ahci_activity_store(struct ata_device *dev,
100 enum sw_activity val);
101static void ahci_init_sw_activity(struct ata_link *link);
102
103static ssize_t ahci_show_host_caps(struct device *dev,
104 struct device_attribute *attr, char *buf);
105static ssize_t ahci_show_host_cap2(struct device *dev,
106 struct device_attribute *attr, char *buf);
107static ssize_t ahci_show_host_version(struct device *dev,
108 struct device_attribute *attr, char *buf);
109static ssize_t ahci_show_port_cmd(struct device *dev,
110 struct device_attribute *attr, char *buf);
Harry Zhangc0623162010-04-23 17:28:38 +0800111static ssize_t ahci_read_em_buffer(struct device *dev,
112 struct device_attribute *attr, char *buf);
113static ssize_t ahci_store_em_buffer(struct device *dev,
114 struct device_attribute *attr,
115 const char *buf, size_t size);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100116static ssize_t ahci_show_em_supported(struct device *dev,
117 struct device_attribute *attr, char *buf);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400118
119static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
120static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
121static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
122static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
Harry Zhangc0623162010-04-23 17:28:38 +0800123static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
124 ahci_read_em_buffer, ahci_store_em_buffer);
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100125static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400126
Tejun Heofad16e72010-09-21 09:25:48 +0200127struct device_attribute *ahci_shost_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400128 &dev_attr_link_power_management_policy,
129 &dev_attr_em_message_type,
130 &dev_attr_em_message,
131 &dev_attr_ahci_host_caps,
132 &dev_attr_ahci_host_cap2,
133 &dev_attr_ahci_host_version,
134 &dev_attr_ahci_port_cmd,
Harry Zhangc0623162010-04-23 17:28:38 +0800135 &dev_attr_em_buffer,
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100136 &dev_attr_em_message_supported,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400137 NULL
138};
Tejun Heofad16e72010-09-21 09:25:48 +0200139EXPORT_SYMBOL_GPL(ahci_shost_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400140
Tejun Heofad16e72010-09-21 09:25:48 +0200141struct device_attribute *ahci_sdev_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400142 &dev_attr_sw_activity,
143 &dev_attr_unload_heads,
144 NULL
145};
Tejun Heofad16e72010-09-21 09:25:48 +0200146EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400147
148struct ata_port_operations ahci_ops = {
149 .inherits = &sata_pmp_port_ops,
150
151 .qc_defer = ahci_pmp_qc_defer,
152 .qc_prep = ahci_qc_prep,
153 .qc_issue = ahci_qc_issue,
154 .qc_fill_rtf = ahci_qc_fill_rtf,
155
156 .freeze = ahci_freeze,
157 .thaw = ahci_thaw,
158 .softreset = ahci_softreset,
159 .hardreset = ahci_hardreset,
160 .postreset = ahci_postreset,
161 .pmp_softreset = ahci_softreset,
162 .error_handler = ahci_error_handler,
163 .post_internal_cmd = ahci_post_internal_cmd,
164 .dev_config = ahci_dev_config,
165
166 .scr_read = ahci_scr_read,
167 .scr_write = ahci_scr_write,
168 .pmp_attach = ahci_pmp_attach,
169 .pmp_detach = ahci_pmp_detach,
170
Tejun Heo6b7ae952010-09-01 17:50:06 +0200171 .set_lpm = ahci_set_lpm,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400172 .em_show = ahci_led_show,
173 .em_store = ahci_led_store,
174 .sw_activity_show = ahci_activity_show,
175 .sw_activity_store = ahci_activity_store,
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500176 .transmit_led_message = ahci_transmit_led_message,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400177#ifdef CONFIG_PM
178 .port_suspend = ahci_port_suspend,
179 .port_resume = ahci_port_resume,
180#endif
181 .port_start = ahci_port_start,
182 .port_stop = ahci_port_stop,
183};
184EXPORT_SYMBOL_GPL(ahci_ops);
185
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +0800186struct ata_port_operations ahci_pmp_retry_srst_ops = {
187 .inherits = &ahci_ops,
188 .softreset = ahci_pmp_retry_softreset,
189};
190EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
191
Chuansheng Liued08d402013-09-18 20:21:49 +0800192static bool ahci_em_messages __read_mostly = true;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400193EXPORT_SYMBOL_GPL(ahci_em_messages);
Chuansheng Liued08d402013-09-18 20:21:49 +0800194module_param(ahci_em_messages, bool, 0444);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400195/* add other LED protocol types when they become supported */
196MODULE_PARM_DESC(ahci_em_messages,
Harry Zhang008dbd62010-04-23 17:27:19 +0800197 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400198
Chuansheng Liued08d402013-09-18 20:21:49 +0800199/* device sleep idle timeout in ms */
200static int devslp_idle_timeout __read_mostly = 1000;
Shane Huang65fe1f02012-09-07 22:40:01 +0800201module_param(devslp_idle_timeout, int, 0644);
202MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
203
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400204static void ahci_enable_ahci(void __iomem *mmio)
205{
206 int i;
207 u32 tmp;
208
209 /* turn on AHCI_EN */
210 tmp = readl(mmio + HOST_CTL);
211 if (tmp & HOST_AHCI_EN)
212 return;
213
214 /* Some controllers need AHCI_EN to be written multiple times.
215 * Try a few times before giving up.
216 */
217 for (i = 0; i < 5; i++) {
218 tmp |= HOST_AHCI_EN;
219 writel(tmp, mmio + HOST_CTL);
220 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
221 if (tmp & HOST_AHCI_EN)
222 return;
223 msleep(10);
224 }
225
226 WARN_ON(1);
227}
228
229static ssize_t ahci_show_host_caps(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct Scsi_Host *shost = class_to_shost(dev);
233 struct ata_port *ap = ata_shost_to_port(shost);
234 struct ahci_host_priv *hpriv = ap->host->private_data;
235
236 return sprintf(buf, "%x\n", hpriv->cap);
237}
238
239static ssize_t ahci_show_host_cap2(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 struct Scsi_Host *shost = class_to_shost(dev);
243 struct ata_port *ap = ata_shost_to_port(shost);
244 struct ahci_host_priv *hpriv = ap->host->private_data;
245
246 return sprintf(buf, "%x\n", hpriv->cap2);
247}
248
249static ssize_t ahci_show_host_version(struct device *dev,
250 struct device_attribute *attr, char *buf)
251{
252 struct Scsi_Host *shost = class_to_shost(dev);
253 struct ata_port *ap = ata_shost_to_port(shost);
254 struct ahci_host_priv *hpriv = ap->host->private_data;
255 void __iomem *mmio = hpriv->mmio;
256
257 return sprintf(buf, "%x\n", readl(mmio + HOST_VERSION));
258}
259
260static ssize_t ahci_show_port_cmd(struct device *dev,
261 struct device_attribute *attr, char *buf)
262{
263 struct Scsi_Host *shost = class_to_shost(dev);
264 struct ata_port *ap = ata_shost_to_port(shost);
265 void __iomem *port_mmio = ahci_port_base(ap);
266
267 return sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
268}
269
Harry Zhangc0623162010-04-23 17:28:38 +0800270static ssize_t ahci_read_em_buffer(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
273 struct Scsi_Host *shost = class_to_shost(dev);
274 struct ata_port *ap = ata_shost_to_port(shost);
275 struct ahci_host_priv *hpriv = ap->host->private_data;
276 void __iomem *mmio = hpriv->mmio;
277 void __iomem *em_mmio = mmio + hpriv->em_loc;
278 u32 em_ctl, msg;
279 unsigned long flags;
280 size_t count;
281 int i;
282
283 spin_lock_irqsave(ap->lock, flags);
284
285 em_ctl = readl(mmio + HOST_EM_CTL);
286 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
287 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
288 spin_unlock_irqrestore(ap->lock, flags);
289 return -EINVAL;
290 }
291
292 if (!(em_ctl & EM_CTL_MR)) {
293 spin_unlock_irqrestore(ap->lock, flags);
294 return -EAGAIN;
295 }
296
297 if (!(em_ctl & EM_CTL_SMB))
298 em_mmio += hpriv->em_buf_sz;
299
300 count = hpriv->em_buf_sz;
301
302 /* the count should not be larger than PAGE_SIZE */
303 if (count > PAGE_SIZE) {
304 if (printk_ratelimit())
Joe Perchesa9a79df2011-04-15 15:51:59 -0700305 ata_port_warn(ap,
306 "EM read buffer size too large: "
307 "buffer size %u, page size %lu\n",
308 hpriv->em_buf_sz, PAGE_SIZE);
Harry Zhangc0623162010-04-23 17:28:38 +0800309 count = PAGE_SIZE;
310 }
311
312 for (i = 0; i < count; i += 4) {
313 msg = readl(em_mmio + i);
314 buf[i] = msg & 0xff;
315 buf[i + 1] = (msg >> 8) & 0xff;
316 buf[i + 2] = (msg >> 16) & 0xff;
317 buf[i + 3] = (msg >> 24) & 0xff;
318 }
319
320 spin_unlock_irqrestore(ap->lock, flags);
321
322 return i;
323}
324
325static ssize_t ahci_store_em_buffer(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf, size_t size)
328{
329 struct Scsi_Host *shost = class_to_shost(dev);
330 struct ata_port *ap = ata_shost_to_port(shost);
331 struct ahci_host_priv *hpriv = ap->host->private_data;
332 void __iomem *mmio = hpriv->mmio;
333 void __iomem *em_mmio = mmio + hpriv->em_loc;
Harry Zhangf9ce8892010-06-24 11:34:23 +0800334 const unsigned char *msg_buf = buf;
Harry Zhangc0623162010-04-23 17:28:38 +0800335 u32 em_ctl, msg;
336 unsigned long flags;
337 int i;
338
339 /* check size validity */
340 if (!(ap->flags & ATA_FLAG_EM) ||
341 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
342 size % 4 || size > hpriv->em_buf_sz)
343 return -EINVAL;
344
345 spin_lock_irqsave(ap->lock, flags);
346
347 em_ctl = readl(mmio + HOST_EM_CTL);
348 if (em_ctl & EM_CTL_TM) {
349 spin_unlock_irqrestore(ap->lock, flags);
350 return -EBUSY;
351 }
352
353 for (i = 0; i < size; i += 4) {
Harry Zhangf9ce8892010-06-24 11:34:23 +0800354 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
355 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
Harry Zhangc0623162010-04-23 17:28:38 +0800356 writel(msg, em_mmio + i);
357 }
358
359 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
360
361 spin_unlock_irqrestore(ap->lock, flags);
362
363 return size;
364}
365
Hannes Reinecke6e5fe5b12011-03-04 09:54:52 +0100366static ssize_t ahci_show_em_supported(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369 struct Scsi_Host *shost = class_to_shost(dev);
370 struct ata_port *ap = ata_shost_to_port(shost);
371 struct ahci_host_priv *hpriv = ap->host->private_data;
372 void __iomem *mmio = hpriv->mmio;
373 u32 em_ctl;
374
375 em_ctl = readl(mmio + HOST_EM_CTL);
376
377 return sprintf(buf, "%s%s%s%s\n",
378 em_ctl & EM_CTL_LED ? "led " : "",
379 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
380 em_ctl & EM_CTL_SES ? "ses-2 " : "",
381 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
382}
383
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400384/**
385 * ahci_save_initial_config - Save and fixup initial config values
386 * @dev: target AHCI device
387 * @hpriv: host private area to store config values
388 * @force_port_map: force port map to a specified value
389 * @mask_port_map: mask out particular bits from port map
390 *
391 * Some registers containing configuration info might be setup by
392 * BIOS and might be cleared on reset. This function saves the
393 * initial values of those registers into @hpriv such that they
394 * can be restored after controller reset.
395 *
396 * If inconsistent, config values are fixed up by this function.
397 *
398 * LOCKING:
399 * None.
400 */
401void ahci_save_initial_config(struct device *dev,
402 struct ahci_host_priv *hpriv,
403 unsigned int force_port_map,
404 unsigned int mask_port_map)
405{
406 void __iomem *mmio = hpriv->mmio;
407 u32 cap, cap2, vers, port_map;
408 int i;
409
410 /* make sure AHCI mode is enabled before accessing CAP */
411 ahci_enable_ahci(mmio);
412
413 /* Values prefixed with saved_ are written back to host after
414 * reset. Values without are used for driver operation.
415 */
416 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
417 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
418
419 /* CAP2 register is only defined for AHCI 1.2 and later */
420 vers = readl(mmio + HOST_VERSION);
421 if ((vers >> 16) > 1 ||
422 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
423 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
424 else
425 hpriv->saved_cap2 = cap2 = 0;
426
427 /* some chips have errata preventing 64bit use */
428 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700429 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400430 cap &= ~HOST_CAP_64;
431 }
432
433 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700434 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400435 cap &= ~HOST_CAP_NCQ;
436 }
437
438 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700439 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400440 cap |= HOST_CAP_NCQ;
441 }
442
443 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700444 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400445 cap &= ~HOST_CAP_PMP;
446 }
447
448 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700449 dev_info(dev,
450 "controller can't do SNTF, turning off CAP_SNTF\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400451 cap &= ~HOST_CAP_SNTF;
452 }
453
Tejun Heo5f173102010-07-24 16:53:48 +0200454 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700455 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
Tejun Heo5f173102010-07-24 16:53:48 +0200456 cap |= HOST_CAP_FBS;
457 }
458
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400459 if (force_port_map && port_map != force_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700460 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
461 port_map, force_port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400462 port_map = force_port_map;
463 }
464
465 if (mask_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700466 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
467 port_map,
468 port_map & mask_port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400469 port_map &= mask_port_map;
470 }
471
472 /* cross check port_map and cap.n_ports */
473 if (port_map) {
474 int map_ports = 0;
475
476 for (i = 0; i < AHCI_MAX_PORTS; i++)
477 if (port_map & (1 << i))
478 map_ports++;
479
480 /* If PI has more ports than n_ports, whine, clear
481 * port_map and let it be generated from n_ports.
482 */
483 if (map_ports > ahci_nr_ports(cap)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700484 dev_warn(dev,
485 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
486 port_map, ahci_nr_ports(cap));
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400487 port_map = 0;
488 }
489 }
490
491 /* fabricate port_map from cap.nr_ports */
492 if (!port_map) {
493 port_map = (1 << ahci_nr_ports(cap)) - 1;
Joe Perchesa44fec12011-04-15 15:51:58 -0700494 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400495
496 /* write the fixed up value to the PI register */
497 hpriv->saved_port_map = port_map;
498 }
499
500 /* record values to use during operation */
501 hpriv->cap = cap;
502 hpriv->cap2 = cap2;
503 hpriv->port_map = port_map;
504}
505EXPORT_SYMBOL_GPL(ahci_save_initial_config);
506
507/**
508 * ahci_restore_initial_config - Restore initial config
509 * @host: target ATA host
510 *
511 * Restore initial config stored by ahci_save_initial_config().
512 *
513 * LOCKING:
514 * None.
515 */
516static void ahci_restore_initial_config(struct ata_host *host)
517{
518 struct ahci_host_priv *hpriv = host->private_data;
519 void __iomem *mmio = hpriv->mmio;
520
521 writel(hpriv->saved_cap, mmio + HOST_CAP);
522 if (hpriv->saved_cap2)
523 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
524 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
525 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
526}
527
528static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
529{
530 static const int offset[] = {
531 [SCR_STATUS] = PORT_SCR_STAT,
532 [SCR_CONTROL] = PORT_SCR_CTL,
533 [SCR_ERROR] = PORT_SCR_ERR,
534 [SCR_ACTIVE] = PORT_SCR_ACT,
535 [SCR_NOTIFICATION] = PORT_SCR_NTF,
536 };
537 struct ahci_host_priv *hpriv = ap->host->private_data;
538
539 if (sc_reg < ARRAY_SIZE(offset) &&
540 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
541 return offset[sc_reg];
542 return 0;
543}
544
545static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
546{
547 void __iomem *port_mmio = ahci_port_base(link->ap);
548 int offset = ahci_scr_offset(link->ap, sc_reg);
549
550 if (offset) {
551 *val = readl(port_mmio + offset);
552 return 0;
553 }
554 return -EINVAL;
555}
556
557static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
558{
559 void __iomem *port_mmio = ahci_port_base(link->ap);
560 int offset = ahci_scr_offset(link->ap, sc_reg);
561
562 if (offset) {
563 writel(val, port_mmio + offset);
564 return 0;
565 }
566 return -EINVAL;
567}
568
569void ahci_start_engine(struct ata_port *ap)
570{
571 void __iomem *port_mmio = ahci_port_base(ap);
572 u32 tmp;
573
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400574 /* start DMA */
575 tmp = readl(port_mmio + PORT_CMD);
576 tmp |= PORT_CMD_START;
577 writel(tmp, port_mmio + PORT_CMD);
578 readl(port_mmio + PORT_CMD); /* flush */
579}
580EXPORT_SYMBOL_GPL(ahci_start_engine);
581
582int ahci_stop_engine(struct ata_port *ap)
583{
584 void __iomem *port_mmio = ahci_port_base(ap);
585 u32 tmp;
586
587 tmp = readl(port_mmio + PORT_CMD);
588
589 /* check if the HBA is idle */
590 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
591 return 0;
592
593 /* setting HBA to idle */
594 tmp &= ~PORT_CMD_START;
595 writel(tmp, port_mmio + PORT_CMD);
596
597 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo97750ce2010-09-06 17:56:29 +0200598 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400599 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
600 if (tmp & PORT_CMD_LIST_ON)
601 return -EIO;
602
603 return 0;
604}
605EXPORT_SYMBOL_GPL(ahci_stop_engine);
606
607static void ahci_start_fis_rx(struct ata_port *ap)
608{
609 void __iomem *port_mmio = ahci_port_base(ap);
610 struct ahci_host_priv *hpriv = ap->host->private_data;
611 struct ahci_port_priv *pp = ap->private_data;
612 u32 tmp;
613
614 /* set FIS registers */
615 if (hpriv->cap & HOST_CAP_64)
616 writel((pp->cmd_slot_dma >> 16) >> 16,
617 port_mmio + PORT_LST_ADDR_HI);
618 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
619
620 if (hpriv->cap & HOST_CAP_64)
621 writel((pp->rx_fis_dma >> 16) >> 16,
622 port_mmio + PORT_FIS_ADDR_HI);
623 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
624
625 /* enable FIS reception */
626 tmp = readl(port_mmio + PORT_CMD);
627 tmp |= PORT_CMD_FIS_RX;
628 writel(tmp, port_mmio + PORT_CMD);
629
630 /* flush */
631 readl(port_mmio + PORT_CMD);
632}
633
634static int ahci_stop_fis_rx(struct ata_port *ap)
635{
636 void __iomem *port_mmio = ahci_port_base(ap);
637 u32 tmp;
638
639 /* disable FIS reception */
640 tmp = readl(port_mmio + PORT_CMD);
641 tmp &= ~PORT_CMD_FIS_RX;
642 writel(tmp, port_mmio + PORT_CMD);
643
644 /* wait for completion, spec says 500ms, give it 1000 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200645 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400646 PORT_CMD_FIS_ON, 10, 1000);
647 if (tmp & PORT_CMD_FIS_ON)
648 return -EBUSY;
649
650 return 0;
651}
652
653static void ahci_power_up(struct ata_port *ap)
654{
655 struct ahci_host_priv *hpriv = ap->host->private_data;
656 void __iomem *port_mmio = ahci_port_base(ap);
657 u32 cmd;
658
659 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
660
661 /* spin up device */
662 if (hpriv->cap & HOST_CAP_SSS) {
663 cmd |= PORT_CMD_SPIN_UP;
664 writel(cmd, port_mmio + PORT_CMD);
665 }
666
667 /* wake up link */
668 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
669}
670
Tejun Heo6b7ae952010-09-01 17:50:06 +0200671static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
672 unsigned int hints)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400673{
Tejun Heo6b7ae952010-09-01 17:50:06 +0200674 struct ata_port *ap = link->ap;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400675 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400676 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400677 void __iomem *port_mmio = ahci_port_base(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400678
Tejun Heo6b7ae952010-09-01 17:50:06 +0200679 if (policy != ATA_LPM_MAX_POWER) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400680 /*
Tejun Heo6b7ae952010-09-01 17:50:06 +0200681 * Disable interrupts on Phy Ready. This keeps us from
682 * getting woken up due to spurious phy ready
683 * interrupts.
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400684 */
Tejun Heo6b7ae952010-09-01 17:50:06 +0200685 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
686 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
687
688 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400689 }
690
Tejun Heo6b7ae952010-09-01 17:50:06 +0200691 if (hpriv->cap & HOST_CAP_ALPM) {
692 u32 cmd = readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400693
Tejun Heo6b7ae952010-09-01 17:50:06 +0200694 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
695 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
696 cmd |= PORT_CMD_ICC_ACTIVE;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400697
Tejun Heo6b7ae952010-09-01 17:50:06 +0200698 writel(cmd, port_mmio + PORT_CMD);
699 readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400700
Tejun Heo6b7ae952010-09-01 17:50:06 +0200701 /* wait 10ms to be sure we've come out of LPM state */
Tejun Heo97750ce2010-09-06 17:56:29 +0200702 ata_msleep(ap, 10);
Tejun Heo6b7ae952010-09-01 17:50:06 +0200703 } else {
704 cmd |= PORT_CMD_ALPE;
705 if (policy == ATA_LPM_MIN_POWER)
706 cmd |= PORT_CMD_ASP;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400707
Tejun Heo6b7ae952010-09-01 17:50:06 +0200708 /* write out new cmd value */
709 writel(cmd, port_mmio + PORT_CMD);
710 }
711 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400712
Shane Huang65fe1f02012-09-07 22:40:01 +0800713 /* set aggressive device sleep */
714 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
715 (hpriv->cap2 & HOST_CAP2_SADM) &&
716 (link->device->flags & ATA_DFLAG_DEVSLP)) {
717 if (policy == ATA_LPM_MIN_POWER)
718 ahci_set_aggressive_devslp(ap, true);
719 else
720 ahci_set_aggressive_devslp(ap, false);
721 }
722
Tejun Heo6b7ae952010-09-01 17:50:06 +0200723 if (policy == ATA_LPM_MAX_POWER) {
724 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400725
Tejun Heo6b7ae952010-09-01 17:50:06 +0200726 /* turn PHYRDY IRQ back on */
727 pp->intr_mask |= PORT_IRQ_PHYRDY;
728 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
729 }
730
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400731 return 0;
732}
733
734#ifdef CONFIG_PM
735static void ahci_power_down(struct ata_port *ap)
736{
737 struct ahci_host_priv *hpriv = ap->host->private_data;
738 void __iomem *port_mmio = ahci_port_base(ap);
739 u32 cmd, scontrol;
740
741 if (!(hpriv->cap & HOST_CAP_SSS))
742 return;
743
744 /* put device into listen mode, first set PxSCTL.DET to 0 */
745 scontrol = readl(port_mmio + PORT_SCR_CTL);
746 scontrol &= ~0xf;
747 writel(scontrol, port_mmio + PORT_SCR_CTL);
748
749 /* then set PxCMD.SUD to 0 */
750 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
751 cmd &= ~PORT_CMD_SPIN_UP;
752 writel(cmd, port_mmio + PORT_CMD);
753}
754#endif
755
756static void ahci_start_port(struct ata_port *ap)
757{
Brian Norris66583c92012-02-21 10:38:42 -0800758 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400759 struct ahci_port_priv *pp = ap->private_data;
760 struct ata_link *link;
761 struct ahci_em_priv *emp;
762 ssize_t rc;
763 int i;
764
765 /* enable FIS reception */
766 ahci_start_fis_rx(ap);
767
Brian Norris66583c92012-02-21 10:38:42 -0800768 /* enable DMA */
769 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
770 ahci_start_engine(ap);
771
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400772 /* turn on LEDs */
773 if (ap->flags & ATA_FLAG_EM) {
774 ata_for_each_link(link, ap, EDGE) {
775 emp = &pp->em_priv[link->pmp];
776
777 /* EM Transmit bit maybe busy during init */
778 for (i = 0; i < EM_MAX_RETRY; i++) {
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500779 rc = ap->ops->transmit_led_message(ap,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400780 emp->led_state,
781 4);
782 if (rc == -EBUSY)
Tejun Heo97750ce2010-09-06 17:56:29 +0200783 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400784 else
785 break;
786 }
787 }
788 }
789
790 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
791 ata_for_each_link(link, ap, EDGE)
792 ahci_init_sw_activity(link);
793
794}
795
796static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
797{
798 int rc;
799
800 /* disable DMA */
801 rc = ahci_stop_engine(ap);
802 if (rc) {
803 *emsg = "failed to stop engine";
804 return rc;
805 }
806
807 /* disable FIS reception */
808 rc = ahci_stop_fis_rx(ap);
809 if (rc) {
810 *emsg = "failed stop FIS RX";
811 return rc;
812 }
813
814 return 0;
815}
816
817int ahci_reset_controller(struct ata_host *host)
818{
819 struct ahci_host_priv *hpriv = host->private_data;
820 void __iomem *mmio = hpriv->mmio;
821 u32 tmp;
822
823 /* we must be in AHCI mode, before using anything
824 * AHCI-specific, such as HOST_RESET.
825 */
826 ahci_enable_ahci(mmio);
827
828 /* global controller reset */
829 if (!ahci_skip_host_reset) {
830 tmp = readl(mmio + HOST_CTL);
831 if ((tmp & HOST_RESET) == 0) {
832 writel(tmp | HOST_RESET, mmio + HOST_CTL);
833 readl(mmio + HOST_CTL); /* flush */
834 }
835
836 /*
837 * to perform host reset, OS should set HOST_RESET
838 * and poll until this bit is read to be "0".
839 * reset must complete within 1 second, or
840 * the hardware should be considered fried.
841 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200842 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400843 HOST_RESET, 10, 1000);
844
845 if (tmp & HOST_RESET) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700846 dev_err(host->dev, "controller reset failed (0x%x)\n",
847 tmp);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400848 return -EIO;
849 }
850
851 /* turn on AHCI mode */
852 ahci_enable_ahci(mmio);
853
854 /* Some registers might be cleared on reset. Restore
855 * initial values.
856 */
857 ahci_restore_initial_config(host);
858 } else
Joe Perchesa44fec12011-04-15 15:51:58 -0700859 dev_info(host->dev, "skipping global host reset\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400860
861 return 0;
862}
863EXPORT_SYMBOL_GPL(ahci_reset_controller);
864
865static void ahci_sw_activity(struct ata_link *link)
866{
867 struct ata_port *ap = link->ap;
868 struct ahci_port_priv *pp = ap->private_data;
869 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
870
871 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
872 return;
873
874 emp->activity++;
875 if (!timer_pending(&emp->timer))
876 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
877}
878
879static void ahci_sw_activity_blink(unsigned long arg)
880{
881 struct ata_link *link = (struct ata_link *)arg;
882 struct ata_port *ap = link->ap;
883 struct ahci_port_priv *pp = ap->private_data;
884 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
885 unsigned long led_message = emp->led_state;
886 u32 activity_led_state;
887 unsigned long flags;
888
889 led_message &= EM_MSG_LED_VALUE;
890 led_message |= ap->port_no | (link->pmp << 8);
891
892 /* check to see if we've had activity. If so,
893 * toggle state of LED and reset timer. If not,
894 * turn LED to desired idle state.
895 */
896 spin_lock_irqsave(ap->lock, flags);
897 if (emp->saved_activity != emp->activity) {
898 emp->saved_activity = emp->activity;
899 /* get the current LED state */
900 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
901
902 if (activity_led_state)
903 activity_led_state = 0;
904 else
905 activity_led_state = 1;
906
907 /* clear old state */
908 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
909
910 /* toggle state */
911 led_message |= (activity_led_state << 16);
912 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
913 } else {
914 /* switch to idle */
915 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
916 if (emp->blink_policy == BLINK_OFF)
917 led_message |= (1 << 16);
918 }
919 spin_unlock_irqrestore(ap->lock, flags);
Mark Langsdorf439d7a32013-05-30 15:17:30 -0500920 ap->ops->transmit_led_message(ap, led_message, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400921}
922
923static void ahci_init_sw_activity(struct ata_link *link)
924{
925 struct ata_port *ap = link->ap;
926 struct ahci_port_priv *pp = ap->private_data;
927 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
928
929 /* init activity stats, setup timer */
930 emp->saved_activity = emp->activity = 0;
931 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
932
933 /* check our blink policy and set flag for link if it's enabled */
934 if (emp->blink_policy)
935 link->flags |= ATA_LFLAG_SW_ACTIVITY;
936}
937
938int ahci_reset_em(struct ata_host *host)
939{
940 struct ahci_host_priv *hpriv = host->private_data;
941 void __iomem *mmio = hpriv->mmio;
942 u32 em_ctl;
943
944 em_ctl = readl(mmio + HOST_EM_CTL);
945 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
946 return -EINVAL;
947
948 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
949 return 0;
950}
951EXPORT_SYMBOL_GPL(ahci_reset_em);
952
953static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
954 ssize_t size)
955{
956 struct ahci_host_priv *hpriv = ap->host->private_data;
957 struct ahci_port_priv *pp = ap->private_data;
958 void __iomem *mmio = hpriv->mmio;
959 u32 em_ctl;
960 u32 message[] = {0, 0};
961 unsigned long flags;
962 int pmp;
963 struct ahci_em_priv *emp;
964
965 /* get the slot number from the message */
966 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
967 if (pmp < EM_MAX_SLOTS)
968 emp = &pp->em_priv[pmp];
969 else
970 return -EINVAL;
971
972 spin_lock_irqsave(ap->lock, flags);
973
974 /*
975 * if we are still busy transmitting a previous message,
976 * do not allow
977 */
978 em_ctl = readl(mmio + HOST_EM_CTL);
979 if (em_ctl & EM_CTL_TM) {
980 spin_unlock_irqrestore(ap->lock, flags);
981 return -EBUSY;
982 }
983
Harry Zhang008dbd62010-04-23 17:27:19 +0800984 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
985 /*
986 * create message header - this is all zero except for
987 * the message size, which is 4 bytes.
988 */
989 message[0] |= (4 << 8);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400990
Harry Zhang008dbd62010-04-23 17:27:19 +0800991 /* ignore 0:4 of byte zero, fill in port info yourself */
992 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400993
Harry Zhang008dbd62010-04-23 17:27:19 +0800994 /* write message to EM_LOC */
995 writel(message[0], mmio + hpriv->em_loc);
996 writel(message[1], mmio + hpriv->em_loc+4);
997
998 /*
999 * tell hardware to transmit the message
1000 */
1001 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1002 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001003
1004 /* save off new led state for port/slot */
1005 emp->led_state = state;
1006
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001007 spin_unlock_irqrestore(ap->lock, flags);
1008 return size;
1009}
1010
1011static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1012{
1013 struct ahci_port_priv *pp = ap->private_data;
1014 struct ata_link *link;
1015 struct ahci_em_priv *emp;
1016 int rc = 0;
1017
1018 ata_for_each_link(link, ap, EDGE) {
1019 emp = &pp->em_priv[link->pmp];
1020 rc += sprintf(buf, "%lx\n", emp->led_state);
1021 }
1022 return rc;
1023}
1024
1025static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1026 size_t size)
1027{
1028 int state;
1029 int pmp;
1030 struct ahci_port_priv *pp = ap->private_data;
1031 struct ahci_em_priv *emp;
1032
1033 state = simple_strtoul(buf, NULL, 0);
1034
1035 /* get the slot number from the message */
1036 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1037 if (pmp < EM_MAX_SLOTS)
1038 emp = &pp->em_priv[pmp];
1039 else
1040 return -EINVAL;
1041
1042 /* mask off the activity bits if we are in sw_activity
1043 * mode, user should turn off sw_activity before setting
1044 * activity led through em_message
1045 */
1046 if (emp->blink_policy)
1047 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1048
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001049 return ap->ops->transmit_led_message(ap, state, size);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001050}
1051
1052static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1053{
1054 struct ata_link *link = dev->link;
1055 struct ata_port *ap = link->ap;
1056 struct ahci_port_priv *pp = ap->private_data;
1057 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1058 u32 port_led_state = emp->led_state;
1059
1060 /* save the desired Activity LED behavior */
1061 if (val == OFF) {
1062 /* clear LFLAG */
1063 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1064
1065 /* set the LED to OFF */
1066 port_led_state &= EM_MSG_LED_VALUE_OFF;
1067 port_led_state |= (ap->port_no | (link->pmp << 8));
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001068 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001069 } else {
1070 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1071 if (val == BLINK_OFF) {
1072 /* set LED to ON for idle */
1073 port_led_state &= EM_MSG_LED_VALUE_OFF;
1074 port_led_state |= (ap->port_no | (link->pmp << 8));
1075 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
Mark Langsdorf439d7a32013-05-30 15:17:30 -05001076 ap->ops->transmit_led_message(ap, port_led_state, 4);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001077 }
1078 }
1079 emp->blink_policy = val;
1080 return 0;
1081}
1082
1083static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1084{
1085 struct ata_link *link = dev->link;
1086 struct ata_port *ap = link->ap;
1087 struct ahci_port_priv *pp = ap->private_data;
1088 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1089
1090 /* display the saved value of activity behavior for this
1091 * disk.
1092 */
1093 return sprintf(buf, "%d\n", emp->blink_policy);
1094}
1095
1096static void ahci_port_init(struct device *dev, struct ata_port *ap,
1097 int port_no, void __iomem *mmio,
1098 void __iomem *port_mmio)
1099{
1100 const char *emsg = NULL;
1101 int rc;
1102 u32 tmp;
1103
1104 /* make sure port is not active */
1105 rc = ahci_deinit_port(ap, &emsg);
1106 if (rc)
1107 dev_warn(dev, "%s (%d)\n", emsg, rc);
1108
1109 /* clear SError */
1110 tmp = readl(port_mmio + PORT_SCR_ERR);
1111 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1112 writel(tmp, port_mmio + PORT_SCR_ERR);
1113
1114 /* clear port IRQ */
1115 tmp = readl(port_mmio + PORT_IRQ_STAT);
1116 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1117 if (tmp)
1118 writel(tmp, port_mmio + PORT_IRQ_STAT);
1119
1120 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1121}
1122
1123void ahci_init_controller(struct ata_host *host)
1124{
1125 struct ahci_host_priv *hpriv = host->private_data;
1126 void __iomem *mmio = hpriv->mmio;
1127 int i;
1128 void __iomem *port_mmio;
1129 u32 tmp;
1130
1131 for (i = 0; i < host->n_ports; i++) {
1132 struct ata_port *ap = host->ports[i];
1133
1134 port_mmio = ahci_port_base(ap);
1135 if (ata_port_is_dummy(ap))
1136 continue;
1137
1138 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1139 }
1140
1141 tmp = readl(mmio + HOST_CTL);
1142 VPRINTK("HOST_CTL 0x%x\n", tmp);
1143 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1144 tmp = readl(mmio + HOST_CTL);
1145 VPRINTK("HOST_CTL 0x%x\n", tmp);
1146}
1147EXPORT_SYMBOL_GPL(ahci_init_controller);
1148
1149static void ahci_dev_config(struct ata_device *dev)
1150{
1151 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1152
1153 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1154 dev->max_sectors = 255;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001155 ata_dev_info(dev,
1156 "SB600 AHCI: limiting to 255 sectors per cmd\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001157 }
1158}
1159
Rob Herringbbb4ab42012-08-17 09:51:50 -05001160unsigned int ahci_dev_classify(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001161{
1162 void __iomem *port_mmio = ahci_port_base(ap);
1163 struct ata_taskfile tf;
1164 u32 tmp;
1165
1166 tmp = readl(port_mmio + PORT_SIG);
1167 tf.lbah = (tmp >> 24) & 0xff;
1168 tf.lbam = (tmp >> 16) & 0xff;
1169 tf.lbal = (tmp >> 8) & 0xff;
1170 tf.nsect = (tmp) & 0xff;
1171
1172 return ata_dev_classify(&tf);
1173}
Rob Herringbbb4ab42012-08-17 09:51:50 -05001174EXPORT_SYMBOL_GPL(ahci_dev_classify);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001175
David Milburn02cdfcf2010-11-12 15:38:21 -06001176void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1177 u32 opts)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001178{
1179 dma_addr_t cmd_tbl_dma;
1180
1181 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1182
1183 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1184 pp->cmd_slot[tag].status = 0;
1185 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1186 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1187}
David Milburn02cdfcf2010-11-12 15:38:21 -06001188EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001189
1190int ahci_kick_engine(struct ata_port *ap)
1191{
1192 void __iomem *port_mmio = ahci_port_base(ap);
1193 struct ahci_host_priv *hpriv = ap->host->private_data;
1194 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1195 u32 tmp;
1196 int busy, rc;
1197
1198 /* stop engine */
1199 rc = ahci_stop_engine(ap);
1200 if (rc)
1201 goto out_restart;
1202
1203 /* need to do CLO?
1204 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1205 */
1206 busy = status & (ATA_BUSY | ATA_DRQ);
1207 if (!busy && !sata_pmp_attached(ap)) {
1208 rc = 0;
1209 goto out_restart;
1210 }
1211
1212 if (!(hpriv->cap & HOST_CAP_CLO)) {
1213 rc = -EOPNOTSUPP;
1214 goto out_restart;
1215 }
1216
1217 /* perform CLO */
1218 tmp = readl(port_mmio + PORT_CMD);
1219 tmp |= PORT_CMD_CLO;
1220 writel(tmp, port_mmio + PORT_CMD);
1221
1222 rc = 0;
Tejun Heo97750ce2010-09-06 17:56:29 +02001223 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001224 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1225 if (tmp & PORT_CMD_CLO)
1226 rc = -EIO;
1227
1228 /* restart engine */
1229 out_restart:
1230 ahci_start_engine(ap);
1231 return rc;
1232}
1233EXPORT_SYMBOL_GPL(ahci_kick_engine);
1234
1235static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1236 struct ata_taskfile *tf, int is_cmd, u16 flags,
1237 unsigned long timeout_msec)
1238{
1239 const u32 cmd_fis_len = 5; /* five dwords */
1240 struct ahci_port_priv *pp = ap->private_data;
1241 void __iomem *port_mmio = ahci_port_base(ap);
1242 u8 *fis = pp->cmd_tbl;
1243 u32 tmp;
1244
1245 /* prep the command */
1246 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1247 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1248
1249 /* issue & wait */
1250 writel(1, port_mmio + PORT_CMD_ISSUE);
1251
1252 if (timeout_msec) {
Tejun Heo97750ce2010-09-06 17:56:29 +02001253 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1254 0x1, 0x1, 1, timeout_msec);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001255 if (tmp & 0x1) {
1256 ahci_kick_engine(ap);
1257 return -EBUSY;
1258 }
1259 } else
1260 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1261
1262 return 0;
1263}
1264
1265int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1266 int pmp, unsigned long deadline,
1267 int (*check_ready)(struct ata_link *link))
1268{
1269 struct ata_port *ap = link->ap;
1270 struct ahci_host_priv *hpriv = ap->host->private_data;
1271 const char *reason = NULL;
1272 unsigned long now, msecs;
1273 struct ata_taskfile tf;
1274 int rc;
1275
1276 DPRINTK("ENTER\n");
1277
1278 /* prepare for SRST (AHCI-1.1 10.4.1) */
1279 rc = ahci_kick_engine(ap);
1280 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001281 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001282
1283 ata_tf_init(link->device, &tf);
1284
1285 /* issue the first D2H Register FIS */
1286 msecs = 0;
1287 now = jiffies;
Tejun Heof1f5a802010-08-27 11:09:15 +02001288 if (time_after(deadline, now))
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001289 msecs = jiffies_to_msecs(deadline - now);
1290
1291 tf.ctl |= ATA_SRST;
1292 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1293 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1294 rc = -EIO;
1295 reason = "1st FIS failed";
1296 goto fail;
1297 }
1298
1299 /* spec says at least 5us, but be generous and sleep for 1ms */
Tejun Heo97750ce2010-09-06 17:56:29 +02001300 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001301
1302 /* issue the second D2H Register FIS */
1303 tf.ctl &= ~ATA_SRST;
1304 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1305
1306 /* wait for link to become ready */
1307 rc = ata_wait_after_reset(link, deadline, check_ready);
1308 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1309 /*
1310 * Workaround for cases where link online status can't
1311 * be trusted. Treat device readiness timeout as link
1312 * offline.
1313 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001314 ata_link_info(link, "device not ready, treating as offline\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001315 *class = ATA_DEV_NONE;
1316 } else if (rc) {
1317 /* link occupied, -ENODEV too is an error */
1318 reason = "device not ready";
1319 goto fail;
1320 } else
1321 *class = ahci_dev_classify(ap);
1322
1323 DPRINTK("EXIT, class=%u\n", *class);
1324 return 0;
1325
1326 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07001327 ata_link_err(link, "softreset failed (%s)\n", reason);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001328 return rc;
1329}
1330
1331int ahci_check_ready(struct ata_link *link)
1332{
1333 void __iomem *port_mmio = ahci_port_base(link->ap);
1334 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1335
1336 return ata_check_ready(status);
1337}
1338EXPORT_SYMBOL_GPL(ahci_check_ready);
1339
1340static int ahci_softreset(struct ata_link *link, unsigned int *class,
1341 unsigned long deadline)
1342{
1343 int pmp = sata_srst_pmp(link);
1344
1345 DPRINTK("ENTER\n");
1346
1347 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1348}
1349EXPORT_SYMBOL_GPL(ahci_do_softreset);
1350
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001351static int ahci_bad_pmp_check_ready(struct ata_link *link)
1352{
1353 void __iomem *port_mmio = ahci_port_base(link->ap);
1354 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1355 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1356
1357 /*
1358 * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1359 * which can save timeout delay.
1360 */
1361 if (irq_status & PORT_IRQ_BAD_PMP)
1362 return -EIO;
1363
1364 return ata_check_ready(status);
1365}
1366
1367int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1368 unsigned long deadline)
1369{
1370 struct ata_port *ap = link->ap;
1371 void __iomem *port_mmio = ahci_port_base(ap);
1372 int pmp = sata_srst_pmp(link);
1373 int rc;
1374 u32 irq_sts;
1375
1376 DPRINTK("ENTER\n");
1377
1378 rc = ahci_do_softreset(link, class, pmp, deadline,
1379 ahci_bad_pmp_check_ready);
1380
1381 /*
1382 * Soft reset fails with IPMS set when PMP is enabled but
1383 * SATA HDD/ODD is connected to SATA port, do soft reset
1384 * again to port 0.
1385 */
1386 if (rc == -EIO) {
1387 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1388 if (irq_sts & PORT_IRQ_BAD_PMP) {
Wei Yongjun39f80ac2012-12-03 23:39:31 -05001389 ata_link_warn(link,
Yuan-Hsin Chen345347c2011-06-21 17:17:38 +08001390 "applying PMP SRST workaround "
1391 "and retrying\n");
1392 rc = ahci_do_softreset(link, class, 0, deadline,
1393 ahci_check_ready);
1394 }
1395 }
1396
1397 return rc;
1398}
1399
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001400static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1401 unsigned long deadline)
1402{
1403 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1404 struct ata_port *ap = link->ap;
1405 struct ahci_port_priv *pp = ap->private_data;
1406 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1407 struct ata_taskfile tf;
1408 bool online;
1409 int rc;
1410
1411 DPRINTK("ENTER\n");
1412
1413 ahci_stop_engine(ap);
1414
1415 /* clear D2H reception area to properly wait for D2H FIS */
1416 ata_tf_init(link->device, &tf);
Sergei Shtylyov9bbb1b02013-06-23 01:39:39 +04001417 tf.command = ATA_BUSY;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001418 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1419
1420 rc = sata_link_hardreset(link, timing, deadline, &online,
1421 ahci_check_ready);
1422
1423 ahci_start_engine(ap);
1424
1425 if (online)
1426 *class = ahci_dev_classify(ap);
1427
1428 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1429 return rc;
1430}
1431
1432static void ahci_postreset(struct ata_link *link, unsigned int *class)
1433{
1434 struct ata_port *ap = link->ap;
1435 void __iomem *port_mmio = ahci_port_base(ap);
1436 u32 new_tmp, tmp;
1437
1438 ata_std_postreset(link, class);
1439
1440 /* Make sure port's ATAPI bit is set appropriately */
1441 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1442 if (*class == ATA_DEV_ATAPI)
1443 new_tmp |= PORT_CMD_ATAPI;
1444 else
1445 new_tmp &= ~PORT_CMD_ATAPI;
1446 if (new_tmp != tmp) {
1447 writel(new_tmp, port_mmio + PORT_CMD);
1448 readl(port_mmio + PORT_CMD); /* flush */
1449 }
1450}
1451
1452static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1453{
1454 struct scatterlist *sg;
1455 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1456 unsigned int si;
1457
1458 VPRINTK("ENTER\n");
1459
1460 /*
1461 * Next, the S/G list.
1462 */
1463 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1464 dma_addr_t addr = sg_dma_address(sg);
1465 u32 sg_len = sg_dma_len(sg);
1466
1467 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1468 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1469 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1470 }
1471
1472 return si;
1473}
1474
1475static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1476{
1477 struct ata_port *ap = qc->ap;
1478 struct ahci_port_priv *pp = ap->private_data;
1479
1480 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1481 return ata_std_qc_defer(qc);
1482 else
1483 return sata_pmp_qc_defer_cmd_switch(qc);
1484}
1485
1486static void ahci_qc_prep(struct ata_queued_cmd *qc)
1487{
1488 struct ata_port *ap = qc->ap;
1489 struct ahci_port_priv *pp = ap->private_data;
1490 int is_atapi = ata_is_atapi(qc->tf.protocol);
1491 void *cmd_tbl;
1492 u32 opts;
1493 const u32 cmd_fis_len = 5; /* five dwords */
1494 unsigned int n_elem;
1495
1496 /*
1497 * Fill in command table information. First, the header,
1498 * a SATA Register - Host to Device command FIS.
1499 */
1500 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1501
1502 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1503 if (is_atapi) {
1504 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1505 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1506 }
1507
1508 n_elem = 0;
1509 if (qc->flags & ATA_QCFLAG_DMAMAP)
1510 n_elem = ahci_fill_sg(qc, cmd_tbl);
1511
1512 /*
1513 * Fill in command slot information.
1514 */
1515 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1516 if (qc->tf.flags & ATA_TFLAG_WRITE)
1517 opts |= AHCI_CMD_WRITE;
1518 if (is_atapi)
1519 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1520
1521 ahci_fill_cmd_slot(pp, qc->tag, opts);
1522}
1523
1524static void ahci_fbs_dec_intr(struct ata_port *ap)
1525{
1526 struct ahci_port_priv *pp = ap->private_data;
1527 void __iomem *port_mmio = ahci_port_base(ap);
1528 u32 fbs = readl(port_mmio + PORT_FBS);
1529 int retries = 3;
1530
1531 DPRINTK("ENTER\n");
1532 BUG_ON(!pp->fbs_enabled);
1533
1534 /* time to wait for DEC is not specified by AHCI spec,
1535 * add a retry loop for safety.
1536 */
1537 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1538 fbs = readl(port_mmio + PORT_FBS);
1539 while ((fbs & PORT_FBS_DEC) && retries--) {
1540 udelay(1);
1541 fbs = readl(port_mmio + PORT_FBS);
1542 }
1543
1544 if (fbs & PORT_FBS_DEC)
Joe Perchesa44fec12011-04-15 15:51:58 -07001545 dev_err(ap->host->dev, "failed to clear device error\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001546}
1547
1548static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1549{
1550 struct ahci_host_priv *hpriv = ap->host->private_data;
1551 struct ahci_port_priv *pp = ap->private_data;
1552 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1553 struct ata_link *link = NULL;
1554 struct ata_queued_cmd *active_qc;
1555 struct ata_eh_info *active_ehi;
1556 bool fbs_need_dec = false;
1557 u32 serror;
1558
1559 /* determine active link with error */
1560 if (pp->fbs_enabled) {
1561 void __iomem *port_mmio = ahci_port_base(ap);
1562 u32 fbs = readl(port_mmio + PORT_FBS);
1563 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1564
Shane Huang912b9ac2013-06-08 16:00:16 +08001565 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001566 link = &ap->pmp_link[pmp];
1567 fbs_need_dec = true;
1568 }
1569
1570 } else
1571 ata_for_each_link(link, ap, EDGE)
1572 if (ata_link_active(link))
1573 break;
1574
1575 if (!link)
1576 link = &ap->link;
1577
1578 active_qc = ata_qc_from_tag(ap, link->active_tag);
1579 active_ehi = &link->eh_info;
1580
1581 /* record irq stat */
1582 ata_ehi_clear_desc(host_ehi);
1583 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1584
1585 /* AHCI needs SError cleared; otherwise, it might lock up */
1586 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1587 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1588 host_ehi->serror |= serror;
1589
1590 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1591 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1592 irq_stat &= ~PORT_IRQ_IF_ERR;
1593
1594 if (irq_stat & PORT_IRQ_TF_ERR) {
1595 /* If qc is active, charge it; otherwise, the active
1596 * link. There's no active qc on NCQ errors. It will
1597 * be determined by EH by reading log page 10h.
1598 */
1599 if (active_qc)
1600 active_qc->err_mask |= AC_ERR_DEV;
1601 else
1602 active_ehi->err_mask |= AC_ERR_DEV;
1603
1604 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1605 host_ehi->serror &= ~SERR_INTERNAL;
1606 }
1607
1608 if (irq_stat & PORT_IRQ_UNK_FIS) {
1609 u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
1610
1611 active_ehi->err_mask |= AC_ERR_HSM;
1612 active_ehi->action |= ATA_EH_RESET;
1613 ata_ehi_push_desc(active_ehi,
1614 "unknown FIS %08x %08x %08x %08x" ,
1615 unk[0], unk[1], unk[2], unk[3]);
1616 }
1617
1618 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1619 active_ehi->err_mask |= AC_ERR_HSM;
1620 active_ehi->action |= ATA_EH_RESET;
1621 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1622 }
1623
1624 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1625 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1626 host_ehi->action |= ATA_EH_RESET;
1627 ata_ehi_push_desc(host_ehi, "host bus error");
1628 }
1629
1630 if (irq_stat & PORT_IRQ_IF_ERR) {
1631 if (fbs_need_dec)
1632 active_ehi->err_mask |= AC_ERR_DEV;
1633 else {
1634 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1635 host_ehi->action |= ATA_EH_RESET;
1636 }
1637
1638 ata_ehi_push_desc(host_ehi, "interface fatal error");
1639 }
1640
1641 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1642 ata_ehi_hotplugged(host_ehi);
1643 ata_ehi_push_desc(host_ehi, "%s",
1644 irq_stat & PORT_IRQ_CONNECT ?
1645 "connection status changed" : "PHY RDY changed");
1646 }
1647
1648 /* okay, let's hand over to EH */
1649
1650 if (irq_stat & PORT_IRQ_FREEZE)
1651 ata_port_freeze(ap);
1652 else if (fbs_need_dec) {
1653 ata_link_abort(link);
1654 ahci_fbs_dec_intr(ap);
1655 } else
1656 ata_port_abort(ap);
1657}
1658
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001659static void ahci_handle_port_interrupt(struct ata_port *ap,
1660 void __iomem *port_mmio, u32 status)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001661{
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001662 struct ata_eh_info *ehi = &ap->link.eh_info;
1663 struct ahci_port_priv *pp = ap->private_data;
1664 struct ahci_host_priv *hpriv = ap->host->private_data;
1665 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001666 u32 qc_active = 0;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001667 int rc;
1668
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001669 /* ignore BAD_PMP while resetting */
1670 if (unlikely(resetting))
1671 status &= ~PORT_IRQ_BAD_PMP;
1672
Tejun Heo6b7ae952010-09-01 17:50:06 +02001673 /* if LPM is enabled, PHYRDY doesn't mean anything */
1674 if (ap->link.lpm_policy > ATA_LPM_MAX_POWER) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001675 status &= ~PORT_IRQ_PHYRDY;
Tejun Heo6b7ae952010-09-01 17:50:06 +02001676 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001677 }
1678
1679 if (unlikely(status & PORT_IRQ_ERROR)) {
1680 ahci_error_intr(ap, status);
1681 return;
1682 }
1683
1684 if (status & PORT_IRQ_SDB_FIS) {
1685 /* If SNotification is available, leave notification
1686 * handling to sata_async_notification(). If not,
1687 * emulate it by snooping SDB FIS RX area.
1688 *
1689 * Snooping FIS RX area is probably cheaper than
1690 * poking SNotification but some constrollers which
1691 * implement SNotification, ICH9 for example, don't
1692 * store AN SDB FIS into receive area.
1693 */
1694 if (hpriv->cap & HOST_CAP_SNTF)
1695 sata_async_notification(ap);
1696 else {
1697 /* If the 'N' bit in word 0 of the FIS is set,
1698 * we just received asynchronous notification.
1699 * Tell libata about it.
1700 *
1701 * Lack of SNotification should not appear in
1702 * ahci 1.2, so the workaround is unnecessary
1703 * when FBS is enabled.
1704 */
1705 if (pp->fbs_enabled)
1706 WARN_ON_ONCE(1);
1707 else {
1708 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1709 u32 f0 = le32_to_cpu(f[0]);
1710 if (f0 & (1 << 15))
1711 sata_async_notification(ap);
1712 }
1713 }
1714 }
1715
1716 /* pp->active_link is not reliable once FBS is enabled, both
1717 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1718 * NCQ and non-NCQ commands may be in flight at the same time.
1719 */
1720 if (pp->fbs_enabled) {
1721 if (ap->qc_active) {
1722 qc_active = readl(port_mmio + PORT_SCR_ACT);
1723 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1724 }
1725 } else {
1726 /* pp->active_link is valid iff any command is in flight */
1727 if (ap->qc_active && pp->active_link->sactive)
1728 qc_active = readl(port_mmio + PORT_SCR_ACT);
1729 else
1730 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1731 }
1732
1733
1734 rc = ata_qc_complete_multiple(ap, qc_active);
1735
1736 /* while resetting, invalid completions are expected */
1737 if (unlikely(rc < 0 && !resetting)) {
1738 ehi->err_mask |= AC_ERR_HSM;
1739 ehi->action |= ATA_EH_RESET;
1740 ata_port_freeze(ap);
1741 }
1742}
1743
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01001744void ahci_port_intr(struct ata_port *ap)
1745{
1746 void __iomem *port_mmio = ahci_port_base(ap);
1747 u32 status;
1748
1749 status = readl(port_mmio + PORT_IRQ_STAT);
1750 writel(status, port_mmio + PORT_IRQ_STAT);
1751
1752 ahci_handle_port_interrupt(ap, port_mmio, status);
1753}
1754
1755irqreturn_t ahci_thread_fn(int irq, void *dev_instance)
1756{
1757 struct ata_port *ap = dev_instance;
1758 struct ahci_port_priv *pp = ap->private_data;
1759 void __iomem *port_mmio = ahci_port_base(ap);
1760 unsigned long flags;
1761 u32 status;
1762
1763 spin_lock_irqsave(&ap->host->lock, flags);
1764 status = pp->intr_status;
1765 if (status)
1766 pp->intr_status = 0;
1767 spin_unlock_irqrestore(&ap->host->lock, flags);
1768
1769 spin_lock_bh(ap->lock);
1770 ahci_handle_port_interrupt(ap, port_mmio, status);
1771 spin_unlock_bh(ap->lock);
1772
1773 return IRQ_HANDLED;
1774}
1775EXPORT_SYMBOL_GPL(ahci_thread_fn);
1776
1777void ahci_hw_port_interrupt(struct ata_port *ap)
1778{
1779 void __iomem *port_mmio = ahci_port_base(ap);
1780 struct ahci_port_priv *pp = ap->private_data;
1781 u32 status;
1782
1783 status = readl(port_mmio + PORT_IRQ_STAT);
1784 writel(status, port_mmio + PORT_IRQ_STAT);
1785
1786 pp->intr_status |= status;
1787}
1788
1789irqreturn_t ahci_hw_interrupt(int irq, void *dev_instance)
1790{
1791 struct ata_port *ap_this = dev_instance;
1792 struct ahci_port_priv *pp = ap_this->private_data;
1793 struct ata_host *host = ap_this->host;
1794 struct ahci_host_priv *hpriv = host->private_data;
1795 void __iomem *mmio = hpriv->mmio;
1796 unsigned int i;
1797 u32 irq_stat, irq_masked;
1798
1799 VPRINTK("ENTER\n");
1800
1801 spin_lock(&host->lock);
1802
1803 irq_stat = readl(mmio + HOST_IRQ_STAT);
1804
1805 if (!irq_stat) {
1806 u32 status = pp->intr_status;
1807
1808 spin_unlock(&host->lock);
1809
1810 VPRINTK("EXIT\n");
1811
1812 return status ? IRQ_WAKE_THREAD : IRQ_NONE;
1813 }
1814
1815 irq_masked = irq_stat & hpriv->port_map;
1816
1817 for (i = 0; i < host->n_ports; i++) {
1818 struct ata_port *ap;
1819
1820 if (!(irq_masked & (1 << i)))
1821 continue;
1822
1823 ap = host->ports[i];
1824 if (ap) {
1825 ahci_hw_port_interrupt(ap);
1826 VPRINTK("port %u\n", i);
1827 } else {
1828 VPRINTK("port %u (no irq)\n", i);
1829 if (ata_ratelimit())
1830 dev_warn(host->dev,
1831 "interrupt on disabled port %u\n", i);
1832 }
1833 }
1834
1835 writel(irq_stat, mmio + HOST_IRQ_STAT);
1836
1837 spin_unlock(&host->lock);
1838
1839 VPRINTK("EXIT\n");
1840
1841 return IRQ_WAKE_THREAD;
1842}
1843EXPORT_SYMBOL_GPL(ahci_hw_interrupt);
1844
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001845irqreturn_t ahci_interrupt(int irq, void *dev_instance)
1846{
1847 struct ata_host *host = dev_instance;
1848 struct ahci_host_priv *hpriv;
1849 unsigned int i, handled = 0;
1850 void __iomem *mmio;
1851 u32 irq_stat, irq_masked;
1852
1853 VPRINTK("ENTER\n");
1854
1855 hpriv = host->private_data;
1856 mmio = hpriv->mmio;
1857
1858 /* sigh. 0xffffffff is a valid return from h/w */
1859 irq_stat = readl(mmio + HOST_IRQ_STAT);
1860 if (!irq_stat)
1861 return IRQ_NONE;
1862
1863 irq_masked = irq_stat & hpriv->port_map;
1864
1865 spin_lock(&host->lock);
1866
1867 for (i = 0; i < host->n_ports; i++) {
1868 struct ata_port *ap;
1869
1870 if (!(irq_masked & (1 << i)))
1871 continue;
1872
1873 ap = host->ports[i];
1874 if (ap) {
1875 ahci_port_intr(ap);
1876 VPRINTK("port %u\n", i);
1877 } else {
1878 VPRINTK("port %u (no irq)\n", i);
1879 if (ata_ratelimit())
Joe Perchesa44fec12011-04-15 15:51:58 -07001880 dev_warn(host->dev,
1881 "interrupt on disabled port %u\n", i);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001882 }
1883
1884 handled = 1;
1885 }
1886
1887 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1888 * it should be cleared after all the port events are cleared;
1889 * otherwise, it will raise a spurious interrupt after each
1890 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1891 * information.
1892 *
1893 * Also, use the unmasked value to clear interrupt as spurious
1894 * pending event on a dummy port might cause screaming IRQ.
1895 */
1896 writel(irq_stat, mmio + HOST_IRQ_STAT);
1897
1898 spin_unlock(&host->lock);
1899
1900 VPRINTK("EXIT\n");
1901
1902 return IRQ_RETVAL(handled);
1903}
1904EXPORT_SYMBOL_GPL(ahci_interrupt);
1905
1906static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1907{
1908 struct ata_port *ap = qc->ap;
1909 void __iomem *port_mmio = ahci_port_base(ap);
1910 struct ahci_port_priv *pp = ap->private_data;
1911
1912 /* Keep track of the currently active link. It will be used
1913 * in completion path to determine whether NCQ phase is in
1914 * progress.
1915 */
1916 pp->active_link = qc->dev->link;
1917
1918 if (qc->tf.protocol == ATA_PROT_NCQ)
1919 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1920
1921 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
1922 u32 fbs = readl(port_mmio + PORT_FBS);
1923 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1924 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
1925 writel(fbs, port_mmio + PORT_FBS);
1926 pp->fbs_last_dev = qc->dev->link->pmp;
1927 }
1928
1929 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
1930
1931 ahci_sw_activity(qc->dev->link);
1932
1933 return 0;
1934}
1935
1936static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
1937{
1938 struct ahci_port_priv *pp = qc->ap->private_data;
Tejun Heo6ad60192010-10-15 11:00:08 +02001939 u8 *rx_fis = pp->rx_fis;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001940
1941 if (pp->fbs_enabled)
Tejun Heo6ad60192010-10-15 11:00:08 +02001942 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001943
Tejun Heo6ad60192010-10-15 11:00:08 +02001944 /*
1945 * After a successful execution of an ATA PIO data-in command,
1946 * the device doesn't send D2H Reg FIS to update the TF and
1947 * the host should take TF and E_Status from the preceding PIO
1948 * Setup FIS.
1949 */
1950 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
1951 !(qc->flags & ATA_QCFLAG_FAILED)) {
1952 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
1953 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
1954 } else
1955 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
1956
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001957 return true;
1958}
1959
1960static void ahci_freeze(struct ata_port *ap)
1961{
1962 void __iomem *port_mmio = ahci_port_base(ap);
1963
1964 /* turn IRQ off */
1965 writel(0, port_mmio + PORT_IRQ_MASK);
1966}
1967
1968static void ahci_thaw(struct ata_port *ap)
1969{
1970 struct ahci_host_priv *hpriv = ap->host->private_data;
1971 void __iomem *mmio = hpriv->mmio;
1972 void __iomem *port_mmio = ahci_port_base(ap);
1973 u32 tmp;
1974 struct ahci_port_priv *pp = ap->private_data;
1975
1976 /* clear IRQ */
1977 tmp = readl(port_mmio + PORT_IRQ_STAT);
1978 writel(tmp, port_mmio + PORT_IRQ_STAT);
1979 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
1980
1981 /* turn IRQ back on */
1982 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1983}
1984
1985static void ahci_error_handler(struct ata_port *ap)
1986{
1987 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
1988 /* restart engine */
1989 ahci_stop_engine(ap);
1990 ahci_start_engine(ap);
1991 }
1992
1993 sata_pmp_error_handler(ap);
Tejun Heo0ee71952010-06-07 15:15:08 +02001994
1995 if (!ata_dev_enabled(ap->link.device))
1996 ahci_stop_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001997}
1998
1999static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2000{
2001 struct ata_port *ap = qc->ap;
2002
2003 /* make DMA engine forget about the failed command */
2004 if (qc->flags & ATA_QCFLAG_FAILED)
2005 ahci_kick_engine(ap);
2006}
2007
Shane Huang65fe1f02012-09-07 22:40:01 +08002008static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2009{
2010 void __iomem *port_mmio = ahci_port_base(ap);
2011 struct ata_device *dev = ap->link.device;
2012 u32 devslp, dm, dito, mdat, deto;
2013 int rc;
2014 unsigned int err_mask;
2015
2016 devslp = readl(port_mmio + PORT_DEVSLP);
2017 if (!(devslp & PORT_DEVSLP_DSP)) {
2018 dev_err(ap->host->dev, "port does not support device sleep\n");
2019 return;
2020 }
2021
2022 /* disable device sleep */
2023 if (!sleep) {
2024 if (devslp & PORT_DEVSLP_ADSE) {
2025 writel(devslp & ~PORT_DEVSLP_ADSE,
2026 port_mmio + PORT_DEVSLP);
2027 err_mask = ata_dev_set_feature(dev,
2028 SETFEATURES_SATA_DISABLE,
2029 SATA_DEVSLP);
2030 if (err_mask && err_mask != AC_ERR_DEV)
2031 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2032 }
2033 return;
2034 }
2035
2036 /* device sleep was already enabled */
2037 if (devslp & PORT_DEVSLP_ADSE)
2038 return;
2039
2040 /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
2041 rc = ahci_stop_engine(ap);
2042 if (rc)
2043 return;
2044
2045 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2046 dito = devslp_idle_timeout / (dm + 1);
2047 if (dito > 0x3ff)
2048 dito = 0x3ff;
2049
2050 /* Use the nominal value 10 ms if the read MDAT is zero,
2051 * the nominal value of DETO is 20 ms.
2052 */
Shane Huang803739d2012-12-17 23:18:59 +08002053 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002054 ATA_LOG_DEVSLP_VALID_MASK) {
Shane Huang803739d2012-12-17 23:18:59 +08002055 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
Shane Huang65fe1f02012-09-07 22:40:01 +08002056 ATA_LOG_DEVSLP_MDAT_MASK;
2057 if (!mdat)
2058 mdat = 10;
Shane Huang803739d2012-12-17 23:18:59 +08002059 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
Shane Huang65fe1f02012-09-07 22:40:01 +08002060 if (!deto)
2061 deto = 20;
2062 } else {
2063 mdat = 10;
2064 deto = 20;
2065 }
2066
2067 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2068 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2069 (deto << PORT_DEVSLP_DETO_OFFSET) |
2070 PORT_DEVSLP_ADSE);
2071 writel(devslp, port_mmio + PORT_DEVSLP);
2072
2073 ahci_start_engine(ap);
2074
2075 /* enable device sleep feature for the drive */
2076 err_mask = ata_dev_set_feature(dev,
2077 SETFEATURES_SATA_ENABLE,
2078 SATA_DEVSLP);
2079 if (err_mask && err_mask != AC_ERR_DEV)
2080 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2081}
2082
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002083static void ahci_enable_fbs(struct ata_port *ap)
2084{
2085 struct ahci_port_priv *pp = ap->private_data;
2086 void __iomem *port_mmio = ahci_port_base(ap);
2087 u32 fbs;
2088 int rc;
2089
2090 if (!pp->fbs_supported)
2091 return;
2092
2093 fbs = readl(port_mmio + PORT_FBS);
2094 if (fbs & PORT_FBS_EN) {
2095 pp->fbs_enabled = true;
2096 pp->fbs_last_dev = -1; /* initialization */
2097 return;
2098 }
2099
2100 rc = ahci_stop_engine(ap);
2101 if (rc)
2102 return;
2103
2104 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2105 fbs = readl(port_mmio + PORT_FBS);
2106 if (fbs & PORT_FBS_EN) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002107 dev_info(ap->host->dev, "FBS is enabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002108 pp->fbs_enabled = true;
2109 pp->fbs_last_dev = -1; /* initialization */
2110 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002111 dev_err(ap->host->dev, "Failed to enable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002112
2113 ahci_start_engine(ap);
2114}
2115
2116static void ahci_disable_fbs(struct ata_port *ap)
2117{
2118 struct ahci_port_priv *pp = ap->private_data;
2119 void __iomem *port_mmio = ahci_port_base(ap);
2120 u32 fbs;
2121 int rc;
2122
2123 if (!pp->fbs_supported)
2124 return;
2125
2126 fbs = readl(port_mmio + PORT_FBS);
2127 if ((fbs & PORT_FBS_EN) == 0) {
2128 pp->fbs_enabled = false;
2129 return;
2130 }
2131
2132 rc = ahci_stop_engine(ap);
2133 if (rc)
2134 return;
2135
2136 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2137 fbs = readl(port_mmio + PORT_FBS);
2138 if (fbs & PORT_FBS_EN)
Joe Perchesa44fec12011-04-15 15:51:58 -07002139 dev_err(ap->host->dev, "Failed to disable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002140 else {
Joe Perchesa44fec12011-04-15 15:51:58 -07002141 dev_info(ap->host->dev, "FBS is disabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002142 pp->fbs_enabled = false;
2143 }
2144
2145 ahci_start_engine(ap);
2146}
2147
2148static void ahci_pmp_attach(struct ata_port *ap)
2149{
2150 void __iomem *port_mmio = ahci_port_base(ap);
2151 struct ahci_port_priv *pp = ap->private_data;
2152 u32 cmd;
2153
2154 cmd = readl(port_mmio + PORT_CMD);
2155 cmd |= PORT_CMD_PMP;
2156 writel(cmd, port_mmio + PORT_CMD);
2157
2158 ahci_enable_fbs(ap);
2159
2160 pp->intr_mask |= PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002161
2162 /*
2163 * We must not change the port interrupt mask register if the
2164 * port is marked frozen, the value in pp->intr_mask will be
2165 * restored later when the port is thawed.
2166 *
2167 * Note that during initialization, the port is marked as
2168 * frozen since the irq handler is not yet registered.
2169 */
2170 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2171 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002172}
2173
2174static void ahci_pmp_detach(struct ata_port *ap)
2175{
2176 void __iomem *port_mmio = ahci_port_base(ap);
2177 struct ahci_port_priv *pp = ap->private_data;
2178 u32 cmd;
2179
2180 ahci_disable_fbs(ap);
2181
2182 cmd = readl(port_mmio + PORT_CMD);
2183 cmd &= ~PORT_CMD_PMP;
2184 writel(cmd, port_mmio + PORT_CMD);
2185
2186 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01002187
2188 /* see comment above in ahci_pmp_attach() */
2189 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2190 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002191}
2192
David Milburn02cdfcf2010-11-12 15:38:21 -06002193int ahci_port_resume(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002194{
2195 ahci_power_up(ap);
2196 ahci_start_port(ap);
2197
2198 if (sata_pmp_attached(ap))
2199 ahci_pmp_attach(ap);
2200 else
2201 ahci_pmp_detach(ap);
2202
2203 return 0;
2204}
David Milburn02cdfcf2010-11-12 15:38:21 -06002205EXPORT_SYMBOL_GPL(ahci_port_resume);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002206
2207#ifdef CONFIG_PM
2208static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2209{
2210 const char *emsg = NULL;
2211 int rc;
2212
2213 rc = ahci_deinit_port(ap, &emsg);
2214 if (rc == 0)
2215 ahci_power_down(ap);
2216 else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002217 ata_port_err(ap, "%s (%d)\n", emsg, rc);
Tejun Heo7faa33d2011-07-22 11:41:26 +02002218 ata_port_freeze(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002219 }
2220
2221 return rc;
2222}
2223#endif
2224
2225static int ahci_port_start(struct ata_port *ap)
2226{
2227 struct ahci_host_priv *hpriv = ap->host->private_data;
2228 struct device *dev = ap->host->dev;
2229 struct ahci_port_priv *pp;
2230 void *mem;
2231 dma_addr_t mem_dma;
2232 size_t dma_sz, rx_fis_sz;
2233
2234 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2235 if (!pp)
2236 return -ENOMEM;
2237
Alexander Gordeevb29900e2013-05-22 08:53:48 +09002238 if (ap->host->n_ports > 1) {
2239 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2240 if (!pp->irq_desc) {
2241 devm_kfree(dev, pp);
2242 return -ENOMEM;
2243 }
2244 snprintf(pp->irq_desc, 8,
2245 "%s%d", dev_driver_string(dev), ap->port_no);
2246 }
2247
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002248 /* check FBS capability */
2249 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2250 void __iomem *port_mmio = ahci_port_base(ap);
2251 u32 cmd = readl(port_mmio + PORT_CMD);
2252 if (cmd & PORT_CMD_FBSCP)
2253 pp->fbs_supported = true;
Tejun Heo5f173102010-07-24 16:53:48 +02002254 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002255 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2256 ap->port_no);
Tejun Heo5f173102010-07-24 16:53:48 +02002257 pp->fbs_supported = true;
2258 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07002259 dev_warn(dev, "port %d is not capable of FBS\n",
2260 ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002261 }
2262
2263 if (pp->fbs_supported) {
2264 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2265 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2266 } else {
2267 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2268 rx_fis_sz = AHCI_RX_FIS_SZ;
2269 }
2270
2271 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2272 if (!mem)
2273 return -ENOMEM;
2274 memset(mem, 0, dma_sz);
2275
2276 /*
2277 * First item in chunk of DMA memory: 32-slot command table,
2278 * 32 bytes each in size
2279 */
2280 pp->cmd_slot = mem;
2281 pp->cmd_slot_dma = mem_dma;
2282
2283 mem += AHCI_CMD_SLOT_SZ;
2284 mem_dma += AHCI_CMD_SLOT_SZ;
2285
2286 /*
2287 * Second item: Received-FIS area
2288 */
2289 pp->rx_fis = mem;
2290 pp->rx_fis_dma = mem_dma;
2291
2292 mem += rx_fis_sz;
2293 mem_dma += rx_fis_sz;
2294
2295 /*
2296 * Third item: data area for storing a single command
2297 * and its scatter-gather table
2298 */
2299 pp->cmd_tbl = mem;
2300 pp->cmd_tbl_dma = mem_dma;
2301
2302 /*
2303 * Save off initial list of interrupts to be enabled.
2304 * This could be changed later
2305 */
2306 pp->intr_mask = DEF_PORT_IRQ;
2307
Alexander Gordeev5ca72c42012-11-19 16:02:48 +01002308 /*
2309 * Switch to per-port locking in case each port has its own MSI vector.
2310 */
2311 if ((hpriv->flags & AHCI_HFLAG_MULTI_MSI)) {
2312 spin_lock_init(&pp->lock);
2313 ap->lock = &pp->lock;
2314 }
2315
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002316 ap->private_data = pp;
2317
2318 /* engage engines, captain */
2319 return ahci_port_resume(ap);
2320}
2321
2322static void ahci_port_stop(struct ata_port *ap)
2323{
2324 const char *emsg = NULL;
2325 int rc;
2326
2327 /* de-initialize port */
2328 rc = ahci_deinit_port(ap, &emsg);
2329 if (rc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002330 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002331}
2332
2333void ahci_print_info(struct ata_host *host, const char *scc_s)
2334{
2335 struct ahci_host_priv *hpriv = host->private_data;
2336 void __iomem *mmio = hpriv->mmio;
2337 u32 vers, cap, cap2, impl, speed;
2338 const char *speed_s;
2339
2340 vers = readl(mmio + HOST_VERSION);
2341 cap = hpriv->cap;
2342 cap2 = hpriv->cap2;
2343 impl = hpriv->port_map;
2344
2345 speed = (cap >> 20) & 0xf;
2346 if (speed == 1)
2347 speed_s = "1.5";
2348 else if (speed == 2)
2349 speed_s = "3";
2350 else if (speed == 3)
2351 speed_s = "6";
2352 else
2353 speed_s = "?";
2354
2355 dev_info(host->dev,
2356 "AHCI %02x%02x.%02x%02x "
2357 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2358 ,
2359
2360 (vers >> 24) & 0xff,
2361 (vers >> 16) & 0xff,
2362 (vers >> 8) & 0xff,
2363 vers & 0xff,
2364
2365 ((cap >> 8) & 0x1f) + 1,
2366 (cap & 0x1f) + 1,
2367 speed_s,
2368 impl,
2369 scc_s);
2370
2371 dev_info(host->dev,
2372 "flags: "
2373 "%s%s%s%s%s%s%s"
2374 "%s%s%s%s%s%s%s"
Shane Huang65fe1f02012-09-07 22:40:01 +08002375 "%s%s%s%s%s%s%s"
2376 "%s%s\n"
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002377 ,
2378
2379 cap & HOST_CAP_64 ? "64bit " : "",
2380 cap & HOST_CAP_NCQ ? "ncq " : "",
2381 cap & HOST_CAP_SNTF ? "sntf " : "",
2382 cap & HOST_CAP_MPS ? "ilck " : "",
2383 cap & HOST_CAP_SSS ? "stag " : "",
2384 cap & HOST_CAP_ALPM ? "pm " : "",
2385 cap & HOST_CAP_LED ? "led " : "",
2386 cap & HOST_CAP_CLO ? "clo " : "",
2387 cap & HOST_CAP_ONLY ? "only " : "",
2388 cap & HOST_CAP_PMP ? "pmp " : "",
2389 cap & HOST_CAP_FBS ? "fbs " : "",
2390 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2391 cap & HOST_CAP_SSC ? "slum " : "",
2392 cap & HOST_CAP_PART ? "part " : "",
2393 cap & HOST_CAP_CCC ? "ccc " : "",
2394 cap & HOST_CAP_EMS ? "ems " : "",
2395 cap & HOST_CAP_SXS ? "sxs " : "",
Shane Huang65fe1f02012-09-07 22:40:01 +08002396 cap2 & HOST_CAP2_DESO ? "deso " : "",
2397 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2398 cap2 & HOST_CAP2_SDS ? "sds " : "",
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002399 cap2 & HOST_CAP2_APST ? "apst " : "",
2400 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2401 cap2 & HOST_CAP2_BOH ? "boh " : ""
2402 );
2403}
2404EXPORT_SYMBOL_GPL(ahci_print_info);
2405
2406void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2407 struct ata_port_info *pi)
2408{
2409 u8 messages;
2410 void __iomem *mmio = hpriv->mmio;
2411 u32 em_loc = readl(mmio + HOST_EM_LOC);
2412 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2413
2414 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2415 return;
2416
2417 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2418
Harry Zhang008dbd62010-04-23 17:27:19 +08002419 if (messages) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002420 /* store em_loc */
2421 hpriv->em_loc = ((em_loc >> 16) * 4);
Harry Zhangc0623162010-04-23 17:28:38 +08002422 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
Harry Zhang008dbd62010-04-23 17:27:19 +08002423 hpriv->em_msg_type = messages;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002424 pi->flags |= ATA_FLAG_EM;
2425 if (!(em_ctl & EM_CTL_ALHD))
2426 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2427 }
2428}
2429EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2430
2431MODULE_AUTHOR("Jeff Garzik");
2432MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2433MODULE_LICENSE("GPL");