fm10k_iov.c 14 KB

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