0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include <linux/kernel.h>
0020 #include <linux/gfp.h>
0021 #include <linux/module.h>
0022 #include <linux/nospec.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/delay.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/dma-mapping.h>
0027 #include <linux/device.h>
0028 #include <scsi/scsi_host.h>
0029 #include <scsi/scsi_cmnd.h>
0030 #include <linux/libata.h>
0031 #include <linux/pci.h>
0032 #include "ahci.h"
0033 #include "libata.h"
0034
0035 static int ahci_skip_host_reset;
0036 int ahci_ignore_sss;
0037 EXPORT_SYMBOL_GPL(ahci_ignore_sss);
0038
0039 module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
0040 MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
0041
0042 module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
0043 MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
0044
0045 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
0046 unsigned hints);
0047 static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
0048 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
0049 size_t size);
0050 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
0051 ssize_t size);
0052
0053
0054
0055 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
0056 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
0057 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
0058 static int ahci_port_start(struct ata_port *ap);
0059 static void ahci_port_stop(struct ata_port *ap);
0060 static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc);
0061 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
0062 static void ahci_freeze(struct ata_port *ap);
0063 static void ahci_thaw(struct ata_port *ap);
0064 static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
0065 static void ahci_enable_fbs(struct ata_port *ap);
0066 static void ahci_disable_fbs(struct ata_port *ap);
0067 static void ahci_pmp_attach(struct ata_port *ap);
0068 static void ahci_pmp_detach(struct ata_port *ap);
0069 static int ahci_softreset(struct ata_link *link, unsigned int *class,
0070 unsigned long deadline);
0071 static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
0072 unsigned long deadline);
0073 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
0074 unsigned long deadline);
0075 static void ahci_postreset(struct ata_link *link, unsigned int *class);
0076 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
0077 static void ahci_dev_config(struct ata_device *dev);
0078 #ifdef CONFIG_PM
0079 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
0080 #endif
0081 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
0082 static ssize_t ahci_activity_store(struct ata_device *dev,
0083 enum sw_activity val);
0084 static void ahci_init_sw_activity(struct ata_link *link);
0085
0086 static ssize_t ahci_show_host_caps(struct device *dev,
0087 struct device_attribute *attr, char *buf);
0088 static ssize_t ahci_show_host_cap2(struct device *dev,
0089 struct device_attribute *attr, char *buf);
0090 static ssize_t ahci_show_host_version(struct device *dev,
0091 struct device_attribute *attr, char *buf);
0092 static ssize_t ahci_show_port_cmd(struct device *dev,
0093 struct device_attribute *attr, char *buf);
0094 static ssize_t ahci_read_em_buffer(struct device *dev,
0095 struct device_attribute *attr, char *buf);
0096 static ssize_t ahci_store_em_buffer(struct device *dev,
0097 struct device_attribute *attr,
0098 const char *buf, size_t size);
0099 static ssize_t ahci_show_em_supported(struct device *dev,
0100 struct device_attribute *attr, char *buf);
0101 static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
0102
0103 static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
0104 static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
0105 static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
0106 static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
0107 static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
0108 ahci_read_em_buffer, ahci_store_em_buffer);
0109 static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
0110
0111 static struct attribute *ahci_shost_attrs[] = {
0112 &dev_attr_link_power_management_policy.attr,
0113 &dev_attr_em_message_type.attr,
0114 &dev_attr_em_message.attr,
0115 &dev_attr_ahci_host_caps.attr,
0116 &dev_attr_ahci_host_cap2.attr,
0117 &dev_attr_ahci_host_version.attr,
0118 &dev_attr_ahci_port_cmd.attr,
0119 &dev_attr_em_buffer.attr,
0120 &dev_attr_em_message_supported.attr,
0121 NULL
0122 };
0123
0124 static const struct attribute_group ahci_shost_attr_group = {
0125 .attrs = ahci_shost_attrs
0126 };
0127
0128 const struct attribute_group *ahci_shost_groups[] = {
0129 &ahci_shost_attr_group,
0130 NULL
0131 };
0132 EXPORT_SYMBOL_GPL(ahci_shost_groups);
0133
0134 static struct attribute *ahci_sdev_attrs[] = {
0135 &dev_attr_sw_activity.attr,
0136 &dev_attr_unload_heads.attr,
0137 &dev_attr_ncq_prio_supported.attr,
0138 &dev_attr_ncq_prio_enable.attr,
0139 NULL
0140 };
0141
0142 static const struct attribute_group ahci_sdev_attr_group = {
0143 .attrs = ahci_sdev_attrs
0144 };
0145
0146 const struct attribute_group *ahci_sdev_groups[] = {
0147 &ahci_sdev_attr_group,
0148 NULL
0149 };
0150 EXPORT_SYMBOL_GPL(ahci_sdev_groups);
0151
0152 struct ata_port_operations ahci_ops = {
0153 .inherits = &sata_pmp_port_ops,
0154
0155 .qc_defer = ahci_pmp_qc_defer,
0156 .qc_prep = ahci_qc_prep,
0157 .qc_issue = ahci_qc_issue,
0158 .qc_fill_rtf = ahci_qc_fill_rtf,
0159
0160 .freeze = ahci_freeze,
0161 .thaw = ahci_thaw,
0162 .softreset = ahci_softreset,
0163 .hardreset = ahci_hardreset,
0164 .postreset = ahci_postreset,
0165 .pmp_softreset = ahci_softreset,
0166 .error_handler = ahci_error_handler,
0167 .post_internal_cmd = ahci_post_internal_cmd,
0168 .dev_config = ahci_dev_config,
0169
0170 .scr_read = ahci_scr_read,
0171 .scr_write = ahci_scr_write,
0172 .pmp_attach = ahci_pmp_attach,
0173 .pmp_detach = ahci_pmp_detach,
0174
0175 .set_lpm = ahci_set_lpm,
0176 .em_show = ahci_led_show,
0177 .em_store = ahci_led_store,
0178 .sw_activity_show = ahci_activity_show,
0179 .sw_activity_store = ahci_activity_store,
0180 .transmit_led_message = ahci_transmit_led_message,
0181 #ifdef CONFIG_PM
0182 .port_suspend = ahci_port_suspend,
0183 .port_resume = ahci_port_resume,
0184 #endif
0185 .port_start = ahci_port_start,
0186 .port_stop = ahci_port_stop,
0187 };
0188 EXPORT_SYMBOL_GPL(ahci_ops);
0189
0190 struct ata_port_operations ahci_pmp_retry_srst_ops = {
0191 .inherits = &ahci_ops,
0192 .softreset = ahci_pmp_retry_softreset,
0193 };
0194 EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
0195
0196 static bool ahci_em_messages __read_mostly = true;
0197 module_param(ahci_em_messages, bool, 0444);
0198
0199 MODULE_PARM_DESC(ahci_em_messages,
0200 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
0201
0202
0203 static int devslp_idle_timeout __read_mostly = 1000;
0204 module_param(devslp_idle_timeout, int, 0644);
0205 MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
0206
0207 static void ahci_enable_ahci(void __iomem *mmio)
0208 {
0209 int i;
0210 u32 tmp;
0211
0212
0213 tmp = readl(mmio + HOST_CTL);
0214 if (tmp & HOST_AHCI_EN)
0215 return;
0216
0217
0218
0219
0220 for (i = 0; i < 5; i++) {
0221 tmp |= HOST_AHCI_EN;
0222 writel(tmp, mmio + HOST_CTL);
0223 tmp = readl(mmio + HOST_CTL);
0224 if (tmp & HOST_AHCI_EN)
0225 return;
0226 msleep(10);
0227 }
0228
0229 WARN_ON(1);
0230 }
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 static int ahci_rpm_get_port(struct ata_port *ap)
0241 {
0242 return pm_runtime_get_sync(ap->dev);
0243 }
0244
0245
0246
0247
0248
0249
0250
0251
0252 static void ahci_rpm_put_port(struct ata_port *ap)
0253 {
0254 pm_runtime_put(ap->dev);
0255 }
0256
0257 static ssize_t ahci_show_host_caps(struct device *dev,
0258 struct device_attribute *attr, char *buf)
0259 {
0260 struct Scsi_Host *shost = class_to_shost(dev);
0261 struct ata_port *ap = ata_shost_to_port(shost);
0262 struct ahci_host_priv *hpriv = ap->host->private_data;
0263
0264 return sprintf(buf, "%x\n", hpriv->cap);
0265 }
0266
0267 static ssize_t ahci_show_host_cap2(struct device *dev,
0268 struct device_attribute *attr, char *buf)
0269 {
0270 struct Scsi_Host *shost = class_to_shost(dev);
0271 struct ata_port *ap = ata_shost_to_port(shost);
0272 struct ahci_host_priv *hpriv = ap->host->private_data;
0273
0274 return sprintf(buf, "%x\n", hpriv->cap2);
0275 }
0276
0277 static ssize_t ahci_show_host_version(struct device *dev,
0278 struct device_attribute *attr, char *buf)
0279 {
0280 struct Scsi_Host *shost = class_to_shost(dev);
0281 struct ata_port *ap = ata_shost_to_port(shost);
0282 struct ahci_host_priv *hpriv = ap->host->private_data;
0283
0284 return sprintf(buf, "%x\n", hpriv->version);
0285 }
0286
0287 static ssize_t ahci_show_port_cmd(struct device *dev,
0288 struct device_attribute *attr, char *buf)
0289 {
0290 struct Scsi_Host *shost = class_to_shost(dev);
0291 struct ata_port *ap = ata_shost_to_port(shost);
0292 void __iomem *port_mmio = ahci_port_base(ap);
0293 ssize_t ret;
0294
0295 ahci_rpm_get_port(ap);
0296 ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
0297 ahci_rpm_put_port(ap);
0298
0299 return ret;
0300 }
0301
0302 static ssize_t ahci_read_em_buffer(struct device *dev,
0303 struct device_attribute *attr, char *buf)
0304 {
0305 struct Scsi_Host *shost = class_to_shost(dev);
0306 struct ata_port *ap = ata_shost_to_port(shost);
0307 struct ahci_host_priv *hpriv = ap->host->private_data;
0308 void __iomem *mmio = hpriv->mmio;
0309 void __iomem *em_mmio = mmio + hpriv->em_loc;
0310 u32 em_ctl, msg;
0311 unsigned long flags;
0312 size_t count;
0313 int i;
0314
0315 ahci_rpm_get_port(ap);
0316 spin_lock_irqsave(ap->lock, flags);
0317
0318 em_ctl = readl(mmio + HOST_EM_CTL);
0319 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
0320 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
0321 spin_unlock_irqrestore(ap->lock, flags);
0322 ahci_rpm_put_port(ap);
0323 return -EINVAL;
0324 }
0325
0326 if (!(em_ctl & EM_CTL_MR)) {
0327 spin_unlock_irqrestore(ap->lock, flags);
0328 ahci_rpm_put_port(ap);
0329 return -EAGAIN;
0330 }
0331
0332 if (!(em_ctl & EM_CTL_SMB))
0333 em_mmio += hpriv->em_buf_sz;
0334
0335 count = hpriv->em_buf_sz;
0336
0337
0338 if (count > PAGE_SIZE) {
0339 if (printk_ratelimit())
0340 ata_port_warn(ap,
0341 "EM read buffer size too large: "
0342 "buffer size %u, page size %lu\n",
0343 hpriv->em_buf_sz, PAGE_SIZE);
0344 count = PAGE_SIZE;
0345 }
0346
0347 for (i = 0; i < count; i += 4) {
0348 msg = readl(em_mmio + i);
0349 buf[i] = msg & 0xff;
0350 buf[i + 1] = (msg >> 8) & 0xff;
0351 buf[i + 2] = (msg >> 16) & 0xff;
0352 buf[i + 3] = (msg >> 24) & 0xff;
0353 }
0354
0355 spin_unlock_irqrestore(ap->lock, flags);
0356 ahci_rpm_put_port(ap);
0357
0358 return i;
0359 }
0360
0361 static ssize_t ahci_store_em_buffer(struct device *dev,
0362 struct device_attribute *attr,
0363 const char *buf, size_t size)
0364 {
0365 struct Scsi_Host *shost = class_to_shost(dev);
0366 struct ata_port *ap = ata_shost_to_port(shost);
0367 struct ahci_host_priv *hpriv = ap->host->private_data;
0368 void __iomem *mmio = hpriv->mmio;
0369 void __iomem *em_mmio = mmio + hpriv->em_loc;
0370 const unsigned char *msg_buf = buf;
0371 u32 em_ctl, msg;
0372 unsigned long flags;
0373 int i;
0374
0375
0376 if (!(ap->flags & ATA_FLAG_EM) ||
0377 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
0378 size % 4 || size > hpriv->em_buf_sz)
0379 return -EINVAL;
0380
0381 ahci_rpm_get_port(ap);
0382 spin_lock_irqsave(ap->lock, flags);
0383
0384 em_ctl = readl(mmio + HOST_EM_CTL);
0385 if (em_ctl & EM_CTL_TM) {
0386 spin_unlock_irqrestore(ap->lock, flags);
0387 ahci_rpm_put_port(ap);
0388 return -EBUSY;
0389 }
0390
0391 for (i = 0; i < size; i += 4) {
0392 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
0393 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
0394 writel(msg, em_mmio + i);
0395 }
0396
0397 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
0398
0399 spin_unlock_irqrestore(ap->lock, flags);
0400 ahci_rpm_put_port(ap);
0401
0402 return size;
0403 }
0404
0405 static ssize_t ahci_show_em_supported(struct device *dev,
0406 struct device_attribute *attr, char *buf)
0407 {
0408 struct Scsi_Host *shost = class_to_shost(dev);
0409 struct ata_port *ap = ata_shost_to_port(shost);
0410 struct ahci_host_priv *hpriv = ap->host->private_data;
0411 void __iomem *mmio = hpriv->mmio;
0412 u32 em_ctl;
0413
0414 ahci_rpm_get_port(ap);
0415 em_ctl = readl(mmio + HOST_EM_CTL);
0416 ahci_rpm_put_port(ap);
0417
0418 return sprintf(buf, "%s%s%s%s\n",
0419 em_ctl & EM_CTL_LED ? "led " : "",
0420 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
0421 em_ctl & EM_CTL_SES ? "ses-2 " : "",
0422 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
0423 }
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443 void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
0444 {
0445 void __iomem *mmio = hpriv->mmio;
0446 u32 cap, cap2, vers, port_map;
0447 int i;
0448
0449
0450 ahci_enable_ahci(mmio);
0451
0452
0453
0454
0455 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
0456 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
0457
0458
0459 vers = readl(mmio + HOST_VERSION);
0460 if ((vers >> 16) > 1 ||
0461 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
0462 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
0463 else
0464 hpriv->saved_cap2 = cap2 = 0;
0465
0466
0467 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
0468 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
0469 cap &= ~HOST_CAP_64;
0470 }
0471
0472 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
0473 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
0474 cap &= ~HOST_CAP_NCQ;
0475 }
0476
0477 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
0478 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
0479 cap |= HOST_CAP_NCQ;
0480 }
0481
0482 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
0483 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
0484 cap &= ~HOST_CAP_PMP;
0485 }
0486
0487 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
0488 dev_info(dev,
0489 "controller can't do SNTF, turning off CAP_SNTF\n");
0490 cap &= ~HOST_CAP_SNTF;
0491 }
0492
0493 if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
0494 dev_info(dev,
0495 "controller can't do DEVSLP, turning off\n");
0496 cap2 &= ~HOST_CAP2_SDS;
0497 cap2 &= ~HOST_CAP2_SADM;
0498 }
0499
0500 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
0501 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
0502 cap |= HOST_CAP_FBS;
0503 }
0504
0505 if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
0506 dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
0507 cap &= ~HOST_CAP_FBS;
0508 }
0509
0510 if (!(cap & HOST_CAP_ALPM) && (hpriv->flags & AHCI_HFLAG_YES_ALPM)) {
0511 dev_info(dev, "controller can do ALPM, turning on CAP_ALPM\n");
0512 cap |= HOST_CAP_ALPM;
0513 }
0514
0515 if ((cap & HOST_CAP_SXS) && (hpriv->flags & AHCI_HFLAG_NO_SXS)) {
0516 dev_info(dev, "controller does not support SXS, disabling CAP_SXS\n");
0517 cap &= ~HOST_CAP_SXS;
0518 }
0519
0520 if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
0521 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
0522 port_map, hpriv->force_port_map);
0523 port_map = hpriv->force_port_map;
0524 hpriv->saved_port_map = port_map;
0525 }
0526
0527 if (hpriv->mask_port_map) {
0528 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
0529 port_map,
0530 port_map & hpriv->mask_port_map);
0531 port_map &= hpriv->mask_port_map;
0532 }
0533
0534
0535 if (port_map) {
0536 int map_ports = 0;
0537
0538 for (i = 0; i < AHCI_MAX_PORTS; i++)
0539 if (port_map & (1 << i))
0540 map_ports++;
0541
0542
0543
0544
0545 if (map_ports > ahci_nr_ports(cap)) {
0546 dev_warn(dev,
0547 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
0548 port_map, ahci_nr_ports(cap));
0549 port_map = 0;
0550 }
0551 }
0552
0553
0554 if (!port_map && vers < 0x10300) {
0555 port_map = (1 << ahci_nr_ports(cap)) - 1;
0556 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
0557
0558
0559 hpriv->saved_port_map = port_map;
0560 }
0561
0562
0563 hpriv->cap = cap;
0564 hpriv->cap2 = cap2;
0565 hpriv->version = readl(mmio + HOST_VERSION);
0566 hpriv->port_map = port_map;
0567
0568 if (!hpriv->start_engine)
0569 hpriv->start_engine = ahci_start_engine;
0570
0571 if (!hpriv->stop_engine)
0572 hpriv->stop_engine = ahci_stop_engine;
0573
0574 if (!hpriv->irq_handler)
0575 hpriv->irq_handler = ahci_single_level_irq_intr;
0576 }
0577 EXPORT_SYMBOL_GPL(ahci_save_initial_config);
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588 static void ahci_restore_initial_config(struct ata_host *host)
0589 {
0590 struct ahci_host_priv *hpriv = host->private_data;
0591 void __iomem *mmio = hpriv->mmio;
0592
0593 writel(hpriv->saved_cap, mmio + HOST_CAP);
0594 if (hpriv->saved_cap2)
0595 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
0596 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
0597 (void) readl(mmio + HOST_PORTS_IMPL);
0598 }
0599
0600 static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
0601 {
0602 static const int offset[] = {
0603 [SCR_STATUS] = PORT_SCR_STAT,
0604 [SCR_CONTROL] = PORT_SCR_CTL,
0605 [SCR_ERROR] = PORT_SCR_ERR,
0606 [SCR_ACTIVE] = PORT_SCR_ACT,
0607 [SCR_NOTIFICATION] = PORT_SCR_NTF,
0608 };
0609 struct ahci_host_priv *hpriv = ap->host->private_data;
0610
0611 if (sc_reg < ARRAY_SIZE(offset) &&
0612 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
0613 return offset[sc_reg];
0614 return 0;
0615 }
0616
0617 static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
0618 {
0619 void __iomem *port_mmio = ahci_port_base(link->ap);
0620 int offset = ahci_scr_offset(link->ap, sc_reg);
0621
0622 if (offset) {
0623 *val = readl(port_mmio + offset);
0624 return 0;
0625 }
0626 return -EINVAL;
0627 }
0628
0629 static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
0630 {
0631 void __iomem *port_mmio = ahci_port_base(link->ap);
0632 int offset = ahci_scr_offset(link->ap, sc_reg);
0633
0634 if (offset) {
0635 writel(val, port_mmio + offset);
0636 return 0;
0637 }
0638 return -EINVAL;
0639 }
0640
0641 void ahci_start_engine(struct ata_port *ap)
0642 {
0643 void __iomem *port_mmio = ahci_port_base(ap);
0644 u32 tmp;
0645
0646
0647 tmp = readl(port_mmio + PORT_CMD);
0648 tmp |= PORT_CMD_START;
0649 writel(tmp, port_mmio + PORT_CMD);
0650 readl(port_mmio + PORT_CMD);
0651 }
0652 EXPORT_SYMBOL_GPL(ahci_start_engine);
0653
0654 int ahci_stop_engine(struct ata_port *ap)
0655 {
0656 void __iomem *port_mmio = ahci_port_base(ap);
0657 struct ahci_host_priv *hpriv = ap->host->private_data;
0658 u32 tmp;
0659
0660
0661
0662
0663
0664
0665
0666 if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
0667 (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
0668 ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
0669 dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
0670 return -EIO;
0671 }
0672
0673 tmp = readl(port_mmio + PORT_CMD);
0674
0675
0676 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
0677 return 0;
0678
0679
0680
0681
0682
0683
0684 if (tmp == 0xffffffff) {
0685 dev_err(ap->host->dev, "AHCI controller unavailable!\n");
0686 return -ENODEV;
0687 }
0688
0689
0690 tmp &= ~PORT_CMD_START;
0691 writel(tmp, port_mmio + PORT_CMD);
0692
0693
0694 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
0695 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
0696 if (tmp & PORT_CMD_LIST_ON)
0697 return -EIO;
0698
0699 return 0;
0700 }
0701 EXPORT_SYMBOL_GPL(ahci_stop_engine);
0702
0703 void ahci_start_fis_rx(struct ata_port *ap)
0704 {
0705 void __iomem *port_mmio = ahci_port_base(ap);
0706 struct ahci_host_priv *hpriv = ap->host->private_data;
0707 struct ahci_port_priv *pp = ap->private_data;
0708 u32 tmp;
0709
0710
0711 if (hpriv->cap & HOST_CAP_64)
0712 writel((pp->cmd_slot_dma >> 16) >> 16,
0713 port_mmio + PORT_LST_ADDR_HI);
0714 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
0715
0716 if (hpriv->cap & HOST_CAP_64)
0717 writel((pp->rx_fis_dma >> 16) >> 16,
0718 port_mmio + PORT_FIS_ADDR_HI);
0719 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
0720
0721
0722 tmp = readl(port_mmio + PORT_CMD);
0723 tmp |= PORT_CMD_FIS_RX;
0724 writel(tmp, port_mmio + PORT_CMD);
0725
0726
0727 readl(port_mmio + PORT_CMD);
0728 }
0729 EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
0730
0731 static int ahci_stop_fis_rx(struct ata_port *ap)
0732 {
0733 void __iomem *port_mmio = ahci_port_base(ap);
0734 u32 tmp;
0735
0736
0737 tmp = readl(port_mmio + PORT_CMD);
0738 tmp &= ~PORT_CMD_FIS_RX;
0739 writel(tmp, port_mmio + PORT_CMD);
0740
0741
0742 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
0743 PORT_CMD_FIS_ON, 10, 1000);
0744 if (tmp & PORT_CMD_FIS_ON)
0745 return -EBUSY;
0746
0747 return 0;
0748 }
0749
0750 static void ahci_power_up(struct ata_port *ap)
0751 {
0752 struct ahci_host_priv *hpriv = ap->host->private_data;
0753 void __iomem *port_mmio = ahci_port_base(ap);
0754 u32 cmd;
0755
0756 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
0757
0758
0759 if (hpriv->cap & HOST_CAP_SSS) {
0760 cmd |= PORT_CMD_SPIN_UP;
0761 writel(cmd, port_mmio + PORT_CMD);
0762 }
0763
0764
0765 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
0766 }
0767
0768 static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
0769 unsigned int hints)
0770 {
0771 struct ata_port *ap = link->ap;
0772 struct ahci_host_priv *hpriv = ap->host->private_data;
0773 struct ahci_port_priv *pp = ap->private_data;
0774 void __iomem *port_mmio = ahci_port_base(ap);
0775
0776 if (policy != ATA_LPM_MAX_POWER) {
0777
0778 hints &= ~ATA_LPM_WAKE_ONLY;
0779
0780
0781
0782
0783
0784
0785 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
0786 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
0787
0788 sata_link_scr_lpm(link, policy, false);
0789 }
0790
0791 if (hpriv->cap & HOST_CAP_ALPM) {
0792 u32 cmd = readl(port_mmio + PORT_CMD);
0793
0794 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
0795 if (!(hints & ATA_LPM_WAKE_ONLY))
0796 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
0797 cmd |= PORT_CMD_ICC_ACTIVE;
0798
0799 writel(cmd, port_mmio + PORT_CMD);
0800 readl(port_mmio + PORT_CMD);
0801
0802
0803 ata_msleep(ap, 10);
0804
0805 if (hints & ATA_LPM_WAKE_ONLY)
0806 return 0;
0807 } else {
0808 cmd |= PORT_CMD_ALPE;
0809 if (policy == ATA_LPM_MIN_POWER)
0810 cmd |= PORT_CMD_ASP;
0811 else if (policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
0812 cmd &= ~PORT_CMD_ASP;
0813
0814
0815 writel(cmd, port_mmio + PORT_CMD);
0816 }
0817 }
0818
0819
0820 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
0821 (hpriv->cap2 & HOST_CAP2_SADM) &&
0822 (link->device->flags & ATA_DFLAG_DEVSLP)) {
0823 if (policy == ATA_LPM_MIN_POWER ||
0824 policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
0825 ahci_set_aggressive_devslp(ap, true);
0826 else
0827 ahci_set_aggressive_devslp(ap, false);
0828 }
0829
0830 if (policy == ATA_LPM_MAX_POWER) {
0831 sata_link_scr_lpm(link, policy, false);
0832
0833
0834 pp->intr_mask |= PORT_IRQ_PHYRDY;
0835 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
0836 }
0837
0838 return 0;
0839 }
0840
0841 #ifdef CONFIG_PM
0842 static void ahci_power_down(struct ata_port *ap)
0843 {
0844 struct ahci_host_priv *hpriv = ap->host->private_data;
0845 void __iomem *port_mmio = ahci_port_base(ap);
0846 u32 cmd, scontrol;
0847
0848 if (!(hpriv->cap & HOST_CAP_SSS))
0849 return;
0850
0851
0852 scontrol = readl(port_mmio + PORT_SCR_CTL);
0853 scontrol &= ~0xf;
0854 writel(scontrol, port_mmio + PORT_SCR_CTL);
0855
0856
0857 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
0858 cmd &= ~PORT_CMD_SPIN_UP;
0859 writel(cmd, port_mmio + PORT_CMD);
0860 }
0861 #endif
0862
0863 static void ahci_start_port(struct ata_port *ap)
0864 {
0865 struct ahci_host_priv *hpriv = ap->host->private_data;
0866 struct ahci_port_priv *pp = ap->private_data;
0867 struct ata_link *link;
0868 struct ahci_em_priv *emp;
0869 ssize_t rc;
0870 int i;
0871
0872
0873 ahci_start_fis_rx(ap);
0874
0875
0876 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
0877 hpriv->start_engine(ap);
0878
0879
0880 if (ap->flags & ATA_FLAG_EM) {
0881 ata_for_each_link(link, ap, EDGE) {
0882 emp = &pp->em_priv[link->pmp];
0883
0884
0885 for (i = 0; i < EM_MAX_RETRY; i++) {
0886 rc = ap->ops->transmit_led_message(ap,
0887 emp->led_state,
0888 4);
0889
0890
0891
0892
0893
0894
0895
0896
0897 if (rc == -EBUSY)
0898 msleep(1);
0899 else
0900 break;
0901 }
0902 }
0903 }
0904
0905 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
0906 ata_for_each_link(link, ap, EDGE)
0907 ahci_init_sw_activity(link);
0908
0909 }
0910
0911 static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
0912 {
0913 int rc;
0914 struct ahci_host_priv *hpriv = ap->host->private_data;
0915
0916
0917 rc = hpriv->stop_engine(ap);
0918 if (rc) {
0919 *emsg = "failed to stop engine";
0920 return rc;
0921 }
0922
0923
0924 rc = ahci_stop_fis_rx(ap);
0925 if (rc) {
0926 *emsg = "failed stop FIS RX";
0927 return rc;
0928 }
0929
0930 return 0;
0931 }
0932
0933 int ahci_reset_controller(struct ata_host *host)
0934 {
0935 struct ahci_host_priv *hpriv = host->private_data;
0936 void __iomem *mmio = hpriv->mmio;
0937 u32 tmp;
0938
0939
0940
0941
0942 ahci_enable_ahci(mmio);
0943
0944
0945 if (!ahci_skip_host_reset) {
0946 tmp = readl(mmio + HOST_CTL);
0947 if ((tmp & HOST_RESET) == 0) {
0948 writel(tmp | HOST_RESET, mmio + HOST_CTL);
0949 readl(mmio + HOST_CTL);
0950 }
0951
0952
0953
0954
0955
0956
0957
0958 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
0959 HOST_RESET, 10, 1000);
0960
0961 if (tmp & HOST_RESET) {
0962 dev_err(host->dev, "controller reset failed (0x%x)\n",
0963 tmp);
0964 return -EIO;
0965 }
0966
0967
0968 ahci_enable_ahci(mmio);
0969
0970
0971
0972
0973 if (!(hpriv->flags & AHCI_HFLAG_NO_WRITE_TO_RO))
0974 ahci_restore_initial_config(host);
0975 } else
0976 dev_info(host->dev, "skipping global host reset\n");
0977
0978 return 0;
0979 }
0980 EXPORT_SYMBOL_GPL(ahci_reset_controller);
0981
0982 static void ahci_sw_activity(struct ata_link *link)
0983 {
0984 struct ata_port *ap = link->ap;
0985 struct ahci_port_priv *pp = ap->private_data;
0986 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
0987
0988 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
0989 return;
0990
0991 emp->activity++;
0992 if (!timer_pending(&emp->timer))
0993 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
0994 }
0995
0996 static void ahci_sw_activity_blink(struct timer_list *t)
0997 {
0998 struct ahci_em_priv *emp = from_timer(emp, t, timer);
0999 struct ata_link *link = emp->link;
1000 struct ata_port *ap = link->ap;
1001
1002 unsigned long led_message = emp->led_state;
1003 u32 activity_led_state;
1004 unsigned long flags;
1005
1006 led_message &= EM_MSG_LED_VALUE;
1007 led_message |= ap->port_no | (link->pmp << 8);
1008
1009
1010
1011
1012
1013 spin_lock_irqsave(ap->lock, flags);
1014 if (emp->saved_activity != emp->activity) {
1015 emp->saved_activity = emp->activity;
1016
1017 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
1018
1019 if (activity_led_state)
1020 activity_led_state = 0;
1021 else
1022 activity_led_state = 1;
1023
1024
1025 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1026
1027
1028 led_message |= (activity_led_state << 16);
1029 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1030 } else {
1031
1032 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1033 if (emp->blink_policy == BLINK_OFF)
1034 led_message |= (1 << 16);
1035 }
1036 spin_unlock_irqrestore(ap->lock, flags);
1037 ap->ops->transmit_led_message(ap, led_message, 4);
1038 }
1039
1040 static void ahci_init_sw_activity(struct ata_link *link)
1041 {
1042 struct ata_port *ap = link->ap;
1043 struct ahci_port_priv *pp = ap->private_data;
1044 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1045
1046
1047 emp->saved_activity = emp->activity = 0;
1048 emp->link = link;
1049 timer_setup(&emp->timer, ahci_sw_activity_blink, 0);
1050
1051
1052 if (emp->blink_policy)
1053 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1054 }
1055
1056 int ahci_reset_em(struct ata_host *host)
1057 {
1058 struct ahci_host_priv *hpriv = host->private_data;
1059 void __iomem *mmio = hpriv->mmio;
1060 u32 em_ctl;
1061
1062 em_ctl = readl(mmio + HOST_EM_CTL);
1063 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1064 return -EINVAL;
1065
1066 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1067 return 0;
1068 }
1069 EXPORT_SYMBOL_GPL(ahci_reset_em);
1070
1071 static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1072 ssize_t size)
1073 {
1074 struct ahci_host_priv *hpriv = ap->host->private_data;
1075 struct ahci_port_priv *pp = ap->private_data;
1076 void __iomem *mmio = hpriv->mmio;
1077 u32 em_ctl;
1078 u32 message[] = {0, 0};
1079 unsigned long flags;
1080 int pmp;
1081 struct ahci_em_priv *emp;
1082
1083
1084 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1085 if (pmp < EM_MAX_SLOTS)
1086 emp = &pp->em_priv[pmp];
1087 else
1088 return -EINVAL;
1089
1090 ahci_rpm_get_port(ap);
1091 spin_lock_irqsave(ap->lock, flags);
1092
1093
1094
1095
1096
1097 em_ctl = readl(mmio + HOST_EM_CTL);
1098 if (em_ctl & EM_CTL_TM) {
1099 spin_unlock_irqrestore(ap->lock, flags);
1100 ahci_rpm_put_port(ap);
1101 return -EBUSY;
1102 }
1103
1104 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1105
1106
1107
1108
1109 message[0] |= (4 << 8);
1110
1111
1112 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
1113
1114
1115 writel(message[0], mmio + hpriv->em_loc);
1116 writel(message[1], mmio + hpriv->em_loc+4);
1117
1118
1119
1120
1121 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1122 }
1123
1124
1125 emp->led_state = state;
1126
1127 spin_unlock_irqrestore(ap->lock, flags);
1128 ahci_rpm_put_port(ap);
1129
1130 return size;
1131 }
1132
1133 static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1134 {
1135 struct ahci_port_priv *pp = ap->private_data;
1136 struct ata_link *link;
1137 struct ahci_em_priv *emp;
1138 int rc = 0;
1139
1140 ata_for_each_link(link, ap, EDGE) {
1141 emp = &pp->em_priv[link->pmp];
1142 rc += sprintf(buf, "%lx\n", emp->led_state);
1143 }
1144 return rc;
1145 }
1146
1147 static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1148 size_t size)
1149 {
1150 unsigned int state;
1151 int pmp;
1152 struct ahci_port_priv *pp = ap->private_data;
1153 struct ahci_em_priv *emp;
1154
1155 if (kstrtouint(buf, 0, &state) < 0)
1156 return -EINVAL;
1157
1158
1159 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1160 if (pmp < EM_MAX_SLOTS) {
1161 pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
1162 emp = &pp->em_priv[pmp];
1163 } else {
1164 return -EINVAL;
1165 }
1166
1167
1168
1169
1170
1171 if (emp->blink_policy)
1172 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1173
1174 return ap->ops->transmit_led_message(ap, state, size);
1175 }
1176
1177 static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1178 {
1179 struct ata_link *link = dev->link;
1180 struct ata_port *ap = link->ap;
1181 struct ahci_port_priv *pp = ap->private_data;
1182 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1183 u32 port_led_state = emp->led_state;
1184
1185
1186 if (val == OFF) {
1187
1188 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1189
1190
1191 port_led_state &= EM_MSG_LED_VALUE_OFF;
1192 port_led_state |= (ap->port_no | (link->pmp << 8));
1193 ap->ops->transmit_led_message(ap, port_led_state, 4);
1194 } else {
1195 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1196 if (val == BLINK_OFF) {
1197
1198 port_led_state &= EM_MSG_LED_VALUE_OFF;
1199 port_led_state |= (ap->port_no | (link->pmp << 8));
1200 port_led_state |= EM_MSG_LED_VALUE_ON;
1201 ap->ops->transmit_led_message(ap, port_led_state, 4);
1202 }
1203 }
1204 emp->blink_policy = val;
1205 return 0;
1206 }
1207
1208 static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1209 {
1210 struct ata_link *link = dev->link;
1211 struct ata_port *ap = link->ap;
1212 struct ahci_port_priv *pp = ap->private_data;
1213 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1214
1215
1216
1217
1218 return sprintf(buf, "%d\n", emp->blink_policy);
1219 }
1220
1221 static void ahci_port_init(struct device *dev, struct ata_port *ap,
1222 int port_no, void __iomem *mmio,
1223 void __iomem *port_mmio)
1224 {
1225 struct ahci_host_priv *hpriv = ap->host->private_data;
1226 const char *emsg = NULL;
1227 int rc;
1228 u32 tmp;
1229
1230
1231 rc = ahci_deinit_port(ap, &emsg);
1232 if (rc)
1233 dev_warn(dev, "%s (%d)\n", emsg, rc);
1234
1235
1236 tmp = readl(port_mmio + PORT_SCR_ERR);
1237 dev_dbg(dev, "PORT_SCR_ERR 0x%x\n", tmp);
1238 writel(tmp, port_mmio + PORT_SCR_ERR);
1239
1240
1241 tmp = readl(port_mmio + PORT_IRQ_STAT);
1242 dev_dbg(dev, "PORT_IRQ_STAT 0x%x\n", tmp);
1243 if (tmp)
1244 writel(tmp, port_mmio + PORT_IRQ_STAT);
1245
1246 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1247
1248
1249 tmp = readl(port_mmio + PORT_CMD);
1250 if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
1251 ap->pflags |= ATA_PFLAG_EXTERNAL;
1252 }
1253
1254 void ahci_init_controller(struct ata_host *host)
1255 {
1256 struct ahci_host_priv *hpriv = host->private_data;
1257 void __iomem *mmio = hpriv->mmio;
1258 int i;
1259 void __iomem *port_mmio;
1260 u32 tmp;
1261
1262 for (i = 0; i < host->n_ports; i++) {
1263 struct ata_port *ap = host->ports[i];
1264
1265 port_mmio = ahci_port_base(ap);
1266 if (ata_port_is_dummy(ap))
1267 continue;
1268
1269 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1270 }
1271
1272 tmp = readl(mmio + HOST_CTL);
1273 dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
1274 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1275 tmp = readl(mmio + HOST_CTL);
1276 dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
1277 }
1278 EXPORT_SYMBOL_GPL(ahci_init_controller);
1279
1280 static void ahci_dev_config(struct ata_device *dev)
1281 {
1282 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1283
1284 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1285 dev->max_sectors = 255;
1286 ata_dev_info(dev,
1287 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1288 }
1289 }
1290
1291 unsigned int ahci_dev_classify(struct ata_port *ap)
1292 {
1293 void __iomem *port_mmio = ahci_port_base(ap);
1294 struct ata_taskfile tf;
1295 u32 tmp;
1296
1297 tmp = readl(port_mmio + PORT_SIG);
1298 tf.lbah = (tmp >> 24) & 0xff;
1299 tf.lbam = (tmp >> 16) & 0xff;
1300 tf.lbal = (tmp >> 8) & 0xff;
1301 tf.nsect = (tmp) & 0xff;
1302
1303 return ata_port_classify(ap, &tf);
1304 }
1305 EXPORT_SYMBOL_GPL(ahci_dev_classify);
1306
1307 void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1308 u32 opts)
1309 {
1310 dma_addr_t cmd_tbl_dma;
1311
1312 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1313
1314 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1315 pp->cmd_slot[tag].status = 0;
1316 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1317 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1318 }
1319 EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
1320
1321 int ahci_kick_engine(struct ata_port *ap)
1322 {
1323 void __iomem *port_mmio = ahci_port_base(ap);
1324 struct ahci_host_priv *hpriv = ap->host->private_data;
1325 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1326 u32 tmp;
1327 int busy, rc;
1328
1329
1330 rc = hpriv->stop_engine(ap);
1331 if (rc)
1332 goto out_restart;
1333
1334
1335
1336
1337 busy = status & (ATA_BUSY | ATA_DRQ);
1338 if (!busy && !sata_pmp_attached(ap)) {
1339 rc = 0;
1340 goto out_restart;
1341 }
1342
1343 if (!(hpriv->cap & HOST_CAP_CLO)) {
1344 rc = -EOPNOTSUPP;
1345 goto out_restart;
1346 }
1347
1348
1349 tmp = readl(port_mmio + PORT_CMD);
1350 tmp |= PORT_CMD_CLO;
1351 writel(tmp, port_mmio + PORT_CMD);
1352
1353 rc = 0;
1354 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
1355 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1356 if (tmp & PORT_CMD_CLO)
1357 rc = -EIO;
1358
1359
1360 out_restart:
1361 hpriv->start_engine(ap);
1362 return rc;
1363 }
1364 EXPORT_SYMBOL_GPL(ahci_kick_engine);
1365
1366 static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1367 struct ata_taskfile *tf, int is_cmd, u16 flags,
1368 unsigned long timeout_msec)
1369 {
1370 const u32 cmd_fis_len = 5;
1371 struct ahci_port_priv *pp = ap->private_data;
1372 void __iomem *port_mmio = ahci_port_base(ap);
1373 u8 *fis = pp->cmd_tbl;
1374 u32 tmp;
1375
1376
1377 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1378 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1379
1380
1381 if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1382 tmp = readl(port_mmio + PORT_FBS);
1383 tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1384 tmp |= pmp << PORT_FBS_DEV_OFFSET;
1385 writel(tmp, port_mmio + PORT_FBS);
1386 pp->fbs_last_dev = pmp;
1387 }
1388
1389
1390 writel(1, port_mmio + PORT_CMD_ISSUE);
1391
1392 if (timeout_msec) {
1393 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1394 0x1, 0x1, 1, timeout_msec);
1395 if (tmp & 0x1) {
1396 ahci_kick_engine(ap);
1397 return -EBUSY;
1398 }
1399 } else
1400 readl(port_mmio + PORT_CMD_ISSUE);
1401
1402 return 0;
1403 }
1404
1405 int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1406 int pmp, unsigned long deadline,
1407 int (*check_ready)(struct ata_link *link))
1408 {
1409 struct ata_port *ap = link->ap;
1410 struct ahci_host_priv *hpriv = ap->host->private_data;
1411 struct ahci_port_priv *pp = ap->private_data;
1412 const char *reason = NULL;
1413 unsigned long now, msecs;
1414 struct ata_taskfile tf;
1415 bool fbs_disabled = false;
1416 int rc;
1417
1418
1419 rc = ahci_kick_engine(ap);
1420 if (rc && rc != -EOPNOTSUPP)
1421 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
1422
1423
1424
1425
1426
1427
1428 if (!ata_is_host_link(link) && pp->fbs_enabled) {
1429 ahci_disable_fbs(ap);
1430 fbs_disabled = true;
1431 }
1432
1433 ata_tf_init(link->device, &tf);
1434
1435
1436 msecs = 0;
1437 now = jiffies;
1438 if (time_after(deadline, now))
1439 msecs = jiffies_to_msecs(deadline - now);
1440
1441 tf.ctl |= ATA_SRST;
1442 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1443 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1444 rc = -EIO;
1445 reason = "1st FIS failed";
1446 goto fail;
1447 }
1448
1449
1450 ata_msleep(ap, 1);
1451
1452
1453 tf.ctl &= ~ATA_SRST;
1454 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1455
1456
1457 rc = ata_wait_after_reset(link, deadline, check_ready);
1458 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1459
1460
1461
1462
1463
1464 ata_link_info(link, "device not ready, treating as offline\n");
1465 *class = ATA_DEV_NONE;
1466 } else if (rc) {
1467
1468 reason = "device not ready";
1469 goto fail;
1470 } else
1471 *class = ahci_dev_classify(ap);
1472
1473
1474 if (fbs_disabled)
1475 ahci_enable_fbs(ap);
1476
1477 return 0;
1478
1479 fail:
1480 ata_link_err(link, "softreset failed (%s)\n", reason);
1481 return rc;
1482 }
1483
1484 int ahci_check_ready(struct ata_link *link)
1485 {
1486 void __iomem *port_mmio = ahci_port_base(link->ap);
1487 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1488
1489 return ata_check_ready(status);
1490 }
1491 EXPORT_SYMBOL_GPL(ahci_check_ready);
1492
1493 static int ahci_softreset(struct ata_link *link, unsigned int *class,
1494 unsigned long deadline)
1495 {
1496 int pmp = sata_srst_pmp(link);
1497
1498 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1499 }
1500 EXPORT_SYMBOL_GPL(ahci_do_softreset);
1501
1502 static int ahci_bad_pmp_check_ready(struct ata_link *link)
1503 {
1504 void __iomem *port_mmio = ahci_port_base(link->ap);
1505 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1506 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1507
1508
1509
1510
1511
1512 if (irq_status & PORT_IRQ_BAD_PMP)
1513 return -EIO;
1514
1515 return ata_check_ready(status);
1516 }
1517
1518 static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1519 unsigned long deadline)
1520 {
1521 struct ata_port *ap = link->ap;
1522 void __iomem *port_mmio = ahci_port_base(ap);
1523 int pmp = sata_srst_pmp(link);
1524 int rc;
1525 u32 irq_sts;
1526
1527 rc = ahci_do_softreset(link, class, pmp, deadline,
1528 ahci_bad_pmp_check_ready);
1529
1530
1531
1532
1533
1534
1535 if (rc == -EIO) {
1536 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1537 if (irq_sts & PORT_IRQ_BAD_PMP) {
1538 ata_link_warn(link,
1539 "applying PMP SRST workaround "
1540 "and retrying\n");
1541 rc = ahci_do_softreset(link, class, 0, deadline,
1542 ahci_check_ready);
1543 }
1544 }
1545
1546 return rc;
1547 }
1548
1549 int ahci_do_hardreset(struct ata_link *link, unsigned int *class,
1550 unsigned long deadline, bool *online)
1551 {
1552 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1553 struct ata_port *ap = link->ap;
1554 struct ahci_port_priv *pp = ap->private_data;
1555 struct ahci_host_priv *hpriv = ap->host->private_data;
1556 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1557 struct ata_taskfile tf;
1558 int rc;
1559
1560 hpriv->stop_engine(ap);
1561
1562
1563 ata_tf_init(link->device, &tf);
1564 tf.status = ATA_BUSY;
1565 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1566
1567 rc = sata_link_hardreset(link, timing, deadline, online,
1568 ahci_check_ready);
1569
1570 hpriv->start_engine(ap);
1571
1572 if (*online)
1573 *class = ahci_dev_classify(ap);
1574
1575 return rc;
1576 }
1577 EXPORT_SYMBOL_GPL(ahci_do_hardreset);
1578
1579 static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1580 unsigned long deadline)
1581 {
1582 bool online;
1583
1584 return ahci_do_hardreset(link, class, deadline, &online);
1585 }
1586
1587 static void ahci_postreset(struct ata_link *link, unsigned int *class)
1588 {
1589 struct ata_port *ap = link->ap;
1590 void __iomem *port_mmio = ahci_port_base(ap);
1591 u32 new_tmp, tmp;
1592
1593 ata_std_postreset(link, class);
1594
1595
1596 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1597 if (*class == ATA_DEV_ATAPI)
1598 new_tmp |= PORT_CMD_ATAPI;
1599 else
1600 new_tmp &= ~PORT_CMD_ATAPI;
1601 if (new_tmp != tmp) {
1602 writel(new_tmp, port_mmio + PORT_CMD);
1603 readl(port_mmio + PORT_CMD);
1604 }
1605 }
1606
1607 static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1608 {
1609 struct scatterlist *sg;
1610 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1611 unsigned int si;
1612
1613
1614
1615
1616 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1617 dma_addr_t addr = sg_dma_address(sg);
1618 u32 sg_len = sg_dma_len(sg);
1619
1620 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1621 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1622 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1623 }
1624
1625 return si;
1626 }
1627
1628 static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1629 {
1630 struct ata_port *ap = qc->ap;
1631 struct ahci_port_priv *pp = ap->private_data;
1632
1633 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1634 return ata_std_qc_defer(qc);
1635 else
1636 return sata_pmp_qc_defer_cmd_switch(qc);
1637 }
1638
1639 static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc)
1640 {
1641 struct ata_port *ap = qc->ap;
1642 struct ahci_port_priv *pp = ap->private_data;
1643 int is_atapi = ata_is_atapi(qc->tf.protocol);
1644 void *cmd_tbl;
1645 u32 opts;
1646 const u32 cmd_fis_len = 5;
1647 unsigned int n_elem;
1648
1649
1650
1651
1652
1653 cmd_tbl = pp->cmd_tbl + qc->hw_tag * AHCI_CMD_TBL_SZ;
1654
1655 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1656 if (is_atapi) {
1657 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1658 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1659 }
1660
1661 n_elem = 0;
1662 if (qc->flags & ATA_QCFLAG_DMAMAP)
1663 n_elem = ahci_fill_sg(qc, cmd_tbl);
1664
1665
1666
1667
1668 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1669 if (qc->tf.flags & ATA_TFLAG_WRITE)
1670 opts |= AHCI_CMD_WRITE;
1671 if (is_atapi)
1672 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1673
1674 ahci_fill_cmd_slot(pp, qc->hw_tag, opts);
1675
1676 return AC_ERR_OK;
1677 }
1678
1679 static void ahci_fbs_dec_intr(struct ata_port *ap)
1680 {
1681 struct ahci_port_priv *pp = ap->private_data;
1682 void __iomem *port_mmio = ahci_port_base(ap);
1683 u32 fbs = readl(port_mmio + PORT_FBS);
1684 int retries = 3;
1685
1686 BUG_ON(!pp->fbs_enabled);
1687
1688
1689
1690
1691 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1692 fbs = readl(port_mmio + PORT_FBS);
1693 while ((fbs & PORT_FBS_DEC) && retries--) {
1694 udelay(1);
1695 fbs = readl(port_mmio + PORT_FBS);
1696 }
1697
1698 if (fbs & PORT_FBS_DEC)
1699 dev_err(ap->host->dev, "failed to clear device error\n");
1700 }
1701
1702 static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1703 {
1704 struct ahci_host_priv *hpriv = ap->host->private_data;
1705 struct ahci_port_priv *pp = ap->private_data;
1706 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1707 struct ata_link *link = NULL;
1708 struct ata_queued_cmd *active_qc;
1709 struct ata_eh_info *active_ehi;
1710 bool fbs_need_dec = false;
1711 u32 serror;
1712
1713
1714 if (pp->fbs_enabled) {
1715 void __iomem *port_mmio = ahci_port_base(ap);
1716 u32 fbs = readl(port_mmio + PORT_FBS);
1717 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1718
1719 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
1720 link = &ap->pmp_link[pmp];
1721 fbs_need_dec = true;
1722 }
1723
1724 } else
1725 ata_for_each_link(link, ap, EDGE)
1726 if (ata_link_active(link))
1727 break;
1728
1729 if (!link)
1730 link = &ap->link;
1731
1732 active_qc = ata_qc_from_tag(ap, link->active_tag);
1733 active_ehi = &link->eh_info;
1734
1735
1736 ata_ehi_clear_desc(host_ehi);
1737 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1738
1739
1740 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1741 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1742 host_ehi->serror |= serror;
1743
1744
1745 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1746 irq_stat &= ~PORT_IRQ_IF_ERR;
1747
1748 if (irq_stat & PORT_IRQ_TF_ERR) {
1749
1750
1751
1752
1753 if (active_qc)
1754 active_qc->err_mask |= AC_ERR_DEV;
1755 else
1756 active_ehi->err_mask |= AC_ERR_DEV;
1757
1758 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1759 host_ehi->serror &= ~SERR_INTERNAL;
1760 }
1761
1762 if (irq_stat & PORT_IRQ_UNK_FIS) {
1763 u32 *unk = pp->rx_fis + RX_FIS_UNK;
1764
1765 active_ehi->err_mask |= AC_ERR_HSM;
1766 active_ehi->action |= ATA_EH_RESET;
1767 ata_ehi_push_desc(active_ehi,
1768 "unknown FIS %08x %08x %08x %08x" ,
1769 unk[0], unk[1], unk[2], unk[3]);
1770 }
1771
1772 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1773 active_ehi->err_mask |= AC_ERR_HSM;
1774 active_ehi->action |= ATA_EH_RESET;
1775 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1776 }
1777
1778 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1779 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1780 host_ehi->action |= ATA_EH_RESET;
1781 ata_ehi_push_desc(host_ehi, "host bus error");
1782 }
1783
1784 if (irq_stat & PORT_IRQ_IF_ERR) {
1785 if (fbs_need_dec)
1786 active_ehi->err_mask |= AC_ERR_DEV;
1787 else {
1788 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1789 host_ehi->action |= ATA_EH_RESET;
1790 }
1791
1792 ata_ehi_push_desc(host_ehi, "interface fatal error");
1793 }
1794
1795 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1796 ata_ehi_hotplugged(host_ehi);
1797 ata_ehi_push_desc(host_ehi, "%s",
1798 irq_stat & PORT_IRQ_CONNECT ?
1799 "connection status changed" : "PHY RDY changed");
1800 }
1801
1802
1803
1804 if (irq_stat & PORT_IRQ_FREEZE)
1805 ata_port_freeze(ap);
1806 else if (fbs_need_dec) {
1807 ata_link_abort(link);
1808 ahci_fbs_dec_intr(ap);
1809 } else
1810 ata_port_abort(ap);
1811 }
1812
1813 static void ahci_handle_port_interrupt(struct ata_port *ap,
1814 void __iomem *port_mmio, u32 status)
1815 {
1816 struct ata_eh_info *ehi = &ap->link.eh_info;
1817 struct ahci_port_priv *pp = ap->private_data;
1818 struct ahci_host_priv *hpriv = ap->host->private_data;
1819 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1820 u32 qc_active = 0;
1821 int rc;
1822
1823
1824 if (unlikely(resetting))
1825 status &= ~PORT_IRQ_BAD_PMP;
1826
1827 if (sata_lpm_ignore_phy_events(&ap->link)) {
1828 status &= ~PORT_IRQ_PHYRDY;
1829 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
1830 }
1831
1832 if (unlikely(status & PORT_IRQ_ERROR)) {
1833 ahci_error_intr(ap, status);
1834 return;
1835 }
1836
1837 if (status & PORT_IRQ_SDB_FIS) {
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847 if (hpriv->cap & HOST_CAP_SNTF)
1848 sata_async_notification(ap);
1849 else {
1850
1851
1852
1853
1854
1855
1856
1857
1858 if (pp->fbs_enabled)
1859 WARN_ON_ONCE(1);
1860 else {
1861 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1862 u32 f0 = le32_to_cpu(f[0]);
1863 if (f0 & (1 << 15))
1864 sata_async_notification(ap);
1865 }
1866 }
1867 }
1868
1869
1870
1871
1872
1873 if (pp->fbs_enabled) {
1874 if (ap->qc_active) {
1875 qc_active = readl(port_mmio + PORT_SCR_ACT);
1876 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1877 }
1878 } else {
1879
1880 if (ap->qc_active && pp->active_link->sactive)
1881 qc_active = readl(port_mmio + PORT_SCR_ACT);
1882 else
1883 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1884 }
1885
1886
1887 rc = ata_qc_complete_multiple(ap, qc_active);
1888
1889
1890 if (unlikely(rc < 0 && !resetting)) {
1891 ehi->err_mask |= AC_ERR_HSM;
1892 ehi->action |= ATA_EH_RESET;
1893 ata_port_freeze(ap);
1894 }
1895 }
1896
1897 static void ahci_port_intr(struct ata_port *ap)
1898 {
1899 void __iomem *port_mmio = ahci_port_base(ap);
1900 u32 status;
1901
1902 status = readl(port_mmio + PORT_IRQ_STAT);
1903 writel(status, port_mmio + PORT_IRQ_STAT);
1904
1905 ahci_handle_port_interrupt(ap, port_mmio, status);
1906 }
1907
1908 static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
1909 {
1910 struct ata_port *ap = dev_instance;
1911 void __iomem *port_mmio = ahci_port_base(ap);
1912 u32 status;
1913
1914 status = readl(port_mmio + PORT_IRQ_STAT);
1915 writel(status, port_mmio + PORT_IRQ_STAT);
1916
1917 spin_lock(ap->lock);
1918 ahci_handle_port_interrupt(ap, port_mmio, status);
1919 spin_unlock(ap->lock);
1920
1921 return IRQ_HANDLED;
1922 }
1923
1924 u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
1925 {
1926 unsigned int i, handled = 0;
1927
1928 for (i = 0; i < host->n_ports; i++) {
1929 struct ata_port *ap;
1930
1931 if (!(irq_masked & (1 << i)))
1932 continue;
1933
1934 ap = host->ports[i];
1935 if (ap) {
1936 ahci_port_intr(ap);
1937 } else {
1938 if (ata_ratelimit())
1939 dev_warn(host->dev,
1940 "interrupt on disabled port %u\n", i);
1941 }
1942
1943 handled = 1;
1944 }
1945
1946 return handled;
1947 }
1948 EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
1949
1950 static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1951 {
1952 struct ata_host *host = dev_instance;
1953 struct ahci_host_priv *hpriv;
1954 unsigned int rc = 0;
1955 void __iomem *mmio;
1956 u32 irq_stat, irq_masked;
1957
1958 hpriv = host->private_data;
1959 mmio = hpriv->mmio;
1960
1961
1962 irq_stat = readl(mmio + HOST_IRQ_STAT);
1963 if (!irq_stat)
1964 return IRQ_NONE;
1965
1966 irq_masked = irq_stat & hpriv->port_map;
1967
1968 spin_lock(&host->lock);
1969
1970 rc = ahci_handle_port_intr(host, irq_masked);
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981 writel(irq_stat, mmio + HOST_IRQ_STAT);
1982
1983 spin_unlock(&host->lock);
1984
1985 return IRQ_RETVAL(rc);
1986 }
1987
1988 unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1989 {
1990 struct ata_port *ap = qc->ap;
1991 void __iomem *port_mmio = ahci_port_base(ap);
1992 struct ahci_port_priv *pp = ap->private_data;
1993
1994
1995
1996
1997
1998 pp->active_link = qc->dev->link;
1999
2000 if (ata_is_ncq(qc->tf.protocol))
2001 writel(1 << qc->hw_tag, port_mmio + PORT_SCR_ACT);
2002
2003 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
2004 u32 fbs = readl(port_mmio + PORT_FBS);
2005 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
2006 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
2007 writel(fbs, port_mmio + PORT_FBS);
2008 pp->fbs_last_dev = qc->dev->link->pmp;
2009 }
2010
2011 writel(1 << qc->hw_tag, port_mmio + PORT_CMD_ISSUE);
2012
2013 ahci_sw_activity(qc->dev->link);
2014
2015 return 0;
2016 }
2017 EXPORT_SYMBOL_GPL(ahci_qc_issue);
2018
2019 static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2020 {
2021 struct ahci_port_priv *pp = qc->ap->private_data;
2022 u8 *rx_fis = pp->rx_fis;
2023
2024 if (pp->fbs_enabled)
2025 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
2026
2027
2028
2029
2030
2031
2032
2033 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
2034 !(qc->flags & ATA_QCFLAG_FAILED)) {
2035 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
2036 qc->result_tf.status = (rx_fis + RX_FIS_PIO_SETUP)[15];
2037 } else
2038 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
2039
2040 return true;
2041 }
2042
2043 static void ahci_freeze(struct ata_port *ap)
2044 {
2045 void __iomem *port_mmio = ahci_port_base(ap);
2046
2047
2048 writel(0, port_mmio + PORT_IRQ_MASK);
2049 }
2050
2051 static void ahci_thaw(struct ata_port *ap)
2052 {
2053 struct ahci_host_priv *hpriv = ap->host->private_data;
2054 void __iomem *mmio = hpriv->mmio;
2055 void __iomem *port_mmio = ahci_port_base(ap);
2056 u32 tmp;
2057 struct ahci_port_priv *pp = ap->private_data;
2058
2059
2060 tmp = readl(port_mmio + PORT_IRQ_STAT);
2061 writel(tmp, port_mmio + PORT_IRQ_STAT);
2062 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2063
2064
2065 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2066 }
2067
2068 void ahci_error_handler(struct ata_port *ap)
2069 {
2070 struct ahci_host_priv *hpriv = ap->host->private_data;
2071
2072 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2073
2074 hpriv->stop_engine(ap);
2075 hpriv->start_engine(ap);
2076 }
2077
2078 sata_pmp_error_handler(ap);
2079
2080 if (!ata_dev_enabled(ap->link.device))
2081 hpriv->stop_engine(ap);
2082 }
2083 EXPORT_SYMBOL_GPL(ahci_error_handler);
2084
2085 static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2086 {
2087 struct ata_port *ap = qc->ap;
2088
2089
2090 if (qc->flags & ATA_QCFLAG_FAILED)
2091 ahci_kick_engine(ap);
2092 }
2093
2094 static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2095 {
2096 struct ahci_host_priv *hpriv = ap->host->private_data;
2097 void __iomem *port_mmio = ahci_port_base(ap);
2098 struct ata_device *dev = ap->link.device;
2099 u32 devslp, dm, dito, mdat, deto, dito_conf;
2100 int rc;
2101 unsigned int err_mask;
2102
2103 devslp = readl(port_mmio + PORT_DEVSLP);
2104 if (!(devslp & PORT_DEVSLP_DSP)) {
2105 dev_info(ap->host->dev, "port does not support device sleep\n");
2106 return;
2107 }
2108
2109
2110 if (!sleep) {
2111 if (devslp & PORT_DEVSLP_ADSE) {
2112 writel(devslp & ~PORT_DEVSLP_ADSE,
2113 port_mmio + PORT_DEVSLP);
2114 err_mask = ata_dev_set_feature(dev,
2115 SETFEATURES_SATA_DISABLE,
2116 SATA_DEVSLP);
2117 if (err_mask && err_mask != AC_ERR_DEV)
2118 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2119 }
2120 return;
2121 }
2122
2123 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2124 dito = devslp_idle_timeout / (dm + 1);
2125 if (dito > 0x3ff)
2126 dito = 0x3ff;
2127
2128 dito_conf = (devslp >> PORT_DEVSLP_DITO_OFFSET) & 0x3FF;
2129
2130
2131 if ((devslp & PORT_DEVSLP_ADSE) && (dito_conf == dito))
2132 return;
2133
2134
2135 rc = hpriv->stop_engine(ap);
2136 if (rc)
2137 return;
2138
2139
2140
2141
2142 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
2143 ATA_LOG_DEVSLP_VALID_MASK) {
2144 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
2145 ATA_LOG_DEVSLP_MDAT_MASK;
2146 if (!mdat)
2147 mdat = 10;
2148 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
2149 if (!deto)
2150 deto = 20;
2151 } else {
2152 mdat = 10;
2153 deto = 20;
2154 }
2155
2156
2157 devslp &= ~GENMASK_ULL(24, 2);
2158 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2159 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2160 (deto << PORT_DEVSLP_DETO_OFFSET) |
2161 PORT_DEVSLP_ADSE);
2162 writel(devslp, port_mmio + PORT_DEVSLP);
2163
2164 hpriv->start_engine(ap);
2165
2166
2167 err_mask = ata_dev_set_feature(dev,
2168 SETFEATURES_SATA_ENABLE,
2169 SATA_DEVSLP);
2170 if (err_mask && err_mask != AC_ERR_DEV)
2171 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2172 }
2173
2174 static void ahci_enable_fbs(struct ata_port *ap)
2175 {
2176 struct ahci_host_priv *hpriv = ap->host->private_data;
2177 struct ahci_port_priv *pp = ap->private_data;
2178 void __iomem *port_mmio = ahci_port_base(ap);
2179 u32 fbs;
2180 int rc;
2181
2182 if (!pp->fbs_supported)
2183 return;
2184
2185 fbs = readl(port_mmio + PORT_FBS);
2186 if (fbs & PORT_FBS_EN) {
2187 pp->fbs_enabled = true;
2188 pp->fbs_last_dev = -1;
2189 return;
2190 }
2191
2192 rc = hpriv->stop_engine(ap);
2193 if (rc)
2194 return;
2195
2196 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2197 fbs = readl(port_mmio + PORT_FBS);
2198 if (fbs & PORT_FBS_EN) {
2199 dev_info(ap->host->dev, "FBS is enabled\n");
2200 pp->fbs_enabled = true;
2201 pp->fbs_last_dev = -1;
2202 } else
2203 dev_err(ap->host->dev, "Failed to enable FBS\n");
2204
2205 hpriv->start_engine(ap);
2206 }
2207
2208 static void ahci_disable_fbs(struct ata_port *ap)
2209 {
2210 struct ahci_host_priv *hpriv = ap->host->private_data;
2211 struct ahci_port_priv *pp = ap->private_data;
2212 void __iomem *port_mmio = ahci_port_base(ap);
2213 u32 fbs;
2214 int rc;
2215
2216 if (!pp->fbs_supported)
2217 return;
2218
2219 fbs = readl(port_mmio + PORT_FBS);
2220 if ((fbs & PORT_FBS_EN) == 0) {
2221 pp->fbs_enabled = false;
2222 return;
2223 }
2224
2225 rc = hpriv->stop_engine(ap);
2226 if (rc)
2227 return;
2228
2229 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2230 fbs = readl(port_mmio + PORT_FBS);
2231 if (fbs & PORT_FBS_EN)
2232 dev_err(ap->host->dev, "Failed to disable FBS\n");
2233 else {
2234 dev_info(ap->host->dev, "FBS is disabled\n");
2235 pp->fbs_enabled = false;
2236 }
2237
2238 hpriv->start_engine(ap);
2239 }
2240
2241 static void ahci_pmp_attach(struct ata_port *ap)
2242 {
2243 void __iomem *port_mmio = ahci_port_base(ap);
2244 struct ahci_port_priv *pp = ap->private_data;
2245 u32 cmd;
2246
2247 cmd = readl(port_mmio + PORT_CMD);
2248 cmd |= PORT_CMD_PMP;
2249 writel(cmd, port_mmio + PORT_CMD);
2250
2251 ahci_enable_fbs(ap);
2252
2253 pp->intr_mask |= PORT_IRQ_BAD_PMP;
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2264 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2265 }
2266
2267 static void ahci_pmp_detach(struct ata_port *ap)
2268 {
2269 void __iomem *port_mmio = ahci_port_base(ap);
2270 struct ahci_port_priv *pp = ap->private_data;
2271 u32 cmd;
2272
2273 ahci_disable_fbs(ap);
2274
2275 cmd = readl(port_mmio + PORT_CMD);
2276 cmd &= ~PORT_CMD_PMP;
2277 writel(cmd, port_mmio + PORT_CMD);
2278
2279 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2280
2281
2282 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2283 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2284 }
2285
2286 int ahci_port_resume(struct ata_port *ap)
2287 {
2288 ahci_rpm_get_port(ap);
2289
2290 ahci_power_up(ap);
2291 ahci_start_port(ap);
2292
2293 if (sata_pmp_attached(ap))
2294 ahci_pmp_attach(ap);
2295 else
2296 ahci_pmp_detach(ap);
2297
2298 return 0;
2299 }
2300 EXPORT_SYMBOL_GPL(ahci_port_resume);
2301
2302 #ifdef CONFIG_PM
2303 static void ahci_handle_s2idle(struct ata_port *ap)
2304 {
2305 void __iomem *port_mmio = ahci_port_base(ap);
2306 u32 devslp;
2307
2308 if (pm_suspend_via_firmware())
2309 return;
2310 devslp = readl(port_mmio + PORT_DEVSLP);
2311 if ((devslp & PORT_DEVSLP_ADSE))
2312 ata_msleep(ap, devslp_idle_timeout);
2313 }
2314
2315 static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2316 {
2317 const char *emsg = NULL;
2318 int rc;
2319
2320 rc = ahci_deinit_port(ap, &emsg);
2321 if (rc == 0)
2322 ahci_power_down(ap);
2323 else {
2324 ata_port_err(ap, "%s (%d)\n", emsg, rc);
2325 ata_port_freeze(ap);
2326 }
2327
2328 if (acpi_storage_d3(ap->host->dev))
2329 ahci_handle_s2idle(ap);
2330
2331 ahci_rpm_put_port(ap);
2332 return rc;
2333 }
2334 #endif
2335
2336 static int ahci_port_start(struct ata_port *ap)
2337 {
2338 struct ahci_host_priv *hpriv = ap->host->private_data;
2339 struct device *dev = ap->host->dev;
2340 struct ahci_port_priv *pp;
2341 void *mem;
2342 dma_addr_t mem_dma;
2343 size_t dma_sz, rx_fis_sz;
2344
2345 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2346 if (!pp)
2347 return -ENOMEM;
2348
2349 if (ap->host->n_ports > 1) {
2350 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2351 if (!pp->irq_desc) {
2352 devm_kfree(dev, pp);
2353 return -ENOMEM;
2354 }
2355 snprintf(pp->irq_desc, 8,
2356 "%s%d", dev_driver_string(dev), ap->port_no);
2357 }
2358
2359
2360 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2361 void __iomem *port_mmio = ahci_port_base(ap);
2362 u32 cmd = readl(port_mmio + PORT_CMD);
2363 if (cmd & PORT_CMD_FBSCP)
2364 pp->fbs_supported = true;
2365 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
2366 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2367 ap->port_no);
2368 pp->fbs_supported = true;
2369 } else
2370 dev_warn(dev, "port %d is not capable of FBS\n",
2371 ap->port_no);
2372 }
2373
2374 if (pp->fbs_supported) {
2375 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2376 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2377 } else {
2378 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2379 rx_fis_sz = AHCI_RX_FIS_SZ;
2380 }
2381
2382 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2383 if (!mem)
2384 return -ENOMEM;
2385
2386
2387
2388
2389
2390 pp->cmd_slot = mem;
2391 pp->cmd_slot_dma = mem_dma;
2392
2393 mem += AHCI_CMD_SLOT_SZ;
2394 mem_dma += AHCI_CMD_SLOT_SZ;
2395
2396
2397
2398
2399 pp->rx_fis = mem;
2400 pp->rx_fis_dma = mem_dma;
2401
2402 mem += rx_fis_sz;
2403 mem_dma += rx_fis_sz;
2404
2405
2406
2407
2408
2409 pp->cmd_tbl = mem;
2410 pp->cmd_tbl_dma = mem_dma;
2411
2412
2413
2414
2415
2416 pp->intr_mask = DEF_PORT_IRQ;
2417
2418
2419
2420
2421 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2422 spin_lock_init(&pp->lock);
2423 ap->lock = &pp->lock;
2424 }
2425
2426 ap->private_data = pp;
2427
2428
2429 return ahci_port_resume(ap);
2430 }
2431
2432 static void ahci_port_stop(struct ata_port *ap)
2433 {
2434 const char *emsg = NULL;
2435 struct ahci_host_priv *hpriv = ap->host->private_data;
2436 void __iomem *host_mmio = hpriv->mmio;
2437 int rc;
2438
2439
2440 rc = ahci_deinit_port(ap, &emsg);
2441 if (rc)
2442 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
2443
2444
2445
2446
2447
2448 writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
2449
2450 ahci_rpm_put_port(ap);
2451 }
2452
2453 void ahci_print_info(struct ata_host *host, const char *scc_s)
2454 {
2455 struct ahci_host_priv *hpriv = host->private_data;
2456 u32 vers, cap, cap2, impl, speed;
2457 const char *speed_s;
2458
2459 vers = hpriv->version;
2460 cap = hpriv->cap;
2461 cap2 = hpriv->cap2;
2462 impl = hpriv->port_map;
2463
2464 speed = (cap >> 20) & 0xf;
2465 if (speed == 1)
2466 speed_s = "1.5";
2467 else if (speed == 2)
2468 speed_s = "3";
2469 else if (speed == 3)
2470 speed_s = "6";
2471 else
2472 speed_s = "?";
2473
2474 dev_info(host->dev,
2475 "AHCI %02x%02x.%02x%02x "
2476 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2477 ,
2478
2479 (vers >> 24) & 0xff,
2480 (vers >> 16) & 0xff,
2481 (vers >> 8) & 0xff,
2482 vers & 0xff,
2483
2484 ((cap >> 8) & 0x1f) + 1,
2485 (cap & 0x1f) + 1,
2486 speed_s,
2487 impl,
2488 scc_s);
2489
2490 dev_info(host->dev,
2491 "flags: "
2492 "%s%s%s%s%s%s%s"
2493 "%s%s%s%s%s%s%s"
2494 "%s%s%s%s%s%s%s"
2495 "%s%s\n"
2496 ,
2497
2498 cap & HOST_CAP_64 ? "64bit " : "",
2499 cap & HOST_CAP_NCQ ? "ncq " : "",
2500 cap & HOST_CAP_SNTF ? "sntf " : "",
2501 cap & HOST_CAP_MPS ? "ilck " : "",
2502 cap & HOST_CAP_SSS ? "stag " : "",
2503 cap & HOST_CAP_ALPM ? "pm " : "",
2504 cap & HOST_CAP_LED ? "led " : "",
2505 cap & HOST_CAP_CLO ? "clo " : "",
2506 cap & HOST_CAP_ONLY ? "only " : "",
2507 cap & HOST_CAP_PMP ? "pmp " : "",
2508 cap & HOST_CAP_FBS ? "fbs " : "",
2509 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2510 cap & HOST_CAP_SSC ? "slum " : "",
2511 cap & HOST_CAP_PART ? "part " : "",
2512 cap & HOST_CAP_CCC ? "ccc " : "",
2513 cap & HOST_CAP_EMS ? "ems " : "",
2514 cap & HOST_CAP_SXS ? "sxs " : "",
2515 cap2 & HOST_CAP2_DESO ? "deso " : "",
2516 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2517 cap2 & HOST_CAP2_SDS ? "sds " : "",
2518 cap2 & HOST_CAP2_APST ? "apst " : "",
2519 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2520 cap2 & HOST_CAP2_BOH ? "boh " : ""
2521 );
2522 }
2523 EXPORT_SYMBOL_GPL(ahci_print_info);
2524
2525 void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2526 struct ata_port_info *pi)
2527 {
2528 u8 messages;
2529 void __iomem *mmio = hpriv->mmio;
2530 u32 em_loc = readl(mmio + HOST_EM_LOC);
2531 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2532
2533 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2534 return;
2535
2536 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2537
2538 if (messages) {
2539
2540 hpriv->em_loc = ((em_loc >> 16) * 4);
2541 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
2542 hpriv->em_msg_type = messages;
2543 pi->flags |= ATA_FLAG_EM;
2544 if (!(em_ctl & EM_CTL_ALHD))
2545 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2546 }
2547 }
2548 EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2549
2550 static int ahci_host_activate_multi_irqs(struct ata_host *host,
2551 struct scsi_host_template *sht)
2552 {
2553 struct ahci_host_priv *hpriv = host->private_data;
2554 int i, rc;
2555
2556 rc = ata_host_start(host);
2557 if (rc)
2558 return rc;
2559
2560
2561
2562
2563 for (i = 0; i < host->n_ports; i++) {
2564 struct ahci_port_priv *pp = host->ports[i]->private_data;
2565 int irq = hpriv->get_irq_vector(host, i);
2566
2567
2568 if (!pp) {
2569 disable_irq(irq);
2570 continue;
2571 }
2572
2573 rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2574 0, pp->irq_desc, host->ports[i]);
2575
2576 if (rc)
2577 return rc;
2578 ata_port_desc(host->ports[i], "irq %d", irq);
2579 }
2580
2581 return ata_host_register(host, sht);
2582 }
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595 int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
2596 {
2597 struct ahci_host_priv *hpriv = host->private_data;
2598 int irq = hpriv->irq;
2599 int rc;
2600
2601 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2602 if (hpriv->irq_handler &&
2603 hpriv->irq_handler != ahci_single_level_irq_intr)
2604 dev_warn(host->dev,
2605 "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
2606 if (!hpriv->get_irq_vector) {
2607 dev_err(host->dev,
2608 "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
2609 return -EIO;
2610 }
2611
2612 rc = ahci_host_activate_multi_irqs(host, sht);
2613 } else {
2614 rc = ata_host_activate(host, irq, hpriv->irq_handler,
2615 IRQF_SHARED, sht);
2616 }
2617
2618
2619 return rc;
2620 }
2621 EXPORT_SYMBOL_GPL(ahci_host_activate);
2622
2623 MODULE_AUTHOR("Jeff Garzik");
2624 MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2625 MODULE_LICENSE("GPL");