]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/sata_mv.c
Merge branch 'master'
[mv-sheeva.git] / drivers / scsi / sata_mv.c
1 /*
2  * sata_mv.c - Marvell SATA support
3  *
4  * Copyright 2005: EMC Corporation, all rights reserved. 
5  *
6  * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/sched.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/device.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <linux/libata.h>
36 #include <asm/io.h>
37
38 #define DRV_NAME        "sata_mv"
39 #define DRV_VERSION     "0.25"
40
41 enum {
42         /* BAR's are enumerated in terms of pci_resource_start() terms */
43         MV_PRIMARY_BAR          = 0,    /* offset 0x10: memory space */
44         MV_IO_BAR               = 2,    /* offset 0x18: IO space */
45         MV_MISC_BAR             = 3,    /* offset 0x1c: FLASH, NVRAM, SRAM */
46
47         MV_MAJOR_REG_AREA_SZ    = 0x10000,      /* 64KB */
48         MV_MINOR_REG_AREA_SZ    = 0x2000,       /* 8KB */
49
50         MV_PCI_REG_BASE         = 0,
51         MV_IRQ_COAL_REG_BASE    = 0x18000,      /* 6xxx part only */
52         MV_SATAHC0_REG_BASE     = 0x20000,
53
54         MV_PCI_REG_SZ           = MV_MAJOR_REG_AREA_SZ,
55         MV_SATAHC_REG_SZ        = MV_MAJOR_REG_AREA_SZ,
56         MV_SATAHC_ARBTR_REG_SZ  = MV_MINOR_REG_AREA_SZ,         /* arbiter */
57         MV_PORT_REG_SZ          = MV_MINOR_REG_AREA_SZ,
58
59         MV_USE_Q_DEPTH          = ATA_DEF_QUEUE,
60
61         MV_MAX_Q_DEPTH          = 32,
62         MV_MAX_Q_DEPTH_MASK     = MV_MAX_Q_DEPTH - 1,
63
64         /* CRQB needs alignment on a 1KB boundary. Size == 1KB
65          * CRPB needs alignment on a 256B boundary. Size == 256B
66          * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
67          * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
68          */
69         MV_CRQB_Q_SZ            = (32 * MV_MAX_Q_DEPTH),
70         MV_CRPB_Q_SZ            = (8 * MV_MAX_Q_DEPTH),
71         MV_MAX_SG_CT            = 176,
72         MV_SG_TBL_SZ            = (16 * MV_MAX_SG_CT),
73         MV_PORT_PRIV_DMA_SZ     = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
74
75         /* Our DMA boundary is determined by an ePRD being unable to handle
76          * anything larger than 64KB
77          */
78         MV_DMA_BOUNDARY         = 0xffffU,
79
80         MV_PORTS_PER_HC         = 4,
81         /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
82         MV_PORT_HC_SHIFT        = 2,
83         /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
84         MV_PORT_MASK            = 3,
85
86         /* Host Flags */
87         MV_FLAG_DUAL_HC         = (1 << 30),  /* two SATA Host Controllers */
88         MV_FLAG_IRQ_COALESCE    = (1 << 29),  /* IRQ coalescing capability */
89         MV_FLAG_GLBL_SFT_RST    = (1 << 28),  /* Global Soft Reset support */
90         MV_COMMON_FLAGS         = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
91                                    ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
92                                    ATA_FLAG_PIO_POLLING),
93         MV_6XXX_FLAGS           = (MV_FLAG_IRQ_COALESCE | 
94                                    MV_FLAG_GLBL_SFT_RST),
95
96         chip_504x               = 0,
97         chip_508x               = 1,
98         chip_604x               = 2,
99         chip_608x               = 3,
100
101         CRQB_FLAG_READ          = (1 << 0),
102         CRQB_TAG_SHIFT          = 1,
103         CRQB_CMD_ADDR_SHIFT     = 8,
104         CRQB_CMD_CS             = (0x2 << 11),
105         CRQB_CMD_LAST           = (1 << 15),
106
107         CRPB_FLAG_STATUS_SHIFT  = 8,
108
109         EPRD_FLAG_END_OF_TBL    = (1 << 31),
110
111         /* PCI interface registers */
112
113         PCI_COMMAND_OFS         = 0xc00,
114
115         PCI_MAIN_CMD_STS_OFS    = 0xd30,
116         STOP_PCI_MASTER         = (1 << 2),
117         PCI_MASTER_EMPTY        = (1 << 3),
118         GLOB_SFT_RST            = (1 << 4),
119
120         PCI_IRQ_CAUSE_OFS       = 0x1d58,
121         PCI_IRQ_MASK_OFS        = 0x1d5c,
122         PCI_UNMASK_ALL_IRQS     = 0x7fffff,     /* bits 22-0 */
123
124         HC_MAIN_IRQ_CAUSE_OFS   = 0x1d60,
125         HC_MAIN_IRQ_MASK_OFS    = 0x1d64,
126         PORT0_ERR               = (1 << 0),     /* shift by port # */
127         PORT0_DONE              = (1 << 1),     /* shift by port # */
128         HC0_IRQ_PEND            = 0x1ff,        /* bits 0-8 = HC0's ports */
129         HC_SHIFT                = 9,            /* bits 9-17 = HC1's ports */
130         PCI_ERR                 = (1 << 18),
131         TRAN_LO_DONE            = (1 << 19),    /* 6xxx: IRQ coalescing */
132         TRAN_HI_DONE            = (1 << 20),    /* 6xxx: IRQ coalescing */
133         PORTS_0_7_COAL_DONE     = (1 << 21),    /* 6xxx: IRQ coalescing */
134         GPIO_INT                = (1 << 22),
135         SELF_INT                = (1 << 23),
136         TWSI_INT                = (1 << 24),
137         HC_MAIN_RSVD            = (0x7f << 25), /* bits 31-25 */
138         HC_MAIN_MASKED_IRQS     = (TRAN_LO_DONE | TRAN_HI_DONE | 
139                                    PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
140                                    HC_MAIN_RSVD),
141
142         /* SATAHC registers */
143         HC_CFG_OFS              = 0,
144
145         HC_IRQ_CAUSE_OFS        = 0x14,
146         CRPB_DMA_DONE           = (1 << 0),     /* shift by port # */
147         HC_IRQ_COAL             = (1 << 4),     /* IRQ coalescing */
148         DEV_IRQ                 = (1 << 8),     /* shift by port # */
149
150         /* Shadow block registers */
151         SHD_BLK_OFS             = 0x100,
152         SHD_CTL_AST_OFS         = 0x20,         /* ofs from SHD_BLK_OFS */
153
154         /* SATA registers */
155         SATA_STATUS_OFS         = 0x300,  /* ctrl, err regs follow status */
156         SATA_ACTIVE_OFS         = 0x350,
157
158         /* Port registers */
159         EDMA_CFG_OFS            = 0,
160         EDMA_CFG_Q_DEPTH        = 0,                    /* queueing disabled */
161         EDMA_CFG_NCQ            = (1 << 5),
162         EDMA_CFG_NCQ_GO_ON_ERR  = (1 << 14),            /* continue on error */
163         EDMA_CFG_RD_BRST_EXT    = (1 << 11),            /* read burst 512B */
164         EDMA_CFG_WR_BUFF_LEN    = (1 << 13),            /* write buffer 512B */
165
166         EDMA_ERR_IRQ_CAUSE_OFS  = 0x8,
167         EDMA_ERR_IRQ_MASK_OFS   = 0xc,
168         EDMA_ERR_D_PAR          = (1 << 0),
169         EDMA_ERR_PRD_PAR        = (1 << 1),
170         EDMA_ERR_DEV            = (1 << 2),
171         EDMA_ERR_DEV_DCON       = (1 << 3),
172         EDMA_ERR_DEV_CON        = (1 << 4),
173         EDMA_ERR_SERR           = (1 << 5),
174         EDMA_ERR_SELF_DIS       = (1 << 7),
175         EDMA_ERR_BIST_ASYNC     = (1 << 8),
176         EDMA_ERR_CRBQ_PAR       = (1 << 9),
177         EDMA_ERR_CRPB_PAR       = (1 << 10),
178         EDMA_ERR_INTRL_PAR      = (1 << 11),
179         EDMA_ERR_IORDY          = (1 << 12),
180         EDMA_ERR_LNK_CTRL_RX    = (0xf << 13),
181         EDMA_ERR_LNK_CTRL_RX_2  = (1 << 15),
182         EDMA_ERR_LNK_DATA_RX    = (0xf << 17),
183         EDMA_ERR_LNK_CTRL_TX    = (0x1f << 21),
184         EDMA_ERR_LNK_DATA_TX    = (0x1f << 26),
185         EDMA_ERR_TRANS_PROTO    = (1 << 31),
186         EDMA_ERR_FATAL          = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR | 
187                                    EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
188                                    EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
189                                    EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 | 
190                                    EDMA_ERR_LNK_DATA_RX |
191                                    EDMA_ERR_LNK_DATA_TX | 
192                                    EDMA_ERR_TRANS_PROTO),
193
194         EDMA_REQ_Q_BASE_HI_OFS  = 0x10,
195         EDMA_REQ_Q_IN_PTR_OFS   = 0x14,         /* also contains BASE_LO */
196         EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
197
198         EDMA_REQ_Q_OUT_PTR_OFS  = 0x18,
199         EDMA_REQ_Q_PTR_SHIFT    = 5,
200
201         EDMA_RSP_Q_BASE_HI_OFS  = 0x1c,
202         EDMA_RSP_Q_IN_PTR_OFS   = 0x20,
203         EDMA_RSP_Q_OUT_PTR_OFS  = 0x24,         /* also contains BASE_LO */
204         EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
205         EDMA_RSP_Q_PTR_SHIFT    = 3,
206
207         EDMA_CMD_OFS            = 0x28,
208         EDMA_EN                 = (1 << 0),
209         EDMA_DS                 = (1 << 1),
210         ATA_RST                 = (1 << 2),
211
212         /* Host private flags (hp_flags) */
213         MV_HP_FLAG_MSI          = (1 << 0),
214
215         /* Port private flags (pp_flags) */
216         MV_PP_FLAG_EDMA_EN      = (1 << 0),
217         MV_PP_FLAG_EDMA_DS_ACT  = (1 << 1),
218 };
219
220 /* Command ReQuest Block: 32B */
221 struct mv_crqb {
222         u32                     sg_addr;
223         u32                     sg_addr_hi;
224         u16                     ctrl_flags;
225         u16                     ata_cmd[11];
226 };
227
228 /* Command ResPonse Block: 8B */
229 struct mv_crpb {
230         u16                     id;
231         u16                     flags;
232         u32                     tmstmp;
233 };
234
235 /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
236 struct mv_sg {
237         u32                     addr;
238         u32                     flags_size;
239         u32                     addr_hi;
240         u32                     reserved;
241 };
242
243 struct mv_port_priv {
244         struct mv_crqb          *crqb;
245         dma_addr_t              crqb_dma;
246         struct mv_crpb          *crpb;
247         dma_addr_t              crpb_dma;
248         struct mv_sg            *sg_tbl;
249         dma_addr_t              sg_tbl_dma;
250
251         unsigned                req_producer;           /* cp of req_in_ptr */
252         unsigned                rsp_consumer;           /* cp of rsp_out_ptr */
253         u32                     pp_flags;
254 };
255
256 struct mv_host_priv {
257         u32                     hp_flags;
258 };
259
260 static void mv_irq_clear(struct ata_port *ap);
261 static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
262 static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
263 static void mv_phy_reset(struct ata_port *ap);
264 static void mv_host_stop(struct ata_host_set *host_set);
265 static int mv_port_start(struct ata_port *ap);
266 static void mv_port_stop(struct ata_port *ap);
267 static void mv_qc_prep(struct ata_queued_cmd *qc);
268 static int mv_qc_issue(struct ata_queued_cmd *qc);
269 static irqreturn_t mv_interrupt(int irq, void *dev_instance,
270                                 struct pt_regs *regs);
271 static void mv_eng_timeout(struct ata_port *ap);
272 static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
273
274 static struct scsi_host_template mv_sht = {
275         .module                 = THIS_MODULE,
276         .name                   = DRV_NAME,
277         .ioctl                  = ata_scsi_ioctl,
278         .queuecommand           = ata_scsi_queuecmd,
279         .eh_strategy_handler    = ata_scsi_error,
280         .can_queue              = MV_USE_Q_DEPTH,
281         .this_id                = ATA_SHT_THIS_ID,
282         .sg_tablesize           = MV_MAX_SG_CT,
283         .max_sectors            = ATA_MAX_SECTORS,
284         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
285         .emulated               = ATA_SHT_EMULATED,
286         .use_clustering         = ATA_SHT_USE_CLUSTERING,
287         .proc_name              = DRV_NAME,
288         .dma_boundary           = MV_DMA_BOUNDARY,
289         .slave_configure        = ata_scsi_slave_config,
290         .bios_param             = ata_std_bios_param,
291         .ordered_flush          = 1,
292 };
293
294 static const struct ata_port_operations mv_ops = {
295         .port_disable           = ata_port_disable,
296
297         .tf_load                = ata_tf_load,
298         .tf_read                = ata_tf_read,
299         .check_status           = ata_check_status,
300         .exec_command           = ata_exec_command,
301         .dev_select             = ata_std_dev_select,
302
303         .phy_reset              = mv_phy_reset,
304
305         .qc_prep                = mv_qc_prep,
306         .qc_issue               = mv_qc_issue,
307
308         .eng_timeout            = mv_eng_timeout,
309
310         .irq_handler            = mv_interrupt,
311         .irq_clear              = mv_irq_clear,
312
313         .scr_read               = mv_scr_read,
314         .scr_write              = mv_scr_write,
315
316         .port_start             = mv_port_start,
317         .port_stop              = mv_port_stop,
318         .host_stop              = mv_host_stop,
319 };
320
321 static struct ata_port_info mv_port_info[] = {
322         {  /* chip_504x */
323                 .sht            = &mv_sht,
324                 .host_flags     = MV_COMMON_FLAGS,
325                 .pio_mask       = 0x1f, /* pio0-4 */
326                 .udma_mask      = 0,    /* 0x7f (udma0-6 disabled for now) */
327                 .port_ops       = &mv_ops,
328         },
329         {  /* chip_508x */
330                 .sht            = &mv_sht,
331                 .host_flags     = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
332                 .pio_mask       = 0x1f, /* pio0-4 */
333                 .udma_mask      = 0,    /* 0x7f (udma0-6 disabled for now) */
334                 .port_ops       = &mv_ops,
335         },
336         {  /* chip_604x */
337                 .sht            = &mv_sht,
338                 .host_flags     = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
339                 .pio_mask       = 0x1f, /* pio0-4 */
340                 .udma_mask      = 0x7f, /* udma0-6 */
341                 .port_ops       = &mv_ops,
342         },
343         {  /* chip_608x */
344                 .sht            = &mv_sht,
345                 .host_flags     = (MV_COMMON_FLAGS | MV_6XXX_FLAGS | 
346                                    MV_FLAG_DUAL_HC),
347                 .pio_mask       = 0x1f, /* pio0-4 */
348                 .udma_mask      = 0x7f, /* udma0-6 */
349                 .port_ops       = &mv_ops,
350         },
351 };
352
353 static const struct pci_device_id mv_pci_tbl[] = {
354         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
355         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
356         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_508x},
357         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
358
359         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
360         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
361         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
362         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
363
364         {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
365         {}                      /* terminate list */
366 };
367
368 static struct pci_driver mv_pci_driver = {
369         .name                   = DRV_NAME,
370         .id_table               = mv_pci_tbl,
371         .probe                  = mv_init_one,
372         .remove                 = ata_pci_remove_one,
373 };
374
375 /*
376  * Functions
377  */
378
379 static inline void writelfl(unsigned long data, void __iomem *addr)
380 {
381         writel(data, addr);
382         (void) readl(addr);     /* flush to avoid PCI posted write */
383 }
384
385 static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
386 {
387         return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
388 }
389
390 static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
391 {
392         return (mv_hc_base(base, port >> MV_PORT_HC_SHIFT) +
393                 MV_SATAHC_ARBTR_REG_SZ + 
394                 ((port & MV_PORT_MASK) * MV_PORT_REG_SZ));
395 }
396
397 static inline void __iomem *mv_ap_base(struct ata_port *ap)
398 {
399         return mv_port_base(ap->host_set->mmio_base, ap->port_no);
400 }
401
402 static inline int mv_get_hc_count(unsigned long hp_flags)
403 {
404         return ((hp_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
405 }
406
407 static void mv_irq_clear(struct ata_port *ap)
408 {
409 }
410
411 /**
412  *      mv_start_dma - Enable eDMA engine
413  *      @base: port base address
414  *      @pp: port private data
415  *
416  *      Verify the local cache of the eDMA state is accurate with an
417  *      assert.
418  *
419  *      LOCKING:
420  *      Inherited from caller.
421  */
422 static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
423 {
424         if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
425                 writelfl(EDMA_EN, base + EDMA_CMD_OFS);
426                 pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
427         }
428         assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
429 }
430
431 /**
432  *      mv_stop_dma - Disable eDMA engine
433  *      @ap: ATA channel to manipulate
434  *
435  *      Verify the local cache of the eDMA state is accurate with an
436  *      assert.
437  *
438  *      LOCKING:
439  *      Inherited from caller.
440  */
441 static void mv_stop_dma(struct ata_port *ap)
442 {
443         void __iomem *port_mmio = mv_ap_base(ap);
444         struct mv_port_priv *pp = ap->private_data;
445         u32 reg;
446         int i;
447
448         if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
449                 /* Disable EDMA if active.   The disable bit auto clears.
450                  */
451                 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
452                 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
453         } else {
454                 assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
455         }
456         
457         /* now properly wait for the eDMA to stop */
458         for (i = 1000; i > 0; i--) {
459                 reg = readl(port_mmio + EDMA_CMD_OFS);
460                 if (!(EDMA_EN & reg)) {
461                         break;
462                 }
463                 udelay(100);
464         }
465
466         if (EDMA_EN & reg) {
467                 printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
468                 /* FIXME: Consider doing a reset here to recover */
469         }
470 }
471
472 #ifdef ATA_DEBUG
473 static void mv_dump_mem(void __iomem *start, unsigned bytes)
474 {
475         int b, w;
476         for (b = 0; b < bytes; ) {
477                 DPRINTK("%p: ", start + b);
478                 for (w = 0; b < bytes && w < 4; w++) {
479                         printk("%08x ",readl(start + b));
480                         b += sizeof(u32);
481                 }
482                 printk("\n");
483         }
484 }
485 #endif
486
487 static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
488 {
489 #ifdef ATA_DEBUG
490         int b, w;
491         u32 dw;
492         for (b = 0; b < bytes; ) {
493                 DPRINTK("%02x: ", b);
494                 for (w = 0; b < bytes && w < 4; w++) {
495                         (void) pci_read_config_dword(pdev,b,&dw);
496                         printk("%08x ",dw);
497                         b += sizeof(u32);
498                 }
499                 printk("\n");
500         }
501 #endif
502 }
503 static void mv_dump_all_regs(void __iomem *mmio_base, int port,
504                              struct pci_dev *pdev)
505 {
506 #ifdef ATA_DEBUG
507         void __iomem *hc_base = mv_hc_base(mmio_base, 
508                                            port >> MV_PORT_HC_SHIFT);
509         void __iomem *port_base;
510         int start_port, num_ports, p, start_hc, num_hcs, hc;
511
512         if (0 > port) {
513                 start_hc = start_port = 0;
514                 num_ports = 8;          /* shld be benign for 4 port devs */
515                 num_hcs = 2;
516         } else {
517                 start_hc = port >> MV_PORT_HC_SHIFT;
518                 start_port = port;
519                 num_ports = num_hcs = 1;
520         }
521         DPRINTK("All registers for port(s) %u-%u:\n", start_port, 
522                 num_ports > 1 ? num_ports - 1 : start_port);
523
524         if (NULL != pdev) {
525                 DPRINTK("PCI config space regs:\n");
526                 mv_dump_pci_cfg(pdev, 0x68);
527         }
528         DPRINTK("PCI regs:\n");
529         mv_dump_mem(mmio_base+0xc00, 0x3c);
530         mv_dump_mem(mmio_base+0xd00, 0x34);
531         mv_dump_mem(mmio_base+0xf00, 0x4);
532         mv_dump_mem(mmio_base+0x1d00, 0x6c);
533         for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
534                 hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
535                 DPRINTK("HC regs (HC %i):\n", hc);
536                 mv_dump_mem(hc_base, 0x1c);
537         }
538         for (p = start_port; p < start_port + num_ports; p++) {
539                 port_base = mv_port_base(mmio_base, p);
540                 DPRINTK("EDMA regs (port %i):\n",p);
541                 mv_dump_mem(port_base, 0x54);
542                 DPRINTK("SATA regs (port %i):\n",p);
543                 mv_dump_mem(port_base+0x300, 0x60);
544         }
545 #endif
546 }
547
548 static unsigned int mv_scr_offset(unsigned int sc_reg_in)
549 {
550         unsigned int ofs;
551
552         switch (sc_reg_in) {
553         case SCR_STATUS:
554         case SCR_CONTROL:
555         case SCR_ERROR:
556                 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
557                 break;
558         case SCR_ACTIVE:
559                 ofs = SATA_ACTIVE_OFS;   /* active is not with the others */
560                 break;
561         default:
562                 ofs = 0xffffffffU;
563                 break;
564         }
565         return ofs;
566 }
567
568 static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
569 {
570         unsigned int ofs = mv_scr_offset(sc_reg_in);
571
572         if (0xffffffffU != ofs) {
573                 return readl(mv_ap_base(ap) + ofs);
574         } else {
575                 return (u32) ofs;
576         }
577 }
578
579 static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
580 {
581         unsigned int ofs = mv_scr_offset(sc_reg_in);
582
583         if (0xffffffffU != ofs) {
584                 writelfl(val, mv_ap_base(ap) + ofs);
585         }
586 }
587
588 /**
589  *      mv_global_soft_reset - Perform the 6xxx global soft reset
590  *      @mmio_base: base address of the HBA
591  *
592  *      This routine only applies to 6xxx parts.
593  *
594  *      LOCKING:
595  *      Inherited from caller.
596  */
597 static int mv_global_soft_reset(void __iomem *mmio_base)
598 {
599         void __iomem *reg = mmio_base + PCI_MAIN_CMD_STS_OFS;
600         int i, rc = 0;
601         u32 t;
602
603         /* Following procedure defined in PCI "main command and status
604          * register" table.
605          */
606         t = readl(reg);
607         writel(t | STOP_PCI_MASTER, reg);
608
609         for (i = 0; i < 1000; i++) {
610                 udelay(1);
611                 t = readl(reg);
612                 if (PCI_MASTER_EMPTY & t) {
613                         break;
614                 }
615         }
616         if (!(PCI_MASTER_EMPTY & t)) {
617                 printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
618                 rc = 1;
619                 goto done;
620         }
621
622         /* set reset */
623         i = 5;
624         do {
625                 writel(t | GLOB_SFT_RST, reg);
626                 t = readl(reg);
627                 udelay(1);
628         } while (!(GLOB_SFT_RST & t) && (i-- > 0));
629
630         if (!(GLOB_SFT_RST & t)) {
631                 printk(KERN_ERR DRV_NAME ": can't set global reset\n");
632                 rc = 1;
633                 goto done;
634         }
635
636         /* clear reset and *reenable the PCI master* (not mentioned in spec) */
637         i = 5;
638         do {
639                 writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
640                 t = readl(reg);
641                 udelay(1);
642         } while ((GLOB_SFT_RST & t) && (i-- > 0));
643
644         if (GLOB_SFT_RST & t) {
645                 printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
646                 rc = 1;
647         }
648 done:
649         return rc;
650 }
651
652 /**
653  *      mv_host_stop - Host specific cleanup/stop routine.
654  *      @host_set: host data structure
655  *
656  *      Disable ints, cleanup host memory, call general purpose
657  *      host_stop.
658  *
659  *      LOCKING:
660  *      Inherited from caller.
661  */
662 static void mv_host_stop(struct ata_host_set *host_set)
663 {
664         struct mv_host_priv *hpriv = host_set->private_data;
665         struct pci_dev *pdev = to_pci_dev(host_set->dev);
666
667         if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
668                 pci_disable_msi(pdev);
669         } else {
670                 pci_intx(pdev, 0);
671         }
672         kfree(hpriv);
673         ata_host_stop(host_set);
674 }
675
676 static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
677 {
678         dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
679 }
680
681 /**
682  *      mv_port_start - Port specific init/start routine.
683  *      @ap: ATA channel to manipulate
684  *
685  *      Allocate and point to DMA memory, init port private memory,
686  *      zero indices.
687  *
688  *      LOCKING:
689  *      Inherited from caller.
690  */
691 static int mv_port_start(struct ata_port *ap)
692 {
693         struct device *dev = ap->host_set->dev;
694         struct mv_port_priv *pp;
695         void __iomem *port_mmio = mv_ap_base(ap);
696         void *mem;
697         dma_addr_t mem_dma;
698         int rc = -ENOMEM;
699
700         pp = kmalloc(sizeof(*pp), GFP_KERNEL);
701         if (!pp)
702                 goto err_out;
703         memset(pp, 0, sizeof(*pp));
704
705         mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma, 
706                                  GFP_KERNEL);
707         if (!mem)
708                 goto err_out_pp;
709         memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
710
711         rc = ata_pad_alloc(ap, dev);
712         if (rc)
713                 goto err_out_priv;
714
715         /* First item in chunk of DMA memory: 
716          * 32-slot command request table (CRQB), 32 bytes each in size
717          */
718         pp->crqb = mem;
719         pp->crqb_dma = mem_dma;
720         mem += MV_CRQB_Q_SZ;
721         mem_dma += MV_CRQB_Q_SZ;
722
723         /* Second item: 
724          * 32-slot command response table (CRPB), 8 bytes each in size
725          */
726         pp->crpb = mem;
727         pp->crpb_dma = mem_dma;
728         mem += MV_CRPB_Q_SZ;
729         mem_dma += MV_CRPB_Q_SZ;
730
731         /* Third item:
732          * Table of scatter-gather descriptors (ePRD), 16 bytes each
733          */
734         pp->sg_tbl = mem;
735         pp->sg_tbl_dma = mem_dma;
736
737         writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT | 
738                  EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
739
740         writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
741         writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK, 
742                  port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
743
744         writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
745         writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
746
747         writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
748         writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK, 
749                  port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
750
751         pp->req_producer = pp->rsp_consumer = 0;
752
753         /* Don't turn on EDMA here...do it before DMA commands only.  Else
754          * we'll be unable to send non-data, PIO, etc due to restricted access
755          * to shadow regs.
756          */
757         ap->private_data = pp;
758         return 0;
759
760 err_out_priv:
761         mv_priv_free(pp, dev);
762 err_out_pp:
763         kfree(pp);
764 err_out:
765         return rc;
766 }
767
768 /**
769  *      mv_port_stop - Port specific cleanup/stop routine.
770  *      @ap: ATA channel to manipulate
771  *
772  *      Stop DMA, cleanup port memory.
773  *
774  *      LOCKING:
775  *      This routine uses the host_set lock to protect the DMA stop.
776  */
777 static void mv_port_stop(struct ata_port *ap)
778 {
779         struct device *dev = ap->host_set->dev;
780         struct mv_port_priv *pp = ap->private_data;
781         unsigned long flags;
782
783         spin_lock_irqsave(&ap->host_set->lock, flags);
784         mv_stop_dma(ap);
785         spin_unlock_irqrestore(&ap->host_set->lock, flags);
786
787         ap->private_data = NULL;
788         ata_pad_free(ap, dev);
789         mv_priv_free(pp, dev);
790         kfree(pp);
791 }
792
793 /**
794  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
795  *      @qc: queued command whose SG list to source from
796  *
797  *      Populate the SG list and mark the last entry.
798  *
799  *      LOCKING:
800  *      Inherited from caller.
801  */
802 static void mv_fill_sg(struct ata_queued_cmd *qc)
803 {
804         struct mv_port_priv *pp = qc->ap->private_data;
805         unsigned int i = 0;
806         struct scatterlist *sg;
807
808         ata_for_each_sg(sg, qc) {
809                 u32 sg_len;
810                 dma_addr_t addr;
811
812                 addr = sg_dma_address(sg);
813                 sg_len = sg_dma_len(sg);
814
815                 pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
816                 pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
817                 assert(0 == (sg_len & ~MV_DMA_BOUNDARY));
818                 pp->sg_tbl[i].flags_size = cpu_to_le32(sg_len);
819                 if (ata_sg_is_last(sg, qc))
820                         pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
821
822                 i++;
823         }
824 }
825
826 static inline unsigned mv_inc_q_index(unsigned *index)
827 {
828         *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
829         return *index;
830 }
831
832 static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
833 {
834         *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
835                 (last ? CRQB_CMD_LAST : 0);
836 }
837
838 /**
839  *      mv_qc_prep - Host specific command preparation.
840  *      @qc: queued command to prepare
841  *
842  *      This routine simply redirects to the general purpose routine
843  *      if command is not DMA.  Else, it handles prep of the CRQB
844  *      (command request block), does some sanity checking, and calls
845  *      the SG load routine.
846  *
847  *      LOCKING:
848  *      Inherited from caller.
849  */
850 static void mv_qc_prep(struct ata_queued_cmd *qc)
851 {
852         struct ata_port *ap = qc->ap;
853         struct mv_port_priv *pp = ap->private_data;
854         u16 *cw;
855         struct ata_taskfile *tf;
856         u16 flags = 0;
857
858         if (ATA_PROT_DMA != qc->tf.protocol) {
859                 return;
860         }
861
862         /* the req producer index should be the same as we remember it */
863         assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >> 
864                  EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
865                pp->req_producer);
866
867         /* Fill in command request block
868          */
869         if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
870                 flags |= CRQB_FLAG_READ;
871         }
872         assert(MV_MAX_Q_DEPTH > qc->tag);
873         flags |= qc->tag << CRQB_TAG_SHIFT;
874
875         pp->crqb[pp->req_producer].sg_addr = 
876                 cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
877         pp->crqb[pp->req_producer].sg_addr_hi = 
878                 cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
879         pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
880
881         cw = &pp->crqb[pp->req_producer].ata_cmd[0];
882         tf = &qc->tf;
883
884         /* Sadly, the CRQB cannot accomodate all registers--there are
885          * only 11 bytes...so we must pick and choose required
886          * registers based on the command.  So, we drop feature and
887          * hob_feature for [RW] DMA commands, but they are needed for
888          * NCQ.  NCQ will drop hob_nsect.
889          */
890         switch (tf->command) {
891         case ATA_CMD_READ:
892         case ATA_CMD_READ_EXT:
893         case ATA_CMD_WRITE:
894         case ATA_CMD_WRITE_EXT:
895                 mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
896                 break;
897 #ifdef LIBATA_NCQ               /* FIXME: remove this line when NCQ added */
898         case ATA_CMD_FPDMA_READ:
899         case ATA_CMD_FPDMA_WRITE:
900                 mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0); 
901                 mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
902                 break;
903 #endif                          /* FIXME: remove this line when NCQ added */
904         default:
905                 /* The only other commands EDMA supports in non-queued and
906                  * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
907                  * of which are defined/used by Linux.  If we get here, this
908                  * driver needs work.
909                  *
910                  * FIXME: modify libata to give qc_prep a return value and
911                  * return error here.
912                  */
913                 BUG_ON(tf->command);
914                 break;
915         }
916         mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
917         mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
918         mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
919         mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
920         mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
921         mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
922         mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
923         mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
924         mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);    /* last */
925
926         if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
927                 return;
928         }
929         mv_fill_sg(qc);
930 }
931
932 /**
933  *      mv_qc_issue - Initiate a command to the host
934  *      @qc: queued command to start
935  *
936  *      This routine simply redirects to the general purpose routine
937  *      if command is not DMA.  Else, it sanity checks our local
938  *      caches of the request producer/consumer indices then enables
939  *      DMA and bumps the request producer index.
940  *
941  *      LOCKING:
942  *      Inherited from caller.
943  */
944 static int mv_qc_issue(struct ata_queued_cmd *qc)
945 {
946         void __iomem *port_mmio = mv_ap_base(qc->ap);
947         struct mv_port_priv *pp = qc->ap->private_data;
948         u32 in_ptr;
949
950         if (ATA_PROT_DMA != qc->tf.protocol) {
951                 /* We're about to send a non-EDMA capable command to the
952                  * port.  Turn off EDMA so there won't be problems accessing
953                  * shadow block, etc registers.
954                  */
955                 mv_stop_dma(qc->ap);
956                 return ata_qc_issue_prot(qc);
957         }
958
959         in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
960
961         /* the req producer index should be the same as we remember it */
962         assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
963                pp->req_producer);
964         /* until we do queuing, the queue should be empty at this point */
965         assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
966                ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >> 
967                  EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
968
969         mv_inc_q_index(&pp->req_producer);      /* now incr producer index */
970
971         mv_start_dma(port_mmio, pp);
972
973         /* and write the request in pointer to kick the EDMA to life */
974         in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
975         in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
976         writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
977
978         return 0;
979 }
980
981 /**
982  *      mv_get_crpb_status - get status from most recently completed cmd
983  *      @ap: ATA channel to manipulate
984  *
985  *      This routine is for use when the port is in DMA mode, when it
986  *      will be using the CRPB (command response block) method of
987  *      returning command completion information.  We assert indices
988  *      are good, grab status, and bump the response consumer index to
989  *      prove that we're up to date.
990  *
991  *      LOCKING:
992  *      Inherited from caller.
993  */
994 static u8 mv_get_crpb_status(struct ata_port *ap)
995 {
996         void __iomem *port_mmio = mv_ap_base(ap);
997         struct mv_port_priv *pp = ap->private_data;
998         u32 out_ptr;
999
1000         out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1001
1002         /* the response consumer index should be the same as we remember it */
1003         assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) == 
1004                pp->rsp_consumer);
1005
1006         /* increment our consumer index... */
1007         pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
1008         
1009         /* and, until we do NCQ, there should only be 1 CRPB waiting */
1010         assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >> 
1011                  EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) == 
1012                pp->rsp_consumer);
1013
1014         /* write out our inc'd consumer index so EDMA knows we're caught up */
1015         out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
1016         out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
1017         writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1018
1019         /* Return ATA status register for completed CRPB */
1020         return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
1021 }
1022
1023 /**
1024  *      mv_err_intr - Handle error interrupts on the port
1025  *      @ap: ATA channel to manipulate
1026  *
1027  *      In most cases, just clear the interrupt and move on.  However,
1028  *      some cases require an eDMA reset, which is done right before
1029  *      the COMRESET in mv_phy_reset().  The SERR case requires a
1030  *      clear of pending errors in the SATA SERROR register.  Finally,
1031  *      if the port disabled DMA, update our cached copy to match.
1032  *
1033  *      LOCKING:
1034  *      Inherited from caller.
1035  */
1036 static void mv_err_intr(struct ata_port *ap)
1037 {
1038         void __iomem *port_mmio = mv_ap_base(ap);
1039         u32 edma_err_cause, serr = 0;
1040
1041         edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1042
1043         if (EDMA_ERR_SERR & edma_err_cause) {
1044                 serr = scr_read(ap, SCR_ERROR);
1045                 scr_write_flush(ap, SCR_ERROR, serr);
1046         }
1047         if (EDMA_ERR_SELF_DIS & edma_err_cause) {
1048                 struct mv_port_priv *pp = ap->private_data;
1049                 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1050         }
1051         DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
1052                 "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
1053
1054         /* Clear EDMA now that SERR cleanup done */
1055         writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1056
1057         /* check for fatal here and recover if needed */
1058         if (EDMA_ERR_FATAL & edma_err_cause) {
1059                 mv_phy_reset(ap);
1060         }
1061 }
1062
1063 /**
1064  *      mv_host_intr - Handle all interrupts on the given host controller
1065  *      @host_set: host specific structure
1066  *      @relevant: port error bits relevant to this host controller
1067  *      @hc: which host controller we're to look at
1068  *
1069  *      Read then write clear the HC interrupt status then walk each
1070  *      port connected to the HC and see if it needs servicing.  Port
1071  *      success ints are reported in the HC interrupt status reg, the
1072  *      port error ints are reported in the higher level main
1073  *      interrupt status register and thus are passed in via the
1074  *      'relevant' argument.
1075  *
1076  *      LOCKING:
1077  *      Inherited from caller.
1078  */
1079 static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
1080                          unsigned int hc)
1081 {
1082         void __iomem *mmio = host_set->mmio_base;
1083         void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1084         struct ata_port *ap;
1085         struct ata_queued_cmd *qc;
1086         u32 hc_irq_cause;
1087         int shift, port, port0, hard_port, handled;
1088         unsigned int err_mask;
1089         u8 ata_status = 0;
1090
1091         if (hc == 0) {
1092                 port0 = 0;
1093         } else {
1094                 port0 = MV_PORTS_PER_HC;
1095         }
1096
1097         /* we'll need the HC success int register in most cases */
1098         hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
1099         if (hc_irq_cause) {
1100                 writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1101         }
1102
1103         VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
1104                 hc,relevant,hc_irq_cause);
1105
1106         for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
1107                 ap = host_set->ports[port];
1108                 hard_port = port & MV_PORT_MASK;        /* range 0-3 */
1109                 handled = 0;    /* ensure ata_status is set if handled++ */
1110
1111                 if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
1112                         /* new CRPB on the queue; just one at a time until NCQ
1113                          */
1114                         ata_status = mv_get_crpb_status(ap);
1115                         handled++;
1116                 } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
1117                         /* received ATA IRQ; read the status reg to clear INTRQ
1118                          */
1119                         ata_status = readb((void __iomem *)
1120                                            ap->ioaddr.status_addr);
1121                         handled++;
1122                 }
1123
1124                 err_mask = ac_err_mask(ata_status);
1125
1126                 shift = port << 1;              /* (port * 2) */
1127                 if (port >= MV_PORTS_PER_HC) {
1128                         shift++;        /* skip bit 8 in the HC Main IRQ reg */
1129                 }
1130                 if ((PORT0_ERR << shift) & relevant) {
1131                         mv_err_intr(ap);
1132                         err_mask |= AC_ERR_OTHER;
1133                         handled++;
1134                 }
1135                 
1136                 if (handled && ap) {
1137                         qc = ata_qc_from_tag(ap, ap->active_tag);
1138                         if (NULL != qc) {
1139                                 VPRINTK("port %u IRQ found for qc, "
1140                                         "ata_status 0x%x\n", port,ata_status);
1141                                 /* mark qc status appropriately */
1142                                 ata_qc_complete(qc, err_mask);
1143                         }
1144                 }
1145         }
1146         VPRINTK("EXIT\n");
1147 }
1148
1149 /**
1150  *      mv_interrupt - 
1151  *      @irq: unused
1152  *      @dev_instance: private data; in this case the host structure
1153  *      @regs: unused
1154  *
1155  *      Read the read only register to determine if any host
1156  *      controllers have pending interrupts.  If so, call lower level
1157  *      routine to handle.  Also check for PCI errors which are only
1158  *      reported here.
1159  *
1160  *      LOCKING: 
1161  *      This routine holds the host_set lock while processing pending
1162  *      interrupts.
1163  */
1164 static irqreturn_t mv_interrupt(int irq, void *dev_instance,
1165                                 struct pt_regs *regs)
1166 {
1167         struct ata_host_set *host_set = dev_instance;
1168         unsigned int hc, handled = 0, n_hcs;
1169         void __iomem *mmio = host_set->mmio_base;
1170         u32 irq_stat;
1171
1172         irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
1173
1174         /* check the cases where we either have nothing pending or have read
1175          * a bogus register value which can indicate HW removal or PCI fault
1176          */
1177         if (!irq_stat || (0xffffffffU == irq_stat)) {
1178                 return IRQ_NONE;
1179         }
1180
1181         n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
1182         spin_lock(&host_set->lock);
1183
1184         for (hc = 0; hc < n_hcs; hc++) {
1185                 u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
1186                 if (relevant) {
1187                         mv_host_intr(host_set, relevant, hc);
1188                         handled++;
1189                 }
1190         }
1191         if (PCI_ERR & irq_stat) {
1192                 printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
1193                        readl(mmio + PCI_IRQ_CAUSE_OFS));
1194
1195                 DPRINTK("All regs @ PCI error\n");
1196                 mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
1197
1198                 writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
1199                 handled++;
1200         }
1201         spin_unlock(&host_set->lock);
1202
1203         return IRQ_RETVAL(handled);
1204 }
1205
1206 /**
1207  *      mv_phy_reset - Perform eDMA reset followed by COMRESET
1208  *      @ap: ATA channel to manipulate
1209  *
1210  *      Part of this is taken from __sata_phy_reset and modified to
1211  *      not sleep since this routine gets called from interrupt level.
1212  *
1213  *      LOCKING:
1214  *      Inherited from caller.  This is coded to safe to call at
1215  *      interrupt level, i.e. it does not sleep.
1216  */
1217 static void mv_phy_reset(struct ata_port *ap)
1218 {
1219         void __iomem *port_mmio = mv_ap_base(ap);
1220         struct ata_taskfile tf;
1221         struct ata_device *dev = &ap->device[0];
1222         unsigned long timeout;
1223
1224         VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
1225
1226         mv_stop_dma(ap);
1227
1228         writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
1229         udelay(25);             /* allow reset propagation */
1230
1231         /* Spec never mentions clearing the bit.  Marvell's driver does
1232          * clear the bit, however.
1233          */
1234         writelfl(0, port_mmio + EDMA_CMD_OFS);
1235
1236         VPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
1237                 "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
1238                 mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
1239
1240         /* proceed to init communications via the scr_control reg */
1241         scr_write_flush(ap, SCR_CONTROL, 0x301);
1242         mdelay(1);
1243         scr_write_flush(ap, SCR_CONTROL, 0x300);
1244         timeout = jiffies + (HZ * 1);
1245         do {
1246                 mdelay(10);
1247                 if ((scr_read(ap, SCR_STATUS) & 0xf) != 1)
1248                         break;
1249         } while (time_before(jiffies, timeout));
1250
1251         VPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
1252                 "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
1253                 mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
1254
1255         if (sata_dev_present(ap)) {
1256                 ata_port_probe(ap);
1257         } else {
1258                 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1259                        ap->id, scr_read(ap, SCR_STATUS));
1260                 ata_port_disable(ap);
1261                 return;
1262         }
1263         ap->cbl = ATA_CBL_SATA;
1264
1265         tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
1266         tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
1267         tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
1268         tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
1269
1270         dev->class = ata_dev_classify(&tf);
1271         if (!ata_dev_present(dev)) {
1272                 VPRINTK("Port disabled post-sig: No device present.\n");
1273                 ata_port_disable(ap);
1274         }
1275         VPRINTK("EXIT\n");
1276 }
1277
1278 /**
1279  *      mv_eng_timeout - Routine called by libata when SCSI times out I/O
1280  *      @ap: ATA channel to manipulate
1281  *
1282  *      Intent is to clear all pending error conditions, reset the
1283  *      chip/bus, fail the command, and move on.
1284  *
1285  *      LOCKING:
1286  *      This routine holds the host_set lock while failing the command.
1287  */
1288 static void mv_eng_timeout(struct ata_port *ap)
1289 {
1290         struct ata_queued_cmd *qc;
1291         unsigned long flags;
1292
1293         printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
1294         DPRINTK("All regs @ start of eng_timeout\n");
1295         mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no, 
1296                          to_pci_dev(ap->host_set->dev));
1297
1298         qc = ata_qc_from_tag(ap, ap->active_tag);
1299         printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
1300                ap->host_set->mmio_base, ap, qc, qc->scsicmd, 
1301                &qc->scsicmd->cmnd);
1302
1303         mv_err_intr(ap);
1304         mv_phy_reset(ap);
1305
1306         if (!qc) {
1307                 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
1308                        ap->id);
1309         } else {
1310                 /* hack alert!  We cannot use the supplied completion
1311                  * function from inside the ->eh_strategy_handler() thread.
1312                  * libata is the only user of ->eh_strategy_handler() in
1313                  * any kernel, so the default scsi_done() assumes it is
1314                  * not being called from the SCSI EH.
1315                  */
1316                 spin_lock_irqsave(&ap->host_set->lock, flags);
1317                 qc->scsidone = scsi_finish_command;
1318                 ata_qc_complete(qc, AC_ERR_OTHER);
1319                 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1320         }
1321 }
1322
1323 /**
1324  *      mv_port_init - Perform some early initialization on a single port.
1325  *      @port: libata data structure storing shadow register addresses
1326  *      @port_mmio: base address of the port
1327  *
1328  *      Initialize shadow register mmio addresses, clear outstanding
1329  *      interrupts on the port, and unmask interrupts for the future
1330  *      start of the port.
1331  *
1332  *      LOCKING:
1333  *      Inherited from caller.
1334  */
1335 static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
1336 {
1337         unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
1338         unsigned serr_ofs;
1339
1340         /* PIO related setup 
1341          */
1342         port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
1343         port->error_addr = 
1344                 port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
1345         port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
1346         port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
1347         port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
1348         port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
1349         port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
1350         port->status_addr = 
1351                 port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
1352         /* special case: control/altstatus doesn't have ATA_REG_ address */
1353         port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
1354
1355         /* unused: */
1356         port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
1357
1358         /* Clear any currently outstanding port interrupt conditions */
1359         serr_ofs = mv_scr_offset(SCR_ERROR);
1360         writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
1361         writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1362
1363         /* unmask all EDMA error interrupts */
1364         writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
1365
1366         VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n", 
1367                 readl(port_mmio + EDMA_CFG_OFS),
1368                 readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
1369                 readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
1370 }
1371
1372 /**
1373  *      mv_host_init - Perform some early initialization of the host.
1374  *      @probe_ent: early data struct representing the host
1375  *
1376  *      If possible, do an early global reset of the host.  Then do
1377  *      our port init and clear/unmask all/relevant host interrupts.
1378  *
1379  *      LOCKING:
1380  *      Inherited from caller.
1381  */
1382 static int mv_host_init(struct ata_probe_ent *probe_ent)
1383 {
1384         int rc = 0, n_hc, port, hc;
1385         void __iomem *mmio = probe_ent->mmio_base;
1386         void __iomem *port_mmio;
1387
1388         if ((MV_FLAG_GLBL_SFT_RST & probe_ent->host_flags) && 
1389             mv_global_soft_reset(probe_ent->mmio_base)) {
1390                 rc = 1;
1391                 goto done;
1392         }
1393
1394         n_hc = mv_get_hc_count(probe_ent->host_flags);
1395         probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
1396
1397         for (port = 0; port < probe_ent->n_ports; port++) {
1398                 port_mmio = mv_port_base(mmio, port);
1399                 mv_port_init(&probe_ent->port[port], port_mmio);
1400         }
1401
1402         for (hc = 0; hc < n_hc; hc++) {
1403                 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1404
1405                 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
1406                         "(before clear)=0x%08x\n", hc,
1407                         readl(hc_mmio + HC_CFG_OFS),
1408                         readl(hc_mmio + HC_IRQ_CAUSE_OFS));
1409
1410                 /* Clear any currently outstanding hc interrupt conditions */
1411                 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
1412         }
1413
1414         /* Clear any currently outstanding host interrupt conditions */
1415         writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
1416
1417         /* and unmask interrupt generation for host regs */
1418         writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
1419         writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
1420
1421         VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
1422                 "PCI int cause/mask=0x%08x/0x%08x\n", 
1423                 readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
1424                 readl(mmio + HC_MAIN_IRQ_MASK_OFS),
1425                 readl(mmio + PCI_IRQ_CAUSE_OFS),
1426                 readl(mmio + PCI_IRQ_MASK_OFS));
1427 done:
1428         return rc;
1429 }
1430
1431 /**
1432  *      mv_print_info - Dump key info to kernel log for perusal.
1433  *      @probe_ent: early data struct representing the host
1434  *
1435  *      FIXME: complete this.
1436  *
1437  *      LOCKING:
1438  *      Inherited from caller.
1439  */
1440 static void mv_print_info(struct ata_probe_ent *probe_ent)
1441 {
1442         struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1443         struct mv_host_priv *hpriv = probe_ent->private_data;
1444         u8 rev_id, scc;
1445         const char *scc_s;
1446
1447         /* Use this to determine the HW stepping of the chip so we know
1448          * what errata to workaround
1449          */
1450         pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
1451
1452         pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
1453         if (scc == 0)
1454                 scc_s = "SCSI";
1455         else if (scc == 0x01)
1456                 scc_s = "RAID";
1457         else
1458                 scc_s = "unknown";
1459
1460         dev_printk(KERN_INFO, &pdev->dev,
1461                "%u slots %u ports %s mode IRQ via %s\n",
1462                (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports, 
1463                scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
1464 }
1465
1466 /**
1467  *      mv_init_one - handle a positive probe of a Marvell host
1468  *      @pdev: PCI device found
1469  *      @ent: PCI device ID entry for the matched host
1470  *
1471  *      LOCKING:
1472  *      Inherited from caller.
1473  */
1474 static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1475 {
1476         static int printed_version = 0;
1477         struct ata_probe_ent *probe_ent = NULL;
1478         struct mv_host_priv *hpriv;
1479         unsigned int board_idx = (unsigned int)ent->driver_data;
1480         void __iomem *mmio_base;
1481         int pci_dev_busy = 0, rc;
1482
1483         if (!printed_version++)
1484                 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
1485
1486         rc = pci_enable_device(pdev);
1487         if (rc) {
1488                 return rc;
1489         }
1490
1491         rc = pci_request_regions(pdev, DRV_NAME);
1492         if (rc) {
1493                 pci_dev_busy = 1;
1494                 goto err_out;
1495         }
1496
1497         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
1498         if (probe_ent == NULL) {
1499                 rc = -ENOMEM;
1500                 goto err_out_regions;
1501         }
1502
1503         memset(probe_ent, 0, sizeof(*probe_ent));
1504         probe_ent->dev = pci_dev_to_dev(pdev);
1505         INIT_LIST_HEAD(&probe_ent->node);
1506
1507         mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
1508         if (mmio_base == NULL) {
1509                 rc = -ENOMEM;
1510                 goto err_out_free_ent;
1511         }
1512
1513         hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
1514         if (!hpriv) {
1515                 rc = -ENOMEM;
1516                 goto err_out_iounmap;
1517         }
1518         memset(hpriv, 0, sizeof(*hpriv));
1519
1520         probe_ent->sht = mv_port_info[board_idx].sht;
1521         probe_ent->host_flags = mv_port_info[board_idx].host_flags;
1522         probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
1523         probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
1524         probe_ent->port_ops = mv_port_info[board_idx].port_ops;
1525
1526         probe_ent->irq = pdev->irq;
1527         probe_ent->irq_flags = SA_SHIRQ;
1528         probe_ent->mmio_base = mmio_base;
1529         probe_ent->private_data = hpriv;
1530
1531         /* initialize adapter */
1532         rc = mv_host_init(probe_ent);
1533         if (rc) {
1534                 goto err_out_hpriv;
1535         }
1536
1537         /* Enable interrupts */
1538         if (pci_enable_msi(pdev) == 0) {
1539                 hpriv->hp_flags |= MV_HP_FLAG_MSI;
1540         } else {
1541                 pci_intx(pdev, 1);
1542         }
1543
1544         mv_dump_pci_cfg(pdev, 0x68);
1545         mv_print_info(probe_ent);
1546
1547         if (ata_device_add(probe_ent) == 0) {
1548                 rc = -ENODEV;           /* No devices discovered */
1549                 goto err_out_dev_add;
1550         }
1551
1552         kfree(probe_ent);
1553         return 0;
1554
1555 err_out_dev_add:
1556         if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
1557                 pci_disable_msi(pdev);
1558         } else {
1559                 pci_intx(pdev, 0);
1560         }
1561 err_out_hpriv:
1562         kfree(hpriv);
1563 err_out_iounmap:
1564         pci_iounmap(pdev, mmio_base);
1565 err_out_free_ent:
1566         kfree(probe_ent);
1567 err_out_regions:
1568         pci_release_regions(pdev);
1569 err_out:
1570         if (!pci_dev_busy) {
1571                 pci_disable_device(pdev);
1572         }
1573
1574         return rc;
1575 }
1576
1577 static int __init mv_init(void)
1578 {
1579         return pci_module_init(&mv_pci_driver);
1580 }
1581
1582 static void __exit mv_exit(void)
1583 {
1584         pci_unregister_driver(&mv_pci_driver);
1585 }
1586
1587 MODULE_AUTHOR("Brett Russ");
1588 MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
1589 MODULE_LICENSE("GPL");
1590 MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
1591 MODULE_VERSION(DRV_VERSION);
1592
1593 module_init(mv_init);
1594 module_exit(mv_exit);