blob: 168342ac35c552d8640e04bb35477746b4381c71 [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"
Andy Gospodarekcabfb092018-04-26 17:44:40 -040065#include "bnxt_debugfs.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040066
67#define BNXT_TX_TIMEOUT (5 * HZ)
68
69static const char version[] =
70 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
71
72MODULE_LICENSE("GPL");
73MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
74MODULE_VERSION(DRV_MODULE_VERSION);
75
76#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
77#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
78#define BNXT_RX_COPY_THRESH 256
79
Michael Chan4419dbe2016-02-10 17:33:49 -050080#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040081
82enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050083 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040084 BCM57302,
85 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040086 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040087 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040088 BCM57311,
89 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050090 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040091 BCM57404,
92 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040093 BCM57402_NPAR,
94 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040095 BCM57412,
96 BCM57414,
97 BCM57416,
98 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040099 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -0400100 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -0400101 BCM57417_SFP,
102 BCM57416_SFP,
103 BCM57404_NPAR,
104 BCM57406_NPAR,
105 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400106 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400107 BCM57414_NPAR,
108 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500109 BCM57452,
110 BCM57454,
Vasundhara Volam92abef32018-01-17 03:21:13 -0500111 BCM5745x_NPAR,
Ray Jui4a581392017-08-28 13:40:28 -0400112 BCM58802,
Ray Jui8ed693b2017-10-26 11:51:20 -0400113 BCM58804,
Ray Jui4a581392017-08-28 13:40:28 -0400114 BCM58808,
Michael Chanadbc8302016-09-19 03:58:01 -0400115 NETXTREME_E_VF,
116 NETXTREME_C_VF,
Rob Miller618784e2017-10-26 11:51:21 -0400117 NETXTREME_S_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400118};
119
120/* indexed by enum above */
121static const struct {
122 char *name;
123} board_info[] = {
Scott Branden27573a72017-08-28 13:40:29 -0400124 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
125 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
126 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
127 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
128 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
129 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
130 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
131 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
132 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
133 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
134 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
135 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
136 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
137 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
138 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
139 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
140 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
141 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
142 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
143 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
144 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
145 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
146 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
147 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
148 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
149 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
150 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
151 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Vasundhara Volam92abef32018-01-17 03:21:13 -0500152 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
Scott Branden27573a72017-08-28 13:40:29 -0400153 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
Ray Jui8ed693b2017-10-26 11:51:20 -0400154 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400155 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
156 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
157 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Rob Miller618784e2017-10-26 11:51:21 -0400158 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400159};
160
161static const struct pci_device_id bnxt_pci_tbl[] = {
Vasundhara Volam92abef32018-01-17 03:21:13 -0500162 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400164 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500166 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400167 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
168 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400169 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400170 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
172 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500173 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400174 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
175 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400176 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
177 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400178 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
179 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
180 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
181 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400182 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400183 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400184 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
185 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
186 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
187 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
188 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400189 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
190 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400191 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400192 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400193 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400194 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400195 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
Deepak Khungar32b40792017-02-12 19:18:18 -0500196 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
Ray Jui4a581392017-08-28 13:40:28 -0400197 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
Ray Jui8ed693b2017-10-26 11:51:20 -0400198 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400199#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400200 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400202 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
203 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
204 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
205 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
206 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
207 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Rob Miller618784e2017-10-26 11:51:21 -0400208 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400209#endif
210 { 0 }
211};
212
213MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
214
215static const u16 bnxt_vf_req_snif[] = {
216 HWRM_FUNC_CFG,
Vasundhara Volam91cdda42018-01-17 03:21:14 -0500217 HWRM_FUNC_VF_CFG,
Michael Chanc0c050c2015-10-22 16:01:17 -0400218 HWRM_PORT_PHY_QCFG,
219 HWRM_CFA_L2_FILTER_ALLOC,
220};
221
Michael Chan25be8622016-04-05 14:09:00 -0400222static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500223 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
224 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
225 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
226 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
227 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400228};
229
Michael Chanc213eae2017-10-13 21:09:29 -0400230static struct workqueue_struct *bnxt_pf_wq;
231
Michael Chanc0c050c2015-10-22 16:01:17 -0400232static bool bnxt_vf_pciid(enum board_idx idx)
233{
Rob Miller618784e2017-10-26 11:51:21 -0400234 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
235 idx == NETXTREME_S_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400236}
237
238#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
239#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
240#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
241
242#define BNXT_CP_DB_REARM(db, raw_cons) \
243 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
244
245#define BNXT_CP_DB(db, raw_cons) \
246 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
247
248#define BNXT_CP_DB_IRQ_DIS(db) \
249 writel(DB_CP_IRQ_DIS_FLAGS, db)
250
Michael Chan38413402017-02-06 16:55:43 -0500251const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400252 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
253 TX_BD_FLAGS_LHINT_512_TO_1023,
254 TX_BD_FLAGS_LHINT_1024_TO_2047,
255 TX_BD_FLAGS_LHINT_1024_TO_2047,
256 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
257 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
258 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
259 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
260 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
261 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
262 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
263 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
264 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
265 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
266 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
267 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
268 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
269 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
270 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
271};
272
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400273static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
274{
275 struct metadata_dst *md_dst = skb_metadata_dst(skb);
276
277 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
278 return 0;
279
280 return md_dst->u.port_info.port_id;
281}
282
Michael Chanc0c050c2015-10-22 16:01:17 -0400283static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
284{
285 struct bnxt *bp = netdev_priv(dev);
286 struct tx_bd *txbd;
287 struct tx_bd_ext *txbd1;
288 struct netdev_queue *txq;
289 int i;
290 dma_addr_t mapping;
291 unsigned int length, pad = 0;
292 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
293 u16 prod, last_frag;
294 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400295 struct bnxt_tx_ring_info *txr;
296 struct bnxt_sw_tx_bd *tx_buf;
297
298 i = skb_get_queue_mapping(skb);
299 if (unlikely(i >= bp->tx_nr_rings)) {
300 dev_kfree_skb_any(skb);
301 return NETDEV_TX_OK;
302 }
303
Michael Chanc0c050c2015-10-22 16:01:17 -0400304 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500305 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400306 prod = txr->tx_prod;
307
308 free_size = bnxt_tx_avail(bp, txr);
309 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
310 netif_tx_stop_queue(txq);
311 return NETDEV_TX_BUSY;
312 }
313
314 length = skb->len;
315 len = skb_headlen(skb);
316 last_frag = skb_shinfo(skb)->nr_frags;
317
318 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
319
320 txbd->tx_bd_opaque = prod;
321
322 tx_buf = &txr->tx_buf_ring[prod];
323 tx_buf->skb = skb;
324 tx_buf->nr_frags = last_frag;
325
326 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400327 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400328 if (skb_vlan_tag_present(skb)) {
329 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
330 skb_vlan_tag_get(skb);
331 /* Currently supports 8021Q, 8021AD vlan offloads
332 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
333 */
334 if (skb->vlan_proto == htons(ETH_P_8021Q))
335 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
336 }
337
338 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500339 struct tx_push_buffer *tx_push_buf = txr->tx_push;
340 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
341 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
342 void *pdata = tx_push_buf->data;
343 u64 *end;
344 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345
346 /* Set COAL_NOW to be ready quickly for the next push */
347 tx_push->tx_bd_len_flags_type =
348 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
349 TX_BD_TYPE_LONG_TX_BD |
350 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
351 TX_BD_FLAGS_COAL_NOW |
352 TX_BD_FLAGS_PACKET_END |
353 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
354
355 if (skb->ip_summed == CHECKSUM_PARTIAL)
356 tx_push1->tx_bd_hsize_lflags =
357 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
358 else
359 tx_push1->tx_bd_hsize_lflags = 0;
360
361 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400362 tx_push1->tx_bd_cfa_action =
363 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400364
Michael Chanfbb0fa82016-02-22 02:10:26 -0500365 end = pdata + length;
366 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500367 *end = 0;
368
Michael Chanc0c050c2015-10-22 16:01:17 -0400369 skb_copy_from_linear_data(skb, pdata, len);
370 pdata += len;
371 for (j = 0; j < last_frag; j++) {
372 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
373 void *fptr;
374
375 fptr = skb_frag_address_safe(frag);
376 if (!fptr)
377 goto normal_tx;
378
379 memcpy(pdata, fptr, skb_frag_size(frag));
380 pdata += skb_frag_size(frag);
381 }
382
Michael Chan4419dbe2016-02-10 17:33:49 -0500383 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
384 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400385 prod = NEXT_TX(prod);
386 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
387 memcpy(txbd, tx_push1, sizeof(*txbd));
388 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500389 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400390 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
391 txr->tx_prod = prod;
392
Michael Chanb9a84602016-06-06 02:37:14 -0400393 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400394 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400395 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400396
Michael Chan4419dbe2016-02-10 17:33:49 -0500397 push_len = (length + sizeof(*tx_push) + 7) / 8;
398 if (push_len > 16) {
399 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400400 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
401 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500402 } else {
403 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
404 push_len);
405 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400406
Michael Chanc0c050c2015-10-22 16:01:17 -0400407 goto tx_done;
408 }
409
410normal_tx:
411 if (length < BNXT_MIN_PKT_SIZE) {
412 pad = BNXT_MIN_PKT_SIZE - length;
413 if (skb_pad(skb, pad)) {
414 /* SKB already freed. */
415 tx_buf->skb = NULL;
416 return NETDEV_TX_OK;
417 }
418 length = BNXT_MIN_PKT_SIZE;
419 }
420
421 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
422
423 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
424 dev_kfree_skb_any(skb);
425 tx_buf->skb = NULL;
426 return NETDEV_TX_OK;
427 }
428
429 dma_unmap_addr_set(tx_buf, mapping, mapping);
430 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
431 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
432
433 txbd->tx_bd_haddr = cpu_to_le64(mapping);
434
435 prod = NEXT_TX(prod);
436 txbd1 = (struct tx_bd_ext *)
437 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
438
439 txbd1->tx_bd_hsize_lflags = 0;
440 if (skb_is_gso(skb)) {
441 u32 hdr_len;
442
443 if (skb->encapsulation)
444 hdr_len = skb_inner_network_offset(skb) +
445 skb_inner_network_header_len(skb) +
446 inner_tcp_hdrlen(skb);
447 else
448 hdr_len = skb_transport_offset(skb) +
449 tcp_hdrlen(skb);
450
451 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
452 TX_BD_FLAGS_T_IPID |
453 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
454 length = skb_shinfo(skb)->gso_size;
455 txbd1->tx_bd_mss = cpu_to_le32(length);
456 length += hdr_len;
457 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
458 txbd1->tx_bd_hsize_lflags =
459 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
460 txbd1->tx_bd_mss = 0;
461 }
462
463 length >>= 9;
464 flags |= bnxt_lhint_arr[length];
465 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
466
467 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400468 txbd1->tx_bd_cfa_action =
469 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400470 for (i = 0; i < last_frag; i++) {
471 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
472
473 prod = NEXT_TX(prod);
474 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
475
476 len = skb_frag_size(frag);
477 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
478 DMA_TO_DEVICE);
479
480 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
481 goto tx_dma_error;
482
483 tx_buf = &txr->tx_buf_ring[prod];
484 dma_unmap_addr_set(tx_buf, mapping, mapping);
485
486 txbd->tx_bd_haddr = cpu_to_le64(mapping);
487
488 flags = len << TX_BD_LEN_SHIFT;
489 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
490 }
491
492 flags &= ~TX_BD_LEN;
493 txbd->tx_bd_len_flags_type =
494 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
495 TX_BD_FLAGS_PACKET_END);
496
497 netdev_tx_sent_queue(txq, skb->len);
498
499 /* Sync BD data before updating doorbell */
500 wmb();
501
502 prod = NEXT_TX(prod);
503 txr->tx_prod = prod;
504
Michael Chanffe40642017-05-30 20:03:00 -0400505 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400506 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400507
508tx_done:
509
510 mmiowb();
511
512 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400513 if (skb->xmit_more && !tx_buf->is_push)
514 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
515
Michael Chanc0c050c2015-10-22 16:01:17 -0400516 netif_tx_stop_queue(txq);
517
518 /* netif_tx_stop_queue() must be done before checking
519 * tx index in bnxt_tx_avail() below, because in
520 * bnxt_tx_int(), we update tx index before checking for
521 * netif_tx_queue_stopped().
522 */
523 smp_mb();
524 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
525 netif_tx_wake_queue(txq);
526 }
527 return NETDEV_TX_OK;
528
529tx_dma_error:
530 last_frag = i;
531
532 /* start back at beginning and unmap skb */
533 prod = txr->tx_prod;
534 tx_buf = &txr->tx_buf_ring[prod];
535 tx_buf->skb = NULL;
536 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
537 skb_headlen(skb), PCI_DMA_TODEVICE);
538 prod = NEXT_TX(prod);
539
540 /* unmap remaining mapped pages */
541 for (i = 0; i < last_frag; i++) {
542 prod = NEXT_TX(prod);
543 tx_buf = &txr->tx_buf_ring[prod];
544 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
545 skb_frag_size(&skb_shinfo(skb)->frags[i]),
546 PCI_DMA_TODEVICE);
547 }
548
549 dev_kfree_skb_any(skb);
550 return NETDEV_TX_OK;
551}
552
553static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
554{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500555 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500556 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400557 u16 cons = txr->tx_cons;
558 struct pci_dev *pdev = bp->pdev;
559 int i;
560 unsigned int tx_bytes = 0;
561
562 for (i = 0; i < nr_pkts; i++) {
563 struct bnxt_sw_tx_bd *tx_buf;
564 struct sk_buff *skb;
565 int j, last;
566
567 tx_buf = &txr->tx_buf_ring[cons];
568 cons = NEXT_TX(cons);
569 skb = tx_buf->skb;
570 tx_buf->skb = NULL;
571
572 if (tx_buf->is_push) {
573 tx_buf->is_push = 0;
574 goto next_tx_int;
575 }
576
577 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
578 skb_headlen(skb), PCI_DMA_TODEVICE);
579 last = tx_buf->nr_frags;
580
581 for (j = 0; j < last; j++) {
582 cons = NEXT_TX(cons);
583 tx_buf = &txr->tx_buf_ring[cons];
584 dma_unmap_page(
585 &pdev->dev,
586 dma_unmap_addr(tx_buf, mapping),
587 skb_frag_size(&skb_shinfo(skb)->frags[j]),
588 PCI_DMA_TODEVICE);
589 }
590
591next_tx_int:
592 cons = NEXT_TX(cons);
593
594 tx_bytes += skb->len;
595 dev_kfree_skb_any(skb);
596 }
597
598 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
599 txr->tx_cons = cons;
600
601 /* Need to make the tx_cons update visible to bnxt_start_xmit()
602 * before checking for netif_tx_queue_stopped(). Without the
603 * memory barrier, there is a small possibility that bnxt_start_xmit()
604 * will miss it and cause the queue to be stopped forever.
605 */
606 smp_mb();
607
608 if (unlikely(netif_tx_queue_stopped(txq)) &&
609 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
610 __netif_tx_lock(txq, smp_processor_id());
611 if (netif_tx_queue_stopped(txq) &&
612 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
613 txr->dev_state != BNXT_DEV_STATE_CLOSING)
614 netif_tx_wake_queue(txq);
615 __netif_tx_unlock(txq);
616 }
617}
618
Michael Chanc61fb992017-02-06 16:55:36 -0500619static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
620 gfp_t gfp)
621{
622 struct device *dev = &bp->pdev->dev;
623 struct page *page;
624
625 page = alloc_page(gfp);
626 if (!page)
627 return NULL;
628
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700629 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
630 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500631 if (dma_mapping_error(dev, *mapping)) {
632 __free_page(page);
633 return NULL;
634 }
635 *mapping += bp->rx_dma_offset;
636 return page;
637}
638
Michael Chanc0c050c2015-10-22 16:01:17 -0400639static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
640 gfp_t gfp)
641{
642 u8 *data;
643 struct pci_dev *pdev = bp->pdev;
644
645 data = kmalloc(bp->rx_buf_size, gfp);
646 if (!data)
647 return NULL;
648
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700649 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
650 bp->rx_buf_use_size, bp->rx_dir,
651 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400652
653 if (dma_mapping_error(&pdev->dev, *mapping)) {
654 kfree(data);
655 data = NULL;
656 }
657 return data;
658}
659
Michael Chan38413402017-02-06 16:55:43 -0500660int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
661 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400662{
663 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
664 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400665 dma_addr_t mapping;
666
Michael Chanc61fb992017-02-06 16:55:36 -0500667 if (BNXT_RX_PAGE_MODE(bp)) {
668 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400669
Michael Chanc61fb992017-02-06 16:55:36 -0500670 if (!page)
671 return -ENOMEM;
672
673 rx_buf->data = page;
674 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
675 } else {
676 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
677
678 if (!data)
679 return -ENOMEM;
680
681 rx_buf->data = data;
682 rx_buf->data_ptr = data + bp->rx_offset;
683 }
Michael Chan11cd1192017-02-06 16:55:33 -0500684 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400685
686 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400687 return 0;
688}
689
Michael Chanc6d30e82017-02-06 16:55:42 -0500690void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400691{
692 u16 prod = rxr->rx_prod;
693 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
694 struct rx_bd *cons_bd, *prod_bd;
695
696 prod_rx_buf = &rxr->rx_buf_ring[prod];
697 cons_rx_buf = &rxr->rx_buf_ring[cons];
698
699 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500700 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400701
Michael Chan11cd1192017-02-06 16:55:33 -0500702 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400703
704 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
705 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
706
707 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
708}
709
710static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
711{
712 u16 next, max = rxr->rx_agg_bmap_size;
713
714 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
715 if (next >= max)
716 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
717 return next;
718}
719
720static inline int bnxt_alloc_rx_page(struct bnxt *bp,
721 struct bnxt_rx_ring_info *rxr,
722 u16 prod, gfp_t gfp)
723{
724 struct rx_bd *rxbd =
725 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
726 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
727 struct pci_dev *pdev = bp->pdev;
728 struct page *page;
729 dma_addr_t mapping;
730 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400731 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400732
Michael Chan89d0a062016-04-25 02:30:51 -0400733 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
734 page = rxr->rx_page;
735 if (!page) {
736 page = alloc_page(gfp);
737 if (!page)
738 return -ENOMEM;
739 rxr->rx_page = page;
740 rxr->rx_page_offset = 0;
741 }
742 offset = rxr->rx_page_offset;
743 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
744 if (rxr->rx_page_offset == PAGE_SIZE)
745 rxr->rx_page = NULL;
746 else
747 get_page(page);
748 } else {
749 page = alloc_page(gfp);
750 if (!page)
751 return -ENOMEM;
752 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400753
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700754 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
755 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
756 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400757 if (dma_mapping_error(&pdev->dev, mapping)) {
758 __free_page(page);
759 return -EIO;
760 }
761
762 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
763 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
764
765 __set_bit(sw_prod, rxr->rx_agg_bmap);
766 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
767 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
768
769 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400770 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400771 rx_agg_buf->mapping = mapping;
772 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
773 rxbd->rx_bd_opaque = sw_prod;
774 return 0;
775}
776
777static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
778 u32 agg_bufs)
779{
780 struct bnxt *bp = bnapi->bp;
781 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500782 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400783 u16 prod = rxr->rx_agg_prod;
784 u16 sw_prod = rxr->rx_sw_agg_prod;
785 u32 i;
786
787 for (i = 0; i < agg_bufs; i++) {
788 u16 cons;
789 struct rx_agg_cmp *agg;
790 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
791 struct rx_bd *prod_bd;
792 struct page *page;
793
794 agg = (struct rx_agg_cmp *)
795 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
796 cons = agg->rx_agg_cmp_opaque;
797 __clear_bit(cons, rxr->rx_agg_bmap);
798
799 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
800 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
801
802 __set_bit(sw_prod, rxr->rx_agg_bmap);
803 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
804 cons_rx_buf = &rxr->rx_agg_ring[cons];
805
806 /* It is possible for sw_prod to be equal to cons, so
807 * set cons_rx_buf->page to NULL first.
808 */
809 page = cons_rx_buf->page;
810 cons_rx_buf->page = NULL;
811 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400812 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400813
814 prod_rx_buf->mapping = cons_rx_buf->mapping;
815
816 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
817
818 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
819 prod_bd->rx_bd_opaque = sw_prod;
820
821 prod = NEXT_RX_AGG(prod);
822 sw_prod = NEXT_RX_AGG(sw_prod);
823 cp_cons = NEXT_CMP(cp_cons);
824 }
825 rxr->rx_agg_prod = prod;
826 rxr->rx_sw_agg_prod = sw_prod;
827}
828
Michael Chanc61fb992017-02-06 16:55:36 -0500829static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
830 struct bnxt_rx_ring_info *rxr,
831 u16 cons, void *data, u8 *data_ptr,
832 dma_addr_t dma_addr,
833 unsigned int offset_and_len)
834{
835 unsigned int payload = offset_and_len >> 16;
836 unsigned int len = offset_and_len & 0xffff;
837 struct skb_frag_struct *frag;
838 struct page *page = data;
839 u16 prod = rxr->rx_prod;
840 struct sk_buff *skb;
841 int off, err;
842
843 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
844 if (unlikely(err)) {
845 bnxt_reuse_rx_data(rxr, cons, data);
846 return NULL;
847 }
848 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700849 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
850 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500851
852 if (unlikely(!payload))
853 payload = eth_get_headlen(data_ptr, len);
854
855 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
856 if (!skb) {
857 __free_page(page);
858 return NULL;
859 }
860
861 off = (void *)data_ptr - page_address(page);
862 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
863 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
864 payload + NET_IP_ALIGN);
865
866 frag = &skb_shinfo(skb)->frags[0];
867 skb_frag_size_sub(frag, payload);
868 frag->page_offset += payload;
869 skb->data_len -= payload;
870 skb->tail += payload;
871
872 return skb;
873}
874
Michael Chanc0c050c2015-10-22 16:01:17 -0400875static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
876 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500877 void *data, u8 *data_ptr,
878 dma_addr_t dma_addr,
879 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400880{
Michael Chan6bb19472017-02-06 16:55:32 -0500881 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400882 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500883 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400884
885 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
886 if (unlikely(err)) {
887 bnxt_reuse_rx_data(rxr, cons, data);
888 return NULL;
889 }
890
891 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700892 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
893 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400894 if (!skb) {
895 kfree(data);
896 return NULL;
897 }
898
Michael Chanb3dba772017-02-06 16:55:35 -0500899 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500900 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400901 return skb;
902}
903
904static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
905 struct sk_buff *skb, u16 cp_cons,
906 u32 agg_bufs)
907{
908 struct pci_dev *pdev = bp->pdev;
909 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500910 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400911 u16 prod = rxr->rx_agg_prod;
912 u32 i;
913
914 for (i = 0; i < agg_bufs; i++) {
915 u16 cons, frag_len;
916 struct rx_agg_cmp *agg;
917 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
918 struct page *page;
919 dma_addr_t mapping;
920
921 agg = (struct rx_agg_cmp *)
922 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
923 cons = agg->rx_agg_cmp_opaque;
924 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
925 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
926
927 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400928 skb_fill_page_desc(skb, i, cons_rx_buf->page,
929 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400930 __clear_bit(cons, rxr->rx_agg_bmap);
931
932 /* It is possible for bnxt_alloc_rx_page() to allocate
933 * a sw_prod index that equals the cons index, so we
934 * need to clear the cons entry now.
935 */
Michael Chan11cd1192017-02-06 16:55:33 -0500936 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400937 page = cons_rx_buf->page;
938 cons_rx_buf->page = NULL;
939
940 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
941 struct skb_shared_info *shinfo;
942 unsigned int nr_frags;
943
944 shinfo = skb_shinfo(skb);
945 nr_frags = --shinfo->nr_frags;
946 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
947
948 dev_kfree_skb(skb);
949
950 cons_rx_buf->page = page;
951
952 /* Update prod since possibly some pages have been
953 * allocated already.
954 */
955 rxr->rx_agg_prod = prod;
956 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
957 return NULL;
958 }
959
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700960 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
961 PCI_DMA_FROMDEVICE,
962 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400963
964 skb->data_len += frag_len;
965 skb->len += frag_len;
966 skb->truesize += PAGE_SIZE;
967
968 prod = NEXT_RX_AGG(prod);
969 cp_cons = NEXT_CMP(cp_cons);
970 }
971 rxr->rx_agg_prod = prod;
972 return skb;
973}
974
975static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
976 u8 agg_bufs, u32 *raw_cons)
977{
978 u16 last;
979 struct rx_agg_cmp *agg;
980
981 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
982 last = RING_CMP(*raw_cons);
983 agg = (struct rx_agg_cmp *)
984 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
985 return RX_AGG_CMP_VALID(agg, *raw_cons);
986}
987
988static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
989 unsigned int len,
990 dma_addr_t mapping)
991{
992 struct bnxt *bp = bnapi->bp;
993 struct pci_dev *pdev = bp->pdev;
994 struct sk_buff *skb;
995
996 skb = napi_alloc_skb(&bnapi->napi, len);
997 if (!skb)
998 return NULL;
999
Michael Chan745fc052017-02-06 16:55:34 -05001000 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1001 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001002
Michael Chan6bb19472017-02-06 16:55:32 -05001003 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1004 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -04001005
Michael Chan745fc052017-02-06 16:55:34 -05001006 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1007 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001008
1009 skb_put(skb, len);
1010 return skb;
1011}
1012
Michael Chanfa7e2812016-05-10 19:18:00 -04001013static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1014 u32 *raw_cons, void *cmp)
1015{
1016 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1017 struct rx_cmp *rxcmp = cmp;
1018 u32 tmp_raw_cons = *raw_cons;
1019 u8 cmp_type, agg_bufs = 0;
1020
1021 cmp_type = RX_CMP_TYPE(rxcmp);
1022
1023 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1024 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1025 RX_CMP_AGG_BUFS) >>
1026 RX_CMP_AGG_BUFS_SHIFT;
1027 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1028 struct rx_tpa_end_cmp *tpa_end = cmp;
1029
1030 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1031 RX_TPA_END_CMP_AGG_BUFS) >>
1032 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1033 }
1034
1035 if (agg_bufs) {
1036 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1037 return -EBUSY;
1038 }
1039 *raw_cons = tmp_raw_cons;
1040 return 0;
1041}
1042
Michael Chanc213eae2017-10-13 21:09:29 -04001043static void bnxt_queue_sp_work(struct bnxt *bp)
1044{
1045 if (BNXT_PF(bp))
1046 queue_work(bnxt_pf_wq, &bp->sp_task);
1047 else
1048 schedule_work(&bp->sp_task);
1049}
1050
1051static void bnxt_cancel_sp_work(struct bnxt *bp)
1052{
1053 if (BNXT_PF(bp))
1054 flush_workqueue(bnxt_pf_wq);
1055 else
1056 cancel_work_sync(&bp->sp_task);
1057}
1058
Michael Chanfa7e2812016-05-10 19:18:00 -04001059static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1060{
1061 if (!rxr->bnapi->in_reset) {
1062 rxr->bnapi->in_reset = true;
1063 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001064 bnxt_queue_sp_work(bp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001065 }
1066 rxr->rx_next_cons = 0xffff;
1067}
1068
Michael Chanc0c050c2015-10-22 16:01:17 -04001069static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1070 struct rx_tpa_start_cmp *tpa_start,
1071 struct rx_tpa_start_cmp_ext *tpa_start1)
1072{
1073 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1074 u16 cons, prod;
1075 struct bnxt_tpa_info *tpa_info;
1076 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1077 struct rx_bd *prod_bd;
1078 dma_addr_t mapping;
1079
1080 cons = tpa_start->rx_tpa_start_cmp_opaque;
1081 prod = rxr->rx_prod;
1082 cons_rx_buf = &rxr->rx_buf_ring[cons];
1083 prod_rx_buf = &rxr->rx_buf_ring[prod];
1084 tpa_info = &rxr->rx_tpa[agg_id];
1085
Michael Chanfa7e2812016-05-10 19:18:00 -04001086 if (unlikely(cons != rxr->rx_next_cons)) {
1087 bnxt_sched_reset(bp, rxr);
1088 return;
1089 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001090 /* Store cfa_code in tpa_info to use in tpa_end
1091 * completion processing.
1092 */
1093 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001094 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001095 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001096
1097 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001098 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001099
1100 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1101
1102 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1103
1104 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001105 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001106 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001107 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001108
1109 tpa_info->len =
1110 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1111 RX_TPA_START_CMP_LEN_SHIFT;
1112 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1113 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1114
1115 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1116 tpa_info->gso_type = SKB_GSO_TCPV4;
1117 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1118 if (hash_type == 3)
1119 tpa_info->gso_type = SKB_GSO_TCPV6;
1120 tpa_info->rss_hash =
1121 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1122 } else {
1123 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1124 tpa_info->gso_type = 0;
1125 if (netif_msg_rx_err(bp))
1126 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1127 }
1128 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1129 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001130 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001131
1132 rxr->rx_prod = NEXT_RX(prod);
1133 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001134 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001135 cons_rx_buf = &rxr->rx_buf_ring[cons];
1136
1137 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1138 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1139 cons_rx_buf->data = NULL;
1140}
1141
1142static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1143 u16 cp_cons, u32 agg_bufs)
1144{
1145 if (agg_bufs)
1146 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1147}
1148
Michael Chan94758f82016-06-13 02:25:35 -04001149static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1150 int payload_off, int tcp_ts,
1151 struct sk_buff *skb)
1152{
1153#ifdef CONFIG_INET
1154 struct tcphdr *th;
1155 int len, nw_off;
1156 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1157 u32 hdr_info = tpa_info->hdr_info;
1158 bool loopback = false;
1159
1160 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1161 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1162 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1163
1164 /* If the packet is an internal loopback packet, the offsets will
1165 * have an extra 4 bytes.
1166 */
1167 if (inner_mac_off == 4) {
1168 loopback = true;
1169 } else if (inner_mac_off > 4) {
1170 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1171 ETH_HLEN - 2));
1172
1173 /* We only support inner iPv4/ipv6. If we don't see the
1174 * correct protocol ID, it must be a loopback packet where
1175 * the offsets are off by 4.
1176 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001177 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001178 loopback = true;
1179 }
1180 if (loopback) {
1181 /* internal loopback packet, subtract all offsets by 4 */
1182 inner_ip_off -= 4;
1183 inner_mac_off -= 4;
1184 outer_ip_off -= 4;
1185 }
1186
1187 nw_off = inner_ip_off - ETH_HLEN;
1188 skb_set_network_header(skb, nw_off);
1189 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1190 struct ipv6hdr *iph = ipv6_hdr(skb);
1191
1192 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1193 len = skb->len - skb_transport_offset(skb);
1194 th = tcp_hdr(skb);
1195 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1196 } else {
1197 struct iphdr *iph = ip_hdr(skb);
1198
1199 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1200 len = skb->len - skb_transport_offset(skb);
1201 th = tcp_hdr(skb);
1202 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1203 }
1204
1205 if (inner_mac_off) { /* tunnel */
1206 struct udphdr *uh = NULL;
1207 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1208 ETH_HLEN - 2));
1209
1210 if (proto == htons(ETH_P_IP)) {
1211 struct iphdr *iph = (struct iphdr *)skb->data;
1212
1213 if (iph->protocol == IPPROTO_UDP)
1214 uh = (struct udphdr *)(iph + 1);
1215 } else {
1216 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1217
1218 if (iph->nexthdr == IPPROTO_UDP)
1219 uh = (struct udphdr *)(iph + 1);
1220 }
1221 if (uh) {
1222 if (uh->check)
1223 skb_shinfo(skb)->gso_type |=
1224 SKB_GSO_UDP_TUNNEL_CSUM;
1225 else
1226 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1227 }
1228 }
1229#endif
1230 return skb;
1231}
1232
Michael Chanc0c050c2015-10-22 16:01:17 -04001233#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1234#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1235
Michael Chan309369c2016-06-13 02:25:34 -04001236static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1237 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001238 struct sk_buff *skb)
1239{
Michael Chand1611c32015-10-25 22:27:57 -04001240#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001241 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001242 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001243
Michael Chan309369c2016-06-13 02:25:34 -04001244 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001245 tcp_opt_len = 12;
1246
Michael Chanc0c050c2015-10-22 16:01:17 -04001247 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1248 struct iphdr *iph;
1249
1250 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1251 ETH_HLEN;
1252 skb_set_network_header(skb, nw_off);
1253 iph = ip_hdr(skb);
1254 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1255 len = skb->len - skb_transport_offset(skb);
1256 th = tcp_hdr(skb);
1257 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1258 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1259 struct ipv6hdr *iph;
1260
1261 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1262 ETH_HLEN;
1263 skb_set_network_header(skb, nw_off);
1264 iph = ipv6_hdr(skb);
1265 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1266 len = skb->len - skb_transport_offset(skb);
1267 th = tcp_hdr(skb);
1268 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1269 } else {
1270 dev_kfree_skb_any(skb);
1271 return NULL;
1272 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001273
1274 if (nw_off) { /* tunnel */
1275 struct udphdr *uh = NULL;
1276
1277 if (skb->protocol == htons(ETH_P_IP)) {
1278 struct iphdr *iph = (struct iphdr *)skb->data;
1279
1280 if (iph->protocol == IPPROTO_UDP)
1281 uh = (struct udphdr *)(iph + 1);
1282 } else {
1283 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1284
1285 if (iph->nexthdr == IPPROTO_UDP)
1286 uh = (struct udphdr *)(iph + 1);
1287 }
1288 if (uh) {
1289 if (uh->check)
1290 skb_shinfo(skb)->gso_type |=
1291 SKB_GSO_UDP_TUNNEL_CSUM;
1292 else
1293 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1294 }
1295 }
1296#endif
1297 return skb;
1298}
1299
Michael Chan309369c2016-06-13 02:25:34 -04001300static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1301 struct bnxt_tpa_info *tpa_info,
1302 struct rx_tpa_end_cmp *tpa_end,
1303 struct rx_tpa_end_cmp_ext *tpa_end1,
1304 struct sk_buff *skb)
1305{
1306#ifdef CONFIG_INET
1307 int payload_off;
1308 u16 segs;
1309
1310 segs = TPA_END_TPA_SEGS(tpa_end);
1311 if (segs == 1)
1312 return skb;
1313
1314 NAPI_GRO_CB(skb)->count = segs;
1315 skb_shinfo(skb)->gso_size =
1316 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1317 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1318 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1319 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1320 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1321 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001322 if (likely(skb))
1323 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001324#endif
1325 return skb;
1326}
1327
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001328/* Given the cfa_code of a received packet determine which
1329 * netdev (vf-rep or PF) the packet is destined to.
1330 */
1331static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1332{
1333 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1334
1335 /* if vf-rep dev is NULL, the must belongs to the PF */
1336 return dev ? dev : bp->dev;
1337}
1338
Michael Chanc0c050c2015-10-22 16:01:17 -04001339static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1340 struct bnxt_napi *bnapi,
1341 u32 *raw_cons,
1342 struct rx_tpa_end_cmp *tpa_end,
1343 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001344 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001345{
1346 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001347 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001348 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001349 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001350 u16 cp_cons = RING_CMP(*raw_cons);
1351 unsigned int len;
1352 struct bnxt_tpa_info *tpa_info;
1353 dma_addr_t mapping;
1354 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001355 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001356
Michael Chanfa7e2812016-05-10 19:18:00 -04001357 if (unlikely(bnapi->in_reset)) {
1358 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1359
1360 if (rc < 0)
1361 return ERR_PTR(-EBUSY);
1362 return NULL;
1363 }
1364
Michael Chanc0c050c2015-10-22 16:01:17 -04001365 tpa_info = &rxr->rx_tpa[agg_id];
1366 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001367 data_ptr = tpa_info->data_ptr;
1368 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001369 len = tpa_info->len;
1370 mapping = tpa_info->mapping;
1371
1372 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1373 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1374
1375 if (agg_bufs) {
1376 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1377 return ERR_PTR(-EBUSY);
1378
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001379 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001380 cp_cons = NEXT_CMP(cp_cons);
1381 }
1382
Michael Chan69c149e2017-06-23 14:01:00 -04001383 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001384 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001385 if (agg_bufs > MAX_SKB_FRAGS)
1386 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1387 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001388 return NULL;
1389 }
1390
1391 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001392 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001393 if (!skb) {
1394 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1395 return NULL;
1396 }
1397 } else {
1398 u8 *new_data;
1399 dma_addr_t new_mapping;
1400
1401 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1402 if (!new_data) {
1403 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1404 return NULL;
1405 }
1406
1407 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001408 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001409 tpa_info->mapping = new_mapping;
1410
1411 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001412 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1413 bp->rx_buf_use_size, bp->rx_dir,
1414 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001415
1416 if (!skb) {
1417 kfree(data);
1418 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1419 return NULL;
1420 }
Michael Chanb3dba772017-02-06 16:55:35 -05001421 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001422 skb_put(skb, len);
1423 }
1424
1425 if (agg_bufs) {
1426 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1427 if (!skb) {
1428 /* Page reuse already handled by bnxt_rx_pages(). */
1429 return NULL;
1430 }
1431 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001432
1433 skb->protocol =
1434 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001435
1436 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1437 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1438
Michael Chan8852ddb2016-06-06 02:37:16 -04001439 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1440 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001441 u16 vlan_proto = tpa_info->metadata >>
1442 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chaned7bc6022018-03-09 23:46:06 -05001443 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001444
Michael Chan8852ddb2016-06-06 02:37:16 -04001445 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001446 }
1447
1448 skb_checksum_none_assert(skb);
1449 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1451 skb->csum_level =
1452 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1453 }
1454
1455 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001456 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001457
1458 return skb;
1459}
1460
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001461static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1462 struct sk_buff *skb)
1463{
1464 if (skb->dev != bp->dev) {
1465 /* this packet belongs to a vf-rep */
1466 bnxt_vf_rep_rx(bp, skb);
1467 return;
1468 }
1469 skb_record_rx_queue(skb, bnapi->index);
1470 napi_gro_receive(&bnapi->napi, skb);
1471}
1472
Michael Chanc0c050c2015-10-22 16:01:17 -04001473/* returns the following:
1474 * 1 - 1 packet successfully received
1475 * 0 - successful TPA_START, packet not completed yet
1476 * -EBUSY - completion ring does not have all the agg buffers yet
1477 * -ENOMEM - packet aborted due to out of memory
1478 * -EIO - packet aborted due to hw error indicated in BD
1479 */
1480static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001481 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001482{
1483 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001484 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001485 struct net_device *dev = bp->dev;
1486 struct rx_cmp *rxcmp;
1487 struct rx_cmp_ext *rxcmp1;
1488 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001489 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001490 struct bnxt_sw_rx_bd *rx_buf;
1491 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001492 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001493 dma_addr_t dma_addr;
1494 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001495 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001496 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001497 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001498
1499 rxcmp = (struct rx_cmp *)
1500 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1501
1502 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1503 cp_cons = RING_CMP(tmp_raw_cons);
1504 rxcmp1 = (struct rx_cmp_ext *)
1505 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1506
1507 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1508 return -EBUSY;
1509
1510 cmp_type = RX_CMP_TYPE(rxcmp);
1511
1512 prod = rxr->rx_prod;
1513
1514 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1515 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1516 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1517
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001518 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001519 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001520
1521 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1522 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1523 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001524 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001525
Tobias Klauser1fac4b22017-09-26 15:12:26 +02001526 if (IS_ERR(skb))
Michael Chanc0c050c2015-10-22 16:01:17 -04001527 return -EBUSY;
1528
1529 rc = -ENOMEM;
1530 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001531 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001532 rc = 1;
1533 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001534 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001535 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001536 }
1537
1538 cons = rxcmp->rx_cmp_opaque;
1539 rx_buf = &rxr->rx_buf_ring[cons];
1540 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001541 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001542 if (unlikely(cons != rxr->rx_next_cons)) {
1543 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1544
1545 bnxt_sched_reset(bp, rxr);
1546 return rc1;
1547 }
Michael Chan6bb19472017-02-06 16:55:32 -05001548 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001549
Michael Chanc61fb992017-02-06 16:55:36 -05001550 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1551 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001552
1553 if (agg_bufs) {
1554 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1555 return -EBUSY;
1556
1557 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001558 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001559 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001560 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001561
1562 rx_buf->data = NULL;
1563 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1564 bnxt_reuse_rx_data(rxr, cons, data);
1565 if (agg_bufs)
1566 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1567
1568 rc = -EIO;
1569 goto next_rx;
1570 }
1571
1572 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001573 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001574
Michael Chanc6d30e82017-02-06 16:55:42 -05001575 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1576 rc = 1;
1577 goto next_rx;
1578 }
1579
Michael Chanc0c050c2015-10-22 16:01:17 -04001580 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001581 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001582 bnxt_reuse_rx_data(rxr, cons, data);
1583 if (!skb) {
1584 rc = -ENOMEM;
1585 goto next_rx;
1586 }
1587 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001588 u32 payload;
1589
Michael Chanc6d30e82017-02-06 16:55:42 -05001590 if (rx_buf->data_ptr == data_ptr)
1591 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1592 else
1593 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001594 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001595 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001596 if (!skb) {
1597 rc = -ENOMEM;
1598 goto next_rx;
1599 }
1600 }
1601
1602 if (agg_bufs) {
1603 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1604 if (!skb) {
1605 rc = -ENOMEM;
1606 goto next_rx;
1607 }
1608 }
1609
1610 if (RX_CMP_HASH_VALID(rxcmp)) {
1611 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1612 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1613
1614 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1615 if (hash_type != 1 && hash_type != 3)
1616 type = PKT_HASH_TYPE_L3;
1617 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1618 }
1619
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001620 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1621 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001622
Michael Chan8852ddb2016-06-06 02:37:16 -04001623 if ((rxcmp1->rx_cmp_flags2 &
1624 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1625 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001626 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chaned7bc6022018-03-09 23:46:06 -05001627 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001628 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1629
Michael Chan8852ddb2016-06-06 02:37:16 -04001630 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001631 }
1632
1633 skb_checksum_none_assert(skb);
1634 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1635 if (dev->features & NETIF_F_RXCSUM) {
1636 skb->ip_summed = CHECKSUM_UNNECESSARY;
1637 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1638 }
1639 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001640 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1641 if (dev->features & NETIF_F_RXCSUM)
1642 cpr->rx_l4_csum_errors++;
1643 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001644 }
1645
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001646 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001647 rc = 1;
1648
1649next_rx:
1650 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001651 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001652
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001653 cpr->rx_packets += 1;
1654 cpr->rx_bytes += len;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001655
1656next_rx_no_prod_no_len:
Michael Chanc0c050c2015-10-22 16:01:17 -04001657 *raw_cons = tmp_raw_cons;
1658
1659 return rc;
1660}
1661
Michael Chan2270bc52017-06-23 14:01:01 -04001662/* In netpoll mode, if we are using a combined completion ring, we need to
1663 * discard the rx packets and recycle the buffers.
1664 */
1665static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1666 u32 *raw_cons, u8 *event)
1667{
1668 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1669 u32 tmp_raw_cons = *raw_cons;
1670 struct rx_cmp_ext *rxcmp1;
1671 struct rx_cmp *rxcmp;
1672 u16 cp_cons;
1673 u8 cmp_type;
1674
1675 cp_cons = RING_CMP(tmp_raw_cons);
1676 rxcmp = (struct rx_cmp *)
1677 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1678
1679 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1680 cp_cons = RING_CMP(tmp_raw_cons);
1681 rxcmp1 = (struct rx_cmp_ext *)
1682 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1683
1684 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1685 return -EBUSY;
1686
1687 cmp_type = RX_CMP_TYPE(rxcmp);
1688 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1689 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1690 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1691 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1692 struct rx_tpa_end_cmp_ext *tpa_end1;
1693
1694 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1695 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1696 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1697 }
1698 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1699}
1700
Michael Chan4bb13ab2016-04-05 14:09:01 -04001701#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001702 ((data) & \
1703 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001704
Michael Chanc0c050c2015-10-22 16:01:17 -04001705static int bnxt_async_event_process(struct bnxt *bp,
1706 struct hwrm_async_event_cmpl *cmpl)
1707{
1708 u16 event_id = le16_to_cpu(cmpl->event_id);
1709
1710 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1711 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001712 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001713 u32 data1 = le32_to_cpu(cmpl->event_data1);
1714 struct bnxt_link_info *link_info = &bp->link_info;
1715
1716 if (BNXT_VF(bp))
1717 goto async_event_process_exit;
Michael Chana8168b62017-12-06 17:31:22 -05001718
1719 /* print unsupported speed warning in forced speed mode only */
1720 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1721 (data1 & 0x20000)) {
Michael Chan8cbde112016-04-11 04:11:14 -04001722 u16 fw_speed = link_info->force_link_speed;
1723 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1724
Michael Chana8168b62017-12-06 17:31:22 -05001725 if (speed != SPEED_UNKNOWN)
1726 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1727 speed);
Michael Chan8cbde112016-04-11 04:11:14 -04001728 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001729 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001730 /* fall thru */
1731 }
Michael Chan87c374d2016-12-02 21:17:16 -05001732 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001733 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001734 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001735 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001736 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001737 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001738 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001739 u32 data1 = le32_to_cpu(cmpl->event_data1);
1740 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1741
1742 if (BNXT_VF(bp))
1743 break;
1744
1745 if (bp->pf.port_id != port_id)
1746 break;
1747
Michael Chan4bb13ab2016-04-05 14:09:01 -04001748 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1749 break;
1750 }
Michael Chan87c374d2016-12-02 21:17:16 -05001751 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001752 if (BNXT_PF(bp))
1753 goto async_event_process_exit;
1754 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1755 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001756 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001757 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001758 }
Michael Chanc213eae2017-10-13 21:09:29 -04001759 bnxt_queue_sp_work(bp);
Jeffrey Huang19241362016-02-26 04:00:00 -05001760async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001761 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001762 return 0;
1763}
1764
1765static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1766{
1767 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1768 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1769 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1770 (struct hwrm_fwd_req_cmpl *)txcmp;
1771
1772 switch (cmpl_type) {
1773 case CMPL_BASE_TYPE_HWRM_DONE:
1774 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1775 if (seq_id == bp->hwrm_intr_seq_id)
1776 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1777 else
1778 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1779 break;
1780
1781 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1782 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1783
1784 if ((vf_id < bp->pf.first_vf_id) ||
1785 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1786 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1787 vf_id);
1788 return -EINVAL;
1789 }
1790
1791 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1792 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001793 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04001794 break;
1795
1796 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1797 bnxt_async_event_process(bp,
1798 (struct hwrm_async_event_cmpl *)txcmp);
1799
1800 default:
1801 break;
1802 }
1803
1804 return 0;
1805}
1806
1807static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1808{
1809 struct bnxt_napi *bnapi = dev_instance;
1810 struct bnxt *bp = bnapi->bp;
1811 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1812 u32 cons = RING_CMP(cpr->cp_raw_cons);
1813
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001814 cpr->event_ctr++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001815 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1816 napi_schedule(&bnapi->napi);
1817 return IRQ_HANDLED;
1818}
1819
1820static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1821{
1822 u32 raw_cons = cpr->cp_raw_cons;
1823 u16 cons = RING_CMP(raw_cons);
1824 struct tx_cmp *txcmp;
1825
1826 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1827
1828 return TX_CMP_VALID(txcmp, raw_cons);
1829}
1830
Michael Chanc0c050c2015-10-22 16:01:17 -04001831static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1832{
1833 struct bnxt_napi *bnapi = dev_instance;
1834 struct bnxt *bp = bnapi->bp;
1835 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1836 u32 cons = RING_CMP(cpr->cp_raw_cons);
1837 u32 int_status;
1838
1839 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1840
1841 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001842 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001843 /* return if erroneous interrupt */
1844 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1845 return IRQ_NONE;
1846 }
1847
1848 /* disable ring IRQ */
1849 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1850
1851 /* Return here if interrupt is shared and is disabled. */
1852 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1853 return IRQ_HANDLED;
1854
1855 napi_schedule(&bnapi->napi);
1856 return IRQ_HANDLED;
1857}
1858
1859static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1860{
1861 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1862 u32 raw_cons = cpr->cp_raw_cons;
1863 u32 cons;
1864 int tx_pkts = 0;
1865 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001866 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001867 struct tx_cmp *txcmp;
1868
1869 while (1) {
1870 int rc;
1871
1872 cons = RING_CMP(raw_cons);
1873 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1874
1875 if (!TX_CMP_VALID(txcmp, raw_cons))
1876 break;
1877
Michael Chan67a95e22016-05-04 16:56:43 -04001878 /* The valid test of the entry must be done first before
1879 * reading any further.
1880 */
Michael Chanb67daab2016-05-15 03:04:51 -04001881 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001882 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1883 tx_pkts++;
1884 /* return full budget so NAPI will complete. */
1885 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1886 rx_pkts = budget;
1887 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001888 if (likely(budget))
1889 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1890 else
1891 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1892 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001893 if (likely(rc >= 0))
1894 rx_pkts += rc;
Michael Chan903649e2017-08-28 13:40:30 -04001895 /* Increment rx_pkts when rc is -ENOMEM to count towards
1896 * the NAPI budget. Otherwise, we may potentially loop
1897 * here forever if we consistently cannot allocate
1898 * buffers.
1899 */
Calvin Owens2edbdb32017-12-08 09:05:26 -08001900 else if (rc == -ENOMEM && budget)
Michael Chan903649e2017-08-28 13:40:30 -04001901 rx_pkts++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001902 else if (rc == -EBUSY) /* partial completion */
1903 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001904 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1905 CMPL_BASE_TYPE_HWRM_DONE) ||
1906 (TX_CMP_TYPE(txcmp) ==
1907 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1908 (TX_CMP_TYPE(txcmp) ==
1909 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1910 bnxt_hwrm_handler(bp, txcmp);
1911 }
1912 raw_cons = NEXT_RAW_CMP(raw_cons);
1913
1914 if (rx_pkts == budget)
1915 break;
1916 }
1917
Michael Chan38413402017-02-06 16:55:43 -05001918 if (event & BNXT_TX_EVENT) {
1919 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1920 void __iomem *db = txr->tx_doorbell;
1921 u16 prod = txr->tx_prod;
1922
1923 /* Sync BD data before updating doorbell */
1924 wmb();
1925
Sinan Kayafd141fa2018-03-25 10:39:20 -04001926 bnxt_db_write_relaxed(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001927 }
1928
Michael Chanc0c050c2015-10-22 16:01:17 -04001929 cpr->cp_raw_cons = raw_cons;
1930 /* ACK completion ring before freeing tx ring and producing new
1931 * buffers in rx/agg rings to prevent overflowing the completion
1932 * ring.
1933 */
1934 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1935
1936 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001937 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001938
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001939 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001940 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001941
Michael Chan434c9752017-05-29 19:06:08 -04001942 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1943 if (event & BNXT_AGG_EVENT)
1944 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1945 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001946 }
1947 return rx_pkts;
1948}
1949
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001950static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1951{
1952 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1953 struct bnxt *bp = bnapi->bp;
1954 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1955 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1956 struct tx_cmp *txcmp;
1957 struct rx_cmp_ext *rxcmp1;
1958 u32 cp_cons, tmp_raw_cons;
1959 u32 raw_cons = cpr->cp_raw_cons;
1960 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001961 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001962
1963 while (1) {
1964 int rc;
1965
1966 cp_cons = RING_CMP(raw_cons);
1967 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1968
1969 if (!TX_CMP_VALID(txcmp, raw_cons))
1970 break;
1971
1972 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1973 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1974 cp_cons = RING_CMP(tmp_raw_cons);
1975 rxcmp1 = (struct rx_cmp_ext *)
1976 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1977
1978 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1979 break;
1980
1981 /* force an error to recycle the buffer */
1982 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1983 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1984
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001985 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Calvin Owens2edbdb32017-12-08 09:05:26 -08001986 if (likely(rc == -EIO) && budget)
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001987 rx_pkts++;
1988 else if (rc == -EBUSY) /* partial completion */
1989 break;
1990 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1991 CMPL_BASE_TYPE_HWRM_DONE)) {
1992 bnxt_hwrm_handler(bp, txcmp);
1993 } else {
1994 netdev_err(bp->dev,
1995 "Invalid completion received on special ring\n");
1996 }
1997 raw_cons = NEXT_RAW_CMP(raw_cons);
1998
1999 if (rx_pkts == budget)
2000 break;
2001 }
2002
2003 cpr->cp_raw_cons = raw_cons;
2004 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04002005 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002006
Michael Chan434c9752017-05-29 19:06:08 -04002007 if (event & BNXT_AGG_EVENT)
2008 bnxt_db_write(bp, rxr->rx_agg_doorbell,
2009 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002010
2011 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08002012 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002013 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2014 }
2015 return rx_pkts;
2016}
2017
Michael Chanc0c050c2015-10-22 16:01:17 -04002018static int bnxt_poll(struct napi_struct *napi, int budget)
2019{
2020 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2021 struct bnxt *bp = bnapi->bp;
2022 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2023 int work_done = 0;
2024
Michael Chanc0c050c2015-10-22 16:01:17 -04002025 while (1) {
2026 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2027
2028 if (work_done >= budget)
2029 break;
2030
2031 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05002032 if (napi_complete_done(napi, work_done))
2033 BNXT_CP_DB_REARM(cpr->cp_doorbell,
2034 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002035 break;
2036 }
2037 }
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002038 if (bp->flags & BNXT_FLAG_DIM) {
2039 struct net_dim_sample dim_sample;
2040
2041 net_dim_sample(cpr->event_ctr,
2042 cpr->rx_packets,
2043 cpr->rx_bytes,
2044 &dim_sample);
2045 net_dim(&cpr->dim, dim_sample);
2046 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002047 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04002048 return work_done;
2049}
2050
Michael Chanc0c050c2015-10-22 16:01:17 -04002051static void bnxt_free_tx_skbs(struct bnxt *bp)
2052{
2053 int i, max_idx;
2054 struct pci_dev *pdev = bp->pdev;
2055
Michael Chanb6ab4b02016-01-02 23:44:59 -05002056 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002057 return;
2058
2059 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2060 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002061 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002062 int j;
2063
Michael Chanc0c050c2015-10-22 16:01:17 -04002064 for (j = 0; j < max_idx;) {
2065 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2066 struct sk_buff *skb = tx_buf->skb;
2067 int k, last;
2068
2069 if (!skb) {
2070 j++;
2071 continue;
2072 }
2073
2074 tx_buf->skb = NULL;
2075
2076 if (tx_buf->is_push) {
2077 dev_kfree_skb(skb);
2078 j += 2;
2079 continue;
2080 }
2081
2082 dma_unmap_single(&pdev->dev,
2083 dma_unmap_addr(tx_buf, mapping),
2084 skb_headlen(skb),
2085 PCI_DMA_TODEVICE);
2086
2087 last = tx_buf->nr_frags;
2088 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002089 for (k = 0; k < last; k++, j++) {
2090 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002091 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2092
Michael Chand612a572016-01-28 03:11:22 -05002093 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002094 dma_unmap_page(
2095 &pdev->dev,
2096 dma_unmap_addr(tx_buf, mapping),
2097 skb_frag_size(frag), PCI_DMA_TODEVICE);
2098 }
2099 dev_kfree_skb(skb);
2100 }
2101 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2102 }
2103}
2104
2105static void bnxt_free_rx_skbs(struct bnxt *bp)
2106{
2107 int i, max_idx, max_agg_idx;
2108 struct pci_dev *pdev = bp->pdev;
2109
Michael Chanb6ab4b02016-01-02 23:44:59 -05002110 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002111 return;
2112
2113 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2114 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2115 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002116 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002117 int j;
2118
Michael Chanc0c050c2015-10-22 16:01:17 -04002119 if (rxr->rx_tpa) {
2120 for (j = 0; j < MAX_TPA; j++) {
2121 struct bnxt_tpa_info *tpa_info =
2122 &rxr->rx_tpa[j];
2123 u8 *data = tpa_info->data;
2124
2125 if (!data)
2126 continue;
2127
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002128 dma_unmap_single_attrs(&pdev->dev,
2129 tpa_info->mapping,
2130 bp->rx_buf_use_size,
2131 bp->rx_dir,
2132 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002133
2134 tpa_info->data = NULL;
2135
2136 kfree(data);
2137 }
2138 }
2139
2140 for (j = 0; j < max_idx; j++) {
2141 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002142 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002143 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002144
2145 if (!data)
2146 continue;
2147
Michael Chanc0c050c2015-10-22 16:01:17 -04002148 rx_buf->data = NULL;
2149
Michael Chan3ed3a832017-03-28 19:47:31 -04002150 if (BNXT_RX_PAGE_MODE(bp)) {
2151 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002152 dma_unmap_page_attrs(&pdev->dev, mapping,
2153 PAGE_SIZE, bp->rx_dir,
2154 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002155 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002156 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002157 dma_unmap_single_attrs(&pdev->dev, mapping,
2158 bp->rx_buf_use_size,
2159 bp->rx_dir,
2160 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002161 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002162 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002163 }
2164
2165 for (j = 0; j < max_agg_idx; j++) {
2166 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2167 &rxr->rx_agg_ring[j];
2168 struct page *page = rx_agg_buf->page;
2169
2170 if (!page)
2171 continue;
2172
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002173 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2174 BNXT_RX_PAGE_SIZE,
2175 PCI_DMA_FROMDEVICE,
2176 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002177
2178 rx_agg_buf->page = NULL;
2179 __clear_bit(j, rxr->rx_agg_bmap);
2180
2181 __free_page(page);
2182 }
Michael Chan89d0a062016-04-25 02:30:51 -04002183 if (rxr->rx_page) {
2184 __free_page(rxr->rx_page);
2185 rxr->rx_page = NULL;
2186 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002187 }
2188}
2189
2190static void bnxt_free_skbs(struct bnxt *bp)
2191{
2192 bnxt_free_tx_skbs(bp);
2193 bnxt_free_rx_skbs(bp);
2194}
2195
2196static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2197{
2198 struct pci_dev *pdev = bp->pdev;
2199 int i;
2200
2201 for (i = 0; i < ring->nr_pages; i++) {
2202 if (!ring->pg_arr[i])
2203 continue;
2204
2205 dma_free_coherent(&pdev->dev, ring->page_size,
2206 ring->pg_arr[i], ring->dma_arr[i]);
2207
2208 ring->pg_arr[i] = NULL;
2209 }
2210 if (ring->pg_tbl) {
2211 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2212 ring->pg_tbl, ring->pg_tbl_map);
2213 ring->pg_tbl = NULL;
2214 }
2215 if (ring->vmem_size && *ring->vmem) {
2216 vfree(*ring->vmem);
2217 *ring->vmem = NULL;
2218 }
2219}
2220
2221static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2222{
2223 int i;
2224 struct pci_dev *pdev = bp->pdev;
2225
2226 if (ring->nr_pages > 1) {
2227 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2228 ring->nr_pages * 8,
2229 &ring->pg_tbl_map,
2230 GFP_KERNEL);
2231 if (!ring->pg_tbl)
2232 return -ENOMEM;
2233 }
2234
2235 for (i = 0; i < ring->nr_pages; i++) {
2236 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2237 ring->page_size,
2238 &ring->dma_arr[i],
2239 GFP_KERNEL);
2240 if (!ring->pg_arr[i])
2241 return -ENOMEM;
2242
2243 if (ring->nr_pages > 1)
2244 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2245 }
2246
2247 if (ring->vmem_size) {
2248 *ring->vmem = vzalloc(ring->vmem_size);
2249 if (!(*ring->vmem))
2250 return -ENOMEM;
2251 }
2252 return 0;
2253}
2254
2255static void bnxt_free_rx_rings(struct bnxt *bp)
2256{
2257 int i;
2258
Michael Chanb6ab4b02016-01-02 23:44:59 -05002259 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002260 return;
2261
2262 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002263 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002264 struct bnxt_ring_struct *ring;
2265
Michael Chanc6d30e82017-02-06 16:55:42 -05002266 if (rxr->xdp_prog)
2267 bpf_prog_put(rxr->xdp_prog);
2268
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002269 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2270 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2271
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 kfree(rxr->rx_tpa);
2273 rxr->rx_tpa = NULL;
2274
2275 kfree(rxr->rx_agg_bmap);
2276 rxr->rx_agg_bmap = NULL;
2277
2278 ring = &rxr->rx_ring_struct;
2279 bnxt_free_ring(bp, ring);
2280
2281 ring = &rxr->rx_agg_ring_struct;
2282 bnxt_free_ring(bp, ring);
2283 }
2284}
2285
2286static int bnxt_alloc_rx_rings(struct bnxt *bp)
2287{
2288 int i, rc, agg_rings = 0, tpa_rings = 0;
2289
Michael Chanb6ab4b02016-01-02 23:44:59 -05002290 if (!bp->rx_ring)
2291 return -ENOMEM;
2292
Michael Chanc0c050c2015-10-22 16:01:17 -04002293 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2294 agg_rings = 1;
2295
2296 if (bp->flags & BNXT_FLAG_TPA)
2297 tpa_rings = 1;
2298
2299 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002300 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002301 struct bnxt_ring_struct *ring;
2302
Michael Chanc0c050c2015-10-22 16:01:17 -04002303 ring = &rxr->rx_ring_struct;
2304
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002305 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2306 if (rc < 0)
2307 return rc;
2308
Michael Chanc0c050c2015-10-22 16:01:17 -04002309 rc = bnxt_alloc_ring(bp, ring);
2310 if (rc)
2311 return rc;
2312
2313 if (agg_rings) {
2314 u16 mem_size;
2315
2316 ring = &rxr->rx_agg_ring_struct;
2317 rc = bnxt_alloc_ring(bp, ring);
2318 if (rc)
2319 return rc;
2320
Michael Chan9899bb52018-03-31 13:54:16 -04002321 ring->grp_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002322 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2323 mem_size = rxr->rx_agg_bmap_size / 8;
2324 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2325 if (!rxr->rx_agg_bmap)
2326 return -ENOMEM;
2327
2328 if (tpa_rings) {
2329 rxr->rx_tpa = kcalloc(MAX_TPA,
2330 sizeof(struct bnxt_tpa_info),
2331 GFP_KERNEL);
2332 if (!rxr->rx_tpa)
2333 return -ENOMEM;
2334 }
2335 }
2336 }
2337 return 0;
2338}
2339
2340static void bnxt_free_tx_rings(struct bnxt *bp)
2341{
2342 int i;
2343 struct pci_dev *pdev = bp->pdev;
2344
Michael Chanb6ab4b02016-01-02 23:44:59 -05002345 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002346 return;
2347
2348 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002349 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002350 struct bnxt_ring_struct *ring;
2351
Michael Chanc0c050c2015-10-22 16:01:17 -04002352 if (txr->tx_push) {
2353 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2354 txr->tx_push, txr->tx_push_mapping);
2355 txr->tx_push = NULL;
2356 }
2357
2358 ring = &txr->tx_ring_struct;
2359
2360 bnxt_free_ring(bp, ring);
2361 }
2362}
2363
2364static int bnxt_alloc_tx_rings(struct bnxt *bp)
2365{
2366 int i, j, rc;
2367 struct pci_dev *pdev = bp->pdev;
2368
2369 bp->tx_push_size = 0;
2370 if (bp->tx_push_thresh) {
2371 int push_size;
2372
2373 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2374 bp->tx_push_thresh);
2375
Michael Chan4419dbe2016-02-10 17:33:49 -05002376 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002377 push_size = 0;
2378 bp->tx_push_thresh = 0;
2379 }
2380
2381 bp->tx_push_size = push_size;
2382 }
2383
2384 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002385 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002386 struct bnxt_ring_struct *ring;
Michael Chan2e8ef772018-04-26 17:44:31 -04002387 u8 qidx;
Michael Chanc0c050c2015-10-22 16:01:17 -04002388
Michael Chanc0c050c2015-10-22 16:01:17 -04002389 ring = &txr->tx_ring_struct;
2390
2391 rc = bnxt_alloc_ring(bp, ring);
2392 if (rc)
2393 return rc;
2394
Michael Chan9899bb52018-03-31 13:54:16 -04002395 ring->grp_idx = txr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04002396 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002397 dma_addr_t mapping;
2398
2399 /* One pre-allocated DMA buffer to backup
2400 * TX push operation
2401 */
2402 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2403 bp->tx_push_size,
2404 &txr->tx_push_mapping,
2405 GFP_KERNEL);
2406
2407 if (!txr->tx_push)
2408 return -ENOMEM;
2409
Michael Chanc0c050c2015-10-22 16:01:17 -04002410 mapping = txr->tx_push_mapping +
2411 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002412 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002413
Michael Chan4419dbe2016-02-10 17:33:49 -05002414 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002415 }
Michael Chan2e8ef772018-04-26 17:44:31 -04002416 qidx = bp->tc_to_qidx[j];
2417 ring->queue_id = bp->q_info[qidx].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002418 if (i < bp->tx_nr_rings_xdp)
2419 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002420 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2421 j++;
2422 }
2423 return 0;
2424}
2425
2426static void bnxt_free_cp_rings(struct bnxt *bp)
2427{
2428 int i;
2429
2430 if (!bp->bnapi)
2431 return;
2432
2433 for (i = 0; i < bp->cp_nr_rings; i++) {
2434 struct bnxt_napi *bnapi = bp->bnapi[i];
2435 struct bnxt_cp_ring_info *cpr;
2436 struct bnxt_ring_struct *ring;
2437
2438 if (!bnapi)
2439 continue;
2440
2441 cpr = &bnapi->cp_ring;
2442 ring = &cpr->cp_ring_struct;
2443
2444 bnxt_free_ring(bp, ring);
2445 }
2446}
2447
2448static int bnxt_alloc_cp_rings(struct bnxt *bp)
2449{
Michael Chane5811b82018-03-31 13:54:18 -04002450 int i, rc, ulp_base_vec, ulp_msix;
Michael Chanc0c050c2015-10-22 16:01:17 -04002451
Michael Chane5811b82018-03-31 13:54:18 -04002452 ulp_msix = bnxt_get_ulp_msix_num(bp);
2453 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04002454 for (i = 0; i < bp->cp_nr_rings; i++) {
2455 struct bnxt_napi *bnapi = bp->bnapi[i];
2456 struct bnxt_cp_ring_info *cpr;
2457 struct bnxt_ring_struct *ring;
2458
2459 if (!bnapi)
2460 continue;
2461
2462 cpr = &bnapi->cp_ring;
2463 ring = &cpr->cp_ring_struct;
2464
2465 rc = bnxt_alloc_ring(bp, ring);
2466 if (rc)
2467 return rc;
Michael Chane5811b82018-03-31 13:54:18 -04002468
2469 if (ulp_msix && i >= ulp_base_vec)
2470 ring->map_idx = i + ulp_msix;
2471 else
2472 ring->map_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002473 }
2474 return 0;
2475}
2476
2477static void bnxt_init_ring_struct(struct bnxt *bp)
2478{
2479 int i;
2480
2481 for (i = 0; i < bp->cp_nr_rings; i++) {
2482 struct bnxt_napi *bnapi = bp->bnapi[i];
2483 struct bnxt_cp_ring_info *cpr;
2484 struct bnxt_rx_ring_info *rxr;
2485 struct bnxt_tx_ring_info *txr;
2486 struct bnxt_ring_struct *ring;
2487
2488 if (!bnapi)
2489 continue;
2490
2491 cpr = &bnapi->cp_ring;
2492 ring = &cpr->cp_ring_struct;
2493 ring->nr_pages = bp->cp_nr_pages;
2494 ring->page_size = HW_CMPD_RING_SIZE;
2495 ring->pg_arr = (void **)cpr->cp_desc_ring;
2496 ring->dma_arr = cpr->cp_desc_mapping;
2497 ring->vmem_size = 0;
2498
Michael Chanb6ab4b02016-01-02 23:44:59 -05002499 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002500 if (!rxr)
2501 goto skip_rx;
2502
Michael Chanc0c050c2015-10-22 16:01:17 -04002503 ring = &rxr->rx_ring_struct;
2504 ring->nr_pages = bp->rx_nr_pages;
2505 ring->page_size = HW_RXBD_RING_SIZE;
2506 ring->pg_arr = (void **)rxr->rx_desc_ring;
2507 ring->dma_arr = rxr->rx_desc_mapping;
2508 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2509 ring->vmem = (void **)&rxr->rx_buf_ring;
2510
2511 ring = &rxr->rx_agg_ring_struct;
2512 ring->nr_pages = bp->rx_agg_nr_pages;
2513 ring->page_size = HW_RXBD_RING_SIZE;
2514 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2515 ring->dma_arr = rxr->rx_agg_desc_mapping;
2516 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2517 ring->vmem = (void **)&rxr->rx_agg_ring;
2518
Michael Chan3b2b7d92016-01-02 23:45:00 -05002519skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002520 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002521 if (!txr)
2522 continue;
2523
Michael Chanc0c050c2015-10-22 16:01:17 -04002524 ring = &txr->tx_ring_struct;
2525 ring->nr_pages = bp->tx_nr_pages;
2526 ring->page_size = HW_RXBD_RING_SIZE;
2527 ring->pg_arr = (void **)txr->tx_desc_ring;
2528 ring->dma_arr = txr->tx_desc_mapping;
2529 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2530 ring->vmem = (void **)&txr->tx_buf_ring;
2531 }
2532}
2533
2534static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2535{
2536 int i;
2537 u32 prod;
2538 struct rx_bd **rx_buf_ring;
2539
2540 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2541 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2542 int j;
2543 struct rx_bd *rxbd;
2544
2545 rxbd = rx_buf_ring[i];
2546 if (!rxbd)
2547 continue;
2548
2549 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2550 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2551 rxbd->rx_bd_opaque = prod;
2552 }
2553 }
2554}
2555
2556static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2557{
2558 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002559 struct bnxt_rx_ring_info *rxr;
2560 struct bnxt_ring_struct *ring;
2561 u32 prod, type;
2562 int i;
2563
Michael Chanc0c050c2015-10-22 16:01:17 -04002564 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2565 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2566
2567 if (NET_IP_ALIGN == 2)
2568 type |= RX_BD_FLAGS_SOP;
2569
Michael Chanb6ab4b02016-01-02 23:44:59 -05002570 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002571 ring = &rxr->rx_ring_struct;
2572 bnxt_init_rxbd_pages(ring, type);
2573
Michael Chanc6d30e82017-02-06 16:55:42 -05002574 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2575 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2576 if (IS_ERR(rxr->xdp_prog)) {
2577 int rc = PTR_ERR(rxr->xdp_prog);
2578
2579 rxr->xdp_prog = NULL;
2580 return rc;
2581 }
2582 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002583 prod = rxr->rx_prod;
2584 for (i = 0; i < bp->rx_ring_size; i++) {
2585 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2586 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2587 ring_nr, i, bp->rx_ring_size);
2588 break;
2589 }
2590 prod = NEXT_RX(prod);
2591 }
2592 rxr->rx_prod = prod;
2593 ring->fw_ring_id = INVALID_HW_RING_ID;
2594
Michael Chanedd0c2c2015-12-27 18:19:19 -05002595 ring = &rxr->rx_agg_ring_struct;
2596 ring->fw_ring_id = INVALID_HW_RING_ID;
2597
Michael Chanc0c050c2015-10-22 16:01:17 -04002598 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2599 return 0;
2600
Michael Chan2839f282016-04-25 02:30:50 -04002601 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002602 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2603
2604 bnxt_init_rxbd_pages(ring, type);
2605
2606 prod = rxr->rx_agg_prod;
2607 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2608 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2609 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2610 ring_nr, i, bp->rx_ring_size);
2611 break;
2612 }
2613 prod = NEXT_RX_AGG(prod);
2614 }
2615 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002616
2617 if (bp->flags & BNXT_FLAG_TPA) {
2618 if (rxr->rx_tpa) {
2619 u8 *data;
2620 dma_addr_t mapping;
2621
2622 for (i = 0; i < MAX_TPA; i++) {
2623 data = __bnxt_alloc_rx_data(bp, &mapping,
2624 GFP_KERNEL);
2625 if (!data)
2626 return -ENOMEM;
2627
2628 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002629 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002630 rxr->rx_tpa[i].mapping = mapping;
2631 }
2632 } else {
2633 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2634 return -ENOMEM;
2635 }
2636 }
2637
2638 return 0;
2639}
2640
Sankar Patchineelam22479252017-03-28 19:47:29 -04002641static void bnxt_init_cp_rings(struct bnxt *bp)
2642{
2643 int i;
2644
2645 for (i = 0; i < bp->cp_nr_rings; i++) {
2646 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2647 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2648
2649 ring->fw_ring_id = INVALID_HW_RING_ID;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002650 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2651 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
Sankar Patchineelam22479252017-03-28 19:47:29 -04002652 }
2653}
2654
Michael Chanc0c050c2015-10-22 16:01:17 -04002655static int bnxt_init_rx_rings(struct bnxt *bp)
2656{
2657 int i, rc = 0;
2658
Michael Chanc61fb992017-02-06 16:55:36 -05002659 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002660 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2661 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002662 } else {
2663 bp->rx_offset = BNXT_RX_OFFSET;
2664 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2665 }
Michael Chanb3dba772017-02-06 16:55:35 -05002666
Michael Chanc0c050c2015-10-22 16:01:17 -04002667 for (i = 0; i < bp->rx_nr_rings; i++) {
2668 rc = bnxt_init_one_rx_ring(bp, i);
2669 if (rc)
2670 break;
2671 }
2672
2673 return rc;
2674}
2675
2676static int bnxt_init_tx_rings(struct bnxt *bp)
2677{
2678 u16 i;
2679
2680 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2681 MAX_SKB_FRAGS + 1);
2682
2683 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002684 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002685 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2686
2687 ring->fw_ring_id = INVALID_HW_RING_ID;
2688 }
2689
2690 return 0;
2691}
2692
2693static void bnxt_free_ring_grps(struct bnxt *bp)
2694{
2695 kfree(bp->grp_info);
2696 bp->grp_info = NULL;
2697}
2698
2699static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2700{
2701 int i;
2702
2703 if (irq_re_init) {
2704 bp->grp_info = kcalloc(bp->cp_nr_rings,
2705 sizeof(struct bnxt_ring_grp_info),
2706 GFP_KERNEL);
2707 if (!bp->grp_info)
2708 return -ENOMEM;
2709 }
2710 for (i = 0; i < bp->cp_nr_rings; i++) {
2711 if (irq_re_init)
2712 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2713 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2714 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2715 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2716 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2717 }
2718 return 0;
2719}
2720
2721static void bnxt_free_vnics(struct bnxt *bp)
2722{
2723 kfree(bp->vnic_info);
2724 bp->vnic_info = NULL;
2725 bp->nr_vnics = 0;
2726}
2727
2728static int bnxt_alloc_vnics(struct bnxt *bp)
2729{
2730 int num_vnics = 1;
2731
2732#ifdef CONFIG_RFS_ACCEL
2733 if (bp->flags & BNXT_FLAG_RFS)
2734 num_vnics += bp->rx_nr_rings;
2735#endif
2736
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002737 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2738 num_vnics++;
2739
Michael Chanc0c050c2015-10-22 16:01:17 -04002740 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2741 GFP_KERNEL);
2742 if (!bp->vnic_info)
2743 return -ENOMEM;
2744
2745 bp->nr_vnics = num_vnics;
2746 return 0;
2747}
2748
2749static void bnxt_init_vnics(struct bnxt *bp)
2750{
2751 int i;
2752
2753 for (i = 0; i < bp->nr_vnics; i++) {
2754 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2755
2756 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002757 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2758 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002759 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2760
2761 if (bp->vnic_info[i].rss_hash_key) {
2762 if (i == 0)
2763 prandom_bytes(vnic->rss_hash_key,
2764 HW_HASH_KEY_SIZE);
2765 else
2766 memcpy(vnic->rss_hash_key,
2767 bp->vnic_info[0].rss_hash_key,
2768 HW_HASH_KEY_SIZE);
2769 }
2770 }
2771}
2772
2773static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2774{
2775 int pages;
2776
2777 pages = ring_size / desc_per_pg;
2778
2779 if (!pages)
2780 return 1;
2781
2782 pages++;
2783
2784 while (pages & (pages - 1))
2785 pages++;
2786
2787 return pages;
2788}
2789
Michael Chanc6d30e82017-02-06 16:55:42 -05002790void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002791{
2792 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002793 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2794 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002795 if (bp->dev->features & NETIF_F_LRO)
2796 bp->flags |= BNXT_FLAG_LRO;
Michael Chan1054aee2017-12-16 03:09:42 -05002797 else if (bp->dev->features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04002798 bp->flags |= BNXT_FLAG_GRO;
2799}
2800
2801/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2802 * be set on entry.
2803 */
2804void bnxt_set_ring_params(struct bnxt *bp)
2805{
2806 u32 ring_size, rx_size, rx_space;
2807 u32 agg_factor = 0, agg_ring_size = 0;
2808
2809 /* 8 for CRC and VLAN */
2810 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2811
2812 rx_space = rx_size + NET_SKB_PAD +
2813 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2814
2815 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2816 ring_size = bp->rx_ring_size;
2817 bp->rx_agg_ring_size = 0;
2818 bp->rx_agg_nr_pages = 0;
2819
2820 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002821 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002822
2823 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002824 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002825 u32 jumbo_factor;
2826
2827 bp->flags |= BNXT_FLAG_JUMBO;
2828 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2829 if (jumbo_factor > agg_factor)
2830 agg_factor = jumbo_factor;
2831 }
2832 agg_ring_size = ring_size * agg_factor;
2833
2834 if (agg_ring_size) {
2835 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2836 RX_DESC_CNT);
2837 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2838 u32 tmp = agg_ring_size;
2839
2840 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2841 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2842 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2843 tmp, agg_ring_size);
2844 }
2845 bp->rx_agg_ring_size = agg_ring_size;
2846 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2847 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2848 rx_space = rx_size + NET_SKB_PAD +
2849 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2850 }
2851
2852 bp->rx_buf_use_size = rx_size;
2853 bp->rx_buf_size = rx_space;
2854
2855 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2856 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2857
2858 ring_size = bp->tx_ring_size;
2859 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2860 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2861
2862 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2863 bp->cp_ring_size = ring_size;
2864
2865 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2866 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2867 bp->cp_nr_pages = MAX_CP_PAGES;
2868 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2869 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2870 ring_size, bp->cp_ring_size);
2871 }
2872 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2873 bp->cp_ring_mask = bp->cp_bit - 1;
2874}
2875
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002876/* Changing allocation mode of RX rings.
2877 * TODO: Update when extending xdp_rxq_info to support allocation modes.
2878 */
Michael Chanc61fb992017-02-06 16:55:36 -05002879int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002880{
Michael Chanc61fb992017-02-06 16:55:36 -05002881 if (page_mode) {
2882 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2883 return -EOPNOTSUPP;
Michael Chan7eb9bb32017-10-26 11:51:25 -04002884 bp->dev->max_mtu =
2885 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
Michael Chanc61fb992017-02-06 16:55:36 -05002886 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2887 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
Michael Chanc61fb992017-02-06 16:55:36 -05002888 bp->rx_dir = DMA_BIDIRECTIONAL;
2889 bp->rx_skb_func = bnxt_rx_page_skb;
Michael Chan1054aee2017-12-16 03:09:42 -05002890 /* Disable LRO or GRO_HW */
2891 netdev_update_features(bp->dev);
Michael Chanc61fb992017-02-06 16:55:36 -05002892 } else {
Michael Chan7eb9bb32017-10-26 11:51:25 -04002893 bp->dev->max_mtu = bp->max_mtu;
Michael Chanc61fb992017-02-06 16:55:36 -05002894 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2895 bp->rx_dir = DMA_FROM_DEVICE;
2896 bp->rx_skb_func = bnxt_rx_skb;
2897 }
Michael Chan6bb19472017-02-06 16:55:32 -05002898 return 0;
2899}
2900
Michael Chanc0c050c2015-10-22 16:01:17 -04002901static void bnxt_free_vnic_attributes(struct bnxt *bp)
2902{
2903 int i;
2904 struct bnxt_vnic_info *vnic;
2905 struct pci_dev *pdev = bp->pdev;
2906
2907 if (!bp->vnic_info)
2908 return;
2909
2910 for (i = 0; i < bp->nr_vnics; i++) {
2911 vnic = &bp->vnic_info[i];
2912
2913 kfree(vnic->fw_grp_ids);
2914 vnic->fw_grp_ids = NULL;
2915
2916 kfree(vnic->uc_list);
2917 vnic->uc_list = NULL;
2918
2919 if (vnic->mc_list) {
2920 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2921 vnic->mc_list, vnic->mc_list_mapping);
2922 vnic->mc_list = NULL;
2923 }
2924
2925 if (vnic->rss_table) {
2926 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2927 vnic->rss_table,
2928 vnic->rss_table_dma_addr);
2929 vnic->rss_table = NULL;
2930 }
2931
2932 vnic->rss_hash_key = NULL;
2933 vnic->flags = 0;
2934 }
2935}
2936
2937static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2938{
2939 int i, rc = 0, size;
2940 struct bnxt_vnic_info *vnic;
2941 struct pci_dev *pdev = bp->pdev;
2942 int max_rings;
2943
2944 for (i = 0; i < bp->nr_vnics; i++) {
2945 vnic = &bp->vnic_info[i];
2946
2947 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2948 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2949
2950 if (mem_size > 0) {
2951 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2952 if (!vnic->uc_list) {
2953 rc = -ENOMEM;
2954 goto out;
2955 }
2956 }
2957 }
2958
2959 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2960 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2961 vnic->mc_list =
2962 dma_alloc_coherent(&pdev->dev,
2963 vnic->mc_list_size,
2964 &vnic->mc_list_mapping,
2965 GFP_KERNEL);
2966 if (!vnic->mc_list) {
2967 rc = -ENOMEM;
2968 goto out;
2969 }
2970 }
2971
2972 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2973 max_rings = bp->rx_nr_rings;
2974 else
2975 max_rings = 1;
2976
2977 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2978 if (!vnic->fw_grp_ids) {
2979 rc = -ENOMEM;
2980 goto out;
2981 }
2982
Michael Chanae10ae72016-12-29 12:13:38 -05002983 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2984 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2985 continue;
2986
Michael Chanc0c050c2015-10-22 16:01:17 -04002987 /* Allocate rss table and hash key */
2988 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2989 &vnic->rss_table_dma_addr,
2990 GFP_KERNEL);
2991 if (!vnic->rss_table) {
2992 rc = -ENOMEM;
2993 goto out;
2994 }
2995
2996 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2997
2998 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2999 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3000 }
3001 return 0;
3002
3003out:
3004 return rc;
3005}
3006
3007static void bnxt_free_hwrm_resources(struct bnxt *bp)
3008{
3009 struct pci_dev *pdev = bp->pdev;
3010
3011 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3012 bp->hwrm_cmd_resp_dma_addr);
3013
3014 bp->hwrm_cmd_resp_addr = NULL;
3015 if (bp->hwrm_dbg_resp_addr) {
3016 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
3017 bp->hwrm_dbg_resp_addr,
3018 bp->hwrm_dbg_resp_dma_addr);
3019
3020 bp->hwrm_dbg_resp_addr = NULL;
3021 }
3022}
3023
3024static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3025{
3026 struct pci_dev *pdev = bp->pdev;
3027
3028 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3029 &bp->hwrm_cmd_resp_dma_addr,
3030 GFP_KERNEL);
3031 if (!bp->hwrm_cmd_resp_addr)
3032 return -ENOMEM;
3033 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
3034 HWRM_DBG_REG_BUF_SIZE,
3035 &bp->hwrm_dbg_resp_dma_addr,
3036 GFP_KERNEL);
3037 if (!bp->hwrm_dbg_resp_addr)
3038 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
3039
3040 return 0;
3041}
3042
Deepak Khungare605db82017-05-29 19:06:04 -04003043static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3044{
3045 if (bp->hwrm_short_cmd_req_addr) {
3046 struct pci_dev *pdev = bp->pdev;
3047
3048 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3049 bp->hwrm_short_cmd_req_addr,
3050 bp->hwrm_short_cmd_req_dma_addr);
3051 bp->hwrm_short_cmd_req_addr = NULL;
3052 }
3053}
3054
3055static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3056{
3057 struct pci_dev *pdev = bp->pdev;
3058
3059 bp->hwrm_short_cmd_req_addr =
3060 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3061 &bp->hwrm_short_cmd_req_dma_addr,
3062 GFP_KERNEL);
3063 if (!bp->hwrm_short_cmd_req_addr)
3064 return -ENOMEM;
3065
3066 return 0;
3067}
3068
Michael Chanc0c050c2015-10-22 16:01:17 -04003069static void bnxt_free_stats(struct bnxt *bp)
3070{
3071 u32 size, i;
3072 struct pci_dev *pdev = bp->pdev;
3073
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003074 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3075 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3076
Michael Chan3bdf56c2016-03-07 15:38:45 -05003077 if (bp->hw_rx_port_stats) {
3078 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3079 bp->hw_rx_port_stats,
3080 bp->hw_rx_port_stats_map);
3081 bp->hw_rx_port_stats = NULL;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003082 }
3083
3084 if (bp->hw_rx_port_stats_ext) {
3085 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3086 bp->hw_rx_port_stats_ext,
3087 bp->hw_rx_port_stats_ext_map);
3088 bp->hw_rx_port_stats_ext = NULL;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003089 }
3090
Michael Chanc0c050c2015-10-22 16:01:17 -04003091 if (!bp->bnapi)
3092 return;
3093
3094 size = sizeof(struct ctx_hw_stats);
3095
3096 for (i = 0; i < bp->cp_nr_rings; i++) {
3097 struct bnxt_napi *bnapi = bp->bnapi[i];
3098 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3099
3100 if (cpr->hw_stats) {
3101 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3102 cpr->hw_stats_map);
3103 cpr->hw_stats = NULL;
3104 }
3105 }
3106}
3107
3108static int bnxt_alloc_stats(struct bnxt *bp)
3109{
3110 u32 size, i;
3111 struct pci_dev *pdev = bp->pdev;
3112
3113 size = sizeof(struct ctx_hw_stats);
3114
3115 for (i = 0; i < bp->cp_nr_rings; i++) {
3116 struct bnxt_napi *bnapi = bp->bnapi[i];
3117 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3118
3119 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3120 &cpr->hw_stats_map,
3121 GFP_KERNEL);
3122 if (!cpr->hw_stats)
3123 return -ENOMEM;
3124
3125 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3126 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003127
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003128 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05003129 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3130 sizeof(struct tx_port_stats) + 1024;
3131
3132 bp->hw_rx_port_stats =
3133 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3134 &bp->hw_rx_port_stats_map,
3135 GFP_KERNEL);
3136 if (!bp->hw_rx_port_stats)
3137 return -ENOMEM;
3138
3139 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3140 512;
3141 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3142 sizeof(struct rx_port_stats) + 512;
3143 bp->flags |= BNXT_FLAG_PORT_STATS;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003144
3145 /* Display extended statistics only if FW supports it */
3146 if (bp->hwrm_spec_code < 0x10804 ||
3147 bp->hwrm_spec_code == 0x10900)
3148 return 0;
3149
3150 bp->hw_rx_port_stats_ext =
3151 dma_zalloc_coherent(&pdev->dev,
3152 sizeof(struct rx_port_stats_ext),
3153 &bp->hw_rx_port_stats_ext_map,
3154 GFP_KERNEL);
3155 if (!bp->hw_rx_port_stats_ext)
3156 return 0;
3157
3158 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003159 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003160 return 0;
3161}
3162
3163static void bnxt_clear_ring_indices(struct bnxt *bp)
3164{
3165 int i;
3166
3167 if (!bp->bnapi)
3168 return;
3169
3170 for (i = 0; i < bp->cp_nr_rings; i++) {
3171 struct bnxt_napi *bnapi = bp->bnapi[i];
3172 struct bnxt_cp_ring_info *cpr;
3173 struct bnxt_rx_ring_info *rxr;
3174 struct bnxt_tx_ring_info *txr;
3175
3176 if (!bnapi)
3177 continue;
3178
3179 cpr = &bnapi->cp_ring;
3180 cpr->cp_raw_cons = 0;
3181
Michael Chanb6ab4b02016-01-02 23:44:59 -05003182 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003183 if (txr) {
3184 txr->tx_prod = 0;
3185 txr->tx_cons = 0;
3186 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003187
Michael Chanb6ab4b02016-01-02 23:44:59 -05003188 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003189 if (rxr) {
3190 rxr->rx_prod = 0;
3191 rxr->rx_agg_prod = 0;
3192 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003193 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003194 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003195 }
3196}
3197
3198static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3199{
3200#ifdef CONFIG_RFS_ACCEL
3201 int i;
3202
3203 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3204 * safe to delete the hash table.
3205 */
3206 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3207 struct hlist_head *head;
3208 struct hlist_node *tmp;
3209 struct bnxt_ntuple_filter *fltr;
3210
3211 head = &bp->ntp_fltr_hash_tbl[i];
3212 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3213 hlist_del(&fltr->hash);
3214 kfree(fltr);
3215 }
3216 }
3217 if (irq_reinit) {
3218 kfree(bp->ntp_fltr_bmap);
3219 bp->ntp_fltr_bmap = NULL;
3220 }
3221 bp->ntp_fltr_count = 0;
3222#endif
3223}
3224
3225static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3226{
3227#ifdef CONFIG_RFS_ACCEL
3228 int i, rc = 0;
3229
3230 if (!(bp->flags & BNXT_FLAG_RFS))
3231 return 0;
3232
3233 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3234 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3235
3236 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003237 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3238 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003239 GFP_KERNEL);
3240
3241 if (!bp->ntp_fltr_bmap)
3242 rc = -ENOMEM;
3243
3244 return rc;
3245#else
3246 return 0;
3247#endif
3248}
3249
3250static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3251{
3252 bnxt_free_vnic_attributes(bp);
3253 bnxt_free_tx_rings(bp);
3254 bnxt_free_rx_rings(bp);
3255 bnxt_free_cp_rings(bp);
3256 bnxt_free_ntp_fltrs(bp, irq_re_init);
3257 if (irq_re_init) {
3258 bnxt_free_stats(bp);
3259 bnxt_free_ring_grps(bp);
3260 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003261 kfree(bp->tx_ring_map);
3262 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003263 kfree(bp->tx_ring);
3264 bp->tx_ring = NULL;
3265 kfree(bp->rx_ring);
3266 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003267 kfree(bp->bnapi);
3268 bp->bnapi = NULL;
3269 } else {
3270 bnxt_clear_ring_indices(bp);
3271 }
3272}
3273
3274static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3275{
Michael Chan01657bc2016-01-02 23:45:03 -05003276 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003277 void *bnapi;
3278
3279 if (irq_re_init) {
3280 /* Allocate bnapi mem pointer array and mem block for
3281 * all queues
3282 */
3283 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3284 bp->cp_nr_rings);
3285 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3286 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3287 if (!bnapi)
3288 return -ENOMEM;
3289
3290 bp->bnapi = bnapi;
3291 bnapi += arr_size;
3292 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3293 bp->bnapi[i] = bnapi;
3294 bp->bnapi[i]->index = i;
3295 bp->bnapi[i]->bp = bp;
3296 }
3297
Michael Chanb6ab4b02016-01-02 23:44:59 -05003298 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3299 sizeof(struct bnxt_rx_ring_info),
3300 GFP_KERNEL);
3301 if (!bp->rx_ring)
3302 return -ENOMEM;
3303
3304 for (i = 0; i < bp->rx_nr_rings; i++) {
3305 bp->rx_ring[i].bnapi = bp->bnapi[i];
3306 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3307 }
3308
3309 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3310 sizeof(struct bnxt_tx_ring_info),
3311 GFP_KERNEL);
3312 if (!bp->tx_ring)
3313 return -ENOMEM;
3314
Michael Chana960dec2017-02-06 16:55:39 -05003315 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3316 GFP_KERNEL);
3317
3318 if (!bp->tx_ring_map)
3319 return -ENOMEM;
3320
Michael Chan01657bc2016-01-02 23:45:03 -05003321 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3322 j = 0;
3323 else
3324 j = bp->rx_nr_rings;
3325
3326 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3327 bp->tx_ring[i].bnapi = bp->bnapi[j];
3328 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003329 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003330 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003331 bp->tx_ring[i].txq_index = i -
3332 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003333 bp->bnapi[j]->tx_int = bnxt_tx_int;
3334 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003335 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003336 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3337 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003338 }
3339
Michael Chanc0c050c2015-10-22 16:01:17 -04003340 rc = bnxt_alloc_stats(bp);
3341 if (rc)
3342 goto alloc_mem_err;
3343
3344 rc = bnxt_alloc_ntp_fltrs(bp);
3345 if (rc)
3346 goto alloc_mem_err;
3347
3348 rc = bnxt_alloc_vnics(bp);
3349 if (rc)
3350 goto alloc_mem_err;
3351 }
3352
3353 bnxt_init_ring_struct(bp);
3354
3355 rc = bnxt_alloc_rx_rings(bp);
3356 if (rc)
3357 goto alloc_mem_err;
3358
3359 rc = bnxt_alloc_tx_rings(bp);
3360 if (rc)
3361 goto alloc_mem_err;
3362
3363 rc = bnxt_alloc_cp_rings(bp);
3364 if (rc)
3365 goto alloc_mem_err;
3366
3367 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3368 BNXT_VNIC_UCAST_FLAG;
3369 rc = bnxt_alloc_vnic_attributes(bp);
3370 if (rc)
3371 goto alloc_mem_err;
3372 return 0;
3373
3374alloc_mem_err:
3375 bnxt_free_mem(bp, true);
3376 return rc;
3377}
3378
Michael Chan9d8bc092016-12-29 12:13:33 -05003379static void bnxt_disable_int(struct bnxt *bp)
3380{
3381 int i;
3382
3383 if (!bp->bnapi)
3384 return;
3385
3386 for (i = 0; i < bp->cp_nr_rings; i++) {
3387 struct bnxt_napi *bnapi = bp->bnapi[i];
3388 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003389 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003390
Michael Chandaf1f1e2017-02-20 19:25:17 -05003391 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3392 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003393 }
3394}
3395
Michael Chane5811b82018-03-31 13:54:18 -04003396static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3397{
3398 struct bnxt_napi *bnapi = bp->bnapi[n];
3399 struct bnxt_cp_ring_info *cpr;
3400
3401 cpr = &bnapi->cp_ring;
3402 return cpr->cp_ring_struct.map_idx;
3403}
3404
Michael Chan9d8bc092016-12-29 12:13:33 -05003405static void bnxt_disable_int_sync(struct bnxt *bp)
3406{
3407 int i;
3408
3409 atomic_inc(&bp->intr_sem);
3410
3411 bnxt_disable_int(bp);
Michael Chane5811b82018-03-31 13:54:18 -04003412 for (i = 0; i < bp->cp_nr_rings; i++) {
3413 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3414
3415 synchronize_irq(bp->irq_tbl[map_idx].vector);
3416 }
Michael Chan9d8bc092016-12-29 12:13:33 -05003417}
3418
3419static void bnxt_enable_int(struct bnxt *bp)
3420{
3421 int i;
3422
3423 atomic_set(&bp->intr_sem, 0);
3424 for (i = 0; i < bp->cp_nr_rings; i++) {
3425 struct bnxt_napi *bnapi = bp->bnapi[i];
3426 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3427
3428 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3429 }
3430}
3431
Michael Chanc0c050c2015-10-22 16:01:17 -04003432void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3433 u16 cmpl_ring, u16 target_id)
3434{
Michael Chana8643e12016-02-26 04:00:05 -05003435 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003436
Michael Chana8643e12016-02-26 04:00:05 -05003437 req->req_type = cpu_to_le16(req_type);
3438 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3439 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003440 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3441}
3442
Michael Chanfbfbc482016-02-26 04:00:07 -05003443static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3444 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003445{
Michael Chana11fa2b2016-05-15 03:04:47 -04003446 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003447 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003448 u32 *data = msg;
Michael Chan845adfe2018-03-31 13:54:15 -04003449 __le32 *resp_len;
3450 u8 *valid;
Michael Chanc0c050c2015-10-22 16:01:17 -04003451 u16 cp_ring_id, len = 0;
3452 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003453 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Vasundhara Volamebd58182017-12-01 03:13:05 -05003454 struct hwrm_short_input short_input = {0};
Michael Chanc0c050c2015-10-22 16:01:17 -04003455
Michael Chana8643e12016-02-26 04:00:05 -05003456 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003457 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003458 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003459 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3460
Deepak Khungare605db82017-05-29 19:06:04 -04003461 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3462 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003463
3464 memcpy(short_cmd_req, req, msg_len);
3465 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3466 msg_len);
3467
3468 short_input.req_type = req->req_type;
3469 short_input.signature =
3470 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3471 short_input.size = cpu_to_le16(msg_len);
3472 short_input.req_addr =
3473 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3474
3475 data = (u32 *)&short_input;
3476 msg_len = sizeof(short_input);
3477
3478 /* Sync memory write before updating doorbell */
3479 wmb();
3480
3481 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3482 }
3483
Michael Chanc0c050c2015-10-22 16:01:17 -04003484 /* Write request msg to hwrm channel */
3485 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3486
Deepak Khungare605db82017-05-29 19:06:04 -04003487 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003488 writel(0, bp->bar0 + i);
3489
Michael Chanc0c050c2015-10-22 16:01:17 -04003490 /* currently supports only one outstanding message */
3491 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003492 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003493
3494 /* Ring channel doorbell */
3495 writel(1, bp->bar0 + 0x100);
3496
Michael Chanff4fe812016-02-26 04:00:04 -05003497 if (!timeout)
3498 timeout = DFLT_HWRM_CMD_TIMEOUT;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003499 /* convert timeout to usec */
3500 timeout *= 1000;
Michael Chanff4fe812016-02-26 04:00:04 -05003501
Michael Chanc0c050c2015-10-22 16:01:17 -04003502 i = 0;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003503 /* Short timeout for the first few iterations:
3504 * number of loops = number of loops for short timeout +
3505 * number of loops for standard timeout.
3506 */
3507 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3508 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3509 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
Michael Chan845adfe2018-03-31 13:54:15 -04003510 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chanc0c050c2015-10-22 16:01:17 -04003511 if (intr_process) {
3512 /* Wait until hwrm response cmpl interrupt is processed */
3513 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003514 i++ < tmo_count) {
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003515 /* on first few passes, just barely sleep */
3516 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3517 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3518 HWRM_SHORT_MAX_TIMEOUT);
3519 else
3520 usleep_range(HWRM_MIN_TIMEOUT,
3521 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003522 }
3523
3524 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3525 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003526 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003527 return -1;
3528 }
Michael Chan845adfe2018-03-31 13:54:15 -04003529 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3530 HWRM_RESP_LEN_SFT;
3531 valid = bp->hwrm_cmd_resp_addr + len - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003532 } else {
Michael Chancc559c12018-05-08 03:18:38 -04003533 int j;
3534
Michael Chanc0c050c2015-10-22 16:01:17 -04003535 /* Check if response len is updated */
Michael Chana11fa2b2016-05-15 03:04:47 -04003536 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003537 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3538 HWRM_RESP_LEN_SFT;
3539 if (len)
3540 break;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003541 /* on first few passes, just barely sleep */
3542 if (i < DFLT_HWRM_CMD_TIMEOUT)
3543 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3544 HWRM_SHORT_MAX_TIMEOUT);
3545 else
3546 usleep_range(HWRM_MIN_TIMEOUT,
3547 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003548 }
3549
Michael Chana11fa2b2016-05-15 03:04:47 -04003550 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003551 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003552 HWRM_TOTAL_TIMEOUT(i),
3553 le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003554 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003555 return -1;
3556 }
3557
Michael Chan845adfe2018-03-31 13:54:15 -04003558 /* Last byte of resp contains valid bit */
3559 valid = bp->hwrm_cmd_resp_addr + len - 1;
Michael Chancc559c12018-05-08 03:18:38 -04003560 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
Michael Chan845adfe2018-03-31 13:54:15 -04003561 /* make sure we read from updated DMA memory */
3562 dma_rmb();
3563 if (*valid)
Michael Chanc0c050c2015-10-22 16:01:17 -04003564 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003565 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003566 }
3567
Michael Chancc559c12018-05-08 03:18:38 -04003568 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003569 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003570 HWRM_TOTAL_TIMEOUT(i),
3571 le16_to_cpu(req->req_type),
Michael Chana8643e12016-02-26 04:00:05 -05003572 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003573 return -1;
3574 }
3575 }
3576
Michael Chan845adfe2018-03-31 13:54:15 -04003577 /* Zero valid bit for compatibility. Valid bit in an older spec
3578 * may become a new field in a newer spec. We must make sure that
3579 * a new field not implemented by old spec will read zero.
3580 */
3581 *valid = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003582 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003583 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003584 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3585 le16_to_cpu(resp->req_type),
3586 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003587 return rc;
3588}
3589
3590int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3591{
3592 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003593}
3594
Michael Chancc72f3b2017-10-13 21:09:33 -04003595int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3596 int timeout)
3597{
3598 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3599}
3600
Michael Chanc0c050c2015-10-22 16:01:17 -04003601int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3602{
3603 int rc;
3604
3605 mutex_lock(&bp->hwrm_cmd_lock);
3606 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3607 mutex_unlock(&bp->hwrm_cmd_lock);
3608 return rc;
3609}
3610
Michael Chan90e209212016-02-26 04:00:08 -05003611int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3612 int timeout)
3613{
3614 int rc;
3615
3616 mutex_lock(&bp->hwrm_cmd_lock);
3617 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3618 mutex_unlock(&bp->hwrm_cmd_lock);
3619 return rc;
3620}
3621
Michael Chana1653b12016-12-07 00:26:20 -05003622int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3623 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003624{
3625 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003626 DECLARE_BITMAP(async_events_bmap, 256);
3627 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003628 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003629
3630 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3631
3632 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003633 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003634
Michael Chan25be8622016-04-05 14:09:00 -04003635 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3636 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3637 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3638
Michael Chana1653b12016-12-07 00:26:20 -05003639 if (bmap && bmap_size) {
3640 for (i = 0; i < bmap_size; i++) {
3641 if (test_bit(i, bmap))
3642 __set_bit(i, async_events_bmap);
3643 }
3644 }
3645
Michael Chan25be8622016-04-05 14:09:00 -04003646 for (i = 0; i < 8; i++)
3647 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3648
Michael Chana1653b12016-12-07 00:26:20 -05003649 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3650}
3651
3652static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3653{
3654 struct hwrm_func_drv_rgtr_input req = {0};
3655
3656 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3657
3658 req.enables =
3659 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3660 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3661
Michael Chan11f15ed2016-04-05 14:08:55 -04003662 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chand4f52de02018-03-31 13:54:06 -04003663 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
3664 req.ver_maj_8b = DRV_VER_MAJ;
3665 req.ver_min_8b = DRV_VER_MIN;
3666 req.ver_upd_8b = DRV_VER_UPD;
3667 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
3668 req.ver_min = cpu_to_le16(DRV_VER_MIN);
3669 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003670
3671 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003672 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003673 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003674
Michael Chan9b0436c2017-07-11 13:05:36 -04003675 memset(data, 0, sizeof(data));
3676 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3677 u16 cmd = bnxt_vf_req_snif[i];
3678 unsigned int bit, idx;
3679
3680 idx = cmd / 32;
3681 bit = cmd % 32;
3682 data[idx] |= 1 << bit;
3683 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003684
Michael Chande68f5de2015-12-09 19:35:41 -05003685 for (i = 0; i < 8; i++)
3686 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3687
Michael Chanc0c050c2015-10-22 16:01:17 -04003688 req.enables |=
3689 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3690 }
3691
3692 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3693}
3694
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003695static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3696{
3697 struct hwrm_func_drv_unrgtr_input req = {0};
3698
3699 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3700 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3701}
3702
Michael Chanc0c050c2015-10-22 16:01:17 -04003703static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3704{
3705 u32 rc = 0;
3706 struct hwrm_tunnel_dst_port_free_input req = {0};
3707
3708 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3709 req.tunnel_type = tunnel_type;
3710
3711 switch (tunnel_type) {
3712 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3713 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3714 break;
3715 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3716 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3717 break;
3718 default:
3719 break;
3720 }
3721
3722 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3723 if (rc)
3724 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3725 rc);
3726 return rc;
3727}
3728
3729static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3730 u8 tunnel_type)
3731{
3732 u32 rc = 0;
3733 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3734 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3735
3736 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3737
3738 req.tunnel_type = tunnel_type;
3739 req.tunnel_dst_port_val = port;
3740
3741 mutex_lock(&bp->hwrm_cmd_lock);
3742 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3743 if (rc) {
3744 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3745 rc);
3746 goto err_out;
3747 }
3748
Christophe Jaillet57aac712016-11-22 06:14:40 +01003749 switch (tunnel_type) {
3750 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003751 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003752 break;
3753 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003754 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003755 break;
3756 default:
3757 break;
3758 }
3759
Michael Chanc0c050c2015-10-22 16:01:17 -04003760err_out:
3761 mutex_unlock(&bp->hwrm_cmd_lock);
3762 return rc;
3763}
3764
3765static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3766{
3767 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3768 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3769
3770 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003771 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003772
3773 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3774 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3775 req.mask = cpu_to_le32(vnic->rx_mask);
3776 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3777}
3778
3779#ifdef CONFIG_RFS_ACCEL
3780static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3781 struct bnxt_ntuple_filter *fltr)
3782{
3783 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3784
3785 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3786 req.ntuple_filter_id = fltr->filter_id;
3787 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3788}
3789
3790#define BNXT_NTP_FLTR_FLAGS \
3791 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3792 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3793 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3794 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3795 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3796 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3797 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3798 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3799 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3800 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3801 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3802 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3803 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003804 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003805
Michael Chan61aad722017-02-12 19:18:14 -05003806#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3807 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3808
Michael Chanc0c050c2015-10-22 16:01:17 -04003809static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3810 struct bnxt_ntuple_filter *fltr)
3811{
3812 int rc = 0;
3813 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3814 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3815 bp->hwrm_cmd_resp_addr;
3816 struct flow_keys *keys = &fltr->fkeys;
3817 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3818
3819 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003820 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003821
3822 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3823
3824 req.ethertype = htons(ETH_P_IP);
3825 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003826 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003827 req.ip_protocol = keys->basic.ip_proto;
3828
Michael Chandda0e742016-12-29 12:13:40 -05003829 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3830 int i;
3831
3832 req.ethertype = htons(ETH_P_IPV6);
3833 req.ip_addr_type =
3834 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3835 *(struct in6_addr *)&req.src_ipaddr[0] =
3836 keys->addrs.v6addrs.src;
3837 *(struct in6_addr *)&req.dst_ipaddr[0] =
3838 keys->addrs.v6addrs.dst;
3839 for (i = 0; i < 4; i++) {
3840 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3841 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3842 }
3843 } else {
3844 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3845 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3846 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3847 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3848 }
Michael Chan61aad722017-02-12 19:18:14 -05003849 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3850 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3851 req.tunnel_type =
3852 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3853 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003854
3855 req.src_port = keys->ports.src;
3856 req.src_port_mask = cpu_to_be16(0xffff);
3857 req.dst_port = keys->ports.dst;
3858 req.dst_port_mask = cpu_to_be16(0xffff);
3859
Michael Chanc1935542015-12-27 18:19:28 -05003860 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003861 mutex_lock(&bp->hwrm_cmd_lock);
3862 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3863 if (!rc)
3864 fltr->filter_id = resp->ntuple_filter_id;
3865 mutex_unlock(&bp->hwrm_cmd_lock);
3866 return rc;
3867}
3868#endif
3869
3870static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3871 u8 *mac_addr)
3872{
3873 u32 rc = 0;
3874 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3875 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3876
3877 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003878 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3879 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3880 req.flags |=
3881 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003882 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003883 req.enables =
3884 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003885 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003886 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3887 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3888 req.l2_addr_mask[0] = 0xff;
3889 req.l2_addr_mask[1] = 0xff;
3890 req.l2_addr_mask[2] = 0xff;
3891 req.l2_addr_mask[3] = 0xff;
3892 req.l2_addr_mask[4] = 0xff;
3893 req.l2_addr_mask[5] = 0xff;
3894
3895 mutex_lock(&bp->hwrm_cmd_lock);
3896 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3897 if (!rc)
3898 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3899 resp->l2_filter_id;
3900 mutex_unlock(&bp->hwrm_cmd_lock);
3901 return rc;
3902}
3903
3904static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3905{
3906 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3907 int rc = 0;
3908
3909 /* Any associated ntuple filters will also be cleared by firmware. */
3910 mutex_lock(&bp->hwrm_cmd_lock);
3911 for (i = 0; i < num_of_vnics; i++) {
3912 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3913
3914 for (j = 0; j < vnic->uc_filter_count; j++) {
3915 struct hwrm_cfa_l2_filter_free_input req = {0};
3916
3917 bnxt_hwrm_cmd_hdr_init(bp, &req,
3918 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3919
3920 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3921
3922 rc = _hwrm_send_message(bp, &req, sizeof(req),
3923 HWRM_CMD_TIMEOUT);
3924 }
3925 vnic->uc_filter_count = 0;
3926 }
3927 mutex_unlock(&bp->hwrm_cmd_lock);
3928
3929 return rc;
3930}
3931
3932static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3933{
3934 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3935 struct hwrm_vnic_tpa_cfg_input req = {0};
3936
Michael Chan3c4fe802018-03-09 23:46:10 -05003937 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
3938 return 0;
3939
Michael Chanc0c050c2015-10-22 16:01:17 -04003940 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3941
3942 if (tpa_flags) {
3943 u16 mss = bp->dev->mtu - 40;
3944 u32 nsegs, n, segs = 0, flags;
3945
3946 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3947 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3948 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3949 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3950 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3951 if (tpa_flags & BNXT_FLAG_GRO)
3952 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3953
3954 req.flags = cpu_to_le32(flags);
3955
3956 req.enables =
3957 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003958 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3959 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003960
3961 /* Number of segs are log2 units, and first packet is not
3962 * included as part of this units.
3963 */
Michael Chan2839f282016-04-25 02:30:50 -04003964 if (mss <= BNXT_RX_PAGE_SIZE) {
3965 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003966 nsegs = (MAX_SKB_FRAGS - 1) * n;
3967 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003968 n = mss / BNXT_RX_PAGE_SIZE;
3969 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003970 n++;
3971 nsegs = (MAX_SKB_FRAGS - n) / n;
3972 }
3973
3974 segs = ilog2(nsegs);
3975 req.max_agg_segs = cpu_to_le16(segs);
3976 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003977
3978 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003979 }
3980 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3981
3982 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3983}
3984
3985static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3986{
3987 u32 i, j, max_rings;
3988 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3989 struct hwrm_vnic_rss_cfg_input req = {0};
3990
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003991 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003992 return 0;
3993
3994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3995 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003996 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003997 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3998 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3999 max_rings = bp->rx_nr_rings - 1;
4000 else
4001 max_rings = bp->rx_nr_rings;
4002 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04004003 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004004 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004005
4006 /* Fill the RSS indirection table with ring group ids */
4007 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4008 if (j == max_rings)
4009 j = 0;
4010 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4011 }
4012
4013 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4014 req.hash_key_tbl_addr =
4015 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4016 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004017 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004018 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4019}
4020
4021static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4022{
4023 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4024 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4025
4026 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4027 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4028 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4029 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4030 req.enables =
4031 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4032 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4033 /* thresholds not implemented in firmware yet */
4034 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4035 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4036 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4037 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4038}
4039
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004040static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4041 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004042{
4043 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4044
4045 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4046 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004047 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004048
4049 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004050 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004051}
4052
4053static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4054{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004055 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04004056
4057 for (i = 0; i < bp->nr_vnics; i++) {
4058 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4059
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004060 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4061 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4062 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4063 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004064 }
4065 bp->rsscos_nr_ctxs = 0;
4066}
4067
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004068static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004069{
4070 int rc;
4071 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4072 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4073 bp->hwrm_cmd_resp_addr;
4074
4075 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4076 -1);
4077
4078 mutex_lock(&bp->hwrm_cmd_lock);
4079 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4080 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004081 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04004082 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4083 mutex_unlock(&bp->hwrm_cmd_lock);
4084
4085 return rc;
4086}
4087
Michael Chanabe93ad2018-03-31 13:54:08 -04004088static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4089{
4090 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4091 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4092 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4093}
4094
Michael Chana588e452016-12-07 00:26:21 -05004095int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04004096{
Michael Chanb81a90d2016-01-02 23:45:01 -05004097 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004098 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4099 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04004100 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004101
4102 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004103
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004104 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4105 /* Only RSS support for now TBD: COS & LB */
4106 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4107 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4108 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4109 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05004110 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4111 req.rss_rule =
4112 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4113 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4114 VNIC_CFG_REQ_ENABLES_MRU);
4115 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004116 } else {
4117 req.rss_rule = cpu_to_le16(0xffff);
4118 }
4119
4120 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4121 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004122 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4123 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4124 } else {
4125 req.cos_rule = cpu_to_le16(0xffff);
4126 }
4127
Michael Chanc0c050c2015-10-22 16:01:17 -04004128 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004129 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004130 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004131 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004132 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4133 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004134
Michael Chanb81a90d2016-01-02 23:45:01 -05004135 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004136 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4137 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4138
4139 req.lb_rule = cpu_to_le16(0xffff);
4140 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4141 VLAN_HLEN);
4142
Michael Chancf6645f2016-06-13 02:25:28 -04004143#ifdef CONFIG_BNXT_SRIOV
4144 if (BNXT_VF(bp))
4145 def_vlan = bp->vf.vlan;
4146#endif
4147 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04004148 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05004149 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
Michael Chanabe93ad2018-03-31 13:54:08 -04004150 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
Michael Chanc0c050c2015-10-22 16:01:17 -04004151
4152 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4153}
4154
4155static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4156{
4157 u32 rc = 0;
4158
4159 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4160 struct hwrm_vnic_free_input req = {0};
4161
4162 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4163 req.vnic_id =
4164 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4165
4166 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4167 if (rc)
4168 return rc;
4169 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4170 }
4171 return rc;
4172}
4173
4174static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4175{
4176 u16 i;
4177
4178 for (i = 0; i < bp->nr_vnics; i++)
4179 bnxt_hwrm_vnic_free_one(bp, i);
4180}
4181
Michael Chanb81a90d2016-01-02 23:45:01 -05004182static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4183 unsigned int start_rx_ring_idx,
4184 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004185{
Michael Chanb81a90d2016-01-02 23:45:01 -05004186 int rc = 0;
4187 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004188 struct hwrm_vnic_alloc_input req = {0};
4189 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4190
4191 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004192 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4193 grp_idx = bp->rx_ring[i].bnapi->index;
4194 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004195 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004196 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004197 break;
4198 }
4199 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05004200 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004201 }
4202
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004203 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4204 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004205 if (vnic_id == 0)
4206 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4207
4208 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4209
4210 mutex_lock(&bp->hwrm_cmd_lock);
4211 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4212 if (!rc)
4213 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4214 mutex_unlock(&bp->hwrm_cmd_lock);
4215 return rc;
4216}
4217
Michael Chan8fdefd62016-12-29 12:13:36 -05004218static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4219{
4220 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4221 struct hwrm_vnic_qcaps_input req = {0};
4222 int rc;
4223
4224 if (bp->hwrm_spec_code < 0x10600)
4225 return 0;
4226
4227 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4228 mutex_lock(&bp->hwrm_cmd_lock);
4229 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4230 if (!rc) {
Michael Chanabe93ad2018-03-31 13:54:08 -04004231 u32 flags = le32_to_cpu(resp->flags);
4232
4233 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)
Michael Chan8fdefd62016-12-29 12:13:36 -05004234 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
Michael Chanabe93ad2018-03-31 13:54:08 -04004235 if (flags &
4236 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4237 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
Michael Chan8fdefd62016-12-29 12:13:36 -05004238 }
4239 mutex_unlock(&bp->hwrm_cmd_lock);
4240 return rc;
4241}
4242
Michael Chanc0c050c2015-10-22 16:01:17 -04004243static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4244{
4245 u16 i;
4246 u32 rc = 0;
4247
4248 mutex_lock(&bp->hwrm_cmd_lock);
4249 for (i = 0; i < bp->rx_nr_rings; i++) {
4250 struct hwrm_ring_grp_alloc_input req = {0};
4251 struct hwrm_ring_grp_alloc_output *resp =
4252 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004253 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004254
4255 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4256
Michael Chanb81a90d2016-01-02 23:45:01 -05004257 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4258 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4259 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4260 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004261
4262 rc = _hwrm_send_message(bp, &req, sizeof(req),
4263 HWRM_CMD_TIMEOUT);
4264 if (rc)
4265 break;
4266
Michael Chanb81a90d2016-01-02 23:45:01 -05004267 bp->grp_info[grp_idx].fw_grp_id =
4268 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004269 }
4270 mutex_unlock(&bp->hwrm_cmd_lock);
4271 return rc;
4272}
4273
4274static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4275{
4276 u16 i;
4277 u32 rc = 0;
4278 struct hwrm_ring_grp_free_input req = {0};
4279
4280 if (!bp->grp_info)
4281 return 0;
4282
4283 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4284
4285 mutex_lock(&bp->hwrm_cmd_lock);
4286 for (i = 0; i < bp->cp_nr_rings; i++) {
4287 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4288 continue;
4289 req.ring_group_id =
4290 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4291
4292 rc = _hwrm_send_message(bp, &req, sizeof(req),
4293 HWRM_CMD_TIMEOUT);
4294 if (rc)
4295 break;
4296 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4297 }
4298 mutex_unlock(&bp->hwrm_cmd_lock);
4299 return rc;
4300}
4301
4302static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4303 struct bnxt_ring_struct *ring,
Michael Chan9899bb52018-03-31 13:54:16 -04004304 u32 ring_type, u32 map_index)
Michael Chanc0c050c2015-10-22 16:01:17 -04004305{
4306 int rc = 0, err = 0;
4307 struct hwrm_ring_alloc_input req = {0};
4308 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9899bb52018-03-31 13:54:16 -04004309 struct bnxt_ring_grp_info *grp_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04004310 u16 ring_id;
4311
4312 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4313
4314 req.enables = 0;
4315 if (ring->nr_pages > 1) {
4316 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4317 /* Page size is in log2 units */
4318 req.page_size = BNXT_PAGE_SHIFT;
4319 req.page_tbl_depth = 1;
4320 } else {
4321 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4322 }
4323 req.fbo = 0;
4324 /* Association of ring index with doorbell index and MSIX number */
4325 req.logical_id = cpu_to_le16(map_index);
4326
4327 switch (ring_type) {
4328 case HWRM_RING_ALLOC_TX:
4329 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4330 /* Association of transmit ring with completion ring */
Michael Chan9899bb52018-03-31 13:54:16 -04004331 grp_info = &bp->grp_info[ring->grp_idx];
4332 req.cmpl_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004333 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
Michael Chan9899bb52018-03-31 13:54:16 -04004334 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004335 req.queue_id = cpu_to_le16(ring->queue_id);
4336 break;
4337 case HWRM_RING_ALLOC_RX:
4338 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4339 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4340 break;
4341 case HWRM_RING_ALLOC_AGG:
4342 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4343 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4344 break;
4345 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004346 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004347 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4348 if (bp->flags & BNXT_FLAG_USING_MSIX)
4349 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4350 break;
4351 default:
4352 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4353 ring_type);
4354 return -1;
4355 }
4356
4357 mutex_lock(&bp->hwrm_cmd_lock);
4358 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4359 err = le16_to_cpu(resp->error_code);
4360 ring_id = le16_to_cpu(resp->ring_id);
4361 mutex_unlock(&bp->hwrm_cmd_lock);
4362
4363 if (rc || err) {
Michael Chan2727c882018-04-26 17:44:35 -04004364 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4365 ring_type, rc, err);
4366 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04004367 }
4368 ring->fw_ring_id = ring_id;
4369 return rc;
4370}
4371
Michael Chan486b5c22016-12-29 12:13:42 -05004372static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4373{
4374 int rc;
4375
4376 if (BNXT_PF(bp)) {
4377 struct hwrm_func_cfg_input req = {0};
4378
4379 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4380 req.fid = cpu_to_le16(0xffff);
4381 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4382 req.async_event_cr = cpu_to_le16(idx);
4383 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4384 } else {
4385 struct hwrm_func_vf_cfg_input req = {0};
4386
4387 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4388 req.enables =
4389 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4390 req.async_event_cr = cpu_to_le16(idx);
4391 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4392 }
4393 return rc;
4394}
4395
Michael Chanc0c050c2015-10-22 16:01:17 -04004396static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4397{
4398 int i, rc = 0;
4399
Michael Chanedd0c2c2015-12-27 18:19:19 -05004400 for (i = 0; i < bp->cp_nr_rings; i++) {
4401 struct bnxt_napi *bnapi = bp->bnapi[i];
4402 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4403 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004404 u32 map_idx = ring->map_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004405
Michael Chan9899bb52018-03-31 13:54:16 -04004406 cpr->cp_doorbell = bp->bar1 + map_idx * 0x80;
4407 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL,
4408 map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004409 if (rc)
4410 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004411 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4412 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004413
4414 if (!i) {
4415 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4416 if (rc)
4417 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4418 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004419 }
4420
Michael Chanedd0c2c2015-12-27 18:19:19 -05004421 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004422 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004423 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004424 u32 map_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004425
Michael Chanb81a90d2016-01-02 23:45:01 -05004426 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
Michael Chan9899bb52018-03-31 13:54:16 -04004427 map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004428 if (rc)
4429 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004430 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004431 }
4432
Michael Chanedd0c2c2015-12-27 18:19:19 -05004433 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004434 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004435 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004436 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004437
Michael Chanb81a90d2016-01-02 23:45:01 -05004438 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
Michael Chan9899bb52018-03-31 13:54:16 -04004439 map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004440 if (rc)
4441 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004442 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004443 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004444 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004445 }
4446
4447 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4448 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004449 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004450 struct bnxt_ring_struct *ring =
4451 &rxr->rx_agg_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004452 u32 grp_idx = ring->grp_idx;
Michael Chanb81a90d2016-01-02 23:45:01 -05004453 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004454
4455 rc = hwrm_ring_alloc_send_msg(bp, ring,
4456 HWRM_RING_ALLOC_AGG,
Michael Chan9899bb52018-03-31 13:54:16 -04004457 map_idx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004458 if (rc)
4459 goto err_out;
4460
Michael Chanb81a90d2016-01-02 23:45:01 -05004461 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004462 writel(DB_KEY_RX | rxr->rx_agg_prod,
4463 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004464 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004465 }
4466 }
4467err_out:
4468 return rc;
4469}
4470
4471static int hwrm_ring_free_send_msg(struct bnxt *bp,
4472 struct bnxt_ring_struct *ring,
4473 u32 ring_type, int cmpl_ring_id)
4474{
4475 int rc;
4476 struct hwrm_ring_free_input req = {0};
4477 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4478 u16 error_code;
4479
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004480 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004481 req.ring_type = ring_type;
4482 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4483
4484 mutex_lock(&bp->hwrm_cmd_lock);
4485 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4486 error_code = le16_to_cpu(resp->error_code);
4487 mutex_unlock(&bp->hwrm_cmd_lock);
4488
4489 if (rc || error_code) {
Michael Chan2727c882018-04-26 17:44:35 -04004490 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
4491 ring_type, rc, error_code);
4492 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04004493 }
4494 return 0;
4495}
4496
Michael Chanedd0c2c2015-12-27 18:19:19 -05004497static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004498{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004499 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004500
4501 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004502 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004503
Michael Chanedd0c2c2015-12-27 18:19:19 -05004504 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004505 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004506 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004507 u32 grp_idx = txr->bnapi->index;
4508 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004509
Michael Chanedd0c2c2015-12-27 18:19:19 -05004510 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4511 hwrm_ring_free_send_msg(bp, ring,
4512 RING_FREE_REQ_RING_TYPE_TX,
4513 close_path ? cmpl_ring_id :
4514 INVALID_HW_RING_ID);
4515 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004516 }
4517 }
4518
Michael Chanedd0c2c2015-12-27 18:19:19 -05004519 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004520 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004521 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004522 u32 grp_idx = rxr->bnapi->index;
4523 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004524
Michael Chanedd0c2c2015-12-27 18:19:19 -05004525 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4526 hwrm_ring_free_send_msg(bp, ring,
4527 RING_FREE_REQ_RING_TYPE_RX,
4528 close_path ? cmpl_ring_id :
4529 INVALID_HW_RING_ID);
4530 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004531 bp->grp_info[grp_idx].rx_fw_ring_id =
4532 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004533 }
4534 }
4535
Michael Chanedd0c2c2015-12-27 18:19:19 -05004536 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004537 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004538 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004539 u32 grp_idx = rxr->bnapi->index;
4540 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004541
Michael Chanedd0c2c2015-12-27 18:19:19 -05004542 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4543 hwrm_ring_free_send_msg(bp, ring,
4544 RING_FREE_REQ_RING_TYPE_RX,
4545 close_path ? cmpl_ring_id :
4546 INVALID_HW_RING_ID);
4547 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004548 bp->grp_info[grp_idx].agg_fw_ring_id =
4549 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004550 }
4551 }
4552
Michael Chan9d8bc092016-12-29 12:13:33 -05004553 /* The completion rings are about to be freed. After that the
4554 * IRQ doorbell will not work anymore. So we need to disable
4555 * IRQ here.
4556 */
4557 bnxt_disable_int_sync(bp);
4558
Michael Chanedd0c2c2015-12-27 18:19:19 -05004559 for (i = 0; i < bp->cp_nr_rings; i++) {
4560 struct bnxt_napi *bnapi = bp->bnapi[i];
4561 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4562 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004563
Michael Chanedd0c2c2015-12-27 18:19:19 -05004564 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4565 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004566 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004567 INVALID_HW_RING_ID);
4568 ring->fw_ring_id = INVALID_HW_RING_ID;
4569 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004570 }
4571 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004572}
4573
Michael Chan674f50a2018-01-17 03:21:09 -05004574static int bnxt_hwrm_get_rings(struct bnxt *bp)
4575{
4576 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4577 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4578 struct hwrm_func_qcfg_input req = {0};
4579 int rc;
4580
4581 if (bp->hwrm_spec_code < 0x10601)
4582 return 0;
4583
4584 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4585 req.fid = cpu_to_le16(0xffff);
4586 mutex_lock(&bp->hwrm_cmd_lock);
4587 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4588 if (rc) {
4589 mutex_unlock(&bp->hwrm_cmd_lock);
4590 return -EIO;
4591 }
4592
4593 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4594 if (bp->flags & BNXT_FLAG_NEW_RM) {
4595 u16 cp, stats;
4596
4597 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
4598 hw_resc->resv_hw_ring_grps =
4599 le32_to_cpu(resp->alloc_hw_ring_grps);
4600 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
4601 cp = le16_to_cpu(resp->alloc_cmpl_rings);
4602 stats = le16_to_cpu(resp->alloc_stat_ctx);
4603 cp = min_t(u16, cp, stats);
4604 hw_resc->resv_cp_rings = cp;
4605 }
4606 mutex_unlock(&bp->hwrm_cmd_lock);
4607 return 0;
4608}
4609
Michael Chan391be5c2016-12-29 12:13:41 -05004610/* Caller must hold bp->hwrm_cmd_lock */
4611int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4612{
4613 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4614 struct hwrm_func_qcfg_input req = {0};
4615 int rc;
4616
4617 if (bp->hwrm_spec_code < 0x10601)
4618 return 0;
4619
4620 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4621 req.fid = cpu_to_le16(fid);
4622 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4623 if (!rc)
4624 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4625
4626 return rc;
4627}
4628
Michael Chan4ed50ef2018-03-09 23:46:03 -05004629static void
4630__bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
4631 int tx_rings, int rx_rings, int ring_grps,
4632 int cp_rings, int vnics)
Michael Chan391be5c2016-12-29 12:13:41 -05004633{
Michael Chan674f50a2018-01-17 03:21:09 -05004634 u32 enables = 0;
Michael Chan391be5c2016-12-29 12:13:41 -05004635
Michael Chan4ed50ef2018-03-09 23:46:03 -05004636 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
4637 req->fid = cpu_to_le16(0xffff);
Michael Chan674f50a2018-01-17 03:21:09 -05004638 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
Michael Chan4ed50ef2018-03-09 23:46:03 -05004639 req->num_tx_rings = cpu_to_le16(tx_rings);
Michael Chan674f50a2018-01-17 03:21:09 -05004640 if (bp->flags & BNXT_FLAG_NEW_RM) {
4641 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4642 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4643 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4644 enables |= ring_grps ?
4645 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4646 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4647
Michael Chan4ed50ef2018-03-09 23:46:03 -05004648 req->num_rx_rings = cpu_to_le16(rx_rings);
4649 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4650 req->num_cmpl_rings = cpu_to_le16(cp_rings);
4651 req->num_stat_ctxs = req->num_cmpl_rings;
4652 req->num_vnics = cpu_to_le16(vnics);
Michael Chan674f50a2018-01-17 03:21:09 -05004653 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05004654 req->enables = cpu_to_le32(enables);
4655}
4656
4657static void
4658__bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
4659 struct hwrm_func_vf_cfg_input *req, int tx_rings,
4660 int rx_rings, int ring_grps, int cp_rings,
4661 int vnics)
4662{
4663 u32 enables = 0;
4664
4665 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
4666 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
4667 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4668 enables |= cp_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4669 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4670 enables |= ring_grps ? FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4671 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4672
4673 req->num_tx_rings = cpu_to_le16(tx_rings);
4674 req->num_rx_rings = cpu_to_le16(rx_rings);
4675 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4676 req->num_cmpl_rings = cpu_to_le16(cp_rings);
4677 req->num_stat_ctxs = req->num_cmpl_rings;
4678 req->num_vnics = cpu_to_le16(vnics);
4679
4680 req->enables = cpu_to_le32(enables);
4681}
4682
4683static int
4684bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4685 int ring_grps, int cp_rings, int vnics)
4686{
4687 struct hwrm_func_cfg_input req = {0};
4688 int rc;
4689
4690 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4691 cp_rings, vnics);
4692 if (!req.enables)
Michael Chan674f50a2018-01-17 03:21:09 -05004693 return 0;
4694
Michael Chan674f50a2018-01-17 03:21:09 -05004695 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4696 if (rc)
4697 return -ENOMEM;
4698
4699 if (bp->hwrm_spec_code < 0x10601)
4700 bp->hw_resc.resv_tx_rings = tx_rings;
4701
4702 rc = bnxt_hwrm_get_rings(bp);
4703 return rc;
4704}
4705
4706static int
4707bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4708 int ring_grps, int cp_rings, int vnics)
4709{
4710 struct hwrm_func_vf_cfg_input req = {0};
Michael Chan674f50a2018-01-17 03:21:09 -05004711 int rc;
4712
4713 if (!(bp->flags & BNXT_FLAG_NEW_RM)) {
4714 bp->hw_resc.resv_tx_rings = tx_rings;
4715 return 0;
4716 }
4717
Michael Chan4ed50ef2018-03-09 23:46:03 -05004718 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4719 cp_rings, vnics);
Michael Chan86c33802018-04-26 17:44:43 -04004720 req.enables |= cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
4721 FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS);
4722 req.num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
4723 req.num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
Michael Chan674f50a2018-01-17 03:21:09 -05004724 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4725 if (rc)
4726 return -ENOMEM;
4727
4728 rc = bnxt_hwrm_get_rings(bp);
4729 return rc;
4730}
4731
4732static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
4733 int cp, int vnic)
4734{
4735 if (BNXT_PF(bp))
4736 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, vnic);
4737 else
4738 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, vnic);
4739}
4740
Michael Chan08654eb2018-03-31 13:54:17 -04004741static int bnxt_cp_rings_in_use(struct bnxt *bp)
4742{
4743 int cp = bp->cp_nr_rings;
4744 int ulp_msix, ulp_base;
4745
4746 ulp_msix = bnxt_get_ulp_msix_num(bp);
4747 if (ulp_msix) {
4748 ulp_base = bnxt_get_ulp_msix_base(bp);
4749 cp += ulp_msix;
4750 if ((ulp_base + ulp_msix) > cp)
4751 cp = ulp_base + ulp_msix;
4752 }
4753 return cp;
4754}
4755
Michael Chan4e41dc52018-03-31 13:54:19 -04004756static bool bnxt_need_reserve_rings(struct bnxt *bp)
4757{
4758 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanfbcfc8e2018-03-31 13:54:20 -04004759 int cp = bnxt_cp_rings_in_use(bp);
Michael Chan4e41dc52018-03-31 13:54:19 -04004760 int rx = bp->rx_nr_rings;
4761 int vnic = 1, grp = rx;
4762
4763 if (bp->hwrm_spec_code < 0x10601)
4764 return false;
4765
4766 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
4767 return true;
4768
4769 if (bp->flags & BNXT_FLAG_RFS)
4770 vnic = rx + 1;
4771 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4772 rx <<= 1;
4773 if ((bp->flags & BNXT_FLAG_NEW_RM) &&
4774 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
4775 hw_resc->resv_hw_ring_grps != grp || hw_resc->resv_vnics != vnic))
4776 return true;
4777 return false;
4778}
4779
Michael Chan674f50a2018-01-17 03:21:09 -05004780static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4781 bool shared);
4782
4783static int __bnxt_reserve_rings(struct bnxt *bp)
4784{
4785 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanfbcfc8e2018-03-31 13:54:20 -04004786 int cp = bnxt_cp_rings_in_use(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05004787 int tx = bp->tx_nr_rings;
4788 int rx = bp->rx_nr_rings;
Michael Chan674f50a2018-01-17 03:21:09 -05004789 int grp, rx_rings, rc;
4790 bool sh = false;
4791 int vnic = 1;
4792
Michael Chan4e41dc52018-03-31 13:54:19 -04004793 if (!bnxt_need_reserve_rings(bp))
Michael Chan391be5c2016-12-29 12:13:41 -05004794 return 0;
4795
Michael Chan674f50a2018-01-17 03:21:09 -05004796 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4797 sh = true;
4798 if (bp->flags & BNXT_FLAG_RFS)
4799 vnic = rx + 1;
4800 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4801 rx <<= 1;
Michael Chan674f50a2018-01-17 03:21:09 -05004802 grp = bp->rx_nr_rings;
Michael Chan391be5c2016-12-29 12:13:41 -05004803
Michael Chan674f50a2018-01-17 03:21:09 -05004804 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, vnic);
Michael Chan391be5c2016-12-29 12:13:41 -05004805 if (rc)
4806 return rc;
4807
Michael Chan674f50a2018-01-17 03:21:09 -05004808 tx = hw_resc->resv_tx_rings;
4809 if (bp->flags & BNXT_FLAG_NEW_RM) {
4810 rx = hw_resc->resv_rx_rings;
4811 cp = hw_resc->resv_cp_rings;
4812 grp = hw_resc->resv_hw_ring_grps;
4813 vnic = hw_resc->resv_vnics;
4814 }
4815
4816 rx_rings = rx;
4817 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4818 if (rx >= 2) {
4819 rx_rings = rx >> 1;
4820 } else {
4821 if (netif_running(bp->dev))
4822 return -ENOMEM;
4823
4824 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4825 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4826 bp->dev->hw_features &= ~NETIF_F_LRO;
4827 bp->dev->features &= ~NETIF_F_LRO;
4828 bnxt_set_ring_params(bp);
4829 }
4830 }
4831 rx_rings = min_t(int, rx_rings, grp);
4832 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
4833 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4834 rx = rx_rings << 1;
4835 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
4836 bp->tx_nr_rings = tx;
4837 bp->rx_nr_rings = rx_rings;
4838 bp->cp_nr_rings = cp;
4839
4840 if (!tx || !rx || !cp || !grp || !vnic)
4841 return -ENOMEM;
4842
Michael Chan391be5c2016-12-29 12:13:41 -05004843 return rc;
4844}
4845
Michael Chan8f23d632018-01-17 03:21:12 -05004846static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004847 int ring_grps, int cp_rings, int vnics)
Michael Chan98fdbe72017-08-28 13:40:26 -04004848{
Michael Chan8f23d632018-01-17 03:21:12 -05004849 struct hwrm_func_vf_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004850 u32 flags;
Michael Chan98fdbe72017-08-28 13:40:26 -04004851 int rc;
4852
Michael Chan8f23d632018-01-17 03:21:12 -05004853 if (!(bp->flags & BNXT_FLAG_NEW_RM))
Michael Chan98fdbe72017-08-28 13:40:26 -04004854 return 0;
4855
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004856 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4857 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05004858 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
4859 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4860 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4861 FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4862 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4863 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
Michael Chan98fdbe72017-08-28 13:40:26 -04004864
Michael Chan8f23d632018-01-17 03:21:12 -05004865 req.flags = cpu_to_le32(flags);
Michael Chan98fdbe72017-08-28 13:40:26 -04004866 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4867 if (rc)
4868 return -ENOMEM;
4869 return 0;
4870}
4871
Michael Chan8f23d632018-01-17 03:21:12 -05004872static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004873 int ring_grps, int cp_rings, int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05004874{
4875 struct hwrm_func_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004876 u32 flags;
Michael Chan8f23d632018-01-17 03:21:12 -05004877 int rc;
4878
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004879 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4880 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05004881 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004882 if (bp->flags & BNXT_FLAG_NEW_RM)
Michael Chan8f23d632018-01-17 03:21:12 -05004883 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4884 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4885 FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4886 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4887 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004888
Michael Chan8f23d632018-01-17 03:21:12 -05004889 req.flags = cpu_to_le32(flags);
Michael Chan8f23d632018-01-17 03:21:12 -05004890 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4891 if (rc)
4892 return -ENOMEM;
4893 return 0;
4894}
4895
4896static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004897 int ring_grps, int cp_rings, int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05004898{
4899 if (bp->hwrm_spec_code < 0x10801)
4900 return 0;
4901
4902 if (BNXT_PF(bp))
4903 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004904 ring_grps, cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05004905
4906 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05004907 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05004908}
4909
Michael Chanf8503962017-10-26 11:51:28 -04004910static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
Michael Chanbb053f52016-02-26 04:00:02 -05004911 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4912{
Michael Chanf8503962017-10-26 11:51:28 -04004913 u16 val, tmr, max, flags;
4914
4915 max = hw_coal->bufs_per_record * 128;
4916 if (hw_coal->budget)
4917 max = hw_coal->bufs_per_record * hw_coal->budget;
4918
4919 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
4920 req->num_cmpl_aggr_int = cpu_to_le16(val);
Michael Chanb153cbc2017-11-03 03:32:39 -04004921
4922 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4923 val = min_t(u16, val, 63);
Michael Chanf8503962017-10-26 11:51:28 -04004924 req->num_cmpl_dma_aggr = cpu_to_le16(val);
4925
Michael Chanb153cbc2017-11-03 03:32:39 -04004926 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4927 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 63);
Michael Chanf8503962017-10-26 11:51:28 -04004928 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
4929
4930 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks);
4931 tmr = max_t(u16, tmr, 1);
4932 req->int_lat_tmr_max = cpu_to_le16(tmr);
4933
4934 /* min timer set to 1/2 of interrupt timer */
4935 val = tmr / 2;
4936 req->int_lat_tmr_min = cpu_to_le16(val);
4937
4938 /* buf timer set to 1/4 of interrupt timer */
4939 val = max_t(u16, tmr / 4, 1);
4940 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
4941
4942 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks_irq);
4943 tmr = max_t(u16, tmr, 1);
4944 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
4945
4946 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4947 if (hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
4948 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
Michael Chanbb053f52016-02-26 04:00:02 -05004949 req->flags = cpu_to_le16(flags);
Michael Chanbb053f52016-02-26 04:00:02 -05004950}
4951
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05004952int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
4953{
4954 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
4955 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4956 struct bnxt_coal coal;
4957 unsigned int grp_idx;
4958
4959 /* Tick values in micro seconds.
4960 * 1 coal_buf x bufs_per_record = 1 completion record.
4961 */
4962 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
4963
4964 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
4965 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
4966
4967 if (!bnapi->rx_ring)
4968 return -ENODEV;
4969
4970 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4971 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4972
4973 bnxt_hwrm_set_coal_params(&coal, &req_rx);
4974
4975 grp_idx = bnapi->index;
4976 req_rx.ring_id = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4977
4978 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
4979 HWRM_CMD_TIMEOUT);
4980}
4981
Michael Chanc0c050c2015-10-22 16:01:17 -04004982int bnxt_hwrm_set_coal(struct bnxt *bp)
4983{
4984 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004985 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4986 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004987
Michael Chandfc9c942016-02-26 04:00:03 -05004988 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4989 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4990 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4991 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004992
Michael Chanf8503962017-10-26 11:51:28 -04004993 bnxt_hwrm_set_coal_params(&bp->rx_coal, &req_rx);
4994 bnxt_hwrm_set_coal_params(&bp->tx_coal, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004995
4996 mutex_lock(&bp->hwrm_cmd_lock);
4997 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004998 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004999
Michael Chandfc9c942016-02-26 04:00:03 -05005000 req = &req_rx;
5001 if (!bnapi->rx_ring)
5002 req = &req_tx;
5003 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
5004
5005 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04005006 HWRM_CMD_TIMEOUT);
5007 if (rc)
5008 break;
5009 }
5010 mutex_unlock(&bp->hwrm_cmd_lock);
5011 return rc;
5012}
5013
5014static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5015{
5016 int rc = 0, i;
5017 struct hwrm_stat_ctx_free_input req = {0};
5018
5019 if (!bp->bnapi)
5020 return 0;
5021
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005022 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5023 return 0;
5024
Michael Chanc0c050c2015-10-22 16:01:17 -04005025 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5026
5027 mutex_lock(&bp->hwrm_cmd_lock);
5028 for (i = 0; i < bp->cp_nr_rings; i++) {
5029 struct bnxt_napi *bnapi = bp->bnapi[i];
5030 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5031
5032 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5033 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5034
5035 rc = _hwrm_send_message(bp, &req, sizeof(req),
5036 HWRM_CMD_TIMEOUT);
5037 if (rc)
5038 break;
5039
5040 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5041 }
5042 }
5043 mutex_unlock(&bp->hwrm_cmd_lock);
5044 return rc;
5045}
5046
5047static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5048{
5049 int rc = 0, i;
5050 struct hwrm_stat_ctx_alloc_input req = {0};
5051 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5052
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005053 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5054 return 0;
5055
Michael Chanc0c050c2015-10-22 16:01:17 -04005056 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5057
Michael Chan51f30782016-07-01 18:46:29 -04005058 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04005059
5060 mutex_lock(&bp->hwrm_cmd_lock);
5061 for (i = 0; i < bp->cp_nr_rings; i++) {
5062 struct bnxt_napi *bnapi = bp->bnapi[i];
5063 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5064
5065 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5066
5067 rc = _hwrm_send_message(bp, &req, sizeof(req),
5068 HWRM_CMD_TIMEOUT);
5069 if (rc)
5070 break;
5071
5072 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5073
5074 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5075 }
5076 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08005077 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005078}
5079
Michael Chancf6645f2016-06-13 02:25:28 -04005080static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5081{
5082 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005083 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04005084 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04005085 int rc;
5086
5087 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5088 req.fid = cpu_to_le16(0xffff);
5089 mutex_lock(&bp->hwrm_cmd_lock);
5090 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5091 if (rc)
5092 goto func_qcfg_exit;
5093
5094#ifdef CONFIG_BNXT_SRIOV
5095 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04005096 struct bnxt_vf_info *vf = &bp->vf;
5097
5098 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5099 }
5100#endif
Michael Chan9315edc2017-07-24 12:34:25 -04005101 flags = le16_to_cpu(resp->flags);
5102 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5103 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
5104 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
5105 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
5106 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04005107 }
Michael Chan9315edc2017-07-24 12:34:25 -04005108 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5109 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05005110
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005111 switch (resp->port_partition_type) {
5112 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5113 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5114 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5115 bp->port_partition_type = resp->port_partition_type;
5116 break;
5117 }
Michael Chan32e8239c2017-07-24 12:34:21 -04005118 if (bp->hwrm_spec_code < 0x10707 ||
5119 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5120 bp->br_mode = BRIDGE_MODE_VEB;
5121 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5122 bp->br_mode = BRIDGE_MODE_VEPA;
5123 else
5124 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04005125
Michael Chan7eb9bb32017-10-26 11:51:25 -04005126 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5127 if (!bp->max_mtu)
5128 bp->max_mtu = BNXT_MAX_MTU;
5129
Michael Chancf6645f2016-06-13 02:25:28 -04005130func_qcfg_exit:
5131 mutex_unlock(&bp->hwrm_cmd_lock);
5132 return rc;
5133}
5134
Michael Chandb4723b2018-03-31 13:54:13 -04005135int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005136{
5137 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5138 struct hwrm_func_resource_qcaps_input req = {0};
5139 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5140 int rc;
5141
5142 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
5143 req.fid = cpu_to_le16(0xffff);
5144
5145 mutex_lock(&bp->hwrm_cmd_lock);
5146 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5147 if (rc) {
5148 rc = -EIO;
5149 goto hwrm_func_resc_qcaps_exit;
5150 }
5151
Michael Chandb4723b2018-03-31 13:54:13 -04005152 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
5153 if (!all)
5154 goto hwrm_func_resc_qcaps_exit;
5155
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005156 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
5157 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5158 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
5159 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5160 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
5161 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5162 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
5163 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5164 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
5165 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
5166 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
5167 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5168 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
5169 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5170 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
5171 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5172
Michael Chan4673d662018-01-17 03:21:11 -05005173 if (BNXT_PF(bp)) {
5174 struct bnxt_pf_info *pf = &bp->pf;
5175
5176 pf->vf_resv_strategy =
5177 le16_to_cpu(resp->vf_reservation_strategy);
5178 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL)
5179 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
5180 }
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005181hwrm_func_resc_qcaps_exit:
5182 mutex_unlock(&bp->hwrm_cmd_lock);
5183 return rc;
5184}
5185
5186static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005187{
5188 int rc = 0;
5189 struct hwrm_func_qcaps_input req = {0};
5190 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6a4f2942018-01-17 03:21:06 -05005191 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5192 u32 flags;
Michael Chanc0c050c2015-10-22 16:01:17 -04005193
5194 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
5195 req.fid = cpu_to_le16(0xffff);
5196
5197 mutex_lock(&bp->hwrm_cmd_lock);
5198 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5199 if (rc)
5200 goto hwrm_func_qcaps_exit;
5201
Michael Chan6a4f2942018-01-17 03:21:06 -05005202 flags = le32_to_cpu(resp->flags);
5203 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05005204 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
Michael Chan6a4f2942018-01-17 03:21:06 -05005205 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05005206 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
5207
Michael Chan7cc5a202016-09-19 03:58:05 -04005208 bp->tx_push_thresh = 0;
Michael Chan6a4f2942018-01-17 03:21:06 -05005209 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
Michael Chan7cc5a202016-09-19 03:58:05 -04005210 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
5211
Michael Chan6a4f2942018-01-17 03:21:06 -05005212 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5213 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5214 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5215 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5216 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
5217 if (!hw_resc->max_hw_ring_grps)
5218 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
5219 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5220 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5221 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5222
Michael Chanc0c050c2015-10-22 16:01:17 -04005223 if (BNXT_PF(bp)) {
5224 struct bnxt_pf_info *pf = &bp->pf;
5225
5226 pf->fw_fid = le16_to_cpu(resp->fid);
5227 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04005228 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04005229 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04005230 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
5231 pf->max_vfs = le16_to_cpu(resp->max_vfs);
5232 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
5233 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
5234 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
5235 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
5236 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
5237 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chan6a4f2942018-01-17 03:21:06 -05005238 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
Michael Chanc1ef1462017-04-04 18:14:07 -04005239 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04005240 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04005241#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04005242 struct bnxt_vf_info *vf = &bp->vf;
5243
5244 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan7cc5a202016-09-19 03:58:05 -04005245 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan379a80a2015-10-23 15:06:19 -04005246#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005247 }
5248
Michael Chanc0c050c2015-10-22 16:01:17 -04005249hwrm_func_qcaps_exit:
5250 mutex_unlock(&bp->hwrm_cmd_lock);
5251 return rc;
5252}
5253
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005254static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
5255{
5256 int rc;
5257
5258 rc = __bnxt_hwrm_func_qcaps(bp);
5259 if (rc)
5260 return rc;
5261 if (bp->hwrm_spec_code >= 0x10803) {
Michael Chandb4723b2018-03-31 13:54:13 -04005262 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005263 if (!rc)
5264 bp->flags |= BNXT_FLAG_NEW_RM;
5265 }
5266 return 0;
5267}
5268
Michael Chanc0c050c2015-10-22 16:01:17 -04005269static int bnxt_hwrm_func_reset(struct bnxt *bp)
5270{
5271 struct hwrm_func_reset_input req = {0};
5272
5273 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
5274 req.enables = 0;
5275
5276 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
5277}
5278
5279static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
5280{
5281 int rc = 0;
5282 struct hwrm_queue_qportcfg_input req = {0};
5283 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
5284 u8 i, *qptr;
5285
5286 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
5287
5288 mutex_lock(&bp->hwrm_cmd_lock);
5289 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5290 if (rc)
5291 goto qportcfg_exit;
5292
5293 if (!resp->max_configurable_queues) {
5294 rc = -EINVAL;
5295 goto qportcfg_exit;
5296 }
5297 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05005298 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04005299 if (bp->max_tc > BNXT_MAX_QUEUE)
5300 bp->max_tc = BNXT_MAX_QUEUE;
5301
Michael Chan441cabb2016-09-19 03:58:02 -04005302 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
5303 bp->max_tc = 1;
5304
Michael Chan87c374d2016-12-02 21:17:16 -05005305 if (bp->max_lltc > bp->max_tc)
5306 bp->max_lltc = bp->max_tc;
5307
Michael Chanc0c050c2015-10-22 16:01:17 -04005308 qptr = &resp->queue_id0;
5309 for (i = 0; i < bp->max_tc; i++) {
5310 bp->q_info[i].queue_id = *qptr++;
5311 bp->q_info[i].queue_profile = *qptr++;
Michael Chan2e8ef772018-04-26 17:44:31 -04005312 bp->tc_to_qidx[i] = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005313 }
5314
5315qportcfg_exit:
5316 mutex_unlock(&bp->hwrm_cmd_lock);
5317 return rc;
5318}
5319
5320static int bnxt_hwrm_ver_get(struct bnxt *bp)
5321{
5322 int rc;
5323 struct hwrm_ver_get_input req = {0};
5324 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04005325 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005326
Michael Chane6ef2692016-03-28 19:46:05 -04005327 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04005328 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
5329 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
5330 req.hwrm_intf_min = HWRM_VERSION_MINOR;
5331 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
5332 mutex_lock(&bp->hwrm_cmd_lock);
5333 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5334 if (rc)
5335 goto hwrm_ver_get_exit;
5336
5337 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
5338
Michael Chan894aa692018-01-17 03:21:03 -05005339 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
5340 resp->hwrm_intf_min_8b << 8 |
5341 resp->hwrm_intf_upd_8b;
5342 if (resp->hwrm_intf_maj_8b < 1) {
Michael Chanc1935542015-12-27 18:19:28 -05005343 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chan894aa692018-01-17 03:21:03 -05005344 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
5345 resp->hwrm_intf_upd_8b);
Michael Chanc1935542015-12-27 18:19:28 -05005346 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005347 }
Michael Chan431aa1e2017-10-26 11:51:23 -04005348 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
Michael Chan894aa692018-01-17 03:21:03 -05005349 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
5350 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
Michael Chanc0c050c2015-10-22 16:01:17 -04005351
Michael Chanff4fe812016-02-26 04:00:04 -05005352 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
5353 if (!bp->hwrm_cmd_timeout)
5354 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
5355
Michael Chan894aa692018-01-17 03:21:03 -05005356 if (resp->hwrm_intf_maj_8b >= 1)
Michael Chane6ef2692016-03-28 19:46:05 -04005357 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
5358
Michael Chan659c8052016-06-13 02:25:33 -04005359 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005360 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
5361 !resp->chip_metal)
5362 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04005363
Deepak Khungare605db82017-05-29 19:06:04 -04005364 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
5365 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
5366 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
5367 bp->flags |= BNXT_FLAG_SHORT_CMD;
5368
Michael Chanc0c050c2015-10-22 16:01:17 -04005369hwrm_ver_get_exit:
5370 mutex_unlock(&bp->hwrm_cmd_lock);
5371 return rc;
5372}
5373
Rob Swindell5ac67d82016-09-19 03:58:03 -04005374int bnxt_hwrm_fw_set_time(struct bnxt *bp)
5375{
5376 struct hwrm_fw_set_time_input req = {0};
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01005377 struct tm tm;
5378 time64_t now = ktime_get_real_seconds();
Rob Swindell5ac67d82016-09-19 03:58:03 -04005379
Michael Chanca2c39e2018-04-26 17:44:34 -04005380 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
5381 bp->hwrm_spec_code < 0x10400)
Rob Swindell5ac67d82016-09-19 03:58:03 -04005382 return -EOPNOTSUPP;
5383
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01005384 time64_to_tm(now, 0, &tm);
Rob Swindell5ac67d82016-09-19 03:58:03 -04005385 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
5386 req.year = cpu_to_le16(1900 + tm.tm_year);
5387 req.month = 1 + tm.tm_mon;
5388 req.day = tm.tm_mday;
5389 req.hour = tm.tm_hour;
5390 req.minute = tm.tm_min;
5391 req.second = tm.tm_sec;
5392 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5393}
5394
Michael Chan3bdf56c2016-03-07 15:38:45 -05005395static int bnxt_hwrm_port_qstats(struct bnxt *bp)
5396{
5397 int rc;
5398 struct bnxt_pf_info *pf = &bp->pf;
5399 struct hwrm_port_qstats_input req = {0};
5400
5401 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
5402 return 0;
5403
5404 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
5405 req.port_id = cpu_to_le16(pf->port_id);
5406 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
5407 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
5408 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5409 return rc;
5410}
5411
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04005412static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
5413{
5414 struct hwrm_port_qstats_ext_input req = {0};
5415 struct bnxt_pf_info *pf = &bp->pf;
5416
5417 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
5418 return 0;
5419
5420 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
5421 req.port_id = cpu_to_le16(pf->port_id);
5422 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
5423 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
5424 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5425}
5426
Michael Chanc0c050c2015-10-22 16:01:17 -04005427static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
5428{
5429 if (bp->vxlan_port_cnt) {
5430 bnxt_hwrm_tunnel_dst_port_free(
5431 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5432 }
5433 bp->vxlan_port_cnt = 0;
5434 if (bp->nge_port_cnt) {
5435 bnxt_hwrm_tunnel_dst_port_free(
5436 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5437 }
5438 bp->nge_port_cnt = 0;
5439}
5440
5441static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
5442{
5443 int rc, i;
5444 u32 tpa_flags = 0;
5445
5446 if (set_tpa)
5447 tpa_flags = bp->flags & BNXT_FLAG_TPA;
5448 for (i = 0; i < bp->nr_vnics; i++) {
5449 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
5450 if (rc) {
5451 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04005452 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04005453 return rc;
5454 }
5455 }
5456 return 0;
5457}
5458
5459static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
5460{
5461 int i;
5462
5463 for (i = 0; i < bp->nr_vnics; i++)
5464 bnxt_hwrm_vnic_set_rss(bp, i, false);
5465}
5466
5467static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
5468 bool irq_re_init)
5469{
5470 if (bp->vnic_info) {
5471 bnxt_hwrm_clear_vnic_filter(bp);
5472 /* clear all RSS setting before free vnic ctx */
5473 bnxt_hwrm_clear_vnic_rss(bp);
5474 bnxt_hwrm_vnic_ctx_free(bp);
5475 /* before free the vnic, undo the vnic tpa settings */
5476 if (bp->flags & BNXT_FLAG_TPA)
5477 bnxt_set_tpa(bp, false);
5478 bnxt_hwrm_vnic_free(bp);
5479 }
5480 bnxt_hwrm_ring_free(bp, close_path);
5481 bnxt_hwrm_ring_grp_free(bp);
5482 if (irq_re_init) {
5483 bnxt_hwrm_stat_ctx_free(bp);
5484 bnxt_hwrm_free_tunnel_ports(bp);
5485 }
5486}
5487
Michael Chan39d8ba22017-07-24 12:34:22 -04005488static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
5489{
5490 struct hwrm_func_cfg_input req = {0};
5491 int rc;
5492
5493 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5494 req.fid = cpu_to_le16(0xffff);
5495 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
5496 if (br_mode == BRIDGE_MODE_VEB)
5497 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
5498 else if (br_mode == BRIDGE_MODE_VEPA)
5499 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
5500 else
5501 return -EINVAL;
5502 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5503 if (rc)
5504 rc = -EIO;
5505 return rc;
5506}
5507
Michael Chanc3480a62018-01-17 03:21:15 -05005508static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
5509{
5510 struct hwrm_func_cfg_input req = {0};
5511 int rc;
5512
5513 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
5514 return 0;
5515
5516 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5517 req.fid = cpu_to_le16(0xffff);
5518 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
Michael Chand4f52de02018-03-31 13:54:06 -04005519 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
Michael Chanc3480a62018-01-17 03:21:15 -05005520 if (size == 128)
Michael Chand4f52de02018-03-31 13:54:06 -04005521 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
Michael Chanc3480a62018-01-17 03:21:15 -05005522
5523 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5524 if (rc)
5525 rc = -EIO;
5526 return rc;
5527}
5528
Michael Chanc0c050c2015-10-22 16:01:17 -04005529static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
5530{
Michael Chanae10ae72016-12-29 12:13:38 -05005531 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04005532 int rc;
5533
Michael Chanae10ae72016-12-29 12:13:38 -05005534 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
5535 goto skip_rss_ctx;
5536
Michael Chanc0c050c2015-10-22 16:01:17 -04005537 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005538 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04005539 if (rc) {
5540 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5541 vnic_id, rc);
5542 goto vnic_setup_err;
5543 }
5544 bp->rsscos_nr_ctxs++;
5545
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005546 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5547 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5548 if (rc) {
5549 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5550 vnic_id, rc);
5551 goto vnic_setup_err;
5552 }
5553 bp->rsscos_nr_ctxs++;
5554 }
5555
Michael Chanae10ae72016-12-29 12:13:38 -05005556skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04005557 /* configure default vnic, ring grp */
5558 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5559 if (rc) {
5560 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5561 vnic_id, rc);
5562 goto vnic_setup_err;
5563 }
5564
5565 /* Enable RSS hashing on vnic */
5566 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5567 if (rc) {
5568 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5569 vnic_id, rc);
5570 goto vnic_setup_err;
5571 }
5572
5573 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5574 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5575 if (rc) {
5576 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5577 vnic_id, rc);
5578 }
5579 }
5580
5581vnic_setup_err:
5582 return rc;
5583}
5584
5585static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5586{
5587#ifdef CONFIG_RFS_ACCEL
5588 int i, rc = 0;
5589
5590 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005591 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005592 u16 vnic_id = i + 1;
5593 u16 ring_id = i;
5594
5595 if (vnic_id >= bp->nr_vnics)
5596 break;
5597
Michael Chanae10ae72016-12-29 12:13:38 -05005598 vnic = &bp->vnic_info[vnic_id];
5599 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5600 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5601 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005602 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005603 if (rc) {
5604 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5605 vnic_id, rc);
5606 break;
5607 }
5608 rc = bnxt_setup_vnic(bp, vnic_id);
5609 if (rc)
5610 break;
5611 }
5612 return rc;
5613#else
5614 return 0;
5615#endif
5616}
5617
Michael Chan17c71ac2016-07-01 18:46:27 -04005618/* Allow PF and VF with default VLAN to be in promiscuous mode */
5619static bool bnxt_promisc_ok(struct bnxt *bp)
5620{
5621#ifdef CONFIG_BNXT_SRIOV
5622 if (BNXT_VF(bp) && !bp->vf.vlan)
5623 return false;
5624#endif
5625 return true;
5626}
5627
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005628static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5629{
5630 unsigned int rc = 0;
5631
5632 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5633 if (rc) {
5634 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5635 rc);
5636 return rc;
5637 }
5638
5639 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5640 if (rc) {
5641 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5642 rc);
5643 return rc;
5644 }
5645 return rc;
5646}
5647
Michael Chanb664f002015-12-02 01:54:08 -05005648static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005649static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005650
Michael Chanc0c050c2015-10-22 16:01:17 -04005651static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5652{
Michael Chan7d2837d2016-05-04 16:56:44 -04005653 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005654 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005655 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005656
5657 if (irq_re_init) {
5658 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5659 if (rc) {
5660 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5661 rc);
5662 goto err_out;
5663 }
5664 }
5665
5666 rc = bnxt_hwrm_ring_alloc(bp);
5667 if (rc) {
5668 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5669 goto err_out;
5670 }
5671
5672 rc = bnxt_hwrm_ring_grp_alloc(bp);
5673 if (rc) {
5674 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5675 goto err_out;
5676 }
5677
Prashant Sreedharan76595192016-07-18 07:15:22 -04005678 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5679 rx_nr_rings--;
5680
Michael Chanc0c050c2015-10-22 16:01:17 -04005681 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005682 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005683 if (rc) {
5684 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5685 goto err_out;
5686 }
5687
5688 rc = bnxt_setup_vnic(bp, 0);
5689 if (rc)
5690 goto err_out;
5691
5692 if (bp->flags & BNXT_FLAG_RFS) {
5693 rc = bnxt_alloc_rfs_vnics(bp);
5694 if (rc)
5695 goto err_out;
5696 }
5697
5698 if (bp->flags & BNXT_FLAG_TPA) {
5699 rc = bnxt_set_tpa(bp, true);
5700 if (rc)
5701 goto err_out;
5702 }
5703
5704 if (BNXT_VF(bp))
5705 bnxt_update_vf_mac(bp);
5706
5707 /* Filter for default vnic 0 */
5708 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5709 if (rc) {
5710 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5711 goto err_out;
5712 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005713 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005714
Michael Chan7d2837d2016-05-04 16:56:44 -04005715 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005716
Michael Chan17c71ac2016-07-01 18:46:27 -04005717 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005718 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5719
5720 if (bp->dev->flags & IFF_ALLMULTI) {
5721 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5722 vnic->mc_list_count = 0;
5723 } else {
5724 u32 mask = 0;
5725
5726 bnxt_mc_list_updated(bp, &mask);
5727 vnic->rx_mask |= mask;
5728 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005729
Michael Chanb664f002015-12-02 01:54:08 -05005730 rc = bnxt_cfg_rx_mode(bp);
5731 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005732 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005733
5734 rc = bnxt_hwrm_set_coal(bp);
5735 if (rc)
5736 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005737 rc);
5738
5739 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5740 rc = bnxt_setup_nitroa0_vnic(bp);
5741 if (rc)
5742 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5743 rc);
5744 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005745
Michael Chancf6645f2016-06-13 02:25:28 -04005746 if (BNXT_VF(bp)) {
5747 bnxt_hwrm_func_qcfg(bp);
5748 netdev_update_features(bp->dev);
5749 }
5750
Michael Chanc0c050c2015-10-22 16:01:17 -04005751 return 0;
5752
5753err_out:
5754 bnxt_hwrm_resource_free(bp, 0, true);
5755
5756 return rc;
5757}
5758
5759static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5760{
5761 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5762 return 0;
5763}
5764
5765static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5766{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005767 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005768 bnxt_init_rx_rings(bp);
5769 bnxt_init_tx_rings(bp);
5770 bnxt_init_ring_grps(bp, irq_re_init);
5771 bnxt_init_vnics(bp);
5772
5773 return bnxt_init_chip(bp, irq_re_init);
5774}
5775
Michael Chanc0c050c2015-10-22 16:01:17 -04005776static int bnxt_set_real_num_queues(struct bnxt *bp)
5777{
5778 int rc;
5779 struct net_device *dev = bp->dev;
5780
Michael Chan5f449242017-02-06 16:55:40 -05005781 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5782 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005783 if (rc)
5784 return rc;
5785
5786 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5787 if (rc)
5788 return rc;
5789
5790#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005791 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005792 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005793#endif
5794
5795 return rc;
5796}
5797
Michael Chan6e6c5a52016-01-02 23:45:02 -05005798static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5799 bool shared)
5800{
5801 int _rx = *rx, _tx = *tx;
5802
5803 if (shared) {
5804 *rx = min_t(int, _rx, max);
5805 *tx = min_t(int, _tx, max);
5806 } else {
5807 if (max < 2)
5808 return -ENOMEM;
5809
5810 while (_rx + _tx > max) {
5811 if (_rx > _tx && _rx > 1)
5812 _rx--;
5813 else if (_tx > 1)
5814 _tx--;
5815 }
5816 *rx = _rx;
5817 *tx = _tx;
5818 }
5819 return 0;
5820}
5821
Michael Chan78095922016-12-07 00:26:16 -05005822static void bnxt_setup_msix(struct bnxt *bp)
5823{
5824 const int len = sizeof(bp->irq_tbl[0].name);
5825 struct net_device *dev = bp->dev;
5826 int tcs, i;
5827
5828 tcs = netdev_get_num_tc(dev);
5829 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005830 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005831
Michael Chand1e79252017-02-06 16:55:38 -05005832 for (i = 0; i < tcs; i++) {
5833 count = bp->tx_nr_rings_per_tc;
5834 off = i * count;
5835 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005836 }
5837 }
5838
5839 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04005840 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
Michael Chan78095922016-12-07 00:26:16 -05005841 char *attr;
5842
5843 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5844 attr = "TxRx";
5845 else if (i < bp->rx_nr_rings)
5846 attr = "rx";
5847 else
5848 attr = "tx";
5849
Michael Chane5811b82018-03-31 13:54:18 -04005850 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
5851 attr, i);
5852 bp->irq_tbl[map_idx].handler = bnxt_msix;
Michael Chan78095922016-12-07 00:26:16 -05005853 }
5854}
5855
5856static void bnxt_setup_inta(struct bnxt *bp)
5857{
5858 const int len = sizeof(bp->irq_tbl[0].name);
5859
5860 if (netdev_get_num_tc(bp->dev))
5861 netdev_reset_tc(bp->dev);
5862
5863 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5864 0);
5865 bp->irq_tbl[0].handler = bnxt_inta;
5866}
5867
5868static int bnxt_setup_int_mode(struct bnxt *bp)
5869{
5870 int rc;
5871
5872 if (bp->flags & BNXT_FLAG_USING_MSIX)
5873 bnxt_setup_msix(bp);
5874 else
5875 bnxt_setup_inta(bp);
5876
5877 rc = bnxt_set_real_num_queues(bp);
5878 return rc;
5879}
5880
Michael Chanb7429952017-01-13 01:32:00 -05005881#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005882static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5883{
Michael Chan6a4f2942018-01-17 03:21:06 -05005884 return bp->hw_resc.max_rsscos_ctxs;
Michael Chan8079e8f2016-12-29 12:13:37 -05005885}
5886
5887static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5888{
Michael Chan6a4f2942018-01-17 03:21:06 -05005889 return bp->hw_resc.max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05005890}
Michael Chanb7429952017-01-13 01:32:00 -05005891#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005892
Michael Chane4060d32016-12-07 00:26:19 -05005893unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5894{
Michael Chan6a4f2942018-01-17 03:21:06 -05005895 return bp->hw_resc.max_stat_ctxs;
Michael Chane4060d32016-12-07 00:26:19 -05005896}
5897
Michael Chana588e452016-12-07 00:26:21 -05005898void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5899{
Michael Chan6a4f2942018-01-17 03:21:06 -05005900 bp->hw_resc.max_stat_ctxs = max;
Michael Chana588e452016-12-07 00:26:21 -05005901}
5902
Michael Chane4060d32016-12-07 00:26:19 -05005903unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5904{
Michael Chan6a4f2942018-01-17 03:21:06 -05005905 return bp->hw_resc.max_cp_rings;
Michael Chane4060d32016-12-07 00:26:19 -05005906}
5907
Michael Chana588e452016-12-07 00:26:21 -05005908void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5909{
Michael Chan6a4f2942018-01-17 03:21:06 -05005910 bp->hw_resc.max_cp_rings = max;
Michael Chana588e452016-12-07 00:26:21 -05005911}
5912
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005913unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
Michael Chan78095922016-12-07 00:26:16 -05005914{
Michael Chan6a4f2942018-01-17 03:21:06 -05005915 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5916
5917 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005918}
5919
Michael Chan33c26572016-12-07 00:26:15 -05005920void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5921{
Michael Chan6a4f2942018-01-17 03:21:06 -05005922 bp->hw_resc.max_irqs = max_irqs;
Michael Chan33c26572016-12-07 00:26:15 -05005923}
5924
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005925int bnxt_get_avail_msix(struct bnxt *bp, int num)
5926{
5927 int max_cp = bnxt_get_max_func_cp_rings(bp);
5928 int max_irq = bnxt_get_max_func_irqs(bp);
5929 int total_req = bp->cp_nr_rings + num;
5930 int max_idx, avail_msix;
5931
5932 max_idx = min_t(int, bp->total_irqs, max_cp);
5933 avail_msix = max_idx - bp->cp_nr_rings;
5934 if (!(bp->flags & BNXT_FLAG_NEW_RM) || avail_msix >= num)
5935 return avail_msix;
5936
5937 if (max_irq < total_req) {
5938 num = max_irq - bp->cp_nr_rings;
5939 if (num <= 0)
5940 return 0;
5941 }
5942 return num;
5943}
5944
Michael Chan08654eb2018-03-31 13:54:17 -04005945static int bnxt_get_num_msix(struct bnxt *bp)
5946{
5947 if (!(bp->flags & BNXT_FLAG_NEW_RM))
5948 return bnxt_get_max_func_irqs(bp);
5949
5950 return bnxt_cp_rings_in_use(bp);
5951}
5952
Michael Chan78095922016-12-07 00:26:16 -05005953static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005954{
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005955 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
Michael Chan78095922016-12-07 00:26:16 -05005956 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005957
Michael Chan08654eb2018-03-31 13:54:17 -04005958 total_vecs = bnxt_get_num_msix(bp);
5959 max = bnxt_get_max_func_irqs(bp);
5960 if (total_vecs > max)
5961 total_vecs = max;
5962
Michael Chan2773dfb2018-04-26 17:44:42 -04005963 if (!total_vecs)
5964 return 0;
5965
Michael Chanc0c050c2015-10-22 16:01:17 -04005966 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5967 if (!msix_ent)
5968 return -ENOMEM;
5969
5970 for (i = 0; i < total_vecs; i++) {
5971 msix_ent[i].entry = i;
5972 msix_ent[i].vector = 0;
5973 }
5974
Michael Chan01657bc2016-01-02 23:45:03 -05005975 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5976 min = 2;
5977
5978 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005979 ulp_msix = bnxt_get_ulp_msix_num(bp);
5980 if (total_vecs < 0 || total_vecs < ulp_msix) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005981 rc = -ENODEV;
5982 goto msix_setup_exit;
5983 }
5984
5985 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5986 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005987 for (i = 0; i < total_vecs; i++)
5988 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005989
Michael Chan78095922016-12-07 00:26:16 -05005990 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005991 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005992 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005993 total_vecs - ulp_msix, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005994 if (rc)
5995 goto msix_setup_exit;
5996
Michael Chan78095922016-12-07 00:26:16 -05005997 bp->cp_nr_rings = (min == 1) ?
5998 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5999 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006000
Michael Chanc0c050c2015-10-22 16:01:17 -04006001 } else {
6002 rc = -ENOMEM;
6003 goto msix_setup_exit;
6004 }
6005 bp->flags |= BNXT_FLAG_USING_MSIX;
6006 kfree(msix_ent);
6007 return 0;
6008
6009msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05006010 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
6011 kfree(bp->irq_tbl);
6012 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04006013 pci_disable_msix(bp->pdev);
6014 kfree(msix_ent);
6015 return rc;
6016}
6017
Michael Chan78095922016-12-07 00:26:16 -05006018static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006019{
Michael Chanc0c050c2015-10-22 16:01:17 -04006020 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05006021 if (!bp->irq_tbl)
6022 return -ENOMEM;
6023
6024 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04006025 bp->rx_nr_rings = 1;
6026 bp->tx_nr_rings = 1;
6027 bp->cp_nr_rings = 1;
Michael Chan01657bc2016-01-02 23:45:03 -05006028 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04006029 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05006030 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006031}
6032
Michael Chan78095922016-12-07 00:26:16 -05006033static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006034{
6035 int rc = 0;
6036
6037 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05006038 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006039
Michael Chan1fa72e22016-04-25 02:30:49 -04006040 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006041 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05006042 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006043 }
6044 return rc;
6045}
6046
Michael Chan78095922016-12-07 00:26:16 -05006047static void bnxt_clear_int_mode(struct bnxt *bp)
6048{
6049 if (bp->flags & BNXT_FLAG_USING_MSIX)
6050 pci_disable_msix(bp->pdev);
6051
6052 kfree(bp->irq_tbl);
6053 bp->irq_tbl = NULL;
6054 bp->flags &= ~BNXT_FLAG_USING_MSIX;
6055}
6056
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006057int bnxt_reserve_rings(struct bnxt *bp)
Michael Chan674f50a2018-01-17 03:21:09 -05006058{
Michael Chan674f50a2018-01-17 03:21:09 -05006059 int tcs = netdev_get_num_tc(bp->dev);
6060 int rc;
6061
6062 if (!bnxt_need_reserve_rings(bp))
6063 return 0;
6064
6065 rc = __bnxt_reserve_rings(bp);
6066 if (rc) {
6067 netdev_err(bp->dev, "ring reservation failure rc: %d\n", rc);
6068 return rc;
6069 }
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006070 if ((bp->flags & BNXT_FLAG_NEW_RM) &&
6071 (bnxt_get_num_msix(bp) != bp->total_irqs)) {
Michael Chanec86f142018-03-31 13:54:21 -04006072 bnxt_ulp_irq_stop(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05006073 bnxt_clear_int_mode(bp);
6074 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -04006075 bnxt_ulp_irq_restart(bp, rc);
Michael Chan674f50a2018-01-17 03:21:09 -05006076 if (rc)
6077 return rc;
6078 }
6079 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
6080 netdev_err(bp->dev, "tx ring reservation failure\n");
6081 netdev_reset_tc(bp->dev);
6082 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
6083 return -ENOMEM;
6084 }
6085 bp->num_stat_ctxs = bp->cp_nr_rings;
6086 return 0;
6087}
6088
Michael Chanc0c050c2015-10-22 16:01:17 -04006089static void bnxt_free_irq(struct bnxt *bp)
6090{
6091 struct bnxt_irq *irq;
6092 int i;
6093
6094#ifdef CONFIG_RFS_ACCEL
6095 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
6096 bp->dev->rx_cpu_rmap = NULL;
6097#endif
Michael Chancb985262018-04-11 11:50:18 -04006098 if (!bp->irq_tbl || !bp->bnapi)
Michael Chanc0c050c2015-10-22 16:01:17 -04006099 return;
6100
6101 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04006102 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6103
6104 irq = &bp->irq_tbl[map_idx];
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006105 if (irq->requested) {
6106 if (irq->have_cpumask) {
6107 irq_set_affinity_hint(irq->vector, NULL);
6108 free_cpumask_var(irq->cpu_mask);
6109 irq->have_cpumask = 0;
6110 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006111 free_irq(irq->vector, bp->bnapi[i]);
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006112 }
6113
Michael Chanc0c050c2015-10-22 16:01:17 -04006114 irq->requested = 0;
6115 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006116}
6117
6118static int bnxt_request_irq(struct bnxt *bp)
6119{
Michael Chanb81a90d2016-01-02 23:45:01 -05006120 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006121 unsigned long flags = 0;
6122#ifdef CONFIG_RFS_ACCEL
Michael Chane5811b82018-03-31 13:54:18 -04006123 struct cpu_rmap *rmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04006124#endif
6125
Michael Chane5811b82018-03-31 13:54:18 -04006126 rc = bnxt_setup_int_mode(bp);
6127 if (rc) {
6128 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6129 rc);
6130 return rc;
6131 }
6132#ifdef CONFIG_RFS_ACCEL
6133 rmap = bp->dev->rx_cpu_rmap;
6134#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006135 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
6136 flags = IRQF_SHARED;
6137
Michael Chanb81a90d2016-01-02 23:45:01 -05006138 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04006139 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6140 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
6141
Michael Chanc0c050c2015-10-22 16:01:17 -04006142#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05006143 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006144 rc = irq_cpu_rmap_add(rmap, irq->vector);
6145 if (rc)
6146 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05006147 j);
6148 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04006149 }
6150#endif
6151 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
6152 bp->bnapi[i]);
6153 if (rc)
6154 break;
6155
6156 irq->requested = 1;
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006157
6158 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
6159 int numa_node = dev_to_node(&bp->pdev->dev);
6160
6161 irq->have_cpumask = 1;
6162 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
6163 irq->cpu_mask);
6164 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
6165 if (rc) {
6166 netdev_warn(bp->dev,
6167 "Set affinity failed, IRQ = %d\n",
6168 irq->vector);
6169 break;
6170 }
6171 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006172 }
6173 return rc;
6174}
6175
6176static void bnxt_del_napi(struct bnxt *bp)
6177{
6178 int i;
6179
6180 if (!bp->bnapi)
6181 return;
6182
6183 for (i = 0; i < bp->cp_nr_rings; i++) {
6184 struct bnxt_napi *bnapi = bp->bnapi[i];
6185
6186 napi_hash_del(&bnapi->napi);
6187 netif_napi_del(&bnapi->napi);
6188 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08006189 /* We called napi_hash_del() before netif_napi_del(), we need
6190 * to respect an RCU grace period before freeing napi structures.
6191 */
6192 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04006193}
6194
6195static void bnxt_init_napi(struct bnxt *bp)
6196{
6197 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04006198 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006199 struct bnxt_napi *bnapi;
6200
6201 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04006202 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6203 cp_nr_rings--;
6204 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006205 bnapi = bp->bnapi[i];
6206 netif_napi_add(bp->dev, &bnapi->napi,
6207 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04006208 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04006209 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6210 bnapi = bp->bnapi[cp_nr_rings];
6211 netif_napi_add(bp->dev, &bnapi->napi,
6212 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04006213 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006214 } else {
6215 bnapi = bp->bnapi[0];
6216 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04006217 }
6218}
6219
6220static void bnxt_disable_napi(struct bnxt *bp)
6221{
6222 int i;
6223
6224 if (!bp->bnapi)
6225 return;
6226
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05006227 for (i = 0; i < bp->cp_nr_rings; i++) {
6228 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
6229
6230 if (bp->bnapi[i]->rx_ring)
6231 cancel_work_sync(&cpr->dim.work);
6232
Michael Chanc0c050c2015-10-22 16:01:17 -04006233 napi_disable(&bp->bnapi[i]->napi);
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05006234 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006235}
6236
6237static void bnxt_enable_napi(struct bnxt *bp)
6238{
6239 int i;
6240
6241 for (i = 0; i < bp->cp_nr_rings; i++) {
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05006242 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
Michael Chanfa7e2812016-05-10 19:18:00 -04006243 bp->bnapi[i]->in_reset = false;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05006244
6245 if (bp->bnapi[i]->rx_ring) {
6246 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
6247 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6248 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006249 napi_enable(&bp->bnapi[i]->napi);
6250 }
6251}
6252
Michael Chan7df4ae92016-12-02 21:17:17 -05006253void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006254{
6255 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04006256 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04006257
Michael Chanb6ab4b02016-01-02 23:44:59 -05006258 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006259 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05006260 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006261 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04006262 }
6263 }
6264 /* Stop all TX queues */
6265 netif_tx_disable(bp->dev);
6266 netif_carrier_off(bp->dev);
6267}
6268
Michael Chan7df4ae92016-12-02 21:17:17 -05006269void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006270{
6271 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04006272 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04006273
6274 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05006275 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006276 txr->dev_state = 0;
6277 }
6278 netif_tx_wake_all_queues(bp->dev);
6279 if (bp->link_info.link_up)
6280 netif_carrier_on(bp->dev);
6281}
6282
6283static void bnxt_report_link(struct bnxt *bp)
6284{
6285 if (bp->link_info.link_up) {
6286 const char *duplex;
6287 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04006288 u32 speed;
6289 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04006290
6291 netif_carrier_on(bp->dev);
6292 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
6293 duplex = "full";
6294 else
6295 duplex = "half";
6296 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
6297 flow_ctrl = "ON - receive & transmit";
6298 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
6299 flow_ctrl = "ON - transmit";
6300 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
6301 flow_ctrl = "ON - receive";
6302 else
6303 flow_ctrl = "none";
6304 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04006305 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04006306 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04006307 if (bp->flags & BNXT_FLAG_EEE_CAP)
6308 netdev_info(bp->dev, "EEE is %s\n",
6309 bp->eee.eee_active ? "active" :
6310 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05006311 fec = bp->link_info.fec_cfg;
6312 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
6313 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
6314 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
6315 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
6316 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04006317 } else {
6318 netif_carrier_off(bp->dev);
6319 netdev_err(bp->dev, "NIC Link is Down\n");
6320 }
6321}
6322
Michael Chan170ce012016-04-05 14:08:57 -04006323static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
6324{
6325 int rc = 0;
6326 struct hwrm_port_phy_qcaps_input req = {0};
6327 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04006328 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04006329
6330 if (bp->hwrm_spec_code < 0x10201)
6331 return 0;
6332
6333 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
6334
6335 mutex_lock(&bp->hwrm_cmd_lock);
6336 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6337 if (rc)
6338 goto hwrm_phy_qcaps_exit;
6339
Michael Chanacb20052017-07-24 12:34:20 -04006340 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04006341 struct ethtool_eee *eee = &bp->eee;
6342 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
6343
6344 bp->flags |= BNXT_FLAG_EEE_CAP;
6345 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6346 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
6347 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
6348 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
6349 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
6350 }
Michael Chan520ad892017-03-08 18:44:35 -05006351 if (resp->supported_speeds_auto_mode)
6352 link_info->support_auto_speeds =
6353 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04006354
Michael Chand5430d32017-08-28 13:40:31 -04006355 bp->port_count = resp->port_cnt;
6356
Michael Chan170ce012016-04-05 14:08:57 -04006357hwrm_phy_qcaps_exit:
6358 mutex_unlock(&bp->hwrm_cmd_lock);
6359 return rc;
6360}
6361
Michael Chanc0c050c2015-10-22 16:01:17 -04006362static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
6363{
6364 int rc = 0;
6365 struct bnxt_link_info *link_info = &bp->link_info;
6366 struct hwrm_port_phy_qcfg_input req = {0};
6367 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6368 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05006369 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04006370
6371 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
6372
6373 mutex_lock(&bp->hwrm_cmd_lock);
6374 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6375 if (rc) {
6376 mutex_unlock(&bp->hwrm_cmd_lock);
6377 return rc;
6378 }
6379
6380 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
6381 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04006382 link_info->duplex = resp->duplex_cfg;
6383 if (bp->hwrm_spec_code >= 0x10800)
6384 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04006385 link_info->pause = resp->pause;
6386 link_info->auto_mode = resp->auto_mode;
6387 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05006388 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04006389 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04006390 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04006391 if (link_info->phy_link_status == BNXT_LINK_LINK)
6392 link_info->link_speed = le16_to_cpu(resp->link_speed);
6393 else
6394 link_info->link_speed = 0;
6395 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04006396 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
6397 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05006398 link_info->lp_auto_link_speeds =
6399 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04006400 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
6401 link_info->phy_ver[0] = resp->phy_maj;
6402 link_info->phy_ver[1] = resp->phy_min;
6403 link_info->phy_ver[2] = resp->phy_bld;
6404 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04006405 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04006406 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04006407 link_info->phy_addr = resp->eee_config_phy_addr &
6408 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04006409 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04006410
Michael Chan170ce012016-04-05 14:08:57 -04006411 if (bp->flags & BNXT_FLAG_EEE_CAP) {
6412 struct ethtool_eee *eee = &bp->eee;
6413 u16 fw_speeds;
6414
6415 eee->eee_active = 0;
6416 if (resp->eee_config_phy_addr &
6417 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
6418 eee->eee_active = 1;
6419 fw_speeds = le16_to_cpu(
6420 resp->link_partner_adv_eee_link_speed_mask);
6421 eee->lp_advertised =
6422 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6423 }
6424
6425 /* Pull initial EEE config */
6426 if (!chng_link_state) {
6427 if (resp->eee_config_phy_addr &
6428 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
6429 eee->eee_enabled = 1;
6430
6431 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
6432 eee->advertised =
6433 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6434
6435 if (resp->eee_config_phy_addr &
6436 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
6437 __le32 tmr;
6438
6439 eee->tx_lpi_enabled = 1;
6440 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
6441 eee->tx_lpi_timer = le32_to_cpu(tmr) &
6442 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
6443 }
6444 }
6445 }
Michael Chane70c7522017-02-12 19:18:16 -05006446
6447 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
6448 if (bp->hwrm_spec_code >= 0x10504)
6449 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
6450
Michael Chanc0c050c2015-10-22 16:01:17 -04006451 /* TODO: need to add more logic to report VF link */
6452 if (chng_link_state) {
6453 if (link_info->phy_link_status == BNXT_LINK_LINK)
6454 link_info->link_up = 1;
6455 else
6456 link_info->link_up = 0;
6457 if (link_up != link_info->link_up)
6458 bnxt_report_link(bp);
6459 } else {
6460 /* alwasy link down if not require to update link state */
6461 link_info->link_up = 0;
6462 }
6463 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05006464
6465 diff = link_info->support_auto_speeds ^ link_info->advertising;
6466 if ((link_info->support_auto_speeds | diff) !=
6467 link_info->support_auto_speeds) {
6468 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05006469 * update the advertisement settings. Caller holds RTNL
6470 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05006471 */
Michael Chan286ef9d2016-11-16 21:13:08 -05006472 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05006473 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05006474 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05006475 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006476 return 0;
6477}
6478
Michael Chan10289be2016-05-15 03:04:49 -04006479static void bnxt_get_port_module_status(struct bnxt *bp)
6480{
6481 struct bnxt_link_info *link_info = &bp->link_info;
6482 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
6483 u8 module_status;
6484
6485 if (bnxt_update_link(bp, true))
6486 return;
6487
6488 module_status = link_info->module_status;
6489 switch (module_status) {
6490 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
6491 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
6492 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
6493 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
6494 bp->pf.port_id);
6495 if (bp->hwrm_spec_code >= 0x10201) {
6496 netdev_warn(bp->dev, "Module part number %s\n",
6497 resp->phy_vendor_partnumber);
6498 }
6499 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
6500 netdev_warn(bp->dev, "TX is disabled\n");
6501 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
6502 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
6503 }
6504}
6505
Michael Chanc0c050c2015-10-22 16:01:17 -04006506static void
6507bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
6508{
6509 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006510 if (bp->hwrm_spec_code >= 0x10201)
6511 req->auto_pause =
6512 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04006513 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6514 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
6515 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04006516 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04006517 req->enables |=
6518 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6519 } else {
6520 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6521 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
6522 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6523 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
6524 req->enables |=
6525 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04006526 if (bp->hwrm_spec_code >= 0x10201) {
6527 req->auto_pause = req->force_pause;
6528 req->enables |= cpu_to_le32(
6529 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6530 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006531 }
6532}
6533
6534static void bnxt_hwrm_set_link_common(struct bnxt *bp,
6535 struct hwrm_port_phy_cfg_input *req)
6536{
6537 u8 autoneg = bp->link_info.autoneg;
6538 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05006539 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04006540
6541 if (autoneg & BNXT_AUTONEG_SPEED) {
6542 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04006543 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04006544
6545 req->enables |= cpu_to_le32(
6546 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
6547 req->auto_link_speed_mask = cpu_to_le16(advertising);
6548
6549 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
6550 req->flags |=
6551 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
6552 } else {
6553 req->force_link_speed = cpu_to_le16(fw_link_speed);
6554 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
6555 }
6556
Michael Chanc0c050c2015-10-22 16:01:17 -04006557 /* tell chimp that the setting takes effect immediately */
6558 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
6559}
6560
6561int bnxt_hwrm_set_pause(struct bnxt *bp)
6562{
6563 struct hwrm_port_phy_cfg_input req = {0};
6564 int rc;
6565
6566 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6567 bnxt_hwrm_set_pause_common(bp, &req);
6568
6569 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
6570 bp->link_info.force_link_chng)
6571 bnxt_hwrm_set_link_common(bp, &req);
6572
6573 mutex_lock(&bp->hwrm_cmd_lock);
6574 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6575 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
6576 /* since changing of pause setting doesn't trigger any link
6577 * change event, the driver needs to update the current pause
6578 * result upon successfully return of the phy_cfg command
6579 */
6580 bp->link_info.pause =
6581 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
6582 bp->link_info.auto_pause_setting = 0;
6583 if (!bp->link_info.force_link_chng)
6584 bnxt_report_link(bp);
6585 }
6586 bp->link_info.force_link_chng = false;
6587 mutex_unlock(&bp->hwrm_cmd_lock);
6588 return rc;
6589}
6590
Michael Chan939f7f02016-04-05 14:08:58 -04006591static void bnxt_hwrm_set_eee(struct bnxt *bp,
6592 struct hwrm_port_phy_cfg_input *req)
6593{
6594 struct ethtool_eee *eee = &bp->eee;
6595
6596 if (eee->eee_enabled) {
6597 u16 eee_speeds;
6598 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
6599
6600 if (eee->tx_lpi_enabled)
6601 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
6602 else
6603 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
6604
6605 req->flags |= cpu_to_le32(flags);
6606 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
6607 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
6608 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
6609 } else {
6610 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
6611 }
6612}
6613
6614int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04006615{
6616 struct hwrm_port_phy_cfg_input req = {0};
6617
6618 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6619 if (set_pause)
6620 bnxt_hwrm_set_pause_common(bp, &req);
6621
6622 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04006623
6624 if (set_eee)
6625 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04006626 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6627}
6628
Michael Chan33f7d552016-04-11 04:11:12 -04006629static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6630{
6631 struct hwrm_port_phy_cfg_input req = {0};
6632
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006633 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04006634 return 0;
6635
6636 if (pci_num_vf(bp->pdev))
6637 return 0;
6638
6639 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05006640 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04006641 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6642}
6643
Michael Chan5ad2cbe2017-01-13 01:32:03 -05006644static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6645{
6646 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6647 struct hwrm_port_led_qcaps_input req = {0};
6648 struct bnxt_pf_info *pf = &bp->pf;
6649 int rc;
6650
6651 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6652 return 0;
6653
6654 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6655 req.port_id = cpu_to_le16(pf->port_id);
6656 mutex_lock(&bp->hwrm_cmd_lock);
6657 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6658 if (rc) {
6659 mutex_unlock(&bp->hwrm_cmd_lock);
6660 return rc;
6661 }
6662 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6663 int i;
6664
6665 bp->num_leds = resp->num_leds;
6666 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6667 bp->num_leds);
6668 for (i = 0; i < bp->num_leds; i++) {
6669 struct bnxt_led_info *led = &bp->leds[i];
6670 __le16 caps = led->led_state_caps;
6671
6672 if (!led->led_group_id ||
6673 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6674 bp->num_leds = 0;
6675 break;
6676 }
6677 }
6678 }
6679 mutex_unlock(&bp->hwrm_cmd_lock);
6680 return 0;
6681}
6682
Michael Chan5282db62017-04-04 18:14:10 -04006683int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6684{
6685 struct hwrm_wol_filter_alloc_input req = {0};
6686 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6687 int rc;
6688
6689 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6690 req.port_id = cpu_to_le16(bp->pf.port_id);
6691 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6692 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6693 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6694 mutex_lock(&bp->hwrm_cmd_lock);
6695 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6696 if (!rc)
6697 bp->wol_filter_id = resp->wol_filter_id;
6698 mutex_unlock(&bp->hwrm_cmd_lock);
6699 return rc;
6700}
6701
6702int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6703{
6704 struct hwrm_wol_filter_free_input req = {0};
6705 int rc;
6706
6707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6708 req.port_id = cpu_to_le16(bp->pf.port_id);
6709 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6710 req.wol_filter_id = bp->wol_filter_id;
6711 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6712 return rc;
6713}
6714
Michael Chanc1ef1462017-04-04 18:14:07 -04006715static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6716{
6717 struct hwrm_wol_filter_qcfg_input req = {0};
6718 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6719 u16 next_handle = 0;
6720 int rc;
6721
6722 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6723 req.port_id = cpu_to_le16(bp->pf.port_id);
6724 req.handle = cpu_to_le16(handle);
6725 mutex_lock(&bp->hwrm_cmd_lock);
6726 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6727 if (!rc) {
6728 next_handle = le16_to_cpu(resp->next_handle);
6729 if (next_handle != 0) {
6730 if (resp->wol_type ==
6731 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6732 bp->wol = 1;
6733 bp->wol_filter_id = resp->wol_filter_id;
6734 }
6735 }
6736 }
6737 mutex_unlock(&bp->hwrm_cmd_lock);
6738 return next_handle;
6739}
6740
6741static void bnxt_get_wol_settings(struct bnxt *bp)
6742{
6743 u16 handle = 0;
6744
6745 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6746 return;
6747
6748 do {
6749 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6750 } while (handle && handle != 0xffff);
6751}
6752
Michael Chan939f7f02016-04-05 14:08:58 -04006753static bool bnxt_eee_config_ok(struct bnxt *bp)
6754{
6755 struct ethtool_eee *eee = &bp->eee;
6756 struct bnxt_link_info *link_info = &bp->link_info;
6757
6758 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6759 return true;
6760
6761 if (eee->eee_enabled) {
6762 u32 advertising =
6763 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6764
6765 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6766 eee->eee_enabled = 0;
6767 return false;
6768 }
6769 if (eee->advertised & ~advertising) {
6770 eee->advertised = advertising & eee->supported;
6771 return false;
6772 }
6773 }
6774 return true;
6775}
6776
Michael Chanc0c050c2015-10-22 16:01:17 -04006777static int bnxt_update_phy_setting(struct bnxt *bp)
6778{
6779 int rc;
6780 bool update_link = false;
6781 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006782 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006783 struct bnxt_link_info *link_info = &bp->link_info;
6784
6785 rc = bnxt_update_link(bp, true);
6786 if (rc) {
6787 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6788 rc);
6789 return rc;
6790 }
Michael Chan33dac242017-02-12 19:18:15 -05006791 if (!BNXT_SINGLE_PF(bp))
6792 return 0;
6793
Michael Chanc0c050c2015-10-22 16:01:17 -04006794 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006795 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6796 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006797 update_pause = true;
6798 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6799 link_info->force_pause_setting != link_info->req_flow_ctrl)
6800 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006801 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6802 if (BNXT_AUTO_MODE(link_info->auto_mode))
6803 update_link = true;
6804 if (link_info->req_link_speed != link_info->force_link_speed)
6805 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006806 if (link_info->req_duplex != link_info->duplex_setting)
6807 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006808 } else {
6809 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6810 update_link = true;
6811 if (link_info->advertising != link_info->auto_link_speeds)
6812 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006813 }
6814
Michael Chan16d663a2016-11-16 21:13:07 -05006815 /* The last close may have shutdown the link, so need to call
6816 * PHY_CFG to bring it back up.
6817 */
6818 if (!netif_carrier_ok(bp->dev))
6819 update_link = true;
6820
Michael Chan939f7f02016-04-05 14:08:58 -04006821 if (!bnxt_eee_config_ok(bp))
6822 update_eee = true;
6823
Michael Chanc0c050c2015-10-22 16:01:17 -04006824 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006825 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006826 else if (update_pause)
6827 rc = bnxt_hwrm_set_pause(bp);
6828 if (rc) {
6829 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6830 rc);
6831 return rc;
6832 }
6833
6834 return rc;
6835}
6836
Jeffrey Huang11809492015-11-05 16:25:49 -05006837/* Common routine to pre-map certain register block to different GRC window.
6838 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6839 * in PF and 3 windows in VF that can be customized to map in different
6840 * register blocks.
6841 */
6842static void bnxt_preset_reg_win(struct bnxt *bp)
6843{
6844 if (BNXT_PF(bp)) {
6845 /* CAG registers map to GRC window #4 */
6846 writel(BNXT_CAG_REG_BASE,
6847 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6848 }
6849}
6850
Michael Chan47558ac2018-04-26 17:44:44 -04006851static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
6852
Michael Chanc0c050c2015-10-22 16:01:17 -04006853static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6854{
6855 int rc = 0;
6856
Jeffrey Huang11809492015-11-05 16:25:49 -05006857 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006858 netif_carrier_off(bp->dev);
6859 if (irq_re_init) {
Michael Chan47558ac2018-04-26 17:44:44 -04006860 /* Reserve rings now if none were reserved at driver probe. */
6861 rc = bnxt_init_dflt_ring_mode(bp);
6862 if (rc) {
6863 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
6864 return rc;
6865 }
Michael Chan674f50a2018-01-17 03:21:09 -05006866 rc = bnxt_reserve_rings(bp);
6867 if (rc)
6868 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006869 }
6870 if ((bp->flags & BNXT_FLAG_RFS) &&
6871 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6872 /* disable RFS if falling back to INTA */
6873 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6874 bp->flags &= ~BNXT_FLAG_RFS;
6875 }
6876
6877 rc = bnxt_alloc_mem(bp, irq_re_init);
6878 if (rc) {
6879 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6880 goto open_err_free_mem;
6881 }
6882
6883 if (irq_re_init) {
6884 bnxt_init_napi(bp);
6885 rc = bnxt_request_irq(bp);
6886 if (rc) {
6887 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6888 goto open_err;
6889 }
6890 }
6891
6892 bnxt_enable_napi(bp);
Andy Gospodarekcabfb092018-04-26 17:44:40 -04006893 bnxt_debug_dev_init(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006894
6895 rc = bnxt_init_nic(bp, irq_re_init);
6896 if (rc) {
6897 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6898 goto open_err;
6899 }
6900
6901 if (link_re_init) {
Michael Chane2dc9b62017-10-13 21:09:30 -04006902 mutex_lock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006903 rc = bnxt_update_phy_setting(bp);
Michael Chane2dc9b62017-10-13 21:09:30 -04006904 mutex_unlock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006905 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006906 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006907 }
6908
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006909 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006910 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006911
Michael Chancaefe522015-12-09 19:35:42 -05006912 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006913 bnxt_enable_int(bp);
6914 /* Enable TX queues */
6915 bnxt_tx_enable(bp);
6916 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006917 /* Poll link status and check for SFP+ module status */
6918 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006919
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006920 /* VF-reps may need to be re-opened after the PF is re-opened */
6921 if (BNXT_PF(bp))
6922 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006923 return 0;
6924
6925open_err:
Andy Gospodarekcabfb092018-04-26 17:44:40 -04006926 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006927 bnxt_disable_napi(bp);
6928 bnxt_del_napi(bp);
6929
6930open_err_free_mem:
6931 bnxt_free_skbs(bp);
6932 bnxt_free_irq(bp);
6933 bnxt_free_mem(bp, true);
6934 return rc;
6935}
6936
6937/* rtnl_lock held */
6938int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6939{
6940 int rc = 0;
6941
6942 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6943 if (rc) {
6944 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6945 dev_close(bp->dev);
6946 }
6947 return rc;
6948}
6949
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006950/* rtnl_lock held, open the NIC half way by allocating all resources, but
6951 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6952 * self tests.
6953 */
6954int bnxt_half_open_nic(struct bnxt *bp)
6955{
6956 int rc = 0;
6957
6958 rc = bnxt_alloc_mem(bp, false);
6959 if (rc) {
6960 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6961 goto half_open_err;
6962 }
6963 rc = bnxt_init_nic(bp, false);
6964 if (rc) {
6965 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6966 goto half_open_err;
6967 }
6968 return 0;
6969
6970half_open_err:
6971 bnxt_free_skbs(bp);
6972 bnxt_free_mem(bp, false);
6973 dev_close(bp->dev);
6974 return rc;
6975}
6976
6977/* rtnl_lock held, this call can only be made after a previous successful
6978 * call to bnxt_half_open_nic().
6979 */
6980void bnxt_half_close_nic(struct bnxt *bp)
6981{
6982 bnxt_hwrm_resource_free(bp, false, false);
6983 bnxt_free_skbs(bp);
6984 bnxt_free_mem(bp, false);
6985}
6986
Michael Chanc0c050c2015-10-22 16:01:17 -04006987static int bnxt_open(struct net_device *dev)
6988{
6989 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006990
Michael Chanc0c050c2015-10-22 16:01:17 -04006991 return __bnxt_open_nic(bp, true, true);
6992}
6993
Michael Chanf9b76eb2017-07-11 13:05:34 -04006994static bool bnxt_drv_busy(struct bnxt *bp)
6995{
6996 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6997 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6998}
6999
Michael Chan86e953d2018-01-17 03:21:04 -05007000static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
7001 bool link_re_init)
Michael Chanc0c050c2015-10-22 16:01:17 -04007002{
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04007003 /* Close the VF-reps before closing PF */
7004 if (BNXT_PF(bp))
7005 bnxt_vf_reps_close(bp);
Michael Chan86e953d2018-01-17 03:21:04 -05007006
Michael Chanc0c050c2015-10-22 16:01:17 -04007007 /* Change device state to avoid TX queue wake up's */
7008 bnxt_tx_disable(bp);
7009
Michael Chancaefe522015-12-09 19:35:42 -05007010 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05007011 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04007012 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05007013 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04007014
Michael Chan9d8bc092016-12-29 12:13:33 -05007015 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04007016 bnxt_shutdown_nic(bp, irq_re_init);
7017
7018 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
7019
Andy Gospodarekcabfb092018-04-26 17:44:40 -04007020 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007021 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007022 del_timer_sync(&bp->timer);
7023 bnxt_free_skbs(bp);
7024
7025 if (irq_re_init) {
7026 bnxt_free_irq(bp);
7027 bnxt_del_napi(bp);
7028 }
7029 bnxt_free_mem(bp, irq_re_init);
Michael Chan86e953d2018-01-17 03:21:04 -05007030}
7031
7032int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7033{
7034 int rc = 0;
7035
7036#ifdef CONFIG_BNXT_SRIOV
7037 if (bp->sriov_cfg) {
7038 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
7039 !bp->sriov_cfg,
7040 BNXT_SRIOV_CFG_WAIT_TMO);
7041 if (rc)
7042 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
7043 }
7044#endif
7045 __bnxt_close_nic(bp, irq_re_init, link_re_init);
Michael Chanc0c050c2015-10-22 16:01:17 -04007046 return rc;
7047}
7048
7049static int bnxt_close(struct net_device *dev)
7050{
7051 struct bnxt *bp = netdev_priv(dev);
7052
7053 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04007054 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007055 return 0;
7056}
7057
7058/* rtnl_lock held */
7059static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7060{
7061 switch (cmd) {
7062 case SIOCGMIIPHY:
7063 /* fallthru */
7064 case SIOCGMIIREG: {
7065 if (!netif_running(dev))
7066 return -EAGAIN;
7067
7068 return 0;
7069 }
7070
7071 case SIOCSMIIREG:
7072 if (!netif_running(dev))
7073 return -EAGAIN;
7074
7075 return 0;
7076
7077 default:
7078 /* do nothing */
7079 break;
7080 }
7081 return -EOPNOTSUPP;
7082}
7083
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007084static void
Michael Chanc0c050c2015-10-22 16:01:17 -04007085bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7086{
7087 u32 i;
7088 struct bnxt *bp = netdev_priv(dev);
7089
Michael Chanf9b76eb2017-07-11 13:05:34 -04007090 set_bit(BNXT_STATE_READ_STATS, &bp->state);
7091 /* Make sure bnxt_close_nic() sees that we are reading stats before
7092 * we check the BNXT_STATE_OPEN flag.
7093 */
7094 smp_mb__after_atomic();
7095 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7096 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007097 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04007098 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007099
7100 /* TODO check if we need to synchronize with bnxt_close path */
7101 for (i = 0; i < bp->cp_nr_rings; i++) {
7102 struct bnxt_napi *bnapi = bp->bnapi[i];
7103 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7104 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
7105
7106 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
7107 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
7108 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
7109
7110 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
7111 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
7112 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
7113
7114 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
7115 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
7116 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
7117
7118 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
7119 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
7120 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
7121
7122 stats->rx_missed_errors +=
7123 le64_to_cpu(hw_stats->rx_discard_pkts);
7124
7125 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
7126
Michael Chanc0c050c2015-10-22 16:01:17 -04007127 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
7128 }
7129
Michael Chan9947f832016-03-07 15:38:46 -05007130 if (bp->flags & BNXT_FLAG_PORT_STATS) {
7131 struct rx_port_stats *rx = bp->hw_rx_port_stats;
7132 struct tx_port_stats *tx = bp->hw_tx_port_stats;
7133
7134 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
7135 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
7136 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
7137 le64_to_cpu(rx->rx_ovrsz_frames) +
7138 le64_to_cpu(rx->rx_runt_frames);
7139 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
7140 le64_to_cpu(rx->rx_jbr_frames);
7141 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
7142 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
7143 stats->tx_errors = le64_to_cpu(tx->tx_err);
7144 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04007145 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007146}
7147
7148static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
7149{
7150 struct net_device *dev = bp->dev;
7151 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7152 struct netdev_hw_addr *ha;
7153 u8 *haddr;
7154 int mc_count = 0;
7155 bool update = false;
7156 int off = 0;
7157
7158 netdev_for_each_mc_addr(ha, dev) {
7159 if (mc_count >= BNXT_MAX_MC_ADDRS) {
7160 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7161 vnic->mc_list_count = 0;
7162 return false;
7163 }
7164 haddr = ha->addr;
7165 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
7166 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
7167 update = true;
7168 }
7169 off += ETH_ALEN;
7170 mc_count++;
7171 }
7172 if (mc_count)
7173 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
7174
7175 if (mc_count != vnic->mc_list_count) {
7176 vnic->mc_list_count = mc_count;
7177 update = true;
7178 }
7179 return update;
7180}
7181
7182static bool bnxt_uc_list_updated(struct bnxt *bp)
7183{
7184 struct net_device *dev = bp->dev;
7185 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7186 struct netdev_hw_addr *ha;
7187 int off = 0;
7188
7189 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
7190 return true;
7191
7192 netdev_for_each_uc_addr(ha, dev) {
7193 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
7194 return true;
7195
7196 off += ETH_ALEN;
7197 }
7198 return false;
7199}
7200
7201static void bnxt_set_rx_mode(struct net_device *dev)
7202{
7203 struct bnxt *bp = netdev_priv(dev);
7204 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7205 u32 mask = vnic->rx_mask;
7206 bool mc_update = false;
7207 bool uc_update;
7208
7209 if (!netif_running(dev))
7210 return;
7211
7212 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
7213 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
7214 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
7215
Michael Chan17c71ac2016-07-01 18:46:27 -04007216 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04007217 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7218
7219 uc_update = bnxt_uc_list_updated(bp);
7220
7221 if (dev->flags & IFF_ALLMULTI) {
7222 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7223 vnic->mc_list_count = 0;
7224 } else {
7225 mc_update = bnxt_mc_list_updated(bp, &mask);
7226 }
7227
7228 if (mask != vnic->rx_mask || uc_update || mc_update) {
7229 vnic->rx_mask = mask;
7230
7231 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007232 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007233 }
7234}
7235
Michael Chanb664f002015-12-02 01:54:08 -05007236static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007237{
7238 struct net_device *dev = bp->dev;
7239 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7240 struct netdev_hw_addr *ha;
7241 int i, off = 0, rc;
7242 bool uc_update;
7243
7244 netif_addr_lock_bh(dev);
7245 uc_update = bnxt_uc_list_updated(bp);
7246 netif_addr_unlock_bh(dev);
7247
7248 if (!uc_update)
7249 goto skip_uc;
7250
7251 mutex_lock(&bp->hwrm_cmd_lock);
7252 for (i = 1; i < vnic->uc_filter_count; i++) {
7253 struct hwrm_cfa_l2_filter_free_input req = {0};
7254
7255 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
7256 -1);
7257
7258 req.l2_filter_id = vnic->fw_l2_filter_id[i];
7259
7260 rc = _hwrm_send_message(bp, &req, sizeof(req),
7261 HWRM_CMD_TIMEOUT);
7262 }
7263 mutex_unlock(&bp->hwrm_cmd_lock);
7264
7265 vnic->uc_filter_count = 1;
7266
7267 netif_addr_lock_bh(dev);
7268 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
7269 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7270 } else {
7271 netdev_for_each_uc_addr(ha, dev) {
7272 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
7273 off += ETH_ALEN;
7274 vnic->uc_filter_count++;
7275 }
7276 }
7277 netif_addr_unlock_bh(dev);
7278
7279 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
7280 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
7281 if (rc) {
7282 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
7283 rc);
7284 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05007285 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007286 }
7287 }
7288
7289skip_uc:
7290 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
7291 if (rc)
7292 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
7293 rc);
Michael Chanb664f002015-12-02 01:54:08 -05007294
7295 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007296}
7297
Michael Chan2773dfb2018-04-26 17:44:42 -04007298static bool bnxt_can_reserve_rings(struct bnxt *bp)
7299{
7300#ifdef CONFIG_BNXT_SRIOV
7301 if ((bp->flags & BNXT_FLAG_NEW_RM) && BNXT_VF(bp)) {
7302 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7303
7304 /* No minimum rings were provisioned by the PF. Don't
7305 * reserve rings by default when device is down.
7306 */
7307 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
7308 return true;
7309
7310 if (!netif_running(bp->dev))
7311 return false;
7312 }
7313#endif
7314 return true;
7315}
7316
Michael Chan8079e8f2016-12-29 12:13:37 -05007317/* If the chip and firmware supports RFS */
7318static bool bnxt_rfs_supported(struct bnxt *bp)
7319{
7320 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
7321 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05007322 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7323 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05007324 return false;
7325}
7326
7327/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007328static bool bnxt_rfs_capable(struct bnxt *bp)
7329{
7330#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05007331 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007332
Michael Chan2773dfb2018-04-26 17:44:42 -04007333 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007334 return false;
7335
7336 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05007337 max_vnics = bnxt_get_max_func_vnics(bp);
7338 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05007339
7340 /* RSS contexts not a limiting factor */
7341 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7342 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05007343 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Michael Chan6a1eef52018-01-17 03:21:10 -05007344 if (bp->rx_nr_rings > 1)
7345 netdev_warn(bp->dev,
7346 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
7347 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007348 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04007349 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007350
Michael Chan6a1eef52018-01-17 03:21:10 -05007351 if (!(bp->flags & BNXT_FLAG_NEW_RM))
7352 return true;
7353
7354 if (vnics == bp->hw_resc.resv_vnics)
7355 return true;
7356
7357 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, vnics);
7358 if (vnics <= bp->hw_resc.resv_vnics)
7359 return true;
7360
7361 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
7362 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 1);
7363 return false;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007364#else
7365 return false;
7366#endif
7367}
7368
Michael Chanc0c050c2015-10-22 16:01:17 -04007369static netdev_features_t bnxt_fix_features(struct net_device *dev,
7370 netdev_features_t features)
7371{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007372 struct bnxt *bp = netdev_priv(dev);
7373
Vasundhara Volama2304902016-07-25 12:33:36 -04007374 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007375 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04007376
Michael Chan1054aee2017-12-16 03:09:42 -05007377 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7378 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
7379
7380 if (!(features & NETIF_F_GRO))
7381 features &= ~NETIF_F_GRO_HW;
7382
7383 if (features & NETIF_F_GRO_HW)
7384 features &= ~NETIF_F_LRO;
7385
Michael Chan5a9f6b22016-06-06 02:37:15 -04007386 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
7387 * turned on or off together.
7388 */
7389 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
7390 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
7391 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
7392 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7393 NETIF_F_HW_VLAN_STAG_RX);
7394 else
7395 features |= NETIF_F_HW_VLAN_CTAG_RX |
7396 NETIF_F_HW_VLAN_STAG_RX;
7397 }
Michael Chancf6645f2016-06-13 02:25:28 -04007398#ifdef CONFIG_BNXT_SRIOV
7399 if (BNXT_VF(bp)) {
7400 if (bp->vf.vlan) {
7401 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7402 NETIF_F_HW_VLAN_STAG_RX);
7403 }
7404 }
7405#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04007406 return features;
7407}
7408
7409static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
7410{
7411 struct bnxt *bp = netdev_priv(dev);
7412 u32 flags = bp->flags;
7413 u32 changes;
7414 int rc = 0;
7415 bool re_init = false;
7416 bool update_tpa = false;
7417
7418 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Michael Chan1054aee2017-12-16 03:09:42 -05007419 if (features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04007420 flags |= BNXT_FLAG_GRO;
Michael Chan1054aee2017-12-16 03:09:42 -05007421 else if (features & NETIF_F_LRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04007422 flags |= BNXT_FLAG_LRO;
7423
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007424 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7425 flags &= ~BNXT_FLAG_TPA;
7426
Michael Chanc0c050c2015-10-22 16:01:17 -04007427 if (features & NETIF_F_HW_VLAN_CTAG_RX)
7428 flags |= BNXT_FLAG_STRIP_VLAN;
7429
7430 if (features & NETIF_F_NTUPLE)
7431 flags |= BNXT_FLAG_RFS;
7432
7433 changes = flags ^ bp->flags;
7434 if (changes & BNXT_FLAG_TPA) {
7435 update_tpa = true;
7436 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
7437 (flags & BNXT_FLAG_TPA) == 0)
7438 re_init = true;
7439 }
7440
7441 if (changes & ~BNXT_FLAG_TPA)
7442 re_init = true;
7443
7444 if (flags != bp->flags) {
7445 u32 old_flags = bp->flags;
7446
7447 bp->flags = flags;
7448
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007449 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007450 if (update_tpa)
7451 bnxt_set_ring_params(bp);
7452 return rc;
7453 }
7454
7455 if (re_init) {
7456 bnxt_close_nic(bp, false, false);
7457 if (update_tpa)
7458 bnxt_set_ring_params(bp);
7459
7460 return bnxt_open_nic(bp, false, false);
7461 }
7462 if (update_tpa) {
7463 rc = bnxt_set_tpa(bp,
7464 (flags & BNXT_FLAG_TPA) ?
7465 true : false);
7466 if (rc)
7467 bp->flags = old_flags;
7468 }
7469 }
7470 return rc;
7471}
7472
Michael Chan9f554592016-01-02 23:44:58 -05007473static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
7474{
Michael Chanb6ab4b02016-01-02 23:44:59 -05007475 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05007476 int i = bnapi->index;
7477
Michael Chan3b2b7d92016-01-02 23:45:00 -05007478 if (!txr)
7479 return;
7480
Michael Chan9f554592016-01-02 23:44:58 -05007481 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
7482 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
7483 txr->tx_cons);
7484}
7485
7486static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
7487{
Michael Chanb6ab4b02016-01-02 23:44:59 -05007488 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05007489 int i = bnapi->index;
7490
Michael Chan3b2b7d92016-01-02 23:45:00 -05007491 if (!rxr)
7492 return;
7493
Michael Chan9f554592016-01-02 23:44:58 -05007494 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",
7495 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
7496 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
7497 rxr->rx_sw_agg_prod);
7498}
7499
7500static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
7501{
7502 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7503 int i = bnapi->index;
7504
7505 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
7506 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
7507}
7508
Michael Chanc0c050c2015-10-22 16:01:17 -04007509static void bnxt_dbg_dump_states(struct bnxt *bp)
7510{
7511 int i;
7512 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04007513
7514 for (i = 0; i < bp->cp_nr_rings; i++) {
7515 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007516 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05007517 bnxt_dump_tx_sw_state(bnapi);
7518 bnxt_dump_rx_sw_state(bnapi);
7519 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04007520 }
7521 }
7522}
7523
Michael Chan6988bd92016-06-13 02:25:29 -04007524static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04007525{
Michael Chan6988bd92016-06-13 02:25:29 -04007526 if (!silent)
7527 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05007528 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05007529 int rc;
7530
7531 if (!silent)
7532 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05007533 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05007534 rc = bnxt_open_nic(bp, false, false);
7535 if (!silent && !rc)
7536 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05007537 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007538}
7539
7540static void bnxt_tx_timeout(struct net_device *dev)
7541{
7542 struct bnxt *bp = netdev_priv(dev);
7543
7544 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
7545 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007546 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007547}
7548
7549#ifdef CONFIG_NET_POLL_CONTROLLER
7550static void bnxt_poll_controller(struct net_device *dev)
7551{
7552 struct bnxt *bp = netdev_priv(dev);
7553 int i;
7554
Michael Chan2270bc52017-06-23 14:01:01 -04007555 /* Only process tx rings/combined rings in netpoll mode. */
7556 for (i = 0; i < bp->tx_nr_rings; i++) {
7557 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007558
Michael Chan2270bc52017-06-23 14:01:01 -04007559 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04007560 }
7561}
7562#endif
7563
Kees Cooke99e88a2017-10-16 14:43:17 -07007564static void bnxt_timer(struct timer_list *t)
Michael Chanc0c050c2015-10-22 16:01:17 -04007565{
Kees Cooke99e88a2017-10-16 14:43:17 -07007566 struct bnxt *bp = from_timer(bp, t, timer);
Michael Chanc0c050c2015-10-22 16:01:17 -04007567 struct net_device *dev = bp->dev;
7568
7569 if (!netif_running(dev))
7570 return;
7571
7572 if (atomic_read(&bp->intr_sem) != 0)
7573 goto bnxt_restart_timer;
7574
Michael Chanadcc3312017-07-24 12:34:24 -04007575 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
7576 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05007577 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007578 bnxt_queue_sp_work(bp);
Michael Chan3bdf56c2016-03-07 15:38:45 -05007579 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04007580
7581 if (bnxt_tc_flower_enabled(bp)) {
7582 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
7583 bnxt_queue_sp_work(bp);
7584 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007585bnxt_restart_timer:
7586 mod_timer(&bp->timer, jiffies + bp->current_interval);
7587}
7588
Michael Chana551ee92017-01-25 02:55:07 -05007589static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04007590{
Michael Chana551ee92017-01-25 02:55:07 -05007591 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
7592 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04007593 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
7594 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
7595 */
7596 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7597 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05007598}
7599
7600static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
7601{
Michael Chan6988bd92016-06-13 02:25:29 -04007602 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7603 rtnl_unlock();
7604}
7605
Michael Chana551ee92017-01-25 02:55:07 -05007606/* Only called from bnxt_sp_task() */
7607static void bnxt_reset(struct bnxt *bp, bool silent)
7608{
7609 bnxt_rtnl_lock_sp(bp);
7610 if (test_bit(BNXT_STATE_OPEN, &bp->state))
7611 bnxt_reset_task(bp, silent);
7612 bnxt_rtnl_unlock_sp(bp);
7613}
7614
Michael Chanc0c050c2015-10-22 16:01:17 -04007615static void bnxt_cfg_ntp_filters(struct bnxt *);
7616
7617static void bnxt_sp_task(struct work_struct *work)
7618{
7619 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007620
Michael Chan4cebdce2015-12-09 19:35:43 -05007621 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7622 smp_mb__after_atomic();
7623 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7624 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007625 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05007626 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007627
7628 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
7629 bnxt_cfg_rx_mode(bp);
7630
7631 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
7632 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007633 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
7634 bnxt_hwrm_exec_fwd_req(bp);
7635 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7636 bnxt_hwrm_tunnel_dst_port_alloc(
7637 bp, bp->vxlan_port,
7638 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7639 }
7640 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7641 bnxt_hwrm_tunnel_dst_port_free(
7642 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7643 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007644 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7645 bnxt_hwrm_tunnel_dst_port_alloc(
7646 bp, bp->nge_port,
7647 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7648 }
7649 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7650 bnxt_hwrm_tunnel_dst_port_free(
7651 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7652 }
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04007653 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05007654 bnxt_hwrm_port_qstats(bp);
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04007655 bnxt_hwrm_port_qstats_ext(bp);
7656 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05007657
Michael Chan0eaa24b2017-01-25 02:55:08 -05007658 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007659 int rc;
Michael Chan0eaa24b2017-01-25 02:55:08 -05007660
Michael Chane2dc9b62017-10-13 21:09:30 -04007661 mutex_lock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007662 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
7663 &bp->sp_event))
7664 bnxt_hwrm_phy_qcaps(bp);
7665
Michael Chane2dc9b62017-10-13 21:09:30 -04007666 rc = bnxt_update_link(bp, true);
7667 mutex_unlock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007668 if (rc)
7669 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
7670 rc);
7671 }
Michael Chan90c694b2017-01-25 02:55:09 -05007672 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007673 mutex_lock(&bp->link_lock);
7674 bnxt_get_port_module_status(bp);
7675 mutex_unlock(&bp->link_lock);
Michael Chan90c694b2017-01-25 02:55:09 -05007676 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04007677
7678 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
7679 bnxt_tc_flow_stats_work(bp);
7680
Michael Chane2dc9b62017-10-13 21:09:30 -04007681 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
7682 * must be the last functions to be called before exiting.
7683 */
Michael Chanc0c050c2015-10-22 16:01:17 -04007684 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
7685 bnxt_reset(bp, false);
7686
7687 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
7688 bnxt_reset(bp, true);
7689
Michael Chanc0c050c2015-10-22 16:01:17 -04007690 smp_mb__before_atomic();
7691 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7692}
7693
Michael Chand1e79252017-02-06 16:55:38 -05007694/* Under rtnl_lock */
Michael Chan98fdbe72017-08-28 13:40:26 -04007695int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7696 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05007697{
7698 int max_rx, max_tx, tx_sets = 1;
7699 int tx_rings_needed;
Michael Chan8f23d632018-01-17 03:21:12 -05007700 int rx_rings = rx;
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05007701 int cp, vnics, rc;
Michael Chand1e79252017-02-06 16:55:38 -05007702
Michael Chand1e79252017-02-06 16:55:38 -05007703 if (tcs)
7704 tx_sets = tcs;
7705
7706 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7707 if (rc)
7708 return rc;
7709
7710 if (max_rx < rx)
7711 return -ENOMEM;
7712
Michael Chan5f449242017-02-06 16:55:40 -05007713 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05007714 if (max_tx < tx_rings_needed)
7715 return -ENOMEM;
7716
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05007717 vnics = 1;
7718 if (bp->flags & BNXT_FLAG_RFS)
7719 vnics += rx_rings;
7720
Michael Chan8f23d632018-01-17 03:21:12 -05007721 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7722 rx_rings <<= 1;
7723 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
Michael Chan11c3ec72018-04-11 11:50:17 -04007724 if (bp->flags & BNXT_FLAG_NEW_RM)
7725 cp += bnxt_get_ulp_msix_num(bp);
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05007726 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
7727 vnics);
Michael Chand1e79252017-02-06 16:55:38 -05007728}
7729
Sathya Perla17086392017-02-20 19:25:18 -05007730static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7731{
7732 if (bp->bar2) {
7733 pci_iounmap(pdev, bp->bar2);
7734 bp->bar2 = NULL;
7735 }
7736
7737 if (bp->bar1) {
7738 pci_iounmap(pdev, bp->bar1);
7739 bp->bar1 = NULL;
7740 }
7741
7742 if (bp->bar0) {
7743 pci_iounmap(pdev, bp->bar0);
7744 bp->bar0 = NULL;
7745 }
7746}
7747
7748static void bnxt_cleanup_pci(struct bnxt *bp)
7749{
7750 bnxt_unmap_bars(bp, bp->pdev);
7751 pci_release_regions(bp->pdev);
7752 pci_disable_device(bp->pdev);
7753}
7754
Michael Chan18775aa2017-10-26 11:51:27 -04007755static void bnxt_init_dflt_coal(struct bnxt *bp)
7756{
7757 struct bnxt_coal *coal;
7758
7759 /* Tick values in micro seconds.
7760 * 1 coal_buf x bufs_per_record = 1 completion record.
7761 */
7762 coal = &bp->rx_coal;
7763 coal->coal_ticks = 14;
7764 coal->coal_bufs = 30;
7765 coal->coal_ticks_irq = 1;
7766 coal->coal_bufs_irq = 2;
Andy Gospodarek05abe4dd2018-04-26 17:44:38 -04007767 coal->idle_thresh = 50;
Michael Chan18775aa2017-10-26 11:51:27 -04007768 coal->bufs_per_record = 2;
7769 coal->budget = 64; /* NAPI budget */
7770
7771 coal = &bp->tx_coal;
7772 coal->coal_ticks = 28;
7773 coal->coal_bufs = 30;
7774 coal->coal_ticks_irq = 2;
7775 coal->coal_bufs_irq = 2;
7776 coal->bufs_per_record = 1;
7777
7778 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7779}
7780
Michael Chanc0c050c2015-10-22 16:01:17 -04007781static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7782{
7783 int rc;
7784 struct bnxt *bp = netdev_priv(dev);
7785
7786 SET_NETDEV_DEV(dev, &pdev->dev);
7787
7788 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7789 rc = pci_enable_device(pdev);
7790 if (rc) {
7791 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7792 goto init_err;
7793 }
7794
7795 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7796 dev_err(&pdev->dev,
7797 "Cannot find PCI device base address, aborting\n");
7798 rc = -ENODEV;
7799 goto init_err_disable;
7800 }
7801
7802 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7803 if (rc) {
7804 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7805 goto init_err_disable;
7806 }
7807
7808 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7809 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7810 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7811 goto init_err_disable;
7812 }
7813
7814 pci_set_master(pdev);
7815
7816 bp->dev = dev;
7817 bp->pdev = pdev;
7818
7819 bp->bar0 = pci_ioremap_bar(pdev, 0);
7820 if (!bp->bar0) {
7821 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7822 rc = -ENOMEM;
7823 goto init_err_release;
7824 }
7825
7826 bp->bar1 = pci_ioremap_bar(pdev, 2);
7827 if (!bp->bar1) {
7828 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7829 rc = -ENOMEM;
7830 goto init_err_release;
7831 }
7832
7833 bp->bar2 = pci_ioremap_bar(pdev, 4);
7834 if (!bp->bar2) {
7835 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7836 rc = -ENOMEM;
7837 goto init_err_release;
7838 }
7839
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007840 pci_enable_pcie_error_reporting(pdev);
7841
Michael Chanc0c050c2015-10-22 16:01:17 -04007842 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7843
7844 spin_lock_init(&bp->ntp_fltr_lock);
7845
7846 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7847 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7848
Michael Chan18775aa2017-10-26 11:51:27 -04007849 bnxt_init_dflt_coal(bp);
Michael Chan51f30782016-07-01 18:46:29 -04007850
Kees Cooke99e88a2017-10-16 14:43:17 -07007851 timer_setup(&bp->timer, bnxt_timer, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04007852 bp->current_interval = BNXT_TIMER_INTERVAL;
7853
Michael Chancaefe522015-12-09 19:35:42 -05007854 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007855 return 0;
7856
7857init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007858 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007859 pci_release_regions(pdev);
7860
7861init_err_disable:
7862 pci_disable_device(pdev);
7863
7864init_err:
7865 return rc;
7866}
7867
7868/* rtnl_lock held */
7869static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7870{
7871 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007872 struct bnxt *bp = netdev_priv(dev);
7873 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007874
7875 if (!is_valid_ether_addr(addr->sa_data))
7876 return -EADDRNOTAVAIL;
7877
Michael Chanc1a7bdf2017-10-26 11:51:24 -04007878 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7879 return 0;
7880
Michael Chan84c33dd2016-04-11 04:11:13 -04007881 rc = bnxt_approve_mac(bp, addr->sa_data);
7882 if (rc)
7883 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007884
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007885 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7886 if (netif_running(dev)) {
7887 bnxt_close_nic(bp, false, false);
7888 rc = bnxt_open_nic(bp, false, false);
7889 }
7890
7891 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007892}
7893
7894/* rtnl_lock held */
7895static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7896{
7897 struct bnxt *bp = netdev_priv(dev);
7898
Michael Chanc0c050c2015-10-22 16:01:17 -04007899 if (netif_running(dev))
7900 bnxt_close_nic(bp, false, false);
7901
7902 dev->mtu = new_mtu;
7903 bnxt_set_ring_params(bp);
7904
7905 if (netif_running(dev))
7906 return bnxt_open_nic(bp, false, false);
7907
7908 return 0;
7909}
7910
Michael Chanc5e3deb2016-12-02 21:17:15 -05007911int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007912{
7913 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007914 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007915 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007916
Michael Chanc0c050c2015-10-22 16:01:17 -04007917 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007918 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007919 tc, bp->max_tc);
7920 return -EINVAL;
7921 }
7922
7923 if (netdev_get_num_tc(dev) == tc)
7924 return 0;
7925
Michael Chan3ffb6a32016-11-11 00:11:42 -05007926 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7927 sh = true;
7928
Michael Chan98fdbe72017-08-28 13:40:26 -04007929 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7930 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007931 if (rc)
7932 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007933
7934 /* Needs to close the device and do hw resource re-allocations */
7935 if (netif_running(bp->dev))
7936 bnxt_close_nic(bp, true, false);
7937
7938 if (tc) {
7939 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7940 netdev_set_num_tc(dev, tc);
7941 } else {
7942 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7943 netdev_reset_tc(dev);
7944 }
Michael Chan87e9b372017-08-23 19:34:03 -04007945 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
Michael Chan3ffb6a32016-11-11 00:11:42 -05007946 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7947 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007948 bp->num_stat_ctxs = bp->cp_nr_rings;
7949
7950 if (netif_running(bp->dev))
7951 return bnxt_open_nic(bp, true, false);
7952
7953 return 0;
7954}
7955
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007956static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7957 void *cb_priv)
Sathya Perla2ae74082017-08-28 13:40:33 -04007958{
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007959 struct bnxt *bp = cb_priv;
Sathya Perla2ae74082017-08-28 13:40:33 -04007960
Jakub Kicinski312324f2018-01-25 14:00:48 -08007961 if (!bnxt_tc_flower_enabled(bp) ||
7962 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
Sathya Perla2ae74082017-08-28 13:40:33 -04007963 return -EOPNOTSUPP;
7964
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007965 switch (type) {
7966 case TC_SETUP_CLSFLOWER:
7967 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
7968 default:
7969 return -EOPNOTSUPP;
7970 }
7971}
7972
7973static int bnxt_setup_tc_block(struct net_device *dev,
7974 struct tc_block_offload *f)
7975{
7976 struct bnxt *bp = netdev_priv(dev);
7977
7978 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7979 return -EOPNOTSUPP;
7980
7981 switch (f->command) {
7982 case TC_BLOCK_BIND:
7983 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
7984 bp, bp);
7985 case TC_BLOCK_UNBIND:
7986 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
7987 return 0;
7988 default:
7989 return -EOPNOTSUPP;
7990 }
Sathya Perla2ae74082017-08-28 13:40:33 -04007991}
7992
Jiri Pirko2572ac52017-08-07 10:15:17 +02007993static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02007994 void *type_data)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007995{
Sathya Perla2ae74082017-08-28 13:40:33 -04007996 switch (type) {
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007997 case TC_SETUP_BLOCK:
7998 return bnxt_setup_tc_block(dev, type_data);
Nogah Frankel575ed7d2017-11-06 07:23:42 +01007999 case TC_SETUP_QDISC_MQPRIO: {
Sathya Perla2ae74082017-08-28 13:40:33 -04008000 struct tc_mqprio_qopt *mqprio = type_data;
Jiri Pirkode4784c2017-08-07 10:15:32 +02008001
Sathya Perla2ae74082017-08-28 13:40:33 -04008002 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
8003
8004 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
8005 }
8006 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02008007 return -EOPNOTSUPP;
Sathya Perla2ae74082017-08-28 13:40:33 -04008008 }
Michael Chanc5e3deb2016-12-02 21:17:15 -05008009}
8010
Michael Chanc0c050c2015-10-22 16:01:17 -04008011#ifdef CONFIG_RFS_ACCEL
8012static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
8013 struct bnxt_ntuple_filter *f2)
8014{
8015 struct flow_keys *keys1 = &f1->fkeys;
8016 struct flow_keys *keys2 = &f2->fkeys;
8017
8018 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
8019 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
8020 keys1->ports.ports == keys2->ports.ports &&
8021 keys1->basic.ip_proto == keys2->basic.ip_proto &&
8022 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05008023 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04008024 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
8025 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04008026 return true;
8027
8028 return false;
8029}
8030
8031static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
8032 u16 rxq_index, u32 flow_id)
8033{
8034 struct bnxt *bp = netdev_priv(dev);
8035 struct bnxt_ntuple_filter *fltr, *new_fltr;
8036 struct flow_keys *fkeys;
8037 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04008038 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04008039 struct hlist_head *head;
8040
Michael Chana54c4d72016-07-25 12:33:35 -04008041 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
8042 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8043 int off = 0, j;
8044
8045 netif_addr_lock_bh(dev);
8046 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
8047 if (ether_addr_equal(eth->h_dest,
8048 vnic->uc_list + off)) {
8049 l2_idx = j + 1;
8050 break;
8051 }
8052 }
8053 netif_addr_unlock_bh(dev);
8054 if (!l2_idx)
8055 return -EINVAL;
8056 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008057 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
8058 if (!new_fltr)
8059 return -ENOMEM;
8060
8061 fkeys = &new_fltr->fkeys;
8062 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
8063 rc = -EPROTONOSUPPORT;
8064 goto err_free;
8065 }
8066
Michael Chandda0e742016-12-29 12:13:40 -05008067 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
8068 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04008069 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
8070 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
8071 rc = -EPROTONOSUPPORT;
8072 goto err_free;
8073 }
Michael Chandda0e742016-12-29 12:13:40 -05008074 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
8075 bp->hwrm_spec_code < 0x10601) {
8076 rc = -EPROTONOSUPPORT;
8077 goto err_free;
8078 }
Michael Chan61aad722017-02-12 19:18:14 -05008079 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
8080 bp->hwrm_spec_code < 0x10601) {
8081 rc = -EPROTONOSUPPORT;
8082 goto err_free;
8083 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008084
Michael Chana54c4d72016-07-25 12:33:35 -04008085 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04008086 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
8087
8088 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
8089 head = &bp->ntp_fltr_hash_tbl[idx];
8090 rcu_read_lock();
8091 hlist_for_each_entry_rcu(fltr, head, hash) {
8092 if (bnxt_fltr_match(fltr, new_fltr)) {
8093 rcu_read_unlock();
8094 rc = 0;
8095 goto err_free;
8096 }
8097 }
8098 rcu_read_unlock();
8099
8100 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05008101 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
8102 BNXT_NTP_FLTR_MAX_FLTR, 0);
8103 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04008104 spin_unlock_bh(&bp->ntp_fltr_lock);
8105 rc = -ENOMEM;
8106 goto err_free;
8107 }
8108
Michael Chan84e86b92015-11-05 16:25:50 -05008109 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04008110 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04008111 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04008112 new_fltr->rxq = rxq_index;
8113 hlist_add_head_rcu(&new_fltr->hash, head);
8114 bp->ntp_fltr_count++;
8115 spin_unlock_bh(&bp->ntp_fltr_lock);
8116
8117 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008118 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008119
8120 return new_fltr->sw_id;
8121
8122err_free:
8123 kfree(new_fltr);
8124 return rc;
8125}
8126
8127static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8128{
8129 int i;
8130
8131 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
8132 struct hlist_head *head;
8133 struct hlist_node *tmp;
8134 struct bnxt_ntuple_filter *fltr;
8135 int rc;
8136
8137 head = &bp->ntp_fltr_hash_tbl[i];
8138 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
8139 bool del = false;
8140
8141 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
8142 if (rps_may_expire_flow(bp->dev, fltr->rxq,
8143 fltr->flow_id,
8144 fltr->sw_id)) {
8145 bnxt_hwrm_cfa_ntuple_filter_free(bp,
8146 fltr);
8147 del = true;
8148 }
8149 } else {
8150 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
8151 fltr);
8152 if (rc)
8153 del = true;
8154 else
8155 set_bit(BNXT_FLTR_VALID, &fltr->state);
8156 }
8157
8158 if (del) {
8159 spin_lock_bh(&bp->ntp_fltr_lock);
8160 hlist_del_rcu(&fltr->hash);
8161 bp->ntp_fltr_count--;
8162 spin_unlock_bh(&bp->ntp_fltr_lock);
8163 synchronize_rcu();
8164 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
8165 kfree(fltr);
8166 }
8167 }
8168 }
Jeffrey Huang19241362016-02-26 04:00:00 -05008169 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
8170 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04008171}
8172
8173#else
8174
8175static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8176{
8177}
8178
8179#endif /* CONFIG_RFS_ACCEL */
8180
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008181static void bnxt_udp_tunnel_add(struct net_device *dev,
8182 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04008183{
8184 struct bnxt *bp = netdev_priv(dev);
8185
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008186 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8187 return;
8188
Michael Chanc0c050c2015-10-22 16:01:17 -04008189 if (!netif_running(dev))
8190 return;
8191
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008192 switch (ti->type) {
8193 case UDP_TUNNEL_TYPE_VXLAN:
8194 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
8195 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04008196
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008197 bp->vxlan_port_cnt++;
8198 if (bp->vxlan_port_cnt == 1) {
8199 bp->vxlan_port = ti->port;
8200 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008201 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008202 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008203 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07008204 case UDP_TUNNEL_TYPE_GENEVE:
8205 if (bp->nge_port_cnt && bp->nge_port != ti->port)
8206 return;
8207
8208 bp->nge_port_cnt++;
8209 if (bp->nge_port_cnt == 1) {
8210 bp->nge_port = ti->port;
8211 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
8212 }
8213 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008214 default:
8215 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04008216 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008217
Michael Chanc213eae2017-10-13 21:09:29 -04008218 bnxt_queue_sp_work(bp);
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008219}
8220
8221static void bnxt_udp_tunnel_del(struct net_device *dev,
8222 struct udp_tunnel_info *ti)
8223{
8224 struct bnxt *bp = netdev_priv(dev);
8225
8226 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8227 return;
8228
8229 if (!netif_running(dev))
8230 return;
8231
8232 switch (ti->type) {
8233 case UDP_TUNNEL_TYPE_VXLAN:
8234 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
8235 return;
8236 bp->vxlan_port_cnt--;
8237
8238 if (bp->vxlan_port_cnt != 0)
8239 return;
8240
8241 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
8242 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07008243 case UDP_TUNNEL_TYPE_GENEVE:
8244 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
8245 return;
8246 bp->nge_port_cnt--;
8247
8248 if (bp->nge_port_cnt != 0)
8249 return;
8250
8251 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
8252 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008253 default:
8254 return;
8255 }
8256
Michael Chanc213eae2017-10-13 21:09:29 -04008257 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008258}
8259
Michael Chan39d8ba22017-07-24 12:34:22 -04008260static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8261 struct net_device *dev, u32 filter_mask,
8262 int nlflags)
8263{
8264 struct bnxt *bp = netdev_priv(dev);
8265
8266 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
8267 nlflags, filter_mask, NULL);
8268}
8269
8270static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8271 u16 flags)
8272{
8273 struct bnxt *bp = netdev_priv(dev);
8274 struct nlattr *attr, *br_spec;
8275 int rem, rc = 0;
8276
8277 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
8278 return -EOPNOTSUPP;
8279
8280 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8281 if (!br_spec)
8282 return -EINVAL;
8283
8284 nla_for_each_nested(attr, br_spec, rem) {
8285 u16 mode;
8286
8287 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8288 continue;
8289
8290 if (nla_len(attr) < sizeof(mode))
8291 return -EINVAL;
8292
8293 mode = nla_get_u16(attr);
8294 if (mode == bp->br_mode)
8295 break;
8296
8297 rc = bnxt_hwrm_set_br_mode(bp, mode);
8298 if (!rc)
8299 bp->br_mode = mode;
8300 break;
8301 }
8302 return rc;
8303}
8304
Sathya Perlac124a622017-07-24 12:34:29 -04008305static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
8306 size_t len)
8307{
8308 struct bnxt *bp = netdev_priv(dev);
8309 int rc;
8310
8311 /* The PF and it's VF-reps only support the switchdev framework */
8312 if (!BNXT_PF(bp))
8313 return -EOPNOTSUPP;
8314
Sathya Perla53f70b82017-07-25 13:28:41 -04008315 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04008316
8317 if (rc >= len)
8318 return -EOPNOTSUPP;
8319 return 0;
8320}
8321
8322int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
8323{
8324 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
8325 return -EOPNOTSUPP;
8326
8327 /* The PF and it's VF-reps only support the switchdev framework */
8328 if (!BNXT_PF(bp))
8329 return -EOPNOTSUPP;
8330
8331 switch (attr->id) {
8332 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Sathya Perladd4ea1d2018-01-17 03:21:16 -05008333 attr->u.ppid.id_len = sizeof(bp->switch_id);
8334 memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
Sathya Perlac124a622017-07-24 12:34:29 -04008335 break;
8336 default:
8337 return -EOPNOTSUPP;
8338 }
8339 return 0;
8340}
8341
8342static int bnxt_swdev_port_attr_get(struct net_device *dev,
8343 struct switchdev_attr *attr)
8344{
8345 return bnxt_port_attr_get(netdev_priv(dev), attr);
8346}
8347
8348static const struct switchdev_ops bnxt_switchdev_ops = {
8349 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
8350};
8351
Michael Chanc0c050c2015-10-22 16:01:17 -04008352static const struct net_device_ops bnxt_netdev_ops = {
8353 .ndo_open = bnxt_open,
8354 .ndo_start_xmit = bnxt_start_xmit,
8355 .ndo_stop = bnxt_close,
8356 .ndo_get_stats64 = bnxt_get_stats64,
8357 .ndo_set_rx_mode = bnxt_set_rx_mode,
8358 .ndo_do_ioctl = bnxt_ioctl,
8359 .ndo_validate_addr = eth_validate_addr,
8360 .ndo_set_mac_address = bnxt_change_mac_addr,
8361 .ndo_change_mtu = bnxt_change_mtu,
8362 .ndo_fix_features = bnxt_fix_features,
8363 .ndo_set_features = bnxt_set_features,
8364 .ndo_tx_timeout = bnxt_tx_timeout,
8365#ifdef CONFIG_BNXT_SRIOV
8366 .ndo_get_vf_config = bnxt_get_vf_config,
8367 .ndo_set_vf_mac = bnxt_set_vf_mac,
8368 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
8369 .ndo_set_vf_rate = bnxt_set_vf_bw,
8370 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
8371 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
Vasundhara Volam746df132018-03-31 13:54:10 -04008372 .ndo_set_vf_trust = bnxt_set_vf_trust,
Michael Chanc0c050c2015-10-22 16:01:17 -04008373#endif
8374#ifdef CONFIG_NET_POLL_CONTROLLER
8375 .ndo_poll_controller = bnxt_poll_controller,
8376#endif
8377 .ndo_setup_tc = bnxt_setup_tc,
8378#ifdef CONFIG_RFS_ACCEL
8379 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
8380#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008381 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
8382 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07008383 .ndo_bpf = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04008384 .ndo_bridge_getlink = bnxt_bridge_getlink,
8385 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04008386 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04008387};
8388
8389static void bnxt_remove_one(struct pci_dev *pdev)
8390{
8391 struct net_device *dev = pci_get_drvdata(pdev);
8392 struct bnxt *bp = netdev_priv(dev);
8393
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008394 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04008395 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008396 bnxt_dl_unregister(bp);
8397 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008398
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008399 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04008400 unregister_netdev(dev);
Sathya Perla2ae74082017-08-28 13:40:33 -04008401 bnxt_shutdown_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04008402 bnxt_cancel_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008403 bp->sp_event = 0;
8404
Michael Chan78095922016-12-07 00:26:16 -05008405 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05008406 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008407 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04008408 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04008409 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05008410 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05008411 kfree(bp->edev);
8412 bp->edev = NULL;
Sathya Perla17086392017-02-20 19:25:18 -05008413 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008414 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04008415}
8416
8417static int bnxt_probe_phy(struct bnxt *bp)
8418{
8419 int rc = 0;
8420 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04008421
Michael Chan170ce012016-04-05 14:08:57 -04008422 rc = bnxt_hwrm_phy_qcaps(bp);
8423 if (rc) {
8424 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
8425 rc);
8426 return rc;
8427 }
Michael Chane2dc9b62017-10-13 21:09:30 -04008428 mutex_init(&bp->link_lock);
Michael Chan170ce012016-04-05 14:08:57 -04008429
Michael Chanc0c050c2015-10-22 16:01:17 -04008430 rc = bnxt_update_link(bp, false);
8431 if (rc) {
8432 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
8433 rc);
8434 return rc;
8435 }
8436
Michael Chan93ed8112016-06-13 02:25:37 -04008437 /* Older firmware does not have supported_auto_speeds, so assume
8438 * that all supported speeds can be autonegotiated.
8439 */
8440 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
8441 link_info->support_auto_speeds = link_info->support_speeds;
8442
Michael Chanc0c050c2015-10-22 16:01:17 -04008443 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05008444 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04008445 link_info->autoneg = BNXT_AUTONEG_SPEED;
8446 if (bp->hwrm_spec_code >= 0x10201) {
8447 if (link_info->auto_pause_setting &
8448 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
8449 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8450 } else {
8451 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8452 }
Michael Chan0d8abf02016-02-10 17:33:47 -05008453 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05008454 } else {
8455 link_info->req_link_speed = link_info->force_link_speed;
8456 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04008457 }
Michael Chanc9ee9512016-04-05 14:08:56 -04008458 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
8459 link_info->req_flow_ctrl =
8460 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
8461 else
8462 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04008463 return rc;
8464}
8465
8466static int bnxt_get_max_irq(struct pci_dev *pdev)
8467{
8468 u16 ctrl;
8469
8470 if (!pdev->msix_cap)
8471 return 1;
8472
8473 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
8474 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
8475}
8476
Michael Chan6e6c5a52016-01-02 23:45:02 -05008477static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8478 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04008479{
Michael Chan6a4f2942018-01-17 03:21:06 -05008480 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008481 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04008482
Michael Chan6a4f2942018-01-17 03:21:06 -05008483 *max_tx = hw_resc->max_tx_rings;
8484 *max_rx = hw_resc->max_rx_rings;
8485 *max_cp = min_t(int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8486 *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
8487 max_ring_grps = hw_resc->max_hw_ring_grps;
Prashant Sreedharan76595192016-07-18 07:15:22 -04008488 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
8489 *max_cp -= 1;
8490 *max_rx -= 2;
8491 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008492 if (bp->flags & BNXT_FLAG_AGG_RINGS)
8493 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05008494 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05008495}
8496
8497int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
8498{
8499 int rx, tx, cp;
8500
8501 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
8502 if (!rx || !tx || !cp)
8503 return -ENOMEM;
8504
8505 *max_rx = rx;
8506 *max_tx = tx;
8507 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
8508}
8509
Michael Chane4060d32016-12-07 00:26:19 -05008510static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8511 bool shared)
8512{
8513 int rc;
8514
8515 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008516 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
8517 /* Not enough rings, try disabling agg rings. */
8518 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8519 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8520 if (rc)
8521 return rc;
8522 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
Michael Chan1054aee2017-12-16 03:09:42 -05008523 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8524 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008525 bnxt_set_ring_params(bp);
8526 }
Michael Chane4060d32016-12-07 00:26:19 -05008527
8528 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
8529 int max_cp, max_stat, max_irq;
8530
8531 /* Reserve minimum resources for RoCE */
8532 max_cp = bnxt_get_max_func_cp_rings(bp);
8533 max_stat = bnxt_get_max_func_stat_ctxs(bp);
8534 max_irq = bnxt_get_max_func_irqs(bp);
8535 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
8536 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
8537 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
8538 return 0;
8539
8540 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
8541 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
8542 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
8543 max_cp = min_t(int, max_cp, max_irq);
8544 max_cp = min_t(int, max_cp, max_stat);
8545 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
8546 if (rc)
8547 rc = 0;
8548 }
8549 return rc;
8550}
8551
Michael Chan58ea8012018-01-17 03:21:08 -05008552/* In initial default shared ring setting, each shared ring must have a
8553 * RX/TX ring pair.
8554 */
8555static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
8556{
8557 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
8558 bp->rx_nr_rings = bp->cp_nr_rings;
8559 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
8560 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8561}
8562
Michael Chan702c2212017-05-29 19:06:10 -04008563static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05008564{
8565 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008566
Michael Chan2773dfb2018-04-26 17:44:42 -04008567 if (!bnxt_can_reserve_rings(bp))
8568 return 0;
8569
Michael Chan6e6c5a52016-01-02 23:45:02 -05008570 if (sh)
8571 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8572 dflt_rings = netif_get_num_default_rss_queues();
Michael Chan1d3ef132018-03-31 13:54:07 -04008573 /* Reduce default rings on multi-port cards so that total default
8574 * rings do not exceed CPU count.
8575 */
8576 if (bp->port_count > 1) {
8577 int max_rings =
8578 max_t(int, num_online_cpus() / bp->port_count, 1);
8579
8580 dflt_rings = min_t(int, dflt_rings, max_rings);
8581 }
Michael Chane4060d32016-12-07 00:26:19 -05008582 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05008583 if (rc)
8584 return rc;
8585 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
8586 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan58ea8012018-01-17 03:21:08 -05008587 if (sh)
8588 bnxt_trim_dflt_sh_rings(bp);
8589 else
8590 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
8591 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
Michael Chan391be5c2016-12-29 12:13:41 -05008592
Michael Chan674f50a2018-01-17 03:21:09 -05008593 rc = __bnxt_reserve_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05008594 if (rc)
8595 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
Michael Chan58ea8012018-01-17 03:21:08 -05008596 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8597 if (sh)
8598 bnxt_trim_dflt_sh_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05008599
Michael Chan674f50a2018-01-17 03:21:09 -05008600 /* Rings may have been trimmed, re-reserve the trimmed rings. */
8601 if (bnxt_need_reserve_rings(bp)) {
8602 rc = __bnxt_reserve_rings(bp);
8603 if (rc)
8604 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
8605 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8606 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05008607 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04008608 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8609 bp->rx_nr_rings++;
8610 bp->cp_nr_rings++;
8611 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05008612 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008613}
8614
Michael Chan47558ac2018-04-26 17:44:44 -04008615static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
8616{
8617 int rc;
8618
8619 if (bp->tx_nr_rings)
8620 return 0;
8621
8622 rc = bnxt_set_dflt_rings(bp, true);
8623 if (rc) {
8624 netdev_err(bp->dev, "Not enough rings available.\n");
8625 return rc;
8626 }
8627 rc = bnxt_init_int_mode(bp);
8628 if (rc)
8629 return rc;
8630 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8631 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
8632 bp->flags |= BNXT_FLAG_RFS;
8633 bp->dev->features |= NETIF_F_NTUPLE;
8634 }
8635 return 0;
8636}
8637
Michael Chan80fcaf42018-01-17 03:21:05 -05008638int bnxt_restore_pf_fw_resources(struct bnxt *bp)
Michael Chan7b08f662016-12-07 00:26:18 -05008639{
Michael Chan80fcaf42018-01-17 03:21:05 -05008640 int rc;
8641
Michael Chan7b08f662016-12-07 00:26:18 -05008642 ASSERT_RTNL();
8643 bnxt_hwrm_func_qcaps(bp);
Venkat Duvvuru1a037782018-03-09 23:46:09 -05008644
8645 if (netif_running(bp->dev))
8646 __bnxt_close_nic(bp, true, false);
8647
Michael Chanec86f142018-03-31 13:54:21 -04008648 bnxt_ulp_irq_stop(bp);
Michael Chan80fcaf42018-01-17 03:21:05 -05008649 bnxt_clear_int_mode(bp);
8650 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -04008651 bnxt_ulp_irq_restart(bp, rc);
Venkat Duvvuru1a037782018-03-09 23:46:09 -05008652
8653 if (netif_running(bp->dev)) {
8654 if (rc)
8655 dev_close(bp->dev);
8656 else
8657 rc = bnxt_open_nic(bp, true, false);
8658 }
8659
Michael Chan80fcaf42018-01-17 03:21:05 -05008660 return rc;
Michael Chan7b08f662016-12-07 00:26:18 -05008661}
8662
Michael Chana22a6ac2017-08-23 19:34:05 -04008663static int bnxt_init_mac_addr(struct bnxt *bp)
8664{
8665 int rc = 0;
8666
8667 if (BNXT_PF(bp)) {
8668 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
8669 } else {
8670#ifdef CONFIG_BNXT_SRIOV
8671 struct bnxt_vf_info *vf = &bp->vf;
8672
8673 if (is_valid_ether_addr(vf->mac_addr)) {
Vasundhara Volam91cdda42018-01-17 03:21:14 -05008674 /* overwrite netdev dev_addr with admin VF MAC */
Michael Chana22a6ac2017-08-23 19:34:05 -04008675 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
8676 } else {
8677 eth_hw_addr_random(bp->dev);
8678 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
8679 }
8680#endif
8681 }
8682 return rc;
8683}
8684
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008685static void bnxt_parse_log_pcie_link(struct bnxt *bp)
8686{
8687 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
8688 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
8689
Vasundhara Volam7ab07602017-10-13 21:09:31 -04008690 if (pcie_get_minimum_link(pci_physfn(bp->pdev), &speed, &width) ||
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008691 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
8692 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
8693 else
8694 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
8695 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
8696 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
8697 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
8698 "Unknown", width);
8699}
8700
Michael Chanc0c050c2015-10-22 16:01:17 -04008701static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8702{
8703 static int version_printed;
8704 struct net_device *dev;
8705 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008706 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04008707
Ray Jui4e003382017-02-20 19:25:16 -05008708 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04008709 return -ENODEV;
8710
Michael Chanc0c050c2015-10-22 16:01:17 -04008711 if (version_printed++ == 0)
8712 pr_info("%s", version);
8713
8714 max_irqs = bnxt_get_max_irq(pdev);
8715 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
8716 if (!dev)
8717 return -ENOMEM;
8718
8719 bp = netdev_priv(dev);
8720
8721 if (bnxt_vf_pciid(ent->driver_data))
8722 bp->flags |= BNXT_FLAG_VF;
8723
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008724 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04008725 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04008726
8727 rc = bnxt_init_board(pdev, dev);
8728 if (rc < 0)
8729 goto init_err_free;
8730
8731 dev->netdev_ops = &bnxt_netdev_ops;
8732 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
8733 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -07008734 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -04008735 pci_set_drvdata(pdev, dev);
8736
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008737 rc = bnxt_alloc_hwrm_resources(bp);
8738 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008739 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008740
8741 mutex_init(&bp->hwrm_cmd_lock);
8742 rc = bnxt_hwrm_ver_get(bp);
8743 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008744 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008745
Deepak Khungare605db82017-05-29 19:06:04 -04008746 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
8747 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
8748 if (rc)
8749 goto init_err_pci_clean;
8750 }
8751
Michael Chan3c2217a2017-03-08 18:44:32 -05008752 rc = bnxt_hwrm_func_reset(bp);
8753 if (rc)
8754 goto init_err_pci_clean;
8755
Rob Swindell5ac67d82016-09-19 03:58:03 -04008756 bnxt_hwrm_fw_set_time(bp);
8757
Michael Chanc0c050c2015-10-22 16:01:17 -04008758 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8759 NETIF_F_TSO | NETIF_F_TSO6 |
8760 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07008761 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07008762 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8763 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008764 NETIF_F_RXCSUM | NETIF_F_GRO;
8765
8766 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8767 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04008768
Michael Chanc0c050c2015-10-22 16:01:17 -04008769 dev->hw_enc_features =
8770 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8771 NETIF_F_TSO | NETIF_F_TSO6 |
8772 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07008773 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07008774 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07008775 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
8776 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04008777 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
8778 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
8779 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
Michael Chan1054aee2017-12-16 03:09:42 -05008780 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8781 dev->hw_features |= NETIF_F_GRO_HW;
Michael Chanc0c050c2015-10-22 16:01:17 -04008782 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Michael Chan1054aee2017-12-16 03:09:42 -05008783 if (dev->features & NETIF_F_GRO_HW)
8784 dev->features &= ~NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04008785 dev->priv_flags |= IFF_UNICAST_FLT;
8786
8787#ifdef CONFIG_BNXT_SRIOV
8788 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008789 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04008790#endif
Michael Chan309369c2016-06-13 02:25:34 -04008791 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04008792 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04008793 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04008794 else
8795 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04008796
Michael Chanc0c050c2015-10-22 16:01:17 -04008797 rc = bnxt_hwrm_func_drv_rgtr(bp);
8798 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008799 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008800
Michael Chana1653b12016-12-07 00:26:20 -05008801 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
8802 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008803 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05008804
Michael Chana588e452016-12-07 00:26:21 -05008805 bp->ulp_probe = bnxt_ulp_probe;
8806
Michael Chanc0c050c2015-10-22 16:01:17 -04008807 /* Get the MAX capabilities for this function */
8808 rc = bnxt_hwrm_func_qcaps(bp);
8809 if (rc) {
8810 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
8811 rc);
8812 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008813 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008814 }
Michael Chana22a6ac2017-08-23 19:34:05 -04008815 rc = bnxt_init_mac_addr(bp);
8816 if (rc) {
8817 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
8818 rc = -EADDRNOTAVAIL;
8819 goto init_err_pci_clean;
8820 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008821 rc = bnxt_hwrm_queue_qportcfg(bp);
8822 if (rc) {
8823 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
8824 rc);
8825 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008826 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008827 }
8828
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008829 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05008830 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04008831 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04008832 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008833
Michael Chan7eb9bb32017-10-26 11:51:25 -04008834 /* MTU range: 60 - FW defined max */
8835 dev->min_mtu = ETH_ZLEN;
8836 dev->max_mtu = bp->max_mtu;
8837
Michael Chand5430d32017-08-28 13:40:31 -04008838 rc = bnxt_probe_phy(bp);
8839 if (rc)
8840 goto init_err_pci_clean;
8841
Michael Chanc61fb992017-02-06 16:55:36 -05008842 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04008843 bnxt_set_tpa_flags(bp);
8844 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05008845 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04008846 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008847 if (rc) {
8848 netdev_err(bp->dev, "Not enough rings available.\n");
8849 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05008850 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008851 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008852
Michael Chan87da7f72016-11-16 21:13:09 -05008853 /* Default RSS hash cfg. */
8854 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
8855 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
8856 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
8857 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04008858 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05008859 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8860 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8861 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8862 }
8863
Michael Chan8fdefd62016-12-29 12:13:36 -05008864 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05008865 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008866 dev->hw_features |= NETIF_F_NTUPLE;
8867 if (bnxt_rfs_capable(bp)) {
8868 bp->flags |= BNXT_FLAG_RFS;
8869 dev->features |= NETIF_F_NTUPLE;
8870 }
8871 }
8872
Michael Chanc0c050c2015-10-22 16:01:17 -04008873 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8874 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8875
Michael Chan78095922016-12-07 00:26:16 -05008876 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008877 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008878 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008879
Michael Chan832aed12018-03-09 23:46:07 -05008880 /* No TC has been set yet and rings may have been trimmed due to
8881 * limited MSIX, so we re-initialize the TX rings per TC.
8882 */
8883 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8884
Michael Chanc1ef1462017-04-04 18:14:07 -04008885 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008886 if (bp->flags & BNXT_FLAG_WOL_CAP)
8887 device_set_wakeup_enable(&pdev->dev, bp->wol);
8888 else
8889 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04008890
Michael Chanc3480a62018-01-17 03:21:15 -05008891 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
8892
Michael Chanc213eae2017-10-13 21:09:29 -04008893 if (BNXT_PF(bp)) {
8894 if (!bnxt_pf_wq) {
8895 bnxt_pf_wq =
8896 create_singlethread_workqueue("bnxt_pf_wq");
8897 if (!bnxt_pf_wq) {
8898 dev_err(&pdev->dev, "Unable to create workqueue.\n");
8899 goto init_err_pci_clean;
8900 }
8901 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008902 bnxt_init_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04008903 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008904
Michael Chan78095922016-12-07 00:26:16 -05008905 rc = register_netdev(dev);
8906 if (rc)
Sathya Perla2ae74082017-08-28 13:40:33 -04008907 goto init_err_cleanup_tc;
Michael Chan78095922016-12-07 00:26:16 -05008908
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008909 if (BNXT_PF(bp))
8910 bnxt_dl_register(bp);
8911
Michael Chanc0c050c2015-10-22 16:01:17 -04008912 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8913 board_info[ent->driver_data].name,
8914 (long)pci_resource_start(pdev, 0), dev->dev_addr);
8915
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008916 bnxt_parse_log_pcie_link(bp);
8917
Michael Chanc0c050c2015-10-22 16:01:17 -04008918 return 0;
8919
Sathya Perla2ae74082017-08-28 13:40:33 -04008920init_err_cleanup_tc:
8921 bnxt_shutdown_tc(bp);
Michael Chan78095922016-12-07 00:26:16 -05008922 bnxt_clear_int_mode(bp);
8923
Sathya Perla17086392017-02-20 19:25:18 -05008924init_err_pci_clean:
8925 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008926
8927init_err_free:
8928 free_netdev(dev);
8929 return rc;
8930}
8931
Michael Chand196ece2017-04-04 18:14:08 -04008932static void bnxt_shutdown(struct pci_dev *pdev)
8933{
8934 struct net_device *dev = pci_get_drvdata(pdev);
8935 struct bnxt *bp;
8936
8937 if (!dev)
8938 return;
8939
8940 rtnl_lock();
8941 bp = netdev_priv(dev);
8942 if (!bp)
8943 goto shutdown_exit;
8944
8945 if (netif_running(dev))
8946 dev_close(dev);
8947
Ray Juia7f3f932017-12-01 03:13:02 -05008948 bnxt_ulp_shutdown(bp);
8949
Michael Chand196ece2017-04-04 18:14:08 -04008950 if (system_state == SYSTEM_POWER_OFF) {
8951 bnxt_clear_int_mode(bp);
8952 pci_wake_from_d3(pdev, bp->wol);
8953 pci_set_power_state(pdev, PCI_D3hot);
8954 }
8955
8956shutdown_exit:
8957 rtnl_unlock();
8958}
8959
Michael Chanf65a2042017-04-04 18:14:11 -04008960#ifdef CONFIG_PM_SLEEP
8961static int bnxt_suspend(struct device *device)
8962{
8963 struct pci_dev *pdev = to_pci_dev(device);
8964 struct net_device *dev = pci_get_drvdata(pdev);
8965 struct bnxt *bp = netdev_priv(dev);
8966 int rc = 0;
8967
8968 rtnl_lock();
8969 if (netif_running(dev)) {
8970 netif_device_detach(dev);
8971 rc = bnxt_close(dev);
8972 }
8973 bnxt_hwrm_func_drv_unrgtr(bp);
8974 rtnl_unlock();
8975 return rc;
8976}
8977
8978static int bnxt_resume(struct device *device)
8979{
8980 struct pci_dev *pdev = to_pci_dev(device);
8981 struct net_device *dev = pci_get_drvdata(pdev);
8982 struct bnxt *bp = netdev_priv(dev);
8983 int rc = 0;
8984
8985 rtnl_lock();
8986 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8987 rc = -ENODEV;
8988 goto resume_exit;
8989 }
8990 rc = bnxt_hwrm_func_reset(bp);
8991 if (rc) {
8992 rc = -EBUSY;
8993 goto resume_exit;
8994 }
8995 bnxt_get_wol_settings(bp);
8996 if (netif_running(dev)) {
8997 rc = bnxt_open(dev);
8998 if (!rc)
8999 netif_device_attach(dev);
9000 }
9001
9002resume_exit:
9003 rtnl_unlock();
9004 return rc;
9005}
9006
9007static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
9008#define BNXT_PM_OPS (&bnxt_pm_ops)
9009
9010#else
9011
9012#define BNXT_PM_OPS NULL
9013
9014#endif /* CONFIG_PM_SLEEP */
9015
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009016/**
9017 * bnxt_io_error_detected - called when PCI error is detected
9018 * @pdev: Pointer to PCI device
9019 * @state: The current pci connection state
9020 *
9021 * This function is called after a PCI bus error affecting
9022 * this device has been detected.
9023 */
9024static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
9025 pci_channel_state_t state)
9026{
9027 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05009028 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009029
9030 netdev_info(netdev, "PCI I/O error detected\n");
9031
9032 rtnl_lock();
9033 netif_device_detach(netdev);
9034
Michael Chana588e452016-12-07 00:26:21 -05009035 bnxt_ulp_stop(bp);
9036
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009037 if (state == pci_channel_io_perm_failure) {
9038 rtnl_unlock();
9039 return PCI_ERS_RESULT_DISCONNECT;
9040 }
9041
9042 if (netif_running(netdev))
9043 bnxt_close(netdev);
9044
9045 pci_disable_device(pdev);
9046 rtnl_unlock();
9047
9048 /* Request a slot slot reset. */
9049 return PCI_ERS_RESULT_NEED_RESET;
9050}
9051
9052/**
9053 * bnxt_io_slot_reset - called after the pci bus has been reset.
9054 * @pdev: Pointer to PCI device
9055 *
9056 * Restart the card from scratch, as if from a cold-boot.
9057 * At this point, the card has exprienced a hard reset,
9058 * followed by fixups by BIOS, and has its config space
9059 * set up identically to what it was at cold boot.
9060 */
9061static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
9062{
9063 struct net_device *netdev = pci_get_drvdata(pdev);
9064 struct bnxt *bp = netdev_priv(netdev);
9065 int err = 0;
9066 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
9067
9068 netdev_info(bp->dev, "PCI Slot Reset\n");
9069
9070 rtnl_lock();
9071
9072 if (pci_enable_device(pdev)) {
9073 dev_err(&pdev->dev,
9074 "Cannot re-enable PCI device after reset.\n");
9075 } else {
9076 pci_set_master(pdev);
9077
Michael Chanaa8ed022016-12-07 00:26:17 -05009078 err = bnxt_hwrm_func_reset(bp);
9079 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009080 err = bnxt_open(netdev);
9081
Michael Chana588e452016-12-07 00:26:21 -05009082 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009083 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05009084 bnxt_ulp_start(bp);
9085 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009086 }
9087
9088 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
9089 dev_close(netdev);
9090
9091 rtnl_unlock();
9092
9093 err = pci_cleanup_aer_uncorrect_error_status(pdev);
9094 if (err) {
9095 dev_err(&pdev->dev,
9096 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
9097 err); /* non-fatal, continue */
9098 }
9099
9100 return PCI_ERS_RESULT_RECOVERED;
9101}
9102
9103/**
9104 * bnxt_io_resume - called when traffic can start flowing again.
9105 * @pdev: Pointer to PCI device
9106 *
9107 * This callback is called when the error recovery driver tells
9108 * us that its OK to resume normal operation.
9109 */
9110static void bnxt_io_resume(struct pci_dev *pdev)
9111{
9112 struct net_device *netdev = pci_get_drvdata(pdev);
9113
9114 rtnl_lock();
9115
9116 netif_device_attach(netdev);
9117
9118 rtnl_unlock();
9119}
9120
9121static const struct pci_error_handlers bnxt_err_handler = {
9122 .error_detected = bnxt_io_error_detected,
9123 .slot_reset = bnxt_io_slot_reset,
9124 .resume = bnxt_io_resume
9125};
9126
Michael Chanc0c050c2015-10-22 16:01:17 -04009127static struct pci_driver bnxt_pci_driver = {
9128 .name = DRV_MODULE_NAME,
9129 .id_table = bnxt_pci_tbl,
9130 .probe = bnxt_init_one,
9131 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04009132 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04009133 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009134 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04009135#if defined(CONFIG_BNXT_SRIOV)
9136 .sriov_configure = bnxt_sriov_configure,
9137#endif
9138};
9139
Michael Chanc213eae2017-10-13 21:09:29 -04009140static int __init bnxt_init(void)
9141{
Andy Gospodarekcabfb092018-04-26 17:44:40 -04009142 bnxt_debug_init();
Michael Chanc213eae2017-10-13 21:09:29 -04009143 return pci_register_driver(&bnxt_pci_driver);
9144}
9145
9146static void __exit bnxt_exit(void)
9147{
9148 pci_unregister_driver(&bnxt_pci_driver);
9149 if (bnxt_pf_wq)
9150 destroy_workqueue(bnxt_pf_wq);
Andy Gospodarekcabfb092018-04-26 17:44:40 -04009151 bnxt_debug_exit();
Michael Chanc213eae2017-10-13 21:09:29 -04009152}
9153
9154module_init(bnxt_init);
9155module_exit(bnxt_exit);