cn23xx_vf_device.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. /* clear IPTR */
  142. reg_val &= ~CN23XX_PKT_OUTPUT_CTL_IPTR;
  143. /* set DPTR */
  144. reg_val |= CN23XX_PKT_OUTPUT_CTL_DPTR;
  145. /* reset BMODE */
  146. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_BMODE);
  147. /* No Relaxed Ordering, No Snoop, 64-bit Byte swap
  148. * for Output Queue ScatterList reset ROR_P, NSR_P
  149. */
  150. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ROR_P);
  151. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_NSR_P);
  152. #ifdef __LITTLE_ENDIAN_BITFIELD
  153. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ES_P);
  154. #else
  155. reg_val |= (CN23XX_PKT_OUTPUT_CTL_ES_P);
  156. #endif
  157. /* No Relaxed Ordering, No Snoop, 64-bit Byte swap
  158. * for Output Queue Data reset ROR, NSR
  159. */
  160. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_ROR);
  161. reg_val &= ~(CN23XX_PKT_OUTPUT_CTL_NSR);
  162. /* set the ES bit */
  163. reg_val |= (CN23XX_PKT_OUTPUT_CTL_ES);
  164. /* write all the selected settings */
  165. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no),
  166. reg_val);
  167. }
  168. }
  169. static int cn23xx_setup_vf_device_regs(struct octeon_device *oct)
  170. {
  171. if (cn23xx_vf_setup_global_input_regs(oct))
  172. return -1;
  173. cn23xx_vf_setup_global_output_regs(oct);
  174. return 0;
  175. }
  176. static void cn23xx_setup_vf_iq_regs(struct octeon_device *oct, u32 iq_no)
  177. {
  178. struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
  179. u64 pkt_in_done;
  180. /* Write the start of the input queue's ring and its size */
  181. octeon_write_csr64(oct, CN23XX_VF_SLI_IQ_BASE_ADDR64(iq_no),
  182. iq->base_addr_dma);
  183. octeon_write_csr(oct, CN23XX_VF_SLI_IQ_SIZE(iq_no), iq->max_count);
  184. /* Remember the doorbell & instruction count register addr
  185. * for this queue
  186. */
  187. iq->doorbell_reg =
  188. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_IQ_DOORBELL(iq_no);
  189. iq->inst_cnt_reg =
  190. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_IQ_INSTR_COUNT64(iq_no);
  191. dev_dbg(&oct->pci_dev->dev, "InstQ[%d]:dbell reg @ 0x%p instcnt_reg @ 0x%p\n",
  192. iq_no, iq->doorbell_reg, iq->inst_cnt_reg);
  193. /* Store the current instruction counter (used in flush_iq
  194. * calculation)
  195. */
  196. pkt_in_done = readq(iq->inst_cnt_reg);
  197. if (oct->msix_on) {
  198. /* Set CINT_ENB to enable IQ interrupt */
  199. writeq((pkt_in_done | CN23XX_INTR_CINT_ENB),
  200. iq->inst_cnt_reg);
  201. }
  202. iq->reset_instr_cnt = 0;
  203. }
  204. static void cn23xx_setup_vf_oq_regs(struct octeon_device *oct, u32 oq_no)
  205. {
  206. struct octeon_droq *droq = oct->droq[oq_no];
  207. octeon_write_csr64(oct, CN23XX_VF_SLI_OQ_BASE_ADDR64(oq_no),
  208. droq->desc_ring_dma);
  209. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_SIZE(oq_no), droq->max_count);
  210. octeon_write_csr(oct, CN23XX_VF_SLI_OQ_BUFF_INFO_SIZE(oq_no),
  211. droq->buffer_size);
  212. /* Get the mapped address of the pkt_sent and pkts_credit regs */
  213. droq->pkts_sent_reg =
  214. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_OQ_PKTS_SENT(oq_no);
  215. droq->pkts_credit_reg =
  216. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_OQ_PKTS_CREDIT(oq_no);
  217. }
  218. static void cn23xx_vf_mbox_thread(struct work_struct *work)
  219. {
  220. struct cavium_wk *wk = (struct cavium_wk *)work;
  221. struct octeon_mbox *mbox = (struct octeon_mbox *)wk->ctxptr;
  222. octeon_mbox_process_message(mbox);
  223. }
  224. static int cn23xx_free_vf_mbox(struct octeon_device *oct)
  225. {
  226. cancel_delayed_work_sync(&oct->mbox[0]->mbox_poll_wk.work);
  227. vfree(oct->mbox[0]);
  228. return 0;
  229. }
  230. static int cn23xx_setup_vf_mbox(struct octeon_device *oct)
  231. {
  232. struct octeon_mbox *mbox = NULL;
  233. mbox = vmalloc(sizeof(*mbox));
  234. if (!mbox)
  235. return 1;
  236. memset(mbox, 0, sizeof(struct octeon_mbox));
  237. spin_lock_init(&mbox->lock);
  238. mbox->oct_dev = oct;
  239. mbox->q_no = 0;
  240. mbox->state = OCTEON_MBOX_STATE_IDLE;
  241. /* VF mbox interrupt reg */
  242. mbox->mbox_int_reg =
  243. (u8 *)oct->mmio[0].hw_addr + CN23XX_VF_SLI_PKT_MBOX_INT(0);
  244. /* VF reads from SIG0 reg */
  245. mbox->mbox_read_reg =
  246. (u8 *)oct->mmio[0].hw_addr + CN23XX_SLI_PKT_PF_VF_MBOX_SIG(0, 0);
  247. /* VF writes into SIG1 reg */
  248. mbox->mbox_write_reg =
  249. (u8 *)oct->mmio[0].hw_addr + CN23XX_SLI_PKT_PF_VF_MBOX_SIG(0, 1);
  250. INIT_DELAYED_WORK(&mbox->mbox_poll_wk.work,
  251. cn23xx_vf_mbox_thread);
  252. mbox->mbox_poll_wk.ctxptr = mbox;
  253. oct->mbox[0] = mbox;
  254. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  255. return 0;
  256. }
  257. static int cn23xx_enable_vf_io_queues(struct octeon_device *oct)
  258. {
  259. u32 q_no;
  260. for (q_no = 0; q_no < oct->num_iqs; q_no++) {
  261. u64 reg_val;
  262. /* set the corresponding IQ IS_64B bit */
  263. if (oct->io_qmask.iq64B & BIT_ULL(q_no)) {
  264. reg_val = octeon_read_csr64(
  265. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  266. reg_val |= CN23XX_PKT_INPUT_CTL_IS_64B;
  267. octeon_write_csr64(
  268. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no), reg_val);
  269. }
  270. /* set the corresponding IQ ENB bit */
  271. if (oct->io_qmask.iq & BIT_ULL(q_no)) {
  272. reg_val = octeon_read_csr64(
  273. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no));
  274. reg_val |= CN23XX_PKT_INPUT_CTL_RING_ENB;
  275. octeon_write_csr64(
  276. oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(q_no), reg_val);
  277. }
  278. }
  279. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  280. u32 reg_val;
  281. /* set the corresponding OQ ENB bit */
  282. if (oct->io_qmask.oq & BIT_ULL(q_no)) {
  283. reg_val = octeon_read_csr(
  284. oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no));
  285. reg_val |= CN23XX_PKT_OUTPUT_CTL_RING_ENB;
  286. octeon_write_csr(
  287. oct, CN23XX_VF_SLI_OQ_PKT_CONTROL(q_no), reg_val);
  288. }
  289. }
  290. return 0;
  291. }
  292. static void cn23xx_disable_vf_io_queues(struct octeon_device *oct)
  293. {
  294. u32 num_queues = oct->num_iqs;
  295. /* per HRM, rings can only be disabled via reset operation,
  296. * NOT via SLI_PKT()_INPUT/OUTPUT_CONTROL[ENB]
  297. */
  298. if (num_queues < oct->num_oqs)
  299. num_queues = oct->num_oqs;
  300. cn23xx_vf_reset_io_queues(oct, num_queues);
  301. }
  302. void cn23xx_vf_ask_pf_to_do_flr(struct octeon_device *oct)
  303. {
  304. struct octeon_mbox_cmd mbox_cmd;
  305. mbox_cmd.msg.u64 = 0;
  306. mbox_cmd.msg.s.type = OCTEON_MBOX_REQUEST;
  307. mbox_cmd.msg.s.resp_needed = 0;
  308. mbox_cmd.msg.s.cmd = OCTEON_VF_FLR_REQUEST;
  309. mbox_cmd.msg.s.len = 1;
  310. mbox_cmd.q_no = 0;
  311. mbox_cmd.recv_len = 0;
  312. mbox_cmd.recv_status = 0;
  313. mbox_cmd.fn = NULL;
  314. mbox_cmd.fn_arg = NULL;
  315. octeon_mbox_write(oct, &mbox_cmd);
  316. }
  317. static void octeon_pfvf_hs_callback(struct octeon_device *oct,
  318. struct octeon_mbox_cmd *cmd,
  319. void *arg)
  320. {
  321. u32 major = 0;
  322. memcpy((uint8_t *)&oct->pfvf_hsword, cmd->msg.s.params,
  323. CN23XX_MAILBOX_MSGPARAM_SIZE);
  324. if (cmd->recv_len > 1) {
  325. major = ((struct lio_version *)(cmd->data))->major;
  326. major = major << 16;
  327. }
  328. atomic_set((atomic_t *)arg, major | 1);
  329. }
  330. int cn23xx_octeon_pfvf_handshake(struct octeon_device *oct)
  331. {
  332. struct octeon_mbox_cmd mbox_cmd;
  333. u32 q_no, count = 0;
  334. atomic_t status;
  335. u32 pfmajor;
  336. u32 vfmajor;
  337. u32 ret;
  338. /* Sending VF_ACTIVE indication to the PF driver */
  339. dev_dbg(&oct->pci_dev->dev, "requesting info from pf\n");
  340. mbox_cmd.msg.u64 = 0;
  341. mbox_cmd.msg.s.type = OCTEON_MBOX_REQUEST;
  342. mbox_cmd.msg.s.resp_needed = 1;
  343. mbox_cmd.msg.s.cmd = OCTEON_VF_ACTIVE;
  344. mbox_cmd.msg.s.len = 2;
  345. mbox_cmd.data[0] = 0;
  346. ((struct lio_version *)&mbox_cmd.data[0])->major =
  347. LIQUIDIO_BASE_MAJOR_VERSION;
  348. ((struct lio_version *)&mbox_cmd.data[0])->minor =
  349. LIQUIDIO_BASE_MINOR_VERSION;
  350. ((struct lio_version *)&mbox_cmd.data[0])->micro =
  351. LIQUIDIO_BASE_MICRO_VERSION;
  352. mbox_cmd.q_no = 0;
  353. mbox_cmd.recv_len = 0;
  354. mbox_cmd.recv_status = 0;
  355. mbox_cmd.fn = (octeon_mbox_callback_t)octeon_pfvf_hs_callback;
  356. mbox_cmd.fn_arg = &status;
  357. octeon_mbox_write(oct, &mbox_cmd);
  358. atomic_set(&status, 0);
  359. do {
  360. schedule_timeout_uninterruptible(1);
  361. } while ((!atomic_read(&status)) && (count++ < 100000));
  362. ret = atomic_read(&status);
  363. if (!ret) {
  364. dev_err(&oct->pci_dev->dev, "octeon_pfvf_handshake timeout\n");
  365. return 1;
  366. }
  367. for (q_no = 0 ; q_no < oct->num_iqs ; q_no++)
  368. oct->instr_queue[q_no]->txpciq.s.pkind = oct->pfvf_hsword.pkind;
  369. vfmajor = LIQUIDIO_BASE_MAJOR_VERSION;
  370. pfmajor = ret >> 16;
  371. if (pfmajor != vfmajor) {
  372. dev_err(&oct->pci_dev->dev,
  373. "VF Liquidio driver (major version %d) is not compatible with Liquidio PF driver (major version %d)\n",
  374. vfmajor, pfmajor);
  375. return 1;
  376. }
  377. dev_dbg(&oct->pci_dev->dev,
  378. "VF Liquidio driver (major version %d), Liquidio PF driver (major version %d)\n",
  379. vfmajor, pfmajor);
  380. dev_dbg(&oct->pci_dev->dev, "got data from pf pkind is %d\n",
  381. oct->pfvf_hsword.pkind);
  382. return 0;
  383. }
  384. static void cn23xx_handle_vf_mbox_intr(struct octeon_ioq_vector *ioq_vector)
  385. {
  386. struct octeon_device *oct = ioq_vector->oct_dev;
  387. u64 mbox_int_val;
  388. if (!ioq_vector->droq_index) {
  389. /* read and clear by writing 1 */
  390. mbox_int_val = readq(oct->mbox[0]->mbox_int_reg);
  391. writeq(mbox_int_val, oct->mbox[0]->mbox_int_reg);
  392. if (octeon_mbox_read(oct->mbox[0]))
  393. schedule_delayed_work(&oct->mbox[0]->mbox_poll_wk.work,
  394. msecs_to_jiffies(0));
  395. }
  396. }
  397. static u64 cn23xx_vf_msix_interrupt_handler(void *dev)
  398. {
  399. struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
  400. struct octeon_device *oct = ioq_vector->oct_dev;
  401. struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
  402. u64 pkts_sent;
  403. u64 ret = 0;
  404. dev_dbg(&oct->pci_dev->dev, "In %s octeon_dev @ %p\n", __func__, oct);
  405. pkts_sent = readq(droq->pkts_sent_reg);
  406. /* If our device has interrupted, then proceed. Also check
  407. * for all f's if interrupt was triggered on an error
  408. * and the PCI read fails.
  409. */
  410. if (!pkts_sent || (pkts_sent == 0xFFFFFFFFFFFFFFFFULL))
  411. return ret;
  412. /* Write count reg in sli_pkt_cnts to clear these int. */
  413. if ((pkts_sent & CN23XX_INTR_PO_INT) ||
  414. (pkts_sent & CN23XX_INTR_PI_INT)) {
  415. if (pkts_sent & CN23XX_INTR_PO_INT)
  416. ret |= MSIX_PO_INT;
  417. }
  418. if (pkts_sent & CN23XX_INTR_PI_INT)
  419. /* We will clear the count when we update the read_index. */
  420. ret |= MSIX_PI_INT;
  421. if (pkts_sent & CN23XX_INTR_MBOX_INT) {
  422. cn23xx_handle_vf_mbox_intr(ioq_vector);
  423. ret |= MSIX_MBOX_INT;
  424. }
  425. return ret;
  426. }
  427. static u32 cn23xx_update_read_index(struct octeon_instr_queue *iq)
  428. {
  429. u32 pkt_in_done = readl(iq->inst_cnt_reg);
  430. u32 last_done;
  431. u32 new_idx;
  432. last_done = pkt_in_done - iq->pkt_in_done;
  433. iq->pkt_in_done = pkt_in_done;
  434. /* Modulo of the new index with the IQ size will give us
  435. * the new index. The iq->reset_instr_cnt is always zero for
  436. * cn23xx, so no extra adjustments are needed.
  437. */
  438. new_idx = (iq->octeon_read_index +
  439. (u32)(last_done & CN23XX_PKT_IN_DONE_CNT_MASK)) %
  440. iq->max_count;
  441. return new_idx;
  442. }
  443. static void cn23xx_enable_vf_interrupt(struct octeon_device *oct, u8 intr_flag)
  444. {
  445. struct octeon_cn23xx_vf *cn23xx = (struct octeon_cn23xx_vf *)oct->chip;
  446. u32 q_no, time_threshold;
  447. if (intr_flag & OCTEON_OUTPUT_INTR) {
  448. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  449. /* Set up interrupt packet and time thresholds
  450. * for all the OQs
  451. */
  452. time_threshold = cn23xx_vf_get_oq_ticks(
  453. oct, (u32)CFG_GET_OQ_INTR_TIME(cn23xx->conf));
  454. octeon_write_csr64(
  455. oct, CN23XX_VF_SLI_OQ_PKT_INT_LEVELS(q_no),
  456. (CFG_GET_OQ_INTR_PKT(cn23xx->conf) |
  457. ((u64)time_threshold << 32)));
  458. }
  459. }
  460. if (intr_flag & OCTEON_INPUT_INTR) {
  461. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  462. /* Set CINT_ENB to enable IQ interrupt */
  463. octeon_write_csr64(
  464. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no),
  465. ((octeon_read_csr64(
  466. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no)) &
  467. ~CN23XX_PKT_IN_DONE_CNT_MASK) |
  468. CN23XX_INTR_CINT_ENB));
  469. }
  470. }
  471. /* Set queue-0 MBOX_ENB to enable VF mailbox interrupt */
  472. if (intr_flag & OCTEON_MBOX_INTR) {
  473. octeon_write_csr64(
  474. oct, CN23XX_VF_SLI_PKT_MBOX_INT(0),
  475. (octeon_read_csr64(oct, CN23XX_VF_SLI_PKT_MBOX_INT(0)) |
  476. CN23XX_INTR_MBOX_ENB));
  477. }
  478. }
  479. static void cn23xx_disable_vf_interrupt(struct octeon_device *oct, u8 intr_flag)
  480. {
  481. u32 q_no;
  482. if (intr_flag & OCTEON_OUTPUT_INTR) {
  483. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  484. /* Write all 1's in INT_LEVEL reg to disable PO_INT */
  485. octeon_write_csr64(
  486. oct, CN23XX_VF_SLI_OQ_PKT_INT_LEVELS(q_no),
  487. 0x3fffffffffffff);
  488. }
  489. }
  490. if (intr_flag & OCTEON_INPUT_INTR) {
  491. for (q_no = 0; q_no < oct->num_oqs; q_no++) {
  492. octeon_write_csr64(
  493. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no),
  494. (octeon_read_csr64(
  495. oct, CN23XX_VF_SLI_IQ_INSTR_COUNT64(q_no)) &
  496. ~(CN23XX_INTR_CINT_ENB |
  497. CN23XX_PKT_IN_DONE_CNT_MASK)));
  498. }
  499. }
  500. if (intr_flag & OCTEON_MBOX_INTR) {
  501. octeon_write_csr64(
  502. oct, CN23XX_VF_SLI_PKT_MBOX_INT(0),
  503. (octeon_read_csr64(oct, CN23XX_VF_SLI_PKT_MBOX_INT(0)) &
  504. ~CN23XX_INTR_MBOX_ENB));
  505. }
  506. }
  507. int cn23xx_setup_octeon_vf_device(struct octeon_device *oct)
  508. {
  509. struct octeon_cn23xx_vf *cn23xx = (struct octeon_cn23xx_vf *)oct->chip;
  510. u32 rings_per_vf, ring_flag;
  511. u64 reg_val;
  512. if (octeon_map_pci_barx(oct, 0, 0))
  513. return 1;
  514. /* INPUT_CONTROL[RPVF] gives the VF IOq count */
  515. reg_val = octeon_read_csr64(oct, CN23XX_VF_SLI_IQ_PKT_CONTROL64(0));
  516. oct->pf_num = (reg_val >> CN23XX_PKT_INPUT_CTL_PF_NUM_POS) &
  517. CN23XX_PKT_INPUT_CTL_PF_NUM_MASK;
  518. oct->vf_num = (reg_val >> CN23XX_PKT_INPUT_CTL_VF_NUM_POS) &
  519. CN23XX_PKT_INPUT_CTL_VF_NUM_MASK;
  520. reg_val = reg_val >> CN23XX_PKT_INPUT_CTL_RPVF_POS;
  521. rings_per_vf = reg_val & CN23XX_PKT_INPUT_CTL_RPVF_MASK;
  522. ring_flag = 0;
  523. cn23xx->conf = oct_get_config_info(oct, LIO_23XX);
  524. if (!cn23xx->conf) {
  525. dev_err(&oct->pci_dev->dev, "%s No Config found for CN23XX\n",
  526. __func__);
  527. octeon_unmap_pci_barx(oct, 0);
  528. return 1;
  529. }
  530. if (oct->sriov_info.rings_per_vf > rings_per_vf) {
  531. dev_warn(&oct->pci_dev->dev,
  532. "num_queues:%d greater than PF configured rings_per_vf:%d. Reducing to %d.\n",
  533. oct->sriov_info.rings_per_vf, rings_per_vf,
  534. rings_per_vf);
  535. oct->sriov_info.rings_per_vf = rings_per_vf;
  536. } else {
  537. if (rings_per_vf > num_present_cpus()) {
  538. dev_warn(&oct->pci_dev->dev,
  539. "PF configured rings_per_vf:%d greater than num_cpu:%d. Using rings_per_vf:%d equal to num cpus\n",
  540. rings_per_vf,
  541. num_present_cpus(),
  542. num_present_cpus());
  543. oct->sriov_info.rings_per_vf =
  544. num_present_cpus();
  545. } else {
  546. oct->sriov_info.rings_per_vf = rings_per_vf;
  547. }
  548. }
  549. oct->fn_list.setup_iq_regs = cn23xx_setup_vf_iq_regs;
  550. oct->fn_list.setup_oq_regs = cn23xx_setup_vf_oq_regs;
  551. oct->fn_list.setup_mbox = cn23xx_setup_vf_mbox;
  552. oct->fn_list.free_mbox = cn23xx_free_vf_mbox;
  553. oct->fn_list.msix_interrupt_handler = cn23xx_vf_msix_interrupt_handler;
  554. oct->fn_list.setup_device_regs = cn23xx_setup_vf_device_regs;
  555. oct->fn_list.update_iq_read_idx = cn23xx_update_read_index;
  556. oct->fn_list.enable_interrupt = cn23xx_enable_vf_interrupt;
  557. oct->fn_list.disable_interrupt = cn23xx_disable_vf_interrupt;
  558. oct->fn_list.enable_io_queues = cn23xx_enable_vf_io_queues;
  559. oct->fn_list.disable_io_queues = cn23xx_disable_vf_io_queues;
  560. return 0;
  561. }