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