sata_sil24.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. * NOTE: No NCQ/ATAPI support yet. The preview driver didn't support
  9. * NCQ nor ATAPI, and, unfortunately, I couldn't find out how to make
  10. * those work. Enabling those shouldn't be difficult. Basic
  11. * structure is all there (in libata-dev tree). If you have any
  12. * information about this hardware, please contact me or linux-ide.
  13. * Info is needed on...
  14. *
  15. * - How to issue tagged commands and turn on sactive on issue accordingly.
  16. * - Where to put an ATAPI command and how to tell the device to send it.
  17. * - How to enable/use 64bit.
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/pci.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/delay.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/device.h>
  38. #include <scsi/scsi_host.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <linux/libata.h>
  41. #include <asm/io.h>
  42. #define DRV_NAME "sata_sil24"
  43. #define DRV_VERSION "0.22" /* Silicon Image's preview driver was 0.10 */
  44. /*
  45. * Port request block (PRB) 32 bytes
  46. */
  47. struct sil24_prb {
  48. u16 ctrl;
  49. u16 prot;
  50. u32 rx_cnt;
  51. u8 fis[6 * 4];
  52. };
  53. /*
  54. * Scatter gather entry (SGE) 16 bytes
  55. */
  56. struct sil24_sge {
  57. u64 addr;
  58. u32 cnt;
  59. u32 flags;
  60. };
  61. /*
  62. * Port multiplier
  63. */
  64. struct sil24_port_multiplier {
  65. u32 diag;
  66. u32 sactive;
  67. };
  68. enum {
  69. /*
  70. * Global controller registers (128 bytes @ BAR0)
  71. */
  72. /* 32 bit regs */
  73. HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */
  74. HOST_CTRL = 0x40,
  75. HOST_IRQ_STAT = 0x44,
  76. HOST_PHY_CFG = 0x48,
  77. HOST_BIST_CTRL = 0x50,
  78. HOST_BIST_PTRN = 0x54,
  79. HOST_BIST_STAT = 0x58,
  80. HOST_MEM_BIST_STAT = 0x5c,
  81. HOST_FLASH_CMD = 0x70,
  82. /* 8 bit regs */
  83. HOST_FLASH_DATA = 0x74,
  84. HOST_TRANSITION_DETECT = 0x75,
  85. HOST_GPIO_CTRL = 0x76,
  86. HOST_I2C_ADDR = 0x78, /* 32 bit */
  87. HOST_I2C_DATA = 0x7c,
  88. HOST_I2C_XFER_CNT = 0x7e,
  89. HOST_I2C_CTRL = 0x7f,
  90. /* HOST_SLOT_STAT bits */
  91. HOST_SSTAT_ATTN = (1 << 31),
  92. /*
  93. * Port registers
  94. * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
  95. */
  96. PORT_REGS_SIZE = 0x2000,
  97. PORT_PRB = 0x0000, /* (32 bytes PRB + 16 bytes SGEs * 6) * 31 (3968 bytes) */
  98. PORT_PM = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */
  99. /* 32 bit regs */
  100. PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */
  101. PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */
  102. PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */
  103. PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */
  104. PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */
  105. PORT_ACTIVATE_UPPER_ADDR= 0x101c,
  106. PORT_EXEC_FIFO = 0x1020, /* command execution fifo */
  107. PORT_CMD_ERR = 0x1024, /* command error number */
  108. PORT_FIS_CFG = 0x1028,
  109. PORT_FIFO_THRES = 0x102c,
  110. /* 16 bit regs */
  111. PORT_DECODE_ERR_CNT = 0x1040,
  112. PORT_DECODE_ERR_THRESH = 0x1042,
  113. PORT_CRC_ERR_CNT = 0x1044,
  114. PORT_CRC_ERR_THRESH = 0x1046,
  115. PORT_HSHK_ERR_CNT = 0x1048,
  116. PORT_HSHK_ERR_THRESH = 0x104a,
  117. /* 32 bit regs */
  118. PORT_PHY_CFG = 0x1050,
  119. PORT_SLOT_STAT = 0x1800,
  120. PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
  121. PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
  122. PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
  123. PORT_SCONTROL = 0x1f00,
  124. PORT_SSTATUS = 0x1f04,
  125. PORT_SERROR = 0x1f08,
  126. PORT_SACTIVE = 0x1f0c,
  127. /* PORT_CTRL_STAT bits */
  128. PORT_CS_PORT_RST = (1 << 0), /* port reset */
  129. PORT_CS_DEV_RST = (1 << 1), /* device reset */
  130. PORT_CS_INIT = (1 << 2), /* port initialize */
  131. PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */
  132. PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */
  133. PORT_CS_RESUME = (1 << 6), /* port resume */
  134. PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */
  135. PORT_CS_PM_EN = (1 << 13), /* port multiplier enable */
  136. PORT_CS_RDY = (1 << 31), /* port ready to accept commands */
  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_FIS = (1 << 11), /* SDB FIS received */
  147. /* bits[27:16] are unmasked (raw) */
  148. PORT_IRQ_RAW_SHIFT = 16,
  149. PORT_IRQ_MASKED_MASK = 0x7ff,
  150. PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT),
  151. /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
  152. PORT_IRQ_STEER_SHIFT = 30,
  153. PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT),
  154. /* PORT_CMD_ERR constants */
  155. PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */
  156. PORT_CERR_SDB = 2, /* Error bit in SDB FIS */
  157. PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */
  158. PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */
  159. PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */
  160. PORT_CERR_DIRECTION = 6, /* Data direction mismatch */
  161. PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */
  162. PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */
  163. PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */
  164. PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */
  165. PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */
  166. PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */
  167. PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
  168. PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
  169. PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */
  170. PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */
  171. PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
  172. PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */
  173. PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */
  174. PORT_CERR_XFR_MSGABRT = 34, /* PSD ecode 10 - master abort */
  175. PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */
  176. PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */
  177. /* bits of PRB control field */
  178. PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */
  179. PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */
  180. PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */
  181. PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */
  182. PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */
  183. /* PRB protocol field */
  184. PRB_PROT_PACKET = (1 << 0),
  185. PRB_PROT_TCQ = (1 << 1),
  186. PRB_PROT_NCQ = (1 << 2),
  187. PRB_PROT_READ = (1 << 3),
  188. PRB_PROT_WRITE = (1 << 4),
  189. PRB_PROT_TRANSPARENT = (1 << 5),
  190. /*
  191. * Other constants
  192. */
  193. SGE_TRM = (1 << 31), /* Last SGE in chain */
  194. SGE_LNK = (1 << 30), /* linked list
  195. Points to SGT, not SGE */
  196. SGE_DRD = (1 << 29), /* discard data read (/dev/null)
  197. data address ignored */
  198. /* board id */
  199. BID_SIL3124 = 0,
  200. BID_SIL3132 = 1,
  201. BID_SIL3131 = 2,
  202. IRQ_STAT_4PORTS = 0xf,
  203. };
  204. struct sil24_cmd_block {
  205. struct sil24_prb prb;
  206. struct sil24_sge sge[LIBATA_MAX_PRD];
  207. };
  208. /*
  209. * ap->private_data
  210. *
  211. * The preview driver always returned 0 for status. We emulate it
  212. * here from the previous interrupt.
  213. */
  214. struct sil24_port_priv {
  215. struct sil24_cmd_block *cmd_block; /* 32 cmd blocks */
  216. dma_addr_t cmd_block_dma; /* DMA base addr for them */
  217. struct ata_taskfile tf; /* Cached taskfile registers */
  218. };
  219. /* ap->host_set->private_data */
  220. struct sil24_host_priv {
  221. void __iomem *host_base; /* global controller control (128 bytes @BAR0) */
  222. void __iomem *port_base; /* port registers (4 * 8192 bytes @BAR2) */
  223. };
  224. static u8 sil24_check_status(struct ata_port *ap);
  225. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg);
  226. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
  227. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  228. static void sil24_phy_reset(struct ata_port *ap);
  229. static void sil24_qc_prep(struct ata_queued_cmd *qc);
  230. static int sil24_qc_issue(struct ata_queued_cmd *qc);
  231. static void sil24_irq_clear(struct ata_port *ap);
  232. static void sil24_eng_timeout(struct ata_port *ap);
  233. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
  234. static int sil24_port_start(struct ata_port *ap);
  235. static void sil24_port_stop(struct ata_port *ap);
  236. static void sil24_host_stop(struct ata_host_set *host_set);
  237. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  238. static const struct pci_device_id sil24_pci_tbl[] = {
  239. { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 },
  240. { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 },
  241. { 0x1095, 0x3131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  242. { 0x1095, 0x3531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 },
  243. { } /* terminate list */
  244. };
  245. static struct pci_driver sil24_pci_driver = {
  246. .name = DRV_NAME,
  247. .id_table = sil24_pci_tbl,
  248. .probe = sil24_init_one,
  249. .remove = ata_pci_remove_one, /* safe? */
  250. };
  251. static struct scsi_host_template sil24_sht = {
  252. .module = THIS_MODULE,
  253. .name = DRV_NAME,
  254. .ioctl = ata_scsi_ioctl,
  255. .queuecommand = ata_scsi_queuecmd,
  256. .eh_strategy_handler = ata_scsi_error,
  257. .can_queue = ATA_DEF_QUEUE,
  258. .this_id = ATA_SHT_THIS_ID,
  259. .sg_tablesize = LIBATA_MAX_PRD,
  260. .max_sectors = ATA_MAX_SECTORS,
  261. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  262. .emulated = ATA_SHT_EMULATED,
  263. .use_clustering = ATA_SHT_USE_CLUSTERING,
  264. .proc_name = DRV_NAME,
  265. .dma_boundary = ATA_DMA_BOUNDARY,
  266. .slave_configure = ata_scsi_slave_config,
  267. .bios_param = ata_std_bios_param,
  268. .ordered_flush = 1, /* NCQ not supported yet */
  269. };
  270. static const struct ata_port_operations sil24_ops = {
  271. .port_disable = ata_port_disable,
  272. .check_status = sil24_check_status,
  273. .check_altstatus = sil24_check_status,
  274. .dev_select = ata_noop_dev_select,
  275. .tf_read = sil24_tf_read,
  276. .phy_reset = sil24_phy_reset,
  277. .qc_prep = sil24_qc_prep,
  278. .qc_issue = sil24_qc_issue,
  279. .eng_timeout = sil24_eng_timeout,
  280. .irq_handler = sil24_interrupt,
  281. .irq_clear = sil24_irq_clear,
  282. .scr_read = sil24_scr_read,
  283. .scr_write = sil24_scr_write,
  284. .port_start = sil24_port_start,
  285. .port_stop = sil24_port_stop,
  286. .host_stop = sil24_host_stop,
  287. };
  288. /*
  289. * Use bits 30-31 of host_flags to encode available port numbers.
  290. * Current maxium is 4.
  291. */
  292. #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30)
  293. #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1)
  294. static struct ata_port_info sil24_port_info[] = {
  295. /* sil_3124 */
  296. {
  297. .sht = &sil24_sht,
  298. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  299. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  300. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(4),
  301. .pio_mask = 0x1f, /* pio0-4 */
  302. .mwdma_mask = 0x07, /* mwdma0-2 */
  303. .udma_mask = 0x3f, /* udma0-5 */
  304. .port_ops = &sil24_ops,
  305. },
  306. /* sil_3132 */
  307. {
  308. .sht = &sil24_sht,
  309. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  310. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  311. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(2),
  312. .pio_mask = 0x1f, /* pio0-4 */
  313. .mwdma_mask = 0x07, /* mwdma0-2 */
  314. .udma_mask = 0x3f, /* udma0-5 */
  315. .port_ops = &sil24_ops,
  316. },
  317. /* sil_3131/sil_3531 */
  318. {
  319. .sht = &sil24_sht,
  320. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  321. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  322. ATA_FLAG_PIO_DMA | SIL24_NPORTS2FLAG(1),
  323. .pio_mask = 0x1f, /* pio0-4 */
  324. .mwdma_mask = 0x07, /* mwdma0-2 */
  325. .udma_mask = 0x3f, /* udma0-5 */
  326. .port_ops = &sil24_ops,
  327. },
  328. };
  329. static inline void sil24_update_tf(struct ata_port *ap)
  330. {
  331. struct sil24_port_priv *pp = ap->private_data;
  332. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  333. struct sil24_prb __iomem *prb = port;
  334. u8 fis[6 * 4];
  335. memcpy_fromio(fis, prb->fis, 6 * 4);
  336. ata_tf_from_fis(fis, &pp->tf);
  337. }
  338. static u8 sil24_check_status(struct ata_port *ap)
  339. {
  340. struct sil24_port_priv *pp = ap->private_data;
  341. return pp->tf.command;
  342. }
  343. static int sil24_scr_map[] = {
  344. [SCR_CONTROL] = 0,
  345. [SCR_STATUS] = 1,
  346. [SCR_ERROR] = 2,
  347. [SCR_ACTIVE] = 3,
  348. };
  349. static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg)
  350. {
  351. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  352. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  353. void __iomem *addr;
  354. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  355. return readl(scr_addr + sil24_scr_map[sc_reg] * 4);
  356. }
  357. return 0xffffffffU;
  358. }
  359. static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  360. {
  361. void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr;
  362. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  363. void __iomem *addr;
  364. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  365. writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
  366. }
  367. }
  368. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  369. {
  370. struct sil24_port_priv *pp = ap->private_data;
  371. *tf = pp->tf;
  372. }
  373. static void sil24_phy_reset(struct ata_port *ap)
  374. {
  375. __sata_phy_reset(ap);
  376. /*
  377. * No ATAPI yet. Just unconditionally indicate ATA device.
  378. * If ATAPI device is attached, it will fail ATA_CMD_ID_ATA
  379. * and libata core will ignore the device.
  380. */
  381. if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
  382. ap->device[0].class = ATA_DEV_ATA;
  383. }
  384. static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
  385. struct sil24_cmd_block *cb)
  386. {
  387. struct sil24_sge *sge = cb->sge;
  388. struct scatterlist *sg;
  389. unsigned int idx = 0;
  390. ata_for_each_sg(sg, qc) {
  391. sge->addr = cpu_to_le64(sg_dma_address(sg));
  392. sge->cnt = cpu_to_le32(sg_dma_len(sg));
  393. if (ata_sg_is_last(sg, qc))
  394. sge->flags = cpu_to_le32(SGE_TRM);
  395. else
  396. sge->flags = 0;
  397. sge++;
  398. idx++;
  399. }
  400. }
  401. static void sil24_qc_prep(struct ata_queued_cmd *qc)
  402. {
  403. struct ata_port *ap = qc->ap;
  404. struct sil24_port_priv *pp = ap->private_data;
  405. struct sil24_cmd_block *cb = pp->cmd_block + qc->tag;
  406. struct sil24_prb *prb = &cb->prb;
  407. switch (qc->tf.protocol) {
  408. case ATA_PROT_PIO:
  409. case ATA_PROT_DMA:
  410. case ATA_PROT_NODATA:
  411. break;
  412. default:
  413. /* ATAPI isn't supported yet */
  414. BUG();
  415. }
  416. ata_tf_to_fis(&qc->tf, prb->fis, 0);
  417. if (qc->flags & ATA_QCFLAG_DMAMAP)
  418. sil24_fill_sg(qc, cb);
  419. }
  420. static int sil24_qc_issue(struct ata_queued_cmd *qc)
  421. {
  422. struct ata_port *ap = qc->ap;
  423. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  424. struct sil24_port_priv *pp = ap->private_data;
  425. dma_addr_t paddr = pp->cmd_block_dma + qc->tag * sizeof(*pp->cmd_block);
  426. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  427. return 0;
  428. }
  429. static void sil24_irq_clear(struct ata_port *ap)
  430. {
  431. /* unused */
  432. }
  433. static int __sil24_reset_controller(void __iomem *port)
  434. {
  435. int cnt;
  436. u32 tmp;
  437. /* Reset controller state. Is this correct? */
  438. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  439. readl(port + PORT_CTRL_STAT); /* sync */
  440. /* Max ~100ms */
  441. for (cnt = 0; cnt < 1000; cnt++) {
  442. udelay(100);
  443. tmp = readl(port + PORT_CTRL_STAT);
  444. if (!(tmp & PORT_CS_DEV_RST))
  445. break;
  446. }
  447. if (tmp & PORT_CS_DEV_RST)
  448. return -1;
  449. return 0;
  450. }
  451. static void sil24_reset_controller(struct ata_port *ap)
  452. {
  453. printk(KERN_NOTICE DRV_NAME
  454. " ata%u: resetting controller...\n", ap->id);
  455. if (__sil24_reset_controller((void __iomem *)ap->ioaddr.cmd_addr))
  456. printk(KERN_ERR DRV_NAME
  457. " ata%u: failed to reset controller\n", ap->id);
  458. }
  459. static void sil24_eng_timeout(struct ata_port *ap)
  460. {
  461. struct ata_queued_cmd *qc;
  462. qc = ata_qc_from_tag(ap, ap->active_tag);
  463. if (!qc) {
  464. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  465. ap->id);
  466. return;
  467. }
  468. /*
  469. * hack alert! We cannot use the supplied completion
  470. * function from inside the ->eh_strategy_handler() thread.
  471. * libata is the only user of ->eh_strategy_handler() in
  472. * any kernel, so the default scsi_done() assumes it is
  473. * not being called from the SCSI EH.
  474. */
  475. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  476. qc->scsidone = scsi_finish_command;
  477. ata_qc_complete(qc, AC_ERR_OTHER);
  478. sil24_reset_controller(ap);
  479. }
  480. static void sil24_error_intr(struct ata_port *ap, u32 slot_stat)
  481. {
  482. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  483. struct sil24_port_priv *pp = ap->private_data;
  484. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  485. u32 irq_stat, cmd_err, sstatus, serror;
  486. unsigned int err_mask;
  487. irq_stat = readl(port + PORT_IRQ_STAT);
  488. writel(irq_stat, port + PORT_IRQ_STAT); /* clear irq */
  489. if (!(irq_stat & PORT_IRQ_ERROR)) {
  490. /* ignore non-completion, non-error irqs for now */
  491. printk(KERN_WARNING DRV_NAME
  492. "ata%u: non-error exception irq (irq_stat %x)\n",
  493. ap->id, irq_stat);
  494. return;
  495. }
  496. cmd_err = readl(port + PORT_CMD_ERR);
  497. sstatus = readl(port + PORT_SSTATUS);
  498. serror = readl(port + PORT_SERROR);
  499. if (serror)
  500. writel(serror, port + PORT_SERROR);
  501. printk(KERN_ERR DRV_NAME " ata%u: error interrupt on port%d\n"
  502. " stat=0x%x irq=0x%x cmd_err=%d sstatus=0x%x serror=0x%x\n",
  503. ap->id, ap->port_no, slot_stat, irq_stat, cmd_err, sstatus, serror);
  504. if (cmd_err == PORT_CERR_DEV || cmd_err == PORT_CERR_SDB) {
  505. /*
  506. * Device is reporting error, tf registers are valid.
  507. */
  508. sil24_update_tf(ap);
  509. err_mask = ac_err_mask(pp->tf.command);
  510. } else {
  511. /*
  512. * Other errors. libata currently doesn't have any
  513. * mechanism to report these errors. Just turn on
  514. * ATA_ERR.
  515. */
  516. err_mask = AC_ERR_OTHER;
  517. }
  518. if (qc)
  519. ata_qc_complete(qc, err_mask);
  520. sil24_reset_controller(ap);
  521. }
  522. static inline void sil24_host_intr(struct ata_port *ap)
  523. {
  524. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  525. void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr;
  526. u32 slot_stat;
  527. slot_stat = readl(port + PORT_SLOT_STAT);
  528. if (!(slot_stat & HOST_SSTAT_ATTN)) {
  529. struct sil24_port_priv *pp = ap->private_data;
  530. /*
  531. * !HOST_SSAT_ATTN guarantees successful completion,
  532. * so reading back tf registers is unnecessary for
  533. * most commands. TODO: read tf registers for
  534. * commands which require these values on successful
  535. * completion (EXECUTE DEVICE DIAGNOSTIC, CHECK POWER,
  536. * DEVICE RESET and READ PORT MULTIPLIER (any more?).
  537. */
  538. sil24_update_tf(ap);
  539. if (qc)
  540. ata_qc_complete(qc, ac_err_mask(pp->tf.command));
  541. } else
  542. sil24_error_intr(ap, slot_stat);
  543. }
  544. static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  545. {
  546. struct ata_host_set *host_set = dev_instance;
  547. struct sil24_host_priv *hpriv = host_set->private_data;
  548. unsigned handled = 0;
  549. u32 status;
  550. int i;
  551. status = readl(hpriv->host_base + HOST_IRQ_STAT);
  552. if (status == 0xffffffff) {
  553. printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
  554. "PCI fault or device removal?\n");
  555. goto out;
  556. }
  557. if (!(status & IRQ_STAT_4PORTS))
  558. goto out;
  559. spin_lock(&host_set->lock);
  560. for (i = 0; i < host_set->n_ports; i++)
  561. if (status & (1 << i)) {
  562. struct ata_port *ap = host_set->ports[i];
  563. if (ap && !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
  564. sil24_host_intr(host_set->ports[i]);
  565. handled++;
  566. } else
  567. printk(KERN_ERR DRV_NAME
  568. ": interrupt from disabled port %d\n", i);
  569. }
  570. spin_unlock(&host_set->lock);
  571. out:
  572. return IRQ_RETVAL(handled);
  573. }
  574. static inline void sil24_cblk_free(struct sil24_port_priv *pp, struct device *dev)
  575. {
  576. const size_t cb_size = sizeof(*pp->cmd_block);
  577. dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma);
  578. }
  579. static int sil24_port_start(struct ata_port *ap)
  580. {
  581. struct device *dev = ap->host_set->dev;
  582. struct sil24_port_priv *pp;
  583. struct sil24_cmd_block *cb;
  584. size_t cb_size = sizeof(*cb);
  585. dma_addr_t cb_dma;
  586. int rc = -ENOMEM;
  587. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  588. if (!pp)
  589. goto err_out;
  590. pp->tf.command = ATA_DRDY;
  591. cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
  592. if (!cb)
  593. goto err_out_pp;
  594. memset(cb, 0, cb_size);
  595. rc = ata_pad_alloc(ap, dev);
  596. if (rc)
  597. goto err_out_pad;
  598. pp->cmd_block = cb;
  599. pp->cmd_block_dma = cb_dma;
  600. ap->private_data = pp;
  601. return 0;
  602. err_out_pad:
  603. sil24_cblk_free(pp, dev);
  604. err_out_pp:
  605. kfree(pp);
  606. err_out:
  607. return rc;
  608. }
  609. static void sil24_port_stop(struct ata_port *ap)
  610. {
  611. struct device *dev = ap->host_set->dev;
  612. struct sil24_port_priv *pp = ap->private_data;
  613. sil24_cblk_free(pp, dev);
  614. ata_pad_free(ap, dev);
  615. kfree(pp);
  616. }
  617. static void sil24_host_stop(struct ata_host_set *host_set)
  618. {
  619. struct sil24_host_priv *hpriv = host_set->private_data;
  620. iounmap(hpriv->host_base);
  621. iounmap(hpriv->port_base);
  622. kfree(hpriv);
  623. }
  624. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  625. {
  626. static int printed_version = 0;
  627. unsigned int board_id = (unsigned int)ent->driver_data;
  628. struct ata_port_info *pinfo = &sil24_port_info[board_id];
  629. struct ata_probe_ent *probe_ent = NULL;
  630. struct sil24_host_priv *hpriv = NULL;
  631. void __iomem *host_base = NULL;
  632. void __iomem *port_base = NULL;
  633. int i, rc;
  634. if (!printed_version++)
  635. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  636. rc = pci_enable_device(pdev);
  637. if (rc)
  638. return rc;
  639. rc = pci_request_regions(pdev, DRV_NAME);
  640. if (rc)
  641. goto out_disable;
  642. rc = -ENOMEM;
  643. /* ioremap mmio registers */
  644. host_base = ioremap(pci_resource_start(pdev, 0),
  645. pci_resource_len(pdev, 0));
  646. if (!host_base)
  647. goto out_free;
  648. port_base = ioremap(pci_resource_start(pdev, 2),
  649. pci_resource_len(pdev, 2));
  650. if (!port_base)
  651. goto out_free;
  652. /* allocate & init probe_ent and hpriv */
  653. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  654. if (!probe_ent)
  655. goto out_free;
  656. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  657. if (!hpriv)
  658. goto out_free;
  659. memset(probe_ent, 0, sizeof(*probe_ent));
  660. probe_ent->dev = pci_dev_to_dev(pdev);
  661. INIT_LIST_HEAD(&probe_ent->node);
  662. probe_ent->sht = pinfo->sht;
  663. probe_ent->host_flags = pinfo->host_flags;
  664. probe_ent->pio_mask = pinfo->pio_mask;
  665. probe_ent->udma_mask = pinfo->udma_mask;
  666. probe_ent->port_ops = pinfo->port_ops;
  667. probe_ent->n_ports = SIL24_FLAG2NPORTS(pinfo->host_flags);
  668. probe_ent->irq = pdev->irq;
  669. probe_ent->irq_flags = SA_SHIRQ;
  670. probe_ent->mmio_base = port_base;
  671. probe_ent->private_data = hpriv;
  672. memset(hpriv, 0, sizeof(*hpriv));
  673. hpriv->host_base = host_base;
  674. hpriv->port_base = port_base;
  675. /*
  676. * Configure the device
  677. */
  678. /*
  679. * FIXME: This device is certainly 64-bit capable. We just
  680. * don't know how to use it. After fixing 32bit activation in
  681. * this function, enable 64bit masks here.
  682. */
  683. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  684. if (rc) {
  685. dev_printk(KERN_ERR, &pdev->dev,
  686. "32-bit DMA enable failed\n");
  687. goto out_free;
  688. }
  689. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  690. if (rc) {
  691. dev_printk(KERN_ERR, &pdev->dev,
  692. "32-bit consistent DMA enable failed\n");
  693. goto out_free;
  694. }
  695. /* GPIO off */
  696. writel(0, host_base + HOST_FLASH_CMD);
  697. /* Mask interrupts during initialization */
  698. writel(0, host_base + HOST_CTRL);
  699. for (i = 0; i < probe_ent->n_ports; i++) {
  700. void __iomem *port = port_base + i * PORT_REGS_SIZE;
  701. unsigned long portu = (unsigned long)port;
  702. u32 tmp;
  703. int cnt;
  704. probe_ent->port[i].cmd_addr = portu + PORT_PRB;
  705. probe_ent->port[i].scr_addr = portu + PORT_SCONTROL;
  706. ata_std_ports(&probe_ent->port[i]);
  707. /* Initial PHY setting */
  708. writel(0x20c, port + PORT_PHY_CFG);
  709. /* Clear port RST */
  710. tmp = readl(port + PORT_CTRL_STAT);
  711. if (tmp & PORT_CS_PORT_RST) {
  712. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  713. readl(port + PORT_CTRL_STAT); /* sync */
  714. for (cnt = 0; cnt < 10; cnt++) {
  715. msleep(10);
  716. tmp = readl(port + PORT_CTRL_STAT);
  717. if (!(tmp & PORT_CS_PORT_RST))
  718. break;
  719. }
  720. if (tmp & PORT_CS_PORT_RST)
  721. dev_printk(KERN_ERR, &pdev->dev,
  722. "failed to clear port RST\n");
  723. }
  724. /* Zero error counters. */
  725. writel(0x8000, port + PORT_DECODE_ERR_THRESH);
  726. writel(0x8000, port + PORT_CRC_ERR_THRESH);
  727. writel(0x8000, port + PORT_HSHK_ERR_THRESH);
  728. writel(0x0000, port + PORT_DECODE_ERR_CNT);
  729. writel(0x0000, port + PORT_CRC_ERR_CNT);
  730. writel(0x0000, port + PORT_HSHK_ERR_CNT);
  731. /* FIXME: 32bit activation? */
  732. writel(0, port + PORT_ACTIVATE_UPPER_ADDR);
  733. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_STAT);
  734. /* Configure interrupts */
  735. writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
  736. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | PORT_IRQ_SDB_FIS,
  737. port + PORT_IRQ_ENABLE_SET);
  738. /* Clear interrupts */
  739. writel(0x0fff0fff, port + PORT_IRQ_STAT);
  740. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  741. /* Clear port multiplier enable and resume bits */
  742. writel(PORT_CS_PM_EN | PORT_CS_RESUME, port + PORT_CTRL_CLR);
  743. /* Reset itself */
  744. if (__sil24_reset_controller(port))
  745. dev_printk(KERN_ERR, &pdev->dev,
  746. "failed to reset controller\n");
  747. }
  748. /* Turn on interrupts */
  749. writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
  750. pci_set_master(pdev);
  751. /* FIXME: check ata_device_add return value */
  752. ata_device_add(probe_ent);
  753. kfree(probe_ent);
  754. return 0;
  755. out_free:
  756. if (host_base)
  757. iounmap(host_base);
  758. if (port_base)
  759. iounmap(port_base);
  760. kfree(probe_ent);
  761. kfree(hpriv);
  762. pci_release_regions(pdev);
  763. out_disable:
  764. pci_disable_device(pdev);
  765. return rc;
  766. }
  767. static int __init sil24_init(void)
  768. {
  769. return pci_module_init(&sil24_pci_driver);
  770. }
  771. static void __exit sil24_exit(void)
  772. {
  773. pci_unregister_driver(&sil24_pci_driver);
  774. }
  775. MODULE_AUTHOR("Tejun Heo");
  776. MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
  777. MODULE_LICENSE("GPL");
  778. MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
  779. module_init(sil24_init);
  780. module_exit(sil24_exit);