fm10k_iov.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2015 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 mailboxes to process */
  47. if (!ACCESS_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 mailboxes to process */
  82. if (!ACCESS_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. /* process the SM mailbox first to drain outgoing messages */
  104. hw->mbx.ops.process(hw, &hw->mbx);
  105. /* verify port mapping is valid, if not reset port */
  106. if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
  107. hw->iov.ops.reset_lport(hw, vf_info);
  108. /* reset VFs that have mailbox timed out */
  109. if (!mbx->timeout) {
  110. hw->iov.ops.reset_resources(hw, vf_info);
  111. mbx->ops.connect(hw, mbx);
  112. }
  113. /* guarantee we have free space in the SM mailbox */
  114. if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU)) {
  115. /* keep track of how many times this occurs */
  116. interface->hw_sm_mbx_full++;
  117. break;
  118. }
  119. /* cleanup mailbox and process received messages */
  120. mbx->ops.process(hw, mbx);
  121. }
  122. /* if we stopped processing mailboxes early, update next_vf_mbx.
  123. * Otherwise, reset next_vf_mbx, and restart loop so that we process
  124. * the remaining mailboxes we skipped at the start.
  125. */
  126. if (i >= 0) {
  127. iov_data->next_vf_mbx = i + 1;
  128. } else if (iov_data->next_vf_mbx) {
  129. iov_data->next_vf_mbx = 0;
  130. goto process_mbx;
  131. }
  132. /* free the lock */
  133. fm10k_mbx_unlock(interface);
  134. read_unlock:
  135. rcu_read_unlock();
  136. return 0;
  137. }
  138. void fm10k_iov_suspend(struct pci_dev *pdev)
  139. {
  140. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  141. struct fm10k_iov_data *iov_data = interface->iov_data;
  142. struct fm10k_hw *hw = &interface->hw;
  143. int num_vfs, i;
  144. /* pull out num_vfs from iov_data */
  145. num_vfs = iov_data ? iov_data->num_vfs : 0;
  146. /* shut down queue mapping for VFs */
  147. fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
  148. FM10K_DGLORTMAP_NONE);
  149. /* Stop any active VFs and reset their resources */
  150. for (i = 0; i < num_vfs; i++) {
  151. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  152. hw->iov.ops.reset_resources(hw, vf_info);
  153. hw->iov.ops.reset_lport(hw, vf_info);
  154. }
  155. }
  156. int fm10k_iov_resume(struct pci_dev *pdev)
  157. {
  158. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  159. struct fm10k_iov_data *iov_data = interface->iov_data;
  160. struct fm10k_dglort_cfg dglort = { 0 };
  161. struct fm10k_hw *hw = &interface->hw;
  162. int num_vfs, i;
  163. /* pull out num_vfs from iov_data */
  164. num_vfs = iov_data ? iov_data->num_vfs : 0;
  165. /* return error if iov_data is not already populated */
  166. if (!iov_data)
  167. return -ENOMEM;
  168. /* allocate hardware resources for the VFs */
  169. hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
  170. /* configure DGLORT mapping for RSS */
  171. dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
  172. dglort.idx = fm10k_dglort_vf_rss;
  173. dglort.inner_rss = 1;
  174. dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
  175. dglort.queue_b = fm10k_vf_queue_index(hw, 0);
  176. dglort.vsi_l = fls(hw->iov.total_vfs - 1);
  177. dglort.vsi_b = 1;
  178. hw->mac.ops.configure_dglort_map(hw, &dglort);
  179. /* assign resources to the device */
  180. for (i = 0; i < num_vfs; i++) {
  181. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  182. /* allocate all but the last GLORT to the VFs */
  183. if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
  184. break;
  185. /* assign GLORT to VF, and restrict it to multicast */
  186. hw->iov.ops.set_lport(hw, vf_info, i,
  187. FM10K_VF_FLAG_MULTI_CAPABLE);
  188. /* mailbox is disconnected so we don't send a message */
  189. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  190. /* now we are ready so we can connect */
  191. vf_info->mbx.ops.connect(hw, &vf_info->mbx);
  192. }
  193. return 0;
  194. }
  195. s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
  196. {
  197. struct fm10k_iov_data *iov_data = interface->iov_data;
  198. struct fm10k_hw *hw = &interface->hw;
  199. struct fm10k_vf_info *vf_info;
  200. u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
  201. /* no IOV support, not our message to process */
  202. if (!iov_data)
  203. return FM10K_ERR_PARAM;
  204. /* glort outside our range, not our message to process */
  205. if (vf_idx >= iov_data->num_vfs)
  206. return FM10K_ERR_PARAM;
  207. /* determine if an update has occurred and if so notify the VF */
  208. vf_info = &iov_data->vf_info[vf_idx];
  209. if (vf_info->sw_vid != pvid) {
  210. vf_info->sw_vid = pvid;
  211. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  212. }
  213. return 0;
  214. }
  215. static void fm10k_iov_free_data(struct pci_dev *pdev)
  216. {
  217. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  218. if (!interface->iov_data)
  219. return;
  220. /* reclaim hardware resources */
  221. fm10k_iov_suspend(pdev);
  222. /* drop iov_data from interface */
  223. kfree_rcu(interface->iov_data, rcu);
  224. interface->iov_data = NULL;
  225. }
  226. static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
  227. {
  228. struct fm10k_intfc *interface = pci_get_drvdata(pdev);
  229. struct fm10k_iov_data *iov_data = interface->iov_data;
  230. struct fm10k_hw *hw = &interface->hw;
  231. size_t size;
  232. int i, err;
  233. /* return error if iov_data is already populated */
  234. if (iov_data)
  235. return -EBUSY;
  236. /* The PF should always be able to assign resources */
  237. if (!hw->iov.ops.assign_resources)
  238. return -ENODEV;
  239. /* nothing to do if no VFs are requested */
  240. if (!num_vfs)
  241. return 0;
  242. /* allocate memory for VF storage */
  243. size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
  244. iov_data = kzalloc(size, GFP_KERNEL);
  245. if (!iov_data)
  246. return -ENOMEM;
  247. /* record number of VFs */
  248. iov_data->num_vfs = num_vfs;
  249. /* loop through vf_info structures initializing each entry */
  250. for (i = 0; i < num_vfs; i++) {
  251. struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
  252. /* Record VF VSI value */
  253. vf_info->vsi = i + 1;
  254. vf_info->vf_idx = i;
  255. /* initialize mailbox memory */
  256. err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
  257. if (err) {
  258. dev_err(&pdev->dev,
  259. "Unable to initialize SR-IOV mailbox\n");
  260. kfree(iov_data);
  261. return err;
  262. }
  263. }
  264. /* assign iov_data to interface */
  265. interface->iov_data = iov_data;
  266. /* allocate hardware resources for the VFs */
  267. fm10k_iov_resume(pdev);
  268. return 0;
  269. }
  270. void fm10k_iov_disable(struct pci_dev *pdev)
  271. {
  272. if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
  273. dev_err(&pdev->dev,
  274. "Cannot disable SR-IOV while VFs are assigned\n");
  275. else
  276. pci_disable_sriov(pdev);
  277. fm10k_iov_free_data(pdev);
  278. }
  279. static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
  280. {
  281. u32 err_sev;
  282. int pos;
  283. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  284. if (!pos)
  285. return;
  286. pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
  287. err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
  288. pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
  289. }
  290. int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
  291. {
  292. int current_vfs = pci_num_vf(pdev);
  293. int err = 0;
  294. if (current_vfs && pci_vfs_assigned(pdev)) {
  295. dev_err(&pdev->dev,
  296. "Cannot modify SR-IOV while VFs are assigned\n");
  297. num_vfs = current_vfs;
  298. } else {
  299. pci_disable_sriov(pdev);
  300. fm10k_iov_free_data(pdev);
  301. }
  302. /* allocate resources for the VFs */
  303. err = fm10k_iov_alloc_data(pdev, num_vfs);
  304. if (err)
  305. return err;
  306. /* allocate VFs if not already allocated */
  307. if (num_vfs && (num_vfs != current_vfs)) {
  308. /* Disable completer abort error reporting as
  309. * the VFs can trigger this any time they read a queue
  310. * that they don't own.
  311. */
  312. fm10k_disable_aer_comp_abort(pdev);
  313. err = pci_enable_sriov(pdev, num_vfs);
  314. if (err) {
  315. dev_err(&pdev->dev,
  316. "Enable PCI SR-IOV failed: %d\n", err);
  317. return err;
  318. }
  319. }
  320. return num_vfs;
  321. }
  322. static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
  323. struct fm10k_vf_info *vf_info)
  324. {
  325. struct fm10k_hw *hw = &interface->hw;
  326. /* assigning the MAC address will send a mailbox message */
  327. fm10k_mbx_lock(interface);
  328. /* disable LPORT for this VF which clears switch rules */
  329. hw->iov.ops.reset_lport(hw, vf_info);
  330. /* assign new MAC+VLAN for this VF */
  331. hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
  332. /* re-enable the LPORT for this VF */
  333. hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
  334. FM10K_VF_FLAG_MULTI_CAPABLE);
  335. fm10k_mbx_unlock(interface);
  336. }
  337. int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
  338. {
  339. struct fm10k_intfc *interface = netdev_priv(netdev);
  340. struct fm10k_iov_data *iov_data = interface->iov_data;
  341. struct fm10k_vf_info *vf_info;
  342. /* verify SR-IOV is active and that vf idx is valid */
  343. if (!iov_data || vf_idx >= iov_data->num_vfs)
  344. return -EINVAL;
  345. /* verify MAC addr is valid */
  346. if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
  347. return -EINVAL;
  348. /* record new MAC address */
  349. vf_info = &iov_data->vf_info[vf_idx];
  350. ether_addr_copy(vf_info->mac, mac);
  351. fm10k_reset_vf_info(interface, vf_info);
  352. return 0;
  353. }
  354. int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
  355. u8 qos)
  356. {
  357. struct fm10k_intfc *interface = netdev_priv(netdev);
  358. struct fm10k_iov_data *iov_data = interface->iov_data;
  359. struct fm10k_hw *hw = &interface->hw;
  360. struct fm10k_vf_info *vf_info;
  361. /* verify SR-IOV is active and that vf idx is valid */
  362. if (!iov_data || vf_idx >= iov_data->num_vfs)
  363. return -EINVAL;
  364. /* QOS is unsupported and VLAN IDs accepted range 0-4094 */
  365. if (qos || (vid > (VLAN_VID_MASK - 1)))
  366. return -EINVAL;
  367. vf_info = &iov_data->vf_info[vf_idx];
  368. /* exit if there is nothing to do */
  369. if (vf_info->pf_vid == vid)
  370. return 0;
  371. /* record default VLAN ID for VF */
  372. vf_info->pf_vid = vid;
  373. /* Clear the VLAN table for the VF */
  374. hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
  375. fm10k_reset_vf_info(interface, vf_info);
  376. return 0;
  377. }
  378. int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
  379. int __always_unused unused, int rate)
  380. {
  381. struct fm10k_intfc *interface = netdev_priv(netdev);
  382. struct fm10k_iov_data *iov_data = interface->iov_data;
  383. struct fm10k_hw *hw = &interface->hw;
  384. /* verify SR-IOV is active and that vf idx is valid */
  385. if (!iov_data || vf_idx >= iov_data->num_vfs)
  386. return -EINVAL;
  387. /* rate limit cannot be less than 10Mbs or greater than link speed */
  388. if (rate && ((rate < FM10K_VF_TC_MIN) || rate > FM10K_VF_TC_MAX))
  389. return -EINVAL;
  390. /* store values */
  391. iov_data->vf_info[vf_idx].rate = rate;
  392. /* update hardware configuration */
  393. hw->iov.ops.configure_tc(hw, vf_idx, rate);
  394. return 0;
  395. }
  396. int fm10k_ndo_get_vf_config(struct net_device *netdev,
  397. int vf_idx, struct ifla_vf_info *ivi)
  398. {
  399. struct fm10k_intfc *interface = netdev_priv(netdev);
  400. struct fm10k_iov_data *iov_data = interface->iov_data;
  401. struct fm10k_vf_info *vf_info;
  402. /* verify SR-IOV is active and that vf idx is valid */
  403. if (!iov_data || vf_idx >= iov_data->num_vfs)
  404. return -EINVAL;
  405. vf_info = &iov_data->vf_info[vf_idx];
  406. ivi->vf = vf_idx;
  407. ivi->max_tx_rate = vf_info->rate;
  408. ivi->min_tx_rate = 0;
  409. ether_addr_copy(ivi->mac, vf_info->mac);
  410. ivi->vlan = vf_info->pf_vid;
  411. ivi->qos = 0;
  412. return 0;
  413. }