ef10_sriov.c 20 KB

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