ef10_sriov.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 = kzalloc_objs(struct ef10_vf, efx->vf_count);
  157. if (!nic_data->vf)
  158. return -ENOMEM;
  159. for (i = 0; i < efx->vf_count; i++) {
  160. eth_random_addr(nic_data->vf[i].mac);
  161. nic_data->vf[i].efx = NULL;
  162. nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
  163. rc = efx_ef10_sriov_assign_vf_vport(efx, i);
  164. if (rc)
  165. goto fail;
  166. }
  167. return 0;
  168. fail:
  169. efx_ef10_sriov_free_vf_vswitching(efx);
  170. return rc;
  171. }
  172. static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
  173. {
  174. unsigned int i;
  175. int rc;
  176. for (i = 0; i < efx->vf_count; i++) {
  177. rc = efx_ef10_sriov_assign_vf_vport(efx, i);
  178. if (rc)
  179. goto fail;
  180. }
  181. return 0;
  182. fail:
  183. efx_ef10_sriov_free_vf_vswitching(efx);
  184. return rc;
  185. }
  186. static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
  187. {
  188. u32 port_flags;
  189. int rc;
  190. rc = efx_ef10_vadaptor_alloc(efx, efx->vport_id);
  191. if (rc)
  192. goto fail_vadaptor_alloc;
  193. rc = efx_ef10_vadaptor_query(efx, efx->vport_id,
  194. &port_flags, NULL, NULL);
  195. if (rc)
  196. goto fail_vadaptor_query;
  197. if (port_flags &
  198. (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
  199. efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  200. else
  201. efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
  202. return 0;
  203. fail_vadaptor_query:
  204. efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
  205. fail_vadaptor_alloc:
  206. return rc;
  207. }
  208. /* On top of the default firmware vswitch setup, create a VEB vswitch and
  209. * expansion vport for use by this function.
  210. */
  211. int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
  212. {
  213. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  214. struct net_device *net_dev = efx->net_dev;
  215. int rc;
  216. if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
  217. /* vswitch not needed as we have no VFs */
  218. efx_ef10_vadaptor_alloc_set_features(efx);
  219. return 0;
  220. }
  221. rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
  222. MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
  223. if (rc)
  224. goto fail1;
  225. rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
  226. MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
  227. EFX_EF10_NO_VLAN, &efx->vport_id);
  228. if (rc)
  229. goto fail2;
  230. rc = efx_ef10_vport_add_mac(efx, efx->vport_id, net_dev->dev_addr);
  231. if (rc)
  232. goto fail3;
  233. ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
  234. rc = efx_ef10_vadaptor_alloc_set_features(efx);
  235. if (rc)
  236. goto fail4;
  237. return 0;
  238. fail4:
  239. efx_ef10_vport_del_mac(efx, efx->vport_id, nic_data->vport_mac);
  240. eth_zero_addr(nic_data->vport_mac);
  241. fail3:
  242. efx_ef10_vport_free(efx, efx->vport_id);
  243. efx->vport_id = EVB_PORT_ID_ASSIGNED;
  244. fail2:
  245. efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
  246. fail1:
  247. return rc;
  248. }
  249. int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
  250. {
  251. return efx_ef10_vadaptor_alloc_set_features(efx);
  252. }
  253. int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
  254. {
  255. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  256. int rc;
  257. if (!nic_data->must_probe_vswitching)
  258. return 0;
  259. rc = efx_ef10_vswitching_probe_pf(efx);
  260. if (rc)
  261. goto fail;
  262. rc = efx_ef10_sriov_restore_vf_vswitching(efx);
  263. if (rc)
  264. goto fail;
  265. nic_data->must_probe_vswitching = false;
  266. fail:
  267. return rc;
  268. }
  269. int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
  270. {
  271. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  272. int rc;
  273. if (!nic_data->must_probe_vswitching)
  274. return 0;
  275. rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
  276. if (rc)
  277. return rc;
  278. nic_data->must_probe_vswitching = false;
  279. return 0;
  280. }
  281. void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
  282. {
  283. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  284. efx_ef10_sriov_free_vf_vswitching(efx);
  285. efx_ef10_vadaptor_free(efx, efx->vport_id);
  286. if (efx->vport_id == EVB_PORT_ID_ASSIGNED)
  287. return; /* No vswitch was ever created */
  288. if (!is_zero_ether_addr(nic_data->vport_mac)) {
  289. efx_ef10_vport_del_mac(efx, efx->vport_id,
  290. efx->net_dev->dev_addr);
  291. eth_zero_addr(nic_data->vport_mac);
  292. }
  293. efx_ef10_vport_free(efx, efx->vport_id);
  294. efx->vport_id = EVB_PORT_ID_ASSIGNED;
  295. /* Only free the vswitch if no VFs are assigned */
  296. if (!pci_vfs_assigned(efx->pci_dev))
  297. efx_ef10_vswitch_free(efx, efx->vport_id);
  298. }
  299. void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
  300. {
  301. efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
  302. }
  303. static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
  304. {
  305. int rc = 0;
  306. struct pci_dev *dev = efx->pci_dev;
  307. efx->vf_count = num_vfs;
  308. rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
  309. if (rc)
  310. goto fail1;
  311. rc = pci_enable_sriov(dev, num_vfs);
  312. if (rc)
  313. goto fail2;
  314. return 0;
  315. fail2:
  316. efx_ef10_sriov_free_vf_vswitching(efx);
  317. fail1:
  318. efx->vf_count = 0;
  319. netif_err(efx, probe, efx->net_dev,
  320. "Failed to enable SRIOV VFs\n");
  321. return rc;
  322. }
  323. /* Disable SRIOV and remove VFs
  324. * If some VFs are attached to a guest (using Xen, only) nothing is
  325. * done if force=false, and vports are freed if force=true (for the non
  326. * attachedc ones, only) but SRIOV is not disabled and VFs are not
  327. * removed in either case.
  328. */
  329. static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
  330. {
  331. struct pci_dev *dev = efx->pci_dev;
  332. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  333. unsigned int vfs_assigned = pci_vfs_assigned(dev);
  334. int i, rc = 0;
  335. if (vfs_assigned && !force) {
  336. netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
  337. "please detach them before disabling SR-IOV\n");
  338. return -EBUSY;
  339. }
  340. if (!vfs_assigned) {
  341. for (i = 0; i < efx->vf_count; i++)
  342. nic_data->vf[i].pci_dev = NULL;
  343. pci_disable_sriov(dev);
  344. } else {
  345. rc = -EBUSY;
  346. }
  347. efx_ef10_sriov_free_vf_vswitching(efx);
  348. efx->vf_count = 0;
  349. return rc;
  350. }
  351. int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
  352. {
  353. if (num_vfs == 0)
  354. return efx_ef10_pci_sriov_disable(efx, false);
  355. else
  356. return efx_ef10_pci_sriov_enable(efx, num_vfs);
  357. }
  358. int efx_ef10_sriov_init(struct efx_nic *efx)
  359. {
  360. return 0;
  361. }
  362. void efx_ef10_sriov_fini(struct efx_nic *efx)
  363. {
  364. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  365. int rc;
  366. if (!nic_data->vf) {
  367. /* Remove any un-assigned orphaned VFs. This can happen if the PF driver
  368. * was unloaded while any VF was assigned to a guest (using Xen, only).
  369. */
  370. if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
  371. pci_disable_sriov(efx->pci_dev);
  372. return;
  373. }
  374. /* Disable SRIOV and remove any VFs in the host */
  375. rc = efx_ef10_pci_sriov_disable(efx, true);
  376. if (rc)
  377. netif_dbg(efx, drv, efx->net_dev,
  378. "Disabling SRIOV was not successful rc=%d\n", rc);
  379. else
  380. netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
  381. }
  382. static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
  383. u8 *mac)
  384. {
  385. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
  386. MCDI_DECLARE_BUF_ERR(outbuf);
  387. size_t outlen;
  388. int rc;
  389. MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
  390. ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
  391. rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
  392. sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
  393. return rc;
  394. }
  395. int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, const u8 *mac)
  396. {
  397. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  398. struct ef10_vf *vf;
  399. int rc;
  400. if (!nic_data->vf)
  401. return -EOPNOTSUPP;
  402. if (vf_i >= efx->vf_count)
  403. return -EINVAL;
  404. vf = nic_data->vf + vf_i;
  405. if (vf->efx) {
  406. efx_device_detach_sync(vf->efx);
  407. efx_net_stop(vf->efx->net_dev);
  408. vf->efx->type->filter_table_remove(vf->efx);
  409. rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
  410. if (rc)
  411. return rc;
  412. }
  413. rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
  414. if (rc)
  415. return rc;
  416. if (!is_zero_ether_addr(vf->mac)) {
  417. rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
  418. if (rc)
  419. return rc;
  420. }
  421. if (!is_zero_ether_addr(mac)) {
  422. rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
  423. if (rc)
  424. goto fail;
  425. if (vf->efx)
  426. eth_hw_addr_set(vf->efx->net_dev, mac);
  427. }
  428. ether_addr_copy(vf->mac, mac);
  429. rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
  430. if (rc)
  431. goto fail;
  432. if (vf->efx) {
  433. /* VF cannot use the vport_id that the PF created */
  434. rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
  435. if (rc)
  436. return rc;
  437. vf->efx->type->filter_table_probe(vf->efx);
  438. efx_net_open(vf->efx->net_dev);
  439. efx_device_attach_if_not_resetting(vf->efx);
  440. }
  441. return 0;
  442. fail:
  443. eth_zero_addr(vf->mac);
  444. return rc;
  445. }
  446. int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
  447. u8 qos)
  448. {
  449. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  450. struct ef10_vf *vf;
  451. u16 new_vlan;
  452. int rc = 0, rc2 = 0;
  453. if (vf_i >= efx->vf_count)
  454. return -EINVAL;
  455. if (qos != 0)
  456. return -EINVAL;
  457. vf = nic_data->vf + vf_i;
  458. new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
  459. if (new_vlan == vf->vlan)
  460. return 0;
  461. if (vf->efx) {
  462. efx_device_detach_sync(vf->efx);
  463. efx_net_stop(vf->efx->net_dev);
  464. mutex_lock(&vf->efx->mac_lock);
  465. vf->efx->type->filter_table_remove(vf->efx);
  466. rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
  467. if (rc)
  468. goto restore_filters;
  469. }
  470. if (vf->vport_assigned) {
  471. rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
  472. if (rc) {
  473. netif_warn(efx, drv, efx->net_dev,
  474. "Failed to change vlan on VF %d.\n", vf_i);
  475. netif_warn(efx, drv, efx->net_dev,
  476. "This is likely because the VF is bound to a driver in a VM.\n");
  477. netif_warn(efx, drv, efx->net_dev,
  478. "Please unload the driver in the VM.\n");
  479. goto restore_vadaptor;
  480. }
  481. vf->vport_assigned = 0;
  482. }
  483. if (!is_zero_ether_addr(vf->mac)) {
  484. rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
  485. if (rc)
  486. goto restore_evb_port;
  487. }
  488. if (vf->vport_id) {
  489. rc = efx_ef10_vport_free(efx, vf->vport_id);
  490. if (rc)
  491. goto restore_mac;
  492. vf->vport_id = 0;
  493. }
  494. /* Do the actual vlan change */
  495. vf->vlan = new_vlan;
  496. /* Restore everything in reverse order */
  497. rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
  498. MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
  499. vf->vlan, &vf->vport_id);
  500. if (rc)
  501. goto reset_nic_up_write;
  502. restore_mac:
  503. if (!is_zero_ether_addr(vf->mac)) {
  504. rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
  505. if (rc2) {
  506. eth_zero_addr(vf->mac);
  507. goto reset_nic_up_write;
  508. }
  509. }
  510. restore_evb_port:
  511. rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
  512. if (rc2)
  513. goto reset_nic_up_write;
  514. else
  515. vf->vport_assigned = 1;
  516. restore_vadaptor:
  517. if (vf->efx) {
  518. rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
  519. if (rc2)
  520. goto reset_nic_up_write;
  521. }
  522. restore_filters:
  523. if (vf->efx) {
  524. rc2 = vf->efx->type->filter_table_probe(vf->efx);
  525. if (rc2)
  526. goto reset_nic_up_write;
  527. mutex_unlock(&vf->efx->mac_lock);
  528. rc2 = efx_net_open(vf->efx->net_dev);
  529. if (rc2)
  530. goto reset_nic;
  531. efx_device_attach_if_not_resetting(vf->efx);
  532. }
  533. return rc;
  534. reset_nic_up_write:
  535. if (vf->efx)
  536. mutex_unlock(&vf->efx->mac_lock);
  537. reset_nic:
  538. if (vf->efx) {
  539. netif_err(efx, drv, efx->net_dev,
  540. "Failed to restore VF - scheduling reset.\n");
  541. efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
  542. } else {
  543. netif_err(efx, drv, efx->net_dev,
  544. "Failed to restore the VF and cannot reset the VF "
  545. "- VF is not functional.\n");
  546. netif_err(efx, drv, efx->net_dev,
  547. "Please reload the driver attached to the VF.\n");
  548. }
  549. return rc ? rc : rc2;
  550. }
  551. static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
  552. u32 mask, u32 value)
  553. {
  554. MCDI_DECLARE_BUF(pm_outbuf, MC_CMD_PRIVILEGE_MASK_OUT_LEN);
  555. MCDI_DECLARE_BUF(pm_inbuf, MC_CMD_PRIVILEGE_MASK_IN_LEN);
  556. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  557. u32 old_mask, new_mask;
  558. size_t outlen;
  559. int rc;
  560. EFX_WARN_ON_PARANOID((value & ~mask) != 0);
  561. /* Get privilege mask */
  562. MCDI_POPULATE_DWORD_2(pm_inbuf, PRIVILEGE_MASK_IN_FUNCTION,
  563. PRIVILEGE_MASK_IN_FUNCTION_PF, nic_data->pf_index,
  564. PRIVILEGE_MASK_IN_FUNCTION_VF, vf_i);
  565. rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
  566. pm_inbuf, sizeof(pm_inbuf),
  567. pm_outbuf, sizeof(pm_outbuf), &outlen);
  568. if (rc != 0)
  569. return rc;
  570. if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
  571. return -EIO;
  572. old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
  573. new_mask = old_mask & ~mask;
  574. new_mask |= value;
  575. if (new_mask == old_mask)
  576. return 0;
  577. new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
  578. /* Set privilege mask */
  579. MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
  580. rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
  581. pm_inbuf, sizeof(pm_inbuf),
  582. pm_outbuf, sizeof(pm_outbuf), &outlen);
  583. if (rc != 0)
  584. return rc;
  585. if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
  586. return -EIO;
  587. return 0;
  588. }
  589. int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
  590. {
  591. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  592. /* Can't enable spoofchk if firmware doesn't support it. */
  593. if (!(nic_data->datapath_caps &
  594. BIT(MC_CMD_GET_CAPABILITIES_OUT_TX_MAC_SECURITY_FILTERING_LBN)) &&
  595. spoofchk)
  596. return -EOPNOTSUPP;
  597. return efx_ef10_sriov_set_privilege_mask(efx, vf_i,
  598. MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX,
  599. spoofchk ? 0 : MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX);
  600. }
  601. int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
  602. int link_state)
  603. {
  604. MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
  605. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  606. BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
  607. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
  608. BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
  609. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
  610. BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
  611. MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
  612. MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
  613. LINK_STATE_MODE_IN_FUNCTION_PF,
  614. nic_data->pf_index,
  615. LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
  616. MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
  617. return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
  618. NULL, 0, NULL); /* don't care what old mode was */
  619. }
  620. int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
  621. struct ifla_vf_info *ivf)
  622. {
  623. MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
  624. MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
  625. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  626. struct ef10_vf *vf;
  627. size_t outlen;
  628. int rc;
  629. if (vf_i >= efx->vf_count)
  630. return -EINVAL;
  631. if (!nic_data->vf)
  632. return -EOPNOTSUPP;
  633. vf = nic_data->vf + vf_i;
  634. ivf->vf = vf_i;
  635. ivf->min_tx_rate = 0;
  636. ivf->max_tx_rate = 0;
  637. ether_addr_copy(ivf->mac, vf->mac);
  638. ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
  639. ivf->qos = 0;
  640. MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
  641. LINK_STATE_MODE_IN_FUNCTION_PF,
  642. nic_data->pf_index,
  643. LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
  644. MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
  645. MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
  646. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
  647. outbuf, sizeof(outbuf), &outlen);
  648. if (rc)
  649. return rc;
  650. if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
  651. return -EIO;
  652. ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
  653. return 0;
  654. }