a5ea9f6df8ebf089a37fd109e1c9359a010eb0df
[sfrench/cifs-2.6.git] / drivers / scsi / sata_sil24.c
1 /*
2  * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers
3  *
4  * Copyright 2005  Tejun Heo
5  *
6  * Based on preview driver from Silicon Image.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/blkdev.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <linux/libata.h>
31 #include <asm/io.h>
32
33 #define DRV_NAME        "sata_sil24"
34 #define DRV_VERSION     "0.24"
35
36 /*
37  * Port request block (PRB) 32 bytes
38  */
39 struct sil24_prb {
40         u16     ctrl;
41         u16     prot;
42         u32     rx_cnt;
43         u8      fis[6 * 4];
44 };
45
46 /*
47  * Scatter gather entry (SGE) 16 bytes
48  */
49 struct sil24_sge {
50         u64     addr;
51         u32     cnt;
52         u32     flags;
53 };
54
55 /*
56  * Port multiplier
57  */
58 struct sil24_port_multiplier {
59         u32     diag;
60         u32     sactive;
61 };
62
63 enum {
64         /*
65          * Global controller registers (128 bytes @ BAR0)
66          */
67                 /* 32 bit regs */
68         HOST_SLOT_STAT          = 0x00, /* 32 bit slot stat * 4 */
69         HOST_CTRL               = 0x40,
70         HOST_IRQ_STAT           = 0x44,
71         HOST_PHY_CFG            = 0x48,
72         HOST_BIST_CTRL          = 0x50,
73         HOST_BIST_PTRN          = 0x54,
74         HOST_BIST_STAT          = 0x58,
75         HOST_MEM_BIST_STAT      = 0x5c,
76         HOST_FLASH_CMD          = 0x70,
77                 /* 8 bit regs */
78         HOST_FLASH_DATA         = 0x74,
79         HOST_TRANSITION_DETECT  = 0x75,
80         HOST_GPIO_CTRL          = 0x76,
81         HOST_I2C_ADDR           = 0x78, /* 32 bit */
82         HOST_I2C_DATA           = 0x7c,
83         HOST_I2C_XFER_CNT       = 0x7e,
84         HOST_I2C_CTRL           = 0x7f,
85
86         /* HOST_SLOT_STAT bits */
87         HOST_SSTAT_ATTN         = (1 << 31),
88
89         /*
90          * Port registers
91          * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
92          */
93         PORT_REGS_SIZE          = 0x2000,
94         PORT_PRB                = 0x0000, /* (32 bytes PRB + 16 bytes SGEs * 6) * 31 (3968 bytes) */
95
96         PORT_PM                 = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */
97                 /* 32 bit regs */
98         PORT_CTRL_STAT          = 0x1000, /* write: ctrl-set, read: stat */
99         PORT_CTRL_CLR           = 0x1004, /* write: ctrl-clear */
100         PORT_IRQ_STAT           = 0x1008, /* high: status, low: interrupt */
101         PORT_IRQ_ENABLE_SET     = 0x1010, /* write: enable-set */
102         PORT_IRQ_ENABLE_CLR     = 0x1014, /* write: enable-clear */
103         PORT_ACTIVATE_UPPER_ADDR= 0x101c,
104         PORT_EXEC_FIFO          = 0x1020, /* command execution fifo */
105         PORT_CMD_ERR            = 0x1024, /* command error number */
106         PORT_FIS_CFG            = 0x1028,
107         PORT_FIFO_THRES         = 0x102c,
108                 /* 16 bit regs */
109         PORT_DECODE_ERR_CNT     = 0x1040,
110         PORT_DECODE_ERR_THRESH  = 0x1042,
111         PORT_CRC_ERR_CNT        = 0x1044,
112         PORT_CRC_ERR_THRESH     = 0x1046,
113         PORT_HSHK_ERR_CNT       = 0x1048,
114         PORT_HSHK_ERR_THRESH    = 0x104a,
115                 /* 32 bit regs */
116         PORT_PHY_CFG            = 0x1050,
117         PORT_SLOT_STAT          = 0x1800,
118         PORT_CMD_ACTIVATE       = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
119         PORT_EXEC_DIAG          = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
120         PORT_PSD_DIAG           = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
121         PORT_SCONTROL           = 0x1f00,
122         PORT_SSTATUS            = 0x1f04,
123         PORT_SERROR             = 0x1f08,
124         PORT_SACTIVE            = 0x1f0c,
125
126         /* PORT_CTRL_STAT bits */
127         PORT_CS_PORT_RST        = (1 << 0), /* port reset */
128         PORT_CS_DEV_RST         = (1 << 1), /* device reset */
129         PORT_CS_INIT            = (1 << 2), /* port initialize */
130         PORT_CS_IRQ_WOC         = (1 << 3), /* interrupt write one to clear */
131         PORT_CS_CDB16           = (1 << 5), /* 0=12b cdb, 1=16b cdb */
132         PORT_CS_RESUME          = (1 << 6), /* port resume */
133         PORT_CS_32BIT_ACTV      = (1 << 10), /* 32-bit activation */
134         PORT_CS_PM_EN           = (1 << 13), /* port multiplier enable */
135         PORT_CS_RDY             = (1 << 31), /* port ready to accept commands */
136
137         /* PORT_IRQ_STAT/ENABLE_SET/CLR */
138         /* bits[11:0] are masked */
139         PORT_IRQ_COMPLETE       = (1 << 0), /* command(s) completed */
140         PORT_IRQ_ERROR          = (1 << 1), /* command execution error */
141         PORT_IRQ_PORTRDY_CHG    = (1 << 2), /* port ready change */
142         PORT_IRQ_PWR_CHG        = (1 << 3), /* power management change */
143         PORT_IRQ_PHYRDY_CHG     = (1 << 4), /* PHY ready change */
144         PORT_IRQ_COMWAKE        = (1 << 5), /* COMWAKE received */
145         PORT_IRQ_UNK_FIS        = (1 << 6), /* Unknown FIS received */
146         PORT_IRQ_SDB_NOTIFY     = (1 << 11), /* SDB notify received */
147
148         /* bits[27:16] are unmasked (raw) */
149         PORT_IRQ_RAW_SHIFT      = 16,
150         PORT_IRQ_MASKED_MASK    = 0x7ff,
151         PORT_IRQ_RAW_MASK       = (0x7ff << PORT_IRQ_RAW_SHIFT),
152
153         /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
154         PORT_IRQ_STEER_SHIFT    = 30,
155         PORT_IRQ_STEER_MASK     = (3 << PORT_IRQ_STEER_SHIFT),
156
157         /* PORT_CMD_ERR constants */
158         PORT_CERR_DEV           = 1, /* Error bit in D2H Register FIS */
159         PORT_CERR_SDB           = 2, /* Error bit in SDB FIS */
160         PORT_CERR_DATA          = 3, /* Error in data FIS not detected by dev */
161         PORT_CERR_SEND          = 4, /* Initial cmd FIS transmission failure */
162         PORT_CERR_INCONSISTENT  = 5, /* Protocol mismatch */
163         PORT_CERR_DIRECTION     = 6, /* Data direction mismatch */
164         PORT_CERR_UNDERRUN      = 7, /* Ran out of SGEs while writing */
165         PORT_CERR_OVERRUN       = 8, /* Ran out of SGEs while reading */
166         PORT_CERR_PKT_PROT      = 11, /* DIR invalid in 1st PIO setup of ATAPI */
167         PORT_CERR_SGT_BOUNDARY  = 16, /* PLD ecode 00 - SGT not on qword boundary */
168         PORT_CERR_SGT_TGTABRT   = 17, /* PLD ecode 01 - target abort */
169         PORT_CERR_SGT_MSTABRT   = 18, /* PLD ecode 10 - master abort */
170         PORT_CERR_SGT_PCIPERR   = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
171         PORT_CERR_CMD_BOUNDARY  = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
172         PORT_CERR_CMD_TGTABRT   = 25, /* ctrl[15:13] 010 - target abort */
173         PORT_CERR_CMD_MSTABRT   = 26, /* ctrl[15:13] 100 - master abort */
174         PORT_CERR_CMD_PCIPERR   = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
175         PORT_CERR_XFR_UNDEF     = 32, /* PSD ecode 00 - undefined */
176         PORT_CERR_XFR_TGTABRT   = 33, /* PSD ecode 01 - target abort */
177         PORT_CERR_XFR_MSTABRT   = 34, /* PSD ecode 10 - master abort */
178         PORT_CERR_XFR_PCIPERR   = 35, /* PSD ecode 11 - PCI prity err during transfer */
179         PORT_CERR_SENDSERVICE   = 36, /* FIS received while sending service */
180
181         /* bits of PRB control field */
182         PRB_CTRL_PROTOCOL       = (1 << 0), /* override def. ATA protocol */
183         PRB_CTRL_PACKET_READ    = (1 << 4), /* PACKET cmd read */
184         PRB_CTRL_PACKET_WRITE   = (1 << 5), /* PACKET cmd write */
185         PRB_CTRL_NIEN           = (1 << 6), /* Mask completion irq */
186         PRB_CTRL_SRST           = (1 << 7), /* Soft reset request (ign BSY?) */
187
188         /* PRB protocol field */
189         PRB_PROT_PACKET         = (1 << 0),
190         PRB_PROT_TCQ            = (1 << 1),
191         PRB_PROT_NCQ            = (1 << 2),
192         PRB_PROT_READ           = (1 << 3),
193         PRB_PROT_WRITE          = (1 << 4),
194         PRB_PROT_TRANSPARENT    = (1 << 5),
195
196         /*
197          * Other constants
198          */
199         SGE_TRM                 = (1 << 31), /* Last SGE in chain */
200         SGE_LNK                 = (1 << 30), /* linked list
201                                                 Points to SGT, not SGE */
202         SGE_DRD                 = (1 << 29), /* discard data read (/dev/null)
203                                                 data address ignored */
204
205         /* board id */
206         BID_SIL3124             = 0,
207         BID_SIL3132             = 1,
208         BID_SIL3131             = 2,
209
210         IRQ_STAT_4PORTS         = 0xf,
211 };
212
213 struct sil24_ata_block {
214         struct sil24_prb prb;
215         struct sil24_sge sge[LIBATA_MAX_PRD];
216 };
217
218 struct sil24_atapi_block {
219         struct sil24_prb prb;
220         u8 cdb[16];
221         struct sil24_sge sge[LIBATA_MAX_PRD - 1];
222 };
223
224 union sil24_cmd_block {
225         struct sil24_ata_block ata;
226         struct sil24_atapi_block atapi;
227 };
228
229 /*
230  * ap->private_data
231  *
232  * The preview driver always returned 0 for status.  We emulate it
233  * here from the previous interrupt.
234  */
235 struct sil24_port_priv {
236         union sil24_cmd_block *cmd_block;       /* 32 cmd blocks */
237         dma_addr_t cmd_block_dma;               /* DMA base addr for them */
238         struct ata_taskfile tf;                 /* Cached taskfile registers */
239 };
240
241 /* ap->host_set->private_data */
242 struct sil24_host_priv {
243         void __iomem *host_base;        /* global controller control (128 bytes @BAR0) */
244         void __iomem *port_base;        /* port registers (4 * 8192 bytes @BAR2) */
245 };
246
247 static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev);
248 static u8 sil24_check_status(struct ata_port *ap);
249 static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg);
250 static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
251 static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
252 static int sil24_probe_reset(struct ata_port *ap, unsigned int *classes);
253 static void sil24_qc_prep(struct ata_queued_cmd *qc);
254 static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc);
255 static void sil24_irq_clear(struct ata_port *ap);
256 static void sil24_eng_timeout(struct ata_port *ap);
257 static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
258 static int sil24_port_start(struct ata_port *ap);
259 static void sil24_port_stop(struct ata_port *ap);
260 static void sil24_host_stop(struct ata_host_set *host_set);
261 static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
262
263 static const struct pci_device_id sil24_pci_tbl[] = {
264         { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
265         { 0x8086, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
266         { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 },
267         { 0x1095, 0x3131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
268         { 0x1095, 0x3531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
269         { } /* terminate list */
270 };
271
272 static struct pci_driver sil24_pci_driver = {
273         .name                   = DRV_NAME,
274         .id_table               = sil24_pci_tbl,
275         .probe                  = sil24_init_one,
276         .remove                 = ata_pci_remove_one, /* safe? */
277 };
278
279 static struct scsi_host_template sil24_sht = {
280         .module                 = THIS_MODULE,
281         .name                   = DRV_NAME,
282         .ioctl                  = ata_scsi_ioctl,
283         .queuecommand           = ata_scsi_queuecmd,
284         .can_queue              = ATA_DEF_QUEUE,
285         .this_id                = ATA_SHT_THIS_ID,
286         .sg_tablesize           = LIBATA_MAX_PRD,
287         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
288         .emulated               = ATA_SHT_EMULATED,
289         .use_clustering         = ATA_SHT_USE_CLUSTERING,
290         .proc_name              = DRV_NAME,
291         .dma_boundary           = ATA_DMA_BOUNDARY,
292         .slave_configure        = ata_scsi_slave_config,
293         .bios_param             = ata_std_bios_param,
294 };
295
296 static const struct ata_port_operations sil24_ops = {
297         .port_disable           = ata_port_disable,
298
299         .dev_config             = sil24_dev_config,
300
301         .check_status           = sil24_check_status,
302         .check_altstatus        = sil24_check_status,
303         .dev_select             = ata_noop_dev_select,
304
305         .tf_read                = sil24_tf_read,
306
307         .probe_reset            = sil24_probe_reset,
308
309         .qc_prep                = sil24_qc_prep,
310         .qc_issue               = sil24_qc_issue,
311
312         .eng_timeout            = sil24_eng_timeout,
313
314         .irq_handler            = sil24_interrupt,
315         .irq_clear              = sil24_irq_clear,
316
317         .scr_read               = sil24_scr_read,
318         .scr_write              = sil24_scr_write,
319
320         .port_start             = sil24_port_start,
321         .port_stop              = sil24_port_stop,
322         .host_stop              = sil24_host_stop,
323 };
324
325 /*
326  * Use bits 30-31 of host_flags to encode available port numbers.
327  * Current maxium is 4.
328  */
329 #define SIL24_NPORTS2FLAG(nports)       ((((unsigned)(nports) - 1) & 0x3) << 30)
330 #define SIL24_FLAG2NPORTS(flag)         ((((flag) >> 30) & 0x3) + 1)
331
332 static struct ata_port_info sil24_port_info[] = {
333         /* sil_3124 */
334         {
335                 .sht            = &sil24_sht,
336                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
337                                   ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
338                                   SIL24_NPORTS2FLAG(4),
339                 .pio_mask       = 0x1f,                 /* pio0-4 */
340                 .mwdma_mask     = 0x07,                 /* mwdma0-2 */
341                 .udma_mask      = 0x3f,                 /* udma0-5 */
342                 .port_ops       = &sil24_ops,
343         },
344         /* sil_3132 */
345         {
346                 .sht            = &sil24_sht,
347                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
348                                   ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
349                                   SIL24_NPORTS2FLAG(2),
350                 .pio_mask       = 0x1f,                 /* pio0-4 */
351                 .mwdma_mask     = 0x07,                 /* mwdma0-2 */
352                 .udma_mask      = 0x3f,                 /* udma0-5 */
353                 .port_ops       = &sil24_ops,
354         },
355         /* sil_3131/sil_3531 */
356         {
357                 .sht            = &sil24_sht,
358                 .host_flags     = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
359                                   ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
360                                   SIL24_NPORTS2FLAG(1),
361                 .pio_mask       = 0x1f,                 /* pio0-4 */
362                 .mwdma_mask     = 0x07,                 /* mwdma0-2 */
363                 .udma_mask      = 0x3f,                 /* udma0-5 */
364                 .port_ops       = &sil24_ops,
365         },
366 };
367
368 static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev)
369 {
370         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
371
372         if (dev->cdb_len == 16)
373                 writel(PORT_CS_CDB16, port + PORT_CTRL_STAT);
374         else
375                 writel(PORT_CS_CDB16, port + PORT_CTRL_CLR);
376 }
377
378 static inline void sil24_update_tf(struct ata_port *ap)
379 {
380         struct sil24_port_priv *pp = ap->private_data;
381         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
382         struct sil24_prb __iomem *prb = port;
383         u8 fis[6 * 4];
384
385         memcpy_fromio(fis, prb->fis, 6 * 4);
386         ata_tf_from_fis(fis, &pp->tf);
387 }
388
389 static u8 sil24_check_status(struct ata_port *ap)
390 {
391         struct sil24_port_priv *pp = ap->private_data;
392         return pp->tf.command;
393 }
394
395 static int sil24_scr_map[] = {
396         [SCR_CONTROL]   = 0,
397         [SCR_STATUS]    = 1,
398         [SCR_ERROR]     = 2,
399         [SCR_ACTIVE]    = 3,
400 };
401
402 static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg)
403 {
404         void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
405         if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
406                 void __iomem *addr;
407                 addr = scr_addr + sil24_scr_map[sc_reg] * 4;
408                 return readl(scr_addr + sil24_scr_map[sc_reg] * 4);
409         }
410         return 0xffffffffU;
411 }
412
413 static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
414 {
415         void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
416         if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
417                 void __iomem *addr;
418                 addr = scr_addr + sil24_scr_map[sc_reg] * 4;
419                 writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
420         }
421 }
422
423 static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
424 {
425         struct sil24_port_priv *pp = ap->private_data;
426         *tf = pp->tf;
427 }
428
429 static int sil24_softreset(struct ata_port *ap, unsigned int *class)
430 {
431         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
432         struct sil24_port_priv *pp = ap->private_data;
433         struct sil24_prb *prb = &pp->cmd_block[0].ata.prb;
434         dma_addr_t paddr = pp->cmd_block_dma;
435         u32 mask, irq_enable, irq_stat;
436         const char *reason;
437
438         DPRINTK("ENTER\n");
439
440         if (!sata_dev_present(ap)) {
441                 DPRINTK("PHY reports no device\n");
442                 *class = ATA_DEV_NONE;
443                 goto out;
444         }
445
446         /* temporarily turn off IRQs during SRST */
447         irq_enable = readl(port + PORT_IRQ_ENABLE_SET);
448         writel(irq_enable, port + PORT_IRQ_ENABLE_CLR);
449
450         /*
451          * XXX: Not sure whether the following sleep is needed or not.
452          * The original driver had it.  So....
453          */
454         msleep(10);
455
456         prb->ctrl = PRB_CTRL_SRST;
457         prb->fis[1] = 0; /* no PM yet */
458
459         writel((u32)paddr, port + PORT_CMD_ACTIVATE);
460
461         mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
462         irq_stat = ata_wait_register(port + PORT_IRQ_STAT, mask, 0x0,
463                                      100, ATA_TMOUT_BOOT / HZ * 1000);
464
465         writel(irq_stat, port + PORT_IRQ_STAT); /* clear IRQs */
466         irq_stat >>= PORT_IRQ_RAW_SHIFT;
467
468         /* restore IRQs */
469         writel(irq_enable, port + PORT_IRQ_ENABLE_SET);
470
471         if (!(irq_stat & PORT_IRQ_COMPLETE)) {
472                 if (irq_stat & PORT_IRQ_ERROR)
473                         reason = "SRST command error";
474                 else
475                         reason = "timeout";
476                 goto err;
477         }
478
479         sil24_update_tf(ap);
480         *class = ata_dev_classify(&pp->tf);
481
482         if (*class == ATA_DEV_UNKNOWN)
483                 *class = ATA_DEV_NONE;
484
485  out:
486         DPRINTK("EXIT, class=%u\n", *class);
487         return 0;
488
489  err:
490         printk(KERN_ERR "ata%u: softreset failed (%s)\n", ap->id, reason);
491         return -EIO;
492 }
493
494 static int sil24_hardreset(struct ata_port *ap, unsigned int *class)
495 {
496         unsigned int dummy_class;
497
498         /* sil24 doesn't report device signature after hard reset */
499         return sata_std_hardreset(ap, &dummy_class);
500 }
501
502 static int sil24_probe_reset(struct ata_port *ap, unsigned int *classes)
503 {
504         return ata_drive_probe_reset(ap, ata_std_probeinit,
505                                      sil24_softreset, sil24_hardreset,
506                                      ata_std_postreset, classes);
507 }
508
509 static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
510                                  struct sil24_sge *sge)
511 {
512         struct scatterlist *sg;
513         unsigned int idx = 0;
514
515         ata_for_each_sg(sg, qc) {
516                 sge->addr = cpu_to_le64(sg_dma_address(sg));
517                 sge->cnt = cpu_to_le32(sg_dma_len(sg));
518                 if (ata_sg_is_last(sg, qc))
519                         sge->flags = cpu_to_le32(SGE_TRM);
520                 else
521                         sge->flags = 0;
522
523                 sge++;
524                 idx++;
525         }
526 }
527
528 static void sil24_qc_prep(struct ata_queued_cmd *qc)
529 {
530         struct ata_port *ap = qc->ap;
531         struct sil24_port_priv *pp = ap->private_data;
532         union sil24_cmd_block *cb = pp->cmd_block + qc->tag;
533         struct sil24_prb *prb;
534         struct sil24_sge *sge;
535
536         switch (qc->tf.protocol) {
537         case ATA_PROT_PIO:
538         case ATA_PROT_DMA:
539         case ATA_PROT_NODATA:
540                 prb = &cb->ata.prb;
541                 sge = cb->ata.sge;
542                 prb->ctrl = 0;
543                 break;
544
545         case ATA_PROT_ATAPI:
546         case ATA_PROT_ATAPI_DMA:
547         case ATA_PROT_ATAPI_NODATA:
548                 prb = &cb->atapi.prb;
549                 sge = cb->atapi.sge;
550                 memset(cb->atapi.cdb, 0, 32);
551                 memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len);
552
553                 if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) {
554                         if (qc->tf.flags & ATA_TFLAG_WRITE)
555                                 prb->ctrl = PRB_CTRL_PACKET_WRITE;
556                         else
557                                 prb->ctrl = PRB_CTRL_PACKET_READ;
558                 } else
559                         prb->ctrl = 0;
560
561                 break;
562
563         default:
564                 prb = NULL;     /* shut up, gcc */
565                 sge = NULL;
566                 BUG();
567         }
568
569         ata_tf_to_fis(&qc->tf, prb->fis, 0);
570
571         if (qc->flags & ATA_QCFLAG_DMAMAP)
572                 sil24_fill_sg(qc, sge);
573 }
574
575 static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
576 {
577         struct ata_port *ap = qc->ap;
578         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
579         struct sil24_port_priv *pp = ap->private_data;
580         dma_addr_t paddr = pp->cmd_block_dma + qc->tag * sizeof(*pp->cmd_block);
581
582         writel((u32)paddr, port + PORT_CMD_ACTIVATE);
583         return 0;
584 }
585
586 static void sil24_irq_clear(struct ata_port *ap)
587 {
588         /* unused */
589 }
590
591 static int __sil24_restart_controller(void __iomem *port)
592 {
593         u32 tmp;
594         int cnt;
595
596         writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
597
598         /* Max ~10ms */
599         for (cnt = 0; cnt < 10000; cnt++) {
600                 tmp = readl(port + PORT_CTRL_STAT);
601                 if (tmp & PORT_CS_RDY)
602                         return 0;
603                 udelay(1);
604         }
605
606         return -1;
607 }
608
609 static void sil24_restart_controller(struct ata_port *ap)
610 {
611         if (__sil24_restart_controller((void __iomem *)ap->ioaddr.cmd_addr))
612                 printk(KERN_ERR DRV_NAME
613                        " ata%u: failed to restart controller\n", ap->id);
614 }
615
616 static int __sil24_reset_controller(void __iomem *port)
617 {
618         int cnt;
619         u32 tmp;
620
621         /* Reset controller state.  Is this correct? */
622         writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
623         readl(port + PORT_CTRL_STAT);   /* sync */
624
625         /* Max ~100ms */
626         for (cnt = 0; cnt < 1000; cnt++) {
627                 udelay(100);
628                 tmp = readl(port + PORT_CTRL_STAT);
629                 if (!(tmp & PORT_CS_DEV_RST))
630                         break;
631         }
632
633         if (tmp & PORT_CS_DEV_RST)
634                 return -1;
635
636         if (tmp & PORT_CS_RDY)
637                 return 0;
638
639         return __sil24_restart_controller(port);
640 }
641
642 static void sil24_reset_controller(struct ata_port *ap)
643 {
644         printk(KERN_NOTICE DRV_NAME
645                " ata%u: resetting controller...\n", ap->id);
646         if (__sil24_reset_controller((void __iomem *)ap->ioaddr.cmd_addr))
647                 printk(KERN_ERR DRV_NAME
648                        " ata%u: failed to reset controller\n", ap->id);
649 }
650
651 static void sil24_eng_timeout(struct ata_port *ap)
652 {
653         struct ata_queued_cmd *qc;
654
655         qc = ata_qc_from_tag(ap, ap->active_tag);
656
657         printk(KERN_ERR "ata%u: command timeout\n", ap->id);
658         qc->err_mask |= AC_ERR_TIMEOUT;
659         ata_eh_qc_complete(qc);
660
661         sil24_reset_controller(ap);
662 }
663
664 static void sil24_error_intr(struct ata_port *ap, u32 slot_stat)
665 {
666         struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
667         struct sil24_port_priv *pp = ap->private_data;
668         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
669         u32 irq_stat, cmd_err, sstatus, serror;
670         unsigned int err_mask;
671
672         irq_stat = readl(port + PORT_IRQ_STAT);
673         writel(irq_stat, port + PORT_IRQ_STAT);         /* clear irq */
674
675         if (!(irq_stat & PORT_IRQ_ERROR)) {
676                 /* ignore non-completion, non-error irqs for now */
677                 printk(KERN_WARNING DRV_NAME
678                        "ata%u: non-error exception irq (irq_stat %x)\n",
679                        ap->id, irq_stat);
680                 return;
681         }
682
683         cmd_err = readl(port + PORT_CMD_ERR);
684         sstatus = readl(port + PORT_SSTATUS);
685         serror = readl(port + PORT_SERROR);
686         if (serror)
687                 writel(serror, port + PORT_SERROR);
688
689         /*
690          * Don't log ATAPI device errors.  They're supposed to happen
691          * and any serious errors will be logged using sense data by
692          * the SCSI layer.
693          */
694         if (ap->device[0].class != ATA_DEV_ATAPI || cmd_err > PORT_CERR_SDB)
695                 printk("ata%u: error interrupt on port%d\n"
696                        "  stat=0x%x irq=0x%x cmd_err=%d sstatus=0x%x serror=0x%x\n",
697                        ap->id, ap->port_no, slot_stat, irq_stat, cmd_err, sstatus, serror);
698
699         if (cmd_err == PORT_CERR_DEV || cmd_err == PORT_CERR_SDB) {
700                 /*
701                  * Device is reporting error, tf registers are valid.
702                  */
703                 sil24_update_tf(ap);
704                 err_mask = ac_err_mask(pp->tf.command);
705                 sil24_restart_controller(ap);
706         } else {
707                 /*
708                  * Other errors.  libata currently doesn't have any
709                  * mechanism to report these errors.  Just turn on
710                  * ATA_ERR.
711                  */
712                 err_mask = AC_ERR_OTHER;
713                 sil24_reset_controller(ap);
714         }
715
716         if (qc) {
717                 qc->err_mask |= err_mask;
718                 ata_qc_complete(qc);
719         }
720 }
721
722 static inline void sil24_host_intr(struct ata_port *ap)
723 {
724         struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
725         void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
726         u32 slot_stat;
727
728         slot_stat = readl(port + PORT_SLOT_STAT);
729         if (!(slot_stat & HOST_SSTAT_ATTN)) {
730                 struct sil24_port_priv *pp = ap->private_data;
731                 /*
732                  * !HOST_SSAT_ATTN guarantees successful completion,
733                  * so reading back tf registers is unnecessary for
734                  * most commands.  TODO: read tf registers for
735                  * commands which require these values on successful
736                  * completion (EXECUTE DEVICE DIAGNOSTIC, CHECK POWER,
737                  * DEVICE RESET and READ PORT MULTIPLIER (any more?).
738                  */
739                 sil24_update_tf(ap);
740
741                 if (qc) {
742                         qc->err_mask |= ac_err_mask(pp->tf.command);
743                         ata_qc_complete(qc);
744                 }
745         } else
746                 sil24_error_intr(ap, slot_stat);
747 }
748
749 static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
750 {
751         struct ata_host_set *host_set = dev_instance;
752         struct sil24_host_priv *hpriv = host_set->private_data;
753         unsigned handled = 0;
754         u32 status;
755         int i;
756
757         status = readl(hpriv->host_base + HOST_IRQ_STAT);
758
759         if (status == 0xffffffff) {
760                 printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
761                        "PCI fault or device removal?\n");
762                 goto out;
763         }
764
765         if (!(status & IRQ_STAT_4PORTS))
766                 goto out;
767
768         spin_lock(&host_set->lock);
769
770         for (i = 0; i < host_set->n_ports; i++)
771                 if (status & (1 << i)) {
772                         struct ata_port *ap = host_set->ports[i];
773                         if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
774                                 sil24_host_intr(host_set->ports[i]);
775                                 handled++;
776                         } else
777                                 printk(KERN_ERR DRV_NAME
778                                        ": interrupt from disabled port %d\n", i);
779                 }
780
781         spin_unlock(&host_set->lock);
782  out:
783         return IRQ_RETVAL(handled);
784 }
785
786 static inline void sil24_cblk_free(struct sil24_port_priv *pp, struct device *dev)
787 {
788         const size_t cb_size = sizeof(*pp->cmd_block);
789
790         dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma);
791 }
792
793 static int sil24_port_start(struct ata_port *ap)
794 {
795         struct device *dev = ap->host_set->dev;
796         struct sil24_port_priv *pp;
797         union sil24_cmd_block *cb;
798         size_t cb_size = sizeof(*cb);
799         dma_addr_t cb_dma;
800         int rc = -ENOMEM;
801
802         pp = kzalloc(sizeof(*pp), GFP_KERNEL);
803         if (!pp)
804                 goto err_out;
805
806         pp->tf.command = ATA_DRDY;
807
808         cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
809         if (!cb)
810                 goto err_out_pp;
811         memset(cb, 0, cb_size);
812
813         rc = ata_pad_alloc(ap, dev);
814         if (rc)
815                 goto err_out_pad;
816
817         pp->cmd_block = cb;
818         pp->cmd_block_dma = cb_dma;
819
820         ap->private_data = pp;
821
822         return 0;
823
824 err_out_pad:
825         sil24_cblk_free(pp, dev);
826 err_out_pp:
827         kfree(pp);
828 err_out:
829         return rc;
830 }
831
832 static void sil24_port_stop(struct ata_port *ap)
833 {
834         struct device *dev = ap->host_set->dev;
835         struct sil24_port_priv *pp = ap->private_data;
836
837         sil24_cblk_free(pp, dev);
838         ata_pad_free(ap, dev);
839         kfree(pp);
840 }
841
842 static void sil24_host_stop(struct ata_host_set *host_set)
843 {
844         struct sil24_host_priv *hpriv = host_set->private_data;
845         struct pci_dev *pdev = to_pci_dev(host_set->dev);
846
847         pci_iounmap(pdev, hpriv->host_base);
848         pci_iounmap(pdev, hpriv->port_base);
849         kfree(hpriv);
850 }
851
852 static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
853 {
854         static int printed_version = 0;
855         unsigned int board_id = (unsigned int)ent->driver_data;
856         struct ata_port_info *pinfo = &sil24_port_info[board_id];
857         struct ata_probe_ent *probe_ent = NULL;
858         struct sil24_host_priv *hpriv = NULL;
859         void __iomem *host_base = NULL;
860         void __iomem *port_base = NULL;
861         int i, rc;
862
863         if (!printed_version++)
864                 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
865
866         rc = pci_enable_device(pdev);
867         if (rc)
868                 return rc;
869
870         rc = pci_request_regions(pdev, DRV_NAME);
871         if (rc)
872                 goto out_disable;
873
874         rc = -ENOMEM;
875         /* map mmio registers */
876         host_base = pci_iomap(pdev, 0, 0);
877         if (!host_base)
878                 goto out_free;
879         port_base = pci_iomap(pdev, 2, 0);
880         if (!port_base)
881                 goto out_free;
882
883         /* allocate & init probe_ent and hpriv */
884         probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
885         if (!probe_ent)
886                 goto out_free;
887
888         hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
889         if (!hpriv)
890                 goto out_free;
891
892         probe_ent->dev = pci_dev_to_dev(pdev);
893         INIT_LIST_HEAD(&probe_ent->node);
894
895         probe_ent->sht          = pinfo->sht;
896         probe_ent->host_flags   = pinfo->host_flags;
897         probe_ent->pio_mask     = pinfo->pio_mask;
898         probe_ent->mwdma_mask   = pinfo->mwdma_mask;
899         probe_ent->udma_mask    = pinfo->udma_mask;
900         probe_ent->port_ops     = pinfo->port_ops;
901         probe_ent->n_ports      = SIL24_FLAG2NPORTS(pinfo->host_flags);
902
903         probe_ent->irq = pdev->irq;
904         probe_ent->irq_flags = SA_SHIRQ;
905         probe_ent->mmio_base = port_base;
906         probe_ent->private_data = hpriv;
907
908         hpriv->host_base = host_base;
909         hpriv->port_base = port_base;
910
911         /*
912          * Configure the device
913          */
914         /*
915          * FIXME: This device is certainly 64-bit capable.  We just
916          * don't know how to use it.  After fixing 32bit activation in
917          * this function, enable 64bit masks here.
918          */
919         rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
920         if (rc) {
921                 dev_printk(KERN_ERR, &pdev->dev,
922                            "32-bit DMA enable failed\n");
923                 goto out_free;
924         }
925         rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
926         if (rc) {
927                 dev_printk(KERN_ERR, &pdev->dev,
928                            "32-bit consistent DMA enable failed\n");
929                 goto out_free;
930         }
931
932         /* GPIO off */
933         writel(0, host_base + HOST_FLASH_CMD);
934
935         /* clear global reset & mask interrupts during initialization */
936         writel(0, host_base + HOST_CTRL);
937
938         for (i = 0; i < probe_ent->n_ports; i++) {
939                 void __iomem *port = port_base + i * PORT_REGS_SIZE;
940                 unsigned long portu = (unsigned long)port;
941                 u32 tmp;
942
943                 probe_ent->port[i].cmd_addr = portu + PORT_PRB;
944                 probe_ent->port[i].scr_addr = portu + PORT_SCONTROL;
945
946                 ata_std_ports(&probe_ent->port[i]);
947
948                 /* Initial PHY setting */
949                 writel(0x20c, port + PORT_PHY_CFG);
950
951                 /* Clear port RST */
952                 tmp = readl(port + PORT_CTRL_STAT);
953                 if (tmp & PORT_CS_PORT_RST) {
954                         writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
955                         tmp = ata_wait_register(port + PORT_CTRL_STAT,
956                                                 PORT_CS_PORT_RST,
957                                                 PORT_CS_PORT_RST, 10, 100);
958                         if (tmp & PORT_CS_PORT_RST)
959                                 dev_printk(KERN_ERR, &pdev->dev,
960                                            "failed to clear port RST\n");
961                 }
962
963                 /* Zero error counters. */
964                 writel(0x8000, port + PORT_DECODE_ERR_THRESH);
965                 writel(0x8000, port + PORT_CRC_ERR_THRESH);
966                 writel(0x8000, port + PORT_HSHK_ERR_THRESH);
967                 writel(0x0000, port + PORT_DECODE_ERR_CNT);
968                 writel(0x0000, port + PORT_CRC_ERR_CNT);
969                 writel(0x0000, port + PORT_HSHK_ERR_CNT);
970
971                 /* FIXME: 32bit activation? */
972                 writel(0, port + PORT_ACTIVATE_UPPER_ADDR);
973                 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_STAT);
974
975                 /* Configure interrupts */
976                 writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
977                 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR |
978                        PORT_IRQ_SDB_NOTIFY, port + PORT_IRQ_ENABLE_SET);
979
980                 /* Clear interrupts */
981                 writel(0x0fff0fff, port + PORT_IRQ_STAT);
982                 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
983
984                 /* Clear port multiplier enable and resume bits */
985                 writel(PORT_CS_PM_EN | PORT_CS_RESUME, port + PORT_CTRL_CLR);
986
987                 /* Reset itself */
988                 if (__sil24_reset_controller(port))
989                         dev_printk(KERN_ERR, &pdev->dev,
990                                    "failed to reset controller\n");
991         }
992
993         /* Turn on interrupts */
994         writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
995
996         pci_set_master(pdev);
997
998         /* FIXME: check ata_device_add return value */
999         ata_device_add(probe_ent);
1000
1001         kfree(probe_ent);
1002         return 0;
1003
1004  out_free:
1005         if (host_base)
1006                 pci_iounmap(pdev, host_base);
1007         if (port_base)
1008                 pci_iounmap(pdev, port_base);
1009         kfree(probe_ent);
1010         kfree(hpriv);
1011         pci_release_regions(pdev);
1012  out_disable:
1013         pci_disable_device(pdev);
1014         return rc;
1015 }
1016
1017 static int __init sil24_init(void)
1018 {
1019         return pci_module_init(&sil24_pci_driver);
1020 }
1021
1022 static void __exit sil24_exit(void)
1023 {
1024         pci_unregister_driver(&sil24_pci_driver);
1025 }
1026
1027 MODULE_AUTHOR("Tejun Heo");
1028 MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
1029 MODULE_LICENSE("GPL");
1030 MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
1031
1032 module_init(sil24_init);
1033 module_exit(sil24_exit);