Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * |
| 3 | * Intel Ethernet Controller XL710 Family Linux Driver |
| 4 | * Copyright(c) 2013 Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * The full GNU General Public License is included in this distribution in |
| 20 | * the file called "COPYING". |
| 21 | * |
| 22 | * Contact Information: |
| 23 | * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> |
| 24 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 25 | * |
| 26 | ******************************************************************************/ |
| 27 | |
| 28 | #include "i40e.h" |
| 29 | |
| 30 | static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size, |
| 31 | u32 td_tag) |
| 32 | { |
| 33 | return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA | |
| 34 | ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) | |
| 35 | ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) | |
| 36 | ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) | |
| 37 | ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT)); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * i40e_program_fdir_filter - Program a Flow Director filter |
| 42 | * @fdir_input: Packet data that will be filter parameters |
| 43 | * @pf: The pf pointer |
| 44 | * @add: True for add/update, False for remove |
| 45 | **/ |
| 46 | int i40e_program_fdir_filter(struct i40e_fdir_data *fdir_data, |
| 47 | struct i40e_pf *pf, bool add) |
| 48 | { |
| 49 | struct i40e_filter_program_desc *fdir_desc; |
| 50 | struct i40e_tx_buffer *tx_buf; |
| 51 | struct i40e_tx_desc *tx_desc; |
| 52 | struct i40e_ring *tx_ring; |
| 53 | struct i40e_vsi *vsi; |
| 54 | struct device *dev; |
| 55 | dma_addr_t dma; |
| 56 | u32 td_cmd = 0; |
| 57 | u16 i; |
| 58 | |
| 59 | /* find existing FDIR VSI */ |
| 60 | vsi = NULL; |
| 61 | for (i = 0; i < pf->hw.func_caps.num_vsis; i++) |
| 62 | if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) |
| 63 | vsi = pf->vsi[i]; |
| 64 | if (!vsi) |
| 65 | return -ENOENT; |
| 66 | |
| 67 | tx_ring = &vsi->tx_rings[0]; |
| 68 | dev = tx_ring->dev; |
| 69 | |
| 70 | dma = dma_map_single(dev, fdir_data->raw_packet, |
| 71 | I40E_FDIR_MAX_RAW_PACKET_LOOKUP, DMA_TO_DEVICE); |
| 72 | if (dma_mapping_error(dev, dma)) |
| 73 | goto dma_fail; |
| 74 | |
| 75 | /* grab the next descriptor */ |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 76 | i = tx_ring->next_to_use; |
| 77 | fdir_desc = I40E_TX_FDIRDESC(tx_ring, i); |
| 78 | tx_buf = &tx_ring->tx_bi[i]; |
| 79 | |
| 80 | i++; |
| 81 | tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 82 | |
| 83 | fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32((fdir_data->q_index |
| 84 | << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) |
| 85 | & I40E_TXD_FLTR_QW0_QINDEX_MASK); |
| 86 | |
| 87 | fdir_desc->qindex_flex_ptype_vsi |= cpu_to_le32((fdir_data->flex_off |
| 88 | << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) |
| 89 | & I40E_TXD_FLTR_QW0_FLEXOFF_MASK); |
| 90 | |
| 91 | fdir_desc->qindex_flex_ptype_vsi |= cpu_to_le32((fdir_data->pctype |
| 92 | << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) |
| 93 | & I40E_TXD_FLTR_QW0_PCTYPE_MASK); |
| 94 | |
| 95 | /* Use LAN VSI Id if not programmed by user */ |
| 96 | if (fdir_data->dest_vsi == 0) |
| 97 | fdir_desc->qindex_flex_ptype_vsi |= |
| 98 | cpu_to_le32((pf->vsi[pf->lan_vsi]->id) |
| 99 | << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT); |
| 100 | else |
| 101 | fdir_desc->qindex_flex_ptype_vsi |= |
| 102 | cpu_to_le32((fdir_data->dest_vsi |
| 103 | << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) |
| 104 | & I40E_TXD_FLTR_QW0_DEST_VSI_MASK); |
| 105 | |
| 106 | fdir_desc->dtype_cmd_cntindex = |
| 107 | cpu_to_le32(I40E_TX_DESC_DTYPE_FILTER_PROG); |
| 108 | |
| 109 | if (add) |
| 110 | fdir_desc->dtype_cmd_cntindex |= cpu_to_le32( |
| 111 | I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE |
| 112 | << I40E_TXD_FLTR_QW1_PCMD_SHIFT); |
| 113 | else |
| 114 | fdir_desc->dtype_cmd_cntindex |= cpu_to_le32( |
| 115 | I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE |
| 116 | << I40E_TXD_FLTR_QW1_PCMD_SHIFT); |
| 117 | |
| 118 | fdir_desc->dtype_cmd_cntindex |= cpu_to_le32((fdir_data->dest_ctl |
| 119 | << I40E_TXD_FLTR_QW1_DEST_SHIFT) |
| 120 | & I40E_TXD_FLTR_QW1_DEST_MASK); |
| 121 | |
| 122 | fdir_desc->dtype_cmd_cntindex |= cpu_to_le32( |
| 123 | (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) |
| 124 | & I40E_TXD_FLTR_QW1_FD_STATUS_MASK); |
| 125 | |
| 126 | if (fdir_data->cnt_index != 0) { |
| 127 | fdir_desc->dtype_cmd_cntindex |= |
| 128 | cpu_to_le32(I40E_TXD_FLTR_QW1_CNT_ENA_MASK); |
| 129 | fdir_desc->dtype_cmd_cntindex |= |
| 130 | cpu_to_le32((fdir_data->cnt_index |
| 131 | << I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) |
| 132 | & I40E_TXD_FLTR_QW1_CNTINDEX_MASK); |
| 133 | } |
| 134 | |
| 135 | fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id); |
| 136 | |
| 137 | /* Now program a dummy descriptor */ |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 138 | i = tx_ring->next_to_use; |
| 139 | tx_desc = I40E_TX_DESC(tx_ring, i); |
| 140 | |
| 141 | i++; |
| 142 | tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 143 | |
| 144 | tx_desc->buffer_addr = cpu_to_le64(dma); |
| 145 | td_cmd = I40E_TX_DESC_CMD_EOP | |
| 146 | I40E_TX_DESC_CMD_RS | |
| 147 | I40E_TX_DESC_CMD_DUMMY; |
| 148 | |
| 149 | tx_desc->cmd_type_offset_bsz = |
| 150 | build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_LOOKUP, 0); |
| 151 | |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 152 | /* Force memory writes to complete before letting h/w |
| 153 | * know there are new descriptors to fetch. (Only |
| 154 | * applicable for weak-ordered memory model archs, |
| 155 | * such as IA-64). |
| 156 | */ |
| 157 | wmb(); |
| 158 | |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 159 | /* Mark the data descriptor to be watched */ |
| 160 | tx_buf->next_to_watch = tx_desc; |
| 161 | |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 162 | writel(tx_ring->next_to_use, tx_ring->tail); |
| 163 | return 0; |
| 164 | |
| 165 | dma_fail: |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * i40e_fd_handle_status - check the Programming Status for FD |
| 171 | * @rx_ring: the Rx ring for this descriptor |
| 172 | * @qw: the descriptor data |
| 173 | * @prog_id: the id originally used for programming |
| 174 | * |
| 175 | * This is used to verify if the FD programming or invalidation |
| 176 | * requested by SW to the HW is successful or not and take actions accordingly. |
| 177 | **/ |
| 178 | static void i40e_fd_handle_status(struct i40e_ring *rx_ring, u32 qw, u8 prog_id) |
| 179 | { |
| 180 | struct pci_dev *pdev = rx_ring->vsi->back->pdev; |
| 181 | u32 error; |
| 182 | |
| 183 | error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >> |
| 184 | I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT; |
| 185 | |
| 186 | /* for now just print the Status */ |
| 187 | dev_info(&pdev->dev, "FD programming id %02x, Status %08x\n", |
| 188 | prog_id, error); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * i40e_unmap_tx_resource - Release a Tx buffer |
| 193 | * @ring: the ring that owns the buffer |
| 194 | * @tx_buffer: the buffer to free |
| 195 | **/ |
| 196 | static inline void i40e_unmap_tx_resource(struct i40e_ring *ring, |
| 197 | struct i40e_tx_buffer *tx_buffer) |
| 198 | { |
Alexander Duyck | 35a1e2a | 2013-09-28 06:00:17 +0000 | [diff] [blame] | 199 | if (dma_unmap_len(tx_buffer, len)) { |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 200 | if (tx_buffer->tx_flags & I40E_TX_FLAGS_MAPPED_AS_PAGE) |
| 201 | dma_unmap_page(ring->dev, |
Alexander Duyck | 35a1e2a | 2013-09-28 06:00:17 +0000 | [diff] [blame] | 202 | dma_unmap_addr(tx_buffer, dma), |
| 203 | dma_unmap_len(tx_buffer, len), |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 204 | DMA_TO_DEVICE); |
| 205 | else |
| 206 | dma_unmap_single(ring->dev, |
Alexander Duyck | 35a1e2a | 2013-09-28 06:00:17 +0000 | [diff] [blame] | 207 | dma_unmap_addr(tx_buffer, dma), |
| 208 | dma_unmap_len(tx_buffer, len), |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 209 | DMA_TO_DEVICE); |
| 210 | } |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 211 | tx_buffer->time_stamp = 0; |
Alexander Duyck | 35a1e2a | 2013-09-28 06:00:17 +0000 | [diff] [blame] | 212 | dma_unmap_len_set(tx_buffer, len, 0); |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * i40e_clean_tx_ring - Free any empty Tx buffers |
| 217 | * @tx_ring: ring to be cleaned |
| 218 | **/ |
| 219 | void i40e_clean_tx_ring(struct i40e_ring *tx_ring) |
| 220 | { |
| 221 | struct i40e_tx_buffer *tx_buffer; |
| 222 | unsigned long bi_size; |
| 223 | u16 i; |
| 224 | |
| 225 | /* ring already cleared, nothing to do */ |
| 226 | if (!tx_ring->tx_bi) |
| 227 | return; |
| 228 | |
| 229 | /* Free all the Tx ring sk_buffs */ |
| 230 | for (i = 0; i < tx_ring->count; i++) { |
| 231 | tx_buffer = &tx_ring->tx_bi[i]; |
| 232 | i40e_unmap_tx_resource(tx_ring, tx_buffer); |
| 233 | if (tx_buffer->skb) |
| 234 | dev_kfree_skb_any(tx_buffer->skb); |
| 235 | tx_buffer->skb = NULL; |
| 236 | } |
| 237 | |
| 238 | bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count; |
| 239 | memset(tx_ring->tx_bi, 0, bi_size); |
| 240 | |
| 241 | /* Zero out the descriptor ring */ |
| 242 | memset(tx_ring->desc, 0, tx_ring->size); |
| 243 | |
| 244 | tx_ring->next_to_use = 0; |
| 245 | tx_ring->next_to_clean = 0; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * i40e_free_tx_resources - Free Tx resources per queue |
| 250 | * @tx_ring: Tx descriptor ring for a specific queue |
| 251 | * |
| 252 | * Free all transmit software resources |
| 253 | **/ |
| 254 | void i40e_free_tx_resources(struct i40e_ring *tx_ring) |
| 255 | { |
| 256 | i40e_clean_tx_ring(tx_ring); |
| 257 | kfree(tx_ring->tx_bi); |
| 258 | tx_ring->tx_bi = NULL; |
| 259 | |
| 260 | if (tx_ring->desc) { |
| 261 | dma_free_coherent(tx_ring->dev, tx_ring->size, |
| 262 | tx_ring->desc, tx_ring->dma); |
| 263 | tx_ring->desc = NULL; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * i40e_get_tx_pending - how many tx descriptors not processed |
| 269 | * @tx_ring: the ring of descriptors |
| 270 | * |
| 271 | * Since there is no access to the ring head register |
| 272 | * in XL710, we need to use our local copies |
| 273 | **/ |
| 274 | static u32 i40e_get_tx_pending(struct i40e_ring *ring) |
| 275 | { |
| 276 | u32 ntu = ((ring->next_to_clean <= ring->next_to_use) |
| 277 | ? ring->next_to_use |
| 278 | : ring->next_to_use + ring->count); |
| 279 | return ntu - ring->next_to_clean; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * i40e_check_tx_hang - Is there a hang in the Tx queue |
| 284 | * @tx_ring: the ring of descriptors |
| 285 | **/ |
| 286 | static bool i40e_check_tx_hang(struct i40e_ring *tx_ring) |
| 287 | { |
| 288 | u32 tx_pending = i40e_get_tx_pending(tx_ring); |
| 289 | bool ret = false; |
| 290 | |
| 291 | clear_check_for_tx_hang(tx_ring); |
| 292 | |
| 293 | /* Check for a hung queue, but be thorough. This verifies |
| 294 | * that a transmit has been completed since the previous |
| 295 | * check AND there is at least one packet pending. The |
| 296 | * ARMED bit is set to indicate a potential hang. The |
| 297 | * bit is cleared if a pause frame is received to remove |
| 298 | * false hang detection due to PFC or 802.3x frames. By |
| 299 | * requiring this to fail twice we avoid races with |
| 300 | * PFC clearing the ARMED bit and conditions where we |
| 301 | * run the check_tx_hang logic with a transmit completion |
| 302 | * pending but without time to complete it yet. |
| 303 | */ |
| 304 | if ((tx_ring->tx_stats.tx_done_old == tx_ring->tx_stats.packets) && |
| 305 | tx_pending) { |
| 306 | /* make sure it is true for two checks in a row */ |
| 307 | ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED, |
| 308 | &tx_ring->state); |
| 309 | } else { |
| 310 | /* update completed stats and disarm the hang check */ |
| 311 | tx_ring->tx_stats.tx_done_old = tx_ring->tx_stats.packets; |
| 312 | clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state); |
| 313 | } |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * i40e_clean_tx_irq - Reclaim resources after transmit completes |
| 320 | * @tx_ring: tx ring to clean |
| 321 | * @budget: how many cleans we're allowed |
| 322 | * |
| 323 | * Returns true if there's any budget left (e.g. the clean is finished) |
| 324 | **/ |
| 325 | static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget) |
| 326 | { |
| 327 | u16 i = tx_ring->next_to_clean; |
| 328 | struct i40e_tx_buffer *tx_buf; |
| 329 | struct i40e_tx_desc *tx_desc; |
| 330 | unsigned int total_packets = 0; |
| 331 | unsigned int total_bytes = 0; |
| 332 | |
| 333 | tx_buf = &tx_ring->tx_bi[i]; |
| 334 | tx_desc = I40E_TX_DESC(tx_ring, i); |
| 335 | |
| 336 | for (; budget; budget--) { |
| 337 | struct i40e_tx_desc *eop_desc; |
| 338 | |
| 339 | eop_desc = tx_buf->next_to_watch; |
| 340 | |
| 341 | /* if next_to_watch is not set then there is no work pending */ |
| 342 | if (!eop_desc) |
| 343 | break; |
| 344 | |
| 345 | /* if the descriptor isn't done, no work yet to do */ |
| 346 | if (!(eop_desc->cmd_type_offset_bsz & |
| 347 | cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE))) |
| 348 | break; |
| 349 | |
Alexander Duyck | c304fda | 2013-09-28 06:00:12 +0000 | [diff] [blame] | 350 | /* clear next_to_watch to prevent false hangs */ |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 351 | tx_buf->next_to_watch = NULL; |
| 352 | tx_buf->time_stamp = 0; |
| 353 | |
| 354 | /* set memory barrier before eop_desc is verified */ |
| 355 | rmb(); |
| 356 | |
| 357 | do { |
| 358 | i40e_unmap_tx_resource(tx_ring, tx_buf); |
| 359 | |
| 360 | /* clear dtype status */ |
| 361 | tx_desc->cmd_type_offset_bsz &= |
| 362 | ~cpu_to_le64(I40E_TXD_QW1_DTYPE_MASK); |
| 363 | |
| 364 | if (likely(tx_desc == eop_desc)) { |
| 365 | eop_desc = NULL; |
| 366 | |
| 367 | dev_kfree_skb_any(tx_buf->skb); |
| 368 | tx_buf->skb = NULL; |
| 369 | |
| 370 | total_bytes += tx_buf->bytecount; |
| 371 | total_packets += tx_buf->gso_segs; |
| 372 | } |
| 373 | |
| 374 | tx_buf++; |
| 375 | tx_desc++; |
| 376 | i++; |
| 377 | if (unlikely(i == tx_ring->count)) { |
| 378 | i = 0; |
| 379 | tx_buf = tx_ring->tx_bi; |
| 380 | tx_desc = I40E_TX_DESC(tx_ring, 0); |
| 381 | } |
| 382 | } while (eop_desc); |
| 383 | } |
| 384 | |
| 385 | tx_ring->next_to_clean = i; |
| 386 | tx_ring->tx_stats.bytes += total_bytes; |
| 387 | tx_ring->tx_stats.packets += total_packets; |
| 388 | tx_ring->q_vector->tx.total_bytes += total_bytes; |
| 389 | tx_ring->q_vector->tx.total_packets += total_packets; |
| 390 | if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) { |
| 391 | /* schedule immediate reset if we believe we hung */ |
| 392 | dev_info(tx_ring->dev, "Detected Tx Unit Hang\n" |
| 393 | " VSI <%d>\n" |
| 394 | " Tx Queue <%d>\n" |
| 395 | " next_to_use <%x>\n" |
| 396 | " next_to_clean <%x>\n", |
| 397 | tx_ring->vsi->seid, |
| 398 | tx_ring->queue_index, |
| 399 | tx_ring->next_to_use, i); |
| 400 | dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n" |
| 401 | " time_stamp <%lx>\n" |
| 402 | " jiffies <%lx>\n", |
| 403 | tx_ring->tx_bi[i].time_stamp, jiffies); |
| 404 | |
| 405 | netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index); |
| 406 | |
| 407 | dev_info(tx_ring->dev, |
| 408 | "tx hang detected on queue %d, resetting adapter\n", |
| 409 | tx_ring->queue_index); |
| 410 | |
| 411 | tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev); |
| 412 | |
| 413 | /* the adapter is about to reset, no point in enabling stuff */ |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2) |
| 418 | if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) && |
| 419 | (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) { |
| 420 | /* Make sure that anybody stopping the queue after this |
| 421 | * sees the new next_to_clean. |
| 422 | */ |
| 423 | smp_mb(); |
| 424 | if (__netif_subqueue_stopped(tx_ring->netdev, |
| 425 | tx_ring->queue_index) && |
| 426 | !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) { |
| 427 | netif_wake_subqueue(tx_ring->netdev, |
| 428 | tx_ring->queue_index); |
| 429 | ++tx_ring->tx_stats.restart_queue; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return budget > 0; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * i40e_set_new_dynamic_itr - Find new ITR level |
| 438 | * @rc: structure containing ring performance data |
| 439 | * |
| 440 | * Stores a new ITR value based on packets and byte counts during |
| 441 | * the last interrupt. The advantage of per interrupt computation |
| 442 | * is faster updates and more accurate ITR for the current traffic |
| 443 | * pattern. Constants in this function were computed based on |
| 444 | * theoretical maximum wire speed and thresholds were set based on |
| 445 | * testing data as well as attempting to minimize response time |
| 446 | * while increasing bulk throughput. |
| 447 | **/ |
| 448 | static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc) |
| 449 | { |
| 450 | enum i40e_latency_range new_latency_range = rc->latency_range; |
| 451 | u32 new_itr = rc->itr; |
| 452 | int bytes_per_int; |
| 453 | |
| 454 | if (rc->total_packets == 0 || !rc->itr) |
| 455 | return; |
| 456 | |
| 457 | /* simple throttlerate management |
| 458 | * 0-10MB/s lowest (100000 ints/s) |
| 459 | * 10-20MB/s low (20000 ints/s) |
| 460 | * 20-1249MB/s bulk (8000 ints/s) |
| 461 | */ |
| 462 | bytes_per_int = rc->total_bytes / rc->itr; |
| 463 | switch (rc->itr) { |
| 464 | case I40E_LOWEST_LATENCY: |
| 465 | if (bytes_per_int > 10) |
| 466 | new_latency_range = I40E_LOW_LATENCY; |
| 467 | break; |
| 468 | case I40E_LOW_LATENCY: |
| 469 | if (bytes_per_int > 20) |
| 470 | new_latency_range = I40E_BULK_LATENCY; |
| 471 | else if (bytes_per_int <= 10) |
| 472 | new_latency_range = I40E_LOWEST_LATENCY; |
| 473 | break; |
| 474 | case I40E_BULK_LATENCY: |
| 475 | if (bytes_per_int <= 20) |
| 476 | rc->latency_range = I40E_LOW_LATENCY; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | switch (new_latency_range) { |
| 481 | case I40E_LOWEST_LATENCY: |
| 482 | new_itr = I40E_ITR_100K; |
| 483 | break; |
| 484 | case I40E_LOW_LATENCY: |
| 485 | new_itr = I40E_ITR_20K; |
| 486 | break; |
| 487 | case I40E_BULK_LATENCY: |
| 488 | new_itr = I40E_ITR_8K; |
| 489 | break; |
| 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | if (new_itr != rc->itr) { |
| 495 | /* do an exponential smoothing */ |
| 496 | new_itr = (10 * new_itr * rc->itr) / |
| 497 | ((9 * new_itr) + rc->itr); |
| 498 | rc->itr = new_itr & I40E_MAX_ITR; |
| 499 | } |
| 500 | |
| 501 | rc->total_bytes = 0; |
| 502 | rc->total_packets = 0; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * i40e_update_dynamic_itr - Adjust ITR based on bytes per int |
| 507 | * @q_vector: the vector to adjust |
| 508 | **/ |
| 509 | static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector) |
| 510 | { |
| 511 | u16 vector = q_vector->vsi->base_vector + q_vector->v_idx; |
| 512 | struct i40e_hw *hw = &q_vector->vsi->back->hw; |
| 513 | u32 reg_addr; |
| 514 | u16 old_itr; |
| 515 | |
| 516 | reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1); |
| 517 | old_itr = q_vector->rx.itr; |
| 518 | i40e_set_new_dynamic_itr(&q_vector->rx); |
| 519 | if (old_itr != q_vector->rx.itr) |
| 520 | wr32(hw, reg_addr, q_vector->rx.itr); |
| 521 | |
| 522 | reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1); |
| 523 | old_itr = q_vector->tx.itr; |
| 524 | i40e_set_new_dynamic_itr(&q_vector->tx); |
| 525 | if (old_itr != q_vector->tx.itr) |
| 526 | wr32(hw, reg_addr, q_vector->tx.itr); |
| 527 | |
| 528 | i40e_flush(hw); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * i40e_clean_programming_status - clean the programming status descriptor |
| 533 | * @rx_ring: the rx ring that has this descriptor |
| 534 | * @rx_desc: the rx descriptor written back by HW |
| 535 | * |
| 536 | * Flow director should handle FD_FILTER_STATUS to check its filter programming |
| 537 | * status being successful or not and take actions accordingly. FCoE should |
| 538 | * handle its context/filter programming/invalidation status and take actions. |
| 539 | * |
| 540 | **/ |
| 541 | static void i40e_clean_programming_status(struct i40e_ring *rx_ring, |
| 542 | union i40e_rx_desc *rx_desc) |
| 543 | { |
| 544 | u64 qw; |
| 545 | u8 id; |
| 546 | |
| 547 | qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len); |
| 548 | id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >> |
| 549 | I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT; |
| 550 | |
| 551 | if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS) |
| 552 | i40e_fd_handle_status(rx_ring, qw, id); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * i40e_setup_tx_descriptors - Allocate the Tx descriptors |
| 557 | * @tx_ring: the tx ring to set up |
| 558 | * |
| 559 | * Return 0 on success, negative on error |
| 560 | **/ |
| 561 | int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring) |
| 562 | { |
| 563 | struct device *dev = tx_ring->dev; |
| 564 | int bi_size; |
| 565 | |
| 566 | if (!dev) |
| 567 | return -ENOMEM; |
| 568 | |
| 569 | bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count; |
| 570 | tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL); |
| 571 | if (!tx_ring->tx_bi) |
| 572 | goto err; |
| 573 | |
| 574 | /* round up to nearest 4K */ |
| 575 | tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc); |
| 576 | tx_ring->size = ALIGN(tx_ring->size, 4096); |
| 577 | tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size, |
| 578 | &tx_ring->dma, GFP_KERNEL); |
| 579 | if (!tx_ring->desc) { |
| 580 | dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n", |
| 581 | tx_ring->size); |
| 582 | goto err; |
| 583 | } |
| 584 | |
| 585 | tx_ring->next_to_use = 0; |
| 586 | tx_ring->next_to_clean = 0; |
| 587 | return 0; |
| 588 | |
| 589 | err: |
| 590 | kfree(tx_ring->tx_bi); |
| 591 | tx_ring->tx_bi = NULL; |
| 592 | return -ENOMEM; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * i40e_clean_rx_ring - Free Rx buffers |
| 597 | * @rx_ring: ring to be cleaned |
| 598 | **/ |
| 599 | void i40e_clean_rx_ring(struct i40e_ring *rx_ring) |
| 600 | { |
| 601 | struct device *dev = rx_ring->dev; |
| 602 | struct i40e_rx_buffer *rx_bi; |
| 603 | unsigned long bi_size; |
| 604 | u16 i; |
| 605 | |
| 606 | /* ring already cleared, nothing to do */ |
| 607 | if (!rx_ring->rx_bi) |
| 608 | return; |
| 609 | |
| 610 | /* Free all the Rx ring sk_buffs */ |
| 611 | for (i = 0; i < rx_ring->count; i++) { |
| 612 | rx_bi = &rx_ring->rx_bi[i]; |
| 613 | if (rx_bi->dma) { |
| 614 | dma_unmap_single(dev, |
| 615 | rx_bi->dma, |
| 616 | rx_ring->rx_buf_len, |
| 617 | DMA_FROM_DEVICE); |
| 618 | rx_bi->dma = 0; |
| 619 | } |
| 620 | if (rx_bi->skb) { |
| 621 | dev_kfree_skb(rx_bi->skb); |
| 622 | rx_bi->skb = NULL; |
| 623 | } |
| 624 | if (rx_bi->page) { |
| 625 | if (rx_bi->page_dma) { |
| 626 | dma_unmap_page(dev, |
| 627 | rx_bi->page_dma, |
| 628 | PAGE_SIZE / 2, |
| 629 | DMA_FROM_DEVICE); |
| 630 | rx_bi->page_dma = 0; |
| 631 | } |
| 632 | __free_page(rx_bi->page); |
| 633 | rx_bi->page = NULL; |
| 634 | rx_bi->page_offset = 0; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count; |
| 639 | memset(rx_ring->rx_bi, 0, bi_size); |
| 640 | |
| 641 | /* Zero out the descriptor ring */ |
| 642 | memset(rx_ring->desc, 0, rx_ring->size); |
| 643 | |
| 644 | rx_ring->next_to_clean = 0; |
| 645 | rx_ring->next_to_use = 0; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * i40e_free_rx_resources - Free Rx resources |
| 650 | * @rx_ring: ring to clean the resources from |
| 651 | * |
| 652 | * Free all receive software resources |
| 653 | **/ |
| 654 | void i40e_free_rx_resources(struct i40e_ring *rx_ring) |
| 655 | { |
| 656 | i40e_clean_rx_ring(rx_ring); |
| 657 | kfree(rx_ring->rx_bi); |
| 658 | rx_ring->rx_bi = NULL; |
| 659 | |
| 660 | if (rx_ring->desc) { |
| 661 | dma_free_coherent(rx_ring->dev, rx_ring->size, |
| 662 | rx_ring->desc, rx_ring->dma); |
| 663 | rx_ring->desc = NULL; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * i40e_setup_rx_descriptors - Allocate Rx descriptors |
| 669 | * @rx_ring: Rx descriptor ring (for a specific queue) to setup |
| 670 | * |
| 671 | * Returns 0 on success, negative on failure |
| 672 | **/ |
| 673 | int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring) |
| 674 | { |
| 675 | struct device *dev = rx_ring->dev; |
| 676 | int bi_size; |
| 677 | |
| 678 | bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count; |
| 679 | rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL); |
| 680 | if (!rx_ring->rx_bi) |
| 681 | goto err; |
| 682 | |
| 683 | /* Round up to nearest 4K */ |
| 684 | rx_ring->size = ring_is_16byte_desc_enabled(rx_ring) |
| 685 | ? rx_ring->count * sizeof(union i40e_16byte_rx_desc) |
| 686 | : rx_ring->count * sizeof(union i40e_32byte_rx_desc); |
| 687 | rx_ring->size = ALIGN(rx_ring->size, 4096); |
| 688 | rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size, |
| 689 | &rx_ring->dma, GFP_KERNEL); |
| 690 | |
| 691 | if (!rx_ring->desc) { |
| 692 | dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n", |
| 693 | rx_ring->size); |
| 694 | goto err; |
| 695 | } |
| 696 | |
| 697 | rx_ring->next_to_clean = 0; |
| 698 | rx_ring->next_to_use = 0; |
| 699 | |
| 700 | return 0; |
| 701 | err: |
| 702 | kfree(rx_ring->rx_bi); |
| 703 | rx_ring->rx_bi = NULL; |
| 704 | return -ENOMEM; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * i40e_release_rx_desc - Store the new tail and head values |
| 709 | * @rx_ring: ring to bump |
| 710 | * @val: new head index |
| 711 | **/ |
| 712 | static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val) |
| 713 | { |
| 714 | rx_ring->next_to_use = val; |
| 715 | /* Force memory writes to complete before letting h/w |
| 716 | * know there are new descriptors to fetch. (Only |
| 717 | * applicable for weak-ordered memory model archs, |
| 718 | * such as IA-64). |
| 719 | */ |
| 720 | wmb(); |
| 721 | writel(val, rx_ring->tail); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * i40e_alloc_rx_buffers - Replace used receive buffers; packet split |
| 726 | * @rx_ring: ring to place buffers on |
| 727 | * @cleaned_count: number of buffers to replace |
| 728 | **/ |
| 729 | void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count) |
| 730 | { |
| 731 | u16 i = rx_ring->next_to_use; |
| 732 | union i40e_rx_desc *rx_desc; |
| 733 | struct i40e_rx_buffer *bi; |
| 734 | struct sk_buff *skb; |
| 735 | |
| 736 | /* do nothing if no valid netdev defined */ |
| 737 | if (!rx_ring->netdev || !cleaned_count) |
| 738 | return; |
| 739 | |
| 740 | while (cleaned_count--) { |
| 741 | rx_desc = I40E_RX_DESC(rx_ring, i); |
| 742 | bi = &rx_ring->rx_bi[i]; |
| 743 | skb = bi->skb; |
| 744 | |
| 745 | if (!skb) { |
| 746 | skb = netdev_alloc_skb_ip_align(rx_ring->netdev, |
| 747 | rx_ring->rx_buf_len); |
| 748 | if (!skb) { |
| 749 | rx_ring->rx_stats.alloc_rx_buff_failed++; |
| 750 | goto no_buffers; |
| 751 | } |
| 752 | /* initialize queue mapping */ |
| 753 | skb_record_rx_queue(skb, rx_ring->queue_index); |
| 754 | bi->skb = skb; |
| 755 | } |
| 756 | |
| 757 | if (!bi->dma) { |
| 758 | bi->dma = dma_map_single(rx_ring->dev, |
| 759 | skb->data, |
| 760 | rx_ring->rx_buf_len, |
| 761 | DMA_FROM_DEVICE); |
| 762 | if (dma_mapping_error(rx_ring->dev, bi->dma)) { |
| 763 | rx_ring->rx_stats.alloc_rx_buff_failed++; |
| 764 | bi->dma = 0; |
| 765 | goto no_buffers; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (ring_is_ps_enabled(rx_ring)) { |
| 770 | if (!bi->page) { |
| 771 | bi->page = alloc_page(GFP_ATOMIC); |
| 772 | if (!bi->page) { |
| 773 | rx_ring->rx_stats.alloc_rx_page_failed++; |
| 774 | goto no_buffers; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if (!bi->page_dma) { |
| 779 | /* use a half page if we're re-using */ |
| 780 | bi->page_offset ^= PAGE_SIZE / 2; |
| 781 | bi->page_dma = dma_map_page(rx_ring->dev, |
| 782 | bi->page, |
| 783 | bi->page_offset, |
| 784 | PAGE_SIZE / 2, |
| 785 | DMA_FROM_DEVICE); |
| 786 | if (dma_mapping_error(rx_ring->dev, |
| 787 | bi->page_dma)) { |
| 788 | rx_ring->rx_stats.alloc_rx_page_failed++; |
| 789 | bi->page_dma = 0; |
| 790 | goto no_buffers; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /* Refresh the desc even if buffer_addrs didn't change |
| 795 | * because each write-back erases this info. |
| 796 | */ |
| 797 | rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma); |
| 798 | rx_desc->read.hdr_addr = cpu_to_le64(bi->dma); |
| 799 | } else { |
| 800 | rx_desc->read.pkt_addr = cpu_to_le64(bi->dma); |
| 801 | rx_desc->read.hdr_addr = 0; |
| 802 | } |
| 803 | i++; |
| 804 | if (i == rx_ring->count) |
| 805 | i = 0; |
| 806 | } |
| 807 | |
| 808 | no_buffers: |
| 809 | if (rx_ring->next_to_use != i) |
| 810 | i40e_release_rx_desc(rx_ring, i); |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * i40e_receive_skb - Send a completed packet up the stack |
| 815 | * @rx_ring: rx ring in play |
| 816 | * @skb: packet to send up |
| 817 | * @vlan_tag: vlan tag for packet |
| 818 | **/ |
| 819 | static void i40e_receive_skb(struct i40e_ring *rx_ring, |
| 820 | struct sk_buff *skb, u16 vlan_tag) |
| 821 | { |
| 822 | struct i40e_q_vector *q_vector = rx_ring->q_vector; |
| 823 | struct i40e_vsi *vsi = rx_ring->vsi; |
| 824 | u64 flags = vsi->back->flags; |
| 825 | |
| 826 | if (vlan_tag & VLAN_VID_MASK) |
| 827 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag); |
| 828 | |
| 829 | if (flags & I40E_FLAG_IN_NETPOLL) |
| 830 | netif_rx(skb); |
| 831 | else |
| 832 | napi_gro_receive(&q_vector->napi, skb); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum |
| 837 | * @vsi: the VSI we care about |
| 838 | * @skb: skb currently being received and modified |
| 839 | * @rx_status: status value of last descriptor in packet |
| 840 | * @rx_error: error value of last descriptor in packet |
| 841 | **/ |
| 842 | static inline void i40e_rx_checksum(struct i40e_vsi *vsi, |
| 843 | struct sk_buff *skb, |
| 844 | u32 rx_status, |
| 845 | u32 rx_error) |
| 846 | { |
| 847 | skb->ip_summed = CHECKSUM_NONE; |
| 848 | |
| 849 | /* Rx csum enabled and ip headers found? */ |
| 850 | if (!(vsi->netdev->features & NETIF_F_RXCSUM && |
| 851 | rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT))) |
| 852 | return; |
| 853 | |
| 854 | /* IP or L4 checksum error */ |
| 855 | if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) | |
| 856 | (1 << I40E_RX_DESC_ERROR_L4E_SHIFT))) { |
| 857 | vsi->back->hw_csum_rx_error++; |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * i40e_rx_hash - returns the hash value from the Rx descriptor |
| 866 | * @ring: descriptor ring |
| 867 | * @rx_desc: specific descriptor |
| 868 | **/ |
| 869 | static inline u32 i40e_rx_hash(struct i40e_ring *ring, |
| 870 | union i40e_rx_desc *rx_desc) |
| 871 | { |
| 872 | if (ring->netdev->features & NETIF_F_RXHASH) { |
| 873 | if ((le64_to_cpu(rx_desc->wb.qword1.status_error_len) >> |
| 874 | I40E_RX_DESC_STATUS_FLTSTAT_SHIFT) & |
| 875 | I40E_RX_DESC_FLTSTAT_RSS_HASH) |
| 876 | return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss); |
| 877 | } |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * i40e_clean_rx_irq - Reclaim resources after receive completes |
| 883 | * @rx_ring: rx ring to clean |
| 884 | * @budget: how many cleans we're allowed |
| 885 | * |
| 886 | * Returns true if there's any budget left (e.g. the clean is finished) |
| 887 | **/ |
| 888 | static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget) |
| 889 | { |
| 890 | unsigned int total_rx_bytes = 0, total_rx_packets = 0; |
| 891 | u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo; |
| 892 | u16 cleaned_count = I40E_DESC_UNUSED(rx_ring); |
| 893 | const int current_node = numa_node_id(); |
| 894 | struct i40e_vsi *vsi = rx_ring->vsi; |
| 895 | u16 i = rx_ring->next_to_clean; |
| 896 | union i40e_rx_desc *rx_desc; |
| 897 | u32 rx_error, rx_status; |
| 898 | u64 qword; |
| 899 | |
| 900 | rx_desc = I40E_RX_DESC(rx_ring, i); |
| 901 | qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); |
| 902 | rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) |
| 903 | >> I40E_RXD_QW1_STATUS_SHIFT; |
| 904 | |
| 905 | while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) { |
| 906 | union i40e_rx_desc *next_rxd; |
| 907 | struct i40e_rx_buffer *rx_bi; |
| 908 | struct sk_buff *skb; |
| 909 | u16 vlan_tag; |
| 910 | if (i40e_rx_is_programming_status(qword)) { |
| 911 | i40e_clean_programming_status(rx_ring, rx_desc); |
| 912 | I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd); |
| 913 | goto next_desc; |
| 914 | } |
| 915 | rx_bi = &rx_ring->rx_bi[i]; |
| 916 | skb = rx_bi->skb; |
| 917 | prefetch(skb->data); |
| 918 | |
| 919 | rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) |
| 920 | >> I40E_RXD_QW1_LENGTH_PBUF_SHIFT; |
| 921 | rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) |
| 922 | >> I40E_RXD_QW1_LENGTH_HBUF_SHIFT; |
| 923 | rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) |
| 924 | >> I40E_RXD_QW1_LENGTH_SPH_SHIFT; |
| 925 | |
| 926 | rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) |
| 927 | >> I40E_RXD_QW1_ERROR_SHIFT; |
| 928 | rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT); |
| 929 | rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT); |
| 930 | |
| 931 | rx_bi->skb = NULL; |
| 932 | |
| 933 | /* This memory barrier is needed to keep us from reading |
| 934 | * any other fields out of the rx_desc until we know the |
| 935 | * STATUS_DD bit is set |
| 936 | */ |
| 937 | rmb(); |
| 938 | |
| 939 | /* Get the header and possibly the whole packet |
| 940 | * If this is an skb from previous receive dma will be 0 |
| 941 | */ |
| 942 | if (rx_bi->dma) { |
| 943 | u16 len; |
| 944 | |
| 945 | if (rx_hbo) |
| 946 | len = I40E_RX_HDR_SIZE; |
| 947 | else if (rx_sph) |
| 948 | len = rx_header_len; |
| 949 | else if (rx_packet_len) |
| 950 | len = rx_packet_len; /* 1buf/no split found */ |
| 951 | else |
| 952 | len = rx_header_len; /* split always mode */ |
| 953 | |
| 954 | skb_put(skb, len); |
| 955 | dma_unmap_single(rx_ring->dev, |
| 956 | rx_bi->dma, |
| 957 | rx_ring->rx_buf_len, |
| 958 | DMA_FROM_DEVICE); |
| 959 | rx_bi->dma = 0; |
| 960 | } |
| 961 | |
| 962 | /* Get the rest of the data if this was a header split */ |
| 963 | if (ring_is_ps_enabled(rx_ring) && rx_packet_len) { |
| 964 | |
| 965 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, |
| 966 | rx_bi->page, |
| 967 | rx_bi->page_offset, |
| 968 | rx_packet_len); |
| 969 | |
| 970 | skb->len += rx_packet_len; |
| 971 | skb->data_len += rx_packet_len; |
| 972 | skb->truesize += rx_packet_len; |
| 973 | |
| 974 | if ((page_count(rx_bi->page) == 1) && |
| 975 | (page_to_nid(rx_bi->page) == current_node)) |
| 976 | get_page(rx_bi->page); |
| 977 | else |
| 978 | rx_bi->page = NULL; |
| 979 | |
| 980 | dma_unmap_page(rx_ring->dev, |
| 981 | rx_bi->page_dma, |
| 982 | PAGE_SIZE / 2, |
| 983 | DMA_FROM_DEVICE); |
| 984 | rx_bi->page_dma = 0; |
| 985 | } |
| 986 | I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd); |
| 987 | |
| 988 | if (unlikely( |
| 989 | !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) { |
| 990 | struct i40e_rx_buffer *next_buffer; |
| 991 | |
| 992 | next_buffer = &rx_ring->rx_bi[i]; |
| 993 | |
| 994 | if (ring_is_ps_enabled(rx_ring)) { |
| 995 | rx_bi->skb = next_buffer->skb; |
| 996 | rx_bi->dma = next_buffer->dma; |
| 997 | next_buffer->skb = skb; |
| 998 | next_buffer->dma = 0; |
| 999 | } |
| 1000 | rx_ring->rx_stats.non_eop_descs++; |
| 1001 | goto next_desc; |
| 1002 | } |
| 1003 | |
| 1004 | /* ERR_MASK will only have valid bits if EOP set */ |
| 1005 | if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) { |
| 1006 | dev_kfree_skb_any(skb); |
| 1007 | goto next_desc; |
| 1008 | } |
| 1009 | |
| 1010 | skb->rxhash = i40e_rx_hash(rx_ring, rx_desc); |
| 1011 | i40e_rx_checksum(vsi, skb, rx_status, rx_error); |
| 1012 | |
| 1013 | /* probably a little skewed due to removing CRC */ |
| 1014 | total_rx_bytes += skb->len; |
| 1015 | total_rx_packets++; |
| 1016 | |
| 1017 | skb->protocol = eth_type_trans(skb, rx_ring->netdev); |
| 1018 | vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT) |
| 1019 | ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) |
| 1020 | : 0; |
| 1021 | i40e_receive_skb(rx_ring, skb, vlan_tag); |
| 1022 | |
| 1023 | rx_ring->netdev->last_rx = jiffies; |
| 1024 | budget--; |
| 1025 | next_desc: |
| 1026 | rx_desc->wb.qword1.status_error_len = 0; |
| 1027 | if (!budget) |
| 1028 | break; |
| 1029 | |
| 1030 | cleaned_count++; |
| 1031 | /* return some buffers to hardware, one at a time is too slow */ |
| 1032 | if (cleaned_count >= I40E_RX_BUFFER_WRITE) { |
| 1033 | i40e_alloc_rx_buffers(rx_ring, cleaned_count); |
| 1034 | cleaned_count = 0; |
| 1035 | } |
| 1036 | |
| 1037 | /* use prefetched values */ |
| 1038 | rx_desc = next_rxd; |
| 1039 | qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); |
| 1040 | rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) |
| 1041 | >> I40E_RXD_QW1_STATUS_SHIFT; |
| 1042 | } |
| 1043 | |
| 1044 | rx_ring->next_to_clean = i; |
| 1045 | rx_ring->rx_stats.packets += total_rx_packets; |
| 1046 | rx_ring->rx_stats.bytes += total_rx_bytes; |
| 1047 | rx_ring->q_vector->rx.total_packets += total_rx_packets; |
| 1048 | rx_ring->q_vector->rx.total_bytes += total_rx_bytes; |
| 1049 | |
| 1050 | if (cleaned_count) |
| 1051 | i40e_alloc_rx_buffers(rx_ring, cleaned_count); |
| 1052 | |
| 1053 | return budget > 0; |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine |
| 1058 | * @napi: napi struct with our devices info in it |
| 1059 | * @budget: amount of work driver is allowed to do this pass, in packets |
| 1060 | * |
| 1061 | * This function will clean all queues associated with a q_vector. |
| 1062 | * |
| 1063 | * Returns the amount of work done |
| 1064 | **/ |
| 1065 | int i40e_napi_poll(struct napi_struct *napi, int budget) |
| 1066 | { |
| 1067 | struct i40e_q_vector *q_vector = |
| 1068 | container_of(napi, struct i40e_q_vector, napi); |
| 1069 | struct i40e_vsi *vsi = q_vector->vsi; |
| 1070 | bool clean_complete = true; |
| 1071 | int budget_per_ring; |
| 1072 | int i; |
| 1073 | |
| 1074 | if (test_bit(__I40E_DOWN, &vsi->state)) { |
| 1075 | napi_complete(napi); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | /* We attempt to distribute budget to each Rx queue fairly, but don't |
| 1080 | * allow the budget to go below 1 because that would exit polling early. |
| 1081 | * Since the actual Tx work is minimal, we can give the Tx a larger |
| 1082 | * budget and be more aggressive about cleaning up the Tx descriptors. |
| 1083 | */ |
| 1084 | budget_per_ring = max(budget/q_vector->num_ringpairs, 1); |
| 1085 | for (i = 0; i < q_vector->num_ringpairs; i++) { |
| 1086 | clean_complete &= i40e_clean_tx_irq(q_vector->tx.ring[i], |
| 1087 | vsi->work_limit); |
| 1088 | clean_complete &= i40e_clean_rx_irq(q_vector->rx.ring[i], |
| 1089 | budget_per_ring); |
| 1090 | } |
| 1091 | |
| 1092 | /* If work not completed, return budget and polling will return */ |
| 1093 | if (!clean_complete) |
| 1094 | return budget; |
| 1095 | |
| 1096 | /* Work is done so exit the polling mode and re-enable the interrupt */ |
| 1097 | napi_complete(napi); |
| 1098 | if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) || |
| 1099 | ITR_IS_DYNAMIC(vsi->tx_itr_setting)) |
| 1100 | i40e_update_dynamic_itr(q_vector); |
| 1101 | |
| 1102 | if (!test_bit(__I40E_DOWN, &vsi->state)) { |
| 1103 | if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) { |
| 1104 | i40e_irq_dynamic_enable(vsi, |
| 1105 | q_vector->v_idx + vsi->base_vector); |
| 1106 | } else { |
| 1107 | struct i40e_hw *hw = &vsi->back->hw; |
| 1108 | /* We re-enable the queue 0 cause, but |
| 1109 | * don't worry about dynamic_enable |
| 1110 | * because we left it on for the other |
| 1111 | * possible interrupts during napi |
| 1112 | */ |
| 1113 | u32 qval = rd32(hw, I40E_QINT_RQCTL(0)); |
| 1114 | qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK; |
| 1115 | wr32(hw, I40E_QINT_RQCTL(0), qval); |
| 1116 | |
| 1117 | qval = rd32(hw, I40E_QINT_TQCTL(0)); |
| 1118 | qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK; |
| 1119 | wr32(hw, I40E_QINT_TQCTL(0), qval); |
| 1120 | i40e_flush(hw); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * i40e_atr - Add a Flow Director ATR filter |
| 1129 | * @tx_ring: ring to add programming descriptor to |
| 1130 | * @skb: send buffer |
| 1131 | * @flags: send flags |
| 1132 | * @protocol: wire protocol |
| 1133 | **/ |
| 1134 | static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb, |
| 1135 | u32 flags, __be16 protocol) |
| 1136 | { |
| 1137 | struct i40e_filter_program_desc *fdir_desc; |
| 1138 | struct i40e_pf *pf = tx_ring->vsi->back; |
| 1139 | union { |
| 1140 | unsigned char *network; |
| 1141 | struct iphdr *ipv4; |
| 1142 | struct ipv6hdr *ipv6; |
| 1143 | } hdr; |
| 1144 | struct tcphdr *th; |
| 1145 | unsigned int hlen; |
| 1146 | u32 flex_ptype, dtype_cmd; |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 1147 | u16 i; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1148 | |
| 1149 | /* make sure ATR is enabled */ |
| 1150 | if (!(pf->flags & I40E_FLAG_FDIR_ATR_ENABLED)) |
| 1151 | return; |
| 1152 | |
| 1153 | /* if sampling is disabled do nothing */ |
| 1154 | if (!tx_ring->atr_sample_rate) |
| 1155 | return; |
| 1156 | |
| 1157 | tx_ring->atr_count++; |
| 1158 | |
| 1159 | /* snag network header to get L4 type and address */ |
| 1160 | hdr.network = skb_network_header(skb); |
| 1161 | |
| 1162 | /* Currently only IPv4/IPv6 with TCP is supported */ |
| 1163 | if (protocol == htons(ETH_P_IP)) { |
| 1164 | if (hdr.ipv4->protocol != IPPROTO_TCP) |
| 1165 | return; |
| 1166 | |
| 1167 | /* access ihl as a u8 to avoid unaligned access on ia64 */ |
| 1168 | hlen = (hdr.network[0] & 0x0F) << 2; |
| 1169 | } else if (protocol == htons(ETH_P_IPV6)) { |
| 1170 | if (hdr.ipv6->nexthdr != IPPROTO_TCP) |
| 1171 | return; |
| 1172 | |
| 1173 | hlen = sizeof(struct ipv6hdr); |
| 1174 | } else { |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | th = (struct tcphdr *)(hdr.network + hlen); |
| 1179 | |
| 1180 | /* sample on all syn/fin packets or once every atr sample rate */ |
| 1181 | if (!th->fin && !th->syn && (tx_ring->atr_count < tx_ring->atr_sample_rate)) |
| 1182 | return; |
| 1183 | |
| 1184 | tx_ring->atr_count = 0; |
| 1185 | |
| 1186 | /* grab the next descriptor */ |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 1187 | i = tx_ring->next_to_use; |
| 1188 | fdir_desc = I40E_TX_FDIRDESC(tx_ring, i); |
| 1189 | |
| 1190 | i++; |
| 1191 | tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1192 | |
| 1193 | flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) & |
| 1194 | I40E_TXD_FLTR_QW0_QINDEX_MASK; |
| 1195 | flex_ptype |= (protocol == htons(ETH_P_IP)) ? |
| 1196 | (I40E_FILTER_PCTYPE_NONF_IPV4_TCP << |
| 1197 | I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) : |
| 1198 | (I40E_FILTER_PCTYPE_NONF_IPV6_TCP << |
| 1199 | I40E_TXD_FLTR_QW0_PCTYPE_SHIFT); |
| 1200 | |
| 1201 | flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT; |
| 1202 | |
| 1203 | dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG; |
| 1204 | |
| 1205 | dtype_cmd |= th->fin ? |
| 1206 | (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE << |
| 1207 | I40E_TXD_FLTR_QW1_PCMD_SHIFT) : |
| 1208 | (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE << |
| 1209 | I40E_TXD_FLTR_QW1_PCMD_SHIFT); |
| 1210 | |
| 1211 | dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX << |
| 1212 | I40E_TXD_FLTR_QW1_DEST_SHIFT; |
| 1213 | |
| 1214 | dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID << |
| 1215 | I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT; |
| 1216 | |
| 1217 | fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype); |
| 1218 | fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd); |
| 1219 | } |
| 1220 | |
| 1221 | #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS) |
| 1222 | /** |
| 1223 | * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW |
| 1224 | * @skb: send buffer |
| 1225 | * @tx_ring: ring to send buffer on |
| 1226 | * @flags: the tx flags to be set |
| 1227 | * |
| 1228 | * Checks the skb and set up correspondingly several generic transmit flags |
| 1229 | * related to VLAN tagging for the HW, such as VLAN, DCB, etc. |
| 1230 | * |
| 1231 | * Returns error code indicate the frame should be dropped upon error and the |
| 1232 | * otherwise returns 0 to indicate the flags has been set properly. |
| 1233 | **/ |
| 1234 | static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb, |
| 1235 | struct i40e_ring *tx_ring, |
| 1236 | u32 *flags) |
| 1237 | { |
| 1238 | __be16 protocol = skb->protocol; |
| 1239 | u32 tx_flags = 0; |
| 1240 | |
| 1241 | /* if we have a HW VLAN tag being added, default to the HW one */ |
| 1242 | if (vlan_tx_tag_present(skb)) { |
| 1243 | tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT; |
| 1244 | tx_flags |= I40E_TX_FLAGS_HW_VLAN; |
| 1245 | /* else if it is a SW VLAN, check the next protocol and store the tag */ |
| 1246 | } else if (protocol == __constant_htons(ETH_P_8021Q)) { |
| 1247 | struct vlan_hdr *vhdr, _vhdr; |
| 1248 | vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr); |
| 1249 | if (!vhdr) |
| 1250 | return -EINVAL; |
| 1251 | |
| 1252 | protocol = vhdr->h_vlan_encapsulated_proto; |
| 1253 | tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT; |
| 1254 | tx_flags |= I40E_TX_FLAGS_SW_VLAN; |
| 1255 | } |
| 1256 | |
| 1257 | /* Insert 802.1p priority into VLAN header */ |
| 1258 | if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) && |
| 1259 | ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) || |
| 1260 | (skb->priority != TC_PRIO_CONTROL))) { |
| 1261 | tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK; |
| 1262 | tx_flags |= (skb->priority & 0x7) << |
| 1263 | I40E_TX_FLAGS_VLAN_PRIO_SHIFT; |
| 1264 | if (tx_flags & I40E_TX_FLAGS_SW_VLAN) { |
| 1265 | struct vlan_ethhdr *vhdr; |
| 1266 | if (skb_header_cloned(skb) && |
| 1267 | pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
| 1268 | return -ENOMEM; |
| 1269 | vhdr = (struct vlan_ethhdr *)skb->data; |
| 1270 | vhdr->h_vlan_TCI = htons(tx_flags >> |
| 1271 | I40E_TX_FLAGS_VLAN_SHIFT); |
| 1272 | } else { |
| 1273 | tx_flags |= I40E_TX_FLAGS_HW_VLAN; |
| 1274 | } |
| 1275 | } |
| 1276 | *flags = tx_flags; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | /** |
| 1281 | * i40e_tx_csum - is checksum offload requested |
| 1282 | * @tx_ring: ptr to the ring to send |
| 1283 | * @skb: ptr to the skb we're sending |
| 1284 | * @tx_flags: the collected send information |
| 1285 | * @protocol: the send protocol |
| 1286 | * |
| 1287 | * Returns true if checksum offload is requested |
| 1288 | **/ |
| 1289 | static bool i40e_tx_csum(struct i40e_ring *tx_ring, struct sk_buff *skb, |
| 1290 | u32 tx_flags, __be16 protocol) |
| 1291 | { |
| 1292 | if ((skb->ip_summed != CHECKSUM_PARTIAL) && |
| 1293 | !(tx_flags & I40E_TX_FLAGS_TXSW)) { |
| 1294 | if (!(tx_flags & I40E_TX_FLAGS_HW_VLAN)) |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | return skb->ip_summed == CHECKSUM_PARTIAL; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * i40e_tso - set up the tso context descriptor |
| 1303 | * @tx_ring: ptr to the ring to send |
| 1304 | * @skb: ptr to the skb we're sending |
| 1305 | * @tx_flags: the collected send information |
| 1306 | * @protocol: the send protocol |
| 1307 | * @hdr_len: ptr to the size of the packet header |
| 1308 | * @cd_tunneling: ptr to context descriptor bits |
| 1309 | * |
| 1310 | * Returns 0 if no TSO can happen, 1 if tso is going, or error |
| 1311 | **/ |
| 1312 | static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb, |
| 1313 | u32 tx_flags, __be16 protocol, u8 *hdr_len, |
| 1314 | u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling) |
| 1315 | { |
| 1316 | u32 cd_cmd, cd_tso_len, cd_mss; |
| 1317 | struct tcphdr *tcph; |
| 1318 | struct iphdr *iph; |
| 1319 | u32 l4len; |
| 1320 | int err; |
| 1321 | struct ipv6hdr *ipv6h; |
| 1322 | |
| 1323 | if (!skb_is_gso(skb)) |
| 1324 | return 0; |
| 1325 | |
| 1326 | if (skb_header_cloned(skb)) { |
| 1327 | err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 1328 | if (err) |
| 1329 | return err; |
| 1330 | } |
| 1331 | |
| 1332 | if (protocol == __constant_htons(ETH_P_IP)) { |
| 1333 | iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb); |
| 1334 | tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb); |
| 1335 | iph->tot_len = 0; |
| 1336 | iph->check = 0; |
| 1337 | tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 1338 | 0, IPPROTO_TCP, 0); |
| 1339 | } else if (skb_is_gso_v6(skb)) { |
| 1340 | |
| 1341 | ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) |
| 1342 | : ipv6_hdr(skb); |
| 1343 | tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb); |
| 1344 | ipv6h->payload_len = 0; |
| 1345 | tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, |
| 1346 | 0, IPPROTO_TCP, 0); |
| 1347 | } |
| 1348 | |
| 1349 | l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb); |
| 1350 | *hdr_len = (skb->encapsulation |
| 1351 | ? (skb_inner_transport_header(skb) - skb->data) |
| 1352 | : skb_transport_offset(skb)) + l4len; |
| 1353 | |
| 1354 | /* find the field values */ |
| 1355 | cd_cmd = I40E_TX_CTX_DESC_TSO; |
| 1356 | cd_tso_len = skb->len - *hdr_len; |
| 1357 | cd_mss = skb_shinfo(skb)->gso_size; |
| 1358 | *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
| 1359 | | ((u64)cd_tso_len |
| 1360 | << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
| 1361 | | ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT); |
| 1362 | return 1; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * i40e_tx_enable_csum - Enable Tx checksum offloads |
| 1367 | * @skb: send buffer |
| 1368 | * @tx_flags: Tx flags currently set |
| 1369 | * @td_cmd: Tx descriptor command bits to set |
| 1370 | * @td_offset: Tx descriptor header offsets to set |
| 1371 | * @cd_tunneling: ptr to context desc bits |
| 1372 | **/ |
| 1373 | static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags, |
| 1374 | u32 *td_cmd, u32 *td_offset, |
| 1375 | struct i40e_ring *tx_ring, |
| 1376 | u32 *cd_tunneling) |
| 1377 | { |
| 1378 | struct ipv6hdr *this_ipv6_hdr; |
| 1379 | unsigned int this_tcp_hdrlen; |
| 1380 | struct iphdr *this_ip_hdr; |
| 1381 | u32 network_hdr_len; |
| 1382 | u8 l4_hdr = 0; |
| 1383 | |
| 1384 | if (skb->encapsulation) { |
| 1385 | network_hdr_len = skb_inner_network_header_len(skb); |
| 1386 | this_ip_hdr = inner_ip_hdr(skb); |
| 1387 | this_ipv6_hdr = inner_ipv6_hdr(skb); |
| 1388 | this_tcp_hdrlen = inner_tcp_hdrlen(skb); |
| 1389 | |
| 1390 | if (tx_flags & I40E_TX_FLAGS_IPV4) { |
| 1391 | |
| 1392 | if (tx_flags & I40E_TX_FLAGS_TSO) { |
| 1393 | *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4; |
| 1394 | ip_hdr(skb)->check = 0; |
| 1395 | } else { |
| 1396 | *cd_tunneling |= |
| 1397 | I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM; |
| 1398 | } |
| 1399 | } else if (tx_flags & I40E_TX_FLAGS_IPV6) { |
| 1400 | if (tx_flags & I40E_TX_FLAGS_TSO) { |
| 1401 | *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6; |
| 1402 | ip_hdr(skb)->check = 0; |
| 1403 | } else { |
| 1404 | *cd_tunneling |= |
| 1405 | I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | /* Now set the ctx descriptor fields */ |
| 1410 | *cd_tunneling |= (skb_network_header_len(skb) >> 2) << |
| 1411 | I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT | |
| 1412 | I40E_TXD_CTX_UDP_TUNNELING | |
| 1413 | ((skb_inner_network_offset(skb) - |
| 1414 | skb_transport_offset(skb)) >> 1) << |
| 1415 | I40E_TXD_CTX_QW0_NATLEN_SHIFT; |
| 1416 | |
| 1417 | } else { |
| 1418 | network_hdr_len = skb_network_header_len(skb); |
| 1419 | this_ip_hdr = ip_hdr(skb); |
| 1420 | this_ipv6_hdr = ipv6_hdr(skb); |
| 1421 | this_tcp_hdrlen = tcp_hdrlen(skb); |
| 1422 | } |
| 1423 | |
| 1424 | /* Enable IP checksum offloads */ |
| 1425 | if (tx_flags & I40E_TX_FLAGS_IPV4) { |
| 1426 | l4_hdr = this_ip_hdr->protocol; |
| 1427 | /* the stack computes the IP header already, the only time we |
| 1428 | * need the hardware to recompute it is in the case of TSO. |
| 1429 | */ |
| 1430 | if (tx_flags & I40E_TX_FLAGS_TSO) { |
| 1431 | *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM; |
| 1432 | this_ip_hdr->check = 0; |
| 1433 | } else { |
| 1434 | *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4; |
| 1435 | } |
| 1436 | /* Now set the td_offset for IP header length */ |
| 1437 | *td_offset = (network_hdr_len >> 2) << |
| 1438 | I40E_TX_DESC_LENGTH_IPLEN_SHIFT; |
| 1439 | } else if (tx_flags & I40E_TX_FLAGS_IPV6) { |
| 1440 | l4_hdr = this_ipv6_hdr->nexthdr; |
| 1441 | *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6; |
| 1442 | /* Now set the td_offset for IP header length */ |
| 1443 | *td_offset = (network_hdr_len >> 2) << |
| 1444 | I40E_TX_DESC_LENGTH_IPLEN_SHIFT; |
| 1445 | } |
| 1446 | /* words in MACLEN + dwords in IPLEN + dwords in L4Len */ |
| 1447 | *td_offset |= (skb_network_offset(skb) >> 1) << |
| 1448 | I40E_TX_DESC_LENGTH_MACLEN_SHIFT; |
| 1449 | |
| 1450 | /* Enable L4 checksum offloads */ |
| 1451 | switch (l4_hdr) { |
| 1452 | case IPPROTO_TCP: |
| 1453 | /* enable checksum offloads */ |
| 1454 | *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP; |
| 1455 | *td_offset |= (this_tcp_hdrlen >> 2) << |
| 1456 | I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; |
| 1457 | break; |
| 1458 | case IPPROTO_SCTP: |
| 1459 | /* enable SCTP checksum offload */ |
| 1460 | *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP; |
| 1461 | *td_offset |= (sizeof(struct sctphdr) >> 2) << |
| 1462 | I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; |
| 1463 | break; |
| 1464 | case IPPROTO_UDP: |
| 1465 | /* enable UDP checksum offload */ |
| 1466 | *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP; |
| 1467 | *td_offset |= (sizeof(struct udphdr) >> 2) << |
| 1468 | I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; |
| 1469 | break; |
| 1470 | default: |
| 1471 | break; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * i40e_create_tx_ctx Build the Tx context descriptor |
| 1477 | * @tx_ring: ring to create the descriptor on |
| 1478 | * @cd_type_cmd_tso_mss: Quad Word 1 |
| 1479 | * @cd_tunneling: Quad Word 0 - bits 0-31 |
| 1480 | * @cd_l2tag2: Quad Word 0 - bits 32-63 |
| 1481 | **/ |
| 1482 | static void i40e_create_tx_ctx(struct i40e_ring *tx_ring, |
| 1483 | const u64 cd_type_cmd_tso_mss, |
| 1484 | const u32 cd_tunneling, const u32 cd_l2tag2) |
| 1485 | { |
| 1486 | struct i40e_tx_context_desc *context_desc; |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 1487 | int i = tx_ring->next_to_use; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1488 | |
| 1489 | if (!cd_type_cmd_tso_mss && !cd_tunneling && !cd_l2tag2) |
| 1490 | return; |
| 1491 | |
| 1492 | /* grab the next descriptor */ |
Alexander Duyck | fc4ac67 | 2013-09-28 06:00:22 +0000 | [diff] [blame^] | 1493 | context_desc = I40E_TX_CTXTDESC(tx_ring, i); |
| 1494 | |
| 1495 | i++; |
| 1496 | tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1497 | |
| 1498 | /* cpu_to_le32 and assign to struct fields */ |
| 1499 | context_desc->tunneling_params = cpu_to_le32(cd_tunneling); |
| 1500 | context_desc->l2tag2 = cpu_to_le16(cd_l2tag2); |
| 1501 | context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss); |
| 1502 | } |
| 1503 | |
| 1504 | /** |
| 1505 | * i40e_tx_map - Build the Tx descriptor |
| 1506 | * @tx_ring: ring to send buffer on |
| 1507 | * @skb: send buffer |
| 1508 | * @first: first buffer info buffer to use |
| 1509 | * @tx_flags: collected send information |
| 1510 | * @hdr_len: size of the packet header |
| 1511 | * @td_cmd: the command field in the descriptor |
| 1512 | * @td_offset: offset for checksum or crc |
| 1513 | **/ |
| 1514 | static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb, |
| 1515 | struct i40e_tx_buffer *first, u32 tx_flags, |
| 1516 | const u8 hdr_len, u32 td_cmd, u32 td_offset) |
| 1517 | { |
| 1518 | struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0]; |
| 1519 | unsigned int data_len = skb->data_len; |
| 1520 | unsigned int size = skb_headlen(skb); |
| 1521 | struct device *dev = tx_ring->dev; |
| 1522 | u32 paylen = skb->len - hdr_len; |
| 1523 | u16 i = tx_ring->next_to_use; |
| 1524 | struct i40e_tx_buffer *tx_bi; |
| 1525 | struct i40e_tx_desc *tx_desc; |
| 1526 | u32 buf_offset = 0; |
| 1527 | u32 td_tag = 0; |
| 1528 | dma_addr_t dma; |
| 1529 | u16 gso_segs; |
| 1530 | |
| 1531 | dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE); |
| 1532 | if (dma_mapping_error(dev, dma)) |
| 1533 | goto dma_error; |
| 1534 | |
| 1535 | if (tx_flags & I40E_TX_FLAGS_HW_VLAN) { |
| 1536 | td_cmd |= I40E_TX_DESC_CMD_IL2TAG1; |
| 1537 | td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >> |
| 1538 | I40E_TX_FLAGS_VLAN_SHIFT; |
| 1539 | } |
| 1540 | |
| 1541 | tx_desc = I40E_TX_DESC(tx_ring, i); |
| 1542 | for (;;) { |
| 1543 | while (size > I40E_MAX_DATA_PER_TXD) { |
| 1544 | tx_desc->buffer_addr = cpu_to_le64(dma + buf_offset); |
| 1545 | tx_desc->cmd_type_offset_bsz = |
| 1546 | build_ctob(td_cmd, td_offset, |
| 1547 | I40E_MAX_DATA_PER_TXD, td_tag); |
| 1548 | |
| 1549 | buf_offset += I40E_MAX_DATA_PER_TXD; |
| 1550 | size -= I40E_MAX_DATA_PER_TXD; |
| 1551 | |
| 1552 | tx_desc++; |
| 1553 | i++; |
| 1554 | if (i == tx_ring->count) { |
| 1555 | tx_desc = I40E_TX_DESC(tx_ring, 0); |
| 1556 | i = 0; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | tx_bi = &tx_ring->tx_bi[i]; |
Alexander Duyck | 35a1e2a | 2013-09-28 06:00:17 +0000 | [diff] [blame] | 1561 | dma_unmap_len_set(tx_bi, len, buf_offset + size); |
| 1562 | dma_unmap_addr_set(tx_bi, dma, dma); |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1563 | tx_bi->tx_flags = tx_flags; |
Jesse Brandeburg | fd0a05c | 2013-09-11 08:39:51 +0000 | [diff] [blame] | 1564 | |
| 1565 | tx_desc->buffer_addr = cpu_to_le64(dma + buf_offset); |
| 1566 | tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset, |
| 1567 | size, td_tag); |
| 1568 | |
| 1569 | if (likely(!data_len)) |
| 1570 | break; |
| 1571 | |
| 1572 | size = skb_frag_size(frag); |
| 1573 | data_len -= size; |
| 1574 | buf_offset = 0; |
| 1575 | tx_flags |= I40E_TX_FLAGS_MAPPED_AS_PAGE; |
| 1576 | |
| 1577 | dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE); |
| 1578 | if (dma_mapping_error(dev, dma)) |
| 1579 | goto dma_error; |
| 1580 | |
| 1581 | tx_desc++; |
| 1582 | i++; |
| 1583 | if (i == tx_ring->count) { |
| 1584 | tx_desc = I40E_TX_DESC(tx_ring, 0); |
| 1585 | i = 0; |
| 1586 | } |
| 1587 | |
| 1588 | frag++; |
| 1589 | } |
| 1590 | |
| 1591 | tx_desc->cmd_type_offset_bsz |= |
| 1592 | cpu_to_le64((u64)I40E_TXD_CMD << I40E_TXD_QW1_CMD_SHIFT); |
| 1593 | |
| 1594 | i++; |
| 1595 | if (i == tx_ring->count) |
| 1596 | i = 0; |
| 1597 | |
| 1598 | tx_ring->next_to_use = i; |
| 1599 | |
| 1600 | if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) |
| 1601 | gso_segs = skb_shinfo(skb)->gso_segs; |
| 1602 | else |
| 1603 | gso_segs = 1; |
| 1604 | |
| 1605 | /* multiply data chunks by size of headers */ |
| 1606 | tx_bi->bytecount = paylen + (gso_segs * hdr_len); |
| 1607 | tx_bi->gso_segs = gso_segs; |
| 1608 | tx_bi->skb = skb; |
| 1609 | |
| 1610 | /* set the timestamp and next to watch values */ |
| 1611 | first->time_stamp = jiffies; |
| 1612 | first->next_to_watch = tx_desc; |
| 1613 | |
| 1614 | /* Force memory writes to complete before letting h/w |
| 1615 | * know there are new descriptors to fetch. (Only |
| 1616 | * applicable for weak-ordered memory model archs, |
| 1617 | * such as IA-64). |
| 1618 | */ |
| 1619 | wmb(); |
| 1620 | |
| 1621 | writel(i, tx_ring->tail); |
| 1622 | return; |
| 1623 | |
| 1624 | dma_error: |
| 1625 | dev_info(dev, "TX DMA map failed\n"); |
| 1626 | |
| 1627 | /* clear dma mappings for failed tx_bi map */ |
| 1628 | for (;;) { |
| 1629 | tx_bi = &tx_ring->tx_bi[i]; |
| 1630 | i40e_unmap_tx_resource(tx_ring, tx_bi); |
| 1631 | if (tx_bi == first) |
| 1632 | break; |
| 1633 | if (i == 0) |
| 1634 | i = tx_ring->count; |
| 1635 | i--; |
| 1636 | } |
| 1637 | |
| 1638 | dev_kfree_skb_any(skb); |
| 1639 | |
| 1640 | tx_ring->next_to_use = i; |
| 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions |
| 1645 | * @tx_ring: the ring to be checked |
| 1646 | * @size: the size buffer we want to assure is available |
| 1647 | * |
| 1648 | * Returns -EBUSY if a stop is needed, else 0 |
| 1649 | **/ |
| 1650 | static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size) |
| 1651 | { |
| 1652 | netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index); |
| 1653 | smp_mb(); |
| 1654 | |
| 1655 | /* Check again in a case another CPU has just made room available. */ |
| 1656 | if (likely(I40E_DESC_UNUSED(tx_ring) < size)) |
| 1657 | return -EBUSY; |
| 1658 | |
| 1659 | /* A reprieve! - use start_queue because it doesn't call schedule */ |
| 1660 | netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index); |
| 1661 | ++tx_ring->tx_stats.restart_queue; |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * i40e_maybe_stop_tx - 1st level check for tx stop conditions |
| 1667 | * @tx_ring: the ring to be checked |
| 1668 | * @size: the size buffer we want to assure is available |
| 1669 | * |
| 1670 | * Returns 0 if stop is not needed |
| 1671 | **/ |
| 1672 | static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size) |
| 1673 | { |
| 1674 | if (likely(I40E_DESC_UNUSED(tx_ring) >= size)) |
| 1675 | return 0; |
| 1676 | return __i40e_maybe_stop_tx(tx_ring, size); |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * i40e_xmit_descriptor_count - calculate number of tx descriptors needed |
| 1681 | * @skb: send buffer |
| 1682 | * @tx_ring: ring to send buffer on |
| 1683 | * |
| 1684 | * Returns number of data descriptors needed for this skb. Returns 0 to indicate |
| 1685 | * there is not enough descriptors available in this ring since we need at least |
| 1686 | * one descriptor. |
| 1687 | **/ |
| 1688 | static int i40e_xmit_descriptor_count(struct sk_buff *skb, |
| 1689 | struct i40e_ring *tx_ring) |
| 1690 | { |
| 1691 | #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD |
| 1692 | unsigned int f; |
| 1693 | #endif |
| 1694 | int count = 0; |
| 1695 | |
| 1696 | /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD, |
| 1697 | * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD, |
| 1698 | * + 2 desc gap to keep tail from touching head, |
| 1699 | * + 1 desc for context descriptor, |
| 1700 | * otherwise try next time |
| 1701 | */ |
| 1702 | #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD |
| 1703 | for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) |
| 1704 | count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size); |
| 1705 | #else |
| 1706 | count += skb_shinfo(skb)->nr_frags; |
| 1707 | #endif |
| 1708 | count += TXD_USE_COUNT(skb_headlen(skb)); |
| 1709 | if (i40e_maybe_stop_tx(tx_ring, count + 3)) { |
| 1710 | tx_ring->tx_stats.tx_busy++; |
| 1711 | return 0; |
| 1712 | } |
| 1713 | return count; |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * i40e_xmit_frame_ring - Sends buffer on Tx ring |
| 1718 | * @skb: send buffer |
| 1719 | * @tx_ring: ring to send buffer on |
| 1720 | * |
| 1721 | * Returns NETDEV_TX_OK if sent, else an error code |
| 1722 | **/ |
| 1723 | static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb, |
| 1724 | struct i40e_ring *tx_ring) |
| 1725 | { |
| 1726 | u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT; |
| 1727 | u32 cd_tunneling = 0, cd_l2tag2 = 0; |
| 1728 | struct i40e_tx_buffer *first; |
| 1729 | u32 td_offset = 0; |
| 1730 | u32 tx_flags = 0; |
| 1731 | __be16 protocol; |
| 1732 | u32 td_cmd = 0; |
| 1733 | u8 hdr_len = 0; |
| 1734 | int tso; |
| 1735 | if (0 == i40e_xmit_descriptor_count(skb, tx_ring)) |
| 1736 | return NETDEV_TX_BUSY; |
| 1737 | |
| 1738 | /* prepare the xmit flags */ |
| 1739 | if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags)) |
| 1740 | goto out_drop; |
| 1741 | |
| 1742 | /* obtain protocol of skb */ |
| 1743 | protocol = skb->protocol; |
| 1744 | |
| 1745 | /* record the location of the first descriptor for this packet */ |
| 1746 | first = &tx_ring->tx_bi[tx_ring->next_to_use]; |
| 1747 | |
| 1748 | /* setup IPv4/IPv6 offloads */ |
| 1749 | if (protocol == __constant_htons(ETH_P_IP)) |
| 1750 | tx_flags |= I40E_TX_FLAGS_IPV4; |
| 1751 | else if (protocol == __constant_htons(ETH_P_IPV6)) |
| 1752 | tx_flags |= I40E_TX_FLAGS_IPV6; |
| 1753 | |
| 1754 | tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len, |
| 1755 | &cd_type_cmd_tso_mss, &cd_tunneling); |
| 1756 | |
| 1757 | if (tso < 0) |
| 1758 | goto out_drop; |
| 1759 | else if (tso) |
| 1760 | tx_flags |= I40E_TX_FLAGS_TSO; |
| 1761 | |
| 1762 | skb_tx_timestamp(skb); |
| 1763 | |
| 1764 | /* Always offload the checksum, since it's in the data descriptor */ |
| 1765 | if (i40e_tx_csum(tx_ring, skb, tx_flags, protocol)) |
| 1766 | tx_flags |= I40E_TX_FLAGS_CSUM; |
| 1767 | |
| 1768 | /* always enable offload insertion */ |
| 1769 | td_cmd |= I40E_TX_DESC_CMD_ICRC; |
| 1770 | |
| 1771 | if (tx_flags & I40E_TX_FLAGS_CSUM) |
| 1772 | i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset, |
| 1773 | tx_ring, &cd_tunneling); |
| 1774 | |
| 1775 | i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss, |
| 1776 | cd_tunneling, cd_l2tag2); |
| 1777 | |
| 1778 | /* Add Flow Director ATR if it's enabled. |
| 1779 | * |
| 1780 | * NOTE: this must always be directly before the data descriptor. |
| 1781 | */ |
| 1782 | i40e_atr(tx_ring, skb, tx_flags, protocol); |
| 1783 | |
| 1784 | i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len, |
| 1785 | td_cmd, td_offset); |
| 1786 | |
| 1787 | i40e_maybe_stop_tx(tx_ring, DESC_NEEDED); |
| 1788 | |
| 1789 | return NETDEV_TX_OK; |
| 1790 | |
| 1791 | out_drop: |
| 1792 | dev_kfree_skb_any(skb); |
| 1793 | return NETDEV_TX_OK; |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer |
| 1798 | * @skb: send buffer |
| 1799 | * @netdev: network interface device structure |
| 1800 | * |
| 1801 | * Returns NETDEV_TX_OK if sent, else an error code |
| 1802 | **/ |
| 1803 | netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev) |
| 1804 | { |
| 1805 | struct i40e_netdev_priv *np = netdev_priv(netdev); |
| 1806 | struct i40e_vsi *vsi = np->vsi; |
| 1807 | struct i40e_ring *tx_ring = &vsi->tx_rings[skb->queue_mapping]; |
| 1808 | |
| 1809 | /* hardware can't handle really short frames, hardware padding works |
| 1810 | * beyond this point |
| 1811 | */ |
| 1812 | if (unlikely(skb->len < I40E_MIN_TX_LEN)) { |
| 1813 | if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len)) |
| 1814 | return NETDEV_TX_OK; |
| 1815 | skb->len = I40E_MIN_TX_LEN; |
| 1816 | skb_set_tail_pointer(skb, I40E_MIN_TX_LEN); |
| 1817 | } |
| 1818 | |
| 1819 | return i40e_xmit_frame_ring(skb, tx_ring); |
| 1820 | } |