blob: 54f7f9cdc4f7555b9305522682e074fe6649635c [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,
Emil Tantilov47068b02014-11-22 07:59:56 +000069 [board_X550_vf] = &ixgbevf_X550_vf_info,
70 [board_X550EM_x_vf] = &ixgbevf_X550EM_x_vf_info,
Greg Rose92915f72010-01-09 02:24:10 +000071};
72
73/* ixgbevf_pci_tbl - PCI Device ID Table
74 *
75 * Wildcard entries (PCI_ANY_ID) should come last
76 * Last entry must be all 0s
77 *
78 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
79 * Class, Class Mask, private data (not used) }
80 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020081static const struct pci_device_id ixgbevf_pci_tbl[] = {
Stephen Hemminger39ba22b2013-02-06 02:37:04 +000082 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
83 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
Emil Tantilov47068b02014-11-22 07:59:56 +000084 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
85 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
Greg Rose92915f72010-01-09 02:24:10 +000086 /* required last entry */
87 {0, }
88};
89MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
90
91MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
Emil Tantilovb8ce18c2014-04-05 05:39:42 +000092MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
Greg Rose92915f72010-01-09 02:24:10 +000093MODULE_LICENSE("GPL");
94MODULE_VERSION(DRV_VERSION);
95
stephen hemmingerb3f4d592012-03-13 06:04:20 +000096#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
97static int debug = -1;
98module_param(debug, int, 0);
99MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
Greg Rose92915f72010-01-09 02:24:10 +0000100
101/* forward decls */
Don Skidmore220fe052013-09-21 01:40:49 +0000102static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000103static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
Alexander Duyck56e94092012-07-20 08:10:03 +0000104static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
Greg Rose92915f72010-01-09 02:24:10 +0000105
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000106static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
107{
108 struct ixgbevf_adapter *adapter = hw->back;
109
110 if (!hw->hw_addr)
111 return;
112 hw->hw_addr = NULL;
113 dev_err(&adapter->pdev->dev, "Adapter removed\n");
Mark Rustadea699562014-03-12 00:38:51 +0000114 if (test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
115 schedule_work(&adapter->watchdog_task);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000116}
117
118static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
119{
120 u32 value;
121
122 /* The following check not only optimizes a bit by not
123 * performing a read on the status register when the
124 * register just read was a status register read that
125 * returned IXGBE_FAILED_READ_REG. It also blocks any
126 * potential recursion.
127 */
128 if (reg == IXGBE_VFSTATUS) {
129 ixgbevf_remove_adapter(hw);
130 return;
131 }
Mark Rustad32c74942014-03-18 07:03:35 +0000132 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000133 if (value == IXGBE_FAILED_READ_REG)
134 ixgbevf_remove_adapter(hw);
135}
136
Mark Rustad32c74942014-03-18 07:03:35 +0000137u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000138{
139 u8 __iomem *reg_addr = ACCESS_ONCE(hw->hw_addr);
140 u32 value;
141
142 if (IXGBE_REMOVED(reg_addr))
143 return IXGBE_FAILED_READ_REG;
144 value = readl(reg_addr + reg);
145 if (unlikely(value == IXGBE_FAILED_READ_REG))
146 ixgbevf_check_remove(hw, reg);
147 return value;
148}
149
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000150/**
Greg Rose65d676c2011-02-03 06:54:13 +0000151 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
Greg Rose92915f72010-01-09 02:24:10 +0000152 * @adapter: pointer to adapter struct
153 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
154 * @queue: queue to map the corresponding interrupt to
155 * @msix_vector: the vector to map to the corresponding queue
Greg Rose92915f72010-01-09 02:24:10 +0000156 */
157static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
158 u8 queue, u8 msix_vector)
159{
160 u32 ivar, index;
161 struct ixgbe_hw *hw = &adapter->hw;
162 if (direction == -1) {
163 /* other causes */
164 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
165 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
166 ivar &= ~0xFF;
167 ivar |= msix_vector;
168 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
169 } else {
170 /* tx or rx causes */
171 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
172 index = ((16 * (queue & 1)) + (8 * direction));
173 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
174 ivar &= ~(0xFF << index);
175 ivar |= (msix_vector << index);
176 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
177 }
178}
179
Alexander Duyck70a10e22012-05-11 08:33:21 +0000180static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800181 struct ixgbevf_tx_buffer *tx_buffer)
Greg Rose92915f72010-01-09 02:24:10 +0000182{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800183 if (tx_buffer->skb) {
184 dev_kfree_skb_any(tx_buffer->skb);
185 if (dma_unmap_len(tx_buffer, len))
Alexander Duyck70a10e22012-05-11 08:33:21 +0000186 dma_unmap_single(tx_ring->dev,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800187 dma_unmap_addr(tx_buffer, dma),
188 dma_unmap_len(tx_buffer, len),
Nick Nunley2a1f8792010-04-27 13:10:50 +0000189 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800190 } else if (dma_unmap_len(tx_buffer, len)) {
191 dma_unmap_page(tx_ring->dev,
192 dma_unmap_addr(tx_buffer, dma),
193 dma_unmap_len(tx_buffer, len),
194 DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000195 }
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800196 tx_buffer->next_to_watch = NULL;
197 tx_buffer->skb = NULL;
198 dma_unmap_len_set(tx_buffer, len, 0);
199 /* tx_buffer must be completely set up in the transmit path */
Greg Rose92915f72010-01-09 02:24:10 +0000200}
201
Greg Rose92915f72010-01-09 02:24:10 +0000202#define IXGBE_MAX_TXD_PWR 14
203#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
204
205/* Tx Descriptors needed, worst case */
Alexander Duyck35959902012-05-11 08:32:40 +0000206#define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
207#define DESC_NEEDED (MAX_SKB_FRAGS + 4)
Greg Rose92915f72010-01-09 02:24:10 +0000208
209static void ixgbevf_tx_timeout(struct net_device *netdev);
210
211/**
212 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000213 * @q_vector: board private structure
Greg Rose92915f72010-01-09 02:24:10 +0000214 * @tx_ring: tx ring to clean
215 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000216static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
Greg Rose92915f72010-01-09 02:24:10 +0000217 struct ixgbevf_ring *tx_ring)
218{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000219 struct ixgbevf_adapter *adapter = q_vector->adapter;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800220 struct ixgbevf_tx_buffer *tx_buffer;
221 union ixgbe_adv_tx_desc *tx_desc;
Greg Rose92915f72010-01-09 02:24:10 +0000222 unsigned int total_bytes = 0, total_packets = 0;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800223 unsigned int budget = tx_ring->count / 2;
224 unsigned int i = tx_ring->next_to_clean;
Greg Rose92915f72010-01-09 02:24:10 +0000225
Alexander Duyck10cc1bd2012-07-16 23:44:48 +0000226 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
227 return true;
228
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800229 tx_buffer = &tx_ring->tx_buffer_info[i];
230 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
231 i -= tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000232
Alexander Duycke757e3e2013-01-31 07:43:22 +0000233 do {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800234 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000235
236 /* if next_to_watch is not set then there is no work pending */
237 if (!eop_desc)
238 break;
239
240 /* prevent any other reads prior to eop_desc */
241 read_barrier_depends();
242
243 /* if DD is not set pending work has not been completed */
244 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
245 break;
246
247 /* clear next_to_watch to prevent false hangs */
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800248 tx_buffer->next_to_watch = NULL;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000249
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800250 /* update the statistics for this packet */
251 total_bytes += tx_buffer->bytecount;
252 total_packets += tx_buffer->gso_segs;
Greg Rose92915f72010-01-09 02:24:10 +0000253
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800254 /* free the skb */
255 dev_kfree_skb_any(tx_buffer->skb);
256
257 /* unmap skb header data */
258 dma_unmap_single(tx_ring->dev,
259 dma_unmap_addr(tx_buffer, dma),
260 dma_unmap_len(tx_buffer, len),
261 DMA_TO_DEVICE);
262
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800263 /* clear tx_buffer data */
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800264 tx_buffer->skb = NULL;
265 dma_unmap_len_set(tx_buffer, len, 0);
Greg Rose92915f72010-01-09 02:24:10 +0000266
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800267 /* unmap remaining buffers */
268 while (tx_desc != eop_desc) {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800269 tx_buffer++;
270 tx_desc++;
Greg Rose92915f72010-01-09 02:24:10 +0000271 i++;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800272 if (unlikely(!i)) {
273 i -= tx_ring->count;
274 tx_buffer = tx_ring->tx_buffer_info;
275 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
276 }
Alexander Duycke757e3e2013-01-31 07:43:22 +0000277
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800278 /* unmap any remaining paged data */
279 if (dma_unmap_len(tx_buffer, len)) {
280 dma_unmap_page(tx_ring->dev,
281 dma_unmap_addr(tx_buffer, dma),
282 dma_unmap_len(tx_buffer, len),
283 DMA_TO_DEVICE);
284 dma_unmap_len_set(tx_buffer, len, 0);
285 }
Greg Rose92915f72010-01-09 02:24:10 +0000286 }
287
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800288 /* move us one more past the eop_desc for start of next pkt */
289 tx_buffer++;
290 tx_desc++;
291 i++;
292 if (unlikely(!i)) {
293 i -= tx_ring->count;
294 tx_buffer = tx_ring->tx_buffer_info;
295 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
296 }
297
298 /* issue prefetch for next Tx descriptor */
299 prefetch(tx_desc);
300
301 /* update budget accounting */
302 budget--;
303 } while (likely(budget));
304
305 i += tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000306 tx_ring->next_to_clean = i;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000307 u64_stats_update_begin(&tx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800308 tx_ring->stats.bytes += total_bytes;
309 tx_ring->stats.packets += total_packets;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000310 u64_stats_update_end(&tx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000311 q_vector->tx.total_bytes += total_bytes;
312 q_vector->tx.total_packets += total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000313
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800314#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
315 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
316 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
317 /* Make sure that anybody stopping the queue after this
318 * sees the new next_to_clean.
319 */
320 smp_mb();
321
322 if (__netif_subqueue_stopped(tx_ring->netdev,
323 tx_ring->queue_index) &&
324 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
325 netif_wake_subqueue(tx_ring->netdev,
326 tx_ring->queue_index);
327 ++tx_ring->tx_stats.restart_queue;
328 }
329 }
330
331 return !!budget;
Greg Rose92915f72010-01-09 02:24:10 +0000332}
333
334/**
Jacob Keller08681612013-09-21 06:24:09 +0000335 * ixgbevf_rx_skb - Helper function to determine proper Rx method
336 * @q_vector: structure containing interrupt and ring information
337 * @skb: packet to send up
Jacob Keller08681612013-09-21 06:24:09 +0000338 **/
339static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
Emil Tantilovdff80522014-11-08 01:39:25 +0000340 struct sk_buff *skb)
Jacob Keller08681612013-09-21 06:24:09 +0000341{
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000342#ifdef CONFIG_NET_RX_BUSY_POLL
343 skb_mark_napi_id(skb, &q_vector->napi);
344
345 if (ixgbevf_qv_busy_polling(q_vector)) {
346 netif_receive_skb(skb);
347 /* exit early if we busy polled */
348 return;
349 }
350#endif /* CONFIG_NET_RX_BUSY_POLL */
Emil Tantilov688ff322014-11-08 01:39:56 +0000351
352 napi_gro_receive(&q_vector->napi, skb);
Jacob Keller08681612013-09-21 06:24:09 +0000353}
354
Emil Tantilovec62fe22014-11-08 01:39:20 +0000355/* ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
356 * @ring: structure containig ring specific data
357 * @rx_desc: current Rx descriptor being processed
Greg Rose92915f72010-01-09 02:24:10 +0000358 * @skb: skb currently being received and modified
Emil Tantilovec62fe22014-11-08 01:39:20 +0000359 */
Greg Rose55fb2772012-11-06 05:53:32 +0000360static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
Emil Tantilovec62fe22014-11-08 01:39:20 +0000361 union ixgbe_adv_rx_desc *rx_desc,
362 struct sk_buff *skb)
Greg Rose92915f72010-01-09 02:24:10 +0000363{
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700364 skb_checksum_none_assert(skb);
Greg Rose92915f72010-01-09 02:24:10 +0000365
366 /* Rx csum disabled */
Alexander Duyckfb401952012-05-11 08:33:16 +0000367 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Greg Rose92915f72010-01-09 02:24:10 +0000368 return;
369
370 /* if IP and error */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000371 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
372 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800373 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000374 return;
375 }
376
Emil Tantilovec62fe22014-11-08 01:39:20 +0000377 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
Greg Rose92915f72010-01-09 02:24:10 +0000378 return;
379
Emil Tantilovec62fe22014-11-08 01:39:20 +0000380 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800381 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000382 return;
383 }
384
385 /* It must be a TCP or UDP packet with a valid checksum */
386 skb->ip_summed = CHECKSUM_UNNECESSARY;
Greg Rose92915f72010-01-09 02:24:10 +0000387}
388
Emil Tantilovdff80522014-11-08 01:39:25 +0000389/* ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
390 * @rx_ring: rx descriptor ring packet is being transacted on
391 * @rx_desc: pointer to the EOP Rx descriptor
392 * @skb: pointer to current skb being populated
393 *
394 * This function checks the ring, descriptor, and packet information in
395 * order to populate the checksum, VLAN, protocol, and other fields within
396 * the skb.
397 */
398static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
399 union ixgbe_adv_rx_desc *rx_desc,
400 struct sk_buff *skb)
401{
402 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
403
404 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
405 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
406 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
407
408 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
409 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
410 }
411
412 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
413}
414
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000415/**
416 * ixgbevf_is_non_eop - process handling of non-EOP buffers
417 * @rx_ring: Rx ring being processed
418 * @rx_desc: Rx descriptor for current buffer
419 * @skb: current socket buffer containing buffer in progress
420 *
421 * This function updates next to clean. If the buffer is an EOP buffer
422 * this function exits returning false, otherwise it will place the
423 * sk_buff in the next buffer to be chained and return true indicating
424 * that this is in fact a non-EOP buffer.
425 **/
426static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
Emil Tantilovbad17232014-11-21 02:57:15 +0000427 union ixgbe_adv_rx_desc *rx_desc)
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000428{
429 u32 ntc = rx_ring->next_to_clean + 1;
430
431 /* fetch, update, and store next to clean */
432 ntc = (ntc < rx_ring->count) ? ntc : 0;
433 rx_ring->next_to_clean = ntc;
434
435 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
436
437 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
438 return false;
439
440 return true;
441}
442
Emil Tantilovbad17232014-11-21 02:57:15 +0000443static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
444 struct ixgbevf_rx_buffer *bi)
Emil Tantilovbafa5782014-11-08 01:39:15 +0000445{
Emil Tantilovbad17232014-11-21 02:57:15 +0000446 struct page *page = bi->page;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000447 dma_addr_t dma = bi->dma;
448
Emil Tantilovbad17232014-11-21 02:57:15 +0000449 /* since we are recycling buffers we should seldom need to alloc */
450 if (likely(page))
Emil Tantilovbafa5782014-11-08 01:39:15 +0000451 return true;
452
Emil Tantilovbad17232014-11-21 02:57:15 +0000453 /* alloc new page for storage */
454 page = dev_alloc_page();
455 if (unlikely(!page)) {
456 rx_ring->rx_stats.alloc_rx_page_failed++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000457 return false;
458 }
459
Emil Tantilovbad17232014-11-21 02:57:15 +0000460 /* map page for use */
461 dma = dma_map_page(rx_ring->dev, page, 0,
462 PAGE_SIZE, DMA_FROM_DEVICE);
Emil Tantilovbafa5782014-11-08 01:39:15 +0000463
464 /* if mapping failed free memory back to system since
465 * there isn't much point in holding memory we can't use
466 */
467 if (dma_mapping_error(rx_ring->dev, dma)) {
Emil Tantilovbad17232014-11-21 02:57:15 +0000468 __free_page(page);
Emil Tantilovbafa5782014-11-08 01:39:15 +0000469
470 rx_ring->rx_stats.alloc_rx_buff_failed++;
471 return false;
472 }
473
Emil Tantilovbafa5782014-11-08 01:39:15 +0000474 bi->dma = dma;
Emil Tantilovbad17232014-11-21 02:57:15 +0000475 bi->page = page;
476 bi->page_offset = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000477
478 return true;
479}
480
Greg Rose92915f72010-01-09 02:24:10 +0000481/**
482 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
Emil Tantilov095e2612014-01-17 18:30:00 -0800483 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
Emil Tantilovbafa5782014-11-08 01:39:15 +0000484 * @cleaned_count: number of buffers to replace
Greg Rose92915f72010-01-09 02:24:10 +0000485 **/
Emil Tantilov095e2612014-01-17 18:30:00 -0800486static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
Emil Tantilovbafa5782014-11-08 01:39:15 +0000487 u16 cleaned_count)
Greg Rose92915f72010-01-09 02:24:10 +0000488{
Greg Rose92915f72010-01-09 02:24:10 +0000489 union ixgbe_adv_rx_desc *rx_desc;
490 struct ixgbevf_rx_buffer *bi;
Alexander Duyckfb401952012-05-11 08:33:16 +0000491 unsigned int i = rx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +0000492
Emil Tantilovbafa5782014-11-08 01:39:15 +0000493 /* nothing to do or no valid netdev defined */
494 if (!cleaned_count || !rx_ring->netdev)
495 return;
Greg Roseb9dd2452012-11-02 05:50:21 +0000496
Emil Tantilovbafa5782014-11-08 01:39:15 +0000497 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
498 bi = &rx_ring->rx_buffer_info[i];
499 i -= rx_ring->count;
Greg Roseb9dd2452012-11-02 05:50:21 +0000500
Emil Tantilovbafa5782014-11-08 01:39:15 +0000501 do {
Emil Tantilovbad17232014-11-21 02:57:15 +0000502 if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
Emil Tantilovbafa5782014-11-08 01:39:15 +0000503 break;
Emil Tantilov05d063a2014-01-17 18:29:59 -0800504
Emil Tantilovbafa5782014-11-08 01:39:15 +0000505 /* Refresh the desc even if pkt_addr didn't change
506 * because each write-back erases this info.
507 */
Emil Tantilovbad17232014-11-21 02:57:15 +0000508 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Greg Rose92915f72010-01-09 02:24:10 +0000509
Emil Tantilovbafa5782014-11-08 01:39:15 +0000510 rx_desc++;
511 bi++;
Greg Rose92915f72010-01-09 02:24:10 +0000512 i++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000513 if (unlikely(!i)) {
514 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
515 bi = rx_ring->rx_buffer_info;
516 i -= rx_ring->count;
517 }
Greg Rose92915f72010-01-09 02:24:10 +0000518
Emil Tantilovbafa5782014-11-08 01:39:15 +0000519 /* clear the hdr_addr for the next_to_use descriptor */
520 rx_desc->read.hdr_addr = 0;
521
522 cleaned_count--;
523 } while (cleaned_count);
524
525 i += rx_ring->count;
526
527 if (rx_ring->next_to_use != i) {
528 /* record the next descriptor to use */
529 rx_ring->next_to_use = i;
530
Emil Tantilovbad17232014-11-21 02:57:15 +0000531 /* update next to alloc since we have filled the ring */
532 rx_ring->next_to_alloc = i;
533
Emil Tantilovbafa5782014-11-08 01:39:15 +0000534 /* Force memory writes to complete before letting h/w
535 * know there are new descriptors to fetch. (Only
536 * applicable for weak-ordered memory model archs,
537 * such as IA-64).
538 */
539 wmb();
540 ixgbevf_write_tail(rx_ring, i);
541 }
Greg Rose92915f72010-01-09 02:24:10 +0000542}
543
Emil Tantilovbad17232014-11-21 02:57:15 +0000544/* ixgbevf_pull_tail - ixgbevf specific version of skb_pull_tail
545 * @rx_ring: rx descriptor ring packet is being transacted on
546 * @skb: pointer to current skb being adjusted
547 *
548 * This function is an ixgbevf specific version of __pskb_pull_tail. The
549 * main difference between this version and the original function is that
550 * this function can make several assumptions about the state of things
551 * that allow for significant optimizations versus the standard function.
552 * As a result we can do things like drop a frag and maintain an accurate
553 * truesize for the skb.
554 */
555static void ixgbevf_pull_tail(struct ixgbevf_ring *rx_ring,
556 struct sk_buff *skb)
557{
558 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
559 unsigned char *va;
560 unsigned int pull_len;
561
562 /* it is valid to use page_address instead of kmap since we are
563 * working with pages allocated out of the lomem pool per
564 * alloc_page(GFP_ATOMIC)
565 */
566 va = skb_frag_address(frag);
567
568 /* we need the header to contain the greater of either ETH_HLEN or
569 * 60 bytes if the skb->len is less than 60 for skb_pad.
570 */
571 pull_len = eth_get_headlen(va, IXGBEVF_RX_HDR_SIZE);
572
573 /* align pull length to size of long to optimize memcpy performance */
574 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
575
576 /* update all of the pointers */
577 skb_frag_size_sub(frag, pull_len);
578 frag->page_offset += pull_len;
579 skb->data_len -= pull_len;
580 skb->tail += pull_len;
581}
582
583/* ixgbevf_cleanup_headers - Correct corrupted or empty headers
584 * @rx_ring: rx descriptor ring packet is being transacted on
585 * @rx_desc: pointer to the EOP Rx descriptor
586 * @skb: pointer to current skb being fixed
587 *
588 * Check for corrupted packet headers caused by senders on the local L2
589 * embedded NIC switch not setting up their Tx Descriptors right. These
590 * should be very rare.
591 *
592 * Also address the case where we are pulling data in on pages only
593 * and as such no data is present in the skb header.
594 *
595 * In addition if skb is not at least 60 bytes we need to pad it so that
596 * it is large enough to qualify as a valid Ethernet frame.
597 *
598 * Returns true if an error was encountered and skb was freed.
599 */
600static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
601 union ixgbe_adv_rx_desc *rx_desc,
602 struct sk_buff *skb)
603{
604 /* verify that the packet does not have any known errors */
605 if (unlikely(ixgbevf_test_staterr(rx_desc,
606 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
607 struct net_device *netdev = rx_ring->netdev;
608
609 if (!(netdev->features & NETIF_F_RXALL)) {
610 dev_kfree_skb_any(skb);
611 return true;
612 }
613 }
614
615 /* place header in linear portion of buffer */
616 if (skb_is_nonlinear(skb))
617 ixgbevf_pull_tail(rx_ring, skb);
618
619 /* if skb_pad returns an error the skb was freed */
620 if (unlikely(skb->len < 60)) {
621 int pad_len = 60 - skb->len;
622
623 if (skb_pad(skb, pad_len))
624 return true;
625 __skb_put(skb, pad_len);
626 }
627
628 return false;
629}
630
631/* ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
632 * @rx_ring: rx descriptor ring to store buffers on
633 * @old_buff: donor buffer to have page reused
634 *
635 * Synchronizes page for reuse by the adapter
636 */
637static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
638 struct ixgbevf_rx_buffer *old_buff)
639{
640 struct ixgbevf_rx_buffer *new_buff;
641 u16 nta = rx_ring->next_to_alloc;
642
643 new_buff = &rx_ring->rx_buffer_info[nta];
644
645 /* update, and store next to alloc */
646 nta++;
647 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
648
649 /* transfer page from old buffer to new buffer */
650 new_buff->page = old_buff->page;
651 new_buff->dma = old_buff->dma;
652 new_buff->page_offset = old_buff->page_offset;
653
654 /* sync the buffer for use by the device */
655 dma_sync_single_range_for_device(rx_ring->dev, new_buff->dma,
656 new_buff->page_offset,
657 IXGBEVF_RX_BUFSZ,
658 DMA_FROM_DEVICE);
659}
660
661static inline bool ixgbevf_page_is_reserved(struct page *page)
662{
663 return (page_to_nid(page) != numa_mem_id()) || page->pfmemalloc;
664}
665
666/* ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
667 * @rx_ring: rx descriptor ring to transact packets on
668 * @rx_buffer: buffer containing page to add
669 * @rx_desc: descriptor containing length of buffer written by hardware
670 * @skb: sk_buff to place the data into
671 *
672 * This function will add the data contained in rx_buffer->page to the skb.
673 * This is done either through a direct copy if the data in the buffer is
674 * less than the skb header size, otherwise it will just attach the page as
675 * a frag to the skb.
676 *
677 * The function will then update the page offset if necessary and return
678 * true if the buffer can be reused by the adapter.
679 */
680static bool ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
681 struct ixgbevf_rx_buffer *rx_buffer,
682 union ixgbe_adv_rx_desc *rx_desc,
683 struct sk_buff *skb)
684{
685 struct page *page = rx_buffer->page;
686 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
687#if (PAGE_SIZE < 8192)
688 unsigned int truesize = IXGBEVF_RX_BUFSZ;
689#else
690 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
691#endif
692
693 if ((size <= IXGBEVF_RX_HDR_SIZE) && !skb_is_nonlinear(skb)) {
694 unsigned char *va = page_address(page) + rx_buffer->page_offset;
695
696 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
697
698 /* page is not reserved, we can reuse buffer as is */
699 if (likely(!ixgbevf_page_is_reserved(page)))
700 return true;
701
702 /* this page cannot be reused so discard it */
703 put_page(page);
704 return false;
705 }
706
707 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
708 rx_buffer->page_offset, size, truesize);
709
710 /* avoid re-using remote pages */
711 if (unlikely(ixgbevf_page_is_reserved(page)))
712 return false;
713
714#if (PAGE_SIZE < 8192)
715 /* if we are only owner of page we can reuse it */
716 if (unlikely(page_count(page) != 1))
717 return false;
718
719 /* flip page offset to other buffer */
720 rx_buffer->page_offset ^= IXGBEVF_RX_BUFSZ;
721
722#else
723 /* move offset up to the next cache line */
724 rx_buffer->page_offset += truesize;
725
726 if (rx_buffer->page_offset > (PAGE_SIZE - IXGBEVF_RX_BUFSZ))
727 return false;
728
729#endif
730 /* Even if we own the page, we are not allowed to use atomic_set()
731 * This would break get_page_unless_zero() users.
732 */
733 atomic_inc(&page->_count);
734
735 return true;
736}
737
738static struct sk_buff *ixgbevf_fetch_rx_buffer(struct ixgbevf_ring *rx_ring,
739 union ixgbe_adv_rx_desc *rx_desc,
740 struct sk_buff *skb)
741{
742 struct ixgbevf_rx_buffer *rx_buffer;
743 struct page *page;
744
745 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
746 page = rx_buffer->page;
747 prefetchw(page);
748
749 if (likely(!skb)) {
750 void *page_addr = page_address(page) +
751 rx_buffer->page_offset;
752
753 /* prefetch first cache line of first page */
754 prefetch(page_addr);
755#if L1_CACHE_BYTES < 128
756 prefetch(page_addr + L1_CACHE_BYTES);
757#endif
758
759 /* allocate a skb to store the frags */
760 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
761 IXGBEVF_RX_HDR_SIZE);
762 if (unlikely(!skb)) {
763 rx_ring->rx_stats.alloc_rx_buff_failed++;
764 return NULL;
765 }
766
767 /* we will be copying header into skb->data in
768 * pskb_may_pull so it is in our interest to prefetch
769 * it now to avoid a possible cache miss
770 */
771 prefetchw(skb->data);
772 }
773
774 /* we are reusing so sync this buffer for CPU use */
775 dma_sync_single_range_for_cpu(rx_ring->dev,
776 rx_buffer->dma,
777 rx_buffer->page_offset,
778 IXGBEVF_RX_BUFSZ,
779 DMA_FROM_DEVICE);
780
781 /* pull page into skb */
782 if (ixgbevf_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
783 /* hand second half of page back to the ring */
784 ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
785 } else {
786 /* we are not reusing the buffer so unmap it */
787 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
788 PAGE_SIZE, DMA_FROM_DEVICE);
789 }
790
791 /* clear contents of buffer_info */
792 rx_buffer->dma = 0;
793 rx_buffer->page = NULL;
794
795 return skb;
796}
797
Greg Rose92915f72010-01-09 02:24:10 +0000798static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000799 u32 qmask)
Greg Rose92915f72010-01-09 02:24:10 +0000800{
Greg Rose92915f72010-01-09 02:24:10 +0000801 struct ixgbe_hw *hw = &adapter->hw;
802
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000803 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
Greg Rose92915f72010-01-09 02:24:10 +0000804}
805
Jacob Keller08e50a22013-09-21 06:24:14 +0000806static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
807 struct ixgbevf_ring *rx_ring,
808 int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000809{
Greg Rose92915f72010-01-09 02:24:10 +0000810 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000811 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
Emil Tantilovbad17232014-11-21 02:57:15 +0000812 struct sk_buff *skb = rx_ring->skb;
Greg Rose92915f72010-01-09 02:24:10 +0000813
Emil Tantilov66224022014-11-08 01:39:51 +0000814 while (likely(total_rx_packets < budget)) {
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000815 union ixgbe_adv_rx_desc *rx_desc;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000816
Emil Tantilov0579eef2014-11-08 01:39:35 +0000817 /* return some buffers to hardware, one at a time is too slow */
818 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
819 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
820 cleaned_count = 0;
821 }
822
Emil Tantilovbad17232014-11-21 02:57:15 +0000823 rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000824
825 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_DD))
Greg Rose92915f72010-01-09 02:24:10 +0000826 break;
Greg Rose92915f72010-01-09 02:24:10 +0000827
Emil Tantilov0579eef2014-11-08 01:39:35 +0000828 /* This memory barrier is needed to keep us from reading
829 * any other fields out of the rx_desc until we know the
830 * RXD_STAT_DD bit is set
831 */
832 rmb();
Emil Tantilovec62fe22014-11-08 01:39:20 +0000833
Emil Tantilovbad17232014-11-21 02:57:15 +0000834 /* retrieve a buffer from the ring */
835 skb = ixgbevf_fetch_rx_buffer(rx_ring, rx_desc, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000836
Emil Tantilovbad17232014-11-21 02:57:15 +0000837 /* exit if we failed to retrieve a buffer */
838 if (!skb)
839 break;
Greg Rose92915f72010-01-09 02:24:10 +0000840
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000841 cleaned_count++;
842
Emil Tantilovbad17232014-11-21 02:57:15 +0000843 /* fetch next buffer in frame if non-eop */
844 if (ixgbevf_is_non_eop(rx_ring, rx_desc))
Emil Tantilov0579eef2014-11-08 01:39:35 +0000845 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000846
Emil Tantilovbad17232014-11-21 02:57:15 +0000847 /* verify the packet layout is correct */
848 if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
849 skb = NULL;
Emil Tantilov0579eef2014-11-08 01:39:35 +0000850 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000851 }
852
Greg Rose92915f72010-01-09 02:24:10 +0000853 /* probably a little skewed due to removing CRC */
854 total_rx_bytes += skb->len;
Greg Rose92915f72010-01-09 02:24:10 +0000855
John Fastabend815cccb2012-10-24 08:13:09 +0000856 /* Workaround hardware that can't do proper VEPA multicast
857 * source pruning.
858 */
Florian Fainellibd9d5592014-02-28 15:46:49 -0800859 if ((skb->pkt_type == PACKET_BROADCAST ||
860 skb->pkt_type == PACKET_MULTICAST) &&
Emil Tantilov095e2612014-01-17 18:30:00 -0800861 ether_addr_equal(rx_ring->netdev->dev_addr,
Joe Perches7367d0b2013-09-01 11:51:23 -0700862 eth_hdr(skb)->h_source)) {
John Fastabend815cccb2012-10-24 08:13:09 +0000863 dev_kfree_skb_irq(skb);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000864 continue;
John Fastabend815cccb2012-10-24 08:13:09 +0000865 }
866
Emil Tantilovdff80522014-11-08 01:39:25 +0000867 /* populate checksum, VLAN, and protocol */
868 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
869
870 ixgbevf_rx_skb(q_vector, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000871
Emil Tantilovbad17232014-11-21 02:57:15 +0000872 /* reset skb pointer */
873 skb = NULL;
874
Emil Tantilov0579eef2014-11-08 01:39:35 +0000875 /* update budget accounting */
Emil Tantilov66224022014-11-08 01:39:51 +0000876 total_rx_packets++;
877 }
Greg Rose92915f72010-01-09 02:24:10 +0000878
Emil Tantilovbad17232014-11-21 02:57:15 +0000879 /* place incomplete frames back on ring for completion */
880 rx_ring->skb = skb;
881
Eric Dumazet4197aa72011-06-22 05:01:35 +0000882 u64_stats_update_begin(&rx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800883 rx_ring->stats.packets += total_rx_packets;
884 rx_ring->stats.bytes += total_rx_bytes;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000885 u64_stats_update_end(&rx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000886 q_vector->rx.total_packets += total_rx_packets;
887 q_vector->rx.total_bytes += total_rx_bytes;
Greg Rose92915f72010-01-09 02:24:10 +0000888
Jacob Keller08e50a22013-09-21 06:24:14 +0000889 return total_rx_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000890}
891
892/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000893 * ixgbevf_poll - NAPI polling calback
Greg Rose92915f72010-01-09 02:24:10 +0000894 * @napi: napi struct with our devices info in it
895 * @budget: amount of work driver is allowed to do this pass, in packets
896 *
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000897 * This function will clean more than one or more rings associated with a
Greg Rose92915f72010-01-09 02:24:10 +0000898 * q_vector.
899 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000900static int ixgbevf_poll(struct napi_struct *napi, int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000901{
902 struct ixgbevf_q_vector *q_vector =
903 container_of(napi, struct ixgbevf_q_vector, napi);
904 struct ixgbevf_adapter *adapter = q_vector->adapter;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000905 struct ixgbevf_ring *ring;
906 int per_ring_budget;
907 bool clean_complete = true;
908
909 ixgbevf_for_each_ring(ring, q_vector->tx)
910 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
Greg Rose92915f72010-01-09 02:24:10 +0000911
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000912#ifdef CONFIG_NET_RX_BUSY_POLL
913 if (!ixgbevf_qv_lock_napi(q_vector))
914 return budget;
915#endif
916
Greg Rose92915f72010-01-09 02:24:10 +0000917 /* attempt to distribute budget to each queue fairly, but don't allow
918 * the budget to go below 1 because we'll exit polling */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000919 if (q_vector->rx.count > 1)
920 per_ring_budget = max(budget/q_vector->rx.count, 1);
921 else
922 per_ring_budget = budget;
Greg Rose92915f72010-01-09 02:24:10 +0000923
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 Rose92915f72010-01-09 02:24:10 +0000928
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000929#ifdef CONFIG_NET_RX_BUSY_POLL
930 ixgbevf_qv_unlock_napi(q_vector);
931#endif
932
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000933 /* If all work not completed, return budget and keep polling */
934 if (!clean_complete)
935 return budget;
936 /* all work done, exit the polling mode */
937 napi_complete(napi);
938 if (adapter->rx_itr_setting & 1)
939 ixgbevf_set_itr(q_vector);
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000940 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
941 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000942 ixgbevf_irq_enable_queues(adapter,
943 1 << q_vector->v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000944
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000945 return 0;
Greg Rose92915f72010-01-09 02:24:10 +0000946}
947
Greg Rosece422602012-05-22 02:17:49 +0000948/**
949 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
950 * @q_vector: structure containing interrupt and ring information
951 */
Jacob Keller38496232013-10-22 06:19:18 +0000952void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
Greg Rosece422602012-05-22 02:17:49 +0000953{
954 struct ixgbevf_adapter *adapter = q_vector->adapter;
955 struct ixgbe_hw *hw = &adapter->hw;
956 int v_idx = q_vector->v_idx;
957 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
958
959 /*
960 * set the WDIS bit to not clear the timer bits and cause an
961 * immediate assertion of the interrupt
962 */
963 itr_reg |= IXGBE_EITR_CNT_WDIS;
964
965 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
966}
Greg Rose92915f72010-01-09 02:24:10 +0000967
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000968#ifdef CONFIG_NET_RX_BUSY_POLL
969/* must be called with local_bh_disable()d */
970static int ixgbevf_busy_poll_recv(struct napi_struct *napi)
971{
972 struct ixgbevf_q_vector *q_vector =
973 container_of(napi, struct ixgbevf_q_vector, napi);
974 struct ixgbevf_adapter *adapter = q_vector->adapter;
975 struct ixgbevf_ring *ring;
976 int found = 0;
977
978 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
979 return LL_FLUSH_FAILED;
980
981 if (!ixgbevf_qv_lock_poll(q_vector))
982 return LL_FLUSH_BUSY;
983
984 ixgbevf_for_each_ring(ring, q_vector->rx) {
985 found = ixgbevf_clean_rx_irq(q_vector, ring, 4);
Jacob Keller3b5dca22013-09-21 06:24:25 +0000986#ifdef BP_EXTENDED_STATS
987 if (found)
Emil Tantilov095e2612014-01-17 18:30:00 -0800988 ring->stats.cleaned += found;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000989 else
Emil Tantilov095e2612014-01-17 18:30:00 -0800990 ring->stats.misses++;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000991#endif
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000992 if (found)
993 break;
994 }
995
996 ixgbevf_qv_unlock_poll(q_vector);
997
998 return found;
999}
1000#endif /* CONFIG_NET_RX_BUSY_POLL */
1001
Greg Rose92915f72010-01-09 02:24:10 +00001002/**
1003 * ixgbevf_configure_msix - Configure MSI-X hardware
1004 * @adapter: board private structure
1005 *
1006 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1007 * interrupts.
1008 **/
1009static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1010{
1011 struct ixgbevf_q_vector *q_vector;
Alexander Duyck6b43c442012-05-11 08:32:45 +00001012 int q_vectors, v_idx;
Greg Rose92915f72010-01-09 02:24:10 +00001013
1014 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001015 adapter->eims_enable_mask = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001016
1017 /*
1018 * Populate the IVAR table and set the ITR values to the
1019 * corresponding register.
1020 */
1021 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
Alexander Duyck6b43c442012-05-11 08:32:45 +00001022 struct ixgbevf_ring *ring;
Greg Rose92915f72010-01-09 02:24:10 +00001023 q_vector = adapter->q_vector[v_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001024
Alexander Duyck6b43c442012-05-11 08:32:45 +00001025 ixgbevf_for_each_ring(ring, q_vector->rx)
1026 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +00001027
Alexander Duyck6b43c442012-05-11 08:32:45 +00001028 ixgbevf_for_each_ring(ring, q_vector->tx)
1029 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +00001030
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001031 if (q_vector->tx.ring && !q_vector->rx.ring) {
1032 /* tx only vector */
1033 if (adapter->tx_itr_setting == 1)
1034 q_vector->itr = IXGBE_10K_ITR;
1035 else
1036 q_vector->itr = adapter->tx_itr_setting;
1037 } else {
1038 /* rx or rx/tx vector */
1039 if (adapter->rx_itr_setting == 1)
1040 q_vector->itr = IXGBE_20K_ITR;
1041 else
1042 q_vector->itr = adapter->rx_itr_setting;
1043 }
Greg Rose92915f72010-01-09 02:24:10 +00001044
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001045 /* add q_vector eims value to global eims_enable_mask */
1046 adapter->eims_enable_mask |= 1 << v_idx;
1047
1048 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001049 }
1050
1051 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001052 /* setup eims_other and add value to global eims_enable_mask */
1053 adapter->eims_other = 1 << v_idx;
1054 adapter->eims_enable_mask |= adapter->eims_other;
Greg Rose92915f72010-01-09 02:24:10 +00001055}
1056
1057enum latency_range {
1058 lowest_latency = 0,
1059 low_latency = 1,
1060 bulk_latency = 2,
1061 latency_invalid = 255
1062};
1063
1064/**
1065 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001066 * @q_vector: structure containing interrupt and ring information
1067 * @ring_container: structure containing ring performance data
Greg Rose92915f72010-01-09 02:24:10 +00001068 *
1069 * Stores a new ITR value based on packets and byte
1070 * counts during the last interrupt. The advantage of per interrupt
1071 * computation is faster updates and more accurate ITR for the current
1072 * traffic pattern. Constants in this function were computed
1073 * based on theoretical maximum wire speed and thresholds were set based
1074 * on testing data as well as attempting to minimize response time
1075 * while increasing bulk throughput.
1076 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001077static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1078 struct ixgbevf_ring_container *ring_container)
Greg Rose92915f72010-01-09 02:24:10 +00001079{
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001080 int bytes = ring_container->total_bytes;
1081 int packets = ring_container->total_packets;
Greg Rose92915f72010-01-09 02:24:10 +00001082 u32 timepassed_us;
1083 u64 bytes_perint;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001084 u8 itr_setting = ring_container->itr;
Greg Rose92915f72010-01-09 02:24:10 +00001085
1086 if (packets == 0)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001087 return;
Greg Rose92915f72010-01-09 02:24:10 +00001088
1089 /* simple throttlerate management
1090 * 0-20MB/s lowest (100000 ints/s)
1091 * 20-100MB/s low (20000 ints/s)
1092 * 100-1249MB/s bulk (8000 ints/s)
1093 */
1094 /* what was last interrupt timeslice? */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001095 timepassed_us = q_vector->itr >> 2;
Greg Rose92915f72010-01-09 02:24:10 +00001096 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1097
1098 switch (itr_setting) {
1099 case lowest_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001100 if (bytes_perint > 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001101 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001102 break;
1103 case low_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001104 if (bytes_perint > 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001105 itr_setting = bulk_latency;
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001106 else if (bytes_perint <= 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001107 itr_setting = lowest_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001108 break;
1109 case bulk_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +00001110 if (bytes_perint <= 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001111 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +00001112 break;
1113 }
1114
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001115 /* clear work counters since we have the values we need */
1116 ring_container->total_bytes = 0;
1117 ring_container->total_packets = 0;
1118
1119 /* write updated itr to ring container */
1120 ring_container->itr = itr_setting;
Greg Rose92915f72010-01-09 02:24:10 +00001121}
1122
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001123static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
Greg Rose92915f72010-01-09 02:24:10 +00001124{
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001125 u32 new_itr = q_vector->itr;
1126 u8 current_itr;
Greg Rose92915f72010-01-09 02:24:10 +00001127
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001128 ixgbevf_update_itr(q_vector, &q_vector->tx);
1129 ixgbevf_update_itr(q_vector, &q_vector->rx);
Greg Rose92915f72010-01-09 02:24:10 +00001130
Alexander Duyck6b43c442012-05-11 08:32:45 +00001131 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Greg Rose92915f72010-01-09 02:24:10 +00001132
1133 switch (current_itr) {
1134 /* counts and packets in update_itr are dependent on these numbers */
1135 case lowest_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001136 new_itr = IXGBE_100K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001137 break;
1138 case low_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001139 new_itr = IXGBE_20K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001140 break;
1141 case bulk_latency:
1142 default:
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001143 new_itr = IXGBE_8K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +00001144 break;
1145 }
1146
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001147 if (new_itr != q_vector->itr) {
Greg Rose92915f72010-01-09 02:24:10 +00001148 /* do an exponential smoothing */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001149 new_itr = (10 * new_itr * q_vector->itr) /
1150 ((9 * new_itr) + q_vector->itr);
1151
1152 /* save the algorithm value here */
1153 q_vector->itr = new_itr;
1154
1155 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001156 }
Greg Rose92915f72010-01-09 02:24:10 +00001157}
1158
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001159static irqreturn_t ixgbevf_msix_other(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +00001160{
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001161 struct ixgbevf_adapter *adapter = data;
Greg Rose92915f72010-01-09 02:24:10 +00001162 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001163
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001164 hw->mac.get_link_status = 1;
Greg Rose375b27c2012-01-18 22:13:31 +00001165
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00001166 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1167 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Don Skidmorec7bb4172013-10-01 04:33:49 -07001168 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose3a2c4032012-02-01 01:28:15 +00001169
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001170 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1171
Greg Rose92915f72010-01-09 02:24:10 +00001172 return IRQ_HANDLED;
1173}
1174
Greg Rose92915f72010-01-09 02:24:10 +00001175/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001176 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
Greg Rose92915f72010-01-09 02:24:10 +00001177 * @irq: unused
1178 * @data: pointer to our q_vector struct for this interrupt vector
1179 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001180static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +00001181{
1182 struct ixgbevf_q_vector *q_vector = data;
Greg Rose92915f72010-01-09 02:24:10 +00001183
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001184 /* EIAM disabled interrupts (on this vector) for us */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001185 if (q_vector->rx.ring || q_vector->tx.ring)
1186 napi_schedule(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001187
1188 return IRQ_HANDLED;
1189}
1190
1191static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
1192 int r_idx)
1193{
1194 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1195
Don Skidmore87e70ab2014-01-16 02:30:08 -08001196 a->rx_ring[r_idx]->next = q_vector->rx.ring;
1197 q_vector->rx.ring = a->rx_ring[r_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001198 q_vector->rx.count++;
Greg Rose92915f72010-01-09 02:24:10 +00001199}
1200
1201static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
1202 int t_idx)
1203{
1204 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1205
Don Skidmore87e70ab2014-01-16 02:30:08 -08001206 a->tx_ring[t_idx]->next = q_vector->tx.ring;
1207 q_vector->tx.ring = a->tx_ring[t_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001208 q_vector->tx.count++;
Greg Rose92915f72010-01-09 02:24:10 +00001209}
1210
1211/**
1212 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
1213 * @adapter: board private structure to initialize
1214 *
1215 * This function maps descriptor rings to the queue-specific vectors
1216 * we were allotted through the MSI-X enabling code. Ideally, we'd have
1217 * one vector per ring/queue, but on a constrained vector budget, we
1218 * group the rings as "efficiently" as possible. You would add new
1219 * mapping configurations in here.
1220 **/
1221static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
1222{
1223 int q_vectors;
1224 int v_start = 0;
1225 int rxr_idx = 0, txr_idx = 0;
1226 int rxr_remaining = adapter->num_rx_queues;
1227 int txr_remaining = adapter->num_tx_queues;
1228 int i, j;
1229 int rqpv, tqpv;
1230 int err = 0;
1231
1232 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1233
1234 /*
1235 * The ideal configuration...
1236 * We have enough vectors to map one per queue.
1237 */
1238 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1239 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1240 map_vector_to_rxq(adapter, v_start, rxr_idx);
1241
1242 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1243 map_vector_to_txq(adapter, v_start, txr_idx);
1244 goto out;
1245 }
1246
1247 /*
1248 * If we don't have enough vectors for a 1-to-1
1249 * mapping, we'll have to group them so there are
1250 * multiple queues per vector.
1251 */
1252 /* Re-adjusting *qpv takes care of the remainder. */
1253 for (i = v_start; i < q_vectors; i++) {
1254 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1255 for (j = 0; j < rqpv; j++) {
1256 map_vector_to_rxq(adapter, i, rxr_idx);
1257 rxr_idx++;
1258 rxr_remaining--;
1259 }
1260 }
1261 for (i = v_start; i < q_vectors; i++) {
1262 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1263 for (j = 0; j < tqpv; j++) {
1264 map_vector_to_txq(adapter, i, txr_idx);
1265 txr_idx++;
1266 txr_remaining--;
1267 }
1268 }
1269
1270out:
1271 return err;
1272}
1273
1274/**
1275 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1276 * @adapter: board private structure
1277 *
1278 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1279 * interrupts from the kernel.
1280 **/
1281static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1282{
1283 struct net_device *netdev = adapter->netdev;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001284 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1285 int vector, err;
Greg Rose92915f72010-01-09 02:24:10 +00001286 int ri = 0, ti = 0;
1287
Greg Rose92915f72010-01-09 02:24:10 +00001288 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001289 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1290 struct msix_entry *entry = &adapter->msix_entries[vector];
Greg Rose92915f72010-01-09 02:24:10 +00001291
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001292 if (q_vector->tx.ring && q_vector->rx.ring) {
1293 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1294 "%s-%s-%d", netdev->name, "TxRx", ri++);
1295 ti++;
1296 } else if (q_vector->rx.ring) {
1297 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1298 "%s-%s-%d", netdev->name, "rx", ri++);
1299 } else if (q_vector->tx.ring) {
1300 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1301 "%s-%s-%d", netdev->name, "tx", ti++);
Greg Rose92915f72010-01-09 02:24:10 +00001302 } else {
1303 /* skip this unused q_vector */
1304 continue;
1305 }
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001306 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1307 q_vector->name, q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001308 if (err) {
1309 hw_dbg(&adapter->hw,
1310 "request_irq failed for MSIX interrupt "
1311 "Error: %d\n", err);
1312 goto free_queue_irqs;
1313 }
1314 }
1315
Greg Rose92915f72010-01-09 02:24:10 +00001316 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001317 &ixgbevf_msix_other, 0, netdev->name, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001318 if (err) {
1319 hw_dbg(&adapter->hw,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001320 "request_irq for msix_other failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00001321 goto free_queue_irqs;
1322 }
1323
1324 return 0;
1325
1326free_queue_irqs:
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001327 while (vector) {
1328 vector--;
1329 free_irq(adapter->msix_entries[vector].vector,
1330 adapter->q_vector[vector]);
1331 }
xunleera1f6c6b2013-03-05 07:44:20 +00001332 /* This failure is non-recoverable - it indicates the system is
1333 * out of MSIX vector resources and the VF driver cannot run
1334 * without them. Set the number of msix vectors to zero
1335 * indicating that not enough can be allocated. The error
1336 * will be returned to the user indicating device open failed.
1337 * Any further attempts to force the driver to open will also
1338 * fail. The only way to recover is to unload the driver and
1339 * reload it again. If the system has recovered some MSIX
1340 * vectors then it may succeed.
1341 */
1342 adapter->num_msix_vectors = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001343 return err;
1344}
1345
1346static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1347{
1348 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1349
1350 for (i = 0; i < q_vectors; i++) {
1351 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001352 q_vector->rx.ring = NULL;
1353 q_vector->tx.ring = NULL;
1354 q_vector->rx.count = 0;
1355 q_vector->tx.count = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001356 }
1357}
1358
1359/**
1360 * ixgbevf_request_irq - initialize interrupts
1361 * @adapter: board private structure
1362 *
1363 * Attempts to configure interrupts using the best available
1364 * capabilities of the hardware and kernel.
1365 **/
1366static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1367{
1368 int err = 0;
1369
1370 err = ixgbevf_request_msix_irqs(adapter);
1371
1372 if (err)
1373 hw_dbg(&adapter->hw,
1374 "request_irq failed, Error %d\n", err);
1375
1376 return err;
1377}
1378
1379static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1380{
Greg Rose92915f72010-01-09 02:24:10 +00001381 int i, q_vectors;
1382
1383 q_vectors = adapter->num_msix_vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001384 i = q_vectors - 1;
1385
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001386 free_irq(adapter->msix_entries[i].vector, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001387 i--;
1388
1389 for (; i >= 0; i--) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001390 /* free only the irqs that were actually requested */
1391 if (!adapter->q_vector[i]->rx.ring &&
1392 !adapter->q_vector[i]->tx.ring)
1393 continue;
1394
Greg Rose92915f72010-01-09 02:24:10 +00001395 free_irq(adapter->msix_entries[i].vector,
1396 adapter->q_vector[i]);
1397 }
1398
1399 ixgbevf_reset_q_vectors(adapter);
1400}
1401
1402/**
1403 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1404 * @adapter: board private structure
1405 **/
1406static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1407{
Greg Rose92915f72010-01-09 02:24:10 +00001408 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001409 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001410
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001411 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001412 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001413 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001414
1415 IXGBE_WRITE_FLUSH(hw);
1416
1417 for (i = 0; i < adapter->num_msix_vectors; i++)
1418 synchronize_irq(adapter->msix_entries[i].vector);
1419}
1420
1421/**
1422 * ixgbevf_irq_enable - Enable default interrupt generation settings
1423 * @adapter: board private structure
1424 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001425static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001426{
1427 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001428
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001429 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1430 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1431 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
Greg Rose92915f72010-01-09 02:24:10 +00001432}
1433
1434/**
Don Skidmorede02dec2014-01-16 02:30:09 -08001435 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1436 * @adapter: board private structure
1437 * @ring: structure containing ring specific data
1438 *
1439 * Configure the Tx descriptor ring after a reset.
1440 **/
1441static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1442 struct ixgbevf_ring *ring)
1443{
1444 struct ixgbe_hw *hw = &adapter->hw;
1445 u64 tdba = ring->dma;
1446 int wait_loop = 10;
1447 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1448 u8 reg_idx = ring->reg_idx;
1449
1450 /* disable queue to avoid issues while updating state */
1451 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1452 IXGBE_WRITE_FLUSH(hw);
1453
1454 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1455 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1456 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1457 ring->count * sizeof(union ixgbe_adv_tx_desc));
1458
1459 /* disable head writeback */
1460 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1461 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1462
1463 /* enable relaxed ordering */
1464 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1465 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1466 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1467
1468 /* reset head and tail pointers */
1469 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1470 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001471 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001472
1473 /* reset ntu and ntc to place SW in sync with hardwdare */
1474 ring->next_to_clean = 0;
1475 ring->next_to_use = 0;
1476
1477 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1478 * to or less than the number of on chip descriptors, which is
1479 * currently 40.
1480 */
1481 txdctl |= (8 << 16); /* WTHRESH = 8 */
1482
1483 /* Setting PTHRESH to 32 both improves performance */
1484 txdctl |= (1 << 8) | /* HTHRESH = 1 */
1485 32; /* PTHRESH = 32 */
1486
1487 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1488
1489 /* poll to verify queue is enabled */
1490 do {
1491 usleep_range(1000, 2000);
1492 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1493 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1494 if (!wait_loop)
1495 pr_err("Could not enable Tx Queue %d\n", reg_idx);
1496}
1497
1498/**
Greg Rose92915f72010-01-09 02:24:10 +00001499 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1500 * @adapter: board private structure
1501 *
1502 * Configure the Tx unit of the MAC after a reset.
1503 **/
1504static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1505{
Don Skidmorede02dec2014-01-16 02:30:09 -08001506 u32 i;
Greg Rose92915f72010-01-09 02:24:10 +00001507
1508 /* Setup the HW Tx Head and Tail descriptor pointers */
Don Skidmorede02dec2014-01-16 02:30:09 -08001509 for (i = 0; i < adapter->num_tx_queues; i++)
1510 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001511}
1512
1513#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1514
1515static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1516{
Greg Rose92915f72010-01-09 02:24:10 +00001517 struct ixgbe_hw *hw = &adapter->hw;
1518 u32 srrctl;
1519
Greg Rose92915f72010-01-09 02:24:10 +00001520 srrctl = IXGBE_SRRCTL_DROP_EN;
1521
Emil Tantilovbad17232014-11-21 02:57:15 +00001522 srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1523 srrctl |= IXGBEVF_RX_BUFSZ >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001524 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
Greg Rose92915f72010-01-09 02:24:10 +00001525
Greg Rose92915f72010-01-09 02:24:10 +00001526 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1527}
1528
Don Skidmore1bb9c632013-09-21 01:57:33 +00001529static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1530{
1531 struct ixgbe_hw *hw = &adapter->hw;
1532
1533 /* PSRTYPE must be initialized in 82599 */
1534 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1535 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1536 IXGBE_PSRTYPE_L2HDR;
1537
1538 if (adapter->num_rx_queues > 1)
1539 psrtype |= 1 << 29;
1540
1541 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1542}
1543
Don Skidmorede02dec2014-01-16 02:30:09 -08001544#define IXGBEVF_MAX_RX_DESC_POLL 10
1545static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1546 struct ixgbevf_ring *ring)
1547{
1548 struct ixgbe_hw *hw = &adapter->hw;
1549 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1550 u32 rxdctl;
1551 u8 reg_idx = ring->reg_idx;
1552
Mark Rustad26597802014-03-04 03:02:45 +00001553 if (IXGBE_REMOVED(hw->hw_addr))
1554 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001555 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1556 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1557
1558 /* write value back with RXDCTL.ENABLE bit cleared */
1559 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1560
1561 /* the hardware may take up to 100us to really disable the rx queue */
1562 do {
1563 udelay(10);
1564 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1565 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1566
1567 if (!wait_loop)
1568 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1569 reg_idx);
1570}
1571
1572static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1573 struct ixgbevf_ring *ring)
1574{
1575 struct ixgbe_hw *hw = &adapter->hw;
1576 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1577 u32 rxdctl;
1578 u8 reg_idx = ring->reg_idx;
1579
Mark Rustad26597802014-03-04 03:02:45 +00001580 if (IXGBE_REMOVED(hw->hw_addr))
1581 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001582 do {
1583 usleep_range(1000, 2000);
1584 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1585 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1586
1587 if (!wait_loop)
1588 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1589 reg_idx);
1590}
1591
1592static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1593 struct ixgbevf_ring *ring)
1594{
1595 struct ixgbe_hw *hw = &adapter->hw;
1596 u64 rdba = ring->dma;
1597 u32 rxdctl;
1598 u8 reg_idx = ring->reg_idx;
1599
1600 /* disable queue to avoid issues while updating state */
1601 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1602 ixgbevf_disable_rx_queue(adapter, ring);
1603
1604 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1605 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1606 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1607 ring->count * sizeof(union ixgbe_adv_rx_desc));
1608
1609 /* enable relaxed ordering */
1610 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1611 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1612
1613 /* reset head and tail pointers */
1614 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1615 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001616 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001617
1618 /* reset ntu and ntc to place SW in sync with hardwdare */
1619 ring->next_to_clean = 0;
1620 ring->next_to_use = 0;
Emil Tantilovbad17232014-11-21 02:57:15 +00001621 ring->next_to_alloc = 0;
Don Skidmorede02dec2014-01-16 02:30:09 -08001622
1623 ixgbevf_configure_srrctl(adapter, reg_idx);
1624
Emil Tantilovbad17232014-11-21 02:57:15 +00001625 /* allow any size packet since we can handle overflow */
1626 rxdctl &= ~IXGBE_RXDCTL_RLPML_EN;
1627
Don Skidmorede02dec2014-01-16 02:30:09 -08001628 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1629 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1630
1631 ixgbevf_rx_desc_queue_enable(adapter, ring);
Emil Tantilov095e2612014-01-17 18:30:00 -08001632 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
Don Skidmorede02dec2014-01-16 02:30:09 -08001633}
1634
Greg Rose92915f72010-01-09 02:24:10 +00001635/**
1636 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1637 * @adapter: board private structure
1638 *
1639 * Configure the Rx unit of the MAC after a reset.
1640 **/
1641static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1642{
Don Skidmorede02dec2014-01-16 02:30:09 -08001643 int i;
Emil Tantilovbad17232014-11-21 02:57:15 +00001644 struct ixgbe_hw *hw = &adapter->hw;
1645 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00001646
Don Skidmore1bb9c632013-09-21 01:57:33 +00001647 ixgbevf_setup_psrtype(adapter);
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001648
Emil Tantilovbad17232014-11-21 02:57:15 +00001649 /* notify the PF of our intent to use this size of frame */
1650 ixgbevf_rlpml_set_vf(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
Greg Rose92915f72010-01-09 02:24:10 +00001651
Greg Rose92915f72010-01-09 02:24:10 +00001652 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1653 * the Base and Length of the Rx Descriptor Ring */
Don Skidmorede02dec2014-01-16 02:30:09 -08001654 for (i = 0; i < adapter->num_rx_queues; i++)
1655 ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001656}
1657
Patrick McHardy80d5c362013-04-19 02:04:28 +00001658static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1659 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001660{
1661 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1662 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001663 int err;
1664
John Fastabend55fdd45b2012-10-01 14:52:20 +00001665 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001666
Greg Rose92915f72010-01-09 02:24:10 +00001667 /* add VID to filter table */
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001668 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001669
John Fastabend55fdd45b2012-10-01 14:52:20 +00001670 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001671
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001672 /* translate error return types so error makes sense */
1673 if (err == IXGBE_ERR_MBX)
1674 return -EIO;
1675
1676 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1677 return -EACCES;
1678
Jiri Pirkodadcd652011-07-21 03:25:09 +00001679 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001680
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001681 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001682}
1683
Patrick McHardy80d5c362013-04-19 02:04:28 +00001684static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1685 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001686{
1687 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1688 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001689 int err = -EOPNOTSUPP;
Greg Rose92915f72010-01-09 02:24:10 +00001690
John Fastabend55fdd45b2012-10-01 14:52:20 +00001691 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001692
Greg Rose92915f72010-01-09 02:24:10 +00001693 /* remove VID from filter table */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001694 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001695
John Fastabend55fdd45b2012-10-01 14:52:20 +00001696 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001697
Jiri Pirkodadcd652011-07-21 03:25:09 +00001698 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001699
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001700 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001701}
1702
1703static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1704{
Jiri Pirkodadcd652011-07-21 03:25:09 +00001705 u16 vid;
Greg Rose92915f72010-01-09 02:24:10 +00001706
Jiri Pirkodadcd652011-07-21 03:25:09 +00001707 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001708 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1709 htons(ETH_P_8021Q), vid);
Greg Rose92915f72010-01-09 02:24:10 +00001710}
1711
Greg Rose46ec20f2011-05-13 01:33:42 +00001712static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1713{
1714 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1715 struct ixgbe_hw *hw = &adapter->hw;
1716 int count = 0;
1717
1718 if ((netdev_uc_count(netdev)) > 10) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001719 pr_err("Too many unicast filters - No Space\n");
Greg Rose46ec20f2011-05-13 01:33:42 +00001720 return -ENOSPC;
1721 }
1722
1723 if (!netdev_uc_empty(netdev)) {
1724 struct netdev_hw_addr *ha;
1725 netdev_for_each_uc_addr(ha, netdev) {
1726 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1727 udelay(200);
1728 }
1729 } else {
1730 /*
1731 * If the list is empty then send message to PF driver to
1732 * clear all macvlans on this VF.
1733 */
1734 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1735 }
1736
1737 return count;
1738}
1739
Greg Rose92915f72010-01-09 02:24:10 +00001740/**
Greg Rosedee847f2012-11-02 05:50:57 +00001741 * ixgbevf_set_rx_mode - Multicast and unicast set
Greg Rose92915f72010-01-09 02:24:10 +00001742 * @netdev: network interface device structure
1743 *
1744 * The set_rx_method entry point is called whenever the multicast address
Greg Rosedee847f2012-11-02 05:50:57 +00001745 * list, unicast address list or the network interface flags are updated.
1746 * This routine is responsible for configuring the hardware for proper
1747 * multicast mode and configuring requested unicast filters.
Greg Rose92915f72010-01-09 02:24:10 +00001748 **/
1749static void ixgbevf_set_rx_mode(struct net_device *netdev)
1750{
1751 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1752 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001753
John Fastabend55fdd45b2012-10-01 14:52:20 +00001754 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001755
Greg Rose92915f72010-01-09 02:24:10 +00001756 /* reprogram multicast list */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001757 hw->mac.ops.update_mc_addr_list(hw, netdev);
Greg Rose46ec20f2011-05-13 01:33:42 +00001758
1759 ixgbevf_write_uc_addr_list(netdev);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001760
John Fastabend55fdd45b2012-10-01 14:52:20 +00001761 spin_unlock_bh(&adapter->mbx_lock);
Greg Rose92915f72010-01-09 02:24:10 +00001762}
1763
1764static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1765{
1766 int q_idx;
1767 struct ixgbevf_q_vector *q_vector;
1768 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1769
1770 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Greg Rose92915f72010-01-09 02:24:10 +00001771 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001772#ifdef CONFIG_NET_RX_BUSY_POLL
1773 ixgbevf_qv_init_lock(adapter->q_vector[q_idx]);
1774#endif
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001775 napi_enable(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001776 }
1777}
1778
1779static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1780{
1781 int q_idx;
1782 struct ixgbevf_q_vector *q_vector;
1783 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1784
1785 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1786 q_vector = adapter->q_vector[q_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001787 napi_disable(&q_vector->napi);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001788#ifdef CONFIG_NET_RX_BUSY_POLL
1789 while (!ixgbevf_qv_disable(adapter->q_vector[q_idx])) {
1790 pr_info("QV %d locked\n", q_idx);
1791 usleep_range(1000, 20000);
1792 }
1793#endif /* CONFIG_NET_RX_BUSY_POLL */
Greg Rose92915f72010-01-09 02:24:10 +00001794 }
1795}
1796
Don Skidmore220fe052013-09-21 01:40:49 +00001797static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
1798{
1799 struct ixgbe_hw *hw = &adapter->hw;
1800 unsigned int def_q = 0;
1801 unsigned int num_tcs = 0;
1802 unsigned int num_rx_queues = 1;
1803 int err;
1804
1805 spin_lock_bh(&adapter->mbx_lock);
1806
1807 /* fetch queue configuration from the PF */
1808 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1809
1810 spin_unlock_bh(&adapter->mbx_lock);
1811
1812 if (err)
1813 return err;
1814
1815 if (num_tcs > 1) {
1816 /* update default Tx ring register index */
Don Skidmore87e70ab2014-01-16 02:30:08 -08001817 adapter->tx_ring[0]->reg_idx = def_q;
Don Skidmore220fe052013-09-21 01:40:49 +00001818
1819 /* we need as many queues as traffic classes */
1820 num_rx_queues = num_tcs;
1821 }
1822
1823 /* if we have a bad config abort request queue reset */
1824 if (adapter->num_rx_queues != num_rx_queues) {
1825 /* force mailbox timeout to prevent further messages */
1826 hw->mbx.timeout = 0;
1827
1828 /* wait for watchdog to come around and bail us out */
1829 adapter->flags |= IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
1830 }
1831
1832 return 0;
1833}
1834
Greg Rose92915f72010-01-09 02:24:10 +00001835static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1836{
Don Skidmore220fe052013-09-21 01:40:49 +00001837 ixgbevf_configure_dcb(adapter);
1838
Don Skidmorede02dec2014-01-16 02:30:09 -08001839 ixgbevf_set_rx_mode(adapter->netdev);
Greg Rose92915f72010-01-09 02:24:10 +00001840
1841 ixgbevf_restore_vlan(adapter);
1842
1843 ixgbevf_configure_tx(adapter);
1844 ixgbevf_configure_rx(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001845}
1846
Greg Rose33bd9f62010-03-19 02:59:52 +00001847static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1848{
1849 /* Only save pre-reset stats if there are some */
1850 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1851 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1852 adapter->stats.base_vfgprc;
1853 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1854 adapter->stats.base_vfgptc;
1855 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1856 adapter->stats.base_vfgorc;
1857 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1858 adapter->stats.base_vfgotc;
1859 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1860 adapter->stats.base_vfmprc;
1861 }
1862}
1863
1864static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1865{
1866 struct ixgbe_hw *hw = &adapter->hw;
1867
1868 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1869 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1870 adapter->stats.last_vfgorc |=
1871 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1872 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1873 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1874 adapter->stats.last_vfgotc |=
1875 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1876 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1877
1878 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1879 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1880 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1881 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1882 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1883}
1884
Alexander Duyck31186782012-07-20 08:09:58 +00001885static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1886{
1887 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck56e94092012-07-20 08:10:03 +00001888 int api[] = { ixgbe_mbox_api_11,
1889 ixgbe_mbox_api_10,
Alexander Duyck31186782012-07-20 08:09:58 +00001890 ixgbe_mbox_api_unknown };
1891 int err = 0, idx = 0;
1892
John Fastabend55fdd45b2012-10-01 14:52:20 +00001893 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001894
1895 while (api[idx] != ixgbe_mbox_api_unknown) {
1896 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1897 if (!err)
1898 break;
1899 idx++;
1900 }
1901
John Fastabend55fdd45b2012-10-01 14:52:20 +00001902 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001903}
1904
Greg Rose795180d2012-04-17 04:29:34 +00001905static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001906{
1907 struct net_device *netdev = adapter->netdev;
1908 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001909
1910 ixgbevf_configure_msix(adapter);
1911
John Fastabend55fdd45b2012-10-01 14:52:20 +00001912 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001913
Greg Rose92fe0bf2012-11-02 05:50:47 +00001914 if (is_valid_ether_addr(hw->mac.addr))
1915 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1916 else
1917 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001918
John Fastabend55fdd45b2012-10-01 14:52:20 +00001919 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001920
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001921 smp_mb__before_atomic();
Greg Rose92915f72010-01-09 02:24:10 +00001922 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1923 ixgbevf_napi_enable_all(adapter);
1924
1925 /* enable transmits */
1926 netif_tx_start_all_queues(netdev);
1927
Greg Rose33bd9f62010-03-19 02:59:52 +00001928 ixgbevf_save_reset_stats(adapter);
1929 ixgbevf_init_last_counter_stats(adapter);
1930
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001931 hw->mac.get_link_status = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001932 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose92915f72010-01-09 02:24:10 +00001933}
1934
Greg Rose795180d2012-04-17 04:29:34 +00001935void ixgbevf_up(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001936{
Greg Rose92915f72010-01-09 02:24:10 +00001937 struct ixgbe_hw *hw = &adapter->hw;
1938
1939 ixgbevf_configure(adapter);
1940
Greg Rose795180d2012-04-17 04:29:34 +00001941 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001942
1943 /* clear any pending interrupts, may auto mask */
1944 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1945
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001946 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001947}
1948
1949/**
1950 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
Greg Rose92915f72010-01-09 02:24:10 +00001951 * @rx_ring: ring to free buffers from
1952 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001953static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001954{
Emil Tantilovbad17232014-11-21 02:57:15 +00001955 struct device *dev = rx_ring->dev;
Greg Rose92915f72010-01-09 02:24:10 +00001956 unsigned long size;
1957 unsigned int i;
1958
Emil Tantilovbad17232014-11-21 02:57:15 +00001959 /* Free Rx ring sk_buff */
1960 if (rx_ring->skb) {
1961 dev_kfree_skb(rx_ring->skb);
1962 rx_ring->skb = NULL;
1963 }
1964
1965 /* ring already cleared, nothing to do */
Greg Rosec0456c22010-01-22 22:47:18 +00001966 if (!rx_ring->rx_buffer_info)
1967 return;
Greg Rose92915f72010-01-09 02:24:10 +00001968
Emil Tantilovbad17232014-11-21 02:57:15 +00001969 /* Free all the Rx ring pages */
Greg Rose92915f72010-01-09 02:24:10 +00001970 for (i = 0; i < rx_ring->count; i++) {
Emil Tantilovbad17232014-11-21 02:57:15 +00001971 struct ixgbevf_rx_buffer *rx_buffer;
Greg Rose92915f72010-01-09 02:24:10 +00001972
Emil Tantilovbad17232014-11-21 02:57:15 +00001973 rx_buffer = &rx_ring->rx_buffer_info[i];
1974 if (rx_buffer->dma)
1975 dma_unmap_page(dev, rx_buffer->dma,
1976 PAGE_SIZE, DMA_FROM_DEVICE);
1977 rx_buffer->dma = 0;
1978 if (rx_buffer->page)
1979 __free_page(rx_buffer->page);
1980 rx_buffer->page = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00001981 }
1982
1983 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1984 memset(rx_ring->rx_buffer_info, 0, size);
1985
1986 /* Zero out the descriptor ring */
1987 memset(rx_ring->desc, 0, rx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001988}
1989
1990/**
1991 * ixgbevf_clean_tx_ring - Free Tx Buffers
Greg Rose92915f72010-01-09 02:24:10 +00001992 * @tx_ring: ring to be cleaned
1993 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001994static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001995{
1996 struct ixgbevf_tx_buffer *tx_buffer_info;
1997 unsigned long size;
1998 unsigned int i;
1999
Greg Rosec0456c22010-01-22 22:47:18 +00002000 if (!tx_ring->tx_buffer_info)
2001 return;
2002
Greg Rose92915f72010-01-09 02:24:10 +00002003 /* Free all the Tx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00002004 for (i = 0; i < tx_ring->count; i++) {
2005 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00002006 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00002007 }
2008
2009 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
2010 memset(tx_ring->tx_buffer_info, 0, size);
2011
2012 memset(tx_ring->desc, 0, tx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00002013}
2014
2015/**
2016 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2017 * @adapter: board private structure
2018 **/
2019static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2020{
2021 int i;
2022
2023 for (i = 0; i < adapter->num_rx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002024 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002025}
2026
2027/**
2028 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2029 * @adapter: board private structure
2030 **/
2031static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2032{
2033 int i;
2034
2035 for (i = 0; i < adapter->num_tx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002036 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002037}
2038
2039void ixgbevf_down(struct ixgbevf_adapter *adapter)
2040{
2041 struct net_device *netdev = adapter->netdev;
2042 struct ixgbe_hw *hw = &adapter->hw;
Don Skidmorede02dec2014-01-16 02:30:09 -08002043 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002044
2045 /* signal that we are down to the interrupt handler */
Mark Rustad5b346dc2014-03-04 03:02:18 +00002046 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2047 return; /* do nothing if already down */
Don Skidmore858c3dd2013-10-01 04:33:50 -07002048
2049 /* disable all enabled rx queues */
2050 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002051 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002052
2053 netif_tx_disable(netdev);
2054
2055 msleep(10);
2056
2057 netif_tx_stop_all_queues(netdev);
2058
2059 ixgbevf_irq_disable(adapter);
2060
2061 ixgbevf_napi_disable_all(adapter);
2062
2063 del_timer_sync(&adapter->watchdog_timer);
2064 /* can't call flush scheduled work here because it can deadlock
2065 * if linkwatch_event tries to acquire the rtnl_lock which we are
2066 * holding */
2067 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
2068 msleep(1);
2069
2070 /* disable transmits in the hardware now that interrupts are off */
2071 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmorede02dec2014-01-16 02:30:09 -08002072 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2073
2074 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2075 IXGBE_TXDCTL_SWFLSH);
Greg Rose92915f72010-01-09 02:24:10 +00002076 }
2077
2078 netif_carrier_off(netdev);
2079
2080 if (!pci_channel_offline(adapter->pdev))
2081 ixgbevf_reset(adapter);
2082
2083 ixgbevf_clean_all_tx_rings(adapter);
2084 ixgbevf_clean_all_rx_rings(adapter);
2085}
2086
2087void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2088{
2089 WARN_ON(in_interrupt());
Greg Rosec0456c22010-01-22 22:47:18 +00002090
Greg Rose92915f72010-01-09 02:24:10 +00002091 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2092 msleep(1);
2093
Alexander Duyck4b2cd272012-08-02 01:16:59 +00002094 ixgbevf_down(adapter);
2095 ixgbevf_up(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002096
2097 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2098}
2099
2100void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2101{
2102 struct ixgbe_hw *hw = &adapter->hw;
2103 struct net_device *netdev = adapter->netdev;
2104
Don Skidmore798e3812013-10-01 04:33:51 -07002105 if (hw->mac.ops.reset_hw(hw)) {
Greg Rose92915f72010-01-09 02:24:10 +00002106 hw_dbg(hw, "PF still resetting\n");
Don Skidmore798e3812013-10-01 04:33:51 -07002107 } else {
Greg Rose92915f72010-01-09 02:24:10 +00002108 hw->mac.ops.init_hw(hw);
Don Skidmore798e3812013-10-01 04:33:51 -07002109 ixgbevf_negotiate_api(adapter);
2110 }
Greg Rose92915f72010-01-09 02:24:10 +00002111
2112 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2113 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
2114 netdev->addr_len);
2115 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
2116 netdev->addr_len);
2117 }
2118}
2119
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002120static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2121 int vectors)
Greg Rose92915f72010-01-09 02:24:10 +00002122{
Emil Tantilova5f93372012-11-13 04:03:17 +00002123 int vector_threshold;
Greg Rose92915f72010-01-09 02:24:10 +00002124
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002125 /* We'll want at least 2 (vector_threshold):
2126 * 1) TxQ[0] + RxQ[0] handler
2127 * 2) Other (Link Status Change, etc.)
Greg Rose92915f72010-01-09 02:24:10 +00002128 */
2129 vector_threshold = MIN_MSIX_COUNT;
2130
2131 /* The more we get, the more we will assign to Tx/Rx Cleanup
2132 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2133 * Right now, we simply care about how many we'll get; we'll
2134 * set them up later while requesting irq's.
2135 */
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002136 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2137 vector_threshold, vectors);
Greg Rose92915f72010-01-09 02:24:10 +00002138
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002139 if (vectors < 0) {
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002140 dev_err(&adapter->pdev->dev,
2141 "Unable to allocate MSI-X interrupts\n");
Greg Rose92915f72010-01-09 02:24:10 +00002142 kfree(adapter->msix_entries);
2143 adapter->msix_entries = NULL;
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002144 return vectors;
Greg Rose92915f72010-01-09 02:24:10 +00002145 }
Greg Rosedee847f2012-11-02 05:50:57 +00002146
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01002147 /* Adjust for only the vectors we'll use, which is minimum
2148 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2149 * vectors we were allocated.
2150 */
2151 adapter->num_msix_vectors = vectors;
2152
2153 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002154}
2155
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002156/**
2157 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
Greg Rose92915f72010-01-09 02:24:10 +00002158 * @adapter: board private structure to initialize
2159 *
2160 * This is the top level queue allocation routine. The order here is very
2161 * important, starting with the "most" number of features turned on at once,
2162 * and ending with the smallest set of features. This way large combinations
2163 * can be allocated if they're turned on, and smaller combinations are the
2164 * fallthrough conditions.
2165 *
2166 **/
2167static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2168{
Don Skidmore220fe052013-09-21 01:40:49 +00002169 struct ixgbe_hw *hw = &adapter->hw;
2170 unsigned int def_q = 0;
2171 unsigned int num_tcs = 0;
2172 int err;
2173
Greg Rose92915f72010-01-09 02:24:10 +00002174 /* Start with base case */
2175 adapter->num_rx_queues = 1;
2176 adapter->num_tx_queues = 1;
Don Skidmore220fe052013-09-21 01:40:49 +00002177
2178 spin_lock_bh(&adapter->mbx_lock);
2179
2180 /* fetch queue configuration from the PF */
2181 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2182
2183 spin_unlock_bh(&adapter->mbx_lock);
2184
2185 if (err)
2186 return;
2187
2188 /* we need as many queues as traffic classes */
2189 if (num_tcs > 1)
2190 adapter->num_rx_queues = num_tcs;
Greg Rose92915f72010-01-09 02:24:10 +00002191}
2192
2193/**
2194 * ixgbevf_alloc_queues - Allocate memory for all rings
2195 * @adapter: board private structure to initialize
2196 *
2197 * We allocate one ring per queue at run-time since we don't know the
2198 * number of queues at compile-time. The polling_netdev array is
2199 * intended for Multiqueue, but should work fine with a single queue.
2200 **/
2201static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
2202{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002203 struct ixgbevf_ring *ring;
2204 int rx = 0, tx = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002205
Don Skidmore87e70ab2014-01-16 02:30:08 -08002206 for (; tx < adapter->num_tx_queues; tx++) {
2207 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2208 if (!ring)
2209 goto err_allocation;
Greg Rose92915f72010-01-09 02:24:10 +00002210
Don Skidmore87e70ab2014-01-16 02:30:08 -08002211 ring->dev = &adapter->pdev->dev;
2212 ring->netdev = adapter->netdev;
2213 ring->count = adapter->tx_ring_count;
2214 ring->queue_index = tx;
2215 ring->reg_idx = tx;
Greg Rose92915f72010-01-09 02:24:10 +00002216
Don Skidmore87e70ab2014-01-16 02:30:08 -08002217 adapter->tx_ring[tx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002218 }
2219
Don Skidmore87e70ab2014-01-16 02:30:08 -08002220 for (; rx < adapter->num_rx_queues; rx++) {
2221 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2222 if (!ring)
2223 goto err_allocation;
2224
2225 ring->dev = &adapter->pdev->dev;
2226 ring->netdev = adapter->netdev;
2227
2228 ring->count = adapter->rx_ring_count;
2229 ring->queue_index = rx;
2230 ring->reg_idx = rx;
2231
2232 adapter->rx_ring[rx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002233 }
2234
2235 return 0;
2236
Don Skidmore87e70ab2014-01-16 02:30:08 -08002237err_allocation:
2238 while (tx) {
2239 kfree(adapter->tx_ring[--tx]);
2240 adapter->tx_ring[tx] = NULL;
2241 }
2242
2243 while (rx) {
2244 kfree(adapter->rx_ring[--rx]);
2245 adapter->rx_ring[rx] = NULL;
2246 }
Greg Rose92915f72010-01-09 02:24:10 +00002247 return -ENOMEM;
2248}
2249
2250/**
2251 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2252 * @adapter: board private structure to initialize
2253 *
2254 * Attempt to configure the interrupts using the best available
2255 * capabilities of the hardware and the kernel.
2256 **/
2257static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2258{
Greg Rose91e2b892012-10-03 00:57:23 +00002259 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002260 int err = 0;
2261 int vector, v_budget;
2262
2263 /*
2264 * It's easy to be greedy for MSI-X vectors, but it really
2265 * doesn't do us much good if we have a lot more vectors
2266 * than CPU's. So let's be conservative and only ask for
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002267 * (roughly) the same number of vectors as there are CPU's.
2268 * The default is to use pairs of vectors.
Greg Rose92915f72010-01-09 02:24:10 +00002269 */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002270 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2271 v_budget = min_t(int, v_budget, num_online_cpus());
2272 v_budget += NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002273
2274 /* A failure in MSI-X entry allocation isn't fatal, but it does
2275 * mean we disable MSI-X capabilities of the adapter. */
2276 adapter->msix_entries = kcalloc(v_budget,
2277 sizeof(struct msix_entry), GFP_KERNEL);
2278 if (!adapter->msix_entries) {
2279 err = -ENOMEM;
2280 goto out;
2281 }
2282
2283 for (vector = 0; vector < v_budget; vector++)
2284 adapter->msix_entries[vector].entry = vector;
2285
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002286 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
2287 if (err)
2288 goto out;
Greg Rose92915f72010-01-09 02:24:10 +00002289
Greg Rose91e2b892012-10-03 00:57:23 +00002290 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
2291 if (err)
2292 goto out;
2293
2294 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
2295
Greg Rose92915f72010-01-09 02:24:10 +00002296out:
2297 return err;
2298}
2299
2300/**
2301 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2302 * @adapter: board private structure to initialize
2303 *
2304 * We allocate one q_vector per queue interrupt. If allocation fails we
2305 * return -ENOMEM.
2306 **/
2307static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2308{
2309 int q_idx, num_q_vectors;
2310 struct ixgbevf_q_vector *q_vector;
Greg Rose92915f72010-01-09 02:24:10 +00002311
2312 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002313
2314 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2315 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2316 if (!q_vector)
2317 goto err_out;
2318 q_vector->adapter = adapter;
2319 q_vector->v_idx = q_idx;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002320 netif_napi_add(adapter->netdev, &q_vector->napi,
2321 ixgbevf_poll, 64);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002322#ifdef CONFIG_NET_RX_BUSY_POLL
2323 napi_hash_add(&q_vector->napi);
2324#endif
Greg Rose92915f72010-01-09 02:24:10 +00002325 adapter->q_vector[q_idx] = q_vector;
2326 }
2327
2328 return 0;
2329
2330err_out:
2331 while (q_idx) {
2332 q_idx--;
2333 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002334#ifdef CONFIG_NET_RX_BUSY_POLL
2335 napi_hash_del(&q_vector->napi);
2336#endif
Greg Rose92915f72010-01-09 02:24:10 +00002337 netif_napi_del(&q_vector->napi);
2338 kfree(q_vector);
2339 adapter->q_vector[q_idx] = NULL;
2340 }
2341 return -ENOMEM;
2342}
2343
2344/**
2345 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2346 * @adapter: board private structure to initialize
2347 *
2348 * This function frees the memory allocated to the q_vectors. In addition if
2349 * NAPI is enabled it will delete any references to the NAPI struct prior
2350 * to freeing the q_vector.
2351 **/
2352static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2353{
John Fastabendf4477702012-09-16 08:19:46 +00002354 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002355
2356 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2357 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2358
2359 adapter->q_vector[q_idx] = NULL;
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002360#ifdef CONFIG_NET_RX_BUSY_POLL
2361 napi_hash_del(&q_vector->napi);
2362#endif
John Fastabendf4477702012-09-16 08:19:46 +00002363 netif_napi_del(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00002364 kfree(q_vector);
2365 }
2366}
2367
2368/**
2369 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2370 * @adapter: board private structure
2371 *
2372 **/
2373static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2374{
2375 pci_disable_msix(adapter->pdev);
2376 kfree(adapter->msix_entries);
2377 adapter->msix_entries = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00002378}
2379
2380/**
2381 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2382 * @adapter: board private structure to initialize
2383 *
2384 **/
2385static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2386{
2387 int err;
2388
2389 /* Number of supported queues */
2390 ixgbevf_set_num_queues(adapter);
2391
2392 err = ixgbevf_set_interrupt_capability(adapter);
2393 if (err) {
2394 hw_dbg(&adapter->hw,
2395 "Unable to setup interrupt capabilities\n");
2396 goto err_set_interrupt;
2397 }
2398
2399 err = ixgbevf_alloc_q_vectors(adapter);
2400 if (err) {
2401 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2402 "vectors\n");
2403 goto err_alloc_q_vectors;
2404 }
2405
2406 err = ixgbevf_alloc_queues(adapter);
2407 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002408 pr_err("Unable to allocate memory for queues\n");
Greg Rose92915f72010-01-09 02:24:10 +00002409 goto err_alloc_queues;
2410 }
2411
2412 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2413 "Tx Queue count = %u\n",
2414 (adapter->num_rx_queues > 1) ? "Enabled" :
2415 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2416
2417 set_bit(__IXGBEVF_DOWN, &adapter->state);
2418
2419 return 0;
2420err_alloc_queues:
2421 ixgbevf_free_q_vectors(adapter);
2422err_alloc_q_vectors:
2423 ixgbevf_reset_interrupt_capability(adapter);
2424err_set_interrupt:
2425 return err;
2426}
2427
2428/**
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002429 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2430 * @adapter: board private structure to clear interrupt scheme on
2431 *
2432 * We go through and clear interrupt specific resources and reset the structure
2433 * to pre-load conditions
2434 **/
2435static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2436{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002437 int i;
2438
2439 for (i = 0; i < adapter->num_tx_queues; i++) {
2440 kfree(adapter->tx_ring[i]);
2441 adapter->tx_ring[i] = NULL;
2442 }
2443 for (i = 0; i < adapter->num_rx_queues; i++) {
2444 kfree(adapter->rx_ring[i]);
2445 adapter->rx_ring[i] = NULL;
2446 }
2447
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002448 adapter->num_tx_queues = 0;
2449 adapter->num_rx_queues = 0;
2450
2451 ixgbevf_free_q_vectors(adapter);
2452 ixgbevf_reset_interrupt_capability(adapter);
2453}
2454
2455/**
Greg Rose92915f72010-01-09 02:24:10 +00002456 * ixgbevf_sw_init - Initialize general software structures
2457 * (struct ixgbevf_adapter)
2458 * @adapter: board private structure to initialize
2459 *
2460 * ixgbevf_sw_init initializes the Adapter private data structure.
2461 * Fields are initialized based on PCI device information and
2462 * OS network device settings (MTU size).
2463 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002464static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00002465{
2466 struct ixgbe_hw *hw = &adapter->hw;
2467 struct pci_dev *pdev = adapter->pdev;
Greg Rosee1941a72013-02-13 03:02:05 +00002468 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002469 int err;
2470
2471 /* PCI config space info */
2472
2473 hw->vendor_id = pdev->vendor;
2474 hw->device_id = pdev->device;
Sergei Shtylyovff938e42011-02-28 11:57:33 -08002475 hw->revision_id = pdev->revision;
Greg Rose92915f72010-01-09 02:24:10 +00002476 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2477 hw->subsystem_device_id = pdev->subsystem_device;
2478
2479 hw->mbx.ops.init_params(hw);
Alexander Duyck56e94092012-07-20 08:10:03 +00002480
2481 /* assume legacy case in which PF would only give VF 2 queues */
2482 hw->mac.max_tx_queues = 2;
2483 hw->mac.max_rx_queues = 2;
2484
Don Skidmore798e3812013-10-01 04:33:51 -07002485 /* lock to protect mailbox accesses */
2486 spin_lock_init(&adapter->mbx_lock);
2487
Greg Rose92915f72010-01-09 02:24:10 +00002488 err = hw->mac.ops.reset_hw(hw);
2489 if (err) {
2490 dev_info(&pdev->dev,
Greg Rosee1941a72013-02-13 03:02:05 +00002491 "PF still in reset state. Is the PF interface up?\n");
Greg Rose92915f72010-01-09 02:24:10 +00002492 } else {
2493 err = hw->mac.ops.init_hw(hw);
2494 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002495 pr_err("init_shared_code failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00002496 goto out;
2497 }
Don Skidmore798e3812013-10-01 04:33:51 -07002498 ixgbevf_negotiate_api(adapter);
Greg Rosee1941a72013-02-13 03:02:05 +00002499 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2500 if (err)
2501 dev_info(&pdev->dev, "Error reading MAC address\n");
2502 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2503 dev_info(&pdev->dev,
2504 "MAC address not assigned by administrator.\n");
2505 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2506 }
2507
2508 if (!is_valid_ether_addr(netdev->dev_addr)) {
2509 dev_info(&pdev->dev, "Assigning random MAC address\n");
2510 eth_hw_addr_random(netdev);
2511 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00002512 }
2513
2514 /* Enable dynamic interrupt throttling rates */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002515 adapter->rx_itr_setting = 1;
2516 adapter->tx_itr_setting = 1;
Greg Rose92915f72010-01-09 02:24:10 +00002517
Greg Rose92915f72010-01-09 02:24:10 +00002518 /* set default ring sizes */
2519 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2520 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2521
Greg Rose92915f72010-01-09 02:24:10 +00002522 set_bit(__IXGBEVF_DOWN, &adapter->state);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00002523 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002524
2525out:
2526 return err;
2527}
2528
Greg Rose92915f72010-01-09 02:24:10 +00002529#define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2530 { \
2531 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2532 if (current_counter < last_counter) \
2533 counter += 0x100000000LL; \
2534 last_counter = current_counter; \
2535 counter &= 0xFFFFFFFF00000000LL; \
2536 counter |= current_counter; \
2537 }
2538
2539#define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2540 { \
2541 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2542 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2543 u64 current_counter = (current_counter_msb << 32) | \
2544 current_counter_lsb; \
2545 if (current_counter < last_counter) \
2546 counter += 0x1000000000LL; \
2547 last_counter = current_counter; \
2548 counter &= 0xFFFFFFF000000000LL; \
2549 counter |= current_counter; \
2550 }
2551/**
2552 * ixgbevf_update_stats - Update the board statistics counters.
2553 * @adapter: board private structure
2554 **/
2555void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2556{
2557 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose55fb2772012-11-06 05:53:32 +00002558 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002559
Greg Rose088245a2013-01-04 07:37:31 +00002560 if (!adapter->link_up)
2561 return;
2562
Greg Rose92915f72010-01-09 02:24:10 +00002563 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2564 adapter->stats.vfgprc);
2565 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2566 adapter->stats.vfgptc);
2567 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2568 adapter->stats.last_vfgorc,
2569 adapter->stats.vfgorc);
2570 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2571 adapter->stats.last_vfgotc,
2572 adapter->stats.vfgotc);
2573 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2574 adapter->stats.vfmprc);
Greg Rose55fb2772012-11-06 05:53:32 +00002575
2576 for (i = 0; i < adapter->num_rx_queues; i++) {
2577 adapter->hw_csum_rx_error +=
Don Skidmore87e70ab2014-01-16 02:30:08 -08002578 adapter->rx_ring[i]->hw_csum_rx_error;
Don Skidmore87e70ab2014-01-16 02:30:08 -08002579 adapter->rx_ring[i]->hw_csum_rx_error = 0;
Greg Rose55fb2772012-11-06 05:53:32 +00002580 }
Greg Rose92915f72010-01-09 02:24:10 +00002581}
2582
2583/**
2584 * ixgbevf_watchdog - Timer Call-back
2585 * @data: pointer to adapter cast into an unsigned long
2586 **/
2587static void ixgbevf_watchdog(unsigned long data)
2588{
2589 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2590 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002591 u32 eics = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002592 int i;
2593
2594 /*
2595 * Do the watchdog outside of interrupt context due to the lovely
2596 * delays that some of the newer hardware requires
2597 */
2598
2599 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2600 goto watchdog_short_circuit;
2601
2602 /* get one bit for every active tx/rx interrupt vector */
2603 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2604 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00002605 if (qv->rx.ring || qv->tx.ring)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002606 eics |= 1 << i;
Greg Rose92915f72010-01-09 02:24:10 +00002607 }
2608
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002609 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
Greg Rose92915f72010-01-09 02:24:10 +00002610
2611watchdog_short_circuit:
2612 schedule_work(&adapter->watchdog_task);
2613}
2614
2615/**
2616 * ixgbevf_tx_timeout - Respond to a Tx Hang
2617 * @netdev: network interface device structure
2618 **/
2619static void ixgbevf_tx_timeout(struct net_device *netdev)
2620{
2621 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2622
2623 /* Do the reset outside of interrupt context */
2624 schedule_work(&adapter->reset_task);
2625}
2626
2627static void ixgbevf_reset_task(struct work_struct *work)
2628{
2629 struct ixgbevf_adapter *adapter;
2630 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2631
2632 /* If we're already down or resetting, just bail */
2633 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002634 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
Greg Rose92915f72010-01-09 02:24:10 +00002635 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2636 return;
2637
2638 adapter->tx_timeout_count++;
2639
2640 ixgbevf_reinit_locked(adapter);
2641}
2642
2643/**
2644 * ixgbevf_watchdog_task - worker thread to bring link up
2645 * @work: pointer to work_struct containing our data
2646 **/
2647static void ixgbevf_watchdog_task(struct work_struct *work)
2648{
2649 struct ixgbevf_adapter *adapter = container_of(work,
2650 struct ixgbevf_adapter,
2651 watchdog_task);
2652 struct net_device *netdev = adapter->netdev;
2653 struct ixgbe_hw *hw = &adapter->hw;
2654 u32 link_speed = adapter->link_speed;
2655 bool link_up = adapter->link_up;
Greg Rose92fe0bf2012-11-02 05:50:47 +00002656 s32 need_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002657
Mark Rustad26597802014-03-04 03:02:45 +00002658 if (IXGBE_REMOVED(hw->hw_addr)) {
2659 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
2660 rtnl_lock();
2661 ixgbevf_down(adapter);
2662 rtnl_unlock();
2663 }
2664 return;
2665 }
Don Skidmore220fe052013-09-21 01:40:49 +00002666 ixgbevf_queue_reset_subtask(adapter);
2667
Greg Rose92915f72010-01-09 02:24:10 +00002668 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2669
2670 /*
2671 * Always check the link on the watchdog because we have
2672 * no LSC interrupt
2673 */
Greg Rose92fe0bf2012-11-02 05:50:47 +00002674 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002675
Greg Rose92fe0bf2012-11-02 05:50:47 +00002676 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002677
Greg Rose92fe0bf2012-11-02 05:50:47 +00002678 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002679
Greg Rose92fe0bf2012-11-02 05:50:47 +00002680 if (need_reset) {
2681 adapter->link_up = link_up;
2682 adapter->link_speed = link_speed;
2683 netif_carrier_off(netdev);
2684 netif_tx_stop_all_queues(netdev);
2685 schedule_work(&adapter->reset_task);
2686 goto pf_has_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002687 }
2688 adapter->link_up = link_up;
2689 adapter->link_speed = link_speed;
2690
2691 if (link_up) {
2692 if (!netif_carrier_ok(netdev)) {
Greg Roseb876a742013-01-19 06:40:22 +00002693 char *link_speed_string;
2694 switch (link_speed) {
2695 case IXGBE_LINK_SPEED_10GB_FULL:
2696 link_speed_string = "10 Gbps";
2697 break;
2698 case IXGBE_LINK_SPEED_1GB_FULL:
2699 link_speed_string = "1 Gbps";
2700 break;
2701 case IXGBE_LINK_SPEED_100_FULL:
2702 link_speed_string = "100 Mbps";
2703 break;
2704 default:
2705 link_speed_string = "unknown speed";
2706 break;
2707 }
Greg Rose6fe59672013-01-04 07:37:26 +00002708 dev_info(&adapter->pdev->dev,
Greg Roseb876a742013-01-19 06:40:22 +00002709 "NIC Link is Up, %s\n", link_speed_string);
Greg Rose92915f72010-01-09 02:24:10 +00002710 netif_carrier_on(netdev);
2711 netif_tx_wake_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002712 }
2713 } else {
2714 adapter->link_up = false;
2715 adapter->link_speed = 0;
2716 if (netif_carrier_ok(netdev)) {
Greg Rose6fe59672013-01-04 07:37:26 +00002717 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
Greg Rose92915f72010-01-09 02:24:10 +00002718 netif_carrier_off(netdev);
2719 netif_tx_stop_all_queues(netdev);
2720 }
2721 }
2722
Greg Rose92915f72010-01-09 02:24:10 +00002723 ixgbevf_update_stats(adapter);
2724
Greg Rose33bd9f62010-03-19 02:59:52 +00002725pf_has_reset:
Greg Rose92915f72010-01-09 02:24:10 +00002726 /* Reset the timer */
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002727 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
2728 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Greg Rose92915f72010-01-09 02:24:10 +00002729 mod_timer(&adapter->watchdog_timer,
2730 round_jiffies(jiffies + (2 * HZ)));
2731
2732 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2733}
2734
2735/**
2736 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
Greg Rose92915f72010-01-09 02:24:10 +00002737 * @tx_ring: Tx descriptor ring for a specific queue
2738 *
2739 * Free all transmit software resources
2740 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002741void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002742{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002743 ixgbevf_clean_tx_ring(tx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002744
2745 vfree(tx_ring->tx_buffer_info);
2746 tx_ring->tx_buffer_info = NULL;
2747
Don Skidmorede02dec2014-01-16 02:30:09 -08002748 /* if not set, then don't free */
2749 if (!tx_ring->desc)
2750 return;
2751
Emil Tantilov05d063a2014-01-17 18:29:59 -08002752 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002753 tx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002754
2755 tx_ring->desc = NULL;
2756}
2757
2758/**
2759 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2760 * @adapter: board private structure
2761 *
2762 * Free all transmit software resources
2763 **/
2764static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2765{
2766 int i;
2767
2768 for (i = 0; i < adapter->num_tx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002769 if (adapter->tx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002770 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002771}
2772
2773/**
2774 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002775 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2776 *
2777 * Return 0 on success, negative on failure
2778 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002779int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002780{
Greg Rose92915f72010-01-09 02:24:10 +00002781 int size;
2782
2783 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002784 tx_ring->tx_buffer_info = vzalloc(size);
Greg Rose92915f72010-01-09 02:24:10 +00002785 if (!tx_ring->tx_buffer_info)
2786 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002787
2788 /* round up to nearest 4K */
2789 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2790 tx_ring->size = ALIGN(tx_ring->size, 4096);
2791
Emil Tantilov05d063a2014-01-17 18:29:59 -08002792 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002793 &tx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002794 if (!tx_ring->desc)
2795 goto err;
2796
Greg Rose92915f72010-01-09 02:24:10 +00002797 return 0;
2798
2799err:
2800 vfree(tx_ring->tx_buffer_info);
2801 tx_ring->tx_buffer_info = NULL;
2802 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2803 "descriptor ring\n");
2804 return -ENOMEM;
2805}
2806
2807/**
2808 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2809 * @adapter: board private structure
2810 *
2811 * If this function returns with an error, then it's possible one or
2812 * more of the rings is populated (while the rest are not). It is the
2813 * callers duty to clean those orphaned rings.
2814 *
2815 * Return 0 on success, negative on failure
2816 **/
2817static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2818{
2819 int i, err = 0;
2820
2821 for (i = 0; i < adapter->num_tx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002822 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002823 if (!err)
2824 continue;
2825 hw_dbg(&adapter->hw,
2826 "Allocation for Tx Queue %u failed\n", i);
2827 break;
2828 }
2829
2830 return err;
2831}
2832
2833/**
2834 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002835 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2836 *
2837 * Returns 0 on success, negative on failure
2838 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002839int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002840{
Greg Rose92915f72010-01-09 02:24:10 +00002841 int size;
2842
2843 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002844 rx_ring->rx_buffer_info = vzalloc(size);
Joe Perchese404dec2012-01-29 12:56:23 +00002845 if (!rx_ring->rx_buffer_info)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002846 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002847
2848 /* Round up to nearest 4K */
2849 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2850 rx_ring->size = ALIGN(rx_ring->size, 4096);
2851
Emil Tantilov05d063a2014-01-17 18:29:59 -08002852 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002853 &rx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002854
Emil Tantilov05d063a2014-01-17 18:29:59 -08002855 if (!rx_ring->desc)
2856 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002857
Greg Rose92915f72010-01-09 02:24:10 +00002858 return 0;
Emil Tantilov05d063a2014-01-17 18:29:59 -08002859err:
2860 vfree(rx_ring->rx_buffer_info);
2861 rx_ring->rx_buffer_info = NULL;
2862 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
Greg Rose92915f72010-01-09 02:24:10 +00002863 return -ENOMEM;
2864}
2865
2866/**
2867 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2868 * @adapter: board private structure
2869 *
2870 * If this function returns with an error, then it's possible one or
2871 * more of the rings is populated (while the rest are not). It is the
2872 * callers duty to clean those orphaned rings.
2873 *
2874 * Return 0 on success, negative on failure
2875 **/
2876static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2877{
2878 int i, err = 0;
2879
2880 for (i = 0; i < adapter->num_rx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002881 err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002882 if (!err)
2883 continue;
2884 hw_dbg(&adapter->hw,
2885 "Allocation for Rx Queue %u failed\n", i);
2886 break;
2887 }
2888 return err;
2889}
2890
2891/**
2892 * ixgbevf_free_rx_resources - Free Rx Resources
Greg Rose92915f72010-01-09 02:24:10 +00002893 * @rx_ring: ring to clean the resources from
2894 *
2895 * Free all receive software resources
2896 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002897void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002898{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002899 ixgbevf_clean_rx_ring(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002900
2901 vfree(rx_ring->rx_buffer_info);
2902 rx_ring->rx_buffer_info = NULL;
2903
Emil Tantilov05d063a2014-01-17 18:29:59 -08002904 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002905 rx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002906
2907 rx_ring->desc = NULL;
2908}
2909
2910/**
2911 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2912 * @adapter: board private structure
2913 *
2914 * Free all receive software resources
2915 **/
2916static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2917{
2918 int i;
2919
2920 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002921 if (adapter->rx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002922 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002923}
2924
2925/**
2926 * ixgbevf_open - Called when a network interface is made active
2927 * @netdev: network interface device structure
2928 *
2929 * Returns 0 on success, negative value on failure
2930 *
2931 * The open entry point is called when a network interface is made
2932 * active by the system (IFF_UP). At this point all resources needed
2933 * for transmit and receive operations are allocated, the interrupt
2934 * handler is registered with the OS, the watchdog timer is started,
2935 * and the stack is notified that the interface is ready.
2936 **/
2937static int ixgbevf_open(struct net_device *netdev)
2938{
2939 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2940 struct ixgbe_hw *hw = &adapter->hw;
2941 int err;
2942
xunleera1f6c6b2013-03-05 07:44:20 +00002943 /* A previous failure to open the device because of a lack of
2944 * available MSIX vector resources may have reset the number
2945 * of msix vectors variable to zero. The only way to recover
2946 * is to unload/reload the driver and hope that the system has
2947 * been able to recover some MSIX vector resources.
2948 */
2949 if (!adapter->num_msix_vectors)
2950 return -ENOMEM;
2951
Greg Rose92915f72010-01-09 02:24:10 +00002952 /* disallow open during test */
2953 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2954 return -EBUSY;
2955
2956 if (hw->adapter_stopped) {
2957 ixgbevf_reset(adapter);
2958 /* if adapter is still stopped then PF isn't up and
2959 * the vf can't start. */
2960 if (hw->adapter_stopped) {
2961 err = IXGBE_ERR_MBX;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002962 pr_err("Unable to start - perhaps the PF Driver isn't "
2963 "up yet\n");
Greg Rose92915f72010-01-09 02:24:10 +00002964 goto err_setup_reset;
2965 }
2966 }
2967
2968 /* allocate transmit descriptors */
2969 err = ixgbevf_setup_all_tx_resources(adapter);
2970 if (err)
2971 goto err_setup_tx;
2972
2973 /* allocate receive descriptors */
2974 err = ixgbevf_setup_all_rx_resources(adapter);
2975 if (err)
2976 goto err_setup_rx;
2977
2978 ixgbevf_configure(adapter);
2979
2980 /*
2981 * Map the Tx/Rx rings to the vectors we were allotted.
2982 * if request_irq will be called in this function map_rings
2983 * must be called *before* up_complete
2984 */
2985 ixgbevf_map_rings_to_vectors(adapter);
2986
Greg Rose795180d2012-04-17 04:29:34 +00002987 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002988
2989 /* clear any pending interrupts, may auto mask */
2990 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2991 err = ixgbevf_request_irq(adapter);
2992 if (err)
2993 goto err_req_irq;
2994
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002995 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002996
2997 return 0;
2998
2999err_req_irq:
3000 ixgbevf_down(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003001err_setup_rx:
3002 ixgbevf_free_all_rx_resources(adapter);
3003err_setup_tx:
3004 ixgbevf_free_all_tx_resources(adapter);
3005 ixgbevf_reset(adapter);
3006
3007err_setup_reset:
3008
3009 return err;
3010}
3011
3012/**
3013 * ixgbevf_close - Disables a network interface
3014 * @netdev: network interface device structure
3015 *
3016 * Returns 0, this is not allowed to fail
3017 *
3018 * The close entry point is called when an interface is de-activated
3019 * by the OS. The hardware is still under the drivers control, but
3020 * needs to be disabled. A global MAC reset is issued to stop the
3021 * hardware, and all transmit and receive resources are freed.
3022 **/
3023static int ixgbevf_close(struct net_device *netdev)
3024{
3025 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3026
3027 ixgbevf_down(adapter);
3028 ixgbevf_free_irq(adapter);
3029
3030 ixgbevf_free_all_tx_resources(adapter);
3031 ixgbevf_free_all_rx_resources(adapter);
3032
3033 return 0;
3034}
3035
Don Skidmore220fe052013-09-21 01:40:49 +00003036static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3037{
3038 struct net_device *dev = adapter->netdev;
3039
3040 if (!(adapter->flags & IXGBEVF_FLAG_QUEUE_RESET_REQUESTED))
3041 return;
3042
3043 adapter->flags &= ~IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
3044
3045 /* if interface is down do nothing */
3046 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3047 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3048 return;
3049
3050 /* Hardware has to reinitialize queues and interrupts to
3051 * match packet buffer alignment. Unfortunately, the
3052 * hardware is not flexible enough to do this dynamically.
3053 */
3054 if (netif_running(dev))
3055 ixgbevf_close(dev);
3056
3057 ixgbevf_clear_interrupt_scheme(adapter);
3058 ixgbevf_init_interrupt_scheme(adapter);
3059
3060 if (netif_running(dev))
3061 ixgbevf_open(dev);
3062}
3063
Alexander Duyck70a10e22012-05-11 08:33:21 +00003064static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3065 u32 vlan_macip_lens, u32 type_tucmd,
3066 u32 mss_l4len_idx)
3067{
3068 struct ixgbe_adv_tx_context_desc *context_desc;
3069 u16 i = tx_ring->next_to_use;
3070
3071 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3072
3073 i++;
3074 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3075
3076 /* set bits to identify this as an advanced context descriptor */
3077 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3078
3079 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3080 context_desc->seqnum_seed = 0;
3081 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3082 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3083}
3084
3085static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003086 struct ixgbevf_tx_buffer *first,
3087 u8 *hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003088{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003089 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003090 u32 vlan_macip_lens, type_tucmd;
Greg Rose92915f72010-01-09 02:24:10 +00003091 u32 mss_l4len_idx, l4len;
Francois Romieu8f12c032014-03-30 03:14:32 +00003092 int err;
Greg Rose92915f72010-01-09 02:24:10 +00003093
Emil Tantilov01a545c2014-02-27 20:32:45 -08003094 if (skb->ip_summed != CHECKSUM_PARTIAL)
3095 return 0;
3096
Alexander Duyck70a10e22012-05-11 08:33:21 +00003097 if (!skb_is_gso(skb))
3098 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00003099
Francois Romieu8f12c032014-03-30 03:14:32 +00003100 err = skb_cow_head(skb, 0);
3101 if (err < 0)
3102 return err;
Greg Rose92915f72010-01-09 02:24:10 +00003103
Alexander Duyck70a10e22012-05-11 08:33:21 +00003104 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3105 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3106
3107 if (skb->protocol == htons(ETH_P_IP)) {
3108 struct iphdr *iph = ip_hdr(skb);
3109 iph->tot_len = 0;
3110 iph->check = 0;
3111 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
3112 iph->daddr, 0,
3113 IPPROTO_TCP,
3114 0);
3115 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003116 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3117 IXGBE_TX_FLAGS_CSUM |
3118 IXGBE_TX_FLAGS_IPV4;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003119 } else if (skb_is_gso_v6(skb)) {
3120 ipv6_hdr(skb)->payload_len = 0;
3121 tcp_hdr(skb)->check =
3122 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3123 &ipv6_hdr(skb)->daddr,
3124 0, IPPROTO_TCP, 0);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003125 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3126 IXGBE_TX_FLAGS_CSUM;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003127 }
3128
3129 /* compute header lengths */
3130 l4len = tcp_hdrlen(skb);
3131 *hdr_len += l4len;
3132 *hdr_len = skb_transport_offset(skb) + l4len;
3133
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003134 /* update gso size and bytecount with header size */
3135 first->gso_segs = skb_shinfo(skb)->gso_segs;
3136 first->bytecount += (first->gso_segs - 1) * *hdr_len;
3137
Alexander Duyck70a10e22012-05-11 08:33:21 +00003138 /* mss_l4len_id: use 1 as index for TSO */
3139 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
3140 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3141 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
3142
3143 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3144 vlan_macip_lens = skb_network_header_len(skb);
3145 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003146 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003147
3148 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3149 type_tucmd, mss_l4len_idx);
3150
3151 return 1;
Greg Rose92915f72010-01-09 02:24:10 +00003152}
3153
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003154static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3155 struct ixgbevf_tx_buffer *first)
Greg Rose92915f72010-01-09 02:24:10 +00003156{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003157 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003158 u32 vlan_macip_lens = 0;
3159 u32 mss_l4len_idx = 0;
3160 u32 type_tucmd = 0;
Greg Rose92915f72010-01-09 02:24:10 +00003161
Alexander Duyck70a10e22012-05-11 08:33:21 +00003162 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3163 u8 l4_hdr = 0;
3164 switch (skb->protocol) {
Joe Perches0933ce42014-03-13 05:19:30 +00003165 case htons(ETH_P_IP):
Alexander Duyck70a10e22012-05-11 08:33:21 +00003166 vlan_macip_lens |= skb_network_header_len(skb);
3167 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3168 l4_hdr = ip_hdr(skb)->protocol;
3169 break;
Joe Perches0933ce42014-03-13 05:19:30 +00003170 case htons(ETH_P_IPV6):
Alexander Duyck70a10e22012-05-11 08:33:21 +00003171 vlan_macip_lens |= skb_network_header_len(skb);
3172 l4_hdr = ipv6_hdr(skb)->nexthdr;
3173 break;
3174 default:
3175 if (unlikely(net_ratelimit())) {
3176 dev_warn(tx_ring->dev,
3177 "partial checksum but proto=%x!\n",
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003178 first->protocol);
Greg Rose92915f72010-01-09 02:24:10 +00003179 }
Alexander Duyck70a10e22012-05-11 08:33:21 +00003180 break;
Greg Rose92915f72010-01-09 02:24:10 +00003181 }
3182
Alexander Duyck70a10e22012-05-11 08:33:21 +00003183 switch (l4_hdr) {
3184 case IPPROTO_TCP:
3185 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
3186 mss_l4len_idx = tcp_hdrlen(skb) <<
3187 IXGBE_ADVTXD_L4LEN_SHIFT;
3188 break;
3189 case IPPROTO_SCTP:
3190 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3191 mss_l4len_idx = sizeof(struct sctphdr) <<
3192 IXGBE_ADVTXD_L4LEN_SHIFT;
3193 break;
3194 case IPPROTO_UDP:
3195 mss_l4len_idx = sizeof(struct udphdr) <<
3196 IXGBE_ADVTXD_L4LEN_SHIFT;
3197 break;
3198 default:
3199 if (unlikely(net_ratelimit())) {
3200 dev_warn(tx_ring->dev,
3201 "partial checksum but l4 proto=%x!\n",
3202 l4_hdr);
3203 }
3204 break;
3205 }
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003206
3207 /* update TX checksum flag */
3208 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003209 }
3210
Alexander Duyck70a10e22012-05-11 08:33:21 +00003211 /* vlan_macip_lens: MACLEN, VLAN tag */
3212 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003213 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003214
3215 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3216 type_tucmd, mss_l4len_idx);
Greg Rose92915f72010-01-09 02:24:10 +00003217}
3218
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003219static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3220{
3221 /* set type for advanced descriptor with frame checksum insertion */
3222 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3223 IXGBE_ADVTXD_DCMD_IFCS |
3224 IXGBE_ADVTXD_DCMD_DEXT);
3225
3226 /* set HW vlan bit if vlan is present */
3227 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3228 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3229
3230 /* set segmentation enable bits for TSO/FSO */
3231 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3232 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3233
3234 return cmd_type;
3235}
3236
3237static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3238 u32 tx_flags, unsigned int paylen)
3239{
3240 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3241
3242 /* enable L4 checksum for TSO and TX checksum offload */
3243 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3244 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3245
3246 /* enble IPv4 checksum for TSO */
3247 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3248 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3249
3250 /* use index 1 context for TSO/FSO/FCOE */
3251 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3252 olinfo_status |= cpu_to_le32(1 << IXGBE_ADVTXD_IDX_SHIFT);
3253
3254 /* Check Context must be set if Tx switch is enabled, which it
3255 * always is for case where virtual functions are running
3256 */
3257 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3258
3259 tx_desc->read.olinfo_status = olinfo_status;
3260}
3261
3262static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3263 struct ixgbevf_tx_buffer *first,
3264 const u8 hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003265{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003266 dma_addr_t dma;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003267 struct sk_buff *skb = first->skb;
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003268 struct ixgbevf_tx_buffer *tx_buffer;
3269 union ixgbe_adv_tx_desc *tx_desc;
3270 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
3271 unsigned int data_len = skb->data_len;
3272 unsigned int size = skb_headlen(skb);
3273 unsigned int paylen = skb->len - hdr_len;
3274 u32 tx_flags = first->tx_flags;
3275 __le32 cmd_type;
3276 u16 i = tx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +00003277
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003278 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +00003279
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003280 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, paylen);
3281 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
Greg Rose92915f72010-01-09 02:24:10 +00003282
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003283 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3284 if (dma_mapping_error(tx_ring->dev, dma))
3285 goto dma_error;
3286
3287 /* record length, and DMA address */
3288 dma_unmap_len_set(first, len, size);
3289 dma_unmap_addr_set(first, dma, dma);
3290
3291 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3292
3293 for (;;) {
3294 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3295 tx_desc->read.cmd_type_len =
3296 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3297
3298 i++;
3299 tx_desc++;
3300 if (i == tx_ring->count) {
3301 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3302 i = 0;
3303 }
3304
3305 dma += IXGBE_MAX_DATA_PER_TXD;
3306 size -= IXGBE_MAX_DATA_PER_TXD;
3307
3308 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3309 tx_desc->read.olinfo_status = 0;
3310 }
3311
3312 if (likely(!data_len))
3313 break;
3314
3315 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3316
3317 i++;
3318 tx_desc++;
3319 if (i == tx_ring->count) {
3320 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3321 i = 0;
3322 }
3323
3324 size = skb_frag_size(frag);
3325 data_len -= size;
3326
3327 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3328 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003329 if (dma_mapping_error(tx_ring->dev, dma))
Greg Rose92915f72010-01-09 02:24:10 +00003330 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00003331
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003332 tx_buffer = &tx_ring->tx_buffer_info[i];
3333 dma_unmap_len_set(tx_buffer, len, size);
3334 dma_unmap_addr_set(tx_buffer, dma, dma);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003335
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003336 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3337 tx_desc->read.olinfo_status = 0;
3338
3339 frag++;
Greg Rose92915f72010-01-09 02:24:10 +00003340 }
3341
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003342 /* write last descriptor with RS and EOP bits */
3343 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3344 tx_desc->read.cmd_type_len = cmd_type;
Greg Rose92915f72010-01-09 02:24:10 +00003345
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003346 /* set the timestamp */
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003347 first->time_stamp = jiffies;
Greg Rose92915f72010-01-09 02:24:10 +00003348
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003349 /* Force memory writes to complete before letting h/w know there
3350 * are new descriptors to fetch. (Only applicable for weak-ordered
3351 * memory model archs, such as IA-64).
3352 *
3353 * We also need this memory barrier (wmb) to make certain all of the
3354 * status bits have been updated before next_to_watch is written.
3355 */
3356 wmb();
Greg Rose92915f72010-01-09 02:24:10 +00003357
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003358 /* set next_to_watch value indicating a packet is present */
3359 first->next_to_watch = tx_desc;
3360
3361 i++;
3362 if (i == tx_ring->count)
3363 i = 0;
3364
3365 tx_ring->next_to_use = i;
3366
3367 /* notify HW of packet */
Mark Rustad06380db2014-03-04 03:02:23 +00003368 ixgbevf_write_tail(tx_ring, i);
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003369
3370 return;
Greg Rose92915f72010-01-09 02:24:10 +00003371dma_error:
Alexander Duyck70a10e22012-05-11 08:33:21 +00003372 dev_err(tx_ring->dev, "TX DMA map failed\n");
Greg Rose92915f72010-01-09 02:24:10 +00003373
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003374 /* clear dma mappings for failed tx_buffer_info map */
3375 for (;;) {
3376 tx_buffer = &tx_ring->tx_buffer_info[i];
3377 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer);
3378 if (tx_buffer == first)
3379 break;
3380 if (i == 0)
3381 i = tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +00003382 i--;
Greg Rose92915f72010-01-09 02:24:10 +00003383 }
3384
Greg Rose92915f72010-01-09 02:24:10 +00003385 tx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +00003386}
3387
Alexander Duyckfb401952012-05-11 08:33:16 +00003388static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003389{
Alexander Duyckfb401952012-05-11 08:33:16 +00003390 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00003391 /* Herbert's original patch had:
3392 * smp_mb__after_netif_stop_queue();
3393 * but since that doesn't exist yet, just open code it. */
3394 smp_mb();
3395
3396 /* We need to check again in a case another CPU has just
3397 * made room available. */
Don Skidmoref880d072013-10-23 02:17:52 +00003398 if (likely(ixgbevf_desc_unused(tx_ring) < size))
Greg Rose92915f72010-01-09 02:24:10 +00003399 return -EBUSY;
3400
3401 /* A reprieve! - use start_queue because it doesn't call schedule */
Alexander Duyckfb401952012-05-11 08:33:16 +00003402 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
Emil Tantilov095e2612014-01-17 18:30:00 -08003403 ++tx_ring->tx_stats.restart_queue;
3404
Greg Rose92915f72010-01-09 02:24:10 +00003405 return 0;
3406}
3407
Alexander Duyckfb401952012-05-11 08:33:16 +00003408static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003409{
Don Skidmoref880d072013-10-23 02:17:52 +00003410 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
Greg Rose92915f72010-01-09 02:24:10 +00003411 return 0;
Alexander Duyckfb401952012-05-11 08:33:16 +00003412 return __ixgbevf_maybe_stop_tx(tx_ring, size);
Greg Rose92915f72010-01-09 02:24:10 +00003413}
3414
3415static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3416{
3417 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003418 struct ixgbevf_tx_buffer *first;
Greg Rose92915f72010-01-09 02:24:10 +00003419 struct ixgbevf_ring *tx_ring;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003420 int tso;
3421 u32 tx_flags = 0;
Alexander Duyck35959902012-05-11 08:32:40 +00003422 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3423#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3424 unsigned short f;
3425#endif
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003426 u8 hdr_len = 0;
Greg Rosef9d08f162012-10-02 00:50:52 +00003427 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003428
Ben Hutchings46acc462012-11-01 09:11:11 +00003429 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
Greg Rosef9d08f162012-10-02 00:50:52 +00003430 dev_kfree_skb(skb);
3431 return NETDEV_TX_OK;
3432 }
Greg Rose92915f72010-01-09 02:24:10 +00003433
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003434 tx_ring = adapter->tx_ring[skb->queue_mapping];
Greg Rose92915f72010-01-09 02:24:10 +00003435
Alexander Duyck35959902012-05-11 08:32:40 +00003436 /*
3437 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3438 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3439 * + 2 desc gap to keep tail from touching head,
3440 * + 1 desc for context descriptor,
3441 * otherwise try next time
3442 */
3443#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3444 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3445 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3446#else
3447 count += skb_shinfo(skb)->nr_frags;
3448#endif
Alexander Duyckfb401952012-05-11 08:33:16 +00003449 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
Emil Tantilov095e2612014-01-17 18:30:00 -08003450 tx_ring->tx_stats.tx_busy++;
Alexander Duyck35959902012-05-11 08:32:40 +00003451 return NETDEV_TX_BUSY;
3452 }
3453
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003454 /* record the location of the first descriptor for this packet */
3455 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
3456 first->skb = skb;
3457 first->bytecount = skb->len;
3458 first->gso_segs = 1;
3459
Jesse Grosseab6d182010-10-20 13:56:03 +00003460 if (vlan_tx_tag_present(skb)) {
Greg Rose92915f72010-01-09 02:24:10 +00003461 tx_flags |= vlan_tx_tag_get(skb);
3462 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3463 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3464 }
3465
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003466 /* record initial flags and protocol */
3467 first->tx_flags = tx_flags;
3468 first->protocol = vlan_get_protocol(skb);
Greg Rose92915f72010-01-09 02:24:10 +00003469
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003470 tso = ixgbevf_tso(tx_ring, first, &hdr_len);
3471 if (tso < 0)
3472 goto out_drop;
Emil Tantilovb5d217f2014-02-27 20:32:44 -08003473 else if (!tso)
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003474 ixgbevf_tx_csum(tx_ring, first);
Greg Rose92915f72010-01-09 02:24:10 +00003475
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003476 ixgbevf_tx_map(tx_ring, first, hdr_len);
Greg Rose92915f72010-01-09 02:24:10 +00003477
Alexander Duyckfb401952012-05-11 08:33:16 +00003478 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Greg Rose92915f72010-01-09 02:24:10 +00003479
3480 return NETDEV_TX_OK;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003481
3482out_drop:
3483 dev_kfree_skb_any(first->skb);
3484 first->skb = NULL;
3485
3486 return NETDEV_TX_OK;
Greg Rose92915f72010-01-09 02:24:10 +00003487}
3488
3489/**
Greg Rose92915f72010-01-09 02:24:10 +00003490 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3491 * @netdev: network interface device structure
3492 * @p: pointer to an address structure
3493 *
3494 * Returns 0 on success, negative on failure
3495 **/
3496static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3497{
3498 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3499 struct ixgbe_hw *hw = &adapter->hw;
3500 struct sockaddr *addr = p;
3501
3502 if (!is_valid_ether_addr(addr->sa_data))
3503 return -EADDRNOTAVAIL;
3504
3505 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3506 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3507
John Fastabend55fdd45b2012-10-01 14:52:20 +00003508 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003509
Greg Rose92fe0bf2012-11-02 05:50:47 +00003510 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00003511
John Fastabend55fdd45b2012-10-01 14:52:20 +00003512 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003513
Greg Rose92915f72010-01-09 02:24:10 +00003514 return 0;
3515}
3516
3517/**
3518 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3519 * @netdev: network interface device structure
3520 * @new_mtu: new value for maximum frame size
3521 *
3522 * Returns 0 on success, negative on failure
3523 **/
3524static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3525{
3526 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilovbad17232014-11-21 02:57:15 +00003527 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00003528 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Greg Rose69bfbec2011-01-26 01:06:12 +00003529 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose69bfbec2011-01-26 01:06:12 +00003530
Alexander Duyck56e94092012-07-20 08:10:03 +00003531 switch (adapter->hw.api_version) {
3532 case ixgbe_mbox_api_11:
Greg Rose69bfbec2011-01-26 01:06:12 +00003533 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
Alexander Duyck56e94092012-07-20 08:10:03 +00003534 break;
3535 default:
Emil Tantilov47068b02014-11-22 07:59:56 +00003536 if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
Alexander Duyck56e94092012-07-20 08:10:03 +00003537 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3538 break;
3539 }
Greg Rose92915f72010-01-09 02:24:10 +00003540
3541 /* MTU < 68 is an error and causes problems on some kernels */
Greg Rose69bfbec2011-01-26 01:06:12 +00003542 if ((new_mtu < 68) || (max_frame > max_possible_frame))
Greg Rose92915f72010-01-09 02:24:10 +00003543 return -EINVAL;
3544
Emil Tantilovbad17232014-11-21 02:57:15 +00003545 hw_dbg(hw, "changing MTU from %d to %d\n",
Greg Rose92915f72010-01-09 02:24:10 +00003546 netdev->mtu, new_mtu);
3547 /* must set new MTU before calling down or up */
3548 netdev->mtu = new_mtu;
3549
Emil Tantilovbad17232014-11-21 02:57:15 +00003550 /* notify the PF of our intent to use this size of frame */
3551 ixgbevf_rlpml_set_vf(hw, max_frame);
Greg Rose92915f72010-01-09 02:24:10 +00003552
3553 return 0;
3554}
3555
Emil Tantilov688ff322014-11-08 01:39:56 +00003556#ifdef CONFIG_NET_POLL_CONTROLLER
3557/* Polling 'interrupt' - used by things like netconsole to send skbs
3558 * without having to re-enable interrupts. It's not called while
3559 * the interrupt routine is executing.
3560 */
3561static void ixgbevf_netpoll(struct net_device *netdev)
3562{
3563 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3564 int i;
3565
3566 /* if interface is down do nothing */
3567 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
3568 return;
3569 for (i = 0; i < adapter->num_rx_queues; i++)
3570 ixgbevf_msix_clean_rings(0, adapter->q_vector[i]);
3571}
3572#endif /* CONFIG_NET_POLL_CONTROLLER */
3573
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003574static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
Greg Rose92915f72010-01-09 02:24:10 +00003575{
3576 struct net_device *netdev = pci_get_drvdata(pdev);
3577 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003578#ifdef CONFIG_PM
3579 int retval = 0;
3580#endif
Greg Rose92915f72010-01-09 02:24:10 +00003581
3582 netif_device_detach(netdev);
3583
3584 if (netif_running(netdev)) {
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003585 rtnl_lock();
Greg Rose92915f72010-01-09 02:24:10 +00003586 ixgbevf_down(adapter);
3587 ixgbevf_free_irq(adapter);
3588 ixgbevf_free_all_tx_resources(adapter);
3589 ixgbevf_free_all_rx_resources(adapter);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003590 rtnl_unlock();
Greg Rose92915f72010-01-09 02:24:10 +00003591 }
3592
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003593 ixgbevf_clear_interrupt_scheme(adapter);
3594
3595#ifdef CONFIG_PM
3596 retval = pci_save_state(pdev);
3597 if (retval)
3598 return retval;
3599
3600#endif
Mark Rustadbc0c7152014-03-12 00:38:45 +00003601 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3602 pci_disable_device(pdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003603
3604 return 0;
3605}
3606
3607#ifdef CONFIG_PM
3608static int ixgbevf_resume(struct pci_dev *pdev)
3609{
Wei Yongjun27ae2962014-01-16 02:30:07 -08003610 struct net_device *netdev = pci_get_drvdata(pdev);
3611 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003612 u32 err;
3613
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003614 pci_restore_state(pdev);
3615 /*
3616 * pci_restore_state clears dev->state_saved so call
3617 * pci_save_state to restore it.
3618 */
Greg Rose92915f72010-01-09 02:24:10 +00003619 pci_save_state(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003620
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003621 err = pci_enable_device_mem(pdev);
3622 if (err) {
3623 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3624 return err;
3625 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003626 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003627 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003628 pci_set_master(pdev);
3629
Don Skidmore798e3812013-10-01 04:33:51 -07003630 ixgbevf_reset(adapter);
3631
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003632 rtnl_lock();
3633 err = ixgbevf_init_interrupt_scheme(adapter);
3634 rtnl_unlock();
3635 if (err) {
3636 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3637 return err;
3638 }
3639
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003640 if (netif_running(netdev)) {
3641 err = ixgbevf_open(netdev);
3642 if (err)
3643 return err;
3644 }
3645
3646 netif_device_attach(netdev);
3647
3648 return err;
3649}
3650
3651#endif /* CONFIG_PM */
3652static void ixgbevf_shutdown(struct pci_dev *pdev)
3653{
3654 ixgbevf_suspend(pdev, PMSG_SUSPEND);
Greg Rose92915f72010-01-09 02:24:10 +00003655}
3656
Eric Dumazet4197aa72011-06-22 05:01:35 +00003657static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3658 struct rtnl_link_stats64 *stats)
3659{
3660 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3661 unsigned int start;
3662 u64 bytes, packets;
3663 const struct ixgbevf_ring *ring;
3664 int i;
3665
3666 ixgbevf_update_stats(adapter);
3667
3668 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3669
3670 for (i = 0; i < adapter->num_rx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003671 ring = adapter->rx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003672 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003673 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003674 bytes = ring->stats.bytes;
3675 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003676 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003677 stats->rx_bytes += bytes;
3678 stats->rx_packets += packets;
3679 }
3680
3681 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003682 ring = adapter->tx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003683 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003684 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003685 bytes = ring->stats.bytes;
3686 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003687 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003688 stats->tx_bytes += bytes;
3689 stats->tx_packets += packets;
3690 }
3691
3692 return stats;
3693}
3694
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003695static const struct net_device_ops ixgbevf_netdev_ops = {
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003696 .ndo_open = ixgbevf_open,
3697 .ndo_stop = ixgbevf_close,
3698 .ndo_start_xmit = ixgbevf_xmit_frame,
3699 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
Eric Dumazet4197aa72011-06-22 05:01:35 +00003700 .ndo_get_stats64 = ixgbevf_get_stats,
Greg Rose92915f72010-01-09 02:24:10 +00003701 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003702 .ndo_set_mac_address = ixgbevf_set_mac,
3703 .ndo_change_mtu = ixgbevf_change_mtu,
3704 .ndo_tx_timeout = ixgbevf_tx_timeout,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003705 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3706 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
Jacob Kellerc777cdf2013-09-21 06:24:20 +00003707#ifdef CONFIG_NET_RX_BUSY_POLL
3708 .ndo_busy_poll = ixgbevf_busy_poll_recv,
3709#endif
Emil Tantilov688ff322014-11-08 01:39:56 +00003710#ifdef CONFIG_NET_POLL_CONTROLLER
3711 .ndo_poll_controller = ixgbevf_netpoll,
3712#endif
Greg Rose92915f72010-01-09 02:24:10 +00003713};
Greg Rose92915f72010-01-09 02:24:10 +00003714
3715static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3716{
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003717 dev->netdev_ops = &ixgbevf_netdev_ops;
Greg Rose92915f72010-01-09 02:24:10 +00003718 ixgbevf_set_ethtool_ops(dev);
3719 dev->watchdog_timeo = 5 * HZ;
3720}
3721
3722/**
3723 * ixgbevf_probe - Device Initialization Routine
3724 * @pdev: PCI device information struct
3725 * @ent: entry in ixgbevf_pci_tbl
3726 *
3727 * Returns 0 on success, negative on failure
3728 *
3729 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3730 * The OS initialization, configuring of the adapter private structure,
3731 * and a hardware reset occur.
3732 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003733static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Greg Rose92915f72010-01-09 02:24:10 +00003734{
3735 struct net_device *netdev;
3736 struct ixgbevf_adapter *adapter = NULL;
3737 struct ixgbe_hw *hw = NULL;
3738 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
Greg Rose92915f72010-01-09 02:24:10 +00003739 int err, pci_using_dac;
3740
3741 err = pci_enable_device(pdev);
3742 if (err)
3743 return err;
3744
Russell King53567aa2013-06-10 12:49:38 +01003745 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Greg Rose92915f72010-01-09 02:24:10 +00003746 pci_using_dac = 1;
3747 } else {
Russell King53567aa2013-06-10 12:49:38 +01003748 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003749 if (err) {
Russell King53567aa2013-06-10 12:49:38 +01003750 dev_err(&pdev->dev, "No usable DMA "
3751 "configuration, aborting\n");
3752 goto err_dma;
Greg Rose92915f72010-01-09 02:24:10 +00003753 }
3754 pci_using_dac = 0;
3755 }
3756
3757 err = pci_request_regions(pdev, ixgbevf_driver_name);
3758 if (err) {
3759 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3760 goto err_pci_reg;
3761 }
3762
3763 pci_set_master(pdev);
3764
Greg Rose92915f72010-01-09 02:24:10 +00003765 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3766 MAX_TX_QUEUES);
Greg Rose92915f72010-01-09 02:24:10 +00003767 if (!netdev) {
3768 err = -ENOMEM;
3769 goto err_alloc_etherdev;
3770 }
3771
3772 SET_NETDEV_DEV(netdev, &pdev->dev);
3773
3774 pci_set_drvdata(pdev, netdev);
3775 adapter = netdev_priv(netdev);
3776
3777 adapter->netdev = netdev;
3778 adapter->pdev = pdev;
3779 hw = &adapter->hw;
3780 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00003781 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Greg Rose92915f72010-01-09 02:24:10 +00003782
3783 /*
3784 * call save state here in standalone driver because it relies on
3785 * adapter struct to exist, and needs to call netdev_priv
3786 */
3787 pci_save_state(pdev);
3788
3789 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3790 pci_resource_len(pdev, 0));
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003791 adapter->io_addr = hw->hw_addr;
Greg Rose92915f72010-01-09 02:24:10 +00003792 if (!hw->hw_addr) {
3793 err = -EIO;
3794 goto err_ioremap;
3795 }
3796
3797 ixgbevf_assign_netdev_ops(netdev);
3798
Greg Rose92915f72010-01-09 02:24:10 +00003799 /* Setup hw api */
3800 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3801 hw->mac.type = ii->mac;
3802
3803 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
Greg Rosef416dfc2011-06-08 07:32:38 +00003804 sizeof(struct ixgbe_mbx_operations));
Greg Rose92915f72010-01-09 02:24:10 +00003805
Greg Rose92915f72010-01-09 02:24:10 +00003806 /* setup the private structure */
3807 err = ixgbevf_sw_init(adapter);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003808 if (err)
3809 goto err_sw_init;
3810
3811 /* The HW MAC address was set and/or determined in sw_init */
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003812 if (!is_valid_ether_addr(netdev->dev_addr)) {
3813 pr_err("invalid MAC address\n");
3814 err = -EIO;
3815 goto err_sw_init;
3816 }
Greg Rose92915f72010-01-09 02:24:10 +00003817
Michał Mirosław471a76d2011-06-08 08:53:03 +00003818 netdev->hw_features = NETIF_F_SG |
Greg Rose92915f72010-01-09 02:24:10 +00003819 NETIF_F_IP_CSUM |
Michał Mirosław471a76d2011-06-08 08:53:03 +00003820 NETIF_F_IPV6_CSUM |
3821 NETIF_F_TSO |
3822 NETIF_F_TSO6 |
3823 NETIF_F_RXCSUM;
3824
3825 netdev->features = netdev->hw_features |
Patrick McHardyf6469682013-04-19 02:04:27 +00003826 NETIF_F_HW_VLAN_CTAG_TX |
3827 NETIF_F_HW_VLAN_CTAG_RX |
3828 NETIF_F_HW_VLAN_CTAG_FILTER;
Greg Rose92915f72010-01-09 02:24:10 +00003829
Greg Rose92915f72010-01-09 02:24:10 +00003830 netdev->vlan_features |= NETIF_F_TSO;
3831 netdev->vlan_features |= NETIF_F_TSO6;
3832 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyck3bfacf92010-08-02 14:59:04 +00003833 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003834 netdev->vlan_features |= NETIF_F_SG;
3835
3836 if (pci_using_dac)
3837 netdev->features |= NETIF_F_HIGHDMA;
3838
Jiri Pirko01789342011-08-16 06:29:00 +00003839 netdev->priv_flags |= IFF_UNICAST_FLT;
3840
Greg Rose92915f72010-01-09 02:24:10 +00003841 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00003842 adapter->watchdog_timer.function = ixgbevf_watchdog;
Greg Rose92915f72010-01-09 02:24:10 +00003843 adapter->watchdog_timer.data = (unsigned long)adapter;
3844
Mark Rustadea699562014-03-12 00:38:51 +00003845 if (IXGBE_REMOVED(hw->hw_addr)) {
3846 err = -EIO;
3847 goto err_sw_init;
3848 }
Greg Rose92915f72010-01-09 02:24:10 +00003849 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3850 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
Mark Rustadea699562014-03-12 00:38:51 +00003851 set_bit(__IXGBEVF_WORK_INIT, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003852
3853 err = ixgbevf_init_interrupt_scheme(adapter);
3854 if (err)
3855 goto err_sw_init;
3856
Greg Rose92915f72010-01-09 02:24:10 +00003857 strcpy(netdev->name, "eth%d");
3858
3859 err = register_netdev(netdev);
3860 if (err)
3861 goto err_register;
3862
Greg Rose5d426ad2010-11-16 19:27:19 -08003863 netif_carrier_off(netdev);
3864
Greg Rose33bd9f62010-03-19 02:59:52 +00003865 ixgbevf_init_last_counter_stats(adapter);
3866
Emil Tantilov47068b02014-11-22 07:59:56 +00003867 /* print the VF info */
3868 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
3869 dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
Greg Rose92915f72010-01-09 02:24:10 +00003870
Emil Tantilov47068b02014-11-22 07:59:56 +00003871 switch (hw->mac.type) {
3872 case ixgbe_mac_X550_vf:
3873 dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
3874 break;
3875 case ixgbe_mac_X540_vf:
3876 dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
3877 break;
3878 case ixgbe_mac_82599_vf:
3879 default:
3880 dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
3881 break;
3882 }
Greg Rose92915f72010-01-09 02:24:10 +00003883
Greg Rose92915f72010-01-09 02:24:10 +00003884 return 0;
3885
3886err_register:
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003887 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003888err_sw_init:
3889 ixgbevf_reset_interrupt_capability(adapter);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003890 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003891err_ioremap:
3892 free_netdev(netdev);
3893err_alloc_etherdev:
3894 pci_release_regions(pdev);
3895err_pci_reg:
3896err_dma:
Mark Rustadbc0c7152014-03-12 00:38:45 +00003897 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3898 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003899 return err;
3900}
3901
3902/**
3903 * ixgbevf_remove - Device Removal Routine
3904 * @pdev: PCI device information struct
3905 *
3906 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3907 * that it should release a PCI device. The could be caused by a
3908 * Hot-Plug event, or because the driver is going to be removed from
3909 * memory.
3910 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003911static void ixgbevf_remove(struct pci_dev *pdev)
Greg Rose92915f72010-01-09 02:24:10 +00003912{
3913 struct net_device *netdev = pci_get_drvdata(pdev);
3914 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3915
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00003916 set_bit(__IXGBEVF_REMOVING, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003917
3918 del_timer_sync(&adapter->watchdog_timer);
3919
Tejun Heo23f333a2010-12-12 16:45:14 +01003920 cancel_work_sync(&adapter->reset_task);
Greg Rose92915f72010-01-09 02:24:10 +00003921 cancel_work_sync(&adapter->watchdog_task);
3922
Alexander Duyckfd13a9a2012-05-11 08:32:24 +00003923 if (netdev->reg_state == NETREG_REGISTERED)
Greg Rose92915f72010-01-09 02:24:10 +00003924 unregister_netdev(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00003925
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003926 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003927 ixgbevf_reset_interrupt_capability(adapter);
3928
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003929 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003930 pci_release_regions(pdev);
3931
3932 hw_dbg(&adapter->hw, "Remove complete\n");
3933
Greg Rose92915f72010-01-09 02:24:10 +00003934 free_netdev(netdev);
3935
Mark Rustadbc0c7152014-03-12 00:38:45 +00003936 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3937 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003938}
3939
Alexander Duyck9f19f312012-05-11 08:33:32 +00003940/**
3941 * ixgbevf_io_error_detected - called when PCI error is detected
3942 * @pdev: Pointer to PCI device
3943 * @state: The current pci connection state
3944 *
3945 * This function is called after a PCI bus error affecting
3946 * this device has been detected.
3947 */
3948static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3949 pci_channel_state_t state)
3950{
3951 struct net_device *netdev = pci_get_drvdata(pdev);
3952 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3953
Mark Rustadea699562014-03-12 00:38:51 +00003954 if (!test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
3955 return PCI_ERS_RESULT_DISCONNECT;
3956
Mark Rustadbc0c7152014-03-12 00:38:45 +00003957 rtnl_lock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003958 netif_device_detach(netdev);
3959
Mark Rustadbc0c7152014-03-12 00:38:45 +00003960 if (state == pci_channel_io_perm_failure) {
3961 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003962 return PCI_ERS_RESULT_DISCONNECT;
Mark Rustadbc0c7152014-03-12 00:38:45 +00003963 }
Alexander Duyck9f19f312012-05-11 08:33:32 +00003964
3965 if (netif_running(netdev))
3966 ixgbevf_down(adapter);
3967
Mark Rustadbc0c7152014-03-12 00:38:45 +00003968 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3969 pci_disable_device(pdev);
3970 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003971
3972 /* Request a slot slot reset. */
3973 return PCI_ERS_RESULT_NEED_RESET;
3974}
3975
3976/**
3977 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3978 * @pdev: Pointer to PCI device
3979 *
3980 * Restart the card from scratch, as if from a cold-boot. Implementation
3981 * resembles the first-half of the ixgbevf_resume routine.
3982 */
3983static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3984{
3985 struct net_device *netdev = pci_get_drvdata(pdev);
3986 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3987
3988 if (pci_enable_device_mem(pdev)) {
3989 dev_err(&pdev->dev,
3990 "Cannot re-enable PCI device after reset.\n");
3991 return PCI_ERS_RESULT_DISCONNECT;
3992 }
3993
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003994 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003995 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck9f19f312012-05-11 08:33:32 +00003996 pci_set_master(pdev);
3997
3998 ixgbevf_reset(adapter);
3999
4000 return PCI_ERS_RESULT_RECOVERED;
4001}
4002
4003/**
4004 * ixgbevf_io_resume - called when traffic can start flowing again.
4005 * @pdev: Pointer to PCI device
4006 *
4007 * This callback is called when the error recovery driver tells us that
4008 * its OK to resume normal operation. Implementation resembles the
4009 * second-half of the ixgbevf_resume routine.
4010 */
4011static void ixgbevf_io_resume(struct pci_dev *pdev)
4012{
4013 struct net_device *netdev = pci_get_drvdata(pdev);
4014 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4015
4016 if (netif_running(netdev))
4017 ixgbevf_up(adapter);
4018
4019 netif_device_attach(netdev);
4020}
4021
4022/* PCI Error Recovery (ERS) */
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004023static const struct pci_error_handlers ixgbevf_err_handler = {
Alexander Duyck9f19f312012-05-11 08:33:32 +00004024 .error_detected = ixgbevf_io_error_detected,
4025 .slot_reset = ixgbevf_io_slot_reset,
4026 .resume = ixgbevf_io_resume,
4027};
4028
Greg Rose92915f72010-01-09 02:24:10 +00004029static struct pci_driver ixgbevf_driver = {
4030 .name = ixgbevf_driver_name,
4031 .id_table = ixgbevf_pci_tbl,
4032 .probe = ixgbevf_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05004033 .remove = ixgbevf_remove,
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00004034#ifdef CONFIG_PM
4035 /* Power Management Hooks */
4036 .suspend = ixgbevf_suspend,
4037 .resume = ixgbevf_resume,
4038#endif
Greg Rose92915f72010-01-09 02:24:10 +00004039 .shutdown = ixgbevf_shutdown,
Alexander Duyck9f19f312012-05-11 08:33:32 +00004040 .err_handler = &ixgbevf_err_handler
Greg Rose92915f72010-01-09 02:24:10 +00004041};
4042
4043/**
Greg Rose65d676c2011-02-03 06:54:13 +00004044 * ixgbevf_init_module - Driver Registration Routine
Greg Rose92915f72010-01-09 02:24:10 +00004045 *
Greg Rose65d676c2011-02-03 06:54:13 +00004046 * ixgbevf_init_module is the first routine called when the driver is
Greg Rose92915f72010-01-09 02:24:10 +00004047 * loaded. All it does is register with the PCI subsystem.
4048 **/
4049static int __init ixgbevf_init_module(void)
4050{
4051 int ret;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00004052 pr_info("%s - version %s\n", ixgbevf_driver_string,
4053 ixgbevf_driver_version);
Greg Rose92915f72010-01-09 02:24:10 +00004054
Jeff Kirsherdbd96362011-10-21 19:38:18 +00004055 pr_info("%s\n", ixgbevf_copyright);
Greg Rose92915f72010-01-09 02:24:10 +00004056
4057 ret = pci_register_driver(&ixgbevf_driver);
4058 return ret;
4059}
4060
4061module_init(ixgbevf_init_module);
4062
4063/**
Greg Rose65d676c2011-02-03 06:54:13 +00004064 * ixgbevf_exit_module - Driver Exit Cleanup Routine
Greg Rose92915f72010-01-09 02:24:10 +00004065 *
Greg Rose65d676c2011-02-03 06:54:13 +00004066 * ixgbevf_exit_module is called just before the driver is removed
Greg Rose92915f72010-01-09 02:24:10 +00004067 * from memory.
4068 **/
4069static void __exit ixgbevf_exit_module(void)
4070{
4071 pci_unregister_driver(&ixgbevf_driver);
4072}
4073
4074#ifdef DEBUG
4075/**
Greg Rose65d676c2011-02-03 06:54:13 +00004076 * ixgbevf_get_hw_dev_name - return device name string
Greg Rose92915f72010-01-09 02:24:10 +00004077 * used by hardware layer to print debugging information
4078 **/
4079char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4080{
4081 struct ixgbevf_adapter *adapter = hw->back;
4082 return adapter->netdev->name;
4083}
4084
4085#endif
4086module_exit(ixgbevf_exit_module);
4087
4088/* ixgbevf_main.c */