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