ef10_sriov.c 18 KB

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