cptpf_mbox.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2016 Cavium, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include "cptpf.h"
  10. static void cpt_send_msg_to_vf(struct cpt_device *cpt, int vf,
  11. struct cpt_mbox *mbx)
  12. {
  13. /* Writing mbox(0) causes interrupt */
  14. cpt_write_csr64(cpt->reg_base, CPTX_PF_VFX_MBOXX(0, vf, 1),
  15. mbx->data);
  16. cpt_write_csr64(cpt->reg_base, CPTX_PF_VFX_MBOXX(0, vf, 0), mbx->msg);
  17. }
  18. /* ACKs VF's mailbox message
  19. * @vf: VF to which ACK to be sent
  20. */
  21. static void cpt_mbox_send_ack(struct cpt_device *cpt, int vf,
  22. struct cpt_mbox *mbx)
  23. {
  24. mbx->data = 0ull;
  25. mbx->msg = CPT_MBOX_MSG_TYPE_ACK;
  26. cpt_send_msg_to_vf(cpt, vf, mbx);
  27. }
  28. static void cpt_clear_mbox_intr(struct cpt_device *cpt, u32 vf)
  29. {
  30. /* W1C for the VF */
  31. cpt_write_csr64(cpt->reg_base, CPTX_PF_MBOX_INTX(0, 0), (1 << vf));
  32. }
  33. /*
  34. * Configure QLEN/Chunk sizes for VF
  35. */
  36. static void cpt_cfg_qlen_for_vf(struct cpt_device *cpt, int vf, u32 size)
  37. {
  38. union cptx_pf_qx_ctl pf_qx_ctl;
  39. pf_qx_ctl.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, vf));
  40. pf_qx_ctl.s.size = size;
  41. pf_qx_ctl.s.cont_err = true;
  42. cpt_write_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, vf), pf_qx_ctl.u);
  43. }
  44. /*
  45. * Configure VQ priority
  46. */
  47. static void cpt_cfg_vq_priority(struct cpt_device *cpt, int vf, u32 pri)
  48. {
  49. union cptx_pf_qx_ctl pf_qx_ctl;
  50. pf_qx_ctl.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, vf));
  51. pf_qx_ctl.s.pri = pri;
  52. cpt_write_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, vf), pf_qx_ctl.u);
  53. }
  54. static int cpt_bind_vq_to_grp(struct cpt_device *cpt, u8 q, u8 grp)
  55. {
  56. struct microcode *mcode = cpt->mcode;
  57. union cptx_pf_qx_ctl pf_qx_ctl;
  58. struct device *dev = &cpt->pdev->dev;
  59. if (q >= CPT_MAX_VF_NUM) {
  60. dev_err(dev, "Queues are more than cores in the group");
  61. return -EINVAL;
  62. }
  63. if (grp >= CPT_MAX_CORE_GROUPS) {
  64. dev_err(dev, "Request group is more than possible groups");
  65. return -EINVAL;
  66. }
  67. if (grp >= cpt->next_mc_idx) {
  68. dev_err(dev, "Request group is higher than available functional groups");
  69. return -EINVAL;
  70. }
  71. pf_qx_ctl.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, q));
  72. pf_qx_ctl.s.grp = mcode[grp].group;
  73. cpt_write_csr64(cpt->reg_base, CPTX_PF_QX_CTL(0, q), pf_qx_ctl.u);
  74. dev_dbg(dev, "VF %d TYPE %s", q, (mcode[grp].is_ae ? "AE" : "SE"));
  75. return mcode[grp].is_ae ? AE_TYPES : SE_TYPES;
  76. }
  77. /* Interrupt handler to handle mailbox messages from VFs */
  78. static void cpt_handle_mbox_intr(struct cpt_device *cpt, int vf)
  79. {
  80. struct cpt_vf_info *vfx = &cpt->vfinfo[vf];
  81. struct cpt_mbox mbx = {};
  82. int vftype;
  83. struct device *dev = &cpt->pdev->dev;
  84. /*
  85. * MBOX[0] contains msg
  86. * MBOX[1] contains data
  87. */
  88. mbx.msg = cpt_read_csr64(cpt->reg_base, CPTX_PF_VFX_MBOXX(0, vf, 0));
  89. mbx.data = cpt_read_csr64(cpt->reg_base, CPTX_PF_VFX_MBOXX(0, vf, 1));
  90. dev_dbg(dev, "%s: Mailbox msg 0x%llx from VF%d", __func__, mbx.msg, vf);
  91. switch (mbx.msg) {
  92. case CPT_MSG_VF_UP:
  93. vfx->state = VF_STATE_UP;
  94. try_module_get(THIS_MODULE);
  95. cpt_mbox_send_ack(cpt, vf, &mbx);
  96. break;
  97. case CPT_MSG_READY:
  98. mbx.msg = CPT_MSG_READY;
  99. mbx.data = vf;
  100. cpt_send_msg_to_vf(cpt, vf, &mbx);
  101. break;
  102. case CPT_MSG_VF_DOWN:
  103. /* First msg in VF teardown sequence */
  104. vfx->state = VF_STATE_DOWN;
  105. module_put(THIS_MODULE);
  106. cpt_mbox_send_ack(cpt, vf, &mbx);
  107. break;
  108. case CPT_MSG_QLEN:
  109. vfx->qlen = mbx.data;
  110. cpt_cfg_qlen_for_vf(cpt, vf, vfx->qlen);
  111. cpt_mbox_send_ack(cpt, vf, &mbx);
  112. break;
  113. case CPT_MSG_QBIND_GRP:
  114. vftype = cpt_bind_vq_to_grp(cpt, vf, (u8)mbx.data);
  115. if ((vftype != AE_TYPES) && (vftype != SE_TYPES))
  116. dev_err(dev, "Queue %d binding to group %llu failed",
  117. vf, mbx.data);
  118. else {
  119. dev_dbg(dev, "Queue %d binding to group %llu successful",
  120. vf, mbx.data);
  121. mbx.msg = CPT_MSG_QBIND_GRP;
  122. mbx.data = vftype;
  123. cpt_send_msg_to_vf(cpt, vf, &mbx);
  124. }
  125. break;
  126. case CPT_MSG_VQ_PRIORITY:
  127. vfx->priority = mbx.data;
  128. cpt_cfg_vq_priority(cpt, vf, vfx->priority);
  129. cpt_mbox_send_ack(cpt, vf, &mbx);
  130. break;
  131. default:
  132. dev_err(&cpt->pdev->dev, "Invalid msg from VF%d, msg 0x%llx\n",
  133. vf, mbx.msg);
  134. break;
  135. }
  136. }
  137. void cpt_mbox_intr_handler (struct cpt_device *cpt, int mbx)
  138. {
  139. u64 intr;
  140. u8 vf;
  141. intr = cpt_read_csr64(cpt->reg_base, CPTX_PF_MBOX_INTX(0, 0));
  142. dev_dbg(&cpt->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
  143. for (vf = 0; vf < CPT_MAX_VF_NUM; vf++) {
  144. if (intr & (1ULL << vf)) {
  145. dev_dbg(&cpt->pdev->dev, "Intr from VF %d\n", vf);
  146. cpt_handle_mbox_intr(cpt, vf);
  147. cpt_clear_mbox_intr(cpt, vf);
  148. }
  149. }
  150. }