PageRenderTime 72ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/mirrors/linux-2.6
C | 3746 lines | 2194 code | 547 blank | 1005 comment | 376 complexity | 448e36811ad40f136a09d522a9226bbd 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/prefetch.h>
  4. #include <linux/bpf_trace.h>
  5. #include <net/xdp.h>
  6. #include "i40e.h"
  7. #include "i40e_trace.h"
  8. #include "i40e_prototype.h"
  9. #include "i40e_txrx_common.h"
  10. #include "i40e_xsk.h"
  11. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  12. /**
  13. * i40e_fdir - Generate a Flow Director descriptor based on fdata
  14. * @tx_ring: Tx ring to send buffer on
  15. * @fdata: Flow director filter data
  16. * @add: Indicate if we are adding a rule or deleting one
  17. *
  18. **/
  19. static void i40e_fdir(struct i40e_ring *tx_ring,
  20. struct i40e_fdir_filter *fdata, bool add)
  21. {
  22. struct i40e_filter_program_desc *fdir_desc;
  23. struct i40e_pf *pf = tx_ring->vsi->back;
  24. u32 flex_ptype, dtype_cmd;
  25. u16 i;
  26. /* grab the next descriptor */
  27. i = tx_ring->next_to_use;
  28. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  29. i++;
  30. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  31. flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
  32. (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
  33. flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
  34. (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  35. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  36. (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  37. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  38. (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  39. /* Use LAN VSI Id if not programmed by user */
  40. flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
  41. ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
  42. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
  43. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  44. dtype_cmd |= add ?
  45. I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  46. I40E_TXD_FLTR_QW1_PCMD_SHIFT :
  47. I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  48. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  49. dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
  50. (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
  51. dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
  52. (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
  53. if (fdata->cnt_index) {
  54. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  55. dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
  56. ((u32)fdata->cnt_index <<
  57. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
  58. }
  59. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  60. fdir_desc->rsvd = cpu_to_le32(0);
  61. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  62. fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
  63. }
  64. #define I40E_FD_CLEAN_DELAY 10
  65. /**
  66. * i40e_program_fdir_filter - Program a Flow Director filter
  67. * @fdir_data: Packet data that will be filter parameters
  68. * @raw_packet: the pre-allocated packet buffer for FDir
  69. * @pf: The PF pointer
  70. * @add: True for add/update, False for remove
  71. **/
  72. static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
  73. u8 *raw_packet, struct i40e_pf *pf,
  74. bool add)
  75. {
  76. struct i40e_tx_buffer *tx_buf, *first;
  77. struct i40e_tx_desc *tx_desc;
  78. struct i40e_ring *tx_ring;
  79. struct i40e_vsi *vsi;
  80. struct device *dev;
  81. dma_addr_t dma;
  82. u32 td_cmd = 0;
  83. u16 i;
  84. /* find existing FDIR VSI */
  85. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  86. if (!vsi)
  87. return -ENOENT;
  88. tx_ring = vsi->tx_rings[0];
  89. dev = tx_ring->dev;
  90. /* we need two descriptors to add/del a filter and we can wait */
  91. for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
  92. if (!i)
  93. return -EAGAIN;
  94. msleep_interruptible(1);
  95. }
  96. dma = dma_map_single(dev, raw_packet,
  97. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  98. if (dma_mapping_error(dev, dma))
  99. goto dma_fail;
  100. /* grab the next descriptor */
  101. i = tx_ring->next_to_use;
  102. first = &tx_ring->tx_bi[i];
  103. i40e_fdir(tx_ring, fdir_data, add);
  104. /* Now program a dummy descriptor */
  105. i = tx_ring->next_to_use;
  106. tx_desc = I40E_TX_DESC(tx_ring, i);
  107. tx_buf = &tx_ring->tx_bi[i];
  108. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  109. memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
  110. /* record length, and DMA address */
  111. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  112. dma_unmap_addr_set(tx_buf, dma, dma);
  113. tx_desc->buffer_addr = cpu_to_le64(dma);
  114. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  115. tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
  116. tx_buf->raw_buf = (void *)raw_packet;
  117. tx_desc->cmd_type_offset_bsz =
  118. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  119. /* Force memory writes to complete before letting h/w
  120. * know there are new descriptors to fetch.
  121. */
  122. wmb();
  123. /* Mark the data descriptor to be watched */
  124. first->next_to_watch = tx_desc;
  125. writel(tx_ring->next_to_use, tx_ring->tail);
  126. return 0;
  127. dma_fail:
  128. return -1;
  129. }
  130. #define IP_HEADER_OFFSET 14
  131. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  132. /**
  133. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  134. * @vsi: pointer to the targeted VSI
  135. * @fd_data: the flow director data required for the FDir descriptor
  136. * @add: true adds a filter, false removes it
  137. *
  138. * Returns 0 if the filters were successfully added or removed
  139. **/
  140. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  141. struct i40e_fdir_filter *fd_data,
  142. bool add)
  143. {
  144. struct i40e_pf *pf = vsi->back;
  145. struct udphdr *udp;
  146. struct iphdr *ip;
  147. u8 *raw_packet;
  148. int ret;
  149. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  150. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  151. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  152. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  153. if (!raw_packet)
  154. return -ENOMEM;
  155. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  156. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  157. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  158. + sizeof(struct iphdr));
  159. ip->daddr = fd_data->dst_ip;
  160. udp->dest = fd_data->dst_port;
  161. ip->saddr = fd_data->src_ip;
  162. udp->source = fd_data->src_port;
  163. if (fd_data->flex_filter) {
  164. u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
  165. __be16 pattern = fd_data->flex_word;
  166. u16 off = fd_data->flex_offset;
  167. *((__force __be16 *)(payload + off)) = pattern;
  168. }
  169. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  170. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  171. if (ret) {
  172. dev_info(&pf->pdev->dev,
  173. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  174. fd_data->pctype, fd_data->fd_id, ret);
  175. /* Free the packet buffer since it wasn't added to the ring */
  176. kfree(raw_packet);
  177. return -EOPNOTSUPP;
  178. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  179. if (add)
  180. dev_info(&pf->pdev->dev,
  181. "Filter OK for PCTYPE %d loc = %d\n",
  182. fd_data->pctype, fd_data->fd_id);
  183. else
  184. dev_info(&pf->pdev->dev,
  185. "Filter deleted for PCTYPE %d loc = %d\n",
  186. fd_data->pctype, fd_data->fd_id);
  187. }
  188. if (add)
  189. pf->fd_udp4_filter_cnt++;
  190. else
  191. pf->fd_udp4_filter_cnt--;
  192. return 0;
  193. }
  194. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  195. /**
  196. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  197. * @vsi: pointer to the targeted VSI
  198. * @fd_data: the flow director data required for the FDir descriptor
  199. * @add: true adds a filter, false removes it
  200. *
  201. * Returns 0 if the filters were successfully added or removed
  202. **/
  203. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  204. struct i40e_fdir_filter *fd_data,
  205. bool add)
  206. {
  207. struct i40e_pf *pf = vsi->back;
  208. struct tcphdr *tcp;
  209. struct iphdr *ip;
  210. u8 *raw_packet;
  211. int ret;
  212. /* Dummy packet */
  213. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  214. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  215. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  216. 0x0, 0x72, 0, 0, 0, 0};
  217. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  218. if (!raw_packet)
  219. return -ENOMEM;
  220. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  221. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  222. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  223. + sizeof(struct iphdr));
  224. ip->daddr = fd_data->dst_ip;
  225. tcp->dest = fd_data->dst_port;
  226. ip->saddr = fd_data->src_ip;
  227. tcp->source = fd_data->src_port;
  228. if (fd_data->flex_filter) {
  229. u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
  230. __be16 pattern = fd_data->flex_word;
  231. u16 off = fd_data->flex_offset;
  232. *((__force __be16 *)(payload + off)) = pattern;
  233. }
  234. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  235. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  236. if (ret) {
  237. dev_info(&pf->pdev->dev,
  238. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  239. fd_data->pctype, fd_data->fd_id, ret);
  240. /* Free the packet buffer since it wasn't added to the ring */
  241. kfree(raw_packet);
  242. return -EOPNOTSUPP;
  243. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  244. if (add)
  245. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
  246. fd_data->pctype, fd_data->fd_id);
  247. else
  248. dev_info(&pf->pdev->dev,
  249. "Filter deleted for PCTYPE %d loc = %d\n",
  250. fd_data->pctype, fd_data->fd_id);
  251. }
  252. if (add) {
  253. pf->fd_tcp4_filter_cnt++;
  254. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  255. I40E_DEBUG_FD & pf->hw.debug_mask)
  256. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  257. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  258. } else {
  259. pf->fd_tcp4_filter_cnt--;
  260. }
  261. return 0;
  262. }
  263. #define I40E_SCTPIP_DUMMY_PACKET_LEN 46
  264. /**
  265. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  266. * a specific flow spec
  267. * @vsi: pointer to the targeted VSI
  268. * @fd_data: the flow director data required for the FDir descriptor
  269. * @add: true adds a filter, false removes it
  270. *
  271. * Returns 0 if the filters were successfully added or removed
  272. **/
  273. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  274. struct i40e_fdir_filter *fd_data,
  275. bool add)
  276. {
  277. struct i40e_pf *pf = vsi->back;
  278. struct sctphdr *sctp;
  279. struct iphdr *ip;
  280. u8 *raw_packet;
  281. int ret;
  282. /* Dummy packet */
  283. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  284. 0x45, 0, 0, 0x20, 0, 0, 0x40, 0, 0x40, 0x84, 0, 0, 0, 0, 0, 0,
  285. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  286. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  287. if (!raw_packet)
  288. return -ENOMEM;
  289. memcpy(raw_packet, packet, I40E_SCTPIP_DUMMY_PACKET_LEN);
  290. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  291. sctp = (struct sctphdr *)(raw_packet + IP_HEADER_OFFSET
  292. + sizeof(struct iphdr));
  293. ip->daddr = fd_data->dst_ip;
  294. sctp->dest = fd_data->dst_port;
  295. ip->saddr = fd_data->src_ip;
  296. sctp->source = fd_data->src_port;
  297. if (fd_data->flex_filter) {
  298. u8 *payload = raw_packet + I40E_SCTPIP_DUMMY_PACKET_LEN;
  299. __be16 pattern = fd_data->flex_word;
  300. u16 off = fd_data->flex_offset;
  301. *((__force __be16 *)(payload + off)) = pattern;
  302. }
  303. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  304. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  305. if (ret) {
  306. dev_info(&pf->pdev->dev,
  307. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  308. fd_data->pctype, fd_data->fd_id, ret);
  309. /* Free the packet buffer since it wasn't added to the ring */
  310. kfree(raw_packet);
  311. return -EOPNOTSUPP;
  312. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  313. if (add)
  314. dev_info(&pf->pdev->dev,
  315. "Filter OK for PCTYPE %d loc = %d\n",
  316. fd_data->pctype, fd_data->fd_id);
  317. else
  318. dev_info(&pf->pdev->dev,
  319. "Filter deleted for PCTYPE %d loc = %d\n",
  320. fd_data->pctype, fd_data->fd_id);
  321. }
  322. if (add)
  323. pf->fd_sctp4_filter_cnt++;
  324. else
  325. pf->fd_sctp4_filter_cnt--;
  326. return 0;
  327. }
  328. #define I40E_IP_DUMMY_PACKET_LEN 34
  329. /**
  330. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  331. * a specific flow spec
  332. * @vsi: pointer to the targeted VSI
  333. * @fd_data: the flow director data required for the FDir descriptor
  334. * @add: true adds a filter, false removes it
  335. *
  336. * Returns 0 if the filters were successfully added or removed
  337. **/
  338. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  339. struct i40e_fdir_filter *fd_data,
  340. bool add)
  341. {
  342. struct i40e_pf *pf = vsi->back;
  343. struct iphdr *ip;
  344. u8 *raw_packet;
  345. int ret;
  346. int i;
  347. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  348. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  349. 0, 0, 0, 0};
  350. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  351. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  352. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  353. if (!raw_packet)
  354. return -ENOMEM;
  355. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  356. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  357. ip->saddr = fd_data->src_ip;
  358. ip->daddr = fd_data->dst_ip;
  359. ip->protocol = 0;
  360. if (fd_data->flex_filter) {
  361. u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
  362. __be16 pattern = fd_data->flex_word;
  363. u16 off = fd_data->flex_offset;
  364. *((__force __be16 *)(payload + off)) = pattern;
  365. }
  366. fd_data->pctype = i;
  367. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  368. if (ret) {
  369. dev_info(&pf->pdev->dev,
  370. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  371. fd_data->pctype, fd_data->fd_id, ret);
  372. /* The packet buffer wasn't added to the ring so we
  373. * need to free it now.
  374. */
  375. kfree(raw_packet);
  376. return -EOPNOTSUPP;
  377. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  378. if (add)
  379. dev_info(&pf->pdev->dev,
  380. "Filter OK for PCTYPE %d loc = %d\n",
  381. fd_data->pctype, fd_data->fd_id);
  382. else
  383. dev_info(&pf->pdev->dev,
  384. "Filter deleted for PCTYPE %d loc = %d\n",
  385. fd_data->pctype, fd_data->fd_id);
  386. }
  387. }
  388. if (add)
  389. pf->fd_ip4_filter_cnt++;
  390. else
  391. pf->fd_ip4_filter_cnt--;
  392. return 0;
  393. }
  394. /**
  395. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  396. * @vsi: pointer to the targeted VSI
  397. * @input: filter to add or delete
  398. * @add: true adds a filter, false removes it
  399. *
  400. **/
  401. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  402. struct i40e_fdir_filter *input, bool add)
  403. {
  404. struct i40e_pf *pf = vsi->back;
  405. int ret;
  406. switch (input->flow_type & ~FLOW_EXT) {
  407. case TCP_V4_FLOW:
  408. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  409. break;
  410. case UDP_V4_FLOW:
  411. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  412. break;
  413. case SCTP_V4_FLOW:
  414. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  415. break;
  416. case IP_USER_FLOW:
  417. switch (input->ip4_proto) {
  418. case IPPROTO_TCP:
  419. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  420. break;
  421. case IPPROTO_UDP:
  422. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  423. break;
  424. case IPPROTO_SCTP:
  425. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  426. break;
  427. case IPPROTO_IP:
  428. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  429. break;
  430. default:
  431. /* We cannot support masking based on protocol */
  432. dev_info(&pf->pdev->dev, "Unsupported IPv4 protocol 0x%02x\n",
  433. input->ip4_proto);
  434. return -EINVAL;
  435. }
  436. break;
  437. default:
  438. dev_info(&pf->pdev->dev, "Unsupported flow type 0x%02x\n",
  439. input->flow_type);
  440. return -EINVAL;
  441. }
  442. /* The buffer allocated here will be normally be freed by
  443. * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
  444. * completion. In the event of an error adding the buffer to the FDIR
  445. * ring, it will immediately be freed. It may also be freed by
  446. * i40e_clean_tx_ring() when closing the VSI.
  447. */
  448. return ret;
  449. }
  450. /**
  451. * i40e_fd_handle_status - check the Programming Status for FD
  452. * @rx_ring: the Rx ring for this descriptor
  453. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  454. * @prog_id: the id originally used for programming
  455. *
  456. * This is used to verify if the FD programming or invalidation
  457. * requested by SW to the HW is successful or not and take actions accordingly.
  458. **/
  459. void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  460. union i40e_rx_desc *rx_desc, u8 prog_id)
  461. {
  462. struct i40e_pf *pf = rx_ring->vsi->back;
  463. struct pci_dev *pdev = pf->pdev;
  464. u32 fcnt_prog, fcnt_avail;
  465. u32 error;
  466. u64 qw;
  467. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  468. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  469. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  470. if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  471. pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
  472. if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
  473. (I40E_DEBUG_FD & pf->hw.debug_mask))
  474. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  475. pf->fd_inv);
  476. /* Check if the programming error is for ATR.
  477. * If so, auto disable ATR and set a state for
  478. * flush in progress. Next time we come here if flush is in
  479. * progress do nothing, once flush is complete the state will
  480. * be cleared.
  481. */
  482. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  483. return;
  484. pf->fd_add_err++;
  485. /* store the current atr filter count */
  486. pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
  487. if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
  488. test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state)) {
  489. /* These set_bit() calls aren't atomic with the
  490. * test_bit() here, but worse case we potentially
  491. * disable ATR and queue a flush right after SB
  492. * support is re-enabled. That shouldn't cause an
  493. * issue in practice
  494. */
  495. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  496. set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  497. }
  498. /* filter programming failed most likely due to table full */
  499. fcnt_prog = i40e_get_global_fd_count(pf);
  500. fcnt_avail = pf->fdir_pf_filter_count;
  501. /* If ATR is running fcnt_prog can quickly change,
  502. * if we are very close to full, it makes sense to disable
  503. * FD ATR/SB and then re-enable it when there is room.
  504. */
  505. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  506. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  507. !test_and_set_bit(__I40E_FD_SB_AUTO_DISABLED,
  508. pf->state))
  509. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  510. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  511. }
  512. } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  513. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  514. dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
  515. rx_desc->wb.qword0.hi_dword.fd_id);
  516. }
  517. }
  518. /**
  519. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  520. * @ring: the ring that owns the buffer
  521. * @tx_buffer: the buffer to free
  522. **/
  523. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  524. struct i40e_tx_buffer *tx_buffer)
  525. {
  526. if (tx_buffer->skb) {
  527. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  528. kfree(tx_buffer->raw_buf);
  529. else if (ring_is_xdp(ring))
  530. xdp_return_frame(tx_buffer->xdpf);
  531. else
  532. dev_kfree_skb_any(tx_buffer->skb);
  533. if (dma_unmap_len(tx_buffer, len))
  534. dma_unmap_single(ring->dev,
  535. dma_unmap_addr(tx_buffer, dma),
  536. dma_unmap_len(tx_buffer, len),
  537. DMA_TO_DEVICE);
  538. } else if (dma_unmap_len(tx_buffer, len)) {
  539. dma_unmap_page(ring->dev,
  540. dma_unmap_addr(tx_buffer, dma),
  541. dma_unmap_len(tx_buffer, len),
  542. DMA_TO_DEVICE);
  543. }
  544. tx_buffer->next_to_watch = NULL;
  545. tx_buffer->skb = NULL;
  546. dma_unmap_len_set(tx_buffer, len, 0);
  547. /* tx_buffer must be completely set up in the transmit path */
  548. }
  549. /**
  550. * i40e_clean_tx_ring - Free any empty Tx buffers
  551. * @tx_ring: ring to be cleaned
  552. **/
  553. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  554. {
  555. unsigned long bi_size;
  556. u16 i;
  557. if (ring_is_xdp(tx_ring) && tx_ring->xsk_umem) {
  558. i40e_xsk_clean_tx_ring(tx_ring);
  559. } else {
  560. /* ring already cleared, nothing to do */
  561. if (!tx_ring->tx_bi)
  562. return;
  563. /* Free all the Tx ring sk_buffs */
  564. for (i = 0; i < tx_ring->count; i++)
  565. i40e_unmap_and_free_tx_resource(tx_ring,
  566. &tx_ring->tx_bi[i]);
  567. }
  568. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  569. memset(tx_ring->tx_bi, 0, bi_size);
  570. /* Zero out the descriptor ring */
  571. memset(tx_ring->desc, 0, tx_ring->size);
  572. tx_ring->next_to_use = 0;
  573. tx_ring->next_to_clean = 0;
  574. if (!tx_ring->netdev)
  575. return;
  576. /* cleanup Tx queue statistics */
  577. netdev_tx_reset_queue(txring_txq(tx_ring));
  578. }
  579. /**
  580. * i40e_free_tx_resources - Free Tx resources per queue
  581. * @tx_ring: Tx descriptor ring for a specific queue
  582. *
  583. * Free all transmit software resources
  584. **/
  585. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  586. {
  587. i40e_clean_tx_ring(tx_ring);
  588. kfree(tx_ring->tx_bi);
  589. tx_ring->tx_bi = NULL;
  590. if (tx_ring->desc) {
  591. dma_free_coherent(tx_ring->dev, tx_ring->size,
  592. tx_ring->desc, tx_ring->dma);
  593. tx_ring->desc = NULL;
  594. }
  595. }
  596. /**
  597. * i40e_get_tx_pending - how many tx descriptors not processed
  598. * @ring: the ring of descriptors
  599. * @in_sw: use SW variables
  600. *
  601. * Since there is no access to the ring head register
  602. * in XL710, we need to use our local copies
  603. **/
  604. u32 i40e_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  605. {
  606. u32 head, tail;
  607. if (!in_sw) {
  608. head = i40e_get_head(ring);
  609. tail = readl(ring->tail);
  610. } else {
  611. head = ring->next_to_clean;
  612. tail = ring->next_to_use;
  613. }
  614. if (head != tail)
  615. return (head < tail) ?
  616. tail - head : (tail + ring->count - head);
  617. return 0;
  618. }
  619. /**
  620. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  621. * @vsi: pointer to vsi struct with tx queues
  622. *
  623. * VSI has netdev and netdev has TX queues. This function is to check each of
  624. * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
  625. **/
  626. void i40e_detect_recover_hung(struct i40e_vsi *vsi)
  627. {
  628. struct i40e_ring *tx_ring = NULL;
  629. struct net_device *netdev;
  630. unsigned int i;
  631. int packets;
  632. if (!vsi)
  633. return;
  634. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  635. return;
  636. netdev = vsi->netdev;
  637. if (!netdev)
  638. return;
  639. if (!netif_carrier_ok(netdev))
  640. return;
  641. for (i = 0; i < vsi->num_queue_pairs; i++) {
  642. tx_ring = vsi->tx_rings[i];
  643. if (tx_ring && tx_ring->desc) {
  644. /* If packet counter has not changed the queue is
  645. * likely stalled, so force an interrupt for this
  646. * queue.
  647. *
  648. * prev_pkt_ctr would be negative if there was no
  649. * pending work.
  650. */
  651. packets = tx_ring->stats.packets & INT_MAX;
  652. if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
  653. i40e_force_wb(vsi, tx_ring->q_vector);
  654. continue;
  655. }
  656. /* Memory barrier between read of packet count and call
  657. * to i40e_get_tx_pending()
  658. */
  659. smp_rmb();
  660. tx_ring->tx_stats.prev_pkt_ctr =
  661. i40e_get_tx_pending(tx_ring, true) ? packets : -1;
  662. }
  663. }
  664. }
  665. /**
  666. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  667. * @vsi: the VSI we care about
  668. * @tx_ring: Tx ring to clean
  669. * @napi_budget: Used to determine if we are in netpoll
  670. *
  671. * Returns true if there's any budget left (e.g. the clean is finished)
  672. **/
  673. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  674. struct i40e_ring *tx_ring, int napi_budget)
  675. {
  676. int i = tx_ring->next_to_clean;
  677. struct i40e_tx_buffer *tx_buf;
  678. struct i40e_tx_desc *tx_head;
  679. struct i40e_tx_desc *tx_desc;
  680. unsigned int total_bytes = 0, total_packets = 0;
  681. unsigned int budget = vsi->work_limit;
  682. tx_buf = &tx_ring->tx_bi[i];
  683. tx_desc = I40E_TX_DESC(tx_ring, i);
  684. i -= tx_ring->count;
  685. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  686. do {
  687. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  688. /* if next_to_watch is not set then there is no work pending */
  689. if (!eop_desc)
  690. break;
  691. /* prevent any other reads prior to eop_desc */
  692. smp_rmb();
  693. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  694. /* we have caught up to head, no work left to do */
  695. if (tx_head == tx_desc)
  696. break;
  697. /* clear next_to_watch to prevent false hangs */
  698. tx_buf->next_to_watch = NULL;
  699. /* update the statistics for this packet */
  700. total_bytes += tx_buf->bytecount;
  701. total_packets += tx_buf->gso_segs;
  702. /* free the skb/XDP data */
  703. if (ring_is_xdp(tx_ring))
  704. xdp_return_frame(tx_buf->xdpf);
  705. else
  706. napi_consume_skb(tx_buf->skb, napi_budget);
  707. /* unmap skb header data */
  708. dma_unmap_single(tx_ring->dev,
  709. dma_unmap_addr(tx_buf, dma),
  710. dma_unmap_len(tx_buf, len),
  711. DMA_TO_DEVICE);
  712. /* clear tx_buffer data */
  713. tx_buf->skb = NULL;
  714. dma_unmap_len_set(tx_buf, len, 0);
  715. /* unmap remaining buffers */
  716. while (tx_desc != eop_desc) {
  717. i40e_trace(clean_tx_irq_unmap,
  718. tx_ring, tx_desc, tx_buf);
  719. tx_buf++;
  720. tx_desc++;
  721. i++;
  722. if (unlikely(!i)) {
  723. i -= tx_ring->count;
  724. tx_buf = tx_ring->tx_bi;
  725. tx_desc = I40E_TX_DESC(tx_ring, 0);
  726. }
  727. /* unmap any remaining paged data */
  728. if (dma_unmap_len(tx_buf, len)) {
  729. dma_unmap_page(tx_ring->dev,
  730. dma_unmap_addr(tx_buf, dma),
  731. dma_unmap_len(tx_buf, len),
  732. DMA_TO_DEVICE);
  733. dma_unmap_len_set(tx_buf, len, 0);
  734. }
  735. }
  736. /* move us one more past the eop_desc for start of next pkt */
  737. tx_buf++;
  738. tx_desc++;
  739. i++;
  740. if (unlikely(!i)) {
  741. i -= tx_ring->count;
  742. tx_buf = tx_ring->tx_bi;
  743. tx_desc = I40E_TX_DESC(tx_ring, 0);
  744. }
  745. prefetch(tx_desc);
  746. /* update budget accounting */
  747. budget--;
  748. } while (likely(budget));
  749. i += tx_ring->count;
  750. tx_ring->next_to_clean = i;
  751. i40e_update_tx_stats(tx_ring, total_packets, total_bytes);
  752. i40e_arm_wb(tx_ring, vsi, budget);
  753. if (ring_is_xdp(tx_ring))
  754. return !!budget;
  755. /* notify netdev of completed buffers */
  756. netdev_tx_completed_queue(txring_txq(tx_ring),
  757. total_packets, total_bytes);
  758. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  759. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  760. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  761. /* Make sure that anybody stopping the queue after this
  762. * sees the new next_to_clean.
  763. */
  764. smp_mb();
  765. if (__netif_subqueue_stopped(tx_ring->netdev,
  766. tx_ring->queue_index) &&
  767. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  768. netif_wake_subqueue(tx_ring->netdev,
  769. tx_ring->queue_index);
  770. ++tx_ring->tx_stats.restart_queue;
  771. }
  772. }
  773. return !!budget;
  774. }
  775. /**
  776. * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  777. * @vsi: the VSI we care about
  778. * @q_vector: the vector on which to enable writeback
  779. *
  780. **/
  781. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  782. struct i40e_q_vector *q_vector)
  783. {
  784. u16 flags = q_vector->tx.ring[0].flags;
  785. u32 val;
  786. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  787. return;
  788. if (q_vector->arm_wb_state)
  789. return;
  790. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  791. val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
  792. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
  793. wr32(&vsi->back->hw,
  794. I40E_PFINT_DYN_CTLN(q_vector->reg_idx),
  795. val);
  796. } else {
  797. val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
  798. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
  799. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  800. }
  801. q_vector->arm_wb_state = true;
  802. }
  803. /**
  804. * i40e_force_wb - Issue SW Interrupt so HW does a wb
  805. * @vsi: the VSI we care about
  806. * @q_vector: the vector on which to force writeback
  807. *
  808. **/
  809. void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  810. {
  811. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  812. u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  813. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
  814. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
  815. I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
  816. /* allow 00 to be written to the index */
  817. wr32(&vsi->back->hw,
  818. I40E_PFINT_DYN_CTLN(q_vector->reg_idx), val);
  819. } else {
  820. u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  821. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
  822. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
  823. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
  824. /* allow 00 to be written to the index */
  825. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  826. }
  827. }
  828. static inline bool i40e_container_is_rx(struct i40e_q_vector *q_vector,
  829. struct i40e_ring_container *rc)
  830. {
  831. return &q_vector->rx == rc;
  832. }
  833. static inline unsigned int i40e_itr_divisor(struct i40e_q_vector *q_vector)
  834. {
  835. unsigned int divisor;
  836. switch (q_vector->vsi->back->hw.phy.link_info.link_speed) {
  837. case I40E_LINK_SPEED_40GB:
  838. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 1024;
  839. break;
  840. case I40E_LINK_SPEED_25GB:
  841. case I40E_LINK_SPEED_20GB:
  842. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 512;
  843. break;
  844. default:
  845. case I40E_LINK_SPEED_10GB:
  846. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 256;
  847. break;
  848. case I40E_LINK_SPEED_1GB:
  849. case I40E_LINK_SPEED_100MB:
  850. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 32;
  851. break;
  852. }
  853. return divisor;
  854. }
  855. /**
  856. * i40e_update_itr - update the dynamic ITR value based on statistics
  857. * @q_vector: structure containing interrupt and ring information
  858. * @rc: structure containing ring performance data
  859. *
  860. * Stores a new ITR value based on packets and byte
  861. * counts during the last interrupt. The advantage of per interrupt
  862. * computation is faster updates and more accurate ITR for the current
  863. * traffic pattern. Constants in this function were computed
  864. * based on theoretical maximum wire speed and thresholds were set based
  865. * on testing data as well as attempting to minimize response time
  866. * while increasing bulk throughput.
  867. **/
  868. static void i40e_update_itr(struct i40e_q_vector *q_vector,
  869. struct i40e_ring_container *rc)
  870. {
  871. unsigned int avg_wire_size, packets, bytes, itr;
  872. unsigned long next_update = jiffies;
  873. /* If we don't have any rings just leave ourselves set for maximum
  874. * possible latency so we take ourselves out of the equation.
  875. */
  876. if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
  877. return;
  878. /* For Rx we want to push the delay up and default to low latency.
  879. * for Tx we want to pull the delay down and default to high latency.
  880. */
  881. itr = i40e_container_is_rx(q_vector, rc) ?
  882. I40E_ITR_ADAPTIVE_MIN_USECS | I40E_ITR_ADAPTIVE_LATENCY :
  883. I40E_ITR_ADAPTIVE_MAX_USECS | I40E_ITR_ADAPTIVE_LATENCY;
  884. /* If we didn't update within up to 1 - 2 jiffies we can assume
  885. * that either packets are coming in so slow there hasn't been
  886. * any work, or that there is so much work that NAPI is dealing
  887. * with interrupt moderation and we don't need to do anything.
  888. */
  889. if (time_after(next_update, rc->next_update))
  890. goto clear_counts;
  891. /* If itr_countdown is set it means we programmed an ITR within
  892. * the last 4 interrupt cycles. This has a side effect of us
  893. * potentially firing an early interrupt. In order to work around
  894. * this we need to throw out any data received for a few
  895. * interrupts following the update.
  896. */
  897. if (q_vector->itr_countdown) {
  898. itr = rc->target_itr;
  899. goto clear_counts;
  900. }
  901. packets = rc->total_packets;
  902. bytes = rc->total_bytes;
  903. if (i40e_container_is_rx(q_vector, rc)) {
  904. /* If Rx there are 1 to 4 packets and bytes are less than
  905. * 9000 assume insufficient data to use bulk rate limiting
  906. * approach unless Tx is already in bulk rate limiting. We
  907. * are likely latency driven.
  908. */
  909. if (packets && packets < 4 && bytes < 9000 &&
  910. (q_vector->tx.target_itr & I40E_ITR_ADAPTIVE_LATENCY)) {
  911. itr = I40E_ITR_ADAPTIVE_LATENCY;
  912. goto adjust_by_size;
  913. }
  914. } else if (packets < 4) {
  915. /* If we have Tx and Rx ITR maxed and Tx ITR is running in
  916. * bulk mode and we are receiving 4 or fewer packets just
  917. * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
  918. * that the Rx can relax.
  919. */
  920. if (rc->target_itr == I40E_ITR_ADAPTIVE_MAX_USECS &&
  921. (q_vector->rx.target_itr & I40E_ITR_MASK) ==
  922. I40E_ITR_ADAPTIVE_MAX_USECS)
  923. goto clear_counts;
  924. } else if (packets > 32) {
  925. /* If we have processed over 32 packets in a single interrupt
  926. * for Tx assume we need to switch over to "bulk" mode.
  927. */
  928. rc->target_itr &= ~I40E_ITR_ADAPTIVE_LATENCY;
  929. }
  930. /* We have no packets to actually measure against. This means
  931. * either one of the other queues on this vector is active or
  932. * we are a Tx queue doing TSO with too high of an interrupt rate.
  933. *
  934. * Between 4 and 56 we can assume that our current interrupt delay
  935. * is only slightly too low. As such we should increase it by a small
  936. * fixed amount.
  937. */
  938. if (packets < 56) {
  939. itr = rc->target_itr + I40E_ITR_ADAPTIVE_MIN_INC;
  940. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  941. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  942. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  943. }
  944. goto clear_counts;
  945. }
  946. if (packets <= 256) {
  947. itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
  948. itr &= I40E_ITR_MASK;
  949. /* Between 56 and 112 is our "goldilocks" zone where we are
  950. * working out "just right". Just report that our current
  951. * ITR is good for us.
  952. */
  953. if (packets <= 112)
  954. goto clear_counts;
  955. /* If packet count is 128 or greater we are likely looking
  956. * at a slight overrun of the delay we want. Try halving
  957. * our delay to see if that will cut the number of packets
  958. * in half per interrupt.
  959. */
  960. itr /= 2;
  961. itr &= I40E_ITR_MASK;
  962. if (itr < I40E_ITR_ADAPTIVE_MIN_USECS)
  963. itr = I40E_ITR_ADAPTIVE_MIN_USECS;
  964. goto clear_counts;
  965. }
  966. /* The paths below assume we are dealing with a bulk ITR since
  967. * number of packets is greater than 256. We are just going to have
  968. * to compute a value and try to bring the count under control,
  969. * though for smaller packet sizes there isn't much we can do as
  970. * NAPI polling will likely be kicking in sooner rather than later.
  971. */
  972. itr = I40E_ITR_ADAPTIVE_BULK;
  973. adjust_by_size:
  974. /* If packet counts are 256 or greater we can assume we have a gross
  975. * overestimation of what the rate should be. Instead of trying to fine
  976. * tune it just use the formula below to try and dial in an exact value
  977. * give the current packet size of the frame.
  978. */
  979. avg_wire_size = bytes / packets;
  980. /* The following is a crude approximation of:
  981. * wmem_default / (size + overhead) = desired_pkts_per_int
  982. * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
  983. * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
  984. *
  985. * Assuming wmem_default is 212992 and overhead is 640 bytes per
  986. * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
  987. * formula down to
  988. *
  989. * (170 * (size + 24)) / (size + 640) = ITR
  990. *
  991. * We first do some math on the packet size and then finally bitshift
  992. * by 8 after rounding up. We also have to account for PCIe link speed
  993. * difference as ITR scales based on this.
  994. */
  995. if (avg_wire_size <= 60) {
  996. /* Start at 250k ints/sec */
  997. avg_wire_size = 4096;
  998. } else if (avg_wire_size <= 380) {
  999. /* 250K ints/sec to 60K ints/sec */
  1000. avg_wire_size *= 40;
  1001. avg_wire_size += 1696;
  1002. } else if (avg_wire_size <= 1084) {
  1003. /* 60K ints/sec to 36K ints/sec */
  1004. avg_wire_size *= 15;
  1005. avg_wire_size += 11452;
  1006. } else if (avg_wire_size <= 1980) {
  1007. /* 36K ints/sec to 30K ints/sec */
  1008. avg_wire_size *= 5;
  1009. avg_wire_size += 22420;
  1010. } else {
  1011. /* plateau at a limit of 30K ints/sec */
  1012. avg_wire_size = 32256;
  1013. }
  1014. /* If we are in low latency mode halve our delay which doubles the
  1015. * rate to somewhere between 100K to 16K ints/sec
  1016. */
  1017. if (itr & I40E_ITR_ADAPTIVE_LATENCY)
  1018. avg_wire_size /= 2;
  1019. /* Resultant value is 256 times larger than it needs to be. This
  1020. * gives us room to adjust the value as needed to either increase
  1021. * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
  1022. *
  1023. * Use addition as we have already recorded the new latency flag
  1024. * for the ITR value.
  1025. */
  1026. itr += DIV_ROUND_UP(avg_wire_size, i40e_itr_divisor(q_vector)) *
  1027. I40E_ITR_ADAPTIVE_MIN_INC;
  1028. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  1029. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  1030. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  1031. }
  1032. clear_counts:
  1033. /* write back value */
  1034. rc->target_itr = itr;
  1035. /* next update should occur within next jiffy */
  1036. rc->next_update = next_update + 1;
  1037. rc->total_bytes = 0;
  1038. rc->total_packets = 0;
  1039. }
  1040. /**
  1041. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  1042. * @rx_ring: rx descriptor ring to store buffers on
  1043. * @old_buff: donor buffer to have page reused
  1044. *
  1045. * Synchronizes page for reuse by the adapter
  1046. **/
  1047. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  1048. struct i40e_rx_buffer *old_buff)
  1049. {
  1050. struct i40e_rx_buffer *new_buff;
  1051. u16 nta = rx_ring->next_to_alloc;
  1052. new_buff = &rx_ring->rx_bi[nta];
  1053. /* update, and store next to alloc */
  1054. nta++;
  1055. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  1056. /* transfer page from old buffer to new buffer */
  1057. new_buff->dma = old_buff->dma;
  1058. new_buff->page = old_buff->page;
  1059. new_buff->page_offset = old_buff->page_offset;
  1060. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  1061. rx_ring->rx_stats.page_reuse_count++;
  1062. /* clear contents of buffer_info */
  1063. old_buff->page = NULL;
  1064. }
  1065. /**
  1066. * i40e_rx_is_programming_status - check for programming status descriptor
  1067. * @qw: qword representing status_error_len in CPU ordering
  1068. *
  1069. * The value of in the descriptor length field indicate if this
  1070. * is a programming status descriptor for flow director or FCoE
  1071. * by the value of I40E_RX_PROG_STATUS_DESC_LENGTH, otherwise
  1072. * it is a packet descriptor.
  1073. **/
  1074. static inline bool i40e_rx_is_programming_status(u64 qw)
  1075. {
  1076. /* The Rx filter programming status and SPH bit occupy the same
  1077. * spot in the descriptor. Since we don't support packet split we
  1078. * can just reuse the bit as an indication that this is a
  1079. * programming status descriptor.
  1080. */
  1081. return qw & I40E_RXD_QW1_LENGTH_SPH_MASK;
  1082. }
  1083. /**
  1084. * i40e_clean_programming_status - try clean the programming status descriptor
  1085. * @rx_ring: the rx ring that has this descriptor
  1086. * @rx_desc: the rx descriptor written back by HW
  1087. * @qw: qword representing status_error_len in CPU ordering
  1088. *
  1089. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  1090. * status being successful or not and take actions accordingly. FCoE should
  1091. * handle its context/filter programming/invalidation status and take actions.
  1092. *
  1093. * Returns an i40e_rx_buffer to reuse if the cleanup occurred, otherwise NULL.
  1094. **/
  1095. struct i40e_rx_buffer *i40e_clean_programming_status(
  1096. struct i40e_ring *rx_ring,
  1097. union i40e_rx_desc *rx_desc,
  1098. u64 qw)
  1099. {
  1100. struct i40e_rx_buffer *rx_buffer;
  1101. u32 ntc;
  1102. u8 id;
  1103. if (!i40e_rx_is_programming_status(qw))
  1104. return NULL;
  1105. ntc = rx_ring->next_to_clean;
  1106. /* fetch, update, and store next to clean */
  1107. rx_buffer = &rx_ring->rx_bi[ntc++];
  1108. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1109. rx_ring->next_to_clean = ntc;
  1110. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1111. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  1112. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  1113. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  1114. i40e_fd_handle_status(rx_ring, rx_desc, id);
  1115. return rx_buffer;
  1116. }
  1117. /**
  1118. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  1119. * @tx_ring: the tx ring to set up
  1120. *
  1121. * Return 0 on success, negative on error
  1122. **/
  1123. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  1124. {
  1125. struct device *dev = tx_ring->dev;
  1126. int bi_size;
  1127. if (!dev)
  1128. return -ENOMEM;
  1129. /* warn if we are about to overwrite the pointer */
  1130. WARN_ON(tx_ring->tx_bi);
  1131. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  1132. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  1133. if (!tx_ring->tx_bi)
  1134. goto err;
  1135. u64_stats_init(&tx_ring->syncp);
  1136. /* round up to nearest 4K */
  1137. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  1138. /* add u32 for head writeback, align after this takes care of
  1139. * guaranteeing this is at least one cache line in size
  1140. */
  1141. tx_ring->size += sizeof(u32);
  1142. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1143. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  1144. &tx_ring->dma, GFP_KERNEL);
  1145. if (!tx_ring->desc) {
  1146. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  1147. tx_ring->size);
  1148. goto err;
  1149. }
  1150. tx_ring->next_to_use = 0;
  1151. tx_ring->next_to_clean = 0;
  1152. tx_ring->tx_stats.prev_pkt_ctr = -1;
  1153. return 0;
  1154. err:
  1155. kfree(tx_ring->tx_bi);
  1156. tx_ring->tx_bi = NULL;
  1157. return -ENOMEM;
  1158. }
  1159. /**
  1160. * i40e_clean_rx_ring - Free Rx buffers
  1161. * @rx_ring: ring to be cleaned
  1162. **/
  1163. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  1164. {
  1165. unsigned long bi_size;
  1166. u16 i;
  1167. /* ring already cleared, nothing to do */
  1168. if (!rx_ring->rx_bi)
  1169. return;
  1170. if (rx_ring->skb) {
  1171. dev_kfree_skb(rx_ring->skb);
  1172. rx_ring->skb = NULL;
  1173. }
  1174. if (rx_ring->xsk_umem) {
  1175. i40e_xsk_clean_rx_ring(rx_ring);
  1176. goto skip_free;
  1177. }
  1178. /* Free all the Rx ring sk_buffs */
  1179. for (i = 0; i < rx_ring->count; i++) {
  1180. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  1181. if (!rx_bi->page)
  1182. continue;
  1183. /* Invalidate cache lines that may have been written to by
  1184. * device so that we avoid corrupting memory.
  1185. */
  1186. dma_sync_single_range_for_cpu(rx_ring->dev,
  1187. rx_bi->dma,
  1188. rx_bi->page_offset,
  1189. rx_ring->rx_buf_len,
  1190. DMA_FROM_DEVICE);
  1191. /* free resources associated with mapping */
  1192. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  1193. i40e_rx_pg_size(rx_ring),
  1194. DMA_FROM_DEVICE,
  1195. I40E_RX_DMA_ATTR);
  1196. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  1197. rx_bi->page = NULL;
  1198. rx_bi->page_offset = 0;
  1199. }
  1200. skip_free:
  1201. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1202. memset(rx_ring->rx_bi, 0, bi_size);
  1203. /* Zero out the descriptor ring */
  1204. memset(rx_ring->desc, 0, rx_ring->size);
  1205. rx_ring->next_to_alloc = 0;
  1206. rx_ring->next_to_clean = 0;
  1207. rx_ring->next_to_use = 0;
  1208. }
  1209. /**
  1210. * i40e_free_rx_resources - Free Rx resources
  1211. * @rx_ring: ring to clean the resources from
  1212. *
  1213. * Free all receive software resources
  1214. **/
  1215. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  1216. {
  1217. i40e_clean_rx_ring(rx_ring);
  1218. if (rx_ring->vsi->type == I40E_VSI_MAIN)
  1219. xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
  1220. rx_ring->xdp_prog = NULL;
  1221. kfree(rx_ring->rx_bi);
  1222. rx_ring->rx_bi = NULL;
  1223. if (rx_ring->desc) {
  1224. dma_free_coherent(rx_ring->dev, rx_ring->size,
  1225. rx_ring->desc, rx_ring->dma);
  1226. rx_ring->desc = NULL;
  1227. }
  1228. }
  1229. /**
  1230. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  1231. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  1232. *
  1233. * Returns 0 on success, negative on failure
  1234. **/
  1235. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  1236. {
  1237. struct device *dev = rx_ring->dev;
  1238. int err = -ENOMEM;
  1239. int bi_size;
  1240. /* warn if we are about to overwrite the pointer */
  1241. WARN_ON(rx_ring->rx_bi);
  1242. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1243. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  1244. if (!rx_ring->rx_bi)
  1245. goto err;
  1246. u64_stats_init(&rx_ring->syncp);
  1247. /* Round up to nearest 4K */
  1248. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  1249. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1250. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  1251. &rx_ring->dma, GFP_KERNEL);
  1252. if (!rx_ring->desc) {
  1253. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  1254. rx_ring->size);
  1255. goto err;
  1256. }
  1257. rx_ring->next_to_alloc = 0;
  1258. rx_ring->next_to_clean = 0;
  1259. rx_ring->next_to_use = 0;
  1260. /* XDP RX-queue info only needed for RX rings exposed to XDP */
  1261. if (rx_ring->vsi->type == I40E_VSI_MAIN) {
  1262. err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
  1263. rx_ring->queue_index);
  1264. if (err < 0)
  1265. goto err;
  1266. }
  1267. rx_ring->xdp_prog = rx_ring->vsi->xdp_prog;
  1268. return 0;
  1269. err:
  1270. kfree(rx_ring->rx_bi);
  1271. rx_ring->rx_bi = NULL;
  1272. return err;
  1273. }
  1274. /**
  1275. * i40e_release_rx_desc - Store the new tail and head values
  1276. * @rx_ring: ring to bump
  1277. * @val: new head index
  1278. **/
  1279. void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  1280. {
  1281. rx_ring->next_to_use = val;
  1282. /* update next to alloc since we have filled the ring */
  1283. rx_ring->next_to_alloc = val;
  1284. /* Force memory writes to complete before letting h/w
  1285. * know there are new descriptors to fetch. (Only
  1286. * applicable for weak-ordered memory model archs,
  1287. * such as IA-64).
  1288. */
  1289. wmb();
  1290. writel(val, rx_ring->tail);
  1291. }
  1292. /**
  1293. * i40e_rx_offset - Return expected offset into page to access data
  1294. * @rx_ring: Ring we are requesting offset of
  1295. *
  1296. * Returns the offset value for ring into the data buffer.
  1297. */
  1298. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  1299. {
  1300. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  1301. }
  1302. /**
  1303. * i40e_alloc_mapped_page - recycle or make a new page
  1304. * @rx_ring: ring to use
  1305. * @bi: rx_buffer struct to modify
  1306. *
  1307. * Returns true if the page was successfully allocated or
  1308. * reused.
  1309. **/
  1310. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  1311. struct i40e_rx_buffer *bi)
  1312. {
  1313. struct page *page = bi->page;
  1314. dma_addr_t dma;
  1315. /* since we are recycling buffers we should seldom need to alloc */
  1316. if (likely(page)) {
  1317. rx_ring->rx_stats.page_reuse_count++;
  1318. return true;
  1319. }
  1320. /* alloc new page for storage */
  1321. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  1322. if (unlikely(!page)) {
  1323. rx_ring->rx_stats.alloc_page_failed++;
  1324. return false;
  1325. }
  1326. /* map page for use */
  1327. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  1328. i40e_rx_pg_size(rx_ring),
  1329. DMA_FROM_DEVICE,
  1330. I40E_RX_DMA_ATTR);
  1331. /* if mapping failed free memory back to system since
  1332. * there isn't much point in holding memory we can't use
  1333. */
  1334. if (dma_mapping_error(rx_ring->dev, dma)) {
  1335. __free_pages(page, i40e_rx_pg_order(rx_ring));
  1336. rx_ring->rx_stats.alloc_page_failed++;
  1337. return false;
  1338. }
  1339. bi->dma = dma;
  1340. bi->page = page;
  1341. bi->page_offset = i40e_rx_offset(rx_ring);
  1342. page_ref_add(page, USHRT_MAX - 1);
  1343. bi->pagecnt_bias = USHRT_MAX;
  1344. return true;
  1345. }
  1346. /**
  1347. * i40e_alloc_rx_buffers - Replace used receive buffers
  1348. * @rx_ring: ring to place buffers on
  1349. * @cleaned_count: number of buffers to replace
  1350. *
  1351. * Returns false if all allocations were successful, true if any fail
  1352. **/
  1353. bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  1354. {
  1355. u16 ntu = rx_ring->next_to_use;
  1356. union i40e_rx_desc *rx_desc;
  1357. struct i40e_rx_buffer *bi;
  1358. /* do nothing if no valid netdev defined */
  1359. if (!rx_ring->netdev || !cleaned_count)
  1360. return false;
  1361. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  1362. bi = &rx_ring->rx_bi[ntu];
  1363. do {
  1364. if (!i40e_alloc_mapped_page(rx_ring, bi))
  1365. goto no_buffers;
  1366. /* sync the buffer for use by the device */
  1367. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1368. bi->page_offset,
  1369. rx_ring->rx_buf_len,
  1370. DMA_FROM_DEVICE);
  1371. /* Refresh the desc even if buffer_addrs didn't change
  1372. * because each write-back erases this info.
  1373. */
  1374. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1375. rx_desc++;
  1376. bi++;
  1377. ntu++;
  1378. if (unlikely(ntu == rx_ring->count)) {
  1379. rx_desc = I40E_RX_DESC(rx_ring, 0);
  1380. bi = rx_ring->rx_bi;
  1381. ntu = 0;
  1382. }
  1383. /* clear the status bits for the next_to_use descriptor */
  1384. rx_desc->wb.qword1.status_error_len = 0;
  1385. cleaned_count--;
  1386. } while (cleaned_count);
  1387. if (rx_ring->next_to_use != ntu)
  1388. i40e_release_rx_desc(rx_ring, ntu);
  1389. return false;
  1390. no_buffers:
  1391. if (rx_ring->next_to_use != ntu)
  1392. i40e_release_rx_desc(rx_ring, ntu);
  1393. /* make sure to come back via polling to try again after
  1394. * allocation failure
  1395. */
  1396. return true;
  1397. }
  1398. /**
  1399. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1400. * @vsi: the VSI we care about
  1401. * @skb: skb currently being received and modified
  1402. * @rx_desc: the receive descriptor
  1403. **/
  1404. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1405. struct sk_buff *skb,
  1406. union i40e_rx_desc *rx_desc)
  1407. {
  1408. struct i40e_rx_ptype_decoded decoded;
  1409. u32 rx_error, rx_status;
  1410. bool ipv4, ipv6;
  1411. u8 ptype;
  1412. u64 qword;
  1413. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1414. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  1415. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1416. I40E_RXD_QW1_ERROR_SHIFT;
  1417. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1418. I40E_RXD_QW1_STATUS_SHIFT;
  1419. decoded = decode_rx_desc_ptype(ptype);
  1420. skb->ip_summed = CHECKSUM_NONE;
  1421. skb_checksum_none_assert(skb);
  1422. /* Rx csum enabled and ip headers found? */
  1423. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1424. return;
  1425. /* did the hardware decode the packet and checksum? */
  1426. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1427. return;
  1428. /* both known and outer_ip must be set for the below code to work */
  1429. if (!(decoded.known && decoded.outer_ip))
  1430. return;
  1431. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1432. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  1433. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1434. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  1435. if (ipv4 &&
  1436. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1437. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1438. goto checksum_fail;
  1439. /* likely incorrect csum if alternate IP extension headers found */
  1440. if (ipv6 &&
  1441. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1442. /* don't increment checksum err here, non-fatal err */
  1443. return;
  1444. /* there was some L4 error, count error and punt packet to the stack */
  1445. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  1446. goto checksum_fail;
  1447. /* handle packets that were not able to be checksummed due
  1448. * to arrival speed, in this case the stack can compute
  1449. * the csum.
  1450. */
  1451. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1452. return;
  1453. /* If there is an outer header present that might contain a checksum
  1454. * we need to bump the checksum level by 1 to reflect the fact that
  1455. * we are indicating we validated the inner checksum.
  1456. */
  1457. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  1458. skb->csum_level = 1;
  1459. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  1460. switch (decoded.inner_prot) {
  1461. case I40E_RX_PTYPE_INNER_PROT_TCP:
  1462. case I40E_RX_PTYPE_INNER_PROT_UDP:
  1463. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  1464. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1465. /* fall though */
  1466. default:
  1467. break;
  1468. }
  1469. return;
  1470. checksum_fail:
  1471. vsi->back->hw_csum_rx_error++;
  1472. }
  1473. /**
  1474. * i40e_ptype_to_htype - get a hash type
  1475. * @ptype: the ptype value from the descriptor
  1476. *
  1477. * Returns a hash type to be used by skb_set_hash
  1478. **/
  1479. static inline int i40e_ptype_to_htype(u8 ptype)
  1480. {
  1481. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1482. if (!decoded.known)
  1483. return PKT_HASH_TYPE_NONE;
  1484. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1485. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1486. return PKT_HASH_TYPE_L4;
  1487. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1488. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1489. return PKT_HASH_TYPE_L3;
  1490. else
  1491. return PKT_HASH_TYPE_L2;
  1492. }
  1493. /**
  1494. * i40e_rx_hash - set the hash value in the skb
  1495. * @ring: descriptor ring
  1496. * @rx_desc: specific descriptor
  1497. * @skb: skb currently being received and modified
  1498. * @rx_ptype: Rx packet type
  1499. **/
  1500. static inline void i40e_rx_hash(struct i40e_ring *ring,
  1501. union i40e_rx_desc *rx_desc,
  1502. struct sk_buff *skb,
  1503. u8 rx_ptype)
  1504. {
  1505. u32 hash;
  1506. const __le64 rss_mask =
  1507. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1508. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1509. if (!(ring->netdev->features & NETIF_F_RXHASH))
  1510. return;
  1511. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  1512. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1513. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  1514. }
  1515. }
  1516. /**
  1517. * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
  1518. * @rx_ring: rx descriptor ring packet is being transacted on
  1519. * @rx_desc: pointer to the EOP Rx descriptor
  1520. * @skb: pointer to current skb being populated
  1521. * @rx_ptype: the packet type decoded by hardware
  1522. *
  1523. * This function checks the ring, descriptor, and packet information in
  1524. * order to populate the hash, checksum, VLAN, protocol, and
  1525. * other fields within the skb.
  1526. **/
  1527. void i40e_process_skb_fields(struct i40e_ring *rx_ring,
  1528. union i40e_rx_desc *rx_desc, struct sk_buff *skb)
  1529. {
  1530. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1531. u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1532. I40E_RXD_QW1_STATUS_SHIFT;
  1533. u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
  1534. u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1535. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
  1536. u8 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1537. I40E_RXD_QW1_PTYPE_SHIFT;
  1538. if (unlikely(tsynvalid))
  1539. i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
  1540. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1541. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  1542. skb_record_rx_queue(skb, rx_ring->queue_index);
  1543. if (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) {
  1544. u16 vlan_tag = rx_desc->wb.qword0.lo_dword.l2tag1;
  1545. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1546. le16_to_cpu(vlan_tag));
  1547. }
  1548. /* modifies the skb - consumes the enet header */
  1549. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1550. }
  1551. /**
  1552. * i40e_cleanup_headers - Correct empty headers
  1553. * @rx_ring: rx descriptor ring packet is being transacted on
  1554. * @skb: pointer to current skb being fixed
  1555. * @rx_desc: pointer to the EOP Rx descriptor
  1556. *
  1557. * Also address the case where we are pulling data in on pages only
  1558. * and as such no data is present in the skb header.
  1559. *
  1560. * In addition if skb is not at least 60 bytes we need to pad it so that
  1561. * it is large enough to qualify as a valid Ethernet frame.
  1562. *
  1563. * Returns true if an error was encountered and skb was freed.
  1564. **/
  1565. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb,
  1566. union i40e_rx_desc *rx_desc)
  1567. {
  1568. /* XDP packets use error pointer so abort at this point */
  1569. if (IS_ERR(skb))
  1570. return true;
  1571. /* ERR_MASK will only have valid bits if EOP set, and
  1572. * what we are doing here is actually checking
  1573. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1574. * the error field
  1575. */
  1576. if (unlikely(i40e_test_staterr(rx_desc,
  1577. BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1578. dev_kfree_skb_any(skb);
  1579. return true;
  1580. }
  1581. /* if eth_skb_pad returns an error the skb was freed */
  1582. if (eth_skb_pad(skb))
  1583. return true;
  1584. return false;
  1585. }
  1586. /**
  1587. * i40e_page_is_reusable - check if any reuse is possible
  1588. * @page: page struct to check
  1589. *
  1590. * A page is not reusable if it was allocated under low memory
  1591. * conditions, or it's not in the same NUMA node as this CPU.
  1592. */
  1593. static inline bool i40e_page_is_reusable(struct page *page)
  1594. {
  1595. return (page_to_nid(page) == numa_mem_id()) &&
  1596. !page_is_pfmemalloc(page);
  1597. }
  1598. /**
  1599. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1600. * the adapter for another receive
  1601. *
  1602. * @rx_buffer: buffer containing the page
  1603. *
  1604. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1605. * an unused region in the page.
  1606. *
  1607. * For small pages, @truesize will be a constant value, half the size
  1608. * of the memory at page. We'll attempt to alternate between high and
  1609. * low halves of the page, with one half ready for use by the hardware
  1610. * and the other half being consumed by the stack. We use the page
  1611. * ref count to determine whether the stack has finished consuming the
  1612. * portion of this page that was passed up with a previous packet. If
  1613. * the page ref count is >1, we'll assume the "other" half page is
  1614. * still busy, and this page cannot be reused.
  1615. *
  1616. * For larger pages, @truesize will be the actual space used by the
  1617. * received packet (adjusted upward to an even multiple of the cache
  1618. * line size). This will advance through the page by the amount
  1619. * actually consumed by the received packets while there is still
  1620. * space for a buffer. Each region of larger pages will be used at
  1621. * most once, after which the page will not be reused.
  1622. *
  1623. * In either case, if the page is reusable its refcount is increased.
  1624. **/
  1625. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1626. {
  1627. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1628. struct page *page = rx_buffer->page;
  1629. /* Is any reuse possible? */
  1630. if (unlikely(!i40e_page_is_reusable(page)))
  1631. return false;
  1632. #if (PAGE_SIZE < 8192)
  1633. /* if we are only owner of page we can reuse it */
  1634. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1635. return false;
  1636. #else
  1637. #define I40E_LAST_OFFSET \
  1638. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1639. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1640. return false;
  1641. #endif
  1642. /* If we have drained the page fragment pool we need to update
  1643. * the pagecnt_bias and page count so that we fully restock the
  1644. * number of references the driver holds.
  1645. */
  1646. if (unlikely(pagecnt_bias == 1)) {
  1647. page_ref_add(page, USHRT_MAX - 1);
  1648. rx_buffer->pagecnt_bias = USHRT_MAX;
  1649. }
  1650. return true;
  1651. }
  1652. /**
  1653. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1654. * @rx_ring: rx descriptor ring to transact packets on
  1655. * @rx_buffer: buffer containing page to add
  1656. * @skb: sk_buff to place the data into
  1657. * @size: packet length from rx_desc
  1658. *
  1659. * This function will add the data contained in rx_buffer->page to the skb.
  1660. * It will just attach the page as a frag to the skb.
  1661. *
  1662. * The function will then update the page offset.
  1663. **/
  1664. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1665. struct i40e_rx_buffer *rx_buffer,
  1666. struct sk_buff *skb,
  1667. unsigned int size)
  1668. {
  1669. #if (PAGE_SIZE < 8192)
  1670. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1671. #else
  1672. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1673. #endif
  1674. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1675. rx_buffer->page_offset, size, truesize);
  1676. /* page is being used so we must update the page offset */
  1677. #if (PAGE_SIZE < 8192)
  1678. rx_buffer->page_offset ^= truesize;
  1679. #else
  1680. rx_buffer->page_offset += truesize;
  1681. #endif
  1682. }
  1683. /**
  1684. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1685. * @rx_ring: rx descriptor ring to transact packets on
  1686. * @size: size of buffer to add to skb
  1687. *
  1688. * This function will pull an Rx buffer from the ring and synchronize it
  1689. * for use by the CPU.
  1690. */
  1691. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1692. const unsigned int size)
  1693. {
  1694. struct i40e_rx_buffer *rx_buffer;
  1695. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1696. prefetchw(rx_buffer->page);
  1697. /* we are reusing so sync this buffer for CPU use */
  1698. dma_sync_single_range_for_cpu(rx_ring->dev,
  1699. rx_buffer->dma,
  1700. rx_buffer->page_offset,
  1701. size,
  1702. DMA_FROM_DEVICE);
  1703. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1704. rx_buffer->pagecnt_bias--;
  1705. return rx_buffer;
  1706. }
  1707. /**
  1708. * i40e_construct_skb - Allocate skb and populate it
  1709. * @rx_ring: rx descriptor ring to transact packets on
  1710. * @rx_buffer: rx buffer to pull data from
  1711. * @xdp: xdp_buff pointing to the data
  1712. *
  1713. * This function allocates an skb. It then populates it with the page
  1714. * data from the current receive descriptor, taking care to set up the
  1715. * skb correctly.
  1716. */
  1717. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1718. struct i40e_rx_buffer *rx_buffer,
  1719. struct xdp_buff *xdp)
  1720. {
  1721. unsigned int size = xdp->data_end - xdp->data;
  1722. #if (PAGE_SIZE < 8192)
  1723. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1724. #else
  1725. unsigned int truesize = SKB_DATA_ALIGN(size);
  1726. #endif
  1727. unsigned int headlen;
  1728. struct sk_buff *skb;
  1729. /* prefetch first cache line of first page */
  1730. prefetch(xdp->data);
  1731. #if L1_CACHE_BYTES < 128
  1732. prefetch(xdp->data + L1_CACHE_BYTES);
  1733. #endif
  1734. /* Note, we get here by enabling legacy-rx via:
  1735. *
  1736. * ethtool --set-priv-flags <dev> legacy-rx on
  1737. *
  1738. * In this mode, we currently get 0 extra XDP headroom as
  1739. * opposed to having legacy-rx off, where we process XDP
  1740. * packets going to stack via i40e_build_skb(). The latter
  1741. * provides us currently with 192 bytes of headroom.
  1742. *
  1743. * For i40e_construct_skb() mode it means that the
  1744. * xdp->data_meta will always point to xdp->data, since
  1745. * the helper cannot expand the head. Should this ever
  1746. * change in future for legacy-rx mode on, then lets also
  1747. * add xdp->data_meta handling here.
  1748. */
  1749. /* allocate a skb to store the frags */
  1750. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1751. I40E_RX_HDR_SIZE,
  1752. GFP_ATOMIC | __GFP_NOWARN);
  1753. if (unlikely(!skb))
  1754. return NULL;
  1755. /* Determine available headroom for copy */
  1756. headlen = size;
  1757. if (headlen > I40E_RX_HDR_SIZE)
  1758. headlen = eth_get_headlen(skb->dev, xdp->data,
  1759. I40E_RX_HDR_SIZE);
  1760. /* align pull length to size of long to optimize memcpy performance */
  1761. memcpy(__skb_put(skb, headlen), xdp->data,
  1762. ALIGN(headlen, sizeof(long)));
  1763. /* update all of the pointers */
  1764. size -= headlen;
  1765. if (size) {
  1766. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1767. rx_buffer->page_offset + headlen,
  1768. size, truesize);
  1769. /* buffer is used by skb, update page_offset */
  1770. #if (PAGE_SIZE < 8192)
  1771. rx_buffer->page_offset ^= truesize;
  1772. #else
  1773. rx_buffer->page_offset += truesize;
  1774. #endif
  1775. } else {
  1776. /* buffer is unused, reset bias back to rx_buffer */
  1777. rx_buffer->pagecnt_bias++;
  1778. }
  1779. return skb;
  1780. }
  1781. /**
  1782. * i40e_build_skb - Build skb around an existing buffer
  1783. * @rx_ring: Rx descriptor ring to transact packets on
  1784. * @rx_buffer: Rx buffer to pull data from
  1785. * @xdp: xdp_buff pointing to the data
  1786. *
  1787. * This function builds an skb around an existing Rx buffer, taking care
  1788. * to set up the skb correctly and avoid any memcpy overhead.
  1789. */
  1790. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1791. struct i40e_rx_buffer *rx_buffer,
  1792. struct xdp_buff *xdp)
  1793. {
  1794. unsigned int metasize = xdp->data - xdp->data_meta;
  1795. #if (PAGE_SIZE < 8192)
  1796. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1797. #else
  1798. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1799. SKB_DATA_ALIGN(xdp->data_end -
  1800. xdp->data_hard_start);
  1801. #endif
  1802. struct sk_buff *skb;
  1803. /* Prefetch first cache line of first page. If xdp->data_meta
  1804. * is unused, this points exactly as xdp->data, otherwise we
  1805. * likely have a consumer accessing first few bytes of meta
  1806. * data, and then actual data.
  1807. */
  1808. prefetch(xdp->data_meta);
  1809. #if L1_CACHE_BYTES < 128
  1810. prefetch(xdp->data_meta + L1_CACHE_BYTES);
  1811. #endif
  1812. /* build an skb around the page buffer */
  1813. skb = build_skb(xdp->data_hard_start, truesize);
  1814. if (unlikely(!skb))
  1815. return NULL;
  1816. /* update pointers within the skb to store the data */
  1817. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  1818. __skb_put(skb, xdp->data_end - xdp->data);
  1819. if (metasize)
  1820. skb_metadata_set(skb, metasize);
  1821. /* buffer is used by skb, update page_offset */
  1822. #if (PAGE_SIZE < 8192)
  1823. rx_buffer->page_offset ^= truesize;
  1824. #else
  1825. rx_buffer->page_offset += truesize;
  1826. #endif
  1827. return skb;
  1828. }
  1829. /**
  1830. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1831. * @rx_ring: rx descriptor ring to transact packets on
  1832. * @rx_buffer: rx buffer to pull data from
  1833. *
  1834. * This function will clean up the contents of the rx_buffer. It will
  1835. * either recycle the buffer or unmap it and free the associated resources.
  1836. */
  1837. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1838. struct i40e_rx_buffer *rx_buffer)
  1839. {
  1840. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1841. /* hand second half of page back to the ring */
  1842. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1843. } else {
  1844. /* we are not reusing the buffer so unmap it */
  1845. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1846. i40e_rx_pg_size(rx_ring),
  1847. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1848. __page_frag_cache_drain(rx_buffer->page,
  1849. rx_buffer->pagecnt_bias);
  1850. /* clear contents of buffer_info */
  1851. rx_buffer->page = NULL;
  1852. }
  1853. }
  1854. /**
  1855. * i40e_is_non_eop - process handling of non-EOP buffers
  1856. * @rx_ring: Rx ring being processed
  1857. * @rx_desc: Rx descriptor for current buffer
  1858. * @skb: Current socket buffer containing buffer in progress
  1859. *
  1860. * This function updates next to clean. If the buffer is an EOP buffer
  1861. * this function exits returning false, otherwise it will place the
  1862. * sk_buff in the next buffer to be chained and return true indicating
  1863. * that this is in fact a non-EOP buffer.
  1864. **/
  1865. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1866. union i40e_rx_desc *rx_desc,
  1867. struct sk_buff *skb)
  1868. {
  1869. u32 ntc = rx_ring->next_to_clean + 1;
  1870. /* fetch, update, and store next to clean */
  1871. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1872. rx_ring->next_to_clean = ntc;
  1873. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1874. /* if we are the last buffer then there is nothing else to do */
  1875. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1876. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1877. return false;
  1878. rx_ring->rx_stats.non_eop_descs++;
  1879. return true;
  1880. }
  1881. static int i40e_xmit_xdp_ring(struct xdp_frame *xdpf,
  1882. struct i40e_ring *xdp_ring);
  1883. int i40e_xmit_xdp_tx_ring(struct xdp_buff *xdp, struct i40e_ring *xdp_ring)
  1884. {
  1885. struct xdp_frame *xdpf = convert_to_xdp_frame(xdp);
  1886. if (unlikely(!xdpf))
  1887. return I40E_XDP_CONSUMED;
  1888. return i40e_xmit_xdp_ring(xdpf, xdp_ring);
  1889. }
  1890. /**
  1891. * i40e_run_xdp - run an XDP program
  1892. * @rx_ring: Rx ring being processed
  1893. * @xdp: XDP buffer containing the frame
  1894. **/
  1895. static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring,
  1896. struct xdp_buff *xdp)
  1897. {
  1898. int err, result = I40E_XDP_PASS;
  1899. struct i40e_ring *xdp_ring;
  1900. struct bpf_prog *xdp_prog;
  1901. u32 act;
  1902. rcu_read_lock();
  1903. xdp_prog = READ_ONCE(rx_ring->xdp_prog);
  1904. if (!xdp_prog)
  1905. goto xdp_out;
  1906. prefetchw(xdp->data_hard_start); /* xdp_frame write */
  1907. act = bpf_prog_run_xdp(xdp_prog, xdp);
  1908. switch (act) {
  1909. case XDP_PASS:
  1910. break;
  1911. case XDP_TX:
  1912. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1913. result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
  1914. break;
  1915. case XDP_REDIRECT:
  1916. err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
  1917. result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
  1918. break;
  1919. default:
  1920. bpf_warn_invalid_xdp_action(act);
  1921. /* fall through */
  1922. case XDP_ABORTED:
  1923. trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
  1924. /* fall through -- handle aborts by dropping packet */
  1925. case XDP_DROP:
  1926. result = I40E_XDP_CONSUMED;
  1927. break;
  1928. }
  1929. xdp_out:
  1930. rcu_read_unlock();
  1931. return ERR_PTR(-result);
  1932. }
  1933. /**
  1934. * i40e_rx_buffer_flip - adjusted rx_buffer to point to an unused region
  1935. * @rx_ring: Rx ring
  1936. * @rx_buffer: Rx buffer to adjust
  1937. * @size: Size of adjustment
  1938. **/
  1939. static void i40e_rx_buffer_flip(struct i40e_ring *rx_ring,
  1940. struct i40e_rx_buffer *rx_buffer,
  1941. unsigned int size)
  1942. {
  1943. #if (PAGE_SIZE < 8192)
  1944. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1945. rx_buffer->page_offset ^= truesize;
  1946. #else
  1947. unsigned int truesize = SKB_DATA_ALIGN(i40e_rx_offset(rx_ring) + size);
  1948. rx_buffer->page_offset += truesize;
  1949. #endif
  1950. }
  1951. /**
  1952. * i40e_xdp_ring_update_tail - Updates the XDP Tx ring tail register
  1953. * @xdp_ring: XDP Tx ring
  1954. *
  1955. * This function updates the XDP Tx ring tail register.
  1956. **/
  1957. void i40e_xdp_ring_update_tail(struct i40e_ring *xdp_ring)
  1958. {
  1959. /* Force memory writes to complete before letting h/w
  1960. * know there are new descriptors to fetch.
  1961. */
  1962. wmb();
  1963. writel_relaxed(xdp_ring->next_to_use, xdp_ring->tail);
  1964. }
  1965. /**
  1966. * i40e_update_rx_stats - Update Rx ring statistics
  1967. * @rx_ring: rx descriptor ring
  1968. * @total_rx_bytes: number of bytes received
  1969. * @total_rx_packets: number of packets received
  1970. *
  1971. * This function updates the Rx ring statistics.
  1972. **/
  1973. void i40e_update_rx_stats(struct i40e_ring *rx_ring,
  1974. unsigned int total_rx_bytes,
  1975. unsigned int total_rx_packets)
  1976. {
  1977. u64_stats_update_begin(&rx_ring->syncp);
  1978. rx_ring->stats.packets += total_rx_packets;
  1979. rx_ring->stats.bytes += total_rx_bytes;
  1980. u64_stats_update_end(&rx_ring->syncp);
  1981. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1982. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1983. }
  1984. /**
  1985. * i40e_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
  1986. * @rx_ring: Rx ring
  1987. * @xdp_res: Result of the receive batch
  1988. *
  1989. * This function bumps XDP Tx tail and/or flush redirect map, and
  1990. * should be called when a batch of packets has been processed in the
  1991. * napi loop.
  1992. **/
  1993. void i40e_finalize_xdp_rx(struct i40e_ring *rx_ring, unsigned int xdp_res)
  1994. {
  1995. if (xdp_res & I40E_XDP_REDIR)
  1996. xdp_do_flush_map();
  1997. if (xdp_res & I40E_XDP_TX) {
  1998. struct i40e_ring *xdp_ring =
  1999. rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  2000. i40e_xdp_ring_update_tail(xdp_ring);
  2001. }
  2002. }
  2003. /**
  2004. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  2005. * @rx_ring: rx descriptor ring to transact packets on
  2006. * @budget: Total limit on number of packets to process
  2007. *
  2008. * This function provides a "bounce buffer" approach to Rx interrupt
  2009. * processing. The advantage to this is that on systems that have
  2010. * expensive overhead for IOMMU access this provides a means of avoiding
  2011. * it by maintaining the mapping of the page to the system.
  2012. *
  2013. * Returns amount of work completed
  2014. **/
  2015. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  2016. {
  2017. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  2018. struct sk_buff *skb = rx_ring->skb;
  2019. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  2020. unsigned int xdp_xmit = 0;
  2021. bool failure = false;
  2022. struct xdp_buff xdp;
  2023. xdp.rxq = &rx_ring->xdp_rxq;
  2024. while (likely(total_rx_packets < (unsigned int)budget)) {
  2025. struct i40e_rx_buffer *rx_buffer;
  2026. union i40e_rx_desc *rx_desc;
  2027. unsigned int size;
  2028. u64 qword;
  2029. /* return some buffers to hardware, one at a time is too slow */
  2030. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  2031. failure = failure ||
  2032. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  2033. cleaned_count = 0;
  2034. }
  2035. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  2036. /* status_error_len will always be zero for unused descriptors
  2037. * because it's cleared in cleanup, and overlaps with hdr_addr
  2038. * which is always zero because packet split isn't used, if the
  2039. * hardware wrote DD then the length will be non-zero
  2040. */
  2041. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  2042. /* This memory barrier is needed to keep us from reading
  2043. * any other fields out of the rx_desc until we have
  2044. * verified the descriptor has been written back.
  2045. */
  2046. dma_rmb();
  2047. rx_buffer = i40e_clean_programming_status(rx_ring, rx_desc,
  2048. qword);
  2049. if (unlikely(rx_buffer)) {
  2050. i40e_reuse_rx_page(rx_ring, rx_buffer);
  2051. cleaned_count++;
  2052. continue;
  2053. }
  2054. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  2055. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  2056. if (!size)
  2057. break;
  2058. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  2059. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  2060. /* retrieve a buffer from the ring */
  2061. if (!skb) {
  2062. xdp.data = page_address(rx_buffer->page) +
  2063. rx_buffer->page_offset;
  2064. xdp.data_meta = xdp.data;
  2065. xdp.data_hard_start = xdp.data -
  2066. i40e_rx_offset(rx_ring);
  2067. xdp.data_end = xdp.data + size;
  2068. skb = i40e_run_xdp(rx_ring, &xdp);
  2069. }
  2070. if (IS_ERR(skb)) {
  2071. unsigned int xdp_res = -PTR_ERR(skb);
  2072. if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) {
  2073. xdp_xmit |= xdp_res;
  2074. i40e_rx_buffer_flip(rx_ring, rx_buffer, size);
  2075. } else {
  2076. rx_buffer->pagecnt_bias++;
  2077. }
  2078. total_rx_bytes += size;
  2079. total_rx_packets++;
  2080. } else if (skb) {
  2081. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  2082. } else if (ring_uses_build_skb(rx_ring)) {
  2083. skb = i40e_build_skb(rx_ring, rx_buffer, &xdp);
  2084. } else {
  2085. skb = i40e_construct_skb(rx_ring, rx_buffer, &xdp);
  2086. }
  2087. /* exit if we failed to retrieve a buffer */
  2088. if (!skb) {
  2089. rx_ring->rx_stats.alloc_buff_failed++;
  2090. rx_buffer->pagecnt_bias++;
  2091. break;
  2092. }
  2093. i40e_put_rx_buffer(rx_ring, rx_buffer);
  2094. cleaned_count++;
  2095. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  2096. continue;
  2097. if (i40e_cleanup_headers(rx_ring, skb, rx_desc)) {
  2098. skb = NULL;
  2099. continue;
  2100. }
  2101. /* probably a little skewed due to removing CRC */
  2102. total_rx_bytes += skb->len;
  2103. /* populate checksum, VLAN, and protocol */
  2104. i40e_process_skb_fields(rx_ring, rx_desc, skb);
  2105. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  2106. napi_gro_receive(&rx_ring->q_vector->napi, skb);
  2107. skb = NULL;
  2108. /* update budget accounting */
  2109. total_rx_packets++;
  2110. }
  2111. i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
  2112. rx_ring->skb = skb;
  2113. i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
  2114. /* guarantee a trip back through this routine if there was a failure */
  2115. return failure ? budget : (int)total_rx_packets;
  2116. }
  2117. static inline u32 i40e_buildreg_itr(const int type, u16 itr)
  2118. {
  2119. u32 val;
  2120. /* We don't bother with setting the CLEARPBA bit as the data sheet
  2121. * points out doing so is "meaningless since it was already
  2122. * auto-cleared". The auto-clearing happens when the interrupt is
  2123. * asserted.
  2124. *
  2125. * Hardware errata 28 for also indicates that writing to a
  2126. * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
  2127. * an event in the PBA anyway so we need to rely on the automask
  2128. * to hold pending events for us until the interrupt is re-enabled
  2129. *
  2130. * The itr value is reported in microseconds, and the register
  2131. * value is recorded in 2 microsecond units. For this reason we
  2132. * only need to shift by the interval shift - 1 instead of the
  2133. * full value.
  2134. */
  2135. itr &= I40E_ITR_MASK;
  2136. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  2137. (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
  2138. (itr << (I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT - 1));
  2139. return val;
  2140. }
  2141. /* a small macro to shorten up some long lines */
  2142. #define INTREG I40E_PFINT_DYN_CTLN
  2143. /* The act of updating the ITR will cause it to immediately trigger. In order
  2144. * to prevent this from throwing off adaptive update statistics we defer the
  2145. * update so that it can only happen so often. So after either Tx or Rx are
  2146. * updated we make the adaptive scheme wait until either the ITR completely
  2147. * expires via the next_update expiration or we have been through at least
  2148. * 3 interrupts.
  2149. */
  2150. #define ITR_COUNTDOWN_START 3
  2151. /**
  2152. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  2153. * @vsi: the VSI we care about
  2154. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  2155. *
  2156. **/
  2157. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  2158. struct i40e_q_vector *q_vector)
  2159. {
  2160. struct i40e_hw *hw = &vsi->back->hw;
  2161. u32 intval;
  2162. /* If we don't have MSIX, then we only need to re-enable icr0 */
  2163. if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED)) {
  2164. i40e_irq_dynamic_enable_icr0(vsi->back);
  2165. return;
  2166. }
  2167. /* These will do nothing if dynamic updates are not enabled */
  2168. i40e_update_itr(q_vector, &q_vector->tx);
  2169. i40e_update_itr(q_vector, &q_vector->rx);
  2170. /* This block of logic allows us to get away with only updating
  2171. * one ITR value with each interrupt. The idea is to perform a
  2172. * pseudo-lazy update with the following criteria.
  2173. *
  2174. * 1. Rx is given higher priority than Tx if both are in same state
  2175. * 2. If we must reduce an ITR that is given highest priority.
  2176. * 3. We then give priority to increasing ITR based on amount.
  2177. */
  2178. if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
  2179. /* Rx ITR needs to be reduced, this is highest priority */
  2180. intval = i40e_buildreg_itr(I40E_RX_ITR,
  2181. q_vector->rx.target_itr);
  2182. q_vector->rx.current_itr = q_vector->rx.target_itr;
  2183. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2184. } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
  2185. ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
  2186. (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
  2187. /* Tx ITR needs to be reduced, this is second priority
  2188. * Tx ITR needs to be increased more than Rx, fourth priority
  2189. */
  2190. intval = i40e_buildreg_itr(I40E_TX_ITR,
  2191. q_vector->tx.target_itr);
  2192. q_vector->tx.current_itr = q_vector->tx.target_itr;
  2193. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2194. } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
  2195. /* Rx ITR needs to be increased, third priority */
  2196. intval = i40e_buildreg_itr(I40E_RX_ITR,
  2197. q_vector->rx.target_itr);
  2198. q_vector->rx.current_itr = q_vector->rx.target_itr;
  2199. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2200. } else {
  2201. /* No ITR update, lowest priority */
  2202. intval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  2203. if (q_vector->itr_countdown)
  2204. q_vector->itr_countdown--;
  2205. }
  2206. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  2207. wr32(hw, INTREG(q_vector->reg_idx), intval);
  2208. }
  2209. /**
  2210. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  2211. * @napi: napi struct with our devices info in it
  2212. * @budget: amount of work driver is allowed to do this pass, in packets
  2213. *
  2214. * This function will clean all queues associated with a q_vector.
  2215. *
  2216. * Returns the amount of work done
  2217. **/
  2218. int i40e_napi_poll(struct napi_struct *napi, int budget)
  2219. {
  2220. struct i40e_q_vector *q_vector =
  2221. container_of(napi, struct i40e_q_vector, napi);
  2222. struct i40e_vsi *vsi = q_vector->vsi;
  2223. struct i40e_ring *ring;
  2224. bool clean_complete = true;
  2225. bool arm_wb = false;
  2226. int budget_per_ring;
  2227. int work_done = 0;
  2228. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  2229. napi_complete(napi);
  2230. return 0;
  2231. }
  2232. /* Since the actual Tx work is minimal, we can give the Tx a larger
  2233. * budget and be more aggressive about cleaning up the Tx descriptors.
  2234. */
  2235. i40e_for_each_ring(ring, q_vector->tx) {
  2236. bool wd = ring->xsk_umem ?
  2237. i40e_clean_xdp_tx_irq(vsi, ring, budget) :
  2238. i40e_clean_tx_irq(vsi, ring, budget);
  2239. if (!wd) {
  2240. clean_complete = false;
  2241. continue;
  2242. }
  2243. arm_wb |= ring->arm_wb;
  2244. ring->arm_wb = false;
  2245. }
  2246. /* Handle case where we are called by netpoll with a budget of 0 */
  2247. if (budget <= 0)
  2248. goto tx_only;
  2249. /* We attempt to distribute budget to each Rx queue fairly, but don't
  2250. * allow the budget to go below 1 because that would exit polling early.
  2251. */
  2252. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  2253. i40e_for_each_ring(ring, q_vector->rx) {
  2254. int cleaned = ring->xsk_umem ?
  2255. i40e_clean_rx_irq_zc(ring, budget_per_ring) :
  2256. i40e_clean_rx_irq(ring, budget_per_ring);
  2257. work_done += cleaned;
  2258. /* if we clean as many as budgeted, we must not be done */
  2259. if (cleaned >= budget_per_ring)
  2260. clean_complete = false;
  2261. }
  2262. /* If work not completed, return budget and polling will return */
  2263. if (!clean_complete) {
  2264. int cpu_id = smp_processor_id();
  2265. /* It is possible that the interrupt affinity has changed but,
  2266. * if the cpu is pegged at 100%, polling will never exit while
  2267. * traffic continues and the interrupt will be stuck on this
  2268. * cpu. We check to make sure affinity is correct before we
  2269. * continue to poll, otherwise we must stop polling so the
  2270. * interrupt can move to the correct cpu.
  2271. */
  2272. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  2273. /* Tell napi that we are done polling */
  2274. napi_complete_done(napi, work_done);
  2275. /* Force an interrupt */
  2276. i40e_force_wb(vsi, q_vector);
  2277. /* Return budget-1 so that polling stops */
  2278. return budget - 1;
  2279. }
  2280. tx_only:
  2281. if (arm_wb) {
  2282. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  2283. i40e_enable_wb_on_itr(vsi, q_vector);
  2284. }
  2285. return budget;
  2286. }
  2287. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  2288. q_vector->arm_wb_state = false;
  2289. /* Exit the polling mode, but don't re-enable interrupts if stack might
  2290. * poll us due to busy-polling
  2291. */
  2292. if (likely(napi_complete_done(napi, work_done)))
  2293. i40e_update_enable_itr(vsi, q_vector);
  2294. return min(work_done, budget - 1);
  2295. }
  2296. /**
  2297. * i40e_atr - Add a Flow Director ATR filter
  2298. * @tx_ring: ring to add programming descriptor to
  2299. * @skb: send buffer
  2300. * @tx_flags: send tx flags
  2301. **/
  2302. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2303. u32 tx_flags)
  2304. {
  2305. struct i40e_filter_program_desc *fdir_desc;
  2306. struct i40e_pf *pf = tx_ring->vsi->back;
  2307. union {
  2308. unsigned char *network;
  2309. struct iphdr *ipv4;
  2310. struct ipv6hdr *ipv6;
  2311. } hdr;
  2312. struct tcphdr *th;
  2313. unsigned int hlen;
  2314. u32 flex_ptype, dtype_cmd;
  2315. int l4_proto;
  2316. u16 i;
  2317. /* make sure ATR is enabled */
  2318. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  2319. return;
  2320. if (test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  2321. return;
  2322. /* if sampling is disabled do nothing */
  2323. if (!tx_ring->atr_sample_rate)
  2324. return;
  2325. /* Currently only IPv4/IPv6 with TCP is supported */
  2326. if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
  2327. return;
  2328. /* snag network header to get L4 type and address */
  2329. hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
  2330. skb_inner_network_header(skb) : skb_network_header(skb);
  2331. /* Note: tx_flags gets modified to reflect inner protocols in
  2332. * tx_enable_csum function if encap is enabled.
  2333. */
  2334. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  2335. /* access ihl as u8 to avoid unaligned access on ia64 */
  2336. hlen = (hdr.network[0] & 0x0F) << 2;
  2337. l4_proto = hdr.ipv4->protocol;
  2338. } else {
  2339. /* find the start of the innermost ipv6 header */
  2340. unsigned int inner_hlen = hdr.network - skb->data;
  2341. unsigned int h_offset = inner_hlen;
  2342. /* this function updates h_offset to the end of the header */
  2343. l4_proto =
  2344. ipv6_find_hdr(skb, &h_offset, IPPROTO_TCP, NULL, NULL);
  2345. /* hlen will contain our best estimate of the tcp header */
  2346. hlen = h_offset - inner_hlen;
  2347. }
  2348. if (l4_proto != IPPROTO_TCP)
  2349. return;
  2350. th = (struct tcphdr *)(hdr.network + hlen);
  2351. /* Due to lack of space, no more new filters can be programmed */
  2352. if (th->syn && test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  2353. return;
  2354. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) {
  2355. /* HW ATR eviction will take care of removing filters on FIN
  2356. * and RST packets.
  2357. */
  2358. if (th->fin || th->rst)
  2359. return;
  2360. }
  2361. tx_ring->atr_count++;
  2362. /* sample on all syn/fin/rst packets or once every atr sample rate */
  2363. if (!th->fin &&
  2364. !th->syn &&
  2365. !th->rst &&
  2366. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  2367. return;
  2368. tx_ring->atr_count = 0;
  2369. /* grab the next descriptor */
  2370. i = tx_ring->next_to_use;
  2371. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  2372. i++;
  2373. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2374. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  2375. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  2376. flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
  2377. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  2378. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  2379. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  2380. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  2381. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  2382. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  2383. dtype_cmd |= (th->fin || th->rst) ?
  2384. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  2385. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  2386. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  2387. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  2388. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  2389. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  2390. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  2391. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  2392. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  2393. if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
  2394. dtype_cmd |=
  2395. ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
  2396. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2397. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2398. else
  2399. dtype_cmd |=
  2400. ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
  2401. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2402. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2403. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED)
  2404. dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
  2405. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  2406. fdir_desc->rsvd = cpu_to_le32(0);
  2407. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  2408. fdir_desc->fd_id = cpu_to_le32(0);
  2409. }
  2410. /**
  2411. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  2412. * @skb: send buffer
  2413. * @tx_ring: ring to send buffer on
  2414. * @flags: the tx flags to be set
  2415. *
  2416. * Checks the skb and set up correspondingly several generic transmit flags
  2417. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  2418. *
  2419. * Returns error code indicate the frame should be dropped upon error and the
  2420. * otherwise returns 0 to indicate the flags has been set properly.
  2421. **/
  2422. static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  2423. struct i40e_ring *tx_ring,
  2424. u32 *flags)
  2425. {
  2426. __be16 protocol = skb->protocol;
  2427. u32 tx_flags = 0;
  2428. if (protocol == htons(ETH_P_8021Q) &&
  2429. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  2430. /* When HW VLAN acceleration is turned off by the user the
  2431. * stack sets the protocol to 8021q so that the driver
  2432. * can take any steps required to support the SW only
  2433. * VLAN handling. In our case the driver doesn't need
  2434. * to take any further steps so just set the protocol
  2435. * to the encapsulated ethertype.
  2436. */
  2437. skb->protocol = vlan_get_protocol(skb);
  2438. goto out;
  2439. }
  2440. /* if we have a HW VLAN tag being added, default to the HW one */
  2441. if (skb_vlan_tag_present(skb)) {
  2442. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  2443. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2444. /* else if it is a SW VLAN, check the next protocol and store the tag */
  2445. } else if (protocol == htons(ETH_P_8021Q)) {
  2446. struct vlan_hdr *vhdr, _vhdr;
  2447. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  2448. if (!vhdr)
  2449. return -EINVAL;
  2450. protocol = vhdr->h_vlan_encapsulated_proto;
  2451. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  2452. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  2453. }
  2454. if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2455. goto out;
  2456. /* Insert 802.1p priority into VLAN header */
  2457. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  2458. (skb->priority != TC_PRIO_CONTROL)) {
  2459. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  2460. tx_flags |= (skb->priority & 0x7) <<
  2461. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  2462. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  2463. struct vlan_ethhdr *vhdr;
  2464. int rc;
  2465. rc = skb_cow_head(skb, 0);
  2466. if (rc < 0)
  2467. return rc;
  2468. vhdr = (struct vlan_ethhdr *)skb->data;
  2469. vhdr->h_vlan_TCI = htons(tx_flags >>
  2470. I40E_TX_FLAGS_VLAN_SHIFT);
  2471. } else {
  2472. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2473. }
  2474. }
  2475. out:
  2476. *flags = tx_flags;
  2477. return 0;
  2478. }
  2479. /**
  2480. * i40e_tso - set up the tso context descriptor
  2481. * @first: pointer to first Tx buffer for xmit
  2482. * @hdr_len: ptr to the size of the packet header
  2483. * @cd_type_cmd_tso_mss: Quad Word 1
  2484. *
  2485. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  2486. **/
  2487. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  2488. u64 *cd_type_cmd_tso_mss)
  2489. {
  2490. struct sk_buff *skb = first->skb;
  2491. u64 cd_cmd, cd_tso_len, cd_mss;
  2492. union {
  2493. struct iphdr *v4;
  2494. struct ipv6hdr *v6;
  2495. unsigned char *hdr;
  2496. } ip;
  2497. union {
  2498. struct tcphdr *tcp;
  2499. struct udphdr *udp;
  2500. unsigned char *hdr;
  2501. } l4;
  2502. u32 paylen, l4_offset;
  2503. u16 gso_segs, gso_size;
  2504. int err;
  2505. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2506. return 0;
  2507. if (!skb_is_gso(skb))
  2508. return 0;
  2509. err = skb_cow_head(skb, 0);
  2510. if (err < 0)
  2511. return err;
  2512. ip.hdr = skb_network_header(skb);
  2513. l4.hdr = skb_transport_header(skb);
  2514. /* initialize outer IP header fields */
  2515. if (ip.v4->version == 4) {
  2516. ip.v4->tot_len = 0;
  2517. ip.v4->check = 0;
  2518. } else {
  2519. ip.v6->payload_len = 0;
  2520. }
  2521. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  2522. SKB_GSO_GRE_CSUM |
  2523. SKB_GSO_IPXIP4 |
  2524. SKB_GSO_IPXIP6 |
  2525. SKB_GSO_UDP_TUNNEL |
  2526. SKB_GSO_UDP_TUNNEL_CSUM)) {
  2527. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2528. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  2529. l4.udp->len = 0;
  2530. /* determine offset of outer transport header */
  2531. l4_offset = l4.hdr - skb->data;
  2532. /* remove payload length from outer checksum */
  2533. paylen = skb->len - l4_offset;
  2534. csum_replace_by_diff(&l4.udp->check,
  2535. (__force __wsum)htonl(paylen));
  2536. }
  2537. /* reset pointers to inner headers */
  2538. ip.hdr = skb_inner_network_header(skb);
  2539. l4.hdr = skb_inner_transport_header(skb);
  2540. /* initialize inner IP header fields */
  2541. if (ip.v4->version == 4) {
  2542. ip.v4->tot_len = 0;
  2543. ip.v4->check = 0;
  2544. } else {
  2545. ip.v6->payload_len = 0;
  2546. }
  2547. }
  2548. /* determine offset of inner transport header */
  2549. l4_offset = l4.hdr - skb->data;
  2550. /* remove payload length from inner checksum */
  2551. paylen = skb->len - l4_offset;
  2552. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
  2553. csum_replace_by_diff(&l4.udp->check, (__force __wsum)htonl(paylen));
  2554. /* compute length of segmentation header */
  2555. *hdr_len = sizeof(*l4.udp) + l4_offset;
  2556. } else {
  2557. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  2558. /* compute length of segmentation header */
  2559. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2560. }
  2561. /* pull values out of skb_shinfo */
  2562. gso_size = skb_shinfo(skb)->gso_size;
  2563. gso_segs = skb_shinfo(skb)->gso_segs;
  2564. /* update GSO size and bytecount with header size */
  2565. first->gso_segs = gso_segs;
  2566. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2567. /* find the field values */
  2568. cd_cmd = I40E_TX_CTX_DESC_TSO;
  2569. cd_tso_len = skb->len - *hdr_len;
  2570. cd_mss = gso_size;
  2571. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  2572. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  2573. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  2574. return 1;
  2575. }
  2576. /**
  2577. * i40e_tsyn - set up the tsyn context descriptor
  2578. * @tx_ring: ptr to the ring to send
  2579. * @skb: ptr to the skb we're sending
  2580. * @tx_flags: the collected send information
  2581. * @cd_type_cmd_tso_mss: Quad Word 1
  2582. *
  2583. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  2584. **/
  2585. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2586. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  2587. {
  2588. struct i40e_pf *pf;
  2589. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  2590. return 0;
  2591. /* Tx timestamps cannot be sampled when doing TSO */
  2592. if (tx_flags & I40E_TX_FLAGS_TSO)
  2593. return 0;
  2594. /* only timestamp the outbound packet if the user has requested it and
  2595. * we are not already transmitting a packet to be timestamped
  2596. */
  2597. pf = i40e_netdev_to_pf(tx_ring->netdev);
  2598. if (!(pf->flags & I40E_FLAG_PTP))
  2599. return 0;
  2600. if (pf->ptp_tx &&
  2601. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
  2602. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2603. pf->ptp_tx_start = jiffies;
  2604. pf->ptp_tx_skb = skb_get(skb);
  2605. } else {
  2606. pf->tx_hwtstamp_skipped++;
  2607. return 0;
  2608. }
  2609. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  2610. I40E_TXD_CTX_QW1_CMD_SHIFT;
  2611. return 1;
  2612. }
  2613. /**
  2614. * i40e_tx_enable_csum - Enable Tx checksum offloads
  2615. * @skb: send buffer
  2616. * @tx_flags: pointer to Tx flags currently set
  2617. * @td_cmd: Tx descriptor command bits to set
  2618. * @td_offset: Tx descriptor header offsets to set
  2619. * @tx_ring: Tx descriptor ring
  2620. * @cd_tunneling: ptr to context desc bits
  2621. **/
  2622. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  2623. u32 *td_cmd, u32 *td_offset,
  2624. struct i40e_ring *tx_ring,
  2625. u32 *cd_tunneling)
  2626. {
  2627. union {
  2628. struct iphdr *v4;
  2629. struct ipv6hdr *v6;
  2630. unsigned char *hdr;
  2631. } ip;
  2632. union {
  2633. struct tcphdr *tcp;
  2634. struct udphdr *udp;
  2635. unsigned char *hdr;
  2636. } l4;
  2637. unsigned char *exthdr;
  2638. u32 offset, cmd = 0;
  2639. __be16 frag_off;
  2640. u8 l4_proto = 0;
  2641. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2642. return 0;
  2643. ip.hdr = skb_network_header(skb);
  2644. l4.hdr = skb_transport_header(skb);
  2645. /* compute outer L2 header size */
  2646. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  2647. if (skb->encapsulation) {
  2648. u32 tunnel = 0;
  2649. /* define outer network header type */
  2650. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2651. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2652. I40E_TX_CTX_EXT_IP_IPV4 :
  2653. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  2654. l4_proto = ip.v4->protocol;
  2655. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2656. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  2657. exthdr = ip.hdr + sizeof(*ip.v6);
  2658. l4_proto = ip.v6->nexthdr;
  2659. if (l4.hdr != exthdr)
  2660. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2661. &l4_proto, &frag_off);
  2662. }
  2663. /* define outer transport */
  2664. switch (l4_proto) {
  2665. case IPPROTO_UDP:
  2666. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  2667. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2668. break;
  2669. case IPPROTO_GRE:
  2670. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  2671. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2672. break;
  2673. case IPPROTO_IPIP:
  2674. case IPPROTO_IPV6:
  2675. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2676. l4.hdr = skb_inner_network_header(skb);
  2677. break;
  2678. default:
  2679. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2680. return -1;
  2681. skb_checksum_help(skb);
  2682. return 0;
  2683. }
  2684. /* compute outer L3 header size */
  2685. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  2686. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  2687. /* switch IP header pointer from outer to inner header */
  2688. ip.hdr = skb_inner_network_header(skb);
  2689. /* compute tunnel header size */
  2690. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  2691. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  2692. /* indicate if we need to offload outer UDP header */
  2693. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  2694. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2695. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  2696. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  2697. /* record tunnel offload values */
  2698. *cd_tunneling |= tunnel;
  2699. /* switch L4 header pointer from outer to inner */
  2700. l4.hdr = skb_inner_transport_header(skb);
  2701. l4_proto = 0;
  2702. /* reset type as we transition from outer to inner headers */
  2703. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  2704. if (ip.v4->version == 4)
  2705. *tx_flags |= I40E_TX_FLAGS_IPV4;
  2706. if (ip.v6->version == 6)
  2707. *tx_flags |= I40E_TX_FLAGS_IPV6;
  2708. }
  2709. /* Enable IP checksum offloads */
  2710. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2711. l4_proto = ip.v4->protocol;
  2712. /* the stack computes the IP header already, the only time we
  2713. * need the hardware to recompute it is in the case of TSO.
  2714. */
  2715. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2716. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  2717. I40E_TX_DESC_CMD_IIPT_IPV4;
  2718. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2719. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  2720. exthdr = ip.hdr + sizeof(*ip.v6);
  2721. l4_proto = ip.v6->nexthdr;
  2722. if (l4.hdr != exthdr)
  2723. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2724. &l4_proto, &frag_off);
  2725. }
  2726. /* compute inner L3 header size */
  2727. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  2728. /* Enable L4 checksum offloads */
  2729. switch (l4_proto) {
  2730. case IPPROTO_TCP:
  2731. /* enable checksum offloads */
  2732. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  2733. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2734. break;
  2735. case IPPROTO_SCTP:
  2736. /* enable SCTP checksum offload */
  2737. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  2738. offset |= (sizeof(struct sctphdr) >> 2) <<
  2739. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2740. break;
  2741. case IPPROTO_UDP:
  2742. /* enable UDP checksum offload */
  2743. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  2744. offset |= (sizeof(struct udphdr) >> 2) <<
  2745. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2746. break;
  2747. default:
  2748. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2749. return -1;
  2750. skb_checksum_help(skb);
  2751. return 0;
  2752. }
  2753. *td_cmd |= cmd;
  2754. *td_offset |= offset;
  2755. return 1;
  2756. }
  2757. /**
  2758. * i40e_create_tx_ctx Build the Tx context descriptor
  2759. * @tx_ring: ring to create the descriptor on
  2760. * @cd_type_cmd_tso_mss: Quad Word 1
  2761. * @cd_tunneling: Quad Word 0 - bits 0-31
  2762. * @cd_l2tag2: Quad Word 0 - bits 32-63
  2763. **/
  2764. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  2765. const u64 cd_type_cmd_tso_mss,
  2766. const u32 cd_tunneling, const u32 cd_l2tag2)
  2767. {
  2768. struct i40e_tx_context_desc *context_desc;
  2769. int i = tx_ring->next_to_use;
  2770. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  2771. !cd_tunneling && !cd_l2tag2)
  2772. return;
  2773. /* grab the next descriptor */
  2774. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  2775. i++;
  2776. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2777. /* cpu_to_le32 and assign to struct fields */
  2778. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  2779. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  2780. context_desc->rsvd = cpu_to_le16(0);
  2781. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  2782. }
  2783. /**
  2784. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  2785. * @tx_ring: the ring to be checked
  2786. * @size: the size buffer we want to assure is available
  2787. *
  2788. * Returns -EBUSY if a stop is needed, else 0
  2789. **/
  2790. int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  2791. {
  2792. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2793. /* Memory barrier before checking head and tail */
  2794. smp_mb();
  2795. /* Check again in a case another CPU has just made room available. */
  2796. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  2797. return -EBUSY;
  2798. /* A reprieve! - use start_queue because it doesn't call schedule */
  2799. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2800. ++tx_ring->tx_stats.restart_queue;
  2801. return 0;
  2802. }
  2803. /**
  2804. * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
  2805. * @skb: send buffer
  2806. *
  2807. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  2808. * and so we need to figure out the cases where we need to linearize the skb.
  2809. *
  2810. * For TSO we need to count the TSO header and segment payload separately.
  2811. * As such we need to check cases where we have 7 fragments or more as we
  2812. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  2813. * the segment payload in the first descriptor, and another 7 for the
  2814. * fragments.
  2815. **/
  2816. bool __i40e_chk_linearize(struct sk_buff *skb)
  2817. {
  2818. const skb_frag_t *frag, *stale;
  2819. int nr_frags, sum;
  2820. /* no need to check if number of frags is less than 7 */
  2821. nr_frags = skb_shinfo(skb)->nr_frags;
  2822. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  2823. return false;
  2824. /* We need to walk through the list and validate that each group
  2825. * of 6 fragments totals at least gso_size.
  2826. */
  2827. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  2828. frag = &skb_shinfo(skb)->frags[0];
  2829. /* Initialize size to the negative value of gso_size minus 1. We
  2830. * use this as the worst case scenerio in which the frag ahead
  2831. * of us only provides one byte which is why we are limited to 6
  2832. * descriptors for a single transmit as the header and previous
  2833. * fragment are already consuming 2 descriptors.
  2834. */
  2835. sum = 1 - skb_shinfo(skb)->gso_size;
  2836. /* Add size of frags 0 through 4 to create our initial sum */
  2837. sum += skb_frag_size(frag++);
  2838. sum += skb_frag_size(frag++);
  2839. sum += skb_frag_size(frag++);
  2840. sum += skb_frag_size(frag++);
  2841. sum += skb_frag_size(frag++);
  2842. /* Walk through fragments adding latest fragment, testing it, and
  2843. * then removing stale fragments from the sum.
  2844. */
  2845. for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
  2846. int stale_size = skb_frag_size(stale);
  2847. sum += skb_frag_size(frag++);
  2848. /* The stale fragment may present us with a smaller
  2849. * descriptor than the actual fragment size. To account
  2850. * for that we need to remove all the data on the front and
  2851. * figure out what the remainder would be in the last
  2852. * descriptor associated with the fragment.
  2853. */
  2854. if (stale_size > I40E_MAX_DATA_PER_TXD) {
  2855. int align_pad = -(skb_frag_off(stale)) &
  2856. (I40E_MAX_READ_REQ_SIZE - 1);
  2857. sum -= align_pad;
  2858. stale_size -= align_pad;
  2859. do {
  2860. sum -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  2861. stale_size -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  2862. } while (stale_size > I40E_MAX_DATA_PER_TXD);
  2863. }
  2864. /* if sum is negative we failed to make sufficient progress */
  2865. if (sum < 0)
  2866. return true;
  2867. if (!nr_frags--)
  2868. break;
  2869. sum -= stale_size;
  2870. }
  2871. return false;
  2872. }
  2873. /**
  2874. * i40e_tx_map - Build the Tx descriptor
  2875. * @tx_ring: ring to send buffer on
  2876. * @skb: send buffer
  2877. * @first: first buffer info buffer to use
  2878. * @tx_flags: collected send information
  2879. * @hdr_len: size of the packet header
  2880. * @td_cmd: the command field in the descriptor
  2881. * @td_offset: offset for checksum or crc
  2882. *
  2883. * Returns 0 on success, -1 on failure to DMA
  2884. **/
  2885. static inline int i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2886. struct i40e_tx_buffer *first, u32 tx_flags,
  2887. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  2888. {
  2889. unsigned int data_len = skb->data_len;
  2890. unsigned int size = skb_headlen(skb);
  2891. skb_frag_t *frag;
  2892. struct i40e_tx_buffer *tx_bi;
  2893. struct i40e_tx_desc *tx_desc;
  2894. u16 i = tx_ring->next_to_use;
  2895. u32 td_tag = 0;
  2896. dma_addr_t dma;
  2897. u16 desc_count = 1;
  2898. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  2899. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  2900. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  2901. I40E_TX_FLAGS_VLAN_SHIFT;
  2902. }
  2903. first->tx_flags = tx_flags;
  2904. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2905. tx_desc = I40E_TX_DESC(tx_ring, i);
  2906. tx_bi = first;
  2907. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  2908. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2909. if (dma_mapping_error(tx_ring->dev, dma))
  2910. goto dma_error;
  2911. /* record length, and DMA address */
  2912. dma_unmap_len_set(tx_bi, len, size);
  2913. dma_unmap_addr_set(tx_bi, dma, dma);
  2914. /* align size to end of page */
  2915. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  2916. tx_desc->buffer_addr = cpu_to_le64(dma);
  2917. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  2918. tx_desc->cmd_type_offset_bsz =
  2919. build_ctob(td_cmd, td_offset,
  2920. max_data, td_tag);
  2921. tx_desc++;
  2922. i++;
  2923. desc_count++;
  2924. if (i == tx_ring->count) {
  2925. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2926. i = 0;
  2927. }
  2928. dma += max_data;
  2929. size -= max_data;
  2930. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2931. tx_desc->buffer_addr = cpu_to_le64(dma);
  2932. }
  2933. if (likely(!data_len))
  2934. break;
  2935. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  2936. size, td_tag);
  2937. tx_desc++;
  2938. i++;
  2939. desc_count++;
  2940. if (i == tx_ring->count) {
  2941. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2942. i = 0;
  2943. }
  2944. size = skb_frag_size(frag);
  2945. data_len -= size;
  2946. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2947. DMA_TO_DEVICE);
  2948. tx_bi = &tx_ring->tx_bi[i];
  2949. }
  2950. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2951. i++;
  2952. if (i == tx_ring->count)
  2953. i = 0;
  2954. tx_ring->next_to_use = i;
  2955. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2956. /* write last descriptor with EOP bit */
  2957. td_cmd |= I40E_TX_DESC_CMD_EOP;
  2958. /* We OR these values together to check both against 4 (WB_STRIDE)
  2959. * below. This is safe since we don't re-use desc_count afterwards.
  2960. */
  2961. desc_count |= ++tx_ring->packet_stride;
  2962. if (desc_count >= WB_STRIDE) {
  2963. /* write last descriptor with RS bit set */
  2964. td_cmd |= I40E_TX_DESC_CMD_RS;
  2965. tx_ring->packet_stride = 0;
  2966. }
  2967. tx_desc->cmd_type_offset_bsz =
  2968. build_ctob(td_cmd, td_offset, size, td_tag);
  2969. skb_tx_timestamp(skb);
  2970. /* Force memory writes to complete before letting h/w know there
  2971. * are new descriptors to fetch.
  2972. *
  2973. * We also use this memory barrier to make certain all of the
  2974. * status bits have been updated before next_to_watch is written.
  2975. */
  2976. wmb();
  2977. /* set next_to_watch value indicating a packet is present */
  2978. first->next_to_watch = tx_desc;
  2979. /* notify HW of packet */
  2980. if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
  2981. writel(i, tx_ring->tail);
  2982. }
  2983. return 0;
  2984. dma_error:
  2985. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2986. /* clear dma mappings for failed tx_bi map */
  2987. for (;;) {
  2988. tx_bi = &tx_ring->tx_bi[i];
  2989. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2990. if (tx_bi == first)
  2991. break;
  2992. if (i == 0)
  2993. i = tx_ring->count;
  2994. i--;
  2995. }
  2996. tx_ring->next_to_use = i;
  2997. return -1;
  2998. }
  2999. /**
  3000. * i40e_xmit_xdp_ring - transmits an XDP buffer to an XDP Tx ring
  3001. * @xdp: data to transmit
  3002. * @xdp_ring: XDP Tx ring
  3003. **/
  3004. static int i40e_xmit_xdp_ring(struct xdp_frame *xdpf,
  3005. struct i40e_ring *xdp_ring)
  3006. {
  3007. u16 i = xdp_ring->next_to_use;
  3008. struct i40e_tx_buffer *tx_bi;
  3009. struct i40e_tx_desc *tx_desc;
  3010. void *data = xdpf->data;
  3011. u32 size = xdpf->len;
  3012. dma_addr_t dma;
  3013. if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
  3014. xdp_ring->tx_stats.tx_busy++;
  3015. return I40E_XDP_CONSUMED;
  3016. }
  3017. dma = dma_map_single(xdp_ring->dev, data, size, DMA_TO_DEVICE);
  3018. if (dma_mapping_error(xdp_ring->dev, dma))
  3019. return I40E_XDP_CONSUMED;
  3020. tx_bi = &xdp_ring->tx_bi[i];
  3021. tx_bi->bytecount = size;
  3022. tx_bi->gso_segs = 1;
  3023. tx_bi->xdpf = xdpf;
  3024. /* record length, and DMA address */
  3025. dma_unmap_len_set(tx_bi, len, size);
  3026. dma_unmap_addr_set(tx_bi, dma, dma);
  3027. tx_desc = I40E_TX_DESC(xdp_ring, i);
  3028. tx_desc->buffer_addr = cpu_to_le64(dma);
  3029. tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC
  3030. | I40E_TXD_CMD,
  3031. 0, size, 0);
  3032. /* Make certain all of the status bits have been updated
  3033. * before next_to_watch is written.
  3034. */
  3035. smp_wmb();
  3036. i++;
  3037. if (i == xdp_ring->count)
  3038. i = 0;
  3039. tx_bi->next_to_watch = tx_desc;
  3040. xdp_ring->next_to_use = i;
  3041. return I40E_XDP_TX;
  3042. }
  3043. /**
  3044. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  3045. * @skb: send buffer
  3046. * @tx_ring: ring to send buffer on
  3047. *
  3048. * Returns NETDEV_TX_OK if sent, else an error code
  3049. **/
  3050. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  3051. struct i40e_ring *tx_ring)
  3052. {
  3053. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  3054. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  3055. struct i40e_tx_buffer *first;
  3056. u32 td_offset = 0;
  3057. u32 tx_flags = 0;
  3058. __be16 protocol;
  3059. u32 td_cmd = 0;
  3060. u8 hdr_len = 0;
  3061. int tso, count;
  3062. int tsyn;
  3063. /* prefetch the data, we'll need it later */
  3064. prefetch(skb->data);
  3065. i40e_trace(xmit_frame_ring, skb, tx_ring);
  3066. count = i40e_xmit_descriptor_count(skb);
  3067. if (i40e_chk_linearize(skb, count)) {
  3068. if (__skb_linearize(skb)) {
  3069. dev_kfree_skb_any(skb);
  3070. return NETDEV_TX_OK;
  3071. }
  3072. count = i40e_txd_use_count(skb->len);
  3073. tx_ring->tx_stats.tx_linearize++;
  3074. }
  3075. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  3076. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  3077. * + 4 desc gap to avoid the cache line where head is,
  3078. * + 1 desc for context descriptor,
  3079. * otherwise try next time
  3080. */
  3081. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  3082. tx_ring->tx_stats.tx_busy++;
  3083. return NETDEV_TX_BUSY;
  3084. }
  3085. /* record the location of the first descriptor for this packet */
  3086. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  3087. first->skb = skb;
  3088. first->bytecount = skb->len;
  3089. first->gso_segs = 1;
  3090. /* prepare the xmit flags */
  3091. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  3092. goto out_drop;
  3093. /* obtain protocol of skb */
  3094. protocol = vlan_get_protocol(skb);
  3095. /* setup IPv4/IPv6 offloads */
  3096. if (protocol == htons(ETH_P_IP))
  3097. tx_flags |= I40E_TX_FLAGS_IPV4;
  3098. else if (protocol == htons(ETH_P_IPV6))
  3099. tx_flags |= I40E_TX_FLAGS_IPV6;
  3100. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  3101. if (tso < 0)
  3102. goto out_drop;
  3103. else if (tso)
  3104. tx_flags |= I40E_TX_FLAGS_TSO;
  3105. /* Always offload the checksum, since it's in the data descriptor */
  3106. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  3107. tx_ring, &cd_tunneling);
  3108. if (tso < 0)
  3109. goto out_drop;
  3110. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  3111. if (tsyn)
  3112. tx_flags |= I40E_TX_FLAGS_TSYN;
  3113. /* always enable CRC insertion offload */
  3114. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  3115. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  3116. cd_tunneling, cd_l2tag2);
  3117. /* Add Flow Director ATR if it's enabled.
  3118. *
  3119. * NOTE: this must always be directly before the data descriptor.
  3120. */
  3121. i40e_atr(tx_ring, skb, tx_flags);
  3122. if (i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  3123. td_cmd, td_offset))
  3124. goto cleanup_tx_tstamp;
  3125. return NETDEV_TX_OK;
  3126. out_drop:
  3127. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  3128. dev_kfree_skb_any(first->skb);
  3129. first->skb = NULL;
  3130. cleanup_tx_tstamp:
  3131. if (unlikely(tx_flags & I40E_TX_FLAGS_TSYN)) {
  3132. struct i40e_pf *pf = i40e_netdev_to_pf(tx_ring->netdev);
  3133. dev_kfree_skb_any(pf->ptp_tx_skb);
  3134. pf->ptp_tx_skb = NULL;
  3135. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  3136. }
  3137. return NETDEV_TX_OK;
  3138. }
  3139. /**
  3140. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  3141. * @skb: send buffer
  3142. * @netdev: network interface device structure
  3143. *
  3144. * Returns NETDEV_TX_OK if sent, else an error code
  3145. **/
  3146. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  3147. {
  3148. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3149. struct i40e_vsi *vsi = np->vsi;
  3150. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  3151. /* hardware can't handle really short frames, hardware padding works
  3152. * beyond this point
  3153. */
  3154. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  3155. return NETDEV_TX_OK;
  3156. return i40e_xmit_frame_ring(skb, tx_ring);
  3157. }
  3158. /**
  3159. * i40e_xdp_xmit - Implements ndo_xdp_xmit
  3160. * @dev: netdev
  3161. * @xdp: XDP buffer
  3162. *
  3163. * Returns number of frames successfully sent. Frames that fail are
  3164. * free'ed via XDP return API.
  3165. *
  3166. * For error cases, a negative errno code is returned and no-frames
  3167. * are transmitted (caller must handle freeing frames).
  3168. **/
  3169. int i40e_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
  3170. u32 flags)
  3171. {
  3172. struct i40e_netdev_priv *np = netdev_priv(dev);
  3173. unsigned int queue_index = smp_processor_id();
  3174. struct i40e_vsi *vsi = np->vsi;
  3175. struct i40e_pf *pf = vsi->back;
  3176. struct i40e_ring *xdp_ring;
  3177. int drops = 0;
  3178. int i;
  3179. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3180. return -ENETDOWN;
  3181. if (!i40e_enabled_xdp_vsi(vsi) || queue_index >= vsi->num_queue_pairs ||
  3182. test_bit(__I40E_CONFIG_BUSY, pf->state))
  3183. return -ENXIO;
  3184. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  3185. return -EINVAL;
  3186. xdp_ring = vsi->xdp_rings[queue_index];
  3187. for (i = 0; i < n; i++) {
  3188. struct xdp_frame *xdpf = frames[i];
  3189. int err;
  3190. err = i40e_xmit_xdp_ring(xdpf, xdp_ring);
  3191. if (err != I40E_XDP_TX) {
  3192. xdp_return_frame_rx_napi(xdpf);
  3193. drops++;
  3194. }
  3195. }
  3196. if (unlikely(flags & XDP_XMIT_FLUSH))
  3197. i40e_xdp_ring_update_tail(xdp_ring);
  3198. return n - drops;
  3199. }