1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/kernel.h>
36#include <linux/gfp.h>
37#include <linux/module.h>
38#include <linux/nospec.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 <linux/pci.h>
48#include "ahci.h"
49#include "libata.h"
50
51static int ahci_skip_host_reset;
52int ahci_ignore_sss;
53EXPORT_SYMBOL_GPL(ahci_ignore_sss);
54
55module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
56MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
57
58module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
59MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
60
61static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
62 unsigned hints);
63static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
64static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
65 size_t size);
66static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
67 ssize_t size);
68
69
70
71static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
72static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
73static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
74static int ahci_port_start(struct ata_port *ap);
75static void ahci_port_stop(struct ata_port *ap);
76static void ahci_qc_prep(struct ata_queued_cmd *qc);
77static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
78static void ahci_freeze(struct ata_port *ap);
79static void ahci_thaw(struct ata_port *ap);
80static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
81static void ahci_enable_fbs(struct ata_port *ap);
82static void ahci_disable_fbs(struct ata_port *ap);
83static void ahci_pmp_attach(struct ata_port *ap);
84static void ahci_pmp_detach(struct ata_port *ap);
85static int ahci_softreset(struct ata_link *link, unsigned int *class,
86 unsigned long deadline);
87static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
88 unsigned long deadline);
89static int ahci_hardreset(struct ata_link *link, unsigned int *class,
90 unsigned long deadline);
91static void ahci_postreset(struct ata_link *link, unsigned int *class);
92static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
93static void ahci_dev_config(struct ata_device *dev);
94#ifdef CONFIG_PM
95static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
96#endif
97static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
98static ssize_t ahci_activity_store(struct ata_device *dev,
99 enum sw_activity val);
100static void ahci_init_sw_activity(struct ata_link *link);
101
102static ssize_t ahci_show_host_caps(struct device *dev,
103 struct device_attribute *attr, char *buf);
104static ssize_t ahci_show_host_cap2(struct device *dev,
105 struct device_attribute *attr, char *buf);
106static ssize_t ahci_show_host_version(struct device *dev,
107 struct device_attribute *attr, char *buf);
108static ssize_t ahci_show_port_cmd(struct device *dev,
109 struct device_attribute *attr, char *buf);
110static ssize_t ahci_read_em_buffer(struct device *dev,
111 struct device_attribute *attr, char *buf);
112static ssize_t ahci_store_em_buffer(struct device *dev,
113 struct device_attribute *attr,
114 const char *buf, size_t size);
115static ssize_t ahci_show_em_supported(struct device *dev,
116 struct device_attribute *attr, char *buf);
117static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
118
119static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
120static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
121static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
122static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
123static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
124 ahci_read_em_buffer, ahci_store_em_buffer);
125static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
126
127struct device_attribute *ahci_shost_attrs[] = {
128 &dev_attr_link_power_management_policy,
129 &dev_attr_em_message_type,
130 &dev_attr_em_message,
131 &dev_attr_ahci_host_caps,
132 &dev_attr_ahci_host_cap2,
133 &dev_attr_ahci_host_version,
134 &dev_attr_ahci_port_cmd,
135 &dev_attr_em_buffer,
136 &dev_attr_em_message_supported,
137 NULL
138};
139EXPORT_SYMBOL_GPL(ahci_shost_attrs);
140
141struct device_attribute *ahci_sdev_attrs[] = {
142 &dev_attr_sw_activity,
143 &dev_attr_unload_heads,
144 &dev_attr_ncq_prio_enable,
145 NULL
146};
147EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
148
149struct ata_port_operations ahci_ops = {
150 .inherits = &sata_pmp_port_ops,
151
152 .qc_defer = ahci_pmp_qc_defer,
153 .qc_prep = ahci_qc_prep,
154 .qc_issue = ahci_qc_issue,
155 .qc_fill_rtf = ahci_qc_fill_rtf,
156
157 .freeze = ahci_freeze,
158 .thaw = ahci_thaw,
159 .softreset = ahci_softreset,
160 .hardreset = ahci_hardreset,
161 .postreset = ahci_postreset,
162 .pmp_softreset = ahci_softreset,
163 .error_handler = ahci_error_handler,
164 .post_internal_cmd = ahci_post_internal_cmd,
165 .dev_config = ahci_dev_config,
166
167 .scr_read = ahci_scr_read,
168 .scr_write = ahci_scr_write,
169 .pmp_attach = ahci_pmp_attach,
170 .pmp_detach = ahci_pmp_detach,
171
172 .set_lpm = ahci_set_lpm,
173 .em_show = ahci_led_show,
174 .em_store = ahci_led_store,
175 .sw_activity_show = ahci_activity_show,
176 .sw_activity_store = ahci_activity_store,
177 .transmit_led_message = ahci_transmit_led_message,
178#ifdef CONFIG_PM
179 .port_suspend = ahci_port_suspend,
180 .port_resume = ahci_port_resume,
181#endif
182 .port_start = ahci_port_start,
183 .port_stop = ahci_port_stop,
184};
185EXPORT_SYMBOL_GPL(ahci_ops);
186
187struct ata_port_operations ahci_pmp_retry_srst_ops = {
188 .inherits = &ahci_ops,
189 .softreset = ahci_pmp_retry_softreset,
190};
191EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
192
193static bool ahci_em_messages __read_mostly = true;
194EXPORT_SYMBOL_GPL(ahci_em_messages);
195module_param(ahci_em_messages, bool, 0444);
196
197MODULE_PARM_DESC(ahci_em_messages,
198 "AHCI Enclosure Management Message control (0 = off, 1 = on)");
199
200
201static int devslp_idle_timeout __read_mostly = 1000;
202module_param(devslp_idle_timeout, int, 0644);
203MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
204
205static void ahci_enable_ahci(void __iomem *mmio)
206{
207 int i;
208 u32 tmp;
209
210
211 tmp = readl(mmio + HOST_CTL);
212 if (tmp & HOST_AHCI_EN)
213 return;
214
215
216
217
218 for (i = 0; i < 5; i++) {
219 tmp |= HOST_AHCI_EN;
220 writel(tmp, mmio + HOST_CTL);
221 tmp = readl(mmio + HOST_CTL);
222 if (tmp & HOST_AHCI_EN)
223 return;
224 msleep(10);
225 }
226
227 WARN_ON(1);
228}
229
230
231
232
233
234
235
236
237
238static int ahci_rpm_get_port(struct ata_port *ap)
239{
240 return pm_runtime_get_sync(ap->dev);
241}
242
243
244
245
246
247
248
249
250static void ahci_rpm_put_port(struct ata_port *ap)
251{
252 pm_runtime_put(ap->dev);
253}
254
255static ssize_t ahci_show_host_caps(struct device *dev,
256 struct device_attribute *attr, char *buf)
257{
258 struct Scsi_Host *shost = class_to_shost(dev);
259 struct ata_port *ap = ata_shost_to_port(shost);
260 struct ahci_host_priv *hpriv = ap->host->private_data;
261
262 return sprintf(buf, "%x\n", hpriv->cap);
263}
264
265static ssize_t ahci_show_host_cap2(struct device *dev,
266 struct device_attribute *attr, char *buf)
267{
268 struct Scsi_Host *shost = class_to_shost(dev);
269 struct ata_port *ap = ata_shost_to_port(shost);
270 struct ahci_host_priv *hpriv = ap->host->private_data;
271
272 return sprintf(buf, "%x\n", hpriv->cap2);
273}
274
275static ssize_t ahci_show_host_version(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct Scsi_Host *shost = class_to_shost(dev);
279 struct ata_port *ap = ata_shost_to_port(shost);
280 struct ahci_host_priv *hpriv = ap->host->private_data;
281
282 return sprintf(buf, "%x\n", hpriv->version);
283}
284
285static ssize_t ahci_show_port_cmd(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct Scsi_Host *shost = class_to_shost(dev);
289 struct ata_port *ap = ata_shost_to_port(shost);
290 void __iomem *port_mmio = ahci_port_base(ap);
291 ssize_t ret;
292
293 ahci_rpm_get_port(ap);
294 ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
295 ahci_rpm_put_port(ap);
296
297 return ret;
298}
299
300static ssize_t ahci_read_em_buffer(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
303 struct Scsi_Host *shost = class_to_shost(dev);
304 struct ata_port *ap = ata_shost_to_port(shost);
305 struct ahci_host_priv *hpriv = ap->host->private_data;
306 void __iomem *mmio = hpriv->mmio;
307 void __iomem *em_mmio = mmio + hpriv->em_loc;
308 u32 em_ctl, msg;
309 unsigned long flags;
310 size_t count;
311 int i;
312
313 ahci_rpm_get_port(ap);
314 spin_lock_irqsave(ap->lock, flags);
315
316 em_ctl = readl(mmio + HOST_EM_CTL);
317 if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
318 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
319 spin_unlock_irqrestore(ap->lock, flags);
320 ahci_rpm_put_port(ap);
321 return -EINVAL;
322 }
323
324 if (!(em_ctl & EM_CTL_MR)) {
325 spin_unlock_irqrestore(ap->lock, flags);
326 ahci_rpm_put_port(ap);
327 return -EAGAIN;
328 }
329
330 if (!(em_ctl & EM_CTL_SMB))
331 em_mmio += hpriv->em_buf_sz;
332
333 count = hpriv->em_buf_sz;
334
335
336 if (count > PAGE_SIZE) {
337 if (printk_ratelimit())
338 ata_port_warn(ap,
339 "EM read buffer size too large: "
340 "buffer size %u, page size %lu\n",
341 hpriv->em_buf_sz, PAGE_SIZE);
342 count = PAGE_SIZE;
343 }
344
345 for (i = 0; i < count; i += 4) {
346 msg = readl(em_mmio + i);
347 buf[i] = msg & 0xff;
348 buf[i + 1] = (msg >> 8) & 0xff;
349 buf[i + 2] = (msg >> 16) & 0xff;
350 buf[i + 3] = (msg >> 24) & 0xff;
351 }
352
353 spin_unlock_irqrestore(ap->lock, flags);
354 ahci_rpm_put_port(ap);
355
356 return i;
357}
358
359static ssize_t ahci_store_em_buffer(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t size)
362{
363 struct Scsi_Host *shost = class_to_shost(dev);
364 struct ata_port *ap = ata_shost_to_port(shost);
365 struct ahci_host_priv *hpriv = ap->host->private_data;
366 void __iomem *mmio = hpriv->mmio;
367 void __iomem *em_mmio = mmio + hpriv->em_loc;
368 const unsigned char *msg_buf = buf;
369 u32 em_ctl, msg;
370 unsigned long flags;
371 int i;
372
373
374 if (!(ap->flags & ATA_FLAG_EM) ||
375 !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
376 size % 4 || size > hpriv->em_buf_sz)
377 return -EINVAL;
378
379 ahci_rpm_get_port(ap);
380 spin_lock_irqsave(ap->lock, flags);
381
382 em_ctl = readl(mmio + HOST_EM_CTL);
383 if (em_ctl & EM_CTL_TM) {
384 spin_unlock_irqrestore(ap->lock, flags);
385 ahci_rpm_put_port(ap);
386 return -EBUSY;
387 }
388
389 for (i = 0; i < size; i += 4) {
390 msg = msg_buf[i] | msg_buf[i + 1] << 8 |
391 msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
392 writel(msg, em_mmio + i);
393 }
394
395 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
396
397 spin_unlock_irqrestore(ap->lock, flags);
398 ahci_rpm_put_port(ap);
399
400 return size;
401}
402
403static ssize_t ahci_show_em_supported(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
406 struct Scsi_Host *shost = class_to_shost(dev);
407 struct ata_port *ap = ata_shost_to_port(shost);
408 struct ahci_host_priv *hpriv = ap->host->private_data;
409 void __iomem *mmio = hpriv->mmio;
410 u32 em_ctl;
411
412 ahci_rpm_get_port(ap);
413 em_ctl = readl(mmio + HOST_EM_CTL);
414 ahci_rpm_put_port(ap);
415
416 return sprintf(buf, "%s%s%s%s\n",
417 em_ctl & EM_CTL_LED ? "led " : "",
418 em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
419 em_ctl & EM_CTL_SES ? "ses-2 " : "",
420 em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
421}
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
442{
443 void __iomem *mmio = hpriv->mmio;
444 u32 cap, cap2, vers, port_map;
445 int i;
446
447
448 ahci_enable_ahci(mmio);
449
450
451
452
453 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
454 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
455
456
457 vers = readl(mmio + HOST_VERSION);
458 if ((vers >> 16) > 1 ||
459 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
460 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
461 else
462 hpriv->saved_cap2 = cap2 = 0;
463
464
465 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
466 dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
467 cap &= ~HOST_CAP_64;
468 }
469
470 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
471 dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
472 cap &= ~HOST_CAP_NCQ;
473 }
474
475 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
476 dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
477 cap |= HOST_CAP_NCQ;
478 }
479
480 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
481 dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
482 cap &= ~HOST_CAP_PMP;
483 }
484
485 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
486 dev_info(dev,
487 "controller can't do SNTF, turning off CAP_SNTF\n");
488 cap &= ~HOST_CAP_SNTF;
489 }
490
491 if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
492 dev_info(dev,
493 "controller can't do DEVSLP, turning off\n");
494 cap2 &= ~HOST_CAP2_SDS;
495 cap2 &= ~HOST_CAP2_SADM;
496 }
497
498 if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
499 dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
500 cap |= HOST_CAP_FBS;
501 }
502
503 if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
504 dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
505 cap &= ~HOST_CAP_FBS;
506 }
507
508 if (!(cap & HOST_CAP_ALPM) && (hpriv->flags & AHCI_HFLAG_YES_ALPM)) {
509 dev_info(dev, "controller can do ALPM, turning on CAP_ALPM\n");
510 cap |= HOST_CAP_ALPM;
511 }
512
513 if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
514 dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
515 port_map, hpriv->force_port_map);
516 port_map = hpriv->force_port_map;
517 hpriv->saved_port_map = port_map;
518 }
519
520 if (hpriv->mask_port_map) {
521 dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
522 port_map,
523 port_map & hpriv->mask_port_map);
524 port_map &= hpriv->mask_port_map;
525 }
526
527
528 if (port_map) {
529 int map_ports = 0;
530
531 for (i = 0; i < AHCI_MAX_PORTS; i++)
532 if (port_map & (1 << i))
533 map_ports++;
534
535
536
537
538 if (map_ports > ahci_nr_ports(cap)) {
539 dev_warn(dev,
540 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
541 port_map, ahci_nr_ports(cap));
542 port_map = 0;
543 }
544 }
545
546
547 if (!port_map && vers < 0x10300) {
548 port_map = (1 << ahci_nr_ports(cap)) - 1;
549 dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
550
551
552 hpriv->saved_port_map = port_map;
553 }
554
555
556 hpriv->cap = cap;
557 hpriv->cap2 = cap2;
558 hpriv->version = readl(mmio + HOST_VERSION);
559 hpriv->port_map = port_map;
560
561 if (!hpriv->start_engine)
562 hpriv->start_engine = ahci_start_engine;
563
564 if (!hpriv->stop_engine)
565 hpriv->stop_engine = ahci_stop_engine;
566
567 if (!hpriv->irq_handler)
568 hpriv->irq_handler = ahci_single_level_irq_intr;
569}
570EXPORT_SYMBOL_GPL(ahci_save_initial_config);
571
572
573
574
575
576
577
578
579
580
581static void ahci_restore_initial_config(struct ata_host *host)
582{
583 struct ahci_host_priv *hpriv = host->private_data;
584 void __iomem *mmio = hpriv->mmio;
585
586 writel(hpriv->saved_cap, mmio + HOST_CAP);
587 if (hpriv->saved_cap2)
588 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
589 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
590 (void) readl(mmio + HOST_PORTS_IMPL);
591}
592
593static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
594{
595 static const int offset[] = {
596 [SCR_STATUS] = PORT_SCR_STAT,
597 [SCR_CONTROL] = PORT_SCR_CTL,
598 [SCR_ERROR] = PORT_SCR_ERR,
599 [SCR_ACTIVE] = PORT_SCR_ACT,
600 [SCR_NOTIFICATION] = PORT_SCR_NTF,
601 };
602 struct ahci_host_priv *hpriv = ap->host->private_data;
603
604 if (sc_reg < ARRAY_SIZE(offset) &&
605 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
606 return offset[sc_reg];
607 return 0;
608}
609
610static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
611{
612 void __iomem *port_mmio = ahci_port_base(link->ap);
613 int offset = ahci_scr_offset(link->ap, sc_reg);
614
615 if (offset) {
616 *val = readl(port_mmio + offset);
617 return 0;
618 }
619 return -EINVAL;
620}
621
622static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
623{
624 void __iomem *port_mmio = ahci_port_base(link->ap);
625 int offset = ahci_scr_offset(link->ap, sc_reg);
626
627 if (offset) {
628 writel(val, port_mmio + offset);
629 return 0;
630 }
631 return -EINVAL;
632}
633
634void ahci_start_engine(struct ata_port *ap)
635{
636 void __iomem *port_mmio = ahci_port_base(ap);
637 u32 tmp;
638
639
640 tmp = readl(port_mmio + PORT_CMD);
641 tmp |= PORT_CMD_START;
642 writel(tmp, port_mmio + PORT_CMD);
643 readl(port_mmio + PORT_CMD);
644}
645EXPORT_SYMBOL_GPL(ahci_start_engine);
646
647int ahci_stop_engine(struct ata_port *ap)
648{
649 void __iomem *port_mmio = ahci_port_base(ap);
650 struct ahci_host_priv *hpriv = ap->host->private_data;
651 u32 tmp;
652
653
654
655
656
657
658
659 if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
660 (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
661 ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
662 dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
663 return -EIO;
664 }
665
666 tmp = readl(port_mmio + PORT_CMD);
667
668
669 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
670 return 0;
671
672
673
674
675
676
677 if (tmp == 0xffffffff) {
678 dev_err(ap->host->dev, "AHCI controller unavailable!\n");
679 return -ENODEV;
680 }
681
682
683 tmp &= ~PORT_CMD_START;
684 writel(tmp, port_mmio + PORT_CMD);
685
686#ifdef CONFIG_ARM64
687
688 if (dev_is_pci(ap->host->dev) &&
689 to_pci_dev(ap->host->dev)->vendor == 0x14e4 &&
690 to_pci_dev(ap->host->dev)->device == 0x9027 &&
691 MIDR_IS_CPU_MODEL_RANGE(read_cpuid_id(),
692 MIDR_CPU_MODEL(ARM_CPU_IMP_BRCM, BRCM_CPU_PART_VULCAN),
693 MIDR_CPU_VAR_REV(0, 0),
694 MIDR_CPU_VAR_REV(0, MIDR_REVISION_MASK))) {
695 tmp = readl(hpriv->mmio + 0x8000);
696 udelay(100);
697 writel(tmp | (1 << 26), hpriv->mmio + 0x8000);
698 udelay(100);
699 writel(tmp & ~(1 << 26), hpriv->mmio + 0x8000);
700 dev_warn(ap->host->dev, "CN99XX SATA reset workaround applied\n");
701 }
702#endif
703
704
705 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
706 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
707 if (tmp & PORT_CMD_LIST_ON)
708 return -EIO;
709
710 return 0;
711}
712EXPORT_SYMBOL_GPL(ahci_stop_engine);
713
714void ahci_start_fis_rx(struct ata_port *ap)
715{
716 void __iomem *port_mmio = ahci_port_base(ap);
717 struct ahci_host_priv *hpriv = ap->host->private_data;
718 struct ahci_port_priv *pp = ap->private_data;
719 u32 tmp;
720
721
722 if (hpriv->cap & HOST_CAP_64)
723 writel((pp->cmd_slot_dma >> 16) >> 16,
724 port_mmio + PORT_LST_ADDR_HI);
725 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
726
727 if (hpriv->cap & HOST_CAP_64)
728 writel((pp->rx_fis_dma >> 16) >> 16,
729 port_mmio + PORT_FIS_ADDR_HI);
730 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
731
732
733 tmp = readl(port_mmio + PORT_CMD);
734 tmp |= PORT_CMD_FIS_RX;
735 writel(tmp, port_mmio + PORT_CMD);
736
737
738 readl(port_mmio + PORT_CMD);
739}
740EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
741
742static int ahci_stop_fis_rx(struct ata_port *ap)
743{
744 void __iomem *port_mmio = ahci_port_base(ap);
745 u32 tmp;
746
747
748 tmp = readl(port_mmio + PORT_CMD);
749 tmp &= ~PORT_CMD_FIS_RX;
750 writel(tmp, port_mmio + PORT_CMD);
751
752
753 tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
754 PORT_CMD_FIS_ON, 10, 1000);
755 if (tmp & PORT_CMD_FIS_ON)
756 return -EBUSY;
757
758 return 0;
759}
760
761static void ahci_power_up(struct ata_port *ap)
762{
763 struct ahci_host_priv *hpriv = ap->host->private_data;
764 void __iomem *port_mmio = ahci_port_base(ap);
765 u32 cmd;
766
767 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
768
769
770 if (hpriv->cap & HOST_CAP_SSS) {
771 cmd |= PORT_CMD_SPIN_UP;
772 writel(cmd, port_mmio + PORT_CMD);
773 }
774
775
776 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
777}
778
779static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
780 unsigned int hints)
781{
782 struct ata_port *ap = link->ap;
783 struct ahci_host_priv *hpriv = ap->host->private_data;
784 struct ahci_port_priv *pp = ap->private_data;
785 void __iomem *port_mmio = ahci_port_base(ap);
786
787 if (policy != ATA_LPM_MAX_POWER) {
788
789 hints &= ~ATA_LPM_WAKE_ONLY;
790
791
792
793
794
795
796 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
797 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
798
799 sata_link_scr_lpm(link, policy, false);
800 }
801
802 if (hpriv->cap & HOST_CAP_ALPM) {
803 u32 cmd = readl(port_mmio + PORT_CMD);
804
805 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
806 if (!(hints & ATA_LPM_WAKE_ONLY))
807 cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
808 cmd |= PORT_CMD_ICC_ACTIVE;
809
810 writel(cmd, port_mmio + PORT_CMD);
811 readl(port_mmio + PORT_CMD);
812
813
814 ata_msleep(ap, 10);
815
816 if (hints & ATA_LPM_WAKE_ONLY)
817 return 0;
818 } else {
819 cmd |= PORT_CMD_ALPE;
820 if (policy == ATA_LPM_MIN_POWER)
821 cmd |= PORT_CMD_ASP;
822
823
824 writel(cmd, port_mmio + PORT_CMD);
825 }
826 }
827
828
829 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
830 (hpriv->cap2 & HOST_CAP2_SADM) &&
831 (link->device->flags & ATA_DFLAG_DEVSLP)) {
832 if (policy == ATA_LPM_MIN_POWER)
833 ahci_set_aggressive_devslp(ap, true);
834 else
835 ahci_set_aggressive_devslp(ap, false);
836 }
837
838 if (policy == ATA_LPM_MAX_POWER) {
839 sata_link_scr_lpm(link, policy, false);
840
841
842 pp->intr_mask |= PORT_IRQ_PHYRDY;
843 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
844 }
845
846 return 0;
847}
848
849#ifdef CONFIG_PM
850static void ahci_power_down(struct ata_port *ap)
851{
852 struct ahci_host_priv *hpriv = ap->host->private_data;
853 void __iomem *port_mmio = ahci_port_base(ap);
854 u32 cmd, scontrol;
855
856 if (!(hpriv->cap & HOST_CAP_SSS))
857 return;
858
859
860 scontrol = readl(port_mmio + PORT_SCR_CTL);
861 scontrol &= ~0xf;
862 writel(scontrol, port_mmio + PORT_SCR_CTL);
863
864
865 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
866 cmd &= ~PORT_CMD_SPIN_UP;
867 writel(cmd, port_mmio + PORT_CMD);
868}
869#endif
870
871static void ahci_start_port(struct ata_port *ap)
872{
873 struct ahci_host_priv *hpriv = ap->host->private_data;
874 struct ahci_port_priv *pp = ap->private_data;
875 struct ata_link *link;
876 struct ahci_em_priv *emp;
877 ssize_t rc;
878 int i;
879
880
881 ahci_start_fis_rx(ap);
882
883
884 if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
885 hpriv->start_engine(ap);
886
887
888 if (ap->flags & ATA_FLAG_EM) {
889 ata_for_each_link(link, ap, EDGE) {
890 emp = &pp->em_priv[link->pmp];
891
892
893 for (i = 0; i < EM_MAX_RETRY; i++) {
894 rc = ap->ops->transmit_led_message(ap,
895 emp->led_state,
896 4);
897
898
899
900
901
902
903
904
905 if (rc == -EBUSY)
906 msleep(1);
907 else
908 break;
909 }
910 }
911 }
912
913 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
914 ata_for_each_link(link, ap, EDGE)
915 ahci_init_sw_activity(link);
916
917}
918
919static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
920{
921 int rc;
922 struct ahci_host_priv *hpriv = ap->host->private_data;
923
924
925 rc = hpriv->stop_engine(ap);
926 if (rc) {
927 *emsg = "failed to stop engine";
928 return rc;
929 }
930
931
932 rc = ahci_stop_fis_rx(ap);
933 if (rc) {
934 *emsg = "failed stop FIS RX";
935 return rc;
936 }
937
938 return 0;
939}
940
941int ahci_reset_controller(struct ata_host *host)
942{
943 struct ahci_host_priv *hpriv = host->private_data;
944 void __iomem *mmio = hpriv->mmio;
945 u32 tmp;
946
947
948
949
950 ahci_enable_ahci(mmio);
951
952
953 if (!ahci_skip_host_reset) {
954 tmp = readl(mmio + HOST_CTL);
955 if ((tmp & HOST_RESET) == 0) {
956 writel(tmp | HOST_RESET, mmio + HOST_CTL);
957 readl(mmio + HOST_CTL);
958 }
959
960
961
962
963
964
965
966 tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
967 HOST_RESET, 10, 1000);
968
969 if (tmp & HOST_RESET) {
970 dev_err(host->dev, "controller reset failed (0x%x)\n",
971 tmp);
972 return -EIO;
973 }
974
975
976 ahci_enable_ahci(mmio);
977
978
979
980
981 if (!(hpriv->flags & AHCI_HFLAG_NO_WRITE_TO_RO))
982 ahci_restore_initial_config(host);
983 } else
984 dev_info(host->dev, "skipping global host reset\n");
985
986 return 0;
987}
988EXPORT_SYMBOL_GPL(ahci_reset_controller);
989
990static void ahci_sw_activity(struct ata_link *link)
991{
992 struct ata_port *ap = link->ap;
993 struct ahci_port_priv *pp = ap->private_data;
994 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
995
996 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
997 return;
998
999 emp->activity++;
1000 if (!timer_pending(&emp->timer))
1001 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
1002}
1003
1004static void ahci_sw_activity_blink(struct timer_list *t)
1005{
1006 struct ahci_em_priv *emp = from_timer(emp, t, timer);
1007 struct ata_link *link = emp->link;
1008 struct ata_port *ap = link->ap;
1009
1010 unsigned long led_message = emp->led_state;
1011 u32 activity_led_state;
1012 unsigned long flags;
1013
1014 led_message &= EM_MSG_LED_VALUE;
1015 led_message |= ap->port_no | (link->pmp << 8);
1016
1017
1018
1019
1020
1021 spin_lock_irqsave(ap->lock, flags);
1022 if (emp->saved_activity != emp->activity) {
1023 emp->saved_activity = emp->activity;
1024
1025 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
1026
1027 if (activity_led_state)
1028 activity_led_state = 0;
1029 else
1030 activity_led_state = 1;
1031
1032
1033 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1034
1035
1036 led_message |= (activity_led_state << 16);
1037 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1038 } else {
1039
1040 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1041 if (emp->blink_policy == BLINK_OFF)
1042 led_message |= (1 << 16);
1043 }
1044 spin_unlock_irqrestore(ap->lock, flags);
1045 ap->ops->transmit_led_message(ap, led_message, 4);
1046}
1047
1048static void ahci_init_sw_activity(struct ata_link *link)
1049{
1050 struct ata_port *ap = link->ap;
1051 struct ahci_port_priv *pp = ap->private_data;
1052 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1053
1054
1055 emp->saved_activity = emp->activity = 0;
1056 emp->link = link;
1057 timer_setup(&emp->timer, ahci_sw_activity_blink, 0);
1058
1059
1060 if (emp->blink_policy)
1061 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1062}
1063
1064int ahci_reset_em(struct ata_host *host)
1065{
1066 struct ahci_host_priv *hpriv = host->private_data;
1067 void __iomem *mmio = hpriv->mmio;
1068 u32 em_ctl;
1069
1070 em_ctl = readl(mmio + HOST_EM_CTL);
1071 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1072 return -EINVAL;
1073
1074 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1075 return 0;
1076}
1077EXPORT_SYMBOL_GPL(ahci_reset_em);
1078
1079static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1080 ssize_t size)
1081{
1082 struct ahci_host_priv *hpriv = ap->host->private_data;
1083 struct ahci_port_priv *pp = ap->private_data;
1084 void __iomem *mmio = hpriv->mmio;
1085 u32 em_ctl;
1086 u32 message[] = {0, 0};
1087 unsigned long flags;
1088 int pmp;
1089 struct ahci_em_priv *emp;
1090
1091
1092 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1093 if (pmp < EM_MAX_SLOTS)
1094 emp = &pp->em_priv[pmp];
1095 else
1096 return -EINVAL;
1097
1098 ahci_rpm_get_port(ap);
1099 spin_lock_irqsave(ap->lock, flags);
1100
1101
1102
1103
1104
1105 em_ctl = readl(mmio + HOST_EM_CTL);
1106 if (em_ctl & EM_CTL_TM) {
1107 spin_unlock_irqrestore(ap->lock, flags);
1108 ahci_rpm_put_port(ap);
1109 return -EBUSY;
1110 }
1111
1112 if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1113
1114
1115
1116
1117 message[0] |= (4 << 8);
1118
1119
1120 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
1121
1122
1123 writel(message[0], mmio + hpriv->em_loc);
1124 writel(message[1], mmio + hpriv->em_loc+4);
1125
1126
1127
1128
1129 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1130 }
1131
1132
1133 emp->led_state = state;
1134
1135 spin_unlock_irqrestore(ap->lock, flags);
1136 ahci_rpm_put_port(ap);
1137
1138 return size;
1139}
1140
1141static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1142{
1143 struct ahci_port_priv *pp = ap->private_data;
1144 struct ata_link *link;
1145 struct ahci_em_priv *emp;
1146 int rc = 0;
1147
1148 ata_for_each_link(link, ap, EDGE) {
1149 emp = &pp->em_priv[link->pmp];
1150 rc += sprintf(buf, "%lx\n", emp->led_state);
1151 }
1152 return rc;
1153}
1154
1155static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1156 size_t size)
1157{
1158 unsigned int state;
1159 int pmp;
1160 struct ahci_port_priv *pp = ap->private_data;
1161 struct ahci_em_priv *emp;
1162
1163 if (kstrtouint(buf, 0, &state) < 0)
1164 return -EINVAL;
1165
1166
1167 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1168 if (pmp < EM_MAX_SLOTS) {
1169 pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
1170 emp = &pp->em_priv[pmp];
1171 } else {
1172 return -EINVAL;
1173 }
1174
1175
1176
1177
1178
1179 if (emp->blink_policy)
1180 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1181
1182 return ap->ops->transmit_led_message(ap, state, size);
1183}
1184
1185static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1186{
1187 struct ata_link *link = dev->link;
1188 struct ata_port *ap = link->ap;
1189 struct ahci_port_priv *pp = ap->private_data;
1190 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1191 u32 port_led_state = emp->led_state;
1192
1193
1194 if (val == OFF) {
1195
1196 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1197
1198
1199 port_led_state &= EM_MSG_LED_VALUE_OFF;
1200 port_led_state |= (ap->port_no | (link->pmp << 8));
1201 ap->ops->transmit_led_message(ap, port_led_state, 4);
1202 } else {
1203 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1204 if (val == BLINK_OFF) {
1205
1206 port_led_state &= EM_MSG_LED_VALUE_OFF;
1207 port_led_state |= (ap->port_no | (link->pmp << 8));
1208 port_led_state |= EM_MSG_LED_VALUE_ON;
1209 ap->ops->transmit_led_message(ap, port_led_state, 4);
1210 }
1211 }
1212 emp->blink_policy = val;
1213 return 0;
1214}
1215
1216static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1217{
1218 struct ata_link *link = dev->link;
1219 struct ata_port *ap = link->ap;
1220 struct ahci_port_priv *pp = ap->private_data;
1221 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1222
1223
1224
1225
1226 return sprintf(buf, "%d\n", emp->blink_policy);
1227}
1228
1229static void ahci_port_init(struct device *dev, struct ata_port *ap,
1230 int port_no, void __iomem *mmio,
1231 void __iomem *port_mmio)
1232{
1233 struct ahci_host_priv *hpriv = ap->host->private_data;
1234 const char *emsg = NULL;
1235 int rc;
1236 u32 tmp;
1237
1238
1239 rc = ahci_deinit_port(ap, &emsg);
1240 if (rc)
1241 dev_warn(dev, "%s (%d)\n", emsg, rc);
1242
1243
1244 tmp = readl(port_mmio + PORT_SCR_ERR);
1245 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1246 writel(tmp, port_mmio + PORT_SCR_ERR);
1247
1248
1249 tmp = readl(port_mmio + PORT_IRQ_STAT);
1250 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1251 if (tmp)
1252 writel(tmp, port_mmio + PORT_IRQ_STAT);
1253
1254 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1255
1256
1257 tmp = readl(port_mmio + PORT_CMD);
1258 if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
1259 ap->pflags |= ATA_PFLAG_EXTERNAL;
1260}
1261
1262void ahci_init_controller(struct ata_host *host)
1263{
1264 struct ahci_host_priv *hpriv = host->private_data;
1265 void __iomem *mmio = hpriv->mmio;
1266 int i;
1267 void __iomem *port_mmio;
1268 u32 tmp;
1269
1270 for (i = 0; i < host->n_ports; i++) {
1271 struct ata_port *ap = host->ports[i];
1272
1273 port_mmio = ahci_port_base(ap);
1274 if (ata_port_is_dummy(ap))
1275 continue;
1276
1277 ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1278 }
1279
1280 tmp = readl(mmio + HOST_CTL);
1281 VPRINTK("HOST_CTL 0x%x\n", tmp);
1282 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1283 tmp = readl(mmio + HOST_CTL);
1284 VPRINTK("HOST_CTL 0x%x\n", tmp);
1285}
1286EXPORT_SYMBOL_GPL(ahci_init_controller);
1287
1288static void ahci_dev_config(struct ata_device *dev)
1289{
1290 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1291
1292 if (hpriv->flags & AHCI_HFLAG_SECT255) {
1293 dev->max_sectors = 255;
1294 ata_dev_info(dev,
1295 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1296 }
1297}
1298
1299unsigned int ahci_dev_classify(struct ata_port *ap)
1300{
1301 void __iomem *port_mmio = ahci_port_base(ap);
1302 struct ata_taskfile tf;
1303 u32 tmp;
1304
1305 tmp = readl(port_mmio + PORT_SIG);
1306 tf.lbah = (tmp >> 24) & 0xff;
1307 tf.lbam = (tmp >> 16) & 0xff;
1308 tf.lbal = (tmp >> 8) & 0xff;
1309 tf.nsect = (tmp) & 0xff;
1310
1311 return ata_dev_classify(&tf);
1312}
1313EXPORT_SYMBOL_GPL(ahci_dev_classify);
1314
1315void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1316 u32 opts)
1317{
1318 dma_addr_t cmd_tbl_dma;
1319
1320 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1321
1322 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1323 pp->cmd_slot[tag].status = 0;
1324 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1325 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1326}
1327EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
1328
1329int ahci_kick_engine(struct ata_port *ap)
1330{
1331 void __iomem *port_mmio = ahci_port_base(ap);
1332 struct ahci_host_priv *hpriv = ap->host->private_data;
1333 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1334 u32 tmp;
1335 int busy, rc;
1336
1337
1338 rc = hpriv->stop_engine(ap);
1339 if (rc)
1340 goto out_restart;
1341
1342
1343
1344
1345 busy = status & (ATA_BUSY | ATA_DRQ);
1346 if (!busy && !sata_pmp_attached(ap)) {
1347 rc = 0;
1348 goto out_restart;
1349 }
1350
1351 if (!(hpriv->cap & HOST_CAP_CLO)) {
1352 rc = -EOPNOTSUPP;
1353 goto out_restart;
1354 }
1355
1356
1357 tmp = readl(port_mmio + PORT_CMD);
1358 tmp |= PORT_CMD_CLO;
1359 writel(tmp, port_mmio + PORT_CMD);
1360
1361 rc = 0;
1362 tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
1363 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1364 if (tmp & PORT_CMD_CLO)
1365 rc = -EIO;
1366
1367
1368 out_restart:
1369 hpriv->start_engine(ap);
1370 return rc;
1371}
1372EXPORT_SYMBOL_GPL(ahci_kick_engine);
1373
1374static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1375 struct ata_taskfile *tf, int is_cmd, u16 flags,
1376 unsigned long timeout_msec)
1377{
1378 const u32 cmd_fis_len = 5;
1379 struct ahci_port_priv *pp = ap->private_data;
1380 void __iomem *port_mmio = ahci_port_base(ap);
1381 u8 *fis = pp->cmd_tbl;
1382 u32 tmp;
1383
1384
1385 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1386 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1387
1388
1389 if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1390 tmp = readl(port_mmio + PORT_FBS);
1391 tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1392 tmp |= pmp << PORT_FBS_DEV_OFFSET;
1393 writel(tmp, port_mmio + PORT_FBS);
1394 pp->fbs_last_dev = pmp;
1395 }
1396
1397
1398 writel(1, port_mmio + PORT_CMD_ISSUE);
1399
1400 if (timeout_msec) {
1401 tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1402 0x1, 0x1, 1, timeout_msec);
1403 if (tmp & 0x1) {
1404 ahci_kick_engine(ap);
1405 return -EBUSY;
1406 }
1407 } else
1408 readl(port_mmio + PORT_CMD_ISSUE);
1409
1410 return 0;
1411}
1412
1413int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1414 int pmp, unsigned long deadline,
1415 int (*check_ready)(struct ata_link *link))
1416{
1417 struct ata_port *ap = link->ap;
1418 struct ahci_host_priv *hpriv = ap->host->private_data;
1419 struct ahci_port_priv *pp = ap->private_data;
1420 const char *reason = NULL;
1421 unsigned long now, msecs;
1422 struct ata_taskfile tf;
1423 bool fbs_disabled = false;
1424 int rc;
1425
1426 DPRINTK("ENTER\n");
1427
1428
1429 rc = ahci_kick_engine(ap);
1430 if (rc && rc != -EOPNOTSUPP)
1431 ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
1432
1433
1434
1435
1436
1437
1438 if (!ata_is_host_link(link) && pp->fbs_enabled) {
1439 ahci_disable_fbs(ap);
1440 fbs_disabled = true;
1441 }
1442
1443 ata_tf_init(link->device, &tf);
1444
1445
1446 msecs = 0;
1447 now = jiffies;
1448 if (time_after(deadline, now))
1449 msecs = jiffies_to_msecs(deadline - now);
1450
1451 tf.ctl |= ATA_SRST;
1452 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1453 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1454 rc = -EIO;
1455 reason = "1st FIS failed";
1456 goto fail;
1457 }
1458
1459
1460 ata_msleep(ap, 1);
1461
1462
1463 tf.ctl &= ~ATA_SRST;
1464 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1465
1466
1467 rc = ata_wait_after_reset(link, deadline, check_ready);
1468 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1469
1470
1471
1472
1473
1474 ata_link_info(link, "device not ready, treating as offline\n");
1475 *class = ATA_DEV_NONE;
1476 } else if (rc) {
1477
1478 reason = "device not ready";
1479 goto fail;
1480 } else
1481 *class = ahci_dev_classify(ap);
1482
1483
1484 if (fbs_disabled)
1485 ahci_enable_fbs(ap);
1486
1487 DPRINTK("EXIT, class=%u\n", *class);
1488 return 0;
1489
1490 fail:
1491 ata_link_err(link, "softreset failed (%s)\n", reason);
1492 return rc;
1493}
1494
1495int ahci_check_ready(struct ata_link *link)
1496{
1497 void __iomem *port_mmio = ahci_port_base(link->ap);
1498 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1499
1500 return ata_check_ready(status);
1501}
1502EXPORT_SYMBOL_GPL(ahci_check_ready);
1503
1504static int ahci_softreset(struct ata_link *link, unsigned int *class,
1505 unsigned long deadline)
1506{
1507 int pmp = sata_srst_pmp(link);
1508
1509 DPRINTK("ENTER\n");
1510
1511 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1512}
1513EXPORT_SYMBOL_GPL(ahci_do_softreset);
1514
1515static int ahci_bad_pmp_check_ready(struct ata_link *link)
1516{
1517 void __iomem *port_mmio = ahci_port_base(link->ap);
1518 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1519 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1520
1521
1522
1523
1524
1525 if (irq_status & PORT_IRQ_BAD_PMP)
1526 return -EIO;
1527
1528 return ata_check_ready(status);
1529}
1530
1531static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1532 unsigned long deadline)
1533{
1534 struct ata_port *ap = link->ap;
1535 void __iomem *port_mmio = ahci_port_base(ap);
1536 int pmp = sata_srst_pmp(link);
1537 int rc;
1538 u32 irq_sts;
1539
1540 DPRINTK("ENTER\n");
1541
1542 rc = ahci_do_softreset(link, class, pmp, deadline,
1543 ahci_bad_pmp_check_ready);
1544
1545
1546
1547
1548
1549
1550 if (rc == -EIO) {
1551 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1552 if (irq_sts & PORT_IRQ_BAD_PMP) {
1553 ata_link_warn(link,
1554 "applying PMP SRST workaround "
1555 "and retrying\n");
1556 rc = ahci_do_softreset(link, class, 0, deadline,
1557 ahci_check_ready);
1558 }
1559 }
1560
1561 return rc;
1562}
1563
1564int ahci_do_hardreset(struct ata_link *link, unsigned int *class,
1565 unsigned long deadline, bool *online)
1566{
1567 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1568 struct ata_port *ap = link->ap;
1569 struct ahci_port_priv *pp = ap->private_data;
1570 struct ahci_host_priv *hpriv = ap->host->private_data;
1571 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1572 struct ata_taskfile tf;
1573 int rc;
1574
1575 DPRINTK("ENTER\n");
1576
1577 hpriv->stop_engine(ap);
1578
1579
1580 ata_tf_init(link->device, &tf);
1581 tf.command = ATA_BUSY;
1582 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1583
1584 rc = sata_link_hardreset(link, timing, deadline, online,
1585 ahci_check_ready);
1586
1587 hpriv->start_engine(ap);
1588
1589 if (*online)
1590 *class = ahci_dev_classify(ap);
1591
1592 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1593 return rc;
1594}
1595EXPORT_SYMBOL_GPL(ahci_do_hardreset);
1596
1597static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1598 unsigned long deadline)
1599{
1600 bool online;
1601
1602 return ahci_do_hardreset(link, class, deadline, &online);
1603}
1604
1605static void ahci_postreset(struct ata_link *link, unsigned int *class)
1606{
1607 struct ata_port *ap = link->ap;
1608 void __iomem *port_mmio = ahci_port_base(ap);
1609 u32 new_tmp, tmp;
1610
1611 ata_std_postreset(link, class);
1612
1613
1614 new_tmp = tmp = readl(port_mmio + PORT_CMD);
1615 if (*class == ATA_DEV_ATAPI)
1616 new_tmp |= PORT_CMD_ATAPI;
1617 else
1618 new_tmp &= ~PORT_CMD_ATAPI;
1619 if (new_tmp != tmp) {
1620 writel(new_tmp, port_mmio + PORT_CMD);
1621 readl(port_mmio + PORT_CMD);
1622 }
1623}
1624
1625static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1626{
1627 struct scatterlist *sg;
1628 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1629 unsigned int si;
1630
1631 VPRINTK("ENTER\n");
1632
1633
1634
1635
1636 for_each_sg(qc->sg, sg, qc->n_elem, si) {
1637 dma_addr_t addr = sg_dma_address(sg);
1638 u32 sg_len = sg_dma_len(sg);
1639
1640 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1641 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1642 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1643 }
1644
1645 return si;
1646}
1647
1648static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1649{
1650 struct ata_port *ap = qc->ap;
1651 struct ahci_port_priv *pp = ap->private_data;
1652
1653 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1654 return ata_std_qc_defer(qc);
1655 else
1656 return sata_pmp_qc_defer_cmd_switch(qc);
1657}
1658
1659static void ahci_qc_prep(struct ata_queued_cmd *qc)
1660{
1661 struct ata_port *ap = qc->ap;
1662 struct ahci_port_priv *pp = ap->private_data;
1663 int is_atapi = ata_is_atapi(qc->tf.protocol);
1664 void *cmd_tbl;
1665 u32 opts;
1666 const u32 cmd_fis_len = 5;
1667 unsigned int n_elem;
1668
1669
1670
1671
1672
1673 cmd_tbl = pp->cmd_tbl + qc->hw_tag * AHCI_CMD_TBL_SZ;
1674
1675 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1676 if (is_atapi) {
1677 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1678 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1679 }
1680
1681 n_elem = 0;
1682 if (qc->flags & ATA_QCFLAG_DMAMAP)
1683 n_elem = ahci_fill_sg(qc, cmd_tbl);
1684
1685
1686
1687
1688 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1689 if (qc->tf.flags & ATA_TFLAG_WRITE)
1690 opts |= AHCI_CMD_WRITE;
1691 if (is_atapi)
1692 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1693
1694 ahci_fill_cmd_slot(pp, qc->hw_tag, opts);
1695}
1696
1697static void ahci_fbs_dec_intr(struct ata_port *ap)
1698{
1699 struct ahci_port_priv *pp = ap->private_data;
1700 void __iomem *port_mmio = ahci_port_base(ap);
1701 u32 fbs = readl(port_mmio + PORT_FBS);
1702 int retries = 3;
1703
1704 DPRINTK("ENTER\n");
1705 BUG_ON(!pp->fbs_enabled);
1706
1707
1708
1709
1710 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1711 fbs = readl(port_mmio + PORT_FBS);
1712 while ((fbs & PORT_FBS_DEC) && retries--) {
1713 udelay(1);
1714 fbs = readl(port_mmio + PORT_FBS);
1715 }
1716
1717 if (fbs & PORT_FBS_DEC)
1718 dev_err(ap->host->dev, "failed to clear device error\n");
1719}
1720
1721static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1722{
1723 struct ahci_host_priv *hpriv = ap->host->private_data;
1724 struct ahci_port_priv *pp = ap->private_data;
1725 struct ata_eh_info *host_ehi = &ap->link.eh_info;
1726 struct ata_link *link = NULL;
1727 struct ata_queued_cmd *active_qc;
1728 struct ata_eh_info *active_ehi;
1729 bool fbs_need_dec = false;
1730 u32 serror;
1731
1732
1733 if (pp->fbs_enabled) {
1734 void __iomem *port_mmio = ahci_port_base(ap);
1735 u32 fbs = readl(port_mmio + PORT_FBS);
1736 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1737
1738 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
1739 link = &ap->pmp_link[pmp];
1740 fbs_need_dec = true;
1741 }
1742
1743 } else
1744 ata_for_each_link(link, ap, EDGE)
1745 if (ata_link_active(link))
1746 break;
1747
1748 if (!link)
1749 link = &ap->link;
1750
1751 active_qc = ata_qc_from_tag(ap, link->active_tag);
1752 active_ehi = &link->eh_info;
1753
1754
1755 ata_ehi_clear_desc(host_ehi);
1756 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1757
1758
1759 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1760 ahci_scr_write(&ap->link, SCR_ERROR, serror);
1761 host_ehi->serror |= serror;
1762
1763
1764 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1765 irq_stat &= ~PORT_IRQ_IF_ERR;
1766
1767 if (irq_stat & PORT_IRQ_TF_ERR) {
1768
1769
1770
1771
1772 if (active_qc)
1773 active_qc->err_mask |= AC_ERR_DEV;
1774 else
1775 active_ehi->err_mask |= AC_ERR_DEV;
1776
1777 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1778 host_ehi->serror &= ~SERR_INTERNAL;
1779 }
1780
1781 if (irq_stat & PORT_IRQ_UNK_FIS) {
1782 u32 *unk = pp->rx_fis + RX_FIS_UNK;
1783
1784 active_ehi->err_mask |= AC_ERR_HSM;
1785 active_ehi->action |= ATA_EH_RESET;
1786 ata_ehi_push_desc(active_ehi,
1787 "unknown FIS %08x %08x %08x %08x" ,
1788 unk[0], unk[1], unk[2], unk[3]);
1789 }
1790
1791 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1792 active_ehi->err_mask |= AC_ERR_HSM;
1793 active_ehi->action |= ATA_EH_RESET;
1794 ata_ehi_push_desc(active_ehi, "incorrect PMP");
1795 }
1796
1797 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1798 host_ehi->err_mask |= AC_ERR_HOST_BUS;
1799 host_ehi->action |= ATA_EH_RESET;
1800 ata_ehi_push_desc(host_ehi, "host bus error");
1801 }
1802
1803 if (irq_stat & PORT_IRQ_IF_ERR) {
1804 if (fbs_need_dec)
1805 active_ehi->err_mask |= AC_ERR_DEV;
1806 else {
1807 host_ehi->err_mask |= AC_ERR_ATA_BUS;
1808 host_ehi->action |= ATA_EH_RESET;
1809 }
1810
1811 ata_ehi_push_desc(host_ehi, "interface fatal error");
1812 }
1813
1814 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1815 ata_ehi_hotplugged(host_ehi);
1816 ata_ehi_push_desc(host_ehi, "%s",
1817 irq_stat & PORT_IRQ_CONNECT ?
1818 "connection status changed" : "PHY RDY changed");
1819 }
1820
1821
1822
1823 if (irq_stat & PORT_IRQ_FREEZE)
1824 ata_port_freeze(ap);
1825 else if (fbs_need_dec) {
1826 ata_link_abort(link);
1827 ahci_fbs_dec_intr(ap);
1828 } else
1829 ata_port_abort(ap);
1830}
1831
1832static void ahci_handle_port_interrupt(struct ata_port *ap,
1833 void __iomem *port_mmio, u32 status)
1834{
1835 struct ata_eh_info *ehi = &ap->link.eh_info;
1836 struct ahci_port_priv *pp = ap->private_data;
1837 struct ahci_host_priv *hpriv = ap->host->private_data;
1838 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1839 u32 qc_active = 0;
1840 int rc;
1841
1842
1843 if (unlikely(resetting))
1844 status &= ~PORT_IRQ_BAD_PMP;
1845
1846 if (sata_lpm_ignore_phy_events(&ap->link)) {
1847 status &= ~PORT_IRQ_PHYRDY;
1848 ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
1849 }
1850
1851 if (unlikely(status & PORT_IRQ_ERROR)) {
1852 ahci_error_intr(ap, status);
1853 return;
1854 }
1855
1856 if (status & PORT_IRQ_SDB_FIS) {
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866 if (hpriv->cap & HOST_CAP_SNTF)
1867 sata_async_notification(ap);
1868 else {
1869
1870
1871
1872
1873
1874
1875
1876
1877 if (pp->fbs_enabled)
1878 WARN_ON_ONCE(1);
1879 else {
1880 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1881 u32 f0 = le32_to_cpu(f[0]);
1882 if (f0 & (1 << 15))
1883 sata_async_notification(ap);
1884 }
1885 }
1886 }
1887
1888
1889
1890
1891
1892 if (pp->fbs_enabled) {
1893 if (ap->qc_active) {
1894 qc_active = readl(port_mmio + PORT_SCR_ACT);
1895 qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1896 }
1897 } else {
1898
1899 if (ap->qc_active && pp->active_link->sactive)
1900 qc_active = readl(port_mmio + PORT_SCR_ACT);
1901 else
1902 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1903 }
1904
1905
1906 rc = ata_qc_complete_multiple(ap, qc_active);
1907
1908
1909 if (unlikely(rc < 0 && !resetting)) {
1910 ehi->err_mask |= AC_ERR_HSM;
1911 ehi->action |= ATA_EH_RESET;
1912 ata_port_freeze(ap);
1913 }
1914}
1915
1916static void ahci_port_intr(struct ata_port *ap)
1917{
1918 void __iomem *port_mmio = ahci_port_base(ap);
1919 u32 status;
1920
1921 status = readl(port_mmio + PORT_IRQ_STAT);
1922 writel(status, port_mmio + PORT_IRQ_STAT);
1923
1924 ahci_handle_port_interrupt(ap, port_mmio, status);
1925}
1926
1927static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
1928{
1929 struct ata_port *ap = dev_instance;
1930 void __iomem *port_mmio = ahci_port_base(ap);
1931 u32 status;
1932
1933 VPRINTK("ENTER\n");
1934
1935 status = readl(port_mmio + PORT_IRQ_STAT);
1936 writel(status, port_mmio + PORT_IRQ_STAT);
1937
1938 spin_lock(ap->lock);
1939 ahci_handle_port_interrupt(ap, port_mmio, status);
1940 spin_unlock(ap->lock);
1941
1942 VPRINTK("EXIT\n");
1943
1944 return IRQ_HANDLED;
1945}
1946
1947u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
1948{
1949 unsigned int i, handled = 0;
1950
1951 for (i = 0; i < host->n_ports; i++) {
1952 struct ata_port *ap;
1953
1954 if (!(irq_masked & (1 << i)))
1955 continue;
1956
1957 ap = host->ports[i];
1958 if (ap) {
1959 ahci_port_intr(ap);
1960 VPRINTK("port %u\n", i);
1961 } else {
1962 VPRINTK("port %u (no irq)\n", i);
1963 if (ata_ratelimit())
1964 dev_warn(host->dev,
1965 "interrupt on disabled port %u\n", i);
1966 }
1967
1968 handled = 1;
1969 }
1970
1971 return handled;
1972}
1973EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
1974
1975static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1976{
1977 struct ata_host *host = dev_instance;
1978 struct ahci_host_priv *hpriv;
1979 unsigned int rc = 0;
1980 void __iomem *mmio;
1981 u32 irq_stat, irq_masked;
1982
1983 VPRINTK("ENTER\n");
1984
1985 hpriv = host->private_data;
1986 mmio = hpriv->mmio;
1987
1988
1989 irq_stat = readl(mmio + HOST_IRQ_STAT);
1990 if (!irq_stat)
1991 return IRQ_NONE;
1992
1993 irq_masked = irq_stat & hpriv->port_map;
1994
1995 spin_lock(&host->lock);
1996
1997 rc = ahci_handle_port_intr(host, irq_masked);
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008 writel(irq_stat, mmio + HOST_IRQ_STAT);
2009
2010 spin_unlock(&host->lock);
2011
2012 VPRINTK("EXIT\n");
2013
2014 return IRQ_RETVAL(rc);
2015}
2016
2017unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
2018{
2019 struct ata_port *ap = qc->ap;
2020 void __iomem *port_mmio = ahci_port_base(ap);
2021 struct ahci_port_priv *pp = ap->private_data;
2022
2023
2024
2025
2026
2027 pp->active_link = qc->dev->link;
2028
2029 if (ata_is_ncq(qc->tf.protocol))
2030 writel(1 << qc->hw_tag, port_mmio + PORT_SCR_ACT);
2031
2032 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
2033 u32 fbs = readl(port_mmio + PORT_FBS);
2034 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
2035 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
2036 writel(fbs, port_mmio + PORT_FBS);
2037 pp->fbs_last_dev = qc->dev->link->pmp;
2038 }
2039
2040 writel(1 << qc->hw_tag, port_mmio + PORT_CMD_ISSUE);
2041
2042 ahci_sw_activity(qc->dev->link);
2043
2044 return 0;
2045}
2046EXPORT_SYMBOL_GPL(ahci_qc_issue);
2047
2048static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2049{
2050 struct ahci_port_priv *pp = qc->ap->private_data;
2051 u8 *rx_fis = pp->rx_fis;
2052
2053 if (pp->fbs_enabled)
2054 rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
2055
2056
2057
2058
2059
2060
2061
2062 if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
2063 !(qc->flags & ATA_QCFLAG_FAILED)) {
2064 ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
2065 qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
2066 } else
2067 ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
2068
2069 return true;
2070}
2071
2072static void ahci_freeze(struct ata_port *ap)
2073{
2074 void __iomem *port_mmio = ahci_port_base(ap);
2075
2076
2077 writel(0, port_mmio + PORT_IRQ_MASK);
2078}
2079
2080static void ahci_thaw(struct ata_port *ap)
2081{
2082 struct ahci_host_priv *hpriv = ap->host->private_data;
2083 void __iomem *mmio = hpriv->mmio;
2084 void __iomem *port_mmio = ahci_port_base(ap);
2085 u32 tmp;
2086 struct ahci_port_priv *pp = ap->private_data;
2087
2088
2089 tmp = readl(port_mmio + PORT_IRQ_STAT);
2090 writel(tmp, port_mmio + PORT_IRQ_STAT);
2091 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2092
2093
2094 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2095}
2096
2097void ahci_error_handler(struct ata_port *ap)
2098{
2099 struct ahci_host_priv *hpriv = ap->host->private_data;
2100
2101 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2102
2103 hpriv->stop_engine(ap);
2104 hpriv->start_engine(ap);
2105 }
2106
2107 sata_pmp_error_handler(ap);
2108
2109 if (!ata_dev_enabled(ap->link.device))
2110 hpriv->stop_engine(ap);
2111}
2112EXPORT_SYMBOL_GPL(ahci_error_handler);
2113
2114static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2115{
2116 struct ata_port *ap = qc->ap;
2117
2118
2119 if (qc->flags & ATA_QCFLAG_FAILED)
2120 ahci_kick_engine(ap);
2121}
2122
2123static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2124{
2125 struct ahci_host_priv *hpriv = ap->host->private_data;
2126 void __iomem *port_mmio = ahci_port_base(ap);
2127 struct ata_device *dev = ap->link.device;
2128 u32 devslp, dm, dito, mdat, deto;
2129 int rc;
2130 unsigned int err_mask;
2131
2132 devslp = readl(port_mmio + PORT_DEVSLP);
2133 if (!(devslp & PORT_DEVSLP_DSP)) {
2134 dev_info(ap->host->dev, "port does not support device sleep\n");
2135 return;
2136 }
2137
2138
2139 if (!sleep) {
2140 if (devslp & PORT_DEVSLP_ADSE) {
2141 writel(devslp & ~PORT_DEVSLP_ADSE,
2142 port_mmio + PORT_DEVSLP);
2143 err_mask = ata_dev_set_feature(dev,
2144 SETFEATURES_SATA_DISABLE,
2145 SATA_DEVSLP);
2146 if (err_mask && err_mask != AC_ERR_DEV)
2147 ata_dev_warn(dev, "failed to disable DEVSLP\n");
2148 }
2149 return;
2150 }
2151
2152
2153 if (devslp & PORT_DEVSLP_ADSE)
2154 return;
2155
2156
2157 rc = hpriv->stop_engine(ap);
2158 if (rc)
2159 return;
2160
2161 dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2162 dito = devslp_idle_timeout / (dm + 1);
2163 if (dito > 0x3ff)
2164 dito = 0x3ff;
2165
2166
2167
2168
2169 if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
2170 ATA_LOG_DEVSLP_VALID_MASK) {
2171 mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
2172 ATA_LOG_DEVSLP_MDAT_MASK;
2173 if (!mdat)
2174 mdat = 10;
2175 deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
2176 if (!deto)
2177 deto = 20;
2178 } else {
2179 mdat = 10;
2180 deto = 20;
2181 }
2182
2183 devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2184 (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2185 (deto << PORT_DEVSLP_DETO_OFFSET) |
2186 PORT_DEVSLP_ADSE);
2187 writel(devslp, port_mmio + PORT_DEVSLP);
2188
2189 hpriv->start_engine(ap);
2190
2191
2192 err_mask = ata_dev_set_feature(dev,
2193 SETFEATURES_SATA_ENABLE,
2194 SATA_DEVSLP);
2195 if (err_mask && err_mask != AC_ERR_DEV)
2196 ata_dev_warn(dev, "failed to enable DEVSLP\n");
2197}
2198
2199static void ahci_enable_fbs(struct ata_port *ap)
2200{
2201 struct ahci_host_priv *hpriv = ap->host->private_data;
2202 struct ahci_port_priv *pp = ap->private_data;
2203 void __iomem *port_mmio = ahci_port_base(ap);
2204 u32 fbs;
2205 int rc;
2206
2207 if (!pp->fbs_supported)
2208 return;
2209
2210 fbs = readl(port_mmio + PORT_FBS);
2211 if (fbs & PORT_FBS_EN) {
2212 pp->fbs_enabled = true;
2213 pp->fbs_last_dev = -1;
2214 return;
2215 }
2216
2217 rc = hpriv->stop_engine(ap);
2218 if (rc)
2219 return;
2220
2221 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2222 fbs = readl(port_mmio + PORT_FBS);
2223 if (fbs & PORT_FBS_EN) {
2224 dev_info(ap->host->dev, "FBS is enabled\n");
2225 pp->fbs_enabled = true;
2226 pp->fbs_last_dev = -1;
2227 } else
2228 dev_err(ap->host->dev, "Failed to enable FBS\n");
2229
2230 hpriv->start_engine(ap);
2231}
2232
2233static void ahci_disable_fbs(struct ata_port *ap)
2234{
2235 struct ahci_host_priv *hpriv = ap->host->private_data;
2236 struct ahci_port_priv *pp = ap->private_data;
2237 void __iomem *port_mmio = ahci_port_base(ap);
2238 u32 fbs;
2239 int rc;
2240
2241 if (!pp->fbs_supported)
2242 return;
2243
2244 fbs = readl(port_mmio + PORT_FBS);
2245 if ((fbs & PORT_FBS_EN) == 0) {
2246 pp->fbs_enabled = false;
2247 return;
2248 }
2249
2250 rc = hpriv->stop_engine(ap);
2251 if (rc)
2252 return;
2253
2254 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2255 fbs = readl(port_mmio + PORT_FBS);
2256 if (fbs & PORT_FBS_EN)
2257 dev_err(ap->host->dev, "Failed to disable FBS\n");
2258 else {
2259 dev_info(ap->host->dev, "FBS is disabled\n");
2260 pp->fbs_enabled = false;
2261 }
2262
2263 hpriv->start_engine(ap);
2264}
2265
2266static void ahci_pmp_attach(struct ata_port *ap)
2267{
2268 void __iomem *port_mmio = ahci_port_base(ap);
2269 struct ahci_port_priv *pp = ap->private_data;
2270 u32 cmd;
2271
2272 cmd = readl(port_mmio + PORT_CMD);
2273 cmd |= PORT_CMD_PMP;
2274 writel(cmd, port_mmio + PORT_CMD);
2275
2276 ahci_enable_fbs(ap);
2277
2278 pp->intr_mask |= PORT_IRQ_BAD_PMP;
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2289 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2290}
2291
2292static void ahci_pmp_detach(struct ata_port *ap)
2293{
2294 void __iomem *port_mmio = ahci_port_base(ap);
2295 struct ahci_port_priv *pp = ap->private_data;
2296 u32 cmd;
2297
2298 ahci_disable_fbs(ap);
2299
2300 cmd = readl(port_mmio + PORT_CMD);
2301 cmd &= ~PORT_CMD_PMP;
2302 writel(cmd, port_mmio + PORT_CMD);
2303
2304 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2305
2306
2307 if (!(ap->pflags & ATA_PFLAG_FROZEN))
2308 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2309}
2310
2311int ahci_port_resume(struct ata_port *ap)
2312{
2313 ahci_rpm_get_port(ap);
2314
2315 ahci_power_up(ap);
2316 ahci_start_port(ap);
2317
2318 if (sata_pmp_attached(ap))
2319 ahci_pmp_attach(ap);
2320 else
2321 ahci_pmp_detach(ap);
2322
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(ahci_port_resume);
2326
2327#ifdef CONFIG_PM
2328static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2329{
2330 const char *emsg = NULL;
2331 int rc;
2332
2333 rc = ahci_deinit_port(ap, &emsg);
2334 if (rc == 0)
2335 ahci_power_down(ap);
2336 else {
2337 ata_port_err(ap, "%s (%d)\n", emsg, rc);
2338 ata_port_freeze(ap);
2339 }
2340
2341 ahci_rpm_put_port(ap);
2342 return rc;
2343}
2344#endif
2345
2346static int ahci_port_start(struct ata_port *ap)
2347{
2348 struct ahci_host_priv *hpriv = ap->host->private_data;
2349 struct device *dev = ap->host->dev;
2350 struct ahci_port_priv *pp;
2351 void *mem;
2352 dma_addr_t mem_dma;
2353 size_t dma_sz, rx_fis_sz;
2354
2355 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2356 if (!pp)
2357 return -ENOMEM;
2358
2359 if (ap->host->n_ports > 1) {
2360 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2361 if (!pp->irq_desc) {
2362 devm_kfree(dev, pp);
2363 return -ENOMEM;
2364 }
2365 snprintf(pp->irq_desc, 8,
2366 "%s%d", dev_driver_string(dev), ap->port_no);
2367 }
2368
2369
2370 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2371 void __iomem *port_mmio = ahci_port_base(ap);
2372 u32 cmd = readl(port_mmio + PORT_CMD);
2373 if (cmd & PORT_CMD_FBSCP)
2374 pp->fbs_supported = true;
2375 else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
2376 dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2377 ap->port_no);
2378 pp->fbs_supported = true;
2379 } else
2380 dev_warn(dev, "port %d is not capable of FBS\n",
2381 ap->port_no);
2382 }
2383
2384 if (pp->fbs_supported) {
2385 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2386 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2387 } else {
2388 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2389 rx_fis_sz = AHCI_RX_FIS_SZ;
2390 }
2391
2392 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2393 if (!mem)
2394 return -ENOMEM;
2395 memset(mem, 0, dma_sz);
2396
2397
2398
2399
2400
2401 pp->cmd_slot = mem;
2402 pp->cmd_slot_dma = mem_dma;
2403
2404 mem += AHCI_CMD_SLOT_SZ;
2405 mem_dma += AHCI_CMD_SLOT_SZ;
2406
2407
2408
2409
2410 pp->rx_fis = mem;
2411 pp->rx_fis_dma = mem_dma;
2412
2413 mem += rx_fis_sz;
2414 mem_dma += rx_fis_sz;
2415
2416
2417
2418
2419
2420 pp->cmd_tbl = mem;
2421 pp->cmd_tbl_dma = mem_dma;
2422
2423
2424
2425
2426
2427 pp->intr_mask = DEF_PORT_IRQ;
2428
2429
2430
2431
2432 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2433 spin_lock_init(&pp->lock);
2434 ap->lock = &pp->lock;
2435 }
2436
2437 ap->private_data = pp;
2438
2439
2440 return ahci_port_resume(ap);
2441}
2442
2443static void ahci_port_stop(struct ata_port *ap)
2444{
2445 const char *emsg = NULL;
2446 struct ahci_host_priv *hpriv = ap->host->private_data;
2447 void __iomem *host_mmio = hpriv->mmio;
2448 int rc;
2449
2450
2451 rc = ahci_deinit_port(ap, &emsg);
2452 if (rc)
2453 ata_port_warn(ap, "%s (%d)\n", emsg, rc);
2454
2455
2456
2457
2458
2459 writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
2460}
2461
2462void ahci_print_info(struct ata_host *host, const char *scc_s)
2463{
2464 struct ahci_host_priv *hpriv = host->private_data;
2465 u32 vers, cap, cap2, impl, speed;
2466 const char *speed_s;
2467
2468 vers = hpriv->version;
2469 cap = hpriv->cap;
2470 cap2 = hpriv->cap2;
2471 impl = hpriv->port_map;
2472
2473 speed = (cap >> 20) & 0xf;
2474 if (speed == 1)
2475 speed_s = "1.5";
2476 else if (speed == 2)
2477 speed_s = "3";
2478 else if (speed == 3)
2479 speed_s = "6";
2480 else
2481 speed_s = "?";
2482
2483 dev_info(host->dev,
2484 "AHCI %02x%02x.%02x%02x "
2485 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2486 ,
2487
2488 (vers >> 24) & 0xff,
2489 (vers >> 16) & 0xff,
2490 (vers >> 8) & 0xff,
2491 vers & 0xff,
2492
2493 ((cap >> 8) & 0x1f) + 1,
2494 (cap & 0x1f) + 1,
2495 speed_s,
2496 impl,
2497 scc_s);
2498
2499 dev_info(host->dev,
2500 "flags: "
2501 "%s%s%s%s%s%s%s"
2502 "%s%s%s%s%s%s%s"
2503 "%s%s%s%s%s%s%s"
2504 "%s%s\n"
2505 ,
2506
2507 cap & HOST_CAP_64 ? "64bit " : "",
2508 cap & HOST_CAP_NCQ ? "ncq " : "",
2509 cap & HOST_CAP_SNTF ? "sntf " : "",
2510 cap & HOST_CAP_MPS ? "ilck " : "",
2511 cap & HOST_CAP_SSS ? "stag " : "",
2512 cap & HOST_CAP_ALPM ? "pm " : "",
2513 cap & HOST_CAP_LED ? "led " : "",
2514 cap & HOST_CAP_CLO ? "clo " : "",
2515 cap & HOST_CAP_ONLY ? "only " : "",
2516 cap & HOST_CAP_PMP ? "pmp " : "",
2517 cap & HOST_CAP_FBS ? "fbs " : "",
2518 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2519 cap & HOST_CAP_SSC ? "slum " : "",
2520 cap & HOST_CAP_PART ? "part " : "",
2521 cap & HOST_CAP_CCC ? "ccc " : "",
2522 cap & HOST_CAP_EMS ? "ems " : "",
2523 cap & HOST_CAP_SXS ? "sxs " : "",
2524 cap2 & HOST_CAP2_DESO ? "deso " : "",
2525 cap2 & HOST_CAP2_SADM ? "sadm " : "",
2526 cap2 & HOST_CAP2_SDS ? "sds " : "",
2527 cap2 & HOST_CAP2_APST ? "apst " : "",
2528 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2529 cap2 & HOST_CAP2_BOH ? "boh " : ""
2530 );
2531}
2532EXPORT_SYMBOL_GPL(ahci_print_info);
2533
2534void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2535 struct ata_port_info *pi)
2536{
2537 u8 messages;
2538 void __iomem *mmio = hpriv->mmio;
2539 u32 em_loc = readl(mmio + HOST_EM_LOC);
2540 u32 em_ctl = readl(mmio + HOST_EM_CTL);
2541
2542 if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2543 return;
2544
2545 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2546
2547 if (messages) {
2548
2549 hpriv->em_loc = ((em_loc >> 16) * 4);
2550 hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
2551 hpriv->em_msg_type = messages;
2552 pi->flags |= ATA_FLAG_EM;
2553 if (!(em_ctl & EM_CTL_ALHD))
2554 pi->flags |= ATA_FLAG_SW_ACTIVITY;
2555 }
2556}
2557EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2558
2559static int ahci_host_activate_multi_irqs(struct ata_host *host,
2560 struct scsi_host_template *sht)
2561{
2562 struct ahci_host_priv *hpriv = host->private_data;
2563 int i, rc;
2564
2565 rc = ata_host_start(host);
2566 if (rc)
2567 return rc;
2568
2569
2570
2571
2572 for (i = 0; i < host->n_ports; i++) {
2573 struct ahci_port_priv *pp = host->ports[i]->private_data;
2574 int irq = hpriv->get_irq_vector(host, i);
2575
2576
2577 if (!pp) {
2578 disable_irq(irq);
2579 continue;
2580 }
2581
2582 rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2583 0, pp->irq_desc, host->ports[i]);
2584
2585 if (rc)
2586 return rc;
2587 ata_port_desc(host->ports[i], "irq %d", irq);
2588 }
2589
2590 return ata_host_register(host, sht);
2591}
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
2605{
2606 struct ahci_host_priv *hpriv = host->private_data;
2607 int irq = hpriv->irq;
2608 int rc;
2609
2610 if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2611 if (hpriv->irq_handler)
2612 dev_warn(host->dev,
2613 "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
2614 if (!hpriv->get_irq_vector) {
2615 dev_err(host->dev,
2616 "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
2617 return -EIO;
2618 }
2619
2620 rc = ahci_host_activate_multi_irqs(host, sht);
2621 } else {
2622 rc = ata_host_activate(host, irq, hpriv->irq_handler,
2623 IRQF_SHARED, sht);
2624 }
2625
2626
2627 return rc;
2628}
2629EXPORT_SYMBOL_GPL(ahci_host_activate);
2630
2631MODULE_AUTHOR("Jeff Garzik");
2632MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2633MODULE_LICENSE("GPL");
2634