blob: c102d7d6428206795aeebe33a02980b089069cc7 [file] [log] [blame]
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001/*
2 * libahci.c - Common AHCI SATA low-level routines
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * 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"
48
49static int ahci_skip_host_reset;
50int ahci_ignore_sss;
51EXPORT_SYMBOL_GPL(ahci_ignore_sss);
52
53module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
54MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
55
56module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
57MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
58
Tejun Heo6b7ae952010-09-01 17:50:06 +020059static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
60 unsigned hints);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040061static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
62static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
63 size_t size);
64static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
65 ssize_t size);
66
67
68
69static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
70static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
71static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
72static 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);
79static void ahci_enable_fbs(struct ata_port *ap);
80static void ahci_disable_fbs(struct ata_port *ap);
81static void ahci_pmp_attach(struct ata_port *ap);
82static void ahci_pmp_detach(struct ata_port *ap);
83static int ahci_softreset(struct ata_link *link, unsigned int *class,
84 unsigned long deadline);
85static int ahci_hardreset(struct ata_link *link, unsigned int *class,
86 unsigned long deadline);
87static void ahci_postreset(struct ata_link *link, unsigned int *class);
88static void ahci_error_handler(struct ata_port *ap);
89static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040090static void ahci_dev_config(struct ata_device *dev);
Anton Vorontsov365cfa12010-03-28 00:22:14 -040091#ifdef CONFIG_PM
92static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
93#endif
94static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
95static ssize_t ahci_activity_store(struct ata_device *dev,
96 enum sw_activity val);
97static void ahci_init_sw_activity(struct ata_link *link);
98
99static ssize_t ahci_show_host_caps(struct device *dev,
100 struct device_attribute *attr, char *buf);
101static ssize_t ahci_show_host_cap2(struct device *dev,
102 struct device_attribute *attr, char *buf);
103static ssize_t ahci_show_host_version(struct device *dev,
104 struct device_attribute *attr, char *buf);
105static ssize_t ahci_show_port_cmd(struct device *dev,
106 struct device_attribute *attr, char *buf);
Harry Zhangc0623162010-04-23 17:28:38 +0800107static ssize_t ahci_read_em_buffer(struct device *dev,
108 struct device_attribute *attr, char *buf);
109static ssize_t ahci_store_em_buffer(struct device *dev,
110 struct device_attribute *attr,
111 const char *buf, size_t size);
Hannes Reinecke6e5fe5b2011-03-04 09:54:52 +0100112static ssize_t ahci_show_em_supported(struct device *dev,
113 struct device_attribute *attr, char *buf);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400114
115static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
116static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
117static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
118static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
Harry Zhangc0623162010-04-23 17:28:38 +0800119static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
120 ahci_read_em_buffer, ahci_store_em_buffer);
Hannes Reinecke6e5fe5b2011-03-04 09:54:52 +0100121static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400122
Tejun Heofad16e72010-09-21 09:25:48 +0200123struct device_attribute *ahci_shost_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400124 &dev_attr_link_power_management_policy,
125 &dev_attr_em_message_type,
126 &dev_attr_em_message,
127 &dev_attr_ahci_host_caps,
128 &dev_attr_ahci_host_cap2,
129 &dev_attr_ahci_host_version,
130 &dev_attr_ahci_port_cmd,
Harry Zhangc0623162010-04-23 17:28:38 +0800131 &dev_attr_em_buffer,
Hannes Reinecke6e5fe5b2011-03-04 09:54:52 +0100132 &dev_attr_em_message_supported,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400133 NULL
134};
Tejun Heofad16e72010-09-21 09:25:48 +0200135EXPORT_SYMBOL_GPL(ahci_shost_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400136
Tejun Heofad16e72010-09-21 09:25:48 +0200137struct device_attribute *ahci_sdev_attrs[] = {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400138 &dev_attr_sw_activity,
139 &dev_attr_unload_heads,
140 NULL
141};
Tejun Heofad16e72010-09-21 09:25:48 +0200142EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400143
144struct ata_port_operations ahci_ops = {
145 .inherits = &sata_pmp_port_ops,
146
147 .qc_defer = ahci_pmp_qc_defer,
148 .qc_prep = ahci_qc_prep,
149 .qc_issue = ahci_qc_issue,
150 .qc_fill_rtf = ahci_qc_fill_rtf,
151
152 .freeze = ahci_freeze,
153 .thaw = ahci_thaw,
154 .softreset = ahci_softreset,
155 .hardreset = ahci_hardreset,
156 .postreset = ahci_postreset,
157 .pmp_softreset = ahci_softreset,
158 .error_handler = ahci_error_handler,
159 .post_internal_cmd = ahci_post_internal_cmd,
160 .dev_config = ahci_dev_config,
161
162 .scr_read = ahci_scr_read,
163 .scr_write = ahci_scr_write,
164 .pmp_attach = ahci_pmp_attach,
165 .pmp_detach = ahci_pmp_detach,
166
Tejun Heo6b7ae952010-09-01 17:50:06 +0200167 .set_lpm = ahci_set_lpm,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400168 .em_show = ahci_led_show,
169 .em_store = ahci_led_store,
170 .sw_activity_show = ahci_activity_show,
171 .sw_activity_store = ahci_activity_store,
172#ifdef CONFIG_PM
173 .port_suspend = ahci_port_suspend,
174 .port_resume = ahci_port_resume,
175#endif
176 .port_start = ahci_port_start,
177 .port_stop = ahci_port_stop,
178};
179EXPORT_SYMBOL_GPL(ahci_ops);
180
181int ahci_em_messages = 1;
182EXPORT_SYMBOL_GPL(ahci_em_messages);
183module_param(ahci_em_messages, int, 0444);
184/* add other LED protocol types when they become supported */
185MODULE_PARM_DESC(ahci_em_messages,
Harry Zhang008dbd62010-04-23 17:27:19 +0800186 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400187
188static void ahci_enable_ahci(void __iomem *mmio)
189{
190 int i;
191 u32 tmp;
192
193 /* turn on AHCI_EN */
194 tmp = readl(mmio + HOST_CTL);
195 if (tmp & HOST_AHCI_EN)
196 return;
197
198 /* Some controllers need AHCI_EN to be written multiple times.
199 * Try a few times before giving up.
200 */
201 for (i = 0; i < 5; i++) {
202 tmp |= HOST_AHCI_EN;
203 writel(tmp, mmio + HOST_CTL);
204 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
205 if (tmp & HOST_AHCI_EN)
206 return;
207 msleep(10);
208 }
209
210 WARN_ON(1);
211}
212
213static ssize_t ahci_show_host_caps(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct Scsi_Host *shost = class_to_shost(dev);
217 struct ata_port *ap = ata_shost_to_port(shost);
218 struct ahci_host_priv *hpriv = ap->host->private_data;
219
220 return sprintf(buf, "%x\n", hpriv->cap);
221}
222
223static ssize_t ahci_show_host_cap2(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct Scsi_Host *shost = class_to_shost(dev);
227 struct ata_port *ap = ata_shost_to_port(shost);
228 struct ahci_host_priv *hpriv = ap->host->private_data;
229
230 return sprintf(buf, "%x\n", hpriv->cap2);
231}
232
233static ssize_t ahci_show_host_version(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
236 struct Scsi_Host *shost = class_to_shost(dev);
237 struct ata_port *ap = ata_shost_to_port(shost);
238 struct ahci_host_priv *hpriv = ap->host->private_data;
239 void __iomem *mmio = hpriv->mmio;
240
241 return sprintf(buf, "%x\n", readl(mmio + HOST_VERSION));
242}
243
244static ssize_t ahci_show_port_cmd(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
247 struct Scsi_Host *shost = class_to_shost(dev);
248 struct ata_port *ap = ata_shost_to_port(shost);
249 void __iomem *port_mmio = ahci_port_base(ap);
250
251 return sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
252}
253
Harry Zhangc0623162010-04-23 17:28:38 +0800254static ssize_t ahci_read_em_buffer(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
257 struct Scsi_Host *shost = class_to_shost(dev);
258 struct ata_port *ap = ata_shost_to_port(shost);
259 struct ahci_host_priv *hpriv = ap->host->private_data;
260 void __iomem *mmio = hpriv->mmio;
261 void __iomem *em_mmio = mmio + hpriv->em_loc;
262 u32 em_ctl, msg;
263 unsigned long flags;
264 size_t count;
265 int i;
266
267 spin_lock_irqsave(ap->lock, flags);
268
269 em_ctl = readl(mmio + HOST_EM_CTL);
270 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
271 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
272 spin_unlock_irqrestore(ap->lock, flags);
273 return -EINVAL;
274 }
275
276 if (!(em_ctl & EM_CTL_MR)) {
277 spin_unlock_irqrestore(ap->lock, flags);
278 return -EAGAIN;
279 }
280
281 if (!(em_ctl & EM_CTL_SMB))
282 em_mmio += hpriv->em_buf_sz;
283
284 count = hpriv->em_buf_sz;
285
286 /* the count should not be larger than PAGE_SIZE */
287 if (count > PAGE_SIZE) {
288 if (printk_ratelimit())
Joe Perchesa9a79df2011-04-15 15:51:59 -0700289 ata_port_warn(ap,
290 "EM read buffer size too large: "
291 "buffer size %u, page size %lu\n",
292 hpriv->em_buf_sz, PAGE_SIZE);
Harry Zhangc0623162010-04-23 17:28:38 +0800293 count = PAGE_SIZE;
294 }
295
296 for (i = 0; i < count; i += 4) {
297 msg = readl(em_mmio + i);
298 buf[i] = msg & 0xff;
299 buf[i + 1] = (msg >> 8) & 0xff;
300 buf[i + 2] = (msg >> 16) & 0xff;
301 buf[i + 3] = (msg >> 24) & 0xff;
302 }
303
304 spin_unlock_irqrestore(ap->lock, flags);
305
306 return i;
307}
308
309static ssize_t ahci_store_em_buffer(struct device *dev,
310 struct device_attribute *attr,
311 const char *buf, size_t size)
312{
313 struct Scsi_Host *shost = class_to_shost(dev);
314 struct ata_port *ap = ata_shost_to_port(shost);
315 struct ahci_host_priv *hpriv = ap->host->private_data;
316 void __iomem *mmio = hpriv->mmio;
317 void __iomem *em_mmio = mmio + hpriv->em_loc;
Harry Zhangf9ce8892010-06-24 11:34:23 +0800318 const unsigned char *msg_buf = buf;
Harry Zhangc0623162010-04-23 17:28:38 +0800319 u32 em_ctl, msg;
320 unsigned long flags;
321 int i;
322
323 /* check size validity */
324 if (!(ap->flags & ATA_FLAG_EM) ||
325 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
326 size % 4 || size > hpriv->em_buf_sz)
327 return -EINVAL;
328
329 spin_lock_irqsave(ap->lock, flags);
330
331 em_ctl = readl(mmio + HOST_EM_CTL);
332 if (em_ctl & EM_CTL_TM) {
333 spin_unlock_irqrestore(ap->lock, flags);
334 return -EBUSY;
335 }
336
337 for (i = 0; i < size; i += 4) {
Harry Zhangf9ce8892010-06-24 11:34:23 +0800338 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
339 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
Harry Zhangc0623162010-04-23 17:28:38 +0800340 writel(msg, em_mmio + i);
341 }
342
343 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
344
345 spin_unlock_irqrestore(ap->lock, flags);
346
347 return size;
348}
349
Hannes Reinecke6e5fe5b2011-03-04 09:54:52 +0100350static ssize_t ahci_show_em_supported(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct Scsi_Host *shost = class_to_shost(dev);
354 struct ata_port *ap = ata_shost_to_port(shost);
355 struct ahci_host_priv *hpriv = ap->host->private_data;
356 void __iomem *mmio = hpriv->mmio;
357 u32 em_ctl;
358
359 em_ctl = readl(mmio + HOST_EM_CTL);
360
361 return sprintf(buf, "%s%s%s%s\n",
362 em_ctl & EM_CTL_LED ? "led " : "",
363 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
364 em_ctl & EM_CTL_SES ? "ses-2 " : "",
365 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
366}
367
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400368/**
369 * ahci_save_initial_config - Save and fixup initial config values
370 * @dev: target AHCI device
371 * @hpriv: host private area to store config values
372 * @force_port_map: force port map to a specified value
373 * @mask_port_map: mask out particular bits from port map
374 *
375 * Some registers containing configuration info might be setup by
376 * BIOS and might be cleared on reset. This function saves the
377 * initial values of those registers into @hpriv such that they
378 * can be restored after controller reset.
379 *
380 * If inconsistent, config values are fixed up by this function.
381 *
382 * LOCKING:
383 * None.
384 */
385void ahci_save_initial_config(struct device *dev,
386 struct ahci_host_priv *hpriv,
387 unsigned int force_port_map,
388 unsigned int mask_port_map)
389{
390 void __iomem *mmio = hpriv->mmio;
391 u32 cap, cap2, vers, port_map;
392 int i;
393
394 /* make sure AHCI mode is enabled before accessing CAP */
395 ahci_enable_ahci(mmio);
396
397 /* Values prefixed with saved_ are written back to host after
398 * reset. Values without are used for driver operation.
399 */
400 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
401 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
402
403 /* CAP2 register is only defined for AHCI 1.2 and later */
404 vers = readl(mmio + HOST_VERSION);
405 if ((vers >> 16) > 1 ||
406 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
407 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
408 else
409 hpriv->saved_cap2 = cap2 = 0;
410
411 /* some chips have errata preventing 64bit use */
412 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700413 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400414 cap &= ~HOST_CAP_64;
415 }
416
417 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700418 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400419 cap &= ~HOST_CAP_NCQ;
420 }
421
422 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700423 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400424 cap |= HOST_CAP_NCQ;
425 }
426
427 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700428 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400429 cap &= ~HOST_CAP_PMP;
430 }
431
432 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700433 dev_info(dev,
434 "controller can't do SNTF, turning off CAP_SNTF\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400435 cap &= ~HOST_CAP_SNTF;
436 }
437
Tejun Heo5f173102010-07-24 16:53:48 +0200438 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700439 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
Tejun Heo5f173102010-07-24 16:53:48 +0200440 cap |= HOST_CAP_FBS;
441 }
442
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400443 if (force_port_map && port_map != force_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700444 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
445 port_map, force_port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400446 port_map = force_port_map;
447 }
448
449 if (mask_port_map) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700450 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
451 port_map,
452 port_map & mask_port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400453 port_map &= mask_port_map;
454 }
455
456 /* cross check port_map and cap.n_ports */
457 if (port_map) {
458 int map_ports = 0;
459
460 for (i = 0; i < AHCI_MAX_PORTS; i++)
461 if (port_map & (1 << i))
462 map_ports++;
463
464 /* If PI has more ports than n_ports, whine, clear
465 * port_map and let it be generated from n_ports.
466 */
467 if (map_ports > ahci_nr_ports(cap)) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700468 dev_warn(dev,
469 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
470 port_map, ahci_nr_ports(cap));
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400471 port_map = 0;
472 }
473 }
474
475 /* fabricate port_map from cap.nr_ports */
476 if (!port_map) {
477 port_map = (1 << ahci_nr_ports(cap)) - 1;
Joe Perchesa44fec12011-04-15 15:51:58 -0700478 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400479
480 /* write the fixed up value to the PI register */
481 hpriv->saved_port_map = port_map;
482 }
483
484 /* record values to use during operation */
485 hpriv->cap = cap;
486 hpriv->cap2 = cap2;
487 hpriv->port_map = port_map;
488}
489EXPORT_SYMBOL_GPL(ahci_save_initial_config);
490
491/**
492 * ahci_restore_initial_config - Restore initial config
493 * @host: target ATA host
494 *
495 * Restore initial config stored by ahci_save_initial_config().
496 *
497 * LOCKING:
498 * None.
499 */
500static void ahci_restore_initial_config(struct ata_host *host)
501{
502 struct ahci_host_priv *hpriv = host->private_data;
503 void __iomem *mmio = hpriv->mmio;
504
505 writel(hpriv->saved_cap, mmio + HOST_CAP);
506 if (hpriv->saved_cap2)
507 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
508 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
509 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
510}
511
512static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
513{
514 static const int offset[] = {
515 [SCR_STATUS] = PORT_SCR_STAT,
516 [SCR_CONTROL] = PORT_SCR_CTL,
517 [SCR_ERROR] = PORT_SCR_ERR,
518 [SCR_ACTIVE] = PORT_SCR_ACT,
519 [SCR_NOTIFICATION] = PORT_SCR_NTF,
520 };
521 struct ahci_host_priv *hpriv = ap->host->private_data;
522
523 if (sc_reg < ARRAY_SIZE(offset) &&
524 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
525 return offset[sc_reg];
526 return 0;
527}
528
529static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
530{
531 void __iomem *port_mmio = ahci_port_base(link->ap);
532 int offset = ahci_scr_offset(link->ap, sc_reg);
533
534 if (offset) {
535 *val = readl(port_mmio + offset);
536 return 0;
537 }
538 return -EINVAL;
539}
540
541static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
542{
543 void __iomem *port_mmio = ahci_port_base(link->ap);
544 int offset = ahci_scr_offset(link->ap, sc_reg);
545
546 if (offset) {
547 writel(val, port_mmio + offset);
548 return 0;
549 }
550 return -EINVAL;
551}
552
553void ahci_start_engine(struct ata_port *ap)
554{
555 void __iomem *port_mmio = ahci_port_base(ap);
556 u32 tmp;
557
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400558 /* start DMA */
559 tmp = readl(port_mmio + PORT_CMD);
560 tmp |= PORT_CMD_START;
561 writel(tmp, port_mmio + PORT_CMD);
562 readl(port_mmio + PORT_CMD); /* flush */
563}
564EXPORT_SYMBOL_GPL(ahci_start_engine);
565
566int ahci_stop_engine(struct ata_port *ap)
567{
568 void __iomem *port_mmio = ahci_port_base(ap);
569 u32 tmp;
570
571 tmp = readl(port_mmio + PORT_CMD);
572
573 /* check if the HBA is idle */
574 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
575 return 0;
576
577 /* setting HBA to idle */
578 tmp &= ~PORT_CMD_START;
579 writel(tmp, port_mmio + PORT_CMD);
580
581 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo97750ce2010-09-06 17:56:29 +0200582 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400583 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
584 if (tmp & PORT_CMD_LIST_ON)
585 return -EIO;
586
587 return 0;
588}
589EXPORT_SYMBOL_GPL(ahci_stop_engine);
590
591static void ahci_start_fis_rx(struct ata_port *ap)
592{
593 void __iomem *port_mmio = ahci_port_base(ap);
594 struct ahci_host_priv *hpriv = ap->host->private_data;
595 struct ahci_port_priv *pp = ap->private_data;
596 u32 tmp;
597
598 /* set FIS registers */
599 if (hpriv->cap & HOST_CAP_64)
600 writel((pp->cmd_slot_dma >> 16) >> 16,
601 port_mmio + PORT_LST_ADDR_HI);
602 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
603
604 if (hpriv->cap & HOST_CAP_64)
605 writel((pp->rx_fis_dma >> 16) >> 16,
606 port_mmio + PORT_FIS_ADDR_HI);
607 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
608
609 /* enable FIS reception */
610 tmp = readl(port_mmio + PORT_CMD);
611 tmp |= PORT_CMD_FIS_RX;
612 writel(tmp, port_mmio + PORT_CMD);
613
614 /* flush */
615 readl(port_mmio + PORT_CMD);
616}
617
618static int ahci_stop_fis_rx(struct ata_port *ap)
619{
620 void __iomem *port_mmio = ahci_port_base(ap);
621 u32 tmp;
622
623 /* disable FIS reception */
624 tmp = readl(port_mmio + PORT_CMD);
625 tmp &= ~PORT_CMD_FIS_RX;
626 writel(tmp, port_mmio + PORT_CMD);
627
628 /* wait for completion, spec says 500ms, give it 1000 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200629 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400630 PORT_CMD_FIS_ON, 10, 1000);
631 if (tmp & PORT_CMD_FIS_ON)
632 return -EBUSY;
633
634 return 0;
635}
636
637static void ahci_power_up(struct ata_port *ap)
638{
639 struct ahci_host_priv *hpriv = ap->host->private_data;
640 void __iomem *port_mmio = ahci_port_base(ap);
641 u32 cmd;
642
643 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
644
645 /* spin up device */
646 if (hpriv->cap & HOST_CAP_SSS) {
647 cmd |= PORT_CMD_SPIN_UP;
648 writel(cmd, port_mmio + PORT_CMD);
649 }
650
651 /* wake up link */
652 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
653}
654
Tejun Heo6b7ae952010-09-01 17:50:06 +0200655static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
656 unsigned int hints)
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400657{
Tejun Heo6b7ae952010-09-01 17:50:06 +0200658 struct ata_port *ap = link->ap;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400659 struct ahci_host_priv *hpriv = ap->host->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400660 struct ahci_port_priv *pp = ap->private_data;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400661 void __iomem *port_mmio = ahci_port_base(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400662
Tejun Heo6b7ae952010-09-01 17:50:06 +0200663 if (policy != ATA_LPM_MAX_POWER) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400664 /*
Tejun Heo6b7ae952010-09-01 17:50:06 +0200665 * Disable interrupts on Phy Ready. This keeps us from
666 * getting woken up due to spurious phy ready
667 * interrupts.
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400668 */
Tejun Heo6b7ae952010-09-01 17:50:06 +0200669 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
670 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
671
672 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400673 }
674
Tejun Heo6b7ae952010-09-01 17:50:06 +0200675 if (hpriv->cap & HOST_CAP_ALPM) {
676 u32 cmd = readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400677
Tejun Heo6b7ae952010-09-01 17:50:06 +0200678 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
679 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
680 cmd |= PORT_CMD_ICC_ACTIVE;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400681
Tejun Heo6b7ae952010-09-01 17:50:06 +0200682 writel(cmd, port_mmio + PORT_CMD);
683 readl(port_mmio + PORT_CMD);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400684
Tejun Heo6b7ae952010-09-01 17:50:06 +0200685 /* wait 10ms to be sure we've come out of LPM state */
Tejun Heo97750ce2010-09-06 17:56:29 +0200686 ata_msleep(ap, 10);
Tejun Heo6b7ae952010-09-01 17:50:06 +0200687 } else {
688 cmd |= PORT_CMD_ALPE;
689 if (policy == ATA_LPM_MIN_POWER)
690 cmd |= PORT_CMD_ASP;
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400691
Tejun Heo6b7ae952010-09-01 17:50:06 +0200692 /* write out new cmd value */
693 writel(cmd, port_mmio + PORT_CMD);
694 }
695 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400696
Tejun Heo6b7ae952010-09-01 17:50:06 +0200697 if (policy == ATA_LPM_MAX_POWER) {
698 sata_link_scr_lpm(link, policy, false);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400699
Tejun Heo6b7ae952010-09-01 17:50:06 +0200700 /* turn PHYRDY IRQ back on */
701 pp->intr_mask |= PORT_IRQ_PHYRDY;
702 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
703 }
704
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400705 return 0;
706}
707
708#ifdef CONFIG_PM
709static void ahci_power_down(struct ata_port *ap)
710{
711 struct ahci_host_priv *hpriv = ap->host->private_data;
712 void __iomem *port_mmio = ahci_port_base(ap);
713 u32 cmd, scontrol;
714
715 if (!(hpriv->cap & HOST_CAP_SSS))
716 return;
717
718 /* put device into listen mode, first set PxSCTL.DET to 0 */
719 scontrol = readl(port_mmio + PORT_SCR_CTL);
720 scontrol &= ~0xf;
721 writel(scontrol, port_mmio + PORT_SCR_CTL);
722
723 /* then set PxCMD.SUD to 0 */
724 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
725 cmd &= ~PORT_CMD_SPIN_UP;
726 writel(cmd, port_mmio + PORT_CMD);
727}
728#endif
729
730static void ahci_start_port(struct ata_port *ap)
731{
732 struct ahci_port_priv *pp = ap->private_data;
733 struct ata_link *link;
734 struct ahci_em_priv *emp;
735 ssize_t rc;
736 int i;
737
738 /* enable FIS reception */
739 ahci_start_fis_rx(ap);
740
741 /* enable DMA */
742 ahci_start_engine(ap);
743
744 /* turn on LEDs */
745 if (ap->flags & ATA_FLAG_EM) {
746 ata_for_each_link(link, ap, EDGE) {
747 emp = &pp->em_priv[link->pmp];
748
749 /* EM Transmit bit maybe busy during init */
750 for (i = 0; i < EM_MAX_RETRY; i++) {
751 rc = ahci_transmit_led_message(ap,
752 emp->led_state,
753 4);
754 if (rc == -EBUSY)
Tejun Heo97750ce2010-09-06 17:56:29 +0200755 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400756 else
757 break;
758 }
759 }
760 }
761
762 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
763 ata_for_each_link(link, ap, EDGE)
764 ahci_init_sw_activity(link);
765
766}
767
768static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
769{
770 int rc;
771
772 /* disable DMA */
773 rc = ahci_stop_engine(ap);
774 if (rc) {
775 *emsg = "failed to stop engine";
776 return rc;
777 }
778
779 /* disable FIS reception */
780 rc = ahci_stop_fis_rx(ap);
781 if (rc) {
782 *emsg = "failed stop FIS RX";
783 return rc;
784 }
785
786 return 0;
787}
788
789int ahci_reset_controller(struct ata_host *host)
790{
791 struct ahci_host_priv *hpriv = host->private_data;
792 void __iomem *mmio = hpriv->mmio;
793 u32 tmp;
794
795 /* we must be in AHCI mode, before using anything
796 * AHCI-specific, such as HOST_RESET.
797 */
798 ahci_enable_ahci(mmio);
799
800 /* global controller reset */
801 if (!ahci_skip_host_reset) {
802 tmp = readl(mmio + HOST_CTL);
803 if ((tmp & HOST_RESET) == 0) {
804 writel(tmp | HOST_RESET, mmio + HOST_CTL);
805 readl(mmio + HOST_CTL); /* flush */
806 }
807
808 /*
809 * to perform host reset, OS should set HOST_RESET
810 * and poll until this bit is read to be "0".
811 * reset must complete within 1 second, or
812 * the hardware should be considered fried.
813 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200814 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400815 HOST_RESET, 10, 1000);
816
817 if (tmp & HOST_RESET) {
Joe Perchesa44fec12011-04-15 15:51:58 -0700818 dev_err(host->dev, "controller reset failed (0x%x)\n",
819 tmp);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400820 return -EIO;
821 }
822
823 /* turn on AHCI mode */
824 ahci_enable_ahci(mmio);
825
826 /* Some registers might be cleared on reset. Restore
827 * initial values.
828 */
829 ahci_restore_initial_config(host);
830 } else
Joe Perchesa44fec12011-04-15 15:51:58 -0700831 dev_info(host->dev, "skipping global host reset\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400832
833 return 0;
834}
835EXPORT_SYMBOL_GPL(ahci_reset_controller);
836
837static void ahci_sw_activity(struct ata_link *link)
838{
839 struct ata_port *ap = link->ap;
840 struct ahci_port_priv *pp = ap->private_data;
841 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
842
843 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
844 return;
845
846 emp->activity++;
847 if (!timer_pending(&emp->timer))
848 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
849}
850
851static void ahci_sw_activity_blink(unsigned long arg)
852{
853 struct ata_link *link = (struct ata_link *)arg;
854 struct ata_port *ap = link->ap;
855 struct ahci_port_priv *pp = ap->private_data;
856 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
857 unsigned long led_message = emp->led_state;
858 u32 activity_led_state;
859 unsigned long flags;
860
861 led_message &= EM_MSG_LED_VALUE;
862 led_message |= ap->port_no | (link->pmp << 8);
863
864 /* check to see if we've had activity. If so,
865 * toggle state of LED and reset timer. If not,
866 * turn LED to desired idle state.
867 */
868 spin_lock_irqsave(ap->lock, flags);
869 if (emp->saved_activity != emp->activity) {
870 emp->saved_activity = emp->activity;
871 /* get the current LED state */
872 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
873
874 if (activity_led_state)
875 activity_led_state = 0;
876 else
877 activity_led_state = 1;
878
879 /* clear old state */
880 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
881
882 /* toggle state */
883 led_message |= (activity_led_state << 16);
884 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
885 } else {
886 /* switch to idle */
887 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
888 if (emp->blink_policy == BLINK_OFF)
889 led_message |= (1 << 16);
890 }
891 spin_unlock_irqrestore(ap->lock, flags);
892 ahci_transmit_led_message(ap, led_message, 4);
893}
894
895static void ahci_init_sw_activity(struct ata_link *link)
896{
897 struct ata_port *ap = link->ap;
898 struct ahci_port_priv *pp = ap->private_data;
899 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
900
901 /* init activity stats, setup timer */
902 emp->saved_activity = emp->activity = 0;
903 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
904
905 /* check our blink policy and set flag for link if it's enabled */
906 if (emp->blink_policy)
907 link->flags |= ATA_LFLAG_SW_ACTIVITY;
908}
909
910int ahci_reset_em(struct ata_host *host)
911{
912 struct ahci_host_priv *hpriv = host->private_data;
913 void __iomem *mmio = hpriv->mmio;
914 u32 em_ctl;
915
916 em_ctl = readl(mmio + HOST_EM_CTL);
917 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
918 return -EINVAL;
919
920 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
921 return 0;
922}
923EXPORT_SYMBOL_GPL(ahci_reset_em);
924
925static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
926 ssize_t size)
927{
928 struct ahci_host_priv *hpriv = ap->host->private_data;
929 struct ahci_port_priv *pp = ap->private_data;
930 void __iomem *mmio = hpriv->mmio;
931 u32 em_ctl;
932 u32 message[] = {0, 0};
933 unsigned long flags;
934 int pmp;
935 struct ahci_em_priv *emp;
936
937 /* get the slot number from the message */
938 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
939 if (pmp < EM_MAX_SLOTS)
940 emp = &pp->em_priv[pmp];
941 else
942 return -EINVAL;
943
944 spin_lock_irqsave(ap->lock, flags);
945
946 /*
947 * if we are still busy transmitting a previous message,
948 * do not allow
949 */
950 em_ctl = readl(mmio + HOST_EM_CTL);
951 if (em_ctl & EM_CTL_TM) {
952 spin_unlock_irqrestore(ap->lock, flags);
953 return -EBUSY;
954 }
955
Harry Zhang008dbd62010-04-23 17:27:19 +0800956 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
957 /*
958 * create message header - this is all zero except for
959 * the message size, which is 4 bytes.
960 */
961 message[0] |= (4 << 8);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400962
Harry Zhang008dbd62010-04-23 17:27:19 +0800963 /* ignore 0:4 of byte zero, fill in port info yourself */
964 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400965
Harry Zhang008dbd62010-04-23 17:27:19 +0800966 /* write message to EM_LOC */
967 writel(message[0], mmio + hpriv->em_loc);
968 writel(message[1], mmio + hpriv->em_loc+4);
969
970 /*
971 * tell hardware to transmit the message
972 */
973 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
974 }
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400975
976 /* save off new led state for port/slot */
977 emp->led_state = state;
978
Anton Vorontsov365cfa12010-03-28 00:22:14 -0400979 spin_unlock_irqrestore(ap->lock, flags);
980 return size;
981}
982
983static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
984{
985 struct ahci_port_priv *pp = ap->private_data;
986 struct ata_link *link;
987 struct ahci_em_priv *emp;
988 int rc = 0;
989
990 ata_for_each_link(link, ap, EDGE) {
991 emp = &pp->em_priv[link->pmp];
992 rc += sprintf(buf, "%lx\n", emp->led_state);
993 }
994 return rc;
995}
996
997static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
998 size_t size)
999{
1000 int state;
1001 int pmp;
1002 struct ahci_port_priv *pp = ap->private_data;
1003 struct ahci_em_priv *emp;
1004
1005 state = simple_strtoul(buf, NULL, 0);
1006
1007 /* get the slot number from the message */
1008 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1009 if (pmp < EM_MAX_SLOTS)
1010 emp = &pp->em_priv[pmp];
1011 else
1012 return -EINVAL;
1013
1014 /* mask off the activity bits if we are in sw_activity
1015 * mode, user should turn off sw_activity before setting
1016 * activity led through em_message
1017 */
1018 if (emp->blink_policy)
1019 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1020
1021 return ahci_transmit_led_message(ap, state, size);
1022}
1023
1024static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1025{
1026 struct ata_link *link = dev->link;
1027 struct ata_port *ap = link->ap;
1028 struct ahci_port_priv *pp = ap->private_data;
1029 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1030 u32 port_led_state = emp->led_state;
1031
1032 /* save the desired Activity LED behavior */
1033 if (val == OFF) {
1034 /* clear LFLAG */
1035 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1036
1037 /* set the LED to OFF */
1038 port_led_state &= EM_MSG_LED_VALUE_OFF;
1039 port_led_state |= (ap->port_no | (link->pmp << 8));
1040 ahci_transmit_led_message(ap, port_led_state, 4);
1041 } else {
1042 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1043 if (val == BLINK_OFF) {
1044 /* set LED to ON for idle */
1045 port_led_state &= EM_MSG_LED_VALUE_OFF;
1046 port_led_state |= (ap->port_no | (link->pmp << 8));
1047 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
1048 ahci_transmit_led_message(ap, port_led_state, 4);
1049 }
1050 }
1051 emp->blink_policy = val;
1052 return 0;
1053}
1054
1055static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1056{
1057 struct ata_link *link = dev->link;
1058 struct ata_port *ap = link->ap;
1059 struct ahci_port_priv *pp = ap->private_data;
1060 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1061
1062 /* display the saved value of activity behavior for this
1063 * disk.
1064 */
1065 return sprintf(buf, "%d\n", emp->blink_policy);
1066}
1067
1068static void ahci_port_init(struct device *dev, struct ata_port *ap,
1069 int port_no, void __iomem *mmio,
1070 void __iomem *port_mmio)
1071{
1072 const char *emsg = NULL;
1073 int rc;
1074 u32 tmp;
1075
1076 /* make sure port is not active */
1077 rc = ahci_deinit_port(ap, &emsg);
1078 if (rc)
1079 dev_warn(dev, "%s (%d)\n", emsg, rc);
1080
1081 /* clear SError */
1082 tmp = readl(port_mmio + PORT_SCR_ERR);
1083 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1084 writel(tmp, port_mmio + PORT_SCR_ERR);
1085
1086 /* clear port IRQ */
1087 tmp = readl(port_mmio + PORT_IRQ_STAT);
1088 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1089 if (tmp)
1090 writel(tmp, port_mmio + PORT_IRQ_STAT);
1091
1092 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1093}
1094
1095void ahci_init_controller(struct ata_host *host)
1096{
1097 struct ahci_host_priv *hpriv = host->private_data;
1098 void __iomem *mmio = hpriv->mmio;
1099 int i;
1100 void __iomem *port_mmio;
1101 u32 tmp;
1102
1103 for (i = 0; i < host->n_ports; i++) {
1104 struct ata_port *ap = host->ports[i];
1105
1106 port_mmio = ahci_port_base(ap);
1107 if (ata_port_is_dummy(ap))
1108 continue;
1109
1110 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1111 }
1112
1113 tmp = readl(mmio + HOST_CTL);
1114 VPRINTK("HOST_CTL 0x%x\n", tmp);
1115 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1116 tmp = readl(mmio + HOST_CTL);
1117 VPRINTK("HOST_CTL 0x%x\n", tmp);
1118}
1119EXPORT_SYMBOL_GPL(ahci_init_controller);
1120
1121static void ahci_dev_config(struct ata_device *dev)
1122{
1123 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1124
1125 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1126 dev->max_sectors = 255;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001127 ata_dev_info(dev,
1128 "SB600 AHCI: limiting to 255 sectors per cmd\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001129 }
1130}
1131
1132static unsigned int ahci_dev_classify(struct ata_port *ap)
1133{
1134 void __iomem *port_mmio = ahci_port_base(ap);
1135 struct ata_taskfile tf;
1136 u32 tmp;
1137
1138 tmp = readl(port_mmio + PORT_SIG);
1139 tf.lbah = (tmp >> 24) & 0xff;
1140 tf.lbam = (tmp >> 16) & 0xff;
1141 tf.lbal = (tmp >> 8) & 0xff;
1142 tf.nsect = (tmp) & 0xff;
1143
1144 return ata_dev_classify(&tf);
1145}
1146
David Milburn02cdfcf2010-11-12 15:38:21 -06001147void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1148 u32 opts)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001149{
1150 dma_addr_t cmd_tbl_dma;
1151
1152 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1153
1154 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1155 pp->cmd_slot[tag].status = 0;
1156 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1157 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1158}
David Milburn02cdfcf2010-11-12 15:38:21 -06001159EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001160
1161int ahci_kick_engine(struct ata_port *ap)
1162{
1163 void __iomem *port_mmio = ahci_port_base(ap);
1164 struct ahci_host_priv *hpriv = ap->host->private_data;
1165 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1166 u32 tmp;
1167 int busy, rc;
1168
1169 /* stop engine */
1170 rc = ahci_stop_engine(ap);
1171 if (rc)
1172 goto out_restart;
1173
1174 /* need to do CLO?
1175 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1176 */
1177 busy = status & (ATA_BUSY | ATA_DRQ);
1178 if (!busy && !sata_pmp_attached(ap)) {
1179 rc = 0;
1180 goto out_restart;
1181 }
1182
1183 if (!(hpriv->cap & HOST_CAP_CLO)) {
1184 rc = -EOPNOTSUPP;
1185 goto out_restart;
1186 }
1187
1188 /* perform CLO */
1189 tmp = readl(port_mmio + PORT_CMD);
1190 tmp |= PORT_CMD_CLO;
1191 writel(tmp, port_mmio + PORT_CMD);
1192
1193 rc = 0;
Tejun Heo97750ce2010-09-06 17:56:29 +02001194 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001195 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1196 if (tmp & PORT_CMD_CLO)
1197 rc = -EIO;
1198
1199 /* restart engine */
1200 out_restart:
1201 ahci_start_engine(ap);
1202 return rc;
1203}
1204EXPORT_SYMBOL_GPL(ahci_kick_engine);
1205
1206static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1207 struct ata_taskfile *tf, int is_cmd, u16 flags,
1208 unsigned long timeout_msec)
1209{
1210 const u32 cmd_fis_len = 5; /* five dwords */
1211 struct ahci_port_priv *pp = ap->private_data;
1212 void __iomem *port_mmio = ahci_port_base(ap);
1213 u8 *fis = pp->cmd_tbl;
1214 u32 tmp;
1215
1216 /* prep the command */
1217 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1218 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1219
1220 /* issue & wait */
1221 writel(1, port_mmio + PORT_CMD_ISSUE);
1222
1223 if (timeout_msec) {
Tejun Heo97750ce2010-09-06 17:56:29 +02001224 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1225 0x1, 0x1, 1, timeout_msec);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001226 if (tmp & 0x1) {
1227 ahci_kick_engine(ap);
1228 return -EBUSY;
1229 }
1230 } else
1231 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1232
1233 return 0;
1234}
1235
1236int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1237 int pmp, unsigned long deadline,
1238 int (*check_ready)(struct ata_link *link))
1239{
1240 struct ata_port *ap = link->ap;
1241 struct ahci_host_priv *hpriv = ap->host->private_data;
1242 const char *reason = NULL;
1243 unsigned long now, msecs;
1244 struct ata_taskfile tf;
1245 int rc;
1246
1247 DPRINTK("ENTER\n");
1248
1249 /* prepare for SRST (AHCI-1.1 10.4.1) */
1250 rc = ahci_kick_engine(ap);
1251 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001252 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001253
1254 ata_tf_init(link->device, &tf);
1255
1256 /* issue the first D2H Register FIS */
1257 msecs = 0;
1258 now = jiffies;
Tejun Heof1f5a802010-08-27 11:09:15 +02001259 if (time_after(deadline, now))
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001260 msecs = jiffies_to_msecs(deadline - now);
1261
1262 tf.ctl |= ATA_SRST;
1263 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1264 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1265 rc = -EIO;
1266 reason = "1st FIS failed";
1267 goto fail;
1268 }
1269
1270 /* spec says at least 5us, but be generous and sleep for 1ms */
Tejun Heo97750ce2010-09-06 17:56:29 +02001271 ata_msleep(ap, 1);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001272
1273 /* issue the second D2H Register FIS */
1274 tf.ctl &= ~ATA_SRST;
1275 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1276
1277 /* wait for link to become ready */
1278 rc = ata_wait_after_reset(link, deadline, check_ready);
1279 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1280 /*
1281 * Workaround for cases where link online status can't
1282 * be trusted. Treat device readiness timeout as link
1283 * offline.
1284 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001285 ata_link_info(link, "device not ready, treating as offline\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001286 *class = ATA_DEV_NONE;
1287 } else if (rc) {
1288 /* link occupied, -ENODEV too is an error */
1289 reason = "device not ready";
1290 goto fail;
1291 } else
1292 *class = ahci_dev_classify(ap);
1293
1294 DPRINTK("EXIT, class=%u\n", *class);
1295 return 0;
1296
1297 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07001298 ata_link_err(link, "softreset failed (%s)\n", reason);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001299 return rc;
1300}
1301
1302int ahci_check_ready(struct ata_link *link)
1303{
1304 void __iomem *port_mmio = ahci_port_base(link->ap);
1305 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1306
1307 return ata_check_ready(status);
1308}
1309EXPORT_SYMBOL_GPL(ahci_check_ready);
1310
1311static int ahci_softreset(struct ata_link *link, unsigned int *class,
1312 unsigned long deadline)
1313{
1314 int pmp = sata_srst_pmp(link);
1315
1316 DPRINTK("ENTER\n");
1317
1318 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1319}
1320EXPORT_SYMBOL_GPL(ahci_do_softreset);
1321
1322static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1323 unsigned long deadline)
1324{
1325 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1326 struct ata_port *ap = link->ap;
1327 struct ahci_port_priv *pp = ap->private_data;
1328 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1329 struct ata_taskfile tf;
1330 bool online;
1331 int rc;
1332
1333 DPRINTK("ENTER\n");
1334
1335 ahci_stop_engine(ap);
1336
1337 /* clear D2H reception area to properly wait for D2H FIS */
1338 ata_tf_init(link->device, &tf);
1339 tf.command = 0x80;
1340 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1341
1342 rc = sata_link_hardreset(link, timing, deadline, &online,
1343 ahci_check_ready);
1344
1345 ahci_start_engine(ap);
1346
1347 if (online)
1348 *class = ahci_dev_classify(ap);
1349
1350 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1351 return rc;
1352}
1353
1354static void ahci_postreset(struct ata_link *link, unsigned int *class)
1355{
1356 struct ata_port *ap = link->ap;
1357 void __iomem *port_mmio = ahci_port_base(ap);
1358 u32 new_tmp, tmp;
1359
1360 ata_std_postreset(link, class);
1361
1362 /* Make sure port's ATAPI bit is set appropriately */
1363 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1364 if (*class == ATA_DEV_ATAPI)
1365 new_tmp |= PORT_CMD_ATAPI;
1366 else
1367 new_tmp &= ~PORT_CMD_ATAPI;
1368 if (new_tmp != tmp) {
1369 writel(new_tmp, port_mmio + PORT_CMD);
1370 readl(port_mmio + PORT_CMD); /* flush */
1371 }
1372}
1373
1374static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1375{
1376 struct scatterlist *sg;
1377 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1378 unsigned int si;
1379
1380 VPRINTK("ENTER\n");
1381
1382 /*
1383 * Next, the S/G list.
1384 */
1385 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1386 dma_addr_t addr = sg_dma_address(sg);
1387 u32 sg_len = sg_dma_len(sg);
1388
1389 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1390 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1391 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1392 }
1393
1394 return si;
1395}
1396
1397static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1398{
1399 struct ata_port *ap = qc->ap;
1400 struct ahci_port_priv *pp = ap->private_data;
1401
1402 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1403 return ata_std_qc_defer(qc);
1404 else
1405 return sata_pmp_qc_defer_cmd_switch(qc);
1406}
1407
1408static void ahci_qc_prep(struct ata_queued_cmd *qc)
1409{
1410 struct ata_port *ap = qc->ap;
1411 struct ahci_port_priv *pp = ap->private_data;
1412 int is_atapi = ata_is_atapi(qc->tf.protocol);
1413 void *cmd_tbl;
1414 u32 opts;
1415 const u32 cmd_fis_len = 5; /* five dwords */
1416 unsigned int n_elem;
1417
1418 /*
1419 * Fill in command table information. First, the header,
1420 * a SATA Register - Host to Device command FIS.
1421 */
1422 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1423
1424 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1425 if (is_atapi) {
1426 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1427 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1428 }
1429
1430 n_elem = 0;
1431 if (qc->flags & ATA_QCFLAG_DMAMAP)
1432 n_elem = ahci_fill_sg(qc, cmd_tbl);
1433
1434 /*
1435 * Fill in command slot information.
1436 */
1437 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1438 if (qc->tf.flags & ATA_TFLAG_WRITE)
1439 opts |= AHCI_CMD_WRITE;
1440 if (is_atapi)
1441 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1442
1443 ahci_fill_cmd_slot(pp, qc->tag, opts);
1444}
1445
1446static void ahci_fbs_dec_intr(struct ata_port *ap)
1447{
1448 struct ahci_port_priv *pp = ap->private_data;
1449 void __iomem *port_mmio = ahci_port_base(ap);
1450 u32 fbs = readl(port_mmio + PORT_FBS);
1451 int retries = 3;
1452
1453 DPRINTK("ENTER\n");
1454 BUG_ON(!pp->fbs_enabled);
1455
1456 /* time to wait for DEC is not specified by AHCI spec,
1457 * add a retry loop for safety.
1458 */
1459 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1460 fbs = readl(port_mmio + PORT_FBS);
1461 while ((fbs & PORT_FBS_DEC) && retries--) {
1462 udelay(1);
1463 fbs = readl(port_mmio + PORT_FBS);
1464 }
1465
1466 if (fbs & PORT_FBS_DEC)
Joe Perchesa44fec12011-04-15 15:51:58 -07001467 dev_err(ap->host->dev, "failed to clear device error\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001468}
1469
1470static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1471{
1472 struct ahci_host_priv *hpriv = ap->host->private_data;
1473 struct ahci_port_priv *pp = ap->private_data;
1474 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1475 struct ata_link *link = NULL;
1476 struct ata_queued_cmd *active_qc;
1477 struct ata_eh_info *active_ehi;
1478 bool fbs_need_dec = false;
1479 u32 serror;
1480
1481 /* determine active link with error */
1482 if (pp->fbs_enabled) {
1483 void __iomem *port_mmio = ahci_port_base(ap);
1484 u32 fbs = readl(port_mmio + PORT_FBS);
1485 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1486
1487 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links) &&
1488 ata_link_online(&ap->pmp_link[pmp])) {
1489 link = &ap->pmp_link[pmp];
1490 fbs_need_dec = true;
1491 }
1492
1493 } else
1494 ata_for_each_link(link, ap, EDGE)
1495 if (ata_link_active(link))
1496 break;
1497
1498 if (!link)
1499 link = &ap->link;
1500
1501 active_qc = ata_qc_from_tag(ap, link->active_tag);
1502 active_ehi = &link->eh_info;
1503
1504 /* record irq stat */
1505 ata_ehi_clear_desc(host_ehi);
1506 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1507
1508 /* AHCI needs SError cleared; otherwise, it might lock up */
1509 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1510 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1511 host_ehi->serror |= serror;
1512
1513 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1514 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1515 irq_stat &= ~PORT_IRQ_IF_ERR;
1516
1517 if (irq_stat & PORT_IRQ_TF_ERR) {
1518 /* If qc is active, charge it; otherwise, the active
1519 * link. There's no active qc on NCQ errors. It will
1520 * be determined by EH by reading log page 10h.
1521 */
1522 if (active_qc)
1523 active_qc->err_mask |= AC_ERR_DEV;
1524 else
1525 active_ehi->err_mask |= AC_ERR_DEV;
1526
1527 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1528 host_ehi->serror &= ~SERR_INTERNAL;
1529 }
1530
1531 if (irq_stat & PORT_IRQ_UNK_FIS) {
1532 u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
1533
1534 active_ehi->err_mask |= AC_ERR_HSM;
1535 active_ehi->action |= ATA_EH_RESET;
1536 ata_ehi_push_desc(active_ehi,
1537 "unknown FIS %08x %08x %08x %08x" ,
1538 unk[0], unk[1], unk[2], unk[3]);
1539 }
1540
1541 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1542 active_ehi->err_mask |= AC_ERR_HSM;
1543 active_ehi->action |= ATA_EH_RESET;
1544 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1545 }
1546
1547 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1548 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1549 host_ehi->action |= ATA_EH_RESET;
1550 ata_ehi_push_desc(host_ehi, "host bus error");
1551 }
1552
1553 if (irq_stat & PORT_IRQ_IF_ERR) {
1554 if (fbs_need_dec)
1555 active_ehi->err_mask |= AC_ERR_DEV;
1556 else {
1557 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1558 host_ehi->action |= ATA_EH_RESET;
1559 }
1560
1561 ata_ehi_push_desc(host_ehi, "interface fatal error");
1562 }
1563
1564 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1565 ata_ehi_hotplugged(host_ehi);
1566 ata_ehi_push_desc(host_ehi, "%s",
1567 irq_stat & PORT_IRQ_CONNECT ?
1568 "connection status changed" : "PHY RDY changed");
1569 }
1570
1571 /* okay, let's hand over to EH */
1572
1573 if (irq_stat & PORT_IRQ_FREEZE)
1574 ata_port_freeze(ap);
1575 else if (fbs_need_dec) {
1576 ata_link_abort(link);
1577 ahci_fbs_dec_intr(ap);
1578 } else
1579 ata_port_abort(ap);
1580}
1581
1582static void ahci_port_intr(struct ata_port *ap)
1583{
1584 void __iomem *port_mmio = ahci_port_base(ap);
1585 struct ata_eh_info *ehi = &ap->link.eh_info;
1586 struct ahci_port_priv *pp = ap->private_data;
1587 struct ahci_host_priv *hpriv = ap->host->private_data;
1588 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1589 u32 status, qc_active = 0;
1590 int rc;
1591
1592 status = readl(port_mmio + PORT_IRQ_STAT);
1593 writel(status, port_mmio + PORT_IRQ_STAT);
1594
1595 /* ignore BAD_PMP while resetting */
1596 if (unlikely(resetting))
1597 status &= ~PORT_IRQ_BAD_PMP;
1598
Tejun Heo6b7ae952010-09-01 17:50:06 +02001599 /* if LPM is enabled, PHYRDY doesn't mean anything */
1600 if (ap->link.lpm_policy > ATA_LPM_MAX_POWER) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001601 status &= ~PORT_IRQ_PHYRDY;
Tejun Heo6b7ae952010-09-01 17:50:06 +02001602 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001603 }
1604
1605 if (unlikely(status & PORT_IRQ_ERROR)) {
1606 ahci_error_intr(ap, status);
1607 return;
1608 }
1609
1610 if (status & PORT_IRQ_SDB_FIS) {
1611 /* If SNotification is available, leave notification
1612 * handling to sata_async_notification(). If not,
1613 * emulate it by snooping SDB FIS RX area.
1614 *
1615 * Snooping FIS RX area is probably cheaper than
1616 * poking SNotification but some constrollers which
1617 * implement SNotification, ICH9 for example, don't
1618 * store AN SDB FIS into receive area.
1619 */
1620 if (hpriv->cap & HOST_CAP_SNTF)
1621 sata_async_notification(ap);
1622 else {
1623 /* If the 'N' bit in word 0 of the FIS is set,
1624 * we just received asynchronous notification.
1625 * Tell libata about it.
1626 *
1627 * Lack of SNotification should not appear in
1628 * ahci 1.2, so the workaround is unnecessary
1629 * when FBS is enabled.
1630 */
1631 if (pp->fbs_enabled)
1632 WARN_ON_ONCE(1);
1633 else {
1634 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1635 u32 f0 = le32_to_cpu(f[0]);
1636 if (f0 & (1 << 15))
1637 sata_async_notification(ap);
1638 }
1639 }
1640 }
1641
1642 /* pp->active_link is not reliable once FBS is enabled, both
1643 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1644 * NCQ and non-NCQ commands may be in flight at the same time.
1645 */
1646 if (pp->fbs_enabled) {
1647 if (ap->qc_active) {
1648 qc_active = readl(port_mmio + PORT_SCR_ACT);
1649 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1650 }
1651 } else {
1652 /* pp->active_link is valid iff any command is in flight */
1653 if (ap->qc_active && pp->active_link->sactive)
1654 qc_active = readl(port_mmio + PORT_SCR_ACT);
1655 else
1656 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1657 }
1658
1659
1660 rc = ata_qc_complete_multiple(ap, qc_active);
1661
1662 /* while resetting, invalid completions are expected */
1663 if (unlikely(rc < 0 && !resetting)) {
1664 ehi->err_mask |= AC_ERR_HSM;
1665 ehi->action |= ATA_EH_RESET;
1666 ata_port_freeze(ap);
1667 }
1668}
1669
1670irqreturn_t ahci_interrupt(int irq, void *dev_instance)
1671{
1672 struct ata_host *host = dev_instance;
1673 struct ahci_host_priv *hpriv;
1674 unsigned int i, handled = 0;
1675 void __iomem *mmio;
1676 u32 irq_stat, irq_masked;
1677
1678 VPRINTK("ENTER\n");
1679
1680 hpriv = host->private_data;
1681 mmio = hpriv->mmio;
1682
1683 /* sigh. 0xffffffff is a valid return from h/w */
1684 irq_stat = readl(mmio + HOST_IRQ_STAT);
1685 if (!irq_stat)
1686 return IRQ_NONE;
1687
1688 irq_masked = irq_stat & hpriv->port_map;
1689
1690 spin_lock(&host->lock);
1691
1692 for (i = 0; i < host->n_ports; i++) {
1693 struct ata_port *ap;
1694
1695 if (!(irq_masked & (1 << i)))
1696 continue;
1697
1698 ap = host->ports[i];
1699 if (ap) {
1700 ahci_port_intr(ap);
1701 VPRINTK("port %u\n", i);
1702 } else {
1703 VPRINTK("port %u (no irq)\n", i);
1704 if (ata_ratelimit())
Joe Perchesa44fec12011-04-15 15:51:58 -07001705 dev_warn(host->dev,
1706 "interrupt on disabled port %u\n", i);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001707 }
1708
1709 handled = 1;
1710 }
1711
1712 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1713 * it should be cleared after all the port events are cleared;
1714 * otherwise, it will raise a spurious interrupt after each
1715 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1716 * information.
1717 *
1718 * Also, use the unmasked value to clear interrupt as spurious
1719 * pending event on a dummy port might cause screaming IRQ.
1720 */
1721 writel(irq_stat, mmio + HOST_IRQ_STAT);
1722
1723 spin_unlock(&host->lock);
1724
1725 VPRINTK("EXIT\n");
1726
1727 return IRQ_RETVAL(handled);
1728}
1729EXPORT_SYMBOL_GPL(ahci_interrupt);
1730
1731static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1732{
1733 struct ata_port *ap = qc->ap;
1734 void __iomem *port_mmio = ahci_port_base(ap);
1735 struct ahci_port_priv *pp = ap->private_data;
1736
1737 /* Keep track of the currently active link. It will be used
1738 * in completion path to determine whether NCQ phase is in
1739 * progress.
1740 */
1741 pp->active_link = qc->dev->link;
1742
1743 if (qc->tf.protocol == ATA_PROT_NCQ)
1744 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1745
1746 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
1747 u32 fbs = readl(port_mmio + PORT_FBS);
1748 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1749 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
1750 writel(fbs, port_mmio + PORT_FBS);
1751 pp->fbs_last_dev = qc->dev->link->pmp;
1752 }
1753
1754 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
1755
1756 ahci_sw_activity(qc->dev->link);
1757
1758 return 0;
1759}
1760
1761static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
1762{
1763 struct ahci_port_priv *pp = qc->ap->private_data;
Tejun Heo6ad60192010-10-15 11:00:08 +02001764 u8 *rx_fis = pp->rx_fis;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001765
1766 if (pp->fbs_enabled)
Tejun Heo6ad60192010-10-15 11:00:08 +02001767 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001768
Tejun Heo6ad60192010-10-15 11:00:08 +02001769 /*
1770 * After a successful execution of an ATA PIO data-in command,
1771 * the device doesn't send D2H Reg FIS to update the TF and
1772 * the host should take TF and E_Status from the preceding PIO
1773 * Setup FIS.
1774 */
1775 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
1776 !(qc->flags & ATA_QCFLAG_FAILED)) {
1777 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
1778 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
1779 } else
1780 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
1781
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001782 return true;
1783}
1784
1785static void ahci_freeze(struct ata_port *ap)
1786{
1787 void __iomem *port_mmio = ahci_port_base(ap);
1788
1789 /* turn IRQ off */
1790 writel(0, port_mmio + PORT_IRQ_MASK);
1791}
1792
1793static void ahci_thaw(struct ata_port *ap)
1794{
1795 struct ahci_host_priv *hpriv = ap->host->private_data;
1796 void __iomem *mmio = hpriv->mmio;
1797 void __iomem *port_mmio = ahci_port_base(ap);
1798 u32 tmp;
1799 struct ahci_port_priv *pp = ap->private_data;
1800
1801 /* clear IRQ */
1802 tmp = readl(port_mmio + PORT_IRQ_STAT);
1803 writel(tmp, port_mmio + PORT_IRQ_STAT);
1804 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
1805
1806 /* turn IRQ back on */
1807 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1808}
1809
1810static void ahci_error_handler(struct ata_port *ap)
1811{
1812 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
1813 /* restart engine */
1814 ahci_stop_engine(ap);
1815 ahci_start_engine(ap);
1816 }
1817
1818 sata_pmp_error_handler(ap);
Tejun Heo0ee71952010-06-07 15:15:08 +02001819
1820 if (!ata_dev_enabled(ap->link.device))
1821 ahci_stop_engine(ap);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001822}
1823
1824static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
1825{
1826 struct ata_port *ap = qc->ap;
1827
1828 /* make DMA engine forget about the failed command */
1829 if (qc->flags & ATA_QCFLAG_FAILED)
1830 ahci_kick_engine(ap);
1831}
1832
1833static void ahci_enable_fbs(struct ata_port *ap)
1834{
1835 struct ahci_port_priv *pp = ap->private_data;
1836 void __iomem *port_mmio = ahci_port_base(ap);
1837 u32 fbs;
1838 int rc;
1839
1840 if (!pp->fbs_supported)
1841 return;
1842
1843 fbs = readl(port_mmio + PORT_FBS);
1844 if (fbs & PORT_FBS_EN) {
1845 pp->fbs_enabled = true;
1846 pp->fbs_last_dev = -1; /* initialization */
1847 return;
1848 }
1849
1850 rc = ahci_stop_engine(ap);
1851 if (rc)
1852 return;
1853
1854 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
1855 fbs = readl(port_mmio + PORT_FBS);
1856 if (fbs & PORT_FBS_EN) {
Joe Perchesa44fec12011-04-15 15:51:58 -07001857 dev_info(ap->host->dev, "FBS is enabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001858 pp->fbs_enabled = true;
1859 pp->fbs_last_dev = -1; /* initialization */
1860 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07001861 dev_err(ap->host->dev, "Failed to enable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001862
1863 ahci_start_engine(ap);
1864}
1865
1866static void ahci_disable_fbs(struct ata_port *ap)
1867{
1868 struct ahci_port_priv *pp = ap->private_data;
1869 void __iomem *port_mmio = ahci_port_base(ap);
1870 u32 fbs;
1871 int rc;
1872
1873 if (!pp->fbs_supported)
1874 return;
1875
1876 fbs = readl(port_mmio + PORT_FBS);
1877 if ((fbs & PORT_FBS_EN) == 0) {
1878 pp->fbs_enabled = false;
1879 return;
1880 }
1881
1882 rc = ahci_stop_engine(ap);
1883 if (rc)
1884 return;
1885
1886 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
1887 fbs = readl(port_mmio + PORT_FBS);
1888 if (fbs & PORT_FBS_EN)
Joe Perchesa44fec12011-04-15 15:51:58 -07001889 dev_err(ap->host->dev, "Failed to disable FBS\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001890 else {
Joe Perchesa44fec12011-04-15 15:51:58 -07001891 dev_info(ap->host->dev, "FBS is disabled\n");
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001892 pp->fbs_enabled = false;
1893 }
1894
1895 ahci_start_engine(ap);
1896}
1897
1898static void ahci_pmp_attach(struct ata_port *ap)
1899{
1900 void __iomem *port_mmio = ahci_port_base(ap);
1901 struct ahci_port_priv *pp = ap->private_data;
1902 u32 cmd;
1903
1904 cmd = readl(port_mmio + PORT_CMD);
1905 cmd |= PORT_CMD_PMP;
1906 writel(cmd, port_mmio + PORT_CMD);
1907
1908 ahci_enable_fbs(ap);
1909
1910 pp->intr_mask |= PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01001911
1912 /*
1913 * We must not change the port interrupt mask register if the
1914 * port is marked frozen, the value in pp->intr_mask will be
1915 * restored later when the port is thawed.
1916 *
1917 * Note that during initialization, the port is marked as
1918 * frozen since the irq handler is not yet registered.
1919 */
1920 if (!(ap->pflags & ATA_PFLAG_FROZEN))
1921 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001922}
1923
1924static void ahci_pmp_detach(struct ata_port *ap)
1925{
1926 void __iomem *port_mmio = ahci_port_base(ap);
1927 struct ahci_port_priv *pp = ap->private_data;
1928 u32 cmd;
1929
1930 ahci_disable_fbs(ap);
1931
1932 cmd = readl(port_mmio + PORT_CMD);
1933 cmd &= ~PORT_CMD_PMP;
1934 writel(cmd, port_mmio + PORT_CMD);
1935
1936 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
Maxime Bizon7b3a24c2011-03-16 14:58:32 +01001937
1938 /* see comment above in ahci_pmp_attach() */
1939 if (!(ap->pflags & ATA_PFLAG_FROZEN))
1940 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001941}
1942
David Milburn02cdfcf2010-11-12 15:38:21 -06001943int ahci_port_resume(struct ata_port *ap)
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001944{
1945 ahci_power_up(ap);
1946 ahci_start_port(ap);
1947
1948 if (sata_pmp_attached(ap))
1949 ahci_pmp_attach(ap);
1950 else
1951 ahci_pmp_detach(ap);
1952
1953 return 0;
1954}
David Milburn02cdfcf2010-11-12 15:38:21 -06001955EXPORT_SYMBOL_GPL(ahci_port_resume);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001956
1957#ifdef CONFIG_PM
1958static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
1959{
1960 const char *emsg = NULL;
1961 int rc;
1962
1963 rc = ahci_deinit_port(ap, &emsg);
1964 if (rc == 0)
1965 ahci_power_down(ap);
1966 else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001967 ata_port_err(ap, "%s (%d)\n", emsg, rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04001968 ahci_start_port(ap);
1969 }
1970
1971 return rc;
1972}
1973#endif
1974
1975static int ahci_port_start(struct ata_port *ap)
1976{
1977 struct ahci_host_priv *hpriv = ap->host->private_data;
1978 struct device *dev = ap->host->dev;
1979 struct ahci_port_priv *pp;
1980 void *mem;
1981 dma_addr_t mem_dma;
1982 size_t dma_sz, rx_fis_sz;
1983
1984 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1985 if (!pp)
1986 return -ENOMEM;
1987
1988 /* check FBS capability */
1989 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
1990 void __iomem *port_mmio = ahci_port_base(ap);
1991 u32 cmd = readl(port_mmio + PORT_CMD);
1992 if (cmd & PORT_CMD_FBSCP)
1993 pp->fbs_supported = true;
Tejun Heo5f173102010-07-24 16:53:48 +02001994 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
Joe Perchesa44fec12011-04-15 15:51:58 -07001995 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
1996 ap->port_no);
Tejun Heo5f173102010-07-24 16:53:48 +02001997 pp->fbs_supported = true;
1998 } else
Joe Perchesa44fec12011-04-15 15:51:58 -07001999 dev_warn(dev, "port %d is not capable of FBS\n",
2000 ap->port_no);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002001 }
2002
2003 if (pp->fbs_supported) {
2004 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2005 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2006 } else {
2007 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2008 rx_fis_sz = AHCI_RX_FIS_SZ;
2009 }
2010
2011 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2012 if (!mem)
2013 return -ENOMEM;
2014 memset(mem, 0, dma_sz);
2015
2016 /*
2017 * First item in chunk of DMA memory: 32-slot command table,
2018 * 32 bytes each in size
2019 */
2020 pp->cmd_slot = mem;
2021 pp->cmd_slot_dma = mem_dma;
2022
2023 mem += AHCI_CMD_SLOT_SZ;
2024 mem_dma += AHCI_CMD_SLOT_SZ;
2025
2026 /*
2027 * Second item: Received-FIS area
2028 */
2029 pp->rx_fis = mem;
2030 pp->rx_fis_dma = mem_dma;
2031
2032 mem += rx_fis_sz;
2033 mem_dma += rx_fis_sz;
2034
2035 /*
2036 * Third item: data area for storing a single command
2037 * and its scatter-gather table
2038 */
2039 pp->cmd_tbl = mem;
2040 pp->cmd_tbl_dma = mem_dma;
2041
2042 /*
2043 * Save off initial list of interrupts to be enabled.
2044 * This could be changed later
2045 */
2046 pp->intr_mask = DEF_PORT_IRQ;
2047
2048 ap->private_data = pp;
2049
2050 /* engage engines, captain */
2051 return ahci_port_resume(ap);
2052}
2053
2054static void ahci_port_stop(struct ata_port *ap)
2055{
2056 const char *emsg = NULL;
2057 int rc;
2058
2059 /* de-initialize port */
2060 rc = ahci_deinit_port(ap, &emsg);
2061 if (rc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002062 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002063}
2064
2065void ahci_print_info(struct ata_host *host, const char *scc_s)
2066{
2067 struct ahci_host_priv *hpriv = host->private_data;
2068 void __iomem *mmio = hpriv->mmio;
2069 u32 vers, cap, cap2, impl, speed;
2070 const char *speed_s;
2071
2072 vers = readl(mmio + HOST_VERSION);
2073 cap = hpriv->cap;
2074 cap2 = hpriv->cap2;
2075 impl = hpriv->port_map;
2076
2077 speed = (cap >> 20) & 0xf;
2078 if (speed == 1)
2079 speed_s = "1.5";
2080 else if (speed == 2)
2081 speed_s = "3";
2082 else if (speed == 3)
2083 speed_s = "6";
2084 else
2085 speed_s = "?";
2086
2087 dev_info(host->dev,
2088 "AHCI %02x%02x.%02x%02x "
2089 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2090 ,
2091
2092 (vers >> 24) & 0xff,
2093 (vers >> 16) & 0xff,
2094 (vers >> 8) & 0xff,
2095 vers & 0xff,
2096
2097 ((cap >> 8) & 0x1f) + 1,
2098 (cap & 0x1f) + 1,
2099 speed_s,
2100 impl,
2101 scc_s);
2102
2103 dev_info(host->dev,
2104 "flags: "
2105 "%s%s%s%s%s%s%s"
2106 "%s%s%s%s%s%s%s"
2107 "%s%s%s%s%s%s\n"
2108 ,
2109
2110 cap & HOST_CAP_64 ? "64bit " : "",
2111 cap & HOST_CAP_NCQ ? "ncq " : "",
2112 cap & HOST_CAP_SNTF ? "sntf " : "",
2113 cap & HOST_CAP_MPS ? "ilck " : "",
2114 cap & HOST_CAP_SSS ? "stag " : "",
2115 cap & HOST_CAP_ALPM ? "pm " : "",
2116 cap & HOST_CAP_LED ? "led " : "",
2117 cap & HOST_CAP_CLO ? "clo " : "",
2118 cap & HOST_CAP_ONLY ? "only " : "",
2119 cap & HOST_CAP_PMP ? "pmp " : "",
2120 cap & HOST_CAP_FBS ? "fbs " : "",
2121 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2122 cap & HOST_CAP_SSC ? "slum " : "",
2123 cap & HOST_CAP_PART ? "part " : "",
2124 cap & HOST_CAP_CCC ? "ccc " : "",
2125 cap & HOST_CAP_EMS ? "ems " : "",
2126 cap & HOST_CAP_SXS ? "sxs " : "",
2127 cap2 & HOST_CAP2_APST ? "apst " : "",
2128 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2129 cap2 & HOST_CAP2_BOH ? "boh " : ""
2130 );
2131}
2132EXPORT_SYMBOL_GPL(ahci_print_info);
2133
2134void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2135 struct ata_port_info *pi)
2136{
2137 u8 messages;
2138 void __iomem *mmio = hpriv->mmio;
2139 u32 em_loc = readl(mmio + HOST_EM_LOC);
2140 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2141
2142 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2143 return;
2144
2145 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2146
Harry Zhang008dbd62010-04-23 17:27:19 +08002147 if (messages) {
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002148 /* store em_loc */
2149 hpriv->em_loc = ((em_loc >> 16) * 4);
Harry Zhangc0623162010-04-23 17:28:38 +08002150 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
Harry Zhang008dbd62010-04-23 17:27:19 +08002151 hpriv->em_msg_type = messages;
Anton Vorontsov365cfa12010-03-28 00:22:14 -04002152 pi->flags |= ATA_FLAG_EM;
2153 if (!(em_ctl & EM_CTL_ALHD))
2154 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2155 }
2156}
2157EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2158
2159MODULE_AUTHOR("Jeff Garzik");
2160MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2161MODULE_LICENSE("GPL");