cn23xx_vf_device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 <linux/vmalloc.h>
  21. #include "liquidio_common.h"
  22. #include "octeon_droq.h"
  23. #include "octeon_iq.h"
  24. #include "response_manager.h"
  25. #include "octeon_device.h"
  26. #include "cn23xx_vf_device.h"
  27. #include "octeon_main.h"
  28. #include "octeon_mailbox.h"
  29. u32 cn23xx_vf_get_oq_ticks(struct octeon_device *oct, u32 time_intr_in_us)
  30. {
  31. /* This gives the SLI clock per microsec */
  32. u32 oqticks_per_us = (u32)oct->pfvf_hsword.coproc_tics_per_us;
  33. /* This gives the clock cycles per millisecond */
  34. oqticks_per_us *= 1000;
  35. /* This gives the oq ticks (1024 core clock cycles) per millisecond */
  36. oqticks_per_us /= 1024;
  37. /* time_intr is in microseconds. The next 2 steps gives the oq ticks
  38. * corressponding to time_intr.
  39. */
  40. oqticks_per_us *= time_intr_in_us;
  41. oqticks_per_us /= 1000;
  42. return oqticks_per_us;
  43. }
  44. static int cn23xx_vf_reset_io_queues(struct octeon_device *oct, u32 num_queues)
  45. {
  46. u32 loop = BUSY_READING_REG_VF_LOOP_COUNT;
  47. int ret_val = 0;
  48. u32 q_no;
  49. u64 d64;
  50. for (q_no = 0; q_no < num_queues; q_no++) {
  51. /* set RST bit to 1. This bit applies to both IQ and OQ */
  52. d64 = octeon_read_csr64(oct,
  53. CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  54. d64 |= CN23XX_PKT_INPUT_CTL_RST;
  55. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no),
  56. d64);
  57. }
  58. /* wait until the RST bit is clear or the RST and QUIET bits are set */
  59. for (q_no = 0; q_no < num_queues; q_no++) {
  60. u64 reg_val = octeon_read_csr64(oct,
  61. CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  62. while ((READ_ONCE(reg_val) & CN23XX_PKT_INPUT_CTL_RST) &&
  63. !(READ_ONCE(reg_val) & CN23XX_PKT_INPUT_CTL_QUIET) &&
  64. loop) {
  65. WRITE_ONCE(reg_val, octeon_read_csr64(
  66. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no)));
  67. loop--;
  68. }
  69. if (!loop) {
  70. dev_err(&oct->pci_dev->dev,
  71. "clearing the reset reg failed or setting the quiet reg failed for qno: %u\n",
  72. q_no);
  73. return -1;
  74. }
  75. WRITE_ONCE(reg_val, READ_ONCE(reg_val) &
  76. ~CN23XX_PKT_INPUT_CTL_RST);
  77. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no),
  78. READ_ONCE(reg_val));
  79. WRITE_ONCE(reg_val, octeon_read_csr64(
  80. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no)));
  81. if (READ_ONCE(reg_val) & CN23XX_PKT_INPUT_CTL_RST) {
  82. dev_err(&oct->pci_dev->dev,
  83. "clearing the reset failed for qno: %u\n",
  84. q_no);
  85. ret_val = -1;
  86. }
  87. }
  88. return ret_val;
  89. }
  90. static int cn23xx_vf_setup_global_input_regs(struct octeon_device *oct)
  91. {
  92. struct octeon_cn23xx_vf *cn23xx = (struct octeon_cn23xx_vf *)oct->chip;
  93. struct octeon_instr_queue *iq;
  94. u64 q_no, intr_threshold;
  95. u64 d64;
  96. if (cn23xx_vf_reset_io_queues(oct, oct->sriov_info.rings_per_vf))
  97. return -1;
  98. for (q_no = 0; q_no < (oct->sriov_info.rings_per_vf); q_no++) {
  99. void __iomem *inst_cnt_reg;
  100. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_DOORBELL(q_no),
  101. 0xFFFFFFFF);
  102. iq = oct->instr_queue[q_no];
  103. if (iq)
  104. inst_cnt_reg = iq->inst_cnt_reg;
  105. else
  106. inst_cnt_reg = (u8 *)oct->mmio[0].hw_addr +
  107. CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no);
  108. d64 = octeon_read_csr64(oct,
  109. CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no));
  110. d64 &= 0xEFFFFFFFFFFFFFFFL;
  111. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no),
  112. d64);
  113. /* Select ES, RO, NS, RDSIZE,DPTR Fomat#0 for
  114. * the Input Queues
  115. */
  116. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no),
  117. CN23XX_PKT_INPUT_CTL_MASK);
  118. /* set the wmark level to trigger PI_INT */
  119. intr_threshold = CFG_GET_IQ_INTR_PKT(cn23xx->conf) &
  120. CN23XX_PKT_IN_DONE_WMARK_MASK;
  121. writeq((readq(inst_cnt_reg) &
  122. ~(CN23XX_PKT_IN_DONE_WMARK_MASK <<
  123. CN23XX_PKT_IN_DONE_WMARK_BIT_POS)) |
  124. (intr_threshold << CN23XX_PKT_IN_DONE_WMARK_BIT_POS),
  125. inst_cnt_reg);
  126. }
  127. return 0;
  128. }
  129. static void cn23xx_vf_setup_global_output_regs(struct octeon_device *oct)
  130. {
  131. u32 reg_val;
  132. u32 q_no;
  133. for (q_no = 0; q_no < (oct->sriov_info.rings_per_vf); q_no++) {
  134. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_PKTS_CREDIT(q_no),
  135. 0xFFFFFFFF);
  136. reg_val =
  137. octeon_read_csr(oct, CN23XX_VF_SLI_OQ_PKTS_SENT(q_no));
  138. reg_val &= 0xEFFFFFFFFFFFFFFFL;
  139. reg_val =
  140. octeon_read_csr(oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no));
  141. /* set DPTR */
  142. reg_val |= CN23XX_PKT_OUTPUT_CTL_DPTR;
  143. /* reset BMODE */
  144. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_BMODE);
  145. /* No Relaxed Ordering, No Snoop, 64-bit Byte swap
  146. * for Output Queue ScatterList reset ROR_P, NSR_P
  147. */
  148. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ROR_P);
  149. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_NSR_P);
  150. #ifdef __LITTLE_ENDIAN_BITFIELD
  151. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ES_P);
  152. #else
  153. reg_val |= (CN23XX_PKT_OUTPUT_CTL_ES_P);
  154. #endif
  155. /* No Relaxed Ordering, No Snoop, 64-bit Byte swap
  156. * for Output Queue Data reset ROR, NSR
  157. */
  158. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ROR);
  159. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_NSR);
  160. /* set the ES bit */
  161. reg_val |= (CN23XX_PKT_OUTPUT_CTL_ES);
  162. /* write all the selected settings */
  163. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no),
  164. reg_val);
  165. }
  166. }
  167. static int cn23xx_setup_vf_device_regs(struct octeon_device *oct)
  168. {
  169. if (cn23xx_vf_setup_global_input_regs(oct))
  170. return -1;
  171. cn23xx_vf_setup_global_output_regs(oct);
  172. return 0;
  173. }
  174. static void cn23xx_setup_vf_iq_regs(struct octeon_device *oct, u32 iq_no)
  175. {
  176. struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
  177. u64 pkt_in_done;
  178. /* Write the start of the input queue's ring and its size */
  179. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_BASE_ADDR64(iq_no),
  180. iq->base_addr_dma);
  181. octeon_write_csr(oct, CN23XX_VF_SLI_IQ_SIZE(iq_no), iq->max_count);
  182. /* Remember the doorbell & instruction count register addr
  183. * for this queue
  184. */
  185. iq->doorbell_reg =
  186. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_IQ_DOORBELL(iq_no);
  187. iq->inst_cnt_reg =
  188. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_IQ_INSTR_COUNT64(iq_no);
  189. dev_dbg(&oct->pci_dev->dev, "InstQ[%d]:dbell reg @ 0x%p instcnt_reg @ 0x%p\n",
  190. iq_no, iq->doorbell_reg, iq->inst_cnt_reg);
  191. /* Store the current instruction counter (used in flush_iq
  192. * calculation)
  193. */
  194. pkt_in_done = readq(iq->inst_cnt_reg);
  195. if (oct->msix_on) {
  196. /* Set CINT_ENB to enable IQ interrupt */
  197. writeq((pkt_in_done | CN23XX_INTR_CINT_ENB),
  198. iq->inst_cnt_reg);
  199. }
  200. iq->reset_instr_cnt = 0;
  201. }
  202. static void cn23xx_setup_vf_oq_regs(struct octeon_device *oct, u32 oq_no)
  203. {
  204. struct octeon_droq *droq = oct->droq[oq_no];
  205. octeon_write_csr64(oct, CN23XX_VF_SLI_OQ_BASE_ADDR64(oq_no),
  206. droq->desc_ring_dma);
  207. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_SIZE(oq_no), droq->max_count);
  208. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_BUFF_INFO_SIZE(oq_no),
  209. droq->buffer_size);
  210. /* Get the mapped address of the pkt_sent and pkts_credit regs */
  211. droq->pkts_sent_reg =
  212. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_OQ_PKTS_SENT(oq_no);
  213. droq->pkts_credit_reg =
  214. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_OQ_PKTS_CREDIT(oq_no);
  215. }
  216. static void cn23xx_vf_mbox_thread(struct work_struct *work)
  217. {
  218. struct cavium_wk *wk = (struct cavium_wk *)work;
  219. struct octeon_mbox *mbox = (struct octeon_mbox *)wk->ctxptr;
  220. octeon_mbox_process_message(mbox);
  221. }
  222. static int cn23xx_free_vf_mbox(struct octeon_device *oct)
  223. {
  224. cancel_delayed_work_sync(&oct->mbox[0]->mbox_poll_wk.work);
  225. vfree(oct->mbox[0]);
  226. return 0;
  227. }
  228. static int cn23xx_setup_vf_mbox(struct octeon_device *oct)
  229. {
  230. struct octeon_mbox *mbox = NULL;
  231. mbox = vmalloc(sizeof(*mbox));
  232. if (!mbox)
  233. return 1;
  234. memset(mbox, 0, sizeof(struct octeon_mbox));
  235. spin_lock_init(&mbox->lock);
  236. mbox->oct_dev = oct;
  237. mbox->q_no = 0;
  238. mbox->state = OCTEON_MBOX_STATE_IDLE;
  239. /* VF mbox interrupt reg */
  240. mbox->mbox_int_reg =
  241. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_PKT_MBOX_INT(0);
  242. /* VF reads from SIG0 reg */
  243. mbox->mbox_read_reg =
  244. (u8 *)oct->mmio[0].hw_addr + CN23XX_SLI_PKT_PF_VF_MBOX_SIG(0, 0);
  245. /* VF writes into SIG1 reg */
  246. mbox->mbox_write_reg =
  247. (u8 *)oct->mmio[0].hw_addr + CN23XX_SLI_PKT_PF_VF_MBOX_SIG(0, 1);
  248. INIT_DELAYED_WORK(&mbox->mbox_poll_wk.work,
  249. cn23xx_vf_mbox_thread);
  250. mbox->mbox_poll_wk.ctxptr = mbox;
  251. oct->mbox[0] = mbox;
  252. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  253. return 0;
  254. }
  255. static int cn23xx_enable_vf_io_queues(struct octeon_device *oct)
  256. {
  257. u32 q_no;
  258. for (q_no = 0; q_no < oct->num_iqs; q_no++) {
  259. u64 reg_val;
  260. /* set the corresponding IQ IS_64B bit */
  261. if (oct->io_qmask.iq64B & BIT_ULL(q_no)) {
  262. reg_val = octeon_read_csr64(
  263. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  264. reg_val |= CN23XX_PKT_INPUT_CTL_IS_64B;
  265. octeon_write_csr64(
  266. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no), reg_val);
  267. }
  268. /* set the corresponding IQ ENB bit */
  269. if (oct->io_qmask.iq & BIT_ULL(q_no)) {
  270. reg_val = octeon_read_csr64(
  271. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  272. reg_val |= CN23XX_PKT_INPUT_CTL_RING_ENB;
  273. octeon_write_csr64(
  274. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no), reg_val);
  275. }
  276. }
  277. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  278. u32 reg_val;
  279. /* set the corresponding OQ ENB bit */
  280. if (oct->io_qmask.oq & BIT_ULL(q_no)) {
  281. reg_val = octeon_read_csr(
  282. oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no));
  283. reg_val |= CN23XX_PKT_OUTPUT_CTL_RING_ENB;
  284. octeon_write_csr(
  285. oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no), reg_val);
  286. }
  287. }
  288. return 0;
  289. }
  290. static void cn23xx_disable_vf_io_queues(struct octeon_device *oct)
  291. {
  292. u32 num_queues = oct->num_iqs;
  293. /* per HRM, rings can only be disabled via reset operation,
  294. * NOT via SLI_PKT()_INPUT/OUTPUT_CONTROL[ENB]
  295. */
  296. if (num_queues < oct->num_oqs)
  297. num_queues = oct->num_oqs;
  298. cn23xx_vf_reset_io_queues(oct, num_queues);
  299. }
  300. void cn23xx_vf_ask_pf_to_do_flr(struct octeon_device *oct)
  301. {
  302. struct octeon_mbox_cmd mbox_cmd;
  303. mbox_cmd.msg.u64 = 0;
  304. mbox_cmd.msg.s.type = OCTEON_MBOX_REQUEST;
  305. mbox_cmd.msg.s.resp_needed = 0;
  306. mbox_cmd.msg.s.cmd = OCTEON_VF_FLR_REQUEST;
  307. mbox_cmd.msg.s.len = 1;
  308. mbox_cmd.q_no = 0;
  309. mbox_cmd.recv_len = 0;
  310. mbox_cmd.recv_status = 0;
  311. mbox_cmd.fn = NULL;
  312. mbox_cmd.fn_arg = 0;
  313. octeon_mbox_write(oct, &mbox_cmd);
  314. }
  315. static void octeon_pfvf_hs_callback(struct octeon_device *oct,
  316. struct octeon_mbox_cmd *cmd,
  317. void *arg)
  318. {
  319. u32 major = 0;
  320. memcpy((uint8_t *)&oct->pfvf_hsword, cmd->msg.s.params,
  321. CN23XX_MAILBOX_MSGPARAM_SIZE);
  322. if (cmd->recv_len > 1) {
  323. major = ((struct lio_version *)(cmd->data))->major;
  324. major = major << 16;
  325. }
  326. atomic_set((atomic_t *)arg, major | 1);
  327. }
  328. int cn23xx_octeon_pfvf_handshake(struct octeon_device *oct)
  329. {
  330. struct octeon_mbox_cmd mbox_cmd;
  331. u32 q_no, count = 0;
  332. atomic_t status;
  333. u32 pfmajor;
  334. u32 vfmajor;
  335. u32 ret;
  336. /* Sending VF_ACTIVE indication to the PF driver */
  337. dev_dbg(&oct->pci_dev->dev, "requesting info from pf\n");
  338. mbox_cmd.msg.u64 = 0;
  339. mbox_cmd.msg.s.type = OCTEON_MBOX_REQUEST;
  340. mbox_cmd.msg.s.resp_needed = 1;
  341. mbox_cmd.msg.s.cmd = OCTEON_VF_ACTIVE;
  342. mbox_cmd.msg.s.len = 2;
  343. mbox_cmd.data[0] = 0;
  344. ((struct lio_version *)&mbox_cmd.data[0])->major =
  345. LIQUIDIO_BASE_MAJOR_VERSION;
  346. ((struct lio_version *)&mbox_cmd.data[0])->minor =
  347. LIQUIDIO_BASE_MINOR_VERSION;
  348. ((struct lio_version *)&mbox_cmd.data[0])->micro =
  349. LIQUIDIO_BASE_MICRO_VERSION;
  350. mbox_cmd.q_no = 0;
  351. mbox_cmd.recv_len = 0;
  352. mbox_cmd.recv_status = 0;
  353. mbox_cmd.fn = (octeon_mbox_callback_t)octeon_pfvf_hs_callback;
  354. mbox_cmd.fn_arg = &status;
  355. octeon_mbox_write(oct, &mbox_cmd);
  356. atomic_set(&status, 0);
  357. do {
  358. schedule_timeout_uninterruptible(1);
  359. } while ((!atomic_read(&status)) && (count++ < 100000));
  360. ret = atomic_read(&status);
  361. if (!ret) {
  362. dev_err(&oct->pci_dev->dev, "octeon_pfvf_handshake timeout\n");
  363. return 1;
  364. }
  365. for (q_no = 0 ; q_no < oct->num_iqs ; q_no++)
  366. oct->instr_queue[q_no]->txpciq.s.pkind = oct->pfvf_hsword.pkind;
  367. vfmajor = LIQUIDIO_BASE_MAJOR_VERSION;
  368. pfmajor = ret >> 16;
  369. if (pfmajor != vfmajor) {
  370. dev_err(&oct->pci_dev->dev,
  371. "VF Liquidio driver (major version %d) is not compatible with Liquidio PF driver (major version %d)\n",
  372. vfmajor, pfmajor);
  373. return 1;
  374. }
  375. dev_dbg(&oct->pci_dev->dev,
  376. "VF Liquidio driver (major version %d), Liquidio PF driver (major version %d)\n",
  377. vfmajor, pfmajor);
  378. dev_dbg(&oct->pci_dev->dev, "got data from pf pkind is %d\n",
  379. oct->pfvf_hsword.pkind);
  380. return 0;
  381. }
  382. static void cn23xx_handle_vf_mbox_intr(struct octeon_ioq_vector *ioq_vector)
  383. {
  384. struct octeon_device *oct = ioq_vector->oct_dev;
  385. u64 mbox_int_val;
  386. if (!ioq_vector->droq_index) {
  387. /* read and clear by writing 1 */
  388. mbox_int_val = readq(oct->mbox[0]->mbox_int_reg);
  389. writeq(mbox_int_val, oct->mbox[0]->mbox_int_reg);
  390. if (octeon_mbox_read(oct->mbox[0]))
  391. schedule_delayed_work(&oct->mbox[0]->mbox_poll_wk.work,
  392. msecs_to_jiffies(0));
  393. }
  394. }
  395. static u64 cn23xx_vf_msix_interrupt_handler(void *dev)
  396. {
  397. struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
  398. struct octeon_device *oct = ioq_vector->oct_dev;
  399. struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
  400. u64 pkts_sent;
  401. u64 ret = 0;
  402. dev_dbg(&oct->pci_dev->dev, "In %s octeon_dev @ %p\n", __func__, oct);
  403. pkts_sent = readq(droq->pkts_sent_reg);
  404. /* If our device has interrupted, then proceed. Also check
  405. * for all f's if interrupt was triggered on an error
  406. * and the PCI read fails.
  407. */
  408. if (!pkts_sent || (pkts_sent == 0xFFFFFFFFFFFFFFFFULL))
  409. return ret;
  410. /* Write count reg in sli_pkt_cnts to clear these int. */
  411. if ((pkts_sent & CN23XX_INTR_PO_INT) ||
  412. (pkts_sent & CN23XX_INTR_PI_INT)) {
  413. if (pkts_sent & CN23XX_INTR_PO_INT)
  414. ret |= MSIX_PO_INT;
  415. }
  416. if (pkts_sent & CN23XX_INTR_PI_INT)
  417. /* We will clear the count when we update the read_index. */
  418. ret |= MSIX_PI_INT;
  419. if (pkts_sent & CN23XX_INTR_MBOX_INT) {
  420. cn23xx_handle_vf_mbox_intr(ioq_vector);
  421. ret |= MSIX_MBOX_INT;
  422. }
  423. return ret;
  424. }
  425. static u32 cn23xx_update_read_index(struct octeon_instr_queue *iq)
  426. {
  427. u32 pkt_in_done = readl(iq->inst_cnt_reg);
  428. u32 last_done;
  429. u32 new_idx;
  430. last_done = pkt_in_done - iq->pkt_in_done;
  431. iq->pkt_in_done = pkt_in_done;
  432. /* Modulo of the new index with the IQ size will give us
  433. * the new index. The iq->reset_instr_cnt is always zero for
  434. * cn23xx, so no extra adjustments are needed.
  435. */
  436. new_idx = (iq->octeon_read_index +
  437. (u32)(last_done & CN23XX_PKT_IN_DONE_CNT_MASK)) %
  438. iq->max_count;
  439. return new_idx;
  440. }
  441. static void cn23xx_enable_vf_interrupt(struct octeon_device *oct, u8 intr_flag)
  442. {
  443. struct octeon_cn23xx_vf *cn23xx = (struct octeon_cn23xx_vf *)oct->chip;
  444. u32 q_no, time_threshold;
  445. if (intr_flag & OCTEON_OUTPUT_INTR) {
  446. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  447. /* Set up interrupt packet and time thresholds
  448. * for all the OQs
  449. */
  450. time_threshold = cn23xx_vf_get_oq_ticks(
  451. oct, (u32)CFG_GET_OQ_INTR_TIME(cn23xx->conf));
  452. octeon_write_csr64(
  453. oct, CN23XX_VF_SLI_OQ_PKT_INT_LEVELS(q_no),
  454. (CFG_GET_OQ_INTR_PKT(cn23xx->conf) |
  455. ((u64)time_threshold << 32)));
  456. }
  457. }
  458. if (intr_flag & OCTEON_INPUT_INTR) {
  459. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  460. /* Set CINT_ENB to enable IQ interrupt */
  461. octeon_write_csr64(
  462. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no),
  463. ((octeon_read_csr64(
  464. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no)) &
  465. ~CN23XX_PKT_IN_DONE_CNT_MASK) |
  466. CN23XX_INTR_CINT_ENB));
  467. }
  468. }
  469. /* Set queue-0 MBOX_ENB to enable VF mailbox interrupt */
  470. if (intr_flag & OCTEON_MBOX_INTR) {
  471. octeon_write_csr64(
  472. oct, CN23XX_VF_SLI_PKT_MBOX_INT(0),
  473. (octeon_read_csr64(oct, CN23XX_VF_SLI_PKT_MBOX_INT(0)) |
  474. CN23XX_INTR_MBOX_ENB));
  475. }
  476. }
  477. static void cn23xx_disable_vf_interrupt(struct octeon_device *oct, u8 intr_flag)
  478. {
  479. u32 q_no;
  480. if (intr_flag & OCTEON_OUTPUT_INTR) {
  481. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  482. /* Write all 1's in INT_LEVEL reg to disable PO_INT */
  483. octeon_write_csr64(
  484. oct, CN23XX_VF_SLI_OQ_PKT_INT_LEVELS(q_no),
  485. 0x3fffffffffffff);
  486. }
  487. }
  488. if (intr_flag & OCTEON_INPUT_INTR) {
  489. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  490. octeon_write_csr64(
  491. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no),
  492. (octeon_read_csr64(
  493. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no)) &
  494. ~(CN23XX_INTR_CINT_ENB |
  495. CN23XX_PKT_IN_DONE_CNT_MASK)));
  496. }
  497. }
  498. if (intr_flag & OCTEON_MBOX_INTR) {
  499. octeon_write_csr64(
  500. oct, CN23XX_VF_SLI_PKT_MBOX_INT(0),
  501. (octeon_read_csr64(oct, CN23XX_VF_SLI_PKT_MBOX_INT(0)) &
  502. ~CN23XX_INTR_MBOX_ENB));
  503. }
  504. }
  505. int cn23xx_setup_octeon_vf_device(struct octeon_device *oct)
  506. {
  507. struct octeon_cn23xx_vf *cn23xx = (struct octeon_cn23xx_vf *)oct->chip;
  508. u32 rings_per_vf, ring_flag;
  509. u64 reg_val;
  510. if (octeon_map_pci_barx(oct, 0, 0))
  511. return 1;
  512. /* INPUT_CONTROL[RPVF] gives the VF IOq count */
  513. reg_val = octeon_read_csr64(oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(0));
  514. oct->pf_num = (reg_val >> CN23XX_PKT_INPUT_CTL_PF_NUM_POS) &
  515. CN23XX_PKT_INPUT_CTL_PF_NUM_MASK;
  516. oct->vf_num = (reg_val >> CN23XX_PKT_INPUT_CTL_VF_NUM_POS) &
  517. CN23XX_PKT_INPUT_CTL_VF_NUM_MASK;
  518. reg_val = reg_val >> CN23XX_PKT_INPUT_CTL_RPVF_POS;
  519. rings_per_vf = reg_val & CN23XX_PKT_INPUT_CTL_RPVF_MASK;
  520. ring_flag = 0;
  521. cn23xx->conf = oct_get_config_info(oct, LIO_23XX);
  522. if (!cn23xx->conf) {
  523. dev_err(&oct->pci_dev->dev, "%s No Config found for CN23XX\n",
  524. __func__);
  525. octeon_unmap_pci_barx(oct, 0);
  526. return 1;
  527. }
  528. if (oct->sriov_info.rings_per_vf > rings_per_vf) {
  529. dev_warn(&oct->pci_dev->dev,
  530. "num_queues:%d greater than PF configured rings_per_vf:%d. Reducing to %d.\n",
  531. oct->sriov_info.rings_per_vf, rings_per_vf,
  532. rings_per_vf);
  533. oct->sriov_info.rings_per_vf = rings_per_vf;
  534. } else {
  535. if (rings_per_vf > num_present_cpus()) {
  536. dev_warn(&oct->pci_dev->dev,
  537. "PF configured rings_per_vf:%d greater than num_cpu:%d. Using rings_per_vf:%d equal to num cpus\n",
  538. rings_per_vf,
  539. num_present_cpus(),
  540. num_present_cpus());
  541. oct->sriov_info.rings_per_vf =
  542. num_present_cpus();
  543. } else {
  544. oct->sriov_info.rings_per_vf = rings_per_vf;
  545. }
  546. }
  547. oct->fn_list.setup_iq_regs = cn23xx_setup_vf_iq_regs;
  548. oct->fn_list.setup_oq_regs = cn23xx_setup_vf_oq_regs;
  549. oct->fn_list.setup_mbox = cn23xx_setup_vf_mbox;
  550. oct->fn_list.free_mbox = cn23xx_free_vf_mbox;
  551. oct->fn_list.msix_interrupt_handler = cn23xx_vf_msix_interrupt_handler;
  552. oct->fn_list.setup_device_regs = cn23xx_setup_vf_device_regs;
  553. oct->fn_list.update_iq_read_idx = cn23xx_update_read_index;
  554. oct->fn_list.enable_interrupt = cn23xx_enable_vf_interrupt;
  555. oct->fn_list.disable_interrupt = cn23xx_disable_vf_interrupt;
  556. oct->fn_list.enable_io_queues = cn23xx_enable_vf_io_queues;
  557. oct->fn_list.disable_io_queues = cn23xx_disable_vf_io_queues;
  558. return 0;
  559. }
  560. void cn23xx_dump_vf_iq_regs(struct octeon_device *oct)
  561. {
  562. u32 regval, q_no;
  563. dev_dbg(&oct->pci_dev->dev, "SLI_IQ_DOORBELL_0 [0x%x]: 0x%016llx\n",
  564. CN23XX_VF_SLI_IQ_DOORBELL(0),
  565. CVM_CAST64(octeon_read_csr64(
  566. oct, CN23XX_VF_SLI_IQ_DOORBELL(0))));
  567. dev_dbg(&oct->pci_dev->dev, "SLI_IQ_BASEADDR_0 [0x%x]: 0x%016llx\n",
  568. CN23XX_VF_SLI_IQ_BASE_ADDR64(0),
  569. CVM_CAST64(octeon_read_csr64(
  570. oct, CN23XX_VF_SLI_IQ_BASE_ADDR64(0))));
  571. dev_dbg(&oct->pci_dev->dev, "SLI_IQ_FIFO_RSIZE_0 [0x%x]: 0x%016llx\n",
  572. CN23XX_VF_SLI_IQ_SIZE(0),
  573. CVM_CAST64(octeon_read_csr64(oct, CN23XX_VF_SLI_IQ_SIZE(0))));
  574. for (q_no = 0; q_no < oct->sriov_info.rings_per_vf; q_no++) {
  575. dev_dbg(&oct->pci_dev->dev, "SLI_PKT[%d]_INPUT_CTL [0x%x]: 0x%016llx\n",
  576. q_no, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no),
  577. CVM_CAST64(octeon_read_csr64(
  578. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no))));
  579. }
  580. pci_read_config_dword(oct->pci_dev, CN23XX_CONFIG_PCIE_DEVCTL, &regval);
  581. dev_dbg(&oct->pci_dev->dev, "Config DevCtl [0x%x]: 0x%08x\n",
  582. CN23XX_CONFIG_PCIE_DEVCTL, regval);
  583. }