blob: e7c8539cbddf6704720a4a11f5e09aebec066066 [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanbac9a7e2017-02-12 19:18:10 -05004 * Copyright (c) 2016-2017 Broadcom Limited
Michael Chanc0c050c2015-10-22 16:01:17 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12
13#include <linux/stringify.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/errno.h>
17#include <linux/ioport.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/interrupt.h>
21#include <linux/pci.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/skbuff.h>
25#include <linux/dma-mapping.h>
26#include <linux/bitops.h>
27#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/delay.h>
30#include <asm/byteorder.h>
31#include <asm/page.h>
32#include <linux/time.h>
33#include <linux/mii.h>
34#include <linux/if.h>
35#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040036#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050037#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040038#include <net/ip.h>
39#include <net/tcp.h>
40#include <net/udp.h>
41#include <net/checksum.h>
42#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070043#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040044#include <linux/workqueue.h>
45#include <linux/prefetch.h>
46#include <linux/cache.h>
47#include <linux/log2.h>
48#include <linux/aer.h>
49#include <linux/bitmap.h>
50#include <linux/cpu_rmap.h>
51
52#include "bnxt_hsi.h"
53#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050054#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040055#include "bnxt_sriov.h"
56#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050057#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050058#include "bnxt_xdp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040059
60#define BNXT_TX_TIMEOUT (5 * HZ)
61
62static const char version[] =
63 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
64
65MODULE_LICENSE("GPL");
66MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
67MODULE_VERSION(DRV_MODULE_VERSION);
68
69#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
70#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
71#define BNXT_RX_COPY_THRESH 256
72
Michael Chan4419dbe2016-02-10 17:33:49 -050073#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040074
75enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050076 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040077 BCM57302,
78 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040079 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040080 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040081 BCM57311,
82 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050083 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040084 BCM57404,
85 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040086 BCM57402_NPAR,
87 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040088 BCM57412,
89 BCM57414,
90 BCM57416,
91 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040093 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040094 BCM57417_SFP,
95 BCM57416_SFP,
96 BCM57404_NPAR,
97 BCM57406_NPAR,
98 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040099 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400100 BCM57414_NPAR,
101 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500102 BCM57452,
103 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400104 NETXTREME_E_VF,
105 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400106};
107
108/* indexed by enum above */
109static const struct {
110 char *name;
111} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400112 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400115 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400116 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
117 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
118 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
119 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400122 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400123 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
124 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
125 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
127 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400128 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
130 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
131 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400132 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400134 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
135 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400136 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
137 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500138 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400140 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
141 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400142};
143
144static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400145 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
148 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400150 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
152 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500153 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400154 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
155 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400156 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
157 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400158 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
159 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
160 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
161 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400162 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400163 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
165 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
166 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
167 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400172 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500175 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
176 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400177#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400178 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
179 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400180 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
182 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400186#endif
187 { 0 }
188};
189
190MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
191
192static const u16 bnxt_vf_req_snif[] = {
193 HWRM_FUNC_CFG,
194 HWRM_PORT_PHY_QCFG,
195 HWRM_CFA_L2_FILTER_ALLOC,
196};
197
Michael Chan25be8622016-04-05 14:09:00 -0400198static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500199 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
200 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
201 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
202 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
203 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400204};
205
Michael Chanc0c050c2015-10-22 16:01:17 -0400206static bool bnxt_vf_pciid(enum board_idx idx)
207{
Michael Chanadbc8302016-09-19 03:58:01 -0400208 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400209}
210
211#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
212#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
213#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
214
215#define BNXT_CP_DB_REARM(db, raw_cons) \
216 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
217
218#define BNXT_CP_DB(db, raw_cons) \
219 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
220
221#define BNXT_CP_DB_IRQ_DIS(db) \
222 writel(DB_CP_IRQ_DIS_FLAGS, db)
223
Michael Chan38413402017-02-06 16:55:43 -0500224const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400225 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
226 TX_BD_FLAGS_LHINT_512_TO_1023,
227 TX_BD_FLAGS_LHINT_1024_TO_2047,
228 TX_BD_FLAGS_LHINT_1024_TO_2047,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
244};
245
246static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
247{
248 struct bnxt *bp = netdev_priv(dev);
249 struct tx_bd *txbd;
250 struct tx_bd_ext *txbd1;
251 struct netdev_queue *txq;
252 int i;
253 dma_addr_t mapping;
254 unsigned int length, pad = 0;
255 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
256 u16 prod, last_frag;
257 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400258 struct bnxt_tx_ring_info *txr;
259 struct bnxt_sw_tx_bd *tx_buf;
260
261 i = skb_get_queue_mapping(skb);
262 if (unlikely(i >= bp->tx_nr_rings)) {
263 dev_kfree_skb_any(skb);
264 return NETDEV_TX_OK;
265 }
266
Michael Chanc0c050c2015-10-22 16:01:17 -0400267 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500268 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400269 prod = txr->tx_prod;
270
271 free_size = bnxt_tx_avail(bp, txr);
272 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
273 netif_tx_stop_queue(txq);
274 return NETDEV_TX_BUSY;
275 }
276
277 length = skb->len;
278 len = skb_headlen(skb);
279 last_frag = skb_shinfo(skb)->nr_frags;
280
281 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
282
283 txbd->tx_bd_opaque = prod;
284
285 tx_buf = &txr->tx_buf_ring[prod];
286 tx_buf->skb = skb;
287 tx_buf->nr_frags = last_frag;
288
289 vlan_tag_flags = 0;
290 cfa_action = 0;
291 if (skb_vlan_tag_present(skb)) {
292 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
293 skb_vlan_tag_get(skb);
294 /* Currently supports 8021Q, 8021AD vlan offloads
295 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
296 */
297 if (skb->vlan_proto == htons(ETH_P_8021Q))
298 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
299 }
300
301 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500302 struct tx_push_buffer *tx_push_buf = txr->tx_push;
303 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
304 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
305 void *pdata = tx_push_buf->data;
306 u64 *end;
307 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400308
309 /* Set COAL_NOW to be ready quickly for the next push */
310 tx_push->tx_bd_len_flags_type =
311 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
312 TX_BD_TYPE_LONG_TX_BD |
313 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
314 TX_BD_FLAGS_COAL_NOW |
315 TX_BD_FLAGS_PACKET_END |
316 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
317
318 if (skb->ip_summed == CHECKSUM_PARTIAL)
319 tx_push1->tx_bd_hsize_lflags =
320 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
321 else
322 tx_push1->tx_bd_hsize_lflags = 0;
323
324 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
325 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
326
Michael Chanfbb0fa82016-02-22 02:10:26 -0500327 end = pdata + length;
328 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500329 *end = 0;
330
Michael Chanc0c050c2015-10-22 16:01:17 -0400331 skb_copy_from_linear_data(skb, pdata, len);
332 pdata += len;
333 for (j = 0; j < last_frag; j++) {
334 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
335 void *fptr;
336
337 fptr = skb_frag_address_safe(frag);
338 if (!fptr)
339 goto normal_tx;
340
341 memcpy(pdata, fptr, skb_frag_size(frag));
342 pdata += skb_frag_size(frag);
343 }
344
Michael Chan4419dbe2016-02-10 17:33:49 -0500345 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
346 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400347 prod = NEXT_TX(prod);
348 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
349 memcpy(txbd, tx_push1, sizeof(*txbd));
350 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500351 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400352 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
353 txr->tx_prod = prod;
354
Michael Chanb9a84602016-06-06 02:37:14 -0400355 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400356 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400357 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400358
Michael Chan4419dbe2016-02-10 17:33:49 -0500359 push_len = (length + sizeof(*tx_push) + 7) / 8;
360 if (push_len > 16) {
361 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400362 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
363 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500364 } else {
365 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
366 push_len);
367 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400368
Michael Chanc0c050c2015-10-22 16:01:17 -0400369 goto tx_done;
370 }
371
372normal_tx:
373 if (length < BNXT_MIN_PKT_SIZE) {
374 pad = BNXT_MIN_PKT_SIZE - length;
375 if (skb_pad(skb, pad)) {
376 /* SKB already freed. */
377 tx_buf->skb = NULL;
378 return NETDEV_TX_OK;
379 }
380 length = BNXT_MIN_PKT_SIZE;
381 }
382
383 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
384
385 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
386 dev_kfree_skb_any(skb);
387 tx_buf->skb = NULL;
388 return NETDEV_TX_OK;
389 }
390
391 dma_unmap_addr_set(tx_buf, mapping, mapping);
392 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
393 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
394
395 txbd->tx_bd_haddr = cpu_to_le64(mapping);
396
397 prod = NEXT_TX(prod);
398 txbd1 = (struct tx_bd_ext *)
399 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
400
401 txbd1->tx_bd_hsize_lflags = 0;
402 if (skb_is_gso(skb)) {
403 u32 hdr_len;
404
405 if (skb->encapsulation)
406 hdr_len = skb_inner_network_offset(skb) +
407 skb_inner_network_header_len(skb) +
408 inner_tcp_hdrlen(skb);
409 else
410 hdr_len = skb_transport_offset(skb) +
411 tcp_hdrlen(skb);
412
413 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
414 TX_BD_FLAGS_T_IPID |
415 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
416 length = skb_shinfo(skb)->gso_size;
417 txbd1->tx_bd_mss = cpu_to_le32(length);
418 length += hdr_len;
419 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 txbd1->tx_bd_hsize_lflags =
421 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
422 txbd1->tx_bd_mss = 0;
423 }
424
425 length >>= 9;
426 flags |= bnxt_lhint_arr[length];
427 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
428
429 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
430 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
431 for (i = 0; i < last_frag; i++) {
432 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
433
434 prod = NEXT_TX(prod);
435 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
436
437 len = skb_frag_size(frag);
438 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
439 DMA_TO_DEVICE);
440
441 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
442 goto tx_dma_error;
443
444 tx_buf = &txr->tx_buf_ring[prod];
445 dma_unmap_addr_set(tx_buf, mapping, mapping);
446
447 txbd->tx_bd_haddr = cpu_to_le64(mapping);
448
449 flags = len << TX_BD_LEN_SHIFT;
450 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
451 }
452
453 flags &= ~TX_BD_LEN;
454 txbd->tx_bd_len_flags_type =
455 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
456 TX_BD_FLAGS_PACKET_END);
457
458 netdev_tx_sent_queue(txq, skb->len);
459
460 /* Sync BD data before updating doorbell */
461 wmb();
462
463 prod = NEXT_TX(prod);
464 txr->tx_prod = prod;
465
Michael Chanffe40642017-05-30 20:03:00 -0400466 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400467 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400468
469tx_done:
470
471 mmiowb();
472
473 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400474 if (skb->xmit_more && !tx_buf->is_push)
475 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
476
Michael Chanc0c050c2015-10-22 16:01:17 -0400477 netif_tx_stop_queue(txq);
478
479 /* netif_tx_stop_queue() must be done before checking
480 * tx index in bnxt_tx_avail() below, because in
481 * bnxt_tx_int(), we update tx index before checking for
482 * netif_tx_queue_stopped().
483 */
484 smp_mb();
485 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
486 netif_tx_wake_queue(txq);
487 }
488 return NETDEV_TX_OK;
489
490tx_dma_error:
491 last_frag = i;
492
493 /* start back at beginning and unmap skb */
494 prod = txr->tx_prod;
495 tx_buf = &txr->tx_buf_ring[prod];
496 tx_buf->skb = NULL;
497 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
498 skb_headlen(skb), PCI_DMA_TODEVICE);
499 prod = NEXT_TX(prod);
500
501 /* unmap remaining mapped pages */
502 for (i = 0; i < last_frag; i++) {
503 prod = NEXT_TX(prod);
504 tx_buf = &txr->tx_buf_ring[prod];
505 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
506 skb_frag_size(&skb_shinfo(skb)->frags[i]),
507 PCI_DMA_TODEVICE);
508 }
509
510 dev_kfree_skb_any(skb);
511 return NETDEV_TX_OK;
512}
513
514static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
515{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500516 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500517 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400518 u16 cons = txr->tx_cons;
519 struct pci_dev *pdev = bp->pdev;
520 int i;
521 unsigned int tx_bytes = 0;
522
523 for (i = 0; i < nr_pkts; i++) {
524 struct bnxt_sw_tx_bd *tx_buf;
525 struct sk_buff *skb;
526 int j, last;
527
528 tx_buf = &txr->tx_buf_ring[cons];
529 cons = NEXT_TX(cons);
530 skb = tx_buf->skb;
531 tx_buf->skb = NULL;
532
533 if (tx_buf->is_push) {
534 tx_buf->is_push = 0;
535 goto next_tx_int;
536 }
537
538 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
539 skb_headlen(skb), PCI_DMA_TODEVICE);
540 last = tx_buf->nr_frags;
541
542 for (j = 0; j < last; j++) {
543 cons = NEXT_TX(cons);
544 tx_buf = &txr->tx_buf_ring[cons];
545 dma_unmap_page(
546 &pdev->dev,
547 dma_unmap_addr(tx_buf, mapping),
548 skb_frag_size(&skb_shinfo(skb)->frags[j]),
549 PCI_DMA_TODEVICE);
550 }
551
552next_tx_int:
553 cons = NEXT_TX(cons);
554
555 tx_bytes += skb->len;
556 dev_kfree_skb_any(skb);
557 }
558
559 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
560 txr->tx_cons = cons;
561
562 /* Need to make the tx_cons update visible to bnxt_start_xmit()
563 * before checking for netif_tx_queue_stopped(). Without the
564 * memory barrier, there is a small possibility that bnxt_start_xmit()
565 * will miss it and cause the queue to be stopped forever.
566 */
567 smp_mb();
568
569 if (unlikely(netif_tx_queue_stopped(txq)) &&
570 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
571 __netif_tx_lock(txq, smp_processor_id());
572 if (netif_tx_queue_stopped(txq) &&
573 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
574 txr->dev_state != BNXT_DEV_STATE_CLOSING)
575 netif_tx_wake_queue(txq);
576 __netif_tx_unlock(txq);
577 }
578}
579
Michael Chanc61fb992017-02-06 16:55:36 -0500580static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
581 gfp_t gfp)
582{
583 struct device *dev = &bp->pdev->dev;
584 struct page *page;
585
586 page = alloc_page(gfp);
587 if (!page)
588 return NULL;
589
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700590 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
591 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500592 if (dma_mapping_error(dev, *mapping)) {
593 __free_page(page);
594 return NULL;
595 }
596 *mapping += bp->rx_dma_offset;
597 return page;
598}
599
Michael Chanc0c050c2015-10-22 16:01:17 -0400600static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
601 gfp_t gfp)
602{
603 u8 *data;
604 struct pci_dev *pdev = bp->pdev;
605
606 data = kmalloc(bp->rx_buf_size, gfp);
607 if (!data)
608 return NULL;
609
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700610 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
611 bp->rx_buf_use_size, bp->rx_dir,
612 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400613
614 if (dma_mapping_error(&pdev->dev, *mapping)) {
615 kfree(data);
616 data = NULL;
617 }
618 return data;
619}
620
Michael Chan38413402017-02-06 16:55:43 -0500621int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
622 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400623{
624 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
625 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400626 dma_addr_t mapping;
627
Michael Chanc61fb992017-02-06 16:55:36 -0500628 if (BNXT_RX_PAGE_MODE(bp)) {
629 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400630
Michael Chanc61fb992017-02-06 16:55:36 -0500631 if (!page)
632 return -ENOMEM;
633
634 rx_buf->data = page;
635 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
636 } else {
637 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
638
639 if (!data)
640 return -ENOMEM;
641
642 rx_buf->data = data;
643 rx_buf->data_ptr = data + bp->rx_offset;
644 }
Michael Chan11cd1192017-02-06 16:55:33 -0500645 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400646
647 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400648 return 0;
649}
650
Michael Chanc6d30e82017-02-06 16:55:42 -0500651void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400652{
653 u16 prod = rxr->rx_prod;
654 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
655 struct rx_bd *cons_bd, *prod_bd;
656
657 prod_rx_buf = &rxr->rx_buf_ring[prod];
658 cons_rx_buf = &rxr->rx_buf_ring[cons];
659
660 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500661 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400662
Michael Chan11cd1192017-02-06 16:55:33 -0500663 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400664
665 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
666 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
667
668 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
669}
670
671static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
672{
673 u16 next, max = rxr->rx_agg_bmap_size;
674
675 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
676 if (next >= max)
677 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
678 return next;
679}
680
681static inline int bnxt_alloc_rx_page(struct bnxt *bp,
682 struct bnxt_rx_ring_info *rxr,
683 u16 prod, gfp_t gfp)
684{
685 struct rx_bd *rxbd =
686 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
687 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
688 struct pci_dev *pdev = bp->pdev;
689 struct page *page;
690 dma_addr_t mapping;
691 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400692 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400693
Michael Chan89d0a062016-04-25 02:30:51 -0400694 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
695 page = rxr->rx_page;
696 if (!page) {
697 page = alloc_page(gfp);
698 if (!page)
699 return -ENOMEM;
700 rxr->rx_page = page;
701 rxr->rx_page_offset = 0;
702 }
703 offset = rxr->rx_page_offset;
704 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
705 if (rxr->rx_page_offset == PAGE_SIZE)
706 rxr->rx_page = NULL;
707 else
708 get_page(page);
709 } else {
710 page = alloc_page(gfp);
711 if (!page)
712 return -ENOMEM;
713 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400714
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700715 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
716 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
717 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400718 if (dma_mapping_error(&pdev->dev, mapping)) {
719 __free_page(page);
720 return -EIO;
721 }
722
723 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
724 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
725
726 __set_bit(sw_prod, rxr->rx_agg_bmap);
727 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
728 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
729
730 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400731 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400732 rx_agg_buf->mapping = mapping;
733 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
734 rxbd->rx_bd_opaque = sw_prod;
735 return 0;
736}
737
738static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
739 u32 agg_bufs)
740{
741 struct bnxt *bp = bnapi->bp;
742 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500743 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400744 u16 prod = rxr->rx_agg_prod;
745 u16 sw_prod = rxr->rx_sw_agg_prod;
746 u32 i;
747
748 for (i = 0; i < agg_bufs; i++) {
749 u16 cons;
750 struct rx_agg_cmp *agg;
751 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
752 struct rx_bd *prod_bd;
753 struct page *page;
754
755 agg = (struct rx_agg_cmp *)
756 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
757 cons = agg->rx_agg_cmp_opaque;
758 __clear_bit(cons, rxr->rx_agg_bmap);
759
760 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
761 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
762
763 __set_bit(sw_prod, rxr->rx_agg_bmap);
764 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
765 cons_rx_buf = &rxr->rx_agg_ring[cons];
766
767 /* It is possible for sw_prod to be equal to cons, so
768 * set cons_rx_buf->page to NULL first.
769 */
770 page = cons_rx_buf->page;
771 cons_rx_buf->page = NULL;
772 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400773 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400774
775 prod_rx_buf->mapping = cons_rx_buf->mapping;
776
777 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
778
779 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
780 prod_bd->rx_bd_opaque = sw_prod;
781
782 prod = NEXT_RX_AGG(prod);
783 sw_prod = NEXT_RX_AGG(sw_prod);
784 cp_cons = NEXT_CMP(cp_cons);
785 }
786 rxr->rx_agg_prod = prod;
787 rxr->rx_sw_agg_prod = sw_prod;
788}
789
Michael Chanc61fb992017-02-06 16:55:36 -0500790static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
791 struct bnxt_rx_ring_info *rxr,
792 u16 cons, void *data, u8 *data_ptr,
793 dma_addr_t dma_addr,
794 unsigned int offset_and_len)
795{
796 unsigned int payload = offset_and_len >> 16;
797 unsigned int len = offset_and_len & 0xffff;
798 struct skb_frag_struct *frag;
799 struct page *page = data;
800 u16 prod = rxr->rx_prod;
801 struct sk_buff *skb;
802 int off, err;
803
804 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
805 if (unlikely(err)) {
806 bnxt_reuse_rx_data(rxr, cons, data);
807 return NULL;
808 }
809 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700810 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
811 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500812
813 if (unlikely(!payload))
814 payload = eth_get_headlen(data_ptr, len);
815
816 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
817 if (!skb) {
818 __free_page(page);
819 return NULL;
820 }
821
822 off = (void *)data_ptr - page_address(page);
823 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
824 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
825 payload + NET_IP_ALIGN);
826
827 frag = &skb_shinfo(skb)->frags[0];
828 skb_frag_size_sub(frag, payload);
829 frag->page_offset += payload;
830 skb->data_len -= payload;
831 skb->tail += payload;
832
833 return skb;
834}
835
Michael Chanc0c050c2015-10-22 16:01:17 -0400836static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
837 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500838 void *data, u8 *data_ptr,
839 dma_addr_t dma_addr,
840 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400841{
Michael Chan6bb19472017-02-06 16:55:32 -0500842 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400843 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500844 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400845
846 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
847 if (unlikely(err)) {
848 bnxt_reuse_rx_data(rxr, cons, data);
849 return NULL;
850 }
851
852 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700853 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
854 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400855 if (!skb) {
856 kfree(data);
857 return NULL;
858 }
859
Michael Chanb3dba772017-02-06 16:55:35 -0500860 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500861 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400862 return skb;
863}
864
865static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
866 struct sk_buff *skb, u16 cp_cons,
867 u32 agg_bufs)
868{
869 struct pci_dev *pdev = bp->pdev;
870 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500871 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400872 u16 prod = rxr->rx_agg_prod;
873 u32 i;
874
875 for (i = 0; i < agg_bufs; i++) {
876 u16 cons, frag_len;
877 struct rx_agg_cmp *agg;
878 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
879 struct page *page;
880 dma_addr_t mapping;
881
882 agg = (struct rx_agg_cmp *)
883 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
884 cons = agg->rx_agg_cmp_opaque;
885 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
886 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
887
888 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400889 skb_fill_page_desc(skb, i, cons_rx_buf->page,
890 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400891 __clear_bit(cons, rxr->rx_agg_bmap);
892
893 /* It is possible for bnxt_alloc_rx_page() to allocate
894 * a sw_prod index that equals the cons index, so we
895 * need to clear the cons entry now.
896 */
Michael Chan11cd1192017-02-06 16:55:33 -0500897 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400898 page = cons_rx_buf->page;
899 cons_rx_buf->page = NULL;
900
901 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
902 struct skb_shared_info *shinfo;
903 unsigned int nr_frags;
904
905 shinfo = skb_shinfo(skb);
906 nr_frags = --shinfo->nr_frags;
907 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
908
909 dev_kfree_skb(skb);
910
911 cons_rx_buf->page = page;
912
913 /* Update prod since possibly some pages have been
914 * allocated already.
915 */
916 rxr->rx_agg_prod = prod;
917 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
918 return NULL;
919 }
920
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700921 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
922 PCI_DMA_FROMDEVICE,
923 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400924
925 skb->data_len += frag_len;
926 skb->len += frag_len;
927 skb->truesize += PAGE_SIZE;
928
929 prod = NEXT_RX_AGG(prod);
930 cp_cons = NEXT_CMP(cp_cons);
931 }
932 rxr->rx_agg_prod = prod;
933 return skb;
934}
935
936static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
937 u8 agg_bufs, u32 *raw_cons)
938{
939 u16 last;
940 struct rx_agg_cmp *agg;
941
942 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
943 last = RING_CMP(*raw_cons);
944 agg = (struct rx_agg_cmp *)
945 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
946 return RX_AGG_CMP_VALID(agg, *raw_cons);
947}
948
949static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
950 unsigned int len,
951 dma_addr_t mapping)
952{
953 struct bnxt *bp = bnapi->bp;
954 struct pci_dev *pdev = bp->pdev;
955 struct sk_buff *skb;
956
957 skb = napi_alloc_skb(&bnapi->napi, len);
958 if (!skb)
959 return NULL;
960
Michael Chan745fc052017-02-06 16:55:34 -0500961 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
962 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400963
Michael Chan6bb19472017-02-06 16:55:32 -0500964 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
965 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400966
Michael Chan745fc052017-02-06 16:55:34 -0500967 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
968 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400969
970 skb_put(skb, len);
971 return skb;
972}
973
Michael Chanfa7e2812016-05-10 19:18:00 -0400974static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
975 u32 *raw_cons, void *cmp)
976{
977 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
978 struct rx_cmp *rxcmp = cmp;
979 u32 tmp_raw_cons = *raw_cons;
980 u8 cmp_type, agg_bufs = 0;
981
982 cmp_type = RX_CMP_TYPE(rxcmp);
983
984 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
985 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
986 RX_CMP_AGG_BUFS) >>
987 RX_CMP_AGG_BUFS_SHIFT;
988 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
989 struct rx_tpa_end_cmp *tpa_end = cmp;
990
991 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
992 RX_TPA_END_CMP_AGG_BUFS) >>
993 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
994 }
995
996 if (agg_bufs) {
997 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
998 return -EBUSY;
999 }
1000 *raw_cons = tmp_raw_cons;
1001 return 0;
1002}
1003
1004static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1005{
1006 if (!rxr->bnapi->in_reset) {
1007 rxr->bnapi->in_reset = true;
1008 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1009 schedule_work(&bp->sp_task);
1010 }
1011 rxr->rx_next_cons = 0xffff;
1012}
1013
Michael Chanc0c050c2015-10-22 16:01:17 -04001014static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1015 struct rx_tpa_start_cmp *tpa_start,
1016 struct rx_tpa_start_cmp_ext *tpa_start1)
1017{
1018 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1019 u16 cons, prod;
1020 struct bnxt_tpa_info *tpa_info;
1021 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1022 struct rx_bd *prod_bd;
1023 dma_addr_t mapping;
1024
1025 cons = tpa_start->rx_tpa_start_cmp_opaque;
1026 prod = rxr->rx_prod;
1027 cons_rx_buf = &rxr->rx_buf_ring[cons];
1028 prod_rx_buf = &rxr->rx_buf_ring[prod];
1029 tpa_info = &rxr->rx_tpa[agg_id];
1030
Michael Chanfa7e2812016-05-10 19:18:00 -04001031 if (unlikely(cons != rxr->rx_next_cons)) {
1032 bnxt_sched_reset(bp, rxr);
1033 return;
1034 }
1035
Michael Chanc0c050c2015-10-22 16:01:17 -04001036 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001037 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001038
1039 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001040 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001041
1042 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1043
1044 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1045
1046 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001047 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001048 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001049 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001050
1051 tpa_info->len =
1052 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1053 RX_TPA_START_CMP_LEN_SHIFT;
1054 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1055 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1056
1057 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1058 tpa_info->gso_type = SKB_GSO_TCPV4;
1059 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1060 if (hash_type == 3)
1061 tpa_info->gso_type = SKB_GSO_TCPV6;
1062 tpa_info->rss_hash =
1063 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1064 } else {
1065 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1066 tpa_info->gso_type = 0;
1067 if (netif_msg_rx_err(bp))
1068 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1069 }
1070 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1071 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001072 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001073
1074 rxr->rx_prod = NEXT_RX(prod);
1075 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001076 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001077 cons_rx_buf = &rxr->rx_buf_ring[cons];
1078
1079 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1080 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1081 cons_rx_buf->data = NULL;
1082}
1083
1084static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1085 u16 cp_cons, u32 agg_bufs)
1086{
1087 if (agg_bufs)
1088 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1089}
1090
Michael Chan94758f82016-06-13 02:25:35 -04001091static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1092 int payload_off, int tcp_ts,
1093 struct sk_buff *skb)
1094{
1095#ifdef CONFIG_INET
1096 struct tcphdr *th;
1097 int len, nw_off;
1098 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1099 u32 hdr_info = tpa_info->hdr_info;
1100 bool loopback = false;
1101
1102 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1103 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1104 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1105
1106 /* If the packet is an internal loopback packet, the offsets will
1107 * have an extra 4 bytes.
1108 */
1109 if (inner_mac_off == 4) {
1110 loopback = true;
1111 } else if (inner_mac_off > 4) {
1112 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1113 ETH_HLEN - 2));
1114
1115 /* We only support inner iPv4/ipv6. If we don't see the
1116 * correct protocol ID, it must be a loopback packet where
1117 * the offsets are off by 4.
1118 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001119 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001120 loopback = true;
1121 }
1122 if (loopback) {
1123 /* internal loopback packet, subtract all offsets by 4 */
1124 inner_ip_off -= 4;
1125 inner_mac_off -= 4;
1126 outer_ip_off -= 4;
1127 }
1128
1129 nw_off = inner_ip_off - ETH_HLEN;
1130 skb_set_network_header(skb, nw_off);
1131 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1132 struct ipv6hdr *iph = ipv6_hdr(skb);
1133
1134 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1135 len = skb->len - skb_transport_offset(skb);
1136 th = tcp_hdr(skb);
1137 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1138 } else {
1139 struct iphdr *iph = ip_hdr(skb);
1140
1141 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1142 len = skb->len - skb_transport_offset(skb);
1143 th = tcp_hdr(skb);
1144 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1145 }
1146
1147 if (inner_mac_off) { /* tunnel */
1148 struct udphdr *uh = NULL;
1149 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1150 ETH_HLEN - 2));
1151
1152 if (proto == htons(ETH_P_IP)) {
1153 struct iphdr *iph = (struct iphdr *)skb->data;
1154
1155 if (iph->protocol == IPPROTO_UDP)
1156 uh = (struct udphdr *)(iph + 1);
1157 } else {
1158 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1159
1160 if (iph->nexthdr == IPPROTO_UDP)
1161 uh = (struct udphdr *)(iph + 1);
1162 }
1163 if (uh) {
1164 if (uh->check)
1165 skb_shinfo(skb)->gso_type |=
1166 SKB_GSO_UDP_TUNNEL_CSUM;
1167 else
1168 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1169 }
1170 }
1171#endif
1172 return skb;
1173}
1174
Michael Chanc0c050c2015-10-22 16:01:17 -04001175#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1176#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1177
Michael Chan309369c2016-06-13 02:25:34 -04001178static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1179 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001180 struct sk_buff *skb)
1181{
Michael Chand1611c32015-10-25 22:27:57 -04001182#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001183 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001184 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001185
Michael Chan309369c2016-06-13 02:25:34 -04001186 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001187 tcp_opt_len = 12;
1188
Michael Chanc0c050c2015-10-22 16:01:17 -04001189 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1190 struct iphdr *iph;
1191
1192 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1193 ETH_HLEN;
1194 skb_set_network_header(skb, nw_off);
1195 iph = ip_hdr(skb);
1196 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1197 len = skb->len - skb_transport_offset(skb);
1198 th = tcp_hdr(skb);
1199 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1200 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1201 struct ipv6hdr *iph;
1202
1203 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1204 ETH_HLEN;
1205 skb_set_network_header(skb, nw_off);
1206 iph = ipv6_hdr(skb);
1207 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1208 len = skb->len - skb_transport_offset(skb);
1209 th = tcp_hdr(skb);
1210 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1211 } else {
1212 dev_kfree_skb_any(skb);
1213 return NULL;
1214 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001215
1216 if (nw_off) { /* tunnel */
1217 struct udphdr *uh = NULL;
1218
1219 if (skb->protocol == htons(ETH_P_IP)) {
1220 struct iphdr *iph = (struct iphdr *)skb->data;
1221
1222 if (iph->protocol == IPPROTO_UDP)
1223 uh = (struct udphdr *)(iph + 1);
1224 } else {
1225 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1226
1227 if (iph->nexthdr == IPPROTO_UDP)
1228 uh = (struct udphdr *)(iph + 1);
1229 }
1230 if (uh) {
1231 if (uh->check)
1232 skb_shinfo(skb)->gso_type |=
1233 SKB_GSO_UDP_TUNNEL_CSUM;
1234 else
1235 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1236 }
1237 }
1238#endif
1239 return skb;
1240}
1241
Michael Chan309369c2016-06-13 02:25:34 -04001242static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1243 struct bnxt_tpa_info *tpa_info,
1244 struct rx_tpa_end_cmp *tpa_end,
1245 struct rx_tpa_end_cmp_ext *tpa_end1,
1246 struct sk_buff *skb)
1247{
1248#ifdef CONFIG_INET
1249 int payload_off;
1250 u16 segs;
1251
1252 segs = TPA_END_TPA_SEGS(tpa_end);
1253 if (segs == 1)
1254 return skb;
1255
1256 NAPI_GRO_CB(skb)->count = segs;
1257 skb_shinfo(skb)->gso_size =
1258 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1259 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1260 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1261 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1262 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1263 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001264 if (likely(skb))
1265 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001266#endif
1267 return skb;
1268}
1269
Michael Chanc0c050c2015-10-22 16:01:17 -04001270static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1271 struct bnxt_napi *bnapi,
1272 u32 *raw_cons,
1273 struct rx_tpa_end_cmp *tpa_end,
1274 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001275 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001276{
1277 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001278 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001279 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001280 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001281 u16 cp_cons = RING_CMP(*raw_cons);
1282 unsigned int len;
1283 struct bnxt_tpa_info *tpa_info;
1284 dma_addr_t mapping;
1285 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001286 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001287
Michael Chanfa7e2812016-05-10 19:18:00 -04001288 if (unlikely(bnapi->in_reset)) {
1289 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1290
1291 if (rc < 0)
1292 return ERR_PTR(-EBUSY);
1293 return NULL;
1294 }
1295
Michael Chanc0c050c2015-10-22 16:01:17 -04001296 tpa_info = &rxr->rx_tpa[agg_id];
1297 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001298 data_ptr = tpa_info->data_ptr;
1299 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001300 len = tpa_info->len;
1301 mapping = tpa_info->mapping;
1302
1303 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1304 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1305
1306 if (agg_bufs) {
1307 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1308 return ERR_PTR(-EBUSY);
1309
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001310 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001311 cp_cons = NEXT_CMP(cp_cons);
1312 }
1313
Michael Chan69c149e2017-06-23 14:01:00 -04001314 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001315 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001316 if (agg_bufs > MAX_SKB_FRAGS)
1317 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1318 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001319 return NULL;
1320 }
1321
1322 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001323 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001324 if (!skb) {
1325 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1326 return NULL;
1327 }
1328 } else {
1329 u8 *new_data;
1330 dma_addr_t new_mapping;
1331
1332 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1333 if (!new_data) {
1334 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1335 return NULL;
1336 }
1337
1338 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001339 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001340 tpa_info->mapping = new_mapping;
1341
1342 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001343 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1344 bp->rx_buf_use_size, bp->rx_dir,
1345 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001346
1347 if (!skb) {
1348 kfree(data);
1349 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1350 return NULL;
1351 }
Michael Chanb3dba772017-02-06 16:55:35 -05001352 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001353 skb_put(skb, len);
1354 }
1355
1356 if (agg_bufs) {
1357 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1358 if (!skb) {
1359 /* Page reuse already handled by bnxt_rx_pages(). */
1360 return NULL;
1361 }
1362 }
1363 skb->protocol = eth_type_trans(skb, bp->dev);
1364
1365 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1366 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1367
Michael Chan8852ddb2016-06-06 02:37:16 -04001368 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1369 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001370 u16 vlan_proto = tpa_info->metadata >>
1371 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001372 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001373
Michael Chan8852ddb2016-06-06 02:37:16 -04001374 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001375 }
1376
1377 skb_checksum_none_assert(skb);
1378 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1379 skb->ip_summed = CHECKSUM_UNNECESSARY;
1380 skb->csum_level =
1381 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1382 }
1383
1384 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001385 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001386
1387 return skb;
1388}
1389
1390/* returns the following:
1391 * 1 - 1 packet successfully received
1392 * 0 - successful TPA_START, packet not completed yet
1393 * -EBUSY - completion ring does not have all the agg buffers yet
1394 * -ENOMEM - packet aborted due to out of memory
1395 * -EIO - packet aborted due to hw error indicated in BD
1396 */
1397static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001398 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001399{
1400 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001401 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001402 struct net_device *dev = bp->dev;
1403 struct rx_cmp *rxcmp;
1404 struct rx_cmp_ext *rxcmp1;
1405 u32 tmp_raw_cons = *raw_cons;
1406 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1407 struct bnxt_sw_rx_bd *rx_buf;
1408 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001409 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001410 dma_addr_t dma_addr;
1411 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001412 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001413 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001414 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001415
1416 rxcmp = (struct rx_cmp *)
1417 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1418
1419 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1420 cp_cons = RING_CMP(tmp_raw_cons);
1421 rxcmp1 = (struct rx_cmp_ext *)
1422 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1423
1424 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1425 return -EBUSY;
1426
1427 cmp_type = RX_CMP_TYPE(rxcmp);
1428
1429 prod = rxr->rx_prod;
1430
1431 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1432 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1433 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1434
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001435 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001436 goto next_rx_no_prod;
1437
1438 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1439 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1440 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001441 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001442
1443 if (unlikely(IS_ERR(skb)))
1444 return -EBUSY;
1445
1446 rc = -ENOMEM;
1447 if (likely(skb)) {
1448 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001449 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001450 rc = 1;
1451 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001452 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001453 goto next_rx_no_prod;
1454 }
1455
1456 cons = rxcmp->rx_cmp_opaque;
1457 rx_buf = &rxr->rx_buf_ring[cons];
1458 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001459 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001460 if (unlikely(cons != rxr->rx_next_cons)) {
1461 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1462
1463 bnxt_sched_reset(bp, rxr);
1464 return rc1;
1465 }
Michael Chan6bb19472017-02-06 16:55:32 -05001466 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001467
Michael Chanc61fb992017-02-06 16:55:36 -05001468 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1469 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001470
1471 if (agg_bufs) {
1472 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1473 return -EBUSY;
1474
1475 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001476 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001477 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001478 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001479
1480 rx_buf->data = NULL;
1481 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1482 bnxt_reuse_rx_data(rxr, cons, data);
1483 if (agg_bufs)
1484 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1485
1486 rc = -EIO;
1487 goto next_rx;
1488 }
1489
1490 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001491 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001492
Michael Chanc6d30e82017-02-06 16:55:42 -05001493 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1494 rc = 1;
1495 goto next_rx;
1496 }
1497
Michael Chanc0c050c2015-10-22 16:01:17 -04001498 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001499 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001500 bnxt_reuse_rx_data(rxr, cons, data);
1501 if (!skb) {
1502 rc = -ENOMEM;
1503 goto next_rx;
1504 }
1505 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001506 u32 payload;
1507
Michael Chanc6d30e82017-02-06 16:55:42 -05001508 if (rx_buf->data_ptr == data_ptr)
1509 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1510 else
1511 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001512 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001513 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001514 if (!skb) {
1515 rc = -ENOMEM;
1516 goto next_rx;
1517 }
1518 }
1519
1520 if (agg_bufs) {
1521 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1522 if (!skb) {
1523 rc = -ENOMEM;
1524 goto next_rx;
1525 }
1526 }
1527
1528 if (RX_CMP_HASH_VALID(rxcmp)) {
1529 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1530 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1531
1532 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1533 if (hash_type != 1 && hash_type != 3)
1534 type = PKT_HASH_TYPE_L3;
1535 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1536 }
1537
1538 skb->protocol = eth_type_trans(skb, dev);
1539
Michael Chan8852ddb2016-06-06 02:37:16 -04001540 if ((rxcmp1->rx_cmp_flags2 &
1541 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1542 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001543 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001544 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001545 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1546
Michael Chan8852ddb2016-06-06 02:37:16 -04001547 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001548 }
1549
1550 skb_checksum_none_assert(skb);
1551 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1552 if (dev->features & NETIF_F_RXCSUM) {
1553 skb->ip_summed = CHECKSUM_UNNECESSARY;
1554 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1555 }
1556 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001557 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1558 if (dev->features & NETIF_F_RXCSUM)
1559 cpr->rx_l4_csum_errors++;
1560 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001561 }
1562
1563 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001564 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001565 rc = 1;
1566
1567next_rx:
1568 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001569 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001570
1571next_rx_no_prod:
1572 *raw_cons = tmp_raw_cons;
1573
1574 return rc;
1575}
1576
Michael Chan2270bc52017-06-23 14:01:01 -04001577/* In netpoll mode, if we are using a combined completion ring, we need to
1578 * discard the rx packets and recycle the buffers.
1579 */
1580static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1581 u32 *raw_cons, u8 *event)
1582{
1583 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1584 u32 tmp_raw_cons = *raw_cons;
1585 struct rx_cmp_ext *rxcmp1;
1586 struct rx_cmp *rxcmp;
1587 u16 cp_cons;
1588 u8 cmp_type;
1589
1590 cp_cons = RING_CMP(tmp_raw_cons);
1591 rxcmp = (struct rx_cmp *)
1592 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1593
1594 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1595 cp_cons = RING_CMP(tmp_raw_cons);
1596 rxcmp1 = (struct rx_cmp_ext *)
1597 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1598
1599 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1600 return -EBUSY;
1601
1602 cmp_type = RX_CMP_TYPE(rxcmp);
1603 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1604 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1605 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1606 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1607 struct rx_tpa_end_cmp_ext *tpa_end1;
1608
1609 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1610 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1611 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1612 }
1613 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1614}
1615
Michael Chan4bb13ab2016-04-05 14:09:01 -04001616#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001617 ((data) & \
1618 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001619
Michael Chanc0c050c2015-10-22 16:01:17 -04001620static int bnxt_async_event_process(struct bnxt *bp,
1621 struct hwrm_async_event_cmpl *cmpl)
1622{
1623 u16 event_id = le16_to_cpu(cmpl->event_id);
1624
1625 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1626 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001627 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001628 u32 data1 = le32_to_cpu(cmpl->event_data1);
1629 struct bnxt_link_info *link_info = &bp->link_info;
1630
1631 if (BNXT_VF(bp))
1632 goto async_event_process_exit;
1633 if (data1 & 0x20000) {
1634 u16 fw_speed = link_info->force_link_speed;
1635 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1636
1637 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1638 speed);
1639 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001640 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001641 /* fall thru */
1642 }
Michael Chan87c374d2016-12-02 21:17:16 -05001643 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001644 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001645 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001646 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001647 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001648 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001649 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001650 u32 data1 = le32_to_cpu(cmpl->event_data1);
1651 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1652
1653 if (BNXT_VF(bp))
1654 break;
1655
1656 if (bp->pf.port_id != port_id)
1657 break;
1658
Michael Chan4bb13ab2016-04-05 14:09:01 -04001659 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1660 break;
1661 }
Michael Chan87c374d2016-12-02 21:17:16 -05001662 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001663 if (BNXT_PF(bp))
1664 goto async_event_process_exit;
1665 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1666 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001667 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001668 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001669 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001670 schedule_work(&bp->sp_task);
1671async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001672 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001673 return 0;
1674}
1675
1676static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1677{
1678 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1679 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1680 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1681 (struct hwrm_fwd_req_cmpl *)txcmp;
1682
1683 switch (cmpl_type) {
1684 case CMPL_BASE_TYPE_HWRM_DONE:
1685 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1686 if (seq_id == bp->hwrm_intr_seq_id)
1687 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1688 else
1689 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1690 break;
1691
1692 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1693 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1694
1695 if ((vf_id < bp->pf.first_vf_id) ||
1696 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1697 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1698 vf_id);
1699 return -EINVAL;
1700 }
1701
1702 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1703 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1704 schedule_work(&bp->sp_task);
1705 break;
1706
1707 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1708 bnxt_async_event_process(bp,
1709 (struct hwrm_async_event_cmpl *)txcmp);
1710
1711 default:
1712 break;
1713 }
1714
1715 return 0;
1716}
1717
1718static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1719{
1720 struct bnxt_napi *bnapi = dev_instance;
1721 struct bnxt *bp = bnapi->bp;
1722 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1723 u32 cons = RING_CMP(cpr->cp_raw_cons);
1724
1725 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1726 napi_schedule(&bnapi->napi);
1727 return IRQ_HANDLED;
1728}
1729
1730static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1731{
1732 u32 raw_cons = cpr->cp_raw_cons;
1733 u16 cons = RING_CMP(raw_cons);
1734 struct tx_cmp *txcmp;
1735
1736 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1737
1738 return TX_CMP_VALID(txcmp, raw_cons);
1739}
1740
Michael Chanc0c050c2015-10-22 16:01:17 -04001741static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1742{
1743 struct bnxt_napi *bnapi = dev_instance;
1744 struct bnxt *bp = bnapi->bp;
1745 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1746 u32 cons = RING_CMP(cpr->cp_raw_cons);
1747 u32 int_status;
1748
1749 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1750
1751 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001752 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001753 /* return if erroneous interrupt */
1754 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1755 return IRQ_NONE;
1756 }
1757
1758 /* disable ring IRQ */
1759 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1760
1761 /* Return here if interrupt is shared and is disabled. */
1762 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1763 return IRQ_HANDLED;
1764
1765 napi_schedule(&bnapi->napi);
1766 return IRQ_HANDLED;
1767}
1768
1769static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1770{
1771 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1772 u32 raw_cons = cpr->cp_raw_cons;
1773 u32 cons;
1774 int tx_pkts = 0;
1775 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001776 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001777 struct tx_cmp *txcmp;
1778
1779 while (1) {
1780 int rc;
1781
1782 cons = RING_CMP(raw_cons);
1783 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1784
1785 if (!TX_CMP_VALID(txcmp, raw_cons))
1786 break;
1787
Michael Chan67a95e22016-05-04 16:56:43 -04001788 /* The valid test of the entry must be done first before
1789 * reading any further.
1790 */
Michael Chanb67daab2016-05-15 03:04:51 -04001791 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001792 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1793 tx_pkts++;
1794 /* return full budget so NAPI will complete. */
1795 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1796 rx_pkts = budget;
1797 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001798 if (likely(budget))
1799 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1800 else
1801 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1802 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001803 if (likely(rc >= 0))
1804 rx_pkts += rc;
1805 else if (rc == -EBUSY) /* partial completion */
1806 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001807 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1808 CMPL_BASE_TYPE_HWRM_DONE) ||
1809 (TX_CMP_TYPE(txcmp) ==
1810 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1811 (TX_CMP_TYPE(txcmp) ==
1812 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1813 bnxt_hwrm_handler(bp, txcmp);
1814 }
1815 raw_cons = NEXT_RAW_CMP(raw_cons);
1816
1817 if (rx_pkts == budget)
1818 break;
1819 }
1820
Michael Chan38413402017-02-06 16:55:43 -05001821 if (event & BNXT_TX_EVENT) {
1822 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1823 void __iomem *db = txr->tx_doorbell;
1824 u16 prod = txr->tx_prod;
1825
1826 /* Sync BD data before updating doorbell */
1827 wmb();
1828
Michael Chan434c9752017-05-29 19:06:08 -04001829 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001830 }
1831
Michael Chanc0c050c2015-10-22 16:01:17 -04001832 cpr->cp_raw_cons = raw_cons;
1833 /* ACK completion ring before freeing tx ring and producing new
1834 * buffers in rx/agg rings to prevent overflowing the completion
1835 * ring.
1836 */
1837 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1838
1839 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001840 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001841
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001842 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001843 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001844
Michael Chan434c9752017-05-29 19:06:08 -04001845 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1846 if (event & BNXT_AGG_EVENT)
1847 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1848 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001849 }
1850 return rx_pkts;
1851}
1852
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001853static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1854{
1855 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1856 struct bnxt *bp = bnapi->bp;
1857 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1858 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1859 struct tx_cmp *txcmp;
1860 struct rx_cmp_ext *rxcmp1;
1861 u32 cp_cons, tmp_raw_cons;
1862 u32 raw_cons = cpr->cp_raw_cons;
1863 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001864 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001865
1866 while (1) {
1867 int rc;
1868
1869 cp_cons = RING_CMP(raw_cons);
1870 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1871
1872 if (!TX_CMP_VALID(txcmp, raw_cons))
1873 break;
1874
1875 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1876 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1877 cp_cons = RING_CMP(tmp_raw_cons);
1878 rxcmp1 = (struct rx_cmp_ext *)
1879 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1880
1881 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1882 break;
1883
1884 /* force an error to recycle the buffer */
1885 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1886 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1887
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001888 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001889 if (likely(rc == -EIO))
1890 rx_pkts++;
1891 else if (rc == -EBUSY) /* partial completion */
1892 break;
1893 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1894 CMPL_BASE_TYPE_HWRM_DONE)) {
1895 bnxt_hwrm_handler(bp, txcmp);
1896 } else {
1897 netdev_err(bp->dev,
1898 "Invalid completion received on special ring\n");
1899 }
1900 raw_cons = NEXT_RAW_CMP(raw_cons);
1901
1902 if (rx_pkts == budget)
1903 break;
1904 }
1905
1906 cpr->cp_raw_cons = raw_cons;
1907 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001908 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001909
Michael Chan434c9752017-05-29 19:06:08 -04001910 if (event & BNXT_AGG_EVENT)
1911 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1912 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001913
1914 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001915 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001916 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1917 }
1918 return rx_pkts;
1919}
1920
Michael Chanc0c050c2015-10-22 16:01:17 -04001921static int bnxt_poll(struct napi_struct *napi, int budget)
1922{
1923 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1924 struct bnxt *bp = bnapi->bp;
1925 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1926 int work_done = 0;
1927
Michael Chanc0c050c2015-10-22 16:01:17 -04001928 while (1) {
1929 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1930
1931 if (work_done >= budget)
1932 break;
1933
1934 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001935 if (napi_complete_done(napi, work_done))
1936 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1937 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001938 break;
1939 }
1940 }
1941 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001942 return work_done;
1943}
1944
Michael Chanc0c050c2015-10-22 16:01:17 -04001945static void bnxt_free_tx_skbs(struct bnxt *bp)
1946{
1947 int i, max_idx;
1948 struct pci_dev *pdev = bp->pdev;
1949
Michael Chanb6ab4b02016-01-02 23:44:59 -05001950 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001951 return;
1952
1953 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1954 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001955 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001956 int j;
1957
Michael Chanc0c050c2015-10-22 16:01:17 -04001958 for (j = 0; j < max_idx;) {
1959 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1960 struct sk_buff *skb = tx_buf->skb;
1961 int k, last;
1962
1963 if (!skb) {
1964 j++;
1965 continue;
1966 }
1967
1968 tx_buf->skb = NULL;
1969
1970 if (tx_buf->is_push) {
1971 dev_kfree_skb(skb);
1972 j += 2;
1973 continue;
1974 }
1975
1976 dma_unmap_single(&pdev->dev,
1977 dma_unmap_addr(tx_buf, mapping),
1978 skb_headlen(skb),
1979 PCI_DMA_TODEVICE);
1980
1981 last = tx_buf->nr_frags;
1982 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001983 for (k = 0; k < last; k++, j++) {
1984 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001985 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1986
Michael Chand612a572016-01-28 03:11:22 -05001987 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001988 dma_unmap_page(
1989 &pdev->dev,
1990 dma_unmap_addr(tx_buf, mapping),
1991 skb_frag_size(frag), PCI_DMA_TODEVICE);
1992 }
1993 dev_kfree_skb(skb);
1994 }
1995 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1996 }
1997}
1998
1999static void bnxt_free_rx_skbs(struct bnxt *bp)
2000{
2001 int i, max_idx, max_agg_idx;
2002 struct pci_dev *pdev = bp->pdev;
2003
Michael Chanb6ab4b02016-01-02 23:44:59 -05002004 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002005 return;
2006
2007 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2008 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2009 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002010 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002011 int j;
2012
Michael Chanc0c050c2015-10-22 16:01:17 -04002013 if (rxr->rx_tpa) {
2014 for (j = 0; j < MAX_TPA; j++) {
2015 struct bnxt_tpa_info *tpa_info =
2016 &rxr->rx_tpa[j];
2017 u8 *data = tpa_info->data;
2018
2019 if (!data)
2020 continue;
2021
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002022 dma_unmap_single_attrs(&pdev->dev,
2023 tpa_info->mapping,
2024 bp->rx_buf_use_size,
2025 bp->rx_dir,
2026 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002027
2028 tpa_info->data = NULL;
2029
2030 kfree(data);
2031 }
2032 }
2033
2034 for (j = 0; j < max_idx; j++) {
2035 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002036 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002037 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002038
2039 if (!data)
2040 continue;
2041
Michael Chanc0c050c2015-10-22 16:01:17 -04002042 rx_buf->data = NULL;
2043
Michael Chan3ed3a832017-03-28 19:47:31 -04002044 if (BNXT_RX_PAGE_MODE(bp)) {
2045 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002046 dma_unmap_page_attrs(&pdev->dev, mapping,
2047 PAGE_SIZE, bp->rx_dir,
2048 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002049 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002050 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002051 dma_unmap_single_attrs(&pdev->dev, mapping,
2052 bp->rx_buf_use_size,
2053 bp->rx_dir,
2054 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002055 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002056 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002057 }
2058
2059 for (j = 0; j < max_agg_idx; j++) {
2060 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2061 &rxr->rx_agg_ring[j];
2062 struct page *page = rx_agg_buf->page;
2063
2064 if (!page)
2065 continue;
2066
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002067 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2068 BNXT_RX_PAGE_SIZE,
2069 PCI_DMA_FROMDEVICE,
2070 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002071
2072 rx_agg_buf->page = NULL;
2073 __clear_bit(j, rxr->rx_agg_bmap);
2074
2075 __free_page(page);
2076 }
Michael Chan89d0a062016-04-25 02:30:51 -04002077 if (rxr->rx_page) {
2078 __free_page(rxr->rx_page);
2079 rxr->rx_page = NULL;
2080 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002081 }
2082}
2083
2084static void bnxt_free_skbs(struct bnxt *bp)
2085{
2086 bnxt_free_tx_skbs(bp);
2087 bnxt_free_rx_skbs(bp);
2088}
2089
2090static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2091{
2092 struct pci_dev *pdev = bp->pdev;
2093 int i;
2094
2095 for (i = 0; i < ring->nr_pages; i++) {
2096 if (!ring->pg_arr[i])
2097 continue;
2098
2099 dma_free_coherent(&pdev->dev, ring->page_size,
2100 ring->pg_arr[i], ring->dma_arr[i]);
2101
2102 ring->pg_arr[i] = NULL;
2103 }
2104 if (ring->pg_tbl) {
2105 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2106 ring->pg_tbl, ring->pg_tbl_map);
2107 ring->pg_tbl = NULL;
2108 }
2109 if (ring->vmem_size && *ring->vmem) {
2110 vfree(*ring->vmem);
2111 *ring->vmem = NULL;
2112 }
2113}
2114
2115static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2116{
2117 int i;
2118 struct pci_dev *pdev = bp->pdev;
2119
2120 if (ring->nr_pages > 1) {
2121 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2122 ring->nr_pages * 8,
2123 &ring->pg_tbl_map,
2124 GFP_KERNEL);
2125 if (!ring->pg_tbl)
2126 return -ENOMEM;
2127 }
2128
2129 for (i = 0; i < ring->nr_pages; i++) {
2130 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2131 ring->page_size,
2132 &ring->dma_arr[i],
2133 GFP_KERNEL);
2134 if (!ring->pg_arr[i])
2135 return -ENOMEM;
2136
2137 if (ring->nr_pages > 1)
2138 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2139 }
2140
2141 if (ring->vmem_size) {
2142 *ring->vmem = vzalloc(ring->vmem_size);
2143 if (!(*ring->vmem))
2144 return -ENOMEM;
2145 }
2146 return 0;
2147}
2148
2149static void bnxt_free_rx_rings(struct bnxt *bp)
2150{
2151 int i;
2152
Michael Chanb6ab4b02016-01-02 23:44:59 -05002153 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002154 return;
2155
2156 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002157 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002158 struct bnxt_ring_struct *ring;
2159
Michael Chanc6d30e82017-02-06 16:55:42 -05002160 if (rxr->xdp_prog)
2161 bpf_prog_put(rxr->xdp_prog);
2162
Michael Chanc0c050c2015-10-22 16:01:17 -04002163 kfree(rxr->rx_tpa);
2164 rxr->rx_tpa = NULL;
2165
2166 kfree(rxr->rx_agg_bmap);
2167 rxr->rx_agg_bmap = NULL;
2168
2169 ring = &rxr->rx_ring_struct;
2170 bnxt_free_ring(bp, ring);
2171
2172 ring = &rxr->rx_agg_ring_struct;
2173 bnxt_free_ring(bp, ring);
2174 }
2175}
2176
2177static int bnxt_alloc_rx_rings(struct bnxt *bp)
2178{
2179 int i, rc, agg_rings = 0, tpa_rings = 0;
2180
Michael Chanb6ab4b02016-01-02 23:44:59 -05002181 if (!bp->rx_ring)
2182 return -ENOMEM;
2183
Michael Chanc0c050c2015-10-22 16:01:17 -04002184 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2185 agg_rings = 1;
2186
2187 if (bp->flags & BNXT_FLAG_TPA)
2188 tpa_rings = 1;
2189
2190 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002191 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002192 struct bnxt_ring_struct *ring;
2193
Michael Chanc0c050c2015-10-22 16:01:17 -04002194 ring = &rxr->rx_ring_struct;
2195
2196 rc = bnxt_alloc_ring(bp, ring);
2197 if (rc)
2198 return rc;
2199
2200 if (agg_rings) {
2201 u16 mem_size;
2202
2203 ring = &rxr->rx_agg_ring_struct;
2204 rc = bnxt_alloc_ring(bp, ring);
2205 if (rc)
2206 return rc;
2207
2208 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2209 mem_size = rxr->rx_agg_bmap_size / 8;
2210 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2211 if (!rxr->rx_agg_bmap)
2212 return -ENOMEM;
2213
2214 if (tpa_rings) {
2215 rxr->rx_tpa = kcalloc(MAX_TPA,
2216 sizeof(struct bnxt_tpa_info),
2217 GFP_KERNEL);
2218 if (!rxr->rx_tpa)
2219 return -ENOMEM;
2220 }
2221 }
2222 }
2223 return 0;
2224}
2225
2226static void bnxt_free_tx_rings(struct bnxt *bp)
2227{
2228 int i;
2229 struct pci_dev *pdev = bp->pdev;
2230
Michael Chanb6ab4b02016-01-02 23:44:59 -05002231 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002232 return;
2233
2234 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002235 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002236 struct bnxt_ring_struct *ring;
2237
Michael Chanc0c050c2015-10-22 16:01:17 -04002238 if (txr->tx_push) {
2239 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2240 txr->tx_push, txr->tx_push_mapping);
2241 txr->tx_push = NULL;
2242 }
2243
2244 ring = &txr->tx_ring_struct;
2245
2246 bnxt_free_ring(bp, ring);
2247 }
2248}
2249
2250static int bnxt_alloc_tx_rings(struct bnxt *bp)
2251{
2252 int i, j, rc;
2253 struct pci_dev *pdev = bp->pdev;
2254
2255 bp->tx_push_size = 0;
2256 if (bp->tx_push_thresh) {
2257 int push_size;
2258
2259 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2260 bp->tx_push_thresh);
2261
Michael Chan4419dbe2016-02-10 17:33:49 -05002262 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002263 push_size = 0;
2264 bp->tx_push_thresh = 0;
2265 }
2266
2267 bp->tx_push_size = push_size;
2268 }
2269
2270 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002271 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 struct bnxt_ring_struct *ring;
2273
Michael Chanc0c050c2015-10-22 16:01:17 -04002274 ring = &txr->tx_ring_struct;
2275
2276 rc = bnxt_alloc_ring(bp, ring);
2277 if (rc)
2278 return rc;
2279
2280 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002281 dma_addr_t mapping;
2282
2283 /* One pre-allocated DMA buffer to backup
2284 * TX push operation
2285 */
2286 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2287 bp->tx_push_size,
2288 &txr->tx_push_mapping,
2289 GFP_KERNEL);
2290
2291 if (!txr->tx_push)
2292 return -ENOMEM;
2293
Michael Chanc0c050c2015-10-22 16:01:17 -04002294 mapping = txr->tx_push_mapping +
2295 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002296 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002297
Michael Chan4419dbe2016-02-10 17:33:49 -05002298 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002299 }
2300 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002301 if (i < bp->tx_nr_rings_xdp)
2302 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002303 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2304 j++;
2305 }
2306 return 0;
2307}
2308
2309static void bnxt_free_cp_rings(struct bnxt *bp)
2310{
2311 int i;
2312
2313 if (!bp->bnapi)
2314 return;
2315
2316 for (i = 0; i < bp->cp_nr_rings; i++) {
2317 struct bnxt_napi *bnapi = bp->bnapi[i];
2318 struct bnxt_cp_ring_info *cpr;
2319 struct bnxt_ring_struct *ring;
2320
2321 if (!bnapi)
2322 continue;
2323
2324 cpr = &bnapi->cp_ring;
2325 ring = &cpr->cp_ring_struct;
2326
2327 bnxt_free_ring(bp, ring);
2328 }
2329}
2330
2331static int bnxt_alloc_cp_rings(struct bnxt *bp)
2332{
2333 int i, rc;
2334
2335 for (i = 0; i < bp->cp_nr_rings; i++) {
2336 struct bnxt_napi *bnapi = bp->bnapi[i];
2337 struct bnxt_cp_ring_info *cpr;
2338 struct bnxt_ring_struct *ring;
2339
2340 if (!bnapi)
2341 continue;
2342
2343 cpr = &bnapi->cp_ring;
2344 ring = &cpr->cp_ring_struct;
2345
2346 rc = bnxt_alloc_ring(bp, ring);
2347 if (rc)
2348 return rc;
2349 }
2350 return 0;
2351}
2352
2353static void bnxt_init_ring_struct(struct bnxt *bp)
2354{
2355 int i;
2356
2357 for (i = 0; i < bp->cp_nr_rings; i++) {
2358 struct bnxt_napi *bnapi = bp->bnapi[i];
2359 struct bnxt_cp_ring_info *cpr;
2360 struct bnxt_rx_ring_info *rxr;
2361 struct bnxt_tx_ring_info *txr;
2362 struct bnxt_ring_struct *ring;
2363
2364 if (!bnapi)
2365 continue;
2366
2367 cpr = &bnapi->cp_ring;
2368 ring = &cpr->cp_ring_struct;
2369 ring->nr_pages = bp->cp_nr_pages;
2370 ring->page_size = HW_CMPD_RING_SIZE;
2371 ring->pg_arr = (void **)cpr->cp_desc_ring;
2372 ring->dma_arr = cpr->cp_desc_mapping;
2373 ring->vmem_size = 0;
2374
Michael Chanb6ab4b02016-01-02 23:44:59 -05002375 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002376 if (!rxr)
2377 goto skip_rx;
2378
Michael Chanc0c050c2015-10-22 16:01:17 -04002379 ring = &rxr->rx_ring_struct;
2380 ring->nr_pages = bp->rx_nr_pages;
2381 ring->page_size = HW_RXBD_RING_SIZE;
2382 ring->pg_arr = (void **)rxr->rx_desc_ring;
2383 ring->dma_arr = rxr->rx_desc_mapping;
2384 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2385 ring->vmem = (void **)&rxr->rx_buf_ring;
2386
2387 ring = &rxr->rx_agg_ring_struct;
2388 ring->nr_pages = bp->rx_agg_nr_pages;
2389 ring->page_size = HW_RXBD_RING_SIZE;
2390 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2391 ring->dma_arr = rxr->rx_agg_desc_mapping;
2392 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2393 ring->vmem = (void **)&rxr->rx_agg_ring;
2394
Michael Chan3b2b7d92016-01-02 23:45:00 -05002395skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002396 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002397 if (!txr)
2398 continue;
2399
Michael Chanc0c050c2015-10-22 16:01:17 -04002400 ring = &txr->tx_ring_struct;
2401 ring->nr_pages = bp->tx_nr_pages;
2402 ring->page_size = HW_RXBD_RING_SIZE;
2403 ring->pg_arr = (void **)txr->tx_desc_ring;
2404 ring->dma_arr = txr->tx_desc_mapping;
2405 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2406 ring->vmem = (void **)&txr->tx_buf_ring;
2407 }
2408}
2409
2410static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2411{
2412 int i;
2413 u32 prod;
2414 struct rx_bd **rx_buf_ring;
2415
2416 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2417 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2418 int j;
2419 struct rx_bd *rxbd;
2420
2421 rxbd = rx_buf_ring[i];
2422 if (!rxbd)
2423 continue;
2424
2425 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2426 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2427 rxbd->rx_bd_opaque = prod;
2428 }
2429 }
2430}
2431
2432static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2433{
2434 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002435 struct bnxt_rx_ring_info *rxr;
2436 struct bnxt_ring_struct *ring;
2437 u32 prod, type;
2438 int i;
2439
Michael Chanc0c050c2015-10-22 16:01:17 -04002440 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2441 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2442
2443 if (NET_IP_ALIGN == 2)
2444 type |= RX_BD_FLAGS_SOP;
2445
Michael Chanb6ab4b02016-01-02 23:44:59 -05002446 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002447 ring = &rxr->rx_ring_struct;
2448 bnxt_init_rxbd_pages(ring, type);
2449
Michael Chanc6d30e82017-02-06 16:55:42 -05002450 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2451 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2452 if (IS_ERR(rxr->xdp_prog)) {
2453 int rc = PTR_ERR(rxr->xdp_prog);
2454
2455 rxr->xdp_prog = NULL;
2456 return rc;
2457 }
2458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002459 prod = rxr->rx_prod;
2460 for (i = 0; i < bp->rx_ring_size; i++) {
2461 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2462 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2463 ring_nr, i, bp->rx_ring_size);
2464 break;
2465 }
2466 prod = NEXT_RX(prod);
2467 }
2468 rxr->rx_prod = prod;
2469 ring->fw_ring_id = INVALID_HW_RING_ID;
2470
Michael Chanedd0c2c2015-12-27 18:19:19 -05002471 ring = &rxr->rx_agg_ring_struct;
2472 ring->fw_ring_id = INVALID_HW_RING_ID;
2473
Michael Chanc0c050c2015-10-22 16:01:17 -04002474 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2475 return 0;
2476
Michael Chan2839f282016-04-25 02:30:50 -04002477 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002478 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2479
2480 bnxt_init_rxbd_pages(ring, type);
2481
2482 prod = rxr->rx_agg_prod;
2483 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2484 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2485 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2486 ring_nr, i, bp->rx_ring_size);
2487 break;
2488 }
2489 prod = NEXT_RX_AGG(prod);
2490 }
2491 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002492
2493 if (bp->flags & BNXT_FLAG_TPA) {
2494 if (rxr->rx_tpa) {
2495 u8 *data;
2496 dma_addr_t mapping;
2497
2498 for (i = 0; i < MAX_TPA; i++) {
2499 data = __bnxt_alloc_rx_data(bp, &mapping,
2500 GFP_KERNEL);
2501 if (!data)
2502 return -ENOMEM;
2503
2504 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002505 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002506 rxr->rx_tpa[i].mapping = mapping;
2507 }
2508 } else {
2509 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2510 return -ENOMEM;
2511 }
2512 }
2513
2514 return 0;
2515}
2516
Sankar Patchineelam22479252017-03-28 19:47:29 -04002517static void bnxt_init_cp_rings(struct bnxt *bp)
2518{
2519 int i;
2520
2521 for (i = 0; i < bp->cp_nr_rings; i++) {
2522 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2523 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2524
2525 ring->fw_ring_id = INVALID_HW_RING_ID;
2526 }
2527}
2528
Michael Chanc0c050c2015-10-22 16:01:17 -04002529static int bnxt_init_rx_rings(struct bnxt *bp)
2530{
2531 int i, rc = 0;
2532
Michael Chanc61fb992017-02-06 16:55:36 -05002533 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002534 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2535 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002536 } else {
2537 bp->rx_offset = BNXT_RX_OFFSET;
2538 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2539 }
Michael Chanb3dba772017-02-06 16:55:35 -05002540
Michael Chanc0c050c2015-10-22 16:01:17 -04002541 for (i = 0; i < bp->rx_nr_rings; i++) {
2542 rc = bnxt_init_one_rx_ring(bp, i);
2543 if (rc)
2544 break;
2545 }
2546
2547 return rc;
2548}
2549
2550static int bnxt_init_tx_rings(struct bnxt *bp)
2551{
2552 u16 i;
2553
2554 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2555 MAX_SKB_FRAGS + 1);
2556
2557 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002558 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002559 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2560
2561 ring->fw_ring_id = INVALID_HW_RING_ID;
2562 }
2563
2564 return 0;
2565}
2566
2567static void bnxt_free_ring_grps(struct bnxt *bp)
2568{
2569 kfree(bp->grp_info);
2570 bp->grp_info = NULL;
2571}
2572
2573static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2574{
2575 int i;
2576
2577 if (irq_re_init) {
2578 bp->grp_info = kcalloc(bp->cp_nr_rings,
2579 sizeof(struct bnxt_ring_grp_info),
2580 GFP_KERNEL);
2581 if (!bp->grp_info)
2582 return -ENOMEM;
2583 }
2584 for (i = 0; i < bp->cp_nr_rings; i++) {
2585 if (irq_re_init)
2586 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2587 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2588 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2589 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2590 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2591 }
2592 return 0;
2593}
2594
2595static void bnxt_free_vnics(struct bnxt *bp)
2596{
2597 kfree(bp->vnic_info);
2598 bp->vnic_info = NULL;
2599 bp->nr_vnics = 0;
2600}
2601
2602static int bnxt_alloc_vnics(struct bnxt *bp)
2603{
2604 int num_vnics = 1;
2605
2606#ifdef CONFIG_RFS_ACCEL
2607 if (bp->flags & BNXT_FLAG_RFS)
2608 num_vnics += bp->rx_nr_rings;
2609#endif
2610
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002611 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2612 num_vnics++;
2613
Michael Chanc0c050c2015-10-22 16:01:17 -04002614 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2615 GFP_KERNEL);
2616 if (!bp->vnic_info)
2617 return -ENOMEM;
2618
2619 bp->nr_vnics = num_vnics;
2620 return 0;
2621}
2622
2623static void bnxt_init_vnics(struct bnxt *bp)
2624{
2625 int i;
2626
2627 for (i = 0; i < bp->nr_vnics; i++) {
2628 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2629
2630 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002631 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2632 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002633 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2634
2635 if (bp->vnic_info[i].rss_hash_key) {
2636 if (i == 0)
2637 prandom_bytes(vnic->rss_hash_key,
2638 HW_HASH_KEY_SIZE);
2639 else
2640 memcpy(vnic->rss_hash_key,
2641 bp->vnic_info[0].rss_hash_key,
2642 HW_HASH_KEY_SIZE);
2643 }
2644 }
2645}
2646
2647static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2648{
2649 int pages;
2650
2651 pages = ring_size / desc_per_pg;
2652
2653 if (!pages)
2654 return 1;
2655
2656 pages++;
2657
2658 while (pages & (pages - 1))
2659 pages++;
2660
2661 return pages;
2662}
2663
Michael Chanc6d30e82017-02-06 16:55:42 -05002664void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002665{
2666 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002667 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2668 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002669 if (bp->dev->features & NETIF_F_LRO)
2670 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002671 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002672 bp->flags |= BNXT_FLAG_GRO;
2673}
2674
2675/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2676 * be set on entry.
2677 */
2678void bnxt_set_ring_params(struct bnxt *bp)
2679{
2680 u32 ring_size, rx_size, rx_space;
2681 u32 agg_factor = 0, agg_ring_size = 0;
2682
2683 /* 8 for CRC and VLAN */
2684 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2685
2686 rx_space = rx_size + NET_SKB_PAD +
2687 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2688
2689 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2690 ring_size = bp->rx_ring_size;
2691 bp->rx_agg_ring_size = 0;
2692 bp->rx_agg_nr_pages = 0;
2693
2694 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002695 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002696
2697 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002698 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002699 u32 jumbo_factor;
2700
2701 bp->flags |= BNXT_FLAG_JUMBO;
2702 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2703 if (jumbo_factor > agg_factor)
2704 agg_factor = jumbo_factor;
2705 }
2706 agg_ring_size = ring_size * agg_factor;
2707
2708 if (agg_ring_size) {
2709 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2710 RX_DESC_CNT);
2711 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2712 u32 tmp = agg_ring_size;
2713
2714 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2715 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2716 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2717 tmp, agg_ring_size);
2718 }
2719 bp->rx_agg_ring_size = agg_ring_size;
2720 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2721 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2722 rx_space = rx_size + NET_SKB_PAD +
2723 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2724 }
2725
2726 bp->rx_buf_use_size = rx_size;
2727 bp->rx_buf_size = rx_space;
2728
2729 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2730 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2731
2732 ring_size = bp->tx_ring_size;
2733 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2734 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2735
2736 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2737 bp->cp_ring_size = ring_size;
2738
2739 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2740 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2741 bp->cp_nr_pages = MAX_CP_PAGES;
2742 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2743 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2744 ring_size, bp->cp_ring_size);
2745 }
2746 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2747 bp->cp_ring_mask = bp->cp_bit - 1;
2748}
2749
Michael Chanc61fb992017-02-06 16:55:36 -05002750int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002751{
Michael Chanc61fb992017-02-06 16:55:36 -05002752 if (page_mode) {
2753 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2754 return -EOPNOTSUPP;
2755 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2756 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2757 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2758 bp->dev->hw_features &= ~NETIF_F_LRO;
2759 bp->dev->features &= ~NETIF_F_LRO;
2760 bp->rx_dir = DMA_BIDIRECTIONAL;
2761 bp->rx_skb_func = bnxt_rx_page_skb;
2762 } else {
2763 bp->dev->max_mtu = BNXT_MAX_MTU;
2764 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2765 bp->rx_dir = DMA_FROM_DEVICE;
2766 bp->rx_skb_func = bnxt_rx_skb;
2767 }
Michael Chan6bb19472017-02-06 16:55:32 -05002768 return 0;
2769}
2770
Michael Chanc0c050c2015-10-22 16:01:17 -04002771static void bnxt_free_vnic_attributes(struct bnxt *bp)
2772{
2773 int i;
2774 struct bnxt_vnic_info *vnic;
2775 struct pci_dev *pdev = bp->pdev;
2776
2777 if (!bp->vnic_info)
2778 return;
2779
2780 for (i = 0; i < bp->nr_vnics; i++) {
2781 vnic = &bp->vnic_info[i];
2782
2783 kfree(vnic->fw_grp_ids);
2784 vnic->fw_grp_ids = NULL;
2785
2786 kfree(vnic->uc_list);
2787 vnic->uc_list = NULL;
2788
2789 if (vnic->mc_list) {
2790 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2791 vnic->mc_list, vnic->mc_list_mapping);
2792 vnic->mc_list = NULL;
2793 }
2794
2795 if (vnic->rss_table) {
2796 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2797 vnic->rss_table,
2798 vnic->rss_table_dma_addr);
2799 vnic->rss_table = NULL;
2800 }
2801
2802 vnic->rss_hash_key = NULL;
2803 vnic->flags = 0;
2804 }
2805}
2806
2807static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2808{
2809 int i, rc = 0, size;
2810 struct bnxt_vnic_info *vnic;
2811 struct pci_dev *pdev = bp->pdev;
2812 int max_rings;
2813
2814 for (i = 0; i < bp->nr_vnics; i++) {
2815 vnic = &bp->vnic_info[i];
2816
2817 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2818 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2819
2820 if (mem_size > 0) {
2821 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2822 if (!vnic->uc_list) {
2823 rc = -ENOMEM;
2824 goto out;
2825 }
2826 }
2827 }
2828
2829 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2830 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2831 vnic->mc_list =
2832 dma_alloc_coherent(&pdev->dev,
2833 vnic->mc_list_size,
2834 &vnic->mc_list_mapping,
2835 GFP_KERNEL);
2836 if (!vnic->mc_list) {
2837 rc = -ENOMEM;
2838 goto out;
2839 }
2840 }
2841
2842 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2843 max_rings = bp->rx_nr_rings;
2844 else
2845 max_rings = 1;
2846
2847 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2848 if (!vnic->fw_grp_ids) {
2849 rc = -ENOMEM;
2850 goto out;
2851 }
2852
Michael Chanae10ae72016-12-29 12:13:38 -05002853 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2854 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2855 continue;
2856
Michael Chanc0c050c2015-10-22 16:01:17 -04002857 /* Allocate rss table and hash key */
2858 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2859 &vnic->rss_table_dma_addr,
2860 GFP_KERNEL);
2861 if (!vnic->rss_table) {
2862 rc = -ENOMEM;
2863 goto out;
2864 }
2865
2866 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2867
2868 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2869 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2870 }
2871 return 0;
2872
2873out:
2874 return rc;
2875}
2876
2877static void bnxt_free_hwrm_resources(struct bnxt *bp)
2878{
2879 struct pci_dev *pdev = bp->pdev;
2880
2881 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2882 bp->hwrm_cmd_resp_dma_addr);
2883
2884 bp->hwrm_cmd_resp_addr = NULL;
2885 if (bp->hwrm_dbg_resp_addr) {
2886 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2887 bp->hwrm_dbg_resp_addr,
2888 bp->hwrm_dbg_resp_dma_addr);
2889
2890 bp->hwrm_dbg_resp_addr = NULL;
2891 }
2892}
2893
2894static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2895{
2896 struct pci_dev *pdev = bp->pdev;
2897
2898 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2899 &bp->hwrm_cmd_resp_dma_addr,
2900 GFP_KERNEL);
2901 if (!bp->hwrm_cmd_resp_addr)
2902 return -ENOMEM;
2903 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2904 HWRM_DBG_REG_BUF_SIZE,
2905 &bp->hwrm_dbg_resp_dma_addr,
2906 GFP_KERNEL);
2907 if (!bp->hwrm_dbg_resp_addr)
2908 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2909
2910 return 0;
2911}
2912
Deepak Khungare605db82017-05-29 19:06:04 -04002913static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2914{
2915 if (bp->hwrm_short_cmd_req_addr) {
2916 struct pci_dev *pdev = bp->pdev;
2917
2918 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2919 bp->hwrm_short_cmd_req_addr,
2920 bp->hwrm_short_cmd_req_dma_addr);
2921 bp->hwrm_short_cmd_req_addr = NULL;
2922 }
2923}
2924
2925static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2926{
2927 struct pci_dev *pdev = bp->pdev;
2928
2929 bp->hwrm_short_cmd_req_addr =
2930 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2931 &bp->hwrm_short_cmd_req_dma_addr,
2932 GFP_KERNEL);
2933 if (!bp->hwrm_short_cmd_req_addr)
2934 return -ENOMEM;
2935
2936 return 0;
2937}
2938
Michael Chanc0c050c2015-10-22 16:01:17 -04002939static void bnxt_free_stats(struct bnxt *bp)
2940{
2941 u32 size, i;
2942 struct pci_dev *pdev = bp->pdev;
2943
Michael Chan3bdf56c2016-03-07 15:38:45 -05002944 if (bp->hw_rx_port_stats) {
2945 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2946 bp->hw_rx_port_stats,
2947 bp->hw_rx_port_stats_map);
2948 bp->hw_rx_port_stats = NULL;
2949 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2950 }
2951
Michael Chanc0c050c2015-10-22 16:01:17 -04002952 if (!bp->bnapi)
2953 return;
2954
2955 size = sizeof(struct ctx_hw_stats);
2956
2957 for (i = 0; i < bp->cp_nr_rings; i++) {
2958 struct bnxt_napi *bnapi = bp->bnapi[i];
2959 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2960
2961 if (cpr->hw_stats) {
2962 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2963 cpr->hw_stats_map);
2964 cpr->hw_stats = NULL;
2965 }
2966 }
2967}
2968
2969static int bnxt_alloc_stats(struct bnxt *bp)
2970{
2971 u32 size, i;
2972 struct pci_dev *pdev = bp->pdev;
2973
2974 size = sizeof(struct ctx_hw_stats);
2975
2976 for (i = 0; i < bp->cp_nr_rings; i++) {
2977 struct bnxt_napi *bnapi = bp->bnapi[i];
2978 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2979
2980 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2981 &cpr->hw_stats_map,
2982 GFP_KERNEL);
2983 if (!cpr->hw_stats)
2984 return -ENOMEM;
2985
2986 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2987 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002988
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002989 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002990 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2991 sizeof(struct tx_port_stats) + 1024;
2992
2993 bp->hw_rx_port_stats =
2994 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2995 &bp->hw_rx_port_stats_map,
2996 GFP_KERNEL);
2997 if (!bp->hw_rx_port_stats)
2998 return -ENOMEM;
2999
3000 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3001 512;
3002 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3003 sizeof(struct rx_port_stats) + 512;
3004 bp->flags |= BNXT_FLAG_PORT_STATS;
3005 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003006 return 0;
3007}
3008
3009static void bnxt_clear_ring_indices(struct bnxt *bp)
3010{
3011 int i;
3012
3013 if (!bp->bnapi)
3014 return;
3015
3016 for (i = 0; i < bp->cp_nr_rings; i++) {
3017 struct bnxt_napi *bnapi = bp->bnapi[i];
3018 struct bnxt_cp_ring_info *cpr;
3019 struct bnxt_rx_ring_info *rxr;
3020 struct bnxt_tx_ring_info *txr;
3021
3022 if (!bnapi)
3023 continue;
3024
3025 cpr = &bnapi->cp_ring;
3026 cpr->cp_raw_cons = 0;
3027
Michael Chanb6ab4b02016-01-02 23:44:59 -05003028 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003029 if (txr) {
3030 txr->tx_prod = 0;
3031 txr->tx_cons = 0;
3032 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003033
Michael Chanb6ab4b02016-01-02 23:44:59 -05003034 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003035 if (rxr) {
3036 rxr->rx_prod = 0;
3037 rxr->rx_agg_prod = 0;
3038 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003039 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003040 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003041 }
3042}
3043
3044static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3045{
3046#ifdef CONFIG_RFS_ACCEL
3047 int i;
3048
3049 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3050 * safe to delete the hash table.
3051 */
3052 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3053 struct hlist_head *head;
3054 struct hlist_node *tmp;
3055 struct bnxt_ntuple_filter *fltr;
3056
3057 head = &bp->ntp_fltr_hash_tbl[i];
3058 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3059 hlist_del(&fltr->hash);
3060 kfree(fltr);
3061 }
3062 }
3063 if (irq_reinit) {
3064 kfree(bp->ntp_fltr_bmap);
3065 bp->ntp_fltr_bmap = NULL;
3066 }
3067 bp->ntp_fltr_count = 0;
3068#endif
3069}
3070
3071static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3072{
3073#ifdef CONFIG_RFS_ACCEL
3074 int i, rc = 0;
3075
3076 if (!(bp->flags & BNXT_FLAG_RFS))
3077 return 0;
3078
3079 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3080 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3081
3082 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003083 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3084 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003085 GFP_KERNEL);
3086
3087 if (!bp->ntp_fltr_bmap)
3088 rc = -ENOMEM;
3089
3090 return rc;
3091#else
3092 return 0;
3093#endif
3094}
3095
3096static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3097{
3098 bnxt_free_vnic_attributes(bp);
3099 bnxt_free_tx_rings(bp);
3100 bnxt_free_rx_rings(bp);
3101 bnxt_free_cp_rings(bp);
3102 bnxt_free_ntp_fltrs(bp, irq_re_init);
3103 if (irq_re_init) {
3104 bnxt_free_stats(bp);
3105 bnxt_free_ring_grps(bp);
3106 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003107 kfree(bp->tx_ring_map);
3108 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003109 kfree(bp->tx_ring);
3110 bp->tx_ring = NULL;
3111 kfree(bp->rx_ring);
3112 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003113 kfree(bp->bnapi);
3114 bp->bnapi = NULL;
3115 } else {
3116 bnxt_clear_ring_indices(bp);
3117 }
3118}
3119
3120static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3121{
Michael Chan01657bc2016-01-02 23:45:03 -05003122 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003123 void *bnapi;
3124
3125 if (irq_re_init) {
3126 /* Allocate bnapi mem pointer array and mem block for
3127 * all queues
3128 */
3129 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3130 bp->cp_nr_rings);
3131 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3132 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3133 if (!bnapi)
3134 return -ENOMEM;
3135
3136 bp->bnapi = bnapi;
3137 bnapi += arr_size;
3138 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3139 bp->bnapi[i] = bnapi;
3140 bp->bnapi[i]->index = i;
3141 bp->bnapi[i]->bp = bp;
3142 }
3143
Michael Chanb6ab4b02016-01-02 23:44:59 -05003144 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3145 sizeof(struct bnxt_rx_ring_info),
3146 GFP_KERNEL);
3147 if (!bp->rx_ring)
3148 return -ENOMEM;
3149
3150 for (i = 0; i < bp->rx_nr_rings; i++) {
3151 bp->rx_ring[i].bnapi = bp->bnapi[i];
3152 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3153 }
3154
3155 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3156 sizeof(struct bnxt_tx_ring_info),
3157 GFP_KERNEL);
3158 if (!bp->tx_ring)
3159 return -ENOMEM;
3160
Michael Chana960dec2017-02-06 16:55:39 -05003161 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3162 GFP_KERNEL);
3163
3164 if (!bp->tx_ring_map)
3165 return -ENOMEM;
3166
Michael Chan01657bc2016-01-02 23:45:03 -05003167 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3168 j = 0;
3169 else
3170 j = bp->rx_nr_rings;
3171
3172 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3173 bp->tx_ring[i].bnapi = bp->bnapi[j];
3174 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003175 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003176 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003177 bp->tx_ring[i].txq_index = i -
3178 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003179 bp->bnapi[j]->tx_int = bnxt_tx_int;
3180 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003181 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003182 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3183 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003184 }
3185
Michael Chanc0c050c2015-10-22 16:01:17 -04003186 rc = bnxt_alloc_stats(bp);
3187 if (rc)
3188 goto alloc_mem_err;
3189
3190 rc = bnxt_alloc_ntp_fltrs(bp);
3191 if (rc)
3192 goto alloc_mem_err;
3193
3194 rc = bnxt_alloc_vnics(bp);
3195 if (rc)
3196 goto alloc_mem_err;
3197 }
3198
3199 bnxt_init_ring_struct(bp);
3200
3201 rc = bnxt_alloc_rx_rings(bp);
3202 if (rc)
3203 goto alloc_mem_err;
3204
3205 rc = bnxt_alloc_tx_rings(bp);
3206 if (rc)
3207 goto alloc_mem_err;
3208
3209 rc = bnxt_alloc_cp_rings(bp);
3210 if (rc)
3211 goto alloc_mem_err;
3212
3213 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3214 BNXT_VNIC_UCAST_FLAG;
3215 rc = bnxt_alloc_vnic_attributes(bp);
3216 if (rc)
3217 goto alloc_mem_err;
3218 return 0;
3219
3220alloc_mem_err:
3221 bnxt_free_mem(bp, true);
3222 return rc;
3223}
3224
Michael Chan9d8bc092016-12-29 12:13:33 -05003225static void bnxt_disable_int(struct bnxt *bp)
3226{
3227 int i;
3228
3229 if (!bp->bnapi)
3230 return;
3231
3232 for (i = 0; i < bp->cp_nr_rings; i++) {
3233 struct bnxt_napi *bnapi = bp->bnapi[i];
3234 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003235 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003236
Michael Chandaf1f1e2017-02-20 19:25:17 -05003237 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3238 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003239 }
3240}
3241
3242static void bnxt_disable_int_sync(struct bnxt *bp)
3243{
3244 int i;
3245
3246 atomic_inc(&bp->intr_sem);
3247
3248 bnxt_disable_int(bp);
3249 for (i = 0; i < bp->cp_nr_rings; i++)
3250 synchronize_irq(bp->irq_tbl[i].vector);
3251}
3252
3253static void bnxt_enable_int(struct bnxt *bp)
3254{
3255 int i;
3256
3257 atomic_set(&bp->intr_sem, 0);
3258 for (i = 0; i < bp->cp_nr_rings; i++) {
3259 struct bnxt_napi *bnapi = bp->bnapi[i];
3260 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3261
3262 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3263 }
3264}
3265
Michael Chanc0c050c2015-10-22 16:01:17 -04003266void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3267 u16 cmpl_ring, u16 target_id)
3268{
Michael Chana8643e12016-02-26 04:00:05 -05003269 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003270
Michael Chana8643e12016-02-26 04:00:05 -05003271 req->req_type = cpu_to_le16(req_type);
3272 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3273 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003274 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3275}
3276
Michael Chanfbfbc482016-02-26 04:00:07 -05003277static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3278 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003279{
Michael Chana11fa2b2016-05-15 03:04:47 -04003280 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003281 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003282 u32 *data = msg;
3283 __le32 *resp_len, *valid;
3284 u16 cp_ring_id, len = 0;
3285 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003286 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003287
Michael Chana8643e12016-02-26 04:00:05 -05003288 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003289 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003290 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003291 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3292
Deepak Khungare605db82017-05-29 19:06:04 -04003293 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3294 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3295 struct hwrm_short_input short_input = {0};
3296
3297 memcpy(short_cmd_req, req, msg_len);
3298 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3299 msg_len);
3300
3301 short_input.req_type = req->req_type;
3302 short_input.signature =
3303 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3304 short_input.size = cpu_to_le16(msg_len);
3305 short_input.req_addr =
3306 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3307
3308 data = (u32 *)&short_input;
3309 msg_len = sizeof(short_input);
3310
3311 /* Sync memory write before updating doorbell */
3312 wmb();
3313
3314 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3315 }
3316
Michael Chanc0c050c2015-10-22 16:01:17 -04003317 /* Write request msg to hwrm channel */
3318 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3319
Deepak Khungare605db82017-05-29 19:06:04 -04003320 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003321 writel(0, bp->bar0 + i);
3322
Michael Chanc0c050c2015-10-22 16:01:17 -04003323 /* currently supports only one outstanding message */
3324 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003325 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003326
3327 /* Ring channel doorbell */
3328 writel(1, bp->bar0 + 0x100);
3329
Michael Chanff4fe812016-02-26 04:00:04 -05003330 if (!timeout)
3331 timeout = DFLT_HWRM_CMD_TIMEOUT;
3332
Michael Chanc0c050c2015-10-22 16:01:17 -04003333 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003334 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003335 if (intr_process) {
3336 /* Wait until hwrm response cmpl interrupt is processed */
3337 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003338 i++ < tmo_count) {
3339 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003340 }
3341
3342 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3343 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003344 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003345 return -1;
3346 }
3347 } else {
3348 /* Check if response len is updated */
3349 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003350 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003351 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3352 HWRM_RESP_LEN_SFT;
3353 if (len)
3354 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003355 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003356 }
3357
Michael Chana11fa2b2016-05-15 03:04:47 -04003358 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003359 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003360 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003361 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003362 return -1;
3363 }
3364
3365 /* Last word of resp contains valid bit */
3366 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003367 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003368 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3369 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003370 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003371 }
3372
Michael Chana11fa2b2016-05-15 03:04:47 -04003373 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003374 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003375 timeout, le16_to_cpu(req->req_type),
3376 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003377 return -1;
3378 }
3379 }
3380
3381 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003382 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003383 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3384 le16_to_cpu(resp->req_type),
3385 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003386 return rc;
3387}
3388
3389int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3390{
3391 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003392}
3393
3394int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3395{
3396 int rc;
3397
3398 mutex_lock(&bp->hwrm_cmd_lock);
3399 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3400 mutex_unlock(&bp->hwrm_cmd_lock);
3401 return rc;
3402}
3403
Michael Chan90e209212016-02-26 04:00:08 -05003404int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3405 int timeout)
3406{
3407 int rc;
3408
3409 mutex_lock(&bp->hwrm_cmd_lock);
3410 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3411 mutex_unlock(&bp->hwrm_cmd_lock);
3412 return rc;
3413}
3414
Michael Chana1653b12016-12-07 00:26:20 -05003415int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3416 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003417{
3418 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003419 DECLARE_BITMAP(async_events_bmap, 256);
3420 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003421 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003422
3423 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3424
3425 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003426 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003427
Michael Chan25be8622016-04-05 14:09:00 -04003428 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3429 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3430 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3431
Michael Chana1653b12016-12-07 00:26:20 -05003432 if (bmap && bmap_size) {
3433 for (i = 0; i < bmap_size; i++) {
3434 if (test_bit(i, bmap))
3435 __set_bit(i, async_events_bmap);
3436 }
3437 }
3438
Michael Chan25be8622016-04-05 14:09:00 -04003439 for (i = 0; i < 8; i++)
3440 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3441
Michael Chana1653b12016-12-07 00:26:20 -05003442 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3443}
3444
3445static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3446{
3447 struct hwrm_func_drv_rgtr_input req = {0};
3448
3449 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3450
3451 req.enables =
3452 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3453 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3454
Michael Chan11f15ed2016-04-05 14:08:55 -04003455 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003456 req.ver_maj = DRV_VER_MAJ;
3457 req.ver_min = DRV_VER_MIN;
3458 req.ver_upd = DRV_VER_UPD;
3459
3460 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003461 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003462 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003463
Michael Chan9b0436c2017-07-11 13:05:36 -04003464 memset(data, 0, sizeof(data));
3465 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3466 u16 cmd = bnxt_vf_req_snif[i];
3467 unsigned int bit, idx;
3468
3469 idx = cmd / 32;
3470 bit = cmd % 32;
3471 data[idx] |= 1 << bit;
3472 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003473
Michael Chande68f5de2015-12-09 19:35:41 -05003474 for (i = 0; i < 8; i++)
3475 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3476
Michael Chanc0c050c2015-10-22 16:01:17 -04003477 req.enables |=
3478 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3479 }
3480
3481 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3482}
3483
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003484static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3485{
3486 struct hwrm_func_drv_unrgtr_input req = {0};
3487
3488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3489 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3490}
3491
Michael Chanc0c050c2015-10-22 16:01:17 -04003492static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3493{
3494 u32 rc = 0;
3495 struct hwrm_tunnel_dst_port_free_input req = {0};
3496
3497 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3498 req.tunnel_type = tunnel_type;
3499
3500 switch (tunnel_type) {
3501 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3502 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3503 break;
3504 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3505 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3506 break;
3507 default:
3508 break;
3509 }
3510
3511 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3512 if (rc)
3513 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3514 rc);
3515 return rc;
3516}
3517
3518static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3519 u8 tunnel_type)
3520{
3521 u32 rc = 0;
3522 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3523 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3524
3525 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3526
3527 req.tunnel_type = tunnel_type;
3528 req.tunnel_dst_port_val = port;
3529
3530 mutex_lock(&bp->hwrm_cmd_lock);
3531 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3532 if (rc) {
3533 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3534 rc);
3535 goto err_out;
3536 }
3537
Christophe Jaillet57aac712016-11-22 06:14:40 +01003538 switch (tunnel_type) {
3539 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003540 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003541 break;
3542 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003543 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003544 break;
3545 default:
3546 break;
3547 }
3548
Michael Chanc0c050c2015-10-22 16:01:17 -04003549err_out:
3550 mutex_unlock(&bp->hwrm_cmd_lock);
3551 return rc;
3552}
3553
3554static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3555{
3556 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3557 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3558
3559 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003560 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003561
3562 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3563 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3564 req.mask = cpu_to_le32(vnic->rx_mask);
3565 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3566}
3567
3568#ifdef CONFIG_RFS_ACCEL
3569static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3570 struct bnxt_ntuple_filter *fltr)
3571{
3572 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3573
3574 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3575 req.ntuple_filter_id = fltr->filter_id;
3576 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3577}
3578
3579#define BNXT_NTP_FLTR_FLAGS \
3580 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3581 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3582 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3583 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3584 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3585 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3586 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3587 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3588 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3589 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3590 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3591 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3592 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003593 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003594
Michael Chan61aad722017-02-12 19:18:14 -05003595#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3596 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3597
Michael Chanc0c050c2015-10-22 16:01:17 -04003598static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3599 struct bnxt_ntuple_filter *fltr)
3600{
3601 int rc = 0;
3602 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3603 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3604 bp->hwrm_cmd_resp_addr;
3605 struct flow_keys *keys = &fltr->fkeys;
3606 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3607
3608 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003609 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003610
3611 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3612
3613 req.ethertype = htons(ETH_P_IP);
3614 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003615 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003616 req.ip_protocol = keys->basic.ip_proto;
3617
Michael Chandda0e742016-12-29 12:13:40 -05003618 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3619 int i;
3620
3621 req.ethertype = htons(ETH_P_IPV6);
3622 req.ip_addr_type =
3623 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3624 *(struct in6_addr *)&req.src_ipaddr[0] =
3625 keys->addrs.v6addrs.src;
3626 *(struct in6_addr *)&req.dst_ipaddr[0] =
3627 keys->addrs.v6addrs.dst;
3628 for (i = 0; i < 4; i++) {
3629 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3630 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3631 }
3632 } else {
3633 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3634 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3635 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3636 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3637 }
Michael Chan61aad722017-02-12 19:18:14 -05003638 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3639 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3640 req.tunnel_type =
3641 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3642 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003643
3644 req.src_port = keys->ports.src;
3645 req.src_port_mask = cpu_to_be16(0xffff);
3646 req.dst_port = keys->ports.dst;
3647 req.dst_port_mask = cpu_to_be16(0xffff);
3648
Michael Chanc1935542015-12-27 18:19:28 -05003649 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003650 mutex_lock(&bp->hwrm_cmd_lock);
3651 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3652 if (!rc)
3653 fltr->filter_id = resp->ntuple_filter_id;
3654 mutex_unlock(&bp->hwrm_cmd_lock);
3655 return rc;
3656}
3657#endif
3658
3659static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3660 u8 *mac_addr)
3661{
3662 u32 rc = 0;
3663 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3664 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3665
3666 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003667 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3668 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3669 req.flags |=
3670 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003671 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003672 req.enables =
3673 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003674 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003675 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3676 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3677 req.l2_addr_mask[0] = 0xff;
3678 req.l2_addr_mask[1] = 0xff;
3679 req.l2_addr_mask[2] = 0xff;
3680 req.l2_addr_mask[3] = 0xff;
3681 req.l2_addr_mask[4] = 0xff;
3682 req.l2_addr_mask[5] = 0xff;
3683
3684 mutex_lock(&bp->hwrm_cmd_lock);
3685 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3686 if (!rc)
3687 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3688 resp->l2_filter_id;
3689 mutex_unlock(&bp->hwrm_cmd_lock);
3690 return rc;
3691}
3692
3693static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3694{
3695 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3696 int rc = 0;
3697
3698 /* Any associated ntuple filters will also be cleared by firmware. */
3699 mutex_lock(&bp->hwrm_cmd_lock);
3700 for (i = 0; i < num_of_vnics; i++) {
3701 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3702
3703 for (j = 0; j < vnic->uc_filter_count; j++) {
3704 struct hwrm_cfa_l2_filter_free_input req = {0};
3705
3706 bnxt_hwrm_cmd_hdr_init(bp, &req,
3707 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3708
3709 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3710
3711 rc = _hwrm_send_message(bp, &req, sizeof(req),
3712 HWRM_CMD_TIMEOUT);
3713 }
3714 vnic->uc_filter_count = 0;
3715 }
3716 mutex_unlock(&bp->hwrm_cmd_lock);
3717
3718 return rc;
3719}
3720
3721static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3722{
3723 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3724 struct hwrm_vnic_tpa_cfg_input req = {0};
3725
3726 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3727
3728 if (tpa_flags) {
3729 u16 mss = bp->dev->mtu - 40;
3730 u32 nsegs, n, segs = 0, flags;
3731
3732 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3733 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3734 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3735 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3736 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3737 if (tpa_flags & BNXT_FLAG_GRO)
3738 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3739
3740 req.flags = cpu_to_le32(flags);
3741
3742 req.enables =
3743 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003744 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3745 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003746
3747 /* Number of segs are log2 units, and first packet is not
3748 * included as part of this units.
3749 */
Michael Chan2839f282016-04-25 02:30:50 -04003750 if (mss <= BNXT_RX_PAGE_SIZE) {
3751 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003752 nsegs = (MAX_SKB_FRAGS - 1) * n;
3753 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003754 n = mss / BNXT_RX_PAGE_SIZE;
3755 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003756 n++;
3757 nsegs = (MAX_SKB_FRAGS - n) / n;
3758 }
3759
3760 segs = ilog2(nsegs);
3761 req.max_agg_segs = cpu_to_le16(segs);
3762 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003763
3764 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003765 }
3766 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3767
3768 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3769}
3770
3771static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3772{
3773 u32 i, j, max_rings;
3774 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3775 struct hwrm_vnic_rss_cfg_input req = {0};
3776
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003777 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003778 return 0;
3779
3780 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3781 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003782 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003783 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3784 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3785 max_rings = bp->rx_nr_rings - 1;
3786 else
3787 max_rings = bp->rx_nr_rings;
3788 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003789 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003790 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003791
3792 /* Fill the RSS indirection table with ring group ids */
3793 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3794 if (j == max_rings)
3795 j = 0;
3796 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3797 }
3798
3799 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3800 req.hash_key_tbl_addr =
3801 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3802 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003803 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003804 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3805}
3806
3807static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3808{
3809 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3810 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3811
3812 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3813 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3814 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3815 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3816 req.enables =
3817 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3818 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3819 /* thresholds not implemented in firmware yet */
3820 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3821 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3822 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3823 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3824}
3825
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003826static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3827 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003828{
3829 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3830
3831 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3832 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003833 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003834
3835 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003836 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003837}
3838
3839static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3840{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003841 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003842
3843 for (i = 0; i < bp->nr_vnics; i++) {
3844 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3845
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003846 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3847 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3848 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3849 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003850 }
3851 bp->rsscos_nr_ctxs = 0;
3852}
3853
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003854static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003855{
3856 int rc;
3857 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3858 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3859 bp->hwrm_cmd_resp_addr;
3860
3861 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3862 -1);
3863
3864 mutex_lock(&bp->hwrm_cmd_lock);
3865 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3866 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003867 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003868 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3869 mutex_unlock(&bp->hwrm_cmd_lock);
3870
3871 return rc;
3872}
3873
Michael Chana588e452016-12-07 00:26:21 -05003874int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003875{
Michael Chanb81a90d2016-01-02 23:45:01 -05003876 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003877 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3878 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003879 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003880
3881 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003882
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003883 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3884 /* Only RSS support for now TBD: COS & LB */
3885 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3886 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3887 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3888 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003889 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3890 req.rss_rule =
3891 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3892 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3893 VNIC_CFG_REQ_ENABLES_MRU);
3894 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003895 } else {
3896 req.rss_rule = cpu_to_le16(0xffff);
3897 }
3898
3899 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3900 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003901 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3902 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3903 } else {
3904 req.cos_rule = cpu_to_le16(0xffff);
3905 }
3906
Michael Chanc0c050c2015-10-22 16:01:17 -04003907 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003908 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003909 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003910 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003911 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3912 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003913
Michael Chanb81a90d2016-01-02 23:45:01 -05003914 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003915 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3916 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3917
3918 req.lb_rule = cpu_to_le16(0xffff);
3919 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3920 VLAN_HLEN);
3921
Michael Chancf6645f2016-06-13 02:25:28 -04003922#ifdef CONFIG_BNXT_SRIOV
3923 if (BNXT_VF(bp))
3924 def_vlan = bp->vf.vlan;
3925#endif
3926 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003927 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003928 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3929 req.flags |=
3930 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003931
3932 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3933}
3934
3935static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3936{
3937 u32 rc = 0;
3938
3939 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3940 struct hwrm_vnic_free_input req = {0};
3941
3942 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3943 req.vnic_id =
3944 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3945
3946 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3947 if (rc)
3948 return rc;
3949 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3950 }
3951 return rc;
3952}
3953
3954static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3955{
3956 u16 i;
3957
3958 for (i = 0; i < bp->nr_vnics; i++)
3959 bnxt_hwrm_vnic_free_one(bp, i);
3960}
3961
Michael Chanb81a90d2016-01-02 23:45:01 -05003962static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3963 unsigned int start_rx_ring_idx,
3964 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003965{
Michael Chanb81a90d2016-01-02 23:45:01 -05003966 int rc = 0;
3967 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003968 struct hwrm_vnic_alloc_input req = {0};
3969 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3970
3971 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003972 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3973 grp_idx = bp->rx_ring[i].bnapi->index;
3974 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003975 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003976 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003977 break;
3978 }
3979 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003980 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003981 }
3982
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003983 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3984 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003985 if (vnic_id == 0)
3986 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3987
3988 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3989
3990 mutex_lock(&bp->hwrm_cmd_lock);
3991 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3992 if (!rc)
3993 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3994 mutex_unlock(&bp->hwrm_cmd_lock);
3995 return rc;
3996}
3997
Michael Chan8fdefd62016-12-29 12:13:36 -05003998static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3999{
4000 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4001 struct hwrm_vnic_qcaps_input req = {0};
4002 int rc;
4003
4004 if (bp->hwrm_spec_code < 0x10600)
4005 return 0;
4006
4007 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4008 mutex_lock(&bp->hwrm_cmd_lock);
4009 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4010 if (!rc) {
4011 if (resp->flags &
4012 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4013 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4014 }
4015 mutex_unlock(&bp->hwrm_cmd_lock);
4016 return rc;
4017}
4018
Michael Chanc0c050c2015-10-22 16:01:17 -04004019static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4020{
4021 u16 i;
4022 u32 rc = 0;
4023
4024 mutex_lock(&bp->hwrm_cmd_lock);
4025 for (i = 0; i < bp->rx_nr_rings; i++) {
4026 struct hwrm_ring_grp_alloc_input req = {0};
4027 struct hwrm_ring_grp_alloc_output *resp =
4028 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004029 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004030
4031 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4032
Michael Chanb81a90d2016-01-02 23:45:01 -05004033 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4034 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4035 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4036 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004037
4038 rc = _hwrm_send_message(bp, &req, sizeof(req),
4039 HWRM_CMD_TIMEOUT);
4040 if (rc)
4041 break;
4042
Michael Chanb81a90d2016-01-02 23:45:01 -05004043 bp->grp_info[grp_idx].fw_grp_id =
4044 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004045 }
4046 mutex_unlock(&bp->hwrm_cmd_lock);
4047 return rc;
4048}
4049
4050static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4051{
4052 u16 i;
4053 u32 rc = 0;
4054 struct hwrm_ring_grp_free_input req = {0};
4055
4056 if (!bp->grp_info)
4057 return 0;
4058
4059 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4060
4061 mutex_lock(&bp->hwrm_cmd_lock);
4062 for (i = 0; i < bp->cp_nr_rings; i++) {
4063 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4064 continue;
4065 req.ring_group_id =
4066 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4067
4068 rc = _hwrm_send_message(bp, &req, sizeof(req),
4069 HWRM_CMD_TIMEOUT);
4070 if (rc)
4071 break;
4072 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4073 }
4074 mutex_unlock(&bp->hwrm_cmd_lock);
4075 return rc;
4076}
4077
4078static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4079 struct bnxt_ring_struct *ring,
4080 u32 ring_type, u32 map_index,
4081 u32 stats_ctx_id)
4082{
4083 int rc = 0, err = 0;
4084 struct hwrm_ring_alloc_input req = {0};
4085 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4086 u16 ring_id;
4087
4088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4089
4090 req.enables = 0;
4091 if (ring->nr_pages > 1) {
4092 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4093 /* Page size is in log2 units */
4094 req.page_size = BNXT_PAGE_SHIFT;
4095 req.page_tbl_depth = 1;
4096 } else {
4097 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4098 }
4099 req.fbo = 0;
4100 /* Association of ring index with doorbell index and MSIX number */
4101 req.logical_id = cpu_to_le16(map_index);
4102
4103 switch (ring_type) {
4104 case HWRM_RING_ALLOC_TX:
4105 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4106 /* Association of transmit ring with completion ring */
4107 req.cmpl_ring_id =
4108 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4109 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4110 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4111 req.queue_id = cpu_to_le16(ring->queue_id);
4112 break;
4113 case HWRM_RING_ALLOC_RX:
4114 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4115 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4116 break;
4117 case HWRM_RING_ALLOC_AGG:
4118 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4119 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4120 break;
4121 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004122 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004123 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4124 if (bp->flags & BNXT_FLAG_USING_MSIX)
4125 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4126 break;
4127 default:
4128 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4129 ring_type);
4130 return -1;
4131 }
4132
4133 mutex_lock(&bp->hwrm_cmd_lock);
4134 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4135 err = le16_to_cpu(resp->error_code);
4136 ring_id = le16_to_cpu(resp->ring_id);
4137 mutex_unlock(&bp->hwrm_cmd_lock);
4138
4139 if (rc || err) {
4140 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004141 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004142 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4143 rc, err);
4144 return -1;
4145
4146 case RING_FREE_REQ_RING_TYPE_RX:
4147 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4148 rc, err);
4149 return -1;
4150
4151 case RING_FREE_REQ_RING_TYPE_TX:
4152 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4153 rc, err);
4154 return -1;
4155
4156 default:
4157 netdev_err(bp->dev, "Invalid ring\n");
4158 return -1;
4159 }
4160 }
4161 ring->fw_ring_id = ring_id;
4162 return rc;
4163}
4164
Michael Chan486b5c22016-12-29 12:13:42 -05004165static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4166{
4167 int rc;
4168
4169 if (BNXT_PF(bp)) {
4170 struct hwrm_func_cfg_input req = {0};
4171
4172 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4173 req.fid = cpu_to_le16(0xffff);
4174 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4175 req.async_event_cr = cpu_to_le16(idx);
4176 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4177 } else {
4178 struct hwrm_func_vf_cfg_input req = {0};
4179
4180 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4181 req.enables =
4182 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4183 req.async_event_cr = cpu_to_le16(idx);
4184 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4185 }
4186 return rc;
4187}
4188
Michael Chanc0c050c2015-10-22 16:01:17 -04004189static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4190{
4191 int i, rc = 0;
4192
Michael Chanedd0c2c2015-12-27 18:19:19 -05004193 for (i = 0; i < bp->cp_nr_rings; i++) {
4194 struct bnxt_napi *bnapi = bp->bnapi[i];
4195 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4196 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004197
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004198 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004199 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4200 INVALID_STATS_CTX_ID);
4201 if (rc)
4202 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004203 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4204 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004205
4206 if (!i) {
4207 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4208 if (rc)
4209 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4210 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004211 }
4212
Michael Chanedd0c2c2015-12-27 18:19:19 -05004213 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004214 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004215 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004216 u32 map_idx = txr->bnapi->index;
4217 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004218
Michael Chanb81a90d2016-01-02 23:45:01 -05004219 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4220 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004221 if (rc)
4222 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004223 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004224 }
4225
Michael Chanedd0c2c2015-12-27 18:19:19 -05004226 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004227 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004228 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004229 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004230
Michael Chanb81a90d2016-01-02 23:45:01 -05004231 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4232 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004233 if (rc)
4234 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004235 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004236 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004237 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004238 }
4239
4240 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4241 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004242 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004243 struct bnxt_ring_struct *ring =
4244 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004245 u32 grp_idx = rxr->bnapi->index;
4246 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004247
4248 rc = hwrm_ring_alloc_send_msg(bp, ring,
4249 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004250 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004251 INVALID_STATS_CTX_ID);
4252 if (rc)
4253 goto err_out;
4254
Michael Chanb81a90d2016-01-02 23:45:01 -05004255 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004256 writel(DB_KEY_RX | rxr->rx_agg_prod,
4257 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004258 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004259 }
4260 }
4261err_out:
4262 return rc;
4263}
4264
4265static int hwrm_ring_free_send_msg(struct bnxt *bp,
4266 struct bnxt_ring_struct *ring,
4267 u32 ring_type, int cmpl_ring_id)
4268{
4269 int rc;
4270 struct hwrm_ring_free_input req = {0};
4271 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4272 u16 error_code;
4273
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004274 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004275 req.ring_type = ring_type;
4276 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4277
4278 mutex_lock(&bp->hwrm_cmd_lock);
4279 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4280 error_code = le16_to_cpu(resp->error_code);
4281 mutex_unlock(&bp->hwrm_cmd_lock);
4282
4283 if (rc || error_code) {
4284 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004285 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004286 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4287 rc);
4288 return rc;
4289 case RING_FREE_REQ_RING_TYPE_RX:
4290 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4291 rc);
4292 return rc;
4293 case RING_FREE_REQ_RING_TYPE_TX:
4294 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4295 rc);
4296 return rc;
4297 default:
4298 netdev_err(bp->dev, "Invalid ring\n");
4299 return -1;
4300 }
4301 }
4302 return 0;
4303}
4304
Michael Chanedd0c2c2015-12-27 18:19:19 -05004305static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004306{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004307 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004308
4309 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004310 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004311
Michael Chanedd0c2c2015-12-27 18:19:19 -05004312 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004313 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004314 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004315 u32 grp_idx = txr->bnapi->index;
4316 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004317
Michael Chanedd0c2c2015-12-27 18:19:19 -05004318 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4319 hwrm_ring_free_send_msg(bp, ring,
4320 RING_FREE_REQ_RING_TYPE_TX,
4321 close_path ? cmpl_ring_id :
4322 INVALID_HW_RING_ID);
4323 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004324 }
4325 }
4326
Michael Chanedd0c2c2015-12-27 18:19:19 -05004327 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004328 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004329 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004330 u32 grp_idx = rxr->bnapi->index;
4331 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004332
Michael Chanedd0c2c2015-12-27 18:19:19 -05004333 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4334 hwrm_ring_free_send_msg(bp, ring,
4335 RING_FREE_REQ_RING_TYPE_RX,
4336 close_path ? cmpl_ring_id :
4337 INVALID_HW_RING_ID);
4338 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004339 bp->grp_info[grp_idx].rx_fw_ring_id =
4340 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004341 }
4342 }
4343
Michael Chanedd0c2c2015-12-27 18:19:19 -05004344 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004345 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004346 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004347 u32 grp_idx = rxr->bnapi->index;
4348 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004349
Michael Chanedd0c2c2015-12-27 18:19:19 -05004350 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4351 hwrm_ring_free_send_msg(bp, ring,
4352 RING_FREE_REQ_RING_TYPE_RX,
4353 close_path ? cmpl_ring_id :
4354 INVALID_HW_RING_ID);
4355 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004356 bp->grp_info[grp_idx].agg_fw_ring_id =
4357 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004358 }
4359 }
4360
Michael Chan9d8bc092016-12-29 12:13:33 -05004361 /* The completion rings are about to be freed. After that the
4362 * IRQ doorbell will not work anymore. So we need to disable
4363 * IRQ here.
4364 */
4365 bnxt_disable_int_sync(bp);
4366
Michael Chanedd0c2c2015-12-27 18:19:19 -05004367 for (i = 0; i < bp->cp_nr_rings; i++) {
4368 struct bnxt_napi *bnapi = bp->bnapi[i];
4369 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4370 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004371
Michael Chanedd0c2c2015-12-27 18:19:19 -05004372 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4373 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004374 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004375 INVALID_HW_RING_ID);
4376 ring->fw_ring_id = INVALID_HW_RING_ID;
4377 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004378 }
4379 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004380}
4381
Michael Chan391be5c2016-12-29 12:13:41 -05004382/* Caller must hold bp->hwrm_cmd_lock */
4383int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4384{
4385 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4386 struct hwrm_func_qcfg_input req = {0};
4387 int rc;
4388
4389 if (bp->hwrm_spec_code < 0x10601)
4390 return 0;
4391
4392 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4393 req.fid = cpu_to_le16(fid);
4394 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4395 if (!rc)
4396 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4397
4398 return rc;
4399}
4400
Michael Chand1e79252017-02-06 16:55:38 -05004401static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004402{
4403 struct hwrm_func_cfg_input req = {0};
4404 int rc;
4405
4406 if (bp->hwrm_spec_code < 0x10601)
4407 return 0;
4408
4409 if (BNXT_VF(bp))
4410 return 0;
4411
4412 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4413 req.fid = cpu_to_le16(0xffff);
4414 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4415 req.num_tx_rings = cpu_to_le16(*tx_rings);
4416 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4417 if (rc)
4418 return rc;
4419
4420 mutex_lock(&bp->hwrm_cmd_lock);
4421 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4422 mutex_unlock(&bp->hwrm_cmd_lock);
4423 return rc;
4424}
4425
Michael Chanbb053f52016-02-26 04:00:02 -05004426static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4427 u32 buf_tmrs, u16 flags,
4428 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4429{
4430 req->flags = cpu_to_le16(flags);
4431 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4432 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4433 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4434 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4435 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4436 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4437 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4438 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4439}
4440
Michael Chanc0c050c2015-10-22 16:01:17 -04004441int bnxt_hwrm_set_coal(struct bnxt *bp)
4442{
4443 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004444 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4445 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004446 u16 max_buf, max_buf_irq;
4447 u16 buf_tmr, buf_tmr_irq;
4448 u32 flags;
4449
Michael Chandfc9c942016-02-26 04:00:03 -05004450 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4451 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4452 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4453 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004454
Michael Chandfb5b892016-02-26 04:00:01 -05004455 /* Each rx completion (2 records) should be DMAed immediately.
4456 * DMA 1/4 of the completion buffers at a time.
4457 */
4458 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004459 /* max_buf must not be zero */
4460 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004461 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4462 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4463 /* buf timer set to 1/4 of interrupt timer */
4464 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4465 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4466 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004467
4468 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4469
4470 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4471 * if coal_ticks is less than 25 us.
4472 */
Michael Chandfb5b892016-02-26 04:00:01 -05004473 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004474 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4475
Michael Chanbb053f52016-02-26 04:00:02 -05004476 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004477 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4478
4479 /* max_buf must not be zero */
4480 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4481 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4482 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4483 /* buf timer set to 1/4 of interrupt timer */
4484 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4485 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4486 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4487
4488 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4489 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4490 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004491
4492 mutex_lock(&bp->hwrm_cmd_lock);
4493 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004494 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004495
Michael Chandfc9c942016-02-26 04:00:03 -05004496 req = &req_rx;
4497 if (!bnapi->rx_ring)
4498 req = &req_tx;
4499 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4500
4501 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004502 HWRM_CMD_TIMEOUT);
4503 if (rc)
4504 break;
4505 }
4506 mutex_unlock(&bp->hwrm_cmd_lock);
4507 return rc;
4508}
4509
4510static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4511{
4512 int rc = 0, i;
4513 struct hwrm_stat_ctx_free_input req = {0};
4514
4515 if (!bp->bnapi)
4516 return 0;
4517
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004518 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4519 return 0;
4520
Michael Chanc0c050c2015-10-22 16:01:17 -04004521 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4522
4523 mutex_lock(&bp->hwrm_cmd_lock);
4524 for (i = 0; i < bp->cp_nr_rings; i++) {
4525 struct bnxt_napi *bnapi = bp->bnapi[i];
4526 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4527
4528 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4529 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4530
4531 rc = _hwrm_send_message(bp, &req, sizeof(req),
4532 HWRM_CMD_TIMEOUT);
4533 if (rc)
4534 break;
4535
4536 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4537 }
4538 }
4539 mutex_unlock(&bp->hwrm_cmd_lock);
4540 return rc;
4541}
4542
4543static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4544{
4545 int rc = 0, i;
4546 struct hwrm_stat_ctx_alloc_input req = {0};
4547 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4548
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004549 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4550 return 0;
4551
Michael Chanc0c050c2015-10-22 16:01:17 -04004552 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4553
Michael Chan51f30782016-07-01 18:46:29 -04004554 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004555
4556 mutex_lock(&bp->hwrm_cmd_lock);
4557 for (i = 0; i < bp->cp_nr_rings; i++) {
4558 struct bnxt_napi *bnapi = bp->bnapi[i];
4559 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4560
4561 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4562
4563 rc = _hwrm_send_message(bp, &req, sizeof(req),
4564 HWRM_CMD_TIMEOUT);
4565 if (rc)
4566 break;
4567
4568 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4569
4570 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4571 }
4572 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004573 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004574}
4575
Michael Chancf6645f2016-06-13 02:25:28 -04004576static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4577{
4578 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004579 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004580 int rc;
4581
4582 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4583 req.fid = cpu_to_le16(0xffff);
4584 mutex_lock(&bp->hwrm_cmd_lock);
4585 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4586 if (rc)
4587 goto func_qcfg_exit;
4588
4589#ifdef CONFIG_BNXT_SRIOV
4590 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004591 struct bnxt_vf_info *vf = &bp->vf;
4592
4593 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4594 }
4595#endif
Deepak Khungar9e54e322017-04-21 20:11:26 -04004596 if (BNXT_PF(bp)) {
4597 u16 flags = le16_to_cpu(resp->flags);
4598
4599 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4600 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED))
4601 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4602 if (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
4603 bp->flags |= BNXT_FLAG_MULTI_HOST;
4604 }
Michael Chanbc39f882017-03-08 18:44:34 -05004605
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004606 switch (resp->port_partition_type) {
4607 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4608 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4609 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4610 bp->port_partition_type = resp->port_partition_type;
4611 break;
4612 }
Michael Chancf6645f2016-06-13 02:25:28 -04004613
4614func_qcfg_exit:
4615 mutex_unlock(&bp->hwrm_cmd_lock);
4616 return rc;
4617}
4618
Michael Chan7b08f662016-12-07 00:26:18 -05004619static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004620{
4621 int rc = 0;
4622 struct hwrm_func_qcaps_input req = {0};
4623 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4624
4625 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4626 req.fid = cpu_to_le16(0xffff);
4627
4628 mutex_lock(&bp->hwrm_cmd_lock);
4629 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4630 if (rc)
4631 goto hwrm_func_qcaps_exit;
4632
Michael Chane4060d32016-12-07 00:26:19 -05004633 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4634 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4635 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4636 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4637
Michael Chan7cc5a202016-09-19 03:58:05 -04004638 bp->tx_push_thresh = 0;
4639 if (resp->flags &
4640 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4641 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4642
Michael Chanc0c050c2015-10-22 16:01:17 -04004643 if (BNXT_PF(bp)) {
4644 struct bnxt_pf_info *pf = &bp->pf;
4645
4646 pf->fw_fid = le16_to_cpu(resp->fid);
4647 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004648 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004649 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004650 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004651 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4652 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4653 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004654 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004655 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4656 if (!pf->max_hw_ring_grps)
4657 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004658 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4659 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4660 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4661 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4662 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4663 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4664 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4665 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4666 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4667 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4668 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004669 if (resp->flags &
4670 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4671 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004672 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004673#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004674 struct bnxt_vf_info *vf = &bp->vf;
4675
4676 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004677
4678 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4679 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4680 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4681 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004682 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4683 if (!vf->max_hw_ring_grps)
4684 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004685 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4686 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4687 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004688
4689 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004690 mutex_unlock(&bp->hwrm_cmd_lock);
4691
4692 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004693 /* overwrite netdev dev_adr with admin VF MAC */
4694 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004695 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004696 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004697 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4698 }
4699 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004700#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004701 }
4702
Michael Chanc0c050c2015-10-22 16:01:17 -04004703hwrm_func_qcaps_exit:
4704 mutex_unlock(&bp->hwrm_cmd_lock);
4705 return rc;
4706}
4707
4708static int bnxt_hwrm_func_reset(struct bnxt *bp)
4709{
4710 struct hwrm_func_reset_input req = {0};
4711
4712 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4713 req.enables = 0;
4714
4715 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4716}
4717
4718static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4719{
4720 int rc = 0;
4721 struct hwrm_queue_qportcfg_input req = {0};
4722 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4723 u8 i, *qptr;
4724
4725 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4726
4727 mutex_lock(&bp->hwrm_cmd_lock);
4728 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4729 if (rc)
4730 goto qportcfg_exit;
4731
4732 if (!resp->max_configurable_queues) {
4733 rc = -EINVAL;
4734 goto qportcfg_exit;
4735 }
4736 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004737 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004738 if (bp->max_tc > BNXT_MAX_QUEUE)
4739 bp->max_tc = BNXT_MAX_QUEUE;
4740
Michael Chan441cabb2016-09-19 03:58:02 -04004741 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4742 bp->max_tc = 1;
4743
Michael Chan87c374d2016-12-02 21:17:16 -05004744 if (bp->max_lltc > bp->max_tc)
4745 bp->max_lltc = bp->max_tc;
4746
Michael Chanc0c050c2015-10-22 16:01:17 -04004747 qptr = &resp->queue_id0;
4748 for (i = 0; i < bp->max_tc; i++) {
4749 bp->q_info[i].queue_id = *qptr++;
4750 bp->q_info[i].queue_profile = *qptr++;
4751 }
4752
4753qportcfg_exit:
4754 mutex_unlock(&bp->hwrm_cmd_lock);
4755 return rc;
4756}
4757
4758static int bnxt_hwrm_ver_get(struct bnxt *bp)
4759{
4760 int rc;
4761 struct hwrm_ver_get_input req = {0};
4762 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004763 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004764
Michael Chane6ef2692016-03-28 19:46:05 -04004765 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004766 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4767 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4768 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4769 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4770 mutex_lock(&bp->hwrm_cmd_lock);
4771 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4772 if (rc)
4773 goto hwrm_ver_get_exit;
4774
4775 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4776
Michael Chan11f15ed2016-04-05 14:08:55 -04004777 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4778 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004779 if (resp->hwrm_intf_maj < 1) {
4780 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004781 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004782 resp->hwrm_intf_upd);
4783 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004784 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004785 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004786 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4787 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4788
Michael Chanff4fe812016-02-26 04:00:04 -05004789 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4790 if (!bp->hwrm_cmd_timeout)
4791 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4792
Michael Chane6ef2692016-03-28 19:46:05 -04004793 if (resp->hwrm_intf_maj >= 1)
4794 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4795
Michael Chan659c8052016-06-13 02:25:33 -04004796 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004797 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4798 !resp->chip_metal)
4799 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004800
Deepak Khungare605db82017-05-29 19:06:04 -04004801 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4802 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4803 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4804 bp->flags |= BNXT_FLAG_SHORT_CMD;
4805
Michael Chanc0c050c2015-10-22 16:01:17 -04004806hwrm_ver_get_exit:
4807 mutex_unlock(&bp->hwrm_cmd_lock);
4808 return rc;
4809}
4810
Rob Swindell5ac67d82016-09-19 03:58:03 -04004811int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4812{
Rob Swindell878786d2016-09-20 03:36:33 -04004813#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004814 struct hwrm_fw_set_time_input req = {0};
4815 struct rtc_time tm;
4816 struct timeval tv;
4817
4818 if (bp->hwrm_spec_code < 0x10400)
4819 return -EOPNOTSUPP;
4820
4821 do_gettimeofday(&tv);
4822 rtc_time_to_tm(tv.tv_sec, &tm);
4823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4824 req.year = cpu_to_le16(1900 + tm.tm_year);
4825 req.month = 1 + tm.tm_mon;
4826 req.day = tm.tm_mday;
4827 req.hour = tm.tm_hour;
4828 req.minute = tm.tm_min;
4829 req.second = tm.tm_sec;
4830 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004831#else
4832 return -EOPNOTSUPP;
4833#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004834}
4835
Michael Chan3bdf56c2016-03-07 15:38:45 -05004836static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4837{
4838 int rc;
4839 struct bnxt_pf_info *pf = &bp->pf;
4840 struct hwrm_port_qstats_input req = {0};
4841
4842 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4843 return 0;
4844
4845 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4846 req.port_id = cpu_to_le16(pf->port_id);
4847 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4848 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4849 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4850 return rc;
4851}
4852
Michael Chanc0c050c2015-10-22 16:01:17 -04004853static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4854{
4855 if (bp->vxlan_port_cnt) {
4856 bnxt_hwrm_tunnel_dst_port_free(
4857 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4858 }
4859 bp->vxlan_port_cnt = 0;
4860 if (bp->nge_port_cnt) {
4861 bnxt_hwrm_tunnel_dst_port_free(
4862 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4863 }
4864 bp->nge_port_cnt = 0;
4865}
4866
4867static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4868{
4869 int rc, i;
4870 u32 tpa_flags = 0;
4871
4872 if (set_tpa)
4873 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4874 for (i = 0; i < bp->nr_vnics; i++) {
4875 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4876 if (rc) {
4877 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004878 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004879 return rc;
4880 }
4881 }
4882 return 0;
4883}
4884
4885static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4886{
4887 int i;
4888
4889 for (i = 0; i < bp->nr_vnics; i++)
4890 bnxt_hwrm_vnic_set_rss(bp, i, false);
4891}
4892
4893static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4894 bool irq_re_init)
4895{
4896 if (bp->vnic_info) {
4897 bnxt_hwrm_clear_vnic_filter(bp);
4898 /* clear all RSS setting before free vnic ctx */
4899 bnxt_hwrm_clear_vnic_rss(bp);
4900 bnxt_hwrm_vnic_ctx_free(bp);
4901 /* before free the vnic, undo the vnic tpa settings */
4902 if (bp->flags & BNXT_FLAG_TPA)
4903 bnxt_set_tpa(bp, false);
4904 bnxt_hwrm_vnic_free(bp);
4905 }
4906 bnxt_hwrm_ring_free(bp, close_path);
4907 bnxt_hwrm_ring_grp_free(bp);
4908 if (irq_re_init) {
4909 bnxt_hwrm_stat_ctx_free(bp);
4910 bnxt_hwrm_free_tunnel_ports(bp);
4911 }
4912}
4913
4914static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4915{
Michael Chanae10ae72016-12-29 12:13:38 -05004916 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004917 int rc;
4918
Michael Chanae10ae72016-12-29 12:13:38 -05004919 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4920 goto skip_rss_ctx;
4921
Michael Chanc0c050c2015-10-22 16:01:17 -04004922 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004923 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004924 if (rc) {
4925 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4926 vnic_id, rc);
4927 goto vnic_setup_err;
4928 }
4929 bp->rsscos_nr_ctxs++;
4930
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004931 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4932 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4933 if (rc) {
4934 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4935 vnic_id, rc);
4936 goto vnic_setup_err;
4937 }
4938 bp->rsscos_nr_ctxs++;
4939 }
4940
Michael Chanae10ae72016-12-29 12:13:38 -05004941skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004942 /* configure default vnic, ring grp */
4943 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4944 if (rc) {
4945 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4946 vnic_id, rc);
4947 goto vnic_setup_err;
4948 }
4949
4950 /* Enable RSS hashing on vnic */
4951 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4952 if (rc) {
4953 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4954 vnic_id, rc);
4955 goto vnic_setup_err;
4956 }
4957
4958 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4959 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4960 if (rc) {
4961 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4962 vnic_id, rc);
4963 }
4964 }
4965
4966vnic_setup_err:
4967 return rc;
4968}
4969
4970static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4971{
4972#ifdef CONFIG_RFS_ACCEL
4973 int i, rc = 0;
4974
4975 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004976 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004977 u16 vnic_id = i + 1;
4978 u16 ring_id = i;
4979
4980 if (vnic_id >= bp->nr_vnics)
4981 break;
4982
Michael Chanae10ae72016-12-29 12:13:38 -05004983 vnic = &bp->vnic_info[vnic_id];
4984 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4985 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4986 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004987 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004988 if (rc) {
4989 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4990 vnic_id, rc);
4991 break;
4992 }
4993 rc = bnxt_setup_vnic(bp, vnic_id);
4994 if (rc)
4995 break;
4996 }
4997 return rc;
4998#else
4999 return 0;
5000#endif
5001}
5002
Michael Chan17c71ac2016-07-01 18:46:27 -04005003/* Allow PF and VF with default VLAN to be in promiscuous mode */
5004static bool bnxt_promisc_ok(struct bnxt *bp)
5005{
5006#ifdef CONFIG_BNXT_SRIOV
5007 if (BNXT_VF(bp) && !bp->vf.vlan)
5008 return false;
5009#endif
5010 return true;
5011}
5012
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005013static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5014{
5015 unsigned int rc = 0;
5016
5017 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5018 if (rc) {
5019 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5020 rc);
5021 return rc;
5022 }
5023
5024 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5025 if (rc) {
5026 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5027 rc);
5028 return rc;
5029 }
5030 return rc;
5031}
5032
Michael Chanb664f002015-12-02 01:54:08 -05005033static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005034static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005035
Michael Chanc0c050c2015-10-22 16:01:17 -04005036static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5037{
Michael Chan7d2837d2016-05-04 16:56:44 -04005038 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005039 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005040 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005041
5042 if (irq_re_init) {
5043 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5044 if (rc) {
5045 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5046 rc);
5047 goto err_out;
5048 }
5049 }
5050
5051 rc = bnxt_hwrm_ring_alloc(bp);
5052 if (rc) {
5053 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5054 goto err_out;
5055 }
5056
5057 rc = bnxt_hwrm_ring_grp_alloc(bp);
5058 if (rc) {
5059 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5060 goto err_out;
5061 }
5062
Prashant Sreedharan76595192016-07-18 07:15:22 -04005063 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5064 rx_nr_rings--;
5065
Michael Chanc0c050c2015-10-22 16:01:17 -04005066 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005067 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005068 if (rc) {
5069 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5070 goto err_out;
5071 }
5072
5073 rc = bnxt_setup_vnic(bp, 0);
5074 if (rc)
5075 goto err_out;
5076
5077 if (bp->flags & BNXT_FLAG_RFS) {
5078 rc = bnxt_alloc_rfs_vnics(bp);
5079 if (rc)
5080 goto err_out;
5081 }
5082
5083 if (bp->flags & BNXT_FLAG_TPA) {
5084 rc = bnxt_set_tpa(bp, true);
5085 if (rc)
5086 goto err_out;
5087 }
5088
5089 if (BNXT_VF(bp))
5090 bnxt_update_vf_mac(bp);
5091
5092 /* Filter for default vnic 0 */
5093 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5094 if (rc) {
5095 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5096 goto err_out;
5097 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005098 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005099
Michael Chan7d2837d2016-05-04 16:56:44 -04005100 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005101
Michael Chan17c71ac2016-07-01 18:46:27 -04005102 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005103 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5104
5105 if (bp->dev->flags & IFF_ALLMULTI) {
5106 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5107 vnic->mc_list_count = 0;
5108 } else {
5109 u32 mask = 0;
5110
5111 bnxt_mc_list_updated(bp, &mask);
5112 vnic->rx_mask |= mask;
5113 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005114
Michael Chanb664f002015-12-02 01:54:08 -05005115 rc = bnxt_cfg_rx_mode(bp);
5116 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005117 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005118
5119 rc = bnxt_hwrm_set_coal(bp);
5120 if (rc)
5121 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005122 rc);
5123
5124 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5125 rc = bnxt_setup_nitroa0_vnic(bp);
5126 if (rc)
5127 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5128 rc);
5129 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005130
Michael Chancf6645f2016-06-13 02:25:28 -04005131 if (BNXT_VF(bp)) {
5132 bnxt_hwrm_func_qcfg(bp);
5133 netdev_update_features(bp->dev);
5134 }
5135
Michael Chanc0c050c2015-10-22 16:01:17 -04005136 return 0;
5137
5138err_out:
5139 bnxt_hwrm_resource_free(bp, 0, true);
5140
5141 return rc;
5142}
5143
5144static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5145{
5146 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5147 return 0;
5148}
5149
5150static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5151{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005152 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005153 bnxt_init_rx_rings(bp);
5154 bnxt_init_tx_rings(bp);
5155 bnxt_init_ring_grps(bp, irq_re_init);
5156 bnxt_init_vnics(bp);
5157
5158 return bnxt_init_chip(bp, irq_re_init);
5159}
5160
Michael Chanc0c050c2015-10-22 16:01:17 -04005161static int bnxt_set_real_num_queues(struct bnxt *bp)
5162{
5163 int rc;
5164 struct net_device *dev = bp->dev;
5165
Michael Chan5f449242017-02-06 16:55:40 -05005166 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5167 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005168 if (rc)
5169 return rc;
5170
5171 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5172 if (rc)
5173 return rc;
5174
5175#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005176 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005177 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005178#endif
5179
5180 return rc;
5181}
5182
Michael Chan6e6c5a52016-01-02 23:45:02 -05005183static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5184 bool shared)
5185{
5186 int _rx = *rx, _tx = *tx;
5187
5188 if (shared) {
5189 *rx = min_t(int, _rx, max);
5190 *tx = min_t(int, _tx, max);
5191 } else {
5192 if (max < 2)
5193 return -ENOMEM;
5194
5195 while (_rx + _tx > max) {
5196 if (_rx > _tx && _rx > 1)
5197 _rx--;
5198 else if (_tx > 1)
5199 _tx--;
5200 }
5201 *rx = _rx;
5202 *tx = _tx;
5203 }
5204 return 0;
5205}
5206
Michael Chan78095922016-12-07 00:26:16 -05005207static void bnxt_setup_msix(struct bnxt *bp)
5208{
5209 const int len = sizeof(bp->irq_tbl[0].name);
5210 struct net_device *dev = bp->dev;
5211 int tcs, i;
5212
5213 tcs = netdev_get_num_tc(dev);
5214 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005215 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005216
Michael Chand1e79252017-02-06 16:55:38 -05005217 for (i = 0; i < tcs; i++) {
5218 count = bp->tx_nr_rings_per_tc;
5219 off = i * count;
5220 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005221 }
5222 }
5223
5224 for (i = 0; i < bp->cp_nr_rings; i++) {
5225 char *attr;
5226
5227 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5228 attr = "TxRx";
5229 else if (i < bp->rx_nr_rings)
5230 attr = "rx";
5231 else
5232 attr = "tx";
5233
5234 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5235 i);
5236 bp->irq_tbl[i].handler = bnxt_msix;
5237 }
5238}
5239
5240static void bnxt_setup_inta(struct bnxt *bp)
5241{
5242 const int len = sizeof(bp->irq_tbl[0].name);
5243
5244 if (netdev_get_num_tc(bp->dev))
5245 netdev_reset_tc(bp->dev);
5246
5247 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5248 0);
5249 bp->irq_tbl[0].handler = bnxt_inta;
5250}
5251
5252static int bnxt_setup_int_mode(struct bnxt *bp)
5253{
5254 int rc;
5255
5256 if (bp->flags & BNXT_FLAG_USING_MSIX)
5257 bnxt_setup_msix(bp);
5258 else
5259 bnxt_setup_inta(bp);
5260
5261 rc = bnxt_set_real_num_queues(bp);
5262 return rc;
5263}
5264
Michael Chanb7429952017-01-13 01:32:00 -05005265#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005266static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5267{
5268#if defined(CONFIG_BNXT_SRIOV)
5269 if (BNXT_VF(bp))
5270 return bp->vf.max_rsscos_ctxs;
5271#endif
5272 return bp->pf.max_rsscos_ctxs;
5273}
5274
5275static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5276{
5277#if defined(CONFIG_BNXT_SRIOV)
5278 if (BNXT_VF(bp))
5279 return bp->vf.max_vnics;
5280#endif
5281 return bp->pf.max_vnics;
5282}
Michael Chanb7429952017-01-13 01:32:00 -05005283#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005284
Michael Chane4060d32016-12-07 00:26:19 -05005285unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5286{
5287#if defined(CONFIG_BNXT_SRIOV)
5288 if (BNXT_VF(bp))
5289 return bp->vf.max_stat_ctxs;
5290#endif
5291 return bp->pf.max_stat_ctxs;
5292}
5293
Michael Chana588e452016-12-07 00:26:21 -05005294void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5295{
5296#if defined(CONFIG_BNXT_SRIOV)
5297 if (BNXT_VF(bp))
5298 bp->vf.max_stat_ctxs = max;
5299 else
5300#endif
5301 bp->pf.max_stat_ctxs = max;
5302}
5303
Michael Chane4060d32016-12-07 00:26:19 -05005304unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5305{
5306#if defined(CONFIG_BNXT_SRIOV)
5307 if (BNXT_VF(bp))
5308 return bp->vf.max_cp_rings;
5309#endif
5310 return bp->pf.max_cp_rings;
5311}
5312
Michael Chana588e452016-12-07 00:26:21 -05005313void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5314{
5315#if defined(CONFIG_BNXT_SRIOV)
5316 if (BNXT_VF(bp))
5317 bp->vf.max_cp_rings = max;
5318 else
5319#endif
5320 bp->pf.max_cp_rings = max;
5321}
5322
Michael Chan78095922016-12-07 00:26:16 -05005323static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5324{
5325#if defined(CONFIG_BNXT_SRIOV)
5326 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005327 return min_t(unsigned int, bp->vf.max_irqs,
5328 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005329#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005330 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005331}
5332
Michael Chan33c26572016-12-07 00:26:15 -05005333void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5334{
5335#if defined(CONFIG_BNXT_SRIOV)
5336 if (BNXT_VF(bp))
5337 bp->vf.max_irqs = max_irqs;
5338 else
5339#endif
5340 bp->pf.max_irqs = max_irqs;
5341}
5342
Michael Chan78095922016-12-07 00:26:16 -05005343static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005344{
Michael Chan01657bc2016-01-02 23:45:03 -05005345 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005346 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005347
Michael Chan78095922016-12-07 00:26:16 -05005348 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005349 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5350 if (!msix_ent)
5351 return -ENOMEM;
5352
5353 for (i = 0; i < total_vecs; i++) {
5354 msix_ent[i].entry = i;
5355 msix_ent[i].vector = 0;
5356 }
5357
Michael Chan01657bc2016-01-02 23:45:03 -05005358 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5359 min = 2;
5360
5361 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005362 if (total_vecs < 0) {
5363 rc = -ENODEV;
5364 goto msix_setup_exit;
5365 }
5366
5367 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5368 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005369 for (i = 0; i < total_vecs; i++)
5370 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005371
Michael Chan78095922016-12-07 00:26:16 -05005372 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005373 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005374 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005375 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005376 if (rc)
5377 goto msix_setup_exit;
5378
Michael Chanc0c050c2015-10-22 16:01:17 -04005379 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005380 bp->cp_nr_rings = (min == 1) ?
5381 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5382 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005383
Michael Chanc0c050c2015-10-22 16:01:17 -04005384 } else {
5385 rc = -ENOMEM;
5386 goto msix_setup_exit;
5387 }
5388 bp->flags |= BNXT_FLAG_USING_MSIX;
5389 kfree(msix_ent);
5390 return 0;
5391
5392msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005393 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5394 kfree(bp->irq_tbl);
5395 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005396 pci_disable_msix(bp->pdev);
5397 kfree(msix_ent);
5398 return rc;
5399}
5400
Michael Chan78095922016-12-07 00:26:16 -05005401static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005402{
Michael Chanc0c050c2015-10-22 16:01:17 -04005403 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005404 if (!bp->irq_tbl)
5405 return -ENOMEM;
5406
5407 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005408 bp->rx_nr_rings = 1;
5409 bp->tx_nr_rings = 1;
5410 bp->cp_nr_rings = 1;
5411 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005412 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005413 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005414 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005415}
5416
Michael Chan78095922016-12-07 00:26:16 -05005417static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005418{
5419 int rc = 0;
5420
5421 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005422 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005423
Michael Chan1fa72e22016-04-25 02:30:49 -04005424 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005425 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005426 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005427 }
5428 return rc;
5429}
5430
Michael Chan78095922016-12-07 00:26:16 -05005431static void bnxt_clear_int_mode(struct bnxt *bp)
5432{
5433 if (bp->flags & BNXT_FLAG_USING_MSIX)
5434 pci_disable_msix(bp->pdev);
5435
5436 kfree(bp->irq_tbl);
5437 bp->irq_tbl = NULL;
5438 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5439}
5440
Michael Chanc0c050c2015-10-22 16:01:17 -04005441static void bnxt_free_irq(struct bnxt *bp)
5442{
5443 struct bnxt_irq *irq;
5444 int i;
5445
5446#ifdef CONFIG_RFS_ACCEL
5447 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5448 bp->dev->rx_cpu_rmap = NULL;
5449#endif
5450 if (!bp->irq_tbl)
5451 return;
5452
5453 for (i = 0; i < bp->cp_nr_rings; i++) {
5454 irq = &bp->irq_tbl[i];
5455 if (irq->requested)
5456 free_irq(irq->vector, bp->bnapi[i]);
5457 irq->requested = 0;
5458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005459}
5460
5461static int bnxt_request_irq(struct bnxt *bp)
5462{
Michael Chanb81a90d2016-01-02 23:45:01 -05005463 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005464 unsigned long flags = 0;
5465#ifdef CONFIG_RFS_ACCEL
5466 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5467#endif
5468
5469 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5470 flags = IRQF_SHARED;
5471
Michael Chanb81a90d2016-01-02 23:45:01 -05005472 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005473 struct bnxt_irq *irq = &bp->irq_tbl[i];
5474#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005475 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005476 rc = irq_cpu_rmap_add(rmap, irq->vector);
5477 if (rc)
5478 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005479 j);
5480 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005481 }
5482#endif
5483 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5484 bp->bnapi[i]);
5485 if (rc)
5486 break;
5487
5488 irq->requested = 1;
5489 }
5490 return rc;
5491}
5492
5493static void bnxt_del_napi(struct bnxt *bp)
5494{
5495 int i;
5496
5497 if (!bp->bnapi)
5498 return;
5499
5500 for (i = 0; i < bp->cp_nr_rings; i++) {
5501 struct bnxt_napi *bnapi = bp->bnapi[i];
5502
5503 napi_hash_del(&bnapi->napi);
5504 netif_napi_del(&bnapi->napi);
5505 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005506 /* We called napi_hash_del() before netif_napi_del(), we need
5507 * to respect an RCU grace period before freeing napi structures.
5508 */
5509 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005510}
5511
5512static void bnxt_init_napi(struct bnxt *bp)
5513{
5514 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005515 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005516 struct bnxt_napi *bnapi;
5517
5518 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005519 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5520 cp_nr_rings--;
5521 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005522 bnapi = bp->bnapi[i];
5523 netif_napi_add(bp->dev, &bnapi->napi,
5524 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005525 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005526 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5527 bnapi = bp->bnapi[cp_nr_rings];
5528 netif_napi_add(bp->dev, &bnapi->napi,
5529 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005530 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005531 } else {
5532 bnapi = bp->bnapi[0];
5533 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005534 }
5535}
5536
5537static void bnxt_disable_napi(struct bnxt *bp)
5538{
5539 int i;
5540
5541 if (!bp->bnapi)
5542 return;
5543
Michael Chanb356a2e2016-12-29 12:13:31 -05005544 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005545 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005546}
5547
5548static void bnxt_enable_napi(struct bnxt *bp)
5549{
5550 int i;
5551
5552 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005553 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005554 napi_enable(&bp->bnapi[i]->napi);
5555 }
5556}
5557
Michael Chan7df4ae92016-12-02 21:17:17 -05005558void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005559{
5560 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005561 struct bnxt_tx_ring_info *txr;
5562 struct netdev_queue *txq;
5563
Michael Chanb6ab4b02016-01-02 23:44:59 -05005564 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005565 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005566 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005567 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005568 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005569 }
5570 }
5571 /* Stop all TX queues */
5572 netif_tx_disable(bp->dev);
5573 netif_carrier_off(bp->dev);
5574}
5575
Michael Chan7df4ae92016-12-02 21:17:17 -05005576void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005577{
5578 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005579 struct bnxt_tx_ring_info *txr;
5580 struct netdev_queue *txq;
5581
5582 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005583 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005584 txq = netdev_get_tx_queue(bp->dev, i);
5585 txr->dev_state = 0;
5586 }
5587 netif_tx_wake_all_queues(bp->dev);
5588 if (bp->link_info.link_up)
5589 netif_carrier_on(bp->dev);
5590}
5591
5592static void bnxt_report_link(struct bnxt *bp)
5593{
5594 if (bp->link_info.link_up) {
5595 const char *duplex;
5596 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005597 u32 speed;
5598 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005599
5600 netif_carrier_on(bp->dev);
5601 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5602 duplex = "full";
5603 else
5604 duplex = "half";
5605 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5606 flow_ctrl = "ON - receive & transmit";
5607 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5608 flow_ctrl = "ON - transmit";
5609 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5610 flow_ctrl = "ON - receive";
5611 else
5612 flow_ctrl = "none";
5613 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005614 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005615 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005616 if (bp->flags & BNXT_FLAG_EEE_CAP)
5617 netdev_info(bp->dev, "EEE is %s\n",
5618 bp->eee.eee_active ? "active" :
5619 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005620 fec = bp->link_info.fec_cfg;
5621 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5622 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5623 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5624 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5625 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005626 } else {
5627 netif_carrier_off(bp->dev);
5628 netdev_err(bp->dev, "NIC Link is Down\n");
5629 }
5630}
5631
Michael Chan170ce012016-04-05 14:08:57 -04005632static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5633{
5634 int rc = 0;
5635 struct hwrm_port_phy_qcaps_input req = {0};
5636 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005637 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005638
5639 if (bp->hwrm_spec_code < 0x10201)
5640 return 0;
5641
5642 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5643
5644 mutex_lock(&bp->hwrm_cmd_lock);
5645 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5646 if (rc)
5647 goto hwrm_phy_qcaps_exit;
5648
5649 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5650 struct ethtool_eee *eee = &bp->eee;
5651 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5652
5653 bp->flags |= BNXT_FLAG_EEE_CAP;
5654 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5655 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5656 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5657 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5658 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5659 }
Michael Chan520ad892017-03-08 18:44:35 -05005660 if (resp->supported_speeds_auto_mode)
5661 link_info->support_auto_speeds =
5662 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005663
5664hwrm_phy_qcaps_exit:
5665 mutex_unlock(&bp->hwrm_cmd_lock);
5666 return rc;
5667}
5668
Michael Chanc0c050c2015-10-22 16:01:17 -04005669static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5670{
5671 int rc = 0;
5672 struct bnxt_link_info *link_info = &bp->link_info;
5673 struct hwrm_port_phy_qcfg_input req = {0};
5674 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5675 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005676 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005677
5678 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5679
5680 mutex_lock(&bp->hwrm_cmd_lock);
5681 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5682 if (rc) {
5683 mutex_unlock(&bp->hwrm_cmd_lock);
5684 return rc;
5685 }
5686
5687 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5688 link_info->phy_link_status = resp->link;
5689 link_info->duplex = resp->duplex;
5690 link_info->pause = resp->pause;
5691 link_info->auto_mode = resp->auto_mode;
5692 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005693 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005694 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005695 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005696 if (link_info->phy_link_status == BNXT_LINK_LINK)
5697 link_info->link_speed = le16_to_cpu(resp->link_speed);
5698 else
5699 link_info->link_speed = 0;
5700 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005701 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5702 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005703 link_info->lp_auto_link_speeds =
5704 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005705 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5706 link_info->phy_ver[0] = resp->phy_maj;
5707 link_info->phy_ver[1] = resp->phy_min;
5708 link_info->phy_ver[2] = resp->phy_bld;
5709 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005710 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005711 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005712 link_info->phy_addr = resp->eee_config_phy_addr &
5713 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005714 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005715
Michael Chan170ce012016-04-05 14:08:57 -04005716 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5717 struct ethtool_eee *eee = &bp->eee;
5718 u16 fw_speeds;
5719
5720 eee->eee_active = 0;
5721 if (resp->eee_config_phy_addr &
5722 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5723 eee->eee_active = 1;
5724 fw_speeds = le16_to_cpu(
5725 resp->link_partner_adv_eee_link_speed_mask);
5726 eee->lp_advertised =
5727 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5728 }
5729
5730 /* Pull initial EEE config */
5731 if (!chng_link_state) {
5732 if (resp->eee_config_phy_addr &
5733 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5734 eee->eee_enabled = 1;
5735
5736 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5737 eee->advertised =
5738 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5739
5740 if (resp->eee_config_phy_addr &
5741 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5742 __le32 tmr;
5743
5744 eee->tx_lpi_enabled = 1;
5745 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5746 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5747 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5748 }
5749 }
5750 }
Michael Chane70c7522017-02-12 19:18:16 -05005751
5752 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5753 if (bp->hwrm_spec_code >= 0x10504)
5754 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5755
Michael Chanc0c050c2015-10-22 16:01:17 -04005756 /* TODO: need to add more logic to report VF link */
5757 if (chng_link_state) {
5758 if (link_info->phy_link_status == BNXT_LINK_LINK)
5759 link_info->link_up = 1;
5760 else
5761 link_info->link_up = 0;
5762 if (link_up != link_info->link_up)
5763 bnxt_report_link(bp);
5764 } else {
5765 /* alwasy link down if not require to update link state */
5766 link_info->link_up = 0;
5767 }
5768 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005769
5770 diff = link_info->support_auto_speeds ^ link_info->advertising;
5771 if ((link_info->support_auto_speeds | diff) !=
5772 link_info->support_auto_speeds) {
5773 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005774 * update the advertisement settings. Caller holds RTNL
5775 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005776 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005777 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005778 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005779 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005780 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005781 return 0;
5782}
5783
Michael Chan10289be2016-05-15 03:04:49 -04005784static void bnxt_get_port_module_status(struct bnxt *bp)
5785{
5786 struct bnxt_link_info *link_info = &bp->link_info;
5787 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5788 u8 module_status;
5789
5790 if (bnxt_update_link(bp, true))
5791 return;
5792
5793 module_status = link_info->module_status;
5794 switch (module_status) {
5795 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5796 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5797 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5798 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5799 bp->pf.port_id);
5800 if (bp->hwrm_spec_code >= 0x10201) {
5801 netdev_warn(bp->dev, "Module part number %s\n",
5802 resp->phy_vendor_partnumber);
5803 }
5804 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5805 netdev_warn(bp->dev, "TX is disabled\n");
5806 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5807 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5808 }
5809}
5810
Michael Chanc0c050c2015-10-22 16:01:17 -04005811static void
5812bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5813{
5814 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005815 if (bp->hwrm_spec_code >= 0x10201)
5816 req->auto_pause =
5817 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005818 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5819 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5820 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005821 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005822 req->enables |=
5823 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5824 } else {
5825 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5826 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5827 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5828 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5829 req->enables |=
5830 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005831 if (bp->hwrm_spec_code >= 0x10201) {
5832 req->auto_pause = req->force_pause;
5833 req->enables |= cpu_to_le32(
5834 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5835 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005836 }
5837}
5838
5839static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5840 struct hwrm_port_phy_cfg_input *req)
5841{
5842 u8 autoneg = bp->link_info.autoneg;
5843 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005844 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005845
5846 if (autoneg & BNXT_AUTONEG_SPEED) {
5847 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005848 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005849
5850 req->enables |= cpu_to_le32(
5851 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5852 req->auto_link_speed_mask = cpu_to_le16(advertising);
5853
5854 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5855 req->flags |=
5856 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5857 } else {
5858 req->force_link_speed = cpu_to_le16(fw_link_speed);
5859 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5860 }
5861
Michael Chanc0c050c2015-10-22 16:01:17 -04005862 /* tell chimp that the setting takes effect immediately */
5863 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5864}
5865
5866int bnxt_hwrm_set_pause(struct bnxt *bp)
5867{
5868 struct hwrm_port_phy_cfg_input req = {0};
5869 int rc;
5870
5871 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5872 bnxt_hwrm_set_pause_common(bp, &req);
5873
5874 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5875 bp->link_info.force_link_chng)
5876 bnxt_hwrm_set_link_common(bp, &req);
5877
5878 mutex_lock(&bp->hwrm_cmd_lock);
5879 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5880 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5881 /* since changing of pause setting doesn't trigger any link
5882 * change event, the driver needs to update the current pause
5883 * result upon successfully return of the phy_cfg command
5884 */
5885 bp->link_info.pause =
5886 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5887 bp->link_info.auto_pause_setting = 0;
5888 if (!bp->link_info.force_link_chng)
5889 bnxt_report_link(bp);
5890 }
5891 bp->link_info.force_link_chng = false;
5892 mutex_unlock(&bp->hwrm_cmd_lock);
5893 return rc;
5894}
5895
Michael Chan939f7f02016-04-05 14:08:58 -04005896static void bnxt_hwrm_set_eee(struct bnxt *bp,
5897 struct hwrm_port_phy_cfg_input *req)
5898{
5899 struct ethtool_eee *eee = &bp->eee;
5900
5901 if (eee->eee_enabled) {
5902 u16 eee_speeds;
5903 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5904
5905 if (eee->tx_lpi_enabled)
5906 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5907 else
5908 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5909
5910 req->flags |= cpu_to_le32(flags);
5911 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5912 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5913 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5914 } else {
5915 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5916 }
5917}
5918
5919int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005920{
5921 struct hwrm_port_phy_cfg_input req = {0};
5922
5923 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5924 if (set_pause)
5925 bnxt_hwrm_set_pause_common(bp, &req);
5926
5927 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005928
5929 if (set_eee)
5930 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005931 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5932}
5933
Michael Chan33f7d552016-04-11 04:11:12 -04005934static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5935{
5936 struct hwrm_port_phy_cfg_input req = {0};
5937
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005938 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005939 return 0;
5940
5941 if (pci_num_vf(bp->pdev))
5942 return 0;
5943
5944 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005945 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005946 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5947}
5948
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005949static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5950{
5951 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5952 struct hwrm_port_led_qcaps_input req = {0};
5953 struct bnxt_pf_info *pf = &bp->pf;
5954 int rc;
5955
5956 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5957 return 0;
5958
5959 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5960 req.port_id = cpu_to_le16(pf->port_id);
5961 mutex_lock(&bp->hwrm_cmd_lock);
5962 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5963 if (rc) {
5964 mutex_unlock(&bp->hwrm_cmd_lock);
5965 return rc;
5966 }
5967 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5968 int i;
5969
5970 bp->num_leds = resp->num_leds;
5971 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5972 bp->num_leds);
5973 for (i = 0; i < bp->num_leds; i++) {
5974 struct bnxt_led_info *led = &bp->leds[i];
5975 __le16 caps = led->led_state_caps;
5976
5977 if (!led->led_group_id ||
5978 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5979 bp->num_leds = 0;
5980 break;
5981 }
5982 }
5983 }
5984 mutex_unlock(&bp->hwrm_cmd_lock);
5985 return 0;
5986}
5987
Michael Chan5282db62017-04-04 18:14:10 -04005988int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
5989{
5990 struct hwrm_wol_filter_alloc_input req = {0};
5991 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5992 int rc;
5993
5994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
5995 req.port_id = cpu_to_le16(bp->pf.port_id);
5996 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
5997 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
5998 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
5999 mutex_lock(&bp->hwrm_cmd_lock);
6000 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6001 if (!rc)
6002 bp->wol_filter_id = resp->wol_filter_id;
6003 mutex_unlock(&bp->hwrm_cmd_lock);
6004 return rc;
6005}
6006
6007int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6008{
6009 struct hwrm_wol_filter_free_input req = {0};
6010 int rc;
6011
6012 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6013 req.port_id = cpu_to_le16(bp->pf.port_id);
6014 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6015 req.wol_filter_id = bp->wol_filter_id;
6016 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6017 return rc;
6018}
6019
Michael Chanc1ef1462017-04-04 18:14:07 -04006020static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6021{
6022 struct hwrm_wol_filter_qcfg_input req = {0};
6023 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6024 u16 next_handle = 0;
6025 int rc;
6026
6027 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6028 req.port_id = cpu_to_le16(bp->pf.port_id);
6029 req.handle = cpu_to_le16(handle);
6030 mutex_lock(&bp->hwrm_cmd_lock);
6031 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6032 if (!rc) {
6033 next_handle = le16_to_cpu(resp->next_handle);
6034 if (next_handle != 0) {
6035 if (resp->wol_type ==
6036 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6037 bp->wol = 1;
6038 bp->wol_filter_id = resp->wol_filter_id;
6039 }
6040 }
6041 }
6042 mutex_unlock(&bp->hwrm_cmd_lock);
6043 return next_handle;
6044}
6045
6046static void bnxt_get_wol_settings(struct bnxt *bp)
6047{
6048 u16 handle = 0;
6049
6050 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6051 return;
6052
6053 do {
6054 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6055 } while (handle && handle != 0xffff);
6056}
6057
Michael Chan939f7f02016-04-05 14:08:58 -04006058static bool bnxt_eee_config_ok(struct bnxt *bp)
6059{
6060 struct ethtool_eee *eee = &bp->eee;
6061 struct bnxt_link_info *link_info = &bp->link_info;
6062
6063 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6064 return true;
6065
6066 if (eee->eee_enabled) {
6067 u32 advertising =
6068 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6069
6070 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6071 eee->eee_enabled = 0;
6072 return false;
6073 }
6074 if (eee->advertised & ~advertising) {
6075 eee->advertised = advertising & eee->supported;
6076 return false;
6077 }
6078 }
6079 return true;
6080}
6081
Michael Chanc0c050c2015-10-22 16:01:17 -04006082static int bnxt_update_phy_setting(struct bnxt *bp)
6083{
6084 int rc;
6085 bool update_link = false;
6086 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006087 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006088 struct bnxt_link_info *link_info = &bp->link_info;
6089
6090 rc = bnxt_update_link(bp, true);
6091 if (rc) {
6092 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6093 rc);
6094 return rc;
6095 }
Michael Chan33dac242017-02-12 19:18:15 -05006096 if (!BNXT_SINGLE_PF(bp))
6097 return 0;
6098
Michael Chanc0c050c2015-10-22 16:01:17 -04006099 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006100 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6101 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006102 update_pause = true;
6103 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6104 link_info->force_pause_setting != link_info->req_flow_ctrl)
6105 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006106 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6107 if (BNXT_AUTO_MODE(link_info->auto_mode))
6108 update_link = true;
6109 if (link_info->req_link_speed != link_info->force_link_speed)
6110 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006111 if (link_info->req_duplex != link_info->duplex_setting)
6112 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006113 } else {
6114 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6115 update_link = true;
6116 if (link_info->advertising != link_info->auto_link_speeds)
6117 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006118 }
6119
Michael Chan16d663a2016-11-16 21:13:07 -05006120 /* The last close may have shutdown the link, so need to call
6121 * PHY_CFG to bring it back up.
6122 */
6123 if (!netif_carrier_ok(bp->dev))
6124 update_link = true;
6125
Michael Chan939f7f02016-04-05 14:08:58 -04006126 if (!bnxt_eee_config_ok(bp))
6127 update_eee = true;
6128
Michael Chanc0c050c2015-10-22 16:01:17 -04006129 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006130 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006131 else if (update_pause)
6132 rc = bnxt_hwrm_set_pause(bp);
6133 if (rc) {
6134 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6135 rc);
6136 return rc;
6137 }
6138
6139 return rc;
6140}
6141
Jeffrey Huang11809492015-11-05 16:25:49 -05006142/* Common routine to pre-map certain register block to different GRC window.
6143 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6144 * in PF and 3 windows in VF that can be customized to map in different
6145 * register blocks.
6146 */
6147static void bnxt_preset_reg_win(struct bnxt *bp)
6148{
6149 if (BNXT_PF(bp)) {
6150 /* CAG registers map to GRC window #4 */
6151 writel(BNXT_CAG_REG_BASE,
6152 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6153 }
6154}
6155
Michael Chanc0c050c2015-10-22 16:01:17 -04006156static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6157{
6158 int rc = 0;
6159
Jeffrey Huang11809492015-11-05 16:25:49 -05006160 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006161 netif_carrier_off(bp->dev);
6162 if (irq_re_init) {
6163 rc = bnxt_setup_int_mode(bp);
6164 if (rc) {
6165 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6166 rc);
6167 return rc;
6168 }
6169 }
6170 if ((bp->flags & BNXT_FLAG_RFS) &&
6171 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6172 /* disable RFS if falling back to INTA */
6173 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6174 bp->flags &= ~BNXT_FLAG_RFS;
6175 }
6176
6177 rc = bnxt_alloc_mem(bp, irq_re_init);
6178 if (rc) {
6179 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6180 goto open_err_free_mem;
6181 }
6182
6183 if (irq_re_init) {
6184 bnxt_init_napi(bp);
6185 rc = bnxt_request_irq(bp);
6186 if (rc) {
6187 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6188 goto open_err;
6189 }
6190 }
6191
6192 bnxt_enable_napi(bp);
6193
6194 rc = bnxt_init_nic(bp, irq_re_init);
6195 if (rc) {
6196 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6197 goto open_err;
6198 }
6199
6200 if (link_re_init) {
6201 rc = bnxt_update_phy_setting(bp);
6202 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006203 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006204 }
6205
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006206 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006207 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006208
Michael Chancaefe522015-12-09 19:35:42 -05006209 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006210 bnxt_enable_int(bp);
6211 /* Enable TX queues */
6212 bnxt_tx_enable(bp);
6213 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006214 /* Poll link status and check for SFP+ module status */
6215 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006216
6217 return 0;
6218
6219open_err:
6220 bnxt_disable_napi(bp);
6221 bnxt_del_napi(bp);
6222
6223open_err_free_mem:
6224 bnxt_free_skbs(bp);
6225 bnxt_free_irq(bp);
6226 bnxt_free_mem(bp, true);
6227 return rc;
6228}
6229
6230/* rtnl_lock held */
6231int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6232{
6233 int rc = 0;
6234
6235 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6236 if (rc) {
6237 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6238 dev_close(bp->dev);
6239 }
6240 return rc;
6241}
6242
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006243/* rtnl_lock held, open the NIC half way by allocating all resources, but
6244 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6245 * self tests.
6246 */
6247int bnxt_half_open_nic(struct bnxt *bp)
6248{
6249 int rc = 0;
6250
6251 rc = bnxt_alloc_mem(bp, false);
6252 if (rc) {
6253 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6254 goto half_open_err;
6255 }
6256 rc = bnxt_init_nic(bp, false);
6257 if (rc) {
6258 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6259 goto half_open_err;
6260 }
6261 return 0;
6262
6263half_open_err:
6264 bnxt_free_skbs(bp);
6265 bnxt_free_mem(bp, false);
6266 dev_close(bp->dev);
6267 return rc;
6268}
6269
6270/* rtnl_lock held, this call can only be made after a previous successful
6271 * call to bnxt_half_open_nic().
6272 */
6273void bnxt_half_close_nic(struct bnxt *bp)
6274{
6275 bnxt_hwrm_resource_free(bp, false, false);
6276 bnxt_free_skbs(bp);
6277 bnxt_free_mem(bp, false);
6278}
6279
Michael Chanc0c050c2015-10-22 16:01:17 -04006280static int bnxt_open(struct net_device *dev)
6281{
6282 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006283
Michael Chanc0c050c2015-10-22 16:01:17 -04006284 return __bnxt_open_nic(bp, true, true);
6285}
6286
Michael Chanf9b76eb2017-07-11 13:05:34 -04006287static bool bnxt_drv_busy(struct bnxt *bp)
6288{
6289 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6290 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6291}
6292
Michael Chanc0c050c2015-10-22 16:01:17 -04006293int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6294{
6295 int rc = 0;
6296
6297#ifdef CONFIG_BNXT_SRIOV
6298 if (bp->sriov_cfg) {
6299 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6300 !bp->sriov_cfg,
6301 BNXT_SRIOV_CFG_WAIT_TMO);
6302 if (rc)
6303 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6304 }
6305#endif
6306 /* Change device state to avoid TX queue wake up's */
6307 bnxt_tx_disable(bp);
6308
Michael Chancaefe522015-12-09 19:35:42 -05006309 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006310 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006311 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006312 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006313
Michael Chan9d8bc092016-12-29 12:13:33 -05006314 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006315 bnxt_shutdown_nic(bp, irq_re_init);
6316
6317 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6318
6319 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006320 del_timer_sync(&bp->timer);
6321 bnxt_free_skbs(bp);
6322
6323 if (irq_re_init) {
6324 bnxt_free_irq(bp);
6325 bnxt_del_napi(bp);
6326 }
6327 bnxt_free_mem(bp, irq_re_init);
6328 return rc;
6329}
6330
6331static int bnxt_close(struct net_device *dev)
6332{
6333 struct bnxt *bp = netdev_priv(dev);
6334
6335 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006336 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006337 return 0;
6338}
6339
6340/* rtnl_lock held */
6341static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6342{
6343 switch (cmd) {
6344 case SIOCGMIIPHY:
6345 /* fallthru */
6346 case SIOCGMIIREG: {
6347 if (!netif_running(dev))
6348 return -EAGAIN;
6349
6350 return 0;
6351 }
6352
6353 case SIOCSMIIREG:
6354 if (!netif_running(dev))
6355 return -EAGAIN;
6356
6357 return 0;
6358
6359 default:
6360 /* do nothing */
6361 break;
6362 }
6363 return -EOPNOTSUPP;
6364}
6365
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006366static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006367bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6368{
6369 u32 i;
6370 struct bnxt *bp = netdev_priv(dev);
6371
Michael Chanf9b76eb2017-07-11 13:05:34 -04006372 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6373 /* Make sure bnxt_close_nic() sees that we are reading stats before
6374 * we check the BNXT_STATE_OPEN flag.
6375 */
6376 smp_mb__after_atomic();
6377 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6378 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006379 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006380 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006381
6382 /* TODO check if we need to synchronize with bnxt_close path */
6383 for (i = 0; i < bp->cp_nr_rings; i++) {
6384 struct bnxt_napi *bnapi = bp->bnapi[i];
6385 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6386 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6387
6388 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6389 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6390 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6391
6392 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6393 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6394 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6395
6396 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6397 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6398 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6399
6400 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6401 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6402 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6403
6404 stats->rx_missed_errors +=
6405 le64_to_cpu(hw_stats->rx_discard_pkts);
6406
6407 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6408
Michael Chanc0c050c2015-10-22 16:01:17 -04006409 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6410 }
6411
Michael Chan9947f832016-03-07 15:38:46 -05006412 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6413 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6414 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6415
6416 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6417 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6418 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6419 le64_to_cpu(rx->rx_ovrsz_frames) +
6420 le64_to_cpu(rx->rx_runt_frames);
6421 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6422 le64_to_cpu(rx->rx_jbr_frames);
6423 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6424 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6425 stats->tx_errors = le64_to_cpu(tx->tx_err);
6426 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006427 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006428}
6429
6430static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6431{
6432 struct net_device *dev = bp->dev;
6433 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6434 struct netdev_hw_addr *ha;
6435 u8 *haddr;
6436 int mc_count = 0;
6437 bool update = false;
6438 int off = 0;
6439
6440 netdev_for_each_mc_addr(ha, dev) {
6441 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6442 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6443 vnic->mc_list_count = 0;
6444 return false;
6445 }
6446 haddr = ha->addr;
6447 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6448 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6449 update = true;
6450 }
6451 off += ETH_ALEN;
6452 mc_count++;
6453 }
6454 if (mc_count)
6455 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6456
6457 if (mc_count != vnic->mc_list_count) {
6458 vnic->mc_list_count = mc_count;
6459 update = true;
6460 }
6461 return update;
6462}
6463
6464static bool bnxt_uc_list_updated(struct bnxt *bp)
6465{
6466 struct net_device *dev = bp->dev;
6467 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6468 struct netdev_hw_addr *ha;
6469 int off = 0;
6470
6471 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6472 return true;
6473
6474 netdev_for_each_uc_addr(ha, dev) {
6475 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6476 return true;
6477
6478 off += ETH_ALEN;
6479 }
6480 return false;
6481}
6482
6483static void bnxt_set_rx_mode(struct net_device *dev)
6484{
6485 struct bnxt *bp = netdev_priv(dev);
6486 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6487 u32 mask = vnic->rx_mask;
6488 bool mc_update = false;
6489 bool uc_update;
6490
6491 if (!netif_running(dev))
6492 return;
6493
6494 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6495 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6496 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6497
Michael Chan17c71ac2016-07-01 18:46:27 -04006498 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006499 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6500
6501 uc_update = bnxt_uc_list_updated(bp);
6502
6503 if (dev->flags & IFF_ALLMULTI) {
6504 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6505 vnic->mc_list_count = 0;
6506 } else {
6507 mc_update = bnxt_mc_list_updated(bp, &mask);
6508 }
6509
6510 if (mask != vnic->rx_mask || uc_update || mc_update) {
6511 vnic->rx_mask = mask;
6512
6513 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6514 schedule_work(&bp->sp_task);
6515 }
6516}
6517
Michael Chanb664f002015-12-02 01:54:08 -05006518static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006519{
6520 struct net_device *dev = bp->dev;
6521 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6522 struct netdev_hw_addr *ha;
6523 int i, off = 0, rc;
6524 bool uc_update;
6525
6526 netif_addr_lock_bh(dev);
6527 uc_update = bnxt_uc_list_updated(bp);
6528 netif_addr_unlock_bh(dev);
6529
6530 if (!uc_update)
6531 goto skip_uc;
6532
6533 mutex_lock(&bp->hwrm_cmd_lock);
6534 for (i = 1; i < vnic->uc_filter_count; i++) {
6535 struct hwrm_cfa_l2_filter_free_input req = {0};
6536
6537 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6538 -1);
6539
6540 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6541
6542 rc = _hwrm_send_message(bp, &req, sizeof(req),
6543 HWRM_CMD_TIMEOUT);
6544 }
6545 mutex_unlock(&bp->hwrm_cmd_lock);
6546
6547 vnic->uc_filter_count = 1;
6548
6549 netif_addr_lock_bh(dev);
6550 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6551 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6552 } else {
6553 netdev_for_each_uc_addr(ha, dev) {
6554 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6555 off += ETH_ALEN;
6556 vnic->uc_filter_count++;
6557 }
6558 }
6559 netif_addr_unlock_bh(dev);
6560
6561 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6562 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6563 if (rc) {
6564 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6565 rc);
6566 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006567 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006568 }
6569 }
6570
6571skip_uc:
6572 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6573 if (rc)
6574 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6575 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006576
6577 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006578}
6579
Michael Chan8079e8f2016-12-29 12:13:37 -05006580/* If the chip and firmware supports RFS */
6581static bool bnxt_rfs_supported(struct bnxt *bp)
6582{
6583 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6584 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006585 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6586 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006587 return false;
6588}
6589
6590/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006591static bool bnxt_rfs_capable(struct bnxt *bp)
6592{
6593#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006594 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006595
Michael Chan964fd482017-02-12 19:18:13 -05006596 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006597 return false;
6598
6599 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006600 max_vnics = bnxt_get_max_func_vnics(bp);
6601 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006602
6603 /* RSS contexts not a limiting factor */
6604 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6605 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006606 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006607 netdev_warn(bp->dev,
6608 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006609 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006610 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006611 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006612
6613 return true;
6614#else
6615 return false;
6616#endif
6617}
6618
Michael Chanc0c050c2015-10-22 16:01:17 -04006619static netdev_features_t bnxt_fix_features(struct net_device *dev,
6620 netdev_features_t features)
6621{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006622 struct bnxt *bp = netdev_priv(dev);
6623
Vasundhara Volama2304902016-07-25 12:33:36 -04006624 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006625 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006626
6627 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6628 * turned on or off together.
6629 */
6630 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6631 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6632 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6633 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6634 NETIF_F_HW_VLAN_STAG_RX);
6635 else
6636 features |= NETIF_F_HW_VLAN_CTAG_RX |
6637 NETIF_F_HW_VLAN_STAG_RX;
6638 }
Michael Chancf6645f2016-06-13 02:25:28 -04006639#ifdef CONFIG_BNXT_SRIOV
6640 if (BNXT_VF(bp)) {
6641 if (bp->vf.vlan) {
6642 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6643 NETIF_F_HW_VLAN_STAG_RX);
6644 }
6645 }
6646#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006647 return features;
6648}
6649
6650static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6651{
6652 struct bnxt *bp = netdev_priv(dev);
6653 u32 flags = bp->flags;
6654 u32 changes;
6655 int rc = 0;
6656 bool re_init = false;
6657 bool update_tpa = false;
6658
6659 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006660 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006661 flags |= BNXT_FLAG_GRO;
6662 if (features & NETIF_F_LRO)
6663 flags |= BNXT_FLAG_LRO;
6664
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006665 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6666 flags &= ~BNXT_FLAG_TPA;
6667
Michael Chanc0c050c2015-10-22 16:01:17 -04006668 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6669 flags |= BNXT_FLAG_STRIP_VLAN;
6670
6671 if (features & NETIF_F_NTUPLE)
6672 flags |= BNXT_FLAG_RFS;
6673
6674 changes = flags ^ bp->flags;
6675 if (changes & BNXT_FLAG_TPA) {
6676 update_tpa = true;
6677 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6678 (flags & BNXT_FLAG_TPA) == 0)
6679 re_init = true;
6680 }
6681
6682 if (changes & ~BNXT_FLAG_TPA)
6683 re_init = true;
6684
6685 if (flags != bp->flags) {
6686 u32 old_flags = bp->flags;
6687
6688 bp->flags = flags;
6689
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006690 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006691 if (update_tpa)
6692 bnxt_set_ring_params(bp);
6693 return rc;
6694 }
6695
6696 if (re_init) {
6697 bnxt_close_nic(bp, false, false);
6698 if (update_tpa)
6699 bnxt_set_ring_params(bp);
6700
6701 return bnxt_open_nic(bp, false, false);
6702 }
6703 if (update_tpa) {
6704 rc = bnxt_set_tpa(bp,
6705 (flags & BNXT_FLAG_TPA) ?
6706 true : false);
6707 if (rc)
6708 bp->flags = old_flags;
6709 }
6710 }
6711 return rc;
6712}
6713
Michael Chan9f554592016-01-02 23:44:58 -05006714static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6715{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006716 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006717 int i = bnapi->index;
6718
Michael Chan3b2b7d92016-01-02 23:45:00 -05006719 if (!txr)
6720 return;
6721
Michael Chan9f554592016-01-02 23:44:58 -05006722 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6723 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6724 txr->tx_cons);
6725}
6726
6727static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6728{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006729 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006730 int i = bnapi->index;
6731
Michael Chan3b2b7d92016-01-02 23:45:00 -05006732 if (!rxr)
6733 return;
6734
Michael Chan9f554592016-01-02 23:44:58 -05006735 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",
6736 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6737 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6738 rxr->rx_sw_agg_prod);
6739}
6740
6741static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6742{
6743 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6744 int i = bnapi->index;
6745
6746 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6747 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6748}
6749
Michael Chanc0c050c2015-10-22 16:01:17 -04006750static void bnxt_dbg_dump_states(struct bnxt *bp)
6751{
6752 int i;
6753 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006754
6755 for (i = 0; i < bp->cp_nr_rings; i++) {
6756 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006757 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006758 bnxt_dump_tx_sw_state(bnapi);
6759 bnxt_dump_rx_sw_state(bnapi);
6760 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006761 }
6762 }
6763}
6764
Michael Chan6988bd92016-06-13 02:25:29 -04006765static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006766{
Michael Chan6988bd92016-06-13 02:25:29 -04006767 if (!silent)
6768 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006769 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006770 int rc;
6771
6772 if (!silent)
6773 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006774 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006775 rc = bnxt_open_nic(bp, false, false);
6776 if (!silent && !rc)
6777 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006778 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006779}
6780
6781static void bnxt_tx_timeout(struct net_device *dev)
6782{
6783 struct bnxt *bp = netdev_priv(dev);
6784
6785 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6786 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6787 schedule_work(&bp->sp_task);
6788}
6789
6790#ifdef CONFIG_NET_POLL_CONTROLLER
6791static void bnxt_poll_controller(struct net_device *dev)
6792{
6793 struct bnxt *bp = netdev_priv(dev);
6794 int i;
6795
Michael Chan2270bc52017-06-23 14:01:01 -04006796 /* Only process tx rings/combined rings in netpoll mode. */
6797 for (i = 0; i < bp->tx_nr_rings; i++) {
6798 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006799
Michael Chan2270bc52017-06-23 14:01:01 -04006800 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006801 }
6802}
6803#endif
6804
6805static void bnxt_timer(unsigned long data)
6806{
6807 struct bnxt *bp = (struct bnxt *)data;
6808 struct net_device *dev = bp->dev;
6809
6810 if (!netif_running(dev))
6811 return;
6812
6813 if (atomic_read(&bp->intr_sem) != 0)
6814 goto bnxt_restart_timer;
6815
Michael Chan3bdf56c2016-03-07 15:38:45 -05006816 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6817 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6818 schedule_work(&bp->sp_task);
6819 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006820bnxt_restart_timer:
6821 mod_timer(&bp->timer, jiffies + bp->current_interval);
6822}
6823
Michael Chana551ee92017-01-25 02:55:07 -05006824static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006825{
Michael Chana551ee92017-01-25 02:55:07 -05006826 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6827 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006828 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6829 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6830 */
6831 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6832 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006833}
6834
6835static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6836{
Michael Chan6988bd92016-06-13 02:25:29 -04006837 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6838 rtnl_unlock();
6839}
6840
Michael Chana551ee92017-01-25 02:55:07 -05006841/* Only called from bnxt_sp_task() */
6842static void bnxt_reset(struct bnxt *bp, bool silent)
6843{
6844 bnxt_rtnl_lock_sp(bp);
6845 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6846 bnxt_reset_task(bp, silent);
6847 bnxt_rtnl_unlock_sp(bp);
6848}
6849
Michael Chanc0c050c2015-10-22 16:01:17 -04006850static void bnxt_cfg_ntp_filters(struct bnxt *);
6851
6852static void bnxt_sp_task(struct work_struct *work)
6853{
6854 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006855
Michael Chan4cebdce2015-12-09 19:35:43 -05006856 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6857 smp_mb__after_atomic();
6858 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6859 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006860 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006861 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006862
6863 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6864 bnxt_cfg_rx_mode(bp);
6865
6866 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6867 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006868 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6869 bnxt_hwrm_exec_fwd_req(bp);
6870 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6871 bnxt_hwrm_tunnel_dst_port_alloc(
6872 bp, bp->vxlan_port,
6873 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6874 }
6875 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6876 bnxt_hwrm_tunnel_dst_port_free(
6877 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6878 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006879 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6880 bnxt_hwrm_tunnel_dst_port_alloc(
6881 bp, bp->nge_port,
6882 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6883 }
6884 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6885 bnxt_hwrm_tunnel_dst_port_free(
6886 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6887 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006888 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6889 bnxt_hwrm_port_qstats(bp);
6890
Michael Chana551ee92017-01-25 02:55:07 -05006891 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6892 * must be the last functions to be called before exiting.
6893 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006894 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6895 int rc = 0;
6896
6897 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6898 &bp->sp_event))
6899 bnxt_hwrm_phy_qcaps(bp);
6900
6901 bnxt_rtnl_lock_sp(bp);
6902 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6903 rc = bnxt_update_link(bp, true);
6904 bnxt_rtnl_unlock_sp(bp);
6905 if (rc)
6906 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6907 rc);
6908 }
Michael Chan90c694b2017-01-25 02:55:09 -05006909 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6910 bnxt_rtnl_lock_sp(bp);
6911 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6912 bnxt_get_port_module_status(bp);
6913 bnxt_rtnl_unlock_sp(bp);
6914 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006915 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6916 bnxt_reset(bp, false);
6917
6918 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6919 bnxt_reset(bp, true);
6920
Michael Chanc0c050c2015-10-22 16:01:17 -04006921 smp_mb__before_atomic();
6922 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6923}
6924
Michael Chand1e79252017-02-06 16:55:38 -05006925/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04006926int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
6927 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006928{
6929 int max_rx, max_tx, tx_sets = 1;
6930 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05006931 int rc;
6932
Michael Chand1e79252017-02-06 16:55:38 -05006933 if (tcs)
6934 tx_sets = tcs;
6935
6936 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6937 if (rc)
6938 return rc;
6939
6940 if (max_rx < rx)
6941 return -ENOMEM;
6942
Michael Chan5f449242017-02-06 16:55:40 -05006943 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006944 if (max_tx < tx_rings_needed)
6945 return -ENOMEM;
6946
6947 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006948 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006949 return -ENOMEM;
6950 return 0;
6951}
6952
Sathya Perla17086392017-02-20 19:25:18 -05006953static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6954{
6955 if (bp->bar2) {
6956 pci_iounmap(pdev, bp->bar2);
6957 bp->bar2 = NULL;
6958 }
6959
6960 if (bp->bar1) {
6961 pci_iounmap(pdev, bp->bar1);
6962 bp->bar1 = NULL;
6963 }
6964
6965 if (bp->bar0) {
6966 pci_iounmap(pdev, bp->bar0);
6967 bp->bar0 = NULL;
6968 }
6969}
6970
6971static void bnxt_cleanup_pci(struct bnxt *bp)
6972{
6973 bnxt_unmap_bars(bp, bp->pdev);
6974 pci_release_regions(bp->pdev);
6975 pci_disable_device(bp->pdev);
6976}
6977
Michael Chanc0c050c2015-10-22 16:01:17 -04006978static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6979{
6980 int rc;
6981 struct bnxt *bp = netdev_priv(dev);
6982
6983 SET_NETDEV_DEV(dev, &pdev->dev);
6984
6985 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6986 rc = pci_enable_device(pdev);
6987 if (rc) {
6988 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6989 goto init_err;
6990 }
6991
6992 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6993 dev_err(&pdev->dev,
6994 "Cannot find PCI device base address, aborting\n");
6995 rc = -ENODEV;
6996 goto init_err_disable;
6997 }
6998
6999 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7000 if (rc) {
7001 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7002 goto init_err_disable;
7003 }
7004
7005 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7006 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7007 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7008 goto init_err_disable;
7009 }
7010
7011 pci_set_master(pdev);
7012
7013 bp->dev = dev;
7014 bp->pdev = pdev;
7015
7016 bp->bar0 = pci_ioremap_bar(pdev, 0);
7017 if (!bp->bar0) {
7018 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7019 rc = -ENOMEM;
7020 goto init_err_release;
7021 }
7022
7023 bp->bar1 = pci_ioremap_bar(pdev, 2);
7024 if (!bp->bar1) {
7025 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7026 rc = -ENOMEM;
7027 goto init_err_release;
7028 }
7029
7030 bp->bar2 = pci_ioremap_bar(pdev, 4);
7031 if (!bp->bar2) {
7032 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7033 rc = -ENOMEM;
7034 goto init_err_release;
7035 }
7036
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007037 pci_enable_pcie_error_reporting(pdev);
7038
Michael Chanc0c050c2015-10-22 16:01:17 -04007039 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7040
7041 spin_lock_init(&bp->ntp_fltr_lock);
7042
7043 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7044 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7045
Michael Chandfb5b892016-02-26 04:00:01 -05007046 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007047 bp->rx_coal_ticks = 12;
7048 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007049 bp->rx_coal_ticks_irq = 1;
7050 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007051
Michael Chandfc9c942016-02-26 04:00:03 -05007052 bp->tx_coal_ticks = 25;
7053 bp->tx_coal_bufs = 30;
7054 bp->tx_coal_ticks_irq = 2;
7055 bp->tx_coal_bufs_irq = 2;
7056
Michael Chan51f30782016-07-01 18:46:29 -04007057 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7058
Michael Chanc0c050c2015-10-22 16:01:17 -04007059 init_timer(&bp->timer);
7060 bp->timer.data = (unsigned long)bp;
7061 bp->timer.function = bnxt_timer;
7062 bp->current_interval = BNXT_TIMER_INTERVAL;
7063
Michael Chancaefe522015-12-09 19:35:42 -05007064 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007065 return 0;
7066
7067init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007068 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007069 pci_release_regions(pdev);
7070
7071init_err_disable:
7072 pci_disable_device(pdev);
7073
7074init_err:
7075 return rc;
7076}
7077
7078/* rtnl_lock held */
7079static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7080{
7081 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007082 struct bnxt *bp = netdev_priv(dev);
7083 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007084
7085 if (!is_valid_ether_addr(addr->sa_data))
7086 return -EADDRNOTAVAIL;
7087
Michael Chan84c33dd2016-04-11 04:11:13 -04007088 rc = bnxt_approve_mac(bp, addr->sa_data);
7089 if (rc)
7090 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007091
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007092 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7093 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007094
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007095 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7096 if (netif_running(dev)) {
7097 bnxt_close_nic(bp, false, false);
7098 rc = bnxt_open_nic(bp, false, false);
7099 }
7100
7101 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007102}
7103
7104/* rtnl_lock held */
7105static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7106{
7107 struct bnxt *bp = netdev_priv(dev);
7108
Michael Chanc0c050c2015-10-22 16:01:17 -04007109 if (netif_running(dev))
7110 bnxt_close_nic(bp, false, false);
7111
7112 dev->mtu = new_mtu;
7113 bnxt_set_ring_params(bp);
7114
7115 if (netif_running(dev))
7116 return bnxt_open_nic(bp, false, false);
7117
7118 return 0;
7119}
7120
Michael Chanc5e3deb2016-12-02 21:17:15 -05007121int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007122{
7123 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007124 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007125 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007126
Michael Chanc0c050c2015-10-22 16:01:17 -04007127 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007128 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007129 tc, bp->max_tc);
7130 return -EINVAL;
7131 }
7132
7133 if (netdev_get_num_tc(dev) == tc)
7134 return 0;
7135
Michael Chan3ffb6a32016-11-11 00:11:42 -05007136 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7137 sh = true;
7138
Michael Chan5f449242017-02-06 16:55:40 -05007139 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007140 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007141 if (rc)
7142 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007143
7144 /* Needs to close the device and do hw resource re-allocations */
7145 if (netif_running(bp->dev))
7146 bnxt_close_nic(bp, true, false);
7147
7148 if (tc) {
7149 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7150 netdev_set_num_tc(dev, tc);
7151 } else {
7152 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7153 netdev_reset_tc(dev);
7154 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007155 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7156 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007157 bp->num_stat_ctxs = bp->cp_nr_rings;
7158
7159 if (netif_running(bp->dev))
7160 return bnxt_open_nic(bp, true, false);
7161
7162 return 0;
7163}
7164
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007165static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7166 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007167{
7168 if (ntc->type != TC_SETUP_MQPRIO)
7169 return -EINVAL;
7170
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007171 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7172
7173 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007174}
7175
Michael Chanc0c050c2015-10-22 16:01:17 -04007176#ifdef CONFIG_RFS_ACCEL
7177static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7178 struct bnxt_ntuple_filter *f2)
7179{
7180 struct flow_keys *keys1 = &f1->fkeys;
7181 struct flow_keys *keys2 = &f2->fkeys;
7182
7183 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7184 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7185 keys1->ports.ports == keys2->ports.ports &&
7186 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7187 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007188 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007189 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7190 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007191 return true;
7192
7193 return false;
7194}
7195
7196static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7197 u16 rxq_index, u32 flow_id)
7198{
7199 struct bnxt *bp = netdev_priv(dev);
7200 struct bnxt_ntuple_filter *fltr, *new_fltr;
7201 struct flow_keys *fkeys;
7202 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007203 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007204 struct hlist_head *head;
7205
Michael Chana54c4d72016-07-25 12:33:35 -04007206 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7207 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7208 int off = 0, j;
7209
7210 netif_addr_lock_bh(dev);
7211 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7212 if (ether_addr_equal(eth->h_dest,
7213 vnic->uc_list + off)) {
7214 l2_idx = j + 1;
7215 break;
7216 }
7217 }
7218 netif_addr_unlock_bh(dev);
7219 if (!l2_idx)
7220 return -EINVAL;
7221 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007222 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7223 if (!new_fltr)
7224 return -ENOMEM;
7225
7226 fkeys = &new_fltr->fkeys;
7227 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7228 rc = -EPROTONOSUPPORT;
7229 goto err_free;
7230 }
7231
Michael Chandda0e742016-12-29 12:13:40 -05007232 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7233 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007234 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7235 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7236 rc = -EPROTONOSUPPORT;
7237 goto err_free;
7238 }
Michael Chandda0e742016-12-29 12:13:40 -05007239 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7240 bp->hwrm_spec_code < 0x10601) {
7241 rc = -EPROTONOSUPPORT;
7242 goto err_free;
7243 }
Michael Chan61aad722017-02-12 19:18:14 -05007244 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7245 bp->hwrm_spec_code < 0x10601) {
7246 rc = -EPROTONOSUPPORT;
7247 goto err_free;
7248 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007249
Michael Chana54c4d72016-07-25 12:33:35 -04007250 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007251 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7252
7253 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7254 head = &bp->ntp_fltr_hash_tbl[idx];
7255 rcu_read_lock();
7256 hlist_for_each_entry_rcu(fltr, head, hash) {
7257 if (bnxt_fltr_match(fltr, new_fltr)) {
7258 rcu_read_unlock();
7259 rc = 0;
7260 goto err_free;
7261 }
7262 }
7263 rcu_read_unlock();
7264
7265 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007266 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7267 BNXT_NTP_FLTR_MAX_FLTR, 0);
7268 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007269 spin_unlock_bh(&bp->ntp_fltr_lock);
7270 rc = -ENOMEM;
7271 goto err_free;
7272 }
7273
Michael Chan84e86b92015-11-05 16:25:50 -05007274 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007275 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007276 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007277 new_fltr->rxq = rxq_index;
7278 hlist_add_head_rcu(&new_fltr->hash, head);
7279 bp->ntp_fltr_count++;
7280 spin_unlock_bh(&bp->ntp_fltr_lock);
7281
7282 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7283 schedule_work(&bp->sp_task);
7284
7285 return new_fltr->sw_id;
7286
7287err_free:
7288 kfree(new_fltr);
7289 return rc;
7290}
7291
7292static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7293{
7294 int i;
7295
7296 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7297 struct hlist_head *head;
7298 struct hlist_node *tmp;
7299 struct bnxt_ntuple_filter *fltr;
7300 int rc;
7301
7302 head = &bp->ntp_fltr_hash_tbl[i];
7303 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7304 bool del = false;
7305
7306 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7307 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7308 fltr->flow_id,
7309 fltr->sw_id)) {
7310 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7311 fltr);
7312 del = true;
7313 }
7314 } else {
7315 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7316 fltr);
7317 if (rc)
7318 del = true;
7319 else
7320 set_bit(BNXT_FLTR_VALID, &fltr->state);
7321 }
7322
7323 if (del) {
7324 spin_lock_bh(&bp->ntp_fltr_lock);
7325 hlist_del_rcu(&fltr->hash);
7326 bp->ntp_fltr_count--;
7327 spin_unlock_bh(&bp->ntp_fltr_lock);
7328 synchronize_rcu();
7329 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7330 kfree(fltr);
7331 }
7332 }
7333 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007334 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7335 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007336}
7337
7338#else
7339
7340static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7341{
7342}
7343
7344#endif /* CONFIG_RFS_ACCEL */
7345
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007346static void bnxt_udp_tunnel_add(struct net_device *dev,
7347 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007348{
7349 struct bnxt *bp = netdev_priv(dev);
7350
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007351 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7352 return;
7353
Michael Chanc0c050c2015-10-22 16:01:17 -04007354 if (!netif_running(dev))
7355 return;
7356
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007357 switch (ti->type) {
7358 case UDP_TUNNEL_TYPE_VXLAN:
7359 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7360 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007361
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007362 bp->vxlan_port_cnt++;
7363 if (bp->vxlan_port_cnt == 1) {
7364 bp->vxlan_port = ti->port;
7365 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007366 schedule_work(&bp->sp_task);
7367 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007368 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007369 case UDP_TUNNEL_TYPE_GENEVE:
7370 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7371 return;
7372
7373 bp->nge_port_cnt++;
7374 if (bp->nge_port_cnt == 1) {
7375 bp->nge_port = ti->port;
7376 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7377 }
7378 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007379 default:
7380 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007381 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007382
7383 schedule_work(&bp->sp_task);
7384}
7385
7386static void bnxt_udp_tunnel_del(struct net_device *dev,
7387 struct udp_tunnel_info *ti)
7388{
7389 struct bnxt *bp = netdev_priv(dev);
7390
7391 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7392 return;
7393
7394 if (!netif_running(dev))
7395 return;
7396
7397 switch (ti->type) {
7398 case UDP_TUNNEL_TYPE_VXLAN:
7399 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7400 return;
7401 bp->vxlan_port_cnt--;
7402
7403 if (bp->vxlan_port_cnt != 0)
7404 return;
7405
7406 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7407 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007408 case UDP_TUNNEL_TYPE_GENEVE:
7409 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7410 return;
7411 bp->nge_port_cnt--;
7412
7413 if (bp->nge_port_cnt != 0)
7414 return;
7415
7416 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7417 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007418 default:
7419 return;
7420 }
7421
7422 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007423}
7424
7425static const struct net_device_ops bnxt_netdev_ops = {
7426 .ndo_open = bnxt_open,
7427 .ndo_start_xmit = bnxt_start_xmit,
7428 .ndo_stop = bnxt_close,
7429 .ndo_get_stats64 = bnxt_get_stats64,
7430 .ndo_set_rx_mode = bnxt_set_rx_mode,
7431 .ndo_do_ioctl = bnxt_ioctl,
7432 .ndo_validate_addr = eth_validate_addr,
7433 .ndo_set_mac_address = bnxt_change_mac_addr,
7434 .ndo_change_mtu = bnxt_change_mtu,
7435 .ndo_fix_features = bnxt_fix_features,
7436 .ndo_set_features = bnxt_set_features,
7437 .ndo_tx_timeout = bnxt_tx_timeout,
7438#ifdef CONFIG_BNXT_SRIOV
7439 .ndo_get_vf_config = bnxt_get_vf_config,
7440 .ndo_set_vf_mac = bnxt_set_vf_mac,
7441 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7442 .ndo_set_vf_rate = bnxt_set_vf_bw,
7443 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7444 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7445#endif
7446#ifdef CONFIG_NET_POLL_CONTROLLER
7447 .ndo_poll_controller = bnxt_poll_controller,
7448#endif
7449 .ndo_setup_tc = bnxt_setup_tc,
7450#ifdef CONFIG_RFS_ACCEL
7451 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7452#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007453 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7454 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007455 .ndo_xdp = bnxt_xdp,
Michael Chanc0c050c2015-10-22 16:01:17 -04007456};
7457
7458static void bnxt_remove_one(struct pci_dev *pdev)
7459{
7460 struct net_device *dev = pci_get_drvdata(pdev);
7461 struct bnxt *bp = netdev_priv(dev);
7462
7463 if (BNXT_PF(bp))
7464 bnxt_sriov_disable(bp);
7465
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007466 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007467 unregister_netdev(dev);
7468 cancel_work_sync(&bp->sp_task);
7469 bp->sp_event = 0;
7470
Michael Chan78095922016-12-07 00:26:16 -05007471 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007472 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007473 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007474 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007475 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007476 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007477 kfree(bp->edev);
7478 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007479 if (bp->xdp_prog)
7480 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007481 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007482 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007483}
7484
7485static int bnxt_probe_phy(struct bnxt *bp)
7486{
7487 int rc = 0;
7488 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007489
Michael Chan170ce012016-04-05 14:08:57 -04007490 rc = bnxt_hwrm_phy_qcaps(bp);
7491 if (rc) {
7492 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7493 rc);
7494 return rc;
7495 }
7496
Michael Chanc0c050c2015-10-22 16:01:17 -04007497 rc = bnxt_update_link(bp, false);
7498 if (rc) {
7499 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7500 rc);
7501 return rc;
7502 }
7503
Michael Chan93ed8112016-06-13 02:25:37 -04007504 /* Older firmware does not have supported_auto_speeds, so assume
7505 * that all supported speeds can be autonegotiated.
7506 */
7507 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7508 link_info->support_auto_speeds = link_info->support_speeds;
7509
Michael Chanc0c050c2015-10-22 16:01:17 -04007510 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007511 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007512 link_info->autoneg = BNXT_AUTONEG_SPEED;
7513 if (bp->hwrm_spec_code >= 0x10201) {
7514 if (link_info->auto_pause_setting &
7515 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7516 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7517 } else {
7518 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7519 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007520 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007521 } else {
7522 link_info->req_link_speed = link_info->force_link_speed;
7523 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007524 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007525 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7526 link_info->req_flow_ctrl =
7527 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7528 else
7529 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007530 return rc;
7531}
7532
7533static int bnxt_get_max_irq(struct pci_dev *pdev)
7534{
7535 u16 ctrl;
7536
7537 if (!pdev->msix_cap)
7538 return 1;
7539
7540 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7541 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7542}
7543
Michael Chan6e6c5a52016-01-02 23:45:02 -05007544static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7545 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007546{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007547 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007548
Michael Chan379a80a2015-10-23 15:06:19 -04007549#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007550 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007551 *max_tx = bp->vf.max_tx_rings;
7552 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007553 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7554 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007555 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007556 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007557#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007558 {
7559 *max_tx = bp->pf.max_tx_rings;
7560 *max_rx = bp->pf.max_rx_rings;
7561 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7562 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7563 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007564 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007565 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7566 *max_cp -= 1;
7567 *max_rx -= 2;
7568 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007569 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7570 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007571 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007572}
7573
7574int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7575{
7576 int rx, tx, cp;
7577
7578 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7579 if (!rx || !tx || !cp)
7580 return -ENOMEM;
7581
7582 *max_rx = rx;
7583 *max_tx = tx;
7584 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7585}
7586
Michael Chane4060d32016-12-07 00:26:19 -05007587static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7588 bool shared)
7589{
7590 int rc;
7591
7592 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007593 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7594 /* Not enough rings, try disabling agg rings. */
7595 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7596 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7597 if (rc)
7598 return rc;
7599 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7600 bp->dev->hw_features &= ~NETIF_F_LRO;
7601 bp->dev->features &= ~NETIF_F_LRO;
7602 bnxt_set_ring_params(bp);
7603 }
Michael Chane4060d32016-12-07 00:26:19 -05007604
7605 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7606 int max_cp, max_stat, max_irq;
7607
7608 /* Reserve minimum resources for RoCE */
7609 max_cp = bnxt_get_max_func_cp_rings(bp);
7610 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7611 max_irq = bnxt_get_max_func_irqs(bp);
7612 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7613 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7614 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7615 return 0;
7616
7617 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7618 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7619 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7620 max_cp = min_t(int, max_cp, max_irq);
7621 max_cp = min_t(int, max_cp, max_stat);
7622 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7623 if (rc)
7624 rc = 0;
7625 }
7626 return rc;
7627}
7628
Michael Chan702c2212017-05-29 19:06:10 -04007629static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007630{
7631 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007632
7633 if (sh)
7634 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7635 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007636 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007637 if (rc)
7638 return rc;
7639 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7640 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007641
7642 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7643 if (rc)
7644 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7645
Michael Chan6e6c5a52016-01-02 23:45:02 -05007646 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7647 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7648 bp->tx_nr_rings + bp->rx_nr_rings;
7649 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007650 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7651 bp->rx_nr_rings++;
7652 bp->cp_nr_rings++;
7653 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007654 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007655}
7656
Michael Chan7b08f662016-12-07 00:26:18 -05007657void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7658{
7659 ASSERT_RTNL();
7660 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007661 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007662}
7663
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007664static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7665{
7666 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7667 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7668
7669 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7670 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7671 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7672 else
7673 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7674 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7675 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7676 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7677 "Unknown", width);
7678}
7679
Michael Chanc0c050c2015-10-22 16:01:17 -04007680static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7681{
7682 static int version_printed;
7683 struct net_device *dev;
7684 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007685 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007686
Ray Jui4e003382017-02-20 19:25:16 -05007687 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007688 return -ENODEV;
7689
Michael Chanc0c050c2015-10-22 16:01:17 -04007690 if (version_printed++ == 0)
7691 pr_info("%s", version);
7692
7693 max_irqs = bnxt_get_max_irq(pdev);
7694 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7695 if (!dev)
7696 return -ENOMEM;
7697
7698 bp = netdev_priv(dev);
7699
7700 if (bnxt_vf_pciid(ent->driver_data))
7701 bp->flags |= BNXT_FLAG_VF;
7702
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007703 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007704 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007705
7706 rc = bnxt_init_board(pdev, dev);
7707 if (rc < 0)
7708 goto init_err_free;
7709
7710 dev->netdev_ops = &bnxt_netdev_ops;
7711 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7712 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007713 pci_set_drvdata(pdev, dev);
7714
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007715 rc = bnxt_alloc_hwrm_resources(bp);
7716 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007717 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007718
7719 mutex_init(&bp->hwrm_cmd_lock);
7720 rc = bnxt_hwrm_ver_get(bp);
7721 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007722 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007723
Deepak Khungare605db82017-05-29 19:06:04 -04007724 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7725 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7726 if (rc)
7727 goto init_err_pci_clean;
7728 }
7729
Michael Chan3c2217a2017-03-08 18:44:32 -05007730 rc = bnxt_hwrm_func_reset(bp);
7731 if (rc)
7732 goto init_err_pci_clean;
7733
Rob Swindell5ac67d82016-09-19 03:58:03 -04007734 bnxt_hwrm_fw_set_time(bp);
7735
Michael Chanc0c050c2015-10-22 16:01:17 -04007736 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7737 NETIF_F_TSO | NETIF_F_TSO6 |
7738 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007739 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007740 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7741 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007742 NETIF_F_RXCSUM | NETIF_F_GRO;
7743
7744 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7745 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007746
Michael Chanc0c050c2015-10-22 16:01:17 -04007747 dev->hw_enc_features =
7748 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7749 NETIF_F_TSO | NETIF_F_TSO6 |
7750 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007751 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007752 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007753 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7754 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007755 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7756 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7757 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7758 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7759 dev->priv_flags |= IFF_UNICAST_FLT;
7760
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007761 /* MTU range: 60 - 9500 */
7762 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007763 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007764
Michael Chanc0c050c2015-10-22 16:01:17 -04007765#ifdef CONFIG_BNXT_SRIOV
7766 init_waitqueue_head(&bp->sriov_cfg_wait);
7767#endif
Michael Chan309369c2016-06-13 02:25:34 -04007768 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007769 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007770 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007771 else
7772 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007773
Michael Chanc0c050c2015-10-22 16:01:17 -04007774 rc = bnxt_hwrm_func_drv_rgtr(bp);
7775 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007776 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007777
Michael Chana1653b12016-12-07 00:26:20 -05007778 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7779 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007780 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007781
Michael Chana588e452016-12-07 00:26:21 -05007782 bp->ulp_probe = bnxt_ulp_probe;
7783
Michael Chanc0c050c2015-10-22 16:01:17 -04007784 /* Get the MAX capabilities for this function */
7785 rc = bnxt_hwrm_func_qcaps(bp);
7786 if (rc) {
7787 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7788 rc);
7789 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007790 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007791 }
7792
7793 rc = bnxt_hwrm_queue_qportcfg(bp);
7794 if (rc) {
7795 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7796 rc);
7797 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007798 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007799 }
7800
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007801 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007802 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007803 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007804 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007805
Michael Chanc61fb992017-02-06 16:55:36 -05007806 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007807 bnxt_set_tpa_flags(bp);
7808 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007809 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007810 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007811 if (rc) {
7812 netdev_err(bp->dev, "Not enough rings available.\n");
7813 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007814 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007815 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007816
Michael Chan87da7f72016-11-16 21:13:09 -05007817 /* Default RSS hash cfg. */
7818 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7819 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7820 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7821 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007822 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007823 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7824 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7825 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7826 }
7827
Michael Chan8fdefd62016-12-29 12:13:36 -05007828 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007829 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007830 dev->hw_features |= NETIF_F_NTUPLE;
7831 if (bnxt_rfs_capable(bp)) {
7832 bp->flags |= BNXT_FLAG_RFS;
7833 dev->features |= NETIF_F_NTUPLE;
7834 }
7835 }
7836
Michael Chanc0c050c2015-10-22 16:01:17 -04007837 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7838 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7839
7840 rc = bnxt_probe_phy(bp);
7841 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007842 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007843
Michael Chan78095922016-12-07 00:26:16 -05007844 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007845 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007846 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007847
Michael Chanc1ef1462017-04-04 18:14:07 -04007848 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007849 if (bp->flags & BNXT_FLAG_WOL_CAP)
7850 device_set_wakeup_enable(&pdev->dev, bp->wol);
7851 else
7852 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007853
Michael Chan78095922016-12-07 00:26:16 -05007854 rc = register_netdev(dev);
7855 if (rc)
7856 goto init_err_clr_int;
7857
Michael Chanc0c050c2015-10-22 16:01:17 -04007858 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7859 board_info[ent->driver_data].name,
7860 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7861
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007862 bnxt_parse_log_pcie_link(bp);
7863
Michael Chanc0c050c2015-10-22 16:01:17 -04007864 return 0;
7865
Michael Chan78095922016-12-07 00:26:16 -05007866init_err_clr_int:
7867 bnxt_clear_int_mode(bp);
7868
Sathya Perla17086392017-02-20 19:25:18 -05007869init_err_pci_clean:
7870 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007871
7872init_err_free:
7873 free_netdev(dev);
7874 return rc;
7875}
7876
Michael Chand196ece2017-04-04 18:14:08 -04007877static void bnxt_shutdown(struct pci_dev *pdev)
7878{
7879 struct net_device *dev = pci_get_drvdata(pdev);
7880 struct bnxt *bp;
7881
7882 if (!dev)
7883 return;
7884
7885 rtnl_lock();
7886 bp = netdev_priv(dev);
7887 if (!bp)
7888 goto shutdown_exit;
7889
7890 if (netif_running(dev))
7891 dev_close(dev);
7892
7893 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04007894 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007895 bnxt_clear_int_mode(bp);
7896 pci_wake_from_d3(pdev, bp->wol);
7897 pci_set_power_state(pdev, PCI_D3hot);
7898 }
7899
7900shutdown_exit:
7901 rtnl_unlock();
7902}
7903
Michael Chanf65a2042017-04-04 18:14:11 -04007904#ifdef CONFIG_PM_SLEEP
7905static int bnxt_suspend(struct device *device)
7906{
7907 struct pci_dev *pdev = to_pci_dev(device);
7908 struct net_device *dev = pci_get_drvdata(pdev);
7909 struct bnxt *bp = netdev_priv(dev);
7910 int rc = 0;
7911
7912 rtnl_lock();
7913 if (netif_running(dev)) {
7914 netif_device_detach(dev);
7915 rc = bnxt_close(dev);
7916 }
7917 bnxt_hwrm_func_drv_unrgtr(bp);
7918 rtnl_unlock();
7919 return rc;
7920}
7921
7922static int bnxt_resume(struct device *device)
7923{
7924 struct pci_dev *pdev = to_pci_dev(device);
7925 struct net_device *dev = pci_get_drvdata(pdev);
7926 struct bnxt *bp = netdev_priv(dev);
7927 int rc = 0;
7928
7929 rtnl_lock();
7930 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
7931 rc = -ENODEV;
7932 goto resume_exit;
7933 }
7934 rc = bnxt_hwrm_func_reset(bp);
7935 if (rc) {
7936 rc = -EBUSY;
7937 goto resume_exit;
7938 }
7939 bnxt_get_wol_settings(bp);
7940 if (netif_running(dev)) {
7941 rc = bnxt_open(dev);
7942 if (!rc)
7943 netif_device_attach(dev);
7944 }
7945
7946resume_exit:
7947 rtnl_unlock();
7948 return rc;
7949}
7950
7951static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
7952#define BNXT_PM_OPS (&bnxt_pm_ops)
7953
7954#else
7955
7956#define BNXT_PM_OPS NULL
7957
7958#endif /* CONFIG_PM_SLEEP */
7959
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007960/**
7961 * bnxt_io_error_detected - called when PCI error is detected
7962 * @pdev: Pointer to PCI device
7963 * @state: The current pci connection state
7964 *
7965 * This function is called after a PCI bus error affecting
7966 * this device has been detected.
7967 */
7968static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7969 pci_channel_state_t state)
7970{
7971 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007972 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007973
7974 netdev_info(netdev, "PCI I/O error detected\n");
7975
7976 rtnl_lock();
7977 netif_device_detach(netdev);
7978
Michael Chana588e452016-12-07 00:26:21 -05007979 bnxt_ulp_stop(bp);
7980
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007981 if (state == pci_channel_io_perm_failure) {
7982 rtnl_unlock();
7983 return PCI_ERS_RESULT_DISCONNECT;
7984 }
7985
7986 if (netif_running(netdev))
7987 bnxt_close(netdev);
7988
7989 pci_disable_device(pdev);
7990 rtnl_unlock();
7991
7992 /* Request a slot slot reset. */
7993 return PCI_ERS_RESULT_NEED_RESET;
7994}
7995
7996/**
7997 * bnxt_io_slot_reset - called after the pci bus has been reset.
7998 * @pdev: Pointer to PCI device
7999 *
8000 * Restart the card from scratch, as if from a cold-boot.
8001 * At this point, the card has exprienced a hard reset,
8002 * followed by fixups by BIOS, and has its config space
8003 * set up identically to what it was at cold boot.
8004 */
8005static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8006{
8007 struct net_device *netdev = pci_get_drvdata(pdev);
8008 struct bnxt *bp = netdev_priv(netdev);
8009 int err = 0;
8010 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8011
8012 netdev_info(bp->dev, "PCI Slot Reset\n");
8013
8014 rtnl_lock();
8015
8016 if (pci_enable_device(pdev)) {
8017 dev_err(&pdev->dev,
8018 "Cannot re-enable PCI device after reset.\n");
8019 } else {
8020 pci_set_master(pdev);
8021
Michael Chanaa8ed022016-12-07 00:26:17 -05008022 err = bnxt_hwrm_func_reset(bp);
8023 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008024 err = bnxt_open(netdev);
8025
Michael Chana588e452016-12-07 00:26:21 -05008026 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008027 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008028 bnxt_ulp_start(bp);
8029 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008030 }
8031
8032 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8033 dev_close(netdev);
8034
8035 rtnl_unlock();
8036
8037 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8038 if (err) {
8039 dev_err(&pdev->dev,
8040 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8041 err); /* non-fatal, continue */
8042 }
8043
8044 return PCI_ERS_RESULT_RECOVERED;
8045}
8046
8047/**
8048 * bnxt_io_resume - called when traffic can start flowing again.
8049 * @pdev: Pointer to PCI device
8050 *
8051 * This callback is called when the error recovery driver tells
8052 * us that its OK to resume normal operation.
8053 */
8054static void bnxt_io_resume(struct pci_dev *pdev)
8055{
8056 struct net_device *netdev = pci_get_drvdata(pdev);
8057
8058 rtnl_lock();
8059
8060 netif_device_attach(netdev);
8061
8062 rtnl_unlock();
8063}
8064
8065static const struct pci_error_handlers bnxt_err_handler = {
8066 .error_detected = bnxt_io_error_detected,
8067 .slot_reset = bnxt_io_slot_reset,
8068 .resume = bnxt_io_resume
8069};
8070
Michael Chanc0c050c2015-10-22 16:01:17 -04008071static struct pci_driver bnxt_pci_driver = {
8072 .name = DRV_MODULE_NAME,
8073 .id_table = bnxt_pci_tbl,
8074 .probe = bnxt_init_one,
8075 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008076 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008077 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008078 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008079#if defined(CONFIG_BNXT_SRIOV)
8080 .sriov_configure = bnxt_sriov_configure,
8081#endif
8082};
8083
8084module_pci_driver(bnxt_pci_driver);