blob: f262fe6092d7795a430d5649b2e8561e1b921716 [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>
Michael Chan32e8239c2017-07-24 12:34:21 -040036#include <linux/if_bridge.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040037#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050038#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040039#include <net/ip.h>
40#include <net/tcp.h>
41#include <net/udp.h>
42#include <net/checksum.h>
43#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070044#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040045#include <linux/workqueue.h>
46#include <linux/prefetch.h>
47#include <linux/cache.h>
48#include <linux/log2.h>
49#include <linux/aer.h>
50#include <linux/bitmap.h>
51#include <linux/cpu_rmap.h>
52
53#include "bnxt_hsi.h"
54#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050055#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056#include "bnxt_sriov.h"
57#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050058#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050059#include "bnxt_xdp.h"
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040060#include "bnxt_vfr.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040061
62#define BNXT_TX_TIMEOUT (5 * HZ)
63
64static const char version[] =
65 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
66
67MODULE_LICENSE("GPL");
68MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
69MODULE_VERSION(DRV_MODULE_VERSION);
70
71#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
72#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
73#define BNXT_RX_COPY_THRESH 256
74
Michael Chan4419dbe2016-02-10 17:33:49 -050075#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040076
77enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050078 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040079 BCM57302,
80 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040081 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040082 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040083 BCM57311,
84 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050085 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040086 BCM57404,
87 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040088 BCM57402_NPAR,
89 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040090 BCM57412,
91 BCM57414,
92 BCM57416,
93 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040094 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040095 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040096 BCM57417_SFP,
97 BCM57416_SFP,
98 BCM57404_NPAR,
99 BCM57406_NPAR,
100 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400101 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400102 BCM57414_NPAR,
103 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500104 BCM57452,
105 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400106 NETXTREME_E_VF,
107 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400108};
109
110/* indexed by enum above */
111static const struct {
112 char *name;
113} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400114 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
115 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
119 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
120 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
122 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
123 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400124 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400125 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
126 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
127 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
128 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
129 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400130 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400131 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
133 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400134 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
135 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400136 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
137 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400138 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
139 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500140 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
141 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400142 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
143 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400144};
145
146static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400147 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500148 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400149 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400151 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400152 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400153 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500155 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400156 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400158 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400160 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400165 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400175 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400176 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500177 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
178 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400179#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400180 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400182 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
186 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
187 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400188#endif
189 { 0 }
190};
191
192MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
193
194static const u16 bnxt_vf_req_snif[] = {
195 HWRM_FUNC_CFG,
196 HWRM_PORT_PHY_QCFG,
197 HWRM_CFA_L2_FILTER_ALLOC,
198};
199
Michael Chan25be8622016-04-05 14:09:00 -0400200static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500201 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
202 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
203 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
204 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
205 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400206};
207
Michael Chanc0c050c2015-10-22 16:01:17 -0400208static bool bnxt_vf_pciid(enum board_idx idx)
209{
Michael Chanadbc8302016-09-19 03:58:01 -0400210 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400211}
212
213#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
214#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
215#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
216
217#define BNXT_CP_DB_REARM(db, raw_cons) \
218 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
219
220#define BNXT_CP_DB(db, raw_cons) \
221 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
222
223#define BNXT_CP_DB_IRQ_DIS(db) \
224 writel(DB_CP_IRQ_DIS_FLAGS, db)
225
Michael Chan38413402017-02-06 16:55:43 -0500226const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400227 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
228 TX_BD_FLAGS_LHINT_512_TO_1023,
229 TX_BD_FLAGS_LHINT_1024_TO_2047,
230 TX_BD_FLAGS_LHINT_1024_TO_2047,
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 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
245 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
246};
247
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400248static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
249{
250 struct metadata_dst *md_dst = skb_metadata_dst(skb);
251
252 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
253 return 0;
254
255 return md_dst->u.port_info.port_id;
256}
257
Michael Chanc0c050c2015-10-22 16:01:17 -0400258static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
259{
260 struct bnxt *bp = netdev_priv(dev);
261 struct tx_bd *txbd;
262 struct tx_bd_ext *txbd1;
263 struct netdev_queue *txq;
264 int i;
265 dma_addr_t mapping;
266 unsigned int length, pad = 0;
267 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
268 u16 prod, last_frag;
269 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400270 struct bnxt_tx_ring_info *txr;
271 struct bnxt_sw_tx_bd *tx_buf;
272
273 i = skb_get_queue_mapping(skb);
274 if (unlikely(i >= bp->tx_nr_rings)) {
275 dev_kfree_skb_any(skb);
276 return NETDEV_TX_OK;
277 }
278
Michael Chanc0c050c2015-10-22 16:01:17 -0400279 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500280 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400281 prod = txr->tx_prod;
282
283 free_size = bnxt_tx_avail(bp, txr);
284 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
285 netif_tx_stop_queue(txq);
286 return NETDEV_TX_BUSY;
287 }
288
289 length = skb->len;
290 len = skb_headlen(skb);
291 last_frag = skb_shinfo(skb)->nr_frags;
292
293 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
294
295 txbd->tx_bd_opaque = prod;
296
297 tx_buf = &txr->tx_buf_ring[prod];
298 tx_buf->skb = skb;
299 tx_buf->nr_frags = last_frag;
300
301 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400302 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400303 if (skb_vlan_tag_present(skb)) {
304 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
305 skb_vlan_tag_get(skb);
306 /* Currently supports 8021Q, 8021AD vlan offloads
307 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
308 */
309 if (skb->vlan_proto == htons(ETH_P_8021Q))
310 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
311 }
312
313 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500314 struct tx_push_buffer *tx_push_buf = txr->tx_push;
315 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
316 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
317 void *pdata = tx_push_buf->data;
318 u64 *end;
319 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400320
321 /* Set COAL_NOW to be ready quickly for the next push */
322 tx_push->tx_bd_len_flags_type =
323 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
324 TX_BD_TYPE_LONG_TX_BD |
325 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
326 TX_BD_FLAGS_COAL_NOW |
327 TX_BD_FLAGS_PACKET_END |
328 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
329
330 if (skb->ip_summed == CHECKSUM_PARTIAL)
331 tx_push1->tx_bd_hsize_lflags =
332 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
333 else
334 tx_push1->tx_bd_hsize_lflags = 0;
335
336 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400337 tx_push1->tx_bd_cfa_action =
338 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400339
Michael Chanfbb0fa82016-02-22 02:10:26 -0500340 end = pdata + length;
341 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500342 *end = 0;
343
Michael Chanc0c050c2015-10-22 16:01:17 -0400344 skb_copy_from_linear_data(skb, pdata, len);
345 pdata += len;
346 for (j = 0; j < last_frag; j++) {
347 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
348 void *fptr;
349
350 fptr = skb_frag_address_safe(frag);
351 if (!fptr)
352 goto normal_tx;
353
354 memcpy(pdata, fptr, skb_frag_size(frag));
355 pdata += skb_frag_size(frag);
356 }
357
Michael Chan4419dbe2016-02-10 17:33:49 -0500358 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
359 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400360 prod = NEXT_TX(prod);
361 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
362 memcpy(txbd, tx_push1, sizeof(*txbd));
363 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500364 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400365 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
366 txr->tx_prod = prod;
367
Michael Chanb9a84602016-06-06 02:37:14 -0400368 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400369 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400370 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400371
Michael Chan4419dbe2016-02-10 17:33:49 -0500372 push_len = (length + sizeof(*tx_push) + 7) / 8;
373 if (push_len > 16) {
374 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400375 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
376 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500377 } else {
378 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
379 push_len);
380 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400381
Michael Chanc0c050c2015-10-22 16:01:17 -0400382 goto tx_done;
383 }
384
385normal_tx:
386 if (length < BNXT_MIN_PKT_SIZE) {
387 pad = BNXT_MIN_PKT_SIZE - length;
388 if (skb_pad(skb, pad)) {
389 /* SKB already freed. */
390 tx_buf->skb = NULL;
391 return NETDEV_TX_OK;
392 }
393 length = BNXT_MIN_PKT_SIZE;
394 }
395
396 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
397
398 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
399 dev_kfree_skb_any(skb);
400 tx_buf->skb = NULL;
401 return NETDEV_TX_OK;
402 }
403
404 dma_unmap_addr_set(tx_buf, mapping, mapping);
405 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
406 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
407
408 txbd->tx_bd_haddr = cpu_to_le64(mapping);
409
410 prod = NEXT_TX(prod);
411 txbd1 = (struct tx_bd_ext *)
412 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
413
414 txbd1->tx_bd_hsize_lflags = 0;
415 if (skb_is_gso(skb)) {
416 u32 hdr_len;
417
418 if (skb->encapsulation)
419 hdr_len = skb_inner_network_offset(skb) +
420 skb_inner_network_header_len(skb) +
421 inner_tcp_hdrlen(skb);
422 else
423 hdr_len = skb_transport_offset(skb) +
424 tcp_hdrlen(skb);
425
426 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
427 TX_BD_FLAGS_T_IPID |
428 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
429 length = skb_shinfo(skb)->gso_size;
430 txbd1->tx_bd_mss = cpu_to_le32(length);
431 length += hdr_len;
432 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
433 txbd1->tx_bd_hsize_lflags =
434 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
435 txbd1->tx_bd_mss = 0;
436 }
437
438 length >>= 9;
439 flags |= bnxt_lhint_arr[length];
440 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
441
442 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400443 txbd1->tx_bd_cfa_action =
444 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400445 for (i = 0; i < last_frag; i++) {
446 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
447
448 prod = NEXT_TX(prod);
449 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
450
451 len = skb_frag_size(frag);
452 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
453 DMA_TO_DEVICE);
454
455 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
456 goto tx_dma_error;
457
458 tx_buf = &txr->tx_buf_ring[prod];
459 dma_unmap_addr_set(tx_buf, mapping, mapping);
460
461 txbd->tx_bd_haddr = cpu_to_le64(mapping);
462
463 flags = len << TX_BD_LEN_SHIFT;
464 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
465 }
466
467 flags &= ~TX_BD_LEN;
468 txbd->tx_bd_len_flags_type =
469 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
470 TX_BD_FLAGS_PACKET_END);
471
472 netdev_tx_sent_queue(txq, skb->len);
473
474 /* Sync BD data before updating doorbell */
475 wmb();
476
477 prod = NEXT_TX(prod);
478 txr->tx_prod = prod;
479
Michael Chanffe40642017-05-30 20:03:00 -0400480 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400481 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400482
483tx_done:
484
485 mmiowb();
486
487 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400488 if (skb->xmit_more && !tx_buf->is_push)
489 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
490
Michael Chanc0c050c2015-10-22 16:01:17 -0400491 netif_tx_stop_queue(txq);
492
493 /* netif_tx_stop_queue() must be done before checking
494 * tx index in bnxt_tx_avail() below, because in
495 * bnxt_tx_int(), we update tx index before checking for
496 * netif_tx_queue_stopped().
497 */
498 smp_mb();
499 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
500 netif_tx_wake_queue(txq);
501 }
502 return NETDEV_TX_OK;
503
504tx_dma_error:
505 last_frag = i;
506
507 /* start back at beginning and unmap skb */
508 prod = txr->tx_prod;
509 tx_buf = &txr->tx_buf_ring[prod];
510 tx_buf->skb = NULL;
511 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
512 skb_headlen(skb), PCI_DMA_TODEVICE);
513 prod = NEXT_TX(prod);
514
515 /* unmap remaining mapped pages */
516 for (i = 0; i < last_frag; i++) {
517 prod = NEXT_TX(prod);
518 tx_buf = &txr->tx_buf_ring[prod];
519 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
520 skb_frag_size(&skb_shinfo(skb)->frags[i]),
521 PCI_DMA_TODEVICE);
522 }
523
524 dev_kfree_skb_any(skb);
525 return NETDEV_TX_OK;
526}
527
528static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
529{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500530 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500531 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400532 u16 cons = txr->tx_cons;
533 struct pci_dev *pdev = bp->pdev;
534 int i;
535 unsigned int tx_bytes = 0;
536
537 for (i = 0; i < nr_pkts; i++) {
538 struct bnxt_sw_tx_bd *tx_buf;
539 struct sk_buff *skb;
540 int j, last;
541
542 tx_buf = &txr->tx_buf_ring[cons];
543 cons = NEXT_TX(cons);
544 skb = tx_buf->skb;
545 tx_buf->skb = NULL;
546
547 if (tx_buf->is_push) {
548 tx_buf->is_push = 0;
549 goto next_tx_int;
550 }
551
552 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
553 skb_headlen(skb), PCI_DMA_TODEVICE);
554 last = tx_buf->nr_frags;
555
556 for (j = 0; j < last; j++) {
557 cons = NEXT_TX(cons);
558 tx_buf = &txr->tx_buf_ring[cons];
559 dma_unmap_page(
560 &pdev->dev,
561 dma_unmap_addr(tx_buf, mapping),
562 skb_frag_size(&skb_shinfo(skb)->frags[j]),
563 PCI_DMA_TODEVICE);
564 }
565
566next_tx_int:
567 cons = NEXT_TX(cons);
568
569 tx_bytes += skb->len;
570 dev_kfree_skb_any(skb);
571 }
572
573 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
574 txr->tx_cons = cons;
575
576 /* Need to make the tx_cons update visible to bnxt_start_xmit()
577 * before checking for netif_tx_queue_stopped(). Without the
578 * memory barrier, there is a small possibility that bnxt_start_xmit()
579 * will miss it and cause the queue to be stopped forever.
580 */
581 smp_mb();
582
583 if (unlikely(netif_tx_queue_stopped(txq)) &&
584 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
585 __netif_tx_lock(txq, smp_processor_id());
586 if (netif_tx_queue_stopped(txq) &&
587 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
588 txr->dev_state != BNXT_DEV_STATE_CLOSING)
589 netif_tx_wake_queue(txq);
590 __netif_tx_unlock(txq);
591 }
592}
593
Michael Chanc61fb992017-02-06 16:55:36 -0500594static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
595 gfp_t gfp)
596{
597 struct device *dev = &bp->pdev->dev;
598 struct page *page;
599
600 page = alloc_page(gfp);
601 if (!page)
602 return NULL;
603
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700604 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
605 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500606 if (dma_mapping_error(dev, *mapping)) {
607 __free_page(page);
608 return NULL;
609 }
610 *mapping += bp->rx_dma_offset;
611 return page;
612}
613
Michael Chanc0c050c2015-10-22 16:01:17 -0400614static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
615 gfp_t gfp)
616{
617 u8 *data;
618 struct pci_dev *pdev = bp->pdev;
619
620 data = kmalloc(bp->rx_buf_size, gfp);
621 if (!data)
622 return NULL;
623
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700624 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
625 bp->rx_buf_use_size, bp->rx_dir,
626 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400627
628 if (dma_mapping_error(&pdev->dev, *mapping)) {
629 kfree(data);
630 data = NULL;
631 }
632 return data;
633}
634
Michael Chan38413402017-02-06 16:55:43 -0500635int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
636 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400637{
638 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
639 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400640 dma_addr_t mapping;
641
Michael Chanc61fb992017-02-06 16:55:36 -0500642 if (BNXT_RX_PAGE_MODE(bp)) {
643 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400644
Michael Chanc61fb992017-02-06 16:55:36 -0500645 if (!page)
646 return -ENOMEM;
647
648 rx_buf->data = page;
649 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
650 } else {
651 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
652
653 if (!data)
654 return -ENOMEM;
655
656 rx_buf->data = data;
657 rx_buf->data_ptr = data + bp->rx_offset;
658 }
Michael Chan11cd1192017-02-06 16:55:33 -0500659 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400660
661 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400662 return 0;
663}
664
Michael Chanc6d30e82017-02-06 16:55:42 -0500665void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400666{
667 u16 prod = rxr->rx_prod;
668 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
669 struct rx_bd *cons_bd, *prod_bd;
670
671 prod_rx_buf = &rxr->rx_buf_ring[prod];
672 cons_rx_buf = &rxr->rx_buf_ring[cons];
673
674 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500675 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400676
Michael Chan11cd1192017-02-06 16:55:33 -0500677 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400678
679 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
680 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
681
682 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
683}
684
685static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
686{
687 u16 next, max = rxr->rx_agg_bmap_size;
688
689 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
690 if (next >= max)
691 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
692 return next;
693}
694
695static inline int bnxt_alloc_rx_page(struct bnxt *bp,
696 struct bnxt_rx_ring_info *rxr,
697 u16 prod, gfp_t gfp)
698{
699 struct rx_bd *rxbd =
700 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
701 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
702 struct pci_dev *pdev = bp->pdev;
703 struct page *page;
704 dma_addr_t mapping;
705 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400706 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400707
Michael Chan89d0a062016-04-25 02:30:51 -0400708 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
709 page = rxr->rx_page;
710 if (!page) {
711 page = alloc_page(gfp);
712 if (!page)
713 return -ENOMEM;
714 rxr->rx_page = page;
715 rxr->rx_page_offset = 0;
716 }
717 offset = rxr->rx_page_offset;
718 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
719 if (rxr->rx_page_offset == PAGE_SIZE)
720 rxr->rx_page = NULL;
721 else
722 get_page(page);
723 } else {
724 page = alloc_page(gfp);
725 if (!page)
726 return -ENOMEM;
727 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400728
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700729 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
730 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
731 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400732 if (dma_mapping_error(&pdev->dev, mapping)) {
733 __free_page(page);
734 return -EIO;
735 }
736
737 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
738 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
739
740 __set_bit(sw_prod, rxr->rx_agg_bmap);
741 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
742 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
743
744 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400745 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400746 rx_agg_buf->mapping = mapping;
747 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
748 rxbd->rx_bd_opaque = sw_prod;
749 return 0;
750}
751
752static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
753 u32 agg_bufs)
754{
755 struct bnxt *bp = bnapi->bp;
756 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500757 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400758 u16 prod = rxr->rx_agg_prod;
759 u16 sw_prod = rxr->rx_sw_agg_prod;
760 u32 i;
761
762 for (i = 0; i < agg_bufs; i++) {
763 u16 cons;
764 struct rx_agg_cmp *agg;
765 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
766 struct rx_bd *prod_bd;
767 struct page *page;
768
769 agg = (struct rx_agg_cmp *)
770 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
771 cons = agg->rx_agg_cmp_opaque;
772 __clear_bit(cons, rxr->rx_agg_bmap);
773
774 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
775 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
776
777 __set_bit(sw_prod, rxr->rx_agg_bmap);
778 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
779 cons_rx_buf = &rxr->rx_agg_ring[cons];
780
781 /* It is possible for sw_prod to be equal to cons, so
782 * set cons_rx_buf->page to NULL first.
783 */
784 page = cons_rx_buf->page;
785 cons_rx_buf->page = NULL;
786 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400787 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400788
789 prod_rx_buf->mapping = cons_rx_buf->mapping;
790
791 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
792
793 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
794 prod_bd->rx_bd_opaque = sw_prod;
795
796 prod = NEXT_RX_AGG(prod);
797 sw_prod = NEXT_RX_AGG(sw_prod);
798 cp_cons = NEXT_CMP(cp_cons);
799 }
800 rxr->rx_agg_prod = prod;
801 rxr->rx_sw_agg_prod = sw_prod;
802}
803
Michael Chanc61fb992017-02-06 16:55:36 -0500804static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
805 struct bnxt_rx_ring_info *rxr,
806 u16 cons, void *data, u8 *data_ptr,
807 dma_addr_t dma_addr,
808 unsigned int offset_and_len)
809{
810 unsigned int payload = offset_and_len >> 16;
811 unsigned int len = offset_and_len & 0xffff;
812 struct skb_frag_struct *frag;
813 struct page *page = data;
814 u16 prod = rxr->rx_prod;
815 struct sk_buff *skb;
816 int off, err;
817
818 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
819 if (unlikely(err)) {
820 bnxt_reuse_rx_data(rxr, cons, data);
821 return NULL;
822 }
823 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700824 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
825 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500826
827 if (unlikely(!payload))
828 payload = eth_get_headlen(data_ptr, len);
829
830 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
831 if (!skb) {
832 __free_page(page);
833 return NULL;
834 }
835
836 off = (void *)data_ptr - page_address(page);
837 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
838 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
839 payload + NET_IP_ALIGN);
840
841 frag = &skb_shinfo(skb)->frags[0];
842 skb_frag_size_sub(frag, payload);
843 frag->page_offset += payload;
844 skb->data_len -= payload;
845 skb->tail += payload;
846
847 return skb;
848}
849
Michael Chanc0c050c2015-10-22 16:01:17 -0400850static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
851 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500852 void *data, u8 *data_ptr,
853 dma_addr_t dma_addr,
854 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400855{
Michael Chan6bb19472017-02-06 16:55:32 -0500856 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400857 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500858 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400859
860 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
861 if (unlikely(err)) {
862 bnxt_reuse_rx_data(rxr, cons, data);
863 return NULL;
864 }
865
866 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700867 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
868 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400869 if (!skb) {
870 kfree(data);
871 return NULL;
872 }
873
Michael Chanb3dba772017-02-06 16:55:35 -0500874 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500875 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400876 return skb;
877}
878
879static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
880 struct sk_buff *skb, u16 cp_cons,
881 u32 agg_bufs)
882{
883 struct pci_dev *pdev = bp->pdev;
884 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500885 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400886 u16 prod = rxr->rx_agg_prod;
887 u32 i;
888
889 for (i = 0; i < agg_bufs; i++) {
890 u16 cons, frag_len;
891 struct rx_agg_cmp *agg;
892 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
893 struct page *page;
894 dma_addr_t mapping;
895
896 agg = (struct rx_agg_cmp *)
897 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
898 cons = agg->rx_agg_cmp_opaque;
899 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
900 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
901
902 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400903 skb_fill_page_desc(skb, i, cons_rx_buf->page,
904 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400905 __clear_bit(cons, rxr->rx_agg_bmap);
906
907 /* It is possible for bnxt_alloc_rx_page() to allocate
908 * a sw_prod index that equals the cons index, so we
909 * need to clear the cons entry now.
910 */
Michael Chan11cd1192017-02-06 16:55:33 -0500911 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400912 page = cons_rx_buf->page;
913 cons_rx_buf->page = NULL;
914
915 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
916 struct skb_shared_info *shinfo;
917 unsigned int nr_frags;
918
919 shinfo = skb_shinfo(skb);
920 nr_frags = --shinfo->nr_frags;
921 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
922
923 dev_kfree_skb(skb);
924
925 cons_rx_buf->page = page;
926
927 /* Update prod since possibly some pages have been
928 * allocated already.
929 */
930 rxr->rx_agg_prod = prod;
931 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
932 return NULL;
933 }
934
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700935 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
936 PCI_DMA_FROMDEVICE,
937 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400938
939 skb->data_len += frag_len;
940 skb->len += frag_len;
941 skb->truesize += PAGE_SIZE;
942
943 prod = NEXT_RX_AGG(prod);
944 cp_cons = NEXT_CMP(cp_cons);
945 }
946 rxr->rx_agg_prod = prod;
947 return skb;
948}
949
950static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
951 u8 agg_bufs, u32 *raw_cons)
952{
953 u16 last;
954 struct rx_agg_cmp *agg;
955
956 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
957 last = RING_CMP(*raw_cons);
958 agg = (struct rx_agg_cmp *)
959 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
960 return RX_AGG_CMP_VALID(agg, *raw_cons);
961}
962
963static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
964 unsigned int len,
965 dma_addr_t mapping)
966{
967 struct bnxt *bp = bnapi->bp;
968 struct pci_dev *pdev = bp->pdev;
969 struct sk_buff *skb;
970
971 skb = napi_alloc_skb(&bnapi->napi, len);
972 if (!skb)
973 return NULL;
974
Michael Chan745fc052017-02-06 16:55:34 -0500975 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
976 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400977
Michael Chan6bb19472017-02-06 16:55:32 -0500978 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
979 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400980
Michael Chan745fc052017-02-06 16:55:34 -0500981 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
982 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400983
984 skb_put(skb, len);
985 return skb;
986}
987
Michael Chanfa7e2812016-05-10 19:18:00 -0400988static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
989 u32 *raw_cons, void *cmp)
990{
991 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
992 struct rx_cmp *rxcmp = cmp;
993 u32 tmp_raw_cons = *raw_cons;
994 u8 cmp_type, agg_bufs = 0;
995
996 cmp_type = RX_CMP_TYPE(rxcmp);
997
998 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
999 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1000 RX_CMP_AGG_BUFS) >>
1001 RX_CMP_AGG_BUFS_SHIFT;
1002 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1003 struct rx_tpa_end_cmp *tpa_end = cmp;
1004
1005 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1006 RX_TPA_END_CMP_AGG_BUFS) >>
1007 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1008 }
1009
1010 if (agg_bufs) {
1011 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1012 return -EBUSY;
1013 }
1014 *raw_cons = tmp_raw_cons;
1015 return 0;
1016}
1017
1018static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1019{
1020 if (!rxr->bnapi->in_reset) {
1021 rxr->bnapi->in_reset = true;
1022 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1023 schedule_work(&bp->sp_task);
1024 }
1025 rxr->rx_next_cons = 0xffff;
1026}
1027
Michael Chanc0c050c2015-10-22 16:01:17 -04001028static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1029 struct rx_tpa_start_cmp *tpa_start,
1030 struct rx_tpa_start_cmp_ext *tpa_start1)
1031{
1032 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1033 u16 cons, prod;
1034 struct bnxt_tpa_info *tpa_info;
1035 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1036 struct rx_bd *prod_bd;
1037 dma_addr_t mapping;
1038
1039 cons = tpa_start->rx_tpa_start_cmp_opaque;
1040 prod = rxr->rx_prod;
1041 cons_rx_buf = &rxr->rx_buf_ring[cons];
1042 prod_rx_buf = &rxr->rx_buf_ring[prod];
1043 tpa_info = &rxr->rx_tpa[agg_id];
1044
Michael Chanfa7e2812016-05-10 19:18:00 -04001045 if (unlikely(cons != rxr->rx_next_cons)) {
1046 bnxt_sched_reset(bp, rxr);
1047 return;
1048 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001049 /* Store cfa_code in tpa_info to use in tpa_end
1050 * completion processing.
1051 */
1052 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001053 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001054 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001055
1056 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001057 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001058
1059 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1060
1061 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1062
1063 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001064 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001065 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001066 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001067
1068 tpa_info->len =
1069 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1070 RX_TPA_START_CMP_LEN_SHIFT;
1071 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1072 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1073
1074 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1075 tpa_info->gso_type = SKB_GSO_TCPV4;
1076 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1077 if (hash_type == 3)
1078 tpa_info->gso_type = SKB_GSO_TCPV6;
1079 tpa_info->rss_hash =
1080 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1081 } else {
1082 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1083 tpa_info->gso_type = 0;
1084 if (netif_msg_rx_err(bp))
1085 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1086 }
1087 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1088 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001089 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001090
1091 rxr->rx_prod = NEXT_RX(prod);
1092 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001093 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001094 cons_rx_buf = &rxr->rx_buf_ring[cons];
1095
1096 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1097 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1098 cons_rx_buf->data = NULL;
1099}
1100
1101static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1102 u16 cp_cons, u32 agg_bufs)
1103{
1104 if (agg_bufs)
1105 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1106}
1107
Michael Chan94758f82016-06-13 02:25:35 -04001108static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1109 int payload_off, int tcp_ts,
1110 struct sk_buff *skb)
1111{
1112#ifdef CONFIG_INET
1113 struct tcphdr *th;
1114 int len, nw_off;
1115 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1116 u32 hdr_info = tpa_info->hdr_info;
1117 bool loopback = false;
1118
1119 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1120 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1121 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1122
1123 /* If the packet is an internal loopback packet, the offsets will
1124 * have an extra 4 bytes.
1125 */
1126 if (inner_mac_off == 4) {
1127 loopback = true;
1128 } else if (inner_mac_off > 4) {
1129 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1130 ETH_HLEN - 2));
1131
1132 /* We only support inner iPv4/ipv6. If we don't see the
1133 * correct protocol ID, it must be a loopback packet where
1134 * the offsets are off by 4.
1135 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001136 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001137 loopback = true;
1138 }
1139 if (loopback) {
1140 /* internal loopback packet, subtract all offsets by 4 */
1141 inner_ip_off -= 4;
1142 inner_mac_off -= 4;
1143 outer_ip_off -= 4;
1144 }
1145
1146 nw_off = inner_ip_off - ETH_HLEN;
1147 skb_set_network_header(skb, nw_off);
1148 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1149 struct ipv6hdr *iph = ipv6_hdr(skb);
1150
1151 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1152 len = skb->len - skb_transport_offset(skb);
1153 th = tcp_hdr(skb);
1154 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1155 } else {
1156 struct iphdr *iph = ip_hdr(skb);
1157
1158 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1159 len = skb->len - skb_transport_offset(skb);
1160 th = tcp_hdr(skb);
1161 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1162 }
1163
1164 if (inner_mac_off) { /* tunnel */
1165 struct udphdr *uh = NULL;
1166 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1167 ETH_HLEN - 2));
1168
1169 if (proto == htons(ETH_P_IP)) {
1170 struct iphdr *iph = (struct iphdr *)skb->data;
1171
1172 if (iph->protocol == IPPROTO_UDP)
1173 uh = (struct udphdr *)(iph + 1);
1174 } else {
1175 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1176
1177 if (iph->nexthdr == IPPROTO_UDP)
1178 uh = (struct udphdr *)(iph + 1);
1179 }
1180 if (uh) {
1181 if (uh->check)
1182 skb_shinfo(skb)->gso_type |=
1183 SKB_GSO_UDP_TUNNEL_CSUM;
1184 else
1185 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1186 }
1187 }
1188#endif
1189 return skb;
1190}
1191
Michael Chanc0c050c2015-10-22 16:01:17 -04001192#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1193#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1194
Michael Chan309369c2016-06-13 02:25:34 -04001195static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1196 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001197 struct sk_buff *skb)
1198{
Michael Chand1611c32015-10-25 22:27:57 -04001199#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001200 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001201 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001202
Michael Chan309369c2016-06-13 02:25:34 -04001203 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001204 tcp_opt_len = 12;
1205
Michael Chanc0c050c2015-10-22 16:01:17 -04001206 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1207 struct iphdr *iph;
1208
1209 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1210 ETH_HLEN;
1211 skb_set_network_header(skb, nw_off);
1212 iph = ip_hdr(skb);
1213 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1214 len = skb->len - skb_transport_offset(skb);
1215 th = tcp_hdr(skb);
1216 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1217 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1218 struct ipv6hdr *iph;
1219
1220 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1221 ETH_HLEN;
1222 skb_set_network_header(skb, nw_off);
1223 iph = ipv6_hdr(skb);
1224 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1225 len = skb->len - skb_transport_offset(skb);
1226 th = tcp_hdr(skb);
1227 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1228 } else {
1229 dev_kfree_skb_any(skb);
1230 return NULL;
1231 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001232
1233 if (nw_off) { /* tunnel */
1234 struct udphdr *uh = NULL;
1235
1236 if (skb->protocol == htons(ETH_P_IP)) {
1237 struct iphdr *iph = (struct iphdr *)skb->data;
1238
1239 if (iph->protocol == IPPROTO_UDP)
1240 uh = (struct udphdr *)(iph + 1);
1241 } else {
1242 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1243
1244 if (iph->nexthdr == IPPROTO_UDP)
1245 uh = (struct udphdr *)(iph + 1);
1246 }
1247 if (uh) {
1248 if (uh->check)
1249 skb_shinfo(skb)->gso_type |=
1250 SKB_GSO_UDP_TUNNEL_CSUM;
1251 else
1252 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1253 }
1254 }
1255#endif
1256 return skb;
1257}
1258
Michael Chan309369c2016-06-13 02:25:34 -04001259static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1260 struct bnxt_tpa_info *tpa_info,
1261 struct rx_tpa_end_cmp *tpa_end,
1262 struct rx_tpa_end_cmp_ext *tpa_end1,
1263 struct sk_buff *skb)
1264{
1265#ifdef CONFIG_INET
1266 int payload_off;
1267 u16 segs;
1268
1269 segs = TPA_END_TPA_SEGS(tpa_end);
1270 if (segs == 1)
1271 return skb;
1272
1273 NAPI_GRO_CB(skb)->count = segs;
1274 skb_shinfo(skb)->gso_size =
1275 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1276 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1277 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1278 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1279 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1280 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001281 if (likely(skb))
1282 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001283#endif
1284 return skb;
1285}
1286
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001287/* Given the cfa_code of a received packet determine which
1288 * netdev (vf-rep or PF) the packet is destined to.
1289 */
1290static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1291{
1292 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1293
1294 /* if vf-rep dev is NULL, the must belongs to the PF */
1295 return dev ? dev : bp->dev;
1296}
1297
Michael Chanc0c050c2015-10-22 16:01:17 -04001298static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1299 struct bnxt_napi *bnapi,
1300 u32 *raw_cons,
1301 struct rx_tpa_end_cmp *tpa_end,
1302 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001303 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001304{
1305 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001306 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001307 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001308 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001309 u16 cp_cons = RING_CMP(*raw_cons);
1310 unsigned int len;
1311 struct bnxt_tpa_info *tpa_info;
1312 dma_addr_t mapping;
1313 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001314 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001315
Michael Chanfa7e2812016-05-10 19:18:00 -04001316 if (unlikely(bnapi->in_reset)) {
1317 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1318
1319 if (rc < 0)
1320 return ERR_PTR(-EBUSY);
1321 return NULL;
1322 }
1323
Michael Chanc0c050c2015-10-22 16:01:17 -04001324 tpa_info = &rxr->rx_tpa[agg_id];
1325 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001326 data_ptr = tpa_info->data_ptr;
1327 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001328 len = tpa_info->len;
1329 mapping = tpa_info->mapping;
1330
1331 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1332 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1333
1334 if (agg_bufs) {
1335 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1336 return ERR_PTR(-EBUSY);
1337
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001338 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001339 cp_cons = NEXT_CMP(cp_cons);
1340 }
1341
Michael Chan69c149e2017-06-23 14:01:00 -04001342 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001343 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001344 if (agg_bufs > MAX_SKB_FRAGS)
1345 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1346 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001347 return NULL;
1348 }
1349
1350 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001351 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001352 if (!skb) {
1353 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1354 return NULL;
1355 }
1356 } else {
1357 u8 *new_data;
1358 dma_addr_t new_mapping;
1359
1360 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1361 if (!new_data) {
1362 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1363 return NULL;
1364 }
1365
1366 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001367 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001368 tpa_info->mapping = new_mapping;
1369
1370 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001371 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1372 bp->rx_buf_use_size, bp->rx_dir,
1373 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001374
1375 if (!skb) {
1376 kfree(data);
1377 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1378 return NULL;
1379 }
Michael Chanb3dba772017-02-06 16:55:35 -05001380 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001381 skb_put(skb, len);
1382 }
1383
1384 if (agg_bufs) {
1385 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1386 if (!skb) {
1387 /* Page reuse already handled by bnxt_rx_pages(). */
1388 return NULL;
1389 }
1390 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001391
1392 skb->protocol =
1393 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001394
1395 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1396 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1397
Michael Chan8852ddb2016-06-06 02:37:16 -04001398 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1399 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001400 u16 vlan_proto = tpa_info->metadata >>
1401 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001402 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001403
Michael Chan8852ddb2016-06-06 02:37:16 -04001404 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001405 }
1406
1407 skb_checksum_none_assert(skb);
1408 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1409 skb->ip_summed = CHECKSUM_UNNECESSARY;
1410 skb->csum_level =
1411 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1412 }
1413
1414 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001415 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001416
1417 return skb;
1418}
1419
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001420static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1421 struct sk_buff *skb)
1422{
1423 if (skb->dev != bp->dev) {
1424 /* this packet belongs to a vf-rep */
1425 bnxt_vf_rep_rx(bp, skb);
1426 return;
1427 }
1428 skb_record_rx_queue(skb, bnapi->index);
1429 napi_gro_receive(&bnapi->napi, skb);
1430}
1431
Michael Chanc0c050c2015-10-22 16:01:17 -04001432/* returns the following:
1433 * 1 - 1 packet successfully received
1434 * 0 - successful TPA_START, packet not completed yet
1435 * -EBUSY - completion ring does not have all the agg buffers yet
1436 * -ENOMEM - packet aborted due to out of memory
1437 * -EIO - packet aborted due to hw error indicated in BD
1438 */
1439static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001440 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001441{
1442 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001443 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001444 struct net_device *dev = bp->dev;
1445 struct rx_cmp *rxcmp;
1446 struct rx_cmp_ext *rxcmp1;
1447 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001448 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001449 struct bnxt_sw_rx_bd *rx_buf;
1450 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001451 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001452 dma_addr_t dma_addr;
1453 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001454 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001455 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001456 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001457
1458 rxcmp = (struct rx_cmp *)
1459 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1460
1461 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1462 cp_cons = RING_CMP(tmp_raw_cons);
1463 rxcmp1 = (struct rx_cmp_ext *)
1464 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1465
1466 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1467 return -EBUSY;
1468
1469 cmp_type = RX_CMP_TYPE(rxcmp);
1470
1471 prod = rxr->rx_prod;
1472
1473 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1474 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1475 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1476
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001477 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001478 goto next_rx_no_prod;
1479
1480 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1481 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1482 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001483 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001484
1485 if (unlikely(IS_ERR(skb)))
1486 return -EBUSY;
1487
1488 rc = -ENOMEM;
1489 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001490 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001491 rc = 1;
1492 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001493 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001494 goto next_rx_no_prod;
1495 }
1496
1497 cons = rxcmp->rx_cmp_opaque;
1498 rx_buf = &rxr->rx_buf_ring[cons];
1499 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001500 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001501 if (unlikely(cons != rxr->rx_next_cons)) {
1502 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1503
1504 bnxt_sched_reset(bp, rxr);
1505 return rc1;
1506 }
Michael Chan6bb19472017-02-06 16:55:32 -05001507 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001508
Michael Chanc61fb992017-02-06 16:55:36 -05001509 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1510 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001511
1512 if (agg_bufs) {
1513 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1514 return -EBUSY;
1515
1516 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001517 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001518 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001519 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001520
1521 rx_buf->data = NULL;
1522 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1523 bnxt_reuse_rx_data(rxr, cons, data);
1524 if (agg_bufs)
1525 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1526
1527 rc = -EIO;
1528 goto next_rx;
1529 }
1530
1531 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001532 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001533
Michael Chanc6d30e82017-02-06 16:55:42 -05001534 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1535 rc = 1;
1536 goto next_rx;
1537 }
1538
Michael Chanc0c050c2015-10-22 16:01:17 -04001539 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001540 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001541 bnxt_reuse_rx_data(rxr, cons, data);
1542 if (!skb) {
1543 rc = -ENOMEM;
1544 goto next_rx;
1545 }
1546 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001547 u32 payload;
1548
Michael Chanc6d30e82017-02-06 16:55:42 -05001549 if (rx_buf->data_ptr == data_ptr)
1550 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1551 else
1552 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001553 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001554 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001555 if (!skb) {
1556 rc = -ENOMEM;
1557 goto next_rx;
1558 }
1559 }
1560
1561 if (agg_bufs) {
1562 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1563 if (!skb) {
1564 rc = -ENOMEM;
1565 goto next_rx;
1566 }
1567 }
1568
1569 if (RX_CMP_HASH_VALID(rxcmp)) {
1570 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1571 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1572
1573 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1574 if (hash_type != 1 && hash_type != 3)
1575 type = PKT_HASH_TYPE_L3;
1576 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1577 }
1578
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001579 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1580 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001581
Michael Chan8852ddb2016-06-06 02:37:16 -04001582 if ((rxcmp1->rx_cmp_flags2 &
1583 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1584 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001585 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001586 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001587 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1588
Michael Chan8852ddb2016-06-06 02:37:16 -04001589 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001590 }
1591
1592 skb_checksum_none_assert(skb);
1593 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1594 if (dev->features & NETIF_F_RXCSUM) {
1595 skb->ip_summed = CHECKSUM_UNNECESSARY;
1596 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1597 }
1598 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001599 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1600 if (dev->features & NETIF_F_RXCSUM)
1601 cpr->rx_l4_csum_errors++;
1602 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001603 }
1604
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001605 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001606 rc = 1;
1607
1608next_rx:
1609 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001610 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001611
1612next_rx_no_prod:
1613 *raw_cons = tmp_raw_cons;
1614
1615 return rc;
1616}
1617
Michael Chan2270bc52017-06-23 14:01:01 -04001618/* In netpoll mode, if we are using a combined completion ring, we need to
1619 * discard the rx packets and recycle the buffers.
1620 */
1621static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1622 u32 *raw_cons, u8 *event)
1623{
1624 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1625 u32 tmp_raw_cons = *raw_cons;
1626 struct rx_cmp_ext *rxcmp1;
1627 struct rx_cmp *rxcmp;
1628 u16 cp_cons;
1629 u8 cmp_type;
1630
1631 cp_cons = RING_CMP(tmp_raw_cons);
1632 rxcmp = (struct rx_cmp *)
1633 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1634
1635 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1636 cp_cons = RING_CMP(tmp_raw_cons);
1637 rxcmp1 = (struct rx_cmp_ext *)
1638 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1639
1640 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1641 return -EBUSY;
1642
1643 cmp_type = RX_CMP_TYPE(rxcmp);
1644 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1645 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1646 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1647 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1648 struct rx_tpa_end_cmp_ext *tpa_end1;
1649
1650 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1651 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1652 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1653 }
1654 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1655}
1656
Michael Chan4bb13ab2016-04-05 14:09:01 -04001657#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001658 ((data) & \
1659 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001660
Michael Chanc0c050c2015-10-22 16:01:17 -04001661static int bnxt_async_event_process(struct bnxt *bp,
1662 struct hwrm_async_event_cmpl *cmpl)
1663{
1664 u16 event_id = le16_to_cpu(cmpl->event_id);
1665
1666 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1667 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001668 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001669 u32 data1 = le32_to_cpu(cmpl->event_data1);
1670 struct bnxt_link_info *link_info = &bp->link_info;
1671
1672 if (BNXT_VF(bp))
1673 goto async_event_process_exit;
1674 if (data1 & 0x20000) {
1675 u16 fw_speed = link_info->force_link_speed;
1676 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1677
1678 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1679 speed);
1680 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001681 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001682 /* fall thru */
1683 }
Michael Chan87c374d2016-12-02 21:17:16 -05001684 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001685 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001686 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001687 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001688 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001689 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001690 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001691 u32 data1 = le32_to_cpu(cmpl->event_data1);
1692 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1693
1694 if (BNXT_VF(bp))
1695 break;
1696
1697 if (bp->pf.port_id != port_id)
1698 break;
1699
Michael Chan4bb13ab2016-04-05 14:09:01 -04001700 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1701 break;
1702 }
Michael Chan87c374d2016-12-02 21:17:16 -05001703 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001704 if (BNXT_PF(bp))
1705 goto async_event_process_exit;
1706 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1707 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001708 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001709 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001710 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001711 schedule_work(&bp->sp_task);
1712async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001713 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001714 return 0;
1715}
1716
1717static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1718{
1719 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1720 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1721 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1722 (struct hwrm_fwd_req_cmpl *)txcmp;
1723
1724 switch (cmpl_type) {
1725 case CMPL_BASE_TYPE_HWRM_DONE:
1726 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1727 if (seq_id == bp->hwrm_intr_seq_id)
1728 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1729 else
1730 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1731 break;
1732
1733 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1734 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1735
1736 if ((vf_id < bp->pf.first_vf_id) ||
1737 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1738 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1739 vf_id);
1740 return -EINVAL;
1741 }
1742
1743 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1744 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1745 schedule_work(&bp->sp_task);
1746 break;
1747
1748 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1749 bnxt_async_event_process(bp,
1750 (struct hwrm_async_event_cmpl *)txcmp);
1751
1752 default:
1753 break;
1754 }
1755
1756 return 0;
1757}
1758
1759static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1760{
1761 struct bnxt_napi *bnapi = dev_instance;
1762 struct bnxt *bp = bnapi->bp;
1763 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1764 u32 cons = RING_CMP(cpr->cp_raw_cons);
1765
1766 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1767 napi_schedule(&bnapi->napi);
1768 return IRQ_HANDLED;
1769}
1770
1771static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1772{
1773 u32 raw_cons = cpr->cp_raw_cons;
1774 u16 cons = RING_CMP(raw_cons);
1775 struct tx_cmp *txcmp;
1776
1777 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1778
1779 return TX_CMP_VALID(txcmp, raw_cons);
1780}
1781
Michael Chanc0c050c2015-10-22 16:01:17 -04001782static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1783{
1784 struct bnxt_napi *bnapi = dev_instance;
1785 struct bnxt *bp = bnapi->bp;
1786 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1787 u32 cons = RING_CMP(cpr->cp_raw_cons);
1788 u32 int_status;
1789
1790 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1791
1792 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001793 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001794 /* return if erroneous interrupt */
1795 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1796 return IRQ_NONE;
1797 }
1798
1799 /* disable ring IRQ */
1800 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1801
1802 /* Return here if interrupt is shared and is disabled. */
1803 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1804 return IRQ_HANDLED;
1805
1806 napi_schedule(&bnapi->napi);
1807 return IRQ_HANDLED;
1808}
1809
1810static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1811{
1812 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1813 u32 raw_cons = cpr->cp_raw_cons;
1814 u32 cons;
1815 int tx_pkts = 0;
1816 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001817 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001818 struct tx_cmp *txcmp;
1819
1820 while (1) {
1821 int rc;
1822
1823 cons = RING_CMP(raw_cons);
1824 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1825
1826 if (!TX_CMP_VALID(txcmp, raw_cons))
1827 break;
1828
Michael Chan67a95e22016-05-04 16:56:43 -04001829 /* The valid test of the entry must be done first before
1830 * reading any further.
1831 */
Michael Chanb67daab2016-05-15 03:04:51 -04001832 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001833 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1834 tx_pkts++;
1835 /* return full budget so NAPI will complete. */
1836 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1837 rx_pkts = budget;
1838 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001839 if (likely(budget))
1840 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1841 else
1842 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1843 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001844 if (likely(rc >= 0))
1845 rx_pkts += rc;
1846 else if (rc == -EBUSY) /* partial completion */
1847 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001848 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1849 CMPL_BASE_TYPE_HWRM_DONE) ||
1850 (TX_CMP_TYPE(txcmp) ==
1851 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1852 (TX_CMP_TYPE(txcmp) ==
1853 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1854 bnxt_hwrm_handler(bp, txcmp);
1855 }
1856 raw_cons = NEXT_RAW_CMP(raw_cons);
1857
1858 if (rx_pkts == budget)
1859 break;
1860 }
1861
Michael Chan38413402017-02-06 16:55:43 -05001862 if (event & BNXT_TX_EVENT) {
1863 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1864 void __iomem *db = txr->tx_doorbell;
1865 u16 prod = txr->tx_prod;
1866
1867 /* Sync BD data before updating doorbell */
1868 wmb();
1869
Michael Chan434c9752017-05-29 19:06:08 -04001870 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001871 }
1872
Michael Chanc0c050c2015-10-22 16:01:17 -04001873 cpr->cp_raw_cons = raw_cons;
1874 /* ACK completion ring before freeing tx ring and producing new
1875 * buffers in rx/agg rings to prevent overflowing the completion
1876 * ring.
1877 */
1878 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1879
1880 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001881 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001882
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001883 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001884 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001885
Michael Chan434c9752017-05-29 19:06:08 -04001886 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1887 if (event & BNXT_AGG_EVENT)
1888 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1889 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001890 }
1891 return rx_pkts;
1892}
1893
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001894static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1895{
1896 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1897 struct bnxt *bp = bnapi->bp;
1898 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1899 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1900 struct tx_cmp *txcmp;
1901 struct rx_cmp_ext *rxcmp1;
1902 u32 cp_cons, tmp_raw_cons;
1903 u32 raw_cons = cpr->cp_raw_cons;
1904 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001905 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001906
1907 while (1) {
1908 int rc;
1909
1910 cp_cons = RING_CMP(raw_cons);
1911 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1912
1913 if (!TX_CMP_VALID(txcmp, raw_cons))
1914 break;
1915
1916 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1917 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1918 cp_cons = RING_CMP(tmp_raw_cons);
1919 rxcmp1 = (struct rx_cmp_ext *)
1920 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1921
1922 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1923 break;
1924
1925 /* force an error to recycle the buffer */
1926 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1927 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1928
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001929 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001930 if (likely(rc == -EIO))
1931 rx_pkts++;
1932 else if (rc == -EBUSY) /* partial completion */
1933 break;
1934 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1935 CMPL_BASE_TYPE_HWRM_DONE)) {
1936 bnxt_hwrm_handler(bp, txcmp);
1937 } else {
1938 netdev_err(bp->dev,
1939 "Invalid completion received on special ring\n");
1940 }
1941 raw_cons = NEXT_RAW_CMP(raw_cons);
1942
1943 if (rx_pkts == budget)
1944 break;
1945 }
1946
1947 cpr->cp_raw_cons = raw_cons;
1948 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001949 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001950
Michael Chan434c9752017-05-29 19:06:08 -04001951 if (event & BNXT_AGG_EVENT)
1952 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1953 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001954
1955 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001956 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001957 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1958 }
1959 return rx_pkts;
1960}
1961
Michael Chanc0c050c2015-10-22 16:01:17 -04001962static int bnxt_poll(struct napi_struct *napi, int budget)
1963{
1964 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1965 struct bnxt *bp = bnapi->bp;
1966 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1967 int work_done = 0;
1968
Michael Chanc0c050c2015-10-22 16:01:17 -04001969 while (1) {
1970 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1971
1972 if (work_done >= budget)
1973 break;
1974
1975 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001976 if (napi_complete_done(napi, work_done))
1977 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1978 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001979 break;
1980 }
1981 }
1982 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001983 return work_done;
1984}
1985
Michael Chanc0c050c2015-10-22 16:01:17 -04001986static void bnxt_free_tx_skbs(struct bnxt *bp)
1987{
1988 int i, max_idx;
1989 struct pci_dev *pdev = bp->pdev;
1990
Michael Chanb6ab4b02016-01-02 23:44:59 -05001991 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001992 return;
1993
1994 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1995 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001996 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001997 int j;
1998
Michael Chanc0c050c2015-10-22 16:01:17 -04001999 for (j = 0; j < max_idx;) {
2000 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2001 struct sk_buff *skb = tx_buf->skb;
2002 int k, last;
2003
2004 if (!skb) {
2005 j++;
2006 continue;
2007 }
2008
2009 tx_buf->skb = NULL;
2010
2011 if (tx_buf->is_push) {
2012 dev_kfree_skb(skb);
2013 j += 2;
2014 continue;
2015 }
2016
2017 dma_unmap_single(&pdev->dev,
2018 dma_unmap_addr(tx_buf, mapping),
2019 skb_headlen(skb),
2020 PCI_DMA_TODEVICE);
2021
2022 last = tx_buf->nr_frags;
2023 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002024 for (k = 0; k < last; k++, j++) {
2025 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002026 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2027
Michael Chand612a572016-01-28 03:11:22 -05002028 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002029 dma_unmap_page(
2030 &pdev->dev,
2031 dma_unmap_addr(tx_buf, mapping),
2032 skb_frag_size(frag), PCI_DMA_TODEVICE);
2033 }
2034 dev_kfree_skb(skb);
2035 }
2036 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2037 }
2038}
2039
2040static void bnxt_free_rx_skbs(struct bnxt *bp)
2041{
2042 int i, max_idx, max_agg_idx;
2043 struct pci_dev *pdev = bp->pdev;
2044
Michael Chanb6ab4b02016-01-02 23:44:59 -05002045 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002046 return;
2047
2048 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2049 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2050 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002051 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002052 int j;
2053
Michael Chanc0c050c2015-10-22 16:01:17 -04002054 if (rxr->rx_tpa) {
2055 for (j = 0; j < MAX_TPA; j++) {
2056 struct bnxt_tpa_info *tpa_info =
2057 &rxr->rx_tpa[j];
2058 u8 *data = tpa_info->data;
2059
2060 if (!data)
2061 continue;
2062
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002063 dma_unmap_single_attrs(&pdev->dev,
2064 tpa_info->mapping,
2065 bp->rx_buf_use_size,
2066 bp->rx_dir,
2067 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002068
2069 tpa_info->data = NULL;
2070
2071 kfree(data);
2072 }
2073 }
2074
2075 for (j = 0; j < max_idx; j++) {
2076 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002077 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002078 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002079
2080 if (!data)
2081 continue;
2082
Michael Chanc0c050c2015-10-22 16:01:17 -04002083 rx_buf->data = NULL;
2084
Michael Chan3ed3a832017-03-28 19:47:31 -04002085 if (BNXT_RX_PAGE_MODE(bp)) {
2086 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002087 dma_unmap_page_attrs(&pdev->dev, mapping,
2088 PAGE_SIZE, bp->rx_dir,
2089 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002090 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002091 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002092 dma_unmap_single_attrs(&pdev->dev, mapping,
2093 bp->rx_buf_use_size,
2094 bp->rx_dir,
2095 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002096 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002097 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002098 }
2099
2100 for (j = 0; j < max_agg_idx; j++) {
2101 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2102 &rxr->rx_agg_ring[j];
2103 struct page *page = rx_agg_buf->page;
2104
2105 if (!page)
2106 continue;
2107
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002108 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2109 BNXT_RX_PAGE_SIZE,
2110 PCI_DMA_FROMDEVICE,
2111 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002112
2113 rx_agg_buf->page = NULL;
2114 __clear_bit(j, rxr->rx_agg_bmap);
2115
2116 __free_page(page);
2117 }
Michael Chan89d0a062016-04-25 02:30:51 -04002118 if (rxr->rx_page) {
2119 __free_page(rxr->rx_page);
2120 rxr->rx_page = NULL;
2121 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002122 }
2123}
2124
2125static void bnxt_free_skbs(struct bnxt *bp)
2126{
2127 bnxt_free_tx_skbs(bp);
2128 bnxt_free_rx_skbs(bp);
2129}
2130
2131static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2132{
2133 struct pci_dev *pdev = bp->pdev;
2134 int i;
2135
2136 for (i = 0; i < ring->nr_pages; i++) {
2137 if (!ring->pg_arr[i])
2138 continue;
2139
2140 dma_free_coherent(&pdev->dev, ring->page_size,
2141 ring->pg_arr[i], ring->dma_arr[i]);
2142
2143 ring->pg_arr[i] = NULL;
2144 }
2145 if (ring->pg_tbl) {
2146 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2147 ring->pg_tbl, ring->pg_tbl_map);
2148 ring->pg_tbl = NULL;
2149 }
2150 if (ring->vmem_size && *ring->vmem) {
2151 vfree(*ring->vmem);
2152 *ring->vmem = NULL;
2153 }
2154}
2155
2156static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2157{
2158 int i;
2159 struct pci_dev *pdev = bp->pdev;
2160
2161 if (ring->nr_pages > 1) {
2162 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2163 ring->nr_pages * 8,
2164 &ring->pg_tbl_map,
2165 GFP_KERNEL);
2166 if (!ring->pg_tbl)
2167 return -ENOMEM;
2168 }
2169
2170 for (i = 0; i < ring->nr_pages; i++) {
2171 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2172 ring->page_size,
2173 &ring->dma_arr[i],
2174 GFP_KERNEL);
2175 if (!ring->pg_arr[i])
2176 return -ENOMEM;
2177
2178 if (ring->nr_pages > 1)
2179 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2180 }
2181
2182 if (ring->vmem_size) {
2183 *ring->vmem = vzalloc(ring->vmem_size);
2184 if (!(*ring->vmem))
2185 return -ENOMEM;
2186 }
2187 return 0;
2188}
2189
2190static void bnxt_free_rx_rings(struct bnxt *bp)
2191{
2192 int i;
2193
Michael Chanb6ab4b02016-01-02 23:44:59 -05002194 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002195 return;
2196
2197 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002198 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002199 struct bnxt_ring_struct *ring;
2200
Michael Chanc6d30e82017-02-06 16:55:42 -05002201 if (rxr->xdp_prog)
2202 bpf_prog_put(rxr->xdp_prog);
2203
Michael Chanc0c050c2015-10-22 16:01:17 -04002204 kfree(rxr->rx_tpa);
2205 rxr->rx_tpa = NULL;
2206
2207 kfree(rxr->rx_agg_bmap);
2208 rxr->rx_agg_bmap = NULL;
2209
2210 ring = &rxr->rx_ring_struct;
2211 bnxt_free_ring(bp, ring);
2212
2213 ring = &rxr->rx_agg_ring_struct;
2214 bnxt_free_ring(bp, ring);
2215 }
2216}
2217
2218static int bnxt_alloc_rx_rings(struct bnxt *bp)
2219{
2220 int i, rc, agg_rings = 0, tpa_rings = 0;
2221
Michael Chanb6ab4b02016-01-02 23:44:59 -05002222 if (!bp->rx_ring)
2223 return -ENOMEM;
2224
Michael Chanc0c050c2015-10-22 16:01:17 -04002225 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2226 agg_rings = 1;
2227
2228 if (bp->flags & BNXT_FLAG_TPA)
2229 tpa_rings = 1;
2230
2231 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002232 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002233 struct bnxt_ring_struct *ring;
2234
Michael Chanc0c050c2015-10-22 16:01:17 -04002235 ring = &rxr->rx_ring_struct;
2236
2237 rc = bnxt_alloc_ring(bp, ring);
2238 if (rc)
2239 return rc;
2240
2241 if (agg_rings) {
2242 u16 mem_size;
2243
2244 ring = &rxr->rx_agg_ring_struct;
2245 rc = bnxt_alloc_ring(bp, ring);
2246 if (rc)
2247 return rc;
2248
2249 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2250 mem_size = rxr->rx_agg_bmap_size / 8;
2251 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2252 if (!rxr->rx_agg_bmap)
2253 return -ENOMEM;
2254
2255 if (tpa_rings) {
2256 rxr->rx_tpa = kcalloc(MAX_TPA,
2257 sizeof(struct bnxt_tpa_info),
2258 GFP_KERNEL);
2259 if (!rxr->rx_tpa)
2260 return -ENOMEM;
2261 }
2262 }
2263 }
2264 return 0;
2265}
2266
2267static void bnxt_free_tx_rings(struct bnxt *bp)
2268{
2269 int i;
2270 struct pci_dev *pdev = bp->pdev;
2271
Michael Chanb6ab4b02016-01-02 23:44:59 -05002272 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002273 return;
2274
2275 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002276 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002277 struct bnxt_ring_struct *ring;
2278
Michael Chanc0c050c2015-10-22 16:01:17 -04002279 if (txr->tx_push) {
2280 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2281 txr->tx_push, txr->tx_push_mapping);
2282 txr->tx_push = NULL;
2283 }
2284
2285 ring = &txr->tx_ring_struct;
2286
2287 bnxt_free_ring(bp, ring);
2288 }
2289}
2290
2291static int bnxt_alloc_tx_rings(struct bnxt *bp)
2292{
2293 int i, j, rc;
2294 struct pci_dev *pdev = bp->pdev;
2295
2296 bp->tx_push_size = 0;
2297 if (bp->tx_push_thresh) {
2298 int push_size;
2299
2300 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2301 bp->tx_push_thresh);
2302
Michael Chan4419dbe2016-02-10 17:33:49 -05002303 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002304 push_size = 0;
2305 bp->tx_push_thresh = 0;
2306 }
2307
2308 bp->tx_push_size = push_size;
2309 }
2310
2311 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002312 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002313 struct bnxt_ring_struct *ring;
2314
Michael Chanc0c050c2015-10-22 16:01:17 -04002315 ring = &txr->tx_ring_struct;
2316
2317 rc = bnxt_alloc_ring(bp, ring);
2318 if (rc)
2319 return rc;
2320
2321 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002322 dma_addr_t mapping;
2323
2324 /* One pre-allocated DMA buffer to backup
2325 * TX push operation
2326 */
2327 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2328 bp->tx_push_size,
2329 &txr->tx_push_mapping,
2330 GFP_KERNEL);
2331
2332 if (!txr->tx_push)
2333 return -ENOMEM;
2334
Michael Chanc0c050c2015-10-22 16:01:17 -04002335 mapping = txr->tx_push_mapping +
2336 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002337 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002338
Michael Chan4419dbe2016-02-10 17:33:49 -05002339 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002340 }
2341 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002342 if (i < bp->tx_nr_rings_xdp)
2343 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002344 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2345 j++;
2346 }
2347 return 0;
2348}
2349
2350static void bnxt_free_cp_rings(struct bnxt *bp)
2351{
2352 int i;
2353
2354 if (!bp->bnapi)
2355 return;
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_ring_struct *ring;
2361
2362 if (!bnapi)
2363 continue;
2364
2365 cpr = &bnapi->cp_ring;
2366 ring = &cpr->cp_ring_struct;
2367
2368 bnxt_free_ring(bp, ring);
2369 }
2370}
2371
2372static int bnxt_alloc_cp_rings(struct bnxt *bp)
2373{
2374 int i, rc;
2375
2376 for (i = 0; i < bp->cp_nr_rings; i++) {
2377 struct bnxt_napi *bnapi = bp->bnapi[i];
2378 struct bnxt_cp_ring_info *cpr;
2379 struct bnxt_ring_struct *ring;
2380
2381 if (!bnapi)
2382 continue;
2383
2384 cpr = &bnapi->cp_ring;
2385 ring = &cpr->cp_ring_struct;
2386
2387 rc = bnxt_alloc_ring(bp, ring);
2388 if (rc)
2389 return rc;
2390 }
2391 return 0;
2392}
2393
2394static void bnxt_init_ring_struct(struct bnxt *bp)
2395{
2396 int i;
2397
2398 for (i = 0; i < bp->cp_nr_rings; i++) {
2399 struct bnxt_napi *bnapi = bp->bnapi[i];
2400 struct bnxt_cp_ring_info *cpr;
2401 struct bnxt_rx_ring_info *rxr;
2402 struct bnxt_tx_ring_info *txr;
2403 struct bnxt_ring_struct *ring;
2404
2405 if (!bnapi)
2406 continue;
2407
2408 cpr = &bnapi->cp_ring;
2409 ring = &cpr->cp_ring_struct;
2410 ring->nr_pages = bp->cp_nr_pages;
2411 ring->page_size = HW_CMPD_RING_SIZE;
2412 ring->pg_arr = (void **)cpr->cp_desc_ring;
2413 ring->dma_arr = cpr->cp_desc_mapping;
2414 ring->vmem_size = 0;
2415
Michael Chanb6ab4b02016-01-02 23:44:59 -05002416 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002417 if (!rxr)
2418 goto skip_rx;
2419
Michael Chanc0c050c2015-10-22 16:01:17 -04002420 ring = &rxr->rx_ring_struct;
2421 ring->nr_pages = bp->rx_nr_pages;
2422 ring->page_size = HW_RXBD_RING_SIZE;
2423 ring->pg_arr = (void **)rxr->rx_desc_ring;
2424 ring->dma_arr = rxr->rx_desc_mapping;
2425 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2426 ring->vmem = (void **)&rxr->rx_buf_ring;
2427
2428 ring = &rxr->rx_agg_ring_struct;
2429 ring->nr_pages = bp->rx_agg_nr_pages;
2430 ring->page_size = HW_RXBD_RING_SIZE;
2431 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2432 ring->dma_arr = rxr->rx_agg_desc_mapping;
2433 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2434 ring->vmem = (void **)&rxr->rx_agg_ring;
2435
Michael Chan3b2b7d92016-01-02 23:45:00 -05002436skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002437 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002438 if (!txr)
2439 continue;
2440
Michael Chanc0c050c2015-10-22 16:01:17 -04002441 ring = &txr->tx_ring_struct;
2442 ring->nr_pages = bp->tx_nr_pages;
2443 ring->page_size = HW_RXBD_RING_SIZE;
2444 ring->pg_arr = (void **)txr->tx_desc_ring;
2445 ring->dma_arr = txr->tx_desc_mapping;
2446 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2447 ring->vmem = (void **)&txr->tx_buf_ring;
2448 }
2449}
2450
2451static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2452{
2453 int i;
2454 u32 prod;
2455 struct rx_bd **rx_buf_ring;
2456
2457 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2458 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2459 int j;
2460 struct rx_bd *rxbd;
2461
2462 rxbd = rx_buf_ring[i];
2463 if (!rxbd)
2464 continue;
2465
2466 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2467 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2468 rxbd->rx_bd_opaque = prod;
2469 }
2470 }
2471}
2472
2473static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2474{
2475 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002476 struct bnxt_rx_ring_info *rxr;
2477 struct bnxt_ring_struct *ring;
2478 u32 prod, type;
2479 int i;
2480
Michael Chanc0c050c2015-10-22 16:01:17 -04002481 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2482 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2483
2484 if (NET_IP_ALIGN == 2)
2485 type |= RX_BD_FLAGS_SOP;
2486
Michael Chanb6ab4b02016-01-02 23:44:59 -05002487 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002488 ring = &rxr->rx_ring_struct;
2489 bnxt_init_rxbd_pages(ring, type);
2490
Michael Chanc6d30e82017-02-06 16:55:42 -05002491 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2492 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2493 if (IS_ERR(rxr->xdp_prog)) {
2494 int rc = PTR_ERR(rxr->xdp_prog);
2495
2496 rxr->xdp_prog = NULL;
2497 return rc;
2498 }
2499 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002500 prod = rxr->rx_prod;
2501 for (i = 0; i < bp->rx_ring_size; i++) {
2502 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2503 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2504 ring_nr, i, bp->rx_ring_size);
2505 break;
2506 }
2507 prod = NEXT_RX(prod);
2508 }
2509 rxr->rx_prod = prod;
2510 ring->fw_ring_id = INVALID_HW_RING_ID;
2511
Michael Chanedd0c2c2015-12-27 18:19:19 -05002512 ring = &rxr->rx_agg_ring_struct;
2513 ring->fw_ring_id = INVALID_HW_RING_ID;
2514
Michael Chanc0c050c2015-10-22 16:01:17 -04002515 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2516 return 0;
2517
Michael Chan2839f282016-04-25 02:30:50 -04002518 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002519 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2520
2521 bnxt_init_rxbd_pages(ring, type);
2522
2523 prod = rxr->rx_agg_prod;
2524 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2525 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2526 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2527 ring_nr, i, bp->rx_ring_size);
2528 break;
2529 }
2530 prod = NEXT_RX_AGG(prod);
2531 }
2532 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002533
2534 if (bp->flags & BNXT_FLAG_TPA) {
2535 if (rxr->rx_tpa) {
2536 u8 *data;
2537 dma_addr_t mapping;
2538
2539 for (i = 0; i < MAX_TPA; i++) {
2540 data = __bnxt_alloc_rx_data(bp, &mapping,
2541 GFP_KERNEL);
2542 if (!data)
2543 return -ENOMEM;
2544
2545 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002546 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002547 rxr->rx_tpa[i].mapping = mapping;
2548 }
2549 } else {
2550 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2551 return -ENOMEM;
2552 }
2553 }
2554
2555 return 0;
2556}
2557
Sankar Patchineelam22479252017-03-28 19:47:29 -04002558static void bnxt_init_cp_rings(struct bnxt *bp)
2559{
2560 int i;
2561
2562 for (i = 0; i < bp->cp_nr_rings; i++) {
2563 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2564 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2565
2566 ring->fw_ring_id = INVALID_HW_RING_ID;
2567 }
2568}
2569
Michael Chanc0c050c2015-10-22 16:01:17 -04002570static int bnxt_init_rx_rings(struct bnxt *bp)
2571{
2572 int i, rc = 0;
2573
Michael Chanc61fb992017-02-06 16:55:36 -05002574 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002575 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2576 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002577 } else {
2578 bp->rx_offset = BNXT_RX_OFFSET;
2579 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2580 }
Michael Chanb3dba772017-02-06 16:55:35 -05002581
Michael Chanc0c050c2015-10-22 16:01:17 -04002582 for (i = 0; i < bp->rx_nr_rings; i++) {
2583 rc = bnxt_init_one_rx_ring(bp, i);
2584 if (rc)
2585 break;
2586 }
2587
2588 return rc;
2589}
2590
2591static int bnxt_init_tx_rings(struct bnxt *bp)
2592{
2593 u16 i;
2594
2595 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2596 MAX_SKB_FRAGS + 1);
2597
2598 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002599 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002600 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2601
2602 ring->fw_ring_id = INVALID_HW_RING_ID;
2603 }
2604
2605 return 0;
2606}
2607
2608static void bnxt_free_ring_grps(struct bnxt *bp)
2609{
2610 kfree(bp->grp_info);
2611 bp->grp_info = NULL;
2612}
2613
2614static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2615{
2616 int i;
2617
2618 if (irq_re_init) {
2619 bp->grp_info = kcalloc(bp->cp_nr_rings,
2620 sizeof(struct bnxt_ring_grp_info),
2621 GFP_KERNEL);
2622 if (!bp->grp_info)
2623 return -ENOMEM;
2624 }
2625 for (i = 0; i < bp->cp_nr_rings; i++) {
2626 if (irq_re_init)
2627 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2628 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2629 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2630 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2631 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2632 }
2633 return 0;
2634}
2635
2636static void bnxt_free_vnics(struct bnxt *bp)
2637{
2638 kfree(bp->vnic_info);
2639 bp->vnic_info = NULL;
2640 bp->nr_vnics = 0;
2641}
2642
2643static int bnxt_alloc_vnics(struct bnxt *bp)
2644{
2645 int num_vnics = 1;
2646
2647#ifdef CONFIG_RFS_ACCEL
2648 if (bp->flags & BNXT_FLAG_RFS)
2649 num_vnics += bp->rx_nr_rings;
2650#endif
2651
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002652 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2653 num_vnics++;
2654
Michael Chanc0c050c2015-10-22 16:01:17 -04002655 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2656 GFP_KERNEL);
2657 if (!bp->vnic_info)
2658 return -ENOMEM;
2659
2660 bp->nr_vnics = num_vnics;
2661 return 0;
2662}
2663
2664static void bnxt_init_vnics(struct bnxt *bp)
2665{
2666 int i;
2667
2668 for (i = 0; i < bp->nr_vnics; i++) {
2669 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2670
2671 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002672 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2673 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002674 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2675
2676 if (bp->vnic_info[i].rss_hash_key) {
2677 if (i == 0)
2678 prandom_bytes(vnic->rss_hash_key,
2679 HW_HASH_KEY_SIZE);
2680 else
2681 memcpy(vnic->rss_hash_key,
2682 bp->vnic_info[0].rss_hash_key,
2683 HW_HASH_KEY_SIZE);
2684 }
2685 }
2686}
2687
2688static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2689{
2690 int pages;
2691
2692 pages = ring_size / desc_per_pg;
2693
2694 if (!pages)
2695 return 1;
2696
2697 pages++;
2698
2699 while (pages & (pages - 1))
2700 pages++;
2701
2702 return pages;
2703}
2704
Michael Chanc6d30e82017-02-06 16:55:42 -05002705void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002706{
2707 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002708 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2709 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002710 if (bp->dev->features & NETIF_F_LRO)
2711 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002712 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002713 bp->flags |= BNXT_FLAG_GRO;
2714}
2715
2716/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2717 * be set on entry.
2718 */
2719void bnxt_set_ring_params(struct bnxt *bp)
2720{
2721 u32 ring_size, rx_size, rx_space;
2722 u32 agg_factor = 0, agg_ring_size = 0;
2723
2724 /* 8 for CRC and VLAN */
2725 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2726
2727 rx_space = rx_size + NET_SKB_PAD +
2728 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2729
2730 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2731 ring_size = bp->rx_ring_size;
2732 bp->rx_agg_ring_size = 0;
2733 bp->rx_agg_nr_pages = 0;
2734
2735 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002736 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002737
2738 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002739 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002740 u32 jumbo_factor;
2741
2742 bp->flags |= BNXT_FLAG_JUMBO;
2743 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2744 if (jumbo_factor > agg_factor)
2745 agg_factor = jumbo_factor;
2746 }
2747 agg_ring_size = ring_size * agg_factor;
2748
2749 if (agg_ring_size) {
2750 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2751 RX_DESC_CNT);
2752 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2753 u32 tmp = agg_ring_size;
2754
2755 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2756 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2757 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2758 tmp, agg_ring_size);
2759 }
2760 bp->rx_agg_ring_size = agg_ring_size;
2761 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2762 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2763 rx_space = rx_size + NET_SKB_PAD +
2764 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2765 }
2766
2767 bp->rx_buf_use_size = rx_size;
2768 bp->rx_buf_size = rx_space;
2769
2770 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2771 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2772
2773 ring_size = bp->tx_ring_size;
2774 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2775 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2776
2777 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2778 bp->cp_ring_size = ring_size;
2779
2780 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2781 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2782 bp->cp_nr_pages = MAX_CP_PAGES;
2783 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2784 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2785 ring_size, bp->cp_ring_size);
2786 }
2787 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2788 bp->cp_ring_mask = bp->cp_bit - 1;
2789}
2790
Michael Chanc61fb992017-02-06 16:55:36 -05002791int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002792{
Michael Chanc61fb992017-02-06 16:55:36 -05002793 if (page_mode) {
2794 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2795 return -EOPNOTSUPP;
2796 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2797 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2798 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2799 bp->dev->hw_features &= ~NETIF_F_LRO;
2800 bp->dev->features &= ~NETIF_F_LRO;
2801 bp->rx_dir = DMA_BIDIRECTIONAL;
2802 bp->rx_skb_func = bnxt_rx_page_skb;
2803 } else {
2804 bp->dev->max_mtu = BNXT_MAX_MTU;
2805 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2806 bp->rx_dir = DMA_FROM_DEVICE;
2807 bp->rx_skb_func = bnxt_rx_skb;
2808 }
Michael Chan6bb19472017-02-06 16:55:32 -05002809 return 0;
2810}
2811
Michael Chanc0c050c2015-10-22 16:01:17 -04002812static void bnxt_free_vnic_attributes(struct bnxt *bp)
2813{
2814 int i;
2815 struct bnxt_vnic_info *vnic;
2816 struct pci_dev *pdev = bp->pdev;
2817
2818 if (!bp->vnic_info)
2819 return;
2820
2821 for (i = 0; i < bp->nr_vnics; i++) {
2822 vnic = &bp->vnic_info[i];
2823
2824 kfree(vnic->fw_grp_ids);
2825 vnic->fw_grp_ids = NULL;
2826
2827 kfree(vnic->uc_list);
2828 vnic->uc_list = NULL;
2829
2830 if (vnic->mc_list) {
2831 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2832 vnic->mc_list, vnic->mc_list_mapping);
2833 vnic->mc_list = NULL;
2834 }
2835
2836 if (vnic->rss_table) {
2837 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2838 vnic->rss_table,
2839 vnic->rss_table_dma_addr);
2840 vnic->rss_table = NULL;
2841 }
2842
2843 vnic->rss_hash_key = NULL;
2844 vnic->flags = 0;
2845 }
2846}
2847
2848static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2849{
2850 int i, rc = 0, size;
2851 struct bnxt_vnic_info *vnic;
2852 struct pci_dev *pdev = bp->pdev;
2853 int max_rings;
2854
2855 for (i = 0; i < bp->nr_vnics; i++) {
2856 vnic = &bp->vnic_info[i];
2857
2858 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2859 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2860
2861 if (mem_size > 0) {
2862 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2863 if (!vnic->uc_list) {
2864 rc = -ENOMEM;
2865 goto out;
2866 }
2867 }
2868 }
2869
2870 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2871 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2872 vnic->mc_list =
2873 dma_alloc_coherent(&pdev->dev,
2874 vnic->mc_list_size,
2875 &vnic->mc_list_mapping,
2876 GFP_KERNEL);
2877 if (!vnic->mc_list) {
2878 rc = -ENOMEM;
2879 goto out;
2880 }
2881 }
2882
2883 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2884 max_rings = bp->rx_nr_rings;
2885 else
2886 max_rings = 1;
2887
2888 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2889 if (!vnic->fw_grp_ids) {
2890 rc = -ENOMEM;
2891 goto out;
2892 }
2893
Michael Chanae10ae72016-12-29 12:13:38 -05002894 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2895 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2896 continue;
2897
Michael Chanc0c050c2015-10-22 16:01:17 -04002898 /* Allocate rss table and hash key */
2899 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2900 &vnic->rss_table_dma_addr,
2901 GFP_KERNEL);
2902 if (!vnic->rss_table) {
2903 rc = -ENOMEM;
2904 goto out;
2905 }
2906
2907 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2908
2909 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2910 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2911 }
2912 return 0;
2913
2914out:
2915 return rc;
2916}
2917
2918static void bnxt_free_hwrm_resources(struct bnxt *bp)
2919{
2920 struct pci_dev *pdev = bp->pdev;
2921
2922 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2923 bp->hwrm_cmd_resp_dma_addr);
2924
2925 bp->hwrm_cmd_resp_addr = NULL;
2926 if (bp->hwrm_dbg_resp_addr) {
2927 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2928 bp->hwrm_dbg_resp_addr,
2929 bp->hwrm_dbg_resp_dma_addr);
2930
2931 bp->hwrm_dbg_resp_addr = NULL;
2932 }
2933}
2934
2935static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2936{
2937 struct pci_dev *pdev = bp->pdev;
2938
2939 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2940 &bp->hwrm_cmd_resp_dma_addr,
2941 GFP_KERNEL);
2942 if (!bp->hwrm_cmd_resp_addr)
2943 return -ENOMEM;
2944 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2945 HWRM_DBG_REG_BUF_SIZE,
2946 &bp->hwrm_dbg_resp_dma_addr,
2947 GFP_KERNEL);
2948 if (!bp->hwrm_dbg_resp_addr)
2949 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2950
2951 return 0;
2952}
2953
Deepak Khungare605db82017-05-29 19:06:04 -04002954static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2955{
2956 if (bp->hwrm_short_cmd_req_addr) {
2957 struct pci_dev *pdev = bp->pdev;
2958
2959 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2960 bp->hwrm_short_cmd_req_addr,
2961 bp->hwrm_short_cmd_req_dma_addr);
2962 bp->hwrm_short_cmd_req_addr = NULL;
2963 }
2964}
2965
2966static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2967{
2968 struct pci_dev *pdev = bp->pdev;
2969
2970 bp->hwrm_short_cmd_req_addr =
2971 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2972 &bp->hwrm_short_cmd_req_dma_addr,
2973 GFP_KERNEL);
2974 if (!bp->hwrm_short_cmd_req_addr)
2975 return -ENOMEM;
2976
2977 return 0;
2978}
2979
Michael Chanc0c050c2015-10-22 16:01:17 -04002980static void bnxt_free_stats(struct bnxt *bp)
2981{
2982 u32 size, i;
2983 struct pci_dev *pdev = bp->pdev;
2984
Michael Chan3bdf56c2016-03-07 15:38:45 -05002985 if (bp->hw_rx_port_stats) {
2986 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2987 bp->hw_rx_port_stats,
2988 bp->hw_rx_port_stats_map);
2989 bp->hw_rx_port_stats = NULL;
2990 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2991 }
2992
Michael Chanc0c050c2015-10-22 16:01:17 -04002993 if (!bp->bnapi)
2994 return;
2995
2996 size = sizeof(struct ctx_hw_stats);
2997
2998 for (i = 0; i < bp->cp_nr_rings; i++) {
2999 struct bnxt_napi *bnapi = bp->bnapi[i];
3000 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3001
3002 if (cpr->hw_stats) {
3003 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3004 cpr->hw_stats_map);
3005 cpr->hw_stats = NULL;
3006 }
3007 }
3008}
3009
3010static int bnxt_alloc_stats(struct bnxt *bp)
3011{
3012 u32 size, i;
3013 struct pci_dev *pdev = bp->pdev;
3014
3015 size = sizeof(struct ctx_hw_stats);
3016
3017 for (i = 0; i < bp->cp_nr_rings; i++) {
3018 struct bnxt_napi *bnapi = bp->bnapi[i];
3019 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3020
3021 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3022 &cpr->hw_stats_map,
3023 GFP_KERNEL);
3024 if (!cpr->hw_stats)
3025 return -ENOMEM;
3026
3027 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3028 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003029
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003030 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05003031 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3032 sizeof(struct tx_port_stats) + 1024;
3033
3034 bp->hw_rx_port_stats =
3035 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3036 &bp->hw_rx_port_stats_map,
3037 GFP_KERNEL);
3038 if (!bp->hw_rx_port_stats)
3039 return -ENOMEM;
3040
3041 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3042 512;
3043 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3044 sizeof(struct rx_port_stats) + 512;
3045 bp->flags |= BNXT_FLAG_PORT_STATS;
3046 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003047 return 0;
3048}
3049
3050static void bnxt_clear_ring_indices(struct bnxt *bp)
3051{
3052 int i;
3053
3054 if (!bp->bnapi)
3055 return;
3056
3057 for (i = 0; i < bp->cp_nr_rings; i++) {
3058 struct bnxt_napi *bnapi = bp->bnapi[i];
3059 struct bnxt_cp_ring_info *cpr;
3060 struct bnxt_rx_ring_info *rxr;
3061 struct bnxt_tx_ring_info *txr;
3062
3063 if (!bnapi)
3064 continue;
3065
3066 cpr = &bnapi->cp_ring;
3067 cpr->cp_raw_cons = 0;
3068
Michael Chanb6ab4b02016-01-02 23:44:59 -05003069 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003070 if (txr) {
3071 txr->tx_prod = 0;
3072 txr->tx_cons = 0;
3073 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003074
Michael Chanb6ab4b02016-01-02 23:44:59 -05003075 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003076 if (rxr) {
3077 rxr->rx_prod = 0;
3078 rxr->rx_agg_prod = 0;
3079 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003080 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003081 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003082 }
3083}
3084
3085static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3086{
3087#ifdef CONFIG_RFS_ACCEL
3088 int i;
3089
3090 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3091 * safe to delete the hash table.
3092 */
3093 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3094 struct hlist_head *head;
3095 struct hlist_node *tmp;
3096 struct bnxt_ntuple_filter *fltr;
3097
3098 head = &bp->ntp_fltr_hash_tbl[i];
3099 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3100 hlist_del(&fltr->hash);
3101 kfree(fltr);
3102 }
3103 }
3104 if (irq_reinit) {
3105 kfree(bp->ntp_fltr_bmap);
3106 bp->ntp_fltr_bmap = NULL;
3107 }
3108 bp->ntp_fltr_count = 0;
3109#endif
3110}
3111
3112static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3113{
3114#ifdef CONFIG_RFS_ACCEL
3115 int i, rc = 0;
3116
3117 if (!(bp->flags & BNXT_FLAG_RFS))
3118 return 0;
3119
3120 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3121 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3122
3123 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003124 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3125 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003126 GFP_KERNEL);
3127
3128 if (!bp->ntp_fltr_bmap)
3129 rc = -ENOMEM;
3130
3131 return rc;
3132#else
3133 return 0;
3134#endif
3135}
3136
3137static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3138{
3139 bnxt_free_vnic_attributes(bp);
3140 bnxt_free_tx_rings(bp);
3141 bnxt_free_rx_rings(bp);
3142 bnxt_free_cp_rings(bp);
3143 bnxt_free_ntp_fltrs(bp, irq_re_init);
3144 if (irq_re_init) {
3145 bnxt_free_stats(bp);
3146 bnxt_free_ring_grps(bp);
3147 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003148 kfree(bp->tx_ring_map);
3149 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003150 kfree(bp->tx_ring);
3151 bp->tx_ring = NULL;
3152 kfree(bp->rx_ring);
3153 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003154 kfree(bp->bnapi);
3155 bp->bnapi = NULL;
3156 } else {
3157 bnxt_clear_ring_indices(bp);
3158 }
3159}
3160
3161static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3162{
Michael Chan01657bc2016-01-02 23:45:03 -05003163 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003164 void *bnapi;
3165
3166 if (irq_re_init) {
3167 /* Allocate bnapi mem pointer array and mem block for
3168 * all queues
3169 */
3170 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3171 bp->cp_nr_rings);
3172 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3173 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3174 if (!bnapi)
3175 return -ENOMEM;
3176
3177 bp->bnapi = bnapi;
3178 bnapi += arr_size;
3179 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3180 bp->bnapi[i] = bnapi;
3181 bp->bnapi[i]->index = i;
3182 bp->bnapi[i]->bp = bp;
3183 }
3184
Michael Chanb6ab4b02016-01-02 23:44:59 -05003185 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3186 sizeof(struct bnxt_rx_ring_info),
3187 GFP_KERNEL);
3188 if (!bp->rx_ring)
3189 return -ENOMEM;
3190
3191 for (i = 0; i < bp->rx_nr_rings; i++) {
3192 bp->rx_ring[i].bnapi = bp->bnapi[i];
3193 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3194 }
3195
3196 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3197 sizeof(struct bnxt_tx_ring_info),
3198 GFP_KERNEL);
3199 if (!bp->tx_ring)
3200 return -ENOMEM;
3201
Michael Chana960dec2017-02-06 16:55:39 -05003202 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3203 GFP_KERNEL);
3204
3205 if (!bp->tx_ring_map)
3206 return -ENOMEM;
3207
Michael Chan01657bc2016-01-02 23:45:03 -05003208 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3209 j = 0;
3210 else
3211 j = bp->rx_nr_rings;
3212
3213 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3214 bp->tx_ring[i].bnapi = bp->bnapi[j];
3215 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003216 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003217 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003218 bp->tx_ring[i].txq_index = i -
3219 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003220 bp->bnapi[j]->tx_int = bnxt_tx_int;
3221 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003222 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003223 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3224 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003225 }
3226
Michael Chanc0c050c2015-10-22 16:01:17 -04003227 rc = bnxt_alloc_stats(bp);
3228 if (rc)
3229 goto alloc_mem_err;
3230
3231 rc = bnxt_alloc_ntp_fltrs(bp);
3232 if (rc)
3233 goto alloc_mem_err;
3234
3235 rc = bnxt_alloc_vnics(bp);
3236 if (rc)
3237 goto alloc_mem_err;
3238 }
3239
3240 bnxt_init_ring_struct(bp);
3241
3242 rc = bnxt_alloc_rx_rings(bp);
3243 if (rc)
3244 goto alloc_mem_err;
3245
3246 rc = bnxt_alloc_tx_rings(bp);
3247 if (rc)
3248 goto alloc_mem_err;
3249
3250 rc = bnxt_alloc_cp_rings(bp);
3251 if (rc)
3252 goto alloc_mem_err;
3253
3254 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3255 BNXT_VNIC_UCAST_FLAG;
3256 rc = bnxt_alloc_vnic_attributes(bp);
3257 if (rc)
3258 goto alloc_mem_err;
3259 return 0;
3260
3261alloc_mem_err:
3262 bnxt_free_mem(bp, true);
3263 return rc;
3264}
3265
Michael Chan9d8bc092016-12-29 12:13:33 -05003266static void bnxt_disable_int(struct bnxt *bp)
3267{
3268 int i;
3269
3270 if (!bp->bnapi)
3271 return;
3272
3273 for (i = 0; i < bp->cp_nr_rings; i++) {
3274 struct bnxt_napi *bnapi = bp->bnapi[i];
3275 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003276 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003277
Michael Chandaf1f1e2017-02-20 19:25:17 -05003278 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3279 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003280 }
3281}
3282
3283static void bnxt_disable_int_sync(struct bnxt *bp)
3284{
3285 int i;
3286
3287 atomic_inc(&bp->intr_sem);
3288
3289 bnxt_disable_int(bp);
3290 for (i = 0; i < bp->cp_nr_rings; i++)
3291 synchronize_irq(bp->irq_tbl[i].vector);
3292}
3293
3294static void bnxt_enable_int(struct bnxt *bp)
3295{
3296 int i;
3297
3298 atomic_set(&bp->intr_sem, 0);
3299 for (i = 0; i < bp->cp_nr_rings; i++) {
3300 struct bnxt_napi *bnapi = bp->bnapi[i];
3301 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3302
3303 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3304 }
3305}
3306
Michael Chanc0c050c2015-10-22 16:01:17 -04003307void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3308 u16 cmpl_ring, u16 target_id)
3309{
Michael Chana8643e12016-02-26 04:00:05 -05003310 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003311
Michael Chana8643e12016-02-26 04:00:05 -05003312 req->req_type = cpu_to_le16(req_type);
3313 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3314 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003315 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3316}
3317
Michael Chanfbfbc482016-02-26 04:00:07 -05003318static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3319 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003320{
Michael Chana11fa2b2016-05-15 03:04:47 -04003321 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003322 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003323 u32 *data = msg;
3324 __le32 *resp_len, *valid;
3325 u16 cp_ring_id, len = 0;
3326 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003327 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003328
Michael Chana8643e12016-02-26 04:00:05 -05003329 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003330 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003331 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003332 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3333
Deepak Khungare605db82017-05-29 19:06:04 -04003334 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3335 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3336 struct hwrm_short_input short_input = {0};
3337
3338 memcpy(short_cmd_req, req, msg_len);
3339 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3340 msg_len);
3341
3342 short_input.req_type = req->req_type;
3343 short_input.signature =
3344 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3345 short_input.size = cpu_to_le16(msg_len);
3346 short_input.req_addr =
3347 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3348
3349 data = (u32 *)&short_input;
3350 msg_len = sizeof(short_input);
3351
3352 /* Sync memory write before updating doorbell */
3353 wmb();
3354
3355 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3356 }
3357
Michael Chanc0c050c2015-10-22 16:01:17 -04003358 /* Write request msg to hwrm channel */
3359 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3360
Deepak Khungare605db82017-05-29 19:06:04 -04003361 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003362 writel(0, bp->bar0 + i);
3363
Michael Chanc0c050c2015-10-22 16:01:17 -04003364 /* currently supports only one outstanding message */
3365 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003366 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003367
3368 /* Ring channel doorbell */
3369 writel(1, bp->bar0 + 0x100);
3370
Michael Chanff4fe812016-02-26 04:00:04 -05003371 if (!timeout)
3372 timeout = DFLT_HWRM_CMD_TIMEOUT;
3373
Michael Chanc0c050c2015-10-22 16:01:17 -04003374 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003375 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003376 if (intr_process) {
3377 /* Wait until hwrm response cmpl interrupt is processed */
3378 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003379 i++ < tmo_count) {
3380 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003381 }
3382
3383 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3384 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003385 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003386 return -1;
3387 }
3388 } else {
3389 /* Check if response len is updated */
3390 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003391 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003392 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3393 HWRM_RESP_LEN_SFT;
3394 if (len)
3395 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003396 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003397 }
3398
Michael Chana11fa2b2016-05-15 03:04:47 -04003399 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003400 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003401 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003402 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003403 return -1;
3404 }
3405
3406 /* Last word of resp contains valid bit */
3407 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003408 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003409 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3410 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003411 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003412 }
3413
Michael Chana11fa2b2016-05-15 03:04:47 -04003414 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003415 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003416 timeout, le16_to_cpu(req->req_type),
3417 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003418 return -1;
3419 }
3420 }
3421
3422 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003423 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003424 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3425 le16_to_cpu(resp->req_type),
3426 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003427 return rc;
3428}
3429
3430int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3431{
3432 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003433}
3434
3435int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3436{
3437 int rc;
3438
3439 mutex_lock(&bp->hwrm_cmd_lock);
3440 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3441 mutex_unlock(&bp->hwrm_cmd_lock);
3442 return rc;
3443}
3444
Michael Chan90e209212016-02-26 04:00:08 -05003445int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3446 int timeout)
3447{
3448 int rc;
3449
3450 mutex_lock(&bp->hwrm_cmd_lock);
3451 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3452 mutex_unlock(&bp->hwrm_cmd_lock);
3453 return rc;
3454}
3455
Michael Chana1653b12016-12-07 00:26:20 -05003456int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3457 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003458{
3459 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003460 DECLARE_BITMAP(async_events_bmap, 256);
3461 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003462 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003463
3464 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3465
3466 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003467 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003468
Michael Chan25be8622016-04-05 14:09:00 -04003469 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3470 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3471 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3472
Michael Chana1653b12016-12-07 00:26:20 -05003473 if (bmap && bmap_size) {
3474 for (i = 0; i < bmap_size; i++) {
3475 if (test_bit(i, bmap))
3476 __set_bit(i, async_events_bmap);
3477 }
3478 }
3479
Michael Chan25be8622016-04-05 14:09:00 -04003480 for (i = 0; i < 8; i++)
3481 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3482
Michael Chana1653b12016-12-07 00:26:20 -05003483 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3484}
3485
3486static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3487{
3488 struct hwrm_func_drv_rgtr_input req = {0};
3489
3490 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3491
3492 req.enables =
3493 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3494 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3495
Michael Chan11f15ed2016-04-05 14:08:55 -04003496 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003497 req.ver_maj = DRV_VER_MAJ;
3498 req.ver_min = DRV_VER_MIN;
3499 req.ver_upd = DRV_VER_UPD;
3500
3501 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003502 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003503 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003504
Michael Chan9b0436c2017-07-11 13:05:36 -04003505 memset(data, 0, sizeof(data));
3506 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3507 u16 cmd = bnxt_vf_req_snif[i];
3508 unsigned int bit, idx;
3509
3510 idx = cmd / 32;
3511 bit = cmd % 32;
3512 data[idx] |= 1 << bit;
3513 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003514
Michael Chande68f5de2015-12-09 19:35:41 -05003515 for (i = 0; i < 8; i++)
3516 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3517
Michael Chanc0c050c2015-10-22 16:01:17 -04003518 req.enables |=
3519 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3520 }
3521
3522 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3523}
3524
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003525static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3526{
3527 struct hwrm_func_drv_unrgtr_input req = {0};
3528
3529 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3530 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3531}
3532
Michael Chanc0c050c2015-10-22 16:01:17 -04003533static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3534{
3535 u32 rc = 0;
3536 struct hwrm_tunnel_dst_port_free_input req = {0};
3537
3538 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3539 req.tunnel_type = tunnel_type;
3540
3541 switch (tunnel_type) {
3542 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3543 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3544 break;
3545 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3546 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3547 break;
3548 default:
3549 break;
3550 }
3551
3552 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3553 if (rc)
3554 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3555 rc);
3556 return rc;
3557}
3558
3559static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3560 u8 tunnel_type)
3561{
3562 u32 rc = 0;
3563 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3564 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3565
3566 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3567
3568 req.tunnel_type = tunnel_type;
3569 req.tunnel_dst_port_val = port;
3570
3571 mutex_lock(&bp->hwrm_cmd_lock);
3572 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3573 if (rc) {
3574 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3575 rc);
3576 goto err_out;
3577 }
3578
Christophe Jaillet57aac712016-11-22 06:14:40 +01003579 switch (tunnel_type) {
3580 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003581 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003582 break;
3583 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003584 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003585 break;
3586 default:
3587 break;
3588 }
3589
Michael Chanc0c050c2015-10-22 16:01:17 -04003590err_out:
3591 mutex_unlock(&bp->hwrm_cmd_lock);
3592 return rc;
3593}
3594
3595static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3596{
3597 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3598 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3599
3600 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003601 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003602
3603 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3604 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3605 req.mask = cpu_to_le32(vnic->rx_mask);
3606 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3607}
3608
3609#ifdef CONFIG_RFS_ACCEL
3610static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3611 struct bnxt_ntuple_filter *fltr)
3612{
3613 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3614
3615 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3616 req.ntuple_filter_id = fltr->filter_id;
3617 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3618}
3619
3620#define BNXT_NTP_FLTR_FLAGS \
3621 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3622 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3623 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3624 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3625 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3626 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3627 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3628 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3629 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3630 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3631 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3632 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3633 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003634 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003635
Michael Chan61aad722017-02-12 19:18:14 -05003636#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3637 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3638
Michael Chanc0c050c2015-10-22 16:01:17 -04003639static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3640 struct bnxt_ntuple_filter *fltr)
3641{
3642 int rc = 0;
3643 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3644 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3645 bp->hwrm_cmd_resp_addr;
3646 struct flow_keys *keys = &fltr->fkeys;
3647 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3648
3649 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003650 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003651
3652 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3653
3654 req.ethertype = htons(ETH_P_IP);
3655 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003656 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003657 req.ip_protocol = keys->basic.ip_proto;
3658
Michael Chandda0e742016-12-29 12:13:40 -05003659 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3660 int i;
3661
3662 req.ethertype = htons(ETH_P_IPV6);
3663 req.ip_addr_type =
3664 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3665 *(struct in6_addr *)&req.src_ipaddr[0] =
3666 keys->addrs.v6addrs.src;
3667 *(struct in6_addr *)&req.dst_ipaddr[0] =
3668 keys->addrs.v6addrs.dst;
3669 for (i = 0; i < 4; i++) {
3670 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3671 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3672 }
3673 } else {
3674 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3675 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3676 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3677 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3678 }
Michael Chan61aad722017-02-12 19:18:14 -05003679 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3680 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3681 req.tunnel_type =
3682 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3683 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003684
3685 req.src_port = keys->ports.src;
3686 req.src_port_mask = cpu_to_be16(0xffff);
3687 req.dst_port = keys->ports.dst;
3688 req.dst_port_mask = cpu_to_be16(0xffff);
3689
Michael Chanc1935542015-12-27 18:19:28 -05003690 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003691 mutex_lock(&bp->hwrm_cmd_lock);
3692 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3693 if (!rc)
3694 fltr->filter_id = resp->ntuple_filter_id;
3695 mutex_unlock(&bp->hwrm_cmd_lock);
3696 return rc;
3697}
3698#endif
3699
3700static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3701 u8 *mac_addr)
3702{
3703 u32 rc = 0;
3704 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3705 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3706
3707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003708 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3709 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3710 req.flags |=
3711 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003712 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003713 req.enables =
3714 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003715 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003716 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3717 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3718 req.l2_addr_mask[0] = 0xff;
3719 req.l2_addr_mask[1] = 0xff;
3720 req.l2_addr_mask[2] = 0xff;
3721 req.l2_addr_mask[3] = 0xff;
3722 req.l2_addr_mask[4] = 0xff;
3723 req.l2_addr_mask[5] = 0xff;
3724
3725 mutex_lock(&bp->hwrm_cmd_lock);
3726 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3727 if (!rc)
3728 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3729 resp->l2_filter_id;
3730 mutex_unlock(&bp->hwrm_cmd_lock);
3731 return rc;
3732}
3733
3734static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3735{
3736 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3737 int rc = 0;
3738
3739 /* Any associated ntuple filters will also be cleared by firmware. */
3740 mutex_lock(&bp->hwrm_cmd_lock);
3741 for (i = 0; i < num_of_vnics; i++) {
3742 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3743
3744 for (j = 0; j < vnic->uc_filter_count; j++) {
3745 struct hwrm_cfa_l2_filter_free_input req = {0};
3746
3747 bnxt_hwrm_cmd_hdr_init(bp, &req,
3748 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3749
3750 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3751
3752 rc = _hwrm_send_message(bp, &req, sizeof(req),
3753 HWRM_CMD_TIMEOUT);
3754 }
3755 vnic->uc_filter_count = 0;
3756 }
3757 mutex_unlock(&bp->hwrm_cmd_lock);
3758
3759 return rc;
3760}
3761
3762static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3763{
3764 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3765 struct hwrm_vnic_tpa_cfg_input req = {0};
3766
3767 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3768
3769 if (tpa_flags) {
3770 u16 mss = bp->dev->mtu - 40;
3771 u32 nsegs, n, segs = 0, flags;
3772
3773 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3774 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3775 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3776 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3777 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3778 if (tpa_flags & BNXT_FLAG_GRO)
3779 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3780
3781 req.flags = cpu_to_le32(flags);
3782
3783 req.enables =
3784 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003785 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3786 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003787
3788 /* Number of segs are log2 units, and first packet is not
3789 * included as part of this units.
3790 */
Michael Chan2839f282016-04-25 02:30:50 -04003791 if (mss <= BNXT_RX_PAGE_SIZE) {
3792 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003793 nsegs = (MAX_SKB_FRAGS - 1) * n;
3794 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003795 n = mss / BNXT_RX_PAGE_SIZE;
3796 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003797 n++;
3798 nsegs = (MAX_SKB_FRAGS - n) / n;
3799 }
3800
3801 segs = ilog2(nsegs);
3802 req.max_agg_segs = cpu_to_le16(segs);
3803 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003804
3805 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003806 }
3807 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3808
3809 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3810}
3811
3812static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3813{
3814 u32 i, j, max_rings;
3815 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3816 struct hwrm_vnic_rss_cfg_input req = {0};
3817
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003818 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003819 return 0;
3820
3821 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3822 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003823 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003824 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3825 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3826 max_rings = bp->rx_nr_rings - 1;
3827 else
3828 max_rings = bp->rx_nr_rings;
3829 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003830 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003831 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003832
3833 /* Fill the RSS indirection table with ring group ids */
3834 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3835 if (j == max_rings)
3836 j = 0;
3837 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3838 }
3839
3840 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3841 req.hash_key_tbl_addr =
3842 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3843 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003844 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003845 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3846}
3847
3848static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3849{
3850 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3851 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3852
3853 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3854 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3855 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3856 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3857 req.enables =
3858 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3859 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3860 /* thresholds not implemented in firmware yet */
3861 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3862 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3863 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3864 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3865}
3866
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003867static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3868 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003869{
3870 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3871
3872 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3873 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003874 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003875
3876 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003877 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003878}
3879
3880static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3881{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003882 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003883
3884 for (i = 0; i < bp->nr_vnics; i++) {
3885 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3886
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003887 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3888 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3889 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3890 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003891 }
3892 bp->rsscos_nr_ctxs = 0;
3893}
3894
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003895static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003896{
3897 int rc;
3898 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3899 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3900 bp->hwrm_cmd_resp_addr;
3901
3902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3903 -1);
3904
3905 mutex_lock(&bp->hwrm_cmd_lock);
3906 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3907 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003908 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003909 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3910 mutex_unlock(&bp->hwrm_cmd_lock);
3911
3912 return rc;
3913}
3914
Michael Chana588e452016-12-07 00:26:21 -05003915int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003916{
Michael Chanb81a90d2016-01-02 23:45:01 -05003917 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003918 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3919 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003920 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003921
3922 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003923
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003924 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3925 /* Only RSS support for now TBD: COS & LB */
3926 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3927 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3928 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3929 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003930 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3931 req.rss_rule =
3932 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3933 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3934 VNIC_CFG_REQ_ENABLES_MRU);
3935 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003936 } else {
3937 req.rss_rule = cpu_to_le16(0xffff);
3938 }
3939
3940 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3941 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003942 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3943 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3944 } else {
3945 req.cos_rule = cpu_to_le16(0xffff);
3946 }
3947
Michael Chanc0c050c2015-10-22 16:01:17 -04003948 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003949 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003950 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003951 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003952 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3953 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003954
Michael Chanb81a90d2016-01-02 23:45:01 -05003955 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003956 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3957 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3958
3959 req.lb_rule = cpu_to_le16(0xffff);
3960 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3961 VLAN_HLEN);
3962
Michael Chancf6645f2016-06-13 02:25:28 -04003963#ifdef CONFIG_BNXT_SRIOV
3964 if (BNXT_VF(bp))
3965 def_vlan = bp->vf.vlan;
3966#endif
3967 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003968 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003969 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3970 req.flags |=
3971 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003972
3973 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3974}
3975
3976static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3977{
3978 u32 rc = 0;
3979
3980 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3981 struct hwrm_vnic_free_input req = {0};
3982
3983 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3984 req.vnic_id =
3985 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3986
3987 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3988 if (rc)
3989 return rc;
3990 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3991 }
3992 return rc;
3993}
3994
3995static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3996{
3997 u16 i;
3998
3999 for (i = 0; i < bp->nr_vnics; i++)
4000 bnxt_hwrm_vnic_free_one(bp, i);
4001}
4002
Michael Chanb81a90d2016-01-02 23:45:01 -05004003static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4004 unsigned int start_rx_ring_idx,
4005 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004006{
Michael Chanb81a90d2016-01-02 23:45:01 -05004007 int rc = 0;
4008 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004009 struct hwrm_vnic_alloc_input req = {0};
4010 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4011
4012 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004013 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4014 grp_idx = bp->rx_ring[i].bnapi->index;
4015 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004016 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004017 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004018 break;
4019 }
4020 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05004021 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004022 }
4023
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004024 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4025 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004026 if (vnic_id == 0)
4027 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4028
4029 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4030
4031 mutex_lock(&bp->hwrm_cmd_lock);
4032 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4033 if (!rc)
4034 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4035 mutex_unlock(&bp->hwrm_cmd_lock);
4036 return rc;
4037}
4038
Michael Chan8fdefd62016-12-29 12:13:36 -05004039static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4040{
4041 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4042 struct hwrm_vnic_qcaps_input req = {0};
4043 int rc;
4044
4045 if (bp->hwrm_spec_code < 0x10600)
4046 return 0;
4047
4048 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4049 mutex_lock(&bp->hwrm_cmd_lock);
4050 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4051 if (!rc) {
4052 if (resp->flags &
4053 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4054 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4055 }
4056 mutex_unlock(&bp->hwrm_cmd_lock);
4057 return rc;
4058}
4059
Michael Chanc0c050c2015-10-22 16:01:17 -04004060static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4061{
4062 u16 i;
4063 u32 rc = 0;
4064
4065 mutex_lock(&bp->hwrm_cmd_lock);
4066 for (i = 0; i < bp->rx_nr_rings; i++) {
4067 struct hwrm_ring_grp_alloc_input req = {0};
4068 struct hwrm_ring_grp_alloc_output *resp =
4069 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004070 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004071
4072 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4073
Michael Chanb81a90d2016-01-02 23:45:01 -05004074 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4075 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4076 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4077 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004078
4079 rc = _hwrm_send_message(bp, &req, sizeof(req),
4080 HWRM_CMD_TIMEOUT);
4081 if (rc)
4082 break;
4083
Michael Chanb81a90d2016-01-02 23:45:01 -05004084 bp->grp_info[grp_idx].fw_grp_id =
4085 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004086 }
4087 mutex_unlock(&bp->hwrm_cmd_lock);
4088 return rc;
4089}
4090
4091static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4092{
4093 u16 i;
4094 u32 rc = 0;
4095 struct hwrm_ring_grp_free_input req = {0};
4096
4097 if (!bp->grp_info)
4098 return 0;
4099
4100 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4101
4102 mutex_lock(&bp->hwrm_cmd_lock);
4103 for (i = 0; i < bp->cp_nr_rings; i++) {
4104 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4105 continue;
4106 req.ring_group_id =
4107 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4108
4109 rc = _hwrm_send_message(bp, &req, sizeof(req),
4110 HWRM_CMD_TIMEOUT);
4111 if (rc)
4112 break;
4113 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4114 }
4115 mutex_unlock(&bp->hwrm_cmd_lock);
4116 return rc;
4117}
4118
4119static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4120 struct bnxt_ring_struct *ring,
4121 u32 ring_type, u32 map_index,
4122 u32 stats_ctx_id)
4123{
4124 int rc = 0, err = 0;
4125 struct hwrm_ring_alloc_input req = {0};
4126 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4127 u16 ring_id;
4128
4129 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4130
4131 req.enables = 0;
4132 if (ring->nr_pages > 1) {
4133 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4134 /* Page size is in log2 units */
4135 req.page_size = BNXT_PAGE_SHIFT;
4136 req.page_tbl_depth = 1;
4137 } else {
4138 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4139 }
4140 req.fbo = 0;
4141 /* Association of ring index with doorbell index and MSIX number */
4142 req.logical_id = cpu_to_le16(map_index);
4143
4144 switch (ring_type) {
4145 case HWRM_RING_ALLOC_TX:
4146 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4147 /* Association of transmit ring with completion ring */
4148 req.cmpl_ring_id =
4149 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4150 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4151 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4152 req.queue_id = cpu_to_le16(ring->queue_id);
4153 break;
4154 case HWRM_RING_ALLOC_RX:
4155 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4156 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4157 break;
4158 case HWRM_RING_ALLOC_AGG:
4159 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4160 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4161 break;
4162 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004163 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004164 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4165 if (bp->flags & BNXT_FLAG_USING_MSIX)
4166 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4167 break;
4168 default:
4169 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4170 ring_type);
4171 return -1;
4172 }
4173
4174 mutex_lock(&bp->hwrm_cmd_lock);
4175 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4176 err = le16_to_cpu(resp->error_code);
4177 ring_id = le16_to_cpu(resp->ring_id);
4178 mutex_unlock(&bp->hwrm_cmd_lock);
4179
4180 if (rc || err) {
4181 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004182 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004183 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4184 rc, err);
4185 return -1;
4186
4187 case RING_FREE_REQ_RING_TYPE_RX:
4188 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4189 rc, err);
4190 return -1;
4191
4192 case RING_FREE_REQ_RING_TYPE_TX:
4193 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4194 rc, err);
4195 return -1;
4196
4197 default:
4198 netdev_err(bp->dev, "Invalid ring\n");
4199 return -1;
4200 }
4201 }
4202 ring->fw_ring_id = ring_id;
4203 return rc;
4204}
4205
Michael Chan486b5c22016-12-29 12:13:42 -05004206static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4207{
4208 int rc;
4209
4210 if (BNXT_PF(bp)) {
4211 struct hwrm_func_cfg_input req = {0};
4212
4213 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4214 req.fid = cpu_to_le16(0xffff);
4215 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4216 req.async_event_cr = cpu_to_le16(idx);
4217 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4218 } else {
4219 struct hwrm_func_vf_cfg_input req = {0};
4220
4221 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4222 req.enables =
4223 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4224 req.async_event_cr = cpu_to_le16(idx);
4225 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4226 }
4227 return rc;
4228}
4229
Michael Chanc0c050c2015-10-22 16:01:17 -04004230static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4231{
4232 int i, rc = 0;
4233
Michael Chanedd0c2c2015-12-27 18:19:19 -05004234 for (i = 0; i < bp->cp_nr_rings; i++) {
4235 struct bnxt_napi *bnapi = bp->bnapi[i];
4236 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4237 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004238
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004239 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004240 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4241 INVALID_STATS_CTX_ID);
4242 if (rc)
4243 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004244 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4245 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004246
4247 if (!i) {
4248 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4249 if (rc)
4250 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4251 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004252 }
4253
Michael Chanedd0c2c2015-12-27 18:19:19 -05004254 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004255 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004256 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004257 u32 map_idx = txr->bnapi->index;
4258 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004259
Michael Chanb81a90d2016-01-02 23:45:01 -05004260 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4261 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004262 if (rc)
4263 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004264 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004265 }
4266
Michael Chanedd0c2c2015-12-27 18:19:19 -05004267 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004268 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004269 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004270 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004271
Michael Chanb81a90d2016-01-02 23:45:01 -05004272 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4273 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004274 if (rc)
4275 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004276 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004277 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004278 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004279 }
4280
4281 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4282 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004283 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004284 struct bnxt_ring_struct *ring =
4285 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004286 u32 grp_idx = rxr->bnapi->index;
4287 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004288
4289 rc = hwrm_ring_alloc_send_msg(bp, ring,
4290 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004291 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004292 INVALID_STATS_CTX_ID);
4293 if (rc)
4294 goto err_out;
4295
Michael Chanb81a90d2016-01-02 23:45:01 -05004296 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004297 writel(DB_KEY_RX | rxr->rx_agg_prod,
4298 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004299 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004300 }
4301 }
4302err_out:
4303 return rc;
4304}
4305
4306static int hwrm_ring_free_send_msg(struct bnxt *bp,
4307 struct bnxt_ring_struct *ring,
4308 u32 ring_type, int cmpl_ring_id)
4309{
4310 int rc;
4311 struct hwrm_ring_free_input req = {0};
4312 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4313 u16 error_code;
4314
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004315 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004316 req.ring_type = ring_type;
4317 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4318
4319 mutex_lock(&bp->hwrm_cmd_lock);
4320 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4321 error_code = le16_to_cpu(resp->error_code);
4322 mutex_unlock(&bp->hwrm_cmd_lock);
4323
4324 if (rc || error_code) {
4325 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004326 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004327 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4328 rc);
4329 return rc;
4330 case RING_FREE_REQ_RING_TYPE_RX:
4331 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4332 rc);
4333 return rc;
4334 case RING_FREE_REQ_RING_TYPE_TX:
4335 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4336 rc);
4337 return rc;
4338 default:
4339 netdev_err(bp->dev, "Invalid ring\n");
4340 return -1;
4341 }
4342 }
4343 return 0;
4344}
4345
Michael Chanedd0c2c2015-12-27 18:19:19 -05004346static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004347{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004348 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004349
4350 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004351 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004352
Michael Chanedd0c2c2015-12-27 18:19:19 -05004353 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004354 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004355 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004356 u32 grp_idx = txr->bnapi->index;
4357 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004358
Michael Chanedd0c2c2015-12-27 18:19:19 -05004359 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4360 hwrm_ring_free_send_msg(bp, ring,
4361 RING_FREE_REQ_RING_TYPE_TX,
4362 close_path ? cmpl_ring_id :
4363 INVALID_HW_RING_ID);
4364 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004365 }
4366 }
4367
Michael Chanedd0c2c2015-12-27 18:19:19 -05004368 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004369 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004370 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004371 u32 grp_idx = rxr->bnapi->index;
4372 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004373
Michael Chanedd0c2c2015-12-27 18:19:19 -05004374 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4375 hwrm_ring_free_send_msg(bp, ring,
4376 RING_FREE_REQ_RING_TYPE_RX,
4377 close_path ? cmpl_ring_id :
4378 INVALID_HW_RING_ID);
4379 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004380 bp->grp_info[grp_idx].rx_fw_ring_id =
4381 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004382 }
4383 }
4384
Michael Chanedd0c2c2015-12-27 18:19:19 -05004385 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004386 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004387 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004388 u32 grp_idx = rxr->bnapi->index;
4389 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004390
Michael Chanedd0c2c2015-12-27 18:19:19 -05004391 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4392 hwrm_ring_free_send_msg(bp, ring,
4393 RING_FREE_REQ_RING_TYPE_RX,
4394 close_path ? cmpl_ring_id :
4395 INVALID_HW_RING_ID);
4396 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004397 bp->grp_info[grp_idx].agg_fw_ring_id =
4398 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004399 }
4400 }
4401
Michael Chan9d8bc092016-12-29 12:13:33 -05004402 /* The completion rings are about to be freed. After that the
4403 * IRQ doorbell will not work anymore. So we need to disable
4404 * IRQ here.
4405 */
4406 bnxt_disable_int_sync(bp);
4407
Michael Chanedd0c2c2015-12-27 18:19:19 -05004408 for (i = 0; i < bp->cp_nr_rings; i++) {
4409 struct bnxt_napi *bnapi = bp->bnapi[i];
4410 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4411 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004412
Michael Chanedd0c2c2015-12-27 18:19:19 -05004413 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4414 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004415 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004416 INVALID_HW_RING_ID);
4417 ring->fw_ring_id = INVALID_HW_RING_ID;
4418 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004419 }
4420 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004421}
4422
Michael Chan391be5c2016-12-29 12:13:41 -05004423/* Caller must hold bp->hwrm_cmd_lock */
4424int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4425{
4426 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4427 struct hwrm_func_qcfg_input req = {0};
4428 int rc;
4429
4430 if (bp->hwrm_spec_code < 0x10601)
4431 return 0;
4432
4433 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4434 req.fid = cpu_to_le16(fid);
4435 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4436 if (!rc)
4437 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4438
4439 return rc;
4440}
4441
Michael Chand1e79252017-02-06 16:55:38 -05004442static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004443{
4444 struct hwrm_func_cfg_input req = {0};
4445 int rc;
4446
4447 if (bp->hwrm_spec_code < 0x10601)
4448 return 0;
4449
4450 if (BNXT_VF(bp))
4451 return 0;
4452
4453 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4454 req.fid = cpu_to_le16(0xffff);
4455 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4456 req.num_tx_rings = cpu_to_le16(*tx_rings);
4457 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4458 if (rc)
4459 return rc;
4460
4461 mutex_lock(&bp->hwrm_cmd_lock);
4462 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4463 mutex_unlock(&bp->hwrm_cmd_lock);
4464 return rc;
4465}
4466
Michael Chanbb053f52016-02-26 04:00:02 -05004467static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4468 u32 buf_tmrs, u16 flags,
4469 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4470{
4471 req->flags = cpu_to_le16(flags);
4472 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4473 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4474 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4475 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4476 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4477 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4478 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4479 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4480}
4481
Michael Chanc0c050c2015-10-22 16:01:17 -04004482int bnxt_hwrm_set_coal(struct bnxt *bp)
4483{
4484 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004485 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4486 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004487 u16 max_buf, max_buf_irq;
4488 u16 buf_tmr, buf_tmr_irq;
4489 u32 flags;
4490
Michael Chandfc9c942016-02-26 04:00:03 -05004491 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4492 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4493 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4494 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004495
Michael Chandfb5b892016-02-26 04:00:01 -05004496 /* Each rx completion (2 records) should be DMAed immediately.
4497 * DMA 1/4 of the completion buffers at a time.
4498 */
4499 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004500 /* max_buf must not be zero */
4501 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004502 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4503 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4504 /* buf timer set to 1/4 of interrupt timer */
4505 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4506 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4507 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004508
4509 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4510
4511 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4512 * if coal_ticks is less than 25 us.
4513 */
Michael Chandfb5b892016-02-26 04:00:01 -05004514 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004515 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4516
Michael Chanbb053f52016-02-26 04:00:02 -05004517 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004518 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4519
4520 /* max_buf must not be zero */
4521 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4522 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4523 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4524 /* buf timer set to 1/4 of interrupt timer */
4525 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4526 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4527 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4528
4529 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4530 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4531 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004532
4533 mutex_lock(&bp->hwrm_cmd_lock);
4534 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004535 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004536
Michael Chandfc9c942016-02-26 04:00:03 -05004537 req = &req_rx;
4538 if (!bnapi->rx_ring)
4539 req = &req_tx;
4540 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4541
4542 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004543 HWRM_CMD_TIMEOUT);
4544 if (rc)
4545 break;
4546 }
4547 mutex_unlock(&bp->hwrm_cmd_lock);
4548 return rc;
4549}
4550
4551static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4552{
4553 int rc = 0, i;
4554 struct hwrm_stat_ctx_free_input req = {0};
4555
4556 if (!bp->bnapi)
4557 return 0;
4558
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004559 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4560 return 0;
4561
Michael Chanc0c050c2015-10-22 16:01:17 -04004562 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4563
4564 mutex_lock(&bp->hwrm_cmd_lock);
4565 for (i = 0; i < bp->cp_nr_rings; i++) {
4566 struct bnxt_napi *bnapi = bp->bnapi[i];
4567 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4568
4569 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4570 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4571
4572 rc = _hwrm_send_message(bp, &req, sizeof(req),
4573 HWRM_CMD_TIMEOUT);
4574 if (rc)
4575 break;
4576
4577 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4578 }
4579 }
4580 mutex_unlock(&bp->hwrm_cmd_lock);
4581 return rc;
4582}
4583
4584static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4585{
4586 int rc = 0, i;
4587 struct hwrm_stat_ctx_alloc_input req = {0};
4588 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4589
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004590 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4591 return 0;
4592
Michael Chanc0c050c2015-10-22 16:01:17 -04004593 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4594
Michael Chan51f30782016-07-01 18:46:29 -04004595 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004596
4597 mutex_lock(&bp->hwrm_cmd_lock);
4598 for (i = 0; i < bp->cp_nr_rings; i++) {
4599 struct bnxt_napi *bnapi = bp->bnapi[i];
4600 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4601
4602 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4603
4604 rc = _hwrm_send_message(bp, &req, sizeof(req),
4605 HWRM_CMD_TIMEOUT);
4606 if (rc)
4607 break;
4608
4609 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4610
4611 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4612 }
4613 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004614 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004615}
4616
Michael Chancf6645f2016-06-13 02:25:28 -04004617static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4618{
4619 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004620 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04004621 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04004622 int rc;
4623
4624 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4625 req.fid = cpu_to_le16(0xffff);
4626 mutex_lock(&bp->hwrm_cmd_lock);
4627 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4628 if (rc)
4629 goto func_qcfg_exit;
4630
4631#ifdef CONFIG_BNXT_SRIOV
4632 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004633 struct bnxt_vf_info *vf = &bp->vf;
4634
4635 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4636 }
4637#endif
Michael Chan9315edc2017-07-24 12:34:25 -04004638 flags = le16_to_cpu(resp->flags);
4639 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4640 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
4641 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4642 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
4643 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04004644 }
Michael Chan9315edc2017-07-24 12:34:25 -04004645 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
4646 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05004647
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004648 switch (resp->port_partition_type) {
4649 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4650 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4651 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4652 bp->port_partition_type = resp->port_partition_type;
4653 break;
4654 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004655 if (bp->hwrm_spec_code < 0x10707 ||
4656 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4657 bp->br_mode = BRIDGE_MODE_VEB;
4658 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4659 bp->br_mode = BRIDGE_MODE_VEPA;
4660 else
4661 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004662
4663func_qcfg_exit:
4664 mutex_unlock(&bp->hwrm_cmd_lock);
4665 return rc;
4666}
4667
Michael Chan7b08f662016-12-07 00:26:18 -05004668static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004669{
4670 int rc = 0;
4671 struct hwrm_func_qcaps_input req = {0};
4672 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4673
4674 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4675 req.fid = cpu_to_le16(0xffff);
4676
4677 mutex_lock(&bp->hwrm_cmd_lock);
4678 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4679 if (rc)
4680 goto hwrm_func_qcaps_exit;
4681
Michael Chane4060d32016-12-07 00:26:19 -05004682 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4683 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4684 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4685 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4686
Michael Chan7cc5a202016-09-19 03:58:05 -04004687 bp->tx_push_thresh = 0;
4688 if (resp->flags &
4689 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4690 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4691
Michael Chanc0c050c2015-10-22 16:01:17 -04004692 if (BNXT_PF(bp)) {
4693 struct bnxt_pf_info *pf = &bp->pf;
4694
4695 pf->fw_fid = le16_to_cpu(resp->fid);
4696 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004697 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004698 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004699 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004700 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4701 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4702 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004703 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004704 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4705 if (!pf->max_hw_ring_grps)
4706 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004707 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4708 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4709 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4710 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4711 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4712 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4713 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4714 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4715 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4716 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4717 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004718 if (resp->flags &
4719 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4720 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004721 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004722#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004723 struct bnxt_vf_info *vf = &bp->vf;
4724
4725 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004726
4727 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4728 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4729 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4730 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004731 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4732 if (!vf->max_hw_ring_grps)
4733 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004734 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4735 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4736 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004737
4738 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004739 mutex_unlock(&bp->hwrm_cmd_lock);
4740
4741 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004742 /* overwrite netdev dev_adr with admin VF MAC */
4743 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004744 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004745 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004746 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4747 }
4748 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004749#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004750 }
4751
Michael Chanc0c050c2015-10-22 16:01:17 -04004752hwrm_func_qcaps_exit:
4753 mutex_unlock(&bp->hwrm_cmd_lock);
4754 return rc;
4755}
4756
4757static int bnxt_hwrm_func_reset(struct bnxt *bp)
4758{
4759 struct hwrm_func_reset_input req = {0};
4760
4761 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4762 req.enables = 0;
4763
4764 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4765}
4766
4767static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4768{
4769 int rc = 0;
4770 struct hwrm_queue_qportcfg_input req = {0};
4771 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4772 u8 i, *qptr;
4773
4774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4775
4776 mutex_lock(&bp->hwrm_cmd_lock);
4777 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4778 if (rc)
4779 goto qportcfg_exit;
4780
4781 if (!resp->max_configurable_queues) {
4782 rc = -EINVAL;
4783 goto qportcfg_exit;
4784 }
4785 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004786 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004787 if (bp->max_tc > BNXT_MAX_QUEUE)
4788 bp->max_tc = BNXT_MAX_QUEUE;
4789
Michael Chan441cabb2016-09-19 03:58:02 -04004790 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4791 bp->max_tc = 1;
4792
Michael Chan87c374d2016-12-02 21:17:16 -05004793 if (bp->max_lltc > bp->max_tc)
4794 bp->max_lltc = bp->max_tc;
4795
Michael Chanc0c050c2015-10-22 16:01:17 -04004796 qptr = &resp->queue_id0;
4797 for (i = 0; i < bp->max_tc; i++) {
4798 bp->q_info[i].queue_id = *qptr++;
4799 bp->q_info[i].queue_profile = *qptr++;
4800 }
4801
4802qportcfg_exit:
4803 mutex_unlock(&bp->hwrm_cmd_lock);
4804 return rc;
4805}
4806
4807static int bnxt_hwrm_ver_get(struct bnxt *bp)
4808{
4809 int rc;
4810 struct hwrm_ver_get_input req = {0};
4811 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004812 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004813
Michael Chane6ef2692016-03-28 19:46:05 -04004814 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004815 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4816 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4817 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4818 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4819 mutex_lock(&bp->hwrm_cmd_lock);
4820 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4821 if (rc)
4822 goto hwrm_ver_get_exit;
4823
4824 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4825
Michael Chan11f15ed2016-04-05 14:08:55 -04004826 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4827 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004828 if (resp->hwrm_intf_maj < 1) {
4829 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004830 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004831 resp->hwrm_intf_upd);
4832 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004833 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004834 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004835 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4836 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4837
Michael Chanff4fe812016-02-26 04:00:04 -05004838 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4839 if (!bp->hwrm_cmd_timeout)
4840 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4841
Michael Chane6ef2692016-03-28 19:46:05 -04004842 if (resp->hwrm_intf_maj >= 1)
4843 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4844
Michael Chan659c8052016-06-13 02:25:33 -04004845 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004846 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4847 !resp->chip_metal)
4848 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004849
Deepak Khungare605db82017-05-29 19:06:04 -04004850 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4851 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4852 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4853 bp->flags |= BNXT_FLAG_SHORT_CMD;
4854
Michael Chanc0c050c2015-10-22 16:01:17 -04004855hwrm_ver_get_exit:
4856 mutex_unlock(&bp->hwrm_cmd_lock);
4857 return rc;
4858}
4859
Rob Swindell5ac67d82016-09-19 03:58:03 -04004860int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4861{
Rob Swindell878786d2016-09-20 03:36:33 -04004862#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004863 struct hwrm_fw_set_time_input req = {0};
4864 struct rtc_time tm;
4865 struct timeval tv;
4866
4867 if (bp->hwrm_spec_code < 0x10400)
4868 return -EOPNOTSUPP;
4869
4870 do_gettimeofday(&tv);
4871 rtc_time_to_tm(tv.tv_sec, &tm);
4872 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4873 req.year = cpu_to_le16(1900 + tm.tm_year);
4874 req.month = 1 + tm.tm_mon;
4875 req.day = tm.tm_mday;
4876 req.hour = tm.tm_hour;
4877 req.minute = tm.tm_min;
4878 req.second = tm.tm_sec;
4879 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004880#else
4881 return -EOPNOTSUPP;
4882#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004883}
4884
Michael Chan3bdf56c2016-03-07 15:38:45 -05004885static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4886{
4887 int rc;
4888 struct bnxt_pf_info *pf = &bp->pf;
4889 struct hwrm_port_qstats_input req = {0};
4890
4891 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4892 return 0;
4893
4894 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4895 req.port_id = cpu_to_le16(pf->port_id);
4896 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4897 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4898 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4899 return rc;
4900}
4901
Michael Chanc0c050c2015-10-22 16:01:17 -04004902static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4903{
4904 if (bp->vxlan_port_cnt) {
4905 bnxt_hwrm_tunnel_dst_port_free(
4906 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4907 }
4908 bp->vxlan_port_cnt = 0;
4909 if (bp->nge_port_cnt) {
4910 bnxt_hwrm_tunnel_dst_port_free(
4911 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4912 }
4913 bp->nge_port_cnt = 0;
4914}
4915
4916static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4917{
4918 int rc, i;
4919 u32 tpa_flags = 0;
4920
4921 if (set_tpa)
4922 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4923 for (i = 0; i < bp->nr_vnics; i++) {
4924 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4925 if (rc) {
4926 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004927 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004928 return rc;
4929 }
4930 }
4931 return 0;
4932}
4933
4934static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4935{
4936 int i;
4937
4938 for (i = 0; i < bp->nr_vnics; i++)
4939 bnxt_hwrm_vnic_set_rss(bp, i, false);
4940}
4941
4942static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4943 bool irq_re_init)
4944{
4945 if (bp->vnic_info) {
4946 bnxt_hwrm_clear_vnic_filter(bp);
4947 /* clear all RSS setting before free vnic ctx */
4948 bnxt_hwrm_clear_vnic_rss(bp);
4949 bnxt_hwrm_vnic_ctx_free(bp);
4950 /* before free the vnic, undo the vnic tpa settings */
4951 if (bp->flags & BNXT_FLAG_TPA)
4952 bnxt_set_tpa(bp, false);
4953 bnxt_hwrm_vnic_free(bp);
4954 }
4955 bnxt_hwrm_ring_free(bp, close_path);
4956 bnxt_hwrm_ring_grp_free(bp);
4957 if (irq_re_init) {
4958 bnxt_hwrm_stat_ctx_free(bp);
4959 bnxt_hwrm_free_tunnel_ports(bp);
4960 }
4961}
4962
Michael Chan39d8ba22017-07-24 12:34:22 -04004963static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
4964{
4965 struct hwrm_func_cfg_input req = {0};
4966 int rc;
4967
4968 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4969 req.fid = cpu_to_le16(0xffff);
4970 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
4971 if (br_mode == BRIDGE_MODE_VEB)
4972 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
4973 else if (br_mode == BRIDGE_MODE_VEPA)
4974 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
4975 else
4976 return -EINVAL;
4977 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4978 if (rc)
4979 rc = -EIO;
4980 return rc;
4981}
4982
Michael Chanc0c050c2015-10-22 16:01:17 -04004983static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4984{
Michael Chanae10ae72016-12-29 12:13:38 -05004985 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004986 int rc;
4987
Michael Chanae10ae72016-12-29 12:13:38 -05004988 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4989 goto skip_rss_ctx;
4990
Michael Chanc0c050c2015-10-22 16:01:17 -04004991 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004992 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004993 if (rc) {
4994 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4995 vnic_id, rc);
4996 goto vnic_setup_err;
4997 }
4998 bp->rsscos_nr_ctxs++;
4999
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005000 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5001 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5002 if (rc) {
5003 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5004 vnic_id, rc);
5005 goto vnic_setup_err;
5006 }
5007 bp->rsscos_nr_ctxs++;
5008 }
5009
Michael Chanae10ae72016-12-29 12:13:38 -05005010skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04005011 /* configure default vnic, ring grp */
5012 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5013 if (rc) {
5014 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5015 vnic_id, rc);
5016 goto vnic_setup_err;
5017 }
5018
5019 /* Enable RSS hashing on vnic */
5020 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5021 if (rc) {
5022 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5023 vnic_id, rc);
5024 goto vnic_setup_err;
5025 }
5026
5027 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5028 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5029 if (rc) {
5030 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5031 vnic_id, rc);
5032 }
5033 }
5034
5035vnic_setup_err:
5036 return rc;
5037}
5038
5039static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5040{
5041#ifdef CONFIG_RFS_ACCEL
5042 int i, rc = 0;
5043
5044 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005045 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005046 u16 vnic_id = i + 1;
5047 u16 ring_id = i;
5048
5049 if (vnic_id >= bp->nr_vnics)
5050 break;
5051
Michael Chanae10ae72016-12-29 12:13:38 -05005052 vnic = &bp->vnic_info[vnic_id];
5053 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5054 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5055 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005056 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005057 if (rc) {
5058 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5059 vnic_id, rc);
5060 break;
5061 }
5062 rc = bnxt_setup_vnic(bp, vnic_id);
5063 if (rc)
5064 break;
5065 }
5066 return rc;
5067#else
5068 return 0;
5069#endif
5070}
5071
Michael Chan17c71ac2016-07-01 18:46:27 -04005072/* Allow PF and VF with default VLAN to be in promiscuous mode */
5073static bool bnxt_promisc_ok(struct bnxt *bp)
5074{
5075#ifdef CONFIG_BNXT_SRIOV
5076 if (BNXT_VF(bp) && !bp->vf.vlan)
5077 return false;
5078#endif
5079 return true;
5080}
5081
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005082static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5083{
5084 unsigned int rc = 0;
5085
5086 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5087 if (rc) {
5088 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5089 rc);
5090 return rc;
5091 }
5092
5093 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5094 if (rc) {
5095 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5096 rc);
5097 return rc;
5098 }
5099 return rc;
5100}
5101
Michael Chanb664f002015-12-02 01:54:08 -05005102static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005103static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005104
Michael Chanc0c050c2015-10-22 16:01:17 -04005105static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5106{
Michael Chan7d2837d2016-05-04 16:56:44 -04005107 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005108 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005109 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005110
5111 if (irq_re_init) {
5112 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5113 if (rc) {
5114 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5115 rc);
5116 goto err_out;
5117 }
5118 }
5119
5120 rc = bnxt_hwrm_ring_alloc(bp);
5121 if (rc) {
5122 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5123 goto err_out;
5124 }
5125
5126 rc = bnxt_hwrm_ring_grp_alloc(bp);
5127 if (rc) {
5128 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5129 goto err_out;
5130 }
5131
Prashant Sreedharan76595192016-07-18 07:15:22 -04005132 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5133 rx_nr_rings--;
5134
Michael Chanc0c050c2015-10-22 16:01:17 -04005135 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005136 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005137 if (rc) {
5138 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5139 goto err_out;
5140 }
5141
5142 rc = bnxt_setup_vnic(bp, 0);
5143 if (rc)
5144 goto err_out;
5145
5146 if (bp->flags & BNXT_FLAG_RFS) {
5147 rc = bnxt_alloc_rfs_vnics(bp);
5148 if (rc)
5149 goto err_out;
5150 }
5151
5152 if (bp->flags & BNXT_FLAG_TPA) {
5153 rc = bnxt_set_tpa(bp, true);
5154 if (rc)
5155 goto err_out;
5156 }
5157
5158 if (BNXT_VF(bp))
5159 bnxt_update_vf_mac(bp);
5160
5161 /* Filter for default vnic 0 */
5162 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5163 if (rc) {
5164 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5165 goto err_out;
5166 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005167 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005168
Michael Chan7d2837d2016-05-04 16:56:44 -04005169 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005170
Michael Chan17c71ac2016-07-01 18:46:27 -04005171 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005172 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5173
5174 if (bp->dev->flags & IFF_ALLMULTI) {
5175 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5176 vnic->mc_list_count = 0;
5177 } else {
5178 u32 mask = 0;
5179
5180 bnxt_mc_list_updated(bp, &mask);
5181 vnic->rx_mask |= mask;
5182 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005183
Michael Chanb664f002015-12-02 01:54:08 -05005184 rc = bnxt_cfg_rx_mode(bp);
5185 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005186 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005187
5188 rc = bnxt_hwrm_set_coal(bp);
5189 if (rc)
5190 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005191 rc);
5192
5193 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5194 rc = bnxt_setup_nitroa0_vnic(bp);
5195 if (rc)
5196 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5197 rc);
5198 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005199
Michael Chancf6645f2016-06-13 02:25:28 -04005200 if (BNXT_VF(bp)) {
5201 bnxt_hwrm_func_qcfg(bp);
5202 netdev_update_features(bp->dev);
5203 }
5204
Michael Chanc0c050c2015-10-22 16:01:17 -04005205 return 0;
5206
5207err_out:
5208 bnxt_hwrm_resource_free(bp, 0, true);
5209
5210 return rc;
5211}
5212
5213static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5214{
5215 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5216 return 0;
5217}
5218
5219static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5220{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005221 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005222 bnxt_init_rx_rings(bp);
5223 bnxt_init_tx_rings(bp);
5224 bnxt_init_ring_grps(bp, irq_re_init);
5225 bnxt_init_vnics(bp);
5226
5227 return bnxt_init_chip(bp, irq_re_init);
5228}
5229
Michael Chanc0c050c2015-10-22 16:01:17 -04005230static int bnxt_set_real_num_queues(struct bnxt *bp)
5231{
5232 int rc;
5233 struct net_device *dev = bp->dev;
5234
Michael Chan5f449242017-02-06 16:55:40 -05005235 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5236 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005237 if (rc)
5238 return rc;
5239
5240 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5241 if (rc)
5242 return rc;
5243
5244#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005245 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005246 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005247#endif
5248
5249 return rc;
5250}
5251
Michael Chan6e6c5a52016-01-02 23:45:02 -05005252static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5253 bool shared)
5254{
5255 int _rx = *rx, _tx = *tx;
5256
5257 if (shared) {
5258 *rx = min_t(int, _rx, max);
5259 *tx = min_t(int, _tx, max);
5260 } else {
5261 if (max < 2)
5262 return -ENOMEM;
5263
5264 while (_rx + _tx > max) {
5265 if (_rx > _tx && _rx > 1)
5266 _rx--;
5267 else if (_tx > 1)
5268 _tx--;
5269 }
5270 *rx = _rx;
5271 *tx = _tx;
5272 }
5273 return 0;
5274}
5275
Michael Chan78095922016-12-07 00:26:16 -05005276static void bnxt_setup_msix(struct bnxt *bp)
5277{
5278 const int len = sizeof(bp->irq_tbl[0].name);
5279 struct net_device *dev = bp->dev;
5280 int tcs, i;
5281
5282 tcs = netdev_get_num_tc(dev);
5283 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005284 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005285
Michael Chand1e79252017-02-06 16:55:38 -05005286 for (i = 0; i < tcs; i++) {
5287 count = bp->tx_nr_rings_per_tc;
5288 off = i * count;
5289 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005290 }
5291 }
5292
5293 for (i = 0; i < bp->cp_nr_rings; i++) {
5294 char *attr;
5295
5296 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5297 attr = "TxRx";
5298 else if (i < bp->rx_nr_rings)
5299 attr = "rx";
5300 else
5301 attr = "tx";
5302
5303 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5304 i);
5305 bp->irq_tbl[i].handler = bnxt_msix;
5306 }
5307}
5308
5309static void bnxt_setup_inta(struct bnxt *bp)
5310{
5311 const int len = sizeof(bp->irq_tbl[0].name);
5312
5313 if (netdev_get_num_tc(bp->dev))
5314 netdev_reset_tc(bp->dev);
5315
5316 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5317 0);
5318 bp->irq_tbl[0].handler = bnxt_inta;
5319}
5320
5321static int bnxt_setup_int_mode(struct bnxt *bp)
5322{
5323 int rc;
5324
5325 if (bp->flags & BNXT_FLAG_USING_MSIX)
5326 bnxt_setup_msix(bp);
5327 else
5328 bnxt_setup_inta(bp);
5329
5330 rc = bnxt_set_real_num_queues(bp);
5331 return rc;
5332}
5333
Michael Chanb7429952017-01-13 01:32:00 -05005334#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005335static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5336{
5337#if defined(CONFIG_BNXT_SRIOV)
5338 if (BNXT_VF(bp))
5339 return bp->vf.max_rsscos_ctxs;
5340#endif
5341 return bp->pf.max_rsscos_ctxs;
5342}
5343
5344static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5345{
5346#if defined(CONFIG_BNXT_SRIOV)
5347 if (BNXT_VF(bp))
5348 return bp->vf.max_vnics;
5349#endif
5350 return bp->pf.max_vnics;
5351}
Michael Chanb7429952017-01-13 01:32:00 -05005352#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005353
Michael Chane4060d32016-12-07 00:26:19 -05005354unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5355{
5356#if defined(CONFIG_BNXT_SRIOV)
5357 if (BNXT_VF(bp))
5358 return bp->vf.max_stat_ctxs;
5359#endif
5360 return bp->pf.max_stat_ctxs;
5361}
5362
Michael Chana588e452016-12-07 00:26:21 -05005363void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5364{
5365#if defined(CONFIG_BNXT_SRIOV)
5366 if (BNXT_VF(bp))
5367 bp->vf.max_stat_ctxs = max;
5368 else
5369#endif
5370 bp->pf.max_stat_ctxs = max;
5371}
5372
Michael Chane4060d32016-12-07 00:26:19 -05005373unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5374{
5375#if defined(CONFIG_BNXT_SRIOV)
5376 if (BNXT_VF(bp))
5377 return bp->vf.max_cp_rings;
5378#endif
5379 return bp->pf.max_cp_rings;
5380}
5381
Michael Chana588e452016-12-07 00:26:21 -05005382void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5383{
5384#if defined(CONFIG_BNXT_SRIOV)
5385 if (BNXT_VF(bp))
5386 bp->vf.max_cp_rings = max;
5387 else
5388#endif
5389 bp->pf.max_cp_rings = max;
5390}
5391
Michael Chan78095922016-12-07 00:26:16 -05005392static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5393{
5394#if defined(CONFIG_BNXT_SRIOV)
5395 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005396 return min_t(unsigned int, bp->vf.max_irqs,
5397 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005398#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005399 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005400}
5401
Michael Chan33c26572016-12-07 00:26:15 -05005402void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5403{
5404#if defined(CONFIG_BNXT_SRIOV)
5405 if (BNXT_VF(bp))
5406 bp->vf.max_irqs = max_irqs;
5407 else
5408#endif
5409 bp->pf.max_irqs = max_irqs;
5410}
5411
Michael Chan78095922016-12-07 00:26:16 -05005412static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005413{
Michael Chan01657bc2016-01-02 23:45:03 -05005414 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005415 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005416
Michael Chan78095922016-12-07 00:26:16 -05005417 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005418 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5419 if (!msix_ent)
5420 return -ENOMEM;
5421
5422 for (i = 0; i < total_vecs; i++) {
5423 msix_ent[i].entry = i;
5424 msix_ent[i].vector = 0;
5425 }
5426
Michael Chan01657bc2016-01-02 23:45:03 -05005427 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5428 min = 2;
5429
5430 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005431 if (total_vecs < 0) {
5432 rc = -ENODEV;
5433 goto msix_setup_exit;
5434 }
5435
5436 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5437 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005438 for (i = 0; i < total_vecs; i++)
5439 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005440
Michael Chan78095922016-12-07 00:26:16 -05005441 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005442 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005443 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005444 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005445 if (rc)
5446 goto msix_setup_exit;
5447
Michael Chanc0c050c2015-10-22 16:01:17 -04005448 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005449 bp->cp_nr_rings = (min == 1) ?
5450 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5451 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005452
Michael Chanc0c050c2015-10-22 16:01:17 -04005453 } else {
5454 rc = -ENOMEM;
5455 goto msix_setup_exit;
5456 }
5457 bp->flags |= BNXT_FLAG_USING_MSIX;
5458 kfree(msix_ent);
5459 return 0;
5460
5461msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005462 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5463 kfree(bp->irq_tbl);
5464 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005465 pci_disable_msix(bp->pdev);
5466 kfree(msix_ent);
5467 return rc;
5468}
5469
Michael Chan78095922016-12-07 00:26:16 -05005470static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005471{
Michael Chanc0c050c2015-10-22 16:01:17 -04005472 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005473 if (!bp->irq_tbl)
5474 return -ENOMEM;
5475
5476 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005477 bp->rx_nr_rings = 1;
5478 bp->tx_nr_rings = 1;
5479 bp->cp_nr_rings = 1;
5480 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005481 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005482 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005483 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005484}
5485
Michael Chan78095922016-12-07 00:26:16 -05005486static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005487{
5488 int rc = 0;
5489
5490 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005491 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005492
Michael Chan1fa72e22016-04-25 02:30:49 -04005493 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005494 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005495 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005496 }
5497 return rc;
5498}
5499
Michael Chan78095922016-12-07 00:26:16 -05005500static void bnxt_clear_int_mode(struct bnxt *bp)
5501{
5502 if (bp->flags & BNXT_FLAG_USING_MSIX)
5503 pci_disable_msix(bp->pdev);
5504
5505 kfree(bp->irq_tbl);
5506 bp->irq_tbl = NULL;
5507 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5508}
5509
Michael Chanc0c050c2015-10-22 16:01:17 -04005510static void bnxt_free_irq(struct bnxt *bp)
5511{
5512 struct bnxt_irq *irq;
5513 int i;
5514
5515#ifdef CONFIG_RFS_ACCEL
5516 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5517 bp->dev->rx_cpu_rmap = NULL;
5518#endif
5519 if (!bp->irq_tbl)
5520 return;
5521
5522 for (i = 0; i < bp->cp_nr_rings; i++) {
5523 irq = &bp->irq_tbl[i];
5524 if (irq->requested)
5525 free_irq(irq->vector, bp->bnapi[i]);
5526 irq->requested = 0;
5527 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005528}
5529
5530static int bnxt_request_irq(struct bnxt *bp)
5531{
Michael Chanb81a90d2016-01-02 23:45:01 -05005532 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005533 unsigned long flags = 0;
5534#ifdef CONFIG_RFS_ACCEL
5535 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5536#endif
5537
5538 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5539 flags = IRQF_SHARED;
5540
Michael Chanb81a90d2016-01-02 23:45:01 -05005541 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005542 struct bnxt_irq *irq = &bp->irq_tbl[i];
5543#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005544 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005545 rc = irq_cpu_rmap_add(rmap, irq->vector);
5546 if (rc)
5547 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005548 j);
5549 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005550 }
5551#endif
5552 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5553 bp->bnapi[i]);
5554 if (rc)
5555 break;
5556
5557 irq->requested = 1;
5558 }
5559 return rc;
5560}
5561
5562static void bnxt_del_napi(struct bnxt *bp)
5563{
5564 int i;
5565
5566 if (!bp->bnapi)
5567 return;
5568
5569 for (i = 0; i < bp->cp_nr_rings; i++) {
5570 struct bnxt_napi *bnapi = bp->bnapi[i];
5571
5572 napi_hash_del(&bnapi->napi);
5573 netif_napi_del(&bnapi->napi);
5574 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005575 /* We called napi_hash_del() before netif_napi_del(), we need
5576 * to respect an RCU grace period before freeing napi structures.
5577 */
5578 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005579}
5580
5581static void bnxt_init_napi(struct bnxt *bp)
5582{
5583 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005584 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005585 struct bnxt_napi *bnapi;
5586
5587 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005588 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5589 cp_nr_rings--;
5590 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005591 bnapi = bp->bnapi[i];
5592 netif_napi_add(bp->dev, &bnapi->napi,
5593 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005594 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005595 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5596 bnapi = bp->bnapi[cp_nr_rings];
5597 netif_napi_add(bp->dev, &bnapi->napi,
5598 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005599 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005600 } else {
5601 bnapi = bp->bnapi[0];
5602 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005603 }
5604}
5605
5606static void bnxt_disable_napi(struct bnxt *bp)
5607{
5608 int i;
5609
5610 if (!bp->bnapi)
5611 return;
5612
Michael Chanb356a2e2016-12-29 12:13:31 -05005613 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005614 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005615}
5616
5617static void bnxt_enable_napi(struct bnxt *bp)
5618{
5619 int i;
5620
5621 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005622 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005623 napi_enable(&bp->bnapi[i]->napi);
5624 }
5625}
5626
Michael Chan7df4ae92016-12-02 21:17:17 -05005627void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005628{
5629 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005630 struct bnxt_tx_ring_info *txr;
5631 struct netdev_queue *txq;
5632
Michael Chanb6ab4b02016-01-02 23:44:59 -05005633 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005634 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005635 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005636 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005637 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005638 }
5639 }
5640 /* Stop all TX queues */
5641 netif_tx_disable(bp->dev);
5642 netif_carrier_off(bp->dev);
5643}
5644
Michael Chan7df4ae92016-12-02 21:17:17 -05005645void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005646{
5647 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005648 struct bnxt_tx_ring_info *txr;
5649 struct netdev_queue *txq;
5650
5651 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005652 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005653 txq = netdev_get_tx_queue(bp->dev, i);
5654 txr->dev_state = 0;
5655 }
5656 netif_tx_wake_all_queues(bp->dev);
5657 if (bp->link_info.link_up)
5658 netif_carrier_on(bp->dev);
5659}
5660
5661static void bnxt_report_link(struct bnxt *bp)
5662{
5663 if (bp->link_info.link_up) {
5664 const char *duplex;
5665 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005666 u32 speed;
5667 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005668
5669 netif_carrier_on(bp->dev);
5670 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5671 duplex = "full";
5672 else
5673 duplex = "half";
5674 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5675 flow_ctrl = "ON - receive & transmit";
5676 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5677 flow_ctrl = "ON - transmit";
5678 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5679 flow_ctrl = "ON - receive";
5680 else
5681 flow_ctrl = "none";
5682 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005683 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005684 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005685 if (bp->flags & BNXT_FLAG_EEE_CAP)
5686 netdev_info(bp->dev, "EEE is %s\n",
5687 bp->eee.eee_active ? "active" :
5688 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005689 fec = bp->link_info.fec_cfg;
5690 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5691 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5692 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5693 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5694 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005695 } else {
5696 netif_carrier_off(bp->dev);
5697 netdev_err(bp->dev, "NIC Link is Down\n");
5698 }
5699}
5700
Michael Chan170ce012016-04-05 14:08:57 -04005701static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5702{
5703 int rc = 0;
5704 struct hwrm_port_phy_qcaps_input req = {0};
5705 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005706 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005707
5708 if (bp->hwrm_spec_code < 0x10201)
5709 return 0;
5710
5711 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5712
5713 mutex_lock(&bp->hwrm_cmd_lock);
5714 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5715 if (rc)
5716 goto hwrm_phy_qcaps_exit;
5717
Michael Chanacb20052017-07-24 12:34:20 -04005718 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005719 struct ethtool_eee *eee = &bp->eee;
5720 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5721
5722 bp->flags |= BNXT_FLAG_EEE_CAP;
5723 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5724 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5725 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5726 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5727 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5728 }
Michael Chan520ad892017-03-08 18:44:35 -05005729 if (resp->supported_speeds_auto_mode)
5730 link_info->support_auto_speeds =
5731 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005732
5733hwrm_phy_qcaps_exit:
5734 mutex_unlock(&bp->hwrm_cmd_lock);
5735 return rc;
5736}
5737
Michael Chanc0c050c2015-10-22 16:01:17 -04005738static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5739{
5740 int rc = 0;
5741 struct bnxt_link_info *link_info = &bp->link_info;
5742 struct hwrm_port_phy_qcfg_input req = {0};
5743 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5744 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005745 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005746
5747 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5748
5749 mutex_lock(&bp->hwrm_cmd_lock);
5750 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5751 if (rc) {
5752 mutex_unlock(&bp->hwrm_cmd_lock);
5753 return rc;
5754 }
5755
5756 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5757 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005758 link_info->duplex = resp->duplex_cfg;
5759 if (bp->hwrm_spec_code >= 0x10800)
5760 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005761 link_info->pause = resp->pause;
5762 link_info->auto_mode = resp->auto_mode;
5763 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005764 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005765 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005766 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005767 if (link_info->phy_link_status == BNXT_LINK_LINK)
5768 link_info->link_speed = le16_to_cpu(resp->link_speed);
5769 else
5770 link_info->link_speed = 0;
5771 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005772 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5773 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005774 link_info->lp_auto_link_speeds =
5775 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005776 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5777 link_info->phy_ver[0] = resp->phy_maj;
5778 link_info->phy_ver[1] = resp->phy_min;
5779 link_info->phy_ver[2] = resp->phy_bld;
5780 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005781 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005782 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005783 link_info->phy_addr = resp->eee_config_phy_addr &
5784 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005785 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005786
Michael Chan170ce012016-04-05 14:08:57 -04005787 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5788 struct ethtool_eee *eee = &bp->eee;
5789 u16 fw_speeds;
5790
5791 eee->eee_active = 0;
5792 if (resp->eee_config_phy_addr &
5793 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5794 eee->eee_active = 1;
5795 fw_speeds = le16_to_cpu(
5796 resp->link_partner_adv_eee_link_speed_mask);
5797 eee->lp_advertised =
5798 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5799 }
5800
5801 /* Pull initial EEE config */
5802 if (!chng_link_state) {
5803 if (resp->eee_config_phy_addr &
5804 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5805 eee->eee_enabled = 1;
5806
5807 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5808 eee->advertised =
5809 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5810
5811 if (resp->eee_config_phy_addr &
5812 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5813 __le32 tmr;
5814
5815 eee->tx_lpi_enabled = 1;
5816 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5817 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5818 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5819 }
5820 }
5821 }
Michael Chane70c7522017-02-12 19:18:16 -05005822
5823 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5824 if (bp->hwrm_spec_code >= 0x10504)
5825 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5826
Michael Chanc0c050c2015-10-22 16:01:17 -04005827 /* TODO: need to add more logic to report VF link */
5828 if (chng_link_state) {
5829 if (link_info->phy_link_status == BNXT_LINK_LINK)
5830 link_info->link_up = 1;
5831 else
5832 link_info->link_up = 0;
5833 if (link_up != link_info->link_up)
5834 bnxt_report_link(bp);
5835 } else {
5836 /* alwasy link down if not require to update link state */
5837 link_info->link_up = 0;
5838 }
5839 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005840
5841 diff = link_info->support_auto_speeds ^ link_info->advertising;
5842 if ((link_info->support_auto_speeds | diff) !=
5843 link_info->support_auto_speeds) {
5844 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005845 * update the advertisement settings. Caller holds RTNL
5846 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005847 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005848 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005849 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005850 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005851 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005852 return 0;
5853}
5854
Michael Chan10289be2016-05-15 03:04:49 -04005855static void bnxt_get_port_module_status(struct bnxt *bp)
5856{
5857 struct bnxt_link_info *link_info = &bp->link_info;
5858 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5859 u8 module_status;
5860
5861 if (bnxt_update_link(bp, true))
5862 return;
5863
5864 module_status = link_info->module_status;
5865 switch (module_status) {
5866 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5867 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5868 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5869 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5870 bp->pf.port_id);
5871 if (bp->hwrm_spec_code >= 0x10201) {
5872 netdev_warn(bp->dev, "Module part number %s\n",
5873 resp->phy_vendor_partnumber);
5874 }
5875 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5876 netdev_warn(bp->dev, "TX is disabled\n");
5877 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5878 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5879 }
5880}
5881
Michael Chanc0c050c2015-10-22 16:01:17 -04005882static void
5883bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5884{
5885 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005886 if (bp->hwrm_spec_code >= 0x10201)
5887 req->auto_pause =
5888 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005889 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5890 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5891 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005892 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005893 req->enables |=
5894 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5895 } else {
5896 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5897 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5898 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5899 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5900 req->enables |=
5901 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005902 if (bp->hwrm_spec_code >= 0x10201) {
5903 req->auto_pause = req->force_pause;
5904 req->enables |= cpu_to_le32(
5905 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5906 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005907 }
5908}
5909
5910static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5911 struct hwrm_port_phy_cfg_input *req)
5912{
5913 u8 autoneg = bp->link_info.autoneg;
5914 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005915 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005916
5917 if (autoneg & BNXT_AUTONEG_SPEED) {
5918 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005919 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005920
5921 req->enables |= cpu_to_le32(
5922 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5923 req->auto_link_speed_mask = cpu_to_le16(advertising);
5924
5925 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5926 req->flags |=
5927 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5928 } else {
5929 req->force_link_speed = cpu_to_le16(fw_link_speed);
5930 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5931 }
5932
Michael Chanc0c050c2015-10-22 16:01:17 -04005933 /* tell chimp that the setting takes effect immediately */
5934 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5935}
5936
5937int bnxt_hwrm_set_pause(struct bnxt *bp)
5938{
5939 struct hwrm_port_phy_cfg_input req = {0};
5940 int rc;
5941
5942 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5943 bnxt_hwrm_set_pause_common(bp, &req);
5944
5945 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5946 bp->link_info.force_link_chng)
5947 bnxt_hwrm_set_link_common(bp, &req);
5948
5949 mutex_lock(&bp->hwrm_cmd_lock);
5950 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5951 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5952 /* since changing of pause setting doesn't trigger any link
5953 * change event, the driver needs to update the current pause
5954 * result upon successfully return of the phy_cfg command
5955 */
5956 bp->link_info.pause =
5957 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5958 bp->link_info.auto_pause_setting = 0;
5959 if (!bp->link_info.force_link_chng)
5960 bnxt_report_link(bp);
5961 }
5962 bp->link_info.force_link_chng = false;
5963 mutex_unlock(&bp->hwrm_cmd_lock);
5964 return rc;
5965}
5966
Michael Chan939f7f02016-04-05 14:08:58 -04005967static void bnxt_hwrm_set_eee(struct bnxt *bp,
5968 struct hwrm_port_phy_cfg_input *req)
5969{
5970 struct ethtool_eee *eee = &bp->eee;
5971
5972 if (eee->eee_enabled) {
5973 u16 eee_speeds;
5974 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5975
5976 if (eee->tx_lpi_enabled)
5977 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5978 else
5979 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5980
5981 req->flags |= cpu_to_le32(flags);
5982 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5983 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5984 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5985 } else {
5986 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5987 }
5988}
5989
5990int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005991{
5992 struct hwrm_port_phy_cfg_input req = {0};
5993
5994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5995 if (set_pause)
5996 bnxt_hwrm_set_pause_common(bp, &req);
5997
5998 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005999
6000 if (set_eee)
6001 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04006002 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6003}
6004
Michael Chan33f7d552016-04-11 04:11:12 -04006005static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6006{
6007 struct hwrm_port_phy_cfg_input req = {0};
6008
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006009 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04006010 return 0;
6011
6012 if (pci_num_vf(bp->pdev))
6013 return 0;
6014
6015 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05006016 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04006017 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6018}
6019
Michael Chan5ad2cbe2017-01-13 01:32:03 -05006020static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6021{
6022 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6023 struct hwrm_port_led_qcaps_input req = {0};
6024 struct bnxt_pf_info *pf = &bp->pf;
6025 int rc;
6026
6027 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6028 return 0;
6029
6030 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6031 req.port_id = cpu_to_le16(pf->port_id);
6032 mutex_lock(&bp->hwrm_cmd_lock);
6033 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6034 if (rc) {
6035 mutex_unlock(&bp->hwrm_cmd_lock);
6036 return rc;
6037 }
6038 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6039 int i;
6040
6041 bp->num_leds = resp->num_leds;
6042 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6043 bp->num_leds);
6044 for (i = 0; i < bp->num_leds; i++) {
6045 struct bnxt_led_info *led = &bp->leds[i];
6046 __le16 caps = led->led_state_caps;
6047
6048 if (!led->led_group_id ||
6049 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6050 bp->num_leds = 0;
6051 break;
6052 }
6053 }
6054 }
6055 mutex_unlock(&bp->hwrm_cmd_lock);
6056 return 0;
6057}
6058
Michael Chan5282db62017-04-04 18:14:10 -04006059int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6060{
6061 struct hwrm_wol_filter_alloc_input req = {0};
6062 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6063 int rc;
6064
6065 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6066 req.port_id = cpu_to_le16(bp->pf.port_id);
6067 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6068 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6069 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6070 mutex_lock(&bp->hwrm_cmd_lock);
6071 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6072 if (!rc)
6073 bp->wol_filter_id = resp->wol_filter_id;
6074 mutex_unlock(&bp->hwrm_cmd_lock);
6075 return rc;
6076}
6077
6078int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6079{
6080 struct hwrm_wol_filter_free_input req = {0};
6081 int rc;
6082
6083 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6084 req.port_id = cpu_to_le16(bp->pf.port_id);
6085 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6086 req.wol_filter_id = bp->wol_filter_id;
6087 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6088 return rc;
6089}
6090
Michael Chanc1ef1462017-04-04 18:14:07 -04006091static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6092{
6093 struct hwrm_wol_filter_qcfg_input req = {0};
6094 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6095 u16 next_handle = 0;
6096 int rc;
6097
6098 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6099 req.port_id = cpu_to_le16(bp->pf.port_id);
6100 req.handle = cpu_to_le16(handle);
6101 mutex_lock(&bp->hwrm_cmd_lock);
6102 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6103 if (!rc) {
6104 next_handle = le16_to_cpu(resp->next_handle);
6105 if (next_handle != 0) {
6106 if (resp->wol_type ==
6107 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6108 bp->wol = 1;
6109 bp->wol_filter_id = resp->wol_filter_id;
6110 }
6111 }
6112 }
6113 mutex_unlock(&bp->hwrm_cmd_lock);
6114 return next_handle;
6115}
6116
6117static void bnxt_get_wol_settings(struct bnxt *bp)
6118{
6119 u16 handle = 0;
6120
6121 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6122 return;
6123
6124 do {
6125 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6126 } while (handle && handle != 0xffff);
6127}
6128
Michael Chan939f7f02016-04-05 14:08:58 -04006129static bool bnxt_eee_config_ok(struct bnxt *bp)
6130{
6131 struct ethtool_eee *eee = &bp->eee;
6132 struct bnxt_link_info *link_info = &bp->link_info;
6133
6134 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6135 return true;
6136
6137 if (eee->eee_enabled) {
6138 u32 advertising =
6139 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6140
6141 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6142 eee->eee_enabled = 0;
6143 return false;
6144 }
6145 if (eee->advertised & ~advertising) {
6146 eee->advertised = advertising & eee->supported;
6147 return false;
6148 }
6149 }
6150 return true;
6151}
6152
Michael Chanc0c050c2015-10-22 16:01:17 -04006153static int bnxt_update_phy_setting(struct bnxt *bp)
6154{
6155 int rc;
6156 bool update_link = false;
6157 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006158 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006159 struct bnxt_link_info *link_info = &bp->link_info;
6160
6161 rc = bnxt_update_link(bp, true);
6162 if (rc) {
6163 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6164 rc);
6165 return rc;
6166 }
Michael Chan33dac242017-02-12 19:18:15 -05006167 if (!BNXT_SINGLE_PF(bp))
6168 return 0;
6169
Michael Chanc0c050c2015-10-22 16:01:17 -04006170 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006171 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6172 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006173 update_pause = true;
6174 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6175 link_info->force_pause_setting != link_info->req_flow_ctrl)
6176 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006177 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6178 if (BNXT_AUTO_MODE(link_info->auto_mode))
6179 update_link = true;
6180 if (link_info->req_link_speed != link_info->force_link_speed)
6181 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006182 if (link_info->req_duplex != link_info->duplex_setting)
6183 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006184 } else {
6185 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6186 update_link = true;
6187 if (link_info->advertising != link_info->auto_link_speeds)
6188 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006189 }
6190
Michael Chan16d663a2016-11-16 21:13:07 -05006191 /* The last close may have shutdown the link, so need to call
6192 * PHY_CFG to bring it back up.
6193 */
6194 if (!netif_carrier_ok(bp->dev))
6195 update_link = true;
6196
Michael Chan939f7f02016-04-05 14:08:58 -04006197 if (!bnxt_eee_config_ok(bp))
6198 update_eee = true;
6199
Michael Chanc0c050c2015-10-22 16:01:17 -04006200 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006201 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006202 else if (update_pause)
6203 rc = bnxt_hwrm_set_pause(bp);
6204 if (rc) {
6205 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6206 rc);
6207 return rc;
6208 }
6209
6210 return rc;
6211}
6212
Jeffrey Huang11809492015-11-05 16:25:49 -05006213/* Common routine to pre-map certain register block to different GRC window.
6214 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6215 * in PF and 3 windows in VF that can be customized to map in different
6216 * register blocks.
6217 */
6218static void bnxt_preset_reg_win(struct bnxt *bp)
6219{
6220 if (BNXT_PF(bp)) {
6221 /* CAG registers map to GRC window #4 */
6222 writel(BNXT_CAG_REG_BASE,
6223 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6224 }
6225}
6226
Michael Chanc0c050c2015-10-22 16:01:17 -04006227static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6228{
6229 int rc = 0;
6230
Jeffrey Huang11809492015-11-05 16:25:49 -05006231 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006232 netif_carrier_off(bp->dev);
6233 if (irq_re_init) {
6234 rc = bnxt_setup_int_mode(bp);
6235 if (rc) {
6236 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6237 rc);
6238 return rc;
6239 }
6240 }
6241 if ((bp->flags & BNXT_FLAG_RFS) &&
6242 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6243 /* disable RFS if falling back to INTA */
6244 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6245 bp->flags &= ~BNXT_FLAG_RFS;
6246 }
6247
6248 rc = bnxt_alloc_mem(bp, irq_re_init);
6249 if (rc) {
6250 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6251 goto open_err_free_mem;
6252 }
6253
6254 if (irq_re_init) {
6255 bnxt_init_napi(bp);
6256 rc = bnxt_request_irq(bp);
6257 if (rc) {
6258 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6259 goto open_err;
6260 }
6261 }
6262
6263 bnxt_enable_napi(bp);
6264
6265 rc = bnxt_init_nic(bp, irq_re_init);
6266 if (rc) {
6267 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6268 goto open_err;
6269 }
6270
6271 if (link_re_init) {
6272 rc = bnxt_update_phy_setting(bp);
6273 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006274 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006275 }
6276
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006277 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006278 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006279
Michael Chancaefe522015-12-09 19:35:42 -05006280 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006281 bnxt_enable_int(bp);
6282 /* Enable TX queues */
6283 bnxt_tx_enable(bp);
6284 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006285 /* Poll link status and check for SFP+ module status */
6286 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006287
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006288 /* VF-reps may need to be re-opened after the PF is re-opened */
6289 if (BNXT_PF(bp))
6290 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006291 return 0;
6292
6293open_err:
6294 bnxt_disable_napi(bp);
6295 bnxt_del_napi(bp);
6296
6297open_err_free_mem:
6298 bnxt_free_skbs(bp);
6299 bnxt_free_irq(bp);
6300 bnxt_free_mem(bp, true);
6301 return rc;
6302}
6303
6304/* rtnl_lock held */
6305int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6306{
6307 int rc = 0;
6308
6309 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6310 if (rc) {
6311 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6312 dev_close(bp->dev);
6313 }
6314 return rc;
6315}
6316
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006317/* rtnl_lock held, open the NIC half way by allocating all resources, but
6318 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6319 * self tests.
6320 */
6321int bnxt_half_open_nic(struct bnxt *bp)
6322{
6323 int rc = 0;
6324
6325 rc = bnxt_alloc_mem(bp, false);
6326 if (rc) {
6327 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6328 goto half_open_err;
6329 }
6330 rc = bnxt_init_nic(bp, false);
6331 if (rc) {
6332 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6333 goto half_open_err;
6334 }
6335 return 0;
6336
6337half_open_err:
6338 bnxt_free_skbs(bp);
6339 bnxt_free_mem(bp, false);
6340 dev_close(bp->dev);
6341 return rc;
6342}
6343
6344/* rtnl_lock held, this call can only be made after a previous successful
6345 * call to bnxt_half_open_nic().
6346 */
6347void bnxt_half_close_nic(struct bnxt *bp)
6348{
6349 bnxt_hwrm_resource_free(bp, false, false);
6350 bnxt_free_skbs(bp);
6351 bnxt_free_mem(bp, false);
6352}
6353
Michael Chanc0c050c2015-10-22 16:01:17 -04006354static int bnxt_open(struct net_device *dev)
6355{
6356 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006357
Michael Chanc0c050c2015-10-22 16:01:17 -04006358 return __bnxt_open_nic(bp, true, true);
6359}
6360
Michael Chanf9b76eb2017-07-11 13:05:34 -04006361static bool bnxt_drv_busy(struct bnxt *bp)
6362{
6363 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6364 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6365}
6366
Michael Chanc0c050c2015-10-22 16:01:17 -04006367int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6368{
6369 int rc = 0;
6370
6371#ifdef CONFIG_BNXT_SRIOV
6372 if (bp->sriov_cfg) {
6373 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6374 !bp->sriov_cfg,
6375 BNXT_SRIOV_CFG_WAIT_TMO);
6376 if (rc)
6377 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6378 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006379
6380 /* Close the VF-reps before closing PF */
6381 if (BNXT_PF(bp))
6382 bnxt_vf_reps_close(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006383#endif
6384 /* Change device state to avoid TX queue wake up's */
6385 bnxt_tx_disable(bp);
6386
Michael Chancaefe522015-12-09 19:35:42 -05006387 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006388 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006389 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006390 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006391
Michael Chan9d8bc092016-12-29 12:13:33 -05006392 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006393 bnxt_shutdown_nic(bp, irq_re_init);
6394
6395 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6396
6397 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006398 del_timer_sync(&bp->timer);
6399 bnxt_free_skbs(bp);
6400
6401 if (irq_re_init) {
6402 bnxt_free_irq(bp);
6403 bnxt_del_napi(bp);
6404 }
6405 bnxt_free_mem(bp, irq_re_init);
6406 return rc;
6407}
6408
6409static int bnxt_close(struct net_device *dev)
6410{
6411 struct bnxt *bp = netdev_priv(dev);
6412
6413 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006414 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006415 return 0;
6416}
6417
6418/* rtnl_lock held */
6419static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6420{
6421 switch (cmd) {
6422 case SIOCGMIIPHY:
6423 /* fallthru */
6424 case SIOCGMIIREG: {
6425 if (!netif_running(dev))
6426 return -EAGAIN;
6427
6428 return 0;
6429 }
6430
6431 case SIOCSMIIREG:
6432 if (!netif_running(dev))
6433 return -EAGAIN;
6434
6435 return 0;
6436
6437 default:
6438 /* do nothing */
6439 break;
6440 }
6441 return -EOPNOTSUPP;
6442}
6443
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006444static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006445bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6446{
6447 u32 i;
6448 struct bnxt *bp = netdev_priv(dev);
6449
Michael Chanf9b76eb2017-07-11 13:05:34 -04006450 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6451 /* Make sure bnxt_close_nic() sees that we are reading stats before
6452 * we check the BNXT_STATE_OPEN flag.
6453 */
6454 smp_mb__after_atomic();
6455 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6456 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006457 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006459
6460 /* TODO check if we need to synchronize with bnxt_close path */
6461 for (i = 0; i < bp->cp_nr_rings; i++) {
6462 struct bnxt_napi *bnapi = bp->bnapi[i];
6463 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6464 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6465
6466 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6467 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6468 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6469
6470 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6471 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6472 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6473
6474 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6475 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6476 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6477
6478 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6479 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6480 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6481
6482 stats->rx_missed_errors +=
6483 le64_to_cpu(hw_stats->rx_discard_pkts);
6484
6485 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6486
Michael Chanc0c050c2015-10-22 16:01:17 -04006487 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6488 }
6489
Michael Chan9947f832016-03-07 15:38:46 -05006490 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6491 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6492 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6493
6494 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6495 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6496 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6497 le64_to_cpu(rx->rx_ovrsz_frames) +
6498 le64_to_cpu(rx->rx_runt_frames);
6499 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6500 le64_to_cpu(rx->rx_jbr_frames);
6501 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6502 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6503 stats->tx_errors = le64_to_cpu(tx->tx_err);
6504 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006505 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006506}
6507
6508static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6509{
6510 struct net_device *dev = bp->dev;
6511 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6512 struct netdev_hw_addr *ha;
6513 u8 *haddr;
6514 int mc_count = 0;
6515 bool update = false;
6516 int off = 0;
6517
6518 netdev_for_each_mc_addr(ha, dev) {
6519 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6520 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6521 vnic->mc_list_count = 0;
6522 return false;
6523 }
6524 haddr = ha->addr;
6525 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6526 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6527 update = true;
6528 }
6529 off += ETH_ALEN;
6530 mc_count++;
6531 }
6532 if (mc_count)
6533 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6534
6535 if (mc_count != vnic->mc_list_count) {
6536 vnic->mc_list_count = mc_count;
6537 update = true;
6538 }
6539 return update;
6540}
6541
6542static bool bnxt_uc_list_updated(struct bnxt *bp)
6543{
6544 struct net_device *dev = bp->dev;
6545 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6546 struct netdev_hw_addr *ha;
6547 int off = 0;
6548
6549 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6550 return true;
6551
6552 netdev_for_each_uc_addr(ha, dev) {
6553 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6554 return true;
6555
6556 off += ETH_ALEN;
6557 }
6558 return false;
6559}
6560
6561static void bnxt_set_rx_mode(struct net_device *dev)
6562{
6563 struct bnxt *bp = netdev_priv(dev);
6564 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6565 u32 mask = vnic->rx_mask;
6566 bool mc_update = false;
6567 bool uc_update;
6568
6569 if (!netif_running(dev))
6570 return;
6571
6572 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6573 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6574 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6575
Michael Chan17c71ac2016-07-01 18:46:27 -04006576 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006577 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6578
6579 uc_update = bnxt_uc_list_updated(bp);
6580
6581 if (dev->flags & IFF_ALLMULTI) {
6582 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6583 vnic->mc_list_count = 0;
6584 } else {
6585 mc_update = bnxt_mc_list_updated(bp, &mask);
6586 }
6587
6588 if (mask != vnic->rx_mask || uc_update || mc_update) {
6589 vnic->rx_mask = mask;
6590
6591 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6592 schedule_work(&bp->sp_task);
6593 }
6594}
6595
Michael Chanb664f002015-12-02 01:54:08 -05006596static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006597{
6598 struct net_device *dev = bp->dev;
6599 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6600 struct netdev_hw_addr *ha;
6601 int i, off = 0, rc;
6602 bool uc_update;
6603
6604 netif_addr_lock_bh(dev);
6605 uc_update = bnxt_uc_list_updated(bp);
6606 netif_addr_unlock_bh(dev);
6607
6608 if (!uc_update)
6609 goto skip_uc;
6610
6611 mutex_lock(&bp->hwrm_cmd_lock);
6612 for (i = 1; i < vnic->uc_filter_count; i++) {
6613 struct hwrm_cfa_l2_filter_free_input req = {0};
6614
6615 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6616 -1);
6617
6618 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6619
6620 rc = _hwrm_send_message(bp, &req, sizeof(req),
6621 HWRM_CMD_TIMEOUT);
6622 }
6623 mutex_unlock(&bp->hwrm_cmd_lock);
6624
6625 vnic->uc_filter_count = 1;
6626
6627 netif_addr_lock_bh(dev);
6628 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6629 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6630 } else {
6631 netdev_for_each_uc_addr(ha, dev) {
6632 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6633 off += ETH_ALEN;
6634 vnic->uc_filter_count++;
6635 }
6636 }
6637 netif_addr_unlock_bh(dev);
6638
6639 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6640 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6641 if (rc) {
6642 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6643 rc);
6644 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006645 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006646 }
6647 }
6648
6649skip_uc:
6650 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6651 if (rc)
6652 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6653 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006654
6655 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006656}
6657
Michael Chan8079e8f2016-12-29 12:13:37 -05006658/* If the chip and firmware supports RFS */
6659static bool bnxt_rfs_supported(struct bnxt *bp)
6660{
6661 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6662 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006663 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6664 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006665 return false;
6666}
6667
6668/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006669static bool bnxt_rfs_capable(struct bnxt *bp)
6670{
6671#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006672 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006673
Michael Chan964fd482017-02-12 19:18:13 -05006674 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006675 return false;
6676
6677 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006678 max_vnics = bnxt_get_max_func_vnics(bp);
6679 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006680
6681 /* RSS contexts not a limiting factor */
6682 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6683 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006684 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006685 netdev_warn(bp->dev,
6686 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006687 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006688 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006689 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006690
6691 return true;
6692#else
6693 return false;
6694#endif
6695}
6696
Michael Chanc0c050c2015-10-22 16:01:17 -04006697static netdev_features_t bnxt_fix_features(struct net_device *dev,
6698 netdev_features_t features)
6699{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006700 struct bnxt *bp = netdev_priv(dev);
6701
Vasundhara Volama2304902016-07-25 12:33:36 -04006702 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006703 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006704
6705 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6706 * turned on or off together.
6707 */
6708 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6709 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6710 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6711 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6712 NETIF_F_HW_VLAN_STAG_RX);
6713 else
6714 features |= NETIF_F_HW_VLAN_CTAG_RX |
6715 NETIF_F_HW_VLAN_STAG_RX;
6716 }
Michael Chancf6645f2016-06-13 02:25:28 -04006717#ifdef CONFIG_BNXT_SRIOV
6718 if (BNXT_VF(bp)) {
6719 if (bp->vf.vlan) {
6720 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6721 NETIF_F_HW_VLAN_STAG_RX);
6722 }
6723 }
6724#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006725 return features;
6726}
6727
6728static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6729{
6730 struct bnxt *bp = netdev_priv(dev);
6731 u32 flags = bp->flags;
6732 u32 changes;
6733 int rc = 0;
6734 bool re_init = false;
6735 bool update_tpa = false;
6736
6737 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006738 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006739 flags |= BNXT_FLAG_GRO;
6740 if (features & NETIF_F_LRO)
6741 flags |= BNXT_FLAG_LRO;
6742
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006743 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6744 flags &= ~BNXT_FLAG_TPA;
6745
Michael Chanc0c050c2015-10-22 16:01:17 -04006746 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6747 flags |= BNXT_FLAG_STRIP_VLAN;
6748
6749 if (features & NETIF_F_NTUPLE)
6750 flags |= BNXT_FLAG_RFS;
6751
6752 changes = flags ^ bp->flags;
6753 if (changes & BNXT_FLAG_TPA) {
6754 update_tpa = true;
6755 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6756 (flags & BNXT_FLAG_TPA) == 0)
6757 re_init = true;
6758 }
6759
6760 if (changes & ~BNXT_FLAG_TPA)
6761 re_init = true;
6762
6763 if (flags != bp->flags) {
6764 u32 old_flags = bp->flags;
6765
6766 bp->flags = flags;
6767
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006768 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006769 if (update_tpa)
6770 bnxt_set_ring_params(bp);
6771 return rc;
6772 }
6773
6774 if (re_init) {
6775 bnxt_close_nic(bp, false, false);
6776 if (update_tpa)
6777 bnxt_set_ring_params(bp);
6778
6779 return bnxt_open_nic(bp, false, false);
6780 }
6781 if (update_tpa) {
6782 rc = bnxt_set_tpa(bp,
6783 (flags & BNXT_FLAG_TPA) ?
6784 true : false);
6785 if (rc)
6786 bp->flags = old_flags;
6787 }
6788 }
6789 return rc;
6790}
6791
Michael Chan9f554592016-01-02 23:44:58 -05006792static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6793{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006794 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006795 int i = bnapi->index;
6796
Michael Chan3b2b7d92016-01-02 23:45:00 -05006797 if (!txr)
6798 return;
6799
Michael Chan9f554592016-01-02 23:44:58 -05006800 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6801 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6802 txr->tx_cons);
6803}
6804
6805static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6806{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006807 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006808 int i = bnapi->index;
6809
Michael Chan3b2b7d92016-01-02 23:45:00 -05006810 if (!rxr)
6811 return;
6812
Michael Chan9f554592016-01-02 23:44:58 -05006813 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",
6814 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6815 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6816 rxr->rx_sw_agg_prod);
6817}
6818
6819static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6820{
6821 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6822 int i = bnapi->index;
6823
6824 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6825 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6826}
6827
Michael Chanc0c050c2015-10-22 16:01:17 -04006828static void bnxt_dbg_dump_states(struct bnxt *bp)
6829{
6830 int i;
6831 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006832
6833 for (i = 0; i < bp->cp_nr_rings; i++) {
6834 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006835 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006836 bnxt_dump_tx_sw_state(bnapi);
6837 bnxt_dump_rx_sw_state(bnapi);
6838 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006839 }
6840 }
6841}
6842
Michael Chan6988bd92016-06-13 02:25:29 -04006843static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006844{
Michael Chan6988bd92016-06-13 02:25:29 -04006845 if (!silent)
6846 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006847 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006848 int rc;
6849
6850 if (!silent)
6851 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006852 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006853 rc = bnxt_open_nic(bp, false, false);
6854 if (!silent && !rc)
6855 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006856 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006857}
6858
6859static void bnxt_tx_timeout(struct net_device *dev)
6860{
6861 struct bnxt *bp = netdev_priv(dev);
6862
6863 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6864 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6865 schedule_work(&bp->sp_task);
6866}
6867
6868#ifdef CONFIG_NET_POLL_CONTROLLER
6869static void bnxt_poll_controller(struct net_device *dev)
6870{
6871 struct bnxt *bp = netdev_priv(dev);
6872 int i;
6873
Michael Chan2270bc52017-06-23 14:01:01 -04006874 /* Only process tx rings/combined rings in netpoll mode. */
6875 for (i = 0; i < bp->tx_nr_rings; i++) {
6876 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006877
Michael Chan2270bc52017-06-23 14:01:01 -04006878 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006879 }
6880}
6881#endif
6882
6883static void bnxt_timer(unsigned long data)
6884{
6885 struct bnxt *bp = (struct bnxt *)data;
6886 struct net_device *dev = bp->dev;
6887
6888 if (!netif_running(dev))
6889 return;
6890
6891 if (atomic_read(&bp->intr_sem) != 0)
6892 goto bnxt_restart_timer;
6893
Michael Chanadcc3312017-07-24 12:34:24 -04006894 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
6895 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05006896 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6897 schedule_work(&bp->sp_task);
6898 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006899bnxt_restart_timer:
6900 mod_timer(&bp->timer, jiffies + bp->current_interval);
6901}
6902
Michael Chana551ee92017-01-25 02:55:07 -05006903static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006904{
Michael Chana551ee92017-01-25 02:55:07 -05006905 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6906 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006907 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6908 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6909 */
6910 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6911 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006912}
6913
6914static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6915{
Michael Chan6988bd92016-06-13 02:25:29 -04006916 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6917 rtnl_unlock();
6918}
6919
Michael Chana551ee92017-01-25 02:55:07 -05006920/* Only called from bnxt_sp_task() */
6921static void bnxt_reset(struct bnxt *bp, bool silent)
6922{
6923 bnxt_rtnl_lock_sp(bp);
6924 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6925 bnxt_reset_task(bp, silent);
6926 bnxt_rtnl_unlock_sp(bp);
6927}
6928
Michael Chanc0c050c2015-10-22 16:01:17 -04006929static void bnxt_cfg_ntp_filters(struct bnxt *);
6930
6931static void bnxt_sp_task(struct work_struct *work)
6932{
6933 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006934
Michael Chan4cebdce2015-12-09 19:35:43 -05006935 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6936 smp_mb__after_atomic();
6937 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6938 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006939 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006940 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006941
6942 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6943 bnxt_cfg_rx_mode(bp);
6944
6945 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6946 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006947 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6948 bnxt_hwrm_exec_fwd_req(bp);
6949 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6950 bnxt_hwrm_tunnel_dst_port_alloc(
6951 bp, bp->vxlan_port,
6952 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6953 }
6954 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6955 bnxt_hwrm_tunnel_dst_port_free(
6956 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6957 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006958 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6959 bnxt_hwrm_tunnel_dst_port_alloc(
6960 bp, bp->nge_port,
6961 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6962 }
6963 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6964 bnxt_hwrm_tunnel_dst_port_free(
6965 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6966 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006967 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6968 bnxt_hwrm_port_qstats(bp);
6969
Michael Chana551ee92017-01-25 02:55:07 -05006970 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6971 * must be the last functions to be called before exiting.
6972 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006973 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6974 int rc = 0;
6975
6976 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6977 &bp->sp_event))
6978 bnxt_hwrm_phy_qcaps(bp);
6979
6980 bnxt_rtnl_lock_sp(bp);
6981 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6982 rc = bnxt_update_link(bp, true);
6983 bnxt_rtnl_unlock_sp(bp);
6984 if (rc)
6985 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6986 rc);
6987 }
Michael Chan90c694b2017-01-25 02:55:09 -05006988 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6989 bnxt_rtnl_lock_sp(bp);
6990 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6991 bnxt_get_port_module_status(bp);
6992 bnxt_rtnl_unlock_sp(bp);
6993 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006994 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6995 bnxt_reset(bp, false);
6996
6997 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6998 bnxt_reset(bp, true);
6999
Michael Chanc0c050c2015-10-22 16:01:17 -04007000 smp_mb__before_atomic();
7001 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7002}
7003
Michael Chand1e79252017-02-06 16:55:38 -05007004/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04007005int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7006 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05007007{
7008 int max_rx, max_tx, tx_sets = 1;
7009 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05007010 int rc;
7011
Michael Chand1e79252017-02-06 16:55:38 -05007012 if (tcs)
7013 tx_sets = tcs;
7014
7015 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7016 if (rc)
7017 return rc;
7018
7019 if (max_rx < rx)
7020 return -ENOMEM;
7021
Michael Chan5f449242017-02-06 16:55:40 -05007022 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05007023 if (max_tx < tx_rings_needed)
7024 return -ENOMEM;
7025
7026 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05007027 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05007028 return -ENOMEM;
7029 return 0;
7030}
7031
Sathya Perla17086392017-02-20 19:25:18 -05007032static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7033{
7034 if (bp->bar2) {
7035 pci_iounmap(pdev, bp->bar2);
7036 bp->bar2 = NULL;
7037 }
7038
7039 if (bp->bar1) {
7040 pci_iounmap(pdev, bp->bar1);
7041 bp->bar1 = NULL;
7042 }
7043
7044 if (bp->bar0) {
7045 pci_iounmap(pdev, bp->bar0);
7046 bp->bar0 = NULL;
7047 }
7048}
7049
7050static void bnxt_cleanup_pci(struct bnxt *bp)
7051{
7052 bnxt_unmap_bars(bp, bp->pdev);
7053 pci_release_regions(bp->pdev);
7054 pci_disable_device(bp->pdev);
7055}
7056
Michael Chanc0c050c2015-10-22 16:01:17 -04007057static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7058{
7059 int rc;
7060 struct bnxt *bp = netdev_priv(dev);
7061
7062 SET_NETDEV_DEV(dev, &pdev->dev);
7063
7064 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7065 rc = pci_enable_device(pdev);
7066 if (rc) {
7067 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7068 goto init_err;
7069 }
7070
7071 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7072 dev_err(&pdev->dev,
7073 "Cannot find PCI device base address, aborting\n");
7074 rc = -ENODEV;
7075 goto init_err_disable;
7076 }
7077
7078 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7079 if (rc) {
7080 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7081 goto init_err_disable;
7082 }
7083
7084 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7085 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7086 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7087 goto init_err_disable;
7088 }
7089
7090 pci_set_master(pdev);
7091
7092 bp->dev = dev;
7093 bp->pdev = pdev;
7094
7095 bp->bar0 = pci_ioremap_bar(pdev, 0);
7096 if (!bp->bar0) {
7097 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7098 rc = -ENOMEM;
7099 goto init_err_release;
7100 }
7101
7102 bp->bar1 = pci_ioremap_bar(pdev, 2);
7103 if (!bp->bar1) {
7104 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7105 rc = -ENOMEM;
7106 goto init_err_release;
7107 }
7108
7109 bp->bar2 = pci_ioremap_bar(pdev, 4);
7110 if (!bp->bar2) {
7111 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7112 rc = -ENOMEM;
7113 goto init_err_release;
7114 }
7115
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007116 pci_enable_pcie_error_reporting(pdev);
7117
Michael Chanc0c050c2015-10-22 16:01:17 -04007118 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7119
7120 spin_lock_init(&bp->ntp_fltr_lock);
7121
7122 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7123 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7124
Michael Chandfb5b892016-02-26 04:00:01 -05007125 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007126 bp->rx_coal_ticks = 12;
7127 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007128 bp->rx_coal_ticks_irq = 1;
7129 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007130
Michael Chandfc9c942016-02-26 04:00:03 -05007131 bp->tx_coal_ticks = 25;
7132 bp->tx_coal_bufs = 30;
7133 bp->tx_coal_ticks_irq = 2;
7134 bp->tx_coal_bufs_irq = 2;
7135
Michael Chan51f30782016-07-01 18:46:29 -04007136 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7137
Michael Chanc0c050c2015-10-22 16:01:17 -04007138 init_timer(&bp->timer);
7139 bp->timer.data = (unsigned long)bp;
7140 bp->timer.function = bnxt_timer;
7141 bp->current_interval = BNXT_TIMER_INTERVAL;
7142
Michael Chancaefe522015-12-09 19:35:42 -05007143 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007144 return 0;
7145
7146init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007147 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007148 pci_release_regions(pdev);
7149
7150init_err_disable:
7151 pci_disable_device(pdev);
7152
7153init_err:
7154 return rc;
7155}
7156
7157/* rtnl_lock held */
7158static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7159{
7160 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007161 struct bnxt *bp = netdev_priv(dev);
7162 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007163
7164 if (!is_valid_ether_addr(addr->sa_data))
7165 return -EADDRNOTAVAIL;
7166
Michael Chan84c33dd2016-04-11 04:11:13 -04007167 rc = bnxt_approve_mac(bp, addr->sa_data);
7168 if (rc)
7169 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007170
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007171 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7172 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007173
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007174 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7175 if (netif_running(dev)) {
7176 bnxt_close_nic(bp, false, false);
7177 rc = bnxt_open_nic(bp, false, false);
7178 }
7179
7180 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007181}
7182
7183/* rtnl_lock held */
7184static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7185{
7186 struct bnxt *bp = netdev_priv(dev);
7187
Michael Chanc0c050c2015-10-22 16:01:17 -04007188 if (netif_running(dev))
7189 bnxt_close_nic(bp, false, false);
7190
7191 dev->mtu = new_mtu;
7192 bnxt_set_ring_params(bp);
7193
7194 if (netif_running(dev))
7195 return bnxt_open_nic(bp, false, false);
7196
7197 return 0;
7198}
7199
Michael Chanc5e3deb2016-12-02 21:17:15 -05007200int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007201{
7202 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007203 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007204 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007205
Michael Chanc0c050c2015-10-22 16:01:17 -04007206 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007207 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007208 tc, bp->max_tc);
7209 return -EINVAL;
7210 }
7211
7212 if (netdev_get_num_tc(dev) == tc)
7213 return 0;
7214
Michael Chan3ffb6a32016-11-11 00:11:42 -05007215 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7216 sh = true;
7217
Michael Chan5f449242017-02-06 16:55:40 -05007218 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007219 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007220 if (rc)
7221 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007222
7223 /* Needs to close the device and do hw resource re-allocations */
7224 if (netif_running(bp->dev))
7225 bnxt_close_nic(bp, true, false);
7226
7227 if (tc) {
7228 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7229 netdev_set_num_tc(dev, tc);
7230 } else {
7231 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7232 netdev_reset_tc(dev);
7233 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007234 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7235 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007236 bp->num_stat_ctxs = bp->cp_nr_rings;
7237
7238 if (netif_running(bp->dev))
7239 return bnxt_open_nic(bp, true, false);
7240
7241 return 0;
7242}
7243
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007244static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7245 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007246{
7247 if (ntc->type != TC_SETUP_MQPRIO)
7248 return -EINVAL;
7249
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007250 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7251
7252 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007253}
7254
Michael Chanc0c050c2015-10-22 16:01:17 -04007255#ifdef CONFIG_RFS_ACCEL
7256static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7257 struct bnxt_ntuple_filter *f2)
7258{
7259 struct flow_keys *keys1 = &f1->fkeys;
7260 struct flow_keys *keys2 = &f2->fkeys;
7261
7262 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7263 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7264 keys1->ports.ports == keys2->ports.ports &&
7265 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7266 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007267 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007268 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7269 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007270 return true;
7271
7272 return false;
7273}
7274
7275static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7276 u16 rxq_index, u32 flow_id)
7277{
7278 struct bnxt *bp = netdev_priv(dev);
7279 struct bnxt_ntuple_filter *fltr, *new_fltr;
7280 struct flow_keys *fkeys;
7281 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007282 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007283 struct hlist_head *head;
7284
Michael Chana54c4d72016-07-25 12:33:35 -04007285 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7286 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7287 int off = 0, j;
7288
7289 netif_addr_lock_bh(dev);
7290 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7291 if (ether_addr_equal(eth->h_dest,
7292 vnic->uc_list + off)) {
7293 l2_idx = j + 1;
7294 break;
7295 }
7296 }
7297 netif_addr_unlock_bh(dev);
7298 if (!l2_idx)
7299 return -EINVAL;
7300 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007301 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7302 if (!new_fltr)
7303 return -ENOMEM;
7304
7305 fkeys = &new_fltr->fkeys;
7306 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7307 rc = -EPROTONOSUPPORT;
7308 goto err_free;
7309 }
7310
Michael Chandda0e742016-12-29 12:13:40 -05007311 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7312 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007313 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7314 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7315 rc = -EPROTONOSUPPORT;
7316 goto err_free;
7317 }
Michael Chandda0e742016-12-29 12:13:40 -05007318 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7319 bp->hwrm_spec_code < 0x10601) {
7320 rc = -EPROTONOSUPPORT;
7321 goto err_free;
7322 }
Michael Chan61aad722017-02-12 19:18:14 -05007323 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7324 bp->hwrm_spec_code < 0x10601) {
7325 rc = -EPROTONOSUPPORT;
7326 goto err_free;
7327 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007328
Michael Chana54c4d72016-07-25 12:33:35 -04007329 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007330 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7331
7332 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7333 head = &bp->ntp_fltr_hash_tbl[idx];
7334 rcu_read_lock();
7335 hlist_for_each_entry_rcu(fltr, head, hash) {
7336 if (bnxt_fltr_match(fltr, new_fltr)) {
7337 rcu_read_unlock();
7338 rc = 0;
7339 goto err_free;
7340 }
7341 }
7342 rcu_read_unlock();
7343
7344 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007345 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7346 BNXT_NTP_FLTR_MAX_FLTR, 0);
7347 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007348 spin_unlock_bh(&bp->ntp_fltr_lock);
7349 rc = -ENOMEM;
7350 goto err_free;
7351 }
7352
Michael Chan84e86b92015-11-05 16:25:50 -05007353 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007354 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007355 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007356 new_fltr->rxq = rxq_index;
7357 hlist_add_head_rcu(&new_fltr->hash, head);
7358 bp->ntp_fltr_count++;
7359 spin_unlock_bh(&bp->ntp_fltr_lock);
7360
7361 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7362 schedule_work(&bp->sp_task);
7363
7364 return new_fltr->sw_id;
7365
7366err_free:
7367 kfree(new_fltr);
7368 return rc;
7369}
7370
7371static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7372{
7373 int i;
7374
7375 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7376 struct hlist_head *head;
7377 struct hlist_node *tmp;
7378 struct bnxt_ntuple_filter *fltr;
7379 int rc;
7380
7381 head = &bp->ntp_fltr_hash_tbl[i];
7382 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7383 bool del = false;
7384
7385 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7386 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7387 fltr->flow_id,
7388 fltr->sw_id)) {
7389 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7390 fltr);
7391 del = true;
7392 }
7393 } else {
7394 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7395 fltr);
7396 if (rc)
7397 del = true;
7398 else
7399 set_bit(BNXT_FLTR_VALID, &fltr->state);
7400 }
7401
7402 if (del) {
7403 spin_lock_bh(&bp->ntp_fltr_lock);
7404 hlist_del_rcu(&fltr->hash);
7405 bp->ntp_fltr_count--;
7406 spin_unlock_bh(&bp->ntp_fltr_lock);
7407 synchronize_rcu();
7408 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7409 kfree(fltr);
7410 }
7411 }
7412 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007413 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7414 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007415}
7416
7417#else
7418
7419static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7420{
7421}
7422
7423#endif /* CONFIG_RFS_ACCEL */
7424
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007425static void bnxt_udp_tunnel_add(struct net_device *dev,
7426 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007427{
7428 struct bnxt *bp = netdev_priv(dev);
7429
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007430 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7431 return;
7432
Michael Chanc0c050c2015-10-22 16:01:17 -04007433 if (!netif_running(dev))
7434 return;
7435
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007436 switch (ti->type) {
7437 case UDP_TUNNEL_TYPE_VXLAN:
7438 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7439 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007440
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007441 bp->vxlan_port_cnt++;
7442 if (bp->vxlan_port_cnt == 1) {
7443 bp->vxlan_port = ti->port;
7444 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007445 schedule_work(&bp->sp_task);
7446 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007447 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007448 case UDP_TUNNEL_TYPE_GENEVE:
7449 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7450 return;
7451
7452 bp->nge_port_cnt++;
7453 if (bp->nge_port_cnt == 1) {
7454 bp->nge_port = ti->port;
7455 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7456 }
7457 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007458 default:
7459 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007460 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007461
7462 schedule_work(&bp->sp_task);
7463}
7464
7465static void bnxt_udp_tunnel_del(struct net_device *dev,
7466 struct udp_tunnel_info *ti)
7467{
7468 struct bnxt *bp = netdev_priv(dev);
7469
7470 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7471 return;
7472
7473 if (!netif_running(dev))
7474 return;
7475
7476 switch (ti->type) {
7477 case UDP_TUNNEL_TYPE_VXLAN:
7478 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7479 return;
7480 bp->vxlan_port_cnt--;
7481
7482 if (bp->vxlan_port_cnt != 0)
7483 return;
7484
7485 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7486 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007487 case UDP_TUNNEL_TYPE_GENEVE:
7488 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7489 return;
7490 bp->nge_port_cnt--;
7491
7492 if (bp->nge_port_cnt != 0)
7493 return;
7494
7495 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7496 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007497 default:
7498 return;
7499 }
7500
7501 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007502}
7503
Michael Chan39d8ba22017-07-24 12:34:22 -04007504static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7505 struct net_device *dev, u32 filter_mask,
7506 int nlflags)
7507{
7508 struct bnxt *bp = netdev_priv(dev);
7509
7510 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7511 nlflags, filter_mask, NULL);
7512}
7513
7514static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7515 u16 flags)
7516{
7517 struct bnxt *bp = netdev_priv(dev);
7518 struct nlattr *attr, *br_spec;
7519 int rem, rc = 0;
7520
7521 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7522 return -EOPNOTSUPP;
7523
7524 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7525 if (!br_spec)
7526 return -EINVAL;
7527
7528 nla_for_each_nested(attr, br_spec, rem) {
7529 u16 mode;
7530
7531 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7532 continue;
7533
7534 if (nla_len(attr) < sizeof(mode))
7535 return -EINVAL;
7536
7537 mode = nla_get_u16(attr);
7538 if (mode == bp->br_mode)
7539 break;
7540
7541 rc = bnxt_hwrm_set_br_mode(bp, mode);
7542 if (!rc)
7543 bp->br_mode = mode;
7544 break;
7545 }
7546 return rc;
7547}
7548
Michael Chanc0c050c2015-10-22 16:01:17 -04007549static const struct net_device_ops bnxt_netdev_ops = {
7550 .ndo_open = bnxt_open,
7551 .ndo_start_xmit = bnxt_start_xmit,
7552 .ndo_stop = bnxt_close,
7553 .ndo_get_stats64 = bnxt_get_stats64,
7554 .ndo_set_rx_mode = bnxt_set_rx_mode,
7555 .ndo_do_ioctl = bnxt_ioctl,
7556 .ndo_validate_addr = eth_validate_addr,
7557 .ndo_set_mac_address = bnxt_change_mac_addr,
7558 .ndo_change_mtu = bnxt_change_mtu,
7559 .ndo_fix_features = bnxt_fix_features,
7560 .ndo_set_features = bnxt_set_features,
7561 .ndo_tx_timeout = bnxt_tx_timeout,
7562#ifdef CONFIG_BNXT_SRIOV
7563 .ndo_get_vf_config = bnxt_get_vf_config,
7564 .ndo_set_vf_mac = bnxt_set_vf_mac,
7565 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7566 .ndo_set_vf_rate = bnxt_set_vf_bw,
7567 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7568 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7569#endif
7570#ifdef CONFIG_NET_POLL_CONTROLLER
7571 .ndo_poll_controller = bnxt_poll_controller,
7572#endif
7573 .ndo_setup_tc = bnxt_setup_tc,
7574#ifdef CONFIG_RFS_ACCEL
7575 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7576#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007577 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7578 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007579 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007580 .ndo_bridge_getlink = bnxt_bridge_getlink,
7581 .ndo_bridge_setlink = bnxt_bridge_setlink,
Michael Chanc0c050c2015-10-22 16:01:17 -04007582};
7583
7584static void bnxt_remove_one(struct pci_dev *pdev)
7585{
7586 struct net_device *dev = pci_get_drvdata(pdev);
7587 struct bnxt *bp = netdev_priv(dev);
7588
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007589 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007590 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007591 bnxt_dl_unregister(bp);
7592 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007593
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007594 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007595 unregister_netdev(dev);
7596 cancel_work_sync(&bp->sp_task);
7597 bp->sp_event = 0;
7598
Michael Chan78095922016-12-07 00:26:16 -05007599 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007600 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007601 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007602 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007603 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007604 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007605 kfree(bp->edev);
7606 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007607 if (bp->xdp_prog)
7608 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007609 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007610 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007611}
7612
7613static int bnxt_probe_phy(struct bnxt *bp)
7614{
7615 int rc = 0;
7616 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007617
Michael Chan170ce012016-04-05 14:08:57 -04007618 rc = bnxt_hwrm_phy_qcaps(bp);
7619 if (rc) {
7620 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7621 rc);
7622 return rc;
7623 }
7624
Michael Chanc0c050c2015-10-22 16:01:17 -04007625 rc = bnxt_update_link(bp, false);
7626 if (rc) {
7627 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7628 rc);
7629 return rc;
7630 }
7631
Michael Chan93ed8112016-06-13 02:25:37 -04007632 /* Older firmware does not have supported_auto_speeds, so assume
7633 * that all supported speeds can be autonegotiated.
7634 */
7635 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7636 link_info->support_auto_speeds = link_info->support_speeds;
7637
Michael Chanc0c050c2015-10-22 16:01:17 -04007638 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007639 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007640 link_info->autoneg = BNXT_AUTONEG_SPEED;
7641 if (bp->hwrm_spec_code >= 0x10201) {
7642 if (link_info->auto_pause_setting &
7643 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7644 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7645 } else {
7646 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7647 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007648 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007649 } else {
7650 link_info->req_link_speed = link_info->force_link_speed;
7651 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007652 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007653 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7654 link_info->req_flow_ctrl =
7655 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7656 else
7657 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007658 return rc;
7659}
7660
7661static int bnxt_get_max_irq(struct pci_dev *pdev)
7662{
7663 u16 ctrl;
7664
7665 if (!pdev->msix_cap)
7666 return 1;
7667
7668 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7669 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7670}
7671
Michael Chan6e6c5a52016-01-02 23:45:02 -05007672static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7673 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007674{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007675 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007676
Michael Chan379a80a2015-10-23 15:06:19 -04007677#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007678 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007679 *max_tx = bp->vf.max_tx_rings;
7680 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007681 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7682 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007683 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007684 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007685#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007686 {
7687 *max_tx = bp->pf.max_tx_rings;
7688 *max_rx = bp->pf.max_rx_rings;
7689 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7690 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7691 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007692 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007693 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7694 *max_cp -= 1;
7695 *max_rx -= 2;
7696 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007697 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7698 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007699 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007700}
7701
7702int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7703{
7704 int rx, tx, cp;
7705
7706 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7707 if (!rx || !tx || !cp)
7708 return -ENOMEM;
7709
7710 *max_rx = rx;
7711 *max_tx = tx;
7712 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7713}
7714
Michael Chane4060d32016-12-07 00:26:19 -05007715static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7716 bool shared)
7717{
7718 int rc;
7719
7720 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007721 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7722 /* Not enough rings, try disabling agg rings. */
7723 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7724 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7725 if (rc)
7726 return rc;
7727 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7728 bp->dev->hw_features &= ~NETIF_F_LRO;
7729 bp->dev->features &= ~NETIF_F_LRO;
7730 bnxt_set_ring_params(bp);
7731 }
Michael Chane4060d32016-12-07 00:26:19 -05007732
7733 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7734 int max_cp, max_stat, max_irq;
7735
7736 /* Reserve minimum resources for RoCE */
7737 max_cp = bnxt_get_max_func_cp_rings(bp);
7738 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7739 max_irq = bnxt_get_max_func_irqs(bp);
7740 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7741 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7742 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7743 return 0;
7744
7745 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7746 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7747 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7748 max_cp = min_t(int, max_cp, max_irq);
7749 max_cp = min_t(int, max_cp, max_stat);
7750 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7751 if (rc)
7752 rc = 0;
7753 }
7754 return rc;
7755}
7756
Michael Chan702c2212017-05-29 19:06:10 -04007757static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007758{
7759 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007760
7761 if (sh)
7762 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7763 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007764 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007765 if (rc)
7766 return rc;
7767 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7768 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007769
7770 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7771 if (rc)
7772 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7773
Michael Chan6e6c5a52016-01-02 23:45:02 -05007774 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7775 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7776 bp->tx_nr_rings + bp->rx_nr_rings;
7777 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007778 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7779 bp->rx_nr_rings++;
7780 bp->cp_nr_rings++;
7781 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007782 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007783}
7784
Michael Chan7b08f662016-12-07 00:26:18 -05007785void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7786{
7787 ASSERT_RTNL();
7788 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007789 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007790}
7791
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007792static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7793{
7794 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7795 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7796
7797 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7798 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7799 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7800 else
7801 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7802 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7803 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7804 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7805 "Unknown", width);
7806}
7807
Michael Chanc0c050c2015-10-22 16:01:17 -04007808static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7809{
7810 static int version_printed;
7811 struct net_device *dev;
7812 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007813 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007814
Ray Jui4e003382017-02-20 19:25:16 -05007815 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007816 return -ENODEV;
7817
Michael Chanc0c050c2015-10-22 16:01:17 -04007818 if (version_printed++ == 0)
7819 pr_info("%s", version);
7820
7821 max_irqs = bnxt_get_max_irq(pdev);
7822 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7823 if (!dev)
7824 return -ENOMEM;
7825
7826 bp = netdev_priv(dev);
7827
7828 if (bnxt_vf_pciid(ent->driver_data))
7829 bp->flags |= BNXT_FLAG_VF;
7830
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007831 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007832 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007833
7834 rc = bnxt_init_board(pdev, dev);
7835 if (rc < 0)
7836 goto init_err_free;
7837
7838 dev->netdev_ops = &bnxt_netdev_ops;
7839 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7840 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007841 pci_set_drvdata(pdev, dev);
7842
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007843 rc = bnxt_alloc_hwrm_resources(bp);
7844 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007845 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007846
7847 mutex_init(&bp->hwrm_cmd_lock);
7848 rc = bnxt_hwrm_ver_get(bp);
7849 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007850 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007851
Deepak Khungare605db82017-05-29 19:06:04 -04007852 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7853 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7854 if (rc)
7855 goto init_err_pci_clean;
7856 }
7857
Michael Chan3c2217a2017-03-08 18:44:32 -05007858 rc = bnxt_hwrm_func_reset(bp);
7859 if (rc)
7860 goto init_err_pci_clean;
7861
Rob Swindell5ac67d82016-09-19 03:58:03 -04007862 bnxt_hwrm_fw_set_time(bp);
7863
Michael Chanc0c050c2015-10-22 16:01:17 -04007864 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7865 NETIF_F_TSO | NETIF_F_TSO6 |
7866 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007867 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007868 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7869 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007870 NETIF_F_RXCSUM | NETIF_F_GRO;
7871
7872 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7873 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007874
Michael Chanc0c050c2015-10-22 16:01:17 -04007875 dev->hw_enc_features =
7876 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7877 NETIF_F_TSO | NETIF_F_TSO6 |
7878 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007879 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007880 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007881 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7882 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007883 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7884 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7885 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7886 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7887 dev->priv_flags |= IFF_UNICAST_FLT;
7888
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007889 /* MTU range: 60 - 9500 */
7890 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007891 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007892
Michael Chanc0c050c2015-10-22 16:01:17 -04007893#ifdef CONFIG_BNXT_SRIOV
7894 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007895 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04007896#endif
Michael Chan309369c2016-06-13 02:25:34 -04007897 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007898 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007899 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007900 else
7901 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007902
Michael Chanc0c050c2015-10-22 16:01:17 -04007903 rc = bnxt_hwrm_func_drv_rgtr(bp);
7904 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007905 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007906
Michael Chana1653b12016-12-07 00:26:20 -05007907 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7908 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007909 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007910
Michael Chana588e452016-12-07 00:26:21 -05007911 bp->ulp_probe = bnxt_ulp_probe;
7912
Michael Chanc0c050c2015-10-22 16:01:17 -04007913 /* Get the MAX capabilities for this function */
7914 rc = bnxt_hwrm_func_qcaps(bp);
7915 if (rc) {
7916 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7917 rc);
7918 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007919 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007920 }
7921
7922 rc = bnxt_hwrm_queue_qportcfg(bp);
7923 if (rc) {
7924 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7925 rc);
7926 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007927 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007928 }
7929
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007930 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007931 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007932 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007933 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007934
Michael Chanc61fb992017-02-06 16:55:36 -05007935 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007936 bnxt_set_tpa_flags(bp);
7937 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007938 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007939 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007940 if (rc) {
7941 netdev_err(bp->dev, "Not enough rings available.\n");
7942 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007943 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007944 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007945
Michael Chan87da7f72016-11-16 21:13:09 -05007946 /* Default RSS hash cfg. */
7947 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7948 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7949 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7950 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007951 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007952 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7953 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7954 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7955 }
7956
Michael Chan8fdefd62016-12-29 12:13:36 -05007957 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007958 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007959 dev->hw_features |= NETIF_F_NTUPLE;
7960 if (bnxt_rfs_capable(bp)) {
7961 bp->flags |= BNXT_FLAG_RFS;
7962 dev->features |= NETIF_F_NTUPLE;
7963 }
7964 }
7965
Michael Chanc0c050c2015-10-22 16:01:17 -04007966 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7967 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7968
7969 rc = bnxt_probe_phy(bp);
7970 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007971 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007972
Michael Chan78095922016-12-07 00:26:16 -05007973 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007974 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007975 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007976
Michael Chanc1ef1462017-04-04 18:14:07 -04007977 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007978 if (bp->flags & BNXT_FLAG_WOL_CAP)
7979 device_set_wakeup_enable(&pdev->dev, bp->wol);
7980 else
7981 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007982
Michael Chan78095922016-12-07 00:26:16 -05007983 rc = register_netdev(dev);
7984 if (rc)
7985 goto init_err_clr_int;
7986
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007987 if (BNXT_PF(bp))
7988 bnxt_dl_register(bp);
7989
Michael Chanc0c050c2015-10-22 16:01:17 -04007990 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7991 board_info[ent->driver_data].name,
7992 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7993
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007994 bnxt_parse_log_pcie_link(bp);
7995
Michael Chanc0c050c2015-10-22 16:01:17 -04007996 return 0;
7997
Michael Chan78095922016-12-07 00:26:16 -05007998init_err_clr_int:
7999 bnxt_clear_int_mode(bp);
8000
Sathya Perla17086392017-02-20 19:25:18 -05008001init_err_pci_clean:
8002 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008003
8004init_err_free:
8005 free_netdev(dev);
8006 return rc;
8007}
8008
Michael Chand196ece2017-04-04 18:14:08 -04008009static void bnxt_shutdown(struct pci_dev *pdev)
8010{
8011 struct net_device *dev = pci_get_drvdata(pdev);
8012 struct bnxt *bp;
8013
8014 if (!dev)
8015 return;
8016
8017 rtnl_lock();
8018 bp = netdev_priv(dev);
8019 if (!bp)
8020 goto shutdown_exit;
8021
8022 if (netif_running(dev))
8023 dev_close(dev);
8024
8025 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04008026 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008027 bnxt_clear_int_mode(bp);
8028 pci_wake_from_d3(pdev, bp->wol);
8029 pci_set_power_state(pdev, PCI_D3hot);
8030 }
8031
8032shutdown_exit:
8033 rtnl_unlock();
8034}
8035
Michael Chanf65a2042017-04-04 18:14:11 -04008036#ifdef CONFIG_PM_SLEEP
8037static int bnxt_suspend(struct device *device)
8038{
8039 struct pci_dev *pdev = to_pci_dev(device);
8040 struct net_device *dev = pci_get_drvdata(pdev);
8041 struct bnxt *bp = netdev_priv(dev);
8042 int rc = 0;
8043
8044 rtnl_lock();
8045 if (netif_running(dev)) {
8046 netif_device_detach(dev);
8047 rc = bnxt_close(dev);
8048 }
8049 bnxt_hwrm_func_drv_unrgtr(bp);
8050 rtnl_unlock();
8051 return rc;
8052}
8053
8054static int bnxt_resume(struct device *device)
8055{
8056 struct pci_dev *pdev = to_pci_dev(device);
8057 struct net_device *dev = pci_get_drvdata(pdev);
8058 struct bnxt *bp = netdev_priv(dev);
8059 int rc = 0;
8060
8061 rtnl_lock();
8062 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8063 rc = -ENODEV;
8064 goto resume_exit;
8065 }
8066 rc = bnxt_hwrm_func_reset(bp);
8067 if (rc) {
8068 rc = -EBUSY;
8069 goto resume_exit;
8070 }
8071 bnxt_get_wol_settings(bp);
8072 if (netif_running(dev)) {
8073 rc = bnxt_open(dev);
8074 if (!rc)
8075 netif_device_attach(dev);
8076 }
8077
8078resume_exit:
8079 rtnl_unlock();
8080 return rc;
8081}
8082
8083static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8084#define BNXT_PM_OPS (&bnxt_pm_ops)
8085
8086#else
8087
8088#define BNXT_PM_OPS NULL
8089
8090#endif /* CONFIG_PM_SLEEP */
8091
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008092/**
8093 * bnxt_io_error_detected - called when PCI error is detected
8094 * @pdev: Pointer to PCI device
8095 * @state: The current pci connection state
8096 *
8097 * This function is called after a PCI bus error affecting
8098 * this device has been detected.
8099 */
8100static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8101 pci_channel_state_t state)
8102{
8103 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008104 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008105
8106 netdev_info(netdev, "PCI I/O error detected\n");
8107
8108 rtnl_lock();
8109 netif_device_detach(netdev);
8110
Michael Chana588e452016-12-07 00:26:21 -05008111 bnxt_ulp_stop(bp);
8112
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008113 if (state == pci_channel_io_perm_failure) {
8114 rtnl_unlock();
8115 return PCI_ERS_RESULT_DISCONNECT;
8116 }
8117
8118 if (netif_running(netdev))
8119 bnxt_close(netdev);
8120
8121 pci_disable_device(pdev);
8122 rtnl_unlock();
8123
8124 /* Request a slot slot reset. */
8125 return PCI_ERS_RESULT_NEED_RESET;
8126}
8127
8128/**
8129 * bnxt_io_slot_reset - called after the pci bus has been reset.
8130 * @pdev: Pointer to PCI device
8131 *
8132 * Restart the card from scratch, as if from a cold-boot.
8133 * At this point, the card has exprienced a hard reset,
8134 * followed by fixups by BIOS, and has its config space
8135 * set up identically to what it was at cold boot.
8136 */
8137static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8138{
8139 struct net_device *netdev = pci_get_drvdata(pdev);
8140 struct bnxt *bp = netdev_priv(netdev);
8141 int err = 0;
8142 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8143
8144 netdev_info(bp->dev, "PCI Slot Reset\n");
8145
8146 rtnl_lock();
8147
8148 if (pci_enable_device(pdev)) {
8149 dev_err(&pdev->dev,
8150 "Cannot re-enable PCI device after reset.\n");
8151 } else {
8152 pci_set_master(pdev);
8153
Michael Chanaa8ed022016-12-07 00:26:17 -05008154 err = bnxt_hwrm_func_reset(bp);
8155 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008156 err = bnxt_open(netdev);
8157
Michael Chana588e452016-12-07 00:26:21 -05008158 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008159 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008160 bnxt_ulp_start(bp);
8161 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008162 }
8163
8164 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8165 dev_close(netdev);
8166
8167 rtnl_unlock();
8168
8169 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8170 if (err) {
8171 dev_err(&pdev->dev,
8172 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8173 err); /* non-fatal, continue */
8174 }
8175
8176 return PCI_ERS_RESULT_RECOVERED;
8177}
8178
8179/**
8180 * bnxt_io_resume - called when traffic can start flowing again.
8181 * @pdev: Pointer to PCI device
8182 *
8183 * This callback is called when the error recovery driver tells
8184 * us that its OK to resume normal operation.
8185 */
8186static void bnxt_io_resume(struct pci_dev *pdev)
8187{
8188 struct net_device *netdev = pci_get_drvdata(pdev);
8189
8190 rtnl_lock();
8191
8192 netif_device_attach(netdev);
8193
8194 rtnl_unlock();
8195}
8196
8197static const struct pci_error_handlers bnxt_err_handler = {
8198 .error_detected = bnxt_io_error_detected,
8199 .slot_reset = bnxt_io_slot_reset,
8200 .resume = bnxt_io_resume
8201};
8202
Michael Chanc0c050c2015-10-22 16:01:17 -04008203static struct pci_driver bnxt_pci_driver = {
8204 .name = DRV_MODULE_NAME,
8205 .id_table = bnxt_pci_tbl,
8206 .probe = bnxt_init_one,
8207 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008208 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008209 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008210 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008211#if defined(CONFIG_BNXT_SRIOV)
8212 .sriov_configure = bnxt_sriov_configure,
8213#endif
8214};
8215
8216module_pci_driver(bnxt_pci_driver);