ef10_sriov.c 19 KB

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