ef10_sriov.c 19 KB

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