blob: f864da991c47b5f46e4f851396adcf74231c59e3 [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 Tantilovbafa5782014-11-08 01:39:15 +0000413static bool ixgbevf_alloc_mapped_skb(struct ixgbevf_ring *rx_ring,
414 struct ixgbevf_rx_buffer *bi)
415{
416 struct sk_buff *skb = bi->skb;
417 dma_addr_t dma = bi->dma;
418
419 if (unlikely(skb))
420 return true;
421
422 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
423 rx_ring->rx_buf_len);
424 if (unlikely(!skb)) {
425 rx_ring->rx_stats.alloc_rx_buff_failed++;
426 return false;
427 }
428
429 dma = dma_map_single(rx_ring->dev, skb->data,
430 rx_ring->rx_buf_len, DMA_FROM_DEVICE);
431
432 /* if mapping failed free memory back to system since
433 * there isn't much point in holding memory we can't use
434 */
435 if (dma_mapping_error(rx_ring->dev, dma)) {
436 dev_kfree_skb_any(skb);
437
438 rx_ring->rx_stats.alloc_rx_buff_failed++;
439 return false;
440 }
441
442 bi->skb = skb;
443 bi->dma = dma;
444
445 return true;
446}
447
Greg Rose92915f72010-01-09 02:24:10 +0000448/**
449 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
Emil Tantilov095e2612014-01-17 18:30:00 -0800450 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
Emil Tantilovbafa5782014-11-08 01:39:15 +0000451 * @cleaned_count: number of buffers to replace
Greg Rose92915f72010-01-09 02:24:10 +0000452 **/
Emil Tantilov095e2612014-01-17 18:30:00 -0800453static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
Emil Tantilovbafa5782014-11-08 01:39:15 +0000454 u16 cleaned_count)
Greg Rose92915f72010-01-09 02:24:10 +0000455{
Greg Rose92915f72010-01-09 02:24:10 +0000456 union ixgbe_adv_rx_desc *rx_desc;
457 struct ixgbevf_rx_buffer *bi;
Alexander Duyckfb401952012-05-11 08:33:16 +0000458 unsigned int i = rx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +0000459
Emil Tantilovbafa5782014-11-08 01:39:15 +0000460 /* nothing to do or no valid netdev defined */
461 if (!cleaned_count || !rx_ring->netdev)
462 return;
Greg Roseb9dd2452012-11-02 05:50:21 +0000463
Emil Tantilovbafa5782014-11-08 01:39:15 +0000464 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
465 bi = &rx_ring->rx_buffer_info[i];
466 i -= rx_ring->count;
Greg Roseb9dd2452012-11-02 05:50:21 +0000467
Emil Tantilovbafa5782014-11-08 01:39:15 +0000468 do {
469 if (!ixgbevf_alloc_mapped_skb(rx_ring, bi))
470 break;
Emil Tantilov05d063a2014-01-17 18:29:59 -0800471
Emil Tantilovbafa5782014-11-08 01:39:15 +0000472 /* Refresh the desc even if pkt_addr didn't change
473 * because each write-back erases this info.
474 */
Alexander Duyck77d5dfc2012-05-11 08:32:19 +0000475 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
Greg Rose92915f72010-01-09 02:24:10 +0000476
Emil Tantilovbafa5782014-11-08 01:39:15 +0000477 rx_desc++;
478 bi++;
Greg Rose92915f72010-01-09 02:24:10 +0000479 i++;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000480 if (unlikely(!i)) {
481 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
482 bi = rx_ring->rx_buffer_info;
483 i -= rx_ring->count;
484 }
Greg Rose92915f72010-01-09 02:24:10 +0000485
Emil Tantilovbafa5782014-11-08 01:39:15 +0000486 /* clear the hdr_addr for the next_to_use descriptor */
487 rx_desc->read.hdr_addr = 0;
488
489 cleaned_count--;
490 } while (cleaned_count);
491
492 i += rx_ring->count;
493
494 if (rx_ring->next_to_use != i) {
495 /* record the next descriptor to use */
496 rx_ring->next_to_use = i;
497
498 /* Force memory writes to complete before letting h/w
499 * know there are new descriptors to fetch. (Only
500 * applicable for weak-ordered memory model archs,
501 * such as IA-64).
502 */
503 wmb();
504 ixgbevf_write_tail(rx_ring, i);
505 }
Greg Rose92915f72010-01-09 02:24:10 +0000506}
507
508static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000509 u32 qmask)
Greg Rose92915f72010-01-09 02:24:10 +0000510{
Greg Rose92915f72010-01-09 02:24:10 +0000511 struct ixgbe_hw *hw = &adapter->hw;
512
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000513 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
Greg Rose92915f72010-01-09 02:24:10 +0000514}
515
Jacob Keller08e50a22013-09-21 06:24:14 +0000516static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
517 struct ixgbevf_ring *rx_ring,
518 int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000519{
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000520 union ixgbe_adv_rx_desc *rx_desc;
Greg Rose92915f72010-01-09 02:24:10 +0000521 unsigned int i;
Greg Rose92915f72010-01-09 02:24:10 +0000522 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Emil Tantilovbafa5782014-11-08 01:39:15 +0000523 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +0000524
525 i = rx_ring->next_to_clean;
Alexander Duyck908421f2012-05-11 08:33:00 +0000526 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000527
Emil Tantilovec62fe22014-11-08 01:39:20 +0000528 while (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_DD)) {
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000529 union ixgbe_adv_rx_desc *next_rxd;
530 struct ixgbevf_rx_buffer *rx_buffer_info;
531 struct sk_buff *skb;
532
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000533 if (!budget)
Greg Rose92915f72010-01-09 02:24:10 +0000534 break;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000535 budget--;
Greg Rose92915f72010-01-09 02:24:10 +0000536
Jeff Kirsher2d0bb1c2010-08-08 16:02:31 +0000537 rmb(); /* read descriptor and rx_buffer_info after status DD */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000538
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000539 rx_buffer_info = &rx_ring->rx_buffer_info[i];
Greg Rose92915f72010-01-09 02:24:10 +0000540 skb = rx_buffer_info->skb;
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000541 prefetch(skb->data);
Greg Rose92915f72010-01-09 02:24:10 +0000542 rx_buffer_info->skb = NULL;
543
Emil Tantilovec62fe22014-11-08 01:39:20 +0000544 dma_unmap_single(rx_ring->dev, rx_buffer_info->dma,
545 rx_ring->rx_buf_len,
546 DMA_FROM_DEVICE);
547 rx_buffer_info->dma = 0;
548 skb_put(skb, le16_to_cpu(rx_desc->wb.upper.length));
Greg Rose92915f72010-01-09 02:24:10 +0000549
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000550 cleaned_count++;
551
Greg Rose92915f72010-01-09 02:24:10 +0000552 i++;
553 if (i == rx_ring->count)
554 i = 0;
555
Alexander Duyck908421f2012-05-11 08:33:00 +0000556 next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000557 prefetch(next_rxd);
Greg Rose92915f72010-01-09 02:24:10 +0000558
Emil Tantilovec62fe22014-11-08 01:39:20 +0000559 if (!(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP))) {
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000560 skb->next = rx_ring->rx_buffer_info[i].skb;
Alexander Duyck5c60f812012-09-01 05:12:38 +0000561 IXGBE_CB(skb->next)->prev = skb;
Emil Tantilov095e2612014-01-17 18:30:00 -0800562 rx_ring->rx_stats.non_eop_descs++;
Greg Rose92915f72010-01-09 02:24:10 +0000563 goto next_desc;
564 }
565
Alexander Duyck5c60f812012-09-01 05:12:38 +0000566 /* we should not be chaining buffers, if we did drop the skb */
567 if (IXGBE_CB(skb)->prev) {
568 do {
569 struct sk_buff *this = skb;
570 skb = IXGBE_CB(skb)->prev;
571 dev_kfree_skb(this);
572 } while (skb);
573 goto next_desc;
574 }
575
Greg Rose92915f72010-01-09 02:24:10 +0000576 /* ERR_MASK will only have valid bits if EOP set */
Emil Tantilovec62fe22014-11-08 01:39:20 +0000577 if (unlikely(ixgbevf_test_staterr(rx_desc,
578 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
Greg Rose92915f72010-01-09 02:24:10 +0000579 dev_kfree_skb_irq(skb);
580 goto next_desc;
581 }
582
Greg Rose92915f72010-01-09 02:24:10 +0000583 /* probably a little skewed due to removing CRC */
584 total_rx_bytes += skb->len;
585 total_rx_packets++;
586
John Fastabend815cccb2012-10-24 08:13:09 +0000587 /* Workaround hardware that can't do proper VEPA multicast
588 * source pruning.
589 */
Florian Fainellibd9d5592014-02-28 15:46:49 -0800590 if ((skb->pkt_type == PACKET_BROADCAST ||
591 skb->pkt_type == PACKET_MULTICAST) &&
Emil Tantilov095e2612014-01-17 18:30:00 -0800592 ether_addr_equal(rx_ring->netdev->dev_addr,
Joe Perches7367d0b2013-09-01 11:51:23 -0700593 eth_hdr(skb)->h_source)) {
John Fastabend815cccb2012-10-24 08:13:09 +0000594 dev_kfree_skb_irq(skb);
595 goto next_desc;
596 }
597
Emil Tantilovdff80522014-11-08 01:39:25 +0000598 /* populate checksum, VLAN, and protocol */
599 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
600
601 ixgbevf_rx_skb(q_vector, skb);
Greg Rose92915f72010-01-09 02:24:10 +0000602
603next_desc:
Greg Rose92915f72010-01-09 02:24:10 +0000604 /* return some buffers to hardware, one at a time is too slow */
605 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
Emil Tantilov095e2612014-01-17 18:30:00 -0800606 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
Greg Rose92915f72010-01-09 02:24:10 +0000607 cleaned_count = 0;
608 }
609
610 /* use prefetched values */
611 rx_desc = next_rxd;
612 rx_buffer_info = &rx_ring->rx_buffer_info[i];
Emil Tantilovb97fe3b2014-11-08 01:39:30 +0000613 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +0000614 }
615
616 rx_ring->next_to_clean = i;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000617 u64_stats_update_begin(&rx_ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -0800618 rx_ring->stats.packets += total_rx_packets;
619 rx_ring->stats.bytes += total_rx_bytes;
Eric Dumazet4197aa72011-06-22 05:01:35 +0000620 u64_stats_update_end(&rx_ring->syncp);
Greg Roseac6ed8f2012-08-31 05:59:28 +0000621 q_vector->rx.total_packets += total_rx_packets;
622 q_vector->rx.total_bytes += total_rx_bytes;
Greg Rose92915f72010-01-09 02:24:10 +0000623
Emil Tantilovbafa5782014-11-08 01:39:15 +0000624 if (cleaned_count)
625 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
626
Jacob Keller08e50a22013-09-21 06:24:14 +0000627 return total_rx_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000628}
629
630/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000631 * ixgbevf_poll - NAPI polling calback
Greg Rose92915f72010-01-09 02:24:10 +0000632 * @napi: napi struct with our devices info in it
633 * @budget: amount of work driver is allowed to do this pass, in packets
634 *
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000635 * This function will clean more than one or more rings associated with a
Greg Rose92915f72010-01-09 02:24:10 +0000636 * q_vector.
637 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000638static int ixgbevf_poll(struct napi_struct *napi, int budget)
Greg Rose92915f72010-01-09 02:24:10 +0000639{
640 struct ixgbevf_q_vector *q_vector =
641 container_of(napi, struct ixgbevf_q_vector, napi);
642 struct ixgbevf_adapter *adapter = q_vector->adapter;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000643 struct ixgbevf_ring *ring;
644 int per_ring_budget;
645 bool clean_complete = true;
646
647 ixgbevf_for_each_ring(ring, q_vector->tx)
648 clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
Greg Rose92915f72010-01-09 02:24:10 +0000649
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000650#ifdef CONFIG_NET_RX_BUSY_POLL
651 if (!ixgbevf_qv_lock_napi(q_vector))
652 return budget;
653#endif
654
Greg Rose92915f72010-01-09 02:24:10 +0000655 /* attempt to distribute budget to each queue fairly, but don't allow
656 * the budget to go below 1 because we'll exit polling */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000657 if (q_vector->rx.count > 1)
658 per_ring_budget = max(budget/q_vector->rx.count, 1);
659 else
660 per_ring_budget = budget;
Greg Rose92915f72010-01-09 02:24:10 +0000661
Greg Rose366c1092012-11-13 04:03:18 +0000662 adapter->flags |= IXGBE_FLAG_IN_NETPOLL;
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000663 ixgbevf_for_each_ring(ring, q_vector->rx)
Jacob Keller08e50a22013-09-21 06:24:14 +0000664 clean_complete &= (ixgbevf_clean_rx_irq(q_vector, ring,
665 per_ring_budget)
666 < per_ring_budget);
Greg Rose366c1092012-11-13 04:03:18 +0000667 adapter->flags &= ~IXGBE_FLAG_IN_NETPOLL;
Greg Rose92915f72010-01-09 02:24:10 +0000668
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000669#ifdef CONFIG_NET_RX_BUSY_POLL
670 ixgbevf_qv_unlock_napi(q_vector);
671#endif
672
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000673 /* If all work not completed, return budget and keep polling */
674 if (!clean_complete)
675 return budget;
676 /* all work done, exit the polling mode */
677 napi_complete(napi);
678 if (adapter->rx_itr_setting & 1)
679 ixgbevf_set_itr(q_vector);
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000680 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
681 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000682 ixgbevf_irq_enable_queues(adapter,
683 1 << q_vector->v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000684
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000685 return 0;
Greg Rose92915f72010-01-09 02:24:10 +0000686}
687
Greg Rosece422602012-05-22 02:17:49 +0000688/**
689 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
690 * @q_vector: structure containing interrupt and ring information
691 */
Jacob Keller38496232013-10-22 06:19:18 +0000692void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
Greg Rosece422602012-05-22 02:17:49 +0000693{
694 struct ixgbevf_adapter *adapter = q_vector->adapter;
695 struct ixgbe_hw *hw = &adapter->hw;
696 int v_idx = q_vector->v_idx;
697 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
698
699 /*
700 * set the WDIS bit to not clear the timer bits and cause an
701 * immediate assertion of the interrupt
702 */
703 itr_reg |= IXGBE_EITR_CNT_WDIS;
704
705 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
706}
Greg Rose92915f72010-01-09 02:24:10 +0000707
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000708#ifdef CONFIG_NET_RX_BUSY_POLL
709/* must be called with local_bh_disable()d */
710static int ixgbevf_busy_poll_recv(struct napi_struct *napi)
711{
712 struct ixgbevf_q_vector *q_vector =
713 container_of(napi, struct ixgbevf_q_vector, napi);
714 struct ixgbevf_adapter *adapter = q_vector->adapter;
715 struct ixgbevf_ring *ring;
716 int found = 0;
717
718 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
719 return LL_FLUSH_FAILED;
720
721 if (!ixgbevf_qv_lock_poll(q_vector))
722 return LL_FLUSH_BUSY;
723
724 ixgbevf_for_each_ring(ring, q_vector->rx) {
725 found = ixgbevf_clean_rx_irq(q_vector, ring, 4);
Jacob Keller3b5dca22013-09-21 06:24:25 +0000726#ifdef BP_EXTENDED_STATS
727 if (found)
Emil Tantilov095e2612014-01-17 18:30:00 -0800728 ring->stats.cleaned += found;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000729 else
Emil Tantilov095e2612014-01-17 18:30:00 -0800730 ring->stats.misses++;
Jacob Keller3b5dca22013-09-21 06:24:25 +0000731#endif
Jacob Kellerc777cdf2013-09-21 06:24:20 +0000732 if (found)
733 break;
734 }
735
736 ixgbevf_qv_unlock_poll(q_vector);
737
738 return found;
739}
740#endif /* CONFIG_NET_RX_BUSY_POLL */
741
Greg Rose92915f72010-01-09 02:24:10 +0000742/**
743 * ixgbevf_configure_msix - Configure MSI-X hardware
744 * @adapter: board private structure
745 *
746 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
747 * interrupts.
748 **/
749static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
750{
751 struct ixgbevf_q_vector *q_vector;
Alexander Duyck6b43c442012-05-11 08:32:45 +0000752 int q_vectors, v_idx;
Greg Rose92915f72010-01-09 02:24:10 +0000753
754 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000755 adapter->eims_enable_mask = 0;
Greg Rose92915f72010-01-09 02:24:10 +0000756
757 /*
758 * Populate the IVAR table and set the ITR values to the
759 * corresponding register.
760 */
761 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
Alexander Duyck6b43c442012-05-11 08:32:45 +0000762 struct ixgbevf_ring *ring;
Greg Rose92915f72010-01-09 02:24:10 +0000763 q_vector = adapter->q_vector[v_idx];
Greg Rose92915f72010-01-09 02:24:10 +0000764
Alexander Duyck6b43c442012-05-11 08:32:45 +0000765 ixgbevf_for_each_ring(ring, q_vector->rx)
766 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000767
Alexander Duyck6b43c442012-05-11 08:32:45 +0000768 ixgbevf_for_each_ring(ring, q_vector->tx)
769 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
Greg Rose92915f72010-01-09 02:24:10 +0000770
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000771 if (q_vector->tx.ring && !q_vector->rx.ring) {
772 /* tx only vector */
773 if (adapter->tx_itr_setting == 1)
774 q_vector->itr = IXGBE_10K_ITR;
775 else
776 q_vector->itr = adapter->tx_itr_setting;
777 } else {
778 /* rx or rx/tx vector */
779 if (adapter->rx_itr_setting == 1)
780 q_vector->itr = IXGBE_20K_ITR;
781 else
782 q_vector->itr = adapter->rx_itr_setting;
783 }
Greg Rose92915f72010-01-09 02:24:10 +0000784
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000785 /* add q_vector eims value to global eims_enable_mask */
786 adapter->eims_enable_mask |= 1 << v_idx;
787
788 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000789 }
790
791 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000792 /* setup eims_other and add value to global eims_enable_mask */
793 adapter->eims_other = 1 << v_idx;
794 adapter->eims_enable_mask |= adapter->eims_other;
Greg Rose92915f72010-01-09 02:24:10 +0000795}
796
797enum latency_range {
798 lowest_latency = 0,
799 low_latency = 1,
800 bulk_latency = 2,
801 latency_invalid = 255
802};
803
804/**
805 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000806 * @q_vector: structure containing interrupt and ring information
807 * @ring_container: structure containing ring performance data
Greg Rose92915f72010-01-09 02:24:10 +0000808 *
809 * Stores a new ITR value based on packets and byte
810 * counts during the last interrupt. The advantage of per interrupt
811 * computation is faster updates and more accurate ITR for the current
812 * traffic pattern. Constants in this function were computed
813 * based on theoretical maximum wire speed and thresholds were set based
814 * on testing data as well as attempting to minimize response time
815 * while increasing bulk throughput.
816 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000817static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
818 struct ixgbevf_ring_container *ring_container)
Greg Rose92915f72010-01-09 02:24:10 +0000819{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000820 int bytes = ring_container->total_bytes;
821 int packets = ring_container->total_packets;
Greg Rose92915f72010-01-09 02:24:10 +0000822 u32 timepassed_us;
823 u64 bytes_perint;
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000824 u8 itr_setting = ring_container->itr;
Greg Rose92915f72010-01-09 02:24:10 +0000825
826 if (packets == 0)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000827 return;
Greg Rose92915f72010-01-09 02:24:10 +0000828
829 /* simple throttlerate management
830 * 0-20MB/s lowest (100000 ints/s)
831 * 20-100MB/s low (20000 ints/s)
832 * 100-1249MB/s bulk (8000 ints/s)
833 */
834 /* what was last interrupt timeslice? */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000835 timepassed_us = q_vector->itr >> 2;
Greg Rose92915f72010-01-09 02:24:10 +0000836 bytes_perint = bytes / timepassed_us; /* bytes/usec */
837
838 switch (itr_setting) {
839 case lowest_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000840 if (bytes_perint > 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000841 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000842 break;
843 case low_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000844 if (bytes_perint > 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000845 itr_setting = bulk_latency;
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000846 else if (bytes_perint <= 10)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000847 itr_setting = lowest_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000848 break;
849 case bulk_latency:
Alexander Duycke2c28ce2012-05-11 08:32:34 +0000850 if (bytes_perint <= 20)
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000851 itr_setting = low_latency;
Greg Rose92915f72010-01-09 02:24:10 +0000852 break;
853 }
854
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000855 /* clear work counters since we have the values we need */
856 ring_container->total_bytes = 0;
857 ring_container->total_packets = 0;
858
859 /* write updated itr to ring container */
860 ring_container->itr = itr_setting;
Greg Rose92915f72010-01-09 02:24:10 +0000861}
862
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000863static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
Greg Rose92915f72010-01-09 02:24:10 +0000864{
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000865 u32 new_itr = q_vector->itr;
866 u8 current_itr;
Greg Rose92915f72010-01-09 02:24:10 +0000867
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000868 ixgbevf_update_itr(q_vector, &q_vector->tx);
869 ixgbevf_update_itr(q_vector, &q_vector->rx);
Greg Rose92915f72010-01-09 02:24:10 +0000870
Alexander Duyck6b43c442012-05-11 08:32:45 +0000871 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Greg Rose92915f72010-01-09 02:24:10 +0000872
873 switch (current_itr) {
874 /* counts and packets in update_itr are dependent on these numbers */
875 case lowest_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000876 new_itr = IXGBE_100K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000877 break;
878 case low_latency:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000879 new_itr = IXGBE_20K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000880 break;
881 case bulk_latency:
882 default:
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000883 new_itr = IXGBE_8K_ITR;
Greg Rose92915f72010-01-09 02:24:10 +0000884 break;
885 }
886
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000887 if (new_itr != q_vector->itr) {
Greg Rose92915f72010-01-09 02:24:10 +0000888 /* do an exponential smoothing */
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000889 new_itr = (10 * new_itr * q_vector->itr) /
890 ((9 * new_itr) + q_vector->itr);
891
892 /* save the algorithm value here */
893 q_vector->itr = new_itr;
894
895 ixgbevf_write_eitr(q_vector);
Greg Rose92915f72010-01-09 02:24:10 +0000896 }
Greg Rose92915f72010-01-09 02:24:10 +0000897}
898
Alexander Duyck4b2cd272012-08-02 01:16:59 +0000899static irqreturn_t ixgbevf_msix_other(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +0000900{
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000901 struct ixgbevf_adapter *adapter = data;
Greg Rose92915f72010-01-09 02:24:10 +0000902 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +0000903
Alexander Duyck4b2cd272012-08-02 01:16:59 +0000904 hw->mac.get_link_status = 1;
Greg Rose375b27c2012-01-18 22:13:31 +0000905
Mark Rustad2e7cfbd2014-03-04 03:02:13 +0000906 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
907 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Don Skidmorec7bb4172013-10-01 04:33:49 -0700908 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose3a2c4032012-02-01 01:28:15 +0000909
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000910 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
911
Greg Rose92915f72010-01-09 02:24:10 +0000912 return IRQ_HANDLED;
913}
914
Greg Rose92915f72010-01-09 02:24:10 +0000915/**
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000916 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
Greg Rose92915f72010-01-09 02:24:10 +0000917 * @irq: unused
918 * @data: pointer to our q_vector struct for this interrupt vector
919 **/
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000920static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
Greg Rose92915f72010-01-09 02:24:10 +0000921{
922 struct ixgbevf_q_vector *q_vector = data;
Greg Rose92915f72010-01-09 02:24:10 +0000923
Alexander Duyck5f3600e2012-05-11 08:32:55 +0000924 /* EIAM disabled interrupts (on this vector) for us */
Alexander Duyckfa71ae22012-05-11 08:32:50 +0000925 if (q_vector->rx.ring || q_vector->tx.ring)
926 napi_schedule(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +0000927
928 return IRQ_HANDLED;
929}
930
931static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
932 int r_idx)
933{
934 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
935
Don Skidmore87e70ab2014-01-16 02:30:08 -0800936 a->rx_ring[r_idx]->next = q_vector->rx.ring;
937 q_vector->rx.ring = a->rx_ring[r_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +0000938 q_vector->rx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000939}
940
941static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
942 int t_idx)
943{
944 struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
945
Don Skidmore87e70ab2014-01-16 02:30:08 -0800946 a->tx_ring[t_idx]->next = q_vector->tx.ring;
947 q_vector->tx.ring = a->tx_ring[t_idx];
Alexander Duyck6b43c442012-05-11 08:32:45 +0000948 q_vector->tx.count++;
Greg Rose92915f72010-01-09 02:24:10 +0000949}
950
951/**
952 * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
953 * @adapter: board private structure to initialize
954 *
955 * This function maps descriptor rings to the queue-specific vectors
956 * we were allotted through the MSI-X enabling code. Ideally, we'd have
957 * one vector per ring/queue, but on a constrained vector budget, we
958 * group the rings as "efficiently" as possible. You would add new
959 * mapping configurations in here.
960 **/
961static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
962{
963 int q_vectors;
964 int v_start = 0;
965 int rxr_idx = 0, txr_idx = 0;
966 int rxr_remaining = adapter->num_rx_queues;
967 int txr_remaining = adapter->num_tx_queues;
968 int i, j;
969 int rqpv, tqpv;
970 int err = 0;
971
972 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
973
974 /*
975 * The ideal configuration...
976 * We have enough vectors to map one per queue.
977 */
978 if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
979 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
980 map_vector_to_rxq(adapter, v_start, rxr_idx);
981
982 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
983 map_vector_to_txq(adapter, v_start, txr_idx);
984 goto out;
985 }
986
987 /*
988 * If we don't have enough vectors for a 1-to-1
989 * mapping, we'll have to group them so there are
990 * multiple queues per vector.
991 */
992 /* Re-adjusting *qpv takes care of the remainder. */
993 for (i = v_start; i < q_vectors; i++) {
994 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
995 for (j = 0; j < rqpv; j++) {
996 map_vector_to_rxq(adapter, i, rxr_idx);
997 rxr_idx++;
998 rxr_remaining--;
999 }
1000 }
1001 for (i = v_start; i < q_vectors; i++) {
1002 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1003 for (j = 0; j < tqpv; j++) {
1004 map_vector_to_txq(adapter, i, txr_idx);
1005 txr_idx++;
1006 txr_remaining--;
1007 }
1008 }
1009
1010out:
1011 return err;
1012}
1013
1014/**
1015 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1016 * @adapter: board private structure
1017 *
1018 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1019 * interrupts from the kernel.
1020 **/
1021static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1022{
1023 struct net_device *netdev = adapter->netdev;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001024 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1025 int vector, err;
Greg Rose92915f72010-01-09 02:24:10 +00001026 int ri = 0, ti = 0;
1027
Greg Rose92915f72010-01-09 02:24:10 +00001028 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001029 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1030 struct msix_entry *entry = &adapter->msix_entries[vector];
Greg Rose92915f72010-01-09 02:24:10 +00001031
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001032 if (q_vector->tx.ring && q_vector->rx.ring) {
1033 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1034 "%s-%s-%d", netdev->name, "TxRx", ri++);
1035 ti++;
1036 } else if (q_vector->rx.ring) {
1037 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1038 "%s-%s-%d", netdev->name, "rx", ri++);
1039 } else if (q_vector->tx.ring) {
1040 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1041 "%s-%s-%d", netdev->name, "tx", ti++);
Greg Rose92915f72010-01-09 02:24:10 +00001042 } else {
1043 /* skip this unused q_vector */
1044 continue;
1045 }
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001046 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1047 q_vector->name, q_vector);
Greg Rose92915f72010-01-09 02:24:10 +00001048 if (err) {
1049 hw_dbg(&adapter->hw,
1050 "request_irq failed for MSIX interrupt "
1051 "Error: %d\n", err);
1052 goto free_queue_irqs;
1053 }
1054 }
1055
Greg Rose92915f72010-01-09 02:24:10 +00001056 err = request_irq(adapter->msix_entries[vector].vector,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001057 &ixgbevf_msix_other, 0, netdev->name, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001058 if (err) {
1059 hw_dbg(&adapter->hw,
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001060 "request_irq for msix_other failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00001061 goto free_queue_irqs;
1062 }
1063
1064 return 0;
1065
1066free_queue_irqs:
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001067 while (vector) {
1068 vector--;
1069 free_irq(adapter->msix_entries[vector].vector,
1070 adapter->q_vector[vector]);
1071 }
xunleera1f6c6b2013-03-05 07:44:20 +00001072 /* This failure is non-recoverable - it indicates the system is
1073 * out of MSIX vector resources and the VF driver cannot run
1074 * without them. Set the number of msix vectors to zero
1075 * indicating that not enough can be allocated. The error
1076 * will be returned to the user indicating device open failed.
1077 * Any further attempts to force the driver to open will also
1078 * fail. The only way to recover is to unload the driver and
1079 * reload it again. If the system has recovered some MSIX
1080 * vectors then it may succeed.
1081 */
1082 adapter->num_msix_vectors = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001083 return err;
1084}
1085
1086static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1087{
1088 int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1089
1090 for (i = 0; i < q_vectors; i++) {
1091 struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00001092 q_vector->rx.ring = NULL;
1093 q_vector->tx.ring = NULL;
1094 q_vector->rx.count = 0;
1095 q_vector->tx.count = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001096 }
1097}
1098
1099/**
1100 * ixgbevf_request_irq - initialize interrupts
1101 * @adapter: board private structure
1102 *
1103 * Attempts to configure interrupts using the best available
1104 * capabilities of the hardware and kernel.
1105 **/
1106static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1107{
1108 int err = 0;
1109
1110 err = ixgbevf_request_msix_irqs(adapter);
1111
1112 if (err)
1113 hw_dbg(&adapter->hw,
1114 "request_irq failed, Error %d\n", err);
1115
1116 return err;
1117}
1118
1119static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1120{
Greg Rose92915f72010-01-09 02:24:10 +00001121 int i, q_vectors;
1122
1123 q_vectors = adapter->num_msix_vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001124 i = q_vectors - 1;
1125
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001126 free_irq(adapter->msix_entries[i].vector, adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001127 i--;
1128
1129 for (; i >= 0; i--) {
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001130 /* free only the irqs that were actually requested */
1131 if (!adapter->q_vector[i]->rx.ring &&
1132 !adapter->q_vector[i]->tx.ring)
1133 continue;
1134
Greg Rose92915f72010-01-09 02:24:10 +00001135 free_irq(adapter->msix_entries[i].vector,
1136 adapter->q_vector[i]);
1137 }
1138
1139 ixgbevf_reset_q_vectors(adapter);
1140}
1141
1142/**
1143 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1144 * @adapter: board private structure
1145 **/
1146static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1147{
Greg Rose92915f72010-01-09 02:24:10 +00001148 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001149 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001150
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001151 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001152 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001153 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001154
1155 IXGBE_WRITE_FLUSH(hw);
1156
1157 for (i = 0; i < adapter->num_msix_vectors; i++)
1158 synchronize_irq(adapter->msix_entries[i].vector);
1159}
1160
1161/**
1162 * ixgbevf_irq_enable - Enable default interrupt generation settings
1163 * @adapter: board private structure
1164 **/
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001165static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001166{
1167 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001168
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001169 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1170 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1171 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
Greg Rose92915f72010-01-09 02:24:10 +00001172}
1173
1174/**
Don Skidmorede02dec2014-01-16 02:30:09 -08001175 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1176 * @adapter: board private structure
1177 * @ring: structure containing ring specific data
1178 *
1179 * Configure the Tx descriptor ring after a reset.
1180 **/
1181static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1182 struct ixgbevf_ring *ring)
1183{
1184 struct ixgbe_hw *hw = &adapter->hw;
1185 u64 tdba = ring->dma;
1186 int wait_loop = 10;
1187 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1188 u8 reg_idx = ring->reg_idx;
1189
1190 /* disable queue to avoid issues while updating state */
1191 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1192 IXGBE_WRITE_FLUSH(hw);
1193
1194 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1195 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1196 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1197 ring->count * sizeof(union ixgbe_adv_tx_desc));
1198
1199 /* disable head writeback */
1200 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1201 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1202
1203 /* enable relaxed ordering */
1204 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1205 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1206 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1207
1208 /* reset head and tail pointers */
1209 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1210 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001211 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001212
1213 /* reset ntu and ntc to place SW in sync with hardwdare */
1214 ring->next_to_clean = 0;
1215 ring->next_to_use = 0;
1216
1217 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1218 * to or less than the number of on chip descriptors, which is
1219 * currently 40.
1220 */
1221 txdctl |= (8 << 16); /* WTHRESH = 8 */
1222
1223 /* Setting PTHRESH to 32 both improves performance */
1224 txdctl |= (1 << 8) | /* HTHRESH = 1 */
1225 32; /* PTHRESH = 32 */
1226
1227 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1228
1229 /* poll to verify queue is enabled */
1230 do {
1231 usleep_range(1000, 2000);
1232 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1233 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1234 if (!wait_loop)
1235 pr_err("Could not enable Tx Queue %d\n", reg_idx);
1236}
1237
1238/**
Greg Rose92915f72010-01-09 02:24:10 +00001239 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1240 * @adapter: board private structure
1241 *
1242 * Configure the Tx unit of the MAC after a reset.
1243 **/
1244static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1245{
Don Skidmorede02dec2014-01-16 02:30:09 -08001246 u32 i;
Greg Rose92915f72010-01-09 02:24:10 +00001247
1248 /* Setup the HW Tx Head and Tail descriptor pointers */
Don Skidmorede02dec2014-01-16 02:30:09 -08001249 for (i = 0; i < adapter->num_tx_queues; i++)
1250 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001251}
1252
1253#define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1254
1255static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1256{
1257 struct ixgbevf_ring *rx_ring;
1258 struct ixgbe_hw *hw = &adapter->hw;
1259 u32 srrctl;
1260
Don Skidmore87e70ab2014-01-16 02:30:08 -08001261 rx_ring = adapter->rx_ring[index];
Greg Rose92915f72010-01-09 02:24:10 +00001262
1263 srrctl = IXGBE_SRRCTL_DROP_EN;
1264
Alexander Duyck77d5dfc2012-05-11 08:32:19 +00001265 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
Greg Rose92915f72010-01-09 02:24:10 +00001266
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001267 srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
1268 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1269
Greg Rose92915f72010-01-09 02:24:10 +00001270 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1271}
1272
Don Skidmore1bb9c632013-09-21 01:57:33 +00001273static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1274{
1275 struct ixgbe_hw *hw = &adapter->hw;
1276
1277 /* PSRTYPE must be initialized in 82599 */
1278 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1279 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1280 IXGBE_PSRTYPE_L2HDR;
1281
1282 if (adapter->num_rx_queues > 1)
1283 psrtype |= 1 << 29;
1284
1285 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1286}
1287
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001288static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
1289{
1290 struct ixgbe_hw *hw = &adapter->hw;
1291 struct net_device *netdev = adapter->netdev;
1292 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1293 int i;
1294 u16 rx_buf_len;
1295
1296 /* notify the PF of our intent to use this size of frame */
1297 ixgbevf_rlpml_set_vf(hw, max_frame);
1298
1299 /* PF will allow an extra 4 bytes past for vlan tagged frames */
1300 max_frame += VLAN_HLEN;
1301
1302 /*
Greg Rose85624ca2012-11-13 04:03:19 +00001303 * Allocate buffer sizes that fit well into 32K and
1304 * take into account max frame size of 9.5K
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001305 */
1306 if ((hw->mac.type == ixgbe_mac_X540_vf) &&
1307 (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
1308 rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose85624ca2012-11-13 04:03:19 +00001309 else if (max_frame <= IXGBEVF_RXBUFFER_2K)
1310 rx_buf_len = IXGBEVF_RXBUFFER_2K;
1311 else if (max_frame <= IXGBEVF_RXBUFFER_4K)
1312 rx_buf_len = IXGBEVF_RXBUFFER_4K;
1313 else if (max_frame <= IXGBEVF_RXBUFFER_8K)
1314 rx_buf_len = IXGBEVF_RXBUFFER_8K;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001315 else
Greg Rose85624ca2012-11-13 04:03:19 +00001316 rx_buf_len = IXGBEVF_RXBUFFER_10K;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001317
1318 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08001319 adapter->rx_ring[i]->rx_buf_len = rx_buf_len;
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001320}
1321
Don Skidmorede02dec2014-01-16 02:30:09 -08001322#define IXGBEVF_MAX_RX_DESC_POLL 10
1323static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1324 struct ixgbevf_ring *ring)
1325{
1326 struct ixgbe_hw *hw = &adapter->hw;
1327 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1328 u32 rxdctl;
1329 u8 reg_idx = ring->reg_idx;
1330
Mark Rustad26597802014-03-04 03:02:45 +00001331 if (IXGBE_REMOVED(hw->hw_addr))
1332 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001333 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1334 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1335
1336 /* write value back with RXDCTL.ENABLE bit cleared */
1337 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1338
1339 /* the hardware may take up to 100us to really disable the rx queue */
1340 do {
1341 udelay(10);
1342 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1343 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1344
1345 if (!wait_loop)
1346 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1347 reg_idx);
1348}
1349
1350static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1351 struct ixgbevf_ring *ring)
1352{
1353 struct ixgbe_hw *hw = &adapter->hw;
1354 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1355 u32 rxdctl;
1356 u8 reg_idx = ring->reg_idx;
1357
Mark Rustad26597802014-03-04 03:02:45 +00001358 if (IXGBE_REMOVED(hw->hw_addr))
1359 return;
Don Skidmorede02dec2014-01-16 02:30:09 -08001360 do {
1361 usleep_range(1000, 2000);
1362 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1363 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1364
1365 if (!wait_loop)
1366 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1367 reg_idx);
1368}
1369
1370static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1371 struct ixgbevf_ring *ring)
1372{
1373 struct ixgbe_hw *hw = &adapter->hw;
1374 u64 rdba = ring->dma;
1375 u32 rxdctl;
1376 u8 reg_idx = ring->reg_idx;
1377
1378 /* disable queue to avoid issues while updating state */
1379 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1380 ixgbevf_disable_rx_queue(adapter, ring);
1381
1382 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1383 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1384 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1385 ring->count * sizeof(union ixgbe_adv_rx_desc));
1386
1387 /* enable relaxed ordering */
1388 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1389 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1390
1391 /* reset head and tail pointers */
1392 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1393 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00001394 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
Don Skidmorede02dec2014-01-16 02:30:09 -08001395
1396 /* reset ntu and ntc to place SW in sync with hardwdare */
1397 ring->next_to_clean = 0;
1398 ring->next_to_use = 0;
1399
1400 ixgbevf_configure_srrctl(adapter, reg_idx);
1401
1402 /* prevent DMA from exceeding buffer space available */
1403 rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
1404 rxdctl |= ring->rx_buf_len | IXGBE_RXDCTL_RLPML_EN;
1405 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1406 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1407
1408 ixgbevf_rx_desc_queue_enable(adapter, ring);
Emil Tantilov095e2612014-01-17 18:30:00 -08001409 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
Don Skidmorede02dec2014-01-16 02:30:09 -08001410}
1411
Greg Rose92915f72010-01-09 02:24:10 +00001412/**
1413 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1414 * @adapter: board private structure
1415 *
1416 * Configure the Rx unit of the MAC after a reset.
1417 **/
1418static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1419{
Don Skidmorede02dec2014-01-16 02:30:09 -08001420 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001421
Don Skidmore1bb9c632013-09-21 01:57:33 +00001422 ixgbevf_setup_psrtype(adapter);
Alexander Duyckdd1fe112012-07-20 08:09:48 +00001423
1424 /* set_rx_buffer_len must be called before ring initialization */
1425 ixgbevf_set_rx_buffer_len(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001426
Greg Rose92915f72010-01-09 02:24:10 +00001427 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1428 * the Base and Length of the Rx Descriptor Ring */
Don Skidmorede02dec2014-01-16 02:30:09 -08001429 for (i = 0; i < adapter->num_rx_queues; i++)
1430 ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001431}
1432
Patrick McHardy80d5c362013-04-19 02:04:28 +00001433static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1434 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001435{
1436 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1437 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001438 int err;
1439
John Fastabend55fdd45b2012-10-01 14:52:20 +00001440 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001441
Greg Rose92915f72010-01-09 02:24:10 +00001442 /* add VID to filter table */
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001443 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001444
John Fastabend55fdd45b2012-10-01 14:52:20 +00001445 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001446
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001447 /* translate error return types so error makes sense */
1448 if (err == IXGBE_ERR_MBX)
1449 return -EIO;
1450
1451 if (err == IXGBE_ERR_INVALID_ARGUMENT)
1452 return -EACCES;
1453
Jiri Pirkodadcd652011-07-21 03:25:09 +00001454 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001455
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001456 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001457}
1458
Patrick McHardy80d5c362013-04-19 02:04:28 +00001459static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1460 __be16 proto, u16 vid)
Greg Rose92915f72010-01-09 02:24:10 +00001461{
1462 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1463 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001464 int err = -EOPNOTSUPP;
Greg Rose92915f72010-01-09 02:24:10 +00001465
John Fastabend55fdd45b2012-10-01 14:52:20 +00001466 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001467
Greg Rose92915f72010-01-09 02:24:10 +00001468 /* remove VID from filter table */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001469 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001470
John Fastabend55fdd45b2012-10-01 14:52:20 +00001471 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001472
Jiri Pirkodadcd652011-07-21 03:25:09 +00001473 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05001474
Alexander Duyck2ddc7fe2012-08-21 00:15:13 +00001475 return err;
Greg Rose92915f72010-01-09 02:24:10 +00001476}
1477
1478static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1479{
Jiri Pirkodadcd652011-07-21 03:25:09 +00001480 u16 vid;
Greg Rose92915f72010-01-09 02:24:10 +00001481
Jiri Pirkodadcd652011-07-21 03:25:09 +00001482 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001483 ixgbevf_vlan_rx_add_vid(adapter->netdev,
1484 htons(ETH_P_8021Q), vid);
Greg Rose92915f72010-01-09 02:24:10 +00001485}
1486
Greg Rose46ec20f2011-05-13 01:33:42 +00001487static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1488{
1489 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1490 struct ixgbe_hw *hw = &adapter->hw;
1491 int count = 0;
1492
1493 if ((netdev_uc_count(netdev)) > 10) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00001494 pr_err("Too many unicast filters - No Space\n");
Greg Rose46ec20f2011-05-13 01:33:42 +00001495 return -ENOSPC;
1496 }
1497
1498 if (!netdev_uc_empty(netdev)) {
1499 struct netdev_hw_addr *ha;
1500 netdev_for_each_uc_addr(ha, netdev) {
1501 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1502 udelay(200);
1503 }
1504 } else {
1505 /*
1506 * If the list is empty then send message to PF driver to
1507 * clear all macvlans on this VF.
1508 */
1509 hw->mac.ops.set_uc_addr(hw, 0, NULL);
1510 }
1511
1512 return count;
1513}
1514
Greg Rose92915f72010-01-09 02:24:10 +00001515/**
Greg Rosedee847f2012-11-02 05:50:57 +00001516 * ixgbevf_set_rx_mode - Multicast and unicast set
Greg Rose92915f72010-01-09 02:24:10 +00001517 * @netdev: network interface device structure
1518 *
1519 * The set_rx_method entry point is called whenever the multicast address
Greg Rosedee847f2012-11-02 05:50:57 +00001520 * list, unicast address list or the network interface flags are updated.
1521 * This routine is responsible for configuring the hardware for proper
1522 * multicast mode and configuring requested unicast filters.
Greg Rose92915f72010-01-09 02:24:10 +00001523 **/
1524static void ixgbevf_set_rx_mode(struct net_device *netdev)
1525{
1526 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1527 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001528
John Fastabend55fdd45b2012-10-01 14:52:20 +00001529 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001530
Greg Rose92915f72010-01-09 02:24:10 +00001531 /* reprogram multicast list */
Greg Rose92fe0bf2012-11-02 05:50:47 +00001532 hw->mac.ops.update_mc_addr_list(hw, netdev);
Greg Rose46ec20f2011-05-13 01:33:42 +00001533
1534 ixgbevf_write_uc_addr_list(netdev);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001535
John Fastabend55fdd45b2012-10-01 14:52:20 +00001536 spin_unlock_bh(&adapter->mbx_lock);
Greg Rose92915f72010-01-09 02:24:10 +00001537}
1538
1539static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1540{
1541 int q_idx;
1542 struct ixgbevf_q_vector *q_vector;
1543 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1544
1545 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Greg Rose92915f72010-01-09 02:24:10 +00001546 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001547#ifdef CONFIG_NET_RX_BUSY_POLL
1548 ixgbevf_qv_init_lock(adapter->q_vector[q_idx]);
1549#endif
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001550 napi_enable(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00001551 }
1552}
1553
1554static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1555{
1556 int q_idx;
1557 struct ixgbevf_q_vector *q_vector;
1558 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1559
1560 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1561 q_vector = adapter->q_vector[q_idx];
Greg Rose92915f72010-01-09 02:24:10 +00001562 napi_disable(&q_vector->napi);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00001563#ifdef CONFIG_NET_RX_BUSY_POLL
1564 while (!ixgbevf_qv_disable(adapter->q_vector[q_idx])) {
1565 pr_info("QV %d locked\n", q_idx);
1566 usleep_range(1000, 20000);
1567 }
1568#endif /* CONFIG_NET_RX_BUSY_POLL */
Greg Rose92915f72010-01-09 02:24:10 +00001569 }
1570}
1571
Don Skidmore220fe052013-09-21 01:40:49 +00001572static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
1573{
1574 struct ixgbe_hw *hw = &adapter->hw;
1575 unsigned int def_q = 0;
1576 unsigned int num_tcs = 0;
1577 unsigned int num_rx_queues = 1;
1578 int err;
1579
1580 spin_lock_bh(&adapter->mbx_lock);
1581
1582 /* fetch queue configuration from the PF */
1583 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1584
1585 spin_unlock_bh(&adapter->mbx_lock);
1586
1587 if (err)
1588 return err;
1589
1590 if (num_tcs > 1) {
1591 /* update default Tx ring register index */
Don Skidmore87e70ab2014-01-16 02:30:08 -08001592 adapter->tx_ring[0]->reg_idx = def_q;
Don Skidmore220fe052013-09-21 01:40:49 +00001593
1594 /* we need as many queues as traffic classes */
1595 num_rx_queues = num_tcs;
1596 }
1597
1598 /* if we have a bad config abort request queue reset */
1599 if (adapter->num_rx_queues != num_rx_queues) {
1600 /* force mailbox timeout to prevent further messages */
1601 hw->mbx.timeout = 0;
1602
1603 /* wait for watchdog to come around and bail us out */
1604 adapter->flags |= IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
1605 }
1606
1607 return 0;
1608}
1609
Greg Rose92915f72010-01-09 02:24:10 +00001610static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
1611{
Don Skidmore220fe052013-09-21 01:40:49 +00001612 ixgbevf_configure_dcb(adapter);
1613
Don Skidmorede02dec2014-01-16 02:30:09 -08001614 ixgbevf_set_rx_mode(adapter->netdev);
Greg Rose92915f72010-01-09 02:24:10 +00001615
1616 ixgbevf_restore_vlan(adapter);
1617
1618 ixgbevf_configure_tx(adapter);
1619 ixgbevf_configure_rx(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001620}
1621
Greg Rose33bd9f62010-03-19 02:59:52 +00001622static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
1623{
1624 /* Only save pre-reset stats if there are some */
1625 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
1626 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
1627 adapter->stats.base_vfgprc;
1628 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
1629 adapter->stats.base_vfgptc;
1630 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
1631 adapter->stats.base_vfgorc;
1632 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
1633 adapter->stats.base_vfgotc;
1634 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
1635 adapter->stats.base_vfmprc;
1636 }
1637}
1638
1639static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
1640{
1641 struct ixgbe_hw *hw = &adapter->hw;
1642
1643 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
1644 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
1645 adapter->stats.last_vfgorc |=
1646 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
1647 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
1648 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
1649 adapter->stats.last_vfgotc |=
1650 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
1651 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
1652
1653 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
1654 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
1655 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
1656 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
1657 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
1658}
1659
Alexander Duyck31186782012-07-20 08:09:58 +00001660static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
1661{
1662 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck56e94092012-07-20 08:10:03 +00001663 int api[] = { ixgbe_mbox_api_11,
1664 ixgbe_mbox_api_10,
Alexander Duyck31186782012-07-20 08:09:58 +00001665 ixgbe_mbox_api_unknown };
1666 int err = 0, idx = 0;
1667
John Fastabend55fdd45b2012-10-01 14:52:20 +00001668 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001669
1670 while (api[idx] != ixgbe_mbox_api_unknown) {
1671 err = ixgbevf_negotiate_api_version(hw, api[idx]);
1672 if (!err)
1673 break;
1674 idx++;
1675 }
1676
John Fastabend55fdd45b2012-10-01 14:52:20 +00001677 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck31186782012-07-20 08:09:58 +00001678}
1679
Greg Rose795180d2012-04-17 04:29:34 +00001680static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001681{
1682 struct net_device *netdev = adapter->netdev;
1683 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose92915f72010-01-09 02:24:10 +00001684
1685 ixgbevf_configure_msix(adapter);
1686
John Fastabend55fdd45b2012-10-01 14:52:20 +00001687 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001688
Greg Rose92fe0bf2012-11-02 05:50:47 +00001689 if (is_valid_ether_addr(hw->mac.addr))
1690 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
1691 else
1692 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00001693
John Fastabend55fdd45b2012-10-01 14:52:20 +00001694 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00001695
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001696 smp_mb__before_atomic();
Greg Rose92915f72010-01-09 02:24:10 +00001697 clear_bit(__IXGBEVF_DOWN, &adapter->state);
1698 ixgbevf_napi_enable_all(adapter);
1699
1700 /* enable transmits */
1701 netif_tx_start_all_queues(netdev);
1702
Greg Rose33bd9f62010-03-19 02:59:52 +00001703 ixgbevf_save_reset_stats(adapter);
1704 ixgbevf_init_last_counter_stats(adapter);
1705
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001706 hw->mac.get_link_status = 1;
Greg Rose92915f72010-01-09 02:24:10 +00001707 mod_timer(&adapter->watchdog_timer, jiffies);
Greg Rose92915f72010-01-09 02:24:10 +00001708}
1709
Greg Rose795180d2012-04-17 04:29:34 +00001710void ixgbevf_up(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00001711{
Greg Rose92915f72010-01-09 02:24:10 +00001712 struct ixgbe_hw *hw = &adapter->hw;
1713
1714 ixgbevf_configure(adapter);
1715
Greg Rose795180d2012-04-17 04:29:34 +00001716 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001717
1718 /* clear any pending interrupts, may auto mask */
1719 IXGBE_READ_REG(hw, IXGBE_VTEICR);
1720
Alexander Duyck5f3600e2012-05-11 08:32:55 +00001721 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001722}
1723
1724/**
1725 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
Greg Rose92915f72010-01-09 02:24:10 +00001726 * @rx_ring: ring to free buffers from
1727 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001728static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001729{
Greg Rose92915f72010-01-09 02:24:10 +00001730 unsigned long size;
1731 unsigned int i;
1732
Greg Rosec0456c22010-01-22 22:47:18 +00001733 if (!rx_ring->rx_buffer_info)
1734 return;
Greg Rose92915f72010-01-09 02:24:10 +00001735
Greg Rosec0456c22010-01-22 22:47:18 +00001736 /* Free all the Rx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00001737 for (i = 0; i < rx_ring->count; i++) {
1738 struct ixgbevf_rx_buffer *rx_buffer_info;
1739
1740 rx_buffer_info = &rx_ring->rx_buffer_info[i];
1741 if (rx_buffer_info->dma) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08001742 dma_unmap_single(rx_ring->dev, rx_buffer_info->dma,
Greg Rose92915f72010-01-09 02:24:10 +00001743 rx_ring->rx_buf_len,
Nick Nunley2a1f8792010-04-27 13:10:50 +00001744 DMA_FROM_DEVICE);
Greg Rose92915f72010-01-09 02:24:10 +00001745 rx_buffer_info->dma = 0;
1746 }
1747 if (rx_buffer_info->skb) {
1748 struct sk_buff *skb = rx_buffer_info->skb;
1749 rx_buffer_info->skb = NULL;
1750 do {
1751 struct sk_buff *this = skb;
Alexander Duyck5c60f812012-09-01 05:12:38 +00001752 skb = IXGBE_CB(skb)->prev;
Greg Rose92915f72010-01-09 02:24:10 +00001753 dev_kfree_skb(this);
1754 } while (skb);
1755 }
Greg Rose92915f72010-01-09 02:24:10 +00001756 }
1757
1758 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
1759 memset(rx_ring->rx_buffer_info, 0, size);
1760
1761 /* Zero out the descriptor ring */
1762 memset(rx_ring->desc, 0, rx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001763}
1764
1765/**
1766 * ixgbevf_clean_tx_ring - Free Tx Buffers
Greg Rose92915f72010-01-09 02:24:10 +00001767 * @tx_ring: ring to be cleaned
1768 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08001769static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00001770{
1771 struct ixgbevf_tx_buffer *tx_buffer_info;
1772 unsigned long size;
1773 unsigned int i;
1774
Greg Rosec0456c22010-01-22 22:47:18 +00001775 if (!tx_ring->tx_buffer_info)
1776 return;
1777
Greg Rose92915f72010-01-09 02:24:10 +00001778 /* Free all the Tx ring sk_buffs */
Greg Rose92915f72010-01-09 02:24:10 +00001779 for (i = 0; i < tx_ring->count; i++) {
1780 tx_buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck70a10e22012-05-11 08:33:21 +00001781 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
Greg Rose92915f72010-01-09 02:24:10 +00001782 }
1783
1784 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
1785 memset(tx_ring->tx_buffer_info, 0, size);
1786
1787 memset(tx_ring->desc, 0, tx_ring->size);
Greg Rose92915f72010-01-09 02:24:10 +00001788}
1789
1790/**
1791 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
1792 * @adapter: board private structure
1793 **/
1794static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
1795{
1796 int i;
1797
1798 for (i = 0; i < adapter->num_rx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08001799 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001800}
1801
1802/**
1803 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
1804 * @adapter: board private structure
1805 **/
1806static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
1807{
1808 int i;
1809
1810 for (i = 0; i < adapter->num_tx_queues; i++)
Emil Tantilov05d063a2014-01-17 18:29:59 -08001811 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001812}
1813
1814void ixgbevf_down(struct ixgbevf_adapter *adapter)
1815{
1816 struct net_device *netdev = adapter->netdev;
1817 struct ixgbe_hw *hw = &adapter->hw;
Don Skidmorede02dec2014-01-16 02:30:09 -08001818 int i;
Greg Rose92915f72010-01-09 02:24:10 +00001819
1820 /* signal that we are down to the interrupt handler */
Mark Rustad5b346dc2014-03-04 03:02:18 +00001821 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
1822 return; /* do nothing if already down */
Don Skidmore858c3dd2013-10-01 04:33:50 -07001823
1824 /* disable all enabled rx queues */
1825 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08001826 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00001827
1828 netif_tx_disable(netdev);
1829
1830 msleep(10);
1831
1832 netif_tx_stop_all_queues(netdev);
1833
1834 ixgbevf_irq_disable(adapter);
1835
1836 ixgbevf_napi_disable_all(adapter);
1837
1838 del_timer_sync(&adapter->watchdog_timer);
1839 /* can't call flush scheduled work here because it can deadlock
1840 * if linkwatch_event tries to acquire the rtnl_lock which we are
1841 * holding */
1842 while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
1843 msleep(1);
1844
1845 /* disable transmits in the hardware now that interrupts are off */
1846 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmorede02dec2014-01-16 02:30:09 -08001847 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
1848
1849 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
1850 IXGBE_TXDCTL_SWFLSH);
Greg Rose92915f72010-01-09 02:24:10 +00001851 }
1852
1853 netif_carrier_off(netdev);
1854
1855 if (!pci_channel_offline(adapter->pdev))
1856 ixgbevf_reset(adapter);
1857
1858 ixgbevf_clean_all_tx_rings(adapter);
1859 ixgbevf_clean_all_rx_rings(adapter);
1860}
1861
1862void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
1863{
1864 WARN_ON(in_interrupt());
Greg Rosec0456c22010-01-22 22:47:18 +00001865
Greg Rose92915f72010-01-09 02:24:10 +00001866 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
1867 msleep(1);
1868
Alexander Duyck4b2cd272012-08-02 01:16:59 +00001869 ixgbevf_down(adapter);
1870 ixgbevf_up(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00001871
1872 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
1873}
1874
1875void ixgbevf_reset(struct ixgbevf_adapter *adapter)
1876{
1877 struct ixgbe_hw *hw = &adapter->hw;
1878 struct net_device *netdev = adapter->netdev;
1879
Don Skidmore798e3812013-10-01 04:33:51 -07001880 if (hw->mac.ops.reset_hw(hw)) {
Greg Rose92915f72010-01-09 02:24:10 +00001881 hw_dbg(hw, "PF still resetting\n");
Don Skidmore798e3812013-10-01 04:33:51 -07001882 } else {
Greg Rose92915f72010-01-09 02:24:10 +00001883 hw->mac.ops.init_hw(hw);
Don Skidmore798e3812013-10-01 04:33:51 -07001884 ixgbevf_negotiate_api(adapter);
1885 }
Greg Rose92915f72010-01-09 02:24:10 +00001886
1887 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
1888 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
1889 netdev->addr_len);
1890 memcpy(netdev->perm_addr, adapter->hw.mac.addr,
1891 netdev->addr_len);
1892 }
1893}
1894
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00001895static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
1896 int vectors)
Greg Rose92915f72010-01-09 02:24:10 +00001897{
Emil Tantilova5f93372012-11-13 04:03:17 +00001898 int vector_threshold;
Greg Rose92915f72010-01-09 02:24:10 +00001899
Alexander Duyckfa71ae22012-05-11 08:32:50 +00001900 /* We'll want at least 2 (vector_threshold):
1901 * 1) TxQ[0] + RxQ[0] handler
1902 * 2) Other (Link Status Change, etc.)
Greg Rose92915f72010-01-09 02:24:10 +00001903 */
1904 vector_threshold = MIN_MSIX_COUNT;
1905
1906 /* The more we get, the more we will assign to Tx/Rx Cleanup
1907 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1908 * Right now, we simply care about how many we'll get; we'll
1909 * set them up later while requesting irq's.
1910 */
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001911 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1912 vector_threshold, vectors);
Greg Rose92915f72010-01-09 02:24:10 +00001913
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001914 if (vectors < 0) {
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00001915 dev_err(&adapter->pdev->dev,
1916 "Unable to allocate MSI-X interrupts\n");
Greg Rose92915f72010-01-09 02:24:10 +00001917 kfree(adapter->msix_entries);
1918 adapter->msix_entries = NULL;
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001919 return vectors;
Greg Rose92915f72010-01-09 02:24:10 +00001920 }
Greg Rosedee847f2012-11-02 05:50:57 +00001921
Alexander Gordeev5c1e35882014-02-18 11:11:46 +01001922 /* Adjust for only the vectors we'll use, which is minimum
1923 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
1924 * vectors we were allocated.
1925 */
1926 adapter->num_msix_vectors = vectors;
1927
1928 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00001929}
1930
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001931/**
1932 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
Greg Rose92915f72010-01-09 02:24:10 +00001933 * @adapter: board private structure to initialize
1934 *
1935 * This is the top level queue allocation routine. The order here is very
1936 * important, starting with the "most" number of features turned on at once,
1937 * and ending with the smallest set of features. This way large combinations
1938 * can be allocated if they're turned on, and smaller combinations are the
1939 * fallthrough conditions.
1940 *
1941 **/
1942static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
1943{
Don Skidmore220fe052013-09-21 01:40:49 +00001944 struct ixgbe_hw *hw = &adapter->hw;
1945 unsigned int def_q = 0;
1946 unsigned int num_tcs = 0;
1947 int err;
1948
Greg Rose92915f72010-01-09 02:24:10 +00001949 /* Start with base case */
1950 adapter->num_rx_queues = 1;
1951 adapter->num_tx_queues = 1;
Don Skidmore220fe052013-09-21 01:40:49 +00001952
1953 spin_lock_bh(&adapter->mbx_lock);
1954
1955 /* fetch queue configuration from the PF */
1956 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1957
1958 spin_unlock_bh(&adapter->mbx_lock);
1959
1960 if (err)
1961 return;
1962
1963 /* we need as many queues as traffic classes */
1964 if (num_tcs > 1)
1965 adapter->num_rx_queues = num_tcs;
Greg Rose92915f72010-01-09 02:24:10 +00001966}
1967
1968/**
1969 * ixgbevf_alloc_queues - Allocate memory for all rings
1970 * @adapter: board private structure to initialize
1971 *
1972 * We allocate one ring per queue at run-time since we don't know the
1973 * number of queues at compile-time. The polling_netdev array is
1974 * intended for Multiqueue, but should work fine with a single queue.
1975 **/
1976static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
1977{
Don Skidmore87e70ab2014-01-16 02:30:08 -08001978 struct ixgbevf_ring *ring;
1979 int rx = 0, tx = 0;
Greg Rose92915f72010-01-09 02:24:10 +00001980
Don Skidmore87e70ab2014-01-16 02:30:08 -08001981 for (; tx < adapter->num_tx_queues; tx++) {
1982 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1983 if (!ring)
1984 goto err_allocation;
Greg Rose92915f72010-01-09 02:24:10 +00001985
Don Skidmore87e70ab2014-01-16 02:30:08 -08001986 ring->dev = &adapter->pdev->dev;
1987 ring->netdev = adapter->netdev;
1988 ring->count = adapter->tx_ring_count;
1989 ring->queue_index = tx;
1990 ring->reg_idx = tx;
Greg Rose92915f72010-01-09 02:24:10 +00001991
Don Skidmore87e70ab2014-01-16 02:30:08 -08001992 adapter->tx_ring[tx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00001993 }
1994
Don Skidmore87e70ab2014-01-16 02:30:08 -08001995 for (; rx < adapter->num_rx_queues; rx++) {
1996 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1997 if (!ring)
1998 goto err_allocation;
1999
2000 ring->dev = &adapter->pdev->dev;
2001 ring->netdev = adapter->netdev;
2002
2003 ring->count = adapter->rx_ring_count;
2004 ring->queue_index = rx;
2005 ring->reg_idx = rx;
2006
2007 adapter->rx_ring[rx] = ring;
Greg Rose92915f72010-01-09 02:24:10 +00002008 }
2009
2010 return 0;
2011
Don Skidmore87e70ab2014-01-16 02:30:08 -08002012err_allocation:
2013 while (tx) {
2014 kfree(adapter->tx_ring[--tx]);
2015 adapter->tx_ring[tx] = NULL;
2016 }
2017
2018 while (rx) {
2019 kfree(adapter->rx_ring[--rx]);
2020 adapter->rx_ring[rx] = NULL;
2021 }
Greg Rose92915f72010-01-09 02:24:10 +00002022 return -ENOMEM;
2023}
2024
2025/**
2026 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2027 * @adapter: board private structure to initialize
2028 *
2029 * Attempt to configure the interrupts using the best available
2030 * capabilities of the hardware and the kernel.
2031 **/
2032static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2033{
Greg Rose91e2b892012-10-03 00:57:23 +00002034 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002035 int err = 0;
2036 int vector, v_budget;
2037
2038 /*
2039 * It's easy to be greedy for MSI-X vectors, but it really
2040 * doesn't do us much good if we have a lot more vectors
2041 * than CPU's. So let's be conservative and only ask for
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002042 * (roughly) the same number of vectors as there are CPU's.
2043 * The default is to use pairs of vectors.
Greg Rose92915f72010-01-09 02:24:10 +00002044 */
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002045 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2046 v_budget = min_t(int, v_budget, num_online_cpus());
2047 v_budget += NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002048
2049 /* A failure in MSI-X entry allocation isn't fatal, but it does
2050 * mean we disable MSI-X capabilities of the adapter. */
2051 adapter->msix_entries = kcalloc(v_budget,
2052 sizeof(struct msix_entry), GFP_KERNEL);
2053 if (!adapter->msix_entries) {
2054 err = -ENOMEM;
2055 goto out;
2056 }
2057
2058 for (vector = 0; vector < v_budget; vector++)
2059 adapter->msix_entries[vector].entry = vector;
2060
Jakub Kicinskie45dd5f2012-11-13 04:03:16 +00002061 err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
2062 if (err)
2063 goto out;
Greg Rose92915f72010-01-09 02:24:10 +00002064
Greg Rose91e2b892012-10-03 00:57:23 +00002065 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
2066 if (err)
2067 goto out;
2068
2069 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
2070
Greg Rose92915f72010-01-09 02:24:10 +00002071out:
2072 return err;
2073}
2074
2075/**
2076 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2077 * @adapter: board private structure to initialize
2078 *
2079 * We allocate one q_vector per queue interrupt. If allocation fails we
2080 * return -ENOMEM.
2081 **/
2082static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2083{
2084 int q_idx, num_q_vectors;
2085 struct ixgbevf_q_vector *q_vector;
Greg Rose92915f72010-01-09 02:24:10 +00002086
2087 num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002088
2089 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2090 q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2091 if (!q_vector)
2092 goto err_out;
2093 q_vector->adapter = adapter;
2094 q_vector->v_idx = q_idx;
Alexander Duyckfa71ae22012-05-11 08:32:50 +00002095 netif_napi_add(adapter->netdev, &q_vector->napi,
2096 ixgbevf_poll, 64);
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002097#ifdef CONFIG_NET_RX_BUSY_POLL
2098 napi_hash_add(&q_vector->napi);
2099#endif
Greg Rose92915f72010-01-09 02:24:10 +00002100 adapter->q_vector[q_idx] = q_vector;
2101 }
2102
2103 return 0;
2104
2105err_out:
2106 while (q_idx) {
2107 q_idx--;
2108 q_vector = adapter->q_vector[q_idx];
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002109#ifdef CONFIG_NET_RX_BUSY_POLL
2110 napi_hash_del(&q_vector->napi);
2111#endif
Greg Rose92915f72010-01-09 02:24:10 +00002112 netif_napi_del(&q_vector->napi);
2113 kfree(q_vector);
2114 adapter->q_vector[q_idx] = NULL;
2115 }
2116 return -ENOMEM;
2117}
2118
2119/**
2120 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2121 * @adapter: board private structure to initialize
2122 *
2123 * This function frees the memory allocated to the q_vectors. In addition if
2124 * NAPI is enabled it will delete any references to the NAPI struct prior
2125 * to freeing the q_vector.
2126 **/
2127static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2128{
John Fastabendf4477702012-09-16 08:19:46 +00002129 int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
Greg Rose92915f72010-01-09 02:24:10 +00002130
2131 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2132 struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2133
2134 adapter->q_vector[q_idx] = NULL;
Jacob Kellerc777cdf2013-09-21 06:24:20 +00002135#ifdef CONFIG_NET_RX_BUSY_POLL
2136 napi_hash_del(&q_vector->napi);
2137#endif
John Fastabendf4477702012-09-16 08:19:46 +00002138 netif_napi_del(&q_vector->napi);
Greg Rose92915f72010-01-09 02:24:10 +00002139 kfree(q_vector);
2140 }
2141}
2142
2143/**
2144 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2145 * @adapter: board private structure
2146 *
2147 **/
2148static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2149{
2150 pci_disable_msix(adapter->pdev);
2151 kfree(adapter->msix_entries);
2152 adapter->msix_entries = NULL;
Greg Rose92915f72010-01-09 02:24:10 +00002153}
2154
2155/**
2156 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2157 * @adapter: board private structure to initialize
2158 *
2159 **/
2160static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2161{
2162 int err;
2163
2164 /* Number of supported queues */
2165 ixgbevf_set_num_queues(adapter);
2166
2167 err = ixgbevf_set_interrupt_capability(adapter);
2168 if (err) {
2169 hw_dbg(&adapter->hw,
2170 "Unable to setup interrupt capabilities\n");
2171 goto err_set_interrupt;
2172 }
2173
2174 err = ixgbevf_alloc_q_vectors(adapter);
2175 if (err) {
2176 hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
2177 "vectors\n");
2178 goto err_alloc_q_vectors;
2179 }
2180
2181 err = ixgbevf_alloc_queues(adapter);
2182 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002183 pr_err("Unable to allocate memory for queues\n");
Greg Rose92915f72010-01-09 02:24:10 +00002184 goto err_alloc_queues;
2185 }
2186
2187 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
2188 "Tx Queue count = %u\n",
2189 (adapter->num_rx_queues > 1) ? "Enabled" :
2190 "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2191
2192 set_bit(__IXGBEVF_DOWN, &adapter->state);
2193
2194 return 0;
2195err_alloc_queues:
2196 ixgbevf_free_q_vectors(adapter);
2197err_alloc_q_vectors:
2198 ixgbevf_reset_interrupt_capability(adapter);
2199err_set_interrupt:
2200 return err;
2201}
2202
2203/**
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002204 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2205 * @adapter: board private structure to clear interrupt scheme on
2206 *
2207 * We go through and clear interrupt specific resources and reset the structure
2208 * to pre-load conditions
2209 **/
2210static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2211{
Don Skidmore87e70ab2014-01-16 02:30:08 -08002212 int i;
2213
2214 for (i = 0; i < adapter->num_tx_queues; i++) {
2215 kfree(adapter->tx_ring[i]);
2216 adapter->tx_ring[i] = NULL;
2217 }
2218 for (i = 0; i < adapter->num_rx_queues; i++) {
2219 kfree(adapter->rx_ring[i]);
2220 adapter->rx_ring[i] = NULL;
2221 }
2222
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00002223 adapter->num_tx_queues = 0;
2224 adapter->num_rx_queues = 0;
2225
2226 ixgbevf_free_q_vectors(adapter);
2227 ixgbevf_reset_interrupt_capability(adapter);
2228}
2229
2230/**
Greg Rose92915f72010-01-09 02:24:10 +00002231 * ixgbevf_sw_init - Initialize general software structures
2232 * (struct ixgbevf_adapter)
2233 * @adapter: board private structure to initialize
2234 *
2235 * ixgbevf_sw_init initializes the Adapter private data structure.
2236 * Fields are initialized based on PCI device information and
2237 * OS network device settings (MTU size).
2238 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002239static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
Greg Rose92915f72010-01-09 02:24:10 +00002240{
2241 struct ixgbe_hw *hw = &adapter->hw;
2242 struct pci_dev *pdev = adapter->pdev;
Greg Rosee1941a72013-02-13 03:02:05 +00002243 struct net_device *netdev = adapter->netdev;
Greg Rose92915f72010-01-09 02:24:10 +00002244 int err;
2245
2246 /* PCI config space info */
2247
2248 hw->vendor_id = pdev->vendor;
2249 hw->device_id = pdev->device;
Sergei Shtylyovff938e42011-02-28 11:57:33 -08002250 hw->revision_id = pdev->revision;
Greg Rose92915f72010-01-09 02:24:10 +00002251 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2252 hw->subsystem_device_id = pdev->subsystem_device;
2253
2254 hw->mbx.ops.init_params(hw);
Alexander Duyck56e94092012-07-20 08:10:03 +00002255
2256 /* assume legacy case in which PF would only give VF 2 queues */
2257 hw->mac.max_tx_queues = 2;
2258 hw->mac.max_rx_queues = 2;
2259
Don Skidmore798e3812013-10-01 04:33:51 -07002260 /* lock to protect mailbox accesses */
2261 spin_lock_init(&adapter->mbx_lock);
2262
Greg Rose92915f72010-01-09 02:24:10 +00002263 err = hw->mac.ops.reset_hw(hw);
2264 if (err) {
2265 dev_info(&pdev->dev,
Greg Rosee1941a72013-02-13 03:02:05 +00002266 "PF still in reset state. Is the PF interface up?\n");
Greg Rose92915f72010-01-09 02:24:10 +00002267 } else {
2268 err = hw->mac.ops.init_hw(hw);
2269 if (err) {
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002270 pr_err("init_shared_code failed: %d\n", err);
Greg Rose92915f72010-01-09 02:24:10 +00002271 goto out;
2272 }
Don Skidmore798e3812013-10-01 04:33:51 -07002273 ixgbevf_negotiate_api(adapter);
Greg Rosee1941a72013-02-13 03:02:05 +00002274 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2275 if (err)
2276 dev_info(&pdev->dev, "Error reading MAC address\n");
2277 else if (is_zero_ether_addr(adapter->hw.mac.addr))
2278 dev_info(&pdev->dev,
2279 "MAC address not assigned by administrator.\n");
2280 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2281 }
2282
2283 if (!is_valid_ether_addr(netdev->dev_addr)) {
2284 dev_info(&pdev->dev, "Assigning random MAC address\n");
2285 eth_hw_addr_random(netdev);
2286 memcpy(hw->mac.addr, netdev->dev_addr, netdev->addr_len);
Greg Rose92915f72010-01-09 02:24:10 +00002287 }
2288
2289 /* Enable dynamic interrupt throttling rates */
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002290 adapter->rx_itr_setting = 1;
2291 adapter->tx_itr_setting = 1;
Greg Rose92915f72010-01-09 02:24:10 +00002292
Greg Rose92915f72010-01-09 02:24:10 +00002293 /* set default ring sizes */
2294 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2295 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2296
Greg Rose92915f72010-01-09 02:24:10 +00002297 set_bit(__IXGBEVF_DOWN, &adapter->state);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00002298 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002299
2300out:
2301 return err;
2302}
2303
Greg Rose92915f72010-01-09 02:24:10 +00002304#define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
2305 { \
2306 u32 current_counter = IXGBE_READ_REG(hw, reg); \
2307 if (current_counter < last_counter) \
2308 counter += 0x100000000LL; \
2309 last_counter = current_counter; \
2310 counter &= 0xFFFFFFFF00000000LL; \
2311 counter |= current_counter; \
2312 }
2313
2314#define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2315 { \
2316 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
2317 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
2318 u64 current_counter = (current_counter_msb << 32) | \
2319 current_counter_lsb; \
2320 if (current_counter < last_counter) \
2321 counter += 0x1000000000LL; \
2322 last_counter = current_counter; \
2323 counter &= 0xFFFFFFF000000000LL; \
2324 counter |= current_counter; \
2325 }
2326/**
2327 * ixgbevf_update_stats - Update the board statistics counters.
2328 * @adapter: board private structure
2329 **/
2330void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2331{
2332 struct ixgbe_hw *hw = &adapter->hw;
Greg Rose55fb2772012-11-06 05:53:32 +00002333 int i;
Greg Rose92915f72010-01-09 02:24:10 +00002334
Greg Rose088245a2013-01-04 07:37:31 +00002335 if (!adapter->link_up)
2336 return;
2337
Greg Rose92915f72010-01-09 02:24:10 +00002338 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2339 adapter->stats.vfgprc);
2340 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2341 adapter->stats.vfgptc);
2342 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2343 adapter->stats.last_vfgorc,
2344 adapter->stats.vfgorc);
2345 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2346 adapter->stats.last_vfgotc,
2347 adapter->stats.vfgotc);
2348 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2349 adapter->stats.vfmprc);
Greg Rose55fb2772012-11-06 05:53:32 +00002350
2351 for (i = 0; i < adapter->num_rx_queues; i++) {
2352 adapter->hw_csum_rx_error +=
Don Skidmore87e70ab2014-01-16 02:30:08 -08002353 adapter->rx_ring[i]->hw_csum_rx_error;
Don Skidmore87e70ab2014-01-16 02:30:08 -08002354 adapter->rx_ring[i]->hw_csum_rx_error = 0;
Greg Rose55fb2772012-11-06 05:53:32 +00002355 }
Greg Rose92915f72010-01-09 02:24:10 +00002356}
2357
2358/**
2359 * ixgbevf_watchdog - Timer Call-back
2360 * @data: pointer to adapter cast into an unsigned long
2361 **/
2362static void ixgbevf_watchdog(unsigned long data)
2363{
2364 struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
2365 struct ixgbe_hw *hw = &adapter->hw;
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002366 u32 eics = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002367 int i;
2368
2369 /*
2370 * Do the watchdog outside of interrupt context due to the lovely
2371 * delays that some of the newer hardware requires
2372 */
2373
2374 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
2375 goto watchdog_short_circuit;
2376
2377 /* get one bit for every active tx/rx interrupt vector */
2378 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2379 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
Alexander Duyck6b43c442012-05-11 08:32:45 +00002380 if (qv->rx.ring || qv->tx.ring)
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002381 eics |= 1 << i;
Greg Rose92915f72010-01-09 02:24:10 +00002382 }
2383
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002384 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
Greg Rose92915f72010-01-09 02:24:10 +00002385
2386watchdog_short_circuit:
2387 schedule_work(&adapter->watchdog_task);
2388}
2389
2390/**
2391 * ixgbevf_tx_timeout - Respond to a Tx Hang
2392 * @netdev: network interface device structure
2393 **/
2394static void ixgbevf_tx_timeout(struct net_device *netdev)
2395{
2396 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2397
2398 /* Do the reset outside of interrupt context */
2399 schedule_work(&adapter->reset_task);
2400}
2401
2402static void ixgbevf_reset_task(struct work_struct *work)
2403{
2404 struct ixgbevf_adapter *adapter;
2405 adapter = container_of(work, struct ixgbevf_adapter, reset_task);
2406
2407 /* If we're already down or resetting, just bail */
2408 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002409 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
Greg Rose92915f72010-01-09 02:24:10 +00002410 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2411 return;
2412
2413 adapter->tx_timeout_count++;
2414
2415 ixgbevf_reinit_locked(adapter);
2416}
2417
2418/**
2419 * ixgbevf_watchdog_task - worker thread to bring link up
2420 * @work: pointer to work_struct containing our data
2421 **/
2422static void ixgbevf_watchdog_task(struct work_struct *work)
2423{
2424 struct ixgbevf_adapter *adapter = container_of(work,
2425 struct ixgbevf_adapter,
2426 watchdog_task);
2427 struct net_device *netdev = adapter->netdev;
2428 struct ixgbe_hw *hw = &adapter->hw;
2429 u32 link_speed = adapter->link_speed;
2430 bool link_up = adapter->link_up;
Greg Rose92fe0bf2012-11-02 05:50:47 +00002431 s32 need_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002432
Mark Rustad26597802014-03-04 03:02:45 +00002433 if (IXGBE_REMOVED(hw->hw_addr)) {
2434 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
2435 rtnl_lock();
2436 ixgbevf_down(adapter);
2437 rtnl_unlock();
2438 }
2439 return;
2440 }
Don Skidmore220fe052013-09-21 01:40:49 +00002441 ixgbevf_queue_reset_subtask(adapter);
2442
Greg Rose92915f72010-01-09 02:24:10 +00002443 adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
2444
2445 /*
2446 * Always check the link on the watchdog because we have
2447 * no LSC interrupt
2448 */
Greg Rose92fe0bf2012-11-02 05:50:47 +00002449 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002450
Greg Rose92fe0bf2012-11-02 05:50:47 +00002451 need_reset = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002452
Greg Rose92fe0bf2012-11-02 05:50:47 +00002453 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00002454
Greg Rose92fe0bf2012-11-02 05:50:47 +00002455 if (need_reset) {
2456 adapter->link_up = link_up;
2457 adapter->link_speed = link_speed;
2458 netif_carrier_off(netdev);
2459 netif_tx_stop_all_queues(netdev);
2460 schedule_work(&adapter->reset_task);
2461 goto pf_has_reset;
Greg Rose92915f72010-01-09 02:24:10 +00002462 }
2463 adapter->link_up = link_up;
2464 adapter->link_speed = link_speed;
2465
2466 if (link_up) {
2467 if (!netif_carrier_ok(netdev)) {
Greg Roseb876a742013-01-19 06:40:22 +00002468 char *link_speed_string;
2469 switch (link_speed) {
2470 case IXGBE_LINK_SPEED_10GB_FULL:
2471 link_speed_string = "10 Gbps";
2472 break;
2473 case IXGBE_LINK_SPEED_1GB_FULL:
2474 link_speed_string = "1 Gbps";
2475 break;
2476 case IXGBE_LINK_SPEED_100_FULL:
2477 link_speed_string = "100 Mbps";
2478 break;
2479 default:
2480 link_speed_string = "unknown speed";
2481 break;
2482 }
Greg Rose6fe59672013-01-04 07:37:26 +00002483 dev_info(&adapter->pdev->dev,
Greg Roseb876a742013-01-19 06:40:22 +00002484 "NIC Link is Up, %s\n", link_speed_string);
Greg Rose92915f72010-01-09 02:24:10 +00002485 netif_carrier_on(netdev);
2486 netif_tx_wake_all_queues(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00002487 }
2488 } else {
2489 adapter->link_up = false;
2490 adapter->link_speed = 0;
2491 if (netif_carrier_ok(netdev)) {
Greg Rose6fe59672013-01-04 07:37:26 +00002492 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
Greg Rose92915f72010-01-09 02:24:10 +00002493 netif_carrier_off(netdev);
2494 netif_tx_stop_all_queues(netdev);
2495 }
2496 }
2497
Greg Rose92915f72010-01-09 02:24:10 +00002498 ixgbevf_update_stats(adapter);
2499
Greg Rose33bd9f62010-03-19 02:59:52 +00002500pf_has_reset:
Greg Rose92915f72010-01-09 02:24:10 +00002501 /* Reset the timer */
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00002502 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
2503 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
Greg Rose92915f72010-01-09 02:24:10 +00002504 mod_timer(&adapter->watchdog_timer,
2505 round_jiffies(jiffies + (2 * HZ)));
2506
2507 adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
2508}
2509
2510/**
2511 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
Greg Rose92915f72010-01-09 02:24:10 +00002512 * @tx_ring: Tx descriptor ring for a specific queue
2513 *
2514 * Free all transmit software resources
2515 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002516void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002517{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002518 ixgbevf_clean_tx_ring(tx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002519
2520 vfree(tx_ring->tx_buffer_info);
2521 tx_ring->tx_buffer_info = NULL;
2522
Don Skidmorede02dec2014-01-16 02:30:09 -08002523 /* if not set, then don't free */
2524 if (!tx_ring->desc)
2525 return;
2526
Emil Tantilov05d063a2014-01-17 18:29:59 -08002527 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002528 tx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002529
2530 tx_ring->desc = NULL;
2531}
2532
2533/**
2534 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
2535 * @adapter: board private structure
2536 *
2537 * Free all transmit software resources
2538 **/
2539static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
2540{
2541 int i;
2542
2543 for (i = 0; i < adapter->num_tx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002544 if (adapter->tx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002545 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002546}
2547
2548/**
2549 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002550 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2551 *
2552 * Return 0 on success, negative on failure
2553 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002554int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002555{
Greg Rose92915f72010-01-09 02:24:10 +00002556 int size;
2557
2558 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002559 tx_ring->tx_buffer_info = vzalloc(size);
Greg Rose92915f72010-01-09 02:24:10 +00002560 if (!tx_ring->tx_buffer_info)
2561 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002562
2563 /* round up to nearest 4K */
2564 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
2565 tx_ring->size = ALIGN(tx_ring->size, 4096);
2566
Emil Tantilov05d063a2014-01-17 18:29:59 -08002567 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002568 &tx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002569 if (!tx_ring->desc)
2570 goto err;
2571
Greg Rose92915f72010-01-09 02:24:10 +00002572 return 0;
2573
2574err:
2575 vfree(tx_ring->tx_buffer_info);
2576 tx_ring->tx_buffer_info = NULL;
2577 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
2578 "descriptor ring\n");
2579 return -ENOMEM;
2580}
2581
2582/**
2583 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
2584 * @adapter: board private structure
2585 *
2586 * If this function returns with an error, then it's possible one or
2587 * more of the rings is populated (while the rest are not). It is the
2588 * callers duty to clean those orphaned rings.
2589 *
2590 * Return 0 on success, negative on failure
2591 **/
2592static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
2593{
2594 int i, err = 0;
2595
2596 for (i = 0; i < adapter->num_tx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002597 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002598 if (!err)
2599 continue;
2600 hw_dbg(&adapter->hw,
2601 "Allocation for Tx Queue %u failed\n", i);
2602 break;
2603 }
2604
2605 return err;
2606}
2607
2608/**
2609 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
Greg Rose92915f72010-01-09 02:24:10 +00002610 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2611 *
2612 * Returns 0 on success, negative on failure
2613 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002614int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002615{
Greg Rose92915f72010-01-09 02:24:10 +00002616 int size;
2617
2618 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
Eric Dumazet89bf67f2010-11-22 00:15:06 +00002619 rx_ring->rx_buffer_info = vzalloc(size);
Joe Perchese404dec2012-01-29 12:56:23 +00002620 if (!rx_ring->rx_buffer_info)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002621 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002622
2623 /* Round up to nearest 4K */
2624 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
2625 rx_ring->size = ALIGN(rx_ring->size, 4096);
2626
Emil Tantilov05d063a2014-01-17 18:29:59 -08002627 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002628 &rx_ring->dma, GFP_KERNEL);
Greg Rose92915f72010-01-09 02:24:10 +00002629
Emil Tantilov05d063a2014-01-17 18:29:59 -08002630 if (!rx_ring->desc)
2631 goto err;
Greg Rose92915f72010-01-09 02:24:10 +00002632
Greg Rose92915f72010-01-09 02:24:10 +00002633 return 0;
Emil Tantilov05d063a2014-01-17 18:29:59 -08002634err:
2635 vfree(rx_ring->rx_buffer_info);
2636 rx_ring->rx_buffer_info = NULL;
2637 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
Greg Rose92915f72010-01-09 02:24:10 +00002638 return -ENOMEM;
2639}
2640
2641/**
2642 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
2643 * @adapter: board private structure
2644 *
2645 * If this function returns with an error, then it's possible one or
2646 * more of the rings is populated (while the rest are not). It is the
2647 * callers duty to clean those orphaned rings.
2648 *
2649 * Return 0 on success, negative on failure
2650 **/
2651static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
2652{
2653 int i, err = 0;
2654
2655 for (i = 0; i < adapter->num_rx_queues; i++) {
Emil Tantilov05d063a2014-01-17 18:29:59 -08002656 err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002657 if (!err)
2658 continue;
2659 hw_dbg(&adapter->hw,
2660 "Allocation for Rx Queue %u failed\n", i);
2661 break;
2662 }
2663 return err;
2664}
2665
2666/**
2667 * ixgbevf_free_rx_resources - Free Rx Resources
Greg Rose92915f72010-01-09 02:24:10 +00002668 * @rx_ring: ring to clean the resources from
2669 *
2670 * Free all receive software resources
2671 **/
Emil Tantilov05d063a2014-01-17 18:29:59 -08002672void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
Greg Rose92915f72010-01-09 02:24:10 +00002673{
Emil Tantilov05d063a2014-01-17 18:29:59 -08002674 ixgbevf_clean_rx_ring(rx_ring);
Greg Rose92915f72010-01-09 02:24:10 +00002675
2676 vfree(rx_ring->rx_buffer_info);
2677 rx_ring->rx_buffer_info = NULL;
2678
Emil Tantilov05d063a2014-01-17 18:29:59 -08002679 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
Nick Nunley2a1f8792010-04-27 13:10:50 +00002680 rx_ring->dma);
Greg Rose92915f72010-01-09 02:24:10 +00002681
2682 rx_ring->desc = NULL;
2683}
2684
2685/**
2686 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
2687 * @adapter: board private structure
2688 *
2689 * Free all receive software resources
2690 **/
2691static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
2692{
2693 int i;
2694
2695 for (i = 0; i < adapter->num_rx_queues; i++)
Don Skidmore87e70ab2014-01-16 02:30:08 -08002696 if (adapter->rx_ring[i]->desc)
Emil Tantilov05d063a2014-01-17 18:29:59 -08002697 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
Greg Rose92915f72010-01-09 02:24:10 +00002698}
2699
2700/**
2701 * ixgbevf_open - Called when a network interface is made active
2702 * @netdev: network interface device structure
2703 *
2704 * Returns 0 on success, negative value on failure
2705 *
2706 * The open entry point is called when a network interface is made
2707 * active by the system (IFF_UP). At this point all resources needed
2708 * for transmit and receive operations are allocated, the interrupt
2709 * handler is registered with the OS, the watchdog timer is started,
2710 * and the stack is notified that the interface is ready.
2711 **/
2712static int ixgbevf_open(struct net_device *netdev)
2713{
2714 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2715 struct ixgbe_hw *hw = &adapter->hw;
2716 int err;
2717
xunleera1f6c6b2013-03-05 07:44:20 +00002718 /* A previous failure to open the device because of a lack of
2719 * available MSIX vector resources may have reset the number
2720 * of msix vectors variable to zero. The only way to recover
2721 * is to unload/reload the driver and hope that the system has
2722 * been able to recover some MSIX vector resources.
2723 */
2724 if (!adapter->num_msix_vectors)
2725 return -ENOMEM;
2726
Greg Rose92915f72010-01-09 02:24:10 +00002727 /* disallow open during test */
2728 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
2729 return -EBUSY;
2730
2731 if (hw->adapter_stopped) {
2732 ixgbevf_reset(adapter);
2733 /* if adapter is still stopped then PF isn't up and
2734 * the vf can't start. */
2735 if (hw->adapter_stopped) {
2736 err = IXGBE_ERR_MBX;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00002737 pr_err("Unable to start - perhaps the PF Driver isn't "
2738 "up yet\n");
Greg Rose92915f72010-01-09 02:24:10 +00002739 goto err_setup_reset;
2740 }
2741 }
2742
2743 /* allocate transmit descriptors */
2744 err = ixgbevf_setup_all_tx_resources(adapter);
2745 if (err)
2746 goto err_setup_tx;
2747
2748 /* allocate receive descriptors */
2749 err = ixgbevf_setup_all_rx_resources(adapter);
2750 if (err)
2751 goto err_setup_rx;
2752
2753 ixgbevf_configure(adapter);
2754
2755 /*
2756 * Map the Tx/Rx rings to the vectors we were allotted.
2757 * if request_irq will be called in this function map_rings
2758 * must be called *before* up_complete
2759 */
2760 ixgbevf_map_rings_to_vectors(adapter);
2761
Greg Rose795180d2012-04-17 04:29:34 +00002762 ixgbevf_up_complete(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002763
2764 /* clear any pending interrupts, may auto mask */
2765 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2766 err = ixgbevf_request_irq(adapter);
2767 if (err)
2768 goto err_req_irq;
2769
Alexander Duyck5f3600e2012-05-11 08:32:55 +00002770 ixgbevf_irq_enable(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002771
2772 return 0;
2773
2774err_req_irq:
2775 ixgbevf_down(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00002776err_setup_rx:
2777 ixgbevf_free_all_rx_resources(adapter);
2778err_setup_tx:
2779 ixgbevf_free_all_tx_resources(adapter);
2780 ixgbevf_reset(adapter);
2781
2782err_setup_reset:
2783
2784 return err;
2785}
2786
2787/**
2788 * ixgbevf_close - Disables a network interface
2789 * @netdev: network interface device structure
2790 *
2791 * Returns 0, this is not allowed to fail
2792 *
2793 * The close entry point is called when an interface is de-activated
2794 * by the OS. The hardware is still under the drivers control, but
2795 * needs to be disabled. A global MAC reset is issued to stop the
2796 * hardware, and all transmit and receive resources are freed.
2797 **/
2798static int ixgbevf_close(struct net_device *netdev)
2799{
2800 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2801
2802 ixgbevf_down(adapter);
2803 ixgbevf_free_irq(adapter);
2804
2805 ixgbevf_free_all_tx_resources(adapter);
2806 ixgbevf_free_all_rx_resources(adapter);
2807
2808 return 0;
2809}
2810
Don Skidmore220fe052013-09-21 01:40:49 +00002811static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
2812{
2813 struct net_device *dev = adapter->netdev;
2814
2815 if (!(adapter->flags & IXGBEVF_FLAG_QUEUE_RESET_REQUESTED))
2816 return;
2817
2818 adapter->flags &= ~IXGBEVF_FLAG_QUEUE_RESET_REQUESTED;
2819
2820 /* if interface is down do nothing */
2821 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2822 test_bit(__IXGBEVF_RESETTING, &adapter->state))
2823 return;
2824
2825 /* Hardware has to reinitialize queues and interrupts to
2826 * match packet buffer alignment. Unfortunately, the
2827 * hardware is not flexible enough to do this dynamically.
2828 */
2829 if (netif_running(dev))
2830 ixgbevf_close(dev);
2831
2832 ixgbevf_clear_interrupt_scheme(adapter);
2833 ixgbevf_init_interrupt_scheme(adapter);
2834
2835 if (netif_running(dev))
2836 ixgbevf_open(dev);
2837}
2838
Alexander Duyck70a10e22012-05-11 08:33:21 +00002839static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
2840 u32 vlan_macip_lens, u32 type_tucmd,
2841 u32 mss_l4len_idx)
2842{
2843 struct ixgbe_adv_tx_context_desc *context_desc;
2844 u16 i = tx_ring->next_to_use;
2845
2846 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
2847
2848 i++;
2849 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2850
2851 /* set bits to identify this as an advanced context descriptor */
2852 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
2853
2854 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
2855 context_desc->seqnum_seed = 0;
2856 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
2857 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
2858}
2859
2860static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002861 struct ixgbevf_tx_buffer *first,
2862 u8 *hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00002863{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002864 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002865 u32 vlan_macip_lens, type_tucmd;
Greg Rose92915f72010-01-09 02:24:10 +00002866 u32 mss_l4len_idx, l4len;
Francois Romieu8f12c032014-03-30 03:14:32 +00002867 int err;
Greg Rose92915f72010-01-09 02:24:10 +00002868
Emil Tantilov01a545c2014-02-27 20:32:45 -08002869 if (skb->ip_summed != CHECKSUM_PARTIAL)
2870 return 0;
2871
Alexander Duyck70a10e22012-05-11 08:33:21 +00002872 if (!skb_is_gso(skb))
2873 return 0;
Greg Rose92915f72010-01-09 02:24:10 +00002874
Francois Romieu8f12c032014-03-30 03:14:32 +00002875 err = skb_cow_head(skb, 0);
2876 if (err < 0)
2877 return err;
Greg Rose92915f72010-01-09 02:24:10 +00002878
Alexander Duyck70a10e22012-05-11 08:33:21 +00002879 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
2880 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
2881
2882 if (skb->protocol == htons(ETH_P_IP)) {
2883 struct iphdr *iph = ip_hdr(skb);
2884 iph->tot_len = 0;
2885 iph->check = 0;
2886 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2887 iph->daddr, 0,
2888 IPPROTO_TCP,
2889 0);
2890 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002891 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
2892 IXGBE_TX_FLAGS_CSUM |
2893 IXGBE_TX_FLAGS_IPV4;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002894 } else if (skb_is_gso_v6(skb)) {
2895 ipv6_hdr(skb)->payload_len = 0;
2896 tcp_hdr(skb)->check =
2897 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2898 &ipv6_hdr(skb)->daddr,
2899 0, IPPROTO_TCP, 0);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002900 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
2901 IXGBE_TX_FLAGS_CSUM;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002902 }
2903
2904 /* compute header lengths */
2905 l4len = tcp_hdrlen(skb);
2906 *hdr_len += l4len;
2907 *hdr_len = skb_transport_offset(skb) + l4len;
2908
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002909 /* update gso size and bytecount with header size */
2910 first->gso_segs = skb_shinfo(skb)->gso_segs;
2911 first->bytecount += (first->gso_segs - 1) * *hdr_len;
2912
Alexander Duyck70a10e22012-05-11 08:33:21 +00002913 /* mss_l4len_id: use 1 as index for TSO */
2914 mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
2915 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
2916 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
2917
2918 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
2919 vlan_macip_lens = skb_network_header_len(skb);
2920 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002921 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002922
2923 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2924 type_tucmd, mss_l4len_idx);
2925
2926 return 1;
Greg Rose92915f72010-01-09 02:24:10 +00002927}
2928
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002929static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
2930 struct ixgbevf_tx_buffer *first)
Greg Rose92915f72010-01-09 02:24:10 +00002931{
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002932 struct sk_buff *skb = first->skb;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002933 u32 vlan_macip_lens = 0;
2934 u32 mss_l4len_idx = 0;
2935 u32 type_tucmd = 0;
Greg Rose92915f72010-01-09 02:24:10 +00002936
Alexander Duyck70a10e22012-05-11 08:33:21 +00002937 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2938 u8 l4_hdr = 0;
2939 switch (skb->protocol) {
Joe Perches0933ce42014-03-13 05:19:30 +00002940 case htons(ETH_P_IP):
Alexander Duyck70a10e22012-05-11 08:33:21 +00002941 vlan_macip_lens |= skb_network_header_len(skb);
2942 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
2943 l4_hdr = ip_hdr(skb)->protocol;
2944 break;
Joe Perches0933ce42014-03-13 05:19:30 +00002945 case htons(ETH_P_IPV6):
Alexander Duyck70a10e22012-05-11 08:33:21 +00002946 vlan_macip_lens |= skb_network_header_len(skb);
2947 l4_hdr = ipv6_hdr(skb)->nexthdr;
2948 break;
2949 default:
2950 if (unlikely(net_ratelimit())) {
2951 dev_warn(tx_ring->dev,
2952 "partial checksum but proto=%x!\n",
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002953 first->protocol);
Greg Rose92915f72010-01-09 02:24:10 +00002954 }
Alexander Duyck70a10e22012-05-11 08:33:21 +00002955 break;
Greg Rose92915f72010-01-09 02:24:10 +00002956 }
2957
Alexander Duyck70a10e22012-05-11 08:33:21 +00002958 switch (l4_hdr) {
2959 case IPPROTO_TCP:
2960 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
2961 mss_l4len_idx = tcp_hdrlen(skb) <<
2962 IXGBE_ADVTXD_L4LEN_SHIFT;
2963 break;
2964 case IPPROTO_SCTP:
2965 type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
2966 mss_l4len_idx = sizeof(struct sctphdr) <<
2967 IXGBE_ADVTXD_L4LEN_SHIFT;
2968 break;
2969 case IPPROTO_UDP:
2970 mss_l4len_idx = sizeof(struct udphdr) <<
2971 IXGBE_ADVTXD_L4LEN_SHIFT;
2972 break;
2973 default:
2974 if (unlikely(net_ratelimit())) {
2975 dev_warn(tx_ring->dev,
2976 "partial checksum but l4 proto=%x!\n",
2977 l4_hdr);
2978 }
2979 break;
2980 }
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002981
2982 /* update TX checksum flag */
2983 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00002984 }
2985
Alexander Duyck70a10e22012-05-11 08:33:21 +00002986 /* vlan_macip_lens: MACLEN, VLAN tag */
2987 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08002988 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Alexander Duyck70a10e22012-05-11 08:33:21 +00002989
2990 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
2991 type_tucmd, mss_l4len_idx);
Greg Rose92915f72010-01-09 02:24:10 +00002992}
2993
Emil Tantilov29d37fa2014-01-17 18:30:05 -08002994static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
2995{
2996 /* set type for advanced descriptor with frame checksum insertion */
2997 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
2998 IXGBE_ADVTXD_DCMD_IFCS |
2999 IXGBE_ADVTXD_DCMD_DEXT);
3000
3001 /* set HW vlan bit if vlan is present */
3002 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3003 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3004
3005 /* set segmentation enable bits for TSO/FSO */
3006 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3007 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3008
3009 return cmd_type;
3010}
3011
3012static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3013 u32 tx_flags, unsigned int paylen)
3014{
3015 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3016
3017 /* enable L4 checksum for TSO and TX checksum offload */
3018 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3019 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3020
3021 /* enble IPv4 checksum for TSO */
3022 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3023 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3024
3025 /* use index 1 context for TSO/FSO/FCOE */
3026 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3027 olinfo_status |= cpu_to_le32(1 << IXGBE_ADVTXD_IDX_SHIFT);
3028
3029 /* Check Context must be set if Tx switch is enabled, which it
3030 * always is for case where virtual functions are running
3031 */
3032 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3033
3034 tx_desc->read.olinfo_status = olinfo_status;
3035}
3036
3037static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3038 struct ixgbevf_tx_buffer *first,
3039 const u8 hdr_len)
Greg Rose92915f72010-01-09 02:24:10 +00003040{
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003041 dma_addr_t dma;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003042 struct sk_buff *skb = first->skb;
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003043 struct ixgbevf_tx_buffer *tx_buffer;
3044 union ixgbe_adv_tx_desc *tx_desc;
3045 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
3046 unsigned int data_len = skb->data_len;
3047 unsigned int size = skb_headlen(skb);
3048 unsigned int paylen = skb->len - hdr_len;
3049 u32 tx_flags = first->tx_flags;
3050 __le32 cmd_type;
3051 u16 i = tx_ring->next_to_use;
Greg Rose92915f72010-01-09 02:24:10 +00003052
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003053 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
Greg Rose92915f72010-01-09 02:24:10 +00003054
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003055 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, paylen);
3056 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
Greg Rose92915f72010-01-09 02:24:10 +00003057
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003058 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3059 if (dma_mapping_error(tx_ring->dev, dma))
3060 goto dma_error;
3061
3062 /* record length, and DMA address */
3063 dma_unmap_len_set(first, len, size);
3064 dma_unmap_addr_set(first, dma, dma);
3065
3066 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3067
3068 for (;;) {
3069 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3070 tx_desc->read.cmd_type_len =
3071 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3072
3073 i++;
3074 tx_desc++;
3075 if (i == tx_ring->count) {
3076 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3077 i = 0;
3078 }
3079
3080 dma += IXGBE_MAX_DATA_PER_TXD;
3081 size -= IXGBE_MAX_DATA_PER_TXD;
3082
3083 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3084 tx_desc->read.olinfo_status = 0;
3085 }
3086
3087 if (likely(!data_len))
3088 break;
3089
3090 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3091
3092 i++;
3093 tx_desc++;
3094 if (i == tx_ring->count) {
3095 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3096 i = 0;
3097 }
3098
3099 size = skb_frag_size(frag);
3100 data_len -= size;
3101
3102 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3103 DMA_TO_DEVICE);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003104 if (dma_mapping_error(tx_ring->dev, dma))
Greg Rose92915f72010-01-09 02:24:10 +00003105 goto dma_error;
Greg Rose92915f72010-01-09 02:24:10 +00003106
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003107 tx_buffer = &tx_ring->tx_buffer_info[i];
3108 dma_unmap_len_set(tx_buffer, len, size);
3109 dma_unmap_addr_set(tx_buffer, dma, dma);
Emil Tantilov9bdfefd2014-01-17 18:30:04 -08003110
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003111 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3112 tx_desc->read.olinfo_status = 0;
3113
3114 frag++;
Greg Rose92915f72010-01-09 02:24:10 +00003115 }
3116
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003117 /* write last descriptor with RS and EOP bits */
3118 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3119 tx_desc->read.cmd_type_len = cmd_type;
Greg Rose92915f72010-01-09 02:24:10 +00003120
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003121 /* set the timestamp */
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003122 first->time_stamp = jiffies;
Greg Rose92915f72010-01-09 02:24:10 +00003123
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003124 /* Force memory writes to complete before letting h/w know there
3125 * are new descriptors to fetch. (Only applicable for weak-ordered
3126 * memory model archs, such as IA-64).
3127 *
3128 * We also need this memory barrier (wmb) to make certain all of the
3129 * status bits have been updated before next_to_watch is written.
3130 */
3131 wmb();
Greg Rose92915f72010-01-09 02:24:10 +00003132
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003133 /* set next_to_watch value indicating a packet is present */
3134 first->next_to_watch = tx_desc;
3135
3136 i++;
3137 if (i == tx_ring->count)
3138 i = 0;
3139
3140 tx_ring->next_to_use = i;
3141
3142 /* notify HW of packet */
Mark Rustad06380db2014-03-04 03:02:23 +00003143 ixgbevf_write_tail(tx_ring, i);
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003144
3145 return;
Greg Rose92915f72010-01-09 02:24:10 +00003146dma_error:
Alexander Duyck70a10e22012-05-11 08:33:21 +00003147 dev_err(tx_ring->dev, "TX DMA map failed\n");
Greg Rose92915f72010-01-09 02:24:10 +00003148
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003149 /* clear dma mappings for failed tx_buffer_info map */
3150 for (;;) {
3151 tx_buffer = &tx_ring->tx_buffer_info[i];
3152 ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer);
3153 if (tx_buffer == first)
3154 break;
3155 if (i == 0)
3156 i = tx_ring->count;
Greg Rose92915f72010-01-09 02:24:10 +00003157 i--;
Greg Rose92915f72010-01-09 02:24:10 +00003158 }
3159
Greg Rose92915f72010-01-09 02:24:10 +00003160 tx_ring->next_to_use = i;
Greg Rose92915f72010-01-09 02:24:10 +00003161}
3162
Alexander Duyckfb401952012-05-11 08:33:16 +00003163static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003164{
Alexander Duyckfb401952012-05-11 08:33:16 +00003165 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose92915f72010-01-09 02:24:10 +00003166 /* Herbert's original patch had:
3167 * smp_mb__after_netif_stop_queue();
3168 * but since that doesn't exist yet, just open code it. */
3169 smp_mb();
3170
3171 /* We need to check again in a case another CPU has just
3172 * made room available. */
Don Skidmoref880d072013-10-23 02:17:52 +00003173 if (likely(ixgbevf_desc_unused(tx_ring) < size))
Greg Rose92915f72010-01-09 02:24:10 +00003174 return -EBUSY;
3175
3176 /* A reprieve! - use start_queue because it doesn't call schedule */
Alexander Duyckfb401952012-05-11 08:33:16 +00003177 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
Emil Tantilov095e2612014-01-17 18:30:00 -08003178 ++tx_ring->tx_stats.restart_queue;
3179
Greg Rose92915f72010-01-09 02:24:10 +00003180 return 0;
3181}
3182
Alexander Duyckfb401952012-05-11 08:33:16 +00003183static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
Greg Rose92915f72010-01-09 02:24:10 +00003184{
Don Skidmoref880d072013-10-23 02:17:52 +00003185 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
Greg Rose92915f72010-01-09 02:24:10 +00003186 return 0;
Alexander Duyckfb401952012-05-11 08:33:16 +00003187 return __ixgbevf_maybe_stop_tx(tx_ring, size);
Greg Rose92915f72010-01-09 02:24:10 +00003188}
3189
3190static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3191{
3192 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003193 struct ixgbevf_tx_buffer *first;
Greg Rose92915f72010-01-09 02:24:10 +00003194 struct ixgbevf_ring *tx_ring;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003195 int tso;
3196 u32 tx_flags = 0;
Alexander Duyck35959902012-05-11 08:32:40 +00003197 u16 count = TXD_USE_COUNT(skb_headlen(skb));
3198#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3199 unsigned short f;
3200#endif
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003201 u8 hdr_len = 0;
Greg Rosef9d08f162012-10-02 00:50:52 +00003202 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003203
Ben Hutchings46acc462012-11-01 09:11:11 +00003204 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
Greg Rosef9d08f162012-10-02 00:50:52 +00003205 dev_kfree_skb(skb);
3206 return NETDEV_TX_OK;
3207 }
Greg Rose92915f72010-01-09 02:24:10 +00003208
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003209 tx_ring = adapter->tx_ring[skb->queue_mapping];
Greg Rose92915f72010-01-09 02:24:10 +00003210
Alexander Duyck35959902012-05-11 08:32:40 +00003211 /*
3212 * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3213 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3214 * + 2 desc gap to keep tail from touching head,
3215 * + 1 desc for context descriptor,
3216 * otherwise try next time
3217 */
3218#if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3219 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3220 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3221#else
3222 count += skb_shinfo(skb)->nr_frags;
3223#endif
Alexander Duyckfb401952012-05-11 08:33:16 +00003224 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
Emil Tantilov095e2612014-01-17 18:30:00 -08003225 tx_ring->tx_stats.tx_busy++;
Alexander Duyck35959902012-05-11 08:32:40 +00003226 return NETDEV_TX_BUSY;
3227 }
3228
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003229 /* record the location of the first descriptor for this packet */
3230 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
3231 first->skb = skb;
3232 first->bytecount = skb->len;
3233 first->gso_segs = 1;
3234
Jesse Grosseab6d182010-10-20 13:56:03 +00003235 if (vlan_tx_tag_present(skb)) {
Greg Rose92915f72010-01-09 02:24:10 +00003236 tx_flags |= vlan_tx_tag_get(skb);
3237 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3238 tx_flags |= IXGBE_TX_FLAGS_VLAN;
3239 }
3240
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003241 /* record initial flags and protocol */
3242 first->tx_flags = tx_flags;
3243 first->protocol = vlan_get_protocol(skb);
Greg Rose92915f72010-01-09 02:24:10 +00003244
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003245 tso = ixgbevf_tso(tx_ring, first, &hdr_len);
3246 if (tso < 0)
3247 goto out_drop;
Emil Tantilovb5d217f2014-02-27 20:32:44 -08003248 else if (!tso)
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003249 ixgbevf_tx_csum(tx_ring, first);
Greg Rose92915f72010-01-09 02:24:10 +00003250
Emil Tantilov29d37fa2014-01-17 18:30:05 -08003251 ixgbevf_tx_map(tx_ring, first, hdr_len);
Greg Rose92915f72010-01-09 02:24:10 +00003252
Alexander Duyckfb401952012-05-11 08:33:16 +00003253 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Greg Rose92915f72010-01-09 02:24:10 +00003254
3255 return NETDEV_TX_OK;
Emil Tantilov7ad1a092014-01-17 18:30:03 -08003256
3257out_drop:
3258 dev_kfree_skb_any(first->skb);
3259 first->skb = NULL;
3260
3261 return NETDEV_TX_OK;
Greg Rose92915f72010-01-09 02:24:10 +00003262}
3263
3264/**
Greg Rose92915f72010-01-09 02:24:10 +00003265 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3266 * @netdev: network interface device structure
3267 * @p: pointer to an address structure
3268 *
3269 * Returns 0 on success, negative on failure
3270 **/
3271static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3272{
3273 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3274 struct ixgbe_hw *hw = &adapter->hw;
3275 struct sockaddr *addr = p;
3276
3277 if (!is_valid_ether_addr(addr->sa_data))
3278 return -EADDRNOTAVAIL;
3279
3280 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3281 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3282
John Fastabend55fdd45b2012-10-01 14:52:20 +00003283 spin_lock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003284
Greg Rose92fe0bf2012-11-02 05:50:47 +00003285 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
Greg Rose92915f72010-01-09 02:24:10 +00003286
John Fastabend55fdd45b2012-10-01 14:52:20 +00003287 spin_unlock_bh(&adapter->mbx_lock);
Alexander Duyck1c55ed72012-05-11 08:33:06 +00003288
Greg Rose92915f72010-01-09 02:24:10 +00003289 return 0;
3290}
3291
3292/**
3293 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3294 * @netdev: network interface device structure
3295 * @new_mtu: new value for maximum frame size
3296 *
3297 * Returns 0 on success, negative on failure
3298 **/
3299static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3300{
3301 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3302 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Greg Rose69bfbec2011-01-26 01:06:12 +00003303 int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
Greg Rose69bfbec2011-01-26 01:06:12 +00003304
Alexander Duyck56e94092012-07-20 08:10:03 +00003305 switch (adapter->hw.api_version) {
3306 case ixgbe_mbox_api_11:
Greg Rose69bfbec2011-01-26 01:06:12 +00003307 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
Alexander Duyck56e94092012-07-20 08:10:03 +00003308 break;
3309 default:
3310 if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
3311 max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
3312 break;
3313 }
Greg Rose92915f72010-01-09 02:24:10 +00003314
3315 /* MTU < 68 is an error and causes problems on some kernels */
Greg Rose69bfbec2011-01-26 01:06:12 +00003316 if ((new_mtu < 68) || (max_frame > max_possible_frame))
Greg Rose92915f72010-01-09 02:24:10 +00003317 return -EINVAL;
3318
3319 hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
3320 netdev->mtu, new_mtu);
3321 /* must set new MTU before calling down or up */
3322 netdev->mtu = new_mtu;
3323
3324 if (netif_running(netdev))
3325 ixgbevf_reinit_locked(adapter);
3326
3327 return 0;
3328}
3329
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003330static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
Greg Rose92915f72010-01-09 02:24:10 +00003331{
3332 struct net_device *netdev = pci_get_drvdata(pdev);
3333 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003334#ifdef CONFIG_PM
3335 int retval = 0;
3336#endif
Greg Rose92915f72010-01-09 02:24:10 +00003337
3338 netif_device_detach(netdev);
3339
3340 if (netif_running(netdev)) {
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003341 rtnl_lock();
Greg Rose92915f72010-01-09 02:24:10 +00003342 ixgbevf_down(adapter);
3343 ixgbevf_free_irq(adapter);
3344 ixgbevf_free_all_tx_resources(adapter);
3345 ixgbevf_free_all_rx_resources(adapter);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003346 rtnl_unlock();
Greg Rose92915f72010-01-09 02:24:10 +00003347 }
3348
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003349 ixgbevf_clear_interrupt_scheme(adapter);
3350
3351#ifdef CONFIG_PM
3352 retval = pci_save_state(pdev);
3353 if (retval)
3354 return retval;
3355
3356#endif
Mark Rustadbc0c7152014-03-12 00:38:45 +00003357 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3358 pci_disable_device(pdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003359
3360 return 0;
3361}
3362
3363#ifdef CONFIG_PM
3364static int ixgbevf_resume(struct pci_dev *pdev)
3365{
Wei Yongjun27ae2962014-01-16 02:30:07 -08003366 struct net_device *netdev = pci_get_drvdata(pdev);
3367 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003368 u32 err;
3369
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003370 pci_restore_state(pdev);
3371 /*
3372 * pci_restore_state clears dev->state_saved so call
3373 * pci_save_state to restore it.
3374 */
Greg Rose92915f72010-01-09 02:24:10 +00003375 pci_save_state(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003376
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003377 err = pci_enable_device_mem(pdev);
3378 if (err) {
3379 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3380 return err;
3381 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003382 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003383 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003384 pci_set_master(pdev);
3385
Don Skidmore798e3812013-10-01 04:33:51 -07003386 ixgbevf_reset(adapter);
3387
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003388 rtnl_lock();
3389 err = ixgbevf_init_interrupt_scheme(adapter);
3390 rtnl_unlock();
3391 if (err) {
3392 dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3393 return err;
3394 }
3395
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003396 if (netif_running(netdev)) {
3397 err = ixgbevf_open(netdev);
3398 if (err)
3399 return err;
3400 }
3401
3402 netif_device_attach(netdev);
3403
3404 return err;
3405}
3406
3407#endif /* CONFIG_PM */
3408static void ixgbevf_shutdown(struct pci_dev *pdev)
3409{
3410 ixgbevf_suspend(pdev, PMSG_SUSPEND);
Greg Rose92915f72010-01-09 02:24:10 +00003411}
3412
Eric Dumazet4197aa72011-06-22 05:01:35 +00003413static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
3414 struct rtnl_link_stats64 *stats)
3415{
3416 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3417 unsigned int start;
3418 u64 bytes, packets;
3419 const struct ixgbevf_ring *ring;
3420 int i;
3421
3422 ixgbevf_update_stats(adapter);
3423
3424 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3425
3426 for (i = 0; i < adapter->num_rx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003427 ring = adapter->rx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003428 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003429 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003430 bytes = ring->stats.bytes;
3431 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003432 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003433 stats->rx_bytes += bytes;
3434 stats->rx_packets += packets;
3435 }
3436
3437 for (i = 0; i < adapter->num_tx_queues; i++) {
Don Skidmore87e70ab2014-01-16 02:30:08 -08003438 ring = adapter->tx_ring[i];
Eric Dumazet4197aa72011-06-22 05:01:35 +00003439 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07003440 start = u64_stats_fetch_begin_irq(&ring->syncp);
Emil Tantilov095e2612014-01-17 18:30:00 -08003441 bytes = ring->stats.bytes;
3442 packets = ring->stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07003443 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
Eric Dumazet4197aa72011-06-22 05:01:35 +00003444 stats->tx_bytes += bytes;
3445 stats->tx_packets += packets;
3446 }
3447
3448 return stats;
3449}
3450
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003451static const struct net_device_ops ixgbevf_netdev_ops = {
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003452 .ndo_open = ixgbevf_open,
3453 .ndo_stop = ixgbevf_close,
3454 .ndo_start_xmit = ixgbevf_xmit_frame,
3455 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
Eric Dumazet4197aa72011-06-22 05:01:35 +00003456 .ndo_get_stats64 = ixgbevf_get_stats,
Greg Rose92915f72010-01-09 02:24:10 +00003457 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003458 .ndo_set_mac_address = ixgbevf_set_mac,
3459 .ndo_change_mtu = ixgbevf_change_mtu,
3460 .ndo_tx_timeout = ixgbevf_tx_timeout,
Stephen Hemmingerc12db762011-06-09 02:58:39 +00003461 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
3462 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
Jacob Kellerc777cdf2013-09-21 06:24:20 +00003463#ifdef CONFIG_NET_RX_BUSY_POLL
3464 .ndo_busy_poll = ixgbevf_busy_poll_recv,
3465#endif
Greg Rose92915f72010-01-09 02:24:10 +00003466};
Greg Rose92915f72010-01-09 02:24:10 +00003467
3468static void ixgbevf_assign_netdev_ops(struct net_device *dev)
3469{
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003470 dev->netdev_ops = &ixgbevf_netdev_ops;
Greg Rose92915f72010-01-09 02:24:10 +00003471 ixgbevf_set_ethtool_ops(dev);
3472 dev->watchdog_timeo = 5 * HZ;
3473}
3474
3475/**
3476 * ixgbevf_probe - Device Initialization Routine
3477 * @pdev: PCI device information struct
3478 * @ent: entry in ixgbevf_pci_tbl
3479 *
3480 * Returns 0 on success, negative on failure
3481 *
3482 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
3483 * The OS initialization, configuring of the adapter private structure,
3484 * and a hardware reset occur.
3485 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003486static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Greg Rose92915f72010-01-09 02:24:10 +00003487{
3488 struct net_device *netdev;
3489 struct ixgbevf_adapter *adapter = NULL;
3490 struct ixgbe_hw *hw = NULL;
3491 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
Greg Rose92915f72010-01-09 02:24:10 +00003492 int err, pci_using_dac;
3493
3494 err = pci_enable_device(pdev);
3495 if (err)
3496 return err;
3497
Russell King53567aa2013-06-10 12:49:38 +01003498 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Greg Rose92915f72010-01-09 02:24:10 +00003499 pci_using_dac = 1;
3500 } else {
Russell King53567aa2013-06-10 12:49:38 +01003501 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Greg Rose92915f72010-01-09 02:24:10 +00003502 if (err) {
Russell King53567aa2013-06-10 12:49:38 +01003503 dev_err(&pdev->dev, "No usable DMA "
3504 "configuration, aborting\n");
3505 goto err_dma;
Greg Rose92915f72010-01-09 02:24:10 +00003506 }
3507 pci_using_dac = 0;
3508 }
3509
3510 err = pci_request_regions(pdev, ixgbevf_driver_name);
3511 if (err) {
3512 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
3513 goto err_pci_reg;
3514 }
3515
3516 pci_set_master(pdev);
3517
Greg Rose92915f72010-01-09 02:24:10 +00003518 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
3519 MAX_TX_QUEUES);
Greg Rose92915f72010-01-09 02:24:10 +00003520 if (!netdev) {
3521 err = -ENOMEM;
3522 goto err_alloc_etherdev;
3523 }
3524
3525 SET_NETDEV_DEV(netdev, &pdev->dev);
3526
3527 pci_set_drvdata(pdev, netdev);
3528 adapter = netdev_priv(netdev);
3529
3530 adapter->netdev = netdev;
3531 adapter->pdev = pdev;
3532 hw = &adapter->hw;
3533 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00003534 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Greg Rose92915f72010-01-09 02:24:10 +00003535
3536 /*
3537 * call save state here in standalone driver because it relies on
3538 * adapter struct to exist, and needs to call netdev_priv
3539 */
3540 pci_save_state(pdev);
3541
3542 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3543 pci_resource_len(pdev, 0));
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003544 adapter->io_addr = hw->hw_addr;
Greg Rose92915f72010-01-09 02:24:10 +00003545 if (!hw->hw_addr) {
3546 err = -EIO;
3547 goto err_ioremap;
3548 }
3549
3550 ixgbevf_assign_netdev_ops(netdev);
3551
Greg Rose92915f72010-01-09 02:24:10 +00003552 /* Setup hw api */
3553 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
3554 hw->mac.type = ii->mac;
3555
3556 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
Greg Rosef416dfc2011-06-08 07:32:38 +00003557 sizeof(struct ixgbe_mbx_operations));
Greg Rose92915f72010-01-09 02:24:10 +00003558
Greg Rose92915f72010-01-09 02:24:10 +00003559 /* setup the private structure */
3560 err = ixgbevf_sw_init(adapter);
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003561 if (err)
3562 goto err_sw_init;
3563
3564 /* The HW MAC address was set and/or determined in sw_init */
Danny Kukawka1a0d6ae2012-02-09 09:48:54 +00003565 if (!is_valid_ether_addr(netdev->dev_addr)) {
3566 pr_err("invalid MAC address\n");
3567 err = -EIO;
3568 goto err_sw_init;
3569 }
Greg Rose92915f72010-01-09 02:24:10 +00003570
Michał Mirosław471a76d2011-06-08 08:53:03 +00003571 netdev->hw_features = NETIF_F_SG |
Greg Rose92915f72010-01-09 02:24:10 +00003572 NETIF_F_IP_CSUM |
Michał Mirosław471a76d2011-06-08 08:53:03 +00003573 NETIF_F_IPV6_CSUM |
3574 NETIF_F_TSO |
3575 NETIF_F_TSO6 |
3576 NETIF_F_RXCSUM;
3577
3578 netdev->features = netdev->hw_features |
Patrick McHardyf6469682013-04-19 02:04:27 +00003579 NETIF_F_HW_VLAN_CTAG_TX |
3580 NETIF_F_HW_VLAN_CTAG_RX |
3581 NETIF_F_HW_VLAN_CTAG_FILTER;
Greg Rose92915f72010-01-09 02:24:10 +00003582
Greg Rose92915f72010-01-09 02:24:10 +00003583 netdev->vlan_features |= NETIF_F_TSO;
3584 netdev->vlan_features |= NETIF_F_TSO6;
3585 netdev->vlan_features |= NETIF_F_IP_CSUM;
Alexander Duyck3bfacf92010-08-02 14:59:04 +00003586 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
Greg Rose92915f72010-01-09 02:24:10 +00003587 netdev->vlan_features |= NETIF_F_SG;
3588
3589 if (pci_using_dac)
3590 netdev->features |= NETIF_F_HIGHDMA;
3591
Jiri Pirko01789342011-08-16 06:29:00 +00003592 netdev->priv_flags |= IFF_UNICAST_FLT;
3593
Greg Rose92915f72010-01-09 02:24:10 +00003594 init_timer(&adapter->watchdog_timer);
Joe Perchesc061b182010-08-23 18:20:03 +00003595 adapter->watchdog_timer.function = ixgbevf_watchdog;
Greg Rose92915f72010-01-09 02:24:10 +00003596 adapter->watchdog_timer.data = (unsigned long)adapter;
3597
Mark Rustadea699562014-03-12 00:38:51 +00003598 if (IXGBE_REMOVED(hw->hw_addr)) {
3599 err = -EIO;
3600 goto err_sw_init;
3601 }
Greg Rose92915f72010-01-09 02:24:10 +00003602 INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
3603 INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
Mark Rustadea699562014-03-12 00:38:51 +00003604 set_bit(__IXGBEVF_WORK_INIT, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003605
3606 err = ixgbevf_init_interrupt_scheme(adapter);
3607 if (err)
3608 goto err_sw_init;
3609
Greg Rose92915f72010-01-09 02:24:10 +00003610 strcpy(netdev->name, "eth%d");
3611
3612 err = register_netdev(netdev);
3613 if (err)
3614 goto err_register;
3615
Greg Rose5d426ad2010-11-16 19:27:19 -08003616 netif_carrier_off(netdev);
3617
Greg Rose33bd9f62010-03-19 02:59:52 +00003618 ixgbevf_init_last_counter_stats(adapter);
3619
Greg Rose92915f72010-01-09 02:24:10 +00003620 /* print the MAC address */
Danny Kukawkaf794e7e2012-02-24 03:45:56 +00003621 hw_dbg(hw, "%pM\n", netdev->dev_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003622
3623 hw_dbg(hw, "MAC: %d\n", hw->mac.type);
3624
Greg Rose92915f72010-01-09 02:24:10 +00003625 hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
Greg Rose92915f72010-01-09 02:24:10 +00003626 return 0;
3627
3628err_register:
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003629 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003630err_sw_init:
3631 ixgbevf_reset_interrupt_capability(adapter);
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003632 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003633err_ioremap:
3634 free_netdev(netdev);
3635err_alloc_etherdev:
3636 pci_release_regions(pdev);
3637err_pci_reg:
3638err_dma:
Mark Rustadbc0c7152014-03-12 00:38:45 +00003639 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3640 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003641 return err;
3642}
3643
3644/**
3645 * ixgbevf_remove - Device Removal Routine
3646 * @pdev: PCI device information struct
3647 *
3648 * ixgbevf_remove is called by the PCI subsystem to alert the driver
3649 * that it should release a PCI device. The could be caused by a
3650 * Hot-Plug event, or because the driver is going to be removed from
3651 * memory.
3652 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003653static void ixgbevf_remove(struct pci_dev *pdev)
Greg Rose92915f72010-01-09 02:24:10 +00003654{
3655 struct net_device *netdev = pci_get_drvdata(pdev);
3656 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3657
Mark Rustad2e7cfbd2014-03-04 03:02:13 +00003658 set_bit(__IXGBEVF_REMOVING, &adapter->state);
Greg Rose92915f72010-01-09 02:24:10 +00003659
3660 del_timer_sync(&adapter->watchdog_timer);
3661
Tejun Heo23f333a2010-12-12 16:45:14 +01003662 cancel_work_sync(&adapter->reset_task);
Greg Rose92915f72010-01-09 02:24:10 +00003663 cancel_work_sync(&adapter->watchdog_task);
3664
Alexander Duyckfd13a9a2012-05-11 08:32:24 +00003665 if (netdev->reg_state == NETREG_REGISTERED)
Greg Rose92915f72010-01-09 02:24:10 +00003666 unregister_netdev(netdev);
Greg Rose92915f72010-01-09 02:24:10 +00003667
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003668 ixgbevf_clear_interrupt_scheme(adapter);
Greg Rose92915f72010-01-09 02:24:10 +00003669 ixgbevf_reset_interrupt_capability(adapter);
3670
Mark Rustaddbf8b0d2014-03-04 03:02:34 +00003671 iounmap(adapter->io_addr);
Greg Rose92915f72010-01-09 02:24:10 +00003672 pci_release_regions(pdev);
3673
3674 hw_dbg(&adapter->hw, "Remove complete\n");
3675
Greg Rose92915f72010-01-09 02:24:10 +00003676 free_netdev(netdev);
3677
Mark Rustadbc0c7152014-03-12 00:38:45 +00003678 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3679 pci_disable_device(pdev);
Greg Rose92915f72010-01-09 02:24:10 +00003680}
3681
Alexander Duyck9f19f312012-05-11 08:33:32 +00003682/**
3683 * ixgbevf_io_error_detected - called when PCI error is detected
3684 * @pdev: Pointer to PCI device
3685 * @state: The current pci connection state
3686 *
3687 * This function is called after a PCI bus error affecting
3688 * this device has been detected.
3689 */
3690static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
3691 pci_channel_state_t state)
3692{
3693 struct net_device *netdev = pci_get_drvdata(pdev);
3694 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3695
Mark Rustadea699562014-03-12 00:38:51 +00003696 if (!test_bit(__IXGBEVF_WORK_INIT, &adapter->state))
3697 return PCI_ERS_RESULT_DISCONNECT;
3698
Mark Rustadbc0c7152014-03-12 00:38:45 +00003699 rtnl_lock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003700 netif_device_detach(netdev);
3701
Mark Rustadbc0c7152014-03-12 00:38:45 +00003702 if (state == pci_channel_io_perm_failure) {
3703 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003704 return PCI_ERS_RESULT_DISCONNECT;
Mark Rustadbc0c7152014-03-12 00:38:45 +00003705 }
Alexander Duyck9f19f312012-05-11 08:33:32 +00003706
3707 if (netif_running(netdev))
3708 ixgbevf_down(adapter);
3709
Mark Rustadbc0c7152014-03-12 00:38:45 +00003710 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3711 pci_disable_device(pdev);
3712 rtnl_unlock();
Alexander Duyck9f19f312012-05-11 08:33:32 +00003713
3714 /* Request a slot slot reset. */
3715 return PCI_ERS_RESULT_NEED_RESET;
3716}
3717
3718/**
3719 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
3720 * @pdev: Pointer to PCI device
3721 *
3722 * Restart the card from scratch, as if from a cold-boot. Implementation
3723 * resembles the first-half of the ixgbevf_resume routine.
3724 */
3725static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
3726{
3727 struct net_device *netdev = pci_get_drvdata(pdev);
3728 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3729
3730 if (pci_enable_device_mem(pdev)) {
3731 dev_err(&pdev->dev,
3732 "Cannot re-enable PCI device after reset.\n");
3733 return PCI_ERS_RESULT_DISCONNECT;
3734 }
3735
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003736 smp_mb__before_atomic();
Mark Rustadbc0c7152014-03-12 00:38:45 +00003737 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
Alexander Duyck9f19f312012-05-11 08:33:32 +00003738 pci_set_master(pdev);
3739
3740 ixgbevf_reset(adapter);
3741
3742 return PCI_ERS_RESULT_RECOVERED;
3743}
3744
3745/**
3746 * ixgbevf_io_resume - called when traffic can start flowing again.
3747 * @pdev: Pointer to PCI device
3748 *
3749 * This callback is called when the error recovery driver tells us that
3750 * its OK to resume normal operation. Implementation resembles the
3751 * second-half of the ixgbevf_resume routine.
3752 */
3753static void ixgbevf_io_resume(struct pci_dev *pdev)
3754{
3755 struct net_device *netdev = pci_get_drvdata(pdev);
3756 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3757
3758 if (netif_running(netdev))
3759 ixgbevf_up(adapter);
3760
3761 netif_device_attach(netdev);
3762}
3763
3764/* PCI Error Recovery (ERS) */
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003765static const struct pci_error_handlers ixgbevf_err_handler = {
Alexander Duyck9f19f312012-05-11 08:33:32 +00003766 .error_detected = ixgbevf_io_error_detected,
3767 .slot_reset = ixgbevf_io_slot_reset,
3768 .resume = ixgbevf_io_resume,
3769};
3770
Greg Rose92915f72010-01-09 02:24:10 +00003771static struct pci_driver ixgbevf_driver = {
3772 .name = ixgbevf_driver_name,
3773 .id_table = ixgbevf_pci_tbl,
3774 .probe = ixgbevf_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003775 .remove = ixgbevf_remove,
Alexander Duyck0ac1e8c2012-05-11 08:33:26 +00003776#ifdef CONFIG_PM
3777 /* Power Management Hooks */
3778 .suspend = ixgbevf_suspend,
3779 .resume = ixgbevf_resume,
3780#endif
Greg Rose92915f72010-01-09 02:24:10 +00003781 .shutdown = ixgbevf_shutdown,
Alexander Duyck9f19f312012-05-11 08:33:32 +00003782 .err_handler = &ixgbevf_err_handler
Greg Rose92915f72010-01-09 02:24:10 +00003783};
3784
3785/**
Greg Rose65d676c2011-02-03 06:54:13 +00003786 * ixgbevf_init_module - Driver Registration Routine
Greg Rose92915f72010-01-09 02:24:10 +00003787 *
Greg Rose65d676c2011-02-03 06:54:13 +00003788 * ixgbevf_init_module is the first routine called when the driver is
Greg Rose92915f72010-01-09 02:24:10 +00003789 * loaded. All it does is register with the PCI subsystem.
3790 **/
3791static int __init ixgbevf_init_module(void)
3792{
3793 int ret;
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003794 pr_info("%s - version %s\n", ixgbevf_driver_string,
3795 ixgbevf_driver_version);
Greg Rose92915f72010-01-09 02:24:10 +00003796
Jeff Kirsherdbd96362011-10-21 19:38:18 +00003797 pr_info("%s\n", ixgbevf_copyright);
Greg Rose92915f72010-01-09 02:24:10 +00003798
3799 ret = pci_register_driver(&ixgbevf_driver);
3800 return ret;
3801}
3802
3803module_init(ixgbevf_init_module);
3804
3805/**
Greg Rose65d676c2011-02-03 06:54:13 +00003806 * ixgbevf_exit_module - Driver Exit Cleanup Routine
Greg Rose92915f72010-01-09 02:24:10 +00003807 *
Greg Rose65d676c2011-02-03 06:54:13 +00003808 * ixgbevf_exit_module is called just before the driver is removed
Greg Rose92915f72010-01-09 02:24:10 +00003809 * from memory.
3810 **/
3811static void __exit ixgbevf_exit_module(void)
3812{
3813 pci_unregister_driver(&ixgbevf_driver);
3814}
3815
3816#ifdef DEBUG
3817/**
Greg Rose65d676c2011-02-03 06:54:13 +00003818 * ixgbevf_get_hw_dev_name - return device name string
Greg Rose92915f72010-01-09 02:24:10 +00003819 * used by hardware layer to print debugging information
3820 **/
3821char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
3822{
3823 struct ixgbevf_adapter *adapter = hw->back;
3824 return adapter->netdev->name;
3825}
3826
3827#endif
3828module_exit(ixgbevf_exit_module);
3829
3830/* ixgbevf_main.c */