i40evf_virtchnl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40evf.h"
  27. #include "i40e_prototype.h"
  28. /* busy wait delay in msec */
  29. #define I40EVF_BUSY_WAIT_DELAY 10
  30. #define I40EVF_BUSY_WAIT_COUNT 50
  31. /**
  32. * i40evf_send_pf_msg
  33. * @adapter: adapter structure
  34. * @op: virtual channel opcode
  35. * @msg: pointer to message buffer
  36. * @len: message length
  37. *
  38. * Send message to PF and print status if failure.
  39. **/
  40. static int i40evf_send_pf_msg(struct i40evf_adapter *adapter,
  41. enum i40e_virtchnl_ops op, u8 *msg, u16 len)
  42. {
  43. struct i40e_hw *hw = &adapter->hw;
  44. i40e_status err;
  45. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
  46. return 0; /* nothing to see here, move along */
  47. err = i40e_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
  48. if (err)
  49. dev_err(&adapter->pdev->dev, "Unable to send opcode %d to PF, err %s, aq_err %s\n",
  50. op, i40evf_stat_str(hw, err),
  51. i40evf_aq_str(hw, hw->aq.asq_last_status));
  52. return err;
  53. }
  54. /**
  55. * i40evf_send_api_ver
  56. * @adapter: adapter structure
  57. *
  58. * Send API version admin queue message to the PF. The reply is not checked
  59. * in this function. Returns 0 if the message was successfully
  60. * sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
  61. **/
  62. int i40evf_send_api_ver(struct i40evf_adapter *adapter)
  63. {
  64. struct i40e_virtchnl_version_info vvi;
  65. vvi.major = I40E_VIRTCHNL_VERSION_MAJOR;
  66. vvi.minor = I40E_VIRTCHNL_VERSION_MINOR;
  67. return i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_VERSION, (u8 *)&vvi,
  68. sizeof(vvi));
  69. }
  70. /**
  71. * i40evf_verify_api_ver
  72. * @adapter: adapter structure
  73. *
  74. * Compare API versions with the PF. Must be called after admin queue is
  75. * initialized. Returns 0 if API versions match, -EIO if they do not,
  76. * I40E_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
  77. * from the firmware are propagated.
  78. **/
  79. int i40evf_verify_api_ver(struct i40evf_adapter *adapter)
  80. {
  81. struct i40e_virtchnl_version_info *pf_vvi;
  82. struct i40e_hw *hw = &adapter->hw;
  83. struct i40e_arq_event_info event;
  84. enum i40e_virtchnl_ops op;
  85. i40e_status err;
  86. event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
  87. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  88. if (!event.msg_buf) {
  89. err = -ENOMEM;
  90. goto out;
  91. }
  92. while (1) {
  93. err = i40evf_clean_arq_element(hw, &event, NULL);
  94. /* When the AQ is empty, i40evf_clean_arq_element will return
  95. * nonzero and this loop will terminate.
  96. */
  97. if (err)
  98. goto out_alloc;
  99. op =
  100. (enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  101. if (op == I40E_VIRTCHNL_OP_VERSION)
  102. break;
  103. }
  104. err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
  105. if (err)
  106. goto out_alloc;
  107. if (op != I40E_VIRTCHNL_OP_VERSION) {
  108. dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
  109. op);
  110. err = -EIO;
  111. goto out_alloc;
  112. }
  113. pf_vvi = (struct i40e_virtchnl_version_info *)event.msg_buf;
  114. adapter->pf_version = *pf_vvi;
  115. if ((pf_vvi->major > I40E_VIRTCHNL_VERSION_MAJOR) ||
  116. ((pf_vvi->major == I40E_VIRTCHNL_VERSION_MAJOR) &&
  117. (pf_vvi->minor > I40E_VIRTCHNL_VERSION_MINOR)))
  118. err = -EIO;
  119. out_alloc:
  120. kfree(event.msg_buf);
  121. out:
  122. return err;
  123. }
  124. /**
  125. * i40evf_send_vf_config_msg
  126. * @adapter: adapter structure
  127. *
  128. * Send VF configuration request admin queue message to the PF. The reply
  129. * is not checked in this function. Returns 0 if the message was
  130. * successfully sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
  131. **/
  132. int i40evf_send_vf_config_msg(struct i40evf_adapter *adapter)
  133. {
  134. u32 caps;
  135. adapter->current_op = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
  136. adapter->aq_required &= ~I40EVF_FLAG_AQ_GET_CONFIG;
  137. caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
  138. I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ |
  139. I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
  140. I40E_VIRTCHNL_VF_OFFLOAD_VLAN |
  141. I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  142. adapter->current_op = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
  143. adapter->aq_required &= ~I40EVF_FLAG_AQ_GET_CONFIG;
  144. if (PF_IS_V11(adapter))
  145. return i40evf_send_pf_msg(adapter,
  146. I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  147. (u8 *)&caps, sizeof(caps));
  148. else
  149. return i40evf_send_pf_msg(adapter,
  150. I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  151. NULL, 0);
  152. }
  153. /**
  154. * i40evf_get_vf_config
  155. * @hw: pointer to the hardware structure
  156. * @len: length of buffer
  157. *
  158. * Get VF configuration from PF and populate hw structure. Must be called after
  159. * admin queue is initialized. Busy waits until response is received from PF,
  160. * with maximum timeout. Response from PF is returned in the buffer for further
  161. * processing by the caller.
  162. **/
  163. int i40evf_get_vf_config(struct i40evf_adapter *adapter)
  164. {
  165. struct i40e_hw *hw = &adapter->hw;
  166. struct i40e_arq_event_info event;
  167. enum i40e_virtchnl_ops op;
  168. i40e_status err;
  169. u16 len;
  170. len = sizeof(struct i40e_virtchnl_vf_resource) +
  171. I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource);
  172. event.buf_len = len;
  173. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  174. if (!event.msg_buf) {
  175. err = -ENOMEM;
  176. goto out;
  177. }
  178. while (1) {
  179. /* When the AQ is empty, i40evf_clean_arq_element will return
  180. * nonzero and this loop will terminate.
  181. */
  182. err = i40evf_clean_arq_element(hw, &event, NULL);
  183. if (err)
  184. goto out_alloc;
  185. op =
  186. (enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  187. if (op == I40E_VIRTCHNL_OP_GET_VF_RESOURCES)
  188. break;
  189. }
  190. err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
  191. memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
  192. i40e_vf_parse_hw_config(hw, adapter->vf_res);
  193. out_alloc:
  194. kfree(event.msg_buf);
  195. out:
  196. return err;
  197. }
  198. /**
  199. * i40evf_configure_queues
  200. * @adapter: adapter structure
  201. *
  202. * Request that the PF set up our (previously allocated) queues.
  203. **/
  204. void i40evf_configure_queues(struct i40evf_adapter *adapter)
  205. {
  206. struct i40e_virtchnl_vsi_queue_config_info *vqci;
  207. struct i40e_virtchnl_queue_pair_info *vqpi;
  208. int pairs = adapter->num_active_queues;
  209. int i, len;
  210. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  211. /* bail because we already have a command pending */
  212. dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
  213. adapter->current_op);
  214. return;
  215. }
  216. adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES;
  217. len = sizeof(struct i40e_virtchnl_vsi_queue_config_info) +
  218. (sizeof(struct i40e_virtchnl_queue_pair_info) * pairs);
  219. vqci = kzalloc(len, GFP_ATOMIC);
  220. if (!vqci)
  221. return;
  222. vqci->vsi_id = adapter->vsi_res->vsi_id;
  223. vqci->num_queue_pairs = pairs;
  224. vqpi = vqci->qpair;
  225. /* Size check is not needed here - HW max is 16 queue pairs, and we
  226. * can fit info for 31 of them into the AQ buffer before it overflows.
  227. */
  228. for (i = 0; i < pairs; i++) {
  229. vqpi->txq.vsi_id = vqci->vsi_id;
  230. vqpi->txq.queue_id = i;
  231. vqpi->txq.ring_len = adapter->tx_rings[i]->count;
  232. vqpi->txq.dma_ring_addr = adapter->tx_rings[i]->dma;
  233. vqpi->txq.headwb_enabled = 1;
  234. vqpi->txq.dma_headwb_addr = vqpi->txq.dma_ring_addr +
  235. (vqpi->txq.ring_len * sizeof(struct i40e_tx_desc));
  236. vqpi->rxq.vsi_id = vqci->vsi_id;
  237. vqpi->rxq.queue_id = i;
  238. vqpi->rxq.ring_len = adapter->rx_rings[i]->count;
  239. vqpi->rxq.dma_ring_addr = adapter->rx_rings[i]->dma;
  240. vqpi->rxq.max_pkt_size = adapter->netdev->mtu
  241. + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
  242. vqpi->rxq.databuffer_size = adapter->rx_rings[i]->rx_buf_len;
  243. vqpi++;
  244. }
  245. adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
  246. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  247. (u8 *)vqci, len);
  248. kfree(vqci);
  249. }
  250. /**
  251. * i40evf_enable_queues
  252. * @adapter: adapter structure
  253. *
  254. * Request that the PF enable all of our queues.
  255. **/
  256. void i40evf_enable_queues(struct i40evf_adapter *adapter)
  257. {
  258. struct i40e_virtchnl_queue_select vqs;
  259. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  260. /* bail because we already have a command pending */
  261. dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
  262. adapter->current_op);
  263. return;
  264. }
  265. adapter->current_op = I40E_VIRTCHNL_OP_ENABLE_QUEUES;
  266. vqs.vsi_id = adapter->vsi_res->vsi_id;
  267. vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
  268. vqs.rx_queues = vqs.tx_queues;
  269. adapter->aq_required &= ~I40EVF_FLAG_AQ_ENABLE_QUEUES;
  270. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
  271. (u8 *)&vqs, sizeof(vqs));
  272. }
  273. /**
  274. * i40evf_disable_queues
  275. * @adapter: adapter structure
  276. *
  277. * Request that the PF disable all of our queues.
  278. **/
  279. void i40evf_disable_queues(struct i40evf_adapter *adapter)
  280. {
  281. struct i40e_virtchnl_queue_select vqs;
  282. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  283. /* bail because we already have a command pending */
  284. dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
  285. adapter->current_op);
  286. return;
  287. }
  288. adapter->current_op = I40E_VIRTCHNL_OP_DISABLE_QUEUES;
  289. vqs.vsi_id = adapter->vsi_res->vsi_id;
  290. vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
  291. vqs.rx_queues = vqs.tx_queues;
  292. adapter->aq_required &= ~I40EVF_FLAG_AQ_DISABLE_QUEUES;
  293. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
  294. (u8 *)&vqs, sizeof(vqs));
  295. }
  296. /**
  297. * i40evf_map_queues
  298. * @adapter: adapter structure
  299. *
  300. * Request that the PF map queues to interrupt vectors. Misc causes, including
  301. * admin queue, are always mapped to vector 0.
  302. **/
  303. void i40evf_map_queues(struct i40evf_adapter *adapter)
  304. {
  305. struct i40e_virtchnl_irq_map_info *vimi;
  306. int v_idx, q_vectors, len;
  307. struct i40e_q_vector *q_vector;
  308. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  309. /* bail because we already have a command pending */
  310. dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
  311. adapter->current_op);
  312. return;
  313. }
  314. adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP;
  315. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  316. len = sizeof(struct i40e_virtchnl_irq_map_info) +
  317. (adapter->num_msix_vectors *
  318. sizeof(struct i40e_virtchnl_vector_map));
  319. vimi = kzalloc(len, GFP_ATOMIC);
  320. if (!vimi)
  321. return;
  322. vimi->num_vectors = adapter->num_msix_vectors;
  323. /* Queue vectors first */
  324. for (v_idx = 0; v_idx < q_vectors; v_idx++) {
  325. q_vector = adapter->q_vector[v_idx];
  326. vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
  327. vimi->vecmap[v_idx].vector_id = v_idx + NONQ_VECS;
  328. vimi->vecmap[v_idx].txq_map = q_vector->ring_mask;
  329. vimi->vecmap[v_idx].rxq_map = q_vector->ring_mask;
  330. }
  331. /* Misc vector last - this is only for AdminQ messages */
  332. vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
  333. vimi->vecmap[v_idx].vector_id = 0;
  334. vimi->vecmap[v_idx].txq_map = 0;
  335. vimi->vecmap[v_idx].rxq_map = 0;
  336. adapter->aq_required &= ~I40EVF_FLAG_AQ_MAP_VECTORS;
  337. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
  338. (u8 *)vimi, len);
  339. kfree(vimi);
  340. }
  341. /**
  342. * i40evf_add_ether_addrs
  343. * @adapter: adapter structure
  344. * @addrs: the MAC address filters to add (contiguous)
  345. * @count: number of filters
  346. *
  347. * Request that the PF add one or more addresses to our filters.
  348. **/
  349. void i40evf_add_ether_addrs(struct i40evf_adapter *adapter)
  350. {
  351. struct i40e_virtchnl_ether_addr_list *veal;
  352. int len, i = 0, count = 0;
  353. struct i40evf_mac_filter *f;
  354. bool more = false;
  355. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  356. /* bail because we already have a command pending */
  357. dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
  358. adapter->current_op);
  359. return;
  360. }
  361. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  362. if (f->add)
  363. count++;
  364. }
  365. if (!count) {
  366. adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  367. return;
  368. }
  369. adapter->current_op = I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS;
  370. len = sizeof(struct i40e_virtchnl_ether_addr_list) +
  371. (count * sizeof(struct i40e_virtchnl_ether_addr));
  372. if (len > I40EVF_MAX_AQ_BUF_SIZE) {
  373. dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
  374. count = (I40EVF_MAX_AQ_BUF_SIZE -
  375. sizeof(struct i40e_virtchnl_ether_addr_list)) /
  376. sizeof(struct i40e_virtchnl_ether_addr);
  377. len = sizeof(struct i40e_virtchnl_ether_addr_list) +
  378. (count * sizeof(struct i40e_virtchnl_ether_addr));
  379. more = true;
  380. }
  381. veal = kzalloc(len, GFP_ATOMIC);
  382. if (!veal)
  383. return;
  384. veal->vsi_id = adapter->vsi_res->vsi_id;
  385. veal->num_elements = count;
  386. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  387. if (f->add) {
  388. ether_addr_copy(veal->list[i].addr, f->macaddr);
  389. i++;
  390. f->add = false;
  391. }
  392. }
  393. if (!more)
  394. adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  395. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
  396. (u8 *)veal, len);
  397. kfree(veal);
  398. }
  399. /**
  400. * i40evf_del_ether_addrs
  401. * @adapter: adapter structure
  402. * @addrs: the MAC address filters to remove (contiguous)
  403. * @count: number of filtes
  404. *
  405. * Request that the PF remove one or more addresses from our filters.
  406. **/
  407. void i40evf_del_ether_addrs(struct i40evf_adapter *adapter)
  408. {
  409. struct i40e_virtchnl_ether_addr_list *veal;
  410. struct i40evf_mac_filter *f, *ftmp;
  411. int len, i = 0, count = 0;
  412. bool more = false;
  413. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  414. /* bail because we already have a command pending */
  415. dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
  416. adapter->current_op);
  417. return;
  418. }
  419. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  420. if (f->remove)
  421. count++;
  422. }
  423. if (!count) {
  424. adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  425. return;
  426. }
  427. adapter->current_op = I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
  428. len = sizeof(struct i40e_virtchnl_ether_addr_list) +
  429. (count * sizeof(struct i40e_virtchnl_ether_addr));
  430. if (len > I40EVF_MAX_AQ_BUF_SIZE) {
  431. dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
  432. count = (I40EVF_MAX_AQ_BUF_SIZE -
  433. sizeof(struct i40e_virtchnl_ether_addr_list)) /
  434. sizeof(struct i40e_virtchnl_ether_addr);
  435. len = sizeof(struct i40e_virtchnl_ether_addr_list) +
  436. (count * sizeof(struct i40e_virtchnl_ether_addr));
  437. more = true;
  438. }
  439. veal = kzalloc(len, GFP_ATOMIC);
  440. if (!veal)
  441. return;
  442. veal->vsi_id = adapter->vsi_res->vsi_id;
  443. veal->num_elements = count;
  444. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  445. if (f->remove) {
  446. ether_addr_copy(veal->list[i].addr, f->macaddr);
  447. i++;
  448. list_del(&f->list);
  449. kfree(f);
  450. }
  451. }
  452. if (!more)
  453. adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  454. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
  455. (u8 *)veal, len);
  456. kfree(veal);
  457. }
  458. /**
  459. * i40evf_add_vlans
  460. * @adapter: adapter structure
  461. * @vlans: the VLANs to add
  462. * @count: number of VLANs
  463. *
  464. * Request that the PF add one or more VLAN filters to our VSI.
  465. **/
  466. void i40evf_add_vlans(struct i40evf_adapter *adapter)
  467. {
  468. struct i40e_virtchnl_vlan_filter_list *vvfl;
  469. int len, i = 0, count = 0;
  470. struct i40evf_vlan_filter *f;
  471. bool more = false;
  472. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  473. /* bail because we already have a command pending */
  474. dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
  475. adapter->current_op);
  476. return;
  477. }
  478. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  479. if (f->add)
  480. count++;
  481. }
  482. if (!count) {
  483. adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  484. return;
  485. }
  486. adapter->current_op = I40E_VIRTCHNL_OP_ADD_VLAN;
  487. len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
  488. (count * sizeof(u16));
  489. if (len > I40EVF_MAX_AQ_BUF_SIZE) {
  490. dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
  491. count = (I40EVF_MAX_AQ_BUF_SIZE -
  492. sizeof(struct i40e_virtchnl_vlan_filter_list)) /
  493. sizeof(u16);
  494. len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
  495. (count * sizeof(u16));
  496. more = true;
  497. }
  498. vvfl = kzalloc(len, GFP_ATOMIC);
  499. if (!vvfl)
  500. return;
  501. vvfl->vsi_id = adapter->vsi_res->vsi_id;
  502. vvfl->num_elements = count;
  503. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  504. if (f->add) {
  505. vvfl->vlan_id[i] = f->vlan;
  506. i++;
  507. f->add = false;
  508. }
  509. }
  510. if (!more)
  511. adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  512. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
  513. kfree(vvfl);
  514. }
  515. /**
  516. * i40evf_del_vlans
  517. * @adapter: adapter structure
  518. * @vlans: the VLANs to remove
  519. * @count: number of VLANs
  520. *
  521. * Request that the PF remove one or more VLAN filters from our VSI.
  522. **/
  523. void i40evf_del_vlans(struct i40evf_adapter *adapter)
  524. {
  525. struct i40e_virtchnl_vlan_filter_list *vvfl;
  526. struct i40evf_vlan_filter *f, *ftmp;
  527. int len, i = 0, count = 0;
  528. bool more = false;
  529. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  530. /* bail because we already have a command pending */
  531. dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
  532. adapter->current_op);
  533. return;
  534. }
  535. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  536. if (f->remove)
  537. count++;
  538. }
  539. if (!count) {
  540. adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  541. return;
  542. }
  543. adapter->current_op = I40E_VIRTCHNL_OP_DEL_VLAN;
  544. len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
  545. (count * sizeof(u16));
  546. if (len > I40EVF_MAX_AQ_BUF_SIZE) {
  547. dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
  548. count = (I40EVF_MAX_AQ_BUF_SIZE -
  549. sizeof(struct i40e_virtchnl_vlan_filter_list)) /
  550. sizeof(u16);
  551. len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
  552. (count * sizeof(u16));
  553. more = true;
  554. }
  555. vvfl = kzalloc(len, GFP_ATOMIC);
  556. if (!vvfl)
  557. return;
  558. vvfl->vsi_id = adapter->vsi_res->vsi_id;
  559. vvfl->num_elements = count;
  560. list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
  561. if (f->remove) {
  562. vvfl->vlan_id[i] = f->vlan;
  563. i++;
  564. list_del(&f->list);
  565. kfree(f);
  566. }
  567. }
  568. if (!more)
  569. adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  570. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
  571. kfree(vvfl);
  572. }
  573. /**
  574. * i40evf_set_promiscuous
  575. * @adapter: adapter structure
  576. * @flags: bitmask to control unicast/multicast promiscuous.
  577. *
  578. * Request that the PF enable promiscuous mode for our VSI.
  579. **/
  580. void i40evf_set_promiscuous(struct i40evf_adapter *adapter, int flags)
  581. {
  582. struct i40e_virtchnl_promisc_info vpi;
  583. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  584. /* bail because we already have a command pending */
  585. dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
  586. adapter->current_op);
  587. return;
  588. }
  589. adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
  590. vpi.vsi_id = adapter->vsi_res->vsi_id;
  591. vpi.flags = flags;
  592. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  593. (u8 *)&vpi, sizeof(vpi));
  594. }
  595. /**
  596. * i40evf_request_stats
  597. * @adapter: adapter structure
  598. *
  599. * Request VSI statistics from PF.
  600. **/
  601. void i40evf_request_stats(struct i40evf_adapter *adapter)
  602. {
  603. struct i40e_virtchnl_queue_select vqs;
  604. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  605. /* no error message, this isn't crucial */
  606. return;
  607. }
  608. adapter->current_op = I40E_VIRTCHNL_OP_GET_STATS;
  609. vqs.vsi_id = adapter->vsi_res->vsi_id;
  610. /* queue maps are ignored for this message - only the vsi is used */
  611. if (i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_GET_STATS,
  612. (u8 *)&vqs, sizeof(vqs)))
  613. /* if the request failed, don't lock out others */
  614. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  615. }
  616. /**
  617. * i40evf_request_reset
  618. * @adapter: adapter structure
  619. *
  620. * Request that the PF reset this VF. No response is expected.
  621. **/
  622. void i40evf_request_reset(struct i40evf_adapter *adapter)
  623. {
  624. /* Don't check CURRENT_OP - this is always higher priority */
  625. i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_RESET_VF, NULL, 0);
  626. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  627. }
  628. /**
  629. * i40evf_virtchnl_completion
  630. * @adapter: adapter structure
  631. * @v_opcode: opcode sent by PF
  632. * @v_retval: retval sent by PF
  633. * @msg: message sent by PF
  634. * @msglen: message length
  635. *
  636. * Asynchronous completion function for admin queue messages. Rather than busy
  637. * wait, we fire off our requests and assume that no errors will be returned.
  638. * This function handles the reply messages.
  639. **/
  640. void i40evf_virtchnl_completion(struct i40evf_adapter *adapter,
  641. enum i40e_virtchnl_ops v_opcode,
  642. i40e_status v_retval,
  643. u8 *msg, u16 msglen)
  644. {
  645. struct net_device *netdev = adapter->netdev;
  646. if (v_opcode == I40E_VIRTCHNL_OP_EVENT) {
  647. struct i40e_virtchnl_pf_event *vpe =
  648. (struct i40e_virtchnl_pf_event *)msg;
  649. switch (vpe->event) {
  650. case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
  651. adapter->link_up =
  652. vpe->event_data.link_event.link_status;
  653. if (adapter->link_up && !netif_carrier_ok(netdev)) {
  654. dev_info(&adapter->pdev->dev, "NIC Link is Up\n");
  655. netif_carrier_on(netdev);
  656. netif_tx_wake_all_queues(netdev);
  657. } else if (!adapter->link_up) {
  658. dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
  659. netif_carrier_off(netdev);
  660. netif_tx_stop_all_queues(netdev);
  661. }
  662. break;
  663. case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
  664. dev_info(&adapter->pdev->dev, "PF reset warning received\n");
  665. if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
  666. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  667. dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
  668. schedule_work(&adapter->reset_task);
  669. }
  670. break;
  671. default:
  672. dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
  673. vpe->event);
  674. break;
  675. }
  676. return;
  677. }
  678. if (v_retval) {
  679. dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
  680. v_retval, i40evf_stat_str(&adapter->hw, v_retval),
  681. v_opcode);
  682. }
  683. switch (v_opcode) {
  684. case I40E_VIRTCHNL_OP_GET_STATS: {
  685. struct i40e_eth_stats *stats =
  686. (struct i40e_eth_stats *)msg;
  687. adapter->net_stats.rx_packets = stats->rx_unicast +
  688. stats->rx_multicast +
  689. stats->rx_broadcast;
  690. adapter->net_stats.tx_packets = stats->tx_unicast +
  691. stats->tx_multicast +
  692. stats->tx_broadcast;
  693. adapter->net_stats.rx_bytes = stats->rx_bytes;
  694. adapter->net_stats.tx_bytes = stats->tx_bytes;
  695. adapter->net_stats.tx_errors = stats->tx_errors;
  696. adapter->net_stats.rx_dropped = stats->rx_discards;
  697. adapter->net_stats.tx_dropped = stats->tx_discards;
  698. adapter->current_stats = *stats;
  699. }
  700. break;
  701. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES: {
  702. u16 len = sizeof(struct i40e_virtchnl_vf_resource) +
  703. I40E_MAX_VF_VSI *
  704. sizeof(struct i40e_virtchnl_vsi_resource);
  705. memcpy(adapter->vf_res, msg, min(msglen, len));
  706. i40e_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
  707. /* restore current mac address */
  708. ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
  709. i40evf_process_config(adapter);
  710. }
  711. break;
  712. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  713. /* enable transmits */
  714. i40evf_irq_enable(adapter, true);
  715. netif_tx_start_all_queues(adapter->netdev);
  716. netif_carrier_on(adapter->netdev);
  717. break;
  718. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  719. i40evf_free_all_tx_resources(adapter);
  720. i40evf_free_all_rx_resources(adapter);
  721. break;
  722. case I40E_VIRTCHNL_OP_VERSION:
  723. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  724. /* Don't display an error if we get these out of sequence.
  725. * If the firmware needed to get kicked, we'll get these and
  726. * it's no problem.
  727. */
  728. if (v_opcode != adapter->current_op)
  729. return;
  730. break;
  731. default:
  732. if (v_opcode != adapter->current_op)
  733. dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
  734. adapter->current_op, v_opcode);
  735. break;
  736. } /* switch v_opcode */
  737. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  738. }