1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2024 Intel Corporation. */
3
4 /******************************************************************************
5 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
6 ******************************************************************************/
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/types.h>
11 #include <linux/bitops.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/vmalloc.h>
16 #include <linux/string.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/tcp.h>
20 #include <linux/sctp.h>
21 #include <linux/ipv6.h>
22 #include <linux/slab.h>
23 #include <net/checksum.h>
24 #include <net/ip6_checksum.h>
25 #include <linux/ethtool.h>
26 #include <linux/if.h>
27 #include <linux/if_vlan.h>
28 #include <linux/prefetch.h>
29 #include <net/mpls.h>
30 #include <linux/bpf.h>
31 #include <linux/bpf_trace.h>
32 #include <linux/atomic.h>
33 #include <net/xfrm.h>
34
35 #include "ixgbevf.h"
36
37 const char ixgbevf_driver_name[] = "ixgbevf";
38 static const char ixgbevf_driver_string[] =
39 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
40
41 static char ixgbevf_copyright[] =
42 "Copyright (c) 2009 - 2024 Intel Corporation.";
43
44 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
45 [board_82599_vf] = &ixgbevf_82599_vf_info,
46 [board_82599_vf_hv] = &ixgbevf_82599_vf_hv_info,
47 [board_X540_vf] = &ixgbevf_X540_vf_info,
48 [board_X540_vf_hv] = &ixgbevf_X540_vf_hv_info,
49 [board_X550_vf] = &ixgbevf_X550_vf_info,
50 [board_X550_vf_hv] = &ixgbevf_X550_vf_hv_info,
51 [board_X550EM_x_vf] = &ixgbevf_X550EM_x_vf_info,
52 [board_X550EM_x_vf_hv] = &ixgbevf_X550EM_x_vf_hv_info,
53 [board_x550em_a_vf] = &ixgbevf_x550em_a_vf_info,
54 [board_e610_vf] = &ixgbevf_e610_vf_info,
55 [board_e610_vf_hv] = &ixgbevf_e610_vf_hv_info,
56 };
57
58 /* ixgbevf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
66 static const struct pci_device_id ixgbevf_pci_tbl[] = {
67 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
68 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF_HV), board_82599_vf_hv },
69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
70 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF_HV), board_X540_vf_hv },
71 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
72 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF_HV), board_X550_vf_hv },
73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
74 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF_HV), board_X550EM_x_vf_hv},
75 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_VF), board_x550em_a_vf },
76 {PCI_VDEVICE_SUB(INTEL, IXGBE_DEV_ID_E610_VF, PCI_ANY_ID,
77 IXGBE_SUBDEV_ID_E610_VF_HV), board_e610_vf_hv},
78 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_E610_VF), board_e610_vf},
79 /* required last entry */
80 {0, }
81 };
82 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
83
84 MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
85 MODULE_LICENSE("GPL v2");
86
87 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
88 static int debug = -1;
89 module_param(debug, int, 0);
90 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
91
92 static struct workqueue_struct *ixgbevf_wq;
93
ixgbevf_service_event_schedule(struct ixgbevf_adapter * adapter)94 static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
95 {
96 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
97 !test_bit(__IXGBEVF_REMOVING, &adapter->state) &&
98 !test_and_set_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state))
99 queue_work(ixgbevf_wq, &adapter->service_task);
100 }
101
ixgbevf_service_event_complete(struct ixgbevf_adapter * adapter)102 static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
103 {
104 BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
105
106 /* flush memory to make sure state is correct before next watchdog */
107 smp_mb__before_atomic();
108 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
109 }
110
111 /* forward decls */
112 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
113 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
114 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
115 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer);
116 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
117 struct ixgbevf_rx_buffer *old_buff);
118
ixgbevf_remove_adapter(struct ixgbe_hw * hw)119 static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
120 {
121 struct ixgbevf_adapter *adapter = hw->back;
122
123 if (!hw->hw_addr)
124 return;
125 hw->hw_addr = NULL;
126 dev_err(&adapter->pdev->dev, "Adapter removed\n");
127 if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
128 ixgbevf_service_event_schedule(adapter);
129 }
130
ixgbevf_check_remove(struct ixgbe_hw * hw,u32 reg)131 static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
132 {
133 u32 value;
134
135 /* The following check not only optimizes a bit by not
136 * performing a read on the status register when the
137 * register just read was a status register read that
138 * returned IXGBE_FAILED_READ_REG. It also blocks any
139 * potential recursion.
140 */
141 if (reg == IXGBE_VFSTATUS) {
142 ixgbevf_remove_adapter(hw);
143 return;
144 }
145 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
146 if (value == IXGBE_FAILED_READ_REG)
147 ixgbevf_remove_adapter(hw);
148 }
149
ixgbevf_read_reg(struct ixgbe_hw * hw,u32 reg)150 u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
151 {
152 u8 __iomem *reg_addr = READ_ONCE(hw->hw_addr);
153 u32 value;
154
155 if (IXGBE_REMOVED(reg_addr))
156 return IXGBE_FAILED_READ_REG;
157 value = readl(reg_addr + reg);
158 if (unlikely(value == IXGBE_FAILED_READ_REG))
159 ixgbevf_check_remove(hw, reg);
160 return value;
161 }
162
163 /**
164 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
165 * @adapter: pointer to adapter struct
166 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
167 * @queue: queue to map the corresponding interrupt to
168 * @msix_vector: the vector to map to the corresponding queue
169 **/
ixgbevf_set_ivar(struct ixgbevf_adapter * adapter,s8 direction,u8 queue,u8 msix_vector)170 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
171 u8 queue, u8 msix_vector)
172 {
173 u32 ivar, index;
174 struct ixgbe_hw *hw = &adapter->hw;
175
176 if (direction == -1) {
177 /* other causes */
178 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
179 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
180 ivar &= ~0xFF;
181 ivar |= msix_vector;
182 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
183 } else {
184 /* Tx or Rx causes */
185 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
186 index = ((16 * (queue & 1)) + (8 * direction));
187 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
188 ivar &= ~(0xFF << index);
189 ivar |= (msix_vector << index);
190 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
191 }
192 }
193
ixgbevf_get_tx_completed(struct ixgbevf_ring * ring)194 static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
195 {
196 return ring->stats.packets;
197 }
198
ixgbevf_get_tx_pending(struct ixgbevf_ring * ring)199 static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
200 {
201 struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
202 struct ixgbe_hw *hw = &adapter->hw;
203
204 u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
205 u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
206
207 if (head != tail)
208 return (head < tail) ?
209 tail - head : (tail + ring->count - head);
210
211 return 0;
212 }
213
ixgbevf_check_tx_hang(struct ixgbevf_ring * tx_ring)214 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
215 {
216 u32 tx_done = ixgbevf_get_tx_completed(tx_ring);
217 u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
218 u32 tx_pending = ixgbevf_get_tx_pending(tx_ring);
219
220 clear_check_for_tx_hang(tx_ring);
221
222 /* Check for a hung queue, but be thorough. This verifies
223 * that a transmit has been completed since the previous
224 * check AND there is at least one packet pending. The
225 * ARMED bit is set to indicate a potential hang.
226 */
227 if ((tx_done_old == tx_done) && tx_pending) {
228 /* make sure it is true for two checks in a row */
229 return test_and_set_bit(__IXGBEVF_HANG_CHECK_ARMED,
230 &tx_ring->state);
231 }
232 /* reset the countdown */
233 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
234
235 /* update completed stats and continue */
236 tx_ring->tx_stats.tx_done_old = tx_done;
237
238 return false;
239 }
240
ixgbevf_tx_timeout_reset(struct ixgbevf_adapter * adapter)241 static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
242 {
243 /* Do the reset outside of interrupt context */
244 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
245 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
246 ixgbevf_service_event_schedule(adapter);
247 }
248 }
249
250 /**
251 * ixgbevf_tx_timeout - Respond to a Tx Hang
252 * @netdev: network interface device structure
253 * @txqueue: transmit queue hanging (unused)
254 **/
ixgbevf_tx_timeout(struct net_device * netdev,unsigned int __always_unused txqueue)255 static void ixgbevf_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue)
256 {
257 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
258
259 ixgbevf_tx_timeout_reset(adapter);
260 }
261
262 /**
263 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
264 * @q_vector: board private structure
265 * @tx_ring: tx ring to clean
266 * @napi_budget: Used to determine if we are in netpoll
267 **/
ixgbevf_clean_tx_irq(struct ixgbevf_q_vector * q_vector,struct ixgbevf_ring * tx_ring,int napi_budget)268 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
269 struct ixgbevf_ring *tx_ring, int napi_budget)
270 {
271 struct ixgbevf_adapter *adapter = q_vector->adapter;
272 struct ixgbevf_tx_buffer *tx_buffer;
273 union ixgbe_adv_tx_desc *tx_desc;
274 unsigned int total_bytes = 0, total_packets = 0, total_ipsec = 0;
275 unsigned int budget = tx_ring->count / 2;
276 unsigned int i = tx_ring->next_to_clean;
277
278 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
279 return true;
280
281 tx_buffer = &tx_ring->tx_buffer_info[i];
282 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
283 i -= tx_ring->count;
284
285 do {
286 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
287
288 /* if next_to_watch is not set then there is no work pending */
289 if (!eop_desc)
290 break;
291
292 /* prevent any other reads prior to eop_desc */
293 smp_rmb();
294
295 /* if DD is not set pending work has not been completed */
296 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
297 break;
298
299 /* clear next_to_watch to prevent false hangs */
300 tx_buffer->next_to_watch = NULL;
301
302 /* update the statistics for this packet */
303 total_bytes += tx_buffer->bytecount;
304 total_packets += tx_buffer->gso_segs;
305 if (tx_buffer->tx_flags & IXGBE_TX_FLAGS_IPSEC)
306 total_ipsec++;
307
308 /* free the skb */
309 if (ring_is_xdp(tx_ring))
310 page_frag_free(tx_buffer->data);
311 else
312 napi_consume_skb(tx_buffer->skb, napi_budget);
313
314 /* unmap skb header data */
315 dma_unmap_single(tx_ring->dev,
316 dma_unmap_addr(tx_buffer, dma),
317 dma_unmap_len(tx_buffer, len),
318 DMA_TO_DEVICE);
319
320 /* clear tx_buffer data */
321 dma_unmap_len_set(tx_buffer, len, 0);
322
323 /* unmap remaining buffers */
324 while (tx_desc != eop_desc) {
325 tx_buffer++;
326 tx_desc++;
327 i++;
328 if (unlikely(!i)) {
329 i -= tx_ring->count;
330 tx_buffer = tx_ring->tx_buffer_info;
331 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
332 }
333
334 /* unmap any remaining paged data */
335 if (dma_unmap_len(tx_buffer, len)) {
336 dma_unmap_page(tx_ring->dev,
337 dma_unmap_addr(tx_buffer, dma),
338 dma_unmap_len(tx_buffer, len),
339 DMA_TO_DEVICE);
340 dma_unmap_len_set(tx_buffer, len, 0);
341 }
342 }
343
344 /* move us one more past the eop_desc for start of next pkt */
345 tx_buffer++;
346 tx_desc++;
347 i++;
348 if (unlikely(!i)) {
349 i -= tx_ring->count;
350 tx_buffer = tx_ring->tx_buffer_info;
351 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
352 }
353
354 /* issue prefetch for next Tx descriptor */
355 prefetch(tx_desc);
356
357 /* update budget accounting */
358 budget--;
359 } while (likely(budget));
360
361 i += tx_ring->count;
362 tx_ring->next_to_clean = i;
363 u64_stats_update_begin(&tx_ring->syncp);
364 tx_ring->stats.bytes += total_bytes;
365 tx_ring->stats.packets += total_packets;
366 u64_stats_update_end(&tx_ring->syncp);
367 q_vector->tx.total_bytes += total_bytes;
368 q_vector->tx.total_packets += total_packets;
369 adapter->tx_ipsec += total_ipsec;
370
371 if (check_for_tx_hang(tx_ring) && ixgbevf_check_tx_hang(tx_ring)) {
372 struct ixgbe_hw *hw = &adapter->hw;
373 union ixgbe_adv_tx_desc *eop_desc;
374
375 eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
376
377 pr_err("Detected Tx Unit Hang%s\n"
378 " Tx Queue <%d>\n"
379 " TDH, TDT <%x>, <%x>\n"
380 " next_to_use <%x>\n"
381 " next_to_clean <%x>\n"
382 "tx_buffer_info[next_to_clean]\n"
383 " next_to_watch <%p>\n"
384 " eop_desc->wb.status <%x>\n"
385 " time_stamp <%lx>\n"
386 " jiffies <%lx>\n",
387 ring_is_xdp(tx_ring) ? " XDP" : "",
388 tx_ring->queue_index,
389 IXGBE_READ_REG(hw, IXGBE_VFTDH(tx_ring->reg_idx)),
390 IXGBE_READ_REG(hw, IXGBE_VFTDT(tx_ring->reg_idx)),
391 tx_ring->next_to_use, i,
392 eop_desc, (eop_desc ? eop_desc->wb.status : 0),
393 tx_ring->tx_buffer_info[i].time_stamp, jiffies);
394
395 if (!ring_is_xdp(tx_ring))
396 netif_stop_subqueue(tx_ring->netdev,
397 tx_ring->queue_index);
398
399 /* schedule immediate reset if we believe we hung */
400 ixgbevf_tx_timeout_reset(adapter);
401
402 return true;
403 }
404
405 if (ring_is_xdp(tx_ring))
406 return !!budget;
407
408 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
409 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
410 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
411 /* Make sure that anybody stopping the queue after this
412 * sees the new next_to_clean.
413 */
414 smp_mb();
415
416 if (__netif_subqueue_stopped(tx_ring->netdev,
417 tx_ring->queue_index) &&
418 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
419 netif_wake_subqueue(tx_ring->netdev,
420 tx_ring->queue_index);
421 ++tx_ring->tx_stats.restart_queue;
422 }
423 }
424
425 return !!budget;
426 }
427
428 /**
429 * ixgbevf_rx_skb - Helper function to determine proper Rx method
430 * @q_vector: structure containing interrupt and ring information
431 * @skb: packet to send up
432 **/
ixgbevf_rx_skb(struct ixgbevf_q_vector * q_vector,struct sk_buff * skb)433 static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
434 struct sk_buff *skb)
435 {
436 napi_gro_receive(&q_vector->napi, skb);
437 }
438
439 #define IXGBE_RSS_L4_TYPES_MASK \
440 ((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
441 (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
442 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
443 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
444
ixgbevf_rx_hash(struct ixgbevf_ring * ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)445 static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
446 union ixgbe_adv_rx_desc *rx_desc,
447 struct sk_buff *skb)
448 {
449 u16 rss_type;
450
451 if (!(ring->netdev->features & NETIF_F_RXHASH))
452 return;
453
454 rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
455 IXGBE_RXDADV_RSSTYPE_MASK;
456
457 if (!rss_type)
458 return;
459
460 skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
461 (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
462 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
463 }
464
465 /**
466 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
467 * @ring: structure containig ring specific data
468 * @rx_desc: current Rx descriptor being processed
469 * @skb: skb currently being received and modified
470 **/
ixgbevf_rx_checksum(struct ixgbevf_ring * ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)471 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
472 union ixgbe_adv_rx_desc *rx_desc,
473 struct sk_buff *skb)
474 {
475 skb_checksum_none_assert(skb);
476
477 /* Rx csum disabled */
478 if (!(ring->netdev->features & NETIF_F_RXCSUM))
479 return;
480
481 /* if IP and error */
482 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
483 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
484 ring->rx_stats.csum_err++;
485 return;
486 }
487
488 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
489 return;
490
491 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
492 ring->rx_stats.csum_err++;
493 return;
494 }
495
496 /* It must be a TCP or UDP packet with a valid checksum */
497 skb->ip_summed = CHECKSUM_UNNECESSARY;
498 }
499
500 /**
501 * ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
502 * @rx_ring: rx descriptor ring packet is being transacted on
503 * @rx_desc: pointer to the EOP Rx descriptor
504 * @skb: pointer to current skb being populated
505 *
506 * This function checks the ring, descriptor, and packet information in
507 * order to populate the checksum, VLAN, protocol, and other fields within
508 * the skb.
509 **/
ixgbevf_process_skb_fields(struct ixgbevf_ring * rx_ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)510 static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
511 union ixgbe_adv_rx_desc *rx_desc,
512 struct sk_buff *skb)
513 {
514 ixgbevf_rx_hash(rx_ring, rx_desc, skb);
515 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
516
517 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
518 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
519 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
520
521 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
522 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
523 }
524
525 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_STAT_SECP))
526 ixgbevf_ipsec_rx(rx_ring, rx_desc, skb);
527
528 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
529 }
530
531 static
ixgbevf_get_rx_buffer(struct ixgbevf_ring * rx_ring,const unsigned int size)532 struct ixgbevf_rx_buffer *ixgbevf_get_rx_buffer(struct ixgbevf_ring *rx_ring,
533 const unsigned int size)
534 {
535 struct ixgbevf_rx_buffer *rx_buffer;
536
537 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
538 prefetchw(rx_buffer->page);
539
540 /* we are reusing so sync this buffer for CPU use */
541 dma_sync_single_range_for_cpu(rx_ring->dev,
542 rx_buffer->dma,
543 rx_buffer->page_offset,
544 size,
545 DMA_FROM_DEVICE);
546
547 rx_buffer->pagecnt_bias--;
548
549 return rx_buffer;
550 }
551
ixgbevf_put_rx_buffer(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * rx_buffer,struct sk_buff * skb)552 static void ixgbevf_put_rx_buffer(struct ixgbevf_ring *rx_ring,
553 struct ixgbevf_rx_buffer *rx_buffer,
554 struct sk_buff *skb)
555 {
556 if (ixgbevf_can_reuse_rx_page(rx_buffer)) {
557 /* hand second half of page back to the ring */
558 ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
559 } else {
560 if (IS_ERR(skb))
561 /* We are not reusing the buffer so unmap it and free
562 * any references we are holding to it
563 */
564 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
565 ixgbevf_rx_pg_size(rx_ring),
566 DMA_FROM_DEVICE,
567 IXGBEVF_RX_DMA_ATTR);
568 __page_frag_cache_drain(rx_buffer->page,
569 rx_buffer->pagecnt_bias);
570 }
571
572 /* clear contents of rx_buffer */
573 rx_buffer->page = NULL;
574 }
575
576 /**
577 * ixgbevf_is_non_eop - process handling of non-EOP buffers
578 * @rx_ring: Rx ring being processed
579 * @rx_desc: Rx descriptor for current buffer
580 *
581 * This function updates next to clean. If the buffer is an EOP buffer
582 * this function exits returning false, otherwise it will place the
583 * sk_buff in the next buffer to be chained and return true indicating
584 * that this is in fact a non-EOP buffer.
585 **/
ixgbevf_is_non_eop(struct ixgbevf_ring * rx_ring,union ixgbe_adv_rx_desc * rx_desc)586 static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
587 union ixgbe_adv_rx_desc *rx_desc)
588 {
589 u32 ntc = rx_ring->next_to_clean + 1;
590
591 /* fetch, update, and store next to clean */
592 ntc = (ntc < rx_ring->count) ? ntc : 0;
593 rx_ring->next_to_clean = ntc;
594
595 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
596
597 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
598 return false;
599
600 return true;
601 }
602
ixgbevf_rx_offset(struct ixgbevf_ring * rx_ring)603 static inline unsigned int ixgbevf_rx_offset(struct ixgbevf_ring *rx_ring)
604 {
605 return ring_uses_build_skb(rx_ring) ? IXGBEVF_SKB_PAD : 0;
606 }
607
ixgbevf_alloc_mapped_page(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * bi)608 static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
609 struct ixgbevf_rx_buffer *bi)
610 {
611 struct page *page = bi->page;
612 dma_addr_t dma;
613
614 /* since we are recycling buffers we should seldom need to alloc */
615 if (likely(page))
616 return true;
617
618 /* alloc new page for storage */
619 page = dev_alloc_pages(ixgbevf_rx_pg_order(rx_ring));
620 if (unlikely(!page)) {
621 rx_ring->rx_stats.alloc_rx_page_failed++;
622 return false;
623 }
624
625 /* map page for use */
626 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
627 ixgbevf_rx_pg_size(rx_ring),
628 DMA_FROM_DEVICE, IXGBEVF_RX_DMA_ATTR);
629
630 /* if mapping failed free memory back to system since
631 * there isn't much point in holding memory we can't use
632 */
633 if (dma_mapping_error(rx_ring->dev, dma)) {
634 __free_pages(page, ixgbevf_rx_pg_order(rx_ring));
635
636 rx_ring->rx_stats.alloc_rx_page_failed++;
637 return false;
638 }
639
640 bi->dma = dma;
641 bi->page = page;
642 bi->page_offset = ixgbevf_rx_offset(rx_ring);
643 bi->pagecnt_bias = 1;
644 rx_ring->rx_stats.alloc_rx_page++;
645
646 return true;
647 }
648
649 /**
650 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
651 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
652 * @cleaned_count: number of buffers to replace
653 **/
ixgbevf_alloc_rx_buffers(struct ixgbevf_ring * rx_ring,u16 cleaned_count)654 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
655 u16 cleaned_count)
656 {
657 union ixgbe_adv_rx_desc *rx_desc;
658 struct ixgbevf_rx_buffer *bi;
659 unsigned int i = rx_ring->next_to_use;
660
661 /* nothing to do or no valid netdev defined */
662 if (!cleaned_count || !rx_ring->netdev)
663 return;
664
665 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
666 bi = &rx_ring->rx_buffer_info[i];
667 i -= rx_ring->count;
668
669 do {
670 if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
671 break;
672
673 /* sync the buffer for use by the device */
674 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
675 bi->page_offset,
676 ixgbevf_rx_bufsz(rx_ring),
677 DMA_FROM_DEVICE);
678
679 /* Refresh the desc even if pkt_addr didn't change
680 * because each write-back erases this info.
681 */
682 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
683
684 rx_desc++;
685 bi++;
686 i++;
687 if (unlikely(!i)) {
688 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
689 bi = rx_ring->rx_buffer_info;
690 i -= rx_ring->count;
691 }
692
693 /* clear the length for the next_to_use descriptor */
694 rx_desc->wb.upper.length = 0;
695
696 cleaned_count--;
697 } while (cleaned_count);
698
699 i += rx_ring->count;
700
701 if (rx_ring->next_to_use != i) {
702 /* record the next descriptor to use */
703 rx_ring->next_to_use = i;
704
705 /* update next to alloc since we have filled the ring */
706 rx_ring->next_to_alloc = i;
707
708 /* Force memory writes to complete before letting h/w
709 * know there are new descriptors to fetch. (Only
710 * applicable for weak-ordered memory model archs,
711 * such as IA-64).
712 */
713 wmb();
714 ixgbevf_write_tail(rx_ring, i);
715 }
716 }
717
718 /**
719 * ixgbevf_cleanup_headers - Correct corrupted or empty headers
720 * @rx_ring: rx descriptor ring packet is being transacted on
721 * @rx_desc: pointer to the EOP Rx descriptor
722 * @skb: pointer to current skb being fixed
723 *
724 * Check for corrupted packet headers caused by senders on the local L2
725 * embedded NIC switch not setting up their Tx Descriptors right. These
726 * should be very rare.
727 *
728 * Also address the case where we are pulling data in on pages only
729 * and as such no data is present in the skb header.
730 *
731 * In addition if skb is not at least 60 bytes we need to pad it so that
732 * it is large enough to qualify as a valid Ethernet frame.
733 *
734 * Returns true if an error was encountered and skb was freed.
735 **/
ixgbevf_cleanup_headers(struct ixgbevf_ring * rx_ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)736 static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
737 union ixgbe_adv_rx_desc *rx_desc,
738 struct sk_buff *skb)
739 {
740 /* verify that the packet does not have any known errors */
741 if (unlikely(ixgbevf_test_staterr(rx_desc,
742 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
743 struct net_device *netdev = rx_ring->netdev;
744
745 if (!(netdev->features & NETIF_F_RXALL)) {
746 dev_kfree_skb_any(skb);
747 return true;
748 }
749 }
750
751 /* if eth_skb_pad returns an error the skb was freed */
752 if (eth_skb_pad(skb))
753 return true;
754
755 return false;
756 }
757
758 /**
759 * ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
760 * @rx_ring: rx descriptor ring to store buffers on
761 * @old_buff: donor buffer to have page reused
762 *
763 * Synchronizes page for reuse by the adapter
764 **/
ixgbevf_reuse_rx_page(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * old_buff)765 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
766 struct ixgbevf_rx_buffer *old_buff)
767 {
768 struct ixgbevf_rx_buffer *new_buff;
769 u16 nta = rx_ring->next_to_alloc;
770
771 new_buff = &rx_ring->rx_buffer_info[nta];
772
773 /* update, and store next to alloc */
774 nta++;
775 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
776
777 /* transfer page from old buffer to new buffer */
778 new_buff->page = old_buff->page;
779 new_buff->dma = old_buff->dma;
780 new_buff->page_offset = old_buff->page_offset;
781 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
782 }
783
ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer * rx_buffer)784 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer)
785 {
786 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
787 struct page *page = rx_buffer->page;
788
789 /* avoid re-using remote and pfmemalloc pages */
790 if (!dev_page_is_reusable(page))
791 return false;
792
793 #if (PAGE_SIZE < 8192)
794 /* if we are only owner of page we can reuse it */
795 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
796 return false;
797 #else
798 #define IXGBEVF_LAST_OFFSET \
799 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IXGBEVF_RXBUFFER_2048)
800
801 if (rx_buffer->page_offset > IXGBEVF_LAST_OFFSET)
802 return false;
803
804 #endif
805
806 /* If we have drained the page fragment pool we need to update
807 * the pagecnt_bias and page count so that we fully restock the
808 * number of references the driver holds.
809 */
810 if (unlikely(!pagecnt_bias)) {
811 page_ref_add(page, USHRT_MAX);
812 rx_buffer->pagecnt_bias = USHRT_MAX;
813 }
814
815 return true;
816 }
817
818 /**
819 * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
820 * @rx_ring: rx descriptor ring to transact packets on
821 * @rx_buffer: buffer containing page to add
822 * @skb: sk_buff to place the data into
823 * @size: size of buffer to be added
824 *
825 * This function will add the data contained in rx_buffer->page to the skb.
826 **/
ixgbevf_add_rx_frag(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * rx_buffer,struct sk_buff * skb,unsigned int size)827 static void ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
828 struct ixgbevf_rx_buffer *rx_buffer,
829 struct sk_buff *skb,
830 unsigned int size)
831 {
832 #if (PAGE_SIZE < 8192)
833 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
834 #else
835 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
836 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) :
837 SKB_DATA_ALIGN(size);
838 #endif
839 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
840 rx_buffer->page_offset, size, truesize);
841 #if (PAGE_SIZE < 8192)
842 rx_buffer->page_offset ^= truesize;
843 #else
844 rx_buffer->page_offset += truesize;
845 #endif
846 }
847
848 static
ixgbevf_construct_skb(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * rx_buffer,struct xdp_buff * xdp,union ixgbe_adv_rx_desc * rx_desc)849 struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
850 struct ixgbevf_rx_buffer *rx_buffer,
851 struct xdp_buff *xdp,
852 union ixgbe_adv_rx_desc *rx_desc)
853 {
854 unsigned int size = xdp->data_end - xdp->data;
855 #if (PAGE_SIZE < 8192)
856 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
857 #else
858 unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
859 xdp->data_hard_start);
860 #endif
861 unsigned int headlen;
862 struct sk_buff *skb;
863
864 /* prefetch first cache line of first page */
865 net_prefetch(xdp->data);
866
867 /* Note, we get here by enabling legacy-rx via:
868 *
869 * ethtool --set-priv-flags <dev> legacy-rx on
870 *
871 * In this mode, we currently get 0 extra XDP headroom as
872 * opposed to having legacy-rx off, where we process XDP
873 * packets going to stack via ixgbevf_build_skb().
874 *
875 * For ixgbevf_construct_skb() mode it means that the
876 * xdp->data_meta will always point to xdp->data, since
877 * the helper cannot expand the head. Should this ever
878 * changed in future for legacy-rx mode on, then lets also
879 * add xdp->data_meta handling here.
880 */
881
882 /* allocate a skb to store the frags */
883 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IXGBEVF_RX_HDR_SIZE);
884 if (unlikely(!skb))
885 return NULL;
886
887 /* Determine available headroom for copy */
888 headlen = size;
889 if (headlen > IXGBEVF_RX_HDR_SIZE)
890 headlen = eth_get_headlen(skb->dev, xdp->data,
891 IXGBEVF_RX_HDR_SIZE);
892
893 /* align pull length to size of long to optimize memcpy performance */
894 memcpy(__skb_put(skb, headlen), xdp->data,
895 ALIGN(headlen, sizeof(long)));
896
897 /* update all of the pointers */
898 size -= headlen;
899 if (size) {
900 skb_add_rx_frag(skb, 0, rx_buffer->page,
901 (xdp->data + headlen) -
902 page_address(rx_buffer->page),
903 size, truesize);
904 #if (PAGE_SIZE < 8192)
905 rx_buffer->page_offset ^= truesize;
906 #else
907 rx_buffer->page_offset += truesize;
908 #endif
909 } else {
910 rx_buffer->pagecnt_bias++;
911 }
912
913 return skb;
914 }
915
ixgbevf_irq_enable_queues(struct ixgbevf_adapter * adapter,u32 qmask)916 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
917 u32 qmask)
918 {
919 struct ixgbe_hw *hw = &adapter->hw;
920
921 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
922 }
923
ixgbevf_build_skb(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * rx_buffer,struct xdp_buff * xdp,union ixgbe_adv_rx_desc * rx_desc)924 static struct sk_buff *ixgbevf_build_skb(struct ixgbevf_ring *rx_ring,
925 struct ixgbevf_rx_buffer *rx_buffer,
926 struct xdp_buff *xdp,
927 union ixgbe_adv_rx_desc *rx_desc)
928 {
929 unsigned int metasize = xdp->data - xdp->data_meta;
930 #if (PAGE_SIZE < 8192)
931 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
932 #else
933 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
934 SKB_DATA_ALIGN(xdp->data_end -
935 xdp->data_hard_start);
936 #endif
937 struct sk_buff *skb;
938
939 /* Prefetch first cache line of first page. If xdp->data_meta
940 * is unused, this points to xdp->data, otherwise, we likely
941 * have a consumer accessing first few bytes of meta data,
942 * and then actual data.
943 */
944 net_prefetch(xdp->data_meta);
945
946 /* build an skb around the page buffer */
947 skb = napi_build_skb(xdp->data_hard_start, truesize);
948 if (unlikely(!skb))
949 return NULL;
950
951 /* update pointers within the skb to store the data */
952 skb_reserve(skb, xdp->data - xdp->data_hard_start);
953 __skb_put(skb, xdp->data_end - xdp->data);
954 if (metasize)
955 skb_metadata_set(skb, metasize);
956
957 /* update buffer offset */
958 #if (PAGE_SIZE < 8192)
959 rx_buffer->page_offset ^= truesize;
960 #else
961 rx_buffer->page_offset += truesize;
962 #endif
963
964 return skb;
965 }
966
967 #define IXGBEVF_XDP_PASS 0
968 #define IXGBEVF_XDP_CONSUMED 1
969 #define IXGBEVF_XDP_TX 2
970
ixgbevf_xmit_xdp_ring(struct ixgbevf_ring * ring,struct xdp_buff * xdp)971 static int ixgbevf_xmit_xdp_ring(struct ixgbevf_ring *ring,
972 struct xdp_buff *xdp)
973 {
974 struct ixgbevf_tx_buffer *tx_buffer;
975 union ixgbe_adv_tx_desc *tx_desc;
976 u32 len, cmd_type;
977 dma_addr_t dma;
978 u16 i;
979
980 len = xdp->data_end - xdp->data;
981
982 if (unlikely(!ixgbevf_desc_unused(ring)))
983 return IXGBEVF_XDP_CONSUMED;
984
985 dma = dma_map_single(ring->dev, xdp->data, len, DMA_TO_DEVICE);
986 if (dma_mapping_error(ring->dev, dma))
987 return IXGBEVF_XDP_CONSUMED;
988
989 /* record the location of the first descriptor for this packet */
990 i = ring->next_to_use;
991 tx_buffer = &ring->tx_buffer_info[i];
992
993 dma_unmap_len_set(tx_buffer, len, len);
994 dma_unmap_addr_set(tx_buffer, dma, dma);
995 tx_buffer->data = xdp->data;
996 tx_buffer->bytecount = len;
997 tx_buffer->gso_segs = 1;
998 tx_buffer->protocol = 0;
999
1000 /* Populate minimal context descriptor that will provide for the
1001 * fact that we are expected to process Ethernet frames.
1002 */
1003 if (!test_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state)) {
1004 struct ixgbe_adv_tx_context_desc *context_desc;
1005
1006 set_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1007
1008 context_desc = IXGBEVF_TX_CTXTDESC(ring, 0);
1009 context_desc->vlan_macip_lens =
1010 cpu_to_le32(ETH_HLEN << IXGBE_ADVTXD_MACLEN_SHIFT);
1011 context_desc->fceof_saidx = 0;
1012 context_desc->type_tucmd_mlhl =
1013 cpu_to_le32(IXGBE_TXD_CMD_DEXT |
1014 IXGBE_ADVTXD_DTYP_CTXT);
1015 context_desc->mss_l4len_idx = 0;
1016
1017 i = 1;
1018 }
1019
1020 /* put descriptor type bits */
1021 cmd_type = IXGBE_ADVTXD_DTYP_DATA |
1022 IXGBE_ADVTXD_DCMD_DEXT |
1023 IXGBE_ADVTXD_DCMD_IFCS;
1024 cmd_type |= len | IXGBE_TXD_CMD;
1025
1026 tx_desc = IXGBEVF_TX_DESC(ring, i);
1027 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1028
1029 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1030 tx_desc->read.olinfo_status =
1031 cpu_to_le32((len << IXGBE_ADVTXD_PAYLEN_SHIFT) |
1032 IXGBE_ADVTXD_CC);
1033
1034 /* Avoid any potential race with cleanup */
1035 smp_wmb();
1036
1037 /* set next_to_watch value indicating a packet is present */
1038 i++;
1039 if (i == ring->count)
1040 i = 0;
1041
1042 tx_buffer->next_to_watch = tx_desc;
1043 ring->next_to_use = i;
1044
1045 return IXGBEVF_XDP_TX;
1046 }
1047
ixgbevf_run_xdp(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * rx_ring,struct xdp_buff * xdp)1048 static int ixgbevf_run_xdp(struct ixgbevf_adapter *adapter,
1049 struct ixgbevf_ring *rx_ring,
1050 struct xdp_buff *xdp)
1051 {
1052 int result = IXGBEVF_XDP_PASS;
1053 struct ixgbevf_ring *xdp_ring;
1054 struct bpf_prog *xdp_prog;
1055 u32 act;
1056
1057 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1058
1059 if (!xdp_prog)
1060 goto xdp_out;
1061
1062 act = bpf_prog_run_xdp(xdp_prog, xdp);
1063 switch (act) {
1064 case XDP_PASS:
1065 break;
1066 case XDP_TX:
1067 xdp_ring = adapter->xdp_ring[rx_ring->queue_index];
1068 result = ixgbevf_xmit_xdp_ring(xdp_ring, xdp);
1069 if (result == IXGBEVF_XDP_CONSUMED)
1070 goto out_failure;
1071 break;
1072 default:
1073 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
1074 fallthrough;
1075 case XDP_ABORTED:
1076 out_failure:
1077 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
1078 fallthrough; /* handle aborts by dropping packet */
1079 case XDP_DROP:
1080 result = IXGBEVF_XDP_CONSUMED;
1081 break;
1082 }
1083 xdp_out:
1084 return result;
1085 }
1086
ixgbevf_rx_frame_truesize(struct ixgbevf_ring * rx_ring,unsigned int size)1087 static unsigned int ixgbevf_rx_frame_truesize(struct ixgbevf_ring *rx_ring,
1088 unsigned int size)
1089 {
1090 unsigned int truesize;
1091
1092 #if (PAGE_SIZE < 8192)
1093 truesize = ixgbevf_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */
1094 #else
1095 truesize = ring_uses_build_skb(rx_ring) ?
1096 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) +
1097 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
1098 SKB_DATA_ALIGN(size);
1099 #endif
1100 return truesize;
1101 }
1102
ixgbevf_rx_buffer_flip(struct ixgbevf_ring * rx_ring,struct ixgbevf_rx_buffer * rx_buffer,unsigned int size)1103 static void ixgbevf_rx_buffer_flip(struct ixgbevf_ring *rx_ring,
1104 struct ixgbevf_rx_buffer *rx_buffer,
1105 unsigned int size)
1106 {
1107 unsigned int truesize = ixgbevf_rx_frame_truesize(rx_ring, size);
1108
1109 #if (PAGE_SIZE < 8192)
1110 rx_buffer->page_offset ^= truesize;
1111 #else
1112 rx_buffer->page_offset += truesize;
1113 #endif
1114 }
1115
ixgbevf_clean_rx_irq(struct ixgbevf_q_vector * q_vector,struct ixgbevf_ring * rx_ring,int budget)1116 static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
1117 struct ixgbevf_ring *rx_ring,
1118 int budget)
1119 {
1120 unsigned int total_rx_bytes = 0, total_rx_packets = 0, frame_sz = 0;
1121 struct ixgbevf_adapter *adapter = q_vector->adapter;
1122 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
1123 struct sk_buff *skb = rx_ring->skb;
1124 bool xdp_xmit = false;
1125 struct xdp_buff xdp;
1126 int xdp_res = 0;
1127
1128 /* Frame size depend on rx_ring setup when PAGE_SIZE=4K */
1129 #if (PAGE_SIZE < 8192)
1130 frame_sz = ixgbevf_rx_frame_truesize(rx_ring, 0);
1131 #endif
1132 xdp_init_buff(&xdp, frame_sz, &rx_ring->xdp_rxq);
1133
1134 while (likely(total_rx_packets < budget)) {
1135 struct ixgbevf_rx_buffer *rx_buffer;
1136 union ixgbe_adv_rx_desc *rx_desc;
1137 unsigned int size;
1138
1139 /* return some buffers to hardware, one at a time is too slow */
1140 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
1141 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
1142 cleaned_count = 0;
1143 }
1144
1145 rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1146 size = le16_to_cpu(rx_desc->wb.upper.length);
1147 if (!size)
1148 break;
1149
1150 /* This memory barrier is needed to keep us from reading
1151 * any other fields out of the rx_desc until we know the
1152 * RXD_STAT_DD bit is set
1153 */
1154 rmb();
1155
1156 rx_buffer = ixgbevf_get_rx_buffer(rx_ring, size);
1157
1158 /* retrieve a buffer from the ring */
1159 if (!skb) {
1160 unsigned int offset = ixgbevf_rx_offset(rx_ring);
1161 unsigned char *hard_start;
1162
1163 hard_start = page_address(rx_buffer->page) +
1164 rx_buffer->page_offset - offset;
1165 xdp_prepare_buff(&xdp, hard_start, offset, size, true);
1166 #if (PAGE_SIZE > 4096)
1167 /* At larger PAGE_SIZE, frame_sz depend on len size */
1168 xdp.frame_sz = ixgbevf_rx_frame_truesize(rx_ring, size);
1169 #endif
1170 xdp_res = ixgbevf_run_xdp(adapter, rx_ring, &xdp);
1171 }
1172
1173 if (xdp_res) {
1174 if (xdp_res == IXGBEVF_XDP_TX) {
1175 xdp_xmit = true;
1176 ixgbevf_rx_buffer_flip(rx_ring, rx_buffer,
1177 size);
1178 } else {
1179 rx_buffer->pagecnt_bias++;
1180 }
1181 total_rx_packets++;
1182 total_rx_bytes += size;
1183 } else if (skb) {
1184 ixgbevf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1185 } else if (ring_uses_build_skb(rx_ring)) {
1186 skb = ixgbevf_build_skb(rx_ring, rx_buffer,
1187 &xdp, rx_desc);
1188 } else {
1189 skb = ixgbevf_construct_skb(rx_ring, rx_buffer,
1190 &xdp, rx_desc);
1191 }
1192
1193 /* exit if we failed to retrieve a buffer */
1194 if (!xdp_res && !skb) {
1195 rx_ring->rx_stats.alloc_rx_buff_failed++;
1196 rx_buffer->pagecnt_bias++;
1197 break;
1198 }
1199
1200 ixgbevf_put_rx_buffer(rx_ring, rx_buffer, skb);
1201 cleaned_count++;
1202
1203 /* fetch next buffer in frame if non-eop */
1204 if (ixgbevf_is_non_eop(rx_ring, rx_desc))
1205 continue;
1206
1207 /* verify the packet layout is correct */
1208 if (xdp_res || ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
1209 skb = NULL;
1210 continue;
1211 }
1212
1213 /* probably a little skewed due to removing CRC */
1214 total_rx_bytes += skb->len;
1215
1216 /* Workaround hardware that can't do proper VEPA multicast
1217 * source pruning.
1218 */
1219 if ((skb->pkt_type == PACKET_BROADCAST ||
1220 skb->pkt_type == PACKET_MULTICAST) &&
1221 ether_addr_equal(rx_ring->netdev->dev_addr,
1222 eth_hdr(skb)->h_source)) {
1223 dev_kfree_skb_irq(skb);
1224 continue;
1225 }
1226
1227 /* populate checksum, VLAN, and protocol */
1228 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
1229
1230 ixgbevf_rx_skb(q_vector, skb);
1231
1232 /* reset skb pointer */
1233 skb = NULL;
1234
1235 /* update budget accounting */
1236 total_rx_packets++;
1237 }
1238
1239 /* place incomplete frames back on ring for completion */
1240 rx_ring->skb = skb;
1241
1242 if (xdp_xmit) {
1243 struct ixgbevf_ring *xdp_ring =
1244 adapter->xdp_ring[rx_ring->queue_index];
1245
1246 /* Force memory writes to complete before letting h/w
1247 * know there are new descriptors to fetch.
1248 */
1249 wmb();
1250 ixgbevf_write_tail(xdp_ring, xdp_ring->next_to_use);
1251 }
1252
1253 u64_stats_update_begin(&rx_ring->syncp);
1254 rx_ring->stats.packets += total_rx_packets;
1255 rx_ring->stats.bytes += total_rx_bytes;
1256 u64_stats_update_end(&rx_ring->syncp);
1257 q_vector->rx.total_packets += total_rx_packets;
1258 q_vector->rx.total_bytes += total_rx_bytes;
1259
1260 return total_rx_packets;
1261 }
1262
1263 /**
1264 * ixgbevf_poll - NAPI polling calback
1265 * @napi: napi struct with our devices info in it
1266 * @budget: amount of work driver is allowed to do this pass, in packets
1267 *
1268 * This function will clean more than one or more rings associated with a
1269 * q_vector.
1270 **/
ixgbevf_poll(struct napi_struct * napi,int budget)1271 static int ixgbevf_poll(struct napi_struct *napi, int budget)
1272 {
1273 struct ixgbevf_q_vector *q_vector =
1274 container_of(napi, struct ixgbevf_q_vector, napi);
1275 struct ixgbevf_adapter *adapter = q_vector->adapter;
1276 struct ixgbevf_ring *ring;
1277 int per_ring_budget, work_done = 0;
1278 bool clean_complete = true;
1279
1280 ixgbevf_for_each_ring(ring, q_vector->tx) {
1281 if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
1282 clean_complete = false;
1283 }
1284
1285 if (budget <= 0)
1286 return budget;
1287
1288 /* attempt to distribute budget to each queue fairly, but don't allow
1289 * the budget to go below 1 because we'll exit polling
1290 */
1291 if (q_vector->rx.count > 1)
1292 per_ring_budget = max(budget/q_vector->rx.count, 1);
1293 else
1294 per_ring_budget = budget;
1295
1296 ixgbevf_for_each_ring(ring, q_vector->rx) {
1297 int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
1298 per_ring_budget);
1299 work_done += cleaned;
1300 if (cleaned >= per_ring_budget)
1301 clean_complete = false;
1302 }
1303
1304 /* If all work not completed, return budget and keep polling */
1305 if (!clean_complete)
1306 return budget;
1307
1308 /* Exit the polling mode, but don't re-enable interrupts if stack might
1309 * poll us due to busy-polling
1310 */
1311 if (likely(napi_complete_done(napi, work_done))) {
1312 if (adapter->rx_itr_setting == 1)
1313 ixgbevf_set_itr(q_vector);
1314 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1315 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
1316 ixgbevf_irq_enable_queues(adapter,
1317 BIT(q_vector->v_idx));
1318 }
1319
1320 return min(work_done, budget - 1);
1321 }
1322
1323 /**
1324 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
1325 * @q_vector: structure containing interrupt and ring information
1326 **/
ixgbevf_write_eitr(struct ixgbevf_q_vector * q_vector)1327 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
1328 {
1329 struct ixgbevf_adapter *adapter = q_vector->adapter;
1330 struct ixgbe_hw *hw = &adapter->hw;
1331 int v_idx = q_vector->v_idx;
1332 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1333
1334 /* set the WDIS bit to not clear the timer bits and cause an
1335 * immediate assertion of the interrupt
1336 */
1337 itr_reg |= IXGBE_EITR_CNT_WDIS;
1338
1339 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
1340 }
1341
1342 /**
1343 * ixgbevf_configure_msix - Configure MSI-X hardware
1344 * @adapter: board private structure
1345 *
1346 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1347 * interrupts.
1348 **/
ixgbevf_configure_msix(struct ixgbevf_adapter * adapter)1349 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1350 {
1351 struct ixgbevf_q_vector *q_vector;
1352 int q_vectors, v_idx;
1353
1354 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1355 adapter->eims_enable_mask = 0;
1356
1357 /* Populate the IVAR table and set the ITR values to the
1358 * corresponding register.
1359 */
1360 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1361 struct ixgbevf_ring *ring;
1362
1363 q_vector = adapter->q_vector[v_idx];
1364
1365 ixgbevf_for_each_ring(ring, q_vector->rx)
1366 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1367
1368 ixgbevf_for_each_ring(ring, q_vector->tx)
1369 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1370
1371 if (q_vector->tx.ring && !q_vector->rx.ring) {
1372 /* Tx only vector */
1373 if (adapter->tx_itr_setting == 1)
1374 q_vector->itr = IXGBE_12K_ITR;
1375 else
1376 q_vector->itr = adapter->tx_itr_setting;
1377 } else {
1378 /* Rx or Rx/Tx vector */
1379 if (adapter->rx_itr_setting == 1)
1380 q_vector->itr = IXGBE_20K_ITR;
1381 else
1382 q_vector->itr = adapter->rx_itr_setting;
1383 }
1384
1385 /* add q_vector eims value to global eims_enable_mask */
1386 adapter->eims_enable_mask |= BIT(v_idx);
1387
1388 ixgbevf_write_eitr(q_vector);
1389 }
1390
1391 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
1392 /* setup eims_other and add value to global eims_enable_mask */
1393 adapter->eims_other = BIT(v_idx);
1394 adapter->eims_enable_mask |= adapter->eims_other;
1395 }
1396
1397 enum latency_range {
1398 lowest_latency = 0,
1399 low_latency = 1,
1400 bulk_latency = 2,
1401 latency_invalid = 255
1402 };
1403
1404 /**
1405 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
1406 * @q_vector: structure containing interrupt and ring information
1407 * @ring_container: structure containing ring performance data
1408 *
1409 * Stores a new ITR value based on packets and byte
1410 * counts during the last interrupt. The advantage of per interrupt
1411 * computation is faster updates and more accurate ITR for the current
1412 * traffic pattern. Constants in this function were computed
1413 * based on theoretical maximum wire speed and thresholds were set based
1414 * on testing data as well as attempting to minimize response time
1415 * while increasing bulk throughput.
1416 **/
ixgbevf_update_itr(struct ixgbevf_q_vector * q_vector,struct ixgbevf_ring_container * ring_container)1417 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1418 struct ixgbevf_ring_container *ring_container)
1419 {
1420 int bytes = ring_container->total_bytes;
1421 int packets = ring_container->total_packets;
1422 u32 timepassed_us;
1423 u64 bytes_perint;
1424 u8 itr_setting = ring_container->itr;
1425
1426 if (packets == 0)
1427 return;
1428
1429 /* simple throttle rate management
1430 * 0-20MB/s lowest (100000 ints/s)
1431 * 20-100MB/s low (20000 ints/s)
1432 * 100-1249MB/s bulk (12000 ints/s)
1433 */
1434 /* what was last interrupt timeslice? */
1435 timepassed_us = q_vector->itr >> 2;
1436 if (timepassed_us == 0)
1437 return;
1438
1439 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1440
1441 switch (itr_setting) {
1442 case lowest_latency:
1443 if (bytes_perint > 10)
1444 itr_setting = low_latency;
1445 break;
1446 case low_latency:
1447 if (bytes_perint > 20)
1448 itr_setting = bulk_latency;
1449 else if (bytes_perint <= 10)
1450 itr_setting = lowest_latency;
1451 break;
1452 case bulk_latency:
1453 if (bytes_perint <= 20)
1454 itr_setting = low_latency;
1455 break;
1456 }
1457
1458 /* clear work counters since we have the values we need */
1459 ring_container->total_bytes = 0;
1460 ring_container->total_packets = 0;
1461
1462 /* write updated itr to ring container */
1463 ring_container->itr = itr_setting;
1464 }
1465
ixgbevf_set_itr(struct ixgbevf_q_vector * q_vector)1466 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
1467 {
1468 u32 new_itr = q_vector->itr;
1469 u8 current_itr;
1470
1471 ixgbevf_update_itr(q_vector, &q_vector->tx);
1472 ixgbevf_update_itr(q_vector, &q_vector->rx);
1473
1474 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1475
1476 switch (current_itr) {
1477 /* counts and packets in update_itr are dependent on these numbers */
1478 case lowest_latency:
1479 new_itr = IXGBE_100K_ITR;
1480 break;
1481 case low_latency:
1482 new_itr = IXGBE_20K_ITR;
1483 break;
1484 case bulk_latency:
1485 new_itr = IXGBE_12K_ITR;
1486 break;
1487 default:
1488 break;
1489 }
1490
1491 if (new_itr != q_vector->itr) {
1492 /* do an exponential smoothing */
1493 new_itr = (10 * new_itr * q_vector->itr) /
1494 ((9 * new_itr) + q_vector->itr);
1495
1496 /* save the algorithm value here */
1497 q_vector->itr = new_itr;
1498
1499 ixgbevf_write_eitr(q_vector);
1500 }
1501 }
1502
ixgbevf_msix_other(int irq,void * data)1503 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
1504 {
1505 struct ixgbevf_adapter *adapter = data;
1506 struct ixgbe_hw *hw = &adapter->hw;
1507
1508 hw->mac.get_link_status = 1;
1509
1510 ixgbevf_service_event_schedule(adapter);
1511
1512 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1513
1514 return IRQ_HANDLED;
1515 }
1516
1517 /**
1518 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
1519 * @irq: unused
1520 * @data: pointer to our q_vector struct for this interrupt vector
1521 **/
ixgbevf_msix_clean_rings(int irq,void * data)1522 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
1523 {
1524 struct ixgbevf_q_vector *q_vector = data;
1525
1526 /* EIAM disabled interrupts (on this vector) for us */
1527 if (q_vector->rx.ring || q_vector->tx.ring)
1528 napi_schedule_irqoff(&q_vector->napi);
1529
1530 return IRQ_HANDLED;
1531 }
1532
1533 /**
1534 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1535 * @adapter: board private structure
1536 *
1537 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1538 * interrupts from the kernel.
1539 **/
ixgbevf_request_msix_irqs(struct ixgbevf_adapter * adapter)1540 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1541 {
1542 struct net_device *netdev = adapter->netdev;
1543 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1544 unsigned int ri = 0, ti = 0;
1545 int vector, err;
1546
1547 for (vector = 0; vector < q_vectors; vector++) {
1548 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1549 struct msix_entry *entry = &adapter->msix_entries[vector];
1550
1551 if (q_vector->tx.ring && q_vector->rx.ring) {
1552 snprintf(q_vector->name, sizeof(q_vector->name),
1553 "%s-TxRx-%u", netdev->name, ri++);
1554 ti++;
1555 } else if (q_vector->rx.ring) {
1556 snprintf(q_vector->name, sizeof(q_vector->name),
1557 "%s-rx-%u", netdev->name, ri++);
1558 } else if (q_vector->tx.ring) {
1559 snprintf(q_vector->name, sizeof(q_vector->name),
1560 "%s-tx-%u", netdev->name, ti++);
1561 } else {
1562 /* skip this unused q_vector */
1563 continue;
1564 }
1565 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1566 q_vector->name, q_vector);
1567 if (err) {
1568 hw_dbg(&adapter->hw,
1569 "request_irq failed for MSIX interrupt Error: %d\n",
1570 err);
1571 goto free_queue_irqs;
1572 }
1573 }
1574
1575 err = request_irq(adapter->msix_entries[vector].vector,
1576 &ixgbevf_msix_other, 0, netdev->name, adapter);
1577 if (err) {
1578 hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
1579 err);
1580 goto free_queue_irqs;
1581 }
1582
1583 return 0;
1584
1585 free_queue_irqs:
1586 while (vector) {
1587 vector--;
1588 free_irq(adapter->msix_entries[vector].vector,
1589 adapter->q_vector[vector]);
1590 }
1591 /* This failure is non-recoverable - it indicates the system is
1592 * out of MSIX vector resources and the VF driver cannot run
1593 * without them. Set the number of msix vectors to zero
1594 * indicating that not enough can be allocated. The error
1595 * will be returned to the user indicating device open failed.
1596 * Any further attempts to force the driver to open will also
1597 * fail. The only way to recover is to unload the driver and
1598 * reload it again. If the system has recovered some MSIX
1599 * vectors then it may succeed.
1600 */
1601 adapter->num_msix_vectors = 0;
1602 return err;
1603 }
1604
1605 /**
1606 * ixgbevf_request_irq - initialize interrupts
1607 * @adapter: board private structure
1608 *
1609 * Attempts to configure interrupts using the best available
1610 * capabilities of the hardware and kernel.
1611 **/
ixgbevf_request_irq(struct ixgbevf_adapter * adapter)1612 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1613 {
1614 int err = ixgbevf_request_msix_irqs(adapter);
1615
1616 if (err)
1617 hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
1618
1619 return err;
1620 }
1621
ixgbevf_free_irq(struct ixgbevf_adapter * adapter)1622 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1623 {
1624 int i, q_vectors;
1625
1626 if (!adapter->msix_entries)
1627 return;
1628
1629 q_vectors = adapter->num_msix_vectors;
1630 i = q_vectors - 1;
1631
1632 free_irq(adapter->msix_entries[i].vector, adapter);
1633 i--;
1634
1635 for (; i >= 0; i--) {
1636 /* free only the irqs that were actually requested */
1637 if (!adapter->q_vector[i]->rx.ring &&
1638 !adapter->q_vector[i]->tx.ring)
1639 continue;
1640
1641 free_irq(adapter->msix_entries[i].vector,
1642 adapter->q_vector[i]);
1643 }
1644 }
1645
1646 /**
1647 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1648 * @adapter: board private structure
1649 **/
ixgbevf_irq_disable(struct ixgbevf_adapter * adapter)1650 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1651 {
1652 struct ixgbe_hw *hw = &adapter->hw;
1653 int i;
1654
1655 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1656 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1657 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1658
1659 IXGBE_WRITE_FLUSH(hw);
1660
1661 for (i = 0; i < adapter->num_msix_vectors; i++)
1662 synchronize_irq(adapter->msix_entries[i].vector);
1663 }
1664
1665 /**
1666 * ixgbevf_irq_enable - Enable default interrupt generation settings
1667 * @adapter: board private structure
1668 **/
ixgbevf_irq_enable(struct ixgbevf_adapter * adapter)1669 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1670 {
1671 struct ixgbe_hw *hw = &adapter->hw;
1672
1673 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1674 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1675 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1676 }
1677
1678 /**
1679 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1680 * @adapter: board private structure
1681 * @ring: structure containing ring specific data
1682 *
1683 * Configure the Tx descriptor ring after a reset.
1684 **/
ixgbevf_configure_tx_ring(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * ring)1685 static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1686 struct ixgbevf_ring *ring)
1687 {
1688 struct ixgbe_hw *hw = &adapter->hw;
1689 u64 tdba = ring->dma;
1690 int wait_loop = 10;
1691 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1692 u8 reg_idx = ring->reg_idx;
1693
1694 /* disable queue to avoid issues while updating state */
1695 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1696 IXGBE_WRITE_FLUSH(hw);
1697
1698 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1699 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1700 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1701 ring->count * sizeof(union ixgbe_adv_tx_desc));
1702
1703 /* disable head writeback */
1704 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1705 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1706
1707 /* enable relaxed ordering */
1708 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1709 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1710 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1711
1712 /* reset head and tail pointers */
1713 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1714 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
1715 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
1716
1717 /* reset ntu and ntc to place SW in sync with hardwdare */
1718 ring->next_to_clean = 0;
1719 ring->next_to_use = 0;
1720
1721 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1722 * to or less than the number of on chip descriptors, which is
1723 * currently 40.
1724 */
1725 txdctl |= (8 << 16); /* WTHRESH = 8 */
1726
1727 /* Setting PTHRESH to 32 both improves performance */
1728 txdctl |= (1u << 8) | /* HTHRESH = 1 */
1729 32; /* PTHRESH = 32 */
1730
1731 /* reinitialize tx_buffer_info */
1732 memset(ring->tx_buffer_info, 0,
1733 sizeof(struct ixgbevf_tx_buffer) * ring->count);
1734
1735 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
1736 clear_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1737
1738 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1739
1740 /* poll to verify queue is enabled */
1741 do {
1742 usleep_range(1000, 2000);
1743 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1744 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1745 if (!wait_loop)
1746 hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
1747 }
1748
1749 /**
1750 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1751 * @adapter: board private structure
1752 *
1753 * Configure the Tx unit of the MAC after a reset.
1754 **/
ixgbevf_configure_tx(struct ixgbevf_adapter * adapter)1755 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1756 {
1757 u32 i;
1758
1759 /* Setup the HW Tx Head and Tail descriptor pointers */
1760 for (i = 0; i < adapter->num_tx_queues; i++)
1761 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
1762 for (i = 0; i < adapter->num_xdp_queues; i++)
1763 ixgbevf_configure_tx_ring(adapter, adapter->xdp_ring[i]);
1764 }
1765
1766 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1767
ixgbevf_configure_srrctl(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * ring,int index)1768 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter,
1769 struct ixgbevf_ring *ring, int index)
1770 {
1771 struct ixgbe_hw *hw = &adapter->hw;
1772 u32 srrctl;
1773
1774 srrctl = IXGBE_SRRCTL_DROP_EN;
1775
1776 srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1777 if (ring_uses_large_buffer(ring))
1778 srrctl |= IXGBEVF_RXBUFFER_3072 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1779 else
1780 srrctl |= IXGBEVF_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1781 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1782
1783 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1784 }
1785
ixgbevf_setup_psrtype(struct ixgbevf_adapter * adapter)1786 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1787 {
1788 struct ixgbe_hw *hw = &adapter->hw;
1789
1790 /* PSRTYPE must be initialized in 82599 */
1791 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1792 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1793 IXGBE_PSRTYPE_L2HDR;
1794
1795 if (adapter->num_rx_queues > 1)
1796 psrtype |= BIT(29);
1797
1798 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1799 }
1800
1801 #define IXGBEVF_MAX_RX_DESC_POLL 10
ixgbevf_disable_rx_queue(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * ring)1802 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1803 struct ixgbevf_ring *ring)
1804 {
1805 struct ixgbe_hw *hw = &adapter->hw;
1806 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1807 u32 rxdctl;
1808 u8 reg_idx = ring->reg_idx;
1809
1810 if (IXGBE_REMOVED(hw->hw_addr))
1811 return;
1812 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1813 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1814
1815 /* write value back with RXDCTL.ENABLE bit cleared */
1816 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1817
1818 /* the hardware may take up to 100us to really disable the Rx queue */
1819 do {
1820 udelay(10);
1821 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1822 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1823
1824 if (!wait_loop)
1825 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1826 reg_idx);
1827 }
1828
ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * ring)1829 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1830 struct ixgbevf_ring *ring)
1831 {
1832 struct ixgbe_hw *hw = &adapter->hw;
1833 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1834 u32 rxdctl;
1835 u8 reg_idx = ring->reg_idx;
1836
1837 if (IXGBE_REMOVED(hw->hw_addr))
1838 return;
1839 do {
1840 usleep_range(1000, 2000);
1841 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1842 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1843
1844 if (!wait_loop)
1845 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1846 reg_idx);
1847 }
1848
1849 /**
1850 * ixgbevf_init_rss_key - Initialize adapter RSS key
1851 * @adapter: device handle
1852 *
1853 * Allocates and initializes the RSS key if it is not allocated.
1854 **/
ixgbevf_init_rss_key(struct ixgbevf_adapter * adapter)1855 static inline int ixgbevf_init_rss_key(struct ixgbevf_adapter *adapter)
1856 {
1857 u32 *rss_key;
1858
1859 if (!adapter->rss_key) {
1860 rss_key = kzalloc(IXGBEVF_RSS_HASH_KEY_SIZE, GFP_KERNEL);
1861 if (unlikely(!rss_key))
1862 return -ENOMEM;
1863
1864 netdev_rss_key_fill(rss_key, IXGBEVF_RSS_HASH_KEY_SIZE);
1865 adapter->rss_key = rss_key;
1866 }
1867
1868 return 0;
1869 }
1870
ixgbevf_setup_vfmrqc(struct ixgbevf_adapter * adapter)1871 static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
1872 {
1873 struct ixgbe_hw *hw = &adapter->hw;
1874 u32 vfmrqc = 0, vfreta = 0;
1875 u16 rss_i = adapter->num_rx_queues;
1876 u8 i, j;
1877
1878 /* Fill out hash function seeds */
1879 for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
1880 IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), *(adapter->rss_key + i));
1881
1882 for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
1883 if (j == rss_i)
1884 j = 0;
1885
1886 adapter->rss_indir_tbl[i] = j;
1887
1888 vfreta |= j << (i & 0x3) * 8;
1889 if ((i & 3) == 3) {
1890 IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
1891 vfreta = 0;
1892 }
1893 }
1894
1895 /* Perform hash on these packet types */
1896 vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
1897 IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
1898 IXGBE_VFMRQC_RSS_FIELD_IPV6 |
1899 IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
1900
1901 vfmrqc |= IXGBE_VFMRQC_RSSEN;
1902
1903 IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
1904 }
1905
ixgbevf_configure_rx_ring(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * ring)1906 static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1907 struct ixgbevf_ring *ring)
1908 {
1909 struct ixgbe_hw *hw = &adapter->hw;
1910 union ixgbe_adv_rx_desc *rx_desc;
1911 u64 rdba = ring->dma;
1912 u32 rxdctl;
1913 u8 reg_idx = ring->reg_idx;
1914
1915 /* disable queue to avoid issues while updating state */
1916 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1917 ixgbevf_disable_rx_queue(adapter, ring);
1918
1919 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1920 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1921 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1922 ring->count * sizeof(union ixgbe_adv_rx_desc));
1923
1924 #ifndef CONFIG_SPARC
1925 /* enable relaxed ordering */
1926 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1927 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1928 #else
1929 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1930 IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1931 IXGBE_DCA_RXCTRL_DATA_WRO_EN);
1932 #endif
1933
1934 /* reset head and tail pointers */
1935 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1936 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
1937 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
1938
1939 /* initialize rx_buffer_info */
1940 memset(ring->rx_buffer_info, 0,
1941 sizeof(struct ixgbevf_rx_buffer) * ring->count);
1942
1943 /* initialize Rx descriptor 0 */
1944 rx_desc = IXGBEVF_RX_DESC(ring, 0);
1945 rx_desc->wb.upper.length = 0;
1946
1947 /* reset ntu and ntc to place SW in sync with hardwdare */
1948 ring->next_to_clean = 0;
1949 ring->next_to_use = 0;
1950 ring->next_to_alloc = 0;
1951
1952 ixgbevf_configure_srrctl(adapter, ring, reg_idx);
1953
1954 /* RXDCTL.RLPML does not work on 82599 */
1955 if (adapter->hw.mac.type != ixgbe_mac_82599_vf) {
1956 rxdctl &= ~(IXGBE_RXDCTL_RLPMLMASK |
1957 IXGBE_RXDCTL_RLPML_EN);
1958
1959 #if (PAGE_SIZE < 8192)
1960 /* Limit the maximum frame size so we don't overrun the skb */
1961 if (ring_uses_build_skb(ring) &&
1962 !ring_uses_large_buffer(ring))
1963 rxdctl |= IXGBEVF_MAX_FRAME_BUILD_SKB |
1964 IXGBE_RXDCTL_RLPML_EN;
1965 #endif
1966 }
1967
1968 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1969 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1970
1971 ixgbevf_rx_desc_queue_enable(adapter, ring);
1972 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
1973 }
1974
ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * rx_ring)1975 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter,
1976 struct ixgbevf_ring *rx_ring)
1977 {
1978 struct net_device *netdev = adapter->netdev;
1979 unsigned int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1980
1981 /* set build_skb and buffer size flags */
1982 clear_ring_build_skb_enabled(rx_ring);
1983 clear_ring_uses_large_buffer(rx_ring);
1984
1985 if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
1986 return;
1987
1988 if (PAGE_SIZE < 8192)
1989 if (max_frame > IXGBEVF_MAX_FRAME_BUILD_SKB)
1990 set_ring_uses_large_buffer(rx_ring);
1991
1992 /* 82599 can't rely on RXDCTL.RLPML to restrict the size of the frame */
1993 if (adapter->hw.mac.type == ixgbe_mac_82599_vf && !ring_uses_large_buffer(rx_ring))
1994 return;
1995
1996 set_ring_build_skb_enabled(rx_ring);
1997 }
1998
1999 /**
2000 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
2001 * @adapter: board private structure
2002 *
2003 * Configure the Rx unit of the MAC after a reset.
2004 **/
ixgbevf_configure_rx(struct ixgbevf_adapter * adapter)2005 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
2006 {
2007 struct ixgbe_hw *hw = &adapter->hw;
2008 struct net_device *netdev = adapter->netdev;
2009 int i, ret;
2010
2011 ixgbevf_setup_psrtype(adapter);
2012 if (hw->mac.type >= ixgbe_mac_X550_vf)
2013 ixgbevf_setup_vfmrqc(adapter);
2014
2015 spin_lock_bh(&adapter->mbx_lock);
2016 /* notify the PF of our intent to use this size of frame */
2017 ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
2018 spin_unlock_bh(&adapter->mbx_lock);
2019 if (ret)
2020 dev_err(&adapter->pdev->dev,
2021 "Failed to set MTU at %d\n", netdev->mtu);
2022
2023 /* Setup the HW Rx Head and Tail Descriptor Pointers and
2024 * the Base and Length of the Rx Descriptor Ring
2025 */
2026 for (i = 0; i < adapter->num_rx_queues; i++) {
2027 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
2028
2029 ixgbevf_set_rx_buffer_len(adapter, rx_ring);
2030 ixgbevf_configure_rx_ring(adapter, rx_ring);
2031 }
2032 }
2033
ixgbevf_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)2034 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
2035 __be16 proto, u16 vid)
2036 {
2037 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2038 struct ixgbe_hw *hw = &adapter->hw;
2039 int err;
2040
2041 spin_lock_bh(&adapter->mbx_lock);
2042
2043 /* add VID to filter table */
2044 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
2045
2046 spin_unlock_bh(&adapter->mbx_lock);
2047
2048 if (err) {
2049 netdev_err(netdev, "VF could not set VLAN %d\n", vid);
2050
2051 /* translate error return types so error makes sense */
2052 if (err == IXGBE_ERR_MBX)
2053 return -EIO;
2054
2055 if (err == IXGBE_ERR_INVALID_ARGUMENT)
2056 return -EACCES;
2057 }
2058
2059 set_bit(vid, adapter->active_vlans);
2060
2061 return err;
2062 }
2063
ixgbevf_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)2064 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
2065 __be16 proto, u16 vid)
2066 {
2067 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2068 struct ixgbe_hw *hw = &adapter->hw;
2069 int err;
2070
2071 spin_lock_bh(&adapter->mbx_lock);
2072
2073 /* remove VID from filter table */
2074 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
2075
2076 spin_unlock_bh(&adapter->mbx_lock);
2077
2078 if (err)
2079 netdev_err(netdev, "Could not remove VLAN %d\n", vid);
2080
2081 clear_bit(vid, adapter->active_vlans);
2082
2083 return err;
2084 }
2085
ixgbevf_restore_vlan(struct ixgbevf_adapter * adapter)2086 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
2087 {
2088 u16 vid;
2089
2090 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2091 ixgbevf_vlan_rx_add_vid(adapter->netdev,
2092 htons(ETH_P_8021Q), vid);
2093 }
2094
ixgbevf_write_uc_addr_list(struct net_device * netdev)2095 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
2096 {
2097 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2098 struct ixgbe_hw *hw = &adapter->hw;
2099 int count = 0;
2100
2101 if (!netdev_uc_empty(netdev)) {
2102 struct netdev_hw_addr *ha;
2103
2104 netdev_for_each_uc_addr(ha, netdev) {
2105 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
2106 udelay(200);
2107 }
2108 } else {
2109 /* If the list is empty then send message to PF driver to
2110 * clear all MAC VLANs on this VF.
2111 */
2112 hw->mac.ops.set_uc_addr(hw, 0, NULL);
2113 }
2114
2115 return count;
2116 }
2117
2118 /**
2119 * ixgbevf_set_rx_mode - Multicast and unicast set
2120 * @netdev: network interface device structure
2121 *
2122 * The set_rx_method entry point is called whenever the multicast address
2123 * list, unicast address list or the network interface flags are updated.
2124 * This routine is responsible for configuring the hardware for proper
2125 * multicast mode and configuring requested unicast filters.
2126 **/
ixgbevf_set_rx_mode(struct net_device * netdev)2127 static void ixgbevf_set_rx_mode(struct net_device *netdev)
2128 {
2129 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2130 struct ixgbe_hw *hw = &adapter->hw;
2131 unsigned int flags = netdev->flags;
2132 int xcast_mode;
2133
2134 /* request the most inclusive mode we need */
2135 if (flags & IFF_PROMISC)
2136 xcast_mode = IXGBEVF_XCAST_MODE_PROMISC;
2137 else if (flags & IFF_ALLMULTI)
2138 xcast_mode = IXGBEVF_XCAST_MODE_ALLMULTI;
2139 else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
2140 xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
2141 else
2142 xcast_mode = IXGBEVF_XCAST_MODE_NONE;
2143
2144 spin_lock_bh(&adapter->mbx_lock);
2145
2146 hw->mac.ops.update_xcast_mode(hw, xcast_mode);
2147
2148 /* reprogram multicast list */
2149 hw->mac.ops.update_mc_addr_list(hw, netdev);
2150
2151 ixgbevf_write_uc_addr_list(netdev);
2152
2153 spin_unlock_bh(&adapter->mbx_lock);
2154 }
2155
ixgbevf_napi_enable_all(struct ixgbevf_adapter * adapter)2156 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
2157 {
2158 int q_idx;
2159 struct ixgbevf_q_vector *q_vector;
2160 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2161
2162 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2163 q_vector = adapter->q_vector[q_idx];
2164 napi_enable(&q_vector->napi);
2165 }
2166 }
2167
ixgbevf_napi_disable_all(struct ixgbevf_adapter * adapter)2168 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
2169 {
2170 int q_idx;
2171 struct ixgbevf_q_vector *q_vector;
2172 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2173
2174 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2175 q_vector = adapter->q_vector[q_idx];
2176 napi_disable(&q_vector->napi);
2177 }
2178 }
2179
ixgbevf_configure_dcb(struct ixgbevf_adapter * adapter)2180 static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
2181 {
2182 struct ixgbe_hw *hw = &adapter->hw;
2183 unsigned int def_q = 0;
2184 unsigned int num_tcs = 0;
2185 unsigned int num_rx_queues = adapter->num_rx_queues;
2186 unsigned int num_tx_queues = adapter->num_tx_queues;
2187 int err;
2188
2189 spin_lock_bh(&adapter->mbx_lock);
2190
2191 /* fetch queue configuration from the PF */
2192 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2193
2194 spin_unlock_bh(&adapter->mbx_lock);
2195
2196 if (err)
2197 return err;
2198
2199 if (num_tcs > 1) {
2200 /* we need only one Tx queue */
2201 num_tx_queues = 1;
2202
2203 /* update default Tx ring register index */
2204 adapter->tx_ring[0]->reg_idx = def_q;
2205
2206 /* we need as many queues as traffic classes */
2207 num_rx_queues = num_tcs;
2208 }
2209
2210 /* if we have a bad config abort request queue reset */
2211 if ((adapter->num_rx_queues != num_rx_queues) ||
2212 (adapter->num_tx_queues != num_tx_queues)) {
2213 /* force mailbox timeout to prevent further messages */
2214 hw->mbx.timeout = 0;
2215
2216 /* wait for watchdog to come around and bail us out */
2217 set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
2218 }
2219
2220 return 0;
2221 }
2222
ixgbevf_configure(struct ixgbevf_adapter * adapter)2223 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
2224 {
2225 ixgbevf_configure_dcb(adapter);
2226
2227 ixgbevf_set_rx_mode(adapter->netdev);
2228
2229 ixgbevf_restore_vlan(adapter);
2230 ixgbevf_ipsec_restore(adapter);
2231
2232 ixgbevf_configure_tx(adapter);
2233 ixgbevf_configure_rx(adapter);
2234 }
2235
ixgbevf_save_reset_stats(struct ixgbevf_adapter * adapter)2236 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
2237 {
2238 /* Only save pre-reset stats if there are some */
2239 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
2240 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
2241 adapter->stats.base_vfgprc;
2242 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
2243 adapter->stats.base_vfgptc;
2244 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
2245 adapter->stats.base_vfgorc;
2246 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
2247 adapter->stats.base_vfgotc;
2248 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
2249 adapter->stats.base_vfmprc;
2250 }
2251 }
2252
ixgbevf_init_last_counter_stats(struct ixgbevf_adapter * adapter)2253 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
2254 {
2255 struct ixgbe_hw *hw = &adapter->hw;
2256
2257 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2258 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2259 adapter->stats.last_vfgorc |=
2260 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2261 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2262 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2263 adapter->stats.last_vfgotc |=
2264 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2265 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2266
2267 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
2268 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
2269 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
2270 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
2271 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
2272 }
2273
2274 /**
2275 * ixgbevf_set_features - Set features supported by PF
2276 * @adapter: pointer to the adapter struct
2277 *
2278 * Negotiate with PF supported features and then set pf_features accordingly.
2279 */
ixgbevf_set_features(struct ixgbevf_adapter * adapter)2280 static void ixgbevf_set_features(struct ixgbevf_adapter *adapter)
2281 {
2282 u32 *pf_features = &adapter->pf_features;
2283 struct ixgbe_hw *hw = &adapter->hw;
2284 int err;
2285
2286 err = hw->mac.ops.negotiate_features(hw, pf_features);
2287 if (err && err != -EOPNOTSUPP)
2288 netdev_dbg(adapter->netdev,
2289 "PF feature negotiation failed.\n");
2290
2291 /* Address also pre API 1.7 cases */
2292 if (hw->api_version == ixgbe_mbox_api_14)
2293 *pf_features |= IXGBEVF_PF_SUP_IPSEC;
2294 else if (hw->api_version == ixgbe_mbox_api_15)
2295 *pf_features |= IXGBEVF_PF_SUP_ESX_MBX;
2296 }
2297
ixgbevf_negotiate_api(struct ixgbevf_adapter * adapter)2298 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
2299 {
2300 struct ixgbe_hw *hw = &adapter->hw;
2301 static const int api[] = {
2302 ixgbe_mbox_api_17,
2303 ixgbe_mbox_api_16,
2304 ixgbe_mbox_api_15,
2305 ixgbe_mbox_api_14,
2306 ixgbe_mbox_api_13,
2307 ixgbe_mbox_api_12,
2308 ixgbe_mbox_api_11,
2309 ixgbe_mbox_api_10,
2310 ixgbe_mbox_api_unknown
2311 };
2312 int err, idx = 0;
2313
2314 spin_lock_bh(&adapter->mbx_lock);
2315
2316 while (api[idx] != ixgbe_mbox_api_unknown) {
2317 err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
2318 if (!err)
2319 break;
2320 idx++;
2321 }
2322
2323 ixgbevf_set_features(adapter);
2324
2325 if (adapter->pf_features & IXGBEVF_PF_SUP_ESX_MBX) {
2326 hw->mbx.ops.init_params(hw);
2327 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
2328 sizeof(struct ixgbe_mbx_operations));
2329 }
2330
2331 spin_unlock_bh(&adapter->mbx_lock);
2332 }
2333
ixgbevf_up_complete(struct ixgbevf_adapter * adapter)2334 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
2335 {
2336 struct net_device *netdev = adapter->netdev;
2337 struct pci_dev *pdev = adapter->pdev;
2338 struct ixgbe_hw *hw = &adapter->hw;
2339 bool state;
2340
2341 ixgbevf_configure_msix(adapter);
2342
2343 spin_lock_bh(&adapter->mbx_lock);
2344
2345 if (is_valid_ether_addr(hw->mac.addr))
2346 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2347 else
2348 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
2349
2350 spin_unlock_bh(&adapter->mbx_lock);
2351
2352 state = adapter->link_state;
2353 hw->mac.ops.get_link_state(hw, &adapter->link_state);
2354 if (state && state != adapter->link_state)
2355 dev_info(&pdev->dev, "VF is administratively disabled\n");
2356
2357 smp_mb__before_atomic();
2358 clear_bit(__IXGBEVF_DOWN, &adapter->state);
2359 ixgbevf_napi_enable_all(adapter);
2360
2361 /* clear any pending interrupts, may auto mask */
2362 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2363 ixgbevf_irq_enable(adapter);
2364
2365 /* enable transmits */
2366 netif_tx_start_all_queues(netdev);
2367
2368 ixgbevf_save_reset_stats(adapter);
2369 ixgbevf_init_last_counter_stats(adapter);
2370
2371 hw->mac.get_link_status = 1;
2372 mod_timer(&adapter->service_timer, jiffies);
2373 }
2374
ixgbevf_up(struct ixgbevf_adapter * adapter)2375 void ixgbevf_up(struct ixgbevf_adapter *adapter)
2376 {
2377 ixgbevf_configure(adapter);
2378
2379 ixgbevf_up_complete(adapter);
2380 }
2381
2382 /**
2383 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
2384 * @rx_ring: ring to free buffers from
2385 **/
ixgbevf_clean_rx_ring(struct ixgbevf_ring * rx_ring)2386 static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
2387 {
2388 u16 i = rx_ring->next_to_clean;
2389
2390 /* Free Rx ring sk_buff */
2391 if (rx_ring->skb) {
2392 dev_kfree_skb(rx_ring->skb);
2393 rx_ring->skb = NULL;
2394 }
2395
2396 /* Free all the Rx ring pages */
2397 while (i != rx_ring->next_to_alloc) {
2398 struct ixgbevf_rx_buffer *rx_buffer;
2399
2400 rx_buffer = &rx_ring->rx_buffer_info[i];
2401
2402 /* Invalidate cache lines that may have been written to by
2403 * device so that we avoid corrupting memory.
2404 */
2405 dma_sync_single_range_for_cpu(rx_ring->dev,
2406 rx_buffer->dma,
2407 rx_buffer->page_offset,
2408 ixgbevf_rx_bufsz(rx_ring),
2409 DMA_FROM_DEVICE);
2410
2411 /* free resources associated with mapping */
2412 dma_unmap_page_attrs(rx_ring->dev,
2413 rx_buffer->dma,
2414 ixgbevf_rx_pg_size(rx_ring),
2415 DMA_FROM_DEVICE,
2416 IXGBEVF_RX_DMA_ATTR);
2417
2418 __page_frag_cache_drain(rx_buffer->page,
2419 rx_buffer->pagecnt_bias);
2420
2421 i++;
2422 if (i == rx_ring->count)
2423 i = 0;
2424 }
2425
2426 rx_ring->next_to_alloc = 0;
2427 rx_ring->next_to_clean = 0;
2428 rx_ring->next_to_use = 0;
2429 }
2430
2431 /**
2432 * ixgbevf_clean_tx_ring - Free Tx Buffers
2433 * @tx_ring: ring to be cleaned
2434 **/
ixgbevf_clean_tx_ring(struct ixgbevf_ring * tx_ring)2435 static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
2436 {
2437 u16 i = tx_ring->next_to_clean;
2438 struct ixgbevf_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
2439
2440 while (i != tx_ring->next_to_use) {
2441 union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
2442
2443 /* Free all the Tx ring sk_buffs */
2444 if (ring_is_xdp(tx_ring))
2445 page_frag_free(tx_buffer->data);
2446 else
2447 dev_kfree_skb_any(tx_buffer->skb);
2448
2449 /* unmap skb header data */
2450 dma_unmap_single(tx_ring->dev,
2451 dma_unmap_addr(tx_buffer, dma),
2452 dma_unmap_len(tx_buffer, len),
2453 DMA_TO_DEVICE);
2454
2455 /* check for eop_desc to determine the end of the packet */
2456 eop_desc = tx_buffer->next_to_watch;
2457 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2458
2459 /* unmap remaining buffers */
2460 while (tx_desc != eop_desc) {
2461 tx_buffer++;
2462 tx_desc++;
2463 i++;
2464 if (unlikely(i == tx_ring->count)) {
2465 i = 0;
2466 tx_buffer = tx_ring->tx_buffer_info;
2467 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
2468 }
2469
2470 /* unmap any remaining paged data */
2471 if (dma_unmap_len(tx_buffer, len))
2472 dma_unmap_page(tx_ring->dev,
2473 dma_unmap_addr(tx_buffer, dma),
2474 dma_unmap_len(tx_buffer, len),
2475 DMA_TO_DEVICE);
2476 }
2477
2478 /* move us one more past the eop_desc for start of next pkt */
2479 tx_buffer++;
2480 i++;
2481 if (unlikely(i == tx_ring->count)) {
2482 i = 0;
2483 tx_buffer = tx_ring->tx_buffer_info;
2484 }
2485 }
2486
2487 /* reset next_to_use and next_to_clean */
2488 tx_ring->next_to_use = 0;
2489 tx_ring->next_to_clean = 0;
2490
2491 }
2492
2493 /**
2494 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2495 * @adapter: board private structure
2496 **/
ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter * adapter)2497 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2498 {
2499 int i;
2500
2501 for (i = 0; i < adapter->num_rx_queues; i++)
2502 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
2503 }
2504
2505 /**
2506 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2507 * @adapter: board private structure
2508 **/
ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter * adapter)2509 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2510 {
2511 int i;
2512
2513 for (i = 0; i < adapter->num_tx_queues; i++)
2514 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
2515 for (i = 0; i < adapter->num_xdp_queues; i++)
2516 ixgbevf_clean_tx_ring(adapter->xdp_ring[i]);
2517 }
2518
ixgbevf_down(struct ixgbevf_adapter * adapter)2519 void ixgbevf_down(struct ixgbevf_adapter *adapter)
2520 {
2521 struct net_device *netdev = adapter->netdev;
2522 struct ixgbe_hw *hw = &adapter->hw;
2523 int i;
2524
2525 /* signal that we are down to the interrupt handler */
2526 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2527 return; /* do nothing if already down */
2528
2529 /* disable all enabled Rx queues */
2530 for (i = 0; i < adapter->num_rx_queues; i++)
2531 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
2532
2533 usleep_range(10000, 20000);
2534
2535 netif_tx_stop_all_queues(netdev);
2536
2537 /* call carrier off first to avoid false dev_watchdog timeouts */
2538 netif_carrier_off(netdev);
2539 netif_tx_disable(netdev);
2540
2541 ixgbevf_irq_disable(adapter);
2542
2543 ixgbevf_napi_disable_all(adapter);
2544
2545 timer_delete_sync(&adapter->service_timer);
2546
2547 /* disable transmits in the hardware now that interrupts are off */
2548 for (i = 0; i < adapter->num_tx_queues; i++) {
2549 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2550
2551 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2552 IXGBE_TXDCTL_SWFLSH);
2553 }
2554
2555 for (i = 0; i < adapter->num_xdp_queues; i++) {
2556 u8 reg_idx = adapter->xdp_ring[i]->reg_idx;
2557
2558 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2559 IXGBE_TXDCTL_SWFLSH);
2560 }
2561
2562 if (!pci_channel_offline(adapter->pdev))
2563 ixgbevf_reset(adapter);
2564
2565 ixgbevf_clean_all_tx_rings(adapter);
2566 ixgbevf_clean_all_rx_rings(adapter);
2567 }
2568
ixgbevf_reinit_locked(struct ixgbevf_adapter * adapter)2569 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2570 {
2571 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2572 msleep(1);
2573
2574 ixgbevf_down(adapter);
2575 pci_set_master(adapter->pdev);
2576 ixgbevf_up(adapter);
2577
2578 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2579 }
2580
ixgbevf_reset(struct ixgbevf_adapter * adapter)2581 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2582 {
2583 struct ixgbe_hw *hw = &adapter->hw;
2584 struct net_device *netdev = adapter->netdev;
2585
2586 if (hw->mac.ops.reset_hw(hw)) {
2587 hw_dbg(hw, "PF still resetting\n");
2588 } else {
2589 hw->mac.ops.init_hw(hw);
2590 ixgbevf_negotiate_api(adapter);
2591 }
2592
2593 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2594 eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2595 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2596 }
2597
2598 adapter->last_reset = jiffies;
2599 }
2600
ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter * adapter,int vectors)2601 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2602 int vectors)
2603 {
2604 int vector_threshold;
2605
2606 /* We'll want at least 2 (vector_threshold):
2607 * 1) TxQ[0] + RxQ[0] handler
2608 * 2) Other (Link Status Change, etc.)
2609 */
2610 vector_threshold = MIN_MSIX_COUNT;
2611
2612 /* The more we get, the more we will assign to Tx/Rx Cleanup
2613 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2614 * Right now, we simply care about how many we'll get; we'll
2615 * set them up later while requesting irq's.
2616 */
2617 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2618 vector_threshold, vectors);
2619
2620 if (vectors < 0) {
2621 dev_err(&adapter->pdev->dev,
2622 "Unable to allocate MSI-X interrupts\n");
2623 kfree(adapter->msix_entries);
2624 adapter->msix_entries = NULL;
2625 return vectors;
2626 }
2627
2628 /* Adjust for only the vectors we'll use, which is minimum
2629 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2630 * vectors we were allocated.
2631 */
2632 adapter->num_msix_vectors = vectors;
2633
2634 return 0;
2635 }
2636
2637 /**
2638 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
2639 * @adapter: board private structure to initialize
2640 *
2641 * This is the top level queue allocation routine. The order here is very
2642 * important, starting with the "most" number of features turned on at once,
2643 * and ending with the smallest set of features. This way large combinations
2644 * can be allocated if they're turned on, and smaller combinations are the
2645 * fall through conditions.
2646 *
2647 **/
ixgbevf_set_num_queues(struct ixgbevf_adapter * adapter)2648 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2649 {
2650 struct ixgbe_hw *hw = &adapter->hw;
2651 unsigned int def_q = 0;
2652 unsigned int num_tcs = 0;
2653 int err;
2654
2655 /* Start with base case */
2656 adapter->num_rx_queues = 1;
2657 adapter->num_tx_queues = 1;
2658 adapter->num_xdp_queues = 0;
2659
2660 spin_lock_bh(&adapter->mbx_lock);
2661
2662 /* fetch queue configuration from the PF */
2663 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2664
2665 spin_unlock_bh(&adapter->mbx_lock);
2666
2667 if (err)
2668 return;
2669
2670 /* we need as many queues as traffic classes */
2671 if (num_tcs > 1) {
2672 adapter->num_rx_queues = num_tcs;
2673 } else {
2674 u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
2675
2676 switch (hw->api_version) {
2677 case ixgbe_mbox_api_11:
2678 case ixgbe_mbox_api_12:
2679 case ixgbe_mbox_api_13:
2680 case ixgbe_mbox_api_14:
2681 case ixgbe_mbox_api_15:
2682 case ixgbe_mbox_api_16:
2683 case ixgbe_mbox_api_17:
2684 if (adapter->xdp_prog &&
2685 hw->mac.max_tx_queues == rss)
2686 rss = rss > 3 ? 2 : 1;
2687
2688 adapter->num_rx_queues = rss;
2689 adapter->num_tx_queues = rss;
2690 adapter->num_xdp_queues = adapter->xdp_prog ? rss : 0;
2691 break;
2692 default:
2693 break;
2694 }
2695 }
2696 }
2697
2698 /**
2699 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2700 * @adapter: board private structure to initialize
2701 *
2702 * Attempt to configure the interrupts using the best available
2703 * capabilities of the hardware and the kernel.
2704 **/
ixgbevf_set_interrupt_capability(struct ixgbevf_adapter * adapter)2705 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2706 {
2707 int vector, v_budget;
2708
2709 /* It's easy to be greedy for MSI-X vectors, but it really
2710 * doesn't do us much good if we have a lot more vectors
2711 * than CPU's. So let's be conservative and only ask for
2712 * (roughly) the same number of vectors as there are CPU's.
2713 * The default is to use pairs of vectors.
2714 */
2715 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2716 v_budget = min_t(int, v_budget, num_online_cpus());
2717 v_budget += NON_Q_VECTORS;
2718
2719 adapter->msix_entries = kzalloc_objs(struct msix_entry, v_budget);
2720 if (!adapter->msix_entries)
2721 return -ENOMEM;
2722
2723 for (vector = 0; vector < v_budget; vector++)
2724 adapter->msix_entries[vector].entry = vector;
2725
2726 /* A failure in MSI-X entry allocation isn't fatal, but the VF driver
2727 * does not support any other modes, so we will simply fail here. Note
2728 * that we clean up the msix_entries pointer else-where.
2729 */
2730 return ixgbevf_acquire_msix_vectors(adapter, v_budget);
2731 }
2732
ixgbevf_add_ring(struct ixgbevf_ring * ring,struct ixgbevf_ring_container * head)2733 static void ixgbevf_add_ring(struct ixgbevf_ring *ring,
2734 struct ixgbevf_ring_container *head)
2735 {
2736 ring->next = head->ring;
2737 head->ring = ring;
2738 head->count++;
2739 }
2740
2741 /**
2742 * ixgbevf_alloc_q_vector - Allocate memory for a single interrupt vector
2743 * @adapter: board private structure to initialize
2744 * @v_idx: index of vector in adapter struct
2745 * @txr_count: number of Tx rings for q vector
2746 * @txr_idx: index of first Tx ring to assign
2747 * @xdp_count: total number of XDP rings to allocate
2748 * @xdp_idx: index of first XDP ring to allocate
2749 * @rxr_count: number of Rx rings for q vector
2750 * @rxr_idx: index of first Rx ring to assign
2751 *
2752 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2753 **/
ixgbevf_alloc_q_vector(struct ixgbevf_adapter * adapter,int v_idx,int txr_count,int txr_idx,int xdp_count,int xdp_idx,int rxr_count,int rxr_idx)2754 static int ixgbevf_alloc_q_vector(struct ixgbevf_adapter *adapter, int v_idx,
2755 int txr_count, int txr_idx,
2756 int xdp_count, int xdp_idx,
2757 int rxr_count, int rxr_idx)
2758 {
2759 struct ixgbevf_q_vector *q_vector;
2760 int reg_idx = txr_idx + xdp_idx;
2761 struct ixgbevf_ring *ring;
2762 int ring_count, size;
2763
2764 ring_count = txr_count + xdp_count + rxr_count;
2765 size = sizeof(*q_vector) + (sizeof(*ring) * ring_count);
2766
2767 /* allocate q_vector and rings */
2768 q_vector = kzalloc(size, GFP_KERNEL);
2769 if (!q_vector)
2770 return -ENOMEM;
2771
2772 /* initialize NAPI */
2773 netif_napi_add(adapter->netdev, &q_vector->napi, ixgbevf_poll);
2774
2775 /* tie q_vector and adapter together */
2776 adapter->q_vector[v_idx] = q_vector;
2777 q_vector->adapter = adapter;
2778 q_vector->v_idx = v_idx;
2779
2780 /* initialize pointer to rings */
2781 ring = q_vector->ring;
2782
2783 while (txr_count) {
2784 /* assign generic ring traits */
2785 ring->dev = &adapter->pdev->dev;
2786 ring->netdev = adapter->netdev;
2787
2788 /* configure backlink on ring */
2789 ring->q_vector = q_vector;
2790
2791 /* update q_vector Tx values */
2792 ixgbevf_add_ring(ring, &q_vector->tx);
2793
2794 /* apply Tx specific ring traits */
2795 ring->count = adapter->tx_ring_count;
2796 ring->queue_index = txr_idx;
2797 ring->reg_idx = reg_idx;
2798
2799 /* assign ring to adapter */
2800 adapter->tx_ring[txr_idx] = ring;
2801
2802 /* update count and index */
2803 txr_count--;
2804 txr_idx++;
2805 reg_idx++;
2806
2807 /* push pointer to next ring */
2808 ring++;
2809 }
2810
2811 while (xdp_count) {
2812 /* assign generic ring traits */
2813 ring->dev = &adapter->pdev->dev;
2814 ring->netdev = adapter->netdev;
2815
2816 /* configure backlink on ring */
2817 ring->q_vector = q_vector;
2818
2819 /* update q_vector Tx values */
2820 ixgbevf_add_ring(ring, &q_vector->tx);
2821
2822 /* apply Tx specific ring traits */
2823 ring->count = adapter->tx_ring_count;
2824 ring->queue_index = xdp_idx;
2825 ring->reg_idx = reg_idx;
2826 set_ring_xdp(ring);
2827
2828 /* assign ring to adapter */
2829 adapter->xdp_ring[xdp_idx] = ring;
2830
2831 /* update count and index */
2832 xdp_count--;
2833 xdp_idx++;
2834 reg_idx++;
2835
2836 /* push pointer to next ring */
2837 ring++;
2838 }
2839
2840 while (rxr_count) {
2841 /* assign generic ring traits */
2842 ring->dev = &adapter->pdev->dev;
2843 ring->netdev = adapter->netdev;
2844
2845 /* configure backlink on ring */
2846 ring->q_vector = q_vector;
2847
2848 /* update q_vector Rx values */
2849 ixgbevf_add_ring(ring, &q_vector->rx);
2850
2851 /* apply Rx specific ring traits */
2852 ring->count = adapter->rx_ring_count;
2853 ring->queue_index = rxr_idx;
2854 ring->reg_idx = rxr_idx;
2855
2856 /* assign ring to adapter */
2857 adapter->rx_ring[rxr_idx] = ring;
2858
2859 /* update count and index */
2860 rxr_count--;
2861 rxr_idx++;
2862
2863 /* push pointer to next ring */
2864 ring++;
2865 }
2866
2867 return 0;
2868 }
2869
2870 /**
2871 * ixgbevf_free_q_vector - Free memory allocated for specific interrupt vector
2872 * @adapter: board private structure to initialize
2873 * @v_idx: index of vector in adapter struct
2874 *
2875 * This function frees the memory allocated to the q_vector. In addition if
2876 * NAPI is enabled it will delete any references to the NAPI struct prior
2877 * to freeing the q_vector.
2878 **/
ixgbevf_free_q_vector(struct ixgbevf_adapter * adapter,int v_idx)2879 static void ixgbevf_free_q_vector(struct ixgbevf_adapter *adapter, int v_idx)
2880 {
2881 struct ixgbevf_q_vector *q_vector = adapter->q_vector[v_idx];
2882 struct ixgbevf_ring *ring;
2883
2884 ixgbevf_for_each_ring(ring, q_vector->tx) {
2885 if (ring_is_xdp(ring))
2886 adapter->xdp_ring[ring->queue_index] = NULL;
2887 else
2888 adapter->tx_ring[ring->queue_index] = NULL;
2889 }
2890
2891 ixgbevf_for_each_ring(ring, q_vector->rx)
2892 adapter->rx_ring[ring->queue_index] = NULL;
2893
2894 adapter->q_vector[v_idx] = NULL;
2895 netif_napi_del(&q_vector->napi);
2896
2897 /* ixgbevf_get_stats() might access the rings on this vector,
2898 * we must wait a grace period before freeing it.
2899 */
2900 kfree_rcu(q_vector, rcu);
2901 }
2902
2903 /**
2904 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2905 * @adapter: board private structure to initialize
2906 *
2907 * We allocate one q_vector per queue interrupt. If allocation fails we
2908 * return -ENOMEM.
2909 **/
ixgbevf_alloc_q_vectors(struct ixgbevf_adapter * adapter)2910 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2911 {
2912 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2913 int rxr_remaining = adapter->num_rx_queues;
2914 int txr_remaining = adapter->num_tx_queues;
2915 int xdp_remaining = adapter->num_xdp_queues;
2916 int rxr_idx = 0, txr_idx = 0, xdp_idx = 0, v_idx = 0;
2917 int err;
2918
2919 if (q_vectors >= (rxr_remaining + txr_remaining + xdp_remaining)) {
2920 for (; rxr_remaining; v_idx++, q_vectors--) {
2921 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2922
2923 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2924 0, 0, 0, 0, rqpv, rxr_idx);
2925 if (err)
2926 goto err_out;
2927
2928 /* update counts and index */
2929 rxr_remaining -= rqpv;
2930 rxr_idx += rqpv;
2931 }
2932 }
2933
2934 for (; q_vectors; v_idx++, q_vectors--) {
2935 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2936 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors);
2937 int xqpv = DIV_ROUND_UP(xdp_remaining, q_vectors);
2938
2939 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2940 tqpv, txr_idx,
2941 xqpv, xdp_idx,
2942 rqpv, rxr_idx);
2943
2944 if (err)
2945 goto err_out;
2946
2947 /* update counts and index */
2948 rxr_remaining -= rqpv;
2949 rxr_idx += rqpv;
2950 txr_remaining -= tqpv;
2951 txr_idx += tqpv;
2952 xdp_remaining -= xqpv;
2953 xdp_idx += xqpv;
2954 }
2955
2956 return 0;
2957
2958 err_out:
2959 while (v_idx) {
2960 v_idx--;
2961 ixgbevf_free_q_vector(adapter, v_idx);
2962 }
2963
2964 return -ENOMEM;
2965 }
2966
2967 /**
2968 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2969 * @adapter: board private structure to initialize
2970 *
2971 * This function frees the memory allocated to the q_vectors. In addition if
2972 * NAPI is enabled it will delete any references to the NAPI struct prior
2973 * to freeing the q_vector.
2974 **/
ixgbevf_free_q_vectors(struct ixgbevf_adapter * adapter)2975 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2976 {
2977 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2978
2979 while (q_vectors) {
2980 q_vectors--;
2981 ixgbevf_free_q_vector(adapter, q_vectors);
2982 }
2983 }
2984
2985 /**
2986 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2987 * @adapter: board private structure
2988 *
2989 **/
ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter * adapter)2990 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2991 {
2992 if (!adapter->msix_entries)
2993 return;
2994
2995 pci_disable_msix(adapter->pdev);
2996 kfree(adapter->msix_entries);
2997 adapter->msix_entries = NULL;
2998 }
2999
3000 /**
3001 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
3002 * @adapter: board private structure to initialize
3003 *
3004 **/
ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter * adapter)3005 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
3006 {
3007 int err;
3008
3009 /* Number of supported queues */
3010 ixgbevf_set_num_queues(adapter);
3011
3012 err = ixgbevf_set_interrupt_capability(adapter);
3013 if (err) {
3014 hw_dbg(&adapter->hw,
3015 "Unable to setup interrupt capabilities\n");
3016 goto err_set_interrupt;
3017 }
3018
3019 err = ixgbevf_alloc_q_vectors(adapter);
3020 if (err) {
3021 hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
3022 goto err_alloc_q_vectors;
3023 }
3024
3025 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u XDP Queue count %u\n",
3026 (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
3027 adapter->num_rx_queues, adapter->num_tx_queues,
3028 adapter->num_xdp_queues);
3029
3030 set_bit(__IXGBEVF_DOWN, &adapter->state);
3031
3032 return 0;
3033 err_alloc_q_vectors:
3034 ixgbevf_reset_interrupt_capability(adapter);
3035 err_set_interrupt:
3036 return err;
3037 }
3038
3039 /**
3040 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
3041 * @adapter: board private structure to clear interrupt scheme on
3042 *
3043 * We go through and clear interrupt specific resources and reset the structure
3044 * to pre-load conditions
3045 **/
ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter * adapter)3046 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
3047 {
3048 adapter->num_tx_queues = 0;
3049 adapter->num_xdp_queues = 0;
3050 adapter->num_rx_queues = 0;
3051
3052 ixgbevf_free_q_vectors(adapter);
3053 ixgbevf_reset_interrupt_capability(adapter);
3054 }
3055
3056 /**
3057 * ixgbevf_sw_init - Initialize general software structures
3058 * @adapter: board private structure to initialize
3059 *
3060 * ixgbevf_sw_init initializes the Adapter private data structure.
3061 * Fields are initialized based on PCI device information and
3062 * OS network device settings (MTU size).
3063 **/
ixgbevf_sw_init(struct ixgbevf_adapter * adapter)3064 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
3065 {
3066 struct ixgbe_hw *hw = &adapter->hw;
3067 struct pci_dev *pdev = adapter->pdev;
3068 struct net_device *netdev = adapter->netdev;
3069 int err;
3070
3071 /* PCI config space info */
3072 hw->vendor_id = pdev->vendor;
3073 hw->device_id = pdev->device;
3074 hw->revision_id = pdev->revision;
3075 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3076 hw->subsystem_device_id = pdev->subsystem_device;
3077
3078 hw->mbx.ops.init_params(hw);
3079
3080 if (hw->mac.type >= ixgbe_mac_X550_vf) {
3081 err = ixgbevf_init_rss_key(adapter);
3082 if (err)
3083 goto out;
3084 }
3085
3086 /* assume legacy case in which PF would only give VF 2 queues */
3087 hw->mac.max_tx_queues = 2;
3088 hw->mac.max_rx_queues = 2;
3089
3090 /* lock to protect mailbox accesses */
3091 spin_lock_init(&adapter->mbx_lock);
3092
3093 err = hw->mac.ops.reset_hw(hw);
3094 if (err) {
3095 dev_info(&pdev->dev,
3096 "PF still in reset state. Is the PF interface up?\n");
3097 } else {
3098 err = hw->mac.ops.init_hw(hw);
3099 if (err) {
3100 pr_err("init_shared_code failed: %d\n", err);
3101 goto out;
3102 }
3103 ixgbevf_negotiate_api(adapter);
3104 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
3105 if (err)
3106 dev_info(&pdev->dev, "Error reading MAC address\n");
3107 else if (is_zero_ether_addr(adapter->hw.mac.addr))
3108 dev_info(&pdev->dev,
3109 "MAC address not assigned by administrator.\n");
3110 eth_hw_addr_set(netdev, hw->mac.addr);
3111 }
3112
3113 if (!is_valid_ether_addr(netdev->dev_addr)) {
3114 dev_info(&pdev->dev, "Assigning random MAC address\n");
3115 eth_hw_addr_random(netdev);
3116 ether_addr_copy(hw->mac.addr, netdev->dev_addr);
3117 ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
3118 }
3119
3120 /* Enable dynamic interrupt throttling rates */
3121 adapter->rx_itr_setting = 1;
3122 adapter->tx_itr_setting = 1;
3123
3124 /* set default ring sizes */
3125 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
3126 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
3127
3128 adapter->link_state = true;
3129
3130 set_bit(__IXGBEVF_DOWN, &adapter->state);
3131 return 0;
3132
3133 out:
3134 return err;
3135 }
3136
3137 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
3138 { \
3139 u32 current_counter = IXGBE_READ_REG(hw, reg); \
3140 if (current_counter < last_counter) \
3141 counter += 0x100000000LL; \
3142 last_counter = current_counter; \
3143 counter &= 0xFFFFFFFF00000000LL; \
3144 counter |= current_counter; \
3145 }
3146
3147 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
3148 { \
3149 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
3150 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
3151 u64 current_counter = (current_counter_msb << 32) | \
3152 current_counter_lsb; \
3153 if (current_counter < last_counter) \
3154 counter += 0x1000000000LL; \
3155 last_counter = current_counter; \
3156 counter &= 0xFFFFFFF000000000LL; \
3157 counter |= current_counter; \
3158 }
3159 /**
3160 * ixgbevf_update_stats - Update the board statistics counters.
3161 * @adapter: board private structure
3162 **/
ixgbevf_update_stats(struct ixgbevf_adapter * adapter)3163 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
3164 {
3165 struct ixgbe_hw *hw = &adapter->hw;
3166 u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
3167 u64 alloc_rx_page = 0, hw_csum_rx_error = 0;
3168 int i;
3169
3170 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3171 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3172 return;
3173
3174 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
3175 adapter->stats.vfgprc);
3176 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
3177 adapter->stats.vfgptc);
3178 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
3179 adapter->stats.last_vfgorc,
3180 adapter->stats.vfgorc);
3181 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
3182 adapter->stats.last_vfgotc,
3183 adapter->stats.vfgotc);
3184 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
3185 adapter->stats.vfmprc);
3186
3187 for (i = 0; i < adapter->num_rx_queues; i++) {
3188 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
3189
3190 hw_csum_rx_error += rx_ring->rx_stats.csum_err;
3191 alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
3192 alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
3193 alloc_rx_page += rx_ring->rx_stats.alloc_rx_page;
3194 }
3195
3196 adapter->hw_csum_rx_error = hw_csum_rx_error;
3197 adapter->alloc_rx_page_failed = alloc_rx_page_failed;
3198 adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
3199 adapter->alloc_rx_page = alloc_rx_page;
3200 }
3201
3202 /**
3203 * ixgbevf_service_timer - Timer Call-back
3204 * @t: pointer to timer_list struct
3205 **/
ixgbevf_service_timer(struct timer_list * t)3206 static void ixgbevf_service_timer(struct timer_list *t)
3207 {
3208 struct ixgbevf_adapter *adapter = timer_container_of(adapter, t,
3209 service_timer);
3210
3211 /* Reset the timer */
3212 mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
3213
3214 ixgbevf_service_event_schedule(adapter);
3215 }
3216
ixgbevf_reset_subtask(struct ixgbevf_adapter * adapter)3217 static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
3218 {
3219 if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
3220 return;
3221
3222 rtnl_lock();
3223 /* If we're already down or resetting, just bail */
3224 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3225 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
3226 test_bit(__IXGBEVF_RESETTING, &adapter->state)) {
3227 rtnl_unlock();
3228 return;
3229 }
3230
3231 adapter->tx_timeout_count++;
3232
3233 ixgbevf_reinit_locked(adapter);
3234 rtnl_unlock();
3235 }
3236
3237 /**
3238 * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
3239 * @adapter: pointer to the device adapter structure
3240 *
3241 * This function serves two purposes. First it strobes the interrupt lines
3242 * in order to make certain interrupts are occurring. Secondly it sets the
3243 * bits needed to check for TX hangs. As a result we should immediately
3244 * determine if a hang has occurred.
3245 **/
ixgbevf_check_hang_subtask(struct ixgbevf_adapter * adapter)3246 static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
3247 {
3248 struct ixgbe_hw *hw = &adapter->hw;
3249 u32 eics = 0;
3250 int i;
3251
3252 /* If we're down or resetting, just bail */
3253 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3254 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3255 return;
3256
3257 /* Force detection of hung controller */
3258 if (netif_carrier_ok(adapter->netdev)) {
3259 for (i = 0; i < adapter->num_tx_queues; i++)
3260 set_check_for_tx_hang(adapter->tx_ring[i]);
3261 for (i = 0; i < adapter->num_xdp_queues; i++)
3262 set_check_for_tx_hang(adapter->xdp_ring[i]);
3263 }
3264
3265 /* get one bit for every active Tx/Rx interrupt vector */
3266 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
3267 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
3268
3269 if (qv->rx.ring || qv->tx.ring)
3270 eics |= BIT(i);
3271 }
3272
3273 /* Cause software interrupt to ensure rings are cleaned */
3274 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
3275 }
3276
3277 /**
3278 * ixgbevf_watchdog_update_link - update the link status
3279 * @adapter: pointer to the device adapter structure
3280 **/
ixgbevf_watchdog_update_link(struct ixgbevf_adapter * adapter)3281 static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
3282 {
3283 struct ixgbe_hw *hw = &adapter->hw;
3284 u32 link_speed = adapter->link_speed;
3285 bool link_up = adapter->link_up;
3286 s32 err;
3287
3288 spin_lock_bh(&adapter->mbx_lock);
3289
3290 err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3291
3292 spin_unlock_bh(&adapter->mbx_lock);
3293
3294 /* if check for link returns error we will need to reset */
3295 if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
3296 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
3297 link_up = false;
3298 }
3299
3300 adapter->link_up = link_up;
3301 adapter->link_speed = link_speed;
3302 }
3303
3304 /**
3305 * ixgbevf_watchdog_link_is_up - update netif_carrier status and
3306 * print link up message
3307 * @adapter: pointer to the device adapter structure
3308 **/
ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter * adapter)3309 static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
3310 {
3311 struct net_device *netdev = adapter->netdev;
3312
3313 /* only continue if link was previously down */
3314 if (netif_carrier_ok(netdev))
3315 return;
3316
3317 dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
3318 (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
3319 "10 Gbps" :
3320 (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
3321 "1 Gbps" :
3322 (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
3323 "100 Mbps" :
3324 "unknown speed");
3325
3326 netif_carrier_on(netdev);
3327 }
3328
3329 /**
3330 * ixgbevf_watchdog_link_is_down - update netif_carrier status and
3331 * print link down message
3332 * @adapter: pointer to the adapter structure
3333 **/
ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter * adapter)3334 static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
3335 {
3336 struct net_device *netdev = adapter->netdev;
3337
3338 adapter->link_speed = 0;
3339
3340 /* only continue if link was up previously */
3341 if (!netif_carrier_ok(netdev))
3342 return;
3343
3344 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
3345
3346 netif_carrier_off(netdev);
3347 }
3348
3349 /**
3350 * ixgbevf_watchdog_subtask - worker thread to bring link up
3351 * @adapter: board private structure
3352 **/
ixgbevf_watchdog_subtask(struct ixgbevf_adapter * adapter)3353 static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
3354 {
3355 /* if interface is down do nothing */
3356 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3357 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3358 return;
3359
3360 ixgbevf_watchdog_update_link(adapter);
3361
3362 if (adapter->link_up && adapter->link_state)
3363 ixgbevf_watchdog_link_is_up(adapter);
3364 else
3365 ixgbevf_watchdog_link_is_down(adapter);
3366
3367 ixgbevf_update_stats(adapter);
3368 }
3369
3370 /**
3371 * ixgbevf_service_task - manages and runs subtasks
3372 * @work: pointer to work_struct containing our data
3373 **/
ixgbevf_service_task(struct work_struct * work)3374 static void ixgbevf_service_task(struct work_struct *work)
3375 {
3376 struct ixgbevf_adapter *adapter = container_of(work,
3377 struct ixgbevf_adapter,
3378 service_task);
3379 struct ixgbe_hw *hw = &adapter->hw;
3380
3381 if (IXGBE_REMOVED(hw->hw_addr)) {
3382 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
3383 rtnl_lock();
3384 ixgbevf_down(adapter);
3385 rtnl_unlock();
3386 }
3387 return;
3388 }
3389
3390 ixgbevf_queue_reset_subtask(adapter);
3391 ixgbevf_reset_subtask(adapter);
3392 ixgbevf_watchdog_subtask(adapter);
3393 ixgbevf_check_hang_subtask(adapter);
3394
3395 ixgbevf_service_event_complete(adapter);
3396 }
3397
3398 /**
3399 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
3400 * @tx_ring: Tx descriptor ring for a specific queue
3401 *
3402 * Free all transmit software resources
3403 **/
ixgbevf_free_tx_resources(struct ixgbevf_ring * tx_ring)3404 void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
3405 {
3406 ixgbevf_clean_tx_ring(tx_ring);
3407
3408 vfree(tx_ring->tx_buffer_info);
3409 tx_ring->tx_buffer_info = NULL;
3410
3411 /* if not set, then don't free */
3412 if (!tx_ring->desc)
3413 return;
3414
3415 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
3416 tx_ring->dma);
3417
3418 tx_ring->desc = NULL;
3419 }
3420
3421 /**
3422 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
3423 * @adapter: board private structure
3424 *
3425 * Free all transmit software resources
3426 **/
ixgbevf_free_all_tx_resources(struct ixgbevf_adapter * adapter)3427 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
3428 {
3429 int i;
3430
3431 for (i = 0; i < adapter->num_tx_queues; i++)
3432 if (adapter->tx_ring[i]->desc)
3433 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3434 for (i = 0; i < adapter->num_xdp_queues; i++)
3435 if (adapter->xdp_ring[i]->desc)
3436 ixgbevf_free_tx_resources(adapter->xdp_ring[i]);
3437 }
3438
3439 /**
3440 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
3441 * @tx_ring: Tx descriptor ring (for a specific queue) to setup
3442 *
3443 * Return 0 on success, negative on failure
3444 **/
ixgbevf_setup_tx_resources(struct ixgbevf_ring * tx_ring)3445 int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
3446 {
3447 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3448 int size;
3449
3450 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
3451 tx_ring->tx_buffer_info = vmalloc(size);
3452 if (!tx_ring->tx_buffer_info)
3453 goto err;
3454
3455 u64_stats_init(&tx_ring->syncp);
3456
3457 /* round up to nearest 4K */
3458 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3459 tx_ring->size = ALIGN(tx_ring->size, 4096);
3460
3461 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
3462 &tx_ring->dma, GFP_KERNEL);
3463 if (!tx_ring->desc)
3464 goto err;
3465
3466 return 0;
3467
3468 err:
3469 vfree(tx_ring->tx_buffer_info);
3470 tx_ring->tx_buffer_info = NULL;
3471 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
3472 return -ENOMEM;
3473 }
3474
3475 /**
3476 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
3477 * @adapter: board private structure
3478 *
3479 * If this function returns with an error, then it's possible one or
3480 * more of the rings is populated (while the rest are not). It is the
3481 * callers duty to clean those orphaned rings.
3482 *
3483 * Return 0 on success, negative on failure
3484 **/
ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter * adapter)3485 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
3486 {
3487 int i, j = 0, err = 0;
3488
3489 for (i = 0; i < adapter->num_tx_queues; i++) {
3490 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
3491 if (!err)
3492 continue;
3493 hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
3494 goto err_setup_tx;
3495 }
3496
3497 for (j = 0; j < adapter->num_xdp_queues; j++) {
3498 err = ixgbevf_setup_tx_resources(adapter->xdp_ring[j]);
3499 if (!err)
3500 continue;
3501 hw_dbg(&adapter->hw, "Allocation for XDP Queue %u failed\n", j);
3502 goto err_setup_tx;
3503 }
3504
3505 return 0;
3506 err_setup_tx:
3507 /* rewind the index freeing the rings as we go */
3508 while (j--)
3509 ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
3510 while (i--)
3511 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3512
3513 return err;
3514 }
3515
3516 /**
3517 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
3518 * @adapter: board private structure
3519 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3520 *
3521 * Returns 0 on success, negative on failure
3522 **/
ixgbevf_setup_rx_resources(struct ixgbevf_adapter * adapter,struct ixgbevf_ring * rx_ring)3523 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
3524 struct ixgbevf_ring *rx_ring)
3525 {
3526 int size;
3527
3528 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
3529 rx_ring->rx_buffer_info = vmalloc(size);
3530 if (!rx_ring->rx_buffer_info)
3531 goto err;
3532
3533 u64_stats_init(&rx_ring->syncp);
3534
3535 /* Round up to nearest 4K */
3536 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
3537 rx_ring->size = ALIGN(rx_ring->size, 4096);
3538
3539 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
3540 &rx_ring->dma, GFP_KERNEL);
3541
3542 if (!rx_ring->desc)
3543 goto err;
3544
3545 /* XDP RX-queue info */
3546 if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, adapter->netdev,
3547 rx_ring->queue_index, 0) < 0)
3548 goto err;
3549
3550 rx_ring->xdp_prog = adapter->xdp_prog;
3551
3552 return 0;
3553 err:
3554 vfree(rx_ring->rx_buffer_info);
3555 rx_ring->rx_buffer_info = NULL;
3556 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
3557 return -ENOMEM;
3558 }
3559
3560 /**
3561 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
3562 * @adapter: board private structure
3563 *
3564 * If this function returns with an error, then it's possible one or
3565 * more of the rings is populated (while the rest are not). It is the
3566 * callers duty to clean those orphaned rings.
3567 *
3568 * Return 0 on success, negative on failure
3569 **/
ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter * adapter)3570 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
3571 {
3572 int i, err = 0;
3573
3574 for (i = 0; i < adapter->num_rx_queues; i++) {
3575 err = ixgbevf_setup_rx_resources(adapter, adapter->rx_ring[i]);
3576 if (!err)
3577 continue;
3578 hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
3579 goto err_setup_rx;
3580 }
3581
3582 return 0;
3583 err_setup_rx:
3584 /* rewind the index freeing the rings as we go */
3585 while (i--)
3586 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3587 return err;
3588 }
3589
3590 /**
3591 * ixgbevf_free_rx_resources - Free Rx Resources
3592 * @rx_ring: ring to clean the resources from
3593 *
3594 * Free all receive software resources
3595 **/
ixgbevf_free_rx_resources(struct ixgbevf_ring * rx_ring)3596 void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
3597 {
3598 ixgbevf_clean_rx_ring(rx_ring);
3599
3600 rx_ring->xdp_prog = NULL;
3601 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
3602 vfree(rx_ring->rx_buffer_info);
3603 rx_ring->rx_buffer_info = NULL;
3604
3605 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
3606 rx_ring->dma);
3607
3608 rx_ring->desc = NULL;
3609 }
3610
3611 /**
3612 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
3613 * @adapter: board private structure
3614 *
3615 * Free all receive software resources
3616 **/
ixgbevf_free_all_rx_resources(struct ixgbevf_adapter * adapter)3617 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
3618 {
3619 int i;
3620
3621 for (i = 0; i < adapter->num_rx_queues; i++)
3622 if (adapter->rx_ring[i]->desc)
3623 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3624 }
3625
3626 /**
3627 * ixgbevf_open - Called when a network interface is made active
3628 * @netdev: network interface device structure
3629 *
3630 * Returns 0 on success, negative value on failure
3631 *
3632 * The open entry point is called when a network interface is made
3633 * active by the system (IFF_UP). At this point all resources needed
3634 * for transmit and receive operations are allocated, the interrupt
3635 * handler is registered with the OS, the watchdog timer is started,
3636 * and the stack is notified that the interface is ready.
3637 **/
ixgbevf_open(struct net_device * netdev)3638 int ixgbevf_open(struct net_device *netdev)
3639 {
3640 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3641 struct ixgbe_hw *hw = &adapter->hw;
3642 int err;
3643
3644 /* A previous failure to open the device because of a lack of
3645 * available MSIX vector resources may have reset the number
3646 * of msix vectors variable to zero. The only way to recover
3647 * is to unload/reload the driver and hope that the system has
3648 * been able to recover some MSIX vector resources.
3649 */
3650 if (!adapter->num_msix_vectors)
3651 return -ENOMEM;
3652
3653 if (hw->adapter_stopped) {
3654 ixgbevf_reset(adapter);
3655 /* if adapter is still stopped then PF isn't up and
3656 * the VF can't start.
3657 */
3658 if (hw->adapter_stopped) {
3659 err = IXGBE_ERR_MBX;
3660 pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
3661 goto err_setup_reset;
3662 }
3663 }
3664
3665 /* disallow open during test */
3666 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
3667 return -EBUSY;
3668
3669 netif_carrier_off(netdev);
3670
3671 /* allocate transmit descriptors */
3672 err = ixgbevf_setup_all_tx_resources(adapter);
3673 if (err)
3674 goto err_setup_tx;
3675
3676 /* allocate receive descriptors */
3677 err = ixgbevf_setup_all_rx_resources(adapter);
3678 if (err)
3679 goto err_setup_rx;
3680
3681 ixgbevf_configure(adapter);
3682
3683 err = ixgbevf_request_irq(adapter);
3684 if (err)
3685 goto err_req_irq;
3686
3687 /* Notify the stack of the actual queue counts. */
3688 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3689 if (err)
3690 goto err_set_queues;
3691
3692 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3693 if (err)
3694 goto err_set_queues;
3695
3696 ixgbevf_up_complete(adapter);
3697
3698 return 0;
3699
3700 err_set_queues:
3701 ixgbevf_free_irq(adapter);
3702 err_req_irq:
3703 ixgbevf_free_all_rx_resources(adapter);
3704 err_setup_rx:
3705 ixgbevf_free_all_tx_resources(adapter);
3706 err_setup_tx:
3707 ixgbevf_reset(adapter);
3708 err_setup_reset:
3709
3710 return err;
3711 }
3712
3713 /**
3714 * ixgbevf_close_suspend - actions necessary to both suspend and close flows
3715 * @adapter: the private adapter struct
3716 *
3717 * This function should contain the necessary work common to both suspending
3718 * and closing of the device.
3719 */
ixgbevf_close_suspend(struct ixgbevf_adapter * adapter)3720 static void ixgbevf_close_suspend(struct ixgbevf_adapter *adapter)
3721 {
3722 ixgbevf_down(adapter);
3723 ixgbevf_free_irq(adapter);
3724 ixgbevf_free_all_tx_resources(adapter);
3725 ixgbevf_free_all_rx_resources(adapter);
3726 }
3727
3728 /**
3729 * ixgbevf_close - Disables a network interface
3730 * @netdev: network interface device structure
3731 *
3732 * Returns 0, this is not allowed to fail
3733 *
3734 * The close entry point is called when an interface is de-activated
3735 * by the OS. The hardware is still under the drivers control, but
3736 * needs to be disabled. A global MAC reset is issued to stop the
3737 * hardware, and all transmit and receive resources are freed.
3738 **/
ixgbevf_close(struct net_device * netdev)3739 int ixgbevf_close(struct net_device *netdev)
3740 {
3741 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3742
3743 if (netif_device_present(netdev))
3744 ixgbevf_close_suspend(adapter);
3745
3746 return 0;
3747 }
3748
ixgbevf_queue_reset_subtask(struct ixgbevf_adapter * adapter)3749 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3750 {
3751 struct net_device *dev = adapter->netdev;
3752
3753 if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
3754 &adapter->state))
3755 return;
3756
3757 /* if interface is down do nothing */
3758 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3759 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3760 return;
3761
3762 /* Hardware has to reinitialize queues and interrupts to
3763 * match packet buffer alignment. Unfortunately, the
3764 * hardware is not flexible enough to do this dynamically.
3765 */
3766 rtnl_lock();
3767
3768 if (netif_running(dev))
3769 ixgbevf_close(dev);
3770
3771 ixgbevf_clear_interrupt_scheme(adapter);
3772 ixgbevf_init_interrupt_scheme(adapter);
3773
3774 if (netif_running(dev))
3775 ixgbevf_open(dev);
3776
3777 rtnl_unlock();
3778 }
3779
ixgbevf_tx_ctxtdesc(struct ixgbevf_ring * tx_ring,u32 vlan_macip_lens,u32 fceof_saidx,u32 type_tucmd,u32 mss_l4len_idx)3780 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3781 u32 vlan_macip_lens, u32 fceof_saidx,
3782 u32 type_tucmd, u32 mss_l4len_idx)
3783 {
3784 struct ixgbe_adv_tx_context_desc *context_desc;
3785 u16 i = tx_ring->next_to_use;
3786
3787 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3788
3789 i++;
3790 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3791
3792 /* set bits to identify this as an advanced context descriptor */
3793 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3794
3795 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3796 context_desc->fceof_saidx = cpu_to_le32(fceof_saidx);
3797 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3798 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3799 }
3800
ixgbevf_tso(struct ixgbevf_ring * tx_ring,struct ixgbevf_tx_buffer * first,u8 * hdr_len,struct ixgbevf_ipsec_tx_data * itd)3801 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
3802 struct ixgbevf_tx_buffer *first,
3803 u8 *hdr_len,
3804 struct ixgbevf_ipsec_tx_data *itd)
3805 {
3806 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
3807 struct sk_buff *skb = first->skb;
3808 union {
3809 struct iphdr *v4;
3810 struct ipv6hdr *v6;
3811 unsigned char *hdr;
3812 } ip;
3813 union {
3814 struct tcphdr *tcp;
3815 unsigned char *hdr;
3816 } l4;
3817 u32 paylen, l4_offset;
3818 u32 fceof_saidx = 0;
3819 int err;
3820
3821 if (skb->ip_summed != CHECKSUM_PARTIAL)
3822 return 0;
3823
3824 if (!skb_is_gso(skb))
3825 return 0;
3826
3827 err = skb_cow_head(skb, 0);
3828 if (err < 0)
3829 return err;
3830
3831 if (eth_p_mpls(first->protocol))
3832 ip.hdr = skb_inner_network_header(skb);
3833 else
3834 ip.hdr = skb_network_header(skb);
3835 l4.hdr = skb_checksum_start(skb);
3836
3837 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3838 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3839
3840 /* initialize outer IP header fields */
3841 if (ip.v4->version == 4) {
3842 unsigned char *csum_start = skb_checksum_start(skb);
3843 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
3844 int len = csum_start - trans_start;
3845
3846 /* IP header will have to cancel out any data that
3847 * is not a part of the outer IP header, so set to
3848 * a reverse csum if needed, else init check to 0.
3849 */
3850 ip.v4->check = (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) ?
3851 csum_fold(csum_partial(trans_start,
3852 len, 0)) : 0;
3853 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3854
3855 ip.v4->tot_len = 0;
3856 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3857 IXGBE_TX_FLAGS_CSUM |
3858 IXGBE_TX_FLAGS_IPV4;
3859 } else {
3860 ip.v6->payload_len = 0;
3861 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3862 IXGBE_TX_FLAGS_CSUM;
3863 }
3864
3865 /* determine offset of inner transport header */
3866 l4_offset = l4.hdr - skb->data;
3867
3868 /* compute length of segmentation header */
3869 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
3870
3871 /* remove payload length from inner checksum */
3872 paylen = skb->len - l4_offset;
3873 csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
3874
3875 /* update gso size and bytecount with header size */
3876 first->gso_segs = skb_shinfo(skb)->gso_segs;
3877 first->bytecount += (first->gso_segs - 1) * *hdr_len;
3878
3879 /* mss_l4len_id: use 1 as index for TSO */
3880 mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
3881 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3882 mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
3883
3884 fceof_saidx |= itd->pfsa;
3885 type_tucmd |= itd->flags | itd->trailer_len;
3886
3887 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3888 vlan_macip_lens = l4.hdr - ip.hdr;
3889 vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
3890 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3891
3892 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, fceof_saidx, type_tucmd,
3893 mss_l4len_idx);
3894
3895 return 1;
3896 }
3897
ixgbevf_tx_csum(struct ixgbevf_ring * tx_ring,struct ixgbevf_tx_buffer * first,struct ixgbevf_ipsec_tx_data * itd)3898 static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3899 struct ixgbevf_tx_buffer *first,
3900 struct ixgbevf_ipsec_tx_data *itd)
3901 {
3902 struct sk_buff *skb = first->skb;
3903 u32 vlan_macip_lens = 0;
3904 u32 fceof_saidx = 0;
3905 u32 type_tucmd = 0;
3906
3907 if (skb->ip_summed != CHECKSUM_PARTIAL)
3908 goto no_csum;
3909
3910 switch (skb->csum_offset) {
3911 case offsetof(struct tcphdr, check):
3912 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3913 fallthrough;
3914 case offsetof(struct udphdr, check):
3915 break;
3916 case offsetof(struct sctphdr, checksum):
3917 /* validate that this is actually an SCTP request */
3918 if (skb_csum_is_sctp(skb)) {
3919 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3920 break;
3921 }
3922 fallthrough;
3923 default:
3924 skb_checksum_help(skb);
3925 goto no_csum;
3926 }
3927
3928 if (first->protocol == htons(ETH_P_IP))
3929 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3930
3931 /* update TX checksum flag */
3932 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
3933 vlan_macip_lens = skb_checksum_start_offset(skb) -
3934 skb_network_offset(skb);
3935 no_csum:
3936 /* vlan_macip_lens: MACLEN, VLAN tag */
3937 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
3938 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3939
3940 fceof_saidx |= itd->pfsa;
3941 type_tucmd |= itd->flags | itd->trailer_len;
3942
3943 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3944 fceof_saidx, type_tucmd, 0);
3945 }
3946
ixgbevf_tx_cmd_type(u32 tx_flags)3947 static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3948 {
3949 /* set type for advanced descriptor with frame checksum insertion */
3950 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3951 IXGBE_ADVTXD_DCMD_IFCS |
3952 IXGBE_ADVTXD_DCMD_DEXT);
3953
3954 /* set HW VLAN bit if VLAN is present */
3955 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3956 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3957
3958 /* set segmentation enable bits for TSO/FSO */
3959 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3960 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3961
3962 return cmd_type;
3963 }
3964
ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc * tx_desc,u32 tx_flags,unsigned int paylen)3965 static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3966 u32 tx_flags, unsigned int paylen)
3967 {
3968 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3969
3970 /* enable L4 checksum for TSO and TX checksum offload */
3971 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3972 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3973
3974 /* enble IPv4 checksum for TSO */
3975 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3976 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3977
3978 /* enable IPsec */
3979 if (tx_flags & IXGBE_TX_FLAGS_IPSEC)
3980 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IPSEC);
3981
3982 /* use index 1 context for TSO/FSO/FCOE/IPSEC */
3983 if (tx_flags & (IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_IPSEC))
3984 olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
3985
3986 /* Check Context must be set if Tx switch is enabled, which it
3987 * always is for case where virtual functions are running
3988 */
3989 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3990
3991 tx_desc->read.olinfo_status = olinfo_status;
3992 }
3993
ixgbevf_tx_map(struct ixgbevf_ring * tx_ring,struct ixgbevf_tx_buffer * first,const u8 hdr_len)3994 static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3995 struct ixgbevf_tx_buffer *first,
3996 const u8 hdr_len)
3997 {
3998 struct sk_buff *skb = first->skb;
3999 struct ixgbevf_tx_buffer *tx_buffer;
4000 union ixgbe_adv_tx_desc *tx_desc;
4001 skb_frag_t *frag;
4002 dma_addr_t dma;
4003 unsigned int data_len, size;
4004 u32 tx_flags = first->tx_flags;
4005 __le32 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
4006 u16 i = tx_ring->next_to_use;
4007
4008 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
4009
4010 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
4011
4012 size = skb_headlen(skb);
4013 data_len = skb->data_len;
4014
4015 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
4016
4017 tx_buffer = first;
4018
4019 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4020 if (dma_mapping_error(tx_ring->dev, dma))
4021 goto dma_error;
4022
4023 /* record length, and DMA address */
4024 dma_unmap_len_set(tx_buffer, len, size);
4025 dma_unmap_addr_set(tx_buffer, dma, dma);
4026
4027 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4028
4029 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
4030 tx_desc->read.cmd_type_len =
4031 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
4032
4033 i++;
4034 tx_desc++;
4035 if (i == tx_ring->count) {
4036 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
4037 i = 0;
4038 }
4039 tx_desc->read.olinfo_status = 0;
4040
4041 dma += IXGBE_MAX_DATA_PER_TXD;
4042 size -= IXGBE_MAX_DATA_PER_TXD;
4043
4044 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4045 }
4046
4047 if (likely(!data_len))
4048 break;
4049
4050 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
4051
4052 i++;
4053 tx_desc++;
4054 if (i == tx_ring->count) {
4055 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
4056 i = 0;
4057 }
4058 tx_desc->read.olinfo_status = 0;
4059
4060 size = skb_frag_size(frag);
4061 data_len -= size;
4062
4063 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
4064 DMA_TO_DEVICE);
4065
4066 tx_buffer = &tx_ring->tx_buffer_info[i];
4067 }
4068
4069 /* write last descriptor with RS and EOP bits */
4070 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
4071 tx_desc->read.cmd_type_len = cmd_type;
4072
4073 /* set the timestamp */
4074 first->time_stamp = jiffies;
4075
4076 skb_tx_timestamp(skb);
4077
4078 /* Force memory writes to complete before letting h/w know there
4079 * are new descriptors to fetch. (Only applicable for weak-ordered
4080 * memory model archs, such as IA-64).
4081 *
4082 * We also need this memory barrier (wmb) to make certain all of the
4083 * status bits have been updated before next_to_watch is written.
4084 */
4085 wmb();
4086
4087 /* set next_to_watch value indicating a packet is present */
4088 first->next_to_watch = tx_desc;
4089
4090 i++;
4091 if (i == tx_ring->count)
4092 i = 0;
4093
4094 tx_ring->next_to_use = i;
4095
4096 /* notify HW of packet */
4097 ixgbevf_write_tail(tx_ring, i);
4098
4099 return;
4100 dma_error:
4101 dev_err(tx_ring->dev, "TX DMA map failed\n");
4102 tx_buffer = &tx_ring->tx_buffer_info[i];
4103
4104 /* clear dma mappings for failed tx_buffer_info map */
4105 while (tx_buffer != first) {
4106 if (dma_unmap_len(tx_buffer, len))
4107 dma_unmap_page(tx_ring->dev,
4108 dma_unmap_addr(tx_buffer, dma),
4109 dma_unmap_len(tx_buffer, len),
4110 DMA_TO_DEVICE);
4111 dma_unmap_len_set(tx_buffer, len, 0);
4112
4113 if (i-- == 0)
4114 i += tx_ring->count;
4115 tx_buffer = &tx_ring->tx_buffer_info[i];
4116 }
4117
4118 if (dma_unmap_len(tx_buffer, len))
4119 dma_unmap_single(tx_ring->dev,
4120 dma_unmap_addr(tx_buffer, dma),
4121 dma_unmap_len(tx_buffer, len),
4122 DMA_TO_DEVICE);
4123 dma_unmap_len_set(tx_buffer, len, 0);
4124
4125 dev_kfree_skb_any(tx_buffer->skb);
4126 tx_buffer->skb = NULL;
4127
4128 tx_ring->next_to_use = i;
4129 }
4130
__ixgbevf_maybe_stop_tx(struct ixgbevf_ring * tx_ring,int size)4131 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4132 {
4133 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
4134 /* Herbert's original patch had:
4135 * smp_mb__after_netif_stop_queue();
4136 * but since that doesn't exist yet, just open code it.
4137 */
4138 smp_mb();
4139
4140 /* We need to check again in a case another CPU has just
4141 * made room available.
4142 */
4143 if (likely(ixgbevf_desc_unused(tx_ring) < size))
4144 return -EBUSY;
4145
4146 /* A reprieve! - use start_queue because it doesn't call schedule */
4147 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
4148 ++tx_ring->tx_stats.restart_queue;
4149
4150 return 0;
4151 }
4152
ixgbevf_maybe_stop_tx(struct ixgbevf_ring * tx_ring,int size)4153 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4154 {
4155 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
4156 return 0;
4157 return __ixgbevf_maybe_stop_tx(tx_ring, size);
4158 }
4159
ixgbevf_xmit_frame_ring(struct sk_buff * skb,struct ixgbevf_ring * tx_ring)4160 static int ixgbevf_xmit_frame_ring(struct sk_buff *skb,
4161 struct ixgbevf_ring *tx_ring)
4162 {
4163 struct ixgbevf_tx_buffer *first;
4164 int tso;
4165 u32 tx_flags = 0;
4166 u16 count = TXD_USE_COUNT(skb_headlen(skb));
4167 struct ixgbevf_ipsec_tx_data ipsec_tx = { 0 };
4168 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4169 unsigned short f;
4170 #endif
4171 u8 hdr_len = 0;
4172 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
4173
4174 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
4175 dev_kfree_skb_any(skb);
4176 return NETDEV_TX_OK;
4177 }
4178
4179 /* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
4180 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
4181 * + 2 desc gap to keep tail from touching head,
4182 * + 1 desc for context descriptor,
4183 * otherwise try next time
4184 */
4185 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4186 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
4187 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
4188
4189 count += TXD_USE_COUNT(skb_frag_size(frag));
4190 }
4191 #else
4192 count += skb_shinfo(skb)->nr_frags;
4193 #endif
4194 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
4195 tx_ring->tx_stats.tx_busy++;
4196 return NETDEV_TX_BUSY;
4197 }
4198
4199 /* record the location of the first descriptor for this packet */
4200 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4201 first->skb = skb;
4202 first->bytecount = skb->len;
4203 first->gso_segs = 1;
4204
4205 if (skb_vlan_tag_present(skb)) {
4206 tx_flags |= skb_vlan_tag_get(skb);
4207 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
4208 tx_flags |= IXGBE_TX_FLAGS_VLAN;
4209 }
4210
4211 /* record initial flags and protocol */
4212 first->tx_flags = tx_flags;
4213 first->protocol = vlan_get_protocol(skb);
4214
4215 #ifdef CONFIG_IXGBEVF_IPSEC
4216 if (xfrm_offload(skb) && !ixgbevf_ipsec_tx(tx_ring, first, &ipsec_tx))
4217 goto out_drop;
4218 #endif
4219 tso = ixgbevf_tso(tx_ring, first, &hdr_len, &ipsec_tx);
4220 if (tso < 0)
4221 goto out_drop;
4222 else if (!tso)
4223 ixgbevf_tx_csum(tx_ring, first, &ipsec_tx);
4224
4225 ixgbevf_tx_map(tx_ring, first, hdr_len);
4226
4227 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
4228
4229 return NETDEV_TX_OK;
4230
4231 out_drop:
4232 dev_kfree_skb_any(first->skb);
4233 first->skb = NULL;
4234
4235 return NETDEV_TX_OK;
4236 }
4237
ixgbevf_xmit_frame(struct sk_buff * skb,struct net_device * netdev)4238 static netdev_tx_t ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
4239 {
4240 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4241 struct ixgbevf_ring *tx_ring;
4242
4243 if (skb->len <= 0) {
4244 dev_kfree_skb_any(skb);
4245 return NETDEV_TX_OK;
4246 }
4247
4248 /* The minimum packet size for olinfo paylen is 17 so pad the skb
4249 * in order to meet this minimum size requirement.
4250 */
4251 if (skb->len < 17) {
4252 if (skb_padto(skb, 17))
4253 return NETDEV_TX_OK;
4254 skb->len = 17;
4255 }
4256
4257 tx_ring = adapter->tx_ring[skb->queue_mapping];
4258 return ixgbevf_xmit_frame_ring(skb, tx_ring);
4259 }
4260
4261 /**
4262 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
4263 * @netdev: network interface device structure
4264 * @p: pointer to an address structure
4265 *
4266 * Returns 0 on success, negative on failure
4267 **/
ixgbevf_set_mac(struct net_device * netdev,void * p)4268 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
4269 {
4270 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4271 struct ixgbe_hw *hw = &adapter->hw;
4272 struct sockaddr *addr = p;
4273 int err;
4274
4275 if (!is_valid_ether_addr(addr->sa_data))
4276 return -EADDRNOTAVAIL;
4277
4278 spin_lock_bh(&adapter->mbx_lock);
4279
4280 err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
4281
4282 spin_unlock_bh(&adapter->mbx_lock);
4283
4284 if (err)
4285 return -EPERM;
4286
4287 ether_addr_copy(hw->mac.addr, addr->sa_data);
4288 ether_addr_copy(hw->mac.perm_addr, addr->sa_data);
4289 eth_hw_addr_set(netdev, addr->sa_data);
4290
4291 return 0;
4292 }
4293
4294 /**
4295 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
4296 * @netdev: network interface device structure
4297 * @new_mtu: new value for maximum frame size
4298 *
4299 * Returns 0 on success, negative on failure
4300 **/
ixgbevf_change_mtu(struct net_device * netdev,int new_mtu)4301 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
4302 {
4303 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4304 struct ixgbe_hw *hw = &adapter->hw;
4305 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4306 int ret;
4307
4308 /* prevent MTU being changed to a size unsupported by XDP */
4309 if (adapter->xdp_prog) {
4310 dev_warn(&adapter->pdev->dev, "MTU cannot be changed while XDP program is loaded\n");
4311 return -EPERM;
4312 }
4313
4314 spin_lock_bh(&adapter->mbx_lock);
4315 /* notify the PF of our intent to use this size of frame */
4316 ret = hw->mac.ops.set_rlpml(hw, max_frame);
4317 spin_unlock_bh(&adapter->mbx_lock);
4318 if (ret)
4319 return -EINVAL;
4320
4321 hw_dbg(hw, "changing MTU from %d to %d\n",
4322 netdev->mtu, new_mtu);
4323
4324 /* must set new MTU before calling down or up */
4325 WRITE_ONCE(netdev->mtu, new_mtu);
4326
4327 if (netif_running(netdev))
4328 ixgbevf_reinit_locked(adapter);
4329
4330 return 0;
4331 }
4332
ixgbevf_suspend(struct device * dev_d)4333 static int ixgbevf_suspend(struct device *dev_d)
4334 {
4335 struct net_device *netdev = dev_get_drvdata(dev_d);
4336 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4337
4338 rtnl_lock();
4339 netif_device_detach(netdev);
4340
4341 if (netif_running(netdev))
4342 ixgbevf_close_suspend(adapter);
4343
4344 ixgbevf_clear_interrupt_scheme(adapter);
4345 rtnl_unlock();
4346
4347 return 0;
4348 }
4349
ixgbevf_resume(struct device * dev_d)4350 static int ixgbevf_resume(struct device *dev_d)
4351 {
4352 struct pci_dev *pdev = to_pci_dev(dev_d);
4353 struct net_device *netdev = pci_get_drvdata(pdev);
4354 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4355 int err;
4356
4357 adapter->hw.hw_addr = adapter->io_addr;
4358 smp_mb__before_atomic();
4359 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4360 pci_set_master(pdev);
4361
4362 ixgbevf_reset(adapter);
4363
4364 rtnl_lock();
4365 err = ixgbevf_init_interrupt_scheme(adapter);
4366 if (!err && netif_running(netdev))
4367 err = ixgbevf_open(netdev);
4368 rtnl_unlock();
4369 if (err)
4370 return err;
4371
4372 netif_device_attach(netdev);
4373
4374 return err;
4375 }
4376
ixgbevf_shutdown(struct pci_dev * pdev)4377 static void ixgbevf_shutdown(struct pci_dev *pdev)
4378 {
4379 ixgbevf_suspend(&pdev->dev);
4380 }
4381
ixgbevf_get_tx_ring_stats(struct rtnl_link_stats64 * stats,const struct ixgbevf_ring * ring)4382 static void ixgbevf_get_tx_ring_stats(struct rtnl_link_stats64 *stats,
4383 const struct ixgbevf_ring *ring)
4384 {
4385 u64 bytes, packets;
4386 unsigned int start;
4387
4388 if (ring) {
4389 do {
4390 start = u64_stats_fetch_begin(&ring->syncp);
4391 bytes = ring->stats.bytes;
4392 packets = ring->stats.packets;
4393 } while (u64_stats_fetch_retry(&ring->syncp, start));
4394 stats->tx_bytes += bytes;
4395 stats->tx_packets += packets;
4396 }
4397 }
4398
ixgbevf_get_stats(struct net_device * netdev,struct rtnl_link_stats64 * stats)4399 static void ixgbevf_get_stats(struct net_device *netdev,
4400 struct rtnl_link_stats64 *stats)
4401 {
4402 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4403 unsigned int start;
4404 u64 bytes, packets;
4405 const struct ixgbevf_ring *ring;
4406 int i;
4407
4408 ixgbevf_update_stats(adapter);
4409
4410 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
4411
4412 rcu_read_lock();
4413 for (i = 0; i < adapter->num_rx_queues; i++) {
4414 ring = adapter->rx_ring[i];
4415 do {
4416 start = u64_stats_fetch_begin(&ring->syncp);
4417 bytes = ring->stats.bytes;
4418 packets = ring->stats.packets;
4419 } while (u64_stats_fetch_retry(&ring->syncp, start));
4420 stats->rx_bytes += bytes;
4421 stats->rx_packets += packets;
4422 }
4423
4424 for (i = 0; i < adapter->num_tx_queues; i++) {
4425 ring = adapter->tx_ring[i];
4426 ixgbevf_get_tx_ring_stats(stats, ring);
4427 }
4428
4429 for (i = 0; i < adapter->num_xdp_queues; i++) {
4430 ring = adapter->xdp_ring[i];
4431 ixgbevf_get_tx_ring_stats(stats, ring);
4432 }
4433 rcu_read_unlock();
4434 }
4435
4436 #define IXGBEVF_MAX_MAC_HDR_LEN 127
4437 #define IXGBEVF_MAX_NETWORK_HDR_LEN 511
4438
4439 static netdev_features_t
ixgbevf_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)4440 ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
4441 netdev_features_t features)
4442 {
4443 unsigned int network_hdr_len, mac_hdr_len;
4444
4445 /* Make certain the headers can be described by a context descriptor */
4446 mac_hdr_len = skb_network_offset(skb);
4447 if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
4448 return features & ~(NETIF_F_HW_CSUM |
4449 NETIF_F_SCTP_CRC |
4450 NETIF_F_HW_VLAN_CTAG_TX |
4451 NETIF_F_TSO |
4452 NETIF_F_TSO6);
4453
4454 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4455 if (unlikely(network_hdr_len > IXGBEVF_MAX_NETWORK_HDR_LEN))
4456 return features & ~(NETIF_F_HW_CSUM |
4457 NETIF_F_SCTP_CRC |
4458 NETIF_F_TSO |
4459 NETIF_F_TSO6);
4460
4461 /* We can only support IPV4 TSO in tunnels if we can mangle the
4462 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4463 */
4464 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4465 features &= ~NETIF_F_TSO;
4466
4467 return features;
4468 }
4469
ixgbevf_xdp_setup(struct net_device * dev,struct bpf_prog * prog)4470 static int ixgbevf_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
4471 {
4472 int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4473 struct ixgbevf_adapter *adapter = netdev_priv(dev);
4474 struct bpf_prog *old_prog;
4475
4476 /* verify ixgbevf ring attributes are sufficient for XDP */
4477 for (i = 0; i < adapter->num_rx_queues; i++) {
4478 struct ixgbevf_ring *ring = adapter->rx_ring[i];
4479
4480 if (frame_size > ixgbevf_rx_bufsz(ring))
4481 return -EINVAL;
4482 }
4483
4484 old_prog = xchg(&adapter->xdp_prog, prog);
4485
4486 /* If transitioning XDP modes reconfigure rings */
4487 if (!!prog != !!old_prog) {
4488 /* Hardware has to reinitialize queues and interrupts to
4489 * match packet buffer alignment. Unfortunately, the
4490 * hardware is not flexible enough to do this dynamically.
4491 */
4492 if (netif_running(dev))
4493 ixgbevf_close(dev);
4494
4495 ixgbevf_clear_interrupt_scheme(adapter);
4496 ixgbevf_init_interrupt_scheme(adapter);
4497
4498 if (netif_running(dev))
4499 ixgbevf_open(dev);
4500 } else {
4501 for (i = 0; i < adapter->num_rx_queues; i++)
4502 xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
4503 }
4504
4505 if (old_prog)
4506 bpf_prog_put(old_prog);
4507
4508 return 0;
4509 }
4510
ixgbevf_xdp(struct net_device * dev,struct netdev_bpf * xdp)4511 static int ixgbevf_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4512 {
4513 switch (xdp->command) {
4514 case XDP_SETUP_PROG:
4515 return ixgbevf_xdp_setup(dev, xdp->prog);
4516 default:
4517 return -EINVAL;
4518 }
4519 }
4520
4521 static const struct net_device_ops ixgbevf_netdev_ops = {
4522 .ndo_open = ixgbevf_open,
4523 .ndo_stop = ixgbevf_close,
4524 .ndo_start_xmit = ixgbevf_xmit_frame,
4525 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
4526 .ndo_get_stats64 = ixgbevf_get_stats,
4527 .ndo_validate_addr = eth_validate_addr,
4528 .ndo_set_mac_address = ixgbevf_set_mac,
4529 .ndo_change_mtu = ixgbevf_change_mtu,
4530 .ndo_tx_timeout = ixgbevf_tx_timeout,
4531 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
4532 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
4533 .ndo_features_check = ixgbevf_features_check,
4534 .ndo_bpf = ixgbevf_xdp,
4535 };
4536
ixgbevf_assign_netdev_ops(struct net_device * dev)4537 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
4538 {
4539 dev->netdev_ops = &ixgbevf_netdev_ops;
4540 ixgbevf_set_ethtool_ops(dev);
4541 dev->watchdog_timeo = 5 * HZ;
4542 }
4543
4544 /**
4545 * ixgbevf_probe - Device Initialization Routine
4546 * @pdev: PCI device information struct
4547 * @ent: entry in ixgbevf_pci_tbl
4548 *
4549 * Returns 0 on success, negative on failure
4550 *
4551 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
4552 * The OS initialization, configuring of the adapter private structure,
4553 * and a hardware reset occur.
4554 **/
ixgbevf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)4555 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4556 {
4557 struct net_device *netdev;
4558 struct ixgbevf_adapter *adapter = NULL;
4559 struct ixgbe_hw *hw = NULL;
4560 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
4561 bool disable_dev = false;
4562 int err;
4563
4564 err = pci_enable_device(pdev);
4565 if (err)
4566 return err;
4567
4568 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4569 if (err) {
4570 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4571 goto err_dma;
4572 }
4573
4574 err = pci_request_regions(pdev, ixgbevf_driver_name);
4575 if (err) {
4576 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4577 goto err_pci_reg;
4578 }
4579
4580 pci_set_master(pdev);
4581
4582 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
4583 MAX_TX_QUEUES);
4584 if (!netdev) {
4585 err = -ENOMEM;
4586 goto err_alloc_etherdev;
4587 }
4588
4589 SET_NETDEV_DEV(netdev, &pdev->dev);
4590
4591 adapter = netdev_priv(netdev);
4592
4593 adapter->netdev = netdev;
4594 adapter->pdev = pdev;
4595 hw = &adapter->hw;
4596 hw->back = adapter;
4597 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4598
4599 /* call save state here in standalone driver because it relies on
4600 * adapter struct to exist, and needs to call netdev_priv
4601 */
4602 pci_save_state(pdev);
4603
4604 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4605 pci_resource_len(pdev, 0));
4606 adapter->io_addr = hw->hw_addr;
4607 if (!hw->hw_addr) {
4608 err = -EIO;
4609 goto err_ioremap;
4610 }
4611
4612 ixgbevf_assign_netdev_ops(netdev);
4613
4614 /* Setup HW API */
4615 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4616 hw->mac.type = ii->mac;
4617
4618 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops_legacy,
4619 sizeof(struct ixgbe_mbx_operations));
4620
4621 /* setup the private structure */
4622 err = ixgbevf_sw_init(adapter);
4623 if (err)
4624 goto err_sw_init;
4625
4626 /* The HW MAC address was set and/or determined in sw_init */
4627 if (!is_valid_ether_addr(netdev->dev_addr)) {
4628 pr_err("invalid MAC address\n");
4629 err = -EIO;
4630 goto err_sw_init;
4631 }
4632
4633 netdev->hw_features = NETIF_F_SG |
4634 NETIF_F_TSO |
4635 NETIF_F_TSO6 |
4636 NETIF_F_RXCSUM |
4637 NETIF_F_HW_CSUM |
4638 NETIF_F_SCTP_CRC;
4639
4640 #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
4641 NETIF_F_GSO_GRE_CSUM | \
4642 NETIF_F_GSO_IPXIP4 | \
4643 NETIF_F_GSO_IPXIP6 | \
4644 NETIF_F_GSO_UDP_TUNNEL | \
4645 NETIF_F_GSO_UDP_TUNNEL_CSUM)
4646
4647 netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
4648 netdev->hw_features |= NETIF_F_GSO_PARTIAL |
4649 IXGBEVF_GSO_PARTIAL_FEATURES;
4650
4651 netdev->features = netdev->hw_features | NETIF_F_HIGHDMA;
4652
4653 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
4654 netdev->mpls_features |= NETIF_F_SG |
4655 NETIF_F_TSO |
4656 NETIF_F_TSO6 |
4657 NETIF_F_HW_CSUM;
4658 netdev->mpls_features |= IXGBEVF_GSO_PARTIAL_FEATURES;
4659 netdev->hw_enc_features |= netdev->vlan_features;
4660
4661 /* set this bit last since it cannot be part of vlan_features */
4662 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4663 NETIF_F_HW_VLAN_CTAG_RX |
4664 NETIF_F_HW_VLAN_CTAG_TX;
4665
4666 netdev->priv_flags |= IFF_UNICAST_FLT;
4667 netdev->xdp_features = NETDEV_XDP_ACT_BASIC;
4668
4669 /* MTU range: 68 - 1504 or 9710 */
4670 netdev->min_mtu = ETH_MIN_MTU;
4671 switch (adapter->hw.api_version) {
4672 case ixgbe_mbox_api_11:
4673 case ixgbe_mbox_api_12:
4674 case ixgbe_mbox_api_13:
4675 case ixgbe_mbox_api_14:
4676 case ixgbe_mbox_api_15:
4677 case ixgbe_mbox_api_16:
4678 case ixgbe_mbox_api_17:
4679 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4680 (ETH_HLEN + ETH_FCS_LEN);
4681 break;
4682 default:
4683 if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
4684 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4685 (ETH_HLEN + ETH_FCS_LEN);
4686 else
4687 netdev->max_mtu = ETH_DATA_LEN + ETH_FCS_LEN;
4688 break;
4689 }
4690
4691 if (IXGBE_REMOVED(hw->hw_addr)) {
4692 err = -EIO;
4693 goto err_sw_init;
4694 }
4695
4696 timer_setup(&adapter->service_timer, ixgbevf_service_timer, 0);
4697
4698 INIT_WORK(&adapter->service_task, ixgbevf_service_task);
4699 set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
4700 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
4701
4702 err = ixgbevf_init_interrupt_scheme(adapter);
4703 if (err)
4704 goto err_sw_init;
4705
4706 strcpy(netdev->name, "eth%d");
4707
4708 err = register_netdev(netdev);
4709 if (err)
4710 goto err_register;
4711
4712 pci_set_drvdata(pdev, netdev);
4713 netif_carrier_off(netdev);
4714 ixgbevf_init_ipsec_offload(adapter);
4715
4716 ixgbevf_init_last_counter_stats(adapter);
4717
4718 /* print the VF info */
4719 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
4720 dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
4721
4722 switch (hw->mac.type) {
4723 case ixgbe_mac_X550_vf:
4724 dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
4725 break;
4726 case ixgbe_mac_X540_vf:
4727 dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
4728 break;
4729 case ixgbe_mac_e610_vf:
4730 dev_info(&pdev->dev, "Intel(R) E610 Virtual Function\n");
4731 break;
4732 case ixgbe_mac_82599_vf:
4733 default:
4734 dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
4735 break;
4736 }
4737
4738 return 0;
4739
4740 err_register:
4741 ixgbevf_clear_interrupt_scheme(adapter);
4742 err_sw_init:
4743 ixgbevf_reset_interrupt_capability(adapter);
4744 iounmap(adapter->io_addr);
4745 kfree(adapter->rss_key);
4746 err_ioremap:
4747 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4748 free_netdev(netdev);
4749 err_alloc_etherdev:
4750 pci_release_regions(pdev);
4751 err_pci_reg:
4752 err_dma:
4753 if (!adapter || disable_dev)
4754 pci_disable_device(pdev);
4755 return err;
4756 }
4757
4758 /**
4759 * ixgbevf_remove - Device Removal Routine
4760 * @pdev: PCI device information struct
4761 *
4762 * ixgbevf_remove is called by the PCI subsystem to alert the driver
4763 * that it should release a PCI device. The could be caused by a
4764 * Hot-Plug event, or because the driver is going to be removed from
4765 * memory.
4766 **/
ixgbevf_remove(struct pci_dev * pdev)4767 static void ixgbevf_remove(struct pci_dev *pdev)
4768 {
4769 struct net_device *netdev = pci_get_drvdata(pdev);
4770 struct ixgbevf_adapter *adapter;
4771 bool disable_dev;
4772
4773 if (!netdev)
4774 return;
4775
4776 adapter = netdev_priv(netdev);
4777
4778 set_bit(__IXGBEVF_REMOVING, &adapter->state);
4779 cancel_work_sync(&adapter->service_task);
4780
4781 if (netdev->reg_state == NETREG_REGISTERED)
4782 unregister_netdev(netdev);
4783
4784 ixgbevf_stop_ipsec_offload(adapter);
4785 ixgbevf_clear_interrupt_scheme(adapter);
4786 ixgbevf_reset_interrupt_capability(adapter);
4787
4788 iounmap(adapter->io_addr);
4789 pci_release_regions(pdev);
4790
4791 hw_dbg(&adapter->hw, "Remove complete\n");
4792
4793 kfree(adapter->rss_key);
4794 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4795 free_netdev(netdev);
4796
4797 if (disable_dev)
4798 pci_disable_device(pdev);
4799 }
4800
4801 /**
4802 * ixgbevf_io_error_detected - called when PCI error is detected
4803 * @pdev: Pointer to PCI device
4804 * @state: The current pci connection state
4805 *
4806 * This function is called after a PCI bus error affecting
4807 * this device has been detected.
4808 **/
ixgbevf_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)4809 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
4810 pci_channel_state_t state)
4811 {
4812 struct net_device *netdev = pci_get_drvdata(pdev);
4813 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4814
4815 if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
4816 return PCI_ERS_RESULT_DISCONNECT;
4817
4818 rtnl_lock();
4819 netif_device_detach(netdev);
4820
4821 if (netif_running(netdev))
4822 ixgbevf_close_suspend(adapter);
4823
4824 if (state == pci_channel_io_perm_failure) {
4825 rtnl_unlock();
4826 return PCI_ERS_RESULT_DISCONNECT;
4827 }
4828
4829 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4830 pci_disable_device(pdev);
4831 rtnl_unlock();
4832
4833 /* Request a slot reset. */
4834 return PCI_ERS_RESULT_NEED_RESET;
4835 }
4836
4837 /**
4838 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
4839 * @pdev: Pointer to PCI device
4840 *
4841 * Restart the card from scratch, as if from a cold-boot. Implementation
4842 * resembles the first-half of the ixgbevf_resume routine.
4843 **/
ixgbevf_io_slot_reset(struct pci_dev * pdev)4844 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
4845 {
4846 struct net_device *netdev = pci_get_drvdata(pdev);
4847 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4848
4849 if (pci_enable_device_mem(pdev)) {
4850 dev_err(&pdev->dev,
4851 "Cannot re-enable PCI device after reset.\n");
4852 return PCI_ERS_RESULT_DISCONNECT;
4853 }
4854
4855 adapter->hw.hw_addr = adapter->io_addr;
4856 smp_mb__before_atomic();
4857 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4858 pci_set_master(pdev);
4859
4860 ixgbevf_reset(adapter);
4861
4862 return PCI_ERS_RESULT_RECOVERED;
4863 }
4864
4865 /**
4866 * ixgbevf_io_resume - called when traffic can start flowing again.
4867 * @pdev: Pointer to PCI device
4868 *
4869 * This callback is called when the error recovery driver tells us that
4870 * its OK to resume normal operation. Implementation resembles the
4871 * second-half of the ixgbevf_resume routine.
4872 **/
ixgbevf_io_resume(struct pci_dev * pdev)4873 static void ixgbevf_io_resume(struct pci_dev *pdev)
4874 {
4875 struct net_device *netdev = pci_get_drvdata(pdev);
4876
4877 rtnl_lock();
4878 if (netif_running(netdev))
4879 ixgbevf_open(netdev);
4880
4881 netif_device_attach(netdev);
4882 rtnl_unlock();
4883 }
4884
4885 /* PCI Error Recovery (ERS) */
4886 static const struct pci_error_handlers ixgbevf_err_handler = {
4887 .error_detected = ixgbevf_io_error_detected,
4888 .slot_reset = ixgbevf_io_slot_reset,
4889 .resume = ixgbevf_io_resume,
4890 };
4891
4892 static DEFINE_SIMPLE_DEV_PM_OPS(ixgbevf_pm_ops, ixgbevf_suspend, ixgbevf_resume);
4893
4894 static struct pci_driver ixgbevf_driver = {
4895 .name = ixgbevf_driver_name,
4896 .id_table = ixgbevf_pci_tbl,
4897 .probe = ixgbevf_probe,
4898 .remove = ixgbevf_remove,
4899
4900 /* Power Management Hooks */
4901 .driver.pm = pm_sleep_ptr(&ixgbevf_pm_ops),
4902
4903 .shutdown = ixgbevf_shutdown,
4904 .err_handler = &ixgbevf_err_handler
4905 };
4906
4907 /**
4908 * ixgbevf_init_module - Driver Registration Routine
4909 *
4910 * ixgbevf_init_module is the first routine called when the driver is
4911 * loaded. All it does is register with the PCI subsystem.
4912 **/
ixgbevf_init_module(void)4913 static int __init ixgbevf_init_module(void)
4914 {
4915 int err;
4916
4917 pr_info("%s\n", ixgbevf_driver_string);
4918 pr_info("%s\n", ixgbevf_copyright);
4919 ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
4920 if (!ixgbevf_wq) {
4921 pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
4922 return -ENOMEM;
4923 }
4924
4925 err = pci_register_driver(&ixgbevf_driver);
4926 if (err) {
4927 destroy_workqueue(ixgbevf_wq);
4928 return err;
4929 }
4930
4931 return 0;
4932 }
4933
4934 module_init(ixgbevf_init_module);
4935
4936 /**
4937 * ixgbevf_exit_module - Driver Exit Cleanup Routine
4938 *
4939 * ixgbevf_exit_module is called just before the driver is removed
4940 * from memory.
4941 **/
ixgbevf_exit_module(void)4942 static void __exit ixgbevf_exit_module(void)
4943 {
4944 pci_unregister_driver(&ixgbevf_driver);
4945 if (ixgbevf_wq) {
4946 destroy_workqueue(ixgbevf_wq);
4947 ixgbevf_wq = NULL;
4948 }
4949 }
4950
4951 #ifdef DEBUG
4952 /**
4953 * ixgbevf_get_hw_dev_name - return device name string
4954 * used by hardware layer to print debugging information
4955 * @hw: pointer to private hardware struct
4956 **/
ixgbevf_get_hw_dev_name(struct ixgbe_hw * hw)4957 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4958 {
4959 struct ixgbevf_adapter *adapter = hw->back;
4960
4961 return adapter->netdev->name;
4962 }
4963
4964 #endif
4965 module_exit(ixgbevf_exit_module);
4966
4967 /* ixgbevf_main.c */
4968