ef10_sriov.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2015 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/module.h>
  11. #include "net_driver.h"
  12. #include "ef10_sriov.h"
  13. #include "efx.h"
  14. #include "nic.h"
  15. #include "mcdi_pcol.h"
  16. static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
  17. unsigned int vf_fn)
  18. {
  19. MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
  20. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  21. MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
  22. MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
  23. EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
  24. EVB_PORT_ASSIGN_IN_VF, vf_fn);
  25. return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
  26. NULL, 0, NULL);
  27. }
  28. static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
  29. unsigned int vswitch_type)
  30. {
  31. MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
  32. int rc;
  33. MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
  34. MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
  35. MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
  36. MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
  37. VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
  38. /* Quietly try to allocate 2 VLAN tags */
  39. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
  40. NULL, 0, NULL);
  41. /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
  42. if (rc == -EPROTO) {
  43. MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
  44. rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
  45. sizeof(inbuf), NULL, 0, NULL);
  46. } else if (rc) {
  47. efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
  48. MC_CMD_VSWITCH_ALLOC_IN_LEN,
  49. NULL, 0, rc);
  50. }
  51. return rc;
  52. }
  53. static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
  54. {
  55. MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
  56. MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
  57. return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
  58. NULL, 0, NULL);
  59. }
  60. static int efx_ef10_vport_alloc(struct efx_nic *efx,
  61. unsigned int port_id_in,
  62. unsigned int vport_type,
  63. u16 vlan,
  64. unsigned int *port_id_out)
  65. {
  66. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
  67. MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
  68. size_t outlen;
  69. int rc;
  70. EFX_WARN_ON_PARANOID(!port_id_out);
  71. MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
  72. MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
  73. MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
  74. (vlan != EFX_EF10_NO_VLAN));
  75. MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
  76. VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
  77. if (vlan != EFX_EF10_NO_VLAN)
  78. MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
  79. VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
  80. rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
  81. outbuf, sizeof(outbuf), &outlen);
  82. if (rc)
  83. return rc;
  84. if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
  85. return -EIO;
  86. *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
  87. return 0;
  88. }
  89. static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
  90. {
  91. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
  92. MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
  93. return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
  94. NULL, 0, NULL);
  95. }
  96. static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
  97. {
  98. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  99. int i;
  100. if (!nic_data->vf)
  101. return;
  102. for (i = 0; i < efx->vf_count; i++) {
  103. struct ef10_vf *vf = nic_data->vf + i;
  104. /* If VF is assigned, do not free the vport */
  105. if (vf->pci_dev &&
  106. vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
  107. continue;
  108. if (vf->vport_assigned) {
  109. efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
  110. vf->vport_assigned = 0;
  111. }
  112. if (!is_zero_ether_addr(vf->mac)) {
  113. efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
  114. eth_zero_addr(vf->mac);
  115. }
  116. if (vf->vport_id) {
  117. efx_ef10_vport_free(efx, vf->vport_id);
  118. vf->vport_id = 0;
  119. }
  120. vf->efx = NULL;
  121. }
  122. }
  123. static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
  124. {
  125. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  126. efx_ef10_sriov_free_vf_vports(efx);
  127. kfree(nic_data->vf);
  128. nic_data->vf = NULL;
  129. }
  130. static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
  131. unsigned int vf_i)
  132. {
  133. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  134. struct ef10_vf *vf = nic_data->vf + vf_i;
  135. int rc;
  136. if (WARN_ON_ONCE(!nic_data->vf))
  137. return -EOPNOTSUPP;
  138. rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
  139. MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
  140. vf->vlan, &vf->vport_id);
  141. if (rc)
  142. return rc;
  143. rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
  144. if (rc) {
  145. eth_zero_addr(vf->mac);
  146. return rc;
  147. }
  148. rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
  149. if (rc)
  150. return rc;
  151. vf->vport_assigned = 1;
  152. return 0;
  153. }
  154. static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
  155. {
  156. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  157. unsigned int i;
  158. int rc;
  159. nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
  160. GFP_KERNEL);
  161. if (!nic_data->vf)
  162. return -ENOMEM;
  163. for (i = 0; i < efx->vf_count; i++) {
  164. random_ether_addr(nic_data->vf[i].mac);
  165. nic_data->vf[i].efx = NULL;
  166. nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
  167. rc = efx_ef10_sriov_assign_vf_vport(efx, i);
  168. if (rc)
  169. goto fail;
  170. }
  171. return 0;
  172. fail:
  173. efx_ef10_sriov_free_vf_vports(efx);
  174. kfree(nic_data->vf);
  175. nic_data->vf = NULL;
  176. return rc;
  177. }
  178. static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
  179. {
  180. unsigned int i;
  181. int rc;
  182. for (i = 0; i < efx->vf_count; i++) {
  183. rc = efx_ef10_sriov_assign_vf_vport(efx, i);
  184. if (rc)
  185. goto fail;
  186. }
  187. return 0;
  188. fail:
  189. efx_ef10_sriov_free_vf_vswitching(efx);
  190. return rc;
  191. }
  192. /* On top of the default firmware vswitch setup, create a VEB vswitch and
  193. * expansion vport for use by this function.
  194. */
  195. int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
  196. {
  197. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  198. struct net_device *net_dev = efx->net_dev;
  199. int rc;
  200. if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
  201. /* vswitch not needed as we have no VFs */
  202. efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
  203. return 0;
  204. }
  205. rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
  206. MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
  207. if (rc)
  208. goto fail1;
  209. rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
  210. MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
  211. EFX_EF10_NO_VLAN, &nic_data->vport_id);
  212. if (rc)
  213. goto fail2;
  214. rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
  215. if (rc)
  216. goto fail3;
  217. ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
  218. rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
  219. if (rc)
  220. goto fail4;
  221. return 0;
  222. fail4:
  223. efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
  224. eth_zero_addr(nic_data->vport_mac);
  225. fail3:
  226. efx_ef10_vport_free(efx, nic_data->vport_id);
  227. nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
  228. fail2:
  229. efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
  230. fail1:
  231. return rc;
  232. }
  233. int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
  234. {
  235. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  236. return efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
  237. }
  238. int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
  239. {
  240. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  241. int rc;
  242. if (!nic_data->must_probe_vswitching)
  243. return 0;
  244. rc = efx_ef10_vswitching_probe_pf(efx);
  245. if (rc)
  246. goto fail;
  247. rc = efx_ef10_sriov_restore_vf_vswitching(efx);
  248. if (rc)
  249. goto fail;
  250. nic_data->must_probe_vswitching = false;
  251. fail:
  252. return rc;
  253. }
  254. int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
  255. {
  256. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  257. int rc;
  258. if (!nic_data->must_probe_vswitching)
  259. return 0;
  260. rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
  261. if (rc)
  262. return rc;
  263. nic_data->must_probe_vswitching = false;
  264. return 0;
  265. }
  266. void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
  267. {
  268. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  269. efx_ef10_sriov_free_vf_vswitching(efx);
  270. efx_ef10_vadaptor_free(efx, nic_data->vport_id);
  271. if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
  272. return; /* No vswitch was ever created */
  273. if (!is_zero_ether_addr(nic_data->vport_mac)) {
  274. efx_ef10_vport_del_mac(efx, nic_data->vport_id,
  275. efx->net_dev->dev_addr);
  276. eth_zero_addr(nic_data->vport_mac);
  277. }
  278. efx_ef10_vport_free(efx, nic_data->vport_id);
  279. nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
  280. /* Only free the vswitch if no VFs are assigned */
  281. if (!pci_vfs_assigned(efx->pci_dev))
  282. efx_ef10_vswitch_free(efx, nic_data->vport_id);
  283. }
  284. void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
  285. {
  286. efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
  287. }
  288. static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
  289. {
  290. int rc = 0;
  291. struct pci_dev *dev = efx->pci_dev;
  292. efx->vf_count = num_vfs;
  293. rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
  294. if (rc)
  295. goto fail1;
  296. rc = pci_enable_sriov(dev, num_vfs);
  297. if (rc)
  298. goto fail2;
  299. return 0;
  300. fail2:
  301. efx_ef10_sriov_free_vf_vswitching(efx);
  302. fail1:
  303. efx->vf_count = 0;
  304. netif_err(efx, probe, efx->net_dev,
  305. "Failed to enable SRIOV VFs\n");
  306. return rc;
  307. }
  308. static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
  309. {
  310. struct pci_dev *dev = efx->pci_dev;
  311. unsigned int vfs_assigned = 0;
  312. vfs_assigned = pci_vfs_assigned(dev);
  313. if (vfs_assigned && !force) {
  314. netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
  315. "please detach them before disabling SR-IOV\n");
  316. return -EBUSY;
  317. }
  318. if (!vfs_assigned)
  319. pci_disable_sriov(dev);
  320. efx_ef10_sriov_free_vf_vswitching(efx);
  321. efx->vf_count = 0;
  322. return 0;
  323. }
  324. int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
  325. {
  326. if (num_vfs == 0)
  327. return efx_ef10_pci_sriov_disable(efx, false);
  328. else
  329. return efx_ef10_pci_sriov_enable(efx, num_vfs);
  330. }
  331. int efx_ef10_sriov_init(struct efx_nic *efx)
  332. {
  333. return 0;
  334. }
  335. void efx_ef10_sriov_fini(struct efx_nic *efx)
  336. {
  337. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  338. unsigned int i;
  339. int rc;
  340. if (!nic_data->vf) {
  341. /* Remove any un-assigned orphaned VFs */
  342. if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
  343. pci_disable_sriov(efx->pci_dev);
  344. return;
  345. }
  346. /* Remove any VFs in the host */
  347. for (i = 0; i < efx->vf_count; ++i) {
  348. struct efx_nic *vf_efx = nic_data->vf[i].efx;
  349. if (vf_efx)
  350. vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
  351. }
  352. rc = efx_ef10_pci_sriov_disable(efx, true);
  353. if (rc)
  354. netif_dbg(efx, drv, efx->net_dev,
  355. "Disabling SRIOV was not successful rc=%d\n", rc);
  356. else
  357. netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
  358. }
  359. static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
  360. u8 *mac)
  361. {
  362. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
  363. MCDI_DECLARE_BUF_ERR(outbuf);
  364. size_t outlen;
  365. int rc;
  366. MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
  367. ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
  368. rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
  369. sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
  370. return rc;
  371. }
  372. int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
  373. {
  374. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  375. struct ef10_vf *vf;
  376. int rc;
  377. if (!nic_data->vf)
  378. return -EOPNOTSUPP;
  379. if (vf_i >= efx->vf_count)
  380. return -EINVAL;
  381. vf = nic_data->vf + vf_i;
  382. if (vf->efx) {
  383. efx_device_detach_sync(vf->efx);
  384. efx_net_stop(vf->efx->net_dev);
  385. down_write(&vf->efx->filter_sem);
  386. vf->efx->type->filter_table_remove(vf->efx);
  387. rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
  388. if (rc) {
  389. up_write(&vf->efx->filter_sem);
  390. return rc;
  391. }
  392. }
  393. rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
  394. if (rc)
  395. return rc;
  396. if (!is_zero_ether_addr(vf->mac)) {
  397. rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
  398. if (rc)
  399. return rc;
  400. }
  401. if (!is_zero_ether_addr(mac)) {
  402. rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
  403. if (rc) {
  404. eth_zero_addr(vf->mac);
  405. goto fail;
  406. }
  407. if (vf->efx)
  408. ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
  409. }
  410. ether_addr_copy(vf->mac, mac);
  411. rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
  412. if (rc)
  413. goto fail;
  414. if (vf->efx) {
  415. /* VF cannot use the vport_id that the PF created */
  416. rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
  417. if (rc) {
  418. up_write(&vf->efx->filter_sem);
  419. return rc;
  420. }
  421. vf->efx->type->filter_table_probe(vf->efx);
  422. up_write(&vf->efx->filter_sem);
  423. efx_net_open(vf->efx->net_dev);
  424. netif_device_attach(vf->efx->net_dev);
  425. }
  426. return 0;
  427. fail:
  428. memset(vf->mac, 0, ETH_ALEN);
  429. return rc;
  430. }
  431. int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
  432. u8 qos)
  433. {
  434. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  435. struct ef10_vf *vf;
  436. u16 old_vlan, new_vlan;
  437. int rc = 0, rc2 = 0;
  438. if (vf_i >= efx->vf_count)
  439. return -EINVAL;
  440. if (qos != 0)
  441. return -EINVAL;
  442. vf = nic_data->vf + vf_i;
  443. new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
  444. if (new_vlan == vf->vlan)
  445. return 0;
  446. if (vf->efx) {
  447. efx_device_detach_sync(vf->efx);
  448. efx_net_stop(vf->efx->net_dev);
  449. down_write(&vf->efx->filter_sem);
  450. vf->efx->type->filter_table_remove(vf->efx);
  451. rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
  452. if (rc)
  453. goto restore_filters;
  454. }
  455. if (vf->vport_assigned) {
  456. rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
  457. if (rc) {
  458. netif_warn(efx, drv, efx->net_dev,
  459. "Failed to change vlan on VF %d.\n", vf_i);
  460. netif_warn(efx, drv, efx->net_dev,
  461. "This is likely because the VF is bound to a driver in a VM.\n");
  462. netif_warn(efx, drv, efx->net_dev,
  463. "Please unload the driver in the VM.\n");
  464. goto restore_vadaptor;
  465. }
  466. vf->vport_assigned = 0;
  467. }
  468. if (!is_zero_ether_addr(vf->mac)) {
  469. rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
  470. if (rc)
  471. goto restore_evb_port;
  472. }
  473. if (vf->vport_id) {
  474. rc = efx_ef10_vport_free(efx, vf->vport_id);
  475. if (rc)
  476. goto restore_mac;
  477. vf->vport_id = 0;
  478. }
  479. /* Do the actual vlan change */
  480. old_vlan = vf->vlan;
  481. vf->vlan = new_vlan;
  482. /* Restore everything in reverse order */
  483. rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
  484. MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
  485. vf->vlan, &vf->vport_id);
  486. if (rc)
  487. goto reset_nic_up_write;
  488. restore_mac:
  489. if (!is_zero_ether_addr(vf->mac)) {
  490. rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
  491. if (rc2) {
  492. eth_zero_addr(vf->mac);
  493. goto reset_nic_up_write;
  494. }
  495. }
  496. restore_evb_port:
  497. rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
  498. if (rc2)
  499. goto reset_nic_up_write;
  500. else
  501. vf->vport_assigned = 1;
  502. restore_vadaptor:
  503. if (vf->efx) {
  504. rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
  505. if (rc2)
  506. goto reset_nic_up_write;
  507. }
  508. restore_filters:
  509. if (vf->efx) {
  510. rc2 = vf->efx->type->filter_table_probe(vf->efx);
  511. if (rc2)
  512. goto reset_nic_up_write;
  513. up_write(&vf->efx->filter_sem);
  514. up_write(&vf->efx->filter_sem);
  515. rc2 = efx_net_open(vf->efx->net_dev);
  516. if (rc2)
  517. goto reset_nic;
  518. netif_device_attach(vf->efx->net_dev);
  519. }
  520. return rc;
  521. reset_nic_up_write:
  522. if (vf->efx)
  523. up_write(&vf->efx->filter_sem);
  524. reset_nic:
  525. if (vf->efx) {
  526. netif_err(efx, drv, efx->net_dev,
  527. "Failed to restore VF - scheduling reset.\n");
  528. efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
  529. } else {
  530. netif_err(efx, drv, efx->net_dev,
  531. "Failed to restore the VF and cannot reset the VF "
  532. "- VF is not functional.\n");
  533. netif_err(efx, drv, efx->net_dev,
  534. "Please reload the driver attached to the VF.\n");
  535. }
  536. return rc ? rc : rc2;
  537. }
  538. int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
  539. bool spoofchk)
  540. {
  541. return spoofchk ? -EOPNOTSUPP : 0;
  542. }
  543. int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
  544. int link_state)
  545. {
  546. MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
  547. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  548. BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
  549. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
  550. BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
  551. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
  552. BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
  553. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
  554. MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
  555. LINK_STATE_MODE_IN_FUNCTION_PF,
  556. nic_data->pf_index,
  557. LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
  558. MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
  559. return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
  560. NULL, 0, NULL); /* don't care what old mode was */
  561. }
  562. int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
  563. struct ifla_vf_info *ivf)
  564. {
  565. MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
  566. MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
  567. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  568. struct ef10_vf *vf;
  569. size_t outlen;
  570. int rc;
  571. if (vf_i >= efx->vf_count)
  572. return -EINVAL;
  573. if (!nic_data->vf)
  574. return -EOPNOTSUPP;
  575. vf = nic_data->vf + vf_i;
  576. ivf->vf = vf_i;
  577. ivf->min_tx_rate = 0;
  578. ivf->max_tx_rate = 0;
  579. ether_addr_copy(ivf->mac, vf->mac);
  580. ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
  581. ivf->qos = 0;
  582. MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
  583. LINK_STATE_MODE_IN_FUNCTION_PF,
  584. nic_data->pf_index,
  585. LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
  586. MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
  587. MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
  588. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
  589. outbuf, sizeof(outbuf), &outlen);
  590. if (rc)
  591. return rc;
  592. if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
  593. return -EIO;
  594. ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
  595. return 0;
  596. }
  597. int efx_ef10_sriov_get_phys_port_id(struct efx_nic *efx,
  598. struct netdev_phys_item_id *ppid)
  599. {
  600. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  601. if (!is_valid_ether_addr(nic_data->port_id))
  602. return -EOPNOTSUPP;
  603. ppid->id_len = ETH_ALEN;
  604. memcpy(ppid->id, nic_data->port_id, ppid->id_len);
  605. return 0;
  606. }