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