octeon_mailbox.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "liquidio_common.h"
  21. #include "octeon_droq.h"
  22. #include "octeon_iq.h"
  23. #include "response_manager.h"
  24. #include "octeon_device.h"
  25. #include "octeon_main.h"
  26. #include "octeon_mailbox.h"
  27. /**
  28. * octeon_mbox_read:
  29. * @oct: Pointer mailbox
  30. *
  31. * Reads the 8-bytes of data from the mbox register
  32. * Writes back the acknowldgement inidcating completion of read
  33. */
  34. int octeon_mbox_read(struct octeon_mbox *mbox)
  35. {
  36. union octeon_mbox_message msg;
  37. int ret = 0;
  38. spin_lock(&mbox->lock);
  39. msg.u64 = readq(mbox->mbox_read_reg);
  40. if ((msg.u64 == OCTEON_PFVFACK) || (msg.u64 == OCTEON_PFVFSIG)) {
  41. spin_unlock(&mbox->lock);
  42. return 0;
  43. }
  44. if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVING) {
  45. mbox->mbox_req.data[mbox->mbox_req.recv_len - 1] = msg.u64;
  46. mbox->mbox_req.recv_len++;
  47. } else {
  48. if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVING) {
  49. mbox->mbox_resp.data[mbox->mbox_resp.recv_len - 1] =
  50. msg.u64;
  51. mbox->mbox_resp.recv_len++;
  52. } else {
  53. if ((mbox->state & OCTEON_MBOX_STATE_IDLE) &&
  54. (msg.s.type == OCTEON_MBOX_REQUEST)) {
  55. mbox->state &= ~OCTEON_MBOX_STATE_IDLE;
  56. mbox->state |=
  57. OCTEON_MBOX_STATE_REQUEST_RECEIVING;
  58. mbox->mbox_req.msg.u64 = msg.u64;
  59. mbox->mbox_req.q_no = mbox->q_no;
  60. mbox->mbox_req.recv_len = 1;
  61. } else {
  62. if ((mbox->state &
  63. OCTEON_MBOX_STATE_RESPONSE_PENDING) &&
  64. (msg.s.type == OCTEON_MBOX_RESPONSE)) {
  65. mbox->state &=
  66. ~OCTEON_MBOX_STATE_RESPONSE_PENDING;
  67. mbox->state |=
  68. OCTEON_MBOX_STATE_RESPONSE_RECEIVING
  69. ;
  70. mbox->mbox_resp.msg.u64 = msg.u64;
  71. mbox->mbox_resp.q_no = mbox->q_no;
  72. mbox->mbox_resp.recv_len = 1;
  73. } else {
  74. writeq(OCTEON_PFVFERR,
  75. mbox->mbox_read_reg);
  76. mbox->state |= OCTEON_MBOX_STATE_ERROR;
  77. spin_unlock(&mbox->lock);
  78. return 1;
  79. }
  80. }
  81. }
  82. }
  83. if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVING) {
  84. if (mbox->mbox_req.recv_len < msg.s.len) {
  85. ret = 0;
  86. } else {
  87. mbox->state &= ~OCTEON_MBOX_STATE_REQUEST_RECEIVING;
  88. mbox->state |= OCTEON_MBOX_STATE_REQUEST_RECEIVED;
  89. ret = 1;
  90. }
  91. } else {
  92. if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVING) {
  93. if (mbox->mbox_resp.recv_len < msg.s.len) {
  94. ret = 0;
  95. } else {
  96. mbox->state &=
  97. ~OCTEON_MBOX_STATE_RESPONSE_RECEIVING;
  98. mbox->state |=
  99. OCTEON_MBOX_STATE_RESPONSE_RECEIVED;
  100. ret = 1;
  101. }
  102. } else {
  103. WARN_ON(1);
  104. }
  105. }
  106. writeq(OCTEON_PFVFACK, mbox->mbox_read_reg);
  107. spin_unlock(&mbox->lock);
  108. return ret;
  109. }
  110. /**
  111. * octeon_mbox_write:
  112. * @oct: Pointer Octeon Device
  113. * @mbox_cmd: Cmd to send to mailbox.
  114. *
  115. * Populates the queue specific mbox structure
  116. * with cmd information.
  117. * Write the cmd to mbox register
  118. */
  119. int octeon_mbox_write(struct octeon_device *oct,
  120. struct octeon_mbox_cmd *mbox_cmd)
  121. {
  122. struct octeon_mbox *mbox = oct->mbox[mbox_cmd->q_no];
  123. u32 count, i, ret = OCTEON_MBOX_STATUS_SUCCESS;
  124. long timeout = LIO_MBOX_WRITE_WAIT_TIME;
  125. unsigned long flags;
  126. spin_lock_irqsave(&mbox->lock, flags);
  127. if ((mbox_cmd->msg.s.type == OCTEON_MBOX_RESPONSE) &&
  128. !(mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVED)) {
  129. spin_unlock_irqrestore(&mbox->lock, flags);
  130. return OCTEON_MBOX_STATUS_FAILED;
  131. }
  132. if ((mbox_cmd->msg.s.type == OCTEON_MBOX_REQUEST) &&
  133. !(mbox->state & OCTEON_MBOX_STATE_IDLE)) {
  134. spin_unlock_irqrestore(&mbox->lock, flags);
  135. return OCTEON_MBOX_STATUS_BUSY;
  136. }
  137. if (mbox_cmd->msg.s.type == OCTEON_MBOX_REQUEST) {
  138. memcpy(&mbox->mbox_resp, mbox_cmd,
  139. sizeof(struct octeon_mbox_cmd));
  140. mbox->state = OCTEON_MBOX_STATE_RESPONSE_PENDING;
  141. }
  142. spin_unlock_irqrestore(&mbox->lock, flags);
  143. count = 0;
  144. while (readq(mbox->mbox_write_reg) != OCTEON_PFVFSIG) {
  145. schedule_timeout_uninterruptible(timeout);
  146. if (count++ == LIO_MBOX_WRITE_WAIT_CNT) {
  147. ret = OCTEON_MBOX_STATUS_FAILED;
  148. break;
  149. }
  150. }
  151. if (ret == OCTEON_MBOX_STATUS_SUCCESS) {
  152. writeq(mbox_cmd->msg.u64, mbox->mbox_write_reg);
  153. for (i = 0; i < (u32)(mbox_cmd->msg.s.len - 1); i++) {
  154. count = 0;
  155. while (readq(mbox->mbox_write_reg) !=
  156. OCTEON_PFVFACK) {
  157. schedule_timeout_uninterruptible(timeout);
  158. if (count++ == LIO_MBOX_WRITE_WAIT_CNT) {
  159. ret = OCTEON_MBOX_STATUS_FAILED;
  160. break;
  161. }
  162. }
  163. if (ret == OCTEON_MBOX_STATUS_SUCCESS)
  164. writeq(mbox_cmd->data[i], mbox->mbox_write_reg);
  165. else
  166. break;
  167. }
  168. }
  169. spin_lock_irqsave(&mbox->lock, flags);
  170. if (mbox_cmd->msg.s.type == OCTEON_MBOX_RESPONSE) {
  171. mbox->state = OCTEON_MBOX_STATE_IDLE;
  172. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  173. } else {
  174. if ((!mbox_cmd->msg.s.resp_needed) ||
  175. (ret == OCTEON_MBOX_STATUS_FAILED)) {
  176. mbox->state &= ~OCTEON_MBOX_STATE_RESPONSE_PENDING;
  177. if (!(mbox->state &
  178. (OCTEON_MBOX_STATE_REQUEST_RECEIVING |
  179. OCTEON_MBOX_STATE_REQUEST_RECEIVED)))
  180. mbox->state = OCTEON_MBOX_STATE_IDLE;
  181. }
  182. }
  183. spin_unlock_irqrestore(&mbox->lock, flags);
  184. return ret;
  185. }
  186. /**
  187. * octeon_mbox_process_cmd:
  188. * @mbox: Pointer mailbox
  189. * @mbox_cmd: Pointer to command received
  190. *
  191. * Process the cmd received in mbox
  192. */
  193. static int octeon_mbox_process_cmd(struct octeon_mbox *mbox,
  194. struct octeon_mbox_cmd *mbox_cmd)
  195. {
  196. struct octeon_device *oct = mbox->oct_dev;
  197. switch (mbox_cmd->msg.s.cmd) {
  198. case OCTEON_VF_ACTIVE:
  199. dev_dbg(&oct->pci_dev->dev, "got vfactive sending data back\n");
  200. mbox_cmd->msg.s.type = OCTEON_MBOX_RESPONSE;
  201. mbox_cmd->msg.s.resp_needed = 1;
  202. mbox_cmd->msg.s.len = 2;
  203. mbox_cmd->data[0] = 0; /* VF version is in mbox_cmd->data[0] */
  204. ((struct lio_version *)&mbox_cmd->data[0])->major =
  205. LIQUIDIO_BASE_MAJOR_VERSION;
  206. ((struct lio_version *)&mbox_cmd->data[0])->minor =
  207. LIQUIDIO_BASE_MINOR_VERSION;
  208. ((struct lio_version *)&mbox_cmd->data[0])->micro =
  209. LIQUIDIO_BASE_MICRO_VERSION;
  210. memcpy(mbox_cmd->msg.s.params, (uint8_t *)&oct->pfvf_hsword, 6);
  211. /* Sending core cofig info to the corresponding active VF.*/
  212. octeon_mbox_write(oct, mbox_cmd);
  213. break;
  214. case OCTEON_VF_FLR_REQUEST:
  215. dev_info(&oct->pci_dev->dev,
  216. "got a request for FLR from VF that owns DPI ring %u\n",
  217. mbox->q_no);
  218. pcie_capability_set_word(
  219. oct->sriov_info.dpiring_to_vfpcidev_lut[mbox->q_no],
  220. PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  221. break;
  222. case OCTEON_PF_CHANGED_VF_MACADDR:
  223. if (OCTEON_CN23XX_VF(oct))
  224. octeon_pf_changed_vf_macaddr(oct,
  225. mbox_cmd->msg.s.params);
  226. break;
  227. default:
  228. break;
  229. }
  230. return 0;
  231. }
  232. /**
  233. *octeon_mbox_process_message:
  234. *
  235. * Process the received mbox message.
  236. */
  237. int octeon_mbox_process_message(struct octeon_mbox *mbox)
  238. {
  239. struct octeon_mbox_cmd mbox_cmd;
  240. unsigned long flags;
  241. spin_lock_irqsave(&mbox->lock, flags);
  242. if (mbox->state & OCTEON_MBOX_STATE_ERROR) {
  243. if (mbox->state & (OCTEON_MBOX_STATE_RESPONSE_PENDING |
  244. OCTEON_MBOX_STATE_RESPONSE_RECEIVING)) {
  245. memcpy(&mbox_cmd, &mbox->mbox_resp,
  246. sizeof(struct octeon_mbox_cmd));
  247. mbox->state = OCTEON_MBOX_STATE_IDLE;
  248. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  249. spin_unlock_irqrestore(&mbox->lock, flags);
  250. mbox_cmd.recv_status = 1;
  251. if (mbox_cmd.fn)
  252. mbox_cmd.fn(mbox->oct_dev, &mbox_cmd,
  253. mbox_cmd.fn_arg);
  254. return 0;
  255. }
  256. mbox->state = OCTEON_MBOX_STATE_IDLE;
  257. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  258. spin_unlock_irqrestore(&mbox->lock, flags);
  259. return 0;
  260. }
  261. if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVED) {
  262. memcpy(&mbox_cmd, &mbox->mbox_resp,
  263. sizeof(struct octeon_mbox_cmd));
  264. mbox->state = OCTEON_MBOX_STATE_IDLE;
  265. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  266. spin_unlock_irqrestore(&mbox->lock, flags);
  267. mbox_cmd.recv_status = 0;
  268. if (mbox_cmd.fn)
  269. mbox_cmd.fn(mbox->oct_dev, &mbox_cmd, mbox_cmd.fn_arg);
  270. return 0;
  271. }
  272. if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVED) {
  273. memcpy(&mbox_cmd, &mbox->mbox_req,
  274. sizeof(struct octeon_mbox_cmd));
  275. if (!mbox_cmd.msg.s.resp_needed) {
  276. mbox->state &= ~OCTEON_MBOX_STATE_REQUEST_RECEIVED;
  277. if (!(mbox->state &
  278. OCTEON_MBOX_STATE_RESPONSE_PENDING))
  279. mbox->state = OCTEON_MBOX_STATE_IDLE;
  280. writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
  281. }
  282. spin_unlock_irqrestore(&mbox->lock, flags);
  283. octeon_mbox_process_cmd(mbox, &mbox_cmd);
  284. return 0;
  285. }
  286. spin_unlock_irqrestore(&mbox->lock, flags);
  287. WARN_ON(1);
  288. return 0;
  289. }