cn66xx_device.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. #include <linux/pci.h>
  19. #include <linux/netdevice.h>
  20. #include "liquidio_common.h"
  21. #include "octeon_droq.h"
  22. #include "octeon_iq.h"
  23. #include "response_manager.h"
  24. #include "octeon_device.h"
  25. #include "octeon_main.h"
  26. #include "cn66xx_regs.h"
  27. #include "cn66xx_device.h"
  28. int lio_cn6xxx_soft_reset(struct octeon_device *oct)
  29. {
  30. octeon_write_csr64(oct, CN6XXX_WIN_WR_MASK_REG, 0xFF);
  31. dev_dbg(&oct->pci_dev->dev, "BIST enabled for soft reset\n");
  32. lio_pci_writeq(oct, 1, CN6XXX_CIU_SOFT_BIST);
  33. octeon_write_csr64(oct, CN6XXX_SLI_SCRATCH1, 0x1234ULL);
  34. lio_pci_readq(oct, CN6XXX_CIU_SOFT_RST);
  35. lio_pci_writeq(oct, 1, CN6XXX_CIU_SOFT_RST);
  36. /* make sure that the reset is written before starting timer */
  37. mmiowb();
  38. /* Wait for 10ms as Octeon resets. */
  39. mdelay(100);
  40. if (octeon_read_csr64(oct, CN6XXX_SLI_SCRATCH1) == 0x1234ULL) {
  41. dev_err(&oct->pci_dev->dev, "Soft reset failed\n");
  42. return 1;
  43. }
  44. dev_dbg(&oct->pci_dev->dev, "Reset completed\n");
  45. octeon_write_csr64(oct, CN6XXX_WIN_WR_MASK_REG, 0xFF);
  46. return 0;
  47. }
  48. void lio_cn6xxx_enable_error_reporting(struct octeon_device *oct)
  49. {
  50. u32 val;
  51. pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
  52. if (val & 0x000c0000) {
  53. dev_err(&oct->pci_dev->dev, "PCI-E Link error detected: 0x%08x\n",
  54. val & 0x000c0000);
  55. }
  56. val |= 0xf; /* Enable Link error reporting */
  57. dev_dbg(&oct->pci_dev->dev, "Enabling PCI-E error reporting..\n");
  58. pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
  59. }
  60. void lio_cn6xxx_setup_pcie_mps(struct octeon_device *oct,
  61. enum octeon_pcie_mps mps)
  62. {
  63. u32 val;
  64. u64 r64;
  65. /* Read config register for MPS */
  66. pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
  67. if (mps == PCIE_MPS_DEFAULT) {
  68. mps = ((val & (0x7 << 5)) >> 5);
  69. } else {
  70. val &= ~(0x7 << 5); /* Turn off any MPS bits */
  71. val |= (mps << 5); /* Set MPS */
  72. pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
  73. }
  74. /* Set MPS in DPI_SLI_PRT0_CFG to the same value. */
  75. r64 = lio_pci_readq(oct, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
  76. r64 |= (mps << 4);
  77. lio_pci_writeq(oct, r64, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
  78. }
  79. void lio_cn6xxx_setup_pcie_mrrs(struct octeon_device *oct,
  80. enum octeon_pcie_mrrs mrrs)
  81. {
  82. u32 val;
  83. u64 r64;
  84. /* Read config register for MRRS */
  85. pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
  86. if (mrrs == PCIE_MRRS_DEFAULT) {
  87. mrrs = ((val & (0x7 << 12)) >> 12);
  88. } else {
  89. val &= ~(0x7 << 12); /* Turn off any MRRS bits */
  90. val |= (mrrs << 12); /* Set MRRS */
  91. pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
  92. }
  93. /* Set MRRS in SLI_S2M_PORT0_CTL to the same value. */
  94. r64 = octeon_read_csr64(oct, CN6XXX_SLI_S2M_PORTX_CTL(oct->pcie_port));
  95. r64 |= mrrs;
  96. octeon_write_csr64(oct, CN6XXX_SLI_S2M_PORTX_CTL(oct->pcie_port), r64);
  97. /* Set MRRS in DPI_SLI_PRT0_CFG to the same value. */
  98. r64 = lio_pci_readq(oct, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
  99. r64 |= mrrs;
  100. lio_pci_writeq(oct, r64, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
  101. }
  102. u32 lio_cn6xxx_coprocessor_clock(struct octeon_device *oct)
  103. {
  104. /* Bits 29:24 of MIO_RST_BOOT holds the ref. clock multiplier
  105. * for SLI.
  106. */
  107. return ((lio_pci_readq(oct, CN6XXX_MIO_RST_BOOT) >> 24) & 0x3f) * 50;
  108. }
  109. u32 lio_cn6xxx_get_oq_ticks(struct octeon_device *oct,
  110. u32 time_intr_in_us)
  111. {
  112. /* This gives the SLI clock per microsec */
  113. u32 oqticks_per_us = lio_cn6xxx_coprocessor_clock(oct);
  114. /* core clock per us / oq ticks will be fractional. TO avoid that
  115. * we use the method below.
  116. */
  117. /* This gives the clock cycles per millisecond */
  118. oqticks_per_us *= 1000;
  119. /* This gives the oq ticks (1024 core clock cycles) per millisecond */
  120. oqticks_per_us /= 1024;
  121. /* time_intr is in microseconds. The next 2 steps gives the oq ticks
  122. * corressponding to time_intr.
  123. */
  124. oqticks_per_us *= time_intr_in_us;
  125. oqticks_per_us /= 1000;
  126. return oqticks_per_us;
  127. }
  128. void lio_cn6xxx_setup_global_input_regs(struct octeon_device *oct)
  129. {
  130. /* Select Round-Robin Arb, ES, RO, NS for Input Queues */
  131. octeon_write_csr(oct, CN6XXX_SLI_PKT_INPUT_CONTROL,
  132. CN6XXX_INPUT_CTL_MASK);
  133. /* Instruction Read Size - Max 4 instructions per PCIE Read */
  134. octeon_write_csr64(oct, CN6XXX_SLI_PKT_INSTR_RD_SIZE,
  135. 0xFFFFFFFFFFFFFFFFULL);
  136. /* Select PCIE Port for all Input rings. */
  137. octeon_write_csr64(oct, CN6XXX_SLI_IN_PCIE_PORT,
  138. (oct->pcie_port * 0x5555555555555555ULL));
  139. }
  140. static void lio_cn66xx_setup_pkt_ctl_regs(struct octeon_device *oct)
  141. {
  142. u64 pktctl;
  143. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  144. pktctl = octeon_read_csr64(oct, CN6XXX_SLI_PKT_CTL);
  145. /* 66XX SPECIFIC */
  146. if (CFG_GET_OQ_MAX_Q(cn6xxx->conf) <= 4)
  147. /* Disable RING_EN if only upto 4 rings are used. */
  148. pktctl &= ~(1 << 4);
  149. else
  150. pktctl |= (1 << 4);
  151. if (CFG_GET_IS_SLI_BP_ON(cn6xxx->conf))
  152. pktctl |= 0xF;
  153. else
  154. /* Disable per-port backpressure. */
  155. pktctl &= ~0xF;
  156. octeon_write_csr64(oct, CN6XXX_SLI_PKT_CTL, pktctl);
  157. }
  158. void lio_cn6xxx_setup_global_output_regs(struct octeon_device *oct)
  159. {
  160. u32 time_threshold;
  161. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  162. /* / Select PCI-E Port for all Output queues */
  163. octeon_write_csr64(oct, CN6XXX_SLI_PKT_PCIE_PORT64,
  164. (oct->pcie_port * 0x5555555555555555ULL));
  165. if (CFG_GET_IS_SLI_BP_ON(cn6xxx->conf)) {
  166. octeon_write_csr64(oct, CN6XXX_SLI_OQ_WMARK, 32);
  167. } else {
  168. /* / Set Output queue watermark to 0 to disable backpressure */
  169. octeon_write_csr64(oct, CN6XXX_SLI_OQ_WMARK, 0);
  170. }
  171. /* / Select Info Ptr for length & data */
  172. octeon_write_csr(oct, CN6XXX_SLI_PKT_IPTR, 0xFFFFFFFF);
  173. /* / Select Packet count instead of bytes for SLI_PKTi_CNTS[CNT] */
  174. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_BMODE, 0);
  175. /* Select ES, RO, NS setting from register for Output Queue Packet
  176. * Address
  177. */
  178. octeon_write_csr(oct, CN6XXX_SLI_PKT_DPADDR, 0xFFFFFFFF);
  179. /* No Relaxed Ordering, No Snoop, 64-bit swap for Output
  180. * Queue ScatterList
  181. */
  182. octeon_write_csr(oct, CN6XXX_SLI_PKT_SLIST_ROR, 0);
  183. octeon_write_csr(oct, CN6XXX_SLI_PKT_SLIST_NS, 0);
  184. /* / ENDIAN_SPECIFIC CHANGES - 0 works for LE. */
  185. #ifdef __BIG_ENDIAN_BITFIELD
  186. octeon_write_csr64(oct, CN6XXX_SLI_PKT_SLIST_ES64,
  187. 0x5555555555555555ULL);
  188. #else
  189. octeon_write_csr64(oct, CN6XXX_SLI_PKT_SLIST_ES64, 0ULL);
  190. #endif
  191. /* / No Relaxed Ordering, No Snoop, 64-bit swap for Output Queue Data */
  192. octeon_write_csr(oct, CN6XXX_SLI_PKT_DATA_OUT_ROR, 0);
  193. octeon_write_csr(oct, CN6XXX_SLI_PKT_DATA_OUT_NS, 0);
  194. octeon_write_csr64(oct, CN6XXX_SLI_PKT_DATA_OUT_ES64,
  195. 0x5555555555555555ULL);
  196. /* / Set up interrupt packet and time threshold */
  197. octeon_write_csr(oct, CN6XXX_SLI_OQ_INT_LEVEL_PKTS,
  198. (u32)CFG_GET_OQ_INTR_PKT(cn6xxx->conf));
  199. time_threshold =
  200. lio_cn6xxx_get_oq_ticks(oct, (u32)
  201. CFG_GET_OQ_INTR_TIME(cn6xxx->conf));
  202. octeon_write_csr(oct, CN6XXX_SLI_OQ_INT_LEVEL_TIME, time_threshold);
  203. }
  204. static int lio_cn6xxx_setup_device_regs(struct octeon_device *oct)
  205. {
  206. lio_cn6xxx_setup_pcie_mps(oct, PCIE_MPS_DEFAULT);
  207. lio_cn6xxx_setup_pcie_mrrs(oct, PCIE_MRRS_512B);
  208. lio_cn6xxx_enable_error_reporting(oct);
  209. lio_cn6xxx_setup_global_input_regs(oct);
  210. lio_cn66xx_setup_pkt_ctl_regs(oct);
  211. lio_cn6xxx_setup_global_output_regs(oct);
  212. /* Default error timeout value should be 0x200000 to avoid host hang
  213. * when reads invalid register
  214. */
  215. octeon_write_csr64(oct, CN6XXX_SLI_WINDOW_CTL, 0x200000ULL);
  216. return 0;
  217. }
  218. void lio_cn6xxx_setup_iq_regs(struct octeon_device *oct, u32 iq_no)
  219. {
  220. struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
  221. octeon_write_csr64(oct, CN6XXX_SLI_IQ_PKT_INSTR_HDR64(iq_no), 0);
  222. /* Write the start of the input queue's ring and its size */
  223. octeon_write_csr64(oct, CN6XXX_SLI_IQ_BASE_ADDR64(iq_no),
  224. iq->base_addr_dma);
  225. octeon_write_csr(oct, CN6XXX_SLI_IQ_SIZE(iq_no), iq->max_count);
  226. /* Remember the doorbell & instruction count register addr for this
  227. * queue
  228. */
  229. iq->doorbell_reg = oct->mmio[0].hw_addr + CN6XXX_SLI_IQ_DOORBELL(iq_no);
  230. iq->inst_cnt_reg = oct->mmio[0].hw_addr
  231. + CN6XXX_SLI_IQ_INSTR_COUNT(iq_no);
  232. dev_dbg(&oct->pci_dev->dev, "InstQ[%d]:dbell reg @ 0x%p instcnt_reg @ 0x%p\n",
  233. iq_no, iq->doorbell_reg, iq->inst_cnt_reg);
  234. /* Store the current instruction counter
  235. * (used in flush_iq calculation)
  236. */
  237. iq->reset_instr_cnt = readl(iq->inst_cnt_reg);
  238. }
  239. static void lio_cn66xx_setup_iq_regs(struct octeon_device *oct, u32 iq_no)
  240. {
  241. lio_cn6xxx_setup_iq_regs(oct, iq_no);
  242. /* Backpressure for this queue - WMARK set to all F's. This effectively
  243. * disables the backpressure mechanism.
  244. */
  245. octeon_write_csr64(oct, CN66XX_SLI_IQ_BP64(iq_no),
  246. (0xFFFFFFFFULL << 32));
  247. }
  248. void lio_cn6xxx_setup_oq_regs(struct octeon_device *oct, u32 oq_no)
  249. {
  250. u32 intr;
  251. struct octeon_droq *droq = oct->droq[oq_no];
  252. octeon_write_csr64(oct, CN6XXX_SLI_OQ_BASE_ADDR64(oq_no),
  253. droq->desc_ring_dma);
  254. octeon_write_csr(oct, CN6XXX_SLI_OQ_SIZE(oq_no), droq->max_count);
  255. octeon_write_csr(oct, CN6XXX_SLI_OQ_BUFF_INFO_SIZE(oq_no),
  256. (droq->buffer_size | (OCT_RH_SIZE << 16)));
  257. /* Get the mapped address of the pkt_sent and pkts_credit regs */
  258. droq->pkts_sent_reg =
  259. oct->mmio[0].hw_addr + CN6XXX_SLI_OQ_PKTS_SENT(oq_no);
  260. droq->pkts_credit_reg =
  261. oct->mmio[0].hw_addr + CN6XXX_SLI_OQ_PKTS_CREDIT(oq_no);
  262. /* Enable this output queue to generate Packet Timer Interrupt */
  263. intr = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB);
  264. intr |= (1 << oq_no);
  265. octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB, intr);
  266. /* Enable this output queue to generate Packet Timer Interrupt */
  267. intr = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB);
  268. intr |= (1 << oq_no);
  269. octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB, intr);
  270. }
  271. int lio_cn6xxx_enable_io_queues(struct octeon_device *oct)
  272. {
  273. u32 mask;
  274. mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_SIZE);
  275. mask |= oct->io_qmask.iq64B;
  276. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_SIZE, mask);
  277. mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB);
  278. mask |= oct->io_qmask.iq;
  279. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, mask);
  280. mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
  281. mask |= oct->io_qmask.oq;
  282. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, mask);
  283. return 0;
  284. }
  285. void lio_cn6xxx_disable_io_queues(struct octeon_device *oct)
  286. {
  287. int i;
  288. u32 mask, loop = HZ;
  289. u32 d32;
  290. /* Reset the Enable bits for Input Queues. */
  291. mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB);
  292. mask ^= oct->io_qmask.iq;
  293. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, mask);
  294. /* Wait until hardware indicates that the queues are out of reset. */
  295. mask = (u32)oct->io_qmask.iq;
  296. d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_IQ);
  297. while (((d32 & mask) != mask) && loop--) {
  298. d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_IQ);
  299. schedule_timeout_uninterruptible(1);
  300. }
  301. /* Reset the doorbell register for each Input queue. */
  302. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  303. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  304. continue;
  305. octeon_write_csr(oct, CN6XXX_SLI_IQ_DOORBELL(i), 0xFFFFFFFF);
  306. d32 = octeon_read_csr(oct, CN6XXX_SLI_IQ_DOORBELL(i));
  307. }
  308. /* Reset the Enable bits for Output Queues. */
  309. mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
  310. mask ^= oct->io_qmask.oq;
  311. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, mask);
  312. /* Wait until hardware indicates that the queues are out of reset. */
  313. loop = HZ;
  314. mask = (u32)oct->io_qmask.oq;
  315. d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_OQ);
  316. while (((d32 & mask) != mask) && loop--) {
  317. d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_OQ);
  318. schedule_timeout_uninterruptible(1);
  319. }
  320. ;
  321. /* Reset the doorbell register for each Output queue. */
  322. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  323. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  324. continue;
  325. octeon_write_csr(oct, CN6XXX_SLI_OQ_PKTS_CREDIT(i), 0xFFFFFFFF);
  326. d32 = octeon_read_csr(oct, CN6XXX_SLI_OQ_PKTS_CREDIT(i));
  327. d32 = octeon_read_csr(oct, CN6XXX_SLI_OQ_PKTS_SENT(i));
  328. octeon_write_csr(oct, CN6XXX_SLI_OQ_PKTS_SENT(i), d32);
  329. }
  330. d32 = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT);
  331. if (d32)
  332. octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT, d32);
  333. d32 = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT);
  334. if (d32)
  335. octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT, d32);
  336. }
  337. void
  338. lio_cn6xxx_bar1_idx_setup(struct octeon_device *oct,
  339. u64 core_addr,
  340. u32 idx,
  341. int valid)
  342. {
  343. u64 bar1;
  344. if (valid == 0) {
  345. bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
  346. lio_pci_writeq(oct, (bar1 & 0xFFFFFFFEULL),
  347. CN6XXX_BAR1_REG(idx, oct->pcie_port));
  348. bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
  349. return;
  350. }
  351. /* Bits 17:4 of the PCI_BAR1_INDEXx stores bits 35:22 of
  352. * the Core Addr
  353. */
  354. lio_pci_writeq(oct, (((core_addr >> 22) << 4) | PCI_BAR1_MASK),
  355. CN6XXX_BAR1_REG(idx, oct->pcie_port));
  356. bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
  357. }
  358. void lio_cn6xxx_bar1_idx_write(struct octeon_device *oct,
  359. u32 idx,
  360. u32 mask)
  361. {
  362. lio_pci_writeq(oct, mask, CN6XXX_BAR1_REG(idx, oct->pcie_port));
  363. }
  364. u32 lio_cn6xxx_bar1_idx_read(struct octeon_device *oct, u32 idx)
  365. {
  366. return (u32)lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
  367. }
  368. u32
  369. lio_cn6xxx_update_read_index(struct octeon_instr_queue *iq)
  370. {
  371. u32 new_idx = readl(iq->inst_cnt_reg);
  372. /* The new instr cnt reg is a 32-bit counter that can roll over. We have
  373. * noted the counter's initial value at init time into
  374. * reset_instr_cnt
  375. */
  376. if (iq->reset_instr_cnt < new_idx)
  377. new_idx -= iq->reset_instr_cnt;
  378. else
  379. new_idx += (0xffffffff - iq->reset_instr_cnt) + 1;
  380. /* Modulo of the new index with the IQ size will give us
  381. * the new index.
  382. */
  383. new_idx %= iq->max_count;
  384. return new_idx;
  385. }
  386. void lio_cn6xxx_enable_interrupt(struct octeon_device *oct,
  387. u8 unused __attribute__((unused)))
  388. {
  389. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  390. u64 mask = cn6xxx->intr_mask64 | CN6XXX_INTR_DMA0_FORCE;
  391. /* Enable Interrupt */
  392. writeq(mask, cn6xxx->intr_enb_reg64);
  393. }
  394. void lio_cn6xxx_disable_interrupt(struct octeon_device *oct,
  395. u8 unused __attribute__((unused)))
  396. {
  397. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  398. /* Disable Interrupts */
  399. writeq(0, cn6xxx->intr_enb_reg64);
  400. /* make sure interrupts are really disabled */
  401. mmiowb();
  402. }
  403. static void lio_cn6xxx_get_pcie_qlmport(struct octeon_device *oct)
  404. {
  405. /* CN63xx Pass2 and newer parts implements the SLI_MAC_NUMBER register
  406. * to determine the PCIE port #
  407. */
  408. oct->pcie_port = octeon_read_csr(oct, CN6XXX_SLI_MAC_NUMBER) & 0xff;
  409. dev_dbg(&oct->pci_dev->dev, "Using PCIE Port %d\n", oct->pcie_port);
  410. }
  411. static void
  412. lio_cn6xxx_process_pcie_error_intr(struct octeon_device *oct, u64 intr64)
  413. {
  414. dev_err(&oct->pci_dev->dev, "Error Intr: 0x%016llx\n",
  415. CVM_CAST64(intr64));
  416. }
  417. static int lio_cn6xxx_process_droq_intr_regs(struct octeon_device *oct)
  418. {
  419. struct octeon_droq *droq;
  420. int oq_no;
  421. u32 pkt_count, droq_time_mask, droq_mask, droq_int_enb;
  422. u32 droq_cnt_enb, droq_cnt_mask;
  423. droq_cnt_enb = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB);
  424. droq_cnt_mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT);
  425. droq_mask = droq_cnt_mask & droq_cnt_enb;
  426. droq_time_mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT);
  427. droq_int_enb = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB);
  428. droq_mask |= (droq_time_mask & droq_int_enb);
  429. droq_mask &= oct->io_qmask.oq;
  430. oct->droq_intr = 0;
  431. for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct); oq_no++) {
  432. if (!(droq_mask & BIT_ULL(oq_no)))
  433. continue;
  434. droq = oct->droq[oq_no];
  435. pkt_count = octeon_droq_check_hw_for_pkts(droq);
  436. if (pkt_count) {
  437. oct->droq_intr |= BIT_ULL(oq_no);
  438. if (droq->ops.poll_mode) {
  439. u32 value;
  440. u32 reg;
  441. struct octeon_cn6xxx *cn6xxx =
  442. (struct octeon_cn6xxx *)oct->chip;
  443. /* disable interrupts for this droq */
  444. spin_lock
  445. (&cn6xxx->lock_for_droq_int_enb_reg);
  446. reg = CN6XXX_SLI_PKT_TIME_INT_ENB;
  447. value = octeon_read_csr(oct, reg);
  448. value &= ~(1 << oq_no);
  449. octeon_write_csr(oct, reg, value);
  450. reg = CN6XXX_SLI_PKT_CNT_INT_ENB;
  451. value = octeon_read_csr(oct, reg);
  452. value &= ~(1 << oq_no);
  453. octeon_write_csr(oct, reg, value);
  454. /* Ensure that the enable register is written.
  455. */
  456. mmiowb();
  457. spin_unlock(&cn6xxx->lock_for_droq_int_enb_reg);
  458. }
  459. }
  460. }
  461. droq_time_mask &= oct->io_qmask.oq;
  462. droq_cnt_mask &= oct->io_qmask.oq;
  463. /* Reset the PKT_CNT/TIME_INT registers. */
  464. if (droq_time_mask)
  465. octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT, droq_time_mask);
  466. if (droq_cnt_mask) /* reset PKT_CNT register:66xx */
  467. octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT, droq_cnt_mask);
  468. return 0;
  469. }
  470. irqreturn_t lio_cn6xxx_process_interrupt_regs(void *dev)
  471. {
  472. struct octeon_device *oct = (struct octeon_device *)dev;
  473. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  474. u64 intr64;
  475. intr64 = readq(cn6xxx->intr_sum_reg64);
  476. /* If our device has interrupted, then proceed.
  477. * Also check for all f's if interrupt was triggered on an error
  478. * and the PCI read fails.
  479. */
  480. if (!intr64 || (intr64 == 0xFFFFFFFFFFFFFFFFULL))
  481. return IRQ_NONE;
  482. oct->int_status = 0;
  483. if (intr64 & CN6XXX_INTR_ERR)
  484. lio_cn6xxx_process_pcie_error_intr(oct, intr64);
  485. if (intr64 & CN6XXX_INTR_PKT_DATA) {
  486. lio_cn6xxx_process_droq_intr_regs(oct);
  487. oct->int_status |= OCT_DEV_INTR_PKT_DATA;
  488. }
  489. if (intr64 & CN6XXX_INTR_DMA0_FORCE)
  490. oct->int_status |= OCT_DEV_INTR_DMA0_FORCE;
  491. if (intr64 & CN6XXX_INTR_DMA1_FORCE)
  492. oct->int_status |= OCT_DEV_INTR_DMA1_FORCE;
  493. /* Clear the current interrupts */
  494. writeq(intr64, cn6xxx->intr_sum_reg64);
  495. return IRQ_HANDLED;
  496. }
  497. void lio_cn6xxx_setup_reg_address(struct octeon_device *oct,
  498. void *chip,
  499. struct octeon_reg_list *reg_list)
  500. {
  501. u8 __iomem *bar0_pciaddr = oct->mmio[0].hw_addr;
  502. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)chip;
  503. reg_list->pci_win_wr_addr_hi =
  504. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR_HI);
  505. reg_list->pci_win_wr_addr_lo =
  506. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR_LO);
  507. reg_list->pci_win_wr_addr =
  508. (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR64);
  509. reg_list->pci_win_rd_addr_hi =
  510. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR_HI);
  511. reg_list->pci_win_rd_addr_lo =
  512. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR_LO);
  513. reg_list->pci_win_rd_addr =
  514. (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR64);
  515. reg_list->pci_win_wr_data_hi =
  516. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA_HI);
  517. reg_list->pci_win_wr_data_lo =
  518. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA_LO);
  519. reg_list->pci_win_wr_data =
  520. (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA64);
  521. reg_list->pci_win_rd_data_hi =
  522. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA_HI);
  523. reg_list->pci_win_rd_data_lo =
  524. (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA_LO);
  525. reg_list->pci_win_rd_data =
  526. (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA64);
  527. lio_cn6xxx_get_pcie_qlmport(oct);
  528. cn6xxx->intr_sum_reg64 = bar0_pciaddr + CN6XXX_SLI_INT_SUM64;
  529. cn6xxx->intr_mask64 = CN6XXX_INTR_MASK;
  530. cn6xxx->intr_enb_reg64 =
  531. bar0_pciaddr + CN6XXX_SLI_INT_ENB64(oct->pcie_port);
  532. }
  533. int lio_setup_cn66xx_octeon_device(struct octeon_device *oct)
  534. {
  535. struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
  536. if (octeon_map_pci_barx(oct, 0, 0))
  537. return 1;
  538. if (octeon_map_pci_barx(oct, 1, MAX_BAR1_IOREMAP_SIZE)) {
  539. dev_err(&oct->pci_dev->dev, "%s CN66XX BAR1 map failed\n",
  540. __func__);
  541. octeon_unmap_pci_barx(oct, 0);
  542. return 1;
  543. }
  544. spin_lock_init(&cn6xxx->lock_for_droq_int_enb_reg);
  545. oct->fn_list.setup_iq_regs = lio_cn66xx_setup_iq_regs;
  546. oct->fn_list.setup_oq_regs = lio_cn6xxx_setup_oq_regs;
  547. oct->fn_list.soft_reset = lio_cn6xxx_soft_reset;
  548. oct->fn_list.setup_device_regs = lio_cn6xxx_setup_device_regs;
  549. oct->fn_list.update_iq_read_idx = lio_cn6xxx_update_read_index;
  550. oct->fn_list.bar1_idx_setup = lio_cn6xxx_bar1_idx_setup;
  551. oct->fn_list.bar1_idx_write = lio_cn6xxx_bar1_idx_write;
  552. oct->fn_list.bar1_idx_read = lio_cn6xxx_bar1_idx_read;
  553. oct->fn_list.process_interrupt_regs = lio_cn6xxx_process_interrupt_regs;
  554. oct->fn_list.enable_interrupt = lio_cn6xxx_enable_interrupt;
  555. oct->fn_list.disable_interrupt = lio_cn6xxx_disable_interrupt;
  556. oct->fn_list.enable_io_queues = lio_cn6xxx_enable_io_queues;
  557. oct->fn_list.disable_io_queues = lio_cn6xxx_disable_io_queues;
  558. lio_cn6xxx_setup_reg_address(oct, oct->chip, &oct->reg_list);
  559. cn6xxx->conf = (struct octeon_config *)
  560. oct_get_config_info(oct, LIO_210SV);
  561. if (!cn6xxx->conf) {
  562. dev_err(&oct->pci_dev->dev, "%s No Config found for CN66XX\n",
  563. __func__);
  564. octeon_unmap_pci_barx(oct, 0);
  565. octeon_unmap_pci_barx(oct, 1);
  566. return 1;
  567. }
  568. oct->coproc_clock_rate = 1000000ULL * lio_cn6xxx_coprocessor_clock(oct);
  569. return 0;
  570. }
  571. int lio_validate_cn6xxx_config_info(struct octeon_device *oct,
  572. struct octeon_config *conf6xxx)
  573. {
  574. if (CFG_GET_IQ_MAX_Q(conf6xxx) > CN6XXX_MAX_INPUT_QUEUES) {
  575. dev_err(&oct->pci_dev->dev, "%s: Num IQ (%d) exceeds Max (%d)\n",
  576. __func__, CFG_GET_IQ_MAX_Q(conf6xxx),
  577. CN6XXX_MAX_INPUT_QUEUES);
  578. return 1;
  579. }
  580. if (CFG_GET_OQ_MAX_Q(conf6xxx) > CN6XXX_MAX_OUTPUT_QUEUES) {
  581. dev_err(&oct->pci_dev->dev, "%s: Num OQ (%d) exceeds Max (%d)\n",
  582. __func__, CFG_GET_OQ_MAX_Q(conf6xxx),
  583. CN6XXX_MAX_OUTPUT_QUEUES);
  584. return 1;
  585. }
  586. if (CFG_GET_IQ_INSTR_TYPE(conf6xxx) != OCTEON_32BYTE_INSTR &&
  587. CFG_GET_IQ_INSTR_TYPE(conf6xxx) != OCTEON_64BYTE_INSTR) {
  588. dev_err(&oct->pci_dev->dev, "%s: Invalid instr type for IQ\n",
  589. __func__);
  590. return 1;
  591. }
  592. if (!(CFG_GET_OQ_INFO_PTR(conf6xxx)) ||
  593. !(CFG_GET_OQ_REFILL_THRESHOLD(conf6xxx))) {
  594. dev_err(&oct->pci_dev->dev, "%s: Invalid parameter for OQ\n",
  595. __func__);
  596. return 1;
  597. }
  598. if (!(CFG_GET_OQ_INTR_TIME(conf6xxx))) {
  599. dev_err(&oct->pci_dev->dev, "%s: No Time Interrupt for OQ\n",
  600. __func__);
  601. return 1;
  602. }
  603. return 0;
  604. }