fm10k_iov.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* Intel(R) Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2016 Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  16. * Contact Information:
  17. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. */
  20. #include "fm10k.h"
  21. #include "fm10k_vf.h"
  22. #include "fm10k_pf.h"
  23. static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results,
  24. struct fm10k_mbx_info *mbx)
  25. {
  26. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  27. struct fm10k_intfc *interface = hw->back;
  28. struct pci_dev *pdev = interface->pdev;
  29. dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n",
  30. **results & FM10K_TLV_ID_MASK, vf_info->vf_idx);
  31. return fm10k_tlv_msg_error(hw, results, mbx);
  32. }
  33. static const struct fm10k_msg_data iov_mbx_data[] = {
  34. FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
  35. FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf),
  36. FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_mac_vlan_pf),
  37. FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf),
  38. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_iov_msg_error),
  39. };
  40. s32 fm10k_iov_event(struct fm10k_intfc *interface)
  41. {
  42. struct fm10k_hw *hw = &interface->hw;
  43. struct fm10k_iov_data *iov_data;
  44. s64 vflre;
  45. int i;
  46. /* if there is no iov_data then there is no mailbox to process */
  47. if (!READ_ONCE(interface->iov_data))
  48. return 0;
  49. rcu_read_lock();
  50. iov_data = interface->iov_data;
  51. /* check again now that we are in the RCU block */
  52. if (!iov_data)
  53. goto read_unlock;
  54. if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
  55. goto read_unlock;
  56. /* read VFLRE to determine if any VFs have been reset */
  57. do {
  58. vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(0));
  59. vflre <<= 32;
  60. vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(1));
  61. vflre = (vflre << 32) | (vflre >> 32);
  62. vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(0));
  63. i = iov_data->num_vfs;
  64. for (vflre <<= 64 - i; vflre && i--; vflre += vflre) {
  65. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  66. if (vflre >= 0)
  67. continue;
  68. hw->iov.ops.reset_resources(hw, vf_info);
  69. vf_info->mbx.ops.connect(hw, &vf_info->mbx);
  70. }
  71. } while (i != iov_data->num_vfs);
  72. read_unlock:
  73. rcu_read_unlock();
  74. return 0;
  75. }
  76. s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
  77. {
  78. struct fm10k_hw *hw = &interface->hw;
  79. struct fm10k_iov_data *iov_data;
  80. int i;
  81. /* if there is no iov_data then there is no mailbox to process */
  82. if (!READ_ONCE(interface->iov_data))
  83. return 0;
  84. rcu_read_lock();
  85. iov_data = interface->iov_data;
  86. /* check again now that we are in the RCU block */
  87. if (!iov_data)
  88. goto read_unlock;
  89. /* lock the mailbox for transmit and receive */
  90. fm10k_mbx_lock(interface);
  91. /* Most VF messages sent to the PF cause the PF to respond by
  92. * requesting from the SM mailbox. This means that too many VF
  93. * messages processed at once could cause a mailbox timeout on the PF.
  94. * To prevent this, store a pointer to the next VF mbx to process. Use
  95. * that as the start of the loop so that we don't starve whichever VF
  96. * got ignored on the previous run.
  97. */
  98. process_mbx:
  99. for (i = iov_data->next_vf_mbx ? : iov_data->num_vfs; i--;) {
  100. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  101. struct fm10k_mbx_info *mbx = &vf_info->mbx;
  102. u16 glort = vf_info->glort;
  103. /* verify port mapping is valid, if not reset port */
  104. if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
  105. hw->iov.ops.reset_lport(hw, vf_info);
  106. /* reset VFs that have mailbox timed out */
  107. if (!mbx->timeout) {
  108. hw->iov.ops.reset_resources(hw, vf_info);
  109. mbx->ops.connect(hw, mbx);
  110. }
  111. /* guarantee we have free space in the SM mailbox */
  112. if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU)) {
  113. /* keep track of how many times this occurs */
  114. interface->hw_sm_mbx_full++;
  115. break;
  116. }
  117. /* cleanup mailbox and process received messages */
  118. mbx->ops.process(hw, mbx);
  119. }
  120. /* if we stopped processing mailboxes early, update next_vf_mbx.
  121. * Otherwise, reset next_vf_mbx, and restart loop so that we process
  122. * the remaining mailboxes we skipped at the start.
  123. */
  124. if (i >= 0) {
  125. iov_data->next_vf_mbx = i + 1;
  126. } else if (iov_data->next_vf_mbx) {
  127. iov_data->next_vf_mbx = 0;
  128. goto process_mbx;
  129. }
  130. /* free the lock */
  131. fm10k_mbx_unlock(interface);
  132. read_unlock:
  133. rcu_read_unlock();
  134. return 0;
  135. }
  136. void fm10k_iov_suspend(struct pci_dev *pdev)
  137. {
  138. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  139. struct fm10k_iov_data *iov_data = interface->iov_data;
  140. struct fm10k_hw *hw = &interface->hw;
  141. int num_vfs, i;
  142. /* pull out num_vfs from iov_data */
  143. num_vfs = iov_data ? iov_data->num_vfs : 0;
  144. /* shut down queue mapping for VFs */
  145. fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
  146. FM10K_DGLORTMAP_NONE);
  147. /* Stop any active VFs and reset their resources */
  148. for (i = 0; i < num_vfs; i++) {
  149. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  150. hw->iov.ops.reset_resources(hw, vf_info);
  151. hw->iov.ops.reset_lport(hw, vf_info);
  152. }
  153. }
  154. int fm10k_iov_resume(struct pci_dev *pdev)
  155. {
  156. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  157. struct fm10k_iov_data *iov_data = interface->iov_data;
  158. struct fm10k_dglort_cfg dglort = { 0 };
  159. struct fm10k_hw *hw = &interface->hw;
  160. int num_vfs, i;
  161. /* pull out num_vfs from iov_data */
  162. num_vfs = iov_data ? iov_data->num_vfs : 0;
  163. /* return error if iov_data is not already populated */
  164. if (!iov_data)
  165. return -ENOMEM;
  166. /* allocate hardware resources for the VFs */
  167. hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
  168. /* configure DGLORT mapping for RSS */
  169. dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
  170. dglort.idx = fm10k_dglort_vf_rss;
  171. dglort.inner_rss = 1;
  172. dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
  173. dglort.queue_b = fm10k_vf_queue_index(hw, 0);
  174. dglort.vsi_l = fls(hw->iov.total_vfs - 1);
  175. dglort.vsi_b = 1;
  176. hw->mac.ops.configure_dglort_map(hw, &dglort);
  177. /* assign resources to the device */
  178. for (i = 0; i < num_vfs; i++) {
  179. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  180. /* allocate all but the last GLORT to the VFs */
  181. if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
  182. break;
  183. /* assign GLORT to VF, and restrict it to multicast */
  184. hw->iov.ops.set_lport(hw, vf_info, i,
  185. FM10K_VF_FLAG_MULTI_CAPABLE);
  186. /* mailbox is disconnected so we don't send a message */
  187. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  188. /* now we are ready so we can connect */
  189. vf_info->mbx.ops.connect(hw, &vf_info->mbx);
  190. }
  191. return 0;
  192. }
  193. s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
  194. {
  195. struct fm10k_iov_data *iov_data = interface->iov_data;
  196. struct fm10k_hw *hw = &interface->hw;
  197. struct fm10k_vf_info *vf_info;
  198. u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
  199. /* no IOV support, not our message to process */
  200. if (!iov_data)
  201. return FM10K_ERR_PARAM;
  202. /* glort outside our range, not our message to process */
  203. if (vf_idx >= iov_data->num_vfs)
  204. return FM10K_ERR_PARAM;
  205. /* determine if an update has occurred and if so notify the VF */
  206. vf_info = &iov_data->vf_info[vf_idx];
  207. if (vf_info->sw_vid != pvid) {
  208. vf_info->sw_vid = pvid;
  209. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  210. }
  211. return 0;
  212. }
  213. static void fm10k_iov_free_data(struct pci_dev *pdev)
  214. {
  215. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  216. if (!interface->iov_data)
  217. return;
  218. /* reclaim hardware resources */
  219. fm10k_iov_suspend(pdev);
  220. /* drop iov_data from interface */
  221. kfree_rcu(interface->iov_data, rcu);
  222. interface->iov_data = NULL;
  223. }
  224. static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
  225. {
  226. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  227. struct fm10k_iov_data *iov_data = interface->iov_data;
  228. struct fm10k_hw *hw = &interface->hw;
  229. size_t size;
  230. int i, err;
  231. /* return error if iov_data is already populated */
  232. if (iov_data)
  233. return -EBUSY;
  234. /* The PF should always be able to assign resources */
  235. if (!hw->iov.ops.assign_resources)
  236. return -ENODEV;
  237. /* nothing to do if no VFs are requested */
  238. if (!num_vfs)
  239. return 0;
  240. /* allocate memory for VF storage */
  241. size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
  242. iov_data = kzalloc(size, GFP_KERNEL);
  243. if (!iov_data)
  244. return -ENOMEM;
  245. /* record number of VFs */
  246. iov_data->num_vfs = num_vfs;
  247. /* loop through vf_info structures initializing each entry */
  248. for (i = 0; i < num_vfs; i++) {
  249. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  250. /* Record VF VSI value */
  251. vf_info->vsi = i + 1;
  252. vf_info->vf_idx = i;
  253. /* initialize mailbox memory */
  254. err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
  255. if (err) {
  256. dev_err(&pdev->dev,
  257. "Unable to initialize SR-IOV mailbox\n");
  258. kfree(iov_data);
  259. return err;
  260. }
  261. }
  262. /* assign iov_data to interface */
  263. interface->iov_data = iov_data;
  264. /* allocate hardware resources for the VFs */
  265. fm10k_iov_resume(pdev);
  266. return 0;
  267. }
  268. void fm10k_iov_disable(struct pci_dev *pdev)
  269. {
  270. if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
  271. dev_err(&pdev->dev,
  272. "Cannot disable SR-IOV while VFs are assigned\n");
  273. else
  274. pci_disable_sriov(pdev);
  275. fm10k_iov_free_data(pdev);
  276. }
  277. static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
  278. {
  279. u32 err_sev;
  280. int pos;
  281. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  282. if (!pos)
  283. return;
  284. pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
  285. err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
  286. pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
  287. }
  288. int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
  289. {
  290. int current_vfs = pci_num_vf(pdev);
  291. int err = 0;
  292. if (current_vfs && pci_vfs_assigned(pdev)) {
  293. dev_err(&pdev->dev,
  294. "Cannot modify SR-IOV while VFs are assigned\n");
  295. num_vfs = current_vfs;
  296. } else {
  297. pci_disable_sriov(pdev);
  298. fm10k_iov_free_data(pdev);
  299. }
  300. /* allocate resources for the VFs */
  301. err = fm10k_iov_alloc_data(pdev, num_vfs);
  302. if (err)
  303. return err;
  304. /* allocate VFs if not already allocated */
  305. if (num_vfs && (num_vfs != current_vfs)) {
  306. /* Disable completer abort error reporting as
  307. * the VFs can trigger this any time they read a queue
  308. * that they don't own.
  309. */
  310. fm10k_disable_aer_comp_abort(pdev);
  311. err = pci_enable_sriov(pdev, num_vfs);
  312. if (err) {
  313. dev_err(&pdev->dev,
  314. "Enable PCI SR-IOV failed: %d\n", err);
  315. return err;
  316. }
  317. }
  318. return num_vfs;
  319. }
  320. static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
  321. struct fm10k_vf_info *vf_info)
  322. {
  323. struct fm10k_hw *hw = &interface->hw;
  324. /* assigning the MAC address will send a mailbox message */
  325. fm10k_mbx_lock(interface);
  326. /* disable LPORT for this VF which clears switch rules */
  327. hw->iov.ops.reset_lport(hw, vf_info);
  328. /* assign new MAC+VLAN for this VF */
  329. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  330. /* re-enable the LPORT for this VF */
  331. hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
  332. FM10K_VF_FLAG_MULTI_CAPABLE);
  333. fm10k_mbx_unlock(interface);
  334. }
  335. int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
  336. {
  337. struct fm10k_intfc *interface = netdev_priv(netdev);
  338. struct fm10k_iov_data *iov_data = interface->iov_data;
  339. struct fm10k_vf_info *vf_info;
  340. /* verify SR-IOV is active and that vf idx is valid */
  341. if (!iov_data || vf_idx >= iov_data->num_vfs)
  342. return -EINVAL;
  343. /* verify MAC addr is valid */
  344. if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
  345. return -EINVAL;
  346. /* record new MAC address */
  347. vf_info = &iov_data->vf_info[vf_idx];
  348. ether_addr_copy(vf_info->mac, mac);
  349. fm10k_reset_vf_info(interface, vf_info);
  350. return 0;
  351. }
  352. int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
  353. u8 qos, __be16 vlan_proto)
  354. {
  355. struct fm10k_intfc *interface = netdev_priv(netdev);
  356. struct fm10k_iov_data *iov_data = interface->iov_data;
  357. struct fm10k_hw *hw = &interface->hw;
  358. struct fm10k_vf_info *vf_info;
  359. /* verify SR-IOV is active and that vf idx is valid */
  360. if (!iov_data || vf_idx >= iov_data->num_vfs)
  361. return -EINVAL;
  362. /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
  363. if (qos || (vid > (VLAN_VID_MASK - 1)))
  364. return -EINVAL;
  365. /* VF VLAN Protocol part to default is unsupported */
  366. if (vlan_proto != htons(ETH_P_8021Q))
  367. return -EPROTONOSUPPORT;
  368. vf_info = &iov_data->vf_info[vf_idx];
  369. /* exit if there is nothing to do */
  370. if (vf_info->pf_vid == vid)
  371. return 0;
  372. /* record default VLAN ID for VF */
  373. vf_info->pf_vid = vid;
  374. /* Clear the VLAN table for the VF */
  375. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
  376. fm10k_reset_vf_info(interface, vf_info);
  377. return 0;
  378. }
  379. int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
  380. int __always_unused unused, int rate)
  381. {
  382. struct fm10k_intfc *interface = netdev_priv(netdev);
  383. struct fm10k_iov_data *iov_data = interface->iov_data;
  384. struct fm10k_hw *hw = &interface->hw;
  385. /* verify SR-IOV is active and that vf idx is valid */
  386. if (!iov_data || vf_idx >= iov_data->num_vfs)
  387. return -EINVAL;
  388. /* rate limit cannot be less than 10Mbs or greater than link speed */
  389. if (rate && ((rate < FM10K_VF_TC_MIN) || rate > FM10K_VF_TC_MAX))
  390. return -EINVAL;
  391. /* store values */
  392. iov_data->vf_info[vf_idx].rate = rate;
  393. /* update hardware configuration */
  394. hw->iov.ops.configure_tc(hw, vf_idx, rate);
  395. return 0;
  396. }
  397. int fm10k_ndo_get_vf_config(struct net_device *netdev,
  398. int vf_idx, struct ifla_vf_info *ivi)
  399. {
  400. struct fm10k_intfc *interface = netdev_priv(netdev);
  401. struct fm10k_iov_data *iov_data = interface->iov_data;
  402. struct fm10k_vf_info *vf_info;
  403. /* verify SR-IOV is active and that vf idx is valid */
  404. if (!iov_data || vf_idx >= iov_data->num_vfs)
  405. return -EINVAL;
  406. vf_info = &iov_data->vf_info[vf_idx];
  407. ivi->vf = vf_idx;
  408. ivi->max_tx_rate = vf_info->rate;
  409. ivi->min_tx_rate = 0;
  410. ether_addr_copy(ivi->mac, vf_info->mac);
  411. ivi->vlan = vf_info->pf_vid;
  412. ivi->qos = 0;
  413. return 0;
  414. }