Staging: sep: do not select the driver by default
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / libahci.c
blobebc08d65b3dd04c9af94da0d7146d3a165ce9803
1 /*
2 * libahci.c - Common AHCI SATA low-level routines
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2004-2005 Red Hat, Inc.
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.
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.
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.
26 * libata documentation is available via 'make {ps|pdf}docs',
27 * as Documentation/DocBook/libata.*
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
35 #include <linux/kernel.h>
36 #include <linux/gfp.h>
37 #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"
49 static int ahci_skip_host_reset;
50 int ahci_ignore_sss;
51 EXPORT_SYMBOL_GPL(ahci_ignore_sss);
53 module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
54 MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
56 module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
57 MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
59 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
60 unsigned hints);
61 static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
62 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
63 size_t size);
64 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
65 ssize_t size);
69 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
70 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
71 static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
72 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
73 static int ahci_port_start(struct ata_port *ap);
74 static void ahci_port_stop(struct ata_port *ap);
75 static void ahci_qc_prep(struct ata_queued_cmd *qc);
76 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
77 static void ahci_freeze(struct ata_port *ap);
78 static void ahci_thaw(struct ata_port *ap);
79 static void ahci_enable_fbs(struct ata_port *ap);
80 static void ahci_disable_fbs(struct ata_port *ap);
81 static void ahci_pmp_attach(struct ata_port *ap);
82 static void ahci_pmp_detach(struct ata_port *ap);
83 static int ahci_softreset(struct ata_link *link, unsigned int *class,
84 unsigned long deadline);
85 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
86 unsigned long deadline);
87 static void ahci_postreset(struct ata_link *link, unsigned int *class);
88 static void ahci_error_handler(struct ata_port *ap);
89 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
90 static int ahci_port_resume(struct ata_port *ap);
91 static void ahci_dev_config(struct ata_device *dev);
92 static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
93 u32 opts);
94 #ifdef CONFIG_PM
95 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
96 #endif
97 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
98 static ssize_t ahci_activity_store(struct ata_device *dev,
99 enum sw_activity val);
100 static void ahci_init_sw_activity(struct ata_link *link);
102 static ssize_t ahci_show_host_caps(struct device *dev,
103 struct device_attribute *attr, char *buf);
104 static ssize_t ahci_show_host_cap2(struct device *dev,
105 struct device_attribute *attr, char *buf);
106 static ssize_t ahci_show_host_version(struct device *dev,
107 struct device_attribute *attr, char *buf);
108 static ssize_t ahci_show_port_cmd(struct device *dev,
109 struct device_attribute *attr, char *buf);
110 static ssize_t ahci_read_em_buffer(struct device *dev,
111 struct device_attribute *attr, char *buf);
112 static ssize_t ahci_store_em_buffer(struct device *dev,
113 struct device_attribute *attr,
114 const char *buf, size_t size);
116 static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
117 static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
118 static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
119 static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
120 static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
121 ahci_read_em_buffer, ahci_store_em_buffer);
123 struct device_attribute *ahci_shost_attrs[] = {
124 &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,
131 &dev_attr_em_buffer,
132 NULL
134 EXPORT_SYMBOL_GPL(ahci_shost_attrs);
136 struct device_attribute *ahci_sdev_attrs[] = {
137 &dev_attr_sw_activity,
138 &dev_attr_unload_heads,
139 NULL
141 EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
143 struct ata_port_operations ahci_ops = {
144 .inherits = &sata_pmp_port_ops,
146 .qc_defer = ahci_pmp_qc_defer,
147 .qc_prep = ahci_qc_prep,
148 .qc_issue = ahci_qc_issue,
149 .qc_fill_rtf = ahci_qc_fill_rtf,
151 .freeze = ahci_freeze,
152 .thaw = ahci_thaw,
153 .softreset = ahci_softreset,
154 .hardreset = ahci_hardreset,
155 .postreset = ahci_postreset,
156 .pmp_softreset = ahci_softreset,
157 .error_handler = ahci_error_handler,
158 .post_internal_cmd = ahci_post_internal_cmd,
159 .dev_config = ahci_dev_config,
161 .scr_read = ahci_scr_read,
162 .scr_write = ahci_scr_write,
163 .pmp_attach = ahci_pmp_attach,
164 .pmp_detach = ahci_pmp_detach,
166 .set_lpm = ahci_set_lpm,
167 .em_show = ahci_led_show,
168 .em_store = ahci_led_store,
169 .sw_activity_show = ahci_activity_show,
170 .sw_activity_store = ahci_activity_store,
171 #ifdef CONFIG_PM
172 .port_suspend = ahci_port_suspend,
173 .port_resume = ahci_port_resume,
174 #endif
175 .port_start = ahci_port_start,
176 .port_stop = ahci_port_stop,
178 EXPORT_SYMBOL_GPL(ahci_ops);
180 int ahci_em_messages = 1;
181 EXPORT_SYMBOL_GPL(ahci_em_messages);
182 module_param(ahci_em_messages, int, 0444);
183 /* add other LED protocol types when they become supported */
184 MODULE_PARM_DESC(ahci_em_messages,
185 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
187 static void ahci_enable_ahci(void __iomem *mmio)
189 int i;
190 u32 tmp;
192 /* turn on AHCI_EN */
193 tmp = readl(mmio + HOST_CTL);
194 if (tmp & HOST_AHCI_EN)
195 return;
197 /* Some controllers need AHCI_EN to be written multiple times.
198 * Try a few times before giving up.
200 for (i = 0; i < 5; i++) {
201 tmp |= HOST_AHCI_EN;
202 writel(tmp, mmio + HOST_CTL);
203 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
204 if (tmp & HOST_AHCI_EN)
205 return;
206 msleep(10);
209 WARN_ON(1);
212 static ssize_t ahci_show_host_caps(struct device *dev,
213 struct device_attribute *attr, char *buf)
215 struct Scsi_Host *shost = class_to_shost(dev);
216 struct ata_port *ap = ata_shost_to_port(shost);
217 struct ahci_host_priv *hpriv = ap->host->private_data;
219 return sprintf(buf, "%x\n", hpriv->cap);
222 static ssize_t ahci_show_host_cap2(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 struct Scsi_Host *shost = class_to_shost(dev);
226 struct ata_port *ap = ata_shost_to_port(shost);
227 struct ahci_host_priv *hpriv = ap->host->private_data;
229 return sprintf(buf, "%x\n", hpriv->cap2);
232 static ssize_t ahci_show_host_version(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct Scsi_Host *shost = class_to_shost(dev);
236 struct ata_port *ap = ata_shost_to_port(shost);
237 struct ahci_host_priv *hpriv = ap->host->private_data;
238 void __iomem *mmio = hpriv->mmio;
240 return sprintf(buf, "%x\n", readl(mmio + HOST_VERSION));
243 static ssize_t ahci_show_port_cmd(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 struct Scsi_Host *shost = class_to_shost(dev);
247 struct ata_port *ap = ata_shost_to_port(shost);
248 void __iomem *port_mmio = ahci_port_base(ap);
250 return sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
253 static ssize_t ahci_read_em_buffer(struct device *dev,
254 struct device_attribute *attr, char *buf)
256 struct Scsi_Host *shost = class_to_shost(dev);
257 struct ata_port *ap = ata_shost_to_port(shost);
258 struct ahci_host_priv *hpriv = ap->host->private_data;
259 void __iomem *mmio = hpriv->mmio;
260 void __iomem *em_mmio = mmio + hpriv->em_loc;
261 u32 em_ctl, msg;
262 unsigned long flags;
263 size_t count;
264 int i;
266 spin_lock_irqsave(ap->lock, flags);
268 em_ctl = readl(mmio + HOST_EM_CTL);
269 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
270 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
271 spin_unlock_irqrestore(ap->lock, flags);
272 return -EINVAL;
275 if (!(em_ctl & EM_CTL_MR)) {
276 spin_unlock_irqrestore(ap->lock, flags);
277 return -EAGAIN;
280 if (!(em_ctl & EM_CTL_SMB))
281 em_mmio += hpriv->em_buf_sz;
283 count = hpriv->em_buf_sz;
285 /* the count should not be larger than PAGE_SIZE */
286 if (count > PAGE_SIZE) {
287 if (printk_ratelimit())
288 ata_port_printk(ap, KERN_WARNING,
289 "EM read buffer size too large: "
290 "buffer size %u, page size %lu\n",
291 hpriv->em_buf_sz, PAGE_SIZE);
292 count = PAGE_SIZE;
295 for (i = 0; i < count; i += 4) {
296 msg = readl(em_mmio + i);
297 buf[i] = msg & 0xff;
298 buf[i + 1] = (msg >> 8) & 0xff;
299 buf[i + 2] = (msg >> 16) & 0xff;
300 buf[i + 3] = (msg >> 24) & 0xff;
303 spin_unlock_irqrestore(ap->lock, flags);
305 return i;
308 static ssize_t ahci_store_em_buffer(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf, size_t size)
312 struct Scsi_Host *shost = class_to_shost(dev);
313 struct ata_port *ap = ata_shost_to_port(shost);
314 struct ahci_host_priv *hpriv = ap->host->private_data;
315 void __iomem *mmio = hpriv->mmio;
316 void __iomem *em_mmio = mmio + hpriv->em_loc;
317 const unsigned char *msg_buf = buf;
318 u32 em_ctl, msg;
319 unsigned long flags;
320 int i;
322 /* check size validity */
323 if (!(ap->flags & ATA_FLAG_EM) ||
324 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
325 size % 4 || size > hpriv->em_buf_sz)
326 return -EINVAL;
328 spin_lock_irqsave(ap->lock, flags);
330 em_ctl = readl(mmio + HOST_EM_CTL);
331 if (em_ctl & EM_CTL_TM) {
332 spin_unlock_irqrestore(ap->lock, flags);
333 return -EBUSY;
336 for (i = 0; i < size; i += 4) {
337 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
338 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
339 writel(msg, em_mmio + i);
342 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
344 spin_unlock_irqrestore(ap->lock, flags);
346 return size;
350 * ahci_save_initial_config - Save and fixup initial config values
351 * @dev: target AHCI device
352 * @hpriv: host private area to store config values
353 * @force_port_map: force port map to a specified value
354 * @mask_port_map: mask out particular bits from port map
356 * Some registers containing configuration info might be setup by
357 * BIOS and might be cleared on reset. This function saves the
358 * initial values of those registers into @hpriv such that they
359 * can be restored after controller reset.
361 * If inconsistent, config values are fixed up by this function.
363 * LOCKING:
364 * None.
366 void ahci_save_initial_config(struct device *dev,
367 struct ahci_host_priv *hpriv,
368 unsigned int force_port_map,
369 unsigned int mask_port_map)
371 void __iomem *mmio = hpriv->mmio;
372 u32 cap, cap2, vers, port_map;
373 int i;
375 /* make sure AHCI mode is enabled before accessing CAP */
376 ahci_enable_ahci(mmio);
378 /* Values prefixed with saved_ are written back to host after
379 * reset. Values without are used for driver operation.
381 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
382 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
384 /* CAP2 register is only defined for AHCI 1.2 and later */
385 vers = readl(mmio + HOST_VERSION);
386 if ((vers >> 16) > 1 ||
387 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
388 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
389 else
390 hpriv->saved_cap2 = cap2 = 0;
392 /* some chips have errata preventing 64bit use */
393 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
394 dev_printk(KERN_INFO, dev,
395 "controller can't do 64bit DMA, forcing 32bit\n");
396 cap &= ~HOST_CAP_64;
399 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
400 dev_printk(KERN_INFO, dev,
401 "controller can't do NCQ, turning off CAP_NCQ\n");
402 cap &= ~HOST_CAP_NCQ;
405 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
406 dev_printk(KERN_INFO, dev,
407 "controller can do NCQ, turning on CAP_NCQ\n");
408 cap |= HOST_CAP_NCQ;
411 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
412 dev_printk(KERN_INFO, dev,
413 "controller can't do PMP, turning off CAP_PMP\n");
414 cap &= ~HOST_CAP_PMP;
417 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
418 dev_printk(KERN_INFO, dev,
419 "controller can't do SNTF, turning off CAP_SNTF\n");
420 cap &= ~HOST_CAP_SNTF;
423 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
424 dev_printk(KERN_INFO, dev,
425 "controller can do FBS, turning on CAP_FBS\n");
426 cap |= HOST_CAP_FBS;
429 if (force_port_map && port_map != force_port_map) {
430 dev_printk(KERN_INFO, dev, "forcing port_map 0x%x -> 0x%x\n",
431 port_map, force_port_map);
432 port_map = force_port_map;
435 if (mask_port_map) {
436 dev_printk(KERN_ERR, dev, "masking port_map 0x%x -> 0x%x\n",
437 port_map,
438 port_map & mask_port_map);
439 port_map &= mask_port_map;
442 /* cross check port_map and cap.n_ports */
443 if (port_map) {
444 int map_ports = 0;
446 for (i = 0; i < AHCI_MAX_PORTS; i++)
447 if (port_map & (1 << i))
448 map_ports++;
450 /* If PI has more ports than n_ports, whine, clear
451 * port_map and let it be generated from n_ports.
453 if (map_ports > ahci_nr_ports(cap)) {
454 dev_printk(KERN_WARNING, dev,
455 "implemented port map (0x%x) contains more "
456 "ports than nr_ports (%u), using nr_ports\n",
457 port_map, ahci_nr_ports(cap));
458 port_map = 0;
462 /* fabricate port_map from cap.nr_ports */
463 if (!port_map) {
464 port_map = (1 << ahci_nr_ports(cap)) - 1;
465 dev_printk(KERN_WARNING, dev,
466 "forcing PORTS_IMPL to 0x%x\n", port_map);
468 /* write the fixed up value to the PI register */
469 hpriv->saved_port_map = port_map;
472 /* record values to use during operation */
473 hpriv->cap = cap;
474 hpriv->cap2 = cap2;
475 hpriv->port_map = port_map;
477 EXPORT_SYMBOL_GPL(ahci_save_initial_config);
480 * ahci_restore_initial_config - Restore initial config
481 * @host: target ATA host
483 * Restore initial config stored by ahci_save_initial_config().
485 * LOCKING:
486 * None.
488 static void ahci_restore_initial_config(struct ata_host *host)
490 struct ahci_host_priv *hpriv = host->private_data;
491 void __iomem *mmio = hpriv->mmio;
493 writel(hpriv->saved_cap, mmio + HOST_CAP);
494 if (hpriv->saved_cap2)
495 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
496 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
497 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
500 static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
502 static const int offset[] = {
503 [SCR_STATUS] = PORT_SCR_STAT,
504 [SCR_CONTROL] = PORT_SCR_CTL,
505 [SCR_ERROR] = PORT_SCR_ERR,
506 [SCR_ACTIVE] = PORT_SCR_ACT,
507 [SCR_NOTIFICATION] = PORT_SCR_NTF,
509 struct ahci_host_priv *hpriv = ap->host->private_data;
511 if (sc_reg < ARRAY_SIZE(offset) &&
512 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
513 return offset[sc_reg];
514 return 0;
517 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
519 void __iomem *port_mmio = ahci_port_base(link->ap);
520 int offset = ahci_scr_offset(link->ap, sc_reg);
522 if (offset) {
523 *val = readl(port_mmio + offset);
524 return 0;
526 return -EINVAL;
529 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
531 void __iomem *port_mmio = ahci_port_base(link->ap);
532 int offset = ahci_scr_offset(link->ap, sc_reg);
534 if (offset) {
535 writel(val, port_mmio + offset);
536 return 0;
538 return -EINVAL;
541 void ahci_start_engine(struct ata_port *ap)
543 void __iomem *port_mmio = ahci_port_base(ap);
544 u32 tmp;
546 /* start DMA */
547 tmp = readl(port_mmio + PORT_CMD);
548 tmp |= PORT_CMD_START;
549 writel(tmp, port_mmio + PORT_CMD);
550 readl(port_mmio + PORT_CMD); /* flush */
552 EXPORT_SYMBOL_GPL(ahci_start_engine);
554 int ahci_stop_engine(struct ata_port *ap)
556 void __iomem *port_mmio = ahci_port_base(ap);
557 u32 tmp;
559 tmp = readl(port_mmio + PORT_CMD);
561 /* check if the HBA is idle */
562 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
563 return 0;
565 /* setting HBA to idle */
566 tmp &= ~PORT_CMD_START;
567 writel(tmp, port_mmio + PORT_CMD);
569 /* wait for engine to stop. This could be as long as 500 msec */
570 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
571 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
572 if (tmp & PORT_CMD_LIST_ON)
573 return -EIO;
575 return 0;
577 EXPORT_SYMBOL_GPL(ahci_stop_engine);
579 static void ahci_start_fis_rx(struct ata_port *ap)
581 void __iomem *port_mmio = ahci_port_base(ap);
582 struct ahci_host_priv *hpriv = ap->host->private_data;
583 struct ahci_port_priv *pp = ap->private_data;
584 u32 tmp;
586 /* set FIS registers */
587 if (hpriv->cap & HOST_CAP_64)
588 writel((pp->cmd_slot_dma >> 16) >> 16,
589 port_mmio + PORT_LST_ADDR_HI);
590 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
592 if (hpriv->cap & HOST_CAP_64)
593 writel((pp->rx_fis_dma >> 16) >> 16,
594 port_mmio + PORT_FIS_ADDR_HI);
595 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
597 /* enable FIS reception */
598 tmp = readl(port_mmio + PORT_CMD);
599 tmp |= PORT_CMD_FIS_RX;
600 writel(tmp, port_mmio + PORT_CMD);
602 /* flush */
603 readl(port_mmio + PORT_CMD);
606 static int ahci_stop_fis_rx(struct ata_port *ap)
608 void __iomem *port_mmio = ahci_port_base(ap);
609 u32 tmp;
611 /* disable FIS reception */
612 tmp = readl(port_mmio + PORT_CMD);
613 tmp &= ~PORT_CMD_FIS_RX;
614 writel(tmp, port_mmio + PORT_CMD);
616 /* wait for completion, spec says 500ms, give it 1000 */
617 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
618 PORT_CMD_FIS_ON, 10, 1000);
619 if (tmp & PORT_CMD_FIS_ON)
620 return -EBUSY;
622 return 0;
625 static void ahci_power_up(struct ata_port *ap)
627 struct ahci_host_priv *hpriv = ap->host->private_data;
628 void __iomem *port_mmio = ahci_port_base(ap);
629 u32 cmd;
631 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
633 /* spin up device */
634 if (hpriv->cap & HOST_CAP_SSS) {
635 cmd |= PORT_CMD_SPIN_UP;
636 writel(cmd, port_mmio + PORT_CMD);
639 /* wake up link */
640 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
643 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
644 unsigned int hints)
646 struct ata_port *ap = link->ap;
647 struct ahci_host_priv *hpriv = ap->host->private_data;
648 struct ahci_port_priv *pp = ap->private_data;
649 void __iomem *port_mmio = ahci_port_base(ap);
651 if (policy != ATA_LPM_MAX_POWER) {
653 * Disable interrupts on Phy Ready. This keeps us from
654 * getting woken up due to spurious phy ready
655 * interrupts.
657 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
658 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
660 sata_link_scr_lpm(link, policy, false);
663 if (hpriv->cap & HOST_CAP_ALPM) {
664 u32 cmd = readl(port_mmio + PORT_CMD);
666 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
667 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
668 cmd |= PORT_CMD_ICC_ACTIVE;
670 writel(cmd, port_mmio + PORT_CMD);
671 readl(port_mmio + PORT_CMD);
673 /* wait 10ms to be sure we've come out of LPM state */
674 ata_msleep(ap, 10);
675 } else {
676 cmd |= PORT_CMD_ALPE;
677 if (policy == ATA_LPM_MIN_POWER)
678 cmd |= PORT_CMD_ASP;
680 /* write out new cmd value */
681 writel(cmd, port_mmio + PORT_CMD);
685 if (policy == ATA_LPM_MAX_POWER) {
686 sata_link_scr_lpm(link, policy, false);
688 /* turn PHYRDY IRQ back on */
689 pp->intr_mask |= PORT_IRQ_PHYRDY;
690 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
693 return 0;
696 #ifdef CONFIG_PM
697 static void ahci_power_down(struct ata_port *ap)
699 struct ahci_host_priv *hpriv = ap->host->private_data;
700 void __iomem *port_mmio = ahci_port_base(ap);
701 u32 cmd, scontrol;
703 if (!(hpriv->cap & HOST_CAP_SSS))
704 return;
706 /* put device into listen mode, first set PxSCTL.DET to 0 */
707 scontrol = readl(port_mmio + PORT_SCR_CTL);
708 scontrol &= ~0xf;
709 writel(scontrol, port_mmio + PORT_SCR_CTL);
711 /* then set PxCMD.SUD to 0 */
712 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
713 cmd &= ~PORT_CMD_SPIN_UP;
714 writel(cmd, port_mmio + PORT_CMD);
716 #endif
718 static void ahci_start_port(struct ata_port *ap)
720 struct ahci_port_priv *pp = ap->private_data;
721 struct ata_link *link;
722 struct ahci_em_priv *emp;
723 ssize_t rc;
724 int i;
726 /* enable FIS reception */
727 ahci_start_fis_rx(ap);
729 /* enable DMA */
730 ahci_start_engine(ap);
732 /* turn on LEDs */
733 if (ap->flags & ATA_FLAG_EM) {
734 ata_for_each_link(link, ap, EDGE) {
735 emp = &pp->em_priv[link->pmp];
737 /* EM Transmit bit maybe busy during init */
738 for (i = 0; i < EM_MAX_RETRY; i++) {
739 rc = ahci_transmit_led_message(ap,
740 emp->led_state,
742 if (rc == -EBUSY)
743 ata_msleep(ap, 1);
744 else
745 break;
750 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
751 ata_for_each_link(link, ap, EDGE)
752 ahci_init_sw_activity(link);
756 static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
758 int rc;
760 /* disable DMA */
761 rc = ahci_stop_engine(ap);
762 if (rc) {
763 *emsg = "failed to stop engine";
764 return rc;
767 /* disable FIS reception */
768 rc = ahci_stop_fis_rx(ap);
769 if (rc) {
770 *emsg = "failed stop FIS RX";
771 return rc;
774 return 0;
777 int ahci_reset_controller(struct ata_host *host)
779 struct ahci_host_priv *hpriv = host->private_data;
780 void __iomem *mmio = hpriv->mmio;
781 u32 tmp;
783 /* we must be in AHCI mode, before using anything
784 * AHCI-specific, such as HOST_RESET.
786 ahci_enable_ahci(mmio);
788 /* global controller reset */
789 if (!ahci_skip_host_reset) {
790 tmp = readl(mmio + HOST_CTL);
791 if ((tmp & HOST_RESET) == 0) {
792 writel(tmp | HOST_RESET, mmio + HOST_CTL);
793 readl(mmio + HOST_CTL); /* flush */
797 * to perform host reset, OS should set HOST_RESET
798 * and poll until this bit is read to be "0".
799 * reset must complete within 1 second, or
800 * the hardware should be considered fried.
802 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
803 HOST_RESET, 10, 1000);
805 if (tmp & HOST_RESET) {
806 dev_printk(KERN_ERR, host->dev,
807 "controller reset failed (0x%x)\n", tmp);
808 return -EIO;
811 /* turn on AHCI mode */
812 ahci_enable_ahci(mmio);
814 /* Some registers might be cleared on reset. Restore
815 * initial values.
817 ahci_restore_initial_config(host);
818 } else
819 dev_printk(KERN_INFO, host->dev,
820 "skipping global host reset\n");
822 return 0;
824 EXPORT_SYMBOL_GPL(ahci_reset_controller);
826 static void ahci_sw_activity(struct ata_link *link)
828 struct ata_port *ap = link->ap;
829 struct ahci_port_priv *pp = ap->private_data;
830 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
832 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
833 return;
835 emp->activity++;
836 if (!timer_pending(&emp->timer))
837 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
840 static void ahci_sw_activity_blink(unsigned long arg)
842 struct ata_link *link = (struct ata_link *)arg;
843 struct ata_port *ap = link->ap;
844 struct ahci_port_priv *pp = ap->private_data;
845 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
846 unsigned long led_message = emp->led_state;
847 u32 activity_led_state;
848 unsigned long flags;
850 led_message &= EM_MSG_LED_VALUE;
851 led_message |= ap->port_no | (link->pmp << 8);
853 /* check to see if we've had activity. If so,
854 * toggle state of LED and reset timer. If not,
855 * turn LED to desired idle state.
857 spin_lock_irqsave(ap->lock, flags);
858 if (emp->saved_activity != emp->activity) {
859 emp->saved_activity = emp->activity;
860 /* get the current LED state */
861 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
863 if (activity_led_state)
864 activity_led_state = 0;
865 else
866 activity_led_state = 1;
868 /* clear old state */
869 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
871 /* toggle state */
872 led_message |= (activity_led_state << 16);
873 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
874 } else {
875 /* switch to idle */
876 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
877 if (emp->blink_policy == BLINK_OFF)
878 led_message |= (1 << 16);
880 spin_unlock_irqrestore(ap->lock, flags);
881 ahci_transmit_led_message(ap, led_message, 4);
884 static void ahci_init_sw_activity(struct ata_link *link)
886 struct ata_port *ap = link->ap;
887 struct ahci_port_priv *pp = ap->private_data;
888 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
890 /* init activity stats, setup timer */
891 emp->saved_activity = emp->activity = 0;
892 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
894 /* check our blink policy and set flag for link if it's enabled */
895 if (emp->blink_policy)
896 link->flags |= ATA_LFLAG_SW_ACTIVITY;
899 int ahci_reset_em(struct ata_host *host)
901 struct ahci_host_priv *hpriv = host->private_data;
902 void __iomem *mmio = hpriv->mmio;
903 u32 em_ctl;
905 em_ctl = readl(mmio + HOST_EM_CTL);
906 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
907 return -EINVAL;
909 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
910 return 0;
912 EXPORT_SYMBOL_GPL(ahci_reset_em);
914 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
915 ssize_t size)
917 struct ahci_host_priv *hpriv = ap->host->private_data;
918 struct ahci_port_priv *pp = ap->private_data;
919 void __iomem *mmio = hpriv->mmio;
920 u32 em_ctl;
921 u32 message[] = {0, 0};
922 unsigned long flags;
923 int pmp;
924 struct ahci_em_priv *emp;
926 /* get the slot number from the message */
927 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
928 if (pmp < EM_MAX_SLOTS)
929 emp = &pp->em_priv[pmp];
930 else
931 return -EINVAL;
933 spin_lock_irqsave(ap->lock, flags);
936 * if we are still busy transmitting a previous message,
937 * do not allow
939 em_ctl = readl(mmio + HOST_EM_CTL);
940 if (em_ctl & EM_CTL_TM) {
941 spin_unlock_irqrestore(ap->lock, flags);
942 return -EBUSY;
945 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
947 * create message header - this is all zero except for
948 * the message size, which is 4 bytes.
950 message[0] |= (4 << 8);
952 /* ignore 0:4 of byte zero, fill in port info yourself */
953 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
955 /* write message to EM_LOC */
956 writel(message[0], mmio + hpriv->em_loc);
957 writel(message[1], mmio + hpriv->em_loc+4);
960 * tell hardware to transmit the message
962 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
965 /* save off new led state for port/slot */
966 emp->led_state = state;
968 spin_unlock_irqrestore(ap->lock, flags);
969 return size;
972 static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
974 struct ahci_port_priv *pp = ap->private_data;
975 struct ata_link *link;
976 struct ahci_em_priv *emp;
977 int rc = 0;
979 ata_for_each_link(link, ap, EDGE) {
980 emp = &pp->em_priv[link->pmp];
981 rc += sprintf(buf, "%lx\n", emp->led_state);
983 return rc;
986 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
987 size_t size)
989 int state;
990 int pmp;
991 struct ahci_port_priv *pp = ap->private_data;
992 struct ahci_em_priv *emp;
994 state = simple_strtoul(buf, NULL, 0);
996 /* get the slot number from the message */
997 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
998 if (pmp < EM_MAX_SLOTS)
999 emp = &pp->em_priv[pmp];
1000 else
1001 return -EINVAL;
1003 /* mask off the activity bits if we are in sw_activity
1004 * mode, user should turn off sw_activity before setting
1005 * activity led through em_message
1007 if (emp->blink_policy)
1008 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1010 return ahci_transmit_led_message(ap, state, size);
1013 static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1015 struct ata_link *link = dev->link;
1016 struct ata_port *ap = link->ap;
1017 struct ahci_port_priv *pp = ap->private_data;
1018 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1019 u32 port_led_state = emp->led_state;
1021 /* save the desired Activity LED behavior */
1022 if (val == OFF) {
1023 /* clear LFLAG */
1024 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1026 /* set the LED to OFF */
1027 port_led_state &= EM_MSG_LED_VALUE_OFF;
1028 port_led_state |= (ap->port_no | (link->pmp << 8));
1029 ahci_transmit_led_message(ap, port_led_state, 4);
1030 } else {
1031 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1032 if (val == BLINK_OFF) {
1033 /* set LED to ON for idle */
1034 port_led_state &= EM_MSG_LED_VALUE_OFF;
1035 port_led_state |= (ap->port_no | (link->pmp << 8));
1036 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
1037 ahci_transmit_led_message(ap, port_led_state, 4);
1040 emp->blink_policy = val;
1041 return 0;
1044 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1046 struct ata_link *link = dev->link;
1047 struct ata_port *ap = link->ap;
1048 struct ahci_port_priv *pp = ap->private_data;
1049 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1051 /* display the saved value of activity behavior for this
1052 * disk.
1054 return sprintf(buf, "%d\n", emp->blink_policy);
1057 static void ahci_port_init(struct device *dev, struct ata_port *ap,
1058 int port_no, void __iomem *mmio,
1059 void __iomem *port_mmio)
1061 const char *emsg = NULL;
1062 int rc;
1063 u32 tmp;
1065 /* make sure port is not active */
1066 rc = ahci_deinit_port(ap, &emsg);
1067 if (rc)
1068 dev_warn(dev, "%s (%d)\n", emsg, rc);
1070 /* clear SError */
1071 tmp = readl(port_mmio + PORT_SCR_ERR);
1072 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1073 writel(tmp, port_mmio + PORT_SCR_ERR);
1075 /* clear port IRQ */
1076 tmp = readl(port_mmio + PORT_IRQ_STAT);
1077 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1078 if (tmp)
1079 writel(tmp, port_mmio + PORT_IRQ_STAT);
1081 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1084 void ahci_init_controller(struct ata_host *host)
1086 struct ahci_host_priv *hpriv = host->private_data;
1087 void __iomem *mmio = hpriv->mmio;
1088 int i;
1089 void __iomem *port_mmio;
1090 u32 tmp;
1092 for (i = 0; i < host->n_ports; i++) {
1093 struct ata_port *ap = host->ports[i];
1095 port_mmio = ahci_port_base(ap);
1096 if (ata_port_is_dummy(ap))
1097 continue;
1099 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1102 tmp = readl(mmio + HOST_CTL);
1103 VPRINTK("HOST_CTL 0x%x\n", tmp);
1104 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1105 tmp = readl(mmio + HOST_CTL);
1106 VPRINTK("HOST_CTL 0x%x\n", tmp);
1108 EXPORT_SYMBOL_GPL(ahci_init_controller);
1110 static void ahci_dev_config(struct ata_device *dev)
1112 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1114 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1115 dev->max_sectors = 255;
1116 ata_dev_printk(dev, KERN_INFO,
1117 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1121 static unsigned int ahci_dev_classify(struct ata_port *ap)
1123 void __iomem *port_mmio = ahci_port_base(ap);
1124 struct ata_taskfile tf;
1125 u32 tmp;
1127 tmp = readl(port_mmio + PORT_SIG);
1128 tf.lbah = (tmp >> 24) & 0xff;
1129 tf.lbam = (tmp >> 16) & 0xff;
1130 tf.lbal = (tmp >> 8) & 0xff;
1131 tf.nsect = (tmp) & 0xff;
1133 return ata_dev_classify(&tf);
1136 static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1137 u32 opts)
1139 dma_addr_t cmd_tbl_dma;
1141 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1143 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1144 pp->cmd_slot[tag].status = 0;
1145 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1146 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1149 int ahci_kick_engine(struct ata_port *ap)
1151 void __iomem *port_mmio = ahci_port_base(ap);
1152 struct ahci_host_priv *hpriv = ap->host->private_data;
1153 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1154 u32 tmp;
1155 int busy, rc;
1157 /* stop engine */
1158 rc = ahci_stop_engine(ap);
1159 if (rc)
1160 goto out_restart;
1162 /* need to do CLO?
1163 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1165 busy = status & (ATA_BUSY | ATA_DRQ);
1166 if (!busy && !sata_pmp_attached(ap)) {
1167 rc = 0;
1168 goto out_restart;
1171 if (!(hpriv->cap & HOST_CAP_CLO)) {
1172 rc = -EOPNOTSUPP;
1173 goto out_restart;
1176 /* perform CLO */
1177 tmp = readl(port_mmio + PORT_CMD);
1178 tmp |= PORT_CMD_CLO;
1179 writel(tmp, port_mmio + PORT_CMD);
1181 rc = 0;
1182 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
1183 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1184 if (tmp & PORT_CMD_CLO)
1185 rc = -EIO;
1187 /* restart engine */
1188 out_restart:
1189 ahci_start_engine(ap);
1190 return rc;
1192 EXPORT_SYMBOL_GPL(ahci_kick_engine);
1194 static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1195 struct ata_taskfile *tf, int is_cmd, u16 flags,
1196 unsigned long timeout_msec)
1198 const u32 cmd_fis_len = 5; /* five dwords */
1199 struct ahci_port_priv *pp = ap->private_data;
1200 void __iomem *port_mmio = ahci_port_base(ap);
1201 u8 *fis = pp->cmd_tbl;
1202 u32 tmp;
1204 /* prep the command */
1205 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1206 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1208 /* issue & wait */
1209 writel(1, port_mmio + PORT_CMD_ISSUE);
1211 if (timeout_msec) {
1212 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1213 0x1, 0x1, 1, timeout_msec);
1214 if (tmp & 0x1) {
1215 ahci_kick_engine(ap);
1216 return -EBUSY;
1218 } else
1219 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1221 return 0;
1224 int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1225 int pmp, unsigned long deadline,
1226 int (*check_ready)(struct ata_link *link))
1228 struct ata_port *ap = link->ap;
1229 struct ahci_host_priv *hpriv = ap->host->private_data;
1230 const char *reason = NULL;
1231 unsigned long now, msecs;
1232 struct ata_taskfile tf;
1233 int rc;
1235 DPRINTK("ENTER\n");
1237 /* prepare for SRST (AHCI-1.1 10.4.1) */
1238 rc = ahci_kick_engine(ap);
1239 if (rc && rc != -EOPNOTSUPP)
1240 ata_link_printk(link, KERN_WARNING,
1241 "failed to reset engine (errno=%d)\n", rc);
1243 ata_tf_init(link->device, &tf);
1245 /* issue the first D2H Register FIS */
1246 msecs = 0;
1247 now = jiffies;
1248 if (time_after(deadline, now))
1249 msecs = jiffies_to_msecs(deadline - now);
1251 tf.ctl |= ATA_SRST;
1252 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1253 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1254 rc = -EIO;
1255 reason = "1st FIS failed";
1256 goto fail;
1259 /* spec says at least 5us, but be generous and sleep for 1ms */
1260 ata_msleep(ap, 1);
1262 /* issue the second D2H Register FIS */
1263 tf.ctl &= ~ATA_SRST;
1264 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1266 /* wait for link to become ready */
1267 rc = ata_wait_after_reset(link, deadline, check_ready);
1268 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1270 * Workaround for cases where link online status can't
1271 * be trusted. Treat device readiness timeout as link
1272 * offline.
1274 ata_link_printk(link, KERN_INFO,
1275 "device not ready, treating as offline\n");
1276 *class = ATA_DEV_NONE;
1277 } else if (rc) {
1278 /* link occupied, -ENODEV too is an error */
1279 reason = "device not ready";
1280 goto fail;
1281 } else
1282 *class = ahci_dev_classify(ap);
1284 DPRINTK("EXIT, class=%u\n", *class);
1285 return 0;
1287 fail:
1288 ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
1289 return rc;
1292 int ahci_check_ready(struct ata_link *link)
1294 void __iomem *port_mmio = ahci_port_base(link->ap);
1295 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1297 return ata_check_ready(status);
1299 EXPORT_SYMBOL_GPL(ahci_check_ready);
1301 static int ahci_softreset(struct ata_link *link, unsigned int *class,
1302 unsigned long deadline)
1304 int pmp = sata_srst_pmp(link);
1306 DPRINTK("ENTER\n");
1308 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1310 EXPORT_SYMBOL_GPL(ahci_do_softreset);
1312 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1313 unsigned long deadline)
1315 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1316 struct ata_port *ap = link->ap;
1317 struct ahci_port_priv *pp = ap->private_data;
1318 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1319 struct ata_taskfile tf;
1320 bool online;
1321 int rc;
1323 DPRINTK("ENTER\n");
1325 ahci_stop_engine(ap);
1327 /* clear D2H reception area to properly wait for D2H FIS */
1328 ata_tf_init(link->device, &tf);
1329 tf.command = 0x80;
1330 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1332 rc = sata_link_hardreset(link, timing, deadline, &online,
1333 ahci_check_ready);
1335 ahci_start_engine(ap);
1337 if (online)
1338 *class = ahci_dev_classify(ap);
1340 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1341 return rc;
1344 static void ahci_postreset(struct ata_link *link, unsigned int *class)
1346 struct ata_port *ap = link->ap;
1347 void __iomem *port_mmio = ahci_port_base(ap);
1348 u32 new_tmp, tmp;
1350 ata_std_postreset(link, class);
1352 /* Make sure port's ATAPI bit is set appropriately */
1353 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1354 if (*class == ATA_DEV_ATAPI)
1355 new_tmp |= PORT_CMD_ATAPI;
1356 else
1357 new_tmp &= ~PORT_CMD_ATAPI;
1358 if (new_tmp != tmp) {
1359 writel(new_tmp, port_mmio + PORT_CMD);
1360 readl(port_mmio + PORT_CMD); /* flush */
1364 static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1366 struct scatterlist *sg;
1367 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1368 unsigned int si;
1370 VPRINTK("ENTER\n");
1373 * Next, the S/G list.
1375 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1376 dma_addr_t addr = sg_dma_address(sg);
1377 u32 sg_len = sg_dma_len(sg);
1379 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1380 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1381 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1384 return si;
1387 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1389 struct ata_port *ap = qc->ap;
1390 struct ahci_port_priv *pp = ap->private_data;
1392 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1393 return ata_std_qc_defer(qc);
1394 else
1395 return sata_pmp_qc_defer_cmd_switch(qc);
1398 static void ahci_qc_prep(struct ata_queued_cmd *qc)
1400 struct ata_port *ap = qc->ap;
1401 struct ahci_port_priv *pp = ap->private_data;
1402 int is_atapi = ata_is_atapi(qc->tf.protocol);
1403 void *cmd_tbl;
1404 u32 opts;
1405 const u32 cmd_fis_len = 5; /* five dwords */
1406 unsigned int n_elem;
1409 * Fill in command table information. First, the header,
1410 * a SATA Register - Host to Device command FIS.
1412 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1414 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1415 if (is_atapi) {
1416 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1417 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1420 n_elem = 0;
1421 if (qc->flags & ATA_QCFLAG_DMAMAP)
1422 n_elem = ahci_fill_sg(qc, cmd_tbl);
1425 * Fill in command slot information.
1427 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1428 if (qc->tf.flags & ATA_TFLAG_WRITE)
1429 opts |= AHCI_CMD_WRITE;
1430 if (is_atapi)
1431 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1433 ahci_fill_cmd_slot(pp, qc->tag, opts);
1436 static void ahci_fbs_dec_intr(struct ata_port *ap)
1438 struct ahci_port_priv *pp = ap->private_data;
1439 void __iomem *port_mmio = ahci_port_base(ap);
1440 u32 fbs = readl(port_mmio + PORT_FBS);
1441 int retries = 3;
1443 DPRINTK("ENTER\n");
1444 BUG_ON(!pp->fbs_enabled);
1446 /* time to wait for DEC is not specified by AHCI spec,
1447 * add a retry loop for safety.
1449 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1450 fbs = readl(port_mmio + PORT_FBS);
1451 while ((fbs & PORT_FBS_DEC) && retries--) {
1452 udelay(1);
1453 fbs = readl(port_mmio + PORT_FBS);
1456 if (fbs & PORT_FBS_DEC)
1457 dev_printk(KERN_ERR, ap->host->dev,
1458 "failed to clear device error\n");
1461 static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1463 struct ahci_host_priv *hpriv = ap->host->private_data;
1464 struct ahci_port_priv *pp = ap->private_data;
1465 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1466 struct ata_link *link = NULL;
1467 struct ata_queued_cmd *active_qc;
1468 struct ata_eh_info *active_ehi;
1469 bool fbs_need_dec = false;
1470 u32 serror;
1472 /* determine active link with error */
1473 if (pp->fbs_enabled) {
1474 void __iomem *port_mmio = ahci_port_base(ap);
1475 u32 fbs = readl(port_mmio + PORT_FBS);
1476 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1478 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links) &&
1479 ata_link_online(&ap->pmp_link[pmp])) {
1480 link = &ap->pmp_link[pmp];
1481 fbs_need_dec = true;
1484 } else
1485 ata_for_each_link(link, ap, EDGE)
1486 if (ata_link_active(link))
1487 break;
1489 if (!link)
1490 link = &ap->link;
1492 active_qc = ata_qc_from_tag(ap, link->active_tag);
1493 active_ehi = &link->eh_info;
1495 /* record irq stat */
1496 ata_ehi_clear_desc(host_ehi);
1497 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1499 /* AHCI needs SError cleared; otherwise, it might lock up */
1500 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1501 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1502 host_ehi->serror |= serror;
1504 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1505 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1506 irq_stat &= ~PORT_IRQ_IF_ERR;
1508 if (irq_stat & PORT_IRQ_TF_ERR) {
1509 /* If qc is active, charge it; otherwise, the active
1510 * link. There's no active qc on NCQ errors. It will
1511 * be determined by EH by reading log page 10h.
1513 if (active_qc)
1514 active_qc->err_mask |= AC_ERR_DEV;
1515 else
1516 active_ehi->err_mask |= AC_ERR_DEV;
1518 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1519 host_ehi->serror &= ~SERR_INTERNAL;
1522 if (irq_stat & PORT_IRQ_UNK_FIS) {
1523 u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
1525 active_ehi->err_mask |= AC_ERR_HSM;
1526 active_ehi->action |= ATA_EH_RESET;
1527 ata_ehi_push_desc(active_ehi,
1528 "unknown FIS %08x %08x %08x %08x" ,
1529 unk[0], unk[1], unk[2], unk[3]);
1532 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1533 active_ehi->err_mask |= AC_ERR_HSM;
1534 active_ehi->action |= ATA_EH_RESET;
1535 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1538 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1539 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1540 host_ehi->action |= ATA_EH_RESET;
1541 ata_ehi_push_desc(host_ehi, "host bus error");
1544 if (irq_stat & PORT_IRQ_IF_ERR) {
1545 if (fbs_need_dec)
1546 active_ehi->err_mask |= AC_ERR_DEV;
1547 else {
1548 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1549 host_ehi->action |= ATA_EH_RESET;
1552 ata_ehi_push_desc(host_ehi, "interface fatal error");
1555 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1556 ata_ehi_hotplugged(host_ehi);
1557 ata_ehi_push_desc(host_ehi, "%s",
1558 irq_stat & PORT_IRQ_CONNECT ?
1559 "connection status changed" : "PHY RDY changed");
1562 /* okay, let's hand over to EH */
1564 if (irq_stat & PORT_IRQ_FREEZE)
1565 ata_port_freeze(ap);
1566 else if (fbs_need_dec) {
1567 ata_link_abort(link);
1568 ahci_fbs_dec_intr(ap);
1569 } else
1570 ata_port_abort(ap);
1573 static void ahci_port_intr(struct ata_port *ap)
1575 void __iomem *port_mmio = ahci_port_base(ap);
1576 struct ata_eh_info *ehi = &ap->link.eh_info;
1577 struct ahci_port_priv *pp = ap->private_data;
1578 struct ahci_host_priv *hpriv = ap->host->private_data;
1579 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1580 u32 status, qc_active = 0;
1581 int rc;
1583 status = readl(port_mmio + PORT_IRQ_STAT);
1584 writel(status, port_mmio + PORT_IRQ_STAT);
1586 /* ignore BAD_PMP while resetting */
1587 if (unlikely(resetting))
1588 status &= ~PORT_IRQ_BAD_PMP;
1590 /* if LPM is enabled, PHYRDY doesn't mean anything */
1591 if (ap->link.lpm_policy > ATA_LPM_MAX_POWER) {
1592 status &= ~PORT_IRQ_PHYRDY;
1593 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
1596 if (unlikely(status & PORT_IRQ_ERROR)) {
1597 ahci_error_intr(ap, status);
1598 return;
1601 if (status & PORT_IRQ_SDB_FIS) {
1602 /* If SNotification is available, leave notification
1603 * handling to sata_async_notification(). If not,
1604 * emulate it by snooping SDB FIS RX area.
1606 * Snooping FIS RX area is probably cheaper than
1607 * poking SNotification but some constrollers which
1608 * implement SNotification, ICH9 for example, don't
1609 * store AN SDB FIS into receive area.
1611 if (hpriv->cap & HOST_CAP_SNTF)
1612 sata_async_notification(ap);
1613 else {
1614 /* If the 'N' bit in word 0 of the FIS is set,
1615 * we just received asynchronous notification.
1616 * Tell libata about it.
1618 * Lack of SNotification should not appear in
1619 * ahci 1.2, so the workaround is unnecessary
1620 * when FBS is enabled.
1622 if (pp->fbs_enabled)
1623 WARN_ON_ONCE(1);
1624 else {
1625 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1626 u32 f0 = le32_to_cpu(f[0]);
1627 if (f0 & (1 << 15))
1628 sata_async_notification(ap);
1633 /* pp->active_link is not reliable once FBS is enabled, both
1634 * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1635 * NCQ and non-NCQ commands may be in flight at the same time.
1637 if (pp->fbs_enabled) {
1638 if (ap->qc_active) {
1639 qc_active = readl(port_mmio + PORT_SCR_ACT);
1640 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1642 } else {
1643 /* pp->active_link is valid iff any command is in flight */
1644 if (ap->qc_active && pp->active_link->sactive)
1645 qc_active = readl(port_mmio + PORT_SCR_ACT);
1646 else
1647 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1651 rc = ata_qc_complete_multiple(ap, qc_active);
1653 /* while resetting, invalid completions are expected */
1654 if (unlikely(rc < 0 && !resetting)) {
1655 ehi->err_mask |= AC_ERR_HSM;
1656 ehi->action |= ATA_EH_RESET;
1657 ata_port_freeze(ap);
1661 irqreturn_t ahci_interrupt(int irq, void *dev_instance)
1663 struct ata_host *host = dev_instance;
1664 struct ahci_host_priv *hpriv;
1665 unsigned int i, handled = 0;
1666 void __iomem *mmio;
1667 u32 irq_stat, irq_masked;
1669 VPRINTK("ENTER\n");
1671 hpriv = host->private_data;
1672 mmio = hpriv->mmio;
1674 /* sigh. 0xffffffff is a valid return from h/w */
1675 irq_stat = readl(mmio + HOST_IRQ_STAT);
1676 if (!irq_stat)
1677 return IRQ_NONE;
1679 irq_masked = irq_stat & hpriv->port_map;
1681 spin_lock(&host->lock);
1683 for (i = 0; i < host->n_ports; i++) {
1684 struct ata_port *ap;
1686 if (!(irq_masked & (1 << i)))
1687 continue;
1689 ap = host->ports[i];
1690 if (ap) {
1691 ahci_port_intr(ap);
1692 VPRINTK("port %u\n", i);
1693 } else {
1694 VPRINTK("port %u (no irq)\n", i);
1695 if (ata_ratelimit())
1696 dev_printk(KERN_WARNING, host->dev,
1697 "interrupt on disabled port %u\n", i);
1700 handled = 1;
1703 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
1704 * it should be cleared after all the port events are cleared;
1705 * otherwise, it will raise a spurious interrupt after each
1706 * valid one. Please read section 10.6.2 of ahci 1.1 for more
1707 * information.
1709 * Also, use the unmasked value to clear interrupt as spurious
1710 * pending event on a dummy port might cause screaming IRQ.
1712 writel(irq_stat, mmio + HOST_IRQ_STAT);
1714 spin_unlock(&host->lock);
1716 VPRINTK("EXIT\n");
1718 return IRQ_RETVAL(handled);
1720 EXPORT_SYMBOL_GPL(ahci_interrupt);
1722 static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1724 struct ata_port *ap = qc->ap;
1725 void __iomem *port_mmio = ahci_port_base(ap);
1726 struct ahci_port_priv *pp = ap->private_data;
1728 /* Keep track of the currently active link. It will be used
1729 * in completion path to determine whether NCQ phase is in
1730 * progress.
1732 pp->active_link = qc->dev->link;
1734 if (qc->tf.protocol == ATA_PROT_NCQ)
1735 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1737 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
1738 u32 fbs = readl(port_mmio + PORT_FBS);
1739 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1740 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
1741 writel(fbs, port_mmio + PORT_FBS);
1742 pp->fbs_last_dev = qc->dev->link->pmp;
1745 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
1747 ahci_sw_activity(qc->dev->link);
1749 return 0;
1752 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
1754 struct ahci_port_priv *pp = qc->ap->private_data;
1755 u8 *rx_fis = pp->rx_fis;
1757 if (pp->fbs_enabled)
1758 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
1761 * After a successful execution of an ATA PIO data-in command,
1762 * the device doesn't send D2H Reg FIS to update the TF and
1763 * the host should take TF and E_Status from the preceding PIO
1764 * Setup FIS.
1766 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
1767 !(qc->flags & ATA_QCFLAG_FAILED)) {
1768 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
1769 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
1770 } else
1771 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
1773 return true;
1776 static void ahci_freeze(struct ata_port *ap)
1778 void __iomem *port_mmio = ahci_port_base(ap);
1780 /* turn IRQ off */
1781 writel(0, port_mmio + PORT_IRQ_MASK);
1784 static void ahci_thaw(struct ata_port *ap)
1786 struct ahci_host_priv *hpriv = ap->host->private_data;
1787 void __iomem *mmio = hpriv->mmio;
1788 void __iomem *port_mmio = ahci_port_base(ap);
1789 u32 tmp;
1790 struct ahci_port_priv *pp = ap->private_data;
1792 /* clear IRQ */
1793 tmp = readl(port_mmio + PORT_IRQ_STAT);
1794 writel(tmp, port_mmio + PORT_IRQ_STAT);
1795 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
1797 /* turn IRQ back on */
1798 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1801 static void ahci_error_handler(struct ata_port *ap)
1803 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
1804 /* restart engine */
1805 ahci_stop_engine(ap);
1806 ahci_start_engine(ap);
1809 sata_pmp_error_handler(ap);
1811 if (!ata_dev_enabled(ap->link.device))
1812 ahci_stop_engine(ap);
1815 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
1817 struct ata_port *ap = qc->ap;
1819 /* make DMA engine forget about the failed command */
1820 if (qc->flags & ATA_QCFLAG_FAILED)
1821 ahci_kick_engine(ap);
1824 static void ahci_enable_fbs(struct ata_port *ap)
1826 struct ahci_port_priv *pp = ap->private_data;
1827 void __iomem *port_mmio = ahci_port_base(ap);
1828 u32 fbs;
1829 int rc;
1831 if (!pp->fbs_supported)
1832 return;
1834 fbs = readl(port_mmio + PORT_FBS);
1835 if (fbs & PORT_FBS_EN) {
1836 pp->fbs_enabled = true;
1837 pp->fbs_last_dev = -1; /* initialization */
1838 return;
1841 rc = ahci_stop_engine(ap);
1842 if (rc)
1843 return;
1845 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
1846 fbs = readl(port_mmio + PORT_FBS);
1847 if (fbs & PORT_FBS_EN) {
1848 dev_printk(KERN_INFO, ap->host->dev, "FBS is enabled.\n");
1849 pp->fbs_enabled = true;
1850 pp->fbs_last_dev = -1; /* initialization */
1851 } else
1852 dev_printk(KERN_ERR, ap->host->dev, "Failed to enable FBS\n");
1854 ahci_start_engine(ap);
1857 static void ahci_disable_fbs(struct ata_port *ap)
1859 struct ahci_port_priv *pp = ap->private_data;
1860 void __iomem *port_mmio = ahci_port_base(ap);
1861 u32 fbs;
1862 int rc;
1864 if (!pp->fbs_supported)
1865 return;
1867 fbs = readl(port_mmio + PORT_FBS);
1868 if ((fbs & PORT_FBS_EN) == 0) {
1869 pp->fbs_enabled = false;
1870 return;
1873 rc = ahci_stop_engine(ap);
1874 if (rc)
1875 return;
1877 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
1878 fbs = readl(port_mmio + PORT_FBS);
1879 if (fbs & PORT_FBS_EN)
1880 dev_printk(KERN_ERR, ap->host->dev, "Failed to disable FBS\n");
1881 else {
1882 dev_printk(KERN_INFO, ap->host->dev, "FBS is disabled.\n");
1883 pp->fbs_enabled = false;
1886 ahci_start_engine(ap);
1889 static void ahci_pmp_attach(struct ata_port *ap)
1891 void __iomem *port_mmio = ahci_port_base(ap);
1892 struct ahci_port_priv *pp = ap->private_data;
1893 u32 cmd;
1895 cmd = readl(port_mmio + PORT_CMD);
1896 cmd |= PORT_CMD_PMP;
1897 writel(cmd, port_mmio + PORT_CMD);
1899 ahci_enable_fbs(ap);
1901 pp->intr_mask |= PORT_IRQ_BAD_PMP;
1902 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1905 static void ahci_pmp_detach(struct ata_port *ap)
1907 void __iomem *port_mmio = ahci_port_base(ap);
1908 struct ahci_port_priv *pp = ap->private_data;
1909 u32 cmd;
1911 ahci_disable_fbs(ap);
1913 cmd = readl(port_mmio + PORT_CMD);
1914 cmd &= ~PORT_CMD_PMP;
1915 writel(cmd, port_mmio + PORT_CMD);
1917 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
1918 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1921 static int ahci_port_resume(struct ata_port *ap)
1923 ahci_power_up(ap);
1924 ahci_start_port(ap);
1926 if (sata_pmp_attached(ap))
1927 ahci_pmp_attach(ap);
1928 else
1929 ahci_pmp_detach(ap);
1931 return 0;
1934 #ifdef CONFIG_PM
1935 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
1937 const char *emsg = NULL;
1938 int rc;
1940 rc = ahci_deinit_port(ap, &emsg);
1941 if (rc == 0)
1942 ahci_power_down(ap);
1943 else {
1944 ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
1945 ahci_start_port(ap);
1948 return rc;
1950 #endif
1952 static int ahci_port_start(struct ata_port *ap)
1954 struct ahci_host_priv *hpriv = ap->host->private_data;
1955 struct device *dev = ap->host->dev;
1956 struct ahci_port_priv *pp;
1957 void *mem;
1958 dma_addr_t mem_dma;
1959 size_t dma_sz, rx_fis_sz;
1961 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1962 if (!pp)
1963 return -ENOMEM;
1965 /* check FBS capability */
1966 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
1967 void __iomem *port_mmio = ahci_port_base(ap);
1968 u32 cmd = readl(port_mmio + PORT_CMD);
1969 if (cmd & PORT_CMD_FBSCP)
1970 pp->fbs_supported = true;
1971 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
1972 dev_printk(KERN_INFO, dev,
1973 "port %d can do FBS, forcing FBSCP\n",
1974 ap->port_no);
1975 pp->fbs_supported = true;
1976 } else
1977 dev_printk(KERN_WARNING, dev,
1978 "port %d is not capable of FBS\n",
1979 ap->port_no);
1982 if (pp->fbs_supported) {
1983 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
1984 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
1985 } else {
1986 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
1987 rx_fis_sz = AHCI_RX_FIS_SZ;
1990 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
1991 if (!mem)
1992 return -ENOMEM;
1993 memset(mem, 0, dma_sz);
1996 * First item in chunk of DMA memory: 32-slot command table,
1997 * 32 bytes each in size
1999 pp->cmd_slot = mem;
2000 pp->cmd_slot_dma = mem_dma;
2002 mem += AHCI_CMD_SLOT_SZ;
2003 mem_dma += AHCI_CMD_SLOT_SZ;
2006 * Second item: Received-FIS area
2008 pp->rx_fis = mem;
2009 pp->rx_fis_dma = mem_dma;
2011 mem += rx_fis_sz;
2012 mem_dma += rx_fis_sz;
2015 * Third item: data area for storing a single command
2016 * and its scatter-gather table
2018 pp->cmd_tbl = mem;
2019 pp->cmd_tbl_dma = mem_dma;
2022 * Save off initial list of interrupts to be enabled.
2023 * This could be changed later
2025 pp->intr_mask = DEF_PORT_IRQ;
2027 ap->private_data = pp;
2029 /* engage engines, captain */
2030 return ahci_port_resume(ap);
2033 static void ahci_port_stop(struct ata_port *ap)
2035 const char *emsg = NULL;
2036 int rc;
2038 /* de-initialize port */
2039 rc = ahci_deinit_port(ap, &emsg);
2040 if (rc)
2041 ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
2044 void ahci_print_info(struct ata_host *host, const char *scc_s)
2046 struct ahci_host_priv *hpriv = host->private_data;
2047 void __iomem *mmio = hpriv->mmio;
2048 u32 vers, cap, cap2, impl, speed;
2049 const char *speed_s;
2051 vers = readl(mmio + HOST_VERSION);
2052 cap = hpriv->cap;
2053 cap2 = hpriv->cap2;
2054 impl = hpriv->port_map;
2056 speed = (cap >> 20) & 0xf;
2057 if (speed == 1)
2058 speed_s = "1.5";
2059 else if (speed == 2)
2060 speed_s = "3";
2061 else if (speed == 3)
2062 speed_s = "6";
2063 else
2064 speed_s = "?";
2066 dev_info(host->dev,
2067 "AHCI %02x%02x.%02x%02x "
2068 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2071 (vers >> 24) & 0xff,
2072 (vers >> 16) & 0xff,
2073 (vers >> 8) & 0xff,
2074 vers & 0xff,
2076 ((cap >> 8) & 0x1f) + 1,
2077 (cap & 0x1f) + 1,
2078 speed_s,
2079 impl,
2080 scc_s);
2082 dev_info(host->dev,
2083 "flags: "
2084 "%s%s%s%s%s%s%s"
2085 "%s%s%s%s%s%s%s"
2086 "%s%s%s%s%s%s\n"
2089 cap & HOST_CAP_64 ? "64bit " : "",
2090 cap & HOST_CAP_NCQ ? "ncq " : "",
2091 cap & HOST_CAP_SNTF ? "sntf " : "",
2092 cap & HOST_CAP_MPS ? "ilck " : "",
2093 cap & HOST_CAP_SSS ? "stag " : "",
2094 cap & HOST_CAP_ALPM ? "pm " : "",
2095 cap & HOST_CAP_LED ? "led " : "",
2096 cap & HOST_CAP_CLO ? "clo " : "",
2097 cap & HOST_CAP_ONLY ? "only " : "",
2098 cap & HOST_CAP_PMP ? "pmp " : "",
2099 cap & HOST_CAP_FBS ? "fbs " : "",
2100 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2101 cap & HOST_CAP_SSC ? "slum " : "",
2102 cap & HOST_CAP_PART ? "part " : "",
2103 cap & HOST_CAP_CCC ? "ccc " : "",
2104 cap & HOST_CAP_EMS ? "ems " : "",
2105 cap & HOST_CAP_SXS ? "sxs " : "",
2106 cap2 & HOST_CAP2_APST ? "apst " : "",
2107 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2108 cap2 & HOST_CAP2_BOH ? "boh " : ""
2111 EXPORT_SYMBOL_GPL(ahci_print_info);
2113 void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2114 struct ata_port_info *pi)
2116 u8 messages;
2117 void __iomem *mmio = hpriv->mmio;
2118 u32 em_loc = readl(mmio + HOST_EM_LOC);
2119 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2121 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2122 return;
2124 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2126 if (messages) {
2127 /* store em_loc */
2128 hpriv->em_loc = ((em_loc >> 16) * 4);
2129 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
2130 hpriv->em_msg_type = messages;
2131 pi->flags |= ATA_FLAG_EM;
2132 if (!(em_ctl & EM_CTL_ALHD))
2133 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2136 EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2138 MODULE_AUTHOR("Jeff Garzik");
2139 MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2140 MODULE_LICENSE("GPL");