i40evf_virtchnl.c 23 KB

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