octeon_nic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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
  17. * details.
  18. **********************************************************************/
  19. #include <linux/pci.h>
  20. #include <linux/netdevice.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 "octeon_nic.h"
  27. #include "octeon_main.h"
  28. void *
  29. octeon_alloc_soft_command_resp(struct octeon_device *oct,
  30. union octeon_instr_64B *cmd,
  31. u32 rdatasize)
  32. {
  33. struct octeon_soft_command *sc;
  34. struct octeon_instr_ih3 *ih3;
  35. struct octeon_instr_ih2 *ih2;
  36. struct octeon_instr_irh *irh;
  37. struct octeon_instr_rdp *rdp;
  38. sc = (struct octeon_soft_command *)
  39. octeon_alloc_soft_command(oct, 0, rdatasize, 0);
  40. if (!sc)
  41. return NULL;
  42. /* Copy existing command structure into the soft command */
  43. memcpy(&sc->cmd, cmd, sizeof(union octeon_instr_64B));
  44. /* Add in the response related fields. Opcode and Param are already
  45. * there.
  46. */
  47. if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
  48. ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
  49. rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
  50. irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
  51. /*pkiih3 + irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
  52. ih3->fsz = LIO_SOFTCMDRESP_IH3;
  53. } else {
  54. ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
  55. rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
  56. irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
  57. /* irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
  58. ih2->fsz = LIO_SOFTCMDRESP_IH2;
  59. }
  60. irh->rflag = 1; /* a response is required */
  61. rdp->pcie_port = oct->pcie_port;
  62. rdp->rlen = rdatasize;
  63. *sc->status_word = COMPLETION_WORD_INIT;
  64. if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct))
  65. sc->cmd.cmd3.rptr = sc->dmarptr;
  66. else
  67. sc->cmd.cmd2.rptr = sc->dmarptr;
  68. sc->wait_time = 1000;
  69. sc->timeout = jiffies + sc->wait_time;
  70. return sc;
  71. }
  72. int octnet_send_nic_data_pkt(struct octeon_device *oct,
  73. struct octnic_data_pkt *ndata)
  74. {
  75. int ring_doorbell = 1;
  76. return octeon_send_command(oct, ndata->q_no, ring_doorbell, &ndata->cmd,
  77. ndata->buf, ndata->datasize,
  78. ndata->reqtype);
  79. }
  80. static void octnet_link_ctrl_callback(struct octeon_device *oct,
  81. u32 status,
  82. void *sc_ptr)
  83. {
  84. struct octeon_soft_command *sc = (struct octeon_soft_command *)sc_ptr;
  85. struct octnic_ctrl_pkt *nctrl;
  86. nctrl = (struct octnic_ctrl_pkt *)sc->ctxptr;
  87. /* Call the callback function if status is zero (meaning OK) or status
  88. * contains a firmware status code bigger than zero (meaning the
  89. * firmware is reporting an error).
  90. * If no response was expected, status is OK if the command was posted
  91. * successfully.
  92. */
  93. if ((!status || status > FIRMWARE_STATUS_CODE(0)) && nctrl->cb_fn) {
  94. nctrl->status = status;
  95. nctrl->cb_fn(nctrl);
  96. }
  97. octeon_free_soft_command(oct, sc);
  98. }
  99. static inline struct octeon_soft_command
  100. *octnic_alloc_ctrl_pkt_sc(struct octeon_device *oct,
  101. struct octnic_ctrl_pkt *nctrl)
  102. {
  103. struct octeon_soft_command *sc = NULL;
  104. u8 *data;
  105. u32 rdatasize;
  106. u32 uddsize = 0, datasize = 0;
  107. uddsize = (u32)(nctrl->ncmd.s.more * 8);
  108. datasize = OCTNET_CMD_SIZE + uddsize;
  109. rdatasize = (nctrl->wait_time) ? 16 : 0;
  110. sc = (struct octeon_soft_command *)
  111. octeon_alloc_soft_command(oct, datasize, rdatasize,
  112. sizeof(struct octnic_ctrl_pkt));
  113. if (!sc)
  114. return NULL;
  115. memcpy(sc->ctxptr, nctrl, sizeof(struct octnic_ctrl_pkt));
  116. data = (u8 *)sc->virtdptr;
  117. memcpy(data, &nctrl->ncmd, OCTNET_CMD_SIZE);
  118. octeon_swap_8B_data((u64 *)data, (OCTNET_CMD_SIZE >> 3));
  119. if (uddsize) {
  120. /* Endian-Swap for UDD should have been done by caller. */
  121. memcpy(data + OCTNET_CMD_SIZE, nctrl->udd, uddsize);
  122. }
  123. sc->iq_no = (u32)nctrl->iq_no;
  124. octeon_prepare_soft_command(oct, sc, OPCODE_NIC, OPCODE_NIC_CMD,
  125. 0, 0, 0);
  126. sc->callback = octnet_link_ctrl_callback;
  127. sc->callback_arg = sc;
  128. sc->wait_time = nctrl->wait_time;
  129. return sc;
  130. }
  131. int
  132. octnet_send_nic_ctrl_pkt(struct octeon_device *oct,
  133. struct octnic_ctrl_pkt *nctrl)
  134. {
  135. int retval;
  136. struct octeon_soft_command *sc = NULL;
  137. spin_lock_bh(&oct->cmd_resp_wqlock);
  138. /* Allow only rx ctrl command to stop traffic on the chip
  139. * during offline operations
  140. */
  141. if ((oct->cmd_resp_state == OCT_DRV_OFFLINE) &&
  142. (nctrl->ncmd.s.cmd != OCTNET_CMD_RX_CTL)) {
  143. spin_unlock_bh(&oct->cmd_resp_wqlock);
  144. dev_err(&oct->pci_dev->dev,
  145. "%s cmd:%d not processed since driver offline\n",
  146. __func__, nctrl->ncmd.s.cmd);
  147. return -1;
  148. }
  149. sc = octnic_alloc_ctrl_pkt_sc(oct, nctrl);
  150. if (!sc) {
  151. dev_err(&oct->pci_dev->dev, "%s soft command alloc failed\n",
  152. __func__);
  153. spin_unlock_bh(&oct->cmd_resp_wqlock);
  154. return -1;
  155. }
  156. retval = octeon_send_soft_command(oct, sc);
  157. if (retval == IQ_SEND_FAILED) {
  158. octeon_free_soft_command(oct, sc);
  159. dev_err(&oct->pci_dev->dev, "%s pf_num:%d soft command:%d send failed status: %x\n",
  160. __func__, oct->pf_num, nctrl->ncmd.s.cmd, retval);
  161. spin_unlock_bh(&oct->cmd_resp_wqlock);
  162. return -1;
  163. }
  164. spin_unlock_bh(&oct->cmd_resp_wqlock);
  165. return retval;
  166. }