fm10k_iov.c 14 KB

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