blob: 177c8f59892ba837cebe96397648c59f047dd697 [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 Chan894aa692018-01-17 03:21:03 -05004 * Copyright (c) 2016-2018 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>
Vasundhara Volam56f0fd82017-08-28 13:40:27 -040052#include <linux/cpumask.h>
Sathya Perla2ae74082017-08-28 13:40:33 -040053#include <net/pkt_cls.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040054
55#include "bnxt_hsi.h"
56#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050057#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040058#include "bnxt_sriov.h"
59#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050060#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050061#include "bnxt_xdp.h"
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040062#include "bnxt_vfr.h"
Sathya Perla2ae74082017-08-28 13:40:33 -040063#include "bnxt_tc.h"
Steve Lin3c467bf2017-10-19 10:45:56 -040064#include "bnxt_devlink.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040065
66#define BNXT_TX_TIMEOUT (5 * HZ)
67
68static const char version[] =
69 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
70
71MODULE_LICENSE("GPL");
72MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
73MODULE_VERSION(DRV_MODULE_VERSION);
74
75#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
76#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
77#define BNXT_RX_COPY_THRESH 256
78
Michael Chan4419dbe2016-02-10 17:33:49 -050079#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040080
81enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050082 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040083 BCM57302,
84 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040085 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040086 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040087 BCM57311,
88 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050089 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040090 BCM57404,
91 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57402_NPAR,
93 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040094 BCM57412,
95 BCM57414,
96 BCM57416,
97 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040098 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040099 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -0400100 BCM57417_SFP,
101 BCM57416_SFP,
102 BCM57404_NPAR,
103 BCM57406_NPAR,
104 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400105 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400106 BCM57414_NPAR,
107 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500108 BCM57452,
109 BCM57454,
Ray Jui4a581392017-08-28 13:40:28 -0400110 BCM58802,
Ray Jui8ed693b2017-10-26 11:51:20 -0400111 BCM58804,
Ray Jui4a581392017-08-28 13:40:28 -0400112 BCM58808,
Michael Chanadbc8302016-09-19 03:58:01 -0400113 NETXTREME_E_VF,
114 NETXTREME_C_VF,
Rob Miller618784e2017-10-26 11:51:21 -0400115 NETXTREME_S_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400116};
117
118/* indexed by enum above */
119static const struct {
120 char *name;
121} board_info[] = {
Scott Branden27573a72017-08-28 13:40:29 -0400122 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
123 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
124 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
126 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
127 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
128 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
129 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
130 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
131 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
132 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
133 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
134 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
135 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
136 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
137 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
138 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
139 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
140 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
141 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
142 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
143 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
144 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
145 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
146 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
147 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
148 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
149 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
150 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
Ray Jui8ed693b2017-10-26 11:51:20 -0400151 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400152 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
153 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
154 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Rob Miller618784e2017-10-26 11:51:21 -0400155 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400156};
157
158static const struct pci_device_id bnxt_pci_tbl[] = {
Ray Jui4a581392017-08-28 13:40:28 -0400159 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanadbc8302016-09-19 03:58:01 -0400160 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500161 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400162 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
163 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400165 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
167 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500168 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400169 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
170 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400173 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
174 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
175 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
176 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400177 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400178 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400179 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
180 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
181 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
182 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
183 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400184 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
185 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400186 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400187 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400188 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400189 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400190 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
Deepak Khungar32b40792017-02-12 19:18:18 -0500191 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
Ray Jui4a581392017-08-28 13:40:28 -0400192 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
Ray Jui8ed693b2017-10-26 11:51:20 -0400193 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400194#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400195 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400197 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
198 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
199 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
200 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
201 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
202 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Rob Miller618784e2017-10-26 11:51:21 -0400203 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400204#endif
205 { 0 }
206};
207
208MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
209
210static const u16 bnxt_vf_req_snif[] = {
211 HWRM_FUNC_CFG,
212 HWRM_PORT_PHY_QCFG,
213 HWRM_CFA_L2_FILTER_ALLOC,
214};
215
Michael Chan25be8622016-04-05 14:09:00 -0400216static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500217 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
218 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
219 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
220 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
221 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400222};
223
Michael Chanc213eae2017-10-13 21:09:29 -0400224static struct workqueue_struct *bnxt_pf_wq;
225
Michael Chanc0c050c2015-10-22 16:01:17 -0400226static bool bnxt_vf_pciid(enum board_idx idx)
227{
Rob Miller618784e2017-10-26 11:51:21 -0400228 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
229 idx == NETXTREME_S_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400230}
231
232#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
233#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
234#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
235
236#define BNXT_CP_DB_REARM(db, raw_cons) \
237 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
238
239#define BNXT_CP_DB(db, raw_cons) \
240 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
241
242#define BNXT_CP_DB_IRQ_DIS(db) \
243 writel(DB_CP_IRQ_DIS_FLAGS, db)
244
Michael Chan38413402017-02-06 16:55:43 -0500245const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400246 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
247 TX_BD_FLAGS_LHINT_512_TO_1023,
248 TX_BD_FLAGS_LHINT_1024_TO_2047,
249 TX_BD_FLAGS_LHINT_1024_TO_2047,
250 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
251 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
252 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
253 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
254 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
255 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
256 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
257 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
258 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
259 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
260 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
261 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
262 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
263 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
264 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
265};
266
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400267static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
268{
269 struct metadata_dst *md_dst = skb_metadata_dst(skb);
270
271 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
272 return 0;
273
274 return md_dst->u.port_info.port_id;
275}
276
Michael Chanc0c050c2015-10-22 16:01:17 -0400277static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
278{
279 struct bnxt *bp = netdev_priv(dev);
280 struct tx_bd *txbd;
281 struct tx_bd_ext *txbd1;
282 struct netdev_queue *txq;
283 int i;
284 dma_addr_t mapping;
285 unsigned int length, pad = 0;
286 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
287 u16 prod, last_frag;
288 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400289 struct bnxt_tx_ring_info *txr;
290 struct bnxt_sw_tx_bd *tx_buf;
291
292 i = skb_get_queue_mapping(skb);
293 if (unlikely(i >= bp->tx_nr_rings)) {
294 dev_kfree_skb_any(skb);
295 return NETDEV_TX_OK;
296 }
297
Michael Chanc0c050c2015-10-22 16:01:17 -0400298 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500299 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400300 prod = txr->tx_prod;
301
302 free_size = bnxt_tx_avail(bp, txr);
303 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
304 netif_tx_stop_queue(txq);
305 return NETDEV_TX_BUSY;
306 }
307
308 length = skb->len;
309 len = skb_headlen(skb);
310 last_frag = skb_shinfo(skb)->nr_frags;
311
312 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
313
314 txbd->tx_bd_opaque = prod;
315
316 tx_buf = &txr->tx_buf_ring[prod];
317 tx_buf->skb = skb;
318 tx_buf->nr_frags = last_frag;
319
320 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400321 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400322 if (skb_vlan_tag_present(skb)) {
323 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
324 skb_vlan_tag_get(skb);
325 /* Currently supports 8021Q, 8021AD vlan offloads
326 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
327 */
328 if (skb->vlan_proto == htons(ETH_P_8021Q))
329 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
330 }
331
332 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500333 struct tx_push_buffer *tx_push_buf = txr->tx_push;
334 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
335 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
336 void *pdata = tx_push_buf->data;
337 u64 *end;
338 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400339
340 /* Set COAL_NOW to be ready quickly for the next push */
341 tx_push->tx_bd_len_flags_type =
342 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
343 TX_BD_TYPE_LONG_TX_BD |
344 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
345 TX_BD_FLAGS_COAL_NOW |
346 TX_BD_FLAGS_PACKET_END |
347 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
348
349 if (skb->ip_summed == CHECKSUM_PARTIAL)
350 tx_push1->tx_bd_hsize_lflags =
351 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
352 else
353 tx_push1->tx_bd_hsize_lflags = 0;
354
355 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400356 tx_push1->tx_bd_cfa_action =
357 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400358
Michael Chanfbb0fa82016-02-22 02:10:26 -0500359 end = pdata + length;
360 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500361 *end = 0;
362
Michael Chanc0c050c2015-10-22 16:01:17 -0400363 skb_copy_from_linear_data(skb, pdata, len);
364 pdata += len;
365 for (j = 0; j < last_frag; j++) {
366 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
367 void *fptr;
368
369 fptr = skb_frag_address_safe(frag);
370 if (!fptr)
371 goto normal_tx;
372
373 memcpy(pdata, fptr, skb_frag_size(frag));
374 pdata += skb_frag_size(frag);
375 }
376
Michael Chan4419dbe2016-02-10 17:33:49 -0500377 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
378 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400379 prod = NEXT_TX(prod);
380 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
381 memcpy(txbd, tx_push1, sizeof(*txbd));
382 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500383 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400384 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
385 txr->tx_prod = prod;
386
Michael Chanb9a84602016-06-06 02:37:14 -0400387 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400388 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400389 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400390
Michael Chan4419dbe2016-02-10 17:33:49 -0500391 push_len = (length + sizeof(*tx_push) + 7) / 8;
392 if (push_len > 16) {
393 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400394 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
395 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500396 } else {
397 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
398 push_len);
399 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400400
Michael Chanc0c050c2015-10-22 16:01:17 -0400401 goto tx_done;
402 }
403
404normal_tx:
405 if (length < BNXT_MIN_PKT_SIZE) {
406 pad = BNXT_MIN_PKT_SIZE - length;
407 if (skb_pad(skb, pad)) {
408 /* SKB already freed. */
409 tx_buf->skb = NULL;
410 return NETDEV_TX_OK;
411 }
412 length = BNXT_MIN_PKT_SIZE;
413 }
414
415 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
416
417 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
418 dev_kfree_skb_any(skb);
419 tx_buf->skb = NULL;
420 return NETDEV_TX_OK;
421 }
422
423 dma_unmap_addr_set(tx_buf, mapping, mapping);
424 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
425 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
426
427 txbd->tx_bd_haddr = cpu_to_le64(mapping);
428
429 prod = NEXT_TX(prod);
430 txbd1 = (struct tx_bd_ext *)
431 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
432
433 txbd1->tx_bd_hsize_lflags = 0;
434 if (skb_is_gso(skb)) {
435 u32 hdr_len;
436
437 if (skb->encapsulation)
438 hdr_len = skb_inner_network_offset(skb) +
439 skb_inner_network_header_len(skb) +
440 inner_tcp_hdrlen(skb);
441 else
442 hdr_len = skb_transport_offset(skb) +
443 tcp_hdrlen(skb);
444
445 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
446 TX_BD_FLAGS_T_IPID |
447 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
448 length = skb_shinfo(skb)->gso_size;
449 txbd1->tx_bd_mss = cpu_to_le32(length);
450 length += hdr_len;
451 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
452 txbd1->tx_bd_hsize_lflags =
453 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
454 txbd1->tx_bd_mss = 0;
455 }
456
457 length >>= 9;
458 flags |= bnxt_lhint_arr[length];
459 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
460
461 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400462 txbd1->tx_bd_cfa_action =
463 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400464 for (i = 0; i < last_frag; i++) {
465 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
466
467 prod = NEXT_TX(prod);
468 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
469
470 len = skb_frag_size(frag);
471 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
472 DMA_TO_DEVICE);
473
474 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
475 goto tx_dma_error;
476
477 tx_buf = &txr->tx_buf_ring[prod];
478 dma_unmap_addr_set(tx_buf, mapping, mapping);
479
480 txbd->tx_bd_haddr = cpu_to_le64(mapping);
481
482 flags = len << TX_BD_LEN_SHIFT;
483 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
484 }
485
486 flags &= ~TX_BD_LEN;
487 txbd->tx_bd_len_flags_type =
488 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
489 TX_BD_FLAGS_PACKET_END);
490
491 netdev_tx_sent_queue(txq, skb->len);
492
493 /* Sync BD data before updating doorbell */
494 wmb();
495
496 prod = NEXT_TX(prod);
497 txr->tx_prod = prod;
498
Michael Chanffe40642017-05-30 20:03:00 -0400499 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400500 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400501
502tx_done:
503
504 mmiowb();
505
506 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400507 if (skb->xmit_more && !tx_buf->is_push)
508 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
509
Michael Chanc0c050c2015-10-22 16:01:17 -0400510 netif_tx_stop_queue(txq);
511
512 /* netif_tx_stop_queue() must be done before checking
513 * tx index in bnxt_tx_avail() below, because in
514 * bnxt_tx_int(), we update tx index before checking for
515 * netif_tx_queue_stopped().
516 */
517 smp_mb();
518 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
519 netif_tx_wake_queue(txq);
520 }
521 return NETDEV_TX_OK;
522
523tx_dma_error:
524 last_frag = i;
525
526 /* start back at beginning and unmap skb */
527 prod = txr->tx_prod;
528 tx_buf = &txr->tx_buf_ring[prod];
529 tx_buf->skb = NULL;
530 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
531 skb_headlen(skb), PCI_DMA_TODEVICE);
532 prod = NEXT_TX(prod);
533
534 /* unmap remaining mapped pages */
535 for (i = 0; i < last_frag; i++) {
536 prod = NEXT_TX(prod);
537 tx_buf = &txr->tx_buf_ring[prod];
538 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
539 skb_frag_size(&skb_shinfo(skb)->frags[i]),
540 PCI_DMA_TODEVICE);
541 }
542
543 dev_kfree_skb_any(skb);
544 return NETDEV_TX_OK;
545}
546
547static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
548{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500549 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500550 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400551 u16 cons = txr->tx_cons;
552 struct pci_dev *pdev = bp->pdev;
553 int i;
554 unsigned int tx_bytes = 0;
555
556 for (i = 0; i < nr_pkts; i++) {
557 struct bnxt_sw_tx_bd *tx_buf;
558 struct sk_buff *skb;
559 int j, last;
560
561 tx_buf = &txr->tx_buf_ring[cons];
562 cons = NEXT_TX(cons);
563 skb = tx_buf->skb;
564 tx_buf->skb = NULL;
565
566 if (tx_buf->is_push) {
567 tx_buf->is_push = 0;
568 goto next_tx_int;
569 }
570
571 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
572 skb_headlen(skb), PCI_DMA_TODEVICE);
573 last = tx_buf->nr_frags;
574
575 for (j = 0; j < last; j++) {
576 cons = NEXT_TX(cons);
577 tx_buf = &txr->tx_buf_ring[cons];
578 dma_unmap_page(
579 &pdev->dev,
580 dma_unmap_addr(tx_buf, mapping),
581 skb_frag_size(&skb_shinfo(skb)->frags[j]),
582 PCI_DMA_TODEVICE);
583 }
584
585next_tx_int:
586 cons = NEXT_TX(cons);
587
588 tx_bytes += skb->len;
589 dev_kfree_skb_any(skb);
590 }
591
592 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
593 txr->tx_cons = cons;
594
595 /* Need to make the tx_cons update visible to bnxt_start_xmit()
596 * before checking for netif_tx_queue_stopped(). Without the
597 * memory barrier, there is a small possibility that bnxt_start_xmit()
598 * will miss it and cause the queue to be stopped forever.
599 */
600 smp_mb();
601
602 if (unlikely(netif_tx_queue_stopped(txq)) &&
603 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
604 __netif_tx_lock(txq, smp_processor_id());
605 if (netif_tx_queue_stopped(txq) &&
606 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
607 txr->dev_state != BNXT_DEV_STATE_CLOSING)
608 netif_tx_wake_queue(txq);
609 __netif_tx_unlock(txq);
610 }
611}
612
Michael Chanc61fb992017-02-06 16:55:36 -0500613static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
614 gfp_t gfp)
615{
616 struct device *dev = &bp->pdev->dev;
617 struct page *page;
618
619 page = alloc_page(gfp);
620 if (!page)
621 return NULL;
622
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700623 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
624 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500625 if (dma_mapping_error(dev, *mapping)) {
626 __free_page(page);
627 return NULL;
628 }
629 *mapping += bp->rx_dma_offset;
630 return page;
631}
632
Michael Chanc0c050c2015-10-22 16:01:17 -0400633static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
634 gfp_t gfp)
635{
636 u8 *data;
637 struct pci_dev *pdev = bp->pdev;
638
639 data = kmalloc(bp->rx_buf_size, gfp);
640 if (!data)
641 return NULL;
642
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700643 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
644 bp->rx_buf_use_size, bp->rx_dir,
645 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400646
647 if (dma_mapping_error(&pdev->dev, *mapping)) {
648 kfree(data);
649 data = NULL;
650 }
651 return data;
652}
653
Michael Chan38413402017-02-06 16:55:43 -0500654int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
655 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400656{
657 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
658 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400659 dma_addr_t mapping;
660
Michael Chanc61fb992017-02-06 16:55:36 -0500661 if (BNXT_RX_PAGE_MODE(bp)) {
662 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400663
Michael Chanc61fb992017-02-06 16:55:36 -0500664 if (!page)
665 return -ENOMEM;
666
667 rx_buf->data = page;
668 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
669 } else {
670 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
671
672 if (!data)
673 return -ENOMEM;
674
675 rx_buf->data = data;
676 rx_buf->data_ptr = data + bp->rx_offset;
677 }
Michael Chan11cd1192017-02-06 16:55:33 -0500678 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400679
680 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400681 return 0;
682}
683
Michael Chanc6d30e82017-02-06 16:55:42 -0500684void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400685{
686 u16 prod = rxr->rx_prod;
687 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
688 struct rx_bd *cons_bd, *prod_bd;
689
690 prod_rx_buf = &rxr->rx_buf_ring[prod];
691 cons_rx_buf = &rxr->rx_buf_ring[cons];
692
693 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500694 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400695
Michael Chan11cd1192017-02-06 16:55:33 -0500696 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400697
698 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
699 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
700
701 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
702}
703
704static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
705{
706 u16 next, max = rxr->rx_agg_bmap_size;
707
708 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
709 if (next >= max)
710 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
711 return next;
712}
713
714static inline int bnxt_alloc_rx_page(struct bnxt *bp,
715 struct bnxt_rx_ring_info *rxr,
716 u16 prod, gfp_t gfp)
717{
718 struct rx_bd *rxbd =
719 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
720 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
721 struct pci_dev *pdev = bp->pdev;
722 struct page *page;
723 dma_addr_t mapping;
724 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400725 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400726
Michael Chan89d0a062016-04-25 02:30:51 -0400727 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
728 page = rxr->rx_page;
729 if (!page) {
730 page = alloc_page(gfp);
731 if (!page)
732 return -ENOMEM;
733 rxr->rx_page = page;
734 rxr->rx_page_offset = 0;
735 }
736 offset = rxr->rx_page_offset;
737 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
738 if (rxr->rx_page_offset == PAGE_SIZE)
739 rxr->rx_page = NULL;
740 else
741 get_page(page);
742 } else {
743 page = alloc_page(gfp);
744 if (!page)
745 return -ENOMEM;
746 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400747
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700748 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
749 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
750 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400751 if (dma_mapping_error(&pdev->dev, mapping)) {
752 __free_page(page);
753 return -EIO;
754 }
755
756 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
757 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
758
759 __set_bit(sw_prod, rxr->rx_agg_bmap);
760 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
761 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
762
763 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400764 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400765 rx_agg_buf->mapping = mapping;
766 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
767 rxbd->rx_bd_opaque = sw_prod;
768 return 0;
769}
770
771static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
772 u32 agg_bufs)
773{
774 struct bnxt *bp = bnapi->bp;
775 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500776 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400777 u16 prod = rxr->rx_agg_prod;
778 u16 sw_prod = rxr->rx_sw_agg_prod;
779 u32 i;
780
781 for (i = 0; i < agg_bufs; i++) {
782 u16 cons;
783 struct rx_agg_cmp *agg;
784 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
785 struct rx_bd *prod_bd;
786 struct page *page;
787
788 agg = (struct rx_agg_cmp *)
789 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
790 cons = agg->rx_agg_cmp_opaque;
791 __clear_bit(cons, rxr->rx_agg_bmap);
792
793 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
794 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
795
796 __set_bit(sw_prod, rxr->rx_agg_bmap);
797 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
798 cons_rx_buf = &rxr->rx_agg_ring[cons];
799
800 /* It is possible for sw_prod to be equal to cons, so
801 * set cons_rx_buf->page to NULL first.
802 */
803 page = cons_rx_buf->page;
804 cons_rx_buf->page = NULL;
805 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400806 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400807
808 prod_rx_buf->mapping = cons_rx_buf->mapping;
809
810 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
811
812 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
813 prod_bd->rx_bd_opaque = sw_prod;
814
815 prod = NEXT_RX_AGG(prod);
816 sw_prod = NEXT_RX_AGG(sw_prod);
817 cp_cons = NEXT_CMP(cp_cons);
818 }
819 rxr->rx_agg_prod = prod;
820 rxr->rx_sw_agg_prod = sw_prod;
821}
822
Michael Chanc61fb992017-02-06 16:55:36 -0500823static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
824 struct bnxt_rx_ring_info *rxr,
825 u16 cons, void *data, u8 *data_ptr,
826 dma_addr_t dma_addr,
827 unsigned int offset_and_len)
828{
829 unsigned int payload = offset_and_len >> 16;
830 unsigned int len = offset_and_len & 0xffff;
831 struct skb_frag_struct *frag;
832 struct page *page = data;
833 u16 prod = rxr->rx_prod;
834 struct sk_buff *skb;
835 int off, err;
836
837 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
838 if (unlikely(err)) {
839 bnxt_reuse_rx_data(rxr, cons, data);
840 return NULL;
841 }
842 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700843 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
844 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500845
846 if (unlikely(!payload))
847 payload = eth_get_headlen(data_ptr, len);
848
849 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
850 if (!skb) {
851 __free_page(page);
852 return NULL;
853 }
854
855 off = (void *)data_ptr - page_address(page);
856 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
857 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
858 payload + NET_IP_ALIGN);
859
860 frag = &skb_shinfo(skb)->frags[0];
861 skb_frag_size_sub(frag, payload);
862 frag->page_offset += payload;
863 skb->data_len -= payload;
864 skb->tail += payload;
865
866 return skb;
867}
868
Michael Chanc0c050c2015-10-22 16:01:17 -0400869static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
870 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500871 void *data, u8 *data_ptr,
872 dma_addr_t dma_addr,
873 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400874{
Michael Chan6bb19472017-02-06 16:55:32 -0500875 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400876 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500877 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400878
879 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
880 if (unlikely(err)) {
881 bnxt_reuse_rx_data(rxr, cons, data);
882 return NULL;
883 }
884
885 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700886 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
887 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400888 if (!skb) {
889 kfree(data);
890 return NULL;
891 }
892
Michael Chanb3dba772017-02-06 16:55:35 -0500893 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500894 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400895 return skb;
896}
897
898static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
899 struct sk_buff *skb, u16 cp_cons,
900 u32 agg_bufs)
901{
902 struct pci_dev *pdev = bp->pdev;
903 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500904 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400905 u16 prod = rxr->rx_agg_prod;
906 u32 i;
907
908 for (i = 0; i < agg_bufs; i++) {
909 u16 cons, frag_len;
910 struct rx_agg_cmp *agg;
911 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
912 struct page *page;
913 dma_addr_t mapping;
914
915 agg = (struct rx_agg_cmp *)
916 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
917 cons = agg->rx_agg_cmp_opaque;
918 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
919 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
920
921 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400922 skb_fill_page_desc(skb, i, cons_rx_buf->page,
923 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400924 __clear_bit(cons, rxr->rx_agg_bmap);
925
926 /* It is possible for bnxt_alloc_rx_page() to allocate
927 * a sw_prod index that equals the cons index, so we
928 * need to clear the cons entry now.
929 */
Michael Chan11cd1192017-02-06 16:55:33 -0500930 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400931 page = cons_rx_buf->page;
932 cons_rx_buf->page = NULL;
933
934 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
935 struct skb_shared_info *shinfo;
936 unsigned int nr_frags;
937
938 shinfo = skb_shinfo(skb);
939 nr_frags = --shinfo->nr_frags;
940 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
941
942 dev_kfree_skb(skb);
943
944 cons_rx_buf->page = page;
945
946 /* Update prod since possibly some pages have been
947 * allocated already.
948 */
949 rxr->rx_agg_prod = prod;
950 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
951 return NULL;
952 }
953
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700954 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
955 PCI_DMA_FROMDEVICE,
956 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400957
958 skb->data_len += frag_len;
959 skb->len += frag_len;
960 skb->truesize += PAGE_SIZE;
961
962 prod = NEXT_RX_AGG(prod);
963 cp_cons = NEXT_CMP(cp_cons);
964 }
965 rxr->rx_agg_prod = prod;
966 return skb;
967}
968
969static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
970 u8 agg_bufs, u32 *raw_cons)
971{
972 u16 last;
973 struct rx_agg_cmp *agg;
974
975 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
976 last = RING_CMP(*raw_cons);
977 agg = (struct rx_agg_cmp *)
978 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
979 return RX_AGG_CMP_VALID(agg, *raw_cons);
980}
981
982static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
983 unsigned int len,
984 dma_addr_t mapping)
985{
986 struct bnxt *bp = bnapi->bp;
987 struct pci_dev *pdev = bp->pdev;
988 struct sk_buff *skb;
989
990 skb = napi_alloc_skb(&bnapi->napi, len);
991 if (!skb)
992 return NULL;
993
Michael Chan745fc052017-02-06 16:55:34 -0500994 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
995 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400996
Michael Chan6bb19472017-02-06 16:55:32 -0500997 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
998 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400999
Michael Chan745fc052017-02-06 16:55:34 -05001000 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1001 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001002
1003 skb_put(skb, len);
1004 return skb;
1005}
1006
Michael Chanfa7e2812016-05-10 19:18:00 -04001007static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1008 u32 *raw_cons, void *cmp)
1009{
1010 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1011 struct rx_cmp *rxcmp = cmp;
1012 u32 tmp_raw_cons = *raw_cons;
1013 u8 cmp_type, agg_bufs = 0;
1014
1015 cmp_type = RX_CMP_TYPE(rxcmp);
1016
1017 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1018 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1019 RX_CMP_AGG_BUFS) >>
1020 RX_CMP_AGG_BUFS_SHIFT;
1021 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1022 struct rx_tpa_end_cmp *tpa_end = cmp;
1023
1024 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1025 RX_TPA_END_CMP_AGG_BUFS) >>
1026 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1027 }
1028
1029 if (agg_bufs) {
1030 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1031 return -EBUSY;
1032 }
1033 *raw_cons = tmp_raw_cons;
1034 return 0;
1035}
1036
Michael Chanc213eae2017-10-13 21:09:29 -04001037static void bnxt_queue_sp_work(struct bnxt *bp)
1038{
1039 if (BNXT_PF(bp))
1040 queue_work(bnxt_pf_wq, &bp->sp_task);
1041 else
1042 schedule_work(&bp->sp_task);
1043}
1044
1045static void bnxt_cancel_sp_work(struct bnxt *bp)
1046{
1047 if (BNXT_PF(bp))
1048 flush_workqueue(bnxt_pf_wq);
1049 else
1050 cancel_work_sync(&bp->sp_task);
1051}
1052
Michael Chanfa7e2812016-05-10 19:18:00 -04001053static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1054{
1055 if (!rxr->bnapi->in_reset) {
1056 rxr->bnapi->in_reset = true;
1057 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001058 bnxt_queue_sp_work(bp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001059 }
1060 rxr->rx_next_cons = 0xffff;
1061}
1062
Michael Chanc0c050c2015-10-22 16:01:17 -04001063static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1064 struct rx_tpa_start_cmp *tpa_start,
1065 struct rx_tpa_start_cmp_ext *tpa_start1)
1066{
1067 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1068 u16 cons, prod;
1069 struct bnxt_tpa_info *tpa_info;
1070 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1071 struct rx_bd *prod_bd;
1072 dma_addr_t mapping;
1073
1074 cons = tpa_start->rx_tpa_start_cmp_opaque;
1075 prod = rxr->rx_prod;
1076 cons_rx_buf = &rxr->rx_buf_ring[cons];
1077 prod_rx_buf = &rxr->rx_buf_ring[prod];
1078 tpa_info = &rxr->rx_tpa[agg_id];
1079
Michael Chanfa7e2812016-05-10 19:18:00 -04001080 if (unlikely(cons != rxr->rx_next_cons)) {
1081 bnxt_sched_reset(bp, rxr);
1082 return;
1083 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001084 /* Store cfa_code in tpa_info to use in tpa_end
1085 * completion processing.
1086 */
1087 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001088 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001089 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001090
1091 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001092 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001093
1094 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1095
1096 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1097
1098 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001099 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001100 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001101 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001102
1103 tpa_info->len =
1104 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1105 RX_TPA_START_CMP_LEN_SHIFT;
1106 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1107 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1108
1109 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1110 tpa_info->gso_type = SKB_GSO_TCPV4;
1111 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1112 if (hash_type == 3)
1113 tpa_info->gso_type = SKB_GSO_TCPV6;
1114 tpa_info->rss_hash =
1115 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1116 } else {
1117 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1118 tpa_info->gso_type = 0;
1119 if (netif_msg_rx_err(bp))
1120 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1121 }
1122 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1123 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001124 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001125
1126 rxr->rx_prod = NEXT_RX(prod);
1127 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001128 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001129 cons_rx_buf = &rxr->rx_buf_ring[cons];
1130
1131 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1132 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1133 cons_rx_buf->data = NULL;
1134}
1135
1136static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1137 u16 cp_cons, u32 agg_bufs)
1138{
1139 if (agg_bufs)
1140 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1141}
1142
Michael Chan94758f82016-06-13 02:25:35 -04001143static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1144 int payload_off, int tcp_ts,
1145 struct sk_buff *skb)
1146{
1147#ifdef CONFIG_INET
1148 struct tcphdr *th;
1149 int len, nw_off;
1150 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1151 u32 hdr_info = tpa_info->hdr_info;
1152 bool loopback = false;
1153
1154 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1155 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1156 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1157
1158 /* If the packet is an internal loopback packet, the offsets will
1159 * have an extra 4 bytes.
1160 */
1161 if (inner_mac_off == 4) {
1162 loopback = true;
1163 } else if (inner_mac_off > 4) {
1164 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1165 ETH_HLEN - 2));
1166
1167 /* We only support inner iPv4/ipv6. If we don't see the
1168 * correct protocol ID, it must be a loopback packet where
1169 * the offsets are off by 4.
1170 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001171 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001172 loopback = true;
1173 }
1174 if (loopback) {
1175 /* internal loopback packet, subtract all offsets by 4 */
1176 inner_ip_off -= 4;
1177 inner_mac_off -= 4;
1178 outer_ip_off -= 4;
1179 }
1180
1181 nw_off = inner_ip_off - ETH_HLEN;
1182 skb_set_network_header(skb, nw_off);
1183 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1184 struct ipv6hdr *iph = ipv6_hdr(skb);
1185
1186 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1187 len = skb->len - skb_transport_offset(skb);
1188 th = tcp_hdr(skb);
1189 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1190 } else {
1191 struct iphdr *iph = ip_hdr(skb);
1192
1193 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1194 len = skb->len - skb_transport_offset(skb);
1195 th = tcp_hdr(skb);
1196 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1197 }
1198
1199 if (inner_mac_off) { /* tunnel */
1200 struct udphdr *uh = NULL;
1201 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1202 ETH_HLEN - 2));
1203
1204 if (proto == htons(ETH_P_IP)) {
1205 struct iphdr *iph = (struct iphdr *)skb->data;
1206
1207 if (iph->protocol == IPPROTO_UDP)
1208 uh = (struct udphdr *)(iph + 1);
1209 } else {
1210 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1211
1212 if (iph->nexthdr == IPPROTO_UDP)
1213 uh = (struct udphdr *)(iph + 1);
1214 }
1215 if (uh) {
1216 if (uh->check)
1217 skb_shinfo(skb)->gso_type |=
1218 SKB_GSO_UDP_TUNNEL_CSUM;
1219 else
1220 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1221 }
1222 }
1223#endif
1224 return skb;
1225}
1226
Michael Chanc0c050c2015-10-22 16:01:17 -04001227#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1228#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1229
Michael Chan309369c2016-06-13 02:25:34 -04001230static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1231 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001232 struct sk_buff *skb)
1233{
Michael Chand1611c32015-10-25 22:27:57 -04001234#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001235 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001236 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001237
Michael Chan309369c2016-06-13 02:25:34 -04001238 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001239 tcp_opt_len = 12;
1240
Michael Chanc0c050c2015-10-22 16:01:17 -04001241 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1242 struct iphdr *iph;
1243
1244 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1245 ETH_HLEN;
1246 skb_set_network_header(skb, nw_off);
1247 iph = ip_hdr(skb);
1248 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1249 len = skb->len - skb_transport_offset(skb);
1250 th = tcp_hdr(skb);
1251 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1252 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1253 struct ipv6hdr *iph;
1254
1255 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1256 ETH_HLEN;
1257 skb_set_network_header(skb, nw_off);
1258 iph = ipv6_hdr(skb);
1259 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1260 len = skb->len - skb_transport_offset(skb);
1261 th = tcp_hdr(skb);
1262 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1263 } else {
1264 dev_kfree_skb_any(skb);
1265 return NULL;
1266 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001267
1268 if (nw_off) { /* tunnel */
1269 struct udphdr *uh = NULL;
1270
1271 if (skb->protocol == htons(ETH_P_IP)) {
1272 struct iphdr *iph = (struct iphdr *)skb->data;
1273
1274 if (iph->protocol == IPPROTO_UDP)
1275 uh = (struct udphdr *)(iph + 1);
1276 } else {
1277 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1278
1279 if (iph->nexthdr == IPPROTO_UDP)
1280 uh = (struct udphdr *)(iph + 1);
1281 }
1282 if (uh) {
1283 if (uh->check)
1284 skb_shinfo(skb)->gso_type |=
1285 SKB_GSO_UDP_TUNNEL_CSUM;
1286 else
1287 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1288 }
1289 }
1290#endif
1291 return skb;
1292}
1293
Michael Chan309369c2016-06-13 02:25:34 -04001294static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1295 struct bnxt_tpa_info *tpa_info,
1296 struct rx_tpa_end_cmp *tpa_end,
1297 struct rx_tpa_end_cmp_ext *tpa_end1,
1298 struct sk_buff *skb)
1299{
1300#ifdef CONFIG_INET
1301 int payload_off;
1302 u16 segs;
1303
1304 segs = TPA_END_TPA_SEGS(tpa_end);
1305 if (segs == 1)
1306 return skb;
1307
1308 NAPI_GRO_CB(skb)->count = segs;
1309 skb_shinfo(skb)->gso_size =
1310 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1311 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1312 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1313 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1314 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1315 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001316 if (likely(skb))
1317 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001318#endif
1319 return skb;
1320}
1321
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001322/* Given the cfa_code of a received packet determine which
1323 * netdev (vf-rep or PF) the packet is destined to.
1324 */
1325static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1326{
1327 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1328
1329 /* if vf-rep dev is NULL, the must belongs to the PF */
1330 return dev ? dev : bp->dev;
1331}
1332
Michael Chanc0c050c2015-10-22 16:01:17 -04001333static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1334 struct bnxt_napi *bnapi,
1335 u32 *raw_cons,
1336 struct rx_tpa_end_cmp *tpa_end,
1337 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001338 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001339{
1340 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001341 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001342 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001343 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001344 u16 cp_cons = RING_CMP(*raw_cons);
1345 unsigned int len;
1346 struct bnxt_tpa_info *tpa_info;
1347 dma_addr_t mapping;
1348 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001349 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001350
Michael Chanfa7e2812016-05-10 19:18:00 -04001351 if (unlikely(bnapi->in_reset)) {
1352 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1353
1354 if (rc < 0)
1355 return ERR_PTR(-EBUSY);
1356 return NULL;
1357 }
1358
Michael Chanc0c050c2015-10-22 16:01:17 -04001359 tpa_info = &rxr->rx_tpa[agg_id];
1360 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001361 data_ptr = tpa_info->data_ptr;
1362 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001363 len = tpa_info->len;
1364 mapping = tpa_info->mapping;
1365
1366 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1367 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1368
1369 if (agg_bufs) {
1370 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1371 return ERR_PTR(-EBUSY);
1372
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001373 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001374 cp_cons = NEXT_CMP(cp_cons);
1375 }
1376
Michael Chan69c149e2017-06-23 14:01:00 -04001377 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001378 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001379 if (agg_bufs > MAX_SKB_FRAGS)
1380 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1381 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001382 return NULL;
1383 }
1384
1385 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001386 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001387 if (!skb) {
1388 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1389 return NULL;
1390 }
1391 } else {
1392 u8 *new_data;
1393 dma_addr_t new_mapping;
1394
1395 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1396 if (!new_data) {
1397 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1398 return NULL;
1399 }
1400
1401 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001402 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001403 tpa_info->mapping = new_mapping;
1404
1405 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001406 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1407 bp->rx_buf_use_size, bp->rx_dir,
1408 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001409
1410 if (!skb) {
1411 kfree(data);
1412 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1413 return NULL;
1414 }
Michael Chanb3dba772017-02-06 16:55:35 -05001415 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001416 skb_put(skb, len);
1417 }
1418
1419 if (agg_bufs) {
1420 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1421 if (!skb) {
1422 /* Page reuse already handled by bnxt_rx_pages(). */
1423 return NULL;
1424 }
1425 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001426
1427 skb->protocol =
1428 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001429
1430 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1431 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1432
Michael Chan8852ddb2016-06-06 02:37:16 -04001433 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1434 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001435 u16 vlan_proto = tpa_info->metadata >>
1436 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001437 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001438
Michael Chan8852ddb2016-06-06 02:37:16 -04001439 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001440 }
1441
1442 skb_checksum_none_assert(skb);
1443 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1444 skb->ip_summed = CHECKSUM_UNNECESSARY;
1445 skb->csum_level =
1446 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1447 }
1448
1449 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001450 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001451
1452 return skb;
1453}
1454
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001455static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1456 struct sk_buff *skb)
1457{
1458 if (skb->dev != bp->dev) {
1459 /* this packet belongs to a vf-rep */
1460 bnxt_vf_rep_rx(bp, skb);
1461 return;
1462 }
1463 skb_record_rx_queue(skb, bnapi->index);
1464 napi_gro_receive(&bnapi->napi, skb);
1465}
1466
Michael Chanc0c050c2015-10-22 16:01:17 -04001467/* returns the following:
1468 * 1 - 1 packet successfully received
1469 * 0 - successful TPA_START, packet not completed yet
1470 * -EBUSY - completion ring does not have all the agg buffers yet
1471 * -ENOMEM - packet aborted due to out of memory
1472 * -EIO - packet aborted due to hw error indicated in BD
1473 */
1474static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001475 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001476{
1477 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001478 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001479 struct net_device *dev = bp->dev;
1480 struct rx_cmp *rxcmp;
1481 struct rx_cmp_ext *rxcmp1;
1482 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001483 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001484 struct bnxt_sw_rx_bd *rx_buf;
1485 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001486 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001487 dma_addr_t dma_addr;
1488 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001489 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001490 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001491 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001492
1493 rxcmp = (struct rx_cmp *)
1494 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1495
1496 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1497 cp_cons = RING_CMP(tmp_raw_cons);
1498 rxcmp1 = (struct rx_cmp_ext *)
1499 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1500
1501 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1502 return -EBUSY;
1503
1504 cmp_type = RX_CMP_TYPE(rxcmp);
1505
1506 prod = rxr->rx_prod;
1507
1508 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1509 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1510 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1511
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001512 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001513 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001514
1515 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1516 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1517 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001518 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001519
Tobias Klauser1fac4b22017-09-26 15:12:26 +02001520 if (IS_ERR(skb))
Michael Chanc0c050c2015-10-22 16:01:17 -04001521 return -EBUSY;
1522
1523 rc = -ENOMEM;
1524 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001525 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001526 rc = 1;
1527 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001528 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001529 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001530 }
1531
1532 cons = rxcmp->rx_cmp_opaque;
1533 rx_buf = &rxr->rx_buf_ring[cons];
1534 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001535 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001536 if (unlikely(cons != rxr->rx_next_cons)) {
1537 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1538
1539 bnxt_sched_reset(bp, rxr);
1540 return rc1;
1541 }
Michael Chan6bb19472017-02-06 16:55:32 -05001542 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001543
Michael Chanc61fb992017-02-06 16:55:36 -05001544 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1545 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001546
1547 if (agg_bufs) {
1548 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1549 return -EBUSY;
1550
1551 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001552 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001553 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001554 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001555
1556 rx_buf->data = NULL;
1557 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1558 bnxt_reuse_rx_data(rxr, cons, data);
1559 if (agg_bufs)
1560 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1561
1562 rc = -EIO;
1563 goto next_rx;
1564 }
1565
1566 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001567 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001568
Michael Chanc6d30e82017-02-06 16:55:42 -05001569 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1570 rc = 1;
1571 goto next_rx;
1572 }
1573
Michael Chanc0c050c2015-10-22 16:01:17 -04001574 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001575 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001576 bnxt_reuse_rx_data(rxr, cons, data);
1577 if (!skb) {
1578 rc = -ENOMEM;
1579 goto next_rx;
1580 }
1581 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001582 u32 payload;
1583
Michael Chanc6d30e82017-02-06 16:55:42 -05001584 if (rx_buf->data_ptr == data_ptr)
1585 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1586 else
1587 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001588 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001589 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001590 if (!skb) {
1591 rc = -ENOMEM;
1592 goto next_rx;
1593 }
1594 }
1595
1596 if (agg_bufs) {
1597 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1598 if (!skb) {
1599 rc = -ENOMEM;
1600 goto next_rx;
1601 }
1602 }
1603
1604 if (RX_CMP_HASH_VALID(rxcmp)) {
1605 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1606 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1607
1608 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1609 if (hash_type != 1 && hash_type != 3)
1610 type = PKT_HASH_TYPE_L3;
1611 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1612 }
1613
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001614 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1615 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001616
Michael Chan8852ddb2016-06-06 02:37:16 -04001617 if ((rxcmp1->rx_cmp_flags2 &
1618 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1619 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001620 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001621 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001622 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1623
Michael Chan8852ddb2016-06-06 02:37:16 -04001624 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001625 }
1626
1627 skb_checksum_none_assert(skb);
1628 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1629 if (dev->features & NETIF_F_RXCSUM) {
1630 skb->ip_summed = CHECKSUM_UNNECESSARY;
1631 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1632 }
1633 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001634 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1635 if (dev->features & NETIF_F_RXCSUM)
1636 cpr->rx_l4_csum_errors++;
1637 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001638 }
1639
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001640 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001641 rc = 1;
1642
1643next_rx:
1644 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001645 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001646
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001647 cpr->rx_packets += 1;
1648 cpr->rx_bytes += len;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001649
1650next_rx_no_prod_no_len:
Michael Chanc0c050c2015-10-22 16:01:17 -04001651 *raw_cons = tmp_raw_cons;
1652
1653 return rc;
1654}
1655
Michael Chan2270bc52017-06-23 14:01:01 -04001656/* In netpoll mode, if we are using a combined completion ring, we need to
1657 * discard the rx packets and recycle the buffers.
1658 */
1659static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1660 u32 *raw_cons, u8 *event)
1661{
1662 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1663 u32 tmp_raw_cons = *raw_cons;
1664 struct rx_cmp_ext *rxcmp1;
1665 struct rx_cmp *rxcmp;
1666 u16 cp_cons;
1667 u8 cmp_type;
1668
1669 cp_cons = RING_CMP(tmp_raw_cons);
1670 rxcmp = (struct rx_cmp *)
1671 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1672
1673 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1674 cp_cons = RING_CMP(tmp_raw_cons);
1675 rxcmp1 = (struct rx_cmp_ext *)
1676 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1677
1678 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1679 return -EBUSY;
1680
1681 cmp_type = RX_CMP_TYPE(rxcmp);
1682 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1683 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1684 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1685 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1686 struct rx_tpa_end_cmp_ext *tpa_end1;
1687
1688 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1689 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1690 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1691 }
1692 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1693}
1694
Michael Chan4bb13ab2016-04-05 14:09:01 -04001695#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001696 ((data) & \
1697 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001698
Michael Chanc0c050c2015-10-22 16:01:17 -04001699static int bnxt_async_event_process(struct bnxt *bp,
1700 struct hwrm_async_event_cmpl *cmpl)
1701{
1702 u16 event_id = le16_to_cpu(cmpl->event_id);
1703
1704 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1705 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001706 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001707 u32 data1 = le32_to_cpu(cmpl->event_data1);
1708 struct bnxt_link_info *link_info = &bp->link_info;
1709
1710 if (BNXT_VF(bp))
1711 goto async_event_process_exit;
Michael Chana8168b62017-12-06 17:31:22 -05001712
1713 /* print unsupported speed warning in forced speed mode only */
1714 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1715 (data1 & 0x20000)) {
Michael Chan8cbde112016-04-11 04:11:14 -04001716 u16 fw_speed = link_info->force_link_speed;
1717 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1718
Michael Chana8168b62017-12-06 17:31:22 -05001719 if (speed != SPEED_UNKNOWN)
1720 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1721 speed);
Michael Chan8cbde112016-04-11 04:11:14 -04001722 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001723 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001724 /* fall thru */
1725 }
Michael Chan87c374d2016-12-02 21:17:16 -05001726 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001727 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001728 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001729 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001730 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001731 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001732 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001733 u32 data1 = le32_to_cpu(cmpl->event_data1);
1734 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1735
1736 if (BNXT_VF(bp))
1737 break;
1738
1739 if (bp->pf.port_id != port_id)
1740 break;
1741
Michael Chan4bb13ab2016-04-05 14:09:01 -04001742 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1743 break;
1744 }
Michael Chan87c374d2016-12-02 21:17:16 -05001745 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001746 if (BNXT_PF(bp))
1747 goto async_event_process_exit;
1748 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1749 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001750 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001751 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001752 }
Michael Chanc213eae2017-10-13 21:09:29 -04001753 bnxt_queue_sp_work(bp);
Jeffrey Huang19241362016-02-26 04:00:00 -05001754async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001755 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001756 return 0;
1757}
1758
1759static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1760{
1761 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1762 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1763 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1764 (struct hwrm_fwd_req_cmpl *)txcmp;
1765
1766 switch (cmpl_type) {
1767 case CMPL_BASE_TYPE_HWRM_DONE:
1768 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1769 if (seq_id == bp->hwrm_intr_seq_id)
1770 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1771 else
1772 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1773 break;
1774
1775 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1776 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1777
1778 if ((vf_id < bp->pf.first_vf_id) ||
1779 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1780 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1781 vf_id);
1782 return -EINVAL;
1783 }
1784
1785 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1786 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001787 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04001788 break;
1789
1790 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1791 bnxt_async_event_process(bp,
1792 (struct hwrm_async_event_cmpl *)txcmp);
1793
1794 default:
1795 break;
1796 }
1797
1798 return 0;
1799}
1800
1801static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1802{
1803 struct bnxt_napi *bnapi = dev_instance;
1804 struct bnxt *bp = bnapi->bp;
1805 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1806 u32 cons = RING_CMP(cpr->cp_raw_cons);
1807
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001808 cpr->event_ctr++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001809 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1810 napi_schedule(&bnapi->napi);
1811 return IRQ_HANDLED;
1812}
1813
1814static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1815{
1816 u32 raw_cons = cpr->cp_raw_cons;
1817 u16 cons = RING_CMP(raw_cons);
1818 struct tx_cmp *txcmp;
1819
1820 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1821
1822 return TX_CMP_VALID(txcmp, raw_cons);
1823}
1824
Michael Chanc0c050c2015-10-22 16:01:17 -04001825static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1826{
1827 struct bnxt_napi *bnapi = dev_instance;
1828 struct bnxt *bp = bnapi->bp;
1829 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1830 u32 cons = RING_CMP(cpr->cp_raw_cons);
1831 u32 int_status;
1832
1833 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1834
1835 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001836 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001837 /* return if erroneous interrupt */
1838 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1839 return IRQ_NONE;
1840 }
1841
1842 /* disable ring IRQ */
1843 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1844
1845 /* Return here if interrupt is shared and is disabled. */
1846 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1847 return IRQ_HANDLED;
1848
1849 napi_schedule(&bnapi->napi);
1850 return IRQ_HANDLED;
1851}
1852
1853static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1854{
1855 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1856 u32 raw_cons = cpr->cp_raw_cons;
1857 u32 cons;
1858 int tx_pkts = 0;
1859 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001860 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001861 struct tx_cmp *txcmp;
1862
1863 while (1) {
1864 int rc;
1865
1866 cons = RING_CMP(raw_cons);
1867 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1868
1869 if (!TX_CMP_VALID(txcmp, raw_cons))
1870 break;
1871
Michael Chan67a95e22016-05-04 16:56:43 -04001872 /* The valid test of the entry must be done first before
1873 * reading any further.
1874 */
Michael Chanb67daab2016-05-15 03:04:51 -04001875 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001876 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1877 tx_pkts++;
1878 /* return full budget so NAPI will complete. */
1879 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1880 rx_pkts = budget;
1881 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001882 if (likely(budget))
1883 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1884 else
1885 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1886 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001887 if (likely(rc >= 0))
1888 rx_pkts += rc;
Michael Chan903649e2017-08-28 13:40:30 -04001889 /* Increment rx_pkts when rc is -ENOMEM to count towards
1890 * the NAPI budget. Otherwise, we may potentially loop
1891 * here forever if we consistently cannot allocate
1892 * buffers.
1893 */
Calvin Owens2edbdb32017-12-08 09:05:26 -08001894 else if (rc == -ENOMEM && budget)
Michael Chan903649e2017-08-28 13:40:30 -04001895 rx_pkts++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001896 else if (rc == -EBUSY) /* partial completion */
1897 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001898 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1899 CMPL_BASE_TYPE_HWRM_DONE) ||
1900 (TX_CMP_TYPE(txcmp) ==
1901 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1902 (TX_CMP_TYPE(txcmp) ==
1903 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1904 bnxt_hwrm_handler(bp, txcmp);
1905 }
1906 raw_cons = NEXT_RAW_CMP(raw_cons);
1907
1908 if (rx_pkts == budget)
1909 break;
1910 }
1911
Michael Chan38413402017-02-06 16:55:43 -05001912 if (event & BNXT_TX_EVENT) {
1913 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1914 void __iomem *db = txr->tx_doorbell;
1915 u16 prod = txr->tx_prod;
1916
1917 /* Sync BD data before updating doorbell */
1918 wmb();
1919
Michael Chan434c9752017-05-29 19:06:08 -04001920 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001921 }
1922
Michael Chanc0c050c2015-10-22 16:01:17 -04001923 cpr->cp_raw_cons = raw_cons;
1924 /* ACK completion ring before freeing tx ring and producing new
1925 * buffers in rx/agg rings to prevent overflowing the completion
1926 * ring.
1927 */
1928 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1929
1930 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001931 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001932
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001933 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001934 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001935
Michael Chan434c9752017-05-29 19:06:08 -04001936 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1937 if (event & BNXT_AGG_EVENT)
1938 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1939 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001940 }
1941 return rx_pkts;
1942}
1943
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001944static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1945{
1946 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1947 struct bnxt *bp = bnapi->bp;
1948 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1949 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1950 struct tx_cmp *txcmp;
1951 struct rx_cmp_ext *rxcmp1;
1952 u32 cp_cons, tmp_raw_cons;
1953 u32 raw_cons = cpr->cp_raw_cons;
1954 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001955 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001956
1957 while (1) {
1958 int rc;
1959
1960 cp_cons = RING_CMP(raw_cons);
1961 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1962
1963 if (!TX_CMP_VALID(txcmp, raw_cons))
1964 break;
1965
1966 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1967 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1968 cp_cons = RING_CMP(tmp_raw_cons);
1969 rxcmp1 = (struct rx_cmp_ext *)
1970 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1971
1972 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1973 break;
1974
1975 /* force an error to recycle the buffer */
1976 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1977 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1978
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001979 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Calvin Owens2edbdb32017-12-08 09:05:26 -08001980 if (likely(rc == -EIO) && budget)
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001981 rx_pkts++;
1982 else if (rc == -EBUSY) /* partial completion */
1983 break;
1984 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1985 CMPL_BASE_TYPE_HWRM_DONE)) {
1986 bnxt_hwrm_handler(bp, txcmp);
1987 } else {
1988 netdev_err(bp->dev,
1989 "Invalid completion received on special ring\n");
1990 }
1991 raw_cons = NEXT_RAW_CMP(raw_cons);
1992
1993 if (rx_pkts == budget)
1994 break;
1995 }
1996
1997 cpr->cp_raw_cons = raw_cons;
1998 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001999 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002000
Michael Chan434c9752017-05-29 19:06:08 -04002001 if (event & BNXT_AGG_EVENT)
2002 bnxt_db_write(bp, rxr->rx_agg_doorbell,
2003 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002004
2005 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08002006 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002007 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2008 }
2009 return rx_pkts;
2010}
2011
Michael Chanc0c050c2015-10-22 16:01:17 -04002012static int bnxt_poll(struct napi_struct *napi, int budget)
2013{
2014 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2015 struct bnxt *bp = bnapi->bp;
2016 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2017 int work_done = 0;
2018
Michael Chanc0c050c2015-10-22 16:01:17 -04002019 while (1) {
2020 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2021
2022 if (work_done >= budget)
2023 break;
2024
2025 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05002026 if (napi_complete_done(napi, work_done))
2027 BNXT_CP_DB_REARM(cpr->cp_doorbell,
2028 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002029 break;
2030 }
2031 }
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002032 if (bp->flags & BNXT_FLAG_DIM) {
2033 struct net_dim_sample dim_sample;
2034
2035 net_dim_sample(cpr->event_ctr,
2036 cpr->rx_packets,
2037 cpr->rx_bytes,
2038 &dim_sample);
2039 net_dim(&cpr->dim, dim_sample);
2040 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002041 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04002042 return work_done;
2043}
2044
Michael Chanc0c050c2015-10-22 16:01:17 -04002045static void bnxt_free_tx_skbs(struct bnxt *bp)
2046{
2047 int i, max_idx;
2048 struct pci_dev *pdev = bp->pdev;
2049
Michael Chanb6ab4b02016-01-02 23:44:59 -05002050 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002051 return;
2052
2053 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2054 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002055 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002056 int j;
2057
Michael Chanc0c050c2015-10-22 16:01:17 -04002058 for (j = 0; j < max_idx;) {
2059 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2060 struct sk_buff *skb = tx_buf->skb;
2061 int k, last;
2062
2063 if (!skb) {
2064 j++;
2065 continue;
2066 }
2067
2068 tx_buf->skb = NULL;
2069
2070 if (tx_buf->is_push) {
2071 dev_kfree_skb(skb);
2072 j += 2;
2073 continue;
2074 }
2075
2076 dma_unmap_single(&pdev->dev,
2077 dma_unmap_addr(tx_buf, mapping),
2078 skb_headlen(skb),
2079 PCI_DMA_TODEVICE);
2080
2081 last = tx_buf->nr_frags;
2082 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002083 for (k = 0; k < last; k++, j++) {
2084 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002085 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2086
Michael Chand612a572016-01-28 03:11:22 -05002087 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002088 dma_unmap_page(
2089 &pdev->dev,
2090 dma_unmap_addr(tx_buf, mapping),
2091 skb_frag_size(frag), PCI_DMA_TODEVICE);
2092 }
2093 dev_kfree_skb(skb);
2094 }
2095 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2096 }
2097}
2098
2099static void bnxt_free_rx_skbs(struct bnxt *bp)
2100{
2101 int i, max_idx, max_agg_idx;
2102 struct pci_dev *pdev = bp->pdev;
2103
Michael Chanb6ab4b02016-01-02 23:44:59 -05002104 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002105 return;
2106
2107 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2108 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2109 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002110 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002111 int j;
2112
Michael Chanc0c050c2015-10-22 16:01:17 -04002113 if (rxr->rx_tpa) {
2114 for (j = 0; j < MAX_TPA; j++) {
2115 struct bnxt_tpa_info *tpa_info =
2116 &rxr->rx_tpa[j];
2117 u8 *data = tpa_info->data;
2118
2119 if (!data)
2120 continue;
2121
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002122 dma_unmap_single_attrs(&pdev->dev,
2123 tpa_info->mapping,
2124 bp->rx_buf_use_size,
2125 bp->rx_dir,
2126 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002127
2128 tpa_info->data = NULL;
2129
2130 kfree(data);
2131 }
2132 }
2133
2134 for (j = 0; j < max_idx; j++) {
2135 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002136 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002137 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002138
2139 if (!data)
2140 continue;
2141
Michael Chanc0c050c2015-10-22 16:01:17 -04002142 rx_buf->data = NULL;
2143
Michael Chan3ed3a832017-03-28 19:47:31 -04002144 if (BNXT_RX_PAGE_MODE(bp)) {
2145 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002146 dma_unmap_page_attrs(&pdev->dev, mapping,
2147 PAGE_SIZE, bp->rx_dir,
2148 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002149 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002150 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002151 dma_unmap_single_attrs(&pdev->dev, mapping,
2152 bp->rx_buf_use_size,
2153 bp->rx_dir,
2154 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002155 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002156 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002157 }
2158
2159 for (j = 0; j < max_agg_idx; j++) {
2160 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2161 &rxr->rx_agg_ring[j];
2162 struct page *page = rx_agg_buf->page;
2163
2164 if (!page)
2165 continue;
2166
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002167 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2168 BNXT_RX_PAGE_SIZE,
2169 PCI_DMA_FROMDEVICE,
2170 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002171
2172 rx_agg_buf->page = NULL;
2173 __clear_bit(j, rxr->rx_agg_bmap);
2174
2175 __free_page(page);
2176 }
Michael Chan89d0a062016-04-25 02:30:51 -04002177 if (rxr->rx_page) {
2178 __free_page(rxr->rx_page);
2179 rxr->rx_page = NULL;
2180 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002181 }
2182}
2183
2184static void bnxt_free_skbs(struct bnxt *bp)
2185{
2186 bnxt_free_tx_skbs(bp);
2187 bnxt_free_rx_skbs(bp);
2188}
2189
2190static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2191{
2192 struct pci_dev *pdev = bp->pdev;
2193 int i;
2194
2195 for (i = 0; i < ring->nr_pages; i++) {
2196 if (!ring->pg_arr[i])
2197 continue;
2198
2199 dma_free_coherent(&pdev->dev, ring->page_size,
2200 ring->pg_arr[i], ring->dma_arr[i]);
2201
2202 ring->pg_arr[i] = NULL;
2203 }
2204 if (ring->pg_tbl) {
2205 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2206 ring->pg_tbl, ring->pg_tbl_map);
2207 ring->pg_tbl = NULL;
2208 }
2209 if (ring->vmem_size && *ring->vmem) {
2210 vfree(*ring->vmem);
2211 *ring->vmem = NULL;
2212 }
2213}
2214
2215static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2216{
2217 int i;
2218 struct pci_dev *pdev = bp->pdev;
2219
2220 if (ring->nr_pages > 1) {
2221 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2222 ring->nr_pages * 8,
2223 &ring->pg_tbl_map,
2224 GFP_KERNEL);
2225 if (!ring->pg_tbl)
2226 return -ENOMEM;
2227 }
2228
2229 for (i = 0; i < ring->nr_pages; i++) {
2230 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2231 ring->page_size,
2232 &ring->dma_arr[i],
2233 GFP_KERNEL);
2234 if (!ring->pg_arr[i])
2235 return -ENOMEM;
2236
2237 if (ring->nr_pages > 1)
2238 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2239 }
2240
2241 if (ring->vmem_size) {
2242 *ring->vmem = vzalloc(ring->vmem_size);
2243 if (!(*ring->vmem))
2244 return -ENOMEM;
2245 }
2246 return 0;
2247}
2248
2249static void bnxt_free_rx_rings(struct bnxt *bp)
2250{
2251 int i;
2252
Michael Chanb6ab4b02016-01-02 23:44:59 -05002253 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002254 return;
2255
2256 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002257 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002258 struct bnxt_ring_struct *ring;
2259
Michael Chanc6d30e82017-02-06 16:55:42 -05002260 if (rxr->xdp_prog)
2261 bpf_prog_put(rxr->xdp_prog);
2262
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002263 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2264 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2265
Michael Chanc0c050c2015-10-22 16:01:17 -04002266 kfree(rxr->rx_tpa);
2267 rxr->rx_tpa = NULL;
2268
2269 kfree(rxr->rx_agg_bmap);
2270 rxr->rx_agg_bmap = NULL;
2271
2272 ring = &rxr->rx_ring_struct;
2273 bnxt_free_ring(bp, ring);
2274
2275 ring = &rxr->rx_agg_ring_struct;
2276 bnxt_free_ring(bp, ring);
2277 }
2278}
2279
2280static int bnxt_alloc_rx_rings(struct bnxt *bp)
2281{
2282 int i, rc, agg_rings = 0, tpa_rings = 0;
2283
Michael Chanb6ab4b02016-01-02 23:44:59 -05002284 if (!bp->rx_ring)
2285 return -ENOMEM;
2286
Michael Chanc0c050c2015-10-22 16:01:17 -04002287 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2288 agg_rings = 1;
2289
2290 if (bp->flags & BNXT_FLAG_TPA)
2291 tpa_rings = 1;
2292
2293 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002294 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002295 struct bnxt_ring_struct *ring;
2296
Michael Chanc0c050c2015-10-22 16:01:17 -04002297 ring = &rxr->rx_ring_struct;
2298
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002299 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2300 if (rc < 0)
2301 return rc;
2302
Michael Chanc0c050c2015-10-22 16:01:17 -04002303 rc = bnxt_alloc_ring(bp, ring);
2304 if (rc)
2305 return rc;
2306
2307 if (agg_rings) {
2308 u16 mem_size;
2309
2310 ring = &rxr->rx_agg_ring_struct;
2311 rc = bnxt_alloc_ring(bp, ring);
2312 if (rc)
2313 return rc;
2314
2315 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2316 mem_size = rxr->rx_agg_bmap_size / 8;
2317 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2318 if (!rxr->rx_agg_bmap)
2319 return -ENOMEM;
2320
2321 if (tpa_rings) {
2322 rxr->rx_tpa = kcalloc(MAX_TPA,
2323 sizeof(struct bnxt_tpa_info),
2324 GFP_KERNEL);
2325 if (!rxr->rx_tpa)
2326 return -ENOMEM;
2327 }
2328 }
2329 }
2330 return 0;
2331}
2332
2333static void bnxt_free_tx_rings(struct bnxt *bp)
2334{
2335 int i;
2336 struct pci_dev *pdev = bp->pdev;
2337
Michael Chanb6ab4b02016-01-02 23:44:59 -05002338 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002339 return;
2340
2341 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002342 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002343 struct bnxt_ring_struct *ring;
2344
Michael Chanc0c050c2015-10-22 16:01:17 -04002345 if (txr->tx_push) {
2346 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2347 txr->tx_push, txr->tx_push_mapping);
2348 txr->tx_push = NULL;
2349 }
2350
2351 ring = &txr->tx_ring_struct;
2352
2353 bnxt_free_ring(bp, ring);
2354 }
2355}
2356
2357static int bnxt_alloc_tx_rings(struct bnxt *bp)
2358{
2359 int i, j, rc;
2360 struct pci_dev *pdev = bp->pdev;
2361
2362 bp->tx_push_size = 0;
2363 if (bp->tx_push_thresh) {
2364 int push_size;
2365
2366 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2367 bp->tx_push_thresh);
2368
Michael Chan4419dbe2016-02-10 17:33:49 -05002369 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002370 push_size = 0;
2371 bp->tx_push_thresh = 0;
2372 }
2373
2374 bp->tx_push_size = push_size;
2375 }
2376
2377 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002378 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002379 struct bnxt_ring_struct *ring;
2380
Michael Chanc0c050c2015-10-22 16:01:17 -04002381 ring = &txr->tx_ring_struct;
2382
2383 rc = bnxt_alloc_ring(bp, ring);
2384 if (rc)
2385 return rc;
2386
2387 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002388 dma_addr_t mapping;
2389
2390 /* One pre-allocated DMA buffer to backup
2391 * TX push operation
2392 */
2393 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2394 bp->tx_push_size,
2395 &txr->tx_push_mapping,
2396 GFP_KERNEL);
2397
2398 if (!txr->tx_push)
2399 return -ENOMEM;
2400
Michael Chanc0c050c2015-10-22 16:01:17 -04002401 mapping = txr->tx_push_mapping +
2402 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002403 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002404
Michael Chan4419dbe2016-02-10 17:33:49 -05002405 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002406 }
2407 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002408 if (i < bp->tx_nr_rings_xdp)
2409 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002410 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2411 j++;
2412 }
2413 return 0;
2414}
2415
2416static void bnxt_free_cp_rings(struct bnxt *bp)
2417{
2418 int i;
2419
2420 if (!bp->bnapi)
2421 return;
2422
2423 for (i = 0; i < bp->cp_nr_rings; i++) {
2424 struct bnxt_napi *bnapi = bp->bnapi[i];
2425 struct bnxt_cp_ring_info *cpr;
2426 struct bnxt_ring_struct *ring;
2427
2428 if (!bnapi)
2429 continue;
2430
2431 cpr = &bnapi->cp_ring;
2432 ring = &cpr->cp_ring_struct;
2433
2434 bnxt_free_ring(bp, ring);
2435 }
2436}
2437
2438static int bnxt_alloc_cp_rings(struct bnxt *bp)
2439{
2440 int i, rc;
2441
2442 for (i = 0; i < bp->cp_nr_rings; i++) {
2443 struct bnxt_napi *bnapi = bp->bnapi[i];
2444 struct bnxt_cp_ring_info *cpr;
2445 struct bnxt_ring_struct *ring;
2446
2447 if (!bnapi)
2448 continue;
2449
2450 cpr = &bnapi->cp_ring;
2451 ring = &cpr->cp_ring_struct;
2452
2453 rc = bnxt_alloc_ring(bp, ring);
2454 if (rc)
2455 return rc;
2456 }
2457 return 0;
2458}
2459
2460static void bnxt_init_ring_struct(struct bnxt *bp)
2461{
2462 int i;
2463
2464 for (i = 0; i < bp->cp_nr_rings; i++) {
2465 struct bnxt_napi *bnapi = bp->bnapi[i];
2466 struct bnxt_cp_ring_info *cpr;
2467 struct bnxt_rx_ring_info *rxr;
2468 struct bnxt_tx_ring_info *txr;
2469 struct bnxt_ring_struct *ring;
2470
2471 if (!bnapi)
2472 continue;
2473
2474 cpr = &bnapi->cp_ring;
2475 ring = &cpr->cp_ring_struct;
2476 ring->nr_pages = bp->cp_nr_pages;
2477 ring->page_size = HW_CMPD_RING_SIZE;
2478 ring->pg_arr = (void **)cpr->cp_desc_ring;
2479 ring->dma_arr = cpr->cp_desc_mapping;
2480 ring->vmem_size = 0;
2481
Michael Chanb6ab4b02016-01-02 23:44:59 -05002482 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002483 if (!rxr)
2484 goto skip_rx;
2485
Michael Chanc0c050c2015-10-22 16:01:17 -04002486 ring = &rxr->rx_ring_struct;
2487 ring->nr_pages = bp->rx_nr_pages;
2488 ring->page_size = HW_RXBD_RING_SIZE;
2489 ring->pg_arr = (void **)rxr->rx_desc_ring;
2490 ring->dma_arr = rxr->rx_desc_mapping;
2491 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2492 ring->vmem = (void **)&rxr->rx_buf_ring;
2493
2494 ring = &rxr->rx_agg_ring_struct;
2495 ring->nr_pages = bp->rx_agg_nr_pages;
2496 ring->page_size = HW_RXBD_RING_SIZE;
2497 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2498 ring->dma_arr = rxr->rx_agg_desc_mapping;
2499 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2500 ring->vmem = (void **)&rxr->rx_agg_ring;
2501
Michael Chan3b2b7d92016-01-02 23:45:00 -05002502skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002503 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002504 if (!txr)
2505 continue;
2506
Michael Chanc0c050c2015-10-22 16:01:17 -04002507 ring = &txr->tx_ring_struct;
2508 ring->nr_pages = bp->tx_nr_pages;
2509 ring->page_size = HW_RXBD_RING_SIZE;
2510 ring->pg_arr = (void **)txr->tx_desc_ring;
2511 ring->dma_arr = txr->tx_desc_mapping;
2512 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2513 ring->vmem = (void **)&txr->tx_buf_ring;
2514 }
2515}
2516
2517static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2518{
2519 int i;
2520 u32 prod;
2521 struct rx_bd **rx_buf_ring;
2522
2523 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2524 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2525 int j;
2526 struct rx_bd *rxbd;
2527
2528 rxbd = rx_buf_ring[i];
2529 if (!rxbd)
2530 continue;
2531
2532 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2533 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2534 rxbd->rx_bd_opaque = prod;
2535 }
2536 }
2537}
2538
2539static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2540{
2541 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002542 struct bnxt_rx_ring_info *rxr;
2543 struct bnxt_ring_struct *ring;
2544 u32 prod, type;
2545 int i;
2546
Michael Chanc0c050c2015-10-22 16:01:17 -04002547 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2548 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2549
2550 if (NET_IP_ALIGN == 2)
2551 type |= RX_BD_FLAGS_SOP;
2552
Michael Chanb6ab4b02016-01-02 23:44:59 -05002553 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002554 ring = &rxr->rx_ring_struct;
2555 bnxt_init_rxbd_pages(ring, type);
2556
Michael Chanc6d30e82017-02-06 16:55:42 -05002557 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2558 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2559 if (IS_ERR(rxr->xdp_prog)) {
2560 int rc = PTR_ERR(rxr->xdp_prog);
2561
2562 rxr->xdp_prog = NULL;
2563 return rc;
2564 }
2565 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002566 prod = rxr->rx_prod;
2567 for (i = 0; i < bp->rx_ring_size; i++) {
2568 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2569 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2570 ring_nr, i, bp->rx_ring_size);
2571 break;
2572 }
2573 prod = NEXT_RX(prod);
2574 }
2575 rxr->rx_prod = prod;
2576 ring->fw_ring_id = INVALID_HW_RING_ID;
2577
Michael Chanedd0c2c2015-12-27 18:19:19 -05002578 ring = &rxr->rx_agg_ring_struct;
2579 ring->fw_ring_id = INVALID_HW_RING_ID;
2580
Michael Chanc0c050c2015-10-22 16:01:17 -04002581 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2582 return 0;
2583
Michael Chan2839f282016-04-25 02:30:50 -04002584 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002585 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2586
2587 bnxt_init_rxbd_pages(ring, type);
2588
2589 prod = rxr->rx_agg_prod;
2590 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2591 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2592 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2593 ring_nr, i, bp->rx_ring_size);
2594 break;
2595 }
2596 prod = NEXT_RX_AGG(prod);
2597 }
2598 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002599
2600 if (bp->flags & BNXT_FLAG_TPA) {
2601 if (rxr->rx_tpa) {
2602 u8 *data;
2603 dma_addr_t mapping;
2604
2605 for (i = 0; i < MAX_TPA; i++) {
2606 data = __bnxt_alloc_rx_data(bp, &mapping,
2607 GFP_KERNEL);
2608 if (!data)
2609 return -ENOMEM;
2610
2611 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002612 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002613 rxr->rx_tpa[i].mapping = mapping;
2614 }
2615 } else {
2616 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2617 return -ENOMEM;
2618 }
2619 }
2620
2621 return 0;
2622}
2623
Sankar Patchineelam22479252017-03-28 19:47:29 -04002624static void bnxt_init_cp_rings(struct bnxt *bp)
2625{
2626 int i;
2627
2628 for (i = 0; i < bp->cp_nr_rings; i++) {
2629 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2630 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2631
2632 ring->fw_ring_id = INVALID_HW_RING_ID;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002633 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2634 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
Sankar Patchineelam22479252017-03-28 19:47:29 -04002635 }
2636}
2637
Michael Chanc0c050c2015-10-22 16:01:17 -04002638static int bnxt_init_rx_rings(struct bnxt *bp)
2639{
2640 int i, rc = 0;
2641
Michael Chanc61fb992017-02-06 16:55:36 -05002642 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002643 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2644 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002645 } else {
2646 bp->rx_offset = BNXT_RX_OFFSET;
2647 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2648 }
Michael Chanb3dba772017-02-06 16:55:35 -05002649
Michael Chanc0c050c2015-10-22 16:01:17 -04002650 for (i = 0; i < bp->rx_nr_rings; i++) {
2651 rc = bnxt_init_one_rx_ring(bp, i);
2652 if (rc)
2653 break;
2654 }
2655
2656 return rc;
2657}
2658
2659static int bnxt_init_tx_rings(struct bnxt *bp)
2660{
2661 u16 i;
2662
2663 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2664 MAX_SKB_FRAGS + 1);
2665
2666 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002667 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002668 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2669
2670 ring->fw_ring_id = INVALID_HW_RING_ID;
2671 }
2672
2673 return 0;
2674}
2675
2676static void bnxt_free_ring_grps(struct bnxt *bp)
2677{
2678 kfree(bp->grp_info);
2679 bp->grp_info = NULL;
2680}
2681
2682static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2683{
2684 int i;
2685
2686 if (irq_re_init) {
2687 bp->grp_info = kcalloc(bp->cp_nr_rings,
2688 sizeof(struct bnxt_ring_grp_info),
2689 GFP_KERNEL);
2690 if (!bp->grp_info)
2691 return -ENOMEM;
2692 }
2693 for (i = 0; i < bp->cp_nr_rings; i++) {
2694 if (irq_re_init)
2695 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2696 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2697 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2698 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2699 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2700 }
2701 return 0;
2702}
2703
2704static void bnxt_free_vnics(struct bnxt *bp)
2705{
2706 kfree(bp->vnic_info);
2707 bp->vnic_info = NULL;
2708 bp->nr_vnics = 0;
2709}
2710
2711static int bnxt_alloc_vnics(struct bnxt *bp)
2712{
2713 int num_vnics = 1;
2714
2715#ifdef CONFIG_RFS_ACCEL
2716 if (bp->flags & BNXT_FLAG_RFS)
2717 num_vnics += bp->rx_nr_rings;
2718#endif
2719
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002720 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2721 num_vnics++;
2722
Michael Chanc0c050c2015-10-22 16:01:17 -04002723 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2724 GFP_KERNEL);
2725 if (!bp->vnic_info)
2726 return -ENOMEM;
2727
2728 bp->nr_vnics = num_vnics;
2729 return 0;
2730}
2731
2732static void bnxt_init_vnics(struct bnxt *bp)
2733{
2734 int i;
2735
2736 for (i = 0; i < bp->nr_vnics; i++) {
2737 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2738
2739 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002740 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2741 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002742 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2743
2744 if (bp->vnic_info[i].rss_hash_key) {
2745 if (i == 0)
2746 prandom_bytes(vnic->rss_hash_key,
2747 HW_HASH_KEY_SIZE);
2748 else
2749 memcpy(vnic->rss_hash_key,
2750 bp->vnic_info[0].rss_hash_key,
2751 HW_HASH_KEY_SIZE);
2752 }
2753 }
2754}
2755
2756static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2757{
2758 int pages;
2759
2760 pages = ring_size / desc_per_pg;
2761
2762 if (!pages)
2763 return 1;
2764
2765 pages++;
2766
2767 while (pages & (pages - 1))
2768 pages++;
2769
2770 return pages;
2771}
2772
Michael Chanc6d30e82017-02-06 16:55:42 -05002773void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002774{
2775 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002776 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2777 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002778 if (bp->dev->features & NETIF_F_LRO)
2779 bp->flags |= BNXT_FLAG_LRO;
Michael Chan1054aee2017-12-16 03:09:42 -05002780 else if (bp->dev->features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04002781 bp->flags |= BNXT_FLAG_GRO;
2782}
2783
2784/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2785 * be set on entry.
2786 */
2787void bnxt_set_ring_params(struct bnxt *bp)
2788{
2789 u32 ring_size, rx_size, rx_space;
2790 u32 agg_factor = 0, agg_ring_size = 0;
2791
2792 /* 8 for CRC and VLAN */
2793 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2794
2795 rx_space = rx_size + NET_SKB_PAD +
2796 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2797
2798 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2799 ring_size = bp->rx_ring_size;
2800 bp->rx_agg_ring_size = 0;
2801 bp->rx_agg_nr_pages = 0;
2802
2803 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002804 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002805
2806 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002807 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002808 u32 jumbo_factor;
2809
2810 bp->flags |= BNXT_FLAG_JUMBO;
2811 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2812 if (jumbo_factor > agg_factor)
2813 agg_factor = jumbo_factor;
2814 }
2815 agg_ring_size = ring_size * agg_factor;
2816
2817 if (agg_ring_size) {
2818 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2819 RX_DESC_CNT);
2820 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2821 u32 tmp = agg_ring_size;
2822
2823 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2824 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2825 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2826 tmp, agg_ring_size);
2827 }
2828 bp->rx_agg_ring_size = agg_ring_size;
2829 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2830 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2831 rx_space = rx_size + NET_SKB_PAD +
2832 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2833 }
2834
2835 bp->rx_buf_use_size = rx_size;
2836 bp->rx_buf_size = rx_space;
2837
2838 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2839 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2840
2841 ring_size = bp->tx_ring_size;
2842 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2843 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2844
2845 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2846 bp->cp_ring_size = ring_size;
2847
2848 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2849 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2850 bp->cp_nr_pages = MAX_CP_PAGES;
2851 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2852 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2853 ring_size, bp->cp_ring_size);
2854 }
2855 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2856 bp->cp_ring_mask = bp->cp_bit - 1;
2857}
2858
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002859/* Changing allocation mode of RX rings.
2860 * TODO: Update when extending xdp_rxq_info to support allocation modes.
2861 */
Michael Chanc61fb992017-02-06 16:55:36 -05002862int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002863{
Michael Chanc61fb992017-02-06 16:55:36 -05002864 if (page_mode) {
2865 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2866 return -EOPNOTSUPP;
Michael Chan7eb9bb32017-10-26 11:51:25 -04002867 bp->dev->max_mtu =
2868 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
Michael Chanc61fb992017-02-06 16:55:36 -05002869 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2870 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
Michael Chanc61fb992017-02-06 16:55:36 -05002871 bp->rx_dir = DMA_BIDIRECTIONAL;
2872 bp->rx_skb_func = bnxt_rx_page_skb;
Michael Chan1054aee2017-12-16 03:09:42 -05002873 /* Disable LRO or GRO_HW */
2874 netdev_update_features(bp->dev);
Michael Chanc61fb992017-02-06 16:55:36 -05002875 } else {
Michael Chan7eb9bb32017-10-26 11:51:25 -04002876 bp->dev->max_mtu = bp->max_mtu;
Michael Chanc61fb992017-02-06 16:55:36 -05002877 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2878 bp->rx_dir = DMA_FROM_DEVICE;
2879 bp->rx_skb_func = bnxt_rx_skb;
2880 }
Michael Chan6bb19472017-02-06 16:55:32 -05002881 return 0;
2882}
2883
Michael Chanc0c050c2015-10-22 16:01:17 -04002884static void bnxt_free_vnic_attributes(struct bnxt *bp)
2885{
2886 int i;
2887 struct bnxt_vnic_info *vnic;
2888 struct pci_dev *pdev = bp->pdev;
2889
2890 if (!bp->vnic_info)
2891 return;
2892
2893 for (i = 0; i < bp->nr_vnics; i++) {
2894 vnic = &bp->vnic_info[i];
2895
2896 kfree(vnic->fw_grp_ids);
2897 vnic->fw_grp_ids = NULL;
2898
2899 kfree(vnic->uc_list);
2900 vnic->uc_list = NULL;
2901
2902 if (vnic->mc_list) {
2903 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2904 vnic->mc_list, vnic->mc_list_mapping);
2905 vnic->mc_list = NULL;
2906 }
2907
2908 if (vnic->rss_table) {
2909 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2910 vnic->rss_table,
2911 vnic->rss_table_dma_addr);
2912 vnic->rss_table = NULL;
2913 }
2914
2915 vnic->rss_hash_key = NULL;
2916 vnic->flags = 0;
2917 }
2918}
2919
2920static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2921{
2922 int i, rc = 0, size;
2923 struct bnxt_vnic_info *vnic;
2924 struct pci_dev *pdev = bp->pdev;
2925 int max_rings;
2926
2927 for (i = 0; i < bp->nr_vnics; i++) {
2928 vnic = &bp->vnic_info[i];
2929
2930 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2931 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2932
2933 if (mem_size > 0) {
2934 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2935 if (!vnic->uc_list) {
2936 rc = -ENOMEM;
2937 goto out;
2938 }
2939 }
2940 }
2941
2942 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2943 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2944 vnic->mc_list =
2945 dma_alloc_coherent(&pdev->dev,
2946 vnic->mc_list_size,
2947 &vnic->mc_list_mapping,
2948 GFP_KERNEL);
2949 if (!vnic->mc_list) {
2950 rc = -ENOMEM;
2951 goto out;
2952 }
2953 }
2954
2955 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2956 max_rings = bp->rx_nr_rings;
2957 else
2958 max_rings = 1;
2959
2960 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2961 if (!vnic->fw_grp_ids) {
2962 rc = -ENOMEM;
2963 goto out;
2964 }
2965
Michael Chanae10ae72016-12-29 12:13:38 -05002966 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2967 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2968 continue;
2969
Michael Chanc0c050c2015-10-22 16:01:17 -04002970 /* Allocate rss table and hash key */
2971 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2972 &vnic->rss_table_dma_addr,
2973 GFP_KERNEL);
2974 if (!vnic->rss_table) {
2975 rc = -ENOMEM;
2976 goto out;
2977 }
2978
2979 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2980
2981 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2982 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2983 }
2984 return 0;
2985
2986out:
2987 return rc;
2988}
2989
2990static void bnxt_free_hwrm_resources(struct bnxt *bp)
2991{
2992 struct pci_dev *pdev = bp->pdev;
2993
2994 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2995 bp->hwrm_cmd_resp_dma_addr);
2996
2997 bp->hwrm_cmd_resp_addr = NULL;
2998 if (bp->hwrm_dbg_resp_addr) {
2999 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
3000 bp->hwrm_dbg_resp_addr,
3001 bp->hwrm_dbg_resp_dma_addr);
3002
3003 bp->hwrm_dbg_resp_addr = NULL;
3004 }
3005}
3006
3007static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3008{
3009 struct pci_dev *pdev = bp->pdev;
3010
3011 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3012 &bp->hwrm_cmd_resp_dma_addr,
3013 GFP_KERNEL);
3014 if (!bp->hwrm_cmd_resp_addr)
3015 return -ENOMEM;
3016 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
3017 HWRM_DBG_REG_BUF_SIZE,
3018 &bp->hwrm_dbg_resp_dma_addr,
3019 GFP_KERNEL);
3020 if (!bp->hwrm_dbg_resp_addr)
3021 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
3022
3023 return 0;
3024}
3025
Deepak Khungare605db82017-05-29 19:06:04 -04003026static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3027{
3028 if (bp->hwrm_short_cmd_req_addr) {
3029 struct pci_dev *pdev = bp->pdev;
3030
3031 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3032 bp->hwrm_short_cmd_req_addr,
3033 bp->hwrm_short_cmd_req_dma_addr);
3034 bp->hwrm_short_cmd_req_addr = NULL;
3035 }
3036}
3037
3038static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3039{
3040 struct pci_dev *pdev = bp->pdev;
3041
3042 bp->hwrm_short_cmd_req_addr =
3043 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3044 &bp->hwrm_short_cmd_req_dma_addr,
3045 GFP_KERNEL);
3046 if (!bp->hwrm_short_cmd_req_addr)
3047 return -ENOMEM;
3048
3049 return 0;
3050}
3051
Michael Chanc0c050c2015-10-22 16:01:17 -04003052static void bnxt_free_stats(struct bnxt *bp)
3053{
3054 u32 size, i;
3055 struct pci_dev *pdev = bp->pdev;
3056
Michael Chan3bdf56c2016-03-07 15:38:45 -05003057 if (bp->hw_rx_port_stats) {
3058 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3059 bp->hw_rx_port_stats,
3060 bp->hw_rx_port_stats_map);
3061 bp->hw_rx_port_stats = NULL;
3062 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3063 }
3064
Michael Chanc0c050c2015-10-22 16:01:17 -04003065 if (!bp->bnapi)
3066 return;
3067
3068 size = sizeof(struct ctx_hw_stats);
3069
3070 for (i = 0; i < bp->cp_nr_rings; i++) {
3071 struct bnxt_napi *bnapi = bp->bnapi[i];
3072 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3073
3074 if (cpr->hw_stats) {
3075 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3076 cpr->hw_stats_map);
3077 cpr->hw_stats = NULL;
3078 }
3079 }
3080}
3081
3082static int bnxt_alloc_stats(struct bnxt *bp)
3083{
3084 u32 size, i;
3085 struct pci_dev *pdev = bp->pdev;
3086
3087 size = sizeof(struct ctx_hw_stats);
3088
3089 for (i = 0; i < bp->cp_nr_rings; i++) {
3090 struct bnxt_napi *bnapi = bp->bnapi[i];
3091 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3092
3093 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3094 &cpr->hw_stats_map,
3095 GFP_KERNEL);
3096 if (!cpr->hw_stats)
3097 return -ENOMEM;
3098
3099 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3100 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003101
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003102 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05003103 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3104 sizeof(struct tx_port_stats) + 1024;
3105
3106 bp->hw_rx_port_stats =
3107 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3108 &bp->hw_rx_port_stats_map,
3109 GFP_KERNEL);
3110 if (!bp->hw_rx_port_stats)
3111 return -ENOMEM;
3112
3113 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3114 512;
3115 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3116 sizeof(struct rx_port_stats) + 512;
3117 bp->flags |= BNXT_FLAG_PORT_STATS;
3118 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003119 return 0;
3120}
3121
3122static void bnxt_clear_ring_indices(struct bnxt *bp)
3123{
3124 int i;
3125
3126 if (!bp->bnapi)
3127 return;
3128
3129 for (i = 0; i < bp->cp_nr_rings; i++) {
3130 struct bnxt_napi *bnapi = bp->bnapi[i];
3131 struct bnxt_cp_ring_info *cpr;
3132 struct bnxt_rx_ring_info *rxr;
3133 struct bnxt_tx_ring_info *txr;
3134
3135 if (!bnapi)
3136 continue;
3137
3138 cpr = &bnapi->cp_ring;
3139 cpr->cp_raw_cons = 0;
3140
Michael Chanb6ab4b02016-01-02 23:44:59 -05003141 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003142 if (txr) {
3143 txr->tx_prod = 0;
3144 txr->tx_cons = 0;
3145 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003146
Michael Chanb6ab4b02016-01-02 23:44:59 -05003147 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003148 if (rxr) {
3149 rxr->rx_prod = 0;
3150 rxr->rx_agg_prod = 0;
3151 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003152 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003153 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003154 }
3155}
3156
3157static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3158{
3159#ifdef CONFIG_RFS_ACCEL
3160 int i;
3161
3162 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3163 * safe to delete the hash table.
3164 */
3165 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3166 struct hlist_head *head;
3167 struct hlist_node *tmp;
3168 struct bnxt_ntuple_filter *fltr;
3169
3170 head = &bp->ntp_fltr_hash_tbl[i];
3171 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3172 hlist_del(&fltr->hash);
3173 kfree(fltr);
3174 }
3175 }
3176 if (irq_reinit) {
3177 kfree(bp->ntp_fltr_bmap);
3178 bp->ntp_fltr_bmap = NULL;
3179 }
3180 bp->ntp_fltr_count = 0;
3181#endif
3182}
3183
3184static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3185{
3186#ifdef CONFIG_RFS_ACCEL
3187 int i, rc = 0;
3188
3189 if (!(bp->flags & BNXT_FLAG_RFS))
3190 return 0;
3191
3192 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3193 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3194
3195 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003196 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3197 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003198 GFP_KERNEL);
3199
3200 if (!bp->ntp_fltr_bmap)
3201 rc = -ENOMEM;
3202
3203 return rc;
3204#else
3205 return 0;
3206#endif
3207}
3208
3209static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3210{
3211 bnxt_free_vnic_attributes(bp);
3212 bnxt_free_tx_rings(bp);
3213 bnxt_free_rx_rings(bp);
3214 bnxt_free_cp_rings(bp);
3215 bnxt_free_ntp_fltrs(bp, irq_re_init);
3216 if (irq_re_init) {
3217 bnxt_free_stats(bp);
3218 bnxt_free_ring_grps(bp);
3219 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003220 kfree(bp->tx_ring_map);
3221 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003222 kfree(bp->tx_ring);
3223 bp->tx_ring = NULL;
3224 kfree(bp->rx_ring);
3225 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003226 kfree(bp->bnapi);
3227 bp->bnapi = NULL;
3228 } else {
3229 bnxt_clear_ring_indices(bp);
3230 }
3231}
3232
3233static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3234{
Michael Chan01657bc2016-01-02 23:45:03 -05003235 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003236 void *bnapi;
3237
3238 if (irq_re_init) {
3239 /* Allocate bnapi mem pointer array and mem block for
3240 * all queues
3241 */
3242 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3243 bp->cp_nr_rings);
3244 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3245 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3246 if (!bnapi)
3247 return -ENOMEM;
3248
3249 bp->bnapi = bnapi;
3250 bnapi += arr_size;
3251 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3252 bp->bnapi[i] = bnapi;
3253 bp->bnapi[i]->index = i;
3254 bp->bnapi[i]->bp = bp;
3255 }
3256
Michael Chanb6ab4b02016-01-02 23:44:59 -05003257 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3258 sizeof(struct bnxt_rx_ring_info),
3259 GFP_KERNEL);
3260 if (!bp->rx_ring)
3261 return -ENOMEM;
3262
3263 for (i = 0; i < bp->rx_nr_rings; i++) {
3264 bp->rx_ring[i].bnapi = bp->bnapi[i];
3265 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3266 }
3267
3268 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3269 sizeof(struct bnxt_tx_ring_info),
3270 GFP_KERNEL);
3271 if (!bp->tx_ring)
3272 return -ENOMEM;
3273
Michael Chana960dec2017-02-06 16:55:39 -05003274 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3275 GFP_KERNEL);
3276
3277 if (!bp->tx_ring_map)
3278 return -ENOMEM;
3279
Michael Chan01657bc2016-01-02 23:45:03 -05003280 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3281 j = 0;
3282 else
3283 j = bp->rx_nr_rings;
3284
3285 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3286 bp->tx_ring[i].bnapi = bp->bnapi[j];
3287 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003288 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003289 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003290 bp->tx_ring[i].txq_index = i -
3291 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003292 bp->bnapi[j]->tx_int = bnxt_tx_int;
3293 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003294 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003295 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3296 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003297 }
3298
Michael Chanc0c050c2015-10-22 16:01:17 -04003299 rc = bnxt_alloc_stats(bp);
3300 if (rc)
3301 goto alloc_mem_err;
3302
3303 rc = bnxt_alloc_ntp_fltrs(bp);
3304 if (rc)
3305 goto alloc_mem_err;
3306
3307 rc = bnxt_alloc_vnics(bp);
3308 if (rc)
3309 goto alloc_mem_err;
3310 }
3311
3312 bnxt_init_ring_struct(bp);
3313
3314 rc = bnxt_alloc_rx_rings(bp);
3315 if (rc)
3316 goto alloc_mem_err;
3317
3318 rc = bnxt_alloc_tx_rings(bp);
3319 if (rc)
3320 goto alloc_mem_err;
3321
3322 rc = bnxt_alloc_cp_rings(bp);
3323 if (rc)
3324 goto alloc_mem_err;
3325
3326 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3327 BNXT_VNIC_UCAST_FLAG;
3328 rc = bnxt_alloc_vnic_attributes(bp);
3329 if (rc)
3330 goto alloc_mem_err;
3331 return 0;
3332
3333alloc_mem_err:
3334 bnxt_free_mem(bp, true);
3335 return rc;
3336}
3337
Michael Chan9d8bc092016-12-29 12:13:33 -05003338static void bnxt_disable_int(struct bnxt *bp)
3339{
3340 int i;
3341
3342 if (!bp->bnapi)
3343 return;
3344
3345 for (i = 0; i < bp->cp_nr_rings; i++) {
3346 struct bnxt_napi *bnapi = bp->bnapi[i];
3347 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003348 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003349
Michael Chandaf1f1e2017-02-20 19:25:17 -05003350 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3351 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003352 }
3353}
3354
3355static void bnxt_disable_int_sync(struct bnxt *bp)
3356{
3357 int i;
3358
3359 atomic_inc(&bp->intr_sem);
3360
3361 bnxt_disable_int(bp);
3362 for (i = 0; i < bp->cp_nr_rings; i++)
3363 synchronize_irq(bp->irq_tbl[i].vector);
3364}
3365
3366static void bnxt_enable_int(struct bnxt *bp)
3367{
3368 int i;
3369
3370 atomic_set(&bp->intr_sem, 0);
3371 for (i = 0; i < bp->cp_nr_rings; i++) {
3372 struct bnxt_napi *bnapi = bp->bnapi[i];
3373 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3374
3375 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3376 }
3377}
3378
Michael Chanc0c050c2015-10-22 16:01:17 -04003379void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3380 u16 cmpl_ring, u16 target_id)
3381{
Michael Chana8643e12016-02-26 04:00:05 -05003382 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003383
Michael Chana8643e12016-02-26 04:00:05 -05003384 req->req_type = cpu_to_le16(req_type);
3385 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3386 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003387 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3388}
3389
Michael Chanfbfbc482016-02-26 04:00:07 -05003390static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3391 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003392{
Michael Chana11fa2b2016-05-15 03:04:47 -04003393 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003394 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003395 u32 *data = msg;
3396 __le32 *resp_len, *valid;
3397 u16 cp_ring_id, len = 0;
3398 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003399 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Vasundhara Volamebd58182017-12-01 03:13:05 -05003400 struct hwrm_short_input short_input = {0};
Michael Chanc0c050c2015-10-22 16:01:17 -04003401
Michael Chana8643e12016-02-26 04:00:05 -05003402 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003403 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003404 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003405 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3406
Deepak Khungare605db82017-05-29 19:06:04 -04003407 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3408 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003409
3410 memcpy(short_cmd_req, req, msg_len);
3411 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3412 msg_len);
3413
3414 short_input.req_type = req->req_type;
3415 short_input.signature =
3416 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3417 short_input.size = cpu_to_le16(msg_len);
3418 short_input.req_addr =
3419 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3420
3421 data = (u32 *)&short_input;
3422 msg_len = sizeof(short_input);
3423
3424 /* Sync memory write before updating doorbell */
3425 wmb();
3426
3427 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3428 }
3429
Michael Chanc0c050c2015-10-22 16:01:17 -04003430 /* Write request msg to hwrm channel */
3431 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3432
Deepak Khungare605db82017-05-29 19:06:04 -04003433 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003434 writel(0, bp->bar0 + i);
3435
Michael Chanc0c050c2015-10-22 16:01:17 -04003436 /* currently supports only one outstanding message */
3437 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003438 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003439
3440 /* Ring channel doorbell */
3441 writel(1, bp->bar0 + 0x100);
3442
Michael Chanff4fe812016-02-26 04:00:04 -05003443 if (!timeout)
3444 timeout = DFLT_HWRM_CMD_TIMEOUT;
3445
Michael Chanc0c050c2015-10-22 16:01:17 -04003446 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003447 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003448 if (intr_process) {
3449 /* Wait until hwrm response cmpl interrupt is processed */
3450 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003451 i++ < tmo_count) {
3452 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003453 }
3454
3455 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3456 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003457 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003458 return -1;
3459 }
3460 } else {
3461 /* Check if response len is updated */
3462 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003463 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003464 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3465 HWRM_RESP_LEN_SFT;
3466 if (len)
3467 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003468 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003469 }
3470
Michael Chana11fa2b2016-05-15 03:04:47 -04003471 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003472 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003473 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003474 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003475 return -1;
3476 }
3477
3478 /* Last word of resp contains valid bit */
3479 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003480 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003481 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3482 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003483 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003484 }
3485
Michael Chana11fa2b2016-05-15 03:04:47 -04003486 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003487 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003488 timeout, le16_to_cpu(req->req_type),
3489 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003490 return -1;
3491 }
3492 }
3493
3494 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003495 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003496 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3497 le16_to_cpu(resp->req_type),
3498 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003499 return rc;
3500}
3501
3502int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3503{
3504 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003505}
3506
Michael Chancc72f3b2017-10-13 21:09:33 -04003507int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3508 int timeout)
3509{
3510 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3511}
3512
Michael Chanc0c050c2015-10-22 16:01:17 -04003513int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3514{
3515 int rc;
3516
3517 mutex_lock(&bp->hwrm_cmd_lock);
3518 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3519 mutex_unlock(&bp->hwrm_cmd_lock);
3520 return rc;
3521}
3522
Michael Chan90e209212016-02-26 04:00:08 -05003523int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3524 int timeout)
3525{
3526 int rc;
3527
3528 mutex_lock(&bp->hwrm_cmd_lock);
3529 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3530 mutex_unlock(&bp->hwrm_cmd_lock);
3531 return rc;
3532}
3533
Michael Chana1653b12016-12-07 00:26:20 -05003534int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3535 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003536{
3537 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003538 DECLARE_BITMAP(async_events_bmap, 256);
3539 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003540 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003541
3542 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3543
3544 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003545 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003546
Michael Chan25be8622016-04-05 14:09:00 -04003547 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3548 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3549 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3550
Michael Chana1653b12016-12-07 00:26:20 -05003551 if (bmap && bmap_size) {
3552 for (i = 0; i < bmap_size; i++) {
3553 if (test_bit(i, bmap))
3554 __set_bit(i, async_events_bmap);
3555 }
3556 }
3557
Michael Chan25be8622016-04-05 14:09:00 -04003558 for (i = 0; i < 8; i++)
3559 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3560
Michael Chana1653b12016-12-07 00:26:20 -05003561 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3562}
3563
3564static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3565{
3566 struct hwrm_func_drv_rgtr_input req = {0};
3567
3568 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3569
3570 req.enables =
3571 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3572 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3573
Michael Chan11f15ed2016-04-05 14:08:55 -04003574 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003575 req.ver_maj = DRV_VER_MAJ;
3576 req.ver_min = DRV_VER_MIN;
3577 req.ver_upd = DRV_VER_UPD;
3578
3579 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003580 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003581 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003582
Michael Chan9b0436c2017-07-11 13:05:36 -04003583 memset(data, 0, sizeof(data));
3584 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3585 u16 cmd = bnxt_vf_req_snif[i];
3586 unsigned int bit, idx;
3587
3588 idx = cmd / 32;
3589 bit = cmd % 32;
3590 data[idx] |= 1 << bit;
3591 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003592
Michael Chande68f5de2015-12-09 19:35:41 -05003593 for (i = 0; i < 8; i++)
3594 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3595
Michael Chanc0c050c2015-10-22 16:01:17 -04003596 req.enables |=
3597 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3598 }
3599
3600 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3601}
3602
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003603static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3604{
3605 struct hwrm_func_drv_unrgtr_input req = {0};
3606
3607 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3608 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3609}
3610
Michael Chanc0c050c2015-10-22 16:01:17 -04003611static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3612{
3613 u32 rc = 0;
3614 struct hwrm_tunnel_dst_port_free_input req = {0};
3615
3616 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3617 req.tunnel_type = tunnel_type;
3618
3619 switch (tunnel_type) {
3620 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3621 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3622 break;
3623 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3624 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3625 break;
3626 default:
3627 break;
3628 }
3629
3630 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3631 if (rc)
3632 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3633 rc);
3634 return rc;
3635}
3636
3637static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3638 u8 tunnel_type)
3639{
3640 u32 rc = 0;
3641 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3642 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3643
3644 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3645
3646 req.tunnel_type = tunnel_type;
3647 req.tunnel_dst_port_val = port;
3648
3649 mutex_lock(&bp->hwrm_cmd_lock);
3650 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3651 if (rc) {
3652 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3653 rc);
3654 goto err_out;
3655 }
3656
Christophe Jaillet57aac712016-11-22 06:14:40 +01003657 switch (tunnel_type) {
3658 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003659 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003660 break;
3661 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003662 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003663 break;
3664 default:
3665 break;
3666 }
3667
Michael Chanc0c050c2015-10-22 16:01:17 -04003668err_out:
3669 mutex_unlock(&bp->hwrm_cmd_lock);
3670 return rc;
3671}
3672
3673static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3674{
3675 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3676 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3677
3678 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003679 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003680
3681 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3682 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3683 req.mask = cpu_to_le32(vnic->rx_mask);
3684 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3685}
3686
3687#ifdef CONFIG_RFS_ACCEL
3688static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3689 struct bnxt_ntuple_filter *fltr)
3690{
3691 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3692
3693 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3694 req.ntuple_filter_id = fltr->filter_id;
3695 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3696}
3697
3698#define BNXT_NTP_FLTR_FLAGS \
3699 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3700 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3701 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3702 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3703 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3704 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3705 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3706 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3707 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3708 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3709 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3710 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3711 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003712 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003713
Michael Chan61aad722017-02-12 19:18:14 -05003714#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3715 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3716
Michael Chanc0c050c2015-10-22 16:01:17 -04003717static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3718 struct bnxt_ntuple_filter *fltr)
3719{
3720 int rc = 0;
3721 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3722 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3723 bp->hwrm_cmd_resp_addr;
3724 struct flow_keys *keys = &fltr->fkeys;
3725 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3726
3727 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003728 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003729
3730 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3731
3732 req.ethertype = htons(ETH_P_IP);
3733 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003734 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003735 req.ip_protocol = keys->basic.ip_proto;
3736
Michael Chandda0e742016-12-29 12:13:40 -05003737 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3738 int i;
3739
3740 req.ethertype = htons(ETH_P_IPV6);
3741 req.ip_addr_type =
3742 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3743 *(struct in6_addr *)&req.src_ipaddr[0] =
3744 keys->addrs.v6addrs.src;
3745 *(struct in6_addr *)&req.dst_ipaddr[0] =
3746 keys->addrs.v6addrs.dst;
3747 for (i = 0; i < 4; i++) {
3748 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3749 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3750 }
3751 } else {
3752 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3753 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3754 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3755 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3756 }
Michael Chan61aad722017-02-12 19:18:14 -05003757 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3758 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3759 req.tunnel_type =
3760 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3761 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003762
3763 req.src_port = keys->ports.src;
3764 req.src_port_mask = cpu_to_be16(0xffff);
3765 req.dst_port = keys->ports.dst;
3766 req.dst_port_mask = cpu_to_be16(0xffff);
3767
Michael Chanc1935542015-12-27 18:19:28 -05003768 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003769 mutex_lock(&bp->hwrm_cmd_lock);
3770 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3771 if (!rc)
3772 fltr->filter_id = resp->ntuple_filter_id;
3773 mutex_unlock(&bp->hwrm_cmd_lock);
3774 return rc;
3775}
3776#endif
3777
3778static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3779 u8 *mac_addr)
3780{
3781 u32 rc = 0;
3782 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3783 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3784
3785 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003786 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3787 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3788 req.flags |=
3789 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003790 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003791 req.enables =
3792 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003793 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003794 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3795 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3796 req.l2_addr_mask[0] = 0xff;
3797 req.l2_addr_mask[1] = 0xff;
3798 req.l2_addr_mask[2] = 0xff;
3799 req.l2_addr_mask[3] = 0xff;
3800 req.l2_addr_mask[4] = 0xff;
3801 req.l2_addr_mask[5] = 0xff;
3802
3803 mutex_lock(&bp->hwrm_cmd_lock);
3804 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3805 if (!rc)
3806 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3807 resp->l2_filter_id;
3808 mutex_unlock(&bp->hwrm_cmd_lock);
3809 return rc;
3810}
3811
3812static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3813{
3814 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3815 int rc = 0;
3816
3817 /* Any associated ntuple filters will also be cleared by firmware. */
3818 mutex_lock(&bp->hwrm_cmd_lock);
3819 for (i = 0; i < num_of_vnics; i++) {
3820 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3821
3822 for (j = 0; j < vnic->uc_filter_count; j++) {
3823 struct hwrm_cfa_l2_filter_free_input req = {0};
3824
3825 bnxt_hwrm_cmd_hdr_init(bp, &req,
3826 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3827
3828 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3829
3830 rc = _hwrm_send_message(bp, &req, sizeof(req),
3831 HWRM_CMD_TIMEOUT);
3832 }
3833 vnic->uc_filter_count = 0;
3834 }
3835 mutex_unlock(&bp->hwrm_cmd_lock);
3836
3837 return rc;
3838}
3839
3840static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3841{
3842 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3843 struct hwrm_vnic_tpa_cfg_input req = {0};
3844
3845 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3846
3847 if (tpa_flags) {
3848 u16 mss = bp->dev->mtu - 40;
3849 u32 nsegs, n, segs = 0, flags;
3850
3851 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3852 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3853 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3854 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3855 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3856 if (tpa_flags & BNXT_FLAG_GRO)
3857 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3858
3859 req.flags = cpu_to_le32(flags);
3860
3861 req.enables =
3862 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003863 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3864 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003865
3866 /* Number of segs are log2 units, and first packet is not
3867 * included as part of this units.
3868 */
Michael Chan2839f282016-04-25 02:30:50 -04003869 if (mss <= BNXT_RX_PAGE_SIZE) {
3870 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003871 nsegs = (MAX_SKB_FRAGS - 1) * n;
3872 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003873 n = mss / BNXT_RX_PAGE_SIZE;
3874 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003875 n++;
3876 nsegs = (MAX_SKB_FRAGS - n) / n;
3877 }
3878
3879 segs = ilog2(nsegs);
3880 req.max_agg_segs = cpu_to_le16(segs);
3881 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003882
3883 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003884 }
3885 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3886
3887 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3888}
3889
3890static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3891{
3892 u32 i, j, max_rings;
3893 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3894 struct hwrm_vnic_rss_cfg_input req = {0};
3895
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003896 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003897 return 0;
3898
3899 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3900 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003901 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003902 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3903 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3904 max_rings = bp->rx_nr_rings - 1;
3905 else
3906 max_rings = bp->rx_nr_rings;
3907 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003908 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003909 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003910
3911 /* Fill the RSS indirection table with ring group ids */
3912 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3913 if (j == max_rings)
3914 j = 0;
3915 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3916 }
3917
3918 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3919 req.hash_key_tbl_addr =
3920 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3921 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003922 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003923 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3924}
3925
3926static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3927{
3928 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3929 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3930
3931 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3932 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3933 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3934 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3935 req.enables =
3936 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3937 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3938 /* thresholds not implemented in firmware yet */
3939 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3940 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3941 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3942 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3943}
3944
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003945static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3946 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003947{
3948 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3949
3950 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3951 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003952 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003953
3954 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003955 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003956}
3957
3958static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3959{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003960 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003961
3962 for (i = 0; i < bp->nr_vnics; i++) {
3963 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3964
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003965 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3966 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3967 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3968 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003969 }
3970 bp->rsscos_nr_ctxs = 0;
3971}
3972
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003973static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003974{
3975 int rc;
3976 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3977 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3978 bp->hwrm_cmd_resp_addr;
3979
3980 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3981 -1);
3982
3983 mutex_lock(&bp->hwrm_cmd_lock);
3984 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3985 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003986 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003987 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3988 mutex_unlock(&bp->hwrm_cmd_lock);
3989
3990 return rc;
3991}
3992
Michael Chana588e452016-12-07 00:26:21 -05003993int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003994{
Michael Chanb81a90d2016-01-02 23:45:01 -05003995 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003996 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3997 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003998 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003999
4000 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004001
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004002 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4003 /* Only RSS support for now TBD: COS & LB */
4004 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4005 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4006 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4007 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05004008 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4009 req.rss_rule =
4010 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4011 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4012 VNIC_CFG_REQ_ENABLES_MRU);
4013 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004014 } else {
4015 req.rss_rule = cpu_to_le16(0xffff);
4016 }
4017
4018 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4019 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004020 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4021 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4022 } else {
4023 req.cos_rule = cpu_to_le16(0xffff);
4024 }
4025
Michael Chanc0c050c2015-10-22 16:01:17 -04004026 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004027 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004028 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004029 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004030 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4031 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004032
Michael Chanb81a90d2016-01-02 23:45:01 -05004033 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004034 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4035 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4036
4037 req.lb_rule = cpu_to_le16(0xffff);
4038 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4039 VLAN_HLEN);
4040
Michael Chancf6645f2016-06-13 02:25:28 -04004041#ifdef CONFIG_BNXT_SRIOV
4042 if (BNXT_VF(bp))
4043 def_vlan = bp->vf.vlan;
4044#endif
4045 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04004046 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05004047 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4048 req.flags |=
4049 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04004050
4051 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4052}
4053
4054static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4055{
4056 u32 rc = 0;
4057
4058 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4059 struct hwrm_vnic_free_input req = {0};
4060
4061 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4062 req.vnic_id =
4063 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4064
4065 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4066 if (rc)
4067 return rc;
4068 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4069 }
4070 return rc;
4071}
4072
4073static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4074{
4075 u16 i;
4076
4077 for (i = 0; i < bp->nr_vnics; i++)
4078 bnxt_hwrm_vnic_free_one(bp, i);
4079}
4080
Michael Chanb81a90d2016-01-02 23:45:01 -05004081static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4082 unsigned int start_rx_ring_idx,
4083 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004084{
Michael Chanb81a90d2016-01-02 23:45:01 -05004085 int rc = 0;
4086 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004087 struct hwrm_vnic_alloc_input req = {0};
4088 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4089
4090 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004091 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4092 grp_idx = bp->rx_ring[i].bnapi->index;
4093 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004094 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004095 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004096 break;
4097 }
4098 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05004099 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004100 }
4101
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004102 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4103 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004104 if (vnic_id == 0)
4105 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4106
4107 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4108
4109 mutex_lock(&bp->hwrm_cmd_lock);
4110 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4111 if (!rc)
4112 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4113 mutex_unlock(&bp->hwrm_cmd_lock);
4114 return rc;
4115}
4116
Michael Chan8fdefd62016-12-29 12:13:36 -05004117static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4118{
4119 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4120 struct hwrm_vnic_qcaps_input req = {0};
4121 int rc;
4122
4123 if (bp->hwrm_spec_code < 0x10600)
4124 return 0;
4125
4126 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4127 mutex_lock(&bp->hwrm_cmd_lock);
4128 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4129 if (!rc) {
4130 if (resp->flags &
4131 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4132 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4133 }
4134 mutex_unlock(&bp->hwrm_cmd_lock);
4135 return rc;
4136}
4137
Michael Chanc0c050c2015-10-22 16:01:17 -04004138static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4139{
4140 u16 i;
4141 u32 rc = 0;
4142
4143 mutex_lock(&bp->hwrm_cmd_lock);
4144 for (i = 0; i < bp->rx_nr_rings; i++) {
4145 struct hwrm_ring_grp_alloc_input req = {0};
4146 struct hwrm_ring_grp_alloc_output *resp =
4147 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004148 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004149
4150 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4151
Michael Chanb81a90d2016-01-02 23:45:01 -05004152 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4153 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4154 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4155 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004156
4157 rc = _hwrm_send_message(bp, &req, sizeof(req),
4158 HWRM_CMD_TIMEOUT);
4159 if (rc)
4160 break;
4161
Michael Chanb81a90d2016-01-02 23:45:01 -05004162 bp->grp_info[grp_idx].fw_grp_id =
4163 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004164 }
4165 mutex_unlock(&bp->hwrm_cmd_lock);
4166 return rc;
4167}
4168
4169static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4170{
4171 u16 i;
4172 u32 rc = 0;
4173 struct hwrm_ring_grp_free_input req = {0};
4174
4175 if (!bp->grp_info)
4176 return 0;
4177
4178 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4179
4180 mutex_lock(&bp->hwrm_cmd_lock);
4181 for (i = 0; i < bp->cp_nr_rings; i++) {
4182 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4183 continue;
4184 req.ring_group_id =
4185 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4186
4187 rc = _hwrm_send_message(bp, &req, sizeof(req),
4188 HWRM_CMD_TIMEOUT);
4189 if (rc)
4190 break;
4191 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4192 }
4193 mutex_unlock(&bp->hwrm_cmd_lock);
4194 return rc;
4195}
4196
4197static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4198 struct bnxt_ring_struct *ring,
4199 u32 ring_type, u32 map_index,
4200 u32 stats_ctx_id)
4201{
4202 int rc = 0, err = 0;
4203 struct hwrm_ring_alloc_input req = {0};
4204 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4205 u16 ring_id;
4206
4207 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4208
4209 req.enables = 0;
4210 if (ring->nr_pages > 1) {
4211 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4212 /* Page size is in log2 units */
4213 req.page_size = BNXT_PAGE_SHIFT;
4214 req.page_tbl_depth = 1;
4215 } else {
4216 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4217 }
4218 req.fbo = 0;
4219 /* Association of ring index with doorbell index and MSIX number */
4220 req.logical_id = cpu_to_le16(map_index);
4221
4222 switch (ring_type) {
4223 case HWRM_RING_ALLOC_TX:
4224 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4225 /* Association of transmit ring with completion ring */
4226 req.cmpl_ring_id =
4227 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4228 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4229 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4230 req.queue_id = cpu_to_le16(ring->queue_id);
4231 break;
4232 case HWRM_RING_ALLOC_RX:
4233 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4234 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4235 break;
4236 case HWRM_RING_ALLOC_AGG:
4237 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4238 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4239 break;
4240 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004241 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004242 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4243 if (bp->flags & BNXT_FLAG_USING_MSIX)
4244 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4245 break;
4246 default:
4247 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4248 ring_type);
4249 return -1;
4250 }
4251
4252 mutex_lock(&bp->hwrm_cmd_lock);
4253 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4254 err = le16_to_cpu(resp->error_code);
4255 ring_id = le16_to_cpu(resp->ring_id);
4256 mutex_unlock(&bp->hwrm_cmd_lock);
4257
4258 if (rc || err) {
4259 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004260 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004261 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4262 rc, err);
4263 return -1;
4264
4265 case RING_FREE_REQ_RING_TYPE_RX:
4266 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4267 rc, err);
4268 return -1;
4269
4270 case RING_FREE_REQ_RING_TYPE_TX:
4271 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4272 rc, err);
4273 return -1;
4274
4275 default:
4276 netdev_err(bp->dev, "Invalid ring\n");
4277 return -1;
4278 }
4279 }
4280 ring->fw_ring_id = ring_id;
4281 return rc;
4282}
4283
Michael Chan486b5c22016-12-29 12:13:42 -05004284static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4285{
4286 int rc;
4287
4288 if (BNXT_PF(bp)) {
4289 struct hwrm_func_cfg_input req = {0};
4290
4291 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4292 req.fid = cpu_to_le16(0xffff);
4293 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4294 req.async_event_cr = cpu_to_le16(idx);
4295 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4296 } else {
4297 struct hwrm_func_vf_cfg_input req = {0};
4298
4299 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4300 req.enables =
4301 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4302 req.async_event_cr = cpu_to_le16(idx);
4303 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4304 }
4305 return rc;
4306}
4307
Michael Chanc0c050c2015-10-22 16:01:17 -04004308static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4309{
4310 int i, rc = 0;
4311
Michael Chanedd0c2c2015-12-27 18:19:19 -05004312 for (i = 0; i < bp->cp_nr_rings; i++) {
4313 struct bnxt_napi *bnapi = bp->bnapi[i];
4314 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4315 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004316
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004317 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004318 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4319 INVALID_STATS_CTX_ID);
4320 if (rc)
4321 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004322 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4323 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004324
4325 if (!i) {
4326 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4327 if (rc)
4328 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4329 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004330 }
4331
Michael Chanedd0c2c2015-12-27 18:19:19 -05004332 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004333 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004334 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004335 u32 map_idx = txr->bnapi->index;
4336 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004337
Michael Chanb81a90d2016-01-02 23:45:01 -05004338 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4339 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004340 if (rc)
4341 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004342 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004343 }
4344
Michael Chanedd0c2c2015-12-27 18:19:19 -05004345 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004346 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004347 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004348 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004349
Michael Chanb81a90d2016-01-02 23:45:01 -05004350 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4351 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004352 if (rc)
4353 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004354 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004355 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004356 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004357 }
4358
4359 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4360 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004361 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004362 struct bnxt_ring_struct *ring =
4363 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004364 u32 grp_idx = rxr->bnapi->index;
4365 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004366
4367 rc = hwrm_ring_alloc_send_msg(bp, ring,
4368 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004369 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004370 INVALID_STATS_CTX_ID);
4371 if (rc)
4372 goto err_out;
4373
Michael Chanb81a90d2016-01-02 23:45:01 -05004374 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004375 writel(DB_KEY_RX | rxr->rx_agg_prod,
4376 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004377 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004378 }
4379 }
4380err_out:
4381 return rc;
4382}
4383
4384static int hwrm_ring_free_send_msg(struct bnxt *bp,
4385 struct bnxt_ring_struct *ring,
4386 u32 ring_type, int cmpl_ring_id)
4387{
4388 int rc;
4389 struct hwrm_ring_free_input req = {0};
4390 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4391 u16 error_code;
4392
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004393 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004394 req.ring_type = ring_type;
4395 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4396
4397 mutex_lock(&bp->hwrm_cmd_lock);
4398 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4399 error_code = le16_to_cpu(resp->error_code);
4400 mutex_unlock(&bp->hwrm_cmd_lock);
4401
4402 if (rc || error_code) {
4403 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004404 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004405 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4406 rc);
4407 return rc;
4408 case RING_FREE_REQ_RING_TYPE_RX:
4409 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4410 rc);
4411 return rc;
4412 case RING_FREE_REQ_RING_TYPE_TX:
4413 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4414 rc);
4415 return rc;
4416 default:
4417 netdev_err(bp->dev, "Invalid ring\n");
4418 return -1;
4419 }
4420 }
4421 return 0;
4422}
4423
Michael Chanedd0c2c2015-12-27 18:19:19 -05004424static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004425{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004426 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004427
4428 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004429 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004430
Michael Chanedd0c2c2015-12-27 18:19:19 -05004431 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004432 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004433 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004434 u32 grp_idx = txr->bnapi->index;
4435 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004436
Michael Chanedd0c2c2015-12-27 18:19:19 -05004437 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4438 hwrm_ring_free_send_msg(bp, ring,
4439 RING_FREE_REQ_RING_TYPE_TX,
4440 close_path ? cmpl_ring_id :
4441 INVALID_HW_RING_ID);
4442 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004443 }
4444 }
4445
Michael Chanedd0c2c2015-12-27 18:19:19 -05004446 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004447 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004448 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004449 u32 grp_idx = rxr->bnapi->index;
4450 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004451
Michael Chanedd0c2c2015-12-27 18:19:19 -05004452 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4453 hwrm_ring_free_send_msg(bp, ring,
4454 RING_FREE_REQ_RING_TYPE_RX,
4455 close_path ? cmpl_ring_id :
4456 INVALID_HW_RING_ID);
4457 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004458 bp->grp_info[grp_idx].rx_fw_ring_id =
4459 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004460 }
4461 }
4462
Michael Chanedd0c2c2015-12-27 18:19:19 -05004463 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004464 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004465 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004466 u32 grp_idx = rxr->bnapi->index;
4467 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004468
Michael Chanedd0c2c2015-12-27 18:19:19 -05004469 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4470 hwrm_ring_free_send_msg(bp, ring,
4471 RING_FREE_REQ_RING_TYPE_RX,
4472 close_path ? cmpl_ring_id :
4473 INVALID_HW_RING_ID);
4474 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004475 bp->grp_info[grp_idx].agg_fw_ring_id =
4476 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004477 }
4478 }
4479
Michael Chan9d8bc092016-12-29 12:13:33 -05004480 /* The completion rings are about to be freed. After that the
4481 * IRQ doorbell will not work anymore. So we need to disable
4482 * IRQ here.
4483 */
4484 bnxt_disable_int_sync(bp);
4485
Michael Chanedd0c2c2015-12-27 18:19:19 -05004486 for (i = 0; i < bp->cp_nr_rings; i++) {
4487 struct bnxt_napi *bnapi = bp->bnapi[i];
4488 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4489 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004490
Michael Chanedd0c2c2015-12-27 18:19:19 -05004491 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4492 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004493 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004494 INVALID_HW_RING_ID);
4495 ring->fw_ring_id = INVALID_HW_RING_ID;
4496 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004497 }
4498 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004499}
4500
Michael Chan391be5c2016-12-29 12:13:41 -05004501/* Caller must hold bp->hwrm_cmd_lock */
4502int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4503{
4504 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4505 struct hwrm_func_qcfg_input req = {0};
4506 int rc;
4507
4508 if (bp->hwrm_spec_code < 0x10601)
4509 return 0;
4510
4511 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4512 req.fid = cpu_to_le16(fid);
4513 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4514 if (!rc)
4515 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4516
4517 return rc;
4518}
4519
Michael Chand1e79252017-02-06 16:55:38 -05004520static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004521{
4522 struct hwrm_func_cfg_input req = {0};
4523 int rc;
4524
4525 if (bp->hwrm_spec_code < 0x10601)
4526 return 0;
4527
4528 if (BNXT_VF(bp))
4529 return 0;
4530
4531 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4532 req.fid = cpu_to_le16(0xffff);
4533 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4534 req.num_tx_rings = cpu_to_le16(*tx_rings);
4535 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4536 if (rc)
4537 return rc;
4538
4539 mutex_lock(&bp->hwrm_cmd_lock);
4540 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4541 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan98fdbe72017-08-28 13:40:26 -04004542 if (!rc)
Michael Chan6a4f2942018-01-17 03:21:06 -05004543 bp->hw_resc.resv_tx_rings = *tx_rings;
Michael Chan391be5c2016-12-29 12:13:41 -05004544 return rc;
4545}
4546
Michael Chan98fdbe72017-08-28 13:40:26 -04004547static int bnxt_hwrm_check_tx_rings(struct bnxt *bp, int tx_rings)
4548{
4549 struct hwrm_func_cfg_input req = {0};
4550 int rc;
4551
4552 if (bp->hwrm_spec_code < 0x10801)
4553 return 0;
4554
4555 if (BNXT_VF(bp))
4556 return 0;
4557
4558 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4559 req.fid = cpu_to_le16(0xffff);
4560 req.flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST);
4561 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4562 req.num_tx_rings = cpu_to_le16(tx_rings);
4563 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4564 if (rc)
4565 return -ENOMEM;
4566 return 0;
4567}
4568
Michael Chanf8503962017-10-26 11:51:28 -04004569static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
Michael Chanbb053f52016-02-26 04:00:02 -05004570 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4571{
Michael Chanf8503962017-10-26 11:51:28 -04004572 u16 val, tmr, max, flags;
4573
4574 max = hw_coal->bufs_per_record * 128;
4575 if (hw_coal->budget)
4576 max = hw_coal->bufs_per_record * hw_coal->budget;
4577
4578 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
4579 req->num_cmpl_aggr_int = cpu_to_le16(val);
Michael Chanb153cbc2017-11-03 03:32:39 -04004580
4581 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4582 val = min_t(u16, val, 63);
Michael Chanf8503962017-10-26 11:51:28 -04004583 req->num_cmpl_dma_aggr = cpu_to_le16(val);
4584
Michael Chanb153cbc2017-11-03 03:32:39 -04004585 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4586 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 63);
Michael Chanf8503962017-10-26 11:51:28 -04004587 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
4588
4589 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks);
4590 tmr = max_t(u16, tmr, 1);
4591 req->int_lat_tmr_max = cpu_to_le16(tmr);
4592
4593 /* min timer set to 1/2 of interrupt timer */
4594 val = tmr / 2;
4595 req->int_lat_tmr_min = cpu_to_le16(val);
4596
4597 /* buf timer set to 1/4 of interrupt timer */
4598 val = max_t(u16, tmr / 4, 1);
4599 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
4600
4601 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks_irq);
4602 tmr = max_t(u16, tmr, 1);
4603 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
4604
4605 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4606 if (hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
4607 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
Michael Chanbb053f52016-02-26 04:00:02 -05004608 req->flags = cpu_to_le16(flags);
Michael Chanbb053f52016-02-26 04:00:02 -05004609}
4610
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05004611int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
4612{
4613 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
4614 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4615 struct bnxt_coal coal;
4616 unsigned int grp_idx;
4617
4618 /* Tick values in micro seconds.
4619 * 1 coal_buf x bufs_per_record = 1 completion record.
4620 */
4621 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
4622
4623 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
4624 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
4625
4626 if (!bnapi->rx_ring)
4627 return -ENODEV;
4628
4629 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4630 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4631
4632 bnxt_hwrm_set_coal_params(&coal, &req_rx);
4633
4634 grp_idx = bnapi->index;
4635 req_rx.ring_id = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4636
4637 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
4638 HWRM_CMD_TIMEOUT);
4639}
4640
Michael Chanc0c050c2015-10-22 16:01:17 -04004641int bnxt_hwrm_set_coal(struct bnxt *bp)
4642{
4643 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004644 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4645 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004646
Michael Chandfc9c942016-02-26 04:00:03 -05004647 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4648 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4649 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4650 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004651
Michael Chanf8503962017-10-26 11:51:28 -04004652 bnxt_hwrm_set_coal_params(&bp->rx_coal, &req_rx);
4653 bnxt_hwrm_set_coal_params(&bp->tx_coal, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004654
4655 mutex_lock(&bp->hwrm_cmd_lock);
4656 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004657 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004658
Michael Chandfc9c942016-02-26 04:00:03 -05004659 req = &req_rx;
4660 if (!bnapi->rx_ring)
4661 req = &req_tx;
4662 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4663
4664 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004665 HWRM_CMD_TIMEOUT);
4666 if (rc)
4667 break;
4668 }
4669 mutex_unlock(&bp->hwrm_cmd_lock);
4670 return rc;
4671}
4672
4673static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4674{
4675 int rc = 0, i;
4676 struct hwrm_stat_ctx_free_input req = {0};
4677
4678 if (!bp->bnapi)
4679 return 0;
4680
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004681 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4682 return 0;
4683
Michael Chanc0c050c2015-10-22 16:01:17 -04004684 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4685
4686 mutex_lock(&bp->hwrm_cmd_lock);
4687 for (i = 0; i < bp->cp_nr_rings; i++) {
4688 struct bnxt_napi *bnapi = bp->bnapi[i];
4689 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4690
4691 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4692 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4693
4694 rc = _hwrm_send_message(bp, &req, sizeof(req),
4695 HWRM_CMD_TIMEOUT);
4696 if (rc)
4697 break;
4698
4699 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4700 }
4701 }
4702 mutex_unlock(&bp->hwrm_cmd_lock);
4703 return rc;
4704}
4705
4706static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4707{
4708 int rc = 0, i;
4709 struct hwrm_stat_ctx_alloc_input req = {0};
4710 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4711
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004712 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4713 return 0;
4714
Michael Chanc0c050c2015-10-22 16:01:17 -04004715 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4716
Michael Chan51f30782016-07-01 18:46:29 -04004717 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004718
4719 mutex_lock(&bp->hwrm_cmd_lock);
4720 for (i = 0; i < bp->cp_nr_rings; i++) {
4721 struct bnxt_napi *bnapi = bp->bnapi[i];
4722 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4723
4724 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4725
4726 rc = _hwrm_send_message(bp, &req, sizeof(req),
4727 HWRM_CMD_TIMEOUT);
4728 if (rc)
4729 break;
4730
4731 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4732
4733 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4734 }
4735 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004736 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004737}
4738
Michael Chancf6645f2016-06-13 02:25:28 -04004739static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4740{
4741 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004742 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04004743 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04004744 int rc;
4745
4746 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4747 req.fid = cpu_to_le16(0xffff);
4748 mutex_lock(&bp->hwrm_cmd_lock);
4749 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4750 if (rc)
4751 goto func_qcfg_exit;
4752
4753#ifdef CONFIG_BNXT_SRIOV
4754 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004755 struct bnxt_vf_info *vf = &bp->vf;
4756
4757 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4758 }
4759#endif
Michael Chan9315edc2017-07-24 12:34:25 -04004760 flags = le16_to_cpu(resp->flags);
4761 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4762 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
4763 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4764 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
4765 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04004766 }
Michael Chan9315edc2017-07-24 12:34:25 -04004767 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
4768 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05004769
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004770 switch (resp->port_partition_type) {
4771 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4772 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4773 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4774 bp->port_partition_type = resp->port_partition_type;
4775 break;
4776 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004777 if (bp->hwrm_spec_code < 0x10707 ||
4778 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4779 bp->br_mode = BRIDGE_MODE_VEB;
4780 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4781 bp->br_mode = BRIDGE_MODE_VEPA;
4782 else
4783 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004784
Michael Chan7eb9bb32017-10-26 11:51:25 -04004785 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
4786 if (!bp->max_mtu)
4787 bp->max_mtu = BNXT_MAX_MTU;
4788
Michael Chancf6645f2016-06-13 02:25:28 -04004789func_qcfg_exit:
4790 mutex_unlock(&bp->hwrm_cmd_lock);
4791 return rc;
4792}
4793
Michael Chanbe0dd9c2018-01-17 03:21:07 -05004794static int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp)
4795{
4796 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4797 struct hwrm_func_resource_qcaps_input req = {0};
4798 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4799 int rc;
4800
4801 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
4802 req.fid = cpu_to_le16(0xffff);
4803
4804 mutex_lock(&bp->hwrm_cmd_lock);
4805 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4806 if (rc) {
4807 rc = -EIO;
4808 goto hwrm_func_resc_qcaps_exit;
4809 }
4810
4811 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
4812 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4813 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
4814 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4815 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
4816 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4817 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
4818 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4819 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
4820 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
4821 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
4822 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4823 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
4824 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
4825 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
4826 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4827
4828hwrm_func_resc_qcaps_exit:
4829 mutex_unlock(&bp->hwrm_cmd_lock);
4830 return rc;
4831}
4832
4833static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004834{
4835 int rc = 0;
4836 struct hwrm_func_qcaps_input req = {0};
4837 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6a4f2942018-01-17 03:21:06 -05004838 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4839 u32 flags;
Michael Chanc0c050c2015-10-22 16:01:17 -04004840
4841 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4842 req.fid = cpu_to_le16(0xffff);
4843
4844 mutex_lock(&bp->hwrm_cmd_lock);
4845 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4846 if (rc)
4847 goto hwrm_func_qcaps_exit;
4848
Michael Chan6a4f2942018-01-17 03:21:06 -05004849 flags = le32_to_cpu(resp->flags);
4850 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05004851 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
Michael Chan6a4f2942018-01-17 03:21:06 -05004852 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05004853 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4854
Michael Chan7cc5a202016-09-19 03:58:05 -04004855 bp->tx_push_thresh = 0;
Michael Chan6a4f2942018-01-17 03:21:06 -05004856 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
Michael Chan7cc5a202016-09-19 03:58:05 -04004857 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4858
Michael Chan6a4f2942018-01-17 03:21:06 -05004859 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4860 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4861 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4862 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4863 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4864 if (!hw_resc->max_hw_ring_grps)
4865 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
4866 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4867 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
4868 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4869
Michael Chanc0c050c2015-10-22 16:01:17 -04004870 if (BNXT_PF(bp)) {
4871 struct bnxt_pf_info *pf = &bp->pf;
4872
4873 pf->fw_fid = le16_to_cpu(resp->fid);
4874 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004875 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004876 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004877 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4878 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4879 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4880 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4881 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4882 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4883 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4884 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chan6a4f2942018-01-17 03:21:06 -05004885 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
Michael Chanc1ef1462017-04-04 18:14:07 -04004886 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004887 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004888#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004889 struct bnxt_vf_info *vf = &bp->vf;
4890
4891 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan7cc5a202016-09-19 03:58:05 -04004892 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan379a80a2015-10-23 15:06:19 -04004893#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004894 }
4895
Michael Chanc0c050c2015-10-22 16:01:17 -04004896hwrm_func_qcaps_exit:
4897 mutex_unlock(&bp->hwrm_cmd_lock);
4898 return rc;
4899}
4900
Michael Chanbe0dd9c2018-01-17 03:21:07 -05004901static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
4902{
4903 int rc;
4904
4905 rc = __bnxt_hwrm_func_qcaps(bp);
4906 if (rc)
4907 return rc;
4908 if (bp->hwrm_spec_code >= 0x10803) {
4909 rc = bnxt_hwrm_func_resc_qcaps(bp);
4910 if (!rc)
4911 bp->flags |= BNXT_FLAG_NEW_RM;
4912 }
4913 return 0;
4914}
4915
Michael Chanc0c050c2015-10-22 16:01:17 -04004916static int bnxt_hwrm_func_reset(struct bnxt *bp)
4917{
4918 struct hwrm_func_reset_input req = {0};
4919
4920 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4921 req.enables = 0;
4922
4923 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4924}
4925
4926static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4927{
4928 int rc = 0;
4929 struct hwrm_queue_qportcfg_input req = {0};
4930 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4931 u8 i, *qptr;
4932
4933 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4934
4935 mutex_lock(&bp->hwrm_cmd_lock);
4936 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4937 if (rc)
4938 goto qportcfg_exit;
4939
4940 if (!resp->max_configurable_queues) {
4941 rc = -EINVAL;
4942 goto qportcfg_exit;
4943 }
4944 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004945 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004946 if (bp->max_tc > BNXT_MAX_QUEUE)
4947 bp->max_tc = BNXT_MAX_QUEUE;
4948
Michael Chan441cabb2016-09-19 03:58:02 -04004949 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4950 bp->max_tc = 1;
4951
Michael Chan87c374d2016-12-02 21:17:16 -05004952 if (bp->max_lltc > bp->max_tc)
4953 bp->max_lltc = bp->max_tc;
4954
Michael Chanc0c050c2015-10-22 16:01:17 -04004955 qptr = &resp->queue_id0;
4956 for (i = 0; i < bp->max_tc; i++) {
4957 bp->q_info[i].queue_id = *qptr++;
4958 bp->q_info[i].queue_profile = *qptr++;
4959 }
4960
4961qportcfg_exit:
4962 mutex_unlock(&bp->hwrm_cmd_lock);
4963 return rc;
4964}
4965
4966static int bnxt_hwrm_ver_get(struct bnxt *bp)
4967{
4968 int rc;
4969 struct hwrm_ver_get_input req = {0};
4970 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004971 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004972
Michael Chane6ef2692016-03-28 19:46:05 -04004973 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004974 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4975 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4976 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4977 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4978 mutex_lock(&bp->hwrm_cmd_lock);
4979 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4980 if (rc)
4981 goto hwrm_ver_get_exit;
4982
4983 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4984
Michael Chan894aa692018-01-17 03:21:03 -05004985 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
4986 resp->hwrm_intf_min_8b << 8 |
4987 resp->hwrm_intf_upd_8b;
4988 if (resp->hwrm_intf_maj_8b < 1) {
Michael Chanc1935542015-12-27 18:19:28 -05004989 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chan894aa692018-01-17 03:21:03 -05004990 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
4991 resp->hwrm_intf_upd_8b);
Michael Chanc1935542015-12-27 18:19:28 -05004992 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004993 }
Michael Chan431aa1e2017-10-26 11:51:23 -04004994 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
Michael Chan894aa692018-01-17 03:21:03 -05004995 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
4996 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
Michael Chanc0c050c2015-10-22 16:01:17 -04004997
Michael Chanff4fe812016-02-26 04:00:04 -05004998 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4999 if (!bp->hwrm_cmd_timeout)
5000 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
5001
Michael Chan894aa692018-01-17 03:21:03 -05005002 if (resp->hwrm_intf_maj_8b >= 1)
Michael Chane6ef2692016-03-28 19:46:05 -04005003 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
5004
Michael Chan659c8052016-06-13 02:25:33 -04005005 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005006 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
5007 !resp->chip_metal)
5008 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04005009
Deepak Khungare605db82017-05-29 19:06:04 -04005010 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
5011 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
5012 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
5013 bp->flags |= BNXT_FLAG_SHORT_CMD;
5014
Michael Chanc0c050c2015-10-22 16:01:17 -04005015hwrm_ver_get_exit:
5016 mutex_unlock(&bp->hwrm_cmd_lock);
5017 return rc;
5018}
5019
Rob Swindell5ac67d82016-09-19 03:58:03 -04005020int bnxt_hwrm_fw_set_time(struct bnxt *bp)
5021{
5022 struct hwrm_fw_set_time_input req = {0};
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01005023 struct tm tm;
5024 time64_t now = ktime_get_real_seconds();
Rob Swindell5ac67d82016-09-19 03:58:03 -04005025
5026 if (bp->hwrm_spec_code < 0x10400)
5027 return -EOPNOTSUPP;
5028
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01005029 time64_to_tm(now, 0, &tm);
Rob Swindell5ac67d82016-09-19 03:58:03 -04005030 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
5031 req.year = cpu_to_le16(1900 + tm.tm_year);
5032 req.month = 1 + tm.tm_mon;
5033 req.day = tm.tm_mday;
5034 req.hour = tm.tm_hour;
5035 req.minute = tm.tm_min;
5036 req.second = tm.tm_sec;
5037 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5038}
5039
Michael Chan3bdf56c2016-03-07 15:38:45 -05005040static int bnxt_hwrm_port_qstats(struct bnxt *bp)
5041{
5042 int rc;
5043 struct bnxt_pf_info *pf = &bp->pf;
5044 struct hwrm_port_qstats_input req = {0};
5045
5046 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
5047 return 0;
5048
5049 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
5050 req.port_id = cpu_to_le16(pf->port_id);
5051 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
5052 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
5053 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5054 return rc;
5055}
5056
Michael Chanc0c050c2015-10-22 16:01:17 -04005057static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
5058{
5059 if (bp->vxlan_port_cnt) {
5060 bnxt_hwrm_tunnel_dst_port_free(
5061 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5062 }
5063 bp->vxlan_port_cnt = 0;
5064 if (bp->nge_port_cnt) {
5065 bnxt_hwrm_tunnel_dst_port_free(
5066 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5067 }
5068 bp->nge_port_cnt = 0;
5069}
5070
5071static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
5072{
5073 int rc, i;
5074 u32 tpa_flags = 0;
5075
5076 if (set_tpa)
5077 tpa_flags = bp->flags & BNXT_FLAG_TPA;
5078 for (i = 0; i < bp->nr_vnics; i++) {
5079 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
5080 if (rc) {
5081 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04005082 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04005083 return rc;
5084 }
5085 }
5086 return 0;
5087}
5088
5089static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
5090{
5091 int i;
5092
5093 for (i = 0; i < bp->nr_vnics; i++)
5094 bnxt_hwrm_vnic_set_rss(bp, i, false);
5095}
5096
5097static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
5098 bool irq_re_init)
5099{
5100 if (bp->vnic_info) {
5101 bnxt_hwrm_clear_vnic_filter(bp);
5102 /* clear all RSS setting before free vnic ctx */
5103 bnxt_hwrm_clear_vnic_rss(bp);
5104 bnxt_hwrm_vnic_ctx_free(bp);
5105 /* before free the vnic, undo the vnic tpa settings */
5106 if (bp->flags & BNXT_FLAG_TPA)
5107 bnxt_set_tpa(bp, false);
5108 bnxt_hwrm_vnic_free(bp);
5109 }
5110 bnxt_hwrm_ring_free(bp, close_path);
5111 bnxt_hwrm_ring_grp_free(bp);
5112 if (irq_re_init) {
5113 bnxt_hwrm_stat_ctx_free(bp);
5114 bnxt_hwrm_free_tunnel_ports(bp);
5115 }
5116}
5117
Michael Chan39d8ba22017-07-24 12:34:22 -04005118static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
5119{
5120 struct hwrm_func_cfg_input req = {0};
5121 int rc;
5122
5123 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5124 req.fid = cpu_to_le16(0xffff);
5125 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
5126 if (br_mode == BRIDGE_MODE_VEB)
5127 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
5128 else if (br_mode == BRIDGE_MODE_VEPA)
5129 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
5130 else
5131 return -EINVAL;
5132 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5133 if (rc)
5134 rc = -EIO;
5135 return rc;
5136}
5137
Michael Chanc0c050c2015-10-22 16:01:17 -04005138static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
5139{
Michael Chanae10ae72016-12-29 12:13:38 -05005140 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04005141 int rc;
5142
Michael Chanae10ae72016-12-29 12:13:38 -05005143 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
5144 goto skip_rss_ctx;
5145
Michael Chanc0c050c2015-10-22 16:01:17 -04005146 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005147 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04005148 if (rc) {
5149 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5150 vnic_id, rc);
5151 goto vnic_setup_err;
5152 }
5153 bp->rsscos_nr_ctxs++;
5154
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005155 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5156 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5157 if (rc) {
5158 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5159 vnic_id, rc);
5160 goto vnic_setup_err;
5161 }
5162 bp->rsscos_nr_ctxs++;
5163 }
5164
Michael Chanae10ae72016-12-29 12:13:38 -05005165skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04005166 /* configure default vnic, ring grp */
5167 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5168 if (rc) {
5169 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5170 vnic_id, rc);
5171 goto vnic_setup_err;
5172 }
5173
5174 /* Enable RSS hashing on vnic */
5175 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5176 if (rc) {
5177 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5178 vnic_id, rc);
5179 goto vnic_setup_err;
5180 }
5181
5182 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5183 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5184 if (rc) {
5185 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5186 vnic_id, rc);
5187 }
5188 }
5189
5190vnic_setup_err:
5191 return rc;
5192}
5193
5194static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5195{
5196#ifdef CONFIG_RFS_ACCEL
5197 int i, rc = 0;
5198
5199 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005200 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005201 u16 vnic_id = i + 1;
5202 u16 ring_id = i;
5203
5204 if (vnic_id >= bp->nr_vnics)
5205 break;
5206
Michael Chanae10ae72016-12-29 12:13:38 -05005207 vnic = &bp->vnic_info[vnic_id];
5208 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5209 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5210 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005211 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005212 if (rc) {
5213 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5214 vnic_id, rc);
5215 break;
5216 }
5217 rc = bnxt_setup_vnic(bp, vnic_id);
5218 if (rc)
5219 break;
5220 }
5221 return rc;
5222#else
5223 return 0;
5224#endif
5225}
5226
Michael Chan17c71ac2016-07-01 18:46:27 -04005227/* Allow PF and VF with default VLAN to be in promiscuous mode */
5228static bool bnxt_promisc_ok(struct bnxt *bp)
5229{
5230#ifdef CONFIG_BNXT_SRIOV
5231 if (BNXT_VF(bp) && !bp->vf.vlan)
5232 return false;
5233#endif
5234 return true;
5235}
5236
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005237static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5238{
5239 unsigned int rc = 0;
5240
5241 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5242 if (rc) {
5243 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5244 rc);
5245 return rc;
5246 }
5247
5248 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5249 if (rc) {
5250 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5251 rc);
5252 return rc;
5253 }
5254 return rc;
5255}
5256
Michael Chanb664f002015-12-02 01:54:08 -05005257static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005258static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005259
Michael Chanc0c050c2015-10-22 16:01:17 -04005260static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5261{
Michael Chan7d2837d2016-05-04 16:56:44 -04005262 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005263 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005264 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005265
5266 if (irq_re_init) {
5267 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5268 if (rc) {
5269 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5270 rc);
5271 goto err_out;
5272 }
Michael Chan6a4f2942018-01-17 03:21:06 -05005273 if (bp->hw_resc.resv_tx_rings != bp->tx_nr_rings) {
Michael Chan98fdbe72017-08-28 13:40:26 -04005274 int tx = bp->tx_nr_rings;
5275
5276 if (bnxt_hwrm_reserve_tx_rings(bp, &tx) ||
5277 tx < bp->tx_nr_rings) {
5278 rc = -ENOMEM;
5279 goto err_out;
5280 }
5281 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005282 }
5283
5284 rc = bnxt_hwrm_ring_alloc(bp);
5285 if (rc) {
5286 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5287 goto err_out;
5288 }
5289
5290 rc = bnxt_hwrm_ring_grp_alloc(bp);
5291 if (rc) {
5292 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5293 goto err_out;
5294 }
5295
Prashant Sreedharan76595192016-07-18 07:15:22 -04005296 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5297 rx_nr_rings--;
5298
Michael Chanc0c050c2015-10-22 16:01:17 -04005299 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005300 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005301 if (rc) {
5302 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5303 goto err_out;
5304 }
5305
5306 rc = bnxt_setup_vnic(bp, 0);
5307 if (rc)
5308 goto err_out;
5309
5310 if (bp->flags & BNXT_FLAG_RFS) {
5311 rc = bnxt_alloc_rfs_vnics(bp);
5312 if (rc)
5313 goto err_out;
5314 }
5315
5316 if (bp->flags & BNXT_FLAG_TPA) {
5317 rc = bnxt_set_tpa(bp, true);
5318 if (rc)
5319 goto err_out;
5320 }
5321
5322 if (BNXT_VF(bp))
5323 bnxt_update_vf_mac(bp);
5324
5325 /* Filter for default vnic 0 */
5326 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5327 if (rc) {
5328 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5329 goto err_out;
5330 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005331 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005332
Michael Chan7d2837d2016-05-04 16:56:44 -04005333 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005334
Michael Chan17c71ac2016-07-01 18:46:27 -04005335 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005336 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5337
5338 if (bp->dev->flags & IFF_ALLMULTI) {
5339 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5340 vnic->mc_list_count = 0;
5341 } else {
5342 u32 mask = 0;
5343
5344 bnxt_mc_list_updated(bp, &mask);
5345 vnic->rx_mask |= mask;
5346 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005347
Michael Chanb664f002015-12-02 01:54:08 -05005348 rc = bnxt_cfg_rx_mode(bp);
5349 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005350 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005351
5352 rc = bnxt_hwrm_set_coal(bp);
5353 if (rc)
5354 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005355 rc);
5356
5357 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5358 rc = bnxt_setup_nitroa0_vnic(bp);
5359 if (rc)
5360 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5361 rc);
5362 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005363
Michael Chancf6645f2016-06-13 02:25:28 -04005364 if (BNXT_VF(bp)) {
5365 bnxt_hwrm_func_qcfg(bp);
5366 netdev_update_features(bp->dev);
5367 }
5368
Michael Chanc0c050c2015-10-22 16:01:17 -04005369 return 0;
5370
5371err_out:
5372 bnxt_hwrm_resource_free(bp, 0, true);
5373
5374 return rc;
5375}
5376
5377static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5378{
5379 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5380 return 0;
5381}
5382
5383static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5384{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005385 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005386 bnxt_init_rx_rings(bp);
5387 bnxt_init_tx_rings(bp);
5388 bnxt_init_ring_grps(bp, irq_re_init);
5389 bnxt_init_vnics(bp);
5390
5391 return bnxt_init_chip(bp, irq_re_init);
5392}
5393
Michael Chanc0c050c2015-10-22 16:01:17 -04005394static int bnxt_set_real_num_queues(struct bnxt *bp)
5395{
5396 int rc;
5397 struct net_device *dev = bp->dev;
5398
Michael Chan5f449242017-02-06 16:55:40 -05005399 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5400 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005401 if (rc)
5402 return rc;
5403
5404 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5405 if (rc)
5406 return rc;
5407
5408#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005409 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005410 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005411#endif
5412
5413 return rc;
5414}
5415
Michael Chan6e6c5a52016-01-02 23:45:02 -05005416static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5417 bool shared)
5418{
5419 int _rx = *rx, _tx = *tx;
5420
5421 if (shared) {
5422 *rx = min_t(int, _rx, max);
5423 *tx = min_t(int, _tx, max);
5424 } else {
5425 if (max < 2)
5426 return -ENOMEM;
5427
5428 while (_rx + _tx > max) {
5429 if (_rx > _tx && _rx > 1)
5430 _rx--;
5431 else if (_tx > 1)
5432 _tx--;
5433 }
5434 *rx = _rx;
5435 *tx = _tx;
5436 }
5437 return 0;
5438}
5439
Michael Chan78095922016-12-07 00:26:16 -05005440static void bnxt_setup_msix(struct bnxt *bp)
5441{
5442 const int len = sizeof(bp->irq_tbl[0].name);
5443 struct net_device *dev = bp->dev;
5444 int tcs, i;
5445
5446 tcs = netdev_get_num_tc(dev);
5447 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005448 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005449
Michael Chand1e79252017-02-06 16:55:38 -05005450 for (i = 0; i < tcs; i++) {
5451 count = bp->tx_nr_rings_per_tc;
5452 off = i * count;
5453 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005454 }
5455 }
5456
5457 for (i = 0; i < bp->cp_nr_rings; i++) {
5458 char *attr;
5459
5460 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5461 attr = "TxRx";
5462 else if (i < bp->rx_nr_rings)
5463 attr = "rx";
5464 else
5465 attr = "tx";
5466
5467 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5468 i);
5469 bp->irq_tbl[i].handler = bnxt_msix;
5470 }
5471}
5472
5473static void bnxt_setup_inta(struct bnxt *bp)
5474{
5475 const int len = sizeof(bp->irq_tbl[0].name);
5476
5477 if (netdev_get_num_tc(bp->dev))
5478 netdev_reset_tc(bp->dev);
5479
5480 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5481 0);
5482 bp->irq_tbl[0].handler = bnxt_inta;
5483}
5484
5485static int bnxt_setup_int_mode(struct bnxt *bp)
5486{
5487 int rc;
5488
5489 if (bp->flags & BNXT_FLAG_USING_MSIX)
5490 bnxt_setup_msix(bp);
5491 else
5492 bnxt_setup_inta(bp);
5493
5494 rc = bnxt_set_real_num_queues(bp);
5495 return rc;
5496}
5497
Michael Chanb7429952017-01-13 01:32:00 -05005498#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005499static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5500{
Michael Chan6a4f2942018-01-17 03:21:06 -05005501 return bp->hw_resc.max_rsscos_ctxs;
Michael Chan8079e8f2016-12-29 12:13:37 -05005502}
5503
5504static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5505{
Michael Chan6a4f2942018-01-17 03:21:06 -05005506 return bp->hw_resc.max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05005507}
Michael Chanb7429952017-01-13 01:32:00 -05005508#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005509
Michael Chane4060d32016-12-07 00:26:19 -05005510unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5511{
Michael Chan6a4f2942018-01-17 03:21:06 -05005512 return bp->hw_resc.max_stat_ctxs;
Michael Chane4060d32016-12-07 00:26:19 -05005513}
5514
Michael Chana588e452016-12-07 00:26:21 -05005515void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5516{
Michael Chan6a4f2942018-01-17 03:21:06 -05005517 bp->hw_resc.max_stat_ctxs = max;
Michael Chana588e452016-12-07 00:26:21 -05005518}
5519
Michael Chane4060d32016-12-07 00:26:19 -05005520unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5521{
Michael Chan6a4f2942018-01-17 03:21:06 -05005522 return bp->hw_resc.max_cp_rings;
Michael Chane4060d32016-12-07 00:26:19 -05005523}
5524
Michael Chana588e452016-12-07 00:26:21 -05005525void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5526{
Michael Chan6a4f2942018-01-17 03:21:06 -05005527 bp->hw_resc.max_cp_rings = max;
Michael Chana588e452016-12-07 00:26:21 -05005528}
5529
Michael Chan78095922016-12-07 00:26:16 -05005530static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5531{
Michael Chan6a4f2942018-01-17 03:21:06 -05005532 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5533
5534 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005535}
5536
Michael Chan33c26572016-12-07 00:26:15 -05005537void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5538{
Michael Chan6a4f2942018-01-17 03:21:06 -05005539 bp->hw_resc.max_irqs = max_irqs;
Michael Chan33c26572016-12-07 00:26:15 -05005540}
5541
Michael Chan78095922016-12-07 00:26:16 -05005542static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005543{
Michael Chan01657bc2016-01-02 23:45:03 -05005544 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005545 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005546
Michael Chan78095922016-12-07 00:26:16 -05005547 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005548 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5549 if (!msix_ent)
5550 return -ENOMEM;
5551
5552 for (i = 0; i < total_vecs; i++) {
5553 msix_ent[i].entry = i;
5554 msix_ent[i].vector = 0;
5555 }
5556
Michael Chan01657bc2016-01-02 23:45:03 -05005557 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5558 min = 2;
5559
5560 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005561 if (total_vecs < 0) {
5562 rc = -ENODEV;
5563 goto msix_setup_exit;
5564 }
5565
5566 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5567 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005568 for (i = 0; i < total_vecs; i++)
5569 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005570
Michael Chan78095922016-12-07 00:26:16 -05005571 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005572 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005573 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005574 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005575 if (rc)
5576 goto msix_setup_exit;
5577
Michael Chanc0c050c2015-10-22 16:01:17 -04005578 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005579 bp->cp_nr_rings = (min == 1) ?
5580 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5581 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005582
Michael Chanc0c050c2015-10-22 16:01:17 -04005583 } else {
5584 rc = -ENOMEM;
5585 goto msix_setup_exit;
5586 }
5587 bp->flags |= BNXT_FLAG_USING_MSIX;
5588 kfree(msix_ent);
5589 return 0;
5590
5591msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005592 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5593 kfree(bp->irq_tbl);
5594 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005595 pci_disable_msix(bp->pdev);
5596 kfree(msix_ent);
5597 return rc;
5598}
5599
Michael Chan78095922016-12-07 00:26:16 -05005600static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005601{
Michael Chanc0c050c2015-10-22 16:01:17 -04005602 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005603 if (!bp->irq_tbl)
5604 return -ENOMEM;
5605
5606 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005607 bp->rx_nr_rings = 1;
5608 bp->tx_nr_rings = 1;
5609 bp->cp_nr_rings = 1;
5610 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005611 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005612 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005613 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005614}
5615
Michael Chan78095922016-12-07 00:26:16 -05005616static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005617{
5618 int rc = 0;
5619
5620 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005621 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005622
Michael Chan1fa72e22016-04-25 02:30:49 -04005623 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005624 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005625 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005626 }
5627 return rc;
5628}
5629
Michael Chan78095922016-12-07 00:26:16 -05005630static void bnxt_clear_int_mode(struct bnxt *bp)
5631{
5632 if (bp->flags & BNXT_FLAG_USING_MSIX)
5633 pci_disable_msix(bp->pdev);
5634
5635 kfree(bp->irq_tbl);
5636 bp->irq_tbl = NULL;
5637 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5638}
5639
Michael Chanc0c050c2015-10-22 16:01:17 -04005640static void bnxt_free_irq(struct bnxt *bp)
5641{
5642 struct bnxt_irq *irq;
5643 int i;
5644
5645#ifdef CONFIG_RFS_ACCEL
5646 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5647 bp->dev->rx_cpu_rmap = NULL;
5648#endif
5649 if (!bp->irq_tbl)
5650 return;
5651
5652 for (i = 0; i < bp->cp_nr_rings; i++) {
5653 irq = &bp->irq_tbl[i];
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005654 if (irq->requested) {
5655 if (irq->have_cpumask) {
5656 irq_set_affinity_hint(irq->vector, NULL);
5657 free_cpumask_var(irq->cpu_mask);
5658 irq->have_cpumask = 0;
5659 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005660 free_irq(irq->vector, bp->bnapi[i]);
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005661 }
5662
Michael Chanc0c050c2015-10-22 16:01:17 -04005663 irq->requested = 0;
5664 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005665}
5666
5667static int bnxt_request_irq(struct bnxt *bp)
5668{
Michael Chanb81a90d2016-01-02 23:45:01 -05005669 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005670 unsigned long flags = 0;
5671#ifdef CONFIG_RFS_ACCEL
5672 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5673#endif
5674
5675 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5676 flags = IRQF_SHARED;
5677
Michael Chanb81a90d2016-01-02 23:45:01 -05005678 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005679 struct bnxt_irq *irq = &bp->irq_tbl[i];
5680#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005681 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005682 rc = irq_cpu_rmap_add(rmap, irq->vector);
5683 if (rc)
5684 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005685 j);
5686 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005687 }
5688#endif
5689 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5690 bp->bnapi[i]);
5691 if (rc)
5692 break;
5693
5694 irq->requested = 1;
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005695
5696 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
5697 int numa_node = dev_to_node(&bp->pdev->dev);
5698
5699 irq->have_cpumask = 1;
5700 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
5701 irq->cpu_mask);
5702 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
5703 if (rc) {
5704 netdev_warn(bp->dev,
5705 "Set affinity failed, IRQ = %d\n",
5706 irq->vector);
5707 break;
5708 }
5709 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005710 }
5711 return rc;
5712}
5713
5714static void bnxt_del_napi(struct bnxt *bp)
5715{
5716 int i;
5717
5718 if (!bp->bnapi)
5719 return;
5720
5721 for (i = 0; i < bp->cp_nr_rings; i++) {
5722 struct bnxt_napi *bnapi = bp->bnapi[i];
5723
5724 napi_hash_del(&bnapi->napi);
5725 netif_napi_del(&bnapi->napi);
5726 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005727 /* We called napi_hash_del() before netif_napi_del(), we need
5728 * to respect an RCU grace period before freeing napi structures.
5729 */
5730 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005731}
5732
5733static void bnxt_init_napi(struct bnxt *bp)
5734{
5735 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005736 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005737 struct bnxt_napi *bnapi;
5738
5739 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005740 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5741 cp_nr_rings--;
5742 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005743 bnapi = bp->bnapi[i];
5744 netif_napi_add(bp->dev, &bnapi->napi,
5745 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005746 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005747 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5748 bnapi = bp->bnapi[cp_nr_rings];
5749 netif_napi_add(bp->dev, &bnapi->napi,
5750 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005751 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005752 } else {
5753 bnapi = bp->bnapi[0];
5754 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005755 }
5756}
5757
5758static void bnxt_disable_napi(struct bnxt *bp)
5759{
5760 int i;
5761
5762 if (!bp->bnapi)
5763 return;
5764
Michael Chanb356a2e2016-12-29 12:13:31 -05005765 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005766 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005767}
5768
5769static void bnxt_enable_napi(struct bnxt *bp)
5770{
5771 int i;
5772
5773 for (i = 0; i < bp->cp_nr_rings; i++) {
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005774 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
Michael Chanfa7e2812016-05-10 19:18:00 -04005775 bp->bnapi[i]->in_reset = false;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005776
5777 if (bp->bnapi[i]->rx_ring) {
5778 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
5779 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
5780 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005781 napi_enable(&bp->bnapi[i]->napi);
5782 }
5783}
5784
Michael Chan7df4ae92016-12-02 21:17:17 -05005785void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005786{
5787 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005788 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04005789
Michael Chanb6ab4b02016-01-02 23:44:59 -05005790 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005791 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005792 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005793 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005794 }
5795 }
5796 /* Stop all TX queues */
5797 netif_tx_disable(bp->dev);
5798 netif_carrier_off(bp->dev);
5799}
5800
Michael Chan7df4ae92016-12-02 21:17:17 -05005801void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005802{
5803 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005804 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04005805
5806 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005807 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005808 txr->dev_state = 0;
5809 }
5810 netif_tx_wake_all_queues(bp->dev);
5811 if (bp->link_info.link_up)
5812 netif_carrier_on(bp->dev);
5813}
5814
5815static void bnxt_report_link(struct bnxt *bp)
5816{
5817 if (bp->link_info.link_up) {
5818 const char *duplex;
5819 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005820 u32 speed;
5821 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005822
5823 netif_carrier_on(bp->dev);
5824 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5825 duplex = "full";
5826 else
5827 duplex = "half";
5828 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5829 flow_ctrl = "ON - receive & transmit";
5830 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5831 flow_ctrl = "ON - transmit";
5832 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5833 flow_ctrl = "ON - receive";
5834 else
5835 flow_ctrl = "none";
5836 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005837 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005838 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005839 if (bp->flags & BNXT_FLAG_EEE_CAP)
5840 netdev_info(bp->dev, "EEE is %s\n",
5841 bp->eee.eee_active ? "active" :
5842 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005843 fec = bp->link_info.fec_cfg;
5844 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5845 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5846 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5847 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5848 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005849 } else {
5850 netif_carrier_off(bp->dev);
5851 netdev_err(bp->dev, "NIC Link is Down\n");
5852 }
5853}
5854
Michael Chan170ce012016-04-05 14:08:57 -04005855static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5856{
5857 int rc = 0;
5858 struct hwrm_port_phy_qcaps_input req = {0};
5859 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005860 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005861
5862 if (bp->hwrm_spec_code < 0x10201)
5863 return 0;
5864
5865 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5866
5867 mutex_lock(&bp->hwrm_cmd_lock);
5868 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5869 if (rc)
5870 goto hwrm_phy_qcaps_exit;
5871
Michael Chanacb20052017-07-24 12:34:20 -04005872 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005873 struct ethtool_eee *eee = &bp->eee;
5874 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5875
5876 bp->flags |= BNXT_FLAG_EEE_CAP;
5877 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5878 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5879 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5880 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5881 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5882 }
Michael Chan520ad892017-03-08 18:44:35 -05005883 if (resp->supported_speeds_auto_mode)
5884 link_info->support_auto_speeds =
5885 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005886
Michael Chand5430d32017-08-28 13:40:31 -04005887 bp->port_count = resp->port_cnt;
5888
Michael Chan170ce012016-04-05 14:08:57 -04005889hwrm_phy_qcaps_exit:
5890 mutex_unlock(&bp->hwrm_cmd_lock);
5891 return rc;
5892}
5893
Michael Chanc0c050c2015-10-22 16:01:17 -04005894static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5895{
5896 int rc = 0;
5897 struct bnxt_link_info *link_info = &bp->link_info;
5898 struct hwrm_port_phy_qcfg_input req = {0};
5899 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5900 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005901 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005902
5903 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5904
5905 mutex_lock(&bp->hwrm_cmd_lock);
5906 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5907 if (rc) {
5908 mutex_unlock(&bp->hwrm_cmd_lock);
5909 return rc;
5910 }
5911
5912 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5913 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005914 link_info->duplex = resp->duplex_cfg;
5915 if (bp->hwrm_spec_code >= 0x10800)
5916 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005917 link_info->pause = resp->pause;
5918 link_info->auto_mode = resp->auto_mode;
5919 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005920 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005921 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005922 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005923 if (link_info->phy_link_status == BNXT_LINK_LINK)
5924 link_info->link_speed = le16_to_cpu(resp->link_speed);
5925 else
5926 link_info->link_speed = 0;
5927 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005928 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5929 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005930 link_info->lp_auto_link_speeds =
5931 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005932 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5933 link_info->phy_ver[0] = resp->phy_maj;
5934 link_info->phy_ver[1] = resp->phy_min;
5935 link_info->phy_ver[2] = resp->phy_bld;
5936 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005937 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005938 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005939 link_info->phy_addr = resp->eee_config_phy_addr &
5940 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005941 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005942
Michael Chan170ce012016-04-05 14:08:57 -04005943 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5944 struct ethtool_eee *eee = &bp->eee;
5945 u16 fw_speeds;
5946
5947 eee->eee_active = 0;
5948 if (resp->eee_config_phy_addr &
5949 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5950 eee->eee_active = 1;
5951 fw_speeds = le16_to_cpu(
5952 resp->link_partner_adv_eee_link_speed_mask);
5953 eee->lp_advertised =
5954 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5955 }
5956
5957 /* Pull initial EEE config */
5958 if (!chng_link_state) {
5959 if (resp->eee_config_phy_addr &
5960 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5961 eee->eee_enabled = 1;
5962
5963 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5964 eee->advertised =
5965 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5966
5967 if (resp->eee_config_phy_addr &
5968 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5969 __le32 tmr;
5970
5971 eee->tx_lpi_enabled = 1;
5972 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5973 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5974 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5975 }
5976 }
5977 }
Michael Chane70c7522017-02-12 19:18:16 -05005978
5979 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5980 if (bp->hwrm_spec_code >= 0x10504)
5981 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5982
Michael Chanc0c050c2015-10-22 16:01:17 -04005983 /* TODO: need to add more logic to report VF link */
5984 if (chng_link_state) {
5985 if (link_info->phy_link_status == BNXT_LINK_LINK)
5986 link_info->link_up = 1;
5987 else
5988 link_info->link_up = 0;
5989 if (link_up != link_info->link_up)
5990 bnxt_report_link(bp);
5991 } else {
5992 /* alwasy link down if not require to update link state */
5993 link_info->link_up = 0;
5994 }
5995 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005996
5997 diff = link_info->support_auto_speeds ^ link_info->advertising;
5998 if ((link_info->support_auto_speeds | diff) !=
5999 link_info->support_auto_speeds) {
6000 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05006001 * update the advertisement settings. Caller holds RTNL
6002 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05006003 */
Michael Chan286ef9d2016-11-16 21:13:08 -05006004 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05006005 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05006006 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05006007 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006008 return 0;
6009}
6010
Michael Chan10289be2016-05-15 03:04:49 -04006011static void bnxt_get_port_module_status(struct bnxt *bp)
6012{
6013 struct bnxt_link_info *link_info = &bp->link_info;
6014 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
6015 u8 module_status;
6016
6017 if (bnxt_update_link(bp, true))
6018 return;
6019
6020 module_status = link_info->module_status;
6021 switch (module_status) {
6022 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
6023 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
6024 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
6025 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
6026 bp->pf.port_id);
6027 if (bp->hwrm_spec_code >= 0x10201) {
6028 netdev_warn(bp->dev, "Module part number %s\n",
6029 resp->phy_vendor_partnumber);
6030 }
6031 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
6032 netdev_warn(bp->dev, "TX is disabled\n");
6033 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
6034 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
6035 }
6036}
6037
Michael Chanc0c050c2015-10-22 16:01:17 -04006038static void
6039bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
6040{
6041 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006042 if (bp->hwrm_spec_code >= 0x10201)
6043 req->auto_pause =
6044 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04006045 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6046 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
6047 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04006048 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04006049 req->enables |=
6050 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6051 } else {
6052 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6053 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
6054 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6055 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
6056 req->enables |=
6057 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04006058 if (bp->hwrm_spec_code >= 0x10201) {
6059 req->auto_pause = req->force_pause;
6060 req->enables |= cpu_to_le32(
6061 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6062 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006063 }
6064}
6065
6066static void bnxt_hwrm_set_link_common(struct bnxt *bp,
6067 struct hwrm_port_phy_cfg_input *req)
6068{
6069 u8 autoneg = bp->link_info.autoneg;
6070 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05006071 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04006072
6073 if (autoneg & BNXT_AUTONEG_SPEED) {
6074 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04006075 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04006076
6077 req->enables |= cpu_to_le32(
6078 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
6079 req->auto_link_speed_mask = cpu_to_le16(advertising);
6080
6081 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
6082 req->flags |=
6083 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
6084 } else {
6085 req->force_link_speed = cpu_to_le16(fw_link_speed);
6086 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
6087 }
6088
Michael Chanc0c050c2015-10-22 16:01:17 -04006089 /* tell chimp that the setting takes effect immediately */
6090 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
6091}
6092
6093int bnxt_hwrm_set_pause(struct bnxt *bp)
6094{
6095 struct hwrm_port_phy_cfg_input req = {0};
6096 int rc;
6097
6098 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6099 bnxt_hwrm_set_pause_common(bp, &req);
6100
6101 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
6102 bp->link_info.force_link_chng)
6103 bnxt_hwrm_set_link_common(bp, &req);
6104
6105 mutex_lock(&bp->hwrm_cmd_lock);
6106 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6107 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
6108 /* since changing of pause setting doesn't trigger any link
6109 * change event, the driver needs to update the current pause
6110 * result upon successfully return of the phy_cfg command
6111 */
6112 bp->link_info.pause =
6113 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
6114 bp->link_info.auto_pause_setting = 0;
6115 if (!bp->link_info.force_link_chng)
6116 bnxt_report_link(bp);
6117 }
6118 bp->link_info.force_link_chng = false;
6119 mutex_unlock(&bp->hwrm_cmd_lock);
6120 return rc;
6121}
6122
Michael Chan939f7f02016-04-05 14:08:58 -04006123static void bnxt_hwrm_set_eee(struct bnxt *bp,
6124 struct hwrm_port_phy_cfg_input *req)
6125{
6126 struct ethtool_eee *eee = &bp->eee;
6127
6128 if (eee->eee_enabled) {
6129 u16 eee_speeds;
6130 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
6131
6132 if (eee->tx_lpi_enabled)
6133 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
6134 else
6135 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
6136
6137 req->flags |= cpu_to_le32(flags);
6138 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
6139 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
6140 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
6141 } else {
6142 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
6143 }
6144}
6145
6146int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04006147{
6148 struct hwrm_port_phy_cfg_input req = {0};
6149
6150 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6151 if (set_pause)
6152 bnxt_hwrm_set_pause_common(bp, &req);
6153
6154 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04006155
6156 if (set_eee)
6157 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04006158 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6159}
6160
Michael Chan33f7d552016-04-11 04:11:12 -04006161static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6162{
6163 struct hwrm_port_phy_cfg_input req = {0};
6164
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006165 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04006166 return 0;
6167
6168 if (pci_num_vf(bp->pdev))
6169 return 0;
6170
6171 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05006172 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04006173 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6174}
6175
Michael Chan5ad2cbe2017-01-13 01:32:03 -05006176static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6177{
6178 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6179 struct hwrm_port_led_qcaps_input req = {0};
6180 struct bnxt_pf_info *pf = &bp->pf;
6181 int rc;
6182
6183 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6184 return 0;
6185
6186 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6187 req.port_id = cpu_to_le16(pf->port_id);
6188 mutex_lock(&bp->hwrm_cmd_lock);
6189 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6190 if (rc) {
6191 mutex_unlock(&bp->hwrm_cmd_lock);
6192 return rc;
6193 }
6194 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6195 int i;
6196
6197 bp->num_leds = resp->num_leds;
6198 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6199 bp->num_leds);
6200 for (i = 0; i < bp->num_leds; i++) {
6201 struct bnxt_led_info *led = &bp->leds[i];
6202 __le16 caps = led->led_state_caps;
6203
6204 if (!led->led_group_id ||
6205 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6206 bp->num_leds = 0;
6207 break;
6208 }
6209 }
6210 }
6211 mutex_unlock(&bp->hwrm_cmd_lock);
6212 return 0;
6213}
6214
Michael Chan5282db62017-04-04 18:14:10 -04006215int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6216{
6217 struct hwrm_wol_filter_alloc_input req = {0};
6218 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6219 int rc;
6220
6221 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6222 req.port_id = cpu_to_le16(bp->pf.port_id);
6223 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6224 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6225 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6226 mutex_lock(&bp->hwrm_cmd_lock);
6227 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6228 if (!rc)
6229 bp->wol_filter_id = resp->wol_filter_id;
6230 mutex_unlock(&bp->hwrm_cmd_lock);
6231 return rc;
6232}
6233
6234int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6235{
6236 struct hwrm_wol_filter_free_input req = {0};
6237 int rc;
6238
6239 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6240 req.port_id = cpu_to_le16(bp->pf.port_id);
6241 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6242 req.wol_filter_id = bp->wol_filter_id;
6243 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6244 return rc;
6245}
6246
Michael Chanc1ef1462017-04-04 18:14:07 -04006247static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6248{
6249 struct hwrm_wol_filter_qcfg_input req = {0};
6250 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6251 u16 next_handle = 0;
6252 int rc;
6253
6254 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6255 req.port_id = cpu_to_le16(bp->pf.port_id);
6256 req.handle = cpu_to_le16(handle);
6257 mutex_lock(&bp->hwrm_cmd_lock);
6258 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6259 if (!rc) {
6260 next_handle = le16_to_cpu(resp->next_handle);
6261 if (next_handle != 0) {
6262 if (resp->wol_type ==
6263 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6264 bp->wol = 1;
6265 bp->wol_filter_id = resp->wol_filter_id;
6266 }
6267 }
6268 }
6269 mutex_unlock(&bp->hwrm_cmd_lock);
6270 return next_handle;
6271}
6272
6273static void bnxt_get_wol_settings(struct bnxt *bp)
6274{
6275 u16 handle = 0;
6276
6277 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6278 return;
6279
6280 do {
6281 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6282 } while (handle && handle != 0xffff);
6283}
6284
Michael Chan939f7f02016-04-05 14:08:58 -04006285static bool bnxt_eee_config_ok(struct bnxt *bp)
6286{
6287 struct ethtool_eee *eee = &bp->eee;
6288 struct bnxt_link_info *link_info = &bp->link_info;
6289
6290 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6291 return true;
6292
6293 if (eee->eee_enabled) {
6294 u32 advertising =
6295 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6296
6297 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6298 eee->eee_enabled = 0;
6299 return false;
6300 }
6301 if (eee->advertised & ~advertising) {
6302 eee->advertised = advertising & eee->supported;
6303 return false;
6304 }
6305 }
6306 return true;
6307}
6308
Michael Chanc0c050c2015-10-22 16:01:17 -04006309static int bnxt_update_phy_setting(struct bnxt *bp)
6310{
6311 int rc;
6312 bool update_link = false;
6313 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006314 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006315 struct bnxt_link_info *link_info = &bp->link_info;
6316
6317 rc = bnxt_update_link(bp, true);
6318 if (rc) {
6319 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6320 rc);
6321 return rc;
6322 }
Michael Chan33dac242017-02-12 19:18:15 -05006323 if (!BNXT_SINGLE_PF(bp))
6324 return 0;
6325
Michael Chanc0c050c2015-10-22 16:01:17 -04006326 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006327 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6328 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006329 update_pause = true;
6330 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6331 link_info->force_pause_setting != link_info->req_flow_ctrl)
6332 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006333 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6334 if (BNXT_AUTO_MODE(link_info->auto_mode))
6335 update_link = true;
6336 if (link_info->req_link_speed != link_info->force_link_speed)
6337 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006338 if (link_info->req_duplex != link_info->duplex_setting)
6339 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006340 } else {
6341 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6342 update_link = true;
6343 if (link_info->advertising != link_info->auto_link_speeds)
6344 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006345 }
6346
Michael Chan16d663a2016-11-16 21:13:07 -05006347 /* The last close may have shutdown the link, so need to call
6348 * PHY_CFG to bring it back up.
6349 */
6350 if (!netif_carrier_ok(bp->dev))
6351 update_link = true;
6352
Michael Chan939f7f02016-04-05 14:08:58 -04006353 if (!bnxt_eee_config_ok(bp))
6354 update_eee = true;
6355
Michael Chanc0c050c2015-10-22 16:01:17 -04006356 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006357 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006358 else if (update_pause)
6359 rc = bnxt_hwrm_set_pause(bp);
6360 if (rc) {
6361 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6362 rc);
6363 return rc;
6364 }
6365
6366 return rc;
6367}
6368
Jeffrey Huang11809492015-11-05 16:25:49 -05006369/* Common routine to pre-map certain register block to different GRC window.
6370 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6371 * in PF and 3 windows in VF that can be customized to map in different
6372 * register blocks.
6373 */
6374static void bnxt_preset_reg_win(struct bnxt *bp)
6375{
6376 if (BNXT_PF(bp)) {
6377 /* CAG registers map to GRC window #4 */
6378 writel(BNXT_CAG_REG_BASE,
6379 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6380 }
6381}
6382
Michael Chanc0c050c2015-10-22 16:01:17 -04006383static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6384{
6385 int rc = 0;
6386
Jeffrey Huang11809492015-11-05 16:25:49 -05006387 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006388 netif_carrier_off(bp->dev);
6389 if (irq_re_init) {
6390 rc = bnxt_setup_int_mode(bp);
6391 if (rc) {
6392 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6393 rc);
6394 return rc;
6395 }
6396 }
6397 if ((bp->flags & BNXT_FLAG_RFS) &&
6398 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6399 /* disable RFS if falling back to INTA */
6400 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6401 bp->flags &= ~BNXT_FLAG_RFS;
6402 }
6403
6404 rc = bnxt_alloc_mem(bp, irq_re_init);
6405 if (rc) {
6406 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6407 goto open_err_free_mem;
6408 }
6409
6410 if (irq_re_init) {
6411 bnxt_init_napi(bp);
6412 rc = bnxt_request_irq(bp);
6413 if (rc) {
6414 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6415 goto open_err;
6416 }
6417 }
6418
6419 bnxt_enable_napi(bp);
6420
6421 rc = bnxt_init_nic(bp, irq_re_init);
6422 if (rc) {
6423 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6424 goto open_err;
6425 }
6426
6427 if (link_re_init) {
Michael Chane2dc9b62017-10-13 21:09:30 -04006428 mutex_lock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006429 rc = bnxt_update_phy_setting(bp);
Michael Chane2dc9b62017-10-13 21:09:30 -04006430 mutex_unlock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006431 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006432 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006433 }
6434
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006435 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006436 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006437
Michael Chancaefe522015-12-09 19:35:42 -05006438 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006439 bnxt_enable_int(bp);
6440 /* Enable TX queues */
6441 bnxt_tx_enable(bp);
6442 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006443 /* Poll link status and check for SFP+ module status */
6444 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006445
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006446 /* VF-reps may need to be re-opened after the PF is re-opened */
6447 if (BNXT_PF(bp))
6448 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006449 return 0;
6450
6451open_err:
6452 bnxt_disable_napi(bp);
6453 bnxt_del_napi(bp);
6454
6455open_err_free_mem:
6456 bnxt_free_skbs(bp);
6457 bnxt_free_irq(bp);
6458 bnxt_free_mem(bp, true);
6459 return rc;
6460}
6461
6462/* rtnl_lock held */
6463int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6464{
6465 int rc = 0;
6466
6467 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6468 if (rc) {
6469 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6470 dev_close(bp->dev);
6471 }
6472 return rc;
6473}
6474
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006475/* rtnl_lock held, open the NIC half way by allocating all resources, but
6476 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6477 * self tests.
6478 */
6479int bnxt_half_open_nic(struct bnxt *bp)
6480{
6481 int rc = 0;
6482
6483 rc = bnxt_alloc_mem(bp, false);
6484 if (rc) {
6485 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6486 goto half_open_err;
6487 }
6488 rc = bnxt_init_nic(bp, false);
6489 if (rc) {
6490 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6491 goto half_open_err;
6492 }
6493 return 0;
6494
6495half_open_err:
6496 bnxt_free_skbs(bp);
6497 bnxt_free_mem(bp, false);
6498 dev_close(bp->dev);
6499 return rc;
6500}
6501
6502/* rtnl_lock held, this call can only be made after a previous successful
6503 * call to bnxt_half_open_nic().
6504 */
6505void bnxt_half_close_nic(struct bnxt *bp)
6506{
6507 bnxt_hwrm_resource_free(bp, false, false);
6508 bnxt_free_skbs(bp);
6509 bnxt_free_mem(bp, false);
6510}
6511
Michael Chanc0c050c2015-10-22 16:01:17 -04006512static int bnxt_open(struct net_device *dev)
6513{
6514 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006515
Michael Chanc0c050c2015-10-22 16:01:17 -04006516 return __bnxt_open_nic(bp, true, true);
6517}
6518
Michael Chanf9b76eb2017-07-11 13:05:34 -04006519static bool bnxt_drv_busy(struct bnxt *bp)
6520{
6521 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6522 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6523}
6524
Michael Chan86e953d2018-01-17 03:21:04 -05006525static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
6526 bool link_re_init)
Michael Chanc0c050c2015-10-22 16:01:17 -04006527{
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006528 /* Close the VF-reps before closing PF */
6529 if (BNXT_PF(bp))
6530 bnxt_vf_reps_close(bp);
Michael Chan86e953d2018-01-17 03:21:04 -05006531
Michael Chanc0c050c2015-10-22 16:01:17 -04006532 /* Change device state to avoid TX queue wake up's */
6533 bnxt_tx_disable(bp);
6534
Michael Chancaefe522015-12-09 19:35:42 -05006535 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006536 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006537 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006538 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006539
Michael Chan9d8bc092016-12-29 12:13:33 -05006540 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006541 bnxt_shutdown_nic(bp, irq_re_init);
6542
6543 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6544
6545 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006546 del_timer_sync(&bp->timer);
6547 bnxt_free_skbs(bp);
6548
6549 if (irq_re_init) {
6550 bnxt_free_irq(bp);
6551 bnxt_del_napi(bp);
6552 }
6553 bnxt_free_mem(bp, irq_re_init);
Michael Chan86e953d2018-01-17 03:21:04 -05006554}
6555
6556int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6557{
6558 int rc = 0;
6559
6560#ifdef CONFIG_BNXT_SRIOV
6561 if (bp->sriov_cfg) {
6562 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6563 !bp->sriov_cfg,
6564 BNXT_SRIOV_CFG_WAIT_TMO);
6565 if (rc)
6566 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6567 }
6568#endif
6569 __bnxt_close_nic(bp, irq_re_init, link_re_init);
Michael Chanc0c050c2015-10-22 16:01:17 -04006570 return rc;
6571}
6572
6573static int bnxt_close(struct net_device *dev)
6574{
6575 struct bnxt *bp = netdev_priv(dev);
6576
6577 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006578 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006579 return 0;
6580}
6581
6582/* rtnl_lock held */
6583static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6584{
6585 switch (cmd) {
6586 case SIOCGMIIPHY:
6587 /* fallthru */
6588 case SIOCGMIIREG: {
6589 if (!netif_running(dev))
6590 return -EAGAIN;
6591
6592 return 0;
6593 }
6594
6595 case SIOCSMIIREG:
6596 if (!netif_running(dev))
6597 return -EAGAIN;
6598
6599 return 0;
6600
6601 default:
6602 /* do nothing */
6603 break;
6604 }
6605 return -EOPNOTSUPP;
6606}
6607
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006608static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006609bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6610{
6611 u32 i;
6612 struct bnxt *bp = netdev_priv(dev);
6613
Michael Chanf9b76eb2017-07-11 13:05:34 -04006614 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6615 /* Make sure bnxt_close_nic() sees that we are reading stats before
6616 * we check the BNXT_STATE_OPEN flag.
6617 */
6618 smp_mb__after_atomic();
6619 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6620 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006621 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006622 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006623
6624 /* TODO check if we need to synchronize with bnxt_close path */
6625 for (i = 0; i < bp->cp_nr_rings; i++) {
6626 struct bnxt_napi *bnapi = bp->bnapi[i];
6627 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6628 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6629
6630 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6631 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6632 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6633
6634 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6635 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6636 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6637
6638 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6639 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6640 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6641
6642 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6643 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6644 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6645
6646 stats->rx_missed_errors +=
6647 le64_to_cpu(hw_stats->rx_discard_pkts);
6648
6649 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6650
Michael Chanc0c050c2015-10-22 16:01:17 -04006651 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6652 }
6653
Michael Chan9947f832016-03-07 15:38:46 -05006654 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6655 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6656 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6657
6658 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6659 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6660 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6661 le64_to_cpu(rx->rx_ovrsz_frames) +
6662 le64_to_cpu(rx->rx_runt_frames);
6663 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6664 le64_to_cpu(rx->rx_jbr_frames);
6665 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6666 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6667 stats->tx_errors = le64_to_cpu(tx->tx_err);
6668 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006669 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006670}
6671
6672static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6673{
6674 struct net_device *dev = bp->dev;
6675 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6676 struct netdev_hw_addr *ha;
6677 u8 *haddr;
6678 int mc_count = 0;
6679 bool update = false;
6680 int off = 0;
6681
6682 netdev_for_each_mc_addr(ha, dev) {
6683 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6684 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6685 vnic->mc_list_count = 0;
6686 return false;
6687 }
6688 haddr = ha->addr;
6689 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6690 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6691 update = true;
6692 }
6693 off += ETH_ALEN;
6694 mc_count++;
6695 }
6696 if (mc_count)
6697 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6698
6699 if (mc_count != vnic->mc_list_count) {
6700 vnic->mc_list_count = mc_count;
6701 update = true;
6702 }
6703 return update;
6704}
6705
6706static bool bnxt_uc_list_updated(struct bnxt *bp)
6707{
6708 struct net_device *dev = bp->dev;
6709 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6710 struct netdev_hw_addr *ha;
6711 int off = 0;
6712
6713 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6714 return true;
6715
6716 netdev_for_each_uc_addr(ha, dev) {
6717 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6718 return true;
6719
6720 off += ETH_ALEN;
6721 }
6722 return false;
6723}
6724
6725static void bnxt_set_rx_mode(struct net_device *dev)
6726{
6727 struct bnxt *bp = netdev_priv(dev);
6728 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6729 u32 mask = vnic->rx_mask;
6730 bool mc_update = false;
6731 bool uc_update;
6732
6733 if (!netif_running(dev))
6734 return;
6735
6736 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6737 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6738 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6739
Michael Chan17c71ac2016-07-01 18:46:27 -04006740 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006741 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6742
6743 uc_update = bnxt_uc_list_updated(bp);
6744
6745 if (dev->flags & IFF_ALLMULTI) {
6746 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6747 vnic->mc_list_count = 0;
6748 } else {
6749 mc_update = bnxt_mc_list_updated(bp, &mask);
6750 }
6751
6752 if (mask != vnic->rx_mask || uc_update || mc_update) {
6753 vnic->rx_mask = mask;
6754
6755 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04006756 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006757 }
6758}
6759
Michael Chanb664f002015-12-02 01:54:08 -05006760static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006761{
6762 struct net_device *dev = bp->dev;
6763 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6764 struct netdev_hw_addr *ha;
6765 int i, off = 0, rc;
6766 bool uc_update;
6767
6768 netif_addr_lock_bh(dev);
6769 uc_update = bnxt_uc_list_updated(bp);
6770 netif_addr_unlock_bh(dev);
6771
6772 if (!uc_update)
6773 goto skip_uc;
6774
6775 mutex_lock(&bp->hwrm_cmd_lock);
6776 for (i = 1; i < vnic->uc_filter_count; i++) {
6777 struct hwrm_cfa_l2_filter_free_input req = {0};
6778
6779 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6780 -1);
6781
6782 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6783
6784 rc = _hwrm_send_message(bp, &req, sizeof(req),
6785 HWRM_CMD_TIMEOUT);
6786 }
6787 mutex_unlock(&bp->hwrm_cmd_lock);
6788
6789 vnic->uc_filter_count = 1;
6790
6791 netif_addr_lock_bh(dev);
6792 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6793 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6794 } else {
6795 netdev_for_each_uc_addr(ha, dev) {
6796 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6797 off += ETH_ALEN;
6798 vnic->uc_filter_count++;
6799 }
6800 }
6801 netif_addr_unlock_bh(dev);
6802
6803 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6804 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6805 if (rc) {
6806 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6807 rc);
6808 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006809 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006810 }
6811 }
6812
6813skip_uc:
6814 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6815 if (rc)
6816 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6817 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006818
6819 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006820}
6821
Michael Chan8079e8f2016-12-29 12:13:37 -05006822/* If the chip and firmware supports RFS */
6823static bool bnxt_rfs_supported(struct bnxt *bp)
6824{
6825 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6826 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006827 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6828 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006829 return false;
6830}
6831
6832/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006833static bool bnxt_rfs_capable(struct bnxt *bp)
6834{
6835#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006836 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006837
Michael Chan964fd482017-02-12 19:18:13 -05006838 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006839 return false;
6840
6841 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006842 max_vnics = bnxt_get_max_func_vnics(bp);
6843 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006844
6845 /* RSS contexts not a limiting factor */
6846 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6847 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006848 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006849 netdev_warn(bp->dev,
6850 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006851 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006852 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006853 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006854
6855 return true;
6856#else
6857 return false;
6858#endif
6859}
6860
Michael Chanc0c050c2015-10-22 16:01:17 -04006861static netdev_features_t bnxt_fix_features(struct net_device *dev,
6862 netdev_features_t features)
6863{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006864 struct bnxt *bp = netdev_priv(dev);
6865
Vasundhara Volama2304902016-07-25 12:33:36 -04006866 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006867 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006868
Michael Chan1054aee2017-12-16 03:09:42 -05006869 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6870 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
6871
6872 if (!(features & NETIF_F_GRO))
6873 features &= ~NETIF_F_GRO_HW;
6874
6875 if (features & NETIF_F_GRO_HW)
6876 features &= ~NETIF_F_LRO;
6877
Michael Chan5a9f6b22016-06-06 02:37:15 -04006878 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6879 * turned on or off together.
6880 */
6881 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6882 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6883 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6884 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6885 NETIF_F_HW_VLAN_STAG_RX);
6886 else
6887 features |= NETIF_F_HW_VLAN_CTAG_RX |
6888 NETIF_F_HW_VLAN_STAG_RX;
6889 }
Michael Chancf6645f2016-06-13 02:25:28 -04006890#ifdef CONFIG_BNXT_SRIOV
6891 if (BNXT_VF(bp)) {
6892 if (bp->vf.vlan) {
6893 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6894 NETIF_F_HW_VLAN_STAG_RX);
6895 }
6896 }
6897#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006898 return features;
6899}
6900
6901static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6902{
6903 struct bnxt *bp = netdev_priv(dev);
6904 u32 flags = bp->flags;
6905 u32 changes;
6906 int rc = 0;
6907 bool re_init = false;
6908 bool update_tpa = false;
6909
6910 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Michael Chan1054aee2017-12-16 03:09:42 -05006911 if (features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04006912 flags |= BNXT_FLAG_GRO;
Michael Chan1054aee2017-12-16 03:09:42 -05006913 else if (features & NETIF_F_LRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04006914 flags |= BNXT_FLAG_LRO;
6915
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006916 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6917 flags &= ~BNXT_FLAG_TPA;
6918
Michael Chanc0c050c2015-10-22 16:01:17 -04006919 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6920 flags |= BNXT_FLAG_STRIP_VLAN;
6921
6922 if (features & NETIF_F_NTUPLE)
6923 flags |= BNXT_FLAG_RFS;
6924
6925 changes = flags ^ bp->flags;
6926 if (changes & BNXT_FLAG_TPA) {
6927 update_tpa = true;
6928 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6929 (flags & BNXT_FLAG_TPA) == 0)
6930 re_init = true;
6931 }
6932
6933 if (changes & ~BNXT_FLAG_TPA)
6934 re_init = true;
6935
6936 if (flags != bp->flags) {
6937 u32 old_flags = bp->flags;
6938
6939 bp->flags = flags;
6940
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006941 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006942 if (update_tpa)
6943 bnxt_set_ring_params(bp);
6944 return rc;
6945 }
6946
6947 if (re_init) {
6948 bnxt_close_nic(bp, false, false);
6949 if (update_tpa)
6950 bnxt_set_ring_params(bp);
6951
6952 return bnxt_open_nic(bp, false, false);
6953 }
6954 if (update_tpa) {
6955 rc = bnxt_set_tpa(bp,
6956 (flags & BNXT_FLAG_TPA) ?
6957 true : false);
6958 if (rc)
6959 bp->flags = old_flags;
6960 }
6961 }
6962 return rc;
6963}
6964
Michael Chan9f554592016-01-02 23:44:58 -05006965static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6966{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006967 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006968 int i = bnapi->index;
6969
Michael Chan3b2b7d92016-01-02 23:45:00 -05006970 if (!txr)
6971 return;
6972
Michael Chan9f554592016-01-02 23:44:58 -05006973 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6974 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6975 txr->tx_cons);
6976}
6977
6978static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6979{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006980 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006981 int i = bnapi->index;
6982
Michael Chan3b2b7d92016-01-02 23:45:00 -05006983 if (!rxr)
6984 return;
6985
Michael Chan9f554592016-01-02 23:44:58 -05006986 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",
6987 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6988 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6989 rxr->rx_sw_agg_prod);
6990}
6991
6992static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6993{
6994 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6995 int i = bnapi->index;
6996
6997 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6998 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6999}
7000
Michael Chanc0c050c2015-10-22 16:01:17 -04007001static void bnxt_dbg_dump_states(struct bnxt *bp)
7002{
7003 int i;
7004 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04007005
7006 for (i = 0; i < bp->cp_nr_rings; i++) {
7007 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007008 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05007009 bnxt_dump_tx_sw_state(bnapi);
7010 bnxt_dump_rx_sw_state(bnapi);
7011 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04007012 }
7013 }
7014}
7015
Michael Chan6988bd92016-06-13 02:25:29 -04007016static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04007017{
Michael Chan6988bd92016-06-13 02:25:29 -04007018 if (!silent)
7019 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05007020 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05007021 int rc;
7022
7023 if (!silent)
7024 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05007025 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05007026 rc = bnxt_open_nic(bp, false, false);
7027 if (!silent && !rc)
7028 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05007029 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007030}
7031
7032static void bnxt_tx_timeout(struct net_device *dev)
7033{
7034 struct bnxt *bp = netdev_priv(dev);
7035
7036 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
7037 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007038 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007039}
7040
7041#ifdef CONFIG_NET_POLL_CONTROLLER
7042static void bnxt_poll_controller(struct net_device *dev)
7043{
7044 struct bnxt *bp = netdev_priv(dev);
7045 int i;
7046
Michael Chan2270bc52017-06-23 14:01:01 -04007047 /* Only process tx rings/combined rings in netpoll mode. */
7048 for (i = 0; i < bp->tx_nr_rings; i++) {
7049 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007050
Michael Chan2270bc52017-06-23 14:01:01 -04007051 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04007052 }
7053}
7054#endif
7055
Kees Cooke99e88a2017-10-16 14:43:17 -07007056static void bnxt_timer(struct timer_list *t)
Michael Chanc0c050c2015-10-22 16:01:17 -04007057{
Kees Cooke99e88a2017-10-16 14:43:17 -07007058 struct bnxt *bp = from_timer(bp, t, timer);
Michael Chanc0c050c2015-10-22 16:01:17 -04007059 struct net_device *dev = bp->dev;
7060
7061 if (!netif_running(dev))
7062 return;
7063
7064 if (atomic_read(&bp->intr_sem) != 0)
7065 goto bnxt_restart_timer;
7066
Michael Chanadcc3312017-07-24 12:34:24 -04007067 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
7068 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05007069 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007070 bnxt_queue_sp_work(bp);
Michael Chan3bdf56c2016-03-07 15:38:45 -05007071 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04007072
7073 if (bnxt_tc_flower_enabled(bp)) {
7074 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
7075 bnxt_queue_sp_work(bp);
7076 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007077bnxt_restart_timer:
7078 mod_timer(&bp->timer, jiffies + bp->current_interval);
7079}
7080
Michael Chana551ee92017-01-25 02:55:07 -05007081static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04007082{
Michael Chana551ee92017-01-25 02:55:07 -05007083 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
7084 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04007085 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
7086 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
7087 */
7088 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7089 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05007090}
7091
7092static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
7093{
Michael Chan6988bd92016-06-13 02:25:29 -04007094 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7095 rtnl_unlock();
7096}
7097
Michael Chana551ee92017-01-25 02:55:07 -05007098/* Only called from bnxt_sp_task() */
7099static void bnxt_reset(struct bnxt *bp, bool silent)
7100{
7101 bnxt_rtnl_lock_sp(bp);
7102 if (test_bit(BNXT_STATE_OPEN, &bp->state))
7103 bnxt_reset_task(bp, silent);
7104 bnxt_rtnl_unlock_sp(bp);
7105}
7106
Michael Chanc0c050c2015-10-22 16:01:17 -04007107static void bnxt_cfg_ntp_filters(struct bnxt *);
7108
7109static void bnxt_sp_task(struct work_struct *work)
7110{
7111 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007112
Michael Chan4cebdce2015-12-09 19:35:43 -05007113 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7114 smp_mb__after_atomic();
7115 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7116 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007117 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05007118 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007119
7120 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
7121 bnxt_cfg_rx_mode(bp);
7122
7123 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
7124 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007125 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
7126 bnxt_hwrm_exec_fwd_req(bp);
7127 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7128 bnxt_hwrm_tunnel_dst_port_alloc(
7129 bp, bp->vxlan_port,
7130 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7131 }
7132 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7133 bnxt_hwrm_tunnel_dst_port_free(
7134 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7135 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007136 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7137 bnxt_hwrm_tunnel_dst_port_alloc(
7138 bp, bp->nge_port,
7139 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7140 }
7141 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7142 bnxt_hwrm_tunnel_dst_port_free(
7143 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7144 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05007145 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
7146 bnxt_hwrm_port_qstats(bp);
7147
Michael Chan0eaa24b2017-01-25 02:55:08 -05007148 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007149 int rc;
Michael Chan0eaa24b2017-01-25 02:55:08 -05007150
Michael Chane2dc9b62017-10-13 21:09:30 -04007151 mutex_lock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007152 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
7153 &bp->sp_event))
7154 bnxt_hwrm_phy_qcaps(bp);
7155
Michael Chane2dc9b62017-10-13 21:09:30 -04007156 rc = bnxt_update_link(bp, true);
7157 mutex_unlock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007158 if (rc)
7159 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
7160 rc);
7161 }
Michael Chan90c694b2017-01-25 02:55:09 -05007162 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007163 mutex_lock(&bp->link_lock);
7164 bnxt_get_port_module_status(bp);
7165 mutex_unlock(&bp->link_lock);
Michael Chan90c694b2017-01-25 02:55:09 -05007166 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04007167
7168 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
7169 bnxt_tc_flow_stats_work(bp);
7170
Michael Chane2dc9b62017-10-13 21:09:30 -04007171 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
7172 * must be the last functions to be called before exiting.
7173 */
Michael Chanc0c050c2015-10-22 16:01:17 -04007174 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
7175 bnxt_reset(bp, false);
7176
7177 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
7178 bnxt_reset(bp, true);
7179
Michael Chanc0c050c2015-10-22 16:01:17 -04007180 smp_mb__before_atomic();
7181 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7182}
7183
Michael Chand1e79252017-02-06 16:55:38 -05007184/* Under rtnl_lock */
Michael Chan98fdbe72017-08-28 13:40:26 -04007185int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7186 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05007187{
7188 int max_rx, max_tx, tx_sets = 1;
7189 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05007190 int rc;
7191
Michael Chand1e79252017-02-06 16:55:38 -05007192 if (tcs)
7193 tx_sets = tcs;
7194
7195 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7196 if (rc)
7197 return rc;
7198
7199 if (max_rx < rx)
7200 return -ENOMEM;
7201
Michael Chan5f449242017-02-06 16:55:40 -05007202 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05007203 if (max_tx < tx_rings_needed)
7204 return -ENOMEM;
7205
Michael Chan98fdbe72017-08-28 13:40:26 -04007206 return bnxt_hwrm_check_tx_rings(bp, tx_rings_needed);
Michael Chand1e79252017-02-06 16:55:38 -05007207}
7208
Sathya Perla17086392017-02-20 19:25:18 -05007209static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7210{
7211 if (bp->bar2) {
7212 pci_iounmap(pdev, bp->bar2);
7213 bp->bar2 = NULL;
7214 }
7215
7216 if (bp->bar1) {
7217 pci_iounmap(pdev, bp->bar1);
7218 bp->bar1 = NULL;
7219 }
7220
7221 if (bp->bar0) {
7222 pci_iounmap(pdev, bp->bar0);
7223 bp->bar0 = NULL;
7224 }
7225}
7226
7227static void bnxt_cleanup_pci(struct bnxt *bp)
7228{
7229 bnxt_unmap_bars(bp, bp->pdev);
7230 pci_release_regions(bp->pdev);
7231 pci_disable_device(bp->pdev);
7232}
7233
Michael Chan18775aa2017-10-26 11:51:27 -04007234static void bnxt_init_dflt_coal(struct bnxt *bp)
7235{
7236 struct bnxt_coal *coal;
7237
7238 /* Tick values in micro seconds.
7239 * 1 coal_buf x bufs_per_record = 1 completion record.
7240 */
7241 coal = &bp->rx_coal;
7242 coal->coal_ticks = 14;
7243 coal->coal_bufs = 30;
7244 coal->coal_ticks_irq = 1;
7245 coal->coal_bufs_irq = 2;
7246 coal->idle_thresh = 25;
7247 coal->bufs_per_record = 2;
7248 coal->budget = 64; /* NAPI budget */
7249
7250 coal = &bp->tx_coal;
7251 coal->coal_ticks = 28;
7252 coal->coal_bufs = 30;
7253 coal->coal_ticks_irq = 2;
7254 coal->coal_bufs_irq = 2;
7255 coal->bufs_per_record = 1;
7256
7257 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7258}
7259
Michael Chanc0c050c2015-10-22 16:01:17 -04007260static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7261{
7262 int rc;
7263 struct bnxt *bp = netdev_priv(dev);
7264
7265 SET_NETDEV_DEV(dev, &pdev->dev);
7266
7267 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7268 rc = pci_enable_device(pdev);
7269 if (rc) {
7270 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7271 goto init_err;
7272 }
7273
7274 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7275 dev_err(&pdev->dev,
7276 "Cannot find PCI device base address, aborting\n");
7277 rc = -ENODEV;
7278 goto init_err_disable;
7279 }
7280
7281 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7282 if (rc) {
7283 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7284 goto init_err_disable;
7285 }
7286
7287 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7288 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7289 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7290 goto init_err_disable;
7291 }
7292
7293 pci_set_master(pdev);
7294
7295 bp->dev = dev;
7296 bp->pdev = pdev;
7297
7298 bp->bar0 = pci_ioremap_bar(pdev, 0);
7299 if (!bp->bar0) {
7300 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7301 rc = -ENOMEM;
7302 goto init_err_release;
7303 }
7304
7305 bp->bar1 = pci_ioremap_bar(pdev, 2);
7306 if (!bp->bar1) {
7307 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7308 rc = -ENOMEM;
7309 goto init_err_release;
7310 }
7311
7312 bp->bar2 = pci_ioremap_bar(pdev, 4);
7313 if (!bp->bar2) {
7314 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7315 rc = -ENOMEM;
7316 goto init_err_release;
7317 }
7318
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007319 pci_enable_pcie_error_reporting(pdev);
7320
Michael Chanc0c050c2015-10-22 16:01:17 -04007321 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7322
7323 spin_lock_init(&bp->ntp_fltr_lock);
7324
7325 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7326 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7327
Michael Chan18775aa2017-10-26 11:51:27 -04007328 bnxt_init_dflt_coal(bp);
Michael Chan51f30782016-07-01 18:46:29 -04007329
Kees Cooke99e88a2017-10-16 14:43:17 -07007330 timer_setup(&bp->timer, bnxt_timer, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04007331 bp->current_interval = BNXT_TIMER_INTERVAL;
7332
Michael Chancaefe522015-12-09 19:35:42 -05007333 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007334 return 0;
7335
7336init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007337 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007338 pci_release_regions(pdev);
7339
7340init_err_disable:
7341 pci_disable_device(pdev);
7342
7343init_err:
7344 return rc;
7345}
7346
7347/* rtnl_lock held */
7348static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7349{
7350 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007351 struct bnxt *bp = netdev_priv(dev);
7352 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007353
7354 if (!is_valid_ether_addr(addr->sa_data))
7355 return -EADDRNOTAVAIL;
7356
Michael Chanc1a7bdf2017-10-26 11:51:24 -04007357 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7358 return 0;
7359
Michael Chan84c33dd2016-04-11 04:11:13 -04007360 rc = bnxt_approve_mac(bp, addr->sa_data);
7361 if (rc)
7362 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007363
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007364 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7365 if (netif_running(dev)) {
7366 bnxt_close_nic(bp, false, false);
7367 rc = bnxt_open_nic(bp, false, false);
7368 }
7369
7370 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007371}
7372
7373/* rtnl_lock held */
7374static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7375{
7376 struct bnxt *bp = netdev_priv(dev);
7377
Michael Chanc0c050c2015-10-22 16:01:17 -04007378 if (netif_running(dev))
7379 bnxt_close_nic(bp, false, false);
7380
7381 dev->mtu = new_mtu;
7382 bnxt_set_ring_params(bp);
7383
7384 if (netif_running(dev))
7385 return bnxt_open_nic(bp, false, false);
7386
7387 return 0;
7388}
7389
Michael Chanc5e3deb2016-12-02 21:17:15 -05007390int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007391{
7392 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007393 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007394 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007395
Michael Chanc0c050c2015-10-22 16:01:17 -04007396 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007397 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007398 tc, bp->max_tc);
7399 return -EINVAL;
7400 }
7401
7402 if (netdev_get_num_tc(dev) == tc)
7403 return 0;
7404
Michael Chan3ffb6a32016-11-11 00:11:42 -05007405 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7406 sh = true;
7407
Michael Chan98fdbe72017-08-28 13:40:26 -04007408 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7409 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007410 if (rc)
7411 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007412
7413 /* Needs to close the device and do hw resource re-allocations */
7414 if (netif_running(bp->dev))
7415 bnxt_close_nic(bp, true, false);
7416
7417 if (tc) {
7418 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7419 netdev_set_num_tc(dev, tc);
7420 } else {
7421 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7422 netdev_reset_tc(dev);
7423 }
Michael Chan87e9b372017-08-23 19:34:03 -04007424 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
Michael Chan3ffb6a32016-11-11 00:11:42 -05007425 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7426 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007427 bp->num_stat_ctxs = bp->cp_nr_rings;
7428
7429 if (netif_running(bp->dev))
7430 return bnxt_open_nic(bp, true, false);
7431
7432 return 0;
7433}
7434
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007435static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7436 void *cb_priv)
Sathya Perla2ae74082017-08-28 13:40:33 -04007437{
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007438 struct bnxt *bp = cb_priv;
Sathya Perla2ae74082017-08-28 13:40:33 -04007439
Jiri Pirko44ae12a2017-11-01 11:47:39 +01007440 if (!bnxt_tc_flower_enabled(bp) || !tc_can_offload(bp->dev))
Sathya Perla2ae74082017-08-28 13:40:33 -04007441 return -EOPNOTSUPP;
7442
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007443 switch (type) {
7444 case TC_SETUP_CLSFLOWER:
7445 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
7446 default:
7447 return -EOPNOTSUPP;
7448 }
7449}
7450
7451static int bnxt_setup_tc_block(struct net_device *dev,
7452 struct tc_block_offload *f)
7453{
7454 struct bnxt *bp = netdev_priv(dev);
7455
7456 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7457 return -EOPNOTSUPP;
7458
7459 switch (f->command) {
7460 case TC_BLOCK_BIND:
7461 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
7462 bp, bp);
7463 case TC_BLOCK_UNBIND:
7464 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
7465 return 0;
7466 default:
7467 return -EOPNOTSUPP;
7468 }
Sathya Perla2ae74082017-08-28 13:40:33 -04007469}
7470
Jiri Pirko2572ac52017-08-07 10:15:17 +02007471static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02007472 void *type_data)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007473{
Sathya Perla2ae74082017-08-28 13:40:33 -04007474 switch (type) {
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007475 case TC_SETUP_BLOCK:
7476 return bnxt_setup_tc_block(dev, type_data);
Nogah Frankel575ed7d2017-11-06 07:23:42 +01007477 case TC_SETUP_QDISC_MQPRIO: {
Sathya Perla2ae74082017-08-28 13:40:33 -04007478 struct tc_mqprio_qopt *mqprio = type_data;
Jiri Pirkode4784c2017-08-07 10:15:32 +02007479
Sathya Perla2ae74082017-08-28 13:40:33 -04007480 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7481
7482 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
7483 }
7484 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02007485 return -EOPNOTSUPP;
Sathya Perla2ae74082017-08-28 13:40:33 -04007486 }
Michael Chanc5e3deb2016-12-02 21:17:15 -05007487}
7488
Michael Chanc0c050c2015-10-22 16:01:17 -04007489#ifdef CONFIG_RFS_ACCEL
7490static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7491 struct bnxt_ntuple_filter *f2)
7492{
7493 struct flow_keys *keys1 = &f1->fkeys;
7494 struct flow_keys *keys2 = &f2->fkeys;
7495
7496 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7497 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7498 keys1->ports.ports == keys2->ports.ports &&
7499 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7500 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007501 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007502 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7503 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007504 return true;
7505
7506 return false;
7507}
7508
7509static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7510 u16 rxq_index, u32 flow_id)
7511{
7512 struct bnxt *bp = netdev_priv(dev);
7513 struct bnxt_ntuple_filter *fltr, *new_fltr;
7514 struct flow_keys *fkeys;
7515 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007516 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007517 struct hlist_head *head;
7518
Michael Chana54c4d72016-07-25 12:33:35 -04007519 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7520 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7521 int off = 0, j;
7522
7523 netif_addr_lock_bh(dev);
7524 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7525 if (ether_addr_equal(eth->h_dest,
7526 vnic->uc_list + off)) {
7527 l2_idx = j + 1;
7528 break;
7529 }
7530 }
7531 netif_addr_unlock_bh(dev);
7532 if (!l2_idx)
7533 return -EINVAL;
7534 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007535 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7536 if (!new_fltr)
7537 return -ENOMEM;
7538
7539 fkeys = &new_fltr->fkeys;
7540 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7541 rc = -EPROTONOSUPPORT;
7542 goto err_free;
7543 }
7544
Michael Chandda0e742016-12-29 12:13:40 -05007545 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7546 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007547 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7548 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7549 rc = -EPROTONOSUPPORT;
7550 goto err_free;
7551 }
Michael Chandda0e742016-12-29 12:13:40 -05007552 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7553 bp->hwrm_spec_code < 0x10601) {
7554 rc = -EPROTONOSUPPORT;
7555 goto err_free;
7556 }
Michael Chan61aad722017-02-12 19:18:14 -05007557 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7558 bp->hwrm_spec_code < 0x10601) {
7559 rc = -EPROTONOSUPPORT;
7560 goto err_free;
7561 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007562
Michael Chana54c4d72016-07-25 12:33:35 -04007563 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007564 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7565
7566 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7567 head = &bp->ntp_fltr_hash_tbl[idx];
7568 rcu_read_lock();
7569 hlist_for_each_entry_rcu(fltr, head, hash) {
7570 if (bnxt_fltr_match(fltr, new_fltr)) {
7571 rcu_read_unlock();
7572 rc = 0;
7573 goto err_free;
7574 }
7575 }
7576 rcu_read_unlock();
7577
7578 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007579 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7580 BNXT_NTP_FLTR_MAX_FLTR, 0);
7581 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007582 spin_unlock_bh(&bp->ntp_fltr_lock);
7583 rc = -ENOMEM;
7584 goto err_free;
7585 }
7586
Michael Chan84e86b92015-11-05 16:25:50 -05007587 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007588 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007589 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007590 new_fltr->rxq = rxq_index;
7591 hlist_add_head_rcu(&new_fltr->hash, head);
7592 bp->ntp_fltr_count++;
7593 spin_unlock_bh(&bp->ntp_fltr_lock);
7594
7595 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007596 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007597
7598 return new_fltr->sw_id;
7599
7600err_free:
7601 kfree(new_fltr);
7602 return rc;
7603}
7604
7605static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7606{
7607 int i;
7608
7609 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7610 struct hlist_head *head;
7611 struct hlist_node *tmp;
7612 struct bnxt_ntuple_filter *fltr;
7613 int rc;
7614
7615 head = &bp->ntp_fltr_hash_tbl[i];
7616 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7617 bool del = false;
7618
7619 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7620 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7621 fltr->flow_id,
7622 fltr->sw_id)) {
7623 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7624 fltr);
7625 del = true;
7626 }
7627 } else {
7628 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7629 fltr);
7630 if (rc)
7631 del = true;
7632 else
7633 set_bit(BNXT_FLTR_VALID, &fltr->state);
7634 }
7635
7636 if (del) {
7637 spin_lock_bh(&bp->ntp_fltr_lock);
7638 hlist_del_rcu(&fltr->hash);
7639 bp->ntp_fltr_count--;
7640 spin_unlock_bh(&bp->ntp_fltr_lock);
7641 synchronize_rcu();
7642 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7643 kfree(fltr);
7644 }
7645 }
7646 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007647 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7648 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007649}
7650
7651#else
7652
7653static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7654{
7655}
7656
7657#endif /* CONFIG_RFS_ACCEL */
7658
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007659static void bnxt_udp_tunnel_add(struct net_device *dev,
7660 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007661{
7662 struct bnxt *bp = netdev_priv(dev);
7663
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007664 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7665 return;
7666
Michael Chanc0c050c2015-10-22 16:01:17 -04007667 if (!netif_running(dev))
7668 return;
7669
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007670 switch (ti->type) {
7671 case UDP_TUNNEL_TYPE_VXLAN:
7672 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7673 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007674
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007675 bp->vxlan_port_cnt++;
7676 if (bp->vxlan_port_cnt == 1) {
7677 bp->vxlan_port = ti->port;
7678 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007679 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007680 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007681 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007682 case UDP_TUNNEL_TYPE_GENEVE:
7683 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7684 return;
7685
7686 bp->nge_port_cnt++;
7687 if (bp->nge_port_cnt == 1) {
7688 bp->nge_port = ti->port;
7689 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7690 }
7691 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007692 default:
7693 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007694 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007695
Michael Chanc213eae2017-10-13 21:09:29 -04007696 bnxt_queue_sp_work(bp);
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007697}
7698
7699static void bnxt_udp_tunnel_del(struct net_device *dev,
7700 struct udp_tunnel_info *ti)
7701{
7702 struct bnxt *bp = netdev_priv(dev);
7703
7704 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7705 return;
7706
7707 if (!netif_running(dev))
7708 return;
7709
7710 switch (ti->type) {
7711 case UDP_TUNNEL_TYPE_VXLAN:
7712 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7713 return;
7714 bp->vxlan_port_cnt--;
7715
7716 if (bp->vxlan_port_cnt != 0)
7717 return;
7718
7719 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7720 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007721 case UDP_TUNNEL_TYPE_GENEVE:
7722 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7723 return;
7724 bp->nge_port_cnt--;
7725
7726 if (bp->nge_port_cnt != 0)
7727 return;
7728
7729 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7730 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007731 default:
7732 return;
7733 }
7734
Michael Chanc213eae2017-10-13 21:09:29 -04007735 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007736}
7737
Michael Chan39d8ba22017-07-24 12:34:22 -04007738static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7739 struct net_device *dev, u32 filter_mask,
7740 int nlflags)
7741{
7742 struct bnxt *bp = netdev_priv(dev);
7743
7744 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7745 nlflags, filter_mask, NULL);
7746}
7747
7748static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7749 u16 flags)
7750{
7751 struct bnxt *bp = netdev_priv(dev);
7752 struct nlattr *attr, *br_spec;
7753 int rem, rc = 0;
7754
7755 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7756 return -EOPNOTSUPP;
7757
7758 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7759 if (!br_spec)
7760 return -EINVAL;
7761
7762 nla_for_each_nested(attr, br_spec, rem) {
7763 u16 mode;
7764
7765 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7766 continue;
7767
7768 if (nla_len(attr) < sizeof(mode))
7769 return -EINVAL;
7770
7771 mode = nla_get_u16(attr);
7772 if (mode == bp->br_mode)
7773 break;
7774
7775 rc = bnxt_hwrm_set_br_mode(bp, mode);
7776 if (!rc)
7777 bp->br_mode = mode;
7778 break;
7779 }
7780 return rc;
7781}
7782
Sathya Perlac124a622017-07-24 12:34:29 -04007783static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
7784 size_t len)
7785{
7786 struct bnxt *bp = netdev_priv(dev);
7787 int rc;
7788
7789 /* The PF and it's VF-reps only support the switchdev framework */
7790 if (!BNXT_PF(bp))
7791 return -EOPNOTSUPP;
7792
Sathya Perla53f70b82017-07-25 13:28:41 -04007793 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04007794
7795 if (rc >= len)
7796 return -EOPNOTSUPP;
7797 return 0;
7798}
7799
7800int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
7801{
7802 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
7803 return -EOPNOTSUPP;
7804
7805 /* The PF and it's VF-reps only support the switchdev framework */
7806 if (!BNXT_PF(bp))
7807 return -EOPNOTSUPP;
7808
7809 switch (attr->id) {
7810 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
7811 /* In SRIOV each PF-pool (PF + child VFs) serves as a
7812 * switching domain, the PF's perm mac-addr can be used
7813 * as the unique parent-id
7814 */
7815 attr->u.ppid.id_len = ETH_ALEN;
7816 ether_addr_copy(attr->u.ppid.id, bp->pf.mac_addr);
7817 break;
7818 default:
7819 return -EOPNOTSUPP;
7820 }
7821 return 0;
7822}
7823
7824static int bnxt_swdev_port_attr_get(struct net_device *dev,
7825 struct switchdev_attr *attr)
7826{
7827 return bnxt_port_attr_get(netdev_priv(dev), attr);
7828}
7829
7830static const struct switchdev_ops bnxt_switchdev_ops = {
7831 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
7832};
7833
Michael Chanc0c050c2015-10-22 16:01:17 -04007834static const struct net_device_ops bnxt_netdev_ops = {
7835 .ndo_open = bnxt_open,
7836 .ndo_start_xmit = bnxt_start_xmit,
7837 .ndo_stop = bnxt_close,
7838 .ndo_get_stats64 = bnxt_get_stats64,
7839 .ndo_set_rx_mode = bnxt_set_rx_mode,
7840 .ndo_do_ioctl = bnxt_ioctl,
7841 .ndo_validate_addr = eth_validate_addr,
7842 .ndo_set_mac_address = bnxt_change_mac_addr,
7843 .ndo_change_mtu = bnxt_change_mtu,
7844 .ndo_fix_features = bnxt_fix_features,
7845 .ndo_set_features = bnxt_set_features,
7846 .ndo_tx_timeout = bnxt_tx_timeout,
7847#ifdef CONFIG_BNXT_SRIOV
7848 .ndo_get_vf_config = bnxt_get_vf_config,
7849 .ndo_set_vf_mac = bnxt_set_vf_mac,
7850 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7851 .ndo_set_vf_rate = bnxt_set_vf_bw,
7852 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7853 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7854#endif
7855#ifdef CONFIG_NET_POLL_CONTROLLER
7856 .ndo_poll_controller = bnxt_poll_controller,
7857#endif
7858 .ndo_setup_tc = bnxt_setup_tc,
7859#ifdef CONFIG_RFS_ACCEL
7860 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7861#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007862 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7863 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07007864 .ndo_bpf = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007865 .ndo_bridge_getlink = bnxt_bridge_getlink,
7866 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04007867 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04007868};
7869
7870static void bnxt_remove_one(struct pci_dev *pdev)
7871{
7872 struct net_device *dev = pci_get_drvdata(pdev);
7873 struct bnxt *bp = netdev_priv(dev);
7874
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007875 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007876 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007877 bnxt_dl_unregister(bp);
7878 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007879
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007880 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007881 unregister_netdev(dev);
Sathya Perla2ae74082017-08-28 13:40:33 -04007882 bnxt_shutdown_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04007883 bnxt_cancel_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007884 bp->sp_event = 0;
7885
Michael Chan78095922016-12-07 00:26:16 -05007886 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007887 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007888 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007889 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007890 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007891 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007892 kfree(bp->edev);
7893 bp->edev = NULL;
Sathya Perla17086392017-02-20 19:25:18 -05007894 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007895 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007896}
7897
7898static int bnxt_probe_phy(struct bnxt *bp)
7899{
7900 int rc = 0;
7901 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007902
Michael Chan170ce012016-04-05 14:08:57 -04007903 rc = bnxt_hwrm_phy_qcaps(bp);
7904 if (rc) {
7905 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7906 rc);
7907 return rc;
7908 }
Michael Chane2dc9b62017-10-13 21:09:30 -04007909 mutex_init(&bp->link_lock);
Michael Chan170ce012016-04-05 14:08:57 -04007910
Michael Chanc0c050c2015-10-22 16:01:17 -04007911 rc = bnxt_update_link(bp, false);
7912 if (rc) {
7913 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7914 rc);
7915 return rc;
7916 }
7917
Michael Chan93ed8112016-06-13 02:25:37 -04007918 /* Older firmware does not have supported_auto_speeds, so assume
7919 * that all supported speeds can be autonegotiated.
7920 */
7921 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7922 link_info->support_auto_speeds = link_info->support_speeds;
7923
Michael Chanc0c050c2015-10-22 16:01:17 -04007924 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007925 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007926 link_info->autoneg = BNXT_AUTONEG_SPEED;
7927 if (bp->hwrm_spec_code >= 0x10201) {
7928 if (link_info->auto_pause_setting &
7929 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7930 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7931 } else {
7932 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7933 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007934 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007935 } else {
7936 link_info->req_link_speed = link_info->force_link_speed;
7937 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007938 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007939 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7940 link_info->req_flow_ctrl =
7941 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7942 else
7943 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007944 return rc;
7945}
7946
7947static int bnxt_get_max_irq(struct pci_dev *pdev)
7948{
7949 u16 ctrl;
7950
7951 if (!pdev->msix_cap)
7952 return 1;
7953
7954 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7955 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7956}
7957
Michael Chan6e6c5a52016-01-02 23:45:02 -05007958static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7959 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007960{
Michael Chan6a4f2942018-01-17 03:21:06 -05007961 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007962 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007963
Michael Chan6a4f2942018-01-17 03:21:06 -05007964 *max_tx = hw_resc->max_tx_rings;
7965 *max_rx = hw_resc->max_rx_rings;
7966 *max_cp = min_t(int, hw_resc->max_irqs, hw_resc->max_cp_rings);
7967 *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
7968 max_ring_grps = hw_resc->max_hw_ring_grps;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007969 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7970 *max_cp -= 1;
7971 *max_rx -= 2;
7972 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007973 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7974 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007975 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007976}
7977
7978int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7979{
7980 int rx, tx, cp;
7981
7982 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7983 if (!rx || !tx || !cp)
7984 return -ENOMEM;
7985
7986 *max_rx = rx;
7987 *max_tx = tx;
7988 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7989}
7990
Michael Chane4060d32016-12-07 00:26:19 -05007991static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7992 bool shared)
7993{
7994 int rc;
7995
7996 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007997 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7998 /* Not enough rings, try disabling agg rings. */
7999 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8000 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8001 if (rc)
8002 return rc;
8003 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
Michael Chan1054aee2017-12-16 03:09:42 -05008004 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8005 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008006 bnxt_set_ring_params(bp);
8007 }
Michael Chane4060d32016-12-07 00:26:19 -05008008
8009 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
8010 int max_cp, max_stat, max_irq;
8011
8012 /* Reserve minimum resources for RoCE */
8013 max_cp = bnxt_get_max_func_cp_rings(bp);
8014 max_stat = bnxt_get_max_func_stat_ctxs(bp);
8015 max_irq = bnxt_get_max_func_irqs(bp);
8016 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
8017 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
8018 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
8019 return 0;
8020
8021 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
8022 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
8023 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
8024 max_cp = min_t(int, max_cp, max_irq);
8025 max_cp = min_t(int, max_cp, max_stat);
8026 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
8027 if (rc)
8028 rc = 0;
8029 }
8030 return rc;
8031}
8032
Michael Chan58ea8012018-01-17 03:21:08 -05008033/* In initial default shared ring setting, each shared ring must have a
8034 * RX/TX ring pair.
8035 */
8036static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
8037{
8038 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
8039 bp->rx_nr_rings = bp->cp_nr_rings;
8040 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
8041 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8042}
8043
Michael Chan702c2212017-05-29 19:06:10 -04008044static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05008045{
8046 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008047
8048 if (sh)
8049 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8050 dflt_rings = netif_get_num_default_rss_queues();
Michael Chand5430d32017-08-28 13:40:31 -04008051 /* Reduce default rings to reduce memory usage on multi-port cards */
8052 if (bp->port_count > 1)
8053 dflt_rings = min_t(int, dflt_rings, 4);
Michael Chane4060d32016-12-07 00:26:19 -05008054 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05008055 if (rc)
8056 return rc;
8057 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
8058 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan58ea8012018-01-17 03:21:08 -05008059 if (sh)
8060 bnxt_trim_dflt_sh_rings(bp);
8061 else
8062 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
8063 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
Michael Chan391be5c2016-12-29 12:13:41 -05008064
8065 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
8066 if (rc)
8067 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
Michael Chan58ea8012018-01-17 03:21:08 -05008068 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8069 if (sh)
8070 bnxt_trim_dflt_sh_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05008071
Michael Chan6e6c5a52016-01-02 23:45:02 -05008072 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8073 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8074 bp->tx_nr_rings + bp->rx_nr_rings;
8075 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04008076 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8077 bp->rx_nr_rings++;
8078 bp->cp_nr_rings++;
8079 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05008080 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008081}
8082
Michael Chan80fcaf42018-01-17 03:21:05 -05008083int bnxt_restore_pf_fw_resources(struct bnxt *bp)
Michael Chan7b08f662016-12-07 00:26:18 -05008084{
Michael Chan80fcaf42018-01-17 03:21:05 -05008085 int rc;
8086
Michael Chan7b08f662016-12-07 00:26:18 -05008087 ASSERT_RTNL();
Michael Chan80fcaf42018-01-17 03:21:05 -05008088 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
8089 return 0;
8090
Michael Chan7b08f662016-12-07 00:26:18 -05008091 bnxt_hwrm_func_qcaps(bp);
Michael Chan80fcaf42018-01-17 03:21:05 -05008092 __bnxt_close_nic(bp, true, false);
8093 bnxt_clear_int_mode(bp);
8094 rc = bnxt_init_int_mode(bp);
8095 if (rc)
8096 dev_close(bp->dev);
8097 else
8098 rc = bnxt_open_nic(bp, true, false);
8099 return rc;
Michael Chan7b08f662016-12-07 00:26:18 -05008100}
8101
Michael Chana22a6ac2017-08-23 19:34:05 -04008102static int bnxt_init_mac_addr(struct bnxt *bp)
8103{
8104 int rc = 0;
8105
8106 if (BNXT_PF(bp)) {
8107 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
8108 } else {
8109#ifdef CONFIG_BNXT_SRIOV
8110 struct bnxt_vf_info *vf = &bp->vf;
8111
8112 if (is_valid_ether_addr(vf->mac_addr)) {
8113 /* overwrite netdev dev_adr with admin VF MAC */
8114 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
8115 } else {
8116 eth_hw_addr_random(bp->dev);
8117 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
8118 }
8119#endif
8120 }
8121 return rc;
8122}
8123
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008124static void bnxt_parse_log_pcie_link(struct bnxt *bp)
8125{
8126 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
8127 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
8128
Vasundhara Volam7ab07602017-10-13 21:09:31 -04008129 if (pcie_get_minimum_link(pci_physfn(bp->pdev), &speed, &width) ||
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008130 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
8131 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
8132 else
8133 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
8134 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
8135 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
8136 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
8137 "Unknown", width);
8138}
8139
Michael Chanc0c050c2015-10-22 16:01:17 -04008140static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8141{
8142 static int version_printed;
8143 struct net_device *dev;
8144 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008145 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04008146
Ray Jui4e003382017-02-20 19:25:16 -05008147 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04008148 return -ENODEV;
8149
Michael Chanc0c050c2015-10-22 16:01:17 -04008150 if (version_printed++ == 0)
8151 pr_info("%s", version);
8152
8153 max_irqs = bnxt_get_max_irq(pdev);
8154 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
8155 if (!dev)
8156 return -ENOMEM;
8157
8158 bp = netdev_priv(dev);
8159
8160 if (bnxt_vf_pciid(ent->driver_data))
8161 bp->flags |= BNXT_FLAG_VF;
8162
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008163 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04008164 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04008165
8166 rc = bnxt_init_board(pdev, dev);
8167 if (rc < 0)
8168 goto init_err_free;
8169
8170 dev->netdev_ops = &bnxt_netdev_ops;
8171 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
8172 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -07008173 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -04008174 pci_set_drvdata(pdev, dev);
8175
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008176 rc = bnxt_alloc_hwrm_resources(bp);
8177 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008178 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008179
8180 mutex_init(&bp->hwrm_cmd_lock);
8181 rc = bnxt_hwrm_ver_get(bp);
8182 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008183 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008184
Deepak Khungare605db82017-05-29 19:06:04 -04008185 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
8186 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
8187 if (rc)
8188 goto init_err_pci_clean;
8189 }
8190
Michael Chan3c2217a2017-03-08 18:44:32 -05008191 rc = bnxt_hwrm_func_reset(bp);
8192 if (rc)
8193 goto init_err_pci_clean;
8194
Rob Swindell5ac67d82016-09-19 03:58:03 -04008195 bnxt_hwrm_fw_set_time(bp);
8196
Michael Chanc0c050c2015-10-22 16:01:17 -04008197 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8198 NETIF_F_TSO | NETIF_F_TSO6 |
8199 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07008200 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07008201 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8202 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008203 NETIF_F_RXCSUM | NETIF_F_GRO;
8204
8205 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8206 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04008207
Michael Chanc0c050c2015-10-22 16:01:17 -04008208 dev->hw_enc_features =
8209 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8210 NETIF_F_TSO | NETIF_F_TSO6 |
8211 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07008212 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07008213 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07008214 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
8215 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04008216 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
8217 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
8218 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
Michael Chan1054aee2017-12-16 03:09:42 -05008219 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8220 dev->hw_features |= NETIF_F_GRO_HW;
Michael Chanc0c050c2015-10-22 16:01:17 -04008221 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Michael Chan1054aee2017-12-16 03:09:42 -05008222 if (dev->features & NETIF_F_GRO_HW)
8223 dev->features &= ~NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04008224 dev->priv_flags |= IFF_UNICAST_FLT;
8225
8226#ifdef CONFIG_BNXT_SRIOV
8227 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008228 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04008229#endif
Michael Chan309369c2016-06-13 02:25:34 -04008230 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04008231 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04008232 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04008233 else
8234 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04008235
Michael Chanc0c050c2015-10-22 16:01:17 -04008236 rc = bnxt_hwrm_func_drv_rgtr(bp);
8237 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008238 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008239
Michael Chana1653b12016-12-07 00:26:20 -05008240 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
8241 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008242 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05008243
Michael Chana588e452016-12-07 00:26:21 -05008244 bp->ulp_probe = bnxt_ulp_probe;
8245
Michael Chanc0c050c2015-10-22 16:01:17 -04008246 /* Get the MAX capabilities for this function */
8247 rc = bnxt_hwrm_func_qcaps(bp);
8248 if (rc) {
8249 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
8250 rc);
8251 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008252 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008253 }
Michael Chana22a6ac2017-08-23 19:34:05 -04008254 rc = bnxt_init_mac_addr(bp);
8255 if (rc) {
8256 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
8257 rc = -EADDRNOTAVAIL;
8258 goto init_err_pci_clean;
8259 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008260 rc = bnxt_hwrm_queue_qportcfg(bp);
8261 if (rc) {
8262 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
8263 rc);
8264 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008265 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008266 }
8267
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008268 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05008269 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04008270 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04008271 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008272
Michael Chan7eb9bb32017-10-26 11:51:25 -04008273 /* MTU range: 60 - FW defined max */
8274 dev->min_mtu = ETH_ZLEN;
8275 dev->max_mtu = bp->max_mtu;
8276
Michael Chand5430d32017-08-28 13:40:31 -04008277 rc = bnxt_probe_phy(bp);
8278 if (rc)
8279 goto init_err_pci_clean;
8280
Michael Chanc61fb992017-02-06 16:55:36 -05008281 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04008282 bnxt_set_tpa_flags(bp);
8283 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05008284 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04008285 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008286 if (rc) {
8287 netdev_err(bp->dev, "Not enough rings available.\n");
8288 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05008289 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008290 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008291
Michael Chan87da7f72016-11-16 21:13:09 -05008292 /* Default RSS hash cfg. */
8293 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
8294 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
8295 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
8296 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04008297 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05008298 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8299 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8300 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8301 }
8302
Michael Chan8fdefd62016-12-29 12:13:36 -05008303 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05008304 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008305 dev->hw_features |= NETIF_F_NTUPLE;
8306 if (bnxt_rfs_capable(bp)) {
8307 bp->flags |= BNXT_FLAG_RFS;
8308 dev->features |= NETIF_F_NTUPLE;
8309 }
8310 }
8311
Michael Chanc0c050c2015-10-22 16:01:17 -04008312 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8313 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8314
Michael Chan78095922016-12-07 00:26:16 -05008315 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008316 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008317 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008318
Michael Chanc1ef1462017-04-04 18:14:07 -04008319 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008320 if (bp->flags & BNXT_FLAG_WOL_CAP)
8321 device_set_wakeup_enable(&pdev->dev, bp->wol);
8322 else
8323 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04008324
Michael Chanc213eae2017-10-13 21:09:29 -04008325 if (BNXT_PF(bp)) {
8326 if (!bnxt_pf_wq) {
8327 bnxt_pf_wq =
8328 create_singlethread_workqueue("bnxt_pf_wq");
8329 if (!bnxt_pf_wq) {
8330 dev_err(&pdev->dev, "Unable to create workqueue.\n");
8331 goto init_err_pci_clean;
8332 }
8333 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008334 bnxt_init_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04008335 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008336
Michael Chan78095922016-12-07 00:26:16 -05008337 rc = register_netdev(dev);
8338 if (rc)
Sathya Perla2ae74082017-08-28 13:40:33 -04008339 goto init_err_cleanup_tc;
Michael Chan78095922016-12-07 00:26:16 -05008340
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008341 if (BNXT_PF(bp))
8342 bnxt_dl_register(bp);
8343
Michael Chanc0c050c2015-10-22 16:01:17 -04008344 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8345 board_info[ent->driver_data].name,
8346 (long)pci_resource_start(pdev, 0), dev->dev_addr);
8347
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008348 bnxt_parse_log_pcie_link(bp);
8349
Michael Chanc0c050c2015-10-22 16:01:17 -04008350 return 0;
8351
Sathya Perla2ae74082017-08-28 13:40:33 -04008352init_err_cleanup_tc:
8353 bnxt_shutdown_tc(bp);
Michael Chan78095922016-12-07 00:26:16 -05008354 bnxt_clear_int_mode(bp);
8355
Sathya Perla17086392017-02-20 19:25:18 -05008356init_err_pci_clean:
8357 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008358
8359init_err_free:
8360 free_netdev(dev);
8361 return rc;
8362}
8363
Michael Chand196ece2017-04-04 18:14:08 -04008364static void bnxt_shutdown(struct pci_dev *pdev)
8365{
8366 struct net_device *dev = pci_get_drvdata(pdev);
8367 struct bnxt *bp;
8368
8369 if (!dev)
8370 return;
8371
8372 rtnl_lock();
8373 bp = netdev_priv(dev);
8374 if (!bp)
8375 goto shutdown_exit;
8376
8377 if (netif_running(dev))
8378 dev_close(dev);
8379
Ray Juia7f3f932017-12-01 03:13:02 -05008380 bnxt_ulp_shutdown(bp);
8381
Michael Chand196ece2017-04-04 18:14:08 -04008382 if (system_state == SYSTEM_POWER_OFF) {
8383 bnxt_clear_int_mode(bp);
8384 pci_wake_from_d3(pdev, bp->wol);
8385 pci_set_power_state(pdev, PCI_D3hot);
8386 }
8387
8388shutdown_exit:
8389 rtnl_unlock();
8390}
8391
Michael Chanf65a2042017-04-04 18:14:11 -04008392#ifdef CONFIG_PM_SLEEP
8393static int bnxt_suspend(struct device *device)
8394{
8395 struct pci_dev *pdev = to_pci_dev(device);
8396 struct net_device *dev = pci_get_drvdata(pdev);
8397 struct bnxt *bp = netdev_priv(dev);
8398 int rc = 0;
8399
8400 rtnl_lock();
8401 if (netif_running(dev)) {
8402 netif_device_detach(dev);
8403 rc = bnxt_close(dev);
8404 }
8405 bnxt_hwrm_func_drv_unrgtr(bp);
8406 rtnl_unlock();
8407 return rc;
8408}
8409
8410static int bnxt_resume(struct device *device)
8411{
8412 struct pci_dev *pdev = to_pci_dev(device);
8413 struct net_device *dev = pci_get_drvdata(pdev);
8414 struct bnxt *bp = netdev_priv(dev);
8415 int rc = 0;
8416
8417 rtnl_lock();
8418 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8419 rc = -ENODEV;
8420 goto resume_exit;
8421 }
8422 rc = bnxt_hwrm_func_reset(bp);
8423 if (rc) {
8424 rc = -EBUSY;
8425 goto resume_exit;
8426 }
8427 bnxt_get_wol_settings(bp);
8428 if (netif_running(dev)) {
8429 rc = bnxt_open(dev);
8430 if (!rc)
8431 netif_device_attach(dev);
8432 }
8433
8434resume_exit:
8435 rtnl_unlock();
8436 return rc;
8437}
8438
8439static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8440#define BNXT_PM_OPS (&bnxt_pm_ops)
8441
8442#else
8443
8444#define BNXT_PM_OPS NULL
8445
8446#endif /* CONFIG_PM_SLEEP */
8447
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008448/**
8449 * bnxt_io_error_detected - called when PCI error is detected
8450 * @pdev: Pointer to PCI device
8451 * @state: The current pci connection state
8452 *
8453 * This function is called after a PCI bus error affecting
8454 * this device has been detected.
8455 */
8456static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8457 pci_channel_state_t state)
8458{
8459 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008460 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008461
8462 netdev_info(netdev, "PCI I/O error detected\n");
8463
8464 rtnl_lock();
8465 netif_device_detach(netdev);
8466
Michael Chana588e452016-12-07 00:26:21 -05008467 bnxt_ulp_stop(bp);
8468
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008469 if (state == pci_channel_io_perm_failure) {
8470 rtnl_unlock();
8471 return PCI_ERS_RESULT_DISCONNECT;
8472 }
8473
8474 if (netif_running(netdev))
8475 bnxt_close(netdev);
8476
8477 pci_disable_device(pdev);
8478 rtnl_unlock();
8479
8480 /* Request a slot slot reset. */
8481 return PCI_ERS_RESULT_NEED_RESET;
8482}
8483
8484/**
8485 * bnxt_io_slot_reset - called after the pci bus has been reset.
8486 * @pdev: Pointer to PCI device
8487 *
8488 * Restart the card from scratch, as if from a cold-boot.
8489 * At this point, the card has exprienced a hard reset,
8490 * followed by fixups by BIOS, and has its config space
8491 * set up identically to what it was at cold boot.
8492 */
8493static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8494{
8495 struct net_device *netdev = pci_get_drvdata(pdev);
8496 struct bnxt *bp = netdev_priv(netdev);
8497 int err = 0;
8498 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8499
8500 netdev_info(bp->dev, "PCI Slot Reset\n");
8501
8502 rtnl_lock();
8503
8504 if (pci_enable_device(pdev)) {
8505 dev_err(&pdev->dev,
8506 "Cannot re-enable PCI device after reset.\n");
8507 } else {
8508 pci_set_master(pdev);
8509
Michael Chanaa8ed022016-12-07 00:26:17 -05008510 err = bnxt_hwrm_func_reset(bp);
8511 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008512 err = bnxt_open(netdev);
8513
Michael Chana588e452016-12-07 00:26:21 -05008514 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008515 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008516 bnxt_ulp_start(bp);
8517 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008518 }
8519
8520 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8521 dev_close(netdev);
8522
8523 rtnl_unlock();
8524
8525 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8526 if (err) {
8527 dev_err(&pdev->dev,
8528 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8529 err); /* non-fatal, continue */
8530 }
8531
8532 return PCI_ERS_RESULT_RECOVERED;
8533}
8534
8535/**
8536 * bnxt_io_resume - called when traffic can start flowing again.
8537 * @pdev: Pointer to PCI device
8538 *
8539 * This callback is called when the error recovery driver tells
8540 * us that its OK to resume normal operation.
8541 */
8542static void bnxt_io_resume(struct pci_dev *pdev)
8543{
8544 struct net_device *netdev = pci_get_drvdata(pdev);
8545
8546 rtnl_lock();
8547
8548 netif_device_attach(netdev);
8549
8550 rtnl_unlock();
8551}
8552
8553static const struct pci_error_handlers bnxt_err_handler = {
8554 .error_detected = bnxt_io_error_detected,
8555 .slot_reset = bnxt_io_slot_reset,
8556 .resume = bnxt_io_resume
8557};
8558
Michael Chanc0c050c2015-10-22 16:01:17 -04008559static struct pci_driver bnxt_pci_driver = {
8560 .name = DRV_MODULE_NAME,
8561 .id_table = bnxt_pci_tbl,
8562 .probe = bnxt_init_one,
8563 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008564 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008565 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008566 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008567#if defined(CONFIG_BNXT_SRIOV)
8568 .sriov_configure = bnxt_sriov_configure,
8569#endif
8570};
8571
Michael Chanc213eae2017-10-13 21:09:29 -04008572static int __init bnxt_init(void)
8573{
8574 return pci_register_driver(&bnxt_pci_driver);
8575}
8576
8577static void __exit bnxt_exit(void)
8578{
8579 pci_unregister_driver(&bnxt_pci_driver);
8580 if (bnxt_pf_wq)
8581 destroy_workqueue(bnxt_pf_wq);
8582}
8583
8584module_init(bnxt_init);
8585module_exit(bnxt_exit);