blob: 47bc4f3e58082ae8b64739585ee8d65d9f13e440 [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 Chanbac9a7e2017-02-12 19:18:10 -05004 * Copyright (c) 2016-2017 Broadcom Limited
Michael Chanc0c050c2015-10-22 16:01:17 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12
13#include <linux/stringify.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/errno.h>
17#include <linux/ioport.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/interrupt.h>
21#include <linux/pci.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/skbuff.h>
25#include <linux/dma-mapping.h>
26#include <linux/bitops.h>
27#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/delay.h>
30#include <asm/byteorder.h>
31#include <asm/page.h>
32#include <linux/time.h>
33#include <linux/mii.h>
34#include <linux/if.h>
35#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040036#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050037#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040038#include <net/ip.h>
39#include <net/tcp.h>
40#include <net/udp.h>
41#include <net/checksum.h>
42#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070043#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040044#include <linux/workqueue.h>
45#include <linux/prefetch.h>
46#include <linux/cache.h>
47#include <linux/log2.h>
48#include <linux/aer.h>
49#include <linux/bitmap.h>
50#include <linux/cpu_rmap.h>
51
52#include "bnxt_hsi.h"
53#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050054#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040055#include "bnxt_sriov.h"
56#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050057#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050058#include "bnxt_xdp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040059
60#define BNXT_TX_TIMEOUT (5 * HZ)
61
62static const char version[] =
63 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
64
65MODULE_LICENSE("GPL");
66MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
67MODULE_VERSION(DRV_MODULE_VERSION);
68
69#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
70#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
71#define BNXT_RX_COPY_THRESH 256
72
Michael Chan4419dbe2016-02-10 17:33:49 -050073#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040074
75enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050076 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040077 BCM57302,
78 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040079 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040080 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040081 BCM57311,
82 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050083 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040084 BCM57404,
85 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040086 BCM57402_NPAR,
87 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040088 BCM57412,
89 BCM57414,
90 BCM57416,
91 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040093 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040094 BCM57417_SFP,
95 BCM57416_SFP,
96 BCM57404_NPAR,
97 BCM57406_NPAR,
98 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040099 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400100 BCM57414_NPAR,
101 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500102 BCM57452,
103 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400104 NETXTREME_E_VF,
105 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400106};
107
108/* indexed by enum above */
109static const struct {
110 char *name;
111} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400112 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400115 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400116 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
117 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
118 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
119 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400122 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400123 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
124 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
125 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
127 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400128 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
130 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
131 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400132 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400134 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
135 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400136 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
137 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500138 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400140 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
141 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400142};
143
144static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400145 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
148 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400150 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
152 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500153 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400154 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
155 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400156 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
157 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400158 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
159 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
160 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
161 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400162 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400163 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
165 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
166 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
167 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400172 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500175 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
176 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400177#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400178 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
179 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
180 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
182 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400184#endif
185 { 0 }
186};
187
188MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
189
190static const u16 bnxt_vf_req_snif[] = {
191 HWRM_FUNC_CFG,
192 HWRM_PORT_PHY_QCFG,
193 HWRM_CFA_L2_FILTER_ALLOC,
194};
195
Michael Chan25be8622016-04-05 14:09:00 -0400196static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500197 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
198 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
199 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
200 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
201 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400202};
203
Michael Chanc0c050c2015-10-22 16:01:17 -0400204static bool bnxt_vf_pciid(enum board_idx idx)
205{
Michael Chanadbc8302016-09-19 03:58:01 -0400206 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400207}
208
209#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
210#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
211#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
212
213#define BNXT_CP_DB_REARM(db, raw_cons) \
214 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
215
216#define BNXT_CP_DB(db, raw_cons) \
217 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
218
219#define BNXT_CP_DB_IRQ_DIS(db) \
220 writel(DB_CP_IRQ_DIS_FLAGS, db)
221
Michael Chan38413402017-02-06 16:55:43 -0500222const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanc0c050c2015-10-22 16:01:17 -0400265 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500266 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500512 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 u16 cons = txr->tx_cons;
514 struct pci_dev *pdev = bp->pdev;
515 int i;
516 unsigned int tx_bytes = 0;
517
518 for (i = 0; i < nr_pkts; i++) {
519 struct bnxt_sw_tx_bd *tx_buf;
520 struct sk_buff *skb;
521 int j, last;
522
523 tx_buf = &txr->tx_buf_ring[cons];
524 cons = NEXT_TX(cons);
525 skb = tx_buf->skb;
526 tx_buf->skb = NULL;
527
528 if (tx_buf->is_push) {
529 tx_buf->is_push = 0;
530 goto next_tx_int;
531 }
532
533 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
534 skb_headlen(skb), PCI_DMA_TODEVICE);
535 last = tx_buf->nr_frags;
536
537 for (j = 0; j < last; j++) {
538 cons = NEXT_TX(cons);
539 tx_buf = &txr->tx_buf_ring[cons];
540 dma_unmap_page(
541 &pdev->dev,
542 dma_unmap_addr(tx_buf, mapping),
543 skb_frag_size(&skb_shinfo(skb)->frags[j]),
544 PCI_DMA_TODEVICE);
545 }
546
547next_tx_int:
548 cons = NEXT_TX(cons);
549
550 tx_bytes += skb->len;
551 dev_kfree_skb_any(skb);
552 }
553
554 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
555 txr->tx_cons = cons;
556
557 /* Need to make the tx_cons update visible to bnxt_start_xmit()
558 * before checking for netif_tx_queue_stopped(). Without the
559 * memory barrier, there is a small possibility that bnxt_start_xmit()
560 * will miss it and cause the queue to be stopped forever.
561 */
562 smp_mb();
563
564 if (unlikely(netif_tx_queue_stopped(txq)) &&
565 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
566 __netif_tx_lock(txq, smp_processor_id());
567 if (netif_tx_queue_stopped(txq) &&
568 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
569 txr->dev_state != BNXT_DEV_STATE_CLOSING)
570 netif_tx_wake_queue(txq);
571 __netif_tx_unlock(txq);
572 }
573}
574
Michael Chanc61fb992017-02-06 16:55:36 -0500575static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
576 gfp_t gfp)
577{
578 struct device *dev = &bp->pdev->dev;
579 struct page *page;
580
581 page = alloc_page(gfp);
582 if (!page)
583 return NULL;
584
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700585 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
586 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500587 if (dma_mapping_error(dev, *mapping)) {
588 __free_page(page);
589 return NULL;
590 }
591 *mapping += bp->rx_dma_offset;
592 return page;
593}
594
Michael Chanc0c050c2015-10-22 16:01:17 -0400595static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
596 gfp_t gfp)
597{
598 u8 *data;
599 struct pci_dev *pdev = bp->pdev;
600
601 data = kmalloc(bp->rx_buf_size, gfp);
602 if (!data)
603 return NULL;
604
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700605 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
606 bp->rx_buf_use_size, bp->rx_dir,
607 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400608
609 if (dma_mapping_error(&pdev->dev, *mapping)) {
610 kfree(data);
611 data = NULL;
612 }
613 return data;
614}
615
Michael Chan38413402017-02-06 16:55:43 -0500616int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
617 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400618{
619 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
620 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400621 dma_addr_t mapping;
622
Michael Chanc61fb992017-02-06 16:55:36 -0500623 if (BNXT_RX_PAGE_MODE(bp)) {
624 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400625
Michael Chanc61fb992017-02-06 16:55:36 -0500626 if (!page)
627 return -ENOMEM;
628
629 rx_buf->data = page;
630 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
631 } else {
632 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
633
634 if (!data)
635 return -ENOMEM;
636
637 rx_buf->data = data;
638 rx_buf->data_ptr = data + bp->rx_offset;
639 }
Michael Chan11cd1192017-02-06 16:55:33 -0500640 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400641
642 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400643 return 0;
644}
645
Michael Chanc6d30e82017-02-06 16:55:42 -0500646void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400647{
648 u16 prod = rxr->rx_prod;
649 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
650 struct rx_bd *cons_bd, *prod_bd;
651
652 prod_rx_buf = &rxr->rx_buf_ring[prod];
653 cons_rx_buf = &rxr->rx_buf_ring[cons];
654
655 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500656 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400657
Michael Chan11cd1192017-02-06 16:55:33 -0500658 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400659
660 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
661 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
662
663 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
664}
665
666static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
667{
668 u16 next, max = rxr->rx_agg_bmap_size;
669
670 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
671 if (next >= max)
672 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
673 return next;
674}
675
676static inline int bnxt_alloc_rx_page(struct bnxt *bp,
677 struct bnxt_rx_ring_info *rxr,
678 u16 prod, gfp_t gfp)
679{
680 struct rx_bd *rxbd =
681 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
682 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
683 struct pci_dev *pdev = bp->pdev;
684 struct page *page;
685 dma_addr_t mapping;
686 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400687 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400688
Michael Chan89d0a062016-04-25 02:30:51 -0400689 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
690 page = rxr->rx_page;
691 if (!page) {
692 page = alloc_page(gfp);
693 if (!page)
694 return -ENOMEM;
695 rxr->rx_page = page;
696 rxr->rx_page_offset = 0;
697 }
698 offset = rxr->rx_page_offset;
699 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
700 if (rxr->rx_page_offset == PAGE_SIZE)
701 rxr->rx_page = NULL;
702 else
703 get_page(page);
704 } else {
705 page = alloc_page(gfp);
706 if (!page)
707 return -ENOMEM;
708 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400709
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700710 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
711 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
712 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400713 if (dma_mapping_error(&pdev->dev, mapping)) {
714 __free_page(page);
715 return -EIO;
716 }
717
718 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
719 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
720
721 __set_bit(sw_prod, rxr->rx_agg_bmap);
722 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
723 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
724
725 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400726 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400727 rx_agg_buf->mapping = mapping;
728 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
729 rxbd->rx_bd_opaque = sw_prod;
730 return 0;
731}
732
733static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
734 u32 agg_bufs)
735{
736 struct bnxt *bp = bnapi->bp;
737 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500738 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400739 u16 prod = rxr->rx_agg_prod;
740 u16 sw_prod = rxr->rx_sw_agg_prod;
741 u32 i;
742
743 for (i = 0; i < agg_bufs; i++) {
744 u16 cons;
745 struct rx_agg_cmp *agg;
746 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
747 struct rx_bd *prod_bd;
748 struct page *page;
749
750 agg = (struct rx_agg_cmp *)
751 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
752 cons = agg->rx_agg_cmp_opaque;
753 __clear_bit(cons, rxr->rx_agg_bmap);
754
755 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
756 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
757
758 __set_bit(sw_prod, rxr->rx_agg_bmap);
759 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
760 cons_rx_buf = &rxr->rx_agg_ring[cons];
761
762 /* It is possible for sw_prod to be equal to cons, so
763 * set cons_rx_buf->page to NULL first.
764 */
765 page = cons_rx_buf->page;
766 cons_rx_buf->page = NULL;
767 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400768 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400769
770 prod_rx_buf->mapping = cons_rx_buf->mapping;
771
772 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
773
774 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
775 prod_bd->rx_bd_opaque = sw_prod;
776
777 prod = NEXT_RX_AGG(prod);
778 sw_prod = NEXT_RX_AGG(sw_prod);
779 cp_cons = NEXT_CMP(cp_cons);
780 }
781 rxr->rx_agg_prod = prod;
782 rxr->rx_sw_agg_prod = sw_prod;
783}
784
Michael Chanc61fb992017-02-06 16:55:36 -0500785static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
786 struct bnxt_rx_ring_info *rxr,
787 u16 cons, void *data, u8 *data_ptr,
788 dma_addr_t dma_addr,
789 unsigned int offset_and_len)
790{
791 unsigned int payload = offset_and_len >> 16;
792 unsigned int len = offset_and_len & 0xffff;
793 struct skb_frag_struct *frag;
794 struct page *page = data;
795 u16 prod = rxr->rx_prod;
796 struct sk_buff *skb;
797 int off, err;
798
799 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
800 if (unlikely(err)) {
801 bnxt_reuse_rx_data(rxr, cons, data);
802 return NULL;
803 }
804 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700805 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
806 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500807
808 if (unlikely(!payload))
809 payload = eth_get_headlen(data_ptr, len);
810
811 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
812 if (!skb) {
813 __free_page(page);
814 return NULL;
815 }
816
817 off = (void *)data_ptr - page_address(page);
818 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
819 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
820 payload + NET_IP_ALIGN);
821
822 frag = &skb_shinfo(skb)->frags[0];
823 skb_frag_size_sub(frag, payload);
824 frag->page_offset += payload;
825 skb->data_len -= payload;
826 skb->tail += payload;
827
828 return skb;
829}
830
Michael Chanc0c050c2015-10-22 16:01:17 -0400831static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
832 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500833 void *data, u8 *data_ptr,
834 dma_addr_t dma_addr,
835 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400836{
Michael Chan6bb19472017-02-06 16:55:32 -0500837 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400838 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500839 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400840
841 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
842 if (unlikely(err)) {
843 bnxt_reuse_rx_data(rxr, cons, data);
844 return NULL;
845 }
846
847 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700848 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
849 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400850 if (!skb) {
851 kfree(data);
852 return NULL;
853 }
854
Michael Chanb3dba772017-02-06 16:55:35 -0500855 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500856 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400857 return skb;
858}
859
860static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
861 struct sk_buff *skb, u16 cp_cons,
862 u32 agg_bufs)
863{
864 struct pci_dev *pdev = bp->pdev;
865 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500866 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400867 u16 prod = rxr->rx_agg_prod;
868 u32 i;
869
870 for (i = 0; i < agg_bufs; i++) {
871 u16 cons, frag_len;
872 struct rx_agg_cmp *agg;
873 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
874 struct page *page;
875 dma_addr_t mapping;
876
877 agg = (struct rx_agg_cmp *)
878 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
879 cons = agg->rx_agg_cmp_opaque;
880 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
881 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
882
883 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400884 skb_fill_page_desc(skb, i, cons_rx_buf->page,
885 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400886 __clear_bit(cons, rxr->rx_agg_bmap);
887
888 /* It is possible for bnxt_alloc_rx_page() to allocate
889 * a sw_prod index that equals the cons index, so we
890 * need to clear the cons entry now.
891 */
Michael Chan11cd1192017-02-06 16:55:33 -0500892 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400893 page = cons_rx_buf->page;
894 cons_rx_buf->page = NULL;
895
896 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
897 struct skb_shared_info *shinfo;
898 unsigned int nr_frags;
899
900 shinfo = skb_shinfo(skb);
901 nr_frags = --shinfo->nr_frags;
902 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
903
904 dev_kfree_skb(skb);
905
906 cons_rx_buf->page = page;
907
908 /* Update prod since possibly some pages have been
909 * allocated already.
910 */
911 rxr->rx_agg_prod = prod;
912 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
913 return NULL;
914 }
915
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700916 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
917 PCI_DMA_FROMDEVICE,
918 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400919
920 skb->data_len += frag_len;
921 skb->len += frag_len;
922 skb->truesize += PAGE_SIZE;
923
924 prod = NEXT_RX_AGG(prod);
925 cp_cons = NEXT_CMP(cp_cons);
926 }
927 rxr->rx_agg_prod = prod;
928 return skb;
929}
930
931static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
932 u8 agg_bufs, u32 *raw_cons)
933{
934 u16 last;
935 struct rx_agg_cmp *agg;
936
937 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
938 last = RING_CMP(*raw_cons);
939 agg = (struct rx_agg_cmp *)
940 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
941 return RX_AGG_CMP_VALID(agg, *raw_cons);
942}
943
944static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
945 unsigned int len,
946 dma_addr_t mapping)
947{
948 struct bnxt *bp = bnapi->bp;
949 struct pci_dev *pdev = bp->pdev;
950 struct sk_buff *skb;
951
952 skb = napi_alloc_skb(&bnapi->napi, len);
953 if (!skb)
954 return NULL;
955
Michael Chan745fc052017-02-06 16:55:34 -0500956 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
957 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400958
Michael Chan6bb19472017-02-06 16:55:32 -0500959 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
960 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400961
Michael Chan745fc052017-02-06 16:55:34 -0500962 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
963 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400964
965 skb_put(skb, len);
966 return skb;
967}
968
Michael Chanfa7e2812016-05-10 19:18:00 -0400969static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
970 u32 *raw_cons, void *cmp)
971{
972 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
973 struct rx_cmp *rxcmp = cmp;
974 u32 tmp_raw_cons = *raw_cons;
975 u8 cmp_type, agg_bufs = 0;
976
977 cmp_type = RX_CMP_TYPE(rxcmp);
978
979 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
980 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
981 RX_CMP_AGG_BUFS) >>
982 RX_CMP_AGG_BUFS_SHIFT;
983 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
984 struct rx_tpa_end_cmp *tpa_end = cmp;
985
986 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
987 RX_TPA_END_CMP_AGG_BUFS) >>
988 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
989 }
990
991 if (agg_bufs) {
992 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
993 return -EBUSY;
994 }
995 *raw_cons = tmp_raw_cons;
996 return 0;
997}
998
999static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1000{
1001 if (!rxr->bnapi->in_reset) {
1002 rxr->bnapi->in_reset = true;
1003 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1004 schedule_work(&bp->sp_task);
1005 }
1006 rxr->rx_next_cons = 0xffff;
1007}
1008
Michael Chanc0c050c2015-10-22 16:01:17 -04001009static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1010 struct rx_tpa_start_cmp *tpa_start,
1011 struct rx_tpa_start_cmp_ext *tpa_start1)
1012{
1013 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1014 u16 cons, prod;
1015 struct bnxt_tpa_info *tpa_info;
1016 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1017 struct rx_bd *prod_bd;
1018 dma_addr_t mapping;
1019
1020 cons = tpa_start->rx_tpa_start_cmp_opaque;
1021 prod = rxr->rx_prod;
1022 cons_rx_buf = &rxr->rx_buf_ring[cons];
1023 prod_rx_buf = &rxr->rx_buf_ring[prod];
1024 tpa_info = &rxr->rx_tpa[agg_id];
1025
Michael Chanfa7e2812016-05-10 19:18:00 -04001026 if (unlikely(cons != rxr->rx_next_cons)) {
1027 bnxt_sched_reset(bp, rxr);
1028 return;
1029 }
1030
Michael Chanc0c050c2015-10-22 16:01:17 -04001031 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001032 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001033
1034 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001035 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001036
1037 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1038
1039 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1040
1041 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001042 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001043 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001044 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001045
1046 tpa_info->len =
1047 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1048 RX_TPA_START_CMP_LEN_SHIFT;
1049 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1050 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1051
1052 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1053 tpa_info->gso_type = SKB_GSO_TCPV4;
1054 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1055 if (hash_type == 3)
1056 tpa_info->gso_type = SKB_GSO_TCPV6;
1057 tpa_info->rss_hash =
1058 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1059 } else {
1060 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1061 tpa_info->gso_type = 0;
1062 if (netif_msg_rx_err(bp))
1063 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1064 }
1065 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1066 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001067 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001068
1069 rxr->rx_prod = NEXT_RX(prod);
1070 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001071 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001072 cons_rx_buf = &rxr->rx_buf_ring[cons];
1073
1074 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1075 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1076 cons_rx_buf->data = NULL;
1077}
1078
1079static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1080 u16 cp_cons, u32 agg_bufs)
1081{
1082 if (agg_bufs)
1083 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1084}
1085
Michael Chan94758f82016-06-13 02:25:35 -04001086static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1087 int payload_off, int tcp_ts,
1088 struct sk_buff *skb)
1089{
1090#ifdef CONFIG_INET
1091 struct tcphdr *th;
1092 int len, nw_off;
1093 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1094 u32 hdr_info = tpa_info->hdr_info;
1095 bool loopback = false;
1096
1097 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1098 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1099 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1100
1101 /* If the packet is an internal loopback packet, the offsets will
1102 * have an extra 4 bytes.
1103 */
1104 if (inner_mac_off == 4) {
1105 loopback = true;
1106 } else if (inner_mac_off > 4) {
1107 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1108 ETH_HLEN - 2));
1109
1110 /* We only support inner iPv4/ipv6. If we don't see the
1111 * correct protocol ID, it must be a loopback packet where
1112 * the offsets are off by 4.
1113 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001114 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001115 loopback = true;
1116 }
1117 if (loopback) {
1118 /* internal loopback packet, subtract all offsets by 4 */
1119 inner_ip_off -= 4;
1120 inner_mac_off -= 4;
1121 outer_ip_off -= 4;
1122 }
1123
1124 nw_off = inner_ip_off - ETH_HLEN;
1125 skb_set_network_header(skb, nw_off);
1126 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1127 struct ipv6hdr *iph = ipv6_hdr(skb);
1128
1129 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1130 len = skb->len - skb_transport_offset(skb);
1131 th = tcp_hdr(skb);
1132 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1133 } else {
1134 struct iphdr *iph = ip_hdr(skb);
1135
1136 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1137 len = skb->len - skb_transport_offset(skb);
1138 th = tcp_hdr(skb);
1139 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1140 }
1141
1142 if (inner_mac_off) { /* tunnel */
1143 struct udphdr *uh = NULL;
1144 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1145 ETH_HLEN - 2));
1146
1147 if (proto == htons(ETH_P_IP)) {
1148 struct iphdr *iph = (struct iphdr *)skb->data;
1149
1150 if (iph->protocol == IPPROTO_UDP)
1151 uh = (struct udphdr *)(iph + 1);
1152 } else {
1153 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1154
1155 if (iph->nexthdr == IPPROTO_UDP)
1156 uh = (struct udphdr *)(iph + 1);
1157 }
1158 if (uh) {
1159 if (uh->check)
1160 skb_shinfo(skb)->gso_type |=
1161 SKB_GSO_UDP_TUNNEL_CSUM;
1162 else
1163 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1164 }
1165 }
1166#endif
1167 return skb;
1168}
1169
Michael Chanc0c050c2015-10-22 16:01:17 -04001170#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1171#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1172
Michael Chan309369c2016-06-13 02:25:34 -04001173static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1174 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001175 struct sk_buff *skb)
1176{
Michael Chand1611c32015-10-25 22:27:57 -04001177#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001178 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001179 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001180
Michael Chan309369c2016-06-13 02:25:34 -04001181 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001182 tcp_opt_len = 12;
1183
Michael Chanc0c050c2015-10-22 16:01:17 -04001184 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1185 struct iphdr *iph;
1186
1187 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1188 ETH_HLEN;
1189 skb_set_network_header(skb, nw_off);
1190 iph = ip_hdr(skb);
1191 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1192 len = skb->len - skb_transport_offset(skb);
1193 th = tcp_hdr(skb);
1194 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1195 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1196 struct ipv6hdr *iph;
1197
1198 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1199 ETH_HLEN;
1200 skb_set_network_header(skb, nw_off);
1201 iph = ipv6_hdr(skb);
1202 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1203 len = skb->len - skb_transport_offset(skb);
1204 th = tcp_hdr(skb);
1205 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1206 } else {
1207 dev_kfree_skb_any(skb);
1208 return NULL;
1209 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001210
1211 if (nw_off) { /* tunnel */
1212 struct udphdr *uh = NULL;
1213
1214 if (skb->protocol == htons(ETH_P_IP)) {
1215 struct iphdr *iph = (struct iphdr *)skb->data;
1216
1217 if (iph->protocol == IPPROTO_UDP)
1218 uh = (struct udphdr *)(iph + 1);
1219 } else {
1220 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1221
1222 if (iph->nexthdr == IPPROTO_UDP)
1223 uh = (struct udphdr *)(iph + 1);
1224 }
1225 if (uh) {
1226 if (uh->check)
1227 skb_shinfo(skb)->gso_type |=
1228 SKB_GSO_UDP_TUNNEL_CSUM;
1229 else
1230 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1231 }
1232 }
1233#endif
1234 return skb;
1235}
1236
Michael Chan309369c2016-06-13 02:25:34 -04001237static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1238 struct bnxt_tpa_info *tpa_info,
1239 struct rx_tpa_end_cmp *tpa_end,
1240 struct rx_tpa_end_cmp_ext *tpa_end1,
1241 struct sk_buff *skb)
1242{
1243#ifdef CONFIG_INET
1244 int payload_off;
1245 u16 segs;
1246
1247 segs = TPA_END_TPA_SEGS(tpa_end);
1248 if (segs == 1)
1249 return skb;
1250
1251 NAPI_GRO_CB(skb)->count = segs;
1252 skb_shinfo(skb)->gso_size =
1253 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1254 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1255 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1256 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1257 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1258 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001259 if (likely(skb))
1260 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001261#endif
1262 return skb;
1263}
1264
Michael Chanc0c050c2015-10-22 16:01:17 -04001265static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1266 struct bnxt_napi *bnapi,
1267 u32 *raw_cons,
1268 struct rx_tpa_end_cmp *tpa_end,
1269 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001270 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001271{
1272 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001273 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001274 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001275 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001276 u16 cp_cons = RING_CMP(*raw_cons);
1277 unsigned int len;
1278 struct bnxt_tpa_info *tpa_info;
1279 dma_addr_t mapping;
1280 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001281 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001282
Michael Chanfa7e2812016-05-10 19:18:00 -04001283 if (unlikely(bnapi->in_reset)) {
1284 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1285
1286 if (rc < 0)
1287 return ERR_PTR(-EBUSY);
1288 return NULL;
1289 }
1290
Michael Chanc0c050c2015-10-22 16:01:17 -04001291 tpa_info = &rxr->rx_tpa[agg_id];
1292 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001293 data_ptr = tpa_info->data_ptr;
1294 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001295 len = tpa_info->len;
1296 mapping = tpa_info->mapping;
1297
1298 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1299 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1300
1301 if (agg_bufs) {
1302 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1303 return ERR_PTR(-EBUSY);
1304
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001305 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001306 cp_cons = NEXT_CMP(cp_cons);
1307 }
1308
1309 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1310 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1311 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1312 agg_bufs, (int)MAX_SKB_FRAGS);
1313 return NULL;
1314 }
1315
1316 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001317 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001318 if (!skb) {
1319 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1320 return NULL;
1321 }
1322 } else {
1323 u8 *new_data;
1324 dma_addr_t new_mapping;
1325
1326 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1327 if (!new_data) {
1328 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1329 return NULL;
1330 }
1331
1332 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001333 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001334 tpa_info->mapping = new_mapping;
1335
1336 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001337 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1338 bp->rx_buf_use_size, bp->rx_dir,
1339 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001340
1341 if (!skb) {
1342 kfree(data);
1343 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1344 return NULL;
1345 }
Michael Chanb3dba772017-02-06 16:55:35 -05001346 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001347 skb_put(skb, len);
1348 }
1349
1350 if (agg_bufs) {
1351 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1352 if (!skb) {
1353 /* Page reuse already handled by bnxt_rx_pages(). */
1354 return NULL;
1355 }
1356 }
1357 skb->protocol = eth_type_trans(skb, bp->dev);
1358
1359 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1360 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1361
Michael Chan8852ddb2016-06-06 02:37:16 -04001362 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1363 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001364 u16 vlan_proto = tpa_info->metadata >>
1365 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001366 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001367
Michael Chan8852ddb2016-06-06 02:37:16 -04001368 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001369 }
1370
1371 skb_checksum_none_assert(skb);
1372 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1373 skb->ip_summed = CHECKSUM_UNNECESSARY;
1374 skb->csum_level =
1375 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1376 }
1377
1378 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001379 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001380
1381 return skb;
1382}
1383
1384/* returns the following:
1385 * 1 - 1 packet successfully received
1386 * 0 - successful TPA_START, packet not completed yet
1387 * -EBUSY - completion ring does not have all the agg buffers yet
1388 * -ENOMEM - packet aborted due to out of memory
1389 * -EIO - packet aborted due to hw error indicated in BD
1390 */
1391static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001392 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001393{
1394 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001395 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001396 struct net_device *dev = bp->dev;
1397 struct rx_cmp *rxcmp;
1398 struct rx_cmp_ext *rxcmp1;
1399 u32 tmp_raw_cons = *raw_cons;
1400 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1401 struct bnxt_sw_rx_bd *rx_buf;
1402 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001403 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001404 dma_addr_t dma_addr;
1405 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001406 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001407 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001408 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001409
1410 rxcmp = (struct rx_cmp *)
1411 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1412
1413 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1414 cp_cons = RING_CMP(tmp_raw_cons);
1415 rxcmp1 = (struct rx_cmp_ext *)
1416 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1417
1418 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1419 return -EBUSY;
1420
1421 cmp_type = RX_CMP_TYPE(rxcmp);
1422
1423 prod = rxr->rx_prod;
1424
1425 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1426 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1427 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1428
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001429 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001430 goto next_rx_no_prod;
1431
1432 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1433 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1434 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001435 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001436
1437 if (unlikely(IS_ERR(skb)))
1438 return -EBUSY;
1439
1440 rc = -ENOMEM;
1441 if (likely(skb)) {
1442 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001443 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001444 rc = 1;
1445 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001446 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001447 goto next_rx_no_prod;
1448 }
1449
1450 cons = rxcmp->rx_cmp_opaque;
1451 rx_buf = &rxr->rx_buf_ring[cons];
1452 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001453 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001454 if (unlikely(cons != rxr->rx_next_cons)) {
1455 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1456
1457 bnxt_sched_reset(bp, rxr);
1458 return rc1;
1459 }
Michael Chan6bb19472017-02-06 16:55:32 -05001460 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001461
Michael Chanc61fb992017-02-06 16:55:36 -05001462 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1463 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001464
1465 if (agg_bufs) {
1466 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1467 return -EBUSY;
1468
1469 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001470 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001471 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001472 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001473
1474 rx_buf->data = NULL;
1475 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1476 bnxt_reuse_rx_data(rxr, cons, data);
1477 if (agg_bufs)
1478 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1479
1480 rc = -EIO;
1481 goto next_rx;
1482 }
1483
1484 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001485 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001486
Michael Chanc6d30e82017-02-06 16:55:42 -05001487 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1488 rc = 1;
1489 goto next_rx;
1490 }
1491
Michael Chanc0c050c2015-10-22 16:01:17 -04001492 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001493 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001494 bnxt_reuse_rx_data(rxr, cons, data);
1495 if (!skb) {
1496 rc = -ENOMEM;
1497 goto next_rx;
1498 }
1499 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001500 u32 payload;
1501
Michael Chanc6d30e82017-02-06 16:55:42 -05001502 if (rx_buf->data_ptr == data_ptr)
1503 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1504 else
1505 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001506 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001507 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001508 if (!skb) {
1509 rc = -ENOMEM;
1510 goto next_rx;
1511 }
1512 }
1513
1514 if (agg_bufs) {
1515 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1516 if (!skb) {
1517 rc = -ENOMEM;
1518 goto next_rx;
1519 }
1520 }
1521
1522 if (RX_CMP_HASH_VALID(rxcmp)) {
1523 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1524 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1525
1526 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1527 if (hash_type != 1 && hash_type != 3)
1528 type = PKT_HASH_TYPE_L3;
1529 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1530 }
1531
1532 skb->protocol = eth_type_trans(skb, dev);
1533
Michael Chan8852ddb2016-06-06 02:37:16 -04001534 if ((rxcmp1->rx_cmp_flags2 &
1535 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1536 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001537 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001538 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001539 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1540
Michael Chan8852ddb2016-06-06 02:37:16 -04001541 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001542 }
1543
1544 skb_checksum_none_assert(skb);
1545 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1546 if (dev->features & NETIF_F_RXCSUM) {
1547 skb->ip_summed = CHECKSUM_UNNECESSARY;
1548 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1549 }
1550 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001551 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1552 if (dev->features & NETIF_F_RXCSUM)
1553 cpr->rx_l4_csum_errors++;
1554 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001555 }
1556
1557 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001558 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001559 rc = 1;
1560
1561next_rx:
1562 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001563 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001564
1565next_rx_no_prod:
1566 *raw_cons = tmp_raw_cons;
1567
1568 return rc;
1569}
1570
Michael Chan4bb13ab2016-04-05 14:09:01 -04001571#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001572 ((data) & \
1573 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001574
Michael Chanc0c050c2015-10-22 16:01:17 -04001575static int bnxt_async_event_process(struct bnxt *bp,
1576 struct hwrm_async_event_cmpl *cmpl)
1577{
1578 u16 event_id = le16_to_cpu(cmpl->event_id);
1579
1580 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1581 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001582 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001583 u32 data1 = le32_to_cpu(cmpl->event_data1);
1584 struct bnxt_link_info *link_info = &bp->link_info;
1585
1586 if (BNXT_VF(bp))
1587 goto async_event_process_exit;
1588 if (data1 & 0x20000) {
1589 u16 fw_speed = link_info->force_link_speed;
1590 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1591
1592 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1593 speed);
1594 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001595 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001596 /* fall thru */
1597 }
Michael Chan87c374d2016-12-02 21:17:16 -05001598 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001599 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001600 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001601 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001602 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001603 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001604 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001605 u32 data1 = le32_to_cpu(cmpl->event_data1);
1606 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1607
1608 if (BNXT_VF(bp))
1609 break;
1610
1611 if (bp->pf.port_id != port_id)
1612 break;
1613
Michael Chan4bb13ab2016-04-05 14:09:01 -04001614 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1615 break;
1616 }
Michael Chan87c374d2016-12-02 21:17:16 -05001617 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001618 if (BNXT_PF(bp))
1619 goto async_event_process_exit;
1620 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1621 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001622 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001623 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001624 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001625 schedule_work(&bp->sp_task);
1626async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001627 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001628 return 0;
1629}
1630
1631static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1632{
1633 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1634 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1635 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1636 (struct hwrm_fwd_req_cmpl *)txcmp;
1637
1638 switch (cmpl_type) {
1639 case CMPL_BASE_TYPE_HWRM_DONE:
1640 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1641 if (seq_id == bp->hwrm_intr_seq_id)
1642 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1643 else
1644 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1645 break;
1646
1647 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1648 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1649
1650 if ((vf_id < bp->pf.first_vf_id) ||
1651 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1652 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1653 vf_id);
1654 return -EINVAL;
1655 }
1656
1657 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1658 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1659 schedule_work(&bp->sp_task);
1660 break;
1661
1662 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1663 bnxt_async_event_process(bp,
1664 (struct hwrm_async_event_cmpl *)txcmp);
1665
1666 default:
1667 break;
1668 }
1669
1670 return 0;
1671}
1672
1673static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1674{
1675 struct bnxt_napi *bnapi = dev_instance;
1676 struct bnxt *bp = bnapi->bp;
1677 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1678 u32 cons = RING_CMP(cpr->cp_raw_cons);
1679
1680 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1681 napi_schedule(&bnapi->napi);
1682 return IRQ_HANDLED;
1683}
1684
1685static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1686{
1687 u32 raw_cons = cpr->cp_raw_cons;
1688 u16 cons = RING_CMP(raw_cons);
1689 struct tx_cmp *txcmp;
1690
1691 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1692
1693 return TX_CMP_VALID(txcmp, raw_cons);
1694}
1695
Michael Chanc0c050c2015-10-22 16:01:17 -04001696static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1697{
1698 struct bnxt_napi *bnapi = dev_instance;
1699 struct bnxt *bp = bnapi->bp;
1700 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1701 u32 cons = RING_CMP(cpr->cp_raw_cons);
1702 u32 int_status;
1703
1704 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1705
1706 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001707 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001708 /* return if erroneous interrupt */
1709 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1710 return IRQ_NONE;
1711 }
1712
1713 /* disable ring IRQ */
1714 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1715
1716 /* Return here if interrupt is shared and is disabled. */
1717 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1718 return IRQ_HANDLED;
1719
1720 napi_schedule(&bnapi->napi);
1721 return IRQ_HANDLED;
1722}
1723
1724static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1725{
1726 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1727 u32 raw_cons = cpr->cp_raw_cons;
1728 u32 cons;
1729 int tx_pkts = 0;
1730 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001731 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001732 struct tx_cmp *txcmp;
1733
1734 while (1) {
1735 int rc;
1736
1737 cons = RING_CMP(raw_cons);
1738 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1739
1740 if (!TX_CMP_VALID(txcmp, raw_cons))
1741 break;
1742
Michael Chan67a95e22016-05-04 16:56:43 -04001743 /* The valid test of the entry must be done first before
1744 * reading any further.
1745 */
Michael Chanb67daab2016-05-15 03:04:51 -04001746 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001747 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1748 tx_pkts++;
1749 /* return full budget so NAPI will complete. */
1750 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1751 rx_pkts = budget;
1752 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001753 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001754 if (likely(rc >= 0))
1755 rx_pkts += rc;
1756 else if (rc == -EBUSY) /* partial completion */
1757 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001758 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1759 CMPL_BASE_TYPE_HWRM_DONE) ||
1760 (TX_CMP_TYPE(txcmp) ==
1761 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1762 (TX_CMP_TYPE(txcmp) ==
1763 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1764 bnxt_hwrm_handler(bp, txcmp);
1765 }
1766 raw_cons = NEXT_RAW_CMP(raw_cons);
1767
1768 if (rx_pkts == budget)
1769 break;
1770 }
1771
Michael Chan38413402017-02-06 16:55:43 -05001772 if (event & BNXT_TX_EVENT) {
1773 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1774 void __iomem *db = txr->tx_doorbell;
1775 u16 prod = txr->tx_prod;
1776
1777 /* Sync BD data before updating doorbell */
1778 wmb();
1779
1780 writel(DB_KEY_TX | prod, db);
1781 writel(DB_KEY_TX | prod, db);
1782 }
1783
Michael Chanc0c050c2015-10-22 16:01:17 -04001784 cpr->cp_raw_cons = raw_cons;
1785 /* ACK completion ring before freeing tx ring and producing new
1786 * buffers in rx/agg rings to prevent overflowing the completion
1787 * ring.
1788 */
1789 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1790
1791 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001792 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001793
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001794 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001795 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001796
1797 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1798 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001799 if (event & BNXT_AGG_EVENT) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001800 writel(DB_KEY_RX | rxr->rx_agg_prod,
1801 rxr->rx_agg_doorbell);
1802 writel(DB_KEY_RX | rxr->rx_agg_prod,
1803 rxr->rx_agg_doorbell);
1804 }
1805 }
1806 return rx_pkts;
1807}
1808
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001809static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1810{
1811 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1812 struct bnxt *bp = bnapi->bp;
1813 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1814 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1815 struct tx_cmp *txcmp;
1816 struct rx_cmp_ext *rxcmp1;
1817 u32 cp_cons, tmp_raw_cons;
1818 u32 raw_cons = cpr->cp_raw_cons;
1819 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001820 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001821
1822 while (1) {
1823 int rc;
1824
1825 cp_cons = RING_CMP(raw_cons);
1826 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1827
1828 if (!TX_CMP_VALID(txcmp, raw_cons))
1829 break;
1830
1831 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1832 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1833 cp_cons = RING_CMP(tmp_raw_cons);
1834 rxcmp1 = (struct rx_cmp_ext *)
1835 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1836
1837 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1838 break;
1839
1840 /* force an error to recycle the buffer */
1841 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1842 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1843
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001844 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001845 if (likely(rc == -EIO))
1846 rx_pkts++;
1847 else if (rc == -EBUSY) /* partial completion */
1848 break;
1849 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1850 CMPL_BASE_TYPE_HWRM_DONE)) {
1851 bnxt_hwrm_handler(bp, txcmp);
1852 } else {
1853 netdev_err(bp->dev,
1854 "Invalid completion received on special ring\n");
1855 }
1856 raw_cons = NEXT_RAW_CMP(raw_cons);
1857
1858 if (rx_pkts == budget)
1859 break;
1860 }
1861
1862 cpr->cp_raw_cons = raw_cons;
1863 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1864 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1865 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1866
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001867 if (event & BNXT_AGG_EVENT) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001868 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1869 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1870 }
1871
1872 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001873 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001874 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1875 }
1876 return rx_pkts;
1877}
1878
Michael Chanc0c050c2015-10-22 16:01:17 -04001879static int bnxt_poll(struct napi_struct *napi, int budget)
1880{
1881 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1882 struct bnxt *bp = bnapi->bp;
1883 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1884 int work_done = 0;
1885
Michael Chanc0c050c2015-10-22 16:01:17 -04001886 while (1) {
1887 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1888
1889 if (work_done >= budget)
1890 break;
1891
1892 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001893 if (napi_complete_done(napi, work_done))
1894 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1895 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001896 break;
1897 }
1898 }
1899 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001900 return work_done;
1901}
1902
Michael Chanc0c050c2015-10-22 16:01:17 -04001903static void bnxt_free_tx_skbs(struct bnxt *bp)
1904{
1905 int i, max_idx;
1906 struct pci_dev *pdev = bp->pdev;
1907
Michael Chanb6ab4b02016-01-02 23:44:59 -05001908 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001909 return;
1910
1911 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1912 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001913 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001914 int j;
1915
Michael Chanc0c050c2015-10-22 16:01:17 -04001916 for (j = 0; j < max_idx;) {
1917 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1918 struct sk_buff *skb = tx_buf->skb;
1919 int k, last;
1920
1921 if (!skb) {
1922 j++;
1923 continue;
1924 }
1925
1926 tx_buf->skb = NULL;
1927
1928 if (tx_buf->is_push) {
1929 dev_kfree_skb(skb);
1930 j += 2;
1931 continue;
1932 }
1933
1934 dma_unmap_single(&pdev->dev,
1935 dma_unmap_addr(tx_buf, mapping),
1936 skb_headlen(skb),
1937 PCI_DMA_TODEVICE);
1938
1939 last = tx_buf->nr_frags;
1940 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001941 for (k = 0; k < last; k++, j++) {
1942 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001943 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1944
Michael Chand612a572016-01-28 03:11:22 -05001945 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001946 dma_unmap_page(
1947 &pdev->dev,
1948 dma_unmap_addr(tx_buf, mapping),
1949 skb_frag_size(frag), PCI_DMA_TODEVICE);
1950 }
1951 dev_kfree_skb(skb);
1952 }
1953 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1954 }
1955}
1956
1957static void bnxt_free_rx_skbs(struct bnxt *bp)
1958{
1959 int i, max_idx, max_agg_idx;
1960 struct pci_dev *pdev = bp->pdev;
1961
Michael Chanb6ab4b02016-01-02 23:44:59 -05001962 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001963 return;
1964
1965 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1966 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1967 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001968 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001969 int j;
1970
Michael Chanc0c050c2015-10-22 16:01:17 -04001971 if (rxr->rx_tpa) {
1972 for (j = 0; j < MAX_TPA; j++) {
1973 struct bnxt_tpa_info *tpa_info =
1974 &rxr->rx_tpa[j];
1975 u8 *data = tpa_info->data;
1976
1977 if (!data)
1978 continue;
1979
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001980 dma_unmap_single_attrs(&pdev->dev,
1981 tpa_info->mapping,
1982 bp->rx_buf_use_size,
1983 bp->rx_dir,
1984 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001985
1986 tpa_info->data = NULL;
1987
1988 kfree(data);
1989 }
1990 }
1991
1992 for (j = 0; j < max_idx; j++) {
1993 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04001994 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05001995 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001996
1997 if (!data)
1998 continue;
1999
Michael Chanc0c050c2015-10-22 16:01:17 -04002000 rx_buf->data = NULL;
2001
Michael Chan3ed3a832017-03-28 19:47:31 -04002002 if (BNXT_RX_PAGE_MODE(bp)) {
2003 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002004 dma_unmap_page_attrs(&pdev->dev, mapping,
2005 PAGE_SIZE, bp->rx_dir,
2006 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002007 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002008 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002009 dma_unmap_single_attrs(&pdev->dev, mapping,
2010 bp->rx_buf_use_size,
2011 bp->rx_dir,
2012 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002013 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002014 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002015 }
2016
2017 for (j = 0; j < max_agg_idx; j++) {
2018 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2019 &rxr->rx_agg_ring[j];
2020 struct page *page = rx_agg_buf->page;
2021
2022 if (!page)
2023 continue;
2024
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002025 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2026 BNXT_RX_PAGE_SIZE,
2027 PCI_DMA_FROMDEVICE,
2028 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002029
2030 rx_agg_buf->page = NULL;
2031 __clear_bit(j, rxr->rx_agg_bmap);
2032
2033 __free_page(page);
2034 }
Michael Chan89d0a062016-04-25 02:30:51 -04002035 if (rxr->rx_page) {
2036 __free_page(rxr->rx_page);
2037 rxr->rx_page = NULL;
2038 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002039 }
2040}
2041
2042static void bnxt_free_skbs(struct bnxt *bp)
2043{
2044 bnxt_free_tx_skbs(bp);
2045 bnxt_free_rx_skbs(bp);
2046}
2047
2048static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2049{
2050 struct pci_dev *pdev = bp->pdev;
2051 int i;
2052
2053 for (i = 0; i < ring->nr_pages; i++) {
2054 if (!ring->pg_arr[i])
2055 continue;
2056
2057 dma_free_coherent(&pdev->dev, ring->page_size,
2058 ring->pg_arr[i], ring->dma_arr[i]);
2059
2060 ring->pg_arr[i] = NULL;
2061 }
2062 if (ring->pg_tbl) {
2063 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2064 ring->pg_tbl, ring->pg_tbl_map);
2065 ring->pg_tbl = NULL;
2066 }
2067 if (ring->vmem_size && *ring->vmem) {
2068 vfree(*ring->vmem);
2069 *ring->vmem = NULL;
2070 }
2071}
2072
2073static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2074{
2075 int i;
2076 struct pci_dev *pdev = bp->pdev;
2077
2078 if (ring->nr_pages > 1) {
2079 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2080 ring->nr_pages * 8,
2081 &ring->pg_tbl_map,
2082 GFP_KERNEL);
2083 if (!ring->pg_tbl)
2084 return -ENOMEM;
2085 }
2086
2087 for (i = 0; i < ring->nr_pages; i++) {
2088 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2089 ring->page_size,
2090 &ring->dma_arr[i],
2091 GFP_KERNEL);
2092 if (!ring->pg_arr[i])
2093 return -ENOMEM;
2094
2095 if (ring->nr_pages > 1)
2096 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2097 }
2098
2099 if (ring->vmem_size) {
2100 *ring->vmem = vzalloc(ring->vmem_size);
2101 if (!(*ring->vmem))
2102 return -ENOMEM;
2103 }
2104 return 0;
2105}
2106
2107static void bnxt_free_rx_rings(struct bnxt *bp)
2108{
2109 int i;
2110
Michael Chanb6ab4b02016-01-02 23:44:59 -05002111 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002112 return;
2113
2114 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002115 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002116 struct bnxt_ring_struct *ring;
2117
Michael Chanc6d30e82017-02-06 16:55:42 -05002118 if (rxr->xdp_prog)
2119 bpf_prog_put(rxr->xdp_prog);
2120
Michael Chanc0c050c2015-10-22 16:01:17 -04002121 kfree(rxr->rx_tpa);
2122 rxr->rx_tpa = NULL;
2123
2124 kfree(rxr->rx_agg_bmap);
2125 rxr->rx_agg_bmap = NULL;
2126
2127 ring = &rxr->rx_ring_struct;
2128 bnxt_free_ring(bp, ring);
2129
2130 ring = &rxr->rx_agg_ring_struct;
2131 bnxt_free_ring(bp, ring);
2132 }
2133}
2134
2135static int bnxt_alloc_rx_rings(struct bnxt *bp)
2136{
2137 int i, rc, agg_rings = 0, tpa_rings = 0;
2138
Michael Chanb6ab4b02016-01-02 23:44:59 -05002139 if (!bp->rx_ring)
2140 return -ENOMEM;
2141
Michael Chanc0c050c2015-10-22 16:01:17 -04002142 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2143 agg_rings = 1;
2144
2145 if (bp->flags & BNXT_FLAG_TPA)
2146 tpa_rings = 1;
2147
2148 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002149 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002150 struct bnxt_ring_struct *ring;
2151
Michael Chanc0c050c2015-10-22 16:01:17 -04002152 ring = &rxr->rx_ring_struct;
2153
2154 rc = bnxt_alloc_ring(bp, ring);
2155 if (rc)
2156 return rc;
2157
2158 if (agg_rings) {
2159 u16 mem_size;
2160
2161 ring = &rxr->rx_agg_ring_struct;
2162 rc = bnxt_alloc_ring(bp, ring);
2163 if (rc)
2164 return rc;
2165
2166 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2167 mem_size = rxr->rx_agg_bmap_size / 8;
2168 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2169 if (!rxr->rx_agg_bmap)
2170 return -ENOMEM;
2171
2172 if (tpa_rings) {
2173 rxr->rx_tpa = kcalloc(MAX_TPA,
2174 sizeof(struct bnxt_tpa_info),
2175 GFP_KERNEL);
2176 if (!rxr->rx_tpa)
2177 return -ENOMEM;
2178 }
2179 }
2180 }
2181 return 0;
2182}
2183
2184static void bnxt_free_tx_rings(struct bnxt *bp)
2185{
2186 int i;
2187 struct pci_dev *pdev = bp->pdev;
2188
Michael Chanb6ab4b02016-01-02 23:44:59 -05002189 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002190 return;
2191
2192 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002193 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002194 struct bnxt_ring_struct *ring;
2195
Michael Chanc0c050c2015-10-22 16:01:17 -04002196 if (txr->tx_push) {
2197 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2198 txr->tx_push, txr->tx_push_mapping);
2199 txr->tx_push = NULL;
2200 }
2201
2202 ring = &txr->tx_ring_struct;
2203
2204 bnxt_free_ring(bp, ring);
2205 }
2206}
2207
2208static int bnxt_alloc_tx_rings(struct bnxt *bp)
2209{
2210 int i, j, rc;
2211 struct pci_dev *pdev = bp->pdev;
2212
2213 bp->tx_push_size = 0;
2214 if (bp->tx_push_thresh) {
2215 int push_size;
2216
2217 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2218 bp->tx_push_thresh);
2219
Michael Chan4419dbe2016-02-10 17:33:49 -05002220 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002221 push_size = 0;
2222 bp->tx_push_thresh = 0;
2223 }
2224
2225 bp->tx_push_size = push_size;
2226 }
2227
2228 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002229 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002230 struct bnxt_ring_struct *ring;
2231
Michael Chanc0c050c2015-10-22 16:01:17 -04002232 ring = &txr->tx_ring_struct;
2233
2234 rc = bnxt_alloc_ring(bp, ring);
2235 if (rc)
2236 return rc;
2237
2238 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002239 dma_addr_t mapping;
2240
2241 /* One pre-allocated DMA buffer to backup
2242 * TX push operation
2243 */
2244 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2245 bp->tx_push_size,
2246 &txr->tx_push_mapping,
2247 GFP_KERNEL);
2248
2249 if (!txr->tx_push)
2250 return -ENOMEM;
2251
Michael Chanc0c050c2015-10-22 16:01:17 -04002252 mapping = txr->tx_push_mapping +
2253 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002254 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002255
Michael Chan4419dbe2016-02-10 17:33:49 -05002256 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002257 }
2258 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002259 if (i < bp->tx_nr_rings_xdp)
2260 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002261 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2262 j++;
2263 }
2264 return 0;
2265}
2266
2267static void bnxt_free_cp_rings(struct bnxt *bp)
2268{
2269 int i;
2270
2271 if (!bp->bnapi)
2272 return;
2273
2274 for (i = 0; i < bp->cp_nr_rings; i++) {
2275 struct bnxt_napi *bnapi = bp->bnapi[i];
2276 struct bnxt_cp_ring_info *cpr;
2277 struct bnxt_ring_struct *ring;
2278
2279 if (!bnapi)
2280 continue;
2281
2282 cpr = &bnapi->cp_ring;
2283 ring = &cpr->cp_ring_struct;
2284
2285 bnxt_free_ring(bp, ring);
2286 }
2287}
2288
2289static int bnxt_alloc_cp_rings(struct bnxt *bp)
2290{
2291 int i, rc;
2292
2293 for (i = 0; i < bp->cp_nr_rings; i++) {
2294 struct bnxt_napi *bnapi = bp->bnapi[i];
2295 struct bnxt_cp_ring_info *cpr;
2296 struct bnxt_ring_struct *ring;
2297
2298 if (!bnapi)
2299 continue;
2300
2301 cpr = &bnapi->cp_ring;
2302 ring = &cpr->cp_ring_struct;
2303
2304 rc = bnxt_alloc_ring(bp, ring);
2305 if (rc)
2306 return rc;
2307 }
2308 return 0;
2309}
2310
2311static void bnxt_init_ring_struct(struct bnxt *bp)
2312{
2313 int i;
2314
2315 for (i = 0; i < bp->cp_nr_rings; i++) {
2316 struct bnxt_napi *bnapi = bp->bnapi[i];
2317 struct bnxt_cp_ring_info *cpr;
2318 struct bnxt_rx_ring_info *rxr;
2319 struct bnxt_tx_ring_info *txr;
2320 struct bnxt_ring_struct *ring;
2321
2322 if (!bnapi)
2323 continue;
2324
2325 cpr = &bnapi->cp_ring;
2326 ring = &cpr->cp_ring_struct;
2327 ring->nr_pages = bp->cp_nr_pages;
2328 ring->page_size = HW_CMPD_RING_SIZE;
2329 ring->pg_arr = (void **)cpr->cp_desc_ring;
2330 ring->dma_arr = cpr->cp_desc_mapping;
2331 ring->vmem_size = 0;
2332
Michael Chanb6ab4b02016-01-02 23:44:59 -05002333 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002334 if (!rxr)
2335 goto skip_rx;
2336
Michael Chanc0c050c2015-10-22 16:01:17 -04002337 ring = &rxr->rx_ring_struct;
2338 ring->nr_pages = bp->rx_nr_pages;
2339 ring->page_size = HW_RXBD_RING_SIZE;
2340 ring->pg_arr = (void **)rxr->rx_desc_ring;
2341 ring->dma_arr = rxr->rx_desc_mapping;
2342 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2343 ring->vmem = (void **)&rxr->rx_buf_ring;
2344
2345 ring = &rxr->rx_agg_ring_struct;
2346 ring->nr_pages = bp->rx_agg_nr_pages;
2347 ring->page_size = HW_RXBD_RING_SIZE;
2348 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2349 ring->dma_arr = rxr->rx_agg_desc_mapping;
2350 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2351 ring->vmem = (void **)&rxr->rx_agg_ring;
2352
Michael Chan3b2b7d92016-01-02 23:45:00 -05002353skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002354 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002355 if (!txr)
2356 continue;
2357
Michael Chanc0c050c2015-10-22 16:01:17 -04002358 ring = &txr->tx_ring_struct;
2359 ring->nr_pages = bp->tx_nr_pages;
2360 ring->page_size = HW_RXBD_RING_SIZE;
2361 ring->pg_arr = (void **)txr->tx_desc_ring;
2362 ring->dma_arr = txr->tx_desc_mapping;
2363 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2364 ring->vmem = (void **)&txr->tx_buf_ring;
2365 }
2366}
2367
2368static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2369{
2370 int i;
2371 u32 prod;
2372 struct rx_bd **rx_buf_ring;
2373
2374 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2375 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2376 int j;
2377 struct rx_bd *rxbd;
2378
2379 rxbd = rx_buf_ring[i];
2380 if (!rxbd)
2381 continue;
2382
2383 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2384 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2385 rxbd->rx_bd_opaque = prod;
2386 }
2387 }
2388}
2389
2390static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2391{
2392 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002393 struct bnxt_rx_ring_info *rxr;
2394 struct bnxt_ring_struct *ring;
2395 u32 prod, type;
2396 int i;
2397
Michael Chanc0c050c2015-10-22 16:01:17 -04002398 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2399 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2400
2401 if (NET_IP_ALIGN == 2)
2402 type |= RX_BD_FLAGS_SOP;
2403
Michael Chanb6ab4b02016-01-02 23:44:59 -05002404 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002405 ring = &rxr->rx_ring_struct;
2406 bnxt_init_rxbd_pages(ring, type);
2407
Michael Chanc6d30e82017-02-06 16:55:42 -05002408 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2409 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2410 if (IS_ERR(rxr->xdp_prog)) {
2411 int rc = PTR_ERR(rxr->xdp_prog);
2412
2413 rxr->xdp_prog = NULL;
2414 return rc;
2415 }
2416 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002417 prod = rxr->rx_prod;
2418 for (i = 0; i < bp->rx_ring_size; i++) {
2419 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2420 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2421 ring_nr, i, bp->rx_ring_size);
2422 break;
2423 }
2424 prod = NEXT_RX(prod);
2425 }
2426 rxr->rx_prod = prod;
2427 ring->fw_ring_id = INVALID_HW_RING_ID;
2428
Michael Chanedd0c2c2015-12-27 18:19:19 -05002429 ring = &rxr->rx_agg_ring_struct;
2430 ring->fw_ring_id = INVALID_HW_RING_ID;
2431
Michael Chanc0c050c2015-10-22 16:01:17 -04002432 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2433 return 0;
2434
Michael Chan2839f282016-04-25 02:30:50 -04002435 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002436 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2437
2438 bnxt_init_rxbd_pages(ring, type);
2439
2440 prod = rxr->rx_agg_prod;
2441 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2442 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2443 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2444 ring_nr, i, bp->rx_ring_size);
2445 break;
2446 }
2447 prod = NEXT_RX_AGG(prod);
2448 }
2449 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002450
2451 if (bp->flags & BNXT_FLAG_TPA) {
2452 if (rxr->rx_tpa) {
2453 u8 *data;
2454 dma_addr_t mapping;
2455
2456 for (i = 0; i < MAX_TPA; i++) {
2457 data = __bnxt_alloc_rx_data(bp, &mapping,
2458 GFP_KERNEL);
2459 if (!data)
2460 return -ENOMEM;
2461
2462 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002463 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002464 rxr->rx_tpa[i].mapping = mapping;
2465 }
2466 } else {
2467 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2468 return -ENOMEM;
2469 }
2470 }
2471
2472 return 0;
2473}
2474
Sankar Patchineelam22479252017-03-28 19:47:29 -04002475static void bnxt_init_cp_rings(struct bnxt *bp)
2476{
2477 int i;
2478
2479 for (i = 0; i < bp->cp_nr_rings; i++) {
2480 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2481 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2482
2483 ring->fw_ring_id = INVALID_HW_RING_ID;
2484 }
2485}
2486
Michael Chanc0c050c2015-10-22 16:01:17 -04002487static int bnxt_init_rx_rings(struct bnxt *bp)
2488{
2489 int i, rc = 0;
2490
Michael Chanc61fb992017-02-06 16:55:36 -05002491 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002492 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2493 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002494 } else {
2495 bp->rx_offset = BNXT_RX_OFFSET;
2496 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2497 }
Michael Chanb3dba772017-02-06 16:55:35 -05002498
Michael Chanc0c050c2015-10-22 16:01:17 -04002499 for (i = 0; i < bp->rx_nr_rings; i++) {
2500 rc = bnxt_init_one_rx_ring(bp, i);
2501 if (rc)
2502 break;
2503 }
2504
2505 return rc;
2506}
2507
2508static int bnxt_init_tx_rings(struct bnxt *bp)
2509{
2510 u16 i;
2511
2512 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2513 MAX_SKB_FRAGS + 1);
2514
2515 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002516 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002517 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2518
2519 ring->fw_ring_id = INVALID_HW_RING_ID;
2520 }
2521
2522 return 0;
2523}
2524
2525static void bnxt_free_ring_grps(struct bnxt *bp)
2526{
2527 kfree(bp->grp_info);
2528 bp->grp_info = NULL;
2529}
2530
2531static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2532{
2533 int i;
2534
2535 if (irq_re_init) {
2536 bp->grp_info = kcalloc(bp->cp_nr_rings,
2537 sizeof(struct bnxt_ring_grp_info),
2538 GFP_KERNEL);
2539 if (!bp->grp_info)
2540 return -ENOMEM;
2541 }
2542 for (i = 0; i < bp->cp_nr_rings; i++) {
2543 if (irq_re_init)
2544 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2545 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2546 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2547 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2548 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2549 }
2550 return 0;
2551}
2552
2553static void bnxt_free_vnics(struct bnxt *bp)
2554{
2555 kfree(bp->vnic_info);
2556 bp->vnic_info = NULL;
2557 bp->nr_vnics = 0;
2558}
2559
2560static int bnxt_alloc_vnics(struct bnxt *bp)
2561{
2562 int num_vnics = 1;
2563
2564#ifdef CONFIG_RFS_ACCEL
2565 if (bp->flags & BNXT_FLAG_RFS)
2566 num_vnics += bp->rx_nr_rings;
2567#endif
2568
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002569 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2570 num_vnics++;
2571
Michael Chanc0c050c2015-10-22 16:01:17 -04002572 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2573 GFP_KERNEL);
2574 if (!bp->vnic_info)
2575 return -ENOMEM;
2576
2577 bp->nr_vnics = num_vnics;
2578 return 0;
2579}
2580
2581static void bnxt_init_vnics(struct bnxt *bp)
2582{
2583 int i;
2584
2585 for (i = 0; i < bp->nr_vnics; i++) {
2586 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2587
2588 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002589 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2590 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002591 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2592
2593 if (bp->vnic_info[i].rss_hash_key) {
2594 if (i == 0)
2595 prandom_bytes(vnic->rss_hash_key,
2596 HW_HASH_KEY_SIZE);
2597 else
2598 memcpy(vnic->rss_hash_key,
2599 bp->vnic_info[0].rss_hash_key,
2600 HW_HASH_KEY_SIZE);
2601 }
2602 }
2603}
2604
2605static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2606{
2607 int pages;
2608
2609 pages = ring_size / desc_per_pg;
2610
2611 if (!pages)
2612 return 1;
2613
2614 pages++;
2615
2616 while (pages & (pages - 1))
2617 pages++;
2618
2619 return pages;
2620}
2621
Michael Chanc6d30e82017-02-06 16:55:42 -05002622void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002623{
2624 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002625 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2626 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002627 if (bp->dev->features & NETIF_F_LRO)
2628 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002629 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002630 bp->flags |= BNXT_FLAG_GRO;
2631}
2632
2633/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2634 * be set on entry.
2635 */
2636void bnxt_set_ring_params(struct bnxt *bp)
2637{
2638 u32 ring_size, rx_size, rx_space;
2639 u32 agg_factor = 0, agg_ring_size = 0;
2640
2641 /* 8 for CRC and VLAN */
2642 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2643
2644 rx_space = rx_size + NET_SKB_PAD +
2645 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2646
2647 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2648 ring_size = bp->rx_ring_size;
2649 bp->rx_agg_ring_size = 0;
2650 bp->rx_agg_nr_pages = 0;
2651
2652 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002653 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002654
2655 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002656 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002657 u32 jumbo_factor;
2658
2659 bp->flags |= BNXT_FLAG_JUMBO;
2660 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2661 if (jumbo_factor > agg_factor)
2662 agg_factor = jumbo_factor;
2663 }
2664 agg_ring_size = ring_size * agg_factor;
2665
2666 if (agg_ring_size) {
2667 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2668 RX_DESC_CNT);
2669 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2670 u32 tmp = agg_ring_size;
2671
2672 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2673 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2674 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2675 tmp, agg_ring_size);
2676 }
2677 bp->rx_agg_ring_size = agg_ring_size;
2678 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2679 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2680 rx_space = rx_size + NET_SKB_PAD +
2681 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2682 }
2683
2684 bp->rx_buf_use_size = rx_size;
2685 bp->rx_buf_size = rx_space;
2686
2687 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2688 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2689
2690 ring_size = bp->tx_ring_size;
2691 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2692 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2693
2694 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2695 bp->cp_ring_size = ring_size;
2696
2697 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2698 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2699 bp->cp_nr_pages = MAX_CP_PAGES;
2700 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2701 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2702 ring_size, bp->cp_ring_size);
2703 }
2704 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2705 bp->cp_ring_mask = bp->cp_bit - 1;
2706}
2707
Michael Chanc61fb992017-02-06 16:55:36 -05002708int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002709{
Michael Chanc61fb992017-02-06 16:55:36 -05002710 if (page_mode) {
2711 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2712 return -EOPNOTSUPP;
2713 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2714 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2715 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2716 bp->dev->hw_features &= ~NETIF_F_LRO;
2717 bp->dev->features &= ~NETIF_F_LRO;
2718 bp->rx_dir = DMA_BIDIRECTIONAL;
2719 bp->rx_skb_func = bnxt_rx_page_skb;
2720 } else {
2721 bp->dev->max_mtu = BNXT_MAX_MTU;
2722 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2723 bp->rx_dir = DMA_FROM_DEVICE;
2724 bp->rx_skb_func = bnxt_rx_skb;
2725 }
Michael Chan6bb19472017-02-06 16:55:32 -05002726 return 0;
2727}
2728
Michael Chanc0c050c2015-10-22 16:01:17 -04002729static void bnxt_free_vnic_attributes(struct bnxt *bp)
2730{
2731 int i;
2732 struct bnxt_vnic_info *vnic;
2733 struct pci_dev *pdev = bp->pdev;
2734
2735 if (!bp->vnic_info)
2736 return;
2737
2738 for (i = 0; i < bp->nr_vnics; i++) {
2739 vnic = &bp->vnic_info[i];
2740
2741 kfree(vnic->fw_grp_ids);
2742 vnic->fw_grp_ids = NULL;
2743
2744 kfree(vnic->uc_list);
2745 vnic->uc_list = NULL;
2746
2747 if (vnic->mc_list) {
2748 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2749 vnic->mc_list, vnic->mc_list_mapping);
2750 vnic->mc_list = NULL;
2751 }
2752
2753 if (vnic->rss_table) {
2754 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2755 vnic->rss_table,
2756 vnic->rss_table_dma_addr);
2757 vnic->rss_table = NULL;
2758 }
2759
2760 vnic->rss_hash_key = NULL;
2761 vnic->flags = 0;
2762 }
2763}
2764
2765static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2766{
2767 int i, rc = 0, size;
2768 struct bnxt_vnic_info *vnic;
2769 struct pci_dev *pdev = bp->pdev;
2770 int max_rings;
2771
2772 for (i = 0; i < bp->nr_vnics; i++) {
2773 vnic = &bp->vnic_info[i];
2774
2775 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2776 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2777
2778 if (mem_size > 0) {
2779 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2780 if (!vnic->uc_list) {
2781 rc = -ENOMEM;
2782 goto out;
2783 }
2784 }
2785 }
2786
2787 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2788 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2789 vnic->mc_list =
2790 dma_alloc_coherent(&pdev->dev,
2791 vnic->mc_list_size,
2792 &vnic->mc_list_mapping,
2793 GFP_KERNEL);
2794 if (!vnic->mc_list) {
2795 rc = -ENOMEM;
2796 goto out;
2797 }
2798 }
2799
2800 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2801 max_rings = bp->rx_nr_rings;
2802 else
2803 max_rings = 1;
2804
2805 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2806 if (!vnic->fw_grp_ids) {
2807 rc = -ENOMEM;
2808 goto out;
2809 }
2810
Michael Chanae10ae72016-12-29 12:13:38 -05002811 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2812 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2813 continue;
2814
Michael Chanc0c050c2015-10-22 16:01:17 -04002815 /* Allocate rss table and hash key */
2816 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2817 &vnic->rss_table_dma_addr,
2818 GFP_KERNEL);
2819 if (!vnic->rss_table) {
2820 rc = -ENOMEM;
2821 goto out;
2822 }
2823
2824 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2825
2826 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2827 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2828 }
2829 return 0;
2830
2831out:
2832 return rc;
2833}
2834
2835static void bnxt_free_hwrm_resources(struct bnxt *bp)
2836{
2837 struct pci_dev *pdev = bp->pdev;
2838
2839 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2840 bp->hwrm_cmd_resp_dma_addr);
2841
2842 bp->hwrm_cmd_resp_addr = NULL;
2843 if (bp->hwrm_dbg_resp_addr) {
2844 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2845 bp->hwrm_dbg_resp_addr,
2846 bp->hwrm_dbg_resp_dma_addr);
2847
2848 bp->hwrm_dbg_resp_addr = NULL;
2849 }
2850}
2851
2852static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2853{
2854 struct pci_dev *pdev = bp->pdev;
2855
2856 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2857 &bp->hwrm_cmd_resp_dma_addr,
2858 GFP_KERNEL);
2859 if (!bp->hwrm_cmd_resp_addr)
2860 return -ENOMEM;
2861 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2862 HWRM_DBG_REG_BUF_SIZE,
2863 &bp->hwrm_dbg_resp_dma_addr,
2864 GFP_KERNEL);
2865 if (!bp->hwrm_dbg_resp_addr)
2866 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2867
2868 return 0;
2869}
2870
Deepak Khungare605db82017-05-29 19:06:04 -04002871static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2872{
2873 if (bp->hwrm_short_cmd_req_addr) {
2874 struct pci_dev *pdev = bp->pdev;
2875
2876 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2877 bp->hwrm_short_cmd_req_addr,
2878 bp->hwrm_short_cmd_req_dma_addr);
2879 bp->hwrm_short_cmd_req_addr = NULL;
2880 }
2881}
2882
2883static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2884{
2885 struct pci_dev *pdev = bp->pdev;
2886
2887 bp->hwrm_short_cmd_req_addr =
2888 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2889 &bp->hwrm_short_cmd_req_dma_addr,
2890 GFP_KERNEL);
2891 if (!bp->hwrm_short_cmd_req_addr)
2892 return -ENOMEM;
2893
2894 return 0;
2895}
2896
Michael Chanc0c050c2015-10-22 16:01:17 -04002897static void bnxt_free_stats(struct bnxt *bp)
2898{
2899 u32 size, i;
2900 struct pci_dev *pdev = bp->pdev;
2901
Michael Chan3bdf56c2016-03-07 15:38:45 -05002902 if (bp->hw_rx_port_stats) {
2903 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2904 bp->hw_rx_port_stats,
2905 bp->hw_rx_port_stats_map);
2906 bp->hw_rx_port_stats = NULL;
2907 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2908 }
2909
Michael Chanc0c050c2015-10-22 16:01:17 -04002910 if (!bp->bnapi)
2911 return;
2912
2913 size = sizeof(struct ctx_hw_stats);
2914
2915 for (i = 0; i < bp->cp_nr_rings; i++) {
2916 struct bnxt_napi *bnapi = bp->bnapi[i];
2917 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2918
2919 if (cpr->hw_stats) {
2920 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2921 cpr->hw_stats_map);
2922 cpr->hw_stats = NULL;
2923 }
2924 }
2925}
2926
2927static int bnxt_alloc_stats(struct bnxt *bp)
2928{
2929 u32 size, i;
2930 struct pci_dev *pdev = bp->pdev;
2931
2932 size = sizeof(struct ctx_hw_stats);
2933
2934 for (i = 0; i < bp->cp_nr_rings; i++) {
2935 struct bnxt_napi *bnapi = bp->bnapi[i];
2936 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2937
2938 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2939 &cpr->hw_stats_map,
2940 GFP_KERNEL);
2941 if (!cpr->hw_stats)
2942 return -ENOMEM;
2943
2944 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2945 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002946
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002947 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002948 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2949 sizeof(struct tx_port_stats) + 1024;
2950
2951 bp->hw_rx_port_stats =
2952 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2953 &bp->hw_rx_port_stats_map,
2954 GFP_KERNEL);
2955 if (!bp->hw_rx_port_stats)
2956 return -ENOMEM;
2957
2958 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2959 512;
2960 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2961 sizeof(struct rx_port_stats) + 512;
2962 bp->flags |= BNXT_FLAG_PORT_STATS;
2963 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002964 return 0;
2965}
2966
2967static void bnxt_clear_ring_indices(struct bnxt *bp)
2968{
2969 int i;
2970
2971 if (!bp->bnapi)
2972 return;
2973
2974 for (i = 0; i < bp->cp_nr_rings; i++) {
2975 struct bnxt_napi *bnapi = bp->bnapi[i];
2976 struct bnxt_cp_ring_info *cpr;
2977 struct bnxt_rx_ring_info *rxr;
2978 struct bnxt_tx_ring_info *txr;
2979
2980 if (!bnapi)
2981 continue;
2982
2983 cpr = &bnapi->cp_ring;
2984 cpr->cp_raw_cons = 0;
2985
Michael Chanb6ab4b02016-01-02 23:44:59 -05002986 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002987 if (txr) {
2988 txr->tx_prod = 0;
2989 txr->tx_cons = 0;
2990 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002991
Michael Chanb6ab4b02016-01-02 23:44:59 -05002992 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002993 if (rxr) {
2994 rxr->rx_prod = 0;
2995 rxr->rx_agg_prod = 0;
2996 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002997 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002998 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002999 }
3000}
3001
3002static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3003{
3004#ifdef CONFIG_RFS_ACCEL
3005 int i;
3006
3007 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3008 * safe to delete the hash table.
3009 */
3010 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3011 struct hlist_head *head;
3012 struct hlist_node *tmp;
3013 struct bnxt_ntuple_filter *fltr;
3014
3015 head = &bp->ntp_fltr_hash_tbl[i];
3016 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3017 hlist_del(&fltr->hash);
3018 kfree(fltr);
3019 }
3020 }
3021 if (irq_reinit) {
3022 kfree(bp->ntp_fltr_bmap);
3023 bp->ntp_fltr_bmap = NULL;
3024 }
3025 bp->ntp_fltr_count = 0;
3026#endif
3027}
3028
3029static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3030{
3031#ifdef CONFIG_RFS_ACCEL
3032 int i, rc = 0;
3033
3034 if (!(bp->flags & BNXT_FLAG_RFS))
3035 return 0;
3036
3037 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3038 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3039
3040 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003041 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3042 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003043 GFP_KERNEL);
3044
3045 if (!bp->ntp_fltr_bmap)
3046 rc = -ENOMEM;
3047
3048 return rc;
3049#else
3050 return 0;
3051#endif
3052}
3053
3054static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3055{
3056 bnxt_free_vnic_attributes(bp);
3057 bnxt_free_tx_rings(bp);
3058 bnxt_free_rx_rings(bp);
3059 bnxt_free_cp_rings(bp);
3060 bnxt_free_ntp_fltrs(bp, irq_re_init);
3061 if (irq_re_init) {
3062 bnxt_free_stats(bp);
3063 bnxt_free_ring_grps(bp);
3064 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003065 kfree(bp->tx_ring_map);
3066 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003067 kfree(bp->tx_ring);
3068 bp->tx_ring = NULL;
3069 kfree(bp->rx_ring);
3070 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003071 kfree(bp->bnapi);
3072 bp->bnapi = NULL;
3073 } else {
3074 bnxt_clear_ring_indices(bp);
3075 }
3076}
3077
3078static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3079{
Michael Chan01657bc2016-01-02 23:45:03 -05003080 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003081 void *bnapi;
3082
3083 if (irq_re_init) {
3084 /* Allocate bnapi mem pointer array and mem block for
3085 * all queues
3086 */
3087 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3088 bp->cp_nr_rings);
3089 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3090 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3091 if (!bnapi)
3092 return -ENOMEM;
3093
3094 bp->bnapi = bnapi;
3095 bnapi += arr_size;
3096 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3097 bp->bnapi[i] = bnapi;
3098 bp->bnapi[i]->index = i;
3099 bp->bnapi[i]->bp = bp;
3100 }
3101
Michael Chanb6ab4b02016-01-02 23:44:59 -05003102 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3103 sizeof(struct bnxt_rx_ring_info),
3104 GFP_KERNEL);
3105 if (!bp->rx_ring)
3106 return -ENOMEM;
3107
3108 for (i = 0; i < bp->rx_nr_rings; i++) {
3109 bp->rx_ring[i].bnapi = bp->bnapi[i];
3110 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3111 }
3112
3113 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3114 sizeof(struct bnxt_tx_ring_info),
3115 GFP_KERNEL);
3116 if (!bp->tx_ring)
3117 return -ENOMEM;
3118
Michael Chana960dec2017-02-06 16:55:39 -05003119 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3120 GFP_KERNEL);
3121
3122 if (!bp->tx_ring_map)
3123 return -ENOMEM;
3124
Michael Chan01657bc2016-01-02 23:45:03 -05003125 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3126 j = 0;
3127 else
3128 j = bp->rx_nr_rings;
3129
3130 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3131 bp->tx_ring[i].bnapi = bp->bnapi[j];
3132 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003133 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003134 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003135 bp->tx_ring[i].txq_index = i -
3136 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003137 bp->bnapi[j]->tx_int = bnxt_tx_int;
3138 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003139 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003140 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3141 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003142 }
3143
Michael Chanc0c050c2015-10-22 16:01:17 -04003144 rc = bnxt_alloc_stats(bp);
3145 if (rc)
3146 goto alloc_mem_err;
3147
3148 rc = bnxt_alloc_ntp_fltrs(bp);
3149 if (rc)
3150 goto alloc_mem_err;
3151
3152 rc = bnxt_alloc_vnics(bp);
3153 if (rc)
3154 goto alloc_mem_err;
3155 }
3156
3157 bnxt_init_ring_struct(bp);
3158
3159 rc = bnxt_alloc_rx_rings(bp);
3160 if (rc)
3161 goto alloc_mem_err;
3162
3163 rc = bnxt_alloc_tx_rings(bp);
3164 if (rc)
3165 goto alloc_mem_err;
3166
3167 rc = bnxt_alloc_cp_rings(bp);
3168 if (rc)
3169 goto alloc_mem_err;
3170
3171 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3172 BNXT_VNIC_UCAST_FLAG;
3173 rc = bnxt_alloc_vnic_attributes(bp);
3174 if (rc)
3175 goto alloc_mem_err;
3176 return 0;
3177
3178alloc_mem_err:
3179 bnxt_free_mem(bp, true);
3180 return rc;
3181}
3182
Michael Chan9d8bc092016-12-29 12:13:33 -05003183static void bnxt_disable_int(struct bnxt *bp)
3184{
3185 int i;
3186
3187 if (!bp->bnapi)
3188 return;
3189
3190 for (i = 0; i < bp->cp_nr_rings; i++) {
3191 struct bnxt_napi *bnapi = bp->bnapi[i];
3192 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003193 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003194
Michael Chandaf1f1e2017-02-20 19:25:17 -05003195 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3196 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003197 }
3198}
3199
3200static void bnxt_disable_int_sync(struct bnxt *bp)
3201{
3202 int i;
3203
3204 atomic_inc(&bp->intr_sem);
3205
3206 bnxt_disable_int(bp);
3207 for (i = 0; i < bp->cp_nr_rings; i++)
3208 synchronize_irq(bp->irq_tbl[i].vector);
3209}
3210
3211static void bnxt_enable_int(struct bnxt *bp)
3212{
3213 int i;
3214
3215 atomic_set(&bp->intr_sem, 0);
3216 for (i = 0; i < bp->cp_nr_rings; i++) {
3217 struct bnxt_napi *bnapi = bp->bnapi[i];
3218 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3219
3220 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3221 }
3222}
3223
Michael Chanc0c050c2015-10-22 16:01:17 -04003224void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3225 u16 cmpl_ring, u16 target_id)
3226{
Michael Chana8643e12016-02-26 04:00:05 -05003227 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003228
Michael Chana8643e12016-02-26 04:00:05 -05003229 req->req_type = cpu_to_le16(req_type);
3230 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3231 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003232 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3233}
3234
Michael Chanfbfbc482016-02-26 04:00:07 -05003235static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3236 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003237{
Michael Chana11fa2b2016-05-15 03:04:47 -04003238 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003239 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003240 u32 *data = msg;
3241 __le32 *resp_len, *valid;
3242 u16 cp_ring_id, len = 0;
3243 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003244 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003245
Michael Chana8643e12016-02-26 04:00:05 -05003246 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003247 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003248 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003249 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3250
Deepak Khungare605db82017-05-29 19:06:04 -04003251 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3252 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3253 struct hwrm_short_input short_input = {0};
3254
3255 memcpy(short_cmd_req, req, msg_len);
3256 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3257 msg_len);
3258
3259 short_input.req_type = req->req_type;
3260 short_input.signature =
3261 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3262 short_input.size = cpu_to_le16(msg_len);
3263 short_input.req_addr =
3264 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3265
3266 data = (u32 *)&short_input;
3267 msg_len = sizeof(short_input);
3268
3269 /* Sync memory write before updating doorbell */
3270 wmb();
3271
3272 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3273 }
3274
Michael Chanc0c050c2015-10-22 16:01:17 -04003275 /* Write request msg to hwrm channel */
3276 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3277
Deepak Khungare605db82017-05-29 19:06:04 -04003278 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003279 writel(0, bp->bar0 + i);
3280
Michael Chanc0c050c2015-10-22 16:01:17 -04003281 /* currently supports only one outstanding message */
3282 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003283 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003284
3285 /* Ring channel doorbell */
3286 writel(1, bp->bar0 + 0x100);
3287
Michael Chanff4fe812016-02-26 04:00:04 -05003288 if (!timeout)
3289 timeout = DFLT_HWRM_CMD_TIMEOUT;
3290
Michael Chanc0c050c2015-10-22 16:01:17 -04003291 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003292 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003293 if (intr_process) {
3294 /* Wait until hwrm response cmpl interrupt is processed */
3295 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003296 i++ < tmo_count) {
3297 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003298 }
3299
3300 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3301 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003302 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003303 return -1;
3304 }
3305 } else {
3306 /* Check if response len is updated */
3307 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003308 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003309 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3310 HWRM_RESP_LEN_SFT;
3311 if (len)
3312 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003313 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003314 }
3315
Michael Chana11fa2b2016-05-15 03:04:47 -04003316 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003317 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003318 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003319 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003320 return -1;
3321 }
3322
3323 /* Last word of resp contains valid bit */
3324 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003325 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003326 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3327 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003328 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003329 }
3330
Michael Chana11fa2b2016-05-15 03:04:47 -04003331 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003332 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003333 timeout, le16_to_cpu(req->req_type),
3334 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003335 return -1;
3336 }
3337 }
3338
3339 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003340 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003341 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3342 le16_to_cpu(resp->req_type),
3343 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003344 return rc;
3345}
3346
3347int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3348{
3349 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003350}
3351
3352int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3353{
3354 int rc;
3355
3356 mutex_lock(&bp->hwrm_cmd_lock);
3357 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3358 mutex_unlock(&bp->hwrm_cmd_lock);
3359 return rc;
3360}
3361
Michael Chan90e209212016-02-26 04:00:08 -05003362int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3363 int timeout)
3364{
3365 int rc;
3366
3367 mutex_lock(&bp->hwrm_cmd_lock);
3368 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3369 mutex_unlock(&bp->hwrm_cmd_lock);
3370 return rc;
3371}
3372
Michael Chana1653b12016-12-07 00:26:20 -05003373int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3374 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003375{
3376 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003377 DECLARE_BITMAP(async_events_bmap, 256);
3378 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003379 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003380
3381 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3382
3383 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003384 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003385
Michael Chan25be8622016-04-05 14:09:00 -04003386 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3387 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3388 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3389
Michael Chana1653b12016-12-07 00:26:20 -05003390 if (bmap && bmap_size) {
3391 for (i = 0; i < bmap_size; i++) {
3392 if (test_bit(i, bmap))
3393 __set_bit(i, async_events_bmap);
3394 }
3395 }
3396
Michael Chan25be8622016-04-05 14:09:00 -04003397 for (i = 0; i < 8; i++)
3398 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3399
Michael Chana1653b12016-12-07 00:26:20 -05003400 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3401}
3402
3403static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3404{
3405 struct hwrm_func_drv_rgtr_input req = {0};
3406
3407 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3408
3409 req.enables =
3410 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3411 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3412
Michael Chan11f15ed2016-04-05 14:08:55 -04003413 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003414 req.ver_maj = DRV_VER_MAJ;
3415 req.ver_min = DRV_VER_MIN;
3416 req.ver_upd = DRV_VER_UPD;
3417
3418 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003419 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003420 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003421 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003422
Michael Chande68f5de2015-12-09 19:35:41 -05003423 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003424 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3425 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3426
Michael Chande68f5de2015-12-09 19:35:41 -05003427 for (i = 0; i < 8; i++)
3428 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3429
Michael Chanc0c050c2015-10-22 16:01:17 -04003430 req.enables |=
3431 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3432 }
3433
3434 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3435}
3436
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003437static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3438{
3439 struct hwrm_func_drv_unrgtr_input req = {0};
3440
3441 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3442 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3443}
3444
Michael Chanc0c050c2015-10-22 16:01:17 -04003445static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3446{
3447 u32 rc = 0;
3448 struct hwrm_tunnel_dst_port_free_input req = {0};
3449
3450 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3451 req.tunnel_type = tunnel_type;
3452
3453 switch (tunnel_type) {
3454 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3455 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3456 break;
3457 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3458 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3459 break;
3460 default:
3461 break;
3462 }
3463
3464 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3465 if (rc)
3466 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3467 rc);
3468 return rc;
3469}
3470
3471static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3472 u8 tunnel_type)
3473{
3474 u32 rc = 0;
3475 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3476 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3477
3478 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3479
3480 req.tunnel_type = tunnel_type;
3481 req.tunnel_dst_port_val = port;
3482
3483 mutex_lock(&bp->hwrm_cmd_lock);
3484 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3485 if (rc) {
3486 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3487 rc);
3488 goto err_out;
3489 }
3490
Christophe Jaillet57aac712016-11-22 06:14:40 +01003491 switch (tunnel_type) {
3492 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003493 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003494 break;
3495 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003496 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003497 break;
3498 default:
3499 break;
3500 }
3501
Michael Chanc0c050c2015-10-22 16:01:17 -04003502err_out:
3503 mutex_unlock(&bp->hwrm_cmd_lock);
3504 return rc;
3505}
3506
3507static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3508{
3509 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3510 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3511
3512 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003513 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003514
3515 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3516 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3517 req.mask = cpu_to_le32(vnic->rx_mask);
3518 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3519}
3520
3521#ifdef CONFIG_RFS_ACCEL
3522static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3523 struct bnxt_ntuple_filter *fltr)
3524{
3525 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3526
3527 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3528 req.ntuple_filter_id = fltr->filter_id;
3529 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3530}
3531
3532#define BNXT_NTP_FLTR_FLAGS \
3533 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3534 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3535 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3536 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3537 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3538 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3539 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3540 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3541 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3542 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3543 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3544 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3545 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003546 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003547
Michael Chan61aad722017-02-12 19:18:14 -05003548#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3549 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3550
Michael Chanc0c050c2015-10-22 16:01:17 -04003551static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3552 struct bnxt_ntuple_filter *fltr)
3553{
3554 int rc = 0;
3555 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3556 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3557 bp->hwrm_cmd_resp_addr;
3558 struct flow_keys *keys = &fltr->fkeys;
3559 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3560
3561 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003562 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003563
3564 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3565
3566 req.ethertype = htons(ETH_P_IP);
3567 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003568 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003569 req.ip_protocol = keys->basic.ip_proto;
3570
Michael Chandda0e742016-12-29 12:13:40 -05003571 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3572 int i;
3573
3574 req.ethertype = htons(ETH_P_IPV6);
3575 req.ip_addr_type =
3576 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3577 *(struct in6_addr *)&req.src_ipaddr[0] =
3578 keys->addrs.v6addrs.src;
3579 *(struct in6_addr *)&req.dst_ipaddr[0] =
3580 keys->addrs.v6addrs.dst;
3581 for (i = 0; i < 4; i++) {
3582 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3583 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3584 }
3585 } else {
3586 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3587 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3588 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3589 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3590 }
Michael Chan61aad722017-02-12 19:18:14 -05003591 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3592 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3593 req.tunnel_type =
3594 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3595 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003596
3597 req.src_port = keys->ports.src;
3598 req.src_port_mask = cpu_to_be16(0xffff);
3599 req.dst_port = keys->ports.dst;
3600 req.dst_port_mask = cpu_to_be16(0xffff);
3601
Michael Chanc1935542015-12-27 18:19:28 -05003602 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003603 mutex_lock(&bp->hwrm_cmd_lock);
3604 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3605 if (!rc)
3606 fltr->filter_id = resp->ntuple_filter_id;
3607 mutex_unlock(&bp->hwrm_cmd_lock);
3608 return rc;
3609}
3610#endif
3611
3612static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3613 u8 *mac_addr)
3614{
3615 u32 rc = 0;
3616 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3617 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3618
3619 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003620 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3621 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3622 req.flags |=
3623 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003624 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003625 req.enables =
3626 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003627 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003628 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3629 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3630 req.l2_addr_mask[0] = 0xff;
3631 req.l2_addr_mask[1] = 0xff;
3632 req.l2_addr_mask[2] = 0xff;
3633 req.l2_addr_mask[3] = 0xff;
3634 req.l2_addr_mask[4] = 0xff;
3635 req.l2_addr_mask[5] = 0xff;
3636
3637 mutex_lock(&bp->hwrm_cmd_lock);
3638 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3639 if (!rc)
3640 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3641 resp->l2_filter_id;
3642 mutex_unlock(&bp->hwrm_cmd_lock);
3643 return rc;
3644}
3645
3646static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3647{
3648 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3649 int rc = 0;
3650
3651 /* Any associated ntuple filters will also be cleared by firmware. */
3652 mutex_lock(&bp->hwrm_cmd_lock);
3653 for (i = 0; i < num_of_vnics; i++) {
3654 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3655
3656 for (j = 0; j < vnic->uc_filter_count; j++) {
3657 struct hwrm_cfa_l2_filter_free_input req = {0};
3658
3659 bnxt_hwrm_cmd_hdr_init(bp, &req,
3660 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3661
3662 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3663
3664 rc = _hwrm_send_message(bp, &req, sizeof(req),
3665 HWRM_CMD_TIMEOUT);
3666 }
3667 vnic->uc_filter_count = 0;
3668 }
3669 mutex_unlock(&bp->hwrm_cmd_lock);
3670
3671 return rc;
3672}
3673
3674static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3675{
3676 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3677 struct hwrm_vnic_tpa_cfg_input req = {0};
3678
3679 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3680
3681 if (tpa_flags) {
3682 u16 mss = bp->dev->mtu - 40;
3683 u32 nsegs, n, segs = 0, flags;
3684
3685 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3686 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3687 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3688 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3689 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3690 if (tpa_flags & BNXT_FLAG_GRO)
3691 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3692
3693 req.flags = cpu_to_le32(flags);
3694
3695 req.enables =
3696 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003697 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3698 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003699
3700 /* Number of segs are log2 units, and first packet is not
3701 * included as part of this units.
3702 */
Michael Chan2839f282016-04-25 02:30:50 -04003703 if (mss <= BNXT_RX_PAGE_SIZE) {
3704 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003705 nsegs = (MAX_SKB_FRAGS - 1) * n;
3706 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003707 n = mss / BNXT_RX_PAGE_SIZE;
3708 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003709 n++;
3710 nsegs = (MAX_SKB_FRAGS - n) / n;
3711 }
3712
3713 segs = ilog2(nsegs);
3714 req.max_agg_segs = cpu_to_le16(segs);
3715 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003716
3717 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003718 }
3719 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3720
3721 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3722}
3723
3724static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3725{
3726 u32 i, j, max_rings;
3727 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3728 struct hwrm_vnic_rss_cfg_input req = {0};
3729
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003730 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003731 return 0;
3732
3733 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3734 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003735 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003736 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3737 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3738 max_rings = bp->rx_nr_rings - 1;
3739 else
3740 max_rings = bp->rx_nr_rings;
3741 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003742 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003743 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003744
3745 /* Fill the RSS indirection table with ring group ids */
3746 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3747 if (j == max_rings)
3748 j = 0;
3749 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3750 }
3751
3752 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3753 req.hash_key_tbl_addr =
3754 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3755 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003756 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003757 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3758}
3759
3760static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3761{
3762 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3763 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3764
3765 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3766 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3767 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3768 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3769 req.enables =
3770 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3771 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3772 /* thresholds not implemented in firmware yet */
3773 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3774 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3775 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3776 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3777}
3778
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003779static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3780 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003781{
3782 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3783
3784 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3785 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003786 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003787
3788 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003789 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003790}
3791
3792static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3793{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003794 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003795
3796 for (i = 0; i < bp->nr_vnics; i++) {
3797 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3798
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003799 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3800 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3801 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3802 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003803 }
3804 bp->rsscos_nr_ctxs = 0;
3805}
3806
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003807static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003808{
3809 int rc;
3810 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3811 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3812 bp->hwrm_cmd_resp_addr;
3813
3814 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3815 -1);
3816
3817 mutex_lock(&bp->hwrm_cmd_lock);
3818 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3819 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003820 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003821 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3822 mutex_unlock(&bp->hwrm_cmd_lock);
3823
3824 return rc;
3825}
3826
Michael Chana588e452016-12-07 00:26:21 -05003827int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003828{
Michael Chanb81a90d2016-01-02 23:45:01 -05003829 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003830 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3831 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003832 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003833
3834 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003835
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003836 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3837 /* Only RSS support for now TBD: COS & LB */
3838 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3839 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3840 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3841 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003842 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3843 req.rss_rule =
3844 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3845 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3846 VNIC_CFG_REQ_ENABLES_MRU);
3847 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003848 } else {
3849 req.rss_rule = cpu_to_le16(0xffff);
3850 }
3851
3852 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3853 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003854 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3855 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3856 } else {
3857 req.cos_rule = cpu_to_le16(0xffff);
3858 }
3859
Michael Chanc0c050c2015-10-22 16:01:17 -04003860 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003861 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003862 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003863 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003864 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3865 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003866
Michael Chanb81a90d2016-01-02 23:45:01 -05003867 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003868 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3869 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3870
3871 req.lb_rule = cpu_to_le16(0xffff);
3872 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3873 VLAN_HLEN);
3874
Michael Chancf6645f2016-06-13 02:25:28 -04003875#ifdef CONFIG_BNXT_SRIOV
3876 if (BNXT_VF(bp))
3877 def_vlan = bp->vf.vlan;
3878#endif
3879 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003880 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003881 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3882 req.flags |=
3883 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003884
3885 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3886}
3887
3888static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3889{
3890 u32 rc = 0;
3891
3892 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3893 struct hwrm_vnic_free_input req = {0};
3894
3895 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3896 req.vnic_id =
3897 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3898
3899 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3900 if (rc)
3901 return rc;
3902 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3903 }
3904 return rc;
3905}
3906
3907static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3908{
3909 u16 i;
3910
3911 for (i = 0; i < bp->nr_vnics; i++)
3912 bnxt_hwrm_vnic_free_one(bp, i);
3913}
3914
Michael Chanb81a90d2016-01-02 23:45:01 -05003915static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3916 unsigned int start_rx_ring_idx,
3917 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003918{
Michael Chanb81a90d2016-01-02 23:45:01 -05003919 int rc = 0;
3920 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003921 struct hwrm_vnic_alloc_input req = {0};
3922 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3923
3924 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003925 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3926 grp_idx = bp->rx_ring[i].bnapi->index;
3927 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003928 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003929 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003930 break;
3931 }
3932 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003933 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003934 }
3935
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003936 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3937 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003938 if (vnic_id == 0)
3939 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3940
3941 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3942
3943 mutex_lock(&bp->hwrm_cmd_lock);
3944 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3945 if (!rc)
3946 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3947 mutex_unlock(&bp->hwrm_cmd_lock);
3948 return rc;
3949}
3950
Michael Chan8fdefd62016-12-29 12:13:36 -05003951static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3952{
3953 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3954 struct hwrm_vnic_qcaps_input req = {0};
3955 int rc;
3956
3957 if (bp->hwrm_spec_code < 0x10600)
3958 return 0;
3959
3960 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3961 mutex_lock(&bp->hwrm_cmd_lock);
3962 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3963 if (!rc) {
3964 if (resp->flags &
3965 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3966 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3967 }
3968 mutex_unlock(&bp->hwrm_cmd_lock);
3969 return rc;
3970}
3971
Michael Chanc0c050c2015-10-22 16:01:17 -04003972static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3973{
3974 u16 i;
3975 u32 rc = 0;
3976
3977 mutex_lock(&bp->hwrm_cmd_lock);
3978 for (i = 0; i < bp->rx_nr_rings; i++) {
3979 struct hwrm_ring_grp_alloc_input req = {0};
3980 struct hwrm_ring_grp_alloc_output *resp =
3981 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003982 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003983
3984 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3985
Michael Chanb81a90d2016-01-02 23:45:01 -05003986 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3987 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3988 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3989 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003990
3991 rc = _hwrm_send_message(bp, &req, sizeof(req),
3992 HWRM_CMD_TIMEOUT);
3993 if (rc)
3994 break;
3995
Michael Chanb81a90d2016-01-02 23:45:01 -05003996 bp->grp_info[grp_idx].fw_grp_id =
3997 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003998 }
3999 mutex_unlock(&bp->hwrm_cmd_lock);
4000 return rc;
4001}
4002
4003static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4004{
4005 u16 i;
4006 u32 rc = 0;
4007 struct hwrm_ring_grp_free_input req = {0};
4008
4009 if (!bp->grp_info)
4010 return 0;
4011
4012 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4013
4014 mutex_lock(&bp->hwrm_cmd_lock);
4015 for (i = 0; i < bp->cp_nr_rings; i++) {
4016 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4017 continue;
4018 req.ring_group_id =
4019 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4020
4021 rc = _hwrm_send_message(bp, &req, sizeof(req),
4022 HWRM_CMD_TIMEOUT);
4023 if (rc)
4024 break;
4025 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4026 }
4027 mutex_unlock(&bp->hwrm_cmd_lock);
4028 return rc;
4029}
4030
4031static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4032 struct bnxt_ring_struct *ring,
4033 u32 ring_type, u32 map_index,
4034 u32 stats_ctx_id)
4035{
4036 int rc = 0, err = 0;
4037 struct hwrm_ring_alloc_input req = {0};
4038 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4039 u16 ring_id;
4040
4041 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4042
4043 req.enables = 0;
4044 if (ring->nr_pages > 1) {
4045 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4046 /* Page size is in log2 units */
4047 req.page_size = BNXT_PAGE_SHIFT;
4048 req.page_tbl_depth = 1;
4049 } else {
4050 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4051 }
4052 req.fbo = 0;
4053 /* Association of ring index with doorbell index and MSIX number */
4054 req.logical_id = cpu_to_le16(map_index);
4055
4056 switch (ring_type) {
4057 case HWRM_RING_ALLOC_TX:
4058 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4059 /* Association of transmit ring with completion ring */
4060 req.cmpl_ring_id =
4061 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4062 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4063 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4064 req.queue_id = cpu_to_le16(ring->queue_id);
4065 break;
4066 case HWRM_RING_ALLOC_RX:
4067 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4068 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4069 break;
4070 case HWRM_RING_ALLOC_AGG:
4071 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4072 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4073 break;
4074 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004075 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004076 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4077 if (bp->flags & BNXT_FLAG_USING_MSIX)
4078 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4079 break;
4080 default:
4081 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4082 ring_type);
4083 return -1;
4084 }
4085
4086 mutex_lock(&bp->hwrm_cmd_lock);
4087 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4088 err = le16_to_cpu(resp->error_code);
4089 ring_id = le16_to_cpu(resp->ring_id);
4090 mutex_unlock(&bp->hwrm_cmd_lock);
4091
4092 if (rc || err) {
4093 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004094 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004095 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4096 rc, err);
4097 return -1;
4098
4099 case RING_FREE_REQ_RING_TYPE_RX:
4100 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4101 rc, err);
4102 return -1;
4103
4104 case RING_FREE_REQ_RING_TYPE_TX:
4105 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4106 rc, err);
4107 return -1;
4108
4109 default:
4110 netdev_err(bp->dev, "Invalid ring\n");
4111 return -1;
4112 }
4113 }
4114 ring->fw_ring_id = ring_id;
4115 return rc;
4116}
4117
Michael Chan486b5c22016-12-29 12:13:42 -05004118static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4119{
4120 int rc;
4121
4122 if (BNXT_PF(bp)) {
4123 struct hwrm_func_cfg_input req = {0};
4124
4125 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4126 req.fid = cpu_to_le16(0xffff);
4127 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4128 req.async_event_cr = cpu_to_le16(idx);
4129 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4130 } else {
4131 struct hwrm_func_vf_cfg_input req = {0};
4132
4133 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4134 req.enables =
4135 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4136 req.async_event_cr = cpu_to_le16(idx);
4137 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4138 }
4139 return rc;
4140}
4141
Michael Chanc0c050c2015-10-22 16:01:17 -04004142static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4143{
4144 int i, rc = 0;
4145
Michael Chanedd0c2c2015-12-27 18:19:19 -05004146 for (i = 0; i < bp->cp_nr_rings; i++) {
4147 struct bnxt_napi *bnapi = bp->bnapi[i];
4148 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4149 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004150
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004151 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004152 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4153 INVALID_STATS_CTX_ID);
4154 if (rc)
4155 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004156 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4157 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004158
4159 if (!i) {
4160 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4161 if (rc)
4162 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4163 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004164 }
4165
Michael Chanedd0c2c2015-12-27 18:19:19 -05004166 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004167 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004168 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004169 u32 map_idx = txr->bnapi->index;
4170 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004171
Michael Chanb81a90d2016-01-02 23:45:01 -05004172 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4173 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004174 if (rc)
4175 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004176 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004177 }
4178
Michael Chanedd0c2c2015-12-27 18:19:19 -05004179 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004180 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004181 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004182 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004183
Michael Chanb81a90d2016-01-02 23:45:01 -05004184 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4185 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004186 if (rc)
4187 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004188 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004189 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004190 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004191 }
4192
4193 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4194 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004195 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004196 struct bnxt_ring_struct *ring =
4197 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004198 u32 grp_idx = rxr->bnapi->index;
4199 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004200
4201 rc = hwrm_ring_alloc_send_msg(bp, ring,
4202 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004203 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004204 INVALID_STATS_CTX_ID);
4205 if (rc)
4206 goto err_out;
4207
Michael Chanb81a90d2016-01-02 23:45:01 -05004208 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004209 writel(DB_KEY_RX | rxr->rx_agg_prod,
4210 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004211 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004212 }
4213 }
4214err_out:
4215 return rc;
4216}
4217
4218static int hwrm_ring_free_send_msg(struct bnxt *bp,
4219 struct bnxt_ring_struct *ring,
4220 u32 ring_type, int cmpl_ring_id)
4221{
4222 int rc;
4223 struct hwrm_ring_free_input req = {0};
4224 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4225 u16 error_code;
4226
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004227 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004228 req.ring_type = ring_type;
4229 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4230
4231 mutex_lock(&bp->hwrm_cmd_lock);
4232 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4233 error_code = le16_to_cpu(resp->error_code);
4234 mutex_unlock(&bp->hwrm_cmd_lock);
4235
4236 if (rc || error_code) {
4237 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004238 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004239 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4240 rc);
4241 return rc;
4242 case RING_FREE_REQ_RING_TYPE_RX:
4243 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4244 rc);
4245 return rc;
4246 case RING_FREE_REQ_RING_TYPE_TX:
4247 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4248 rc);
4249 return rc;
4250 default:
4251 netdev_err(bp->dev, "Invalid ring\n");
4252 return -1;
4253 }
4254 }
4255 return 0;
4256}
4257
Michael Chanedd0c2c2015-12-27 18:19:19 -05004258static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004259{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004260 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004261
4262 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004263 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004264
Michael Chanedd0c2c2015-12-27 18:19:19 -05004265 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004266 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004267 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004268 u32 grp_idx = txr->bnapi->index;
4269 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004270
Michael Chanedd0c2c2015-12-27 18:19:19 -05004271 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4272 hwrm_ring_free_send_msg(bp, ring,
4273 RING_FREE_REQ_RING_TYPE_TX,
4274 close_path ? cmpl_ring_id :
4275 INVALID_HW_RING_ID);
4276 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004277 }
4278 }
4279
Michael Chanedd0c2c2015-12-27 18:19:19 -05004280 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004281 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004282 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004283 u32 grp_idx = rxr->bnapi->index;
4284 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004285
Michael Chanedd0c2c2015-12-27 18:19:19 -05004286 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4287 hwrm_ring_free_send_msg(bp, ring,
4288 RING_FREE_REQ_RING_TYPE_RX,
4289 close_path ? cmpl_ring_id :
4290 INVALID_HW_RING_ID);
4291 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004292 bp->grp_info[grp_idx].rx_fw_ring_id =
4293 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004294 }
4295 }
4296
Michael Chanedd0c2c2015-12-27 18:19:19 -05004297 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004298 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004299 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004300 u32 grp_idx = rxr->bnapi->index;
4301 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004302
Michael Chanedd0c2c2015-12-27 18:19:19 -05004303 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4304 hwrm_ring_free_send_msg(bp, ring,
4305 RING_FREE_REQ_RING_TYPE_RX,
4306 close_path ? cmpl_ring_id :
4307 INVALID_HW_RING_ID);
4308 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004309 bp->grp_info[grp_idx].agg_fw_ring_id =
4310 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004311 }
4312 }
4313
Michael Chan9d8bc092016-12-29 12:13:33 -05004314 /* The completion rings are about to be freed. After that the
4315 * IRQ doorbell will not work anymore. So we need to disable
4316 * IRQ here.
4317 */
4318 bnxt_disable_int_sync(bp);
4319
Michael Chanedd0c2c2015-12-27 18:19:19 -05004320 for (i = 0; i < bp->cp_nr_rings; i++) {
4321 struct bnxt_napi *bnapi = bp->bnapi[i];
4322 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4323 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004324
Michael Chanedd0c2c2015-12-27 18:19:19 -05004325 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4326 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004327 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004328 INVALID_HW_RING_ID);
4329 ring->fw_ring_id = INVALID_HW_RING_ID;
4330 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004331 }
4332 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004333}
4334
Michael Chan391be5c2016-12-29 12:13:41 -05004335/* Caller must hold bp->hwrm_cmd_lock */
4336int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4337{
4338 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4339 struct hwrm_func_qcfg_input req = {0};
4340 int rc;
4341
4342 if (bp->hwrm_spec_code < 0x10601)
4343 return 0;
4344
4345 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4346 req.fid = cpu_to_le16(fid);
4347 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4348 if (!rc)
4349 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4350
4351 return rc;
4352}
4353
Michael Chand1e79252017-02-06 16:55:38 -05004354static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004355{
4356 struct hwrm_func_cfg_input req = {0};
4357 int rc;
4358
4359 if (bp->hwrm_spec_code < 0x10601)
4360 return 0;
4361
4362 if (BNXT_VF(bp))
4363 return 0;
4364
4365 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4366 req.fid = cpu_to_le16(0xffff);
4367 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4368 req.num_tx_rings = cpu_to_le16(*tx_rings);
4369 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4370 if (rc)
4371 return rc;
4372
4373 mutex_lock(&bp->hwrm_cmd_lock);
4374 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4375 mutex_unlock(&bp->hwrm_cmd_lock);
4376 return rc;
4377}
4378
Michael Chanbb053f52016-02-26 04:00:02 -05004379static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4380 u32 buf_tmrs, u16 flags,
4381 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4382{
4383 req->flags = cpu_to_le16(flags);
4384 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4385 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4386 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4387 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4388 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4389 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4390 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4391 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4392}
4393
Michael Chanc0c050c2015-10-22 16:01:17 -04004394int bnxt_hwrm_set_coal(struct bnxt *bp)
4395{
4396 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004397 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4398 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004399 u16 max_buf, max_buf_irq;
4400 u16 buf_tmr, buf_tmr_irq;
4401 u32 flags;
4402
Michael Chandfc9c942016-02-26 04:00:03 -05004403 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4404 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4405 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4406 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004407
Michael Chandfb5b892016-02-26 04:00:01 -05004408 /* Each rx completion (2 records) should be DMAed immediately.
4409 * DMA 1/4 of the completion buffers at a time.
4410 */
4411 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004412 /* max_buf must not be zero */
4413 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004414 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4415 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4416 /* buf timer set to 1/4 of interrupt timer */
4417 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4418 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4419 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004420
4421 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4422
4423 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4424 * if coal_ticks is less than 25 us.
4425 */
Michael Chandfb5b892016-02-26 04:00:01 -05004426 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004427 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4428
Michael Chanbb053f52016-02-26 04:00:02 -05004429 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004430 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4431
4432 /* max_buf must not be zero */
4433 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4434 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4435 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4436 /* buf timer set to 1/4 of interrupt timer */
4437 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4438 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4439 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4440
4441 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4442 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4443 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004444
4445 mutex_lock(&bp->hwrm_cmd_lock);
4446 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004447 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004448
Michael Chandfc9c942016-02-26 04:00:03 -05004449 req = &req_rx;
4450 if (!bnapi->rx_ring)
4451 req = &req_tx;
4452 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4453
4454 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004455 HWRM_CMD_TIMEOUT);
4456 if (rc)
4457 break;
4458 }
4459 mutex_unlock(&bp->hwrm_cmd_lock);
4460 return rc;
4461}
4462
4463static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4464{
4465 int rc = 0, i;
4466 struct hwrm_stat_ctx_free_input req = {0};
4467
4468 if (!bp->bnapi)
4469 return 0;
4470
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004471 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4472 return 0;
4473
Michael Chanc0c050c2015-10-22 16:01:17 -04004474 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4475
4476 mutex_lock(&bp->hwrm_cmd_lock);
4477 for (i = 0; i < bp->cp_nr_rings; i++) {
4478 struct bnxt_napi *bnapi = bp->bnapi[i];
4479 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4480
4481 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4482 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4483
4484 rc = _hwrm_send_message(bp, &req, sizeof(req),
4485 HWRM_CMD_TIMEOUT);
4486 if (rc)
4487 break;
4488
4489 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4490 }
4491 }
4492 mutex_unlock(&bp->hwrm_cmd_lock);
4493 return rc;
4494}
4495
4496static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4497{
4498 int rc = 0, i;
4499 struct hwrm_stat_ctx_alloc_input req = {0};
4500 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4501
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004502 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4503 return 0;
4504
Michael Chanc0c050c2015-10-22 16:01:17 -04004505 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4506
Michael Chan51f30782016-07-01 18:46:29 -04004507 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004508
4509 mutex_lock(&bp->hwrm_cmd_lock);
4510 for (i = 0; i < bp->cp_nr_rings; i++) {
4511 struct bnxt_napi *bnapi = bp->bnapi[i];
4512 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4513
4514 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4515
4516 rc = _hwrm_send_message(bp, &req, sizeof(req),
4517 HWRM_CMD_TIMEOUT);
4518 if (rc)
4519 break;
4520
4521 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4522
4523 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4524 }
4525 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004526 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004527}
4528
Michael Chancf6645f2016-06-13 02:25:28 -04004529static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4530{
4531 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004532 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004533 int rc;
4534
4535 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4536 req.fid = cpu_to_le16(0xffff);
4537 mutex_lock(&bp->hwrm_cmd_lock);
4538 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4539 if (rc)
4540 goto func_qcfg_exit;
4541
4542#ifdef CONFIG_BNXT_SRIOV
4543 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004544 struct bnxt_vf_info *vf = &bp->vf;
4545
4546 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4547 }
4548#endif
Deepak Khungar9e54e322017-04-21 20:11:26 -04004549 if (BNXT_PF(bp)) {
4550 u16 flags = le16_to_cpu(resp->flags);
4551
4552 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4553 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED))
4554 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4555 if (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
4556 bp->flags |= BNXT_FLAG_MULTI_HOST;
4557 }
Michael Chanbc39f882017-03-08 18:44:34 -05004558
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004559 switch (resp->port_partition_type) {
4560 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4561 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4562 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4563 bp->port_partition_type = resp->port_partition_type;
4564 break;
4565 }
Michael Chancf6645f2016-06-13 02:25:28 -04004566
4567func_qcfg_exit:
4568 mutex_unlock(&bp->hwrm_cmd_lock);
4569 return rc;
4570}
4571
Michael Chan7b08f662016-12-07 00:26:18 -05004572static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004573{
4574 int rc = 0;
4575 struct hwrm_func_qcaps_input req = {0};
4576 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4577
4578 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4579 req.fid = cpu_to_le16(0xffff);
4580
4581 mutex_lock(&bp->hwrm_cmd_lock);
4582 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4583 if (rc)
4584 goto hwrm_func_qcaps_exit;
4585
Michael Chane4060d32016-12-07 00:26:19 -05004586 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4587 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4588 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4589 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4590
Michael Chan7cc5a202016-09-19 03:58:05 -04004591 bp->tx_push_thresh = 0;
4592 if (resp->flags &
4593 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4594 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4595
Michael Chanc0c050c2015-10-22 16:01:17 -04004596 if (BNXT_PF(bp)) {
4597 struct bnxt_pf_info *pf = &bp->pf;
4598
4599 pf->fw_fid = le16_to_cpu(resp->fid);
4600 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004601 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004602 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004603 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004604 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4605 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4606 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004607 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004608 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4609 if (!pf->max_hw_ring_grps)
4610 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004611 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4612 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4613 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4614 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4615 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4616 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4617 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4618 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4619 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4620 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4621 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004622 if (resp->flags &
4623 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4624 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004625 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004626#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004627 struct bnxt_vf_info *vf = &bp->vf;
4628
4629 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004630
4631 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4632 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4633 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4634 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004635 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4636 if (!vf->max_hw_ring_grps)
4637 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004638 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4639 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4640 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004641
4642 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004643 mutex_unlock(&bp->hwrm_cmd_lock);
4644
4645 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004646 /* overwrite netdev dev_adr with admin VF MAC */
4647 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004648 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004649 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004650 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4651 }
4652 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004653#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004654 }
4655
Michael Chanc0c050c2015-10-22 16:01:17 -04004656hwrm_func_qcaps_exit:
4657 mutex_unlock(&bp->hwrm_cmd_lock);
4658 return rc;
4659}
4660
4661static int bnxt_hwrm_func_reset(struct bnxt *bp)
4662{
4663 struct hwrm_func_reset_input req = {0};
4664
4665 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4666 req.enables = 0;
4667
4668 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4669}
4670
4671static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4672{
4673 int rc = 0;
4674 struct hwrm_queue_qportcfg_input req = {0};
4675 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4676 u8 i, *qptr;
4677
4678 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4679
4680 mutex_lock(&bp->hwrm_cmd_lock);
4681 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4682 if (rc)
4683 goto qportcfg_exit;
4684
4685 if (!resp->max_configurable_queues) {
4686 rc = -EINVAL;
4687 goto qportcfg_exit;
4688 }
4689 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004690 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004691 if (bp->max_tc > BNXT_MAX_QUEUE)
4692 bp->max_tc = BNXT_MAX_QUEUE;
4693
Michael Chan441cabb2016-09-19 03:58:02 -04004694 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4695 bp->max_tc = 1;
4696
Michael Chan87c374d2016-12-02 21:17:16 -05004697 if (bp->max_lltc > bp->max_tc)
4698 bp->max_lltc = bp->max_tc;
4699
Michael Chanc0c050c2015-10-22 16:01:17 -04004700 qptr = &resp->queue_id0;
4701 for (i = 0; i < bp->max_tc; i++) {
4702 bp->q_info[i].queue_id = *qptr++;
4703 bp->q_info[i].queue_profile = *qptr++;
4704 }
4705
4706qportcfg_exit:
4707 mutex_unlock(&bp->hwrm_cmd_lock);
4708 return rc;
4709}
4710
4711static int bnxt_hwrm_ver_get(struct bnxt *bp)
4712{
4713 int rc;
4714 struct hwrm_ver_get_input req = {0};
4715 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004716 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004717
Michael Chane6ef2692016-03-28 19:46:05 -04004718 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004719 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4720 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4721 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4722 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4723 mutex_lock(&bp->hwrm_cmd_lock);
4724 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4725 if (rc)
4726 goto hwrm_ver_get_exit;
4727
4728 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4729
Michael Chan11f15ed2016-04-05 14:08:55 -04004730 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4731 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004732 if (resp->hwrm_intf_maj < 1) {
4733 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004734 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004735 resp->hwrm_intf_upd);
4736 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004737 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004738 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004739 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4740 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4741
Michael Chanff4fe812016-02-26 04:00:04 -05004742 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4743 if (!bp->hwrm_cmd_timeout)
4744 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4745
Michael Chane6ef2692016-03-28 19:46:05 -04004746 if (resp->hwrm_intf_maj >= 1)
4747 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4748
Michael Chan659c8052016-06-13 02:25:33 -04004749 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004750 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4751 !resp->chip_metal)
4752 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004753
Deepak Khungare605db82017-05-29 19:06:04 -04004754 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4755 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4756 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4757 bp->flags |= BNXT_FLAG_SHORT_CMD;
4758
Michael Chanc0c050c2015-10-22 16:01:17 -04004759hwrm_ver_get_exit:
4760 mutex_unlock(&bp->hwrm_cmd_lock);
4761 return rc;
4762}
4763
Rob Swindell5ac67d82016-09-19 03:58:03 -04004764int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4765{
Rob Swindell878786d2016-09-20 03:36:33 -04004766#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004767 struct hwrm_fw_set_time_input req = {0};
4768 struct rtc_time tm;
4769 struct timeval tv;
4770
4771 if (bp->hwrm_spec_code < 0x10400)
4772 return -EOPNOTSUPP;
4773
4774 do_gettimeofday(&tv);
4775 rtc_time_to_tm(tv.tv_sec, &tm);
4776 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4777 req.year = cpu_to_le16(1900 + tm.tm_year);
4778 req.month = 1 + tm.tm_mon;
4779 req.day = tm.tm_mday;
4780 req.hour = tm.tm_hour;
4781 req.minute = tm.tm_min;
4782 req.second = tm.tm_sec;
4783 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004784#else
4785 return -EOPNOTSUPP;
4786#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004787}
4788
Michael Chan3bdf56c2016-03-07 15:38:45 -05004789static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4790{
4791 int rc;
4792 struct bnxt_pf_info *pf = &bp->pf;
4793 struct hwrm_port_qstats_input req = {0};
4794
4795 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4796 return 0;
4797
4798 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4799 req.port_id = cpu_to_le16(pf->port_id);
4800 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4801 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4802 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4803 return rc;
4804}
4805
Michael Chanc0c050c2015-10-22 16:01:17 -04004806static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4807{
4808 if (bp->vxlan_port_cnt) {
4809 bnxt_hwrm_tunnel_dst_port_free(
4810 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4811 }
4812 bp->vxlan_port_cnt = 0;
4813 if (bp->nge_port_cnt) {
4814 bnxt_hwrm_tunnel_dst_port_free(
4815 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4816 }
4817 bp->nge_port_cnt = 0;
4818}
4819
4820static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4821{
4822 int rc, i;
4823 u32 tpa_flags = 0;
4824
4825 if (set_tpa)
4826 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4827 for (i = 0; i < bp->nr_vnics; i++) {
4828 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4829 if (rc) {
4830 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004831 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004832 return rc;
4833 }
4834 }
4835 return 0;
4836}
4837
4838static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4839{
4840 int i;
4841
4842 for (i = 0; i < bp->nr_vnics; i++)
4843 bnxt_hwrm_vnic_set_rss(bp, i, false);
4844}
4845
4846static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4847 bool irq_re_init)
4848{
4849 if (bp->vnic_info) {
4850 bnxt_hwrm_clear_vnic_filter(bp);
4851 /* clear all RSS setting before free vnic ctx */
4852 bnxt_hwrm_clear_vnic_rss(bp);
4853 bnxt_hwrm_vnic_ctx_free(bp);
4854 /* before free the vnic, undo the vnic tpa settings */
4855 if (bp->flags & BNXT_FLAG_TPA)
4856 bnxt_set_tpa(bp, false);
4857 bnxt_hwrm_vnic_free(bp);
4858 }
4859 bnxt_hwrm_ring_free(bp, close_path);
4860 bnxt_hwrm_ring_grp_free(bp);
4861 if (irq_re_init) {
4862 bnxt_hwrm_stat_ctx_free(bp);
4863 bnxt_hwrm_free_tunnel_ports(bp);
4864 }
4865}
4866
4867static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4868{
Michael Chanae10ae72016-12-29 12:13:38 -05004869 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004870 int rc;
4871
Michael Chanae10ae72016-12-29 12:13:38 -05004872 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4873 goto skip_rss_ctx;
4874
Michael Chanc0c050c2015-10-22 16:01:17 -04004875 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004876 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004877 if (rc) {
4878 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4879 vnic_id, rc);
4880 goto vnic_setup_err;
4881 }
4882 bp->rsscos_nr_ctxs++;
4883
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004884 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4885 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4886 if (rc) {
4887 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4888 vnic_id, rc);
4889 goto vnic_setup_err;
4890 }
4891 bp->rsscos_nr_ctxs++;
4892 }
4893
Michael Chanae10ae72016-12-29 12:13:38 -05004894skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004895 /* configure default vnic, ring grp */
4896 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4897 if (rc) {
4898 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4899 vnic_id, rc);
4900 goto vnic_setup_err;
4901 }
4902
4903 /* Enable RSS hashing on vnic */
4904 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4905 if (rc) {
4906 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4907 vnic_id, rc);
4908 goto vnic_setup_err;
4909 }
4910
4911 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4912 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4913 if (rc) {
4914 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4915 vnic_id, rc);
4916 }
4917 }
4918
4919vnic_setup_err:
4920 return rc;
4921}
4922
4923static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4924{
4925#ifdef CONFIG_RFS_ACCEL
4926 int i, rc = 0;
4927
4928 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004929 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004930 u16 vnic_id = i + 1;
4931 u16 ring_id = i;
4932
4933 if (vnic_id >= bp->nr_vnics)
4934 break;
4935
Michael Chanae10ae72016-12-29 12:13:38 -05004936 vnic = &bp->vnic_info[vnic_id];
4937 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4938 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4939 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004940 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004941 if (rc) {
4942 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4943 vnic_id, rc);
4944 break;
4945 }
4946 rc = bnxt_setup_vnic(bp, vnic_id);
4947 if (rc)
4948 break;
4949 }
4950 return rc;
4951#else
4952 return 0;
4953#endif
4954}
4955
Michael Chan17c71ac2016-07-01 18:46:27 -04004956/* Allow PF and VF with default VLAN to be in promiscuous mode */
4957static bool bnxt_promisc_ok(struct bnxt *bp)
4958{
4959#ifdef CONFIG_BNXT_SRIOV
4960 if (BNXT_VF(bp) && !bp->vf.vlan)
4961 return false;
4962#endif
4963 return true;
4964}
4965
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004966static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4967{
4968 unsigned int rc = 0;
4969
4970 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4971 if (rc) {
4972 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4973 rc);
4974 return rc;
4975 }
4976
4977 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4978 if (rc) {
4979 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4980 rc);
4981 return rc;
4982 }
4983 return rc;
4984}
4985
Michael Chanb664f002015-12-02 01:54:08 -05004986static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004987static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004988
Michael Chanc0c050c2015-10-22 16:01:17 -04004989static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4990{
Michael Chan7d2837d2016-05-04 16:56:44 -04004991 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004992 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004993 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004994
4995 if (irq_re_init) {
4996 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4997 if (rc) {
4998 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4999 rc);
5000 goto err_out;
5001 }
5002 }
5003
5004 rc = bnxt_hwrm_ring_alloc(bp);
5005 if (rc) {
5006 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5007 goto err_out;
5008 }
5009
5010 rc = bnxt_hwrm_ring_grp_alloc(bp);
5011 if (rc) {
5012 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5013 goto err_out;
5014 }
5015
Prashant Sreedharan76595192016-07-18 07:15:22 -04005016 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5017 rx_nr_rings--;
5018
Michael Chanc0c050c2015-10-22 16:01:17 -04005019 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005020 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005021 if (rc) {
5022 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5023 goto err_out;
5024 }
5025
5026 rc = bnxt_setup_vnic(bp, 0);
5027 if (rc)
5028 goto err_out;
5029
5030 if (bp->flags & BNXT_FLAG_RFS) {
5031 rc = bnxt_alloc_rfs_vnics(bp);
5032 if (rc)
5033 goto err_out;
5034 }
5035
5036 if (bp->flags & BNXT_FLAG_TPA) {
5037 rc = bnxt_set_tpa(bp, true);
5038 if (rc)
5039 goto err_out;
5040 }
5041
5042 if (BNXT_VF(bp))
5043 bnxt_update_vf_mac(bp);
5044
5045 /* Filter for default vnic 0 */
5046 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5047 if (rc) {
5048 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5049 goto err_out;
5050 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005051 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005052
Michael Chan7d2837d2016-05-04 16:56:44 -04005053 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005054
Michael Chan17c71ac2016-07-01 18:46:27 -04005055 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005056 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5057
5058 if (bp->dev->flags & IFF_ALLMULTI) {
5059 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5060 vnic->mc_list_count = 0;
5061 } else {
5062 u32 mask = 0;
5063
5064 bnxt_mc_list_updated(bp, &mask);
5065 vnic->rx_mask |= mask;
5066 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005067
Michael Chanb664f002015-12-02 01:54:08 -05005068 rc = bnxt_cfg_rx_mode(bp);
5069 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005070 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005071
5072 rc = bnxt_hwrm_set_coal(bp);
5073 if (rc)
5074 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005075 rc);
5076
5077 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5078 rc = bnxt_setup_nitroa0_vnic(bp);
5079 if (rc)
5080 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5081 rc);
5082 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005083
Michael Chancf6645f2016-06-13 02:25:28 -04005084 if (BNXT_VF(bp)) {
5085 bnxt_hwrm_func_qcfg(bp);
5086 netdev_update_features(bp->dev);
5087 }
5088
Michael Chanc0c050c2015-10-22 16:01:17 -04005089 return 0;
5090
5091err_out:
5092 bnxt_hwrm_resource_free(bp, 0, true);
5093
5094 return rc;
5095}
5096
5097static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5098{
5099 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5100 return 0;
5101}
5102
5103static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5104{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005105 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005106 bnxt_init_rx_rings(bp);
5107 bnxt_init_tx_rings(bp);
5108 bnxt_init_ring_grps(bp, irq_re_init);
5109 bnxt_init_vnics(bp);
5110
5111 return bnxt_init_chip(bp, irq_re_init);
5112}
5113
Michael Chanc0c050c2015-10-22 16:01:17 -04005114static int bnxt_set_real_num_queues(struct bnxt *bp)
5115{
5116 int rc;
5117 struct net_device *dev = bp->dev;
5118
Michael Chan5f449242017-02-06 16:55:40 -05005119 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5120 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005121 if (rc)
5122 return rc;
5123
5124 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5125 if (rc)
5126 return rc;
5127
5128#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005129 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005130 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005131#endif
5132
5133 return rc;
5134}
5135
Michael Chan6e6c5a52016-01-02 23:45:02 -05005136static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5137 bool shared)
5138{
5139 int _rx = *rx, _tx = *tx;
5140
5141 if (shared) {
5142 *rx = min_t(int, _rx, max);
5143 *tx = min_t(int, _tx, max);
5144 } else {
5145 if (max < 2)
5146 return -ENOMEM;
5147
5148 while (_rx + _tx > max) {
5149 if (_rx > _tx && _rx > 1)
5150 _rx--;
5151 else if (_tx > 1)
5152 _tx--;
5153 }
5154 *rx = _rx;
5155 *tx = _tx;
5156 }
5157 return 0;
5158}
5159
Michael Chan78095922016-12-07 00:26:16 -05005160static void bnxt_setup_msix(struct bnxt *bp)
5161{
5162 const int len = sizeof(bp->irq_tbl[0].name);
5163 struct net_device *dev = bp->dev;
5164 int tcs, i;
5165
5166 tcs = netdev_get_num_tc(dev);
5167 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005168 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005169
Michael Chand1e79252017-02-06 16:55:38 -05005170 for (i = 0; i < tcs; i++) {
5171 count = bp->tx_nr_rings_per_tc;
5172 off = i * count;
5173 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005174 }
5175 }
5176
5177 for (i = 0; i < bp->cp_nr_rings; i++) {
5178 char *attr;
5179
5180 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5181 attr = "TxRx";
5182 else if (i < bp->rx_nr_rings)
5183 attr = "rx";
5184 else
5185 attr = "tx";
5186
5187 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5188 i);
5189 bp->irq_tbl[i].handler = bnxt_msix;
5190 }
5191}
5192
5193static void bnxt_setup_inta(struct bnxt *bp)
5194{
5195 const int len = sizeof(bp->irq_tbl[0].name);
5196
5197 if (netdev_get_num_tc(bp->dev))
5198 netdev_reset_tc(bp->dev);
5199
5200 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5201 0);
5202 bp->irq_tbl[0].handler = bnxt_inta;
5203}
5204
5205static int bnxt_setup_int_mode(struct bnxt *bp)
5206{
5207 int rc;
5208
5209 if (bp->flags & BNXT_FLAG_USING_MSIX)
5210 bnxt_setup_msix(bp);
5211 else
5212 bnxt_setup_inta(bp);
5213
5214 rc = bnxt_set_real_num_queues(bp);
5215 return rc;
5216}
5217
Michael Chanb7429952017-01-13 01:32:00 -05005218#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005219static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5220{
5221#if defined(CONFIG_BNXT_SRIOV)
5222 if (BNXT_VF(bp))
5223 return bp->vf.max_rsscos_ctxs;
5224#endif
5225 return bp->pf.max_rsscos_ctxs;
5226}
5227
5228static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5229{
5230#if defined(CONFIG_BNXT_SRIOV)
5231 if (BNXT_VF(bp))
5232 return bp->vf.max_vnics;
5233#endif
5234 return bp->pf.max_vnics;
5235}
Michael Chanb7429952017-01-13 01:32:00 -05005236#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005237
Michael Chane4060d32016-12-07 00:26:19 -05005238unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5239{
5240#if defined(CONFIG_BNXT_SRIOV)
5241 if (BNXT_VF(bp))
5242 return bp->vf.max_stat_ctxs;
5243#endif
5244 return bp->pf.max_stat_ctxs;
5245}
5246
Michael Chana588e452016-12-07 00:26:21 -05005247void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5248{
5249#if defined(CONFIG_BNXT_SRIOV)
5250 if (BNXT_VF(bp))
5251 bp->vf.max_stat_ctxs = max;
5252 else
5253#endif
5254 bp->pf.max_stat_ctxs = max;
5255}
5256
Michael Chane4060d32016-12-07 00:26:19 -05005257unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5258{
5259#if defined(CONFIG_BNXT_SRIOV)
5260 if (BNXT_VF(bp))
5261 return bp->vf.max_cp_rings;
5262#endif
5263 return bp->pf.max_cp_rings;
5264}
5265
Michael Chana588e452016-12-07 00:26:21 -05005266void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5267{
5268#if defined(CONFIG_BNXT_SRIOV)
5269 if (BNXT_VF(bp))
5270 bp->vf.max_cp_rings = max;
5271 else
5272#endif
5273 bp->pf.max_cp_rings = max;
5274}
5275
Michael Chan78095922016-12-07 00:26:16 -05005276static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5277{
5278#if defined(CONFIG_BNXT_SRIOV)
5279 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005280 return min_t(unsigned int, bp->vf.max_irqs,
5281 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005282#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005283 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005284}
5285
Michael Chan33c26572016-12-07 00:26:15 -05005286void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5287{
5288#if defined(CONFIG_BNXT_SRIOV)
5289 if (BNXT_VF(bp))
5290 bp->vf.max_irqs = max_irqs;
5291 else
5292#endif
5293 bp->pf.max_irqs = max_irqs;
5294}
5295
Michael Chan78095922016-12-07 00:26:16 -05005296static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005297{
Michael Chan01657bc2016-01-02 23:45:03 -05005298 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005299 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005300
Michael Chan78095922016-12-07 00:26:16 -05005301 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005302 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5303 if (!msix_ent)
5304 return -ENOMEM;
5305
5306 for (i = 0; i < total_vecs; i++) {
5307 msix_ent[i].entry = i;
5308 msix_ent[i].vector = 0;
5309 }
5310
Michael Chan01657bc2016-01-02 23:45:03 -05005311 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5312 min = 2;
5313
5314 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005315 if (total_vecs < 0) {
5316 rc = -ENODEV;
5317 goto msix_setup_exit;
5318 }
5319
5320 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5321 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005322 for (i = 0; i < total_vecs; i++)
5323 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005324
Michael Chan78095922016-12-07 00:26:16 -05005325 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005326 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005327 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005328 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005329 if (rc)
5330 goto msix_setup_exit;
5331
Michael Chanc0c050c2015-10-22 16:01:17 -04005332 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005333 bp->cp_nr_rings = (min == 1) ?
5334 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5335 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005336
Michael Chanc0c050c2015-10-22 16:01:17 -04005337 } else {
5338 rc = -ENOMEM;
5339 goto msix_setup_exit;
5340 }
5341 bp->flags |= BNXT_FLAG_USING_MSIX;
5342 kfree(msix_ent);
5343 return 0;
5344
5345msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005346 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5347 kfree(bp->irq_tbl);
5348 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005349 pci_disable_msix(bp->pdev);
5350 kfree(msix_ent);
5351 return rc;
5352}
5353
Michael Chan78095922016-12-07 00:26:16 -05005354static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005355{
Michael Chanc0c050c2015-10-22 16:01:17 -04005356 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005357 if (!bp->irq_tbl)
5358 return -ENOMEM;
5359
5360 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005361 bp->rx_nr_rings = 1;
5362 bp->tx_nr_rings = 1;
5363 bp->cp_nr_rings = 1;
5364 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005365 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005366 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005367 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005368}
5369
Michael Chan78095922016-12-07 00:26:16 -05005370static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005371{
5372 int rc = 0;
5373
5374 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005375 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005376
Michael Chan1fa72e22016-04-25 02:30:49 -04005377 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005378 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005379 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005380 }
5381 return rc;
5382}
5383
Michael Chan78095922016-12-07 00:26:16 -05005384static void bnxt_clear_int_mode(struct bnxt *bp)
5385{
5386 if (bp->flags & BNXT_FLAG_USING_MSIX)
5387 pci_disable_msix(bp->pdev);
5388
5389 kfree(bp->irq_tbl);
5390 bp->irq_tbl = NULL;
5391 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5392}
5393
Michael Chanc0c050c2015-10-22 16:01:17 -04005394static void bnxt_free_irq(struct bnxt *bp)
5395{
5396 struct bnxt_irq *irq;
5397 int i;
5398
5399#ifdef CONFIG_RFS_ACCEL
5400 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5401 bp->dev->rx_cpu_rmap = NULL;
5402#endif
5403 if (!bp->irq_tbl)
5404 return;
5405
5406 for (i = 0; i < bp->cp_nr_rings; i++) {
5407 irq = &bp->irq_tbl[i];
5408 if (irq->requested)
5409 free_irq(irq->vector, bp->bnapi[i]);
5410 irq->requested = 0;
5411 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005412}
5413
5414static int bnxt_request_irq(struct bnxt *bp)
5415{
Michael Chanb81a90d2016-01-02 23:45:01 -05005416 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005417 unsigned long flags = 0;
5418#ifdef CONFIG_RFS_ACCEL
5419 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5420#endif
5421
5422 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5423 flags = IRQF_SHARED;
5424
Michael Chanb81a90d2016-01-02 23:45:01 -05005425 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005426 struct bnxt_irq *irq = &bp->irq_tbl[i];
5427#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005428 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005429 rc = irq_cpu_rmap_add(rmap, irq->vector);
5430 if (rc)
5431 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005432 j);
5433 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005434 }
5435#endif
5436 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5437 bp->bnapi[i]);
5438 if (rc)
5439 break;
5440
5441 irq->requested = 1;
5442 }
5443 return rc;
5444}
5445
5446static void bnxt_del_napi(struct bnxt *bp)
5447{
5448 int i;
5449
5450 if (!bp->bnapi)
5451 return;
5452
5453 for (i = 0; i < bp->cp_nr_rings; i++) {
5454 struct bnxt_napi *bnapi = bp->bnapi[i];
5455
5456 napi_hash_del(&bnapi->napi);
5457 netif_napi_del(&bnapi->napi);
5458 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005459 /* We called napi_hash_del() before netif_napi_del(), we need
5460 * to respect an RCU grace period before freeing napi structures.
5461 */
5462 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005463}
5464
5465static void bnxt_init_napi(struct bnxt *bp)
5466{
5467 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005468 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005469 struct bnxt_napi *bnapi;
5470
5471 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005472 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5473 cp_nr_rings--;
5474 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005475 bnapi = bp->bnapi[i];
5476 netif_napi_add(bp->dev, &bnapi->napi,
5477 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005478 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005479 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5480 bnapi = bp->bnapi[cp_nr_rings];
5481 netif_napi_add(bp->dev, &bnapi->napi,
5482 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005483 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005484 } else {
5485 bnapi = bp->bnapi[0];
5486 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005487 }
5488}
5489
5490static void bnxt_disable_napi(struct bnxt *bp)
5491{
5492 int i;
5493
5494 if (!bp->bnapi)
5495 return;
5496
Michael Chanb356a2e2016-12-29 12:13:31 -05005497 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005498 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005499}
5500
5501static void bnxt_enable_napi(struct bnxt *bp)
5502{
5503 int i;
5504
5505 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005506 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005507 napi_enable(&bp->bnapi[i]->napi);
5508 }
5509}
5510
Michael Chan7df4ae92016-12-02 21:17:17 -05005511void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005512{
5513 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005514 struct bnxt_tx_ring_info *txr;
5515 struct netdev_queue *txq;
5516
Michael Chanb6ab4b02016-01-02 23:44:59 -05005517 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005518 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005519 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005520 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005521 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005522 }
5523 }
5524 /* Stop all TX queues */
5525 netif_tx_disable(bp->dev);
5526 netif_carrier_off(bp->dev);
5527}
5528
Michael Chan7df4ae92016-12-02 21:17:17 -05005529void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005530{
5531 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005532 struct bnxt_tx_ring_info *txr;
5533 struct netdev_queue *txq;
5534
5535 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005536 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005537 txq = netdev_get_tx_queue(bp->dev, i);
5538 txr->dev_state = 0;
5539 }
5540 netif_tx_wake_all_queues(bp->dev);
5541 if (bp->link_info.link_up)
5542 netif_carrier_on(bp->dev);
5543}
5544
5545static void bnxt_report_link(struct bnxt *bp)
5546{
5547 if (bp->link_info.link_up) {
5548 const char *duplex;
5549 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005550 u32 speed;
5551 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005552
5553 netif_carrier_on(bp->dev);
5554 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5555 duplex = "full";
5556 else
5557 duplex = "half";
5558 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5559 flow_ctrl = "ON - receive & transmit";
5560 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5561 flow_ctrl = "ON - transmit";
5562 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5563 flow_ctrl = "ON - receive";
5564 else
5565 flow_ctrl = "none";
5566 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005567 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005568 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005569 if (bp->flags & BNXT_FLAG_EEE_CAP)
5570 netdev_info(bp->dev, "EEE is %s\n",
5571 bp->eee.eee_active ? "active" :
5572 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005573 fec = bp->link_info.fec_cfg;
5574 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5575 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5576 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5577 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5578 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005579 } else {
5580 netif_carrier_off(bp->dev);
5581 netdev_err(bp->dev, "NIC Link is Down\n");
5582 }
5583}
5584
Michael Chan170ce012016-04-05 14:08:57 -04005585static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5586{
5587 int rc = 0;
5588 struct hwrm_port_phy_qcaps_input req = {0};
5589 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005590 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005591
5592 if (bp->hwrm_spec_code < 0x10201)
5593 return 0;
5594
5595 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5596
5597 mutex_lock(&bp->hwrm_cmd_lock);
5598 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5599 if (rc)
5600 goto hwrm_phy_qcaps_exit;
5601
5602 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5603 struct ethtool_eee *eee = &bp->eee;
5604 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5605
5606 bp->flags |= BNXT_FLAG_EEE_CAP;
5607 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5608 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5609 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5610 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5611 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5612 }
Michael Chan520ad892017-03-08 18:44:35 -05005613 if (resp->supported_speeds_auto_mode)
5614 link_info->support_auto_speeds =
5615 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005616
5617hwrm_phy_qcaps_exit:
5618 mutex_unlock(&bp->hwrm_cmd_lock);
5619 return rc;
5620}
5621
Michael Chanc0c050c2015-10-22 16:01:17 -04005622static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5623{
5624 int rc = 0;
5625 struct bnxt_link_info *link_info = &bp->link_info;
5626 struct hwrm_port_phy_qcfg_input req = {0};
5627 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5628 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005629 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005630
5631 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5632
5633 mutex_lock(&bp->hwrm_cmd_lock);
5634 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5635 if (rc) {
5636 mutex_unlock(&bp->hwrm_cmd_lock);
5637 return rc;
5638 }
5639
5640 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5641 link_info->phy_link_status = resp->link;
5642 link_info->duplex = resp->duplex;
5643 link_info->pause = resp->pause;
5644 link_info->auto_mode = resp->auto_mode;
5645 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005646 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005647 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005648 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005649 if (link_info->phy_link_status == BNXT_LINK_LINK)
5650 link_info->link_speed = le16_to_cpu(resp->link_speed);
5651 else
5652 link_info->link_speed = 0;
5653 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005654 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5655 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005656 link_info->lp_auto_link_speeds =
5657 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005658 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5659 link_info->phy_ver[0] = resp->phy_maj;
5660 link_info->phy_ver[1] = resp->phy_min;
5661 link_info->phy_ver[2] = resp->phy_bld;
5662 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005663 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005664 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005665 link_info->phy_addr = resp->eee_config_phy_addr &
5666 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005667 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005668
Michael Chan170ce012016-04-05 14:08:57 -04005669 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5670 struct ethtool_eee *eee = &bp->eee;
5671 u16 fw_speeds;
5672
5673 eee->eee_active = 0;
5674 if (resp->eee_config_phy_addr &
5675 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5676 eee->eee_active = 1;
5677 fw_speeds = le16_to_cpu(
5678 resp->link_partner_adv_eee_link_speed_mask);
5679 eee->lp_advertised =
5680 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5681 }
5682
5683 /* Pull initial EEE config */
5684 if (!chng_link_state) {
5685 if (resp->eee_config_phy_addr &
5686 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5687 eee->eee_enabled = 1;
5688
5689 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5690 eee->advertised =
5691 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5692
5693 if (resp->eee_config_phy_addr &
5694 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5695 __le32 tmr;
5696
5697 eee->tx_lpi_enabled = 1;
5698 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5699 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5700 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5701 }
5702 }
5703 }
Michael Chane70c7522017-02-12 19:18:16 -05005704
5705 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5706 if (bp->hwrm_spec_code >= 0x10504)
5707 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5708
Michael Chanc0c050c2015-10-22 16:01:17 -04005709 /* TODO: need to add more logic to report VF link */
5710 if (chng_link_state) {
5711 if (link_info->phy_link_status == BNXT_LINK_LINK)
5712 link_info->link_up = 1;
5713 else
5714 link_info->link_up = 0;
5715 if (link_up != link_info->link_up)
5716 bnxt_report_link(bp);
5717 } else {
5718 /* alwasy link down if not require to update link state */
5719 link_info->link_up = 0;
5720 }
5721 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005722
5723 diff = link_info->support_auto_speeds ^ link_info->advertising;
5724 if ((link_info->support_auto_speeds | diff) !=
5725 link_info->support_auto_speeds) {
5726 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005727 * update the advertisement settings. Caller holds RTNL
5728 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005729 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005730 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005731 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005732 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005733 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005734 return 0;
5735}
5736
Michael Chan10289be2016-05-15 03:04:49 -04005737static void bnxt_get_port_module_status(struct bnxt *bp)
5738{
5739 struct bnxt_link_info *link_info = &bp->link_info;
5740 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5741 u8 module_status;
5742
5743 if (bnxt_update_link(bp, true))
5744 return;
5745
5746 module_status = link_info->module_status;
5747 switch (module_status) {
5748 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5749 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5750 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5751 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5752 bp->pf.port_id);
5753 if (bp->hwrm_spec_code >= 0x10201) {
5754 netdev_warn(bp->dev, "Module part number %s\n",
5755 resp->phy_vendor_partnumber);
5756 }
5757 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5758 netdev_warn(bp->dev, "TX is disabled\n");
5759 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5760 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5761 }
5762}
5763
Michael Chanc0c050c2015-10-22 16:01:17 -04005764static void
5765bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5766{
5767 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005768 if (bp->hwrm_spec_code >= 0x10201)
5769 req->auto_pause =
5770 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005771 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5772 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5773 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005774 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005775 req->enables |=
5776 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5777 } else {
5778 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5779 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5780 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5781 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5782 req->enables |=
5783 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005784 if (bp->hwrm_spec_code >= 0x10201) {
5785 req->auto_pause = req->force_pause;
5786 req->enables |= cpu_to_le32(
5787 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5788 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005789 }
5790}
5791
5792static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5793 struct hwrm_port_phy_cfg_input *req)
5794{
5795 u8 autoneg = bp->link_info.autoneg;
5796 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005797 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005798
5799 if (autoneg & BNXT_AUTONEG_SPEED) {
5800 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005801 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005802
5803 req->enables |= cpu_to_le32(
5804 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5805 req->auto_link_speed_mask = cpu_to_le16(advertising);
5806
5807 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5808 req->flags |=
5809 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5810 } else {
5811 req->force_link_speed = cpu_to_le16(fw_link_speed);
5812 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5813 }
5814
Michael Chanc0c050c2015-10-22 16:01:17 -04005815 /* tell chimp that the setting takes effect immediately */
5816 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5817}
5818
5819int bnxt_hwrm_set_pause(struct bnxt *bp)
5820{
5821 struct hwrm_port_phy_cfg_input req = {0};
5822 int rc;
5823
5824 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5825 bnxt_hwrm_set_pause_common(bp, &req);
5826
5827 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5828 bp->link_info.force_link_chng)
5829 bnxt_hwrm_set_link_common(bp, &req);
5830
5831 mutex_lock(&bp->hwrm_cmd_lock);
5832 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5833 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5834 /* since changing of pause setting doesn't trigger any link
5835 * change event, the driver needs to update the current pause
5836 * result upon successfully return of the phy_cfg command
5837 */
5838 bp->link_info.pause =
5839 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5840 bp->link_info.auto_pause_setting = 0;
5841 if (!bp->link_info.force_link_chng)
5842 bnxt_report_link(bp);
5843 }
5844 bp->link_info.force_link_chng = false;
5845 mutex_unlock(&bp->hwrm_cmd_lock);
5846 return rc;
5847}
5848
Michael Chan939f7f02016-04-05 14:08:58 -04005849static void bnxt_hwrm_set_eee(struct bnxt *bp,
5850 struct hwrm_port_phy_cfg_input *req)
5851{
5852 struct ethtool_eee *eee = &bp->eee;
5853
5854 if (eee->eee_enabled) {
5855 u16 eee_speeds;
5856 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5857
5858 if (eee->tx_lpi_enabled)
5859 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5860 else
5861 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5862
5863 req->flags |= cpu_to_le32(flags);
5864 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5865 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5866 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5867 } else {
5868 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5869 }
5870}
5871
5872int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005873{
5874 struct hwrm_port_phy_cfg_input req = {0};
5875
5876 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5877 if (set_pause)
5878 bnxt_hwrm_set_pause_common(bp, &req);
5879
5880 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005881
5882 if (set_eee)
5883 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005884 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5885}
5886
Michael Chan33f7d552016-04-11 04:11:12 -04005887static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5888{
5889 struct hwrm_port_phy_cfg_input req = {0};
5890
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005891 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005892 return 0;
5893
5894 if (pci_num_vf(bp->pdev))
5895 return 0;
5896
5897 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005898 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005899 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5900}
5901
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005902static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5903{
5904 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5905 struct hwrm_port_led_qcaps_input req = {0};
5906 struct bnxt_pf_info *pf = &bp->pf;
5907 int rc;
5908
5909 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5910 return 0;
5911
5912 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5913 req.port_id = cpu_to_le16(pf->port_id);
5914 mutex_lock(&bp->hwrm_cmd_lock);
5915 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5916 if (rc) {
5917 mutex_unlock(&bp->hwrm_cmd_lock);
5918 return rc;
5919 }
5920 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5921 int i;
5922
5923 bp->num_leds = resp->num_leds;
5924 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5925 bp->num_leds);
5926 for (i = 0; i < bp->num_leds; i++) {
5927 struct bnxt_led_info *led = &bp->leds[i];
5928 __le16 caps = led->led_state_caps;
5929
5930 if (!led->led_group_id ||
5931 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5932 bp->num_leds = 0;
5933 break;
5934 }
5935 }
5936 }
5937 mutex_unlock(&bp->hwrm_cmd_lock);
5938 return 0;
5939}
5940
Michael Chan5282db62017-04-04 18:14:10 -04005941int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
5942{
5943 struct hwrm_wol_filter_alloc_input req = {0};
5944 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5945 int rc;
5946
5947 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
5948 req.port_id = cpu_to_le16(bp->pf.port_id);
5949 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
5950 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
5951 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
5952 mutex_lock(&bp->hwrm_cmd_lock);
5953 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5954 if (!rc)
5955 bp->wol_filter_id = resp->wol_filter_id;
5956 mutex_unlock(&bp->hwrm_cmd_lock);
5957 return rc;
5958}
5959
5960int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
5961{
5962 struct hwrm_wol_filter_free_input req = {0};
5963 int rc;
5964
5965 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
5966 req.port_id = cpu_to_le16(bp->pf.port_id);
5967 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
5968 req.wol_filter_id = bp->wol_filter_id;
5969 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5970 return rc;
5971}
5972
Michael Chanc1ef1462017-04-04 18:14:07 -04005973static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
5974{
5975 struct hwrm_wol_filter_qcfg_input req = {0};
5976 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5977 u16 next_handle = 0;
5978 int rc;
5979
5980 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
5981 req.port_id = cpu_to_le16(bp->pf.port_id);
5982 req.handle = cpu_to_le16(handle);
5983 mutex_lock(&bp->hwrm_cmd_lock);
5984 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5985 if (!rc) {
5986 next_handle = le16_to_cpu(resp->next_handle);
5987 if (next_handle != 0) {
5988 if (resp->wol_type ==
5989 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
5990 bp->wol = 1;
5991 bp->wol_filter_id = resp->wol_filter_id;
5992 }
5993 }
5994 }
5995 mutex_unlock(&bp->hwrm_cmd_lock);
5996 return next_handle;
5997}
5998
5999static void bnxt_get_wol_settings(struct bnxt *bp)
6000{
6001 u16 handle = 0;
6002
6003 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6004 return;
6005
6006 do {
6007 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6008 } while (handle && handle != 0xffff);
6009}
6010
Michael Chan939f7f02016-04-05 14:08:58 -04006011static bool bnxt_eee_config_ok(struct bnxt *bp)
6012{
6013 struct ethtool_eee *eee = &bp->eee;
6014 struct bnxt_link_info *link_info = &bp->link_info;
6015
6016 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6017 return true;
6018
6019 if (eee->eee_enabled) {
6020 u32 advertising =
6021 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6022
6023 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6024 eee->eee_enabled = 0;
6025 return false;
6026 }
6027 if (eee->advertised & ~advertising) {
6028 eee->advertised = advertising & eee->supported;
6029 return false;
6030 }
6031 }
6032 return true;
6033}
6034
Michael Chanc0c050c2015-10-22 16:01:17 -04006035static int bnxt_update_phy_setting(struct bnxt *bp)
6036{
6037 int rc;
6038 bool update_link = false;
6039 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006040 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006041 struct bnxt_link_info *link_info = &bp->link_info;
6042
6043 rc = bnxt_update_link(bp, true);
6044 if (rc) {
6045 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6046 rc);
6047 return rc;
6048 }
Michael Chan33dac242017-02-12 19:18:15 -05006049 if (!BNXT_SINGLE_PF(bp))
6050 return 0;
6051
Michael Chanc0c050c2015-10-22 16:01:17 -04006052 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006053 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6054 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006055 update_pause = true;
6056 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6057 link_info->force_pause_setting != link_info->req_flow_ctrl)
6058 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006059 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6060 if (BNXT_AUTO_MODE(link_info->auto_mode))
6061 update_link = true;
6062 if (link_info->req_link_speed != link_info->force_link_speed)
6063 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006064 if (link_info->req_duplex != link_info->duplex_setting)
6065 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006066 } else {
6067 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6068 update_link = true;
6069 if (link_info->advertising != link_info->auto_link_speeds)
6070 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006071 }
6072
Michael Chan16d663a2016-11-16 21:13:07 -05006073 /* The last close may have shutdown the link, so need to call
6074 * PHY_CFG to bring it back up.
6075 */
6076 if (!netif_carrier_ok(bp->dev))
6077 update_link = true;
6078
Michael Chan939f7f02016-04-05 14:08:58 -04006079 if (!bnxt_eee_config_ok(bp))
6080 update_eee = true;
6081
Michael Chanc0c050c2015-10-22 16:01:17 -04006082 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006083 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006084 else if (update_pause)
6085 rc = bnxt_hwrm_set_pause(bp);
6086 if (rc) {
6087 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6088 rc);
6089 return rc;
6090 }
6091
6092 return rc;
6093}
6094
Jeffrey Huang11809492015-11-05 16:25:49 -05006095/* Common routine to pre-map certain register block to different GRC window.
6096 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6097 * in PF and 3 windows in VF that can be customized to map in different
6098 * register blocks.
6099 */
6100static void bnxt_preset_reg_win(struct bnxt *bp)
6101{
6102 if (BNXT_PF(bp)) {
6103 /* CAG registers map to GRC window #4 */
6104 writel(BNXT_CAG_REG_BASE,
6105 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6106 }
6107}
6108
Michael Chanc0c050c2015-10-22 16:01:17 -04006109static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6110{
6111 int rc = 0;
6112
Jeffrey Huang11809492015-11-05 16:25:49 -05006113 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006114 netif_carrier_off(bp->dev);
6115 if (irq_re_init) {
6116 rc = bnxt_setup_int_mode(bp);
6117 if (rc) {
6118 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6119 rc);
6120 return rc;
6121 }
6122 }
6123 if ((bp->flags & BNXT_FLAG_RFS) &&
6124 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6125 /* disable RFS if falling back to INTA */
6126 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6127 bp->flags &= ~BNXT_FLAG_RFS;
6128 }
6129
6130 rc = bnxt_alloc_mem(bp, irq_re_init);
6131 if (rc) {
6132 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6133 goto open_err_free_mem;
6134 }
6135
6136 if (irq_re_init) {
6137 bnxt_init_napi(bp);
6138 rc = bnxt_request_irq(bp);
6139 if (rc) {
6140 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6141 goto open_err;
6142 }
6143 }
6144
6145 bnxt_enable_napi(bp);
6146
6147 rc = bnxt_init_nic(bp, irq_re_init);
6148 if (rc) {
6149 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6150 goto open_err;
6151 }
6152
6153 if (link_re_init) {
6154 rc = bnxt_update_phy_setting(bp);
6155 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006156 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006157 }
6158
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006159 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006160 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006161
Michael Chancaefe522015-12-09 19:35:42 -05006162 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006163 bnxt_enable_int(bp);
6164 /* Enable TX queues */
6165 bnxt_tx_enable(bp);
6166 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006167 /* Poll link status and check for SFP+ module status */
6168 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006169
6170 return 0;
6171
6172open_err:
6173 bnxt_disable_napi(bp);
6174 bnxt_del_napi(bp);
6175
6176open_err_free_mem:
6177 bnxt_free_skbs(bp);
6178 bnxt_free_irq(bp);
6179 bnxt_free_mem(bp, true);
6180 return rc;
6181}
6182
6183/* rtnl_lock held */
6184int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6185{
6186 int rc = 0;
6187
6188 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6189 if (rc) {
6190 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6191 dev_close(bp->dev);
6192 }
6193 return rc;
6194}
6195
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006196/* rtnl_lock held, open the NIC half way by allocating all resources, but
6197 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6198 * self tests.
6199 */
6200int bnxt_half_open_nic(struct bnxt *bp)
6201{
6202 int rc = 0;
6203
6204 rc = bnxt_alloc_mem(bp, false);
6205 if (rc) {
6206 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6207 goto half_open_err;
6208 }
6209 rc = bnxt_init_nic(bp, false);
6210 if (rc) {
6211 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6212 goto half_open_err;
6213 }
6214 return 0;
6215
6216half_open_err:
6217 bnxt_free_skbs(bp);
6218 bnxt_free_mem(bp, false);
6219 dev_close(bp->dev);
6220 return rc;
6221}
6222
6223/* rtnl_lock held, this call can only be made after a previous successful
6224 * call to bnxt_half_open_nic().
6225 */
6226void bnxt_half_close_nic(struct bnxt *bp)
6227{
6228 bnxt_hwrm_resource_free(bp, false, false);
6229 bnxt_free_skbs(bp);
6230 bnxt_free_mem(bp, false);
6231}
6232
Michael Chanc0c050c2015-10-22 16:01:17 -04006233static int bnxt_open(struct net_device *dev)
6234{
6235 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006236
Michael Chanc0c050c2015-10-22 16:01:17 -04006237 return __bnxt_open_nic(bp, true, true);
6238}
6239
Michael Chanc0c050c2015-10-22 16:01:17 -04006240int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6241{
6242 int rc = 0;
6243
6244#ifdef CONFIG_BNXT_SRIOV
6245 if (bp->sriov_cfg) {
6246 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6247 !bp->sriov_cfg,
6248 BNXT_SRIOV_CFG_WAIT_TMO);
6249 if (rc)
6250 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6251 }
6252#endif
6253 /* Change device state to avoid TX queue wake up's */
6254 bnxt_tx_disable(bp);
6255
Michael Chancaefe522015-12-09 19:35:42 -05006256 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006257 smp_mb__after_atomic();
6258 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
6259 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006260
Michael Chan9d8bc092016-12-29 12:13:33 -05006261 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006262 bnxt_shutdown_nic(bp, irq_re_init);
6263
6264 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6265
6266 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006267 del_timer_sync(&bp->timer);
6268 bnxt_free_skbs(bp);
6269
6270 if (irq_re_init) {
6271 bnxt_free_irq(bp);
6272 bnxt_del_napi(bp);
6273 }
6274 bnxt_free_mem(bp, irq_re_init);
6275 return rc;
6276}
6277
6278static int bnxt_close(struct net_device *dev)
6279{
6280 struct bnxt *bp = netdev_priv(dev);
6281
6282 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006283 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006284 return 0;
6285}
6286
6287/* rtnl_lock held */
6288static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6289{
6290 switch (cmd) {
6291 case SIOCGMIIPHY:
6292 /* fallthru */
6293 case SIOCGMIIREG: {
6294 if (!netif_running(dev))
6295 return -EAGAIN;
6296
6297 return 0;
6298 }
6299
6300 case SIOCSMIIREG:
6301 if (!netif_running(dev))
6302 return -EAGAIN;
6303
6304 return 0;
6305
6306 default:
6307 /* do nothing */
6308 break;
6309 }
6310 return -EOPNOTSUPP;
6311}
6312
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006313static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006314bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6315{
6316 u32 i;
6317 struct bnxt *bp = netdev_priv(dev);
6318
Michael Chanc0c050c2015-10-22 16:01:17 -04006319 if (!bp->bnapi)
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006320 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006321
6322 /* TODO check if we need to synchronize with bnxt_close path */
6323 for (i = 0; i < bp->cp_nr_rings; i++) {
6324 struct bnxt_napi *bnapi = bp->bnapi[i];
6325 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6326 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6327
6328 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6329 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6330 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6331
6332 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6333 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6334 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6335
6336 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6337 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6338 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6339
6340 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6341 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6342 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6343
6344 stats->rx_missed_errors +=
6345 le64_to_cpu(hw_stats->rx_discard_pkts);
6346
6347 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6348
Michael Chanc0c050c2015-10-22 16:01:17 -04006349 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6350 }
6351
Michael Chan9947f832016-03-07 15:38:46 -05006352 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6353 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6354 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6355
6356 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6357 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6358 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6359 le64_to_cpu(rx->rx_ovrsz_frames) +
6360 le64_to_cpu(rx->rx_runt_frames);
6361 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6362 le64_to_cpu(rx->rx_jbr_frames);
6363 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6364 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6365 stats->tx_errors = le64_to_cpu(tx->tx_err);
6366 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006367}
6368
6369static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6370{
6371 struct net_device *dev = bp->dev;
6372 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6373 struct netdev_hw_addr *ha;
6374 u8 *haddr;
6375 int mc_count = 0;
6376 bool update = false;
6377 int off = 0;
6378
6379 netdev_for_each_mc_addr(ha, dev) {
6380 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6381 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6382 vnic->mc_list_count = 0;
6383 return false;
6384 }
6385 haddr = ha->addr;
6386 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6387 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6388 update = true;
6389 }
6390 off += ETH_ALEN;
6391 mc_count++;
6392 }
6393 if (mc_count)
6394 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6395
6396 if (mc_count != vnic->mc_list_count) {
6397 vnic->mc_list_count = mc_count;
6398 update = true;
6399 }
6400 return update;
6401}
6402
6403static bool bnxt_uc_list_updated(struct bnxt *bp)
6404{
6405 struct net_device *dev = bp->dev;
6406 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6407 struct netdev_hw_addr *ha;
6408 int off = 0;
6409
6410 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6411 return true;
6412
6413 netdev_for_each_uc_addr(ha, dev) {
6414 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6415 return true;
6416
6417 off += ETH_ALEN;
6418 }
6419 return false;
6420}
6421
6422static void bnxt_set_rx_mode(struct net_device *dev)
6423{
6424 struct bnxt *bp = netdev_priv(dev);
6425 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6426 u32 mask = vnic->rx_mask;
6427 bool mc_update = false;
6428 bool uc_update;
6429
6430 if (!netif_running(dev))
6431 return;
6432
6433 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6434 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6435 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6436
Michael Chan17c71ac2016-07-01 18:46:27 -04006437 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006438 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6439
6440 uc_update = bnxt_uc_list_updated(bp);
6441
6442 if (dev->flags & IFF_ALLMULTI) {
6443 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6444 vnic->mc_list_count = 0;
6445 } else {
6446 mc_update = bnxt_mc_list_updated(bp, &mask);
6447 }
6448
6449 if (mask != vnic->rx_mask || uc_update || mc_update) {
6450 vnic->rx_mask = mask;
6451
6452 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6453 schedule_work(&bp->sp_task);
6454 }
6455}
6456
Michael Chanb664f002015-12-02 01:54:08 -05006457static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006458{
6459 struct net_device *dev = bp->dev;
6460 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6461 struct netdev_hw_addr *ha;
6462 int i, off = 0, rc;
6463 bool uc_update;
6464
6465 netif_addr_lock_bh(dev);
6466 uc_update = bnxt_uc_list_updated(bp);
6467 netif_addr_unlock_bh(dev);
6468
6469 if (!uc_update)
6470 goto skip_uc;
6471
6472 mutex_lock(&bp->hwrm_cmd_lock);
6473 for (i = 1; i < vnic->uc_filter_count; i++) {
6474 struct hwrm_cfa_l2_filter_free_input req = {0};
6475
6476 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6477 -1);
6478
6479 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6480
6481 rc = _hwrm_send_message(bp, &req, sizeof(req),
6482 HWRM_CMD_TIMEOUT);
6483 }
6484 mutex_unlock(&bp->hwrm_cmd_lock);
6485
6486 vnic->uc_filter_count = 1;
6487
6488 netif_addr_lock_bh(dev);
6489 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6490 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6491 } else {
6492 netdev_for_each_uc_addr(ha, dev) {
6493 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6494 off += ETH_ALEN;
6495 vnic->uc_filter_count++;
6496 }
6497 }
6498 netif_addr_unlock_bh(dev);
6499
6500 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6501 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6502 if (rc) {
6503 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6504 rc);
6505 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006506 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006507 }
6508 }
6509
6510skip_uc:
6511 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6512 if (rc)
6513 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6514 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006515
6516 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006517}
6518
Michael Chan8079e8f2016-12-29 12:13:37 -05006519/* If the chip and firmware supports RFS */
6520static bool bnxt_rfs_supported(struct bnxt *bp)
6521{
6522 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6523 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006524 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6525 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006526 return false;
6527}
6528
6529/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006530static bool bnxt_rfs_capable(struct bnxt *bp)
6531{
6532#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006533 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006534
Michael Chan964fd482017-02-12 19:18:13 -05006535 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006536 return false;
6537
6538 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006539 max_vnics = bnxt_get_max_func_vnics(bp);
6540 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006541
6542 /* RSS contexts not a limiting factor */
6543 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6544 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006545 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006546 netdev_warn(bp->dev,
6547 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006548 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006549 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006550 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006551
6552 return true;
6553#else
6554 return false;
6555#endif
6556}
6557
Michael Chanc0c050c2015-10-22 16:01:17 -04006558static netdev_features_t bnxt_fix_features(struct net_device *dev,
6559 netdev_features_t features)
6560{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006561 struct bnxt *bp = netdev_priv(dev);
6562
Vasundhara Volama2304902016-07-25 12:33:36 -04006563 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006564 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006565
6566 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6567 * turned on or off together.
6568 */
6569 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6570 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6571 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6572 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6573 NETIF_F_HW_VLAN_STAG_RX);
6574 else
6575 features |= NETIF_F_HW_VLAN_CTAG_RX |
6576 NETIF_F_HW_VLAN_STAG_RX;
6577 }
Michael Chancf6645f2016-06-13 02:25:28 -04006578#ifdef CONFIG_BNXT_SRIOV
6579 if (BNXT_VF(bp)) {
6580 if (bp->vf.vlan) {
6581 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6582 NETIF_F_HW_VLAN_STAG_RX);
6583 }
6584 }
6585#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006586 return features;
6587}
6588
6589static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6590{
6591 struct bnxt *bp = netdev_priv(dev);
6592 u32 flags = bp->flags;
6593 u32 changes;
6594 int rc = 0;
6595 bool re_init = false;
6596 bool update_tpa = false;
6597
6598 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006599 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006600 flags |= BNXT_FLAG_GRO;
6601 if (features & NETIF_F_LRO)
6602 flags |= BNXT_FLAG_LRO;
6603
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006604 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6605 flags &= ~BNXT_FLAG_TPA;
6606
Michael Chanc0c050c2015-10-22 16:01:17 -04006607 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6608 flags |= BNXT_FLAG_STRIP_VLAN;
6609
6610 if (features & NETIF_F_NTUPLE)
6611 flags |= BNXT_FLAG_RFS;
6612
6613 changes = flags ^ bp->flags;
6614 if (changes & BNXT_FLAG_TPA) {
6615 update_tpa = true;
6616 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6617 (flags & BNXT_FLAG_TPA) == 0)
6618 re_init = true;
6619 }
6620
6621 if (changes & ~BNXT_FLAG_TPA)
6622 re_init = true;
6623
6624 if (flags != bp->flags) {
6625 u32 old_flags = bp->flags;
6626
6627 bp->flags = flags;
6628
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006629 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006630 if (update_tpa)
6631 bnxt_set_ring_params(bp);
6632 return rc;
6633 }
6634
6635 if (re_init) {
6636 bnxt_close_nic(bp, false, false);
6637 if (update_tpa)
6638 bnxt_set_ring_params(bp);
6639
6640 return bnxt_open_nic(bp, false, false);
6641 }
6642 if (update_tpa) {
6643 rc = bnxt_set_tpa(bp,
6644 (flags & BNXT_FLAG_TPA) ?
6645 true : false);
6646 if (rc)
6647 bp->flags = old_flags;
6648 }
6649 }
6650 return rc;
6651}
6652
Michael Chan9f554592016-01-02 23:44:58 -05006653static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6654{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006655 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006656 int i = bnapi->index;
6657
Michael Chan3b2b7d92016-01-02 23:45:00 -05006658 if (!txr)
6659 return;
6660
Michael Chan9f554592016-01-02 23:44:58 -05006661 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6662 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6663 txr->tx_cons);
6664}
6665
6666static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6667{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006668 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006669 int i = bnapi->index;
6670
Michael Chan3b2b7d92016-01-02 23:45:00 -05006671 if (!rxr)
6672 return;
6673
Michael Chan9f554592016-01-02 23:44:58 -05006674 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",
6675 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6676 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6677 rxr->rx_sw_agg_prod);
6678}
6679
6680static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6681{
6682 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6683 int i = bnapi->index;
6684
6685 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6686 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6687}
6688
Michael Chanc0c050c2015-10-22 16:01:17 -04006689static void bnxt_dbg_dump_states(struct bnxt *bp)
6690{
6691 int i;
6692 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006693
6694 for (i = 0; i < bp->cp_nr_rings; i++) {
6695 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006696 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006697 bnxt_dump_tx_sw_state(bnapi);
6698 bnxt_dump_rx_sw_state(bnapi);
6699 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006700 }
6701 }
6702}
6703
Michael Chan6988bd92016-06-13 02:25:29 -04006704static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006705{
Michael Chan6988bd92016-06-13 02:25:29 -04006706 if (!silent)
6707 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006708 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006709 int rc;
6710
6711 if (!silent)
6712 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006713 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006714 rc = bnxt_open_nic(bp, false, false);
6715 if (!silent && !rc)
6716 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006717 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006718}
6719
6720static void bnxt_tx_timeout(struct net_device *dev)
6721{
6722 struct bnxt *bp = netdev_priv(dev);
6723
6724 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6725 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6726 schedule_work(&bp->sp_task);
6727}
6728
6729#ifdef CONFIG_NET_POLL_CONTROLLER
6730static void bnxt_poll_controller(struct net_device *dev)
6731{
6732 struct bnxt *bp = netdev_priv(dev);
6733 int i;
6734
6735 for (i = 0; i < bp->cp_nr_rings; i++) {
6736 struct bnxt_irq *irq = &bp->irq_tbl[i];
6737
6738 disable_irq(irq->vector);
6739 irq->handler(irq->vector, bp->bnapi[i]);
6740 enable_irq(irq->vector);
6741 }
6742}
6743#endif
6744
6745static void bnxt_timer(unsigned long data)
6746{
6747 struct bnxt *bp = (struct bnxt *)data;
6748 struct net_device *dev = bp->dev;
6749
6750 if (!netif_running(dev))
6751 return;
6752
6753 if (atomic_read(&bp->intr_sem) != 0)
6754 goto bnxt_restart_timer;
6755
Michael Chan3bdf56c2016-03-07 15:38:45 -05006756 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6757 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6758 schedule_work(&bp->sp_task);
6759 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006760bnxt_restart_timer:
6761 mod_timer(&bp->timer, jiffies + bp->current_interval);
6762}
6763
Michael Chana551ee92017-01-25 02:55:07 -05006764static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006765{
Michael Chana551ee92017-01-25 02:55:07 -05006766 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6767 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006768 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6769 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6770 */
6771 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6772 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006773}
6774
6775static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6776{
Michael Chan6988bd92016-06-13 02:25:29 -04006777 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6778 rtnl_unlock();
6779}
6780
Michael Chana551ee92017-01-25 02:55:07 -05006781/* Only called from bnxt_sp_task() */
6782static void bnxt_reset(struct bnxt *bp, bool silent)
6783{
6784 bnxt_rtnl_lock_sp(bp);
6785 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6786 bnxt_reset_task(bp, silent);
6787 bnxt_rtnl_unlock_sp(bp);
6788}
6789
Michael Chanc0c050c2015-10-22 16:01:17 -04006790static void bnxt_cfg_ntp_filters(struct bnxt *);
6791
6792static void bnxt_sp_task(struct work_struct *work)
6793{
6794 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006795
Michael Chan4cebdce2015-12-09 19:35:43 -05006796 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6797 smp_mb__after_atomic();
6798 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6799 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006800 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006801 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006802
6803 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6804 bnxt_cfg_rx_mode(bp);
6805
6806 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6807 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006808 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6809 bnxt_hwrm_exec_fwd_req(bp);
6810 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6811 bnxt_hwrm_tunnel_dst_port_alloc(
6812 bp, bp->vxlan_port,
6813 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6814 }
6815 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6816 bnxt_hwrm_tunnel_dst_port_free(
6817 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6818 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006819 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6820 bnxt_hwrm_tunnel_dst_port_alloc(
6821 bp, bp->nge_port,
6822 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6823 }
6824 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6825 bnxt_hwrm_tunnel_dst_port_free(
6826 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6827 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006828 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6829 bnxt_hwrm_port_qstats(bp);
6830
Michael Chana551ee92017-01-25 02:55:07 -05006831 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6832 * must be the last functions to be called before exiting.
6833 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006834 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6835 int rc = 0;
6836
6837 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6838 &bp->sp_event))
6839 bnxt_hwrm_phy_qcaps(bp);
6840
6841 bnxt_rtnl_lock_sp(bp);
6842 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6843 rc = bnxt_update_link(bp, true);
6844 bnxt_rtnl_unlock_sp(bp);
6845 if (rc)
6846 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6847 rc);
6848 }
Michael Chan90c694b2017-01-25 02:55:09 -05006849 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6850 bnxt_rtnl_lock_sp(bp);
6851 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6852 bnxt_get_port_module_status(bp);
6853 bnxt_rtnl_unlock_sp(bp);
6854 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006855 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6856 bnxt_reset(bp, false);
6857
6858 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6859 bnxt_reset(bp, true);
6860
Michael Chanc0c050c2015-10-22 16:01:17 -04006861 smp_mb__before_atomic();
6862 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6863}
6864
Michael Chand1e79252017-02-06 16:55:38 -05006865/* Under rtnl_lock */
Michael Chan5f449242017-02-06 16:55:40 -05006866int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, int tcs, int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006867{
6868 int max_rx, max_tx, tx_sets = 1;
6869 int tx_rings_needed;
6870 bool sh = true;
6871 int rc;
6872
6873 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
6874 sh = false;
6875
6876 if (tcs)
6877 tx_sets = tcs;
6878
6879 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6880 if (rc)
6881 return rc;
6882
6883 if (max_rx < rx)
6884 return -ENOMEM;
6885
Michael Chan5f449242017-02-06 16:55:40 -05006886 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006887 if (max_tx < tx_rings_needed)
6888 return -ENOMEM;
6889
6890 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006891 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006892 return -ENOMEM;
6893 return 0;
6894}
6895
Sathya Perla17086392017-02-20 19:25:18 -05006896static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6897{
6898 if (bp->bar2) {
6899 pci_iounmap(pdev, bp->bar2);
6900 bp->bar2 = NULL;
6901 }
6902
6903 if (bp->bar1) {
6904 pci_iounmap(pdev, bp->bar1);
6905 bp->bar1 = NULL;
6906 }
6907
6908 if (bp->bar0) {
6909 pci_iounmap(pdev, bp->bar0);
6910 bp->bar0 = NULL;
6911 }
6912}
6913
6914static void bnxt_cleanup_pci(struct bnxt *bp)
6915{
6916 bnxt_unmap_bars(bp, bp->pdev);
6917 pci_release_regions(bp->pdev);
6918 pci_disable_device(bp->pdev);
6919}
6920
Michael Chanc0c050c2015-10-22 16:01:17 -04006921static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6922{
6923 int rc;
6924 struct bnxt *bp = netdev_priv(dev);
6925
6926 SET_NETDEV_DEV(dev, &pdev->dev);
6927
6928 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6929 rc = pci_enable_device(pdev);
6930 if (rc) {
6931 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6932 goto init_err;
6933 }
6934
6935 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6936 dev_err(&pdev->dev,
6937 "Cannot find PCI device base address, aborting\n");
6938 rc = -ENODEV;
6939 goto init_err_disable;
6940 }
6941
6942 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6943 if (rc) {
6944 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6945 goto init_err_disable;
6946 }
6947
6948 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6949 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6950 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6951 goto init_err_disable;
6952 }
6953
6954 pci_set_master(pdev);
6955
6956 bp->dev = dev;
6957 bp->pdev = pdev;
6958
6959 bp->bar0 = pci_ioremap_bar(pdev, 0);
6960 if (!bp->bar0) {
6961 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6962 rc = -ENOMEM;
6963 goto init_err_release;
6964 }
6965
6966 bp->bar1 = pci_ioremap_bar(pdev, 2);
6967 if (!bp->bar1) {
6968 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6969 rc = -ENOMEM;
6970 goto init_err_release;
6971 }
6972
6973 bp->bar2 = pci_ioremap_bar(pdev, 4);
6974 if (!bp->bar2) {
6975 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6976 rc = -ENOMEM;
6977 goto init_err_release;
6978 }
6979
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006980 pci_enable_pcie_error_reporting(pdev);
6981
Michael Chanc0c050c2015-10-22 16:01:17 -04006982 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6983
6984 spin_lock_init(&bp->ntp_fltr_lock);
6985
6986 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6987 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6988
Michael Chandfb5b892016-02-26 04:00:01 -05006989 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006990 bp->rx_coal_ticks = 12;
6991 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006992 bp->rx_coal_ticks_irq = 1;
6993 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006994
Michael Chandfc9c942016-02-26 04:00:03 -05006995 bp->tx_coal_ticks = 25;
6996 bp->tx_coal_bufs = 30;
6997 bp->tx_coal_ticks_irq = 2;
6998 bp->tx_coal_bufs_irq = 2;
6999
Michael Chan51f30782016-07-01 18:46:29 -04007000 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7001
Michael Chanc0c050c2015-10-22 16:01:17 -04007002 init_timer(&bp->timer);
7003 bp->timer.data = (unsigned long)bp;
7004 bp->timer.function = bnxt_timer;
7005 bp->current_interval = BNXT_TIMER_INTERVAL;
7006
Michael Chancaefe522015-12-09 19:35:42 -05007007 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007008 return 0;
7009
7010init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007011 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007012 pci_release_regions(pdev);
7013
7014init_err_disable:
7015 pci_disable_device(pdev);
7016
7017init_err:
7018 return rc;
7019}
7020
7021/* rtnl_lock held */
7022static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7023{
7024 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007025 struct bnxt *bp = netdev_priv(dev);
7026 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007027
7028 if (!is_valid_ether_addr(addr->sa_data))
7029 return -EADDRNOTAVAIL;
7030
Michael Chan84c33dd2016-04-11 04:11:13 -04007031 rc = bnxt_approve_mac(bp, addr->sa_data);
7032 if (rc)
7033 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007034
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007035 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7036 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007037
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007038 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7039 if (netif_running(dev)) {
7040 bnxt_close_nic(bp, false, false);
7041 rc = bnxt_open_nic(bp, false, false);
7042 }
7043
7044 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007045}
7046
7047/* rtnl_lock held */
7048static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7049{
7050 struct bnxt *bp = netdev_priv(dev);
7051
Michael Chanc0c050c2015-10-22 16:01:17 -04007052 if (netif_running(dev))
7053 bnxt_close_nic(bp, false, false);
7054
7055 dev->mtu = new_mtu;
7056 bnxt_set_ring_params(bp);
7057
7058 if (netif_running(dev))
7059 return bnxt_open_nic(bp, false, false);
7060
7061 return 0;
7062}
7063
Michael Chanc5e3deb2016-12-02 21:17:15 -05007064int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007065{
7066 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007067 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007068 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007069
Michael Chanc0c050c2015-10-22 16:01:17 -04007070 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007071 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007072 tc, bp->max_tc);
7073 return -EINVAL;
7074 }
7075
7076 if (netdev_get_num_tc(dev) == tc)
7077 return 0;
7078
Michael Chan3ffb6a32016-11-11 00:11:42 -05007079 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7080 sh = true;
7081
Michael Chan5f449242017-02-06 16:55:40 -05007082 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7083 tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007084 if (rc)
7085 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007086
7087 /* Needs to close the device and do hw resource re-allocations */
7088 if (netif_running(bp->dev))
7089 bnxt_close_nic(bp, true, false);
7090
7091 if (tc) {
7092 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7093 netdev_set_num_tc(dev, tc);
7094 } else {
7095 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7096 netdev_reset_tc(dev);
7097 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007098 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7099 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007100 bp->num_stat_ctxs = bp->cp_nr_rings;
7101
7102 if (netif_running(bp->dev))
7103 return bnxt_open_nic(bp, true, false);
7104
7105 return 0;
7106}
7107
Michael Chanc5e3deb2016-12-02 21:17:15 -05007108static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
7109 struct tc_to_netdev *ntc)
7110{
7111 if (ntc->type != TC_SETUP_MQPRIO)
7112 return -EINVAL;
7113
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007114 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7115
7116 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007117}
7118
Michael Chanc0c050c2015-10-22 16:01:17 -04007119#ifdef CONFIG_RFS_ACCEL
7120static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7121 struct bnxt_ntuple_filter *f2)
7122{
7123 struct flow_keys *keys1 = &f1->fkeys;
7124 struct flow_keys *keys2 = &f2->fkeys;
7125
7126 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7127 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7128 keys1->ports.ports == keys2->ports.ports &&
7129 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7130 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007131 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007132 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7133 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007134 return true;
7135
7136 return false;
7137}
7138
7139static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7140 u16 rxq_index, u32 flow_id)
7141{
7142 struct bnxt *bp = netdev_priv(dev);
7143 struct bnxt_ntuple_filter *fltr, *new_fltr;
7144 struct flow_keys *fkeys;
7145 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007146 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007147 struct hlist_head *head;
7148
Michael Chana54c4d72016-07-25 12:33:35 -04007149 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7150 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7151 int off = 0, j;
7152
7153 netif_addr_lock_bh(dev);
7154 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7155 if (ether_addr_equal(eth->h_dest,
7156 vnic->uc_list + off)) {
7157 l2_idx = j + 1;
7158 break;
7159 }
7160 }
7161 netif_addr_unlock_bh(dev);
7162 if (!l2_idx)
7163 return -EINVAL;
7164 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007165 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7166 if (!new_fltr)
7167 return -ENOMEM;
7168
7169 fkeys = &new_fltr->fkeys;
7170 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7171 rc = -EPROTONOSUPPORT;
7172 goto err_free;
7173 }
7174
Michael Chandda0e742016-12-29 12:13:40 -05007175 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7176 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007177 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7178 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7179 rc = -EPROTONOSUPPORT;
7180 goto err_free;
7181 }
Michael Chandda0e742016-12-29 12:13:40 -05007182 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7183 bp->hwrm_spec_code < 0x10601) {
7184 rc = -EPROTONOSUPPORT;
7185 goto err_free;
7186 }
Michael Chan61aad722017-02-12 19:18:14 -05007187 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7188 bp->hwrm_spec_code < 0x10601) {
7189 rc = -EPROTONOSUPPORT;
7190 goto err_free;
7191 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007192
Michael Chana54c4d72016-07-25 12:33:35 -04007193 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007194 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7195
7196 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7197 head = &bp->ntp_fltr_hash_tbl[idx];
7198 rcu_read_lock();
7199 hlist_for_each_entry_rcu(fltr, head, hash) {
7200 if (bnxt_fltr_match(fltr, new_fltr)) {
7201 rcu_read_unlock();
7202 rc = 0;
7203 goto err_free;
7204 }
7205 }
7206 rcu_read_unlock();
7207
7208 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007209 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7210 BNXT_NTP_FLTR_MAX_FLTR, 0);
7211 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007212 spin_unlock_bh(&bp->ntp_fltr_lock);
7213 rc = -ENOMEM;
7214 goto err_free;
7215 }
7216
Michael Chan84e86b92015-11-05 16:25:50 -05007217 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007218 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007219 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007220 new_fltr->rxq = rxq_index;
7221 hlist_add_head_rcu(&new_fltr->hash, head);
7222 bp->ntp_fltr_count++;
7223 spin_unlock_bh(&bp->ntp_fltr_lock);
7224
7225 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7226 schedule_work(&bp->sp_task);
7227
7228 return new_fltr->sw_id;
7229
7230err_free:
7231 kfree(new_fltr);
7232 return rc;
7233}
7234
7235static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7236{
7237 int i;
7238
7239 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7240 struct hlist_head *head;
7241 struct hlist_node *tmp;
7242 struct bnxt_ntuple_filter *fltr;
7243 int rc;
7244
7245 head = &bp->ntp_fltr_hash_tbl[i];
7246 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7247 bool del = false;
7248
7249 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7250 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7251 fltr->flow_id,
7252 fltr->sw_id)) {
7253 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7254 fltr);
7255 del = true;
7256 }
7257 } else {
7258 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7259 fltr);
7260 if (rc)
7261 del = true;
7262 else
7263 set_bit(BNXT_FLTR_VALID, &fltr->state);
7264 }
7265
7266 if (del) {
7267 spin_lock_bh(&bp->ntp_fltr_lock);
7268 hlist_del_rcu(&fltr->hash);
7269 bp->ntp_fltr_count--;
7270 spin_unlock_bh(&bp->ntp_fltr_lock);
7271 synchronize_rcu();
7272 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7273 kfree(fltr);
7274 }
7275 }
7276 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007277 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7278 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007279}
7280
7281#else
7282
7283static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7284{
7285}
7286
7287#endif /* CONFIG_RFS_ACCEL */
7288
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007289static void bnxt_udp_tunnel_add(struct net_device *dev,
7290 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007291{
7292 struct bnxt *bp = netdev_priv(dev);
7293
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007294 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7295 return;
7296
Michael Chanc0c050c2015-10-22 16:01:17 -04007297 if (!netif_running(dev))
7298 return;
7299
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007300 switch (ti->type) {
7301 case UDP_TUNNEL_TYPE_VXLAN:
7302 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7303 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007304
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007305 bp->vxlan_port_cnt++;
7306 if (bp->vxlan_port_cnt == 1) {
7307 bp->vxlan_port = ti->port;
7308 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007309 schedule_work(&bp->sp_task);
7310 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007311 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007312 case UDP_TUNNEL_TYPE_GENEVE:
7313 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7314 return;
7315
7316 bp->nge_port_cnt++;
7317 if (bp->nge_port_cnt == 1) {
7318 bp->nge_port = ti->port;
7319 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7320 }
7321 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007322 default:
7323 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007324 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007325
7326 schedule_work(&bp->sp_task);
7327}
7328
7329static void bnxt_udp_tunnel_del(struct net_device *dev,
7330 struct udp_tunnel_info *ti)
7331{
7332 struct bnxt *bp = netdev_priv(dev);
7333
7334 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7335 return;
7336
7337 if (!netif_running(dev))
7338 return;
7339
7340 switch (ti->type) {
7341 case UDP_TUNNEL_TYPE_VXLAN:
7342 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7343 return;
7344 bp->vxlan_port_cnt--;
7345
7346 if (bp->vxlan_port_cnt != 0)
7347 return;
7348
7349 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7350 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007351 case UDP_TUNNEL_TYPE_GENEVE:
7352 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7353 return;
7354 bp->nge_port_cnt--;
7355
7356 if (bp->nge_port_cnt != 0)
7357 return;
7358
7359 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7360 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007361 default:
7362 return;
7363 }
7364
7365 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007366}
7367
7368static const struct net_device_ops bnxt_netdev_ops = {
7369 .ndo_open = bnxt_open,
7370 .ndo_start_xmit = bnxt_start_xmit,
7371 .ndo_stop = bnxt_close,
7372 .ndo_get_stats64 = bnxt_get_stats64,
7373 .ndo_set_rx_mode = bnxt_set_rx_mode,
7374 .ndo_do_ioctl = bnxt_ioctl,
7375 .ndo_validate_addr = eth_validate_addr,
7376 .ndo_set_mac_address = bnxt_change_mac_addr,
7377 .ndo_change_mtu = bnxt_change_mtu,
7378 .ndo_fix_features = bnxt_fix_features,
7379 .ndo_set_features = bnxt_set_features,
7380 .ndo_tx_timeout = bnxt_tx_timeout,
7381#ifdef CONFIG_BNXT_SRIOV
7382 .ndo_get_vf_config = bnxt_get_vf_config,
7383 .ndo_set_vf_mac = bnxt_set_vf_mac,
7384 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7385 .ndo_set_vf_rate = bnxt_set_vf_bw,
7386 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7387 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7388#endif
7389#ifdef CONFIG_NET_POLL_CONTROLLER
7390 .ndo_poll_controller = bnxt_poll_controller,
7391#endif
7392 .ndo_setup_tc = bnxt_setup_tc,
7393#ifdef CONFIG_RFS_ACCEL
7394 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7395#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007396 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7397 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007398 .ndo_xdp = bnxt_xdp,
Michael Chanc0c050c2015-10-22 16:01:17 -04007399};
7400
7401static void bnxt_remove_one(struct pci_dev *pdev)
7402{
7403 struct net_device *dev = pci_get_drvdata(pdev);
7404 struct bnxt *bp = netdev_priv(dev);
7405
7406 if (BNXT_PF(bp))
7407 bnxt_sriov_disable(bp);
7408
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007409 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007410 unregister_netdev(dev);
7411 cancel_work_sync(&bp->sp_task);
7412 bp->sp_event = 0;
7413
Michael Chan78095922016-12-07 00:26:16 -05007414 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007415 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007416 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007417 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007418 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007419 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007420 kfree(bp->edev);
7421 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007422 if (bp->xdp_prog)
7423 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007424 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007425 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007426}
7427
7428static int bnxt_probe_phy(struct bnxt *bp)
7429{
7430 int rc = 0;
7431 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007432
Michael Chan170ce012016-04-05 14:08:57 -04007433 rc = bnxt_hwrm_phy_qcaps(bp);
7434 if (rc) {
7435 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7436 rc);
7437 return rc;
7438 }
7439
Michael Chanc0c050c2015-10-22 16:01:17 -04007440 rc = bnxt_update_link(bp, false);
7441 if (rc) {
7442 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7443 rc);
7444 return rc;
7445 }
7446
Michael Chan93ed8112016-06-13 02:25:37 -04007447 /* Older firmware does not have supported_auto_speeds, so assume
7448 * that all supported speeds can be autonegotiated.
7449 */
7450 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7451 link_info->support_auto_speeds = link_info->support_speeds;
7452
Michael Chanc0c050c2015-10-22 16:01:17 -04007453 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007454 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007455 link_info->autoneg = BNXT_AUTONEG_SPEED;
7456 if (bp->hwrm_spec_code >= 0x10201) {
7457 if (link_info->auto_pause_setting &
7458 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7459 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7460 } else {
7461 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7462 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007463 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007464 } else {
7465 link_info->req_link_speed = link_info->force_link_speed;
7466 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007467 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007468 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7469 link_info->req_flow_ctrl =
7470 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7471 else
7472 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007473 return rc;
7474}
7475
7476static int bnxt_get_max_irq(struct pci_dev *pdev)
7477{
7478 u16 ctrl;
7479
7480 if (!pdev->msix_cap)
7481 return 1;
7482
7483 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7484 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7485}
7486
Michael Chan6e6c5a52016-01-02 23:45:02 -05007487static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7488 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007489{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007490 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007491
Michael Chan379a80a2015-10-23 15:06:19 -04007492#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007493 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007494 *max_tx = bp->vf.max_tx_rings;
7495 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007496 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7497 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007498 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007499 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007500#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007501 {
7502 *max_tx = bp->pf.max_tx_rings;
7503 *max_rx = bp->pf.max_rx_rings;
7504 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7505 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7506 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007507 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007508 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7509 *max_cp -= 1;
7510 *max_rx -= 2;
7511 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007512 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7513 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007514 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007515}
7516
7517int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7518{
7519 int rx, tx, cp;
7520
7521 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7522 if (!rx || !tx || !cp)
7523 return -ENOMEM;
7524
7525 *max_rx = rx;
7526 *max_tx = tx;
7527 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7528}
7529
Michael Chane4060d32016-12-07 00:26:19 -05007530static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7531 bool shared)
7532{
7533 int rc;
7534
7535 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007536 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7537 /* Not enough rings, try disabling agg rings. */
7538 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7539 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7540 if (rc)
7541 return rc;
7542 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7543 bp->dev->hw_features &= ~NETIF_F_LRO;
7544 bp->dev->features &= ~NETIF_F_LRO;
7545 bnxt_set_ring_params(bp);
7546 }
Michael Chane4060d32016-12-07 00:26:19 -05007547
7548 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7549 int max_cp, max_stat, max_irq;
7550
7551 /* Reserve minimum resources for RoCE */
7552 max_cp = bnxt_get_max_func_cp_rings(bp);
7553 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7554 max_irq = bnxt_get_max_func_irqs(bp);
7555 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7556 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7557 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7558 return 0;
7559
7560 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7561 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7562 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7563 max_cp = min_t(int, max_cp, max_irq);
7564 max_cp = min_t(int, max_cp, max_stat);
7565 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7566 if (rc)
7567 rc = 0;
7568 }
7569 return rc;
7570}
7571
Michael Chan6e6c5a52016-01-02 23:45:02 -05007572static int bnxt_set_dflt_rings(struct bnxt *bp)
7573{
7574 int dflt_rings, max_rx_rings, max_tx_rings, rc;
7575 bool sh = true;
7576
7577 if (sh)
7578 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7579 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007580 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007581 if (rc)
7582 return rc;
7583 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7584 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007585
7586 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7587 if (rc)
7588 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7589
Michael Chan6e6c5a52016-01-02 23:45:02 -05007590 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7591 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7592 bp->tx_nr_rings + bp->rx_nr_rings;
7593 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007594 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7595 bp->rx_nr_rings++;
7596 bp->cp_nr_rings++;
7597 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007598 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007599}
7600
Michael Chan7b08f662016-12-07 00:26:18 -05007601void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7602{
7603 ASSERT_RTNL();
7604 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007605 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007606}
7607
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007608static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7609{
7610 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7611 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7612
7613 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7614 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7615 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7616 else
7617 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7618 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7619 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7620 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7621 "Unknown", width);
7622}
7623
Michael Chanc0c050c2015-10-22 16:01:17 -04007624static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7625{
7626 static int version_printed;
7627 struct net_device *dev;
7628 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007629 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007630
Ray Jui4e003382017-02-20 19:25:16 -05007631 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007632 return -ENODEV;
7633
Michael Chanc0c050c2015-10-22 16:01:17 -04007634 if (version_printed++ == 0)
7635 pr_info("%s", version);
7636
7637 max_irqs = bnxt_get_max_irq(pdev);
7638 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7639 if (!dev)
7640 return -ENOMEM;
7641
7642 bp = netdev_priv(dev);
7643
7644 if (bnxt_vf_pciid(ent->driver_data))
7645 bp->flags |= BNXT_FLAG_VF;
7646
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007647 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007648 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007649
7650 rc = bnxt_init_board(pdev, dev);
7651 if (rc < 0)
7652 goto init_err_free;
7653
7654 dev->netdev_ops = &bnxt_netdev_ops;
7655 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7656 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007657 pci_set_drvdata(pdev, dev);
7658
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007659 rc = bnxt_alloc_hwrm_resources(bp);
7660 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007661 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007662
7663 mutex_init(&bp->hwrm_cmd_lock);
7664 rc = bnxt_hwrm_ver_get(bp);
7665 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007666 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007667
Deepak Khungare605db82017-05-29 19:06:04 -04007668 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7669 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7670 if (rc)
7671 goto init_err_pci_clean;
7672 }
7673
Michael Chan3c2217a2017-03-08 18:44:32 -05007674 rc = bnxt_hwrm_func_reset(bp);
7675 if (rc)
7676 goto init_err_pci_clean;
7677
Rob Swindell5ac67d82016-09-19 03:58:03 -04007678 bnxt_hwrm_fw_set_time(bp);
7679
Michael Chanc0c050c2015-10-22 16:01:17 -04007680 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7681 NETIF_F_TSO | NETIF_F_TSO6 |
7682 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007683 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007684 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7685 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007686 NETIF_F_RXCSUM | NETIF_F_GRO;
7687
7688 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7689 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007690
Michael Chanc0c050c2015-10-22 16:01:17 -04007691 dev->hw_enc_features =
7692 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7693 NETIF_F_TSO | NETIF_F_TSO6 |
7694 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007695 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007696 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007697 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7698 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007699 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7700 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7701 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7702 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7703 dev->priv_flags |= IFF_UNICAST_FLT;
7704
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007705 /* MTU range: 60 - 9500 */
7706 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007707 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007708
Michael Chanc0c050c2015-10-22 16:01:17 -04007709#ifdef CONFIG_BNXT_SRIOV
7710 init_waitqueue_head(&bp->sriov_cfg_wait);
7711#endif
Michael Chan309369c2016-06-13 02:25:34 -04007712 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007713 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7714 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007715
Michael Chanc0c050c2015-10-22 16:01:17 -04007716 rc = bnxt_hwrm_func_drv_rgtr(bp);
7717 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007718 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007719
Michael Chana1653b12016-12-07 00:26:20 -05007720 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7721 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007722 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007723
Michael Chana588e452016-12-07 00:26:21 -05007724 bp->ulp_probe = bnxt_ulp_probe;
7725
Michael Chanc0c050c2015-10-22 16:01:17 -04007726 /* Get the MAX capabilities for this function */
7727 rc = bnxt_hwrm_func_qcaps(bp);
7728 if (rc) {
7729 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7730 rc);
7731 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007732 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007733 }
7734
7735 rc = bnxt_hwrm_queue_qportcfg(bp);
7736 if (rc) {
7737 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7738 rc);
7739 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007740 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007741 }
7742
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007743 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007744 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007745 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007746 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007747
Michael Chanc61fb992017-02-06 16:55:36 -05007748 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007749 bnxt_set_tpa_flags(bp);
7750 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007751 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007752 rc = bnxt_set_dflt_rings(bp);
7753 if (rc) {
7754 netdev_err(bp->dev, "Not enough rings available.\n");
7755 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007756 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007757 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007758
Michael Chan87da7f72016-11-16 21:13:09 -05007759 /* Default RSS hash cfg. */
7760 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7761 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7762 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7763 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7764 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7765 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7766 bp->hwrm_spec_code >= 0x10501) {
7767 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7768 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7769 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7770 }
7771
Michael Chan8fdefd62016-12-29 12:13:36 -05007772 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007773 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007774 dev->hw_features |= NETIF_F_NTUPLE;
7775 if (bnxt_rfs_capable(bp)) {
7776 bp->flags |= BNXT_FLAG_RFS;
7777 dev->features |= NETIF_F_NTUPLE;
7778 }
7779 }
7780
Michael Chanc0c050c2015-10-22 16:01:17 -04007781 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7782 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7783
7784 rc = bnxt_probe_phy(bp);
7785 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007786 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007787
Michael Chan78095922016-12-07 00:26:16 -05007788 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007789 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007790 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007791
Michael Chanc1ef1462017-04-04 18:14:07 -04007792 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007793 if (bp->flags & BNXT_FLAG_WOL_CAP)
7794 device_set_wakeup_enable(&pdev->dev, bp->wol);
7795 else
7796 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007797
Michael Chan78095922016-12-07 00:26:16 -05007798 rc = register_netdev(dev);
7799 if (rc)
7800 goto init_err_clr_int;
7801
Michael Chanc0c050c2015-10-22 16:01:17 -04007802 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7803 board_info[ent->driver_data].name,
7804 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7805
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007806 bnxt_parse_log_pcie_link(bp);
7807
Michael Chanc0c050c2015-10-22 16:01:17 -04007808 return 0;
7809
Michael Chan78095922016-12-07 00:26:16 -05007810init_err_clr_int:
7811 bnxt_clear_int_mode(bp);
7812
Sathya Perla17086392017-02-20 19:25:18 -05007813init_err_pci_clean:
7814 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007815
7816init_err_free:
7817 free_netdev(dev);
7818 return rc;
7819}
7820
Michael Chand196ece2017-04-04 18:14:08 -04007821static void bnxt_shutdown(struct pci_dev *pdev)
7822{
7823 struct net_device *dev = pci_get_drvdata(pdev);
7824 struct bnxt *bp;
7825
7826 if (!dev)
7827 return;
7828
7829 rtnl_lock();
7830 bp = netdev_priv(dev);
7831 if (!bp)
7832 goto shutdown_exit;
7833
7834 if (netif_running(dev))
7835 dev_close(dev);
7836
7837 if (system_state == SYSTEM_POWER_OFF) {
7838 bnxt_clear_int_mode(bp);
7839 pci_wake_from_d3(pdev, bp->wol);
7840 pci_set_power_state(pdev, PCI_D3hot);
7841 }
7842
7843shutdown_exit:
7844 rtnl_unlock();
7845}
7846
Michael Chanf65a2042017-04-04 18:14:11 -04007847#ifdef CONFIG_PM_SLEEP
7848static int bnxt_suspend(struct device *device)
7849{
7850 struct pci_dev *pdev = to_pci_dev(device);
7851 struct net_device *dev = pci_get_drvdata(pdev);
7852 struct bnxt *bp = netdev_priv(dev);
7853 int rc = 0;
7854
7855 rtnl_lock();
7856 if (netif_running(dev)) {
7857 netif_device_detach(dev);
7858 rc = bnxt_close(dev);
7859 }
7860 bnxt_hwrm_func_drv_unrgtr(bp);
7861 rtnl_unlock();
7862 return rc;
7863}
7864
7865static int bnxt_resume(struct device *device)
7866{
7867 struct pci_dev *pdev = to_pci_dev(device);
7868 struct net_device *dev = pci_get_drvdata(pdev);
7869 struct bnxt *bp = netdev_priv(dev);
7870 int rc = 0;
7871
7872 rtnl_lock();
7873 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
7874 rc = -ENODEV;
7875 goto resume_exit;
7876 }
7877 rc = bnxt_hwrm_func_reset(bp);
7878 if (rc) {
7879 rc = -EBUSY;
7880 goto resume_exit;
7881 }
7882 bnxt_get_wol_settings(bp);
7883 if (netif_running(dev)) {
7884 rc = bnxt_open(dev);
7885 if (!rc)
7886 netif_device_attach(dev);
7887 }
7888
7889resume_exit:
7890 rtnl_unlock();
7891 return rc;
7892}
7893
7894static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
7895#define BNXT_PM_OPS (&bnxt_pm_ops)
7896
7897#else
7898
7899#define BNXT_PM_OPS NULL
7900
7901#endif /* CONFIG_PM_SLEEP */
7902
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007903/**
7904 * bnxt_io_error_detected - called when PCI error is detected
7905 * @pdev: Pointer to PCI device
7906 * @state: The current pci connection state
7907 *
7908 * This function is called after a PCI bus error affecting
7909 * this device has been detected.
7910 */
7911static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7912 pci_channel_state_t state)
7913{
7914 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007915 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007916
7917 netdev_info(netdev, "PCI I/O error detected\n");
7918
7919 rtnl_lock();
7920 netif_device_detach(netdev);
7921
Michael Chana588e452016-12-07 00:26:21 -05007922 bnxt_ulp_stop(bp);
7923
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007924 if (state == pci_channel_io_perm_failure) {
7925 rtnl_unlock();
7926 return PCI_ERS_RESULT_DISCONNECT;
7927 }
7928
7929 if (netif_running(netdev))
7930 bnxt_close(netdev);
7931
7932 pci_disable_device(pdev);
7933 rtnl_unlock();
7934
7935 /* Request a slot slot reset. */
7936 return PCI_ERS_RESULT_NEED_RESET;
7937}
7938
7939/**
7940 * bnxt_io_slot_reset - called after the pci bus has been reset.
7941 * @pdev: Pointer to PCI device
7942 *
7943 * Restart the card from scratch, as if from a cold-boot.
7944 * At this point, the card has exprienced a hard reset,
7945 * followed by fixups by BIOS, and has its config space
7946 * set up identically to what it was at cold boot.
7947 */
7948static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7949{
7950 struct net_device *netdev = pci_get_drvdata(pdev);
7951 struct bnxt *bp = netdev_priv(netdev);
7952 int err = 0;
7953 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7954
7955 netdev_info(bp->dev, "PCI Slot Reset\n");
7956
7957 rtnl_lock();
7958
7959 if (pci_enable_device(pdev)) {
7960 dev_err(&pdev->dev,
7961 "Cannot re-enable PCI device after reset.\n");
7962 } else {
7963 pci_set_master(pdev);
7964
Michael Chanaa8ed022016-12-07 00:26:17 -05007965 err = bnxt_hwrm_func_reset(bp);
7966 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007967 err = bnxt_open(netdev);
7968
Michael Chana588e452016-12-07 00:26:21 -05007969 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007970 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007971 bnxt_ulp_start(bp);
7972 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007973 }
7974
7975 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7976 dev_close(netdev);
7977
7978 rtnl_unlock();
7979
7980 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7981 if (err) {
7982 dev_err(&pdev->dev,
7983 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7984 err); /* non-fatal, continue */
7985 }
7986
7987 return PCI_ERS_RESULT_RECOVERED;
7988}
7989
7990/**
7991 * bnxt_io_resume - called when traffic can start flowing again.
7992 * @pdev: Pointer to PCI device
7993 *
7994 * This callback is called when the error recovery driver tells
7995 * us that its OK to resume normal operation.
7996 */
7997static void bnxt_io_resume(struct pci_dev *pdev)
7998{
7999 struct net_device *netdev = pci_get_drvdata(pdev);
8000
8001 rtnl_lock();
8002
8003 netif_device_attach(netdev);
8004
8005 rtnl_unlock();
8006}
8007
8008static const struct pci_error_handlers bnxt_err_handler = {
8009 .error_detected = bnxt_io_error_detected,
8010 .slot_reset = bnxt_io_slot_reset,
8011 .resume = bnxt_io_resume
8012};
8013
Michael Chanc0c050c2015-10-22 16:01:17 -04008014static struct pci_driver bnxt_pci_driver = {
8015 .name = DRV_MODULE_NAME,
8016 .id_table = bnxt_pci_tbl,
8017 .probe = bnxt_init_one,
8018 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008019 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008020 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008021 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008022#if defined(CONFIG_BNXT_SRIOV)
8023 .sriov_configure = bnxt_sriov_configure,
8024#endif
8025};
8026
8027module_pci_driver(bnxt_pci_driver);