blob: c782942d766169ad62319c9473497cea191d4a0f [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanc0c050c2015-10-22 16:01:17 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040035#include <linux/rtc.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040036#include <net/ip.h>
37#include <net/tcp.h>
38#include <net/udp.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070041#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040042#ifdef CONFIG_NET_RX_BUSY_POLL
43#include <net/busy_poll.h>
44#endif
45#include <linux/workqueue.h>
46#include <linux/prefetch.h>
47#include <linux/cache.h>
48#include <linux/log2.h>
49#include <linux/aer.h>
50#include <linux/bitmap.h>
51#include <linux/cpu_rmap.h>
52
53#include "bnxt_hsi.h"
54#include "bnxt.h"
55#include "bnxt_sriov.h"
56#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050057#include "bnxt_dcb.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040058
59#define BNXT_TX_TIMEOUT (5 * HZ)
60
61static const char version[] =
62 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
63
64MODULE_LICENSE("GPL");
65MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
66MODULE_VERSION(DRV_MODULE_VERSION);
67
68#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
69#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
70#define BNXT_RX_COPY_THRESH 256
71
Michael Chan4419dbe2016-02-10 17:33:49 -050072#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040073
74enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050075 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040076 BCM57302,
77 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040078 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040079 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040080 BCM57311,
81 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050082 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040083 BCM57404,
84 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040085 BCM57402_NPAR,
86 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040087 BCM57412,
88 BCM57414,
89 BCM57416,
90 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040092 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040093 BCM57417_SFP,
94 BCM57416_SFP,
95 BCM57404_NPAR,
96 BCM57406_NPAR,
97 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040098 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040099 BCM57414_NPAR,
100 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -0400101 NETXTREME_E_VF,
102 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400103};
104
105/* indexed by enum above */
106static const struct {
107 char *name;
108} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400109 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
110 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
111 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400112 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400113 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
114 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
115 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
117 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
118 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400119 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400120 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
121 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
122 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
123 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
124 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400125 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400126 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
127 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
128 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400129 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
130 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400131 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
132 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400133 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
134 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400135 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
136 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400137};
138
139static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500141 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400142 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
143 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400144 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400145 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400146 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
147 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500148 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
150 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
152 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400153 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
154 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
155 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
156 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400158 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400159 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
160 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
161 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
162 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
165 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400168 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400170#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400171 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
175 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
176 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400177#endif
178 { 0 }
179};
180
181MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
182
183static const u16 bnxt_vf_req_snif[] = {
184 HWRM_FUNC_CFG,
185 HWRM_PORT_PHY_QCFG,
186 HWRM_CFA_L2_FILTER_ALLOC,
187};
188
Michael Chan25be8622016-04-05 14:09:00 -0400189static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500190 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
191 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
192 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
193 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
194 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400195};
196
Michael Chanc0c050c2015-10-22 16:01:17 -0400197static bool bnxt_vf_pciid(enum board_idx idx)
198{
Michael Chanadbc8302016-09-19 03:58:01 -0400199 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400200}
201
202#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
203#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
204#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
205
206#define BNXT_CP_DB_REARM(db, raw_cons) \
207 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
208
209#define BNXT_CP_DB(db, raw_cons) \
210 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
211
212#define BNXT_CP_DB_IRQ_DIS(db) \
213 writel(DB_CP_IRQ_DIS_FLAGS, db)
214
215static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
216{
217 /* Tell compiler to fetch tx indices from memory. */
218 barrier();
219
220 return bp->tx_ring_size -
221 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
222}
223
224static const u16 bnxt_lhint_arr[] = {
225 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
226 TX_BD_FLAGS_LHINT_512_TO_1023,
227 TX_BD_FLAGS_LHINT_1024_TO_2047,
228 TX_BD_FLAGS_LHINT_1024_TO_2047,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
244};
245
246static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
247{
248 struct bnxt *bp = netdev_priv(dev);
249 struct tx_bd *txbd;
250 struct tx_bd_ext *txbd1;
251 struct netdev_queue *txq;
252 int i;
253 dma_addr_t mapping;
254 unsigned int length, pad = 0;
255 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
256 u16 prod, last_frag;
257 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400258 struct bnxt_tx_ring_info *txr;
259 struct bnxt_sw_tx_bd *tx_buf;
260
261 i = skb_get_queue_mapping(skb);
262 if (unlikely(i >= bp->tx_nr_rings)) {
263 dev_kfree_skb_any(skb);
264 return NETDEV_TX_OK;
265 }
266
Michael Chanb6ab4b02016-01-02 23:44:59 -0500267 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400268 txq = netdev_get_tx_queue(dev, i);
269 prod = txr->tx_prod;
270
271 free_size = bnxt_tx_avail(bp, txr);
272 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
273 netif_tx_stop_queue(txq);
274 return NETDEV_TX_BUSY;
275 }
276
277 length = skb->len;
278 len = skb_headlen(skb);
279 last_frag = skb_shinfo(skb)->nr_frags;
280
281 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
282
283 txbd->tx_bd_opaque = prod;
284
285 tx_buf = &txr->tx_buf_ring[prod];
286 tx_buf->skb = skb;
287 tx_buf->nr_frags = last_frag;
288
289 vlan_tag_flags = 0;
290 cfa_action = 0;
291 if (skb_vlan_tag_present(skb)) {
292 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
293 skb_vlan_tag_get(skb);
294 /* Currently supports 8021Q, 8021AD vlan offloads
295 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
296 */
297 if (skb->vlan_proto == htons(ETH_P_8021Q))
298 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
299 }
300
301 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500302 struct tx_push_buffer *tx_push_buf = txr->tx_push;
303 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
304 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
305 void *pdata = tx_push_buf->data;
306 u64 *end;
307 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400308
309 /* Set COAL_NOW to be ready quickly for the next push */
310 tx_push->tx_bd_len_flags_type =
311 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
312 TX_BD_TYPE_LONG_TX_BD |
313 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
314 TX_BD_FLAGS_COAL_NOW |
315 TX_BD_FLAGS_PACKET_END |
316 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
317
318 if (skb->ip_summed == CHECKSUM_PARTIAL)
319 tx_push1->tx_bd_hsize_lflags =
320 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
321 else
322 tx_push1->tx_bd_hsize_lflags = 0;
323
324 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
325 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
326
Michael Chanfbb0fa82016-02-22 02:10:26 -0500327 end = pdata + length;
328 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500329 *end = 0;
330
Michael Chanc0c050c2015-10-22 16:01:17 -0400331 skb_copy_from_linear_data(skb, pdata, len);
332 pdata += len;
333 for (j = 0; j < last_frag; j++) {
334 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
335 void *fptr;
336
337 fptr = skb_frag_address_safe(frag);
338 if (!fptr)
339 goto normal_tx;
340
341 memcpy(pdata, fptr, skb_frag_size(frag));
342 pdata += skb_frag_size(frag);
343 }
344
Michael Chan4419dbe2016-02-10 17:33:49 -0500345 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
346 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400347 prod = NEXT_TX(prod);
348 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
349 memcpy(txbd, tx_push1, sizeof(*txbd));
350 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500351 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400352 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
353 txr->tx_prod = prod;
354
Michael Chanb9a84602016-06-06 02:37:14 -0400355 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400356 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400357 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400358
Michael Chan4419dbe2016-02-10 17:33:49 -0500359 push_len = (length + sizeof(*tx_push) + 7) / 8;
360 if (push_len > 16) {
361 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400362 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
363 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500364 } else {
365 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
366 push_len);
367 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400368
Michael Chanc0c050c2015-10-22 16:01:17 -0400369 goto tx_done;
370 }
371
372normal_tx:
373 if (length < BNXT_MIN_PKT_SIZE) {
374 pad = BNXT_MIN_PKT_SIZE - length;
375 if (skb_pad(skb, pad)) {
376 /* SKB already freed. */
377 tx_buf->skb = NULL;
378 return NETDEV_TX_OK;
379 }
380 length = BNXT_MIN_PKT_SIZE;
381 }
382
383 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
384
385 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
386 dev_kfree_skb_any(skb);
387 tx_buf->skb = NULL;
388 return NETDEV_TX_OK;
389 }
390
391 dma_unmap_addr_set(tx_buf, mapping, mapping);
392 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
393 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
394
395 txbd->tx_bd_haddr = cpu_to_le64(mapping);
396
397 prod = NEXT_TX(prod);
398 txbd1 = (struct tx_bd_ext *)
399 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
400
401 txbd1->tx_bd_hsize_lflags = 0;
402 if (skb_is_gso(skb)) {
403 u32 hdr_len;
404
405 if (skb->encapsulation)
406 hdr_len = skb_inner_network_offset(skb) +
407 skb_inner_network_header_len(skb) +
408 inner_tcp_hdrlen(skb);
409 else
410 hdr_len = skb_transport_offset(skb) +
411 tcp_hdrlen(skb);
412
413 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
414 TX_BD_FLAGS_T_IPID |
415 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
416 length = skb_shinfo(skb)->gso_size;
417 txbd1->tx_bd_mss = cpu_to_le32(length);
418 length += hdr_len;
419 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 txbd1->tx_bd_hsize_lflags =
421 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
422 txbd1->tx_bd_mss = 0;
423 }
424
425 length >>= 9;
426 flags |= bnxt_lhint_arr[length];
427 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
428
429 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
430 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
431 for (i = 0; i < last_frag; i++) {
432 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
433
434 prod = NEXT_TX(prod);
435 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
436
437 len = skb_frag_size(frag);
438 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
439 DMA_TO_DEVICE);
440
441 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
442 goto tx_dma_error;
443
444 tx_buf = &txr->tx_buf_ring[prod];
445 dma_unmap_addr_set(tx_buf, mapping, mapping);
446
447 txbd->tx_bd_haddr = cpu_to_le64(mapping);
448
449 flags = len << TX_BD_LEN_SHIFT;
450 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
451 }
452
453 flags &= ~TX_BD_LEN;
454 txbd->tx_bd_len_flags_type =
455 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
456 TX_BD_FLAGS_PACKET_END);
457
458 netdev_tx_sent_queue(txq, skb->len);
459
460 /* Sync BD data before updating doorbell */
461 wmb();
462
463 prod = NEXT_TX(prod);
464 txr->tx_prod = prod;
465
466 writel(DB_KEY_TX | prod, txr->tx_doorbell);
467 writel(DB_KEY_TX | prod, txr->tx_doorbell);
468
469tx_done:
470
471 mmiowb();
472
473 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
474 netif_tx_stop_queue(txq);
475
476 /* netif_tx_stop_queue() must be done before checking
477 * tx index in bnxt_tx_avail() below, because in
478 * bnxt_tx_int(), we update tx index before checking for
479 * netif_tx_queue_stopped().
480 */
481 smp_mb();
482 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
483 netif_tx_wake_queue(txq);
484 }
485 return NETDEV_TX_OK;
486
487tx_dma_error:
488 last_frag = i;
489
490 /* start back at beginning and unmap skb */
491 prod = txr->tx_prod;
492 tx_buf = &txr->tx_buf_ring[prod];
493 tx_buf->skb = NULL;
494 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
495 skb_headlen(skb), PCI_DMA_TODEVICE);
496 prod = NEXT_TX(prod);
497
498 /* unmap remaining mapped pages */
499 for (i = 0; i < last_frag; i++) {
500 prod = NEXT_TX(prod);
501 tx_buf = &txr->tx_buf_ring[prod];
502 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
503 skb_frag_size(&skb_shinfo(skb)->frags[i]),
504 PCI_DMA_TODEVICE);
505 }
506
507 dev_kfree_skb_any(skb);
508 return NETDEV_TX_OK;
509}
510
511static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
512{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500513 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500514 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400515 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
516 u16 cons = txr->tx_cons;
517 struct pci_dev *pdev = bp->pdev;
518 int i;
519 unsigned int tx_bytes = 0;
520
521 for (i = 0; i < nr_pkts; i++) {
522 struct bnxt_sw_tx_bd *tx_buf;
523 struct sk_buff *skb;
524 int j, last;
525
526 tx_buf = &txr->tx_buf_ring[cons];
527 cons = NEXT_TX(cons);
528 skb = tx_buf->skb;
529 tx_buf->skb = NULL;
530
531 if (tx_buf->is_push) {
532 tx_buf->is_push = 0;
533 goto next_tx_int;
534 }
535
536 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
537 skb_headlen(skb), PCI_DMA_TODEVICE);
538 last = tx_buf->nr_frags;
539
540 for (j = 0; j < last; j++) {
541 cons = NEXT_TX(cons);
542 tx_buf = &txr->tx_buf_ring[cons];
543 dma_unmap_page(
544 &pdev->dev,
545 dma_unmap_addr(tx_buf, mapping),
546 skb_frag_size(&skb_shinfo(skb)->frags[j]),
547 PCI_DMA_TODEVICE);
548 }
549
550next_tx_int:
551 cons = NEXT_TX(cons);
552
553 tx_bytes += skb->len;
554 dev_kfree_skb_any(skb);
555 }
556
557 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
558 txr->tx_cons = cons;
559
560 /* Need to make the tx_cons update visible to bnxt_start_xmit()
561 * before checking for netif_tx_queue_stopped(). Without the
562 * memory barrier, there is a small possibility that bnxt_start_xmit()
563 * will miss it and cause the queue to be stopped forever.
564 */
565 smp_mb();
566
567 if (unlikely(netif_tx_queue_stopped(txq)) &&
568 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
569 __netif_tx_lock(txq, smp_processor_id());
570 if (netif_tx_queue_stopped(txq) &&
571 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
572 txr->dev_state != BNXT_DEV_STATE_CLOSING)
573 netif_tx_wake_queue(txq);
574 __netif_tx_unlock(txq);
575 }
576}
577
578static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
579 gfp_t gfp)
580{
581 u8 *data;
582 struct pci_dev *pdev = bp->pdev;
583
584 data = kmalloc(bp->rx_buf_size, gfp);
585 if (!data)
586 return NULL;
587
588 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
589 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
590
591 if (dma_mapping_error(&pdev->dev, *mapping)) {
592 kfree(data);
593 data = NULL;
594 }
595 return data;
596}
597
598static inline int bnxt_alloc_rx_data(struct bnxt *bp,
599 struct bnxt_rx_ring_info *rxr,
600 u16 prod, gfp_t gfp)
601{
602 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
603 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
604 u8 *data;
605 dma_addr_t mapping;
606
607 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
608 if (!data)
609 return -ENOMEM;
610
611 rx_buf->data = data;
612 dma_unmap_addr_set(rx_buf, mapping, mapping);
613
614 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
615
616 return 0;
617}
618
619static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
620 u8 *data)
621{
622 u16 prod = rxr->rx_prod;
623 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
624 struct rx_bd *cons_bd, *prod_bd;
625
626 prod_rx_buf = &rxr->rx_buf_ring[prod];
627 cons_rx_buf = &rxr->rx_buf_ring[cons];
628
629 prod_rx_buf->data = data;
630
631 dma_unmap_addr_set(prod_rx_buf, mapping,
632 dma_unmap_addr(cons_rx_buf, mapping));
633
634 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
635 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
636
637 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
638}
639
640static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
641{
642 u16 next, max = rxr->rx_agg_bmap_size;
643
644 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
645 if (next >= max)
646 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
647 return next;
648}
649
650static inline int bnxt_alloc_rx_page(struct bnxt *bp,
651 struct bnxt_rx_ring_info *rxr,
652 u16 prod, gfp_t gfp)
653{
654 struct rx_bd *rxbd =
655 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
656 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
657 struct pci_dev *pdev = bp->pdev;
658 struct page *page;
659 dma_addr_t mapping;
660 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400661 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400662
Michael Chan89d0a062016-04-25 02:30:51 -0400663 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
664 page = rxr->rx_page;
665 if (!page) {
666 page = alloc_page(gfp);
667 if (!page)
668 return -ENOMEM;
669 rxr->rx_page = page;
670 rxr->rx_page_offset = 0;
671 }
672 offset = rxr->rx_page_offset;
673 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
674 if (rxr->rx_page_offset == PAGE_SIZE)
675 rxr->rx_page = NULL;
676 else
677 get_page(page);
678 } else {
679 page = alloc_page(gfp);
680 if (!page)
681 return -ENOMEM;
682 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400683
Michael Chan89d0a062016-04-25 02:30:51 -0400684 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400685 PCI_DMA_FROMDEVICE);
686 if (dma_mapping_error(&pdev->dev, mapping)) {
687 __free_page(page);
688 return -EIO;
689 }
690
691 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
692 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
693
694 __set_bit(sw_prod, rxr->rx_agg_bmap);
695 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
696 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
697
698 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400699 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400700 rx_agg_buf->mapping = mapping;
701 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
702 rxbd->rx_bd_opaque = sw_prod;
703 return 0;
704}
705
706static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
707 u32 agg_bufs)
708{
709 struct bnxt *bp = bnapi->bp;
710 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500711 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400712 u16 prod = rxr->rx_agg_prod;
713 u16 sw_prod = rxr->rx_sw_agg_prod;
714 u32 i;
715
716 for (i = 0; i < agg_bufs; i++) {
717 u16 cons;
718 struct rx_agg_cmp *agg;
719 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
720 struct rx_bd *prod_bd;
721 struct page *page;
722
723 agg = (struct rx_agg_cmp *)
724 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
725 cons = agg->rx_agg_cmp_opaque;
726 __clear_bit(cons, rxr->rx_agg_bmap);
727
728 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
729 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
730
731 __set_bit(sw_prod, rxr->rx_agg_bmap);
732 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
733 cons_rx_buf = &rxr->rx_agg_ring[cons];
734
735 /* It is possible for sw_prod to be equal to cons, so
736 * set cons_rx_buf->page to NULL first.
737 */
738 page = cons_rx_buf->page;
739 cons_rx_buf->page = NULL;
740 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400741 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400742
743 prod_rx_buf->mapping = cons_rx_buf->mapping;
744
745 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
746
747 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
748 prod_bd->rx_bd_opaque = sw_prod;
749
750 prod = NEXT_RX_AGG(prod);
751 sw_prod = NEXT_RX_AGG(sw_prod);
752 cp_cons = NEXT_CMP(cp_cons);
753 }
754 rxr->rx_agg_prod = prod;
755 rxr->rx_sw_agg_prod = sw_prod;
756}
757
758static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
759 struct bnxt_rx_ring_info *rxr, u16 cons,
760 u16 prod, u8 *data, dma_addr_t dma_addr,
761 unsigned int len)
762{
763 int err;
764 struct sk_buff *skb;
765
766 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
767 if (unlikely(err)) {
768 bnxt_reuse_rx_data(rxr, cons, data);
769 return NULL;
770 }
771
772 skb = build_skb(data, 0);
773 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
774 PCI_DMA_FROMDEVICE);
775 if (!skb) {
776 kfree(data);
777 return NULL;
778 }
779
780 skb_reserve(skb, BNXT_RX_OFFSET);
781 skb_put(skb, len);
782 return skb;
783}
784
785static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
786 struct sk_buff *skb, u16 cp_cons,
787 u32 agg_bufs)
788{
789 struct pci_dev *pdev = bp->pdev;
790 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500791 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400792 u16 prod = rxr->rx_agg_prod;
793 u32 i;
794
795 for (i = 0; i < agg_bufs; i++) {
796 u16 cons, frag_len;
797 struct rx_agg_cmp *agg;
798 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
799 struct page *page;
800 dma_addr_t mapping;
801
802 agg = (struct rx_agg_cmp *)
803 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
804 cons = agg->rx_agg_cmp_opaque;
805 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
806 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
807
808 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400809 skb_fill_page_desc(skb, i, cons_rx_buf->page,
810 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400811 __clear_bit(cons, rxr->rx_agg_bmap);
812
813 /* It is possible for bnxt_alloc_rx_page() to allocate
814 * a sw_prod index that equals the cons index, so we
815 * need to clear the cons entry now.
816 */
817 mapping = dma_unmap_addr(cons_rx_buf, mapping);
818 page = cons_rx_buf->page;
819 cons_rx_buf->page = NULL;
820
821 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
822 struct skb_shared_info *shinfo;
823 unsigned int nr_frags;
824
825 shinfo = skb_shinfo(skb);
826 nr_frags = --shinfo->nr_frags;
827 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
828
829 dev_kfree_skb(skb);
830
831 cons_rx_buf->page = page;
832
833 /* Update prod since possibly some pages have been
834 * allocated already.
835 */
836 rxr->rx_agg_prod = prod;
837 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
838 return NULL;
839 }
840
Michael Chan2839f282016-04-25 02:30:50 -0400841 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400842 PCI_DMA_FROMDEVICE);
843
844 skb->data_len += frag_len;
845 skb->len += frag_len;
846 skb->truesize += PAGE_SIZE;
847
848 prod = NEXT_RX_AGG(prod);
849 cp_cons = NEXT_CMP(cp_cons);
850 }
851 rxr->rx_agg_prod = prod;
852 return skb;
853}
854
855static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
856 u8 agg_bufs, u32 *raw_cons)
857{
858 u16 last;
859 struct rx_agg_cmp *agg;
860
861 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
862 last = RING_CMP(*raw_cons);
863 agg = (struct rx_agg_cmp *)
864 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
865 return RX_AGG_CMP_VALID(agg, *raw_cons);
866}
867
868static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
869 unsigned int len,
870 dma_addr_t mapping)
871{
872 struct bnxt *bp = bnapi->bp;
873 struct pci_dev *pdev = bp->pdev;
874 struct sk_buff *skb;
875
876 skb = napi_alloc_skb(&bnapi->napi, len);
877 if (!skb)
878 return NULL;
879
880 dma_sync_single_for_cpu(&pdev->dev, mapping,
881 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
882
883 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
884
885 dma_sync_single_for_device(&pdev->dev, mapping,
886 bp->rx_copy_thresh,
887 PCI_DMA_FROMDEVICE);
888
889 skb_put(skb, len);
890 return skb;
891}
892
Michael Chanfa7e2812016-05-10 19:18:00 -0400893static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
894 u32 *raw_cons, void *cmp)
895{
896 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
897 struct rx_cmp *rxcmp = cmp;
898 u32 tmp_raw_cons = *raw_cons;
899 u8 cmp_type, agg_bufs = 0;
900
901 cmp_type = RX_CMP_TYPE(rxcmp);
902
903 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
904 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
905 RX_CMP_AGG_BUFS) >>
906 RX_CMP_AGG_BUFS_SHIFT;
907 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
908 struct rx_tpa_end_cmp *tpa_end = cmp;
909
910 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
911 RX_TPA_END_CMP_AGG_BUFS) >>
912 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
913 }
914
915 if (agg_bufs) {
916 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
917 return -EBUSY;
918 }
919 *raw_cons = tmp_raw_cons;
920 return 0;
921}
922
923static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
924{
925 if (!rxr->bnapi->in_reset) {
926 rxr->bnapi->in_reset = true;
927 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
928 schedule_work(&bp->sp_task);
929 }
930 rxr->rx_next_cons = 0xffff;
931}
932
Michael Chanc0c050c2015-10-22 16:01:17 -0400933static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
934 struct rx_tpa_start_cmp *tpa_start,
935 struct rx_tpa_start_cmp_ext *tpa_start1)
936{
937 u8 agg_id = TPA_START_AGG_ID(tpa_start);
938 u16 cons, prod;
939 struct bnxt_tpa_info *tpa_info;
940 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
941 struct rx_bd *prod_bd;
942 dma_addr_t mapping;
943
944 cons = tpa_start->rx_tpa_start_cmp_opaque;
945 prod = rxr->rx_prod;
946 cons_rx_buf = &rxr->rx_buf_ring[cons];
947 prod_rx_buf = &rxr->rx_buf_ring[prod];
948 tpa_info = &rxr->rx_tpa[agg_id];
949
Michael Chanfa7e2812016-05-10 19:18:00 -0400950 if (unlikely(cons != rxr->rx_next_cons)) {
951 bnxt_sched_reset(bp, rxr);
952 return;
953 }
954
Michael Chanc0c050c2015-10-22 16:01:17 -0400955 prod_rx_buf->data = tpa_info->data;
956
957 mapping = tpa_info->mapping;
958 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
959
960 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
961
962 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
963
964 tpa_info->data = cons_rx_buf->data;
965 cons_rx_buf->data = NULL;
966 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
967
968 tpa_info->len =
969 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
970 RX_TPA_START_CMP_LEN_SHIFT;
971 if (likely(TPA_START_HASH_VALID(tpa_start))) {
972 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
973
974 tpa_info->hash_type = PKT_HASH_TYPE_L4;
975 tpa_info->gso_type = SKB_GSO_TCPV4;
976 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
977 if (hash_type == 3)
978 tpa_info->gso_type = SKB_GSO_TCPV6;
979 tpa_info->rss_hash =
980 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
981 } else {
982 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
983 tpa_info->gso_type = 0;
984 if (netif_msg_rx_err(bp))
985 netdev_warn(bp->dev, "TPA packet without valid hash\n");
986 }
987 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
988 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400989 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400990
991 rxr->rx_prod = NEXT_RX(prod);
992 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400993 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400994 cons_rx_buf = &rxr->rx_buf_ring[cons];
995
996 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
997 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
998 cons_rx_buf->data = NULL;
999}
1000
1001static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1002 u16 cp_cons, u32 agg_bufs)
1003{
1004 if (agg_bufs)
1005 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1006}
1007
Michael Chan94758f82016-06-13 02:25:35 -04001008static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1009 int payload_off, int tcp_ts,
1010 struct sk_buff *skb)
1011{
1012#ifdef CONFIG_INET
1013 struct tcphdr *th;
1014 int len, nw_off;
1015 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1016 u32 hdr_info = tpa_info->hdr_info;
1017 bool loopback = false;
1018
1019 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1020 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1021 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1022
1023 /* If the packet is an internal loopback packet, the offsets will
1024 * have an extra 4 bytes.
1025 */
1026 if (inner_mac_off == 4) {
1027 loopback = true;
1028 } else if (inner_mac_off > 4) {
1029 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1030 ETH_HLEN - 2));
1031
1032 /* We only support inner iPv4/ipv6. If we don't see the
1033 * correct protocol ID, it must be a loopback packet where
1034 * the offsets are off by 4.
1035 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001036 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001037 loopback = true;
1038 }
1039 if (loopback) {
1040 /* internal loopback packet, subtract all offsets by 4 */
1041 inner_ip_off -= 4;
1042 inner_mac_off -= 4;
1043 outer_ip_off -= 4;
1044 }
1045
1046 nw_off = inner_ip_off - ETH_HLEN;
1047 skb_set_network_header(skb, nw_off);
1048 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1049 struct ipv6hdr *iph = ipv6_hdr(skb);
1050
1051 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1052 len = skb->len - skb_transport_offset(skb);
1053 th = tcp_hdr(skb);
1054 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1055 } else {
1056 struct iphdr *iph = ip_hdr(skb);
1057
1058 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1059 len = skb->len - skb_transport_offset(skb);
1060 th = tcp_hdr(skb);
1061 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1062 }
1063
1064 if (inner_mac_off) { /* tunnel */
1065 struct udphdr *uh = NULL;
1066 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1067 ETH_HLEN - 2));
1068
1069 if (proto == htons(ETH_P_IP)) {
1070 struct iphdr *iph = (struct iphdr *)skb->data;
1071
1072 if (iph->protocol == IPPROTO_UDP)
1073 uh = (struct udphdr *)(iph + 1);
1074 } else {
1075 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1076
1077 if (iph->nexthdr == IPPROTO_UDP)
1078 uh = (struct udphdr *)(iph + 1);
1079 }
1080 if (uh) {
1081 if (uh->check)
1082 skb_shinfo(skb)->gso_type |=
1083 SKB_GSO_UDP_TUNNEL_CSUM;
1084 else
1085 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1086 }
1087 }
1088#endif
1089 return skb;
1090}
1091
Michael Chanc0c050c2015-10-22 16:01:17 -04001092#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1093#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1094
Michael Chan309369c2016-06-13 02:25:34 -04001095static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1096 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001097 struct sk_buff *skb)
1098{
Michael Chand1611c32015-10-25 22:27:57 -04001099#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001100 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001101 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001102
Michael Chan309369c2016-06-13 02:25:34 -04001103 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001104 tcp_opt_len = 12;
1105
Michael Chanc0c050c2015-10-22 16:01:17 -04001106 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1107 struct iphdr *iph;
1108
1109 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1110 ETH_HLEN;
1111 skb_set_network_header(skb, nw_off);
1112 iph = ip_hdr(skb);
1113 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1114 len = skb->len - skb_transport_offset(skb);
1115 th = tcp_hdr(skb);
1116 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1117 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1118 struct ipv6hdr *iph;
1119
1120 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1121 ETH_HLEN;
1122 skb_set_network_header(skb, nw_off);
1123 iph = ipv6_hdr(skb);
1124 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1125 len = skb->len - skb_transport_offset(skb);
1126 th = tcp_hdr(skb);
1127 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1128 } else {
1129 dev_kfree_skb_any(skb);
1130 return NULL;
1131 }
1132 tcp_gro_complete(skb);
1133
1134 if (nw_off) { /* tunnel */
1135 struct udphdr *uh = NULL;
1136
1137 if (skb->protocol == htons(ETH_P_IP)) {
1138 struct iphdr *iph = (struct iphdr *)skb->data;
1139
1140 if (iph->protocol == IPPROTO_UDP)
1141 uh = (struct udphdr *)(iph + 1);
1142 } else {
1143 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1144
1145 if (iph->nexthdr == IPPROTO_UDP)
1146 uh = (struct udphdr *)(iph + 1);
1147 }
1148 if (uh) {
1149 if (uh->check)
1150 skb_shinfo(skb)->gso_type |=
1151 SKB_GSO_UDP_TUNNEL_CSUM;
1152 else
1153 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1154 }
1155 }
1156#endif
1157 return skb;
1158}
1159
Michael Chan309369c2016-06-13 02:25:34 -04001160static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1161 struct bnxt_tpa_info *tpa_info,
1162 struct rx_tpa_end_cmp *tpa_end,
1163 struct rx_tpa_end_cmp_ext *tpa_end1,
1164 struct sk_buff *skb)
1165{
1166#ifdef CONFIG_INET
1167 int payload_off;
1168 u16 segs;
1169
1170 segs = TPA_END_TPA_SEGS(tpa_end);
1171 if (segs == 1)
1172 return skb;
1173
1174 NAPI_GRO_CB(skb)->count = segs;
1175 skb_shinfo(skb)->gso_size =
1176 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1177 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1178 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1179 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1180 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1181 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1182#endif
1183 return skb;
1184}
1185
Michael Chanc0c050c2015-10-22 16:01:17 -04001186static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1187 struct bnxt_napi *bnapi,
1188 u32 *raw_cons,
1189 struct rx_tpa_end_cmp *tpa_end,
1190 struct rx_tpa_end_cmp_ext *tpa_end1,
1191 bool *agg_event)
1192{
1193 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001194 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001195 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1196 u8 *data, agg_bufs;
1197 u16 cp_cons = RING_CMP(*raw_cons);
1198 unsigned int len;
1199 struct bnxt_tpa_info *tpa_info;
1200 dma_addr_t mapping;
1201 struct sk_buff *skb;
1202
Michael Chanfa7e2812016-05-10 19:18:00 -04001203 if (unlikely(bnapi->in_reset)) {
1204 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1205
1206 if (rc < 0)
1207 return ERR_PTR(-EBUSY);
1208 return NULL;
1209 }
1210
Michael Chanc0c050c2015-10-22 16:01:17 -04001211 tpa_info = &rxr->rx_tpa[agg_id];
1212 data = tpa_info->data;
1213 prefetch(data);
1214 len = tpa_info->len;
1215 mapping = tpa_info->mapping;
1216
1217 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1218 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1219
1220 if (agg_bufs) {
1221 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1222 return ERR_PTR(-EBUSY);
1223
1224 *agg_event = true;
1225 cp_cons = NEXT_CMP(cp_cons);
1226 }
1227
1228 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1229 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1230 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1231 agg_bufs, (int)MAX_SKB_FRAGS);
1232 return NULL;
1233 }
1234
1235 if (len <= bp->rx_copy_thresh) {
1236 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1237 if (!skb) {
1238 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1239 return NULL;
1240 }
1241 } else {
1242 u8 *new_data;
1243 dma_addr_t new_mapping;
1244
1245 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1246 if (!new_data) {
1247 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1248 return NULL;
1249 }
1250
1251 tpa_info->data = new_data;
1252 tpa_info->mapping = new_mapping;
1253
1254 skb = build_skb(data, 0);
1255 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1256 PCI_DMA_FROMDEVICE);
1257
1258 if (!skb) {
1259 kfree(data);
1260 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1261 return NULL;
1262 }
1263 skb_reserve(skb, BNXT_RX_OFFSET);
1264 skb_put(skb, len);
1265 }
1266
1267 if (agg_bufs) {
1268 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1269 if (!skb) {
1270 /* Page reuse already handled by bnxt_rx_pages(). */
1271 return NULL;
1272 }
1273 }
1274 skb->protocol = eth_type_trans(skb, bp->dev);
1275
1276 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1277 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1278
Michael Chan8852ddb2016-06-06 02:37:16 -04001279 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1280 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001281 u16 vlan_proto = tpa_info->metadata >>
1282 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001283 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001284
Michael Chan8852ddb2016-06-06 02:37:16 -04001285 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001286 }
1287
1288 skb_checksum_none_assert(skb);
1289 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1290 skb->ip_summed = CHECKSUM_UNNECESSARY;
1291 skb->csum_level =
1292 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1293 }
1294
1295 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001296 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001297
1298 return skb;
1299}
1300
1301/* returns the following:
1302 * 1 - 1 packet successfully received
1303 * 0 - successful TPA_START, packet not completed yet
1304 * -EBUSY - completion ring does not have all the agg buffers yet
1305 * -ENOMEM - packet aborted due to out of memory
1306 * -EIO - packet aborted due to hw error indicated in BD
1307 */
1308static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1309 bool *agg_event)
1310{
1311 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001312 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001313 struct net_device *dev = bp->dev;
1314 struct rx_cmp *rxcmp;
1315 struct rx_cmp_ext *rxcmp1;
1316 u32 tmp_raw_cons = *raw_cons;
1317 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1318 struct bnxt_sw_rx_bd *rx_buf;
1319 unsigned int len;
1320 u8 *data, agg_bufs, cmp_type;
1321 dma_addr_t dma_addr;
1322 struct sk_buff *skb;
1323 int rc = 0;
1324
1325 rxcmp = (struct rx_cmp *)
1326 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1327
1328 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1329 cp_cons = RING_CMP(tmp_raw_cons);
1330 rxcmp1 = (struct rx_cmp_ext *)
1331 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1332
1333 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1334 return -EBUSY;
1335
1336 cmp_type = RX_CMP_TYPE(rxcmp);
1337
1338 prod = rxr->rx_prod;
1339
1340 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1341 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1342 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1343
1344 goto next_rx_no_prod;
1345
1346 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1347 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1348 (struct rx_tpa_end_cmp *)rxcmp,
1349 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1350 agg_event);
1351
1352 if (unlikely(IS_ERR(skb)))
1353 return -EBUSY;
1354
1355 rc = -ENOMEM;
1356 if (likely(skb)) {
1357 skb_record_rx_queue(skb, bnapi->index);
1358 skb_mark_napi_id(skb, &bnapi->napi);
1359 if (bnxt_busy_polling(bnapi))
1360 netif_receive_skb(skb);
1361 else
1362 napi_gro_receive(&bnapi->napi, skb);
1363 rc = 1;
1364 }
1365 goto next_rx_no_prod;
1366 }
1367
1368 cons = rxcmp->rx_cmp_opaque;
1369 rx_buf = &rxr->rx_buf_ring[cons];
1370 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001371 if (unlikely(cons != rxr->rx_next_cons)) {
1372 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1373
1374 bnxt_sched_reset(bp, rxr);
1375 return rc1;
1376 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001377 prefetch(data);
1378
1379 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1380 RX_CMP_AGG_BUFS_SHIFT;
1381
1382 if (agg_bufs) {
1383 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1384 return -EBUSY;
1385
1386 cp_cons = NEXT_CMP(cp_cons);
1387 *agg_event = true;
1388 }
1389
1390 rx_buf->data = NULL;
1391 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1392 bnxt_reuse_rx_data(rxr, cons, data);
1393 if (agg_bufs)
1394 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1395
1396 rc = -EIO;
1397 goto next_rx;
1398 }
1399
1400 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1401 dma_addr = dma_unmap_addr(rx_buf, mapping);
1402
1403 if (len <= bp->rx_copy_thresh) {
1404 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1405 bnxt_reuse_rx_data(rxr, cons, data);
1406 if (!skb) {
1407 rc = -ENOMEM;
1408 goto next_rx;
1409 }
1410 } else {
1411 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1412 if (!skb) {
1413 rc = -ENOMEM;
1414 goto next_rx;
1415 }
1416 }
1417
1418 if (agg_bufs) {
1419 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1420 if (!skb) {
1421 rc = -ENOMEM;
1422 goto next_rx;
1423 }
1424 }
1425
1426 if (RX_CMP_HASH_VALID(rxcmp)) {
1427 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1428 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1429
1430 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1431 if (hash_type != 1 && hash_type != 3)
1432 type = PKT_HASH_TYPE_L3;
1433 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1434 }
1435
1436 skb->protocol = eth_type_trans(skb, dev);
1437
Michael Chan8852ddb2016-06-06 02:37:16 -04001438 if ((rxcmp1->rx_cmp_flags2 &
1439 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1440 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001441 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001442 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001443 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1444
Michael Chan8852ddb2016-06-06 02:37:16 -04001445 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001446 }
1447
1448 skb_checksum_none_assert(skb);
1449 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1450 if (dev->features & NETIF_F_RXCSUM) {
1451 skb->ip_summed = CHECKSUM_UNNECESSARY;
1452 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1453 }
1454 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001455 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1456 if (dev->features & NETIF_F_RXCSUM)
1457 cpr->rx_l4_csum_errors++;
1458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001459 }
1460
1461 skb_record_rx_queue(skb, bnapi->index);
1462 skb_mark_napi_id(skb, &bnapi->napi);
1463 if (bnxt_busy_polling(bnapi))
1464 netif_receive_skb(skb);
1465 else
1466 napi_gro_receive(&bnapi->napi, skb);
1467 rc = 1;
1468
1469next_rx:
1470 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001471 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001472
1473next_rx_no_prod:
1474 *raw_cons = tmp_raw_cons;
1475
1476 return rc;
1477}
1478
Michael Chan4bb13ab2016-04-05 14:09:01 -04001479#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001480 ((data) & \
1481 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001482
Michael Chanc0c050c2015-10-22 16:01:17 -04001483static int bnxt_async_event_process(struct bnxt *bp,
1484 struct hwrm_async_event_cmpl *cmpl)
1485{
1486 u16 event_id = le16_to_cpu(cmpl->event_id);
1487
1488 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1489 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001490 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001491 u32 data1 = le32_to_cpu(cmpl->event_data1);
1492 struct bnxt_link_info *link_info = &bp->link_info;
1493
1494 if (BNXT_VF(bp))
1495 goto async_event_process_exit;
1496 if (data1 & 0x20000) {
1497 u16 fw_speed = link_info->force_link_speed;
1498 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1499
1500 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1501 speed);
1502 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001503 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001504 /* fall thru */
1505 }
Michael Chan87c374d2016-12-02 21:17:16 -05001506 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001507 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001508 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001509 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001510 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001511 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001512 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001513 u32 data1 = le32_to_cpu(cmpl->event_data1);
1514 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1515
1516 if (BNXT_VF(bp))
1517 break;
1518
1519 if (bp->pf.port_id != port_id)
1520 break;
1521
Michael Chan4bb13ab2016-04-05 14:09:01 -04001522 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1523 break;
1524 }
Michael Chan87c374d2016-12-02 21:17:16 -05001525 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001526 if (BNXT_PF(bp))
1527 goto async_event_process_exit;
1528 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1529 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001530 default:
1531 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1532 event_id);
Jeffrey Huang19241362016-02-26 04:00:00 -05001533 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001534 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001535 schedule_work(&bp->sp_task);
1536async_event_process_exit:
Michael Chanc0c050c2015-10-22 16:01:17 -04001537 return 0;
1538}
1539
1540static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1541{
1542 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1543 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1544 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1545 (struct hwrm_fwd_req_cmpl *)txcmp;
1546
1547 switch (cmpl_type) {
1548 case CMPL_BASE_TYPE_HWRM_DONE:
1549 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1550 if (seq_id == bp->hwrm_intr_seq_id)
1551 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1552 else
1553 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1554 break;
1555
1556 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1557 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1558
1559 if ((vf_id < bp->pf.first_vf_id) ||
1560 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1561 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1562 vf_id);
1563 return -EINVAL;
1564 }
1565
1566 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1567 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1568 schedule_work(&bp->sp_task);
1569 break;
1570
1571 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1572 bnxt_async_event_process(bp,
1573 (struct hwrm_async_event_cmpl *)txcmp);
1574
1575 default:
1576 break;
1577 }
1578
1579 return 0;
1580}
1581
1582static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1583{
1584 struct bnxt_napi *bnapi = dev_instance;
1585 struct bnxt *bp = bnapi->bp;
1586 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1587 u32 cons = RING_CMP(cpr->cp_raw_cons);
1588
1589 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1590 napi_schedule(&bnapi->napi);
1591 return IRQ_HANDLED;
1592}
1593
1594static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1595{
1596 u32 raw_cons = cpr->cp_raw_cons;
1597 u16 cons = RING_CMP(raw_cons);
1598 struct tx_cmp *txcmp;
1599
1600 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1601
1602 return TX_CMP_VALID(txcmp, raw_cons);
1603}
1604
Michael Chanc0c050c2015-10-22 16:01:17 -04001605static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1606{
1607 struct bnxt_napi *bnapi = dev_instance;
1608 struct bnxt *bp = bnapi->bp;
1609 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1610 u32 cons = RING_CMP(cpr->cp_raw_cons);
1611 u32 int_status;
1612
1613 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1614
1615 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001616 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001617 /* return if erroneous interrupt */
1618 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1619 return IRQ_NONE;
1620 }
1621
1622 /* disable ring IRQ */
1623 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1624
1625 /* Return here if interrupt is shared and is disabled. */
1626 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1627 return IRQ_HANDLED;
1628
1629 napi_schedule(&bnapi->napi);
1630 return IRQ_HANDLED;
1631}
1632
1633static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1634{
1635 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1636 u32 raw_cons = cpr->cp_raw_cons;
1637 u32 cons;
1638 int tx_pkts = 0;
1639 int rx_pkts = 0;
1640 bool rx_event = false;
1641 bool agg_event = false;
1642 struct tx_cmp *txcmp;
1643
1644 while (1) {
1645 int rc;
1646
1647 cons = RING_CMP(raw_cons);
1648 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1649
1650 if (!TX_CMP_VALID(txcmp, raw_cons))
1651 break;
1652
Michael Chan67a95e22016-05-04 16:56:43 -04001653 /* The valid test of the entry must be done first before
1654 * reading any further.
1655 */
Michael Chanb67daab2016-05-15 03:04:51 -04001656 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001657 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1658 tx_pkts++;
1659 /* return full budget so NAPI will complete. */
1660 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1661 rx_pkts = budget;
1662 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1663 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1664 if (likely(rc >= 0))
1665 rx_pkts += rc;
1666 else if (rc == -EBUSY) /* partial completion */
1667 break;
1668 rx_event = true;
1669 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1670 CMPL_BASE_TYPE_HWRM_DONE) ||
1671 (TX_CMP_TYPE(txcmp) ==
1672 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1673 (TX_CMP_TYPE(txcmp) ==
1674 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1675 bnxt_hwrm_handler(bp, txcmp);
1676 }
1677 raw_cons = NEXT_RAW_CMP(raw_cons);
1678
1679 if (rx_pkts == budget)
1680 break;
1681 }
1682
1683 cpr->cp_raw_cons = raw_cons;
1684 /* ACK completion ring before freeing tx ring and producing new
1685 * buffers in rx/agg rings to prevent overflowing the completion
1686 * ring.
1687 */
1688 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1689
1690 if (tx_pkts)
1691 bnxt_tx_int(bp, bnapi, tx_pkts);
1692
1693 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001694 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001695
1696 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1697 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1698 if (agg_event) {
1699 writel(DB_KEY_RX | rxr->rx_agg_prod,
1700 rxr->rx_agg_doorbell);
1701 writel(DB_KEY_RX | rxr->rx_agg_prod,
1702 rxr->rx_agg_doorbell);
1703 }
1704 }
1705 return rx_pkts;
1706}
1707
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001708static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1709{
1710 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1711 struct bnxt *bp = bnapi->bp;
1712 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1713 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1714 struct tx_cmp *txcmp;
1715 struct rx_cmp_ext *rxcmp1;
1716 u32 cp_cons, tmp_raw_cons;
1717 u32 raw_cons = cpr->cp_raw_cons;
1718 u32 rx_pkts = 0;
1719 bool agg_event = false;
1720
1721 while (1) {
1722 int rc;
1723
1724 cp_cons = RING_CMP(raw_cons);
1725 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1726
1727 if (!TX_CMP_VALID(txcmp, raw_cons))
1728 break;
1729
1730 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1731 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1732 cp_cons = RING_CMP(tmp_raw_cons);
1733 rxcmp1 = (struct rx_cmp_ext *)
1734 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1735
1736 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1737 break;
1738
1739 /* force an error to recycle the buffer */
1740 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1741 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1742
1743 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1744 if (likely(rc == -EIO))
1745 rx_pkts++;
1746 else if (rc == -EBUSY) /* partial completion */
1747 break;
1748 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1749 CMPL_BASE_TYPE_HWRM_DONE)) {
1750 bnxt_hwrm_handler(bp, txcmp);
1751 } else {
1752 netdev_err(bp->dev,
1753 "Invalid completion received on special ring\n");
1754 }
1755 raw_cons = NEXT_RAW_CMP(raw_cons);
1756
1757 if (rx_pkts == budget)
1758 break;
1759 }
1760
1761 cpr->cp_raw_cons = raw_cons;
1762 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1763 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1764 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1765
1766 if (agg_event) {
1767 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1768 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1769 }
1770
1771 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1772 napi_complete(napi);
1773 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1774 }
1775 return rx_pkts;
1776}
1777
Michael Chanc0c050c2015-10-22 16:01:17 -04001778static int bnxt_poll(struct napi_struct *napi, int budget)
1779{
1780 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1781 struct bnxt *bp = bnapi->bp;
1782 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1783 int work_done = 0;
1784
1785 if (!bnxt_lock_napi(bnapi))
1786 return budget;
1787
1788 while (1) {
1789 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1790
1791 if (work_done >= budget)
1792 break;
1793
1794 if (!bnxt_has_work(bp, cpr)) {
1795 napi_complete(napi);
1796 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1797 break;
1798 }
1799 }
1800 mmiowb();
1801 bnxt_unlock_napi(bnapi);
1802 return work_done;
1803}
1804
1805#ifdef CONFIG_NET_RX_BUSY_POLL
1806static int bnxt_busy_poll(struct napi_struct *napi)
1807{
1808 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1809 struct bnxt *bp = bnapi->bp;
1810 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1811 int rx_work, budget = 4;
1812
1813 if (atomic_read(&bp->intr_sem) != 0)
1814 return LL_FLUSH_FAILED;
1815
Andy Gospodarek867d1212016-11-22 13:14:08 -05001816 if (!bp->link_info.link_up)
1817 return LL_FLUSH_FAILED;
1818
Michael Chanc0c050c2015-10-22 16:01:17 -04001819 if (!bnxt_lock_poll(bnapi))
1820 return LL_FLUSH_BUSY;
1821
1822 rx_work = bnxt_poll_work(bp, bnapi, budget);
1823
1824 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1825
1826 bnxt_unlock_poll(bnapi);
1827 return rx_work;
1828}
1829#endif
1830
1831static void bnxt_free_tx_skbs(struct bnxt *bp)
1832{
1833 int i, max_idx;
1834 struct pci_dev *pdev = bp->pdev;
1835
Michael Chanb6ab4b02016-01-02 23:44:59 -05001836 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001837 return;
1838
1839 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1840 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001841 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001842 int j;
1843
Michael Chanc0c050c2015-10-22 16:01:17 -04001844 for (j = 0; j < max_idx;) {
1845 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1846 struct sk_buff *skb = tx_buf->skb;
1847 int k, last;
1848
1849 if (!skb) {
1850 j++;
1851 continue;
1852 }
1853
1854 tx_buf->skb = NULL;
1855
1856 if (tx_buf->is_push) {
1857 dev_kfree_skb(skb);
1858 j += 2;
1859 continue;
1860 }
1861
1862 dma_unmap_single(&pdev->dev,
1863 dma_unmap_addr(tx_buf, mapping),
1864 skb_headlen(skb),
1865 PCI_DMA_TODEVICE);
1866
1867 last = tx_buf->nr_frags;
1868 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001869 for (k = 0; k < last; k++, j++) {
1870 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001871 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1872
Michael Chand612a572016-01-28 03:11:22 -05001873 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001874 dma_unmap_page(
1875 &pdev->dev,
1876 dma_unmap_addr(tx_buf, mapping),
1877 skb_frag_size(frag), PCI_DMA_TODEVICE);
1878 }
1879 dev_kfree_skb(skb);
1880 }
1881 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1882 }
1883}
1884
1885static void bnxt_free_rx_skbs(struct bnxt *bp)
1886{
1887 int i, max_idx, max_agg_idx;
1888 struct pci_dev *pdev = bp->pdev;
1889
Michael Chanb6ab4b02016-01-02 23:44:59 -05001890 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001891 return;
1892
1893 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1894 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1895 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001896 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001897 int j;
1898
Michael Chanc0c050c2015-10-22 16:01:17 -04001899 if (rxr->rx_tpa) {
1900 for (j = 0; j < MAX_TPA; j++) {
1901 struct bnxt_tpa_info *tpa_info =
1902 &rxr->rx_tpa[j];
1903 u8 *data = tpa_info->data;
1904
1905 if (!data)
1906 continue;
1907
1908 dma_unmap_single(
1909 &pdev->dev,
1910 dma_unmap_addr(tpa_info, mapping),
1911 bp->rx_buf_use_size,
1912 PCI_DMA_FROMDEVICE);
1913
1914 tpa_info->data = NULL;
1915
1916 kfree(data);
1917 }
1918 }
1919
1920 for (j = 0; j < max_idx; j++) {
1921 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1922 u8 *data = rx_buf->data;
1923
1924 if (!data)
1925 continue;
1926
1927 dma_unmap_single(&pdev->dev,
1928 dma_unmap_addr(rx_buf, mapping),
1929 bp->rx_buf_use_size,
1930 PCI_DMA_FROMDEVICE);
1931
1932 rx_buf->data = NULL;
1933
1934 kfree(data);
1935 }
1936
1937 for (j = 0; j < max_agg_idx; j++) {
1938 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1939 &rxr->rx_agg_ring[j];
1940 struct page *page = rx_agg_buf->page;
1941
1942 if (!page)
1943 continue;
1944
1945 dma_unmap_page(&pdev->dev,
1946 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001947 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001948
1949 rx_agg_buf->page = NULL;
1950 __clear_bit(j, rxr->rx_agg_bmap);
1951
1952 __free_page(page);
1953 }
Michael Chan89d0a062016-04-25 02:30:51 -04001954 if (rxr->rx_page) {
1955 __free_page(rxr->rx_page);
1956 rxr->rx_page = NULL;
1957 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001958 }
1959}
1960
1961static void bnxt_free_skbs(struct bnxt *bp)
1962{
1963 bnxt_free_tx_skbs(bp);
1964 bnxt_free_rx_skbs(bp);
1965}
1966
1967static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1968{
1969 struct pci_dev *pdev = bp->pdev;
1970 int i;
1971
1972 for (i = 0; i < ring->nr_pages; i++) {
1973 if (!ring->pg_arr[i])
1974 continue;
1975
1976 dma_free_coherent(&pdev->dev, ring->page_size,
1977 ring->pg_arr[i], ring->dma_arr[i]);
1978
1979 ring->pg_arr[i] = NULL;
1980 }
1981 if (ring->pg_tbl) {
1982 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1983 ring->pg_tbl, ring->pg_tbl_map);
1984 ring->pg_tbl = NULL;
1985 }
1986 if (ring->vmem_size && *ring->vmem) {
1987 vfree(*ring->vmem);
1988 *ring->vmem = NULL;
1989 }
1990}
1991
1992static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1993{
1994 int i;
1995 struct pci_dev *pdev = bp->pdev;
1996
1997 if (ring->nr_pages > 1) {
1998 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1999 ring->nr_pages * 8,
2000 &ring->pg_tbl_map,
2001 GFP_KERNEL);
2002 if (!ring->pg_tbl)
2003 return -ENOMEM;
2004 }
2005
2006 for (i = 0; i < ring->nr_pages; i++) {
2007 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2008 ring->page_size,
2009 &ring->dma_arr[i],
2010 GFP_KERNEL);
2011 if (!ring->pg_arr[i])
2012 return -ENOMEM;
2013
2014 if (ring->nr_pages > 1)
2015 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2016 }
2017
2018 if (ring->vmem_size) {
2019 *ring->vmem = vzalloc(ring->vmem_size);
2020 if (!(*ring->vmem))
2021 return -ENOMEM;
2022 }
2023 return 0;
2024}
2025
2026static void bnxt_free_rx_rings(struct bnxt *bp)
2027{
2028 int i;
2029
Michael Chanb6ab4b02016-01-02 23:44:59 -05002030 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002031 return;
2032
2033 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002034 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002035 struct bnxt_ring_struct *ring;
2036
Michael Chanc0c050c2015-10-22 16:01:17 -04002037 kfree(rxr->rx_tpa);
2038 rxr->rx_tpa = NULL;
2039
2040 kfree(rxr->rx_agg_bmap);
2041 rxr->rx_agg_bmap = NULL;
2042
2043 ring = &rxr->rx_ring_struct;
2044 bnxt_free_ring(bp, ring);
2045
2046 ring = &rxr->rx_agg_ring_struct;
2047 bnxt_free_ring(bp, ring);
2048 }
2049}
2050
2051static int bnxt_alloc_rx_rings(struct bnxt *bp)
2052{
2053 int i, rc, agg_rings = 0, tpa_rings = 0;
2054
Michael Chanb6ab4b02016-01-02 23:44:59 -05002055 if (!bp->rx_ring)
2056 return -ENOMEM;
2057
Michael Chanc0c050c2015-10-22 16:01:17 -04002058 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2059 agg_rings = 1;
2060
2061 if (bp->flags & BNXT_FLAG_TPA)
2062 tpa_rings = 1;
2063
2064 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002065 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002066 struct bnxt_ring_struct *ring;
2067
Michael Chanc0c050c2015-10-22 16:01:17 -04002068 ring = &rxr->rx_ring_struct;
2069
2070 rc = bnxt_alloc_ring(bp, ring);
2071 if (rc)
2072 return rc;
2073
2074 if (agg_rings) {
2075 u16 mem_size;
2076
2077 ring = &rxr->rx_agg_ring_struct;
2078 rc = bnxt_alloc_ring(bp, ring);
2079 if (rc)
2080 return rc;
2081
2082 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2083 mem_size = rxr->rx_agg_bmap_size / 8;
2084 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2085 if (!rxr->rx_agg_bmap)
2086 return -ENOMEM;
2087
2088 if (tpa_rings) {
2089 rxr->rx_tpa = kcalloc(MAX_TPA,
2090 sizeof(struct bnxt_tpa_info),
2091 GFP_KERNEL);
2092 if (!rxr->rx_tpa)
2093 return -ENOMEM;
2094 }
2095 }
2096 }
2097 return 0;
2098}
2099
2100static void bnxt_free_tx_rings(struct bnxt *bp)
2101{
2102 int i;
2103 struct pci_dev *pdev = bp->pdev;
2104
Michael Chanb6ab4b02016-01-02 23:44:59 -05002105 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002106 return;
2107
2108 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002109 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002110 struct bnxt_ring_struct *ring;
2111
Michael Chanc0c050c2015-10-22 16:01:17 -04002112 if (txr->tx_push) {
2113 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2114 txr->tx_push, txr->tx_push_mapping);
2115 txr->tx_push = NULL;
2116 }
2117
2118 ring = &txr->tx_ring_struct;
2119
2120 bnxt_free_ring(bp, ring);
2121 }
2122}
2123
2124static int bnxt_alloc_tx_rings(struct bnxt *bp)
2125{
2126 int i, j, rc;
2127 struct pci_dev *pdev = bp->pdev;
2128
2129 bp->tx_push_size = 0;
2130 if (bp->tx_push_thresh) {
2131 int push_size;
2132
2133 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2134 bp->tx_push_thresh);
2135
Michael Chan4419dbe2016-02-10 17:33:49 -05002136 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002137 push_size = 0;
2138 bp->tx_push_thresh = 0;
2139 }
2140
2141 bp->tx_push_size = push_size;
2142 }
2143
2144 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002145 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002146 struct bnxt_ring_struct *ring;
2147
Michael Chanc0c050c2015-10-22 16:01:17 -04002148 ring = &txr->tx_ring_struct;
2149
2150 rc = bnxt_alloc_ring(bp, ring);
2151 if (rc)
2152 return rc;
2153
2154 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002155 dma_addr_t mapping;
2156
2157 /* One pre-allocated DMA buffer to backup
2158 * TX push operation
2159 */
2160 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2161 bp->tx_push_size,
2162 &txr->tx_push_mapping,
2163 GFP_KERNEL);
2164
2165 if (!txr->tx_push)
2166 return -ENOMEM;
2167
Michael Chanc0c050c2015-10-22 16:01:17 -04002168 mapping = txr->tx_push_mapping +
2169 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002170 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002171
Michael Chan4419dbe2016-02-10 17:33:49 -05002172 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002173 }
2174 ring->queue_id = bp->q_info[j].queue_id;
2175 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2176 j++;
2177 }
2178 return 0;
2179}
2180
2181static void bnxt_free_cp_rings(struct bnxt *bp)
2182{
2183 int i;
2184
2185 if (!bp->bnapi)
2186 return;
2187
2188 for (i = 0; i < bp->cp_nr_rings; i++) {
2189 struct bnxt_napi *bnapi = bp->bnapi[i];
2190 struct bnxt_cp_ring_info *cpr;
2191 struct bnxt_ring_struct *ring;
2192
2193 if (!bnapi)
2194 continue;
2195
2196 cpr = &bnapi->cp_ring;
2197 ring = &cpr->cp_ring_struct;
2198
2199 bnxt_free_ring(bp, ring);
2200 }
2201}
2202
2203static int bnxt_alloc_cp_rings(struct bnxt *bp)
2204{
2205 int i, rc;
2206
2207 for (i = 0; i < bp->cp_nr_rings; i++) {
2208 struct bnxt_napi *bnapi = bp->bnapi[i];
2209 struct bnxt_cp_ring_info *cpr;
2210 struct bnxt_ring_struct *ring;
2211
2212 if (!bnapi)
2213 continue;
2214
2215 cpr = &bnapi->cp_ring;
2216 ring = &cpr->cp_ring_struct;
2217
2218 rc = bnxt_alloc_ring(bp, ring);
2219 if (rc)
2220 return rc;
2221 }
2222 return 0;
2223}
2224
2225static void bnxt_init_ring_struct(struct bnxt *bp)
2226{
2227 int i;
2228
2229 for (i = 0; i < bp->cp_nr_rings; i++) {
2230 struct bnxt_napi *bnapi = bp->bnapi[i];
2231 struct bnxt_cp_ring_info *cpr;
2232 struct bnxt_rx_ring_info *rxr;
2233 struct bnxt_tx_ring_info *txr;
2234 struct bnxt_ring_struct *ring;
2235
2236 if (!bnapi)
2237 continue;
2238
2239 cpr = &bnapi->cp_ring;
2240 ring = &cpr->cp_ring_struct;
2241 ring->nr_pages = bp->cp_nr_pages;
2242 ring->page_size = HW_CMPD_RING_SIZE;
2243 ring->pg_arr = (void **)cpr->cp_desc_ring;
2244 ring->dma_arr = cpr->cp_desc_mapping;
2245 ring->vmem_size = 0;
2246
Michael Chanb6ab4b02016-01-02 23:44:59 -05002247 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002248 if (!rxr)
2249 goto skip_rx;
2250
Michael Chanc0c050c2015-10-22 16:01:17 -04002251 ring = &rxr->rx_ring_struct;
2252 ring->nr_pages = bp->rx_nr_pages;
2253 ring->page_size = HW_RXBD_RING_SIZE;
2254 ring->pg_arr = (void **)rxr->rx_desc_ring;
2255 ring->dma_arr = rxr->rx_desc_mapping;
2256 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2257 ring->vmem = (void **)&rxr->rx_buf_ring;
2258
2259 ring = &rxr->rx_agg_ring_struct;
2260 ring->nr_pages = bp->rx_agg_nr_pages;
2261 ring->page_size = HW_RXBD_RING_SIZE;
2262 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2263 ring->dma_arr = rxr->rx_agg_desc_mapping;
2264 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2265 ring->vmem = (void **)&rxr->rx_agg_ring;
2266
Michael Chan3b2b7d92016-01-02 23:45:00 -05002267skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002268 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002269 if (!txr)
2270 continue;
2271
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 ring = &txr->tx_ring_struct;
2273 ring->nr_pages = bp->tx_nr_pages;
2274 ring->page_size = HW_RXBD_RING_SIZE;
2275 ring->pg_arr = (void **)txr->tx_desc_ring;
2276 ring->dma_arr = txr->tx_desc_mapping;
2277 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2278 ring->vmem = (void **)&txr->tx_buf_ring;
2279 }
2280}
2281
2282static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2283{
2284 int i;
2285 u32 prod;
2286 struct rx_bd **rx_buf_ring;
2287
2288 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2289 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2290 int j;
2291 struct rx_bd *rxbd;
2292
2293 rxbd = rx_buf_ring[i];
2294 if (!rxbd)
2295 continue;
2296
2297 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2298 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2299 rxbd->rx_bd_opaque = prod;
2300 }
2301 }
2302}
2303
2304static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2305{
2306 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002307 struct bnxt_rx_ring_info *rxr;
2308 struct bnxt_ring_struct *ring;
2309 u32 prod, type;
2310 int i;
2311
Michael Chanc0c050c2015-10-22 16:01:17 -04002312 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2313 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2314
2315 if (NET_IP_ALIGN == 2)
2316 type |= RX_BD_FLAGS_SOP;
2317
Michael Chanb6ab4b02016-01-02 23:44:59 -05002318 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002319 ring = &rxr->rx_ring_struct;
2320 bnxt_init_rxbd_pages(ring, type);
2321
2322 prod = rxr->rx_prod;
2323 for (i = 0; i < bp->rx_ring_size; i++) {
2324 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2325 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2326 ring_nr, i, bp->rx_ring_size);
2327 break;
2328 }
2329 prod = NEXT_RX(prod);
2330 }
2331 rxr->rx_prod = prod;
2332 ring->fw_ring_id = INVALID_HW_RING_ID;
2333
Michael Chanedd0c2c2015-12-27 18:19:19 -05002334 ring = &rxr->rx_agg_ring_struct;
2335 ring->fw_ring_id = INVALID_HW_RING_ID;
2336
Michael Chanc0c050c2015-10-22 16:01:17 -04002337 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2338 return 0;
2339
Michael Chan2839f282016-04-25 02:30:50 -04002340 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002341 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2342
2343 bnxt_init_rxbd_pages(ring, type);
2344
2345 prod = rxr->rx_agg_prod;
2346 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2347 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2348 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2349 ring_nr, i, bp->rx_ring_size);
2350 break;
2351 }
2352 prod = NEXT_RX_AGG(prod);
2353 }
2354 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002355
2356 if (bp->flags & BNXT_FLAG_TPA) {
2357 if (rxr->rx_tpa) {
2358 u8 *data;
2359 dma_addr_t mapping;
2360
2361 for (i = 0; i < MAX_TPA; i++) {
2362 data = __bnxt_alloc_rx_data(bp, &mapping,
2363 GFP_KERNEL);
2364 if (!data)
2365 return -ENOMEM;
2366
2367 rxr->rx_tpa[i].data = data;
2368 rxr->rx_tpa[i].mapping = mapping;
2369 }
2370 } else {
2371 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2372 return -ENOMEM;
2373 }
2374 }
2375
2376 return 0;
2377}
2378
2379static int bnxt_init_rx_rings(struct bnxt *bp)
2380{
2381 int i, rc = 0;
2382
2383 for (i = 0; i < bp->rx_nr_rings; i++) {
2384 rc = bnxt_init_one_rx_ring(bp, i);
2385 if (rc)
2386 break;
2387 }
2388
2389 return rc;
2390}
2391
2392static int bnxt_init_tx_rings(struct bnxt *bp)
2393{
2394 u16 i;
2395
2396 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2397 MAX_SKB_FRAGS + 1);
2398
2399 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002400 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002401 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2402
2403 ring->fw_ring_id = INVALID_HW_RING_ID;
2404 }
2405
2406 return 0;
2407}
2408
2409static void bnxt_free_ring_grps(struct bnxt *bp)
2410{
2411 kfree(bp->grp_info);
2412 bp->grp_info = NULL;
2413}
2414
2415static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2416{
2417 int i;
2418
2419 if (irq_re_init) {
2420 bp->grp_info = kcalloc(bp->cp_nr_rings,
2421 sizeof(struct bnxt_ring_grp_info),
2422 GFP_KERNEL);
2423 if (!bp->grp_info)
2424 return -ENOMEM;
2425 }
2426 for (i = 0; i < bp->cp_nr_rings; i++) {
2427 if (irq_re_init)
2428 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2429 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2430 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2431 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2432 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2433 }
2434 return 0;
2435}
2436
2437static void bnxt_free_vnics(struct bnxt *bp)
2438{
2439 kfree(bp->vnic_info);
2440 bp->vnic_info = NULL;
2441 bp->nr_vnics = 0;
2442}
2443
2444static int bnxt_alloc_vnics(struct bnxt *bp)
2445{
2446 int num_vnics = 1;
2447
2448#ifdef CONFIG_RFS_ACCEL
2449 if (bp->flags & BNXT_FLAG_RFS)
2450 num_vnics += bp->rx_nr_rings;
2451#endif
2452
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002453 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2454 num_vnics++;
2455
Michael Chanc0c050c2015-10-22 16:01:17 -04002456 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2457 GFP_KERNEL);
2458 if (!bp->vnic_info)
2459 return -ENOMEM;
2460
2461 bp->nr_vnics = num_vnics;
2462 return 0;
2463}
2464
2465static void bnxt_init_vnics(struct bnxt *bp)
2466{
2467 int i;
2468
2469 for (i = 0; i < bp->nr_vnics; i++) {
2470 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2471
2472 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002473 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2474 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002475 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2476
2477 if (bp->vnic_info[i].rss_hash_key) {
2478 if (i == 0)
2479 prandom_bytes(vnic->rss_hash_key,
2480 HW_HASH_KEY_SIZE);
2481 else
2482 memcpy(vnic->rss_hash_key,
2483 bp->vnic_info[0].rss_hash_key,
2484 HW_HASH_KEY_SIZE);
2485 }
2486 }
2487}
2488
2489static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2490{
2491 int pages;
2492
2493 pages = ring_size / desc_per_pg;
2494
2495 if (!pages)
2496 return 1;
2497
2498 pages++;
2499
2500 while (pages & (pages - 1))
2501 pages++;
2502
2503 return pages;
2504}
2505
2506static void bnxt_set_tpa_flags(struct bnxt *bp)
2507{
2508 bp->flags &= ~BNXT_FLAG_TPA;
2509 if (bp->dev->features & NETIF_F_LRO)
2510 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002511 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002512 bp->flags |= BNXT_FLAG_GRO;
2513}
2514
2515/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2516 * be set on entry.
2517 */
2518void bnxt_set_ring_params(struct bnxt *bp)
2519{
2520 u32 ring_size, rx_size, rx_space;
2521 u32 agg_factor = 0, agg_ring_size = 0;
2522
2523 /* 8 for CRC and VLAN */
2524 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2525
2526 rx_space = rx_size + NET_SKB_PAD +
2527 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2528
2529 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2530 ring_size = bp->rx_ring_size;
2531 bp->rx_agg_ring_size = 0;
2532 bp->rx_agg_nr_pages = 0;
2533
2534 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002535 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002536
2537 bp->flags &= ~BNXT_FLAG_JUMBO;
2538 if (rx_space > PAGE_SIZE) {
2539 u32 jumbo_factor;
2540
2541 bp->flags |= BNXT_FLAG_JUMBO;
2542 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2543 if (jumbo_factor > agg_factor)
2544 agg_factor = jumbo_factor;
2545 }
2546 agg_ring_size = ring_size * agg_factor;
2547
2548 if (agg_ring_size) {
2549 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2550 RX_DESC_CNT);
2551 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2552 u32 tmp = agg_ring_size;
2553
2554 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2555 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2556 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2557 tmp, agg_ring_size);
2558 }
2559 bp->rx_agg_ring_size = agg_ring_size;
2560 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2561 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2562 rx_space = rx_size + NET_SKB_PAD +
2563 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2564 }
2565
2566 bp->rx_buf_use_size = rx_size;
2567 bp->rx_buf_size = rx_space;
2568
2569 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2570 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2571
2572 ring_size = bp->tx_ring_size;
2573 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2574 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2575
2576 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2577 bp->cp_ring_size = ring_size;
2578
2579 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2580 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2581 bp->cp_nr_pages = MAX_CP_PAGES;
2582 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2583 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2584 ring_size, bp->cp_ring_size);
2585 }
2586 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2587 bp->cp_ring_mask = bp->cp_bit - 1;
2588}
2589
2590static void bnxt_free_vnic_attributes(struct bnxt *bp)
2591{
2592 int i;
2593 struct bnxt_vnic_info *vnic;
2594 struct pci_dev *pdev = bp->pdev;
2595
2596 if (!bp->vnic_info)
2597 return;
2598
2599 for (i = 0; i < bp->nr_vnics; i++) {
2600 vnic = &bp->vnic_info[i];
2601
2602 kfree(vnic->fw_grp_ids);
2603 vnic->fw_grp_ids = NULL;
2604
2605 kfree(vnic->uc_list);
2606 vnic->uc_list = NULL;
2607
2608 if (vnic->mc_list) {
2609 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2610 vnic->mc_list, vnic->mc_list_mapping);
2611 vnic->mc_list = NULL;
2612 }
2613
2614 if (vnic->rss_table) {
2615 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2616 vnic->rss_table,
2617 vnic->rss_table_dma_addr);
2618 vnic->rss_table = NULL;
2619 }
2620
2621 vnic->rss_hash_key = NULL;
2622 vnic->flags = 0;
2623 }
2624}
2625
2626static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2627{
2628 int i, rc = 0, size;
2629 struct bnxt_vnic_info *vnic;
2630 struct pci_dev *pdev = bp->pdev;
2631 int max_rings;
2632
2633 for (i = 0; i < bp->nr_vnics; i++) {
2634 vnic = &bp->vnic_info[i];
2635
2636 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2637 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2638
2639 if (mem_size > 0) {
2640 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2641 if (!vnic->uc_list) {
2642 rc = -ENOMEM;
2643 goto out;
2644 }
2645 }
2646 }
2647
2648 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2649 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2650 vnic->mc_list =
2651 dma_alloc_coherent(&pdev->dev,
2652 vnic->mc_list_size,
2653 &vnic->mc_list_mapping,
2654 GFP_KERNEL);
2655 if (!vnic->mc_list) {
2656 rc = -ENOMEM;
2657 goto out;
2658 }
2659 }
2660
2661 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2662 max_rings = bp->rx_nr_rings;
2663 else
2664 max_rings = 1;
2665
2666 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2667 if (!vnic->fw_grp_ids) {
2668 rc = -ENOMEM;
2669 goto out;
2670 }
2671
2672 /* Allocate rss table and hash key */
2673 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2674 &vnic->rss_table_dma_addr,
2675 GFP_KERNEL);
2676 if (!vnic->rss_table) {
2677 rc = -ENOMEM;
2678 goto out;
2679 }
2680
2681 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2682
2683 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2684 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2685 }
2686 return 0;
2687
2688out:
2689 return rc;
2690}
2691
2692static void bnxt_free_hwrm_resources(struct bnxt *bp)
2693{
2694 struct pci_dev *pdev = bp->pdev;
2695
2696 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2697 bp->hwrm_cmd_resp_dma_addr);
2698
2699 bp->hwrm_cmd_resp_addr = NULL;
2700 if (bp->hwrm_dbg_resp_addr) {
2701 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2702 bp->hwrm_dbg_resp_addr,
2703 bp->hwrm_dbg_resp_dma_addr);
2704
2705 bp->hwrm_dbg_resp_addr = NULL;
2706 }
2707}
2708
2709static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2710{
2711 struct pci_dev *pdev = bp->pdev;
2712
2713 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2714 &bp->hwrm_cmd_resp_dma_addr,
2715 GFP_KERNEL);
2716 if (!bp->hwrm_cmd_resp_addr)
2717 return -ENOMEM;
2718 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2719 HWRM_DBG_REG_BUF_SIZE,
2720 &bp->hwrm_dbg_resp_dma_addr,
2721 GFP_KERNEL);
2722 if (!bp->hwrm_dbg_resp_addr)
2723 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2724
2725 return 0;
2726}
2727
2728static void bnxt_free_stats(struct bnxt *bp)
2729{
2730 u32 size, i;
2731 struct pci_dev *pdev = bp->pdev;
2732
Michael Chan3bdf56c2016-03-07 15:38:45 -05002733 if (bp->hw_rx_port_stats) {
2734 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2735 bp->hw_rx_port_stats,
2736 bp->hw_rx_port_stats_map);
2737 bp->hw_rx_port_stats = NULL;
2738 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2739 }
2740
Michael Chanc0c050c2015-10-22 16:01:17 -04002741 if (!bp->bnapi)
2742 return;
2743
2744 size = sizeof(struct ctx_hw_stats);
2745
2746 for (i = 0; i < bp->cp_nr_rings; i++) {
2747 struct bnxt_napi *bnapi = bp->bnapi[i];
2748 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2749
2750 if (cpr->hw_stats) {
2751 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2752 cpr->hw_stats_map);
2753 cpr->hw_stats = NULL;
2754 }
2755 }
2756}
2757
2758static int bnxt_alloc_stats(struct bnxt *bp)
2759{
2760 u32 size, i;
2761 struct pci_dev *pdev = bp->pdev;
2762
2763 size = sizeof(struct ctx_hw_stats);
2764
2765 for (i = 0; i < bp->cp_nr_rings; i++) {
2766 struct bnxt_napi *bnapi = bp->bnapi[i];
2767 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2768
2769 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2770 &cpr->hw_stats_map,
2771 GFP_KERNEL);
2772 if (!cpr->hw_stats)
2773 return -ENOMEM;
2774
2775 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2776 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002777
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002778 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002779 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2780 sizeof(struct tx_port_stats) + 1024;
2781
2782 bp->hw_rx_port_stats =
2783 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2784 &bp->hw_rx_port_stats_map,
2785 GFP_KERNEL);
2786 if (!bp->hw_rx_port_stats)
2787 return -ENOMEM;
2788
2789 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2790 512;
2791 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2792 sizeof(struct rx_port_stats) + 512;
2793 bp->flags |= BNXT_FLAG_PORT_STATS;
2794 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002795 return 0;
2796}
2797
2798static void bnxt_clear_ring_indices(struct bnxt *bp)
2799{
2800 int i;
2801
2802 if (!bp->bnapi)
2803 return;
2804
2805 for (i = 0; i < bp->cp_nr_rings; i++) {
2806 struct bnxt_napi *bnapi = bp->bnapi[i];
2807 struct bnxt_cp_ring_info *cpr;
2808 struct bnxt_rx_ring_info *rxr;
2809 struct bnxt_tx_ring_info *txr;
2810
2811 if (!bnapi)
2812 continue;
2813
2814 cpr = &bnapi->cp_ring;
2815 cpr->cp_raw_cons = 0;
2816
Michael Chanb6ab4b02016-01-02 23:44:59 -05002817 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002818 if (txr) {
2819 txr->tx_prod = 0;
2820 txr->tx_cons = 0;
2821 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002822
Michael Chanb6ab4b02016-01-02 23:44:59 -05002823 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002824 if (rxr) {
2825 rxr->rx_prod = 0;
2826 rxr->rx_agg_prod = 0;
2827 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002828 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002829 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002830 }
2831}
2832
2833static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2834{
2835#ifdef CONFIG_RFS_ACCEL
2836 int i;
2837
2838 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2839 * safe to delete the hash table.
2840 */
2841 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2842 struct hlist_head *head;
2843 struct hlist_node *tmp;
2844 struct bnxt_ntuple_filter *fltr;
2845
2846 head = &bp->ntp_fltr_hash_tbl[i];
2847 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2848 hlist_del(&fltr->hash);
2849 kfree(fltr);
2850 }
2851 }
2852 if (irq_reinit) {
2853 kfree(bp->ntp_fltr_bmap);
2854 bp->ntp_fltr_bmap = NULL;
2855 }
2856 bp->ntp_fltr_count = 0;
2857#endif
2858}
2859
2860static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2861{
2862#ifdef CONFIG_RFS_ACCEL
2863 int i, rc = 0;
2864
2865 if (!(bp->flags & BNXT_FLAG_RFS))
2866 return 0;
2867
2868 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2869 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2870
2871 bp->ntp_fltr_count = 0;
2872 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2873 GFP_KERNEL);
2874
2875 if (!bp->ntp_fltr_bmap)
2876 rc = -ENOMEM;
2877
2878 return rc;
2879#else
2880 return 0;
2881#endif
2882}
2883
2884static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2885{
2886 bnxt_free_vnic_attributes(bp);
2887 bnxt_free_tx_rings(bp);
2888 bnxt_free_rx_rings(bp);
2889 bnxt_free_cp_rings(bp);
2890 bnxt_free_ntp_fltrs(bp, irq_re_init);
2891 if (irq_re_init) {
2892 bnxt_free_stats(bp);
2893 bnxt_free_ring_grps(bp);
2894 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002895 kfree(bp->tx_ring);
2896 bp->tx_ring = NULL;
2897 kfree(bp->rx_ring);
2898 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002899 kfree(bp->bnapi);
2900 bp->bnapi = NULL;
2901 } else {
2902 bnxt_clear_ring_indices(bp);
2903 }
2904}
2905
2906static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2907{
Michael Chan01657bc2016-01-02 23:45:03 -05002908 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002909 void *bnapi;
2910
2911 if (irq_re_init) {
2912 /* Allocate bnapi mem pointer array and mem block for
2913 * all queues
2914 */
2915 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2916 bp->cp_nr_rings);
2917 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2918 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2919 if (!bnapi)
2920 return -ENOMEM;
2921
2922 bp->bnapi = bnapi;
2923 bnapi += arr_size;
2924 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2925 bp->bnapi[i] = bnapi;
2926 bp->bnapi[i]->index = i;
2927 bp->bnapi[i]->bp = bp;
2928 }
2929
Michael Chanb6ab4b02016-01-02 23:44:59 -05002930 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2931 sizeof(struct bnxt_rx_ring_info),
2932 GFP_KERNEL);
2933 if (!bp->rx_ring)
2934 return -ENOMEM;
2935
2936 for (i = 0; i < bp->rx_nr_rings; i++) {
2937 bp->rx_ring[i].bnapi = bp->bnapi[i];
2938 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2939 }
2940
2941 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2942 sizeof(struct bnxt_tx_ring_info),
2943 GFP_KERNEL);
2944 if (!bp->tx_ring)
2945 return -ENOMEM;
2946
Michael Chan01657bc2016-01-02 23:45:03 -05002947 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2948 j = 0;
2949 else
2950 j = bp->rx_nr_rings;
2951
2952 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2953 bp->tx_ring[i].bnapi = bp->bnapi[j];
2954 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002955 }
2956
Michael Chanc0c050c2015-10-22 16:01:17 -04002957 rc = bnxt_alloc_stats(bp);
2958 if (rc)
2959 goto alloc_mem_err;
2960
2961 rc = bnxt_alloc_ntp_fltrs(bp);
2962 if (rc)
2963 goto alloc_mem_err;
2964
2965 rc = bnxt_alloc_vnics(bp);
2966 if (rc)
2967 goto alloc_mem_err;
2968 }
2969
2970 bnxt_init_ring_struct(bp);
2971
2972 rc = bnxt_alloc_rx_rings(bp);
2973 if (rc)
2974 goto alloc_mem_err;
2975
2976 rc = bnxt_alloc_tx_rings(bp);
2977 if (rc)
2978 goto alloc_mem_err;
2979
2980 rc = bnxt_alloc_cp_rings(bp);
2981 if (rc)
2982 goto alloc_mem_err;
2983
2984 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2985 BNXT_VNIC_UCAST_FLAG;
2986 rc = bnxt_alloc_vnic_attributes(bp);
2987 if (rc)
2988 goto alloc_mem_err;
2989 return 0;
2990
2991alloc_mem_err:
2992 bnxt_free_mem(bp, true);
2993 return rc;
2994}
2995
2996void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2997 u16 cmpl_ring, u16 target_id)
2998{
Michael Chana8643e12016-02-26 04:00:05 -05002999 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003000
Michael Chana8643e12016-02-26 04:00:05 -05003001 req->req_type = cpu_to_le16(req_type);
3002 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3003 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003004 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3005}
3006
Michael Chanfbfbc482016-02-26 04:00:07 -05003007static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3008 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003009{
Michael Chana11fa2b2016-05-15 03:04:47 -04003010 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003011 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003012 u32 *data = msg;
3013 __le32 *resp_len, *valid;
3014 u16 cp_ring_id, len = 0;
3015 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3016
Michael Chana8643e12016-02-26 04:00:05 -05003017 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003018 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003019 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003020 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3021
3022 /* Write request msg to hwrm channel */
3023 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3024
Michael Chane6ef2692016-03-28 19:46:05 -04003025 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003026 writel(0, bp->bar0 + i);
3027
Michael Chanc0c050c2015-10-22 16:01:17 -04003028 /* currently supports only one outstanding message */
3029 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003030 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003031
3032 /* Ring channel doorbell */
3033 writel(1, bp->bar0 + 0x100);
3034
Michael Chanff4fe812016-02-26 04:00:04 -05003035 if (!timeout)
3036 timeout = DFLT_HWRM_CMD_TIMEOUT;
3037
Michael Chanc0c050c2015-10-22 16:01:17 -04003038 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003039 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003040 if (intr_process) {
3041 /* Wait until hwrm response cmpl interrupt is processed */
3042 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003043 i++ < tmo_count) {
3044 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003045 }
3046
3047 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3048 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003049 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003050 return -1;
3051 }
3052 } else {
3053 /* Check if response len is updated */
3054 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003055 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003056 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3057 HWRM_RESP_LEN_SFT;
3058 if (len)
3059 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003060 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003061 }
3062
Michael Chana11fa2b2016-05-15 03:04:47 -04003063 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003064 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003065 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003066 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003067 return -1;
3068 }
3069
3070 /* Last word of resp contains valid bit */
3071 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003072 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003073 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3074 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003075 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003076 }
3077
Michael Chana11fa2b2016-05-15 03:04:47 -04003078 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003079 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003080 timeout, le16_to_cpu(req->req_type),
3081 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003082 return -1;
3083 }
3084 }
3085
3086 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003087 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003088 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3089 le16_to_cpu(resp->req_type),
3090 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003091 return rc;
3092}
3093
3094int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3095{
3096 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003097}
3098
3099int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3100{
3101 int rc;
3102
3103 mutex_lock(&bp->hwrm_cmd_lock);
3104 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3105 mutex_unlock(&bp->hwrm_cmd_lock);
3106 return rc;
3107}
3108
Michael Chan90e209212016-02-26 04:00:08 -05003109int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3110 int timeout)
3111{
3112 int rc;
3113
3114 mutex_lock(&bp->hwrm_cmd_lock);
3115 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3116 mutex_unlock(&bp->hwrm_cmd_lock);
3117 return rc;
3118}
3119
Michael Chana1653b12016-12-07 00:26:20 -05003120int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3121 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003122{
3123 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003124 DECLARE_BITMAP(async_events_bmap, 256);
3125 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003126 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003127
3128 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3129
3130 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003131 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003132
Michael Chan25be8622016-04-05 14:09:00 -04003133 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3134 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3135 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3136
Michael Chana1653b12016-12-07 00:26:20 -05003137 if (bmap && bmap_size) {
3138 for (i = 0; i < bmap_size; i++) {
3139 if (test_bit(i, bmap))
3140 __set_bit(i, async_events_bmap);
3141 }
3142 }
3143
Michael Chan25be8622016-04-05 14:09:00 -04003144 for (i = 0; i < 8; i++)
3145 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3146
Michael Chana1653b12016-12-07 00:26:20 -05003147 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3148}
3149
3150static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3151{
3152 struct hwrm_func_drv_rgtr_input req = {0};
3153
3154 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3155
3156 req.enables =
3157 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3158 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3159
Michael Chan11f15ed2016-04-05 14:08:55 -04003160 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003161 req.ver_maj = DRV_VER_MAJ;
3162 req.ver_min = DRV_VER_MIN;
3163 req.ver_upd = DRV_VER_UPD;
3164
3165 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003166 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003167 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003168 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003169
Michael Chande68f5de2015-12-09 19:35:41 -05003170 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003171 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3172 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3173
Michael Chande68f5de2015-12-09 19:35:41 -05003174 for (i = 0; i < 8; i++)
3175 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3176
Michael Chanc0c050c2015-10-22 16:01:17 -04003177 req.enables |=
3178 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3179 }
3180
3181 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3182}
3183
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003184static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3185{
3186 struct hwrm_func_drv_unrgtr_input req = {0};
3187
3188 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3189 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3190}
3191
Michael Chanc0c050c2015-10-22 16:01:17 -04003192static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3193{
3194 u32 rc = 0;
3195 struct hwrm_tunnel_dst_port_free_input req = {0};
3196
3197 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3198 req.tunnel_type = tunnel_type;
3199
3200 switch (tunnel_type) {
3201 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3202 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3203 break;
3204 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3205 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3206 break;
3207 default:
3208 break;
3209 }
3210
3211 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3212 if (rc)
3213 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3214 rc);
3215 return rc;
3216}
3217
3218static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3219 u8 tunnel_type)
3220{
3221 u32 rc = 0;
3222 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3223 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3224
3225 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3226
3227 req.tunnel_type = tunnel_type;
3228 req.tunnel_dst_port_val = port;
3229
3230 mutex_lock(&bp->hwrm_cmd_lock);
3231 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3232 if (rc) {
3233 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3234 rc);
3235 goto err_out;
3236 }
3237
Christophe Jaillet57aac712016-11-22 06:14:40 +01003238 switch (tunnel_type) {
3239 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003240 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003241 break;
3242 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003243 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003244 break;
3245 default:
3246 break;
3247 }
3248
Michael Chanc0c050c2015-10-22 16:01:17 -04003249err_out:
3250 mutex_unlock(&bp->hwrm_cmd_lock);
3251 return rc;
3252}
3253
3254static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3255{
3256 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3257 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3258
3259 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003260 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003261
3262 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3263 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3264 req.mask = cpu_to_le32(vnic->rx_mask);
3265 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3266}
3267
3268#ifdef CONFIG_RFS_ACCEL
3269static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3270 struct bnxt_ntuple_filter *fltr)
3271{
3272 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3273
3274 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3275 req.ntuple_filter_id = fltr->filter_id;
3276 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3277}
3278
3279#define BNXT_NTP_FLTR_FLAGS \
3280 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3281 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3282 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3283 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3284 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3285 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3286 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3287 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3288 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3289 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3290 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3291 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3292 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003293 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003294
3295static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3296 struct bnxt_ntuple_filter *fltr)
3297{
3298 int rc = 0;
3299 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3300 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3301 bp->hwrm_cmd_resp_addr;
3302 struct flow_keys *keys = &fltr->fkeys;
3303 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3304
3305 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003306 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003307
3308 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3309
3310 req.ethertype = htons(ETH_P_IP);
3311 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003312 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003313 req.ip_protocol = keys->basic.ip_proto;
3314
3315 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3316 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3317 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3318 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3319
3320 req.src_port = keys->ports.src;
3321 req.src_port_mask = cpu_to_be16(0xffff);
3322 req.dst_port = keys->ports.dst;
3323 req.dst_port_mask = cpu_to_be16(0xffff);
3324
Michael Chanc1935542015-12-27 18:19:28 -05003325 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003326 mutex_lock(&bp->hwrm_cmd_lock);
3327 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3328 if (!rc)
3329 fltr->filter_id = resp->ntuple_filter_id;
3330 mutex_unlock(&bp->hwrm_cmd_lock);
3331 return rc;
3332}
3333#endif
3334
3335static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3336 u8 *mac_addr)
3337{
3338 u32 rc = 0;
3339 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3340 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3341
3342 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003343 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3344 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3345 req.flags |=
3346 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003347 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003348 req.enables =
3349 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003350 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003351 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3352 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3353 req.l2_addr_mask[0] = 0xff;
3354 req.l2_addr_mask[1] = 0xff;
3355 req.l2_addr_mask[2] = 0xff;
3356 req.l2_addr_mask[3] = 0xff;
3357 req.l2_addr_mask[4] = 0xff;
3358 req.l2_addr_mask[5] = 0xff;
3359
3360 mutex_lock(&bp->hwrm_cmd_lock);
3361 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3362 if (!rc)
3363 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3364 resp->l2_filter_id;
3365 mutex_unlock(&bp->hwrm_cmd_lock);
3366 return rc;
3367}
3368
3369static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3370{
3371 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3372 int rc = 0;
3373
3374 /* Any associated ntuple filters will also be cleared by firmware. */
3375 mutex_lock(&bp->hwrm_cmd_lock);
3376 for (i = 0; i < num_of_vnics; i++) {
3377 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3378
3379 for (j = 0; j < vnic->uc_filter_count; j++) {
3380 struct hwrm_cfa_l2_filter_free_input req = {0};
3381
3382 bnxt_hwrm_cmd_hdr_init(bp, &req,
3383 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3384
3385 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3386
3387 rc = _hwrm_send_message(bp, &req, sizeof(req),
3388 HWRM_CMD_TIMEOUT);
3389 }
3390 vnic->uc_filter_count = 0;
3391 }
3392 mutex_unlock(&bp->hwrm_cmd_lock);
3393
3394 return rc;
3395}
3396
3397static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3398{
3399 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3400 struct hwrm_vnic_tpa_cfg_input req = {0};
3401
3402 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3403
3404 if (tpa_flags) {
3405 u16 mss = bp->dev->mtu - 40;
3406 u32 nsegs, n, segs = 0, flags;
3407
3408 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3409 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3410 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3411 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3412 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3413 if (tpa_flags & BNXT_FLAG_GRO)
3414 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3415
3416 req.flags = cpu_to_le32(flags);
3417
3418 req.enables =
3419 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003420 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3421 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003422
3423 /* Number of segs are log2 units, and first packet is not
3424 * included as part of this units.
3425 */
Michael Chan2839f282016-04-25 02:30:50 -04003426 if (mss <= BNXT_RX_PAGE_SIZE) {
3427 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003428 nsegs = (MAX_SKB_FRAGS - 1) * n;
3429 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003430 n = mss / BNXT_RX_PAGE_SIZE;
3431 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003432 n++;
3433 nsegs = (MAX_SKB_FRAGS - n) / n;
3434 }
3435
3436 segs = ilog2(nsegs);
3437 req.max_agg_segs = cpu_to_le16(segs);
3438 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003439
3440 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003441 }
3442 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3443
3444 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3445}
3446
3447static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3448{
3449 u32 i, j, max_rings;
3450 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3451 struct hwrm_vnic_rss_cfg_input req = {0};
3452
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003453 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003454 return 0;
3455
3456 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3457 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003458 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003459 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3460 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3461 max_rings = bp->rx_nr_rings - 1;
3462 else
3463 max_rings = bp->rx_nr_rings;
3464 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003465 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003466 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003467
3468 /* Fill the RSS indirection table with ring group ids */
3469 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3470 if (j == max_rings)
3471 j = 0;
3472 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3473 }
3474
3475 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3476 req.hash_key_tbl_addr =
3477 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3478 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003479 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003480 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3481}
3482
3483static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3484{
3485 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3486 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3487
3488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3489 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3490 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3491 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3492 req.enables =
3493 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3494 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3495 /* thresholds not implemented in firmware yet */
3496 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3497 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3498 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3499 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3500}
3501
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003502static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3503 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003504{
3505 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3506
3507 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3508 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003509 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003510
3511 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003512 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003513}
3514
3515static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3516{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003517 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003518
3519 for (i = 0; i < bp->nr_vnics; i++) {
3520 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3521
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003522 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3523 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3524 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3525 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003526 }
3527 bp->rsscos_nr_ctxs = 0;
3528}
3529
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003530static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003531{
3532 int rc;
3533 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3534 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3535 bp->hwrm_cmd_resp_addr;
3536
3537 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3538 -1);
3539
3540 mutex_lock(&bp->hwrm_cmd_lock);
3541 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3542 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003543 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003544 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3545 mutex_unlock(&bp->hwrm_cmd_lock);
3546
3547 return rc;
3548}
3549
3550static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3551{
Michael Chanb81a90d2016-01-02 23:45:01 -05003552 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003553 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3554 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003555 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003556
3557 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003558
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003559 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3560 /* Only RSS support for now TBD: COS & LB */
3561 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3562 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3563 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3564 VNIC_CFG_REQ_ENABLES_MRU);
3565 } else {
3566 req.rss_rule = cpu_to_le16(0xffff);
3567 }
3568
3569 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3570 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003571 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3572 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3573 } else {
3574 req.cos_rule = cpu_to_le16(0xffff);
3575 }
3576
Michael Chanc0c050c2015-10-22 16:01:17 -04003577 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003578 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003579 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003580 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003581 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3582 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003583
Michael Chanb81a90d2016-01-02 23:45:01 -05003584 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003585 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3586 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3587
3588 req.lb_rule = cpu_to_le16(0xffff);
3589 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3590 VLAN_HLEN);
3591
Michael Chancf6645f2016-06-13 02:25:28 -04003592#ifdef CONFIG_BNXT_SRIOV
3593 if (BNXT_VF(bp))
3594 def_vlan = bp->vf.vlan;
3595#endif
3596 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003597 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3598
3599 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3600}
3601
3602static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3603{
3604 u32 rc = 0;
3605
3606 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3607 struct hwrm_vnic_free_input req = {0};
3608
3609 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3610 req.vnic_id =
3611 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3612
3613 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3614 if (rc)
3615 return rc;
3616 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3617 }
3618 return rc;
3619}
3620
3621static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3622{
3623 u16 i;
3624
3625 for (i = 0; i < bp->nr_vnics; i++)
3626 bnxt_hwrm_vnic_free_one(bp, i);
3627}
3628
Michael Chanb81a90d2016-01-02 23:45:01 -05003629static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3630 unsigned int start_rx_ring_idx,
3631 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003632{
Michael Chanb81a90d2016-01-02 23:45:01 -05003633 int rc = 0;
3634 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003635 struct hwrm_vnic_alloc_input req = {0};
3636 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3637
3638 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003639 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3640 grp_idx = bp->rx_ring[i].bnapi->index;
3641 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003642 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003643 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003644 break;
3645 }
3646 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003647 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003648 }
3649
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003650 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3651 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003652 if (vnic_id == 0)
3653 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3654
3655 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3656
3657 mutex_lock(&bp->hwrm_cmd_lock);
3658 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3659 if (!rc)
3660 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3661 mutex_unlock(&bp->hwrm_cmd_lock);
3662 return rc;
3663}
3664
3665static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3666{
3667 u16 i;
3668 u32 rc = 0;
3669
3670 mutex_lock(&bp->hwrm_cmd_lock);
3671 for (i = 0; i < bp->rx_nr_rings; i++) {
3672 struct hwrm_ring_grp_alloc_input req = {0};
3673 struct hwrm_ring_grp_alloc_output *resp =
3674 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003675 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003676
3677 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3678
Michael Chanb81a90d2016-01-02 23:45:01 -05003679 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3680 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3681 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3682 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003683
3684 rc = _hwrm_send_message(bp, &req, sizeof(req),
3685 HWRM_CMD_TIMEOUT);
3686 if (rc)
3687 break;
3688
Michael Chanb81a90d2016-01-02 23:45:01 -05003689 bp->grp_info[grp_idx].fw_grp_id =
3690 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003691 }
3692 mutex_unlock(&bp->hwrm_cmd_lock);
3693 return rc;
3694}
3695
3696static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3697{
3698 u16 i;
3699 u32 rc = 0;
3700 struct hwrm_ring_grp_free_input req = {0};
3701
3702 if (!bp->grp_info)
3703 return 0;
3704
3705 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3706
3707 mutex_lock(&bp->hwrm_cmd_lock);
3708 for (i = 0; i < bp->cp_nr_rings; i++) {
3709 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3710 continue;
3711 req.ring_group_id =
3712 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3713
3714 rc = _hwrm_send_message(bp, &req, sizeof(req),
3715 HWRM_CMD_TIMEOUT);
3716 if (rc)
3717 break;
3718 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3719 }
3720 mutex_unlock(&bp->hwrm_cmd_lock);
3721 return rc;
3722}
3723
3724static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3725 struct bnxt_ring_struct *ring,
3726 u32 ring_type, u32 map_index,
3727 u32 stats_ctx_id)
3728{
3729 int rc = 0, err = 0;
3730 struct hwrm_ring_alloc_input req = {0};
3731 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3732 u16 ring_id;
3733
3734 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3735
3736 req.enables = 0;
3737 if (ring->nr_pages > 1) {
3738 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3739 /* Page size is in log2 units */
3740 req.page_size = BNXT_PAGE_SHIFT;
3741 req.page_tbl_depth = 1;
3742 } else {
3743 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3744 }
3745 req.fbo = 0;
3746 /* Association of ring index with doorbell index and MSIX number */
3747 req.logical_id = cpu_to_le16(map_index);
3748
3749 switch (ring_type) {
3750 case HWRM_RING_ALLOC_TX:
3751 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3752 /* Association of transmit ring with completion ring */
3753 req.cmpl_ring_id =
3754 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3755 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3756 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3757 req.queue_id = cpu_to_le16(ring->queue_id);
3758 break;
3759 case HWRM_RING_ALLOC_RX:
3760 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3761 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3762 break;
3763 case HWRM_RING_ALLOC_AGG:
3764 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3765 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3766 break;
3767 case HWRM_RING_ALLOC_CMPL:
3768 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3769 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3770 if (bp->flags & BNXT_FLAG_USING_MSIX)
3771 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3772 break;
3773 default:
3774 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3775 ring_type);
3776 return -1;
3777 }
3778
3779 mutex_lock(&bp->hwrm_cmd_lock);
3780 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3781 err = le16_to_cpu(resp->error_code);
3782 ring_id = le16_to_cpu(resp->ring_id);
3783 mutex_unlock(&bp->hwrm_cmd_lock);
3784
3785 if (rc || err) {
3786 switch (ring_type) {
3787 case RING_FREE_REQ_RING_TYPE_CMPL:
3788 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3789 rc, err);
3790 return -1;
3791
3792 case RING_FREE_REQ_RING_TYPE_RX:
3793 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3794 rc, err);
3795 return -1;
3796
3797 case RING_FREE_REQ_RING_TYPE_TX:
3798 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3799 rc, err);
3800 return -1;
3801
3802 default:
3803 netdev_err(bp->dev, "Invalid ring\n");
3804 return -1;
3805 }
3806 }
3807 ring->fw_ring_id = ring_id;
3808 return rc;
3809}
3810
3811static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3812{
3813 int i, rc = 0;
3814
Michael Chanedd0c2c2015-12-27 18:19:19 -05003815 for (i = 0; i < bp->cp_nr_rings; i++) {
3816 struct bnxt_napi *bnapi = bp->bnapi[i];
3817 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3818 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003819
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003820 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003821 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3822 INVALID_STATS_CTX_ID);
3823 if (rc)
3824 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003825 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3826 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003827 }
3828
Michael Chanedd0c2c2015-12-27 18:19:19 -05003829 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003830 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003831 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003832 u32 map_idx = txr->bnapi->index;
3833 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003834
Michael Chanb81a90d2016-01-02 23:45:01 -05003835 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3836 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003837 if (rc)
3838 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003839 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003840 }
3841
Michael Chanedd0c2c2015-12-27 18:19:19 -05003842 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003843 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003844 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003845 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003846
Michael Chanb81a90d2016-01-02 23:45:01 -05003847 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3848 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003849 if (rc)
3850 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003851 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003852 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003853 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003854 }
3855
3856 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3857 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003858 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003859 struct bnxt_ring_struct *ring =
3860 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003861 u32 grp_idx = rxr->bnapi->index;
3862 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003863
3864 rc = hwrm_ring_alloc_send_msg(bp, ring,
3865 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003866 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003867 INVALID_STATS_CTX_ID);
3868 if (rc)
3869 goto err_out;
3870
Michael Chanb81a90d2016-01-02 23:45:01 -05003871 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003872 writel(DB_KEY_RX | rxr->rx_agg_prod,
3873 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003874 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003875 }
3876 }
3877err_out:
3878 return rc;
3879}
3880
3881static int hwrm_ring_free_send_msg(struct bnxt *bp,
3882 struct bnxt_ring_struct *ring,
3883 u32 ring_type, int cmpl_ring_id)
3884{
3885 int rc;
3886 struct hwrm_ring_free_input req = {0};
3887 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3888 u16 error_code;
3889
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003890 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003891 req.ring_type = ring_type;
3892 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3893
3894 mutex_lock(&bp->hwrm_cmd_lock);
3895 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3896 error_code = le16_to_cpu(resp->error_code);
3897 mutex_unlock(&bp->hwrm_cmd_lock);
3898
3899 if (rc || error_code) {
3900 switch (ring_type) {
3901 case RING_FREE_REQ_RING_TYPE_CMPL:
3902 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3903 rc);
3904 return rc;
3905 case RING_FREE_REQ_RING_TYPE_RX:
3906 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3907 rc);
3908 return rc;
3909 case RING_FREE_REQ_RING_TYPE_TX:
3910 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3911 rc);
3912 return rc;
3913 default:
3914 netdev_err(bp->dev, "Invalid ring\n");
3915 return -1;
3916 }
3917 }
3918 return 0;
3919}
3920
Michael Chanedd0c2c2015-12-27 18:19:19 -05003921static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003922{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003923 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003924
3925 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003926 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003927
Michael Chanedd0c2c2015-12-27 18:19:19 -05003928 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003929 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003930 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003931 u32 grp_idx = txr->bnapi->index;
3932 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003933
Michael Chanedd0c2c2015-12-27 18:19:19 -05003934 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3935 hwrm_ring_free_send_msg(bp, ring,
3936 RING_FREE_REQ_RING_TYPE_TX,
3937 close_path ? cmpl_ring_id :
3938 INVALID_HW_RING_ID);
3939 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003940 }
3941 }
3942
Michael Chanedd0c2c2015-12-27 18:19:19 -05003943 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003944 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003945 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003946 u32 grp_idx = rxr->bnapi->index;
3947 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003948
Michael Chanedd0c2c2015-12-27 18:19:19 -05003949 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3950 hwrm_ring_free_send_msg(bp, ring,
3951 RING_FREE_REQ_RING_TYPE_RX,
3952 close_path ? cmpl_ring_id :
3953 INVALID_HW_RING_ID);
3954 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003955 bp->grp_info[grp_idx].rx_fw_ring_id =
3956 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003957 }
3958 }
3959
Michael Chanedd0c2c2015-12-27 18:19:19 -05003960 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003961 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003962 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003963 u32 grp_idx = rxr->bnapi->index;
3964 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003965
Michael Chanedd0c2c2015-12-27 18:19:19 -05003966 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3967 hwrm_ring_free_send_msg(bp, ring,
3968 RING_FREE_REQ_RING_TYPE_RX,
3969 close_path ? cmpl_ring_id :
3970 INVALID_HW_RING_ID);
3971 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003972 bp->grp_info[grp_idx].agg_fw_ring_id =
3973 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003974 }
3975 }
3976
Michael Chanedd0c2c2015-12-27 18:19:19 -05003977 for (i = 0; i < bp->cp_nr_rings; i++) {
3978 struct bnxt_napi *bnapi = bp->bnapi[i];
3979 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3980 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003981
Michael Chanedd0c2c2015-12-27 18:19:19 -05003982 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3983 hwrm_ring_free_send_msg(bp, ring,
3984 RING_FREE_REQ_RING_TYPE_CMPL,
3985 INVALID_HW_RING_ID);
3986 ring->fw_ring_id = INVALID_HW_RING_ID;
3987 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003988 }
3989 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003990}
3991
Michael Chanbb053f52016-02-26 04:00:02 -05003992static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3993 u32 buf_tmrs, u16 flags,
3994 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3995{
3996 req->flags = cpu_to_le16(flags);
3997 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3998 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3999 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4000 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4001 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4002 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4003 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4004 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4005}
4006
Michael Chanc0c050c2015-10-22 16:01:17 -04004007int bnxt_hwrm_set_coal(struct bnxt *bp)
4008{
4009 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004010 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4011 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004012 u16 max_buf, max_buf_irq;
4013 u16 buf_tmr, buf_tmr_irq;
4014 u32 flags;
4015
Michael Chandfc9c942016-02-26 04:00:03 -05004016 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4017 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4018 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4019 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004020
Michael Chandfb5b892016-02-26 04:00:01 -05004021 /* Each rx completion (2 records) should be DMAed immediately.
4022 * DMA 1/4 of the completion buffers at a time.
4023 */
4024 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004025 /* max_buf must not be zero */
4026 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004027 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4028 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4029 /* buf timer set to 1/4 of interrupt timer */
4030 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4031 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4032 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004033
4034 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4035
4036 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4037 * if coal_ticks is less than 25 us.
4038 */
Michael Chandfb5b892016-02-26 04:00:01 -05004039 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004040 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4041
Michael Chanbb053f52016-02-26 04:00:02 -05004042 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004043 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4044
4045 /* max_buf must not be zero */
4046 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4047 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4048 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4049 /* buf timer set to 1/4 of interrupt timer */
4050 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4051 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4052 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4053
4054 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4055 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4056 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004057
4058 mutex_lock(&bp->hwrm_cmd_lock);
4059 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004060 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004061
Michael Chandfc9c942016-02-26 04:00:03 -05004062 req = &req_rx;
4063 if (!bnapi->rx_ring)
4064 req = &req_tx;
4065 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4066
4067 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004068 HWRM_CMD_TIMEOUT);
4069 if (rc)
4070 break;
4071 }
4072 mutex_unlock(&bp->hwrm_cmd_lock);
4073 return rc;
4074}
4075
4076static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4077{
4078 int rc = 0, i;
4079 struct hwrm_stat_ctx_free_input req = {0};
4080
4081 if (!bp->bnapi)
4082 return 0;
4083
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004084 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4085 return 0;
4086
Michael Chanc0c050c2015-10-22 16:01:17 -04004087 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4088
4089 mutex_lock(&bp->hwrm_cmd_lock);
4090 for (i = 0; i < bp->cp_nr_rings; i++) {
4091 struct bnxt_napi *bnapi = bp->bnapi[i];
4092 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4093
4094 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4095 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4096
4097 rc = _hwrm_send_message(bp, &req, sizeof(req),
4098 HWRM_CMD_TIMEOUT);
4099 if (rc)
4100 break;
4101
4102 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4103 }
4104 }
4105 mutex_unlock(&bp->hwrm_cmd_lock);
4106 return rc;
4107}
4108
4109static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4110{
4111 int rc = 0, i;
4112 struct hwrm_stat_ctx_alloc_input req = {0};
4113 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4114
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004115 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4116 return 0;
4117
Michael Chanc0c050c2015-10-22 16:01:17 -04004118 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4119
Michael Chan51f30782016-07-01 18:46:29 -04004120 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004121
4122 mutex_lock(&bp->hwrm_cmd_lock);
4123 for (i = 0; i < bp->cp_nr_rings; i++) {
4124 struct bnxt_napi *bnapi = bp->bnapi[i];
4125 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4126
4127 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4128
4129 rc = _hwrm_send_message(bp, &req, sizeof(req),
4130 HWRM_CMD_TIMEOUT);
4131 if (rc)
4132 break;
4133
4134 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4135
4136 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4137 }
4138 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004139 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004140}
4141
Michael Chancf6645f2016-06-13 02:25:28 -04004142static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4143{
4144 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004145 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004146 int rc;
4147
4148 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4149 req.fid = cpu_to_le16(0xffff);
4150 mutex_lock(&bp->hwrm_cmd_lock);
4151 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4152 if (rc)
4153 goto func_qcfg_exit;
4154
4155#ifdef CONFIG_BNXT_SRIOV
4156 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004157 struct bnxt_vf_info *vf = &bp->vf;
4158
4159 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4160 }
4161#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004162 switch (resp->port_partition_type) {
4163 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4164 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4165 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4166 bp->port_partition_type = resp->port_partition_type;
4167 break;
4168 }
Michael Chancf6645f2016-06-13 02:25:28 -04004169
4170func_qcfg_exit:
4171 mutex_unlock(&bp->hwrm_cmd_lock);
4172 return rc;
4173}
4174
Michael Chan7b08f662016-12-07 00:26:18 -05004175static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004176{
4177 int rc = 0;
4178 struct hwrm_func_qcaps_input req = {0};
4179 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4180
4181 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4182 req.fid = cpu_to_le16(0xffff);
4183
4184 mutex_lock(&bp->hwrm_cmd_lock);
4185 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4186 if (rc)
4187 goto hwrm_func_qcaps_exit;
4188
Michael Chane4060d32016-12-07 00:26:19 -05004189 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4190 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4191 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4192 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4193
Michael Chan7cc5a202016-09-19 03:58:05 -04004194 bp->tx_push_thresh = 0;
4195 if (resp->flags &
4196 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4197 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4198
Michael Chanc0c050c2015-10-22 16:01:17 -04004199 if (BNXT_PF(bp)) {
4200 struct bnxt_pf_info *pf = &bp->pf;
4201
4202 pf->fw_fid = le16_to_cpu(resp->fid);
4203 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004204 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004205 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004206 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004207 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4208 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4209 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004210 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004211 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4212 if (!pf->max_hw_ring_grps)
4213 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004214 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4215 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4216 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4217 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4218 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4219 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4220 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4221 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4222 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4223 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4224 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4225 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004226#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004227 struct bnxt_vf_info *vf = &bp->vf;
4228
4229 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004230
4231 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4232 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4233 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4234 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004235 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4236 if (!vf->max_hw_ring_grps)
4237 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004238 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4239 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4240 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004241
4242 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004243 mutex_unlock(&bp->hwrm_cmd_lock);
4244
4245 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004246 /* overwrite netdev dev_adr with admin VF MAC */
4247 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004248 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004249 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004250 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4251 }
4252 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004253#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004254 }
4255
Michael Chanc0c050c2015-10-22 16:01:17 -04004256hwrm_func_qcaps_exit:
4257 mutex_unlock(&bp->hwrm_cmd_lock);
4258 return rc;
4259}
4260
4261static int bnxt_hwrm_func_reset(struct bnxt *bp)
4262{
4263 struct hwrm_func_reset_input req = {0};
4264
4265 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4266 req.enables = 0;
4267
4268 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4269}
4270
4271static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4272{
4273 int rc = 0;
4274 struct hwrm_queue_qportcfg_input req = {0};
4275 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4276 u8 i, *qptr;
4277
4278 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4279
4280 mutex_lock(&bp->hwrm_cmd_lock);
4281 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4282 if (rc)
4283 goto qportcfg_exit;
4284
4285 if (!resp->max_configurable_queues) {
4286 rc = -EINVAL;
4287 goto qportcfg_exit;
4288 }
4289 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004290 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004291 if (bp->max_tc > BNXT_MAX_QUEUE)
4292 bp->max_tc = BNXT_MAX_QUEUE;
4293
Michael Chan441cabb2016-09-19 03:58:02 -04004294 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4295 bp->max_tc = 1;
4296
Michael Chan87c374d2016-12-02 21:17:16 -05004297 if (bp->max_lltc > bp->max_tc)
4298 bp->max_lltc = bp->max_tc;
4299
Michael Chanc0c050c2015-10-22 16:01:17 -04004300 qptr = &resp->queue_id0;
4301 for (i = 0; i < bp->max_tc; i++) {
4302 bp->q_info[i].queue_id = *qptr++;
4303 bp->q_info[i].queue_profile = *qptr++;
4304 }
4305
4306qportcfg_exit:
4307 mutex_unlock(&bp->hwrm_cmd_lock);
4308 return rc;
4309}
4310
4311static int bnxt_hwrm_ver_get(struct bnxt *bp)
4312{
4313 int rc;
4314 struct hwrm_ver_get_input req = {0};
4315 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4316
Michael Chane6ef2692016-03-28 19:46:05 -04004317 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004318 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4319 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4320 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4321 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4322 mutex_lock(&bp->hwrm_cmd_lock);
4323 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4324 if (rc)
4325 goto hwrm_ver_get_exit;
4326
4327 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4328
Michael Chan11f15ed2016-04-05 14:08:55 -04004329 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4330 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004331 if (resp->hwrm_intf_maj < 1) {
4332 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004333 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004334 resp->hwrm_intf_upd);
4335 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004336 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004337 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004338 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4339 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4340
Michael Chanff4fe812016-02-26 04:00:04 -05004341 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4342 if (!bp->hwrm_cmd_timeout)
4343 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4344
Michael Chane6ef2692016-03-28 19:46:05 -04004345 if (resp->hwrm_intf_maj >= 1)
4346 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4347
Michael Chan659c8052016-06-13 02:25:33 -04004348 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004349 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4350 !resp->chip_metal)
4351 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004352
Michael Chanc0c050c2015-10-22 16:01:17 -04004353hwrm_ver_get_exit:
4354 mutex_unlock(&bp->hwrm_cmd_lock);
4355 return rc;
4356}
4357
Rob Swindell5ac67d82016-09-19 03:58:03 -04004358int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4359{
Rob Swindell878786d2016-09-20 03:36:33 -04004360#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004361 struct hwrm_fw_set_time_input req = {0};
4362 struct rtc_time tm;
4363 struct timeval tv;
4364
4365 if (bp->hwrm_spec_code < 0x10400)
4366 return -EOPNOTSUPP;
4367
4368 do_gettimeofday(&tv);
4369 rtc_time_to_tm(tv.tv_sec, &tm);
4370 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4371 req.year = cpu_to_le16(1900 + tm.tm_year);
4372 req.month = 1 + tm.tm_mon;
4373 req.day = tm.tm_mday;
4374 req.hour = tm.tm_hour;
4375 req.minute = tm.tm_min;
4376 req.second = tm.tm_sec;
4377 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004378#else
4379 return -EOPNOTSUPP;
4380#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004381}
4382
Michael Chan3bdf56c2016-03-07 15:38:45 -05004383static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4384{
4385 int rc;
4386 struct bnxt_pf_info *pf = &bp->pf;
4387 struct hwrm_port_qstats_input req = {0};
4388
4389 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4390 return 0;
4391
4392 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4393 req.port_id = cpu_to_le16(pf->port_id);
4394 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4395 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4396 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4397 return rc;
4398}
4399
Michael Chanc0c050c2015-10-22 16:01:17 -04004400static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4401{
4402 if (bp->vxlan_port_cnt) {
4403 bnxt_hwrm_tunnel_dst_port_free(
4404 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4405 }
4406 bp->vxlan_port_cnt = 0;
4407 if (bp->nge_port_cnt) {
4408 bnxt_hwrm_tunnel_dst_port_free(
4409 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4410 }
4411 bp->nge_port_cnt = 0;
4412}
4413
4414static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4415{
4416 int rc, i;
4417 u32 tpa_flags = 0;
4418
4419 if (set_tpa)
4420 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4421 for (i = 0; i < bp->nr_vnics; i++) {
4422 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4423 if (rc) {
4424 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4425 rc, i);
4426 return rc;
4427 }
4428 }
4429 return 0;
4430}
4431
4432static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4433{
4434 int i;
4435
4436 for (i = 0; i < bp->nr_vnics; i++)
4437 bnxt_hwrm_vnic_set_rss(bp, i, false);
4438}
4439
4440static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4441 bool irq_re_init)
4442{
4443 if (bp->vnic_info) {
4444 bnxt_hwrm_clear_vnic_filter(bp);
4445 /* clear all RSS setting before free vnic ctx */
4446 bnxt_hwrm_clear_vnic_rss(bp);
4447 bnxt_hwrm_vnic_ctx_free(bp);
4448 /* before free the vnic, undo the vnic tpa settings */
4449 if (bp->flags & BNXT_FLAG_TPA)
4450 bnxt_set_tpa(bp, false);
4451 bnxt_hwrm_vnic_free(bp);
4452 }
4453 bnxt_hwrm_ring_free(bp, close_path);
4454 bnxt_hwrm_ring_grp_free(bp);
4455 if (irq_re_init) {
4456 bnxt_hwrm_stat_ctx_free(bp);
4457 bnxt_hwrm_free_tunnel_ports(bp);
4458 }
4459}
4460
4461static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4462{
4463 int rc;
4464
4465 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004466 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004467 if (rc) {
4468 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4469 vnic_id, rc);
4470 goto vnic_setup_err;
4471 }
4472 bp->rsscos_nr_ctxs++;
4473
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004474 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4475 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4476 if (rc) {
4477 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4478 vnic_id, rc);
4479 goto vnic_setup_err;
4480 }
4481 bp->rsscos_nr_ctxs++;
4482 }
4483
Michael Chanc0c050c2015-10-22 16:01:17 -04004484 /* configure default vnic, ring grp */
4485 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4486 if (rc) {
4487 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4488 vnic_id, rc);
4489 goto vnic_setup_err;
4490 }
4491
4492 /* Enable RSS hashing on vnic */
4493 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4494 if (rc) {
4495 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4496 vnic_id, rc);
4497 goto vnic_setup_err;
4498 }
4499
4500 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4501 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4502 if (rc) {
4503 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4504 vnic_id, rc);
4505 }
4506 }
4507
4508vnic_setup_err:
4509 return rc;
4510}
4511
4512static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4513{
4514#ifdef CONFIG_RFS_ACCEL
4515 int i, rc = 0;
4516
4517 for (i = 0; i < bp->rx_nr_rings; i++) {
4518 u16 vnic_id = i + 1;
4519 u16 ring_id = i;
4520
4521 if (vnic_id >= bp->nr_vnics)
4522 break;
4523
4524 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004525 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004526 if (rc) {
4527 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4528 vnic_id, rc);
4529 break;
4530 }
4531 rc = bnxt_setup_vnic(bp, vnic_id);
4532 if (rc)
4533 break;
4534 }
4535 return rc;
4536#else
4537 return 0;
4538#endif
4539}
4540
Michael Chan17c71ac2016-07-01 18:46:27 -04004541/* Allow PF and VF with default VLAN to be in promiscuous mode */
4542static bool bnxt_promisc_ok(struct bnxt *bp)
4543{
4544#ifdef CONFIG_BNXT_SRIOV
4545 if (BNXT_VF(bp) && !bp->vf.vlan)
4546 return false;
4547#endif
4548 return true;
4549}
4550
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004551static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4552{
4553 unsigned int rc = 0;
4554
4555 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4556 if (rc) {
4557 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4558 rc);
4559 return rc;
4560 }
4561
4562 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4563 if (rc) {
4564 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4565 rc);
4566 return rc;
4567 }
4568 return rc;
4569}
4570
Michael Chanb664f002015-12-02 01:54:08 -05004571static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004572static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004573
Michael Chanc0c050c2015-10-22 16:01:17 -04004574static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4575{
Michael Chan7d2837d2016-05-04 16:56:44 -04004576 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004577 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004578 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004579
4580 if (irq_re_init) {
4581 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4582 if (rc) {
4583 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4584 rc);
4585 goto err_out;
4586 }
4587 }
4588
4589 rc = bnxt_hwrm_ring_alloc(bp);
4590 if (rc) {
4591 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4592 goto err_out;
4593 }
4594
4595 rc = bnxt_hwrm_ring_grp_alloc(bp);
4596 if (rc) {
4597 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4598 goto err_out;
4599 }
4600
Prashant Sreedharan76595192016-07-18 07:15:22 -04004601 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4602 rx_nr_rings--;
4603
Michael Chanc0c050c2015-10-22 16:01:17 -04004604 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004605 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004606 if (rc) {
4607 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4608 goto err_out;
4609 }
4610
4611 rc = bnxt_setup_vnic(bp, 0);
4612 if (rc)
4613 goto err_out;
4614
4615 if (bp->flags & BNXT_FLAG_RFS) {
4616 rc = bnxt_alloc_rfs_vnics(bp);
4617 if (rc)
4618 goto err_out;
4619 }
4620
4621 if (bp->flags & BNXT_FLAG_TPA) {
4622 rc = bnxt_set_tpa(bp, true);
4623 if (rc)
4624 goto err_out;
4625 }
4626
4627 if (BNXT_VF(bp))
4628 bnxt_update_vf_mac(bp);
4629
4630 /* Filter for default vnic 0 */
4631 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4632 if (rc) {
4633 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4634 goto err_out;
4635 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004636 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004637
Michael Chan7d2837d2016-05-04 16:56:44 -04004638 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004639
Michael Chan17c71ac2016-07-01 18:46:27 -04004640 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004641 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4642
4643 if (bp->dev->flags & IFF_ALLMULTI) {
4644 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4645 vnic->mc_list_count = 0;
4646 } else {
4647 u32 mask = 0;
4648
4649 bnxt_mc_list_updated(bp, &mask);
4650 vnic->rx_mask |= mask;
4651 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004652
Michael Chanb664f002015-12-02 01:54:08 -05004653 rc = bnxt_cfg_rx_mode(bp);
4654 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004655 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004656
4657 rc = bnxt_hwrm_set_coal(bp);
4658 if (rc)
4659 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004660 rc);
4661
4662 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4663 rc = bnxt_setup_nitroa0_vnic(bp);
4664 if (rc)
4665 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4666 rc);
4667 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004668
Michael Chancf6645f2016-06-13 02:25:28 -04004669 if (BNXT_VF(bp)) {
4670 bnxt_hwrm_func_qcfg(bp);
4671 netdev_update_features(bp->dev);
4672 }
4673
Michael Chanc0c050c2015-10-22 16:01:17 -04004674 return 0;
4675
4676err_out:
4677 bnxt_hwrm_resource_free(bp, 0, true);
4678
4679 return rc;
4680}
4681
4682static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4683{
4684 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4685 return 0;
4686}
4687
4688static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4689{
4690 bnxt_init_rx_rings(bp);
4691 bnxt_init_tx_rings(bp);
4692 bnxt_init_ring_grps(bp, irq_re_init);
4693 bnxt_init_vnics(bp);
4694
4695 return bnxt_init_chip(bp, irq_re_init);
4696}
4697
4698static void bnxt_disable_int(struct bnxt *bp)
4699{
4700 int i;
4701
4702 if (!bp->bnapi)
4703 return;
4704
4705 for (i = 0; i < bp->cp_nr_rings; i++) {
4706 struct bnxt_napi *bnapi = bp->bnapi[i];
4707 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4708
4709 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4710 }
4711}
4712
4713static void bnxt_enable_int(struct bnxt *bp)
4714{
4715 int i;
4716
4717 atomic_set(&bp->intr_sem, 0);
4718 for (i = 0; i < bp->cp_nr_rings; i++) {
4719 struct bnxt_napi *bnapi = bp->bnapi[i];
4720 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4721
4722 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4723 }
4724}
4725
4726static int bnxt_set_real_num_queues(struct bnxt *bp)
4727{
4728 int rc;
4729 struct net_device *dev = bp->dev;
4730
4731 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4732 if (rc)
4733 return rc;
4734
4735 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4736 if (rc)
4737 return rc;
4738
4739#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004740 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004741 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004742#endif
4743
4744 return rc;
4745}
4746
Michael Chan6e6c5a52016-01-02 23:45:02 -05004747static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4748 bool shared)
4749{
4750 int _rx = *rx, _tx = *tx;
4751
4752 if (shared) {
4753 *rx = min_t(int, _rx, max);
4754 *tx = min_t(int, _tx, max);
4755 } else {
4756 if (max < 2)
4757 return -ENOMEM;
4758
4759 while (_rx + _tx > max) {
4760 if (_rx > _tx && _rx > 1)
4761 _rx--;
4762 else if (_tx > 1)
4763 _tx--;
4764 }
4765 *rx = _rx;
4766 *tx = _tx;
4767 }
4768 return 0;
4769}
4770
Michael Chan78095922016-12-07 00:26:16 -05004771static void bnxt_setup_msix(struct bnxt *bp)
4772{
4773 const int len = sizeof(bp->irq_tbl[0].name);
4774 struct net_device *dev = bp->dev;
4775 int tcs, i;
4776
4777 tcs = netdev_get_num_tc(dev);
4778 if (tcs > 1) {
4779 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4780 if (bp->tx_nr_rings_per_tc == 0) {
4781 netdev_reset_tc(dev);
4782 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4783 } else {
4784 int i, off, count;
4785
4786 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4787 for (i = 0; i < tcs; i++) {
4788 count = bp->tx_nr_rings_per_tc;
4789 off = i * count;
4790 netdev_set_tc_queue(dev, i, count, off);
4791 }
4792 }
4793 }
4794
4795 for (i = 0; i < bp->cp_nr_rings; i++) {
4796 char *attr;
4797
4798 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4799 attr = "TxRx";
4800 else if (i < bp->rx_nr_rings)
4801 attr = "rx";
4802 else
4803 attr = "tx";
4804
4805 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
4806 i);
4807 bp->irq_tbl[i].handler = bnxt_msix;
4808 }
4809}
4810
4811static void bnxt_setup_inta(struct bnxt *bp)
4812{
4813 const int len = sizeof(bp->irq_tbl[0].name);
4814
4815 if (netdev_get_num_tc(bp->dev))
4816 netdev_reset_tc(bp->dev);
4817
4818 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
4819 0);
4820 bp->irq_tbl[0].handler = bnxt_inta;
4821}
4822
4823static int bnxt_setup_int_mode(struct bnxt *bp)
4824{
4825 int rc;
4826
4827 if (bp->flags & BNXT_FLAG_USING_MSIX)
4828 bnxt_setup_msix(bp);
4829 else
4830 bnxt_setup_inta(bp);
4831
4832 rc = bnxt_set_real_num_queues(bp);
4833 return rc;
4834}
4835
Michael Chane4060d32016-12-07 00:26:19 -05004836unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
4837{
4838#if defined(CONFIG_BNXT_SRIOV)
4839 if (BNXT_VF(bp))
4840 return bp->vf.max_stat_ctxs;
4841#endif
4842 return bp->pf.max_stat_ctxs;
4843}
4844
4845unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
4846{
4847#if defined(CONFIG_BNXT_SRIOV)
4848 if (BNXT_VF(bp))
4849 return bp->vf.max_cp_rings;
4850#endif
4851 return bp->pf.max_cp_rings;
4852}
4853
Michael Chan78095922016-12-07 00:26:16 -05004854static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
4855{
4856#if defined(CONFIG_BNXT_SRIOV)
4857 if (BNXT_VF(bp))
4858 return bp->vf.max_irqs;
4859#endif
4860 return bp->pf.max_irqs;
4861}
4862
Michael Chan33c26572016-12-07 00:26:15 -05004863void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
4864{
4865#if defined(CONFIG_BNXT_SRIOV)
4866 if (BNXT_VF(bp))
4867 bp->vf.max_irqs = max_irqs;
4868 else
4869#endif
4870 bp->pf.max_irqs = max_irqs;
4871}
4872
Michael Chan78095922016-12-07 00:26:16 -05004873static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004874{
Michael Chan01657bc2016-01-02 23:45:03 -05004875 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05004876 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04004877
Michael Chan78095922016-12-07 00:26:16 -05004878 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004879 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4880 if (!msix_ent)
4881 return -ENOMEM;
4882
4883 for (i = 0; i < total_vecs; i++) {
4884 msix_ent[i].entry = i;
4885 msix_ent[i].vector = 0;
4886 }
4887
Michael Chan01657bc2016-01-02 23:45:03 -05004888 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4889 min = 2;
4890
4891 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004892 if (total_vecs < 0) {
4893 rc = -ENODEV;
4894 goto msix_setup_exit;
4895 }
4896
4897 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4898 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05004899 for (i = 0; i < total_vecs; i++)
4900 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04004901
Michael Chan78095922016-12-07 00:26:16 -05004902 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004903 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004904 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004905 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004906 if (rc)
4907 goto msix_setup_exit;
4908
Michael Chanc0c050c2015-10-22 16:01:17 -04004909 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05004910 bp->cp_nr_rings = (min == 1) ?
4911 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
4912 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004913
Michael Chanc0c050c2015-10-22 16:01:17 -04004914 } else {
4915 rc = -ENOMEM;
4916 goto msix_setup_exit;
4917 }
4918 bp->flags |= BNXT_FLAG_USING_MSIX;
4919 kfree(msix_ent);
4920 return 0;
4921
4922msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05004923 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
4924 kfree(bp->irq_tbl);
4925 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004926 pci_disable_msix(bp->pdev);
4927 kfree(msix_ent);
4928 return rc;
4929}
4930
Michael Chan78095922016-12-07 00:26:16 -05004931static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004932{
Michael Chanc0c050c2015-10-22 16:01:17 -04004933 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05004934 if (!bp->irq_tbl)
4935 return -ENOMEM;
4936
4937 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004938 bp->rx_nr_rings = 1;
4939 bp->tx_nr_rings = 1;
4940 bp->cp_nr_rings = 1;
4941 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004942 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004943 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05004944 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004945}
4946
Michael Chan78095922016-12-07 00:26:16 -05004947static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004948{
4949 int rc = 0;
4950
4951 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05004952 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004953
Michael Chan1fa72e22016-04-25 02:30:49 -04004954 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004955 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05004956 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004957 }
4958 return rc;
4959}
4960
Michael Chan78095922016-12-07 00:26:16 -05004961static void bnxt_clear_int_mode(struct bnxt *bp)
4962{
4963 if (bp->flags & BNXT_FLAG_USING_MSIX)
4964 pci_disable_msix(bp->pdev);
4965
4966 kfree(bp->irq_tbl);
4967 bp->irq_tbl = NULL;
4968 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4969}
4970
Michael Chanc0c050c2015-10-22 16:01:17 -04004971static void bnxt_free_irq(struct bnxt *bp)
4972{
4973 struct bnxt_irq *irq;
4974 int i;
4975
4976#ifdef CONFIG_RFS_ACCEL
4977 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4978 bp->dev->rx_cpu_rmap = NULL;
4979#endif
4980 if (!bp->irq_tbl)
4981 return;
4982
4983 for (i = 0; i < bp->cp_nr_rings; i++) {
4984 irq = &bp->irq_tbl[i];
4985 if (irq->requested)
4986 free_irq(irq->vector, bp->bnapi[i]);
4987 irq->requested = 0;
4988 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004989}
4990
4991static int bnxt_request_irq(struct bnxt *bp)
4992{
Michael Chanb81a90d2016-01-02 23:45:01 -05004993 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004994 unsigned long flags = 0;
4995#ifdef CONFIG_RFS_ACCEL
4996 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4997#endif
4998
4999 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5000 flags = IRQF_SHARED;
5001
Michael Chanb81a90d2016-01-02 23:45:01 -05005002 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005003 struct bnxt_irq *irq = &bp->irq_tbl[i];
5004#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005005 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005006 rc = irq_cpu_rmap_add(rmap, irq->vector);
5007 if (rc)
5008 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005009 j);
5010 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005011 }
5012#endif
5013 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5014 bp->bnapi[i]);
5015 if (rc)
5016 break;
5017
5018 irq->requested = 1;
5019 }
5020 return rc;
5021}
5022
5023static void bnxt_del_napi(struct bnxt *bp)
5024{
5025 int i;
5026
5027 if (!bp->bnapi)
5028 return;
5029
5030 for (i = 0; i < bp->cp_nr_rings; i++) {
5031 struct bnxt_napi *bnapi = bp->bnapi[i];
5032
5033 napi_hash_del(&bnapi->napi);
5034 netif_napi_del(&bnapi->napi);
5035 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005036 /* We called napi_hash_del() before netif_napi_del(), we need
5037 * to respect an RCU grace period before freeing napi structures.
5038 */
5039 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005040}
5041
5042static void bnxt_init_napi(struct bnxt *bp)
5043{
5044 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005045 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005046 struct bnxt_napi *bnapi;
5047
5048 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005049 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5050 cp_nr_rings--;
5051 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005052 bnapi = bp->bnapi[i];
5053 netif_napi_add(bp->dev, &bnapi->napi,
5054 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005055 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005056 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5057 bnapi = bp->bnapi[cp_nr_rings];
5058 netif_napi_add(bp->dev, &bnapi->napi,
5059 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005060 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005061 } else {
5062 bnapi = bp->bnapi[0];
5063 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005064 }
5065}
5066
5067static void bnxt_disable_napi(struct bnxt *bp)
5068{
5069 int i;
5070
5071 if (!bp->bnapi)
5072 return;
5073
5074 for (i = 0; i < bp->cp_nr_rings; i++) {
5075 napi_disable(&bp->bnapi[i]->napi);
5076 bnxt_disable_poll(bp->bnapi[i]);
5077 }
5078}
5079
5080static void bnxt_enable_napi(struct bnxt *bp)
5081{
5082 int i;
5083
5084 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005085 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005086 bnxt_enable_poll(bp->bnapi[i]);
5087 napi_enable(&bp->bnapi[i]->napi);
5088 }
5089}
5090
Michael Chan7df4ae92016-12-02 21:17:17 -05005091void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005092{
5093 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005094 struct bnxt_tx_ring_info *txr;
5095 struct netdev_queue *txq;
5096
Michael Chanb6ab4b02016-01-02 23:44:59 -05005097 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005098 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005099 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005100 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005101 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005102 }
5103 }
5104 /* Stop all TX queues */
5105 netif_tx_disable(bp->dev);
5106 netif_carrier_off(bp->dev);
5107}
5108
Michael Chan7df4ae92016-12-02 21:17:17 -05005109void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005110{
5111 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005112 struct bnxt_tx_ring_info *txr;
5113 struct netdev_queue *txq;
5114
5115 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005116 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005117 txq = netdev_get_tx_queue(bp->dev, i);
5118 txr->dev_state = 0;
5119 }
5120 netif_tx_wake_all_queues(bp->dev);
5121 if (bp->link_info.link_up)
5122 netif_carrier_on(bp->dev);
5123}
5124
5125static void bnxt_report_link(struct bnxt *bp)
5126{
5127 if (bp->link_info.link_up) {
5128 const char *duplex;
5129 const char *flow_ctrl;
5130 u16 speed;
5131
5132 netif_carrier_on(bp->dev);
5133 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5134 duplex = "full";
5135 else
5136 duplex = "half";
5137 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5138 flow_ctrl = "ON - receive & transmit";
5139 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5140 flow_ctrl = "ON - transmit";
5141 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5142 flow_ctrl = "ON - receive";
5143 else
5144 flow_ctrl = "none";
5145 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5146 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5147 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005148 if (bp->flags & BNXT_FLAG_EEE_CAP)
5149 netdev_info(bp->dev, "EEE is %s\n",
5150 bp->eee.eee_active ? "active" :
5151 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005152 } else {
5153 netif_carrier_off(bp->dev);
5154 netdev_err(bp->dev, "NIC Link is Down\n");
5155 }
5156}
5157
Michael Chan170ce012016-04-05 14:08:57 -04005158static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5159{
5160 int rc = 0;
5161 struct hwrm_port_phy_qcaps_input req = {0};
5162 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005163 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005164
5165 if (bp->hwrm_spec_code < 0x10201)
5166 return 0;
5167
5168 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5169
5170 mutex_lock(&bp->hwrm_cmd_lock);
5171 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5172 if (rc)
5173 goto hwrm_phy_qcaps_exit;
5174
5175 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5176 struct ethtool_eee *eee = &bp->eee;
5177 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5178
5179 bp->flags |= BNXT_FLAG_EEE_CAP;
5180 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5181 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5182 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5183 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5184 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5185 }
Michael Chan93ed8112016-06-13 02:25:37 -04005186 link_info->support_auto_speeds =
5187 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005188
5189hwrm_phy_qcaps_exit:
5190 mutex_unlock(&bp->hwrm_cmd_lock);
5191 return rc;
5192}
5193
Michael Chanc0c050c2015-10-22 16:01:17 -04005194static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5195{
5196 int rc = 0;
5197 struct bnxt_link_info *link_info = &bp->link_info;
5198 struct hwrm_port_phy_qcfg_input req = {0};
5199 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5200 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005201 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005202
5203 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5204
5205 mutex_lock(&bp->hwrm_cmd_lock);
5206 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5207 if (rc) {
5208 mutex_unlock(&bp->hwrm_cmd_lock);
5209 return rc;
5210 }
5211
5212 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5213 link_info->phy_link_status = resp->link;
5214 link_info->duplex = resp->duplex;
5215 link_info->pause = resp->pause;
5216 link_info->auto_mode = resp->auto_mode;
5217 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005218 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005219 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005220 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005221 if (link_info->phy_link_status == BNXT_LINK_LINK)
5222 link_info->link_speed = le16_to_cpu(resp->link_speed);
5223 else
5224 link_info->link_speed = 0;
5225 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005226 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5227 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005228 link_info->lp_auto_link_speeds =
5229 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005230 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5231 link_info->phy_ver[0] = resp->phy_maj;
5232 link_info->phy_ver[1] = resp->phy_min;
5233 link_info->phy_ver[2] = resp->phy_bld;
5234 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005235 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005236 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005237 link_info->phy_addr = resp->eee_config_phy_addr &
5238 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005239 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005240
Michael Chan170ce012016-04-05 14:08:57 -04005241 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5242 struct ethtool_eee *eee = &bp->eee;
5243 u16 fw_speeds;
5244
5245 eee->eee_active = 0;
5246 if (resp->eee_config_phy_addr &
5247 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5248 eee->eee_active = 1;
5249 fw_speeds = le16_to_cpu(
5250 resp->link_partner_adv_eee_link_speed_mask);
5251 eee->lp_advertised =
5252 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5253 }
5254
5255 /* Pull initial EEE config */
5256 if (!chng_link_state) {
5257 if (resp->eee_config_phy_addr &
5258 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5259 eee->eee_enabled = 1;
5260
5261 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5262 eee->advertised =
5263 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5264
5265 if (resp->eee_config_phy_addr &
5266 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5267 __le32 tmr;
5268
5269 eee->tx_lpi_enabled = 1;
5270 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5271 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5272 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5273 }
5274 }
5275 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005276 /* TODO: need to add more logic to report VF link */
5277 if (chng_link_state) {
5278 if (link_info->phy_link_status == BNXT_LINK_LINK)
5279 link_info->link_up = 1;
5280 else
5281 link_info->link_up = 0;
5282 if (link_up != link_info->link_up)
5283 bnxt_report_link(bp);
5284 } else {
5285 /* alwasy link down if not require to update link state */
5286 link_info->link_up = 0;
5287 }
5288 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005289
5290 diff = link_info->support_auto_speeds ^ link_info->advertising;
5291 if ((link_info->support_auto_speeds | diff) !=
5292 link_info->support_auto_speeds) {
5293 /* An advertised speed is no longer supported, so we need to
5294 * update the advertisement settings. See bnxt_reset() for
5295 * comments about the rtnl_lock() sequence below.
5296 */
5297 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5298 rtnl_lock();
5299 link_info->advertising = link_info->support_auto_speeds;
5300 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
5301 (link_info->autoneg & BNXT_AUTONEG_SPEED))
5302 bnxt_hwrm_set_link_setting(bp, true, false);
5303 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5304 rtnl_unlock();
5305 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005306 return 0;
5307}
5308
Michael Chan10289be2016-05-15 03:04:49 -04005309static void bnxt_get_port_module_status(struct bnxt *bp)
5310{
5311 struct bnxt_link_info *link_info = &bp->link_info;
5312 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5313 u8 module_status;
5314
5315 if (bnxt_update_link(bp, true))
5316 return;
5317
5318 module_status = link_info->module_status;
5319 switch (module_status) {
5320 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5321 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5322 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5323 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5324 bp->pf.port_id);
5325 if (bp->hwrm_spec_code >= 0x10201) {
5326 netdev_warn(bp->dev, "Module part number %s\n",
5327 resp->phy_vendor_partnumber);
5328 }
5329 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5330 netdev_warn(bp->dev, "TX is disabled\n");
5331 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5332 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5333 }
5334}
5335
Michael Chanc0c050c2015-10-22 16:01:17 -04005336static void
5337bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5338{
5339 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005340 if (bp->hwrm_spec_code >= 0x10201)
5341 req->auto_pause =
5342 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005343 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5344 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5345 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005346 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005347 req->enables |=
5348 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5349 } else {
5350 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5351 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5352 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5353 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5354 req->enables |=
5355 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005356 if (bp->hwrm_spec_code >= 0x10201) {
5357 req->auto_pause = req->force_pause;
5358 req->enables |= cpu_to_le32(
5359 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5360 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005361 }
5362}
5363
5364static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5365 struct hwrm_port_phy_cfg_input *req)
5366{
5367 u8 autoneg = bp->link_info.autoneg;
5368 u16 fw_link_speed = bp->link_info.req_link_speed;
5369 u32 advertising = bp->link_info.advertising;
5370
5371 if (autoneg & BNXT_AUTONEG_SPEED) {
5372 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005373 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005374
5375 req->enables |= cpu_to_le32(
5376 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5377 req->auto_link_speed_mask = cpu_to_le16(advertising);
5378
5379 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5380 req->flags |=
5381 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5382 } else {
5383 req->force_link_speed = cpu_to_le16(fw_link_speed);
5384 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5385 }
5386
Michael Chanc0c050c2015-10-22 16:01:17 -04005387 /* tell chimp that the setting takes effect immediately */
5388 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5389}
5390
5391int bnxt_hwrm_set_pause(struct bnxt *bp)
5392{
5393 struct hwrm_port_phy_cfg_input req = {0};
5394 int rc;
5395
5396 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5397 bnxt_hwrm_set_pause_common(bp, &req);
5398
5399 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5400 bp->link_info.force_link_chng)
5401 bnxt_hwrm_set_link_common(bp, &req);
5402
5403 mutex_lock(&bp->hwrm_cmd_lock);
5404 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5405 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5406 /* since changing of pause setting doesn't trigger any link
5407 * change event, the driver needs to update the current pause
5408 * result upon successfully return of the phy_cfg command
5409 */
5410 bp->link_info.pause =
5411 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5412 bp->link_info.auto_pause_setting = 0;
5413 if (!bp->link_info.force_link_chng)
5414 bnxt_report_link(bp);
5415 }
5416 bp->link_info.force_link_chng = false;
5417 mutex_unlock(&bp->hwrm_cmd_lock);
5418 return rc;
5419}
5420
Michael Chan939f7f02016-04-05 14:08:58 -04005421static void bnxt_hwrm_set_eee(struct bnxt *bp,
5422 struct hwrm_port_phy_cfg_input *req)
5423{
5424 struct ethtool_eee *eee = &bp->eee;
5425
5426 if (eee->eee_enabled) {
5427 u16 eee_speeds;
5428 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5429
5430 if (eee->tx_lpi_enabled)
5431 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5432 else
5433 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5434
5435 req->flags |= cpu_to_le32(flags);
5436 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5437 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5438 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5439 } else {
5440 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5441 }
5442}
5443
5444int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005445{
5446 struct hwrm_port_phy_cfg_input req = {0};
5447
5448 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5449 if (set_pause)
5450 bnxt_hwrm_set_pause_common(bp, &req);
5451
5452 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005453
5454 if (set_eee)
5455 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005456 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5457}
5458
Michael Chan33f7d552016-04-11 04:11:12 -04005459static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5460{
5461 struct hwrm_port_phy_cfg_input req = {0};
5462
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005463 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005464 return 0;
5465
5466 if (pci_num_vf(bp->pdev))
5467 return 0;
5468
5469 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005470 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005471 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5472}
5473
Michael Chan939f7f02016-04-05 14:08:58 -04005474static bool bnxt_eee_config_ok(struct bnxt *bp)
5475{
5476 struct ethtool_eee *eee = &bp->eee;
5477 struct bnxt_link_info *link_info = &bp->link_info;
5478
5479 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5480 return true;
5481
5482 if (eee->eee_enabled) {
5483 u32 advertising =
5484 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5485
5486 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5487 eee->eee_enabled = 0;
5488 return false;
5489 }
5490 if (eee->advertised & ~advertising) {
5491 eee->advertised = advertising & eee->supported;
5492 return false;
5493 }
5494 }
5495 return true;
5496}
5497
Michael Chanc0c050c2015-10-22 16:01:17 -04005498static int bnxt_update_phy_setting(struct bnxt *bp)
5499{
5500 int rc;
5501 bool update_link = false;
5502 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005503 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005504 struct bnxt_link_info *link_info = &bp->link_info;
5505
5506 rc = bnxt_update_link(bp, true);
5507 if (rc) {
5508 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5509 rc);
5510 return rc;
5511 }
5512 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005513 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5514 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005515 update_pause = true;
5516 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5517 link_info->force_pause_setting != link_info->req_flow_ctrl)
5518 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005519 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5520 if (BNXT_AUTO_MODE(link_info->auto_mode))
5521 update_link = true;
5522 if (link_info->req_link_speed != link_info->force_link_speed)
5523 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005524 if (link_info->req_duplex != link_info->duplex_setting)
5525 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005526 } else {
5527 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5528 update_link = true;
5529 if (link_info->advertising != link_info->auto_link_speeds)
5530 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005531 }
5532
Michael Chan16d663a2016-11-16 21:13:07 -05005533 /* The last close may have shutdown the link, so need to call
5534 * PHY_CFG to bring it back up.
5535 */
5536 if (!netif_carrier_ok(bp->dev))
5537 update_link = true;
5538
Michael Chan939f7f02016-04-05 14:08:58 -04005539 if (!bnxt_eee_config_ok(bp))
5540 update_eee = true;
5541
Michael Chanc0c050c2015-10-22 16:01:17 -04005542 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005543 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005544 else if (update_pause)
5545 rc = bnxt_hwrm_set_pause(bp);
5546 if (rc) {
5547 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5548 rc);
5549 return rc;
5550 }
5551
5552 return rc;
5553}
5554
Jeffrey Huang11809492015-11-05 16:25:49 -05005555/* Common routine to pre-map certain register block to different GRC window.
5556 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5557 * in PF and 3 windows in VF that can be customized to map in different
5558 * register blocks.
5559 */
5560static void bnxt_preset_reg_win(struct bnxt *bp)
5561{
5562 if (BNXT_PF(bp)) {
5563 /* CAG registers map to GRC window #4 */
5564 writel(BNXT_CAG_REG_BASE,
5565 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5566 }
5567}
5568
Michael Chanc0c050c2015-10-22 16:01:17 -04005569static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5570{
5571 int rc = 0;
5572
Jeffrey Huang11809492015-11-05 16:25:49 -05005573 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005574 netif_carrier_off(bp->dev);
5575 if (irq_re_init) {
5576 rc = bnxt_setup_int_mode(bp);
5577 if (rc) {
5578 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5579 rc);
5580 return rc;
5581 }
5582 }
5583 if ((bp->flags & BNXT_FLAG_RFS) &&
5584 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5585 /* disable RFS if falling back to INTA */
5586 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5587 bp->flags &= ~BNXT_FLAG_RFS;
5588 }
5589
5590 rc = bnxt_alloc_mem(bp, irq_re_init);
5591 if (rc) {
5592 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5593 goto open_err_free_mem;
5594 }
5595
5596 if (irq_re_init) {
5597 bnxt_init_napi(bp);
5598 rc = bnxt_request_irq(bp);
5599 if (rc) {
5600 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5601 goto open_err;
5602 }
5603 }
5604
5605 bnxt_enable_napi(bp);
5606
5607 rc = bnxt_init_nic(bp, irq_re_init);
5608 if (rc) {
5609 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5610 goto open_err;
5611 }
5612
5613 if (link_re_init) {
5614 rc = bnxt_update_phy_setting(bp);
5615 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005616 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005617 }
5618
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005619 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005620 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005621
Michael Chancaefe522015-12-09 19:35:42 -05005622 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005623 bnxt_enable_int(bp);
5624 /* Enable TX queues */
5625 bnxt_tx_enable(bp);
5626 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005627 /* Poll link status and check for SFP+ module status */
5628 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005629
5630 return 0;
5631
5632open_err:
5633 bnxt_disable_napi(bp);
5634 bnxt_del_napi(bp);
5635
5636open_err_free_mem:
5637 bnxt_free_skbs(bp);
5638 bnxt_free_irq(bp);
5639 bnxt_free_mem(bp, true);
5640 return rc;
5641}
5642
5643/* rtnl_lock held */
5644int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5645{
5646 int rc = 0;
5647
5648 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5649 if (rc) {
5650 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5651 dev_close(bp->dev);
5652 }
5653 return rc;
5654}
5655
5656static int bnxt_open(struct net_device *dev)
5657{
5658 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005659
Michael Chanc0c050c2015-10-22 16:01:17 -04005660 return __bnxt_open_nic(bp, true, true);
5661}
5662
5663static void bnxt_disable_int_sync(struct bnxt *bp)
5664{
5665 int i;
5666
5667 atomic_inc(&bp->intr_sem);
5668 if (!netif_running(bp->dev))
5669 return;
5670
5671 bnxt_disable_int(bp);
5672 for (i = 0; i < bp->cp_nr_rings; i++)
5673 synchronize_irq(bp->irq_tbl[i].vector);
5674}
5675
5676int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5677{
5678 int rc = 0;
5679
5680#ifdef CONFIG_BNXT_SRIOV
5681 if (bp->sriov_cfg) {
5682 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5683 !bp->sriov_cfg,
5684 BNXT_SRIOV_CFG_WAIT_TMO);
5685 if (rc)
5686 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5687 }
5688#endif
5689 /* Change device state to avoid TX queue wake up's */
5690 bnxt_tx_disable(bp);
5691
Michael Chancaefe522015-12-09 19:35:42 -05005692 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005693 smp_mb__after_atomic();
5694 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5695 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005696
5697 /* Flush rings before disabling interrupts */
5698 bnxt_shutdown_nic(bp, irq_re_init);
5699
5700 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5701
5702 bnxt_disable_napi(bp);
5703 bnxt_disable_int_sync(bp);
5704 del_timer_sync(&bp->timer);
5705 bnxt_free_skbs(bp);
5706
5707 if (irq_re_init) {
5708 bnxt_free_irq(bp);
5709 bnxt_del_napi(bp);
5710 }
5711 bnxt_free_mem(bp, irq_re_init);
5712 return rc;
5713}
5714
5715static int bnxt_close(struct net_device *dev)
5716{
5717 struct bnxt *bp = netdev_priv(dev);
5718
5719 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005720 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005721 return 0;
5722}
5723
5724/* rtnl_lock held */
5725static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5726{
5727 switch (cmd) {
5728 case SIOCGMIIPHY:
5729 /* fallthru */
5730 case SIOCGMIIREG: {
5731 if (!netif_running(dev))
5732 return -EAGAIN;
5733
5734 return 0;
5735 }
5736
5737 case SIOCSMIIREG:
5738 if (!netif_running(dev))
5739 return -EAGAIN;
5740
5741 return 0;
5742
5743 default:
5744 /* do nothing */
5745 break;
5746 }
5747 return -EOPNOTSUPP;
5748}
5749
5750static struct rtnl_link_stats64 *
5751bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5752{
5753 u32 i;
5754 struct bnxt *bp = netdev_priv(dev);
5755
5756 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5757
5758 if (!bp->bnapi)
5759 return stats;
5760
5761 /* TODO check if we need to synchronize with bnxt_close path */
5762 for (i = 0; i < bp->cp_nr_rings; i++) {
5763 struct bnxt_napi *bnapi = bp->bnapi[i];
5764 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5765 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5766
5767 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5768 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5769 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5770
5771 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5772 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5773 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5774
5775 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5776 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5777 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5778
5779 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5780 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5781 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5782
5783 stats->rx_missed_errors +=
5784 le64_to_cpu(hw_stats->rx_discard_pkts);
5785
5786 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5787
Michael Chanc0c050c2015-10-22 16:01:17 -04005788 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5789 }
5790
Michael Chan9947f832016-03-07 15:38:46 -05005791 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5792 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5793 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5794
5795 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5796 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5797 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5798 le64_to_cpu(rx->rx_ovrsz_frames) +
5799 le64_to_cpu(rx->rx_runt_frames);
5800 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5801 le64_to_cpu(rx->rx_jbr_frames);
5802 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5803 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5804 stats->tx_errors = le64_to_cpu(tx->tx_err);
5805 }
5806
Michael Chanc0c050c2015-10-22 16:01:17 -04005807 return stats;
5808}
5809
5810static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5811{
5812 struct net_device *dev = bp->dev;
5813 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5814 struct netdev_hw_addr *ha;
5815 u8 *haddr;
5816 int mc_count = 0;
5817 bool update = false;
5818 int off = 0;
5819
5820 netdev_for_each_mc_addr(ha, dev) {
5821 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5822 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5823 vnic->mc_list_count = 0;
5824 return false;
5825 }
5826 haddr = ha->addr;
5827 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5828 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5829 update = true;
5830 }
5831 off += ETH_ALEN;
5832 mc_count++;
5833 }
5834 if (mc_count)
5835 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5836
5837 if (mc_count != vnic->mc_list_count) {
5838 vnic->mc_list_count = mc_count;
5839 update = true;
5840 }
5841 return update;
5842}
5843
5844static bool bnxt_uc_list_updated(struct bnxt *bp)
5845{
5846 struct net_device *dev = bp->dev;
5847 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5848 struct netdev_hw_addr *ha;
5849 int off = 0;
5850
5851 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5852 return true;
5853
5854 netdev_for_each_uc_addr(ha, dev) {
5855 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5856 return true;
5857
5858 off += ETH_ALEN;
5859 }
5860 return false;
5861}
5862
5863static void bnxt_set_rx_mode(struct net_device *dev)
5864{
5865 struct bnxt *bp = netdev_priv(dev);
5866 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5867 u32 mask = vnic->rx_mask;
5868 bool mc_update = false;
5869 bool uc_update;
5870
5871 if (!netif_running(dev))
5872 return;
5873
5874 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5875 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5876 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5877
Michael Chan17c71ac2016-07-01 18:46:27 -04005878 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005879 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5880
5881 uc_update = bnxt_uc_list_updated(bp);
5882
5883 if (dev->flags & IFF_ALLMULTI) {
5884 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5885 vnic->mc_list_count = 0;
5886 } else {
5887 mc_update = bnxt_mc_list_updated(bp, &mask);
5888 }
5889
5890 if (mask != vnic->rx_mask || uc_update || mc_update) {
5891 vnic->rx_mask = mask;
5892
5893 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5894 schedule_work(&bp->sp_task);
5895 }
5896}
5897
Michael Chanb664f002015-12-02 01:54:08 -05005898static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005899{
5900 struct net_device *dev = bp->dev;
5901 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5902 struct netdev_hw_addr *ha;
5903 int i, off = 0, rc;
5904 bool uc_update;
5905
5906 netif_addr_lock_bh(dev);
5907 uc_update = bnxt_uc_list_updated(bp);
5908 netif_addr_unlock_bh(dev);
5909
5910 if (!uc_update)
5911 goto skip_uc;
5912
5913 mutex_lock(&bp->hwrm_cmd_lock);
5914 for (i = 1; i < vnic->uc_filter_count; i++) {
5915 struct hwrm_cfa_l2_filter_free_input req = {0};
5916
5917 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5918 -1);
5919
5920 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5921
5922 rc = _hwrm_send_message(bp, &req, sizeof(req),
5923 HWRM_CMD_TIMEOUT);
5924 }
5925 mutex_unlock(&bp->hwrm_cmd_lock);
5926
5927 vnic->uc_filter_count = 1;
5928
5929 netif_addr_lock_bh(dev);
5930 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5931 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5932 } else {
5933 netdev_for_each_uc_addr(ha, dev) {
5934 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5935 off += ETH_ALEN;
5936 vnic->uc_filter_count++;
5937 }
5938 }
5939 netif_addr_unlock_bh(dev);
5940
5941 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5942 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5943 if (rc) {
5944 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5945 rc);
5946 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005947 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005948 }
5949 }
5950
5951skip_uc:
5952 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5953 if (rc)
5954 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5955 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005956
5957 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005958}
5959
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005960static bool bnxt_rfs_capable(struct bnxt *bp)
5961{
5962#ifdef CONFIG_RFS_ACCEL
5963 struct bnxt_pf_info *pf = &bp->pf;
5964 int vnics;
5965
5966 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5967 return false;
5968
5969 vnics = 1 + bp->rx_nr_rings;
Vasundhara Volama2304902016-07-25 12:33:36 -04005970 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5971 netdev_warn(bp->dev,
5972 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5973 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005974 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04005975 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005976
5977 return true;
5978#else
5979 return false;
5980#endif
5981}
5982
Michael Chanc0c050c2015-10-22 16:01:17 -04005983static netdev_features_t bnxt_fix_features(struct net_device *dev,
5984 netdev_features_t features)
5985{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005986 struct bnxt *bp = netdev_priv(dev);
5987
Vasundhara Volama2304902016-07-25 12:33:36 -04005988 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005989 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005990
5991 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5992 * turned on or off together.
5993 */
5994 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5995 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5996 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5997 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5998 NETIF_F_HW_VLAN_STAG_RX);
5999 else
6000 features |= NETIF_F_HW_VLAN_CTAG_RX |
6001 NETIF_F_HW_VLAN_STAG_RX;
6002 }
Michael Chancf6645f2016-06-13 02:25:28 -04006003#ifdef CONFIG_BNXT_SRIOV
6004 if (BNXT_VF(bp)) {
6005 if (bp->vf.vlan) {
6006 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6007 NETIF_F_HW_VLAN_STAG_RX);
6008 }
6009 }
6010#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006011 return features;
6012}
6013
6014static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6015{
6016 struct bnxt *bp = netdev_priv(dev);
6017 u32 flags = bp->flags;
6018 u32 changes;
6019 int rc = 0;
6020 bool re_init = false;
6021 bool update_tpa = false;
6022
6023 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006024 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006025 flags |= BNXT_FLAG_GRO;
6026 if (features & NETIF_F_LRO)
6027 flags |= BNXT_FLAG_LRO;
6028
6029 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6030 flags |= BNXT_FLAG_STRIP_VLAN;
6031
6032 if (features & NETIF_F_NTUPLE)
6033 flags |= BNXT_FLAG_RFS;
6034
6035 changes = flags ^ bp->flags;
6036 if (changes & BNXT_FLAG_TPA) {
6037 update_tpa = true;
6038 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6039 (flags & BNXT_FLAG_TPA) == 0)
6040 re_init = true;
6041 }
6042
6043 if (changes & ~BNXT_FLAG_TPA)
6044 re_init = true;
6045
6046 if (flags != bp->flags) {
6047 u32 old_flags = bp->flags;
6048
6049 bp->flags = flags;
6050
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006051 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006052 if (update_tpa)
6053 bnxt_set_ring_params(bp);
6054 return rc;
6055 }
6056
6057 if (re_init) {
6058 bnxt_close_nic(bp, false, false);
6059 if (update_tpa)
6060 bnxt_set_ring_params(bp);
6061
6062 return bnxt_open_nic(bp, false, false);
6063 }
6064 if (update_tpa) {
6065 rc = bnxt_set_tpa(bp,
6066 (flags & BNXT_FLAG_TPA) ?
6067 true : false);
6068 if (rc)
6069 bp->flags = old_flags;
6070 }
6071 }
6072 return rc;
6073}
6074
Michael Chan9f554592016-01-02 23:44:58 -05006075static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6076{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006077 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006078 int i = bnapi->index;
6079
Michael Chan3b2b7d92016-01-02 23:45:00 -05006080 if (!txr)
6081 return;
6082
Michael Chan9f554592016-01-02 23:44:58 -05006083 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6084 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6085 txr->tx_cons);
6086}
6087
6088static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6089{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006090 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006091 int i = bnapi->index;
6092
Michael Chan3b2b7d92016-01-02 23:45:00 -05006093 if (!rxr)
6094 return;
6095
Michael Chan9f554592016-01-02 23:44:58 -05006096 netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
6097 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6098 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6099 rxr->rx_sw_agg_prod);
6100}
6101
6102static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6103{
6104 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6105 int i = bnapi->index;
6106
6107 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6108 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6109}
6110
Michael Chanc0c050c2015-10-22 16:01:17 -04006111static void bnxt_dbg_dump_states(struct bnxt *bp)
6112{
6113 int i;
6114 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006115
6116 for (i = 0; i < bp->cp_nr_rings; i++) {
6117 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006118 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006119 bnxt_dump_tx_sw_state(bnapi);
6120 bnxt_dump_rx_sw_state(bnapi);
6121 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006122 }
6123 }
6124}
6125
Michael Chan6988bd92016-06-13 02:25:29 -04006126static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006127{
Michael Chan6988bd92016-06-13 02:25:29 -04006128 if (!silent)
6129 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006130 if (netif_running(bp->dev)) {
6131 bnxt_close_nic(bp, false, false);
6132 bnxt_open_nic(bp, false, false);
6133 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006134}
6135
6136static void bnxt_tx_timeout(struct net_device *dev)
6137{
6138 struct bnxt *bp = netdev_priv(dev);
6139
6140 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6141 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6142 schedule_work(&bp->sp_task);
6143}
6144
6145#ifdef CONFIG_NET_POLL_CONTROLLER
6146static void bnxt_poll_controller(struct net_device *dev)
6147{
6148 struct bnxt *bp = netdev_priv(dev);
6149 int i;
6150
6151 for (i = 0; i < bp->cp_nr_rings; i++) {
6152 struct bnxt_irq *irq = &bp->irq_tbl[i];
6153
6154 disable_irq(irq->vector);
6155 irq->handler(irq->vector, bp->bnapi[i]);
6156 enable_irq(irq->vector);
6157 }
6158}
6159#endif
6160
6161static void bnxt_timer(unsigned long data)
6162{
6163 struct bnxt *bp = (struct bnxt *)data;
6164 struct net_device *dev = bp->dev;
6165
6166 if (!netif_running(dev))
6167 return;
6168
6169 if (atomic_read(&bp->intr_sem) != 0)
6170 goto bnxt_restart_timer;
6171
Michael Chan3bdf56c2016-03-07 15:38:45 -05006172 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6173 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6174 schedule_work(&bp->sp_task);
6175 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006176bnxt_restart_timer:
6177 mod_timer(&bp->timer, jiffies + bp->current_interval);
6178}
6179
Michael Chan6988bd92016-06-13 02:25:29 -04006180/* Only called from bnxt_sp_task() */
6181static void bnxt_reset(struct bnxt *bp, bool silent)
6182{
6183 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6184 * for BNXT_STATE_IN_SP_TASK to clear.
6185 * If there is a parallel dev_close(), bnxt_close() may be holding
6186 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6187 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6188 */
6189 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6190 rtnl_lock();
6191 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6192 bnxt_reset_task(bp, silent);
6193 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6194 rtnl_unlock();
6195}
6196
Michael Chanc0c050c2015-10-22 16:01:17 -04006197static void bnxt_cfg_ntp_filters(struct bnxt *);
6198
6199static void bnxt_sp_task(struct work_struct *work)
6200{
6201 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6202 int rc;
6203
Michael Chan4cebdce2015-12-09 19:35:43 -05006204 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6205 smp_mb__after_atomic();
6206 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6207 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006208 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006209 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006210
6211 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6212 bnxt_cfg_rx_mode(bp);
6213
6214 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6215 bnxt_cfg_ntp_filters(bp);
6216 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chan286ef9d2016-11-16 21:13:08 -05006217 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6218 &bp->sp_event))
6219 bnxt_hwrm_phy_qcaps(bp);
6220
Michael Chanc0c050c2015-10-22 16:01:17 -04006221 rc = bnxt_update_link(bp, true);
6222 if (rc)
6223 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6224 rc);
6225 }
6226 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6227 bnxt_hwrm_exec_fwd_req(bp);
6228 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6229 bnxt_hwrm_tunnel_dst_port_alloc(
6230 bp, bp->vxlan_port,
6231 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6232 }
6233 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6234 bnxt_hwrm_tunnel_dst_port_free(
6235 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6236 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006237 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6238 bnxt_hwrm_tunnel_dst_port_alloc(
6239 bp, bp->nge_port,
6240 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6241 }
6242 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6243 bnxt_hwrm_tunnel_dst_port_free(
6244 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6245 }
Michael Chan6988bd92016-06-13 02:25:29 -04006246 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6247 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006248
Michael Chanfc0f1922016-06-13 02:25:30 -04006249 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6250 bnxt_reset(bp, true);
6251
Michael Chan4bb13ab2016-04-05 14:09:01 -04006252 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006253 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006254
Michael Chan3bdf56c2016-03-07 15:38:45 -05006255 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6256 bnxt_hwrm_port_qstats(bp);
6257
Michael Chan4cebdce2015-12-09 19:35:43 -05006258 smp_mb__before_atomic();
6259 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006260}
6261
6262static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6263{
6264 int rc;
6265 struct bnxt *bp = netdev_priv(dev);
6266
6267 SET_NETDEV_DEV(dev, &pdev->dev);
6268
6269 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6270 rc = pci_enable_device(pdev);
6271 if (rc) {
6272 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6273 goto init_err;
6274 }
6275
6276 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6277 dev_err(&pdev->dev,
6278 "Cannot find PCI device base address, aborting\n");
6279 rc = -ENODEV;
6280 goto init_err_disable;
6281 }
6282
6283 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6284 if (rc) {
6285 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6286 goto init_err_disable;
6287 }
6288
6289 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6290 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6291 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6292 goto init_err_disable;
6293 }
6294
6295 pci_set_master(pdev);
6296
6297 bp->dev = dev;
6298 bp->pdev = pdev;
6299
6300 bp->bar0 = pci_ioremap_bar(pdev, 0);
6301 if (!bp->bar0) {
6302 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6303 rc = -ENOMEM;
6304 goto init_err_release;
6305 }
6306
6307 bp->bar1 = pci_ioremap_bar(pdev, 2);
6308 if (!bp->bar1) {
6309 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6310 rc = -ENOMEM;
6311 goto init_err_release;
6312 }
6313
6314 bp->bar2 = pci_ioremap_bar(pdev, 4);
6315 if (!bp->bar2) {
6316 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6317 rc = -ENOMEM;
6318 goto init_err_release;
6319 }
6320
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006321 pci_enable_pcie_error_reporting(pdev);
6322
Michael Chanc0c050c2015-10-22 16:01:17 -04006323 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6324
6325 spin_lock_init(&bp->ntp_fltr_lock);
6326
6327 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6328 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6329
Michael Chandfb5b892016-02-26 04:00:01 -05006330 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006331 bp->rx_coal_ticks = 12;
6332 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006333 bp->rx_coal_ticks_irq = 1;
6334 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006335
Michael Chandfc9c942016-02-26 04:00:03 -05006336 bp->tx_coal_ticks = 25;
6337 bp->tx_coal_bufs = 30;
6338 bp->tx_coal_ticks_irq = 2;
6339 bp->tx_coal_bufs_irq = 2;
6340
Michael Chan51f30782016-07-01 18:46:29 -04006341 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6342
Michael Chanc0c050c2015-10-22 16:01:17 -04006343 init_timer(&bp->timer);
6344 bp->timer.data = (unsigned long)bp;
6345 bp->timer.function = bnxt_timer;
6346 bp->current_interval = BNXT_TIMER_INTERVAL;
6347
Michael Chancaefe522015-12-09 19:35:42 -05006348 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006349
6350 return 0;
6351
6352init_err_release:
6353 if (bp->bar2) {
6354 pci_iounmap(pdev, bp->bar2);
6355 bp->bar2 = NULL;
6356 }
6357
6358 if (bp->bar1) {
6359 pci_iounmap(pdev, bp->bar1);
6360 bp->bar1 = NULL;
6361 }
6362
6363 if (bp->bar0) {
6364 pci_iounmap(pdev, bp->bar0);
6365 bp->bar0 = NULL;
6366 }
6367
6368 pci_release_regions(pdev);
6369
6370init_err_disable:
6371 pci_disable_device(pdev);
6372
6373init_err:
6374 return rc;
6375}
6376
6377/* rtnl_lock held */
6378static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6379{
6380 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006381 struct bnxt *bp = netdev_priv(dev);
6382 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006383
6384 if (!is_valid_ether_addr(addr->sa_data))
6385 return -EADDRNOTAVAIL;
6386
Michael Chan84c33dd2016-04-11 04:11:13 -04006387 rc = bnxt_approve_mac(bp, addr->sa_data);
6388 if (rc)
6389 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006390
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006391 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6392 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006393
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006394 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6395 if (netif_running(dev)) {
6396 bnxt_close_nic(bp, false, false);
6397 rc = bnxt_open_nic(bp, false, false);
6398 }
6399
6400 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006401}
6402
6403/* rtnl_lock held */
6404static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6405{
6406 struct bnxt *bp = netdev_priv(dev);
6407
Michael Chanc0c050c2015-10-22 16:01:17 -04006408 if (netif_running(dev))
6409 bnxt_close_nic(bp, false, false);
6410
6411 dev->mtu = new_mtu;
6412 bnxt_set_ring_params(bp);
6413
6414 if (netif_running(dev))
6415 return bnxt_open_nic(bp, false, false);
6416
6417 return 0;
6418}
6419
Michael Chanc5e3deb2016-12-02 21:17:15 -05006420int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006421{
6422 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006423 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006424
Michael Chanc0c050c2015-10-22 16:01:17 -04006425 if (tc > bp->max_tc) {
6426 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6427 tc, bp->max_tc);
6428 return -EINVAL;
6429 }
6430
6431 if (netdev_get_num_tc(dev) == tc)
6432 return 0;
6433
Michael Chan3ffb6a32016-11-11 00:11:42 -05006434 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6435 sh = true;
6436
Michael Chanc0c050c2015-10-22 16:01:17 -04006437 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006438 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006439
6440 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006441 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006442 return -ENOMEM;
6443 }
6444
6445 /* Needs to close the device and do hw resource re-allocations */
6446 if (netif_running(bp->dev))
6447 bnxt_close_nic(bp, true, false);
6448
6449 if (tc) {
6450 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6451 netdev_set_num_tc(dev, tc);
6452 } else {
6453 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6454 netdev_reset_tc(dev);
6455 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006456 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6457 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006458 bp->num_stat_ctxs = bp->cp_nr_rings;
6459
6460 if (netif_running(bp->dev))
6461 return bnxt_open_nic(bp, true, false);
6462
6463 return 0;
6464}
6465
Michael Chanc5e3deb2016-12-02 21:17:15 -05006466static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6467 struct tc_to_netdev *ntc)
6468{
6469 if (ntc->type != TC_SETUP_MQPRIO)
6470 return -EINVAL;
6471
6472 return bnxt_setup_mq_tc(dev, ntc->tc);
6473}
6474
Michael Chanc0c050c2015-10-22 16:01:17 -04006475#ifdef CONFIG_RFS_ACCEL
6476static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6477 struct bnxt_ntuple_filter *f2)
6478{
6479 struct flow_keys *keys1 = &f1->fkeys;
6480 struct flow_keys *keys2 = &f2->fkeys;
6481
6482 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6483 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6484 keys1->ports.ports == keys2->ports.ports &&
6485 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6486 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006487 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6488 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006489 return true;
6490
6491 return false;
6492}
6493
6494static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6495 u16 rxq_index, u32 flow_id)
6496{
6497 struct bnxt *bp = netdev_priv(dev);
6498 struct bnxt_ntuple_filter *fltr, *new_fltr;
6499 struct flow_keys *fkeys;
6500 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006501 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006502 struct hlist_head *head;
6503
6504 if (skb->encapsulation)
6505 return -EPROTONOSUPPORT;
6506
Michael Chana54c4d72016-07-25 12:33:35 -04006507 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6508 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6509 int off = 0, j;
6510
6511 netif_addr_lock_bh(dev);
6512 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6513 if (ether_addr_equal(eth->h_dest,
6514 vnic->uc_list + off)) {
6515 l2_idx = j + 1;
6516 break;
6517 }
6518 }
6519 netif_addr_unlock_bh(dev);
6520 if (!l2_idx)
6521 return -EINVAL;
6522 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006523 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6524 if (!new_fltr)
6525 return -ENOMEM;
6526
6527 fkeys = &new_fltr->fkeys;
6528 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6529 rc = -EPROTONOSUPPORT;
6530 goto err_free;
6531 }
6532
6533 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6534 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6535 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6536 rc = -EPROTONOSUPPORT;
6537 goto err_free;
6538 }
6539
Michael Chana54c4d72016-07-25 12:33:35 -04006540 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006541 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6542
6543 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6544 head = &bp->ntp_fltr_hash_tbl[idx];
6545 rcu_read_lock();
6546 hlist_for_each_entry_rcu(fltr, head, hash) {
6547 if (bnxt_fltr_match(fltr, new_fltr)) {
6548 rcu_read_unlock();
6549 rc = 0;
6550 goto err_free;
6551 }
6552 }
6553 rcu_read_unlock();
6554
6555 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006556 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6557 BNXT_NTP_FLTR_MAX_FLTR, 0);
6558 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006559 spin_unlock_bh(&bp->ntp_fltr_lock);
6560 rc = -ENOMEM;
6561 goto err_free;
6562 }
6563
Michael Chan84e86b92015-11-05 16:25:50 -05006564 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006565 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006566 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006567 new_fltr->rxq = rxq_index;
6568 hlist_add_head_rcu(&new_fltr->hash, head);
6569 bp->ntp_fltr_count++;
6570 spin_unlock_bh(&bp->ntp_fltr_lock);
6571
6572 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6573 schedule_work(&bp->sp_task);
6574
6575 return new_fltr->sw_id;
6576
6577err_free:
6578 kfree(new_fltr);
6579 return rc;
6580}
6581
6582static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6583{
6584 int i;
6585
6586 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6587 struct hlist_head *head;
6588 struct hlist_node *tmp;
6589 struct bnxt_ntuple_filter *fltr;
6590 int rc;
6591
6592 head = &bp->ntp_fltr_hash_tbl[i];
6593 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6594 bool del = false;
6595
6596 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6597 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6598 fltr->flow_id,
6599 fltr->sw_id)) {
6600 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6601 fltr);
6602 del = true;
6603 }
6604 } else {
6605 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6606 fltr);
6607 if (rc)
6608 del = true;
6609 else
6610 set_bit(BNXT_FLTR_VALID, &fltr->state);
6611 }
6612
6613 if (del) {
6614 spin_lock_bh(&bp->ntp_fltr_lock);
6615 hlist_del_rcu(&fltr->hash);
6616 bp->ntp_fltr_count--;
6617 spin_unlock_bh(&bp->ntp_fltr_lock);
6618 synchronize_rcu();
6619 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6620 kfree(fltr);
6621 }
6622 }
6623 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006624 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6625 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006626}
6627
6628#else
6629
6630static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6631{
6632}
6633
6634#endif /* CONFIG_RFS_ACCEL */
6635
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006636static void bnxt_udp_tunnel_add(struct net_device *dev,
6637 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006638{
6639 struct bnxt *bp = netdev_priv(dev);
6640
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006641 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6642 return;
6643
Michael Chanc0c050c2015-10-22 16:01:17 -04006644 if (!netif_running(dev))
6645 return;
6646
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006647 switch (ti->type) {
6648 case UDP_TUNNEL_TYPE_VXLAN:
6649 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6650 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006651
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006652 bp->vxlan_port_cnt++;
6653 if (bp->vxlan_port_cnt == 1) {
6654 bp->vxlan_port = ti->port;
6655 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006656 schedule_work(&bp->sp_task);
6657 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006658 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006659 case UDP_TUNNEL_TYPE_GENEVE:
6660 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6661 return;
6662
6663 bp->nge_port_cnt++;
6664 if (bp->nge_port_cnt == 1) {
6665 bp->nge_port = ti->port;
6666 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6667 }
6668 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006669 default:
6670 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006671 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006672
6673 schedule_work(&bp->sp_task);
6674}
6675
6676static void bnxt_udp_tunnel_del(struct net_device *dev,
6677 struct udp_tunnel_info *ti)
6678{
6679 struct bnxt *bp = netdev_priv(dev);
6680
6681 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6682 return;
6683
6684 if (!netif_running(dev))
6685 return;
6686
6687 switch (ti->type) {
6688 case UDP_TUNNEL_TYPE_VXLAN:
6689 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6690 return;
6691 bp->vxlan_port_cnt--;
6692
6693 if (bp->vxlan_port_cnt != 0)
6694 return;
6695
6696 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6697 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006698 case UDP_TUNNEL_TYPE_GENEVE:
6699 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6700 return;
6701 bp->nge_port_cnt--;
6702
6703 if (bp->nge_port_cnt != 0)
6704 return;
6705
6706 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6707 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006708 default:
6709 return;
6710 }
6711
6712 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006713}
6714
6715static const struct net_device_ops bnxt_netdev_ops = {
6716 .ndo_open = bnxt_open,
6717 .ndo_start_xmit = bnxt_start_xmit,
6718 .ndo_stop = bnxt_close,
6719 .ndo_get_stats64 = bnxt_get_stats64,
6720 .ndo_set_rx_mode = bnxt_set_rx_mode,
6721 .ndo_do_ioctl = bnxt_ioctl,
6722 .ndo_validate_addr = eth_validate_addr,
6723 .ndo_set_mac_address = bnxt_change_mac_addr,
6724 .ndo_change_mtu = bnxt_change_mtu,
6725 .ndo_fix_features = bnxt_fix_features,
6726 .ndo_set_features = bnxt_set_features,
6727 .ndo_tx_timeout = bnxt_tx_timeout,
6728#ifdef CONFIG_BNXT_SRIOV
6729 .ndo_get_vf_config = bnxt_get_vf_config,
6730 .ndo_set_vf_mac = bnxt_set_vf_mac,
6731 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6732 .ndo_set_vf_rate = bnxt_set_vf_bw,
6733 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6734 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6735#endif
6736#ifdef CONFIG_NET_POLL_CONTROLLER
6737 .ndo_poll_controller = bnxt_poll_controller,
6738#endif
6739 .ndo_setup_tc = bnxt_setup_tc,
6740#ifdef CONFIG_RFS_ACCEL
6741 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6742#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006743 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6744 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006745#ifdef CONFIG_NET_RX_BUSY_POLL
6746 .ndo_busy_poll = bnxt_busy_poll,
6747#endif
6748};
6749
6750static void bnxt_remove_one(struct pci_dev *pdev)
6751{
6752 struct net_device *dev = pci_get_drvdata(pdev);
6753 struct bnxt *bp = netdev_priv(dev);
6754
6755 if (BNXT_PF(bp))
6756 bnxt_sriov_disable(bp);
6757
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006758 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006759 unregister_netdev(dev);
6760 cancel_work_sync(&bp->sp_task);
6761 bp->sp_event = 0;
6762
Michael Chan78095922016-12-07 00:26:16 -05006763 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006764 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006765 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05006766 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006767 pci_iounmap(pdev, bp->bar2);
6768 pci_iounmap(pdev, bp->bar1);
6769 pci_iounmap(pdev, bp->bar0);
6770 free_netdev(dev);
6771
6772 pci_release_regions(pdev);
6773 pci_disable_device(pdev);
6774}
6775
6776static int bnxt_probe_phy(struct bnxt *bp)
6777{
6778 int rc = 0;
6779 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006780
Michael Chan170ce012016-04-05 14:08:57 -04006781 rc = bnxt_hwrm_phy_qcaps(bp);
6782 if (rc) {
6783 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6784 rc);
6785 return rc;
6786 }
6787
Michael Chanc0c050c2015-10-22 16:01:17 -04006788 rc = bnxt_update_link(bp, false);
6789 if (rc) {
6790 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6791 rc);
6792 return rc;
6793 }
6794
Michael Chan93ed8112016-06-13 02:25:37 -04006795 /* Older firmware does not have supported_auto_speeds, so assume
6796 * that all supported speeds can be autonegotiated.
6797 */
6798 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6799 link_info->support_auto_speeds = link_info->support_speeds;
6800
Michael Chanc0c050c2015-10-22 16:01:17 -04006801 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006802 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006803 link_info->autoneg = BNXT_AUTONEG_SPEED;
6804 if (bp->hwrm_spec_code >= 0x10201) {
6805 if (link_info->auto_pause_setting &
6806 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6807 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6808 } else {
6809 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6810 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006811 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006812 } else {
6813 link_info->req_link_speed = link_info->force_link_speed;
6814 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006815 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006816 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6817 link_info->req_flow_ctrl =
6818 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6819 else
6820 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006821 return rc;
6822}
6823
6824static int bnxt_get_max_irq(struct pci_dev *pdev)
6825{
6826 u16 ctrl;
6827
6828 if (!pdev->msix_cap)
6829 return 1;
6830
6831 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6832 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6833}
6834
Michael Chan6e6c5a52016-01-02 23:45:02 -05006835static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6836 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006837{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006838 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006839
Michael Chan379a80a2015-10-23 15:06:19 -04006840#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006841 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006842 *max_tx = bp->vf.max_tx_rings;
6843 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006844 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6845 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006846 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006847 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006848#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006849 {
6850 *max_tx = bp->pf.max_tx_rings;
6851 *max_rx = bp->pf.max_rx_rings;
6852 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6853 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6854 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006855 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006856 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6857 *max_cp -= 1;
6858 *max_rx -= 2;
6859 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006860 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6861 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006862 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006863}
6864
6865int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6866{
6867 int rx, tx, cp;
6868
6869 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6870 if (!rx || !tx || !cp)
6871 return -ENOMEM;
6872
6873 *max_rx = rx;
6874 *max_tx = tx;
6875 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6876}
6877
Michael Chane4060d32016-12-07 00:26:19 -05006878static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6879 bool shared)
6880{
6881 int rc;
6882
6883 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
6884 if (rc)
6885 return rc;
6886
6887 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
6888 int max_cp, max_stat, max_irq;
6889
6890 /* Reserve minimum resources for RoCE */
6891 max_cp = bnxt_get_max_func_cp_rings(bp);
6892 max_stat = bnxt_get_max_func_stat_ctxs(bp);
6893 max_irq = bnxt_get_max_func_irqs(bp);
6894 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
6895 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
6896 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
6897 return 0;
6898
6899 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
6900 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
6901 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
6902 max_cp = min_t(int, max_cp, max_irq);
6903 max_cp = min_t(int, max_cp, max_stat);
6904 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
6905 if (rc)
6906 rc = 0;
6907 }
6908 return rc;
6909}
6910
Michael Chan6e6c5a52016-01-02 23:45:02 -05006911static int bnxt_set_dflt_rings(struct bnxt *bp)
6912{
6913 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6914 bool sh = true;
6915
6916 if (sh)
6917 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6918 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05006919 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006920 if (rc)
6921 return rc;
6922 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6923 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6924 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6925 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6926 bp->tx_nr_rings + bp->rx_nr_rings;
6927 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006928 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6929 bp->rx_nr_rings++;
6930 bp->cp_nr_rings++;
6931 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006932 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006933}
6934
Michael Chan7b08f662016-12-07 00:26:18 -05006935void bnxt_restore_pf_fw_resources(struct bnxt *bp)
6936{
6937 ASSERT_RTNL();
6938 bnxt_hwrm_func_qcaps(bp);
6939}
6940
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006941static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6942{
6943 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6944 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6945
6946 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6947 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6948 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6949 else
6950 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6951 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6952 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6953 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6954 "Unknown", width);
6955}
6956
Michael Chanc0c050c2015-10-22 16:01:17 -04006957static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6958{
6959 static int version_printed;
6960 struct net_device *dev;
6961 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006962 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006963
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04006964 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6965 return -ENODEV;
6966
Michael Chanc0c050c2015-10-22 16:01:17 -04006967 if (version_printed++ == 0)
6968 pr_info("%s", version);
6969
6970 max_irqs = bnxt_get_max_irq(pdev);
6971 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6972 if (!dev)
6973 return -ENOMEM;
6974
6975 bp = netdev_priv(dev);
6976
6977 if (bnxt_vf_pciid(ent->driver_data))
6978 bp->flags |= BNXT_FLAG_VF;
6979
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006980 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006981 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006982
6983 rc = bnxt_init_board(pdev, dev);
6984 if (rc < 0)
6985 goto init_err_free;
6986
6987 dev->netdev_ops = &bnxt_netdev_ops;
6988 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6989 dev->ethtool_ops = &bnxt_ethtool_ops;
6990
6991 pci_set_drvdata(pdev, dev);
6992
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006993 rc = bnxt_alloc_hwrm_resources(bp);
6994 if (rc)
6995 goto init_err;
6996
6997 mutex_init(&bp->hwrm_cmd_lock);
6998 rc = bnxt_hwrm_ver_get(bp);
6999 if (rc)
7000 goto init_err;
7001
Rob Swindell5ac67d82016-09-19 03:58:03 -04007002 bnxt_hwrm_fw_set_time(bp);
7003
Michael Chanc0c050c2015-10-22 16:01:17 -04007004 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7005 NETIF_F_TSO | NETIF_F_TSO6 |
7006 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007007 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007008 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7009 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007010 NETIF_F_RXCSUM | NETIF_F_GRO;
7011
7012 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7013 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007014
Michael Chanc0c050c2015-10-22 16:01:17 -04007015 dev->hw_enc_features =
7016 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7017 NETIF_F_TSO | NETIF_F_TSO6 |
7018 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007019 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007020 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007021 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7022 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007023 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7024 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7025 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7026 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7027 dev->priv_flags |= IFF_UNICAST_FLT;
7028
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007029 /* MTU range: 60 - 9500 */
7030 dev->min_mtu = ETH_ZLEN;
7031 dev->max_mtu = 9500;
7032
Michael Chan7df4ae92016-12-02 21:17:17 -05007033 bnxt_dcb_init(bp);
7034
Michael Chanc0c050c2015-10-22 16:01:17 -04007035#ifdef CONFIG_BNXT_SRIOV
7036 init_waitqueue_head(&bp->sriov_cfg_wait);
7037#endif
Michael Chan309369c2016-06-13 02:25:34 -04007038 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007039 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7040 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007041
Michael Chanc0c050c2015-10-22 16:01:17 -04007042 rc = bnxt_hwrm_func_drv_rgtr(bp);
7043 if (rc)
7044 goto init_err;
7045
Michael Chana1653b12016-12-07 00:26:20 -05007046 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7047 if (rc)
7048 goto init_err;
7049
Michael Chanc0c050c2015-10-22 16:01:17 -04007050 /* Get the MAX capabilities for this function */
7051 rc = bnxt_hwrm_func_qcaps(bp);
7052 if (rc) {
7053 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7054 rc);
7055 rc = -1;
7056 goto init_err;
7057 }
7058
7059 rc = bnxt_hwrm_queue_qportcfg(bp);
7060 if (rc) {
7061 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7062 rc);
7063 rc = -1;
7064 goto init_err;
7065 }
7066
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007067 bnxt_hwrm_func_qcfg(bp);
7068
Michael Chanc0c050c2015-10-22 16:01:17 -04007069 bnxt_set_tpa_flags(bp);
7070 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007071 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007072 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007073
Michael Chan87da7f72016-11-16 21:13:09 -05007074 /* Default RSS hash cfg. */
7075 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7076 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7077 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7078 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7079 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7080 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7081 bp->hwrm_spec_code >= 0x10501) {
7082 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7083 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7084 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7085 }
7086
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007087 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007088 dev->hw_features |= NETIF_F_NTUPLE;
7089 if (bnxt_rfs_capable(bp)) {
7090 bp->flags |= BNXT_FLAG_RFS;
7091 dev->features |= NETIF_F_NTUPLE;
7092 }
7093 }
7094
Michael Chanc0c050c2015-10-22 16:01:17 -04007095 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7096 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7097
7098 rc = bnxt_probe_phy(bp);
7099 if (rc)
7100 goto init_err;
7101
Michael Chanaa8ed022016-12-07 00:26:17 -05007102 rc = bnxt_hwrm_func_reset(bp);
7103 if (rc)
7104 goto init_err;
7105
Michael Chan78095922016-12-07 00:26:16 -05007106 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007107 if (rc)
7108 goto init_err;
7109
Michael Chan78095922016-12-07 00:26:16 -05007110 rc = register_netdev(dev);
7111 if (rc)
7112 goto init_err_clr_int;
7113
Michael Chanc0c050c2015-10-22 16:01:17 -04007114 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7115 board_info[ent->driver_data].name,
7116 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7117
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007118 bnxt_parse_log_pcie_link(bp);
7119
Michael Chanc0c050c2015-10-22 16:01:17 -04007120 return 0;
7121
Michael Chan78095922016-12-07 00:26:16 -05007122init_err_clr_int:
7123 bnxt_clear_int_mode(bp);
7124
Michael Chanc0c050c2015-10-22 16:01:17 -04007125init_err:
7126 pci_iounmap(pdev, bp->bar0);
7127 pci_release_regions(pdev);
7128 pci_disable_device(pdev);
7129
7130init_err_free:
7131 free_netdev(dev);
7132 return rc;
7133}
7134
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007135/**
7136 * bnxt_io_error_detected - called when PCI error is detected
7137 * @pdev: Pointer to PCI device
7138 * @state: The current pci connection state
7139 *
7140 * This function is called after a PCI bus error affecting
7141 * this device has been detected.
7142 */
7143static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7144 pci_channel_state_t state)
7145{
7146 struct net_device *netdev = pci_get_drvdata(pdev);
7147
7148 netdev_info(netdev, "PCI I/O error detected\n");
7149
7150 rtnl_lock();
7151 netif_device_detach(netdev);
7152
7153 if (state == pci_channel_io_perm_failure) {
7154 rtnl_unlock();
7155 return PCI_ERS_RESULT_DISCONNECT;
7156 }
7157
7158 if (netif_running(netdev))
7159 bnxt_close(netdev);
7160
7161 pci_disable_device(pdev);
7162 rtnl_unlock();
7163
7164 /* Request a slot slot reset. */
7165 return PCI_ERS_RESULT_NEED_RESET;
7166}
7167
7168/**
7169 * bnxt_io_slot_reset - called after the pci bus has been reset.
7170 * @pdev: Pointer to PCI device
7171 *
7172 * Restart the card from scratch, as if from a cold-boot.
7173 * At this point, the card has exprienced a hard reset,
7174 * followed by fixups by BIOS, and has its config space
7175 * set up identically to what it was at cold boot.
7176 */
7177static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7178{
7179 struct net_device *netdev = pci_get_drvdata(pdev);
7180 struct bnxt *bp = netdev_priv(netdev);
7181 int err = 0;
7182 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7183
7184 netdev_info(bp->dev, "PCI Slot Reset\n");
7185
7186 rtnl_lock();
7187
7188 if (pci_enable_device(pdev)) {
7189 dev_err(&pdev->dev,
7190 "Cannot re-enable PCI device after reset.\n");
7191 } else {
7192 pci_set_master(pdev);
7193
Michael Chanaa8ed022016-12-07 00:26:17 -05007194 err = bnxt_hwrm_func_reset(bp);
7195 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007196 err = bnxt_open(netdev);
7197
7198 if (!err)
7199 result = PCI_ERS_RESULT_RECOVERED;
7200 }
7201
7202 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7203 dev_close(netdev);
7204
7205 rtnl_unlock();
7206
7207 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7208 if (err) {
7209 dev_err(&pdev->dev,
7210 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7211 err); /* non-fatal, continue */
7212 }
7213
7214 return PCI_ERS_RESULT_RECOVERED;
7215}
7216
7217/**
7218 * bnxt_io_resume - called when traffic can start flowing again.
7219 * @pdev: Pointer to PCI device
7220 *
7221 * This callback is called when the error recovery driver tells
7222 * us that its OK to resume normal operation.
7223 */
7224static void bnxt_io_resume(struct pci_dev *pdev)
7225{
7226 struct net_device *netdev = pci_get_drvdata(pdev);
7227
7228 rtnl_lock();
7229
7230 netif_device_attach(netdev);
7231
7232 rtnl_unlock();
7233}
7234
7235static const struct pci_error_handlers bnxt_err_handler = {
7236 .error_detected = bnxt_io_error_detected,
7237 .slot_reset = bnxt_io_slot_reset,
7238 .resume = bnxt_io_resume
7239};
7240
Michael Chanc0c050c2015-10-22 16:01:17 -04007241static struct pci_driver bnxt_pci_driver = {
7242 .name = DRV_MODULE_NAME,
7243 .id_table = bnxt_pci_tbl,
7244 .probe = bnxt_init_one,
7245 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007246 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007247#if defined(CONFIG_BNXT_SRIOV)
7248 .sriov_configure = bnxt_sriov_configure,
7249#endif
7250};
7251
7252module_pci_driver(bnxt_pci_driver);