OSDN Git Service

1714850df234d161b476390b3625e88398cbfe31
[uclinux-h8/linux.git] / drivers / net / ethernet / broadcom / bnxt / bnxt.c
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2018 Broadcom Limited
5  *
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>
36 #include <linux/if_bridge.h>
37 #include <linux/rtc.h>
38 #include <linux/bpf.h>
39 #include <net/ip.h>
40 #include <net/tcp.h>
41 #include <net/udp.h>
42 #include <net/checksum.h>
43 #include <net/ip6_checksum.h>
44 #include <net/udp_tunnel.h>
45 #include <linux/workqueue.h>
46 #include <linux/prefetch.h>
47 #include <linux/cache.h>
48 #include <linux/log2.h>
49 #include <linux/aer.h>
50 #include <linux/bitmap.h>
51 #include <linux/cpu_rmap.h>
52 #include <linux/cpumask.h>
53 #include <net/pkt_cls.h>
54
55 #include "bnxt_hsi.h"
56 #include "bnxt.h"
57 #include "bnxt_ulp.h"
58 #include "bnxt_sriov.h"
59 #include "bnxt_ethtool.h"
60 #include "bnxt_dcb.h"
61 #include "bnxt_xdp.h"
62 #include "bnxt_vfr.h"
63 #include "bnxt_tc.h"
64 #include "bnxt_devlink.h"
65 #include "bnxt_debugfs.h"
66
67 #define BNXT_TX_TIMEOUT         (5 * HZ)
68
69 static const char version[] =
70         "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
71
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
74 MODULE_VERSION(DRV_MODULE_VERSION);
75
76 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
77 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
78 #define BNXT_RX_COPY_THRESH 256
79
80 #define BNXT_TX_PUSH_THRESH 164
81
82 enum board_idx {
83         BCM57301,
84         BCM57302,
85         BCM57304,
86         BCM57417_NPAR,
87         BCM58700,
88         BCM57311,
89         BCM57312,
90         BCM57402,
91         BCM57404,
92         BCM57406,
93         BCM57402_NPAR,
94         BCM57407,
95         BCM57412,
96         BCM57414,
97         BCM57416,
98         BCM57417,
99         BCM57412_NPAR,
100         BCM57314,
101         BCM57417_SFP,
102         BCM57416_SFP,
103         BCM57404_NPAR,
104         BCM57406_NPAR,
105         BCM57407_SFP,
106         BCM57407_NPAR,
107         BCM57414_NPAR,
108         BCM57416_NPAR,
109         BCM57452,
110         BCM57454,
111         BCM5745x_NPAR,
112         BCM58802,
113         BCM58804,
114         BCM58808,
115         NETXTREME_E_VF,
116         NETXTREME_C_VF,
117         NETXTREME_S_VF,
118 };
119
120 /* indexed by enum above */
121 static const struct {
122         char *name;
123 } board_info[] = {
124         [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
125         [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
126         [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
127         [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
128         [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
129         [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
130         [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
131         [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
132         [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
133         [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
134         [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
135         [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
136         [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
137         [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
138         [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
139         [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
140         [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
141         [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
142         [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
143         [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
144         [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
145         [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
146         [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
147         [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
148         [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
149         [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
150         [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
151         [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
152         [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
153         [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
154         [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
155         [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
156         [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
157         [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
158         [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
159 };
160
161 static const struct pci_device_id bnxt_pci_tbl[] = {
162         { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
163         { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
164         { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
165         { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
166         { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
167         { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
168         { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
169         { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
170         { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
171         { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
172         { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
173         { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
174         { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
175         { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
176         { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
177         { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
178         { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
179         { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
180         { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
181         { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
182         { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
183         { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
184         { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
185         { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
186         { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
187         { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
188         { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
189         { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
190         { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
191         { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
192         { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
193         { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
194         { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
195         { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
196         { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
197         { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198         { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200         { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201         { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
202         { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
203         { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
204         { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
205         { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
206         { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
207         { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
208         { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
209 #endif
210         { 0 }
211 };
212
213 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
214
215 static const u16 bnxt_vf_req_snif[] = {
216         HWRM_FUNC_CFG,
217         HWRM_FUNC_VF_CFG,
218         HWRM_PORT_PHY_QCFG,
219         HWRM_CFA_L2_FILTER_ALLOC,
220 };
221
222 static const u16 bnxt_async_events_arr[] = {
223         ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
224         ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
225         ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
226         ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
227         ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
228 };
229
230 static struct workqueue_struct *bnxt_pf_wq;
231
232 static bool bnxt_vf_pciid(enum board_idx idx)
233 {
234         return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
235                 idx == NETXTREME_S_VF);
236 }
237
238 #define DB_CP_REARM_FLAGS       (DB_KEY_CP | DB_IDX_VALID)
239 #define DB_CP_FLAGS             (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
240 #define DB_CP_IRQ_DIS_FLAGS     (DB_KEY_CP | DB_IRQ_DIS)
241
242 #define BNXT_CP_DB_REARM(db, raw_cons)                                  \
243                 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
244
245 #define BNXT_CP_DB(db, raw_cons)                                        \
246                 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
247
248 #define BNXT_CP_DB_IRQ_DIS(db)                                          \
249                 writel(DB_CP_IRQ_DIS_FLAGS, db)
250
251 const u16 bnxt_lhint_arr[] = {
252         TX_BD_FLAGS_LHINT_512_AND_SMALLER,
253         TX_BD_FLAGS_LHINT_512_TO_1023,
254         TX_BD_FLAGS_LHINT_1024_TO_2047,
255         TX_BD_FLAGS_LHINT_1024_TO_2047,
256         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
257         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
258         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
259         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
260         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
261         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
262         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
263         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
264         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
265         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
266         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
267         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
268         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
269         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
270         TX_BD_FLAGS_LHINT_2048_AND_LARGER,
271 };
272
273 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
274 {
275         struct metadata_dst *md_dst = skb_metadata_dst(skb);
276
277         if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
278                 return 0;
279
280         return md_dst->u.port_info.port_id;
281 }
282
283 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
284 {
285         struct bnxt *bp = netdev_priv(dev);
286         struct tx_bd *txbd;
287         struct tx_bd_ext *txbd1;
288         struct netdev_queue *txq;
289         int i;
290         dma_addr_t mapping;
291         unsigned int length, pad = 0;
292         u32 len, free_size, vlan_tag_flags, cfa_action, flags;
293         u16 prod, last_frag;
294         struct pci_dev *pdev = bp->pdev;
295         struct bnxt_tx_ring_info *txr;
296         struct bnxt_sw_tx_bd *tx_buf;
297
298         i = skb_get_queue_mapping(skb);
299         if (unlikely(i >= bp->tx_nr_rings)) {
300                 dev_kfree_skb_any(skb);
301                 return NETDEV_TX_OK;
302         }
303
304         txq = netdev_get_tx_queue(dev, i);
305         txr = &bp->tx_ring[bp->tx_ring_map[i]];
306         prod = txr->tx_prod;
307
308         free_size = bnxt_tx_avail(bp, txr);
309         if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
310                 netif_tx_stop_queue(txq);
311                 return NETDEV_TX_BUSY;
312         }
313
314         length = skb->len;
315         len = skb_headlen(skb);
316         last_frag = skb_shinfo(skb)->nr_frags;
317
318         txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
319
320         txbd->tx_bd_opaque = prod;
321
322         tx_buf = &txr->tx_buf_ring[prod];
323         tx_buf->skb = skb;
324         tx_buf->nr_frags = last_frag;
325
326         vlan_tag_flags = 0;
327         cfa_action = bnxt_xmit_get_cfa_action(skb);
328         if (skb_vlan_tag_present(skb)) {
329                 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
330                                  skb_vlan_tag_get(skb);
331                 /* Currently supports 8021Q, 8021AD vlan offloads
332                  * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
333                  */
334                 if (skb->vlan_proto == htons(ETH_P_8021Q))
335                         vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
336         }
337
338         if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
339                 struct tx_push_buffer *tx_push_buf = txr->tx_push;
340                 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
341                 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
342                 void *pdata = tx_push_buf->data;
343                 u64 *end;
344                 int j, push_len;
345
346                 /* Set COAL_NOW to be ready quickly for the next push */
347                 tx_push->tx_bd_len_flags_type =
348                         cpu_to_le32((length << TX_BD_LEN_SHIFT) |
349                                         TX_BD_TYPE_LONG_TX_BD |
350                                         TX_BD_FLAGS_LHINT_512_AND_SMALLER |
351                                         TX_BD_FLAGS_COAL_NOW |
352                                         TX_BD_FLAGS_PACKET_END |
353                                         (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
354
355                 if (skb->ip_summed == CHECKSUM_PARTIAL)
356                         tx_push1->tx_bd_hsize_lflags =
357                                         cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
358                 else
359                         tx_push1->tx_bd_hsize_lflags = 0;
360
361                 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
362                 tx_push1->tx_bd_cfa_action =
363                         cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
364
365                 end = pdata + length;
366                 end = PTR_ALIGN(end, 8) - 1;
367                 *end = 0;
368
369                 skb_copy_from_linear_data(skb, pdata, len);
370                 pdata += len;
371                 for (j = 0; j < last_frag; j++) {
372                         skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
373                         void *fptr;
374
375                         fptr = skb_frag_address_safe(frag);
376                         if (!fptr)
377                                 goto normal_tx;
378
379                         memcpy(pdata, fptr, skb_frag_size(frag));
380                         pdata += skb_frag_size(frag);
381                 }
382
383                 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
384                 txbd->tx_bd_haddr = txr->data_mapping;
385                 prod = NEXT_TX(prod);
386                 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
387                 memcpy(txbd, tx_push1, sizeof(*txbd));
388                 prod = NEXT_TX(prod);
389                 tx_push->doorbell =
390                         cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
391                 txr->tx_prod = prod;
392
393                 tx_buf->is_push = 1;
394                 netdev_tx_sent_queue(txq, skb->len);
395                 wmb();  /* Sync is_push and byte queue before pushing data */
396
397                 push_len = (length + sizeof(*tx_push) + 7) / 8;
398                 if (push_len > 16) {
399                         __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
400                         __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
401                                          (push_len - 16) << 1);
402                 } else {
403                         __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
404                                          push_len);
405                 }
406
407                 goto tx_done;
408         }
409
410 normal_tx:
411         if (length < BNXT_MIN_PKT_SIZE) {
412                 pad = BNXT_MIN_PKT_SIZE - length;
413                 if (skb_pad(skb, pad)) {
414                         /* SKB already freed. */
415                         tx_buf->skb = NULL;
416                         return NETDEV_TX_OK;
417                 }
418                 length = BNXT_MIN_PKT_SIZE;
419         }
420
421         mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
422
423         if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
424                 dev_kfree_skb_any(skb);
425                 tx_buf->skb = NULL;
426                 return NETDEV_TX_OK;
427         }
428
429         dma_unmap_addr_set(tx_buf, mapping, mapping);
430         flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
431                 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
432
433         txbd->tx_bd_haddr = cpu_to_le64(mapping);
434
435         prod = NEXT_TX(prod);
436         txbd1 = (struct tx_bd_ext *)
437                 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
438
439         txbd1->tx_bd_hsize_lflags = 0;
440         if (skb_is_gso(skb)) {
441                 u32 hdr_len;
442
443                 if (skb->encapsulation)
444                         hdr_len = skb_inner_network_offset(skb) +
445                                 skb_inner_network_header_len(skb) +
446                                 inner_tcp_hdrlen(skb);
447                 else
448                         hdr_len = skb_transport_offset(skb) +
449                                 tcp_hdrlen(skb);
450
451                 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
452                                         TX_BD_FLAGS_T_IPID |
453                                         (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
454                 length = skb_shinfo(skb)->gso_size;
455                 txbd1->tx_bd_mss = cpu_to_le32(length);
456                 length += hdr_len;
457         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
458                 txbd1->tx_bd_hsize_lflags =
459                         cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
460                 txbd1->tx_bd_mss = 0;
461         }
462
463         length >>= 9;
464         flags |= bnxt_lhint_arr[length];
465         txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
466
467         txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
468         txbd1->tx_bd_cfa_action =
469                         cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
470         for (i = 0; i < last_frag; i++) {
471                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
472
473                 prod = NEXT_TX(prod);
474                 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
475
476                 len = skb_frag_size(frag);
477                 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
478                                            DMA_TO_DEVICE);
479
480                 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
481                         goto tx_dma_error;
482
483                 tx_buf = &txr->tx_buf_ring[prod];
484                 dma_unmap_addr_set(tx_buf, mapping, mapping);
485
486                 txbd->tx_bd_haddr = cpu_to_le64(mapping);
487
488                 flags = len << TX_BD_LEN_SHIFT;
489                 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
490         }
491
492         flags &= ~TX_BD_LEN;
493         txbd->tx_bd_len_flags_type =
494                 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
495                             TX_BD_FLAGS_PACKET_END);
496
497         netdev_tx_sent_queue(txq, skb->len);
498
499         /* Sync BD data before updating doorbell */
500         wmb();
501
502         prod = NEXT_TX(prod);
503         txr->tx_prod = prod;
504
505         if (!skb->xmit_more || netif_xmit_stopped(txq))
506                 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
507
508 tx_done:
509
510         mmiowb();
511
512         if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
513                 if (skb->xmit_more && !tx_buf->is_push)
514                         bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
515
516                 netif_tx_stop_queue(txq);
517
518                 /* netif_tx_stop_queue() must be done before checking
519                  * tx index in bnxt_tx_avail() below, because in
520                  * bnxt_tx_int(), we update tx index before checking for
521                  * netif_tx_queue_stopped().
522                  */
523                 smp_mb();
524                 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
525                         netif_tx_wake_queue(txq);
526         }
527         return NETDEV_TX_OK;
528
529 tx_dma_error:
530         last_frag = i;
531
532         /* start back at beginning and unmap skb */
533         prod = txr->tx_prod;
534         tx_buf = &txr->tx_buf_ring[prod];
535         tx_buf->skb = NULL;
536         dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
537                          skb_headlen(skb), PCI_DMA_TODEVICE);
538         prod = NEXT_TX(prod);
539
540         /* unmap remaining mapped pages */
541         for (i = 0; i < last_frag; i++) {
542                 prod = NEXT_TX(prod);
543                 tx_buf = &txr->tx_buf_ring[prod];
544                 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
545                                skb_frag_size(&skb_shinfo(skb)->frags[i]),
546                                PCI_DMA_TODEVICE);
547         }
548
549         dev_kfree_skb_any(skb);
550         return NETDEV_TX_OK;
551 }
552
553 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
554 {
555         struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
556         struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
557         u16 cons = txr->tx_cons;
558         struct pci_dev *pdev = bp->pdev;
559         int i;
560         unsigned int tx_bytes = 0;
561
562         for (i = 0; i < nr_pkts; i++) {
563                 struct bnxt_sw_tx_bd *tx_buf;
564                 struct sk_buff *skb;
565                 int j, last;
566
567                 tx_buf = &txr->tx_buf_ring[cons];
568                 cons = NEXT_TX(cons);
569                 skb = tx_buf->skb;
570                 tx_buf->skb = NULL;
571
572                 if (tx_buf->is_push) {
573                         tx_buf->is_push = 0;
574                         goto next_tx_int;
575                 }
576
577                 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
578                                  skb_headlen(skb), PCI_DMA_TODEVICE);
579                 last = tx_buf->nr_frags;
580
581                 for (j = 0; j < last; j++) {
582                         cons = NEXT_TX(cons);
583                         tx_buf = &txr->tx_buf_ring[cons];
584                         dma_unmap_page(
585                                 &pdev->dev,
586                                 dma_unmap_addr(tx_buf, mapping),
587                                 skb_frag_size(&skb_shinfo(skb)->frags[j]),
588                                 PCI_DMA_TODEVICE);
589                 }
590
591 next_tx_int:
592                 cons = NEXT_TX(cons);
593
594                 tx_bytes += skb->len;
595                 dev_kfree_skb_any(skb);
596         }
597
598         netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
599         txr->tx_cons = cons;
600
601         /* Need to make the tx_cons update visible to bnxt_start_xmit()
602          * before checking for netif_tx_queue_stopped().  Without the
603          * memory barrier, there is a small possibility that bnxt_start_xmit()
604          * will miss it and cause the queue to be stopped forever.
605          */
606         smp_mb();
607
608         if (unlikely(netif_tx_queue_stopped(txq)) &&
609             (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
610                 __netif_tx_lock(txq, smp_processor_id());
611                 if (netif_tx_queue_stopped(txq) &&
612                     bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
613                     txr->dev_state != BNXT_DEV_STATE_CLOSING)
614                         netif_tx_wake_queue(txq);
615                 __netif_tx_unlock(txq);
616         }
617 }
618
619 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
620                                          gfp_t gfp)
621 {
622         struct device *dev = &bp->pdev->dev;
623         struct page *page;
624
625         page = alloc_page(gfp);
626         if (!page)
627                 return NULL;
628
629         *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
630                                       DMA_ATTR_WEAK_ORDERING);
631         if (dma_mapping_error(dev, *mapping)) {
632                 __free_page(page);
633                 return NULL;
634         }
635         *mapping += bp->rx_dma_offset;
636         return page;
637 }
638
639 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
640                                        gfp_t gfp)
641 {
642         u8 *data;
643         struct pci_dev *pdev = bp->pdev;
644
645         data = kmalloc(bp->rx_buf_size, gfp);
646         if (!data)
647                 return NULL;
648
649         *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
650                                         bp->rx_buf_use_size, bp->rx_dir,
651                                         DMA_ATTR_WEAK_ORDERING);
652
653         if (dma_mapping_error(&pdev->dev, *mapping)) {
654                 kfree(data);
655                 data = NULL;
656         }
657         return data;
658 }
659
660 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
661                        u16 prod, gfp_t gfp)
662 {
663         struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
664         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
665         dma_addr_t mapping;
666
667         if (BNXT_RX_PAGE_MODE(bp)) {
668                 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
669
670                 if (!page)
671                         return -ENOMEM;
672
673                 rx_buf->data = page;
674                 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
675         } else {
676                 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
677
678                 if (!data)
679                         return -ENOMEM;
680
681                 rx_buf->data = data;
682                 rx_buf->data_ptr = data + bp->rx_offset;
683         }
684         rx_buf->mapping = mapping;
685
686         rxbd->rx_bd_haddr = cpu_to_le64(mapping);
687         return 0;
688 }
689
690 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
691 {
692         u16 prod = rxr->rx_prod;
693         struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
694         struct rx_bd *cons_bd, *prod_bd;
695
696         prod_rx_buf = &rxr->rx_buf_ring[prod];
697         cons_rx_buf = &rxr->rx_buf_ring[cons];
698
699         prod_rx_buf->data = data;
700         prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
701
702         prod_rx_buf->mapping = cons_rx_buf->mapping;
703
704         prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
705         cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
706
707         prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
708 }
709
710 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
711 {
712         u16 next, max = rxr->rx_agg_bmap_size;
713
714         next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
715         if (next >= max)
716                 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
717         return next;
718 }
719
720 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
721                                      struct bnxt_rx_ring_info *rxr,
722                                      u16 prod, gfp_t gfp)
723 {
724         struct rx_bd *rxbd =
725                 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
726         struct bnxt_sw_rx_agg_bd *rx_agg_buf;
727         struct pci_dev *pdev = bp->pdev;
728         struct page *page;
729         dma_addr_t mapping;
730         u16 sw_prod = rxr->rx_sw_agg_prod;
731         unsigned int offset = 0;
732
733         if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
734                 page = rxr->rx_page;
735                 if (!page) {
736                         page = alloc_page(gfp);
737                         if (!page)
738                                 return -ENOMEM;
739                         rxr->rx_page = page;
740                         rxr->rx_page_offset = 0;
741                 }
742                 offset = rxr->rx_page_offset;
743                 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
744                 if (rxr->rx_page_offset == PAGE_SIZE)
745                         rxr->rx_page = NULL;
746                 else
747                         get_page(page);
748         } else {
749                 page = alloc_page(gfp);
750                 if (!page)
751                         return -ENOMEM;
752         }
753
754         mapping = dma_map_page_attrs(&pdev->dev, page, offset,
755                                      BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
756                                      DMA_ATTR_WEAK_ORDERING);
757         if (dma_mapping_error(&pdev->dev, mapping)) {
758                 __free_page(page);
759                 return -EIO;
760         }
761
762         if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
763                 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
764
765         __set_bit(sw_prod, rxr->rx_agg_bmap);
766         rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
767         rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
768
769         rx_agg_buf->page = page;
770         rx_agg_buf->offset = offset;
771         rx_agg_buf->mapping = mapping;
772         rxbd->rx_bd_haddr = cpu_to_le64(mapping);
773         rxbd->rx_bd_opaque = sw_prod;
774         return 0;
775 }
776
777 static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
778                                    u32 agg_bufs)
779 {
780         struct bnxt *bp = bnapi->bp;
781         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
782         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
783         u16 prod = rxr->rx_agg_prod;
784         u16 sw_prod = rxr->rx_sw_agg_prod;
785         u32 i;
786
787         for (i = 0; i < agg_bufs; i++) {
788                 u16 cons;
789                 struct rx_agg_cmp *agg;
790                 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
791                 struct rx_bd *prod_bd;
792                 struct page *page;
793
794                 agg = (struct rx_agg_cmp *)
795                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
796                 cons = agg->rx_agg_cmp_opaque;
797                 __clear_bit(cons, rxr->rx_agg_bmap);
798
799                 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
800                         sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
801
802                 __set_bit(sw_prod, rxr->rx_agg_bmap);
803                 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
804                 cons_rx_buf = &rxr->rx_agg_ring[cons];
805
806                 /* It is possible for sw_prod to be equal to cons, so
807                  * set cons_rx_buf->page to NULL first.
808                  */
809                 page = cons_rx_buf->page;
810                 cons_rx_buf->page = NULL;
811                 prod_rx_buf->page = page;
812                 prod_rx_buf->offset = cons_rx_buf->offset;
813
814                 prod_rx_buf->mapping = cons_rx_buf->mapping;
815
816                 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
817
818                 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
819                 prod_bd->rx_bd_opaque = sw_prod;
820
821                 prod = NEXT_RX_AGG(prod);
822                 sw_prod = NEXT_RX_AGG(sw_prod);
823                 cp_cons = NEXT_CMP(cp_cons);
824         }
825         rxr->rx_agg_prod = prod;
826         rxr->rx_sw_agg_prod = sw_prod;
827 }
828
829 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
830                                         struct bnxt_rx_ring_info *rxr,
831                                         u16 cons, void *data, u8 *data_ptr,
832                                         dma_addr_t dma_addr,
833                                         unsigned int offset_and_len)
834 {
835         unsigned int payload = offset_and_len >> 16;
836         unsigned int len = offset_and_len & 0xffff;
837         struct skb_frag_struct *frag;
838         struct page *page = data;
839         u16 prod = rxr->rx_prod;
840         struct sk_buff *skb;
841         int off, err;
842
843         err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
844         if (unlikely(err)) {
845                 bnxt_reuse_rx_data(rxr, cons, data);
846                 return NULL;
847         }
848         dma_addr -= bp->rx_dma_offset;
849         dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
850                              DMA_ATTR_WEAK_ORDERING);
851
852         if (unlikely(!payload))
853                 payload = eth_get_headlen(data_ptr, len);
854
855         skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
856         if (!skb) {
857                 __free_page(page);
858                 return NULL;
859         }
860
861         off = (void *)data_ptr - page_address(page);
862         skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
863         memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
864                payload + NET_IP_ALIGN);
865
866         frag = &skb_shinfo(skb)->frags[0];
867         skb_frag_size_sub(frag, payload);
868         frag->page_offset += payload;
869         skb->data_len -= payload;
870         skb->tail += payload;
871
872         return skb;
873 }
874
875 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
876                                    struct bnxt_rx_ring_info *rxr, u16 cons,
877                                    void *data, u8 *data_ptr,
878                                    dma_addr_t dma_addr,
879                                    unsigned int offset_and_len)
880 {
881         u16 prod = rxr->rx_prod;
882         struct sk_buff *skb;
883         int err;
884
885         err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
886         if (unlikely(err)) {
887                 bnxt_reuse_rx_data(rxr, cons, data);
888                 return NULL;
889         }
890
891         skb = build_skb(data, 0);
892         dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
893                                bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
894         if (!skb) {
895                 kfree(data);
896                 return NULL;
897         }
898
899         skb_reserve(skb, bp->rx_offset);
900         skb_put(skb, offset_and_len & 0xffff);
901         return skb;
902 }
903
904 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
905                                      struct sk_buff *skb, u16 cp_cons,
906                                      u32 agg_bufs)
907 {
908         struct pci_dev *pdev = bp->pdev;
909         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
910         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
911         u16 prod = rxr->rx_agg_prod;
912         u32 i;
913
914         for (i = 0; i < agg_bufs; i++) {
915                 u16 cons, frag_len;
916                 struct rx_agg_cmp *agg;
917                 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
918                 struct page *page;
919                 dma_addr_t mapping;
920
921                 agg = (struct rx_agg_cmp *)
922                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
923                 cons = agg->rx_agg_cmp_opaque;
924                 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
925                             RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
926
927                 cons_rx_buf = &rxr->rx_agg_ring[cons];
928                 skb_fill_page_desc(skb, i, cons_rx_buf->page,
929                                    cons_rx_buf->offset, frag_len);
930                 __clear_bit(cons, rxr->rx_agg_bmap);
931
932                 /* It is possible for bnxt_alloc_rx_page() to allocate
933                  * a sw_prod index that equals the cons index, so we
934                  * need to clear the cons entry now.
935                  */
936                 mapping = cons_rx_buf->mapping;
937                 page = cons_rx_buf->page;
938                 cons_rx_buf->page = NULL;
939
940                 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
941                         struct skb_shared_info *shinfo;
942                         unsigned int nr_frags;
943
944                         shinfo = skb_shinfo(skb);
945                         nr_frags = --shinfo->nr_frags;
946                         __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
947
948                         dev_kfree_skb(skb);
949
950                         cons_rx_buf->page = page;
951
952                         /* Update prod since possibly some pages have been
953                          * allocated already.
954                          */
955                         rxr->rx_agg_prod = prod;
956                         bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
957                         return NULL;
958                 }
959
960                 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
961                                      PCI_DMA_FROMDEVICE,
962                                      DMA_ATTR_WEAK_ORDERING);
963
964                 skb->data_len += frag_len;
965                 skb->len += frag_len;
966                 skb->truesize += PAGE_SIZE;
967
968                 prod = NEXT_RX_AGG(prod);
969                 cp_cons = NEXT_CMP(cp_cons);
970         }
971         rxr->rx_agg_prod = prod;
972         return skb;
973 }
974
975 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
976                                u8 agg_bufs, u32 *raw_cons)
977 {
978         u16 last;
979         struct rx_agg_cmp *agg;
980
981         *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
982         last = RING_CMP(*raw_cons);
983         agg = (struct rx_agg_cmp *)
984                 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
985         return RX_AGG_CMP_VALID(agg, *raw_cons);
986 }
987
988 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
989                                             unsigned int len,
990                                             dma_addr_t mapping)
991 {
992         struct bnxt *bp = bnapi->bp;
993         struct pci_dev *pdev = bp->pdev;
994         struct sk_buff *skb;
995
996         skb = napi_alloc_skb(&bnapi->napi, len);
997         if (!skb)
998                 return NULL;
999
1000         dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1001                                 bp->rx_dir);
1002
1003         memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1004                len + NET_IP_ALIGN);
1005
1006         dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1007                                    bp->rx_dir);
1008
1009         skb_put(skb, len);
1010         return skb;
1011 }
1012
1013 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1014                            u32 *raw_cons, void *cmp)
1015 {
1016         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1017         struct rx_cmp *rxcmp = cmp;
1018         u32 tmp_raw_cons = *raw_cons;
1019         u8 cmp_type, agg_bufs = 0;
1020
1021         cmp_type = RX_CMP_TYPE(rxcmp);
1022
1023         if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1024                 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1025                             RX_CMP_AGG_BUFS) >>
1026                            RX_CMP_AGG_BUFS_SHIFT;
1027         } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1028                 struct rx_tpa_end_cmp *tpa_end = cmp;
1029
1030                 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1031                             RX_TPA_END_CMP_AGG_BUFS) >>
1032                            RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1033         }
1034
1035         if (agg_bufs) {
1036                 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1037                         return -EBUSY;
1038         }
1039         *raw_cons = tmp_raw_cons;
1040         return 0;
1041 }
1042
1043 static void bnxt_queue_sp_work(struct bnxt *bp)
1044 {
1045         if (BNXT_PF(bp))
1046                 queue_work(bnxt_pf_wq, &bp->sp_task);
1047         else
1048                 schedule_work(&bp->sp_task);
1049 }
1050
1051 static void bnxt_cancel_sp_work(struct bnxt *bp)
1052 {
1053         if (BNXT_PF(bp))
1054                 flush_workqueue(bnxt_pf_wq);
1055         else
1056                 cancel_work_sync(&bp->sp_task);
1057 }
1058
1059 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1060 {
1061         if (!rxr->bnapi->in_reset) {
1062                 rxr->bnapi->in_reset = true;
1063                 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1064                 bnxt_queue_sp_work(bp);
1065         }
1066         rxr->rx_next_cons = 0xffff;
1067 }
1068
1069 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1070                            struct rx_tpa_start_cmp *tpa_start,
1071                            struct rx_tpa_start_cmp_ext *tpa_start1)
1072 {
1073         u8 agg_id = TPA_START_AGG_ID(tpa_start);
1074         u16 cons, prod;
1075         struct bnxt_tpa_info *tpa_info;
1076         struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1077         struct rx_bd *prod_bd;
1078         dma_addr_t mapping;
1079
1080         cons = tpa_start->rx_tpa_start_cmp_opaque;
1081         prod = rxr->rx_prod;
1082         cons_rx_buf = &rxr->rx_buf_ring[cons];
1083         prod_rx_buf = &rxr->rx_buf_ring[prod];
1084         tpa_info = &rxr->rx_tpa[agg_id];
1085
1086         if (unlikely(cons != rxr->rx_next_cons)) {
1087                 bnxt_sched_reset(bp, rxr);
1088                 return;
1089         }
1090         /* Store cfa_code in tpa_info to use in tpa_end
1091          * completion processing.
1092          */
1093         tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1094         prod_rx_buf->data = tpa_info->data;
1095         prod_rx_buf->data_ptr = tpa_info->data_ptr;
1096
1097         mapping = tpa_info->mapping;
1098         prod_rx_buf->mapping = mapping;
1099
1100         prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1101
1102         prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1103
1104         tpa_info->data = cons_rx_buf->data;
1105         tpa_info->data_ptr = cons_rx_buf->data_ptr;
1106         cons_rx_buf->data = NULL;
1107         tpa_info->mapping = cons_rx_buf->mapping;
1108
1109         tpa_info->len =
1110                 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1111                                 RX_TPA_START_CMP_LEN_SHIFT;
1112         if (likely(TPA_START_HASH_VALID(tpa_start))) {
1113                 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1114
1115                 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1116                 tpa_info->gso_type = SKB_GSO_TCPV4;
1117                 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1118                 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1119                         tpa_info->gso_type = SKB_GSO_TCPV6;
1120                 tpa_info->rss_hash =
1121                         le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1122         } else {
1123                 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1124                 tpa_info->gso_type = 0;
1125                 if (netif_msg_rx_err(bp))
1126                         netdev_warn(bp->dev, "TPA packet without valid hash\n");
1127         }
1128         tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1129         tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1130         tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1131
1132         rxr->rx_prod = NEXT_RX(prod);
1133         cons = NEXT_RX(cons);
1134         rxr->rx_next_cons = NEXT_RX(cons);
1135         cons_rx_buf = &rxr->rx_buf_ring[cons];
1136
1137         bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1138         rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1139         cons_rx_buf->data = NULL;
1140 }
1141
1142 static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1143                            u16 cp_cons, u32 agg_bufs)
1144 {
1145         if (agg_bufs)
1146                 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1147 }
1148
1149 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1150                                            int payload_off, int tcp_ts,
1151                                            struct sk_buff *skb)
1152 {
1153 #ifdef CONFIG_INET
1154         struct tcphdr *th;
1155         int len, nw_off;
1156         u16 outer_ip_off, inner_ip_off, inner_mac_off;
1157         u32 hdr_info = tpa_info->hdr_info;
1158         bool loopback = false;
1159
1160         inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1161         inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1162         outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1163
1164         /* If the packet is an internal loopback packet, the offsets will
1165          * have an extra 4 bytes.
1166          */
1167         if (inner_mac_off == 4) {
1168                 loopback = true;
1169         } else if (inner_mac_off > 4) {
1170                 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1171                                             ETH_HLEN - 2));
1172
1173                 /* We only support inner iPv4/ipv6.  If we don't see the
1174                  * correct protocol ID, it must be a loopback packet where
1175                  * the offsets are off by 4.
1176                  */
1177                 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1178                         loopback = true;
1179         }
1180         if (loopback) {
1181                 /* internal loopback packet, subtract all offsets by 4 */
1182                 inner_ip_off -= 4;
1183                 inner_mac_off -= 4;
1184                 outer_ip_off -= 4;
1185         }
1186
1187         nw_off = inner_ip_off - ETH_HLEN;
1188         skb_set_network_header(skb, nw_off);
1189         if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1190                 struct ipv6hdr *iph = ipv6_hdr(skb);
1191
1192                 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1193                 len = skb->len - skb_transport_offset(skb);
1194                 th = tcp_hdr(skb);
1195                 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1196         } else {
1197                 struct iphdr *iph = ip_hdr(skb);
1198
1199                 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1200                 len = skb->len - skb_transport_offset(skb);
1201                 th = tcp_hdr(skb);
1202                 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1203         }
1204
1205         if (inner_mac_off) { /* tunnel */
1206                 struct udphdr *uh = NULL;
1207                 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1208                                             ETH_HLEN - 2));
1209
1210                 if (proto == htons(ETH_P_IP)) {
1211                         struct iphdr *iph = (struct iphdr *)skb->data;
1212
1213                         if (iph->protocol == IPPROTO_UDP)
1214                                 uh = (struct udphdr *)(iph + 1);
1215                 } else {
1216                         struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1217
1218                         if (iph->nexthdr == IPPROTO_UDP)
1219                                 uh = (struct udphdr *)(iph + 1);
1220                 }
1221                 if (uh) {
1222                         if (uh->check)
1223                                 skb_shinfo(skb)->gso_type |=
1224                                         SKB_GSO_UDP_TUNNEL_CSUM;
1225                         else
1226                                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1227                 }
1228         }
1229 #endif
1230         return skb;
1231 }
1232
1233 #define BNXT_IPV4_HDR_SIZE      (sizeof(struct iphdr) + sizeof(struct tcphdr))
1234 #define BNXT_IPV6_HDR_SIZE      (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1235
1236 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1237                                            int payload_off, int tcp_ts,
1238                                            struct sk_buff *skb)
1239 {
1240 #ifdef CONFIG_INET
1241         struct tcphdr *th;
1242         int len, nw_off, tcp_opt_len = 0;
1243
1244         if (tcp_ts)
1245                 tcp_opt_len = 12;
1246
1247         if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1248                 struct iphdr *iph;
1249
1250                 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1251                          ETH_HLEN;
1252                 skb_set_network_header(skb, nw_off);
1253                 iph = ip_hdr(skb);
1254                 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1255                 len = skb->len - skb_transport_offset(skb);
1256                 th = tcp_hdr(skb);
1257                 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1258         } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1259                 struct ipv6hdr *iph;
1260
1261                 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1262                          ETH_HLEN;
1263                 skb_set_network_header(skb, nw_off);
1264                 iph = ipv6_hdr(skb);
1265                 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1266                 len = skb->len - skb_transport_offset(skb);
1267                 th = tcp_hdr(skb);
1268                 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1269         } else {
1270                 dev_kfree_skb_any(skb);
1271                 return NULL;
1272         }
1273
1274         if (nw_off) { /* tunnel */
1275                 struct udphdr *uh = NULL;
1276
1277                 if (skb->protocol == htons(ETH_P_IP)) {
1278                         struct iphdr *iph = (struct iphdr *)skb->data;
1279
1280                         if (iph->protocol == IPPROTO_UDP)
1281                                 uh = (struct udphdr *)(iph + 1);
1282                 } else {
1283                         struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1284
1285                         if (iph->nexthdr == IPPROTO_UDP)
1286                                 uh = (struct udphdr *)(iph + 1);
1287                 }
1288                 if (uh) {
1289                         if (uh->check)
1290                                 skb_shinfo(skb)->gso_type |=
1291                                         SKB_GSO_UDP_TUNNEL_CSUM;
1292                         else
1293                                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1294                 }
1295         }
1296 #endif
1297         return skb;
1298 }
1299
1300 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1301                                            struct bnxt_tpa_info *tpa_info,
1302                                            struct rx_tpa_end_cmp *tpa_end,
1303                                            struct rx_tpa_end_cmp_ext *tpa_end1,
1304                                            struct sk_buff *skb)
1305 {
1306 #ifdef CONFIG_INET
1307         int payload_off;
1308         u16 segs;
1309
1310         segs = TPA_END_TPA_SEGS(tpa_end);
1311         if (segs == 1)
1312                 return skb;
1313
1314         NAPI_GRO_CB(skb)->count = segs;
1315         skb_shinfo(skb)->gso_size =
1316                 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1317         skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1318         payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1319                        RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1320                       RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1321         skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1322         if (likely(skb))
1323                 tcp_gro_complete(skb);
1324 #endif
1325         return skb;
1326 }
1327
1328 /* Given the cfa_code of a received packet determine which
1329  * netdev (vf-rep or PF) the packet is destined to.
1330  */
1331 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1332 {
1333         struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1334
1335         /* if vf-rep dev is NULL, the must belongs to the PF */
1336         return dev ? dev : bp->dev;
1337 }
1338
1339 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1340                                            struct bnxt_napi *bnapi,
1341                                            u32 *raw_cons,
1342                                            struct rx_tpa_end_cmp *tpa_end,
1343                                            struct rx_tpa_end_cmp_ext *tpa_end1,
1344                                            u8 *event)
1345 {
1346         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1347         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1348         u8 agg_id = TPA_END_AGG_ID(tpa_end);
1349         u8 *data_ptr, agg_bufs;
1350         u16 cp_cons = RING_CMP(*raw_cons);
1351         unsigned int len;
1352         struct bnxt_tpa_info *tpa_info;
1353         dma_addr_t mapping;
1354         struct sk_buff *skb;
1355         void *data;
1356
1357         if (unlikely(bnapi->in_reset)) {
1358                 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1359
1360                 if (rc < 0)
1361                         return ERR_PTR(-EBUSY);
1362                 return NULL;
1363         }
1364
1365         tpa_info = &rxr->rx_tpa[agg_id];
1366         data = tpa_info->data;
1367         data_ptr = tpa_info->data_ptr;
1368         prefetch(data_ptr);
1369         len = tpa_info->len;
1370         mapping = tpa_info->mapping;
1371
1372         agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1373                     RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1374
1375         if (agg_bufs) {
1376                 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1377                         return ERR_PTR(-EBUSY);
1378
1379                 *event |= BNXT_AGG_EVENT;
1380                 cp_cons = NEXT_CMP(cp_cons);
1381         }
1382
1383         if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1384                 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1385                 if (agg_bufs > MAX_SKB_FRAGS)
1386                         netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1387                                     agg_bufs, (int)MAX_SKB_FRAGS);
1388                 return NULL;
1389         }
1390
1391         if (len <= bp->rx_copy_thresh) {
1392                 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1393                 if (!skb) {
1394                         bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1395                         return NULL;
1396                 }
1397         } else {
1398                 u8 *new_data;
1399                 dma_addr_t new_mapping;
1400
1401                 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1402                 if (!new_data) {
1403                         bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1404                         return NULL;
1405                 }
1406
1407                 tpa_info->data = new_data;
1408                 tpa_info->data_ptr = new_data + bp->rx_offset;
1409                 tpa_info->mapping = new_mapping;
1410
1411                 skb = build_skb(data, 0);
1412                 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1413                                        bp->rx_buf_use_size, bp->rx_dir,
1414                                        DMA_ATTR_WEAK_ORDERING);
1415
1416                 if (!skb) {
1417                         kfree(data);
1418                         bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1419                         return NULL;
1420                 }
1421                 skb_reserve(skb, bp->rx_offset);
1422                 skb_put(skb, len);
1423         }
1424
1425         if (agg_bufs) {
1426                 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1427                 if (!skb) {
1428                         /* Page reuse already handled by bnxt_rx_pages(). */
1429                         return NULL;
1430                 }
1431         }
1432
1433         skb->protocol =
1434                 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1435
1436         if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1437                 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1438
1439         if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1440             (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1441                 u16 vlan_proto = tpa_info->metadata >>
1442                         RX_CMP_FLAGS2_METADATA_TPID_SFT;
1443                 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1444
1445                 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1446         }
1447
1448         skb_checksum_none_assert(skb);
1449         if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1450                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1451                 skb->csum_level =
1452                         (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1453         }
1454
1455         if (TPA_END_GRO(tpa_end))
1456                 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1457
1458         return skb;
1459 }
1460
1461 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1462                              struct sk_buff *skb)
1463 {
1464         if (skb->dev != bp->dev) {
1465                 /* this packet belongs to a vf-rep */
1466                 bnxt_vf_rep_rx(bp, skb);
1467                 return;
1468         }
1469         skb_record_rx_queue(skb, bnapi->index);
1470         napi_gro_receive(&bnapi->napi, skb);
1471 }
1472
1473 /* returns the following:
1474  * 1       - 1 packet successfully received
1475  * 0       - successful TPA_START, packet not completed yet
1476  * -EBUSY  - completion ring does not have all the agg buffers yet
1477  * -ENOMEM - packet aborted due to out of memory
1478  * -EIO    - packet aborted due to hw error indicated in BD
1479  */
1480 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1481                        u8 *event)
1482 {
1483         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1484         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1485         struct net_device *dev = bp->dev;
1486         struct rx_cmp *rxcmp;
1487         struct rx_cmp_ext *rxcmp1;
1488         u32 tmp_raw_cons = *raw_cons;
1489         u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1490         struct bnxt_sw_rx_bd *rx_buf;
1491         unsigned int len;
1492         u8 *data_ptr, agg_bufs, cmp_type;
1493         dma_addr_t dma_addr;
1494         struct sk_buff *skb;
1495         void *data;
1496         int rc = 0;
1497         u32 misc;
1498
1499         rxcmp = (struct rx_cmp *)
1500                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1501
1502         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1503         cp_cons = RING_CMP(tmp_raw_cons);
1504         rxcmp1 = (struct rx_cmp_ext *)
1505                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1506
1507         if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1508                 return -EBUSY;
1509
1510         cmp_type = RX_CMP_TYPE(rxcmp);
1511
1512         prod = rxr->rx_prod;
1513
1514         if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1515                 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1516                                (struct rx_tpa_start_cmp_ext *)rxcmp1);
1517
1518                 *event |= BNXT_RX_EVENT;
1519                 goto next_rx_no_prod_no_len;
1520
1521         } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1522                 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1523                                    (struct rx_tpa_end_cmp *)rxcmp,
1524                                    (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1525
1526                 if (IS_ERR(skb))
1527                         return -EBUSY;
1528
1529                 rc = -ENOMEM;
1530                 if (likely(skb)) {
1531                         bnxt_deliver_skb(bp, bnapi, skb);
1532                         rc = 1;
1533                 }
1534                 *event |= BNXT_RX_EVENT;
1535                 goto next_rx_no_prod_no_len;
1536         }
1537
1538         cons = rxcmp->rx_cmp_opaque;
1539         rx_buf = &rxr->rx_buf_ring[cons];
1540         data = rx_buf->data;
1541         data_ptr = rx_buf->data_ptr;
1542         if (unlikely(cons != rxr->rx_next_cons)) {
1543                 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1544
1545                 bnxt_sched_reset(bp, rxr);
1546                 return rc1;
1547         }
1548         prefetch(data_ptr);
1549
1550         misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1551         agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1552
1553         if (agg_bufs) {
1554                 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1555                         return -EBUSY;
1556
1557                 cp_cons = NEXT_CMP(cp_cons);
1558                 *event |= BNXT_AGG_EVENT;
1559         }
1560         *event |= BNXT_RX_EVENT;
1561
1562         rx_buf->data = NULL;
1563         if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1564                 bnxt_reuse_rx_data(rxr, cons, data);
1565                 if (agg_bufs)
1566                         bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1567
1568                 rc = -EIO;
1569                 goto next_rx;
1570         }
1571
1572         len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1573         dma_addr = rx_buf->mapping;
1574
1575         if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1576                 rc = 1;
1577                 goto next_rx;
1578         }
1579
1580         if (len <= bp->rx_copy_thresh) {
1581                 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1582                 bnxt_reuse_rx_data(rxr, cons, data);
1583                 if (!skb) {
1584                         rc = -ENOMEM;
1585                         goto next_rx;
1586                 }
1587         } else {
1588                 u32 payload;
1589
1590                 if (rx_buf->data_ptr == data_ptr)
1591                         payload = misc & RX_CMP_PAYLOAD_OFFSET;
1592                 else
1593                         payload = 0;
1594                 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1595                                       payload | len);
1596                 if (!skb) {
1597                         rc = -ENOMEM;
1598                         goto next_rx;
1599                 }
1600         }
1601
1602         if (agg_bufs) {
1603                 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1604                 if (!skb) {
1605                         rc = -ENOMEM;
1606                         goto next_rx;
1607                 }
1608         }
1609
1610         if (RX_CMP_HASH_VALID(rxcmp)) {
1611                 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1612                 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1613
1614                 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1615                 if (hash_type != 1 && hash_type != 3)
1616                         type = PKT_HASH_TYPE_L3;
1617                 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1618         }
1619
1620         cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1621         skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1622
1623         if ((rxcmp1->rx_cmp_flags2 &
1624              cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1625             (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1626                 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1627                 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1628                 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1629
1630                 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1631         }
1632
1633         skb_checksum_none_assert(skb);
1634         if (RX_CMP_L4_CS_OK(rxcmp1)) {
1635                 if (dev->features & NETIF_F_RXCSUM) {
1636                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1637                         skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1638                 }
1639         } else {
1640                 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1641                         if (dev->features & NETIF_F_RXCSUM)
1642                                 cpr->rx_l4_csum_errors++;
1643                 }
1644         }
1645
1646         bnxt_deliver_skb(bp, bnapi, skb);
1647         rc = 1;
1648
1649 next_rx:
1650         rxr->rx_prod = NEXT_RX(prod);
1651         rxr->rx_next_cons = NEXT_RX(cons);
1652
1653         cpr->rx_packets += 1;
1654         cpr->rx_bytes += len;
1655
1656 next_rx_no_prod_no_len:
1657         *raw_cons = tmp_raw_cons;
1658
1659         return rc;
1660 }
1661
1662 /* In netpoll mode, if we are using a combined completion ring, we need to
1663  * discard the rx packets and recycle the buffers.
1664  */
1665 static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1666                                  u32 *raw_cons, u8 *event)
1667 {
1668         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1669         u32 tmp_raw_cons = *raw_cons;
1670         struct rx_cmp_ext *rxcmp1;
1671         struct rx_cmp *rxcmp;
1672         u16 cp_cons;
1673         u8 cmp_type;
1674
1675         cp_cons = RING_CMP(tmp_raw_cons);
1676         rxcmp = (struct rx_cmp *)
1677                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1678
1679         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1680         cp_cons = RING_CMP(tmp_raw_cons);
1681         rxcmp1 = (struct rx_cmp_ext *)
1682                         &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1683
1684         if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1685                 return -EBUSY;
1686
1687         cmp_type = RX_CMP_TYPE(rxcmp);
1688         if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1689                 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1690                         cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1691         } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1692                 struct rx_tpa_end_cmp_ext *tpa_end1;
1693
1694                 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1695                 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1696                         cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1697         }
1698         return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1699 }
1700
1701 #define BNXT_GET_EVENT_PORT(data)       \
1702         ((data) &                       \
1703          ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1704
1705 static int bnxt_async_event_process(struct bnxt *bp,
1706                                     struct hwrm_async_event_cmpl *cmpl)
1707 {
1708         u16 event_id = le16_to_cpu(cmpl->event_id);
1709
1710         /* TODO CHIMP_FW: Define event id's for link change, error etc */
1711         switch (event_id) {
1712         case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1713                 u32 data1 = le32_to_cpu(cmpl->event_data1);
1714                 struct bnxt_link_info *link_info = &bp->link_info;
1715
1716                 if (BNXT_VF(bp))
1717                         goto async_event_process_exit;
1718
1719                 /* print unsupported speed warning in forced speed mode only */
1720                 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1721                     (data1 & 0x20000)) {
1722                         u16 fw_speed = link_info->force_link_speed;
1723                         u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1724
1725                         if (speed != SPEED_UNKNOWN)
1726                                 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1727                                             speed);
1728                 }
1729                 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1730                 /* fall through */
1731         }
1732         case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1733                 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1734                 break;
1735         case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1736                 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1737                 break;
1738         case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1739                 u32 data1 = le32_to_cpu(cmpl->event_data1);
1740                 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1741
1742                 if (BNXT_VF(bp))
1743                         break;
1744
1745                 if (bp->pf.port_id != port_id)
1746                         break;
1747
1748                 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1749                 break;
1750         }
1751         case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1752                 if (BNXT_PF(bp))
1753                         goto async_event_process_exit;
1754                 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1755                 break;
1756         default:
1757                 goto async_event_process_exit;
1758         }
1759         bnxt_queue_sp_work(bp);
1760 async_event_process_exit:
1761         bnxt_ulp_async_events(bp, cmpl);
1762         return 0;
1763 }
1764
1765 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1766 {
1767         u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1768         struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1769         struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1770                                 (struct hwrm_fwd_req_cmpl *)txcmp;
1771
1772         switch (cmpl_type) {
1773         case CMPL_BASE_TYPE_HWRM_DONE:
1774                 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1775                 if (seq_id == bp->hwrm_intr_seq_id)
1776                         bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1777                 else
1778                         netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1779                 break;
1780
1781         case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1782                 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1783
1784                 if ((vf_id < bp->pf.first_vf_id) ||
1785                     (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1786                         netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1787                                    vf_id);
1788                         return -EINVAL;
1789                 }
1790
1791                 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1792                 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1793                 bnxt_queue_sp_work(bp);
1794                 break;
1795
1796         case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1797                 bnxt_async_event_process(bp,
1798                                          (struct hwrm_async_event_cmpl *)txcmp);
1799
1800         default:
1801                 break;
1802         }
1803
1804         return 0;
1805 }
1806
1807 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1808 {
1809         struct bnxt_napi *bnapi = dev_instance;
1810         struct bnxt *bp = bnapi->bp;
1811         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1812         u32 cons = RING_CMP(cpr->cp_raw_cons);
1813
1814         cpr->event_ctr++;
1815         prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1816         napi_schedule(&bnapi->napi);
1817         return IRQ_HANDLED;
1818 }
1819
1820 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1821 {
1822         u32 raw_cons = cpr->cp_raw_cons;
1823         u16 cons = RING_CMP(raw_cons);
1824         struct tx_cmp *txcmp;
1825
1826         txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1827
1828         return TX_CMP_VALID(txcmp, raw_cons);
1829 }
1830
1831 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1832 {
1833         struct bnxt_napi *bnapi = dev_instance;
1834         struct bnxt *bp = bnapi->bp;
1835         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1836         u32 cons = RING_CMP(cpr->cp_raw_cons);
1837         u32 int_status;
1838
1839         prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1840
1841         if (!bnxt_has_work(bp, cpr)) {
1842                 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1843                 /* return if erroneous interrupt */
1844                 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1845                         return IRQ_NONE;
1846         }
1847
1848         /* disable ring IRQ */
1849         BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1850
1851         /* Return here if interrupt is shared and is disabled. */
1852         if (unlikely(atomic_read(&bp->intr_sem) != 0))
1853                 return IRQ_HANDLED;
1854
1855         napi_schedule(&bnapi->napi);
1856         return IRQ_HANDLED;
1857 }
1858
1859 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1860 {
1861         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1862         u32 raw_cons = cpr->cp_raw_cons;
1863         u32 cons;
1864         int tx_pkts = 0;
1865         int rx_pkts = 0;
1866         u8 event = 0;
1867         struct tx_cmp *txcmp;
1868
1869         while (1) {
1870                 int rc;
1871
1872                 cons = RING_CMP(raw_cons);
1873                 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1874
1875                 if (!TX_CMP_VALID(txcmp, raw_cons))
1876                         break;
1877
1878                 /* The valid test of the entry must be done first before
1879                  * reading any further.
1880                  */
1881                 dma_rmb();
1882                 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1883                         tx_pkts++;
1884                         /* return full budget so NAPI will complete. */
1885                         if (unlikely(tx_pkts > bp->tx_wake_thresh))
1886                                 rx_pkts = budget;
1887                 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1888                         if (likely(budget))
1889                                 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1890                         else
1891                                 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1892                                                            &event);
1893                         if (likely(rc >= 0))
1894                                 rx_pkts += rc;
1895                         /* Increment rx_pkts when rc is -ENOMEM to count towards
1896                          * the NAPI budget.  Otherwise, we may potentially loop
1897                          * here forever if we consistently cannot allocate
1898                          * buffers.
1899                          */
1900                         else if (rc == -ENOMEM && budget)
1901                                 rx_pkts++;
1902                         else if (rc == -EBUSY)  /* partial completion */
1903                                 break;
1904                 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1905                                      CMPL_BASE_TYPE_HWRM_DONE) ||
1906                                     (TX_CMP_TYPE(txcmp) ==
1907                                      CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1908                                     (TX_CMP_TYPE(txcmp) ==
1909                                      CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1910                         bnxt_hwrm_handler(bp, txcmp);
1911                 }
1912                 raw_cons = NEXT_RAW_CMP(raw_cons);
1913
1914                 if (rx_pkts == budget)
1915                         break;
1916         }
1917
1918         if (event & BNXT_TX_EVENT) {
1919                 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1920                 void __iomem *db = txr->tx_doorbell;
1921                 u16 prod = txr->tx_prod;
1922
1923                 /* Sync BD data before updating doorbell */
1924                 wmb();
1925
1926                 bnxt_db_write_relaxed(bp, db, DB_KEY_TX | prod);
1927         }
1928
1929         cpr->cp_raw_cons = raw_cons;
1930         /* ACK completion ring before freeing tx ring and producing new
1931          * buffers in rx/agg rings to prevent overflowing the completion
1932          * ring.
1933          */
1934         BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1935
1936         if (tx_pkts)
1937                 bnapi->tx_int(bp, bnapi, tx_pkts);
1938
1939         if (event & BNXT_RX_EVENT) {
1940                 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1941
1942                 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1943                 if (event & BNXT_AGG_EVENT)
1944                         bnxt_db_write(bp, rxr->rx_agg_doorbell,
1945                                       DB_KEY_RX | rxr->rx_agg_prod);
1946         }
1947         return rx_pkts;
1948 }
1949
1950 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1951 {
1952         struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1953         struct bnxt *bp = bnapi->bp;
1954         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1955         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1956         struct tx_cmp *txcmp;
1957         struct rx_cmp_ext *rxcmp1;
1958         u32 cp_cons, tmp_raw_cons;
1959         u32 raw_cons = cpr->cp_raw_cons;
1960         u32 rx_pkts = 0;
1961         u8 event = 0;
1962
1963         while (1) {
1964                 int rc;
1965
1966                 cp_cons = RING_CMP(raw_cons);
1967                 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1968
1969                 if (!TX_CMP_VALID(txcmp, raw_cons))
1970                         break;
1971
1972                 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1973                         tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1974                         cp_cons = RING_CMP(tmp_raw_cons);
1975                         rxcmp1 = (struct rx_cmp_ext *)
1976                           &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1977
1978                         if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1979                                 break;
1980
1981                         /* force an error to recycle the buffer */
1982                         rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1983                                 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1984
1985                         rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1986                         if (likely(rc == -EIO) && budget)
1987                                 rx_pkts++;
1988                         else if (rc == -EBUSY)  /* partial completion */
1989                                 break;
1990                 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1991                                     CMPL_BASE_TYPE_HWRM_DONE)) {
1992                         bnxt_hwrm_handler(bp, txcmp);
1993                 } else {
1994                         netdev_err(bp->dev,
1995                                    "Invalid completion received on special ring\n");
1996                 }
1997                 raw_cons = NEXT_RAW_CMP(raw_cons);
1998
1999                 if (rx_pkts == budget)
2000                         break;
2001         }
2002
2003         cpr->cp_raw_cons = raw_cons;
2004         BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2005         bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
2006
2007         if (event & BNXT_AGG_EVENT)
2008                 bnxt_db_write(bp, rxr->rx_agg_doorbell,
2009                               DB_KEY_RX | rxr->rx_agg_prod);
2010
2011         if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2012                 napi_complete_done(napi, rx_pkts);
2013                 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2014         }
2015         return rx_pkts;
2016 }
2017
2018 static int bnxt_poll(struct napi_struct *napi, int budget)
2019 {
2020         struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2021         struct bnxt *bp = bnapi->bp;
2022         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2023         int work_done = 0;
2024
2025         while (1) {
2026                 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2027
2028                 if (work_done >= budget)
2029                         break;
2030
2031                 if (!bnxt_has_work(bp, cpr)) {
2032                         if (napi_complete_done(napi, work_done))
2033                                 BNXT_CP_DB_REARM(cpr->cp_doorbell,
2034                                                  cpr->cp_raw_cons);
2035                         break;
2036                 }
2037         }
2038         if (bp->flags & BNXT_FLAG_DIM) {
2039                 struct net_dim_sample dim_sample;
2040
2041                 net_dim_sample(cpr->event_ctr,
2042                                cpr->rx_packets,
2043                                cpr->rx_bytes,
2044                                &dim_sample);
2045                 net_dim(&cpr->dim, dim_sample);
2046         }
2047         mmiowb();
2048         return work_done;
2049 }
2050
2051 static void bnxt_free_tx_skbs(struct bnxt *bp)
2052 {
2053         int i, max_idx;
2054         struct pci_dev *pdev = bp->pdev;
2055
2056         if (!bp->tx_ring)
2057                 return;
2058
2059         max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2060         for (i = 0; i < bp->tx_nr_rings; i++) {
2061                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2062                 int j;
2063
2064                 for (j = 0; j < max_idx;) {
2065                         struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2066                         struct sk_buff *skb = tx_buf->skb;
2067                         int k, last;
2068
2069                         if (!skb) {
2070                                 j++;
2071                                 continue;
2072                         }
2073
2074                         tx_buf->skb = NULL;
2075
2076                         if (tx_buf->is_push) {
2077                                 dev_kfree_skb(skb);
2078                                 j += 2;
2079                                 continue;
2080                         }
2081
2082                         dma_unmap_single(&pdev->dev,
2083                                          dma_unmap_addr(tx_buf, mapping),
2084                                          skb_headlen(skb),
2085                                          PCI_DMA_TODEVICE);
2086
2087                         last = tx_buf->nr_frags;
2088                         j += 2;
2089                         for (k = 0; k < last; k++, j++) {
2090                                 int ring_idx = j & bp->tx_ring_mask;
2091                                 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2092
2093                                 tx_buf = &txr->tx_buf_ring[ring_idx];
2094                                 dma_unmap_page(
2095                                         &pdev->dev,
2096                                         dma_unmap_addr(tx_buf, mapping),
2097                                         skb_frag_size(frag), PCI_DMA_TODEVICE);
2098                         }
2099                         dev_kfree_skb(skb);
2100                 }
2101                 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2102         }
2103 }
2104
2105 static void bnxt_free_rx_skbs(struct bnxt *bp)
2106 {
2107         int i, max_idx, max_agg_idx;
2108         struct pci_dev *pdev = bp->pdev;
2109
2110         if (!bp->rx_ring)
2111                 return;
2112
2113         max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2114         max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2115         for (i = 0; i < bp->rx_nr_rings; i++) {
2116                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2117                 int j;
2118
2119                 if (rxr->rx_tpa) {
2120                         for (j = 0; j < MAX_TPA; j++) {
2121                                 struct bnxt_tpa_info *tpa_info =
2122                                                         &rxr->rx_tpa[j];
2123                                 u8 *data = tpa_info->data;
2124
2125                                 if (!data)
2126                                         continue;
2127
2128                                 dma_unmap_single_attrs(&pdev->dev,
2129                                                        tpa_info->mapping,
2130                                                        bp->rx_buf_use_size,
2131                                                        bp->rx_dir,
2132                                                        DMA_ATTR_WEAK_ORDERING);
2133
2134                                 tpa_info->data = NULL;
2135
2136                                 kfree(data);
2137                         }
2138                 }
2139
2140                 for (j = 0; j < max_idx; j++) {
2141                         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2142                         dma_addr_t mapping = rx_buf->mapping;
2143                         void *data = rx_buf->data;
2144
2145                         if (!data)
2146                                 continue;
2147
2148                         rx_buf->data = NULL;
2149
2150                         if (BNXT_RX_PAGE_MODE(bp)) {
2151                                 mapping -= bp->rx_dma_offset;
2152                                 dma_unmap_page_attrs(&pdev->dev, mapping,
2153                                                      PAGE_SIZE, bp->rx_dir,
2154                                                      DMA_ATTR_WEAK_ORDERING);
2155                                 __free_page(data);
2156                         } else {
2157                                 dma_unmap_single_attrs(&pdev->dev, mapping,
2158                                                        bp->rx_buf_use_size,
2159                                                        bp->rx_dir,
2160                                                        DMA_ATTR_WEAK_ORDERING);
2161                                 kfree(data);
2162                         }
2163                 }
2164
2165                 for (j = 0; j < max_agg_idx; j++) {
2166                         struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2167                                 &rxr->rx_agg_ring[j];
2168                         struct page *page = rx_agg_buf->page;
2169
2170                         if (!page)
2171                                 continue;
2172
2173                         dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2174                                              BNXT_RX_PAGE_SIZE,
2175                                              PCI_DMA_FROMDEVICE,
2176                                              DMA_ATTR_WEAK_ORDERING);
2177
2178                         rx_agg_buf->page = NULL;
2179                         __clear_bit(j, rxr->rx_agg_bmap);
2180
2181                         __free_page(page);
2182                 }
2183                 if (rxr->rx_page) {
2184                         __free_page(rxr->rx_page);
2185                         rxr->rx_page = NULL;
2186                 }
2187         }
2188 }
2189
2190 static void bnxt_free_skbs(struct bnxt *bp)
2191 {
2192         bnxt_free_tx_skbs(bp);
2193         bnxt_free_rx_skbs(bp);
2194 }
2195
2196 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2197 {
2198         struct pci_dev *pdev = bp->pdev;
2199         int i;
2200
2201         for (i = 0; i < ring->nr_pages; i++) {
2202                 if (!ring->pg_arr[i])
2203                         continue;
2204
2205                 dma_free_coherent(&pdev->dev, ring->page_size,
2206                                   ring->pg_arr[i], ring->dma_arr[i]);
2207
2208                 ring->pg_arr[i] = NULL;
2209         }
2210         if (ring->pg_tbl) {
2211                 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2212                                   ring->pg_tbl, ring->pg_tbl_map);
2213                 ring->pg_tbl = NULL;
2214         }
2215         if (ring->vmem_size && *ring->vmem) {
2216                 vfree(*ring->vmem);
2217                 *ring->vmem = NULL;
2218         }
2219 }
2220
2221 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2222 {
2223         int i;
2224         struct pci_dev *pdev = bp->pdev;
2225
2226         if (ring->nr_pages > 1) {
2227                 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2228                                                   ring->nr_pages * 8,
2229                                                   &ring->pg_tbl_map,
2230                                                   GFP_KERNEL);
2231                 if (!ring->pg_tbl)
2232                         return -ENOMEM;
2233         }
2234
2235         for (i = 0; i < ring->nr_pages; i++) {
2236                 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2237                                                      ring->page_size,
2238                                                      &ring->dma_arr[i],
2239                                                      GFP_KERNEL);
2240                 if (!ring->pg_arr[i])
2241                         return -ENOMEM;
2242
2243                 if (ring->nr_pages > 1)
2244                         ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2245         }
2246
2247         if (ring->vmem_size) {
2248                 *ring->vmem = vzalloc(ring->vmem_size);
2249                 if (!(*ring->vmem))
2250                         return -ENOMEM;
2251         }
2252         return 0;
2253 }
2254
2255 static void bnxt_free_rx_rings(struct bnxt *bp)
2256 {
2257         int i;
2258
2259         if (!bp->rx_ring)
2260                 return;
2261
2262         for (i = 0; i < bp->rx_nr_rings; i++) {
2263                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2264                 struct bnxt_ring_struct *ring;
2265
2266                 if (rxr->xdp_prog)
2267                         bpf_prog_put(rxr->xdp_prog);
2268
2269                 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2270                         xdp_rxq_info_unreg(&rxr->xdp_rxq);
2271
2272                 kfree(rxr->rx_tpa);
2273                 rxr->rx_tpa = NULL;
2274
2275                 kfree(rxr->rx_agg_bmap);
2276                 rxr->rx_agg_bmap = NULL;
2277
2278                 ring = &rxr->rx_ring_struct;
2279                 bnxt_free_ring(bp, ring);
2280
2281                 ring = &rxr->rx_agg_ring_struct;
2282                 bnxt_free_ring(bp, ring);
2283         }
2284 }
2285
2286 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2287 {
2288         int i, rc, agg_rings = 0, tpa_rings = 0;
2289
2290         if (!bp->rx_ring)
2291                 return -ENOMEM;
2292
2293         if (bp->flags & BNXT_FLAG_AGG_RINGS)
2294                 agg_rings = 1;
2295
2296         if (bp->flags & BNXT_FLAG_TPA)
2297                 tpa_rings = 1;
2298
2299         for (i = 0; i < bp->rx_nr_rings; i++) {
2300                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2301                 struct bnxt_ring_struct *ring;
2302
2303                 ring = &rxr->rx_ring_struct;
2304
2305                 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2306                 if (rc < 0)
2307                         return rc;
2308
2309                 rc = bnxt_alloc_ring(bp, ring);
2310                 if (rc)
2311                         return rc;
2312
2313                 if (agg_rings) {
2314                         u16 mem_size;
2315
2316                         ring = &rxr->rx_agg_ring_struct;
2317                         rc = bnxt_alloc_ring(bp, ring);
2318                         if (rc)
2319                                 return rc;
2320
2321                         ring->grp_idx = i;
2322                         rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2323                         mem_size = rxr->rx_agg_bmap_size / 8;
2324                         rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2325                         if (!rxr->rx_agg_bmap)
2326                                 return -ENOMEM;
2327
2328                         if (tpa_rings) {
2329                                 rxr->rx_tpa = kcalloc(MAX_TPA,
2330                                                 sizeof(struct bnxt_tpa_info),
2331                                                 GFP_KERNEL);
2332                                 if (!rxr->rx_tpa)
2333                                         return -ENOMEM;
2334                         }
2335                 }
2336         }
2337         return 0;
2338 }
2339
2340 static void bnxt_free_tx_rings(struct bnxt *bp)
2341 {
2342         int i;
2343         struct pci_dev *pdev = bp->pdev;
2344
2345         if (!bp->tx_ring)
2346                 return;
2347
2348         for (i = 0; i < bp->tx_nr_rings; i++) {
2349                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2350                 struct bnxt_ring_struct *ring;
2351
2352                 if (txr->tx_push) {
2353                         dma_free_coherent(&pdev->dev, bp->tx_push_size,
2354                                           txr->tx_push, txr->tx_push_mapping);
2355                         txr->tx_push = NULL;
2356                 }
2357
2358                 ring = &txr->tx_ring_struct;
2359
2360                 bnxt_free_ring(bp, ring);
2361         }
2362 }
2363
2364 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2365 {
2366         int i, j, rc;
2367         struct pci_dev *pdev = bp->pdev;
2368
2369         bp->tx_push_size = 0;
2370         if (bp->tx_push_thresh) {
2371                 int push_size;
2372
2373                 push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2374                                         bp->tx_push_thresh);
2375
2376                 if (push_size > 256) {
2377                         push_size = 0;
2378                         bp->tx_push_thresh = 0;
2379                 }
2380
2381                 bp->tx_push_size = push_size;
2382         }
2383
2384         for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2385                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2386                 struct bnxt_ring_struct *ring;
2387                 u8 qidx;
2388
2389                 ring = &txr->tx_ring_struct;
2390
2391                 rc = bnxt_alloc_ring(bp, ring);
2392                 if (rc)
2393                         return rc;
2394
2395                 ring->grp_idx = txr->bnapi->index;
2396                 if (bp->tx_push_size) {
2397                         dma_addr_t mapping;
2398
2399                         /* One pre-allocated DMA buffer to backup
2400                          * TX push operation
2401                          */
2402                         txr->tx_push = dma_alloc_coherent(&pdev->dev,
2403                                                 bp->tx_push_size,
2404                                                 &txr->tx_push_mapping,
2405                                                 GFP_KERNEL);
2406
2407                         if (!txr->tx_push)
2408                                 return -ENOMEM;
2409
2410                         mapping = txr->tx_push_mapping +
2411                                 sizeof(struct tx_push_bd);
2412                         txr->data_mapping = cpu_to_le64(mapping);
2413
2414                         memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2415                 }
2416                 qidx = bp->tc_to_qidx[j];
2417                 ring->queue_id = bp->q_info[qidx].queue_id;
2418                 if (i < bp->tx_nr_rings_xdp)
2419                         continue;
2420                 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2421                         j++;
2422         }
2423         return 0;
2424 }
2425
2426 static void bnxt_free_cp_rings(struct bnxt *bp)
2427 {
2428         int i;
2429
2430         if (!bp->bnapi)
2431                 return;
2432
2433         for (i = 0; i < bp->cp_nr_rings; i++) {
2434                 struct bnxt_napi *bnapi = bp->bnapi[i];
2435                 struct bnxt_cp_ring_info *cpr;
2436                 struct bnxt_ring_struct *ring;
2437
2438                 if (!bnapi)
2439                         continue;
2440
2441                 cpr = &bnapi->cp_ring;
2442                 ring = &cpr->cp_ring_struct;
2443
2444                 bnxt_free_ring(bp, ring);
2445         }
2446 }
2447
2448 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2449 {
2450         int i, rc, ulp_base_vec, ulp_msix;
2451
2452         ulp_msix = bnxt_get_ulp_msix_num(bp);
2453         ulp_base_vec = bnxt_get_ulp_msix_base(bp);
2454         for (i = 0; i < bp->cp_nr_rings; i++) {
2455                 struct bnxt_napi *bnapi = bp->bnapi[i];
2456                 struct bnxt_cp_ring_info *cpr;
2457                 struct bnxt_ring_struct *ring;
2458
2459                 if (!bnapi)
2460                         continue;
2461
2462                 cpr = &bnapi->cp_ring;
2463                 ring = &cpr->cp_ring_struct;
2464
2465                 rc = bnxt_alloc_ring(bp, ring);
2466                 if (rc)
2467                         return rc;
2468
2469                 if (ulp_msix && i >= ulp_base_vec)
2470                         ring->map_idx = i + ulp_msix;
2471                 else
2472                         ring->map_idx = i;
2473         }
2474         return 0;
2475 }
2476
2477 static void bnxt_init_ring_struct(struct bnxt *bp)
2478 {
2479         int i;
2480
2481         for (i = 0; i < bp->cp_nr_rings; i++) {
2482                 struct bnxt_napi *bnapi = bp->bnapi[i];
2483                 struct bnxt_cp_ring_info *cpr;
2484                 struct bnxt_rx_ring_info *rxr;
2485                 struct bnxt_tx_ring_info *txr;
2486                 struct bnxt_ring_struct *ring;
2487
2488                 if (!bnapi)
2489                         continue;
2490
2491                 cpr = &bnapi->cp_ring;
2492                 ring = &cpr->cp_ring_struct;
2493                 ring->nr_pages = bp->cp_nr_pages;
2494                 ring->page_size = HW_CMPD_RING_SIZE;
2495                 ring->pg_arr = (void **)cpr->cp_desc_ring;
2496                 ring->dma_arr = cpr->cp_desc_mapping;
2497                 ring->vmem_size = 0;
2498
2499                 rxr = bnapi->rx_ring;
2500                 if (!rxr)
2501                         goto skip_rx;
2502
2503                 ring = &rxr->rx_ring_struct;
2504                 ring->nr_pages = bp->rx_nr_pages;
2505                 ring->page_size = HW_RXBD_RING_SIZE;
2506                 ring->pg_arr = (void **)rxr->rx_desc_ring;
2507                 ring->dma_arr = rxr->rx_desc_mapping;
2508                 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2509                 ring->vmem = (void **)&rxr->rx_buf_ring;
2510
2511                 ring = &rxr->rx_agg_ring_struct;
2512                 ring->nr_pages = bp->rx_agg_nr_pages;
2513                 ring->page_size = HW_RXBD_RING_SIZE;
2514                 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2515                 ring->dma_arr = rxr->rx_agg_desc_mapping;
2516                 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2517                 ring->vmem = (void **)&rxr->rx_agg_ring;
2518
2519 skip_rx:
2520                 txr = bnapi->tx_ring;
2521                 if (!txr)
2522                         continue;
2523
2524                 ring = &txr->tx_ring_struct;
2525                 ring->nr_pages = bp->tx_nr_pages;
2526                 ring->page_size = HW_RXBD_RING_SIZE;
2527                 ring->pg_arr = (void **)txr->tx_desc_ring;
2528                 ring->dma_arr = txr->tx_desc_mapping;
2529                 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2530                 ring->vmem = (void **)&txr->tx_buf_ring;
2531         }
2532 }
2533
2534 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2535 {
2536         int i;
2537         u32 prod;
2538         struct rx_bd **rx_buf_ring;
2539
2540         rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2541         for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2542                 int j;
2543                 struct rx_bd *rxbd;
2544
2545                 rxbd = rx_buf_ring[i];
2546                 if (!rxbd)
2547                         continue;
2548
2549                 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2550                         rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2551                         rxbd->rx_bd_opaque = prod;
2552                 }
2553         }
2554 }
2555
2556 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2557 {
2558         struct net_device *dev = bp->dev;
2559         struct bnxt_rx_ring_info *rxr;
2560         struct bnxt_ring_struct *ring;
2561         u32 prod, type;
2562         int i;
2563
2564         type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2565                 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2566
2567         if (NET_IP_ALIGN == 2)
2568                 type |= RX_BD_FLAGS_SOP;
2569
2570         rxr = &bp->rx_ring[ring_nr];
2571         ring = &rxr->rx_ring_struct;
2572         bnxt_init_rxbd_pages(ring, type);
2573
2574         if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2575                 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2576                 if (IS_ERR(rxr->xdp_prog)) {
2577                         int rc = PTR_ERR(rxr->xdp_prog);
2578
2579                         rxr->xdp_prog = NULL;
2580                         return rc;
2581                 }
2582         }
2583         prod = rxr->rx_prod;
2584         for (i = 0; i < bp->rx_ring_size; i++) {
2585                 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2586                         netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2587                                     ring_nr, i, bp->rx_ring_size);
2588                         break;
2589                 }
2590                 prod = NEXT_RX(prod);
2591         }
2592         rxr->rx_prod = prod;
2593         ring->fw_ring_id = INVALID_HW_RING_ID;
2594
2595         ring = &rxr->rx_agg_ring_struct;
2596         ring->fw_ring_id = INVALID_HW_RING_ID;
2597
2598         if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2599                 return 0;
2600
2601         type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2602                 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2603
2604         bnxt_init_rxbd_pages(ring, type);
2605
2606         prod = rxr->rx_agg_prod;
2607         for (i = 0; i < bp->rx_agg_ring_size; i++) {
2608                 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2609                         netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2610                                     ring_nr, i, bp->rx_ring_size);
2611                         break;
2612                 }
2613                 prod = NEXT_RX_AGG(prod);
2614         }
2615         rxr->rx_agg_prod = prod;
2616
2617         if (bp->flags & BNXT_FLAG_TPA) {
2618                 if (rxr->rx_tpa) {
2619                         u8 *data;
2620                         dma_addr_t mapping;
2621
2622                         for (i = 0; i < MAX_TPA; i++) {
2623                                 data = __bnxt_alloc_rx_data(bp, &mapping,
2624                                                             GFP_KERNEL);
2625                                 if (!data)
2626                                         return -ENOMEM;
2627
2628                                 rxr->rx_tpa[i].data = data;
2629                                 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
2630                                 rxr->rx_tpa[i].mapping = mapping;
2631                         }
2632                 } else {
2633                         netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2634                         return -ENOMEM;
2635                 }
2636         }
2637
2638         return 0;
2639 }
2640
2641 static void bnxt_init_cp_rings(struct bnxt *bp)
2642 {
2643         int i;
2644
2645         for (i = 0; i < bp->cp_nr_rings; i++) {
2646                 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2647                 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2648
2649                 ring->fw_ring_id = INVALID_HW_RING_ID;
2650                 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2651                 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2652         }
2653 }
2654
2655 static int bnxt_init_rx_rings(struct bnxt *bp)
2656 {
2657         int i, rc = 0;
2658
2659         if (BNXT_RX_PAGE_MODE(bp)) {
2660                 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2661                 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
2662         } else {
2663                 bp->rx_offset = BNXT_RX_OFFSET;
2664                 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2665         }
2666
2667         for (i = 0; i < bp->rx_nr_rings; i++) {
2668                 rc = bnxt_init_one_rx_ring(bp, i);
2669                 if (rc)
2670                         break;
2671         }
2672
2673         return rc;
2674 }
2675
2676 static int bnxt_init_tx_rings(struct bnxt *bp)
2677 {
2678         u16 i;
2679
2680         bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2681                                    MAX_SKB_FRAGS + 1);
2682
2683         for (i = 0; i < bp->tx_nr_rings; i++) {
2684                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2685                 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2686
2687                 ring->fw_ring_id = INVALID_HW_RING_ID;
2688         }
2689
2690         return 0;
2691 }
2692
2693 static void bnxt_free_ring_grps(struct bnxt *bp)
2694 {
2695         kfree(bp->grp_info);
2696         bp->grp_info = NULL;
2697 }
2698
2699 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2700 {
2701         int i;
2702
2703         if (irq_re_init) {
2704                 bp->grp_info = kcalloc(bp->cp_nr_rings,
2705                                        sizeof(struct bnxt_ring_grp_info),
2706                                        GFP_KERNEL);
2707                 if (!bp->grp_info)
2708                         return -ENOMEM;
2709         }
2710         for (i = 0; i < bp->cp_nr_rings; i++) {
2711                 if (irq_re_init)
2712                         bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2713                 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2714                 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2715                 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2716                 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2717         }
2718         return 0;
2719 }
2720
2721 static void bnxt_free_vnics(struct bnxt *bp)
2722 {
2723         kfree(bp->vnic_info);
2724         bp->vnic_info = NULL;
2725         bp->nr_vnics = 0;
2726 }
2727
2728 static int bnxt_alloc_vnics(struct bnxt *bp)
2729 {
2730         int num_vnics = 1;
2731
2732 #ifdef CONFIG_RFS_ACCEL
2733         if (bp->flags & BNXT_FLAG_RFS)
2734                 num_vnics += bp->rx_nr_rings;
2735 #endif
2736
2737         if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2738                 num_vnics++;
2739
2740         bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2741                                 GFP_KERNEL);
2742         if (!bp->vnic_info)
2743                 return -ENOMEM;
2744
2745         bp->nr_vnics = num_vnics;
2746         return 0;
2747 }
2748
2749 static void bnxt_init_vnics(struct bnxt *bp)
2750 {
2751         int i;
2752
2753         for (i = 0; i < bp->nr_vnics; i++) {
2754                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2755
2756                 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2757                 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2758                 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
2759                 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2760
2761                 if (bp->vnic_info[i].rss_hash_key) {
2762                         if (i == 0)
2763                                 prandom_bytes(vnic->rss_hash_key,
2764                                               HW_HASH_KEY_SIZE);
2765                         else
2766                                 memcpy(vnic->rss_hash_key,
2767                                        bp->vnic_info[0].rss_hash_key,
2768                                        HW_HASH_KEY_SIZE);
2769                 }
2770         }
2771 }
2772
2773 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2774 {
2775         int pages;
2776
2777         pages = ring_size / desc_per_pg;
2778
2779         if (!pages)
2780                 return 1;
2781
2782         pages++;
2783
2784         while (pages & (pages - 1))
2785                 pages++;
2786
2787         return pages;
2788 }
2789
2790 void bnxt_set_tpa_flags(struct bnxt *bp)
2791 {
2792         bp->flags &= ~BNXT_FLAG_TPA;
2793         if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2794                 return;
2795         if (bp->dev->features & NETIF_F_LRO)
2796                 bp->flags |= BNXT_FLAG_LRO;
2797         else if (bp->dev->features & NETIF_F_GRO_HW)
2798                 bp->flags |= BNXT_FLAG_GRO;
2799 }
2800
2801 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2802  * be set on entry.
2803  */
2804 void bnxt_set_ring_params(struct bnxt *bp)
2805 {
2806         u32 ring_size, rx_size, rx_space;
2807         u32 agg_factor = 0, agg_ring_size = 0;
2808
2809         /* 8 for CRC and VLAN */
2810         rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2811
2812         rx_space = rx_size + NET_SKB_PAD +
2813                 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2814
2815         bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2816         ring_size = bp->rx_ring_size;
2817         bp->rx_agg_ring_size = 0;
2818         bp->rx_agg_nr_pages = 0;
2819
2820         if (bp->flags & BNXT_FLAG_TPA)
2821                 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
2822
2823         bp->flags &= ~BNXT_FLAG_JUMBO;
2824         if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
2825                 u32 jumbo_factor;
2826
2827                 bp->flags |= BNXT_FLAG_JUMBO;
2828                 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2829                 if (jumbo_factor > agg_factor)
2830                         agg_factor = jumbo_factor;
2831         }
2832         agg_ring_size = ring_size * agg_factor;
2833
2834         if (agg_ring_size) {
2835                 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2836                                                         RX_DESC_CNT);
2837                 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2838                         u32 tmp = agg_ring_size;
2839
2840                         bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2841                         agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2842                         netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2843                                     tmp, agg_ring_size);
2844                 }
2845                 bp->rx_agg_ring_size = agg_ring_size;
2846                 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2847                 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2848                 rx_space = rx_size + NET_SKB_PAD +
2849                         SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2850         }
2851
2852         bp->rx_buf_use_size = rx_size;
2853         bp->rx_buf_size = rx_space;
2854
2855         bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2856         bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2857
2858         ring_size = bp->tx_ring_size;
2859         bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2860         bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2861
2862         ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2863         bp->cp_ring_size = ring_size;
2864
2865         bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2866         if (bp->cp_nr_pages > MAX_CP_PAGES) {
2867                 bp->cp_nr_pages = MAX_CP_PAGES;
2868                 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2869                 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2870                             ring_size, bp->cp_ring_size);
2871         }
2872         bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2873         bp->cp_ring_mask = bp->cp_bit - 1;
2874 }
2875
2876 /* Changing allocation mode of RX rings.
2877  * TODO: Update when extending xdp_rxq_info to support allocation modes.
2878  */
2879 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
2880 {
2881         if (page_mode) {
2882                 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2883                         return -EOPNOTSUPP;
2884                 bp->dev->max_mtu =
2885                         min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
2886                 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2887                 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2888                 bp->rx_dir = DMA_BIDIRECTIONAL;
2889                 bp->rx_skb_func = bnxt_rx_page_skb;
2890                 /* Disable LRO or GRO_HW */
2891                 netdev_update_features(bp->dev);
2892         } else {
2893                 bp->dev->max_mtu = bp->max_mtu;
2894                 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2895                 bp->rx_dir = DMA_FROM_DEVICE;
2896                 bp->rx_skb_func = bnxt_rx_skb;
2897         }
2898         return 0;
2899 }
2900
2901 static void bnxt_free_vnic_attributes(struct bnxt *bp)
2902 {
2903         int i;
2904         struct bnxt_vnic_info *vnic;
2905         struct pci_dev *pdev = bp->pdev;
2906
2907         if (!bp->vnic_info)
2908                 return;
2909
2910         for (i = 0; i < bp->nr_vnics; i++) {
2911                 vnic = &bp->vnic_info[i];
2912
2913                 kfree(vnic->fw_grp_ids);
2914                 vnic->fw_grp_ids = NULL;
2915
2916                 kfree(vnic->uc_list);
2917                 vnic->uc_list = NULL;
2918
2919                 if (vnic->mc_list) {
2920                         dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2921                                           vnic->mc_list, vnic->mc_list_mapping);
2922                         vnic->mc_list = NULL;
2923                 }
2924
2925                 if (vnic->rss_table) {
2926                         dma_free_coherent(&pdev->dev, PAGE_SIZE,
2927                                           vnic->rss_table,
2928                                           vnic->rss_table_dma_addr);
2929                         vnic->rss_table = NULL;
2930                 }
2931
2932                 vnic->rss_hash_key = NULL;
2933                 vnic->flags = 0;
2934         }
2935 }
2936
2937 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2938 {
2939         int i, rc = 0, size;
2940         struct bnxt_vnic_info *vnic;
2941         struct pci_dev *pdev = bp->pdev;
2942         int max_rings;
2943
2944         for (i = 0; i < bp->nr_vnics; i++) {
2945                 vnic = &bp->vnic_info[i];
2946
2947                 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2948                         int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2949
2950                         if (mem_size > 0) {
2951                                 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2952                                 if (!vnic->uc_list) {
2953                                         rc = -ENOMEM;
2954                                         goto out;
2955                                 }
2956                         }
2957                 }
2958
2959                 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2960                         vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2961                         vnic->mc_list =
2962                                 dma_alloc_coherent(&pdev->dev,
2963                                                    vnic->mc_list_size,
2964                                                    &vnic->mc_list_mapping,
2965                                                    GFP_KERNEL);
2966                         if (!vnic->mc_list) {
2967                                 rc = -ENOMEM;
2968                                 goto out;
2969                         }
2970                 }
2971
2972                 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2973                         max_rings = bp->rx_nr_rings;
2974                 else
2975                         max_rings = 1;
2976
2977                 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2978                 if (!vnic->fw_grp_ids) {
2979                         rc = -ENOMEM;
2980                         goto out;
2981                 }
2982
2983                 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2984                     !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2985                         continue;
2986
2987                 /* Allocate rss table and hash key */
2988                 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2989                                                      &vnic->rss_table_dma_addr,
2990                                                      GFP_KERNEL);
2991                 if (!vnic->rss_table) {
2992                         rc = -ENOMEM;
2993                         goto out;
2994                 }
2995
2996                 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2997
2998                 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2999                 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3000         }
3001         return 0;
3002
3003 out:
3004         return rc;
3005 }
3006
3007 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3008 {
3009         struct pci_dev *pdev = bp->pdev;
3010
3011         dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3012                           bp->hwrm_cmd_resp_dma_addr);
3013
3014         bp->hwrm_cmd_resp_addr = NULL;
3015 }
3016
3017 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3018 {
3019         struct pci_dev *pdev = bp->pdev;
3020
3021         bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3022                                                    &bp->hwrm_cmd_resp_dma_addr,
3023                                                    GFP_KERNEL);
3024         if (!bp->hwrm_cmd_resp_addr)
3025                 return -ENOMEM;
3026
3027         return 0;
3028 }
3029
3030 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3031 {
3032         if (bp->hwrm_short_cmd_req_addr) {
3033                 struct pci_dev *pdev = bp->pdev;
3034
3035                 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3036                                   bp->hwrm_short_cmd_req_addr,
3037                                   bp->hwrm_short_cmd_req_dma_addr);
3038                 bp->hwrm_short_cmd_req_addr = NULL;
3039         }
3040 }
3041
3042 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3043 {
3044         struct pci_dev *pdev = bp->pdev;
3045
3046         bp->hwrm_short_cmd_req_addr =
3047                 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3048                                    &bp->hwrm_short_cmd_req_dma_addr,
3049                                    GFP_KERNEL);
3050         if (!bp->hwrm_short_cmd_req_addr)
3051                 return -ENOMEM;
3052
3053         return 0;
3054 }
3055
3056 static void bnxt_free_stats(struct bnxt *bp)
3057 {
3058         u32 size, i;
3059         struct pci_dev *pdev = bp->pdev;
3060
3061         bp->flags &= ~BNXT_FLAG_PORT_STATS;
3062         bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3063
3064         if (bp->hw_rx_port_stats) {
3065                 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3066                                   bp->hw_rx_port_stats,
3067                                   bp->hw_rx_port_stats_map);
3068                 bp->hw_rx_port_stats = NULL;
3069         }
3070
3071         if (bp->hw_rx_port_stats_ext) {
3072                 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3073                                   bp->hw_rx_port_stats_ext,
3074                                   bp->hw_rx_port_stats_ext_map);
3075                 bp->hw_rx_port_stats_ext = NULL;
3076         }
3077
3078         if (!bp->bnapi)
3079                 return;
3080
3081         size = sizeof(struct ctx_hw_stats);
3082
3083         for (i = 0; i < bp->cp_nr_rings; i++) {
3084                 struct bnxt_napi *bnapi = bp->bnapi[i];
3085                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3086
3087                 if (cpr->hw_stats) {
3088                         dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3089                                           cpr->hw_stats_map);
3090                         cpr->hw_stats = NULL;
3091                 }
3092         }
3093 }
3094
3095 static int bnxt_alloc_stats(struct bnxt *bp)
3096 {
3097         u32 size, i;
3098         struct pci_dev *pdev = bp->pdev;
3099
3100         size = sizeof(struct ctx_hw_stats);
3101
3102         for (i = 0; i < bp->cp_nr_rings; i++) {
3103                 struct bnxt_napi *bnapi = bp->bnapi[i];
3104                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3105
3106                 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3107                                                    &cpr->hw_stats_map,
3108                                                    GFP_KERNEL);
3109                 if (!cpr->hw_stats)
3110                         return -ENOMEM;
3111
3112                 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3113         }
3114
3115         if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
3116                 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3117                                          sizeof(struct tx_port_stats) + 1024;
3118
3119                 bp->hw_rx_port_stats =
3120                         dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3121                                            &bp->hw_rx_port_stats_map,
3122                                            GFP_KERNEL);
3123                 if (!bp->hw_rx_port_stats)
3124                         return -ENOMEM;
3125
3126                 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3127                                        512;
3128                 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3129                                            sizeof(struct rx_port_stats) + 512;
3130                 bp->flags |= BNXT_FLAG_PORT_STATS;
3131
3132                 /* Display extended statistics only if FW supports it */
3133                 if (bp->hwrm_spec_code < 0x10804 ||
3134                     bp->hwrm_spec_code == 0x10900)
3135                         return 0;
3136
3137                 bp->hw_rx_port_stats_ext =
3138                         dma_zalloc_coherent(&pdev->dev,
3139                                             sizeof(struct rx_port_stats_ext),
3140                                             &bp->hw_rx_port_stats_ext_map,
3141                                             GFP_KERNEL);
3142                 if (!bp->hw_rx_port_stats_ext)
3143                         return 0;
3144
3145                 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3146         }
3147         return 0;
3148 }
3149
3150 static void bnxt_clear_ring_indices(struct bnxt *bp)
3151 {
3152         int i;
3153
3154         if (!bp->bnapi)
3155                 return;
3156
3157         for (i = 0; i < bp->cp_nr_rings; i++) {
3158                 struct bnxt_napi *bnapi = bp->bnapi[i];
3159                 struct bnxt_cp_ring_info *cpr;
3160                 struct bnxt_rx_ring_info *rxr;
3161                 struct bnxt_tx_ring_info *txr;
3162
3163                 if (!bnapi)
3164                         continue;
3165
3166                 cpr = &bnapi->cp_ring;
3167                 cpr->cp_raw_cons = 0;
3168
3169                 txr = bnapi->tx_ring;
3170                 if (txr) {
3171                         txr->tx_prod = 0;
3172                         txr->tx_cons = 0;
3173                 }
3174
3175                 rxr = bnapi->rx_ring;
3176                 if (rxr) {
3177                         rxr->rx_prod = 0;
3178                         rxr->rx_agg_prod = 0;
3179                         rxr->rx_sw_agg_prod = 0;
3180                         rxr->rx_next_cons = 0;
3181                 }
3182         }
3183 }
3184
3185 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3186 {
3187 #ifdef CONFIG_RFS_ACCEL
3188         int i;
3189
3190         /* Under rtnl_lock and all our NAPIs have been disabled.  It's
3191          * safe to delete the hash table.
3192          */
3193         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3194                 struct hlist_head *head;
3195                 struct hlist_node *tmp;
3196                 struct bnxt_ntuple_filter *fltr;
3197
3198                 head = &bp->ntp_fltr_hash_tbl[i];
3199                 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3200                         hlist_del(&fltr->hash);
3201                         kfree(fltr);
3202                 }
3203         }
3204         if (irq_reinit) {
3205                 kfree(bp->ntp_fltr_bmap);
3206                 bp->ntp_fltr_bmap = NULL;
3207         }
3208         bp->ntp_fltr_count = 0;
3209 #endif
3210 }
3211
3212 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3213 {
3214 #ifdef CONFIG_RFS_ACCEL
3215         int i, rc = 0;
3216
3217         if (!(bp->flags & BNXT_FLAG_RFS))
3218                 return 0;
3219
3220         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3221                 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3222
3223         bp->ntp_fltr_count = 0;
3224         bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3225                                     sizeof(long),
3226                                     GFP_KERNEL);
3227
3228         if (!bp->ntp_fltr_bmap)
3229                 rc = -ENOMEM;
3230
3231         return rc;
3232 #else
3233         return 0;
3234 #endif
3235 }
3236
3237 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3238 {
3239         bnxt_free_vnic_attributes(bp);
3240         bnxt_free_tx_rings(bp);
3241         bnxt_free_rx_rings(bp);
3242         bnxt_free_cp_rings(bp);
3243         bnxt_free_ntp_fltrs(bp, irq_re_init);
3244         if (irq_re_init) {
3245                 bnxt_free_stats(bp);
3246                 bnxt_free_ring_grps(bp);
3247                 bnxt_free_vnics(bp);
3248                 kfree(bp->tx_ring_map);
3249                 bp->tx_ring_map = NULL;
3250                 kfree(bp->tx_ring);
3251                 bp->tx_ring = NULL;
3252                 kfree(bp->rx_ring);
3253                 bp->rx_ring = NULL;
3254                 kfree(bp->bnapi);
3255                 bp->bnapi = NULL;
3256         } else {
3257                 bnxt_clear_ring_indices(bp);
3258         }
3259 }
3260
3261 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3262 {
3263         int i, j, rc, size, arr_size;
3264         void *bnapi;
3265
3266         if (irq_re_init) {
3267                 /* Allocate bnapi mem pointer array and mem block for
3268                  * all queues
3269                  */
3270                 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3271                                 bp->cp_nr_rings);
3272                 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3273                 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3274                 if (!bnapi)
3275                         return -ENOMEM;
3276
3277                 bp->bnapi = bnapi;
3278                 bnapi += arr_size;
3279                 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3280                         bp->bnapi[i] = bnapi;
3281                         bp->bnapi[i]->index = i;
3282                         bp->bnapi[i]->bp = bp;
3283                 }
3284
3285                 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3286                                       sizeof(struct bnxt_rx_ring_info),
3287                                       GFP_KERNEL);
3288                 if (!bp->rx_ring)
3289                         return -ENOMEM;
3290
3291                 for (i = 0; i < bp->rx_nr_rings; i++) {
3292                         bp->rx_ring[i].bnapi = bp->bnapi[i];
3293                         bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3294                 }
3295
3296                 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3297                                       sizeof(struct bnxt_tx_ring_info),
3298                                       GFP_KERNEL);
3299                 if (!bp->tx_ring)
3300                         return -ENOMEM;
3301
3302                 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3303                                           GFP_KERNEL);
3304
3305                 if (!bp->tx_ring_map)
3306                         return -ENOMEM;
3307
3308                 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3309                         j = 0;
3310                 else
3311                         j = bp->rx_nr_rings;
3312
3313                 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3314                         bp->tx_ring[i].bnapi = bp->bnapi[j];
3315                         bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
3316                         bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
3317                         if (i >= bp->tx_nr_rings_xdp) {
3318                                 bp->tx_ring[i].txq_index = i -
3319                                         bp->tx_nr_rings_xdp;
3320                                 bp->bnapi[j]->tx_int = bnxt_tx_int;
3321                         } else {
3322                                 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
3323                                 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3324                         }
3325                 }
3326
3327                 rc = bnxt_alloc_stats(bp);
3328                 if (rc)
3329                         goto alloc_mem_err;
3330
3331                 rc = bnxt_alloc_ntp_fltrs(bp);
3332                 if (rc)
3333                         goto alloc_mem_err;
3334
3335                 rc = bnxt_alloc_vnics(bp);
3336                 if (rc)
3337                         goto alloc_mem_err;
3338         }
3339
3340         bnxt_init_ring_struct(bp);
3341
3342         rc = bnxt_alloc_rx_rings(bp);
3343         if (rc)
3344                 goto alloc_mem_err;
3345
3346         rc = bnxt_alloc_tx_rings(bp);
3347         if (rc)
3348                 goto alloc_mem_err;
3349
3350         rc = bnxt_alloc_cp_rings(bp);
3351         if (rc)
3352                 goto alloc_mem_err;
3353
3354         bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3355                                   BNXT_VNIC_UCAST_FLAG;
3356         rc = bnxt_alloc_vnic_attributes(bp);
3357         if (rc)
3358                 goto alloc_mem_err;
3359         return 0;
3360
3361 alloc_mem_err:
3362         bnxt_free_mem(bp, true);
3363         return rc;
3364 }
3365
3366 static void bnxt_disable_int(struct bnxt *bp)
3367 {
3368         int i;
3369
3370         if (!bp->bnapi)
3371                 return;
3372
3373         for (i = 0; i < bp->cp_nr_rings; i++) {
3374                 struct bnxt_napi *bnapi = bp->bnapi[i];
3375                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3376                 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3377
3378                 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3379                         BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3380         }
3381 }
3382
3383 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3384 {
3385         struct bnxt_napi *bnapi = bp->bnapi[n];
3386         struct bnxt_cp_ring_info *cpr;
3387
3388         cpr = &bnapi->cp_ring;
3389         return cpr->cp_ring_struct.map_idx;
3390 }
3391
3392 static void bnxt_disable_int_sync(struct bnxt *bp)
3393 {
3394         int i;
3395
3396         atomic_inc(&bp->intr_sem);
3397
3398         bnxt_disable_int(bp);
3399         for (i = 0; i < bp->cp_nr_rings; i++) {
3400                 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3401
3402                 synchronize_irq(bp->irq_tbl[map_idx].vector);
3403         }
3404 }
3405
3406 static void bnxt_enable_int(struct bnxt *bp)
3407 {
3408         int i;
3409
3410         atomic_set(&bp->intr_sem, 0);
3411         for (i = 0; i < bp->cp_nr_rings; i++) {
3412                 struct bnxt_napi *bnapi = bp->bnapi[i];
3413                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3414
3415                 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3416         }
3417 }
3418
3419 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3420                             u16 cmpl_ring, u16 target_id)
3421 {
3422         struct input *req = request;
3423
3424         req->req_type = cpu_to_le16(req_type);
3425         req->cmpl_ring = cpu_to_le16(cmpl_ring);
3426         req->target_id = cpu_to_le16(target_id);
3427         req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3428 }
3429
3430 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3431                                  int timeout, bool silent)
3432 {
3433         int i, intr_process, rc, tmo_count;
3434         struct input *req = msg;
3435         u32 *data = msg;
3436         __le32 *resp_len;
3437         u8 *valid;
3438         u16 cp_ring_id, len = 0;
3439         struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3440         u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
3441         struct hwrm_short_input short_input = {0};
3442
3443         req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
3444         memset(resp, 0, PAGE_SIZE);
3445         cp_ring_id = le16_to_cpu(req->cmpl_ring);
3446         intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3447
3448         if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3449                 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3450
3451                 memcpy(short_cmd_req, req, msg_len);
3452                 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3453                                                    msg_len);
3454
3455                 short_input.req_type = req->req_type;
3456                 short_input.signature =
3457                                 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3458                 short_input.size = cpu_to_le16(msg_len);
3459                 short_input.req_addr =
3460                         cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3461
3462                 data = (u32 *)&short_input;
3463                 msg_len = sizeof(short_input);
3464
3465                 /* Sync memory write before updating doorbell */
3466                 wmb();
3467
3468                 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3469         }
3470
3471         /* Write request msg to hwrm channel */
3472         __iowrite32_copy(bp->bar0, data, msg_len / 4);
3473
3474         for (i = msg_len; i < max_req_len; i += 4)
3475                 writel(0, bp->bar0 + i);
3476
3477         /* currently supports only one outstanding message */
3478         if (intr_process)
3479                 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
3480
3481         /* Ring channel doorbell */
3482         writel(1, bp->bar0 + 0x100);
3483
3484         if (!timeout)
3485                 timeout = DFLT_HWRM_CMD_TIMEOUT;
3486         /* convert timeout to usec */
3487         timeout *= 1000;
3488
3489         i = 0;
3490         /* Short timeout for the first few iterations:
3491          * number of loops = number of loops for short timeout +
3492          * number of loops for standard timeout.
3493          */
3494         tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3495         timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3496         tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
3497         resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
3498         if (intr_process) {
3499                 /* Wait until hwrm response cmpl interrupt is processed */
3500                 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
3501                        i++ < tmo_count) {
3502                         /* on first few passes, just barely sleep */
3503                         if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3504                                 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3505                                              HWRM_SHORT_MAX_TIMEOUT);
3506                         else
3507                                 usleep_range(HWRM_MIN_TIMEOUT,
3508                                              HWRM_MAX_TIMEOUT);
3509                 }
3510
3511                 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3512                         netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
3513                                    le16_to_cpu(req->req_type));
3514                         return -1;
3515                 }
3516                 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3517                       HWRM_RESP_LEN_SFT;
3518                 valid = bp->hwrm_cmd_resp_addr + len - 1;
3519         } else {
3520                 int j;
3521
3522                 /* Check if response len is updated */
3523                 for (i = 0; i < tmo_count; i++) {
3524                         len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3525                               HWRM_RESP_LEN_SFT;
3526                         if (len)
3527                                 break;
3528                         /* on first few passes, just barely sleep */
3529                         if (i < DFLT_HWRM_CMD_TIMEOUT)
3530                                 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3531                                              HWRM_SHORT_MAX_TIMEOUT);
3532                         else
3533                                 usleep_range(HWRM_MIN_TIMEOUT,
3534                                              HWRM_MAX_TIMEOUT);
3535                 }
3536
3537                 if (i >= tmo_count) {
3538                         netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
3539                                    HWRM_TOTAL_TIMEOUT(i),
3540                                    le16_to_cpu(req->req_type),
3541                                    le16_to_cpu(req->seq_id), len);
3542                         return -1;
3543                 }
3544
3545                 /* Last byte of resp contains valid bit */
3546                 valid = bp->hwrm_cmd_resp_addr + len - 1;
3547                 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
3548                         /* make sure we read from updated DMA memory */
3549                         dma_rmb();
3550                         if (*valid)
3551                                 break;
3552                         udelay(1);
3553                 }
3554
3555                 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
3556                         netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
3557                                    HWRM_TOTAL_TIMEOUT(i),
3558                                    le16_to_cpu(req->req_type),
3559                                    le16_to_cpu(req->seq_id), len, *valid);
3560                         return -1;
3561                 }
3562         }
3563
3564         /* Zero valid bit for compatibility.  Valid bit in an older spec
3565          * may become a new field in a newer spec.  We must make sure that
3566          * a new field not implemented by old spec will read zero.
3567          */
3568         *valid = 0;
3569         rc = le16_to_cpu(resp->error_code);
3570         if (rc && !silent)
3571                 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3572                            le16_to_cpu(resp->req_type),
3573                            le16_to_cpu(resp->seq_id), rc);
3574         return rc;
3575 }
3576
3577 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3578 {
3579         return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
3580 }
3581
3582 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3583                               int timeout)
3584 {
3585         return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3586 }
3587
3588 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3589 {
3590         int rc;
3591
3592         mutex_lock(&bp->hwrm_cmd_lock);
3593         rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3594         mutex_unlock(&bp->hwrm_cmd_lock);
3595         return rc;
3596 }
3597
3598 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3599                              int timeout)
3600 {
3601         int rc;
3602
3603         mutex_lock(&bp->hwrm_cmd_lock);
3604         rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3605         mutex_unlock(&bp->hwrm_cmd_lock);
3606         return rc;
3607 }
3608
3609 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3610                                      int bmap_size)
3611 {
3612         struct hwrm_func_drv_rgtr_input req = {0};
3613         DECLARE_BITMAP(async_events_bmap, 256);
3614         u32 *events = (u32 *)async_events_bmap;
3615         int i;
3616
3617         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3618
3619         req.enables =
3620                 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3621
3622         memset(async_events_bmap, 0, sizeof(async_events_bmap));
3623         for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3624                 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3625
3626         if (bmap && bmap_size) {
3627                 for (i = 0; i < bmap_size; i++) {
3628                         if (test_bit(i, bmap))
3629                                 __set_bit(i, async_events_bmap);
3630                 }
3631         }
3632
3633         for (i = 0; i < 8; i++)
3634                 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3635
3636         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3637 }
3638
3639 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3640 {
3641         struct hwrm_func_drv_rgtr_input req = {0};
3642
3643         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3644
3645         req.enables =
3646                 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3647                             FUNC_DRV_RGTR_REQ_ENABLES_VER);
3648
3649         req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
3650         req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
3651         req.ver_maj_8b = DRV_VER_MAJ;
3652         req.ver_min_8b = DRV_VER_MIN;
3653         req.ver_upd_8b = DRV_VER_UPD;
3654         req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
3655         req.ver_min = cpu_to_le16(DRV_VER_MIN);
3656         req.ver_upd = cpu_to_le16(DRV_VER_UPD);
3657
3658         if (BNXT_PF(bp)) {
3659                 u32 data[8];
3660                 int i;
3661
3662                 memset(data, 0, sizeof(data));
3663                 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3664                         u16 cmd = bnxt_vf_req_snif[i];
3665                         unsigned int bit, idx;
3666
3667                         idx = cmd / 32;
3668                         bit = cmd % 32;
3669                         data[idx] |= 1 << bit;
3670                 }
3671
3672                 for (i = 0; i < 8; i++)
3673                         req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3674
3675                 req.enables |=
3676                         cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3677         }
3678
3679         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3680 }
3681
3682 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3683 {
3684         struct hwrm_func_drv_unrgtr_input req = {0};
3685
3686         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3687         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3688 }
3689
3690 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3691 {
3692         u32 rc = 0;
3693         struct hwrm_tunnel_dst_port_free_input req = {0};
3694
3695         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3696         req.tunnel_type = tunnel_type;
3697
3698         switch (tunnel_type) {
3699         case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3700                 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3701                 break;
3702         case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3703                 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3704                 break;
3705         default:
3706                 break;
3707         }
3708
3709         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3710         if (rc)
3711                 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3712                            rc);
3713         return rc;
3714 }
3715
3716 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3717                                            u8 tunnel_type)
3718 {
3719         u32 rc = 0;
3720         struct hwrm_tunnel_dst_port_alloc_input req = {0};
3721         struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3722
3723         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3724
3725         req.tunnel_type = tunnel_type;
3726         req.tunnel_dst_port_val = port;
3727
3728         mutex_lock(&bp->hwrm_cmd_lock);
3729         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3730         if (rc) {
3731                 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3732                            rc);
3733                 goto err_out;
3734         }
3735
3736         switch (tunnel_type) {
3737         case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
3738                 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3739                 break;
3740         case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
3741                 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3742                 break;
3743         default:
3744                 break;
3745         }
3746
3747 err_out:
3748         mutex_unlock(&bp->hwrm_cmd_lock);
3749         return rc;
3750 }
3751
3752 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3753 {
3754         struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3755         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3756
3757         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
3758         req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3759
3760         req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3761         req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3762         req.mask = cpu_to_le32(vnic->rx_mask);
3763         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3764 }
3765
3766 #ifdef CONFIG_RFS_ACCEL
3767 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3768                                             struct bnxt_ntuple_filter *fltr)
3769 {
3770         struct hwrm_cfa_ntuple_filter_free_input req = {0};
3771
3772         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3773         req.ntuple_filter_id = fltr->filter_id;
3774         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3775 }
3776
3777 #define BNXT_NTP_FLTR_FLAGS                                     \
3778         (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |     \
3779          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |        \
3780          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |      \
3781          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |      \
3782          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |       \
3783          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |  \
3784          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |       \
3785          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |  \
3786          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |      \
3787          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |         \
3788          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |    \
3789          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |         \
3790          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |    \
3791          CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
3792
3793 #define BNXT_NTP_TUNNEL_FLTR_FLAG                               \
3794                 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3795
3796 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3797                                              struct bnxt_ntuple_filter *fltr)
3798 {
3799         int rc = 0;
3800         struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3801         struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3802                 bp->hwrm_cmd_resp_addr;
3803         struct flow_keys *keys = &fltr->fkeys;
3804         struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3805
3806         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3807         req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
3808
3809         req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3810
3811         req.ethertype = htons(ETH_P_IP);
3812         memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
3813         req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
3814         req.ip_protocol = keys->basic.ip_proto;
3815
3816         if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3817                 int i;
3818
3819                 req.ethertype = htons(ETH_P_IPV6);
3820                 req.ip_addr_type =
3821                         CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3822                 *(struct in6_addr *)&req.src_ipaddr[0] =
3823                         keys->addrs.v6addrs.src;
3824                 *(struct in6_addr *)&req.dst_ipaddr[0] =
3825                         keys->addrs.v6addrs.dst;
3826                 for (i = 0; i < 4; i++) {
3827                         req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3828                         req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3829                 }
3830         } else {
3831                 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3832                 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3833                 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3834                 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3835         }
3836         if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3837                 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3838                 req.tunnel_type =
3839                         CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3840         }
3841
3842         req.src_port = keys->ports.src;
3843         req.src_port_mask = cpu_to_be16(0xffff);
3844         req.dst_port = keys->ports.dst;
3845         req.dst_port_mask = cpu_to_be16(0xffff);
3846
3847         req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
3848         mutex_lock(&bp->hwrm_cmd_lock);
3849         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3850         if (!rc)
3851                 fltr->filter_id = resp->ntuple_filter_id;
3852         mutex_unlock(&bp->hwrm_cmd_lock);
3853         return rc;
3854 }
3855 #endif
3856
3857 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3858                                      u8 *mac_addr)
3859 {
3860         u32 rc = 0;
3861         struct hwrm_cfa_l2_filter_alloc_input req = {0};
3862         struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3863
3864         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
3865         req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3866         if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3867                 req.flags |=
3868                         cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
3869         req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
3870         req.enables =
3871                 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
3872                             CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
3873                             CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3874         memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3875         req.l2_addr_mask[0] = 0xff;
3876         req.l2_addr_mask[1] = 0xff;
3877         req.l2_addr_mask[2] = 0xff;
3878         req.l2_addr_mask[3] = 0xff;
3879         req.l2_addr_mask[4] = 0xff;
3880         req.l2_addr_mask[5] = 0xff;
3881
3882         mutex_lock(&bp->hwrm_cmd_lock);
3883         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3884         if (!rc)
3885                 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3886                                                         resp->l2_filter_id;
3887         mutex_unlock(&bp->hwrm_cmd_lock);
3888         return rc;
3889 }
3890
3891 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3892 {
3893         u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3894         int rc = 0;
3895
3896         /* Any associated ntuple filters will also be cleared by firmware. */
3897         mutex_lock(&bp->hwrm_cmd_lock);
3898         for (i = 0; i < num_of_vnics; i++) {
3899                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3900
3901                 for (j = 0; j < vnic->uc_filter_count; j++) {
3902                         struct hwrm_cfa_l2_filter_free_input req = {0};
3903
3904                         bnxt_hwrm_cmd_hdr_init(bp, &req,
3905                                                HWRM_CFA_L2_FILTER_FREE, -1, -1);
3906
3907                         req.l2_filter_id = vnic->fw_l2_filter_id[j];
3908
3909                         rc = _hwrm_send_message(bp, &req, sizeof(req),
3910                                                 HWRM_CMD_TIMEOUT);
3911                 }
3912                 vnic->uc_filter_count = 0;
3913         }
3914         mutex_unlock(&bp->hwrm_cmd_lock);
3915
3916         return rc;
3917 }
3918
3919 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3920 {
3921         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3922         struct hwrm_vnic_tpa_cfg_input req = {0};
3923
3924         if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
3925                 return 0;
3926
3927         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3928
3929         if (tpa_flags) {
3930                 u16 mss = bp->dev->mtu - 40;
3931                 u32 nsegs, n, segs = 0, flags;
3932
3933                 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3934                         VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3935                         VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3936                         VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3937                         VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3938                 if (tpa_flags & BNXT_FLAG_GRO)
3939                         flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3940
3941                 req.flags = cpu_to_le32(flags);
3942
3943                 req.enables =
3944                         cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
3945                                     VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3946                                     VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
3947
3948                 /* Number of segs are log2 units, and first packet is not
3949                  * included as part of this units.
3950                  */
3951                 if (mss <= BNXT_RX_PAGE_SIZE) {
3952                         n = BNXT_RX_PAGE_SIZE / mss;
3953                         nsegs = (MAX_SKB_FRAGS - 1) * n;
3954                 } else {
3955                         n = mss / BNXT_RX_PAGE_SIZE;
3956                         if (mss & (BNXT_RX_PAGE_SIZE - 1))
3957                                 n++;
3958                         nsegs = (MAX_SKB_FRAGS - n) / n;
3959                 }
3960
3961                 segs = ilog2(nsegs);
3962                 req.max_agg_segs = cpu_to_le16(segs);
3963                 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
3964
3965                 req.min_agg_len = cpu_to_le32(512);
3966         }
3967         req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3968
3969         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3970 }
3971
3972 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3973 {
3974         u32 i, j, max_rings;
3975         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3976         struct hwrm_vnic_rss_cfg_input req = {0};
3977
3978         if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
3979                 return 0;
3980
3981         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3982         if (set_rss) {
3983                 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
3984                 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
3985                 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3986                         if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3987                                 max_rings = bp->rx_nr_rings - 1;
3988                         else
3989                                 max_rings = bp->rx_nr_rings;
3990                 } else {
3991                         max_rings = 1;
3992                 }
3993
3994                 /* Fill the RSS indirection table with ring group ids */
3995                 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3996                         if (j == max_rings)
3997                                 j = 0;
3998                         vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3999                 }
4000
4001                 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4002                 req.hash_key_tbl_addr =
4003                         cpu_to_le64(vnic->rss_hash_key_dma_addr);
4004         }
4005         req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4006         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4007 }
4008
4009 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4010 {
4011         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4012         struct hwrm_vnic_plcmodes_cfg_input req = {0};
4013
4014         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4015         req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4016                                 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4017                                 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4018         req.enables =
4019                 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4020                             VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4021         /* thresholds not implemented in firmware yet */
4022         req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4023         req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4024         req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4025         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4026 }
4027
4028 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4029                                         u16 ctx_idx)
4030 {
4031         struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4032
4033         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4034         req.rss_cos_lb_ctx_id =
4035                 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
4036
4037         hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4038         bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
4039 }
4040
4041 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4042 {
4043         int i, j;
4044
4045         for (i = 0; i < bp->nr_vnics; i++) {
4046                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4047
4048                 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4049                         if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4050                                 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4051                 }
4052         }
4053         bp->rsscos_nr_ctxs = 0;
4054 }
4055
4056 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
4057 {
4058         int rc;
4059         struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4060         struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4061                                                 bp->hwrm_cmd_resp_addr;
4062
4063         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4064                                -1);
4065
4066         mutex_lock(&bp->hwrm_cmd_lock);
4067         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4068         if (!rc)
4069                 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
4070                         le16_to_cpu(resp->rss_cos_lb_ctx_id);
4071         mutex_unlock(&bp->hwrm_cmd_lock);
4072
4073         return rc;
4074 }
4075
4076 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4077 {
4078         if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4079                 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4080         return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4081 }
4082
4083 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
4084 {
4085         unsigned int ring = 0, grp_idx;
4086         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4087         struct hwrm_vnic_cfg_input req = {0};
4088         u16 def_vlan = 0;
4089
4090         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
4091
4092         req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4093         /* Only RSS support for now TBD: COS & LB */
4094         if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4095                 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4096                 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4097                                            VNIC_CFG_REQ_ENABLES_MRU);
4098         } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4099                 req.rss_rule =
4100                         cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4101                 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4102                                            VNIC_CFG_REQ_ENABLES_MRU);
4103                 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
4104         } else {
4105                 req.rss_rule = cpu_to_le16(0xffff);
4106         }
4107
4108         if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4109             (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
4110                 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4111                 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4112         } else {
4113                 req.cos_rule = cpu_to_le16(0xffff);
4114         }
4115
4116         if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4117                 ring = 0;
4118         else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
4119                 ring = vnic_id - 1;
4120         else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4121                 ring = bp->rx_nr_rings - 1;
4122
4123         grp_idx = bp->rx_ring[ring].bnapi->index;
4124         req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4125         req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4126
4127         req.lb_rule = cpu_to_le16(0xffff);
4128         req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4129                               VLAN_HLEN);
4130
4131 #ifdef CONFIG_BNXT_SRIOV
4132         if (BNXT_VF(bp))
4133                 def_vlan = bp->vf.vlan;
4134 #endif
4135         if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
4136                 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
4137         if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4138                 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
4139
4140         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4141 }
4142
4143 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4144 {
4145         u32 rc = 0;
4146
4147         if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4148                 struct hwrm_vnic_free_input req = {0};
4149
4150                 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4151                 req.vnic_id =
4152                         cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4153
4154                 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4155                 if (rc)
4156                         return rc;
4157                 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4158         }
4159         return rc;
4160 }
4161
4162 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4163 {
4164         u16 i;
4165
4166         for (i = 0; i < bp->nr_vnics; i++)
4167                 bnxt_hwrm_vnic_free_one(bp, i);
4168 }
4169
4170 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4171                                 unsigned int start_rx_ring_idx,
4172                                 unsigned int nr_rings)
4173 {
4174         int rc = 0;
4175         unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
4176         struct hwrm_vnic_alloc_input req = {0};
4177         struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4178
4179         /* map ring groups to this vnic */
4180         for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4181                 grp_idx = bp->rx_ring[i].bnapi->index;
4182                 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
4183                         netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
4184                                    j, nr_rings);
4185                         break;
4186                 }
4187                 bp->vnic_info[vnic_id].fw_grp_ids[j] =
4188                                         bp->grp_info[grp_idx].fw_grp_id;
4189         }
4190
4191         bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4192         bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
4193         if (vnic_id == 0)
4194                 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4195
4196         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4197
4198         mutex_lock(&bp->hwrm_cmd_lock);
4199         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4200         if (!rc)
4201                 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4202         mutex_unlock(&bp->hwrm_cmd_lock);
4203         return rc;
4204 }
4205
4206 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4207 {
4208         struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4209         struct hwrm_vnic_qcaps_input req = {0};
4210         int rc;
4211
4212         if (bp->hwrm_spec_code < 0x10600)
4213                 return 0;
4214
4215         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4216         mutex_lock(&bp->hwrm_cmd_lock);
4217         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4218         if (!rc) {
4219                 u32 flags = le32_to_cpu(resp->flags);
4220
4221                 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)
4222                         bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4223                 if (flags &
4224                     VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4225                         bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
4226         }
4227         mutex_unlock(&bp->hwrm_cmd_lock);
4228         return rc;
4229 }
4230
4231 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4232 {
4233         u16 i;
4234         u32 rc = 0;
4235
4236         mutex_lock(&bp->hwrm_cmd_lock);
4237         for (i = 0; i < bp->rx_nr_rings; i++) {
4238                 struct hwrm_ring_grp_alloc_input req = {0};
4239                 struct hwrm_ring_grp_alloc_output *resp =
4240                                         bp->hwrm_cmd_resp_addr;
4241                 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
4242
4243                 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4244
4245                 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4246                 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4247                 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4248                 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
4249
4250                 rc = _hwrm_send_message(bp, &req, sizeof(req),
4251                                         HWRM_CMD_TIMEOUT);
4252                 if (rc)
4253                         break;
4254
4255                 bp->grp_info[grp_idx].fw_grp_id =
4256                         le32_to_cpu(resp->ring_group_id);
4257         }
4258         mutex_unlock(&bp->hwrm_cmd_lock);
4259         return rc;
4260 }
4261
4262 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4263 {
4264         u16 i;
4265         u32 rc = 0;
4266         struct hwrm_ring_grp_free_input req = {0};
4267
4268         if (!bp->grp_info)
4269                 return 0;
4270
4271         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4272
4273         mutex_lock(&bp->hwrm_cmd_lock);
4274         for (i = 0; i < bp->cp_nr_rings; i++) {
4275                 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4276                         continue;
4277                 req.ring_group_id =
4278                         cpu_to_le32(bp->grp_info[i].fw_grp_id);
4279
4280                 rc = _hwrm_send_message(bp, &req, sizeof(req),
4281                                         HWRM_CMD_TIMEOUT);
4282                 if (rc)
4283                         break;
4284                 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4285         }
4286         mutex_unlock(&bp->hwrm_cmd_lock);
4287         return rc;
4288 }
4289
4290 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4291                                     struct bnxt_ring_struct *ring,
4292                                     u32 ring_type, u32 map_index)
4293 {
4294         int rc = 0, err = 0;
4295         struct hwrm_ring_alloc_input req = {0};
4296         struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4297         struct bnxt_ring_grp_info *grp_info;
4298         u16 ring_id;
4299
4300         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4301
4302         req.enables = 0;
4303         if (ring->nr_pages > 1) {
4304                 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4305                 /* Page size is in log2 units */
4306                 req.page_size = BNXT_PAGE_SHIFT;
4307                 req.page_tbl_depth = 1;
4308         } else {
4309                 req.page_tbl_addr =  cpu_to_le64(ring->dma_arr[0]);
4310         }
4311         req.fbo = 0;
4312         /* Association of ring index with doorbell index and MSIX number */
4313         req.logical_id = cpu_to_le16(map_index);
4314
4315         switch (ring_type) {
4316         case HWRM_RING_ALLOC_TX:
4317                 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4318                 /* Association of transmit ring with completion ring */
4319                 grp_info = &bp->grp_info[ring->grp_idx];
4320                 req.cmpl_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
4321                 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4322                 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4323                 req.queue_id = cpu_to_le16(ring->queue_id);
4324                 break;
4325         case HWRM_RING_ALLOC_RX:
4326                 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4327                 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4328                 break;
4329         case HWRM_RING_ALLOC_AGG:
4330                 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4331                 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4332                 break;
4333         case HWRM_RING_ALLOC_CMPL:
4334                 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
4335                 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4336                 if (bp->flags & BNXT_FLAG_USING_MSIX)
4337                         req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4338                 break;
4339         default:
4340                 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4341                            ring_type);
4342                 return -1;
4343         }
4344
4345         mutex_lock(&bp->hwrm_cmd_lock);
4346         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4347         err = le16_to_cpu(resp->error_code);
4348         ring_id = le16_to_cpu(resp->ring_id);
4349         mutex_unlock(&bp->hwrm_cmd_lock);
4350
4351         if (rc || err) {
4352                 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4353                            ring_type, rc, err);
4354                 return -EIO;
4355         }
4356         ring->fw_ring_id = ring_id;
4357         return rc;
4358 }
4359
4360 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4361 {
4362         int rc;
4363
4364         if (BNXT_PF(bp)) {
4365                 struct hwrm_func_cfg_input req = {0};
4366
4367                 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4368                 req.fid = cpu_to_le16(0xffff);
4369                 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4370                 req.async_event_cr = cpu_to_le16(idx);
4371                 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4372         } else {
4373                 struct hwrm_func_vf_cfg_input req = {0};
4374
4375                 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4376                 req.enables =
4377                         cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4378                 req.async_event_cr = cpu_to_le16(idx);
4379                 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4380         }
4381         return rc;
4382 }
4383
4384 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4385 {
4386         int i, rc = 0;
4387
4388         for (i = 0; i < bp->cp_nr_rings; i++) {
4389                 struct bnxt_napi *bnapi = bp->bnapi[i];
4390                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4391                 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4392                 u32 map_idx = ring->map_idx;
4393
4394                 cpr->cp_doorbell = bp->bar1 + map_idx * 0x80;
4395                 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL,
4396                                               map_idx);
4397                 if (rc)
4398                         goto err_out;
4399                 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4400                 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
4401
4402                 if (!i) {
4403                         rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4404                         if (rc)
4405                                 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4406                 }
4407         }
4408
4409         for (i = 0; i < bp->tx_nr_rings; i++) {
4410                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4411                 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4412                 u32 map_idx = i;
4413
4414                 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4415                                               map_idx);
4416                 if (rc)
4417                         goto err_out;
4418                 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
4419         }
4420
4421         for (i = 0; i < bp->rx_nr_rings; i++) {
4422                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4423                 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4424                 u32 map_idx = rxr->bnapi->index;
4425
4426                 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4427                                               map_idx);
4428                 if (rc)
4429                         goto err_out;
4430                 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
4431                 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
4432                 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
4433         }
4434
4435         if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4436                 for (i = 0; i < bp->rx_nr_rings; i++) {
4437                         struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4438                         struct bnxt_ring_struct *ring =
4439                                                 &rxr->rx_agg_ring_struct;
4440                         u32 grp_idx = ring->grp_idx;
4441                         u32 map_idx = grp_idx + bp->rx_nr_rings;
4442
4443                         rc = hwrm_ring_alloc_send_msg(bp, ring,
4444                                                       HWRM_RING_ALLOC_AGG,
4445                                                       map_idx);
4446                         if (rc)
4447                                 goto err_out;
4448
4449                         rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
4450                         writel(DB_KEY_RX | rxr->rx_agg_prod,
4451                                rxr->rx_agg_doorbell);
4452                         bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
4453                 }
4454         }
4455 err_out:
4456         return rc;
4457 }
4458
4459 static int hwrm_ring_free_send_msg(struct bnxt *bp,
4460                                    struct bnxt_ring_struct *ring,
4461                                    u32 ring_type, int cmpl_ring_id)
4462 {
4463         int rc;
4464         struct hwrm_ring_free_input req = {0};
4465         struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4466         u16 error_code;
4467
4468         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
4469         req.ring_type = ring_type;
4470         req.ring_id = cpu_to_le16(ring->fw_ring_id);
4471
4472         mutex_lock(&bp->hwrm_cmd_lock);
4473         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4474         error_code = le16_to_cpu(resp->error_code);
4475         mutex_unlock(&bp->hwrm_cmd_lock);
4476
4477         if (rc || error_code) {
4478                 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
4479                            ring_type, rc, error_code);
4480                 return -EIO;
4481         }
4482         return 0;
4483 }
4484
4485 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
4486 {
4487         int i;
4488
4489         if (!bp->bnapi)
4490                 return;
4491
4492         for (i = 0; i < bp->tx_nr_rings; i++) {
4493                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4494                 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4495                 u32 grp_idx = txr->bnapi->index;
4496                 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4497
4498                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4499                         hwrm_ring_free_send_msg(bp, ring,
4500                                                 RING_FREE_REQ_RING_TYPE_TX,
4501                                                 close_path ? cmpl_ring_id :
4502                                                 INVALID_HW_RING_ID);
4503                         ring->fw_ring_id = INVALID_HW_RING_ID;
4504                 }
4505         }
4506
4507         for (i = 0; i < bp->rx_nr_rings; i++) {
4508                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4509                 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4510                 u32 grp_idx = rxr->bnapi->index;
4511                 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4512
4513                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4514                         hwrm_ring_free_send_msg(bp, ring,
4515                                                 RING_FREE_REQ_RING_TYPE_RX,
4516                                                 close_path ? cmpl_ring_id :
4517                                                 INVALID_HW_RING_ID);
4518                         ring->fw_ring_id = INVALID_HW_RING_ID;
4519                         bp->grp_info[grp_idx].rx_fw_ring_id =
4520                                 INVALID_HW_RING_ID;
4521                 }
4522         }
4523
4524         for (i = 0; i < bp->rx_nr_rings; i++) {
4525                 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4526                 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
4527                 u32 grp_idx = rxr->bnapi->index;
4528                 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4529
4530                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4531                         hwrm_ring_free_send_msg(bp, ring,
4532                                                 RING_FREE_REQ_RING_TYPE_RX,
4533                                                 close_path ? cmpl_ring_id :
4534                                                 INVALID_HW_RING_ID);
4535                         ring->fw_ring_id = INVALID_HW_RING_ID;
4536                         bp->grp_info[grp_idx].agg_fw_ring_id =
4537                                 INVALID_HW_RING_ID;
4538                 }
4539         }
4540
4541         /* The completion rings are about to be freed.  After that the
4542          * IRQ doorbell will not work anymore.  So we need to disable
4543          * IRQ here.
4544          */
4545         bnxt_disable_int_sync(bp);
4546
4547         for (i = 0; i < bp->cp_nr_rings; i++) {
4548                 struct bnxt_napi *bnapi = bp->bnapi[i];
4549                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4550                 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4551
4552                 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4553                         hwrm_ring_free_send_msg(bp, ring,
4554                                                 RING_FREE_REQ_RING_TYPE_L2_CMPL,
4555                                                 INVALID_HW_RING_ID);
4556                         ring->fw_ring_id = INVALID_HW_RING_ID;
4557                         bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4558                 }
4559         }
4560 }
4561
4562 static int bnxt_hwrm_get_rings(struct bnxt *bp)
4563 {
4564         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4565         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4566         struct hwrm_func_qcfg_input req = {0};
4567         int rc;
4568
4569         if (bp->hwrm_spec_code < 0x10601)
4570                 return 0;
4571
4572         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4573         req.fid = cpu_to_le16(0xffff);
4574         mutex_lock(&bp->hwrm_cmd_lock);
4575         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4576         if (rc) {
4577                 mutex_unlock(&bp->hwrm_cmd_lock);
4578                 return -EIO;
4579         }
4580
4581         hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4582         if (bp->flags & BNXT_FLAG_NEW_RM) {
4583                 u16 cp, stats;
4584
4585                 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
4586                 hw_resc->resv_hw_ring_grps =
4587                         le32_to_cpu(resp->alloc_hw_ring_grps);
4588                 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
4589                 cp = le16_to_cpu(resp->alloc_cmpl_rings);
4590                 stats = le16_to_cpu(resp->alloc_stat_ctx);
4591                 cp = min_t(u16, cp, stats);
4592                 hw_resc->resv_cp_rings = cp;
4593         }
4594         mutex_unlock(&bp->hwrm_cmd_lock);
4595         return 0;
4596 }
4597
4598 /* Caller must hold bp->hwrm_cmd_lock */
4599 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4600 {
4601         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4602         struct hwrm_func_qcfg_input req = {0};
4603         int rc;
4604
4605         if (bp->hwrm_spec_code < 0x10601)
4606                 return 0;
4607
4608         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4609         req.fid = cpu_to_le16(fid);
4610         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4611         if (!rc)
4612                 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4613
4614         return rc;
4615 }
4616
4617 static void
4618 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
4619                              int tx_rings, int rx_rings, int ring_grps,
4620                              int cp_rings, int vnics)
4621 {
4622         u32 enables = 0;
4623
4624         bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
4625         req->fid = cpu_to_le16(0xffff);
4626         enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
4627         req->num_tx_rings = cpu_to_le16(tx_rings);
4628         if (bp->flags & BNXT_FLAG_NEW_RM) {
4629                 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4630                 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4631                                       FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4632                 enables |= ring_grps ?
4633                            FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4634                 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4635
4636                 req->num_rx_rings = cpu_to_le16(rx_rings);
4637                 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4638                 req->num_cmpl_rings = cpu_to_le16(cp_rings);
4639                 req->num_stat_ctxs = req->num_cmpl_rings;
4640                 req->num_vnics = cpu_to_le16(vnics);
4641         }
4642         req->enables = cpu_to_le32(enables);
4643 }
4644
4645 static void
4646 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
4647                              struct hwrm_func_vf_cfg_input *req, int tx_rings,
4648                              int rx_rings, int ring_grps, int cp_rings,
4649                              int vnics)
4650 {
4651         u32 enables = 0;
4652
4653         bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
4654         enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
4655         enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4656         enables |= cp_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4657                               FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4658         enables |= ring_grps ? FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4659         enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4660
4661         req->num_tx_rings = cpu_to_le16(tx_rings);
4662         req->num_rx_rings = cpu_to_le16(rx_rings);
4663         req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4664         req->num_cmpl_rings = cpu_to_le16(cp_rings);
4665         req->num_stat_ctxs = req->num_cmpl_rings;
4666         req->num_vnics = cpu_to_le16(vnics);
4667
4668         req->enables = cpu_to_le32(enables);
4669 }
4670
4671 static int
4672 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4673                            int ring_grps, int cp_rings, int vnics)
4674 {
4675         struct hwrm_func_cfg_input req = {0};
4676         int rc;
4677
4678         __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4679                                      cp_rings, vnics);
4680         if (!req.enables)
4681                 return 0;
4682
4683         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4684         if (rc)
4685                 return -ENOMEM;
4686
4687         if (bp->hwrm_spec_code < 0x10601)
4688                 bp->hw_resc.resv_tx_rings = tx_rings;
4689
4690         rc = bnxt_hwrm_get_rings(bp);
4691         return rc;
4692 }
4693
4694 static int
4695 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4696                            int ring_grps, int cp_rings, int vnics)
4697 {
4698         struct hwrm_func_vf_cfg_input req = {0};
4699         int rc;
4700
4701         if (!(bp->flags & BNXT_FLAG_NEW_RM)) {
4702                 bp->hw_resc.resv_tx_rings = tx_rings;
4703                 return 0;
4704         }
4705
4706         __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4707                                      cp_rings, vnics);
4708         req.enables |= cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
4709                                    FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS);
4710         req.num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
4711         req.num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
4712         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4713         if (rc)
4714                 return -ENOMEM;
4715
4716         rc = bnxt_hwrm_get_rings(bp);
4717         return rc;
4718 }
4719
4720 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
4721                                    int cp, int vnic)
4722 {
4723         if (BNXT_PF(bp))
4724                 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, vnic);
4725         else
4726                 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, vnic);
4727 }
4728
4729 static int bnxt_cp_rings_in_use(struct bnxt *bp)
4730 {
4731         int cp = bp->cp_nr_rings;
4732         int ulp_msix, ulp_base;
4733
4734         ulp_msix = bnxt_get_ulp_msix_num(bp);
4735         if (ulp_msix) {
4736                 ulp_base = bnxt_get_ulp_msix_base(bp);
4737                 cp += ulp_msix;
4738                 if ((ulp_base + ulp_msix) > cp)
4739                         cp = ulp_base + ulp_msix;
4740         }
4741         return cp;
4742 }
4743
4744 static bool bnxt_need_reserve_rings(struct bnxt *bp)
4745 {
4746         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4747         int cp = bnxt_cp_rings_in_use(bp);
4748         int rx = bp->rx_nr_rings;
4749         int vnic = 1, grp = rx;
4750
4751         if (bp->hwrm_spec_code < 0x10601)
4752                 return false;
4753
4754         if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
4755                 return true;
4756
4757         if (bp->flags & BNXT_FLAG_RFS)
4758                 vnic = rx + 1;
4759         if (bp->flags & BNXT_FLAG_AGG_RINGS)
4760                 rx <<= 1;
4761         if ((bp->flags & BNXT_FLAG_NEW_RM) &&
4762             (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
4763              hw_resc->resv_hw_ring_grps != grp || hw_resc->resv_vnics != vnic))
4764                 return true;
4765         return false;
4766 }
4767
4768 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4769                            bool shared);
4770
4771 static int __bnxt_reserve_rings(struct bnxt *bp)
4772 {
4773         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4774         int cp = bnxt_cp_rings_in_use(bp);
4775         int tx = bp->tx_nr_rings;
4776         int rx = bp->rx_nr_rings;
4777         int grp, rx_rings, rc;
4778         bool sh = false;
4779         int vnic = 1;
4780
4781         if (!bnxt_need_reserve_rings(bp))
4782                 return 0;
4783
4784         if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4785                 sh = true;
4786         if (bp->flags & BNXT_FLAG_RFS)
4787                 vnic = rx + 1;
4788         if (bp->flags & BNXT_FLAG_AGG_RINGS)
4789                 rx <<= 1;
4790         grp = bp->rx_nr_rings;
4791
4792         rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, vnic);
4793         if (rc)
4794                 return rc;
4795
4796         tx = hw_resc->resv_tx_rings;
4797         if (bp->flags & BNXT_FLAG_NEW_RM) {
4798                 rx = hw_resc->resv_rx_rings;
4799                 cp = hw_resc->resv_cp_rings;
4800                 grp = hw_resc->resv_hw_ring_grps;
4801                 vnic = hw_resc->resv_vnics;
4802         }
4803
4804         rx_rings = rx;
4805         if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4806                 if (rx >= 2) {
4807                         rx_rings = rx >> 1;
4808                 } else {
4809                         if (netif_running(bp->dev))
4810                                 return -ENOMEM;
4811
4812                         bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4813                         bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4814                         bp->dev->hw_features &= ~NETIF_F_LRO;
4815                         bp->dev->features &= ~NETIF_F_LRO;
4816                         bnxt_set_ring_params(bp);
4817                 }
4818         }
4819         rx_rings = min_t(int, rx_rings, grp);
4820         rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
4821         if (bp->flags & BNXT_FLAG_AGG_RINGS)
4822                 rx = rx_rings << 1;
4823         cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
4824         bp->tx_nr_rings = tx;
4825         bp->rx_nr_rings = rx_rings;
4826         bp->cp_nr_rings = cp;
4827
4828         if (!tx || !rx || !cp || !grp || !vnic)
4829                 return -ENOMEM;
4830
4831         return rc;
4832 }
4833
4834 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4835                                     int ring_grps, int cp_rings, int vnics)
4836 {
4837         struct hwrm_func_vf_cfg_input req = {0};
4838         u32 flags;
4839         int rc;
4840
4841         if (!(bp->flags & BNXT_FLAG_NEW_RM))
4842                 return 0;
4843
4844         __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4845                                      cp_rings, vnics);
4846         flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
4847                 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4848                 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4849                 FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4850                 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4851                 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
4852
4853         req.flags = cpu_to_le32(flags);
4854         rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4855         if (rc)
4856                 return -ENOMEM;
4857         return 0;
4858 }
4859
4860 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4861                                     int ring_grps, int cp_rings, int vnics)
4862 {
4863         struct hwrm_func_cfg_input req = {0};
4864         u32 flags;
4865         int rc;
4866
4867         __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4868                                      cp_rings, vnics);
4869         flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
4870         if (bp->flags & BNXT_FLAG_NEW_RM)
4871                 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4872                          FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4873                          FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4874                          FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4875                          FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
4876
4877         req.flags = cpu_to_le32(flags);
4878         rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4879         if (rc)
4880                 return -ENOMEM;
4881         return 0;
4882 }
4883
4884 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4885                                  int ring_grps, int cp_rings, int vnics)
4886 {
4887         if (bp->hwrm_spec_code < 0x10801)
4888                 return 0;
4889
4890         if (BNXT_PF(bp))
4891                 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
4892                                                 ring_grps, cp_rings, vnics);
4893
4894         return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
4895                                         cp_rings, vnics);
4896 }
4897
4898 static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
4899         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4900 {
4901         u16 val, tmr, max, flags;
4902
4903         max = hw_coal->bufs_per_record * 128;
4904         if (hw_coal->budget)
4905                 max = hw_coal->bufs_per_record * hw_coal->budget;
4906
4907         val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
4908         req->num_cmpl_aggr_int = cpu_to_le16(val);
4909
4910         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4911         val = min_t(u16, val, 63);
4912         req->num_cmpl_dma_aggr = cpu_to_le16(val);
4913
4914         /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4915         val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 63);
4916         req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
4917
4918         tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks);
4919         tmr = max_t(u16, tmr, 1);
4920         req->int_lat_tmr_max = cpu_to_le16(tmr);
4921
4922         /* min timer set to 1/2 of interrupt timer */
4923         val = tmr / 2;
4924         req->int_lat_tmr_min = cpu_to_le16(val);
4925
4926         /* buf timer set to 1/4 of interrupt timer */
4927         val = max_t(u16, tmr / 4, 1);
4928         req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
4929
4930         tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks_irq);
4931         tmr = max_t(u16, tmr, 1);
4932         req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
4933
4934         flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4935         if (hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
4936                 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4937         req->flags = cpu_to_le16(flags);
4938 }
4939
4940 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
4941 {
4942         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
4943         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4944         struct bnxt_coal coal;
4945         unsigned int grp_idx;
4946
4947         /* Tick values in micro seconds.
4948          * 1 coal_buf x bufs_per_record = 1 completion record.
4949          */
4950         memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
4951
4952         coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
4953         coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
4954
4955         if (!bnapi->rx_ring)
4956                 return -ENODEV;
4957
4958         bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4959                                HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4960
4961         bnxt_hwrm_set_coal_params(&coal, &req_rx);
4962
4963         grp_idx = bnapi->index;
4964         req_rx.ring_id = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4965
4966         return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
4967                                  HWRM_CMD_TIMEOUT);
4968 }
4969
4970 int bnxt_hwrm_set_coal(struct bnxt *bp)
4971 {
4972         int i, rc = 0;
4973         struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4974                                                            req_tx = {0}, *req;
4975
4976         bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4977                                HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4978         bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4979                                HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4980
4981         bnxt_hwrm_set_coal_params(&bp->rx_coal, &req_rx);
4982         bnxt_hwrm_set_coal_params(&bp->tx_coal, &req_tx);
4983
4984         mutex_lock(&bp->hwrm_cmd_lock);
4985         for (i = 0; i < bp->cp_nr_rings; i++) {
4986                 struct bnxt_napi *bnapi = bp->bnapi[i];
4987
4988                 req = &req_rx;
4989                 if (!bnapi->rx_ring)
4990                         req = &req_tx;
4991                 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4992
4993                 rc = _hwrm_send_message(bp, req, sizeof(*req),
4994                                         HWRM_CMD_TIMEOUT);
4995                 if (rc)
4996                         break;
4997         }
4998         mutex_unlock(&bp->hwrm_cmd_lock);
4999         return rc;
5000 }
5001
5002 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5003 {
5004         int rc = 0, i;
5005         struct hwrm_stat_ctx_free_input req = {0};
5006
5007         if (!bp->bnapi)
5008                 return 0;
5009
5010         if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5011                 return 0;
5012
5013         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5014
5015         mutex_lock(&bp->hwrm_cmd_lock);
5016         for (i = 0; i < bp->cp_nr_rings; i++) {
5017                 struct bnxt_napi *bnapi = bp->bnapi[i];
5018                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5019
5020                 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5021                         req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5022
5023                         rc = _hwrm_send_message(bp, &req, sizeof(req),
5024                                                 HWRM_CMD_TIMEOUT);
5025                         if (rc)
5026                                 break;
5027
5028                         cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5029                 }
5030         }
5031         mutex_unlock(&bp->hwrm_cmd_lock);
5032         return rc;
5033 }
5034
5035 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5036 {
5037         int rc = 0, i;
5038         struct hwrm_stat_ctx_alloc_input req = {0};
5039         struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5040
5041         if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5042                 return 0;
5043
5044         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5045
5046         req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
5047
5048         mutex_lock(&bp->hwrm_cmd_lock);
5049         for (i = 0; i < bp->cp_nr_rings; i++) {
5050                 struct bnxt_napi *bnapi = bp->bnapi[i];
5051                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5052
5053                 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5054
5055                 rc = _hwrm_send_message(bp, &req, sizeof(req),
5056                                         HWRM_CMD_TIMEOUT);
5057                 if (rc)
5058                         break;
5059
5060                 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5061
5062                 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5063         }
5064         mutex_unlock(&bp->hwrm_cmd_lock);
5065         return rc;
5066 }
5067
5068 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5069 {
5070         struct hwrm_func_qcfg_input req = {0};
5071         struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5072         u16 flags;
5073         int rc;
5074
5075         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5076         req.fid = cpu_to_le16(0xffff);
5077         mutex_lock(&bp->hwrm_cmd_lock);
5078         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5079         if (rc)
5080                 goto func_qcfg_exit;
5081
5082 #ifdef CONFIG_BNXT_SRIOV
5083         if (BNXT_VF(bp)) {
5084                 struct bnxt_vf_info *vf = &bp->vf;
5085
5086                 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5087         }
5088 #endif
5089         flags = le16_to_cpu(resp->flags);
5090         if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5091                      FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
5092                 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
5093                 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
5094                         bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
5095         }
5096         if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5097                 bp->flags |= BNXT_FLAG_MULTI_HOST;
5098
5099         switch (resp->port_partition_type) {
5100         case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5101         case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5102         case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5103                 bp->port_partition_type = resp->port_partition_type;
5104                 break;
5105         }
5106         if (bp->hwrm_spec_code < 0x10707 ||
5107             resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5108                 bp->br_mode = BRIDGE_MODE_VEB;
5109         else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5110                 bp->br_mode = BRIDGE_MODE_VEPA;
5111         else
5112                 bp->br_mode = BRIDGE_MODE_UNDEF;
5113
5114         bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5115         if (!bp->max_mtu)
5116                 bp->max_mtu = BNXT_MAX_MTU;
5117
5118 func_qcfg_exit:
5119         mutex_unlock(&bp->hwrm_cmd_lock);
5120         return rc;
5121 }
5122
5123 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
5124 {
5125         struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5126         struct hwrm_func_resource_qcaps_input req = {0};
5127         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5128         int rc;
5129
5130         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
5131         req.fid = cpu_to_le16(0xffff);
5132
5133         mutex_lock(&bp->hwrm_cmd_lock);
5134         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5135         if (rc) {
5136                 rc = -EIO;
5137                 goto hwrm_func_resc_qcaps_exit;
5138         }
5139
5140         hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
5141         if (!all)
5142                 goto hwrm_func_resc_qcaps_exit;
5143
5144         hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
5145         hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5146         hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
5147         hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5148         hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
5149         hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5150         hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
5151         hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5152         hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
5153         hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
5154         hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
5155         hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5156         hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
5157         hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5158         hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
5159         hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5160
5161         if (BNXT_PF(bp)) {
5162                 struct bnxt_pf_info *pf = &bp->pf;
5163
5164                 pf->vf_resv_strategy =
5165                         le16_to_cpu(resp->vf_reservation_strategy);
5166                 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
5167                         pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
5168         }
5169 hwrm_func_resc_qcaps_exit:
5170         mutex_unlock(&bp->hwrm_cmd_lock);
5171         return rc;
5172 }
5173
5174 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
5175 {
5176         int rc = 0;
5177         struct hwrm_func_qcaps_input req = {0};
5178         struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5179         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5180         u32 flags;
5181
5182         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
5183         req.fid = cpu_to_le16(0xffff);
5184
5185         mutex_lock(&bp->hwrm_cmd_lock);
5186         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5187         if (rc)
5188                 goto hwrm_func_qcaps_exit;
5189
5190         flags = le32_to_cpu(resp->flags);
5191         if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
5192                 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
5193         if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
5194                 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
5195
5196         bp->tx_push_thresh = 0;
5197         if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
5198                 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
5199
5200         hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5201         hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5202         hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5203         hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5204         hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
5205         if (!hw_resc->max_hw_ring_grps)
5206                 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
5207         hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5208         hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5209         hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5210
5211         if (BNXT_PF(bp)) {
5212                 struct bnxt_pf_info *pf = &bp->pf;
5213
5214                 pf->fw_fid = le16_to_cpu(resp->fid);
5215                 pf->port_id = le16_to_cpu(resp->port_id);
5216                 bp->dev->dev_port = pf->port_id;
5217                 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
5218                 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
5219                 pf->max_vfs = le16_to_cpu(resp->max_vfs);
5220                 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
5221                 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
5222                 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
5223                 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
5224                 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
5225                 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
5226                 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
5227                         bp->flags |= BNXT_FLAG_WOL_CAP;
5228         } else {
5229 #ifdef CONFIG_BNXT_SRIOV
5230                 struct bnxt_vf_info *vf = &bp->vf;
5231
5232                 vf->fw_fid = le16_to_cpu(resp->fid);
5233                 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
5234 #endif
5235         }
5236
5237 hwrm_func_qcaps_exit:
5238         mutex_unlock(&bp->hwrm_cmd_lock);
5239         return rc;
5240 }
5241
5242 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
5243 {
5244         int rc;
5245
5246         rc = __bnxt_hwrm_func_qcaps(bp);
5247         if (rc)
5248                 return rc;
5249         if (bp->hwrm_spec_code >= 0x10803) {
5250                 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
5251                 if (!rc)
5252                         bp->flags |= BNXT_FLAG_NEW_RM;
5253         }
5254         return 0;
5255 }
5256
5257 static int bnxt_hwrm_func_reset(struct bnxt *bp)
5258 {
5259         struct hwrm_func_reset_input req = {0};
5260
5261         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
5262         req.enables = 0;
5263
5264         return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
5265 }
5266
5267 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
5268 {
5269         int rc = 0;
5270         struct hwrm_queue_qportcfg_input req = {0};
5271         struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
5272         u8 i, *qptr;
5273
5274         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
5275
5276         mutex_lock(&bp->hwrm_cmd_lock);
5277         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5278         if (rc)
5279                 goto qportcfg_exit;
5280
5281         if (!resp->max_configurable_queues) {
5282                 rc = -EINVAL;
5283                 goto qportcfg_exit;
5284         }
5285         bp->max_tc = resp->max_configurable_queues;
5286         bp->max_lltc = resp->max_configurable_lossless_queues;
5287         if (bp->max_tc > BNXT_MAX_QUEUE)
5288                 bp->max_tc = BNXT_MAX_QUEUE;
5289
5290         if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
5291                 bp->max_tc = 1;
5292
5293         if (bp->max_lltc > bp->max_tc)
5294                 bp->max_lltc = bp->max_tc;
5295
5296         qptr = &resp->queue_id0;
5297         for (i = 0; i < bp->max_tc; i++) {
5298                 bp->q_info[i].queue_id = *qptr++;
5299                 bp->q_info[i].queue_profile = *qptr++;
5300                 bp->tc_to_qidx[i] = i;
5301         }
5302
5303 qportcfg_exit:
5304         mutex_unlock(&bp->hwrm_cmd_lock);
5305         return rc;
5306 }
5307
5308 static int bnxt_hwrm_ver_get(struct bnxt *bp)
5309 {
5310         int rc;
5311         struct hwrm_ver_get_input req = {0};
5312         struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
5313         u32 dev_caps_cfg;
5314
5315         bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
5316         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
5317         req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
5318         req.hwrm_intf_min = HWRM_VERSION_MINOR;
5319         req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
5320         mutex_lock(&bp->hwrm_cmd_lock);
5321         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5322         if (rc)
5323                 goto hwrm_ver_get_exit;
5324
5325         memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
5326
5327         bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
5328                              resp->hwrm_intf_min_8b << 8 |
5329                              resp->hwrm_intf_upd_8b;
5330         if (resp->hwrm_intf_maj_8b < 1) {
5331                 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
5332                             resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
5333                             resp->hwrm_intf_upd_8b);
5334                 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
5335         }
5336         snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
5337                  resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
5338                  resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
5339
5340         bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
5341         if (!bp->hwrm_cmd_timeout)
5342                 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
5343
5344         if (resp->hwrm_intf_maj_8b >= 1)
5345                 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
5346
5347         bp->chip_num = le16_to_cpu(resp->chip_num);
5348         if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
5349             !resp->chip_metal)
5350                 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
5351
5352         dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
5353         if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
5354             (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
5355                 bp->flags |= BNXT_FLAG_SHORT_CMD;
5356
5357 hwrm_ver_get_exit:
5358         mutex_unlock(&bp->hwrm_cmd_lock);
5359         return rc;
5360 }
5361
5362 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
5363 {
5364         struct hwrm_fw_set_time_input req = {0};
5365         struct tm tm;
5366         time64_t now = ktime_get_real_seconds();
5367
5368         if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
5369             bp->hwrm_spec_code < 0x10400)
5370                 return -EOPNOTSUPP;
5371
5372         time64_to_tm(now, 0, &tm);
5373         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
5374         req.year = cpu_to_le16(1900 + tm.tm_year);
5375         req.month = 1 + tm.tm_mon;
5376         req.day = tm.tm_mday;
5377         req.hour = tm.tm_hour;
5378         req.minute = tm.tm_min;
5379         req.second = tm.tm_sec;
5380         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5381 }
5382
5383 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
5384 {
5385         int rc;
5386         struct bnxt_pf_info *pf = &bp->pf;
5387         struct hwrm_port_qstats_input req = {0};
5388
5389         if (!(bp->flags & BNXT_FLAG_PORT_STATS))
5390                 return 0;
5391
5392         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
5393         req.port_id = cpu_to_le16(pf->port_id);
5394         req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
5395         req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
5396         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5397         return rc;
5398 }
5399
5400 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
5401 {
5402         struct hwrm_port_qstats_ext_input req = {0};
5403         struct bnxt_pf_info *pf = &bp->pf;
5404
5405         if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
5406                 return 0;
5407
5408         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
5409         req.port_id = cpu_to_le16(pf->port_id);
5410         req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
5411         req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
5412         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5413 }
5414
5415 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
5416 {
5417         if (bp->vxlan_port_cnt) {
5418                 bnxt_hwrm_tunnel_dst_port_free(
5419                         bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5420         }
5421         bp->vxlan_port_cnt = 0;
5422         if (bp->nge_port_cnt) {
5423                 bnxt_hwrm_tunnel_dst_port_free(
5424                         bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5425         }
5426         bp->nge_port_cnt = 0;
5427 }
5428
5429 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
5430 {
5431         int rc, i;
5432         u32 tpa_flags = 0;
5433
5434         if (set_tpa)
5435                 tpa_flags = bp->flags & BNXT_FLAG_TPA;
5436         for (i = 0; i < bp->nr_vnics; i++) {
5437                 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
5438                 if (rc) {
5439                         netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
5440                                    i, rc);
5441                         return rc;
5442                 }
5443         }
5444         return 0;
5445 }
5446
5447 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
5448 {
5449         int i;
5450
5451         for (i = 0; i < bp->nr_vnics; i++)
5452                 bnxt_hwrm_vnic_set_rss(bp, i, false);
5453 }
5454
5455 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
5456                                     bool irq_re_init)
5457 {
5458         if (bp->vnic_info) {
5459                 bnxt_hwrm_clear_vnic_filter(bp);
5460                 /* clear all RSS setting before free vnic ctx */
5461                 bnxt_hwrm_clear_vnic_rss(bp);
5462                 bnxt_hwrm_vnic_ctx_free(bp);
5463                 /* before free the vnic, undo the vnic tpa settings */
5464                 if (bp->flags & BNXT_FLAG_TPA)
5465                         bnxt_set_tpa(bp, false);
5466                 bnxt_hwrm_vnic_free(bp);
5467         }
5468         bnxt_hwrm_ring_free(bp, close_path);
5469         bnxt_hwrm_ring_grp_free(bp);
5470         if (irq_re_init) {
5471                 bnxt_hwrm_stat_ctx_free(bp);
5472                 bnxt_hwrm_free_tunnel_ports(bp);
5473         }
5474 }
5475
5476 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
5477 {
5478         struct hwrm_func_cfg_input req = {0};
5479         int rc;
5480
5481         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5482         req.fid = cpu_to_le16(0xffff);
5483         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
5484         if (br_mode == BRIDGE_MODE_VEB)
5485                 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
5486         else if (br_mode == BRIDGE_MODE_VEPA)
5487                 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
5488         else
5489                 return -EINVAL;
5490         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5491         if (rc)
5492                 rc = -EIO;
5493         return rc;
5494 }
5495
5496 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
5497 {
5498         struct hwrm_func_cfg_input req = {0};
5499         int rc;
5500
5501         if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
5502                 return 0;
5503
5504         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5505         req.fid = cpu_to_le16(0xffff);
5506         req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
5507         req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
5508         if (size == 128)
5509                 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
5510
5511         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5512         if (rc)
5513                 rc = -EIO;
5514         return rc;
5515 }
5516
5517 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
5518 {
5519         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5520         int rc;
5521
5522         if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
5523                 goto skip_rss_ctx;
5524
5525         /* allocate context for vnic */
5526         rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
5527         if (rc) {
5528                 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5529                            vnic_id, rc);
5530                 goto vnic_setup_err;
5531         }
5532         bp->rsscos_nr_ctxs++;
5533
5534         if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5535                 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5536                 if (rc) {
5537                         netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5538                                    vnic_id, rc);
5539                         goto vnic_setup_err;
5540                 }
5541                 bp->rsscos_nr_ctxs++;
5542         }
5543
5544 skip_rss_ctx:
5545         /* configure default vnic, ring grp */
5546         rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5547         if (rc) {
5548                 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5549                            vnic_id, rc);
5550                 goto vnic_setup_err;
5551         }
5552
5553         /* Enable RSS hashing on vnic */
5554         rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5555         if (rc) {
5556                 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5557                            vnic_id, rc);
5558                 goto vnic_setup_err;
5559         }
5560
5561         if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5562                 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5563                 if (rc) {
5564                         netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5565                                    vnic_id, rc);
5566                 }
5567         }
5568
5569 vnic_setup_err:
5570         return rc;
5571 }
5572
5573 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5574 {
5575 #ifdef CONFIG_RFS_ACCEL
5576         int i, rc = 0;
5577
5578         for (i = 0; i < bp->rx_nr_rings; i++) {
5579                 struct bnxt_vnic_info *vnic;
5580                 u16 vnic_id = i + 1;
5581                 u16 ring_id = i;
5582
5583                 if (vnic_id >= bp->nr_vnics)
5584                         break;
5585
5586                 vnic = &bp->vnic_info[vnic_id];
5587                 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5588                 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5589                         vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
5590                 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
5591                 if (rc) {
5592                         netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5593                                    vnic_id, rc);
5594                         break;
5595                 }
5596                 rc = bnxt_setup_vnic(bp, vnic_id);
5597                 if (rc)
5598                         break;
5599         }
5600         return rc;
5601 #else
5602         return 0;
5603 #endif
5604 }
5605
5606 /* Allow PF and VF with default VLAN to be in promiscuous mode */
5607 static bool bnxt_promisc_ok(struct bnxt *bp)
5608 {
5609 #ifdef CONFIG_BNXT_SRIOV
5610         if (BNXT_VF(bp) && !bp->vf.vlan)
5611                 return false;
5612 #endif
5613         return true;
5614 }
5615
5616 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5617 {
5618         unsigned int rc = 0;
5619
5620         rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5621         if (rc) {
5622                 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5623                            rc);
5624                 return rc;
5625         }
5626
5627         rc = bnxt_hwrm_vnic_cfg(bp, 1);
5628         if (rc) {
5629                 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5630                            rc);
5631                 return rc;
5632         }
5633         return rc;
5634 }
5635
5636 static int bnxt_cfg_rx_mode(struct bnxt *);
5637 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
5638
5639 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5640 {
5641         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5642         int rc = 0;
5643         unsigned int rx_nr_rings = bp->rx_nr_rings;
5644
5645         if (irq_re_init) {
5646                 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5647                 if (rc) {
5648                         netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5649                                    rc);
5650                         goto err_out;
5651                 }
5652         }
5653
5654         rc = bnxt_hwrm_ring_alloc(bp);
5655         if (rc) {
5656                 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5657                 goto err_out;
5658         }
5659
5660         rc = bnxt_hwrm_ring_grp_alloc(bp);
5661         if (rc) {
5662                 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5663                 goto err_out;
5664         }
5665
5666         if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5667                 rx_nr_rings--;
5668
5669         /* default vnic 0 */
5670         rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
5671         if (rc) {
5672                 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5673                 goto err_out;
5674         }
5675
5676         rc = bnxt_setup_vnic(bp, 0);
5677         if (rc)
5678                 goto err_out;
5679
5680         if (bp->flags & BNXT_FLAG_RFS) {
5681                 rc = bnxt_alloc_rfs_vnics(bp);
5682                 if (rc)
5683                         goto err_out;
5684         }
5685
5686         if (bp->flags & BNXT_FLAG_TPA) {
5687                 rc = bnxt_set_tpa(bp, true);
5688                 if (rc)
5689                         goto err_out;
5690         }
5691
5692         if (BNXT_VF(bp))
5693                 bnxt_update_vf_mac(bp);
5694
5695         /* Filter for default vnic 0 */
5696         rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5697         if (rc) {
5698                 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5699                 goto err_out;
5700         }
5701         vnic->uc_filter_count = 1;
5702
5703         vnic->rx_mask = 0;
5704         if (bp->dev->flags & IFF_BROADCAST)
5705                 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
5706
5707         if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
5708                 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5709
5710         if (bp->dev->flags & IFF_ALLMULTI) {
5711                 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5712                 vnic->mc_list_count = 0;
5713         } else {
5714                 u32 mask = 0;
5715
5716                 bnxt_mc_list_updated(bp, &mask);
5717                 vnic->rx_mask |= mask;
5718         }
5719
5720         rc = bnxt_cfg_rx_mode(bp);
5721         if (rc)
5722                 goto err_out;
5723
5724         rc = bnxt_hwrm_set_coal(bp);
5725         if (rc)
5726                 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
5727                                 rc);
5728
5729         if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5730                 rc = bnxt_setup_nitroa0_vnic(bp);
5731                 if (rc)
5732                         netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5733                                    rc);
5734         }
5735
5736         if (BNXT_VF(bp)) {
5737                 bnxt_hwrm_func_qcfg(bp);
5738                 netdev_update_features(bp->dev);
5739         }
5740
5741         return 0;
5742
5743 err_out:
5744         bnxt_hwrm_resource_free(bp, 0, true);
5745
5746         return rc;
5747 }
5748
5749 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5750 {
5751         bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5752         return 0;
5753 }
5754
5755 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5756 {
5757         bnxt_init_cp_rings(bp);
5758         bnxt_init_rx_rings(bp);
5759         bnxt_init_tx_rings(bp);
5760         bnxt_init_ring_grps(bp, irq_re_init);
5761         bnxt_init_vnics(bp);
5762
5763         return bnxt_init_chip(bp, irq_re_init);
5764 }
5765
5766 static int bnxt_set_real_num_queues(struct bnxt *bp)
5767 {
5768         int rc;
5769         struct net_device *dev = bp->dev;
5770
5771         rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5772                                           bp->tx_nr_rings_xdp);
5773         if (rc)
5774                 return rc;
5775
5776         rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5777         if (rc)
5778                 return rc;
5779
5780 #ifdef CONFIG_RFS_ACCEL
5781         if (bp->flags & BNXT_FLAG_RFS)
5782                 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
5783 #endif
5784
5785         return rc;
5786 }
5787
5788 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5789                            bool shared)
5790 {
5791         int _rx = *rx, _tx = *tx;
5792
5793         if (shared) {
5794                 *rx = min_t(int, _rx, max);
5795                 *tx = min_t(int, _tx, max);
5796         } else {
5797                 if (max < 2)
5798                         return -ENOMEM;
5799
5800                 while (_rx + _tx > max) {
5801                         if (_rx > _tx && _rx > 1)
5802                                 _rx--;
5803                         else if (_tx > 1)
5804                                 _tx--;
5805                 }
5806                 *rx = _rx;
5807                 *tx = _tx;
5808         }
5809         return 0;
5810 }
5811
5812 static void bnxt_setup_msix(struct bnxt *bp)
5813 {
5814         const int len = sizeof(bp->irq_tbl[0].name);
5815         struct net_device *dev = bp->dev;
5816         int tcs, i;
5817
5818         tcs = netdev_get_num_tc(dev);
5819         if (tcs > 1) {
5820                 int i, off, count;
5821
5822                 for (i = 0; i < tcs; i++) {
5823                         count = bp->tx_nr_rings_per_tc;
5824                         off = i * count;
5825                         netdev_set_tc_queue(dev, i, count, off);
5826                 }
5827         }
5828
5829         for (i = 0; i < bp->cp_nr_rings; i++) {
5830                 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5831                 char *attr;
5832
5833                 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5834                         attr = "TxRx";
5835                 else if (i < bp->rx_nr_rings)
5836                         attr = "rx";
5837                 else
5838                         attr = "tx";
5839
5840                 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
5841                          attr, i);
5842                 bp->irq_tbl[map_idx].handler = bnxt_msix;
5843         }
5844 }
5845
5846 static void bnxt_setup_inta(struct bnxt *bp)
5847 {
5848         const int len = sizeof(bp->irq_tbl[0].name);
5849
5850         if (netdev_get_num_tc(bp->dev))
5851                 netdev_reset_tc(bp->dev);
5852
5853         snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5854                  0);
5855         bp->irq_tbl[0].handler = bnxt_inta;
5856 }
5857
5858 static int bnxt_setup_int_mode(struct bnxt *bp)
5859 {
5860         int rc;
5861
5862         if (bp->flags & BNXT_FLAG_USING_MSIX)
5863                 bnxt_setup_msix(bp);
5864         else
5865                 bnxt_setup_inta(bp);
5866
5867         rc = bnxt_set_real_num_queues(bp);
5868         return rc;
5869 }
5870
5871 #ifdef CONFIG_RFS_ACCEL
5872 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5873 {
5874         return bp->hw_resc.max_rsscos_ctxs;
5875 }
5876
5877 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5878 {
5879         return bp->hw_resc.max_vnics;
5880 }
5881 #endif
5882
5883 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5884 {
5885         return bp->hw_resc.max_stat_ctxs;
5886 }
5887
5888 void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5889 {
5890         bp->hw_resc.max_stat_ctxs = max;
5891 }
5892
5893 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5894 {
5895         return bp->hw_resc.max_cp_rings;
5896 }
5897
5898 void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5899 {
5900         bp->hw_resc.max_cp_rings = max;
5901 }
5902
5903 unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5904 {
5905         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5906
5907         return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
5908 }
5909
5910 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5911 {
5912         bp->hw_resc.max_irqs = max_irqs;
5913 }
5914
5915 int bnxt_get_avail_msix(struct bnxt *bp, int num)
5916 {
5917         int max_cp = bnxt_get_max_func_cp_rings(bp);
5918         int max_irq = bnxt_get_max_func_irqs(bp);
5919         int total_req = bp->cp_nr_rings + num;
5920         int max_idx, avail_msix;
5921
5922         max_idx = min_t(int, bp->total_irqs, max_cp);
5923         avail_msix = max_idx - bp->cp_nr_rings;
5924         if (!(bp->flags & BNXT_FLAG_NEW_RM) || avail_msix >= num)
5925                 return avail_msix;
5926
5927         if (max_irq < total_req) {
5928                 num = max_irq - bp->cp_nr_rings;
5929                 if (num <= 0)
5930                         return 0;
5931         }
5932         return num;
5933 }
5934
5935 static int bnxt_get_num_msix(struct bnxt *bp)
5936 {
5937         if (!(bp->flags & BNXT_FLAG_NEW_RM))
5938                 return bnxt_get_max_func_irqs(bp);
5939
5940         return bnxt_cp_rings_in_use(bp);
5941 }
5942
5943 static int bnxt_init_msix(struct bnxt *bp)
5944 {
5945         int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
5946         struct msix_entry *msix_ent;
5947
5948         total_vecs = bnxt_get_num_msix(bp);
5949         max = bnxt_get_max_func_irqs(bp);
5950         if (total_vecs > max)
5951                 total_vecs = max;
5952
5953         if (!total_vecs)
5954                 return 0;
5955
5956         msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5957         if (!msix_ent)
5958                 return -ENOMEM;
5959
5960         for (i = 0; i < total_vecs; i++) {
5961                 msix_ent[i].entry = i;
5962                 msix_ent[i].vector = 0;
5963         }
5964
5965         if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5966                 min = 2;
5967
5968         total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
5969         ulp_msix = bnxt_get_ulp_msix_num(bp);
5970         if (total_vecs < 0 || total_vecs < ulp_msix) {
5971                 rc = -ENODEV;
5972                 goto msix_setup_exit;
5973         }
5974
5975         bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5976         if (bp->irq_tbl) {
5977                 for (i = 0; i < total_vecs; i++)
5978                         bp->irq_tbl[i].vector = msix_ent[i].vector;
5979
5980                 bp->total_irqs = total_vecs;
5981                 /* Trim rings based upon num of vectors allocated */
5982                 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
5983                                      total_vecs - ulp_msix, min == 1);
5984                 if (rc)
5985                         goto msix_setup_exit;
5986
5987                 bp->cp_nr_rings = (min == 1) ?
5988                                   max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5989                                   bp->tx_nr_rings + bp->rx_nr_rings;
5990
5991         } else {
5992                 rc = -ENOMEM;
5993                 goto msix_setup_exit;
5994         }
5995         bp->flags |= BNXT_FLAG_USING_MSIX;
5996         kfree(msix_ent);
5997         return 0;
5998
5999 msix_setup_exit:
6000         netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
6001         kfree(bp->irq_tbl);
6002         bp->irq_tbl = NULL;
6003         pci_disable_msix(bp->pdev);
6004         kfree(msix_ent);
6005         return rc;
6006 }
6007
6008 static int bnxt_init_inta(struct bnxt *bp)
6009 {
6010         bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
6011         if (!bp->irq_tbl)
6012                 return -ENOMEM;
6013
6014         bp->total_irqs = 1;
6015         bp->rx_nr_rings = 1;
6016         bp->tx_nr_rings = 1;
6017         bp->cp_nr_rings = 1;
6018         bp->flags |= BNXT_FLAG_SHARED_RINGS;
6019         bp->irq_tbl[0].vector = bp->pdev->irq;
6020         return 0;
6021 }
6022
6023 static int bnxt_init_int_mode(struct bnxt *bp)
6024 {
6025         int rc = 0;
6026
6027         if (bp->flags & BNXT_FLAG_MSIX_CAP)
6028                 rc = bnxt_init_msix(bp);
6029
6030         if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
6031                 /* fallback to INTA */
6032                 rc = bnxt_init_inta(bp);
6033         }
6034         return rc;
6035 }
6036
6037 static void bnxt_clear_int_mode(struct bnxt *bp)
6038 {
6039         if (bp->flags & BNXT_FLAG_USING_MSIX)
6040                 pci_disable_msix(bp->pdev);
6041
6042         kfree(bp->irq_tbl);
6043         bp->irq_tbl = NULL;
6044         bp->flags &= ~BNXT_FLAG_USING_MSIX;
6045 }
6046
6047 int bnxt_reserve_rings(struct bnxt *bp)
6048 {
6049         int tcs = netdev_get_num_tc(bp->dev);
6050         int rc;
6051
6052         if (!bnxt_need_reserve_rings(bp))
6053                 return 0;
6054
6055         rc = __bnxt_reserve_rings(bp);
6056         if (rc) {
6057                 netdev_err(bp->dev, "ring reservation failure rc: %d\n", rc);
6058                 return rc;
6059         }
6060         if ((bp->flags & BNXT_FLAG_NEW_RM) &&
6061             (bnxt_get_num_msix(bp) != bp->total_irqs)) {
6062                 bnxt_ulp_irq_stop(bp);
6063                 bnxt_clear_int_mode(bp);
6064                 rc = bnxt_init_int_mode(bp);
6065                 bnxt_ulp_irq_restart(bp, rc);
6066                 if (rc)
6067                         return rc;
6068         }
6069         if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
6070                 netdev_err(bp->dev, "tx ring reservation failure\n");
6071                 netdev_reset_tc(bp->dev);
6072                 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
6073                 return -ENOMEM;
6074         }
6075         bp->num_stat_ctxs = bp->cp_nr_rings;
6076         return 0;
6077 }
6078
6079 static void bnxt_free_irq(struct bnxt *bp)
6080 {
6081         struct bnxt_irq *irq;
6082         int i;
6083
6084 #ifdef CONFIG_RFS_ACCEL
6085         free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
6086         bp->dev->rx_cpu_rmap = NULL;
6087 #endif
6088         if (!bp->irq_tbl || !bp->bnapi)
6089                 return;
6090
6091         for (i = 0; i < bp->cp_nr_rings; i++) {
6092                 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6093
6094                 irq = &bp->irq_tbl[map_idx];
6095                 if (irq->requested) {
6096                         if (irq->have_cpumask) {
6097                                 irq_set_affinity_hint(irq->vector, NULL);
6098                                 free_cpumask_var(irq->cpu_mask);
6099                                 irq->have_cpumask = 0;
6100                         }
6101                         free_irq(irq->vector, bp->bnapi[i]);
6102                 }
6103
6104                 irq->requested = 0;
6105         }
6106 }
6107
6108 static int bnxt_request_irq(struct bnxt *bp)
6109 {
6110         int i, j, rc = 0;
6111         unsigned long flags = 0;
6112 #ifdef CONFIG_RFS_ACCEL
6113         struct cpu_rmap *rmap;
6114 #endif
6115
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 #ifdef CONFIG_RFS_ACCEL
6123         rmap = bp->dev->rx_cpu_rmap;
6124 #endif
6125         if (!(bp->flags & BNXT_FLAG_USING_MSIX))
6126                 flags = IRQF_SHARED;
6127
6128         for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
6129                 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6130                 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
6131
6132 #ifdef CONFIG_RFS_ACCEL
6133                 if (rmap && bp->bnapi[i]->rx_ring) {
6134                         rc = irq_cpu_rmap_add(rmap, irq->vector);
6135                         if (rc)
6136                                 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
6137                                             j);
6138                         j++;
6139                 }
6140 #endif
6141                 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
6142                                  bp->bnapi[i]);
6143                 if (rc)
6144                         break;
6145
6146                 irq->requested = 1;
6147
6148                 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
6149                         int numa_node = dev_to_node(&bp->pdev->dev);
6150
6151                         irq->have_cpumask = 1;
6152                         cpumask_set_cpu(cpumask_local_spread(i, numa_node),
6153                                         irq->cpu_mask);
6154                         rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
6155                         if (rc) {
6156                                 netdev_warn(bp->dev,
6157                                             "Set affinity failed, IRQ = %d\n",
6158                                             irq->vector);
6159                                 break;
6160                         }
6161                 }
6162         }
6163         return rc;
6164 }
6165
6166 static void bnxt_del_napi(struct bnxt *bp)
6167 {
6168         int i;
6169
6170         if (!bp->bnapi)
6171                 return;
6172
6173         for (i = 0; i < bp->cp_nr_rings; i++) {
6174                 struct bnxt_napi *bnapi = bp->bnapi[i];
6175
6176                 napi_hash_del(&bnapi->napi);
6177                 netif_napi_del(&bnapi->napi);
6178         }
6179         /* We called napi_hash_del() before netif_napi_del(), we need
6180          * to respect an RCU grace period before freeing napi structures.
6181          */
6182         synchronize_net();
6183 }
6184
6185 static void bnxt_init_napi(struct bnxt *bp)
6186 {
6187         int i;
6188         unsigned int cp_nr_rings = bp->cp_nr_rings;
6189         struct bnxt_napi *bnapi;
6190
6191         if (bp->flags & BNXT_FLAG_USING_MSIX) {
6192                 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6193                         cp_nr_rings--;
6194                 for (i = 0; i < cp_nr_rings; i++) {
6195                         bnapi = bp->bnapi[i];
6196                         netif_napi_add(bp->dev, &bnapi->napi,
6197                                        bnxt_poll, 64);
6198                 }
6199                 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6200                         bnapi = bp->bnapi[cp_nr_rings];
6201                         netif_napi_add(bp->dev, &bnapi->napi,
6202                                        bnxt_poll_nitroa0, 64);
6203                 }
6204         } else {
6205                 bnapi = bp->bnapi[0];
6206                 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
6207         }
6208 }
6209
6210 static void bnxt_disable_napi(struct bnxt *bp)
6211 {
6212         int i;
6213
6214         if (!bp->bnapi)
6215                 return;
6216
6217         for (i = 0; i < bp->cp_nr_rings; i++) {
6218                 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
6219
6220                 if (bp->bnapi[i]->rx_ring)
6221                         cancel_work_sync(&cpr->dim.work);
6222
6223                 napi_disable(&bp->bnapi[i]->napi);
6224         }
6225 }
6226
6227 static void bnxt_enable_napi(struct bnxt *bp)
6228 {
6229         int i;
6230
6231         for (i = 0; i < bp->cp_nr_rings; i++) {
6232                 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
6233                 bp->bnapi[i]->in_reset = false;
6234
6235                 if (bp->bnapi[i]->rx_ring) {
6236                         INIT_WORK(&cpr->dim.work, bnxt_dim_work);
6237                         cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6238                 }
6239                 napi_enable(&bp->bnapi[i]->napi);
6240         }
6241 }
6242
6243 void bnxt_tx_disable(struct bnxt *bp)
6244 {
6245         int i;
6246         struct bnxt_tx_ring_info *txr;
6247
6248         if (bp->tx_ring) {
6249                 for (i = 0; i < bp->tx_nr_rings; i++) {
6250                         txr = &bp->tx_ring[i];
6251                         txr->dev_state = BNXT_DEV_STATE_CLOSING;
6252                 }
6253         }
6254         /* Stop all TX queues */
6255         netif_tx_disable(bp->dev);
6256         netif_carrier_off(bp->dev);
6257 }
6258
6259 void bnxt_tx_enable(struct bnxt *bp)
6260 {
6261         int i;
6262         struct bnxt_tx_ring_info *txr;
6263
6264         for (i = 0; i < bp->tx_nr_rings; i++) {
6265                 txr = &bp->tx_ring[i];
6266                 txr->dev_state = 0;
6267         }
6268         netif_tx_wake_all_queues(bp->dev);
6269         if (bp->link_info.link_up)
6270                 netif_carrier_on(bp->dev);
6271 }
6272
6273 static void bnxt_report_link(struct bnxt *bp)
6274 {
6275         if (bp->link_info.link_up) {
6276                 const char *duplex;
6277                 const char *flow_ctrl;
6278                 u32 speed;
6279                 u16 fec;
6280
6281                 netif_carrier_on(bp->dev);
6282                 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
6283                         duplex = "full";
6284                 else
6285                         duplex = "half";
6286                 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
6287                         flow_ctrl = "ON - receive & transmit";
6288                 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
6289                         flow_ctrl = "ON - transmit";
6290                 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
6291                         flow_ctrl = "ON - receive";
6292                 else
6293                         flow_ctrl = "none";
6294                 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
6295                 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
6296                             speed, duplex, flow_ctrl);
6297                 if (bp->flags & BNXT_FLAG_EEE_CAP)
6298                         netdev_info(bp->dev, "EEE is %s\n",
6299                                     bp->eee.eee_active ? "active" :
6300                                                          "not active");
6301                 fec = bp->link_info.fec_cfg;
6302                 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
6303                         netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
6304                                     (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
6305                                     (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
6306                                      (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
6307         } else {
6308                 netif_carrier_off(bp->dev);
6309                 netdev_err(bp->dev, "NIC Link is Down\n");
6310         }
6311 }
6312
6313 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
6314 {
6315         int rc = 0;
6316         struct hwrm_port_phy_qcaps_input req = {0};
6317         struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6318         struct bnxt_link_info *link_info = &bp->link_info;
6319
6320         if (bp->hwrm_spec_code < 0x10201)
6321                 return 0;
6322
6323         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
6324
6325         mutex_lock(&bp->hwrm_cmd_lock);
6326         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6327         if (rc)
6328                 goto hwrm_phy_qcaps_exit;
6329
6330         if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
6331                 struct ethtool_eee *eee = &bp->eee;
6332                 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
6333
6334                 bp->flags |= BNXT_FLAG_EEE_CAP;
6335                 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6336                 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
6337                                  PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
6338                 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
6339                                  PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
6340         }
6341         if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
6342                 if (bp->test_info)
6343                         bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
6344         }
6345         if (resp->supported_speeds_auto_mode)
6346                 link_info->support_auto_speeds =
6347                         le16_to_cpu(resp->supported_speeds_auto_mode);
6348
6349         bp->port_count = resp->port_cnt;
6350
6351 hwrm_phy_qcaps_exit:
6352         mutex_unlock(&bp->hwrm_cmd_lock);
6353         return rc;
6354 }
6355
6356 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
6357 {
6358         int rc = 0;
6359         struct bnxt_link_info *link_info = &bp->link_info;
6360         struct hwrm_port_phy_qcfg_input req = {0};
6361         struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6362         u8 link_up = link_info->link_up;
6363         u16 diff;
6364
6365         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
6366
6367         mutex_lock(&bp->hwrm_cmd_lock);
6368         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6369         if (rc) {
6370                 mutex_unlock(&bp->hwrm_cmd_lock);
6371                 return rc;
6372         }
6373
6374         memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
6375         link_info->phy_link_status = resp->link;
6376         link_info->duplex = resp->duplex_cfg;
6377         if (bp->hwrm_spec_code >= 0x10800)
6378                 link_info->duplex = resp->duplex_state;
6379         link_info->pause = resp->pause;
6380         link_info->auto_mode = resp->auto_mode;
6381         link_info->auto_pause_setting = resp->auto_pause;
6382         link_info->lp_pause = resp->link_partner_adv_pause;
6383         link_info->force_pause_setting = resp->force_pause;
6384         link_info->duplex_setting = resp->duplex_cfg;
6385         if (link_info->phy_link_status == BNXT_LINK_LINK)
6386                 link_info->link_speed = le16_to_cpu(resp->link_speed);
6387         else
6388                 link_info->link_speed = 0;
6389         link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
6390         link_info->support_speeds = le16_to_cpu(resp->support_speeds);
6391         link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
6392         link_info->lp_auto_link_speeds =
6393                 le16_to_cpu(resp->link_partner_adv_speeds);
6394         link_info->preemphasis = le32_to_cpu(resp->preemphasis);
6395         link_info->phy_ver[0] = resp->phy_maj;
6396         link_info->phy_ver[1] = resp->phy_min;
6397         link_info->phy_ver[2] = resp->phy_bld;
6398         link_info->media_type = resp->media_type;
6399         link_info->phy_type = resp->phy_type;
6400         link_info->transceiver = resp->xcvr_pkg_type;
6401         link_info->phy_addr = resp->eee_config_phy_addr &
6402                               PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
6403         link_info->module_status = resp->module_status;
6404
6405         if (bp->flags & BNXT_FLAG_EEE_CAP) {
6406                 struct ethtool_eee *eee = &bp->eee;
6407                 u16 fw_speeds;
6408
6409                 eee->eee_active = 0;
6410                 if (resp->eee_config_phy_addr &
6411                     PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
6412                         eee->eee_active = 1;
6413                         fw_speeds = le16_to_cpu(
6414                                 resp->link_partner_adv_eee_link_speed_mask);
6415                         eee->lp_advertised =
6416                                 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6417                 }
6418
6419                 /* Pull initial EEE config */
6420                 if (!chng_link_state) {
6421                         if (resp->eee_config_phy_addr &
6422                             PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
6423                                 eee->eee_enabled = 1;
6424
6425                         fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
6426                         eee->advertised =
6427                                 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6428
6429                         if (resp->eee_config_phy_addr &
6430                             PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
6431                                 __le32 tmr;
6432
6433                                 eee->tx_lpi_enabled = 1;
6434                                 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
6435                                 eee->tx_lpi_timer = le32_to_cpu(tmr) &
6436                                         PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
6437                         }
6438                 }
6439         }
6440
6441         link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
6442         if (bp->hwrm_spec_code >= 0x10504)
6443                 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
6444
6445         /* TODO: need to add more logic to report VF link */
6446         if (chng_link_state) {
6447                 if (link_info->phy_link_status == BNXT_LINK_LINK)
6448                         link_info->link_up = 1;
6449                 else
6450                         link_info->link_up = 0;
6451                 if (link_up != link_info->link_up)
6452                         bnxt_report_link(bp);
6453         } else {
6454                 /* alwasy link down if not require to update link state */
6455                 link_info->link_up = 0;
6456         }
6457         mutex_unlock(&bp->hwrm_cmd_lock);
6458
6459         if (!BNXT_SINGLE_PF(bp))
6460                 return 0;
6461
6462         diff = link_info->support_auto_speeds ^ link_info->advertising;
6463         if ((link_info->support_auto_speeds | diff) !=
6464             link_info->support_auto_speeds) {
6465                 /* An advertised speed is no longer supported, so we need to
6466                  * update the advertisement settings.  Caller holds RTNL
6467                  * so we can modify link settings.
6468                  */
6469                 link_info->advertising = link_info->support_auto_speeds;
6470                 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
6471                         bnxt_hwrm_set_link_setting(bp, true, false);
6472         }
6473         return 0;
6474 }
6475
6476 static void bnxt_get_port_module_status(struct bnxt *bp)
6477 {
6478         struct bnxt_link_info *link_info = &bp->link_info;
6479         struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
6480         u8 module_status;
6481
6482         if (bnxt_update_link(bp, true))
6483                 return;
6484
6485         module_status = link_info->module_status;
6486         switch (module_status) {
6487         case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
6488         case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
6489         case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
6490                 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
6491                             bp->pf.port_id);
6492                 if (bp->hwrm_spec_code >= 0x10201) {
6493                         netdev_warn(bp->dev, "Module part number %s\n",
6494                                     resp->phy_vendor_partnumber);
6495                 }
6496                 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
6497                         netdev_warn(bp->dev, "TX is disabled\n");
6498                 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
6499                         netdev_warn(bp->dev, "SFP+ module is shutdown\n");
6500         }
6501 }
6502
6503 static void
6504 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
6505 {
6506         if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
6507                 if (bp->hwrm_spec_code >= 0x10201)
6508                         req->auto_pause =
6509                                 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
6510                 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6511                         req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
6512                 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6513                         req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
6514                 req->enables |=
6515                         cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6516         } else {
6517                 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6518                         req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
6519                 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6520                         req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
6521                 req->enables |=
6522                         cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
6523                 if (bp->hwrm_spec_code >= 0x10201) {
6524                         req->auto_pause = req->force_pause;
6525                         req->enables |= cpu_to_le32(
6526                                 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6527                 }
6528         }
6529 }
6530
6531 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
6532                                       struct hwrm_port_phy_cfg_input *req)
6533 {
6534         u8 autoneg = bp->link_info.autoneg;
6535         u16 fw_link_speed = bp->link_info.req_link_speed;
6536         u16 advertising = bp->link_info.advertising;
6537
6538         if (autoneg & BNXT_AUTONEG_SPEED) {
6539                 req->auto_mode |=
6540                         PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
6541
6542                 req->enables |= cpu_to_le32(
6543                         PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
6544                 req->auto_link_speed_mask = cpu_to_le16(advertising);
6545
6546                 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
6547                 req->flags |=
6548                         cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
6549         } else {
6550                 req->force_link_speed = cpu_to_le16(fw_link_speed);
6551                 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
6552         }
6553
6554         /* tell chimp that the setting takes effect immediately */
6555         req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
6556 }
6557
6558 int bnxt_hwrm_set_pause(struct bnxt *bp)
6559 {
6560         struct hwrm_port_phy_cfg_input req = {0};
6561         int rc;
6562
6563         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6564         bnxt_hwrm_set_pause_common(bp, &req);
6565
6566         if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
6567             bp->link_info.force_link_chng)
6568                 bnxt_hwrm_set_link_common(bp, &req);
6569
6570         mutex_lock(&bp->hwrm_cmd_lock);
6571         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6572         if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
6573                 /* since changing of pause setting doesn't trigger any link
6574                  * change event, the driver needs to update the current pause
6575                  * result upon successfully return of the phy_cfg command
6576                  */
6577                 bp->link_info.pause =
6578                 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
6579                 bp->link_info.auto_pause_setting = 0;
6580                 if (!bp->link_info.force_link_chng)
6581                         bnxt_report_link(bp);
6582         }
6583         bp->link_info.force_link_chng = false;
6584         mutex_unlock(&bp->hwrm_cmd_lock);
6585         return rc;
6586 }
6587
6588 static void bnxt_hwrm_set_eee(struct bnxt *bp,
6589                               struct hwrm_port_phy_cfg_input *req)
6590 {
6591         struct ethtool_eee *eee = &bp->eee;
6592
6593         if (eee->eee_enabled) {
6594                 u16 eee_speeds;
6595                 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
6596
6597                 if (eee->tx_lpi_enabled)
6598                         flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
6599                 else
6600                         flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
6601
6602                 req->flags |= cpu_to_le32(flags);
6603                 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
6604                 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
6605                 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
6606         } else {
6607                 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
6608         }
6609 }
6610
6611 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
6612 {
6613         struct hwrm_port_phy_cfg_input req = {0};
6614
6615         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6616         if (set_pause)
6617                 bnxt_hwrm_set_pause_common(bp, &req);
6618
6619         bnxt_hwrm_set_link_common(bp, &req);
6620
6621         if (set_eee)
6622                 bnxt_hwrm_set_eee(bp, &req);
6623         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6624 }
6625
6626 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6627 {
6628         struct hwrm_port_phy_cfg_input req = {0};
6629
6630         if (!BNXT_SINGLE_PF(bp))
6631                 return 0;
6632
6633         if (pci_num_vf(bp->pdev))
6634                 return 0;
6635
6636         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6637         req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
6638         return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6639 }
6640
6641 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6642 {
6643         struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6644         struct hwrm_port_led_qcaps_input req = {0};
6645         struct bnxt_pf_info *pf = &bp->pf;
6646         int rc;
6647
6648         if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6649                 return 0;
6650
6651         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6652         req.port_id = cpu_to_le16(pf->port_id);
6653         mutex_lock(&bp->hwrm_cmd_lock);
6654         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6655         if (rc) {
6656                 mutex_unlock(&bp->hwrm_cmd_lock);
6657                 return rc;
6658         }
6659         if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6660                 int i;
6661
6662                 bp->num_leds = resp->num_leds;
6663                 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6664                                                  bp->num_leds);
6665                 for (i = 0; i < bp->num_leds; i++) {
6666                         struct bnxt_led_info *led = &bp->leds[i];
6667                         __le16 caps = led->led_state_caps;
6668
6669                         if (!led->led_group_id ||
6670                             !BNXT_LED_ALT_BLINK_CAP(caps)) {
6671                                 bp->num_leds = 0;
6672                                 break;
6673                         }
6674                 }
6675         }
6676         mutex_unlock(&bp->hwrm_cmd_lock);
6677         return 0;
6678 }
6679
6680 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6681 {
6682         struct hwrm_wol_filter_alloc_input req = {0};
6683         struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6684         int rc;
6685
6686         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6687         req.port_id = cpu_to_le16(bp->pf.port_id);
6688         req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6689         req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6690         memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6691         mutex_lock(&bp->hwrm_cmd_lock);
6692         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6693         if (!rc)
6694                 bp->wol_filter_id = resp->wol_filter_id;
6695         mutex_unlock(&bp->hwrm_cmd_lock);
6696         return rc;
6697 }
6698
6699 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6700 {
6701         struct hwrm_wol_filter_free_input req = {0};
6702         int rc;
6703
6704         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6705         req.port_id = cpu_to_le16(bp->pf.port_id);
6706         req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6707         req.wol_filter_id = bp->wol_filter_id;
6708         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6709         return rc;
6710 }
6711
6712 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6713 {
6714         struct hwrm_wol_filter_qcfg_input req = {0};
6715         struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6716         u16 next_handle = 0;
6717         int rc;
6718
6719         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6720         req.port_id = cpu_to_le16(bp->pf.port_id);
6721         req.handle = cpu_to_le16(handle);
6722         mutex_lock(&bp->hwrm_cmd_lock);
6723         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6724         if (!rc) {
6725                 next_handle = le16_to_cpu(resp->next_handle);
6726                 if (next_handle != 0) {
6727                         if (resp->wol_type ==
6728                             WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6729                                 bp->wol = 1;
6730                                 bp->wol_filter_id = resp->wol_filter_id;
6731                         }
6732                 }
6733         }
6734         mutex_unlock(&bp->hwrm_cmd_lock);
6735         return next_handle;
6736 }
6737
6738 static void bnxt_get_wol_settings(struct bnxt *bp)
6739 {
6740         u16 handle = 0;
6741
6742         if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6743                 return;
6744
6745         do {
6746                 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6747         } while (handle && handle != 0xffff);
6748 }
6749
6750 static bool bnxt_eee_config_ok(struct bnxt *bp)
6751 {
6752         struct ethtool_eee *eee = &bp->eee;
6753         struct bnxt_link_info *link_info = &bp->link_info;
6754
6755         if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6756                 return true;
6757
6758         if (eee->eee_enabled) {
6759                 u32 advertising =
6760                         _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6761
6762                 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6763                         eee->eee_enabled = 0;
6764                         return false;
6765                 }
6766                 if (eee->advertised & ~advertising) {
6767                         eee->advertised = advertising & eee->supported;
6768                         return false;
6769                 }
6770         }
6771         return true;
6772 }
6773
6774 static int bnxt_update_phy_setting(struct bnxt *bp)
6775 {
6776         int rc;
6777         bool update_link = false;
6778         bool update_pause = false;
6779         bool update_eee = false;
6780         struct bnxt_link_info *link_info = &bp->link_info;
6781
6782         rc = bnxt_update_link(bp, true);
6783         if (rc) {
6784                 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6785                            rc);
6786                 return rc;
6787         }
6788         if (!BNXT_SINGLE_PF(bp))
6789                 return 0;
6790
6791         if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6792             (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6793             link_info->req_flow_ctrl)
6794                 update_pause = true;
6795         if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6796             link_info->force_pause_setting != link_info->req_flow_ctrl)
6797                 update_pause = true;
6798         if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6799                 if (BNXT_AUTO_MODE(link_info->auto_mode))
6800                         update_link = true;
6801                 if (link_info->req_link_speed != link_info->force_link_speed)
6802                         update_link = true;
6803                 if (link_info->req_duplex != link_info->duplex_setting)
6804                         update_link = true;
6805         } else {
6806                 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6807                         update_link = true;
6808                 if (link_info->advertising != link_info->auto_link_speeds)
6809                         update_link = true;
6810         }
6811
6812         /* The last close may have shutdown the link, so need to call
6813          * PHY_CFG to bring it back up.
6814          */
6815         if (!netif_carrier_ok(bp->dev))
6816                 update_link = true;
6817
6818         if (!bnxt_eee_config_ok(bp))
6819                 update_eee = true;
6820
6821         if (update_link)
6822                 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
6823         else if (update_pause)
6824                 rc = bnxt_hwrm_set_pause(bp);
6825         if (rc) {
6826                 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6827                            rc);
6828                 return rc;
6829         }
6830
6831         return rc;
6832 }
6833
6834 /* Common routine to pre-map certain register block to different GRC window.
6835  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6836  * in PF and 3 windows in VF that can be customized to map in different
6837  * register blocks.
6838  */
6839 static void bnxt_preset_reg_win(struct bnxt *bp)
6840 {
6841         if (BNXT_PF(bp)) {
6842                 /* CAG registers map to GRC window #4 */
6843                 writel(BNXT_CAG_REG_BASE,
6844                        bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6845         }
6846 }
6847
6848 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
6849
6850 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6851 {
6852         int rc = 0;
6853
6854         bnxt_preset_reg_win(bp);
6855         netif_carrier_off(bp->dev);
6856         if (irq_re_init) {
6857                 /* Reserve rings now if none were reserved at driver probe. */
6858                 rc = bnxt_init_dflt_ring_mode(bp);
6859                 if (rc) {
6860                         netdev_err(bp->dev, "Failed to reserve default rings at open\n");
6861                         return rc;
6862                 }
6863                 rc = bnxt_reserve_rings(bp);
6864                 if (rc)
6865                         return rc;
6866         }
6867         if ((bp->flags & BNXT_FLAG_RFS) &&
6868             !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6869                 /* disable RFS if falling back to INTA */
6870                 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6871                 bp->flags &= ~BNXT_FLAG_RFS;
6872         }
6873
6874         rc = bnxt_alloc_mem(bp, irq_re_init);
6875         if (rc) {
6876                 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6877                 goto open_err_free_mem;
6878         }
6879
6880         if (irq_re_init) {
6881                 bnxt_init_napi(bp);
6882                 rc = bnxt_request_irq(bp);
6883                 if (rc) {
6884                         netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6885                         goto open_err_irq;
6886                 }
6887         }
6888
6889         bnxt_enable_napi(bp);
6890         bnxt_debug_dev_init(bp);
6891
6892         rc = bnxt_init_nic(bp, irq_re_init);
6893         if (rc) {
6894                 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6895                 goto open_err;
6896         }
6897
6898         if (link_re_init) {
6899                 mutex_lock(&bp->link_lock);
6900                 rc = bnxt_update_phy_setting(bp);
6901                 mutex_unlock(&bp->link_lock);
6902                 if (rc) {
6903                         netdev_warn(bp->dev, "failed to update phy settings\n");
6904                         if (BNXT_SINGLE_PF(bp)) {
6905                                 bp->link_info.phy_retry = true;
6906                                 bp->link_info.phy_retry_expires =
6907                                         jiffies + 5 * HZ;
6908                         }
6909                 }
6910         }
6911
6912         if (irq_re_init)
6913                 udp_tunnel_get_rx_info(bp->dev);
6914
6915         set_bit(BNXT_STATE_OPEN, &bp->state);
6916         bnxt_enable_int(bp);
6917         /* Enable TX queues */
6918         bnxt_tx_enable(bp);
6919         mod_timer(&bp->timer, jiffies + bp->current_interval);
6920         /* Poll link status and check for SFP+ module status */
6921         bnxt_get_port_module_status(bp);
6922
6923         /* VF-reps may need to be re-opened after the PF is re-opened */
6924         if (BNXT_PF(bp))
6925                 bnxt_vf_reps_open(bp);
6926         return 0;
6927
6928 open_err:
6929         bnxt_debug_dev_exit(bp);
6930         bnxt_disable_napi(bp);
6931
6932 open_err_irq:
6933         bnxt_del_napi(bp);
6934
6935 open_err_free_mem:
6936         bnxt_free_skbs(bp);
6937         bnxt_free_irq(bp);
6938         bnxt_free_mem(bp, true);
6939         return rc;
6940 }
6941
6942 /* rtnl_lock held */
6943 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6944 {
6945         int rc = 0;
6946
6947         rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6948         if (rc) {
6949                 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6950                 dev_close(bp->dev);
6951         }
6952         return rc;
6953 }
6954
6955 /* rtnl_lock held, open the NIC half way by allocating all resources, but
6956  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
6957  * self tests.
6958  */
6959 int bnxt_half_open_nic(struct bnxt *bp)
6960 {
6961         int rc = 0;
6962
6963         rc = bnxt_alloc_mem(bp, false);
6964         if (rc) {
6965                 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6966                 goto half_open_err;
6967         }
6968         rc = bnxt_init_nic(bp, false);
6969         if (rc) {
6970                 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6971                 goto half_open_err;
6972         }
6973         return 0;
6974
6975 half_open_err:
6976         bnxt_free_skbs(bp);
6977         bnxt_free_mem(bp, false);
6978         dev_close(bp->dev);
6979         return rc;
6980 }
6981
6982 /* rtnl_lock held, this call can only be made after a previous successful
6983  * call to bnxt_half_open_nic().
6984  */
6985 void bnxt_half_close_nic(struct bnxt *bp)
6986 {
6987         bnxt_hwrm_resource_free(bp, false, false);
6988         bnxt_free_skbs(bp);
6989         bnxt_free_mem(bp, false);
6990 }
6991
6992 static int bnxt_open(struct net_device *dev)
6993 {
6994         struct bnxt *bp = netdev_priv(dev);
6995
6996         return __bnxt_open_nic(bp, true, true);
6997 }
6998
6999 static bool bnxt_drv_busy(struct bnxt *bp)
7000 {
7001         return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
7002                 test_bit(BNXT_STATE_READ_STATS, &bp->state));
7003 }
7004
7005 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
7006                              bool link_re_init)
7007 {
7008         /* Close the VF-reps before closing PF */
7009         if (BNXT_PF(bp))
7010                 bnxt_vf_reps_close(bp);
7011
7012         /* Change device state to avoid TX queue wake up's */
7013         bnxt_tx_disable(bp);
7014
7015         clear_bit(BNXT_STATE_OPEN, &bp->state);
7016         smp_mb__after_atomic();
7017         while (bnxt_drv_busy(bp))
7018                 msleep(20);
7019
7020         /* Flush rings and and disable interrupts */
7021         bnxt_shutdown_nic(bp, irq_re_init);
7022
7023         /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
7024
7025         bnxt_debug_dev_exit(bp);
7026         bnxt_disable_napi(bp);
7027         del_timer_sync(&bp->timer);
7028         bnxt_free_skbs(bp);
7029
7030         if (irq_re_init) {
7031                 bnxt_free_irq(bp);
7032                 bnxt_del_napi(bp);
7033         }
7034         bnxt_free_mem(bp, irq_re_init);
7035 }
7036
7037 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7038 {
7039         int rc = 0;
7040
7041 #ifdef CONFIG_BNXT_SRIOV
7042         if (bp->sriov_cfg) {
7043                 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
7044                                                       !bp->sriov_cfg,
7045                                                       BNXT_SRIOV_CFG_WAIT_TMO);
7046                 if (rc)
7047                         netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
7048         }
7049 #endif
7050         __bnxt_close_nic(bp, irq_re_init, link_re_init);
7051         return rc;
7052 }
7053
7054 static int bnxt_close(struct net_device *dev)
7055 {
7056         struct bnxt *bp = netdev_priv(dev);
7057
7058         bnxt_close_nic(bp, true, true);
7059         bnxt_hwrm_shutdown_link(bp);
7060         return 0;
7061 }
7062
7063 /* rtnl_lock held */
7064 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7065 {
7066         switch (cmd) {
7067         case SIOCGMIIPHY:
7068                 /* fallthru */
7069         case SIOCGMIIREG: {
7070                 if (!netif_running(dev))
7071                         return -EAGAIN;
7072
7073                 return 0;
7074         }
7075
7076         case SIOCSMIIREG:
7077                 if (!netif_running(dev))
7078                         return -EAGAIN;
7079
7080                 return 0;
7081
7082         default:
7083                 /* do nothing */
7084                 break;
7085         }
7086         return -EOPNOTSUPP;
7087 }
7088
7089 static void
7090 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7091 {
7092         u32 i;
7093         struct bnxt *bp = netdev_priv(dev);
7094
7095         set_bit(BNXT_STATE_READ_STATS, &bp->state);
7096         /* Make sure bnxt_close_nic() sees that we are reading stats before
7097          * we check the BNXT_STATE_OPEN flag.
7098          */
7099         smp_mb__after_atomic();
7100         if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7101                 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
7102                 return;
7103         }
7104
7105         /* TODO check if we need to synchronize with bnxt_close path */
7106         for (i = 0; i < bp->cp_nr_rings; i++) {
7107                 struct bnxt_napi *bnapi = bp->bnapi[i];
7108                 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7109                 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
7110
7111                 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
7112                 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
7113                 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
7114
7115                 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
7116                 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
7117                 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
7118
7119                 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
7120                 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
7121                 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
7122
7123                 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
7124                 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
7125                 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
7126
7127                 stats->rx_missed_errors +=
7128                         le64_to_cpu(hw_stats->rx_discard_pkts);
7129
7130                 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
7131
7132                 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
7133         }
7134
7135         if (bp->flags & BNXT_FLAG_PORT_STATS) {
7136                 struct rx_port_stats *rx = bp->hw_rx_port_stats;
7137                 struct tx_port_stats *tx = bp->hw_tx_port_stats;
7138
7139                 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
7140                 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
7141                 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
7142                                           le64_to_cpu(rx->rx_ovrsz_frames) +
7143                                           le64_to_cpu(rx->rx_runt_frames);
7144                 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
7145                                    le64_to_cpu(rx->rx_jbr_frames);
7146                 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
7147                 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
7148                 stats->tx_errors = le64_to_cpu(tx->tx_err);
7149         }
7150         clear_bit(BNXT_STATE_READ_STATS, &bp->state);
7151 }
7152
7153 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
7154 {
7155         struct net_device *dev = bp->dev;
7156         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7157         struct netdev_hw_addr *ha;
7158         u8 *haddr;
7159         int mc_count = 0;
7160         bool update = false;
7161         int off = 0;
7162
7163         netdev_for_each_mc_addr(ha, dev) {
7164                 if (mc_count >= BNXT_MAX_MC_ADDRS) {
7165                         *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7166                         vnic->mc_list_count = 0;
7167                         return false;
7168                 }
7169                 haddr = ha->addr;
7170                 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
7171                         memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
7172                         update = true;
7173                 }
7174                 off += ETH_ALEN;
7175                 mc_count++;
7176         }
7177         if (mc_count)
7178                 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
7179
7180         if (mc_count != vnic->mc_list_count) {
7181                 vnic->mc_list_count = mc_count;
7182                 update = true;
7183         }
7184         return update;
7185 }
7186
7187 static bool bnxt_uc_list_updated(struct bnxt *bp)
7188 {
7189         struct net_device *dev = bp->dev;
7190         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7191         struct netdev_hw_addr *ha;
7192         int off = 0;
7193
7194         if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
7195                 return true;
7196
7197         netdev_for_each_uc_addr(ha, dev) {
7198                 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
7199                         return true;
7200
7201                 off += ETH_ALEN;
7202         }
7203         return false;
7204 }
7205
7206 static void bnxt_set_rx_mode(struct net_device *dev)
7207 {
7208         struct bnxt *bp = netdev_priv(dev);
7209         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7210         u32 mask = vnic->rx_mask;
7211         bool mc_update = false;
7212         bool uc_update;
7213
7214         if (!netif_running(dev))
7215                 return;
7216
7217         mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
7218                   CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
7219                   CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
7220                   CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
7221
7222         if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
7223                 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7224
7225         uc_update = bnxt_uc_list_updated(bp);
7226
7227         if (dev->flags & IFF_BROADCAST)
7228                 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
7229         if (dev->flags & IFF_ALLMULTI) {
7230                 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7231                 vnic->mc_list_count = 0;
7232         } else {
7233                 mc_update = bnxt_mc_list_updated(bp, &mask);
7234         }
7235
7236         if (mask != vnic->rx_mask || uc_update || mc_update) {
7237                 vnic->rx_mask = mask;
7238
7239                 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
7240                 bnxt_queue_sp_work(bp);
7241         }
7242 }
7243
7244 static int bnxt_cfg_rx_mode(struct bnxt *bp)
7245 {
7246         struct net_device *dev = bp->dev;
7247         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7248         struct netdev_hw_addr *ha;
7249         int i, off = 0, rc;
7250         bool uc_update;
7251
7252         netif_addr_lock_bh(dev);
7253         uc_update = bnxt_uc_list_updated(bp);
7254         netif_addr_unlock_bh(dev);
7255
7256         if (!uc_update)
7257                 goto skip_uc;
7258
7259         mutex_lock(&bp->hwrm_cmd_lock);
7260         for (i = 1; i < vnic->uc_filter_count; i++) {
7261                 struct hwrm_cfa_l2_filter_free_input req = {0};
7262
7263                 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
7264                                        -1);
7265
7266                 req.l2_filter_id = vnic->fw_l2_filter_id[i];
7267
7268                 rc = _hwrm_send_message(bp, &req, sizeof(req),
7269                                         HWRM_CMD_TIMEOUT);
7270         }
7271         mutex_unlock(&bp->hwrm_cmd_lock);
7272
7273         vnic->uc_filter_count = 1;
7274
7275         netif_addr_lock_bh(dev);
7276         if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
7277                 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7278         } else {
7279                 netdev_for_each_uc_addr(ha, dev) {
7280                         memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
7281                         off += ETH_ALEN;
7282                         vnic->uc_filter_count++;
7283                 }
7284         }
7285         netif_addr_unlock_bh(dev);
7286
7287         for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
7288                 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
7289                 if (rc) {
7290                         netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
7291                                    rc);
7292                         vnic->uc_filter_count = i;
7293                         return rc;
7294                 }
7295         }
7296
7297 skip_uc:
7298         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
7299         if (rc)
7300                 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
7301                            rc);
7302
7303         return rc;
7304 }
7305
7306 static bool bnxt_can_reserve_rings(struct bnxt *bp)
7307 {
7308 #ifdef CONFIG_BNXT_SRIOV
7309         if ((bp->flags & BNXT_FLAG_NEW_RM) && BNXT_VF(bp)) {
7310                 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7311
7312                 /* No minimum rings were provisioned by the PF.  Don't
7313                  * reserve rings by default when device is down.
7314                  */
7315                 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
7316                         return true;
7317
7318                 if (!netif_running(bp->dev))
7319                         return false;
7320         }
7321 #endif
7322         return true;
7323 }
7324
7325 /* If the chip and firmware supports RFS */
7326 static bool bnxt_rfs_supported(struct bnxt *bp)
7327 {
7328         if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
7329                 return true;
7330         if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7331                 return true;
7332         return false;
7333 }
7334
7335 /* If runtime conditions support RFS */
7336 static bool bnxt_rfs_capable(struct bnxt *bp)
7337 {
7338 #ifdef CONFIG_RFS_ACCEL
7339         int vnics, max_vnics, max_rss_ctxs;
7340
7341         if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
7342                 return false;
7343
7344         vnics = 1 + bp->rx_nr_rings;
7345         max_vnics = bnxt_get_max_func_vnics(bp);
7346         max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
7347
7348         /* RSS contexts not a limiting factor */
7349         if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7350                 max_rss_ctxs = max_vnics;
7351         if (vnics > max_vnics || vnics > max_rss_ctxs) {
7352                 if (bp->rx_nr_rings > 1)
7353                         netdev_warn(bp->dev,
7354                                     "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
7355                                     min(max_rss_ctxs - 1, max_vnics - 1));
7356                 return false;
7357         }
7358
7359         if (!(bp->flags & BNXT_FLAG_NEW_RM))
7360                 return true;
7361
7362         if (vnics == bp->hw_resc.resv_vnics)
7363                 return true;
7364
7365         bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, vnics);
7366         if (vnics <= bp->hw_resc.resv_vnics)
7367                 return true;
7368
7369         netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
7370         bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 1);
7371         return false;
7372 #else
7373         return false;
7374 #endif
7375 }
7376
7377 static netdev_features_t bnxt_fix_features(struct net_device *dev,
7378                                            netdev_features_t features)
7379 {
7380         struct bnxt *bp = netdev_priv(dev);
7381
7382         if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
7383                 features &= ~NETIF_F_NTUPLE;
7384
7385         if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7386                 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
7387
7388         if (!(features & NETIF_F_GRO))
7389                 features &= ~NETIF_F_GRO_HW;
7390
7391         if (features & NETIF_F_GRO_HW)
7392                 features &= ~NETIF_F_LRO;
7393
7394         /* Both CTAG and STAG VLAN accelaration on the RX side have to be
7395          * turned on or off together.
7396          */
7397         if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
7398             (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
7399                 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
7400                         features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7401                                       NETIF_F_HW_VLAN_STAG_RX);
7402                 else
7403                         features |= NETIF_F_HW_VLAN_CTAG_RX |
7404                                     NETIF_F_HW_VLAN_STAG_RX;
7405         }
7406 #ifdef CONFIG_BNXT_SRIOV
7407         if (BNXT_VF(bp)) {
7408                 if (bp->vf.vlan) {
7409                         features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7410                                       NETIF_F_HW_VLAN_STAG_RX);
7411                 }
7412         }
7413 #endif
7414         return features;
7415 }
7416
7417 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
7418 {
7419         struct bnxt *bp = netdev_priv(dev);
7420         u32 flags = bp->flags;
7421         u32 changes;
7422         int rc = 0;
7423         bool re_init = false;
7424         bool update_tpa = false;
7425
7426         flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
7427         if (features & NETIF_F_GRO_HW)
7428                 flags |= BNXT_FLAG_GRO;
7429         else if (features & NETIF_F_LRO)
7430                 flags |= BNXT_FLAG_LRO;
7431
7432         if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7433                 flags &= ~BNXT_FLAG_TPA;
7434
7435         if (features & NETIF_F_HW_VLAN_CTAG_RX)
7436                 flags |= BNXT_FLAG_STRIP_VLAN;
7437
7438         if (features & NETIF_F_NTUPLE)
7439                 flags |= BNXT_FLAG_RFS;
7440
7441         changes = flags ^ bp->flags;
7442         if (changes & BNXT_FLAG_TPA) {
7443                 update_tpa = true;
7444                 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
7445                     (flags & BNXT_FLAG_TPA) == 0)
7446                         re_init = true;
7447         }
7448
7449         if (changes & ~BNXT_FLAG_TPA)
7450                 re_init = true;
7451
7452         if (flags != bp->flags) {
7453                 u32 old_flags = bp->flags;
7454
7455                 bp->flags = flags;
7456
7457                 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7458                         if (update_tpa)
7459                                 bnxt_set_ring_params(bp);
7460                         return rc;
7461                 }
7462
7463                 if (re_init) {
7464                         bnxt_close_nic(bp, false, false);
7465                         if (update_tpa)
7466                                 bnxt_set_ring_params(bp);
7467
7468                         return bnxt_open_nic(bp, false, false);
7469                 }
7470                 if (update_tpa) {
7471                         rc = bnxt_set_tpa(bp,
7472                                           (flags & BNXT_FLAG_TPA) ?
7473                                           true : false);
7474                         if (rc)
7475                                 bp->flags = old_flags;
7476                 }
7477         }
7478         return rc;
7479 }
7480
7481 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
7482 {
7483         struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
7484         int i = bnapi->index;
7485
7486         if (!txr)
7487                 return;
7488
7489         netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
7490                     i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
7491                     txr->tx_cons);
7492 }
7493
7494 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
7495 {
7496         struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
7497         int i = bnapi->index;
7498
7499         if (!rxr)
7500                 return;
7501
7502         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",
7503                     i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
7504                     rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
7505                     rxr->rx_sw_agg_prod);
7506 }
7507
7508 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
7509 {
7510         struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7511         int i = bnapi->index;
7512
7513         netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
7514                     i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
7515 }
7516
7517 static void bnxt_dbg_dump_states(struct bnxt *bp)
7518 {
7519         int i;
7520         struct bnxt_napi *bnapi;
7521
7522         for (i = 0; i < bp->cp_nr_rings; i++) {
7523                 bnapi = bp->bnapi[i];
7524                 if (netif_msg_drv(bp)) {
7525                         bnxt_dump_tx_sw_state(bnapi);
7526                         bnxt_dump_rx_sw_state(bnapi);
7527                         bnxt_dump_cp_sw_state(bnapi);
7528                 }
7529         }
7530 }
7531
7532 static void bnxt_reset_task(struct bnxt *bp, bool silent)
7533 {
7534         if (!silent)
7535                 bnxt_dbg_dump_states(bp);
7536         if (netif_running(bp->dev)) {
7537                 int rc;
7538
7539                 if (!silent)
7540                         bnxt_ulp_stop(bp);
7541                 bnxt_close_nic(bp, false, false);
7542                 rc = bnxt_open_nic(bp, false, false);
7543                 if (!silent && !rc)
7544                         bnxt_ulp_start(bp);
7545         }
7546 }
7547
7548 static void bnxt_tx_timeout(struct net_device *dev)
7549 {
7550         struct bnxt *bp = netdev_priv(dev);
7551
7552         netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
7553         set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
7554         bnxt_queue_sp_work(bp);
7555 }
7556
7557 #ifdef CONFIG_NET_POLL_CONTROLLER
7558 static void bnxt_poll_controller(struct net_device *dev)
7559 {
7560         struct bnxt *bp = netdev_priv(dev);
7561         int i;
7562
7563         /* Only process tx rings/combined rings in netpoll mode. */
7564         for (i = 0; i < bp->tx_nr_rings; i++) {
7565                 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7566
7567                 napi_schedule(&txr->bnapi->napi);
7568         }
7569 }
7570 #endif
7571
7572 static void bnxt_timer(struct timer_list *t)
7573 {
7574         struct bnxt *bp = from_timer(bp, t, timer);
7575         struct net_device *dev = bp->dev;
7576
7577         if (!netif_running(dev))
7578                 return;
7579
7580         if (atomic_read(&bp->intr_sem) != 0)
7581                 goto bnxt_restart_timer;
7582
7583         if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
7584             bp->stats_coal_ticks) {
7585                 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
7586                 bnxt_queue_sp_work(bp);
7587         }
7588
7589         if (bnxt_tc_flower_enabled(bp)) {
7590                 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
7591                 bnxt_queue_sp_work(bp);
7592         }
7593
7594         if (bp->link_info.phy_retry) {
7595                 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
7596                         bp->link_info.phy_retry = 0;
7597                         netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
7598                 } else {
7599                         set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
7600                         bnxt_queue_sp_work(bp);
7601                 }
7602         }
7603 bnxt_restart_timer:
7604         mod_timer(&bp->timer, jiffies + bp->current_interval);
7605 }
7606
7607 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
7608 {
7609         /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
7610          * set.  If the device is being closed, bnxt_close() may be holding
7611          * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
7612          * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
7613          */
7614         clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7615         rtnl_lock();
7616 }
7617
7618 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
7619 {
7620         set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7621         rtnl_unlock();
7622 }
7623
7624 /* Only called from bnxt_sp_task() */
7625 static void bnxt_reset(struct bnxt *bp, bool silent)
7626 {
7627         bnxt_rtnl_lock_sp(bp);
7628         if (test_bit(BNXT_STATE_OPEN, &bp->state))
7629                 bnxt_reset_task(bp, silent);
7630         bnxt_rtnl_unlock_sp(bp);
7631 }
7632
7633 static void bnxt_cfg_ntp_filters(struct bnxt *);
7634
7635 static void bnxt_sp_task(struct work_struct *work)
7636 {
7637         struct bnxt *bp = container_of(work, struct bnxt, sp_task);
7638
7639         set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7640         smp_mb__after_atomic();
7641         if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7642                 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7643                 return;
7644         }
7645
7646         if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
7647                 bnxt_cfg_rx_mode(bp);
7648
7649         if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
7650                 bnxt_cfg_ntp_filters(bp);
7651         if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
7652                 bnxt_hwrm_exec_fwd_req(bp);
7653         if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7654                 bnxt_hwrm_tunnel_dst_port_alloc(
7655                         bp, bp->vxlan_port,
7656                         TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7657         }
7658         if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7659                 bnxt_hwrm_tunnel_dst_port_free(
7660                         bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7661         }
7662         if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7663                 bnxt_hwrm_tunnel_dst_port_alloc(
7664                         bp, bp->nge_port,
7665                         TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7666         }
7667         if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7668                 bnxt_hwrm_tunnel_dst_port_free(
7669                         bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7670         }
7671         if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
7672                 bnxt_hwrm_port_qstats(bp);
7673                 bnxt_hwrm_port_qstats_ext(bp);
7674         }
7675
7676         if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
7677                 int rc;
7678
7679                 mutex_lock(&bp->link_lock);
7680                 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
7681                                        &bp->sp_event))
7682                         bnxt_hwrm_phy_qcaps(bp);
7683
7684                 rc = bnxt_update_link(bp, true);
7685                 mutex_unlock(&bp->link_lock);
7686                 if (rc)
7687                         netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
7688                                    rc);
7689         }
7690         if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
7691                 int rc;
7692
7693                 mutex_lock(&bp->link_lock);
7694                 rc = bnxt_update_phy_setting(bp);
7695                 mutex_unlock(&bp->link_lock);
7696                 if (rc) {
7697                         netdev_warn(bp->dev, "update phy settings retry failed\n");
7698                 } else {
7699                         bp->link_info.phy_retry = false;
7700                         netdev_info(bp->dev, "update phy settings retry succeeded\n");
7701                 }
7702         }
7703         if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
7704                 mutex_lock(&bp->link_lock);
7705                 bnxt_get_port_module_status(bp);
7706                 mutex_unlock(&bp->link_lock);
7707         }
7708
7709         if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
7710                 bnxt_tc_flow_stats_work(bp);
7711
7712         /* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
7713          * must be the last functions to be called before exiting.
7714          */
7715         if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
7716                 bnxt_reset(bp, false);
7717
7718         if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
7719                 bnxt_reset(bp, true);
7720
7721         smp_mb__before_atomic();
7722         clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7723 }
7724
7725 /* Under rtnl_lock */
7726 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7727                      int tx_xdp)
7728 {
7729         int max_rx, max_tx, tx_sets = 1;
7730         int tx_rings_needed;
7731         int rx_rings = rx;
7732         int cp, vnics, rc;
7733
7734         if (tcs)
7735                 tx_sets = tcs;
7736
7737         rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7738         if (rc)
7739                 return rc;
7740
7741         if (max_rx < rx)
7742                 return -ENOMEM;
7743
7744         tx_rings_needed = tx * tx_sets + tx_xdp;
7745         if (max_tx < tx_rings_needed)
7746                 return -ENOMEM;
7747
7748         vnics = 1;
7749         if (bp->flags & BNXT_FLAG_RFS)
7750                 vnics += rx_rings;
7751
7752         if (bp->flags & BNXT_FLAG_AGG_RINGS)
7753                 rx_rings <<= 1;
7754         cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
7755         if (bp->flags & BNXT_FLAG_NEW_RM)
7756                 cp += bnxt_get_ulp_msix_num(bp);
7757         return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
7758                                      vnics);
7759 }
7760
7761 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7762 {
7763         if (bp->bar2) {
7764                 pci_iounmap(pdev, bp->bar2);
7765                 bp->bar2 = NULL;
7766         }
7767
7768         if (bp->bar1) {
7769                 pci_iounmap(pdev, bp->bar1);
7770                 bp->bar1 = NULL;
7771         }
7772
7773         if (bp->bar0) {
7774                 pci_iounmap(pdev, bp->bar0);
7775                 bp->bar0 = NULL;
7776         }
7777 }
7778
7779 static void bnxt_cleanup_pci(struct bnxt *bp)
7780 {
7781         bnxt_unmap_bars(bp, bp->pdev);
7782         pci_release_regions(bp->pdev);
7783         pci_disable_device(bp->pdev);
7784 }
7785
7786 static void bnxt_init_dflt_coal(struct bnxt *bp)
7787 {
7788         struct bnxt_coal *coal;
7789
7790         /* Tick values in micro seconds.
7791          * 1 coal_buf x bufs_per_record = 1 completion record.
7792          */
7793         coal = &bp->rx_coal;
7794         coal->coal_ticks = 14;
7795         coal->coal_bufs = 30;
7796         coal->coal_ticks_irq = 1;
7797         coal->coal_bufs_irq = 2;
7798         coal->idle_thresh = 50;
7799         coal->bufs_per_record = 2;
7800         coal->budget = 64;              /* NAPI budget */
7801
7802         coal = &bp->tx_coal;
7803         coal->coal_ticks = 28;
7804         coal->coal_bufs = 30;
7805         coal->coal_ticks_irq = 2;
7806         coal->coal_bufs_irq = 2;
7807         coal->bufs_per_record = 1;
7808
7809         bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7810 }
7811
7812 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7813 {
7814         int rc;
7815         struct bnxt *bp = netdev_priv(dev);
7816
7817         SET_NETDEV_DEV(dev, &pdev->dev);
7818
7819         /* enable device (incl. PCI PM wakeup), and bus-mastering */
7820         rc = pci_enable_device(pdev);
7821         if (rc) {
7822                 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7823                 goto init_err;
7824         }
7825
7826         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7827                 dev_err(&pdev->dev,
7828                         "Cannot find PCI device base address, aborting\n");
7829                 rc = -ENODEV;
7830                 goto init_err_disable;
7831         }
7832
7833         rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7834         if (rc) {
7835                 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7836                 goto init_err_disable;
7837         }
7838
7839         if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7840             dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7841                 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7842                 goto init_err_disable;
7843         }
7844
7845         pci_set_master(pdev);
7846
7847         bp->dev = dev;
7848         bp->pdev = pdev;
7849
7850         bp->bar0 = pci_ioremap_bar(pdev, 0);
7851         if (!bp->bar0) {
7852                 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7853                 rc = -ENOMEM;
7854                 goto init_err_release;
7855         }
7856
7857         bp->bar1 = pci_ioremap_bar(pdev, 2);
7858         if (!bp->bar1) {
7859                 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7860                 rc = -ENOMEM;
7861                 goto init_err_release;
7862         }
7863
7864         bp->bar2 = pci_ioremap_bar(pdev, 4);
7865         if (!bp->bar2) {
7866                 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7867                 rc = -ENOMEM;
7868                 goto init_err_release;
7869         }
7870
7871         pci_enable_pcie_error_reporting(pdev);
7872
7873         INIT_WORK(&bp->sp_task, bnxt_sp_task);
7874
7875         spin_lock_init(&bp->ntp_fltr_lock);
7876
7877         bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7878         bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7879
7880         bnxt_init_dflt_coal(bp);
7881
7882         timer_setup(&bp->timer, bnxt_timer, 0);
7883         bp->current_interval = BNXT_TIMER_INTERVAL;
7884
7885         clear_bit(BNXT_STATE_OPEN, &bp->state);
7886         return 0;
7887
7888 init_err_release:
7889         bnxt_unmap_bars(bp, pdev);
7890         pci_release_regions(pdev);
7891
7892 init_err_disable:
7893         pci_disable_device(pdev);
7894
7895 init_err:
7896         return rc;
7897 }
7898
7899 /* rtnl_lock held */
7900 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7901 {
7902         struct sockaddr *addr = p;
7903         struct bnxt *bp = netdev_priv(dev);
7904         int rc = 0;
7905
7906         if (!is_valid_ether_addr(addr->sa_data))
7907                 return -EADDRNOTAVAIL;
7908
7909         if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7910                 return 0;
7911
7912         rc = bnxt_approve_mac(bp, addr->sa_data);
7913         if (rc)
7914                 return rc;
7915
7916         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7917         if (netif_running(dev)) {
7918                 bnxt_close_nic(bp, false, false);
7919                 rc = bnxt_open_nic(bp, false, false);
7920         }
7921
7922         return rc;
7923 }
7924
7925 /* rtnl_lock held */
7926 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7927 {
7928         struct bnxt *bp = netdev_priv(dev);
7929
7930         if (netif_running(dev))
7931                 bnxt_close_nic(bp, false, false);
7932
7933         dev->mtu = new_mtu;
7934         bnxt_set_ring_params(bp);
7935
7936         if (netif_running(dev))
7937                 return bnxt_open_nic(bp, false, false);
7938
7939         return 0;
7940 }
7941
7942 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
7943 {
7944         struct bnxt *bp = netdev_priv(dev);
7945         bool sh = false;
7946         int rc;
7947
7948         if (tc > bp->max_tc) {
7949                 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
7950                            tc, bp->max_tc);
7951                 return -EINVAL;
7952         }
7953
7954         if (netdev_get_num_tc(dev) == tc)
7955                 return 0;
7956
7957         if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7958                 sh = true;
7959
7960         rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7961                               sh, tc, bp->tx_nr_rings_xdp);
7962         if (rc)
7963                 return rc;
7964
7965         /* Needs to close the device and do hw resource re-allocations */
7966         if (netif_running(bp->dev))
7967                 bnxt_close_nic(bp, true, false);
7968
7969         if (tc) {
7970                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7971                 netdev_set_num_tc(dev, tc);
7972         } else {
7973                 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7974                 netdev_reset_tc(dev);
7975         }
7976         bp->tx_nr_rings += bp->tx_nr_rings_xdp;
7977         bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7978                                bp->tx_nr_rings + bp->rx_nr_rings;
7979         bp->num_stat_ctxs = bp->cp_nr_rings;
7980
7981         if (netif_running(bp->dev))
7982                 return bnxt_open_nic(bp, true, false);
7983
7984         return 0;
7985 }
7986
7987 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7988                                   void *cb_priv)
7989 {
7990         struct bnxt *bp = cb_priv;
7991
7992         if (!bnxt_tc_flower_enabled(bp) ||
7993             !tc_cls_can_offload_and_chain0(bp->dev, type_data))
7994                 return -EOPNOTSUPP;
7995
7996         switch (type) {
7997         case TC_SETUP_CLSFLOWER:
7998                 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
7999         default:
8000                 return -EOPNOTSUPP;
8001         }
8002 }
8003
8004 static int bnxt_setup_tc_block(struct net_device *dev,
8005                                struct tc_block_offload *f)
8006 {
8007         struct bnxt *bp = netdev_priv(dev);
8008
8009         if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8010                 return -EOPNOTSUPP;
8011
8012         switch (f->command) {
8013         case TC_BLOCK_BIND:
8014                 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
8015                                              bp, bp, f->extack);
8016         case TC_BLOCK_UNBIND:
8017                 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
8018                 return 0;
8019         default:
8020                 return -EOPNOTSUPP;
8021         }
8022 }
8023
8024 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
8025                          void *type_data)
8026 {
8027         switch (type) {
8028         case TC_SETUP_BLOCK:
8029                 return bnxt_setup_tc_block(dev, type_data);
8030         case TC_SETUP_QDISC_MQPRIO: {
8031                 struct tc_mqprio_qopt *mqprio = type_data;
8032
8033                 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
8034
8035                 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
8036         }
8037         default:
8038                 return -EOPNOTSUPP;
8039         }
8040 }
8041
8042 #ifdef CONFIG_RFS_ACCEL
8043 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
8044                             struct bnxt_ntuple_filter *f2)
8045 {
8046         struct flow_keys *keys1 = &f1->fkeys;
8047         struct flow_keys *keys2 = &f2->fkeys;
8048
8049         if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
8050             keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
8051             keys1->ports.ports == keys2->ports.ports &&
8052             keys1->basic.ip_proto == keys2->basic.ip_proto &&
8053             keys1->basic.n_proto == keys2->basic.n_proto &&
8054             keys1->control.flags == keys2->control.flags &&
8055             ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
8056             ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
8057                 return true;
8058
8059         return false;
8060 }
8061
8062 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
8063                               u16 rxq_index, u32 flow_id)
8064 {
8065         struct bnxt *bp = netdev_priv(dev);
8066         struct bnxt_ntuple_filter *fltr, *new_fltr;
8067         struct flow_keys *fkeys;
8068         struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
8069         int rc = 0, idx, bit_id, l2_idx = 0;
8070         struct hlist_head *head;
8071
8072         if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
8073                 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8074                 int off = 0, j;
8075
8076                 netif_addr_lock_bh(dev);
8077                 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
8078                         if (ether_addr_equal(eth->h_dest,
8079                                              vnic->uc_list + off)) {
8080                                 l2_idx = j + 1;
8081                                 break;
8082                         }
8083                 }
8084                 netif_addr_unlock_bh(dev);
8085                 if (!l2_idx)
8086                         return -EINVAL;
8087         }
8088         new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
8089         if (!new_fltr)
8090                 return -ENOMEM;
8091
8092         fkeys = &new_fltr->fkeys;
8093         if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
8094                 rc = -EPROTONOSUPPORT;
8095                 goto err_free;
8096         }
8097
8098         if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
8099              fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
8100             ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
8101              (fkeys->basic.ip_proto != IPPROTO_UDP))) {
8102                 rc = -EPROTONOSUPPORT;
8103                 goto err_free;
8104         }
8105         if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
8106             bp->hwrm_spec_code < 0x10601) {
8107                 rc = -EPROTONOSUPPORT;
8108                 goto err_free;
8109         }
8110         if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
8111             bp->hwrm_spec_code < 0x10601) {
8112                 rc = -EPROTONOSUPPORT;
8113                 goto err_free;
8114         }
8115
8116         memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
8117         memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
8118
8119         idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
8120         head = &bp->ntp_fltr_hash_tbl[idx];
8121         rcu_read_lock();
8122         hlist_for_each_entry_rcu(fltr, head, hash) {
8123                 if (bnxt_fltr_match(fltr, new_fltr)) {
8124                         rcu_read_unlock();
8125                         rc = 0;
8126                         goto err_free;
8127                 }
8128         }
8129         rcu_read_unlock();
8130
8131         spin_lock_bh(&bp->ntp_fltr_lock);
8132         bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
8133                                          BNXT_NTP_FLTR_MAX_FLTR, 0);
8134         if (bit_id < 0) {
8135                 spin_unlock_bh(&bp->ntp_fltr_lock);
8136                 rc = -ENOMEM;
8137                 goto err_free;
8138         }
8139
8140         new_fltr->sw_id = (u16)bit_id;
8141         new_fltr->flow_id = flow_id;
8142         new_fltr->l2_fltr_idx = l2_idx;
8143         new_fltr->rxq = rxq_index;
8144         hlist_add_head_rcu(&new_fltr->hash, head);
8145         bp->ntp_fltr_count++;
8146         spin_unlock_bh(&bp->ntp_fltr_lock);
8147
8148         set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
8149         bnxt_queue_sp_work(bp);
8150
8151         return new_fltr->sw_id;
8152
8153 err_free:
8154         kfree(new_fltr);
8155         return rc;
8156 }
8157
8158 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8159 {
8160         int i;
8161
8162         for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
8163                 struct hlist_head *head;
8164                 struct hlist_node *tmp;
8165                 struct bnxt_ntuple_filter *fltr;
8166                 int rc;
8167
8168                 head = &bp->ntp_fltr_hash_tbl[i];
8169                 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
8170                         bool del = false;
8171
8172                         if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
8173                                 if (rps_may_expire_flow(bp->dev, fltr->rxq,
8174                                                         fltr->flow_id,
8175                                                         fltr->sw_id)) {
8176                                         bnxt_hwrm_cfa_ntuple_filter_free(bp,
8177                                                                          fltr);
8178                                         del = true;
8179                                 }
8180                         } else {
8181                                 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
8182                                                                        fltr);
8183                                 if (rc)
8184                                         del = true;
8185                                 else
8186                                         set_bit(BNXT_FLTR_VALID, &fltr->state);
8187                         }
8188
8189                         if (del) {
8190                                 spin_lock_bh(&bp->ntp_fltr_lock);
8191                                 hlist_del_rcu(&fltr->hash);
8192                                 bp->ntp_fltr_count--;
8193                                 spin_unlock_bh(&bp->ntp_fltr_lock);
8194                                 synchronize_rcu();
8195                                 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
8196                                 kfree(fltr);
8197                         }
8198                 }
8199         }
8200         if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
8201                 netdev_info(bp->dev, "Receive PF driver unload event!");
8202 }
8203
8204 #else
8205
8206 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8207 {
8208 }
8209
8210 #endif /* CONFIG_RFS_ACCEL */
8211
8212 static void bnxt_udp_tunnel_add(struct net_device *dev,
8213                                 struct udp_tunnel_info *ti)
8214 {
8215         struct bnxt *bp = netdev_priv(dev);
8216
8217         if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8218                 return;
8219
8220         if (!netif_running(dev))
8221                 return;
8222
8223         switch (ti->type) {
8224         case UDP_TUNNEL_TYPE_VXLAN:
8225                 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
8226                         return;
8227
8228                 bp->vxlan_port_cnt++;
8229                 if (bp->vxlan_port_cnt == 1) {
8230                         bp->vxlan_port = ti->port;
8231                         set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
8232                         bnxt_queue_sp_work(bp);
8233                 }
8234                 break;
8235         case UDP_TUNNEL_TYPE_GENEVE:
8236                 if (bp->nge_port_cnt && bp->nge_port != ti->port)
8237                         return;
8238
8239                 bp->nge_port_cnt++;
8240                 if (bp->nge_port_cnt == 1) {
8241                         bp->nge_port = ti->port;
8242                         set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
8243                 }
8244                 break;
8245         default:
8246                 return;
8247         }
8248
8249         bnxt_queue_sp_work(bp);
8250 }
8251
8252 static void bnxt_udp_tunnel_del(struct net_device *dev,
8253                                 struct udp_tunnel_info *ti)
8254 {
8255         struct bnxt *bp = netdev_priv(dev);
8256
8257         if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8258                 return;
8259
8260         if (!netif_running(dev))
8261                 return;
8262
8263         switch (ti->type) {
8264         case UDP_TUNNEL_TYPE_VXLAN:
8265                 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
8266                         return;
8267                 bp->vxlan_port_cnt--;
8268
8269                 if (bp->vxlan_port_cnt != 0)
8270                         return;
8271
8272                 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
8273                 break;
8274         case UDP_TUNNEL_TYPE_GENEVE:
8275                 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
8276                         return;
8277                 bp->nge_port_cnt--;
8278
8279                 if (bp->nge_port_cnt != 0)
8280                         return;
8281
8282                 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
8283                 break;
8284         default:
8285                 return;
8286         }
8287
8288         bnxt_queue_sp_work(bp);
8289 }
8290
8291 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8292                                struct net_device *dev, u32 filter_mask,
8293                                int nlflags)
8294 {
8295         struct bnxt *bp = netdev_priv(dev);
8296
8297         return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
8298                                        nlflags, filter_mask, NULL);
8299 }
8300
8301 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8302                                u16 flags)
8303 {
8304         struct bnxt *bp = netdev_priv(dev);
8305         struct nlattr *attr, *br_spec;
8306         int rem, rc = 0;
8307
8308         if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
8309                 return -EOPNOTSUPP;
8310
8311         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8312         if (!br_spec)
8313                 return -EINVAL;
8314
8315         nla_for_each_nested(attr, br_spec, rem) {
8316                 u16 mode;
8317
8318                 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8319                         continue;
8320
8321                 if (nla_len(attr) < sizeof(mode))
8322                         return -EINVAL;
8323
8324                 mode = nla_get_u16(attr);
8325                 if (mode == bp->br_mode)
8326                         break;
8327
8328                 rc = bnxt_hwrm_set_br_mode(bp, mode);
8329                 if (!rc)
8330                         bp->br_mode = mode;
8331                 break;
8332         }
8333         return rc;
8334 }
8335
8336 static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
8337                                    size_t len)
8338 {
8339         struct bnxt *bp = netdev_priv(dev);
8340         int rc;
8341
8342         /* The PF and it's VF-reps only support the switchdev framework */
8343         if (!BNXT_PF(bp))
8344                 return -EOPNOTSUPP;
8345
8346         rc = snprintf(buf, len, "p%d", bp->pf.port_id);
8347
8348         if (rc >= len)
8349                 return -EOPNOTSUPP;
8350         return 0;
8351 }
8352
8353 int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
8354 {
8355         if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
8356                 return -EOPNOTSUPP;
8357
8358         /* The PF and it's VF-reps only support the switchdev framework */
8359         if (!BNXT_PF(bp))
8360                 return -EOPNOTSUPP;
8361
8362         switch (attr->id) {
8363         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
8364                 attr->u.ppid.id_len = sizeof(bp->switch_id);
8365                 memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
8366                 break;
8367         default:
8368                 return -EOPNOTSUPP;
8369         }
8370         return 0;
8371 }
8372
8373 static int bnxt_swdev_port_attr_get(struct net_device *dev,
8374                                     struct switchdev_attr *attr)
8375 {
8376         return bnxt_port_attr_get(netdev_priv(dev), attr);
8377 }
8378
8379 static const struct switchdev_ops bnxt_switchdev_ops = {
8380         .switchdev_port_attr_get        = bnxt_swdev_port_attr_get
8381 };
8382
8383 static const struct net_device_ops bnxt_netdev_ops = {
8384         .ndo_open               = bnxt_open,
8385         .ndo_start_xmit         = bnxt_start_xmit,
8386         .ndo_stop               = bnxt_close,
8387         .ndo_get_stats64        = bnxt_get_stats64,
8388         .ndo_set_rx_mode        = bnxt_set_rx_mode,
8389         .ndo_do_ioctl           = bnxt_ioctl,
8390         .ndo_validate_addr      = eth_validate_addr,
8391         .ndo_set_mac_address    = bnxt_change_mac_addr,
8392         .ndo_change_mtu         = bnxt_change_mtu,
8393         .ndo_fix_features       = bnxt_fix_features,
8394         .ndo_set_features       = bnxt_set_features,
8395         .ndo_tx_timeout         = bnxt_tx_timeout,
8396 #ifdef CONFIG_BNXT_SRIOV
8397         .ndo_get_vf_config      = bnxt_get_vf_config,
8398         .ndo_set_vf_mac         = bnxt_set_vf_mac,
8399         .ndo_set_vf_vlan        = bnxt_set_vf_vlan,
8400         .ndo_set_vf_rate        = bnxt_set_vf_bw,
8401         .ndo_set_vf_link_state  = bnxt_set_vf_link_state,
8402         .ndo_set_vf_spoofchk    = bnxt_set_vf_spoofchk,
8403         .ndo_set_vf_trust       = bnxt_set_vf_trust,
8404 #endif
8405 #ifdef CONFIG_NET_POLL_CONTROLLER
8406         .ndo_poll_controller    = bnxt_poll_controller,
8407 #endif
8408         .ndo_setup_tc           = bnxt_setup_tc,
8409 #ifdef CONFIG_RFS_ACCEL
8410         .ndo_rx_flow_steer      = bnxt_rx_flow_steer,
8411 #endif
8412         .ndo_udp_tunnel_add     = bnxt_udp_tunnel_add,
8413         .ndo_udp_tunnel_del     = bnxt_udp_tunnel_del,
8414         .ndo_bpf                = bnxt_xdp,
8415         .ndo_bridge_getlink     = bnxt_bridge_getlink,
8416         .ndo_bridge_setlink     = bnxt_bridge_setlink,
8417         .ndo_get_phys_port_name = bnxt_get_phys_port_name
8418 };
8419
8420 static void bnxt_remove_one(struct pci_dev *pdev)
8421 {
8422         struct net_device *dev = pci_get_drvdata(pdev);
8423         struct bnxt *bp = netdev_priv(dev);
8424
8425         if (BNXT_PF(bp)) {
8426                 bnxt_sriov_disable(bp);
8427                 bnxt_dl_unregister(bp);
8428         }
8429
8430         pci_disable_pcie_error_reporting(pdev);
8431         unregister_netdev(dev);
8432         bnxt_shutdown_tc(bp);
8433         bnxt_cancel_sp_work(bp);
8434         bp->sp_event = 0;
8435
8436         bnxt_clear_int_mode(bp);
8437         bnxt_hwrm_func_drv_unrgtr(bp);
8438         bnxt_free_hwrm_resources(bp);
8439         bnxt_free_hwrm_short_cmd_req(bp);
8440         bnxt_ethtool_free(bp);
8441         bnxt_dcb_free(bp);
8442         kfree(bp->edev);
8443         bp->edev = NULL;
8444         bnxt_cleanup_pci(bp);
8445         free_netdev(dev);
8446 }
8447
8448 static int bnxt_probe_phy(struct bnxt *bp)
8449 {
8450         int rc = 0;
8451         struct bnxt_link_info *link_info = &bp->link_info;
8452
8453         rc = bnxt_hwrm_phy_qcaps(bp);
8454         if (rc) {
8455                 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
8456                            rc);
8457                 return rc;
8458         }
8459         mutex_init(&bp->link_lock);
8460
8461         rc = bnxt_update_link(bp, false);
8462         if (rc) {
8463                 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
8464                            rc);
8465                 return rc;
8466         }
8467
8468         /* Older firmware does not have supported_auto_speeds, so assume
8469          * that all supported speeds can be autonegotiated.
8470          */
8471         if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
8472                 link_info->support_auto_speeds = link_info->support_speeds;
8473
8474         /*initialize the ethool setting copy with NVM settings */
8475         if (BNXT_AUTO_MODE(link_info->auto_mode)) {
8476                 link_info->autoneg = BNXT_AUTONEG_SPEED;
8477                 if (bp->hwrm_spec_code >= 0x10201) {
8478                         if (link_info->auto_pause_setting &
8479                             PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
8480                                 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8481                 } else {
8482                         link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8483                 }
8484                 link_info->advertising = link_info->auto_link_speeds;
8485         } else {
8486                 link_info->req_link_speed = link_info->force_link_speed;
8487                 link_info->req_duplex = link_info->duplex_setting;
8488         }
8489         if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
8490                 link_info->req_flow_ctrl =
8491                         link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
8492         else
8493                 link_info->req_flow_ctrl = link_info->force_pause_setting;
8494         return rc;
8495 }
8496
8497 static int bnxt_get_max_irq(struct pci_dev *pdev)
8498 {
8499         u16 ctrl;
8500
8501         if (!pdev->msix_cap)
8502                 return 1;
8503
8504         pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
8505         return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
8506 }
8507
8508 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8509                                 int *max_cp)
8510 {
8511         struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8512         int max_ring_grps = 0;
8513
8514         *max_tx = hw_resc->max_tx_rings;
8515         *max_rx = hw_resc->max_rx_rings;
8516         *max_cp = min_t(int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8517         *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
8518         max_ring_grps = hw_resc->max_hw_ring_grps;
8519         if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
8520                 *max_cp -= 1;
8521                 *max_rx -= 2;
8522         }
8523         if (bp->flags & BNXT_FLAG_AGG_RINGS)
8524                 *max_rx >>= 1;
8525         *max_rx = min_t(int, *max_rx, max_ring_grps);
8526 }
8527
8528 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
8529 {
8530         int rx, tx, cp;
8531
8532         _bnxt_get_max_rings(bp, &rx, &tx, &cp);
8533         *max_rx = rx;
8534         *max_tx = tx;
8535         if (!rx || !tx || !cp)
8536                 return -ENOMEM;
8537
8538         return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
8539 }
8540
8541 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8542                                bool shared)
8543 {
8544         int rc;
8545
8546         rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8547         if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
8548                 /* Not enough rings, try disabling agg rings. */
8549                 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8550                 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8551                 if (rc) {
8552                         /* set BNXT_FLAG_AGG_RINGS back for consistency */
8553                         bp->flags |= BNXT_FLAG_AGG_RINGS;
8554                         return rc;
8555                 }
8556                 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8557                 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8558                 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8559                 bnxt_set_ring_params(bp);
8560         }
8561
8562         if (bp->flags & BNXT_FLAG_ROCE_CAP) {
8563                 int max_cp, max_stat, max_irq;
8564
8565                 /* Reserve minimum resources for RoCE */
8566                 max_cp = bnxt_get_max_func_cp_rings(bp);
8567                 max_stat = bnxt_get_max_func_stat_ctxs(bp);
8568                 max_irq = bnxt_get_max_func_irqs(bp);
8569                 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
8570                     max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
8571                     max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
8572                         return 0;
8573
8574                 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
8575                 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
8576                 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
8577                 max_cp = min_t(int, max_cp, max_irq);
8578                 max_cp = min_t(int, max_cp, max_stat);
8579                 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
8580                 if (rc)
8581                         rc = 0;
8582         }
8583         return rc;
8584 }
8585
8586 /* In initial default shared ring setting, each shared ring must have a
8587  * RX/TX ring pair.
8588  */
8589 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
8590 {
8591         bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
8592         bp->rx_nr_rings = bp->cp_nr_rings;
8593         bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
8594         bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8595 }
8596
8597 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
8598 {
8599         int dflt_rings, max_rx_rings, max_tx_rings, rc;
8600
8601         if (!bnxt_can_reserve_rings(bp))
8602                 return 0;
8603
8604         if (sh)
8605                 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8606         dflt_rings = netif_get_num_default_rss_queues();
8607         /* Reduce default rings on multi-port cards so that total default
8608          * rings do not exceed CPU count.
8609          */
8610         if (bp->port_count > 1) {
8611                 int max_rings =
8612                         max_t(int, num_online_cpus() / bp->port_count, 1);
8613
8614                 dflt_rings = min_t(int, dflt_rings, max_rings);
8615         }
8616         rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
8617         if (rc)
8618                 return rc;
8619         bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
8620         bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
8621         if (sh)
8622                 bnxt_trim_dflt_sh_rings(bp);
8623         else
8624                 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
8625         bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8626
8627         rc = __bnxt_reserve_rings(bp);
8628         if (rc)
8629                 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
8630         bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8631         if (sh)
8632                 bnxt_trim_dflt_sh_rings(bp);
8633
8634         /* Rings may have been trimmed, re-reserve the trimmed rings. */
8635         if (bnxt_need_reserve_rings(bp)) {
8636                 rc = __bnxt_reserve_rings(bp);
8637                 if (rc)
8638                         netdev_warn(bp->dev, "2nd rings reservation failed.\n");
8639                 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8640         }
8641         bp->num_stat_ctxs = bp->cp_nr_rings;
8642         if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8643                 bp->rx_nr_rings++;
8644                 bp->cp_nr_rings++;
8645         }
8646         return rc;
8647 }
8648
8649 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
8650 {
8651         int rc;
8652
8653         if (bp->tx_nr_rings)
8654                 return 0;
8655
8656         rc = bnxt_set_dflt_rings(bp, true);
8657         if (rc) {
8658                 netdev_err(bp->dev, "Not enough rings available.\n");
8659                 return rc;
8660         }
8661         rc = bnxt_init_int_mode(bp);
8662         if (rc)
8663                 return rc;
8664         bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8665         if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
8666                 bp->flags |= BNXT_FLAG_RFS;
8667                 bp->dev->features |= NETIF_F_NTUPLE;
8668         }
8669         return 0;
8670 }
8671
8672 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
8673 {
8674         int rc;
8675
8676         ASSERT_RTNL();
8677         bnxt_hwrm_func_qcaps(bp);
8678
8679         if (netif_running(bp->dev))
8680                 __bnxt_close_nic(bp, true, false);
8681
8682         bnxt_ulp_irq_stop(bp);
8683         bnxt_clear_int_mode(bp);
8684         rc = bnxt_init_int_mode(bp);
8685         bnxt_ulp_irq_restart(bp, rc);
8686
8687         if (netif_running(bp->dev)) {
8688                 if (rc)
8689                         dev_close(bp->dev);
8690                 else
8691                         rc = bnxt_open_nic(bp, true, false);
8692         }
8693
8694         return rc;
8695 }
8696
8697 static int bnxt_init_mac_addr(struct bnxt *bp)
8698 {
8699         int rc = 0;
8700
8701         if (BNXT_PF(bp)) {
8702                 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
8703         } else {
8704 #ifdef CONFIG_BNXT_SRIOV
8705                 struct bnxt_vf_info *vf = &bp->vf;
8706
8707                 if (is_valid_ether_addr(vf->mac_addr)) {
8708                         /* overwrite netdev dev_addr with admin VF MAC */
8709                         memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
8710                 } else {
8711                         eth_hw_addr_random(bp->dev);
8712                 }
8713                 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
8714 #endif
8715         }
8716         return rc;
8717 }
8718
8719 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8720 {
8721         static int version_printed;
8722         struct net_device *dev;
8723         struct bnxt *bp;
8724         int rc, max_irqs;
8725
8726         if (pci_is_bridge(pdev))
8727                 return -ENODEV;
8728
8729         if (version_printed++ == 0)
8730                 pr_info("%s", version);
8731
8732         max_irqs = bnxt_get_max_irq(pdev);
8733         dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
8734         if (!dev)
8735                 return -ENOMEM;
8736
8737         bp = netdev_priv(dev);
8738
8739         if (bnxt_vf_pciid(ent->driver_data))
8740                 bp->flags |= BNXT_FLAG_VF;
8741
8742         if (pdev->msix_cap)
8743                 bp->flags |= BNXT_FLAG_MSIX_CAP;
8744
8745         rc = bnxt_init_board(pdev, dev);
8746         if (rc < 0)
8747                 goto init_err_free;
8748
8749         dev->netdev_ops = &bnxt_netdev_ops;
8750         dev->watchdog_timeo = BNXT_TX_TIMEOUT;
8751         dev->ethtool_ops = &bnxt_ethtool_ops;
8752         SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
8753         pci_set_drvdata(pdev, dev);
8754
8755         rc = bnxt_alloc_hwrm_resources(bp);
8756         if (rc)
8757                 goto init_err_pci_clean;
8758
8759         mutex_init(&bp->hwrm_cmd_lock);
8760         rc = bnxt_hwrm_ver_get(bp);
8761         if (rc)
8762                 goto init_err_pci_clean;
8763
8764         if (bp->flags & BNXT_FLAG_SHORT_CMD) {
8765                 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
8766                 if (rc)
8767                         goto init_err_pci_clean;
8768         }
8769
8770         rc = bnxt_hwrm_func_reset(bp);
8771         if (rc)
8772                 goto init_err_pci_clean;
8773
8774         bnxt_hwrm_fw_set_time(bp);
8775
8776         dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8777                            NETIF_F_TSO | NETIF_F_TSO6 |
8778                            NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
8779                            NETIF_F_GSO_IPXIP4 |
8780                            NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8781                            NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
8782                            NETIF_F_RXCSUM | NETIF_F_GRO;
8783
8784         if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8785                 dev->hw_features |= NETIF_F_LRO;
8786
8787         dev->hw_enc_features =
8788                         NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8789                         NETIF_F_TSO | NETIF_F_TSO6 |
8790                         NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
8791                         NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8792                         NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
8793         dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
8794                                     NETIF_F_GSO_GRE_CSUM;
8795         dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
8796         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
8797                             NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
8798         if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8799                 dev->hw_features |= NETIF_F_GRO_HW;
8800         dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
8801         if (dev->features & NETIF_F_GRO_HW)
8802                 dev->features &= ~NETIF_F_LRO;
8803         dev->priv_flags |= IFF_UNICAST_FLT;
8804
8805 #ifdef CONFIG_BNXT_SRIOV
8806         init_waitqueue_head(&bp->sriov_cfg_wait);
8807         mutex_init(&bp->sriov_lock);
8808 #endif
8809         bp->gro_func = bnxt_gro_func_5730x;
8810         if (BNXT_CHIP_P4_PLUS(bp))
8811                 bp->gro_func = bnxt_gro_func_5731x;
8812         else
8813                 bp->flags |= BNXT_FLAG_DOUBLE_DB;
8814
8815         rc = bnxt_hwrm_func_drv_rgtr(bp);
8816         if (rc)
8817                 goto init_err_pci_clean;
8818
8819         rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
8820         if (rc)
8821                 goto init_err_pci_clean;
8822
8823         bp->ulp_probe = bnxt_ulp_probe;
8824
8825         /* Get the MAX capabilities for this function */
8826         rc = bnxt_hwrm_func_qcaps(bp);
8827         if (rc) {
8828                 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
8829                            rc);
8830                 rc = -1;
8831                 goto init_err_pci_clean;
8832         }
8833         rc = bnxt_init_mac_addr(bp);
8834         if (rc) {
8835                 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
8836                 rc = -EADDRNOTAVAIL;
8837                 goto init_err_pci_clean;
8838         }
8839         rc = bnxt_hwrm_queue_qportcfg(bp);
8840         if (rc) {
8841                 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
8842                            rc);
8843                 rc = -1;
8844                 goto init_err_pci_clean;
8845         }
8846
8847         bnxt_hwrm_func_qcfg(bp);
8848         bnxt_hwrm_port_led_qcaps(bp);
8849         bnxt_ethtool_init(bp);
8850         bnxt_dcb_init(bp);
8851
8852         /* MTU range: 60 - FW defined max */
8853         dev->min_mtu = ETH_ZLEN;
8854         dev->max_mtu = bp->max_mtu;
8855
8856         rc = bnxt_probe_phy(bp);
8857         if (rc)
8858                 goto init_err_pci_clean;
8859
8860         bnxt_set_rx_skb_mode(bp, false);
8861         bnxt_set_tpa_flags(bp);
8862         bnxt_set_ring_params(bp);
8863         bnxt_set_max_func_irqs(bp, max_irqs);
8864         rc = bnxt_set_dflt_rings(bp, true);
8865         if (rc) {
8866                 netdev_err(bp->dev, "Not enough rings available.\n");
8867                 rc = -ENOMEM;
8868                 goto init_err_pci_clean;
8869         }
8870
8871         /* Default RSS hash cfg. */
8872         bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
8873                            VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
8874                            VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
8875                            VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
8876         if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
8877                 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8878                 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8879                                     VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8880         }
8881
8882         bnxt_hwrm_vnic_qcaps(bp);
8883         if (bnxt_rfs_supported(bp)) {
8884                 dev->hw_features |= NETIF_F_NTUPLE;
8885                 if (bnxt_rfs_capable(bp)) {
8886                         bp->flags |= BNXT_FLAG_RFS;
8887                         dev->features |= NETIF_F_NTUPLE;
8888                 }
8889         }
8890
8891         if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8892                 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8893
8894         rc = bnxt_init_int_mode(bp);
8895         if (rc)
8896                 goto init_err_pci_clean;
8897
8898         /* No TC has been set yet and rings may have been trimmed due to
8899          * limited MSIX, so we re-initialize the TX rings per TC.
8900          */
8901         bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8902
8903         bnxt_get_wol_settings(bp);
8904         if (bp->flags & BNXT_FLAG_WOL_CAP)
8905                 device_set_wakeup_enable(&pdev->dev, bp->wol);
8906         else
8907                 device_set_wakeup_capable(&pdev->dev, false);
8908
8909         bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
8910
8911         if (BNXT_PF(bp)) {
8912                 if (!bnxt_pf_wq) {
8913                         bnxt_pf_wq =
8914                                 create_singlethread_workqueue("bnxt_pf_wq");
8915                         if (!bnxt_pf_wq) {
8916                                 dev_err(&pdev->dev, "Unable to create workqueue.\n");
8917                                 goto init_err_pci_clean;
8918                         }
8919                 }
8920                 bnxt_init_tc(bp);
8921         }
8922
8923         rc = register_netdev(dev);
8924         if (rc)
8925                 goto init_err_cleanup_tc;
8926
8927         if (BNXT_PF(bp))
8928                 bnxt_dl_register(bp);
8929
8930         netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8931                     board_info[ent->driver_data].name,
8932                     (long)pci_resource_start(pdev, 0), dev->dev_addr);
8933         pcie_print_link_status(pdev);
8934
8935         return 0;
8936
8937 init_err_cleanup_tc:
8938         bnxt_shutdown_tc(bp);
8939         bnxt_clear_int_mode(bp);
8940
8941 init_err_pci_clean:
8942         bnxt_cleanup_pci(bp);
8943
8944 init_err_free:
8945         free_netdev(dev);
8946         return rc;
8947 }
8948
8949 static void bnxt_shutdown(struct pci_dev *pdev)
8950 {
8951         struct net_device *dev = pci_get_drvdata(pdev);
8952         struct bnxt *bp;
8953
8954         if (!dev)
8955                 return;
8956
8957         rtnl_lock();
8958         bp = netdev_priv(dev);
8959         if (!bp)
8960                 goto shutdown_exit;
8961
8962         if (netif_running(dev))
8963                 dev_close(dev);
8964
8965         bnxt_ulp_shutdown(bp);
8966
8967         if (system_state == SYSTEM_POWER_OFF) {
8968                 bnxt_clear_int_mode(bp);
8969                 pci_wake_from_d3(pdev, bp->wol);
8970                 pci_set_power_state(pdev, PCI_D3hot);
8971         }
8972
8973 shutdown_exit:
8974         rtnl_unlock();
8975 }
8976
8977 #ifdef CONFIG_PM_SLEEP
8978 static int bnxt_suspend(struct device *device)
8979 {
8980         struct pci_dev *pdev = to_pci_dev(device);
8981         struct net_device *dev = pci_get_drvdata(pdev);
8982         struct bnxt *bp = netdev_priv(dev);
8983         int rc = 0;
8984
8985         rtnl_lock();
8986         if (netif_running(dev)) {
8987                 netif_device_detach(dev);
8988                 rc = bnxt_close(dev);
8989         }
8990         bnxt_hwrm_func_drv_unrgtr(bp);
8991         rtnl_unlock();
8992         return rc;
8993 }
8994
8995 static int bnxt_resume(struct device *device)
8996 {
8997         struct pci_dev *pdev = to_pci_dev(device);
8998         struct net_device *dev = pci_get_drvdata(pdev);
8999         struct bnxt *bp = netdev_priv(dev);
9000         int rc = 0;
9001
9002         rtnl_lock();
9003         if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
9004                 rc = -ENODEV;
9005                 goto resume_exit;
9006         }
9007         rc = bnxt_hwrm_func_reset(bp);
9008         if (rc) {
9009                 rc = -EBUSY;
9010                 goto resume_exit;
9011         }
9012         bnxt_get_wol_settings(bp);
9013         if (netif_running(dev)) {
9014                 rc = bnxt_open(dev);
9015                 if (!rc)
9016                         netif_device_attach(dev);
9017         }
9018
9019 resume_exit:
9020         rtnl_unlock();
9021         return rc;
9022 }
9023
9024 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
9025 #define BNXT_PM_OPS (&bnxt_pm_ops)
9026
9027 #else
9028
9029 #define BNXT_PM_OPS NULL
9030
9031 #endif /* CONFIG_PM_SLEEP */
9032
9033 /**
9034  * bnxt_io_error_detected - called when PCI error is detected
9035  * @pdev: Pointer to PCI device
9036  * @state: The current pci connection state
9037  *
9038  * This function is called after a PCI bus error affecting
9039  * this device has been detected.
9040  */
9041 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
9042                                                pci_channel_state_t state)
9043 {
9044         struct net_device *netdev = pci_get_drvdata(pdev);
9045         struct bnxt *bp = netdev_priv(netdev);
9046
9047         netdev_info(netdev, "PCI I/O error detected\n");
9048
9049         rtnl_lock();
9050         netif_device_detach(netdev);
9051
9052         bnxt_ulp_stop(bp);
9053
9054         if (state == pci_channel_io_perm_failure) {
9055                 rtnl_unlock();
9056                 return PCI_ERS_RESULT_DISCONNECT;
9057         }
9058
9059         if (netif_running(netdev))
9060                 bnxt_close(netdev);
9061
9062         pci_disable_device(pdev);
9063         rtnl_unlock();
9064
9065         /* Request a slot slot reset. */
9066         return PCI_ERS_RESULT_NEED_RESET;
9067 }
9068
9069 /**
9070  * bnxt_io_slot_reset - called after the pci bus has been reset.
9071  * @pdev: Pointer to PCI device
9072  *
9073  * Restart the card from scratch, as if from a cold-boot.
9074  * At this point, the card has exprienced a hard reset,
9075  * followed by fixups by BIOS, and has its config space
9076  * set up identically to what it was at cold boot.
9077  */
9078 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
9079 {
9080         struct net_device *netdev = pci_get_drvdata(pdev);
9081         struct bnxt *bp = netdev_priv(netdev);
9082         int err = 0;
9083         pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
9084
9085         netdev_info(bp->dev, "PCI Slot Reset\n");
9086
9087         rtnl_lock();
9088
9089         if (pci_enable_device(pdev)) {
9090                 dev_err(&pdev->dev,
9091                         "Cannot re-enable PCI device after reset.\n");
9092         } else {
9093                 pci_set_master(pdev);
9094
9095                 err = bnxt_hwrm_func_reset(bp);
9096                 if (!err && netif_running(netdev))
9097                         err = bnxt_open(netdev);
9098
9099                 if (!err) {
9100                         result = PCI_ERS_RESULT_RECOVERED;
9101                         bnxt_ulp_start(bp);
9102                 }
9103         }
9104
9105         if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
9106                 dev_close(netdev);
9107
9108         rtnl_unlock();
9109
9110         err = pci_cleanup_aer_uncorrect_error_status(pdev);
9111         if (err) {
9112                 dev_err(&pdev->dev,
9113                         "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
9114                          err); /* non-fatal, continue */
9115         }
9116
9117         return PCI_ERS_RESULT_RECOVERED;
9118 }
9119
9120 /**
9121  * bnxt_io_resume - called when traffic can start flowing again.
9122  * @pdev: Pointer to PCI device
9123  *
9124  * This callback is called when the error recovery driver tells
9125  * us that its OK to resume normal operation.
9126  */
9127 static void bnxt_io_resume(struct pci_dev *pdev)
9128 {
9129         struct net_device *netdev = pci_get_drvdata(pdev);
9130
9131         rtnl_lock();
9132
9133         netif_device_attach(netdev);
9134
9135         rtnl_unlock();
9136 }
9137
9138 static const struct pci_error_handlers bnxt_err_handler = {
9139         .error_detected = bnxt_io_error_detected,
9140         .slot_reset     = bnxt_io_slot_reset,
9141         .resume         = bnxt_io_resume
9142 };
9143
9144 static struct pci_driver bnxt_pci_driver = {
9145         .name           = DRV_MODULE_NAME,
9146         .id_table       = bnxt_pci_tbl,
9147         .probe          = bnxt_init_one,
9148         .remove         = bnxt_remove_one,
9149         .shutdown       = bnxt_shutdown,
9150         .driver.pm      = BNXT_PM_OPS,
9151         .err_handler    = &bnxt_err_handler,
9152 #if defined(CONFIG_BNXT_SRIOV)
9153         .sriov_configure = bnxt_sriov_configure,
9154 #endif
9155 };
9156
9157 static int __init bnxt_init(void)
9158 {
9159         bnxt_debug_init();
9160         return pci_register_driver(&bnxt_pci_driver);
9161 }
9162
9163 static void __exit bnxt_exit(void)
9164 {
9165         pci_unregister_driver(&bnxt_pci_driver);
9166         if (bnxt_pf_wq)
9167                 destroy_workqueue(bnxt_pf_wq);
9168         bnxt_debug_exit();
9169 }
9170
9171 module_init(bnxt_init);
9172 module_exit(bnxt_exit);