octeon_nic.c 5.6 KB

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