blob: a78f72a530427c445a0c5369ce7e9178bb4805c9 [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;
Michael Chanc0c050c2015-10-22 16:01:17 -04005631
Michael Chanb6ab4b02016-01-02 23:44:59 -05005632 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005633 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005634 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005635 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005636 }
5637 }
5638 /* Stop all TX queues */
5639 netif_tx_disable(bp->dev);
5640 netif_carrier_off(bp->dev);
5641}
5642
Michael Chan7df4ae92016-12-02 21:17:17 -05005643void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005644{
5645 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005646 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04005647
5648 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005649 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005650 txr->dev_state = 0;
5651 }
5652 netif_tx_wake_all_queues(bp->dev);
5653 if (bp->link_info.link_up)
5654 netif_carrier_on(bp->dev);
5655}
5656
5657static void bnxt_report_link(struct bnxt *bp)
5658{
5659 if (bp->link_info.link_up) {
5660 const char *duplex;
5661 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005662 u32 speed;
5663 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005664
5665 netif_carrier_on(bp->dev);
5666 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5667 duplex = "full";
5668 else
5669 duplex = "half";
5670 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5671 flow_ctrl = "ON - receive & transmit";
5672 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5673 flow_ctrl = "ON - transmit";
5674 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5675 flow_ctrl = "ON - receive";
5676 else
5677 flow_ctrl = "none";
5678 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005679 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005680 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005681 if (bp->flags & BNXT_FLAG_EEE_CAP)
5682 netdev_info(bp->dev, "EEE is %s\n",
5683 bp->eee.eee_active ? "active" :
5684 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005685 fec = bp->link_info.fec_cfg;
5686 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5687 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5688 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5689 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5690 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005691 } else {
5692 netif_carrier_off(bp->dev);
5693 netdev_err(bp->dev, "NIC Link is Down\n");
5694 }
5695}
5696
Michael Chan170ce012016-04-05 14:08:57 -04005697static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5698{
5699 int rc = 0;
5700 struct hwrm_port_phy_qcaps_input req = {0};
5701 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005702 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005703
5704 if (bp->hwrm_spec_code < 0x10201)
5705 return 0;
5706
5707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5708
5709 mutex_lock(&bp->hwrm_cmd_lock);
5710 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5711 if (rc)
5712 goto hwrm_phy_qcaps_exit;
5713
Michael Chanacb20052017-07-24 12:34:20 -04005714 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005715 struct ethtool_eee *eee = &bp->eee;
5716 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5717
5718 bp->flags |= BNXT_FLAG_EEE_CAP;
5719 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5720 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5721 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5722 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5723 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5724 }
Michael Chan520ad892017-03-08 18:44:35 -05005725 if (resp->supported_speeds_auto_mode)
5726 link_info->support_auto_speeds =
5727 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005728
5729hwrm_phy_qcaps_exit:
5730 mutex_unlock(&bp->hwrm_cmd_lock);
5731 return rc;
5732}
5733
Michael Chanc0c050c2015-10-22 16:01:17 -04005734static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5735{
5736 int rc = 0;
5737 struct bnxt_link_info *link_info = &bp->link_info;
5738 struct hwrm_port_phy_qcfg_input req = {0};
5739 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5740 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005741 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005742
5743 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5744
5745 mutex_lock(&bp->hwrm_cmd_lock);
5746 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5747 if (rc) {
5748 mutex_unlock(&bp->hwrm_cmd_lock);
5749 return rc;
5750 }
5751
5752 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5753 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005754 link_info->duplex = resp->duplex_cfg;
5755 if (bp->hwrm_spec_code >= 0x10800)
5756 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005757 link_info->pause = resp->pause;
5758 link_info->auto_mode = resp->auto_mode;
5759 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005760 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005761 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005762 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005763 if (link_info->phy_link_status == BNXT_LINK_LINK)
5764 link_info->link_speed = le16_to_cpu(resp->link_speed);
5765 else
5766 link_info->link_speed = 0;
5767 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005768 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5769 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005770 link_info->lp_auto_link_speeds =
5771 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005772 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5773 link_info->phy_ver[0] = resp->phy_maj;
5774 link_info->phy_ver[1] = resp->phy_min;
5775 link_info->phy_ver[2] = resp->phy_bld;
5776 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005777 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005778 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005779 link_info->phy_addr = resp->eee_config_phy_addr &
5780 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005781 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005782
Michael Chan170ce012016-04-05 14:08:57 -04005783 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5784 struct ethtool_eee *eee = &bp->eee;
5785 u16 fw_speeds;
5786
5787 eee->eee_active = 0;
5788 if (resp->eee_config_phy_addr &
5789 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5790 eee->eee_active = 1;
5791 fw_speeds = le16_to_cpu(
5792 resp->link_partner_adv_eee_link_speed_mask);
5793 eee->lp_advertised =
5794 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5795 }
5796
5797 /* Pull initial EEE config */
5798 if (!chng_link_state) {
5799 if (resp->eee_config_phy_addr &
5800 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5801 eee->eee_enabled = 1;
5802
5803 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5804 eee->advertised =
5805 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5806
5807 if (resp->eee_config_phy_addr &
5808 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5809 __le32 tmr;
5810
5811 eee->tx_lpi_enabled = 1;
5812 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5813 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5814 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5815 }
5816 }
5817 }
Michael Chane70c7522017-02-12 19:18:16 -05005818
5819 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5820 if (bp->hwrm_spec_code >= 0x10504)
5821 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5822
Michael Chanc0c050c2015-10-22 16:01:17 -04005823 /* TODO: need to add more logic to report VF link */
5824 if (chng_link_state) {
5825 if (link_info->phy_link_status == BNXT_LINK_LINK)
5826 link_info->link_up = 1;
5827 else
5828 link_info->link_up = 0;
5829 if (link_up != link_info->link_up)
5830 bnxt_report_link(bp);
5831 } else {
5832 /* alwasy link down if not require to update link state */
5833 link_info->link_up = 0;
5834 }
5835 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005836
5837 diff = link_info->support_auto_speeds ^ link_info->advertising;
5838 if ((link_info->support_auto_speeds | diff) !=
5839 link_info->support_auto_speeds) {
5840 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005841 * update the advertisement settings. Caller holds RTNL
5842 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005843 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005844 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005845 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005846 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005847 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005848 return 0;
5849}
5850
Michael Chan10289be2016-05-15 03:04:49 -04005851static void bnxt_get_port_module_status(struct bnxt *bp)
5852{
5853 struct bnxt_link_info *link_info = &bp->link_info;
5854 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5855 u8 module_status;
5856
5857 if (bnxt_update_link(bp, true))
5858 return;
5859
5860 module_status = link_info->module_status;
5861 switch (module_status) {
5862 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5863 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5864 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5865 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5866 bp->pf.port_id);
5867 if (bp->hwrm_spec_code >= 0x10201) {
5868 netdev_warn(bp->dev, "Module part number %s\n",
5869 resp->phy_vendor_partnumber);
5870 }
5871 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5872 netdev_warn(bp->dev, "TX is disabled\n");
5873 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5874 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5875 }
5876}
5877
Michael Chanc0c050c2015-10-22 16:01:17 -04005878static void
5879bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5880{
5881 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005882 if (bp->hwrm_spec_code >= 0x10201)
5883 req->auto_pause =
5884 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005885 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5886 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5887 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005888 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005889 req->enables |=
5890 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5891 } else {
5892 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5893 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5894 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5895 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5896 req->enables |=
5897 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005898 if (bp->hwrm_spec_code >= 0x10201) {
5899 req->auto_pause = req->force_pause;
5900 req->enables |= cpu_to_le32(
5901 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5902 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005903 }
5904}
5905
5906static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5907 struct hwrm_port_phy_cfg_input *req)
5908{
5909 u8 autoneg = bp->link_info.autoneg;
5910 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005911 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005912
5913 if (autoneg & BNXT_AUTONEG_SPEED) {
5914 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005915 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005916
5917 req->enables |= cpu_to_le32(
5918 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5919 req->auto_link_speed_mask = cpu_to_le16(advertising);
5920
5921 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5922 req->flags |=
5923 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5924 } else {
5925 req->force_link_speed = cpu_to_le16(fw_link_speed);
5926 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5927 }
5928
Michael Chanc0c050c2015-10-22 16:01:17 -04005929 /* tell chimp that the setting takes effect immediately */
5930 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5931}
5932
5933int bnxt_hwrm_set_pause(struct bnxt *bp)
5934{
5935 struct hwrm_port_phy_cfg_input req = {0};
5936 int rc;
5937
5938 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5939 bnxt_hwrm_set_pause_common(bp, &req);
5940
5941 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5942 bp->link_info.force_link_chng)
5943 bnxt_hwrm_set_link_common(bp, &req);
5944
5945 mutex_lock(&bp->hwrm_cmd_lock);
5946 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5947 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5948 /* since changing of pause setting doesn't trigger any link
5949 * change event, the driver needs to update the current pause
5950 * result upon successfully return of the phy_cfg command
5951 */
5952 bp->link_info.pause =
5953 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5954 bp->link_info.auto_pause_setting = 0;
5955 if (!bp->link_info.force_link_chng)
5956 bnxt_report_link(bp);
5957 }
5958 bp->link_info.force_link_chng = false;
5959 mutex_unlock(&bp->hwrm_cmd_lock);
5960 return rc;
5961}
5962
Michael Chan939f7f02016-04-05 14:08:58 -04005963static void bnxt_hwrm_set_eee(struct bnxt *bp,
5964 struct hwrm_port_phy_cfg_input *req)
5965{
5966 struct ethtool_eee *eee = &bp->eee;
5967
5968 if (eee->eee_enabled) {
5969 u16 eee_speeds;
5970 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5971
5972 if (eee->tx_lpi_enabled)
5973 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5974 else
5975 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5976
5977 req->flags |= cpu_to_le32(flags);
5978 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5979 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5980 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5981 } else {
5982 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5983 }
5984}
5985
5986int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005987{
5988 struct hwrm_port_phy_cfg_input req = {0};
5989
5990 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5991 if (set_pause)
5992 bnxt_hwrm_set_pause_common(bp, &req);
5993
5994 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005995
5996 if (set_eee)
5997 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005998 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5999}
6000
Michael Chan33f7d552016-04-11 04:11:12 -04006001static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6002{
6003 struct hwrm_port_phy_cfg_input req = {0};
6004
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006005 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04006006 return 0;
6007
6008 if (pci_num_vf(bp->pdev))
6009 return 0;
6010
6011 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05006012 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04006013 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6014}
6015
Michael Chan5ad2cbe2017-01-13 01:32:03 -05006016static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6017{
6018 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6019 struct hwrm_port_led_qcaps_input req = {0};
6020 struct bnxt_pf_info *pf = &bp->pf;
6021 int rc;
6022
6023 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6024 return 0;
6025
6026 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6027 req.port_id = cpu_to_le16(pf->port_id);
6028 mutex_lock(&bp->hwrm_cmd_lock);
6029 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6030 if (rc) {
6031 mutex_unlock(&bp->hwrm_cmd_lock);
6032 return rc;
6033 }
6034 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6035 int i;
6036
6037 bp->num_leds = resp->num_leds;
6038 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6039 bp->num_leds);
6040 for (i = 0; i < bp->num_leds; i++) {
6041 struct bnxt_led_info *led = &bp->leds[i];
6042 __le16 caps = led->led_state_caps;
6043
6044 if (!led->led_group_id ||
6045 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6046 bp->num_leds = 0;
6047 break;
6048 }
6049 }
6050 }
6051 mutex_unlock(&bp->hwrm_cmd_lock);
6052 return 0;
6053}
6054
Michael Chan5282db62017-04-04 18:14:10 -04006055int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6056{
6057 struct hwrm_wol_filter_alloc_input req = {0};
6058 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6059 int rc;
6060
6061 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6062 req.port_id = cpu_to_le16(bp->pf.port_id);
6063 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6064 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6065 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6066 mutex_lock(&bp->hwrm_cmd_lock);
6067 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6068 if (!rc)
6069 bp->wol_filter_id = resp->wol_filter_id;
6070 mutex_unlock(&bp->hwrm_cmd_lock);
6071 return rc;
6072}
6073
6074int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6075{
6076 struct hwrm_wol_filter_free_input req = {0};
6077 int rc;
6078
6079 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6080 req.port_id = cpu_to_le16(bp->pf.port_id);
6081 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6082 req.wol_filter_id = bp->wol_filter_id;
6083 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6084 return rc;
6085}
6086
Michael Chanc1ef1462017-04-04 18:14:07 -04006087static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6088{
6089 struct hwrm_wol_filter_qcfg_input req = {0};
6090 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6091 u16 next_handle = 0;
6092 int rc;
6093
6094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6095 req.port_id = cpu_to_le16(bp->pf.port_id);
6096 req.handle = cpu_to_le16(handle);
6097 mutex_lock(&bp->hwrm_cmd_lock);
6098 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6099 if (!rc) {
6100 next_handle = le16_to_cpu(resp->next_handle);
6101 if (next_handle != 0) {
6102 if (resp->wol_type ==
6103 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6104 bp->wol = 1;
6105 bp->wol_filter_id = resp->wol_filter_id;
6106 }
6107 }
6108 }
6109 mutex_unlock(&bp->hwrm_cmd_lock);
6110 return next_handle;
6111}
6112
6113static void bnxt_get_wol_settings(struct bnxt *bp)
6114{
6115 u16 handle = 0;
6116
6117 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6118 return;
6119
6120 do {
6121 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6122 } while (handle && handle != 0xffff);
6123}
6124
Michael Chan939f7f02016-04-05 14:08:58 -04006125static bool bnxt_eee_config_ok(struct bnxt *bp)
6126{
6127 struct ethtool_eee *eee = &bp->eee;
6128 struct bnxt_link_info *link_info = &bp->link_info;
6129
6130 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6131 return true;
6132
6133 if (eee->eee_enabled) {
6134 u32 advertising =
6135 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6136
6137 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6138 eee->eee_enabled = 0;
6139 return false;
6140 }
6141 if (eee->advertised & ~advertising) {
6142 eee->advertised = advertising & eee->supported;
6143 return false;
6144 }
6145 }
6146 return true;
6147}
6148
Michael Chanc0c050c2015-10-22 16:01:17 -04006149static int bnxt_update_phy_setting(struct bnxt *bp)
6150{
6151 int rc;
6152 bool update_link = false;
6153 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006154 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006155 struct bnxt_link_info *link_info = &bp->link_info;
6156
6157 rc = bnxt_update_link(bp, true);
6158 if (rc) {
6159 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6160 rc);
6161 return rc;
6162 }
Michael Chan33dac242017-02-12 19:18:15 -05006163 if (!BNXT_SINGLE_PF(bp))
6164 return 0;
6165
Michael Chanc0c050c2015-10-22 16:01:17 -04006166 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006167 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6168 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006169 update_pause = true;
6170 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6171 link_info->force_pause_setting != link_info->req_flow_ctrl)
6172 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006173 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6174 if (BNXT_AUTO_MODE(link_info->auto_mode))
6175 update_link = true;
6176 if (link_info->req_link_speed != link_info->force_link_speed)
6177 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006178 if (link_info->req_duplex != link_info->duplex_setting)
6179 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006180 } else {
6181 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6182 update_link = true;
6183 if (link_info->advertising != link_info->auto_link_speeds)
6184 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006185 }
6186
Michael Chan16d663a2016-11-16 21:13:07 -05006187 /* The last close may have shutdown the link, so need to call
6188 * PHY_CFG to bring it back up.
6189 */
6190 if (!netif_carrier_ok(bp->dev))
6191 update_link = true;
6192
Michael Chan939f7f02016-04-05 14:08:58 -04006193 if (!bnxt_eee_config_ok(bp))
6194 update_eee = true;
6195
Michael Chanc0c050c2015-10-22 16:01:17 -04006196 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006197 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006198 else if (update_pause)
6199 rc = bnxt_hwrm_set_pause(bp);
6200 if (rc) {
6201 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6202 rc);
6203 return rc;
6204 }
6205
6206 return rc;
6207}
6208
Jeffrey Huang11809492015-11-05 16:25:49 -05006209/* Common routine to pre-map certain register block to different GRC window.
6210 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6211 * in PF and 3 windows in VF that can be customized to map in different
6212 * register blocks.
6213 */
6214static void bnxt_preset_reg_win(struct bnxt *bp)
6215{
6216 if (BNXT_PF(bp)) {
6217 /* CAG registers map to GRC window #4 */
6218 writel(BNXT_CAG_REG_BASE,
6219 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6220 }
6221}
6222
Michael Chanc0c050c2015-10-22 16:01:17 -04006223static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6224{
6225 int rc = 0;
6226
Jeffrey Huang11809492015-11-05 16:25:49 -05006227 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006228 netif_carrier_off(bp->dev);
6229 if (irq_re_init) {
6230 rc = bnxt_setup_int_mode(bp);
6231 if (rc) {
6232 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6233 rc);
6234 return rc;
6235 }
6236 }
6237 if ((bp->flags & BNXT_FLAG_RFS) &&
6238 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6239 /* disable RFS if falling back to INTA */
6240 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6241 bp->flags &= ~BNXT_FLAG_RFS;
6242 }
6243
6244 rc = bnxt_alloc_mem(bp, irq_re_init);
6245 if (rc) {
6246 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6247 goto open_err_free_mem;
6248 }
6249
6250 if (irq_re_init) {
6251 bnxt_init_napi(bp);
6252 rc = bnxt_request_irq(bp);
6253 if (rc) {
6254 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6255 goto open_err;
6256 }
6257 }
6258
6259 bnxt_enable_napi(bp);
6260
6261 rc = bnxt_init_nic(bp, irq_re_init);
6262 if (rc) {
6263 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6264 goto open_err;
6265 }
6266
6267 if (link_re_init) {
6268 rc = bnxt_update_phy_setting(bp);
6269 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006270 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006271 }
6272
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006273 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006274 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006275
Michael Chancaefe522015-12-09 19:35:42 -05006276 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006277 bnxt_enable_int(bp);
6278 /* Enable TX queues */
6279 bnxt_tx_enable(bp);
6280 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006281 /* Poll link status and check for SFP+ module status */
6282 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006283
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006284 /* VF-reps may need to be re-opened after the PF is re-opened */
6285 if (BNXT_PF(bp))
6286 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006287 return 0;
6288
6289open_err:
6290 bnxt_disable_napi(bp);
6291 bnxt_del_napi(bp);
6292
6293open_err_free_mem:
6294 bnxt_free_skbs(bp);
6295 bnxt_free_irq(bp);
6296 bnxt_free_mem(bp, true);
6297 return rc;
6298}
6299
6300/* rtnl_lock held */
6301int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6302{
6303 int rc = 0;
6304
6305 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6306 if (rc) {
6307 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6308 dev_close(bp->dev);
6309 }
6310 return rc;
6311}
6312
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006313/* rtnl_lock held, open the NIC half way by allocating all resources, but
6314 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6315 * self tests.
6316 */
6317int bnxt_half_open_nic(struct bnxt *bp)
6318{
6319 int rc = 0;
6320
6321 rc = bnxt_alloc_mem(bp, false);
6322 if (rc) {
6323 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6324 goto half_open_err;
6325 }
6326 rc = bnxt_init_nic(bp, false);
6327 if (rc) {
6328 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6329 goto half_open_err;
6330 }
6331 return 0;
6332
6333half_open_err:
6334 bnxt_free_skbs(bp);
6335 bnxt_free_mem(bp, false);
6336 dev_close(bp->dev);
6337 return rc;
6338}
6339
6340/* rtnl_lock held, this call can only be made after a previous successful
6341 * call to bnxt_half_open_nic().
6342 */
6343void bnxt_half_close_nic(struct bnxt *bp)
6344{
6345 bnxt_hwrm_resource_free(bp, false, false);
6346 bnxt_free_skbs(bp);
6347 bnxt_free_mem(bp, false);
6348}
6349
Michael Chanc0c050c2015-10-22 16:01:17 -04006350static int bnxt_open(struct net_device *dev)
6351{
6352 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006353
Michael Chanc0c050c2015-10-22 16:01:17 -04006354 return __bnxt_open_nic(bp, true, true);
6355}
6356
Michael Chanf9b76eb2017-07-11 13:05:34 -04006357static bool bnxt_drv_busy(struct bnxt *bp)
6358{
6359 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6360 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6361}
6362
Michael Chanc0c050c2015-10-22 16:01:17 -04006363int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6364{
6365 int rc = 0;
6366
6367#ifdef CONFIG_BNXT_SRIOV
6368 if (bp->sriov_cfg) {
6369 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6370 !bp->sriov_cfg,
6371 BNXT_SRIOV_CFG_WAIT_TMO);
6372 if (rc)
6373 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6374 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006375
6376 /* Close the VF-reps before closing PF */
6377 if (BNXT_PF(bp))
6378 bnxt_vf_reps_close(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006379#endif
6380 /* Change device state to avoid TX queue wake up's */
6381 bnxt_tx_disable(bp);
6382
Michael Chancaefe522015-12-09 19:35:42 -05006383 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006384 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006385 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006386 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006387
Michael Chan9d8bc092016-12-29 12:13:33 -05006388 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006389 bnxt_shutdown_nic(bp, irq_re_init);
6390
6391 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6392
6393 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006394 del_timer_sync(&bp->timer);
6395 bnxt_free_skbs(bp);
6396
6397 if (irq_re_init) {
6398 bnxt_free_irq(bp);
6399 bnxt_del_napi(bp);
6400 }
6401 bnxt_free_mem(bp, irq_re_init);
6402 return rc;
6403}
6404
6405static int bnxt_close(struct net_device *dev)
6406{
6407 struct bnxt *bp = netdev_priv(dev);
6408
6409 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006410 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006411 return 0;
6412}
6413
6414/* rtnl_lock held */
6415static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6416{
6417 switch (cmd) {
6418 case SIOCGMIIPHY:
6419 /* fallthru */
6420 case SIOCGMIIREG: {
6421 if (!netif_running(dev))
6422 return -EAGAIN;
6423
6424 return 0;
6425 }
6426
6427 case SIOCSMIIREG:
6428 if (!netif_running(dev))
6429 return -EAGAIN;
6430
6431 return 0;
6432
6433 default:
6434 /* do nothing */
6435 break;
6436 }
6437 return -EOPNOTSUPP;
6438}
6439
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006440static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006441bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6442{
6443 u32 i;
6444 struct bnxt *bp = netdev_priv(dev);
6445
Michael Chanf9b76eb2017-07-11 13:05:34 -04006446 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6447 /* Make sure bnxt_close_nic() sees that we are reading stats before
6448 * we check the BNXT_STATE_OPEN flag.
6449 */
6450 smp_mb__after_atomic();
6451 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6452 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006453 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006454 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006455
6456 /* TODO check if we need to synchronize with bnxt_close path */
6457 for (i = 0; i < bp->cp_nr_rings; i++) {
6458 struct bnxt_napi *bnapi = bp->bnapi[i];
6459 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6460 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6461
6462 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6463 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6464 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6465
6466 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6467 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6468 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6469
6470 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6471 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6472 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6473
6474 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6475 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6476 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6477
6478 stats->rx_missed_errors +=
6479 le64_to_cpu(hw_stats->rx_discard_pkts);
6480
6481 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6482
Michael Chanc0c050c2015-10-22 16:01:17 -04006483 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6484 }
6485
Michael Chan9947f832016-03-07 15:38:46 -05006486 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6487 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6488 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6489
6490 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6491 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6492 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6493 le64_to_cpu(rx->rx_ovrsz_frames) +
6494 le64_to_cpu(rx->rx_runt_frames);
6495 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6496 le64_to_cpu(rx->rx_jbr_frames);
6497 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6498 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6499 stats->tx_errors = le64_to_cpu(tx->tx_err);
6500 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006501 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006502}
6503
6504static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6505{
6506 struct net_device *dev = bp->dev;
6507 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6508 struct netdev_hw_addr *ha;
6509 u8 *haddr;
6510 int mc_count = 0;
6511 bool update = false;
6512 int off = 0;
6513
6514 netdev_for_each_mc_addr(ha, dev) {
6515 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6516 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6517 vnic->mc_list_count = 0;
6518 return false;
6519 }
6520 haddr = ha->addr;
6521 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6522 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6523 update = true;
6524 }
6525 off += ETH_ALEN;
6526 mc_count++;
6527 }
6528 if (mc_count)
6529 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6530
6531 if (mc_count != vnic->mc_list_count) {
6532 vnic->mc_list_count = mc_count;
6533 update = true;
6534 }
6535 return update;
6536}
6537
6538static bool bnxt_uc_list_updated(struct bnxt *bp)
6539{
6540 struct net_device *dev = bp->dev;
6541 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6542 struct netdev_hw_addr *ha;
6543 int off = 0;
6544
6545 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6546 return true;
6547
6548 netdev_for_each_uc_addr(ha, dev) {
6549 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6550 return true;
6551
6552 off += ETH_ALEN;
6553 }
6554 return false;
6555}
6556
6557static void bnxt_set_rx_mode(struct net_device *dev)
6558{
6559 struct bnxt *bp = netdev_priv(dev);
6560 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6561 u32 mask = vnic->rx_mask;
6562 bool mc_update = false;
6563 bool uc_update;
6564
6565 if (!netif_running(dev))
6566 return;
6567
6568 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6569 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6570 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6571
Michael Chan17c71ac2016-07-01 18:46:27 -04006572 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006573 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6574
6575 uc_update = bnxt_uc_list_updated(bp);
6576
6577 if (dev->flags & IFF_ALLMULTI) {
6578 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6579 vnic->mc_list_count = 0;
6580 } else {
6581 mc_update = bnxt_mc_list_updated(bp, &mask);
6582 }
6583
6584 if (mask != vnic->rx_mask || uc_update || mc_update) {
6585 vnic->rx_mask = mask;
6586
6587 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6588 schedule_work(&bp->sp_task);
6589 }
6590}
6591
Michael Chanb664f002015-12-02 01:54:08 -05006592static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006593{
6594 struct net_device *dev = bp->dev;
6595 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6596 struct netdev_hw_addr *ha;
6597 int i, off = 0, rc;
6598 bool uc_update;
6599
6600 netif_addr_lock_bh(dev);
6601 uc_update = bnxt_uc_list_updated(bp);
6602 netif_addr_unlock_bh(dev);
6603
6604 if (!uc_update)
6605 goto skip_uc;
6606
6607 mutex_lock(&bp->hwrm_cmd_lock);
6608 for (i = 1; i < vnic->uc_filter_count; i++) {
6609 struct hwrm_cfa_l2_filter_free_input req = {0};
6610
6611 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6612 -1);
6613
6614 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6615
6616 rc = _hwrm_send_message(bp, &req, sizeof(req),
6617 HWRM_CMD_TIMEOUT);
6618 }
6619 mutex_unlock(&bp->hwrm_cmd_lock);
6620
6621 vnic->uc_filter_count = 1;
6622
6623 netif_addr_lock_bh(dev);
6624 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6625 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6626 } else {
6627 netdev_for_each_uc_addr(ha, dev) {
6628 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6629 off += ETH_ALEN;
6630 vnic->uc_filter_count++;
6631 }
6632 }
6633 netif_addr_unlock_bh(dev);
6634
6635 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6636 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6637 if (rc) {
6638 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6639 rc);
6640 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006641 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006642 }
6643 }
6644
6645skip_uc:
6646 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6647 if (rc)
6648 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6649 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006650
6651 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006652}
6653
Michael Chan8079e8f2016-12-29 12:13:37 -05006654/* If the chip and firmware supports RFS */
6655static bool bnxt_rfs_supported(struct bnxt *bp)
6656{
6657 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6658 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006659 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6660 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006661 return false;
6662}
6663
6664/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006665static bool bnxt_rfs_capable(struct bnxt *bp)
6666{
6667#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006668 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006669
Michael Chan964fd482017-02-12 19:18:13 -05006670 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006671 return false;
6672
6673 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006674 max_vnics = bnxt_get_max_func_vnics(bp);
6675 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006676
6677 /* RSS contexts not a limiting factor */
6678 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6679 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006680 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006681 netdev_warn(bp->dev,
6682 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006683 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006684 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006685 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006686
6687 return true;
6688#else
6689 return false;
6690#endif
6691}
6692
Michael Chanc0c050c2015-10-22 16:01:17 -04006693static netdev_features_t bnxt_fix_features(struct net_device *dev,
6694 netdev_features_t features)
6695{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006696 struct bnxt *bp = netdev_priv(dev);
6697
Vasundhara Volama2304902016-07-25 12:33:36 -04006698 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006699 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006700
6701 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6702 * turned on or off together.
6703 */
6704 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6705 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6706 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6707 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6708 NETIF_F_HW_VLAN_STAG_RX);
6709 else
6710 features |= NETIF_F_HW_VLAN_CTAG_RX |
6711 NETIF_F_HW_VLAN_STAG_RX;
6712 }
Michael Chancf6645f2016-06-13 02:25:28 -04006713#ifdef CONFIG_BNXT_SRIOV
6714 if (BNXT_VF(bp)) {
6715 if (bp->vf.vlan) {
6716 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6717 NETIF_F_HW_VLAN_STAG_RX);
6718 }
6719 }
6720#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006721 return features;
6722}
6723
6724static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6725{
6726 struct bnxt *bp = netdev_priv(dev);
6727 u32 flags = bp->flags;
6728 u32 changes;
6729 int rc = 0;
6730 bool re_init = false;
6731 bool update_tpa = false;
6732
6733 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006734 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006735 flags |= BNXT_FLAG_GRO;
6736 if (features & NETIF_F_LRO)
6737 flags |= BNXT_FLAG_LRO;
6738
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006739 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6740 flags &= ~BNXT_FLAG_TPA;
6741
Michael Chanc0c050c2015-10-22 16:01:17 -04006742 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6743 flags |= BNXT_FLAG_STRIP_VLAN;
6744
6745 if (features & NETIF_F_NTUPLE)
6746 flags |= BNXT_FLAG_RFS;
6747
6748 changes = flags ^ bp->flags;
6749 if (changes & BNXT_FLAG_TPA) {
6750 update_tpa = true;
6751 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6752 (flags & BNXT_FLAG_TPA) == 0)
6753 re_init = true;
6754 }
6755
6756 if (changes & ~BNXT_FLAG_TPA)
6757 re_init = true;
6758
6759 if (flags != bp->flags) {
6760 u32 old_flags = bp->flags;
6761
6762 bp->flags = flags;
6763
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006764 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006765 if (update_tpa)
6766 bnxt_set_ring_params(bp);
6767 return rc;
6768 }
6769
6770 if (re_init) {
6771 bnxt_close_nic(bp, false, false);
6772 if (update_tpa)
6773 bnxt_set_ring_params(bp);
6774
6775 return bnxt_open_nic(bp, false, false);
6776 }
6777 if (update_tpa) {
6778 rc = bnxt_set_tpa(bp,
6779 (flags & BNXT_FLAG_TPA) ?
6780 true : false);
6781 if (rc)
6782 bp->flags = old_flags;
6783 }
6784 }
6785 return rc;
6786}
6787
Michael Chan9f554592016-01-02 23:44:58 -05006788static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6789{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006790 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006791 int i = bnapi->index;
6792
Michael Chan3b2b7d92016-01-02 23:45:00 -05006793 if (!txr)
6794 return;
6795
Michael Chan9f554592016-01-02 23:44:58 -05006796 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6797 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6798 txr->tx_cons);
6799}
6800
6801static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6802{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006803 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006804 int i = bnapi->index;
6805
Michael Chan3b2b7d92016-01-02 23:45:00 -05006806 if (!rxr)
6807 return;
6808
Michael Chan9f554592016-01-02 23:44:58 -05006809 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",
6810 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6811 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6812 rxr->rx_sw_agg_prod);
6813}
6814
6815static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6816{
6817 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6818 int i = bnapi->index;
6819
6820 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6821 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6822}
6823
Michael Chanc0c050c2015-10-22 16:01:17 -04006824static void bnxt_dbg_dump_states(struct bnxt *bp)
6825{
6826 int i;
6827 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006828
6829 for (i = 0; i < bp->cp_nr_rings; i++) {
6830 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006831 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006832 bnxt_dump_tx_sw_state(bnapi);
6833 bnxt_dump_rx_sw_state(bnapi);
6834 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006835 }
6836 }
6837}
6838
Michael Chan6988bd92016-06-13 02:25:29 -04006839static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006840{
Michael Chan6988bd92016-06-13 02:25:29 -04006841 if (!silent)
6842 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006843 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006844 int rc;
6845
6846 if (!silent)
6847 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006848 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006849 rc = bnxt_open_nic(bp, false, false);
6850 if (!silent && !rc)
6851 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006852 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006853}
6854
6855static void bnxt_tx_timeout(struct net_device *dev)
6856{
6857 struct bnxt *bp = netdev_priv(dev);
6858
6859 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6860 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6861 schedule_work(&bp->sp_task);
6862}
6863
6864#ifdef CONFIG_NET_POLL_CONTROLLER
6865static void bnxt_poll_controller(struct net_device *dev)
6866{
6867 struct bnxt *bp = netdev_priv(dev);
6868 int i;
6869
Michael Chan2270bc52017-06-23 14:01:01 -04006870 /* Only process tx rings/combined rings in netpoll mode. */
6871 for (i = 0; i < bp->tx_nr_rings; i++) {
6872 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006873
Michael Chan2270bc52017-06-23 14:01:01 -04006874 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006875 }
6876}
6877#endif
6878
6879static void bnxt_timer(unsigned long data)
6880{
6881 struct bnxt *bp = (struct bnxt *)data;
6882 struct net_device *dev = bp->dev;
6883
6884 if (!netif_running(dev))
6885 return;
6886
6887 if (atomic_read(&bp->intr_sem) != 0)
6888 goto bnxt_restart_timer;
6889
Michael Chanadcc3312017-07-24 12:34:24 -04006890 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
6891 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05006892 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6893 schedule_work(&bp->sp_task);
6894 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006895bnxt_restart_timer:
6896 mod_timer(&bp->timer, jiffies + bp->current_interval);
6897}
6898
Michael Chana551ee92017-01-25 02:55:07 -05006899static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006900{
Michael Chana551ee92017-01-25 02:55:07 -05006901 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6902 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006903 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6904 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6905 */
6906 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6907 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006908}
6909
6910static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6911{
Michael Chan6988bd92016-06-13 02:25:29 -04006912 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6913 rtnl_unlock();
6914}
6915
Michael Chana551ee92017-01-25 02:55:07 -05006916/* Only called from bnxt_sp_task() */
6917static void bnxt_reset(struct bnxt *bp, bool silent)
6918{
6919 bnxt_rtnl_lock_sp(bp);
6920 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6921 bnxt_reset_task(bp, silent);
6922 bnxt_rtnl_unlock_sp(bp);
6923}
6924
Michael Chanc0c050c2015-10-22 16:01:17 -04006925static void bnxt_cfg_ntp_filters(struct bnxt *);
6926
6927static void bnxt_sp_task(struct work_struct *work)
6928{
6929 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006930
Michael Chan4cebdce2015-12-09 19:35:43 -05006931 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6932 smp_mb__after_atomic();
6933 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6934 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006935 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006936 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006937
6938 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6939 bnxt_cfg_rx_mode(bp);
6940
6941 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6942 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006943 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6944 bnxt_hwrm_exec_fwd_req(bp);
6945 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6946 bnxt_hwrm_tunnel_dst_port_alloc(
6947 bp, bp->vxlan_port,
6948 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6949 }
6950 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6951 bnxt_hwrm_tunnel_dst_port_free(
6952 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6953 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006954 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6955 bnxt_hwrm_tunnel_dst_port_alloc(
6956 bp, bp->nge_port,
6957 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6958 }
6959 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6960 bnxt_hwrm_tunnel_dst_port_free(
6961 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6962 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006963 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6964 bnxt_hwrm_port_qstats(bp);
6965
Michael Chana551ee92017-01-25 02:55:07 -05006966 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6967 * must be the last functions to be called before exiting.
6968 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006969 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6970 int rc = 0;
6971
6972 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6973 &bp->sp_event))
6974 bnxt_hwrm_phy_qcaps(bp);
6975
6976 bnxt_rtnl_lock_sp(bp);
6977 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6978 rc = bnxt_update_link(bp, true);
6979 bnxt_rtnl_unlock_sp(bp);
6980 if (rc)
6981 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6982 rc);
6983 }
Michael Chan90c694b2017-01-25 02:55:09 -05006984 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6985 bnxt_rtnl_lock_sp(bp);
6986 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6987 bnxt_get_port_module_status(bp);
6988 bnxt_rtnl_unlock_sp(bp);
6989 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006990 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6991 bnxt_reset(bp, false);
6992
6993 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6994 bnxt_reset(bp, true);
6995
Michael Chanc0c050c2015-10-22 16:01:17 -04006996 smp_mb__before_atomic();
6997 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6998}
6999
Michael Chand1e79252017-02-06 16:55:38 -05007000/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04007001int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7002 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05007003{
7004 int max_rx, max_tx, tx_sets = 1;
7005 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05007006 int rc;
7007
Michael Chand1e79252017-02-06 16:55:38 -05007008 if (tcs)
7009 tx_sets = tcs;
7010
7011 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7012 if (rc)
7013 return rc;
7014
7015 if (max_rx < rx)
7016 return -ENOMEM;
7017
Michael Chan5f449242017-02-06 16:55:40 -05007018 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05007019 if (max_tx < tx_rings_needed)
7020 return -ENOMEM;
7021
7022 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05007023 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05007024 return -ENOMEM;
7025 return 0;
7026}
7027
Sathya Perla17086392017-02-20 19:25:18 -05007028static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7029{
7030 if (bp->bar2) {
7031 pci_iounmap(pdev, bp->bar2);
7032 bp->bar2 = NULL;
7033 }
7034
7035 if (bp->bar1) {
7036 pci_iounmap(pdev, bp->bar1);
7037 bp->bar1 = NULL;
7038 }
7039
7040 if (bp->bar0) {
7041 pci_iounmap(pdev, bp->bar0);
7042 bp->bar0 = NULL;
7043 }
7044}
7045
7046static void bnxt_cleanup_pci(struct bnxt *bp)
7047{
7048 bnxt_unmap_bars(bp, bp->pdev);
7049 pci_release_regions(bp->pdev);
7050 pci_disable_device(bp->pdev);
7051}
7052
Michael Chanc0c050c2015-10-22 16:01:17 -04007053static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7054{
7055 int rc;
7056 struct bnxt *bp = netdev_priv(dev);
7057
7058 SET_NETDEV_DEV(dev, &pdev->dev);
7059
7060 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7061 rc = pci_enable_device(pdev);
7062 if (rc) {
7063 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7064 goto init_err;
7065 }
7066
7067 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7068 dev_err(&pdev->dev,
7069 "Cannot find PCI device base address, aborting\n");
7070 rc = -ENODEV;
7071 goto init_err_disable;
7072 }
7073
7074 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7075 if (rc) {
7076 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7077 goto init_err_disable;
7078 }
7079
7080 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7081 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7082 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7083 goto init_err_disable;
7084 }
7085
7086 pci_set_master(pdev);
7087
7088 bp->dev = dev;
7089 bp->pdev = pdev;
7090
7091 bp->bar0 = pci_ioremap_bar(pdev, 0);
7092 if (!bp->bar0) {
7093 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7094 rc = -ENOMEM;
7095 goto init_err_release;
7096 }
7097
7098 bp->bar1 = pci_ioremap_bar(pdev, 2);
7099 if (!bp->bar1) {
7100 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7101 rc = -ENOMEM;
7102 goto init_err_release;
7103 }
7104
7105 bp->bar2 = pci_ioremap_bar(pdev, 4);
7106 if (!bp->bar2) {
7107 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7108 rc = -ENOMEM;
7109 goto init_err_release;
7110 }
7111
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007112 pci_enable_pcie_error_reporting(pdev);
7113
Michael Chanc0c050c2015-10-22 16:01:17 -04007114 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7115
7116 spin_lock_init(&bp->ntp_fltr_lock);
7117
7118 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7119 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7120
Michael Chandfb5b892016-02-26 04:00:01 -05007121 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007122 bp->rx_coal_ticks = 12;
7123 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007124 bp->rx_coal_ticks_irq = 1;
7125 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007126
Michael Chandfc9c942016-02-26 04:00:03 -05007127 bp->tx_coal_ticks = 25;
7128 bp->tx_coal_bufs = 30;
7129 bp->tx_coal_ticks_irq = 2;
7130 bp->tx_coal_bufs_irq = 2;
7131
Michael Chan51f30782016-07-01 18:46:29 -04007132 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7133
Michael Chanc0c050c2015-10-22 16:01:17 -04007134 init_timer(&bp->timer);
7135 bp->timer.data = (unsigned long)bp;
7136 bp->timer.function = bnxt_timer;
7137 bp->current_interval = BNXT_TIMER_INTERVAL;
7138
Michael Chancaefe522015-12-09 19:35:42 -05007139 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007140 return 0;
7141
7142init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007143 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007144 pci_release_regions(pdev);
7145
7146init_err_disable:
7147 pci_disable_device(pdev);
7148
7149init_err:
7150 return rc;
7151}
7152
7153/* rtnl_lock held */
7154static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7155{
7156 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007157 struct bnxt *bp = netdev_priv(dev);
7158 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007159
7160 if (!is_valid_ether_addr(addr->sa_data))
7161 return -EADDRNOTAVAIL;
7162
Michael Chan84c33dd2016-04-11 04:11:13 -04007163 rc = bnxt_approve_mac(bp, addr->sa_data);
7164 if (rc)
7165 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007166
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007167 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7168 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007169
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007170 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7171 if (netif_running(dev)) {
7172 bnxt_close_nic(bp, false, false);
7173 rc = bnxt_open_nic(bp, false, false);
7174 }
7175
7176 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007177}
7178
7179/* rtnl_lock held */
7180static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7181{
7182 struct bnxt *bp = netdev_priv(dev);
7183
Michael Chanc0c050c2015-10-22 16:01:17 -04007184 if (netif_running(dev))
7185 bnxt_close_nic(bp, false, false);
7186
7187 dev->mtu = new_mtu;
7188 bnxt_set_ring_params(bp);
7189
7190 if (netif_running(dev))
7191 return bnxt_open_nic(bp, false, false);
7192
7193 return 0;
7194}
7195
Michael Chanc5e3deb2016-12-02 21:17:15 -05007196int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007197{
7198 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007199 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007200 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007201
Michael Chanc0c050c2015-10-22 16:01:17 -04007202 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007203 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007204 tc, bp->max_tc);
7205 return -EINVAL;
7206 }
7207
7208 if (netdev_get_num_tc(dev) == tc)
7209 return 0;
7210
Michael Chan3ffb6a32016-11-11 00:11:42 -05007211 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7212 sh = true;
7213
Michael Chan5f449242017-02-06 16:55:40 -05007214 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007215 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007216 if (rc)
7217 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007218
7219 /* Needs to close the device and do hw resource re-allocations */
7220 if (netif_running(bp->dev))
7221 bnxt_close_nic(bp, true, false);
7222
7223 if (tc) {
7224 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7225 netdev_set_num_tc(dev, tc);
7226 } else {
7227 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7228 netdev_reset_tc(dev);
7229 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007230 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7231 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007232 bp->num_stat_ctxs = bp->cp_nr_rings;
7233
7234 if (netif_running(bp->dev))
7235 return bnxt_open_nic(bp, true, false);
7236
7237 return 0;
7238}
7239
Jiri Pirko2572ac52017-08-07 10:15:17 +02007240static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirko2572ac52017-08-07 10:15:17 +02007241 struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007242{
Jiri Pirko2572ac52017-08-07 10:15:17 +02007243 if (type != TC_SETUP_MQPRIO)
Jiri Pirko38cf0422017-08-07 10:15:31 +02007244 return -EOPNOTSUPP;
Michael Chanc5e3deb2016-12-02 21:17:15 -05007245
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007246 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7247
7248 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007249}
7250
Michael Chanc0c050c2015-10-22 16:01:17 -04007251#ifdef CONFIG_RFS_ACCEL
7252static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7253 struct bnxt_ntuple_filter *f2)
7254{
7255 struct flow_keys *keys1 = &f1->fkeys;
7256 struct flow_keys *keys2 = &f2->fkeys;
7257
7258 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7259 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7260 keys1->ports.ports == keys2->ports.ports &&
7261 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7262 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007263 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007264 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7265 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007266 return true;
7267
7268 return false;
7269}
7270
7271static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7272 u16 rxq_index, u32 flow_id)
7273{
7274 struct bnxt *bp = netdev_priv(dev);
7275 struct bnxt_ntuple_filter *fltr, *new_fltr;
7276 struct flow_keys *fkeys;
7277 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007278 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007279 struct hlist_head *head;
7280
Michael Chana54c4d72016-07-25 12:33:35 -04007281 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7282 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7283 int off = 0, j;
7284
7285 netif_addr_lock_bh(dev);
7286 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7287 if (ether_addr_equal(eth->h_dest,
7288 vnic->uc_list + off)) {
7289 l2_idx = j + 1;
7290 break;
7291 }
7292 }
7293 netif_addr_unlock_bh(dev);
7294 if (!l2_idx)
7295 return -EINVAL;
7296 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007297 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7298 if (!new_fltr)
7299 return -ENOMEM;
7300
7301 fkeys = &new_fltr->fkeys;
7302 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7303 rc = -EPROTONOSUPPORT;
7304 goto err_free;
7305 }
7306
Michael Chandda0e742016-12-29 12:13:40 -05007307 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7308 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007309 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7310 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7311 rc = -EPROTONOSUPPORT;
7312 goto err_free;
7313 }
Michael Chandda0e742016-12-29 12:13:40 -05007314 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7315 bp->hwrm_spec_code < 0x10601) {
7316 rc = -EPROTONOSUPPORT;
7317 goto err_free;
7318 }
Michael Chan61aad722017-02-12 19:18:14 -05007319 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7320 bp->hwrm_spec_code < 0x10601) {
7321 rc = -EPROTONOSUPPORT;
7322 goto err_free;
7323 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007324
Michael Chana54c4d72016-07-25 12:33:35 -04007325 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007326 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7327
7328 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7329 head = &bp->ntp_fltr_hash_tbl[idx];
7330 rcu_read_lock();
7331 hlist_for_each_entry_rcu(fltr, head, hash) {
7332 if (bnxt_fltr_match(fltr, new_fltr)) {
7333 rcu_read_unlock();
7334 rc = 0;
7335 goto err_free;
7336 }
7337 }
7338 rcu_read_unlock();
7339
7340 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007341 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7342 BNXT_NTP_FLTR_MAX_FLTR, 0);
7343 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007344 spin_unlock_bh(&bp->ntp_fltr_lock);
7345 rc = -ENOMEM;
7346 goto err_free;
7347 }
7348
Michael Chan84e86b92015-11-05 16:25:50 -05007349 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007350 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007351 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007352 new_fltr->rxq = rxq_index;
7353 hlist_add_head_rcu(&new_fltr->hash, head);
7354 bp->ntp_fltr_count++;
7355 spin_unlock_bh(&bp->ntp_fltr_lock);
7356
7357 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7358 schedule_work(&bp->sp_task);
7359
7360 return new_fltr->sw_id;
7361
7362err_free:
7363 kfree(new_fltr);
7364 return rc;
7365}
7366
7367static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7368{
7369 int i;
7370
7371 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7372 struct hlist_head *head;
7373 struct hlist_node *tmp;
7374 struct bnxt_ntuple_filter *fltr;
7375 int rc;
7376
7377 head = &bp->ntp_fltr_hash_tbl[i];
7378 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7379 bool del = false;
7380
7381 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7382 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7383 fltr->flow_id,
7384 fltr->sw_id)) {
7385 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7386 fltr);
7387 del = true;
7388 }
7389 } else {
7390 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7391 fltr);
7392 if (rc)
7393 del = true;
7394 else
7395 set_bit(BNXT_FLTR_VALID, &fltr->state);
7396 }
7397
7398 if (del) {
7399 spin_lock_bh(&bp->ntp_fltr_lock);
7400 hlist_del_rcu(&fltr->hash);
7401 bp->ntp_fltr_count--;
7402 spin_unlock_bh(&bp->ntp_fltr_lock);
7403 synchronize_rcu();
7404 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7405 kfree(fltr);
7406 }
7407 }
7408 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007409 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7410 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007411}
7412
7413#else
7414
7415static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7416{
7417}
7418
7419#endif /* CONFIG_RFS_ACCEL */
7420
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007421static void bnxt_udp_tunnel_add(struct net_device *dev,
7422 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007423{
7424 struct bnxt *bp = netdev_priv(dev);
7425
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007426 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7427 return;
7428
Michael Chanc0c050c2015-10-22 16:01:17 -04007429 if (!netif_running(dev))
7430 return;
7431
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007432 switch (ti->type) {
7433 case UDP_TUNNEL_TYPE_VXLAN:
7434 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7435 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007436
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007437 bp->vxlan_port_cnt++;
7438 if (bp->vxlan_port_cnt == 1) {
7439 bp->vxlan_port = ti->port;
7440 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007441 schedule_work(&bp->sp_task);
7442 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007443 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007444 case UDP_TUNNEL_TYPE_GENEVE:
7445 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7446 return;
7447
7448 bp->nge_port_cnt++;
7449 if (bp->nge_port_cnt == 1) {
7450 bp->nge_port = ti->port;
7451 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7452 }
7453 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007454 default:
7455 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007456 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007457
7458 schedule_work(&bp->sp_task);
7459}
7460
7461static void bnxt_udp_tunnel_del(struct net_device *dev,
7462 struct udp_tunnel_info *ti)
7463{
7464 struct bnxt *bp = netdev_priv(dev);
7465
7466 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7467 return;
7468
7469 if (!netif_running(dev))
7470 return;
7471
7472 switch (ti->type) {
7473 case UDP_TUNNEL_TYPE_VXLAN:
7474 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7475 return;
7476 bp->vxlan_port_cnt--;
7477
7478 if (bp->vxlan_port_cnt != 0)
7479 return;
7480
7481 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7482 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007483 case UDP_TUNNEL_TYPE_GENEVE:
7484 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7485 return;
7486 bp->nge_port_cnt--;
7487
7488 if (bp->nge_port_cnt != 0)
7489 return;
7490
7491 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7492 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007493 default:
7494 return;
7495 }
7496
7497 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007498}
7499
Michael Chan39d8ba22017-07-24 12:34:22 -04007500static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7501 struct net_device *dev, u32 filter_mask,
7502 int nlflags)
7503{
7504 struct bnxt *bp = netdev_priv(dev);
7505
7506 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7507 nlflags, filter_mask, NULL);
7508}
7509
7510static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7511 u16 flags)
7512{
7513 struct bnxt *bp = netdev_priv(dev);
7514 struct nlattr *attr, *br_spec;
7515 int rem, rc = 0;
7516
7517 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7518 return -EOPNOTSUPP;
7519
7520 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7521 if (!br_spec)
7522 return -EINVAL;
7523
7524 nla_for_each_nested(attr, br_spec, rem) {
7525 u16 mode;
7526
7527 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7528 continue;
7529
7530 if (nla_len(attr) < sizeof(mode))
7531 return -EINVAL;
7532
7533 mode = nla_get_u16(attr);
7534 if (mode == bp->br_mode)
7535 break;
7536
7537 rc = bnxt_hwrm_set_br_mode(bp, mode);
7538 if (!rc)
7539 bp->br_mode = mode;
7540 break;
7541 }
7542 return rc;
7543}
7544
Sathya Perlac124a622017-07-24 12:34:29 -04007545static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
7546 size_t len)
7547{
7548 struct bnxt *bp = netdev_priv(dev);
7549 int rc;
7550
7551 /* The PF and it's VF-reps only support the switchdev framework */
7552 if (!BNXT_PF(bp))
7553 return -EOPNOTSUPP;
7554
Sathya Perla53f70b82017-07-25 13:28:41 -04007555 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04007556
7557 if (rc >= len)
7558 return -EOPNOTSUPP;
7559 return 0;
7560}
7561
7562int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
7563{
7564 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
7565 return -EOPNOTSUPP;
7566
7567 /* The PF and it's VF-reps only support the switchdev framework */
7568 if (!BNXT_PF(bp))
7569 return -EOPNOTSUPP;
7570
7571 switch (attr->id) {
7572 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
7573 /* In SRIOV each PF-pool (PF + child VFs) serves as a
7574 * switching domain, the PF's perm mac-addr can be used
7575 * as the unique parent-id
7576 */
7577 attr->u.ppid.id_len = ETH_ALEN;
7578 ether_addr_copy(attr->u.ppid.id, bp->pf.mac_addr);
7579 break;
7580 default:
7581 return -EOPNOTSUPP;
7582 }
7583 return 0;
7584}
7585
7586static int bnxt_swdev_port_attr_get(struct net_device *dev,
7587 struct switchdev_attr *attr)
7588{
7589 return bnxt_port_attr_get(netdev_priv(dev), attr);
7590}
7591
7592static const struct switchdev_ops bnxt_switchdev_ops = {
7593 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
7594};
7595
Michael Chanc0c050c2015-10-22 16:01:17 -04007596static const struct net_device_ops bnxt_netdev_ops = {
7597 .ndo_open = bnxt_open,
7598 .ndo_start_xmit = bnxt_start_xmit,
7599 .ndo_stop = bnxt_close,
7600 .ndo_get_stats64 = bnxt_get_stats64,
7601 .ndo_set_rx_mode = bnxt_set_rx_mode,
7602 .ndo_do_ioctl = bnxt_ioctl,
7603 .ndo_validate_addr = eth_validate_addr,
7604 .ndo_set_mac_address = bnxt_change_mac_addr,
7605 .ndo_change_mtu = bnxt_change_mtu,
7606 .ndo_fix_features = bnxt_fix_features,
7607 .ndo_set_features = bnxt_set_features,
7608 .ndo_tx_timeout = bnxt_tx_timeout,
7609#ifdef CONFIG_BNXT_SRIOV
7610 .ndo_get_vf_config = bnxt_get_vf_config,
7611 .ndo_set_vf_mac = bnxt_set_vf_mac,
7612 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7613 .ndo_set_vf_rate = bnxt_set_vf_bw,
7614 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7615 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7616#endif
7617#ifdef CONFIG_NET_POLL_CONTROLLER
7618 .ndo_poll_controller = bnxt_poll_controller,
7619#endif
7620 .ndo_setup_tc = bnxt_setup_tc,
7621#ifdef CONFIG_RFS_ACCEL
7622 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7623#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007624 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7625 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007626 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007627 .ndo_bridge_getlink = bnxt_bridge_getlink,
7628 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04007629 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04007630};
7631
7632static void bnxt_remove_one(struct pci_dev *pdev)
7633{
7634 struct net_device *dev = pci_get_drvdata(pdev);
7635 struct bnxt *bp = netdev_priv(dev);
7636
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007637 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007638 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007639 bnxt_dl_unregister(bp);
7640 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007641
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007642 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007643 unregister_netdev(dev);
7644 cancel_work_sync(&bp->sp_task);
7645 bp->sp_event = 0;
7646
Michael Chan78095922016-12-07 00:26:16 -05007647 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007648 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007649 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007650 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007651 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007652 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007653 kfree(bp->edev);
7654 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007655 if (bp->xdp_prog)
7656 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007657 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007658 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007659}
7660
7661static int bnxt_probe_phy(struct bnxt *bp)
7662{
7663 int rc = 0;
7664 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007665
Michael Chan170ce012016-04-05 14:08:57 -04007666 rc = bnxt_hwrm_phy_qcaps(bp);
7667 if (rc) {
7668 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7669 rc);
7670 return rc;
7671 }
7672
Michael Chanc0c050c2015-10-22 16:01:17 -04007673 rc = bnxt_update_link(bp, false);
7674 if (rc) {
7675 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7676 rc);
7677 return rc;
7678 }
7679
Michael Chan93ed8112016-06-13 02:25:37 -04007680 /* Older firmware does not have supported_auto_speeds, so assume
7681 * that all supported speeds can be autonegotiated.
7682 */
7683 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7684 link_info->support_auto_speeds = link_info->support_speeds;
7685
Michael Chanc0c050c2015-10-22 16:01:17 -04007686 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007687 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007688 link_info->autoneg = BNXT_AUTONEG_SPEED;
7689 if (bp->hwrm_spec_code >= 0x10201) {
7690 if (link_info->auto_pause_setting &
7691 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7692 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7693 } else {
7694 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7695 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007696 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007697 } else {
7698 link_info->req_link_speed = link_info->force_link_speed;
7699 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007700 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007701 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7702 link_info->req_flow_ctrl =
7703 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7704 else
7705 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007706 return rc;
7707}
7708
7709static int bnxt_get_max_irq(struct pci_dev *pdev)
7710{
7711 u16 ctrl;
7712
7713 if (!pdev->msix_cap)
7714 return 1;
7715
7716 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7717 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7718}
7719
Michael Chan6e6c5a52016-01-02 23:45:02 -05007720static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7721 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007722{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007723 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007724
Michael Chan379a80a2015-10-23 15:06:19 -04007725#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007726 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007727 *max_tx = bp->vf.max_tx_rings;
7728 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007729 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7730 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007731 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007732 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007733#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007734 {
7735 *max_tx = bp->pf.max_tx_rings;
7736 *max_rx = bp->pf.max_rx_rings;
7737 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7738 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7739 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007740 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007741 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7742 *max_cp -= 1;
7743 *max_rx -= 2;
7744 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007745 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7746 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007747 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007748}
7749
7750int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7751{
7752 int rx, tx, cp;
7753
7754 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7755 if (!rx || !tx || !cp)
7756 return -ENOMEM;
7757
7758 *max_rx = rx;
7759 *max_tx = tx;
7760 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7761}
7762
Michael Chane4060d32016-12-07 00:26:19 -05007763static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7764 bool shared)
7765{
7766 int rc;
7767
7768 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007769 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7770 /* Not enough rings, try disabling agg rings. */
7771 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7772 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7773 if (rc)
7774 return rc;
7775 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7776 bp->dev->hw_features &= ~NETIF_F_LRO;
7777 bp->dev->features &= ~NETIF_F_LRO;
7778 bnxt_set_ring_params(bp);
7779 }
Michael Chane4060d32016-12-07 00:26:19 -05007780
7781 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7782 int max_cp, max_stat, max_irq;
7783
7784 /* Reserve minimum resources for RoCE */
7785 max_cp = bnxt_get_max_func_cp_rings(bp);
7786 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7787 max_irq = bnxt_get_max_func_irqs(bp);
7788 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7789 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7790 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7791 return 0;
7792
7793 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7794 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7795 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7796 max_cp = min_t(int, max_cp, max_irq);
7797 max_cp = min_t(int, max_cp, max_stat);
7798 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7799 if (rc)
7800 rc = 0;
7801 }
7802 return rc;
7803}
7804
Michael Chan702c2212017-05-29 19:06:10 -04007805static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007806{
7807 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007808
7809 if (sh)
7810 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7811 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007812 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007813 if (rc)
7814 return rc;
7815 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7816 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007817
7818 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7819 if (rc)
7820 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7821
Michael Chan6e6c5a52016-01-02 23:45:02 -05007822 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7823 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7824 bp->tx_nr_rings + bp->rx_nr_rings;
7825 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007826 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7827 bp->rx_nr_rings++;
7828 bp->cp_nr_rings++;
7829 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007830 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007831}
7832
Michael Chan7b08f662016-12-07 00:26:18 -05007833void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7834{
7835 ASSERT_RTNL();
7836 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007837 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007838}
7839
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007840static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7841{
7842 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7843 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7844
7845 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7846 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7847 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7848 else
7849 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7850 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7851 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7852 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7853 "Unknown", width);
7854}
7855
Michael Chanc0c050c2015-10-22 16:01:17 -04007856static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7857{
7858 static int version_printed;
7859 struct net_device *dev;
7860 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007861 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007862
Ray Jui4e003382017-02-20 19:25:16 -05007863 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007864 return -ENODEV;
7865
Michael Chanc0c050c2015-10-22 16:01:17 -04007866 if (version_printed++ == 0)
7867 pr_info("%s", version);
7868
7869 max_irqs = bnxt_get_max_irq(pdev);
7870 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7871 if (!dev)
7872 return -ENOMEM;
7873
7874 bp = netdev_priv(dev);
7875
7876 if (bnxt_vf_pciid(ent->driver_data))
7877 bp->flags |= BNXT_FLAG_VF;
7878
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007879 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007880 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007881
7882 rc = bnxt_init_board(pdev, dev);
7883 if (rc < 0)
7884 goto init_err_free;
7885
7886 dev->netdev_ops = &bnxt_netdev_ops;
7887 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7888 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -07007889 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -04007890 pci_set_drvdata(pdev, dev);
7891
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007892 rc = bnxt_alloc_hwrm_resources(bp);
7893 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007894 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007895
7896 mutex_init(&bp->hwrm_cmd_lock);
7897 rc = bnxt_hwrm_ver_get(bp);
7898 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007899 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007900
Deepak Khungare605db82017-05-29 19:06:04 -04007901 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7902 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7903 if (rc)
7904 goto init_err_pci_clean;
7905 }
7906
Michael Chan3c2217a2017-03-08 18:44:32 -05007907 rc = bnxt_hwrm_func_reset(bp);
7908 if (rc)
7909 goto init_err_pci_clean;
7910
Rob Swindell5ac67d82016-09-19 03:58:03 -04007911 bnxt_hwrm_fw_set_time(bp);
7912
Michael Chanc0c050c2015-10-22 16:01:17 -04007913 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7914 NETIF_F_TSO | NETIF_F_TSO6 |
7915 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007916 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007917 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7918 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007919 NETIF_F_RXCSUM | NETIF_F_GRO;
7920
7921 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7922 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007923
Michael Chanc0c050c2015-10-22 16:01:17 -04007924 dev->hw_enc_features =
7925 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7926 NETIF_F_TSO | NETIF_F_TSO6 |
7927 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007928 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007929 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007930 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7931 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007932 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7933 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7934 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7935 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7936 dev->priv_flags |= IFF_UNICAST_FLT;
7937
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007938 /* MTU range: 60 - 9500 */
7939 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007940 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007941
Michael Chanc0c050c2015-10-22 16:01:17 -04007942#ifdef CONFIG_BNXT_SRIOV
7943 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007944 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04007945#endif
Michael Chan309369c2016-06-13 02:25:34 -04007946 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007947 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007948 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007949 else
7950 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007951
Michael Chanc0c050c2015-10-22 16:01:17 -04007952 rc = bnxt_hwrm_func_drv_rgtr(bp);
7953 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007954 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007955
Michael Chana1653b12016-12-07 00:26:20 -05007956 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7957 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007958 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007959
Michael Chana588e452016-12-07 00:26:21 -05007960 bp->ulp_probe = bnxt_ulp_probe;
7961
Michael Chanc0c050c2015-10-22 16:01:17 -04007962 /* Get the MAX capabilities for this function */
7963 rc = bnxt_hwrm_func_qcaps(bp);
7964 if (rc) {
7965 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7966 rc);
7967 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007968 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007969 }
7970
7971 rc = bnxt_hwrm_queue_qportcfg(bp);
7972 if (rc) {
7973 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7974 rc);
7975 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007976 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007977 }
7978
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007979 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007980 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007981 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007982 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007983
Michael Chanc61fb992017-02-06 16:55:36 -05007984 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007985 bnxt_set_tpa_flags(bp);
7986 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007987 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007988 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007989 if (rc) {
7990 netdev_err(bp->dev, "Not enough rings available.\n");
7991 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007992 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007993 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007994
Michael Chan87da7f72016-11-16 21:13:09 -05007995 /* Default RSS hash cfg. */
7996 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7997 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7998 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7999 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04008000 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05008001 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8002 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8003 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8004 }
8005
Michael Chan8fdefd62016-12-29 12:13:36 -05008006 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05008007 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008008 dev->hw_features |= NETIF_F_NTUPLE;
8009 if (bnxt_rfs_capable(bp)) {
8010 bp->flags |= BNXT_FLAG_RFS;
8011 dev->features |= NETIF_F_NTUPLE;
8012 }
8013 }
8014
Michael Chanc0c050c2015-10-22 16:01:17 -04008015 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8016 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8017
8018 rc = bnxt_probe_phy(bp);
8019 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008020 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008021
Michael Chan78095922016-12-07 00:26:16 -05008022 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008023 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008024 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008025
Michael Chanc1ef1462017-04-04 18:14:07 -04008026 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008027 if (bp->flags & BNXT_FLAG_WOL_CAP)
8028 device_set_wakeup_enable(&pdev->dev, bp->wol);
8029 else
8030 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04008031
Michael Chan78095922016-12-07 00:26:16 -05008032 rc = register_netdev(dev);
8033 if (rc)
8034 goto init_err_clr_int;
8035
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008036 if (BNXT_PF(bp))
8037 bnxt_dl_register(bp);
8038
Michael Chanc0c050c2015-10-22 16:01:17 -04008039 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8040 board_info[ent->driver_data].name,
8041 (long)pci_resource_start(pdev, 0), dev->dev_addr);
8042
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008043 bnxt_parse_log_pcie_link(bp);
8044
Michael Chanc0c050c2015-10-22 16:01:17 -04008045 return 0;
8046
Michael Chan78095922016-12-07 00:26:16 -05008047init_err_clr_int:
8048 bnxt_clear_int_mode(bp);
8049
Sathya Perla17086392017-02-20 19:25:18 -05008050init_err_pci_clean:
8051 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008052
8053init_err_free:
8054 free_netdev(dev);
8055 return rc;
8056}
8057
Michael Chand196ece2017-04-04 18:14:08 -04008058static void bnxt_shutdown(struct pci_dev *pdev)
8059{
8060 struct net_device *dev = pci_get_drvdata(pdev);
8061 struct bnxt *bp;
8062
8063 if (!dev)
8064 return;
8065
8066 rtnl_lock();
8067 bp = netdev_priv(dev);
8068 if (!bp)
8069 goto shutdown_exit;
8070
8071 if (netif_running(dev))
8072 dev_close(dev);
8073
8074 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04008075 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008076 bnxt_clear_int_mode(bp);
8077 pci_wake_from_d3(pdev, bp->wol);
8078 pci_set_power_state(pdev, PCI_D3hot);
8079 }
8080
8081shutdown_exit:
8082 rtnl_unlock();
8083}
8084
Michael Chanf65a2042017-04-04 18:14:11 -04008085#ifdef CONFIG_PM_SLEEP
8086static int bnxt_suspend(struct device *device)
8087{
8088 struct pci_dev *pdev = to_pci_dev(device);
8089 struct net_device *dev = pci_get_drvdata(pdev);
8090 struct bnxt *bp = netdev_priv(dev);
8091 int rc = 0;
8092
8093 rtnl_lock();
8094 if (netif_running(dev)) {
8095 netif_device_detach(dev);
8096 rc = bnxt_close(dev);
8097 }
8098 bnxt_hwrm_func_drv_unrgtr(bp);
8099 rtnl_unlock();
8100 return rc;
8101}
8102
8103static int bnxt_resume(struct device *device)
8104{
8105 struct pci_dev *pdev = to_pci_dev(device);
8106 struct net_device *dev = pci_get_drvdata(pdev);
8107 struct bnxt *bp = netdev_priv(dev);
8108 int rc = 0;
8109
8110 rtnl_lock();
8111 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8112 rc = -ENODEV;
8113 goto resume_exit;
8114 }
8115 rc = bnxt_hwrm_func_reset(bp);
8116 if (rc) {
8117 rc = -EBUSY;
8118 goto resume_exit;
8119 }
8120 bnxt_get_wol_settings(bp);
8121 if (netif_running(dev)) {
8122 rc = bnxt_open(dev);
8123 if (!rc)
8124 netif_device_attach(dev);
8125 }
8126
8127resume_exit:
8128 rtnl_unlock();
8129 return rc;
8130}
8131
8132static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8133#define BNXT_PM_OPS (&bnxt_pm_ops)
8134
8135#else
8136
8137#define BNXT_PM_OPS NULL
8138
8139#endif /* CONFIG_PM_SLEEP */
8140
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008141/**
8142 * bnxt_io_error_detected - called when PCI error is detected
8143 * @pdev: Pointer to PCI device
8144 * @state: The current pci connection state
8145 *
8146 * This function is called after a PCI bus error affecting
8147 * this device has been detected.
8148 */
8149static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8150 pci_channel_state_t state)
8151{
8152 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008153 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008154
8155 netdev_info(netdev, "PCI I/O error detected\n");
8156
8157 rtnl_lock();
8158 netif_device_detach(netdev);
8159
Michael Chana588e452016-12-07 00:26:21 -05008160 bnxt_ulp_stop(bp);
8161
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008162 if (state == pci_channel_io_perm_failure) {
8163 rtnl_unlock();
8164 return PCI_ERS_RESULT_DISCONNECT;
8165 }
8166
8167 if (netif_running(netdev))
8168 bnxt_close(netdev);
8169
8170 pci_disable_device(pdev);
8171 rtnl_unlock();
8172
8173 /* Request a slot slot reset. */
8174 return PCI_ERS_RESULT_NEED_RESET;
8175}
8176
8177/**
8178 * bnxt_io_slot_reset - called after the pci bus has been reset.
8179 * @pdev: Pointer to PCI device
8180 *
8181 * Restart the card from scratch, as if from a cold-boot.
8182 * At this point, the card has exprienced a hard reset,
8183 * followed by fixups by BIOS, and has its config space
8184 * set up identically to what it was at cold boot.
8185 */
8186static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8187{
8188 struct net_device *netdev = pci_get_drvdata(pdev);
8189 struct bnxt *bp = netdev_priv(netdev);
8190 int err = 0;
8191 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8192
8193 netdev_info(bp->dev, "PCI Slot Reset\n");
8194
8195 rtnl_lock();
8196
8197 if (pci_enable_device(pdev)) {
8198 dev_err(&pdev->dev,
8199 "Cannot re-enable PCI device after reset.\n");
8200 } else {
8201 pci_set_master(pdev);
8202
Michael Chanaa8ed022016-12-07 00:26:17 -05008203 err = bnxt_hwrm_func_reset(bp);
8204 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008205 err = bnxt_open(netdev);
8206
Michael Chana588e452016-12-07 00:26:21 -05008207 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008208 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008209 bnxt_ulp_start(bp);
8210 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008211 }
8212
8213 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8214 dev_close(netdev);
8215
8216 rtnl_unlock();
8217
8218 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8219 if (err) {
8220 dev_err(&pdev->dev,
8221 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8222 err); /* non-fatal, continue */
8223 }
8224
8225 return PCI_ERS_RESULT_RECOVERED;
8226}
8227
8228/**
8229 * bnxt_io_resume - called when traffic can start flowing again.
8230 * @pdev: Pointer to PCI device
8231 *
8232 * This callback is called when the error recovery driver tells
8233 * us that its OK to resume normal operation.
8234 */
8235static void bnxt_io_resume(struct pci_dev *pdev)
8236{
8237 struct net_device *netdev = pci_get_drvdata(pdev);
8238
8239 rtnl_lock();
8240
8241 netif_device_attach(netdev);
8242
8243 rtnl_unlock();
8244}
8245
8246static const struct pci_error_handlers bnxt_err_handler = {
8247 .error_detected = bnxt_io_error_detected,
8248 .slot_reset = bnxt_io_slot_reset,
8249 .resume = bnxt_io_resume
8250};
8251
Michael Chanc0c050c2015-10-22 16:01:17 -04008252static struct pci_driver bnxt_pci_driver = {
8253 .name = DRV_MODULE_NAME,
8254 .id_table = bnxt_pci_tbl,
8255 .probe = bnxt_init_one,
8256 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008257 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008258 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008259 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008260#if defined(CONFIG_BNXT_SRIOV)
8261 .sriov_configure = bnxt_sriov_configure,
8262#endif
8263};
8264
8265module_pci_driver(bnxt_pci_driver);