Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / scsi / ahci.c
1 /*
2  *  ahci.c - AHCI SATA support
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/module.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/blkdev.h>
40 #include <linux/delay.h>
41 #include <linux/interrupt.h>
42 #include <linux/sched.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/device.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <linux/libata.h>
49 #include <asm/io.h>
50
51 #define DRV_NAME        "ahci"
52 #define DRV_VERSION     "1.2"
53
54
55 enum {
56         AHCI_PCI_BAR            = 5,
57         AHCI_MAX_SG             = 168, /* hardware max is 64K */
58         AHCI_DMA_BOUNDARY       = 0xffffffff,
59         AHCI_USE_CLUSTERING     = 0,
60         AHCI_CMD_SLOT_SZ        = 32 * 32,
61         AHCI_RX_FIS_SZ          = 256,
62         AHCI_CMD_TBL_HDR        = 0x80,
63         AHCI_CMD_TBL_CDB        = 0x40,
64         AHCI_CMD_TBL_SZ         = AHCI_CMD_TBL_HDR + (AHCI_MAX_SG * 16),
65         AHCI_PORT_PRIV_DMA_SZ   = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_SZ +
66                                   AHCI_RX_FIS_SZ,
67         AHCI_IRQ_ON_SG          = (1 << 31),
68         AHCI_CMD_ATAPI          = (1 << 5),
69         AHCI_CMD_WRITE          = (1 << 6),
70         AHCI_CMD_PREFETCH       = (1 << 7),
71         AHCI_CMD_RESET          = (1 << 8),
72         AHCI_CMD_CLR_BUSY       = (1 << 10),
73
74         RX_FIS_D2H_REG          = 0x40, /* offset of D2H Register FIS data */
75
76         board_ahci              = 0,
77
78         /* global controller registers */
79         HOST_CAP                = 0x00, /* host capabilities */
80         HOST_CTL                = 0x04, /* global host control */
81         HOST_IRQ_STAT           = 0x08, /* interrupt status */
82         HOST_PORTS_IMPL         = 0x0c, /* bitmap of implemented ports */
83         HOST_VERSION            = 0x10, /* AHCI spec. version compliancy */
84
85         /* HOST_CTL bits */
86         HOST_RESET              = (1 << 0),  /* reset controller; self-clear */
87         HOST_IRQ_EN             = (1 << 1),  /* global IRQ enable */
88         HOST_AHCI_EN            = (1 << 31), /* AHCI enabled */
89
90         /* HOST_CAP bits */
91         HOST_CAP_64             = (1 << 31), /* PCI DAC (64-bit DMA) support */
92         HOST_CAP_SSS            = (1 << 27), /* Staggered Spin-up */
93         HOST_CAP_CLO            = (1 << 24), /* Command List Override support */
94         HOST_CAP_SSC            = (1 << 14), /* Slumber capable */
95
96         /* registers for each SATA port */
97         PORT_LST_ADDR           = 0x00, /* command list DMA addr */
98         PORT_LST_ADDR_HI        = 0x04, /* command list DMA addr hi */
99         PORT_FIS_ADDR           = 0x08, /* FIS rx buf addr */
100         PORT_FIS_ADDR_HI        = 0x0c, /* FIS rx buf addr hi */
101         PORT_IRQ_STAT           = 0x10, /* interrupt status */
102         PORT_IRQ_MASK           = 0x14, /* interrupt enable/disable mask */
103         PORT_CMD                = 0x18, /* port command */
104         PORT_TFDATA             = 0x20, /* taskfile data */
105         PORT_SIG                = 0x24, /* device TF signature */
106         PORT_CMD_ISSUE          = 0x38, /* command issue */
107         PORT_SCR                = 0x28, /* SATA phy register block */
108         PORT_SCR_STAT           = 0x28, /* SATA phy register: SStatus */
109         PORT_SCR_CTL            = 0x2c, /* SATA phy register: SControl */
110         PORT_SCR_ERR            = 0x30, /* SATA phy register: SError */
111         PORT_SCR_ACT            = 0x34, /* SATA phy register: SActive */
112
113         /* PORT_IRQ_{STAT,MASK} bits */
114         PORT_IRQ_COLD_PRES      = (1 << 31), /* cold presence detect */
115         PORT_IRQ_TF_ERR         = (1 << 30), /* task file error */
116         PORT_IRQ_HBUS_ERR       = (1 << 29), /* host bus fatal error */
117         PORT_IRQ_HBUS_DATA_ERR  = (1 << 28), /* host bus data error */
118         PORT_IRQ_IF_ERR         = (1 << 27), /* interface fatal error */
119         PORT_IRQ_IF_NONFATAL    = (1 << 26), /* interface non-fatal error */
120         PORT_IRQ_OVERFLOW       = (1 << 24), /* xfer exhausted available S/G */
121         PORT_IRQ_BAD_PMP        = (1 << 23), /* incorrect port multiplier */
122
123         PORT_IRQ_PHYRDY         = (1 << 22), /* PhyRdy changed */
124         PORT_IRQ_DEV_ILCK       = (1 << 7), /* device interlock */
125         PORT_IRQ_CONNECT        = (1 << 6), /* port connect change status */
126         PORT_IRQ_SG_DONE        = (1 << 5), /* descriptor processed */
127         PORT_IRQ_UNK_FIS        = (1 << 4), /* unknown FIS rx'd */
128         PORT_IRQ_SDB_FIS        = (1 << 3), /* Set Device Bits FIS rx'd */
129         PORT_IRQ_DMAS_FIS       = (1 << 2), /* DMA Setup FIS rx'd */
130         PORT_IRQ_PIOS_FIS       = (1 << 1), /* PIO Setup FIS rx'd */
131         PORT_IRQ_D2H_REG_FIS    = (1 << 0), /* D2H Register FIS rx'd */
132
133         PORT_IRQ_FATAL          = PORT_IRQ_TF_ERR |
134                                   PORT_IRQ_HBUS_ERR |
135                                   PORT_IRQ_HBUS_DATA_ERR |
136                                   PORT_IRQ_IF_ERR,
137         DEF_PORT_IRQ            = PORT_IRQ_FATAL | PORT_IRQ_PHYRDY |
138                                   PORT_IRQ_CONNECT | PORT_IRQ_SG_DONE |
139                                   PORT_IRQ_UNK_FIS | PORT_IRQ_SDB_FIS |
140                                   PORT_IRQ_DMAS_FIS | PORT_IRQ_PIOS_FIS |
141                                   PORT_IRQ_D2H_REG_FIS,
142
143         /* PORT_CMD bits */
144         PORT_CMD_ATAPI          = (1 << 24), /* Device is ATAPI */
145         PORT_CMD_CPD            = (1 << 20), /* Cold presence detection */
146         PORT_CMD_LIST_ON        = (1 << 15), /* cmd list DMA engine running */
147         PORT_CMD_FIS_ON         = (1 << 14), /* FIS DMA engine running */
148         PORT_CMD_FIS_RX         = (1 << 4), /* Enable FIS receive DMA engine */
149         PORT_CMD_CLO            = (1 << 3), /* Command list override */
150         PORT_CMD_POWER_ON       = (1 << 2), /* Power up device */
151         PORT_CMD_SPIN_UP        = (1 << 1), /* Spin up device */
152         PORT_CMD_START          = (1 << 0), /* Enable port DMA engine */
153
154         PORT_CMD_ICC_MASK       = (0xf << 28), /* i/f ICC state mask */
155         PORT_CMD_ICC_ACTIVE     = (0x1 << 28), /* Put i/f in active state */
156         PORT_CMD_ICC_PARTIAL    = (0x2 << 28), /* Put i/f in partial state */
157         PORT_CMD_ICC_SLUMBER    = (0x6 << 28), /* Put i/f in slumber state */
158
159         /* hpriv->flags bits */
160         AHCI_FLAG_MSI           = (1 << 0),
161 };
162
163 struct ahci_cmd_hdr {
164         u32                     opts;
165         u32                     status;
166         u32                     tbl_addr;
167         u32                     tbl_addr_hi;
168         u32                     reserved[4];
169 };
170
171 struct ahci_sg {
172         u32                     addr;
173         u32                     addr_hi;
174         u32                     reserved;
175         u32                     flags_size;
176 };
177
178 struct ahci_host_priv {
179         unsigned long           flags;
180         u32                     cap;    /* cache of HOST_CAP register */
181         u32                     port_map; /* cache of HOST_PORTS_IMPL reg */
182         u32                     dev_map; /* connected devices */
183 };
184
185 struct ahci_port_priv {
186         struct ahci_cmd_hdr     *cmd_slot;
187         dma_addr_t              cmd_slot_dma;
188         void                    *cmd_tbl;
189         dma_addr_t              cmd_tbl_dma;
190         struct ahci_sg          *cmd_tbl_sg;
191         void                    *rx_fis;
192         dma_addr_t              rx_fis_dma;
193 };
194
195 static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
196 static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
197 static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
198 static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
199 static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
200 static int ahci_start_engine(void __iomem *port_mmio);
201 static int ahci_stop_engine(void __iomem *port_mmio);
202 static int ahci_stop_fis_rx(void __iomem *port_mmio);
203 static void ahci_start_fis_rx(void __iomem *port_mmio,
204                              struct ahci_port_priv *pp,
205                              struct ahci_host_priv *hpriv);
206 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes);
207 static void ahci_irq_clear(struct ata_port *ap);
208 static void ahci_eng_timeout(struct ata_port *ap);
209 static int ahci_port_start(struct ata_port *ap);
210 static void ahci_port_stop(struct ata_port *ap);
211 static int ahci_port_suspend(struct ata_port *ap, pm_message_t state);
212 static int ahci_port_resume(struct ata_port *ap);
213 static int ahci_port_standby(void __iomem *port_mmio, u32 cap);
214 static int ahci_port_spinup(void __iomem *port_mmio, u32 cap);
215 static void ahci_port_disable(struct ata_port *ap);
216 static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
217 static void ahci_qc_prep(struct ata_queued_cmd *qc);
218 static u8 ahci_check_status(struct ata_port *ap);
219 static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
220 static int ahci_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state);
221 static int ahci_scsi_device_resume(struct scsi_device *sdev);
222 static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t state);
223 static int ahci_pci_device_resume(struct pci_dev *pdev);
224 static void ahci_remove_one (struct pci_dev *pdev);
225
226 static struct scsi_host_template ahci_sht = {
227         .module                 = THIS_MODULE,
228         .name                   = DRV_NAME,
229         .ioctl                  = ata_scsi_ioctl,
230         .queuecommand           = ata_scsi_queuecmd,
231         .can_queue              = ATA_DEF_QUEUE,
232         .this_id                = ATA_SHT_THIS_ID,
233         .sg_tablesize           = AHCI_MAX_SG,
234         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
235         .emulated               = ATA_SHT_EMULATED,
236         .use_clustering         = AHCI_USE_CLUSTERING,
237         .proc_name              = DRV_NAME,
238         .dma_boundary           = AHCI_DMA_BOUNDARY,
239         .slave_configure        = ata_scsi_slave_config,
240         .bios_param             = ata_std_bios_param,
241         .resume                 = ahci_scsi_device_resume,
242         .suspend                = ahci_scsi_device_suspend,
243 };
244
245 static const struct ata_port_operations ahci_ops = {
246         .port_disable           = ahci_port_disable,
247
248         .check_status           = ahci_check_status,
249         .check_altstatus        = ahci_check_status,
250         .dev_select             = ata_noop_dev_select,
251
252         .tf_read                = ahci_tf_read,
253
254         .probe_reset            = ahci_probe_reset,
255
256         .qc_prep                = ahci_qc_prep,
257         .qc_issue               = ahci_qc_issue,
258
259         .eng_timeout            = ahci_eng_timeout,
260
261         .irq_handler            = ahci_interrupt,
262         .irq_clear              = ahci_irq_clear,
263
264         .scr_read               = ahci_scr_read,
265         .scr_write              = ahci_scr_write,
266
267         .port_start             = ahci_port_start,
268         .port_stop              = ahci_port_stop,
269 };
270
271 static const struct ata_port_info ahci_port_info[] = {
272         /* board_ahci */
273         {
274                 .sht            = &ahci_sht,
275                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
276                                   ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
277                 .pio_mask       = 0x1f, /* pio0-4 */
278                 .udma_mask      = 0x7f, /* udma0-6 ; FIXME */
279                 .port_ops       = &ahci_ops,
280         },
281 };
282
283 static const struct pci_device_id ahci_pci_tbl[] = {
284         { PCI_VENDOR_ID_INTEL, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
285           board_ahci }, /* ICH6 */
286         { PCI_VENDOR_ID_INTEL, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
287           board_ahci }, /* ICH6M */
288         { PCI_VENDOR_ID_INTEL, 0x27c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
289           board_ahci }, /* ICH7 */
290         { PCI_VENDOR_ID_INTEL, 0x27c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
291           board_ahci }, /* ICH7M */
292         { PCI_VENDOR_ID_INTEL, 0x27c3, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
293           board_ahci }, /* ICH7R */
294         { PCI_VENDOR_ID_AL, 0x5288, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
295           board_ahci }, /* ULi M5288 */
296         { PCI_VENDOR_ID_INTEL, 0x2681, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
297           board_ahci }, /* ESB2 */
298         { PCI_VENDOR_ID_INTEL, 0x2682, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
299           board_ahci }, /* ESB2 */
300         { PCI_VENDOR_ID_INTEL, 0x2683, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
301           board_ahci }, /* ESB2 */
302         { PCI_VENDOR_ID_INTEL, 0x27c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
303           board_ahci }, /* ICH7-M DH */
304         { PCI_VENDOR_ID_INTEL, 0x2821, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
305           board_ahci }, /* ICH8 */
306         { PCI_VENDOR_ID_INTEL, 0x2822, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
307           board_ahci }, /* ICH8 */
308         { PCI_VENDOR_ID_INTEL, 0x2824, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
309           board_ahci }, /* ICH8 */
310         { PCI_VENDOR_ID_INTEL, 0x2829, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
311           board_ahci }, /* ICH8M */
312         { PCI_VENDOR_ID_INTEL, 0x282a, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
313           board_ahci }, /* ICH8M */
314         { 0x197b, 0x2360, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
315           board_ahci }, /* JMicron JMB360 */
316         { 0x197b, 0x2363, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
317           board_ahci }, /* JMicron JMB363 */
318         { PCI_VENDOR_ID_ATI, 0x4380, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
319           board_ahci }, /* ATI SB600 non-raid */
320         { PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
321           board_ahci }, /* ATI SB600 raid */
322         { }     /* terminate list */
323 };
324
325
326 static struct pci_driver ahci_pci_driver = {
327         .name                   = DRV_NAME,
328         .id_table               = ahci_pci_tbl,
329         .probe                  = ahci_init_one,
330         .remove                 = ahci_remove_one,
331         .suspend                = ahci_pci_device_suspend,
332         .resume                 = ahci_pci_device_resume,
333 };
334
335
336 static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
337 {
338         return base + 0x100 + (port * 0x80);
339 }
340
341 static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
342 {
343         return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
344 }
345
346 static int ahci_port_start(struct ata_port *ap)
347 {
348         struct device *dev = ap->host_set->dev;
349         struct ahci_host_priv *hpriv = ap->host_set->private_data;
350         struct ahci_port_priv *pp;
351         void __iomem *mmio = ap->host_set->mmio_base;
352         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
353         void *mem;
354         dma_addr_t mem_dma;
355         int rc;
356
357         pp = kmalloc(sizeof(*pp), GFP_KERNEL);
358         if (!pp)
359                 return -ENOMEM;
360         memset(pp, 0, sizeof(*pp));
361
362         rc = ata_pad_alloc(ap, dev);
363         if (rc) {
364                 kfree(pp);
365                 return rc;
366         }
367
368         mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
369         if (!mem) {
370                 ata_pad_free(ap, dev);
371                 kfree(pp);
372                 return -ENOMEM;
373         }
374         memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
375
376         /*
377          * First item in chunk of DMA memory: 32-slot command table,
378          * 32 bytes each in size
379          */
380         pp->cmd_slot = mem;
381         pp->cmd_slot_dma = mem_dma;
382
383         mem += AHCI_CMD_SLOT_SZ;
384         mem_dma += AHCI_CMD_SLOT_SZ;
385
386         /*
387          * Second item: Received-FIS area
388          */
389         pp->rx_fis = mem;
390         pp->rx_fis_dma = mem_dma;
391
392         mem += AHCI_RX_FIS_SZ;
393         mem_dma += AHCI_RX_FIS_SZ;
394
395         /*
396          * Third item: data area for storing a single command
397          * and its scatter-gather table
398          */
399         pp->cmd_tbl = mem;
400         pp->cmd_tbl_dma = mem_dma;
401
402         pp->cmd_tbl_sg = mem + AHCI_CMD_TBL_HDR;
403
404         ap->private_data = pp;
405
406         /*
407          * Driver is setup; initialize the HBA
408          */
409         ahci_start_fis_rx(port_mmio, pp, hpriv);
410         rc = ahci_port_spinup(port_mmio, hpriv->cap);
411         if (rc)
412                 printk(KERN_WARNING "ata%d: could not spinup device (%d)\n",
413                        ap->id, rc);
414  
415         /*
416          * Do not enable DMA here; according to the spec
417          * (section 10.1.1) we should first enable FIS reception,
418          * then check if the port is enabled before we try to 
419          * switch on DMA.
420          * And as the port check is done during probe
421          * we really shouldn't be doing it here.
422          */
423         return 0;
424 }
425
426
427 static void ahci_port_stop(struct ata_port *ap)
428 {
429         struct device *dev = ap->host_set->dev;
430         struct ahci_port_priv *pp = ap->private_data;
431
432         ahci_port_suspend(ap, PMSG_SUSPEND);
433
434         ap->private_data = NULL;
435         dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
436                           pp->cmd_slot, pp->cmd_slot_dma);
437         ata_pad_free(ap, dev);
438         kfree(pp);
439 }
440
441 static int ahci_port_suspend(struct ata_port *ap, pm_message_t state)
442 {
443         void __iomem *mmio = ap->host_set->mmio_base;
444         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
445         struct ahci_host_priv *hpriv = ap->host_set->private_data;
446         int rc;
447
448         /*
449          * Disable DMA
450          */
451         rc = ahci_stop_engine(port_mmio);
452         if (rc) {
453                 printk(KERN_WARNING "ata%u: DMA engine busy\n", ap->id);
454                 return rc;
455         }
456
457         /*
458          * Disable FIS reception
459          */
460         rc = ahci_stop_fis_rx(port_mmio);
461         if (rc)
462                 printk(KERN_WARNING "ata%d: FIS RX still running (rc %d)\n",
463                        ap->id, rc);
464
465         /*
466          * Put device into slumber mode
467          */
468         if (!rc && state.event != PM_EVENT_FREEZE)
469                 ahci_port_standby(port_mmio, hpriv->cap);
470
471         return rc;
472 }
473
474 static int ahci_port_resume(struct ata_port *ap)
475 {
476         void __iomem *mmio = ap->host_set->mmio_base;
477         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
478         struct ahci_host_priv *hpriv = ap->host_set->private_data;
479         struct ahci_port_priv *pp = ap->private_data;
480         int rc;
481         u32 tmp;
482
483         /*
484          * Enable FIS reception
485          */
486         ahci_start_fis_rx(port_mmio, pp, hpriv);
487
488         rc = ahci_port_spinup(port_mmio, hpriv->cap);
489         if (rc)
490                 printk(KERN_WARNING "ata%d: could not spinup device (%d)\n",
491                        ap->id, rc);
492
493         /*
494          * Clear error status
495          */
496         tmp = readl(port_mmio + PORT_SCR_ERR);
497         writel(tmp, port_mmio + PORT_SCR_ERR);
498         /*
499          * Clear interrupt status
500          */
501         tmp = readl(mmio + HOST_CTL);
502         if (!(tmp & HOST_IRQ_EN)) {
503                 u32 irq_stat;
504
505                 /* ack any pending irq events for this port */
506                 irq_stat = readl(port_mmio + PORT_IRQ_STAT);
507                 if (irq_stat)
508                         writel(irq_stat, port_mmio + PORT_IRQ_STAT);
509
510                 /* set irq mask (enables interrupts) */
511                 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
512
513                 if ((hpriv->dev_map >> (ap->port_no + 1)) == 0) {
514                         /*
515                          * Enable interrupts if this was the last port
516                          */
517                         printk(KERN_WARNING "ata%d: enabling interrupts\n",
518                                ap->id);
519
520                         irq_stat = readl(mmio + HOST_IRQ_STAT);
521                         if (irq_stat)
522                                 writel(irq_stat, mmio + HOST_IRQ_STAT);
523
524                         tmp |= HOST_IRQ_EN;
525                         writel(tmp, mmio + HOST_CTL);
526                         (void) readl(mmio + HOST_CTL);
527                 }
528         }
529
530         /*
531          * Enable DMA
532          */
533         rc = ahci_start_engine(port_mmio);
534         if (rc)
535                 printk(KERN_WARNING "ata%d: cannot start DMA engine (rc %d)\n",
536                        ap->id, rc);
537
538         return rc;
539 }
540
541 static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
542 {
543         unsigned int sc_reg;
544
545         switch (sc_reg_in) {
546         case SCR_STATUS:        sc_reg = 0; break;
547         case SCR_CONTROL:       sc_reg = 1; break;
548         case SCR_ERROR:         sc_reg = 2; break;
549         case SCR_ACTIVE:        sc_reg = 3; break;
550         default:
551                 return 0xffffffffU;
552         }
553
554         return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
555 }
556
557
558 static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
559                                u32 val)
560 {
561         unsigned int sc_reg;
562
563         switch (sc_reg_in) {
564         case SCR_STATUS:        sc_reg = 0; break;
565         case SCR_CONTROL:       sc_reg = 1; break;
566         case SCR_ERROR:         sc_reg = 2; break;
567         case SCR_ACTIVE:        sc_reg = 3; break;
568         default:
569                 return;
570         }
571
572         writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
573 }
574
575 static int ahci_stop_engine(void __iomem *port_mmio)
576 {
577         int work;
578         u32 tmp;
579
580         tmp = readl(port_mmio + PORT_CMD);
581         /* Check if the HBA is idle */
582         if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
583                 return 0;
584
585         /* Setting HBA to idle */
586         tmp &= ~PORT_CMD_START;
587         writel(tmp, port_mmio + PORT_CMD);
588
589         /*
590          * wait for engine to become idle
591          */
592         work = 1000;
593         while (work-- > 0) {
594                 tmp = readl(port_mmio + PORT_CMD);
595                 if ((tmp & PORT_CMD_LIST_ON) == 0)
596                         return 0;
597                 udelay(10);
598         }
599
600         return -EIO;
601 }
602
603 static int ahci_start_engine(void __iomem *port_mmio)
604 {
605         u32 tmp;
606         int work = 1000;
607
608         /*
609          * Get current status
610          */
611         tmp = readl(port_mmio + PORT_CMD);
612
613         /*
614          * AHCI rev 1.1 section 10.3.1:
615          * Software shall not set PxCMD.ST to '1' until it verifies
616          * that PxCMD.CR is '0' and has set PxCMD.FRE to '1'
617          */
618         if ((tmp & PORT_CMD_FIS_RX) == 0)
619                 return -EPERM;
620
621         /*
622          * wait for engine to become idle.
623          */
624         while (work-- > 0) {
625                 tmp = readl(port_mmio + PORT_CMD);
626                 if ((tmp & PORT_CMD_LIST_ON) == 0)
627                         break;
628                 udelay(10);
629         }
630
631         if (!work) {
632                 /*
633                  * We need to do a port reset / HBA reset here
634                  */
635                 return -EBUSY;
636         }
637
638         /*
639          * Start DMA
640          */
641         tmp |= PORT_CMD_START;
642         writel(tmp, port_mmio + PORT_CMD);
643         readl(port_mmio + PORT_CMD); /* flush */
644
645         return 0;
646 }
647
648 static int ahci_stop_fis_rx(void __iomem *port_mmio)
649 {
650         u32 tmp;
651         int work = 1000;
652
653         /*
654          * Get current status
655          */
656         tmp = readl(port_mmio + PORT_CMD);
657
658         /* Check if FIS RX is already disabled */
659         if ((tmp & PORT_CMD_FIS_RX) == 0)
660                 return 0;
661
662         /*
663          * AHCI Rev 1.1 section 10.3.2
664          * Software shall not clear PxCMD.FRE while
665          * PxCMD.ST or PxCMD.CR is set to '1'
666          */
667         if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_START)) {
668                 return -EPERM;
669         }
670
671         /*
672          * Disable FIS reception
673          *
674          * AHCI Rev 1.1 Section 10.1.2:
675          * If PxCMD.FRE is set to '1', software should clear it
676          * to '0' and wait at least 500 milliseconds for PxCMD.FR
677          * to return '0' when read. If PxCMD.FR does not clear
678          * '0' correctly, then software may attempt a port reset
679          * of a full HBA reset to recover.
680          */
681         tmp &= ~(PORT_CMD_FIS_RX);
682         writel(tmp, port_mmio + PORT_CMD);
683
684         mdelay(500);
685         work = 1000;
686         while (work-- > 0) {
687                 tmp = readl(port_mmio + PORT_CMD);
688                 if ((tmp & PORT_CMD_FIS_ON) == 0)
689                         return 0;
690                 udelay(10);
691         }
692
693         return -EBUSY;
694 }
695
696 static void ahci_start_fis_rx(void __iomem *port_mmio,
697                              struct ahci_port_priv *pp,
698                              struct ahci_host_priv *hpriv)
699 {
700         u32 tmp;
701
702         /*
703          * Set FIS registers
704          */
705         if (hpriv->cap & HOST_CAP_64)
706                 writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
707         writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
708         readl(port_mmio + PORT_LST_ADDR); /* flush */
709
710         if (hpriv->cap & HOST_CAP_64)
711                 writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
712         writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
713         readl(port_mmio + PORT_FIS_ADDR); /* flush */
714
715         /*
716          * Enable FIS reception
717          */
718         tmp = readl(port_mmio + PORT_CMD);
719         tmp |= PORT_CMD_FIS_RX;
720         writel(tmp, port_mmio + PORT_CMD);
721         readl(port_mmio + PORT_CMD); /* flush */
722 }
723
724 static int ahci_port_standby(void __iomem *port_mmio, u32 cap)
725 {
726         u32 tmp, scontrol, sstatus;
727
728         tmp = readl(port_mmio + PORT_CMD);
729         /*
730          * AHCI Rev1.1 Section 5.3.2.3:
731          * Software is only allowed to program the PxCMD.FRE,
732          * PxCMD.POD, PxSCTL.DET, and PxCMD.SUD register bits
733          * when PxCMD.ST is set to '0'
734          */
735         if (tmp & PORT_CMD_START)
736                 return -EBUSY;
737
738         if (cap & HOST_CAP_SSC) {
739                 /*
740                  * Enable transitions to slumber mode
741                  */
742                 scontrol = readl(port_mmio + PORT_SCR_CTL);
743                 if ((scontrol & 0x0f00) > 0x100) {
744                         scontrol &= ~0xf00;
745                         writel(scontrol, port_mmio + PORT_SCR_CTL);
746                 }
747                 /*
748                  * Put device into slumber mode
749                  */
750                 tmp |= PORT_CMD_ICC_SLUMBER;
751                 writel(tmp, port_mmio + PORT_CMD);
752                 tmp = readl(port_mmio + PORT_CMD);
753
754                 /*
755                  * Actually, we should wait for the device to
756                  * enter slumber mode by checking
757                  * sstatus & 0xf00 == 6
758                  */
759                 sstatus = readl(port_mmio + PORT_SCR_STAT);
760         }
761
762         /*
763          * Put device into listen mode
764          */
765         scontrol = readl(port_mmio + PORT_SCR_CTL);
766         scontrol &= ~0xf;
767         writel(scontrol, port_mmio + PORT_SCR_CTL);
768
769         tmp = readl(port_mmio + PORT_CMD);
770         if (cap & HOST_CAP_SSS) {
771                 /*
772                  * Spin down the device for staggered spin-up support
773                  */
774                 tmp &= ~PORT_CMD_SPIN_UP;
775                 writel(tmp, port_mmio + PORT_CMD);
776                 readl(port_mmio + PORT_CMD); /* flush */
777         }
778
779         return 0;
780 }
781
782 static int ahci_port_spinup(void __iomem *port_mmio, u32 cap)
783 {
784         u32 tmp;
785
786         tmp = readl(port_mmio + PORT_CMD);
787         /*
788          * AHCI Rev1.1 Section 5.3.2.3:
789          * Software is only allowed to program the PxCMD.FRE,
790          * PxCMD.POD, PxSCTL.DET, and PxCMD.SUD register bits
791          * when PxCMD.ST is set to '0'
792          */
793         if (tmp & PORT_CMD_START)
794                 return -EBUSY;
795
796         /*
797          * Power on device if supported
798          */
799         if (tmp & PORT_CMD_CPD) {
800                 tmp |= PORT_CMD_POWER_ON;
801                 writel(tmp, port_mmio + PORT_CMD);
802                 tmp = readl(port_mmio + PORT_CMD);
803         }
804
805         /*
806          * Spin up device
807          */
808         if (cap & HOST_CAP_SSS) {
809                 tmp |= PORT_CMD_SPIN_UP;
810                 writel(tmp, port_mmio + PORT_CMD);
811                 tmp = readl(port_mmio + PORT_CMD);
812         }
813
814         if ((tmp & PORT_CMD_ICC_MASK) != PORT_CMD_ICC_ACTIVE) {
815                 tmp |= PORT_CMD_ICC_ACTIVE;
816                 writel(tmp, port_mmio + PORT_CMD);
817                 tmp = readl(port_mmio + PORT_CMD);
818         }
819
820         return 0;
821 }
822
823 static void ahci_port_disable(struct ata_port *ap)
824 {
825         struct ahci_host_priv *hpriv = ap->host_set->private_data;
826
827         ata_port_disable(ap);
828
829         hpriv->dev_map &= ~(1 << ap->port_no);
830 }
831
832 static unsigned int ahci_dev_classify(struct ata_port *ap)
833 {
834         void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
835         struct ata_taskfile tf;
836         u32 tmp;
837
838         tmp = readl(port_mmio + PORT_SIG);
839         tf.lbah         = (tmp >> 24)   & 0xff;
840         tf.lbam         = (tmp >> 16)   & 0xff;
841         tf.lbal         = (tmp >> 8)    & 0xff;
842         tf.nsect        = (tmp)         & 0xff;
843
844         return ata_dev_classify(&tf);
845 }
846
847 static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, u32 opts)
848 {
849         pp->cmd_slot[0].opts = cpu_to_le32(opts);
850         pp->cmd_slot[0].status = 0;
851         pp->cmd_slot[0].tbl_addr = cpu_to_le32(pp->cmd_tbl_dma & 0xffffffff);
852         pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
853 }
854
855 static int ahci_poll_register(void __iomem *reg, u32 mask, u32 val,
856                               unsigned long interval_msec,
857                               unsigned long timeout_msec)
858 {
859         unsigned long timeout;
860         u32 tmp;
861
862         timeout = jiffies + (timeout_msec * HZ) / 1000;
863         do {
864                 tmp = readl(reg);
865                 if ((tmp & mask) == val)
866                         return 0;
867                 msleep(interval_msec);
868         } while (time_before(jiffies, timeout));
869
870         return -1;
871 }
872
873 static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
874 {
875         struct ahci_host_priv *hpriv = ap->host_set->private_data;
876         struct ahci_port_priv *pp = ap->private_data;
877         void __iomem *mmio = ap->host_set->mmio_base;
878         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
879         const u32 cmd_fis_len = 5; /* five dwords */
880         const char *reason = NULL;
881         struct ata_taskfile tf;
882         u8 *fis;
883         int rc;
884
885         DPRINTK("ENTER\n");
886
887         /* prepare for SRST (AHCI-1.1 10.4.1) */
888         rc = ahci_stop_engine(port_mmio);
889         if (rc) {
890                 reason = "failed to stop engine";
891                 goto fail_restart;
892         }
893
894         /* check BUSY/DRQ, perform Command List Override if necessary */
895         ahci_tf_read(ap, &tf);
896         if (tf.command & (ATA_BUSY | ATA_DRQ)) {
897                 u32 tmp;
898
899                 if (!(hpriv->cap & HOST_CAP_CLO)) {
900                         rc = -EIO;
901                         reason = "port busy but no CLO";
902                         goto fail_restart;
903                 }
904
905                 tmp = readl(port_mmio + PORT_CMD);
906                 tmp |= PORT_CMD_CLO;
907                 writel(tmp, port_mmio + PORT_CMD);
908                 readl(port_mmio + PORT_CMD); /* flush */
909
910                 if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
911                                        1, 500)) {
912                         rc = -EIO;
913                         reason = "CLO failed";
914                         goto fail_restart;
915                 }
916         }
917
918         /* restart engine */
919         ahci_start_engine(port_mmio);
920
921         ata_tf_init(ap, &tf, 0);
922         fis = pp->cmd_tbl;
923
924         /* issue the first D2H Register FIS */
925         ahci_fill_cmd_slot(pp, cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
926
927         tf.ctl |= ATA_SRST;
928         ata_tf_to_fis(&tf, fis, 0);
929         fis[1] &= ~(1 << 7);    /* turn off Command FIS bit */
930
931         writel(1, port_mmio + PORT_CMD_ISSUE);
932         readl(port_mmio + PORT_CMD_ISSUE);      /* flush */
933
934         if (ahci_poll_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x0, 1, 500)) {
935                 rc = -EIO;
936                 reason = "1st FIS failed";
937                 goto fail;
938         }
939
940         /* spec says at least 5us, but be generous and sleep for 1ms */
941         msleep(1);
942
943         /* issue the second D2H Register FIS */
944         ahci_fill_cmd_slot(pp, cmd_fis_len);
945
946         tf.ctl &= ~ATA_SRST;
947         ata_tf_to_fis(&tf, fis, 0);
948         fis[1] &= ~(1 << 7);    /* turn off Command FIS bit */
949
950         writel(1, port_mmio + PORT_CMD_ISSUE);
951         readl(port_mmio + PORT_CMD_ISSUE);      /* flush */
952
953         /* spec mandates ">= 2ms" before checking status.
954          * We wait 150ms, because that was the magic delay used for
955          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
956          * between when the ATA command register is written, and then
957          * status is checked.  Because waiting for "a while" before
958          * checking status is fine, post SRST, we perform this magic
959          * delay here as well.
960          */
961         msleep(150);
962
963         *class = ATA_DEV_NONE;
964         if (sata_dev_present(ap)) {
965                 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
966                         rc = -EIO;
967                         reason = "device not ready";
968                         goto fail;
969                 }
970                 *class = ahci_dev_classify(ap);
971         }
972
973         DPRINTK("EXIT, class=%u\n", *class);
974         return 0;
975
976  fail_restart:
977         ahci_start_engine(port_mmio);
978  fail:
979         if (verbose)
980                 printk(KERN_ERR "ata%u: softreset failed (%s)\n",
981                        ap->id, reason);
982         else
983                 DPRINTK("EXIT, rc=%d reason=\"%s\"\n", rc, reason);
984         return rc;
985 }
986
987 static int ahci_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
988 {
989         int rc;
990         void __iomem *mmio = ap->host_set->mmio_base;
991         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
992
993
994         DPRINTK("ENTER\n");
995
996         ahci_stop_engine(port_mmio);
997         rc = sata_std_hardreset(ap, verbose, class);
998         ahci_start_engine(port_mmio);
999
1000         if (rc == 0)
1001                 *class = ahci_dev_classify(ap);
1002         if (*class == ATA_DEV_UNKNOWN)
1003                 *class = ATA_DEV_NONE;
1004
1005         DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1006         return rc;
1007 }
1008
1009 static void ahci_postreset(struct ata_port *ap, unsigned int *class)
1010 {
1011         void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
1012         struct ahci_host_priv *hpriv = ap->host_set->private_data;
1013         u32 new_tmp, tmp;
1014
1015         ata_std_postreset(ap, class);
1016
1017         /* Make sure port's ATAPI bit is set appropriately */
1018         new_tmp = tmp = readl(port_mmio + PORT_CMD);
1019         if (*class == ATA_DEV_ATAPI)
1020                 new_tmp |= PORT_CMD_ATAPI;
1021         else
1022                 new_tmp &= ~PORT_CMD_ATAPI;
1023         if (new_tmp != tmp) {
1024                 writel(new_tmp, port_mmio + PORT_CMD);
1025                 readl(port_mmio + PORT_CMD); /* flush */
1026         }
1027
1028         if (*class != ATA_DEV_NONE)
1029                 hpriv->dev_map |= (1 << ap->port_no);
1030 }
1031
1032 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
1033 {
1034         return ata_drive_probe_reset(ap, ata_std_probeinit,
1035                                      ahci_softreset, ahci_hardreset,
1036                                      ahci_postreset, classes);
1037 }
1038
1039 static u8 ahci_check_status(struct ata_port *ap)
1040 {
1041         void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
1042
1043         return readl(mmio + PORT_TFDATA) & 0xFF;
1044 }
1045
1046 static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
1047 {
1048         struct ahci_port_priv *pp = ap->private_data;
1049         u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1050
1051         ata_tf_from_fis(d2h_fis, tf);
1052 }
1053
1054 static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc)
1055 {
1056         struct ahci_port_priv *pp = qc->ap->private_data;
1057         struct scatterlist *sg;
1058         struct ahci_sg *ahci_sg;
1059         unsigned int n_sg = 0;
1060
1061         VPRINTK("ENTER\n");
1062
1063         /*
1064          * Next, the S/G list.
1065          */
1066         ahci_sg = pp->cmd_tbl_sg;
1067         ata_for_each_sg(sg, qc) {
1068                 dma_addr_t addr = sg_dma_address(sg);
1069                 u32 sg_len = sg_dma_len(sg);
1070
1071                 ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
1072                 ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
1073                 ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
1074
1075                 ahci_sg++;
1076                 n_sg++;
1077         }
1078
1079         return n_sg;
1080 }
1081
1082 static void ahci_qc_prep(struct ata_queued_cmd *qc)
1083 {
1084         struct ata_port *ap = qc->ap;
1085         struct ahci_port_priv *pp = ap->private_data;
1086         int is_atapi = is_atapi_taskfile(&qc->tf);
1087         u32 opts;
1088         const u32 cmd_fis_len = 5; /* five dwords */
1089         unsigned int n_elem;
1090
1091         /*
1092          * Fill in command table information.  First, the header,
1093          * a SATA Register - Host to Device command FIS.
1094          */
1095         ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
1096         if (is_atapi) {
1097                 memset(pp->cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1098                 memcpy(pp->cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb,
1099                        qc->dev->cdb_len);
1100         }
1101
1102         n_elem = 0;
1103         if (qc->flags & ATA_QCFLAG_DMAMAP)
1104                 n_elem = ahci_fill_sg(qc);
1105
1106         /*
1107          * Fill in command slot information.
1108          */
1109         opts = cmd_fis_len | n_elem << 16;
1110         if (qc->tf.flags & ATA_TFLAG_WRITE)
1111                 opts |= AHCI_CMD_WRITE;
1112         if (is_atapi)
1113                 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1114
1115         ahci_fill_cmd_slot(pp, opts);
1116 }
1117
1118 static void ahci_restart_port(struct ata_port *ap, u32 irq_stat)
1119 {
1120         void __iomem *mmio = ap->host_set->mmio_base;
1121         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1122         u32 tmp;
1123
1124         if ((ap->device[0].class != ATA_DEV_ATAPI) ||
1125             ((irq_stat & PORT_IRQ_TF_ERR) == 0))
1126                 printk(KERN_WARNING "ata%u: port reset, "
1127                        "p_is %x is %x pis %x cmd %x tf %x ss %x se %x\n",
1128                         ap->id,
1129                         irq_stat,
1130                         readl(mmio + HOST_IRQ_STAT),
1131                         readl(port_mmio + PORT_IRQ_STAT),
1132                         readl(port_mmio + PORT_CMD),
1133                         readl(port_mmio + PORT_TFDATA),
1134                         readl(port_mmio + PORT_SCR_STAT),
1135                         readl(port_mmio + PORT_SCR_ERR));
1136
1137         /* stop DMA */
1138         ahci_stop_engine(port_mmio);
1139
1140         /* clear SATA phy error, if any */
1141         tmp = readl(port_mmio + PORT_SCR_ERR);
1142         writel(tmp, port_mmio + PORT_SCR_ERR);
1143
1144         /* if DRQ/BSY is set, device needs to be reset.
1145          * if so, issue COMRESET
1146          */
1147         tmp = readl(port_mmio + PORT_TFDATA);
1148         if (tmp & (ATA_BUSY | ATA_DRQ)) {
1149                 writel(0x301, port_mmio + PORT_SCR_CTL);
1150                 readl(port_mmio + PORT_SCR_CTL); /* flush */
1151                 udelay(10);
1152                 writel(0x300, port_mmio + PORT_SCR_CTL);
1153                 readl(port_mmio + PORT_SCR_CTL); /* flush */
1154         }
1155
1156         /* re-start DMA */
1157         ahci_start_engine(port_mmio);
1158 }
1159
1160 static void ahci_eng_timeout(struct ata_port *ap)
1161 {
1162         struct ata_host_set *host_set = ap->host_set;
1163         void __iomem *mmio = host_set->mmio_base;
1164         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1165         struct ata_queued_cmd *qc;
1166         unsigned long flags;
1167
1168         printk(KERN_WARNING "ata%u: handling error/timeout\n", ap->id);
1169
1170         spin_lock_irqsave(&host_set->lock, flags);
1171
1172         ahci_restart_port(ap, readl(port_mmio + PORT_IRQ_STAT));
1173         qc = ata_qc_from_tag(ap, ap->active_tag);
1174         qc->err_mask |= AC_ERR_TIMEOUT;
1175
1176         spin_unlock_irqrestore(&host_set->lock, flags);
1177
1178         ata_eh_qc_complete(qc);
1179 }
1180
1181 int ahci_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
1182 {
1183         struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
1184         struct ata_device *dev = &ap->device[sdev->id];
1185         int rc;
1186
1187         rc = ata_device_suspend(ap, dev, state);
1188
1189         if (!rc)
1190                 rc = ahci_port_suspend(ap, state);
1191
1192         return rc;
1193 }
1194
1195 int ahci_scsi_device_resume(struct scsi_device *sdev)
1196 {
1197         struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
1198         struct ata_device *dev = &ap->device[sdev->id];
1199
1200         ahci_port_resume(ap);
1201
1202         return ata_device_resume(ap, dev);
1203 }
1204
1205 int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
1206 {
1207         struct device *dev = pci_dev_to_dev(pdev);
1208         struct ata_host_set *host_set = dev_get_drvdata(dev);
1209         void __iomem *mmio = host_set->mmio_base;
1210         u32 tmp;
1211
1212         /*
1213          * AHCI spec rev1.1 section 8.3.3:
1214          * Software must disable interrupts prior to
1215          * requesting a transition of the HBA to
1216          * D3 state.
1217          */
1218         tmp = readl(mmio + HOST_CTL);
1219         tmp &= ~HOST_IRQ_EN;
1220         writel(tmp, mmio + HOST_CTL);
1221         tmp = readl(mmio + HOST_CTL); /* flush */
1222
1223         return ata_pci_device_suspend(pdev, state);
1224 }
1225
1226 int ahci_pci_device_resume(struct pci_dev *pdev)
1227 {
1228         struct device *dev = pci_dev_to_dev(pdev);
1229         struct ata_host_set *host_set = dev_get_drvdata(dev);
1230         void __iomem *mmio = host_set->mmio_base;
1231         u32 tmp;
1232
1233         /*
1234          * Enabling AHCI mode
1235          */
1236         tmp = readl(mmio + HOST_CTL);
1237         if (!(tmp & HOST_AHCI_EN)) {
1238                 tmp |= HOST_AHCI_EN;
1239                 writel(tmp, mmio + HOST_CTL);
1240                 tmp = readl(mmio + HOST_CTL);
1241         }
1242
1243         return ata_pci_device_resume(pdev);
1244 }
1245
1246 static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1247 {
1248         void __iomem *mmio = ap->host_set->mmio_base;
1249         void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1250         u32 status, serr, ci;
1251
1252         serr = readl(port_mmio + PORT_SCR_ERR);
1253         writel(serr, port_mmio + PORT_SCR_ERR);
1254
1255         status = readl(port_mmio + PORT_IRQ_STAT);
1256         writel(status, port_mmio + PORT_IRQ_STAT);
1257
1258         ci = readl(port_mmio + PORT_CMD_ISSUE);
1259         if (likely((ci & 0x1) == 0)) {
1260                 if (qc) {
1261                         WARN_ON(qc->err_mask);
1262                         ata_qc_complete(qc);
1263                         qc = NULL;
1264                 }
1265         }
1266
1267         if (status & PORT_IRQ_FATAL) {
1268                 unsigned int err_mask;
1269                 if (status & PORT_IRQ_TF_ERR)
1270                         err_mask = AC_ERR_DEV;
1271                 else if (status & PORT_IRQ_IF_ERR)
1272                         err_mask = AC_ERR_ATA_BUS;
1273                 else
1274                         err_mask = AC_ERR_HOST_BUS;
1275
1276                 /* command processing has stopped due to error; restart */
1277                 ahci_restart_port(ap, status);
1278
1279                 if (qc) {
1280                         qc->err_mask |= err_mask;
1281                         ata_qc_complete(qc);
1282                 }
1283         }
1284
1285         return 1;
1286 }
1287
1288 static void ahci_irq_clear(struct ata_port *ap)
1289 {
1290         /* TODO */
1291 }
1292
1293 static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
1294 {
1295         struct ata_host_set *host_set = dev_instance;
1296         struct ahci_host_priv *hpriv;
1297         unsigned int i, handled = 0;
1298         void __iomem *mmio;
1299         u32 irq_stat, irq_ack = 0;
1300
1301         VPRINTK("ENTER\n");
1302
1303         hpriv = host_set->private_data;
1304         mmio = host_set->mmio_base;
1305
1306         /* sigh.  0xffffffff is a valid return from h/w */
1307         irq_stat = readl(mmio + HOST_IRQ_STAT);
1308         irq_stat &= hpriv->port_map;
1309         if (!irq_stat)
1310                 return IRQ_NONE;
1311
1312         spin_lock(&host_set->lock);
1313
1314         for (i = 0; i < host_set->n_ports; i++) {
1315                 struct ata_port *ap;
1316
1317                 if (!(irq_stat & (1 << i)))
1318                         continue;
1319
1320                 ap = host_set->ports[i];
1321                 if (ap) {
1322                         struct ata_queued_cmd *qc;
1323                         qc = ata_qc_from_tag(ap, ap->active_tag);
1324                         if (!ahci_host_intr(ap, qc))
1325                                 if (ata_ratelimit())
1326                                         dev_printk(KERN_WARNING, host_set->dev,
1327                                           "unhandled interrupt on port %u\n",
1328                                           i);
1329
1330                         VPRINTK("port %u\n", i);
1331                 } else {
1332                         VPRINTK("port %u (no irq)\n", i);
1333                         if (ata_ratelimit())
1334                                 dev_printk(KERN_WARNING, host_set->dev,
1335                                         "interrupt on disabled port %u\n", i);
1336                 }
1337
1338                 irq_ack |= (1 << i);
1339         }
1340
1341         if (irq_ack) {
1342                 writel(irq_ack, mmio + HOST_IRQ_STAT);
1343                 handled = 1;
1344         }
1345
1346         spin_unlock(&host_set->lock);
1347
1348         VPRINTK("EXIT\n");
1349
1350         return IRQ_RETVAL(handled);
1351 }
1352
1353 static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
1354 {
1355         struct ata_port *ap = qc->ap;
1356         void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
1357
1358         writel(1, port_mmio + PORT_CMD_ISSUE);
1359         readl(port_mmio + PORT_CMD_ISSUE);      /* flush */
1360
1361         return 0;
1362 }
1363
1364 static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
1365                             unsigned int port_idx)
1366 {
1367         VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
1368         base = ahci_port_base_ul(base, port_idx);
1369         VPRINTK("base now==0x%lx\n", base);
1370
1371         port->cmd_addr          = base;
1372         port->scr_addr          = base + PORT_SCR;
1373
1374         VPRINTK("EXIT\n");
1375 }
1376
1377 static int ahci_host_init(struct ata_probe_ent *probe_ent)
1378 {
1379         struct ahci_host_priv *hpriv = probe_ent->private_data;
1380         struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1381         void __iomem *mmio = probe_ent->mmio_base;
1382         u32 tmp, cap_save;
1383         unsigned int i, j, using_dac;
1384         int rc;
1385         void __iomem *port_mmio;
1386
1387         cap_save = readl(mmio + HOST_CAP);
1388         cap_save &= ( (1<<28) | (1<<17) );
1389         cap_save |= (1 << 27);
1390
1391         /* global controller reset */
1392         tmp = readl(mmio + HOST_CTL);
1393         if ((tmp & HOST_RESET) == 0) {
1394                 writel(tmp | HOST_RESET, mmio + HOST_CTL);
1395                 readl(mmio + HOST_CTL); /* flush */
1396         }
1397
1398         /* reset must complete within 1 second, or
1399          * the hardware should be considered fried.
1400          */
1401         ssleep(1);
1402
1403         tmp = readl(mmio + HOST_CTL);
1404         if (tmp & HOST_RESET) {
1405                 dev_printk(KERN_ERR, &pdev->dev,
1406                            "controller reset failed (0x%x)\n", tmp);
1407                 return -EIO;
1408         }
1409
1410         writel(HOST_AHCI_EN, mmio + HOST_CTL);
1411         (void) readl(mmio + HOST_CTL);  /* flush */
1412         writel(cap_save, mmio + HOST_CAP);
1413         writel(0xf, mmio + HOST_PORTS_IMPL);
1414         (void) readl(mmio + HOST_PORTS_IMPL);   /* flush */
1415
1416         if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1417                 u16 tmp16;
1418
1419                 pci_read_config_word(pdev, 0x92, &tmp16);
1420                 tmp16 |= 0xf;
1421                 pci_write_config_word(pdev, 0x92, tmp16);
1422         }
1423
1424         hpriv->cap = readl(mmio + HOST_CAP);
1425         hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
1426         hpriv->dev_map = 0;
1427         probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
1428
1429         VPRINTK("cap 0x%x  port_map 0x%x  n_ports %d\n",
1430                 hpriv->cap, hpriv->port_map, probe_ent->n_ports);
1431
1432         using_dac = hpriv->cap & HOST_CAP_64;
1433         if (using_dac &&
1434             !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
1435                 rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1436                 if (rc) {
1437                         rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1438                         if (rc) {
1439                                 dev_printk(KERN_ERR, &pdev->dev,
1440                                            "64-bit DMA enable failed\n");
1441                                 return rc;
1442                         }
1443                 }
1444         } else {
1445                 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1446                 if (rc) {
1447                         dev_printk(KERN_ERR, &pdev->dev,
1448                                    "32-bit DMA enable failed\n");
1449                         return rc;
1450                 }
1451                 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1452                 if (rc) {
1453                         dev_printk(KERN_ERR, &pdev->dev,
1454                                    "32-bit consistent DMA enable failed\n");
1455                         return rc;
1456                 }
1457         }
1458
1459         for (i = 0; i < probe_ent->n_ports; i++) {
1460 #if 0 /* BIOSen initialize this incorrectly */
1461                 if (!(hpriv->port_map & (1 << i)))
1462                         continue;
1463 #endif
1464
1465                 port_mmio = ahci_port_base(mmio, i);
1466                 VPRINTK("mmio %p  port_mmio %p\n", mmio, port_mmio);
1467
1468                 ahci_setup_port(&probe_ent->port[i],
1469                                 (unsigned long) mmio, i);
1470
1471                 /* make sure port is not active */
1472                 rc = ahci_stop_engine(port_mmio);
1473                 if (rc)
1474                         printk(KERN_WARNING "ata%u: DMA engine busy (rc %d)\n",
1475                                i, rc);
1476
1477                 rc = ahci_stop_fis_rx(port_mmio);
1478                 if (rc)
1479                         printk(KERN_WARNING "ata%u: FIS RX not stopped (rc %d)\n",
1480                                i, rc);
1481
1482                 /*
1483                  * Actually, this is wrong again.
1484                  * AHCI spec says that we first should
1485                  * enable FIS reception before sending
1486                  * SPIN_UP to the device ...
1487                  */
1488
1489                 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
1490
1491                 /*
1492                  * Wait for the communications link to establish
1493                  */
1494
1495                 j = 0;
1496                 while (j < 100) {
1497                         msleep(10);
1498                         tmp = readl(port_mmio + PORT_SCR_STAT);
1499                         if ((tmp & 0xf) == 0x3)
1500                                 break;
1501                         j++;
1502                 }
1503
1504                 tmp = readl(port_mmio + PORT_SCR_ERR);
1505                 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1506                 writel(tmp, port_mmio + PORT_SCR_ERR);
1507
1508                 /* ack any pending irq events for this port */
1509                 tmp = readl(port_mmio + PORT_IRQ_STAT);
1510                 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1511                 if (tmp)
1512                         writel(tmp, port_mmio + PORT_IRQ_STAT);
1513
1514                 writel(1 << i, mmio + HOST_IRQ_STAT);
1515
1516                 /* set irq mask (enables interrupts) */
1517                 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
1518         }
1519
1520         tmp = readl(mmio + HOST_CTL);
1521         VPRINTK("HOST_CTL 0x%x\n", tmp);
1522         writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1523         tmp = readl(mmio + HOST_CTL);
1524         VPRINTK("HOST_CTL 0x%x\n", tmp);
1525
1526         pci_set_master(pdev);
1527
1528         return 0;
1529 }
1530
1531 static void ahci_print_info(struct ata_probe_ent *probe_ent)
1532 {
1533         struct ahci_host_priv *hpriv = probe_ent->private_data;
1534         struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1535         void __iomem *mmio = probe_ent->mmio_base;
1536         u32 vers, cap, impl, speed;
1537         const char *speed_s;
1538         u16 cc;
1539         const char *scc_s;
1540
1541         vers = readl(mmio + HOST_VERSION);
1542         cap = hpriv->cap;
1543         impl = hpriv->port_map;
1544
1545         speed = (cap >> 20) & 0xf;
1546         if (speed == 1)
1547                 speed_s = "1.5";
1548         else if (speed == 2)
1549                 speed_s = "3";
1550         else
1551                 speed_s = "?";
1552
1553         pci_read_config_word(pdev, 0x0a, &cc);
1554         if (cc == 0x0101)
1555                 scc_s = "IDE";
1556         else if (cc == 0x0106)
1557                 scc_s = "SATA";
1558         else if (cc == 0x0104)
1559                 scc_s = "RAID";
1560         else
1561                 scc_s = "unknown";
1562
1563         dev_printk(KERN_INFO, &pdev->dev,
1564                 "AHCI %02x%02x.%02x%02x "
1565                 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
1566                 ,
1567
1568                 (vers >> 24) & 0xff,
1569                 (vers >> 16) & 0xff,
1570                 (vers >> 8) & 0xff,
1571                 vers & 0xff,
1572
1573                 ((cap >> 8) & 0x1f) + 1,
1574                 (cap & 0x1f) + 1,
1575                 speed_s,
1576                 impl,
1577                 scc_s);
1578
1579         dev_printk(KERN_INFO, &pdev->dev,
1580                 "flags: "
1581                 "%s%s%s%s%s%s"
1582                 "%s%s%s%s%s%s%s\n"
1583                 ,
1584
1585                 cap & (1 << 31) ? "64bit " : "",
1586                 cap & (1 << 30) ? "ncq " : "",
1587                 cap & (1 << 28) ? "ilck " : "",
1588                 cap & (1 << 27) ? "stag " : "",
1589                 cap & (1 << 26) ? "pm " : "",
1590                 cap & (1 << 25) ? "led " : "",
1591
1592                 cap & (1 << 24) ? "clo " : "",
1593                 cap & (1 << 19) ? "nz " : "",
1594                 cap & (1 << 18) ? "only " : "",
1595                 cap & (1 << 17) ? "pmp " : "",
1596                 cap & (1 << 15) ? "pio " : "",
1597                 cap & (1 << 14) ? "slum " : "",
1598                 cap & (1 << 13) ? "part " : ""
1599                 );
1600 }
1601
1602 static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1603 {
1604         static int printed_version;
1605         struct ata_probe_ent *probe_ent = NULL;
1606         struct ahci_host_priv *hpriv;
1607         unsigned long base;
1608         void __iomem *mmio_base;
1609         unsigned int board_idx = (unsigned int) ent->driver_data;
1610         int have_msi, pci_dev_busy = 0;
1611         int rc;
1612
1613         VPRINTK("ENTER\n");
1614
1615         if (!printed_version++)
1616                 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
1617
1618         rc = pci_enable_device(pdev);
1619         if (rc)
1620                 return rc;
1621
1622         rc = pci_request_regions(pdev, DRV_NAME);
1623         if (rc) {
1624                 pci_dev_busy = 1;
1625                 goto err_out;
1626         }
1627
1628         if (pci_enable_msi(pdev) == 0)
1629                 have_msi = 1;
1630         else {
1631                 pci_intx(pdev, 1);
1632                 have_msi = 0;
1633         }
1634
1635         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
1636         if (probe_ent == NULL) {
1637                 rc = -ENOMEM;
1638                 goto err_out_msi;
1639         }
1640
1641         memset(probe_ent, 0, sizeof(*probe_ent));
1642         probe_ent->dev = pci_dev_to_dev(pdev);
1643         INIT_LIST_HEAD(&probe_ent->node);
1644
1645         mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
1646         if (mmio_base == NULL) {
1647                 rc = -ENOMEM;
1648                 goto err_out_free_ent;
1649         }
1650         base = (unsigned long) mmio_base;
1651
1652         hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
1653         if (!hpriv) {
1654                 rc = -ENOMEM;
1655                 goto err_out_iounmap;
1656         }
1657         memset(hpriv, 0, sizeof(*hpriv));
1658
1659         probe_ent->sht          = ahci_port_info[board_idx].sht;
1660         probe_ent->host_flags   = ahci_port_info[board_idx].host_flags;
1661         probe_ent->pio_mask     = ahci_port_info[board_idx].pio_mask;
1662         probe_ent->udma_mask    = ahci_port_info[board_idx].udma_mask;
1663         probe_ent->port_ops     = ahci_port_info[board_idx].port_ops;
1664
1665         probe_ent->irq = pdev->irq;
1666         probe_ent->irq_flags = SA_SHIRQ;
1667         probe_ent->mmio_base = mmio_base;
1668         probe_ent->private_data = hpriv;
1669
1670         if (have_msi)
1671                 hpriv->flags |= AHCI_FLAG_MSI;
1672
1673         /* JMicron-specific fixup: make sure we're in AHCI mode */
1674         if (pdev->vendor == 0x197b)
1675                 pci_write_config_byte(pdev, 0x41, 0xa1);
1676
1677         /* initialize adapter */
1678         rc = ahci_host_init(probe_ent);
1679         if (rc)
1680                 goto err_out_hpriv;
1681
1682         ahci_print_info(probe_ent);
1683
1684         /* FIXME: check ata_device_add return value */
1685         ata_device_add(probe_ent);
1686         kfree(probe_ent);
1687
1688         return 0;
1689
1690 err_out_hpriv:
1691         kfree(hpriv);
1692 err_out_iounmap:
1693         pci_iounmap(pdev, mmio_base);
1694 err_out_free_ent:
1695         kfree(probe_ent);
1696 err_out_msi:
1697         if (have_msi)
1698                 pci_disable_msi(pdev);
1699         else
1700                 pci_intx(pdev, 0);
1701         pci_release_regions(pdev);
1702 err_out:
1703         if (!pci_dev_busy)
1704                 pci_disable_device(pdev);
1705         return rc;
1706 }
1707
1708 static void ahci_remove_one (struct pci_dev *pdev)
1709 {
1710         struct device *dev = pci_dev_to_dev(pdev);
1711         struct ata_host_set *host_set = dev_get_drvdata(dev);
1712         struct ahci_host_priv *hpriv = host_set->private_data;
1713         struct ata_port *ap;
1714         unsigned int i;
1715         int have_msi;
1716
1717         for (i = 0; i < host_set->n_ports; i++) {
1718                 ap = host_set->ports[i];
1719
1720                 scsi_remove_host(ap->host);
1721         }
1722
1723         have_msi = hpriv->flags & AHCI_FLAG_MSI;
1724         free_irq(host_set->irq, host_set);
1725
1726         for (i = 0; i < host_set->n_ports; i++) {
1727                 ap = host_set->ports[i];
1728
1729                 ata_scsi_release(ap->host);
1730                 scsi_host_put(ap->host);
1731         }
1732
1733         kfree(hpriv);
1734         pci_iounmap(pdev, host_set->mmio_base);
1735         kfree(host_set);
1736
1737         if (have_msi)
1738                 pci_disable_msi(pdev);
1739         else
1740                 pci_intx(pdev, 0);
1741         pci_release_regions(pdev);
1742         pci_disable_device(pdev);
1743         dev_set_drvdata(dev, NULL);
1744 }
1745
1746 static int __init ahci_init(void)
1747 {
1748         return pci_module_init(&ahci_pci_driver);
1749 }
1750
1751 static void __exit ahci_exit(void)
1752 {
1753         pci_unregister_driver(&ahci_pci_driver);
1754 }
1755
1756
1757 MODULE_AUTHOR("Jeff Garzik");
1758 MODULE_DESCRIPTION("AHCI SATA low-level driver");
1759 MODULE_LICENSE("GPL");
1760 MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
1761 MODULE_VERSION(DRV_VERSION);
1762
1763 module_init(ahci_init);
1764 module_exit(ahci_exit);