PageRenderTime 62ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 3ms

/drivers/net/ethernet/intel/i40e/i40e_main.c

http://github.com/mirrors/linux-2.6
C | 15811 lines | 10302 code | 1975 blank | 3534 comment | 1980 complexity | 0f76330d2621dae3914651ca6b0cd824 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/of_net.h>
  5. #include <linux/pci.h>
  6. #include <linux/bpf.h>
  7. /* Local includes */
  8. #include "i40e.h"
  9. #include "i40e_diag.h"
  10. #include "i40e_xsk.h"
  11. #include <net/udp_tunnel.h>
  12. #include <net/xdp_sock.h>
  13. /* All i40e tracepoints are defined by the include below, which
  14. * must be included exactly once across the whole kernel with
  15. * CREATE_TRACE_POINTS defined
  16. */
  17. #define CREATE_TRACE_POINTS
  18. #include "i40e_trace.h"
  19. const char i40e_driver_name[] = "i40e";
  20. static const char i40e_driver_string[] =
  21. "Intel(R) Ethernet Connection XL710 Network Driver";
  22. #define DRV_KERN "-k"
  23. #define DRV_VERSION_MAJOR 2
  24. #define DRV_VERSION_MINOR 8
  25. #define DRV_VERSION_BUILD 20
  26. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  27. __stringify(DRV_VERSION_MINOR) "." \
  28. __stringify(DRV_VERSION_BUILD) DRV_KERN
  29. const char i40e_driver_version_str[] = DRV_VERSION;
  30. static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
  31. /* a bit of forward declarations */
  32. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  33. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  34. static int i40e_add_vsi(struct i40e_vsi *vsi);
  35. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  36. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  37. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  38. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  39. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  40. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  41. static int i40e_reset(struct i40e_pf *pf);
  42. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  43. static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
  44. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
  45. static bool i40e_check_recovery_mode(struct i40e_pf *pf);
  46. static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
  47. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  48. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  49. static int i40e_get_capabilities(struct i40e_pf *pf,
  50. enum i40e_admin_queue_opc list_type);
  51. /* i40e_pci_tbl - PCI Device ID Table
  52. *
  53. * Last entry must be all 0s
  54. *
  55. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  56. * Class, Class Mask, private data (not used) }
  57. */
  58. static const struct pci_device_id i40e_pci_tbl[] = {
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  83. /* required last entry */
  84. {0, }
  85. };
  86. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  87. #define I40E_MAX_VF_COUNT 128
  88. static int debug = -1;
  89. module_param(debug, uint, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  91. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_LICENSE("GPL v2");
  94. MODULE_VERSION(DRV_VERSION);
  95. static struct workqueue_struct *i40e_wq;
  96. /**
  97. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to fill out
  100. * @size: size of memory requested
  101. * @alignment: what to align the allocation to
  102. **/
  103. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  104. u64 size, u32 alignment)
  105. {
  106. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  107. mem->size = ALIGN(size, alignment);
  108. mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
  109. GFP_KERNEL);
  110. if (!mem->va)
  111. return -ENOMEM;
  112. return 0;
  113. }
  114. /**
  115. * i40e_free_dma_mem_d - OS specific memory free for shared code
  116. * @hw: pointer to the HW structure
  117. * @mem: ptr to mem struct to free
  118. **/
  119. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  120. {
  121. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  122. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  123. mem->va = NULL;
  124. mem->pa = 0;
  125. mem->size = 0;
  126. return 0;
  127. }
  128. /**
  129. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to fill out
  132. * @size: size of memory requested
  133. **/
  134. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  135. u32 size)
  136. {
  137. mem->size = size;
  138. mem->va = kzalloc(size, GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_virt_mem_d - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  149. {
  150. /* it's ok to kfree a NULL pointer */
  151. kfree(mem->va);
  152. mem->va = NULL;
  153. mem->size = 0;
  154. return 0;
  155. }
  156. /**
  157. * i40e_get_lump - find a lump of free generic resource
  158. * @pf: board private structure
  159. * @pile: the pile of resource to search
  160. * @needed: the number of items needed
  161. * @id: an owner id to stick on the items assigned
  162. *
  163. * Returns the base item index of the lump, or negative for error
  164. *
  165. * The search_hint trick and lack of advanced fit-finding only work
  166. * because we're highly likely to have all the same size lump requests.
  167. * Linear search time and any fragmentation should be minimal.
  168. **/
  169. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  170. u16 needed, u16 id)
  171. {
  172. int ret = -ENOMEM;
  173. int i, j;
  174. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  175. dev_info(&pf->pdev->dev,
  176. "param err: pile=%s needed=%d id=0x%04x\n",
  177. pile ? "<valid>" : "<null>", needed, id);
  178. return -EINVAL;
  179. }
  180. /* start the linear search with an imperfect hint */
  181. i = pile->search_hint;
  182. while (i < pile->num_entries) {
  183. /* skip already allocated entries */
  184. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  185. i++;
  186. continue;
  187. }
  188. /* do we have enough in this lump? */
  189. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  190. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  191. break;
  192. }
  193. if (j == needed) {
  194. /* there was enough, so assign it to the requestor */
  195. for (j = 0; j < needed; j++)
  196. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  197. ret = i;
  198. pile->search_hint = i + j;
  199. break;
  200. }
  201. /* not enough, so skip over it and continue looking */
  202. i += j;
  203. }
  204. return ret;
  205. }
  206. /**
  207. * i40e_put_lump - return a lump of generic resource
  208. * @pile: the pile of resource to search
  209. * @index: the base item index
  210. * @id: the owner id of the items assigned
  211. *
  212. * Returns the count of items in the lump
  213. **/
  214. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  215. {
  216. int valid_id = (id | I40E_PILE_VALID_BIT);
  217. int count = 0;
  218. int i;
  219. if (!pile || index >= pile->num_entries)
  220. return -EINVAL;
  221. for (i = index;
  222. i < pile->num_entries && pile->list[i] == valid_id;
  223. i++) {
  224. pile->list[i] = 0;
  225. count++;
  226. }
  227. if (count && index < pile->search_hint)
  228. pile->search_hint = index;
  229. return count;
  230. }
  231. /**
  232. * i40e_find_vsi_from_id - searches for the vsi with the given id
  233. * @pf: the pf structure to search for the vsi
  234. * @id: id of the vsi it is searching for
  235. **/
  236. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  237. {
  238. int i;
  239. for (i = 0; i < pf->num_alloc_vsi; i++)
  240. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  241. return pf->vsi[i];
  242. return NULL;
  243. }
  244. /**
  245. * i40e_service_event_schedule - Schedule the service task to wake up
  246. * @pf: board private structure
  247. *
  248. * If not already scheduled, this puts the task into the work queue
  249. **/
  250. void i40e_service_event_schedule(struct i40e_pf *pf)
  251. {
  252. if ((!test_bit(__I40E_DOWN, pf->state) &&
  253. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
  254. test_bit(__I40E_RECOVERY_MODE, pf->state))
  255. queue_work(i40e_wq, &pf->service_task);
  256. }
  257. /**
  258. * i40e_tx_timeout - Respond to a Tx Hang
  259. * @netdev: network interface device structure
  260. *
  261. * If any port has noticed a Tx timeout, it is likely that the whole
  262. * device is munged, not just the one netdev port, so go for the full
  263. * reset.
  264. **/
  265. static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  266. {
  267. struct i40e_netdev_priv *np = netdev_priv(netdev);
  268. struct i40e_vsi *vsi = np->vsi;
  269. struct i40e_pf *pf = vsi->back;
  270. struct i40e_ring *tx_ring = NULL;
  271. unsigned int i;
  272. u32 head, val;
  273. pf->tx_timeout_count++;
  274. /* with txqueue index, find the tx_ring struct */
  275. for (i = 0; i < vsi->num_queue_pairs; i++) {
  276. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  277. if (txqueue ==
  278. vsi->tx_rings[i]->queue_index) {
  279. tx_ring = vsi->tx_rings[i];
  280. break;
  281. }
  282. }
  283. }
  284. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  285. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  286. else if (time_before(jiffies,
  287. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  288. return; /* don't do any new action before the next timeout */
  289. /* don't kick off another recovery if one is already pending */
  290. if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
  291. return;
  292. if (tx_ring) {
  293. head = i40e_get_head(tx_ring);
  294. /* Read interrupt register */
  295. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  296. val = rd32(&pf->hw,
  297. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  298. tx_ring->vsi->base_vector - 1));
  299. else
  300. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  301. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  302. vsi->seid, txqueue, tx_ring->next_to_clean,
  303. head, tx_ring->next_to_use,
  304. readl(tx_ring->tail), val);
  305. }
  306. pf->tx_timeout_last_recovery = jiffies;
  307. netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
  308. pf->tx_timeout_recovery_level, txqueue);
  309. switch (pf->tx_timeout_recovery_level) {
  310. case 1:
  311. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  312. break;
  313. case 2:
  314. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  315. break;
  316. case 3:
  317. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  318. break;
  319. default:
  320. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  321. break;
  322. }
  323. i40e_service_event_schedule(pf);
  324. pf->tx_timeout_recovery_level++;
  325. }
  326. /**
  327. * i40e_get_vsi_stats_struct - Get System Network Statistics
  328. * @vsi: the VSI we care about
  329. *
  330. * Returns the address of the device statistics structure.
  331. * The statistics are actually updated from the service task.
  332. **/
  333. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  334. {
  335. return &vsi->net_stats;
  336. }
  337. /**
  338. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  339. * @ring: Tx ring to get statistics from
  340. * @stats: statistics entry to be updated
  341. **/
  342. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  343. struct rtnl_link_stats64 *stats)
  344. {
  345. u64 bytes, packets;
  346. unsigned int start;
  347. do {
  348. start = u64_stats_fetch_begin_irq(&ring->syncp);
  349. packets = ring->stats.packets;
  350. bytes = ring->stats.bytes;
  351. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  352. stats->tx_packets += packets;
  353. stats->tx_bytes += bytes;
  354. }
  355. /**
  356. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  357. * @netdev: network interface device structure
  358. * @stats: data structure to store statistics
  359. *
  360. * Returns the address of the device statistics structure.
  361. * The statistics are actually updated from the service task.
  362. **/
  363. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  364. struct rtnl_link_stats64 *stats)
  365. {
  366. struct i40e_netdev_priv *np = netdev_priv(netdev);
  367. struct i40e_vsi *vsi = np->vsi;
  368. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  369. struct i40e_ring *ring;
  370. int i;
  371. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  372. return;
  373. if (!vsi->tx_rings)
  374. return;
  375. rcu_read_lock();
  376. for (i = 0; i < vsi->num_queue_pairs; i++) {
  377. u64 bytes, packets;
  378. unsigned int start;
  379. ring = READ_ONCE(vsi->tx_rings[i]);
  380. if (!ring)
  381. continue;
  382. i40e_get_netdev_stats_struct_tx(ring, stats);
  383. if (i40e_enabled_xdp_vsi(vsi)) {
  384. ring++;
  385. i40e_get_netdev_stats_struct_tx(ring, stats);
  386. }
  387. ring++;
  388. do {
  389. start = u64_stats_fetch_begin_irq(&ring->syncp);
  390. packets = ring->stats.packets;
  391. bytes = ring->stats.bytes;
  392. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  393. stats->rx_packets += packets;
  394. stats->rx_bytes += bytes;
  395. }
  396. rcu_read_unlock();
  397. /* following stats updated by i40e_watchdog_subtask() */
  398. stats->multicast = vsi_stats->multicast;
  399. stats->tx_errors = vsi_stats->tx_errors;
  400. stats->tx_dropped = vsi_stats->tx_dropped;
  401. stats->rx_errors = vsi_stats->rx_errors;
  402. stats->rx_dropped = vsi_stats->rx_dropped;
  403. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  404. stats->rx_length_errors = vsi_stats->rx_length_errors;
  405. }
  406. /**
  407. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  408. * @vsi: the VSI to have its stats reset
  409. **/
  410. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  411. {
  412. struct rtnl_link_stats64 *ns;
  413. int i;
  414. if (!vsi)
  415. return;
  416. ns = i40e_get_vsi_stats_struct(vsi);
  417. memset(ns, 0, sizeof(*ns));
  418. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  419. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  420. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  421. if (vsi->rx_rings && vsi->rx_rings[0]) {
  422. for (i = 0; i < vsi->num_queue_pairs; i++) {
  423. memset(&vsi->rx_rings[i]->stats, 0,
  424. sizeof(vsi->rx_rings[i]->stats));
  425. memset(&vsi->rx_rings[i]->rx_stats, 0,
  426. sizeof(vsi->rx_rings[i]->rx_stats));
  427. memset(&vsi->tx_rings[i]->stats, 0,
  428. sizeof(vsi->tx_rings[i]->stats));
  429. memset(&vsi->tx_rings[i]->tx_stats, 0,
  430. sizeof(vsi->tx_rings[i]->tx_stats));
  431. }
  432. }
  433. vsi->stat_offsets_loaded = false;
  434. }
  435. /**
  436. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  437. * @pf: the PF to be reset
  438. **/
  439. void i40e_pf_reset_stats(struct i40e_pf *pf)
  440. {
  441. int i;
  442. memset(&pf->stats, 0, sizeof(pf->stats));
  443. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  444. pf->stat_offsets_loaded = false;
  445. for (i = 0; i < I40E_MAX_VEB; i++) {
  446. if (pf->veb[i]) {
  447. memset(&pf->veb[i]->stats, 0,
  448. sizeof(pf->veb[i]->stats));
  449. memset(&pf->veb[i]->stats_offsets, 0,
  450. sizeof(pf->veb[i]->stats_offsets));
  451. memset(&pf->veb[i]->tc_stats, 0,
  452. sizeof(pf->veb[i]->tc_stats));
  453. memset(&pf->veb[i]->tc_stats_offsets, 0,
  454. sizeof(pf->veb[i]->tc_stats_offsets));
  455. pf->veb[i]->stat_offsets_loaded = false;
  456. }
  457. }
  458. pf->hw_csum_rx_error = 0;
  459. }
  460. /**
  461. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  462. * @hw: ptr to the hardware info
  463. * @hireg: the high 32 bit reg to read
  464. * @loreg: the low 32 bit reg to read
  465. * @offset_loaded: has the initial offset been loaded yet
  466. * @offset: ptr to current offset value
  467. * @stat: ptr to the stat
  468. *
  469. * Since the device stats are not reset at PFReset, they likely will not
  470. * be zeroed when the driver starts. We'll save the first values read
  471. * and use them as offsets to be subtracted from the raw values in order
  472. * to report stats that count from zero. In the process, we also manage
  473. * the potential roll-over.
  474. **/
  475. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  476. bool offset_loaded, u64 *offset, u64 *stat)
  477. {
  478. u64 new_data;
  479. if (hw->device_id == I40E_DEV_ID_QEMU) {
  480. new_data = rd32(hw, loreg);
  481. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  482. } else {
  483. new_data = rd64(hw, loreg);
  484. }
  485. if (!offset_loaded)
  486. *offset = new_data;
  487. if (likely(new_data >= *offset))
  488. *stat = new_data - *offset;
  489. else
  490. *stat = (new_data + BIT_ULL(48)) - *offset;
  491. *stat &= 0xFFFFFFFFFFFFULL;
  492. }
  493. /**
  494. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  495. * @hw: ptr to the hardware info
  496. * @reg: the hw reg to read
  497. * @offset_loaded: has the initial offset been loaded yet
  498. * @offset: ptr to current offset value
  499. * @stat: ptr to the stat
  500. **/
  501. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  502. bool offset_loaded, u64 *offset, u64 *stat)
  503. {
  504. u32 new_data;
  505. new_data = rd32(hw, reg);
  506. if (!offset_loaded)
  507. *offset = new_data;
  508. if (likely(new_data >= *offset))
  509. *stat = (u32)(new_data - *offset);
  510. else
  511. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  512. }
  513. /**
  514. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  515. * @hw: ptr to the hardware info
  516. * @reg: the hw reg to read and clear
  517. * @stat: ptr to the stat
  518. **/
  519. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  520. {
  521. u32 new_data = rd32(hw, reg);
  522. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  523. *stat += new_data;
  524. }
  525. /**
  526. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  527. * @vsi: the VSI to be updated
  528. **/
  529. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  530. {
  531. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  532. struct i40e_pf *pf = vsi->back;
  533. struct i40e_hw *hw = &pf->hw;
  534. struct i40e_eth_stats *oes;
  535. struct i40e_eth_stats *es; /* device's eth stats */
  536. es = &vsi->eth_stats;
  537. oes = &vsi->eth_stats_offsets;
  538. /* Gather up the stats that the hw collects */
  539. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  540. vsi->stat_offsets_loaded,
  541. &oes->tx_errors, &es->tx_errors);
  542. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  543. vsi->stat_offsets_loaded,
  544. &oes->rx_discards, &es->rx_discards);
  545. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  546. vsi->stat_offsets_loaded,
  547. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  548. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  549. I40E_GLV_GORCL(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->rx_bytes, &es->rx_bytes);
  552. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  553. I40E_GLV_UPRCL(stat_idx),
  554. vsi->stat_offsets_loaded,
  555. &oes->rx_unicast, &es->rx_unicast);
  556. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  557. I40E_GLV_MPRCL(stat_idx),
  558. vsi->stat_offsets_loaded,
  559. &oes->rx_multicast, &es->rx_multicast);
  560. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  561. I40E_GLV_BPRCL(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->rx_broadcast, &es->rx_broadcast);
  564. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  565. I40E_GLV_GOTCL(stat_idx),
  566. vsi->stat_offsets_loaded,
  567. &oes->tx_bytes, &es->tx_bytes);
  568. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  569. I40E_GLV_UPTCL(stat_idx),
  570. vsi->stat_offsets_loaded,
  571. &oes->tx_unicast, &es->tx_unicast);
  572. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  573. I40E_GLV_MPTCL(stat_idx),
  574. vsi->stat_offsets_loaded,
  575. &oes->tx_multicast, &es->tx_multicast);
  576. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  577. I40E_GLV_BPTCL(stat_idx),
  578. vsi->stat_offsets_loaded,
  579. &oes->tx_broadcast, &es->tx_broadcast);
  580. vsi->stat_offsets_loaded = true;
  581. }
  582. /**
  583. * i40e_update_veb_stats - Update Switch component statistics
  584. * @veb: the VEB being updated
  585. **/
  586. void i40e_update_veb_stats(struct i40e_veb *veb)
  587. {
  588. struct i40e_pf *pf = veb->pf;
  589. struct i40e_hw *hw = &pf->hw;
  590. struct i40e_eth_stats *oes;
  591. struct i40e_eth_stats *es; /* device's eth stats */
  592. struct i40e_veb_tc_stats *veb_oes;
  593. struct i40e_veb_tc_stats *veb_es;
  594. int i, idx = 0;
  595. idx = veb->stats_idx;
  596. es = &veb->stats;
  597. oes = &veb->stats_offsets;
  598. veb_es = &veb->tc_stats;
  599. veb_oes = &veb->tc_stats_offsets;
  600. /* Gather up the stats that the hw collects */
  601. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->tx_discards, &es->tx_discards);
  604. if (hw->revision_id > 0)
  605. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  606. veb->stat_offsets_loaded,
  607. &oes->rx_unknown_protocol,
  608. &es->rx_unknown_protocol);
  609. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  610. veb->stat_offsets_loaded,
  611. &oes->rx_bytes, &es->rx_bytes);
  612. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  613. veb->stat_offsets_loaded,
  614. &oes->rx_unicast, &es->rx_unicast);
  615. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  616. veb->stat_offsets_loaded,
  617. &oes->rx_multicast, &es->rx_multicast);
  618. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  619. veb->stat_offsets_loaded,
  620. &oes->rx_broadcast, &es->rx_broadcast);
  621. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  622. veb->stat_offsets_loaded,
  623. &oes->tx_bytes, &es->tx_bytes);
  624. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  625. veb->stat_offsets_loaded,
  626. &oes->tx_unicast, &es->tx_unicast);
  627. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  628. veb->stat_offsets_loaded,
  629. &oes->tx_multicast, &es->tx_multicast);
  630. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  631. veb->stat_offsets_loaded,
  632. &oes->tx_broadcast, &es->tx_broadcast);
  633. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  634. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  635. I40E_GLVEBTC_RPCL(i, idx),
  636. veb->stat_offsets_loaded,
  637. &veb_oes->tc_rx_packets[i],
  638. &veb_es->tc_rx_packets[i]);
  639. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  640. I40E_GLVEBTC_RBCL(i, idx),
  641. veb->stat_offsets_loaded,
  642. &veb_oes->tc_rx_bytes[i],
  643. &veb_es->tc_rx_bytes[i]);
  644. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  645. I40E_GLVEBTC_TPCL(i, idx),
  646. veb->stat_offsets_loaded,
  647. &veb_oes->tc_tx_packets[i],
  648. &veb_es->tc_tx_packets[i]);
  649. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  650. I40E_GLVEBTC_TBCL(i, idx),
  651. veb->stat_offsets_loaded,
  652. &veb_oes->tc_tx_bytes[i],
  653. &veb_es->tc_tx_bytes[i]);
  654. }
  655. veb->stat_offsets_loaded = true;
  656. }
  657. /**
  658. * i40e_update_vsi_stats - Update the vsi statistics counters.
  659. * @vsi: the VSI to be updated
  660. *
  661. * There are a few instances where we store the same stat in a
  662. * couple of different structs. This is partly because we have
  663. * the netdev stats that need to be filled out, which is slightly
  664. * different from the "eth_stats" defined by the chip and used in
  665. * VF communications. We sort it out here.
  666. **/
  667. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  668. {
  669. struct i40e_pf *pf = vsi->back;
  670. struct rtnl_link_stats64 *ons;
  671. struct rtnl_link_stats64 *ns; /* netdev stats */
  672. struct i40e_eth_stats *oes;
  673. struct i40e_eth_stats *es; /* device's eth stats */
  674. u32 tx_restart, tx_busy;
  675. struct i40e_ring *p;
  676. u32 rx_page, rx_buf;
  677. u64 bytes, packets;
  678. unsigned int start;
  679. u64 tx_linearize;
  680. u64 tx_force_wb;
  681. u64 rx_p, rx_b;
  682. u64 tx_p, tx_b;
  683. u16 q;
  684. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  685. test_bit(__I40E_CONFIG_BUSY, pf->state))
  686. return;
  687. ns = i40e_get_vsi_stats_struct(vsi);
  688. ons = &vsi->net_stats_offsets;
  689. es = &vsi->eth_stats;
  690. oes = &vsi->eth_stats_offsets;
  691. /* Gather up the netdev and vsi stats that the driver collects
  692. * on the fly during packet processing
  693. */
  694. rx_b = rx_p = 0;
  695. tx_b = tx_p = 0;
  696. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  697. rx_page = 0;
  698. rx_buf = 0;
  699. rcu_read_lock();
  700. for (q = 0; q < vsi->num_queue_pairs; q++) {
  701. /* locate Tx ring */
  702. p = READ_ONCE(vsi->tx_rings[q]);
  703. do {
  704. start = u64_stats_fetch_begin_irq(&p->syncp);
  705. packets = p->stats.packets;
  706. bytes = p->stats.bytes;
  707. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  708. tx_b += bytes;
  709. tx_p += packets;
  710. tx_restart += p->tx_stats.restart_queue;
  711. tx_busy += p->tx_stats.tx_busy;
  712. tx_linearize += p->tx_stats.tx_linearize;
  713. tx_force_wb += p->tx_stats.tx_force_wb;
  714. /* Rx queue is part of the same block as Tx queue */
  715. p = &p[1];
  716. do {
  717. start = u64_stats_fetch_begin_irq(&p->syncp);
  718. packets = p->stats.packets;
  719. bytes = p->stats.bytes;
  720. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  721. rx_b += bytes;
  722. rx_p += packets;
  723. rx_buf += p->rx_stats.alloc_buff_failed;
  724. rx_page += p->rx_stats.alloc_page_failed;
  725. }
  726. rcu_read_unlock();
  727. vsi->tx_restart = tx_restart;
  728. vsi->tx_busy = tx_busy;
  729. vsi->tx_linearize = tx_linearize;
  730. vsi->tx_force_wb = tx_force_wb;
  731. vsi->rx_page_failed = rx_page;
  732. vsi->rx_buf_failed = rx_buf;
  733. ns->rx_packets = rx_p;
  734. ns->rx_bytes = rx_b;
  735. ns->tx_packets = tx_p;
  736. ns->tx_bytes = tx_b;
  737. /* update netdev stats from eth stats */
  738. i40e_update_eth_stats(vsi);
  739. ons->tx_errors = oes->tx_errors;
  740. ns->tx_errors = es->tx_errors;
  741. ons->multicast = oes->rx_multicast;
  742. ns->multicast = es->rx_multicast;
  743. ons->rx_dropped = oes->rx_discards;
  744. ns->rx_dropped = es->rx_discards;
  745. ons->tx_dropped = oes->tx_discards;
  746. ns->tx_dropped = es->tx_discards;
  747. /* pull in a couple PF stats if this is the main vsi */
  748. if (vsi == pf->vsi[pf->lan_vsi]) {
  749. ns->rx_crc_errors = pf->stats.crc_errors;
  750. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  751. ns->rx_length_errors = pf->stats.rx_length_errors;
  752. }
  753. }
  754. /**
  755. * i40e_update_pf_stats - Update the PF statistics counters.
  756. * @pf: the PF to be updated
  757. **/
  758. static void i40e_update_pf_stats(struct i40e_pf *pf)
  759. {
  760. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  761. struct i40e_hw_port_stats *nsd = &pf->stats;
  762. struct i40e_hw *hw = &pf->hw;
  763. u32 val;
  764. int i;
  765. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  766. I40E_GLPRT_GORCL(hw->port),
  767. pf->stat_offsets_loaded,
  768. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  769. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  770. I40E_GLPRT_GOTCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  773. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  774. pf->stat_offsets_loaded,
  775. &osd->eth.rx_discards,
  776. &nsd->eth.rx_discards);
  777. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  778. I40E_GLPRT_UPRCL(hw->port),
  779. pf->stat_offsets_loaded,
  780. &osd->eth.rx_unicast,
  781. &nsd->eth.rx_unicast);
  782. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  783. I40E_GLPRT_MPRCL(hw->port),
  784. pf->stat_offsets_loaded,
  785. &osd->eth.rx_multicast,
  786. &nsd->eth.rx_multicast);
  787. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  788. I40E_GLPRT_BPRCL(hw->port),
  789. pf->stat_offsets_loaded,
  790. &osd->eth.rx_broadcast,
  791. &nsd->eth.rx_broadcast);
  792. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  793. I40E_GLPRT_UPTCL(hw->port),
  794. pf->stat_offsets_loaded,
  795. &osd->eth.tx_unicast,
  796. &nsd->eth.tx_unicast);
  797. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  798. I40E_GLPRT_MPTCL(hw->port),
  799. pf->stat_offsets_loaded,
  800. &osd->eth.tx_multicast,
  801. &nsd->eth.tx_multicast);
  802. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  803. I40E_GLPRT_BPTCL(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->eth.tx_broadcast,
  806. &nsd->eth.tx_broadcast);
  807. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  808. pf->stat_offsets_loaded,
  809. &osd->tx_dropped_link_down,
  810. &nsd->tx_dropped_link_down);
  811. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->crc_errors, &nsd->crc_errors);
  814. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  815. pf->stat_offsets_loaded,
  816. &osd->illegal_bytes, &nsd->illegal_bytes);
  817. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  818. pf->stat_offsets_loaded,
  819. &osd->mac_local_faults,
  820. &nsd->mac_local_faults);
  821. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->mac_remote_faults,
  824. &nsd->mac_remote_faults);
  825. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->rx_length_errors,
  828. &nsd->rx_length_errors);
  829. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  830. pf->stat_offsets_loaded,
  831. &osd->link_xon_rx, &nsd->link_xon_rx);
  832. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  833. pf->stat_offsets_loaded,
  834. &osd->link_xon_tx, &nsd->link_xon_tx);
  835. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  836. pf->stat_offsets_loaded,
  837. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  838. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  839. pf->stat_offsets_loaded,
  840. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  841. for (i = 0; i < 8; i++) {
  842. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  843. pf->stat_offsets_loaded,
  844. &osd->priority_xoff_rx[i],
  845. &nsd->priority_xoff_rx[i]);
  846. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xon_rx[i],
  849. &nsd->priority_xon_rx[i]);
  850. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  851. pf->stat_offsets_loaded,
  852. &osd->priority_xon_tx[i],
  853. &nsd->priority_xon_tx[i]);
  854. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  855. pf->stat_offsets_loaded,
  856. &osd->priority_xoff_tx[i],
  857. &nsd->priority_xoff_tx[i]);
  858. i40e_stat_update32(hw,
  859. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  860. pf->stat_offsets_loaded,
  861. &osd->priority_xon_2_xoff[i],
  862. &nsd->priority_xon_2_xoff[i]);
  863. }
  864. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  865. I40E_GLPRT_PRC64L(hw->port),
  866. pf->stat_offsets_loaded,
  867. &osd->rx_size_64, &nsd->rx_size_64);
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  869. I40E_GLPRT_PRC127L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_127, &nsd->rx_size_127);
  872. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  873. I40E_GLPRT_PRC255L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->rx_size_255, &nsd->rx_size_255);
  876. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  877. I40E_GLPRT_PRC511L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_size_511, &nsd->rx_size_511);
  880. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  881. I40E_GLPRT_PRC1023L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->rx_size_1023, &nsd->rx_size_1023);
  884. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  885. I40E_GLPRT_PRC1522L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->rx_size_1522, &nsd->rx_size_1522);
  888. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  889. I40E_GLPRT_PRC9522L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->rx_size_big, &nsd->rx_size_big);
  892. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  893. I40E_GLPRT_PTC64L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->tx_size_64, &nsd->tx_size_64);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  897. I40E_GLPRT_PTC127L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_127, &nsd->tx_size_127);
  900. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  901. I40E_GLPRT_PTC255L(hw->port),
  902. pf->stat_offsets_loaded,
  903. &osd->tx_size_255, &nsd->tx_size_255);
  904. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  905. I40E_GLPRT_PTC511L(hw->port),
  906. pf->stat_offsets_loaded,
  907. &osd->tx_size_511, &nsd->tx_size_511);
  908. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  909. I40E_GLPRT_PTC1023L(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->tx_size_1023, &nsd->tx_size_1023);
  912. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  913. I40E_GLPRT_PTC1522L(hw->port),
  914. pf->stat_offsets_loaded,
  915. &osd->tx_size_1522, &nsd->tx_size_1522);
  916. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  917. I40E_GLPRT_PTC9522L(hw->port),
  918. pf->stat_offsets_loaded,
  919. &osd->tx_size_big, &nsd->tx_size_big);
  920. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  921. pf->stat_offsets_loaded,
  922. &osd->rx_undersize, &nsd->rx_undersize);
  923. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  924. pf->stat_offsets_loaded,
  925. &osd->rx_fragments, &nsd->rx_fragments);
  926. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  927. pf->stat_offsets_loaded,
  928. &osd->rx_oversize, &nsd->rx_oversize);
  929. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  930. pf->stat_offsets_loaded,
  931. &osd->rx_jabber, &nsd->rx_jabber);
  932. /* FDIR stats */
  933. i40e_stat_update_and_clear32(hw,
  934. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  935. &nsd->fd_atr_match);
  936. i40e_stat_update_and_clear32(hw,
  937. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  938. &nsd->fd_sb_match);
  939. i40e_stat_update_and_clear32(hw,
  940. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  941. &nsd->fd_atr_tunnel_match);
  942. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  943. nsd->tx_lpi_status =
  944. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  945. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  946. nsd->rx_lpi_status =
  947. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  948. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  949. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  950. pf->stat_offsets_loaded,
  951. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  952. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  953. pf->stat_offsets_loaded,
  954. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  955. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  956. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  957. nsd->fd_sb_status = true;
  958. else
  959. nsd->fd_sb_status = false;
  960. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  961. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  962. nsd->fd_atr_status = true;
  963. else
  964. nsd->fd_atr_status = false;
  965. pf->stat_offsets_loaded = true;
  966. }
  967. /**
  968. * i40e_update_stats - Update the various statistics counters.
  969. * @vsi: the VSI to be updated
  970. *
  971. * Update the various stats for this VSI and its related entities.
  972. **/
  973. void i40e_update_stats(struct i40e_vsi *vsi)
  974. {
  975. struct i40e_pf *pf = vsi->back;
  976. if (vsi == pf->vsi[pf->lan_vsi])
  977. i40e_update_pf_stats(pf);
  978. i40e_update_vsi_stats(vsi);
  979. }
  980. /**
  981. * i40e_count_filters - counts VSI mac filters
  982. * @vsi: the VSI to be searched
  983. *
  984. * Returns count of mac filters
  985. **/
  986. int i40e_count_filters(struct i40e_vsi *vsi)
  987. {
  988. struct i40e_mac_filter *f;
  989. struct hlist_node *h;
  990. int bkt;
  991. int cnt = 0;
  992. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  993. ++cnt;
  994. return cnt;
  995. }
  996. /**
  997. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  998. * @vsi: the VSI to be searched
  999. * @macaddr: the MAC address
  1000. * @vlan: the vlan
  1001. *
  1002. * Returns ptr to the filter object or NULL
  1003. **/
  1004. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  1005. const u8 *macaddr, s16 vlan)
  1006. {
  1007. struct i40e_mac_filter *f;
  1008. u64 key;
  1009. if (!vsi || !macaddr)
  1010. return NULL;
  1011. key = i40e_addr_to_hkey(macaddr);
  1012. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1013. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1014. (vlan == f->vlan))
  1015. return f;
  1016. }
  1017. return NULL;
  1018. }
  1019. /**
  1020. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1021. * @vsi: the VSI to be searched
  1022. * @macaddr: the MAC address we are searching for
  1023. *
  1024. * Returns the first filter with the provided MAC address or NULL if
  1025. * MAC address was not found
  1026. **/
  1027. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1028. {
  1029. struct i40e_mac_filter *f;
  1030. u64 key;
  1031. if (!vsi || !macaddr)
  1032. return NULL;
  1033. key = i40e_addr_to_hkey(macaddr);
  1034. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1035. if ((ether_addr_equal(macaddr, f->macaddr)))
  1036. return f;
  1037. }
  1038. return NULL;
  1039. }
  1040. /**
  1041. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1042. * @vsi: the VSI to be searched
  1043. *
  1044. * Returns true if VSI is in vlan mode or false otherwise
  1045. **/
  1046. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1047. {
  1048. /* If we have a PVID, always operate in VLAN mode */
  1049. if (vsi->info.pvid)
  1050. return true;
  1051. /* We need to operate in VLAN mode whenever we have any filters with
  1052. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1053. * time, incurring search cost repeatedly. However, we can notice two
  1054. * things:
  1055. *
  1056. * 1) the only place where we can gain a VLAN filter is in
  1057. * i40e_add_filter.
  1058. *
  1059. * 2) the only place where filters are actually removed is in
  1060. * i40e_sync_filters_subtask.
  1061. *
  1062. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1063. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1064. * we have to perform the full search after deleting filters in
  1065. * i40e_sync_filters_subtask, but we already have to search
  1066. * filters here and can perform the check at the same time. This
  1067. * results in avoiding embedding a loop for VLAN mode inside another
  1068. * loop over all the filters, and should maintain correctness as noted
  1069. * above.
  1070. */
  1071. return vsi->has_vlan_filter;
  1072. }
  1073. /**
  1074. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1075. * @vsi: the VSI to configure
  1076. * @tmp_add_list: list of filters ready to be added
  1077. * @tmp_del_list: list of filters ready to be deleted
  1078. * @vlan_filters: the number of active VLAN filters
  1079. *
  1080. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1081. * behave as expected. If we have any active VLAN filters remaining or about
  1082. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1083. * so that they only match against untagged traffic. If we no longer have any
  1084. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1085. * so that they match against both tagged and untagged traffic. In this way,
  1086. * we ensure that we correctly receive the desired traffic. This ensures that
  1087. * when we have an active VLAN we will receive only untagged traffic and
  1088. * traffic matching active VLANs. If we have no active VLANs then we will
  1089. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1090. *
  1091. * Finally, in a similar fashion, this function also corrects filters when
  1092. * there is an active PVID assigned to this VSI.
  1093. *
  1094. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1095. *
  1096. * This function is only expected to be called from within
  1097. * i40e_sync_vsi_filters.
  1098. *
  1099. * NOTE: This function expects to be called while under the
  1100. * mac_filter_hash_lock
  1101. */
  1102. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1103. struct hlist_head *tmp_add_list,
  1104. struct hlist_head *tmp_del_list,
  1105. int vlan_filters)
  1106. {
  1107. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1108. struct i40e_mac_filter *f, *add_head;
  1109. struct i40e_new_mac_filter *new;
  1110. struct hlist_node *h;
  1111. int bkt, new_vlan;
  1112. /* To determine if a particular filter needs to be replaced we
  1113. * have the three following conditions:
  1114. *
  1115. * a) if we have a PVID assigned, then all filters which are
  1116. * not marked as VLAN=PVID must be replaced with filters that
  1117. * are.
  1118. * b) otherwise, if we have any active VLANS, all filters
  1119. * which are marked as VLAN=-1 must be replaced with
  1120. * filters marked as VLAN=0
  1121. * c) finally, if we do not have any active VLANS, all filters
  1122. * which are marked as VLAN=0 must be replaced with filters
  1123. * marked as VLAN=-1
  1124. */
  1125. /* Update the filters about to be added in place */
  1126. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1127. if (pvid && new->f->vlan != pvid)
  1128. new->f->vlan = pvid;
  1129. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1130. new->f->vlan = 0;
  1131. else if (!vlan_filters && new->f->vlan == 0)
  1132. new->f->vlan = I40E_VLAN_ANY;
  1133. }
  1134. /* Update the remaining active filters */
  1135. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1136. /* Combine the checks for whether a filter needs to be changed
  1137. * and then determine the new VLAN inside the if block, in
  1138. * order to avoid duplicating code for adding the new filter
  1139. * then deleting the old filter.
  1140. */
  1141. if ((pvid && f->vlan != pvid) ||
  1142. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1143. (!vlan_filters && f->vlan == 0)) {
  1144. /* Determine the new vlan we will be adding */
  1145. if (pvid)
  1146. new_vlan = pvid;
  1147. else if (vlan_filters)
  1148. new_vlan = 0;
  1149. else
  1150. new_vlan = I40E_VLAN_ANY;
  1151. /* Create the new filter */
  1152. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1153. if (!add_head)
  1154. return -ENOMEM;
  1155. /* Create a temporary i40e_new_mac_filter */
  1156. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1157. if (!new)
  1158. return -ENOMEM;
  1159. new->f = add_head;
  1160. new->state = add_head->state;
  1161. /* Add the new filter to the tmp list */
  1162. hlist_add_head(&new->hlist, tmp_add_list);
  1163. /* Put the original filter into the delete list */
  1164. f->state = I40E_FILTER_REMOVE;
  1165. hash_del(&f->hlist);
  1166. hlist_add_head(&f->hlist, tmp_del_list);
  1167. }
  1168. }
  1169. vsi->has_vlan_filter = !!vlan_filters;
  1170. return 0;
  1171. }
  1172. /**
  1173. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1174. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1175. * @macaddr: the MAC address
  1176. *
  1177. * Remove whatever filter the firmware set up so the driver can manage
  1178. * its own filtering intelligently.
  1179. **/
  1180. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1181. {
  1182. struct i40e_aqc_remove_macvlan_element_data element;
  1183. struct i40e_pf *pf = vsi->back;
  1184. /* Only appropriate for the PF main VSI */
  1185. if (vsi->type != I40E_VSI_MAIN)
  1186. return;
  1187. memset(&element, 0, sizeof(element));
  1188. ether_addr_copy(element.mac_addr, macaddr);
  1189. element.vlan_tag = 0;
  1190. /* Ignore error returns, some firmware does it this way... */
  1191. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1192. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1193. memset(&element, 0, sizeof(element));
  1194. ether_addr_copy(element.mac_addr, macaddr);
  1195. element.vlan_tag = 0;
  1196. /* ...and some firmware does it this way. */
  1197. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1198. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1199. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1200. }
  1201. /**
  1202. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1203. * @vsi: the VSI to be searched
  1204. * @macaddr: the MAC address
  1205. * @vlan: the vlan
  1206. *
  1207. * Returns ptr to the filter object or NULL when no memory available.
  1208. *
  1209. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1210. * being held.
  1211. **/
  1212. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1213. const u8 *macaddr, s16 vlan)
  1214. {
  1215. struct i40e_mac_filter *f;
  1216. u64 key;
  1217. if (!vsi || !macaddr)
  1218. return NULL;
  1219. f = i40e_find_filter(vsi, macaddr, vlan);
  1220. if (!f) {
  1221. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1222. if (!f)
  1223. return NULL;
  1224. /* Update the boolean indicating if we need to function in
  1225. * VLAN mode.
  1226. */
  1227. if (vlan >= 0)
  1228. vsi->has_vlan_filter = true;
  1229. ether_addr_copy(f->macaddr, macaddr);
  1230. f->vlan = vlan;
  1231. f->state = I40E_FILTER_NEW;
  1232. INIT_HLIST_NODE(&f->hlist);
  1233. key = i40e_addr_to_hkey(macaddr);
  1234. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1235. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1236. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1237. }
  1238. /* If we're asked to add a filter that has been marked for removal, it
  1239. * is safe to simply restore it to active state. __i40e_del_filter
  1240. * will have simply deleted any filters which were previously marked
  1241. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1242. * previously been ACTIVE. Since we haven't yet run the sync filters
  1243. * task, just restore this filter to the ACTIVE state so that the
  1244. * sync task leaves it in place
  1245. */
  1246. if (f->state == I40E_FILTER_REMOVE)
  1247. f->state = I40E_FILTER_ACTIVE;
  1248. return f;
  1249. }
  1250. /**
  1251. * __i40e_del_filter - Remove a specific filter from the VSI
  1252. * @vsi: VSI to remove from
  1253. * @f: the filter to remove from the list
  1254. *
  1255. * This function should be called instead of i40e_del_filter only if you know
  1256. * the exact filter you will remove already, such as via i40e_find_filter or
  1257. * i40e_find_mac.
  1258. *
  1259. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1260. * being held.
  1261. * ANOTHER NOTE: This function MUST be called from within the context of
  1262. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1263. * instead of list_for_each_entry().
  1264. **/
  1265. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1266. {
  1267. if (!f)
  1268. return;
  1269. /* If the filter was never added to firmware then we can just delete it
  1270. * directly and we don't want to set the status to remove or else an
  1271. * admin queue command will unnecessarily fire.
  1272. */
  1273. if ((f->state == I40E_FILTER_FAILED) ||
  1274. (f->state == I40E_FILTER_NEW)) {
  1275. hash_del(&f->hlist);
  1276. kfree(f);
  1277. } else {
  1278. f->state = I40E_FILTER_REMOVE;
  1279. }
  1280. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1281. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1282. }
  1283. /**
  1284. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1285. * @vsi: the VSI to be searched
  1286. * @macaddr: the MAC address
  1287. * @vlan: the VLAN
  1288. *
  1289. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1290. * being held.
  1291. * ANOTHER NOTE: This function MUST be called from within the context of
  1292. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1293. * instead of list_for_each_entry().
  1294. **/
  1295. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1296. {
  1297. struct i40e_mac_filter *f;
  1298. if (!vsi || !macaddr)
  1299. return;
  1300. f = i40e_find_filter(vsi, macaddr, vlan);
  1301. __i40e_del_filter(vsi, f);
  1302. }
  1303. /**
  1304. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1305. * @vsi: the VSI to be searched
  1306. * @macaddr: the mac address to be filtered
  1307. *
  1308. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1309. * go through all the macvlan filters and add a macvlan filter for each
  1310. * unique vlan that already exists. If a PVID has been assigned, instead only
  1311. * add the macaddr to that VLAN.
  1312. *
  1313. * Returns last filter added on success, else NULL
  1314. **/
  1315. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1316. const u8 *macaddr)
  1317. {
  1318. struct i40e_mac_filter *f, *add = NULL;
  1319. struct hlist_node *h;
  1320. int bkt;
  1321. if (vsi->info.pvid)
  1322. return i40e_add_filter(vsi, macaddr,
  1323. le16_to_cpu(vsi->info.pvid));
  1324. if (!i40e_is_vsi_in_vlan(vsi))
  1325. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1326. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1327. if (f->state == I40E_FILTER_REMOVE)
  1328. continue;
  1329. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1330. if (!add)
  1331. return NULL;
  1332. }
  1333. return add;
  1334. }
  1335. /**
  1336. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1337. * @vsi: the VSI to be searched
  1338. * @macaddr: the mac address to be removed
  1339. *
  1340. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1341. * associated with.
  1342. *
  1343. * Returns 0 for success, or error
  1344. **/
  1345. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1346. {
  1347. struct i40e_mac_filter *f;
  1348. struct hlist_node *h;
  1349. bool found = false;
  1350. int bkt;
  1351. lockdep_assert_held(&vsi->mac_filter_hash_lock);
  1352. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1353. if (ether_addr_equal(macaddr, f->macaddr)) {
  1354. __i40e_del_filter(vsi, f);
  1355. found = true;
  1356. }
  1357. }
  1358. if (found)
  1359. return 0;
  1360. else
  1361. return -ENOENT;
  1362. }
  1363. /**
  1364. * i40e_set_mac - NDO callback to set mac address
  1365. * @netdev: network interface device structure
  1366. * @p: pointer to an address structure
  1367. *
  1368. * Returns 0 on success, negative on failure
  1369. **/
  1370. static int i40e_set_mac(struct net_device *netdev, void *p)
  1371. {
  1372. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1373. struct i40e_vsi *vsi = np->vsi;
  1374. struct i40e_pf *pf = vsi->back;
  1375. struct i40e_hw *hw = &pf->hw;
  1376. struct sockaddr *addr = p;
  1377. if (!is_valid_ether_addr(addr->sa_data))
  1378. return -EADDRNOTAVAIL;
  1379. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1380. netdev_info(netdev, "already using mac address %pM\n",
  1381. addr->sa_data);
  1382. return 0;
  1383. }
  1384. if (test_bit(__I40E_DOWN, pf->state) ||
  1385. test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  1386. return -EADDRNOTAVAIL;
  1387. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1388. netdev_info(netdev, "returning to hw mac address %pM\n",
  1389. hw->mac.addr);
  1390. else
  1391. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1392. /* Copy the address first, so that we avoid a possible race with
  1393. * .set_rx_mode().
  1394. * - Remove old address from MAC filter
  1395. * - Copy new address
  1396. * - Add new address to MAC filter
  1397. */
  1398. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1399. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1400. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1401. i40e_add_mac_filter(vsi, netdev->dev_addr);
  1402. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1403. if (vsi->type == I40E_VSI_MAIN) {
  1404. i40e_status ret;
  1405. ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
  1406. addr->sa_data, NULL);
  1407. if (ret)
  1408. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1409. i40e_stat_str(hw, ret),
  1410. i40e_aq_str(hw, hw->aq.asq_last_status));
  1411. }
  1412. /* schedule our worker thread which will take care of
  1413. * applying the new filter changes
  1414. */
  1415. i40e_service_event_schedule(pf);
  1416. return 0;
  1417. }
  1418. /**
  1419. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1420. * @vsi: vsi structure
  1421. * @seed: RSS hash seed
  1422. **/
  1423. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1424. u8 *lut, u16 lut_size)
  1425. {
  1426. struct i40e_pf *pf = vsi->back;
  1427. struct i40e_hw *hw = &pf->hw;
  1428. int ret = 0;
  1429. if (seed) {
  1430. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1431. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1432. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1433. if (ret) {
  1434. dev_info(&pf->pdev->dev,
  1435. "Cannot set RSS key, err %s aq_err %s\n",
  1436. i40e_stat_str(hw, ret),
  1437. i40e_aq_str(hw, hw->aq.asq_last_status));
  1438. return ret;
  1439. }
  1440. }
  1441. if (lut) {
  1442. bool pf_lut = vsi->type == I40E_VSI_MAIN;
  1443. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1444. if (ret) {
  1445. dev_info(&pf->pdev->dev,
  1446. "Cannot set RSS lut, err %s aq_err %s\n",
  1447. i40e_stat_str(hw, ret),
  1448. i40e_aq_str(hw, hw->aq.asq_last_status));
  1449. return ret;
  1450. }
  1451. }
  1452. return ret;
  1453. }
  1454. /**
  1455. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1456. * @vsi: VSI structure
  1457. **/
  1458. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1459. {
  1460. struct i40e_pf *pf = vsi->back;
  1461. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1462. u8 *lut;
  1463. int ret;
  1464. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1465. return 0;
  1466. if (!vsi->rss_size)
  1467. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1468. vsi->num_queue_pairs);
  1469. if (!vsi->rss_size)
  1470. return -EINVAL;
  1471. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1472. if (!lut)
  1473. return -ENOMEM;
  1474. /* Use the user configured hash keys and lookup table if there is one,
  1475. * otherwise use default
  1476. */
  1477. if (vsi->rss_lut_user)
  1478. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1479. else
  1480. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1481. if (vsi->rss_hkey_user)
  1482. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1483. else
  1484. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1485. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1486. kfree(lut);
  1487. return ret;
  1488. }
  1489. /**
  1490. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1491. * @vsi: the VSI being configured,
  1492. * @ctxt: VSI context structure
  1493. * @enabled_tc: number of traffic classes to enable
  1494. *
  1495. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1496. **/
  1497. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1498. struct i40e_vsi_context *ctxt,
  1499. u8 enabled_tc)
  1500. {
  1501. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1502. int i, override_q, pow, num_qps, ret;
  1503. u8 netdev_tc = 0, offset = 0;
  1504. if (vsi->type != I40E_VSI_MAIN)
  1505. return -EINVAL;
  1506. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1507. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1508. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1509. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1510. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1511. /* find the next higher power-of-2 of num queue pairs */
  1512. pow = ilog2(num_qps);
  1513. if (!is_power_of_2(num_qps))
  1514. pow++;
  1515. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1516. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1517. /* Setup queue offset/count for all TCs for given VSI */
  1518. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1519. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1520. /* See if the given TC is enabled for the given VSI */
  1521. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1522. offset = vsi->mqprio_qopt.qopt.offset[i];
  1523. qcount = vsi->mqprio_qopt.qopt.count[i];
  1524. if (qcount > max_qcount)
  1525. max_qcount = qcount;
  1526. vsi->tc_config.tc_info[i].qoffset = offset;
  1527. vsi->tc_config.tc_info[i].qcount = qcount;
  1528. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1529. } else {
  1530. /* TC is not enabled so set the offset to
  1531. * default queue and allocate one queue
  1532. * for the given TC.
  1533. */
  1534. vsi->tc_config.tc_info[i].qoffset = 0;
  1535. vsi->tc_config.tc_info[i].qcount = 1;
  1536. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1537. }
  1538. }
  1539. /* Set actual Tx/Rx queue pairs */
  1540. vsi->num_queue_pairs = offset + qcount;
  1541. /* Setup queue TC[0].qmap for given VSI context */
  1542. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1543. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1544. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1545. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1546. /* Reconfigure RSS for main VSI with max queue count */
  1547. vsi->rss_size = max_qcount;
  1548. ret = i40e_vsi_config_rss(vsi);
  1549. if (ret) {
  1550. dev_info(&vsi->back->pdev->dev,
  1551. "Failed to reconfig rss for num_queues (%u)\n",
  1552. max_qcount);
  1553. return ret;
  1554. }
  1555. vsi->reconfig_rss = true;
  1556. dev_dbg(&vsi->back->pdev->dev,
  1557. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1558. /* Find queue count available for channel VSIs and starting offset
  1559. * for channel VSIs
  1560. */
  1561. override_q = vsi->mqprio_qopt.qopt.count[0];
  1562. if (override_q && override_q < vsi->num_queue_pairs) {
  1563. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1564. vsi->next_base_queue = override_q;
  1565. }
  1566. return 0;
  1567. }
  1568. /**
  1569. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1570. * @vsi: the VSI being setup
  1571. * @ctxt: VSI context structure
  1572. * @enabled_tc: Enabled TCs bitmap
  1573. * @is_add: True if called before Add VSI
  1574. *
  1575. * Setup VSI queue mapping for enabled traffic classes.
  1576. **/
  1577. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1578. struct i40e_vsi_context *ctxt,
  1579. u8 enabled_tc,
  1580. bool is_add)
  1581. {
  1582. struct i40e_pf *pf = vsi->back;
  1583. u16 sections = 0;
  1584. u8 netdev_tc = 0;
  1585. u16 numtc = 1;
  1586. u16 qcount;
  1587. u8 offset;
  1588. u16 qmap;
  1589. int i;
  1590. u16 num_tc_qps = 0;
  1591. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1592. offset = 0;
  1593. /* Number of queues per enabled TC */
  1594. num_tc_qps = vsi->alloc_queue_pairs;
  1595. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1596. /* Find numtc from enabled TC bitmap */
  1597. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1598. if (enabled_tc & BIT(i)) /* TC is enabled */
  1599. numtc++;
  1600. }
  1601. if (!numtc) {
  1602. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1603. numtc = 1;
  1604. }
  1605. num_tc_qps = num_tc_qps / numtc;
  1606. num_tc_qps = min_t(int, num_tc_qps,
  1607. i40e_pf_get_max_q_per_tc(pf));
  1608. }
  1609. vsi->tc_config.numtc = numtc;
  1610. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1611. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1612. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1613. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1614. /* Setup queue offset/count for all TCs for given VSI */
  1615. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1616. /* See if the given TC is enabled for the given VSI */
  1617. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1618. /* TC is enabled */
  1619. int pow, num_qps;
  1620. switch (vsi->type) {
  1621. case I40E_VSI_MAIN:
  1622. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1623. I40E_FLAG_FD_ATR_ENABLED)) ||
  1624. vsi->tc_config.enabled_tc != 1) {
  1625. qcount = min_t(int, pf->alloc_rss_size,
  1626. num_tc_qps);
  1627. break;
  1628. }
  1629. /* fall through */
  1630. case I40E_VSI_FDIR:
  1631. case I40E_VSI_SRIOV:
  1632. case I40E_VSI_VMDQ2:
  1633. default:
  1634. qcount = num_tc_qps;
  1635. WARN_ON(i != 0);
  1636. break;
  1637. }
  1638. vsi->tc_config.tc_info[i].qoffset = offset;
  1639. vsi->tc_config.tc_info[i].qcount = qcount;
  1640. /* find the next higher power-of-2 of num queue pairs */
  1641. num_qps = qcount;
  1642. pow = 0;
  1643. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1644. pow++;
  1645. num_qps >>= 1;
  1646. }
  1647. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1648. qmap =
  1649. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1650. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1651. offset += qcount;
  1652. } else {
  1653. /* TC is not enabled so set the offset to
  1654. * default queue and allocate one queue
  1655. * for the given TC.
  1656. */
  1657. vsi->tc_config.tc_info[i].qoffset = 0;
  1658. vsi->tc_config.tc_info[i].qcount = 1;
  1659. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1660. qmap = 0;
  1661. }
  1662. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1663. }
  1664. /* Set actual Tx/Rx queue pairs */
  1665. vsi->num_queue_pairs = offset;
  1666. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1667. if (vsi->req_queue_pairs > 0)
  1668. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1669. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1670. vsi->num_queue_pairs = pf->num_lan_msix;
  1671. }
  1672. /* Scheduler section valid can only be set for ADD VSI */
  1673. if (is_add) {
  1674. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1675. ctxt->info.up_enable_bits = enabled_tc;
  1676. }
  1677. if (vsi->type == I40E_VSI_SRIOV) {
  1678. ctxt->info.mapping_flags |=
  1679. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1680. for (i = 0; i < vsi->num_queue_pairs; i++)
  1681. ctxt->info.queue_mapping[i] =
  1682. cpu_to_le16(vsi->base_queue + i);
  1683. } else {
  1684. ctxt->info.mapping_flags |=
  1685. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1686. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1687. }
  1688. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1689. }
  1690. /**
  1691. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1692. * @netdev: the netdevice
  1693. * @addr: address to add
  1694. *
  1695. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1696. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1697. */
  1698. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1699. {
  1700. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1701. struct i40e_vsi *vsi = np->vsi;
  1702. if (i40e_add_mac_filter(vsi, addr))
  1703. return 0;
  1704. else
  1705. return -ENOMEM;
  1706. }
  1707. /**
  1708. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1709. * @netdev: the netdevice
  1710. * @addr: address to add
  1711. *
  1712. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1713. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1714. */
  1715. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1716. {
  1717. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1718. struct i40e_vsi *vsi = np->vsi;
  1719. /* Under some circumstances, we might receive a request to delete
  1720. * our own device address from our uc list. Because we store the
  1721. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1722. * such requests and not delete our device address from this list.
  1723. */
  1724. if (ether_addr_equal(addr, netdev->dev_addr))
  1725. return 0;
  1726. i40e_del_mac_filter(vsi, addr);
  1727. return 0;
  1728. }
  1729. /**
  1730. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1731. * @netdev: network interface device structure
  1732. **/
  1733. static void i40e_set_rx_mode(struct net_device *netdev)
  1734. {
  1735. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1736. struct i40e_vsi *vsi = np->vsi;
  1737. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1738. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1739. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1740. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1741. /* check for other flag changes */
  1742. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1743. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1744. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1745. }
  1746. }
  1747. /**
  1748. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1749. * @vsi: Pointer to VSI struct
  1750. * @from: Pointer to list which contains MAC filter entries - changes to
  1751. * those entries needs to be undone.
  1752. *
  1753. * MAC filter entries from this list were slated for deletion.
  1754. **/
  1755. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1756. struct hlist_head *from)
  1757. {
  1758. struct i40e_mac_filter *f;
  1759. struct hlist_node *h;
  1760. hlist_for_each_entry_safe(f, h, from, hlist) {
  1761. u64 key = i40e_addr_to_hkey(f->macaddr);
  1762. /* Move the element back into MAC filter list*/
  1763. hlist_del(&f->hlist);
  1764. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1765. }
  1766. }
  1767. /**
  1768. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1769. * @vsi: Pointer to vsi struct
  1770. * @from: Pointer to list which contains MAC filter entries - changes to
  1771. * those entries needs to be undone.
  1772. *
  1773. * MAC filter entries from this list were slated for addition.
  1774. **/
  1775. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1776. struct hlist_head *from)
  1777. {
  1778. struct i40e_new_mac_filter *new;
  1779. struct hlist_node *h;
  1780. hlist_for_each_entry_safe(new, h, from, hlist) {
  1781. /* We can simply free the wrapper structure */
  1782. hlist_del(&new->hlist);
  1783. kfree(new);
  1784. }
  1785. }
  1786. /**
  1787. * i40e_next_entry - Get the next non-broadcast filter from a list
  1788. * @next: pointer to filter in list
  1789. *
  1790. * Returns the next non-broadcast filter in the list. Required so that we
  1791. * ignore broadcast filters within the list, since these are not handled via
  1792. * the normal firmware update path.
  1793. */
  1794. static
  1795. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1796. {
  1797. hlist_for_each_entry_continue(next, hlist) {
  1798. if (!is_broadcast_ether_addr(next->f->macaddr))
  1799. return next;
  1800. }
  1801. return NULL;
  1802. }
  1803. /**
  1804. * i40e_update_filter_state - Update filter state based on return data
  1805. * from firmware
  1806. * @count: Number of filters added
  1807. * @add_list: return data from fw
  1808. * @add_head: pointer to first filter in current batch
  1809. *
  1810. * MAC filter entries from list were slated to be added to device. Returns
  1811. * number of successful filters. Note that 0 does NOT mean success!
  1812. **/
  1813. static int
  1814. i40e_update_filter_state(int count,
  1815. struct i40e_aqc_add_macvlan_element_data *add_list,
  1816. struct i40e_new_mac_filter *add_head)
  1817. {
  1818. int retval = 0;
  1819. int i;
  1820. for (i = 0; i < count; i++) {
  1821. /* Always check status of each filter. We don't need to check
  1822. * the firmware return status because we pre-set the filter
  1823. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1824. * request to the adminq. Thus, if it no longer matches then
  1825. * we know the filter is active.
  1826. */
  1827. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1828. add_head->state = I40E_FILTER_FAILED;
  1829. } else {
  1830. add_head->state = I40E_FILTER_ACTIVE;
  1831. retval++;
  1832. }
  1833. add_head = i40e_next_filter(add_head);
  1834. if (!add_head)
  1835. break;
  1836. }
  1837. return retval;
  1838. }
  1839. /**
  1840. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1841. * @vsi: ptr to the VSI
  1842. * @vsi_name: name to display in messages
  1843. * @list: the list of filters to send to firmware
  1844. * @num_del: the number of filters to delete
  1845. * @retval: Set to -EIO on failure to delete
  1846. *
  1847. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1848. * *retval instead of a return value so that success does not force ret_val to
  1849. * be set to 0. This ensures that a sequence of calls to this function
  1850. * preserve the previous value of *retval on successful delete.
  1851. */
  1852. static
  1853. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1854. struct i40e_aqc_remove_macvlan_element_data *list,
  1855. int num_del, int *retval)
  1856. {
  1857. struct i40e_hw *hw = &vsi->back->hw;
  1858. i40e_status aq_ret;
  1859. int aq_err;
  1860. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1861. aq_err = hw->aq.asq_last_status;
  1862. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1863. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1864. *retval = -EIO;
  1865. dev_info(&vsi->back->pdev->dev,
  1866. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1867. vsi_name, i40e_stat_str(hw, aq_ret),
  1868. i40e_aq_str(hw, aq_err));
  1869. }
  1870. }
  1871. /**
  1872. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1873. * @vsi: ptr to the VSI
  1874. * @vsi_name: name to display in messages
  1875. * @list: the list of filters to send to firmware
  1876. * @add_head: Position in the add hlist
  1877. * @num_add: the number of filters to add
  1878. *
  1879. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1880. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1881. * space for more filters.
  1882. */
  1883. static
  1884. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1885. struct i40e_aqc_add_macvlan_element_data *list,
  1886. struct i40e_new_mac_filter *add_head,
  1887. int num_add)
  1888. {
  1889. struct i40e_hw *hw = &vsi->back->hw;
  1890. int aq_err, fcnt;
  1891. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1892. aq_err = hw->aq.asq_last_status;
  1893. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1894. if (fcnt != num_add) {
  1895. if (vsi->type == I40E_VSI_MAIN) {
  1896. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1897. dev_warn(&vsi->back->pdev->dev,
  1898. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1899. i40e_aq_str(hw, aq_err), vsi_name);
  1900. } else if (vsi->type == I40E_VSI_SRIOV ||
  1901. vsi->type == I40E_VSI_VMDQ1 ||
  1902. vsi->type == I40E_VSI_VMDQ2) {
  1903. dev_warn(&vsi->back->pdev->dev,
  1904. "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
  1905. i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
  1906. } else {
  1907. dev_warn(&vsi->back->pdev->dev,
  1908. "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
  1909. i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
  1910. }
  1911. }
  1912. }
  1913. /**
  1914. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1915. * @vsi: pointer to the VSI
  1916. * @vsi_name: the VSI name
  1917. * @f: filter data
  1918. *
  1919. * This function sets or clears the promiscuous broadcast flags for VLAN
  1920. * filters in order to properly receive broadcast frames. Assumes that only
  1921. * broadcast filters are passed.
  1922. *
  1923. * Returns status indicating success or failure;
  1924. **/
  1925. static i40e_status
  1926. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1927. struct i40e_mac_filter *f)
  1928. {
  1929. bool enable = f->state == I40E_FILTER_NEW;
  1930. struct i40e_hw *hw = &vsi->back->hw;
  1931. i40e_status aq_ret;
  1932. if (f->vlan == I40E_VLAN_ANY) {
  1933. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1934. vsi->seid,
  1935. enable,
  1936. NULL);
  1937. } else {
  1938. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1939. vsi->seid,
  1940. enable,
  1941. f->vlan,
  1942. NULL);
  1943. }
  1944. if (aq_ret) {
  1945. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1946. dev_warn(&vsi->back->pdev->dev,
  1947. "Error %s, forcing overflow promiscuous on %s\n",
  1948. i40e_aq_str(hw, hw->aq.asq_last_status),
  1949. vsi_name);
  1950. }
  1951. return aq_ret;
  1952. }
  1953. /**
  1954. * i40e_set_promiscuous - set promiscuous mode
  1955. * @pf: board private structure
  1956. * @promisc: promisc on or off
  1957. *
  1958. * There are different ways of setting promiscuous mode on a PF depending on
  1959. * what state/environment we're in. This identifies and sets it appropriately.
  1960. * Returns 0 on success.
  1961. **/
  1962. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1963. {
  1964. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1965. struct i40e_hw *hw = &pf->hw;
  1966. i40e_status aq_ret;
  1967. if (vsi->type == I40E_VSI_MAIN &&
  1968. pf->lan_veb != I40E_NO_VEB &&
  1969. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1970. /* set defport ON for Main VSI instead of true promisc
  1971. * this way we will get all unicast/multicast and VLAN
  1972. * promisc behavior but will not get VF or VMDq traffic
  1973. * replicated on the Main VSI.
  1974. */
  1975. if (promisc)
  1976. aq_ret = i40e_aq_set_default_vsi(hw,
  1977. vsi->seid,
  1978. NULL);
  1979. else
  1980. aq_ret = i40e_aq_clear_default_vsi(hw,
  1981. vsi->seid,
  1982. NULL);
  1983. if (aq_ret) {
  1984. dev_info(&pf->pdev->dev,
  1985. "Set default VSI failed, err %s, aq_err %s\n",
  1986. i40e_stat_str(hw, aq_ret),
  1987. i40e_aq_str(hw, hw->aq.asq_last_status));
  1988. }
  1989. } else {
  1990. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1991. hw,
  1992. vsi->seid,
  1993. promisc, NULL,
  1994. true);
  1995. if (aq_ret) {
  1996. dev_info(&pf->pdev->dev,
  1997. "set unicast promisc failed, err %s, aq_err %s\n",
  1998. i40e_stat_str(hw, aq_ret),
  1999. i40e_aq_str(hw, hw->aq.asq_last_status));
  2000. }
  2001. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2002. hw,
  2003. vsi->seid,
  2004. promisc, NULL);
  2005. if (aq_ret) {
  2006. dev_info(&pf->pdev->dev,
  2007. "set multicast promisc failed, err %s, aq_err %s\n",
  2008. i40e_stat_str(hw, aq_ret),
  2009. i40e_aq_str(hw, hw->aq.asq_last_status));
  2010. }
  2011. }
  2012. if (!aq_ret)
  2013. pf->cur_promisc = promisc;
  2014. return aq_ret;
  2015. }
  2016. /**
  2017. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  2018. * @vsi: ptr to the VSI
  2019. *
  2020. * Push any outstanding VSI filter changes through the AdminQ.
  2021. *
  2022. * Returns 0 or error value
  2023. **/
  2024. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  2025. {
  2026. struct hlist_head tmp_add_list, tmp_del_list;
  2027. struct i40e_mac_filter *f;
  2028. struct i40e_new_mac_filter *new, *add_head = NULL;
  2029. struct i40e_hw *hw = &vsi->back->hw;
  2030. bool old_overflow, new_overflow;
  2031. unsigned int failed_filters = 0;
  2032. unsigned int vlan_filters = 0;
  2033. char vsi_name[16] = "PF";
  2034. int filter_list_len = 0;
  2035. i40e_status aq_ret = 0;
  2036. u32 changed_flags = 0;
  2037. struct hlist_node *h;
  2038. struct i40e_pf *pf;
  2039. int num_add = 0;
  2040. int num_del = 0;
  2041. int retval = 0;
  2042. u16 cmd_flags;
  2043. int list_size;
  2044. int bkt;
  2045. /* empty array typed pointers, kcalloc later */
  2046. struct i40e_aqc_add_macvlan_element_data *add_list;
  2047. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2048. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2049. usleep_range(1000, 2000);
  2050. pf = vsi->back;
  2051. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2052. if (vsi->netdev) {
  2053. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2054. vsi->current_netdev_flags = vsi->netdev->flags;
  2055. }
  2056. INIT_HLIST_HEAD(&tmp_add_list);
  2057. INIT_HLIST_HEAD(&tmp_del_list);
  2058. if (vsi->type == I40E_VSI_SRIOV)
  2059. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2060. else if (vsi->type != I40E_VSI_MAIN)
  2061. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2062. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2063. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2064. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2065. /* Create a list of filters to delete. */
  2066. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2067. if (f->state == I40E_FILTER_REMOVE) {
  2068. /* Move the element into temporary del_list */
  2069. hash_del(&f->hlist);
  2070. hlist_add_head(&f->hlist, &tmp_del_list);
  2071. /* Avoid counting removed filters */
  2072. continue;
  2073. }
  2074. if (f->state == I40E_FILTER_NEW) {
  2075. /* Create a temporary i40e_new_mac_filter */
  2076. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2077. if (!new)
  2078. goto err_no_memory_locked;
  2079. /* Store pointer to the real filter */
  2080. new->f = f;
  2081. new->state = f->state;
  2082. /* Add it to the hash list */
  2083. hlist_add_head(&new->hlist, &tmp_add_list);
  2084. }
  2085. /* Count the number of active (current and new) VLAN
  2086. * filters we have now. Does not count filters which
  2087. * are marked for deletion.
  2088. */
  2089. if (f->vlan > 0)
  2090. vlan_filters++;
  2091. }
  2092. retval = i40e_correct_mac_vlan_filters(vsi,
  2093. &tmp_add_list,
  2094. &tmp_del_list,
  2095. vlan_filters);
  2096. if (retval)
  2097. goto err_no_memory_locked;
  2098. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2099. }
  2100. /* Now process 'del_list' outside the lock */
  2101. if (!hlist_empty(&tmp_del_list)) {
  2102. filter_list_len = hw->aq.asq_buf_size /
  2103. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2104. list_size = filter_list_len *
  2105. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2106. del_list = kzalloc(list_size, GFP_ATOMIC);
  2107. if (!del_list)
  2108. goto err_no_memory;
  2109. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2110. cmd_flags = 0;
  2111. /* handle broadcast filters by updating the broadcast
  2112. * promiscuous flag and release filter list.
  2113. */
  2114. if (is_broadcast_ether_addr(f->macaddr)) {
  2115. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2116. hlist_del(&f->hlist);
  2117. kfree(f);
  2118. continue;
  2119. }
  2120. /* add to delete list */
  2121. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2122. if (f->vlan == I40E_VLAN_ANY) {
  2123. del_list[num_del].vlan_tag = 0;
  2124. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2125. } else {
  2126. del_list[num_del].vlan_tag =
  2127. cpu_to_le16((u16)(f->vlan));
  2128. }
  2129. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2130. del_list[num_del].flags = cmd_flags;
  2131. num_del++;
  2132. /* flush a full buffer */
  2133. if (num_del == filter_list_len) {
  2134. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2135. num_del, &retval);
  2136. memset(del_list, 0, list_size);
  2137. num_del = 0;
  2138. }
  2139. /* Release memory for MAC filter entries which were
  2140. * synced up with HW.
  2141. */
  2142. hlist_del(&f->hlist);
  2143. kfree(f);
  2144. }
  2145. if (num_del) {
  2146. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2147. num_del, &retval);
  2148. }
  2149. kfree(del_list);
  2150. del_list = NULL;
  2151. }
  2152. if (!hlist_empty(&tmp_add_list)) {
  2153. /* Do all the adds now. */
  2154. filter_list_len = hw->aq.asq_buf_size /
  2155. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2156. list_size = filter_list_len *
  2157. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2158. add_list = kzalloc(list_size, GFP_ATOMIC);
  2159. if (!add_list)
  2160. goto err_no_memory;
  2161. num_add = 0;
  2162. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2163. /* handle broadcast filters by updating the broadcast
  2164. * promiscuous flag instead of adding a MAC filter.
  2165. */
  2166. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2167. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2168. new->f))
  2169. new->state = I40E_FILTER_FAILED;
  2170. else
  2171. new->state = I40E_FILTER_ACTIVE;
  2172. continue;
  2173. }
  2174. /* add to add array */
  2175. if (num_add == 0)
  2176. add_head = new;
  2177. cmd_flags = 0;
  2178. ether_addr_copy(add_list[num_add].mac_addr,
  2179. new->f->macaddr);
  2180. if (new->f->vlan == I40E_VLAN_ANY) {
  2181. add_list[num_add].vlan_tag = 0;
  2182. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2183. } else {
  2184. add_list[num_add].vlan_tag =
  2185. cpu_to_le16((u16)(new->f->vlan));
  2186. }
  2187. add_list[num_add].queue_number = 0;
  2188. /* set invalid match method for later detection */
  2189. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2190. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2191. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2192. num_add++;
  2193. /* flush a full buffer */
  2194. if (num_add == filter_list_len) {
  2195. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2196. add_head, num_add);
  2197. memset(add_list, 0, list_size);
  2198. num_add = 0;
  2199. }
  2200. }
  2201. if (num_add) {
  2202. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2203. num_add);
  2204. }
  2205. /* Now move all of the filters from the temp add list back to
  2206. * the VSI's list.
  2207. */
  2208. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2209. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2210. /* Only update the state if we're still NEW */
  2211. if (new->f->state == I40E_FILTER_NEW)
  2212. new->f->state = new->state;
  2213. hlist_del(&new->hlist);
  2214. kfree(new);
  2215. }
  2216. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2217. kfree(add_list);
  2218. add_list = NULL;
  2219. }
  2220. /* Determine the number of active and failed filters. */
  2221. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2222. vsi->active_filters = 0;
  2223. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2224. if (f->state == I40E_FILTER_ACTIVE)
  2225. vsi->active_filters++;
  2226. else if (f->state == I40E_FILTER_FAILED)
  2227. failed_filters++;
  2228. }
  2229. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2230. /* Check if we are able to exit overflow promiscuous mode. We can
  2231. * safely exit if we didn't just enter, we no longer have any failed
  2232. * filters, and we have reduced filters below the threshold value.
  2233. */
  2234. if (old_overflow && !failed_filters &&
  2235. vsi->active_filters < vsi->promisc_threshold) {
  2236. dev_info(&pf->pdev->dev,
  2237. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2238. vsi_name);
  2239. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2240. vsi->promisc_threshold = 0;
  2241. }
  2242. /* if the VF is not trusted do not do promisc */
  2243. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2244. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2245. goto out;
  2246. }
  2247. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2248. /* If we are entering overflow promiscuous, we need to calculate a new
  2249. * threshold for when we are safe to exit
  2250. */
  2251. if (!old_overflow && new_overflow)
  2252. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2253. /* check for changes in promiscuous modes */
  2254. if (changed_flags & IFF_ALLMULTI) {
  2255. bool cur_multipromisc;
  2256. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2257. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2258. vsi->seid,
  2259. cur_multipromisc,
  2260. NULL);
  2261. if (aq_ret) {
  2262. retval = i40e_aq_rc_to_posix(aq_ret,
  2263. hw->aq.asq_last_status);
  2264. dev_info(&pf->pdev->dev,
  2265. "set multi promisc failed on %s, err %s aq_err %s\n",
  2266. vsi_name,
  2267. i40e_stat_str(hw, aq_ret),
  2268. i40e_aq_str(hw, hw->aq.asq_last_status));
  2269. } else {
  2270. dev_info(&pf->pdev->dev, "%s is %s allmulti mode.\n",
  2271. vsi->netdev->name,
  2272. cur_multipromisc ? "entering" : "leaving");
  2273. }
  2274. }
  2275. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2276. bool cur_promisc;
  2277. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2278. new_overflow);
  2279. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2280. if (aq_ret) {
  2281. retval = i40e_aq_rc_to_posix(aq_ret,
  2282. hw->aq.asq_last_status);
  2283. dev_info(&pf->pdev->dev,
  2284. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2285. cur_promisc ? "on" : "off",
  2286. vsi_name,
  2287. i40e_stat_str(hw, aq_ret),
  2288. i40e_aq_str(hw, hw->aq.asq_last_status));
  2289. }
  2290. }
  2291. out:
  2292. /* if something went wrong then set the changed flag so we try again */
  2293. if (retval)
  2294. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2295. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2296. return retval;
  2297. err_no_memory:
  2298. /* Restore elements on the temporary add and delete lists */
  2299. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2300. err_no_memory_locked:
  2301. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2302. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2303. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2304. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2305. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2306. return -ENOMEM;
  2307. }
  2308. /**
  2309. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2310. * @pf: board private structure
  2311. **/
  2312. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2313. {
  2314. int v;
  2315. if (!pf)
  2316. return;
  2317. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2318. return;
  2319. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state)) {
  2320. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  2321. return;
  2322. }
  2323. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2324. if (pf->vsi[v] &&
  2325. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2326. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2327. if (ret) {
  2328. /* come back and try again later */
  2329. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2330. pf->state);
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. clear_bit(__I40E_VF_DISABLE, pf->state);
  2336. }
  2337. /**
  2338. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2339. * @vsi: the vsi
  2340. **/
  2341. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2342. {
  2343. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2344. return I40E_RXBUFFER_2048;
  2345. else
  2346. return I40E_RXBUFFER_3072;
  2347. }
  2348. /**
  2349. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2350. * @netdev: network interface device structure
  2351. * @new_mtu: new value for maximum frame size
  2352. *
  2353. * Returns 0 on success, negative on failure
  2354. **/
  2355. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2356. {
  2357. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2358. struct i40e_vsi *vsi = np->vsi;
  2359. struct i40e_pf *pf = vsi->back;
  2360. if (i40e_enabled_xdp_vsi(vsi)) {
  2361. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2362. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2363. return -EINVAL;
  2364. }
  2365. netdev_dbg(netdev, "changing MTU from %d to %d\n",
  2366. netdev->mtu, new_mtu);
  2367. netdev->mtu = new_mtu;
  2368. if (netif_running(netdev))
  2369. i40e_vsi_reinit_locked(vsi);
  2370. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2371. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2372. return 0;
  2373. }
  2374. /**
  2375. * i40e_ioctl - Access the hwtstamp interface
  2376. * @netdev: network interface device structure
  2377. * @ifr: interface request data
  2378. * @cmd: ioctl command
  2379. **/
  2380. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2381. {
  2382. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2383. struct i40e_pf *pf = np->vsi->back;
  2384. switch (cmd) {
  2385. case SIOCGHWTSTAMP:
  2386. return i40e_ptp_get_ts_config(pf, ifr);
  2387. case SIOCSHWTSTAMP:
  2388. return i40e_ptp_set_ts_config(pf, ifr);
  2389. default:
  2390. return -EOPNOTSUPP;
  2391. }
  2392. }
  2393. /**
  2394. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2395. * @vsi: the vsi being adjusted
  2396. **/
  2397. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2398. {
  2399. struct i40e_vsi_context ctxt;
  2400. i40e_status ret;
  2401. /* Don't modify stripping options if a port VLAN is active */
  2402. if (vsi->info.pvid)
  2403. return;
  2404. if ((vsi->info.valid_sections &
  2405. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2406. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2407. return; /* already enabled */
  2408. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2409. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2410. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2411. ctxt.seid = vsi->seid;
  2412. ctxt.info = vsi->info;
  2413. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2414. if (ret) {
  2415. dev_info(&vsi->back->pdev->dev,
  2416. "update vlan stripping failed, err %s aq_err %s\n",
  2417. i40e_stat_str(&vsi->back->hw, ret),
  2418. i40e_aq_str(&vsi->back->hw,
  2419. vsi->back->hw.aq.asq_last_status));
  2420. }
  2421. }
  2422. /**
  2423. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2424. * @vsi: the vsi being adjusted
  2425. **/
  2426. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2427. {
  2428. struct i40e_vsi_context ctxt;
  2429. i40e_status ret;
  2430. /* Don't modify stripping options if a port VLAN is active */
  2431. if (vsi->info.pvid)
  2432. return;
  2433. if ((vsi->info.valid_sections &
  2434. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2435. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2436. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2437. return; /* already disabled */
  2438. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2439. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2440. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2441. ctxt.seid = vsi->seid;
  2442. ctxt.info = vsi->info;
  2443. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2444. if (ret) {
  2445. dev_info(&vsi->back->pdev->dev,
  2446. "update vlan stripping failed, err %s aq_err %s\n",
  2447. i40e_stat_str(&vsi->back->hw, ret),
  2448. i40e_aq_str(&vsi->back->hw,
  2449. vsi->back->hw.aq.asq_last_status));
  2450. }
  2451. }
  2452. /**
  2453. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2454. * @vsi: the vsi being configured
  2455. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2456. *
  2457. * This is a helper function for adding a new MAC/VLAN filter with the
  2458. * specified VLAN for each existing MAC address already in the hash table.
  2459. * This function does *not* perform any accounting to update filters based on
  2460. * VLAN mode.
  2461. *
  2462. * NOTE: this function expects to be called while under the
  2463. * mac_filter_hash_lock
  2464. **/
  2465. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2466. {
  2467. struct i40e_mac_filter *f, *add_f;
  2468. struct hlist_node *h;
  2469. int bkt;
  2470. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2471. if (f->state == I40E_FILTER_REMOVE)
  2472. continue;
  2473. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2474. if (!add_f) {
  2475. dev_info(&vsi->back->pdev->dev,
  2476. "Could not add vlan filter %d for %pM\n",
  2477. vid, f->macaddr);
  2478. return -ENOMEM;
  2479. }
  2480. }
  2481. return 0;
  2482. }
  2483. /**
  2484. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2485. * @vsi: the VSI being configured
  2486. * @vid: VLAN id to be added
  2487. **/
  2488. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2489. {
  2490. int err;
  2491. if (vsi->info.pvid)
  2492. return -EINVAL;
  2493. /* The network stack will attempt to add VID=0, with the intention to
  2494. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2495. * these packets by default when configured to receive untagged
  2496. * packets, so we don't need to add a filter for this case.
  2497. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2498. * receive *only* tagged traffic and stops receiving untagged traffic.
  2499. * Thus, we do not want to actually add a filter for VID=0
  2500. */
  2501. if (!vid)
  2502. return 0;
  2503. /* Locked once because all functions invoked below iterates list*/
  2504. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2505. err = i40e_add_vlan_all_mac(vsi, vid);
  2506. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2507. if (err)
  2508. return err;
  2509. /* schedule our worker thread which will take care of
  2510. * applying the new filter changes
  2511. */
  2512. i40e_service_event_schedule(vsi->back);
  2513. return 0;
  2514. }
  2515. /**
  2516. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2517. * @vsi: the vsi being configured
  2518. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2519. *
  2520. * This function should be used to remove all VLAN filters which match the
  2521. * given VID. It does not schedule the service event and does not take the
  2522. * mac_filter_hash_lock so it may be combined with other operations under
  2523. * a single invocation of the mac_filter_hash_lock.
  2524. *
  2525. * NOTE: this function expects to be called while under the
  2526. * mac_filter_hash_lock
  2527. */
  2528. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2529. {
  2530. struct i40e_mac_filter *f;
  2531. struct hlist_node *h;
  2532. int bkt;
  2533. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2534. if (f->vlan == vid)
  2535. __i40e_del_filter(vsi, f);
  2536. }
  2537. }
  2538. /**
  2539. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2540. * @vsi: the VSI being configured
  2541. * @vid: VLAN id to be removed
  2542. **/
  2543. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2544. {
  2545. if (!vid || vsi->info.pvid)
  2546. return;
  2547. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2548. i40e_rm_vlan_all_mac(vsi, vid);
  2549. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2550. /* schedule our worker thread which will take care of
  2551. * applying the new filter changes
  2552. */
  2553. i40e_service_event_schedule(vsi->back);
  2554. }
  2555. /**
  2556. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2557. * @netdev: network interface to be adjusted
  2558. * @proto: unused protocol value
  2559. * @vid: vlan id to be added
  2560. *
  2561. * net_device_ops implementation for adding vlan ids
  2562. **/
  2563. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2564. __always_unused __be16 proto, u16 vid)
  2565. {
  2566. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2567. struct i40e_vsi *vsi = np->vsi;
  2568. int ret = 0;
  2569. if (vid >= VLAN_N_VID)
  2570. return -EINVAL;
  2571. ret = i40e_vsi_add_vlan(vsi, vid);
  2572. if (!ret)
  2573. set_bit(vid, vsi->active_vlans);
  2574. return ret;
  2575. }
  2576. /**
  2577. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2578. * @netdev: network interface to be adjusted
  2579. * @proto: unused protocol value
  2580. * @vid: vlan id to be added
  2581. **/
  2582. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2583. __always_unused __be16 proto, u16 vid)
  2584. {
  2585. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2586. struct i40e_vsi *vsi = np->vsi;
  2587. if (vid >= VLAN_N_VID)
  2588. return;
  2589. set_bit(vid, vsi->active_vlans);
  2590. }
  2591. /**
  2592. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2593. * @netdev: network interface to be adjusted
  2594. * @proto: unused protocol value
  2595. * @vid: vlan id to be removed
  2596. *
  2597. * net_device_ops implementation for removing vlan ids
  2598. **/
  2599. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2600. __always_unused __be16 proto, u16 vid)
  2601. {
  2602. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2603. struct i40e_vsi *vsi = np->vsi;
  2604. /* return code is ignored as there is nothing a user
  2605. * can do about failure to remove and a log message was
  2606. * already printed from the other function
  2607. */
  2608. i40e_vsi_kill_vlan(vsi, vid);
  2609. clear_bit(vid, vsi->active_vlans);
  2610. return 0;
  2611. }
  2612. /**
  2613. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2614. * @vsi: the vsi being brought back up
  2615. **/
  2616. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2617. {
  2618. u16 vid;
  2619. if (!vsi->netdev)
  2620. return;
  2621. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2622. i40e_vlan_stripping_enable(vsi);
  2623. else
  2624. i40e_vlan_stripping_disable(vsi);
  2625. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2626. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2627. vid);
  2628. }
  2629. /**
  2630. * i40e_vsi_add_pvid - Add pvid for the VSI
  2631. * @vsi: the vsi being adjusted
  2632. * @vid: the vlan id to set as a PVID
  2633. **/
  2634. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2635. {
  2636. struct i40e_vsi_context ctxt;
  2637. i40e_status ret;
  2638. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2639. vsi->info.pvid = cpu_to_le16(vid);
  2640. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2641. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2642. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2643. ctxt.seid = vsi->seid;
  2644. ctxt.info = vsi->info;
  2645. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2646. if (ret) {
  2647. dev_info(&vsi->back->pdev->dev,
  2648. "add pvid failed, err %s aq_err %s\n",
  2649. i40e_stat_str(&vsi->back->hw, ret),
  2650. i40e_aq_str(&vsi->back->hw,
  2651. vsi->back->hw.aq.asq_last_status));
  2652. return -ENOENT;
  2653. }
  2654. return 0;
  2655. }
  2656. /**
  2657. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2658. * @vsi: the vsi being adjusted
  2659. *
  2660. * Just use the vlan_rx_register() service to put it back to normal
  2661. **/
  2662. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2663. {
  2664. vsi->info.pvid = 0;
  2665. i40e_vlan_stripping_disable(vsi);
  2666. }
  2667. /**
  2668. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2669. * @vsi: ptr to the VSI
  2670. *
  2671. * If this function returns with an error, then it's possible one or
  2672. * more of the rings is populated (while the rest are not). It is the
  2673. * callers duty to clean those orphaned rings.
  2674. *
  2675. * Return 0 on success, negative on failure
  2676. **/
  2677. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2678. {
  2679. int i, err = 0;
  2680. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2681. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2682. if (!i40e_enabled_xdp_vsi(vsi))
  2683. return err;
  2684. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2685. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2686. return err;
  2687. }
  2688. /**
  2689. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2690. * @vsi: ptr to the VSI
  2691. *
  2692. * Free VSI's transmit software resources
  2693. **/
  2694. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2695. {
  2696. int i;
  2697. if (vsi->tx_rings) {
  2698. for (i = 0; i < vsi->num_queue_pairs; i++)
  2699. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2700. i40e_free_tx_resources(vsi->tx_rings[i]);
  2701. }
  2702. if (vsi->xdp_rings) {
  2703. for (i = 0; i < vsi->num_queue_pairs; i++)
  2704. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2705. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2706. }
  2707. }
  2708. /**
  2709. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2710. * @vsi: ptr to the VSI
  2711. *
  2712. * If this function returns with an error, then it's possible one or
  2713. * more of the rings is populated (while the rest are not). It is the
  2714. * callers duty to clean those orphaned rings.
  2715. *
  2716. * Return 0 on success, negative on failure
  2717. **/
  2718. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2719. {
  2720. int i, err = 0;
  2721. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2722. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2723. return err;
  2724. }
  2725. /**
  2726. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2727. * @vsi: ptr to the VSI
  2728. *
  2729. * Free all receive software resources
  2730. **/
  2731. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2732. {
  2733. int i;
  2734. if (!vsi->rx_rings)
  2735. return;
  2736. for (i = 0; i < vsi->num_queue_pairs; i++)
  2737. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2738. i40e_free_rx_resources(vsi->rx_rings[i]);
  2739. }
  2740. /**
  2741. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2742. * @ring: The Tx ring to configure
  2743. *
  2744. * This enables/disables XPS for a given Tx descriptor ring
  2745. * based on the TCs enabled for the VSI that ring belongs to.
  2746. **/
  2747. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2748. {
  2749. int cpu;
  2750. if (!ring->q_vector || !ring->netdev || ring->ch)
  2751. return;
  2752. /* We only initialize XPS once, so as not to overwrite user settings */
  2753. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2754. return;
  2755. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2756. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2757. ring->queue_index);
  2758. }
  2759. /**
  2760. * i40e_xsk_umem - Retrieve the AF_XDP ZC if XDP and ZC is enabled
  2761. * @ring: The Tx or Rx ring
  2762. *
  2763. * Returns the UMEM or NULL.
  2764. **/
  2765. static struct xdp_umem *i40e_xsk_umem(struct i40e_ring *ring)
  2766. {
  2767. bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
  2768. int qid = ring->queue_index;
  2769. if (ring_is_xdp(ring))
  2770. qid -= ring->vsi->alloc_queue_pairs;
  2771. if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
  2772. return NULL;
  2773. return xdp_get_umem_from_qid(ring->vsi->netdev, qid);
  2774. }
  2775. /**
  2776. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2777. * @ring: The Tx ring to configure
  2778. *
  2779. * Configure the Tx descriptor ring in the HMC context.
  2780. **/
  2781. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2782. {
  2783. struct i40e_vsi *vsi = ring->vsi;
  2784. u16 pf_q = vsi->base_queue + ring->queue_index;
  2785. struct i40e_hw *hw = &vsi->back->hw;
  2786. struct i40e_hmc_obj_txq tx_ctx;
  2787. i40e_status err = 0;
  2788. u32 qtx_ctl = 0;
  2789. if (ring_is_xdp(ring))
  2790. ring->xsk_umem = i40e_xsk_umem(ring);
  2791. /* some ATR related tx ring init */
  2792. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2793. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2794. ring->atr_count = 0;
  2795. } else {
  2796. ring->atr_sample_rate = 0;
  2797. }
  2798. /* configure XPS */
  2799. i40e_config_xps_tx_ring(ring);
  2800. /* clear the context structure first */
  2801. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2802. tx_ctx.new_context = 1;
  2803. tx_ctx.base = (ring->dma / 128);
  2804. tx_ctx.qlen = ring->count;
  2805. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2806. I40E_FLAG_FD_ATR_ENABLED));
  2807. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2808. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2809. if (vsi->type != I40E_VSI_FDIR)
  2810. tx_ctx.head_wb_ena = 1;
  2811. tx_ctx.head_wb_addr = ring->dma +
  2812. (ring->count * sizeof(struct i40e_tx_desc));
  2813. /* As part of VSI creation/update, FW allocates certain
  2814. * Tx arbitration queue sets for each TC enabled for
  2815. * the VSI. The FW returns the handles to these queue
  2816. * sets as part of the response buffer to Add VSI,
  2817. * Update VSI, etc. AQ commands. It is expected that
  2818. * these queue set handles be associated with the Tx
  2819. * queues by the driver as part of the TX queue context
  2820. * initialization. This has to be done regardless of
  2821. * DCB as by default everything is mapped to TC0.
  2822. */
  2823. if (ring->ch)
  2824. tx_ctx.rdylist =
  2825. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2826. else
  2827. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2828. tx_ctx.rdylist_act = 0;
  2829. /* clear the context in the HMC */
  2830. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2831. if (err) {
  2832. dev_info(&vsi->back->pdev->dev,
  2833. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2834. ring->queue_index, pf_q, err);
  2835. return -ENOMEM;
  2836. }
  2837. /* set the context in the HMC */
  2838. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2839. if (err) {
  2840. dev_info(&vsi->back->pdev->dev,
  2841. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2842. ring->queue_index, pf_q, err);
  2843. return -ENOMEM;
  2844. }
  2845. /* Now associate this queue with this PCI function */
  2846. if (ring->ch) {
  2847. if (ring->ch->type == I40E_VSI_VMDQ2)
  2848. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2849. else
  2850. return -EINVAL;
  2851. qtx_ctl |= (ring->ch->vsi_number <<
  2852. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2853. I40E_QTX_CTL_VFVM_INDX_MASK;
  2854. } else {
  2855. if (vsi->type == I40E_VSI_VMDQ2) {
  2856. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2857. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2858. I40E_QTX_CTL_VFVM_INDX_MASK;
  2859. } else {
  2860. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2861. }
  2862. }
  2863. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2864. I40E_QTX_CTL_PF_INDX_MASK);
  2865. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2866. i40e_flush(hw);
  2867. /* cache tail off for easier writes later */
  2868. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2869. return 0;
  2870. }
  2871. /**
  2872. * i40e_configure_rx_ring - Configure a receive ring context
  2873. * @ring: The Rx ring to configure
  2874. *
  2875. * Configure the Rx descriptor ring in the HMC context.
  2876. **/
  2877. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2878. {
  2879. struct i40e_vsi *vsi = ring->vsi;
  2880. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2881. u16 pf_q = vsi->base_queue + ring->queue_index;
  2882. struct i40e_hw *hw = &vsi->back->hw;
  2883. struct i40e_hmc_obj_rxq rx_ctx;
  2884. i40e_status err = 0;
  2885. bool ok;
  2886. int ret;
  2887. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2888. /* clear the context structure first */
  2889. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2890. if (ring->vsi->type == I40E_VSI_MAIN)
  2891. xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
  2892. ring->xsk_umem = i40e_xsk_umem(ring);
  2893. if (ring->xsk_umem) {
  2894. ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
  2895. XDP_PACKET_HEADROOM;
  2896. /* For AF_XDP ZC, we disallow packets to span on
  2897. * multiple buffers, thus letting us skip that
  2898. * handling in the fast-path.
  2899. */
  2900. chain_len = 1;
  2901. ring->zca.free = i40e_zca_free;
  2902. ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  2903. MEM_TYPE_ZERO_COPY,
  2904. &ring->zca);
  2905. if (ret)
  2906. return ret;
  2907. dev_info(&vsi->back->pdev->dev,
  2908. "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
  2909. ring->queue_index);
  2910. } else {
  2911. ring->rx_buf_len = vsi->rx_buf_len;
  2912. if (ring->vsi->type == I40E_VSI_MAIN) {
  2913. ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  2914. MEM_TYPE_PAGE_SHARED,
  2915. NULL);
  2916. if (ret)
  2917. return ret;
  2918. }
  2919. }
  2920. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2921. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2922. rx_ctx.base = (ring->dma / 128);
  2923. rx_ctx.qlen = ring->count;
  2924. /* use 32 byte descriptors */
  2925. rx_ctx.dsize = 1;
  2926. /* descriptor type is always zero
  2927. * rx_ctx.dtype = 0;
  2928. */
  2929. rx_ctx.hsplit_0 = 0;
  2930. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2931. if (hw->revision_id == 0)
  2932. rx_ctx.lrxqthresh = 0;
  2933. else
  2934. rx_ctx.lrxqthresh = 1;
  2935. rx_ctx.crcstrip = 1;
  2936. rx_ctx.l2tsel = 1;
  2937. /* this controls whether VLAN is stripped from inner headers */
  2938. rx_ctx.showiv = 0;
  2939. /* set the prefena field to 1 because the manual says to */
  2940. rx_ctx.prefena = 1;
  2941. /* clear the context in the HMC */
  2942. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2943. if (err) {
  2944. dev_info(&vsi->back->pdev->dev,
  2945. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2946. ring->queue_index, pf_q, err);
  2947. return -ENOMEM;
  2948. }
  2949. /* set the context in the HMC */
  2950. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2951. if (err) {
  2952. dev_info(&vsi->back->pdev->dev,
  2953. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2954. ring->queue_index, pf_q, err);
  2955. return -ENOMEM;
  2956. }
  2957. /* configure Rx buffer alignment */
  2958. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2959. clear_ring_build_skb_enabled(ring);
  2960. else
  2961. set_ring_build_skb_enabled(ring);
  2962. /* cache tail for quicker writes, and clear the reg before use */
  2963. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2964. writel(0, ring->tail);
  2965. ok = ring->xsk_umem ?
  2966. i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
  2967. !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2968. if (!ok) {
  2969. /* Log this in case the user has forgotten to give the kernel
  2970. * any buffers, even later in the application.
  2971. */
  2972. dev_info(&vsi->back->pdev->dev,
  2973. "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
  2974. ring->xsk_umem ? "UMEM enabled " : "",
  2975. ring->queue_index, pf_q);
  2976. }
  2977. return 0;
  2978. }
  2979. /**
  2980. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2981. * @vsi: VSI structure describing this set of rings and resources
  2982. *
  2983. * Configure the Tx VSI for operation.
  2984. **/
  2985. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2986. {
  2987. int err = 0;
  2988. u16 i;
  2989. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2990. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2991. if (err || !i40e_enabled_xdp_vsi(vsi))
  2992. return err;
  2993. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2994. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2995. return err;
  2996. }
  2997. /**
  2998. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2999. * @vsi: the VSI being configured
  3000. *
  3001. * Configure the Rx VSI for operation.
  3002. **/
  3003. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  3004. {
  3005. int err = 0;
  3006. u16 i;
  3007. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  3008. vsi->max_frame = I40E_MAX_RXBUFFER;
  3009. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  3010. #if (PAGE_SIZE < 8192)
  3011. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  3012. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  3013. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  3014. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  3015. #endif
  3016. } else {
  3017. vsi->max_frame = I40E_MAX_RXBUFFER;
  3018. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  3019. I40E_RXBUFFER_2048;
  3020. }
  3021. /* set up individual rings */
  3022. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  3023. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  3024. return err;
  3025. }
  3026. /**
  3027. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  3028. * @vsi: ptr to the VSI
  3029. **/
  3030. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  3031. {
  3032. struct i40e_ring *tx_ring, *rx_ring;
  3033. u16 qoffset, qcount;
  3034. int i, n;
  3035. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  3036. /* Reset the TC information */
  3037. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3038. rx_ring = vsi->rx_rings[i];
  3039. tx_ring = vsi->tx_rings[i];
  3040. rx_ring->dcb_tc = 0;
  3041. tx_ring->dcb_tc = 0;
  3042. }
  3043. return;
  3044. }
  3045. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  3046. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  3047. continue;
  3048. qoffset = vsi->tc_config.tc_info[n].qoffset;
  3049. qcount = vsi->tc_config.tc_info[n].qcount;
  3050. for (i = qoffset; i < (qoffset + qcount); i++) {
  3051. rx_ring = vsi->rx_rings[i];
  3052. tx_ring = vsi->tx_rings[i];
  3053. rx_ring->dcb_tc = n;
  3054. tx_ring->dcb_tc = n;
  3055. }
  3056. }
  3057. }
  3058. /**
  3059. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  3060. * @vsi: ptr to the VSI
  3061. **/
  3062. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  3063. {
  3064. if (vsi->netdev)
  3065. i40e_set_rx_mode(vsi->netdev);
  3066. }
  3067. /**
  3068. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  3069. * @vsi: Pointer to the targeted VSI
  3070. *
  3071. * This function replays the hlist on the hw where all the SB Flow Director
  3072. * filters were saved.
  3073. **/
  3074. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  3075. {
  3076. struct i40e_fdir_filter *filter;
  3077. struct i40e_pf *pf = vsi->back;
  3078. struct hlist_node *node;
  3079. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  3080. return;
  3081. /* Reset FDir counters as we're replaying all existing filters */
  3082. pf->fd_tcp4_filter_cnt = 0;
  3083. pf->fd_udp4_filter_cnt = 0;
  3084. pf->fd_sctp4_filter_cnt = 0;
  3085. pf->fd_ip4_filter_cnt = 0;
  3086. hlist_for_each_entry_safe(filter, node,
  3087. &pf->fdir_filter_list, fdir_node) {
  3088. i40e_add_del_fdir(vsi, filter, true);
  3089. }
  3090. }
  3091. /**
  3092. * i40e_vsi_configure - Set up the VSI for action
  3093. * @vsi: the VSI being configured
  3094. **/
  3095. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  3096. {
  3097. int err;
  3098. i40e_set_vsi_rx_mode(vsi);
  3099. i40e_restore_vlan(vsi);
  3100. i40e_vsi_config_dcb_rings(vsi);
  3101. err = i40e_vsi_configure_tx(vsi);
  3102. if (!err)
  3103. err = i40e_vsi_configure_rx(vsi);
  3104. return err;
  3105. }
  3106. /**
  3107. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3108. * @vsi: the VSI being configured
  3109. **/
  3110. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3111. {
  3112. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3113. struct i40e_pf *pf = vsi->back;
  3114. struct i40e_hw *hw = &pf->hw;
  3115. u16 vector;
  3116. int i, q;
  3117. u32 qp;
  3118. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3119. * and PFINT_LNKLSTn registers, e.g.:
  3120. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3121. */
  3122. qp = vsi->base_queue;
  3123. vector = vsi->base_vector;
  3124. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3125. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3126. q_vector->rx.next_update = jiffies + 1;
  3127. q_vector->rx.target_itr =
  3128. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3129. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3130. q_vector->rx.target_itr >> 1);
  3131. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3132. q_vector->tx.next_update = jiffies + 1;
  3133. q_vector->tx.target_itr =
  3134. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3135. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3136. q_vector->tx.target_itr >> 1);
  3137. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3138. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3139. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3140. /* Linked list for the queuepairs assigned to this vector */
  3141. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3142. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3143. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3144. u32 val;
  3145. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3146. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3147. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3148. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3149. (I40E_QUEUE_TYPE_TX <<
  3150. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3151. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3152. if (has_xdp) {
  3153. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3154. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3155. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3156. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3157. (I40E_QUEUE_TYPE_TX <<
  3158. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3159. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3160. }
  3161. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3162. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3163. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3164. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3165. (I40E_QUEUE_TYPE_RX <<
  3166. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3167. /* Terminate the linked list */
  3168. if (q == (q_vector->num_ringpairs - 1))
  3169. val |= (I40E_QUEUE_END_OF_LIST <<
  3170. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3171. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3172. qp++;
  3173. }
  3174. }
  3175. i40e_flush(hw);
  3176. }
  3177. /**
  3178. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3179. * @pf: pointer to private device data structure
  3180. **/
  3181. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3182. {
  3183. struct i40e_hw *hw = &pf->hw;
  3184. u32 val;
  3185. /* clear things first */
  3186. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3187. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3188. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3189. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3190. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3191. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3192. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3193. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3194. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3195. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3196. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3197. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3198. if (pf->flags & I40E_FLAG_PTP)
  3199. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3200. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3201. /* SW_ITR_IDX = 0, but don't change INTENA */
  3202. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3203. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3204. /* OTHER_ITR_IDX = 0 */
  3205. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3206. }
  3207. /**
  3208. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3209. * @vsi: the VSI being configured
  3210. **/
  3211. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3212. {
  3213. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3214. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3215. struct i40e_pf *pf = vsi->back;
  3216. struct i40e_hw *hw = &pf->hw;
  3217. u32 val;
  3218. /* set the ITR configuration */
  3219. q_vector->rx.next_update = jiffies + 1;
  3220. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3221. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
  3222. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3223. q_vector->tx.next_update = jiffies + 1;
  3224. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3225. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
  3226. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3227. i40e_enable_misc_int_causes(pf);
  3228. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3229. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3230. /* Associate the queue pair to the vector and enable the queue int */
  3231. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3232. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3233. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3234. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3235. wr32(hw, I40E_QINT_RQCTL(0), val);
  3236. if (i40e_enabled_xdp_vsi(vsi)) {
  3237. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3238. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3239. (I40E_QUEUE_TYPE_TX
  3240. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3241. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3242. }
  3243. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3244. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3245. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3246. wr32(hw, I40E_QINT_TQCTL(0), val);
  3247. i40e_flush(hw);
  3248. }
  3249. /**
  3250. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3251. * @pf: board private structure
  3252. **/
  3253. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3254. {
  3255. struct i40e_hw *hw = &pf->hw;
  3256. wr32(hw, I40E_PFINT_DYN_CTL0,
  3257. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3258. i40e_flush(hw);
  3259. }
  3260. /**
  3261. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3262. * @pf: board private structure
  3263. **/
  3264. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3265. {
  3266. struct i40e_hw *hw = &pf->hw;
  3267. u32 val;
  3268. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3269. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3270. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3271. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3272. i40e_flush(hw);
  3273. }
  3274. /**
  3275. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3276. * @irq: interrupt number
  3277. * @data: pointer to a q_vector
  3278. **/
  3279. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3280. {
  3281. struct i40e_q_vector *q_vector = data;
  3282. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3283. return IRQ_HANDLED;
  3284. napi_schedule_irqoff(&q_vector->napi);
  3285. return IRQ_HANDLED;
  3286. }
  3287. /**
  3288. * i40e_irq_affinity_notify - Callback for affinity changes
  3289. * @notify: context as to what irq was changed
  3290. * @mask: the new affinity mask
  3291. *
  3292. * This is a callback function used by the irq_set_affinity_notifier function
  3293. * so that we may register to receive changes to the irq affinity masks.
  3294. **/
  3295. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3296. const cpumask_t *mask)
  3297. {
  3298. struct i40e_q_vector *q_vector =
  3299. container_of(notify, struct i40e_q_vector, affinity_notify);
  3300. cpumask_copy(&q_vector->affinity_mask, mask);
  3301. }
  3302. /**
  3303. * i40e_irq_affinity_release - Callback for affinity notifier release
  3304. * @ref: internal core kernel usage
  3305. *
  3306. * This is a callback function used by the irq_set_affinity_notifier function
  3307. * to inform the current notification subscriber that they will no longer
  3308. * receive notifications.
  3309. **/
  3310. static void i40e_irq_affinity_release(struct kref *ref) {}
  3311. /**
  3312. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3313. * @vsi: the VSI being configured
  3314. * @basename: name for the vector
  3315. *
  3316. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3317. **/
  3318. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3319. {
  3320. int q_vectors = vsi->num_q_vectors;
  3321. struct i40e_pf *pf = vsi->back;
  3322. int base = vsi->base_vector;
  3323. int rx_int_idx = 0;
  3324. int tx_int_idx = 0;
  3325. int vector, err;
  3326. int irq_num;
  3327. int cpu;
  3328. for (vector = 0; vector < q_vectors; vector++) {
  3329. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3330. irq_num = pf->msix_entries[base + vector].vector;
  3331. if (q_vector->tx.ring && q_vector->rx.ring) {
  3332. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3333. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3334. tx_int_idx++;
  3335. } else if (q_vector->rx.ring) {
  3336. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3337. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3338. } else if (q_vector->tx.ring) {
  3339. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3340. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3341. } else {
  3342. /* skip this unused q_vector */
  3343. continue;
  3344. }
  3345. err = request_irq(irq_num,
  3346. vsi->irq_handler,
  3347. 0,
  3348. q_vector->name,
  3349. q_vector);
  3350. if (err) {
  3351. dev_info(&pf->pdev->dev,
  3352. "MSIX request_irq failed, error: %d\n", err);
  3353. goto free_queue_irqs;
  3354. }
  3355. /* register for affinity change notifications */
  3356. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3357. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3358. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3359. /* Spread affinity hints out across online CPUs.
  3360. *
  3361. * get_cpu_mask returns a static constant mask with
  3362. * a permanent lifetime so it's ok to pass to
  3363. * irq_set_affinity_hint without making a copy.
  3364. */
  3365. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3366. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3367. }
  3368. vsi->irqs_ready = true;
  3369. return 0;
  3370. free_queue_irqs:
  3371. while (vector) {
  3372. vector--;
  3373. irq_num = pf->msix_entries[base + vector].vector;
  3374. irq_set_affinity_notifier(irq_num, NULL);
  3375. irq_set_affinity_hint(irq_num, NULL);
  3376. free_irq(irq_num, &vsi->q_vectors[vector]);
  3377. }
  3378. return err;
  3379. }
  3380. /**
  3381. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3382. * @vsi: the VSI being un-configured
  3383. **/
  3384. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3385. {
  3386. struct i40e_pf *pf = vsi->back;
  3387. struct i40e_hw *hw = &pf->hw;
  3388. int base = vsi->base_vector;
  3389. int i;
  3390. /* disable interrupt causation from each queue */
  3391. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3392. u32 val;
  3393. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3394. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3395. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3396. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3397. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3398. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3399. if (!i40e_enabled_xdp_vsi(vsi))
  3400. continue;
  3401. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3402. }
  3403. /* disable each interrupt */
  3404. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3405. for (i = vsi->base_vector;
  3406. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3407. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3408. i40e_flush(hw);
  3409. for (i = 0; i < vsi->num_q_vectors; i++)
  3410. synchronize_irq(pf->msix_entries[i + base].vector);
  3411. } else {
  3412. /* Legacy and MSI mode - this stops all interrupt handling */
  3413. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3414. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3415. i40e_flush(hw);
  3416. synchronize_irq(pf->pdev->irq);
  3417. }
  3418. }
  3419. /**
  3420. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3421. * @vsi: the VSI being configured
  3422. **/
  3423. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3424. {
  3425. struct i40e_pf *pf = vsi->back;
  3426. int i;
  3427. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3428. for (i = 0; i < vsi->num_q_vectors; i++)
  3429. i40e_irq_dynamic_enable(vsi, i);
  3430. } else {
  3431. i40e_irq_dynamic_enable_icr0(pf);
  3432. }
  3433. i40e_flush(&pf->hw);
  3434. return 0;
  3435. }
  3436. /**
  3437. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3438. * @pf: board private structure
  3439. **/
  3440. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3441. {
  3442. /* Disable ICR 0 */
  3443. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3444. i40e_flush(&pf->hw);
  3445. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3446. synchronize_irq(pf->msix_entries[0].vector);
  3447. free_irq(pf->msix_entries[0].vector, pf);
  3448. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3449. }
  3450. }
  3451. /**
  3452. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3453. * @irq: interrupt number
  3454. * @data: pointer to a q_vector
  3455. *
  3456. * This is the handler used for all MSI/Legacy interrupts, and deals
  3457. * with both queue and non-queue interrupts. This is also used in
  3458. * MSIX mode to handle the non-queue interrupts.
  3459. **/
  3460. static irqreturn_t i40e_intr(int irq, void *data)
  3461. {
  3462. struct i40e_pf *pf = (struct i40e_pf *)data;
  3463. struct i40e_hw *hw = &pf->hw;
  3464. irqreturn_t ret = IRQ_NONE;
  3465. u32 icr0, icr0_remaining;
  3466. u32 val, ena_mask;
  3467. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3468. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3469. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3470. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3471. goto enable_intr;
  3472. /* if interrupt but no bits showing, must be SWINT */
  3473. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3474. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3475. pf->sw_int_count++;
  3476. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3477. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3478. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3479. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3480. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3481. }
  3482. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3483. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3484. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3485. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3486. /* We do not have a way to disarm Queue causes while leaving
  3487. * interrupt enabled for all other causes, ideally
  3488. * interrupt should be disabled while we are in NAPI but
  3489. * this is not a performance path and napi_schedule()
  3490. * can deal with rescheduling.
  3491. */
  3492. if (!test_bit(__I40E_DOWN, pf->state))
  3493. napi_schedule_irqoff(&q_vector->napi);
  3494. }
  3495. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3496. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3497. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3498. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3499. }
  3500. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3501. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3502. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3503. }
  3504. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3505. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3506. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3507. }
  3508. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3509. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3510. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3511. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3512. val = rd32(hw, I40E_GLGEN_RSTAT);
  3513. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3514. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3515. if (val == I40E_RESET_CORER) {
  3516. pf->corer_count++;
  3517. } else if (val == I40E_RESET_GLOBR) {
  3518. pf->globr_count++;
  3519. } else if (val == I40E_RESET_EMPR) {
  3520. pf->empr_count++;
  3521. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3522. }
  3523. }
  3524. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3525. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3526. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3527. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3528. rd32(hw, I40E_PFHMC_ERRORINFO),
  3529. rd32(hw, I40E_PFHMC_ERRORDATA));
  3530. }
  3531. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3532. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3533. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3534. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3535. i40e_ptp_tx_hwtstamp(pf);
  3536. }
  3537. }
  3538. /* If a critical error is pending we have no choice but to reset the
  3539. * device.
  3540. * Report and mask out any remaining unexpected interrupts.
  3541. */
  3542. icr0_remaining = icr0 & ena_mask;
  3543. if (icr0_remaining) {
  3544. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3545. icr0_remaining);
  3546. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3547. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3548. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3549. dev_info(&pf->pdev->dev, "device will be reset\n");
  3550. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3551. i40e_service_event_schedule(pf);
  3552. }
  3553. ena_mask &= ~icr0_remaining;
  3554. }
  3555. ret = IRQ_HANDLED;
  3556. enable_intr:
  3557. /* re-enable interrupt causes */
  3558. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3559. if (!test_bit(__I40E_DOWN, pf->state) ||
  3560. test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  3561. i40e_service_event_schedule(pf);
  3562. i40e_irq_dynamic_enable_icr0(pf);
  3563. }
  3564. return ret;
  3565. }
  3566. /**
  3567. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3568. * @tx_ring: tx ring to clean
  3569. * @budget: how many cleans we're allowed
  3570. *
  3571. * Returns true if there's any budget left (e.g. the clean is finished)
  3572. **/
  3573. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3574. {
  3575. struct i40e_vsi *vsi = tx_ring->vsi;
  3576. u16 i = tx_ring->next_to_clean;
  3577. struct i40e_tx_buffer *tx_buf;
  3578. struct i40e_tx_desc *tx_desc;
  3579. tx_buf = &tx_ring->tx_bi[i];
  3580. tx_desc = I40E_TX_DESC(tx_ring, i);
  3581. i -= tx_ring->count;
  3582. do {
  3583. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3584. /* if next_to_watch is not set then there is no work pending */
  3585. if (!eop_desc)
  3586. break;
  3587. /* prevent any other reads prior to eop_desc */
  3588. smp_rmb();
  3589. /* if the descriptor isn't done, no work yet to do */
  3590. if (!(eop_desc->cmd_type_offset_bsz &
  3591. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3592. break;
  3593. /* clear next_to_watch to prevent false hangs */
  3594. tx_buf->next_to_watch = NULL;
  3595. tx_desc->buffer_addr = 0;
  3596. tx_desc->cmd_type_offset_bsz = 0;
  3597. /* move past filter desc */
  3598. tx_buf++;
  3599. tx_desc++;
  3600. i++;
  3601. if (unlikely(!i)) {
  3602. i -= tx_ring->count;
  3603. tx_buf = tx_ring->tx_bi;
  3604. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3605. }
  3606. /* unmap skb header data */
  3607. dma_unmap_single(tx_ring->dev,
  3608. dma_unmap_addr(tx_buf, dma),
  3609. dma_unmap_len(tx_buf, len),
  3610. DMA_TO_DEVICE);
  3611. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3612. kfree(tx_buf->raw_buf);
  3613. tx_buf->raw_buf = NULL;
  3614. tx_buf->tx_flags = 0;
  3615. tx_buf->next_to_watch = NULL;
  3616. dma_unmap_len_set(tx_buf, len, 0);
  3617. tx_desc->buffer_addr = 0;
  3618. tx_desc->cmd_type_offset_bsz = 0;
  3619. /* move us past the eop_desc for start of next FD desc */
  3620. tx_buf++;
  3621. tx_desc++;
  3622. i++;
  3623. if (unlikely(!i)) {
  3624. i -= tx_ring->count;
  3625. tx_buf = tx_ring->tx_bi;
  3626. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3627. }
  3628. /* update budget accounting */
  3629. budget--;
  3630. } while (likely(budget));
  3631. i += tx_ring->count;
  3632. tx_ring->next_to_clean = i;
  3633. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3634. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3635. return budget > 0;
  3636. }
  3637. /**
  3638. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3639. * @irq: interrupt number
  3640. * @data: pointer to a q_vector
  3641. **/
  3642. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3643. {
  3644. struct i40e_q_vector *q_vector = data;
  3645. struct i40e_vsi *vsi;
  3646. if (!q_vector->tx.ring)
  3647. return IRQ_HANDLED;
  3648. vsi = q_vector->tx.ring->vsi;
  3649. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3650. return IRQ_HANDLED;
  3651. }
  3652. /**
  3653. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3654. * @vsi: the VSI being configured
  3655. * @v_idx: vector index
  3656. * @qp_idx: queue pair index
  3657. **/
  3658. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3659. {
  3660. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3661. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3662. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3663. tx_ring->q_vector = q_vector;
  3664. tx_ring->next = q_vector->tx.ring;
  3665. q_vector->tx.ring = tx_ring;
  3666. q_vector->tx.count++;
  3667. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3668. if (i40e_enabled_xdp_vsi(vsi)) {
  3669. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3670. xdp_ring->q_vector = q_vector;
  3671. xdp_ring->next = q_vector->tx.ring;
  3672. q_vector->tx.ring = xdp_ring;
  3673. q_vector->tx.count++;
  3674. }
  3675. rx_ring->q_vector = q_vector;
  3676. rx_ring->next = q_vector->rx.ring;
  3677. q_vector->rx.ring = rx_ring;
  3678. q_vector->rx.count++;
  3679. }
  3680. /**
  3681. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3682. * @vsi: the VSI being configured
  3683. *
  3684. * This function maps descriptor rings to the queue-specific vectors
  3685. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3686. * one vector per queue pair, but on a constrained vector budget, we
  3687. * group the queue pairs as "efficiently" as possible.
  3688. **/
  3689. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3690. {
  3691. int qp_remaining = vsi->num_queue_pairs;
  3692. int q_vectors = vsi->num_q_vectors;
  3693. int num_ringpairs;
  3694. int v_start = 0;
  3695. int qp_idx = 0;
  3696. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3697. * group them so there are multiple queues per vector.
  3698. * It is also important to go through all the vectors available to be
  3699. * sure that if we don't use all the vectors, that the remaining vectors
  3700. * are cleared. This is especially important when decreasing the
  3701. * number of queues in use.
  3702. */
  3703. for (; v_start < q_vectors; v_start++) {
  3704. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3705. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3706. q_vector->num_ringpairs = num_ringpairs;
  3707. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3708. q_vector->rx.count = 0;
  3709. q_vector->tx.count = 0;
  3710. q_vector->rx.ring = NULL;
  3711. q_vector->tx.ring = NULL;
  3712. while (num_ringpairs--) {
  3713. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3714. qp_idx++;
  3715. qp_remaining--;
  3716. }
  3717. }
  3718. }
  3719. /**
  3720. * i40e_vsi_request_irq - Request IRQ from the OS
  3721. * @vsi: the VSI being configured
  3722. * @basename: name for the vector
  3723. **/
  3724. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3725. {
  3726. struct i40e_pf *pf = vsi->back;
  3727. int err;
  3728. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3729. err = i40e_vsi_request_irq_msix(vsi, basename);
  3730. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3731. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3732. pf->int_name, pf);
  3733. else
  3734. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3735. pf->int_name, pf);
  3736. if (err)
  3737. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3738. return err;
  3739. }
  3740. #ifdef CONFIG_NET_POLL_CONTROLLER
  3741. /**
  3742. * i40e_netpoll - A Polling 'interrupt' handler
  3743. * @netdev: network interface device structure
  3744. *
  3745. * This is used by netconsole to send skbs without having to re-enable
  3746. * interrupts. It's not called while the normal interrupt routine is executing.
  3747. **/
  3748. static void i40e_netpoll(struct net_device *netdev)
  3749. {
  3750. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3751. struct i40e_vsi *vsi = np->vsi;
  3752. struct i40e_pf *pf = vsi->back;
  3753. int i;
  3754. /* if interface is down do nothing */
  3755. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3756. return;
  3757. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3758. for (i = 0; i < vsi->num_q_vectors; i++)
  3759. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3760. } else {
  3761. i40e_intr(pf->pdev->irq, netdev);
  3762. }
  3763. }
  3764. #endif
  3765. #define I40E_QTX_ENA_WAIT_COUNT 50
  3766. /**
  3767. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3768. * @pf: the PF being configured
  3769. * @pf_q: the PF queue
  3770. * @enable: enable or disable state of the queue
  3771. *
  3772. * This routine will wait for the given Tx queue of the PF to reach the
  3773. * enabled or disabled state.
  3774. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3775. * multiple retries; else will return 0 in case of success.
  3776. **/
  3777. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3778. {
  3779. int i;
  3780. u32 tx_reg;
  3781. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3782. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3783. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3784. break;
  3785. usleep_range(10, 20);
  3786. }
  3787. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3788. return -ETIMEDOUT;
  3789. return 0;
  3790. }
  3791. /**
  3792. * i40e_control_tx_q - Start or stop a particular Tx queue
  3793. * @pf: the PF structure
  3794. * @pf_q: the PF queue to configure
  3795. * @enable: start or stop the queue
  3796. *
  3797. * This function enables or disables a single queue. Note that any delay
  3798. * required after the operation is expected to be handled by the caller of
  3799. * this function.
  3800. **/
  3801. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3802. {
  3803. struct i40e_hw *hw = &pf->hw;
  3804. u32 tx_reg;
  3805. int i;
  3806. /* warn the TX unit of coming changes */
  3807. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3808. if (!enable)
  3809. usleep_range(10, 20);
  3810. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3811. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3812. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3813. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3814. break;
  3815. usleep_range(1000, 2000);
  3816. }
  3817. /* Skip if the queue is already in the requested state */
  3818. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3819. return;
  3820. /* turn on/off the queue */
  3821. if (enable) {
  3822. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3823. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3824. } else {
  3825. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3826. }
  3827. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3828. }
  3829. /**
  3830. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3831. * @seid: VSI SEID
  3832. * @pf: the PF structure
  3833. * @pf_q: the PF queue to configure
  3834. * @is_xdp: true if the queue is used for XDP
  3835. * @enable: start or stop the queue
  3836. **/
  3837. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3838. bool is_xdp, bool enable)
  3839. {
  3840. int ret;
  3841. i40e_control_tx_q(pf, pf_q, enable);
  3842. /* wait for the change to finish */
  3843. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3844. if (ret) {
  3845. dev_info(&pf->pdev->dev,
  3846. "VSI seid %d %sTx ring %d %sable timeout\n",
  3847. seid, (is_xdp ? "XDP " : ""), pf_q,
  3848. (enable ? "en" : "dis"));
  3849. }
  3850. return ret;
  3851. }
  3852. /**
  3853. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3854. * @vsi: the VSI being configured
  3855. * @enable: start or stop the rings
  3856. **/
  3857. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3858. {
  3859. struct i40e_pf *pf = vsi->back;
  3860. int i, pf_q, ret = 0;
  3861. pf_q = vsi->base_queue;
  3862. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3863. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3864. pf_q,
  3865. false /*is xdp*/, enable);
  3866. if (ret)
  3867. break;
  3868. if (!i40e_enabled_xdp_vsi(vsi))
  3869. continue;
  3870. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3871. pf_q + vsi->alloc_queue_pairs,
  3872. true /*is xdp*/, enable);
  3873. if (ret)
  3874. break;
  3875. }
  3876. return ret;
  3877. }
  3878. /**
  3879. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3880. * @pf: the PF being configured
  3881. * @pf_q: the PF queue
  3882. * @enable: enable or disable state of the queue
  3883. *
  3884. * This routine will wait for the given Rx queue of the PF to reach the
  3885. * enabled or disabled state.
  3886. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3887. * multiple retries; else will return 0 in case of success.
  3888. **/
  3889. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3890. {
  3891. int i;
  3892. u32 rx_reg;
  3893. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3894. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3895. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3896. break;
  3897. usleep_range(10, 20);
  3898. }
  3899. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3900. return -ETIMEDOUT;
  3901. return 0;
  3902. }
  3903. /**
  3904. * i40e_control_rx_q - Start or stop a particular Rx queue
  3905. * @pf: the PF structure
  3906. * @pf_q: the PF queue to configure
  3907. * @enable: start or stop the queue
  3908. *
  3909. * This function enables or disables a single queue. Note that
  3910. * any delay required after the operation is expected to be
  3911. * handled by the caller of this function.
  3912. **/
  3913. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3914. {
  3915. struct i40e_hw *hw = &pf->hw;
  3916. u32 rx_reg;
  3917. int i;
  3918. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3919. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3920. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3921. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3922. break;
  3923. usleep_range(1000, 2000);
  3924. }
  3925. /* Skip if the queue is already in the requested state */
  3926. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3927. return;
  3928. /* turn on/off the queue */
  3929. if (enable)
  3930. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3931. else
  3932. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3933. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3934. }
  3935. /**
  3936. * i40e_control_wait_rx_q
  3937. * @pf: the PF structure
  3938. * @pf_q: queue being configured
  3939. * @enable: start or stop the rings
  3940. *
  3941. * This function enables or disables a single queue along with waiting
  3942. * for the change to finish. The caller of this function should handle
  3943. * the delays needed in the case of disabling queues.
  3944. **/
  3945. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3946. {
  3947. int ret = 0;
  3948. i40e_control_rx_q(pf, pf_q, enable);
  3949. /* wait for the change to finish */
  3950. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3951. if (ret)
  3952. return ret;
  3953. return ret;
  3954. }
  3955. /**
  3956. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3957. * @vsi: the VSI being configured
  3958. * @enable: start or stop the rings
  3959. **/
  3960. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3961. {
  3962. struct i40e_pf *pf = vsi->back;
  3963. int i, pf_q, ret = 0;
  3964. pf_q = vsi->base_queue;
  3965. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3966. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3967. if (ret) {
  3968. dev_info(&pf->pdev->dev,
  3969. "VSI seid %d Rx ring %d %sable timeout\n",
  3970. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3971. break;
  3972. }
  3973. }
  3974. /* Due to HW errata, on Rx disable only, the register can indicate done
  3975. * before it really is. Needs 50ms to be sure
  3976. */
  3977. if (!enable)
  3978. mdelay(50);
  3979. return ret;
  3980. }
  3981. /**
  3982. * i40e_vsi_start_rings - Start a VSI's rings
  3983. * @vsi: the VSI being configured
  3984. **/
  3985. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3986. {
  3987. int ret = 0;
  3988. /* do rx first for enable and last for disable */
  3989. ret = i40e_vsi_control_rx(vsi, true);
  3990. if (ret)
  3991. return ret;
  3992. ret = i40e_vsi_control_tx(vsi, true);
  3993. return ret;
  3994. }
  3995. /**
  3996. * i40e_vsi_stop_rings - Stop a VSI's rings
  3997. * @vsi: the VSI being configured
  3998. **/
  3999. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  4000. {
  4001. /* When port TX is suspended, don't wait */
  4002. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  4003. return i40e_vsi_stop_rings_no_wait(vsi);
  4004. /* do rx first for enable and last for disable
  4005. * Ignore return value, we need to shutdown whatever we can
  4006. */
  4007. i40e_vsi_control_tx(vsi, false);
  4008. i40e_vsi_control_rx(vsi, false);
  4009. }
  4010. /**
  4011. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  4012. * @vsi: the VSI being shutdown
  4013. *
  4014. * This function stops all the rings for a VSI but does not delay to verify
  4015. * that rings have been disabled. It is expected that the caller is shutting
  4016. * down multiple VSIs at once and will delay together for all the VSIs after
  4017. * initiating the shutdown. This is particularly useful for shutting down lots
  4018. * of VFs together. Otherwise, a large delay can be incurred while configuring
  4019. * each VSI in serial.
  4020. **/
  4021. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  4022. {
  4023. struct i40e_pf *pf = vsi->back;
  4024. int i, pf_q;
  4025. pf_q = vsi->base_queue;
  4026. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4027. i40e_control_tx_q(pf, pf_q, false);
  4028. i40e_control_rx_q(pf, pf_q, false);
  4029. }
  4030. }
  4031. /**
  4032. * i40e_vsi_free_irq - Free the irq association with the OS
  4033. * @vsi: the VSI being configured
  4034. **/
  4035. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  4036. {
  4037. struct i40e_pf *pf = vsi->back;
  4038. struct i40e_hw *hw = &pf->hw;
  4039. int base = vsi->base_vector;
  4040. u32 val, qp;
  4041. int i;
  4042. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4043. if (!vsi->q_vectors)
  4044. return;
  4045. if (!vsi->irqs_ready)
  4046. return;
  4047. vsi->irqs_ready = false;
  4048. for (i = 0; i < vsi->num_q_vectors; i++) {
  4049. int irq_num;
  4050. u16 vector;
  4051. vector = i + base;
  4052. irq_num = pf->msix_entries[vector].vector;
  4053. /* free only the irqs that were actually requested */
  4054. if (!vsi->q_vectors[i] ||
  4055. !vsi->q_vectors[i]->num_ringpairs)
  4056. continue;
  4057. /* clear the affinity notifier in the IRQ descriptor */
  4058. irq_set_affinity_notifier(irq_num, NULL);
  4059. /* remove our suggested affinity mask for this IRQ */
  4060. irq_set_affinity_hint(irq_num, NULL);
  4061. synchronize_irq(irq_num);
  4062. free_irq(irq_num, vsi->q_vectors[i]);
  4063. /* Tear down the interrupt queue link list
  4064. *
  4065. * We know that they come in pairs and always
  4066. * the Rx first, then the Tx. To clear the
  4067. * link list, stick the EOL value into the
  4068. * next_q field of the registers.
  4069. */
  4070. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  4071. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4072. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4073. val |= I40E_QUEUE_END_OF_LIST
  4074. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4075. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  4076. while (qp != I40E_QUEUE_END_OF_LIST) {
  4077. u32 next;
  4078. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4079. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4080. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4081. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4082. I40E_QINT_RQCTL_INTEVENT_MASK);
  4083. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4084. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4085. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4086. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4087. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  4088. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  4089. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4090. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4091. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4092. I40E_QINT_TQCTL_INTEVENT_MASK);
  4093. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4094. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4095. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4096. qp = next;
  4097. }
  4098. }
  4099. } else {
  4100. free_irq(pf->pdev->irq, pf);
  4101. val = rd32(hw, I40E_PFINT_LNKLST0);
  4102. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4103. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4104. val |= I40E_QUEUE_END_OF_LIST
  4105. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4106. wr32(hw, I40E_PFINT_LNKLST0, val);
  4107. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4108. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4109. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4110. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4111. I40E_QINT_RQCTL_INTEVENT_MASK);
  4112. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4113. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4114. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4115. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4116. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4117. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4118. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4119. I40E_QINT_TQCTL_INTEVENT_MASK);
  4120. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4121. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4122. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4123. }
  4124. }
  4125. /**
  4126. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4127. * @vsi: the VSI being configured
  4128. * @v_idx: Index of vector to be freed
  4129. *
  4130. * This function frees the memory allocated to the q_vector. In addition if
  4131. * NAPI is enabled it will delete any references to the NAPI struct prior
  4132. * to freeing the q_vector.
  4133. **/
  4134. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4135. {
  4136. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4137. struct i40e_ring *ring;
  4138. if (!q_vector)
  4139. return;
  4140. /* disassociate q_vector from rings */
  4141. i40e_for_each_ring(ring, q_vector->tx)
  4142. ring->q_vector = NULL;
  4143. i40e_for_each_ring(ring, q_vector->rx)
  4144. ring->q_vector = NULL;
  4145. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4146. if (vsi->netdev)
  4147. netif_napi_del(&q_vector->napi);
  4148. vsi->q_vectors[v_idx] = NULL;
  4149. kfree_rcu(q_vector, rcu);
  4150. }
  4151. /**
  4152. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4153. * @vsi: the VSI being un-configured
  4154. *
  4155. * This frees the memory allocated to the q_vectors and
  4156. * deletes references to the NAPI struct.
  4157. **/
  4158. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4159. {
  4160. int v_idx;
  4161. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4162. i40e_free_q_vector(vsi, v_idx);
  4163. }
  4164. /**
  4165. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4166. * @pf: board private structure
  4167. **/
  4168. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4169. {
  4170. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4171. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4172. pci_disable_msix(pf->pdev);
  4173. kfree(pf->msix_entries);
  4174. pf->msix_entries = NULL;
  4175. kfree(pf->irq_pile);
  4176. pf->irq_pile = NULL;
  4177. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4178. pci_disable_msi(pf->pdev);
  4179. }
  4180. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4181. }
  4182. /**
  4183. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4184. * @pf: board private structure
  4185. *
  4186. * We go through and clear interrupt specific resources and reset the structure
  4187. * to pre-load conditions
  4188. **/
  4189. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4190. {
  4191. int i;
  4192. i40e_free_misc_vector(pf);
  4193. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4194. I40E_IWARP_IRQ_PILE_ID);
  4195. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4196. for (i = 0; i < pf->num_alloc_vsi; i++)
  4197. if (pf->vsi[i])
  4198. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4199. i40e_reset_interrupt_capability(pf);
  4200. }
  4201. /**
  4202. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4203. * @vsi: the VSI being configured
  4204. **/
  4205. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4206. {
  4207. int q_idx;
  4208. if (!vsi->netdev)
  4209. return;
  4210. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4211. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4212. if (q_vector->rx.ring || q_vector->tx.ring)
  4213. napi_enable(&q_vector->napi);
  4214. }
  4215. }
  4216. /**
  4217. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4218. * @vsi: the VSI being configured
  4219. **/
  4220. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4221. {
  4222. int q_idx;
  4223. if (!vsi->netdev)
  4224. return;
  4225. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4226. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4227. if (q_vector->rx.ring || q_vector->tx.ring)
  4228. napi_disable(&q_vector->napi);
  4229. }
  4230. }
  4231. /**
  4232. * i40e_vsi_close - Shut down a VSI
  4233. * @vsi: the vsi to be quelled
  4234. **/
  4235. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4236. {
  4237. struct i40e_pf *pf = vsi->back;
  4238. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4239. i40e_down(vsi);
  4240. i40e_vsi_free_irq(vsi);
  4241. i40e_vsi_free_tx_resources(vsi);
  4242. i40e_vsi_free_rx_resources(vsi);
  4243. vsi->current_netdev_flags = 0;
  4244. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4245. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4246. set_bit(__I40E_CLIENT_RESET, pf->state);
  4247. }
  4248. /**
  4249. * i40e_quiesce_vsi - Pause a given VSI
  4250. * @vsi: the VSI being paused
  4251. **/
  4252. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4253. {
  4254. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4255. return;
  4256. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4257. if (vsi->netdev && netif_running(vsi->netdev))
  4258. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4259. else
  4260. i40e_vsi_close(vsi);
  4261. }
  4262. /**
  4263. * i40e_unquiesce_vsi - Resume a given VSI
  4264. * @vsi: the VSI being resumed
  4265. **/
  4266. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4267. {
  4268. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4269. return;
  4270. if (vsi->netdev && netif_running(vsi->netdev))
  4271. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4272. else
  4273. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4274. }
  4275. /**
  4276. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4277. * @pf: the PF
  4278. **/
  4279. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4280. {
  4281. int v;
  4282. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4283. if (pf->vsi[v])
  4284. i40e_quiesce_vsi(pf->vsi[v]);
  4285. }
  4286. }
  4287. /**
  4288. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4289. * @pf: the PF
  4290. **/
  4291. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4292. {
  4293. int v;
  4294. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4295. if (pf->vsi[v])
  4296. i40e_unquiesce_vsi(pf->vsi[v]);
  4297. }
  4298. }
  4299. /**
  4300. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4301. * @vsi: the VSI being configured
  4302. *
  4303. * Wait until all queues on a given VSI have been disabled.
  4304. **/
  4305. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4306. {
  4307. struct i40e_pf *pf = vsi->back;
  4308. int i, pf_q, ret;
  4309. pf_q = vsi->base_queue;
  4310. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4311. /* Check and wait for the Tx queue */
  4312. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4313. if (ret) {
  4314. dev_info(&pf->pdev->dev,
  4315. "VSI seid %d Tx ring %d disable timeout\n",
  4316. vsi->seid, pf_q);
  4317. return ret;
  4318. }
  4319. if (!i40e_enabled_xdp_vsi(vsi))
  4320. goto wait_rx;
  4321. /* Check and wait for the XDP Tx queue */
  4322. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4323. false);
  4324. if (ret) {
  4325. dev_info(&pf->pdev->dev,
  4326. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4327. vsi->seid, pf_q);
  4328. return ret;
  4329. }
  4330. wait_rx:
  4331. /* Check and wait for the Rx queue */
  4332. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4333. if (ret) {
  4334. dev_info(&pf->pdev->dev,
  4335. "VSI seid %d Rx ring %d disable timeout\n",
  4336. vsi->seid, pf_q);
  4337. return ret;
  4338. }
  4339. }
  4340. return 0;
  4341. }
  4342. #ifdef CONFIG_I40E_DCB
  4343. /**
  4344. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4345. * @pf: the PF
  4346. *
  4347. * This function waits for the queues to be in disabled state for all the
  4348. * VSIs that are managed by this PF.
  4349. **/
  4350. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4351. {
  4352. int v, ret = 0;
  4353. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4354. if (pf->vsi[v]) {
  4355. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4356. if (ret)
  4357. break;
  4358. }
  4359. }
  4360. return ret;
  4361. }
  4362. #endif
  4363. /**
  4364. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4365. * @pf: pointer to PF
  4366. *
  4367. * Get TC map for ISCSI PF type that will include iSCSI TC
  4368. * and LAN TC.
  4369. **/
  4370. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4371. {
  4372. struct i40e_dcb_app_priority_table app;
  4373. struct i40e_hw *hw = &pf->hw;
  4374. u8 enabled_tc = 1; /* TC0 is always enabled */
  4375. u8 tc, i;
  4376. /* Get the iSCSI APP TLV */
  4377. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4378. for (i = 0; i < dcbcfg->numapps; i++) {
  4379. app = dcbcfg->app[i];
  4380. if (app.selector == I40E_APP_SEL_TCPIP &&
  4381. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4382. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4383. enabled_tc |= BIT(tc);
  4384. break;
  4385. }
  4386. }
  4387. return enabled_tc;
  4388. }
  4389. /**
  4390. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4391. * @dcbcfg: the corresponding DCBx configuration structure
  4392. *
  4393. * Return the number of TCs from given DCBx configuration
  4394. **/
  4395. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4396. {
  4397. int i, tc_unused = 0;
  4398. u8 num_tc = 0;
  4399. u8 ret = 0;
  4400. /* Scan the ETS Config Priority Table to find
  4401. * traffic class enabled for a given priority
  4402. * and create a bitmask of enabled TCs
  4403. */
  4404. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4405. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4406. /* Now scan the bitmask to check for
  4407. * contiguous TCs starting with TC0
  4408. */
  4409. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4410. if (num_tc & BIT(i)) {
  4411. if (!tc_unused) {
  4412. ret++;
  4413. } else {
  4414. pr_err("Non-contiguous TC - Disabling DCB\n");
  4415. return 1;
  4416. }
  4417. } else {
  4418. tc_unused = 1;
  4419. }
  4420. }
  4421. /* There is always at least TC0 */
  4422. if (!ret)
  4423. ret = 1;
  4424. return ret;
  4425. }
  4426. /**
  4427. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4428. * @dcbcfg: the corresponding DCBx configuration structure
  4429. *
  4430. * Query the current DCB configuration and return the number of
  4431. * traffic classes enabled from the given DCBX config
  4432. **/
  4433. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4434. {
  4435. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4436. u8 enabled_tc = 1;
  4437. u8 i;
  4438. for (i = 0; i < num_tc; i++)
  4439. enabled_tc |= BIT(i);
  4440. return enabled_tc;
  4441. }
  4442. /**
  4443. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4444. * @pf: PF being queried
  4445. *
  4446. * Query the current MQPRIO configuration and return the number of
  4447. * traffic classes enabled.
  4448. **/
  4449. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4450. {
  4451. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4452. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4453. u8 enabled_tc = 1, i;
  4454. for (i = 1; i < num_tc; i++)
  4455. enabled_tc |= BIT(i);
  4456. return enabled_tc;
  4457. }
  4458. /**
  4459. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4460. * @pf: PF being queried
  4461. *
  4462. * Return number of traffic classes enabled for the given PF
  4463. **/
  4464. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4465. {
  4466. struct i40e_hw *hw = &pf->hw;
  4467. u8 i, enabled_tc = 1;
  4468. u8 num_tc = 0;
  4469. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4470. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4471. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4472. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4473. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4474. return 1;
  4475. /* SFP mode will be enabled for all TCs on port */
  4476. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4477. return i40e_dcb_get_num_tc(dcbcfg);
  4478. /* MFP mode return count of enabled TCs for this PF */
  4479. if (pf->hw.func_caps.iscsi)
  4480. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4481. else
  4482. return 1; /* Only TC0 */
  4483. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4484. if (enabled_tc & BIT(i))
  4485. num_tc++;
  4486. }
  4487. return num_tc;
  4488. }
  4489. /**
  4490. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4491. * @pf: PF being queried
  4492. *
  4493. * Return a bitmap for enabled traffic classes for this PF.
  4494. **/
  4495. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4496. {
  4497. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4498. return i40e_mqprio_get_enabled_tc(pf);
  4499. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4500. * default TC
  4501. */
  4502. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4503. return I40E_DEFAULT_TRAFFIC_CLASS;
  4504. /* SFP mode we want PF to be enabled for all TCs */
  4505. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4506. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4507. /* MFP enabled and iSCSI PF type */
  4508. if (pf->hw.func_caps.iscsi)
  4509. return i40e_get_iscsi_tc_map(pf);
  4510. else
  4511. return I40E_DEFAULT_TRAFFIC_CLASS;
  4512. }
  4513. /**
  4514. * i40e_vsi_get_bw_info - Query VSI BW Information
  4515. * @vsi: the VSI being queried
  4516. *
  4517. * Returns 0 on success, negative value on failure
  4518. **/
  4519. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4520. {
  4521. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4522. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4523. struct i40e_pf *pf = vsi->back;
  4524. struct i40e_hw *hw = &pf->hw;
  4525. i40e_status ret;
  4526. u32 tc_bw_max;
  4527. int i;
  4528. /* Get the VSI level BW configuration */
  4529. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4530. if (ret) {
  4531. dev_info(&pf->pdev->dev,
  4532. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4533. i40e_stat_str(&pf->hw, ret),
  4534. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4535. return -EINVAL;
  4536. }
  4537. /* Get the VSI level BW configuration per TC */
  4538. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4539. NULL);
  4540. if (ret) {
  4541. dev_info(&pf->pdev->dev,
  4542. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4543. i40e_stat_str(&pf->hw, ret),
  4544. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4545. return -EINVAL;
  4546. }
  4547. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4548. dev_info(&pf->pdev->dev,
  4549. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4550. bw_config.tc_valid_bits,
  4551. bw_ets_config.tc_valid_bits);
  4552. /* Still continuing */
  4553. }
  4554. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4555. vsi->bw_max_quanta = bw_config.max_bw;
  4556. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4557. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4558. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4559. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4560. vsi->bw_ets_limit_credits[i] =
  4561. le16_to_cpu(bw_ets_config.credits[i]);
  4562. /* 3 bits out of 4 for each TC */
  4563. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4564. }
  4565. return 0;
  4566. }
  4567. /**
  4568. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4569. * @vsi: the VSI being configured
  4570. * @enabled_tc: TC bitmap
  4571. * @bw_share: BW shared credits per TC
  4572. *
  4573. * Returns 0 on success, negative value on failure
  4574. **/
  4575. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4576. u8 *bw_share)
  4577. {
  4578. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4579. struct i40e_pf *pf = vsi->back;
  4580. i40e_status ret;
  4581. int i;
  4582. /* There is no need to reset BW when mqprio mode is on. */
  4583. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4584. return 0;
  4585. if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4586. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4587. if (ret)
  4588. dev_info(&pf->pdev->dev,
  4589. "Failed to reset tx rate for vsi->seid %u\n",
  4590. vsi->seid);
  4591. return ret;
  4592. }
  4593. bw_data.tc_valid_bits = enabled_tc;
  4594. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4595. bw_data.tc_bw_credits[i] = bw_share[i];
  4596. ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
  4597. if (ret) {
  4598. dev_info(&pf->pdev->dev,
  4599. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4600. pf->hw.aq.asq_last_status);
  4601. return -EINVAL;
  4602. }
  4603. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4604. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4605. return 0;
  4606. }
  4607. /**
  4608. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4609. * @vsi: the VSI being configured
  4610. * @enabled_tc: TC map to be enabled
  4611. *
  4612. **/
  4613. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4614. {
  4615. struct net_device *netdev = vsi->netdev;
  4616. struct i40e_pf *pf = vsi->back;
  4617. struct i40e_hw *hw = &pf->hw;
  4618. u8 netdev_tc = 0;
  4619. int i;
  4620. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4621. if (!netdev)
  4622. return;
  4623. if (!enabled_tc) {
  4624. netdev_reset_tc(netdev);
  4625. return;
  4626. }
  4627. /* Set up actual enabled TCs on the VSI */
  4628. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4629. return;
  4630. /* set per TC queues for the VSI */
  4631. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4632. /* Only set TC queues for enabled tcs
  4633. *
  4634. * e.g. For a VSI that has TC0 and TC3 enabled the
  4635. * enabled_tc bitmap would be 0x00001001; the driver
  4636. * will set the numtc for netdev as 2 that will be
  4637. * referenced by the netdev layer as TC 0 and 1.
  4638. */
  4639. if (vsi->tc_config.enabled_tc & BIT(i))
  4640. netdev_set_tc_queue(netdev,
  4641. vsi->tc_config.tc_info[i].netdev_tc,
  4642. vsi->tc_config.tc_info[i].qcount,
  4643. vsi->tc_config.tc_info[i].qoffset);
  4644. }
  4645. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4646. return;
  4647. /* Assign UP2TC map for the VSI */
  4648. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4649. /* Get the actual TC# for the UP */
  4650. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4651. /* Get the mapped netdev TC# for the UP */
  4652. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4653. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4654. }
  4655. }
  4656. /**
  4657. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4658. * @vsi: the VSI being configured
  4659. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4660. **/
  4661. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4662. struct i40e_vsi_context *ctxt)
  4663. {
  4664. /* copy just the sections touched not the entire info
  4665. * since not all sections are valid as returned by
  4666. * update vsi params
  4667. */
  4668. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4669. memcpy(&vsi->info.queue_mapping,
  4670. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4671. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4672. sizeof(vsi->info.tc_mapping));
  4673. }
  4674. /**
  4675. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4676. * @vsi: VSI to be configured
  4677. * @enabled_tc: TC bitmap
  4678. *
  4679. * This configures a particular VSI for TCs that are mapped to the
  4680. * given TC bitmap. It uses default bandwidth share for TCs across
  4681. * VSIs to configure TC for a particular VSI.
  4682. *
  4683. * NOTE:
  4684. * It is expected that the VSI queues have been quisced before calling
  4685. * this function.
  4686. **/
  4687. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4688. {
  4689. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4690. struct i40e_pf *pf = vsi->back;
  4691. struct i40e_hw *hw = &pf->hw;
  4692. struct i40e_vsi_context ctxt;
  4693. int ret = 0;
  4694. int i;
  4695. /* Check if enabled_tc is same as existing or new TCs */
  4696. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4697. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4698. return ret;
  4699. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4700. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4701. if (enabled_tc & BIT(i))
  4702. bw_share[i] = 1;
  4703. }
  4704. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4705. if (ret) {
  4706. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4707. dev_info(&pf->pdev->dev,
  4708. "Failed configuring TC map %d for VSI %d\n",
  4709. enabled_tc, vsi->seid);
  4710. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4711. &bw_config, NULL);
  4712. if (ret) {
  4713. dev_info(&pf->pdev->dev,
  4714. "Failed querying vsi bw info, err %s aq_err %s\n",
  4715. i40e_stat_str(hw, ret),
  4716. i40e_aq_str(hw, hw->aq.asq_last_status));
  4717. goto out;
  4718. }
  4719. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4720. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4721. if (!valid_tc)
  4722. valid_tc = bw_config.tc_valid_bits;
  4723. /* Always enable TC0, no matter what */
  4724. valid_tc |= 1;
  4725. dev_info(&pf->pdev->dev,
  4726. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4727. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4728. enabled_tc = valid_tc;
  4729. }
  4730. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4731. if (ret) {
  4732. dev_err(&pf->pdev->dev,
  4733. "Unable to configure TC map %d for VSI %d\n",
  4734. enabled_tc, vsi->seid);
  4735. goto out;
  4736. }
  4737. }
  4738. /* Update Queue Pairs Mapping for currently enabled UPs */
  4739. ctxt.seid = vsi->seid;
  4740. ctxt.pf_num = vsi->back->hw.pf_id;
  4741. ctxt.vf_num = 0;
  4742. ctxt.uplink_seid = vsi->uplink_seid;
  4743. ctxt.info = vsi->info;
  4744. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4745. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4746. if (ret)
  4747. goto out;
  4748. } else {
  4749. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4750. }
  4751. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4752. * queues changed.
  4753. */
  4754. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4755. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4756. vsi->num_queue_pairs);
  4757. ret = i40e_vsi_config_rss(vsi);
  4758. if (ret) {
  4759. dev_info(&vsi->back->pdev->dev,
  4760. "Failed to reconfig rss for num_queues\n");
  4761. return ret;
  4762. }
  4763. vsi->reconfig_rss = false;
  4764. }
  4765. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4766. ctxt.info.valid_sections |=
  4767. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4768. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4769. }
  4770. /* Update the VSI after updating the VSI queue-mapping
  4771. * information
  4772. */
  4773. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4774. if (ret) {
  4775. dev_info(&pf->pdev->dev,
  4776. "Update vsi tc config failed, err %s aq_err %s\n",
  4777. i40e_stat_str(hw, ret),
  4778. i40e_aq_str(hw, hw->aq.asq_last_status));
  4779. goto out;
  4780. }
  4781. /* update the local VSI info with updated queue map */
  4782. i40e_vsi_update_queue_map(vsi, &ctxt);
  4783. vsi->info.valid_sections = 0;
  4784. /* Update current VSI BW information */
  4785. ret = i40e_vsi_get_bw_info(vsi);
  4786. if (ret) {
  4787. dev_info(&pf->pdev->dev,
  4788. "Failed updating vsi bw info, err %s aq_err %s\n",
  4789. i40e_stat_str(hw, ret),
  4790. i40e_aq_str(hw, hw->aq.asq_last_status));
  4791. goto out;
  4792. }
  4793. /* Update the netdev TC setup */
  4794. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4795. out:
  4796. return ret;
  4797. }
  4798. /**
  4799. * i40e_get_link_speed - Returns link speed for the interface
  4800. * @vsi: VSI to be configured
  4801. *
  4802. **/
  4803. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4804. {
  4805. struct i40e_pf *pf = vsi->back;
  4806. switch (pf->hw.phy.link_info.link_speed) {
  4807. case I40E_LINK_SPEED_40GB:
  4808. return 40000;
  4809. case I40E_LINK_SPEED_25GB:
  4810. return 25000;
  4811. case I40E_LINK_SPEED_20GB:
  4812. return 20000;
  4813. case I40E_LINK_SPEED_10GB:
  4814. return 10000;
  4815. case I40E_LINK_SPEED_1GB:
  4816. return 1000;
  4817. default:
  4818. return -EINVAL;
  4819. }
  4820. }
  4821. /**
  4822. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4823. * @vsi: VSI to be configured
  4824. * @seid: seid of the channel/VSI
  4825. * @max_tx_rate: max TX rate to be configured as BW limit
  4826. *
  4827. * Helper function to set BW limit for a given VSI
  4828. **/
  4829. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4830. {
  4831. struct i40e_pf *pf = vsi->back;
  4832. u64 credits = 0;
  4833. int speed = 0;
  4834. int ret = 0;
  4835. speed = i40e_get_link_speed(vsi);
  4836. if (max_tx_rate > speed) {
  4837. dev_err(&pf->pdev->dev,
  4838. "Invalid max tx rate %llu specified for VSI seid %d.",
  4839. max_tx_rate, seid);
  4840. return -EINVAL;
  4841. }
  4842. if (max_tx_rate && max_tx_rate < 50) {
  4843. dev_warn(&pf->pdev->dev,
  4844. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4845. max_tx_rate = 50;
  4846. }
  4847. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4848. credits = max_tx_rate;
  4849. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4850. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4851. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4852. if (ret)
  4853. dev_err(&pf->pdev->dev,
  4854. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4855. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4856. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4857. return ret;
  4858. }
  4859. /**
  4860. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4861. * @vsi: VSI to be configured
  4862. *
  4863. * Remove queue channels for the TCs
  4864. **/
  4865. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4866. {
  4867. enum i40e_admin_queue_err last_aq_status;
  4868. struct i40e_cloud_filter *cfilter;
  4869. struct i40e_channel *ch, *ch_tmp;
  4870. struct i40e_pf *pf = vsi->back;
  4871. struct hlist_node *node;
  4872. int ret, i;
  4873. /* Reset rss size that was stored when reconfiguring rss for
  4874. * channel VSIs with non-power-of-2 queue count.
  4875. */
  4876. vsi->current_rss_size = 0;
  4877. /* perform cleanup for channels if they exist */
  4878. if (list_empty(&vsi->ch_list))
  4879. return;
  4880. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4881. struct i40e_vsi *p_vsi;
  4882. list_del(&ch->list);
  4883. p_vsi = ch->parent_vsi;
  4884. if (!p_vsi || !ch->initialized) {
  4885. kfree(ch);
  4886. continue;
  4887. }
  4888. /* Reset queue contexts */
  4889. for (i = 0; i < ch->num_queue_pairs; i++) {
  4890. struct i40e_ring *tx_ring, *rx_ring;
  4891. u16 pf_q;
  4892. pf_q = ch->base_queue + i;
  4893. tx_ring = vsi->tx_rings[pf_q];
  4894. tx_ring->ch = NULL;
  4895. rx_ring = vsi->rx_rings[pf_q];
  4896. rx_ring->ch = NULL;
  4897. }
  4898. /* Reset BW configured for this VSI via mqprio */
  4899. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4900. if (ret)
  4901. dev_info(&vsi->back->pdev->dev,
  4902. "Failed to reset tx rate for ch->seid %u\n",
  4903. ch->seid);
  4904. /* delete cloud filters associated with this channel */
  4905. hlist_for_each_entry_safe(cfilter, node,
  4906. &pf->cloud_filter_list, cloud_node) {
  4907. if (cfilter->seid != ch->seid)
  4908. continue;
  4909. hash_del(&cfilter->cloud_node);
  4910. if (cfilter->dst_port)
  4911. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4912. cfilter,
  4913. false);
  4914. else
  4915. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4916. false);
  4917. last_aq_status = pf->hw.aq.asq_last_status;
  4918. if (ret)
  4919. dev_info(&pf->pdev->dev,
  4920. "Failed to delete cloud filter, err %s aq_err %s\n",
  4921. i40e_stat_str(&pf->hw, ret),
  4922. i40e_aq_str(&pf->hw, last_aq_status));
  4923. kfree(cfilter);
  4924. }
  4925. /* delete VSI from FW */
  4926. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4927. NULL);
  4928. if (ret)
  4929. dev_err(&vsi->back->pdev->dev,
  4930. "unable to remove channel (%d) for parent VSI(%d)\n",
  4931. ch->seid, p_vsi->seid);
  4932. kfree(ch);
  4933. }
  4934. INIT_LIST_HEAD(&vsi->ch_list);
  4935. }
  4936. /**
  4937. * i40e_is_any_channel - channel exist or not
  4938. * @vsi: ptr to VSI to which channels are associated with
  4939. *
  4940. * Returns true or false if channel(s) exist for associated VSI or not
  4941. **/
  4942. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4943. {
  4944. struct i40e_channel *ch, *ch_tmp;
  4945. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4946. if (ch->initialized)
  4947. return true;
  4948. }
  4949. return false;
  4950. }
  4951. /**
  4952. * i40e_get_max_queues_for_channel
  4953. * @vsi: ptr to VSI to which channels are associated with
  4954. *
  4955. * Helper function which returns max value among the queue counts set on the
  4956. * channels/TCs created.
  4957. **/
  4958. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4959. {
  4960. struct i40e_channel *ch, *ch_tmp;
  4961. int max = 0;
  4962. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4963. if (!ch->initialized)
  4964. continue;
  4965. if (ch->num_queue_pairs > max)
  4966. max = ch->num_queue_pairs;
  4967. }
  4968. return max;
  4969. }
  4970. /**
  4971. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4972. * @pf: ptr to PF device
  4973. * @num_queues: number of queues
  4974. * @vsi: the parent VSI
  4975. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4976. *
  4977. * This function validates number of queues in the context of new channel
  4978. * which is being established and determines if RSS should be reconfigured
  4979. * or not for parent VSI.
  4980. **/
  4981. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4982. struct i40e_vsi *vsi, bool *reconfig_rss)
  4983. {
  4984. int max_ch_queues;
  4985. if (!reconfig_rss)
  4986. return -EINVAL;
  4987. *reconfig_rss = false;
  4988. if (vsi->current_rss_size) {
  4989. if (num_queues > vsi->current_rss_size) {
  4990. dev_dbg(&pf->pdev->dev,
  4991. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4992. num_queues, vsi->current_rss_size);
  4993. return -EINVAL;
  4994. } else if ((num_queues < vsi->current_rss_size) &&
  4995. (!is_power_of_2(num_queues))) {
  4996. dev_dbg(&pf->pdev->dev,
  4997. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4998. num_queues, vsi->current_rss_size);
  4999. return -EINVAL;
  5000. }
  5001. }
  5002. if (!is_power_of_2(num_queues)) {
  5003. /* Find the max num_queues configured for channel if channel
  5004. * exist.
  5005. * if channel exist, then enforce 'num_queues' to be more than
  5006. * max ever queues configured for channel.
  5007. */
  5008. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  5009. if (num_queues < max_ch_queues) {
  5010. dev_dbg(&pf->pdev->dev,
  5011. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  5012. num_queues, max_ch_queues);
  5013. return -EINVAL;
  5014. }
  5015. *reconfig_rss = true;
  5016. }
  5017. return 0;
  5018. }
  5019. /**
  5020. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  5021. * @vsi: the VSI being setup
  5022. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  5023. *
  5024. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  5025. **/
  5026. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  5027. {
  5028. struct i40e_pf *pf = vsi->back;
  5029. u8 seed[I40E_HKEY_ARRAY_SIZE];
  5030. struct i40e_hw *hw = &pf->hw;
  5031. int local_rss_size;
  5032. u8 *lut;
  5033. int ret;
  5034. if (!vsi->rss_size)
  5035. return -EINVAL;
  5036. if (rss_size > vsi->rss_size)
  5037. return -EINVAL;
  5038. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  5039. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  5040. if (!lut)
  5041. return -ENOMEM;
  5042. /* Ignoring user configured lut if there is one */
  5043. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  5044. /* Use user configured hash key if there is one, otherwise
  5045. * use default.
  5046. */
  5047. if (vsi->rss_hkey_user)
  5048. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  5049. else
  5050. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  5051. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  5052. if (ret) {
  5053. dev_info(&pf->pdev->dev,
  5054. "Cannot set RSS lut, err %s aq_err %s\n",
  5055. i40e_stat_str(hw, ret),
  5056. i40e_aq_str(hw, hw->aq.asq_last_status));
  5057. kfree(lut);
  5058. return ret;
  5059. }
  5060. kfree(lut);
  5061. /* Do the update w.r.t. storing rss_size */
  5062. if (!vsi->orig_rss_size)
  5063. vsi->orig_rss_size = vsi->rss_size;
  5064. vsi->current_rss_size = local_rss_size;
  5065. return ret;
  5066. }
  5067. /**
  5068. * i40e_channel_setup_queue_map - Setup a channel queue map
  5069. * @pf: ptr to PF device
  5070. * @vsi: the VSI being setup
  5071. * @ctxt: VSI context structure
  5072. * @ch: ptr to channel structure
  5073. *
  5074. * Setup queue map for a specific channel
  5075. **/
  5076. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  5077. struct i40e_vsi_context *ctxt,
  5078. struct i40e_channel *ch)
  5079. {
  5080. u16 qcount, qmap, sections = 0;
  5081. u8 offset = 0;
  5082. int pow;
  5083. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  5084. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  5085. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  5086. ch->num_queue_pairs = qcount;
  5087. /* find the next higher power-of-2 of num queue pairs */
  5088. pow = ilog2(qcount);
  5089. if (!is_power_of_2(qcount))
  5090. pow++;
  5091. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  5092. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  5093. /* Setup queue TC[0].qmap for given VSI context */
  5094. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  5095. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  5096. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  5097. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  5098. ctxt->info.valid_sections |= cpu_to_le16(sections);
  5099. }
  5100. /**
  5101. * i40e_add_channel - add a channel by adding VSI
  5102. * @pf: ptr to PF device
  5103. * @uplink_seid: underlying HW switching element (VEB) ID
  5104. * @ch: ptr to channel structure
  5105. *
  5106. * Add a channel (VSI) using add_vsi and queue_map
  5107. **/
  5108. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5109. struct i40e_channel *ch)
  5110. {
  5111. struct i40e_hw *hw = &pf->hw;
  5112. struct i40e_vsi_context ctxt;
  5113. u8 enabled_tc = 0x1; /* TC0 enabled */
  5114. int ret;
  5115. if (ch->type != I40E_VSI_VMDQ2) {
  5116. dev_info(&pf->pdev->dev,
  5117. "add new vsi failed, ch->type %d\n", ch->type);
  5118. return -EINVAL;
  5119. }
  5120. memset(&ctxt, 0, sizeof(ctxt));
  5121. ctxt.pf_num = hw->pf_id;
  5122. ctxt.vf_num = 0;
  5123. ctxt.uplink_seid = uplink_seid;
  5124. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5125. if (ch->type == I40E_VSI_VMDQ2)
  5126. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5127. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5128. ctxt.info.valid_sections |=
  5129. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5130. ctxt.info.switch_id =
  5131. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5132. }
  5133. /* Set queue map for a given VSI context */
  5134. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5135. /* Now time to create VSI */
  5136. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5137. if (ret) {
  5138. dev_info(&pf->pdev->dev,
  5139. "add new vsi failed, err %s aq_err %s\n",
  5140. i40e_stat_str(&pf->hw, ret),
  5141. i40e_aq_str(&pf->hw,
  5142. pf->hw.aq.asq_last_status));
  5143. return -ENOENT;
  5144. }
  5145. /* Success, update channel, set enabled_tc only if the channel
  5146. * is not a macvlan
  5147. */
  5148. ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
  5149. ch->seid = ctxt.seid;
  5150. ch->vsi_number = ctxt.vsi_number;
  5151. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5152. /* copy just the sections touched not the entire info
  5153. * since not all sections are valid as returned by
  5154. * update vsi params
  5155. */
  5156. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5157. memcpy(&ch->info.queue_mapping,
  5158. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5159. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5160. sizeof(ctxt.info.tc_mapping));
  5161. return 0;
  5162. }
  5163. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5164. u8 *bw_share)
  5165. {
  5166. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5167. i40e_status ret;
  5168. int i;
  5169. bw_data.tc_valid_bits = ch->enabled_tc;
  5170. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5171. bw_data.tc_bw_credits[i] = bw_share[i];
  5172. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5173. &bw_data, NULL);
  5174. if (ret) {
  5175. dev_info(&vsi->back->pdev->dev,
  5176. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5177. vsi->back->hw.aq.asq_last_status, ch->seid);
  5178. return -EINVAL;
  5179. }
  5180. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5181. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5182. return 0;
  5183. }
  5184. /**
  5185. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5186. * @pf: ptr to PF device
  5187. * @vsi: the VSI being setup
  5188. * @ch: ptr to channel structure
  5189. *
  5190. * Configure TX rings associated with channel (VSI) since queues are being
  5191. * from parent VSI.
  5192. **/
  5193. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5194. struct i40e_vsi *vsi,
  5195. struct i40e_channel *ch)
  5196. {
  5197. i40e_status ret;
  5198. int i;
  5199. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5200. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5201. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5202. if (ch->enabled_tc & BIT(i))
  5203. bw_share[i] = 1;
  5204. }
  5205. /* configure BW for new VSI */
  5206. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5207. if (ret) {
  5208. dev_info(&vsi->back->pdev->dev,
  5209. "Failed configuring TC map %d for channel (seid %u)\n",
  5210. ch->enabled_tc, ch->seid);
  5211. return ret;
  5212. }
  5213. for (i = 0; i < ch->num_queue_pairs; i++) {
  5214. struct i40e_ring *tx_ring, *rx_ring;
  5215. u16 pf_q;
  5216. pf_q = ch->base_queue + i;
  5217. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5218. * context
  5219. */
  5220. tx_ring = vsi->tx_rings[pf_q];
  5221. tx_ring->ch = ch;
  5222. /* Get the RX ring ptr */
  5223. rx_ring = vsi->rx_rings[pf_q];
  5224. rx_ring->ch = ch;
  5225. }
  5226. return 0;
  5227. }
  5228. /**
  5229. * i40e_setup_hw_channel - setup new channel
  5230. * @pf: ptr to PF device
  5231. * @vsi: the VSI being setup
  5232. * @ch: ptr to channel structure
  5233. * @uplink_seid: underlying HW switching element (VEB) ID
  5234. * @type: type of channel to be created (VMDq2/VF)
  5235. *
  5236. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5237. * and configures TX rings accordingly
  5238. **/
  5239. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5240. struct i40e_vsi *vsi,
  5241. struct i40e_channel *ch,
  5242. u16 uplink_seid, u8 type)
  5243. {
  5244. int ret;
  5245. ch->initialized = false;
  5246. ch->base_queue = vsi->next_base_queue;
  5247. ch->type = type;
  5248. /* Proceed with creation of channel (VMDq2) VSI */
  5249. ret = i40e_add_channel(pf, uplink_seid, ch);
  5250. if (ret) {
  5251. dev_info(&pf->pdev->dev,
  5252. "failed to add_channel using uplink_seid %u\n",
  5253. uplink_seid);
  5254. return ret;
  5255. }
  5256. /* Mark the successful creation of channel */
  5257. ch->initialized = true;
  5258. /* Reconfigure TX queues using QTX_CTL register */
  5259. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5260. if (ret) {
  5261. dev_info(&pf->pdev->dev,
  5262. "failed to configure TX rings for channel %u\n",
  5263. ch->seid);
  5264. return ret;
  5265. }
  5266. /* update 'next_base_queue' */
  5267. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5268. dev_dbg(&pf->pdev->dev,
  5269. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5270. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5271. ch->num_queue_pairs,
  5272. vsi->next_base_queue);
  5273. return ret;
  5274. }
  5275. /**
  5276. * i40e_setup_channel - setup new channel using uplink element
  5277. * @pf: ptr to PF device
  5278. * @type: type of channel to be created (VMDq2/VF)
  5279. * @uplink_seid: underlying HW switching element (VEB) ID
  5280. * @ch: ptr to channel structure
  5281. *
  5282. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5283. * and uplink switching element (uplink_seid)
  5284. **/
  5285. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5286. struct i40e_channel *ch)
  5287. {
  5288. u8 vsi_type;
  5289. u16 seid;
  5290. int ret;
  5291. if (vsi->type == I40E_VSI_MAIN) {
  5292. vsi_type = I40E_VSI_VMDQ2;
  5293. } else {
  5294. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5295. vsi->type);
  5296. return false;
  5297. }
  5298. /* underlying switching element */
  5299. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5300. /* create channel (VSI), configure TX rings */
  5301. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5302. if (ret) {
  5303. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5304. return false;
  5305. }
  5306. return ch->initialized ? true : false;
  5307. }
  5308. /**
  5309. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5310. * @vsi: ptr to VSI which has PF backing
  5311. *
  5312. * Sets up switch mode correctly if it needs to be changed and perform
  5313. * what are allowed modes.
  5314. **/
  5315. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5316. {
  5317. u8 mode;
  5318. struct i40e_pf *pf = vsi->back;
  5319. struct i40e_hw *hw = &pf->hw;
  5320. int ret;
  5321. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5322. if (ret)
  5323. return -EINVAL;
  5324. if (hw->dev_caps.switch_mode) {
  5325. /* if switch mode is set, support mode2 (non-tunneled for
  5326. * cloud filter) for now
  5327. */
  5328. u32 switch_mode = hw->dev_caps.switch_mode &
  5329. I40E_SWITCH_MODE_MASK;
  5330. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5331. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5332. return 0;
  5333. dev_err(&pf->pdev->dev,
  5334. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5335. hw->dev_caps.switch_mode);
  5336. return -EINVAL;
  5337. }
  5338. }
  5339. /* Set Bit 7 to be valid */
  5340. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5341. /* Set L4type for TCP support */
  5342. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5343. /* Set cloud filter mode */
  5344. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5345. /* Prep mode field for set_switch_config */
  5346. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5347. pf->last_sw_conf_valid_flags,
  5348. mode, NULL);
  5349. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5350. dev_err(&pf->pdev->dev,
  5351. "couldn't set switch config bits, err %s aq_err %s\n",
  5352. i40e_stat_str(hw, ret),
  5353. i40e_aq_str(hw,
  5354. hw->aq.asq_last_status));
  5355. return ret;
  5356. }
  5357. /**
  5358. * i40e_create_queue_channel - function to create channel
  5359. * @vsi: VSI to be configured
  5360. * @ch: ptr to channel (it contains channel specific params)
  5361. *
  5362. * This function creates channel (VSI) using num_queues specified by user,
  5363. * reconfigs RSS if needed.
  5364. **/
  5365. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5366. struct i40e_channel *ch)
  5367. {
  5368. struct i40e_pf *pf = vsi->back;
  5369. bool reconfig_rss;
  5370. int err;
  5371. if (!ch)
  5372. return -EINVAL;
  5373. if (!ch->num_queue_pairs) {
  5374. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5375. ch->num_queue_pairs);
  5376. return -EINVAL;
  5377. }
  5378. /* validate user requested num_queues for channel */
  5379. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5380. &reconfig_rss);
  5381. if (err) {
  5382. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5383. ch->num_queue_pairs);
  5384. return -EINVAL;
  5385. }
  5386. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5387. * VSI to be added switch to VEB mode.
  5388. */
  5389. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5390. (!i40e_is_any_channel(vsi))) {
  5391. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5392. dev_dbg(&pf->pdev->dev,
  5393. "Failed to create channel. Override queues (%u) not power of 2\n",
  5394. vsi->tc_config.tc_info[0].qcount);
  5395. return -EINVAL;
  5396. }
  5397. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5398. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5399. if (vsi->type == I40E_VSI_MAIN) {
  5400. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5401. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5402. true);
  5403. else
  5404. i40e_do_reset_safe(pf,
  5405. I40E_PF_RESET_FLAG);
  5406. }
  5407. }
  5408. /* now onwards for main VSI, number of queues will be value
  5409. * of TC0's queue count
  5410. */
  5411. }
  5412. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5413. * it should be more than num_queues
  5414. */
  5415. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5416. dev_dbg(&pf->pdev->dev,
  5417. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5418. vsi->cnt_q_avail, ch->num_queue_pairs);
  5419. return -EINVAL;
  5420. }
  5421. /* reconfig_rss only if vsi type is MAIN_VSI */
  5422. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5423. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5424. if (err) {
  5425. dev_info(&pf->pdev->dev,
  5426. "Error: unable to reconfig rss for num_queues (%u)\n",
  5427. ch->num_queue_pairs);
  5428. return -EINVAL;
  5429. }
  5430. }
  5431. if (!i40e_setup_channel(pf, vsi, ch)) {
  5432. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5433. return -EINVAL;
  5434. }
  5435. dev_info(&pf->pdev->dev,
  5436. "Setup channel (id:%u) utilizing num_queues %d\n",
  5437. ch->seid, ch->num_queue_pairs);
  5438. /* configure VSI for BW limit */
  5439. if (ch->max_tx_rate) {
  5440. u64 credits = ch->max_tx_rate;
  5441. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5442. return -EINVAL;
  5443. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5444. dev_dbg(&pf->pdev->dev,
  5445. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5446. ch->max_tx_rate,
  5447. credits,
  5448. ch->seid);
  5449. }
  5450. /* in case of VF, this will be main SRIOV VSI */
  5451. ch->parent_vsi = vsi;
  5452. /* and update main_vsi's count for queue_available to use */
  5453. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5454. return 0;
  5455. }
  5456. /**
  5457. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5458. * @vsi: VSI to be configured
  5459. *
  5460. * Configures queue channel mapping to the given TCs
  5461. **/
  5462. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5463. {
  5464. struct i40e_channel *ch;
  5465. u64 max_rate = 0;
  5466. int ret = 0, i;
  5467. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5468. vsi->tc_seid_map[0] = vsi->seid;
  5469. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5470. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5471. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5472. if (!ch) {
  5473. ret = -ENOMEM;
  5474. goto err_free;
  5475. }
  5476. INIT_LIST_HEAD(&ch->list);
  5477. ch->num_queue_pairs =
  5478. vsi->tc_config.tc_info[i].qcount;
  5479. ch->base_queue =
  5480. vsi->tc_config.tc_info[i].qoffset;
  5481. /* Bandwidth limit through tc interface is in bytes/s,
  5482. * change to Mbit/s
  5483. */
  5484. max_rate = vsi->mqprio_qopt.max_rate[i];
  5485. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5486. ch->max_tx_rate = max_rate;
  5487. list_add_tail(&ch->list, &vsi->ch_list);
  5488. ret = i40e_create_queue_channel(vsi, ch);
  5489. if (ret) {
  5490. dev_err(&vsi->back->pdev->dev,
  5491. "Failed creating queue channel with TC%d: queues %d\n",
  5492. i, ch->num_queue_pairs);
  5493. goto err_free;
  5494. }
  5495. vsi->tc_seid_map[i] = ch->seid;
  5496. }
  5497. }
  5498. return ret;
  5499. err_free:
  5500. i40e_remove_queue_channels(vsi);
  5501. return ret;
  5502. }
  5503. /**
  5504. * i40e_veb_config_tc - Configure TCs for given VEB
  5505. * @veb: given VEB
  5506. * @enabled_tc: TC bitmap
  5507. *
  5508. * Configures given TC bitmap for VEB (switching) element
  5509. **/
  5510. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5511. {
  5512. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5513. struct i40e_pf *pf = veb->pf;
  5514. int ret = 0;
  5515. int i;
  5516. /* No TCs or already enabled TCs just return */
  5517. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5518. return ret;
  5519. bw_data.tc_valid_bits = enabled_tc;
  5520. /* bw_data.absolute_credits is not set (relative) */
  5521. /* Enable ETS TCs with equal BW Share for now */
  5522. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5523. if (enabled_tc & BIT(i))
  5524. bw_data.tc_bw_share_credits[i] = 1;
  5525. }
  5526. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5527. &bw_data, NULL);
  5528. if (ret) {
  5529. dev_info(&pf->pdev->dev,
  5530. "VEB bw config failed, err %s aq_err %s\n",
  5531. i40e_stat_str(&pf->hw, ret),
  5532. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5533. goto out;
  5534. }
  5535. /* Update the BW information */
  5536. ret = i40e_veb_get_bw_info(veb);
  5537. if (ret) {
  5538. dev_info(&pf->pdev->dev,
  5539. "Failed getting veb bw config, err %s aq_err %s\n",
  5540. i40e_stat_str(&pf->hw, ret),
  5541. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5542. }
  5543. out:
  5544. return ret;
  5545. }
  5546. #ifdef CONFIG_I40E_DCB
  5547. /**
  5548. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5549. * @pf: PF struct
  5550. *
  5551. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5552. * the caller would've quiesce all the VSIs before calling
  5553. * this function
  5554. **/
  5555. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5556. {
  5557. u8 tc_map = 0;
  5558. int ret;
  5559. u8 v;
  5560. /* Enable the TCs available on PF to all VEBs */
  5561. tc_map = i40e_pf_get_tc_map(pf);
  5562. for (v = 0; v < I40E_MAX_VEB; v++) {
  5563. if (!pf->veb[v])
  5564. continue;
  5565. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5566. if (ret) {
  5567. dev_info(&pf->pdev->dev,
  5568. "Failed configuring TC for VEB seid=%d\n",
  5569. pf->veb[v]->seid);
  5570. /* Will try to configure as many components */
  5571. }
  5572. }
  5573. /* Update each VSI */
  5574. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5575. if (!pf->vsi[v])
  5576. continue;
  5577. /* - Enable all TCs for the LAN VSI
  5578. * - For all others keep them at TC0 for now
  5579. */
  5580. if (v == pf->lan_vsi)
  5581. tc_map = i40e_pf_get_tc_map(pf);
  5582. else
  5583. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5584. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5585. if (ret) {
  5586. dev_info(&pf->pdev->dev,
  5587. "Failed configuring TC for VSI seid=%d\n",
  5588. pf->vsi[v]->seid);
  5589. /* Will try to configure as many components */
  5590. } else {
  5591. /* Re-configure VSI vectors based on updated TC map */
  5592. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5593. if (pf->vsi[v]->netdev)
  5594. i40e_dcbnl_set_all(pf->vsi[v]);
  5595. }
  5596. }
  5597. }
  5598. /**
  5599. * i40e_resume_port_tx - Resume port Tx
  5600. * @pf: PF struct
  5601. *
  5602. * Resume a port's Tx and issue a PF reset in case of failure to
  5603. * resume.
  5604. **/
  5605. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5606. {
  5607. struct i40e_hw *hw = &pf->hw;
  5608. int ret;
  5609. ret = i40e_aq_resume_port_tx(hw, NULL);
  5610. if (ret) {
  5611. dev_info(&pf->pdev->dev,
  5612. "Resume Port Tx failed, err %s aq_err %s\n",
  5613. i40e_stat_str(&pf->hw, ret),
  5614. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5615. /* Schedule PF reset to recover */
  5616. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5617. i40e_service_event_schedule(pf);
  5618. }
  5619. return ret;
  5620. }
  5621. /**
  5622. * i40e_init_pf_dcb - Initialize DCB configuration
  5623. * @pf: PF being configured
  5624. *
  5625. * Query the current DCB configuration and cache it
  5626. * in the hardware structure
  5627. **/
  5628. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5629. {
  5630. struct i40e_hw *hw = &pf->hw;
  5631. int err = 0;
  5632. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5633. * Also do not enable DCBx if FW LLDP agent is disabled
  5634. */
  5635. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5636. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
  5637. dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
  5638. err = I40E_NOT_SUPPORTED;
  5639. goto out;
  5640. }
  5641. err = i40e_init_dcb(hw, true);
  5642. if (!err) {
  5643. /* Device/Function is not DCBX capable */
  5644. if ((!hw->func_caps.dcb) ||
  5645. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5646. dev_info(&pf->pdev->dev,
  5647. "DCBX offload is not supported or is disabled for this PF.\n");
  5648. } else {
  5649. /* When status is not DISABLED then DCBX in FW */
  5650. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5651. DCB_CAP_DCBX_VER_IEEE;
  5652. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5653. /* Enable DCB tagging only when more than one TC
  5654. * or explicitly disable if only one TC
  5655. */
  5656. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5657. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5658. else
  5659. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5660. dev_dbg(&pf->pdev->dev,
  5661. "DCBX offload is supported for this PF.\n");
  5662. }
  5663. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5664. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5665. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5666. } else {
  5667. dev_info(&pf->pdev->dev,
  5668. "Query for DCB configuration failed, err %s aq_err %s\n",
  5669. i40e_stat_str(&pf->hw, err),
  5670. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5671. }
  5672. out:
  5673. return err;
  5674. }
  5675. #endif /* CONFIG_I40E_DCB */
  5676. #define SPEED_SIZE 14
  5677. #define FC_SIZE 8
  5678. /**
  5679. * i40e_print_link_message - print link up or down
  5680. * @vsi: the VSI for which link needs a message
  5681. * @isup: true of link is up, false otherwise
  5682. */
  5683. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5684. {
  5685. enum i40e_aq_link_speed new_speed;
  5686. struct i40e_pf *pf = vsi->back;
  5687. char *speed = "Unknown";
  5688. char *fc = "Unknown";
  5689. char *fec = "";
  5690. char *req_fec = "";
  5691. char *an = "";
  5692. if (isup)
  5693. new_speed = pf->hw.phy.link_info.link_speed;
  5694. else
  5695. new_speed = I40E_LINK_SPEED_UNKNOWN;
  5696. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5697. return;
  5698. vsi->current_isup = isup;
  5699. vsi->current_speed = new_speed;
  5700. if (!isup) {
  5701. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5702. return;
  5703. }
  5704. /* Warn user if link speed on NPAR enabled partition is not at
  5705. * least 10GB
  5706. */
  5707. if (pf->hw.func_caps.npar_enable &&
  5708. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5709. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5710. netdev_warn(vsi->netdev,
  5711. "The partition detected link speed that is less than 10Gbps\n");
  5712. switch (pf->hw.phy.link_info.link_speed) {
  5713. case I40E_LINK_SPEED_40GB:
  5714. speed = "40 G";
  5715. break;
  5716. case I40E_LINK_SPEED_20GB:
  5717. speed = "20 G";
  5718. break;
  5719. case I40E_LINK_SPEED_25GB:
  5720. speed = "25 G";
  5721. break;
  5722. case I40E_LINK_SPEED_10GB:
  5723. speed = "10 G";
  5724. break;
  5725. case I40E_LINK_SPEED_5GB:
  5726. speed = "5 G";
  5727. break;
  5728. case I40E_LINK_SPEED_2_5GB:
  5729. speed = "2.5 G";
  5730. break;
  5731. case I40E_LINK_SPEED_1GB:
  5732. speed = "1000 M";
  5733. break;
  5734. case I40E_LINK_SPEED_100MB:
  5735. speed = "100 M";
  5736. break;
  5737. default:
  5738. break;
  5739. }
  5740. switch (pf->hw.fc.current_mode) {
  5741. case I40E_FC_FULL:
  5742. fc = "RX/TX";
  5743. break;
  5744. case I40E_FC_TX_PAUSE:
  5745. fc = "TX";
  5746. break;
  5747. case I40E_FC_RX_PAUSE:
  5748. fc = "RX";
  5749. break;
  5750. default:
  5751. fc = "None";
  5752. break;
  5753. }
  5754. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5755. req_fec = "None";
  5756. fec = "None";
  5757. an = "False";
  5758. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5759. an = "True";
  5760. if (pf->hw.phy.link_info.fec_info &
  5761. I40E_AQ_CONFIG_FEC_KR_ENA)
  5762. fec = "CL74 FC-FEC/BASE-R";
  5763. else if (pf->hw.phy.link_info.fec_info &
  5764. I40E_AQ_CONFIG_FEC_RS_ENA)
  5765. fec = "CL108 RS-FEC";
  5766. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5767. * both RS and FC are requested
  5768. */
  5769. if (vsi->back->hw.phy.link_info.req_fec_info &
  5770. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5771. if (vsi->back->hw.phy.link_info.req_fec_info &
  5772. I40E_AQ_REQUEST_FEC_RS)
  5773. req_fec = "CL108 RS-FEC";
  5774. else
  5775. req_fec = "CL74 FC-FEC/BASE-R";
  5776. }
  5777. netdev_info(vsi->netdev,
  5778. "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
  5779. speed, req_fec, fec, an, fc);
  5780. } else {
  5781. netdev_info(vsi->netdev,
  5782. "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
  5783. speed, fc);
  5784. }
  5785. }
  5786. /**
  5787. * i40e_up_complete - Finish the last steps of bringing up a connection
  5788. * @vsi: the VSI being configured
  5789. **/
  5790. static int i40e_up_complete(struct i40e_vsi *vsi)
  5791. {
  5792. struct i40e_pf *pf = vsi->back;
  5793. int err;
  5794. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5795. i40e_vsi_configure_msix(vsi);
  5796. else
  5797. i40e_configure_msi_and_legacy(vsi);
  5798. /* start rings */
  5799. err = i40e_vsi_start_rings(vsi);
  5800. if (err)
  5801. return err;
  5802. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5803. i40e_napi_enable_all(vsi);
  5804. i40e_vsi_enable_irq(vsi);
  5805. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5806. (vsi->netdev)) {
  5807. i40e_print_link_message(vsi, true);
  5808. netif_tx_start_all_queues(vsi->netdev);
  5809. netif_carrier_on(vsi->netdev);
  5810. }
  5811. /* replay FDIR SB filters */
  5812. if (vsi->type == I40E_VSI_FDIR) {
  5813. /* reset fd counters */
  5814. pf->fd_add_err = 0;
  5815. pf->fd_atr_cnt = 0;
  5816. i40e_fdir_filter_restore(vsi);
  5817. }
  5818. /* On the next run of the service_task, notify any clients of the new
  5819. * opened netdev
  5820. */
  5821. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5822. i40e_service_event_schedule(pf);
  5823. return 0;
  5824. }
  5825. /**
  5826. * i40e_vsi_reinit_locked - Reset the VSI
  5827. * @vsi: the VSI being configured
  5828. *
  5829. * Rebuild the ring structs after some configuration
  5830. * has changed, e.g. MTU size.
  5831. **/
  5832. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5833. {
  5834. struct i40e_pf *pf = vsi->back;
  5835. WARN_ON(in_interrupt());
  5836. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5837. usleep_range(1000, 2000);
  5838. i40e_down(vsi);
  5839. i40e_up(vsi);
  5840. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5841. }
  5842. /**
  5843. * i40e_up - Bring the connection back up after being down
  5844. * @vsi: the VSI being configured
  5845. **/
  5846. int i40e_up(struct i40e_vsi *vsi)
  5847. {
  5848. int err;
  5849. err = i40e_vsi_configure(vsi);
  5850. if (!err)
  5851. err = i40e_up_complete(vsi);
  5852. return err;
  5853. }
  5854. /**
  5855. * i40e_force_link_state - Force the link status
  5856. * @pf: board private structure
  5857. * @is_up: whether the link state should be forced up or down
  5858. **/
  5859. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5860. {
  5861. struct i40e_aq_get_phy_abilities_resp abilities;
  5862. struct i40e_aq_set_phy_config config = {0};
  5863. struct i40e_hw *hw = &pf->hw;
  5864. i40e_status err;
  5865. u64 mask;
  5866. u8 speed;
  5867. /* Card might've been put in an unstable state by other drivers
  5868. * and applications, which causes incorrect speed values being
  5869. * set on startup. In order to clear speed registers, we call
  5870. * get_phy_capabilities twice, once to get initial state of
  5871. * available speeds, and once to get current PHY config.
  5872. */
  5873. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
  5874. NULL);
  5875. if (err) {
  5876. dev_err(&pf->pdev->dev,
  5877. "failed to get phy cap., ret = %s last_status = %s\n",
  5878. i40e_stat_str(hw, err),
  5879. i40e_aq_str(hw, hw->aq.asq_last_status));
  5880. return err;
  5881. }
  5882. speed = abilities.link_speed;
  5883. /* Get the current phy config */
  5884. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5885. NULL);
  5886. if (err) {
  5887. dev_err(&pf->pdev->dev,
  5888. "failed to get phy cap., ret = %s last_status = %s\n",
  5889. i40e_stat_str(hw, err),
  5890. i40e_aq_str(hw, hw->aq.asq_last_status));
  5891. return err;
  5892. }
  5893. /* If link needs to go up, but was not forced to go down,
  5894. * and its speed values are OK, no need for a flap
  5895. */
  5896. if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
  5897. return I40E_SUCCESS;
  5898. /* To force link we need to set bits for all supported PHY types,
  5899. * but there are now more than 32, so we need to split the bitmap
  5900. * across two fields.
  5901. */
  5902. mask = I40E_PHY_TYPES_BITMASK;
  5903. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5904. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5905. /* Copy the old settings, except of phy_type */
  5906. config.abilities = abilities.abilities;
  5907. if (abilities.link_speed != 0)
  5908. config.link_speed = abilities.link_speed;
  5909. else
  5910. config.link_speed = speed;
  5911. config.eee_capability = abilities.eee_capability;
  5912. config.eeer = abilities.eeer_val;
  5913. config.low_power_ctrl = abilities.d3_lpan;
  5914. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  5915. I40E_AQ_PHY_FEC_CONFIG_MASK;
  5916. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5917. if (err) {
  5918. dev_err(&pf->pdev->dev,
  5919. "set phy config ret = %s last_status = %s\n",
  5920. i40e_stat_str(&pf->hw, err),
  5921. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5922. return err;
  5923. }
  5924. /* Update the link info */
  5925. err = i40e_update_link_info(hw);
  5926. if (err) {
  5927. /* Wait a little bit (on 40G cards it sometimes takes a really
  5928. * long time for link to come back from the atomic reset)
  5929. * and try once more
  5930. */
  5931. msleep(1000);
  5932. i40e_update_link_info(hw);
  5933. }
  5934. i40e_aq_set_link_restart_an(hw, true, NULL);
  5935. return I40E_SUCCESS;
  5936. }
  5937. /**
  5938. * i40e_down - Shutdown the connection processing
  5939. * @vsi: the VSI being stopped
  5940. **/
  5941. void i40e_down(struct i40e_vsi *vsi)
  5942. {
  5943. int i;
  5944. /* It is assumed that the caller of this function
  5945. * sets the vsi->state __I40E_VSI_DOWN bit.
  5946. */
  5947. if (vsi->netdev) {
  5948. netif_carrier_off(vsi->netdev);
  5949. netif_tx_disable(vsi->netdev);
  5950. }
  5951. i40e_vsi_disable_irq(vsi);
  5952. i40e_vsi_stop_rings(vsi);
  5953. if (vsi->type == I40E_VSI_MAIN &&
  5954. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5955. i40e_force_link_state(vsi->back, false);
  5956. i40e_napi_disable_all(vsi);
  5957. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5958. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5959. if (i40e_enabled_xdp_vsi(vsi)) {
  5960. /* Make sure that in-progress ndo_xdp_xmit and
  5961. * ndo_xsk_wakeup calls are completed.
  5962. */
  5963. synchronize_rcu();
  5964. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5965. }
  5966. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5967. }
  5968. }
  5969. /**
  5970. * i40e_validate_mqprio_qopt- validate queue mapping info
  5971. * @vsi: the VSI being configured
  5972. * @mqprio_qopt: queue parametrs
  5973. **/
  5974. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5975. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5976. {
  5977. u64 sum_max_rate = 0;
  5978. u64 max_rate = 0;
  5979. int i;
  5980. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5981. mqprio_qopt->qopt.num_tc < 1 ||
  5982. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5983. return -EINVAL;
  5984. for (i = 0; ; i++) {
  5985. if (!mqprio_qopt->qopt.count[i])
  5986. return -EINVAL;
  5987. if (mqprio_qopt->min_rate[i]) {
  5988. dev_err(&vsi->back->pdev->dev,
  5989. "Invalid min tx rate (greater than 0) specified\n");
  5990. return -EINVAL;
  5991. }
  5992. max_rate = mqprio_qopt->max_rate[i];
  5993. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5994. sum_max_rate += max_rate;
  5995. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5996. break;
  5997. if (mqprio_qopt->qopt.offset[i + 1] !=
  5998. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5999. return -EINVAL;
  6000. }
  6001. if (vsi->num_queue_pairs <
  6002. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  6003. return -EINVAL;
  6004. }
  6005. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  6006. dev_err(&vsi->back->pdev->dev,
  6007. "Invalid max tx rate specified\n");
  6008. return -EINVAL;
  6009. }
  6010. return 0;
  6011. }
  6012. /**
  6013. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  6014. * @vsi: the VSI being configured
  6015. **/
  6016. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  6017. {
  6018. u16 qcount;
  6019. int i;
  6020. /* Only TC0 is enabled */
  6021. vsi->tc_config.numtc = 1;
  6022. vsi->tc_config.enabled_tc = 1;
  6023. qcount = min_t(int, vsi->alloc_queue_pairs,
  6024. i40e_pf_get_max_q_per_tc(vsi->back));
  6025. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  6026. /* For the TC that is not enabled set the offset to to default
  6027. * queue and allocate one queue for the given TC.
  6028. */
  6029. vsi->tc_config.tc_info[i].qoffset = 0;
  6030. if (i == 0)
  6031. vsi->tc_config.tc_info[i].qcount = qcount;
  6032. else
  6033. vsi->tc_config.tc_info[i].qcount = 1;
  6034. vsi->tc_config.tc_info[i].netdev_tc = 0;
  6035. }
  6036. }
  6037. /**
  6038. * i40e_del_macvlan_filter
  6039. * @hw: pointer to the HW structure
  6040. * @seid: seid of the channel VSI
  6041. * @macaddr: the mac address to apply as a filter
  6042. * @aq_err: store the admin Q error
  6043. *
  6044. * This function deletes a mac filter on the channel VSI which serves as the
  6045. * macvlan. Returns 0 on success.
  6046. **/
  6047. static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
  6048. const u8 *macaddr, int *aq_err)
  6049. {
  6050. struct i40e_aqc_remove_macvlan_element_data element;
  6051. i40e_status status;
  6052. memset(&element, 0, sizeof(element));
  6053. ether_addr_copy(element.mac_addr, macaddr);
  6054. element.vlan_tag = 0;
  6055. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  6056. status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
  6057. *aq_err = hw->aq.asq_last_status;
  6058. return status;
  6059. }
  6060. /**
  6061. * i40e_add_macvlan_filter
  6062. * @hw: pointer to the HW structure
  6063. * @seid: seid of the channel VSI
  6064. * @macaddr: the mac address to apply as a filter
  6065. * @aq_err: store the admin Q error
  6066. *
  6067. * This function adds a mac filter on the channel VSI which serves as the
  6068. * macvlan. Returns 0 on success.
  6069. **/
  6070. static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
  6071. const u8 *macaddr, int *aq_err)
  6072. {
  6073. struct i40e_aqc_add_macvlan_element_data element;
  6074. i40e_status status;
  6075. u16 cmd_flags = 0;
  6076. ether_addr_copy(element.mac_addr, macaddr);
  6077. element.vlan_tag = 0;
  6078. element.queue_number = 0;
  6079. element.match_method = I40E_AQC_MM_ERR_NO_RES;
  6080. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  6081. element.flags = cpu_to_le16(cmd_flags);
  6082. status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
  6083. *aq_err = hw->aq.asq_last_status;
  6084. return status;
  6085. }
  6086. /**
  6087. * i40e_reset_ch_rings - Reset the queue contexts in a channel
  6088. * @vsi: the VSI we want to access
  6089. * @ch: the channel we want to access
  6090. */
  6091. static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
  6092. {
  6093. struct i40e_ring *tx_ring, *rx_ring;
  6094. u16 pf_q;
  6095. int i;
  6096. for (i = 0; i < ch->num_queue_pairs; i++) {
  6097. pf_q = ch->base_queue + i;
  6098. tx_ring = vsi->tx_rings[pf_q];
  6099. tx_ring->ch = NULL;
  6100. rx_ring = vsi->rx_rings[pf_q];
  6101. rx_ring->ch = NULL;
  6102. }
  6103. }
  6104. /**
  6105. * i40e_free_macvlan_channels
  6106. * @vsi: the VSI we want to access
  6107. *
  6108. * This function frees the Qs of the channel VSI from
  6109. * the stack and also deletes the channel VSIs which
  6110. * serve as macvlans.
  6111. */
  6112. static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
  6113. {
  6114. struct i40e_channel *ch, *ch_tmp;
  6115. int ret;
  6116. if (list_empty(&vsi->macvlan_list))
  6117. return;
  6118. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  6119. struct i40e_vsi *parent_vsi;
  6120. if (i40e_is_channel_macvlan(ch)) {
  6121. i40e_reset_ch_rings(vsi, ch);
  6122. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  6123. netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
  6124. netdev_set_sb_channel(ch->fwd->netdev, 0);
  6125. kfree(ch->fwd);
  6126. ch->fwd = NULL;
  6127. }
  6128. list_del(&ch->list);
  6129. parent_vsi = ch->parent_vsi;
  6130. if (!parent_vsi || !ch->initialized) {
  6131. kfree(ch);
  6132. continue;
  6133. }
  6134. /* remove the VSI */
  6135. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  6136. NULL);
  6137. if (ret)
  6138. dev_err(&vsi->back->pdev->dev,
  6139. "unable to remove channel (%d) for parent VSI(%d)\n",
  6140. ch->seid, parent_vsi->seid);
  6141. kfree(ch);
  6142. }
  6143. vsi->macvlan_cnt = 0;
  6144. }
  6145. /**
  6146. * i40e_fwd_ring_up - bring the macvlan device up
  6147. * @vsi: the VSI we want to access
  6148. * @vdev: macvlan netdevice
  6149. * @fwd: the private fwd structure
  6150. */
  6151. static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
  6152. struct i40e_fwd_adapter *fwd)
  6153. {
  6154. int ret = 0, num_tc = 1, i, aq_err;
  6155. struct i40e_channel *ch, *ch_tmp;
  6156. struct i40e_pf *pf = vsi->back;
  6157. struct i40e_hw *hw = &pf->hw;
  6158. if (list_empty(&vsi->macvlan_list))
  6159. return -EINVAL;
  6160. /* Go through the list and find an available channel */
  6161. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  6162. if (!i40e_is_channel_macvlan(ch)) {
  6163. ch->fwd = fwd;
  6164. /* record configuration for macvlan interface in vdev */
  6165. for (i = 0; i < num_tc; i++)
  6166. netdev_bind_sb_channel_queue(vsi->netdev, vdev,
  6167. i,
  6168. ch->num_queue_pairs,
  6169. ch->base_queue);
  6170. for (i = 0; i < ch->num_queue_pairs; i++) {
  6171. struct i40e_ring *tx_ring, *rx_ring;
  6172. u16 pf_q;
  6173. pf_q = ch->base_queue + i;
  6174. /* Get to TX ring ptr */
  6175. tx_ring = vsi->tx_rings[pf_q];
  6176. tx_ring->ch = ch;
  6177. /* Get the RX ring ptr */
  6178. rx_ring = vsi->rx_rings[pf_q];
  6179. rx_ring->ch = ch;
  6180. }
  6181. break;
  6182. }
  6183. }
  6184. /* Guarantee all rings are updated before we update the
  6185. * MAC address filter.
  6186. */
  6187. wmb();
  6188. /* Add a mac filter */
  6189. ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
  6190. if (ret) {
  6191. /* if we cannot add the MAC rule then disable the offload */
  6192. macvlan_release_l2fw_offload(vdev);
  6193. for (i = 0; i < ch->num_queue_pairs; i++) {
  6194. struct i40e_ring *rx_ring;
  6195. u16 pf_q;
  6196. pf_q = ch->base_queue + i;
  6197. rx_ring = vsi->rx_rings[pf_q];
  6198. rx_ring->netdev = NULL;
  6199. }
  6200. dev_info(&pf->pdev->dev,
  6201. "Error adding mac filter on macvlan err %s, aq_err %s\n",
  6202. i40e_stat_str(hw, ret),
  6203. i40e_aq_str(hw, aq_err));
  6204. netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
  6205. }
  6206. return ret;
  6207. }
  6208. /**
  6209. * i40e_setup_macvlans - create the channels which will be macvlans
  6210. * @vsi: the VSI we want to access
  6211. * @macvlan_cnt: no. of macvlans to be setup
  6212. * @qcnt: no. of Qs per macvlan
  6213. * @vdev: macvlan netdevice
  6214. */
  6215. static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
  6216. struct net_device *vdev)
  6217. {
  6218. struct i40e_pf *pf = vsi->back;
  6219. struct i40e_hw *hw = &pf->hw;
  6220. struct i40e_vsi_context ctxt;
  6221. u16 sections, qmap, num_qps;
  6222. struct i40e_channel *ch;
  6223. int i, pow, ret = 0;
  6224. u8 offset = 0;
  6225. if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
  6226. return -EINVAL;
  6227. num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
  6228. /* find the next higher power-of-2 of num queue pairs */
  6229. pow = fls(roundup_pow_of_two(num_qps) - 1);
  6230. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  6231. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  6232. /* Setup context bits for the main VSI */
  6233. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  6234. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  6235. memset(&ctxt, 0, sizeof(ctxt));
  6236. ctxt.seid = vsi->seid;
  6237. ctxt.pf_num = vsi->back->hw.pf_id;
  6238. ctxt.vf_num = 0;
  6239. ctxt.uplink_seid = vsi->uplink_seid;
  6240. ctxt.info = vsi->info;
  6241. ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
  6242. ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  6243. ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  6244. ctxt.info.valid_sections |= cpu_to_le16(sections);
  6245. /* Reconfigure RSS for main VSI with new max queue count */
  6246. vsi->rss_size = max_t(u16, num_qps, qcnt);
  6247. ret = i40e_vsi_config_rss(vsi);
  6248. if (ret) {
  6249. dev_info(&pf->pdev->dev,
  6250. "Failed to reconfig RSS for num_queues (%u)\n",
  6251. vsi->rss_size);
  6252. return ret;
  6253. }
  6254. vsi->reconfig_rss = true;
  6255. dev_dbg(&vsi->back->pdev->dev,
  6256. "Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
  6257. vsi->next_base_queue = num_qps;
  6258. vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
  6259. /* Update the VSI after updating the VSI queue-mapping
  6260. * information
  6261. */
  6262. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  6263. if (ret) {
  6264. dev_info(&pf->pdev->dev,
  6265. "Update vsi tc config failed, err %s aq_err %s\n",
  6266. i40e_stat_str(hw, ret),
  6267. i40e_aq_str(hw, hw->aq.asq_last_status));
  6268. return ret;
  6269. }
  6270. /* update the local VSI info with updated queue map */
  6271. i40e_vsi_update_queue_map(vsi, &ctxt);
  6272. vsi->info.valid_sections = 0;
  6273. /* Create channels for macvlans */
  6274. INIT_LIST_HEAD(&vsi->macvlan_list);
  6275. for (i = 0; i < macvlan_cnt; i++) {
  6276. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  6277. if (!ch) {
  6278. ret = -ENOMEM;
  6279. goto err_free;
  6280. }
  6281. INIT_LIST_HEAD(&ch->list);
  6282. ch->num_queue_pairs = qcnt;
  6283. if (!i40e_setup_channel(pf, vsi, ch)) {
  6284. ret = -EINVAL;
  6285. kfree(ch);
  6286. goto err_free;
  6287. }
  6288. ch->parent_vsi = vsi;
  6289. vsi->cnt_q_avail -= ch->num_queue_pairs;
  6290. vsi->macvlan_cnt++;
  6291. list_add_tail(&ch->list, &vsi->macvlan_list);
  6292. }
  6293. return ret;
  6294. err_free:
  6295. dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
  6296. i40e_free_macvlan_channels(vsi);
  6297. return ret;
  6298. }
  6299. /**
  6300. * i40e_fwd_add - configure macvlans
  6301. * @netdev: net device to configure
  6302. * @vdev: macvlan netdevice
  6303. **/
  6304. static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
  6305. {
  6306. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6307. u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
  6308. struct i40e_vsi *vsi = np->vsi;
  6309. struct i40e_pf *pf = vsi->back;
  6310. struct i40e_fwd_adapter *fwd;
  6311. int avail_macvlan, ret;
  6312. if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6313. netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
  6314. return ERR_PTR(-EINVAL);
  6315. }
  6316. if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
  6317. netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
  6318. return ERR_PTR(-EINVAL);
  6319. }
  6320. if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
  6321. netdev_info(netdev, "Not enough vectors available to support macvlans\n");
  6322. return ERR_PTR(-EINVAL);
  6323. }
  6324. /* The macvlan device has to be a single Q device so that the
  6325. * tc_to_txq field can be reused to pick the tx queue.
  6326. */
  6327. if (netif_is_multiqueue(vdev))
  6328. return ERR_PTR(-ERANGE);
  6329. if (!vsi->macvlan_cnt) {
  6330. /* reserve bit 0 for the pf device */
  6331. set_bit(0, vsi->fwd_bitmask);
  6332. /* Try to reserve as many queues as possible for macvlans. First
  6333. * reserve 3/4th of max vectors, then half, then quarter and
  6334. * calculate Qs per macvlan as you go
  6335. */
  6336. vectors = pf->num_lan_msix;
  6337. if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
  6338. /* allocate 4 Qs per macvlan and 32 Qs to the PF*/
  6339. q_per_macvlan = 4;
  6340. macvlan_cnt = (vectors - 32) / 4;
  6341. } else if (vectors <= 64 && vectors > 32) {
  6342. /* allocate 2 Qs per macvlan and 16 Qs to the PF*/
  6343. q_per_macvlan = 2;
  6344. macvlan_cnt = (vectors - 16) / 2;
  6345. } else if (vectors <= 32 && vectors > 16) {
  6346. /* allocate 1 Q per macvlan and 16 Qs to the PF*/
  6347. q_per_macvlan = 1;
  6348. macvlan_cnt = vectors - 16;
  6349. } else if (vectors <= 16 && vectors > 8) {
  6350. /* allocate 1 Q per macvlan and 8 Qs to the PF */
  6351. q_per_macvlan = 1;
  6352. macvlan_cnt = vectors - 8;
  6353. } else {
  6354. /* allocate 1 Q per macvlan and 1 Q to the PF */
  6355. q_per_macvlan = 1;
  6356. macvlan_cnt = vectors - 1;
  6357. }
  6358. if (macvlan_cnt == 0)
  6359. return ERR_PTR(-EBUSY);
  6360. /* Quiesce VSI queues */
  6361. i40e_quiesce_vsi(vsi);
  6362. /* sets up the macvlans but does not "enable" them */
  6363. ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
  6364. vdev);
  6365. if (ret)
  6366. return ERR_PTR(ret);
  6367. /* Unquiesce VSI */
  6368. i40e_unquiesce_vsi(vsi);
  6369. }
  6370. avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
  6371. vsi->macvlan_cnt);
  6372. if (avail_macvlan >= I40E_MAX_MACVLANS)
  6373. return ERR_PTR(-EBUSY);
  6374. /* create the fwd struct */
  6375. fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
  6376. if (!fwd)
  6377. return ERR_PTR(-ENOMEM);
  6378. set_bit(avail_macvlan, vsi->fwd_bitmask);
  6379. fwd->bit_no = avail_macvlan;
  6380. netdev_set_sb_channel(vdev, avail_macvlan);
  6381. fwd->netdev = vdev;
  6382. if (!netif_running(netdev))
  6383. return fwd;
  6384. /* Set fwd ring up */
  6385. ret = i40e_fwd_ring_up(vsi, vdev, fwd);
  6386. if (ret) {
  6387. /* unbind the queues and drop the subordinate channel config */
  6388. netdev_unbind_sb_channel(netdev, vdev);
  6389. netdev_set_sb_channel(vdev, 0);
  6390. kfree(fwd);
  6391. return ERR_PTR(-EINVAL);
  6392. }
  6393. return fwd;
  6394. }
  6395. /**
  6396. * i40e_del_all_macvlans - Delete all the mac filters on the channels
  6397. * @vsi: the VSI we want to access
  6398. */
  6399. static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
  6400. {
  6401. struct i40e_channel *ch, *ch_tmp;
  6402. struct i40e_pf *pf = vsi->back;
  6403. struct i40e_hw *hw = &pf->hw;
  6404. int aq_err, ret = 0;
  6405. if (list_empty(&vsi->macvlan_list))
  6406. return;
  6407. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  6408. if (i40e_is_channel_macvlan(ch)) {
  6409. ret = i40e_del_macvlan_filter(hw, ch->seid,
  6410. i40e_channel_mac(ch),
  6411. &aq_err);
  6412. if (!ret) {
  6413. /* Reset queue contexts */
  6414. i40e_reset_ch_rings(vsi, ch);
  6415. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  6416. netdev_unbind_sb_channel(vsi->netdev,
  6417. ch->fwd->netdev);
  6418. netdev_set_sb_channel(ch->fwd->netdev, 0);
  6419. kfree(ch->fwd);
  6420. ch->fwd = NULL;
  6421. }
  6422. }
  6423. }
  6424. }
  6425. /**
  6426. * i40e_fwd_del - delete macvlan interfaces
  6427. * @netdev: net device to configure
  6428. * @vdev: macvlan netdevice
  6429. */
  6430. static void i40e_fwd_del(struct net_device *netdev, void *vdev)
  6431. {
  6432. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6433. struct i40e_fwd_adapter *fwd = vdev;
  6434. struct i40e_channel *ch, *ch_tmp;
  6435. struct i40e_vsi *vsi = np->vsi;
  6436. struct i40e_pf *pf = vsi->back;
  6437. struct i40e_hw *hw = &pf->hw;
  6438. int aq_err, ret = 0;
  6439. /* Find the channel associated with the macvlan and del mac filter */
  6440. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  6441. if (i40e_is_channel_macvlan(ch) &&
  6442. ether_addr_equal(i40e_channel_mac(ch),
  6443. fwd->netdev->dev_addr)) {
  6444. ret = i40e_del_macvlan_filter(hw, ch->seid,
  6445. i40e_channel_mac(ch),
  6446. &aq_err);
  6447. if (!ret) {
  6448. /* Reset queue contexts */
  6449. i40e_reset_ch_rings(vsi, ch);
  6450. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  6451. netdev_unbind_sb_channel(netdev, fwd->netdev);
  6452. netdev_set_sb_channel(fwd->netdev, 0);
  6453. kfree(ch->fwd);
  6454. ch->fwd = NULL;
  6455. } else {
  6456. dev_info(&pf->pdev->dev,
  6457. "Error deleting mac filter on macvlan err %s, aq_err %s\n",
  6458. i40e_stat_str(hw, ret),
  6459. i40e_aq_str(hw, aq_err));
  6460. }
  6461. break;
  6462. }
  6463. }
  6464. }
  6465. /**
  6466. * i40e_setup_tc - configure multiple traffic classes
  6467. * @netdev: net device to configure
  6468. * @type_data: tc offload data
  6469. **/
  6470. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  6471. {
  6472. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  6473. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6474. struct i40e_vsi *vsi = np->vsi;
  6475. struct i40e_pf *pf = vsi->back;
  6476. u8 enabled_tc = 0, num_tc, hw;
  6477. bool need_reset = false;
  6478. int old_queue_pairs;
  6479. int ret = -EINVAL;
  6480. u16 mode;
  6481. int i;
  6482. old_queue_pairs = vsi->num_queue_pairs;
  6483. num_tc = mqprio_qopt->qopt.num_tc;
  6484. hw = mqprio_qopt->qopt.hw;
  6485. mode = mqprio_qopt->mode;
  6486. if (!hw) {
  6487. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  6488. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  6489. goto config_tc;
  6490. }
  6491. /* Check if MFP enabled */
  6492. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  6493. netdev_info(netdev,
  6494. "Configuring TC not supported in MFP mode\n");
  6495. return ret;
  6496. }
  6497. switch (mode) {
  6498. case TC_MQPRIO_MODE_DCB:
  6499. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  6500. /* Check if DCB enabled to continue */
  6501. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6502. netdev_info(netdev,
  6503. "DCB is not enabled for adapter\n");
  6504. return ret;
  6505. }
  6506. /* Check whether tc count is within enabled limit */
  6507. if (num_tc > i40e_pf_get_num_tc(pf)) {
  6508. netdev_info(netdev,
  6509. "TC count greater than enabled on link for adapter\n");
  6510. return ret;
  6511. }
  6512. break;
  6513. case TC_MQPRIO_MODE_CHANNEL:
  6514. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  6515. netdev_info(netdev,
  6516. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  6517. return ret;
  6518. }
  6519. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6520. return ret;
  6521. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  6522. if (ret)
  6523. return ret;
  6524. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  6525. sizeof(*mqprio_qopt));
  6526. pf->flags |= I40E_FLAG_TC_MQPRIO;
  6527. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  6528. break;
  6529. default:
  6530. return -EINVAL;
  6531. }
  6532. config_tc:
  6533. /* Generate TC map for number of tc requested */
  6534. for (i = 0; i < num_tc; i++)
  6535. enabled_tc |= BIT(i);
  6536. /* Requesting same TC configuration as already enabled */
  6537. if (enabled_tc == vsi->tc_config.enabled_tc &&
  6538. mode != TC_MQPRIO_MODE_CHANNEL)
  6539. return 0;
  6540. /* Quiesce VSI queues */
  6541. i40e_quiesce_vsi(vsi);
  6542. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  6543. i40e_remove_queue_channels(vsi);
  6544. /* Configure VSI for enabled TCs */
  6545. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  6546. if (ret) {
  6547. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  6548. vsi->seid);
  6549. need_reset = true;
  6550. goto exit;
  6551. } else {
  6552. dev_info(&vsi->back->pdev->dev,
  6553. "Setup channel (id:%u) utilizing num_queues %d\n",
  6554. vsi->seid, vsi->tc_config.tc_info[0].qcount);
  6555. }
  6556. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  6557. if (vsi->mqprio_qopt.max_rate[0]) {
  6558. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  6559. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  6560. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  6561. if (!ret) {
  6562. u64 credits = max_tx_rate;
  6563. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  6564. dev_dbg(&vsi->back->pdev->dev,
  6565. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  6566. max_tx_rate,
  6567. credits,
  6568. vsi->seid);
  6569. } else {
  6570. need_reset = true;
  6571. goto exit;
  6572. }
  6573. }
  6574. ret = i40e_configure_queue_channels(vsi);
  6575. if (ret) {
  6576. vsi->num_queue_pairs = old_queue_pairs;
  6577. netdev_info(netdev,
  6578. "Failed configuring queue channels\n");
  6579. need_reset = true;
  6580. goto exit;
  6581. }
  6582. }
  6583. exit:
  6584. /* Reset the configuration data to defaults, only TC0 is enabled */
  6585. if (need_reset) {
  6586. i40e_vsi_set_default_tc_config(vsi);
  6587. need_reset = false;
  6588. }
  6589. /* Unquiesce VSI */
  6590. i40e_unquiesce_vsi(vsi);
  6591. return ret;
  6592. }
  6593. /**
  6594. * i40e_set_cld_element - sets cloud filter element data
  6595. * @filter: cloud filter rule
  6596. * @cld: ptr to cloud filter element data
  6597. *
  6598. * This is helper function to copy data into cloud filter element
  6599. **/
  6600. static inline void
  6601. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6602. struct i40e_aqc_cloud_filters_element_data *cld)
  6603. {
  6604. int i, j;
  6605. u32 ipa;
  6606. memset(cld, 0, sizeof(*cld));
  6607. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6608. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6609. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6610. return;
  6611. if (filter->n_proto == ETH_P_IPV6) {
  6612. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6613. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6614. i++, j += 2) {
  6615. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6616. ipa = cpu_to_le32(ipa);
  6617. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6618. }
  6619. } else {
  6620. ipa = be32_to_cpu(filter->dst_ipv4);
  6621. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6622. }
  6623. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6624. /* tenant_id is not supported by FW now, once the support is enabled
  6625. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6626. */
  6627. if (filter->tenant_id)
  6628. return;
  6629. }
  6630. /**
  6631. * i40e_add_del_cloud_filter - Add/del cloud filter
  6632. * @vsi: pointer to VSI
  6633. * @filter: cloud filter rule
  6634. * @add: if true, add, if false, delete
  6635. *
  6636. * Add or delete a cloud filter for a specific flow spec.
  6637. * Returns 0 if the filter were successfully added.
  6638. **/
  6639. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6640. struct i40e_cloud_filter *filter, bool add)
  6641. {
  6642. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6643. struct i40e_pf *pf = vsi->back;
  6644. int ret;
  6645. static const u16 flag_table[128] = {
  6646. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6647. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6648. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6649. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6650. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6651. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6652. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6653. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6654. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6655. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6656. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6657. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6658. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6659. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6660. };
  6661. if (filter->flags >= ARRAY_SIZE(flag_table))
  6662. return I40E_ERR_CONFIG;
  6663. /* copy element needed to add cloud filter from filter */
  6664. i40e_set_cld_element(filter, &cld_filter);
  6665. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6666. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6667. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6668. if (filter->n_proto == ETH_P_IPV6)
  6669. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6670. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6671. else
  6672. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6673. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6674. if (add)
  6675. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6676. &cld_filter, 1);
  6677. else
  6678. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6679. &cld_filter, 1);
  6680. if (ret)
  6681. dev_dbg(&pf->pdev->dev,
  6682. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6683. add ? "add" : "delete", filter->dst_port, ret,
  6684. pf->hw.aq.asq_last_status);
  6685. else
  6686. dev_info(&pf->pdev->dev,
  6687. "%s cloud filter for VSI: %d\n",
  6688. add ? "Added" : "Deleted", filter->seid);
  6689. return ret;
  6690. }
  6691. /**
  6692. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6693. * @vsi: pointer to VSI
  6694. * @filter: cloud filter rule
  6695. * @add: if true, add, if false, delete
  6696. *
  6697. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6698. * Returns 0 if the filter were successfully added.
  6699. **/
  6700. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6701. struct i40e_cloud_filter *filter,
  6702. bool add)
  6703. {
  6704. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6705. struct i40e_pf *pf = vsi->back;
  6706. int ret;
  6707. /* Both (src/dst) valid mac_addr are not supported */
  6708. if ((is_valid_ether_addr(filter->dst_mac) &&
  6709. is_valid_ether_addr(filter->src_mac)) ||
  6710. (is_multicast_ether_addr(filter->dst_mac) &&
  6711. is_multicast_ether_addr(filter->src_mac)))
  6712. return -EOPNOTSUPP;
  6713. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6714. * ports are not supported via big buffer now.
  6715. */
  6716. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6717. return -EOPNOTSUPP;
  6718. /* adding filter using src_port/src_ip is not supported at this stage */
  6719. if (filter->src_port || filter->src_ipv4 ||
  6720. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6721. return -EOPNOTSUPP;
  6722. /* copy element needed to add cloud filter from filter */
  6723. i40e_set_cld_element(filter, &cld_filter.element);
  6724. if (is_valid_ether_addr(filter->dst_mac) ||
  6725. is_valid_ether_addr(filter->src_mac) ||
  6726. is_multicast_ether_addr(filter->dst_mac) ||
  6727. is_multicast_ether_addr(filter->src_mac)) {
  6728. /* MAC + IP : unsupported mode */
  6729. if (filter->dst_ipv4)
  6730. return -EOPNOTSUPP;
  6731. /* since we validated that L4 port must be valid before
  6732. * we get here, start with respective "flags" value
  6733. * and update if vlan is present or not
  6734. */
  6735. cld_filter.element.flags =
  6736. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6737. if (filter->vlan_id) {
  6738. cld_filter.element.flags =
  6739. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6740. }
  6741. } else if (filter->dst_ipv4 ||
  6742. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6743. cld_filter.element.flags =
  6744. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6745. if (filter->n_proto == ETH_P_IPV6)
  6746. cld_filter.element.flags |=
  6747. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6748. else
  6749. cld_filter.element.flags |=
  6750. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6751. } else {
  6752. dev_err(&pf->pdev->dev,
  6753. "either mac or ip has to be valid for cloud filter\n");
  6754. return -EINVAL;
  6755. }
  6756. /* Now copy L4 port in Byte 6..7 in general fields */
  6757. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6758. be16_to_cpu(filter->dst_port);
  6759. if (add) {
  6760. /* Validate current device switch mode, change if necessary */
  6761. ret = i40e_validate_and_set_switch_mode(vsi);
  6762. if (ret) {
  6763. dev_err(&pf->pdev->dev,
  6764. "failed to set switch mode, ret %d\n",
  6765. ret);
  6766. return ret;
  6767. }
  6768. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6769. &cld_filter, 1);
  6770. } else {
  6771. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6772. &cld_filter, 1);
  6773. }
  6774. if (ret)
  6775. dev_dbg(&pf->pdev->dev,
  6776. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6777. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6778. else
  6779. dev_info(&pf->pdev->dev,
  6780. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6781. add ? "add" : "delete", filter->seid,
  6782. ntohs(filter->dst_port));
  6783. return ret;
  6784. }
  6785. /**
  6786. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6787. * @vsi: Pointer to VSI
  6788. * @cls_flower: Pointer to struct flow_cls_offload
  6789. * @filter: Pointer to cloud filter structure
  6790. *
  6791. **/
  6792. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6793. struct flow_cls_offload *f,
  6794. struct i40e_cloud_filter *filter)
  6795. {
  6796. struct flow_rule *rule = flow_cls_offload_flow_rule(f);
  6797. struct flow_dissector *dissector = rule->match.dissector;
  6798. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6799. struct i40e_pf *pf = vsi->back;
  6800. u8 field_flags = 0;
  6801. if (dissector->used_keys &
  6802. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6803. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6804. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6805. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6806. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6807. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6808. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6809. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6810. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6811. dissector->used_keys);
  6812. return -EOPNOTSUPP;
  6813. }
  6814. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6815. struct flow_match_enc_keyid match;
  6816. flow_rule_match_enc_keyid(rule, &match);
  6817. if (match.mask->keyid != 0)
  6818. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6819. filter->tenant_id = be32_to_cpu(match.key->keyid);
  6820. }
  6821. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  6822. struct flow_match_basic match;
  6823. flow_rule_match_basic(rule, &match);
  6824. n_proto_key = ntohs(match.key->n_proto);
  6825. n_proto_mask = ntohs(match.mask->n_proto);
  6826. if (n_proto_key == ETH_P_ALL) {
  6827. n_proto_key = 0;
  6828. n_proto_mask = 0;
  6829. }
  6830. filter->n_proto = n_proto_key & n_proto_mask;
  6831. filter->ip_proto = match.key->ip_proto;
  6832. }
  6833. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6834. struct flow_match_eth_addrs match;
  6835. flow_rule_match_eth_addrs(rule, &match);
  6836. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6837. if (!is_zero_ether_addr(match.mask->dst)) {
  6838. if (is_broadcast_ether_addr(match.mask->dst)) {
  6839. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6840. } else {
  6841. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6842. match.mask->dst);
  6843. return I40E_ERR_CONFIG;
  6844. }
  6845. }
  6846. if (!is_zero_ether_addr(match.mask->src)) {
  6847. if (is_broadcast_ether_addr(match.mask->src)) {
  6848. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6849. } else {
  6850. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6851. match.mask->src);
  6852. return I40E_ERR_CONFIG;
  6853. }
  6854. }
  6855. ether_addr_copy(filter->dst_mac, match.key->dst);
  6856. ether_addr_copy(filter->src_mac, match.key->src);
  6857. }
  6858. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
  6859. struct flow_match_vlan match;
  6860. flow_rule_match_vlan(rule, &match);
  6861. if (match.mask->vlan_id) {
  6862. if (match.mask->vlan_id == VLAN_VID_MASK) {
  6863. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6864. } else {
  6865. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6866. match.mask->vlan_id);
  6867. return I40E_ERR_CONFIG;
  6868. }
  6869. }
  6870. filter->vlan_id = cpu_to_be16(match.key->vlan_id);
  6871. }
  6872. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
  6873. struct flow_match_control match;
  6874. flow_rule_match_control(rule, &match);
  6875. addr_type = match.key->addr_type;
  6876. }
  6877. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6878. struct flow_match_ipv4_addrs match;
  6879. flow_rule_match_ipv4_addrs(rule, &match);
  6880. if (match.mask->dst) {
  6881. if (match.mask->dst == cpu_to_be32(0xffffffff)) {
  6882. field_flags |= I40E_CLOUD_FIELD_IIP;
  6883. } else {
  6884. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6885. &match.mask->dst);
  6886. return I40E_ERR_CONFIG;
  6887. }
  6888. }
  6889. if (match.mask->src) {
  6890. if (match.mask->src == cpu_to_be32(0xffffffff)) {
  6891. field_flags |= I40E_CLOUD_FIELD_IIP;
  6892. } else {
  6893. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6894. &match.mask->src);
  6895. return I40E_ERR_CONFIG;
  6896. }
  6897. }
  6898. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6899. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6900. return I40E_ERR_CONFIG;
  6901. }
  6902. filter->dst_ipv4 = match.key->dst;
  6903. filter->src_ipv4 = match.key->src;
  6904. }
  6905. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6906. struct flow_match_ipv6_addrs match;
  6907. flow_rule_match_ipv6_addrs(rule, &match);
  6908. /* src and dest IPV6 address should not be LOOPBACK
  6909. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6910. */
  6911. if (ipv6_addr_loopback(&match.key->dst) ||
  6912. ipv6_addr_loopback(&match.key->src)) {
  6913. dev_err(&pf->pdev->dev,
  6914. "Bad ipv6, addr is LOOPBACK\n");
  6915. return I40E_ERR_CONFIG;
  6916. }
  6917. if (!ipv6_addr_any(&match.mask->dst) ||
  6918. !ipv6_addr_any(&match.mask->src))
  6919. field_flags |= I40E_CLOUD_FIELD_IIP;
  6920. memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
  6921. sizeof(filter->src_ipv6));
  6922. memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
  6923. sizeof(filter->dst_ipv6));
  6924. }
  6925. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
  6926. struct flow_match_ports match;
  6927. flow_rule_match_ports(rule, &match);
  6928. if (match.mask->src) {
  6929. if (match.mask->src == cpu_to_be16(0xffff)) {
  6930. field_flags |= I40E_CLOUD_FIELD_IIP;
  6931. } else {
  6932. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6933. be16_to_cpu(match.mask->src));
  6934. return I40E_ERR_CONFIG;
  6935. }
  6936. }
  6937. if (match.mask->dst) {
  6938. if (match.mask->dst == cpu_to_be16(0xffff)) {
  6939. field_flags |= I40E_CLOUD_FIELD_IIP;
  6940. } else {
  6941. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6942. be16_to_cpu(match.mask->dst));
  6943. return I40E_ERR_CONFIG;
  6944. }
  6945. }
  6946. filter->dst_port = match.key->dst;
  6947. filter->src_port = match.key->src;
  6948. switch (filter->ip_proto) {
  6949. case IPPROTO_TCP:
  6950. case IPPROTO_UDP:
  6951. break;
  6952. default:
  6953. dev_err(&pf->pdev->dev,
  6954. "Only UDP and TCP transport are supported\n");
  6955. return -EINVAL;
  6956. }
  6957. }
  6958. filter->flags = field_flags;
  6959. return 0;
  6960. }
  6961. /**
  6962. * i40e_handle_tclass: Forward to a traffic class on the device
  6963. * @vsi: Pointer to VSI
  6964. * @tc: traffic class index on the device
  6965. * @filter: Pointer to cloud filter structure
  6966. *
  6967. **/
  6968. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6969. struct i40e_cloud_filter *filter)
  6970. {
  6971. struct i40e_channel *ch, *ch_tmp;
  6972. /* direct to a traffic class on the same device */
  6973. if (tc == 0) {
  6974. filter->seid = vsi->seid;
  6975. return 0;
  6976. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6977. if (!filter->dst_port) {
  6978. dev_err(&vsi->back->pdev->dev,
  6979. "Specify destination port to direct to traffic class that is not default\n");
  6980. return -EINVAL;
  6981. }
  6982. if (list_empty(&vsi->ch_list))
  6983. return -EINVAL;
  6984. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6985. list) {
  6986. if (ch->seid == vsi->tc_seid_map[tc])
  6987. filter->seid = ch->seid;
  6988. }
  6989. return 0;
  6990. }
  6991. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6992. return -EINVAL;
  6993. }
  6994. /**
  6995. * i40e_configure_clsflower - Configure tc flower filters
  6996. * @vsi: Pointer to VSI
  6997. * @cls_flower: Pointer to struct flow_cls_offload
  6998. *
  6999. **/
  7000. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  7001. struct flow_cls_offload *cls_flower)
  7002. {
  7003. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  7004. struct i40e_cloud_filter *filter = NULL;
  7005. struct i40e_pf *pf = vsi->back;
  7006. int err = 0;
  7007. if (tc < 0) {
  7008. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  7009. return -EOPNOTSUPP;
  7010. }
  7011. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  7012. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  7013. return -EBUSY;
  7014. if (pf->fdir_pf_active_filters ||
  7015. (!hlist_empty(&pf->fdir_filter_list))) {
  7016. dev_err(&vsi->back->pdev->dev,
  7017. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  7018. return -EINVAL;
  7019. }
  7020. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  7021. dev_err(&vsi->back->pdev->dev,
  7022. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  7023. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7024. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  7025. }
  7026. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  7027. if (!filter)
  7028. return -ENOMEM;
  7029. filter->cookie = cls_flower->cookie;
  7030. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  7031. if (err < 0)
  7032. goto err;
  7033. err = i40e_handle_tclass(vsi, tc, filter);
  7034. if (err < 0)
  7035. goto err;
  7036. /* Add cloud filter */
  7037. if (filter->dst_port)
  7038. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  7039. else
  7040. err = i40e_add_del_cloud_filter(vsi, filter, true);
  7041. if (err) {
  7042. dev_err(&pf->pdev->dev,
  7043. "Failed to add cloud filter, err %s\n",
  7044. i40e_stat_str(&pf->hw, err));
  7045. goto err;
  7046. }
  7047. /* add filter to the ordered list */
  7048. INIT_HLIST_NODE(&filter->cloud_node);
  7049. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  7050. pf->num_cloud_filters++;
  7051. return err;
  7052. err:
  7053. kfree(filter);
  7054. return err;
  7055. }
  7056. /**
  7057. * i40e_find_cloud_filter - Find the could filter in the list
  7058. * @vsi: Pointer to VSI
  7059. * @cookie: filter specific cookie
  7060. *
  7061. **/
  7062. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  7063. unsigned long *cookie)
  7064. {
  7065. struct i40e_cloud_filter *filter = NULL;
  7066. struct hlist_node *node2;
  7067. hlist_for_each_entry_safe(filter, node2,
  7068. &vsi->back->cloud_filter_list, cloud_node)
  7069. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  7070. return filter;
  7071. return NULL;
  7072. }
  7073. /**
  7074. * i40e_delete_clsflower - Remove tc flower filters
  7075. * @vsi: Pointer to VSI
  7076. * @cls_flower: Pointer to struct flow_cls_offload
  7077. *
  7078. **/
  7079. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  7080. struct flow_cls_offload *cls_flower)
  7081. {
  7082. struct i40e_cloud_filter *filter = NULL;
  7083. struct i40e_pf *pf = vsi->back;
  7084. int err = 0;
  7085. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  7086. if (!filter)
  7087. return -EINVAL;
  7088. hash_del(&filter->cloud_node);
  7089. if (filter->dst_port)
  7090. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  7091. else
  7092. err = i40e_add_del_cloud_filter(vsi, filter, false);
  7093. kfree(filter);
  7094. if (err) {
  7095. dev_err(&pf->pdev->dev,
  7096. "Failed to delete cloud filter, err %s\n",
  7097. i40e_stat_str(&pf->hw, err));
  7098. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  7099. }
  7100. pf->num_cloud_filters--;
  7101. if (!pf->num_cloud_filters)
  7102. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  7103. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  7104. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7105. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  7106. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  7107. }
  7108. return 0;
  7109. }
  7110. /**
  7111. * i40e_setup_tc_cls_flower - flower classifier offloads
  7112. * @netdev: net device to configure
  7113. * @type_data: offload data
  7114. **/
  7115. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  7116. struct flow_cls_offload *cls_flower)
  7117. {
  7118. struct i40e_vsi *vsi = np->vsi;
  7119. switch (cls_flower->command) {
  7120. case FLOW_CLS_REPLACE:
  7121. return i40e_configure_clsflower(vsi, cls_flower);
  7122. case FLOW_CLS_DESTROY:
  7123. return i40e_delete_clsflower(vsi, cls_flower);
  7124. case FLOW_CLS_STATS:
  7125. return -EOPNOTSUPP;
  7126. default:
  7127. return -EOPNOTSUPP;
  7128. }
  7129. }
  7130. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  7131. void *cb_priv)
  7132. {
  7133. struct i40e_netdev_priv *np = cb_priv;
  7134. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  7135. return -EOPNOTSUPP;
  7136. switch (type) {
  7137. case TC_SETUP_CLSFLOWER:
  7138. return i40e_setup_tc_cls_flower(np, type_data);
  7139. default:
  7140. return -EOPNOTSUPP;
  7141. }
  7142. }
  7143. static LIST_HEAD(i40e_block_cb_list);
  7144. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  7145. void *type_data)
  7146. {
  7147. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7148. switch (type) {
  7149. case TC_SETUP_QDISC_MQPRIO:
  7150. return i40e_setup_tc(netdev, type_data);
  7151. case TC_SETUP_BLOCK:
  7152. return flow_block_cb_setup_simple(type_data,
  7153. &i40e_block_cb_list,
  7154. i40e_setup_tc_block_cb,
  7155. np, np, true);
  7156. default:
  7157. return -EOPNOTSUPP;
  7158. }
  7159. }
  7160. /**
  7161. * i40e_open - Called when a network interface is made active
  7162. * @netdev: network interface device structure
  7163. *
  7164. * The open entry point is called when a network interface is made
  7165. * active by the system (IFF_UP). At this point all resources needed
  7166. * for transmit and receive operations are allocated, the interrupt
  7167. * handler is registered with the OS, the netdev watchdog subtask is
  7168. * enabled, and the stack is notified that the interface is ready.
  7169. *
  7170. * Returns 0 on success, negative value on failure
  7171. **/
  7172. int i40e_open(struct net_device *netdev)
  7173. {
  7174. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7175. struct i40e_vsi *vsi = np->vsi;
  7176. struct i40e_pf *pf = vsi->back;
  7177. int err;
  7178. /* disallow open during test or if eeprom is broken */
  7179. if (test_bit(__I40E_TESTING, pf->state) ||
  7180. test_bit(__I40E_BAD_EEPROM, pf->state))
  7181. return -EBUSY;
  7182. netif_carrier_off(netdev);
  7183. if (i40e_force_link_state(pf, true))
  7184. return -EAGAIN;
  7185. err = i40e_vsi_open(vsi);
  7186. if (err)
  7187. return err;
  7188. /* configure global TSO hardware offload settings */
  7189. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  7190. TCP_FLAG_FIN) >> 16);
  7191. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  7192. TCP_FLAG_FIN |
  7193. TCP_FLAG_CWR) >> 16);
  7194. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  7195. udp_tunnel_get_rx_info(netdev);
  7196. return 0;
  7197. }
  7198. /**
  7199. * i40e_vsi_open -
  7200. * @vsi: the VSI to open
  7201. *
  7202. * Finish initialization of the VSI.
  7203. *
  7204. * Returns 0 on success, negative value on failure
  7205. *
  7206. * Note: expects to be called while under rtnl_lock()
  7207. **/
  7208. int i40e_vsi_open(struct i40e_vsi *vsi)
  7209. {
  7210. struct i40e_pf *pf = vsi->back;
  7211. char int_name[I40E_INT_NAME_STR_LEN];
  7212. int err;
  7213. /* allocate descriptors */
  7214. err = i40e_vsi_setup_tx_resources(vsi);
  7215. if (err)
  7216. goto err_setup_tx;
  7217. err = i40e_vsi_setup_rx_resources(vsi);
  7218. if (err)
  7219. goto err_setup_rx;
  7220. err = i40e_vsi_configure(vsi);
  7221. if (err)
  7222. goto err_setup_rx;
  7223. if (vsi->netdev) {
  7224. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  7225. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  7226. err = i40e_vsi_request_irq(vsi, int_name);
  7227. if (err)
  7228. goto err_setup_rx;
  7229. /* Notify the stack of the actual queue counts. */
  7230. err = netif_set_real_num_tx_queues(vsi->netdev,
  7231. vsi->num_queue_pairs);
  7232. if (err)
  7233. goto err_set_queues;
  7234. err = netif_set_real_num_rx_queues(vsi->netdev,
  7235. vsi->num_queue_pairs);
  7236. if (err)
  7237. goto err_set_queues;
  7238. } else if (vsi->type == I40E_VSI_FDIR) {
  7239. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  7240. dev_driver_string(&pf->pdev->dev),
  7241. dev_name(&pf->pdev->dev));
  7242. err = i40e_vsi_request_irq(vsi, int_name);
  7243. } else {
  7244. err = -EINVAL;
  7245. goto err_setup_rx;
  7246. }
  7247. err = i40e_up_complete(vsi);
  7248. if (err)
  7249. goto err_up_complete;
  7250. return 0;
  7251. err_up_complete:
  7252. i40e_down(vsi);
  7253. err_set_queues:
  7254. i40e_vsi_free_irq(vsi);
  7255. err_setup_rx:
  7256. i40e_vsi_free_rx_resources(vsi);
  7257. err_setup_tx:
  7258. i40e_vsi_free_tx_resources(vsi);
  7259. if (vsi == pf->vsi[pf->lan_vsi])
  7260. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  7261. return err;
  7262. }
  7263. /**
  7264. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  7265. * @pf: Pointer to PF
  7266. *
  7267. * This function destroys the hlist where all the Flow Director
  7268. * filters were saved.
  7269. **/
  7270. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  7271. {
  7272. struct i40e_fdir_filter *filter;
  7273. struct i40e_flex_pit *pit_entry, *tmp;
  7274. struct hlist_node *node2;
  7275. hlist_for_each_entry_safe(filter, node2,
  7276. &pf->fdir_filter_list, fdir_node) {
  7277. hlist_del(&filter->fdir_node);
  7278. kfree(filter);
  7279. }
  7280. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  7281. list_del(&pit_entry->list);
  7282. kfree(pit_entry);
  7283. }
  7284. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  7285. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  7286. list_del(&pit_entry->list);
  7287. kfree(pit_entry);
  7288. }
  7289. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  7290. pf->fdir_pf_active_filters = 0;
  7291. pf->fd_tcp4_filter_cnt = 0;
  7292. pf->fd_udp4_filter_cnt = 0;
  7293. pf->fd_sctp4_filter_cnt = 0;
  7294. pf->fd_ip4_filter_cnt = 0;
  7295. /* Reprogram the default input set for TCP/IPv4 */
  7296. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7297. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7298. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7299. /* Reprogram the default input set for UDP/IPv4 */
  7300. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  7301. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7302. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7303. /* Reprogram the default input set for SCTP/IPv4 */
  7304. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  7305. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7306. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7307. /* Reprogram the default input set for Other/IPv4 */
  7308. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  7309. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  7310. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  7311. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  7312. }
  7313. /**
  7314. * i40e_cloud_filter_exit - Cleans up the cloud filters
  7315. * @pf: Pointer to PF
  7316. *
  7317. * This function destroys the hlist where all the cloud filters
  7318. * were saved.
  7319. **/
  7320. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  7321. {
  7322. struct i40e_cloud_filter *cfilter;
  7323. struct hlist_node *node;
  7324. hlist_for_each_entry_safe(cfilter, node,
  7325. &pf->cloud_filter_list, cloud_node) {
  7326. hlist_del(&cfilter->cloud_node);
  7327. kfree(cfilter);
  7328. }
  7329. pf->num_cloud_filters = 0;
  7330. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  7331. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  7332. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7333. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  7334. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  7335. }
  7336. }
  7337. /**
  7338. * i40e_close - Disables a network interface
  7339. * @netdev: network interface device structure
  7340. *
  7341. * The close entry point is called when an interface is de-activated
  7342. * by the OS. The hardware is still under the driver's control, but
  7343. * this netdev interface is disabled.
  7344. *
  7345. * Returns 0, this is not allowed to fail
  7346. **/
  7347. int i40e_close(struct net_device *netdev)
  7348. {
  7349. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7350. struct i40e_vsi *vsi = np->vsi;
  7351. i40e_vsi_close(vsi);
  7352. return 0;
  7353. }
  7354. /**
  7355. * i40e_do_reset - Start a PF or Core Reset sequence
  7356. * @pf: board private structure
  7357. * @reset_flags: which reset is requested
  7358. * @lock_acquired: indicates whether or not the lock has been acquired
  7359. * before this function was called.
  7360. *
  7361. * The essential difference in resets is that the PF Reset
  7362. * doesn't clear the packet buffers, doesn't reset the PE
  7363. * firmware, and doesn't bother the other PFs on the chip.
  7364. **/
  7365. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  7366. {
  7367. u32 val;
  7368. WARN_ON(in_interrupt());
  7369. /* do the biggest reset indicated */
  7370. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  7371. /* Request a Global Reset
  7372. *
  7373. * This will start the chip's countdown to the actual full
  7374. * chip reset event, and a warning interrupt to be sent
  7375. * to all PFs, including the requestor. Our handler
  7376. * for the warning interrupt will deal with the shutdown
  7377. * and recovery of the switch setup.
  7378. */
  7379. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  7380. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  7381. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  7382. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  7383. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  7384. /* Request a Core Reset
  7385. *
  7386. * Same as Global Reset, except does *not* include the MAC/PHY
  7387. */
  7388. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  7389. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  7390. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  7391. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  7392. i40e_flush(&pf->hw);
  7393. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  7394. /* Request a PF Reset
  7395. *
  7396. * Resets only the PF-specific registers
  7397. *
  7398. * This goes directly to the tear-down and rebuild of
  7399. * the switch, since we need to do all the recovery as
  7400. * for the Core Reset.
  7401. */
  7402. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  7403. i40e_handle_reset_warning(pf, lock_acquired);
  7404. dev_info(&pf->pdev->dev,
  7405. pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
  7406. "FW LLDP is disabled\n" :
  7407. "FW LLDP is enabled\n");
  7408. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  7409. int v;
  7410. /* Find the VSI(s) that requested a re-init */
  7411. dev_info(&pf->pdev->dev,
  7412. "VSI reinit requested\n");
  7413. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7414. struct i40e_vsi *vsi = pf->vsi[v];
  7415. if (vsi != NULL &&
  7416. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  7417. vsi->state))
  7418. i40e_vsi_reinit_locked(pf->vsi[v]);
  7419. }
  7420. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  7421. int v;
  7422. /* Find the VSI(s) that needs to be brought down */
  7423. dev_info(&pf->pdev->dev, "VSI down requested\n");
  7424. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7425. struct i40e_vsi *vsi = pf->vsi[v];
  7426. if (vsi != NULL &&
  7427. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  7428. vsi->state)) {
  7429. set_bit(__I40E_VSI_DOWN, vsi->state);
  7430. i40e_down(vsi);
  7431. }
  7432. }
  7433. } else {
  7434. dev_info(&pf->pdev->dev,
  7435. "bad reset request 0x%08x\n", reset_flags);
  7436. }
  7437. }
  7438. #ifdef CONFIG_I40E_DCB
  7439. /**
  7440. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  7441. * @pf: board private structure
  7442. * @old_cfg: current DCB config
  7443. * @new_cfg: new DCB config
  7444. **/
  7445. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  7446. struct i40e_dcbx_config *old_cfg,
  7447. struct i40e_dcbx_config *new_cfg)
  7448. {
  7449. bool need_reconfig = false;
  7450. /* Check if ETS configuration has changed */
  7451. if (memcmp(&new_cfg->etscfg,
  7452. &old_cfg->etscfg,
  7453. sizeof(new_cfg->etscfg))) {
  7454. /* If Priority Table has changed reconfig is needed */
  7455. if (memcmp(&new_cfg->etscfg.prioritytable,
  7456. &old_cfg->etscfg.prioritytable,
  7457. sizeof(new_cfg->etscfg.prioritytable))) {
  7458. need_reconfig = true;
  7459. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  7460. }
  7461. if (memcmp(&new_cfg->etscfg.tcbwtable,
  7462. &old_cfg->etscfg.tcbwtable,
  7463. sizeof(new_cfg->etscfg.tcbwtable)))
  7464. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  7465. if (memcmp(&new_cfg->etscfg.tsatable,
  7466. &old_cfg->etscfg.tsatable,
  7467. sizeof(new_cfg->etscfg.tsatable)))
  7468. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  7469. }
  7470. /* Check if PFC configuration has changed */
  7471. if (memcmp(&new_cfg->pfc,
  7472. &old_cfg->pfc,
  7473. sizeof(new_cfg->pfc))) {
  7474. need_reconfig = true;
  7475. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  7476. }
  7477. /* Check if APP Table has changed */
  7478. if (memcmp(&new_cfg->app,
  7479. &old_cfg->app,
  7480. sizeof(new_cfg->app))) {
  7481. need_reconfig = true;
  7482. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  7483. }
  7484. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  7485. return need_reconfig;
  7486. }
  7487. /**
  7488. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  7489. * @pf: board private structure
  7490. * @e: event info posted on ARQ
  7491. **/
  7492. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  7493. struct i40e_arq_event_info *e)
  7494. {
  7495. struct i40e_aqc_lldp_get_mib *mib =
  7496. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  7497. struct i40e_hw *hw = &pf->hw;
  7498. struct i40e_dcbx_config tmp_dcbx_cfg;
  7499. bool need_reconfig = false;
  7500. int ret = 0;
  7501. u8 type;
  7502. /* Not DCB capable or capability disabled */
  7503. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  7504. return ret;
  7505. /* Ignore if event is not for Nearest Bridge */
  7506. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  7507. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  7508. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  7509. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  7510. return ret;
  7511. /* Check MIB Type and return if event for Remote MIB update */
  7512. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  7513. dev_dbg(&pf->pdev->dev,
  7514. "LLDP event mib type %s\n", type ? "remote" : "local");
  7515. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  7516. /* Update the remote cached instance and return */
  7517. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  7518. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  7519. &hw->remote_dcbx_config);
  7520. goto exit;
  7521. }
  7522. /* Store the old configuration */
  7523. tmp_dcbx_cfg = hw->local_dcbx_config;
  7524. /* Reset the old DCBx configuration data */
  7525. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  7526. /* Get updated DCBX data from firmware */
  7527. ret = i40e_get_dcb_config(&pf->hw);
  7528. if (ret) {
  7529. dev_info(&pf->pdev->dev,
  7530. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  7531. i40e_stat_str(&pf->hw, ret),
  7532. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7533. goto exit;
  7534. }
  7535. /* No change detected in DCBX configs */
  7536. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7537. sizeof(tmp_dcbx_cfg))) {
  7538. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7539. goto exit;
  7540. }
  7541. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7542. &hw->local_dcbx_config);
  7543. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7544. if (!need_reconfig)
  7545. goto exit;
  7546. /* Enable DCB tagging only when more than one TC */
  7547. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7548. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7549. else
  7550. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7551. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7552. /* Reconfiguration needed quiesce all VSIs */
  7553. i40e_pf_quiesce_all_vsi(pf);
  7554. /* Changes in configuration update VEB/VSI */
  7555. i40e_dcb_reconfigure(pf);
  7556. ret = i40e_resume_port_tx(pf);
  7557. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7558. /* In case of error no point in resuming VSIs */
  7559. if (ret)
  7560. goto exit;
  7561. /* Wait for the PF's queues to be disabled */
  7562. ret = i40e_pf_wait_queues_disabled(pf);
  7563. if (ret) {
  7564. /* Schedule PF reset to recover */
  7565. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7566. i40e_service_event_schedule(pf);
  7567. } else {
  7568. i40e_pf_unquiesce_all_vsi(pf);
  7569. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7570. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7571. }
  7572. exit:
  7573. return ret;
  7574. }
  7575. #endif /* CONFIG_I40E_DCB */
  7576. /**
  7577. * i40e_do_reset_safe - Protected reset path for userland calls.
  7578. * @pf: board private structure
  7579. * @reset_flags: which reset is requested
  7580. *
  7581. **/
  7582. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7583. {
  7584. rtnl_lock();
  7585. i40e_do_reset(pf, reset_flags, true);
  7586. rtnl_unlock();
  7587. }
  7588. /**
  7589. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7590. * @pf: board private structure
  7591. * @e: event info posted on ARQ
  7592. *
  7593. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7594. * and VF queues
  7595. **/
  7596. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7597. struct i40e_arq_event_info *e)
  7598. {
  7599. struct i40e_aqc_lan_overflow *data =
  7600. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7601. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7602. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7603. struct i40e_hw *hw = &pf->hw;
  7604. struct i40e_vf *vf;
  7605. u16 vf_id;
  7606. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7607. queue, qtx_ctl);
  7608. /* Queue belongs to VF, find the VF and issue VF reset */
  7609. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7610. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7611. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7612. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7613. vf_id -= hw->func_caps.vf_base_id;
  7614. vf = &pf->vf[vf_id];
  7615. i40e_vc_notify_vf_reset(vf);
  7616. /* Allow VF to process pending reset notification */
  7617. msleep(20);
  7618. i40e_reset_vf(vf, false);
  7619. }
  7620. }
  7621. /**
  7622. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7623. * @pf: board private structure
  7624. **/
  7625. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7626. {
  7627. u32 val, fcnt_prog;
  7628. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7629. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7630. return fcnt_prog;
  7631. }
  7632. /**
  7633. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7634. * @pf: board private structure
  7635. **/
  7636. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7637. {
  7638. u32 val, fcnt_prog;
  7639. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7640. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7641. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7642. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7643. return fcnt_prog;
  7644. }
  7645. /**
  7646. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7647. * @pf: board private structure
  7648. **/
  7649. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7650. {
  7651. u32 val, fcnt_prog;
  7652. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7653. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7654. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7655. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7656. return fcnt_prog;
  7657. }
  7658. /**
  7659. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7660. * @pf: board private structure
  7661. **/
  7662. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7663. {
  7664. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7665. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7666. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7667. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7668. }
  7669. /**
  7670. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7671. * @pf: board private structure
  7672. **/
  7673. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7674. {
  7675. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7676. /* ATR uses the same filtering logic as SB rules. It only
  7677. * functions properly if the input set mask is at the default
  7678. * settings. It is safe to restore the default input set
  7679. * because there are no active TCPv4 filter rules.
  7680. */
  7681. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7682. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7683. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7684. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7685. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7686. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7687. }
  7688. }
  7689. /**
  7690. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7691. * @pf: board private structure
  7692. * @filter: FDir filter to remove
  7693. */
  7694. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7695. struct i40e_fdir_filter *filter)
  7696. {
  7697. /* Update counters */
  7698. pf->fdir_pf_active_filters--;
  7699. pf->fd_inv = 0;
  7700. switch (filter->flow_type) {
  7701. case TCP_V4_FLOW:
  7702. pf->fd_tcp4_filter_cnt--;
  7703. break;
  7704. case UDP_V4_FLOW:
  7705. pf->fd_udp4_filter_cnt--;
  7706. break;
  7707. case SCTP_V4_FLOW:
  7708. pf->fd_sctp4_filter_cnt--;
  7709. break;
  7710. case IP_USER_FLOW:
  7711. switch (filter->ip4_proto) {
  7712. case IPPROTO_TCP:
  7713. pf->fd_tcp4_filter_cnt--;
  7714. break;
  7715. case IPPROTO_UDP:
  7716. pf->fd_udp4_filter_cnt--;
  7717. break;
  7718. case IPPROTO_SCTP:
  7719. pf->fd_sctp4_filter_cnt--;
  7720. break;
  7721. case IPPROTO_IP:
  7722. pf->fd_ip4_filter_cnt--;
  7723. break;
  7724. }
  7725. break;
  7726. }
  7727. /* Remove the filter from the list and free memory */
  7728. hlist_del(&filter->fdir_node);
  7729. kfree(filter);
  7730. }
  7731. /**
  7732. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7733. * @pf: board private structure
  7734. **/
  7735. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7736. {
  7737. struct i40e_fdir_filter *filter;
  7738. u32 fcnt_prog, fcnt_avail;
  7739. struct hlist_node *node;
  7740. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7741. return;
  7742. /* Check if we have enough room to re-enable FDir SB capability. */
  7743. fcnt_prog = i40e_get_global_fd_count(pf);
  7744. fcnt_avail = pf->fdir_pf_filter_count;
  7745. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7746. (pf->fd_add_err == 0) ||
  7747. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7748. i40e_reenable_fdir_sb(pf);
  7749. /* We should wait for even more space before re-enabling ATR.
  7750. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7751. * rules active.
  7752. */
  7753. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7754. (pf->fd_tcp4_filter_cnt == 0))
  7755. i40e_reenable_fdir_atr(pf);
  7756. /* if hw had a problem adding a filter, delete it */
  7757. if (pf->fd_inv > 0) {
  7758. hlist_for_each_entry_safe(filter, node,
  7759. &pf->fdir_filter_list, fdir_node)
  7760. if (filter->fd_id == pf->fd_inv)
  7761. i40e_delete_invalid_filter(pf, filter);
  7762. }
  7763. }
  7764. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7765. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7766. /**
  7767. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7768. * @pf: board private structure
  7769. **/
  7770. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7771. {
  7772. unsigned long min_flush_time;
  7773. int flush_wait_retry = 50;
  7774. bool disable_atr = false;
  7775. int fd_room;
  7776. int reg;
  7777. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7778. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7779. return;
  7780. /* If the flush is happening too quick and we have mostly SB rules we
  7781. * should not re-enable ATR for some time.
  7782. */
  7783. min_flush_time = pf->fd_flush_timestamp +
  7784. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7785. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7786. if (!(time_after(jiffies, min_flush_time)) &&
  7787. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7788. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7789. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7790. disable_atr = true;
  7791. }
  7792. pf->fd_flush_timestamp = jiffies;
  7793. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7794. /* flush all filters */
  7795. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7796. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7797. i40e_flush(&pf->hw);
  7798. pf->fd_flush_cnt++;
  7799. pf->fd_add_err = 0;
  7800. do {
  7801. /* Check FD flush status every 5-6msec */
  7802. usleep_range(5000, 6000);
  7803. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7804. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7805. break;
  7806. } while (flush_wait_retry--);
  7807. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7808. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7809. } else {
  7810. /* replay sideband filters */
  7811. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7812. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7813. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7814. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7815. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7816. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7817. }
  7818. }
  7819. /**
  7820. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7821. * @pf: board private structure
  7822. **/
  7823. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7824. {
  7825. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7826. }
  7827. /* We can see up to 256 filter programming desc in transit if the filters are
  7828. * being applied really fast; before we see the first
  7829. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7830. * reacting will make sure we don't cause flush too often.
  7831. */
  7832. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7833. /**
  7834. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7835. * @pf: board private structure
  7836. **/
  7837. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7838. {
  7839. /* if interface is down do nothing */
  7840. if (test_bit(__I40E_DOWN, pf->state))
  7841. return;
  7842. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7843. i40e_fdir_flush_and_replay(pf);
  7844. i40e_fdir_check_and_reenable(pf);
  7845. }
  7846. /**
  7847. * i40e_vsi_link_event - notify VSI of a link event
  7848. * @vsi: vsi to be notified
  7849. * @link_up: link up or down
  7850. **/
  7851. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7852. {
  7853. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7854. return;
  7855. switch (vsi->type) {
  7856. case I40E_VSI_MAIN:
  7857. if (!vsi->netdev || !vsi->netdev_registered)
  7858. break;
  7859. if (link_up) {
  7860. netif_carrier_on(vsi->netdev);
  7861. netif_tx_wake_all_queues(vsi->netdev);
  7862. } else {
  7863. netif_carrier_off(vsi->netdev);
  7864. netif_tx_stop_all_queues(vsi->netdev);
  7865. }
  7866. break;
  7867. case I40E_VSI_SRIOV:
  7868. case I40E_VSI_VMDQ2:
  7869. case I40E_VSI_CTRL:
  7870. case I40E_VSI_IWARP:
  7871. case I40E_VSI_MIRROR:
  7872. default:
  7873. /* there is no notification for other VSIs */
  7874. break;
  7875. }
  7876. }
  7877. /**
  7878. * i40e_veb_link_event - notify elements on the veb of a link event
  7879. * @veb: veb to be notified
  7880. * @link_up: link up or down
  7881. **/
  7882. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7883. {
  7884. struct i40e_pf *pf;
  7885. int i;
  7886. if (!veb || !veb->pf)
  7887. return;
  7888. pf = veb->pf;
  7889. /* depth first... */
  7890. for (i = 0; i < I40E_MAX_VEB; i++)
  7891. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7892. i40e_veb_link_event(pf->veb[i], link_up);
  7893. /* ... now the local VSIs */
  7894. for (i = 0; i < pf->num_alloc_vsi; i++)
  7895. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7896. i40e_vsi_link_event(pf->vsi[i], link_up);
  7897. }
  7898. /**
  7899. * i40e_link_event - Update netif_carrier status
  7900. * @pf: board private structure
  7901. **/
  7902. static void i40e_link_event(struct i40e_pf *pf)
  7903. {
  7904. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7905. u8 new_link_speed, old_link_speed;
  7906. i40e_status status;
  7907. bool new_link, old_link;
  7908. /* set this to force the get_link_status call to refresh state */
  7909. pf->hw.phy.get_link_info = true;
  7910. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7911. status = i40e_get_link_status(&pf->hw, &new_link);
  7912. /* On success, disable temp link polling */
  7913. if (status == I40E_SUCCESS) {
  7914. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7915. } else {
  7916. /* Enable link polling temporarily until i40e_get_link_status
  7917. * returns I40E_SUCCESS
  7918. */
  7919. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7920. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7921. status);
  7922. return;
  7923. }
  7924. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7925. new_link_speed = pf->hw.phy.link_info.link_speed;
  7926. if (new_link == old_link &&
  7927. new_link_speed == old_link_speed &&
  7928. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7929. new_link == netif_carrier_ok(vsi->netdev)))
  7930. return;
  7931. i40e_print_link_message(vsi, new_link);
  7932. /* Notify the base of the switch tree connected to
  7933. * the link. Floating VEBs are not notified.
  7934. */
  7935. if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
  7936. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7937. else
  7938. i40e_vsi_link_event(vsi, new_link);
  7939. if (pf->vf)
  7940. i40e_vc_notify_link_state(pf);
  7941. if (pf->flags & I40E_FLAG_PTP)
  7942. i40e_ptp_set_increment(pf);
  7943. }
  7944. /**
  7945. * i40e_watchdog_subtask - periodic checks not using event driven response
  7946. * @pf: board private structure
  7947. **/
  7948. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7949. {
  7950. int i;
  7951. /* if interface is down do nothing */
  7952. if (test_bit(__I40E_DOWN, pf->state) ||
  7953. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7954. return;
  7955. /* make sure we don't do these things too often */
  7956. if (time_before(jiffies, (pf->service_timer_previous +
  7957. pf->service_timer_period)))
  7958. return;
  7959. pf->service_timer_previous = jiffies;
  7960. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7961. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7962. i40e_link_event(pf);
  7963. /* Update the stats for active netdevs so the network stack
  7964. * can look at updated numbers whenever it cares to
  7965. */
  7966. for (i = 0; i < pf->num_alloc_vsi; i++)
  7967. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7968. i40e_update_stats(pf->vsi[i]);
  7969. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7970. /* Update the stats for the active switching components */
  7971. for (i = 0; i < I40E_MAX_VEB; i++)
  7972. if (pf->veb[i])
  7973. i40e_update_veb_stats(pf->veb[i]);
  7974. }
  7975. i40e_ptp_rx_hang(pf);
  7976. i40e_ptp_tx_hang(pf);
  7977. }
  7978. /**
  7979. * i40e_reset_subtask - Set up for resetting the device and driver
  7980. * @pf: board private structure
  7981. **/
  7982. static void i40e_reset_subtask(struct i40e_pf *pf)
  7983. {
  7984. u32 reset_flags = 0;
  7985. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7986. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7987. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7988. }
  7989. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7990. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7991. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7992. }
  7993. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7994. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7995. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7996. }
  7997. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7998. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7999. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  8000. }
  8001. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  8002. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  8003. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  8004. }
  8005. /* If there's a recovery already waiting, it takes
  8006. * precedence before starting a new reset sequence.
  8007. */
  8008. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  8009. i40e_prep_for_reset(pf, false);
  8010. i40e_reset(pf);
  8011. i40e_rebuild(pf, false, false);
  8012. }
  8013. /* If we're already down or resetting, just bail */
  8014. if (reset_flags &&
  8015. !test_bit(__I40E_DOWN, pf->state) &&
  8016. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  8017. i40e_do_reset(pf, reset_flags, false);
  8018. }
  8019. }
  8020. /**
  8021. * i40e_handle_link_event - Handle link event
  8022. * @pf: board private structure
  8023. * @e: event info posted on ARQ
  8024. **/
  8025. static void i40e_handle_link_event(struct i40e_pf *pf,
  8026. struct i40e_arq_event_info *e)
  8027. {
  8028. struct i40e_aqc_get_link_status *status =
  8029. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  8030. /* Do a new status request to re-enable LSE reporting
  8031. * and load new status information into the hw struct
  8032. * This completely ignores any state information
  8033. * in the ARQ event info, instead choosing to always
  8034. * issue the AQ update link status command.
  8035. */
  8036. i40e_link_event(pf);
  8037. /* Check if module meets thermal requirements */
  8038. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  8039. dev_err(&pf->pdev->dev,
  8040. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  8041. dev_err(&pf->pdev->dev,
  8042. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  8043. } else {
  8044. /* check for unqualified module, if link is down, suppress
  8045. * the message if link was forced to be down.
  8046. */
  8047. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  8048. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  8049. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  8050. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  8051. dev_err(&pf->pdev->dev,
  8052. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  8053. dev_err(&pf->pdev->dev,
  8054. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  8055. }
  8056. }
  8057. }
  8058. /**
  8059. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  8060. * @pf: board private structure
  8061. **/
  8062. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  8063. {
  8064. struct i40e_arq_event_info event;
  8065. struct i40e_hw *hw = &pf->hw;
  8066. u16 pending, i = 0;
  8067. i40e_status ret;
  8068. u16 opcode;
  8069. u32 oldval;
  8070. u32 val;
  8071. /* Do not run clean AQ when PF reset fails */
  8072. if (test_bit(__I40E_RESET_FAILED, pf->state))
  8073. return;
  8074. /* check for error indications */
  8075. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  8076. oldval = val;
  8077. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  8078. if (hw->debug_mask & I40E_DEBUG_AQ)
  8079. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  8080. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  8081. }
  8082. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  8083. if (hw->debug_mask & I40E_DEBUG_AQ)
  8084. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  8085. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  8086. pf->arq_overflows++;
  8087. }
  8088. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  8089. if (hw->debug_mask & I40E_DEBUG_AQ)
  8090. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  8091. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  8092. }
  8093. if (oldval != val)
  8094. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  8095. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  8096. oldval = val;
  8097. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  8098. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8099. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  8100. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  8101. }
  8102. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  8103. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8104. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  8105. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  8106. }
  8107. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  8108. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8109. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  8110. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  8111. }
  8112. if (oldval != val)
  8113. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  8114. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  8115. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  8116. if (!event.msg_buf)
  8117. return;
  8118. do {
  8119. ret = i40e_clean_arq_element(hw, &event, &pending);
  8120. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  8121. break;
  8122. else if (ret) {
  8123. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  8124. break;
  8125. }
  8126. opcode = le16_to_cpu(event.desc.opcode);
  8127. switch (opcode) {
  8128. case i40e_aqc_opc_get_link_status:
  8129. i40e_handle_link_event(pf, &event);
  8130. break;
  8131. case i40e_aqc_opc_send_msg_to_pf:
  8132. ret = i40e_vc_process_vf_msg(pf,
  8133. le16_to_cpu(event.desc.retval),
  8134. le32_to_cpu(event.desc.cookie_high),
  8135. le32_to_cpu(event.desc.cookie_low),
  8136. event.msg_buf,
  8137. event.msg_len);
  8138. break;
  8139. case i40e_aqc_opc_lldp_update_mib:
  8140. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  8141. #ifdef CONFIG_I40E_DCB
  8142. rtnl_lock();
  8143. ret = i40e_handle_lldp_event(pf, &event);
  8144. rtnl_unlock();
  8145. #endif /* CONFIG_I40E_DCB */
  8146. break;
  8147. case i40e_aqc_opc_event_lan_overflow:
  8148. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  8149. i40e_handle_lan_overflow_event(pf, &event);
  8150. break;
  8151. case i40e_aqc_opc_send_msg_to_peer:
  8152. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  8153. break;
  8154. case i40e_aqc_opc_nvm_erase:
  8155. case i40e_aqc_opc_nvm_update:
  8156. case i40e_aqc_opc_oem_post_update:
  8157. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  8158. "ARQ NVM operation 0x%04x completed\n",
  8159. opcode);
  8160. break;
  8161. default:
  8162. dev_info(&pf->pdev->dev,
  8163. "ARQ: Unknown event 0x%04x ignored\n",
  8164. opcode);
  8165. break;
  8166. }
  8167. } while (i++ < pf->adminq_work_limit);
  8168. if (i < pf->adminq_work_limit)
  8169. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  8170. /* re-enable Admin queue interrupt cause */
  8171. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  8172. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  8173. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  8174. i40e_flush(hw);
  8175. kfree(event.msg_buf);
  8176. }
  8177. /**
  8178. * i40e_verify_eeprom - make sure eeprom is good to use
  8179. * @pf: board private structure
  8180. **/
  8181. static void i40e_verify_eeprom(struct i40e_pf *pf)
  8182. {
  8183. int err;
  8184. err = i40e_diag_eeprom_test(&pf->hw);
  8185. if (err) {
  8186. /* retry in case of garbage read */
  8187. err = i40e_diag_eeprom_test(&pf->hw);
  8188. if (err) {
  8189. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  8190. err);
  8191. set_bit(__I40E_BAD_EEPROM, pf->state);
  8192. }
  8193. }
  8194. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  8195. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  8196. clear_bit(__I40E_BAD_EEPROM, pf->state);
  8197. }
  8198. }
  8199. /**
  8200. * i40e_enable_pf_switch_lb
  8201. * @pf: pointer to the PF structure
  8202. *
  8203. * enable switch loop back or die - no point in a return value
  8204. **/
  8205. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  8206. {
  8207. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8208. struct i40e_vsi_context ctxt;
  8209. int ret;
  8210. ctxt.seid = pf->main_vsi_seid;
  8211. ctxt.pf_num = pf->hw.pf_id;
  8212. ctxt.vf_num = 0;
  8213. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8214. if (ret) {
  8215. dev_info(&pf->pdev->dev,
  8216. "couldn't get PF vsi config, err %s aq_err %s\n",
  8217. i40e_stat_str(&pf->hw, ret),
  8218. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8219. return;
  8220. }
  8221. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8222. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8223. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8224. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  8225. if (ret) {
  8226. dev_info(&pf->pdev->dev,
  8227. "update vsi switch failed, err %s aq_err %s\n",
  8228. i40e_stat_str(&pf->hw, ret),
  8229. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8230. }
  8231. }
  8232. /**
  8233. * i40e_disable_pf_switch_lb
  8234. * @pf: pointer to the PF structure
  8235. *
  8236. * disable switch loop back or die - no point in a return value
  8237. **/
  8238. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  8239. {
  8240. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8241. struct i40e_vsi_context ctxt;
  8242. int ret;
  8243. ctxt.seid = pf->main_vsi_seid;
  8244. ctxt.pf_num = pf->hw.pf_id;
  8245. ctxt.vf_num = 0;
  8246. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8247. if (ret) {
  8248. dev_info(&pf->pdev->dev,
  8249. "couldn't get PF vsi config, err %s aq_err %s\n",
  8250. i40e_stat_str(&pf->hw, ret),
  8251. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8252. return;
  8253. }
  8254. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8255. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8256. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8257. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  8258. if (ret) {
  8259. dev_info(&pf->pdev->dev,
  8260. "update vsi switch failed, err %s aq_err %s\n",
  8261. i40e_stat_str(&pf->hw, ret),
  8262. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8263. }
  8264. }
  8265. /**
  8266. * i40e_config_bridge_mode - Configure the HW bridge mode
  8267. * @veb: pointer to the bridge instance
  8268. *
  8269. * Configure the loop back mode for the LAN VSI that is downlink to the
  8270. * specified HW bridge instance. It is expected this function is called
  8271. * when a new HW bridge is instantiated.
  8272. **/
  8273. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  8274. {
  8275. struct i40e_pf *pf = veb->pf;
  8276. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  8277. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  8278. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  8279. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  8280. i40e_disable_pf_switch_lb(pf);
  8281. else
  8282. i40e_enable_pf_switch_lb(pf);
  8283. }
  8284. /**
  8285. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  8286. * @veb: pointer to the VEB instance
  8287. *
  8288. * This is a recursive function that first builds the attached VSIs then
  8289. * recurses in to build the next layer of VEB. We track the connections
  8290. * through our own index numbers because the seid's from the HW could
  8291. * change across the reset.
  8292. **/
  8293. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  8294. {
  8295. struct i40e_vsi *ctl_vsi = NULL;
  8296. struct i40e_pf *pf = veb->pf;
  8297. int v, veb_idx;
  8298. int ret;
  8299. /* build VSI that owns this VEB, temporarily attached to base VEB */
  8300. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  8301. if (pf->vsi[v] &&
  8302. pf->vsi[v]->veb_idx == veb->idx &&
  8303. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8304. ctl_vsi = pf->vsi[v];
  8305. break;
  8306. }
  8307. }
  8308. if (!ctl_vsi) {
  8309. dev_info(&pf->pdev->dev,
  8310. "missing owner VSI for veb_idx %d\n", veb->idx);
  8311. ret = -ENOENT;
  8312. goto end_reconstitute;
  8313. }
  8314. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  8315. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  8316. ret = i40e_add_vsi(ctl_vsi);
  8317. if (ret) {
  8318. dev_info(&pf->pdev->dev,
  8319. "rebuild of veb_idx %d owner VSI failed: %d\n",
  8320. veb->idx, ret);
  8321. goto end_reconstitute;
  8322. }
  8323. i40e_vsi_reset_stats(ctl_vsi);
  8324. /* create the VEB in the switch and move the VSI onto the VEB */
  8325. ret = i40e_add_veb(veb, ctl_vsi);
  8326. if (ret)
  8327. goto end_reconstitute;
  8328. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  8329. veb->bridge_mode = BRIDGE_MODE_VEB;
  8330. else
  8331. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8332. i40e_config_bridge_mode(veb);
  8333. /* create the remaining VSIs attached to this VEB */
  8334. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8335. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  8336. continue;
  8337. if (pf->vsi[v]->veb_idx == veb->idx) {
  8338. struct i40e_vsi *vsi = pf->vsi[v];
  8339. vsi->uplink_seid = veb->seid;
  8340. ret = i40e_add_vsi(vsi);
  8341. if (ret) {
  8342. dev_info(&pf->pdev->dev,
  8343. "rebuild of vsi_idx %d failed: %d\n",
  8344. v, ret);
  8345. goto end_reconstitute;
  8346. }
  8347. i40e_vsi_reset_stats(vsi);
  8348. }
  8349. }
  8350. /* create any VEBs attached to this VEB - RECURSION */
  8351. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  8352. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  8353. pf->veb[veb_idx]->uplink_seid = veb->seid;
  8354. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  8355. if (ret)
  8356. break;
  8357. }
  8358. }
  8359. end_reconstitute:
  8360. return ret;
  8361. }
  8362. /**
  8363. * i40e_get_capabilities - get info about the HW
  8364. * @pf: the PF struct
  8365. **/
  8366. static int i40e_get_capabilities(struct i40e_pf *pf,
  8367. enum i40e_admin_queue_opc list_type)
  8368. {
  8369. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  8370. u16 data_size;
  8371. int buf_len;
  8372. int err;
  8373. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  8374. do {
  8375. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  8376. if (!cap_buf)
  8377. return -ENOMEM;
  8378. /* this loads the data into the hw struct for us */
  8379. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  8380. &data_size, list_type,
  8381. NULL);
  8382. /* data loaded, buffer no longer needed */
  8383. kfree(cap_buf);
  8384. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  8385. /* retry with a larger buffer */
  8386. buf_len = data_size;
  8387. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  8388. dev_info(&pf->pdev->dev,
  8389. "capability discovery failed, err %s aq_err %s\n",
  8390. i40e_stat_str(&pf->hw, err),
  8391. i40e_aq_str(&pf->hw,
  8392. pf->hw.aq.asq_last_status));
  8393. return -ENODEV;
  8394. }
  8395. } while (err);
  8396. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  8397. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  8398. dev_info(&pf->pdev->dev,
  8399. "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
  8400. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  8401. pf->hw.func_caps.num_msix_vectors,
  8402. pf->hw.func_caps.num_msix_vectors_vf,
  8403. pf->hw.func_caps.fd_filters_guaranteed,
  8404. pf->hw.func_caps.fd_filters_best_effort,
  8405. pf->hw.func_caps.num_tx_qp,
  8406. pf->hw.func_caps.num_vsis);
  8407. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  8408. dev_info(&pf->pdev->dev,
  8409. "switch_mode=0x%04x, function_valid=0x%08x\n",
  8410. pf->hw.dev_caps.switch_mode,
  8411. pf->hw.dev_caps.valid_functions);
  8412. dev_info(&pf->pdev->dev,
  8413. "SR-IOV=%d, num_vfs for all function=%u\n",
  8414. pf->hw.dev_caps.sr_iov_1_1,
  8415. pf->hw.dev_caps.num_vfs);
  8416. dev_info(&pf->pdev->dev,
  8417. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  8418. pf->hw.dev_caps.num_vsis,
  8419. pf->hw.dev_caps.num_rx_qp,
  8420. pf->hw.dev_caps.num_tx_qp);
  8421. }
  8422. }
  8423. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  8424. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  8425. + pf->hw.func_caps.num_vfs)
  8426. if (pf->hw.revision_id == 0 &&
  8427. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  8428. dev_info(&pf->pdev->dev,
  8429. "got num_vsis %d, setting num_vsis to %d\n",
  8430. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  8431. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  8432. }
  8433. }
  8434. return 0;
  8435. }
  8436. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  8437. /**
  8438. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  8439. * @pf: board private structure
  8440. **/
  8441. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  8442. {
  8443. struct i40e_vsi *vsi;
  8444. /* quick workaround for an NVM issue that leaves a critical register
  8445. * uninitialized
  8446. */
  8447. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  8448. static const u32 hkey[] = {
  8449. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  8450. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  8451. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  8452. 0x95b3a76d};
  8453. int i;
  8454. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  8455. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  8456. }
  8457. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  8458. return;
  8459. /* find existing VSI and see if it needs configuring */
  8460. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  8461. /* create a new VSI if none exists */
  8462. if (!vsi) {
  8463. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  8464. pf->vsi[pf->lan_vsi]->seid, 0);
  8465. if (!vsi) {
  8466. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  8467. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  8468. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  8469. return;
  8470. }
  8471. }
  8472. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  8473. }
  8474. /**
  8475. * i40e_fdir_teardown - release the Flow Director resources
  8476. * @pf: board private structure
  8477. **/
  8478. static void i40e_fdir_teardown(struct i40e_pf *pf)
  8479. {
  8480. struct i40e_vsi *vsi;
  8481. i40e_fdir_filter_exit(pf);
  8482. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  8483. if (vsi)
  8484. i40e_vsi_release(vsi);
  8485. }
  8486. /**
  8487. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  8488. * @vsi: PF main vsi
  8489. * @seid: seid of main or channel VSIs
  8490. *
  8491. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  8492. * existed before reset
  8493. **/
  8494. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  8495. {
  8496. struct i40e_cloud_filter *cfilter;
  8497. struct i40e_pf *pf = vsi->back;
  8498. struct hlist_node *node;
  8499. i40e_status ret;
  8500. /* Add cloud filters back if they exist */
  8501. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  8502. cloud_node) {
  8503. if (cfilter->seid != seid)
  8504. continue;
  8505. if (cfilter->dst_port)
  8506. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  8507. true);
  8508. else
  8509. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  8510. if (ret) {
  8511. dev_dbg(&pf->pdev->dev,
  8512. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  8513. i40e_stat_str(&pf->hw, ret),
  8514. i40e_aq_str(&pf->hw,
  8515. pf->hw.aq.asq_last_status));
  8516. return ret;
  8517. }
  8518. }
  8519. return 0;
  8520. }
  8521. /**
  8522. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  8523. * @vsi: PF main vsi
  8524. *
  8525. * Rebuilds channel VSIs if they existed before reset
  8526. **/
  8527. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  8528. {
  8529. struct i40e_channel *ch, *ch_tmp;
  8530. i40e_status ret;
  8531. if (list_empty(&vsi->ch_list))
  8532. return 0;
  8533. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8534. if (!ch->initialized)
  8535. break;
  8536. /* Proceed with creation of channel (VMDq2) VSI */
  8537. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8538. if (ret) {
  8539. dev_info(&vsi->back->pdev->dev,
  8540. "failed to rebuild channels using uplink_seid %u\n",
  8541. vsi->uplink_seid);
  8542. return ret;
  8543. }
  8544. /* Reconfigure TX queues using QTX_CTL register */
  8545. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8546. if (ret) {
  8547. dev_info(&vsi->back->pdev->dev,
  8548. "failed to configure TX rings for channel %u\n",
  8549. ch->seid);
  8550. return ret;
  8551. }
  8552. /* update 'next_base_queue' */
  8553. vsi->next_base_queue = vsi->next_base_queue +
  8554. ch->num_queue_pairs;
  8555. if (ch->max_tx_rate) {
  8556. u64 credits = ch->max_tx_rate;
  8557. if (i40e_set_bw_limit(vsi, ch->seid,
  8558. ch->max_tx_rate))
  8559. return -EINVAL;
  8560. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8561. dev_dbg(&vsi->back->pdev->dev,
  8562. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8563. ch->max_tx_rate,
  8564. credits,
  8565. ch->seid);
  8566. }
  8567. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8568. if (ret) {
  8569. dev_dbg(&vsi->back->pdev->dev,
  8570. "Failed to rebuild cloud filters for channel VSI %u\n",
  8571. ch->seid);
  8572. return ret;
  8573. }
  8574. }
  8575. return 0;
  8576. }
  8577. /**
  8578. * i40e_prep_for_reset - prep for the core to reset
  8579. * @pf: board private structure
  8580. * @lock_acquired: indicates whether or not the lock has been acquired
  8581. * before this function was called.
  8582. *
  8583. * Close up the VFs and other things in prep for PF Reset.
  8584. **/
  8585. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8586. {
  8587. struct i40e_hw *hw = &pf->hw;
  8588. i40e_status ret = 0;
  8589. u32 v;
  8590. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8591. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8592. return;
  8593. if (i40e_check_asq_alive(&pf->hw))
  8594. i40e_vc_notify_reset(pf);
  8595. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8596. /* quiesce the VSIs and their queues that are not already DOWN */
  8597. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8598. if (!lock_acquired)
  8599. rtnl_lock();
  8600. i40e_pf_quiesce_all_vsi(pf);
  8601. if (!lock_acquired)
  8602. rtnl_unlock();
  8603. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8604. if (pf->vsi[v])
  8605. pf->vsi[v]->seid = 0;
  8606. }
  8607. i40e_shutdown_adminq(&pf->hw);
  8608. /* call shutdown HMC */
  8609. if (hw->hmc.hmc_obj) {
  8610. ret = i40e_shutdown_lan_hmc(hw);
  8611. if (ret)
  8612. dev_warn(&pf->pdev->dev,
  8613. "shutdown_lan_hmc failed: %d\n", ret);
  8614. }
  8615. /* Save the current PTP time so that we can restore the time after the
  8616. * reset completes.
  8617. */
  8618. i40e_ptp_save_hw_time(pf);
  8619. }
  8620. /**
  8621. * i40e_send_version - update firmware with driver version
  8622. * @pf: PF struct
  8623. */
  8624. static void i40e_send_version(struct i40e_pf *pf)
  8625. {
  8626. struct i40e_driver_version dv;
  8627. dv.major_version = DRV_VERSION_MAJOR;
  8628. dv.minor_version = DRV_VERSION_MINOR;
  8629. dv.build_version = DRV_VERSION_BUILD;
  8630. dv.subbuild_version = 0;
  8631. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8632. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8633. }
  8634. /**
  8635. * i40e_get_oem_version - get OEM specific version information
  8636. * @hw: pointer to the hardware structure
  8637. **/
  8638. static void i40e_get_oem_version(struct i40e_hw *hw)
  8639. {
  8640. u16 block_offset = 0xffff;
  8641. u16 block_length = 0;
  8642. u16 capabilities = 0;
  8643. u16 gen_snap = 0;
  8644. u16 release = 0;
  8645. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8646. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8647. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8648. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8649. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8650. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8651. #define I40E_NVM_OEM_LENGTH 3
  8652. /* Check if pointer to OEM version block is valid. */
  8653. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8654. if (block_offset == 0xffff)
  8655. return;
  8656. /* Check if OEM version block has correct length. */
  8657. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8658. &block_length);
  8659. if (block_length < I40E_NVM_OEM_LENGTH)
  8660. return;
  8661. /* Check if OEM version format is as expected. */
  8662. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8663. &capabilities);
  8664. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8665. return;
  8666. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8667. &gen_snap);
  8668. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8669. &release);
  8670. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8671. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8672. }
  8673. /**
  8674. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8675. * @pf: board private structure
  8676. **/
  8677. static int i40e_reset(struct i40e_pf *pf)
  8678. {
  8679. struct i40e_hw *hw = &pf->hw;
  8680. i40e_status ret;
  8681. ret = i40e_pf_reset(hw);
  8682. if (ret) {
  8683. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8684. set_bit(__I40E_RESET_FAILED, pf->state);
  8685. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8686. } else {
  8687. pf->pfr_count++;
  8688. }
  8689. return ret;
  8690. }
  8691. /**
  8692. * i40e_rebuild - rebuild using a saved config
  8693. * @pf: board private structure
  8694. * @reinit: if the Main VSI needs to re-initialized.
  8695. * @lock_acquired: indicates whether or not the lock has been acquired
  8696. * before this function was called.
  8697. **/
  8698. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8699. {
  8700. int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
  8701. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8702. struct i40e_hw *hw = &pf->hw;
  8703. u8 set_fc_aq_fail = 0;
  8704. i40e_status ret;
  8705. u32 val;
  8706. int v;
  8707. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8708. i40e_check_recovery_mode(pf)) {
  8709. i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
  8710. }
  8711. if (test_bit(__I40E_DOWN, pf->state) &&
  8712. !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
  8713. !old_recovery_mode_bit)
  8714. goto clear_recovery;
  8715. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8716. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8717. ret = i40e_init_adminq(&pf->hw);
  8718. if (ret) {
  8719. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8720. i40e_stat_str(&pf->hw, ret),
  8721. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8722. goto clear_recovery;
  8723. }
  8724. i40e_get_oem_version(&pf->hw);
  8725. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8726. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8727. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8728. /* The following delay is necessary for 4.33 firmware and older
  8729. * to recover after EMP reset. 200 ms should suffice but we
  8730. * put here 300 ms to be sure that FW is ready to operate
  8731. * after reset.
  8732. */
  8733. mdelay(300);
  8734. }
  8735. /* re-verify the eeprom if we just had an EMP reset */
  8736. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8737. i40e_verify_eeprom(pf);
  8738. /* if we are going out of or into recovery mode we have to act
  8739. * accordingly with regard to resources initialization
  8740. * and deinitialization
  8741. */
  8742. if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
  8743. old_recovery_mode_bit) {
  8744. if (i40e_get_capabilities(pf,
  8745. i40e_aqc_opc_list_func_capabilities))
  8746. goto end_unlock;
  8747. if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  8748. /* we're staying in recovery mode so we'll reinitialize
  8749. * misc vector here
  8750. */
  8751. if (i40e_setup_misc_vector_for_recovery_mode(pf))
  8752. goto end_unlock;
  8753. } else {
  8754. if (!lock_acquired)
  8755. rtnl_lock();
  8756. /* we're going out of recovery mode so we'll free
  8757. * the IRQ allocated specifically for recovery mode
  8758. * and restore the interrupt scheme
  8759. */
  8760. free_irq(pf->pdev->irq, pf);
  8761. i40e_clear_interrupt_scheme(pf);
  8762. if (i40e_restore_interrupt_scheme(pf))
  8763. goto end_unlock;
  8764. }
  8765. /* tell the firmware that we're starting */
  8766. i40e_send_version(pf);
  8767. /* bail out in case recovery mode was detected, as there is
  8768. * no need for further configuration.
  8769. */
  8770. goto end_unlock;
  8771. }
  8772. i40e_clear_pxe_mode(hw);
  8773. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8774. if (ret)
  8775. goto end_core_reset;
  8776. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8777. hw->func_caps.num_rx_qp, 0, 0);
  8778. if (ret) {
  8779. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8780. goto end_core_reset;
  8781. }
  8782. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8783. if (ret) {
  8784. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8785. goto end_core_reset;
  8786. }
  8787. /* Enable FW to write a default DCB config on link-up */
  8788. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8789. #ifdef CONFIG_I40E_DCB
  8790. ret = i40e_init_pf_dcb(pf);
  8791. if (ret) {
  8792. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8793. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8794. /* Continue without DCB enabled */
  8795. }
  8796. #endif /* CONFIG_I40E_DCB */
  8797. /* do basic switch setup */
  8798. if (!lock_acquired)
  8799. rtnl_lock();
  8800. ret = i40e_setup_pf_switch(pf, reinit);
  8801. if (ret)
  8802. goto end_unlock;
  8803. /* The driver only wants link up/down and module qualification
  8804. * reports from firmware. Note the negative logic.
  8805. */
  8806. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8807. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8808. I40E_AQ_EVENT_MEDIA_NA |
  8809. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8810. if (ret)
  8811. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8812. i40e_stat_str(&pf->hw, ret),
  8813. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8814. /* make sure our flow control settings are restored */
  8815. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8816. if (ret)
  8817. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8818. i40e_stat_str(&pf->hw, ret),
  8819. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8820. /* Rebuild the VSIs and VEBs that existed before reset.
  8821. * They are still in our local switch element arrays, so only
  8822. * need to rebuild the switch model in the HW.
  8823. *
  8824. * If there were VEBs but the reconstitution failed, we'll try
  8825. * try to recover minimal use by getting the basic PF VSI working.
  8826. */
  8827. if (vsi->uplink_seid != pf->mac_seid) {
  8828. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8829. /* find the one VEB connected to the MAC, and find orphans */
  8830. for (v = 0; v < I40E_MAX_VEB; v++) {
  8831. if (!pf->veb[v])
  8832. continue;
  8833. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8834. pf->veb[v]->uplink_seid == 0) {
  8835. ret = i40e_reconstitute_veb(pf->veb[v]);
  8836. if (!ret)
  8837. continue;
  8838. /* If Main VEB failed, we're in deep doodoo,
  8839. * so give up rebuilding the switch and set up
  8840. * for minimal rebuild of PF VSI.
  8841. * If orphan failed, we'll report the error
  8842. * but try to keep going.
  8843. */
  8844. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8845. dev_info(&pf->pdev->dev,
  8846. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8847. ret);
  8848. vsi->uplink_seid = pf->mac_seid;
  8849. break;
  8850. } else if (pf->veb[v]->uplink_seid == 0) {
  8851. dev_info(&pf->pdev->dev,
  8852. "rebuild of orphan VEB failed: %d\n",
  8853. ret);
  8854. }
  8855. }
  8856. }
  8857. }
  8858. if (vsi->uplink_seid == pf->mac_seid) {
  8859. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8860. /* no VEB, so rebuild only the Main VSI */
  8861. ret = i40e_add_vsi(vsi);
  8862. if (ret) {
  8863. dev_info(&pf->pdev->dev,
  8864. "rebuild of Main VSI failed: %d\n", ret);
  8865. goto end_unlock;
  8866. }
  8867. }
  8868. if (vsi->mqprio_qopt.max_rate[0]) {
  8869. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8870. u64 credits = 0;
  8871. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8872. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8873. if (ret)
  8874. goto end_unlock;
  8875. credits = max_tx_rate;
  8876. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8877. dev_dbg(&vsi->back->pdev->dev,
  8878. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8879. max_tx_rate,
  8880. credits,
  8881. vsi->seid);
  8882. }
  8883. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8884. if (ret)
  8885. goto end_unlock;
  8886. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8887. * for this main VSI if they exist
  8888. */
  8889. ret = i40e_rebuild_channels(vsi);
  8890. if (ret)
  8891. goto end_unlock;
  8892. /* Reconfigure hardware for allowing smaller MSS in the case
  8893. * of TSO, so that we avoid the MDD being fired and causing
  8894. * a reset in the case of small MSS+TSO.
  8895. */
  8896. #define I40E_REG_MSS 0x000E64DC
  8897. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8898. #define I40E_64BYTE_MSS 0x400000
  8899. val = rd32(hw, I40E_REG_MSS);
  8900. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8901. val &= ~I40E_REG_MSS_MIN_MASK;
  8902. val |= I40E_64BYTE_MSS;
  8903. wr32(hw, I40E_REG_MSS, val);
  8904. }
  8905. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8906. msleep(75);
  8907. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8908. if (ret)
  8909. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8910. i40e_stat_str(&pf->hw, ret),
  8911. i40e_aq_str(&pf->hw,
  8912. pf->hw.aq.asq_last_status));
  8913. }
  8914. /* reinit the misc interrupt */
  8915. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8916. ret = i40e_setup_misc_vector(pf);
  8917. /* Add a filter to drop all Flow control frames from any VSI from being
  8918. * transmitted. By doing so we stop a malicious VF from sending out
  8919. * PAUSE or PFC frames and potentially controlling traffic for other
  8920. * PF/VF VSIs.
  8921. * The FW can still send Flow control frames if enabled.
  8922. */
  8923. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8924. pf->main_vsi_seid);
  8925. /* restart the VSIs that were rebuilt and running before the reset */
  8926. i40e_pf_unquiesce_all_vsi(pf);
  8927. /* Release the RTNL lock before we start resetting VFs */
  8928. if (!lock_acquired)
  8929. rtnl_unlock();
  8930. /* Restore promiscuous settings */
  8931. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8932. if (ret)
  8933. dev_warn(&pf->pdev->dev,
  8934. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8935. pf->cur_promisc ? "on" : "off",
  8936. i40e_stat_str(&pf->hw, ret),
  8937. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8938. i40e_reset_all_vfs(pf, true);
  8939. /* tell the firmware that we're starting */
  8940. i40e_send_version(pf);
  8941. /* We've already released the lock, so don't do it again */
  8942. goto end_core_reset;
  8943. end_unlock:
  8944. if (!lock_acquired)
  8945. rtnl_unlock();
  8946. end_core_reset:
  8947. clear_bit(__I40E_RESET_FAILED, pf->state);
  8948. clear_recovery:
  8949. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8950. clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
  8951. }
  8952. /**
  8953. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8954. * @pf: board private structure
  8955. * @reinit: if the Main VSI needs to re-initialized.
  8956. * @lock_acquired: indicates whether or not the lock has been acquired
  8957. * before this function was called.
  8958. **/
  8959. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8960. bool lock_acquired)
  8961. {
  8962. int ret;
  8963. /* Now we wait for GRST to settle out.
  8964. * We don't have to delete the VEBs or VSIs from the hw switch
  8965. * because the reset will make them disappear.
  8966. */
  8967. ret = i40e_reset(pf);
  8968. if (!ret)
  8969. i40e_rebuild(pf, reinit, lock_acquired);
  8970. }
  8971. /**
  8972. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8973. * @pf: board private structure
  8974. *
  8975. * Close up the VFs and other things in prep for a Core Reset,
  8976. * then get ready to rebuild the world.
  8977. * @lock_acquired: indicates whether or not the lock has been acquired
  8978. * before this function was called.
  8979. **/
  8980. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8981. {
  8982. i40e_prep_for_reset(pf, lock_acquired);
  8983. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8984. }
  8985. /**
  8986. * i40e_handle_mdd_event
  8987. * @pf: pointer to the PF structure
  8988. *
  8989. * Called from the MDD irq handler to identify possibly malicious vfs
  8990. **/
  8991. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8992. {
  8993. struct i40e_hw *hw = &pf->hw;
  8994. bool mdd_detected = false;
  8995. struct i40e_vf *vf;
  8996. u32 reg;
  8997. int i;
  8998. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8999. return;
  9000. /* find what triggered the MDD event */
  9001. reg = rd32(hw, I40E_GL_MDET_TX);
  9002. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  9003. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  9004. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  9005. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  9006. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  9007. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  9008. I40E_GL_MDET_TX_EVENT_SHIFT;
  9009. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  9010. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  9011. pf->hw.func_caps.base_queue;
  9012. if (netif_msg_tx_err(pf))
  9013. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  9014. event, queue, pf_num, vf_num);
  9015. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  9016. mdd_detected = true;
  9017. }
  9018. reg = rd32(hw, I40E_GL_MDET_RX);
  9019. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  9020. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  9021. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  9022. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  9023. I40E_GL_MDET_RX_EVENT_SHIFT;
  9024. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  9025. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  9026. pf->hw.func_caps.base_queue;
  9027. if (netif_msg_rx_err(pf))
  9028. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  9029. event, queue, func);
  9030. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  9031. mdd_detected = true;
  9032. }
  9033. if (mdd_detected) {
  9034. reg = rd32(hw, I40E_PF_MDET_TX);
  9035. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  9036. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  9037. dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
  9038. }
  9039. reg = rd32(hw, I40E_PF_MDET_RX);
  9040. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  9041. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  9042. dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
  9043. }
  9044. }
  9045. /* see if one of the VFs needs its hand slapped */
  9046. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  9047. vf = &(pf->vf[i]);
  9048. reg = rd32(hw, I40E_VP_MDET_TX(i));
  9049. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  9050. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  9051. vf->num_mdd_events++;
  9052. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  9053. i);
  9054. dev_info(&pf->pdev->dev,
  9055. "Use PF Control I/F to re-enable the VF\n");
  9056. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  9057. }
  9058. reg = rd32(hw, I40E_VP_MDET_RX(i));
  9059. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  9060. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  9061. vf->num_mdd_events++;
  9062. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  9063. i);
  9064. dev_info(&pf->pdev->dev,
  9065. "Use PF Control I/F to re-enable the VF\n");
  9066. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  9067. }
  9068. }
  9069. /* re-enable mdd interrupt cause */
  9070. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  9071. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  9072. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  9073. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  9074. i40e_flush(hw);
  9075. }
  9076. static const char *i40e_tunnel_name(u8 type)
  9077. {
  9078. switch (type) {
  9079. case UDP_TUNNEL_TYPE_VXLAN:
  9080. return "vxlan";
  9081. case UDP_TUNNEL_TYPE_GENEVE:
  9082. return "geneve";
  9083. default:
  9084. return "unknown";
  9085. }
  9086. }
  9087. /**
  9088. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  9089. * @pf: board private structure
  9090. **/
  9091. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  9092. {
  9093. int i;
  9094. /* loop through and set pending bit for all active UDP filters */
  9095. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  9096. if (pf->udp_ports[i].port)
  9097. pf->pending_udp_bitmap |= BIT_ULL(i);
  9098. }
  9099. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  9100. }
  9101. /**
  9102. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  9103. * @pf: board private structure
  9104. **/
  9105. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  9106. {
  9107. struct i40e_hw *hw = &pf->hw;
  9108. u8 filter_index, type;
  9109. u16 port;
  9110. int i;
  9111. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  9112. return;
  9113. /* acquire RTNL to maintain state of flags and port requests */
  9114. rtnl_lock();
  9115. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  9116. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  9117. struct i40e_udp_port_config *udp_port;
  9118. i40e_status ret = 0;
  9119. udp_port = &pf->udp_ports[i];
  9120. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  9121. port = READ_ONCE(udp_port->port);
  9122. type = READ_ONCE(udp_port->type);
  9123. filter_index = READ_ONCE(udp_port->filter_index);
  9124. /* release RTNL while we wait on AQ command */
  9125. rtnl_unlock();
  9126. if (port)
  9127. ret = i40e_aq_add_udp_tunnel(hw, port,
  9128. type,
  9129. &filter_index,
  9130. NULL);
  9131. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  9132. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  9133. NULL);
  9134. /* reacquire RTNL so we can update filter_index */
  9135. rtnl_lock();
  9136. if (ret) {
  9137. dev_info(&pf->pdev->dev,
  9138. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  9139. i40e_tunnel_name(type),
  9140. port ? "add" : "delete",
  9141. port,
  9142. filter_index,
  9143. i40e_stat_str(&pf->hw, ret),
  9144. i40e_aq_str(&pf->hw,
  9145. pf->hw.aq.asq_last_status));
  9146. if (port) {
  9147. /* failed to add, just reset port,
  9148. * drop pending bit for any deletion
  9149. */
  9150. udp_port->port = 0;
  9151. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  9152. }
  9153. } else if (port) {
  9154. /* record filter index on success */
  9155. udp_port->filter_index = filter_index;
  9156. }
  9157. }
  9158. }
  9159. rtnl_unlock();
  9160. }
  9161. /**
  9162. * i40e_service_task - Run the driver's async subtasks
  9163. * @work: pointer to work_struct containing our data
  9164. **/
  9165. static void i40e_service_task(struct work_struct *work)
  9166. {
  9167. struct i40e_pf *pf = container_of(work,
  9168. struct i40e_pf,
  9169. service_task);
  9170. unsigned long start_time = jiffies;
  9171. /* don't bother with service tasks if a reset is in progress */
  9172. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  9173. test_bit(__I40E_SUSPENDED, pf->state))
  9174. return;
  9175. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  9176. return;
  9177. if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  9178. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  9179. i40e_sync_filters_subtask(pf);
  9180. i40e_reset_subtask(pf);
  9181. i40e_handle_mdd_event(pf);
  9182. i40e_vc_process_vflr_event(pf);
  9183. i40e_watchdog_subtask(pf);
  9184. i40e_fdir_reinit_subtask(pf);
  9185. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  9186. /* Client subtask will reopen next time through. */
  9187. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
  9188. true);
  9189. } else {
  9190. i40e_client_subtask(pf);
  9191. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  9192. pf->state))
  9193. i40e_notify_client_of_l2_param_changes(
  9194. pf->vsi[pf->lan_vsi]);
  9195. }
  9196. i40e_sync_filters_subtask(pf);
  9197. i40e_sync_udp_filters_subtask(pf);
  9198. } else {
  9199. i40e_reset_subtask(pf);
  9200. }
  9201. i40e_clean_adminq_subtask(pf);
  9202. /* flush memory to make sure state is correct before next watchdog */
  9203. smp_mb__before_atomic();
  9204. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  9205. /* If the tasks have taken longer than one timer cycle or there
  9206. * is more work to be done, reschedule the service task now
  9207. * rather than wait for the timer to tick again.
  9208. */
  9209. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  9210. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  9211. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  9212. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  9213. i40e_service_event_schedule(pf);
  9214. }
  9215. /**
  9216. * i40e_service_timer - timer callback
  9217. * @data: pointer to PF struct
  9218. **/
  9219. static void i40e_service_timer(struct timer_list *t)
  9220. {
  9221. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  9222. mod_timer(&pf->service_timer,
  9223. round_jiffies(jiffies + pf->service_timer_period));
  9224. i40e_service_event_schedule(pf);
  9225. }
  9226. /**
  9227. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  9228. * @vsi: the VSI being configured
  9229. **/
  9230. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  9231. {
  9232. struct i40e_pf *pf = vsi->back;
  9233. switch (vsi->type) {
  9234. case I40E_VSI_MAIN:
  9235. vsi->alloc_queue_pairs = pf->num_lan_qps;
  9236. if (!vsi->num_tx_desc)
  9237. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9238. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9239. if (!vsi->num_rx_desc)
  9240. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9241. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9242. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9243. vsi->num_q_vectors = pf->num_lan_msix;
  9244. else
  9245. vsi->num_q_vectors = 1;
  9246. break;
  9247. case I40E_VSI_FDIR:
  9248. vsi->alloc_queue_pairs = 1;
  9249. vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
  9250. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9251. vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
  9252. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9253. vsi->num_q_vectors = pf->num_fdsb_msix;
  9254. break;
  9255. case I40E_VSI_VMDQ2:
  9256. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  9257. if (!vsi->num_tx_desc)
  9258. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9259. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9260. if (!vsi->num_rx_desc)
  9261. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9262. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9263. vsi->num_q_vectors = pf->num_vmdq_msix;
  9264. break;
  9265. case I40E_VSI_SRIOV:
  9266. vsi->alloc_queue_pairs = pf->num_vf_qps;
  9267. if (!vsi->num_tx_desc)
  9268. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9269. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9270. if (!vsi->num_rx_desc)
  9271. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  9272. I40E_REQ_DESCRIPTOR_MULTIPLE);
  9273. break;
  9274. default:
  9275. WARN_ON(1);
  9276. return -ENODATA;
  9277. }
  9278. return 0;
  9279. }
  9280. /**
  9281. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  9282. * @vsi: VSI pointer
  9283. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  9284. *
  9285. * On error: returns error code (negative)
  9286. * On success: returns 0
  9287. **/
  9288. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  9289. {
  9290. struct i40e_ring **next_rings;
  9291. int size;
  9292. int ret = 0;
  9293. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  9294. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  9295. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  9296. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  9297. if (!vsi->tx_rings)
  9298. return -ENOMEM;
  9299. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  9300. if (i40e_enabled_xdp_vsi(vsi)) {
  9301. vsi->xdp_rings = next_rings;
  9302. next_rings += vsi->alloc_queue_pairs;
  9303. }
  9304. vsi->rx_rings = next_rings;
  9305. if (alloc_qvectors) {
  9306. /* allocate memory for q_vector pointers */
  9307. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  9308. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  9309. if (!vsi->q_vectors) {
  9310. ret = -ENOMEM;
  9311. goto err_vectors;
  9312. }
  9313. }
  9314. return ret;
  9315. err_vectors:
  9316. kfree(vsi->tx_rings);
  9317. return ret;
  9318. }
  9319. /**
  9320. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  9321. * @pf: board private structure
  9322. * @type: type of VSI
  9323. *
  9324. * On error: returns error code (negative)
  9325. * On success: returns vsi index in PF (positive)
  9326. **/
  9327. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  9328. {
  9329. int ret = -ENODEV;
  9330. struct i40e_vsi *vsi;
  9331. int vsi_idx;
  9332. int i;
  9333. /* Need to protect the allocation of the VSIs at the PF level */
  9334. mutex_lock(&pf->switch_mutex);
  9335. /* VSI list may be fragmented if VSI creation/destruction has
  9336. * been happening. We can afford to do a quick scan to look
  9337. * for any free VSIs in the list.
  9338. *
  9339. * find next empty vsi slot, looping back around if necessary
  9340. */
  9341. i = pf->next_vsi;
  9342. while (i < pf->num_alloc_vsi && pf->vsi[i])
  9343. i++;
  9344. if (i >= pf->num_alloc_vsi) {
  9345. i = 0;
  9346. while (i < pf->next_vsi && pf->vsi[i])
  9347. i++;
  9348. }
  9349. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  9350. vsi_idx = i; /* Found one! */
  9351. } else {
  9352. ret = -ENODEV;
  9353. goto unlock_pf; /* out of VSI slots! */
  9354. }
  9355. pf->next_vsi = ++i;
  9356. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  9357. if (!vsi) {
  9358. ret = -ENOMEM;
  9359. goto unlock_pf;
  9360. }
  9361. vsi->type = type;
  9362. vsi->back = pf;
  9363. set_bit(__I40E_VSI_DOWN, vsi->state);
  9364. vsi->flags = 0;
  9365. vsi->idx = vsi_idx;
  9366. vsi->int_rate_limit = 0;
  9367. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  9368. pf->rss_table_size : 64;
  9369. vsi->netdev_registered = false;
  9370. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  9371. hash_init(vsi->mac_filter_hash);
  9372. vsi->irqs_ready = false;
  9373. if (type == I40E_VSI_MAIN) {
  9374. vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
  9375. if (!vsi->af_xdp_zc_qps)
  9376. goto err_rings;
  9377. }
  9378. ret = i40e_set_num_rings_in_vsi(vsi);
  9379. if (ret)
  9380. goto err_rings;
  9381. ret = i40e_vsi_alloc_arrays(vsi, true);
  9382. if (ret)
  9383. goto err_rings;
  9384. /* Setup default MSIX irq handler for VSI */
  9385. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  9386. /* Initialize VSI lock */
  9387. spin_lock_init(&vsi->mac_filter_hash_lock);
  9388. pf->vsi[vsi_idx] = vsi;
  9389. ret = vsi_idx;
  9390. goto unlock_pf;
  9391. err_rings:
  9392. bitmap_free(vsi->af_xdp_zc_qps);
  9393. pf->next_vsi = i - 1;
  9394. kfree(vsi);
  9395. unlock_pf:
  9396. mutex_unlock(&pf->switch_mutex);
  9397. return ret;
  9398. }
  9399. /**
  9400. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  9401. * @vsi: VSI pointer
  9402. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  9403. *
  9404. * On error: returns error code (negative)
  9405. * On success: returns 0
  9406. **/
  9407. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  9408. {
  9409. /* free the ring and vector containers */
  9410. if (free_qvectors) {
  9411. kfree(vsi->q_vectors);
  9412. vsi->q_vectors = NULL;
  9413. }
  9414. kfree(vsi->tx_rings);
  9415. vsi->tx_rings = NULL;
  9416. vsi->rx_rings = NULL;
  9417. vsi->xdp_rings = NULL;
  9418. }
  9419. /**
  9420. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  9421. * and lookup table
  9422. * @vsi: Pointer to VSI structure
  9423. */
  9424. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  9425. {
  9426. if (!vsi)
  9427. return;
  9428. kfree(vsi->rss_hkey_user);
  9429. vsi->rss_hkey_user = NULL;
  9430. kfree(vsi->rss_lut_user);
  9431. vsi->rss_lut_user = NULL;
  9432. }
  9433. /**
  9434. * i40e_vsi_clear - Deallocate the VSI provided
  9435. * @vsi: the VSI being un-configured
  9436. **/
  9437. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  9438. {
  9439. struct i40e_pf *pf;
  9440. if (!vsi)
  9441. return 0;
  9442. if (!vsi->back)
  9443. goto free_vsi;
  9444. pf = vsi->back;
  9445. mutex_lock(&pf->switch_mutex);
  9446. if (!pf->vsi[vsi->idx]) {
  9447. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  9448. vsi->idx, vsi->idx, vsi->type);
  9449. goto unlock_vsi;
  9450. }
  9451. if (pf->vsi[vsi->idx] != vsi) {
  9452. dev_err(&pf->pdev->dev,
  9453. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  9454. pf->vsi[vsi->idx]->idx,
  9455. pf->vsi[vsi->idx]->type,
  9456. vsi->idx, vsi->type);
  9457. goto unlock_vsi;
  9458. }
  9459. /* updates the PF for this cleared vsi */
  9460. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  9461. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  9462. bitmap_free(vsi->af_xdp_zc_qps);
  9463. i40e_vsi_free_arrays(vsi, true);
  9464. i40e_clear_rss_config_user(vsi);
  9465. pf->vsi[vsi->idx] = NULL;
  9466. if (vsi->idx < pf->next_vsi)
  9467. pf->next_vsi = vsi->idx;
  9468. unlock_vsi:
  9469. mutex_unlock(&pf->switch_mutex);
  9470. free_vsi:
  9471. kfree(vsi);
  9472. return 0;
  9473. }
  9474. /**
  9475. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  9476. * @vsi: the VSI being cleaned
  9477. **/
  9478. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  9479. {
  9480. int i;
  9481. if (vsi->tx_rings && vsi->tx_rings[0]) {
  9482. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  9483. kfree_rcu(vsi->tx_rings[i], rcu);
  9484. vsi->tx_rings[i] = NULL;
  9485. vsi->rx_rings[i] = NULL;
  9486. if (vsi->xdp_rings)
  9487. vsi->xdp_rings[i] = NULL;
  9488. }
  9489. }
  9490. }
  9491. /**
  9492. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  9493. * @vsi: the VSI being configured
  9494. **/
  9495. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  9496. {
  9497. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  9498. struct i40e_pf *pf = vsi->back;
  9499. struct i40e_ring *ring;
  9500. /* Set basic values in the rings to be used later during open() */
  9501. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  9502. /* allocate space for both Tx and Rx in one shot */
  9503. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  9504. if (!ring)
  9505. goto err_out;
  9506. ring->queue_index = i;
  9507. ring->reg_idx = vsi->base_queue + i;
  9508. ring->ring_active = false;
  9509. ring->vsi = vsi;
  9510. ring->netdev = vsi->netdev;
  9511. ring->dev = &pf->pdev->dev;
  9512. ring->count = vsi->num_tx_desc;
  9513. ring->size = 0;
  9514. ring->dcb_tc = 0;
  9515. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  9516. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  9517. ring->itr_setting = pf->tx_itr_default;
  9518. vsi->tx_rings[i] = ring++;
  9519. if (!i40e_enabled_xdp_vsi(vsi))
  9520. goto setup_rx;
  9521. ring->queue_index = vsi->alloc_queue_pairs + i;
  9522. ring->reg_idx = vsi->base_queue + ring->queue_index;
  9523. ring->ring_active = false;
  9524. ring->vsi = vsi;
  9525. ring->netdev = NULL;
  9526. ring->dev = &pf->pdev->dev;
  9527. ring->count = vsi->num_tx_desc;
  9528. ring->size = 0;
  9529. ring->dcb_tc = 0;
  9530. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  9531. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  9532. set_ring_xdp(ring);
  9533. ring->itr_setting = pf->tx_itr_default;
  9534. vsi->xdp_rings[i] = ring++;
  9535. setup_rx:
  9536. ring->queue_index = i;
  9537. ring->reg_idx = vsi->base_queue + i;
  9538. ring->ring_active = false;
  9539. ring->vsi = vsi;
  9540. ring->netdev = vsi->netdev;
  9541. ring->dev = &pf->pdev->dev;
  9542. ring->count = vsi->num_rx_desc;
  9543. ring->size = 0;
  9544. ring->dcb_tc = 0;
  9545. ring->itr_setting = pf->rx_itr_default;
  9546. vsi->rx_rings[i] = ring;
  9547. }
  9548. return 0;
  9549. err_out:
  9550. i40e_vsi_clear_rings(vsi);
  9551. return -ENOMEM;
  9552. }
  9553. /**
  9554. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  9555. * @pf: board private structure
  9556. * @vectors: the number of MSI-X vectors to request
  9557. *
  9558. * Returns the number of vectors reserved, or error
  9559. **/
  9560. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  9561. {
  9562. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  9563. I40E_MIN_MSIX, vectors);
  9564. if (vectors < 0) {
  9565. dev_info(&pf->pdev->dev,
  9566. "MSI-X vector reservation failed: %d\n", vectors);
  9567. vectors = 0;
  9568. }
  9569. return vectors;
  9570. }
  9571. /**
  9572. * i40e_init_msix - Setup the MSIX capability
  9573. * @pf: board private structure
  9574. *
  9575. * Work with the OS to set up the MSIX vectors needed.
  9576. *
  9577. * Returns the number of vectors reserved or negative on failure
  9578. **/
  9579. static int i40e_init_msix(struct i40e_pf *pf)
  9580. {
  9581. struct i40e_hw *hw = &pf->hw;
  9582. int cpus, extra_vectors;
  9583. int vectors_left;
  9584. int v_budget, i;
  9585. int v_actual;
  9586. int iwarp_requested = 0;
  9587. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  9588. return -ENODEV;
  9589. /* The number of vectors we'll request will be comprised of:
  9590. * - Add 1 for "other" cause for Admin Queue events, etc.
  9591. * - The number of LAN queue pairs
  9592. * - Queues being used for RSS.
  9593. * We don't need as many as max_rss_size vectors.
  9594. * use rss_size instead in the calculation since that
  9595. * is governed by number of cpus in the system.
  9596. * - assumes symmetric Tx/Rx pairing
  9597. * - The number of VMDq pairs
  9598. * - The CPU count within the NUMA node if iWARP is enabled
  9599. * Once we count this up, try the request.
  9600. *
  9601. * If we can't get what we want, we'll simplify to nearly nothing
  9602. * and try again. If that still fails, we punt.
  9603. */
  9604. vectors_left = hw->func_caps.num_msix_vectors;
  9605. v_budget = 0;
  9606. /* reserve one vector for miscellaneous handler */
  9607. if (vectors_left) {
  9608. v_budget++;
  9609. vectors_left--;
  9610. }
  9611. /* reserve some vectors for the main PF traffic queues. Initially we
  9612. * only reserve at most 50% of the available vectors, in the case that
  9613. * the number of online CPUs is large. This ensures that we can enable
  9614. * extra features as well. Once we've enabled the other features, we
  9615. * will use any remaining vectors to reach as close as we can to the
  9616. * number of online CPUs.
  9617. */
  9618. cpus = num_online_cpus();
  9619. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9620. vectors_left -= pf->num_lan_msix;
  9621. /* reserve one vector for sideband flow director */
  9622. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9623. if (vectors_left) {
  9624. pf->num_fdsb_msix = 1;
  9625. v_budget++;
  9626. vectors_left--;
  9627. } else {
  9628. pf->num_fdsb_msix = 0;
  9629. }
  9630. }
  9631. /* can we reserve enough for iWARP? */
  9632. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9633. iwarp_requested = pf->num_iwarp_msix;
  9634. if (!vectors_left)
  9635. pf->num_iwarp_msix = 0;
  9636. else if (vectors_left < pf->num_iwarp_msix)
  9637. pf->num_iwarp_msix = 1;
  9638. v_budget += pf->num_iwarp_msix;
  9639. vectors_left -= pf->num_iwarp_msix;
  9640. }
  9641. /* any vectors left over go for VMDq support */
  9642. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9643. if (!vectors_left) {
  9644. pf->num_vmdq_msix = 0;
  9645. pf->num_vmdq_qps = 0;
  9646. } else {
  9647. int vmdq_vecs_wanted =
  9648. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9649. int vmdq_vecs =
  9650. min_t(int, vectors_left, vmdq_vecs_wanted);
  9651. /* if we're short on vectors for what's desired, we limit
  9652. * the queues per vmdq. If this is still more than are
  9653. * available, the user will need to change the number of
  9654. * queues/vectors used by the PF later with the ethtool
  9655. * channels command
  9656. */
  9657. if (vectors_left < vmdq_vecs_wanted) {
  9658. pf->num_vmdq_qps = 1;
  9659. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9660. vmdq_vecs = min_t(int,
  9661. vectors_left,
  9662. vmdq_vecs_wanted);
  9663. }
  9664. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9665. v_budget += vmdq_vecs;
  9666. vectors_left -= vmdq_vecs;
  9667. }
  9668. }
  9669. /* On systems with a large number of SMP cores, we previously limited
  9670. * the number of vectors for num_lan_msix to be at most 50% of the
  9671. * available vectors, to allow for other features. Now, we add back
  9672. * the remaining vectors. However, we ensure that the total
  9673. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9674. * calculate the number of vectors we can add without going over the
  9675. * cap of CPUs. For systems with a small number of CPUs this will be
  9676. * zero.
  9677. */
  9678. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9679. pf->num_lan_msix += extra_vectors;
  9680. vectors_left -= extra_vectors;
  9681. WARN(vectors_left < 0,
  9682. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9683. v_budget += pf->num_lan_msix;
  9684. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9685. GFP_KERNEL);
  9686. if (!pf->msix_entries)
  9687. return -ENOMEM;
  9688. for (i = 0; i < v_budget; i++)
  9689. pf->msix_entries[i].entry = i;
  9690. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9691. if (v_actual < I40E_MIN_MSIX) {
  9692. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9693. kfree(pf->msix_entries);
  9694. pf->msix_entries = NULL;
  9695. pci_disable_msix(pf->pdev);
  9696. return -ENODEV;
  9697. } else if (v_actual == I40E_MIN_MSIX) {
  9698. /* Adjust for minimal MSIX use */
  9699. pf->num_vmdq_vsis = 0;
  9700. pf->num_vmdq_qps = 0;
  9701. pf->num_lan_qps = 1;
  9702. pf->num_lan_msix = 1;
  9703. } else if (v_actual != v_budget) {
  9704. /* If we have limited resources, we will start with no vectors
  9705. * for the special features and then allocate vectors to some
  9706. * of these features based on the policy and at the end disable
  9707. * the features that did not get any vectors.
  9708. */
  9709. int vec;
  9710. dev_info(&pf->pdev->dev,
  9711. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9712. v_actual, v_budget);
  9713. /* reserve the misc vector */
  9714. vec = v_actual - 1;
  9715. /* Scale vector usage down */
  9716. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9717. pf->num_vmdq_vsis = 1;
  9718. pf->num_vmdq_qps = 1;
  9719. /* partition out the remaining vectors */
  9720. switch (vec) {
  9721. case 2:
  9722. pf->num_lan_msix = 1;
  9723. break;
  9724. case 3:
  9725. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9726. pf->num_lan_msix = 1;
  9727. pf->num_iwarp_msix = 1;
  9728. } else {
  9729. pf->num_lan_msix = 2;
  9730. }
  9731. break;
  9732. default:
  9733. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9734. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9735. iwarp_requested);
  9736. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9737. I40E_DEFAULT_NUM_VMDQ_VSI);
  9738. } else {
  9739. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9740. I40E_DEFAULT_NUM_VMDQ_VSI);
  9741. }
  9742. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9743. pf->num_fdsb_msix = 1;
  9744. vec--;
  9745. }
  9746. pf->num_lan_msix = min_t(int,
  9747. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9748. pf->num_lan_msix);
  9749. pf->num_lan_qps = pf->num_lan_msix;
  9750. break;
  9751. }
  9752. }
  9753. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9754. (pf->num_fdsb_msix == 0)) {
  9755. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9756. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9757. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9758. }
  9759. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9760. (pf->num_vmdq_msix == 0)) {
  9761. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9762. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9763. }
  9764. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9765. (pf->num_iwarp_msix == 0)) {
  9766. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9767. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9768. }
  9769. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9770. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9771. pf->num_lan_msix,
  9772. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9773. pf->num_fdsb_msix,
  9774. pf->num_iwarp_msix);
  9775. return v_actual;
  9776. }
  9777. /**
  9778. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9779. * @vsi: the VSI being configured
  9780. * @v_idx: index of the vector in the vsi struct
  9781. * @cpu: cpu to be used on affinity_mask
  9782. *
  9783. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9784. **/
  9785. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9786. {
  9787. struct i40e_q_vector *q_vector;
  9788. /* allocate q_vector */
  9789. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9790. if (!q_vector)
  9791. return -ENOMEM;
  9792. q_vector->vsi = vsi;
  9793. q_vector->v_idx = v_idx;
  9794. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9795. if (vsi->netdev)
  9796. netif_napi_add(vsi->netdev, &q_vector->napi,
  9797. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9798. /* tie q_vector and vsi together */
  9799. vsi->q_vectors[v_idx] = q_vector;
  9800. return 0;
  9801. }
  9802. /**
  9803. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9804. * @vsi: the VSI being configured
  9805. *
  9806. * We allocate one q_vector per queue interrupt. If allocation fails we
  9807. * return -ENOMEM.
  9808. **/
  9809. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9810. {
  9811. struct i40e_pf *pf = vsi->back;
  9812. int err, v_idx, num_q_vectors, current_cpu;
  9813. /* if not MSIX, give the one vector only to the LAN VSI */
  9814. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9815. num_q_vectors = vsi->num_q_vectors;
  9816. else if (vsi == pf->vsi[pf->lan_vsi])
  9817. num_q_vectors = 1;
  9818. else
  9819. return -EINVAL;
  9820. current_cpu = cpumask_first(cpu_online_mask);
  9821. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9822. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9823. if (err)
  9824. goto err_out;
  9825. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9826. if (unlikely(current_cpu >= nr_cpu_ids))
  9827. current_cpu = cpumask_first(cpu_online_mask);
  9828. }
  9829. return 0;
  9830. err_out:
  9831. while (v_idx--)
  9832. i40e_free_q_vector(vsi, v_idx);
  9833. return err;
  9834. }
  9835. /**
  9836. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9837. * @pf: board private structure to initialize
  9838. **/
  9839. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9840. {
  9841. int vectors = 0;
  9842. ssize_t size;
  9843. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9844. vectors = i40e_init_msix(pf);
  9845. if (vectors < 0) {
  9846. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9847. I40E_FLAG_IWARP_ENABLED |
  9848. I40E_FLAG_RSS_ENABLED |
  9849. I40E_FLAG_DCB_CAPABLE |
  9850. I40E_FLAG_DCB_ENABLED |
  9851. I40E_FLAG_SRIOV_ENABLED |
  9852. I40E_FLAG_FD_SB_ENABLED |
  9853. I40E_FLAG_FD_ATR_ENABLED |
  9854. I40E_FLAG_VMDQ_ENABLED);
  9855. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9856. /* rework the queue expectations without MSIX */
  9857. i40e_determine_queue_usage(pf);
  9858. }
  9859. }
  9860. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9861. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9862. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9863. vectors = pci_enable_msi(pf->pdev);
  9864. if (vectors < 0) {
  9865. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9866. vectors);
  9867. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9868. }
  9869. vectors = 1; /* one MSI or Legacy vector */
  9870. }
  9871. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9872. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9873. /* set up vector assignment tracking */
  9874. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9875. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9876. if (!pf->irq_pile)
  9877. return -ENOMEM;
  9878. pf->irq_pile->num_entries = vectors;
  9879. pf->irq_pile->search_hint = 0;
  9880. /* track first vector for misc interrupts, ignore return */
  9881. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9882. return 0;
  9883. }
  9884. /**
  9885. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9886. * @pf: private board data structure
  9887. *
  9888. * Restore the interrupt scheme that was cleared when we suspended the
  9889. * device. This should be called during resume to re-allocate the q_vectors
  9890. * and reacquire IRQs.
  9891. */
  9892. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9893. {
  9894. int err, i;
  9895. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9896. * scheme. We need to re-enabled them here in order to attempt to
  9897. * re-acquire the MSI or MSI-X vectors
  9898. */
  9899. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9900. err = i40e_init_interrupt_scheme(pf);
  9901. if (err)
  9902. return err;
  9903. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9904. * rings together again.
  9905. */
  9906. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9907. if (pf->vsi[i]) {
  9908. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9909. if (err)
  9910. goto err_unwind;
  9911. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9912. }
  9913. }
  9914. err = i40e_setup_misc_vector(pf);
  9915. if (err)
  9916. goto err_unwind;
  9917. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9918. i40e_client_update_msix_info(pf);
  9919. return 0;
  9920. err_unwind:
  9921. while (i--) {
  9922. if (pf->vsi[i])
  9923. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9924. }
  9925. return err;
  9926. }
  9927. /**
  9928. * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
  9929. * non queue events in recovery mode
  9930. * @pf: board private structure
  9931. *
  9932. * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
  9933. * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
  9934. * This is handled differently than in recovery mode since no Tx/Rx resources
  9935. * are being allocated.
  9936. **/
  9937. static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
  9938. {
  9939. int err;
  9940. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9941. err = i40e_setup_misc_vector(pf);
  9942. if (err) {
  9943. dev_info(&pf->pdev->dev,
  9944. "MSI-X misc vector request failed, error %d\n",
  9945. err);
  9946. return err;
  9947. }
  9948. } else {
  9949. u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
  9950. err = request_irq(pf->pdev->irq, i40e_intr, flags,
  9951. pf->int_name, pf);
  9952. if (err) {
  9953. dev_info(&pf->pdev->dev,
  9954. "MSI/legacy misc vector request failed, error %d\n",
  9955. err);
  9956. return err;
  9957. }
  9958. i40e_enable_misc_int_causes(pf);
  9959. i40e_irq_dynamic_enable_icr0(pf);
  9960. }
  9961. return 0;
  9962. }
  9963. /**
  9964. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9965. * @pf: board private structure
  9966. *
  9967. * This sets up the handler for MSIX 0, which is used to manage the
  9968. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9969. * when in MSI or Legacy interrupt mode.
  9970. **/
  9971. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9972. {
  9973. struct i40e_hw *hw = &pf->hw;
  9974. int err = 0;
  9975. /* Only request the IRQ once, the first time through. */
  9976. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9977. err = request_irq(pf->msix_entries[0].vector,
  9978. i40e_intr, 0, pf->int_name, pf);
  9979. if (err) {
  9980. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9981. dev_info(&pf->pdev->dev,
  9982. "request_irq for %s failed: %d\n",
  9983. pf->int_name, err);
  9984. return -EFAULT;
  9985. }
  9986. }
  9987. i40e_enable_misc_int_causes(pf);
  9988. /* associate no queues to the misc vector */
  9989. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9990. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
  9991. i40e_flush(hw);
  9992. i40e_irq_dynamic_enable_icr0(pf);
  9993. return err;
  9994. }
  9995. /**
  9996. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9997. * @vsi: Pointer to vsi structure
  9998. * @seed: Buffter to store the hash keys
  9999. * @lut: Buffer to store the lookup table entries
  10000. * @lut_size: Size of buffer to store the lookup table entries
  10001. *
  10002. * Return 0 on success, negative on failure
  10003. */
  10004. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  10005. u8 *lut, u16 lut_size)
  10006. {
  10007. struct i40e_pf *pf = vsi->back;
  10008. struct i40e_hw *hw = &pf->hw;
  10009. int ret = 0;
  10010. if (seed) {
  10011. ret = i40e_aq_get_rss_key(hw, vsi->id,
  10012. (struct i40e_aqc_get_set_rss_key_data *)seed);
  10013. if (ret) {
  10014. dev_info(&pf->pdev->dev,
  10015. "Cannot get RSS key, err %s aq_err %s\n",
  10016. i40e_stat_str(&pf->hw, ret),
  10017. i40e_aq_str(&pf->hw,
  10018. pf->hw.aq.asq_last_status));
  10019. return ret;
  10020. }
  10021. }
  10022. if (lut) {
  10023. bool pf_lut = vsi->type == I40E_VSI_MAIN;
  10024. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  10025. if (ret) {
  10026. dev_info(&pf->pdev->dev,
  10027. "Cannot get RSS lut, err %s aq_err %s\n",
  10028. i40e_stat_str(&pf->hw, ret),
  10029. i40e_aq_str(&pf->hw,
  10030. pf->hw.aq.asq_last_status));
  10031. return ret;
  10032. }
  10033. }
  10034. return ret;
  10035. }
  10036. /**
  10037. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  10038. * @vsi: Pointer to vsi structure
  10039. * @seed: RSS hash seed
  10040. * @lut: Lookup table
  10041. * @lut_size: Lookup table size
  10042. *
  10043. * Returns 0 on success, negative on failure
  10044. **/
  10045. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  10046. const u8 *lut, u16 lut_size)
  10047. {
  10048. struct i40e_pf *pf = vsi->back;
  10049. struct i40e_hw *hw = &pf->hw;
  10050. u16 vf_id = vsi->vf_id;
  10051. u8 i;
  10052. /* Fill out hash function seed */
  10053. if (seed) {
  10054. u32 *seed_dw = (u32 *)seed;
  10055. if (vsi->type == I40E_VSI_MAIN) {
  10056. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  10057. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  10058. } else if (vsi->type == I40E_VSI_SRIOV) {
  10059. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  10060. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  10061. } else {
  10062. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  10063. }
  10064. }
  10065. if (lut) {
  10066. u32 *lut_dw = (u32 *)lut;
  10067. if (vsi->type == I40E_VSI_MAIN) {
  10068. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  10069. return -EINVAL;
  10070. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10071. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  10072. } else if (vsi->type == I40E_VSI_SRIOV) {
  10073. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  10074. return -EINVAL;
  10075. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  10076. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  10077. } else {
  10078. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  10079. }
  10080. }
  10081. i40e_flush(hw);
  10082. return 0;
  10083. }
  10084. /**
  10085. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  10086. * @vsi: Pointer to VSI structure
  10087. * @seed: Buffer to store the keys
  10088. * @lut: Buffer to store the lookup table entries
  10089. * @lut_size: Size of buffer to store the lookup table entries
  10090. *
  10091. * Returns 0 on success, negative on failure
  10092. */
  10093. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  10094. u8 *lut, u16 lut_size)
  10095. {
  10096. struct i40e_pf *pf = vsi->back;
  10097. struct i40e_hw *hw = &pf->hw;
  10098. u16 i;
  10099. if (seed) {
  10100. u32 *seed_dw = (u32 *)seed;
  10101. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  10102. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  10103. }
  10104. if (lut) {
  10105. u32 *lut_dw = (u32 *)lut;
  10106. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  10107. return -EINVAL;
  10108. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10109. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  10110. }
  10111. return 0;
  10112. }
  10113. /**
  10114. * i40e_config_rss - Configure RSS keys and lut
  10115. * @vsi: Pointer to VSI structure
  10116. * @seed: RSS hash seed
  10117. * @lut: Lookup table
  10118. * @lut_size: Lookup table size
  10119. *
  10120. * Returns 0 on success, negative on failure
  10121. */
  10122. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  10123. {
  10124. struct i40e_pf *pf = vsi->back;
  10125. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  10126. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  10127. else
  10128. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  10129. }
  10130. /**
  10131. * i40e_get_rss - Get RSS keys and lut
  10132. * @vsi: Pointer to VSI structure
  10133. * @seed: Buffer to store the keys
  10134. * @lut: Buffer to store the lookup table entries
  10135. * @lut_size: Size of buffer to store the lookup table entries
  10136. *
  10137. * Returns 0 on success, negative on failure
  10138. */
  10139. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  10140. {
  10141. struct i40e_pf *pf = vsi->back;
  10142. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  10143. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  10144. else
  10145. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  10146. }
  10147. /**
  10148. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  10149. * @pf: Pointer to board private structure
  10150. * @lut: Lookup table
  10151. * @rss_table_size: Lookup table size
  10152. * @rss_size: Range of queue number for hashing
  10153. */
  10154. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  10155. u16 rss_table_size, u16 rss_size)
  10156. {
  10157. u16 i;
  10158. for (i = 0; i < rss_table_size; i++)
  10159. lut[i] = i % rss_size;
  10160. }
  10161. /**
  10162. * i40e_pf_config_rss - Prepare for RSS if used
  10163. * @pf: board private structure
  10164. **/
  10165. static int i40e_pf_config_rss(struct i40e_pf *pf)
  10166. {
  10167. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  10168. u8 seed[I40E_HKEY_ARRAY_SIZE];
  10169. u8 *lut;
  10170. struct i40e_hw *hw = &pf->hw;
  10171. u32 reg_val;
  10172. u64 hena;
  10173. int ret;
  10174. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  10175. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  10176. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  10177. hena |= i40e_pf_get_default_rss_hena(pf);
  10178. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  10179. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  10180. /* Determine the RSS table size based on the hardware capabilities */
  10181. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  10182. reg_val = (pf->rss_table_size == 512) ?
  10183. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  10184. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  10185. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  10186. /* Determine the RSS size of the VSI */
  10187. if (!vsi->rss_size) {
  10188. u16 qcount;
  10189. /* If the firmware does something weird during VSI init, we
  10190. * could end up with zero TCs. Check for that to avoid
  10191. * divide-by-zero. It probably won't pass traffic, but it also
  10192. * won't panic.
  10193. */
  10194. qcount = vsi->num_queue_pairs /
  10195. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  10196. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  10197. }
  10198. if (!vsi->rss_size)
  10199. return -EINVAL;
  10200. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  10201. if (!lut)
  10202. return -ENOMEM;
  10203. /* Use user configured lut if there is one, otherwise use default */
  10204. if (vsi->rss_lut_user)
  10205. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  10206. else
  10207. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  10208. /* Use user configured hash key if there is one, otherwise
  10209. * use default.
  10210. */
  10211. if (vsi->rss_hkey_user)
  10212. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  10213. else
  10214. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  10215. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  10216. kfree(lut);
  10217. return ret;
  10218. }
  10219. /**
  10220. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  10221. * @pf: board private structure
  10222. * @queue_count: the requested queue count for rss.
  10223. *
  10224. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  10225. * count which may be different from the requested queue count.
  10226. * Note: expects to be called while under rtnl_lock()
  10227. **/
  10228. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  10229. {
  10230. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  10231. int new_rss_size;
  10232. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  10233. return 0;
  10234. queue_count = min_t(int, queue_count, num_online_cpus());
  10235. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  10236. if (queue_count != vsi->num_queue_pairs) {
  10237. u16 qcount;
  10238. vsi->req_queue_pairs = queue_count;
  10239. i40e_prep_for_reset(pf, true);
  10240. pf->alloc_rss_size = new_rss_size;
  10241. i40e_reset_and_rebuild(pf, true, true);
  10242. /* Discard the user configured hash keys and lut, if less
  10243. * queues are enabled.
  10244. */
  10245. if (queue_count < vsi->rss_size) {
  10246. i40e_clear_rss_config_user(vsi);
  10247. dev_dbg(&pf->pdev->dev,
  10248. "discard user configured hash keys and lut\n");
  10249. }
  10250. /* Reset vsi->rss_size, as number of enabled queues changed */
  10251. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  10252. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  10253. i40e_pf_config_rss(pf);
  10254. }
  10255. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  10256. vsi->req_queue_pairs, pf->rss_size_max);
  10257. return pf->alloc_rss_size;
  10258. }
  10259. /**
  10260. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  10261. * @pf: board private structure
  10262. **/
  10263. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  10264. {
  10265. i40e_status status;
  10266. bool min_valid, max_valid;
  10267. u32 max_bw, min_bw;
  10268. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  10269. &min_valid, &max_valid);
  10270. if (!status) {
  10271. if (min_valid)
  10272. pf->min_bw = min_bw;
  10273. if (max_valid)
  10274. pf->max_bw = max_bw;
  10275. }
  10276. return status;
  10277. }
  10278. /**
  10279. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  10280. * @pf: board private structure
  10281. **/
  10282. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  10283. {
  10284. struct i40e_aqc_configure_partition_bw_data bw_data;
  10285. i40e_status status;
  10286. /* Set the valid bit for this PF */
  10287. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  10288. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  10289. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  10290. /* Set the new bandwidths */
  10291. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  10292. return status;
  10293. }
  10294. /**
  10295. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  10296. * @pf: board private structure
  10297. **/
  10298. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  10299. {
  10300. /* Commit temporary BW setting to permanent NVM image */
  10301. enum i40e_admin_queue_err last_aq_status;
  10302. i40e_status ret;
  10303. u16 nvm_word;
  10304. if (pf->hw.partition_id != 1) {
  10305. dev_info(&pf->pdev->dev,
  10306. "Commit BW only works on partition 1! This is partition %d",
  10307. pf->hw.partition_id);
  10308. ret = I40E_NOT_SUPPORTED;
  10309. goto bw_commit_out;
  10310. }
  10311. /* Acquire NVM for read access */
  10312. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  10313. last_aq_status = pf->hw.aq.asq_last_status;
  10314. if (ret) {
  10315. dev_info(&pf->pdev->dev,
  10316. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  10317. i40e_stat_str(&pf->hw, ret),
  10318. i40e_aq_str(&pf->hw, last_aq_status));
  10319. goto bw_commit_out;
  10320. }
  10321. /* Read word 0x10 of NVM - SW compatibility word 1 */
  10322. ret = i40e_aq_read_nvm(&pf->hw,
  10323. I40E_SR_NVM_CONTROL_WORD,
  10324. 0x10, sizeof(nvm_word), &nvm_word,
  10325. false, NULL);
  10326. /* Save off last admin queue command status before releasing
  10327. * the NVM
  10328. */
  10329. last_aq_status = pf->hw.aq.asq_last_status;
  10330. i40e_release_nvm(&pf->hw);
  10331. if (ret) {
  10332. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  10333. i40e_stat_str(&pf->hw, ret),
  10334. i40e_aq_str(&pf->hw, last_aq_status));
  10335. goto bw_commit_out;
  10336. }
  10337. /* Wait a bit for NVM release to complete */
  10338. msleep(50);
  10339. /* Acquire NVM for write access */
  10340. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  10341. last_aq_status = pf->hw.aq.asq_last_status;
  10342. if (ret) {
  10343. dev_info(&pf->pdev->dev,
  10344. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  10345. i40e_stat_str(&pf->hw, ret),
  10346. i40e_aq_str(&pf->hw, last_aq_status));
  10347. goto bw_commit_out;
  10348. }
  10349. /* Write it back out unchanged to initiate update NVM,
  10350. * which will force a write of the shadow (alt) RAM to
  10351. * the NVM - thus storing the bandwidth values permanently.
  10352. */
  10353. ret = i40e_aq_update_nvm(&pf->hw,
  10354. I40E_SR_NVM_CONTROL_WORD,
  10355. 0x10, sizeof(nvm_word),
  10356. &nvm_word, true, 0, NULL);
  10357. /* Save off last admin queue command status before releasing
  10358. * the NVM
  10359. */
  10360. last_aq_status = pf->hw.aq.asq_last_status;
  10361. i40e_release_nvm(&pf->hw);
  10362. if (ret)
  10363. dev_info(&pf->pdev->dev,
  10364. "BW settings NOT SAVED, err %s aq_err %s\n",
  10365. i40e_stat_str(&pf->hw, ret),
  10366. i40e_aq_str(&pf->hw, last_aq_status));
  10367. bw_commit_out:
  10368. return ret;
  10369. }
  10370. /**
  10371. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  10372. * @pf: board private structure to initialize
  10373. *
  10374. * i40e_sw_init initializes the Adapter private data structure.
  10375. * Fields are initialized based on PCI device information and
  10376. * OS network device settings (MTU size).
  10377. **/
  10378. static int i40e_sw_init(struct i40e_pf *pf)
  10379. {
  10380. int err = 0;
  10381. int size;
  10382. /* Set default capability flags */
  10383. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  10384. I40E_FLAG_MSI_ENABLED |
  10385. I40E_FLAG_MSIX_ENABLED;
  10386. /* Set default ITR */
  10387. pf->rx_itr_default = I40E_ITR_RX_DEF;
  10388. pf->tx_itr_default = I40E_ITR_TX_DEF;
  10389. /* Depending on PF configurations, it is possible that the RSS
  10390. * maximum might end up larger than the available queues
  10391. */
  10392. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  10393. pf->alloc_rss_size = 1;
  10394. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  10395. pf->rss_size_max = min_t(int, pf->rss_size_max,
  10396. pf->hw.func_caps.num_tx_qp);
  10397. if (pf->hw.func_caps.rss) {
  10398. pf->flags |= I40E_FLAG_RSS_ENABLED;
  10399. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  10400. num_online_cpus());
  10401. }
  10402. /* MFP mode enabled */
  10403. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  10404. pf->flags |= I40E_FLAG_MFP_ENABLED;
  10405. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  10406. if (i40e_get_partition_bw_setting(pf)) {
  10407. dev_warn(&pf->pdev->dev,
  10408. "Could not get partition bw settings\n");
  10409. } else {
  10410. dev_info(&pf->pdev->dev,
  10411. "Partition BW Min = %8.8x, Max = %8.8x\n",
  10412. pf->min_bw, pf->max_bw);
  10413. /* nudge the Tx scheduler */
  10414. i40e_set_partition_bw_setting(pf);
  10415. }
  10416. }
  10417. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  10418. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  10419. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  10420. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  10421. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  10422. pf->hw.num_partitions > 1)
  10423. dev_info(&pf->pdev->dev,
  10424. "Flow Director Sideband mode Disabled in MFP mode\n");
  10425. else
  10426. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  10427. pf->fdir_pf_filter_count =
  10428. pf->hw.func_caps.fd_filters_guaranteed;
  10429. pf->hw.fdir_shared_filter_count =
  10430. pf->hw.func_caps.fd_filters_best_effort;
  10431. }
  10432. if (pf->hw.mac.type == I40E_MAC_X722) {
  10433. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  10434. I40E_HW_128_QP_RSS_CAPABLE |
  10435. I40E_HW_ATR_EVICT_CAPABLE |
  10436. I40E_HW_WB_ON_ITR_CAPABLE |
  10437. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  10438. I40E_HW_NO_PCI_LINK_CHECK |
  10439. I40E_HW_USE_SET_LLDP_MIB |
  10440. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  10441. I40E_HW_PTP_L4_CAPABLE |
  10442. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  10443. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  10444. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  10445. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  10446. I40E_FDEVICT_PCTYPE_DEFAULT) {
  10447. dev_warn(&pf->pdev->dev,
  10448. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  10449. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  10450. }
  10451. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  10452. ((pf->hw.aq.api_maj_ver == 1) &&
  10453. (pf->hw.aq.api_min_ver > 4))) {
  10454. /* Supported in FW API version higher than 1.4 */
  10455. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  10456. }
  10457. /* Enable HW ATR eviction if possible */
  10458. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  10459. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  10460. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  10461. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  10462. (pf->hw.aq.fw_maj_ver < 4))) {
  10463. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  10464. /* No DCB support for FW < v4.33 */
  10465. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  10466. }
  10467. /* Disable FW LLDP if FW < v4.3 */
  10468. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  10469. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  10470. (pf->hw.aq.fw_maj_ver < 4)))
  10471. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  10472. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  10473. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  10474. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  10475. (pf->hw.aq.fw_maj_ver >= 5)))
  10476. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  10477. /* Enable PTP L4 if FW > v6.0 */
  10478. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  10479. pf->hw.aq.fw_maj_ver >= 6)
  10480. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  10481. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  10482. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  10483. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  10484. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  10485. }
  10486. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  10487. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  10488. /* IWARP needs one extra vector for CQP just like MISC.*/
  10489. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  10490. }
  10491. /* Stopping FW LLDP engine is supported on XL710 and X722
  10492. * starting from FW versions determined in i40e_init_adminq.
  10493. * Stopping the FW LLDP engine is not supported on XL710
  10494. * if NPAR is functioning so unset this hw flag in this case.
  10495. */
  10496. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  10497. pf->hw.func_caps.npar_enable &&
  10498. (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
  10499. pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
  10500. #ifdef CONFIG_PCI_IOV
  10501. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  10502. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  10503. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  10504. pf->num_req_vfs = min_t(int,
  10505. pf->hw.func_caps.num_vfs,
  10506. I40E_MAX_VF_COUNT);
  10507. }
  10508. #endif /* CONFIG_PCI_IOV */
  10509. pf->eeprom_version = 0xDEAD;
  10510. pf->lan_veb = I40E_NO_VEB;
  10511. pf->lan_vsi = I40E_NO_VSI;
  10512. /* By default FW has this off for performance reasons */
  10513. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  10514. /* set up queue assignment tracking */
  10515. size = sizeof(struct i40e_lump_tracking)
  10516. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  10517. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  10518. if (!pf->qp_pile) {
  10519. err = -ENOMEM;
  10520. goto sw_init_done;
  10521. }
  10522. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  10523. pf->qp_pile->search_hint = 0;
  10524. pf->tx_timeout_recovery_level = 1;
  10525. mutex_init(&pf->switch_mutex);
  10526. sw_init_done:
  10527. return err;
  10528. }
  10529. /**
  10530. * i40e_set_ntuple - set the ntuple feature flag and take action
  10531. * @pf: board private structure to initialize
  10532. * @features: the feature set that the stack is suggesting
  10533. *
  10534. * returns a bool to indicate if reset needs to happen
  10535. **/
  10536. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  10537. {
  10538. bool need_reset = false;
  10539. /* Check if Flow Director n-tuple support was enabled or disabled. If
  10540. * the state changed, we need to reset.
  10541. */
  10542. if (features & NETIF_F_NTUPLE) {
  10543. /* Enable filters and mark for reset */
  10544. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  10545. need_reset = true;
  10546. /* enable FD_SB only if there is MSI-X vector and no cloud
  10547. * filters exist
  10548. */
  10549. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  10550. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  10551. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  10552. }
  10553. } else {
  10554. /* turn off filters, mark for reset and clear SW filter list */
  10555. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  10556. need_reset = true;
  10557. i40e_fdir_filter_exit(pf);
  10558. }
  10559. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  10560. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  10561. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  10562. /* reset fd counters */
  10563. pf->fd_add_err = 0;
  10564. pf->fd_atr_cnt = 0;
  10565. /* if ATR was auto disabled it can be re-enabled. */
  10566. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  10567. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  10568. (I40E_DEBUG_FD & pf->hw.debug_mask))
  10569. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  10570. }
  10571. return need_reset;
  10572. }
  10573. /**
  10574. * i40e_clear_rss_lut - clear the rx hash lookup table
  10575. * @vsi: the VSI being configured
  10576. **/
  10577. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  10578. {
  10579. struct i40e_pf *pf = vsi->back;
  10580. struct i40e_hw *hw = &pf->hw;
  10581. u16 vf_id = vsi->vf_id;
  10582. u8 i;
  10583. if (vsi->type == I40E_VSI_MAIN) {
  10584. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10585. wr32(hw, I40E_PFQF_HLUT(i), 0);
  10586. } else if (vsi->type == I40E_VSI_SRIOV) {
  10587. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  10588. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  10589. } else {
  10590. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  10591. }
  10592. }
  10593. /**
  10594. * i40e_set_features - set the netdev feature flags
  10595. * @netdev: ptr to the netdev being adjusted
  10596. * @features: the feature set that the stack is suggesting
  10597. * Note: expects to be called while under rtnl_lock()
  10598. **/
  10599. static int i40e_set_features(struct net_device *netdev,
  10600. netdev_features_t features)
  10601. {
  10602. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10603. struct i40e_vsi *vsi = np->vsi;
  10604. struct i40e_pf *pf = vsi->back;
  10605. bool need_reset;
  10606. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  10607. i40e_pf_config_rss(pf);
  10608. else if (!(features & NETIF_F_RXHASH) &&
  10609. netdev->features & NETIF_F_RXHASH)
  10610. i40e_clear_rss_lut(vsi);
  10611. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  10612. i40e_vlan_stripping_enable(vsi);
  10613. else
  10614. i40e_vlan_stripping_disable(vsi);
  10615. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  10616. dev_err(&pf->pdev->dev,
  10617. "Offloaded tc filters active, can't turn hw_tc_offload off");
  10618. return -EINVAL;
  10619. }
  10620. if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
  10621. i40e_del_all_macvlans(vsi);
  10622. need_reset = i40e_set_ntuple(pf, features);
  10623. if (need_reset)
  10624. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10625. return 0;
  10626. }
  10627. /**
  10628. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  10629. * @pf: board private structure
  10630. * @port: The UDP port to look up
  10631. *
  10632. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  10633. **/
  10634. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10635. {
  10636. u8 i;
  10637. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10638. /* Do not report ports with pending deletions as
  10639. * being available.
  10640. */
  10641. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10642. continue;
  10643. if (pf->udp_ports[i].port == port)
  10644. return i;
  10645. }
  10646. return i;
  10647. }
  10648. /**
  10649. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10650. * @netdev: This physical port's netdev
  10651. * @ti: Tunnel endpoint information
  10652. **/
  10653. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10654. struct udp_tunnel_info *ti)
  10655. {
  10656. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10657. struct i40e_vsi *vsi = np->vsi;
  10658. struct i40e_pf *pf = vsi->back;
  10659. u16 port = ntohs(ti->port);
  10660. u8 next_idx;
  10661. u8 idx;
  10662. idx = i40e_get_udp_port_idx(pf, port);
  10663. /* Check if port already exists */
  10664. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10665. netdev_info(netdev, "port %d already offloaded\n", port);
  10666. return;
  10667. }
  10668. /* Now check if there is space to add the new port */
  10669. next_idx = i40e_get_udp_port_idx(pf, 0);
  10670. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10671. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10672. port);
  10673. return;
  10674. }
  10675. switch (ti->type) {
  10676. case UDP_TUNNEL_TYPE_VXLAN:
  10677. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10678. break;
  10679. case UDP_TUNNEL_TYPE_GENEVE:
  10680. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10681. return;
  10682. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10683. break;
  10684. default:
  10685. return;
  10686. }
  10687. /* New port: add it and mark its index in the bitmap */
  10688. pf->udp_ports[next_idx].port = port;
  10689. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10690. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10691. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10692. }
  10693. /**
  10694. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10695. * @netdev: This physical port's netdev
  10696. * @ti: Tunnel endpoint information
  10697. **/
  10698. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10699. struct udp_tunnel_info *ti)
  10700. {
  10701. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10702. struct i40e_vsi *vsi = np->vsi;
  10703. struct i40e_pf *pf = vsi->back;
  10704. u16 port = ntohs(ti->port);
  10705. u8 idx;
  10706. idx = i40e_get_udp_port_idx(pf, port);
  10707. /* Check if port already exists */
  10708. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10709. goto not_found;
  10710. switch (ti->type) {
  10711. case UDP_TUNNEL_TYPE_VXLAN:
  10712. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10713. goto not_found;
  10714. break;
  10715. case UDP_TUNNEL_TYPE_GENEVE:
  10716. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10717. goto not_found;
  10718. break;
  10719. default:
  10720. goto not_found;
  10721. }
  10722. /* if port exists, set it to 0 (mark for deletion)
  10723. * and make it pending
  10724. */
  10725. pf->udp_ports[idx].port = 0;
  10726. /* Toggle pending bit instead of setting it. This way if we are
  10727. * deleting a port that has yet to be added we just clear the pending
  10728. * bit and don't have to worry about it.
  10729. */
  10730. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10731. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10732. return;
  10733. not_found:
  10734. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10735. port);
  10736. }
  10737. static int i40e_get_phys_port_id(struct net_device *netdev,
  10738. struct netdev_phys_item_id *ppid)
  10739. {
  10740. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10741. struct i40e_pf *pf = np->vsi->back;
  10742. struct i40e_hw *hw = &pf->hw;
  10743. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10744. return -EOPNOTSUPP;
  10745. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10746. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10747. return 0;
  10748. }
  10749. /**
  10750. * i40e_ndo_fdb_add - add an entry to the hardware database
  10751. * @ndm: the input from the stack
  10752. * @tb: pointer to array of nladdr (unused)
  10753. * @dev: the net device pointer
  10754. * @addr: the MAC address entry being added
  10755. * @vid: VLAN ID
  10756. * @flags: instructions from stack about fdb operation
  10757. */
  10758. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10759. struct net_device *dev,
  10760. const unsigned char *addr, u16 vid,
  10761. u16 flags,
  10762. struct netlink_ext_ack *extack)
  10763. {
  10764. struct i40e_netdev_priv *np = netdev_priv(dev);
  10765. struct i40e_pf *pf = np->vsi->back;
  10766. int err = 0;
  10767. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10768. return -EOPNOTSUPP;
  10769. if (vid) {
  10770. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10771. return -EINVAL;
  10772. }
  10773. /* Hardware does not support aging addresses so if a
  10774. * ndm_state is given only allow permanent addresses
  10775. */
  10776. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10777. netdev_info(dev, "FDB only supports static addresses\n");
  10778. return -EINVAL;
  10779. }
  10780. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10781. err = dev_uc_add_excl(dev, addr);
  10782. else if (is_multicast_ether_addr(addr))
  10783. err = dev_mc_add_excl(dev, addr);
  10784. else
  10785. err = -EINVAL;
  10786. /* Only return duplicate errors if NLM_F_EXCL is set */
  10787. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10788. err = 0;
  10789. return err;
  10790. }
  10791. /**
  10792. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10793. * @dev: the netdev being configured
  10794. * @nlh: RTNL message
  10795. * @flags: bridge flags
  10796. * @extack: netlink extended ack
  10797. *
  10798. * Inserts a new hardware bridge if not already created and
  10799. * enables the bridging mode requested (VEB or VEPA). If the
  10800. * hardware bridge has already been inserted and the request
  10801. * is to change the mode then that requires a PF reset to
  10802. * allow rebuild of the components with required hardware
  10803. * bridge mode enabled.
  10804. *
  10805. * Note: expects to be called while under rtnl_lock()
  10806. **/
  10807. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10808. struct nlmsghdr *nlh,
  10809. u16 flags,
  10810. struct netlink_ext_ack *extack)
  10811. {
  10812. struct i40e_netdev_priv *np = netdev_priv(dev);
  10813. struct i40e_vsi *vsi = np->vsi;
  10814. struct i40e_pf *pf = vsi->back;
  10815. struct i40e_veb *veb = NULL;
  10816. struct nlattr *attr, *br_spec;
  10817. int i, rem;
  10818. /* Only for PF VSI for now */
  10819. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10820. return -EOPNOTSUPP;
  10821. /* Find the HW bridge for PF VSI */
  10822. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10823. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10824. veb = pf->veb[i];
  10825. }
  10826. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10827. nla_for_each_nested(attr, br_spec, rem) {
  10828. __u16 mode;
  10829. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10830. continue;
  10831. mode = nla_get_u16(attr);
  10832. if ((mode != BRIDGE_MODE_VEPA) &&
  10833. (mode != BRIDGE_MODE_VEB))
  10834. return -EINVAL;
  10835. /* Insert a new HW bridge */
  10836. if (!veb) {
  10837. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10838. vsi->tc_config.enabled_tc);
  10839. if (veb) {
  10840. veb->bridge_mode = mode;
  10841. i40e_config_bridge_mode(veb);
  10842. } else {
  10843. /* No Bridge HW offload available */
  10844. return -ENOENT;
  10845. }
  10846. break;
  10847. } else if (mode != veb->bridge_mode) {
  10848. /* Existing HW bridge but different mode needs reset */
  10849. veb->bridge_mode = mode;
  10850. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10851. if (mode == BRIDGE_MODE_VEB)
  10852. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10853. else
  10854. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10855. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10856. break;
  10857. }
  10858. }
  10859. return 0;
  10860. }
  10861. /**
  10862. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10863. * @skb: skb buff
  10864. * @pid: process id
  10865. * @seq: RTNL message seq #
  10866. * @dev: the netdev being configured
  10867. * @filter_mask: unused
  10868. * @nlflags: netlink flags passed in
  10869. *
  10870. * Return the mode in which the hardware bridge is operating in
  10871. * i.e VEB or VEPA.
  10872. **/
  10873. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10874. struct net_device *dev,
  10875. u32 __always_unused filter_mask,
  10876. int nlflags)
  10877. {
  10878. struct i40e_netdev_priv *np = netdev_priv(dev);
  10879. struct i40e_vsi *vsi = np->vsi;
  10880. struct i40e_pf *pf = vsi->back;
  10881. struct i40e_veb *veb = NULL;
  10882. int i;
  10883. /* Only for PF VSI for now */
  10884. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10885. return -EOPNOTSUPP;
  10886. /* Find the HW bridge for the PF VSI */
  10887. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10888. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10889. veb = pf->veb[i];
  10890. }
  10891. if (!veb)
  10892. return 0;
  10893. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10894. 0, 0, nlflags, filter_mask, NULL);
  10895. }
  10896. /**
  10897. * i40e_features_check - Validate encapsulated packet conforms to limits
  10898. * @skb: skb buff
  10899. * @dev: This physical port's netdev
  10900. * @features: Offload features that the stack believes apply
  10901. **/
  10902. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10903. struct net_device *dev,
  10904. netdev_features_t features)
  10905. {
  10906. size_t len;
  10907. /* No point in doing any of this if neither checksum nor GSO are
  10908. * being requested for this frame. We can rule out both by just
  10909. * checking for CHECKSUM_PARTIAL
  10910. */
  10911. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10912. return features;
  10913. /* We cannot support GSO if the MSS is going to be less than
  10914. * 64 bytes. If it is then we need to drop support for GSO.
  10915. */
  10916. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10917. features &= ~NETIF_F_GSO_MASK;
  10918. /* MACLEN can support at most 63 words */
  10919. len = skb_network_header(skb) - skb->data;
  10920. if (len & ~(63 * 2))
  10921. goto out_err;
  10922. /* IPLEN and EIPLEN can support at most 127 dwords */
  10923. len = skb_transport_header(skb) - skb_network_header(skb);
  10924. if (len & ~(127 * 4))
  10925. goto out_err;
  10926. if (skb->encapsulation) {
  10927. /* L4TUNLEN can support 127 words */
  10928. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10929. if (len & ~(127 * 2))
  10930. goto out_err;
  10931. /* IPLEN can support at most 127 dwords */
  10932. len = skb_inner_transport_header(skb) -
  10933. skb_inner_network_header(skb);
  10934. if (len & ~(127 * 4))
  10935. goto out_err;
  10936. }
  10937. /* No need to validate L4LEN as TCP is the only protocol with a
  10938. * a flexible value and we support all possible values supported
  10939. * by TCP, which is at most 15 dwords
  10940. */
  10941. return features;
  10942. out_err:
  10943. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10944. }
  10945. /**
  10946. * i40e_xdp_setup - add/remove an XDP program
  10947. * @vsi: VSI to changed
  10948. * @prog: XDP program
  10949. **/
  10950. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10951. struct bpf_prog *prog)
  10952. {
  10953. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10954. struct i40e_pf *pf = vsi->back;
  10955. struct bpf_prog *old_prog;
  10956. bool need_reset;
  10957. int i;
  10958. /* Don't allow frames that span over multiple buffers */
  10959. if (frame_size > vsi->rx_buf_len)
  10960. return -EINVAL;
  10961. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10962. return 0;
  10963. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10964. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10965. if (need_reset)
  10966. i40e_prep_for_reset(pf, true);
  10967. old_prog = xchg(&vsi->xdp_prog, prog);
  10968. if (need_reset) {
  10969. if (!prog)
  10970. /* Wait until ndo_xsk_wakeup completes. */
  10971. synchronize_rcu();
  10972. i40e_reset_and_rebuild(pf, true, true);
  10973. }
  10974. for (i = 0; i < vsi->num_queue_pairs; i++)
  10975. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10976. if (old_prog)
  10977. bpf_prog_put(old_prog);
  10978. /* Kick start the NAPI context if there is an AF_XDP socket open
  10979. * on that queue id. This so that receiving will start.
  10980. */
  10981. if (need_reset && prog)
  10982. for (i = 0; i < vsi->num_queue_pairs; i++)
  10983. if (vsi->xdp_rings[i]->xsk_umem)
  10984. (void)i40e_xsk_wakeup(vsi->netdev, i,
  10985. XDP_WAKEUP_RX);
  10986. return 0;
  10987. }
  10988. /**
  10989. * i40e_enter_busy_conf - Enters busy config state
  10990. * @vsi: vsi
  10991. *
  10992. * Returns 0 on success, <0 for failure.
  10993. **/
  10994. static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
  10995. {
  10996. struct i40e_pf *pf = vsi->back;
  10997. int timeout = 50;
  10998. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
  10999. timeout--;
  11000. if (!timeout)
  11001. return -EBUSY;
  11002. usleep_range(1000, 2000);
  11003. }
  11004. return 0;
  11005. }
  11006. /**
  11007. * i40e_exit_busy_conf - Exits busy config state
  11008. * @vsi: vsi
  11009. **/
  11010. static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
  11011. {
  11012. struct i40e_pf *pf = vsi->back;
  11013. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  11014. }
  11015. /**
  11016. * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
  11017. * @vsi: vsi
  11018. * @queue_pair: queue pair
  11019. **/
  11020. static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
  11021. {
  11022. memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
  11023. sizeof(vsi->rx_rings[queue_pair]->rx_stats));
  11024. memset(&vsi->tx_rings[queue_pair]->stats, 0,
  11025. sizeof(vsi->tx_rings[queue_pair]->stats));
  11026. if (i40e_enabled_xdp_vsi(vsi)) {
  11027. memset(&vsi->xdp_rings[queue_pair]->stats, 0,
  11028. sizeof(vsi->xdp_rings[queue_pair]->stats));
  11029. }
  11030. }
  11031. /**
  11032. * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
  11033. * @vsi: vsi
  11034. * @queue_pair: queue pair
  11035. **/
  11036. static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
  11037. {
  11038. i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
  11039. if (i40e_enabled_xdp_vsi(vsi)) {
  11040. /* Make sure that in-progress ndo_xdp_xmit calls are
  11041. * completed.
  11042. */
  11043. synchronize_rcu();
  11044. i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
  11045. }
  11046. i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
  11047. }
  11048. /**
  11049. * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
  11050. * @vsi: vsi
  11051. * @queue_pair: queue pair
  11052. * @enable: true for enable, false for disable
  11053. **/
  11054. static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
  11055. bool enable)
  11056. {
  11057. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11058. struct i40e_q_vector *q_vector = rxr->q_vector;
  11059. if (!vsi->netdev)
  11060. return;
  11061. /* All rings in a qp belong to the same qvector. */
  11062. if (q_vector->rx.ring || q_vector->tx.ring) {
  11063. if (enable)
  11064. napi_enable(&q_vector->napi);
  11065. else
  11066. napi_disable(&q_vector->napi);
  11067. }
  11068. }
  11069. /**
  11070. * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
  11071. * @vsi: vsi
  11072. * @queue_pair: queue pair
  11073. * @enable: true for enable, false for disable
  11074. *
  11075. * Returns 0 on success, <0 on failure.
  11076. **/
  11077. static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
  11078. bool enable)
  11079. {
  11080. struct i40e_pf *pf = vsi->back;
  11081. int pf_q, ret = 0;
  11082. pf_q = vsi->base_queue + queue_pair;
  11083. ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
  11084. false /*is xdp*/, enable);
  11085. if (ret) {
  11086. dev_info(&pf->pdev->dev,
  11087. "VSI seid %d Tx ring %d %sable timeout\n",
  11088. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11089. return ret;
  11090. }
  11091. i40e_control_rx_q(pf, pf_q, enable);
  11092. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  11093. if (ret) {
  11094. dev_info(&pf->pdev->dev,
  11095. "VSI seid %d Rx ring %d %sable timeout\n",
  11096. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11097. return ret;
  11098. }
  11099. /* Due to HW errata, on Rx disable only, the register can
  11100. * indicate done before it really is. Needs 50ms to be sure
  11101. */
  11102. if (!enable)
  11103. mdelay(50);
  11104. if (!i40e_enabled_xdp_vsi(vsi))
  11105. return ret;
  11106. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  11107. pf_q + vsi->alloc_queue_pairs,
  11108. true /*is xdp*/, enable);
  11109. if (ret) {
  11110. dev_info(&pf->pdev->dev,
  11111. "VSI seid %d XDP Tx ring %d %sable timeout\n",
  11112. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11113. }
  11114. return ret;
  11115. }
  11116. /**
  11117. * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
  11118. * @vsi: vsi
  11119. * @queue_pair: queue_pair
  11120. **/
  11121. static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
  11122. {
  11123. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11124. struct i40e_pf *pf = vsi->back;
  11125. struct i40e_hw *hw = &pf->hw;
  11126. /* All rings in a qp belong to the same qvector. */
  11127. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  11128. i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
  11129. else
  11130. i40e_irq_dynamic_enable_icr0(pf);
  11131. i40e_flush(hw);
  11132. }
  11133. /**
  11134. * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
  11135. * @vsi: vsi
  11136. * @queue_pair: queue_pair
  11137. **/
  11138. static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
  11139. {
  11140. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11141. struct i40e_pf *pf = vsi->back;
  11142. struct i40e_hw *hw = &pf->hw;
  11143. /* For simplicity, instead of removing the qp interrupt causes
  11144. * from the interrupt linked list, we simply disable the interrupt, and
  11145. * leave the list intact.
  11146. *
  11147. * All rings in a qp belong to the same qvector.
  11148. */
  11149. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  11150. u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
  11151. wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
  11152. i40e_flush(hw);
  11153. synchronize_irq(pf->msix_entries[intpf].vector);
  11154. } else {
  11155. /* Legacy and MSI mode - this stops all interrupt handling */
  11156. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  11157. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  11158. i40e_flush(hw);
  11159. synchronize_irq(pf->pdev->irq);
  11160. }
  11161. }
  11162. /**
  11163. * i40e_queue_pair_disable - Disables a queue pair
  11164. * @vsi: vsi
  11165. * @queue_pair: queue pair
  11166. *
  11167. * Returns 0 on success, <0 on failure.
  11168. **/
  11169. int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
  11170. {
  11171. int err;
  11172. err = i40e_enter_busy_conf(vsi);
  11173. if (err)
  11174. return err;
  11175. i40e_queue_pair_disable_irq(vsi, queue_pair);
  11176. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
  11177. i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
  11178. i40e_queue_pair_clean_rings(vsi, queue_pair);
  11179. i40e_queue_pair_reset_stats(vsi, queue_pair);
  11180. return err;
  11181. }
  11182. /**
  11183. * i40e_queue_pair_enable - Enables a queue pair
  11184. * @vsi: vsi
  11185. * @queue_pair: queue pair
  11186. *
  11187. * Returns 0 on success, <0 on failure.
  11188. **/
  11189. int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
  11190. {
  11191. int err;
  11192. err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
  11193. if (err)
  11194. return err;
  11195. if (i40e_enabled_xdp_vsi(vsi)) {
  11196. err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
  11197. if (err)
  11198. return err;
  11199. }
  11200. err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
  11201. if (err)
  11202. return err;
  11203. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
  11204. i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
  11205. i40e_queue_pair_enable_irq(vsi, queue_pair);
  11206. i40e_exit_busy_conf(vsi);
  11207. return err;
  11208. }
  11209. /**
  11210. * i40e_xdp - implements ndo_bpf for i40e
  11211. * @dev: netdevice
  11212. * @xdp: XDP command
  11213. **/
  11214. static int i40e_xdp(struct net_device *dev,
  11215. struct netdev_bpf *xdp)
  11216. {
  11217. struct i40e_netdev_priv *np = netdev_priv(dev);
  11218. struct i40e_vsi *vsi = np->vsi;
  11219. if (vsi->type != I40E_VSI_MAIN)
  11220. return -EINVAL;
  11221. switch (xdp->command) {
  11222. case XDP_SETUP_PROG:
  11223. return i40e_xdp_setup(vsi, xdp->prog);
  11224. case XDP_QUERY_PROG:
  11225. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  11226. return 0;
  11227. case XDP_SETUP_XSK_UMEM:
  11228. return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
  11229. xdp->xsk.queue_id);
  11230. default:
  11231. return -EINVAL;
  11232. }
  11233. }
  11234. static const struct net_device_ops i40e_netdev_ops = {
  11235. .ndo_open = i40e_open,
  11236. .ndo_stop = i40e_close,
  11237. .ndo_start_xmit = i40e_lan_xmit_frame,
  11238. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  11239. .ndo_set_rx_mode = i40e_set_rx_mode,
  11240. .ndo_validate_addr = eth_validate_addr,
  11241. .ndo_set_mac_address = i40e_set_mac,
  11242. .ndo_change_mtu = i40e_change_mtu,
  11243. .ndo_do_ioctl = i40e_ioctl,
  11244. .ndo_tx_timeout = i40e_tx_timeout,
  11245. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  11246. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  11247. #ifdef CONFIG_NET_POLL_CONTROLLER
  11248. .ndo_poll_controller = i40e_netpoll,
  11249. #endif
  11250. .ndo_setup_tc = __i40e_setup_tc,
  11251. .ndo_set_features = i40e_set_features,
  11252. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  11253. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  11254. .ndo_get_vf_stats = i40e_get_vf_stats,
  11255. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  11256. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  11257. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  11258. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  11259. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  11260. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  11261. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  11262. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  11263. .ndo_fdb_add = i40e_ndo_fdb_add,
  11264. .ndo_features_check = i40e_features_check,
  11265. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  11266. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  11267. .ndo_bpf = i40e_xdp,
  11268. .ndo_xdp_xmit = i40e_xdp_xmit,
  11269. .ndo_xsk_wakeup = i40e_xsk_wakeup,
  11270. .ndo_dfwd_add_station = i40e_fwd_add,
  11271. .ndo_dfwd_del_station = i40e_fwd_del,
  11272. };
  11273. /**
  11274. * i40e_config_netdev - Setup the netdev flags
  11275. * @vsi: the VSI being configured
  11276. *
  11277. * Returns 0 on success, negative value on failure
  11278. **/
  11279. static int i40e_config_netdev(struct i40e_vsi *vsi)
  11280. {
  11281. struct i40e_pf *pf = vsi->back;
  11282. struct i40e_hw *hw = &pf->hw;
  11283. struct i40e_netdev_priv *np;
  11284. struct net_device *netdev;
  11285. u8 broadcast[ETH_ALEN];
  11286. u8 mac_addr[ETH_ALEN];
  11287. int etherdev_size;
  11288. netdev_features_t hw_enc_features;
  11289. netdev_features_t hw_features;
  11290. etherdev_size = sizeof(struct i40e_netdev_priv);
  11291. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  11292. if (!netdev)
  11293. return -ENOMEM;
  11294. vsi->netdev = netdev;
  11295. np = netdev_priv(netdev);
  11296. np->vsi = vsi;
  11297. hw_enc_features = NETIF_F_SG |
  11298. NETIF_F_IP_CSUM |
  11299. NETIF_F_IPV6_CSUM |
  11300. NETIF_F_HIGHDMA |
  11301. NETIF_F_SOFT_FEATURES |
  11302. NETIF_F_TSO |
  11303. NETIF_F_TSO_ECN |
  11304. NETIF_F_TSO6 |
  11305. NETIF_F_GSO_GRE |
  11306. NETIF_F_GSO_GRE_CSUM |
  11307. NETIF_F_GSO_PARTIAL |
  11308. NETIF_F_GSO_IPXIP4 |
  11309. NETIF_F_GSO_IPXIP6 |
  11310. NETIF_F_GSO_UDP_TUNNEL |
  11311. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  11312. NETIF_F_GSO_UDP_L4 |
  11313. NETIF_F_SCTP_CRC |
  11314. NETIF_F_RXHASH |
  11315. NETIF_F_RXCSUM |
  11316. 0;
  11317. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  11318. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  11319. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  11320. netdev->hw_enc_features |= hw_enc_features;
  11321. /* record features VLANs can make use of */
  11322. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  11323. /* enable macvlan offloads */
  11324. netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  11325. hw_features = hw_enc_features |
  11326. NETIF_F_HW_VLAN_CTAG_TX |
  11327. NETIF_F_HW_VLAN_CTAG_RX;
  11328. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  11329. hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  11330. netdev->hw_features |= hw_features;
  11331. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  11332. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  11333. if (vsi->type == I40E_VSI_MAIN) {
  11334. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  11335. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  11336. /* The following steps are necessary for two reasons. First,
  11337. * some older NVM configurations load a default MAC-VLAN
  11338. * filter that will accept any tagged packet, and we want to
  11339. * replace this with a normal filter. Additionally, it is
  11340. * possible our MAC address was provided by the platform using
  11341. * Open Firmware or similar.
  11342. *
  11343. * Thus, we need to remove the default filter and install one
  11344. * specific to the MAC address.
  11345. */
  11346. i40e_rm_default_mac_filter(vsi, mac_addr);
  11347. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11348. i40e_add_mac_filter(vsi, mac_addr);
  11349. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11350. } else {
  11351. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  11352. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  11353. * the end, which is 4 bytes long, so force truncation of the
  11354. * original name by IFNAMSIZ - 4
  11355. */
  11356. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  11357. IFNAMSIZ - 4,
  11358. pf->vsi[pf->lan_vsi]->netdev->name);
  11359. eth_random_addr(mac_addr);
  11360. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11361. i40e_add_mac_filter(vsi, mac_addr);
  11362. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11363. }
  11364. /* Add the broadcast filter so that we initially will receive
  11365. * broadcast packets. Note that when a new VLAN is first added the
  11366. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  11367. * specific filters as part of transitioning into "vlan" operation.
  11368. * When more VLANs are added, the driver will copy each existing MAC
  11369. * filter and add it for the new VLAN.
  11370. *
  11371. * Broadcast filters are handled specially by
  11372. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  11373. * promiscuous bit instead of adding this directly as a MAC/VLAN
  11374. * filter. The subtask will update the correct broadcast promiscuous
  11375. * bits as VLANs become active or inactive.
  11376. */
  11377. eth_broadcast_addr(broadcast);
  11378. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11379. i40e_add_mac_filter(vsi, broadcast);
  11380. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11381. ether_addr_copy(netdev->dev_addr, mac_addr);
  11382. ether_addr_copy(netdev->perm_addr, mac_addr);
  11383. /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
  11384. netdev->neigh_priv_len = sizeof(u32) * 4;
  11385. netdev->priv_flags |= IFF_UNICAST_FLT;
  11386. netdev->priv_flags |= IFF_SUPP_NOFCS;
  11387. /* Setup netdev TC information */
  11388. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  11389. netdev->netdev_ops = &i40e_netdev_ops;
  11390. netdev->watchdog_timeo = 5 * HZ;
  11391. i40e_set_ethtool_ops(netdev);
  11392. /* MTU range: 68 - 9706 */
  11393. netdev->min_mtu = ETH_MIN_MTU;
  11394. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  11395. return 0;
  11396. }
  11397. /**
  11398. * i40e_vsi_delete - Delete a VSI from the switch
  11399. * @vsi: the VSI being removed
  11400. *
  11401. * Returns 0 on success, negative value on failure
  11402. **/
  11403. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  11404. {
  11405. /* remove default VSI is not allowed */
  11406. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  11407. return;
  11408. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  11409. }
  11410. /**
  11411. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  11412. * @vsi: the VSI being queried
  11413. *
  11414. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  11415. **/
  11416. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  11417. {
  11418. struct i40e_veb *veb;
  11419. struct i40e_pf *pf = vsi->back;
  11420. /* Uplink is not a bridge so default to VEB */
  11421. if (vsi->veb_idx >= I40E_MAX_VEB)
  11422. return 1;
  11423. veb = pf->veb[vsi->veb_idx];
  11424. if (!veb) {
  11425. dev_info(&pf->pdev->dev,
  11426. "There is no veb associated with the bridge\n");
  11427. return -ENOENT;
  11428. }
  11429. /* Uplink is a bridge in VEPA mode */
  11430. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  11431. return 0;
  11432. } else {
  11433. /* Uplink is a bridge in VEB mode */
  11434. return 1;
  11435. }
  11436. /* VEPA is now default bridge, so return 0 */
  11437. return 0;
  11438. }
  11439. /**
  11440. * i40e_add_vsi - Add a VSI to the switch
  11441. * @vsi: the VSI being configured
  11442. *
  11443. * This initializes a VSI context depending on the VSI type to be added and
  11444. * passes it down to the add_vsi aq command.
  11445. **/
  11446. static int i40e_add_vsi(struct i40e_vsi *vsi)
  11447. {
  11448. int ret = -ENODEV;
  11449. struct i40e_pf *pf = vsi->back;
  11450. struct i40e_hw *hw = &pf->hw;
  11451. struct i40e_vsi_context ctxt;
  11452. struct i40e_mac_filter *f;
  11453. struct hlist_node *h;
  11454. int bkt;
  11455. u8 enabled_tc = 0x1; /* TC0 enabled */
  11456. int f_count = 0;
  11457. memset(&ctxt, 0, sizeof(ctxt));
  11458. switch (vsi->type) {
  11459. case I40E_VSI_MAIN:
  11460. /* The PF's main VSI is already setup as part of the
  11461. * device initialization, so we'll not bother with
  11462. * the add_vsi call, but we will retrieve the current
  11463. * VSI context.
  11464. */
  11465. ctxt.seid = pf->main_vsi_seid;
  11466. ctxt.pf_num = pf->hw.pf_id;
  11467. ctxt.vf_num = 0;
  11468. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  11469. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  11470. if (ret) {
  11471. dev_info(&pf->pdev->dev,
  11472. "couldn't get PF vsi config, err %s aq_err %s\n",
  11473. i40e_stat_str(&pf->hw, ret),
  11474. i40e_aq_str(&pf->hw,
  11475. pf->hw.aq.asq_last_status));
  11476. return -ENOENT;
  11477. }
  11478. vsi->info = ctxt.info;
  11479. vsi->info.valid_sections = 0;
  11480. vsi->seid = ctxt.seid;
  11481. vsi->id = ctxt.vsi_number;
  11482. enabled_tc = i40e_pf_get_tc_map(pf);
  11483. /* Source pruning is enabled by default, so the flag is
  11484. * negative logic - if it's set, we need to fiddle with
  11485. * the VSI to disable source pruning.
  11486. */
  11487. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  11488. memset(&ctxt, 0, sizeof(ctxt));
  11489. ctxt.seid = pf->main_vsi_seid;
  11490. ctxt.pf_num = pf->hw.pf_id;
  11491. ctxt.vf_num = 0;
  11492. ctxt.info.valid_sections |=
  11493. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  11494. ctxt.info.switch_id =
  11495. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  11496. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  11497. if (ret) {
  11498. dev_info(&pf->pdev->dev,
  11499. "update vsi failed, err %s aq_err %s\n",
  11500. i40e_stat_str(&pf->hw, ret),
  11501. i40e_aq_str(&pf->hw,
  11502. pf->hw.aq.asq_last_status));
  11503. ret = -ENOENT;
  11504. goto err;
  11505. }
  11506. }
  11507. /* MFP mode setup queue map and update VSI */
  11508. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  11509. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  11510. memset(&ctxt, 0, sizeof(ctxt));
  11511. ctxt.seid = pf->main_vsi_seid;
  11512. ctxt.pf_num = pf->hw.pf_id;
  11513. ctxt.vf_num = 0;
  11514. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  11515. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  11516. if (ret) {
  11517. dev_info(&pf->pdev->dev,
  11518. "update vsi failed, err %s aq_err %s\n",
  11519. i40e_stat_str(&pf->hw, ret),
  11520. i40e_aq_str(&pf->hw,
  11521. pf->hw.aq.asq_last_status));
  11522. ret = -ENOENT;
  11523. goto err;
  11524. }
  11525. /* update the local VSI info queue map */
  11526. i40e_vsi_update_queue_map(vsi, &ctxt);
  11527. vsi->info.valid_sections = 0;
  11528. } else {
  11529. /* Default/Main VSI is only enabled for TC0
  11530. * reconfigure it to enable all TCs that are
  11531. * available on the port in SFP mode.
  11532. * For MFP case the iSCSI PF would use this
  11533. * flow to enable LAN+iSCSI TC.
  11534. */
  11535. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  11536. if (ret) {
  11537. /* Single TC condition is not fatal,
  11538. * message and continue
  11539. */
  11540. dev_info(&pf->pdev->dev,
  11541. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  11542. enabled_tc,
  11543. i40e_stat_str(&pf->hw, ret),
  11544. i40e_aq_str(&pf->hw,
  11545. pf->hw.aq.asq_last_status));
  11546. }
  11547. }
  11548. break;
  11549. case I40E_VSI_FDIR:
  11550. ctxt.pf_num = hw->pf_id;
  11551. ctxt.vf_num = 0;
  11552. ctxt.uplink_seid = vsi->uplink_seid;
  11553. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  11554. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  11555. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  11556. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  11557. ctxt.info.valid_sections |=
  11558. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  11559. ctxt.info.switch_id =
  11560. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  11561. }
  11562. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  11563. break;
  11564. case I40E_VSI_VMDQ2:
  11565. ctxt.pf_num = hw->pf_id;
  11566. ctxt.vf_num = 0;
  11567. ctxt.uplink_seid = vsi->uplink_seid;
  11568. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  11569. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  11570. /* This VSI is connected to VEB so the switch_id
  11571. * should be set to zero by default.
  11572. */
  11573. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  11574. ctxt.info.valid_sections |=
  11575. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  11576. ctxt.info.switch_id =
  11577. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  11578. }
  11579. /* Setup the VSI tx/rx queue map for TC0 only for now */
  11580. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  11581. break;
  11582. case I40E_VSI_SRIOV:
  11583. ctxt.pf_num = hw->pf_id;
  11584. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  11585. ctxt.uplink_seid = vsi->uplink_seid;
  11586. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  11587. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  11588. /* This VSI is connected to VEB so the switch_id
  11589. * should be set to zero by default.
  11590. */
  11591. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  11592. ctxt.info.valid_sections |=
  11593. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  11594. ctxt.info.switch_id =
  11595. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  11596. }
  11597. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  11598. ctxt.info.valid_sections |=
  11599. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  11600. ctxt.info.queueing_opt_flags |=
  11601. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  11602. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  11603. }
  11604. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  11605. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  11606. if (pf->vf[vsi->vf_id].spoofchk) {
  11607. ctxt.info.valid_sections |=
  11608. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  11609. ctxt.info.sec_flags |=
  11610. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  11611. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  11612. }
  11613. /* Setup the VSI tx/rx queue map for TC0 only for now */
  11614. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  11615. break;
  11616. case I40E_VSI_IWARP:
  11617. /* send down message to iWARP */
  11618. break;
  11619. default:
  11620. return -ENODEV;
  11621. }
  11622. if (vsi->type != I40E_VSI_MAIN) {
  11623. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  11624. if (ret) {
  11625. dev_info(&vsi->back->pdev->dev,
  11626. "add vsi failed, err %s aq_err %s\n",
  11627. i40e_stat_str(&pf->hw, ret),
  11628. i40e_aq_str(&pf->hw,
  11629. pf->hw.aq.asq_last_status));
  11630. ret = -ENOENT;
  11631. goto err;
  11632. }
  11633. vsi->info = ctxt.info;
  11634. vsi->info.valid_sections = 0;
  11635. vsi->seid = ctxt.seid;
  11636. vsi->id = ctxt.vsi_number;
  11637. }
  11638. vsi->active_filters = 0;
  11639. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  11640. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11641. /* If macvlan filters already exist, force them to get loaded */
  11642. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  11643. f->state = I40E_FILTER_NEW;
  11644. f_count++;
  11645. }
  11646. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11647. if (f_count) {
  11648. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  11649. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  11650. }
  11651. /* Update VSI BW information */
  11652. ret = i40e_vsi_get_bw_info(vsi);
  11653. if (ret) {
  11654. dev_info(&pf->pdev->dev,
  11655. "couldn't get vsi bw info, err %s aq_err %s\n",
  11656. i40e_stat_str(&pf->hw, ret),
  11657. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11658. /* VSI is already added so not tearing that up */
  11659. ret = 0;
  11660. }
  11661. err:
  11662. return ret;
  11663. }
  11664. /**
  11665. * i40e_vsi_release - Delete a VSI and free its resources
  11666. * @vsi: the VSI being removed
  11667. *
  11668. * Returns 0 on success or < 0 on error
  11669. **/
  11670. int i40e_vsi_release(struct i40e_vsi *vsi)
  11671. {
  11672. struct i40e_mac_filter *f;
  11673. struct hlist_node *h;
  11674. struct i40e_veb *veb = NULL;
  11675. struct i40e_pf *pf;
  11676. u16 uplink_seid;
  11677. int i, n, bkt;
  11678. pf = vsi->back;
  11679. /* release of a VEB-owner or last VSI is not allowed */
  11680. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  11681. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  11682. vsi->seid, vsi->uplink_seid);
  11683. return -ENODEV;
  11684. }
  11685. if (vsi == pf->vsi[pf->lan_vsi] &&
  11686. !test_bit(__I40E_DOWN, pf->state)) {
  11687. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  11688. return -ENODEV;
  11689. }
  11690. uplink_seid = vsi->uplink_seid;
  11691. if (vsi->type != I40E_VSI_SRIOV) {
  11692. if (vsi->netdev_registered) {
  11693. vsi->netdev_registered = false;
  11694. if (vsi->netdev) {
  11695. /* results in a call to i40e_close() */
  11696. unregister_netdev(vsi->netdev);
  11697. }
  11698. } else {
  11699. i40e_vsi_close(vsi);
  11700. }
  11701. i40e_vsi_disable_irq(vsi);
  11702. }
  11703. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11704. /* clear the sync flag on all filters */
  11705. if (vsi->netdev) {
  11706. __dev_uc_unsync(vsi->netdev, NULL);
  11707. __dev_mc_unsync(vsi->netdev, NULL);
  11708. }
  11709. /* make sure any remaining filters are marked for deletion */
  11710. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  11711. __i40e_del_filter(vsi, f);
  11712. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11713. i40e_sync_vsi_filters(vsi);
  11714. i40e_vsi_delete(vsi);
  11715. i40e_vsi_free_q_vectors(vsi);
  11716. if (vsi->netdev) {
  11717. free_netdev(vsi->netdev);
  11718. vsi->netdev = NULL;
  11719. }
  11720. i40e_vsi_clear_rings(vsi);
  11721. i40e_vsi_clear(vsi);
  11722. /* If this was the last thing on the VEB, except for the
  11723. * controlling VSI, remove the VEB, which puts the controlling
  11724. * VSI onto the next level down in the switch.
  11725. *
  11726. * Well, okay, there's one more exception here: don't remove
  11727. * the orphan VEBs yet. We'll wait for an explicit remove request
  11728. * from up the network stack.
  11729. */
  11730. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  11731. if (pf->vsi[i] &&
  11732. pf->vsi[i]->uplink_seid == uplink_seid &&
  11733. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11734. n++; /* count the VSIs */
  11735. }
  11736. }
  11737. for (i = 0; i < I40E_MAX_VEB; i++) {
  11738. if (!pf->veb[i])
  11739. continue;
  11740. if (pf->veb[i]->uplink_seid == uplink_seid)
  11741. n++; /* count the VEBs */
  11742. if (pf->veb[i]->seid == uplink_seid)
  11743. veb = pf->veb[i];
  11744. }
  11745. if (n == 0 && veb && veb->uplink_seid != 0)
  11746. i40e_veb_release(veb);
  11747. return 0;
  11748. }
  11749. /**
  11750. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  11751. * @vsi: ptr to the VSI
  11752. *
  11753. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  11754. * corresponding SW VSI structure and initializes num_queue_pairs for the
  11755. * newly allocated VSI.
  11756. *
  11757. * Returns 0 on success or negative on failure
  11758. **/
  11759. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  11760. {
  11761. int ret = -ENOENT;
  11762. struct i40e_pf *pf = vsi->back;
  11763. if (vsi->q_vectors[0]) {
  11764. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  11765. vsi->seid);
  11766. return -EEXIST;
  11767. }
  11768. if (vsi->base_vector) {
  11769. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  11770. vsi->seid, vsi->base_vector);
  11771. return -EEXIST;
  11772. }
  11773. ret = i40e_vsi_alloc_q_vectors(vsi);
  11774. if (ret) {
  11775. dev_info(&pf->pdev->dev,
  11776. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  11777. vsi->num_q_vectors, vsi->seid, ret);
  11778. vsi->num_q_vectors = 0;
  11779. goto vector_setup_out;
  11780. }
  11781. /* In Legacy mode, we do not have to get any other vector since we
  11782. * piggyback on the misc/ICR0 for queue interrupts.
  11783. */
  11784. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  11785. return ret;
  11786. if (vsi->num_q_vectors)
  11787. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  11788. vsi->num_q_vectors, vsi->idx);
  11789. if (vsi->base_vector < 0) {
  11790. dev_info(&pf->pdev->dev,
  11791. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  11792. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  11793. i40e_vsi_free_q_vectors(vsi);
  11794. ret = -ENOENT;
  11795. goto vector_setup_out;
  11796. }
  11797. vector_setup_out:
  11798. return ret;
  11799. }
  11800. /**
  11801. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  11802. * @vsi: pointer to the vsi.
  11803. *
  11804. * This re-allocates a vsi's queue resources.
  11805. *
  11806. * Returns pointer to the successfully allocated and configured VSI sw struct
  11807. * on success, otherwise returns NULL on failure.
  11808. **/
  11809. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  11810. {
  11811. u16 alloc_queue_pairs;
  11812. struct i40e_pf *pf;
  11813. u8 enabled_tc;
  11814. int ret;
  11815. if (!vsi)
  11816. return NULL;
  11817. pf = vsi->back;
  11818. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  11819. i40e_vsi_clear_rings(vsi);
  11820. i40e_vsi_free_arrays(vsi, false);
  11821. i40e_set_num_rings_in_vsi(vsi);
  11822. ret = i40e_vsi_alloc_arrays(vsi, false);
  11823. if (ret)
  11824. goto err_vsi;
  11825. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11826. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11827. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11828. if (ret < 0) {
  11829. dev_info(&pf->pdev->dev,
  11830. "failed to get tracking for %d queues for VSI %d err %d\n",
  11831. alloc_queue_pairs, vsi->seid, ret);
  11832. goto err_vsi;
  11833. }
  11834. vsi->base_queue = ret;
  11835. /* Update the FW view of the VSI. Force a reset of TC and queue
  11836. * layout configurations.
  11837. */
  11838. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11839. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11840. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11841. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11842. if (vsi->type == I40E_VSI_MAIN)
  11843. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  11844. /* assign it some queues */
  11845. ret = i40e_alloc_rings(vsi);
  11846. if (ret)
  11847. goto err_rings;
  11848. /* map all of the rings to the q_vectors */
  11849. i40e_vsi_map_rings_to_vectors(vsi);
  11850. return vsi;
  11851. err_rings:
  11852. i40e_vsi_free_q_vectors(vsi);
  11853. if (vsi->netdev_registered) {
  11854. vsi->netdev_registered = false;
  11855. unregister_netdev(vsi->netdev);
  11856. free_netdev(vsi->netdev);
  11857. vsi->netdev = NULL;
  11858. }
  11859. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11860. err_vsi:
  11861. i40e_vsi_clear(vsi);
  11862. return NULL;
  11863. }
  11864. /**
  11865. * i40e_vsi_setup - Set up a VSI by a given type
  11866. * @pf: board private structure
  11867. * @type: VSI type
  11868. * @uplink_seid: the switch element to link to
  11869. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  11870. *
  11871. * This allocates the sw VSI structure and its queue resources, then add a VSI
  11872. * to the identified VEB.
  11873. *
  11874. * Returns pointer to the successfully allocated and configure VSI sw struct on
  11875. * success, otherwise returns NULL on failure.
  11876. **/
  11877. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  11878. u16 uplink_seid, u32 param1)
  11879. {
  11880. struct i40e_vsi *vsi = NULL;
  11881. struct i40e_veb *veb = NULL;
  11882. u16 alloc_queue_pairs;
  11883. int ret, i;
  11884. int v_idx;
  11885. /* The requested uplink_seid must be either
  11886. * - the PF's port seid
  11887. * no VEB is needed because this is the PF
  11888. * or this is a Flow Director special case VSI
  11889. * - seid of an existing VEB
  11890. * - seid of a VSI that owns an existing VEB
  11891. * - seid of a VSI that doesn't own a VEB
  11892. * a new VEB is created and the VSI becomes the owner
  11893. * - seid of the PF VSI, which is what creates the first VEB
  11894. * this is a special case of the previous
  11895. *
  11896. * Find which uplink_seid we were given and create a new VEB if needed
  11897. */
  11898. for (i = 0; i < I40E_MAX_VEB; i++) {
  11899. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11900. veb = pf->veb[i];
  11901. break;
  11902. }
  11903. }
  11904. if (!veb && uplink_seid != pf->mac_seid) {
  11905. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11906. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11907. vsi = pf->vsi[i];
  11908. break;
  11909. }
  11910. }
  11911. if (!vsi) {
  11912. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11913. uplink_seid);
  11914. return NULL;
  11915. }
  11916. if (vsi->uplink_seid == pf->mac_seid)
  11917. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11918. vsi->tc_config.enabled_tc);
  11919. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11920. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11921. vsi->tc_config.enabled_tc);
  11922. if (veb) {
  11923. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11924. dev_info(&vsi->back->pdev->dev,
  11925. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11926. return NULL;
  11927. }
  11928. /* We come up by default in VEPA mode if SRIOV is not
  11929. * already enabled, in which case we can't force VEPA
  11930. * mode.
  11931. */
  11932. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11933. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11934. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11935. }
  11936. i40e_config_bridge_mode(veb);
  11937. }
  11938. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11939. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11940. veb = pf->veb[i];
  11941. }
  11942. if (!veb) {
  11943. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11944. return NULL;
  11945. }
  11946. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11947. uplink_seid = veb->seid;
  11948. }
  11949. /* get vsi sw struct */
  11950. v_idx = i40e_vsi_mem_alloc(pf, type);
  11951. if (v_idx < 0)
  11952. goto err_alloc;
  11953. vsi = pf->vsi[v_idx];
  11954. if (!vsi)
  11955. goto err_alloc;
  11956. vsi->type = type;
  11957. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11958. if (type == I40E_VSI_MAIN)
  11959. pf->lan_vsi = v_idx;
  11960. else if (type == I40E_VSI_SRIOV)
  11961. vsi->vf_id = param1;
  11962. /* assign it some queues */
  11963. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11964. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11965. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11966. if (ret < 0) {
  11967. dev_info(&pf->pdev->dev,
  11968. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11969. alloc_queue_pairs, vsi->seid, ret);
  11970. goto err_vsi;
  11971. }
  11972. vsi->base_queue = ret;
  11973. /* get a VSI from the hardware */
  11974. vsi->uplink_seid = uplink_seid;
  11975. ret = i40e_add_vsi(vsi);
  11976. if (ret)
  11977. goto err_vsi;
  11978. switch (vsi->type) {
  11979. /* setup the netdev if needed */
  11980. case I40E_VSI_MAIN:
  11981. case I40E_VSI_VMDQ2:
  11982. ret = i40e_config_netdev(vsi);
  11983. if (ret)
  11984. goto err_netdev;
  11985. ret = register_netdev(vsi->netdev);
  11986. if (ret)
  11987. goto err_netdev;
  11988. vsi->netdev_registered = true;
  11989. netif_carrier_off(vsi->netdev);
  11990. #ifdef CONFIG_I40E_DCB
  11991. /* Setup DCB netlink interface */
  11992. i40e_dcbnl_setup(vsi);
  11993. #endif /* CONFIG_I40E_DCB */
  11994. /* fall through */
  11995. case I40E_VSI_FDIR:
  11996. /* set up vectors and rings if needed */
  11997. ret = i40e_vsi_setup_vectors(vsi);
  11998. if (ret)
  11999. goto err_msix;
  12000. ret = i40e_alloc_rings(vsi);
  12001. if (ret)
  12002. goto err_rings;
  12003. /* map all of the rings to the q_vectors */
  12004. i40e_vsi_map_rings_to_vectors(vsi);
  12005. i40e_vsi_reset_stats(vsi);
  12006. break;
  12007. default:
  12008. /* no netdev or rings for the other VSI types */
  12009. break;
  12010. }
  12011. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  12012. (vsi->type == I40E_VSI_VMDQ2)) {
  12013. ret = i40e_vsi_config_rss(vsi);
  12014. }
  12015. return vsi;
  12016. err_rings:
  12017. i40e_vsi_free_q_vectors(vsi);
  12018. err_msix:
  12019. if (vsi->netdev_registered) {
  12020. vsi->netdev_registered = false;
  12021. unregister_netdev(vsi->netdev);
  12022. free_netdev(vsi->netdev);
  12023. vsi->netdev = NULL;
  12024. }
  12025. err_netdev:
  12026. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  12027. err_vsi:
  12028. i40e_vsi_clear(vsi);
  12029. err_alloc:
  12030. return NULL;
  12031. }
  12032. /**
  12033. * i40e_veb_get_bw_info - Query VEB BW information
  12034. * @veb: the veb to query
  12035. *
  12036. * Query the Tx scheduler BW configuration data for given VEB
  12037. **/
  12038. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  12039. {
  12040. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  12041. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  12042. struct i40e_pf *pf = veb->pf;
  12043. struct i40e_hw *hw = &pf->hw;
  12044. u32 tc_bw_max;
  12045. int ret = 0;
  12046. int i;
  12047. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  12048. &bw_data, NULL);
  12049. if (ret) {
  12050. dev_info(&pf->pdev->dev,
  12051. "query veb bw config failed, err %s aq_err %s\n",
  12052. i40e_stat_str(&pf->hw, ret),
  12053. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  12054. goto out;
  12055. }
  12056. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  12057. &ets_data, NULL);
  12058. if (ret) {
  12059. dev_info(&pf->pdev->dev,
  12060. "query veb bw ets config failed, err %s aq_err %s\n",
  12061. i40e_stat_str(&pf->hw, ret),
  12062. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  12063. goto out;
  12064. }
  12065. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  12066. veb->bw_max_quanta = ets_data.tc_bw_max;
  12067. veb->is_abs_credits = bw_data.absolute_credits_enable;
  12068. veb->enabled_tc = ets_data.tc_valid_bits;
  12069. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  12070. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  12071. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  12072. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  12073. veb->bw_tc_limit_credits[i] =
  12074. le16_to_cpu(bw_data.tc_bw_limits[i]);
  12075. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  12076. }
  12077. out:
  12078. return ret;
  12079. }
  12080. /**
  12081. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  12082. * @pf: board private structure
  12083. *
  12084. * On error: returns error code (negative)
  12085. * On success: returns vsi index in PF (positive)
  12086. **/
  12087. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  12088. {
  12089. int ret = -ENOENT;
  12090. struct i40e_veb *veb;
  12091. int i;
  12092. /* Need to protect the allocation of switch elements at the PF level */
  12093. mutex_lock(&pf->switch_mutex);
  12094. /* VEB list may be fragmented if VEB creation/destruction has
  12095. * been happening. We can afford to do a quick scan to look
  12096. * for any free slots in the list.
  12097. *
  12098. * find next empty veb slot, looping back around if necessary
  12099. */
  12100. i = 0;
  12101. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  12102. i++;
  12103. if (i >= I40E_MAX_VEB) {
  12104. ret = -ENOMEM;
  12105. goto err_alloc_veb; /* out of VEB slots! */
  12106. }
  12107. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  12108. if (!veb) {
  12109. ret = -ENOMEM;
  12110. goto err_alloc_veb;
  12111. }
  12112. veb->pf = pf;
  12113. veb->idx = i;
  12114. veb->enabled_tc = 1;
  12115. pf->veb[i] = veb;
  12116. ret = i;
  12117. err_alloc_veb:
  12118. mutex_unlock(&pf->switch_mutex);
  12119. return ret;
  12120. }
  12121. /**
  12122. * i40e_switch_branch_release - Delete a branch of the switch tree
  12123. * @branch: where to start deleting
  12124. *
  12125. * This uses recursion to find the tips of the branch to be
  12126. * removed, deleting until we get back to and can delete this VEB.
  12127. **/
  12128. static void i40e_switch_branch_release(struct i40e_veb *branch)
  12129. {
  12130. struct i40e_pf *pf = branch->pf;
  12131. u16 branch_seid = branch->seid;
  12132. u16 veb_idx = branch->idx;
  12133. int i;
  12134. /* release any VEBs on this VEB - RECURSION */
  12135. for (i = 0; i < I40E_MAX_VEB; i++) {
  12136. if (!pf->veb[i])
  12137. continue;
  12138. if (pf->veb[i]->uplink_seid == branch->seid)
  12139. i40e_switch_branch_release(pf->veb[i]);
  12140. }
  12141. /* Release the VSIs on this VEB, but not the owner VSI.
  12142. *
  12143. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  12144. * the VEB itself, so don't use (*branch) after this loop.
  12145. */
  12146. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12147. if (!pf->vsi[i])
  12148. continue;
  12149. if (pf->vsi[i]->uplink_seid == branch_seid &&
  12150. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  12151. i40e_vsi_release(pf->vsi[i]);
  12152. }
  12153. }
  12154. /* There's one corner case where the VEB might not have been
  12155. * removed, so double check it here and remove it if needed.
  12156. * This case happens if the veb was created from the debugfs
  12157. * commands and no VSIs were added to it.
  12158. */
  12159. if (pf->veb[veb_idx])
  12160. i40e_veb_release(pf->veb[veb_idx]);
  12161. }
  12162. /**
  12163. * i40e_veb_clear - remove veb struct
  12164. * @veb: the veb to remove
  12165. **/
  12166. static void i40e_veb_clear(struct i40e_veb *veb)
  12167. {
  12168. if (!veb)
  12169. return;
  12170. if (veb->pf) {
  12171. struct i40e_pf *pf = veb->pf;
  12172. mutex_lock(&pf->switch_mutex);
  12173. if (pf->veb[veb->idx] == veb)
  12174. pf->veb[veb->idx] = NULL;
  12175. mutex_unlock(&pf->switch_mutex);
  12176. }
  12177. kfree(veb);
  12178. }
  12179. /**
  12180. * i40e_veb_release - Delete a VEB and free its resources
  12181. * @veb: the VEB being removed
  12182. **/
  12183. void i40e_veb_release(struct i40e_veb *veb)
  12184. {
  12185. struct i40e_vsi *vsi = NULL;
  12186. struct i40e_pf *pf;
  12187. int i, n = 0;
  12188. pf = veb->pf;
  12189. /* find the remaining VSI and check for extras */
  12190. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12191. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  12192. n++;
  12193. vsi = pf->vsi[i];
  12194. }
  12195. }
  12196. if (n != 1) {
  12197. dev_info(&pf->pdev->dev,
  12198. "can't remove VEB %d with %d VSIs left\n",
  12199. veb->seid, n);
  12200. return;
  12201. }
  12202. /* move the remaining VSI to uplink veb */
  12203. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  12204. if (veb->uplink_seid) {
  12205. vsi->uplink_seid = veb->uplink_seid;
  12206. if (veb->uplink_seid == pf->mac_seid)
  12207. vsi->veb_idx = I40E_NO_VEB;
  12208. else
  12209. vsi->veb_idx = veb->veb_idx;
  12210. } else {
  12211. /* floating VEB */
  12212. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  12213. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  12214. }
  12215. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  12216. i40e_veb_clear(veb);
  12217. }
  12218. /**
  12219. * i40e_add_veb - create the VEB in the switch
  12220. * @veb: the VEB to be instantiated
  12221. * @vsi: the controlling VSI
  12222. **/
  12223. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  12224. {
  12225. struct i40e_pf *pf = veb->pf;
  12226. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  12227. int ret;
  12228. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  12229. veb->enabled_tc, false,
  12230. &veb->seid, enable_stats, NULL);
  12231. /* get a VEB from the hardware */
  12232. if (ret) {
  12233. dev_info(&pf->pdev->dev,
  12234. "couldn't add VEB, err %s aq_err %s\n",
  12235. i40e_stat_str(&pf->hw, ret),
  12236. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12237. return -EPERM;
  12238. }
  12239. /* get statistics counter */
  12240. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  12241. &veb->stats_idx, NULL, NULL, NULL);
  12242. if (ret) {
  12243. dev_info(&pf->pdev->dev,
  12244. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  12245. i40e_stat_str(&pf->hw, ret),
  12246. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12247. return -EPERM;
  12248. }
  12249. ret = i40e_veb_get_bw_info(veb);
  12250. if (ret) {
  12251. dev_info(&pf->pdev->dev,
  12252. "couldn't get VEB bw info, err %s aq_err %s\n",
  12253. i40e_stat_str(&pf->hw, ret),
  12254. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12255. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  12256. return -ENOENT;
  12257. }
  12258. vsi->uplink_seid = veb->seid;
  12259. vsi->veb_idx = veb->idx;
  12260. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  12261. return 0;
  12262. }
  12263. /**
  12264. * i40e_veb_setup - Set up a VEB
  12265. * @pf: board private structure
  12266. * @flags: VEB setup flags
  12267. * @uplink_seid: the switch element to link to
  12268. * @vsi_seid: the initial VSI seid
  12269. * @enabled_tc: Enabled TC bit-map
  12270. *
  12271. * This allocates the sw VEB structure and links it into the switch
  12272. * It is possible and legal for this to be a duplicate of an already
  12273. * existing VEB. It is also possible for both uplink and vsi seids
  12274. * to be zero, in order to create a floating VEB.
  12275. *
  12276. * Returns pointer to the successfully allocated VEB sw struct on
  12277. * success, otherwise returns NULL on failure.
  12278. **/
  12279. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  12280. u16 uplink_seid, u16 vsi_seid,
  12281. u8 enabled_tc)
  12282. {
  12283. struct i40e_veb *veb, *uplink_veb = NULL;
  12284. int vsi_idx, veb_idx;
  12285. int ret;
  12286. /* if one seid is 0, the other must be 0 to create a floating relay */
  12287. if ((uplink_seid == 0 || vsi_seid == 0) &&
  12288. (uplink_seid + vsi_seid != 0)) {
  12289. dev_info(&pf->pdev->dev,
  12290. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  12291. uplink_seid, vsi_seid);
  12292. return NULL;
  12293. }
  12294. /* make sure there is such a vsi and uplink */
  12295. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  12296. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  12297. break;
  12298. if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
  12299. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  12300. vsi_seid);
  12301. return NULL;
  12302. }
  12303. if (uplink_seid && uplink_seid != pf->mac_seid) {
  12304. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  12305. if (pf->veb[veb_idx] &&
  12306. pf->veb[veb_idx]->seid == uplink_seid) {
  12307. uplink_veb = pf->veb[veb_idx];
  12308. break;
  12309. }
  12310. }
  12311. if (!uplink_veb) {
  12312. dev_info(&pf->pdev->dev,
  12313. "uplink seid %d not found\n", uplink_seid);
  12314. return NULL;
  12315. }
  12316. }
  12317. /* get veb sw struct */
  12318. veb_idx = i40e_veb_mem_alloc(pf);
  12319. if (veb_idx < 0)
  12320. goto err_alloc;
  12321. veb = pf->veb[veb_idx];
  12322. veb->flags = flags;
  12323. veb->uplink_seid = uplink_seid;
  12324. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  12325. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  12326. /* create the VEB in the switch */
  12327. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  12328. if (ret)
  12329. goto err_veb;
  12330. if (vsi_idx == pf->lan_vsi)
  12331. pf->lan_veb = veb->idx;
  12332. return veb;
  12333. err_veb:
  12334. i40e_veb_clear(veb);
  12335. err_alloc:
  12336. return NULL;
  12337. }
  12338. /**
  12339. * i40e_setup_pf_switch_element - set PF vars based on switch type
  12340. * @pf: board private structure
  12341. * @ele: element we are building info from
  12342. * @num_reported: total number of elements
  12343. * @printconfig: should we print the contents
  12344. *
  12345. * helper function to assist in extracting a few useful SEID values.
  12346. **/
  12347. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  12348. struct i40e_aqc_switch_config_element_resp *ele,
  12349. u16 num_reported, bool printconfig)
  12350. {
  12351. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  12352. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  12353. u8 element_type = ele->element_type;
  12354. u16 seid = le16_to_cpu(ele->seid);
  12355. if (printconfig)
  12356. dev_info(&pf->pdev->dev,
  12357. "type=%d seid=%d uplink=%d downlink=%d\n",
  12358. element_type, seid, uplink_seid, downlink_seid);
  12359. switch (element_type) {
  12360. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  12361. pf->mac_seid = seid;
  12362. break;
  12363. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  12364. /* Main VEB? */
  12365. if (uplink_seid != pf->mac_seid)
  12366. break;
  12367. if (pf->lan_veb >= I40E_MAX_VEB) {
  12368. int v;
  12369. /* find existing or else empty VEB */
  12370. for (v = 0; v < I40E_MAX_VEB; v++) {
  12371. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  12372. pf->lan_veb = v;
  12373. break;
  12374. }
  12375. }
  12376. if (pf->lan_veb >= I40E_MAX_VEB) {
  12377. v = i40e_veb_mem_alloc(pf);
  12378. if (v < 0)
  12379. break;
  12380. pf->lan_veb = v;
  12381. }
  12382. }
  12383. if (pf->lan_veb >= I40E_MAX_VEB)
  12384. break;
  12385. pf->veb[pf->lan_veb]->seid = seid;
  12386. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  12387. pf->veb[pf->lan_veb]->pf = pf;
  12388. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  12389. break;
  12390. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  12391. if (num_reported != 1)
  12392. break;
  12393. /* This is immediately after a reset so we can assume this is
  12394. * the PF's VSI
  12395. */
  12396. pf->mac_seid = uplink_seid;
  12397. pf->pf_seid = downlink_seid;
  12398. pf->main_vsi_seid = seid;
  12399. if (printconfig)
  12400. dev_info(&pf->pdev->dev,
  12401. "pf_seid=%d main_vsi_seid=%d\n",
  12402. pf->pf_seid, pf->main_vsi_seid);
  12403. break;
  12404. case I40E_SWITCH_ELEMENT_TYPE_PF:
  12405. case I40E_SWITCH_ELEMENT_TYPE_VF:
  12406. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  12407. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  12408. case I40E_SWITCH_ELEMENT_TYPE_PE:
  12409. case I40E_SWITCH_ELEMENT_TYPE_PA:
  12410. /* ignore these for now */
  12411. break;
  12412. default:
  12413. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  12414. element_type, seid);
  12415. break;
  12416. }
  12417. }
  12418. /**
  12419. * i40e_fetch_switch_configuration - Get switch config from firmware
  12420. * @pf: board private structure
  12421. * @printconfig: should we print the contents
  12422. *
  12423. * Get the current switch configuration from the device and
  12424. * extract a few useful SEID values.
  12425. **/
  12426. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  12427. {
  12428. struct i40e_aqc_get_switch_config_resp *sw_config;
  12429. u16 next_seid = 0;
  12430. int ret = 0;
  12431. u8 *aq_buf;
  12432. int i;
  12433. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  12434. if (!aq_buf)
  12435. return -ENOMEM;
  12436. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  12437. do {
  12438. u16 num_reported, num_total;
  12439. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  12440. I40E_AQ_LARGE_BUF,
  12441. &next_seid, NULL);
  12442. if (ret) {
  12443. dev_info(&pf->pdev->dev,
  12444. "get switch config failed err %s aq_err %s\n",
  12445. i40e_stat_str(&pf->hw, ret),
  12446. i40e_aq_str(&pf->hw,
  12447. pf->hw.aq.asq_last_status));
  12448. kfree(aq_buf);
  12449. return -ENOENT;
  12450. }
  12451. num_reported = le16_to_cpu(sw_config->header.num_reported);
  12452. num_total = le16_to_cpu(sw_config->header.num_total);
  12453. if (printconfig)
  12454. dev_info(&pf->pdev->dev,
  12455. "header: %d reported %d total\n",
  12456. num_reported, num_total);
  12457. for (i = 0; i < num_reported; i++) {
  12458. struct i40e_aqc_switch_config_element_resp *ele =
  12459. &sw_config->element[i];
  12460. i40e_setup_pf_switch_element(pf, ele, num_reported,
  12461. printconfig);
  12462. }
  12463. } while (next_seid != 0);
  12464. kfree(aq_buf);
  12465. return ret;
  12466. }
  12467. /**
  12468. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  12469. * @pf: board private structure
  12470. * @reinit: if the Main VSI needs to re-initialized.
  12471. *
  12472. * Returns 0 on success, negative value on failure
  12473. **/
  12474. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  12475. {
  12476. u16 flags = 0;
  12477. int ret;
  12478. /* find out what's out there already */
  12479. ret = i40e_fetch_switch_configuration(pf, false);
  12480. if (ret) {
  12481. dev_info(&pf->pdev->dev,
  12482. "couldn't fetch switch config, err %s aq_err %s\n",
  12483. i40e_stat_str(&pf->hw, ret),
  12484. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12485. return ret;
  12486. }
  12487. i40e_pf_reset_stats(pf);
  12488. /* set the switch config bit for the whole device to
  12489. * support limited promisc or true promisc
  12490. * when user requests promisc. The default is limited
  12491. * promisc.
  12492. */
  12493. if ((pf->hw.pf_id == 0) &&
  12494. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  12495. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  12496. pf->last_sw_conf_flags = flags;
  12497. }
  12498. if (pf->hw.pf_id == 0) {
  12499. u16 valid_flags;
  12500. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  12501. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  12502. NULL);
  12503. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  12504. dev_info(&pf->pdev->dev,
  12505. "couldn't set switch config bits, err %s aq_err %s\n",
  12506. i40e_stat_str(&pf->hw, ret),
  12507. i40e_aq_str(&pf->hw,
  12508. pf->hw.aq.asq_last_status));
  12509. /* not a fatal problem, just keep going */
  12510. }
  12511. pf->last_sw_conf_valid_flags = valid_flags;
  12512. }
  12513. /* first time setup */
  12514. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  12515. struct i40e_vsi *vsi = NULL;
  12516. u16 uplink_seid;
  12517. /* Set up the PF VSI associated with the PF's main VSI
  12518. * that is already in the HW switch
  12519. */
  12520. if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
  12521. uplink_seid = pf->veb[pf->lan_veb]->seid;
  12522. else
  12523. uplink_seid = pf->mac_seid;
  12524. if (pf->lan_vsi == I40E_NO_VSI)
  12525. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  12526. else if (reinit)
  12527. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  12528. if (!vsi) {
  12529. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  12530. i40e_cloud_filter_exit(pf);
  12531. i40e_fdir_teardown(pf);
  12532. return -EAGAIN;
  12533. }
  12534. } else {
  12535. /* force a reset of TC and queue layout configurations */
  12536. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  12537. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  12538. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  12539. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  12540. }
  12541. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  12542. i40e_fdir_sb_setup(pf);
  12543. /* Setup static PF queue filter control settings */
  12544. ret = i40e_setup_pf_filter_control(pf);
  12545. if (ret) {
  12546. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  12547. ret);
  12548. /* Failure here should not stop continuing other steps */
  12549. }
  12550. /* enable RSS in the HW, even for only one queue, as the stack can use
  12551. * the hash
  12552. */
  12553. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  12554. i40e_pf_config_rss(pf);
  12555. /* fill in link information and enable LSE reporting */
  12556. i40e_link_event(pf);
  12557. /* Initialize user-specific link properties */
  12558. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  12559. I40E_AQ_AN_COMPLETED) ? true : false);
  12560. i40e_ptp_init(pf);
  12561. /* repopulate tunnel port filters */
  12562. i40e_sync_udp_filters(pf);
  12563. return ret;
  12564. }
  12565. /**
  12566. * i40e_determine_queue_usage - Work out queue distribution
  12567. * @pf: board private structure
  12568. **/
  12569. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  12570. {
  12571. int queues_left;
  12572. int q_max;
  12573. pf->num_lan_qps = 0;
  12574. /* Find the max queues to be put into basic use. We'll always be
  12575. * using TC0, whether or not DCB is running, and TC0 will get the
  12576. * big RSS set.
  12577. */
  12578. queues_left = pf->hw.func_caps.num_tx_qp;
  12579. if ((queues_left == 1) ||
  12580. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  12581. /* one qp for PF, no queues for anything else */
  12582. queues_left = 0;
  12583. pf->alloc_rss_size = pf->num_lan_qps = 1;
  12584. /* make sure all the fancies are disabled */
  12585. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  12586. I40E_FLAG_IWARP_ENABLED |
  12587. I40E_FLAG_FD_SB_ENABLED |
  12588. I40E_FLAG_FD_ATR_ENABLED |
  12589. I40E_FLAG_DCB_CAPABLE |
  12590. I40E_FLAG_DCB_ENABLED |
  12591. I40E_FLAG_SRIOV_ENABLED |
  12592. I40E_FLAG_VMDQ_ENABLED);
  12593. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  12594. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  12595. I40E_FLAG_FD_SB_ENABLED |
  12596. I40E_FLAG_FD_ATR_ENABLED |
  12597. I40E_FLAG_DCB_CAPABLE))) {
  12598. /* one qp for PF */
  12599. pf->alloc_rss_size = pf->num_lan_qps = 1;
  12600. queues_left -= pf->num_lan_qps;
  12601. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  12602. I40E_FLAG_IWARP_ENABLED |
  12603. I40E_FLAG_FD_SB_ENABLED |
  12604. I40E_FLAG_FD_ATR_ENABLED |
  12605. I40E_FLAG_DCB_ENABLED |
  12606. I40E_FLAG_VMDQ_ENABLED);
  12607. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  12608. } else {
  12609. /* Not enough queues for all TCs */
  12610. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  12611. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  12612. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  12613. I40E_FLAG_DCB_ENABLED);
  12614. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  12615. }
  12616. /* limit lan qps to the smaller of qps, cpus or msix */
  12617. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  12618. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  12619. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  12620. pf->num_lan_qps = q_max;
  12621. queues_left -= pf->num_lan_qps;
  12622. }
  12623. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  12624. if (queues_left > 1) {
  12625. queues_left -= 1; /* save 1 queue for FD */
  12626. } else {
  12627. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  12628. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  12629. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  12630. }
  12631. }
  12632. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12633. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  12634. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  12635. (queues_left / pf->num_vf_qps));
  12636. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  12637. }
  12638. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  12639. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  12640. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  12641. (queues_left / pf->num_vmdq_qps));
  12642. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  12643. }
  12644. pf->queues_left = queues_left;
  12645. dev_dbg(&pf->pdev->dev,
  12646. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  12647. pf->hw.func_caps.num_tx_qp,
  12648. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  12649. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  12650. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  12651. queues_left);
  12652. }
  12653. /**
  12654. * i40e_setup_pf_filter_control - Setup PF static filter control
  12655. * @pf: PF to be setup
  12656. *
  12657. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  12658. * settings. If PE/FCoE are enabled then it will also set the per PF
  12659. * based filter sizes required for them. It also enables Flow director,
  12660. * ethertype and macvlan type filter settings for the pf.
  12661. *
  12662. * Returns 0 on success, negative on failure
  12663. **/
  12664. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  12665. {
  12666. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  12667. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  12668. /* Flow Director is enabled */
  12669. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  12670. settings->enable_fdir = true;
  12671. /* Ethtype and MACVLAN filters enabled for PF */
  12672. settings->enable_ethtype = true;
  12673. settings->enable_macvlan = true;
  12674. if (i40e_set_filter_control(&pf->hw, settings))
  12675. return -ENOENT;
  12676. return 0;
  12677. }
  12678. #define INFO_STRING_LEN 255
  12679. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  12680. static void i40e_print_features(struct i40e_pf *pf)
  12681. {
  12682. struct i40e_hw *hw = &pf->hw;
  12683. char *buf;
  12684. int i;
  12685. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  12686. if (!buf)
  12687. return;
  12688. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  12689. #ifdef CONFIG_PCI_IOV
  12690. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  12691. #endif
  12692. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  12693. pf->hw.func_caps.num_vsis,
  12694. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  12695. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  12696. i += snprintf(&buf[i], REMAIN(i), " RSS");
  12697. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  12698. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  12699. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  12700. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  12701. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  12702. }
  12703. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  12704. i += snprintf(&buf[i], REMAIN(i), " DCB");
  12705. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  12706. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  12707. if (pf->flags & I40E_FLAG_PTP)
  12708. i += snprintf(&buf[i], REMAIN(i), " PTP");
  12709. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  12710. i += snprintf(&buf[i], REMAIN(i), " VEB");
  12711. else
  12712. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  12713. dev_info(&pf->pdev->dev, "%s\n", buf);
  12714. kfree(buf);
  12715. WARN_ON(i > INFO_STRING_LEN);
  12716. }
  12717. /**
  12718. * i40e_get_platform_mac_addr - get platform-specific MAC address
  12719. * @pdev: PCI device information struct
  12720. * @pf: board private structure
  12721. *
  12722. * Look up the MAC address for the device. First we'll try
  12723. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  12724. * specific fallback. Otherwise, we'll default to the stored value in
  12725. * firmware.
  12726. **/
  12727. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  12728. {
  12729. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  12730. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  12731. }
  12732. /**
  12733. * i40e_set_fec_in_flags - helper function for setting FEC options in flags
  12734. * @fec_cfg: FEC option to set in flags
  12735. * @flags: ptr to flags in which we set FEC option
  12736. **/
  12737. void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
  12738. {
  12739. if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
  12740. *flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
  12741. if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
  12742. (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
  12743. *flags |= I40E_FLAG_RS_FEC;
  12744. *flags &= ~I40E_FLAG_BASE_R_FEC;
  12745. }
  12746. if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
  12747. (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
  12748. *flags |= I40E_FLAG_BASE_R_FEC;
  12749. *flags &= ~I40E_FLAG_RS_FEC;
  12750. }
  12751. if (fec_cfg == 0)
  12752. *flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
  12753. }
  12754. /**
  12755. * i40e_check_recovery_mode - check if we are running transition firmware
  12756. * @pf: board private structure
  12757. *
  12758. * Check registers indicating the firmware runs in recovery mode. Sets the
  12759. * appropriate driver state.
  12760. *
  12761. * Returns true if the recovery mode was detected, false otherwise
  12762. **/
  12763. static bool i40e_check_recovery_mode(struct i40e_pf *pf)
  12764. {
  12765. u32 val = rd32(&pf->hw, I40E_GL_FWSTS) & I40E_GL_FWSTS_FWS1B_MASK;
  12766. bool is_recovery_mode = false;
  12767. if (pf->hw.mac.type == I40E_MAC_XL710)
  12768. is_recovery_mode =
  12769. val == I40E_XL710_GL_FWSTS_FWS1B_REC_MOD_CORER_MASK ||
  12770. val == I40E_XL710_GL_FWSTS_FWS1B_REC_MOD_GLOBR_MASK ||
  12771. val == I40E_XL710_GL_FWSTS_FWS1B_REC_MOD_TRANSITION_MASK ||
  12772. val == I40E_XL710_GL_FWSTS_FWS1B_REC_MOD_NVM_MASK;
  12773. if (pf->hw.mac.type == I40E_MAC_X722)
  12774. is_recovery_mode =
  12775. val == I40E_X722_GL_FWSTS_FWS1B_REC_MOD_CORER_MASK ||
  12776. val == I40E_X722_GL_FWSTS_FWS1B_REC_MOD_GLOBR_MASK;
  12777. if (is_recovery_mode) {
  12778. dev_notice(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
  12779. dev_notice(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
  12780. set_bit(__I40E_RECOVERY_MODE, pf->state);
  12781. return true;
  12782. }
  12783. if (test_and_clear_bit(__I40E_RECOVERY_MODE, pf->state))
  12784. dev_info(&pf->pdev->dev, "Reinitializing in normal mode with full functionality.\n");
  12785. return false;
  12786. }
  12787. /**
  12788. * i40e_pf_loop_reset - perform reset in a loop.
  12789. * @pf: board private structure
  12790. *
  12791. * This function is useful when a NIC is about to enter recovery mode.
  12792. * When a NIC's internal data structures are corrupted the NIC's
  12793. * firmware is going to enter recovery mode.
  12794. * Right after a POR it takes about 7 minutes for firmware to enter
  12795. * recovery mode. Until that time a NIC is in some kind of intermediate
  12796. * state. After that time period the NIC almost surely enters
  12797. * recovery mode. The only way for a driver to detect intermediate
  12798. * state is to issue a series of pf-resets and check a return value.
  12799. * If a PF reset returns success then the firmware could be in recovery
  12800. * mode so the caller of this code needs to check for recovery mode
  12801. * if this function returns success. There is a little chance that
  12802. * firmware will hang in intermediate state forever.
  12803. * Since waiting 7 minutes is quite a lot of time this function waits
  12804. * 10 seconds and then gives up by returning an error.
  12805. *
  12806. * Return 0 on success, negative on failure.
  12807. **/
  12808. static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
  12809. {
  12810. const unsigned short MAX_CNT = 1000;
  12811. const unsigned short MSECS = 10;
  12812. struct i40e_hw *hw = &pf->hw;
  12813. i40e_status ret;
  12814. int cnt;
  12815. for (cnt = 0; cnt < MAX_CNT; ++cnt) {
  12816. ret = i40e_pf_reset(hw);
  12817. if (!ret)
  12818. break;
  12819. msleep(MSECS);
  12820. }
  12821. if (cnt == MAX_CNT) {
  12822. dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
  12823. return ret;
  12824. }
  12825. pf->pfr_count++;
  12826. return ret;
  12827. }
  12828. /**
  12829. * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
  12830. * @pf: board private structure
  12831. * @hw: ptr to the hardware info
  12832. *
  12833. * This function does a minimal setup of all subsystems needed for running
  12834. * recovery mode.
  12835. *
  12836. * Returns 0 on success, negative on failure
  12837. **/
  12838. static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
  12839. {
  12840. struct i40e_vsi *vsi;
  12841. int err;
  12842. int v_idx;
  12843. pci_save_state(pf->pdev);
  12844. /* set up periodic task facility */
  12845. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12846. pf->service_timer_period = HZ;
  12847. INIT_WORK(&pf->service_task, i40e_service_task);
  12848. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12849. err = i40e_init_interrupt_scheme(pf);
  12850. if (err)
  12851. goto err_switch_setup;
  12852. /* The number of VSIs reported by the FW is the minimum guaranteed
  12853. * to us; HW supports far more and we share the remaining pool with
  12854. * the other PFs. We allocate space for more than the guarantee with
  12855. * the understanding that we might not get them all later.
  12856. */
  12857. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12858. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12859. else
  12860. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12861. /* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
  12862. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12863. GFP_KERNEL);
  12864. if (!pf->vsi) {
  12865. err = -ENOMEM;
  12866. goto err_switch_setup;
  12867. }
  12868. /* We allocate one VSI which is needed as absolute minimum
  12869. * in order to register the netdev
  12870. */
  12871. v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
  12872. if (v_idx < 0)
  12873. goto err_switch_setup;
  12874. pf->lan_vsi = v_idx;
  12875. vsi = pf->vsi[v_idx];
  12876. if (!vsi)
  12877. goto err_switch_setup;
  12878. vsi->alloc_queue_pairs = 1;
  12879. err = i40e_config_netdev(vsi);
  12880. if (err)
  12881. goto err_switch_setup;
  12882. err = register_netdev(vsi->netdev);
  12883. if (err)
  12884. goto err_switch_setup;
  12885. vsi->netdev_registered = true;
  12886. i40e_dbg_pf_init(pf);
  12887. err = i40e_setup_misc_vector_for_recovery_mode(pf);
  12888. if (err)
  12889. goto err_switch_setup;
  12890. /* tell the firmware that we're starting */
  12891. i40e_send_version(pf);
  12892. /* since everything's happy, start the service_task timer */
  12893. mod_timer(&pf->service_timer,
  12894. round_jiffies(jiffies + pf->service_timer_period));
  12895. return 0;
  12896. err_switch_setup:
  12897. i40e_reset_interrupt_capability(pf);
  12898. del_timer_sync(&pf->service_timer);
  12899. i40e_shutdown_adminq(hw);
  12900. iounmap(hw->hw_addr);
  12901. pci_disable_pcie_error_reporting(pf->pdev);
  12902. pci_release_mem_regions(pf->pdev);
  12903. pci_disable_device(pf->pdev);
  12904. kfree(pf);
  12905. return err;
  12906. }
  12907. /**
  12908. * i40e_probe - Device initialization routine
  12909. * @pdev: PCI device information struct
  12910. * @ent: entry in i40e_pci_tbl
  12911. *
  12912. * i40e_probe initializes a PF identified by a pci_dev structure.
  12913. * The OS initialization, configuring of the PF private structure,
  12914. * and a hardware reset occur.
  12915. *
  12916. * Returns 0 on success, negative on failure
  12917. **/
  12918. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  12919. {
  12920. struct i40e_aq_get_phy_abilities_resp abilities;
  12921. struct i40e_pf *pf;
  12922. struct i40e_hw *hw;
  12923. static u16 pfs_found;
  12924. u16 wol_nvm_bits;
  12925. u16 link_status;
  12926. int err;
  12927. u32 val;
  12928. u32 i;
  12929. u8 set_fc_aq_fail;
  12930. err = pci_enable_device_mem(pdev);
  12931. if (err)
  12932. return err;
  12933. /* set up for high or low dma */
  12934. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  12935. if (err) {
  12936. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  12937. if (err) {
  12938. dev_err(&pdev->dev,
  12939. "DMA configuration failed: 0x%x\n", err);
  12940. goto err_dma;
  12941. }
  12942. }
  12943. /* set up pci connections */
  12944. err = pci_request_mem_regions(pdev, i40e_driver_name);
  12945. if (err) {
  12946. dev_info(&pdev->dev,
  12947. "pci_request_selected_regions failed %d\n", err);
  12948. goto err_pci_reg;
  12949. }
  12950. pci_enable_pcie_error_reporting(pdev);
  12951. pci_set_master(pdev);
  12952. /* Now that we have a PCI connection, we need to do the
  12953. * low level device setup. This is primarily setting up
  12954. * the Admin Queue structures and then querying for the
  12955. * device's current profile information.
  12956. */
  12957. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  12958. if (!pf) {
  12959. err = -ENOMEM;
  12960. goto err_pf_alloc;
  12961. }
  12962. pf->next_vsi = 0;
  12963. pf->pdev = pdev;
  12964. set_bit(__I40E_DOWN, pf->state);
  12965. hw = &pf->hw;
  12966. hw->back = pf;
  12967. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  12968. I40E_MAX_CSR_SPACE);
  12969. /* We believe that the highest register to read is
  12970. * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
  12971. * is not less than that before mapping to prevent a
  12972. * kernel panic.
  12973. */
  12974. if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
  12975. dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
  12976. pf->ioremap_len);
  12977. err = -ENOMEM;
  12978. goto err_ioremap;
  12979. }
  12980. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  12981. if (!hw->hw_addr) {
  12982. err = -EIO;
  12983. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  12984. (unsigned int)pci_resource_start(pdev, 0),
  12985. pf->ioremap_len, err);
  12986. goto err_ioremap;
  12987. }
  12988. hw->vendor_id = pdev->vendor;
  12989. hw->device_id = pdev->device;
  12990. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  12991. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  12992. hw->subsystem_device_id = pdev->subsystem_device;
  12993. hw->bus.device = PCI_SLOT(pdev->devfn);
  12994. hw->bus.func = PCI_FUNC(pdev->devfn);
  12995. hw->bus.bus_id = pdev->bus->number;
  12996. pf->instance = pfs_found;
  12997. /* Select something other than the 802.1ad ethertype for the
  12998. * switch to use internally and drop on ingress.
  12999. */
  13000. hw->switch_tag = 0xffff;
  13001. hw->first_tag = ETH_P_8021AD;
  13002. hw->second_tag = ETH_P_8021Q;
  13003. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  13004. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  13005. INIT_LIST_HEAD(&pf->ddp_old_prof);
  13006. /* set up the locks for the AQ, do this only once in probe
  13007. * and destroy them only once in remove
  13008. */
  13009. mutex_init(&hw->aq.asq_mutex);
  13010. mutex_init(&hw->aq.arq_mutex);
  13011. pf->msg_enable = netif_msg_init(debug,
  13012. NETIF_MSG_DRV |
  13013. NETIF_MSG_PROBE |
  13014. NETIF_MSG_LINK);
  13015. if (debug < -1)
  13016. pf->hw.debug_mask = debug;
  13017. /* do a special CORER for clearing PXE mode once at init */
  13018. if (hw->revision_id == 0 &&
  13019. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  13020. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  13021. i40e_flush(hw);
  13022. msleep(200);
  13023. pf->corer_count++;
  13024. i40e_clear_pxe_mode(hw);
  13025. }
  13026. /* Reset here to make sure all is clean and to define PF 'n' */
  13027. i40e_clear_hw(hw);
  13028. err = i40e_set_mac_type(hw);
  13029. if (err) {
  13030. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  13031. err);
  13032. goto err_pf_reset;
  13033. }
  13034. err = i40e_pf_loop_reset(pf);
  13035. if (err) {
  13036. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  13037. goto err_pf_reset;
  13038. }
  13039. i40e_check_recovery_mode(pf);
  13040. hw->aq.num_arq_entries = I40E_AQ_LEN;
  13041. hw->aq.num_asq_entries = I40E_AQ_LEN;
  13042. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  13043. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  13044. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  13045. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  13046. "%s-%s:misc",
  13047. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  13048. err = i40e_init_shared_code(hw);
  13049. if (err) {
  13050. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  13051. err);
  13052. goto err_pf_reset;
  13053. }
  13054. /* set up a default setting for link flow control */
  13055. pf->hw.fc.requested_mode = I40E_FC_NONE;
  13056. err = i40e_init_adminq(hw);
  13057. if (err) {
  13058. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  13059. dev_info(&pdev->dev,
  13060. "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
  13061. hw->aq.api_maj_ver,
  13062. hw->aq.api_min_ver,
  13063. I40E_FW_API_VERSION_MAJOR,
  13064. I40E_FW_MINOR_VERSION(hw));
  13065. else
  13066. dev_info(&pdev->dev,
  13067. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  13068. goto err_pf_reset;
  13069. }
  13070. i40e_get_oem_version(hw);
  13071. /* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
  13072. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
  13073. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  13074. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  13075. i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
  13076. hw->subsystem_vendor_id, hw->subsystem_device_id);
  13077. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  13078. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  13079. dev_info(&pdev->dev,
  13080. "The driver for the device detected a newer version of the NVM image v%u.%u than expected v%u.%u. Please install the most recent version of the network driver.\n",
  13081. hw->aq.api_maj_ver,
  13082. hw->aq.api_min_ver,
  13083. I40E_FW_API_VERSION_MAJOR,
  13084. I40E_FW_MINOR_VERSION(hw));
  13085. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  13086. dev_info(&pdev->dev,
  13087. "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
  13088. hw->aq.api_maj_ver,
  13089. hw->aq.api_min_ver,
  13090. I40E_FW_API_VERSION_MAJOR,
  13091. I40E_FW_MINOR_VERSION(hw));
  13092. i40e_verify_eeprom(pf);
  13093. /* Rev 0 hardware was never productized */
  13094. if (hw->revision_id < 1)
  13095. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  13096. i40e_clear_pxe_mode(hw);
  13097. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  13098. if (err)
  13099. goto err_adminq_setup;
  13100. err = i40e_sw_init(pf);
  13101. if (err) {
  13102. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  13103. goto err_sw_init;
  13104. }
  13105. if (test_bit(__I40E_RECOVERY_MODE, pf->state))
  13106. return i40e_init_recovery_mode(pf, hw);
  13107. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  13108. hw->func_caps.num_rx_qp, 0, 0);
  13109. if (err) {
  13110. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  13111. goto err_init_lan_hmc;
  13112. }
  13113. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  13114. if (err) {
  13115. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  13116. err = -ENOENT;
  13117. goto err_configure_lan_hmc;
  13118. }
  13119. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  13120. * Ignore error return codes because if it was already disabled via
  13121. * hardware settings this will fail
  13122. */
  13123. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  13124. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  13125. i40e_aq_stop_lldp(hw, true, false, NULL);
  13126. }
  13127. /* allow a platform config to override the HW addr */
  13128. i40e_get_platform_mac_addr(pdev, pf);
  13129. if (!is_valid_ether_addr(hw->mac.addr)) {
  13130. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  13131. err = -EIO;
  13132. goto err_mac_addr;
  13133. }
  13134. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  13135. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  13136. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  13137. if (is_valid_ether_addr(hw->mac.port_addr))
  13138. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  13139. pci_set_drvdata(pdev, pf);
  13140. pci_save_state(pdev);
  13141. dev_info(&pdev->dev,
  13142. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
  13143. "FW LLDP is disabled\n" :
  13144. "FW LLDP is enabled\n");
  13145. /* Enable FW to write default DCB config on link-up */
  13146. i40e_aq_set_dcb_parameters(hw, true, NULL);
  13147. #ifdef CONFIG_I40E_DCB
  13148. err = i40e_init_pf_dcb(pf);
  13149. if (err) {
  13150. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  13151. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  13152. /* Continue without DCB enabled */
  13153. }
  13154. #endif /* CONFIG_I40E_DCB */
  13155. /* set up periodic task facility */
  13156. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  13157. pf->service_timer_period = HZ;
  13158. INIT_WORK(&pf->service_task, i40e_service_task);
  13159. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  13160. /* NVM bit on means WoL disabled for the port */
  13161. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  13162. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  13163. pf->wol_en = false;
  13164. else
  13165. pf->wol_en = true;
  13166. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  13167. /* set up the main switch operations */
  13168. i40e_determine_queue_usage(pf);
  13169. err = i40e_init_interrupt_scheme(pf);
  13170. if (err)
  13171. goto err_switch_setup;
  13172. /* The number of VSIs reported by the FW is the minimum guaranteed
  13173. * to us; HW supports far more and we share the remaining pool with
  13174. * the other PFs. We allocate space for more than the guarantee with
  13175. * the understanding that we might not get them all later.
  13176. */
  13177. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  13178. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  13179. else
  13180. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  13181. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  13182. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  13183. GFP_KERNEL);
  13184. if (!pf->vsi) {
  13185. err = -ENOMEM;
  13186. goto err_switch_setup;
  13187. }
  13188. #ifdef CONFIG_PCI_IOV
  13189. /* prep for VF support */
  13190. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  13191. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  13192. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  13193. if (pci_num_vf(pdev))
  13194. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  13195. }
  13196. #endif
  13197. err = i40e_setup_pf_switch(pf, false);
  13198. if (err) {
  13199. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  13200. goto err_vsis;
  13201. }
  13202. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  13203. /* Make sure flow control is set according to current settings */
  13204. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  13205. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  13206. dev_dbg(&pf->pdev->dev,
  13207. "Set fc with err %s aq_err %s on get_phy_cap\n",
  13208. i40e_stat_str(hw, err),
  13209. i40e_aq_str(hw, hw->aq.asq_last_status));
  13210. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  13211. dev_dbg(&pf->pdev->dev,
  13212. "Set fc with err %s aq_err %s on set_phy_config\n",
  13213. i40e_stat_str(hw, err),
  13214. i40e_aq_str(hw, hw->aq.asq_last_status));
  13215. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  13216. dev_dbg(&pf->pdev->dev,
  13217. "Set fc with err %s aq_err %s on get_link_info\n",
  13218. i40e_stat_str(hw, err),
  13219. i40e_aq_str(hw, hw->aq.asq_last_status));
  13220. /* if FDIR VSI was set up, start it now */
  13221. for (i = 0; i < pf->num_alloc_vsi; i++) {
  13222. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  13223. i40e_vsi_open(pf->vsi[i]);
  13224. break;
  13225. }
  13226. }
  13227. /* The driver only wants link up/down and module qualification
  13228. * reports from firmware. Note the negative logic.
  13229. */
  13230. err = i40e_aq_set_phy_int_mask(&pf->hw,
  13231. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  13232. I40E_AQ_EVENT_MEDIA_NA |
  13233. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  13234. if (err)
  13235. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  13236. i40e_stat_str(&pf->hw, err),
  13237. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13238. /* Reconfigure hardware for allowing smaller MSS in the case
  13239. * of TSO, so that we avoid the MDD being fired and causing
  13240. * a reset in the case of small MSS+TSO.
  13241. */
  13242. val = rd32(hw, I40E_REG_MSS);
  13243. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  13244. val &= ~I40E_REG_MSS_MIN_MASK;
  13245. val |= I40E_64BYTE_MSS;
  13246. wr32(hw, I40E_REG_MSS, val);
  13247. }
  13248. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  13249. msleep(75);
  13250. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  13251. if (err)
  13252. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  13253. i40e_stat_str(&pf->hw, err),
  13254. i40e_aq_str(&pf->hw,
  13255. pf->hw.aq.asq_last_status));
  13256. }
  13257. /* The main driver is (mostly) up and happy. We need to set this state
  13258. * before setting up the misc vector or we get a race and the vector
  13259. * ends up disabled forever.
  13260. */
  13261. clear_bit(__I40E_DOWN, pf->state);
  13262. /* In case of MSIX we are going to setup the misc vector right here
  13263. * to handle admin queue events etc. In case of legacy and MSI
  13264. * the misc functionality and queue processing is combined in
  13265. * the same vector and that gets setup at open.
  13266. */
  13267. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  13268. err = i40e_setup_misc_vector(pf);
  13269. if (err) {
  13270. dev_info(&pdev->dev,
  13271. "setup of misc vector failed: %d\n", err);
  13272. goto err_vsis;
  13273. }
  13274. }
  13275. #ifdef CONFIG_PCI_IOV
  13276. /* prep for VF support */
  13277. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  13278. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  13279. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  13280. /* disable link interrupts for VFs */
  13281. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  13282. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  13283. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  13284. i40e_flush(hw);
  13285. if (pci_num_vf(pdev)) {
  13286. dev_info(&pdev->dev,
  13287. "Active VFs found, allocating resources.\n");
  13288. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  13289. if (err)
  13290. dev_info(&pdev->dev,
  13291. "Error %d allocating resources for existing VFs\n",
  13292. err);
  13293. }
  13294. }
  13295. #endif /* CONFIG_PCI_IOV */
  13296. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  13297. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  13298. pf->num_iwarp_msix,
  13299. I40E_IWARP_IRQ_PILE_ID);
  13300. if (pf->iwarp_base_vector < 0) {
  13301. dev_info(&pdev->dev,
  13302. "failed to get tracking for %d vectors for IWARP err=%d\n",
  13303. pf->num_iwarp_msix, pf->iwarp_base_vector);
  13304. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  13305. }
  13306. }
  13307. i40e_dbg_pf_init(pf);
  13308. /* tell the firmware that we're starting */
  13309. i40e_send_version(pf);
  13310. /* since everything's happy, start the service_task timer */
  13311. mod_timer(&pf->service_timer,
  13312. round_jiffies(jiffies + pf->service_timer_period));
  13313. /* add this PF to client device list and launch a client service task */
  13314. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  13315. err = i40e_lan_add_device(pf);
  13316. if (err)
  13317. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  13318. err);
  13319. }
  13320. #define PCI_SPEED_SIZE 8
  13321. #define PCI_WIDTH_SIZE 8
  13322. /* Devices on the IOSF bus do not have this information
  13323. * and will report PCI Gen 1 x 1 by default so don't bother
  13324. * checking them.
  13325. */
  13326. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  13327. char speed[PCI_SPEED_SIZE] = "Unknown";
  13328. char width[PCI_WIDTH_SIZE] = "Unknown";
  13329. /* Get the negotiated link width and speed from PCI config
  13330. * space
  13331. */
  13332. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  13333. &link_status);
  13334. i40e_set_pci_config_data(hw, link_status);
  13335. switch (hw->bus.speed) {
  13336. case i40e_bus_speed_8000:
  13337. strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
  13338. case i40e_bus_speed_5000:
  13339. strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
  13340. case i40e_bus_speed_2500:
  13341. strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
  13342. default:
  13343. break;
  13344. }
  13345. switch (hw->bus.width) {
  13346. case i40e_bus_width_pcie_x8:
  13347. strlcpy(width, "8", PCI_WIDTH_SIZE); break;
  13348. case i40e_bus_width_pcie_x4:
  13349. strlcpy(width, "4", PCI_WIDTH_SIZE); break;
  13350. case i40e_bus_width_pcie_x2:
  13351. strlcpy(width, "2", PCI_WIDTH_SIZE); break;
  13352. case i40e_bus_width_pcie_x1:
  13353. strlcpy(width, "1", PCI_WIDTH_SIZE); break;
  13354. default:
  13355. break;
  13356. }
  13357. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  13358. speed, width);
  13359. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  13360. hw->bus.speed < i40e_bus_speed_8000) {
  13361. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  13362. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  13363. }
  13364. }
  13365. /* get the requested speeds from the fw */
  13366. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  13367. if (err)
  13368. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  13369. i40e_stat_str(&pf->hw, err),
  13370. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13371. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  13372. /* set the FEC config due to the board capabilities */
  13373. i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
  13374. /* get the supported phy types from the fw */
  13375. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  13376. if (err)
  13377. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  13378. i40e_stat_str(&pf->hw, err),
  13379. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13380. /* Add a filter to drop all Flow control frames from any VSI from being
  13381. * transmitted. By doing so we stop a malicious VF from sending out
  13382. * PAUSE or PFC frames and potentially controlling traffic for other
  13383. * PF/VF VSIs.
  13384. * The FW can still send Flow control frames if enabled.
  13385. */
  13386. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  13387. pf->main_vsi_seid);
  13388. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  13389. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  13390. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  13391. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  13392. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  13393. /* print a string summarizing features */
  13394. i40e_print_features(pf);
  13395. return 0;
  13396. /* Unwind what we've done if something failed in the setup */
  13397. err_vsis:
  13398. set_bit(__I40E_DOWN, pf->state);
  13399. i40e_clear_interrupt_scheme(pf);
  13400. kfree(pf->vsi);
  13401. err_switch_setup:
  13402. i40e_reset_interrupt_capability(pf);
  13403. del_timer_sync(&pf->service_timer);
  13404. err_mac_addr:
  13405. err_configure_lan_hmc:
  13406. (void)i40e_shutdown_lan_hmc(hw);
  13407. err_init_lan_hmc:
  13408. kfree(pf->qp_pile);
  13409. err_sw_init:
  13410. err_adminq_setup:
  13411. err_pf_reset:
  13412. iounmap(hw->hw_addr);
  13413. err_ioremap:
  13414. kfree(pf);
  13415. err_pf_alloc:
  13416. pci_disable_pcie_error_reporting(pdev);
  13417. pci_release_mem_regions(pdev);
  13418. err_pci_reg:
  13419. err_dma:
  13420. pci_disable_device(pdev);
  13421. return err;
  13422. }
  13423. /**
  13424. * i40e_remove - Device removal routine
  13425. * @pdev: PCI device information struct
  13426. *
  13427. * i40e_remove is called by the PCI subsystem to alert the driver
  13428. * that is should release a PCI device. This could be caused by a
  13429. * Hot-Plug event, or because the driver is going to be removed from
  13430. * memory.
  13431. **/
  13432. static void i40e_remove(struct pci_dev *pdev)
  13433. {
  13434. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13435. struct i40e_hw *hw = &pf->hw;
  13436. i40e_status ret_code;
  13437. int i;
  13438. i40e_dbg_pf_exit(pf);
  13439. i40e_ptp_stop(pf);
  13440. /* Disable RSS in hw */
  13441. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  13442. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  13443. /* no more scheduling of any task */
  13444. set_bit(__I40E_SUSPENDED, pf->state);
  13445. set_bit(__I40E_DOWN, pf->state);
  13446. if (pf->service_timer.function)
  13447. del_timer_sync(&pf->service_timer);
  13448. if (pf->service_task.func)
  13449. cancel_work_sync(&pf->service_task);
  13450. if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  13451. struct i40e_vsi *vsi = pf->vsi[0];
  13452. /* We know that we have allocated only one vsi for this PF,
  13453. * it was just for registering netdevice, so the interface
  13454. * could be visible in the 'ifconfig' output
  13455. */
  13456. unregister_netdev(vsi->netdev);
  13457. free_netdev(vsi->netdev);
  13458. goto unmap;
  13459. }
  13460. /* Client close must be called explicitly here because the timer
  13461. * has been stopped.
  13462. */
  13463. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  13464. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  13465. i40e_free_vfs(pf);
  13466. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  13467. }
  13468. i40e_fdir_teardown(pf);
  13469. /* If there is a switch structure or any orphans, remove them.
  13470. * This will leave only the PF's VSI remaining.
  13471. */
  13472. for (i = 0; i < I40E_MAX_VEB; i++) {
  13473. if (!pf->veb[i])
  13474. continue;
  13475. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  13476. pf->veb[i]->uplink_seid == 0)
  13477. i40e_switch_branch_release(pf->veb[i]);
  13478. }
  13479. /* Now we can shutdown the PF's VSI, just before we kill
  13480. * adminq and hmc.
  13481. */
  13482. if (pf->vsi[pf->lan_vsi])
  13483. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  13484. i40e_cloud_filter_exit(pf);
  13485. /* remove attached clients */
  13486. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  13487. ret_code = i40e_lan_del_device(pf);
  13488. if (ret_code)
  13489. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  13490. ret_code);
  13491. }
  13492. /* shutdown and destroy the HMC */
  13493. if (hw->hmc.hmc_obj) {
  13494. ret_code = i40e_shutdown_lan_hmc(hw);
  13495. if (ret_code)
  13496. dev_warn(&pdev->dev,
  13497. "Failed to destroy the HMC resources: %d\n",
  13498. ret_code);
  13499. }
  13500. unmap:
  13501. /* Free MSI/legacy interrupt 0 when in recovery mode. */
  13502. if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
  13503. !(pf->flags & I40E_FLAG_MSIX_ENABLED))
  13504. free_irq(pf->pdev->irq, pf);
  13505. /* shutdown the adminq */
  13506. i40e_shutdown_adminq(hw);
  13507. /* destroy the locks only once, here */
  13508. mutex_destroy(&hw->aq.arq_mutex);
  13509. mutex_destroy(&hw->aq.asq_mutex);
  13510. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  13511. rtnl_lock();
  13512. i40e_clear_interrupt_scheme(pf);
  13513. for (i = 0; i < pf->num_alloc_vsi; i++) {
  13514. if (pf->vsi[i]) {
  13515. if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
  13516. i40e_vsi_clear_rings(pf->vsi[i]);
  13517. i40e_vsi_clear(pf->vsi[i]);
  13518. pf->vsi[i] = NULL;
  13519. }
  13520. }
  13521. rtnl_unlock();
  13522. for (i = 0; i < I40E_MAX_VEB; i++) {
  13523. kfree(pf->veb[i]);
  13524. pf->veb[i] = NULL;
  13525. }
  13526. kfree(pf->qp_pile);
  13527. kfree(pf->vsi);
  13528. iounmap(hw->hw_addr);
  13529. kfree(pf);
  13530. pci_release_mem_regions(pdev);
  13531. pci_disable_pcie_error_reporting(pdev);
  13532. pci_disable_device(pdev);
  13533. }
  13534. /**
  13535. * i40e_pci_error_detected - warning that something funky happened in PCI land
  13536. * @pdev: PCI device information struct
  13537. * @error: the type of PCI error
  13538. *
  13539. * Called to warn that something happened and the error handling steps
  13540. * are in progress. Allows the driver to quiesce things, be ready for
  13541. * remediation.
  13542. **/
  13543. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  13544. enum pci_channel_state error)
  13545. {
  13546. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13547. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  13548. if (!pf) {
  13549. dev_info(&pdev->dev,
  13550. "Cannot recover - error happened during device probe\n");
  13551. return PCI_ERS_RESULT_DISCONNECT;
  13552. }
  13553. /* shutdown all operations */
  13554. if (!test_bit(__I40E_SUSPENDED, pf->state))
  13555. i40e_prep_for_reset(pf, false);
  13556. /* Request a slot reset */
  13557. return PCI_ERS_RESULT_NEED_RESET;
  13558. }
  13559. /**
  13560. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  13561. * @pdev: PCI device information struct
  13562. *
  13563. * Called to find if the driver can work with the device now that
  13564. * the pci slot has been reset. If a basic connection seems good
  13565. * (registers are readable and have sane content) then return a
  13566. * happy little PCI_ERS_RESULT_xxx.
  13567. **/
  13568. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  13569. {
  13570. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13571. pci_ers_result_t result;
  13572. u32 reg;
  13573. dev_dbg(&pdev->dev, "%s\n", __func__);
  13574. if (pci_enable_device_mem(pdev)) {
  13575. dev_info(&pdev->dev,
  13576. "Cannot re-enable PCI device after reset.\n");
  13577. result = PCI_ERS_RESULT_DISCONNECT;
  13578. } else {
  13579. pci_set_master(pdev);
  13580. pci_restore_state(pdev);
  13581. pci_save_state(pdev);
  13582. pci_wake_from_d3(pdev, false);
  13583. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  13584. if (reg == 0)
  13585. result = PCI_ERS_RESULT_RECOVERED;
  13586. else
  13587. result = PCI_ERS_RESULT_DISCONNECT;
  13588. }
  13589. return result;
  13590. }
  13591. /**
  13592. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  13593. * @pdev: PCI device information struct
  13594. */
  13595. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  13596. {
  13597. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13598. i40e_prep_for_reset(pf, false);
  13599. }
  13600. /**
  13601. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  13602. * @pdev: PCI device information struct
  13603. */
  13604. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  13605. {
  13606. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13607. i40e_reset_and_rebuild(pf, false, false);
  13608. }
  13609. /**
  13610. * i40e_pci_error_resume - restart operations after PCI error recovery
  13611. * @pdev: PCI device information struct
  13612. *
  13613. * Called to allow the driver to bring things back up after PCI error
  13614. * and/or reset recovery has finished.
  13615. **/
  13616. static void i40e_pci_error_resume(struct pci_dev *pdev)
  13617. {
  13618. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13619. dev_dbg(&pdev->dev, "%s\n", __func__);
  13620. if (test_bit(__I40E_SUSPENDED, pf->state))
  13621. return;
  13622. i40e_handle_reset_warning(pf, false);
  13623. }
  13624. /**
  13625. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  13626. * using the mac_address_write admin q function
  13627. * @pf: pointer to i40e_pf struct
  13628. **/
  13629. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  13630. {
  13631. struct i40e_hw *hw = &pf->hw;
  13632. i40e_status ret;
  13633. u8 mac_addr[6];
  13634. u16 flags = 0;
  13635. /* Get current MAC address in case it's an LAA */
  13636. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  13637. ether_addr_copy(mac_addr,
  13638. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  13639. } else {
  13640. dev_err(&pf->pdev->dev,
  13641. "Failed to retrieve MAC address; using default\n");
  13642. ether_addr_copy(mac_addr, hw->mac.addr);
  13643. }
  13644. /* The FW expects the mac address write cmd to first be called with
  13645. * one of these flags before calling it again with the multicast
  13646. * enable flags.
  13647. */
  13648. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  13649. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  13650. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  13651. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  13652. if (ret) {
  13653. dev_err(&pf->pdev->dev,
  13654. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  13655. return;
  13656. }
  13657. flags = I40E_AQC_MC_MAG_EN
  13658. | I40E_AQC_WOL_PRESERVE_ON_PFR
  13659. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  13660. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  13661. if (ret)
  13662. dev_err(&pf->pdev->dev,
  13663. "Failed to enable Multicast Magic Packet wake up\n");
  13664. }
  13665. /**
  13666. * i40e_shutdown - PCI callback for shutting down
  13667. * @pdev: PCI device information struct
  13668. **/
  13669. static void i40e_shutdown(struct pci_dev *pdev)
  13670. {
  13671. struct i40e_pf *pf = pci_get_drvdata(pdev);
  13672. struct i40e_hw *hw = &pf->hw;
  13673. set_bit(__I40E_SUSPENDED, pf->state);
  13674. set_bit(__I40E_DOWN, pf->state);
  13675. del_timer_sync(&pf->service_timer);
  13676. cancel_work_sync(&pf->service_task);
  13677. i40e_cloud_filter_exit(pf);
  13678. i40e_fdir_teardown(pf);
  13679. /* Client close must be called explicitly here because the timer
  13680. * has been stopped.
  13681. */
  13682. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  13683. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  13684. i40e_enable_mc_magic_wake(pf);
  13685. i40e_prep_for_reset(pf, false);
  13686. wr32(hw, I40E_PFPM_APM,
  13687. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  13688. wr32(hw, I40E_PFPM_WUFC,
  13689. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  13690. /* Free MSI/legacy interrupt 0 when in recovery mode. */
  13691. if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
  13692. !(pf->flags & I40E_FLAG_MSIX_ENABLED))
  13693. free_irq(pf->pdev->irq, pf);
  13694. /* Since we're going to destroy queues during the
  13695. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  13696. * whole section
  13697. */
  13698. rtnl_lock();
  13699. i40e_clear_interrupt_scheme(pf);
  13700. rtnl_unlock();
  13701. if (system_state == SYSTEM_POWER_OFF) {
  13702. pci_wake_from_d3(pdev, pf->wol_en);
  13703. pci_set_power_state(pdev, PCI_D3hot);
  13704. }
  13705. }
  13706. /**
  13707. * i40e_suspend - PM callback for moving to D3
  13708. * @dev: generic device information structure
  13709. **/
  13710. static int __maybe_unused i40e_suspend(struct device *dev)
  13711. {
  13712. struct i40e_pf *pf = dev_get_drvdata(dev);
  13713. struct i40e_hw *hw = &pf->hw;
  13714. /* If we're already suspended, then there is nothing to do */
  13715. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  13716. return 0;
  13717. set_bit(__I40E_DOWN, pf->state);
  13718. /* Ensure service task will not be running */
  13719. del_timer_sync(&pf->service_timer);
  13720. cancel_work_sync(&pf->service_task);
  13721. /* Client close must be called explicitly here because the timer
  13722. * has been stopped.
  13723. */
  13724. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  13725. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  13726. i40e_enable_mc_magic_wake(pf);
  13727. /* Since we're going to destroy queues during the
  13728. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  13729. * whole section
  13730. */
  13731. rtnl_lock();
  13732. i40e_prep_for_reset(pf, true);
  13733. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  13734. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  13735. /* Clear the interrupt scheme and release our IRQs so that the system
  13736. * can safely hibernate even when there are a large number of CPUs.
  13737. * Otherwise hibernation might fail when mapping all the vectors back
  13738. * to CPU0.
  13739. */
  13740. i40e_clear_interrupt_scheme(pf);
  13741. rtnl_unlock();
  13742. return 0;
  13743. }
  13744. /**
  13745. * i40e_resume - PM callback for waking up from D3
  13746. * @dev: generic device information structure
  13747. **/
  13748. static int __maybe_unused i40e_resume(struct device *dev)
  13749. {
  13750. struct i40e_pf *pf = dev_get_drvdata(dev);
  13751. int err;
  13752. /* If we're not suspended, then there is nothing to do */
  13753. if (!test_bit(__I40E_SUSPENDED, pf->state))
  13754. return 0;
  13755. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  13756. * since we're going to be restoring queues
  13757. */
  13758. rtnl_lock();
  13759. /* We cleared the interrupt scheme when we suspended, so we need to
  13760. * restore it now to resume device functionality.
  13761. */
  13762. err = i40e_restore_interrupt_scheme(pf);
  13763. if (err) {
  13764. dev_err(dev, "Cannot restore interrupt scheme: %d\n",
  13765. err);
  13766. }
  13767. clear_bit(__I40E_DOWN, pf->state);
  13768. i40e_reset_and_rebuild(pf, false, true);
  13769. rtnl_unlock();
  13770. /* Clear suspended state last after everything is recovered */
  13771. clear_bit(__I40E_SUSPENDED, pf->state);
  13772. /* Restart the service task */
  13773. mod_timer(&pf->service_timer,
  13774. round_jiffies(jiffies + pf->service_timer_period));
  13775. return 0;
  13776. }
  13777. static const struct pci_error_handlers i40e_err_handler = {
  13778. .error_detected = i40e_pci_error_detected,
  13779. .slot_reset = i40e_pci_error_slot_reset,
  13780. .reset_prepare = i40e_pci_error_reset_prepare,
  13781. .reset_done = i40e_pci_error_reset_done,
  13782. .resume = i40e_pci_error_resume,
  13783. };
  13784. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  13785. static struct pci_driver i40e_driver = {
  13786. .name = i40e_driver_name,
  13787. .id_table = i40e_pci_tbl,
  13788. .probe = i40e_probe,
  13789. .remove = i40e_remove,
  13790. .driver = {
  13791. .pm = &i40e_pm_ops,
  13792. },
  13793. .shutdown = i40e_shutdown,
  13794. .err_handler = &i40e_err_handler,
  13795. .sriov_configure = i40e_pci_sriov_configure,
  13796. };
  13797. /**
  13798. * i40e_init_module - Driver registration routine
  13799. *
  13800. * i40e_init_module is the first routine called when the driver is
  13801. * loaded. All it does is register with the PCI subsystem.
  13802. **/
  13803. static int __init i40e_init_module(void)
  13804. {
  13805. pr_info("%s: %s - version %s\n", i40e_driver_name,
  13806. i40e_driver_string, i40e_driver_version_str);
  13807. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  13808. /* There is no need to throttle the number of active tasks because
  13809. * each device limits its own task using a state bit for scheduling
  13810. * the service task, and the device tasks do not interfere with each
  13811. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  13812. * since we need to be able to guarantee forward progress even under
  13813. * memory pressure.
  13814. */
  13815. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  13816. if (!i40e_wq) {
  13817. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  13818. return -ENOMEM;
  13819. }
  13820. i40e_dbg_init();
  13821. return pci_register_driver(&i40e_driver);
  13822. }
  13823. module_init(i40e_init_module);
  13824. /**
  13825. * i40e_exit_module - Driver exit cleanup routine
  13826. *
  13827. * i40e_exit_module is called just before the driver is removed
  13828. * from memory.
  13829. **/
  13830. static void __exit i40e_exit_module(void)
  13831. {
  13832. pci_unregister_driver(&i40e_driver);
  13833. destroy_workqueue(i40e_wq);
  13834. i40e_dbg_exit();
  13835. }
  13836. module_exit(i40e_exit_module);