]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c
ixgbevf: Adds function to set PSRTYPE register
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / intel / ixgbevf / ixgbevf_main.c
1 /*******************************************************************************
2
3 Intel 82599 Virtual Function driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26 *******************************************************************************/
27
28
29 /******************************************************************************
30 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
31 ******************************************************************************/
32
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #include <linux/types.h>
36 #include <linux/bitops.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/netdevice.h>
40 #include <linux/vmalloc.h>
41 #include <linux/string.h>
42 #include <linux/in.h>
43 #include <linux/ip.h>
44 #include <linux/tcp.h>
45 #include <linux/sctp.h>
46 #include <linux/ipv6.h>
47 #include <linux/slab.h>
48 #include <net/checksum.h>
49 #include <net/ip6_checksum.h>
50 #include <linux/ethtool.h>
51 #include <linux/if.h>
52 #include <linux/if_vlan.h>
53 #include <linux/prefetch.h>
54
55 #include "ixgbevf.h"
56
57 const char ixgbevf_driver_name[] = "ixgbevf";
58 static const char ixgbevf_driver_string[] =
59 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
60
61 #define DRV_VERSION "2.7.12-k"
62 const char ixgbevf_driver_version[] = DRV_VERSION;
63 static char ixgbevf_copyright[] =
64 "Copyright (c) 2009 - 2012 Intel Corporation.";
65
66 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
67 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
69 };
70
71 /* ixgbevf_pci_tbl - PCI Device ID Table
72 *
73 * Wildcard entries (PCI_ANY_ID) should come last
74 * Last entry must be all 0s
75 *
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
78 */
79 static DEFINE_PCI_DEVICE_TABLE(ixgbevf_pci_tbl) = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
82 /* required last entry */
83 {0, }
84 };
85 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
86
87 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
88 MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(DRV_VERSION);
91
92 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
93 static int debug = -1;
94 module_param(debug, int, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
96
97 /* forward decls */
98 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
99 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
100
101 static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
102 struct ixgbevf_ring *rx_ring,
103 u32 val)
104 {
105 /*
106 * Force memory writes to complete before letting h/w
107 * know there are new descriptors to fetch. (Only
108 * applicable for weak-ordered memory model archs,
109 * such as IA-64).
110 */
111 wmb();
112 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
113 }
114
115 /**
116 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
117 * @adapter: pointer to adapter struct
118 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
119 * @queue: queue to map the corresponding interrupt to
120 * @msix_vector: the vector to map to the corresponding queue
121 */
122 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
123 u8 queue, u8 msix_vector)
124 {
125 u32 ivar, index;
126 struct ixgbe_hw *hw = &adapter->hw;
127 if (direction == -1) {
128 /* other causes */
129 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
130 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
131 ivar &= ~0xFF;
132 ivar |= msix_vector;
133 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
134 } else {
135 /* tx or rx causes */
136 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
137 index = ((16 * (queue & 1)) + (8 * direction));
138 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
139 ivar &= ~(0xFF << index);
140 ivar |= (msix_vector << index);
141 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
142 }
143 }
144
145 static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
146 struct ixgbevf_tx_buffer
147 *tx_buffer_info)
148 {
149 if (tx_buffer_info->dma) {
150 if (tx_buffer_info->mapped_as_page)
151 dma_unmap_page(tx_ring->dev,
152 tx_buffer_info->dma,
153 tx_buffer_info->length,
154 DMA_TO_DEVICE);
155 else
156 dma_unmap_single(tx_ring->dev,
157 tx_buffer_info->dma,
158 tx_buffer_info->length,
159 DMA_TO_DEVICE);
160 tx_buffer_info->dma = 0;
161 }
162 if (tx_buffer_info->skb) {
163 dev_kfree_skb_any(tx_buffer_info->skb);
164 tx_buffer_info->skb = NULL;
165 }
166 tx_buffer_info->time_stamp = 0;
167 /* tx_buffer_info must be completely set up in the transmit path */
168 }
169
170 #define IXGBE_MAX_TXD_PWR 14
171 #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
172
173 /* Tx Descriptors needed, worst case */
174 #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
175 #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
176
177 static void ixgbevf_tx_timeout(struct net_device *netdev);
178
179 /**
180 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
181 * @q_vector: board private structure
182 * @tx_ring: tx ring to clean
183 **/
184 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
185 struct ixgbevf_ring *tx_ring)
186 {
187 struct ixgbevf_adapter *adapter = q_vector->adapter;
188 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
189 struct ixgbevf_tx_buffer *tx_buffer_info;
190 unsigned int i, count = 0;
191 unsigned int total_bytes = 0, total_packets = 0;
192
193 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
194 return true;
195
196 i = tx_ring->next_to_clean;
197 tx_buffer_info = &tx_ring->tx_buffer_info[i];
198 eop_desc = tx_buffer_info->next_to_watch;
199
200 do {
201 bool cleaned = false;
202
203 /* if next_to_watch is not set then there is no work pending */
204 if (!eop_desc)
205 break;
206
207 /* prevent any other reads prior to eop_desc */
208 read_barrier_depends();
209
210 /* if DD is not set pending work has not been completed */
211 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
212 break;
213
214 /* clear next_to_watch to prevent false hangs */
215 tx_buffer_info->next_to_watch = NULL;
216
217 for ( ; !cleaned; count++) {
218 struct sk_buff *skb;
219 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
220 cleaned = (tx_desc == eop_desc);
221 skb = tx_buffer_info->skb;
222
223 if (cleaned && skb) {
224 unsigned int segs, bytecount;
225
226 /* gso_segs is currently only valid for tcp */
227 segs = skb_shinfo(skb)->gso_segs ?: 1;
228 /* multiply data chunks by size of headers */
229 bytecount = ((segs - 1) * skb_headlen(skb)) +
230 skb->len;
231 total_packets += segs;
232 total_bytes += bytecount;
233 }
234
235 ixgbevf_unmap_and_free_tx_resource(tx_ring,
236 tx_buffer_info);
237
238 tx_desc->wb.status = 0;
239
240 i++;
241 if (i == tx_ring->count)
242 i = 0;
243
244 tx_buffer_info = &tx_ring->tx_buffer_info[i];
245 }
246
247 eop_desc = tx_buffer_info->next_to_watch;
248 } while (count < tx_ring->count);
249
250 tx_ring->next_to_clean = i;
251
252 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
253 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
254 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
255 /* Make sure that anybody stopping the queue after this
256 * sees the new next_to_clean.
257 */
258 smp_mb();
259 if (__netif_subqueue_stopped(tx_ring->netdev,
260 tx_ring->queue_index) &&
261 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
262 netif_wake_subqueue(tx_ring->netdev,
263 tx_ring->queue_index);
264 ++adapter->restart_queue;
265 }
266 }
267
268 u64_stats_update_begin(&tx_ring->syncp);
269 tx_ring->total_bytes += total_bytes;
270 tx_ring->total_packets += total_packets;
271 u64_stats_update_end(&tx_ring->syncp);
272 q_vector->tx.total_bytes += total_bytes;
273 q_vector->tx.total_packets += total_packets;
274
275 return count < tx_ring->count;
276 }
277
278 /**
279 * ixgbevf_receive_skb - Send a completed packet up the stack
280 * @q_vector: structure containing interrupt and ring information
281 * @skb: packet to send up
282 * @status: hardware indication of status of receive
283 * @rx_desc: rx descriptor
284 **/
285 static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
286 struct sk_buff *skb, u8 status,
287 union ixgbe_adv_rx_desc *rx_desc)
288 {
289 struct ixgbevf_adapter *adapter = q_vector->adapter;
290 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
291 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
292
293 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
294 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
295
296 if (!(adapter->flags & IXGBE_FLAG_IN_NETPOLL))
297 napi_gro_receive(&q_vector->napi, skb);
298 else
299 netif_rx(skb);
300 }
301
302 /**
303 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
304 * @ring: pointer to Rx descriptor ring structure
305 * @status_err: hardware indication of status of receive
306 * @skb: skb currently being received and modified
307 **/
308 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
309 u32 status_err, struct sk_buff *skb)
310 {
311 skb_checksum_none_assert(skb);
312
313 /* Rx csum disabled */
314 if (!(ring->netdev->features & NETIF_F_RXCSUM))
315 return;
316
317 /* if IP and error */
318 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
319 (status_err & IXGBE_RXDADV_ERR_IPE)) {
320 ring->hw_csum_rx_error++;
321 return;
322 }
323
324 if (!(status_err & IXGBE_RXD_STAT_L4CS))
325 return;
326
327 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
328 ring->hw_csum_rx_error++;
329 return;
330 }
331
332 /* It must be a TCP or UDP packet with a valid checksum */
333 skb->ip_summed = CHECKSUM_UNNECESSARY;
334 ring->hw_csum_rx_good++;
335 }
336
337 /**
338 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
339 * @adapter: address of board private structure
340 **/
341 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
342 struct ixgbevf_ring *rx_ring,
343 int cleaned_count)
344 {
345 struct pci_dev *pdev = adapter->pdev;
346 union ixgbe_adv_rx_desc *rx_desc;
347 struct ixgbevf_rx_buffer *bi;
348 unsigned int i = rx_ring->next_to_use;
349
350 bi = &rx_ring->rx_buffer_info[i];
351
352 while (cleaned_count--) {
353 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
354
355 if (!bi->skb) {
356 struct sk_buff *skb;
357
358 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
359 rx_ring->rx_buf_len);
360 if (!skb) {
361 adapter->alloc_rx_buff_failed++;
362 goto no_buffers;
363 }
364 bi->skb = skb;
365
366 bi->dma = dma_map_single(&pdev->dev, skb->data,
367 rx_ring->rx_buf_len,
368 DMA_FROM_DEVICE);
369 if (dma_mapping_error(&pdev->dev, bi->dma)) {
370 dev_kfree_skb(skb);
371 bi->skb = NULL;
372 dev_err(&pdev->dev, "RX DMA map failed\n");
373 break;
374 }
375 }
376 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
377
378 i++;
379 if (i == rx_ring->count)
380 i = 0;
381 bi = &rx_ring->rx_buffer_info[i];
382 }
383
384 no_buffers:
385 if (rx_ring->next_to_use != i) {
386 rx_ring->next_to_use = i;
387 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
388 }
389 }
390
391 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
392 u32 qmask)
393 {
394 struct ixgbe_hw *hw = &adapter->hw;
395
396 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
397 }
398
399 static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
400 struct ixgbevf_ring *rx_ring,
401 int budget)
402 {
403 struct ixgbevf_adapter *adapter = q_vector->adapter;
404 struct pci_dev *pdev = adapter->pdev;
405 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
406 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
407 struct sk_buff *skb;
408 unsigned int i;
409 u32 len, staterr;
410 int cleaned_count = 0;
411 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
412
413 i = rx_ring->next_to_clean;
414 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
415 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
416 rx_buffer_info = &rx_ring->rx_buffer_info[i];
417
418 while (staterr & IXGBE_RXD_STAT_DD) {
419 if (!budget)
420 break;
421 budget--;
422
423 rmb(); /* read descriptor and rx_buffer_info after status DD */
424 len = le16_to_cpu(rx_desc->wb.upper.length);
425 skb = rx_buffer_info->skb;
426 prefetch(skb->data - NET_IP_ALIGN);
427 rx_buffer_info->skb = NULL;
428
429 if (rx_buffer_info->dma) {
430 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
431 rx_ring->rx_buf_len,
432 DMA_FROM_DEVICE);
433 rx_buffer_info->dma = 0;
434 skb_put(skb, len);
435 }
436
437 i++;
438 if (i == rx_ring->count)
439 i = 0;
440
441 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
442 prefetch(next_rxd);
443 cleaned_count++;
444
445 next_buffer = &rx_ring->rx_buffer_info[i];
446
447 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
448 skb->next = next_buffer->skb;
449 IXGBE_CB(skb->next)->prev = skb;
450 adapter->non_eop_descs++;
451 goto next_desc;
452 }
453
454 /* we should not be chaining buffers, if we did drop the skb */
455 if (IXGBE_CB(skb)->prev) {
456 do {
457 struct sk_buff *this = skb;
458 skb = IXGBE_CB(skb)->prev;
459 dev_kfree_skb(this);
460 } while (skb);
461 goto next_desc;
462 }
463
464 /* ERR_MASK will only have valid bits if EOP set */
465 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
466 dev_kfree_skb_irq(skb);
467 goto next_desc;
468 }
469
470 ixgbevf_rx_checksum(rx_ring, staterr, skb);
471
472 /* probably a little skewed due to removing CRC */
473 total_rx_bytes += skb->len;
474 total_rx_packets++;
475
476 /*
477 * Work around issue of some types of VM to VM loop back
478 * packets not getting split correctly
479 */
480 if (staterr & IXGBE_RXD_STAT_LB) {
481 u32 header_fixup_len = skb_headlen(skb);
482 if (header_fixup_len < 14)
483 skb_push(skb, header_fixup_len);
484 }
485 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
486
487 /* Workaround hardware that can't do proper VEPA multicast
488 * source pruning.
489 */
490 if ((skb->pkt_type & (PACKET_BROADCAST | PACKET_MULTICAST)) &&
491 ether_addr_equal(adapter->netdev->dev_addr,
492 eth_hdr(skb)->h_source)) {
493 dev_kfree_skb_irq(skb);
494 goto next_desc;
495 }
496
497 ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
498
499 next_desc:
500 rx_desc->wb.upper.status_error = 0;
501
502 /* return some buffers to hardware, one at a time is too slow */
503 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
504 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
505 cleaned_count);
506 cleaned_count = 0;
507 }
508
509 /* use prefetched values */
510 rx_desc = next_rxd;
511 rx_buffer_info = &rx_ring->rx_buffer_info[i];
512
513 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
514 }
515
516 rx_ring->next_to_clean = i;
517 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
518
519 if (cleaned_count)
520 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
521
522 u64_stats_update_begin(&rx_ring->syncp);
523 rx_ring->total_packets += total_rx_packets;
524 rx_ring->total_bytes += total_rx_bytes;
525 u64_stats_update_end(&rx_ring->syncp);
526 q_vector->rx.total_packets += total_rx_packets;
527 q_vector->rx.total_bytes += total_rx_bytes;
528
529 return !!budget;
530 }
531
532 /**
533 * ixgbevf_poll - NAPI polling calback
534 * @napi: napi struct with our devices info in it
535 * @budget: amount of work driver is allowed to do this pass, in packets
536 *
537 * This function will clean more than one or more rings associated with a
538 * q_vector.
539 **/
540 static int ixgbevf_poll(struct napi_struct *napi, int budget)
541 {
542 struct ixgbevf_q_vector *q_vector =
543 container_of(napi, struct ixgbevf_q_vector, napi);
544 struct ixgbevf_adapter *adapter = q_vector->adapter;
545 struct ixgbevf_ring *ring;
546 int per_ring_budget;
547 bool clean_complete = true;
548
549 ixgbevf_for_each_ring(ring, q_vector->tx)
550 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
551
552 /* attempt to distribute budget to each queue fairly, but don't allow
553 * the budget to go below 1 because we'll exit polling */
554 if (q_vector->rx.count > 1)
555 per_ring_budget = max(budget/q_vector->rx.count, 1);
556 else
557 per_ring_budget = budget;
558
559 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
560 ixgbevf_for_each_ring(ring, q_vector->rx)
561 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
562 per_ring_budget);
563 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
564
565 /* If all work not completed, return budget and keep polling */
566 if (!clean_complete)
567 return budget;
568 /* all work done, exit the polling mode */
569 napi_complete(napi);
570 if (adapter->rx_itr_setting & 1)
571 ixgbevf_set_itr(q_vector);
572 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
573 ixgbevf_irq_enable_queues(adapter,
574 1 << q_vector->v_idx);
575
576 return 0;
577 }
578
579 /**
580 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
581 * @q_vector: structure containing interrupt and ring information
582 */
583 static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
584 {
585 struct ixgbevf_adapter *adapter = q_vector->adapter;
586 struct ixgbe_hw *hw = &adapter->hw;
587 int v_idx = q_vector->v_idx;
588 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
589
590 /*
591 * set the WDIS bit to not clear the timer bits and cause an
592 * immediate assertion of the interrupt
593 */
594 itr_reg |= IXGBE_EITR_CNT_WDIS;
595
596 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
597 }
598
599 /**
600 * ixgbevf_configure_msix - Configure MSI-X hardware
601 * @adapter: board private structure
602 *
603 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
604 * interrupts.
605 **/
606 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
607 {
608 struct ixgbevf_q_vector *q_vector;
609 int q_vectors, v_idx;
610
611 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
612 adapter->eims_enable_mask = 0;
613
614 /*
615 * Populate the IVAR table and set the ITR values to the
616 * corresponding register.
617 */
618 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
619 struct ixgbevf_ring *ring;
620 q_vector = adapter->q_vector[v_idx];
621
622 ixgbevf_for_each_ring(ring, q_vector->rx)
623 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
624
625 ixgbevf_for_each_ring(ring, q_vector->tx)
626 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
627
628 if (q_vector->tx.ring && !q_vector->rx.ring) {
629 /* tx only vector */
630 if (adapter->tx_itr_setting == 1)
631 q_vector->itr = IXGBE_10K_ITR;
632 else
633 q_vector->itr = adapter->tx_itr_setting;
634 } else {
635 /* rx or rx/tx vector */
636 if (adapter->rx_itr_setting == 1)
637 q_vector->itr = IXGBE_20K_ITR;
638 else
639 q_vector->itr = adapter->rx_itr_setting;
640 }
641
642 /* add q_vector eims value to global eims_enable_mask */
643 adapter->eims_enable_mask |= 1 << v_idx;
644
645 ixgbevf_write_eitr(q_vector);
646 }
647
648 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
649 /* setup eims_other and add value to global eims_enable_mask */
650 adapter->eims_other = 1 << v_idx;
651 adapter->eims_enable_mask |= adapter->eims_other;
652 }
653
654 enum latency_range {
655 lowest_latency = 0,
656 low_latency = 1,
657 bulk_latency = 2,
658 latency_invalid = 255
659 };
660
661 /**
662 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
663 * @q_vector: structure containing interrupt and ring information
664 * @ring_container: structure containing ring performance data
665 *
666 * Stores a new ITR value based on packets and byte
667 * counts during the last interrupt. The advantage of per interrupt
668 * computation is faster updates and more accurate ITR for the current
669 * traffic pattern. Constants in this function were computed
670 * based on theoretical maximum wire speed and thresholds were set based
671 * on testing data as well as attempting to minimize response time
672 * while increasing bulk throughput.
673 **/
674 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
675 struct ixgbevf_ring_container *ring_container)
676 {
677 int bytes = ring_container->total_bytes;
678 int packets = ring_container->total_packets;
679 u32 timepassed_us;
680 u64 bytes_perint;
681 u8 itr_setting = ring_container->itr;
682
683 if (packets == 0)
684 return;
685
686 /* simple throttlerate management
687 * 0-20MB/s lowest (100000 ints/s)
688 * 20-100MB/s low (20000 ints/s)
689 * 100-1249MB/s bulk (8000 ints/s)
690 */
691 /* what was last interrupt timeslice? */
692 timepassed_us = q_vector->itr >> 2;
693 bytes_perint = bytes / timepassed_us; /* bytes/usec */
694
695 switch (itr_setting) {
696 case lowest_latency:
697 if (bytes_perint > 10)
698 itr_setting = low_latency;
699 break;
700 case low_latency:
701 if (bytes_perint > 20)
702 itr_setting = bulk_latency;
703 else if (bytes_perint <= 10)
704 itr_setting = lowest_latency;
705 break;
706 case bulk_latency:
707 if (bytes_perint <= 20)
708 itr_setting = low_latency;
709 break;
710 }
711
712 /* clear work counters since we have the values we need */
713 ring_container->total_bytes = 0;
714 ring_container->total_packets = 0;
715
716 /* write updated itr to ring container */
717 ring_container->itr = itr_setting;
718 }
719
720 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
721 {
722 u32 new_itr = q_vector->itr;
723 u8 current_itr;
724
725 ixgbevf_update_itr(q_vector, &q_vector->tx);
726 ixgbevf_update_itr(q_vector, &q_vector->rx);
727
728 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
729
730 switch (current_itr) {
731 /* counts and packets in update_itr are dependent on these numbers */
732 case lowest_latency:
733 new_itr = IXGBE_100K_ITR;
734 break;
735 case low_latency:
736 new_itr = IXGBE_20K_ITR;
737 break;
738 case bulk_latency:
739 default:
740 new_itr = IXGBE_8K_ITR;
741 break;
742 }
743
744 if (new_itr != q_vector->itr) {
745 /* do an exponential smoothing */
746 new_itr = (10 * new_itr * q_vector->itr) /
747 ((9 * new_itr) + q_vector->itr);
748
749 /* save the algorithm value here */
750 q_vector->itr = new_itr;
751
752 ixgbevf_write_eitr(q_vector);
753 }
754 }
755
756 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
757 {
758 struct ixgbevf_adapter *adapter = data;
759 struct ixgbe_hw *hw = &adapter->hw;
760
761 hw->mac.get_link_status = 1;
762
763 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
764 mod_timer(&adapter->watchdog_timer, jiffies);
765
766 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
767
768 return IRQ_HANDLED;
769 }
770
771 /**
772 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
773 * @irq: unused
774 * @data: pointer to our q_vector struct for this interrupt vector
775 **/
776 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
777 {
778 struct ixgbevf_q_vector *q_vector = data;
779
780 /* EIAM disabled interrupts (on this vector) for us */
781 if (q_vector->rx.ring || q_vector->tx.ring)
782 napi_schedule(&q_vector->napi);
783
784 return IRQ_HANDLED;
785 }
786
787 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
788 int r_idx)
789 {
790 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
791
792 a->rx_ring[r_idx].next = q_vector->rx.ring;
793 q_vector->rx.ring = &a->rx_ring[r_idx];
794 q_vector->rx.count++;
795 }
796
797 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
798 int t_idx)
799 {
800 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
801
802 a->tx_ring[t_idx].next = q_vector->tx.ring;
803 q_vector->tx.ring = &a->tx_ring[t_idx];
804 q_vector->tx.count++;
805 }
806
807 /**
808 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
809 * @adapter: board private structure to initialize
810 *
811 * This function maps descriptor rings to the queue-specific vectors
812 * we were allotted through the MSI-X enabling code. Ideally, we'd have
813 * one vector per ring/queue, but on a constrained vector budget, we
814 * group the rings as "efficiently" as possible. You would add new
815 * mapping configurations in here.
816 **/
817 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
818 {
819 int q_vectors;
820 int v_start = 0;
821 int rxr_idx = 0, txr_idx = 0;
822 int rxr_remaining = adapter->num_rx_queues;
823 int txr_remaining = adapter->num_tx_queues;
824 int i, j;
825 int rqpv, tqpv;
826 int err = 0;
827
828 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
829
830 /*
831 * The ideal configuration...
832 * We have enough vectors to map one per queue.
833 */
834 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
835 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
836 map_vector_to_rxq(adapter, v_start, rxr_idx);
837
838 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
839 map_vector_to_txq(adapter, v_start, txr_idx);
840 goto out;
841 }
842
843 /*
844 * If we don't have enough vectors for a 1-to-1
845 * mapping, we'll have to group them so there are
846 * multiple queues per vector.
847 */
848 /* Re-adjusting *qpv takes care of the remainder. */
849 for (i = v_start; i < q_vectors; i++) {
850 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
851 for (j = 0; j < rqpv; j++) {
852 map_vector_to_rxq(adapter, i, rxr_idx);
853 rxr_idx++;
854 rxr_remaining--;
855 }
856 }
857 for (i = v_start; i < q_vectors; i++) {
858 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
859 for (j = 0; j < tqpv; j++) {
860 map_vector_to_txq(adapter, i, txr_idx);
861 txr_idx++;
862 txr_remaining--;
863 }
864 }
865
866 out:
867 return err;
868 }
869
870 /**
871 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
872 * @adapter: board private structure
873 *
874 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
875 * interrupts from the kernel.
876 **/
877 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
878 {
879 struct net_device *netdev = adapter->netdev;
880 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
881 int vector, err;
882 int ri = 0, ti = 0;
883
884 for (vector = 0; vector < q_vectors; vector++) {
885 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
886 struct msix_entry *entry = &adapter->msix_entries[vector];
887
888 if (q_vector->tx.ring && q_vector->rx.ring) {
889 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
890 "%s-%s-%d", netdev->name, "TxRx", ri++);
891 ti++;
892 } else if (q_vector->rx.ring) {
893 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
894 "%s-%s-%d", netdev->name, "rx", ri++);
895 } else if (q_vector->tx.ring) {
896 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
897 "%s-%s-%d", netdev->name, "tx", ti++);
898 } else {
899 /* skip this unused q_vector */
900 continue;
901 }
902 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
903 q_vector->name, q_vector);
904 if (err) {
905 hw_dbg(&adapter->hw,
906 "request_irq failed for MSIX interrupt "
907 "Error: %d\n", err);
908 goto free_queue_irqs;
909 }
910 }
911
912 err = request_irq(adapter->msix_entries[vector].vector,
913 &ixgbevf_msix_other, 0, netdev->name, adapter);
914 if (err) {
915 hw_dbg(&adapter->hw,
916 "request_irq for msix_other failed: %d\n", err);
917 goto free_queue_irqs;
918 }
919
920 return 0;
921
922 free_queue_irqs:
923 while (vector) {
924 vector--;
925 free_irq(adapter->msix_entries[vector].vector,
926 adapter->q_vector[vector]);
927 }
928 /* This failure is non-recoverable - it indicates the system is
929 * out of MSIX vector resources and the VF driver cannot run
930 * without them. Set the number of msix vectors to zero
931 * indicating that not enough can be allocated. The error
932 * will be returned to the user indicating device open failed.
933 * Any further attempts to force the driver to open will also
934 * fail. The only way to recover is to unload the driver and
935 * reload it again. If the system has recovered some MSIX
936 * vectors then it may succeed.
937 */
938 adapter->num_msix_vectors = 0;
939 return err;
940 }
941
942 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
943 {
944 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
945
946 for (i = 0; i < q_vectors; i++) {
947 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
948 q_vector->rx.ring = NULL;
949 q_vector->tx.ring = NULL;
950 q_vector->rx.count = 0;
951 q_vector->tx.count = 0;
952 }
953 }
954
955 /**
956 * ixgbevf_request_irq - initialize interrupts
957 * @adapter: board private structure
958 *
959 * Attempts to configure interrupts using the best available
960 * capabilities of the hardware and kernel.
961 **/
962 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
963 {
964 int err = 0;
965
966 err = ixgbevf_request_msix_irqs(adapter);
967
968 if (err)
969 hw_dbg(&adapter->hw,
970 "request_irq failed, Error %d\n", err);
971
972 return err;
973 }
974
975 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
976 {
977 int i, q_vectors;
978
979 q_vectors = adapter->num_msix_vectors;
980 i = q_vectors - 1;
981
982 free_irq(adapter->msix_entries[i].vector, adapter);
983 i--;
984
985 for (; i >= 0; i--) {
986 /* free only the irqs that were actually requested */
987 if (!adapter->q_vector[i]->rx.ring &&
988 !adapter->q_vector[i]->tx.ring)
989 continue;
990
991 free_irq(adapter->msix_entries[i].vector,
992 adapter->q_vector[i]);
993 }
994
995 ixgbevf_reset_q_vectors(adapter);
996 }
997
998 /**
999 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1000 * @adapter: board private structure
1001 **/
1002 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1003 {
1004 struct ixgbe_hw *hw = &adapter->hw;
1005 int i;
1006
1007 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1008 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1009 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1010
1011 IXGBE_WRITE_FLUSH(hw);
1012
1013 for (i = 0; i < adapter->num_msix_vectors; i++)
1014 synchronize_irq(adapter->msix_entries[i].vector);
1015 }
1016
1017 /**
1018 * ixgbevf_irq_enable - Enable default interrupt generation settings
1019 * @adapter: board private structure
1020 **/
1021 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1022 {
1023 struct ixgbe_hw *hw = &adapter->hw;
1024
1025 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1026 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1027 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1028 }
1029
1030 /**
1031 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1032 * @adapter: board private structure
1033 *
1034 * Configure the Tx unit of the MAC after a reset.
1035 **/
1036 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1037 {
1038 u64 tdba;
1039 struct ixgbe_hw *hw = &adapter->hw;
1040 u32 i, j, tdlen, txctrl;
1041
1042 /* Setup the HW Tx Head and Tail descriptor pointers */
1043 for (i = 0; i < adapter->num_tx_queues; i++) {
1044 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1045 j = ring->reg_idx;
1046 tdba = ring->dma;
1047 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1048 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1049 (tdba & DMA_BIT_MASK(32)));
1050 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1051 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1052 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1053 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1054 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1055 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1056 /* Disable Tx Head Writeback RO bit, since this hoses
1057 * bookkeeping if things aren't delivered in order.
1058 */
1059 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1060 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1061 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1062 }
1063 }
1064
1065 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1066
1067 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1068 {
1069 struct ixgbevf_ring *rx_ring;
1070 struct ixgbe_hw *hw = &adapter->hw;
1071 u32 srrctl;
1072
1073 rx_ring = &adapter->rx_ring[index];
1074
1075 srrctl = IXGBE_SRRCTL_DROP_EN;
1076
1077 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1078
1079 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1080 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1081
1082 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1083 }
1084
1085 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1086 {
1087 struct ixgbe_hw *hw = &adapter->hw;
1088
1089 /* PSRTYPE must be initialized in 82599 */
1090 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1091 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1092 IXGBE_PSRTYPE_L2HDR;
1093
1094 if (adapter->num_rx_queues > 1)
1095 psrtype |= 1 << 29;
1096
1097 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1098 }
1099
1100 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1101 {
1102 struct ixgbe_hw *hw = &adapter->hw;
1103 struct net_device *netdev = adapter->netdev;
1104 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1105 int i;
1106 u16 rx_buf_len;
1107
1108 /* notify the PF of our intent to use this size of frame */
1109 ixgbevf_rlpml_set_vf(hw, max_frame);
1110
1111 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1112 max_frame += VLAN_HLEN;
1113
1114 /*
1115 * Allocate buffer sizes that fit well into 32K and
1116 * take into account max frame size of 9.5K
1117 */
1118 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1119 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1120 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1121 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1122 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1123 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1124 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1125 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1126 rx_buf_len = IXGBEVF_RXBUFFER_8K;
1127 else
1128 rx_buf_len = IXGBEVF_RXBUFFER_10K;
1129
1130 for (i = 0; i < adapter->num_rx_queues; i++)
1131 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1132 }
1133
1134 /**
1135 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1136 * @adapter: board private structure
1137 *
1138 * Configure the Rx unit of the MAC after a reset.
1139 **/
1140 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1141 {
1142 u64 rdba;
1143 struct ixgbe_hw *hw = &adapter->hw;
1144 int i, j;
1145 u32 rdlen;
1146
1147 ixgbevf_setup_psrtype(adapter);
1148
1149 /* set_rx_buffer_len must be called before ring initialization */
1150 ixgbevf_set_rx_buffer_len(adapter);
1151
1152 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1153 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1154 * the Base and Length of the Rx Descriptor Ring */
1155 for (i = 0; i < adapter->num_rx_queues; i++) {
1156 rdba = adapter->rx_ring[i].dma;
1157 j = adapter->rx_ring[i].reg_idx;
1158 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1159 (rdba & DMA_BIT_MASK(32)));
1160 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1161 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1162 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1163 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1164 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1165 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
1166
1167 ixgbevf_configure_srrctl(adapter, j);
1168 }
1169 }
1170
1171 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1172 __be16 proto, u16 vid)
1173 {
1174 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1175 struct ixgbe_hw *hw = &adapter->hw;
1176 int err;
1177
1178 spin_lock_bh(&adapter->mbx_lock);
1179
1180 /* add VID to filter table */
1181 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1182
1183 spin_unlock_bh(&adapter->mbx_lock);
1184
1185 /* translate error return types so error makes sense */
1186 if (err == IXGBE_ERR_MBX)
1187 return -EIO;
1188
1189 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1190 return -EACCES;
1191
1192 set_bit(vid, adapter->active_vlans);
1193
1194 return err;
1195 }
1196
1197 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1198 __be16 proto, u16 vid)
1199 {
1200 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1201 struct ixgbe_hw *hw = &adapter->hw;
1202 int err = -EOPNOTSUPP;
1203
1204 spin_lock_bh(&adapter->mbx_lock);
1205
1206 /* remove VID from filter table */
1207 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1208
1209 spin_unlock_bh(&adapter->mbx_lock);
1210
1211 clear_bit(vid, adapter->active_vlans);
1212
1213 return err;
1214 }
1215
1216 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1217 {
1218 u16 vid;
1219
1220 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1221 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1222 htons(ETH_P_8021Q), vid);
1223 }
1224
1225 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1226 {
1227 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1228 struct ixgbe_hw *hw = &adapter->hw;
1229 int count = 0;
1230
1231 if ((netdev_uc_count(netdev)) > 10) {
1232 pr_err("Too many unicast filters - No Space\n");
1233 return -ENOSPC;
1234 }
1235
1236 if (!netdev_uc_empty(netdev)) {
1237 struct netdev_hw_addr *ha;
1238 netdev_for_each_uc_addr(ha, netdev) {
1239 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1240 udelay(200);
1241 }
1242 } else {
1243 /*
1244 * If the list is empty then send message to PF driver to
1245 * clear all macvlans on this VF.
1246 */
1247 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1248 }
1249
1250 return count;
1251 }
1252
1253 /**
1254 * ixgbevf_set_rx_mode - Multicast and unicast set
1255 * @netdev: network interface device structure
1256 *
1257 * The set_rx_method entry point is called whenever the multicast address
1258 * list, unicast address list or the network interface flags are updated.
1259 * This routine is responsible for configuring the hardware for proper
1260 * multicast mode and configuring requested unicast filters.
1261 **/
1262 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1263 {
1264 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1265 struct ixgbe_hw *hw = &adapter->hw;
1266
1267 spin_lock_bh(&adapter->mbx_lock);
1268
1269 /* reprogram multicast list */
1270 hw->mac.ops.update_mc_addr_list(hw, netdev);
1271
1272 ixgbevf_write_uc_addr_list(netdev);
1273
1274 spin_unlock_bh(&adapter->mbx_lock);
1275 }
1276
1277 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1278 {
1279 int q_idx;
1280 struct ixgbevf_q_vector *q_vector;
1281 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1282
1283 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1284 q_vector = adapter->q_vector[q_idx];
1285 napi_enable(&q_vector->napi);
1286 }
1287 }
1288
1289 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1290 {
1291 int q_idx;
1292 struct ixgbevf_q_vector *q_vector;
1293 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1294
1295 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1296 q_vector = adapter->q_vector[q_idx];
1297 napi_disable(&q_vector->napi);
1298 }
1299 }
1300
1301 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1302 {
1303 struct net_device *netdev = adapter->netdev;
1304 int i;
1305
1306 ixgbevf_set_rx_mode(netdev);
1307
1308 ixgbevf_restore_vlan(adapter);
1309
1310 ixgbevf_configure_tx(adapter);
1311 ixgbevf_configure_rx(adapter);
1312 for (i = 0; i < adapter->num_rx_queues; i++) {
1313 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
1314 ixgbevf_alloc_rx_buffers(adapter, ring,
1315 IXGBE_DESC_UNUSED(ring));
1316 }
1317 }
1318
1319 #define IXGBEVF_MAX_RX_DESC_POLL 10
1320 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1321 int rxr)
1322 {
1323 struct ixgbe_hw *hw = &adapter->hw;
1324 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1325 u32 rxdctl;
1326 int j = adapter->rx_ring[rxr].reg_idx;
1327
1328 do {
1329 usleep_range(1000, 2000);
1330 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1331 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1332
1333 if (!wait_loop)
1334 hw_dbg(hw, "RXDCTL.ENABLE queue %d not set while polling\n",
1335 rxr);
1336
1337 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1338 (adapter->rx_ring[rxr].count - 1));
1339 }
1340
1341 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1342 struct ixgbevf_ring *ring)
1343 {
1344 struct ixgbe_hw *hw = &adapter->hw;
1345 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1346 u32 rxdctl;
1347 u8 reg_idx = ring->reg_idx;
1348
1349 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1350 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1351
1352 /* write value back with RXDCTL.ENABLE bit cleared */
1353 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1354
1355 /* the hardware may take up to 100us to really disable the rx queue */
1356 do {
1357 udelay(10);
1358 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1359 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1360
1361 if (!wait_loop)
1362 hw_dbg(hw, "RXDCTL.ENABLE queue %d not cleared while polling\n",
1363 reg_idx);
1364 }
1365
1366 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1367 {
1368 /* Only save pre-reset stats if there are some */
1369 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1370 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1371 adapter->stats.base_vfgprc;
1372 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1373 adapter->stats.base_vfgptc;
1374 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1375 adapter->stats.base_vfgorc;
1376 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1377 adapter->stats.base_vfgotc;
1378 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1379 adapter->stats.base_vfmprc;
1380 }
1381 }
1382
1383 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1384 {
1385 struct ixgbe_hw *hw = &adapter->hw;
1386
1387 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1388 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1389 adapter->stats.last_vfgorc |=
1390 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1391 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1392 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1393 adapter->stats.last_vfgotc |=
1394 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1395 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1396
1397 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1398 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1399 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1400 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1401 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1402 }
1403
1404 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1405 {
1406 struct ixgbe_hw *hw = &adapter->hw;
1407 int api[] = { ixgbe_mbox_api_11,
1408 ixgbe_mbox_api_10,
1409 ixgbe_mbox_api_unknown };
1410 int err = 0, idx = 0;
1411
1412 spin_lock_bh(&adapter->mbx_lock);
1413
1414 while (api[idx] != ixgbe_mbox_api_unknown) {
1415 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1416 if (!err)
1417 break;
1418 idx++;
1419 }
1420
1421 spin_unlock_bh(&adapter->mbx_lock);
1422 }
1423
1424 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
1425 {
1426 struct net_device *netdev = adapter->netdev;
1427 struct ixgbe_hw *hw = &adapter->hw;
1428 int i, j = 0;
1429 int num_rx_rings = adapter->num_rx_queues;
1430 u32 txdctl, rxdctl;
1431
1432 for (i = 0; i < adapter->num_tx_queues; i++) {
1433 j = adapter->tx_ring[i].reg_idx;
1434 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1435 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1436 txdctl |= (8 << 16);
1437 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1438 }
1439
1440 for (i = 0; i < adapter->num_tx_queues; i++) {
1441 j = adapter->tx_ring[i].reg_idx;
1442 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1443 txdctl |= IXGBE_TXDCTL_ENABLE;
1444 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1445 }
1446
1447 for (i = 0; i < num_rx_rings; i++) {
1448 j = adapter->rx_ring[i].reg_idx;
1449 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1450 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1451 if (hw->mac.type == ixgbe_mac_X540_vf) {
1452 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1453 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1454 IXGBE_RXDCTL_RLPML_EN);
1455 }
1456 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1457 ixgbevf_rx_desc_queue_enable(adapter, i);
1458 }
1459
1460 ixgbevf_configure_msix(adapter);
1461
1462 spin_lock_bh(&adapter->mbx_lock);
1463
1464 if (is_valid_ether_addr(hw->mac.addr))
1465 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1466 else
1467 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1468
1469 spin_unlock_bh(&adapter->mbx_lock);
1470
1471 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1472 ixgbevf_napi_enable_all(adapter);
1473
1474 /* enable transmits */
1475 netif_tx_start_all_queues(netdev);
1476
1477 ixgbevf_save_reset_stats(adapter);
1478 ixgbevf_init_last_counter_stats(adapter);
1479
1480 hw->mac.get_link_status = 1;
1481 mod_timer(&adapter->watchdog_timer, jiffies);
1482 }
1483
1484 static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
1485 {
1486 struct ixgbe_hw *hw = &adapter->hw;
1487 struct ixgbevf_ring *rx_ring;
1488 unsigned int def_q = 0;
1489 unsigned int num_tcs = 0;
1490 unsigned int num_rx_queues = 1;
1491 int err, i;
1492
1493 spin_lock_bh(&adapter->mbx_lock);
1494
1495 /* fetch queue configuration from the PF */
1496 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1497
1498 spin_unlock_bh(&adapter->mbx_lock);
1499
1500 if (err)
1501 return err;
1502
1503 if (num_tcs > 1) {
1504 /* update default Tx ring register index */
1505 adapter->tx_ring[0].reg_idx = def_q;
1506
1507 /* we need as many queues as traffic classes */
1508 num_rx_queues = num_tcs;
1509 }
1510
1511 /* nothing to do if we have the correct number of queues */
1512 if (adapter->num_rx_queues == num_rx_queues)
1513 return 0;
1514
1515 /* allocate new rings */
1516 rx_ring = kcalloc(num_rx_queues,
1517 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1518 if (!rx_ring)
1519 return -ENOMEM;
1520
1521 /* setup ring fields */
1522 for (i = 0; i < num_rx_queues; i++) {
1523 rx_ring[i].count = adapter->rx_ring_count;
1524 rx_ring[i].queue_index = i;
1525 rx_ring[i].reg_idx = i;
1526 rx_ring[i].dev = &adapter->pdev->dev;
1527 rx_ring[i].netdev = adapter->netdev;
1528
1529 /* allocate resources on the ring */
1530 err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
1531 if (err) {
1532 while (i) {
1533 i--;
1534 ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
1535 }
1536 kfree(rx_ring);
1537 return err;
1538 }
1539 }
1540
1541 /* free the existing rings and queues */
1542 ixgbevf_free_all_rx_resources(adapter);
1543 adapter->num_rx_queues = 0;
1544 kfree(adapter->rx_ring);
1545
1546 /* move new rings into position on the adapter struct */
1547 adapter->rx_ring = rx_ring;
1548 adapter->num_rx_queues = num_rx_queues;
1549
1550 /* reset ring to vector mapping */
1551 ixgbevf_reset_q_vectors(adapter);
1552 ixgbevf_map_rings_to_vectors(adapter);
1553
1554 return 0;
1555 }
1556
1557 void ixgbevf_up(struct ixgbevf_adapter *adapter)
1558 {
1559 struct ixgbe_hw *hw = &adapter->hw;
1560
1561 ixgbevf_reset_queues(adapter);
1562
1563 ixgbevf_configure(adapter);
1564
1565 ixgbevf_up_complete(adapter);
1566
1567 /* clear any pending interrupts, may auto mask */
1568 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1569
1570 ixgbevf_irq_enable(adapter);
1571 }
1572
1573 /**
1574 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1575 * @adapter: board private structure
1576 * @rx_ring: ring to free buffers from
1577 **/
1578 static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1579 struct ixgbevf_ring *rx_ring)
1580 {
1581 struct pci_dev *pdev = adapter->pdev;
1582 unsigned long size;
1583 unsigned int i;
1584
1585 if (!rx_ring->rx_buffer_info)
1586 return;
1587
1588 /* Free all the Rx ring sk_buffs */
1589 for (i = 0; i < rx_ring->count; i++) {
1590 struct ixgbevf_rx_buffer *rx_buffer_info;
1591
1592 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1593 if (rx_buffer_info->dma) {
1594 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
1595 rx_ring->rx_buf_len,
1596 DMA_FROM_DEVICE);
1597 rx_buffer_info->dma = 0;
1598 }
1599 if (rx_buffer_info->skb) {
1600 struct sk_buff *skb = rx_buffer_info->skb;
1601 rx_buffer_info->skb = NULL;
1602 do {
1603 struct sk_buff *this = skb;
1604 skb = IXGBE_CB(skb)->prev;
1605 dev_kfree_skb(this);
1606 } while (skb);
1607 }
1608 }
1609
1610 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1611 memset(rx_ring->rx_buffer_info, 0, size);
1612
1613 /* Zero out the descriptor ring */
1614 memset(rx_ring->desc, 0, rx_ring->size);
1615
1616 rx_ring->next_to_clean = 0;
1617 rx_ring->next_to_use = 0;
1618
1619 if (rx_ring->head)
1620 writel(0, adapter->hw.hw_addr + rx_ring->head);
1621 if (rx_ring->tail)
1622 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1623 }
1624
1625 /**
1626 * ixgbevf_clean_tx_ring - Free Tx Buffers
1627 * @adapter: board private structure
1628 * @tx_ring: ring to be cleaned
1629 **/
1630 static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1631 struct ixgbevf_ring *tx_ring)
1632 {
1633 struct ixgbevf_tx_buffer *tx_buffer_info;
1634 unsigned long size;
1635 unsigned int i;
1636
1637 if (!tx_ring->tx_buffer_info)
1638 return;
1639
1640 /* Free all the Tx ring sk_buffs */
1641 for (i = 0; i < tx_ring->count; i++) {
1642 tx_buffer_info = &tx_ring->tx_buffer_info[i];
1643 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
1644 }
1645
1646 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1647 memset(tx_ring->tx_buffer_info, 0, size);
1648
1649 memset(tx_ring->desc, 0, tx_ring->size);
1650
1651 tx_ring->next_to_use = 0;
1652 tx_ring->next_to_clean = 0;
1653
1654 if (tx_ring->head)
1655 writel(0, adapter->hw.hw_addr + tx_ring->head);
1656 if (tx_ring->tail)
1657 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1658 }
1659
1660 /**
1661 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1662 * @adapter: board private structure
1663 **/
1664 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1665 {
1666 int i;
1667
1668 for (i = 0; i < adapter->num_rx_queues; i++)
1669 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1670 }
1671
1672 /**
1673 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1674 * @adapter: board private structure
1675 **/
1676 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1677 {
1678 int i;
1679
1680 for (i = 0; i < adapter->num_tx_queues; i++)
1681 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1682 }
1683
1684 void ixgbevf_down(struct ixgbevf_adapter *adapter)
1685 {
1686 struct net_device *netdev = adapter->netdev;
1687 struct ixgbe_hw *hw = &adapter->hw;
1688 u32 txdctl;
1689 int i, j;
1690
1691 /* signal that we are down to the interrupt handler */
1692 set_bit(__IXGBEVF_DOWN, &adapter->state);
1693
1694 /* disable all enabled rx queues */
1695 for (i = 0; i < adapter->num_rx_queues; i++)
1696 ixgbevf_disable_rx_queue(adapter, &adapter->rx_ring[i]);
1697
1698 netif_tx_disable(netdev);
1699
1700 msleep(10);
1701
1702 netif_tx_stop_all_queues(netdev);
1703
1704 ixgbevf_irq_disable(adapter);
1705
1706 ixgbevf_napi_disable_all(adapter);
1707
1708 del_timer_sync(&adapter->watchdog_timer);
1709 /* can't call flush scheduled work here because it can deadlock
1710 * if linkwatch_event tries to acquire the rtnl_lock which we are
1711 * holding */
1712 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1713 msleep(1);
1714
1715 /* disable transmits in the hardware now that interrupts are off */
1716 for (i = 0; i < adapter->num_tx_queues; i++) {
1717 j = adapter->tx_ring[i].reg_idx;
1718 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1719 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1720 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1721 }
1722
1723 netif_carrier_off(netdev);
1724
1725 if (!pci_channel_offline(adapter->pdev))
1726 ixgbevf_reset(adapter);
1727
1728 ixgbevf_clean_all_tx_rings(adapter);
1729 ixgbevf_clean_all_rx_rings(adapter);
1730 }
1731
1732 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1733 {
1734 WARN_ON(in_interrupt());
1735
1736 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1737 msleep(1);
1738
1739 ixgbevf_down(adapter);
1740 ixgbevf_up(adapter);
1741
1742 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1743 }
1744
1745 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1746 {
1747 struct ixgbe_hw *hw = &adapter->hw;
1748 struct net_device *netdev = adapter->netdev;
1749
1750 if (hw->mac.ops.reset_hw(hw)) {
1751 hw_dbg(hw, "PF still resetting\n");
1752 } else {
1753 hw->mac.ops.init_hw(hw);
1754 ixgbevf_negotiate_api(adapter);
1755 }
1756
1757 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1758 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1759 netdev->addr_len);
1760 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1761 netdev->addr_len);
1762 }
1763 }
1764
1765 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1766 int vectors)
1767 {
1768 int err = 0;
1769 int vector_threshold;
1770
1771 /* We'll want at least 2 (vector_threshold):
1772 * 1) TxQ[0] + RxQ[0] handler
1773 * 2) Other (Link Status Change, etc.)
1774 */
1775 vector_threshold = MIN_MSIX_COUNT;
1776
1777 /* The more we get, the more we will assign to Tx/Rx Cleanup
1778 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1779 * Right now, we simply care about how many we'll get; we'll
1780 * set them up later while requesting irq's.
1781 */
1782 while (vectors >= vector_threshold) {
1783 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1784 vectors);
1785 if (!err || err < 0) /* Success or a nasty failure. */
1786 break;
1787 else /* err == number of vectors we should try again with */
1788 vectors = err;
1789 }
1790
1791 if (vectors < vector_threshold)
1792 err = -ENOMEM;
1793
1794 if (err) {
1795 dev_err(&adapter->pdev->dev,
1796 "Unable to allocate MSI-X interrupts\n");
1797 kfree(adapter->msix_entries);
1798 adapter->msix_entries = NULL;
1799 } else {
1800 /*
1801 * Adjust for only the vectors we'll use, which is minimum
1802 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1803 * vectors we were allocated.
1804 */
1805 adapter->num_msix_vectors = vectors;
1806 }
1807
1808 return err;
1809 }
1810
1811 /**
1812 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
1813 * @adapter: board private structure to initialize
1814 *
1815 * This is the top level queue allocation routine. The order here is very
1816 * important, starting with the "most" number of features turned on at once,
1817 * and ending with the smallest set of features. This way large combinations
1818 * can be allocated if they're turned on, and smaller combinations are the
1819 * fallthrough conditions.
1820 *
1821 **/
1822 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1823 {
1824 /* Start with base case */
1825 adapter->num_rx_queues = 1;
1826 adapter->num_tx_queues = 1;
1827 }
1828
1829 /**
1830 * ixgbevf_alloc_queues - Allocate memory for all rings
1831 * @adapter: board private structure to initialize
1832 *
1833 * We allocate one ring per queue at run-time since we don't know the
1834 * number of queues at compile-time. The polling_netdev array is
1835 * intended for Multiqueue, but should work fine with a single queue.
1836 **/
1837 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1838 {
1839 int i;
1840
1841 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1842 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1843 if (!adapter->tx_ring)
1844 goto err_tx_ring_allocation;
1845
1846 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1847 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1848 if (!adapter->rx_ring)
1849 goto err_rx_ring_allocation;
1850
1851 for (i = 0; i < adapter->num_tx_queues; i++) {
1852 adapter->tx_ring[i].count = adapter->tx_ring_count;
1853 adapter->tx_ring[i].queue_index = i;
1854 /* reg_idx may be remapped later by DCB config */
1855 adapter->tx_ring[i].reg_idx = i;
1856 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1857 adapter->tx_ring[i].netdev = adapter->netdev;
1858 }
1859
1860 for (i = 0; i < adapter->num_rx_queues; i++) {
1861 adapter->rx_ring[i].count = adapter->rx_ring_count;
1862 adapter->rx_ring[i].queue_index = i;
1863 adapter->rx_ring[i].reg_idx = i;
1864 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1865 adapter->rx_ring[i].netdev = adapter->netdev;
1866 }
1867
1868 return 0;
1869
1870 err_rx_ring_allocation:
1871 kfree(adapter->tx_ring);
1872 err_tx_ring_allocation:
1873 return -ENOMEM;
1874 }
1875
1876 /**
1877 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1878 * @adapter: board private structure to initialize
1879 *
1880 * Attempt to configure the interrupts using the best available
1881 * capabilities of the hardware and the kernel.
1882 **/
1883 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1884 {
1885 struct net_device *netdev = adapter->netdev;
1886 int err = 0;
1887 int vector, v_budget;
1888
1889 /*
1890 * It's easy to be greedy for MSI-X vectors, but it really
1891 * doesn't do us much good if we have a lot more vectors
1892 * than CPU's. So let's be conservative and only ask for
1893 * (roughly) the same number of vectors as there are CPU's.
1894 * The default is to use pairs of vectors.
1895 */
1896 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1897 v_budget = min_t(int, v_budget, num_online_cpus());
1898 v_budget += NON_Q_VECTORS;
1899
1900 /* A failure in MSI-X entry allocation isn't fatal, but it does
1901 * mean we disable MSI-X capabilities of the adapter. */
1902 adapter->msix_entries = kcalloc(v_budget,
1903 sizeof(struct msix_entry), GFP_KERNEL);
1904 if (!adapter->msix_entries) {
1905 err = -ENOMEM;
1906 goto out;
1907 }
1908
1909 for (vector = 0; vector < v_budget; vector++)
1910 adapter->msix_entries[vector].entry = vector;
1911
1912 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
1913 if (err)
1914 goto out;
1915
1916 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
1917 if (err)
1918 goto out;
1919
1920 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
1921
1922 out:
1923 return err;
1924 }
1925
1926 /**
1927 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1928 * @adapter: board private structure to initialize
1929 *
1930 * We allocate one q_vector per queue interrupt. If allocation fails we
1931 * return -ENOMEM.
1932 **/
1933 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1934 {
1935 int q_idx, num_q_vectors;
1936 struct ixgbevf_q_vector *q_vector;
1937
1938 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1939
1940 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1941 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1942 if (!q_vector)
1943 goto err_out;
1944 q_vector->adapter = adapter;
1945 q_vector->v_idx = q_idx;
1946 netif_napi_add(adapter->netdev, &q_vector->napi,
1947 ixgbevf_poll, 64);
1948 adapter->q_vector[q_idx] = q_vector;
1949 }
1950
1951 return 0;
1952
1953 err_out:
1954 while (q_idx) {
1955 q_idx--;
1956 q_vector = adapter->q_vector[q_idx];
1957 netif_napi_del(&q_vector->napi);
1958 kfree(q_vector);
1959 adapter->q_vector[q_idx] = NULL;
1960 }
1961 return -ENOMEM;
1962 }
1963
1964 /**
1965 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1966 * @adapter: board private structure to initialize
1967 *
1968 * This function frees the memory allocated to the q_vectors. In addition if
1969 * NAPI is enabled it will delete any references to the NAPI struct prior
1970 * to freeing the q_vector.
1971 **/
1972 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1973 {
1974 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1975
1976 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1977 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1978
1979 adapter->q_vector[q_idx] = NULL;
1980 netif_napi_del(&q_vector->napi);
1981 kfree(q_vector);
1982 }
1983 }
1984
1985 /**
1986 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1987 * @adapter: board private structure
1988 *
1989 **/
1990 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1991 {
1992 pci_disable_msix(adapter->pdev);
1993 kfree(adapter->msix_entries);
1994 adapter->msix_entries = NULL;
1995 }
1996
1997 /**
1998 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1999 * @adapter: board private structure to initialize
2000 *
2001 **/
2002 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2003 {
2004 int err;
2005
2006 /* Number of supported queues */
2007 ixgbevf_set_num_queues(adapter);
2008
2009 err = ixgbevf_set_interrupt_capability(adapter);
2010 if (err) {
2011 hw_dbg(&adapter->hw,
2012 "Unable to setup interrupt capabilities\n");
2013 goto err_set_interrupt;
2014 }
2015
2016 err = ixgbevf_alloc_q_vectors(adapter);
2017 if (err) {
2018 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2019 "vectors\n");
2020 goto err_alloc_q_vectors;
2021 }
2022
2023 err = ixgbevf_alloc_queues(adapter);
2024 if (err) {
2025 pr_err("Unable to allocate memory for queues\n");
2026 goto err_alloc_queues;
2027 }
2028
2029 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2030 "Tx Queue count = %u\n",
2031 (adapter->num_rx_queues > 1) ? "Enabled" :
2032 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2033
2034 set_bit(__IXGBEVF_DOWN, &adapter->state);
2035
2036 return 0;
2037 err_alloc_queues:
2038 ixgbevf_free_q_vectors(adapter);
2039 err_alloc_q_vectors:
2040 ixgbevf_reset_interrupt_capability(adapter);
2041 err_set_interrupt:
2042 return err;
2043 }
2044
2045 /**
2046 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2047 * @adapter: board private structure to clear interrupt scheme on
2048 *
2049 * We go through and clear interrupt specific resources and reset the structure
2050 * to pre-load conditions
2051 **/
2052 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2053 {
2054 adapter->num_tx_queues = 0;
2055 adapter->num_rx_queues = 0;
2056
2057 ixgbevf_free_q_vectors(adapter);
2058 ixgbevf_reset_interrupt_capability(adapter);
2059 }
2060
2061 /**
2062 * ixgbevf_sw_init - Initialize general software structures
2063 * (struct ixgbevf_adapter)
2064 * @adapter: board private structure to initialize
2065 *
2066 * ixgbevf_sw_init initializes the Adapter private data structure.
2067 * Fields are initialized based on PCI device information and
2068 * OS network device settings (MTU size).
2069 **/
2070 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2071 {
2072 struct ixgbe_hw *hw = &adapter->hw;
2073 struct pci_dev *pdev = adapter->pdev;
2074 struct net_device *netdev = adapter->netdev;
2075 int err;
2076
2077 /* PCI config space info */
2078
2079 hw->vendor_id = pdev->vendor;
2080 hw->device_id = pdev->device;
2081 hw->revision_id = pdev->revision;
2082 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2083 hw->subsystem_device_id = pdev->subsystem_device;
2084
2085 hw->mbx.ops.init_params(hw);
2086
2087 /* assume legacy case in which PF would only give VF 2 queues */
2088 hw->mac.max_tx_queues = 2;
2089 hw->mac.max_rx_queues = 2;
2090
2091 /* lock to protect mailbox accesses */
2092 spin_lock_init(&adapter->mbx_lock);
2093
2094 err = hw->mac.ops.reset_hw(hw);
2095 if (err) {
2096 dev_info(&pdev->dev,
2097 "PF still in reset state. Is the PF interface up?\n");
2098 } else {
2099 err = hw->mac.ops.init_hw(hw);
2100 if (err) {
2101 pr_err("init_shared_code failed: %d\n", err);
2102 goto out;
2103 }
2104 ixgbevf_negotiate_api(adapter);
2105 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2106 if (err)
2107 dev_info(&pdev->dev, "Error reading MAC address\n");
2108 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2109 dev_info(&pdev->dev,
2110 "MAC address not assigned by administrator.\n");
2111 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2112 }
2113
2114 if (!is_valid_ether_addr(netdev->dev_addr)) {
2115 dev_info(&pdev->dev, "Assigning random MAC address\n");
2116 eth_hw_addr_random(netdev);
2117 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
2118 }
2119
2120 /* Enable dynamic interrupt throttling rates */
2121 adapter->rx_itr_setting = 1;
2122 adapter->tx_itr_setting = 1;
2123
2124 /* set default ring sizes */
2125 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2126 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2127
2128 set_bit(__IXGBEVF_DOWN, &adapter->state);
2129 return 0;
2130
2131 out:
2132 return err;
2133 }
2134
2135 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2136 { \
2137 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2138 if (current_counter < last_counter) \
2139 counter += 0x100000000LL; \
2140 last_counter = current_counter; \
2141 counter &= 0xFFFFFFFF00000000LL; \
2142 counter |= current_counter; \
2143 }
2144
2145 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2146 { \
2147 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2148 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2149 u64 current_counter = (current_counter_msb << 32) | \
2150 current_counter_lsb; \
2151 if (current_counter < last_counter) \
2152 counter += 0x1000000000LL; \
2153 last_counter = current_counter; \
2154 counter &= 0xFFFFFFF000000000LL; \
2155 counter |= current_counter; \
2156 }
2157 /**
2158 * ixgbevf_update_stats - Update the board statistics counters.
2159 * @adapter: board private structure
2160 **/
2161 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2162 {
2163 struct ixgbe_hw *hw = &adapter->hw;
2164 int i;
2165
2166 if (!adapter->link_up)
2167 return;
2168
2169 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2170 adapter->stats.vfgprc);
2171 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2172 adapter->stats.vfgptc);
2173 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2174 adapter->stats.last_vfgorc,
2175 adapter->stats.vfgorc);
2176 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2177 adapter->stats.last_vfgotc,
2178 adapter->stats.vfgotc);
2179 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2180 adapter->stats.vfmprc);
2181
2182 for (i = 0; i < adapter->num_rx_queues; i++) {
2183 adapter->hw_csum_rx_error +=
2184 adapter->rx_ring[i].hw_csum_rx_error;
2185 adapter->hw_csum_rx_good +=
2186 adapter->rx_ring[i].hw_csum_rx_good;
2187 adapter->rx_ring[i].hw_csum_rx_error = 0;
2188 adapter->rx_ring[i].hw_csum_rx_good = 0;
2189 }
2190 }
2191
2192 /**
2193 * ixgbevf_watchdog - Timer Call-back
2194 * @data: pointer to adapter cast into an unsigned long
2195 **/
2196 static void ixgbevf_watchdog(unsigned long data)
2197 {
2198 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2199 struct ixgbe_hw *hw = &adapter->hw;
2200 u32 eics = 0;
2201 int i;
2202
2203 /*
2204 * Do the watchdog outside of interrupt context due to the lovely
2205 * delays that some of the newer hardware requires
2206 */
2207
2208 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2209 goto watchdog_short_circuit;
2210
2211 /* get one bit for every active tx/rx interrupt vector */
2212 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2213 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2214 if (qv->rx.ring || qv->tx.ring)
2215 eics |= 1 << i;
2216 }
2217
2218 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2219
2220 watchdog_short_circuit:
2221 schedule_work(&adapter->watchdog_task);
2222 }
2223
2224 /**
2225 * ixgbevf_tx_timeout - Respond to a Tx Hang
2226 * @netdev: network interface device structure
2227 **/
2228 static void ixgbevf_tx_timeout(struct net_device *netdev)
2229 {
2230 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2231
2232 /* Do the reset outside of interrupt context */
2233 schedule_work(&adapter->reset_task);
2234 }
2235
2236 static void ixgbevf_reset_task(struct work_struct *work)
2237 {
2238 struct ixgbevf_adapter *adapter;
2239 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2240
2241 /* If we're already down or resetting, just bail */
2242 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2243 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2244 return;
2245
2246 adapter->tx_timeout_count++;
2247
2248 ixgbevf_reinit_locked(adapter);
2249 }
2250
2251 /**
2252 * ixgbevf_watchdog_task - worker thread to bring link up
2253 * @work: pointer to work_struct containing our data
2254 **/
2255 static void ixgbevf_watchdog_task(struct work_struct *work)
2256 {
2257 struct ixgbevf_adapter *adapter = container_of(work,
2258 struct ixgbevf_adapter,
2259 watchdog_task);
2260 struct net_device *netdev = adapter->netdev;
2261 struct ixgbe_hw *hw = &adapter->hw;
2262 u32 link_speed = adapter->link_speed;
2263 bool link_up = adapter->link_up;
2264 s32 need_reset;
2265
2266 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2267
2268 /*
2269 * Always check the link on the watchdog because we have
2270 * no LSC interrupt
2271 */
2272 spin_lock_bh(&adapter->mbx_lock);
2273
2274 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2275
2276 spin_unlock_bh(&adapter->mbx_lock);
2277
2278 if (need_reset) {
2279 adapter->link_up = link_up;
2280 adapter->link_speed = link_speed;
2281 netif_carrier_off(netdev);
2282 netif_tx_stop_all_queues(netdev);
2283 schedule_work(&adapter->reset_task);
2284 goto pf_has_reset;
2285 }
2286 adapter->link_up = link_up;
2287 adapter->link_speed = link_speed;
2288
2289 if (link_up) {
2290 if (!netif_carrier_ok(netdev)) {
2291 char *link_speed_string;
2292 switch (link_speed) {
2293 case IXGBE_LINK_SPEED_10GB_FULL:
2294 link_speed_string = "10 Gbps";
2295 break;
2296 case IXGBE_LINK_SPEED_1GB_FULL:
2297 link_speed_string = "1 Gbps";
2298 break;
2299 case IXGBE_LINK_SPEED_100_FULL:
2300 link_speed_string = "100 Mbps";
2301 break;
2302 default:
2303 link_speed_string = "unknown speed";
2304 break;
2305 }
2306 dev_info(&adapter->pdev->dev,
2307 "NIC Link is Up, %s\n", link_speed_string);
2308 netif_carrier_on(netdev);
2309 netif_tx_wake_all_queues(netdev);
2310 }
2311 } else {
2312 adapter->link_up = false;
2313 adapter->link_speed = 0;
2314 if (netif_carrier_ok(netdev)) {
2315 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
2316 netif_carrier_off(netdev);
2317 netif_tx_stop_all_queues(netdev);
2318 }
2319 }
2320
2321 ixgbevf_update_stats(adapter);
2322
2323 pf_has_reset:
2324 /* Reset the timer */
2325 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2326 mod_timer(&adapter->watchdog_timer,
2327 round_jiffies(jiffies + (2 * HZ)));
2328
2329 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2330 }
2331
2332 /**
2333 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2334 * @adapter: board private structure
2335 * @tx_ring: Tx descriptor ring for a specific queue
2336 *
2337 * Free all transmit software resources
2338 **/
2339 void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2340 struct ixgbevf_ring *tx_ring)
2341 {
2342 struct pci_dev *pdev = adapter->pdev;
2343
2344 ixgbevf_clean_tx_ring(adapter, tx_ring);
2345
2346 vfree(tx_ring->tx_buffer_info);
2347 tx_ring->tx_buffer_info = NULL;
2348
2349 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2350 tx_ring->dma);
2351
2352 tx_ring->desc = NULL;
2353 }
2354
2355 /**
2356 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2357 * @adapter: board private structure
2358 *
2359 * Free all transmit software resources
2360 **/
2361 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2362 {
2363 int i;
2364
2365 for (i = 0; i < adapter->num_tx_queues; i++)
2366 if (adapter->tx_ring[i].desc)
2367 ixgbevf_free_tx_resources(adapter,
2368 &adapter->tx_ring[i]);
2369
2370 }
2371
2372 /**
2373 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2374 * @adapter: board private structure
2375 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2376 *
2377 * Return 0 on success, negative on failure
2378 **/
2379 int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2380 struct ixgbevf_ring *tx_ring)
2381 {
2382 struct pci_dev *pdev = adapter->pdev;
2383 int size;
2384
2385 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2386 tx_ring->tx_buffer_info = vzalloc(size);
2387 if (!tx_ring->tx_buffer_info)
2388 goto err;
2389
2390 /* round up to nearest 4K */
2391 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2392 tx_ring->size = ALIGN(tx_ring->size, 4096);
2393
2394 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2395 &tx_ring->dma, GFP_KERNEL);
2396 if (!tx_ring->desc)
2397 goto err;
2398
2399 tx_ring->next_to_use = 0;
2400 tx_ring->next_to_clean = 0;
2401 return 0;
2402
2403 err:
2404 vfree(tx_ring->tx_buffer_info);
2405 tx_ring->tx_buffer_info = NULL;
2406 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2407 "descriptor ring\n");
2408 return -ENOMEM;
2409 }
2410
2411 /**
2412 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2413 * @adapter: board private structure
2414 *
2415 * If this function returns with an error, then it's possible one or
2416 * more of the rings is populated (while the rest are not). It is the
2417 * callers duty to clean those orphaned rings.
2418 *
2419 * Return 0 on success, negative on failure
2420 **/
2421 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2422 {
2423 int i, err = 0;
2424
2425 for (i = 0; i < adapter->num_tx_queues; i++) {
2426 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2427 if (!err)
2428 continue;
2429 hw_dbg(&adapter->hw,
2430 "Allocation for Tx Queue %u failed\n", i);
2431 break;
2432 }
2433
2434 return err;
2435 }
2436
2437 /**
2438 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2439 * @adapter: board private structure
2440 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2441 *
2442 * Returns 0 on success, negative on failure
2443 **/
2444 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2445 struct ixgbevf_ring *rx_ring)
2446 {
2447 struct pci_dev *pdev = adapter->pdev;
2448 int size;
2449
2450 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
2451 rx_ring->rx_buffer_info = vzalloc(size);
2452 if (!rx_ring->rx_buffer_info)
2453 goto alloc_failed;
2454
2455 /* Round up to nearest 4K */
2456 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2457 rx_ring->size = ALIGN(rx_ring->size, 4096);
2458
2459 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2460 &rx_ring->dma, GFP_KERNEL);
2461
2462 if (!rx_ring->desc) {
2463 vfree(rx_ring->rx_buffer_info);
2464 rx_ring->rx_buffer_info = NULL;
2465 goto alloc_failed;
2466 }
2467
2468 rx_ring->next_to_clean = 0;
2469 rx_ring->next_to_use = 0;
2470
2471 return 0;
2472 alloc_failed:
2473 return -ENOMEM;
2474 }
2475
2476 /**
2477 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2478 * @adapter: board private structure
2479 *
2480 * If this function returns with an error, then it's possible one or
2481 * more of the rings is populated (while the rest are not). It is the
2482 * callers duty to clean those orphaned rings.
2483 *
2484 * Return 0 on success, negative on failure
2485 **/
2486 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2487 {
2488 int i, err = 0;
2489
2490 for (i = 0; i < adapter->num_rx_queues; i++) {
2491 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2492 if (!err)
2493 continue;
2494 hw_dbg(&adapter->hw,
2495 "Allocation for Rx Queue %u failed\n", i);
2496 break;
2497 }
2498 return err;
2499 }
2500
2501 /**
2502 * ixgbevf_free_rx_resources - Free Rx Resources
2503 * @adapter: board private structure
2504 * @rx_ring: ring to clean the resources from
2505 *
2506 * Free all receive software resources
2507 **/
2508 void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2509 struct ixgbevf_ring *rx_ring)
2510 {
2511 struct pci_dev *pdev = adapter->pdev;
2512
2513 ixgbevf_clean_rx_ring(adapter, rx_ring);
2514
2515 vfree(rx_ring->rx_buffer_info);
2516 rx_ring->rx_buffer_info = NULL;
2517
2518 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2519 rx_ring->dma);
2520
2521 rx_ring->desc = NULL;
2522 }
2523
2524 /**
2525 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2526 * @adapter: board private structure
2527 *
2528 * Free all receive software resources
2529 **/
2530 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2531 {
2532 int i;
2533
2534 for (i = 0; i < adapter->num_rx_queues; i++)
2535 if (adapter->rx_ring[i].desc)
2536 ixgbevf_free_rx_resources(adapter,
2537 &adapter->rx_ring[i]);
2538 }
2539
2540 static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
2541 {
2542 struct ixgbe_hw *hw = &adapter->hw;
2543 struct ixgbevf_ring *rx_ring;
2544 unsigned int def_q = 0;
2545 unsigned int num_tcs = 0;
2546 unsigned int num_rx_queues = 1;
2547 int err, i;
2548
2549 spin_lock_bh(&adapter->mbx_lock);
2550
2551 /* fetch queue configuration from the PF */
2552 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2553
2554 spin_unlock_bh(&adapter->mbx_lock);
2555
2556 if (err)
2557 return err;
2558
2559 if (num_tcs > 1) {
2560 /* update default Tx ring register index */
2561 adapter->tx_ring[0].reg_idx = def_q;
2562
2563 /* we need as many queues as traffic classes */
2564 num_rx_queues = num_tcs;
2565 }
2566
2567 /* nothing to do if we have the correct number of queues */
2568 if (adapter->num_rx_queues == num_rx_queues)
2569 return 0;
2570
2571 /* allocate new rings */
2572 rx_ring = kcalloc(num_rx_queues,
2573 sizeof(struct ixgbevf_ring), GFP_KERNEL);
2574 if (!rx_ring)
2575 return -ENOMEM;
2576
2577 /* setup ring fields */
2578 for (i = 0; i < num_rx_queues; i++) {
2579 rx_ring[i].count = adapter->rx_ring_count;
2580 rx_ring[i].queue_index = i;
2581 rx_ring[i].reg_idx = i;
2582 rx_ring[i].dev = &adapter->pdev->dev;
2583 rx_ring[i].netdev = adapter->netdev;
2584 }
2585
2586 /* free the existing ring and queues */
2587 adapter->num_rx_queues = 0;
2588 kfree(adapter->rx_ring);
2589
2590 /* move new rings into position on the adapter struct */
2591 adapter->rx_ring = rx_ring;
2592 adapter->num_rx_queues = num_rx_queues;
2593
2594 return 0;
2595 }
2596
2597 /**
2598 * ixgbevf_open - Called when a network interface is made active
2599 * @netdev: network interface device structure
2600 *
2601 * Returns 0 on success, negative value on failure
2602 *
2603 * The open entry point is called when a network interface is made
2604 * active by the system (IFF_UP). At this point all resources needed
2605 * for transmit and receive operations are allocated, the interrupt
2606 * handler is registered with the OS, the watchdog timer is started,
2607 * and the stack is notified that the interface is ready.
2608 **/
2609 static int ixgbevf_open(struct net_device *netdev)
2610 {
2611 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2612 struct ixgbe_hw *hw = &adapter->hw;
2613 int err;
2614
2615 /* A previous failure to open the device because of a lack of
2616 * available MSIX vector resources may have reset the number
2617 * of msix vectors variable to zero. The only way to recover
2618 * is to unload/reload the driver and hope that the system has
2619 * been able to recover some MSIX vector resources.
2620 */
2621 if (!adapter->num_msix_vectors)
2622 return -ENOMEM;
2623
2624 /* disallow open during test */
2625 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2626 return -EBUSY;
2627
2628 if (hw->adapter_stopped) {
2629 ixgbevf_reset(adapter);
2630 /* if adapter is still stopped then PF isn't up and
2631 * the vf can't start. */
2632 if (hw->adapter_stopped) {
2633 err = IXGBE_ERR_MBX;
2634 pr_err("Unable to start - perhaps the PF Driver isn't "
2635 "up yet\n");
2636 goto err_setup_reset;
2637 }
2638 }
2639
2640 /* setup queue reg_idx and Rx queue count */
2641 err = ixgbevf_setup_queues(adapter);
2642 if (err)
2643 goto err_setup_queues;
2644
2645 /* allocate transmit descriptors */
2646 err = ixgbevf_setup_all_tx_resources(adapter);
2647 if (err)
2648 goto err_setup_tx;
2649
2650 /* allocate receive descriptors */
2651 err = ixgbevf_setup_all_rx_resources(adapter);
2652 if (err)
2653 goto err_setup_rx;
2654
2655 ixgbevf_configure(adapter);
2656
2657 /*
2658 * Map the Tx/Rx rings to the vectors we were allotted.
2659 * if request_irq will be called in this function map_rings
2660 * must be called *before* up_complete
2661 */
2662 ixgbevf_map_rings_to_vectors(adapter);
2663
2664 ixgbevf_up_complete(adapter);
2665
2666 /* clear any pending interrupts, may auto mask */
2667 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2668 err = ixgbevf_request_irq(adapter);
2669 if (err)
2670 goto err_req_irq;
2671
2672 ixgbevf_irq_enable(adapter);
2673
2674 return 0;
2675
2676 err_req_irq:
2677 ixgbevf_down(adapter);
2678 err_setup_rx:
2679 ixgbevf_free_all_rx_resources(adapter);
2680 err_setup_tx:
2681 ixgbevf_free_all_tx_resources(adapter);
2682 err_setup_queues:
2683 ixgbevf_reset(adapter);
2684
2685 err_setup_reset:
2686
2687 return err;
2688 }
2689
2690 /**
2691 * ixgbevf_close - Disables a network interface
2692 * @netdev: network interface device structure
2693 *
2694 * Returns 0, this is not allowed to fail
2695 *
2696 * The close entry point is called when an interface is de-activated
2697 * by the OS. The hardware is still under the drivers control, but
2698 * needs to be disabled. A global MAC reset is issued to stop the
2699 * hardware, and all transmit and receive resources are freed.
2700 **/
2701 static int ixgbevf_close(struct net_device *netdev)
2702 {
2703 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2704
2705 ixgbevf_down(adapter);
2706 ixgbevf_free_irq(adapter);
2707
2708 ixgbevf_free_all_tx_resources(adapter);
2709 ixgbevf_free_all_rx_resources(adapter);
2710
2711 return 0;
2712 }
2713
2714 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2715 u32 vlan_macip_lens, u32 type_tucmd,
2716 u32 mss_l4len_idx)
2717 {
2718 struct ixgbe_adv_tx_context_desc *context_desc;
2719 u16 i = tx_ring->next_to_use;
2720
2721 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2722
2723 i++;
2724 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2725
2726 /* set bits to identify this as an advanced context descriptor */
2727 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2728
2729 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2730 context_desc->seqnum_seed = 0;
2731 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2732 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2733 }
2734
2735 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
2736 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2737 {
2738 u32 vlan_macip_lens, type_tucmd;
2739 u32 mss_l4len_idx, l4len;
2740
2741 if (!skb_is_gso(skb))
2742 return 0;
2743
2744 if (skb_header_cloned(skb)) {
2745 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2746 if (err)
2747 return err;
2748 }
2749
2750 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2751 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2752
2753 if (skb->protocol == htons(ETH_P_IP)) {
2754 struct iphdr *iph = ip_hdr(skb);
2755 iph->tot_len = 0;
2756 iph->check = 0;
2757 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2758 iph->daddr, 0,
2759 IPPROTO_TCP,
2760 0);
2761 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2762 } else if (skb_is_gso_v6(skb)) {
2763 ipv6_hdr(skb)->payload_len = 0;
2764 tcp_hdr(skb)->check =
2765 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2766 &ipv6_hdr(skb)->daddr,
2767 0, IPPROTO_TCP, 0);
2768 }
2769
2770 /* compute header lengths */
2771 l4len = tcp_hdrlen(skb);
2772 *hdr_len += l4len;
2773 *hdr_len = skb_transport_offset(skb) + l4len;
2774
2775 /* mss_l4len_id: use 1 as index for TSO */
2776 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2777 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2778 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2779
2780 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2781 vlan_macip_lens = skb_network_header_len(skb);
2782 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2783 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2784
2785 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2786 type_tucmd, mss_l4len_idx);
2787
2788 return 1;
2789 }
2790
2791 static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2792 struct sk_buff *skb, u32 tx_flags)
2793 {
2794 u32 vlan_macip_lens = 0;
2795 u32 mss_l4len_idx = 0;
2796 u32 type_tucmd = 0;
2797
2798 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2799 u8 l4_hdr = 0;
2800 switch (skb->protocol) {
2801 case __constant_htons(ETH_P_IP):
2802 vlan_macip_lens |= skb_network_header_len(skb);
2803 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2804 l4_hdr = ip_hdr(skb)->protocol;
2805 break;
2806 case __constant_htons(ETH_P_IPV6):
2807 vlan_macip_lens |= skb_network_header_len(skb);
2808 l4_hdr = ipv6_hdr(skb)->nexthdr;
2809 break;
2810 default:
2811 if (unlikely(net_ratelimit())) {
2812 dev_warn(tx_ring->dev,
2813 "partial checksum but proto=%x!\n",
2814 skb->protocol);
2815 }
2816 break;
2817 }
2818
2819 switch (l4_hdr) {
2820 case IPPROTO_TCP:
2821 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2822 mss_l4len_idx = tcp_hdrlen(skb) <<
2823 IXGBE_ADVTXD_L4LEN_SHIFT;
2824 break;
2825 case IPPROTO_SCTP:
2826 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2827 mss_l4len_idx = sizeof(struct sctphdr) <<
2828 IXGBE_ADVTXD_L4LEN_SHIFT;
2829 break;
2830 case IPPROTO_UDP:
2831 mss_l4len_idx = sizeof(struct udphdr) <<
2832 IXGBE_ADVTXD_L4LEN_SHIFT;
2833 break;
2834 default:
2835 if (unlikely(net_ratelimit())) {
2836 dev_warn(tx_ring->dev,
2837 "partial checksum but l4 proto=%x!\n",
2838 l4_hdr);
2839 }
2840 break;
2841 }
2842 }
2843
2844 /* vlan_macip_lens: MACLEN, VLAN tag */
2845 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2846 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2847
2848 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2849 type_tucmd, mss_l4len_idx);
2850
2851 return (skb->ip_summed == CHECKSUM_PARTIAL);
2852 }
2853
2854 static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
2855 struct sk_buff *skb, u32 tx_flags)
2856 {
2857 struct ixgbevf_tx_buffer *tx_buffer_info;
2858 unsigned int len;
2859 unsigned int total = skb->len;
2860 unsigned int offset = 0, size;
2861 int count = 0;
2862 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2863 unsigned int f;
2864 int i;
2865
2866 i = tx_ring->next_to_use;
2867
2868 len = min(skb_headlen(skb), total);
2869 while (len) {
2870 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2871 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2872
2873 tx_buffer_info->length = size;
2874 tx_buffer_info->mapped_as_page = false;
2875 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
2876 skb->data + offset,
2877 size, DMA_TO_DEVICE);
2878 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
2879 goto dma_error;
2880
2881 len -= size;
2882 total -= size;
2883 offset += size;
2884 count++;
2885 i++;
2886 if (i == tx_ring->count)
2887 i = 0;
2888 }
2889
2890 for (f = 0; f < nr_frags; f++) {
2891 const struct skb_frag_struct *frag;
2892
2893 frag = &skb_shinfo(skb)->frags[f];
2894 len = min((unsigned int)skb_frag_size(frag), total);
2895 offset = 0;
2896
2897 while (len) {
2898 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2899 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2900
2901 tx_buffer_info->length = size;
2902 tx_buffer_info->dma =
2903 skb_frag_dma_map(tx_ring->dev, frag,
2904 offset, size, DMA_TO_DEVICE);
2905 if (dma_mapping_error(tx_ring->dev,
2906 tx_buffer_info->dma))
2907 goto dma_error;
2908 tx_buffer_info->mapped_as_page = true;
2909
2910 len -= size;
2911 total -= size;
2912 offset += size;
2913 count++;
2914 i++;
2915 if (i == tx_ring->count)
2916 i = 0;
2917 }
2918 if (total == 0)
2919 break;
2920 }
2921
2922 if (i == 0)
2923 i = tx_ring->count - 1;
2924 else
2925 i = i - 1;
2926 tx_ring->tx_buffer_info[i].skb = skb;
2927
2928 return count;
2929
2930 dma_error:
2931 dev_err(tx_ring->dev, "TX DMA map failed\n");
2932
2933 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2934 tx_buffer_info->dma = 0;
2935 count--;
2936
2937 /* clear timestamp and dma mappings for remaining portion of packet */
2938 while (count >= 0) {
2939 count--;
2940 i--;
2941 if (i < 0)
2942 i += tx_ring->count;
2943 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2944 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
2945 }
2946
2947 return count;
2948 }
2949
2950 static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
2951 int count, unsigned int first, u32 paylen,
2952 u8 hdr_len)
2953 {
2954 union ixgbe_adv_tx_desc *tx_desc = NULL;
2955 struct ixgbevf_tx_buffer *tx_buffer_info;
2956 u32 olinfo_status = 0, cmd_type_len = 0;
2957 unsigned int i;
2958
2959 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2960
2961 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2962
2963 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2964
2965 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2966 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2967
2968 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2969 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2970
2971 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2972 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2973
2974 /* use index 1 context for tso */
2975 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2976 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
2977 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
2978 }
2979
2980 /*
2981 * Check Context must be set if Tx switch is enabled, which it
2982 * always is for case where virtual functions are running
2983 */
2984 olinfo_status |= IXGBE_ADVTXD_CC;
2985
2986 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2987
2988 i = tx_ring->next_to_use;
2989 while (count--) {
2990 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2991 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2992 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2993 tx_desc->read.cmd_type_len =
2994 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2995 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2996 i++;
2997 if (i == tx_ring->count)
2998 i = 0;
2999 }
3000
3001 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
3002
3003 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
3004
3005 /* Force memory writes to complete before letting h/w
3006 * know there are new descriptors to fetch. (Only
3007 * applicable for weak-ordered memory model archs,
3008 * such as IA-64).
3009 */
3010 wmb();
3011
3012 tx_ring->tx_buffer_info[first].next_to_watch = tx_desc;
3013 tx_ring->next_to_use = i;
3014 }
3015
3016 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3017 {
3018 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3019
3020 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
3021 /* Herbert's original patch had:
3022 * smp_mb__after_netif_stop_queue();
3023 * but since that doesn't exist yet, just open code it. */
3024 smp_mb();
3025
3026 /* We need to check again in a case another CPU has just
3027 * made room available. */
3028 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
3029 return -EBUSY;
3030
3031 /* A reprieve! - use start_queue because it doesn't call schedule */
3032 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
3033 ++adapter->restart_queue;
3034 return 0;
3035 }
3036
3037 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3038 {
3039 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
3040 return 0;
3041 return __ixgbevf_maybe_stop_tx(tx_ring, size);
3042 }
3043
3044 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3045 {
3046 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3047 struct ixgbevf_ring *tx_ring;
3048 unsigned int first;
3049 unsigned int tx_flags = 0;
3050 u8 hdr_len = 0;
3051 int r_idx = 0, tso;
3052 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3053 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3054 unsigned short f;
3055 #endif
3056 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
3057 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
3058 dev_kfree_skb(skb);
3059 return NETDEV_TX_OK;
3060 }
3061
3062 tx_ring = &adapter->tx_ring[r_idx];
3063
3064 /*
3065 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3066 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3067 * + 2 desc gap to keep tail from touching head,
3068 * + 1 desc for context descriptor,
3069 * otherwise try next time
3070 */
3071 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3072 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3073 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3074 #else
3075 count += skb_shinfo(skb)->nr_frags;
3076 #endif
3077 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
3078 adapter->tx_busy++;
3079 return NETDEV_TX_BUSY;
3080 }
3081
3082 if (vlan_tx_tag_present(skb)) {
3083 tx_flags |= vlan_tx_tag_get(skb);
3084 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3085 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3086 }
3087
3088 first = tx_ring->next_to_use;
3089
3090 if (skb->protocol == htons(ETH_P_IP))
3091 tx_flags |= IXGBE_TX_FLAGS_IPV4;
3092 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
3093 if (tso < 0) {
3094 dev_kfree_skb_any(skb);
3095 return NETDEV_TX_OK;
3096 }
3097
3098 if (tso)
3099 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
3100 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
3101 tx_flags |= IXGBE_TX_FLAGS_CSUM;
3102
3103 ixgbevf_tx_queue(tx_ring, tx_flags,
3104 ixgbevf_tx_map(tx_ring, skb, tx_flags),
3105 first, skb->len, hdr_len);
3106
3107 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
3108
3109 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3110
3111 return NETDEV_TX_OK;
3112 }
3113
3114 /**
3115 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3116 * @netdev: network interface device structure
3117 * @p: pointer to an address structure
3118 *
3119 * Returns 0 on success, negative on failure
3120 **/
3121 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3122 {
3123 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3124 struct ixgbe_hw *hw = &adapter->hw;
3125 struct sockaddr *addr = p;
3126
3127 if (!is_valid_ether_addr(addr->sa_data))
3128 return -EADDRNOTAVAIL;
3129
3130 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3131 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3132
3133 spin_lock_bh(&adapter->mbx_lock);
3134
3135 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
3136
3137 spin_unlock_bh(&adapter->mbx_lock);
3138
3139 return 0;
3140 }
3141
3142 /**
3143 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3144 * @netdev: network interface device structure
3145 * @new_mtu: new value for maximum frame size
3146 *
3147 * Returns 0 on success, negative on failure
3148 **/
3149 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3150 {
3151 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3152 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3153 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
3154
3155 switch (adapter->hw.api_version) {
3156 case ixgbe_mbox_api_11:
3157 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3158 break;
3159 default:
3160 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3161 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3162 break;
3163 }
3164
3165 /* MTU < 68 is an error and causes problems on some kernels */
3166 if ((new_mtu < 68) || (max_frame > max_possible_frame))
3167 return -EINVAL;
3168
3169 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3170 netdev->mtu, new_mtu);
3171 /* must set new MTU before calling down or up */
3172 netdev->mtu = new_mtu;
3173
3174 if (netif_running(netdev))
3175 ixgbevf_reinit_locked(adapter);
3176
3177 return 0;
3178 }
3179
3180 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3181 {
3182 struct net_device *netdev = pci_get_drvdata(pdev);
3183 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3184 #ifdef CONFIG_PM
3185 int retval = 0;
3186 #endif
3187
3188 netif_device_detach(netdev);
3189
3190 if (netif_running(netdev)) {
3191 rtnl_lock();
3192 ixgbevf_down(adapter);
3193 ixgbevf_free_irq(adapter);
3194 ixgbevf_free_all_tx_resources(adapter);
3195 ixgbevf_free_all_rx_resources(adapter);
3196 rtnl_unlock();
3197 }
3198
3199 ixgbevf_clear_interrupt_scheme(adapter);
3200
3201 #ifdef CONFIG_PM
3202 retval = pci_save_state(pdev);
3203 if (retval)
3204 return retval;
3205
3206 #endif
3207 pci_disable_device(pdev);
3208
3209 return 0;
3210 }
3211
3212 #ifdef CONFIG_PM
3213 static int ixgbevf_resume(struct pci_dev *pdev)
3214 {
3215 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
3216 struct net_device *netdev = adapter->netdev;
3217 u32 err;
3218
3219 pci_set_power_state(pdev, PCI_D0);
3220 pci_restore_state(pdev);
3221 /*
3222 * pci_restore_state clears dev->state_saved so call
3223 * pci_save_state to restore it.
3224 */
3225 pci_save_state(pdev);
3226
3227 err = pci_enable_device_mem(pdev);
3228 if (err) {
3229 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3230 return err;
3231 }
3232 pci_set_master(pdev);
3233
3234 ixgbevf_reset(adapter);
3235
3236 rtnl_lock();
3237 err = ixgbevf_init_interrupt_scheme(adapter);
3238 rtnl_unlock();
3239 if (err) {
3240 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3241 return err;
3242 }
3243
3244 if (netif_running(netdev)) {
3245 err = ixgbevf_open(netdev);
3246 if (err)
3247 return err;
3248 }
3249
3250 netif_device_attach(netdev);
3251
3252 return err;
3253 }
3254
3255 #endif /* CONFIG_PM */
3256 static void ixgbevf_shutdown(struct pci_dev *pdev)
3257 {
3258 ixgbevf_suspend(pdev, PMSG_SUSPEND);
3259 }
3260
3261 static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3262 struct rtnl_link_stats64 *stats)
3263 {
3264 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3265 unsigned int start;
3266 u64 bytes, packets;
3267 const struct ixgbevf_ring *ring;
3268 int i;
3269
3270 ixgbevf_update_stats(adapter);
3271
3272 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3273
3274 for (i = 0; i < adapter->num_rx_queues; i++) {
3275 ring = &adapter->rx_ring[i];
3276 do {
3277 start = u64_stats_fetch_begin_bh(&ring->syncp);
3278 bytes = ring->total_bytes;
3279 packets = ring->total_packets;
3280 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3281 stats->rx_bytes += bytes;
3282 stats->rx_packets += packets;
3283 }
3284
3285 for (i = 0; i < adapter->num_tx_queues; i++) {
3286 ring = &adapter->tx_ring[i];
3287 do {
3288 start = u64_stats_fetch_begin_bh(&ring->syncp);
3289 bytes = ring->total_bytes;
3290 packets = ring->total_packets;
3291 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3292 stats->tx_bytes += bytes;
3293 stats->tx_packets += packets;
3294 }
3295
3296 return stats;
3297 }
3298
3299 static const struct net_device_ops ixgbevf_netdev_ops = {
3300 .ndo_open = ixgbevf_open,
3301 .ndo_stop = ixgbevf_close,
3302 .ndo_start_xmit = ixgbevf_xmit_frame,
3303 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
3304 .ndo_get_stats64 = ixgbevf_get_stats,
3305 .ndo_validate_addr = eth_validate_addr,
3306 .ndo_set_mac_address = ixgbevf_set_mac,
3307 .ndo_change_mtu = ixgbevf_change_mtu,
3308 .ndo_tx_timeout = ixgbevf_tx_timeout,
3309 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3310 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
3311 };
3312
3313 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3314 {
3315 dev->netdev_ops = &ixgbevf_netdev_ops;
3316 ixgbevf_set_ethtool_ops(dev);
3317 dev->watchdog_timeo = 5 * HZ;
3318 }
3319
3320 /**
3321 * ixgbevf_probe - Device Initialization Routine
3322 * @pdev: PCI device information struct
3323 * @ent: entry in ixgbevf_pci_tbl
3324 *
3325 * Returns 0 on success, negative on failure
3326 *
3327 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3328 * The OS initialization, configuring of the adapter private structure,
3329 * and a hardware reset occur.
3330 **/
3331 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3332 {
3333 struct net_device *netdev;
3334 struct ixgbevf_adapter *adapter = NULL;
3335 struct ixgbe_hw *hw = NULL;
3336 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3337 static int cards_found;
3338 int err, pci_using_dac;
3339
3340 err = pci_enable_device(pdev);
3341 if (err)
3342 return err;
3343
3344 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3345 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3346 pci_using_dac = 1;
3347 } else {
3348 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3349 if (err) {
3350 err = dma_set_coherent_mask(&pdev->dev,
3351 DMA_BIT_MASK(32));
3352 if (err) {
3353 dev_err(&pdev->dev, "No usable DMA "
3354 "configuration, aborting\n");
3355 goto err_dma;
3356 }
3357 }
3358 pci_using_dac = 0;
3359 }
3360
3361 err = pci_request_regions(pdev, ixgbevf_driver_name);
3362 if (err) {
3363 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3364 goto err_pci_reg;
3365 }
3366
3367 pci_set_master(pdev);
3368
3369 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3370 MAX_TX_QUEUES);
3371 if (!netdev) {
3372 err = -ENOMEM;
3373 goto err_alloc_etherdev;
3374 }
3375
3376 SET_NETDEV_DEV(netdev, &pdev->dev);
3377
3378 pci_set_drvdata(pdev, netdev);
3379 adapter = netdev_priv(netdev);
3380
3381 adapter->netdev = netdev;
3382 adapter->pdev = pdev;
3383 hw = &adapter->hw;
3384 hw->back = adapter;
3385 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3386
3387 /*
3388 * call save state here in standalone driver because it relies on
3389 * adapter struct to exist, and needs to call netdev_priv
3390 */
3391 pci_save_state(pdev);
3392
3393 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3394 pci_resource_len(pdev, 0));
3395 if (!hw->hw_addr) {
3396 err = -EIO;
3397 goto err_ioremap;
3398 }
3399
3400 ixgbevf_assign_netdev_ops(netdev);
3401
3402 adapter->bd_number = cards_found;
3403
3404 /* Setup hw api */
3405 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3406 hw->mac.type = ii->mac;
3407
3408 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
3409 sizeof(struct ixgbe_mbx_operations));
3410
3411 /* setup the private structure */
3412 err = ixgbevf_sw_init(adapter);
3413 if (err)
3414 goto err_sw_init;
3415
3416 /* The HW MAC address was set and/or determined in sw_init */
3417 if (!is_valid_ether_addr(netdev->dev_addr)) {
3418 pr_err("invalid MAC address\n");
3419 err = -EIO;
3420 goto err_sw_init;
3421 }
3422
3423 netdev->hw_features = NETIF_F_SG |
3424 NETIF_F_IP_CSUM |
3425 NETIF_F_IPV6_CSUM |
3426 NETIF_F_TSO |
3427 NETIF_F_TSO6 |
3428 NETIF_F_RXCSUM;
3429
3430 netdev->features = netdev->hw_features |
3431 NETIF_F_HW_VLAN_CTAG_TX |
3432 NETIF_F_HW_VLAN_CTAG_RX |
3433 NETIF_F_HW_VLAN_CTAG_FILTER;
3434
3435 netdev->vlan_features |= NETIF_F_TSO;
3436 netdev->vlan_features |= NETIF_F_TSO6;
3437 netdev->vlan_features |= NETIF_F_IP_CSUM;
3438 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3439 netdev->vlan_features |= NETIF_F_SG;
3440
3441 if (pci_using_dac)
3442 netdev->features |= NETIF_F_HIGHDMA;
3443
3444 netdev->priv_flags |= IFF_UNICAST_FLT;
3445
3446 init_timer(&adapter->watchdog_timer);
3447 adapter->watchdog_timer.function = ixgbevf_watchdog;
3448 adapter->watchdog_timer.data = (unsigned long)adapter;
3449
3450 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3451 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3452
3453 err = ixgbevf_init_interrupt_scheme(adapter);
3454 if (err)
3455 goto err_sw_init;
3456
3457 strcpy(netdev->name, "eth%d");
3458
3459 err = register_netdev(netdev);
3460 if (err)
3461 goto err_register;
3462
3463 netif_carrier_off(netdev);
3464
3465 ixgbevf_init_last_counter_stats(adapter);
3466
3467 /* print the MAC address */
3468 hw_dbg(hw, "%pM\n", netdev->dev_addr);
3469
3470 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3471
3472 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3473 cards_found++;
3474 return 0;
3475
3476 err_register:
3477 ixgbevf_clear_interrupt_scheme(adapter);
3478 err_sw_init:
3479 ixgbevf_reset_interrupt_capability(adapter);
3480 iounmap(hw->hw_addr);
3481 err_ioremap:
3482 free_netdev(netdev);
3483 err_alloc_etherdev:
3484 pci_release_regions(pdev);
3485 err_pci_reg:
3486 err_dma:
3487 pci_disable_device(pdev);
3488 return err;
3489 }
3490
3491 /**
3492 * ixgbevf_remove - Device Removal Routine
3493 * @pdev: PCI device information struct
3494 *
3495 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3496 * that it should release a PCI device. The could be caused by a
3497 * Hot-Plug event, or because the driver is going to be removed from
3498 * memory.
3499 **/
3500 static void ixgbevf_remove(struct pci_dev *pdev)
3501 {
3502 struct net_device *netdev = pci_get_drvdata(pdev);
3503 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3504
3505 set_bit(__IXGBEVF_DOWN, &adapter->state);
3506
3507 del_timer_sync(&adapter->watchdog_timer);
3508
3509 cancel_work_sync(&adapter->reset_task);
3510 cancel_work_sync(&adapter->watchdog_task);
3511
3512 if (netdev->reg_state == NETREG_REGISTERED)
3513 unregister_netdev(netdev);
3514
3515 ixgbevf_clear_interrupt_scheme(adapter);
3516 ixgbevf_reset_interrupt_capability(adapter);
3517
3518 iounmap(adapter->hw.hw_addr);
3519 pci_release_regions(pdev);
3520
3521 hw_dbg(&adapter->hw, "Remove complete\n");
3522
3523 kfree(adapter->tx_ring);
3524 kfree(adapter->rx_ring);
3525
3526 free_netdev(netdev);
3527
3528 pci_disable_device(pdev);
3529 }
3530
3531 /**
3532 * ixgbevf_io_error_detected - called when PCI error is detected
3533 * @pdev: Pointer to PCI device
3534 * @state: The current pci connection state
3535 *
3536 * This function is called after a PCI bus error affecting
3537 * this device has been detected.
3538 */
3539 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3540 pci_channel_state_t state)
3541 {
3542 struct net_device *netdev = pci_get_drvdata(pdev);
3543 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3544
3545 netif_device_detach(netdev);
3546
3547 if (state == pci_channel_io_perm_failure)
3548 return PCI_ERS_RESULT_DISCONNECT;
3549
3550 if (netif_running(netdev))
3551 ixgbevf_down(adapter);
3552
3553 pci_disable_device(pdev);
3554
3555 /* Request a slot slot reset. */
3556 return PCI_ERS_RESULT_NEED_RESET;
3557 }
3558
3559 /**
3560 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3561 * @pdev: Pointer to PCI device
3562 *
3563 * Restart the card from scratch, as if from a cold-boot. Implementation
3564 * resembles the first-half of the ixgbevf_resume routine.
3565 */
3566 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3567 {
3568 struct net_device *netdev = pci_get_drvdata(pdev);
3569 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3570
3571 if (pci_enable_device_mem(pdev)) {
3572 dev_err(&pdev->dev,
3573 "Cannot re-enable PCI device after reset.\n");
3574 return PCI_ERS_RESULT_DISCONNECT;
3575 }
3576
3577 pci_set_master(pdev);
3578
3579 ixgbevf_reset(adapter);
3580
3581 return PCI_ERS_RESULT_RECOVERED;
3582 }
3583
3584 /**
3585 * ixgbevf_io_resume - called when traffic can start flowing again.
3586 * @pdev: Pointer to PCI device
3587 *
3588 * This callback is called when the error recovery driver tells us that
3589 * its OK to resume normal operation. Implementation resembles the
3590 * second-half of the ixgbevf_resume routine.
3591 */
3592 static void ixgbevf_io_resume(struct pci_dev *pdev)
3593 {
3594 struct net_device *netdev = pci_get_drvdata(pdev);
3595 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3596
3597 if (netif_running(netdev))
3598 ixgbevf_up(adapter);
3599
3600 netif_device_attach(netdev);
3601 }
3602
3603 /* PCI Error Recovery (ERS) */
3604 static const struct pci_error_handlers ixgbevf_err_handler = {
3605 .error_detected = ixgbevf_io_error_detected,
3606 .slot_reset = ixgbevf_io_slot_reset,
3607 .resume = ixgbevf_io_resume,
3608 };
3609
3610 static struct pci_driver ixgbevf_driver = {
3611 .name = ixgbevf_driver_name,
3612 .id_table = ixgbevf_pci_tbl,
3613 .probe = ixgbevf_probe,
3614 .remove = ixgbevf_remove,
3615 #ifdef CONFIG_PM
3616 /* Power Management Hooks */
3617 .suspend = ixgbevf_suspend,
3618 .resume = ixgbevf_resume,
3619 #endif
3620 .shutdown = ixgbevf_shutdown,
3621 .err_handler = &ixgbevf_err_handler
3622 };
3623
3624 /**
3625 * ixgbevf_init_module - Driver Registration Routine
3626 *
3627 * ixgbevf_init_module is the first routine called when the driver is
3628 * loaded. All it does is register with the PCI subsystem.
3629 **/
3630 static int __init ixgbevf_init_module(void)
3631 {
3632 int ret;
3633 pr_info("%s - version %s\n", ixgbevf_driver_string,
3634 ixgbevf_driver_version);
3635
3636 pr_info("%s\n", ixgbevf_copyright);
3637
3638 ret = pci_register_driver(&ixgbevf_driver);
3639 return ret;
3640 }
3641
3642 module_init(ixgbevf_init_module);
3643
3644 /**
3645 * ixgbevf_exit_module - Driver Exit Cleanup Routine
3646 *
3647 * ixgbevf_exit_module is called just before the driver is removed
3648 * from memory.
3649 **/
3650 static void __exit ixgbevf_exit_module(void)
3651 {
3652 pci_unregister_driver(&ixgbevf_driver);
3653 }
3654
3655 #ifdef DEBUG
3656 /**
3657 * ixgbevf_get_hw_dev_name - return device name string
3658 * used by hardware layer to print debugging information
3659 **/
3660 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3661 {
3662 struct ixgbevf_adapter *adapter = hw->back;
3663 return adapter->netdev->name;
3664 }
3665
3666 #endif
3667 module_exit(ixgbevf_exit_module);
3668
3669 /* ixgbevf_main.c */