blob: 07222b84026e7a623becfcb579e95c822e1ce082 [file] [log] [blame]
Greg Rose92915f72010-01-09 02:24:10 +00001/*******************************************************************************
2
3 Intel 82599 Virtual Function driver
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00004 Copyright(c) 1999 - 2014 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
Don Skidmore86f359f2014-01-17 01:21:38 -080061#define DRV_VERSION "2.12.1-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 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020079static const struct pci_device_id ixgbevf_pci_tbl[] = {
Stephen Hemminger39ba22b2013-02-06 02:37:04 +000080 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
Greg Rose92915f72010-01-09 02:24:10 +000082 /* required last entry */
83 {0, }
84};
85MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
86
87MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
Emil Tantilovb8ce18c2014-04-05 05:39:42 +000088MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
Greg Rose92915f72010-01-09 02:24:10 +000089MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
stephen hemmingerb3f4d592012-03-13 06:04:20 +000092#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
93static int debug = -1;
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
Greg Rose92915f72010-01-09 02:24:10 +000096
97/* forward decls */
Don Skidmore220fe052013-09-21 01:40:49 +000098static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
Alexander Duyckfa71ae22012-05-11 08:32:50 +000099static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
Alexander Duyck56e94092012-07-20 08:10:03 +0000100static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
Greg Rose92915f72010-01-09 02:24:10 +0000101
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000102static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
103{
104 struct ixgbevf_adapter *adapter = hw->back;
105
106 if (!hw->hw_addr)
107 return;
108 hw->hw_addr = NULL;
109 dev_err(&adapter->pdev->dev, "Adapter removed\n");
Mark Rustadea699562014-03-12 00:38:51 +0000110 if (test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
111 schedule_work(&adapter->watchdog_task);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000112}
113
114static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
115{
116 u32 value;
117
118 /* The following check not only optimizes a bit by not
119 * performing a read on the status register when the
120 * register just read was a status register read that
121 * returned IXGBE_FAILED_READ_REG. It also blocks any
122 * potential recursion.
123 */
124 if (reg == IXGBE_VFSTATUS) {
125 ixgbevf_remove_adapter(hw);
126 return;
127 }
Mark Rustad32c74942014-03-18 07:03:35 +0000128 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000129 if (value == IXGBE_FAILED_READ_REG)
130 ixgbevf_remove_adapter(hw);
131}
132
Mark Rustad32c74942014-03-18 07:03:35 +0000133u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000134{
135 u8 __iomem *reg_addr = ACCESS_ONCE(hw->hw_addr);
136 u32 value;
137
138 if (IXGBE_REMOVED(reg_addr))
139 return IXGBE_FAILED_READ_REG;
140 value = readl(reg_addr + reg);
141 if (unlikely(value == IXGBE_FAILED_READ_REG))
142 ixgbevf_check_remove(hw, reg);
143 return value;
144}
145
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000146/**
Greg Rose65d676c2011-02-03 06:54:13 +0000147 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
Greg Rose92915f72010-01-09 02:24:10 +0000148 * @adapter: pointer to adapter struct
149 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
150 * @queue: queue to map the corresponding interrupt to
151 * @msix_vector: the vector to map to the corresponding queue
Greg Rose92915f72010-01-09 02:24:10 +0000152 */
153static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
154 u8 queue, u8 msix_vector)
155{
156 u32 ivar, index;
157 struct ixgbe_hw *hw = &adapter->hw;
158 if (direction == -1) {
159 /* other causes */
160 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
161 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
162 ivar &= ~0xFF;
163 ivar |= msix_vector;
164 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
165 } else {
166 /* tx or rx causes */
167 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
168 index = ((16 * (queue & 1)) + (8 * direction));
169 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
170 ivar &= ~(0xFF << index);
171 ivar |= (msix_vector << index);
172 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
173 }
174}
175
Alexander Duyck70a10e22012-05-11 08:33:21 +0000176static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800177 struct ixgbevf_tx_buffer *tx_buffer)
Greg Rose92915f72010-01-09 02:24:10 +0000178{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800179 if (tx_buffer->skb) {
180 dev_kfree_skb_any(tx_buffer->skb);
181 if (dma_unmap_len(tx_buffer, len))
Alexander Duyck70a10e22012-05-11 08:33:21 +0000182 dma_unmap_single(tx_ring->dev,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800183 dma_unmap_addr(tx_buffer, dma),
184 dma_unmap_len(tx_buffer, len),
Nick Nunley2a1f8792010-04-27 13:10:50 +0000185 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800186 } else if (dma_unmap_len(tx_buffer, len)) {
187 dma_unmap_page(tx_ring->dev,
188 dma_unmap_addr(tx_buffer, dma),
189 dma_unmap_len(tx_buffer, len),
190 DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000191 }
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800192 tx_buffer->next_to_watch = NULL;
193 tx_buffer->skb = NULL;
194 dma_unmap_len_set(tx_buffer, len, 0);
195 /* tx_buffer must be completely set up in the transmit path */
Greg Rose92915f72010-01-09 02:24:10 +0000196}
197
Greg Rose92915f72010-01-09 02:24:10 +0000198#define IXGBE_MAX_TXD_PWR 14
199#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
200
201/* Tx Descriptors needed, worst case */
Alexander Duyck35959902012-05-11 08:32:40 +0000202#define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
203#define DESC_NEEDED (MAX_SKB_FRAGS + 4)
Greg Rose92915f72010-01-09 02:24:10 +0000204
205static void ixgbevf_tx_timeout(struct net_device *netdev);
206
207/**
208 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000209 * @q_vector: board private structure
Greg Rose92915f72010-01-09 02:24:10 +0000210 * @tx_ring: tx ring to clean
211 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000212static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
Greg Rose92915f72010-01-09 02:24:10 +0000213 struct ixgbevf_ring *tx_ring)
214{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000215 struct ixgbevf_adapter *adapter = q_vector->adapter;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800216 struct ixgbevf_tx_buffer *tx_buffer;
217 union ixgbe_adv_tx_desc *tx_desc;
Greg Rose92915f72010-01-09 02:24:10 +0000218 unsigned int total_bytes = 0, total_packets = 0;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800219 unsigned int budget = tx_ring->count / 2;
220 unsigned int i = tx_ring->next_to_clean;
Greg Rose92915f72010-01-09 02:24:10 +0000221
Alexander Duyck10cc1bd2012-07-16 23:44:48 +0000222 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
223 return true;
224
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800225 tx_buffer = &tx_ring->tx_buffer_info[i];
226 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
227 i -= tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000228
Alexander Duycke757e3e2013-01-31 07:43:22 +0000229 do {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800230 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000231
232 /* if next_to_watch is not set then there is no work pending */
233 if (!eop_desc)
234 break;
235
236 /* prevent any other reads prior to eop_desc */
237 read_barrier_depends();
238
239 /* if DD is not set pending work has not been completed */
240 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
241 break;
242
243 /* clear next_to_watch to prevent false hangs */
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800244 tx_buffer->next_to_watch = NULL;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000245
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800246 /* update the statistics for this packet */
247 total_bytes += tx_buffer->bytecount;
248 total_packets += tx_buffer->gso_segs;
Greg Rose92915f72010-01-09 02:24:10 +0000249
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800250 /* free the skb */
251 dev_kfree_skb_any(tx_buffer->skb);
252
253 /* unmap skb header data */
254 dma_unmap_single(tx_ring->dev,
255 dma_unmap_addr(tx_buffer, dma),
256 dma_unmap_len(tx_buffer, len),
257 DMA_TO_DEVICE);
258
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800259 /* clear tx_buffer data */
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800260 tx_buffer->skb = NULL;
261 dma_unmap_len_set(tx_buffer, len, 0);
Greg Rose92915f72010-01-09 02:24:10 +0000262
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800263 /* unmap remaining buffers */
264 while (tx_desc != eop_desc) {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800265 tx_buffer++;
266 tx_desc++;
Greg Rose92915f72010-01-09 02:24:10 +0000267 i++;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800268 if (unlikely(!i)) {
269 i -= tx_ring->count;
270 tx_buffer = tx_ring->tx_buffer_info;
271 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
272 }
Alexander Duycke757e3e2013-01-31 07:43:22 +0000273
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800274 /* unmap any remaining paged data */
275 if (dma_unmap_len(tx_buffer, len)) {
276 dma_unmap_page(tx_ring->dev,
277 dma_unmap_addr(tx_buffer, dma),
278 dma_unmap_len(tx_buffer, len),
279 DMA_TO_DEVICE);
280 dma_unmap_len_set(tx_buffer, len, 0);
281 }
Greg Rose92915f72010-01-09 02:24:10 +0000282 }
283
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800284 /* move us one more past the eop_desc for start of next pkt */
285 tx_buffer++;
286 tx_desc++;
287 i++;
288 if (unlikely(!i)) {
289 i -= tx_ring->count;
290 tx_buffer = tx_ring->tx_buffer_info;
291 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
292 }
293
294 /* issue prefetch for next Tx descriptor */
295 prefetch(tx_desc);
296
297 /* update budget accounting */
298 budget--;
299 } while (likely(budget));
300
301 i += tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000302 tx_ring->next_to_clean = i;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000303 u64_stats_update_begin(&tx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800304 tx_ring->stats.bytes += total_bytes;
305 tx_ring->stats.packets += total_packets;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000306 u64_stats_update_end(&tx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000307 q_vector->tx.total_bytes += total_bytes;
308 q_vector->tx.total_packets += total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000309
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800310#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
311 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
312 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
313 /* Make sure that anybody stopping the queue after this
314 * sees the new next_to_clean.
315 */
316 smp_mb();
317
318 if (__netif_subqueue_stopped(tx_ring->netdev,
319 tx_ring->queue_index) &&
320 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
321 netif_wake_subqueue(tx_ring->netdev,
322 tx_ring->queue_index);
323 ++tx_ring->tx_stats.restart_queue;
324 }
325 }
326
327 return !!budget;
Greg Rose92915f72010-01-09 02:24:10 +0000328}
329
330/**
Jacob Keller08681612013-09-21 06:24:09 +0000331 * ixgbevf_rx_skb - Helper function to determine proper Rx method
332 * @q_vector: structure containing interrupt and ring information
333 * @skb: packet to send up
Jacob Keller08681612013-09-21 06:24:09 +0000334 **/
335static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
Emil Tantilovdff80522014-11-08 01:39:25 +0000336 struct sk_buff *skb)
Jacob Keller08681612013-09-21 06:24:09 +0000337{
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000338#ifdef CONFIG_NET_RX_BUSY_POLL
339 skb_mark_napi_id(skb, &q_vector->napi);
340
341 if (ixgbevf_qv_busy_polling(q_vector)) {
342 netif_receive_skb(skb);
343 /* exit early if we busy polled */
344 return;
345 }
346#endif /* CONFIG_NET_RX_BUSY_POLL */
Emil Tantilovdff80522014-11-08 01:39:25 +0000347 if (!(q_vector->adapter->flags & IXGBE_FLAG_IN_NETPOLL))
348 napi_gro_receive(&q_vector->napi, skb);
349 else
350 netif_rx(skb);
Jacob Keller08681612013-09-21 06:24:09 +0000351}
352
Emil Tantilovec62fe22014-11-08 01:39:20 +0000353/* ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
354 * @ring: structure containig ring specific data
355 * @rx_desc: current Rx descriptor being processed
Greg Rose92915f72010-01-09 02:24:10 +0000356 * @skb: skb currently being received and modified
Emil Tantilovec62fe22014-11-08 01:39:20 +0000357 */
Greg Rose55fb2772012-11-06 05:53:32 +0000358static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
Emil Tantilovec62fe22014-11-08 01:39:20 +0000359 union ixgbe_adv_rx_desc *rx_desc,
360 struct sk_buff *skb)
Greg Rose92915f72010-01-09 02:24:10 +0000361{
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700362 skb_checksum_none_assert(skb);
Greg Rose92915f72010-01-09 02:24:10 +0000363
364 /* Rx csum disabled */
Alexander Duyckfb401952012-05-11 08:33:16 +0000365 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Greg Rose92915f72010-01-09 02:24:10 +0000366 return;
367
368 /* if IP and error */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000369 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
370 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800371 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000372 return;
373 }
374
Emil Tantilovec62fe22014-11-08 01:39:20 +0000375 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
Greg Rose92915f72010-01-09 02:24:10 +0000376 return;
377
Emil Tantilovec62fe22014-11-08 01:39:20 +0000378 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800379 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000380 return;
381 }
382
383 /* It must be a TCP or UDP packet with a valid checksum */
384 skb->ip_summed = CHECKSUM_UNNECESSARY;
Greg Rose92915f72010-01-09 02:24:10 +0000385}
386
Emil Tantilovdff80522014-11-08 01:39:25 +0000387/* ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
388 * @rx_ring: rx descriptor ring packet is being transacted on
389 * @rx_desc: pointer to the EOP Rx descriptor
390 * @skb: pointer to current skb being populated
391 *
392 * This function checks the ring, descriptor, and packet information in
393 * order to populate the checksum, VLAN, protocol, and other fields within
394 * the skb.
395 */
396static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
397 union ixgbe_adv_rx_desc *rx_desc,
398 struct sk_buff *skb)
399{
400 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
401
402 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
403 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
404 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
405
406 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
407 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
408 }
409
410 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
411}
412
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000413/**
414 * ixgbevf_is_non_eop - process handling of non-EOP buffers
415 * @rx_ring: Rx ring being processed
416 * @rx_desc: Rx descriptor for current buffer
417 * @skb: current socket buffer containing buffer in progress
418 *
419 * This function updates next to clean. If the buffer is an EOP buffer
420 * this function exits returning false, otherwise it will place the
421 * sk_buff in the next buffer to be chained and return true indicating
422 * that this is in fact a non-EOP buffer.
423 **/
424static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
Emil Tantilovbad17232014-11-21 02:57:15 +0000425 union ixgbe_adv_rx_desc *rx_desc)
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000426{
427 u32 ntc = rx_ring->next_to_clean + 1;
428
429 /* fetch, update, and store next to clean */
430 ntc = (ntc < rx_ring->count) ? ntc : 0;
431 rx_ring->next_to_clean = ntc;
432
433 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
434
435 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
436 return false;
437
438 return true;
439}
440
Emil Tantilovbad17232014-11-21 02:57:15 +0000441static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
442 struct ixgbevf_rx_buffer *bi)
Emil Tantilovbafa5782014-11-08 01:39:15 +0000443{
Emil Tantilovbad17232014-11-21 02:57:15 +0000444 struct page *page = bi->page;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000445 dma_addr_t dma = bi->dma;
446
Emil Tantilovbad17232014-11-21 02:57:15 +0000447 /* since we are recycling buffers we should seldom need to alloc */
448 if (likely(page))
Emil Tantilovbafa5782014-11-08 01:39:15 +0000449 return true;
450
Emil Tantilovbad17232014-11-21 02:57:15 +0000451 /* alloc new page for storage */
452 page = dev_alloc_page();
453 if (unlikely(!page)) {
454 rx_ring->rx_stats.alloc_rx_page_failed++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000455 return false;
456 }
457
Emil Tantilovbad17232014-11-21 02:57:15 +0000458 /* map page for use */
459 dma = dma_map_page(rx_ring->dev, page, 0,
460 PAGE_SIZE, DMA_FROM_DEVICE);
Emil Tantilovbafa5782014-11-08 01:39:15 +0000461
462 /* if mapping failed free memory back to system since
463 * there isn't much point in holding memory we can't use
464 */
465 if (dma_mapping_error(rx_ring->dev, dma)) {
Emil Tantilovbad17232014-11-21 02:57:15 +0000466 __free_page(page);
Emil Tantilovbafa5782014-11-08 01:39:15 +0000467
468 rx_ring->rx_stats.alloc_rx_buff_failed++;
469 return false;
470 }
471
Emil Tantilovbafa5782014-11-08 01:39:15 +0000472 bi->dma = dma;
Emil Tantilovbad17232014-11-21 02:57:15 +0000473 bi->page = page;
474 bi->page_offset = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000475
476 return true;
477}
478
Greg Rose92915f72010-01-09 02:24:10 +0000479/**
480 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
Emil Tantilov095e2612014-01-17 18:30:00 -0800481 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
Emil Tantilovbafa5782014-11-08 01:39:15 +0000482 * @cleaned_count: number of buffers to replace
Greg Rose92915f72010-01-09 02:24:10 +0000483 **/
Emil Tantilov095e2612014-01-17 18:30:00 -0800484static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
Emil Tantilovbafa5782014-11-08 01:39:15 +0000485 u16 cleaned_count)
Greg Rose92915f72010-01-09 02:24:10 +0000486{
Greg Rose92915f72010-01-09 02:24:10 +0000487 union ixgbe_adv_rx_desc *rx_desc;
488 struct ixgbevf_rx_buffer *bi;
Alexander Duyckfb401952012-05-11 08:33:16 +0000489 unsigned int i = rx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +0000490
Emil Tantilovbafa5782014-11-08 01:39:15 +0000491 /* nothing to do or no valid netdev defined */
492 if (!cleaned_count || !rx_ring->netdev)
493 return;
Greg Roseb9dd2452012-11-02 05:50:21 +0000494
Emil Tantilovbafa5782014-11-08 01:39:15 +0000495 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
496 bi = &rx_ring->rx_buffer_info[i];
497 i -= rx_ring->count;
Greg Roseb9dd2452012-11-02 05:50:21 +0000498
Emil Tantilovbafa5782014-11-08 01:39:15 +0000499 do {
Emil Tantilovbad17232014-11-21 02:57:15 +0000500 if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
Emil Tantilovbafa5782014-11-08 01:39:15 +0000501 break;
Emil Tantilov05d063a2014-01-17 18:29:59 -0800502
Emil Tantilovbafa5782014-11-08 01:39:15 +0000503 /* Refresh the desc even if pkt_addr didn't change
504 * because each write-back erases this info.
505 */
Emil Tantilovbad17232014-11-21 02:57:15 +0000506 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Greg Rose92915f72010-01-09 02:24:10 +0000507
Emil Tantilovbafa5782014-11-08 01:39:15 +0000508 rx_desc++;
509 bi++;
Greg Rose92915f72010-01-09 02:24:10 +0000510 i++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000511 if (unlikely(!i)) {
512 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
513 bi = rx_ring->rx_buffer_info;
514 i -= rx_ring->count;
515 }
Greg Rose92915f72010-01-09 02:24:10 +0000516
Emil Tantilovbafa5782014-11-08 01:39:15 +0000517 /* clear the hdr_addr for the next_to_use descriptor */
518 rx_desc->read.hdr_addr = 0;
519
520 cleaned_count--;
521 } while (cleaned_count);
522
523 i += rx_ring->count;
524
525 if (rx_ring->next_to_use != i) {
526 /* record the next descriptor to use */
527 rx_ring->next_to_use = i;
528
Emil Tantilovbad17232014-11-21 02:57:15 +0000529 /* update next to alloc since we have filled the ring */
530 rx_ring->next_to_alloc = i;
531
Emil Tantilovbafa5782014-11-08 01:39:15 +0000532 /* Force memory writes to complete before letting h/w
533 * know there are new descriptors to fetch. (Only
534 * applicable for weak-ordered memory model archs,
535 * such as IA-64).
536 */
537 wmb();
538 ixgbevf_write_tail(rx_ring, i);
539 }
Greg Rose92915f72010-01-09 02:24:10 +0000540}
541
Emil Tantilovbad17232014-11-21 02:57:15 +0000542/* ixgbevf_pull_tail - ixgbevf specific version of skb_pull_tail
543 * @rx_ring: rx descriptor ring packet is being transacted on
544 * @skb: pointer to current skb being adjusted
545 *
546 * This function is an ixgbevf specific version of __pskb_pull_tail. The
547 * main difference between this version and the original function is that
548 * this function can make several assumptions about the state of things
549 * that allow for significant optimizations versus the standard function.
550 * As a result we can do things like drop a frag and maintain an accurate
551 * truesize for the skb.
552 */
553static void ixgbevf_pull_tail(struct ixgbevf_ring *rx_ring,
554 struct sk_buff *skb)
555{
556 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
557 unsigned char *va;
558 unsigned int pull_len;
559
560 /* it is valid to use page_address instead of kmap since we are
561 * working with pages allocated out of the lomem pool per
562 * alloc_page(GFP_ATOMIC)
563 */
564 va = skb_frag_address(frag);
565
566 /* we need the header to contain the greater of either ETH_HLEN or
567 * 60 bytes if the skb->len is less than 60 for skb_pad.
568 */
569 pull_len = eth_get_headlen(va, IXGBEVF_RX_HDR_SIZE);
570
571 /* align pull length to size of long to optimize memcpy performance */
572 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
573
574 /* update all of the pointers */
575 skb_frag_size_sub(frag, pull_len);
576 frag->page_offset += pull_len;
577 skb->data_len -= pull_len;
578 skb->tail += pull_len;
579}
580
581/* ixgbevf_cleanup_headers - Correct corrupted or empty headers
582 * @rx_ring: rx descriptor ring packet is being transacted on
583 * @rx_desc: pointer to the EOP Rx descriptor
584 * @skb: pointer to current skb being fixed
585 *
586 * Check for corrupted packet headers caused by senders on the local L2
587 * embedded NIC switch not setting up their Tx Descriptors right. These
588 * should be very rare.
589 *
590 * Also address the case where we are pulling data in on pages only
591 * and as such no data is present in the skb header.
592 *
593 * In addition if skb is not at least 60 bytes we need to pad it so that
594 * it is large enough to qualify as a valid Ethernet frame.
595 *
596 * Returns true if an error was encountered and skb was freed.
597 */
598static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
599 union ixgbe_adv_rx_desc *rx_desc,
600 struct sk_buff *skb)
601{
602 /* verify that the packet does not have any known errors */
603 if (unlikely(ixgbevf_test_staterr(rx_desc,
604 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
605 struct net_device *netdev = rx_ring->netdev;
606
607 if (!(netdev->features & NETIF_F_RXALL)) {
608 dev_kfree_skb_any(skb);
609 return true;
610 }
611 }
612
613 /* place header in linear portion of buffer */
614 if (skb_is_nonlinear(skb))
615 ixgbevf_pull_tail(rx_ring, skb);
616
617 /* if skb_pad returns an error the skb was freed */
618 if (unlikely(skb->len < 60)) {
619 int pad_len = 60 - skb->len;
620
621 if (skb_pad(skb, pad_len))
622 return true;
623 __skb_put(skb, pad_len);
624 }
625
626 return false;
627}
628
629/* ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
630 * @rx_ring: rx descriptor ring to store buffers on
631 * @old_buff: donor buffer to have page reused
632 *
633 * Synchronizes page for reuse by the adapter
634 */
635static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
636 struct ixgbevf_rx_buffer *old_buff)
637{
638 struct ixgbevf_rx_buffer *new_buff;
639 u16 nta = rx_ring->next_to_alloc;
640
641 new_buff = &rx_ring->rx_buffer_info[nta];
642
643 /* update, and store next to alloc */
644 nta++;
645 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
646
647 /* transfer page from old buffer to new buffer */
648 new_buff->page = old_buff->page;
649 new_buff->dma = old_buff->dma;
650 new_buff->page_offset = old_buff->page_offset;
651
652 /* sync the buffer for use by the device */
653 dma_sync_single_range_for_device(rx_ring->dev, new_buff->dma,
654 new_buff->page_offset,
655 IXGBEVF_RX_BUFSZ,
656 DMA_FROM_DEVICE);
657}
658
659static inline bool ixgbevf_page_is_reserved(struct page *page)
660{
661 return (page_to_nid(page) != numa_mem_id()) || page->pfmemalloc;
662}
663
664/* ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
665 * @rx_ring: rx descriptor ring to transact packets on
666 * @rx_buffer: buffer containing page to add
667 * @rx_desc: descriptor containing length of buffer written by hardware
668 * @skb: sk_buff to place the data into
669 *
670 * This function will add the data contained in rx_buffer->page to the skb.
671 * This is done either through a direct copy if the data in the buffer is
672 * less than the skb header size, otherwise it will just attach the page as
673 * a frag to the skb.
674 *
675 * The function will then update the page offset if necessary and return
676 * true if the buffer can be reused by the adapter.
677 */
678static bool ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
679 struct ixgbevf_rx_buffer *rx_buffer,
680 union ixgbe_adv_rx_desc *rx_desc,
681 struct sk_buff *skb)
682{
683 struct page *page = rx_buffer->page;
684 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
685#if (PAGE_SIZE < 8192)
686 unsigned int truesize = IXGBEVF_RX_BUFSZ;
687#else
688 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
689#endif
690
691 if ((size <= IXGBEVF_RX_HDR_SIZE) && !skb_is_nonlinear(skb)) {
692 unsigned char *va = page_address(page) + rx_buffer->page_offset;
693
694 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
695
696 /* page is not reserved, we can reuse buffer as is */
697 if (likely(!ixgbevf_page_is_reserved(page)))
698 return true;
699
700 /* this page cannot be reused so discard it */
701 put_page(page);
702 return false;
703 }
704
705 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
706 rx_buffer->page_offset, size, truesize);
707
708 /* avoid re-using remote pages */
709 if (unlikely(ixgbevf_page_is_reserved(page)))
710 return false;
711
712#if (PAGE_SIZE < 8192)
713 /* if we are only owner of page we can reuse it */
714 if (unlikely(page_count(page) != 1))
715 return false;
716
717 /* flip page offset to other buffer */
718 rx_buffer->page_offset ^= IXGBEVF_RX_BUFSZ;
719
720#else
721 /* move offset up to the next cache line */
722 rx_buffer->page_offset += truesize;
723
724 if (rx_buffer->page_offset > (PAGE_SIZE - IXGBEVF_RX_BUFSZ))
725 return false;
726
727#endif
728 /* Even if we own the page, we are not allowed to use atomic_set()
729 * This would break get_page_unless_zero() users.
730 */
731 atomic_inc(&page->_count);
732
733 return true;
734}
735
736static struct sk_buff *ixgbevf_fetch_rx_buffer(struct ixgbevf_ring *rx_ring,
737 union ixgbe_adv_rx_desc *rx_desc,
738 struct sk_buff *skb)
739{
740 struct ixgbevf_rx_buffer *rx_buffer;
741 struct page *page;
742
743 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
744 page = rx_buffer->page;
745 prefetchw(page);
746
747 if (likely(!skb)) {
748 void *page_addr = page_address(page) +
749 rx_buffer->page_offset;
750
751 /* prefetch first cache line of first page */
752 prefetch(page_addr);
753#if L1_CACHE_BYTES < 128
754 prefetch(page_addr + L1_CACHE_BYTES);
755#endif
756
757 /* allocate a skb to store the frags */
758 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
759 IXGBEVF_RX_HDR_SIZE);
760 if (unlikely(!skb)) {
761 rx_ring->rx_stats.alloc_rx_buff_failed++;
762 return NULL;
763 }
764
765 /* we will be copying header into skb->data in
766 * pskb_may_pull so it is in our interest to prefetch
767 * it now to avoid a possible cache miss
768 */
769 prefetchw(skb->data);
770 }
771
772 /* we are reusing so sync this buffer for CPU use */
773 dma_sync_single_range_for_cpu(rx_ring->dev,
774 rx_buffer->dma,
775 rx_buffer->page_offset,
776 IXGBEVF_RX_BUFSZ,
777 DMA_FROM_DEVICE);
778
779 /* pull page into skb */
780 if (ixgbevf_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
781 /* hand second half of page back to the ring */
782 ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
783 } else {
784 /* we are not reusing the buffer so unmap it */
785 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
786 PAGE_SIZE, DMA_FROM_DEVICE);
787 }
788
789 /* clear contents of buffer_info */
790 rx_buffer->dma = 0;
791 rx_buffer->page = NULL;
792
793 return skb;
794}
795
Greg Rose92915f72010-01-09 02:24:10 +0000796static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000797 u32 qmask)
Greg Rose92915f72010-01-09 02:24:10 +0000798{
Greg Rose92915f72010-01-09 02:24:10 +0000799 struct ixgbe_hw *hw = &adapter->hw;
800
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000801 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
Greg Rose92915f72010-01-09 02:24:10 +0000802}
803
Jacob Keller08e50a22013-09-21 06:24:14 +0000804static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
805 struct ixgbevf_ring *rx_ring,
806 int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000807{
Greg Rose92915f72010-01-09 02:24:10 +0000808 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000809 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
Emil Tantilovbad17232014-11-21 02:57:15 +0000810 struct sk_buff *skb = rx_ring->skb;
Greg Rose92915f72010-01-09 02:24:10 +0000811
Emil Tantilov0579eef2014-11-08 01:39:35 +0000812 do {
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000813 union ixgbe_adv_rx_desc *rx_desc;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000814
Emil Tantilov0579eef2014-11-08 01:39:35 +0000815 /* return some buffers to hardware, one at a time is too slow */
816 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
817 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
818 cleaned_count = 0;
819 }
820
Emil Tantilovbad17232014-11-21 02:57:15 +0000821 rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000822
823 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_DD))
Greg Rose92915f72010-01-09 02:24:10 +0000824 break;
Greg Rose92915f72010-01-09 02:24:10 +0000825
Emil Tantilov0579eef2014-11-08 01:39:35 +0000826 /* This memory barrier is needed to keep us from reading
827 * any other fields out of the rx_desc until we know the
828 * RXD_STAT_DD bit is set
829 */
830 rmb();
Emil Tantilovec62fe22014-11-08 01:39:20 +0000831
Emil Tantilovbad17232014-11-21 02:57:15 +0000832 /* retrieve a buffer from the ring */
833 skb = ixgbevf_fetch_rx_buffer(rx_ring, rx_desc, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000834
Emil Tantilovbad17232014-11-21 02:57:15 +0000835 /* exit if we failed to retrieve a buffer */
836 if (!skb)
837 break;
Greg Rose92915f72010-01-09 02:24:10 +0000838
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000839 cleaned_count++;
840
Emil Tantilovbad17232014-11-21 02:57:15 +0000841 /* fetch next buffer in frame if non-eop */
842 if (ixgbevf_is_non_eop(rx_ring, rx_desc))
Emil Tantilov0579eef2014-11-08 01:39:35 +0000843 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000844
Emil Tantilovbad17232014-11-21 02:57:15 +0000845 /* verify the packet layout is correct */
846 if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
847 skb = NULL;
Emil Tantilov0579eef2014-11-08 01:39:35 +0000848 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000849 }
850
Greg Rose92915f72010-01-09 02:24:10 +0000851 /* probably a little skewed due to removing CRC */
852 total_rx_bytes += skb->len;
853 total_rx_packets++;
854
John Fastabend815cccb2012-10-24 08:13:09 +0000855 /* Workaround hardware that can't do proper VEPA multicast
856 * source pruning.
857 */
Florian Fainellibd9d5592014-02-28 15:46:49 -0800858 if ((skb->pkt_type == PACKET_BROADCAST ||
859 skb->pkt_type == PACKET_MULTICAST) &&
Emil Tantilov095e2612014-01-17 18:30:00 -0800860 ether_addr_equal(rx_ring->netdev->dev_addr,
Joe Perches7367d0b2013-09-01 11:51:23 -0700861 eth_hdr(skb)->h_source)) {
John Fastabend815cccb2012-10-24 08:13:09 +0000862 dev_kfree_skb_irq(skb);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000863 continue;
John Fastabend815cccb2012-10-24 08:13:09 +0000864 }
865
Emil Tantilovdff80522014-11-08 01:39:25 +0000866 /* populate checksum, VLAN, and protocol */
867 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
868
869 ixgbevf_rx_skb(q_vector, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000870
Emil Tantilovbad17232014-11-21 02:57:15 +0000871 /* reset skb pointer */
872 skb = NULL;
873
Emil Tantilov0579eef2014-11-08 01:39:35 +0000874 /* update budget accounting */
875 budget--;
876 } while (likely(budget));
Greg Rose92915f72010-01-09 02:24:10 +0000877
Emil Tantilovbad17232014-11-21 02:57:15 +0000878 /* place incomplete frames back on ring for completion */
879 rx_ring->skb = skb;
880
Eric Dumazet4197aa72011-06-22 05:01:35 +0000881 u64_stats_update_begin(&rx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800882 rx_ring->stats.packets += total_rx_packets;
883 rx_ring->stats.bytes += total_rx_bytes;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000884 u64_stats_update_end(&rx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000885 q_vector->rx.total_packets += total_rx_packets;
886 q_vector->rx.total_bytes += total_rx_bytes;
Greg Rose92915f72010-01-09 02:24:10 +0000887
Jacob Keller08e50a22013-09-21 06:24:14 +0000888 return total_rx_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000889}
890
891/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000892 * ixgbevf_poll - NAPI polling calback
Greg Rose92915f72010-01-09 02:24:10 +0000893 * @napi: napi struct with our devices info in it
894 * @budget: amount of work driver is allowed to do this pass, in packets
895 *
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000896 * This function will clean more than one or more rings associated with a
Greg Rose92915f72010-01-09 02:24:10 +0000897 * q_vector.
898 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000899static int ixgbevf_poll(struct napi_struct *napi, int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000900{
901 struct ixgbevf_q_vector *q_vector =
902 container_of(napi, struct ixgbevf_q_vector, napi);
903 struct ixgbevf_adapter *adapter = q_vector->adapter;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000904 struct ixgbevf_ring *ring;
905 int per_ring_budget;
906 bool clean_complete = true;
907
908 ixgbevf_for_each_ring(ring, q_vector->tx)
909 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
Greg Rose92915f72010-01-09 02:24:10 +0000910
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000911#ifdef CONFIG_NET_RX_BUSY_POLL
912 if (!ixgbevf_qv_lock_napi(q_vector))
913 return budget;
914#endif
915
Greg Rose92915f72010-01-09 02:24:10 +0000916 /* attempt to distribute budget to each queue fairly, but don't allow
917 * the budget to go below 1 because we'll exit polling */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000918 if (q_vector->rx.count > 1)
919 per_ring_budget = max(budget/q_vector->rx.count, 1);
920 else
921 per_ring_budget = budget;
Greg Rose92915f72010-01-09 02:24:10 +0000922
Greg Rose366c1092012-11-13 04:03:18 +0000923 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000924 ixgbevf_for_each_ring(ring, q_vector->rx)
Jacob Keller08e50a22013-09-21 06:24:14 +0000925 clean_complete &= (ixgbevf_clean_rx_irq(q_vector, ring,
926 per_ring_budget)
927 < per_ring_budget);
Greg Rose366c1092012-11-13 04:03:18 +0000928 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
Greg Rose92915f72010-01-09 02:24:10 +0000929
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000930#ifdef CONFIG_NET_RX_BUSY_POLL
931 ixgbevf_qv_unlock_napi(q_vector);
932#endif
933
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000934 /* If all work not completed, return budget and keep polling */
935 if (!clean_complete)
936 return budget;
937 /* all work done, exit the polling mode */
938 napi_complete(napi);
939 if (adapter->rx_itr_setting & 1)
940 ixgbevf_set_itr(q_vector);
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000941 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
942 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000943 ixgbevf_irq_enable_queues(adapter,
944 1 << q_vector->v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000945
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000946 return 0;
Greg Rose92915f72010-01-09 02:24:10 +0000947}
948
Greg Rosece422602012-05-22 02:17:49 +0000949/**
950 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
951 * @q_vector: structure containing interrupt and ring information
952 */
Jacob Keller38496232013-10-22 06:19:18 +0000953void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
Greg Rosece422602012-05-22 02:17:49 +0000954{
955 struct ixgbevf_adapter *adapter = q_vector->adapter;
956 struct ixgbe_hw *hw = &adapter->hw;
957 int v_idx = q_vector->v_idx;
958 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
959
960 /*
961 * set the WDIS bit to not clear the timer bits and cause an
962 * immediate assertion of the interrupt
963 */
964 itr_reg |= IXGBE_EITR_CNT_WDIS;
965
966 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
967}
Greg Rose92915f72010-01-09 02:24:10 +0000968
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000969#ifdef CONFIG_NET_RX_BUSY_POLL
970/* must be called with local_bh_disable()d */
971static int ixgbevf_busy_poll_recv(struct napi_struct *napi)
972{
973 struct ixgbevf_q_vector *q_vector =
974 container_of(napi, struct ixgbevf_q_vector, napi);
975 struct ixgbevf_adapter *adapter = q_vector->adapter;
976 struct ixgbevf_ring *ring;
977 int found = 0;
978
979 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
980 return LL_FLUSH_FAILED;
981
982 if (!ixgbevf_qv_lock_poll(q_vector))
983 return LL_FLUSH_BUSY;
984
985 ixgbevf_for_each_ring(ring, q_vector->rx) {
986 found = ixgbevf_clean_rx_irq(q_vector, ring, 4);
Jacob Keller3b5dca22013-09-21 06:24:25 +0000987#ifdef BP_EXTENDED_STATS
988 if (found)
Emil Tantilov095e2612014-01-17 18:30:00 -0800989 ring->stats.cleaned += found;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000990 else
Emil Tantilov095e2612014-01-17 18:30:00 -0800991 ring->stats.misses++;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000992#endif
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000993 if (found)
994 break;
995 }
996
997 ixgbevf_qv_unlock_poll(q_vector);
998
999 return found;
1000}
1001#endif /* CONFIG_NET_RX_BUSY_POLL */
1002
Greg Rose92915f72010-01-09 02:24:10 +00001003/**
1004 * ixgbevf_configure_msix - Configure MSI-X hardware
1005 * @adapter: board private structure
1006 *
1007 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1008 * interrupts.
1009 **/
1010static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1011{
1012 struct ixgbevf_q_vector *q_vector;
Alexander Duyck6b43c442012-05-11 08:32:45 +00001013 int q_vectors, v_idx;
Greg Rose92915f72010-01-09 02:24:10 +00001014
1015 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001016 adapter->eims_enable_mask = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001017
1018 /*
1019 * Populate the IVAR table and set the ITR values to the
1020 * corresponding register.
1021 */
1022 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
Alexander Duyck6b43c442012-05-11 08:32:45 +00001023 struct ixgbevf_ring *ring;
Greg Rose92915f72010-01-09 02:24:10 +00001024 q_vector = adapter->q_vector[v_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001025
Alexander Duyck6b43c442012-05-11 08:32:45 +00001026 ixgbevf_for_each_ring(ring, q_vector->rx)
1027 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +00001028
Alexander Duyck6b43c442012-05-11 08:32:45 +00001029 ixgbevf_for_each_ring(ring, q_vector->tx)
1030 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +00001031
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001032 if (q_vector->tx.ring && !q_vector->rx.ring) {
1033 /* tx only vector */
1034 if (adapter->tx_itr_setting == 1)
1035 q_vector->itr = IXGBE_10K_ITR;
1036 else
1037 q_vector->itr = adapter->tx_itr_setting;
1038 } else {
1039 /* rx or rx/tx vector */
1040 if (adapter->rx_itr_setting == 1)
1041 q_vector->itr = IXGBE_20K_ITR;
1042 else
1043 q_vector->itr = adapter->rx_itr_setting;
1044 }
Greg Rose92915f72010-01-09 02:24:10 +00001045
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001046 /* add q_vector eims value to global eims_enable_mask */
1047 adapter->eims_enable_mask |= 1 << v_idx;
1048
1049 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001050 }
1051
1052 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001053 /* setup eims_other and add value to global eims_enable_mask */
1054 adapter->eims_other = 1 << v_idx;
1055 adapter->eims_enable_mask |= adapter->eims_other;
Greg Rose92915f72010-01-09 02:24:10 +00001056}
1057
1058enum latency_range {
1059 lowest_latency = 0,
1060 low_latency = 1,
1061 bulk_latency = 2,
1062 latency_invalid = 255
1063};
1064
1065/**
1066 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001067 * @q_vector: structure containing interrupt and ring information
1068 * @ring_container: structure containing ring performance data
Greg Rose92915f72010-01-09 02:24:10 +00001069 *
1070 * Stores a new ITR value based on packets and byte
1071 * counts during the last interrupt. The advantage of per interrupt
1072 * computation is faster updates and more accurate ITR for the current
1073 * traffic pattern. Constants in this function were computed
1074 * based on theoretical maximum wire speed and thresholds were set based
1075 * on testing data as well as attempting to minimize response time
1076 * while increasing bulk throughput.
1077 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001078static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1079 struct ixgbevf_ring_container *ring_container)
Greg Rose92915f72010-01-09 02:24:10 +00001080{
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001081 int bytes = ring_container->total_bytes;
1082 int packets = ring_container->total_packets;
Greg Rose92915f72010-01-09 02:24:10 +00001083 u32 timepassed_us;
1084 u64 bytes_perint;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001085 u8 itr_setting = ring_container->itr;
Greg Rose92915f72010-01-09 02:24:10 +00001086
1087 if (packets == 0)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001088 return;
Greg Rose92915f72010-01-09 02:24:10 +00001089
1090 /* simple throttlerate management
1091 * 0-20MB/s lowest (100000 ints/s)
1092 * 20-100MB/s low (20000 ints/s)
1093 * 100-1249MB/s bulk (8000 ints/s)
1094 */
1095 /* what was last interrupt timeslice? */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001096 timepassed_us = q_vector->itr >> 2;
Greg Rose92915f72010-01-09 02:24:10 +00001097 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1098
1099 switch (itr_setting) {
1100 case lowest_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001101 if (bytes_perint > 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001102 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001103 break;
1104 case low_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001105 if (bytes_perint > 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001106 itr_setting = bulk_latency;
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001107 else if (bytes_perint <= 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001108 itr_setting = lowest_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001109 break;
1110 case bulk_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001111 if (bytes_perint <= 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001112 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001113 break;
1114 }
1115
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001116 /* clear work counters since we have the values we need */
1117 ring_container->total_bytes = 0;
1118 ring_container->total_packets = 0;
1119
1120 /* write updated itr to ring container */
1121 ring_container->itr = itr_setting;
Greg Rose92915f72010-01-09 02:24:10 +00001122}
1123
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001124static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
Greg Rose92915f72010-01-09 02:24:10 +00001125{
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001126 u32 new_itr = q_vector->itr;
1127 u8 current_itr;
Greg Rose92915f72010-01-09 02:24:10 +00001128
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001129 ixgbevf_update_itr(q_vector, &q_vector->tx);
1130 ixgbevf_update_itr(q_vector, &q_vector->rx);
Greg Rose92915f72010-01-09 02:24:10 +00001131
Alexander Duyck6b43c442012-05-11 08:32:45 +00001132 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Greg Rose92915f72010-01-09 02:24:10 +00001133
1134 switch (current_itr) {
1135 /* counts and packets in update_itr are dependent on these numbers */
1136 case lowest_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001137 new_itr = IXGBE_100K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001138 break;
1139 case low_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001140 new_itr = IXGBE_20K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001141 break;
1142 case bulk_latency:
1143 default:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001144 new_itr = IXGBE_8K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001145 break;
1146 }
1147
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001148 if (new_itr != q_vector->itr) {
Greg Rose92915f72010-01-09 02:24:10 +00001149 /* do an exponential smoothing */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001150 new_itr = (10 * new_itr * q_vector->itr) /
1151 ((9 * new_itr) + q_vector->itr);
1152
1153 /* save the algorithm value here */
1154 q_vector->itr = new_itr;
1155
1156 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001157 }
Greg Rose92915f72010-01-09 02:24:10 +00001158}
1159
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001160static irqreturn_t ixgbevf_msix_other(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +00001161{
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001162 struct ixgbevf_adapter *adapter = data;
Greg Rose92915f72010-01-09 02:24:10 +00001163 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001164
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001165 hw->mac.get_link_status = 1;
Greg Rose375b27c2012-01-18 22:13:31 +00001166
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00001167 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1168 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Don Skidmorec7bb4172013-10-01 04:33:49 -07001169 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose3a2c4032012-02-01 01:28:15 +00001170
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001171 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1172
Greg Rose92915f72010-01-09 02:24:10 +00001173 return IRQ_HANDLED;
1174}
1175
Greg Rose92915f72010-01-09 02:24:10 +00001176/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001177 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
Greg Rose92915f72010-01-09 02:24:10 +00001178 * @irq: unused
1179 * @data: pointer to our q_vector struct for this interrupt vector
1180 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001181static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +00001182{
1183 struct ixgbevf_q_vector *q_vector = data;
Greg Rose92915f72010-01-09 02:24:10 +00001184
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001185 /* EIAM disabled interrupts (on this vector) for us */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001186 if (q_vector->rx.ring || q_vector->tx.ring)
1187 napi_schedule(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001188
1189 return IRQ_HANDLED;
1190}
1191
1192static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
1193 int r_idx)
1194{
1195 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1196
Don Skidmore87e70ab2014-01-16 02:30:08 -08001197 a->rx_ring[r_idx]->next = q_vector->rx.ring;
1198 q_vector->rx.ring = a->rx_ring[r_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001199 q_vector->rx.count++;
Greg Rose92915f72010-01-09 02:24:10 +00001200}
1201
1202static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
1203 int t_idx)
1204{
1205 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1206
Don Skidmore87e70ab2014-01-16 02:30:08 -08001207 a->tx_ring[t_idx]->next = q_vector->tx.ring;
1208 q_vector->tx.ring = a->tx_ring[t_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001209 q_vector->tx.count++;
Greg Rose92915f72010-01-09 02:24:10 +00001210}
1211
1212/**
1213 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
1214 * @adapter: board private structure to initialize
1215 *
1216 * This function maps descriptor rings to the queue-specific vectors
1217 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1218 * one vector per ring/queue, but on a constrained vector budget, we
1219 * group the rings as "efficiently" as possible. You would add new
1220 * mapping configurations in here.
1221 **/
1222static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
1223{
1224 int q_vectors;
1225 int v_start = 0;
1226 int rxr_idx = 0, txr_idx = 0;
1227 int rxr_remaining = adapter->num_rx_queues;
1228 int txr_remaining = adapter->num_tx_queues;
1229 int i, j;
1230 int rqpv, tqpv;
1231 int err = 0;
1232
1233 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1234
1235 /*
1236 * The ideal configuration...
1237 * We have enough vectors to map one per queue.
1238 */
1239 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1240 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1241 map_vector_to_rxq(adapter, v_start, rxr_idx);
1242
1243 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1244 map_vector_to_txq(adapter, v_start, txr_idx);
1245 goto out;
1246 }
1247
1248 /*
1249 * If we don't have enough vectors for a 1-to-1
1250 * mapping, we'll have to group them so there are
1251 * multiple queues per vector.
1252 */
1253 /* Re-adjusting *qpv takes care of the remainder. */
1254 for (i = v_start; i < q_vectors; i++) {
1255 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1256 for (j = 0; j < rqpv; j++) {
1257 map_vector_to_rxq(adapter, i, rxr_idx);
1258 rxr_idx++;
1259 rxr_remaining--;
1260 }
1261 }
1262 for (i = v_start; i < q_vectors; i++) {
1263 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1264 for (j = 0; j < tqpv; j++) {
1265 map_vector_to_txq(adapter, i, txr_idx);
1266 txr_idx++;
1267 txr_remaining--;
1268 }
1269 }
1270
1271out:
1272 return err;
1273}
1274
1275/**
1276 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1277 * @adapter: board private structure
1278 *
1279 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1280 * interrupts from the kernel.
1281 **/
1282static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1283{
1284 struct net_device *netdev = adapter->netdev;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001285 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1286 int vector, err;
Greg Rose92915f72010-01-09 02:24:10 +00001287 int ri = 0, ti = 0;
1288
Greg Rose92915f72010-01-09 02:24:10 +00001289 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001290 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1291 struct msix_entry *entry = &adapter->msix_entries[vector];
Greg Rose92915f72010-01-09 02:24:10 +00001292
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001293 if (q_vector->tx.ring && q_vector->rx.ring) {
1294 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1295 "%s-%s-%d", netdev->name, "TxRx", ri++);
1296 ti++;
1297 } else if (q_vector->rx.ring) {
1298 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1299 "%s-%s-%d", netdev->name, "rx", ri++);
1300 } else if (q_vector->tx.ring) {
1301 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1302 "%s-%s-%d", netdev->name, "tx", ti++);
Greg Rose92915f72010-01-09 02:24:10 +00001303 } else {
1304 /* skip this unused q_vector */
1305 continue;
1306 }
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001307 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1308 q_vector->name, q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001309 if (err) {
1310 hw_dbg(&adapter->hw,
1311 "request_irq failed for MSIX interrupt "
1312 "Error: %d\n", err);
1313 goto free_queue_irqs;
1314 }
1315 }
1316
Greg Rose92915f72010-01-09 02:24:10 +00001317 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001318 &ixgbevf_msix_other, 0, netdev->name, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001319 if (err) {
1320 hw_dbg(&adapter->hw,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001321 "request_irq for msix_other failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00001322 goto free_queue_irqs;
1323 }
1324
1325 return 0;
1326
1327free_queue_irqs:
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001328 while (vector) {
1329 vector--;
1330 free_irq(adapter->msix_entries[vector].vector,
1331 adapter->q_vector[vector]);
1332 }
xunleera1f6c6b2013-03-05 07:44:20 +00001333 /* This failure is non-recoverable - it indicates the system is
1334 * out of MSIX vector resources and the VF driver cannot run
1335 * without them. Set the number of msix vectors to zero
1336 * indicating that not enough can be allocated. The error
1337 * will be returned to the user indicating device open failed.
1338 * Any further attempts to force the driver to open will also
1339 * fail. The only way to recover is to unload the driver and
1340 * reload it again. If the system has recovered some MSIX
1341 * vectors then it may succeed.
1342 */
1343 adapter->num_msix_vectors = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001344 return err;
1345}
1346
1347static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1348{
1349 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1350
1351 for (i = 0; i < q_vectors; i++) {
1352 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001353 q_vector->rx.ring = NULL;
1354 q_vector->tx.ring = NULL;
1355 q_vector->rx.count = 0;
1356 q_vector->tx.count = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001357 }
1358}
1359
1360/**
1361 * ixgbevf_request_irq - initialize interrupts
1362 * @adapter: board private structure
1363 *
1364 * Attempts to configure interrupts using the best available
1365 * capabilities of the hardware and kernel.
1366 **/
1367static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1368{
1369 int err = 0;
1370
1371 err = ixgbevf_request_msix_irqs(adapter);
1372
1373 if (err)
1374 hw_dbg(&adapter->hw,
1375 "request_irq failed, Error %d\n", err);
1376
1377 return err;
1378}
1379
1380static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1381{
Greg Rose92915f72010-01-09 02:24:10 +00001382 int i, q_vectors;
1383
1384 q_vectors = adapter->num_msix_vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001385 i = q_vectors - 1;
1386
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001387 free_irq(adapter->msix_entries[i].vector, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001388 i--;
1389
1390 for (; i >= 0; i--) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001391 /* free only the irqs that were actually requested */
1392 if (!adapter->q_vector[i]->rx.ring &&
1393 !adapter->q_vector[i]->tx.ring)
1394 continue;
1395
Greg Rose92915f72010-01-09 02:24:10 +00001396 free_irq(adapter->msix_entries[i].vector,
1397 adapter->q_vector[i]);
1398 }
1399
1400 ixgbevf_reset_q_vectors(adapter);
1401}
1402
1403/**
1404 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1405 * @adapter: board private structure
1406 **/
1407static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1408{
Greg Rose92915f72010-01-09 02:24:10 +00001409 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001410 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001411
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001412 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001413 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001414 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001415
1416 IXGBE_WRITE_FLUSH(hw);
1417
1418 for (i = 0; i < adapter->num_msix_vectors; i++)
1419 synchronize_irq(adapter->msix_entries[i].vector);
1420}
1421
1422/**
1423 * ixgbevf_irq_enable - Enable default interrupt generation settings
1424 * @adapter: board private structure
1425 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001426static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001427{
1428 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001429
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001430 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1431 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1432 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
Greg Rose92915f72010-01-09 02:24:10 +00001433}
1434
1435/**
Don Skidmorede02dec2014-01-16 02:30:09 -08001436 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1437 * @adapter: board private structure
1438 * @ring: structure containing ring specific data
1439 *
1440 * Configure the Tx descriptor ring after a reset.
1441 **/
1442static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1443 struct ixgbevf_ring *ring)
1444{
1445 struct ixgbe_hw *hw = &adapter->hw;
1446 u64 tdba = ring->dma;
1447 int wait_loop = 10;
1448 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1449 u8 reg_idx = ring->reg_idx;
1450
1451 /* disable queue to avoid issues while updating state */
1452 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1453 IXGBE_WRITE_FLUSH(hw);
1454
1455 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1456 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1457 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1458 ring->count * sizeof(union ixgbe_adv_tx_desc));
1459
1460 /* disable head writeback */
1461 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1462 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1463
1464 /* enable relaxed ordering */
1465 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1466 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1467 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1468
1469 /* reset head and tail pointers */
1470 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1471 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001472 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001473
1474 /* reset ntu and ntc to place SW in sync with hardwdare */
1475 ring->next_to_clean = 0;
1476 ring->next_to_use = 0;
1477
1478 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1479 * to or less than the number of on chip descriptors, which is
1480 * currently 40.
1481 */
1482 txdctl |= (8 << 16); /* WTHRESH = 8 */
1483
1484 /* Setting PTHRESH to 32 both improves performance */
1485 txdctl |= (1 << 8) | /* HTHRESH = 1 */
1486 32; /* PTHRESH = 32 */
1487
1488 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1489
1490 /* poll to verify queue is enabled */
1491 do {
1492 usleep_range(1000, 2000);
1493 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1494 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1495 if (!wait_loop)
1496 pr_err("Could not enable Tx Queue %d\n", reg_idx);
1497}
1498
1499/**
Greg Rose92915f72010-01-09 02:24:10 +00001500 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1501 * @adapter: board private structure
1502 *
1503 * Configure the Tx unit of the MAC after a reset.
1504 **/
1505static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1506{
Don Skidmorede02dec2014-01-16 02:30:09 -08001507 u32 i;
Greg Rose92915f72010-01-09 02:24:10 +00001508
1509 /* Setup the HW Tx Head and Tail descriptor pointers */
Don Skidmorede02dec2014-01-16 02:30:09 -08001510 for (i = 0; i < adapter->num_tx_queues; i++)
1511 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001512}
1513
1514#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1515
1516static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1517{
Greg Rose92915f72010-01-09 02:24:10 +00001518 struct ixgbe_hw *hw = &adapter->hw;
1519 u32 srrctl;
1520
Greg Rose92915f72010-01-09 02:24:10 +00001521 srrctl = IXGBE_SRRCTL_DROP_EN;
1522
Emil Tantilovbad17232014-11-21 02:57:15 +00001523 srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1524 srrctl |= IXGBEVF_RX_BUFSZ >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001525 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
Greg Rose92915f72010-01-09 02:24:10 +00001526
Greg Rose92915f72010-01-09 02:24:10 +00001527 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1528}
1529
Don Skidmore1bb9c632013-09-21 01:57:33 +00001530static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1531{
1532 struct ixgbe_hw *hw = &adapter->hw;
1533
1534 /* PSRTYPE must be initialized in 82599 */
1535 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1536 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1537 IXGBE_PSRTYPE_L2HDR;
1538
1539 if (adapter->num_rx_queues > 1)
1540 psrtype |= 1 << 29;
1541
1542 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1543}
1544
Don Skidmorede02dec2014-01-16 02:30:09 -08001545#define IXGBEVF_MAX_RX_DESC_POLL 10
1546static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1547 struct ixgbevf_ring *ring)
1548{
1549 struct ixgbe_hw *hw = &adapter->hw;
1550 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1551 u32 rxdctl;
1552 u8 reg_idx = ring->reg_idx;
1553
Mark Rustad26597802014-03-04 03:02:45 +00001554 if (IXGBE_REMOVED(hw->hw_addr))
1555 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001556 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1557 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1558
1559 /* write value back with RXDCTL.ENABLE bit cleared */
1560 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1561
1562 /* the hardware may take up to 100us to really disable the rx queue */
1563 do {
1564 udelay(10);
1565 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1566 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1567
1568 if (!wait_loop)
1569 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1570 reg_idx);
1571}
1572
1573static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1574 struct ixgbevf_ring *ring)
1575{
1576 struct ixgbe_hw *hw = &adapter->hw;
1577 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1578 u32 rxdctl;
1579 u8 reg_idx = ring->reg_idx;
1580
Mark Rustad26597802014-03-04 03:02:45 +00001581 if (IXGBE_REMOVED(hw->hw_addr))
1582 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001583 do {
1584 usleep_range(1000, 2000);
1585 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1586 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1587
1588 if (!wait_loop)
1589 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1590 reg_idx);
1591}
1592
1593static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1594 struct ixgbevf_ring *ring)
1595{
1596 struct ixgbe_hw *hw = &adapter->hw;
1597 u64 rdba = ring->dma;
1598 u32 rxdctl;
1599 u8 reg_idx = ring->reg_idx;
1600
1601 /* disable queue to avoid issues while updating state */
1602 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1603 ixgbevf_disable_rx_queue(adapter, ring);
1604
1605 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1606 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1607 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1608 ring->count * sizeof(union ixgbe_adv_rx_desc));
1609
1610 /* enable relaxed ordering */
1611 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1612 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1613
1614 /* reset head and tail pointers */
1615 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1616 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001617 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001618
1619 /* reset ntu and ntc to place SW in sync with hardwdare */
1620 ring->next_to_clean = 0;
1621 ring->next_to_use = 0;
Emil Tantilovbad17232014-11-21 02:57:15 +00001622 ring->next_to_alloc = 0;
Don Skidmorede02dec2014-01-16 02:30:09 -08001623
1624 ixgbevf_configure_srrctl(adapter, reg_idx);
1625
Emil Tantilovbad17232014-11-21 02:57:15 +00001626 /* allow any size packet since we can handle overflow */
1627 rxdctl &= ~IXGBE_RXDCTL_RLPML_EN;
1628
Don Skidmorede02dec2014-01-16 02:30:09 -08001629 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1630 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1631
1632 ixgbevf_rx_desc_queue_enable(adapter, ring);
Emil Tantilov095e2612014-01-17 18:30:00 -08001633 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
Don Skidmorede02dec2014-01-16 02:30:09 -08001634}
1635
Greg Rose92915f72010-01-09 02:24:10 +00001636/**
1637 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1638 * @adapter: board private structure
1639 *
1640 * Configure the Rx unit of the MAC after a reset.
1641 **/
1642static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1643{
Don Skidmorede02dec2014-01-16 02:30:09 -08001644 int i;
Emil Tantilovbad17232014-11-21 02:57:15 +00001645 struct ixgbe_hw *hw = &adapter->hw;
1646 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00001647
Don Skidmore1bb9c632013-09-21 01:57:33 +00001648 ixgbevf_setup_psrtype(adapter);
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001649
Emil Tantilovbad17232014-11-21 02:57:15 +00001650 /* notify the PF of our intent to use this size of frame */
1651 ixgbevf_rlpml_set_vf(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
Greg Rose92915f72010-01-09 02:24:10 +00001652
Greg Rose92915f72010-01-09 02:24:10 +00001653 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1654 * the Base and Length of the Rx Descriptor Ring */
Don Skidmorede02dec2014-01-16 02:30:09 -08001655 for (i = 0; i < adapter->num_rx_queues; i++)
1656 ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001657}
1658
Patrick McHardy80d5c362013-04-19 02:04:28 +00001659static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1660 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001661{
1662 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1663 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001664 int err;
1665
John Fastabend55fdd45b2012-10-01 14:52:20 +00001666 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001667
Greg Rose92915f72010-01-09 02:24:10 +00001668 /* add VID to filter table */
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001669 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001670
John Fastabend55fdd45b2012-10-01 14:52:20 +00001671 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001672
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001673 /* translate error return types so error makes sense */
1674 if (err == IXGBE_ERR_MBX)
1675 return -EIO;
1676
1677 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1678 return -EACCES;
1679
Jiri Pirkodadcd652011-07-21 03:25:09 +00001680 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001681
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001682 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001683}
1684
Patrick McHardy80d5c362013-04-19 02:04:28 +00001685static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1686 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001687{
1688 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1689 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001690 int err = -EOPNOTSUPP;
Greg Rose92915f72010-01-09 02:24:10 +00001691
John Fastabend55fdd45b2012-10-01 14:52:20 +00001692 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001693
Greg Rose92915f72010-01-09 02:24:10 +00001694 /* remove VID from filter table */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001695 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001696
John Fastabend55fdd45b2012-10-01 14:52:20 +00001697 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001698
Jiri Pirkodadcd652011-07-21 03:25:09 +00001699 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001700
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001701 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001702}
1703
1704static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1705{
Jiri Pirkodadcd652011-07-21 03:25:09 +00001706 u16 vid;
Greg Rose92915f72010-01-09 02:24:10 +00001707
Jiri Pirkodadcd652011-07-21 03:25:09 +00001708 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001709 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1710 htons(ETH_P_8021Q), vid);
Greg Rose92915f72010-01-09 02:24:10 +00001711}
1712
Greg Rose46ec20f2011-05-13 01:33:42 +00001713static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1714{
1715 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1716 struct ixgbe_hw *hw = &adapter->hw;
1717 int count = 0;
1718
1719 if ((netdev_uc_count(netdev)) > 10) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001720 pr_err("Too many unicast filters - No Space\n");
Greg Rose46ec20f2011-05-13 01:33:42 +00001721 return -ENOSPC;
1722 }
1723
1724 if (!netdev_uc_empty(netdev)) {
1725 struct netdev_hw_addr *ha;
1726 netdev_for_each_uc_addr(ha, netdev) {
1727 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1728 udelay(200);
1729 }
1730 } else {
1731 /*
1732 * If the list is empty then send message to PF driver to
1733 * clear all macvlans on this VF.
1734 */
1735 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1736 }
1737
1738 return count;
1739}
1740
Greg Rose92915f72010-01-09 02:24:10 +00001741/**
Greg Rosedee847f2012-11-02 05:50:57 +00001742 * ixgbevf_set_rx_mode - Multicast and unicast set
Greg Rose92915f72010-01-09 02:24:10 +00001743 * @netdev: network interface device structure
1744 *
1745 * The set_rx_method entry point is called whenever the multicast address
Greg Rosedee847f2012-11-02 05:50:57 +00001746 * list, unicast address list or the network interface flags are updated.
1747 * This routine is responsible for configuring the hardware for proper
1748 * multicast mode and configuring requested unicast filters.
Greg Rose92915f72010-01-09 02:24:10 +00001749 **/
1750static void ixgbevf_set_rx_mode(struct net_device *netdev)
1751{
1752 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1753 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001754
John Fastabend55fdd45b2012-10-01 14:52:20 +00001755 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001756
Greg Rose92915f72010-01-09 02:24:10 +00001757 /* reprogram multicast list */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001758 hw->mac.ops.update_mc_addr_list(hw, netdev);
Greg Rose46ec20f2011-05-13 01:33:42 +00001759
1760 ixgbevf_write_uc_addr_list(netdev);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001761
John Fastabend55fdd45b2012-10-01 14:52:20 +00001762 spin_unlock_bh(&adapter->mbx_lock);
Greg Rose92915f72010-01-09 02:24:10 +00001763}
1764
1765static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1766{
1767 int q_idx;
1768 struct ixgbevf_q_vector *q_vector;
1769 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1770
1771 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Greg Rose92915f72010-01-09 02:24:10 +00001772 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001773#ifdef CONFIG_NET_RX_BUSY_POLL
1774 ixgbevf_qv_init_lock(adapter->q_vector[q_idx]);
1775#endif
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001776 napi_enable(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001777 }
1778}
1779
1780static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1781{
1782 int q_idx;
1783 struct ixgbevf_q_vector *q_vector;
1784 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1785
1786 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1787 q_vector = adapter->q_vector[q_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001788 napi_disable(&q_vector->napi);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001789#ifdef CONFIG_NET_RX_BUSY_POLL
1790 while (!ixgbevf_qv_disable(adapter->q_vector[q_idx])) {
1791 pr_info("QV %d locked\n", q_idx);
1792 usleep_range(1000, 20000);
1793 }
1794#endif /* CONFIG_NET_RX_BUSY_POLL */
Greg Rose92915f72010-01-09 02:24:10 +00001795 }
1796}
1797
Don Skidmore220fe052013-09-21 01:40:49 +00001798static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
1799{
1800 struct ixgbe_hw *hw = &adapter->hw;
1801 unsigned int def_q = 0;
1802 unsigned int num_tcs = 0;
1803 unsigned int num_rx_queues = 1;
1804 int err;
1805
1806 spin_lock_bh(&adapter->mbx_lock);
1807
1808 /* fetch queue configuration from the PF */
1809 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1810
1811 spin_unlock_bh(&adapter->mbx_lock);
1812
1813 if (err)
1814 return err;
1815
1816 if (num_tcs > 1) {
1817 /* update default Tx ring register index */
Don Skidmore87e70ab2014-01-16 02:30:08 -08001818 adapter->tx_ring[0]->reg_idx = def_q;
Don Skidmore220fe052013-09-21 01:40:49 +00001819
1820 /* we need as many queues as traffic classes */
1821 num_rx_queues = num_tcs;
1822 }
1823
1824 /* if we have a bad config abort request queue reset */
1825 if (adapter->num_rx_queues != num_rx_queues) {
1826 /* force mailbox timeout to prevent further messages */
1827 hw->mbx.timeout = 0;
1828
1829 /* wait for watchdog to come around and bail us out */
1830 adapter->flags |= IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
1831 }
1832
1833 return 0;
1834}
1835
Greg Rose92915f72010-01-09 02:24:10 +00001836static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1837{
Don Skidmore220fe052013-09-21 01:40:49 +00001838 ixgbevf_configure_dcb(adapter);
1839
Don Skidmorede02dec2014-01-16 02:30:09 -08001840 ixgbevf_set_rx_mode(adapter->netdev);
Greg Rose92915f72010-01-09 02:24:10 +00001841
1842 ixgbevf_restore_vlan(adapter);
1843
1844 ixgbevf_configure_tx(adapter);
1845 ixgbevf_configure_rx(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001846}
1847
Greg Rose33bd9f62010-03-19 02:59:52 +00001848static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1849{
1850 /* Only save pre-reset stats if there are some */
1851 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1852 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1853 adapter->stats.base_vfgprc;
1854 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1855 adapter->stats.base_vfgptc;
1856 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1857 adapter->stats.base_vfgorc;
1858 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1859 adapter->stats.base_vfgotc;
1860 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1861 adapter->stats.base_vfmprc;
1862 }
1863}
1864
1865static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1866{
1867 struct ixgbe_hw *hw = &adapter->hw;
1868
1869 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1870 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1871 adapter->stats.last_vfgorc |=
1872 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1873 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1874 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1875 adapter->stats.last_vfgotc |=
1876 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1877 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1878
1879 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1880 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1881 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1882 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1883 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1884}
1885
Alexander Duyck31186782012-07-20 08:09:58 +00001886static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1887{
1888 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck56e94092012-07-20 08:10:03 +00001889 int api[] = { ixgbe_mbox_api_11,
1890 ixgbe_mbox_api_10,
Alexander Duyck31186782012-07-20 08:09:58 +00001891 ixgbe_mbox_api_unknown };
1892 int err = 0, idx = 0;
1893
John Fastabend55fdd45b2012-10-01 14:52:20 +00001894 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001895
1896 while (api[idx] != ixgbe_mbox_api_unknown) {
1897 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1898 if (!err)
1899 break;
1900 idx++;
1901 }
1902
John Fastabend55fdd45b2012-10-01 14:52:20 +00001903 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001904}
1905
Greg Rose795180d2012-04-17 04:29:34 +00001906static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001907{
1908 struct net_device *netdev = adapter->netdev;
1909 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001910
1911 ixgbevf_configure_msix(adapter);
1912
John Fastabend55fdd45b2012-10-01 14:52:20 +00001913 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001914
Greg Rose92fe0bf2012-11-02 05:50:47 +00001915 if (is_valid_ether_addr(hw->mac.addr))
1916 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1917 else
1918 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001919
John Fastabend55fdd45b2012-10-01 14:52:20 +00001920 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001921
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001922 smp_mb__before_atomic();
Greg Rose92915f72010-01-09 02:24:10 +00001923 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1924 ixgbevf_napi_enable_all(adapter);
1925
1926 /* enable transmits */
1927 netif_tx_start_all_queues(netdev);
1928
Greg Rose33bd9f62010-03-19 02:59:52 +00001929 ixgbevf_save_reset_stats(adapter);
1930 ixgbevf_init_last_counter_stats(adapter);
1931
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001932 hw->mac.get_link_status = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001933 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose92915f72010-01-09 02:24:10 +00001934}
1935
Greg Rose795180d2012-04-17 04:29:34 +00001936void ixgbevf_up(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001937{
Greg Rose92915f72010-01-09 02:24:10 +00001938 struct ixgbe_hw *hw = &adapter->hw;
1939
1940 ixgbevf_configure(adapter);
1941
Greg Rose795180d2012-04-17 04:29:34 +00001942 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001943
1944 /* clear any pending interrupts, may auto mask */
1945 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1946
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001947 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001948}
1949
1950/**
1951 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
Greg Rose92915f72010-01-09 02:24:10 +00001952 * @rx_ring: ring to free buffers from
1953 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001954static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001955{
Emil Tantilovbad17232014-11-21 02:57:15 +00001956 struct device *dev = rx_ring->dev;
Greg Rose92915f72010-01-09 02:24:10 +00001957 unsigned long size;
1958 unsigned int i;
1959
Emil Tantilovbad17232014-11-21 02:57:15 +00001960 /* Free Rx ring sk_buff */
1961 if (rx_ring->skb) {
1962 dev_kfree_skb(rx_ring->skb);
1963 rx_ring->skb = NULL;
1964 }
1965
1966 /* ring already cleared, nothing to do */
Greg Rosec0456c22010-01-22 22:47:18 +00001967 if (!rx_ring->rx_buffer_info)
1968 return;
Greg Rose92915f72010-01-09 02:24:10 +00001969
Emil Tantilovbad17232014-11-21 02:57:15 +00001970 /* Free all the Rx ring pages */
Greg Rose92915f72010-01-09 02:24:10 +00001971 for (i = 0; i < rx_ring->count; i++) {
Emil Tantilovbad17232014-11-21 02:57:15 +00001972 struct ixgbevf_rx_buffer *rx_buffer;
Greg Rose92915f72010-01-09 02:24:10 +00001973
Emil Tantilovbad17232014-11-21 02:57:15 +00001974 rx_buffer = &rx_ring->rx_buffer_info[i];
1975 if (rx_buffer->dma)
1976 dma_unmap_page(dev, rx_buffer->dma,
1977 PAGE_SIZE, DMA_FROM_DEVICE);
1978 rx_buffer->dma = 0;
1979 if (rx_buffer->page)
1980 __free_page(rx_buffer->page);
1981 rx_buffer->page = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00001982 }
1983
1984 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1985 memset(rx_ring->rx_buffer_info, 0, size);
1986
1987 /* Zero out the descriptor ring */
1988 memset(rx_ring->desc, 0, rx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001989}
1990
1991/**
1992 * ixgbevf_clean_tx_ring - Free Tx Buffers
Greg Rose92915f72010-01-09 02:24:10 +00001993 * @tx_ring: ring to be cleaned
1994 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001995static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001996{
1997 struct ixgbevf_tx_buffer *tx_buffer_info;
1998 unsigned long size;
1999 unsigned int i;
2000
Greg Rosec0456c22010-01-22 22:47:18 +00002001 if (!tx_ring->tx_buffer_info)
2002 return;
2003
Greg Rose92915f72010-01-09 02:24:10 +00002004 /* Free all the Tx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00002005 for (i = 0; i < tx_ring->count; i++) {
2006 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00002007 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00002008 }
2009
2010 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2011 memset(tx_ring->tx_buffer_info, 0, size);
2012
2013 memset(tx_ring->desc, 0, tx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00002014}
2015
2016/**
2017 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2018 * @adapter: board private structure
2019 **/
2020static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2021{
2022 int i;
2023
2024 for (i = 0; i < adapter->num_rx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002025 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002026}
2027
2028/**
2029 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2030 * @adapter: board private structure
2031 **/
2032static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2033{
2034 int i;
2035
2036 for (i = 0; i < adapter->num_tx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002037 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002038}
2039
2040void ixgbevf_down(struct ixgbevf_adapter *adapter)
2041{
2042 struct net_device *netdev = adapter->netdev;
2043 struct ixgbe_hw *hw = &adapter->hw;
Don Skidmorede02dec2014-01-16 02:30:09 -08002044 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002045
2046 /* signal that we are down to the interrupt handler */
Mark Rustad5b346dc2014-03-04 03:02:18 +00002047 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2048 return; /* do nothing if already down */
Don Skidmore858c3dd2013-10-01 04:33:50 -07002049
2050 /* disable all enabled rx queues */
2051 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002052 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002053
2054 netif_tx_disable(netdev);
2055
2056 msleep(10);
2057
2058 netif_tx_stop_all_queues(netdev);
2059
2060 ixgbevf_irq_disable(adapter);
2061
2062 ixgbevf_napi_disable_all(adapter);
2063
2064 del_timer_sync(&adapter->watchdog_timer);
2065 /* can't call flush scheduled work here because it can deadlock
2066 * if linkwatch_event tries to acquire the rtnl_lock which we are
2067 * holding */
2068 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
2069 msleep(1);
2070
2071 /* disable transmits in the hardware now that interrupts are off */
2072 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmorede02dec2014-01-16 02:30:09 -08002073 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2074
2075 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2076 IXGBE_TXDCTL_SWFLSH);
Greg Rose92915f72010-01-09 02:24:10 +00002077 }
2078
2079 netif_carrier_off(netdev);
2080
2081 if (!pci_channel_offline(adapter->pdev))
2082 ixgbevf_reset(adapter);
2083
2084 ixgbevf_clean_all_tx_rings(adapter);
2085 ixgbevf_clean_all_rx_rings(adapter);
2086}
2087
2088void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2089{
2090 WARN_ON(in_interrupt());
Greg Rosec0456c22010-01-22 22:47:18 +00002091
Greg Rose92915f72010-01-09 02:24:10 +00002092 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2093 msleep(1);
2094
Alexander Duyck4b2cd272012-08-02 01:16:59 +00002095 ixgbevf_down(adapter);
2096 ixgbevf_up(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002097
2098 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2099}
2100
2101void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2102{
2103 struct ixgbe_hw *hw = &adapter->hw;
2104 struct net_device *netdev = adapter->netdev;
2105
Don Skidmore798e3812013-10-01 04:33:51 -07002106 if (hw->mac.ops.reset_hw(hw)) {
Greg Rose92915f72010-01-09 02:24:10 +00002107 hw_dbg(hw, "PF still resetting\n");
Don Skidmore798e3812013-10-01 04:33:51 -07002108 } else {
Greg Rose92915f72010-01-09 02:24:10 +00002109 hw->mac.ops.init_hw(hw);
Don Skidmore798e3812013-10-01 04:33:51 -07002110 ixgbevf_negotiate_api(adapter);
2111 }
Greg Rose92915f72010-01-09 02:24:10 +00002112
2113 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2114 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
2115 netdev->addr_len);
2116 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
2117 netdev->addr_len);
2118 }
2119}
2120
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002121static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2122 int vectors)
Greg Rose92915f72010-01-09 02:24:10 +00002123{
Emil Tantilova5f93372012-11-13 04:03:17 +00002124 int vector_threshold;
Greg Rose92915f72010-01-09 02:24:10 +00002125
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002126 /* We'll want at least 2 (vector_threshold):
2127 * 1) TxQ[0] + RxQ[0] handler
2128 * 2) Other (Link Status Change, etc.)
Greg Rose92915f72010-01-09 02:24:10 +00002129 */
2130 vector_threshold = MIN_MSIX_COUNT;
2131
2132 /* The more we get, the more we will assign to Tx/Rx Cleanup
2133 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2134 * Right now, we simply care about how many we'll get; we'll
2135 * set them up later while requesting irq's.
2136 */
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002137 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2138 vector_threshold, vectors);
Greg Rose92915f72010-01-09 02:24:10 +00002139
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002140 if (vectors < 0) {
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002141 dev_err(&adapter->pdev->dev,
2142 "Unable to allocate MSI-X interrupts\n");
Greg Rose92915f72010-01-09 02:24:10 +00002143 kfree(adapter->msix_entries);
2144 adapter->msix_entries = NULL;
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002145 return vectors;
Greg Rose92915f72010-01-09 02:24:10 +00002146 }
Greg Rosedee847f2012-11-02 05:50:57 +00002147
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002148 /* Adjust for only the vectors we'll use, which is minimum
2149 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2150 * vectors we were allocated.
2151 */
2152 adapter->num_msix_vectors = vectors;
2153
2154 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002155}
2156
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002157/**
2158 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
Greg Rose92915f72010-01-09 02:24:10 +00002159 * @adapter: board private structure to initialize
2160 *
2161 * This is the top level queue allocation routine. The order here is very
2162 * important, starting with the "most" number of features turned on at once,
2163 * and ending with the smallest set of features. This way large combinations
2164 * can be allocated if they're turned on, and smaller combinations are the
2165 * fallthrough conditions.
2166 *
2167 **/
2168static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2169{
Don Skidmore220fe052013-09-21 01:40:49 +00002170 struct ixgbe_hw *hw = &adapter->hw;
2171 unsigned int def_q = 0;
2172 unsigned int num_tcs = 0;
2173 int err;
2174
Greg Rose92915f72010-01-09 02:24:10 +00002175 /* Start with base case */
2176 adapter->num_rx_queues = 1;
2177 adapter->num_tx_queues = 1;
Don Skidmore220fe052013-09-21 01:40:49 +00002178
2179 spin_lock_bh(&adapter->mbx_lock);
2180
2181 /* fetch queue configuration from the PF */
2182 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2183
2184 spin_unlock_bh(&adapter->mbx_lock);
2185
2186 if (err)
2187 return;
2188
2189 /* we need as many queues as traffic classes */
2190 if (num_tcs > 1)
2191 adapter->num_rx_queues = num_tcs;
Greg Rose92915f72010-01-09 02:24:10 +00002192}
2193
2194/**
2195 * ixgbevf_alloc_queues - Allocate memory for all rings
2196 * @adapter: board private structure to initialize
2197 *
2198 * We allocate one ring per queue at run-time since we don't know the
2199 * number of queues at compile-time. The polling_netdev array is
2200 * intended for Multiqueue, but should work fine with a single queue.
2201 **/
2202static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
2203{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002204 struct ixgbevf_ring *ring;
2205 int rx = 0, tx = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002206
Don Skidmore87e70ab2014-01-16 02:30:08 -08002207 for (; tx < adapter->num_tx_queues; tx++) {
2208 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2209 if (!ring)
2210 goto err_allocation;
Greg Rose92915f72010-01-09 02:24:10 +00002211
Don Skidmore87e70ab2014-01-16 02:30:08 -08002212 ring->dev = &adapter->pdev->dev;
2213 ring->netdev = adapter->netdev;
2214 ring->count = adapter->tx_ring_count;
2215 ring->queue_index = tx;
2216 ring->reg_idx = tx;
Greg Rose92915f72010-01-09 02:24:10 +00002217
Don Skidmore87e70ab2014-01-16 02:30:08 -08002218 adapter->tx_ring[tx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002219 }
2220
Don Skidmore87e70ab2014-01-16 02:30:08 -08002221 for (; rx < adapter->num_rx_queues; rx++) {
2222 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2223 if (!ring)
2224 goto err_allocation;
2225
2226 ring->dev = &adapter->pdev->dev;
2227 ring->netdev = adapter->netdev;
2228
2229 ring->count = adapter->rx_ring_count;
2230 ring->queue_index = rx;
2231 ring->reg_idx = rx;
2232
2233 adapter->rx_ring[rx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002234 }
2235
2236 return 0;
2237
Don Skidmore87e70ab2014-01-16 02:30:08 -08002238err_allocation:
2239 while (tx) {
2240 kfree(adapter->tx_ring[--tx]);
2241 adapter->tx_ring[tx] = NULL;
2242 }
2243
2244 while (rx) {
2245 kfree(adapter->rx_ring[--rx]);
2246 adapter->rx_ring[rx] = NULL;
2247 }
Greg Rose92915f72010-01-09 02:24:10 +00002248 return -ENOMEM;
2249}
2250
2251/**
2252 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2253 * @adapter: board private structure to initialize
2254 *
2255 * Attempt to configure the interrupts using the best available
2256 * capabilities of the hardware and the kernel.
2257 **/
2258static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2259{
Greg Rose91e2b892012-10-03 00:57:23 +00002260 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002261 int err = 0;
2262 int vector, v_budget;
2263
2264 /*
2265 * It's easy to be greedy for MSI-X vectors, but it really
2266 * doesn't do us much good if we have a lot more vectors
2267 * than CPU's. So let's be conservative and only ask for
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002268 * (roughly) the same number of vectors as there are CPU's.
2269 * The default is to use pairs of vectors.
Greg Rose92915f72010-01-09 02:24:10 +00002270 */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002271 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2272 v_budget = min_t(int, v_budget, num_online_cpus());
2273 v_budget += NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002274
2275 /* A failure in MSI-X entry allocation isn't fatal, but it does
2276 * mean we disable MSI-X capabilities of the adapter. */
2277 adapter->msix_entries = kcalloc(v_budget,
2278 sizeof(struct msix_entry), GFP_KERNEL);
2279 if (!adapter->msix_entries) {
2280 err = -ENOMEM;
2281 goto out;
2282 }
2283
2284 for (vector = 0; vector < v_budget; vector++)
2285 adapter->msix_entries[vector].entry = vector;
2286
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002287 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
2288 if (err)
2289 goto out;
Greg Rose92915f72010-01-09 02:24:10 +00002290
Greg Rose91e2b892012-10-03 00:57:23 +00002291 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
2292 if (err)
2293 goto out;
2294
2295 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
2296
Greg Rose92915f72010-01-09 02:24:10 +00002297out:
2298 return err;
2299}
2300
2301/**
2302 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2303 * @adapter: board private structure to initialize
2304 *
2305 * We allocate one q_vector per queue interrupt. If allocation fails we
2306 * return -ENOMEM.
2307 **/
2308static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2309{
2310 int q_idx, num_q_vectors;
2311 struct ixgbevf_q_vector *q_vector;
Greg Rose92915f72010-01-09 02:24:10 +00002312
2313 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002314
2315 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2316 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2317 if (!q_vector)
2318 goto err_out;
2319 q_vector->adapter = adapter;
2320 q_vector->v_idx = q_idx;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002321 netif_napi_add(adapter->netdev, &q_vector->napi,
2322 ixgbevf_poll, 64);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002323#ifdef CONFIG_NET_RX_BUSY_POLL
2324 napi_hash_add(&q_vector->napi);
2325#endif
Greg Rose92915f72010-01-09 02:24:10 +00002326 adapter->q_vector[q_idx] = q_vector;
2327 }
2328
2329 return 0;
2330
2331err_out:
2332 while (q_idx) {
2333 q_idx--;
2334 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002335#ifdef CONFIG_NET_RX_BUSY_POLL
2336 napi_hash_del(&q_vector->napi);
2337#endif
Greg Rose92915f72010-01-09 02:24:10 +00002338 netif_napi_del(&q_vector->napi);
2339 kfree(q_vector);
2340 adapter->q_vector[q_idx] = NULL;
2341 }
2342 return -ENOMEM;
2343}
2344
2345/**
2346 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2347 * @adapter: board private structure to initialize
2348 *
2349 * This function frees the memory allocated to the q_vectors. In addition if
2350 * NAPI is enabled it will delete any references to the NAPI struct prior
2351 * to freeing the q_vector.
2352 **/
2353static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2354{
John Fastabendf4477702012-09-16 08:19:46 +00002355 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002356
2357 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2358 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2359
2360 adapter->q_vector[q_idx] = NULL;
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002361#ifdef CONFIG_NET_RX_BUSY_POLL
2362 napi_hash_del(&q_vector->napi);
2363#endif
John Fastabendf4477702012-09-16 08:19:46 +00002364 netif_napi_del(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00002365 kfree(q_vector);
2366 }
2367}
2368
2369/**
2370 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2371 * @adapter: board private structure
2372 *
2373 **/
2374static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2375{
2376 pci_disable_msix(adapter->pdev);
2377 kfree(adapter->msix_entries);
2378 adapter->msix_entries = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00002379}
2380
2381/**
2382 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2383 * @adapter: board private structure to initialize
2384 *
2385 **/
2386static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2387{
2388 int err;
2389
2390 /* Number of supported queues */
2391 ixgbevf_set_num_queues(adapter);
2392
2393 err = ixgbevf_set_interrupt_capability(adapter);
2394 if (err) {
2395 hw_dbg(&adapter->hw,
2396 "Unable to setup interrupt capabilities\n");
2397 goto err_set_interrupt;
2398 }
2399
2400 err = ixgbevf_alloc_q_vectors(adapter);
2401 if (err) {
2402 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2403 "vectors\n");
2404 goto err_alloc_q_vectors;
2405 }
2406
2407 err = ixgbevf_alloc_queues(adapter);
2408 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002409 pr_err("Unable to allocate memory for queues\n");
Greg Rose92915f72010-01-09 02:24:10 +00002410 goto err_alloc_queues;
2411 }
2412
2413 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2414 "Tx Queue count = %u\n",
2415 (adapter->num_rx_queues > 1) ? "Enabled" :
2416 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2417
2418 set_bit(__IXGBEVF_DOWN, &adapter->state);
2419
2420 return 0;
2421err_alloc_queues:
2422 ixgbevf_free_q_vectors(adapter);
2423err_alloc_q_vectors:
2424 ixgbevf_reset_interrupt_capability(adapter);
2425err_set_interrupt:
2426 return err;
2427}
2428
2429/**
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002430 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2431 * @adapter: board private structure to clear interrupt scheme on
2432 *
2433 * We go through and clear interrupt specific resources and reset the structure
2434 * to pre-load conditions
2435 **/
2436static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2437{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002438 int i;
2439
2440 for (i = 0; i < adapter->num_tx_queues; i++) {
2441 kfree(adapter->tx_ring[i]);
2442 adapter->tx_ring[i] = NULL;
2443 }
2444 for (i = 0; i < adapter->num_rx_queues; i++) {
2445 kfree(adapter->rx_ring[i]);
2446 adapter->rx_ring[i] = NULL;
2447 }
2448
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002449 adapter->num_tx_queues = 0;
2450 adapter->num_rx_queues = 0;
2451
2452 ixgbevf_free_q_vectors(adapter);
2453 ixgbevf_reset_interrupt_capability(adapter);
2454}
2455
2456/**
Greg Rose92915f72010-01-09 02:24:10 +00002457 * ixgbevf_sw_init - Initialize general software structures
2458 * (struct ixgbevf_adapter)
2459 * @adapter: board private structure to initialize
2460 *
2461 * ixgbevf_sw_init initializes the Adapter private data structure.
2462 * Fields are initialized based on PCI device information and
2463 * OS network device settings (MTU size).
2464 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002465static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00002466{
2467 struct ixgbe_hw *hw = &adapter->hw;
2468 struct pci_dev *pdev = adapter->pdev;
Greg Rosee1941a72013-02-13 03:02:05 +00002469 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002470 int err;
2471
2472 /* PCI config space info */
2473
2474 hw->vendor_id = pdev->vendor;
2475 hw->device_id = pdev->device;
Sergei Shtylyovff938e42011-02-28 11:57:33 -08002476 hw->revision_id = pdev->revision;
Greg Rose92915f72010-01-09 02:24:10 +00002477 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2478 hw->subsystem_device_id = pdev->subsystem_device;
2479
2480 hw->mbx.ops.init_params(hw);
Alexander Duyck56e94092012-07-20 08:10:03 +00002481
2482 /* assume legacy case in which PF would only give VF 2 queues */
2483 hw->mac.max_tx_queues = 2;
2484 hw->mac.max_rx_queues = 2;
2485
Don Skidmore798e3812013-10-01 04:33:51 -07002486 /* lock to protect mailbox accesses */
2487 spin_lock_init(&adapter->mbx_lock);
2488
Greg Rose92915f72010-01-09 02:24:10 +00002489 err = hw->mac.ops.reset_hw(hw);
2490 if (err) {
2491 dev_info(&pdev->dev,
Greg Rosee1941a72013-02-13 03:02:05 +00002492 "PF still in reset state. Is the PF interface up?\n");
Greg Rose92915f72010-01-09 02:24:10 +00002493 } else {
2494 err = hw->mac.ops.init_hw(hw);
2495 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002496 pr_err("init_shared_code failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00002497 goto out;
2498 }
Don Skidmore798e3812013-10-01 04:33:51 -07002499 ixgbevf_negotiate_api(adapter);
Greg Rosee1941a72013-02-13 03:02:05 +00002500 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2501 if (err)
2502 dev_info(&pdev->dev, "Error reading MAC address\n");
2503 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2504 dev_info(&pdev->dev,
2505 "MAC address not assigned by administrator.\n");
2506 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2507 }
2508
2509 if (!is_valid_ether_addr(netdev->dev_addr)) {
2510 dev_info(&pdev->dev, "Assigning random MAC address\n");
2511 eth_hw_addr_random(netdev);
2512 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00002513 }
2514
2515 /* Enable dynamic interrupt throttling rates */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002516 adapter->rx_itr_setting = 1;
2517 adapter->tx_itr_setting = 1;
Greg Rose92915f72010-01-09 02:24:10 +00002518
Greg Rose92915f72010-01-09 02:24:10 +00002519 /* set default ring sizes */
2520 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2521 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2522
Greg Rose92915f72010-01-09 02:24:10 +00002523 set_bit(__IXGBEVF_DOWN, &adapter->state);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00002524 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002525
2526out:
2527 return err;
2528}
2529
Greg Rose92915f72010-01-09 02:24:10 +00002530#define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2531 { \
2532 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2533 if (current_counter < last_counter) \
2534 counter += 0x100000000LL; \
2535 last_counter = current_counter; \
2536 counter &= 0xFFFFFFFF00000000LL; \
2537 counter |= current_counter; \
2538 }
2539
2540#define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2541 { \
2542 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2543 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2544 u64 current_counter = (current_counter_msb << 32) | \
2545 current_counter_lsb; \
2546 if (current_counter < last_counter) \
2547 counter += 0x1000000000LL; \
2548 last_counter = current_counter; \
2549 counter &= 0xFFFFFFF000000000LL; \
2550 counter |= current_counter; \
2551 }
2552/**
2553 * ixgbevf_update_stats - Update the board statistics counters.
2554 * @adapter: board private structure
2555 **/
2556void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2557{
2558 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose55fb2772012-11-06 05:53:32 +00002559 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002560
Greg Rose088245a2013-01-04 07:37:31 +00002561 if (!adapter->link_up)
2562 return;
2563
Greg Rose92915f72010-01-09 02:24:10 +00002564 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2565 adapter->stats.vfgprc);
2566 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2567 adapter->stats.vfgptc);
2568 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2569 adapter->stats.last_vfgorc,
2570 adapter->stats.vfgorc);
2571 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2572 adapter->stats.last_vfgotc,
2573 adapter->stats.vfgotc);
2574 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2575 adapter->stats.vfmprc);
Greg Rose55fb2772012-11-06 05:53:32 +00002576
2577 for (i = 0; i < adapter->num_rx_queues; i++) {
2578 adapter->hw_csum_rx_error +=
Don Skidmore87e70ab2014-01-16 02:30:08 -08002579 adapter->rx_ring[i]->hw_csum_rx_error;
Don Skidmore87e70ab2014-01-16 02:30:08 -08002580 adapter->rx_ring[i]->hw_csum_rx_error = 0;
Greg Rose55fb2772012-11-06 05:53:32 +00002581 }
Greg Rose92915f72010-01-09 02:24:10 +00002582}
2583
2584/**
2585 * ixgbevf_watchdog - Timer Call-back
2586 * @data: pointer to adapter cast into an unsigned long
2587 **/
2588static void ixgbevf_watchdog(unsigned long data)
2589{
2590 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2591 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002592 u32 eics = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002593 int i;
2594
2595 /*
2596 * Do the watchdog outside of interrupt context due to the lovely
2597 * delays that some of the newer hardware requires
2598 */
2599
2600 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2601 goto watchdog_short_circuit;
2602
2603 /* get one bit for every active tx/rx interrupt vector */
2604 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2605 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00002606 if (qv->rx.ring || qv->tx.ring)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002607 eics |= 1 << i;
Greg Rose92915f72010-01-09 02:24:10 +00002608 }
2609
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002610 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
Greg Rose92915f72010-01-09 02:24:10 +00002611
2612watchdog_short_circuit:
2613 schedule_work(&adapter->watchdog_task);
2614}
2615
2616/**
2617 * ixgbevf_tx_timeout - Respond to a Tx Hang
2618 * @netdev: network interface device structure
2619 **/
2620static void ixgbevf_tx_timeout(struct net_device *netdev)
2621{
2622 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2623
2624 /* Do the reset outside of interrupt context */
2625 schedule_work(&adapter->reset_task);
2626}
2627
2628static void ixgbevf_reset_task(struct work_struct *work)
2629{
2630 struct ixgbevf_adapter *adapter;
2631 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2632
2633 /* If we're already down or resetting, just bail */
2634 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002635 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
Greg Rose92915f72010-01-09 02:24:10 +00002636 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2637 return;
2638
2639 adapter->tx_timeout_count++;
2640
2641 ixgbevf_reinit_locked(adapter);
2642}
2643
2644/**
2645 * ixgbevf_watchdog_task - worker thread to bring link up
2646 * @work: pointer to work_struct containing our data
2647 **/
2648static void ixgbevf_watchdog_task(struct work_struct *work)
2649{
2650 struct ixgbevf_adapter *adapter = container_of(work,
2651 struct ixgbevf_adapter,
2652 watchdog_task);
2653 struct net_device *netdev = adapter->netdev;
2654 struct ixgbe_hw *hw = &adapter->hw;
2655 u32 link_speed = adapter->link_speed;
2656 bool link_up = adapter->link_up;
Greg Rose92fe0bf2012-11-02 05:50:47 +00002657 s32 need_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002658
Mark Rustad26597802014-03-04 03:02:45 +00002659 if (IXGBE_REMOVED(hw->hw_addr)) {
2660 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
2661 rtnl_lock();
2662 ixgbevf_down(adapter);
2663 rtnl_unlock();
2664 }
2665 return;
2666 }
Don Skidmore220fe052013-09-21 01:40:49 +00002667 ixgbevf_queue_reset_subtask(adapter);
2668
Greg Rose92915f72010-01-09 02:24:10 +00002669 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2670
2671 /*
2672 * Always check the link on the watchdog because we have
2673 * no LSC interrupt
2674 */
Greg Rose92fe0bf2012-11-02 05:50:47 +00002675 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002676
Greg Rose92fe0bf2012-11-02 05:50:47 +00002677 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002678
Greg Rose92fe0bf2012-11-02 05:50:47 +00002679 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002680
Greg Rose92fe0bf2012-11-02 05:50:47 +00002681 if (need_reset) {
2682 adapter->link_up = link_up;
2683 adapter->link_speed = link_speed;
2684 netif_carrier_off(netdev);
2685 netif_tx_stop_all_queues(netdev);
2686 schedule_work(&adapter->reset_task);
2687 goto pf_has_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002688 }
2689 adapter->link_up = link_up;
2690 adapter->link_speed = link_speed;
2691
2692 if (link_up) {
2693 if (!netif_carrier_ok(netdev)) {
Greg Roseb876a742013-01-19 06:40:22 +00002694 char *link_speed_string;
2695 switch (link_speed) {
2696 case IXGBE_LINK_SPEED_10GB_FULL:
2697 link_speed_string = "10 Gbps";
2698 break;
2699 case IXGBE_LINK_SPEED_1GB_FULL:
2700 link_speed_string = "1 Gbps";
2701 break;
2702 case IXGBE_LINK_SPEED_100_FULL:
2703 link_speed_string = "100 Mbps";
2704 break;
2705 default:
2706 link_speed_string = "unknown speed";
2707 break;
2708 }
Greg Rose6fe59672013-01-04 07:37:26 +00002709 dev_info(&adapter->pdev->dev,
Greg Roseb876a742013-01-19 06:40:22 +00002710 "NIC Link is Up, %s\n", link_speed_string);
Greg Rose92915f72010-01-09 02:24:10 +00002711 netif_carrier_on(netdev);
2712 netif_tx_wake_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002713 }
2714 } else {
2715 adapter->link_up = false;
2716 adapter->link_speed = 0;
2717 if (netif_carrier_ok(netdev)) {
Greg Rose6fe59672013-01-04 07:37:26 +00002718 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
Greg Rose92915f72010-01-09 02:24:10 +00002719 netif_carrier_off(netdev);
2720 netif_tx_stop_all_queues(netdev);
2721 }
2722 }
2723
Greg Rose92915f72010-01-09 02:24:10 +00002724 ixgbevf_update_stats(adapter);
2725
Greg Rose33bd9f62010-03-19 02:59:52 +00002726pf_has_reset:
Greg Rose92915f72010-01-09 02:24:10 +00002727 /* Reset the timer */
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002728 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
2729 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Greg Rose92915f72010-01-09 02:24:10 +00002730 mod_timer(&adapter->watchdog_timer,
2731 round_jiffies(jiffies + (2 * HZ)));
2732
2733 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2734}
2735
2736/**
2737 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
Greg Rose92915f72010-01-09 02:24:10 +00002738 * @tx_ring: Tx descriptor ring for a specific queue
2739 *
2740 * Free all transmit software resources
2741 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002742void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002743{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002744 ixgbevf_clean_tx_ring(tx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002745
2746 vfree(tx_ring->tx_buffer_info);
2747 tx_ring->tx_buffer_info = NULL;
2748
Don Skidmorede02dec2014-01-16 02:30:09 -08002749 /* if not set, then don't free */
2750 if (!tx_ring->desc)
2751 return;
2752
Emil Tantilov05d063a2014-01-17 18:29:59 -08002753 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002754 tx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002755
2756 tx_ring->desc = NULL;
2757}
2758
2759/**
2760 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2761 * @adapter: board private structure
2762 *
2763 * Free all transmit software resources
2764 **/
2765static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2766{
2767 int i;
2768
2769 for (i = 0; i < adapter->num_tx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002770 if (adapter->tx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002771 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002772}
2773
2774/**
2775 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002776 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2777 *
2778 * Return 0 on success, negative on failure
2779 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002780int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002781{
Greg Rose92915f72010-01-09 02:24:10 +00002782 int size;
2783
2784 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002785 tx_ring->tx_buffer_info = vzalloc(size);
Greg Rose92915f72010-01-09 02:24:10 +00002786 if (!tx_ring->tx_buffer_info)
2787 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002788
2789 /* round up to nearest 4K */
2790 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2791 tx_ring->size = ALIGN(tx_ring->size, 4096);
2792
Emil Tantilov05d063a2014-01-17 18:29:59 -08002793 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002794 &tx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002795 if (!tx_ring->desc)
2796 goto err;
2797
Greg Rose92915f72010-01-09 02:24:10 +00002798 return 0;
2799
2800err:
2801 vfree(tx_ring->tx_buffer_info);
2802 tx_ring->tx_buffer_info = NULL;
2803 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2804 "descriptor ring\n");
2805 return -ENOMEM;
2806}
2807
2808/**
2809 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2810 * @adapter: board private structure
2811 *
2812 * If this function returns with an error, then it's possible one or
2813 * more of the rings is populated (while the rest are not). It is the
2814 * callers duty to clean those orphaned rings.
2815 *
2816 * Return 0 on success, negative on failure
2817 **/
2818static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2819{
2820 int i, err = 0;
2821
2822 for (i = 0; i < adapter->num_tx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002823 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002824 if (!err)
2825 continue;
2826 hw_dbg(&adapter->hw,
2827 "Allocation for Tx Queue %u failed\n", i);
2828 break;
2829 }
2830
2831 return err;
2832}
2833
2834/**
2835 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002836 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2837 *
2838 * Returns 0 on success, negative on failure
2839 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002840int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002841{
Greg Rose92915f72010-01-09 02:24:10 +00002842 int size;
2843
2844 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002845 rx_ring->rx_buffer_info = vzalloc(size);
Joe Perchese404dec2012-01-29 12:56:23 +00002846 if (!rx_ring->rx_buffer_info)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002847 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002848
2849 /* Round up to nearest 4K */
2850 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2851 rx_ring->size = ALIGN(rx_ring->size, 4096);
2852
Emil Tantilov05d063a2014-01-17 18:29:59 -08002853 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002854 &rx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002855
Emil Tantilov05d063a2014-01-17 18:29:59 -08002856 if (!rx_ring->desc)
2857 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002858
Greg Rose92915f72010-01-09 02:24:10 +00002859 return 0;
Emil Tantilov05d063a2014-01-17 18:29:59 -08002860err:
2861 vfree(rx_ring->rx_buffer_info);
2862 rx_ring->rx_buffer_info = NULL;
2863 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
Greg Rose92915f72010-01-09 02:24:10 +00002864 return -ENOMEM;
2865}
2866
2867/**
2868 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2869 * @adapter: board private structure
2870 *
2871 * If this function returns with an error, then it's possible one or
2872 * more of the rings is populated (while the rest are not). It is the
2873 * callers duty to clean those orphaned rings.
2874 *
2875 * Return 0 on success, negative on failure
2876 **/
2877static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2878{
2879 int i, err = 0;
2880
2881 for (i = 0; i < adapter->num_rx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002882 err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002883 if (!err)
2884 continue;
2885 hw_dbg(&adapter->hw,
2886 "Allocation for Rx Queue %u failed\n", i);
2887 break;
2888 }
2889 return err;
2890}
2891
2892/**
2893 * ixgbevf_free_rx_resources - Free Rx Resources
Greg Rose92915f72010-01-09 02:24:10 +00002894 * @rx_ring: ring to clean the resources from
2895 *
2896 * Free all receive software resources
2897 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002898void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002899{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002900 ixgbevf_clean_rx_ring(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002901
2902 vfree(rx_ring->rx_buffer_info);
2903 rx_ring->rx_buffer_info = NULL;
2904
Emil Tantilov05d063a2014-01-17 18:29:59 -08002905 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002906 rx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002907
2908 rx_ring->desc = NULL;
2909}
2910
2911/**
2912 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2913 * @adapter: board private structure
2914 *
2915 * Free all receive software resources
2916 **/
2917static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2918{
2919 int i;
2920
2921 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002922 if (adapter->rx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002923 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002924}
2925
2926/**
2927 * ixgbevf_open - Called when a network interface is made active
2928 * @netdev: network interface device structure
2929 *
2930 * Returns 0 on success, negative value on failure
2931 *
2932 * The open entry point is called when a network interface is made
2933 * active by the system (IFF_UP). At this point all resources needed
2934 * for transmit and receive operations are allocated, the interrupt
2935 * handler is registered with the OS, the watchdog timer is started,
2936 * and the stack is notified that the interface is ready.
2937 **/
2938static int ixgbevf_open(struct net_device *netdev)
2939{
2940 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2941 struct ixgbe_hw *hw = &adapter->hw;
2942 int err;
2943
xunleera1f6c6b2013-03-05 07:44:20 +00002944 /* A previous failure to open the device because of a lack of
2945 * available MSIX vector resources may have reset the number
2946 * of msix vectors variable to zero. The only way to recover
2947 * is to unload/reload the driver and hope that the system has
2948 * been able to recover some MSIX vector resources.
2949 */
2950 if (!adapter->num_msix_vectors)
2951 return -ENOMEM;
2952
Greg Rose92915f72010-01-09 02:24:10 +00002953 /* disallow open during test */
2954 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2955 return -EBUSY;
2956
2957 if (hw->adapter_stopped) {
2958 ixgbevf_reset(adapter);
2959 /* if adapter is still stopped then PF isn't up and
2960 * the vf can't start. */
2961 if (hw->adapter_stopped) {
2962 err = IXGBE_ERR_MBX;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002963 pr_err("Unable to start - perhaps the PF Driver isn't "
2964 "up yet\n");
Greg Rose92915f72010-01-09 02:24:10 +00002965 goto err_setup_reset;
2966 }
2967 }
2968
2969 /* allocate transmit descriptors */
2970 err = ixgbevf_setup_all_tx_resources(adapter);
2971 if (err)
2972 goto err_setup_tx;
2973
2974 /* allocate receive descriptors */
2975 err = ixgbevf_setup_all_rx_resources(adapter);
2976 if (err)
2977 goto err_setup_rx;
2978
2979 ixgbevf_configure(adapter);
2980
2981 /*
2982 * Map the Tx/Rx rings to the vectors we were allotted.
2983 * if request_irq will be called in this function map_rings
2984 * must be called *before* up_complete
2985 */
2986 ixgbevf_map_rings_to_vectors(adapter);
2987
Greg Rose795180d2012-04-17 04:29:34 +00002988 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002989
2990 /* clear any pending interrupts, may auto mask */
2991 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2992 err = ixgbevf_request_irq(adapter);
2993 if (err)
2994 goto err_req_irq;
2995
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002996 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002997
2998 return 0;
2999
3000err_req_irq:
3001 ixgbevf_down(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003002err_setup_rx:
3003 ixgbevf_free_all_rx_resources(adapter);
3004err_setup_tx:
3005 ixgbevf_free_all_tx_resources(adapter);
3006 ixgbevf_reset(adapter);
3007
3008err_setup_reset:
3009
3010 return err;
3011}
3012
3013/**
3014 * ixgbevf_close - Disables a network interface
3015 * @netdev: network interface device structure
3016 *
3017 * Returns 0, this is not allowed to fail
3018 *
3019 * The close entry point is called when an interface is de-activated
3020 * by the OS. The hardware is still under the drivers control, but
3021 * needs to be disabled. A global MAC reset is issued to stop the
3022 * hardware, and all transmit and receive resources are freed.
3023 **/
3024static int ixgbevf_close(struct net_device *netdev)
3025{
3026 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3027
3028 ixgbevf_down(adapter);
3029 ixgbevf_free_irq(adapter);
3030
3031 ixgbevf_free_all_tx_resources(adapter);
3032 ixgbevf_free_all_rx_resources(adapter);
3033
3034 return 0;
3035}
3036
Don Skidmore220fe052013-09-21 01:40:49 +00003037static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3038{
3039 struct net_device *dev = adapter->netdev;
3040
3041 if (!(adapter->flags & IXGBEVF_FLAG_QUEUE_RESET_REQUESTED))
3042 return;
3043
3044 adapter->flags &= ~IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
3045
3046 /* if interface is down do nothing */
3047 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3048 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3049 return;
3050
3051 /* Hardware has to reinitialize queues and interrupts to
3052 * match packet buffer alignment. Unfortunately, the
3053 * hardware is not flexible enough to do this dynamically.
3054 */
3055 if (netif_running(dev))
3056 ixgbevf_close(dev);
3057
3058 ixgbevf_clear_interrupt_scheme(adapter);
3059 ixgbevf_init_interrupt_scheme(adapter);
3060
3061 if (netif_running(dev))
3062 ixgbevf_open(dev);
3063}
3064
Alexander Duyck70a10e22012-05-11 08:33:21 +00003065static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3066 u32 vlan_macip_lens, u32 type_tucmd,
3067 u32 mss_l4len_idx)
3068{
3069 struct ixgbe_adv_tx_context_desc *context_desc;
3070 u16 i = tx_ring->next_to_use;
3071
3072 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3073
3074 i++;
3075 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3076
3077 /* set bits to identify this as an advanced context descriptor */
3078 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3079
3080 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3081 context_desc->seqnum_seed = 0;
3082 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3083 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3084}
3085
3086static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003087 struct ixgbevf_tx_buffer *first,
3088 u8 *hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003089{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003090 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003091 u32 vlan_macip_lens, type_tucmd;
Greg Rose92915f72010-01-09 02:24:10 +00003092 u32 mss_l4len_idx, l4len;
Francois Romieu8f12c032014-03-30 03:14:32 +00003093 int err;
Greg Rose92915f72010-01-09 02:24:10 +00003094
Emil Tantilov01a545c2014-02-27 20:32:45 -08003095 if (skb->ip_summed != CHECKSUM_PARTIAL)
3096 return 0;
3097
Alexander Duyck70a10e22012-05-11 08:33:21 +00003098 if (!skb_is_gso(skb))
3099 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00003100
Francois Romieu8f12c032014-03-30 03:14:32 +00003101 err = skb_cow_head(skb, 0);
3102 if (err < 0)
3103 return err;
Greg Rose92915f72010-01-09 02:24:10 +00003104
Alexander Duyck70a10e22012-05-11 08:33:21 +00003105 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3106 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3107
3108 if (skb->protocol == htons(ETH_P_IP)) {
3109 struct iphdr *iph = ip_hdr(skb);
3110 iph->tot_len = 0;
3111 iph->check = 0;
3112 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3113 iph->daddr, 0,
3114 IPPROTO_TCP,
3115 0);
3116 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003117 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3118 IXGBE_TX_FLAGS_CSUM |
3119 IXGBE_TX_FLAGS_IPV4;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003120 } else if (skb_is_gso_v6(skb)) {
3121 ipv6_hdr(skb)->payload_len = 0;
3122 tcp_hdr(skb)->check =
3123 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3124 &ipv6_hdr(skb)->daddr,
3125 0, IPPROTO_TCP, 0);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003126 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3127 IXGBE_TX_FLAGS_CSUM;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003128 }
3129
3130 /* compute header lengths */
3131 l4len = tcp_hdrlen(skb);
3132 *hdr_len += l4len;
3133 *hdr_len = skb_transport_offset(skb) + l4len;
3134
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003135 /* update gso size and bytecount with header size */
3136 first->gso_segs = skb_shinfo(skb)->gso_segs;
3137 first->bytecount += (first->gso_segs - 1) * *hdr_len;
3138
Alexander Duyck70a10e22012-05-11 08:33:21 +00003139 /* mss_l4len_id: use 1 as index for TSO */
3140 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
3141 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3142 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
3143
3144 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3145 vlan_macip_lens = skb_network_header_len(skb);
3146 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003147 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003148
3149 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3150 type_tucmd, mss_l4len_idx);
3151
3152 return 1;
Greg Rose92915f72010-01-09 02:24:10 +00003153}
3154
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003155static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3156 struct ixgbevf_tx_buffer *first)
Greg Rose92915f72010-01-09 02:24:10 +00003157{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003158 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003159 u32 vlan_macip_lens = 0;
3160 u32 mss_l4len_idx = 0;
3161 u32 type_tucmd = 0;
Greg Rose92915f72010-01-09 02:24:10 +00003162
Alexander Duyck70a10e22012-05-11 08:33:21 +00003163 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3164 u8 l4_hdr = 0;
3165 switch (skb->protocol) {
Joe Perches0933ce42014-03-13 05:19:30 +00003166 case htons(ETH_P_IP):
Alexander Duyck70a10e22012-05-11 08:33:21 +00003167 vlan_macip_lens |= skb_network_header_len(skb);
3168 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3169 l4_hdr = ip_hdr(skb)->protocol;
3170 break;
Joe Perches0933ce42014-03-13 05:19:30 +00003171 case htons(ETH_P_IPV6):
Alexander Duyck70a10e22012-05-11 08:33:21 +00003172 vlan_macip_lens |= skb_network_header_len(skb);
3173 l4_hdr = ipv6_hdr(skb)->nexthdr;
3174 break;
3175 default:
3176 if (unlikely(net_ratelimit())) {
3177 dev_warn(tx_ring->dev,
3178 "partial checksum but proto=%x!\n",
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003179 first->protocol);
Greg Rose92915f72010-01-09 02:24:10 +00003180 }
Alexander Duyck70a10e22012-05-11 08:33:21 +00003181 break;
Greg Rose92915f72010-01-09 02:24:10 +00003182 }
3183
Alexander Duyck70a10e22012-05-11 08:33:21 +00003184 switch (l4_hdr) {
3185 case IPPROTO_TCP:
3186 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
3187 mss_l4len_idx = tcp_hdrlen(skb) <<
3188 IXGBE_ADVTXD_L4LEN_SHIFT;
3189 break;
3190 case IPPROTO_SCTP:
3191 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3192 mss_l4len_idx = sizeof(struct sctphdr) <<
3193 IXGBE_ADVTXD_L4LEN_SHIFT;
3194 break;
3195 case IPPROTO_UDP:
3196 mss_l4len_idx = sizeof(struct udphdr) <<
3197 IXGBE_ADVTXD_L4LEN_SHIFT;
3198 break;
3199 default:
3200 if (unlikely(net_ratelimit())) {
3201 dev_warn(tx_ring->dev,
3202 "partial checksum but l4 proto=%x!\n",
3203 l4_hdr);
3204 }
3205 break;
3206 }
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003207
3208 /* update TX checksum flag */
3209 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003210 }
3211
Alexander Duyck70a10e22012-05-11 08:33:21 +00003212 /* vlan_macip_lens: MACLEN, VLAN tag */
3213 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003214 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003215
3216 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3217 type_tucmd, mss_l4len_idx);
Greg Rose92915f72010-01-09 02:24:10 +00003218}
3219
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003220static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3221{
3222 /* set type for advanced descriptor with frame checksum insertion */
3223 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3224 IXGBE_ADVTXD_DCMD_IFCS |
3225 IXGBE_ADVTXD_DCMD_DEXT);
3226
3227 /* set HW vlan bit if vlan is present */
3228 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3229 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3230
3231 /* set segmentation enable bits for TSO/FSO */
3232 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3233 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3234
3235 return cmd_type;
3236}
3237
3238static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3239 u32 tx_flags, unsigned int paylen)
3240{
3241 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3242
3243 /* enable L4 checksum for TSO and TX checksum offload */
3244 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3245 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3246
3247 /* enble IPv4 checksum for TSO */
3248 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3249 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3250
3251 /* use index 1 context for TSO/FSO/FCOE */
3252 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3253 olinfo_status |= cpu_to_le32(1 << IXGBE_ADVTXD_IDX_SHIFT);
3254
3255 /* Check Context must be set if Tx switch is enabled, which it
3256 * always is for case where virtual functions are running
3257 */
3258 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3259
3260 tx_desc->read.olinfo_status = olinfo_status;
3261}
3262
3263static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3264 struct ixgbevf_tx_buffer *first,
3265 const u8 hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003266{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003267 dma_addr_t dma;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003268 struct sk_buff *skb = first->skb;
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003269 struct ixgbevf_tx_buffer *tx_buffer;
3270 union ixgbe_adv_tx_desc *tx_desc;
3271 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
3272 unsigned int data_len = skb->data_len;
3273 unsigned int size = skb_headlen(skb);
3274 unsigned int paylen = skb->len - hdr_len;
3275 u32 tx_flags = first->tx_flags;
3276 __le32 cmd_type;
3277 u16 i = tx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +00003278
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003279 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +00003280
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003281 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, paylen);
3282 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
Greg Rose92915f72010-01-09 02:24:10 +00003283
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003284 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3285 if (dma_mapping_error(tx_ring->dev, dma))
3286 goto dma_error;
3287
3288 /* record length, and DMA address */
3289 dma_unmap_len_set(first, len, size);
3290 dma_unmap_addr_set(first, dma, dma);
3291
3292 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3293
3294 for (;;) {
3295 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3296 tx_desc->read.cmd_type_len =
3297 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3298
3299 i++;
3300 tx_desc++;
3301 if (i == tx_ring->count) {
3302 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3303 i = 0;
3304 }
3305
3306 dma += IXGBE_MAX_DATA_PER_TXD;
3307 size -= IXGBE_MAX_DATA_PER_TXD;
3308
3309 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3310 tx_desc->read.olinfo_status = 0;
3311 }
3312
3313 if (likely(!data_len))
3314 break;
3315
3316 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3317
3318 i++;
3319 tx_desc++;
3320 if (i == tx_ring->count) {
3321 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3322 i = 0;
3323 }
3324
3325 size = skb_frag_size(frag);
3326 data_len -= size;
3327
3328 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3329 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003330 if (dma_mapping_error(tx_ring->dev, dma))
Greg Rose92915f72010-01-09 02:24:10 +00003331 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00003332
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003333 tx_buffer = &tx_ring->tx_buffer_info[i];
3334 dma_unmap_len_set(tx_buffer, len, size);
3335 dma_unmap_addr_set(tx_buffer, dma, dma);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003336
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003337 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3338 tx_desc->read.olinfo_status = 0;
3339
3340 frag++;
Greg Rose92915f72010-01-09 02:24:10 +00003341 }
3342
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003343 /* write last descriptor with RS and EOP bits */
3344 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3345 tx_desc->read.cmd_type_len = cmd_type;
Greg Rose92915f72010-01-09 02:24:10 +00003346
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003347 /* set the timestamp */
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003348 first->time_stamp = jiffies;
Greg Rose92915f72010-01-09 02:24:10 +00003349
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003350 /* Force memory writes to complete before letting h/w know there
3351 * are new descriptors to fetch. (Only applicable for weak-ordered
3352 * memory model archs, such as IA-64).
3353 *
3354 * We also need this memory barrier (wmb) to make certain all of the
3355 * status bits have been updated before next_to_watch is written.
3356 */
3357 wmb();
Greg Rose92915f72010-01-09 02:24:10 +00003358
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003359 /* set next_to_watch value indicating a packet is present */
3360 first->next_to_watch = tx_desc;
3361
3362 i++;
3363 if (i == tx_ring->count)
3364 i = 0;
3365
3366 tx_ring->next_to_use = i;
3367
3368 /* notify HW of packet */
Mark Rustad06380db2014-03-04 03:02:23 +00003369 ixgbevf_write_tail(tx_ring, i);
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003370
3371 return;
Greg Rose92915f72010-01-09 02:24:10 +00003372dma_error:
Alexander Duyck70a10e22012-05-11 08:33:21 +00003373 dev_err(tx_ring->dev, "TX DMA map failed\n");
Greg Rose92915f72010-01-09 02:24:10 +00003374
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003375 /* clear dma mappings for failed tx_buffer_info map */
3376 for (;;) {
3377 tx_buffer = &tx_ring->tx_buffer_info[i];
3378 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer);
3379 if (tx_buffer == first)
3380 break;
3381 if (i == 0)
3382 i = tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +00003383 i--;
Greg Rose92915f72010-01-09 02:24:10 +00003384 }
3385
Greg Rose92915f72010-01-09 02:24:10 +00003386 tx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +00003387}
3388
Alexander Duyckfb401952012-05-11 08:33:16 +00003389static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003390{
Alexander Duyckfb401952012-05-11 08:33:16 +00003391 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00003392 /* Herbert's original patch had:
3393 * smp_mb__after_netif_stop_queue();
3394 * but since that doesn't exist yet, just open code it. */
3395 smp_mb();
3396
3397 /* We need to check again in a case another CPU has just
3398 * made room available. */
Don Skidmoref880d072013-10-23 02:17:52 +00003399 if (likely(ixgbevf_desc_unused(tx_ring) < size))
Greg Rose92915f72010-01-09 02:24:10 +00003400 return -EBUSY;
3401
3402 /* A reprieve! - use start_queue because it doesn't call schedule */
Alexander Duyckfb401952012-05-11 08:33:16 +00003403 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
Emil Tantilov095e2612014-01-17 18:30:00 -08003404 ++tx_ring->tx_stats.restart_queue;
3405
Greg Rose92915f72010-01-09 02:24:10 +00003406 return 0;
3407}
3408
Alexander Duyckfb401952012-05-11 08:33:16 +00003409static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003410{
Don Skidmoref880d072013-10-23 02:17:52 +00003411 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
Greg Rose92915f72010-01-09 02:24:10 +00003412 return 0;
Alexander Duyckfb401952012-05-11 08:33:16 +00003413 return __ixgbevf_maybe_stop_tx(tx_ring, size);
Greg Rose92915f72010-01-09 02:24:10 +00003414}
3415
3416static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3417{
3418 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003419 struct ixgbevf_tx_buffer *first;
Greg Rose92915f72010-01-09 02:24:10 +00003420 struct ixgbevf_ring *tx_ring;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003421 int tso;
3422 u32 tx_flags = 0;
Alexander Duyck35959902012-05-11 08:32:40 +00003423 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3424#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3425 unsigned short f;
3426#endif
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003427 u8 hdr_len = 0;
Greg Rosef9d08f162012-10-02 00:50:52 +00003428 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003429
Ben Hutchings46acc462012-11-01 09:11:11 +00003430 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
Greg Rosef9d08f162012-10-02 00:50:52 +00003431 dev_kfree_skb(skb);
3432 return NETDEV_TX_OK;
3433 }
Greg Rose92915f72010-01-09 02:24:10 +00003434
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003435 tx_ring = adapter->tx_ring[skb->queue_mapping];
Greg Rose92915f72010-01-09 02:24:10 +00003436
Alexander Duyck35959902012-05-11 08:32:40 +00003437 /*
3438 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3439 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3440 * + 2 desc gap to keep tail from touching head,
3441 * + 1 desc for context descriptor,
3442 * otherwise try next time
3443 */
3444#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3445 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3446 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3447#else
3448 count += skb_shinfo(skb)->nr_frags;
3449#endif
Alexander Duyckfb401952012-05-11 08:33:16 +00003450 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
Emil Tantilov095e2612014-01-17 18:30:00 -08003451 tx_ring->tx_stats.tx_busy++;
Alexander Duyck35959902012-05-11 08:32:40 +00003452 return NETDEV_TX_BUSY;
3453 }
3454
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003455 /* record the location of the first descriptor for this packet */
3456 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
3457 first->skb = skb;
3458 first->bytecount = skb->len;
3459 first->gso_segs = 1;
3460
Jesse Grosseab6d182010-10-20 13:56:03 +00003461 if (vlan_tx_tag_present(skb)) {
Greg Rose92915f72010-01-09 02:24:10 +00003462 tx_flags |= vlan_tx_tag_get(skb);
3463 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3464 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3465 }
3466
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003467 /* record initial flags and protocol */
3468 first->tx_flags = tx_flags;
3469 first->protocol = vlan_get_protocol(skb);
Greg Rose92915f72010-01-09 02:24:10 +00003470
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003471 tso = ixgbevf_tso(tx_ring, first, &hdr_len);
3472 if (tso < 0)
3473 goto out_drop;
Emil Tantilovb5d217f2014-02-27 20:32:44 -08003474 else if (!tso)
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003475 ixgbevf_tx_csum(tx_ring, first);
Greg Rose92915f72010-01-09 02:24:10 +00003476
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003477 ixgbevf_tx_map(tx_ring, first, hdr_len);
Greg Rose92915f72010-01-09 02:24:10 +00003478
Alexander Duyckfb401952012-05-11 08:33:16 +00003479 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Greg Rose92915f72010-01-09 02:24:10 +00003480
3481 return NETDEV_TX_OK;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003482
3483out_drop:
3484 dev_kfree_skb_any(first->skb);
3485 first->skb = NULL;
3486
3487 return NETDEV_TX_OK;
Greg Rose92915f72010-01-09 02:24:10 +00003488}
3489
3490/**
Greg Rose92915f72010-01-09 02:24:10 +00003491 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3492 * @netdev: network interface device structure
3493 * @p: pointer to an address structure
3494 *
3495 * Returns 0 on success, negative on failure
3496 **/
3497static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3498{
3499 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3500 struct ixgbe_hw *hw = &adapter->hw;
3501 struct sockaddr *addr = p;
3502
3503 if (!is_valid_ether_addr(addr->sa_data))
3504 return -EADDRNOTAVAIL;
3505
3506 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3507 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3508
John Fastabend55fdd45b2012-10-01 14:52:20 +00003509 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003510
Greg Rose92fe0bf2012-11-02 05:50:47 +00003511 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00003512
John Fastabend55fdd45b2012-10-01 14:52:20 +00003513 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003514
Greg Rose92915f72010-01-09 02:24:10 +00003515 return 0;
3516}
3517
3518/**
3519 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3520 * @netdev: network interface device structure
3521 * @new_mtu: new value for maximum frame size
3522 *
3523 * Returns 0 on success, negative on failure
3524 **/
3525static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3526{
3527 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilovbad17232014-11-21 02:57:15 +00003528 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00003529 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Greg Rose69bfbec2011-01-26 01:06:12 +00003530 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose69bfbec2011-01-26 01:06:12 +00003531
Alexander Duyck56e94092012-07-20 08:10:03 +00003532 switch (adapter->hw.api_version) {
3533 case ixgbe_mbox_api_11:
Greg Rose69bfbec2011-01-26 01:06:12 +00003534 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
Alexander Duyck56e94092012-07-20 08:10:03 +00003535 break;
3536 default:
3537 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3538 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3539 break;
3540 }
Greg Rose92915f72010-01-09 02:24:10 +00003541
3542 /* MTU < 68 is an error and causes problems on some kernels */
Greg Rose69bfbec2011-01-26 01:06:12 +00003543 if ((new_mtu < 68) || (max_frame > max_possible_frame))
Greg Rose92915f72010-01-09 02:24:10 +00003544 return -EINVAL;
3545
Emil Tantilovbad17232014-11-21 02:57:15 +00003546 hw_dbg(hw, "changing MTU from %d to %d\n",
Greg Rose92915f72010-01-09 02:24:10 +00003547 netdev->mtu, new_mtu);
3548 /* must set new MTU before calling down or up */
3549 netdev->mtu = new_mtu;
3550
Emil Tantilovbad17232014-11-21 02:57:15 +00003551 /* notify the PF of our intent to use this size of frame */
3552 ixgbevf_rlpml_set_vf(hw, max_frame);
Greg Rose92915f72010-01-09 02:24:10 +00003553
3554 return 0;
3555}
3556
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003557static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
Greg Rose92915f72010-01-09 02:24:10 +00003558{
3559 struct net_device *netdev = pci_get_drvdata(pdev);
3560 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003561#ifdef CONFIG_PM
3562 int retval = 0;
3563#endif
Greg Rose92915f72010-01-09 02:24:10 +00003564
3565 netif_device_detach(netdev);
3566
3567 if (netif_running(netdev)) {
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003568 rtnl_lock();
Greg Rose92915f72010-01-09 02:24:10 +00003569 ixgbevf_down(adapter);
3570 ixgbevf_free_irq(adapter);
3571 ixgbevf_free_all_tx_resources(adapter);
3572 ixgbevf_free_all_rx_resources(adapter);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003573 rtnl_unlock();
Greg Rose92915f72010-01-09 02:24:10 +00003574 }
3575
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003576 ixgbevf_clear_interrupt_scheme(adapter);
3577
3578#ifdef CONFIG_PM
3579 retval = pci_save_state(pdev);
3580 if (retval)
3581 return retval;
3582
3583#endif
Mark Rustadbc0c7152014-03-12 00:38:45 +00003584 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3585 pci_disable_device(pdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003586
3587 return 0;
3588}
3589
3590#ifdef CONFIG_PM
3591static int ixgbevf_resume(struct pci_dev *pdev)
3592{
Wei Yongjun27ae2962014-01-16 02:30:07 -08003593 struct net_device *netdev = pci_get_drvdata(pdev);
3594 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003595 u32 err;
3596
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003597 pci_restore_state(pdev);
3598 /*
3599 * pci_restore_state clears dev->state_saved so call
3600 * pci_save_state to restore it.
3601 */
Greg Rose92915f72010-01-09 02:24:10 +00003602 pci_save_state(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003603
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003604 err = pci_enable_device_mem(pdev);
3605 if (err) {
3606 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3607 return err;
3608 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003609 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003610 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003611 pci_set_master(pdev);
3612
Don Skidmore798e3812013-10-01 04:33:51 -07003613 ixgbevf_reset(adapter);
3614
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003615 rtnl_lock();
3616 err = ixgbevf_init_interrupt_scheme(adapter);
3617 rtnl_unlock();
3618 if (err) {
3619 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3620 return err;
3621 }
3622
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003623 if (netif_running(netdev)) {
3624 err = ixgbevf_open(netdev);
3625 if (err)
3626 return err;
3627 }
3628
3629 netif_device_attach(netdev);
3630
3631 return err;
3632}
3633
3634#endif /* CONFIG_PM */
3635static void ixgbevf_shutdown(struct pci_dev *pdev)
3636{
3637 ixgbevf_suspend(pdev, PMSG_SUSPEND);
Greg Rose92915f72010-01-09 02:24:10 +00003638}
3639
Eric Dumazet4197aa72011-06-22 05:01:35 +00003640static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3641 struct rtnl_link_stats64 *stats)
3642{
3643 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3644 unsigned int start;
3645 u64 bytes, packets;
3646 const struct ixgbevf_ring *ring;
3647 int i;
3648
3649 ixgbevf_update_stats(adapter);
3650
3651 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3652
3653 for (i = 0; i < adapter->num_rx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003654 ring = adapter->rx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003655 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003656 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003657 bytes = ring->stats.bytes;
3658 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003659 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003660 stats->rx_bytes += bytes;
3661 stats->rx_packets += packets;
3662 }
3663
3664 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003665 ring = adapter->tx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003666 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003667 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003668 bytes = ring->stats.bytes;
3669 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003670 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003671 stats->tx_bytes += bytes;
3672 stats->tx_packets += packets;
3673 }
3674
3675 return stats;
3676}
3677
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003678static const struct net_device_ops ixgbevf_netdev_ops = {
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003679 .ndo_open = ixgbevf_open,
3680 .ndo_stop = ixgbevf_close,
3681 .ndo_start_xmit = ixgbevf_xmit_frame,
3682 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
Eric Dumazet4197aa72011-06-22 05:01:35 +00003683 .ndo_get_stats64 = ixgbevf_get_stats,
Greg Rose92915f72010-01-09 02:24:10 +00003684 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003685 .ndo_set_mac_address = ixgbevf_set_mac,
3686 .ndo_change_mtu = ixgbevf_change_mtu,
3687 .ndo_tx_timeout = ixgbevf_tx_timeout,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003688 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3689 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
Jacob Kellerc777cdf2013-09-21 06:24:20 +00003690#ifdef CONFIG_NET_RX_BUSY_POLL
3691 .ndo_busy_poll = ixgbevf_busy_poll_recv,
3692#endif
Greg Rose92915f72010-01-09 02:24:10 +00003693};
Greg Rose92915f72010-01-09 02:24:10 +00003694
3695static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3696{
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003697 dev->netdev_ops = &ixgbevf_netdev_ops;
Greg Rose92915f72010-01-09 02:24:10 +00003698 ixgbevf_set_ethtool_ops(dev);
3699 dev->watchdog_timeo = 5 * HZ;
3700}
3701
3702/**
3703 * ixgbevf_probe - Device Initialization Routine
3704 * @pdev: PCI device information struct
3705 * @ent: entry in ixgbevf_pci_tbl
3706 *
3707 * Returns 0 on success, negative on failure
3708 *
3709 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3710 * The OS initialization, configuring of the adapter private structure,
3711 * and a hardware reset occur.
3712 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003713static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Greg Rose92915f72010-01-09 02:24:10 +00003714{
3715 struct net_device *netdev;
3716 struct ixgbevf_adapter *adapter = NULL;
3717 struct ixgbe_hw *hw = NULL;
3718 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
Greg Rose92915f72010-01-09 02:24:10 +00003719 int err, pci_using_dac;
3720
3721 err = pci_enable_device(pdev);
3722 if (err)
3723 return err;
3724
Russell King53567aa2013-06-10 12:49:38 +01003725 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Greg Rose92915f72010-01-09 02:24:10 +00003726 pci_using_dac = 1;
3727 } else {
Russell King53567aa2013-06-10 12:49:38 +01003728 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003729 if (err) {
Russell King53567aa2013-06-10 12:49:38 +01003730 dev_err(&pdev->dev, "No usable DMA "
3731 "configuration, aborting\n");
3732 goto err_dma;
Greg Rose92915f72010-01-09 02:24:10 +00003733 }
3734 pci_using_dac = 0;
3735 }
3736
3737 err = pci_request_regions(pdev, ixgbevf_driver_name);
3738 if (err) {
3739 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3740 goto err_pci_reg;
3741 }
3742
3743 pci_set_master(pdev);
3744
Greg Rose92915f72010-01-09 02:24:10 +00003745 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3746 MAX_TX_QUEUES);
Greg Rose92915f72010-01-09 02:24:10 +00003747 if (!netdev) {
3748 err = -ENOMEM;
3749 goto err_alloc_etherdev;
3750 }
3751
3752 SET_NETDEV_DEV(netdev, &pdev->dev);
3753
3754 pci_set_drvdata(pdev, netdev);
3755 adapter = netdev_priv(netdev);
3756
3757 adapter->netdev = netdev;
3758 adapter->pdev = pdev;
3759 hw = &adapter->hw;
3760 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00003761 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Greg Rose92915f72010-01-09 02:24:10 +00003762
3763 /*
3764 * call save state here in standalone driver because it relies on
3765 * adapter struct to exist, and needs to call netdev_priv
3766 */
3767 pci_save_state(pdev);
3768
3769 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3770 pci_resource_len(pdev, 0));
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003771 adapter->io_addr = hw->hw_addr;
Greg Rose92915f72010-01-09 02:24:10 +00003772 if (!hw->hw_addr) {
3773 err = -EIO;
3774 goto err_ioremap;
3775 }
3776
3777 ixgbevf_assign_netdev_ops(netdev);
3778
Greg Rose92915f72010-01-09 02:24:10 +00003779 /* Setup hw api */
3780 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3781 hw->mac.type = ii->mac;
3782
3783 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
Greg Rosef416dfc2011-06-08 07:32:38 +00003784 sizeof(struct ixgbe_mbx_operations));
Greg Rose92915f72010-01-09 02:24:10 +00003785
Greg Rose92915f72010-01-09 02:24:10 +00003786 /* setup the private structure */
3787 err = ixgbevf_sw_init(adapter);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003788 if (err)
3789 goto err_sw_init;
3790
3791 /* The HW MAC address was set and/or determined in sw_init */
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003792 if (!is_valid_ether_addr(netdev->dev_addr)) {
3793 pr_err("invalid MAC address\n");
3794 err = -EIO;
3795 goto err_sw_init;
3796 }
Greg Rose92915f72010-01-09 02:24:10 +00003797
Michał Mirosław471a76d2011-06-08 08:53:03 +00003798 netdev->hw_features = NETIF_F_SG |
Greg Rose92915f72010-01-09 02:24:10 +00003799 NETIF_F_IP_CSUM |
Michał Mirosław471a76d2011-06-08 08:53:03 +00003800 NETIF_F_IPV6_CSUM |
3801 NETIF_F_TSO |
3802 NETIF_F_TSO6 |
3803 NETIF_F_RXCSUM;
3804
3805 netdev->features = netdev->hw_features |
Patrick McHardyf6469682013-04-19 02:04:27 +00003806 NETIF_F_HW_VLAN_CTAG_TX |
3807 NETIF_F_HW_VLAN_CTAG_RX |
3808 NETIF_F_HW_VLAN_CTAG_FILTER;
Greg Rose92915f72010-01-09 02:24:10 +00003809
Greg Rose92915f72010-01-09 02:24:10 +00003810 netdev->vlan_features |= NETIF_F_TSO;
3811 netdev->vlan_features |= NETIF_F_TSO6;
3812 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyck3bfacf92010-08-02 14:59:04 +00003813 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003814 netdev->vlan_features |= NETIF_F_SG;
3815
3816 if (pci_using_dac)
3817 netdev->features |= NETIF_F_HIGHDMA;
3818
Jiri Pirko01789342011-08-16 06:29:00 +00003819 netdev->priv_flags |= IFF_UNICAST_FLT;
3820
Greg Rose92915f72010-01-09 02:24:10 +00003821 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00003822 adapter->watchdog_timer.function = ixgbevf_watchdog;
Greg Rose92915f72010-01-09 02:24:10 +00003823 adapter->watchdog_timer.data = (unsigned long)adapter;
3824
Mark Rustadea699562014-03-12 00:38:51 +00003825 if (IXGBE_REMOVED(hw->hw_addr)) {
3826 err = -EIO;
3827 goto err_sw_init;
3828 }
Greg Rose92915f72010-01-09 02:24:10 +00003829 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3830 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
Mark Rustadea699562014-03-12 00:38:51 +00003831 set_bit(__IXGBEVF_WORK_INIT, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003832
3833 err = ixgbevf_init_interrupt_scheme(adapter);
3834 if (err)
3835 goto err_sw_init;
3836
Greg Rose92915f72010-01-09 02:24:10 +00003837 strcpy(netdev->name, "eth%d");
3838
3839 err = register_netdev(netdev);
3840 if (err)
3841 goto err_register;
3842
Greg Rose5d426ad2010-11-16 19:27:19 -08003843 netif_carrier_off(netdev);
3844
Greg Rose33bd9f62010-03-19 02:59:52 +00003845 ixgbevf_init_last_counter_stats(adapter);
3846
Greg Rose92915f72010-01-09 02:24:10 +00003847 /* print the MAC address */
Danny Kukawkaf794e7e2012-02-24 03:45:56 +00003848 hw_dbg(hw, "%pM\n", netdev->dev_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003849
3850 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3851
Greg Rose92915f72010-01-09 02:24:10 +00003852 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
Greg Rose92915f72010-01-09 02:24:10 +00003853 return 0;
3854
3855err_register:
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003856 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003857err_sw_init:
3858 ixgbevf_reset_interrupt_capability(adapter);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003859 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003860err_ioremap:
3861 free_netdev(netdev);
3862err_alloc_etherdev:
3863 pci_release_regions(pdev);
3864err_pci_reg:
3865err_dma:
Mark Rustadbc0c7152014-03-12 00:38:45 +00003866 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3867 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003868 return err;
3869}
3870
3871/**
3872 * ixgbevf_remove - Device Removal Routine
3873 * @pdev: PCI device information struct
3874 *
3875 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3876 * that it should release a PCI device. The could be caused by a
3877 * Hot-Plug event, or because the driver is going to be removed from
3878 * memory.
3879 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003880static void ixgbevf_remove(struct pci_dev *pdev)
Greg Rose92915f72010-01-09 02:24:10 +00003881{
3882 struct net_device *netdev = pci_get_drvdata(pdev);
3883 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3884
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00003885 set_bit(__IXGBEVF_REMOVING, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003886
3887 del_timer_sync(&adapter->watchdog_timer);
3888
Tejun Heo23f333a2010-12-12 16:45:14 +01003889 cancel_work_sync(&adapter->reset_task);
Greg Rose92915f72010-01-09 02:24:10 +00003890 cancel_work_sync(&adapter->watchdog_task);
3891
Alexander Duyckfd13a9a2012-05-11 08:32:24 +00003892 if (netdev->reg_state == NETREG_REGISTERED)
Greg Rose92915f72010-01-09 02:24:10 +00003893 unregister_netdev(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00003894
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003895 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003896 ixgbevf_reset_interrupt_capability(adapter);
3897
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003898 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003899 pci_release_regions(pdev);
3900
3901 hw_dbg(&adapter->hw, "Remove complete\n");
3902
Greg Rose92915f72010-01-09 02:24:10 +00003903 free_netdev(netdev);
3904
Mark Rustadbc0c7152014-03-12 00:38:45 +00003905 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3906 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003907}
3908
Alexander Duyck9f19f312012-05-11 08:33:32 +00003909/**
3910 * ixgbevf_io_error_detected - called when PCI error is detected
3911 * @pdev: Pointer to PCI device
3912 * @state: The current pci connection state
3913 *
3914 * This function is called after a PCI bus error affecting
3915 * this device has been detected.
3916 */
3917static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3918 pci_channel_state_t state)
3919{
3920 struct net_device *netdev = pci_get_drvdata(pdev);
3921 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3922
Mark Rustadea699562014-03-12 00:38:51 +00003923 if (!test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
3924 return PCI_ERS_RESULT_DISCONNECT;
3925
Mark Rustadbc0c7152014-03-12 00:38:45 +00003926 rtnl_lock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003927 netif_device_detach(netdev);
3928
Mark Rustadbc0c7152014-03-12 00:38:45 +00003929 if (state == pci_channel_io_perm_failure) {
3930 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003931 return PCI_ERS_RESULT_DISCONNECT;
Mark Rustadbc0c7152014-03-12 00:38:45 +00003932 }
Alexander Duyck9f19f312012-05-11 08:33:32 +00003933
3934 if (netif_running(netdev))
3935 ixgbevf_down(adapter);
3936
Mark Rustadbc0c7152014-03-12 00:38:45 +00003937 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3938 pci_disable_device(pdev);
3939 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003940
3941 /* Request a slot slot reset. */
3942 return PCI_ERS_RESULT_NEED_RESET;
3943}
3944
3945/**
3946 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3947 * @pdev: Pointer to PCI device
3948 *
3949 * Restart the card from scratch, as if from a cold-boot. Implementation
3950 * resembles the first-half of the ixgbevf_resume routine.
3951 */
3952static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3953{
3954 struct net_device *netdev = pci_get_drvdata(pdev);
3955 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3956
3957 if (pci_enable_device_mem(pdev)) {
3958 dev_err(&pdev->dev,
3959 "Cannot re-enable PCI device after reset.\n");
3960 return PCI_ERS_RESULT_DISCONNECT;
3961 }
3962
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003963 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003964 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck9f19f312012-05-11 08:33:32 +00003965 pci_set_master(pdev);
3966
3967 ixgbevf_reset(adapter);
3968
3969 return PCI_ERS_RESULT_RECOVERED;
3970}
3971
3972/**
3973 * ixgbevf_io_resume - called when traffic can start flowing again.
3974 * @pdev: Pointer to PCI device
3975 *
3976 * This callback is called when the error recovery driver tells us that
3977 * its OK to resume normal operation. Implementation resembles the
3978 * second-half of the ixgbevf_resume routine.
3979 */
3980static void ixgbevf_io_resume(struct pci_dev *pdev)
3981{
3982 struct net_device *netdev = pci_get_drvdata(pdev);
3983 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3984
3985 if (netif_running(netdev))
3986 ixgbevf_up(adapter);
3987
3988 netif_device_attach(netdev);
3989}
3990
3991/* PCI Error Recovery (ERS) */
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003992static const struct pci_error_handlers ixgbevf_err_handler = {
Alexander Duyck9f19f312012-05-11 08:33:32 +00003993 .error_detected = ixgbevf_io_error_detected,
3994 .slot_reset = ixgbevf_io_slot_reset,
3995 .resume = ixgbevf_io_resume,
3996};
3997
Greg Rose92915f72010-01-09 02:24:10 +00003998static struct pci_driver ixgbevf_driver = {
3999 .name = ixgbevf_driver_name,
4000 .id_table = ixgbevf_pci_tbl,
4001 .probe = ixgbevf_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05004002 .remove = ixgbevf_remove,
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00004003#ifdef CONFIG_PM
4004 /* Power Management Hooks */
4005 .suspend = ixgbevf_suspend,
4006 .resume = ixgbevf_resume,
4007#endif
Greg Rose92915f72010-01-09 02:24:10 +00004008 .shutdown = ixgbevf_shutdown,
Alexander Duyck9f19f312012-05-11 08:33:32 +00004009 .err_handler = &ixgbevf_err_handler
Greg Rose92915f72010-01-09 02:24:10 +00004010};
4011
4012/**
Greg Rose65d676c2011-02-03 06:54:13 +00004013 * ixgbevf_init_module - Driver Registration Routine
Greg Rose92915f72010-01-09 02:24:10 +00004014 *
Greg Rose65d676c2011-02-03 06:54:13 +00004015 * ixgbevf_init_module is the first routine called when the driver is
Greg Rose92915f72010-01-09 02:24:10 +00004016 * loaded. All it does is register with the PCI subsystem.
4017 **/
4018static int __init ixgbevf_init_module(void)
4019{
4020 int ret;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00004021 pr_info("%s - version %s\n", ixgbevf_driver_string,
4022 ixgbevf_driver_version);
Greg Rose92915f72010-01-09 02:24:10 +00004023
Jeff Kirsherdbd96362011-10-21 19:38:18 +00004024 pr_info("%s\n", ixgbevf_copyright);
Greg Rose92915f72010-01-09 02:24:10 +00004025
4026 ret = pci_register_driver(&ixgbevf_driver);
4027 return ret;
4028}
4029
4030module_init(ixgbevf_init_module);
4031
4032/**
Greg Rose65d676c2011-02-03 06:54:13 +00004033 * ixgbevf_exit_module - Driver Exit Cleanup Routine
Greg Rose92915f72010-01-09 02:24:10 +00004034 *
Greg Rose65d676c2011-02-03 06:54:13 +00004035 * ixgbevf_exit_module is called just before the driver is removed
Greg Rose92915f72010-01-09 02:24:10 +00004036 * from memory.
4037 **/
4038static void __exit ixgbevf_exit_module(void)
4039{
4040 pci_unregister_driver(&ixgbevf_driver);
4041}
4042
4043#ifdef DEBUG
4044/**
Greg Rose65d676c2011-02-03 06:54:13 +00004045 * ixgbevf_get_hw_dev_name - return device name string
Greg Rose92915f72010-01-09 02:24:10 +00004046 * used by hardware layer to print debugging information
4047 **/
4048char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4049{
4050 struct ixgbevf_adapter *adapter = hw->back;
4051 return adapter->netdev->name;
4052}
4053
4054#endif
4055module_exit(ixgbevf_exit_module);
4056
4057/* ixgbevf_main.c */