blob: c5ffe1dd3fcde2177e58f08034557e9775804e77 [file] [log] [blame]
Greg Rose92915f72010-01-09 02:24:10 +00001/*******************************************************************************
2
3 Intel 82599 Virtual Function driver
Greg Rose5c47a2b2012-01-06 02:53:30 +00004 Copyright(c) 1999 - 2012 Intel Corporation.
Greg Rose92915f72010-01-09 02:24:10 +00005
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******************************************************************************/
Jeff Kirsherdbd96362011-10-21 19:38:18 +000032
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Greg Rose92915f72010-01-09 02:24:10 +000035#include <linux/types.h>
Jiri Pirkodadcd652011-07-21 03:25:09 +000036#include <linux/bitops.h>
Greg Rose92915f72010-01-09 02:24:10 +000037#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>
Alexander Duyck70a10e22012-05-11 08:33:21 +000045#include <linux/sctp.h>
Greg Rose92915f72010-01-09 02:24:10 +000046#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Greg Rose92915f72010-01-09 02:24:10 +000048#include <net/checksum.h>
49#include <net/ip6_checksum.h>
50#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000051#include <linux/if.h>
Greg Rose92915f72010-01-09 02:24:10 +000052#include <linux/if_vlan.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040053#include <linux/prefetch.h>
Greg Rose92915f72010-01-09 02:24:10 +000054
55#include "ixgbevf.h"
56
Stephen Hemminger3d8fe982012-01-18 22:13:34 +000057const char ixgbevf_driver_name[] = "ixgbevf";
Greg Rose92915f72010-01-09 02:24:10 +000058static const char ixgbevf_driver_string[] =
Greg Rose422e05d2011-03-12 02:01:29 +000059 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
Greg Rose92915f72010-01-09 02:24:10 +000060
Greg Rose9cd91302012-04-17 04:29:39 +000061#define DRV_VERSION "2.6.0-k"
Greg Rose92915f72010-01-09 02:24:10 +000062const char ixgbevf_driver_version[] = DRV_VERSION;
Greg Rose66c87bd2010-11-16 19:26:43 -080063static char ixgbevf_copyright[] =
Greg Rose5c47a2b2012-01-06 02:53:30 +000064 "Copyright (c) 2009 - 2012 Intel Corporation.";
Greg Rose92915f72010-01-09 02:24:10 +000065
66static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
Greg Rose2316aa22010-12-02 07:12:26 +000067 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
Greg Rose92915f72010-01-09 02:24:10 +000069};
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 */
79static struct pci_device_id ixgbevf_pci_tbl[] = {
80 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
81 board_82599_vf},
Greg Rose2316aa22010-12-02 07:12:26 +000082 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
83 board_X540_vf},
Greg Rose92915f72010-01-09 02:24:10 +000084
85 /* required last entry */
86 {0, }
87};
88MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
89
90MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
91MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
92MODULE_LICENSE("GPL");
93MODULE_VERSION(DRV_VERSION);
94
stephen hemmingerb3f4d592012-03-13 06:04:20 +000095#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
96static int debug = -1;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
Greg Rose92915f72010-01-09 02:24:10 +000099
100/* forward decls */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000101static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000102
103static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
104 struct ixgbevf_ring *rx_ring,
105 u32 val)
106{
107 /*
108 * Force memory writes to complete before letting h/w
109 * know there are new descriptors to fetch. (Only
110 * applicable for weak-ordered memory model archs,
111 * such as IA-64).
112 */
113 wmb();
114 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
115}
116
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000117/**
Greg Rose65d676c2011-02-03 06:54:13 +0000118 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
Greg Rose92915f72010-01-09 02:24:10 +0000119 * @adapter: pointer to adapter struct
120 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
121 * @queue: queue to map the corresponding interrupt to
122 * @msix_vector: the vector to map to the corresponding queue
123 *
124 */
125static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
126 u8 queue, u8 msix_vector)
127{
128 u32 ivar, index;
129 struct ixgbe_hw *hw = &adapter->hw;
130 if (direction == -1) {
131 /* other causes */
132 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
133 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
134 ivar &= ~0xFF;
135 ivar |= msix_vector;
136 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
137 } else {
138 /* tx or rx causes */
139 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
140 index = ((16 * (queue & 1)) + (8 * direction));
141 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
142 ivar &= ~(0xFF << index);
143 ivar |= (msix_vector << index);
144 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
145 }
146}
147
Alexander Duyck70a10e22012-05-11 08:33:21 +0000148static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
Greg Rose92915f72010-01-09 02:24:10 +0000149 struct ixgbevf_tx_buffer
150 *tx_buffer_info)
151{
152 if (tx_buffer_info->dma) {
153 if (tx_buffer_info->mapped_as_page)
Alexander Duyck70a10e22012-05-11 08:33:21 +0000154 dma_unmap_page(tx_ring->dev,
Greg Rose92915f72010-01-09 02:24:10 +0000155 tx_buffer_info->dma,
156 tx_buffer_info->length,
Nick Nunley2a1f8792010-04-27 13:10:50 +0000157 DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000158 else
Alexander Duyck70a10e22012-05-11 08:33:21 +0000159 dma_unmap_single(tx_ring->dev,
Greg Rose92915f72010-01-09 02:24:10 +0000160 tx_buffer_info->dma,
161 tx_buffer_info->length,
Nick Nunley2a1f8792010-04-27 13:10:50 +0000162 DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000163 tx_buffer_info->dma = 0;
164 }
165 if (tx_buffer_info->skb) {
166 dev_kfree_skb_any(tx_buffer_info->skb);
167 tx_buffer_info->skb = NULL;
168 }
169 tx_buffer_info->time_stamp = 0;
170 /* tx_buffer_info must be completely set up in the transmit path */
171}
172
Greg Rose92915f72010-01-09 02:24:10 +0000173#define IXGBE_MAX_TXD_PWR 14
174#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
175
176/* Tx Descriptors needed, worst case */
Alexander Duyck35959902012-05-11 08:32:40 +0000177#define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
178#define DESC_NEEDED (MAX_SKB_FRAGS + 4)
Greg Rose92915f72010-01-09 02:24:10 +0000179
180static void ixgbevf_tx_timeout(struct net_device *netdev);
181
182/**
183 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000184 * @q_vector: board private structure
Greg Rose92915f72010-01-09 02:24:10 +0000185 * @tx_ring: tx ring to clean
186 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000187static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
Greg Rose92915f72010-01-09 02:24:10 +0000188 struct ixgbevf_ring *tx_ring)
189{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000190 struct ixgbevf_adapter *adapter = q_vector->adapter;
Greg Rose92915f72010-01-09 02:24:10 +0000191 union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
192 struct ixgbevf_tx_buffer *tx_buffer_info;
193 unsigned int i, eop, count = 0;
194 unsigned int total_bytes = 0, total_packets = 0;
195
Alexander Duyck10cc1bd2012-07-16 23:44:48 +0000196 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
197 return true;
198
Greg Rose92915f72010-01-09 02:24:10 +0000199 i = tx_ring->next_to_clean;
200 eop = tx_ring->tx_buffer_info[i].next_to_watch;
Alexander Duyck908421f2012-05-11 08:33:00 +0000201 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
Greg Rose92915f72010-01-09 02:24:10 +0000202
203 while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000204 (count < tx_ring->count)) {
Greg Rose92915f72010-01-09 02:24:10 +0000205 bool cleaned = false;
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +0000206 rmb(); /* read buffer_info after eop_desc */
Greg Rose98b9e482011-06-03 03:53:24 +0000207 /* eop could change between read and DD-check */
208 if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
209 goto cont_loop;
Greg Rose92915f72010-01-09 02:24:10 +0000210 for ( ; !cleaned; count++) {
211 struct sk_buff *skb;
Alexander Duyck908421f2012-05-11 08:33:00 +0000212 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000213 tx_buffer_info = &tx_ring->tx_buffer_info[i];
214 cleaned = (i == eop);
215 skb = tx_buffer_info->skb;
216
217 if (cleaned && skb) {
218 unsigned int segs, bytecount;
219
220 /* gso_segs is currently only valid for tcp */
221 segs = skb_shinfo(skb)->gso_segs ?: 1;
222 /* multiply data chunks by size of headers */
223 bytecount = ((segs - 1) * skb_headlen(skb)) +
224 skb->len;
225 total_packets += segs;
226 total_bytes += bytecount;
227 }
228
Alexander Duyck70a10e22012-05-11 08:33:21 +0000229 ixgbevf_unmap_and_free_tx_resource(tx_ring,
Greg Rose92915f72010-01-09 02:24:10 +0000230 tx_buffer_info);
231
232 tx_desc->wb.status = 0;
233
234 i++;
235 if (i == tx_ring->count)
236 i = 0;
237 }
238
Greg Rose98b9e482011-06-03 03:53:24 +0000239cont_loop:
Greg Rose92915f72010-01-09 02:24:10 +0000240 eop = tx_ring->tx_buffer_info[i].next_to_watch;
Alexander Duyck908421f2012-05-11 08:33:00 +0000241 eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
Greg Rose92915f72010-01-09 02:24:10 +0000242 }
243
244 tx_ring->next_to_clean = i;
245
246#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
Alexander Duyckfb401952012-05-11 08:33:16 +0000247 if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
Greg Rose92915f72010-01-09 02:24:10 +0000248 (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
249 /* Make sure that anybody stopping the queue after this
250 * sees the new next_to_clean.
251 */
252 smp_mb();
Alexander Duyckfb401952012-05-11 08:33:16 +0000253 if (__netif_subqueue_stopped(tx_ring->netdev,
254 tx_ring->queue_index) &&
Greg Rose92915f72010-01-09 02:24:10 +0000255 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
Alexander Duyckfb401952012-05-11 08:33:16 +0000256 netif_wake_subqueue(tx_ring->netdev,
257 tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +0000258 ++adapter->restart_queue;
259 }
Greg Rose92915f72010-01-09 02:24:10 +0000260 }
261
Eric Dumazet4197aa72011-06-22 05:01:35 +0000262 u64_stats_update_begin(&tx_ring->syncp);
Greg Rose92915f72010-01-09 02:24:10 +0000263 tx_ring->total_bytes += total_bytes;
264 tx_ring->total_packets += total_packets;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000265 u64_stats_update_end(&tx_ring->syncp);
Greg Rose92915f72010-01-09 02:24:10 +0000266
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000267 return count < tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000268}
269
270/**
271 * ixgbevf_receive_skb - Send a completed packet up the stack
272 * @q_vector: structure containing interrupt and ring information
273 * @skb: packet to send up
274 * @status: hardware indication of status of receive
275 * @rx_ring: rx descriptor ring (for a specific queue) to setup
276 * @rx_desc: rx descriptor
277 **/
278static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
279 struct sk_buff *skb, u8 status,
280 struct ixgbevf_ring *ring,
281 union ixgbe_adv_rx_desc *rx_desc)
282{
283 struct ixgbevf_adapter *adapter = q_vector->adapter;
284 bool is_vlan = (status & IXGBE_RXD_STAT_VP);
Greg Rosedd1ed3b2011-08-27 02:06:25 +0000285 u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
Greg Rose92915f72010-01-09 02:24:10 +0000286
Pascal Bouchareine5d9a5332012-06-14 02:18:18 +0000287 if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
Jiri Pirkodadcd652011-07-21 03:25:09 +0000288 __vlan_hwaccel_put_tag(skb, tag);
Jiri Pirkodadcd652011-07-21 03:25:09 +0000289
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000290 napi_gro_receive(&q_vector->napi, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000291}
292
293/**
294 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
295 * @adapter: address of board private structure
296 * @status_err: hardware indication of status of receive
297 * @skb: skb currently being received and modified
298 **/
299static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
Alexander Duyckfb401952012-05-11 08:33:16 +0000300 struct ixgbevf_ring *ring,
Greg Rose92915f72010-01-09 02:24:10 +0000301 u32 status_err, struct sk_buff *skb)
302{
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700303 skb_checksum_none_assert(skb);
Greg Rose92915f72010-01-09 02:24:10 +0000304
305 /* Rx csum disabled */
Alexander Duyckfb401952012-05-11 08:33:16 +0000306 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Greg Rose92915f72010-01-09 02:24:10 +0000307 return;
308
309 /* if IP and error */
310 if ((status_err & IXGBE_RXD_STAT_IPCS) &&
311 (status_err & IXGBE_RXDADV_ERR_IPE)) {
312 adapter->hw_csum_rx_error++;
313 return;
314 }
315
316 if (!(status_err & IXGBE_RXD_STAT_L4CS))
317 return;
318
319 if (status_err & IXGBE_RXDADV_ERR_TCPE) {
320 adapter->hw_csum_rx_error++;
321 return;
322 }
323
324 /* It must be a TCP or UDP packet with a valid checksum */
325 skb->ip_summed = CHECKSUM_UNNECESSARY;
326 adapter->hw_csum_rx_good++;
327}
328
329/**
330 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
331 * @adapter: address of board private structure
332 **/
333static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
334 struct ixgbevf_ring *rx_ring,
335 int cleaned_count)
336{
337 struct pci_dev *pdev = adapter->pdev;
338 union ixgbe_adv_rx_desc *rx_desc;
339 struct ixgbevf_rx_buffer *bi;
340 struct sk_buff *skb;
Alexander Duyckfb401952012-05-11 08:33:16 +0000341 unsigned int i = rx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +0000342
Greg Rose92915f72010-01-09 02:24:10 +0000343 bi = &rx_ring->rx_buffer_info[i];
344
345 while (cleaned_count--) {
Alexander Duyck908421f2012-05-11 08:33:00 +0000346 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000347 skb = bi->skb;
348 if (!skb) {
Alexander Duyckfb401952012-05-11 08:33:16 +0000349 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
350 rx_ring->rx_buf_len);
Greg Rose92915f72010-01-09 02:24:10 +0000351 if (!skb) {
352 adapter->alloc_rx_buff_failed++;
353 goto no_buffers;
354 }
Greg Rose92915f72010-01-09 02:24:10 +0000355 bi->skb = skb;
356 }
357 if (!bi->dma) {
Nick Nunley2a1f8792010-04-27 13:10:50 +0000358 bi->dma = dma_map_single(&pdev->dev, skb->data,
Greg Rose92915f72010-01-09 02:24:10 +0000359 rx_ring->rx_buf_len,
Nick Nunley2a1f8792010-04-27 13:10:50 +0000360 DMA_FROM_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000361 }
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000362 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
Greg Rose92915f72010-01-09 02:24:10 +0000363
364 i++;
365 if (i == rx_ring->count)
366 i = 0;
367 bi = &rx_ring->rx_buffer_info[i];
368 }
369
370no_buffers:
371 if (rx_ring->next_to_use != i) {
372 rx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +0000373
374 ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
375 }
376}
377
378static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000379 u32 qmask)
Greg Rose92915f72010-01-09 02:24:10 +0000380{
Greg Rose92915f72010-01-09 02:24:10 +0000381 struct ixgbe_hw *hw = &adapter->hw;
382
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000383 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
Greg Rose92915f72010-01-09 02:24:10 +0000384}
385
Greg Rose92915f72010-01-09 02:24:10 +0000386static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
387 struct ixgbevf_ring *rx_ring,
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000388 int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000389{
390 struct ixgbevf_adapter *adapter = q_vector->adapter;
391 struct pci_dev *pdev = adapter->pdev;
392 union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
393 struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
394 struct sk_buff *skb;
395 unsigned int i;
396 u32 len, staterr;
Greg Rose92915f72010-01-09 02:24:10 +0000397 int cleaned_count = 0;
398 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
399
400 i = rx_ring->next_to_clean;
Alexander Duyck908421f2012-05-11 08:33:00 +0000401 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000402 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
403 rx_buffer_info = &rx_ring->rx_buffer_info[i];
404
405 while (staterr & IXGBE_RXD_STAT_DD) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000406 if (!budget)
Greg Rose92915f72010-01-09 02:24:10 +0000407 break;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000408 budget--;
Greg Rose92915f72010-01-09 02:24:10 +0000409
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +0000410 rmb(); /* read descriptor and rx_buffer_info after status DD */
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000411 len = le16_to_cpu(rx_desc->wb.upper.length);
Greg Rose92915f72010-01-09 02:24:10 +0000412 skb = rx_buffer_info->skb;
413 prefetch(skb->data - NET_IP_ALIGN);
414 rx_buffer_info->skb = NULL;
415
416 if (rx_buffer_info->dma) {
Nick Nunley2a1f8792010-04-27 13:10:50 +0000417 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
Greg Rose92915f72010-01-09 02:24:10 +0000418 rx_ring->rx_buf_len,
Nick Nunley2a1f8792010-04-27 13:10:50 +0000419 DMA_FROM_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000420 rx_buffer_info->dma = 0;
421 skb_put(skb, len);
422 }
423
Greg Rose92915f72010-01-09 02:24:10 +0000424 i++;
425 if (i == rx_ring->count)
426 i = 0;
427
Alexander Duyck908421f2012-05-11 08:33:00 +0000428 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000429 prefetch(next_rxd);
430 cleaned_count++;
431
432 next_buffer = &rx_ring->rx_buffer_info[i];
433
434 if (!(staterr & IXGBE_RXD_STAT_EOP)) {
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000435 skb->next = next_buffer->skb;
436 skb->next->prev = skb;
Greg Rose92915f72010-01-09 02:24:10 +0000437 adapter->non_eop_descs++;
438 goto next_desc;
439 }
440
441 /* ERR_MASK will only have valid bits if EOP set */
442 if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
443 dev_kfree_skb_irq(skb);
444 goto next_desc;
445 }
446
Alexander Duyckfb401952012-05-11 08:33:16 +0000447 ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000448
449 /* probably a little skewed due to removing CRC */
450 total_rx_bytes += skb->len;
451 total_rx_packets++;
452
453 /*
454 * Work around issue of some types of VM to VM loop back
455 * packets not getting split correctly
456 */
457 if (staterr & IXGBE_RXD_STAT_LB) {
Eric Dumazete743d312010-04-14 15:59:40 -0700458 u32 header_fixup_len = skb_headlen(skb);
Greg Rose92915f72010-01-09 02:24:10 +0000459 if (header_fixup_len < 14)
460 skb_push(skb, header_fixup_len);
461 }
Alexander Duyckfb401952012-05-11 08:33:16 +0000462 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Greg Rose92915f72010-01-09 02:24:10 +0000463
464 ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
Greg Rose92915f72010-01-09 02:24:10 +0000465
466next_desc:
467 rx_desc->wb.upper.status_error = 0;
468
469 /* return some buffers to hardware, one at a time is too slow */
470 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
471 ixgbevf_alloc_rx_buffers(adapter, rx_ring,
472 cleaned_count);
473 cleaned_count = 0;
474 }
475
476 /* use prefetched values */
477 rx_desc = next_rxd;
478 rx_buffer_info = &rx_ring->rx_buffer_info[i];
479
480 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
481 }
482
483 rx_ring->next_to_clean = i;
484 cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
485
486 if (cleaned_count)
487 ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
488
Eric Dumazet4197aa72011-06-22 05:01:35 +0000489 u64_stats_update_begin(&rx_ring->syncp);
Greg Rose92915f72010-01-09 02:24:10 +0000490 rx_ring->total_packets += total_rx_packets;
491 rx_ring->total_bytes += total_rx_bytes;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000492 u64_stats_update_end(&rx_ring->syncp);
Greg Rose92915f72010-01-09 02:24:10 +0000493
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000494 return !!budget;
Greg Rose92915f72010-01-09 02:24:10 +0000495}
496
497/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000498 * ixgbevf_poll - NAPI polling calback
Greg Rose92915f72010-01-09 02:24:10 +0000499 * @napi: napi struct with our devices info in it
500 * @budget: amount of work driver is allowed to do this pass, in packets
501 *
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000502 * This function will clean more than one or more rings associated with a
Greg Rose92915f72010-01-09 02:24:10 +0000503 * q_vector.
504 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000505static int ixgbevf_poll(struct napi_struct *napi, int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000506{
507 struct ixgbevf_q_vector *q_vector =
508 container_of(napi, struct ixgbevf_q_vector, napi);
509 struct ixgbevf_adapter *adapter = q_vector->adapter;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000510 struct ixgbevf_ring *ring;
511 int per_ring_budget;
512 bool clean_complete = true;
513
514 ixgbevf_for_each_ring(ring, q_vector->tx)
515 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
Greg Rose92915f72010-01-09 02:24:10 +0000516
517 /* attempt to distribute budget to each queue fairly, but don't allow
518 * the budget to go below 1 because we'll exit polling */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000519 if (q_vector->rx.count > 1)
520 per_ring_budget = max(budget/q_vector->rx.count, 1);
521 else
522 per_ring_budget = budget;
Greg Rose92915f72010-01-09 02:24:10 +0000523
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000524 ixgbevf_for_each_ring(ring, q_vector->rx)
525 clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
526 per_ring_budget);
Greg Rose92915f72010-01-09 02:24:10 +0000527
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000528 /* If all work not completed, return budget and keep polling */
529 if (!clean_complete)
530 return budget;
531 /* all work done, exit the polling mode */
532 napi_complete(napi);
533 if (adapter->rx_itr_setting & 1)
534 ixgbevf_set_itr(q_vector);
535 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
536 ixgbevf_irq_enable_queues(adapter,
537 1 << q_vector->v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000538
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000539 return 0;
Greg Rose92915f72010-01-09 02:24:10 +0000540}
541
Greg Rosece422602012-05-22 02:17:49 +0000542/**
543 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
544 * @q_vector: structure containing interrupt and ring information
545 */
546static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
547{
548 struct ixgbevf_adapter *adapter = q_vector->adapter;
549 struct ixgbe_hw *hw = &adapter->hw;
550 int v_idx = q_vector->v_idx;
551 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
552
553 /*
554 * set the WDIS bit to not clear the timer bits and cause an
555 * immediate assertion of the interrupt
556 */
557 itr_reg |= IXGBE_EITR_CNT_WDIS;
558
559 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
560}
Greg Rose92915f72010-01-09 02:24:10 +0000561
562/**
563 * ixgbevf_configure_msix - Configure MSI-X hardware
564 * @adapter: board private structure
565 *
566 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
567 * interrupts.
568 **/
569static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
570{
571 struct ixgbevf_q_vector *q_vector;
Alexander Duyck6b43c442012-05-11 08:32:45 +0000572 int q_vectors, v_idx;
Greg Rose92915f72010-01-09 02:24:10 +0000573
574 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000575 adapter->eims_enable_mask = 0;
Greg Rose92915f72010-01-09 02:24:10 +0000576
577 /*
578 * Populate the IVAR table and set the ITR values to the
579 * corresponding register.
580 */
581 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
Alexander Duyck6b43c442012-05-11 08:32:45 +0000582 struct ixgbevf_ring *ring;
Greg Rose92915f72010-01-09 02:24:10 +0000583 q_vector = adapter->q_vector[v_idx];
Greg Rose92915f72010-01-09 02:24:10 +0000584
Alexander Duyck6b43c442012-05-11 08:32:45 +0000585 ixgbevf_for_each_ring(ring, q_vector->rx)
586 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000587
Alexander Duyck6b43c442012-05-11 08:32:45 +0000588 ixgbevf_for_each_ring(ring, q_vector->tx)
589 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000590
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000591 if (q_vector->tx.ring && !q_vector->rx.ring) {
592 /* tx only vector */
593 if (adapter->tx_itr_setting == 1)
594 q_vector->itr = IXGBE_10K_ITR;
595 else
596 q_vector->itr = adapter->tx_itr_setting;
597 } else {
598 /* rx or rx/tx vector */
599 if (adapter->rx_itr_setting == 1)
600 q_vector->itr = IXGBE_20K_ITR;
601 else
602 q_vector->itr = adapter->rx_itr_setting;
603 }
Greg Rose92915f72010-01-09 02:24:10 +0000604
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000605 /* add q_vector eims value to global eims_enable_mask */
606 adapter->eims_enable_mask |= 1 << v_idx;
607
608 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000609 }
610
611 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000612 /* setup eims_other and add value to global eims_enable_mask */
613 adapter->eims_other = 1 << v_idx;
614 adapter->eims_enable_mask |= adapter->eims_other;
Greg Rose92915f72010-01-09 02:24:10 +0000615}
616
617enum latency_range {
618 lowest_latency = 0,
619 low_latency = 1,
620 bulk_latency = 2,
621 latency_invalid = 255
622};
623
624/**
625 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000626 * @q_vector: structure containing interrupt and ring information
627 * @ring_container: structure containing ring performance data
Greg Rose92915f72010-01-09 02:24:10 +0000628 *
629 * Stores a new ITR value based on packets and byte
630 * counts during the last interrupt. The advantage of per interrupt
631 * computation is faster updates and more accurate ITR for the current
632 * traffic pattern. Constants in this function were computed
633 * based on theoretical maximum wire speed and thresholds were set based
634 * on testing data as well as attempting to minimize response time
635 * while increasing bulk throughput.
636 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000637static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
638 struct ixgbevf_ring_container *ring_container)
Greg Rose92915f72010-01-09 02:24:10 +0000639{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000640 int bytes = ring_container->total_bytes;
641 int packets = ring_container->total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000642 u32 timepassed_us;
643 u64 bytes_perint;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000644 u8 itr_setting = ring_container->itr;
Greg Rose92915f72010-01-09 02:24:10 +0000645
646 if (packets == 0)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000647 return;
Greg Rose92915f72010-01-09 02:24:10 +0000648
649 /* simple throttlerate management
650 * 0-20MB/s lowest (100000 ints/s)
651 * 20-100MB/s low (20000 ints/s)
652 * 100-1249MB/s bulk (8000 ints/s)
653 */
654 /* what was last interrupt timeslice? */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000655 timepassed_us = q_vector->itr >> 2;
Greg Rose92915f72010-01-09 02:24:10 +0000656 bytes_perint = bytes / timepassed_us; /* bytes/usec */
657
658 switch (itr_setting) {
659 case lowest_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000660 if (bytes_perint > 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000661 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000662 break;
663 case low_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000664 if (bytes_perint > 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000665 itr_setting = bulk_latency;
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000666 else if (bytes_perint <= 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000667 itr_setting = lowest_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000668 break;
669 case bulk_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000670 if (bytes_perint <= 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000671 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000672 break;
673 }
674
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000675 /* clear work counters since we have the values we need */
676 ring_container->total_bytes = 0;
677 ring_container->total_packets = 0;
678
679 /* write updated itr to ring container */
680 ring_container->itr = itr_setting;
Greg Rose92915f72010-01-09 02:24:10 +0000681}
682
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000683static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
Greg Rose92915f72010-01-09 02:24:10 +0000684{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000685 u32 new_itr = q_vector->itr;
686 u8 current_itr;
Greg Rose92915f72010-01-09 02:24:10 +0000687
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000688 ixgbevf_update_itr(q_vector, &q_vector->tx);
689 ixgbevf_update_itr(q_vector, &q_vector->rx);
Greg Rose92915f72010-01-09 02:24:10 +0000690
Alexander Duyck6b43c442012-05-11 08:32:45 +0000691 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Greg Rose92915f72010-01-09 02:24:10 +0000692
693 switch (current_itr) {
694 /* counts and packets in update_itr are dependent on these numbers */
695 case lowest_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000696 new_itr = IXGBE_100K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000697 break;
698 case low_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000699 new_itr = IXGBE_20K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000700 break;
701 case bulk_latency:
702 default:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000703 new_itr = IXGBE_8K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000704 break;
705 }
706
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000707 if (new_itr != q_vector->itr) {
Greg Rose92915f72010-01-09 02:24:10 +0000708 /* do an exponential smoothing */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000709 new_itr = (10 * new_itr * q_vector->itr) /
710 ((9 * new_itr) + q_vector->itr);
711
712 /* save the algorithm value here */
713 q_vector->itr = new_itr;
714
715 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000716 }
Greg Rose92915f72010-01-09 02:24:10 +0000717}
718
719static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
720{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000721 struct ixgbevf_adapter *adapter = data;
Greg Rose92915f72010-01-09 02:24:10 +0000722 struct ixgbe_hw *hw = &adapter->hw;
Greg Rosea9ee25a2010-01-22 22:47:00 +0000723 u32 msg;
Greg Rose375b27c2012-01-18 22:13:31 +0000724 bool got_ack = false;
Greg Rose92915f72010-01-09 02:24:10 +0000725
Greg Rose375b27c2012-01-18 22:13:31 +0000726 if (!hw->mbx.ops.check_for_ack(hw))
727 got_ack = true;
728
729 if (!hw->mbx.ops.check_for_msg(hw)) {
730 hw->mbx.ops.read(hw, &msg, 1);
731
732 if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
733 mod_timer(&adapter->watchdog_timer,
734 round_jiffies(jiffies + 1));
735
736 if (msg & IXGBE_VT_MSGTYPE_NACK)
737 pr_warn("Last Request of type %2.2x to PF Nacked\n",
738 msg & 0xFF);
Greg Rose3a2c4032012-02-01 01:28:15 +0000739 /*
740 * Restore the PFSTS bit in case someone is polling for a
741 * return message from the PF
742 */
743 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
Greg Rose08259592010-05-05 19:57:49 +0000744 }
745
Greg Rose375b27c2012-01-18 22:13:31 +0000746 /*
747 * checking for the ack clears the PFACK bit. Place
748 * it back in the v2p_mailbox cache so that anyone
749 * polling for an ack will not miss it
750 */
751 if (got_ack)
752 hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
Greg Rose3a2c4032012-02-01 01:28:15 +0000753
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000754 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
755
Greg Rose92915f72010-01-09 02:24:10 +0000756 return IRQ_HANDLED;
757}
758
Greg Rose92915f72010-01-09 02:24:10 +0000759
760/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000761 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
Greg Rose92915f72010-01-09 02:24:10 +0000762 * @irq: unused
763 * @data: pointer to our q_vector struct for this interrupt vector
764 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000765static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +0000766{
767 struct ixgbevf_q_vector *q_vector = data;
Greg Rose92915f72010-01-09 02:24:10 +0000768
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000769 /* EIAM disabled interrupts (on this vector) for us */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000770 if (q_vector->rx.ring || q_vector->tx.ring)
771 napi_schedule(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +0000772
773 return IRQ_HANDLED;
774}
775
776static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
777 int r_idx)
778{
779 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
780
Alexander Duyck6b43c442012-05-11 08:32:45 +0000781 a->rx_ring[r_idx].next = q_vector->rx.ring;
782 q_vector->rx.ring = &a->rx_ring[r_idx];
783 q_vector->rx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000784}
785
786static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
787 int t_idx)
788{
789 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
790
Alexander Duyck6b43c442012-05-11 08:32:45 +0000791 a->tx_ring[t_idx].next = q_vector->tx.ring;
792 q_vector->tx.ring = &a->tx_ring[t_idx];
793 q_vector->tx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000794}
795
796/**
797 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
798 * @adapter: board private structure to initialize
799 *
800 * This function maps descriptor rings to the queue-specific vectors
801 * we were allotted through the MSI-X enabling code. Ideally, we'd have
802 * one vector per ring/queue, but on a constrained vector budget, we
803 * group the rings as "efficiently" as possible. You would add new
804 * mapping configurations in here.
805 **/
806static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
807{
808 int q_vectors;
809 int v_start = 0;
810 int rxr_idx = 0, txr_idx = 0;
811 int rxr_remaining = adapter->num_rx_queues;
812 int txr_remaining = adapter->num_tx_queues;
813 int i, j;
814 int rqpv, tqpv;
815 int err = 0;
816
817 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
818
819 /*
820 * The ideal configuration...
821 * We have enough vectors to map one per queue.
822 */
823 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
824 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
825 map_vector_to_rxq(adapter, v_start, rxr_idx);
826
827 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
828 map_vector_to_txq(adapter, v_start, txr_idx);
829 goto out;
830 }
831
832 /*
833 * If we don't have enough vectors for a 1-to-1
834 * mapping, we'll have to group them so there are
835 * multiple queues per vector.
836 */
837 /* Re-adjusting *qpv takes care of the remainder. */
838 for (i = v_start; i < q_vectors; i++) {
839 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
840 for (j = 0; j < rqpv; j++) {
841 map_vector_to_rxq(adapter, i, rxr_idx);
842 rxr_idx++;
843 rxr_remaining--;
844 }
845 }
846 for (i = v_start; i < q_vectors; i++) {
847 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
848 for (j = 0; j < tqpv; j++) {
849 map_vector_to_txq(adapter, i, txr_idx);
850 txr_idx++;
851 txr_remaining--;
852 }
853 }
854
855out:
856 return err;
857}
858
859/**
860 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
861 * @adapter: board private structure
862 *
863 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
864 * interrupts from the kernel.
865 **/
866static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
867{
868 struct net_device *netdev = adapter->netdev;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000869 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
870 int vector, err;
Greg Rose92915f72010-01-09 02:24:10 +0000871 int ri = 0, ti = 0;
872
Greg Rose92915f72010-01-09 02:24:10 +0000873 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000874 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
875 struct msix_entry *entry = &adapter->msix_entries[vector];
Greg Rose92915f72010-01-09 02:24:10 +0000876
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000877 if (q_vector->tx.ring && q_vector->rx.ring) {
878 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
879 "%s-%s-%d", netdev->name, "TxRx", ri++);
880 ti++;
881 } else if (q_vector->rx.ring) {
882 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
883 "%s-%s-%d", netdev->name, "rx", ri++);
884 } else if (q_vector->tx.ring) {
885 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
886 "%s-%s-%d", netdev->name, "tx", ti++);
Greg Rose92915f72010-01-09 02:24:10 +0000887 } else {
888 /* skip this unused q_vector */
889 continue;
890 }
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000891 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
892 q_vector->name, q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000893 if (err) {
894 hw_dbg(&adapter->hw,
895 "request_irq failed for MSIX interrupt "
896 "Error: %d\n", err);
897 goto free_queue_irqs;
898 }
899 }
900
Greg Rose92915f72010-01-09 02:24:10 +0000901 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000902 &ixgbevf_msix_mbx, 0, netdev->name, adapter);
Greg Rose92915f72010-01-09 02:24:10 +0000903 if (err) {
904 hw_dbg(&adapter->hw,
905 "request_irq for msix_mbx failed: %d\n", err);
906 goto free_queue_irqs;
907 }
908
909 return 0;
910
911free_queue_irqs:
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000912 while (vector) {
913 vector--;
914 free_irq(adapter->msix_entries[vector].vector,
915 adapter->q_vector[vector]);
916 }
Greg Rose92915f72010-01-09 02:24:10 +0000917 pci_disable_msix(adapter->pdev);
918 kfree(adapter->msix_entries);
919 adapter->msix_entries = NULL;
920 return err;
921}
922
923static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
924{
925 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
926
927 for (i = 0; i < q_vectors; i++) {
928 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +0000929 q_vector->rx.ring = NULL;
930 q_vector->tx.ring = NULL;
931 q_vector->rx.count = 0;
932 q_vector->tx.count = 0;
Greg Rose92915f72010-01-09 02:24:10 +0000933 }
934}
935
936/**
937 * ixgbevf_request_irq - initialize interrupts
938 * @adapter: board private structure
939 *
940 * Attempts to configure interrupts using the best available
941 * capabilities of the hardware and kernel.
942 **/
943static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
944{
945 int err = 0;
946
947 err = ixgbevf_request_msix_irqs(adapter);
948
949 if (err)
950 hw_dbg(&adapter->hw,
951 "request_irq failed, Error %d\n", err);
952
953 return err;
954}
955
956static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
957{
Greg Rose92915f72010-01-09 02:24:10 +0000958 int i, q_vectors;
959
960 q_vectors = adapter->num_msix_vectors;
Greg Rose92915f72010-01-09 02:24:10 +0000961 i = q_vectors - 1;
962
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000963 free_irq(adapter->msix_entries[i].vector, adapter);
Greg Rose92915f72010-01-09 02:24:10 +0000964 i--;
965
966 for (; i >= 0; i--) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000967 /* free only the irqs that were actually requested */
968 if (!adapter->q_vector[i]->rx.ring &&
969 !adapter->q_vector[i]->tx.ring)
970 continue;
971
Greg Rose92915f72010-01-09 02:24:10 +0000972 free_irq(adapter->msix_entries[i].vector,
973 adapter->q_vector[i]);
974 }
975
976 ixgbevf_reset_q_vectors(adapter);
977}
978
979/**
980 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
981 * @adapter: board private structure
982 **/
983static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
984{
Greg Rose92915f72010-01-09 02:24:10 +0000985 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000986 int i;
Greg Rose92915f72010-01-09 02:24:10 +0000987
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000988 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
Greg Rose92915f72010-01-09 02:24:10 +0000989 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000990 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
Greg Rose92915f72010-01-09 02:24:10 +0000991
992 IXGBE_WRITE_FLUSH(hw);
993
994 for (i = 0; i < adapter->num_msix_vectors; i++)
995 synchronize_irq(adapter->msix_entries[i].vector);
996}
997
998/**
999 * ixgbevf_irq_enable - Enable default interrupt generation settings
1000 * @adapter: board private structure
1001 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001002static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001003{
1004 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001005
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001006 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1007 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1008 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
Greg Rose92915f72010-01-09 02:24:10 +00001009}
1010
1011/**
1012 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1013 * @adapter: board private structure
1014 *
1015 * Configure the Tx unit of the MAC after a reset.
1016 **/
1017static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1018{
1019 u64 tdba;
1020 struct ixgbe_hw *hw = &adapter->hw;
1021 u32 i, j, tdlen, txctrl;
1022
1023 /* Setup the HW Tx Head and Tail descriptor pointers */
1024 for (i = 0; i < adapter->num_tx_queues; i++) {
1025 struct ixgbevf_ring *ring = &adapter->tx_ring[i];
1026 j = ring->reg_idx;
1027 tdba = ring->dma;
1028 tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
1029 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1030 (tdba & DMA_BIT_MASK(32)));
1031 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1032 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
1033 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
1034 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
1035 adapter->tx_ring[i].head = IXGBE_VFTDH(j);
1036 adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
1037 /* Disable Tx Head Writeback RO bit, since this hoses
1038 * bookkeeping if things aren't delivered in order.
1039 */
1040 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1041 txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
1042 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1043 }
1044}
1045
1046#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1047
1048static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1049{
1050 struct ixgbevf_ring *rx_ring;
1051 struct ixgbe_hw *hw = &adapter->hw;
1052 u32 srrctl;
1053
1054 rx_ring = &adapter->rx_ring[index];
1055
1056 srrctl = IXGBE_SRRCTL_DROP_EN;
1057
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001058 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
Greg Rose92915f72010-01-09 02:24:10 +00001059
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001060 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1061 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1062
Greg Rose92915f72010-01-09 02:24:10 +00001063 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1064}
1065
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001066static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1067{
1068 struct ixgbe_hw *hw = &adapter->hw;
1069 struct net_device *netdev = adapter->netdev;
1070 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1071 int i;
1072 u16 rx_buf_len;
1073
1074 /* notify the PF of our intent to use this size of frame */
1075 ixgbevf_rlpml_set_vf(hw, max_frame);
1076
1077 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1078 max_frame += VLAN_HLEN;
1079
1080 /*
1081 * Make best use of allocation by using all but 1K of a
1082 * power of 2 allocation that will be used for skb->head.
1083 */
1084 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1085 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1086 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
1087 else if (max_frame <= IXGBEVF_RXBUFFER_3K)
1088 rx_buf_len = IXGBEVF_RXBUFFER_3K;
1089 else if (max_frame <= IXGBEVF_RXBUFFER_7K)
1090 rx_buf_len = IXGBEVF_RXBUFFER_7K;
1091 else if (max_frame <= IXGBEVF_RXBUFFER_15K)
1092 rx_buf_len = IXGBEVF_RXBUFFER_15K;
1093 else
1094 rx_buf_len = IXGBEVF_MAX_RXBUFFER;
1095
1096 for (i = 0; i < adapter->num_rx_queues; i++)
1097 adapter->rx_ring[i].rx_buf_len = rx_buf_len;
1098}
1099
Greg Rose92915f72010-01-09 02:24:10 +00001100/**
1101 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1102 * @adapter: board private structure
1103 *
1104 * Configure the Rx unit of the MAC after a reset.
1105 **/
1106static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1107{
1108 u64 rdba;
1109 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001110 int i, j;
1111 u32 rdlen;
Greg Rose92915f72010-01-09 02:24:10 +00001112
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001113 /* PSRTYPE must be initialized in 82599 */
1114 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001115
1116 /* set_rx_buffer_len must be called before ring initialization */
1117 ixgbevf_set_rx_buffer_len(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001118
1119 rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
1120 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1121 * the Base and Length of the Rx Descriptor Ring */
1122 for (i = 0; i < adapter->num_rx_queues; i++) {
1123 rdba = adapter->rx_ring[i].dma;
1124 j = adapter->rx_ring[i].reg_idx;
1125 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1126 (rdba & DMA_BIT_MASK(32)));
1127 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1128 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
1129 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
1130 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
1131 adapter->rx_ring[i].head = IXGBE_VFRDH(j);
1132 adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
Greg Rose92915f72010-01-09 02:24:10 +00001133
1134 ixgbevf_configure_srrctl(adapter, j);
1135 }
1136}
1137
Jiri Pirko8e586132011-12-08 19:52:37 -05001138static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001139{
1140 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1141 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001142
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001143 spin_lock(&adapter->mbx_lock);
1144
Greg Rose92915f72010-01-09 02:24:10 +00001145 /* add VID to filter table */
1146 if (hw->mac.ops.set_vfta)
1147 hw->mac.ops.set_vfta(hw, vid, 0, true);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001148
1149 spin_unlock(&adapter->mbx_lock);
1150
Jiri Pirkodadcd652011-07-21 03:25:09 +00001151 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001152
1153 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00001154}
1155
Jiri Pirko8e586132011-12-08 19:52:37 -05001156static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001157{
1158 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1159 struct ixgbe_hw *hw = &adapter->hw;
1160
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001161 spin_lock(&adapter->mbx_lock);
1162
Greg Rose92915f72010-01-09 02:24:10 +00001163 /* remove VID from filter table */
1164 if (hw->mac.ops.set_vfta)
1165 hw->mac.ops.set_vfta(hw, vid, 0, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001166
1167 spin_unlock(&adapter->mbx_lock);
1168
Jiri Pirkodadcd652011-07-21 03:25:09 +00001169 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001170
1171 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00001172}
1173
1174static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1175{
Jiri Pirkodadcd652011-07-21 03:25:09 +00001176 u16 vid;
Greg Rose92915f72010-01-09 02:24:10 +00001177
Jiri Pirkodadcd652011-07-21 03:25:09 +00001178 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1179 ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
Greg Rose92915f72010-01-09 02:24:10 +00001180}
1181
Greg Rose46ec20f2011-05-13 01:33:42 +00001182static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1183{
1184 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1185 struct ixgbe_hw *hw = &adapter->hw;
1186 int count = 0;
1187
1188 if ((netdev_uc_count(netdev)) > 10) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001189 pr_err("Too many unicast filters - No Space\n");
Greg Rose46ec20f2011-05-13 01:33:42 +00001190 return -ENOSPC;
1191 }
1192
1193 if (!netdev_uc_empty(netdev)) {
1194 struct netdev_hw_addr *ha;
1195 netdev_for_each_uc_addr(ha, netdev) {
1196 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1197 udelay(200);
1198 }
1199 } else {
1200 /*
1201 * If the list is empty then send message to PF driver to
1202 * clear all macvlans on this VF.
1203 */
1204 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1205 }
1206
1207 return count;
1208}
1209
Greg Rose92915f72010-01-09 02:24:10 +00001210/**
1211 * ixgbevf_set_rx_mode - Multicast set
1212 * @netdev: network interface device structure
1213 *
1214 * The set_rx_method entry point is called whenever the multicast address
1215 * list or the network interface flags are updated. This routine is
1216 * responsible for configuring the hardware for proper multicast mode.
1217 **/
1218static void ixgbevf_set_rx_mode(struct net_device *netdev)
1219{
1220 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1221 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001222
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001223 spin_lock(&adapter->mbx_lock);
1224
Greg Rose92915f72010-01-09 02:24:10 +00001225 /* reprogram multicast list */
Greg Rose92915f72010-01-09 02:24:10 +00001226 if (hw->mac.ops.update_mc_addr_list)
Jiri Pirko5c58c472010-03-23 22:58:20 +00001227 hw->mac.ops.update_mc_addr_list(hw, netdev);
Greg Rose46ec20f2011-05-13 01:33:42 +00001228
1229 ixgbevf_write_uc_addr_list(netdev);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001230
1231 spin_unlock(&adapter->mbx_lock);
Greg Rose92915f72010-01-09 02:24:10 +00001232}
1233
1234static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1235{
1236 int q_idx;
1237 struct ixgbevf_q_vector *q_vector;
1238 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1239
1240 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Greg Rose92915f72010-01-09 02:24:10 +00001241 q_vector = adapter->q_vector[q_idx];
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001242 napi_enable(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001243 }
1244}
1245
1246static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1247{
1248 int q_idx;
1249 struct ixgbevf_q_vector *q_vector;
1250 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1251
1252 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1253 q_vector = adapter->q_vector[q_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001254 napi_disable(&q_vector->napi);
1255 }
1256}
1257
1258static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1259{
1260 struct net_device *netdev = adapter->netdev;
1261 int i;
1262
1263 ixgbevf_set_rx_mode(netdev);
1264
1265 ixgbevf_restore_vlan(adapter);
1266
1267 ixgbevf_configure_tx(adapter);
1268 ixgbevf_configure_rx(adapter);
1269 for (i = 0; i < adapter->num_rx_queues; i++) {
1270 struct ixgbevf_ring *ring = &adapter->rx_ring[i];
Alexander Duyck18c63082012-05-11 08:33:11 +00001271 ixgbevf_alloc_rx_buffers(adapter, ring,
1272 IXGBE_DESC_UNUSED(ring));
Greg Rose92915f72010-01-09 02:24:10 +00001273 }
1274}
1275
1276#define IXGBE_MAX_RX_DESC_POLL 10
1277static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1278 int rxr)
1279{
1280 struct ixgbe_hw *hw = &adapter->hw;
1281 int j = adapter->rx_ring[rxr].reg_idx;
1282 int k;
1283
1284 for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
1285 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
1286 break;
1287 else
1288 msleep(1);
1289 }
1290 if (k >= IXGBE_MAX_RX_DESC_POLL) {
1291 hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
1292 "not set within the polling period\n", rxr);
1293 }
1294
1295 ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
1296 (adapter->rx_ring[rxr].count - 1));
1297}
1298
Greg Rose33bd9f62010-03-19 02:59:52 +00001299static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1300{
1301 /* Only save pre-reset stats if there are some */
1302 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1303 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1304 adapter->stats.base_vfgprc;
1305 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1306 adapter->stats.base_vfgptc;
1307 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1308 adapter->stats.base_vfgorc;
1309 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1310 adapter->stats.base_vfgotc;
1311 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1312 adapter->stats.base_vfmprc;
1313 }
1314}
1315
1316static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1317{
1318 struct ixgbe_hw *hw = &adapter->hw;
1319
1320 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1321 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1322 adapter->stats.last_vfgorc |=
1323 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1324 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1325 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1326 adapter->stats.last_vfgotc |=
1327 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1328 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1329
1330 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1331 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1332 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1333 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1334 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1335}
1336
Alexander Duyck31186782012-07-20 08:09:58 +00001337static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1338{
1339 struct ixgbe_hw *hw = &adapter->hw;
1340 int api[] = { ixgbe_mbox_api_10,
1341 ixgbe_mbox_api_unknown };
1342 int err = 0, idx = 0;
1343
1344 spin_lock(&adapter->mbx_lock);
1345
1346 while (api[idx] != ixgbe_mbox_api_unknown) {
1347 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1348 if (!err)
1349 break;
1350 idx++;
1351 }
1352
1353 spin_unlock(&adapter->mbx_lock);
1354}
1355
Greg Rose795180d2012-04-17 04:29:34 +00001356static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001357{
1358 struct net_device *netdev = adapter->netdev;
1359 struct ixgbe_hw *hw = &adapter->hw;
1360 int i, j = 0;
1361 int num_rx_rings = adapter->num_rx_queues;
1362 u32 txdctl, rxdctl;
1363
1364 for (i = 0; i < adapter->num_tx_queues; i++) {
1365 j = adapter->tx_ring[i].reg_idx;
1366 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1367 /* enable WTHRESH=8 descriptors, to encourage burst writeback */
1368 txdctl |= (8 << 16);
1369 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1370 }
1371
1372 for (i = 0; i < adapter->num_tx_queues; i++) {
1373 j = adapter->tx_ring[i].reg_idx;
1374 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1375 txdctl |= IXGBE_TXDCTL_ENABLE;
1376 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1377 }
1378
1379 for (i = 0; i < num_rx_rings; i++) {
1380 j = adapter->rx_ring[i].reg_idx;
1381 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
Jiri Pirkodadcd652011-07-21 03:25:09 +00001382 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
Greg Rose69bfbec2011-01-26 01:06:12 +00001383 if (hw->mac.type == ixgbe_mac_X540_vf) {
1384 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1385 rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
1386 IXGBE_RXDCTL_RLPML_EN);
1387 }
Greg Rose92915f72010-01-09 02:24:10 +00001388 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1389 ixgbevf_rx_desc_queue_enable(adapter, i);
1390 }
1391
1392 ixgbevf_configure_msix(adapter);
1393
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001394 spin_lock(&adapter->mbx_lock);
1395
Greg Rose92915f72010-01-09 02:24:10 +00001396 if (hw->mac.ops.set_rar) {
1397 if (is_valid_ether_addr(hw->mac.addr))
1398 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1399 else
1400 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
1401 }
1402
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001403 spin_unlock(&adapter->mbx_lock);
1404
Greg Rose92915f72010-01-09 02:24:10 +00001405 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1406 ixgbevf_napi_enable_all(adapter);
1407
1408 /* enable transmits */
1409 netif_tx_start_all_queues(netdev);
1410
Greg Rose33bd9f62010-03-19 02:59:52 +00001411 ixgbevf_save_reset_stats(adapter);
1412 ixgbevf_init_last_counter_stats(adapter);
1413
Greg Rose92915f72010-01-09 02:24:10 +00001414 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose92915f72010-01-09 02:24:10 +00001415}
1416
Greg Rose795180d2012-04-17 04:29:34 +00001417void ixgbevf_up(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001418{
Greg Rose92915f72010-01-09 02:24:10 +00001419 struct ixgbe_hw *hw = &adapter->hw;
1420
Alexander Duyck31186782012-07-20 08:09:58 +00001421 ixgbevf_negotiate_api(adapter);
1422
Greg Rose92915f72010-01-09 02:24:10 +00001423 ixgbevf_configure(adapter);
1424
Greg Rose795180d2012-04-17 04:29:34 +00001425 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001426
1427 /* clear any pending interrupts, may auto mask */
1428 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1429
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001430 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001431}
1432
1433/**
1434 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
1435 * @adapter: board private structure
1436 * @rx_ring: ring to free buffers from
1437 **/
1438static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
1439 struct ixgbevf_ring *rx_ring)
1440{
1441 struct pci_dev *pdev = adapter->pdev;
1442 unsigned long size;
1443 unsigned int i;
1444
Greg Rosec0456c22010-01-22 22:47:18 +00001445 if (!rx_ring->rx_buffer_info)
1446 return;
Greg Rose92915f72010-01-09 02:24:10 +00001447
Greg Rosec0456c22010-01-22 22:47:18 +00001448 /* Free all the Rx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00001449 for (i = 0; i < rx_ring->count; i++) {
1450 struct ixgbevf_rx_buffer *rx_buffer_info;
1451
1452 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1453 if (rx_buffer_info->dma) {
Nick Nunley2a1f8792010-04-27 13:10:50 +00001454 dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
Greg Rose92915f72010-01-09 02:24:10 +00001455 rx_ring->rx_buf_len,
Nick Nunley2a1f8792010-04-27 13:10:50 +00001456 DMA_FROM_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +00001457 rx_buffer_info->dma = 0;
1458 }
1459 if (rx_buffer_info->skb) {
1460 struct sk_buff *skb = rx_buffer_info->skb;
1461 rx_buffer_info->skb = NULL;
1462 do {
1463 struct sk_buff *this = skb;
1464 skb = skb->prev;
1465 dev_kfree_skb(this);
1466 } while (skb);
1467 }
Greg Rose92915f72010-01-09 02:24:10 +00001468 }
1469
1470 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1471 memset(rx_ring->rx_buffer_info, 0, size);
1472
1473 /* Zero out the descriptor ring */
1474 memset(rx_ring->desc, 0, rx_ring->size);
1475
1476 rx_ring->next_to_clean = 0;
1477 rx_ring->next_to_use = 0;
1478
1479 if (rx_ring->head)
1480 writel(0, adapter->hw.hw_addr + rx_ring->head);
1481 if (rx_ring->tail)
1482 writel(0, adapter->hw.hw_addr + rx_ring->tail);
1483}
1484
1485/**
1486 * ixgbevf_clean_tx_ring - Free Tx Buffers
1487 * @adapter: board private structure
1488 * @tx_ring: ring to be cleaned
1489 **/
1490static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
1491 struct ixgbevf_ring *tx_ring)
1492{
1493 struct ixgbevf_tx_buffer *tx_buffer_info;
1494 unsigned long size;
1495 unsigned int i;
1496
Greg Rosec0456c22010-01-22 22:47:18 +00001497 if (!tx_ring->tx_buffer_info)
1498 return;
1499
Greg Rose92915f72010-01-09 02:24:10 +00001500 /* Free all the Tx ring sk_buffs */
1501
1502 for (i = 0; i < tx_ring->count; i++) {
1503 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00001504 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00001505 }
1506
1507 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1508 memset(tx_ring->tx_buffer_info, 0, size);
1509
1510 memset(tx_ring->desc, 0, tx_ring->size);
1511
1512 tx_ring->next_to_use = 0;
1513 tx_ring->next_to_clean = 0;
1514
1515 if (tx_ring->head)
1516 writel(0, adapter->hw.hw_addr + tx_ring->head);
1517 if (tx_ring->tail)
1518 writel(0, adapter->hw.hw_addr + tx_ring->tail);
1519}
1520
1521/**
1522 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1523 * @adapter: board private structure
1524 **/
1525static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1526{
1527 int i;
1528
1529 for (i = 0; i < adapter->num_rx_queues; i++)
1530 ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1531}
1532
1533/**
1534 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1535 * @adapter: board private structure
1536 **/
1537static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1538{
1539 int i;
1540
1541 for (i = 0; i < adapter->num_tx_queues; i++)
1542 ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1543}
1544
1545void ixgbevf_down(struct ixgbevf_adapter *adapter)
1546{
1547 struct net_device *netdev = adapter->netdev;
1548 struct ixgbe_hw *hw = &adapter->hw;
1549 u32 txdctl;
1550 int i, j;
1551
1552 /* signal that we are down to the interrupt handler */
1553 set_bit(__IXGBEVF_DOWN, &adapter->state);
1554 /* disable receives */
1555
1556 netif_tx_disable(netdev);
1557
1558 msleep(10);
1559
1560 netif_tx_stop_all_queues(netdev);
1561
1562 ixgbevf_irq_disable(adapter);
1563
1564 ixgbevf_napi_disable_all(adapter);
1565
1566 del_timer_sync(&adapter->watchdog_timer);
1567 /* can't call flush scheduled work here because it can deadlock
1568 * if linkwatch_event tries to acquire the rtnl_lock which we are
1569 * holding */
1570 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1571 msleep(1);
1572
1573 /* disable transmits in the hardware now that interrupts are off */
1574 for (i = 0; i < adapter->num_tx_queues; i++) {
1575 j = adapter->tx_ring[i].reg_idx;
1576 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1577 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
1578 (txdctl & ~IXGBE_TXDCTL_ENABLE));
1579 }
1580
1581 netif_carrier_off(netdev);
1582
1583 if (!pci_channel_offline(adapter->pdev))
1584 ixgbevf_reset(adapter);
1585
1586 ixgbevf_clean_all_tx_rings(adapter);
1587 ixgbevf_clean_all_rx_rings(adapter);
1588}
1589
1590void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1591{
Greg Rosec0456c22010-01-22 22:47:18 +00001592 struct ixgbe_hw *hw = &adapter->hw;
1593
Greg Rose92915f72010-01-09 02:24:10 +00001594 WARN_ON(in_interrupt());
Greg Rosec0456c22010-01-22 22:47:18 +00001595
Greg Rose92915f72010-01-09 02:24:10 +00001596 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1597 msleep(1);
1598
Greg Rosec0456c22010-01-22 22:47:18 +00001599 /*
1600 * Check if PF is up before re-init. If not then skip until
1601 * later when the PF is up and ready to service requests from
1602 * the VF via mailbox. If the VF is up and running then the
1603 * watchdog task will continue to schedule reset tasks until
1604 * the PF is up and running.
1605 */
1606 if (!hw->mac.ops.reset_hw(hw)) {
1607 ixgbevf_down(adapter);
1608 ixgbevf_up(adapter);
1609 }
Greg Rose92915f72010-01-09 02:24:10 +00001610
1611 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1612}
1613
1614void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1615{
1616 struct ixgbe_hw *hw = &adapter->hw;
1617 struct net_device *netdev = adapter->netdev;
1618
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001619 spin_lock(&adapter->mbx_lock);
1620
Greg Rose92915f72010-01-09 02:24:10 +00001621 if (hw->mac.ops.reset_hw(hw))
1622 hw_dbg(hw, "PF still resetting\n");
1623 else
1624 hw->mac.ops.init_hw(hw);
1625
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001626 spin_unlock(&adapter->mbx_lock);
1627
Greg Rose92915f72010-01-09 02:24:10 +00001628 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1629 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1630 netdev->addr_len);
1631 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1632 netdev->addr_len);
1633 }
1634}
1635
1636static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1637 int vectors)
1638{
1639 int err, vector_threshold;
1640
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001641 /* We'll want at least 2 (vector_threshold):
1642 * 1) TxQ[0] + RxQ[0] handler
1643 * 2) Other (Link Status Change, etc.)
Greg Rose92915f72010-01-09 02:24:10 +00001644 */
1645 vector_threshold = MIN_MSIX_COUNT;
1646
1647 /* The more we get, the more we will assign to Tx/Rx Cleanup
1648 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1649 * Right now, we simply care about how many we'll get; we'll
1650 * set them up later while requesting irq's.
1651 */
1652 while (vectors >= vector_threshold) {
1653 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1654 vectors);
1655 if (!err) /* Success in acquiring all requested vectors. */
1656 break;
1657 else if (err < 0)
1658 vectors = 0; /* Nasty failure, quit now */
1659 else /* err == number of vectors we should try again with */
1660 vectors = err;
1661 }
1662
1663 if (vectors < vector_threshold) {
1664 /* Can't allocate enough MSI-X interrupts? Oh well.
1665 * This just means we'll go with either a single MSI
1666 * vector or fall back to legacy interrupts.
1667 */
1668 hw_dbg(&adapter->hw,
1669 "Unable to allocate MSI-X interrupts\n");
1670 kfree(adapter->msix_entries);
1671 adapter->msix_entries = NULL;
1672 } else {
1673 /*
1674 * Adjust for only the vectors we'll use, which is minimum
1675 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1676 * vectors we were allocated.
1677 */
1678 adapter->num_msix_vectors = vectors;
1679 }
1680}
1681
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001682/**
1683 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
Greg Rose92915f72010-01-09 02:24:10 +00001684 * @adapter: board private structure to initialize
1685 *
1686 * This is the top level queue allocation routine. The order here is very
1687 * important, starting with the "most" number of features turned on at once,
1688 * and ending with the smallest set of features. This way large combinations
1689 * can be allocated if they're turned on, and smaller combinations are the
1690 * fallthrough conditions.
1691 *
1692 **/
1693static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1694{
1695 /* Start with base case */
1696 adapter->num_rx_queues = 1;
1697 adapter->num_tx_queues = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001698}
1699
1700/**
1701 * ixgbevf_alloc_queues - Allocate memory for all rings
1702 * @adapter: board private structure to initialize
1703 *
1704 * We allocate one ring per queue at run-time since we don't know the
1705 * number of queues at compile-time. The polling_netdev array is
1706 * intended for Multiqueue, but should work fine with a single queue.
1707 **/
1708static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1709{
1710 int i;
1711
1712 adapter->tx_ring = kcalloc(adapter->num_tx_queues,
1713 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1714 if (!adapter->tx_ring)
1715 goto err_tx_ring_allocation;
1716
1717 adapter->rx_ring = kcalloc(adapter->num_rx_queues,
1718 sizeof(struct ixgbevf_ring), GFP_KERNEL);
1719 if (!adapter->rx_ring)
1720 goto err_rx_ring_allocation;
1721
1722 for (i = 0; i < adapter->num_tx_queues; i++) {
1723 adapter->tx_ring[i].count = adapter->tx_ring_count;
1724 adapter->tx_ring[i].queue_index = i;
1725 adapter->tx_ring[i].reg_idx = i;
Alexander Duyckfb401952012-05-11 08:33:16 +00001726 adapter->tx_ring[i].dev = &adapter->pdev->dev;
1727 adapter->tx_ring[i].netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00001728 }
1729
1730 for (i = 0; i < adapter->num_rx_queues; i++) {
1731 adapter->rx_ring[i].count = adapter->rx_ring_count;
1732 adapter->rx_ring[i].queue_index = i;
1733 adapter->rx_ring[i].reg_idx = i;
Alexander Duyckfb401952012-05-11 08:33:16 +00001734 adapter->rx_ring[i].dev = &adapter->pdev->dev;
1735 adapter->rx_ring[i].netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00001736 }
1737
1738 return 0;
1739
1740err_rx_ring_allocation:
1741 kfree(adapter->tx_ring);
1742err_tx_ring_allocation:
1743 return -ENOMEM;
1744}
1745
1746/**
1747 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
1748 * @adapter: board private structure to initialize
1749 *
1750 * Attempt to configure the interrupts using the best available
1751 * capabilities of the hardware and the kernel.
1752 **/
1753static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
1754{
1755 int err = 0;
1756 int vector, v_budget;
1757
1758 /*
1759 * It's easy to be greedy for MSI-X vectors, but it really
1760 * doesn't do us much good if we have a lot more vectors
1761 * than CPU's. So let's be conservative and only ask for
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001762 * (roughly) the same number of vectors as there are CPU's.
1763 * The default is to use pairs of vectors.
Greg Rose92915f72010-01-09 02:24:10 +00001764 */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001765 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
1766 v_budget = min_t(int, v_budget, num_online_cpus());
1767 v_budget += NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00001768
1769 /* A failure in MSI-X entry allocation isn't fatal, but it does
1770 * mean we disable MSI-X capabilities of the adapter. */
1771 adapter->msix_entries = kcalloc(v_budget,
1772 sizeof(struct msix_entry), GFP_KERNEL);
1773 if (!adapter->msix_entries) {
1774 err = -ENOMEM;
1775 goto out;
1776 }
1777
1778 for (vector = 0; vector < v_budget; vector++)
1779 adapter->msix_entries[vector].entry = vector;
1780
1781 ixgbevf_acquire_msix_vectors(adapter, v_budget);
1782
1783out:
1784 return err;
1785}
1786
1787/**
1788 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
1789 * @adapter: board private structure to initialize
1790 *
1791 * We allocate one q_vector per queue interrupt. If allocation fails we
1792 * return -ENOMEM.
1793 **/
1794static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
1795{
1796 int q_idx, num_q_vectors;
1797 struct ixgbevf_q_vector *q_vector;
Greg Rose92915f72010-01-09 02:24:10 +00001798
1799 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00001800
1801 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1802 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
1803 if (!q_vector)
1804 goto err_out;
1805 q_vector->adapter = adapter;
1806 q_vector->v_idx = q_idx;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001807 netif_napi_add(adapter->netdev, &q_vector->napi,
1808 ixgbevf_poll, 64);
Greg Rose92915f72010-01-09 02:24:10 +00001809 adapter->q_vector[q_idx] = q_vector;
1810 }
1811
1812 return 0;
1813
1814err_out:
1815 while (q_idx) {
1816 q_idx--;
1817 q_vector = adapter->q_vector[q_idx];
1818 netif_napi_del(&q_vector->napi);
1819 kfree(q_vector);
1820 adapter->q_vector[q_idx] = NULL;
1821 }
1822 return -ENOMEM;
1823}
1824
1825/**
1826 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
1827 * @adapter: board private structure to initialize
1828 *
1829 * This function frees the memory allocated to the q_vectors. In addition if
1830 * NAPI is enabled it will delete any references to the NAPI struct prior
1831 * to freeing the q_vector.
1832 **/
1833static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
1834{
1835 int q_idx, num_q_vectors;
1836 int napi_vectors;
1837
1838 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1839 napi_vectors = adapter->num_rx_queues;
1840
1841 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1842 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
1843
1844 adapter->q_vector[q_idx] = NULL;
1845 if (q_idx < napi_vectors)
1846 netif_napi_del(&q_vector->napi);
1847 kfree(q_vector);
1848 }
1849}
1850
1851/**
1852 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
1853 * @adapter: board private structure
1854 *
1855 **/
1856static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
1857{
1858 pci_disable_msix(adapter->pdev);
1859 kfree(adapter->msix_entries);
1860 adapter->msix_entries = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00001861}
1862
1863/**
1864 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
1865 * @adapter: board private structure to initialize
1866 *
1867 **/
1868static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
1869{
1870 int err;
1871
1872 /* Number of supported queues */
1873 ixgbevf_set_num_queues(adapter);
1874
1875 err = ixgbevf_set_interrupt_capability(adapter);
1876 if (err) {
1877 hw_dbg(&adapter->hw,
1878 "Unable to setup interrupt capabilities\n");
1879 goto err_set_interrupt;
1880 }
1881
1882 err = ixgbevf_alloc_q_vectors(adapter);
1883 if (err) {
1884 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
1885 "vectors\n");
1886 goto err_alloc_q_vectors;
1887 }
1888
1889 err = ixgbevf_alloc_queues(adapter);
1890 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001891 pr_err("Unable to allocate memory for queues\n");
Greg Rose92915f72010-01-09 02:24:10 +00001892 goto err_alloc_queues;
1893 }
1894
1895 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
1896 "Tx Queue count = %u\n",
1897 (adapter->num_rx_queues > 1) ? "Enabled" :
1898 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
1899
1900 set_bit(__IXGBEVF_DOWN, &adapter->state);
1901
1902 return 0;
1903err_alloc_queues:
1904 ixgbevf_free_q_vectors(adapter);
1905err_alloc_q_vectors:
1906 ixgbevf_reset_interrupt_capability(adapter);
1907err_set_interrupt:
1908 return err;
1909}
1910
1911/**
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00001912 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
1913 * @adapter: board private structure to clear interrupt scheme on
1914 *
1915 * We go through and clear interrupt specific resources and reset the structure
1916 * to pre-load conditions
1917 **/
1918static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
1919{
1920 adapter->num_tx_queues = 0;
1921 adapter->num_rx_queues = 0;
1922
1923 ixgbevf_free_q_vectors(adapter);
1924 ixgbevf_reset_interrupt_capability(adapter);
1925}
1926
1927/**
Greg Rose92915f72010-01-09 02:24:10 +00001928 * ixgbevf_sw_init - Initialize general software structures
1929 * (struct ixgbevf_adapter)
1930 * @adapter: board private structure to initialize
1931 *
1932 * ixgbevf_sw_init initializes the Adapter private data structure.
1933 * Fields are initialized based on PCI device information and
1934 * OS network device settings (MTU size).
1935 **/
1936static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
1937{
1938 struct ixgbe_hw *hw = &adapter->hw;
1939 struct pci_dev *pdev = adapter->pdev;
1940 int err;
1941
1942 /* PCI config space info */
1943
1944 hw->vendor_id = pdev->vendor;
1945 hw->device_id = pdev->device;
Sergei Shtylyovff938e42011-02-28 11:57:33 -08001946 hw->revision_id = pdev->revision;
Greg Rose92915f72010-01-09 02:24:10 +00001947 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1948 hw->subsystem_device_id = pdev->subsystem_device;
1949
1950 hw->mbx.ops.init_params(hw);
1951 hw->mac.max_tx_queues = MAX_TX_QUEUES;
1952 hw->mac.max_rx_queues = MAX_RX_QUEUES;
1953 err = hw->mac.ops.reset_hw(hw);
1954 if (err) {
1955 dev_info(&pdev->dev,
1956 "PF still in reset state, assigning new address\n");
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00001957 eth_hw_addr_random(adapter->netdev);
1958 memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
1959 adapter->netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00001960 } else {
1961 err = hw->mac.ops.init_hw(hw);
1962 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001963 pr_err("init_shared_code failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00001964 goto out;
1965 }
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00001966 memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
1967 adapter->netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00001968 }
1969
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001970 /* lock to protect mailbox accesses */
1971 spin_lock_init(&adapter->mbx_lock);
1972
Greg Rose92915f72010-01-09 02:24:10 +00001973 /* Enable dynamic interrupt throttling rates */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001974 adapter->rx_itr_setting = 1;
1975 adapter->tx_itr_setting = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001976
Greg Rose92915f72010-01-09 02:24:10 +00001977 /* set default ring sizes */
1978 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
1979 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
1980
Greg Rose92915f72010-01-09 02:24:10 +00001981 set_bit(__IXGBEVF_DOWN, &adapter->state);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00001982 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00001983
1984out:
1985 return err;
1986}
1987
Greg Rose92915f72010-01-09 02:24:10 +00001988#define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
1989 { \
1990 u32 current_counter = IXGBE_READ_REG(hw, reg); \
1991 if (current_counter < last_counter) \
1992 counter += 0x100000000LL; \
1993 last_counter = current_counter; \
1994 counter &= 0xFFFFFFFF00000000LL; \
1995 counter |= current_counter; \
1996 }
1997
1998#define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
1999 { \
2000 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2001 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2002 u64 current_counter = (current_counter_msb << 32) | \
2003 current_counter_lsb; \
2004 if (current_counter < last_counter) \
2005 counter += 0x1000000000LL; \
2006 last_counter = current_counter; \
2007 counter &= 0xFFFFFFF000000000LL; \
2008 counter |= current_counter; \
2009 }
2010/**
2011 * ixgbevf_update_stats - Update the board statistics counters.
2012 * @adapter: board private structure
2013 **/
2014void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2015{
2016 struct ixgbe_hw *hw = &adapter->hw;
2017
2018 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2019 adapter->stats.vfgprc);
2020 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2021 adapter->stats.vfgptc);
2022 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2023 adapter->stats.last_vfgorc,
2024 adapter->stats.vfgorc);
2025 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2026 adapter->stats.last_vfgotc,
2027 adapter->stats.vfgotc);
2028 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2029 adapter->stats.vfmprc);
Greg Rose92915f72010-01-09 02:24:10 +00002030}
2031
2032/**
2033 * ixgbevf_watchdog - Timer Call-back
2034 * @data: pointer to adapter cast into an unsigned long
2035 **/
2036static void ixgbevf_watchdog(unsigned long data)
2037{
2038 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2039 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002040 u32 eics = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002041 int i;
2042
2043 /*
2044 * Do the watchdog outside of interrupt context due to the lovely
2045 * delays that some of the newer hardware requires
2046 */
2047
2048 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2049 goto watchdog_short_circuit;
2050
2051 /* get one bit for every active tx/rx interrupt vector */
2052 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2053 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00002054 if (qv->rx.ring || qv->tx.ring)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002055 eics |= 1 << i;
Greg Rose92915f72010-01-09 02:24:10 +00002056 }
2057
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002058 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
Greg Rose92915f72010-01-09 02:24:10 +00002059
2060watchdog_short_circuit:
2061 schedule_work(&adapter->watchdog_task);
2062}
2063
2064/**
2065 * ixgbevf_tx_timeout - Respond to a Tx Hang
2066 * @netdev: network interface device structure
2067 **/
2068static void ixgbevf_tx_timeout(struct net_device *netdev)
2069{
2070 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2071
2072 /* Do the reset outside of interrupt context */
2073 schedule_work(&adapter->reset_task);
2074}
2075
2076static void ixgbevf_reset_task(struct work_struct *work)
2077{
2078 struct ixgbevf_adapter *adapter;
2079 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2080
2081 /* If we're already down or resetting, just bail */
2082 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2083 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2084 return;
2085
2086 adapter->tx_timeout_count++;
2087
2088 ixgbevf_reinit_locked(adapter);
2089}
2090
2091/**
2092 * ixgbevf_watchdog_task - worker thread to bring link up
2093 * @work: pointer to work_struct containing our data
2094 **/
2095static void ixgbevf_watchdog_task(struct work_struct *work)
2096{
2097 struct ixgbevf_adapter *adapter = container_of(work,
2098 struct ixgbevf_adapter,
2099 watchdog_task);
2100 struct net_device *netdev = adapter->netdev;
2101 struct ixgbe_hw *hw = &adapter->hw;
2102 u32 link_speed = adapter->link_speed;
2103 bool link_up = adapter->link_up;
2104
2105 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2106
2107 /*
2108 * Always check the link on the watchdog because we have
2109 * no LSC interrupt
2110 */
2111 if (hw->mac.ops.check_link) {
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002112 s32 need_reset;
2113
2114 spin_lock(&adapter->mbx_lock);
2115
2116 need_reset = hw->mac.ops.check_link(hw, &link_speed,
2117 &link_up, false);
2118
2119 spin_unlock(&adapter->mbx_lock);
2120
2121 if (need_reset) {
Greg Rose92915f72010-01-09 02:24:10 +00002122 adapter->link_up = link_up;
2123 adapter->link_speed = link_speed;
Greg Roseda6b3332010-01-22 22:47:37 +00002124 netif_carrier_off(netdev);
2125 netif_tx_stop_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002126 schedule_work(&adapter->reset_task);
2127 goto pf_has_reset;
2128 }
2129 } else {
2130 /* always assume link is up, if no check link
2131 * function */
2132 link_speed = IXGBE_LINK_SPEED_10GB_FULL;
2133 link_up = true;
2134 }
2135 adapter->link_up = link_up;
2136 adapter->link_speed = link_speed;
2137
2138 if (link_up) {
2139 if (!netif_carrier_ok(netdev)) {
Joe Perches300bc062010-03-22 20:08:04 -07002140 hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
2141 (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2142 10 : 1);
Greg Rose92915f72010-01-09 02:24:10 +00002143 netif_carrier_on(netdev);
2144 netif_tx_wake_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002145 }
2146 } else {
2147 adapter->link_up = false;
2148 adapter->link_speed = 0;
2149 if (netif_carrier_ok(netdev)) {
2150 hw_dbg(&adapter->hw, "NIC Link is Down\n");
2151 netif_carrier_off(netdev);
2152 netif_tx_stop_all_queues(netdev);
2153 }
2154 }
2155
Greg Rose92915f72010-01-09 02:24:10 +00002156 ixgbevf_update_stats(adapter);
2157
Greg Rose33bd9f62010-03-19 02:59:52 +00002158pf_has_reset:
Greg Rose92915f72010-01-09 02:24:10 +00002159 /* Reset the timer */
2160 if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
2161 mod_timer(&adapter->watchdog_timer,
2162 round_jiffies(jiffies + (2 * HZ)));
2163
2164 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2165}
2166
2167/**
2168 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
2169 * @adapter: board private structure
2170 * @tx_ring: Tx descriptor ring for a specific queue
2171 *
2172 * Free all transmit software resources
2173 **/
2174void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
2175 struct ixgbevf_ring *tx_ring)
2176{
2177 struct pci_dev *pdev = adapter->pdev;
2178
Greg Rose92915f72010-01-09 02:24:10 +00002179 ixgbevf_clean_tx_ring(adapter, tx_ring);
2180
2181 vfree(tx_ring->tx_buffer_info);
2182 tx_ring->tx_buffer_info = NULL;
2183
Nick Nunley2a1f8792010-04-27 13:10:50 +00002184 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2185 tx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002186
2187 tx_ring->desc = NULL;
2188}
2189
2190/**
2191 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2192 * @adapter: board private structure
2193 *
2194 * Free all transmit software resources
2195 **/
2196static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2197{
2198 int i;
2199
2200 for (i = 0; i < adapter->num_tx_queues; i++)
2201 if (adapter->tx_ring[i].desc)
2202 ixgbevf_free_tx_resources(adapter,
2203 &adapter->tx_ring[i]);
2204
2205}
2206
2207/**
2208 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
2209 * @adapter: board private structure
2210 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2211 *
2212 * Return 0 on success, negative on failure
2213 **/
2214int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
2215 struct ixgbevf_ring *tx_ring)
2216{
2217 struct pci_dev *pdev = adapter->pdev;
2218 int size;
2219
2220 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002221 tx_ring->tx_buffer_info = vzalloc(size);
Greg Rose92915f72010-01-09 02:24:10 +00002222 if (!tx_ring->tx_buffer_info)
2223 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002224
2225 /* round up to nearest 4K */
2226 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2227 tx_ring->size = ALIGN(tx_ring->size, 4096);
2228
Nick Nunley2a1f8792010-04-27 13:10:50 +00002229 tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
2230 &tx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002231 if (!tx_ring->desc)
2232 goto err;
2233
2234 tx_ring->next_to_use = 0;
2235 tx_ring->next_to_clean = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002236 return 0;
2237
2238err:
2239 vfree(tx_ring->tx_buffer_info);
2240 tx_ring->tx_buffer_info = NULL;
2241 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2242 "descriptor ring\n");
2243 return -ENOMEM;
2244}
2245
2246/**
2247 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2248 * @adapter: board private structure
2249 *
2250 * If this function returns with an error, then it's possible one or
2251 * more of the rings is populated (while the rest are not). It is the
2252 * callers duty to clean those orphaned rings.
2253 *
2254 * Return 0 on success, negative on failure
2255 **/
2256static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2257{
2258 int i, err = 0;
2259
2260 for (i = 0; i < adapter->num_tx_queues; i++) {
2261 err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
2262 if (!err)
2263 continue;
2264 hw_dbg(&adapter->hw,
2265 "Allocation for Tx Queue %u failed\n", i);
2266 break;
2267 }
2268
2269 return err;
2270}
2271
2272/**
2273 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
2274 * @adapter: board private structure
2275 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2276 *
2277 * Returns 0 on success, negative on failure
2278 **/
2279int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
2280 struct ixgbevf_ring *rx_ring)
2281{
2282 struct pci_dev *pdev = adapter->pdev;
2283 int size;
2284
2285 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002286 rx_ring->rx_buffer_info = vzalloc(size);
Joe Perchese404dec2012-01-29 12:56:23 +00002287 if (!rx_ring->rx_buffer_info)
Greg Rose92915f72010-01-09 02:24:10 +00002288 goto alloc_failed;
Greg Rose92915f72010-01-09 02:24:10 +00002289
2290 /* Round up to nearest 4K */
2291 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2292 rx_ring->size = ALIGN(rx_ring->size, 4096);
2293
Nick Nunley2a1f8792010-04-27 13:10:50 +00002294 rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
2295 &rx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002296
2297 if (!rx_ring->desc) {
2298 hw_dbg(&adapter->hw,
2299 "Unable to allocate memory for "
2300 "the receive descriptor ring\n");
2301 vfree(rx_ring->rx_buffer_info);
2302 rx_ring->rx_buffer_info = NULL;
2303 goto alloc_failed;
2304 }
2305
2306 rx_ring->next_to_clean = 0;
2307 rx_ring->next_to_use = 0;
2308
2309 return 0;
2310alloc_failed:
2311 return -ENOMEM;
2312}
2313
2314/**
2315 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2316 * @adapter: board private structure
2317 *
2318 * If this function returns with an error, then it's possible one or
2319 * more of the rings is populated (while the rest are not). It is the
2320 * callers duty to clean those orphaned rings.
2321 *
2322 * Return 0 on success, negative on failure
2323 **/
2324static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2325{
2326 int i, err = 0;
2327
2328 for (i = 0; i < adapter->num_rx_queues; i++) {
2329 err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
2330 if (!err)
2331 continue;
2332 hw_dbg(&adapter->hw,
2333 "Allocation for Rx Queue %u failed\n", i);
2334 break;
2335 }
2336 return err;
2337}
2338
2339/**
2340 * ixgbevf_free_rx_resources - Free Rx Resources
2341 * @adapter: board private structure
2342 * @rx_ring: ring to clean the resources from
2343 *
2344 * Free all receive software resources
2345 **/
2346void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
2347 struct ixgbevf_ring *rx_ring)
2348{
2349 struct pci_dev *pdev = adapter->pdev;
2350
2351 ixgbevf_clean_rx_ring(adapter, rx_ring);
2352
2353 vfree(rx_ring->rx_buffer_info);
2354 rx_ring->rx_buffer_info = NULL;
2355
Nick Nunley2a1f8792010-04-27 13:10:50 +00002356 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2357 rx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002358
2359 rx_ring->desc = NULL;
2360}
2361
2362/**
2363 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2364 * @adapter: board private structure
2365 *
2366 * Free all receive software resources
2367 **/
2368static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2369{
2370 int i;
2371
2372 for (i = 0; i < adapter->num_rx_queues; i++)
2373 if (adapter->rx_ring[i].desc)
2374 ixgbevf_free_rx_resources(adapter,
2375 &adapter->rx_ring[i]);
2376}
2377
2378/**
2379 * ixgbevf_open - Called when a network interface is made active
2380 * @netdev: network interface device structure
2381 *
2382 * Returns 0 on success, negative value on failure
2383 *
2384 * The open entry point is called when a network interface is made
2385 * active by the system (IFF_UP). At this point all resources needed
2386 * for transmit and receive operations are allocated, the interrupt
2387 * handler is registered with the OS, the watchdog timer is started,
2388 * and the stack is notified that the interface is ready.
2389 **/
2390static int ixgbevf_open(struct net_device *netdev)
2391{
2392 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2393 struct ixgbe_hw *hw = &adapter->hw;
2394 int err;
2395
2396 /* disallow open during test */
2397 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2398 return -EBUSY;
2399
2400 if (hw->adapter_stopped) {
2401 ixgbevf_reset(adapter);
2402 /* if adapter is still stopped then PF isn't up and
2403 * the vf can't start. */
2404 if (hw->adapter_stopped) {
2405 err = IXGBE_ERR_MBX;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002406 pr_err("Unable to start - perhaps the PF Driver isn't "
2407 "up yet\n");
Greg Rose92915f72010-01-09 02:24:10 +00002408 goto err_setup_reset;
2409 }
2410 }
2411
Alexander Duyck31186782012-07-20 08:09:58 +00002412 ixgbevf_negotiate_api(adapter);
2413
Greg Rose92915f72010-01-09 02:24:10 +00002414 /* allocate transmit descriptors */
2415 err = ixgbevf_setup_all_tx_resources(adapter);
2416 if (err)
2417 goto err_setup_tx;
2418
2419 /* allocate receive descriptors */
2420 err = ixgbevf_setup_all_rx_resources(adapter);
2421 if (err)
2422 goto err_setup_rx;
2423
2424 ixgbevf_configure(adapter);
2425
2426 /*
2427 * Map the Tx/Rx rings to the vectors we were allotted.
2428 * if request_irq will be called in this function map_rings
2429 * must be called *before* up_complete
2430 */
2431 ixgbevf_map_rings_to_vectors(adapter);
2432
Greg Rose795180d2012-04-17 04:29:34 +00002433 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002434
2435 /* clear any pending interrupts, may auto mask */
2436 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2437 err = ixgbevf_request_irq(adapter);
2438 if (err)
2439 goto err_req_irq;
2440
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002441 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002442
2443 return 0;
2444
2445err_req_irq:
2446 ixgbevf_down(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002447 ixgbevf_free_irq(adapter);
2448err_setup_rx:
2449 ixgbevf_free_all_rx_resources(adapter);
2450err_setup_tx:
2451 ixgbevf_free_all_tx_resources(adapter);
2452 ixgbevf_reset(adapter);
2453
2454err_setup_reset:
2455
2456 return err;
2457}
2458
2459/**
2460 * ixgbevf_close - Disables a network interface
2461 * @netdev: network interface device structure
2462 *
2463 * Returns 0, this is not allowed to fail
2464 *
2465 * The close entry point is called when an interface is de-activated
2466 * by the OS. The hardware is still under the drivers control, but
2467 * needs to be disabled. A global MAC reset is issued to stop the
2468 * hardware, and all transmit and receive resources are freed.
2469 **/
2470static int ixgbevf_close(struct net_device *netdev)
2471{
2472 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2473
2474 ixgbevf_down(adapter);
2475 ixgbevf_free_irq(adapter);
2476
2477 ixgbevf_free_all_tx_resources(adapter);
2478 ixgbevf_free_all_rx_resources(adapter);
2479
2480 return 0;
2481}
2482
Alexander Duyck70a10e22012-05-11 08:33:21 +00002483static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2484 u32 vlan_macip_lens, u32 type_tucmd,
2485 u32 mss_l4len_idx)
2486{
2487 struct ixgbe_adv_tx_context_desc *context_desc;
2488 u16 i = tx_ring->next_to_use;
2489
2490 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2491
2492 i++;
2493 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2494
2495 /* set bits to identify this as an advanced context descriptor */
2496 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2497
2498 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2499 context_desc->seqnum_seed = 0;
2500 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2501 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2502}
2503
2504static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
Greg Rose92915f72010-01-09 02:24:10 +00002505 struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
2506{
Alexander Duyck70a10e22012-05-11 08:33:21 +00002507 u32 vlan_macip_lens, type_tucmd;
Greg Rose92915f72010-01-09 02:24:10 +00002508 u32 mss_l4len_idx, l4len;
2509
Alexander Duyck70a10e22012-05-11 08:33:21 +00002510 if (!skb_is_gso(skb))
2511 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002512
Alexander Duyck70a10e22012-05-11 08:33:21 +00002513 if (skb_header_cloned(skb)) {
2514 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2515 if (err)
2516 return err;
Greg Rose92915f72010-01-09 02:24:10 +00002517 }
2518
Alexander Duyck70a10e22012-05-11 08:33:21 +00002519 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2520 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2521
2522 if (skb->protocol == htons(ETH_P_IP)) {
2523 struct iphdr *iph = ip_hdr(skb);
2524 iph->tot_len = 0;
2525 iph->check = 0;
2526 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2527 iph->daddr, 0,
2528 IPPROTO_TCP,
2529 0);
2530 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2531 } else if (skb_is_gso_v6(skb)) {
2532 ipv6_hdr(skb)->payload_len = 0;
2533 tcp_hdr(skb)->check =
2534 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2535 &ipv6_hdr(skb)->daddr,
2536 0, IPPROTO_TCP, 0);
2537 }
2538
2539 /* compute header lengths */
2540 l4len = tcp_hdrlen(skb);
2541 *hdr_len += l4len;
2542 *hdr_len = skb_transport_offset(skb) + l4len;
2543
2544 /* mss_l4len_id: use 1 as index for TSO */
2545 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2546 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2547 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2548
2549 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2550 vlan_macip_lens = skb_network_header_len(skb);
2551 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2552 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2553
2554 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2555 type_tucmd, mss_l4len_idx);
2556
2557 return 1;
Greg Rose92915f72010-01-09 02:24:10 +00002558}
2559
Alexander Duyck70a10e22012-05-11 08:33:21 +00002560static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
Greg Rose92915f72010-01-09 02:24:10 +00002561 struct sk_buff *skb, u32 tx_flags)
2562{
Greg Rose92915f72010-01-09 02:24:10 +00002563
Greg Rose92915f72010-01-09 02:24:10 +00002564
Greg Rose92915f72010-01-09 02:24:10 +00002565
Alexander Duyck70a10e22012-05-11 08:33:21 +00002566 u32 vlan_macip_lens = 0;
2567 u32 mss_l4len_idx = 0;
2568 u32 type_tucmd = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002569
Alexander Duyck70a10e22012-05-11 08:33:21 +00002570 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2571 u8 l4_hdr = 0;
2572 switch (skb->protocol) {
2573 case __constant_htons(ETH_P_IP):
2574 vlan_macip_lens |= skb_network_header_len(skb);
2575 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2576 l4_hdr = ip_hdr(skb)->protocol;
2577 break;
2578 case __constant_htons(ETH_P_IPV6):
2579 vlan_macip_lens |= skb_network_header_len(skb);
2580 l4_hdr = ipv6_hdr(skb)->nexthdr;
2581 break;
2582 default:
2583 if (unlikely(net_ratelimit())) {
2584 dev_warn(tx_ring->dev,
2585 "partial checksum but proto=%x!\n",
2586 skb->protocol);
Greg Rose92915f72010-01-09 02:24:10 +00002587 }
Alexander Duyck70a10e22012-05-11 08:33:21 +00002588 break;
Greg Rose92915f72010-01-09 02:24:10 +00002589 }
2590
Alexander Duyck70a10e22012-05-11 08:33:21 +00002591 switch (l4_hdr) {
2592 case IPPROTO_TCP:
2593 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2594 mss_l4len_idx = tcp_hdrlen(skb) <<
2595 IXGBE_ADVTXD_L4LEN_SHIFT;
2596 break;
2597 case IPPROTO_SCTP:
2598 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2599 mss_l4len_idx = sizeof(struct sctphdr) <<
2600 IXGBE_ADVTXD_L4LEN_SHIFT;
2601 break;
2602 case IPPROTO_UDP:
2603 mss_l4len_idx = sizeof(struct udphdr) <<
2604 IXGBE_ADVTXD_L4LEN_SHIFT;
2605 break;
2606 default:
2607 if (unlikely(net_ratelimit())) {
2608 dev_warn(tx_ring->dev,
2609 "partial checksum but l4 proto=%x!\n",
2610 l4_hdr);
2611 }
2612 break;
2613 }
Greg Rose92915f72010-01-09 02:24:10 +00002614 }
2615
Alexander Duyck70a10e22012-05-11 08:33:21 +00002616 /* vlan_macip_lens: MACLEN, VLAN tag */
2617 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
2618 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
2619
2620 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2621 type_tucmd, mss_l4len_idx);
2622
2623 return (skb->ip_summed == CHECKSUM_PARTIAL);
Greg Rose92915f72010-01-09 02:24:10 +00002624}
2625
Alexander Duyck70a10e22012-05-11 08:33:21 +00002626static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
Greg Rose92915f72010-01-09 02:24:10 +00002627 struct sk_buff *skb, u32 tx_flags,
2628 unsigned int first)
2629{
Greg Rose92915f72010-01-09 02:24:10 +00002630 struct ixgbevf_tx_buffer *tx_buffer_info;
2631 unsigned int len;
2632 unsigned int total = skb->len;
Kulikov Vasiliy2540ddb2010-07-15 08:45:57 +00002633 unsigned int offset = 0, size;
2634 int count = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002635 unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2636 unsigned int f;
Greg Rose65deeed2010-03-24 09:35:42 +00002637 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002638
2639 i = tx_ring->next_to_use;
2640
2641 len = min(skb_headlen(skb), total);
2642 while (len) {
2643 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2644 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2645
2646 tx_buffer_info->length = size;
2647 tx_buffer_info->mapped_as_page = false;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002648 tx_buffer_info->dma = dma_map_single(tx_ring->dev,
Greg Rose92915f72010-01-09 02:24:10 +00002649 skb->data + offset,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002650 size, DMA_TO_DEVICE);
Alexander Duyck70a10e22012-05-11 08:33:21 +00002651 if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
Greg Rose92915f72010-01-09 02:24:10 +00002652 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00002653 tx_buffer_info->next_to_watch = i;
2654
2655 len -= size;
2656 total -= size;
2657 offset += size;
2658 count++;
2659 i++;
2660 if (i == tx_ring->count)
2661 i = 0;
2662 }
2663
2664 for (f = 0; f < nr_frags; f++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002665 const struct skb_frag_struct *frag;
Greg Rose92915f72010-01-09 02:24:10 +00002666
2667 frag = &skb_shinfo(skb)->frags[f];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002668 len = min((unsigned int)skb_frag_size(frag), total);
Ian Campbell877749b2011-08-29 23:18:26 +00002669 offset = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002670
2671 while (len) {
2672 tx_buffer_info = &tx_ring->tx_buffer_info[i];
2673 size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
2674
2675 tx_buffer_info->length = size;
Ian Campbell877749b2011-08-29 23:18:26 +00002676 tx_buffer_info->dma =
Alexander Duyck70a10e22012-05-11 08:33:21 +00002677 skb_frag_dma_map(tx_ring->dev, frag,
Ian Campbell877749b2011-08-29 23:18:26 +00002678 offset, size, DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +00002679 tx_buffer_info->mapped_as_page = true;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002680 if (dma_mapping_error(tx_ring->dev,
2681 tx_buffer_info->dma))
Greg Rose92915f72010-01-09 02:24:10 +00002682 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00002683 tx_buffer_info->next_to_watch = i;
2684
2685 len -= size;
2686 total -= size;
2687 offset += size;
2688 count++;
2689 i++;
2690 if (i == tx_ring->count)
2691 i = 0;
2692 }
2693 if (total == 0)
2694 break;
2695 }
2696
2697 if (i == 0)
2698 i = tx_ring->count - 1;
2699 else
2700 i = i - 1;
2701 tx_ring->tx_buffer_info[i].skb = skb;
2702 tx_ring->tx_buffer_info[first].next_to_watch = i;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002703 tx_ring->tx_buffer_info[first].time_stamp = jiffies;
Greg Rose92915f72010-01-09 02:24:10 +00002704
2705 return count;
2706
2707dma_error:
Alexander Duyck70a10e22012-05-11 08:33:21 +00002708 dev_err(tx_ring->dev, "TX DMA map failed\n");
Greg Rose92915f72010-01-09 02:24:10 +00002709
2710 /* clear timestamp and dma mappings for failed tx_buffer_info map */
2711 tx_buffer_info->dma = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002712 tx_buffer_info->next_to_watch = 0;
2713 count--;
2714
2715 /* clear timestamp and dma mappings for remaining portion of packet */
2716 while (count >= 0) {
2717 count--;
2718 i--;
2719 if (i < 0)
2720 i += tx_ring->count;
2721 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00002722 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00002723 }
2724
2725 return count;
2726}
2727
Alexander Duyck70a10e22012-05-11 08:33:21 +00002728static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
Greg Rose92915f72010-01-09 02:24:10 +00002729 int count, u32 paylen, u8 hdr_len)
2730{
2731 union ixgbe_adv_tx_desc *tx_desc = NULL;
2732 struct ixgbevf_tx_buffer *tx_buffer_info;
2733 u32 olinfo_status = 0, cmd_type_len = 0;
2734 unsigned int i;
2735
2736 u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
2737
2738 cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
2739
2740 cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
2741
2742 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
2743 cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
2744
Alexander Duyck70a10e22012-05-11 08:33:21 +00002745 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
2746 olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
2747
Greg Rose92915f72010-01-09 02:24:10 +00002748 if (tx_flags & IXGBE_TX_FLAGS_TSO) {
2749 cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
2750
Greg Rose92915f72010-01-09 02:24:10 +00002751 /* use index 1 context for tso */
2752 olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
2753 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
Alexander Duyck70a10e22012-05-11 08:33:21 +00002754 olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
Greg Rose92915f72010-01-09 02:24:10 +00002755
Alexander Duyck70a10e22012-05-11 08:33:21 +00002756 }
2757
2758 /*
2759 * Check Context must be set if Tx switch is enabled, which it
2760 * always is for case where virtual functions are running
2761 */
2762 olinfo_status |= IXGBE_ADVTXD_CC;
Greg Rose92915f72010-01-09 02:24:10 +00002763
2764 olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
2765
2766 i = tx_ring->next_to_use;
2767 while (count--) {
2768 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck908421f2012-05-11 08:33:00 +00002769 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +00002770 tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
2771 tx_desc->read.cmd_type_len =
2772 cpu_to_le32(cmd_type_len | tx_buffer_info->length);
2773 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2774 i++;
2775 if (i == tx_ring->count)
2776 i = 0;
2777 }
2778
2779 tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
2780
Greg Rose92915f72010-01-09 02:24:10 +00002781 tx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +00002782}
2783
Alexander Duyckfb401952012-05-11 08:33:16 +00002784static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00002785{
Alexander Duyckfb401952012-05-11 08:33:16 +00002786 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002787
Alexander Duyckfb401952012-05-11 08:33:16 +00002788 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00002789 /* Herbert's original patch had:
2790 * smp_mb__after_netif_stop_queue();
2791 * but since that doesn't exist yet, just open code it. */
2792 smp_mb();
2793
2794 /* We need to check again in a case another CPU has just
2795 * made room available. */
2796 if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
2797 return -EBUSY;
2798
2799 /* A reprieve! - use start_queue because it doesn't call schedule */
Alexander Duyckfb401952012-05-11 08:33:16 +00002800 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00002801 ++adapter->restart_queue;
2802 return 0;
2803}
2804
Alexander Duyckfb401952012-05-11 08:33:16 +00002805static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00002806{
2807 if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
2808 return 0;
Alexander Duyckfb401952012-05-11 08:33:16 +00002809 return __ixgbevf_maybe_stop_tx(tx_ring, size);
Greg Rose92915f72010-01-09 02:24:10 +00002810}
2811
2812static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2813{
2814 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2815 struct ixgbevf_ring *tx_ring;
2816 unsigned int first;
2817 unsigned int tx_flags = 0;
2818 u8 hdr_len = 0;
2819 int r_idx = 0, tso;
Alexander Duyck35959902012-05-11 08:32:40 +00002820 u16 count = TXD_USE_COUNT(skb_headlen(skb));
2821#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2822 unsigned short f;
2823#endif
Greg Rose92915f72010-01-09 02:24:10 +00002824
2825 tx_ring = &adapter->tx_ring[r_idx];
2826
Alexander Duyck35959902012-05-11 08:32:40 +00002827 /*
2828 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
2829 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
2830 * + 2 desc gap to keep tail from touching head,
2831 * + 1 desc for context descriptor,
2832 * otherwise try next time
2833 */
2834#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
2835 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2836 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2837#else
2838 count += skb_shinfo(skb)->nr_frags;
2839#endif
Alexander Duyckfb401952012-05-11 08:33:16 +00002840 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
Alexander Duyck35959902012-05-11 08:32:40 +00002841 adapter->tx_busy++;
2842 return NETDEV_TX_BUSY;
2843 }
2844
Jesse Grosseab6d182010-10-20 13:56:03 +00002845 if (vlan_tx_tag_present(skb)) {
Greg Rose92915f72010-01-09 02:24:10 +00002846 tx_flags |= vlan_tx_tag_get(skb);
2847 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
2848 tx_flags |= IXGBE_TX_FLAGS_VLAN;
2849 }
2850
Greg Rose92915f72010-01-09 02:24:10 +00002851 first = tx_ring->next_to_use;
2852
2853 if (skb->protocol == htons(ETH_P_IP))
2854 tx_flags |= IXGBE_TX_FLAGS_IPV4;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002855 tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
Greg Rose92915f72010-01-09 02:24:10 +00002856 if (tso < 0) {
2857 dev_kfree_skb_any(skb);
2858 return NETDEV_TX_OK;
2859 }
2860
2861 if (tso)
Alexander Duyck70a10e22012-05-11 08:33:21 +00002862 tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
2863 else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
Greg Rose92915f72010-01-09 02:24:10 +00002864 tx_flags |= IXGBE_TX_FLAGS_CSUM;
2865
Alexander Duyck70a10e22012-05-11 08:33:21 +00002866 ixgbevf_tx_queue(tx_ring, tx_flags,
2867 ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
Greg Rose92915f72010-01-09 02:24:10 +00002868 skb->len, hdr_len);
Alexander Duyck70a10e22012-05-11 08:33:21 +00002869 /*
2870 * Force memory writes to complete before letting h/w
2871 * know there are new descriptors to fetch. (Only
2872 * applicable for weak-ordered memory model archs,
2873 * such as IA-64).
2874 */
2875 wmb();
2876
2877 writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
Greg Rose92915f72010-01-09 02:24:10 +00002878
Alexander Duyckfb401952012-05-11 08:33:16 +00002879 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Greg Rose92915f72010-01-09 02:24:10 +00002880
2881 return NETDEV_TX_OK;
2882}
2883
2884/**
Greg Rose92915f72010-01-09 02:24:10 +00002885 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
2886 * @netdev: network interface device structure
2887 * @p: pointer to an address structure
2888 *
2889 * Returns 0 on success, negative on failure
2890 **/
2891static int ixgbevf_set_mac(struct net_device *netdev, void *p)
2892{
2893 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2894 struct ixgbe_hw *hw = &adapter->hw;
2895 struct sockaddr *addr = p;
2896
2897 if (!is_valid_ether_addr(addr->sa_data))
2898 return -EADDRNOTAVAIL;
2899
2900 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2901 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
2902
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002903 spin_lock(&adapter->mbx_lock);
2904
Greg Rose92915f72010-01-09 02:24:10 +00002905 if (hw->mac.ops.set_rar)
2906 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2907
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002908 spin_unlock(&adapter->mbx_lock);
2909
Greg Rose92915f72010-01-09 02:24:10 +00002910 return 0;
2911}
2912
2913/**
2914 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
2915 * @netdev: network interface device structure
2916 * @new_mtu: new value for maximum frame size
2917 *
2918 * Returns 0 on success, negative on failure
2919 **/
2920static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
2921{
2922 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2923 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Greg Rose69bfbec2011-01-26 01:06:12 +00002924 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose69bfbec2011-01-26 01:06:12 +00002925
2926 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
2927 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
Greg Rose92915f72010-01-09 02:24:10 +00002928
2929 /* MTU < 68 is an error and causes problems on some kernels */
Greg Rose69bfbec2011-01-26 01:06:12 +00002930 if ((new_mtu < 68) || (max_frame > max_possible_frame))
Greg Rose92915f72010-01-09 02:24:10 +00002931 return -EINVAL;
2932
2933 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
2934 netdev->mtu, new_mtu);
2935 /* must set new MTU before calling down or up */
2936 netdev->mtu = new_mtu;
2937
2938 if (netif_running(netdev))
2939 ixgbevf_reinit_locked(adapter);
2940
2941 return 0;
2942}
2943
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002944static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
Greg Rose92915f72010-01-09 02:24:10 +00002945{
2946 struct net_device *netdev = pci_get_drvdata(pdev);
2947 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002948#ifdef CONFIG_PM
2949 int retval = 0;
2950#endif
Greg Rose92915f72010-01-09 02:24:10 +00002951
2952 netif_device_detach(netdev);
2953
2954 if (netif_running(netdev)) {
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002955 rtnl_lock();
Greg Rose92915f72010-01-09 02:24:10 +00002956 ixgbevf_down(adapter);
2957 ixgbevf_free_irq(adapter);
2958 ixgbevf_free_all_tx_resources(adapter);
2959 ixgbevf_free_all_rx_resources(adapter);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002960 rtnl_unlock();
Greg Rose92915f72010-01-09 02:24:10 +00002961 }
2962
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002963 ixgbevf_clear_interrupt_scheme(adapter);
2964
2965#ifdef CONFIG_PM
2966 retval = pci_save_state(pdev);
2967 if (retval)
2968 return retval;
2969
2970#endif
2971 pci_disable_device(pdev);
2972
2973 return 0;
2974}
2975
2976#ifdef CONFIG_PM
2977static int ixgbevf_resume(struct pci_dev *pdev)
2978{
2979 struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
2980 struct net_device *netdev = adapter->netdev;
2981 u32 err;
2982
2983 pci_set_power_state(pdev, PCI_D0);
2984 pci_restore_state(pdev);
2985 /*
2986 * pci_restore_state clears dev->state_saved so call
2987 * pci_save_state to restore it.
2988 */
Greg Rose92915f72010-01-09 02:24:10 +00002989 pci_save_state(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00002990
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002991 err = pci_enable_device_mem(pdev);
2992 if (err) {
2993 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
2994 return err;
2995 }
2996 pci_set_master(pdev);
2997
2998 rtnl_lock();
2999 err = ixgbevf_init_interrupt_scheme(adapter);
3000 rtnl_unlock();
3001 if (err) {
3002 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3003 return err;
3004 }
3005
3006 ixgbevf_reset(adapter);
3007
3008 if (netif_running(netdev)) {
3009 err = ixgbevf_open(netdev);
3010 if (err)
3011 return err;
3012 }
3013
3014 netif_device_attach(netdev);
3015
3016 return err;
3017}
3018
3019#endif /* CONFIG_PM */
3020static void ixgbevf_shutdown(struct pci_dev *pdev)
3021{
3022 ixgbevf_suspend(pdev, PMSG_SUSPEND);
Greg Rose92915f72010-01-09 02:24:10 +00003023}
3024
Eric Dumazet4197aa72011-06-22 05:01:35 +00003025static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3026 struct rtnl_link_stats64 *stats)
3027{
3028 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3029 unsigned int start;
3030 u64 bytes, packets;
3031 const struct ixgbevf_ring *ring;
3032 int i;
3033
3034 ixgbevf_update_stats(adapter);
3035
3036 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3037
3038 for (i = 0; i < adapter->num_rx_queues; i++) {
3039 ring = &adapter->rx_ring[i];
3040 do {
3041 start = u64_stats_fetch_begin_bh(&ring->syncp);
3042 bytes = ring->total_bytes;
3043 packets = ring->total_packets;
3044 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3045 stats->rx_bytes += bytes;
3046 stats->rx_packets += packets;
3047 }
3048
3049 for (i = 0; i < adapter->num_tx_queues; i++) {
3050 ring = &adapter->tx_ring[i];
3051 do {
3052 start = u64_stats_fetch_begin_bh(&ring->syncp);
3053 bytes = ring->total_bytes;
3054 packets = ring->total_packets;
3055 } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
3056 stats->tx_bytes += bytes;
3057 stats->tx_packets += packets;
3058 }
3059
3060 return stats;
3061}
3062
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003063static const struct net_device_ops ixgbevf_netdev_ops = {
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003064 .ndo_open = ixgbevf_open,
3065 .ndo_stop = ixgbevf_close,
3066 .ndo_start_xmit = ixgbevf_xmit_frame,
3067 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
Eric Dumazet4197aa72011-06-22 05:01:35 +00003068 .ndo_get_stats64 = ixgbevf_get_stats,
Greg Rose92915f72010-01-09 02:24:10 +00003069 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003070 .ndo_set_mac_address = ixgbevf_set_mac,
3071 .ndo_change_mtu = ixgbevf_change_mtu,
3072 .ndo_tx_timeout = ixgbevf_tx_timeout,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003073 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3074 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
Greg Rose92915f72010-01-09 02:24:10 +00003075};
Greg Rose92915f72010-01-09 02:24:10 +00003076
3077static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3078{
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003079 dev->netdev_ops = &ixgbevf_netdev_ops;
Greg Rose92915f72010-01-09 02:24:10 +00003080 ixgbevf_set_ethtool_ops(dev);
3081 dev->watchdog_timeo = 5 * HZ;
3082}
3083
3084/**
3085 * ixgbevf_probe - Device Initialization Routine
3086 * @pdev: PCI device information struct
3087 * @ent: entry in ixgbevf_pci_tbl
3088 *
3089 * Returns 0 on success, negative on failure
3090 *
3091 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3092 * The OS initialization, configuring of the adapter private structure,
3093 * and a hardware reset occur.
3094 **/
3095static int __devinit ixgbevf_probe(struct pci_dev *pdev,
3096 const struct pci_device_id *ent)
3097{
3098 struct net_device *netdev;
3099 struct ixgbevf_adapter *adapter = NULL;
3100 struct ixgbe_hw *hw = NULL;
3101 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
3102 static int cards_found;
3103 int err, pci_using_dac;
3104
3105 err = pci_enable_device(pdev);
3106 if (err)
3107 return err;
3108
Nick Nunley2a1f8792010-04-27 13:10:50 +00003109 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3110 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
Greg Rose92915f72010-01-09 02:24:10 +00003111 pci_using_dac = 1;
3112 } else {
Nick Nunley2a1f8792010-04-27 13:10:50 +00003113 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003114 if (err) {
Nick Nunley2a1f8792010-04-27 13:10:50 +00003115 err = dma_set_coherent_mask(&pdev->dev,
3116 DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003117 if (err) {
3118 dev_err(&pdev->dev, "No usable DMA "
3119 "configuration, aborting\n");
3120 goto err_dma;
3121 }
3122 }
3123 pci_using_dac = 0;
3124 }
3125
3126 err = pci_request_regions(pdev, ixgbevf_driver_name);
3127 if (err) {
3128 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3129 goto err_pci_reg;
3130 }
3131
3132 pci_set_master(pdev);
3133
Greg Rose92915f72010-01-09 02:24:10 +00003134 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3135 MAX_TX_QUEUES);
Greg Rose92915f72010-01-09 02:24:10 +00003136 if (!netdev) {
3137 err = -ENOMEM;
3138 goto err_alloc_etherdev;
3139 }
3140
3141 SET_NETDEV_DEV(netdev, &pdev->dev);
3142
3143 pci_set_drvdata(pdev, netdev);
3144 adapter = netdev_priv(netdev);
3145
3146 adapter->netdev = netdev;
3147 adapter->pdev = pdev;
3148 hw = &adapter->hw;
3149 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00003150 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Greg Rose92915f72010-01-09 02:24:10 +00003151
3152 /*
3153 * call save state here in standalone driver because it relies on
3154 * adapter struct to exist, and needs to call netdev_priv
3155 */
3156 pci_save_state(pdev);
3157
3158 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3159 pci_resource_len(pdev, 0));
3160 if (!hw->hw_addr) {
3161 err = -EIO;
3162 goto err_ioremap;
3163 }
3164
3165 ixgbevf_assign_netdev_ops(netdev);
3166
3167 adapter->bd_number = cards_found;
3168
3169 /* Setup hw api */
3170 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3171 hw->mac.type = ii->mac;
3172
3173 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
Greg Rosef416dfc2011-06-08 07:32:38 +00003174 sizeof(struct ixgbe_mbx_operations));
Greg Rose92915f72010-01-09 02:24:10 +00003175
Greg Rose92915f72010-01-09 02:24:10 +00003176 /* setup the private structure */
3177 err = ixgbevf_sw_init(adapter);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003178 if (err)
3179 goto err_sw_init;
3180
3181 /* The HW MAC address was set and/or determined in sw_init */
3182 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
3183
3184 if (!is_valid_ether_addr(netdev->dev_addr)) {
3185 pr_err("invalid MAC address\n");
3186 err = -EIO;
3187 goto err_sw_init;
3188 }
Greg Rose92915f72010-01-09 02:24:10 +00003189
Michał Mirosław471a76d2011-06-08 08:53:03 +00003190 netdev->hw_features = NETIF_F_SG |
Greg Rose92915f72010-01-09 02:24:10 +00003191 NETIF_F_IP_CSUM |
Michał Mirosław471a76d2011-06-08 08:53:03 +00003192 NETIF_F_IPV6_CSUM |
3193 NETIF_F_TSO |
3194 NETIF_F_TSO6 |
3195 NETIF_F_RXCSUM;
3196
3197 netdev->features = netdev->hw_features |
Greg Rose92915f72010-01-09 02:24:10 +00003198 NETIF_F_HW_VLAN_TX |
3199 NETIF_F_HW_VLAN_RX |
3200 NETIF_F_HW_VLAN_FILTER;
3201
Greg Rose92915f72010-01-09 02:24:10 +00003202 netdev->vlan_features |= NETIF_F_TSO;
3203 netdev->vlan_features |= NETIF_F_TSO6;
3204 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyck3bfacf92010-08-02 14:59:04 +00003205 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003206 netdev->vlan_features |= NETIF_F_SG;
3207
3208 if (pci_using_dac)
3209 netdev->features |= NETIF_F_HIGHDMA;
3210
Jiri Pirko01789342011-08-16 06:29:00 +00003211 netdev->priv_flags |= IFF_UNICAST_FLT;
3212
Greg Rose92915f72010-01-09 02:24:10 +00003213 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00003214 adapter->watchdog_timer.function = ixgbevf_watchdog;
Greg Rose92915f72010-01-09 02:24:10 +00003215 adapter->watchdog_timer.data = (unsigned long)adapter;
3216
3217 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3218 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
3219
3220 err = ixgbevf_init_interrupt_scheme(adapter);
3221 if (err)
3222 goto err_sw_init;
3223
3224 /* pick up the PCI bus settings for reporting later */
3225 if (hw->mac.ops.get_bus_info)
3226 hw->mac.ops.get_bus_info(hw);
3227
Greg Rose92915f72010-01-09 02:24:10 +00003228 strcpy(netdev->name, "eth%d");
3229
3230 err = register_netdev(netdev);
3231 if (err)
3232 goto err_register;
3233
Greg Rose5d426ad2010-11-16 19:27:19 -08003234 netif_carrier_off(netdev);
3235
Greg Rose33bd9f62010-03-19 02:59:52 +00003236 ixgbevf_init_last_counter_stats(adapter);
3237
Greg Rose92915f72010-01-09 02:24:10 +00003238 /* print the MAC address */
Danny Kukawkaf794e7e2012-02-24 03:45:56 +00003239 hw_dbg(hw, "%pM\n", netdev->dev_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003240
3241 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3242
Greg Rose92915f72010-01-09 02:24:10 +00003243 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
3244 cards_found++;
3245 return 0;
3246
3247err_register:
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003248 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003249err_sw_init:
3250 ixgbevf_reset_interrupt_capability(adapter);
3251 iounmap(hw->hw_addr);
3252err_ioremap:
3253 free_netdev(netdev);
3254err_alloc_etherdev:
3255 pci_release_regions(pdev);
3256err_pci_reg:
3257err_dma:
3258 pci_disable_device(pdev);
3259 return err;
3260}
3261
3262/**
3263 * ixgbevf_remove - Device Removal Routine
3264 * @pdev: PCI device information struct
3265 *
3266 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3267 * that it should release a PCI device. The could be caused by a
3268 * Hot-Plug event, or because the driver is going to be removed from
3269 * memory.
3270 **/
3271static void __devexit ixgbevf_remove(struct pci_dev *pdev)
3272{
3273 struct net_device *netdev = pci_get_drvdata(pdev);
3274 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3275
3276 set_bit(__IXGBEVF_DOWN, &adapter->state);
3277
3278 del_timer_sync(&adapter->watchdog_timer);
3279
Tejun Heo23f333a2010-12-12 16:45:14 +01003280 cancel_work_sync(&adapter->reset_task);
Greg Rose92915f72010-01-09 02:24:10 +00003281 cancel_work_sync(&adapter->watchdog_task);
3282
Alexander Duyckfd13a9a2012-05-11 08:32:24 +00003283 if (netdev->reg_state == NETREG_REGISTERED)
Greg Rose92915f72010-01-09 02:24:10 +00003284 unregister_netdev(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00003285
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003286 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003287 ixgbevf_reset_interrupt_capability(adapter);
3288
3289 iounmap(adapter->hw.hw_addr);
3290 pci_release_regions(pdev);
3291
3292 hw_dbg(&adapter->hw, "Remove complete\n");
3293
3294 kfree(adapter->tx_ring);
3295 kfree(adapter->rx_ring);
3296
3297 free_netdev(netdev);
3298
3299 pci_disable_device(pdev);
3300}
3301
Alexander Duyck9f19f312012-05-11 08:33:32 +00003302/**
3303 * ixgbevf_io_error_detected - called when PCI error is detected
3304 * @pdev: Pointer to PCI device
3305 * @state: The current pci connection state
3306 *
3307 * This function is called after a PCI bus error affecting
3308 * this device has been detected.
3309 */
3310static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3311 pci_channel_state_t state)
3312{
3313 struct net_device *netdev = pci_get_drvdata(pdev);
3314 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3315
3316 netif_device_detach(netdev);
3317
3318 if (state == pci_channel_io_perm_failure)
3319 return PCI_ERS_RESULT_DISCONNECT;
3320
3321 if (netif_running(netdev))
3322 ixgbevf_down(adapter);
3323
3324 pci_disable_device(pdev);
3325
3326 /* Request a slot slot reset. */
3327 return PCI_ERS_RESULT_NEED_RESET;
3328}
3329
3330/**
3331 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3332 * @pdev: Pointer to PCI device
3333 *
3334 * Restart the card from scratch, as if from a cold-boot. Implementation
3335 * resembles the first-half of the ixgbevf_resume routine.
3336 */
3337static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3338{
3339 struct net_device *netdev = pci_get_drvdata(pdev);
3340 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3341
3342 if (pci_enable_device_mem(pdev)) {
3343 dev_err(&pdev->dev,
3344 "Cannot re-enable PCI device after reset.\n");
3345 return PCI_ERS_RESULT_DISCONNECT;
3346 }
3347
3348 pci_set_master(pdev);
3349
3350 ixgbevf_reset(adapter);
3351
3352 return PCI_ERS_RESULT_RECOVERED;
3353}
3354
3355/**
3356 * ixgbevf_io_resume - called when traffic can start flowing again.
3357 * @pdev: Pointer to PCI device
3358 *
3359 * This callback is called when the error recovery driver tells us that
3360 * its OK to resume normal operation. Implementation resembles the
3361 * second-half of the ixgbevf_resume routine.
3362 */
3363static void ixgbevf_io_resume(struct pci_dev *pdev)
3364{
3365 struct net_device *netdev = pci_get_drvdata(pdev);
3366 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3367
3368 if (netif_running(netdev))
3369 ixgbevf_up(adapter);
3370
3371 netif_device_attach(netdev);
3372}
3373
3374/* PCI Error Recovery (ERS) */
3375static struct pci_error_handlers ixgbevf_err_handler = {
3376 .error_detected = ixgbevf_io_error_detected,
3377 .slot_reset = ixgbevf_io_slot_reset,
3378 .resume = ixgbevf_io_resume,
3379};
3380
Greg Rose92915f72010-01-09 02:24:10 +00003381static struct pci_driver ixgbevf_driver = {
3382 .name = ixgbevf_driver_name,
3383 .id_table = ixgbevf_pci_tbl,
3384 .probe = ixgbevf_probe,
3385 .remove = __devexit_p(ixgbevf_remove),
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003386#ifdef CONFIG_PM
3387 /* Power Management Hooks */
3388 .suspend = ixgbevf_suspend,
3389 .resume = ixgbevf_resume,
3390#endif
Greg Rose92915f72010-01-09 02:24:10 +00003391 .shutdown = ixgbevf_shutdown,
Alexander Duyck9f19f312012-05-11 08:33:32 +00003392 .err_handler = &ixgbevf_err_handler
Greg Rose92915f72010-01-09 02:24:10 +00003393};
3394
3395/**
Greg Rose65d676c2011-02-03 06:54:13 +00003396 * ixgbevf_init_module - Driver Registration Routine
Greg Rose92915f72010-01-09 02:24:10 +00003397 *
Greg Rose65d676c2011-02-03 06:54:13 +00003398 * ixgbevf_init_module is the first routine called when the driver is
Greg Rose92915f72010-01-09 02:24:10 +00003399 * loaded. All it does is register with the PCI subsystem.
3400 **/
3401static int __init ixgbevf_init_module(void)
3402{
3403 int ret;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003404 pr_info("%s - version %s\n", ixgbevf_driver_string,
3405 ixgbevf_driver_version);
Greg Rose92915f72010-01-09 02:24:10 +00003406
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003407 pr_info("%s\n", ixgbevf_copyright);
Greg Rose92915f72010-01-09 02:24:10 +00003408
3409 ret = pci_register_driver(&ixgbevf_driver);
3410 return ret;
3411}
3412
3413module_init(ixgbevf_init_module);
3414
3415/**
Greg Rose65d676c2011-02-03 06:54:13 +00003416 * ixgbevf_exit_module - Driver Exit Cleanup Routine
Greg Rose92915f72010-01-09 02:24:10 +00003417 *
Greg Rose65d676c2011-02-03 06:54:13 +00003418 * ixgbevf_exit_module is called just before the driver is removed
Greg Rose92915f72010-01-09 02:24:10 +00003419 * from memory.
3420 **/
3421static void __exit ixgbevf_exit_module(void)
3422{
3423 pci_unregister_driver(&ixgbevf_driver);
3424}
3425
3426#ifdef DEBUG
3427/**
Greg Rose65d676c2011-02-03 06:54:13 +00003428 * ixgbevf_get_hw_dev_name - return device name string
Greg Rose92915f72010-01-09 02:24:10 +00003429 * used by hardware layer to print debugging information
3430 **/
3431char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3432{
3433 struct ixgbevf_adapter *adapter = hw->back;
3434 return adapter->netdev->name;
3435}
3436
3437#endif
3438module_exit(ixgbevf_exit_module);
3439
3440/* ixgbevf_main.c */