blob: 20bebd212ac2f603317fba3ce785dacba6a50eae [file] [log] [blame]
Greg Rose92915f72010-01-09 02:24:10 +00001/*******************************************************************************
2
3 Intel 82599 Virtual Function driver
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00004 Copyright(c) 1999 - 2014 Intel Corporation.
Greg Rose92915f72010-01-09 02:24:10 +00005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
28
29/******************************************************************************
30 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
31******************************************************************************/
Jeff Kirsherdbd96362011-10-21 19:38:18 +000032
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Greg Rose92915f72010-01-09 02:24:10 +000035#include <linux/types.h>
Jiri Pirkodadcd652011-07-21 03:25:09 +000036#include <linux/bitops.h>
Greg Rose92915f72010-01-09 02:24:10 +000037#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/netdevice.h>
40#include <linux/vmalloc.h>
41#include <linux/string.h>
42#include <linux/in.h>
43#include <linux/ip.h>
44#include <linux/tcp.h>
Alexander Duyck70a10e22012-05-11 08:33:21 +000045#include <linux/sctp.h>
Greg Rose92915f72010-01-09 02:24:10 +000046#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Greg Rose92915f72010-01-09 02:24:10 +000048#include <net/checksum.h>
49#include <net/ip6_checksum.h>
50#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000051#include <linux/if.h>
Greg Rose92915f72010-01-09 02:24:10 +000052#include <linux/if_vlan.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040053#include <linux/prefetch.h>
Greg Rose92915f72010-01-09 02:24:10 +000054
55#include "ixgbevf.h"
56
Stephen Hemminger3d8fe982012-01-18 22:13:34 +000057const char ixgbevf_driver_name[] = "ixgbevf";
Greg Rose92915f72010-01-09 02:24:10 +000058static const char ixgbevf_driver_string[] =
Greg Rose422e05d2011-03-12 02:01:29 +000059 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
Greg Rose92915f72010-01-09 02:24:10 +000060
Don Skidmore86f359f2014-01-17 01:21:38 -080061#define DRV_VERSION "2.12.1-k"
Greg Rose92915f72010-01-09 02:24:10 +000062const char ixgbevf_driver_version[] = DRV_VERSION;
Greg Rose66c87bd2010-11-16 19:26:43 -080063static char ixgbevf_copyright[] =
Greg Rose5c47a2b2012-01-06 02:53:30 +000064 "Copyright (c) 2009 - 2012 Intel Corporation.";
Greg Rose92915f72010-01-09 02:24:10 +000065
66static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
Greg Rose2316aa22010-12-02 07:12:26 +000067 [board_82599_vf] = &ixgbevf_82599_vf_info,
68 [board_X540_vf] = &ixgbevf_X540_vf_info,
Greg Rose92915f72010-01-09 02:24:10 +000069};
70
71/* ixgbevf_pci_tbl - PCI Device ID Table
72 *
73 * Wildcard entries (PCI_ANY_ID) should come last
74 * Last entry must be all 0s
75 *
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
78 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020079static const struct pci_device_id ixgbevf_pci_tbl[] = {
Stephen Hemminger39ba22b2013-02-06 02:37:04 +000080 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
81 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
Greg Rose92915f72010-01-09 02:24:10 +000082 /* required last entry */
83 {0, }
84};
85MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
86
87MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
Emil Tantilovb8ce18c2014-04-05 05:39:42 +000088MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
Greg Rose92915f72010-01-09 02:24:10 +000089MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
stephen hemmingerb3f4d592012-03-13 06:04:20 +000092#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
93static int debug = -1;
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
Greg Rose92915f72010-01-09 02:24:10 +000096
97/* forward decls */
Don Skidmore220fe052013-09-21 01:40:49 +000098static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
Alexander Duyckfa71ae22012-05-11 08:32:50 +000099static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
Alexander Duyck56e94092012-07-20 08:10:03 +0000100static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
Greg Rose92915f72010-01-09 02:24:10 +0000101
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000102static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
103{
104 struct ixgbevf_adapter *adapter = hw->back;
105
106 if (!hw->hw_addr)
107 return;
108 hw->hw_addr = NULL;
109 dev_err(&adapter->pdev->dev, "Adapter removed\n");
Mark Rustadea699562014-03-12 00:38:51 +0000110 if (test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
111 schedule_work(&adapter->watchdog_task);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000112}
113
114static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
115{
116 u32 value;
117
118 /* The following check not only optimizes a bit by not
119 * performing a read on the status register when the
120 * register just read was a status register read that
121 * returned IXGBE_FAILED_READ_REG. It also blocks any
122 * potential recursion.
123 */
124 if (reg == IXGBE_VFSTATUS) {
125 ixgbevf_remove_adapter(hw);
126 return;
127 }
Mark Rustad32c74942014-03-18 07:03:35 +0000128 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000129 if (value == IXGBE_FAILED_READ_REG)
130 ixgbevf_remove_adapter(hw);
131}
132
Mark Rustad32c74942014-03-18 07:03:35 +0000133u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
Mark Rustaddbf8b0d2014-03-04 03:02:34 +0000134{
135 u8 __iomem *reg_addr = ACCESS_ONCE(hw->hw_addr);
136 u32 value;
137
138 if (IXGBE_REMOVED(reg_addr))
139 return IXGBE_FAILED_READ_REG;
140 value = readl(reg_addr + reg);
141 if (unlikely(value == IXGBE_FAILED_READ_REG))
142 ixgbevf_check_remove(hw, reg);
143 return value;
144}
145
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000146/**
Greg Rose65d676c2011-02-03 06:54:13 +0000147 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
Greg Rose92915f72010-01-09 02:24:10 +0000148 * @adapter: pointer to adapter struct
149 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
150 * @queue: queue to map the corresponding interrupt to
151 * @msix_vector: the vector to map to the corresponding queue
Greg Rose92915f72010-01-09 02:24:10 +0000152 */
153static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
154 u8 queue, u8 msix_vector)
155{
156 u32 ivar, index;
157 struct ixgbe_hw *hw = &adapter->hw;
158 if (direction == -1) {
159 /* other causes */
160 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
161 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
162 ivar &= ~0xFF;
163 ivar |= msix_vector;
164 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
165 } else {
166 /* tx or rx causes */
167 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
168 index = ((16 * (queue & 1)) + (8 * direction));
169 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
170 ivar &= ~(0xFF << index);
171 ivar |= (msix_vector << index);
172 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
173 }
174}
175
Alexander Duyck70a10e22012-05-11 08:33:21 +0000176static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800177 struct ixgbevf_tx_buffer *tx_buffer)
Greg Rose92915f72010-01-09 02:24:10 +0000178{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800179 if (tx_buffer->skb) {
180 dev_kfree_skb_any(tx_buffer->skb);
181 if (dma_unmap_len(tx_buffer, len))
Alexander Duyck70a10e22012-05-11 08:33:21 +0000182 dma_unmap_single(tx_ring->dev,
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800183 dma_unmap_addr(tx_buffer, dma),
184 dma_unmap_len(tx_buffer, len),
Nick Nunley2a1f8792010-04-27 13:10:50 +0000185 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800186 } else if (dma_unmap_len(tx_buffer, len)) {
187 dma_unmap_page(tx_ring->dev,
188 dma_unmap_addr(tx_buffer, dma),
189 dma_unmap_len(tx_buffer, len),
190 DMA_TO_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +0000191 }
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800192 tx_buffer->next_to_watch = NULL;
193 tx_buffer->skb = NULL;
194 dma_unmap_len_set(tx_buffer, len, 0);
195 /* tx_buffer must be completely set up in the transmit path */
Greg Rose92915f72010-01-09 02:24:10 +0000196}
197
Greg Rose92915f72010-01-09 02:24:10 +0000198#define IXGBE_MAX_TXD_PWR 14
199#define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
200
201/* Tx Descriptors needed, worst case */
Alexander Duyck35959902012-05-11 08:32:40 +0000202#define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
203#define DESC_NEEDED (MAX_SKB_FRAGS + 4)
Greg Rose92915f72010-01-09 02:24:10 +0000204
205static void ixgbevf_tx_timeout(struct net_device *netdev);
206
207/**
208 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000209 * @q_vector: board private structure
Greg Rose92915f72010-01-09 02:24:10 +0000210 * @tx_ring: tx ring to clean
211 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000212static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
Greg Rose92915f72010-01-09 02:24:10 +0000213 struct ixgbevf_ring *tx_ring)
214{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000215 struct ixgbevf_adapter *adapter = q_vector->adapter;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800216 struct ixgbevf_tx_buffer *tx_buffer;
217 union ixgbe_adv_tx_desc *tx_desc;
Greg Rose92915f72010-01-09 02:24:10 +0000218 unsigned int total_bytes = 0, total_packets = 0;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800219 unsigned int budget = tx_ring->count / 2;
220 unsigned int i = tx_ring->next_to_clean;
Greg Rose92915f72010-01-09 02:24:10 +0000221
Alexander Duyck10cc1bd2012-07-16 23:44:48 +0000222 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
223 return true;
224
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800225 tx_buffer = &tx_ring->tx_buffer_info[i];
226 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
227 i -= tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000228
Alexander Duycke757e3e2013-01-31 07:43:22 +0000229 do {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800230 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000231
232 /* if next_to_watch is not set then there is no work pending */
233 if (!eop_desc)
234 break;
235
236 /* prevent any other reads prior to eop_desc */
237 read_barrier_depends();
238
239 /* if DD is not set pending work has not been completed */
240 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
241 break;
242
243 /* clear next_to_watch to prevent false hangs */
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800244 tx_buffer->next_to_watch = NULL;
Alexander Duycke757e3e2013-01-31 07:43:22 +0000245
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800246 /* update the statistics for this packet */
247 total_bytes += tx_buffer->bytecount;
248 total_packets += tx_buffer->gso_segs;
Greg Rose92915f72010-01-09 02:24:10 +0000249
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800250 /* free the skb */
251 dev_kfree_skb_any(tx_buffer->skb);
252
253 /* unmap skb header data */
254 dma_unmap_single(tx_ring->dev,
255 dma_unmap_addr(tx_buffer, dma),
256 dma_unmap_len(tx_buffer, len),
257 DMA_TO_DEVICE);
258
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800259 /* clear tx_buffer data */
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800260 tx_buffer->skb = NULL;
261 dma_unmap_len_set(tx_buffer, len, 0);
Greg Rose92915f72010-01-09 02:24:10 +0000262
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800263 /* unmap remaining buffers */
264 while (tx_desc != eop_desc) {
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800265 tx_buffer++;
266 tx_desc++;
Greg Rose92915f72010-01-09 02:24:10 +0000267 i++;
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800268 if (unlikely(!i)) {
269 i -= tx_ring->count;
270 tx_buffer = tx_ring->tx_buffer_info;
271 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
272 }
Alexander Duycke757e3e2013-01-31 07:43:22 +0000273
Emil Tantilov9bdfefd2014-01-17 18:30:04 -0800274 /* unmap any remaining paged data */
275 if (dma_unmap_len(tx_buffer, len)) {
276 dma_unmap_page(tx_ring->dev,
277 dma_unmap_addr(tx_buffer, dma),
278 dma_unmap_len(tx_buffer, len),
279 DMA_TO_DEVICE);
280 dma_unmap_len_set(tx_buffer, len, 0);
281 }
Greg Rose92915f72010-01-09 02:24:10 +0000282 }
283
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800284 /* move us one more past the eop_desc for start of next pkt */
285 tx_buffer++;
286 tx_desc++;
287 i++;
288 if (unlikely(!i)) {
289 i -= tx_ring->count;
290 tx_buffer = tx_ring->tx_buffer_info;
291 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
292 }
293
294 /* issue prefetch for next Tx descriptor */
295 prefetch(tx_desc);
296
297 /* update budget accounting */
298 budget--;
299 } while (likely(budget));
300
301 i += tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +0000302 tx_ring->next_to_clean = i;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000303 u64_stats_update_begin(&tx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800304 tx_ring->stats.bytes += total_bytes;
305 tx_ring->stats.packets += total_packets;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000306 u64_stats_update_end(&tx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000307 q_vector->tx.total_bytes += total_bytes;
308 q_vector->tx.total_packets += total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000309
Emil Tantilov7ad1a092014-01-17 18:30:03 -0800310#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
311 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
312 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
313 /* Make sure that anybody stopping the queue after this
314 * sees the new next_to_clean.
315 */
316 smp_mb();
317
318 if (__netif_subqueue_stopped(tx_ring->netdev,
319 tx_ring->queue_index) &&
320 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
321 netif_wake_subqueue(tx_ring->netdev,
322 tx_ring->queue_index);
323 ++tx_ring->tx_stats.restart_queue;
324 }
325 }
326
327 return !!budget;
Greg Rose92915f72010-01-09 02:24:10 +0000328}
329
330/**
Jacob Keller08681612013-09-21 06:24:09 +0000331 * ixgbevf_rx_skb - Helper function to determine proper Rx method
332 * @q_vector: structure containing interrupt and ring information
333 * @skb: packet to send up
Jacob Keller08681612013-09-21 06:24:09 +0000334 **/
335static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
Emil Tantilovdff80522014-11-08 01:39:25 +0000336 struct sk_buff *skb)
Jacob Keller08681612013-09-21 06:24:09 +0000337{
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000338#ifdef CONFIG_NET_RX_BUSY_POLL
339 skb_mark_napi_id(skb, &q_vector->napi);
340
341 if (ixgbevf_qv_busy_polling(q_vector)) {
342 netif_receive_skb(skb);
343 /* exit early if we busy polled */
344 return;
345 }
346#endif /* CONFIG_NET_RX_BUSY_POLL */
Emil Tantilovdff80522014-11-08 01:39:25 +0000347 if (!(q_vector->adapter->flags & IXGBE_FLAG_IN_NETPOLL))
348 napi_gro_receive(&q_vector->napi, skb);
349 else
350 netif_rx(skb);
Jacob Keller08681612013-09-21 06:24:09 +0000351}
352
Emil Tantilovec62fe22014-11-08 01:39:20 +0000353/* ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
354 * @ring: structure containig ring specific data
355 * @rx_desc: current Rx descriptor being processed
Greg Rose92915f72010-01-09 02:24:10 +0000356 * @skb: skb currently being received and modified
Emil Tantilovec62fe22014-11-08 01:39:20 +0000357 */
Greg Rose55fb2772012-11-06 05:53:32 +0000358static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
Emil Tantilovec62fe22014-11-08 01:39:20 +0000359 union ixgbe_adv_rx_desc *rx_desc,
360 struct sk_buff *skb)
Greg Rose92915f72010-01-09 02:24:10 +0000361{
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700362 skb_checksum_none_assert(skb);
Greg Rose92915f72010-01-09 02:24:10 +0000363
364 /* Rx csum disabled */
Alexander Duyckfb401952012-05-11 08:33:16 +0000365 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Greg Rose92915f72010-01-09 02:24:10 +0000366 return;
367
368 /* if IP and error */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000369 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
370 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800371 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000372 return;
373 }
374
Emil Tantilovec62fe22014-11-08 01:39:20 +0000375 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
Greg Rose92915f72010-01-09 02:24:10 +0000376 return;
377
Emil Tantilovec62fe22014-11-08 01:39:20 +0000378 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800379 ring->rx_stats.csum_err++;
Greg Rose92915f72010-01-09 02:24:10 +0000380 return;
381 }
382
383 /* It must be a TCP or UDP packet with a valid checksum */
384 skb->ip_summed = CHECKSUM_UNNECESSARY;
Greg Rose92915f72010-01-09 02:24:10 +0000385}
386
Emil Tantilovdff80522014-11-08 01:39:25 +0000387/* ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
388 * @rx_ring: rx descriptor ring packet is being transacted on
389 * @rx_desc: pointer to the EOP Rx descriptor
390 * @skb: pointer to current skb being populated
391 *
392 * This function checks the ring, descriptor, and packet information in
393 * order to populate the checksum, VLAN, protocol, and other fields within
394 * the skb.
395 */
396static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
397 union ixgbe_adv_rx_desc *rx_desc,
398 struct sk_buff *skb)
399{
400 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
401
402 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
403 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
404 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
405
406 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
407 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
408 }
409
410 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
411}
412
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000413/**
414 * ixgbevf_is_non_eop - process handling of non-EOP buffers
415 * @rx_ring: Rx ring being processed
416 * @rx_desc: Rx descriptor for current buffer
417 * @skb: current socket buffer containing buffer in progress
418 *
419 * This function updates next to clean. If the buffer is an EOP buffer
420 * this function exits returning false, otherwise it will place the
421 * sk_buff in the next buffer to be chained and return true indicating
422 * that this is in fact a non-EOP buffer.
423 **/
424static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
425 union ixgbe_adv_rx_desc *rx_desc,
426 struct sk_buff *skb)
427{
428 u32 ntc = rx_ring->next_to_clean + 1;
429
430 /* fetch, update, and store next to clean */
431 ntc = (ntc < rx_ring->count) ? ntc : 0;
432 rx_ring->next_to_clean = ntc;
433
434 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
435
436 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
437 return false;
438
439 return true;
440}
441
Emil Tantilovbafa5782014-11-08 01:39:15 +0000442static bool ixgbevf_alloc_mapped_skb(struct ixgbevf_ring *rx_ring,
443 struct ixgbevf_rx_buffer *bi)
444{
445 struct sk_buff *skb = bi->skb;
446 dma_addr_t dma = bi->dma;
447
448 if (unlikely(skb))
449 return true;
450
451 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
452 rx_ring->rx_buf_len);
453 if (unlikely(!skb)) {
454 rx_ring->rx_stats.alloc_rx_buff_failed++;
455 return false;
456 }
457
458 dma = dma_map_single(rx_ring->dev, skb->data,
459 rx_ring->rx_buf_len, DMA_FROM_DEVICE);
460
461 /* if mapping failed free memory back to system since
462 * there isn't much point in holding memory we can't use
463 */
464 if (dma_mapping_error(rx_ring->dev, dma)) {
465 dev_kfree_skb_any(skb);
466
467 rx_ring->rx_stats.alloc_rx_buff_failed++;
468 return false;
469 }
470
471 bi->skb = skb;
472 bi->dma = dma;
473
474 return true;
475}
476
Greg Rose92915f72010-01-09 02:24:10 +0000477/**
478 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
Emil Tantilov095e2612014-01-17 18:30:00 -0800479 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
Emil Tantilovbafa5782014-11-08 01:39:15 +0000480 * @cleaned_count: number of buffers to replace
Greg Rose92915f72010-01-09 02:24:10 +0000481 **/
Emil Tantilov095e2612014-01-17 18:30:00 -0800482static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
Emil Tantilovbafa5782014-11-08 01:39:15 +0000483 u16 cleaned_count)
Greg Rose92915f72010-01-09 02:24:10 +0000484{
Greg Rose92915f72010-01-09 02:24:10 +0000485 union ixgbe_adv_rx_desc *rx_desc;
486 struct ixgbevf_rx_buffer *bi;
Alexander Duyckfb401952012-05-11 08:33:16 +0000487 unsigned int i = rx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +0000488
Emil Tantilovbafa5782014-11-08 01:39:15 +0000489 /* nothing to do or no valid netdev defined */
490 if (!cleaned_count || !rx_ring->netdev)
491 return;
Greg Roseb9dd2452012-11-02 05:50:21 +0000492
Emil Tantilovbafa5782014-11-08 01:39:15 +0000493 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
494 bi = &rx_ring->rx_buffer_info[i];
495 i -= rx_ring->count;
Greg Roseb9dd2452012-11-02 05:50:21 +0000496
Emil Tantilovbafa5782014-11-08 01:39:15 +0000497 do {
498 if (!ixgbevf_alloc_mapped_skb(rx_ring, bi))
499 break;
Emil Tantilov05d063a2014-01-17 18:29:59 -0800500
Emil Tantilovbafa5782014-11-08 01:39:15 +0000501 /* Refresh the desc even if pkt_addr didn't change
502 * because each write-back erases this info.
503 */
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000504 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
Greg Rose92915f72010-01-09 02:24:10 +0000505
Emil Tantilovbafa5782014-11-08 01:39:15 +0000506 rx_desc++;
507 bi++;
Greg Rose92915f72010-01-09 02:24:10 +0000508 i++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000509 if (unlikely(!i)) {
510 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
511 bi = rx_ring->rx_buffer_info;
512 i -= rx_ring->count;
513 }
Greg Rose92915f72010-01-09 02:24:10 +0000514
Emil Tantilovbafa5782014-11-08 01:39:15 +0000515 /* clear the hdr_addr for the next_to_use descriptor */
516 rx_desc->read.hdr_addr = 0;
517
518 cleaned_count--;
519 } while (cleaned_count);
520
521 i += rx_ring->count;
522
523 if (rx_ring->next_to_use != i) {
524 /* record the next descriptor to use */
525 rx_ring->next_to_use = i;
526
527 /* Force memory writes to complete before letting h/w
528 * know there are new descriptors to fetch. (Only
529 * applicable for weak-ordered memory model archs,
530 * such as IA-64).
531 */
532 wmb();
533 ixgbevf_write_tail(rx_ring, i);
534 }
Greg Rose92915f72010-01-09 02:24:10 +0000535}
536
537static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000538 u32 qmask)
Greg Rose92915f72010-01-09 02:24:10 +0000539{
Greg Rose92915f72010-01-09 02:24:10 +0000540 struct ixgbe_hw *hw = &adapter->hw;
541
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000542 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
Greg Rose92915f72010-01-09 02:24:10 +0000543}
544
Jacob Keller08e50a22013-09-21 06:24:14 +0000545static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
546 struct ixgbevf_ring *rx_ring,
547 int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000548{
Greg Rose92915f72010-01-09 02:24:10 +0000549 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000550 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +0000551
Emil Tantilov0579eef2014-11-08 01:39:35 +0000552 do {
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000553 union ixgbe_adv_rx_desc *rx_desc;
Emil Tantilov0579eef2014-11-08 01:39:35 +0000554 struct ixgbevf_rx_buffer *rx_buffer;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000555 struct sk_buff *skb;
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000556 u16 ntc;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000557
Emil Tantilov0579eef2014-11-08 01:39:35 +0000558 /* return some buffers to hardware, one at a time is too slow */
559 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
560 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
561 cleaned_count = 0;
562 }
563
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000564 ntc = rx_ring->next_to_clean;
565 rx_desc = IXGBEVF_RX_DESC(rx_ring, ntc);
566 rx_buffer = &rx_ring->rx_buffer_info[ntc];
Emil Tantilov0579eef2014-11-08 01:39:35 +0000567
568 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_DD))
Greg Rose92915f72010-01-09 02:24:10 +0000569 break;
Greg Rose92915f72010-01-09 02:24:10 +0000570
Emil Tantilov0579eef2014-11-08 01:39:35 +0000571 /* This memory barrier is needed to keep us from reading
572 * any other fields out of the rx_desc until we know the
573 * RXD_STAT_DD bit is set
574 */
575 rmb();
Emil Tantilovec62fe22014-11-08 01:39:20 +0000576
Emil Tantilov0579eef2014-11-08 01:39:35 +0000577 skb = rx_buffer->skb;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000578 prefetch(skb->data);
Greg Rose92915f72010-01-09 02:24:10 +0000579
Emil Tantilov0579eef2014-11-08 01:39:35 +0000580 /* pull the header of the skb in */
581 __skb_put(skb, le16_to_cpu(rx_desc->wb.upper.length));
582
583 dma_unmap_single(rx_ring->dev, rx_buffer->dma,
Emil Tantilovec62fe22014-11-08 01:39:20 +0000584 rx_ring->rx_buf_len,
585 DMA_FROM_DEVICE);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000586
587 /* clear skb reference in buffer info structure */
588 rx_buffer->skb = NULL;
589 rx_buffer->dma = 0;
Greg Rose92915f72010-01-09 02:24:10 +0000590
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000591 cleaned_count++;
592
Emil Tantilov4b95fe32014-11-08 01:39:41 +0000593 /* place incomplete frames back on ring for completion */
594 if (ixgbevf_is_non_eop(rx_ring, rx_desc, skb))
Emil Tantilov0579eef2014-11-08 01:39:35 +0000595 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000596
Alexander Duyck5c60f812012-09-01 05:12:38 +0000597 /* we should not be chaining buffers, if we did drop the skb */
598 if (IXGBE_CB(skb)->prev) {
599 do {
600 struct sk_buff *this = skb;
601 skb = IXGBE_CB(skb)->prev;
602 dev_kfree_skb(this);
603 } while (skb);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000604 continue;
Alexander Duyck5c60f812012-09-01 05:12:38 +0000605 }
606
Greg Rose92915f72010-01-09 02:24:10 +0000607 /* ERR_MASK will only have valid bits if EOP set */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000608 if (unlikely(ixgbevf_test_staterr(rx_desc,
609 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
Greg Rose92915f72010-01-09 02:24:10 +0000610 dev_kfree_skb_irq(skb);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000611 continue;
Greg Rose92915f72010-01-09 02:24:10 +0000612 }
613
Greg Rose92915f72010-01-09 02:24:10 +0000614 /* probably a little skewed due to removing CRC */
615 total_rx_bytes += skb->len;
616 total_rx_packets++;
617
John Fastabend815cccb2012-10-24 08:13:09 +0000618 /* Workaround hardware that can't do proper VEPA multicast
619 * source pruning.
620 */
Florian Fainellibd9d5592014-02-28 15:46:49 -0800621 if ((skb->pkt_type == PACKET_BROADCAST ||
622 skb->pkt_type == PACKET_MULTICAST) &&
Emil Tantilov095e2612014-01-17 18:30:00 -0800623 ether_addr_equal(rx_ring->netdev->dev_addr,
Joe Perches7367d0b2013-09-01 11:51:23 -0700624 eth_hdr(skb)->h_source)) {
John Fastabend815cccb2012-10-24 08:13:09 +0000625 dev_kfree_skb_irq(skb);
Emil Tantilov0579eef2014-11-08 01:39:35 +0000626 continue;
John Fastabend815cccb2012-10-24 08:13:09 +0000627 }
628
Emil Tantilovdff80522014-11-08 01:39:25 +0000629 /* populate checksum, VLAN, and protocol */
630 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
631
632 ixgbevf_rx_skb(q_vector, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000633
Emil Tantilov0579eef2014-11-08 01:39:35 +0000634 /* update budget accounting */
635 budget--;
636 } while (likely(budget));
Greg Rose92915f72010-01-09 02:24:10 +0000637
Eric Dumazet4197aa72011-06-22 05:01:35 +0000638 u64_stats_update_begin(&rx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800639 rx_ring->stats.packets += total_rx_packets;
640 rx_ring->stats.bytes += total_rx_bytes;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000641 u64_stats_update_end(&rx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000642 q_vector->rx.total_packets += total_rx_packets;
643 q_vector->rx.total_bytes += total_rx_bytes;
Greg Rose92915f72010-01-09 02:24:10 +0000644
Emil Tantilovbafa5782014-11-08 01:39:15 +0000645 if (cleaned_count)
646 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
647
Jacob Keller08e50a22013-09-21 06:24:14 +0000648 return total_rx_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000649}
650
651/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000652 * ixgbevf_poll - NAPI polling calback
Greg Rose92915f72010-01-09 02:24:10 +0000653 * @napi: napi struct with our devices info in it
654 * @budget: amount of work driver is allowed to do this pass, in packets
655 *
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000656 * This function will clean more than one or more rings associated with a
Greg Rose92915f72010-01-09 02:24:10 +0000657 * q_vector.
658 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000659static int ixgbevf_poll(struct napi_struct *napi, int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000660{
661 struct ixgbevf_q_vector *q_vector =
662 container_of(napi, struct ixgbevf_q_vector, napi);
663 struct ixgbevf_adapter *adapter = q_vector->adapter;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000664 struct ixgbevf_ring *ring;
665 int per_ring_budget;
666 bool clean_complete = true;
667
668 ixgbevf_for_each_ring(ring, q_vector->tx)
669 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
Greg Rose92915f72010-01-09 02:24:10 +0000670
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000671#ifdef CONFIG_NET_RX_BUSY_POLL
672 if (!ixgbevf_qv_lock_napi(q_vector))
673 return budget;
674#endif
675
Greg Rose92915f72010-01-09 02:24:10 +0000676 /* attempt to distribute budget to each queue fairly, but don't allow
677 * the budget to go below 1 because we'll exit polling */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000678 if (q_vector->rx.count > 1)
679 per_ring_budget = max(budget/q_vector->rx.count, 1);
680 else
681 per_ring_budget = budget;
Greg Rose92915f72010-01-09 02:24:10 +0000682
Greg Rose366c1092012-11-13 04:03:18 +0000683 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000684 ixgbevf_for_each_ring(ring, q_vector->rx)
Jacob Keller08e50a22013-09-21 06:24:14 +0000685 clean_complete &= (ixgbevf_clean_rx_irq(q_vector, ring,
686 per_ring_budget)
687 < per_ring_budget);
Greg Rose366c1092012-11-13 04:03:18 +0000688 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
Greg Rose92915f72010-01-09 02:24:10 +0000689
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000690#ifdef CONFIG_NET_RX_BUSY_POLL
691 ixgbevf_qv_unlock_napi(q_vector);
692#endif
693
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000694 /* If all work not completed, return budget and keep polling */
695 if (!clean_complete)
696 return budget;
697 /* all work done, exit the polling mode */
698 napi_complete(napi);
699 if (adapter->rx_itr_setting & 1)
700 ixgbevf_set_itr(q_vector);
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000701 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
702 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000703 ixgbevf_irq_enable_queues(adapter,
704 1 << q_vector->v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000705
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000706 return 0;
Greg Rose92915f72010-01-09 02:24:10 +0000707}
708
Greg Rosece422602012-05-22 02:17:49 +0000709/**
710 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
711 * @q_vector: structure containing interrupt and ring information
712 */
Jacob Keller38496232013-10-22 06:19:18 +0000713void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
Greg Rosece422602012-05-22 02:17:49 +0000714{
715 struct ixgbevf_adapter *adapter = q_vector->adapter;
716 struct ixgbe_hw *hw = &adapter->hw;
717 int v_idx = q_vector->v_idx;
718 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
719
720 /*
721 * set the WDIS bit to not clear the timer bits and cause an
722 * immediate assertion of the interrupt
723 */
724 itr_reg |= IXGBE_EITR_CNT_WDIS;
725
726 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
727}
Greg Rose92915f72010-01-09 02:24:10 +0000728
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000729#ifdef CONFIG_NET_RX_BUSY_POLL
730/* must be called with local_bh_disable()d */
731static int ixgbevf_busy_poll_recv(struct napi_struct *napi)
732{
733 struct ixgbevf_q_vector *q_vector =
734 container_of(napi, struct ixgbevf_q_vector, napi);
735 struct ixgbevf_adapter *adapter = q_vector->adapter;
736 struct ixgbevf_ring *ring;
737 int found = 0;
738
739 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
740 return LL_FLUSH_FAILED;
741
742 if (!ixgbevf_qv_lock_poll(q_vector))
743 return LL_FLUSH_BUSY;
744
745 ixgbevf_for_each_ring(ring, q_vector->rx) {
746 found = ixgbevf_clean_rx_irq(q_vector, ring, 4);
Jacob Keller3b5dca22013-09-21 06:24:25 +0000747#ifdef BP_EXTENDED_STATS
748 if (found)
Emil Tantilov095e2612014-01-17 18:30:00 -0800749 ring->stats.cleaned += found;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000750 else
Emil Tantilov095e2612014-01-17 18:30:00 -0800751 ring->stats.misses++;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000752#endif
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000753 if (found)
754 break;
755 }
756
757 ixgbevf_qv_unlock_poll(q_vector);
758
759 return found;
760}
761#endif /* CONFIG_NET_RX_BUSY_POLL */
762
Greg Rose92915f72010-01-09 02:24:10 +0000763/**
764 * ixgbevf_configure_msix - Configure MSI-X hardware
765 * @adapter: board private structure
766 *
767 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
768 * interrupts.
769 **/
770static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
771{
772 struct ixgbevf_q_vector *q_vector;
Alexander Duyck6b43c442012-05-11 08:32:45 +0000773 int q_vectors, v_idx;
Greg Rose92915f72010-01-09 02:24:10 +0000774
775 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000776 adapter->eims_enable_mask = 0;
Greg Rose92915f72010-01-09 02:24:10 +0000777
778 /*
779 * Populate the IVAR table and set the ITR values to the
780 * corresponding register.
781 */
782 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
Alexander Duyck6b43c442012-05-11 08:32:45 +0000783 struct ixgbevf_ring *ring;
Greg Rose92915f72010-01-09 02:24:10 +0000784 q_vector = adapter->q_vector[v_idx];
Greg Rose92915f72010-01-09 02:24:10 +0000785
Alexander Duyck6b43c442012-05-11 08:32:45 +0000786 ixgbevf_for_each_ring(ring, q_vector->rx)
787 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000788
Alexander Duyck6b43c442012-05-11 08:32:45 +0000789 ixgbevf_for_each_ring(ring, q_vector->tx)
790 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000791
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000792 if (q_vector->tx.ring && !q_vector->rx.ring) {
793 /* tx only vector */
794 if (adapter->tx_itr_setting == 1)
795 q_vector->itr = IXGBE_10K_ITR;
796 else
797 q_vector->itr = adapter->tx_itr_setting;
798 } else {
799 /* rx or rx/tx vector */
800 if (adapter->rx_itr_setting == 1)
801 q_vector->itr = IXGBE_20K_ITR;
802 else
803 q_vector->itr = adapter->rx_itr_setting;
804 }
Greg Rose92915f72010-01-09 02:24:10 +0000805
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000806 /* add q_vector eims value to global eims_enable_mask */
807 adapter->eims_enable_mask |= 1 << v_idx;
808
809 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000810 }
811
812 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000813 /* setup eims_other and add value to global eims_enable_mask */
814 adapter->eims_other = 1 << v_idx;
815 adapter->eims_enable_mask |= adapter->eims_other;
Greg Rose92915f72010-01-09 02:24:10 +0000816}
817
818enum latency_range {
819 lowest_latency = 0,
820 low_latency = 1,
821 bulk_latency = 2,
822 latency_invalid = 255
823};
824
825/**
826 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000827 * @q_vector: structure containing interrupt and ring information
828 * @ring_container: structure containing ring performance data
Greg Rose92915f72010-01-09 02:24:10 +0000829 *
830 * Stores a new ITR value based on packets and byte
831 * counts during the last interrupt. The advantage of per interrupt
832 * computation is faster updates and more accurate ITR for the current
833 * traffic pattern. Constants in this function were computed
834 * based on theoretical maximum wire speed and thresholds were set based
835 * on testing data as well as attempting to minimize response time
836 * while increasing bulk throughput.
837 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000838static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
839 struct ixgbevf_ring_container *ring_container)
Greg Rose92915f72010-01-09 02:24:10 +0000840{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000841 int bytes = ring_container->total_bytes;
842 int packets = ring_container->total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000843 u32 timepassed_us;
844 u64 bytes_perint;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000845 u8 itr_setting = ring_container->itr;
Greg Rose92915f72010-01-09 02:24:10 +0000846
847 if (packets == 0)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000848 return;
Greg Rose92915f72010-01-09 02:24:10 +0000849
850 /* simple throttlerate management
851 * 0-20MB/s lowest (100000 ints/s)
852 * 20-100MB/s low (20000 ints/s)
853 * 100-1249MB/s bulk (8000 ints/s)
854 */
855 /* what was last interrupt timeslice? */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000856 timepassed_us = q_vector->itr >> 2;
Greg Rose92915f72010-01-09 02:24:10 +0000857 bytes_perint = bytes / timepassed_us; /* bytes/usec */
858
859 switch (itr_setting) {
860 case lowest_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000861 if (bytes_perint > 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000862 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000863 break;
864 case low_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000865 if (bytes_perint > 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000866 itr_setting = bulk_latency;
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000867 else if (bytes_perint <= 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000868 itr_setting = lowest_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000869 break;
870 case bulk_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000871 if (bytes_perint <= 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000872 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000873 break;
874 }
875
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000876 /* clear work counters since we have the values we need */
877 ring_container->total_bytes = 0;
878 ring_container->total_packets = 0;
879
880 /* write updated itr to ring container */
881 ring_container->itr = itr_setting;
Greg Rose92915f72010-01-09 02:24:10 +0000882}
883
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000884static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
Greg Rose92915f72010-01-09 02:24:10 +0000885{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000886 u32 new_itr = q_vector->itr;
887 u8 current_itr;
Greg Rose92915f72010-01-09 02:24:10 +0000888
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000889 ixgbevf_update_itr(q_vector, &q_vector->tx);
890 ixgbevf_update_itr(q_vector, &q_vector->rx);
Greg Rose92915f72010-01-09 02:24:10 +0000891
Alexander Duyck6b43c442012-05-11 08:32:45 +0000892 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Greg Rose92915f72010-01-09 02:24:10 +0000893
894 switch (current_itr) {
895 /* counts and packets in update_itr are dependent on these numbers */
896 case lowest_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000897 new_itr = IXGBE_100K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000898 break;
899 case low_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000900 new_itr = IXGBE_20K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000901 break;
902 case bulk_latency:
903 default:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000904 new_itr = IXGBE_8K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000905 break;
906 }
907
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000908 if (new_itr != q_vector->itr) {
Greg Rose92915f72010-01-09 02:24:10 +0000909 /* do an exponential smoothing */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000910 new_itr = (10 * new_itr * q_vector->itr) /
911 ((9 * new_itr) + q_vector->itr);
912
913 /* save the algorithm value here */
914 q_vector->itr = new_itr;
915
916 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000917 }
Greg Rose92915f72010-01-09 02:24:10 +0000918}
919
Alexander Duyck4b2cd272012-08-02 01:16:59 +0000920static irqreturn_t ixgbevf_msix_other(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +0000921{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000922 struct ixgbevf_adapter *adapter = data;
Greg Rose92915f72010-01-09 02:24:10 +0000923 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +0000924
Alexander Duyck4b2cd272012-08-02 01:16:59 +0000925 hw->mac.get_link_status = 1;
Greg Rose375b27c2012-01-18 22:13:31 +0000926
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000927 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
928 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Don Skidmorec7bb4172013-10-01 04:33:49 -0700929 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose3a2c4032012-02-01 01:28:15 +0000930
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000931 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
932
Greg Rose92915f72010-01-09 02:24:10 +0000933 return IRQ_HANDLED;
934}
935
Greg Rose92915f72010-01-09 02:24:10 +0000936/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000937 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
Greg Rose92915f72010-01-09 02:24:10 +0000938 * @irq: unused
939 * @data: pointer to our q_vector struct for this interrupt vector
940 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000941static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +0000942{
943 struct ixgbevf_q_vector *q_vector = data;
Greg Rose92915f72010-01-09 02:24:10 +0000944
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000945 /* EIAM disabled interrupts (on this vector) for us */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000946 if (q_vector->rx.ring || q_vector->tx.ring)
947 napi_schedule(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +0000948
949 return IRQ_HANDLED;
950}
951
952static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
953 int r_idx)
954{
955 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
956
Don Skidmore87e70ab2014-01-16 02:30:08 -0800957 a->rx_ring[r_idx]->next = q_vector->rx.ring;
958 q_vector->rx.ring = a->rx_ring[r_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +0000959 q_vector->rx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000960}
961
962static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
963 int t_idx)
964{
965 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
966
Don Skidmore87e70ab2014-01-16 02:30:08 -0800967 a->tx_ring[t_idx]->next = q_vector->tx.ring;
968 q_vector->tx.ring = a->tx_ring[t_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +0000969 q_vector->tx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000970}
971
972/**
973 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
974 * @adapter: board private structure to initialize
975 *
976 * This function maps descriptor rings to the queue-specific vectors
977 * we were allotted through the MSI-X enabling code. Ideally, we'd have
978 * one vector per ring/queue, but on a constrained vector budget, we
979 * group the rings as "efficiently" as possible. You would add new
980 * mapping configurations in here.
981 **/
982static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
983{
984 int q_vectors;
985 int v_start = 0;
986 int rxr_idx = 0, txr_idx = 0;
987 int rxr_remaining = adapter->num_rx_queues;
988 int txr_remaining = adapter->num_tx_queues;
989 int i, j;
990 int rqpv, tqpv;
991 int err = 0;
992
993 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
994
995 /*
996 * The ideal configuration...
997 * We have enough vectors to map one per queue.
998 */
999 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1000 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1001 map_vector_to_rxq(adapter, v_start, rxr_idx);
1002
1003 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1004 map_vector_to_txq(adapter, v_start, txr_idx);
1005 goto out;
1006 }
1007
1008 /*
1009 * If we don't have enough vectors for a 1-to-1
1010 * mapping, we'll have to group them so there are
1011 * multiple queues per vector.
1012 */
1013 /* Re-adjusting *qpv takes care of the remainder. */
1014 for (i = v_start; i < q_vectors; i++) {
1015 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1016 for (j = 0; j < rqpv; j++) {
1017 map_vector_to_rxq(adapter, i, rxr_idx);
1018 rxr_idx++;
1019 rxr_remaining--;
1020 }
1021 }
1022 for (i = v_start; i < q_vectors; i++) {
1023 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1024 for (j = 0; j < tqpv; j++) {
1025 map_vector_to_txq(adapter, i, txr_idx);
1026 txr_idx++;
1027 txr_remaining--;
1028 }
1029 }
1030
1031out:
1032 return err;
1033}
1034
1035/**
1036 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1037 * @adapter: board private structure
1038 *
1039 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1040 * interrupts from the kernel.
1041 **/
1042static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1043{
1044 struct net_device *netdev = adapter->netdev;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001045 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1046 int vector, err;
Greg Rose92915f72010-01-09 02:24:10 +00001047 int ri = 0, ti = 0;
1048
Greg Rose92915f72010-01-09 02:24:10 +00001049 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001050 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1051 struct msix_entry *entry = &adapter->msix_entries[vector];
Greg Rose92915f72010-01-09 02:24:10 +00001052
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001053 if (q_vector->tx.ring && q_vector->rx.ring) {
1054 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1055 "%s-%s-%d", netdev->name, "TxRx", ri++);
1056 ti++;
1057 } else if (q_vector->rx.ring) {
1058 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1059 "%s-%s-%d", netdev->name, "rx", ri++);
1060 } else if (q_vector->tx.ring) {
1061 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1062 "%s-%s-%d", netdev->name, "tx", ti++);
Greg Rose92915f72010-01-09 02:24:10 +00001063 } else {
1064 /* skip this unused q_vector */
1065 continue;
1066 }
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001067 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1068 q_vector->name, q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001069 if (err) {
1070 hw_dbg(&adapter->hw,
1071 "request_irq failed for MSIX interrupt "
1072 "Error: %d\n", err);
1073 goto free_queue_irqs;
1074 }
1075 }
1076
Greg Rose92915f72010-01-09 02:24:10 +00001077 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001078 &ixgbevf_msix_other, 0, netdev->name, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001079 if (err) {
1080 hw_dbg(&adapter->hw,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001081 "request_irq for msix_other failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00001082 goto free_queue_irqs;
1083 }
1084
1085 return 0;
1086
1087free_queue_irqs:
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001088 while (vector) {
1089 vector--;
1090 free_irq(adapter->msix_entries[vector].vector,
1091 adapter->q_vector[vector]);
1092 }
xunleera1f6c6b2013-03-05 07:44:20 +00001093 /* This failure is non-recoverable - it indicates the system is
1094 * out of MSIX vector resources and the VF driver cannot run
1095 * without them. Set the number of msix vectors to zero
1096 * indicating that not enough can be allocated. The error
1097 * will be returned to the user indicating device open failed.
1098 * Any further attempts to force the driver to open will also
1099 * fail. The only way to recover is to unload the driver and
1100 * reload it again. If the system has recovered some MSIX
1101 * vectors then it may succeed.
1102 */
1103 adapter->num_msix_vectors = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001104 return err;
1105}
1106
1107static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1108{
1109 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1110
1111 for (i = 0; i < q_vectors; i++) {
1112 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001113 q_vector->rx.ring = NULL;
1114 q_vector->tx.ring = NULL;
1115 q_vector->rx.count = 0;
1116 q_vector->tx.count = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001117 }
1118}
1119
1120/**
1121 * ixgbevf_request_irq - initialize interrupts
1122 * @adapter: board private structure
1123 *
1124 * Attempts to configure interrupts using the best available
1125 * capabilities of the hardware and kernel.
1126 **/
1127static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1128{
1129 int err = 0;
1130
1131 err = ixgbevf_request_msix_irqs(adapter);
1132
1133 if (err)
1134 hw_dbg(&adapter->hw,
1135 "request_irq failed, Error %d\n", err);
1136
1137 return err;
1138}
1139
1140static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1141{
Greg Rose92915f72010-01-09 02:24:10 +00001142 int i, q_vectors;
1143
1144 q_vectors = adapter->num_msix_vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001145 i = q_vectors - 1;
1146
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001147 free_irq(adapter->msix_entries[i].vector, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001148 i--;
1149
1150 for (; i >= 0; i--) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001151 /* free only the irqs that were actually requested */
1152 if (!adapter->q_vector[i]->rx.ring &&
1153 !adapter->q_vector[i]->tx.ring)
1154 continue;
1155
Greg Rose92915f72010-01-09 02:24:10 +00001156 free_irq(adapter->msix_entries[i].vector,
1157 adapter->q_vector[i]);
1158 }
1159
1160 ixgbevf_reset_q_vectors(adapter);
1161}
1162
1163/**
1164 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1165 * @adapter: board private structure
1166 **/
1167static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1168{
Greg Rose92915f72010-01-09 02:24:10 +00001169 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001170 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001171
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001172 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001173 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001174 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001175
1176 IXGBE_WRITE_FLUSH(hw);
1177
1178 for (i = 0; i < adapter->num_msix_vectors; i++)
1179 synchronize_irq(adapter->msix_entries[i].vector);
1180}
1181
1182/**
1183 * ixgbevf_irq_enable - Enable default interrupt generation settings
1184 * @adapter: board private structure
1185 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001186static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001187{
1188 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001189
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001190 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1191 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1192 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
Greg Rose92915f72010-01-09 02:24:10 +00001193}
1194
1195/**
Don Skidmorede02dec2014-01-16 02:30:09 -08001196 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1197 * @adapter: board private structure
1198 * @ring: structure containing ring specific data
1199 *
1200 * Configure the Tx descriptor ring after a reset.
1201 **/
1202static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1203 struct ixgbevf_ring *ring)
1204{
1205 struct ixgbe_hw *hw = &adapter->hw;
1206 u64 tdba = ring->dma;
1207 int wait_loop = 10;
1208 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1209 u8 reg_idx = ring->reg_idx;
1210
1211 /* disable queue to avoid issues while updating state */
1212 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1213 IXGBE_WRITE_FLUSH(hw);
1214
1215 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1216 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1217 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1218 ring->count * sizeof(union ixgbe_adv_tx_desc));
1219
1220 /* disable head writeback */
1221 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1222 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1223
1224 /* enable relaxed ordering */
1225 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1226 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1227 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1228
1229 /* reset head and tail pointers */
1230 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1231 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001232 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001233
1234 /* reset ntu and ntc to place SW in sync with hardwdare */
1235 ring->next_to_clean = 0;
1236 ring->next_to_use = 0;
1237
1238 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1239 * to or less than the number of on chip descriptors, which is
1240 * currently 40.
1241 */
1242 txdctl |= (8 << 16); /* WTHRESH = 8 */
1243
1244 /* Setting PTHRESH to 32 both improves performance */
1245 txdctl |= (1 << 8) | /* HTHRESH = 1 */
1246 32; /* PTHRESH = 32 */
1247
1248 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1249
1250 /* poll to verify queue is enabled */
1251 do {
1252 usleep_range(1000, 2000);
1253 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1254 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1255 if (!wait_loop)
1256 pr_err("Could not enable Tx Queue %d\n", reg_idx);
1257}
1258
1259/**
Greg Rose92915f72010-01-09 02:24:10 +00001260 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1261 * @adapter: board private structure
1262 *
1263 * Configure the Tx unit of the MAC after a reset.
1264 **/
1265static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1266{
Don Skidmorede02dec2014-01-16 02:30:09 -08001267 u32 i;
Greg Rose92915f72010-01-09 02:24:10 +00001268
1269 /* Setup the HW Tx Head and Tail descriptor pointers */
Don Skidmorede02dec2014-01-16 02:30:09 -08001270 for (i = 0; i < adapter->num_tx_queues; i++)
1271 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001272}
1273
1274#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1275
1276static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1277{
1278 struct ixgbevf_ring *rx_ring;
1279 struct ixgbe_hw *hw = &adapter->hw;
1280 u32 srrctl;
1281
Don Skidmore87e70ab2014-01-16 02:30:08 -08001282 rx_ring = adapter->rx_ring[index];
Greg Rose92915f72010-01-09 02:24:10 +00001283
1284 srrctl = IXGBE_SRRCTL_DROP_EN;
1285
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001286 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
Greg Rose92915f72010-01-09 02:24:10 +00001287
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001288 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1289 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1290
Greg Rose92915f72010-01-09 02:24:10 +00001291 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1292}
1293
Don Skidmore1bb9c632013-09-21 01:57:33 +00001294static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1295{
1296 struct ixgbe_hw *hw = &adapter->hw;
1297
1298 /* PSRTYPE must be initialized in 82599 */
1299 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1300 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1301 IXGBE_PSRTYPE_L2HDR;
1302
1303 if (adapter->num_rx_queues > 1)
1304 psrtype |= 1 << 29;
1305
1306 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1307}
1308
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001309static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1310{
1311 struct ixgbe_hw *hw = &adapter->hw;
1312 struct net_device *netdev = adapter->netdev;
1313 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1314 int i;
1315 u16 rx_buf_len;
1316
1317 /* notify the PF of our intent to use this size of frame */
1318 ixgbevf_rlpml_set_vf(hw, max_frame);
1319
1320 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1321 max_frame += VLAN_HLEN;
1322
1323 /*
Greg Rose85624ca2012-11-13 04:03:19 +00001324 * Allocate buffer sizes that fit well into 32K and
1325 * take into account max frame size of 9.5K
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001326 */
1327 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1328 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1329 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose85624ca2012-11-13 04:03:19 +00001330 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1331 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1332 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1333 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1334 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1335 rx_buf_len = IXGBEVF_RXBUFFER_8K;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001336 else
Greg Rose85624ca2012-11-13 04:03:19 +00001337 rx_buf_len = IXGBEVF_RXBUFFER_10K;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001338
1339 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08001340 adapter->rx_ring[i]->rx_buf_len = rx_buf_len;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001341}
1342
Don Skidmorede02dec2014-01-16 02:30:09 -08001343#define IXGBEVF_MAX_RX_DESC_POLL 10
1344static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1345 struct ixgbevf_ring *ring)
1346{
1347 struct ixgbe_hw *hw = &adapter->hw;
1348 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1349 u32 rxdctl;
1350 u8 reg_idx = ring->reg_idx;
1351
Mark Rustad26597802014-03-04 03:02:45 +00001352 if (IXGBE_REMOVED(hw->hw_addr))
1353 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001354 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1355 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1356
1357 /* write value back with RXDCTL.ENABLE bit cleared */
1358 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1359
1360 /* the hardware may take up to 100us to really disable the rx queue */
1361 do {
1362 udelay(10);
1363 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1364 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1365
1366 if (!wait_loop)
1367 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1368 reg_idx);
1369}
1370
1371static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1372 struct ixgbevf_ring *ring)
1373{
1374 struct ixgbe_hw *hw = &adapter->hw;
1375 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1376 u32 rxdctl;
1377 u8 reg_idx = ring->reg_idx;
1378
Mark Rustad26597802014-03-04 03:02:45 +00001379 if (IXGBE_REMOVED(hw->hw_addr))
1380 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001381 do {
1382 usleep_range(1000, 2000);
1383 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1384 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1385
1386 if (!wait_loop)
1387 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1388 reg_idx);
1389}
1390
1391static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1392 struct ixgbevf_ring *ring)
1393{
1394 struct ixgbe_hw *hw = &adapter->hw;
1395 u64 rdba = ring->dma;
1396 u32 rxdctl;
1397 u8 reg_idx = ring->reg_idx;
1398
1399 /* disable queue to avoid issues while updating state */
1400 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1401 ixgbevf_disable_rx_queue(adapter, ring);
1402
1403 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1404 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1405 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1406 ring->count * sizeof(union ixgbe_adv_rx_desc));
1407
1408 /* enable relaxed ordering */
1409 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1410 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1411
1412 /* reset head and tail pointers */
1413 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1414 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001415 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001416
1417 /* reset ntu and ntc to place SW in sync with hardwdare */
1418 ring->next_to_clean = 0;
1419 ring->next_to_use = 0;
1420
1421 ixgbevf_configure_srrctl(adapter, reg_idx);
1422
1423 /* prevent DMA from exceeding buffer space available */
1424 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1425 rxdctl |= ring->rx_buf_len | IXGBE_RXDCTL_RLPML_EN;
1426 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1427 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1428
1429 ixgbevf_rx_desc_queue_enable(adapter, ring);
Emil Tantilov095e2612014-01-17 18:30:00 -08001430 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
Don Skidmorede02dec2014-01-16 02:30:09 -08001431}
1432
Greg Rose92915f72010-01-09 02:24:10 +00001433/**
1434 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1435 * @adapter: board private structure
1436 *
1437 * Configure the Rx unit of the MAC after a reset.
1438 **/
1439static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1440{
Don Skidmorede02dec2014-01-16 02:30:09 -08001441 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001442
Don Skidmore1bb9c632013-09-21 01:57:33 +00001443 ixgbevf_setup_psrtype(adapter);
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001444
1445 /* set_rx_buffer_len must be called before ring initialization */
1446 ixgbevf_set_rx_buffer_len(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001447
Greg Rose92915f72010-01-09 02:24:10 +00001448 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1449 * the Base and Length of the Rx Descriptor Ring */
Don Skidmorede02dec2014-01-16 02:30:09 -08001450 for (i = 0; i < adapter->num_rx_queues; i++)
1451 ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001452}
1453
Patrick McHardy80d5c362013-04-19 02:04:28 +00001454static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1455 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001456{
1457 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1458 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001459 int err;
1460
John Fastabend55fdd45b2012-10-01 14:52:20 +00001461 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001462
Greg Rose92915f72010-01-09 02:24:10 +00001463 /* add VID to filter table */
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001464 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001465
John Fastabend55fdd45b2012-10-01 14:52:20 +00001466 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001467
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001468 /* translate error return types so error makes sense */
1469 if (err == IXGBE_ERR_MBX)
1470 return -EIO;
1471
1472 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1473 return -EACCES;
1474
Jiri Pirkodadcd652011-07-21 03:25:09 +00001475 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001476
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001477 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001478}
1479
Patrick McHardy80d5c362013-04-19 02:04:28 +00001480static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1481 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001482{
1483 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1484 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001485 int err = -EOPNOTSUPP;
Greg Rose92915f72010-01-09 02:24:10 +00001486
John Fastabend55fdd45b2012-10-01 14:52:20 +00001487 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001488
Greg Rose92915f72010-01-09 02:24:10 +00001489 /* remove VID from filter table */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001490 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001491
John Fastabend55fdd45b2012-10-01 14:52:20 +00001492 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001493
Jiri Pirkodadcd652011-07-21 03:25:09 +00001494 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001495
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001496 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001497}
1498
1499static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1500{
Jiri Pirkodadcd652011-07-21 03:25:09 +00001501 u16 vid;
Greg Rose92915f72010-01-09 02:24:10 +00001502
Jiri Pirkodadcd652011-07-21 03:25:09 +00001503 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001504 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1505 htons(ETH_P_8021Q), vid);
Greg Rose92915f72010-01-09 02:24:10 +00001506}
1507
Greg Rose46ec20f2011-05-13 01:33:42 +00001508static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1509{
1510 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1511 struct ixgbe_hw *hw = &adapter->hw;
1512 int count = 0;
1513
1514 if ((netdev_uc_count(netdev)) > 10) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001515 pr_err("Too many unicast filters - No Space\n");
Greg Rose46ec20f2011-05-13 01:33:42 +00001516 return -ENOSPC;
1517 }
1518
1519 if (!netdev_uc_empty(netdev)) {
1520 struct netdev_hw_addr *ha;
1521 netdev_for_each_uc_addr(ha, netdev) {
1522 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1523 udelay(200);
1524 }
1525 } else {
1526 /*
1527 * If the list is empty then send message to PF driver to
1528 * clear all macvlans on this VF.
1529 */
1530 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1531 }
1532
1533 return count;
1534}
1535
Greg Rose92915f72010-01-09 02:24:10 +00001536/**
Greg Rosedee847f2012-11-02 05:50:57 +00001537 * ixgbevf_set_rx_mode - Multicast and unicast set
Greg Rose92915f72010-01-09 02:24:10 +00001538 * @netdev: network interface device structure
1539 *
1540 * The set_rx_method entry point is called whenever the multicast address
Greg Rosedee847f2012-11-02 05:50:57 +00001541 * list, unicast address list or the network interface flags are updated.
1542 * This routine is responsible for configuring the hardware for proper
1543 * multicast mode and configuring requested unicast filters.
Greg Rose92915f72010-01-09 02:24:10 +00001544 **/
1545static void ixgbevf_set_rx_mode(struct net_device *netdev)
1546{
1547 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1548 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001549
John Fastabend55fdd45b2012-10-01 14:52:20 +00001550 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001551
Greg Rose92915f72010-01-09 02:24:10 +00001552 /* reprogram multicast list */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001553 hw->mac.ops.update_mc_addr_list(hw, netdev);
Greg Rose46ec20f2011-05-13 01:33:42 +00001554
1555 ixgbevf_write_uc_addr_list(netdev);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001556
John Fastabend55fdd45b2012-10-01 14:52:20 +00001557 spin_unlock_bh(&adapter->mbx_lock);
Greg Rose92915f72010-01-09 02:24:10 +00001558}
1559
1560static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1561{
1562 int q_idx;
1563 struct ixgbevf_q_vector *q_vector;
1564 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1565
1566 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Greg Rose92915f72010-01-09 02:24:10 +00001567 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001568#ifdef CONFIG_NET_RX_BUSY_POLL
1569 ixgbevf_qv_init_lock(adapter->q_vector[q_idx]);
1570#endif
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001571 napi_enable(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001572 }
1573}
1574
1575static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1576{
1577 int q_idx;
1578 struct ixgbevf_q_vector *q_vector;
1579 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1580
1581 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1582 q_vector = adapter->q_vector[q_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001583 napi_disable(&q_vector->napi);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001584#ifdef CONFIG_NET_RX_BUSY_POLL
1585 while (!ixgbevf_qv_disable(adapter->q_vector[q_idx])) {
1586 pr_info("QV %d locked\n", q_idx);
1587 usleep_range(1000, 20000);
1588 }
1589#endif /* CONFIG_NET_RX_BUSY_POLL */
Greg Rose92915f72010-01-09 02:24:10 +00001590 }
1591}
1592
Don Skidmore220fe052013-09-21 01:40:49 +00001593static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
1594{
1595 struct ixgbe_hw *hw = &adapter->hw;
1596 unsigned int def_q = 0;
1597 unsigned int num_tcs = 0;
1598 unsigned int num_rx_queues = 1;
1599 int err;
1600
1601 spin_lock_bh(&adapter->mbx_lock);
1602
1603 /* fetch queue configuration from the PF */
1604 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1605
1606 spin_unlock_bh(&adapter->mbx_lock);
1607
1608 if (err)
1609 return err;
1610
1611 if (num_tcs > 1) {
1612 /* update default Tx ring register index */
Don Skidmore87e70ab2014-01-16 02:30:08 -08001613 adapter->tx_ring[0]->reg_idx = def_q;
Don Skidmore220fe052013-09-21 01:40:49 +00001614
1615 /* we need as many queues as traffic classes */
1616 num_rx_queues = num_tcs;
1617 }
1618
1619 /* if we have a bad config abort request queue reset */
1620 if (adapter->num_rx_queues != num_rx_queues) {
1621 /* force mailbox timeout to prevent further messages */
1622 hw->mbx.timeout = 0;
1623
1624 /* wait for watchdog to come around and bail us out */
1625 adapter->flags |= IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
1626 }
1627
1628 return 0;
1629}
1630
Greg Rose92915f72010-01-09 02:24:10 +00001631static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1632{
Don Skidmore220fe052013-09-21 01:40:49 +00001633 ixgbevf_configure_dcb(adapter);
1634
Don Skidmorede02dec2014-01-16 02:30:09 -08001635 ixgbevf_set_rx_mode(adapter->netdev);
Greg Rose92915f72010-01-09 02:24:10 +00001636
1637 ixgbevf_restore_vlan(adapter);
1638
1639 ixgbevf_configure_tx(adapter);
1640 ixgbevf_configure_rx(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001641}
1642
Greg Rose33bd9f62010-03-19 02:59:52 +00001643static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1644{
1645 /* Only save pre-reset stats if there are some */
1646 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1647 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1648 adapter->stats.base_vfgprc;
1649 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1650 adapter->stats.base_vfgptc;
1651 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1652 adapter->stats.base_vfgorc;
1653 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1654 adapter->stats.base_vfgotc;
1655 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1656 adapter->stats.base_vfmprc;
1657 }
1658}
1659
1660static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1661{
1662 struct ixgbe_hw *hw = &adapter->hw;
1663
1664 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1665 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1666 adapter->stats.last_vfgorc |=
1667 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1668 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1669 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1670 adapter->stats.last_vfgotc |=
1671 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1672 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1673
1674 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1675 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1676 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1677 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1678 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1679}
1680
Alexander Duyck31186782012-07-20 08:09:58 +00001681static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1682{
1683 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck56e94092012-07-20 08:10:03 +00001684 int api[] = { ixgbe_mbox_api_11,
1685 ixgbe_mbox_api_10,
Alexander Duyck31186782012-07-20 08:09:58 +00001686 ixgbe_mbox_api_unknown };
1687 int err = 0, idx = 0;
1688
John Fastabend55fdd45b2012-10-01 14:52:20 +00001689 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001690
1691 while (api[idx] != ixgbe_mbox_api_unknown) {
1692 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1693 if (!err)
1694 break;
1695 idx++;
1696 }
1697
John Fastabend55fdd45b2012-10-01 14:52:20 +00001698 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001699}
1700
Greg Rose795180d2012-04-17 04:29:34 +00001701static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001702{
1703 struct net_device *netdev = adapter->netdev;
1704 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001705
1706 ixgbevf_configure_msix(adapter);
1707
John Fastabend55fdd45b2012-10-01 14:52:20 +00001708 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001709
Greg Rose92fe0bf2012-11-02 05:50:47 +00001710 if (is_valid_ether_addr(hw->mac.addr))
1711 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1712 else
1713 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001714
John Fastabend55fdd45b2012-10-01 14:52:20 +00001715 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001716
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001717 smp_mb__before_atomic();
Greg Rose92915f72010-01-09 02:24:10 +00001718 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1719 ixgbevf_napi_enable_all(adapter);
1720
1721 /* enable transmits */
1722 netif_tx_start_all_queues(netdev);
1723
Greg Rose33bd9f62010-03-19 02:59:52 +00001724 ixgbevf_save_reset_stats(adapter);
1725 ixgbevf_init_last_counter_stats(adapter);
1726
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001727 hw->mac.get_link_status = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001728 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose92915f72010-01-09 02:24:10 +00001729}
1730
Greg Rose795180d2012-04-17 04:29:34 +00001731void ixgbevf_up(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001732{
Greg Rose92915f72010-01-09 02:24:10 +00001733 struct ixgbe_hw *hw = &adapter->hw;
1734
1735 ixgbevf_configure(adapter);
1736
Greg Rose795180d2012-04-17 04:29:34 +00001737 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001738
1739 /* clear any pending interrupts, may auto mask */
1740 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1741
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001742 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001743}
1744
1745/**
1746 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
Greg Rose92915f72010-01-09 02:24:10 +00001747 * @rx_ring: ring to free buffers from
1748 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001749static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001750{
Greg Rose92915f72010-01-09 02:24:10 +00001751 unsigned long size;
1752 unsigned int i;
1753
Greg Rosec0456c22010-01-22 22:47:18 +00001754 if (!rx_ring->rx_buffer_info)
1755 return;
Greg Rose92915f72010-01-09 02:24:10 +00001756
Greg Rosec0456c22010-01-22 22:47:18 +00001757 /* Free all the Rx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00001758 for (i = 0; i < rx_ring->count; i++) {
1759 struct ixgbevf_rx_buffer *rx_buffer_info;
1760
1761 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1762 if (rx_buffer_info->dma) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08001763 dma_unmap_single(rx_ring->dev, rx_buffer_info->dma,
Greg Rose92915f72010-01-09 02:24:10 +00001764 rx_ring->rx_buf_len,
Nick Nunley2a1f8792010-04-27 13:10:50 +00001765 DMA_FROM_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +00001766 rx_buffer_info->dma = 0;
1767 }
1768 if (rx_buffer_info->skb) {
1769 struct sk_buff *skb = rx_buffer_info->skb;
1770 rx_buffer_info->skb = NULL;
1771 do {
1772 struct sk_buff *this = skb;
Alexander Duyck5c60f812012-09-01 05:12:38 +00001773 skb = IXGBE_CB(skb)->prev;
Greg Rose92915f72010-01-09 02:24:10 +00001774 dev_kfree_skb(this);
1775 } while (skb);
1776 }
Greg Rose92915f72010-01-09 02:24:10 +00001777 }
1778
1779 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1780 memset(rx_ring->rx_buffer_info, 0, size);
1781
1782 /* Zero out the descriptor ring */
1783 memset(rx_ring->desc, 0, rx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001784}
1785
1786/**
1787 * ixgbevf_clean_tx_ring - Free Tx Buffers
Greg Rose92915f72010-01-09 02:24:10 +00001788 * @tx_ring: ring to be cleaned
1789 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001790static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001791{
1792 struct ixgbevf_tx_buffer *tx_buffer_info;
1793 unsigned long size;
1794 unsigned int i;
1795
Greg Rosec0456c22010-01-22 22:47:18 +00001796 if (!tx_ring->tx_buffer_info)
1797 return;
1798
Greg Rose92915f72010-01-09 02:24:10 +00001799 /* Free all the Tx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00001800 for (i = 0; i < tx_ring->count; i++) {
1801 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00001802 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00001803 }
1804
1805 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1806 memset(tx_ring->tx_buffer_info, 0, size);
1807
1808 memset(tx_ring->desc, 0, tx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001809}
1810
1811/**
1812 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1813 * @adapter: board private structure
1814 **/
1815static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1816{
1817 int i;
1818
1819 for (i = 0; i < adapter->num_rx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08001820 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001821}
1822
1823/**
1824 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1825 * @adapter: board private structure
1826 **/
1827static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1828{
1829 int i;
1830
1831 for (i = 0; i < adapter->num_tx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08001832 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001833}
1834
1835void ixgbevf_down(struct ixgbevf_adapter *adapter)
1836{
1837 struct net_device *netdev = adapter->netdev;
1838 struct ixgbe_hw *hw = &adapter->hw;
Don Skidmorede02dec2014-01-16 02:30:09 -08001839 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001840
1841 /* signal that we are down to the interrupt handler */
Mark Rustad5b346dc2014-03-04 03:02:18 +00001842 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
1843 return; /* do nothing if already down */
Don Skidmore858c3dd2013-10-01 04:33:50 -07001844
1845 /* disable all enabled rx queues */
1846 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08001847 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001848
1849 netif_tx_disable(netdev);
1850
1851 msleep(10);
1852
1853 netif_tx_stop_all_queues(netdev);
1854
1855 ixgbevf_irq_disable(adapter);
1856
1857 ixgbevf_napi_disable_all(adapter);
1858
1859 del_timer_sync(&adapter->watchdog_timer);
1860 /* can't call flush scheduled work here because it can deadlock
1861 * if linkwatch_event tries to acquire the rtnl_lock which we are
1862 * holding */
1863 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1864 msleep(1);
1865
1866 /* disable transmits in the hardware now that interrupts are off */
1867 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmorede02dec2014-01-16 02:30:09 -08001868 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
1869
1870 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
1871 IXGBE_TXDCTL_SWFLSH);
Greg Rose92915f72010-01-09 02:24:10 +00001872 }
1873
1874 netif_carrier_off(netdev);
1875
1876 if (!pci_channel_offline(adapter->pdev))
1877 ixgbevf_reset(adapter);
1878
1879 ixgbevf_clean_all_tx_rings(adapter);
1880 ixgbevf_clean_all_rx_rings(adapter);
1881}
1882
1883void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1884{
1885 WARN_ON(in_interrupt());
Greg Rosec0456c22010-01-22 22:47:18 +00001886
Greg Rose92915f72010-01-09 02:24:10 +00001887 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1888 msleep(1);
1889
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001890 ixgbevf_down(adapter);
1891 ixgbevf_up(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001892
1893 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1894}
1895
1896void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1897{
1898 struct ixgbe_hw *hw = &adapter->hw;
1899 struct net_device *netdev = adapter->netdev;
1900
Don Skidmore798e3812013-10-01 04:33:51 -07001901 if (hw->mac.ops.reset_hw(hw)) {
Greg Rose92915f72010-01-09 02:24:10 +00001902 hw_dbg(hw, "PF still resetting\n");
Don Skidmore798e3812013-10-01 04:33:51 -07001903 } else {
Greg Rose92915f72010-01-09 02:24:10 +00001904 hw->mac.ops.init_hw(hw);
Don Skidmore798e3812013-10-01 04:33:51 -07001905 ixgbevf_negotiate_api(adapter);
1906 }
Greg Rose92915f72010-01-09 02:24:10 +00001907
1908 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1909 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1910 netdev->addr_len);
1911 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1912 netdev->addr_len);
1913 }
1914}
1915
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00001916static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1917 int vectors)
Greg Rose92915f72010-01-09 02:24:10 +00001918{
Emil Tantilova5f93372012-11-13 04:03:17 +00001919 int vector_threshold;
Greg Rose92915f72010-01-09 02:24:10 +00001920
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001921 /* We'll want at least 2 (vector_threshold):
1922 * 1) TxQ[0] + RxQ[0] handler
1923 * 2) Other (Link Status Change, etc.)
Greg Rose92915f72010-01-09 02:24:10 +00001924 */
1925 vector_threshold = MIN_MSIX_COUNT;
1926
1927 /* The more we get, the more we will assign to Tx/Rx Cleanup
1928 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1929 * Right now, we simply care about how many we'll get; we'll
1930 * set them up later while requesting irq's.
1931 */
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001932 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1933 vector_threshold, vectors);
Greg Rose92915f72010-01-09 02:24:10 +00001934
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001935 if (vectors < 0) {
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00001936 dev_err(&adapter->pdev->dev,
1937 "Unable to allocate MSI-X interrupts\n");
Greg Rose92915f72010-01-09 02:24:10 +00001938 kfree(adapter->msix_entries);
1939 adapter->msix_entries = NULL;
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001940 return vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001941 }
Greg Rosedee847f2012-11-02 05:50:57 +00001942
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001943 /* Adjust for only the vectors we'll use, which is minimum
1944 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1945 * vectors we were allocated.
1946 */
1947 adapter->num_msix_vectors = vectors;
1948
1949 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00001950}
1951
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001952/**
1953 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
Greg Rose92915f72010-01-09 02:24:10 +00001954 * @adapter: board private structure to initialize
1955 *
1956 * This is the top level queue allocation routine. The order here is very
1957 * important, starting with the "most" number of features turned on at once,
1958 * and ending with the smallest set of features. This way large combinations
1959 * can be allocated if they're turned on, and smaller combinations are the
1960 * fallthrough conditions.
1961 *
1962 **/
1963static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1964{
Don Skidmore220fe052013-09-21 01:40:49 +00001965 struct ixgbe_hw *hw = &adapter->hw;
1966 unsigned int def_q = 0;
1967 unsigned int num_tcs = 0;
1968 int err;
1969
Greg Rose92915f72010-01-09 02:24:10 +00001970 /* Start with base case */
1971 adapter->num_rx_queues = 1;
1972 adapter->num_tx_queues = 1;
Don Skidmore220fe052013-09-21 01:40:49 +00001973
1974 spin_lock_bh(&adapter->mbx_lock);
1975
1976 /* fetch queue configuration from the PF */
1977 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1978
1979 spin_unlock_bh(&adapter->mbx_lock);
1980
1981 if (err)
1982 return;
1983
1984 /* we need as many queues as traffic classes */
1985 if (num_tcs > 1)
1986 adapter->num_rx_queues = num_tcs;
Greg Rose92915f72010-01-09 02:24:10 +00001987}
1988
1989/**
1990 * ixgbevf_alloc_queues - Allocate memory for all rings
1991 * @adapter: board private structure to initialize
1992 *
1993 * We allocate one ring per queue at run-time since we don't know the
1994 * number of queues at compile-time. The polling_netdev array is
1995 * intended for Multiqueue, but should work fine with a single queue.
1996 **/
1997static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1998{
Don Skidmore87e70ab2014-01-16 02:30:08 -08001999 struct ixgbevf_ring *ring;
2000 int rx = 0, tx = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002001
Don Skidmore87e70ab2014-01-16 02:30:08 -08002002 for (; tx < adapter->num_tx_queues; tx++) {
2003 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2004 if (!ring)
2005 goto err_allocation;
Greg Rose92915f72010-01-09 02:24:10 +00002006
Don Skidmore87e70ab2014-01-16 02:30:08 -08002007 ring->dev = &adapter->pdev->dev;
2008 ring->netdev = adapter->netdev;
2009 ring->count = adapter->tx_ring_count;
2010 ring->queue_index = tx;
2011 ring->reg_idx = tx;
Greg Rose92915f72010-01-09 02:24:10 +00002012
Don Skidmore87e70ab2014-01-16 02:30:08 -08002013 adapter->tx_ring[tx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002014 }
2015
Don Skidmore87e70ab2014-01-16 02:30:08 -08002016 for (; rx < adapter->num_rx_queues; rx++) {
2017 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2018 if (!ring)
2019 goto err_allocation;
2020
2021 ring->dev = &adapter->pdev->dev;
2022 ring->netdev = adapter->netdev;
2023
2024 ring->count = adapter->rx_ring_count;
2025 ring->queue_index = rx;
2026 ring->reg_idx = rx;
2027
2028 adapter->rx_ring[rx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002029 }
2030
2031 return 0;
2032
Don Skidmore87e70ab2014-01-16 02:30:08 -08002033err_allocation:
2034 while (tx) {
2035 kfree(adapter->tx_ring[--tx]);
2036 adapter->tx_ring[tx] = NULL;
2037 }
2038
2039 while (rx) {
2040 kfree(adapter->rx_ring[--rx]);
2041 adapter->rx_ring[rx] = NULL;
2042 }
Greg Rose92915f72010-01-09 02:24:10 +00002043 return -ENOMEM;
2044}
2045
2046/**
2047 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2048 * @adapter: board private structure to initialize
2049 *
2050 * Attempt to configure the interrupts using the best available
2051 * capabilities of the hardware and the kernel.
2052 **/
2053static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2054{
Greg Rose91e2b892012-10-03 00:57:23 +00002055 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002056 int err = 0;
2057 int vector, v_budget;
2058
2059 /*
2060 * It's easy to be greedy for MSI-X vectors, but it really
2061 * doesn't do us much good if we have a lot more vectors
2062 * than CPU's. So let's be conservative and only ask for
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002063 * (roughly) the same number of vectors as there are CPU's.
2064 * The default is to use pairs of vectors.
Greg Rose92915f72010-01-09 02:24:10 +00002065 */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002066 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2067 v_budget = min_t(int, v_budget, num_online_cpus());
2068 v_budget += NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002069
2070 /* A failure in MSI-X entry allocation isn't fatal, but it does
2071 * mean we disable MSI-X capabilities of the adapter. */
2072 adapter->msix_entries = kcalloc(v_budget,
2073 sizeof(struct msix_entry), GFP_KERNEL);
2074 if (!adapter->msix_entries) {
2075 err = -ENOMEM;
2076 goto out;
2077 }
2078
2079 for (vector = 0; vector < v_budget; vector++)
2080 adapter->msix_entries[vector].entry = vector;
2081
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002082 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
2083 if (err)
2084 goto out;
Greg Rose92915f72010-01-09 02:24:10 +00002085
Greg Rose91e2b892012-10-03 00:57:23 +00002086 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
2087 if (err)
2088 goto out;
2089
2090 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
2091
Greg Rose92915f72010-01-09 02:24:10 +00002092out:
2093 return err;
2094}
2095
2096/**
2097 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2098 * @adapter: board private structure to initialize
2099 *
2100 * We allocate one q_vector per queue interrupt. If allocation fails we
2101 * return -ENOMEM.
2102 **/
2103static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2104{
2105 int q_idx, num_q_vectors;
2106 struct ixgbevf_q_vector *q_vector;
Greg Rose92915f72010-01-09 02:24:10 +00002107
2108 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002109
2110 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2111 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2112 if (!q_vector)
2113 goto err_out;
2114 q_vector->adapter = adapter;
2115 q_vector->v_idx = q_idx;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002116 netif_napi_add(adapter->netdev, &q_vector->napi,
2117 ixgbevf_poll, 64);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002118#ifdef CONFIG_NET_RX_BUSY_POLL
2119 napi_hash_add(&q_vector->napi);
2120#endif
Greg Rose92915f72010-01-09 02:24:10 +00002121 adapter->q_vector[q_idx] = q_vector;
2122 }
2123
2124 return 0;
2125
2126err_out:
2127 while (q_idx) {
2128 q_idx--;
2129 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002130#ifdef CONFIG_NET_RX_BUSY_POLL
2131 napi_hash_del(&q_vector->napi);
2132#endif
Greg Rose92915f72010-01-09 02:24:10 +00002133 netif_napi_del(&q_vector->napi);
2134 kfree(q_vector);
2135 adapter->q_vector[q_idx] = NULL;
2136 }
2137 return -ENOMEM;
2138}
2139
2140/**
2141 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2142 * @adapter: board private structure to initialize
2143 *
2144 * This function frees the memory allocated to the q_vectors. In addition if
2145 * NAPI is enabled it will delete any references to the NAPI struct prior
2146 * to freeing the q_vector.
2147 **/
2148static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2149{
John Fastabendf4477702012-09-16 08:19:46 +00002150 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002151
2152 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2153 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2154
2155 adapter->q_vector[q_idx] = NULL;
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002156#ifdef CONFIG_NET_RX_BUSY_POLL
2157 napi_hash_del(&q_vector->napi);
2158#endif
John Fastabendf4477702012-09-16 08:19:46 +00002159 netif_napi_del(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00002160 kfree(q_vector);
2161 }
2162}
2163
2164/**
2165 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2166 * @adapter: board private structure
2167 *
2168 **/
2169static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2170{
2171 pci_disable_msix(adapter->pdev);
2172 kfree(adapter->msix_entries);
2173 adapter->msix_entries = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00002174}
2175
2176/**
2177 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2178 * @adapter: board private structure to initialize
2179 *
2180 **/
2181static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2182{
2183 int err;
2184
2185 /* Number of supported queues */
2186 ixgbevf_set_num_queues(adapter);
2187
2188 err = ixgbevf_set_interrupt_capability(adapter);
2189 if (err) {
2190 hw_dbg(&adapter->hw,
2191 "Unable to setup interrupt capabilities\n");
2192 goto err_set_interrupt;
2193 }
2194
2195 err = ixgbevf_alloc_q_vectors(adapter);
2196 if (err) {
2197 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2198 "vectors\n");
2199 goto err_alloc_q_vectors;
2200 }
2201
2202 err = ixgbevf_alloc_queues(adapter);
2203 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002204 pr_err("Unable to allocate memory for queues\n");
Greg Rose92915f72010-01-09 02:24:10 +00002205 goto err_alloc_queues;
2206 }
2207
2208 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2209 "Tx Queue count = %u\n",
2210 (adapter->num_rx_queues > 1) ? "Enabled" :
2211 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2212
2213 set_bit(__IXGBEVF_DOWN, &adapter->state);
2214
2215 return 0;
2216err_alloc_queues:
2217 ixgbevf_free_q_vectors(adapter);
2218err_alloc_q_vectors:
2219 ixgbevf_reset_interrupt_capability(adapter);
2220err_set_interrupt:
2221 return err;
2222}
2223
2224/**
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002225 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2226 * @adapter: board private structure to clear interrupt scheme on
2227 *
2228 * We go through and clear interrupt specific resources and reset the structure
2229 * to pre-load conditions
2230 **/
2231static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2232{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002233 int i;
2234
2235 for (i = 0; i < adapter->num_tx_queues; i++) {
2236 kfree(adapter->tx_ring[i]);
2237 adapter->tx_ring[i] = NULL;
2238 }
2239 for (i = 0; i < adapter->num_rx_queues; i++) {
2240 kfree(adapter->rx_ring[i]);
2241 adapter->rx_ring[i] = NULL;
2242 }
2243
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002244 adapter->num_tx_queues = 0;
2245 adapter->num_rx_queues = 0;
2246
2247 ixgbevf_free_q_vectors(adapter);
2248 ixgbevf_reset_interrupt_capability(adapter);
2249}
2250
2251/**
Greg Rose92915f72010-01-09 02:24:10 +00002252 * ixgbevf_sw_init - Initialize general software structures
2253 * (struct ixgbevf_adapter)
2254 * @adapter: board private structure to initialize
2255 *
2256 * ixgbevf_sw_init initializes the Adapter private data structure.
2257 * Fields are initialized based on PCI device information and
2258 * OS network device settings (MTU size).
2259 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002260static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00002261{
2262 struct ixgbe_hw *hw = &adapter->hw;
2263 struct pci_dev *pdev = adapter->pdev;
Greg Rosee1941a72013-02-13 03:02:05 +00002264 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002265 int err;
2266
2267 /* PCI config space info */
2268
2269 hw->vendor_id = pdev->vendor;
2270 hw->device_id = pdev->device;
Sergei Shtylyovff938e42011-02-28 11:57:33 -08002271 hw->revision_id = pdev->revision;
Greg Rose92915f72010-01-09 02:24:10 +00002272 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2273 hw->subsystem_device_id = pdev->subsystem_device;
2274
2275 hw->mbx.ops.init_params(hw);
Alexander Duyck56e94092012-07-20 08:10:03 +00002276
2277 /* assume legacy case in which PF would only give VF 2 queues */
2278 hw->mac.max_tx_queues = 2;
2279 hw->mac.max_rx_queues = 2;
2280
Don Skidmore798e3812013-10-01 04:33:51 -07002281 /* lock to protect mailbox accesses */
2282 spin_lock_init(&adapter->mbx_lock);
2283
Greg Rose92915f72010-01-09 02:24:10 +00002284 err = hw->mac.ops.reset_hw(hw);
2285 if (err) {
2286 dev_info(&pdev->dev,
Greg Rosee1941a72013-02-13 03:02:05 +00002287 "PF still in reset state. Is the PF interface up?\n");
Greg Rose92915f72010-01-09 02:24:10 +00002288 } else {
2289 err = hw->mac.ops.init_hw(hw);
2290 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002291 pr_err("init_shared_code failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00002292 goto out;
2293 }
Don Skidmore798e3812013-10-01 04:33:51 -07002294 ixgbevf_negotiate_api(adapter);
Greg Rosee1941a72013-02-13 03:02:05 +00002295 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2296 if (err)
2297 dev_info(&pdev->dev, "Error reading MAC address\n");
2298 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2299 dev_info(&pdev->dev,
2300 "MAC address not assigned by administrator.\n");
2301 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2302 }
2303
2304 if (!is_valid_ether_addr(netdev->dev_addr)) {
2305 dev_info(&pdev->dev, "Assigning random MAC address\n");
2306 eth_hw_addr_random(netdev);
2307 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00002308 }
2309
2310 /* Enable dynamic interrupt throttling rates */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002311 adapter->rx_itr_setting = 1;
2312 adapter->tx_itr_setting = 1;
Greg Rose92915f72010-01-09 02:24:10 +00002313
Greg Rose92915f72010-01-09 02:24:10 +00002314 /* set default ring sizes */
2315 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2316 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2317
Greg Rose92915f72010-01-09 02:24:10 +00002318 set_bit(__IXGBEVF_DOWN, &adapter->state);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00002319 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002320
2321out:
2322 return err;
2323}
2324
Greg Rose92915f72010-01-09 02:24:10 +00002325#define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2326 { \
2327 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2328 if (current_counter < last_counter) \
2329 counter += 0x100000000LL; \
2330 last_counter = current_counter; \
2331 counter &= 0xFFFFFFFF00000000LL; \
2332 counter |= current_counter; \
2333 }
2334
2335#define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2336 { \
2337 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2338 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2339 u64 current_counter = (current_counter_msb << 32) | \
2340 current_counter_lsb; \
2341 if (current_counter < last_counter) \
2342 counter += 0x1000000000LL; \
2343 last_counter = current_counter; \
2344 counter &= 0xFFFFFFF000000000LL; \
2345 counter |= current_counter; \
2346 }
2347/**
2348 * ixgbevf_update_stats - Update the board statistics counters.
2349 * @adapter: board private structure
2350 **/
2351void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2352{
2353 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose55fb2772012-11-06 05:53:32 +00002354 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002355
Greg Rose088245a2013-01-04 07:37:31 +00002356 if (!adapter->link_up)
2357 return;
2358
Greg Rose92915f72010-01-09 02:24:10 +00002359 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2360 adapter->stats.vfgprc);
2361 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2362 adapter->stats.vfgptc);
2363 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2364 adapter->stats.last_vfgorc,
2365 adapter->stats.vfgorc);
2366 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2367 adapter->stats.last_vfgotc,
2368 adapter->stats.vfgotc);
2369 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2370 adapter->stats.vfmprc);
Greg Rose55fb2772012-11-06 05:53:32 +00002371
2372 for (i = 0; i < adapter->num_rx_queues; i++) {
2373 adapter->hw_csum_rx_error +=
Don Skidmore87e70ab2014-01-16 02:30:08 -08002374 adapter->rx_ring[i]->hw_csum_rx_error;
Don Skidmore87e70ab2014-01-16 02:30:08 -08002375 adapter->rx_ring[i]->hw_csum_rx_error = 0;
Greg Rose55fb2772012-11-06 05:53:32 +00002376 }
Greg Rose92915f72010-01-09 02:24:10 +00002377}
2378
2379/**
2380 * ixgbevf_watchdog - Timer Call-back
2381 * @data: pointer to adapter cast into an unsigned long
2382 **/
2383static void ixgbevf_watchdog(unsigned long data)
2384{
2385 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2386 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002387 u32 eics = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002388 int i;
2389
2390 /*
2391 * Do the watchdog outside of interrupt context due to the lovely
2392 * delays that some of the newer hardware requires
2393 */
2394
2395 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2396 goto watchdog_short_circuit;
2397
2398 /* get one bit for every active tx/rx interrupt vector */
2399 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2400 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00002401 if (qv->rx.ring || qv->tx.ring)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002402 eics |= 1 << i;
Greg Rose92915f72010-01-09 02:24:10 +00002403 }
2404
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002405 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
Greg Rose92915f72010-01-09 02:24:10 +00002406
2407watchdog_short_circuit:
2408 schedule_work(&adapter->watchdog_task);
2409}
2410
2411/**
2412 * ixgbevf_tx_timeout - Respond to a Tx Hang
2413 * @netdev: network interface device structure
2414 **/
2415static void ixgbevf_tx_timeout(struct net_device *netdev)
2416{
2417 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2418
2419 /* Do the reset outside of interrupt context */
2420 schedule_work(&adapter->reset_task);
2421}
2422
2423static void ixgbevf_reset_task(struct work_struct *work)
2424{
2425 struct ixgbevf_adapter *adapter;
2426 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2427
2428 /* If we're already down or resetting, just bail */
2429 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002430 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
Greg Rose92915f72010-01-09 02:24:10 +00002431 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2432 return;
2433
2434 adapter->tx_timeout_count++;
2435
2436 ixgbevf_reinit_locked(adapter);
2437}
2438
2439/**
2440 * ixgbevf_watchdog_task - worker thread to bring link up
2441 * @work: pointer to work_struct containing our data
2442 **/
2443static void ixgbevf_watchdog_task(struct work_struct *work)
2444{
2445 struct ixgbevf_adapter *adapter = container_of(work,
2446 struct ixgbevf_adapter,
2447 watchdog_task);
2448 struct net_device *netdev = adapter->netdev;
2449 struct ixgbe_hw *hw = &adapter->hw;
2450 u32 link_speed = adapter->link_speed;
2451 bool link_up = adapter->link_up;
Greg Rose92fe0bf2012-11-02 05:50:47 +00002452 s32 need_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002453
Mark Rustad26597802014-03-04 03:02:45 +00002454 if (IXGBE_REMOVED(hw->hw_addr)) {
2455 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
2456 rtnl_lock();
2457 ixgbevf_down(adapter);
2458 rtnl_unlock();
2459 }
2460 return;
2461 }
Don Skidmore220fe052013-09-21 01:40:49 +00002462 ixgbevf_queue_reset_subtask(adapter);
2463
Greg Rose92915f72010-01-09 02:24:10 +00002464 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2465
2466 /*
2467 * Always check the link on the watchdog because we have
2468 * no LSC interrupt
2469 */
Greg Rose92fe0bf2012-11-02 05:50:47 +00002470 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002471
Greg Rose92fe0bf2012-11-02 05:50:47 +00002472 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002473
Greg Rose92fe0bf2012-11-02 05:50:47 +00002474 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002475
Greg Rose92fe0bf2012-11-02 05:50:47 +00002476 if (need_reset) {
2477 adapter->link_up = link_up;
2478 adapter->link_speed = link_speed;
2479 netif_carrier_off(netdev);
2480 netif_tx_stop_all_queues(netdev);
2481 schedule_work(&adapter->reset_task);
2482 goto pf_has_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002483 }
2484 adapter->link_up = link_up;
2485 adapter->link_speed = link_speed;
2486
2487 if (link_up) {
2488 if (!netif_carrier_ok(netdev)) {
Greg Roseb876a742013-01-19 06:40:22 +00002489 char *link_speed_string;
2490 switch (link_speed) {
2491 case IXGBE_LINK_SPEED_10GB_FULL:
2492 link_speed_string = "10 Gbps";
2493 break;
2494 case IXGBE_LINK_SPEED_1GB_FULL:
2495 link_speed_string = "1 Gbps";
2496 break;
2497 case IXGBE_LINK_SPEED_100_FULL:
2498 link_speed_string = "100 Mbps";
2499 break;
2500 default:
2501 link_speed_string = "unknown speed";
2502 break;
2503 }
Greg Rose6fe59672013-01-04 07:37:26 +00002504 dev_info(&adapter->pdev->dev,
Greg Roseb876a742013-01-19 06:40:22 +00002505 "NIC Link is Up, %s\n", link_speed_string);
Greg Rose92915f72010-01-09 02:24:10 +00002506 netif_carrier_on(netdev);
2507 netif_tx_wake_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002508 }
2509 } else {
2510 adapter->link_up = false;
2511 adapter->link_speed = 0;
2512 if (netif_carrier_ok(netdev)) {
Greg Rose6fe59672013-01-04 07:37:26 +00002513 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
Greg Rose92915f72010-01-09 02:24:10 +00002514 netif_carrier_off(netdev);
2515 netif_tx_stop_all_queues(netdev);
2516 }
2517 }
2518
Greg Rose92915f72010-01-09 02:24:10 +00002519 ixgbevf_update_stats(adapter);
2520
Greg Rose33bd9f62010-03-19 02:59:52 +00002521pf_has_reset:
Greg Rose92915f72010-01-09 02:24:10 +00002522 /* Reset the timer */
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002523 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
2524 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Greg Rose92915f72010-01-09 02:24:10 +00002525 mod_timer(&adapter->watchdog_timer,
2526 round_jiffies(jiffies + (2 * HZ)));
2527
2528 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2529}
2530
2531/**
2532 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
Greg Rose92915f72010-01-09 02:24:10 +00002533 * @tx_ring: Tx descriptor ring for a specific queue
2534 *
2535 * Free all transmit software resources
2536 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002537void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002538{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002539 ixgbevf_clean_tx_ring(tx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002540
2541 vfree(tx_ring->tx_buffer_info);
2542 tx_ring->tx_buffer_info = NULL;
2543
Don Skidmorede02dec2014-01-16 02:30:09 -08002544 /* if not set, then don't free */
2545 if (!tx_ring->desc)
2546 return;
2547
Emil Tantilov05d063a2014-01-17 18:29:59 -08002548 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002549 tx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002550
2551 tx_ring->desc = NULL;
2552}
2553
2554/**
2555 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2556 * @adapter: board private structure
2557 *
2558 * Free all transmit software resources
2559 **/
2560static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2561{
2562 int i;
2563
2564 for (i = 0; i < adapter->num_tx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002565 if (adapter->tx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002566 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002567}
2568
2569/**
2570 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002571 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2572 *
2573 * Return 0 on success, negative on failure
2574 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002575int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002576{
Greg Rose92915f72010-01-09 02:24:10 +00002577 int size;
2578
2579 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002580 tx_ring->tx_buffer_info = vzalloc(size);
Greg Rose92915f72010-01-09 02:24:10 +00002581 if (!tx_ring->tx_buffer_info)
2582 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002583
2584 /* round up to nearest 4K */
2585 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2586 tx_ring->size = ALIGN(tx_ring->size, 4096);
2587
Emil Tantilov05d063a2014-01-17 18:29:59 -08002588 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002589 &tx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002590 if (!tx_ring->desc)
2591 goto err;
2592
Greg Rose92915f72010-01-09 02:24:10 +00002593 return 0;
2594
2595err:
2596 vfree(tx_ring->tx_buffer_info);
2597 tx_ring->tx_buffer_info = NULL;
2598 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2599 "descriptor ring\n");
2600 return -ENOMEM;
2601}
2602
2603/**
2604 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2605 * @adapter: board private structure
2606 *
2607 * If this function returns with an error, then it's possible one or
2608 * more of the rings is populated (while the rest are not). It is the
2609 * callers duty to clean those orphaned rings.
2610 *
2611 * Return 0 on success, negative on failure
2612 **/
2613static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2614{
2615 int i, err = 0;
2616
2617 for (i = 0; i < adapter->num_tx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002618 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002619 if (!err)
2620 continue;
2621 hw_dbg(&adapter->hw,
2622 "Allocation for Tx Queue %u failed\n", i);
2623 break;
2624 }
2625
2626 return err;
2627}
2628
2629/**
2630 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002631 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2632 *
2633 * Returns 0 on success, negative on failure
2634 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002635int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002636{
Greg Rose92915f72010-01-09 02:24:10 +00002637 int size;
2638
2639 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002640 rx_ring->rx_buffer_info = vzalloc(size);
Joe Perchese404dec2012-01-29 12:56:23 +00002641 if (!rx_ring->rx_buffer_info)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002642 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002643
2644 /* Round up to nearest 4K */
2645 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2646 rx_ring->size = ALIGN(rx_ring->size, 4096);
2647
Emil Tantilov05d063a2014-01-17 18:29:59 -08002648 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002649 &rx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002650
Emil Tantilov05d063a2014-01-17 18:29:59 -08002651 if (!rx_ring->desc)
2652 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002653
Greg Rose92915f72010-01-09 02:24:10 +00002654 return 0;
Emil Tantilov05d063a2014-01-17 18:29:59 -08002655err:
2656 vfree(rx_ring->rx_buffer_info);
2657 rx_ring->rx_buffer_info = NULL;
2658 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
Greg Rose92915f72010-01-09 02:24:10 +00002659 return -ENOMEM;
2660}
2661
2662/**
2663 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2664 * @adapter: board private structure
2665 *
2666 * If this function returns with an error, then it's possible one or
2667 * more of the rings is populated (while the rest are not). It is the
2668 * callers duty to clean those orphaned rings.
2669 *
2670 * Return 0 on success, negative on failure
2671 **/
2672static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2673{
2674 int i, err = 0;
2675
2676 for (i = 0; i < adapter->num_rx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002677 err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002678 if (!err)
2679 continue;
2680 hw_dbg(&adapter->hw,
2681 "Allocation for Rx Queue %u failed\n", i);
2682 break;
2683 }
2684 return err;
2685}
2686
2687/**
2688 * ixgbevf_free_rx_resources - Free Rx Resources
Greg Rose92915f72010-01-09 02:24:10 +00002689 * @rx_ring: ring to clean the resources from
2690 *
2691 * Free all receive software resources
2692 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002693void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002694{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002695 ixgbevf_clean_rx_ring(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002696
2697 vfree(rx_ring->rx_buffer_info);
2698 rx_ring->rx_buffer_info = NULL;
2699
Emil Tantilov05d063a2014-01-17 18:29:59 -08002700 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002701 rx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002702
2703 rx_ring->desc = NULL;
2704}
2705
2706/**
2707 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2708 * @adapter: board private structure
2709 *
2710 * Free all receive software resources
2711 **/
2712static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2713{
2714 int i;
2715
2716 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002717 if (adapter->rx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002718 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002719}
2720
2721/**
2722 * ixgbevf_open - Called when a network interface is made active
2723 * @netdev: network interface device structure
2724 *
2725 * Returns 0 on success, negative value on failure
2726 *
2727 * The open entry point is called when a network interface is made
2728 * active by the system (IFF_UP). At this point all resources needed
2729 * for transmit and receive operations are allocated, the interrupt
2730 * handler is registered with the OS, the watchdog timer is started,
2731 * and the stack is notified that the interface is ready.
2732 **/
2733static int ixgbevf_open(struct net_device *netdev)
2734{
2735 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2736 struct ixgbe_hw *hw = &adapter->hw;
2737 int err;
2738
xunleera1f6c6b2013-03-05 07:44:20 +00002739 /* A previous failure to open the device because of a lack of
2740 * available MSIX vector resources may have reset the number
2741 * of msix vectors variable to zero. The only way to recover
2742 * is to unload/reload the driver and hope that the system has
2743 * been able to recover some MSIX vector resources.
2744 */
2745 if (!adapter->num_msix_vectors)
2746 return -ENOMEM;
2747
Greg Rose92915f72010-01-09 02:24:10 +00002748 /* disallow open during test */
2749 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2750 return -EBUSY;
2751
2752 if (hw->adapter_stopped) {
2753 ixgbevf_reset(adapter);
2754 /* if adapter is still stopped then PF isn't up and
2755 * the vf can't start. */
2756 if (hw->adapter_stopped) {
2757 err = IXGBE_ERR_MBX;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002758 pr_err("Unable to start - perhaps the PF Driver isn't "
2759 "up yet\n");
Greg Rose92915f72010-01-09 02:24:10 +00002760 goto err_setup_reset;
2761 }
2762 }
2763
2764 /* allocate transmit descriptors */
2765 err = ixgbevf_setup_all_tx_resources(adapter);
2766 if (err)
2767 goto err_setup_tx;
2768
2769 /* allocate receive descriptors */
2770 err = ixgbevf_setup_all_rx_resources(adapter);
2771 if (err)
2772 goto err_setup_rx;
2773
2774 ixgbevf_configure(adapter);
2775
2776 /*
2777 * Map the Tx/Rx rings to the vectors we were allotted.
2778 * if request_irq will be called in this function map_rings
2779 * must be called *before* up_complete
2780 */
2781 ixgbevf_map_rings_to_vectors(adapter);
2782
Greg Rose795180d2012-04-17 04:29:34 +00002783 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002784
2785 /* clear any pending interrupts, may auto mask */
2786 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2787 err = ixgbevf_request_irq(adapter);
2788 if (err)
2789 goto err_req_irq;
2790
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002791 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002792
2793 return 0;
2794
2795err_req_irq:
2796 ixgbevf_down(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002797err_setup_rx:
2798 ixgbevf_free_all_rx_resources(adapter);
2799err_setup_tx:
2800 ixgbevf_free_all_tx_resources(adapter);
2801 ixgbevf_reset(adapter);
2802
2803err_setup_reset:
2804
2805 return err;
2806}
2807
2808/**
2809 * ixgbevf_close - Disables a network interface
2810 * @netdev: network interface device structure
2811 *
2812 * Returns 0, this is not allowed to fail
2813 *
2814 * The close entry point is called when an interface is de-activated
2815 * by the OS. The hardware is still under the drivers control, but
2816 * needs to be disabled. A global MAC reset is issued to stop the
2817 * hardware, and all transmit and receive resources are freed.
2818 **/
2819static int ixgbevf_close(struct net_device *netdev)
2820{
2821 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2822
2823 ixgbevf_down(adapter);
2824 ixgbevf_free_irq(adapter);
2825
2826 ixgbevf_free_all_tx_resources(adapter);
2827 ixgbevf_free_all_rx_resources(adapter);
2828
2829 return 0;
2830}
2831
Don Skidmore220fe052013-09-21 01:40:49 +00002832static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
2833{
2834 struct net_device *dev = adapter->netdev;
2835
2836 if (!(adapter->flags & IXGBEVF_FLAG_QUEUE_RESET_REQUESTED))
2837 return;
2838
2839 adapter->flags &= ~IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
2840
2841 /* if interface is down do nothing */
2842 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2843 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2844 return;
2845
2846 /* Hardware has to reinitialize queues and interrupts to
2847 * match packet buffer alignment. Unfortunately, the
2848 * hardware is not flexible enough to do this dynamically.
2849 */
2850 if (netif_running(dev))
2851 ixgbevf_close(dev);
2852
2853 ixgbevf_clear_interrupt_scheme(adapter);
2854 ixgbevf_init_interrupt_scheme(adapter);
2855
2856 if (netif_running(dev))
2857 ixgbevf_open(dev);
2858}
2859
Alexander Duyck70a10e22012-05-11 08:33:21 +00002860static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2861 u32 vlan_macip_lens, u32 type_tucmd,
2862 u32 mss_l4len_idx)
2863{
2864 struct ixgbe_adv_tx_context_desc *context_desc;
2865 u16 i = tx_ring->next_to_use;
2866
2867 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2868
2869 i++;
2870 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2871
2872 /* set bits to identify this as an advanced context descriptor */
2873 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2874
2875 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2876 context_desc->seqnum_seed = 0;
2877 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2878 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2879}
2880
2881static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002882 struct ixgbevf_tx_buffer *first,
2883 u8 *hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00002884{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002885 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002886 u32 vlan_macip_lens, type_tucmd;
Greg Rose92915f72010-01-09 02:24:10 +00002887 u32 mss_l4len_idx, l4len;
Francois Romieu8f12c032014-03-30 03:14:32 +00002888 int err;
Greg Rose92915f72010-01-09 02:24:10 +00002889
Emil Tantilov01a545c2014-02-27 20:32:45 -08002890 if (skb->ip_summed != CHECKSUM_PARTIAL)
2891 return 0;
2892
Alexander Duyck70a10e22012-05-11 08:33:21 +00002893 if (!skb_is_gso(skb))
2894 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002895
Francois Romieu8f12c032014-03-30 03:14:32 +00002896 err = skb_cow_head(skb, 0);
2897 if (err < 0)
2898 return err;
Greg Rose92915f72010-01-09 02:24:10 +00002899
Alexander Duyck70a10e22012-05-11 08:33:21 +00002900 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2901 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2902
2903 if (skb->protocol == htons(ETH_P_IP)) {
2904 struct iphdr *iph = ip_hdr(skb);
2905 iph->tot_len = 0;
2906 iph->check = 0;
2907 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2908 iph->daddr, 0,
2909 IPPROTO_TCP,
2910 0);
2911 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002912 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
2913 IXGBE_TX_FLAGS_CSUM |
2914 IXGBE_TX_FLAGS_IPV4;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002915 } else if (skb_is_gso_v6(skb)) {
2916 ipv6_hdr(skb)->payload_len = 0;
2917 tcp_hdr(skb)->check =
2918 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2919 &ipv6_hdr(skb)->daddr,
2920 0, IPPROTO_TCP, 0);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002921 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
2922 IXGBE_TX_FLAGS_CSUM;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002923 }
2924
2925 /* compute header lengths */
2926 l4len = tcp_hdrlen(skb);
2927 *hdr_len += l4len;
2928 *hdr_len = skb_transport_offset(skb) + l4len;
2929
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002930 /* update gso size and bytecount with header size */
2931 first->gso_segs = skb_shinfo(skb)->gso_segs;
2932 first->bytecount += (first->gso_segs - 1) * *hdr_len;
2933
Alexander Duyck70a10e22012-05-11 08:33:21 +00002934 /* mss_l4len_id: use 1 as index for TSO */
2935 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2936 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2937 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2938
2939 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2940 vlan_macip_lens = skb_network_header_len(skb);
2941 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002942 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002943
2944 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2945 type_tucmd, mss_l4len_idx);
2946
2947 return 1;
Greg Rose92915f72010-01-09 02:24:10 +00002948}
2949
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002950static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2951 struct ixgbevf_tx_buffer *first)
Greg Rose92915f72010-01-09 02:24:10 +00002952{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002953 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002954 u32 vlan_macip_lens = 0;
2955 u32 mss_l4len_idx = 0;
2956 u32 type_tucmd = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002957
Alexander Duyck70a10e22012-05-11 08:33:21 +00002958 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2959 u8 l4_hdr = 0;
2960 switch (skb->protocol) {
Joe Perches0933ce42014-03-13 05:19:30 +00002961 case htons(ETH_P_IP):
Alexander Duyck70a10e22012-05-11 08:33:21 +00002962 vlan_macip_lens |= skb_network_header_len(skb);
2963 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2964 l4_hdr = ip_hdr(skb)->protocol;
2965 break;
Joe Perches0933ce42014-03-13 05:19:30 +00002966 case htons(ETH_P_IPV6):
Alexander Duyck70a10e22012-05-11 08:33:21 +00002967 vlan_macip_lens |= skb_network_header_len(skb);
2968 l4_hdr = ipv6_hdr(skb)->nexthdr;
2969 break;
2970 default:
2971 if (unlikely(net_ratelimit())) {
2972 dev_warn(tx_ring->dev,
2973 "partial checksum but proto=%x!\n",
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002974 first->protocol);
Greg Rose92915f72010-01-09 02:24:10 +00002975 }
Alexander Duyck70a10e22012-05-11 08:33:21 +00002976 break;
Greg Rose92915f72010-01-09 02:24:10 +00002977 }
2978
Alexander Duyck70a10e22012-05-11 08:33:21 +00002979 switch (l4_hdr) {
2980 case IPPROTO_TCP:
2981 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2982 mss_l4len_idx = tcp_hdrlen(skb) <<
2983 IXGBE_ADVTXD_L4LEN_SHIFT;
2984 break;
2985 case IPPROTO_SCTP:
2986 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2987 mss_l4len_idx = sizeof(struct sctphdr) <<
2988 IXGBE_ADVTXD_L4LEN_SHIFT;
2989 break;
2990 case IPPROTO_UDP:
2991 mss_l4len_idx = sizeof(struct udphdr) <<
2992 IXGBE_ADVTXD_L4LEN_SHIFT;
2993 break;
2994 default:
2995 if (unlikely(net_ratelimit())) {
2996 dev_warn(tx_ring->dev,
2997 "partial checksum but l4 proto=%x!\n",
2998 l4_hdr);
2999 }
3000 break;
3001 }
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003002
3003 /* update TX checksum flag */
3004 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003005 }
3006
Alexander Duyck70a10e22012-05-11 08:33:21 +00003007 /* vlan_macip_lens: MACLEN, VLAN tag */
3008 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003009 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00003010
3011 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3012 type_tucmd, mss_l4len_idx);
Greg Rose92915f72010-01-09 02:24:10 +00003013}
3014
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003015static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3016{
3017 /* set type for advanced descriptor with frame checksum insertion */
3018 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3019 IXGBE_ADVTXD_DCMD_IFCS |
3020 IXGBE_ADVTXD_DCMD_DEXT);
3021
3022 /* set HW vlan bit if vlan is present */
3023 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3024 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3025
3026 /* set segmentation enable bits for TSO/FSO */
3027 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3028 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3029
3030 return cmd_type;
3031}
3032
3033static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3034 u32 tx_flags, unsigned int paylen)
3035{
3036 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3037
3038 /* enable L4 checksum for TSO and TX checksum offload */
3039 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3040 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3041
3042 /* enble IPv4 checksum for TSO */
3043 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3044 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3045
3046 /* use index 1 context for TSO/FSO/FCOE */
3047 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3048 olinfo_status |= cpu_to_le32(1 << IXGBE_ADVTXD_IDX_SHIFT);
3049
3050 /* Check Context must be set if Tx switch is enabled, which it
3051 * always is for case where virtual functions are running
3052 */
3053 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3054
3055 tx_desc->read.olinfo_status = olinfo_status;
3056}
3057
3058static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3059 struct ixgbevf_tx_buffer *first,
3060 const u8 hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003061{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003062 dma_addr_t dma;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003063 struct sk_buff *skb = first->skb;
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003064 struct ixgbevf_tx_buffer *tx_buffer;
3065 union ixgbe_adv_tx_desc *tx_desc;
3066 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
3067 unsigned int data_len = skb->data_len;
3068 unsigned int size = skb_headlen(skb);
3069 unsigned int paylen = skb->len - hdr_len;
3070 u32 tx_flags = first->tx_flags;
3071 __le32 cmd_type;
3072 u16 i = tx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +00003073
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003074 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +00003075
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003076 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, paylen);
3077 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
Greg Rose92915f72010-01-09 02:24:10 +00003078
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003079 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3080 if (dma_mapping_error(tx_ring->dev, dma))
3081 goto dma_error;
3082
3083 /* record length, and DMA address */
3084 dma_unmap_len_set(first, len, size);
3085 dma_unmap_addr_set(first, dma, dma);
3086
3087 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3088
3089 for (;;) {
3090 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3091 tx_desc->read.cmd_type_len =
3092 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3093
3094 i++;
3095 tx_desc++;
3096 if (i == tx_ring->count) {
3097 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3098 i = 0;
3099 }
3100
3101 dma += IXGBE_MAX_DATA_PER_TXD;
3102 size -= IXGBE_MAX_DATA_PER_TXD;
3103
3104 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3105 tx_desc->read.olinfo_status = 0;
3106 }
3107
3108 if (likely(!data_len))
3109 break;
3110
3111 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3112
3113 i++;
3114 tx_desc++;
3115 if (i == tx_ring->count) {
3116 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3117 i = 0;
3118 }
3119
3120 size = skb_frag_size(frag);
3121 data_len -= size;
3122
3123 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3124 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003125 if (dma_mapping_error(tx_ring->dev, dma))
Greg Rose92915f72010-01-09 02:24:10 +00003126 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00003127
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003128 tx_buffer = &tx_ring->tx_buffer_info[i];
3129 dma_unmap_len_set(tx_buffer, len, size);
3130 dma_unmap_addr_set(tx_buffer, dma, dma);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003131
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003132 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3133 tx_desc->read.olinfo_status = 0;
3134
3135 frag++;
Greg Rose92915f72010-01-09 02:24:10 +00003136 }
3137
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003138 /* write last descriptor with RS and EOP bits */
3139 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3140 tx_desc->read.cmd_type_len = cmd_type;
Greg Rose92915f72010-01-09 02:24:10 +00003141
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003142 /* set the timestamp */
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003143 first->time_stamp = jiffies;
Greg Rose92915f72010-01-09 02:24:10 +00003144
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003145 /* Force memory writes to complete before letting h/w know there
3146 * are new descriptors to fetch. (Only applicable for weak-ordered
3147 * memory model archs, such as IA-64).
3148 *
3149 * We also need this memory barrier (wmb) to make certain all of the
3150 * status bits have been updated before next_to_watch is written.
3151 */
3152 wmb();
Greg Rose92915f72010-01-09 02:24:10 +00003153
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003154 /* set next_to_watch value indicating a packet is present */
3155 first->next_to_watch = tx_desc;
3156
3157 i++;
3158 if (i == tx_ring->count)
3159 i = 0;
3160
3161 tx_ring->next_to_use = i;
3162
3163 /* notify HW of packet */
Mark Rustad06380db2014-03-04 03:02:23 +00003164 ixgbevf_write_tail(tx_ring, i);
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003165
3166 return;
Greg Rose92915f72010-01-09 02:24:10 +00003167dma_error:
Alexander Duyck70a10e22012-05-11 08:33:21 +00003168 dev_err(tx_ring->dev, "TX DMA map failed\n");
Greg Rose92915f72010-01-09 02:24:10 +00003169
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003170 /* clear dma mappings for failed tx_buffer_info map */
3171 for (;;) {
3172 tx_buffer = &tx_ring->tx_buffer_info[i];
3173 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer);
3174 if (tx_buffer == first)
3175 break;
3176 if (i == 0)
3177 i = tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +00003178 i--;
Greg Rose92915f72010-01-09 02:24:10 +00003179 }
3180
Greg Rose92915f72010-01-09 02:24:10 +00003181 tx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +00003182}
3183
Alexander Duyckfb401952012-05-11 08:33:16 +00003184static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003185{
Alexander Duyckfb401952012-05-11 08:33:16 +00003186 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00003187 /* Herbert's original patch had:
3188 * smp_mb__after_netif_stop_queue();
3189 * but since that doesn't exist yet, just open code it. */
3190 smp_mb();
3191
3192 /* We need to check again in a case another CPU has just
3193 * made room available. */
Don Skidmoref880d072013-10-23 02:17:52 +00003194 if (likely(ixgbevf_desc_unused(tx_ring) < size))
Greg Rose92915f72010-01-09 02:24:10 +00003195 return -EBUSY;
3196
3197 /* A reprieve! - use start_queue because it doesn't call schedule */
Alexander Duyckfb401952012-05-11 08:33:16 +00003198 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
Emil Tantilov095e2612014-01-17 18:30:00 -08003199 ++tx_ring->tx_stats.restart_queue;
3200
Greg Rose92915f72010-01-09 02:24:10 +00003201 return 0;
3202}
3203
Alexander Duyckfb401952012-05-11 08:33:16 +00003204static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003205{
Don Skidmoref880d072013-10-23 02:17:52 +00003206 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
Greg Rose92915f72010-01-09 02:24:10 +00003207 return 0;
Alexander Duyckfb401952012-05-11 08:33:16 +00003208 return __ixgbevf_maybe_stop_tx(tx_ring, size);
Greg Rose92915f72010-01-09 02:24:10 +00003209}
3210
3211static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3212{
3213 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003214 struct ixgbevf_tx_buffer *first;
Greg Rose92915f72010-01-09 02:24:10 +00003215 struct ixgbevf_ring *tx_ring;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003216 int tso;
3217 u32 tx_flags = 0;
Alexander Duyck35959902012-05-11 08:32:40 +00003218 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3219#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3220 unsigned short f;
3221#endif
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003222 u8 hdr_len = 0;
Greg Rosef9d08f162012-10-02 00:50:52 +00003223 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003224
Ben Hutchings46acc462012-11-01 09:11:11 +00003225 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
Greg Rosef9d08f162012-10-02 00:50:52 +00003226 dev_kfree_skb(skb);
3227 return NETDEV_TX_OK;
3228 }
Greg Rose92915f72010-01-09 02:24:10 +00003229
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003230 tx_ring = adapter->tx_ring[skb->queue_mapping];
Greg Rose92915f72010-01-09 02:24:10 +00003231
Alexander Duyck35959902012-05-11 08:32:40 +00003232 /*
3233 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3234 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3235 * + 2 desc gap to keep tail from touching head,
3236 * + 1 desc for context descriptor,
3237 * otherwise try next time
3238 */
3239#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3240 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3241 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3242#else
3243 count += skb_shinfo(skb)->nr_frags;
3244#endif
Alexander Duyckfb401952012-05-11 08:33:16 +00003245 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
Emil Tantilov095e2612014-01-17 18:30:00 -08003246 tx_ring->tx_stats.tx_busy++;
Alexander Duyck35959902012-05-11 08:32:40 +00003247 return NETDEV_TX_BUSY;
3248 }
3249
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003250 /* record the location of the first descriptor for this packet */
3251 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
3252 first->skb = skb;
3253 first->bytecount = skb->len;
3254 first->gso_segs = 1;
3255
Jesse Grosseab6d182010-10-20 13:56:03 +00003256 if (vlan_tx_tag_present(skb)) {
Greg Rose92915f72010-01-09 02:24:10 +00003257 tx_flags |= vlan_tx_tag_get(skb);
3258 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3259 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3260 }
3261
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003262 /* record initial flags and protocol */
3263 first->tx_flags = tx_flags;
3264 first->protocol = vlan_get_protocol(skb);
Greg Rose92915f72010-01-09 02:24:10 +00003265
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003266 tso = ixgbevf_tso(tx_ring, first, &hdr_len);
3267 if (tso < 0)
3268 goto out_drop;
Emil Tantilovb5d217f2014-02-27 20:32:44 -08003269 else if (!tso)
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003270 ixgbevf_tx_csum(tx_ring, first);
Greg Rose92915f72010-01-09 02:24:10 +00003271
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003272 ixgbevf_tx_map(tx_ring, first, hdr_len);
Greg Rose92915f72010-01-09 02:24:10 +00003273
Alexander Duyckfb401952012-05-11 08:33:16 +00003274 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Greg Rose92915f72010-01-09 02:24:10 +00003275
3276 return NETDEV_TX_OK;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003277
3278out_drop:
3279 dev_kfree_skb_any(first->skb);
3280 first->skb = NULL;
3281
3282 return NETDEV_TX_OK;
Greg Rose92915f72010-01-09 02:24:10 +00003283}
3284
3285/**
Greg Rose92915f72010-01-09 02:24:10 +00003286 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3287 * @netdev: network interface device structure
3288 * @p: pointer to an address structure
3289 *
3290 * Returns 0 on success, negative on failure
3291 **/
3292static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3293{
3294 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3295 struct ixgbe_hw *hw = &adapter->hw;
3296 struct sockaddr *addr = p;
3297
3298 if (!is_valid_ether_addr(addr->sa_data))
3299 return -EADDRNOTAVAIL;
3300
3301 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3302 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3303
John Fastabend55fdd45b2012-10-01 14:52:20 +00003304 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003305
Greg Rose92fe0bf2012-11-02 05:50:47 +00003306 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00003307
John Fastabend55fdd45b2012-10-01 14:52:20 +00003308 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003309
Greg Rose92915f72010-01-09 02:24:10 +00003310 return 0;
3311}
3312
3313/**
3314 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3315 * @netdev: network interface device structure
3316 * @new_mtu: new value for maximum frame size
3317 *
3318 * Returns 0 on success, negative on failure
3319 **/
3320static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3321{
3322 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3323 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Greg Rose69bfbec2011-01-26 01:06:12 +00003324 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose69bfbec2011-01-26 01:06:12 +00003325
Alexander Duyck56e94092012-07-20 08:10:03 +00003326 switch (adapter->hw.api_version) {
3327 case ixgbe_mbox_api_11:
Greg Rose69bfbec2011-01-26 01:06:12 +00003328 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
Alexander Duyck56e94092012-07-20 08:10:03 +00003329 break;
3330 default:
3331 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3332 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3333 break;
3334 }
Greg Rose92915f72010-01-09 02:24:10 +00003335
3336 /* MTU < 68 is an error and causes problems on some kernels */
Greg Rose69bfbec2011-01-26 01:06:12 +00003337 if ((new_mtu < 68) || (max_frame > max_possible_frame))
Greg Rose92915f72010-01-09 02:24:10 +00003338 return -EINVAL;
3339
3340 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3341 netdev->mtu, new_mtu);
3342 /* must set new MTU before calling down or up */
3343 netdev->mtu = new_mtu;
3344
3345 if (netif_running(netdev))
3346 ixgbevf_reinit_locked(adapter);
3347
3348 return 0;
3349}
3350
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003351static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
Greg Rose92915f72010-01-09 02:24:10 +00003352{
3353 struct net_device *netdev = pci_get_drvdata(pdev);
3354 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003355#ifdef CONFIG_PM
3356 int retval = 0;
3357#endif
Greg Rose92915f72010-01-09 02:24:10 +00003358
3359 netif_device_detach(netdev);
3360
3361 if (netif_running(netdev)) {
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003362 rtnl_lock();
Greg Rose92915f72010-01-09 02:24:10 +00003363 ixgbevf_down(adapter);
3364 ixgbevf_free_irq(adapter);
3365 ixgbevf_free_all_tx_resources(adapter);
3366 ixgbevf_free_all_rx_resources(adapter);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003367 rtnl_unlock();
Greg Rose92915f72010-01-09 02:24:10 +00003368 }
3369
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003370 ixgbevf_clear_interrupt_scheme(adapter);
3371
3372#ifdef CONFIG_PM
3373 retval = pci_save_state(pdev);
3374 if (retval)
3375 return retval;
3376
3377#endif
Mark Rustadbc0c7152014-03-12 00:38:45 +00003378 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3379 pci_disable_device(pdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003380
3381 return 0;
3382}
3383
3384#ifdef CONFIG_PM
3385static int ixgbevf_resume(struct pci_dev *pdev)
3386{
Wei Yongjun27ae2962014-01-16 02:30:07 -08003387 struct net_device *netdev = pci_get_drvdata(pdev);
3388 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003389 u32 err;
3390
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003391 pci_restore_state(pdev);
3392 /*
3393 * pci_restore_state clears dev->state_saved so call
3394 * pci_save_state to restore it.
3395 */
Greg Rose92915f72010-01-09 02:24:10 +00003396 pci_save_state(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003397
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003398 err = pci_enable_device_mem(pdev);
3399 if (err) {
3400 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3401 return err;
3402 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003403 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003404 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003405 pci_set_master(pdev);
3406
Don Skidmore798e3812013-10-01 04:33:51 -07003407 ixgbevf_reset(adapter);
3408
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003409 rtnl_lock();
3410 err = ixgbevf_init_interrupt_scheme(adapter);
3411 rtnl_unlock();
3412 if (err) {
3413 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3414 return err;
3415 }
3416
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003417 if (netif_running(netdev)) {
3418 err = ixgbevf_open(netdev);
3419 if (err)
3420 return err;
3421 }
3422
3423 netif_device_attach(netdev);
3424
3425 return err;
3426}
3427
3428#endif /* CONFIG_PM */
3429static void ixgbevf_shutdown(struct pci_dev *pdev)
3430{
3431 ixgbevf_suspend(pdev, PMSG_SUSPEND);
Greg Rose92915f72010-01-09 02:24:10 +00003432}
3433
Eric Dumazet4197aa72011-06-22 05:01:35 +00003434static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3435 struct rtnl_link_stats64 *stats)
3436{
3437 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3438 unsigned int start;
3439 u64 bytes, packets;
3440 const struct ixgbevf_ring *ring;
3441 int i;
3442
3443 ixgbevf_update_stats(adapter);
3444
3445 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3446
3447 for (i = 0; i < adapter->num_rx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003448 ring = adapter->rx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003449 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003450 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003451 bytes = ring->stats.bytes;
3452 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003453 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003454 stats->rx_bytes += bytes;
3455 stats->rx_packets += packets;
3456 }
3457
3458 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003459 ring = adapter->tx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003460 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003461 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003462 bytes = ring->stats.bytes;
3463 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003464 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003465 stats->tx_bytes += bytes;
3466 stats->tx_packets += packets;
3467 }
3468
3469 return stats;
3470}
3471
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003472static const struct net_device_ops ixgbevf_netdev_ops = {
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003473 .ndo_open = ixgbevf_open,
3474 .ndo_stop = ixgbevf_close,
3475 .ndo_start_xmit = ixgbevf_xmit_frame,
3476 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
Eric Dumazet4197aa72011-06-22 05:01:35 +00003477 .ndo_get_stats64 = ixgbevf_get_stats,
Greg Rose92915f72010-01-09 02:24:10 +00003478 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003479 .ndo_set_mac_address = ixgbevf_set_mac,
3480 .ndo_change_mtu = ixgbevf_change_mtu,
3481 .ndo_tx_timeout = ixgbevf_tx_timeout,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003482 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3483 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
Jacob Kellerc777cdf2013-09-21 06:24:20 +00003484#ifdef CONFIG_NET_RX_BUSY_POLL
3485 .ndo_busy_poll = ixgbevf_busy_poll_recv,
3486#endif
Greg Rose92915f72010-01-09 02:24:10 +00003487};
Greg Rose92915f72010-01-09 02:24:10 +00003488
3489static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3490{
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003491 dev->netdev_ops = &ixgbevf_netdev_ops;
Greg Rose92915f72010-01-09 02:24:10 +00003492 ixgbevf_set_ethtool_ops(dev);
3493 dev->watchdog_timeo = 5 * HZ;
3494}
3495
3496/**
3497 * ixgbevf_probe - Device Initialization Routine
3498 * @pdev: PCI device information struct
3499 * @ent: entry in ixgbevf_pci_tbl
3500 *
3501 * Returns 0 on success, negative on failure
3502 *
3503 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3504 * The OS initialization, configuring of the adapter private structure,
3505 * and a hardware reset occur.
3506 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003507static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Greg Rose92915f72010-01-09 02:24:10 +00003508{
3509 struct net_device *netdev;
3510 struct ixgbevf_adapter *adapter = NULL;
3511 struct ixgbe_hw *hw = NULL;
3512 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
Greg Rose92915f72010-01-09 02:24:10 +00003513 int err, pci_using_dac;
3514
3515 err = pci_enable_device(pdev);
3516 if (err)
3517 return err;
3518
Russell King53567aa2013-06-10 12:49:38 +01003519 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Greg Rose92915f72010-01-09 02:24:10 +00003520 pci_using_dac = 1;
3521 } else {
Russell King53567aa2013-06-10 12:49:38 +01003522 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003523 if (err) {
Russell King53567aa2013-06-10 12:49:38 +01003524 dev_err(&pdev->dev, "No usable DMA "
3525 "configuration, aborting\n");
3526 goto err_dma;
Greg Rose92915f72010-01-09 02:24:10 +00003527 }
3528 pci_using_dac = 0;
3529 }
3530
3531 err = pci_request_regions(pdev, ixgbevf_driver_name);
3532 if (err) {
3533 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3534 goto err_pci_reg;
3535 }
3536
3537 pci_set_master(pdev);
3538
Greg Rose92915f72010-01-09 02:24:10 +00003539 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3540 MAX_TX_QUEUES);
Greg Rose92915f72010-01-09 02:24:10 +00003541 if (!netdev) {
3542 err = -ENOMEM;
3543 goto err_alloc_etherdev;
3544 }
3545
3546 SET_NETDEV_DEV(netdev, &pdev->dev);
3547
3548 pci_set_drvdata(pdev, netdev);
3549 adapter = netdev_priv(netdev);
3550
3551 adapter->netdev = netdev;
3552 adapter->pdev = pdev;
3553 hw = &adapter->hw;
3554 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00003555 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Greg Rose92915f72010-01-09 02:24:10 +00003556
3557 /*
3558 * call save state here in standalone driver because it relies on
3559 * adapter struct to exist, and needs to call netdev_priv
3560 */
3561 pci_save_state(pdev);
3562
3563 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3564 pci_resource_len(pdev, 0));
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003565 adapter->io_addr = hw->hw_addr;
Greg Rose92915f72010-01-09 02:24:10 +00003566 if (!hw->hw_addr) {
3567 err = -EIO;
3568 goto err_ioremap;
3569 }
3570
3571 ixgbevf_assign_netdev_ops(netdev);
3572
Greg Rose92915f72010-01-09 02:24:10 +00003573 /* Setup hw api */
3574 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3575 hw->mac.type = ii->mac;
3576
3577 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
Greg Rosef416dfc2011-06-08 07:32:38 +00003578 sizeof(struct ixgbe_mbx_operations));
Greg Rose92915f72010-01-09 02:24:10 +00003579
Greg Rose92915f72010-01-09 02:24:10 +00003580 /* setup the private structure */
3581 err = ixgbevf_sw_init(adapter);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003582 if (err)
3583 goto err_sw_init;
3584
3585 /* The HW MAC address was set and/or determined in sw_init */
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003586 if (!is_valid_ether_addr(netdev->dev_addr)) {
3587 pr_err("invalid MAC address\n");
3588 err = -EIO;
3589 goto err_sw_init;
3590 }
Greg Rose92915f72010-01-09 02:24:10 +00003591
Michał Mirosław471a76d2011-06-08 08:53:03 +00003592 netdev->hw_features = NETIF_F_SG |
Greg Rose92915f72010-01-09 02:24:10 +00003593 NETIF_F_IP_CSUM |
Michał Mirosław471a76d2011-06-08 08:53:03 +00003594 NETIF_F_IPV6_CSUM |
3595 NETIF_F_TSO |
3596 NETIF_F_TSO6 |
3597 NETIF_F_RXCSUM;
3598
3599 netdev->features = netdev->hw_features |
Patrick McHardyf6469682013-04-19 02:04:27 +00003600 NETIF_F_HW_VLAN_CTAG_TX |
3601 NETIF_F_HW_VLAN_CTAG_RX |
3602 NETIF_F_HW_VLAN_CTAG_FILTER;
Greg Rose92915f72010-01-09 02:24:10 +00003603
Greg Rose92915f72010-01-09 02:24:10 +00003604 netdev->vlan_features |= NETIF_F_TSO;
3605 netdev->vlan_features |= NETIF_F_TSO6;
3606 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyck3bfacf92010-08-02 14:59:04 +00003607 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003608 netdev->vlan_features |= NETIF_F_SG;
3609
3610 if (pci_using_dac)
3611 netdev->features |= NETIF_F_HIGHDMA;
3612
Jiri Pirko01789342011-08-16 06:29:00 +00003613 netdev->priv_flags |= IFF_UNICAST_FLT;
3614
Greg Rose92915f72010-01-09 02:24:10 +00003615 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00003616 adapter->watchdog_timer.function = ixgbevf_watchdog;
Greg Rose92915f72010-01-09 02:24:10 +00003617 adapter->watchdog_timer.data = (unsigned long)adapter;
3618
Mark Rustadea699562014-03-12 00:38:51 +00003619 if (IXGBE_REMOVED(hw->hw_addr)) {
3620 err = -EIO;
3621 goto err_sw_init;
3622 }
Greg Rose92915f72010-01-09 02:24:10 +00003623 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3624 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
Mark Rustadea699562014-03-12 00:38:51 +00003625 set_bit(__IXGBEVF_WORK_INIT, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003626
3627 err = ixgbevf_init_interrupt_scheme(adapter);
3628 if (err)
3629 goto err_sw_init;
3630
Greg Rose92915f72010-01-09 02:24:10 +00003631 strcpy(netdev->name, "eth%d");
3632
3633 err = register_netdev(netdev);
3634 if (err)
3635 goto err_register;
3636
Greg Rose5d426ad2010-11-16 19:27:19 -08003637 netif_carrier_off(netdev);
3638
Greg Rose33bd9f62010-03-19 02:59:52 +00003639 ixgbevf_init_last_counter_stats(adapter);
3640
Greg Rose92915f72010-01-09 02:24:10 +00003641 /* print the MAC address */
Danny Kukawkaf794e7e2012-02-24 03:45:56 +00003642 hw_dbg(hw, "%pM\n", netdev->dev_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003643
3644 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3645
Greg Rose92915f72010-01-09 02:24:10 +00003646 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
Greg Rose92915f72010-01-09 02:24:10 +00003647 return 0;
3648
3649err_register:
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003650 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003651err_sw_init:
3652 ixgbevf_reset_interrupt_capability(adapter);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003653 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003654err_ioremap:
3655 free_netdev(netdev);
3656err_alloc_etherdev:
3657 pci_release_regions(pdev);
3658err_pci_reg:
3659err_dma:
Mark Rustadbc0c7152014-03-12 00:38:45 +00003660 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3661 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003662 return err;
3663}
3664
3665/**
3666 * ixgbevf_remove - Device Removal Routine
3667 * @pdev: PCI device information struct
3668 *
3669 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3670 * that it should release a PCI device. The could be caused by a
3671 * Hot-Plug event, or because the driver is going to be removed from
3672 * memory.
3673 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003674static void ixgbevf_remove(struct pci_dev *pdev)
Greg Rose92915f72010-01-09 02:24:10 +00003675{
3676 struct net_device *netdev = pci_get_drvdata(pdev);
3677 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3678
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00003679 set_bit(__IXGBEVF_REMOVING, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003680
3681 del_timer_sync(&adapter->watchdog_timer);
3682
Tejun Heo23f333a2010-12-12 16:45:14 +01003683 cancel_work_sync(&adapter->reset_task);
Greg Rose92915f72010-01-09 02:24:10 +00003684 cancel_work_sync(&adapter->watchdog_task);
3685
Alexander Duyckfd13a9a2012-05-11 08:32:24 +00003686 if (netdev->reg_state == NETREG_REGISTERED)
Greg Rose92915f72010-01-09 02:24:10 +00003687 unregister_netdev(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00003688
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003689 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003690 ixgbevf_reset_interrupt_capability(adapter);
3691
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003692 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003693 pci_release_regions(pdev);
3694
3695 hw_dbg(&adapter->hw, "Remove complete\n");
3696
Greg Rose92915f72010-01-09 02:24:10 +00003697 free_netdev(netdev);
3698
Mark Rustadbc0c7152014-03-12 00:38:45 +00003699 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3700 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003701}
3702
Alexander Duyck9f19f312012-05-11 08:33:32 +00003703/**
3704 * ixgbevf_io_error_detected - called when PCI error is detected
3705 * @pdev: Pointer to PCI device
3706 * @state: The current pci connection state
3707 *
3708 * This function is called after a PCI bus error affecting
3709 * this device has been detected.
3710 */
3711static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3712 pci_channel_state_t state)
3713{
3714 struct net_device *netdev = pci_get_drvdata(pdev);
3715 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3716
Mark Rustadea699562014-03-12 00:38:51 +00003717 if (!test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
3718 return PCI_ERS_RESULT_DISCONNECT;
3719
Mark Rustadbc0c7152014-03-12 00:38:45 +00003720 rtnl_lock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003721 netif_device_detach(netdev);
3722
Mark Rustadbc0c7152014-03-12 00:38:45 +00003723 if (state == pci_channel_io_perm_failure) {
3724 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003725 return PCI_ERS_RESULT_DISCONNECT;
Mark Rustadbc0c7152014-03-12 00:38:45 +00003726 }
Alexander Duyck9f19f312012-05-11 08:33:32 +00003727
3728 if (netif_running(netdev))
3729 ixgbevf_down(adapter);
3730
Mark Rustadbc0c7152014-03-12 00:38:45 +00003731 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3732 pci_disable_device(pdev);
3733 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003734
3735 /* Request a slot slot reset. */
3736 return PCI_ERS_RESULT_NEED_RESET;
3737}
3738
3739/**
3740 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3741 * @pdev: Pointer to PCI device
3742 *
3743 * Restart the card from scratch, as if from a cold-boot. Implementation
3744 * resembles the first-half of the ixgbevf_resume routine.
3745 */
3746static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3747{
3748 struct net_device *netdev = pci_get_drvdata(pdev);
3749 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3750
3751 if (pci_enable_device_mem(pdev)) {
3752 dev_err(&pdev->dev,
3753 "Cannot re-enable PCI device after reset.\n");
3754 return PCI_ERS_RESULT_DISCONNECT;
3755 }
3756
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003757 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003758 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck9f19f312012-05-11 08:33:32 +00003759 pci_set_master(pdev);
3760
3761 ixgbevf_reset(adapter);
3762
3763 return PCI_ERS_RESULT_RECOVERED;
3764}
3765
3766/**
3767 * ixgbevf_io_resume - called when traffic can start flowing again.
3768 * @pdev: Pointer to PCI device
3769 *
3770 * This callback is called when the error recovery driver tells us that
3771 * its OK to resume normal operation. Implementation resembles the
3772 * second-half of the ixgbevf_resume routine.
3773 */
3774static void ixgbevf_io_resume(struct pci_dev *pdev)
3775{
3776 struct net_device *netdev = pci_get_drvdata(pdev);
3777 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3778
3779 if (netif_running(netdev))
3780 ixgbevf_up(adapter);
3781
3782 netif_device_attach(netdev);
3783}
3784
3785/* PCI Error Recovery (ERS) */
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003786static const struct pci_error_handlers ixgbevf_err_handler = {
Alexander Duyck9f19f312012-05-11 08:33:32 +00003787 .error_detected = ixgbevf_io_error_detected,
3788 .slot_reset = ixgbevf_io_slot_reset,
3789 .resume = ixgbevf_io_resume,
3790};
3791
Greg Rose92915f72010-01-09 02:24:10 +00003792static struct pci_driver ixgbevf_driver = {
3793 .name = ixgbevf_driver_name,
3794 .id_table = ixgbevf_pci_tbl,
3795 .probe = ixgbevf_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003796 .remove = ixgbevf_remove,
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003797#ifdef CONFIG_PM
3798 /* Power Management Hooks */
3799 .suspend = ixgbevf_suspend,
3800 .resume = ixgbevf_resume,
3801#endif
Greg Rose92915f72010-01-09 02:24:10 +00003802 .shutdown = ixgbevf_shutdown,
Alexander Duyck9f19f312012-05-11 08:33:32 +00003803 .err_handler = &ixgbevf_err_handler
Greg Rose92915f72010-01-09 02:24:10 +00003804};
3805
3806/**
Greg Rose65d676c2011-02-03 06:54:13 +00003807 * ixgbevf_init_module - Driver Registration Routine
Greg Rose92915f72010-01-09 02:24:10 +00003808 *
Greg Rose65d676c2011-02-03 06:54:13 +00003809 * ixgbevf_init_module is the first routine called when the driver is
Greg Rose92915f72010-01-09 02:24:10 +00003810 * loaded. All it does is register with the PCI subsystem.
3811 **/
3812static int __init ixgbevf_init_module(void)
3813{
3814 int ret;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003815 pr_info("%s - version %s\n", ixgbevf_driver_string,
3816 ixgbevf_driver_version);
Greg Rose92915f72010-01-09 02:24:10 +00003817
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003818 pr_info("%s\n", ixgbevf_copyright);
Greg Rose92915f72010-01-09 02:24:10 +00003819
3820 ret = pci_register_driver(&ixgbevf_driver);
3821 return ret;
3822}
3823
3824module_init(ixgbevf_init_module);
3825
3826/**
Greg Rose65d676c2011-02-03 06:54:13 +00003827 * ixgbevf_exit_module - Driver Exit Cleanup Routine
Greg Rose92915f72010-01-09 02:24:10 +00003828 *
Greg Rose65d676c2011-02-03 06:54:13 +00003829 * ixgbevf_exit_module is called just before the driver is removed
Greg Rose92915f72010-01-09 02:24:10 +00003830 * from memory.
3831 **/
3832static void __exit ixgbevf_exit_module(void)
3833{
3834 pci_unregister_driver(&ixgbevf_driver);
3835}
3836
3837#ifdef DEBUG
3838/**
Greg Rose65d676c2011-02-03 06:54:13 +00003839 * ixgbevf_get_hw_dev_name - return device name string
Greg Rose92915f72010-01-09 02:24:10 +00003840 * used by hardware layer to print debugging information
3841 **/
3842char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3843{
3844 struct ixgbevf_adapter *adapter = hw->back;
3845 return adapter->netdev->name;
3846}
3847
3848#endif
3849module_exit(ixgbevf_exit_module);
3850
3851/* ixgbevf_main.c */