blob: 7952100e0171059ffbfebb279964fcfe5fa9ecc1 [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>
Vasundhara Volamcde49a42018-08-05 16:51:56 -040054#include <linux/hwmon.h>
55#include <linux/hwmon-sysfs.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040056
57#include "bnxt_hsi.h"
58#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050059#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040060#include "bnxt_sriov.h"
61#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050062#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050063#include "bnxt_xdp.h"
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040064#include "bnxt_vfr.h"
Sathya Perla2ae74082017-08-28 13:40:33 -040065#include "bnxt_tc.h"
Steve Lin3c467bf2017-10-19 10:45:56 -040066#include "bnxt_devlink.h"
Andy Gospodarekcabfb092018-04-26 17:44:40 -040067#include "bnxt_debugfs.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040068
69#define BNXT_TX_TIMEOUT (5 * HZ)
70
71static const char version[] =
72 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
73
74MODULE_LICENSE("GPL");
75MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
76MODULE_VERSION(DRV_MODULE_VERSION);
77
78#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
79#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
80#define BNXT_RX_COPY_THRESH 256
81
Michael Chan4419dbe2016-02-10 17:33:49 -050082#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040083
84enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050085 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040086 BCM57302,
87 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040088 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040089 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040090 BCM57311,
91 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050092 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040093 BCM57404,
94 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040095 BCM57402_NPAR,
96 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040097 BCM57412,
98 BCM57414,
99 BCM57416,
100 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -0400101 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -0400102 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -0400103 BCM57417_SFP,
104 BCM57416_SFP,
105 BCM57404_NPAR,
106 BCM57406_NPAR,
107 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400108 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400109 BCM57414_NPAR,
110 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500111 BCM57452,
112 BCM57454,
Vasundhara Volam92abef32018-01-17 03:21:13 -0500113 BCM5745x_NPAR,
Ray Jui4a581392017-08-28 13:40:28 -0400114 BCM58802,
Ray Jui8ed693b2017-10-26 11:51:20 -0400115 BCM58804,
Ray Jui4a581392017-08-28 13:40:28 -0400116 BCM58808,
Michael Chanadbc8302016-09-19 03:58:01 -0400117 NETXTREME_E_VF,
118 NETXTREME_C_VF,
Rob Miller618784e2017-10-26 11:51:21 -0400119 NETXTREME_S_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400120};
121
122/* indexed by enum above */
123static const struct {
124 char *name;
125} board_info[] = {
Scott Branden27573a72017-08-28 13:40:29 -0400126 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
127 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
128 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
129 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
130 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
131 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
132 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
133 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
134 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
135 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
136 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
137 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
138 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
139 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
140 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
141 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
142 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
143 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
144 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
145 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
146 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
147 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
148 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
149 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
150 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
151 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
152 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
153 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Vasundhara Volam92abef32018-01-17 03:21:13 -0500154 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
Scott Branden27573a72017-08-28 13:40:29 -0400155 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
Ray Jui8ed693b2017-10-26 11:51:20 -0400156 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400157 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
158 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
159 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Rob Miller618784e2017-10-26 11:51:21 -0400160 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400161};
162
163static const struct pci_device_id bnxt_pci_tbl[] = {
Vasundhara Volam92abef32018-01-17 03:21:13 -0500164 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
165 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400166 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500168 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
170 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400172 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
174 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500175 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400176 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
177 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400178 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
179 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400180 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
181 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
182 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
183 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400184 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400185 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400186 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
187 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
188 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
189 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
190 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400191 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
192 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400193 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400194 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400195 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400196 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400197 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
Deepak Khungar32b40792017-02-12 19:18:18 -0500198 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
Ray Jui4a581392017-08-28 13:40:28 -0400199 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
Ray Jui8ed693b2017-10-26 11:51:20 -0400200 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400201#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400202 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400204 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
205 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Rob Miller618784e2017-10-26 11:51:21 -0400210 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400211#endif
212 { 0 }
213};
214
215MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
216
217static const u16 bnxt_vf_req_snif[] = {
218 HWRM_FUNC_CFG,
Vasundhara Volam91cdda42018-01-17 03:21:14 -0500219 HWRM_FUNC_VF_CFG,
Michael Chanc0c050c2015-10-22 16:01:17 -0400220 HWRM_PORT_PHY_QCFG,
221 HWRM_CFA_L2_FILTER_ALLOC,
222};
223
Michael Chan25be8622016-04-05 14:09:00 -0400224static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500225 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
226 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
227 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
228 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
229 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400230};
231
Michael Chanc213eae2017-10-13 21:09:29 -0400232static struct workqueue_struct *bnxt_pf_wq;
233
Michael Chanc0c050c2015-10-22 16:01:17 -0400234static bool bnxt_vf_pciid(enum board_idx idx)
235{
Rob Miller618784e2017-10-26 11:51:21 -0400236 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
237 idx == NETXTREME_S_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400238}
239
240#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
241#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
242#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
243
Michael Chanc0c050c2015-10-22 16:01:17 -0400244#define BNXT_CP_DB_IRQ_DIS(db) \
245 writel(DB_CP_IRQ_DIS_FLAGS, db)
246
Michael Chan697197e2018-10-14 07:02:46 -0400247#define BNXT_DB_CQ(db, idx) \
248 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
249
250#define BNXT_DB_NQ_P5(db, idx) \
251 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
252
253#define BNXT_DB_CQ_ARM(db, idx) \
254 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
255
256#define BNXT_DB_NQ_ARM_P5(db, idx) \
257 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
258
259static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
260{
261 if (bp->flags & BNXT_FLAG_CHIP_P5)
262 BNXT_DB_NQ_P5(db, idx);
263 else
264 BNXT_DB_CQ(db, idx);
265}
266
267static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
268{
269 if (bp->flags & BNXT_FLAG_CHIP_P5)
270 BNXT_DB_NQ_ARM_P5(db, idx);
271 else
272 BNXT_DB_CQ_ARM(db, idx);
273}
274
275static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
276{
277 if (bp->flags & BNXT_FLAG_CHIP_P5)
278 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
279 db->doorbell);
280 else
281 BNXT_DB_CQ(db, idx);
282}
283
Michael Chan38413402017-02-06 16:55:43 -0500284const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400285 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
286 TX_BD_FLAGS_LHINT_512_TO_1023,
287 TX_BD_FLAGS_LHINT_1024_TO_2047,
288 TX_BD_FLAGS_LHINT_1024_TO_2047,
289 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
290 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
291 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
292 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
293 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
294 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
295 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
296 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
297 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
298 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
299 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
300 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
301 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
302 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
303 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
304};
305
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400306static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
307{
308 struct metadata_dst *md_dst = skb_metadata_dst(skb);
309
310 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
311 return 0;
312
313 return md_dst->u.port_info.port_id;
314}
315
Michael Chanc0c050c2015-10-22 16:01:17 -0400316static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
317{
318 struct bnxt *bp = netdev_priv(dev);
319 struct tx_bd *txbd;
320 struct tx_bd_ext *txbd1;
321 struct netdev_queue *txq;
322 int i;
323 dma_addr_t mapping;
324 unsigned int length, pad = 0;
325 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
326 u16 prod, last_frag;
327 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400328 struct bnxt_tx_ring_info *txr;
329 struct bnxt_sw_tx_bd *tx_buf;
330
331 i = skb_get_queue_mapping(skb);
332 if (unlikely(i >= bp->tx_nr_rings)) {
333 dev_kfree_skb_any(skb);
334 return NETDEV_TX_OK;
335 }
336
Michael Chanc0c050c2015-10-22 16:01:17 -0400337 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500338 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400339 prod = txr->tx_prod;
340
341 free_size = bnxt_tx_avail(bp, txr);
342 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
343 netif_tx_stop_queue(txq);
344 return NETDEV_TX_BUSY;
345 }
346
347 length = skb->len;
348 len = skb_headlen(skb);
349 last_frag = skb_shinfo(skb)->nr_frags;
350
351 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
352
353 txbd->tx_bd_opaque = prod;
354
355 tx_buf = &txr->tx_buf_ring[prod];
356 tx_buf->skb = skb;
357 tx_buf->nr_frags = last_frag;
358
359 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400360 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400361 if (skb_vlan_tag_present(skb)) {
362 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
363 skb_vlan_tag_get(skb);
364 /* Currently supports 8021Q, 8021AD vlan offloads
365 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
366 */
367 if (skb->vlan_proto == htons(ETH_P_8021Q))
368 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
369 }
370
371 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500372 struct tx_push_buffer *tx_push_buf = txr->tx_push;
373 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
374 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
Michael Chan697197e2018-10-14 07:02:46 -0400375 void __iomem *db = txr->tx_db.doorbell;
Michael Chan4419dbe2016-02-10 17:33:49 -0500376 void *pdata = tx_push_buf->data;
377 u64 *end;
378 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400379
380 /* Set COAL_NOW to be ready quickly for the next push */
381 tx_push->tx_bd_len_flags_type =
382 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
383 TX_BD_TYPE_LONG_TX_BD |
384 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
385 TX_BD_FLAGS_COAL_NOW |
386 TX_BD_FLAGS_PACKET_END |
387 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
388
389 if (skb->ip_summed == CHECKSUM_PARTIAL)
390 tx_push1->tx_bd_hsize_lflags =
391 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
392 else
393 tx_push1->tx_bd_hsize_lflags = 0;
394
395 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400396 tx_push1->tx_bd_cfa_action =
397 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400398
Michael Chanfbb0fa82016-02-22 02:10:26 -0500399 end = pdata + length;
400 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500401 *end = 0;
402
Michael Chanc0c050c2015-10-22 16:01:17 -0400403 skb_copy_from_linear_data(skb, pdata, len);
404 pdata += len;
405 for (j = 0; j < last_frag; j++) {
406 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
407 void *fptr;
408
409 fptr = skb_frag_address_safe(frag);
410 if (!fptr)
411 goto normal_tx;
412
413 memcpy(pdata, fptr, skb_frag_size(frag));
414 pdata += skb_frag_size(frag);
415 }
416
Michael Chan4419dbe2016-02-10 17:33:49 -0500417 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
418 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400419 prod = NEXT_TX(prod);
420 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
421 memcpy(txbd, tx_push1, sizeof(*txbd));
422 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500423 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400424 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
425 txr->tx_prod = prod;
426
Michael Chanb9a84602016-06-06 02:37:14 -0400427 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400428 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400429 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400430
Michael Chan4419dbe2016-02-10 17:33:49 -0500431 push_len = (length + sizeof(*tx_push) + 7) / 8;
432 if (push_len > 16) {
Michael Chan697197e2018-10-14 07:02:46 -0400433 __iowrite64_copy(db, tx_push_buf, 16);
434 __iowrite32_copy(db + 4, tx_push_buf + 1,
Michael Chan9d137442016-09-05 01:57:35 -0400435 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500436 } else {
Michael Chan697197e2018-10-14 07:02:46 -0400437 __iowrite64_copy(db, tx_push_buf, push_len);
Michael Chan4419dbe2016-02-10 17:33:49 -0500438 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400439
Michael Chanc0c050c2015-10-22 16:01:17 -0400440 goto tx_done;
441 }
442
443normal_tx:
444 if (length < BNXT_MIN_PKT_SIZE) {
445 pad = BNXT_MIN_PKT_SIZE - length;
446 if (skb_pad(skb, pad)) {
447 /* SKB already freed. */
448 tx_buf->skb = NULL;
449 return NETDEV_TX_OK;
450 }
451 length = BNXT_MIN_PKT_SIZE;
452 }
453
454 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
455
456 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
457 dev_kfree_skb_any(skb);
458 tx_buf->skb = NULL;
459 return NETDEV_TX_OK;
460 }
461
462 dma_unmap_addr_set(tx_buf, mapping, mapping);
463 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
464 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
465
466 txbd->tx_bd_haddr = cpu_to_le64(mapping);
467
468 prod = NEXT_TX(prod);
469 txbd1 = (struct tx_bd_ext *)
470 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
471
472 txbd1->tx_bd_hsize_lflags = 0;
473 if (skb_is_gso(skb)) {
474 u32 hdr_len;
475
476 if (skb->encapsulation)
477 hdr_len = skb_inner_network_offset(skb) +
478 skb_inner_network_header_len(skb) +
479 inner_tcp_hdrlen(skb);
480 else
481 hdr_len = skb_transport_offset(skb) +
482 tcp_hdrlen(skb);
483
484 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
485 TX_BD_FLAGS_T_IPID |
486 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
487 length = skb_shinfo(skb)->gso_size;
488 txbd1->tx_bd_mss = cpu_to_le32(length);
489 length += hdr_len;
490 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
491 txbd1->tx_bd_hsize_lflags =
492 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
493 txbd1->tx_bd_mss = 0;
494 }
495
496 length >>= 9;
497 flags |= bnxt_lhint_arr[length];
498 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
499
500 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400501 txbd1->tx_bd_cfa_action =
502 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400503 for (i = 0; i < last_frag; i++) {
504 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
505
506 prod = NEXT_TX(prod);
507 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
508
509 len = skb_frag_size(frag);
510 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
511 DMA_TO_DEVICE);
512
513 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
514 goto tx_dma_error;
515
516 tx_buf = &txr->tx_buf_ring[prod];
517 dma_unmap_addr_set(tx_buf, mapping, mapping);
518
519 txbd->tx_bd_haddr = cpu_to_le64(mapping);
520
521 flags = len << TX_BD_LEN_SHIFT;
522 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
523 }
524
525 flags &= ~TX_BD_LEN;
526 txbd->tx_bd_len_flags_type =
527 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
528 TX_BD_FLAGS_PACKET_END);
529
530 netdev_tx_sent_queue(txq, skb->len);
531
532 /* Sync BD data before updating doorbell */
533 wmb();
534
535 prod = NEXT_TX(prod);
536 txr->tx_prod = prod;
537
Michael Chanffe40642017-05-30 20:03:00 -0400538 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan697197e2018-10-14 07:02:46 -0400539 bnxt_db_write(bp, &txr->tx_db, prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400540
541tx_done:
542
543 mmiowb();
544
545 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400546 if (skb->xmit_more && !tx_buf->is_push)
Michael Chan697197e2018-10-14 07:02:46 -0400547 bnxt_db_write(bp, &txr->tx_db, prod);
Michael Chan4d172f22017-05-29 19:06:09 -0400548
Michael Chanc0c050c2015-10-22 16:01:17 -0400549 netif_tx_stop_queue(txq);
550
551 /* netif_tx_stop_queue() must be done before checking
552 * tx index in bnxt_tx_avail() below, because in
553 * bnxt_tx_int(), we update tx index before checking for
554 * netif_tx_queue_stopped().
555 */
556 smp_mb();
557 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
558 netif_tx_wake_queue(txq);
559 }
560 return NETDEV_TX_OK;
561
562tx_dma_error:
563 last_frag = i;
564
565 /* start back at beginning and unmap skb */
566 prod = txr->tx_prod;
567 tx_buf = &txr->tx_buf_ring[prod];
568 tx_buf->skb = NULL;
569 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
570 skb_headlen(skb), PCI_DMA_TODEVICE);
571 prod = NEXT_TX(prod);
572
573 /* unmap remaining mapped pages */
574 for (i = 0; i < last_frag; i++) {
575 prod = NEXT_TX(prod);
576 tx_buf = &txr->tx_buf_ring[prod];
577 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
578 skb_frag_size(&skb_shinfo(skb)->frags[i]),
579 PCI_DMA_TODEVICE);
580 }
581
582 dev_kfree_skb_any(skb);
583 return NETDEV_TX_OK;
584}
585
586static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
587{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500588 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500589 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400590 u16 cons = txr->tx_cons;
591 struct pci_dev *pdev = bp->pdev;
592 int i;
593 unsigned int tx_bytes = 0;
594
595 for (i = 0; i < nr_pkts; i++) {
596 struct bnxt_sw_tx_bd *tx_buf;
597 struct sk_buff *skb;
598 int j, last;
599
600 tx_buf = &txr->tx_buf_ring[cons];
601 cons = NEXT_TX(cons);
602 skb = tx_buf->skb;
603 tx_buf->skb = NULL;
604
605 if (tx_buf->is_push) {
606 tx_buf->is_push = 0;
607 goto next_tx_int;
608 }
609
610 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
611 skb_headlen(skb), PCI_DMA_TODEVICE);
612 last = tx_buf->nr_frags;
613
614 for (j = 0; j < last; j++) {
615 cons = NEXT_TX(cons);
616 tx_buf = &txr->tx_buf_ring[cons];
617 dma_unmap_page(
618 &pdev->dev,
619 dma_unmap_addr(tx_buf, mapping),
620 skb_frag_size(&skb_shinfo(skb)->frags[j]),
621 PCI_DMA_TODEVICE);
622 }
623
624next_tx_int:
625 cons = NEXT_TX(cons);
626
627 tx_bytes += skb->len;
628 dev_kfree_skb_any(skb);
629 }
630
631 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
632 txr->tx_cons = cons;
633
634 /* Need to make the tx_cons update visible to bnxt_start_xmit()
635 * before checking for netif_tx_queue_stopped(). Without the
636 * memory barrier, there is a small possibility that bnxt_start_xmit()
637 * will miss it and cause the queue to be stopped forever.
638 */
639 smp_mb();
640
641 if (unlikely(netif_tx_queue_stopped(txq)) &&
642 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
643 __netif_tx_lock(txq, smp_processor_id());
644 if (netif_tx_queue_stopped(txq) &&
645 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
646 txr->dev_state != BNXT_DEV_STATE_CLOSING)
647 netif_tx_wake_queue(txq);
648 __netif_tx_unlock(txq);
649 }
650}
651
Michael Chanc61fb992017-02-06 16:55:36 -0500652static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
653 gfp_t gfp)
654{
655 struct device *dev = &bp->pdev->dev;
656 struct page *page;
657
658 page = alloc_page(gfp);
659 if (!page)
660 return NULL;
661
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700662 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
663 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500664 if (dma_mapping_error(dev, *mapping)) {
665 __free_page(page);
666 return NULL;
667 }
668 *mapping += bp->rx_dma_offset;
669 return page;
670}
671
Michael Chanc0c050c2015-10-22 16:01:17 -0400672static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
673 gfp_t gfp)
674{
675 u8 *data;
676 struct pci_dev *pdev = bp->pdev;
677
678 data = kmalloc(bp->rx_buf_size, gfp);
679 if (!data)
680 return NULL;
681
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700682 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
683 bp->rx_buf_use_size, bp->rx_dir,
684 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400685
686 if (dma_mapping_error(&pdev->dev, *mapping)) {
687 kfree(data);
688 data = NULL;
689 }
690 return data;
691}
692
Michael Chan38413402017-02-06 16:55:43 -0500693int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
694 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400695{
696 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
697 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400698 dma_addr_t mapping;
699
Michael Chanc61fb992017-02-06 16:55:36 -0500700 if (BNXT_RX_PAGE_MODE(bp)) {
701 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400702
Michael Chanc61fb992017-02-06 16:55:36 -0500703 if (!page)
704 return -ENOMEM;
705
706 rx_buf->data = page;
707 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
708 } else {
709 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
710
711 if (!data)
712 return -ENOMEM;
713
714 rx_buf->data = data;
715 rx_buf->data_ptr = data + bp->rx_offset;
716 }
Michael Chan11cd1192017-02-06 16:55:33 -0500717 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400718
719 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400720 return 0;
721}
722
Michael Chanc6d30e82017-02-06 16:55:42 -0500723void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400724{
725 u16 prod = rxr->rx_prod;
726 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
727 struct rx_bd *cons_bd, *prod_bd;
728
729 prod_rx_buf = &rxr->rx_buf_ring[prod];
730 cons_rx_buf = &rxr->rx_buf_ring[cons];
731
732 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500733 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400734
Michael Chan11cd1192017-02-06 16:55:33 -0500735 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400736
737 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
738 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
739
740 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
741}
742
743static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
744{
745 u16 next, max = rxr->rx_agg_bmap_size;
746
747 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
748 if (next >= max)
749 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
750 return next;
751}
752
753static inline int bnxt_alloc_rx_page(struct bnxt *bp,
754 struct bnxt_rx_ring_info *rxr,
755 u16 prod, gfp_t gfp)
756{
757 struct rx_bd *rxbd =
758 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
759 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
760 struct pci_dev *pdev = bp->pdev;
761 struct page *page;
762 dma_addr_t mapping;
763 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400764 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400765
Michael Chan89d0a062016-04-25 02:30:51 -0400766 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
767 page = rxr->rx_page;
768 if (!page) {
769 page = alloc_page(gfp);
770 if (!page)
771 return -ENOMEM;
772 rxr->rx_page = page;
773 rxr->rx_page_offset = 0;
774 }
775 offset = rxr->rx_page_offset;
776 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
777 if (rxr->rx_page_offset == PAGE_SIZE)
778 rxr->rx_page = NULL;
779 else
780 get_page(page);
781 } else {
782 page = alloc_page(gfp);
783 if (!page)
784 return -ENOMEM;
785 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400786
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700787 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
788 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
789 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400790 if (dma_mapping_error(&pdev->dev, mapping)) {
791 __free_page(page);
792 return -EIO;
793 }
794
795 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
796 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
797
798 __set_bit(sw_prod, rxr->rx_agg_bmap);
799 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
800 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
801
802 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400803 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400804 rx_agg_buf->mapping = mapping;
805 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
806 rxbd->rx_bd_opaque = sw_prod;
807 return 0;
808}
809
810static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
811 u32 agg_bufs)
812{
813 struct bnxt *bp = bnapi->bp;
814 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500815 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400816 u16 prod = rxr->rx_agg_prod;
817 u16 sw_prod = rxr->rx_sw_agg_prod;
818 u32 i;
819
820 for (i = 0; i < agg_bufs; i++) {
821 u16 cons;
822 struct rx_agg_cmp *agg;
823 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
824 struct rx_bd *prod_bd;
825 struct page *page;
826
827 agg = (struct rx_agg_cmp *)
828 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
829 cons = agg->rx_agg_cmp_opaque;
830 __clear_bit(cons, rxr->rx_agg_bmap);
831
832 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
833 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
834
835 __set_bit(sw_prod, rxr->rx_agg_bmap);
836 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
837 cons_rx_buf = &rxr->rx_agg_ring[cons];
838
839 /* It is possible for sw_prod to be equal to cons, so
840 * set cons_rx_buf->page to NULL first.
841 */
842 page = cons_rx_buf->page;
843 cons_rx_buf->page = NULL;
844 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400845 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400846
847 prod_rx_buf->mapping = cons_rx_buf->mapping;
848
849 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
850
851 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
852 prod_bd->rx_bd_opaque = sw_prod;
853
854 prod = NEXT_RX_AGG(prod);
855 sw_prod = NEXT_RX_AGG(sw_prod);
856 cp_cons = NEXT_CMP(cp_cons);
857 }
858 rxr->rx_agg_prod = prod;
859 rxr->rx_sw_agg_prod = sw_prod;
860}
861
Michael Chanc61fb992017-02-06 16:55:36 -0500862static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
863 struct bnxt_rx_ring_info *rxr,
864 u16 cons, void *data, u8 *data_ptr,
865 dma_addr_t dma_addr,
866 unsigned int offset_and_len)
867{
868 unsigned int payload = offset_and_len >> 16;
869 unsigned int len = offset_and_len & 0xffff;
870 struct skb_frag_struct *frag;
871 struct page *page = data;
872 u16 prod = rxr->rx_prod;
873 struct sk_buff *skb;
874 int off, err;
875
876 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
877 if (unlikely(err)) {
878 bnxt_reuse_rx_data(rxr, cons, data);
879 return NULL;
880 }
881 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700882 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
883 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500884
885 if (unlikely(!payload))
886 payload = eth_get_headlen(data_ptr, len);
887
888 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
889 if (!skb) {
890 __free_page(page);
891 return NULL;
892 }
893
894 off = (void *)data_ptr - page_address(page);
895 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
896 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
897 payload + NET_IP_ALIGN);
898
899 frag = &skb_shinfo(skb)->frags[0];
900 skb_frag_size_sub(frag, payload);
901 frag->page_offset += payload;
902 skb->data_len -= payload;
903 skb->tail += payload;
904
905 return skb;
906}
907
Michael Chanc0c050c2015-10-22 16:01:17 -0400908static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
909 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500910 void *data, u8 *data_ptr,
911 dma_addr_t dma_addr,
912 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400913{
Michael Chan6bb19472017-02-06 16:55:32 -0500914 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400915 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500916 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400917
918 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
919 if (unlikely(err)) {
920 bnxt_reuse_rx_data(rxr, cons, data);
921 return NULL;
922 }
923
924 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700925 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
926 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400927 if (!skb) {
928 kfree(data);
929 return NULL;
930 }
931
Michael Chanb3dba772017-02-06 16:55:35 -0500932 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500933 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400934 return skb;
935}
936
937static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
938 struct sk_buff *skb, u16 cp_cons,
939 u32 agg_bufs)
940{
941 struct pci_dev *pdev = bp->pdev;
942 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500943 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400944 u16 prod = rxr->rx_agg_prod;
945 u32 i;
946
947 for (i = 0; i < agg_bufs; i++) {
948 u16 cons, frag_len;
949 struct rx_agg_cmp *agg;
950 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
951 struct page *page;
952 dma_addr_t mapping;
953
954 agg = (struct rx_agg_cmp *)
955 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
956 cons = agg->rx_agg_cmp_opaque;
957 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
958 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
959
960 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400961 skb_fill_page_desc(skb, i, cons_rx_buf->page,
962 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400963 __clear_bit(cons, rxr->rx_agg_bmap);
964
965 /* It is possible for bnxt_alloc_rx_page() to allocate
966 * a sw_prod index that equals the cons index, so we
967 * need to clear the cons entry now.
968 */
Michael Chan11cd1192017-02-06 16:55:33 -0500969 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400970 page = cons_rx_buf->page;
971 cons_rx_buf->page = NULL;
972
973 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
974 struct skb_shared_info *shinfo;
975 unsigned int nr_frags;
976
977 shinfo = skb_shinfo(skb);
978 nr_frags = --shinfo->nr_frags;
979 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
980
981 dev_kfree_skb(skb);
982
983 cons_rx_buf->page = page;
984
985 /* Update prod since possibly some pages have been
986 * allocated already.
987 */
988 rxr->rx_agg_prod = prod;
989 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
990 return NULL;
991 }
992
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700993 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
994 PCI_DMA_FROMDEVICE,
995 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400996
997 skb->data_len += frag_len;
998 skb->len += frag_len;
999 skb->truesize += PAGE_SIZE;
1000
1001 prod = NEXT_RX_AGG(prod);
1002 cp_cons = NEXT_CMP(cp_cons);
1003 }
1004 rxr->rx_agg_prod = prod;
1005 return skb;
1006}
1007
1008static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1009 u8 agg_bufs, u32 *raw_cons)
1010{
1011 u16 last;
1012 struct rx_agg_cmp *agg;
1013
1014 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1015 last = RING_CMP(*raw_cons);
1016 agg = (struct rx_agg_cmp *)
1017 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1018 return RX_AGG_CMP_VALID(agg, *raw_cons);
1019}
1020
1021static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1022 unsigned int len,
1023 dma_addr_t mapping)
1024{
1025 struct bnxt *bp = bnapi->bp;
1026 struct pci_dev *pdev = bp->pdev;
1027 struct sk_buff *skb;
1028
1029 skb = napi_alloc_skb(&bnapi->napi, len);
1030 if (!skb)
1031 return NULL;
1032
Michael Chan745fc052017-02-06 16:55:34 -05001033 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1034 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001035
Michael Chan6bb19472017-02-06 16:55:32 -05001036 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1037 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -04001038
Michael Chan745fc052017-02-06 16:55:34 -05001039 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1040 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001041
1042 skb_put(skb, len);
1043 return skb;
1044}
1045
Michael Chanfa7e2812016-05-10 19:18:00 -04001046static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1047 u32 *raw_cons, void *cmp)
1048{
1049 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1050 struct rx_cmp *rxcmp = cmp;
1051 u32 tmp_raw_cons = *raw_cons;
1052 u8 cmp_type, agg_bufs = 0;
1053
1054 cmp_type = RX_CMP_TYPE(rxcmp);
1055
1056 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1057 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1058 RX_CMP_AGG_BUFS) >>
1059 RX_CMP_AGG_BUFS_SHIFT;
1060 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1061 struct rx_tpa_end_cmp *tpa_end = cmp;
1062
1063 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1064 RX_TPA_END_CMP_AGG_BUFS) >>
1065 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1066 }
1067
1068 if (agg_bufs) {
1069 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1070 return -EBUSY;
1071 }
1072 *raw_cons = tmp_raw_cons;
1073 return 0;
1074}
1075
Michael Chanc213eae2017-10-13 21:09:29 -04001076static void bnxt_queue_sp_work(struct bnxt *bp)
1077{
1078 if (BNXT_PF(bp))
1079 queue_work(bnxt_pf_wq, &bp->sp_task);
1080 else
1081 schedule_work(&bp->sp_task);
1082}
1083
1084static void bnxt_cancel_sp_work(struct bnxt *bp)
1085{
1086 if (BNXT_PF(bp))
1087 flush_workqueue(bnxt_pf_wq);
1088 else
1089 cancel_work_sync(&bp->sp_task);
1090}
1091
Michael Chanfa7e2812016-05-10 19:18:00 -04001092static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1093{
1094 if (!rxr->bnapi->in_reset) {
1095 rxr->bnapi->in_reset = true;
1096 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001097 bnxt_queue_sp_work(bp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001098 }
1099 rxr->rx_next_cons = 0xffff;
1100}
1101
Michael Chanc0c050c2015-10-22 16:01:17 -04001102static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1103 struct rx_tpa_start_cmp *tpa_start,
1104 struct rx_tpa_start_cmp_ext *tpa_start1)
1105{
1106 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1107 u16 cons, prod;
1108 struct bnxt_tpa_info *tpa_info;
1109 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1110 struct rx_bd *prod_bd;
1111 dma_addr_t mapping;
1112
1113 cons = tpa_start->rx_tpa_start_cmp_opaque;
1114 prod = rxr->rx_prod;
1115 cons_rx_buf = &rxr->rx_buf_ring[cons];
1116 prod_rx_buf = &rxr->rx_buf_ring[prod];
1117 tpa_info = &rxr->rx_tpa[agg_id];
1118
Michael Chanfa7e2812016-05-10 19:18:00 -04001119 if (unlikely(cons != rxr->rx_next_cons)) {
1120 bnxt_sched_reset(bp, rxr);
1121 return;
1122 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001123 /* Store cfa_code in tpa_info to use in tpa_end
1124 * completion processing.
1125 */
1126 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001127 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001128 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001129
1130 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001131 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001132
1133 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1134
1135 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1136
1137 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001138 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001139 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001140 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001141
1142 tpa_info->len =
1143 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1144 RX_TPA_START_CMP_LEN_SHIFT;
1145 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1146 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1147
1148 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1149 tpa_info->gso_type = SKB_GSO_TCPV4;
1150 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
Michael Chan50f011b2018-08-05 16:51:51 -04001151 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
Michael Chanc0c050c2015-10-22 16:01:17 -04001152 tpa_info->gso_type = SKB_GSO_TCPV6;
1153 tpa_info->rss_hash =
1154 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1155 } else {
1156 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1157 tpa_info->gso_type = 0;
1158 if (netif_msg_rx_err(bp))
1159 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1160 }
1161 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1162 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001163 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001164
1165 rxr->rx_prod = NEXT_RX(prod);
1166 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001167 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001168 cons_rx_buf = &rxr->rx_buf_ring[cons];
1169
1170 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1171 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1172 cons_rx_buf->data = NULL;
1173}
1174
1175static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1176 u16 cp_cons, u32 agg_bufs)
1177{
1178 if (agg_bufs)
1179 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1180}
1181
Michael Chan94758f82016-06-13 02:25:35 -04001182static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1183 int payload_off, int tcp_ts,
1184 struct sk_buff *skb)
1185{
1186#ifdef CONFIG_INET
1187 struct tcphdr *th;
1188 int len, nw_off;
1189 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1190 u32 hdr_info = tpa_info->hdr_info;
1191 bool loopback = false;
1192
1193 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1194 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1195 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1196
1197 /* If the packet is an internal loopback packet, the offsets will
1198 * have an extra 4 bytes.
1199 */
1200 if (inner_mac_off == 4) {
1201 loopback = true;
1202 } else if (inner_mac_off > 4) {
1203 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1204 ETH_HLEN - 2));
1205
1206 /* We only support inner iPv4/ipv6. If we don't see the
1207 * correct protocol ID, it must be a loopback packet where
1208 * the offsets are off by 4.
1209 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001210 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001211 loopback = true;
1212 }
1213 if (loopback) {
1214 /* internal loopback packet, subtract all offsets by 4 */
1215 inner_ip_off -= 4;
1216 inner_mac_off -= 4;
1217 outer_ip_off -= 4;
1218 }
1219
1220 nw_off = inner_ip_off - ETH_HLEN;
1221 skb_set_network_header(skb, nw_off);
1222 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1223 struct ipv6hdr *iph = ipv6_hdr(skb);
1224
1225 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1226 len = skb->len - skb_transport_offset(skb);
1227 th = tcp_hdr(skb);
1228 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1229 } else {
1230 struct iphdr *iph = ip_hdr(skb);
1231
1232 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1233 len = skb->len - skb_transport_offset(skb);
1234 th = tcp_hdr(skb);
1235 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1236 }
1237
1238 if (inner_mac_off) { /* tunnel */
1239 struct udphdr *uh = NULL;
1240 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1241 ETH_HLEN - 2));
1242
1243 if (proto == htons(ETH_P_IP)) {
1244 struct iphdr *iph = (struct iphdr *)skb->data;
1245
1246 if (iph->protocol == IPPROTO_UDP)
1247 uh = (struct udphdr *)(iph + 1);
1248 } else {
1249 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1250
1251 if (iph->nexthdr == IPPROTO_UDP)
1252 uh = (struct udphdr *)(iph + 1);
1253 }
1254 if (uh) {
1255 if (uh->check)
1256 skb_shinfo(skb)->gso_type |=
1257 SKB_GSO_UDP_TUNNEL_CSUM;
1258 else
1259 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1260 }
1261 }
1262#endif
1263 return skb;
1264}
1265
Michael Chanc0c050c2015-10-22 16:01:17 -04001266#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1267#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1268
Michael Chan309369c2016-06-13 02:25:34 -04001269static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1270 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001271 struct sk_buff *skb)
1272{
Michael Chand1611c32015-10-25 22:27:57 -04001273#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001274 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001275 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001276
Michael Chan309369c2016-06-13 02:25:34 -04001277 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001278 tcp_opt_len = 12;
1279
Michael Chanc0c050c2015-10-22 16:01:17 -04001280 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1281 struct iphdr *iph;
1282
1283 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1284 ETH_HLEN;
1285 skb_set_network_header(skb, nw_off);
1286 iph = ip_hdr(skb);
1287 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1288 len = skb->len - skb_transport_offset(skb);
1289 th = tcp_hdr(skb);
1290 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1291 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1292 struct ipv6hdr *iph;
1293
1294 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1295 ETH_HLEN;
1296 skb_set_network_header(skb, nw_off);
1297 iph = ipv6_hdr(skb);
1298 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1299 len = skb->len - skb_transport_offset(skb);
1300 th = tcp_hdr(skb);
1301 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1302 } else {
1303 dev_kfree_skb_any(skb);
1304 return NULL;
1305 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001306
1307 if (nw_off) { /* tunnel */
1308 struct udphdr *uh = NULL;
1309
1310 if (skb->protocol == htons(ETH_P_IP)) {
1311 struct iphdr *iph = (struct iphdr *)skb->data;
1312
1313 if (iph->protocol == IPPROTO_UDP)
1314 uh = (struct udphdr *)(iph + 1);
1315 } else {
1316 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1317
1318 if (iph->nexthdr == IPPROTO_UDP)
1319 uh = (struct udphdr *)(iph + 1);
1320 }
1321 if (uh) {
1322 if (uh->check)
1323 skb_shinfo(skb)->gso_type |=
1324 SKB_GSO_UDP_TUNNEL_CSUM;
1325 else
1326 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1327 }
1328 }
1329#endif
1330 return skb;
1331}
1332
Michael Chan309369c2016-06-13 02:25:34 -04001333static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1334 struct bnxt_tpa_info *tpa_info,
1335 struct rx_tpa_end_cmp *tpa_end,
1336 struct rx_tpa_end_cmp_ext *tpa_end1,
1337 struct sk_buff *skb)
1338{
1339#ifdef CONFIG_INET
1340 int payload_off;
1341 u16 segs;
1342
1343 segs = TPA_END_TPA_SEGS(tpa_end);
1344 if (segs == 1)
1345 return skb;
1346
1347 NAPI_GRO_CB(skb)->count = segs;
1348 skb_shinfo(skb)->gso_size =
1349 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1350 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1351 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1352 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1353 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1354 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001355 if (likely(skb))
1356 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001357#endif
1358 return skb;
1359}
1360
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001361/* Given the cfa_code of a received packet determine which
1362 * netdev (vf-rep or PF) the packet is destined to.
1363 */
1364static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1365{
1366 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1367
1368 /* if vf-rep dev is NULL, the must belongs to the PF */
1369 return dev ? dev : bp->dev;
1370}
1371
Michael Chanc0c050c2015-10-22 16:01:17 -04001372static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1373 struct bnxt_napi *bnapi,
1374 u32 *raw_cons,
1375 struct rx_tpa_end_cmp *tpa_end,
1376 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001377 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001378{
1379 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001380 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001381 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001382 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001383 u16 cp_cons = RING_CMP(*raw_cons);
1384 unsigned int len;
1385 struct bnxt_tpa_info *tpa_info;
1386 dma_addr_t mapping;
1387 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001388 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001389
Michael Chanfa7e2812016-05-10 19:18:00 -04001390 if (unlikely(bnapi->in_reset)) {
1391 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1392
1393 if (rc < 0)
1394 return ERR_PTR(-EBUSY);
1395 return NULL;
1396 }
1397
Michael Chanc0c050c2015-10-22 16:01:17 -04001398 tpa_info = &rxr->rx_tpa[agg_id];
1399 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001400 data_ptr = tpa_info->data_ptr;
1401 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001402 len = tpa_info->len;
1403 mapping = tpa_info->mapping;
1404
1405 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1406 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1407
1408 if (agg_bufs) {
1409 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1410 return ERR_PTR(-EBUSY);
1411
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001412 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001413 cp_cons = NEXT_CMP(cp_cons);
1414 }
1415
Michael Chan69c149e2017-06-23 14:01:00 -04001416 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001417 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001418 if (agg_bufs > MAX_SKB_FRAGS)
1419 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1420 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001421 return NULL;
1422 }
1423
1424 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001425 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001426 if (!skb) {
1427 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1428 return NULL;
1429 }
1430 } else {
1431 u8 *new_data;
1432 dma_addr_t new_mapping;
1433
1434 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1435 if (!new_data) {
1436 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1437 return NULL;
1438 }
1439
1440 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001441 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001442 tpa_info->mapping = new_mapping;
1443
1444 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001445 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1446 bp->rx_buf_use_size, bp->rx_dir,
1447 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001448
1449 if (!skb) {
1450 kfree(data);
1451 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1452 return NULL;
1453 }
Michael Chanb3dba772017-02-06 16:55:35 -05001454 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001455 skb_put(skb, len);
1456 }
1457
1458 if (agg_bufs) {
1459 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1460 if (!skb) {
1461 /* Page reuse already handled by bnxt_rx_pages(). */
1462 return NULL;
1463 }
1464 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001465
1466 skb->protocol =
1467 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001468
1469 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1470 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1471
Michael Chan8852ddb2016-06-06 02:37:16 -04001472 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1473 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001474 u16 vlan_proto = tpa_info->metadata >>
1475 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chaned7bc6022018-03-09 23:46:06 -05001476 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001477
Michael Chan8852ddb2016-06-06 02:37:16 -04001478 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001479 }
1480
1481 skb_checksum_none_assert(skb);
1482 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1483 skb->ip_summed = CHECKSUM_UNNECESSARY;
1484 skb->csum_level =
1485 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1486 }
1487
1488 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001489 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001490
1491 return skb;
1492}
1493
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001494static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1495 struct sk_buff *skb)
1496{
1497 if (skb->dev != bp->dev) {
1498 /* this packet belongs to a vf-rep */
1499 bnxt_vf_rep_rx(bp, skb);
1500 return;
1501 }
1502 skb_record_rx_queue(skb, bnapi->index);
1503 napi_gro_receive(&bnapi->napi, skb);
1504}
1505
Michael Chanc0c050c2015-10-22 16:01:17 -04001506/* returns the following:
1507 * 1 - 1 packet successfully received
1508 * 0 - successful TPA_START, packet not completed yet
1509 * -EBUSY - completion ring does not have all the agg buffers yet
1510 * -ENOMEM - packet aborted due to out of memory
1511 * -EIO - packet aborted due to hw error indicated in BD
1512 */
1513static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001514 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001515{
1516 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001517 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001518 struct net_device *dev = bp->dev;
1519 struct rx_cmp *rxcmp;
1520 struct rx_cmp_ext *rxcmp1;
1521 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001522 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001523 struct bnxt_sw_rx_bd *rx_buf;
1524 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001525 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001526 dma_addr_t dma_addr;
1527 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001528 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001529 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001530 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001531
1532 rxcmp = (struct rx_cmp *)
1533 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1534
1535 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1536 cp_cons = RING_CMP(tmp_raw_cons);
1537 rxcmp1 = (struct rx_cmp_ext *)
1538 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1539
1540 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1541 return -EBUSY;
1542
1543 cmp_type = RX_CMP_TYPE(rxcmp);
1544
1545 prod = rxr->rx_prod;
1546
1547 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1548 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1549 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1550
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001551 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001552 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001553
1554 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1555 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1556 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001557 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001558
Tobias Klauser1fac4b22017-09-26 15:12:26 +02001559 if (IS_ERR(skb))
Michael Chanc0c050c2015-10-22 16:01:17 -04001560 return -EBUSY;
1561
1562 rc = -ENOMEM;
1563 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001564 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001565 rc = 1;
1566 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001567 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001568 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001569 }
1570
1571 cons = rxcmp->rx_cmp_opaque;
1572 rx_buf = &rxr->rx_buf_ring[cons];
1573 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001574 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001575 if (unlikely(cons != rxr->rx_next_cons)) {
1576 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1577
1578 bnxt_sched_reset(bp, rxr);
1579 return rc1;
1580 }
Michael Chan6bb19472017-02-06 16:55:32 -05001581 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001582
Michael Chanc61fb992017-02-06 16:55:36 -05001583 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1584 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001585
1586 if (agg_bufs) {
1587 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1588 return -EBUSY;
1589
1590 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001591 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001592 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001593 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001594
1595 rx_buf->data = NULL;
1596 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1597 bnxt_reuse_rx_data(rxr, cons, data);
1598 if (agg_bufs)
1599 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1600
1601 rc = -EIO;
1602 goto next_rx;
1603 }
1604
1605 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001606 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001607
Michael Chanc6d30e82017-02-06 16:55:42 -05001608 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1609 rc = 1;
1610 goto next_rx;
1611 }
1612
Michael Chanc0c050c2015-10-22 16:01:17 -04001613 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001614 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001615 bnxt_reuse_rx_data(rxr, cons, data);
1616 if (!skb) {
1617 rc = -ENOMEM;
1618 goto next_rx;
1619 }
1620 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001621 u32 payload;
1622
Michael Chanc6d30e82017-02-06 16:55:42 -05001623 if (rx_buf->data_ptr == data_ptr)
1624 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1625 else
1626 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001627 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001628 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001629 if (!skb) {
1630 rc = -ENOMEM;
1631 goto next_rx;
1632 }
1633 }
1634
1635 if (agg_bufs) {
1636 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1637 if (!skb) {
1638 rc = -ENOMEM;
1639 goto next_rx;
1640 }
1641 }
1642
1643 if (RX_CMP_HASH_VALID(rxcmp)) {
1644 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1645 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1646
1647 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1648 if (hash_type != 1 && hash_type != 3)
1649 type = PKT_HASH_TYPE_L3;
1650 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1651 }
1652
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001653 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1654 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001655
Michael Chan8852ddb2016-06-06 02:37:16 -04001656 if ((rxcmp1->rx_cmp_flags2 &
1657 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1658 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001659 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chaned7bc6022018-03-09 23:46:06 -05001660 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001661 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1662
Michael Chan8852ddb2016-06-06 02:37:16 -04001663 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001664 }
1665
1666 skb_checksum_none_assert(skb);
1667 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1668 if (dev->features & NETIF_F_RXCSUM) {
1669 skb->ip_summed = CHECKSUM_UNNECESSARY;
1670 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1671 }
1672 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001673 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1674 if (dev->features & NETIF_F_RXCSUM)
1675 cpr->rx_l4_csum_errors++;
1676 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001677 }
1678
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001679 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001680 rc = 1;
1681
1682next_rx:
1683 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001684 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001685
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001686 cpr->rx_packets += 1;
1687 cpr->rx_bytes += len;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001688
1689next_rx_no_prod_no_len:
Michael Chanc0c050c2015-10-22 16:01:17 -04001690 *raw_cons = tmp_raw_cons;
1691
1692 return rc;
1693}
1694
Michael Chan2270bc52017-06-23 14:01:01 -04001695/* In netpoll mode, if we are using a combined completion ring, we need to
1696 * discard the rx packets and recycle the buffers.
1697 */
1698static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1699 u32 *raw_cons, u8 *event)
1700{
1701 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1702 u32 tmp_raw_cons = *raw_cons;
1703 struct rx_cmp_ext *rxcmp1;
1704 struct rx_cmp *rxcmp;
1705 u16 cp_cons;
1706 u8 cmp_type;
1707
1708 cp_cons = RING_CMP(tmp_raw_cons);
1709 rxcmp = (struct rx_cmp *)
1710 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1711
1712 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1713 cp_cons = RING_CMP(tmp_raw_cons);
1714 rxcmp1 = (struct rx_cmp_ext *)
1715 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1716
1717 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1718 return -EBUSY;
1719
1720 cmp_type = RX_CMP_TYPE(rxcmp);
1721 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1722 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1723 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1724 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1725 struct rx_tpa_end_cmp_ext *tpa_end1;
1726
1727 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1728 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1729 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1730 }
1731 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1732}
1733
Michael Chan4bb13ab2016-04-05 14:09:01 -04001734#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001735 ((data) & \
1736 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001737
Michael Chanc0c050c2015-10-22 16:01:17 -04001738static int bnxt_async_event_process(struct bnxt *bp,
1739 struct hwrm_async_event_cmpl *cmpl)
1740{
1741 u16 event_id = le16_to_cpu(cmpl->event_id);
1742
1743 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1744 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001745 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001746 u32 data1 = le32_to_cpu(cmpl->event_data1);
1747 struct bnxt_link_info *link_info = &bp->link_info;
1748
1749 if (BNXT_VF(bp))
1750 goto async_event_process_exit;
Michael Chana8168b62017-12-06 17:31:22 -05001751
1752 /* print unsupported speed warning in forced speed mode only */
1753 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1754 (data1 & 0x20000)) {
Michael Chan8cbde112016-04-11 04:11:14 -04001755 u16 fw_speed = link_info->force_link_speed;
1756 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1757
Michael Chana8168b62017-12-06 17:31:22 -05001758 if (speed != SPEED_UNKNOWN)
1759 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1760 speed);
Michael Chan8cbde112016-04-11 04:11:14 -04001761 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001762 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001763 }
Gustavo A. R. Silvabc171e82018-08-07 18:11:14 -05001764 /* fall through */
Michael Chan87c374d2016-12-02 21:17:16 -05001765 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001766 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001767 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001768 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001769 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001770 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001771 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001772 u32 data1 = le32_to_cpu(cmpl->event_data1);
1773 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1774
1775 if (BNXT_VF(bp))
1776 break;
1777
1778 if (bp->pf.port_id != port_id)
1779 break;
1780
Michael Chan4bb13ab2016-04-05 14:09:01 -04001781 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1782 break;
1783 }
Michael Chan87c374d2016-12-02 21:17:16 -05001784 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001785 if (BNXT_PF(bp))
1786 goto async_event_process_exit;
1787 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1788 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001789 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001790 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001791 }
Michael Chanc213eae2017-10-13 21:09:29 -04001792 bnxt_queue_sp_work(bp);
Jeffrey Huang19241362016-02-26 04:00:00 -05001793async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001794 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001795 return 0;
1796}
1797
1798static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1799{
1800 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1801 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1802 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1803 (struct hwrm_fwd_req_cmpl *)txcmp;
1804
1805 switch (cmpl_type) {
1806 case CMPL_BASE_TYPE_HWRM_DONE:
1807 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1808 if (seq_id == bp->hwrm_intr_seq_id)
1809 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1810 else
1811 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1812 break;
1813
1814 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1815 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1816
1817 if ((vf_id < bp->pf.first_vf_id) ||
1818 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1819 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1820 vf_id);
1821 return -EINVAL;
1822 }
1823
1824 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1825 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001826 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04001827 break;
1828
1829 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1830 bnxt_async_event_process(bp,
1831 (struct hwrm_async_event_cmpl *)txcmp);
1832
1833 default:
1834 break;
1835 }
1836
1837 return 0;
1838}
1839
1840static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1841{
1842 struct bnxt_napi *bnapi = dev_instance;
1843 struct bnxt *bp = bnapi->bp;
1844 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1845 u32 cons = RING_CMP(cpr->cp_raw_cons);
1846
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001847 cpr->event_ctr++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001848 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1849 napi_schedule(&bnapi->napi);
1850 return IRQ_HANDLED;
1851}
1852
1853static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1854{
1855 u32 raw_cons = cpr->cp_raw_cons;
1856 u16 cons = RING_CMP(raw_cons);
1857 struct tx_cmp *txcmp;
1858
1859 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1860
1861 return TX_CMP_VALID(txcmp, raw_cons);
1862}
1863
Michael Chanc0c050c2015-10-22 16:01:17 -04001864static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1865{
1866 struct bnxt_napi *bnapi = dev_instance;
1867 struct bnxt *bp = bnapi->bp;
1868 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1869 u32 cons = RING_CMP(cpr->cp_raw_cons);
1870 u32 int_status;
1871
1872 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1873
1874 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001875 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001876 /* return if erroneous interrupt */
1877 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1878 return IRQ_NONE;
1879 }
1880
1881 /* disable ring IRQ */
Michael Chan697197e2018-10-14 07:02:46 -04001882 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
Michael Chanc0c050c2015-10-22 16:01:17 -04001883
1884 /* Return here if interrupt is shared and is disabled. */
1885 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1886 return IRQ_HANDLED;
1887
1888 napi_schedule(&bnapi->napi);
1889 return IRQ_HANDLED;
1890}
1891
1892static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1893{
1894 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1895 u32 raw_cons = cpr->cp_raw_cons;
1896 u32 cons;
1897 int tx_pkts = 0;
1898 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001899 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001900 struct tx_cmp *txcmp;
1901
1902 while (1) {
1903 int rc;
1904
1905 cons = RING_CMP(raw_cons);
1906 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1907
1908 if (!TX_CMP_VALID(txcmp, raw_cons))
1909 break;
1910
Michael Chan67a95e22016-05-04 16:56:43 -04001911 /* The valid test of the entry must be done first before
1912 * reading any further.
1913 */
Michael Chanb67daab2016-05-15 03:04:51 -04001914 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001915 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1916 tx_pkts++;
1917 /* return full budget so NAPI will complete. */
Michael Chan73f21c62018-09-26 00:41:04 -04001918 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001919 rx_pkts = budget;
Michael Chan73f21c62018-09-26 00:41:04 -04001920 raw_cons = NEXT_RAW_CMP(raw_cons);
1921 break;
1922 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001923 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001924 if (likely(budget))
1925 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1926 else
1927 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1928 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001929 if (likely(rc >= 0))
1930 rx_pkts += rc;
Michael Chan903649e2017-08-28 13:40:30 -04001931 /* Increment rx_pkts when rc is -ENOMEM to count towards
1932 * the NAPI budget. Otherwise, we may potentially loop
1933 * here forever if we consistently cannot allocate
1934 * buffers.
1935 */
Calvin Owens2edbdb32017-12-08 09:05:26 -08001936 else if (rc == -ENOMEM && budget)
Michael Chan903649e2017-08-28 13:40:30 -04001937 rx_pkts++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001938 else if (rc == -EBUSY) /* partial completion */
1939 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001940 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1941 CMPL_BASE_TYPE_HWRM_DONE) ||
1942 (TX_CMP_TYPE(txcmp) ==
1943 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1944 (TX_CMP_TYPE(txcmp) ==
1945 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1946 bnxt_hwrm_handler(bp, txcmp);
1947 }
1948 raw_cons = NEXT_RAW_CMP(raw_cons);
1949
Michael Chan73f21c62018-09-26 00:41:04 -04001950 if (rx_pkts && rx_pkts == budget)
Michael Chanc0c050c2015-10-22 16:01:17 -04001951 break;
1952 }
1953
Michael Chan38413402017-02-06 16:55:43 -05001954 if (event & BNXT_TX_EVENT) {
1955 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan38413402017-02-06 16:55:43 -05001956 u16 prod = txr->tx_prod;
1957
1958 /* Sync BD data before updating doorbell */
1959 wmb();
1960
Michael Chan697197e2018-10-14 07:02:46 -04001961 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
Michael Chan38413402017-02-06 16:55:43 -05001962 }
1963
Michael Chanc0c050c2015-10-22 16:01:17 -04001964 cpr->cp_raw_cons = raw_cons;
1965 /* ACK completion ring before freeing tx ring and producing new
1966 * buffers in rx/agg rings to prevent overflowing the completion
1967 * ring.
1968 */
Michael Chan697197e2018-10-14 07:02:46 -04001969 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001970
1971 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001972 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001973
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001974 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001975 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001976
Michael Chan697197e2018-10-14 07:02:46 -04001977 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
Michael Chan434c9752017-05-29 19:06:08 -04001978 if (event & BNXT_AGG_EVENT)
Michael Chan697197e2018-10-14 07:02:46 -04001979 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001980 }
1981 return rx_pkts;
1982}
1983
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001984static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1985{
1986 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1987 struct bnxt *bp = bnapi->bp;
1988 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1989 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1990 struct tx_cmp *txcmp;
1991 struct rx_cmp_ext *rxcmp1;
1992 u32 cp_cons, tmp_raw_cons;
1993 u32 raw_cons = cpr->cp_raw_cons;
1994 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001995 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001996
1997 while (1) {
1998 int rc;
1999
2000 cp_cons = RING_CMP(raw_cons);
2001 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2002
2003 if (!TX_CMP_VALID(txcmp, raw_cons))
2004 break;
2005
2006 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2007 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2008 cp_cons = RING_CMP(tmp_raw_cons);
2009 rxcmp1 = (struct rx_cmp_ext *)
2010 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2011
2012 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2013 break;
2014
2015 /* force an error to recycle the buffer */
2016 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2017 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2018
Michael Chan4e5dbbda2017-02-06 16:55:37 -05002019 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Calvin Owens2edbdb32017-12-08 09:05:26 -08002020 if (likely(rc == -EIO) && budget)
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002021 rx_pkts++;
2022 else if (rc == -EBUSY) /* partial completion */
2023 break;
2024 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2025 CMPL_BASE_TYPE_HWRM_DONE)) {
2026 bnxt_hwrm_handler(bp, txcmp);
2027 } else {
2028 netdev_err(bp->dev,
2029 "Invalid completion received on special ring\n");
2030 }
2031 raw_cons = NEXT_RAW_CMP(raw_cons);
2032
2033 if (rx_pkts == budget)
2034 break;
2035 }
2036
2037 cpr->cp_raw_cons = raw_cons;
Michael Chan697197e2018-10-14 07:02:46 -04002038 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2039 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002040
Michael Chan434c9752017-05-29 19:06:08 -04002041 if (event & BNXT_AGG_EVENT)
Michael Chan697197e2018-10-14 07:02:46 -04002042 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002043
2044 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08002045 napi_complete_done(napi, rx_pkts);
Michael Chan697197e2018-10-14 07:02:46 -04002046 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002047 }
2048 return rx_pkts;
2049}
2050
Michael Chanc0c050c2015-10-22 16:01:17 -04002051static int bnxt_poll(struct napi_struct *napi, int budget)
2052{
2053 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2054 struct bnxt *bp = bnapi->bp;
2055 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2056 int work_done = 0;
2057
Michael Chanc0c050c2015-10-22 16:01:17 -04002058 while (1) {
2059 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2060
Michael Chan73f21c62018-09-26 00:41:04 -04002061 if (work_done >= budget) {
2062 if (!budget)
Michael Chan697197e2018-10-14 07:02:46 -04002063 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002064 break;
Michael Chan73f21c62018-09-26 00:41:04 -04002065 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002066
2067 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05002068 if (napi_complete_done(napi, work_done))
Michael Chan697197e2018-10-14 07:02:46 -04002069 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002070 break;
2071 }
2072 }
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002073 if (bp->flags & BNXT_FLAG_DIM) {
2074 struct net_dim_sample dim_sample;
2075
2076 net_dim_sample(cpr->event_ctr,
2077 cpr->rx_packets,
2078 cpr->rx_bytes,
2079 &dim_sample);
2080 net_dim(&cpr->dim, dim_sample);
2081 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002082 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04002083 return work_done;
2084}
2085
Michael Chanc0c050c2015-10-22 16:01:17 -04002086static void bnxt_free_tx_skbs(struct bnxt *bp)
2087{
2088 int i, max_idx;
2089 struct pci_dev *pdev = bp->pdev;
2090
Michael Chanb6ab4b02016-01-02 23:44:59 -05002091 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002092 return;
2093
2094 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2095 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002096 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002097 int j;
2098
Michael Chanc0c050c2015-10-22 16:01:17 -04002099 for (j = 0; j < max_idx;) {
2100 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2101 struct sk_buff *skb = tx_buf->skb;
2102 int k, last;
2103
2104 if (!skb) {
2105 j++;
2106 continue;
2107 }
2108
2109 tx_buf->skb = NULL;
2110
2111 if (tx_buf->is_push) {
2112 dev_kfree_skb(skb);
2113 j += 2;
2114 continue;
2115 }
2116
2117 dma_unmap_single(&pdev->dev,
2118 dma_unmap_addr(tx_buf, mapping),
2119 skb_headlen(skb),
2120 PCI_DMA_TODEVICE);
2121
2122 last = tx_buf->nr_frags;
2123 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002124 for (k = 0; k < last; k++, j++) {
2125 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002126 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2127
Michael Chand612a572016-01-28 03:11:22 -05002128 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002129 dma_unmap_page(
2130 &pdev->dev,
2131 dma_unmap_addr(tx_buf, mapping),
2132 skb_frag_size(frag), PCI_DMA_TODEVICE);
2133 }
2134 dev_kfree_skb(skb);
2135 }
2136 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2137 }
2138}
2139
2140static void bnxt_free_rx_skbs(struct bnxt *bp)
2141{
2142 int i, max_idx, max_agg_idx;
2143 struct pci_dev *pdev = bp->pdev;
2144
Michael Chanb6ab4b02016-01-02 23:44:59 -05002145 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002146 return;
2147
2148 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2149 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2150 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002151 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002152 int j;
2153
Michael Chanc0c050c2015-10-22 16:01:17 -04002154 if (rxr->rx_tpa) {
2155 for (j = 0; j < MAX_TPA; j++) {
2156 struct bnxt_tpa_info *tpa_info =
2157 &rxr->rx_tpa[j];
2158 u8 *data = tpa_info->data;
2159
2160 if (!data)
2161 continue;
2162
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002163 dma_unmap_single_attrs(&pdev->dev,
2164 tpa_info->mapping,
2165 bp->rx_buf_use_size,
2166 bp->rx_dir,
2167 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002168
2169 tpa_info->data = NULL;
2170
2171 kfree(data);
2172 }
2173 }
2174
2175 for (j = 0; j < max_idx; j++) {
2176 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002177 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002178 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002179
2180 if (!data)
2181 continue;
2182
Michael Chanc0c050c2015-10-22 16:01:17 -04002183 rx_buf->data = NULL;
2184
Michael Chan3ed3a832017-03-28 19:47:31 -04002185 if (BNXT_RX_PAGE_MODE(bp)) {
2186 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002187 dma_unmap_page_attrs(&pdev->dev, mapping,
2188 PAGE_SIZE, bp->rx_dir,
2189 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002190 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002191 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002192 dma_unmap_single_attrs(&pdev->dev, mapping,
2193 bp->rx_buf_use_size,
2194 bp->rx_dir,
2195 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002196 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002197 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002198 }
2199
2200 for (j = 0; j < max_agg_idx; j++) {
2201 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2202 &rxr->rx_agg_ring[j];
2203 struct page *page = rx_agg_buf->page;
2204
2205 if (!page)
2206 continue;
2207
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002208 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2209 BNXT_RX_PAGE_SIZE,
2210 PCI_DMA_FROMDEVICE,
2211 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002212
2213 rx_agg_buf->page = NULL;
2214 __clear_bit(j, rxr->rx_agg_bmap);
2215
2216 __free_page(page);
2217 }
Michael Chan89d0a062016-04-25 02:30:51 -04002218 if (rxr->rx_page) {
2219 __free_page(rxr->rx_page);
2220 rxr->rx_page = NULL;
2221 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002222 }
2223}
2224
2225static void bnxt_free_skbs(struct bnxt *bp)
2226{
2227 bnxt_free_tx_skbs(bp);
2228 bnxt_free_rx_skbs(bp);
2229}
2230
Michael Chan6fe19882018-10-14 07:02:41 -04002231static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
Michael Chanc0c050c2015-10-22 16:01:17 -04002232{
2233 struct pci_dev *pdev = bp->pdev;
2234 int i;
2235
Michael Chan6fe19882018-10-14 07:02:41 -04002236 for (i = 0; i < rmem->nr_pages; i++) {
2237 if (!rmem->pg_arr[i])
Michael Chanc0c050c2015-10-22 16:01:17 -04002238 continue;
2239
Michael Chan6fe19882018-10-14 07:02:41 -04002240 dma_free_coherent(&pdev->dev, rmem->page_size,
2241 rmem->pg_arr[i], rmem->dma_arr[i]);
Michael Chanc0c050c2015-10-22 16:01:17 -04002242
Michael Chan6fe19882018-10-14 07:02:41 -04002243 rmem->pg_arr[i] = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002244 }
Michael Chan6fe19882018-10-14 07:02:41 -04002245 if (rmem->pg_tbl) {
2246 dma_free_coherent(&pdev->dev, rmem->nr_pages * 8,
2247 rmem->pg_tbl, rmem->pg_tbl_map);
2248 rmem->pg_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002249 }
Michael Chan6fe19882018-10-14 07:02:41 -04002250 if (rmem->vmem_size && *rmem->vmem) {
2251 vfree(*rmem->vmem);
2252 *rmem->vmem = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002253 }
2254}
2255
Michael Chan6fe19882018-10-14 07:02:41 -04002256static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
Michael Chanc0c050c2015-10-22 16:01:17 -04002257{
Michael Chanc0c050c2015-10-22 16:01:17 -04002258 struct pci_dev *pdev = bp->pdev;
Michael Chan66cca202018-10-14 07:02:42 -04002259 u64 valid_bit = 0;
Michael Chan6fe19882018-10-14 07:02:41 -04002260 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002261
Michael Chan66cca202018-10-14 07:02:42 -04002262 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2263 valid_bit = PTU_PTE_VALID;
Michael Chan6fe19882018-10-14 07:02:41 -04002264 if (rmem->nr_pages > 1) {
2265 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev,
2266 rmem->nr_pages * 8,
2267 &rmem->pg_tbl_map,
Michael Chanc0c050c2015-10-22 16:01:17 -04002268 GFP_KERNEL);
Michael Chan6fe19882018-10-14 07:02:41 -04002269 if (!rmem->pg_tbl)
Michael Chanc0c050c2015-10-22 16:01:17 -04002270 return -ENOMEM;
2271 }
2272
Michael Chan6fe19882018-10-14 07:02:41 -04002273 for (i = 0; i < rmem->nr_pages; i++) {
Michael Chan66cca202018-10-14 07:02:42 -04002274 u64 extra_bits = valid_bit;
2275
Michael Chan6fe19882018-10-14 07:02:41 -04002276 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2277 rmem->page_size,
2278 &rmem->dma_arr[i],
Michael Chanc0c050c2015-10-22 16:01:17 -04002279 GFP_KERNEL);
Michael Chan6fe19882018-10-14 07:02:41 -04002280 if (!rmem->pg_arr[i])
Michael Chanc0c050c2015-10-22 16:01:17 -04002281 return -ENOMEM;
2282
Michael Chan66cca202018-10-14 07:02:42 -04002283 if (rmem->nr_pages > 1) {
2284 if (i == rmem->nr_pages - 2 &&
2285 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2286 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2287 else if (i == rmem->nr_pages - 1 &&
2288 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2289 extra_bits |= PTU_PTE_LAST;
2290 rmem->pg_tbl[i] =
2291 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2292 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002293 }
2294
Michael Chan6fe19882018-10-14 07:02:41 -04002295 if (rmem->vmem_size) {
2296 *rmem->vmem = vzalloc(rmem->vmem_size);
2297 if (!(*rmem->vmem))
Michael Chanc0c050c2015-10-22 16:01:17 -04002298 return -ENOMEM;
2299 }
2300 return 0;
2301}
2302
2303static void bnxt_free_rx_rings(struct bnxt *bp)
2304{
2305 int i;
2306
Michael Chanb6ab4b02016-01-02 23:44:59 -05002307 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002308 return;
2309
2310 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002311 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002312 struct bnxt_ring_struct *ring;
2313
Michael Chanc6d30e82017-02-06 16:55:42 -05002314 if (rxr->xdp_prog)
2315 bpf_prog_put(rxr->xdp_prog);
2316
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002317 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2318 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2319
Michael Chanc0c050c2015-10-22 16:01:17 -04002320 kfree(rxr->rx_tpa);
2321 rxr->rx_tpa = NULL;
2322
2323 kfree(rxr->rx_agg_bmap);
2324 rxr->rx_agg_bmap = NULL;
2325
2326 ring = &rxr->rx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002327 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002328
2329 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002330 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002331 }
2332}
2333
2334static int bnxt_alloc_rx_rings(struct bnxt *bp)
2335{
2336 int i, rc, agg_rings = 0, tpa_rings = 0;
2337
Michael Chanb6ab4b02016-01-02 23:44:59 -05002338 if (!bp->rx_ring)
2339 return -ENOMEM;
2340
Michael Chanc0c050c2015-10-22 16:01:17 -04002341 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2342 agg_rings = 1;
2343
2344 if (bp->flags & BNXT_FLAG_TPA)
2345 tpa_rings = 1;
2346
2347 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002348 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002349 struct bnxt_ring_struct *ring;
2350
Michael Chanc0c050c2015-10-22 16:01:17 -04002351 ring = &rxr->rx_ring_struct;
2352
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002353 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2354 if (rc < 0)
2355 return rc;
2356
Michael Chan6fe19882018-10-14 07:02:41 -04002357 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002358 if (rc)
2359 return rc;
2360
Michael Chan2c61d212018-10-14 07:02:50 -04002361 ring->grp_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002362 if (agg_rings) {
2363 u16 mem_size;
2364
2365 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002366 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002367 if (rc)
2368 return rc;
2369
Michael Chan9899bb52018-03-31 13:54:16 -04002370 ring->grp_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002371 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2372 mem_size = rxr->rx_agg_bmap_size / 8;
2373 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2374 if (!rxr->rx_agg_bmap)
2375 return -ENOMEM;
2376
2377 if (tpa_rings) {
2378 rxr->rx_tpa = kcalloc(MAX_TPA,
2379 sizeof(struct bnxt_tpa_info),
2380 GFP_KERNEL);
2381 if (!rxr->rx_tpa)
2382 return -ENOMEM;
2383 }
2384 }
2385 }
2386 return 0;
2387}
2388
2389static void bnxt_free_tx_rings(struct bnxt *bp)
2390{
2391 int i;
2392 struct pci_dev *pdev = bp->pdev;
2393
Michael Chanb6ab4b02016-01-02 23:44:59 -05002394 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002395 return;
2396
2397 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002398 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002399 struct bnxt_ring_struct *ring;
2400
Michael Chanc0c050c2015-10-22 16:01:17 -04002401 if (txr->tx_push) {
2402 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2403 txr->tx_push, txr->tx_push_mapping);
2404 txr->tx_push = NULL;
2405 }
2406
2407 ring = &txr->tx_ring_struct;
2408
Michael Chan6fe19882018-10-14 07:02:41 -04002409 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002410 }
2411}
2412
2413static int bnxt_alloc_tx_rings(struct bnxt *bp)
2414{
2415 int i, j, rc;
2416 struct pci_dev *pdev = bp->pdev;
2417
2418 bp->tx_push_size = 0;
2419 if (bp->tx_push_thresh) {
2420 int push_size;
2421
2422 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2423 bp->tx_push_thresh);
2424
Michael Chan4419dbe2016-02-10 17:33:49 -05002425 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002426 push_size = 0;
2427 bp->tx_push_thresh = 0;
2428 }
2429
2430 bp->tx_push_size = push_size;
2431 }
2432
2433 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002434 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002435 struct bnxt_ring_struct *ring;
Michael Chan2e8ef772018-04-26 17:44:31 -04002436 u8 qidx;
Michael Chanc0c050c2015-10-22 16:01:17 -04002437
Michael Chanc0c050c2015-10-22 16:01:17 -04002438 ring = &txr->tx_ring_struct;
2439
Michael Chan6fe19882018-10-14 07:02:41 -04002440 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002441 if (rc)
2442 return rc;
2443
Michael Chan9899bb52018-03-31 13:54:16 -04002444 ring->grp_idx = txr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04002445 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002446 dma_addr_t mapping;
2447
2448 /* One pre-allocated DMA buffer to backup
2449 * TX push operation
2450 */
2451 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2452 bp->tx_push_size,
2453 &txr->tx_push_mapping,
2454 GFP_KERNEL);
2455
2456 if (!txr->tx_push)
2457 return -ENOMEM;
2458
Michael Chanc0c050c2015-10-22 16:01:17 -04002459 mapping = txr->tx_push_mapping +
2460 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002461 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002462
Michael Chan4419dbe2016-02-10 17:33:49 -05002463 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002464 }
Michael Chan2e8ef772018-04-26 17:44:31 -04002465 qidx = bp->tc_to_qidx[j];
2466 ring->queue_id = bp->q_info[qidx].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002467 if (i < bp->tx_nr_rings_xdp)
2468 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002469 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2470 j++;
2471 }
2472 return 0;
2473}
2474
2475static void bnxt_free_cp_rings(struct bnxt *bp)
2476{
2477 int i;
2478
2479 if (!bp->bnapi)
2480 return;
2481
2482 for (i = 0; i < bp->cp_nr_rings; i++) {
2483 struct bnxt_napi *bnapi = bp->bnapi[i];
2484 struct bnxt_cp_ring_info *cpr;
2485 struct bnxt_ring_struct *ring;
Michael Chan50e3ab72018-10-14 07:02:49 -04002486 int j;
Michael Chanc0c050c2015-10-22 16:01:17 -04002487
2488 if (!bnapi)
2489 continue;
2490
2491 cpr = &bnapi->cp_ring;
2492 ring = &cpr->cp_ring_struct;
2493
Michael Chan6fe19882018-10-14 07:02:41 -04002494 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chan50e3ab72018-10-14 07:02:49 -04002495
2496 for (j = 0; j < 2; j++) {
2497 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2498
2499 if (cpr2) {
2500 ring = &cpr2->cp_ring_struct;
2501 bnxt_free_ring(bp, &ring->ring_mem);
2502 kfree(cpr2);
2503 cpr->cp_ring_arr[j] = NULL;
2504 }
2505 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002506 }
2507}
2508
Michael Chan50e3ab72018-10-14 07:02:49 -04002509static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
2510{
2511 struct bnxt_ring_mem_info *rmem;
2512 struct bnxt_ring_struct *ring;
2513 struct bnxt_cp_ring_info *cpr;
2514 int rc;
2515
2516 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
2517 if (!cpr)
2518 return NULL;
2519
2520 ring = &cpr->cp_ring_struct;
2521 rmem = &ring->ring_mem;
2522 rmem->nr_pages = bp->cp_nr_pages;
2523 rmem->page_size = HW_CMPD_RING_SIZE;
2524 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2525 rmem->dma_arr = cpr->cp_desc_mapping;
2526 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
2527 rc = bnxt_alloc_ring(bp, rmem);
2528 if (rc) {
2529 bnxt_free_ring(bp, rmem);
2530 kfree(cpr);
2531 cpr = NULL;
2532 }
2533 return cpr;
2534}
2535
Michael Chanc0c050c2015-10-22 16:01:17 -04002536static int bnxt_alloc_cp_rings(struct bnxt *bp)
2537{
Michael Chan50e3ab72018-10-14 07:02:49 -04002538 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
Michael Chane5811b82018-03-31 13:54:18 -04002539 int i, rc, ulp_base_vec, ulp_msix;
Michael Chanc0c050c2015-10-22 16:01:17 -04002540
Michael Chane5811b82018-03-31 13:54:18 -04002541 ulp_msix = bnxt_get_ulp_msix_num(bp);
2542 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04002543 for (i = 0; i < bp->cp_nr_rings; i++) {
2544 struct bnxt_napi *bnapi = bp->bnapi[i];
2545 struct bnxt_cp_ring_info *cpr;
2546 struct bnxt_ring_struct *ring;
2547
2548 if (!bnapi)
2549 continue;
2550
2551 cpr = &bnapi->cp_ring;
Michael Chan50e3ab72018-10-14 07:02:49 -04002552 cpr->bnapi = bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04002553 ring = &cpr->cp_ring_struct;
2554
Michael Chan6fe19882018-10-14 07:02:41 -04002555 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002556 if (rc)
2557 return rc;
Michael Chane5811b82018-03-31 13:54:18 -04002558
2559 if (ulp_msix && i >= ulp_base_vec)
2560 ring->map_idx = i + ulp_msix;
2561 else
2562 ring->map_idx = i;
Michael Chan50e3ab72018-10-14 07:02:49 -04002563
2564 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2565 continue;
2566
2567 if (i < bp->rx_nr_rings) {
2568 struct bnxt_cp_ring_info *cpr2 =
2569 bnxt_alloc_cp_sub_ring(bp);
2570
2571 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
2572 if (!cpr2)
2573 return -ENOMEM;
2574 cpr2->bnapi = bnapi;
2575 }
2576 if ((sh && i < bp->tx_nr_rings) ||
2577 (!sh && i >= bp->rx_nr_rings)) {
2578 struct bnxt_cp_ring_info *cpr2 =
2579 bnxt_alloc_cp_sub_ring(bp);
2580
2581 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
2582 if (!cpr2)
2583 return -ENOMEM;
2584 cpr2->bnapi = bnapi;
2585 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002586 }
2587 return 0;
2588}
2589
2590static void bnxt_init_ring_struct(struct bnxt *bp)
2591{
2592 int i;
2593
2594 for (i = 0; i < bp->cp_nr_rings; i++) {
2595 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chan6fe19882018-10-14 07:02:41 -04002596 struct bnxt_ring_mem_info *rmem;
Michael Chanc0c050c2015-10-22 16:01:17 -04002597 struct bnxt_cp_ring_info *cpr;
2598 struct bnxt_rx_ring_info *rxr;
2599 struct bnxt_tx_ring_info *txr;
2600 struct bnxt_ring_struct *ring;
2601
2602 if (!bnapi)
2603 continue;
2604
2605 cpr = &bnapi->cp_ring;
2606 ring = &cpr->cp_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002607 rmem = &ring->ring_mem;
2608 rmem->nr_pages = bp->cp_nr_pages;
2609 rmem->page_size = HW_CMPD_RING_SIZE;
2610 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2611 rmem->dma_arr = cpr->cp_desc_mapping;
2612 rmem->vmem_size = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04002613
Michael Chanb6ab4b02016-01-02 23:44:59 -05002614 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002615 if (!rxr)
2616 goto skip_rx;
2617
Michael Chanc0c050c2015-10-22 16:01:17 -04002618 ring = &rxr->rx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002619 rmem = &ring->ring_mem;
2620 rmem->nr_pages = bp->rx_nr_pages;
2621 rmem->page_size = HW_RXBD_RING_SIZE;
2622 rmem->pg_arr = (void **)rxr->rx_desc_ring;
2623 rmem->dma_arr = rxr->rx_desc_mapping;
2624 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2625 rmem->vmem = (void **)&rxr->rx_buf_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002626
2627 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002628 rmem = &ring->ring_mem;
2629 rmem->nr_pages = bp->rx_agg_nr_pages;
2630 rmem->page_size = HW_RXBD_RING_SIZE;
2631 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
2632 rmem->dma_arr = rxr->rx_agg_desc_mapping;
2633 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2634 rmem->vmem = (void **)&rxr->rx_agg_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002635
Michael Chan3b2b7d92016-01-02 23:45:00 -05002636skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002637 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002638 if (!txr)
2639 continue;
2640
Michael Chanc0c050c2015-10-22 16:01:17 -04002641 ring = &txr->tx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002642 rmem = &ring->ring_mem;
2643 rmem->nr_pages = bp->tx_nr_pages;
2644 rmem->page_size = HW_RXBD_RING_SIZE;
2645 rmem->pg_arr = (void **)txr->tx_desc_ring;
2646 rmem->dma_arr = txr->tx_desc_mapping;
2647 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2648 rmem->vmem = (void **)&txr->tx_buf_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002649 }
2650}
2651
2652static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2653{
2654 int i;
2655 u32 prod;
2656 struct rx_bd **rx_buf_ring;
2657
Michael Chan6fe19882018-10-14 07:02:41 -04002658 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
2659 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002660 int j;
2661 struct rx_bd *rxbd;
2662
2663 rxbd = rx_buf_ring[i];
2664 if (!rxbd)
2665 continue;
2666
2667 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2668 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2669 rxbd->rx_bd_opaque = prod;
2670 }
2671 }
2672}
2673
2674static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2675{
2676 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002677 struct bnxt_rx_ring_info *rxr;
2678 struct bnxt_ring_struct *ring;
2679 u32 prod, type;
2680 int i;
2681
Michael Chanc0c050c2015-10-22 16:01:17 -04002682 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2683 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2684
2685 if (NET_IP_ALIGN == 2)
2686 type |= RX_BD_FLAGS_SOP;
2687
Michael Chanb6ab4b02016-01-02 23:44:59 -05002688 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002689 ring = &rxr->rx_ring_struct;
2690 bnxt_init_rxbd_pages(ring, type);
2691
Michael Chanc6d30e82017-02-06 16:55:42 -05002692 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2693 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2694 if (IS_ERR(rxr->xdp_prog)) {
2695 int rc = PTR_ERR(rxr->xdp_prog);
2696
2697 rxr->xdp_prog = NULL;
2698 return rc;
2699 }
2700 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002701 prod = rxr->rx_prod;
2702 for (i = 0; i < bp->rx_ring_size; i++) {
2703 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2704 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2705 ring_nr, i, bp->rx_ring_size);
2706 break;
2707 }
2708 prod = NEXT_RX(prod);
2709 }
2710 rxr->rx_prod = prod;
2711 ring->fw_ring_id = INVALID_HW_RING_ID;
2712
Michael Chanedd0c2c2015-12-27 18:19:19 -05002713 ring = &rxr->rx_agg_ring_struct;
2714 ring->fw_ring_id = INVALID_HW_RING_ID;
2715
Michael Chanc0c050c2015-10-22 16:01:17 -04002716 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2717 return 0;
2718
Michael Chan2839f282016-04-25 02:30:50 -04002719 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002720 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2721
2722 bnxt_init_rxbd_pages(ring, type);
2723
2724 prod = rxr->rx_agg_prod;
2725 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2726 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2727 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2728 ring_nr, i, bp->rx_ring_size);
2729 break;
2730 }
2731 prod = NEXT_RX_AGG(prod);
2732 }
2733 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002734
2735 if (bp->flags & BNXT_FLAG_TPA) {
2736 if (rxr->rx_tpa) {
2737 u8 *data;
2738 dma_addr_t mapping;
2739
2740 for (i = 0; i < MAX_TPA; i++) {
2741 data = __bnxt_alloc_rx_data(bp, &mapping,
2742 GFP_KERNEL);
2743 if (!data)
2744 return -ENOMEM;
2745
2746 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002747 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002748 rxr->rx_tpa[i].mapping = mapping;
2749 }
2750 } else {
2751 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2752 return -ENOMEM;
2753 }
2754 }
2755
2756 return 0;
2757}
2758
Sankar Patchineelam22479252017-03-28 19:47:29 -04002759static void bnxt_init_cp_rings(struct bnxt *bp)
2760{
Michael Chan3e08b182018-10-14 07:02:52 -04002761 int i, j;
Sankar Patchineelam22479252017-03-28 19:47:29 -04002762
2763 for (i = 0; i < bp->cp_nr_rings; i++) {
2764 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2765 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2766
2767 ring->fw_ring_id = INVALID_HW_RING_ID;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002768 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2769 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
Michael Chan3e08b182018-10-14 07:02:52 -04002770 for (j = 0; j < 2; j++) {
2771 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2772
2773 if (!cpr2)
2774 continue;
2775
2776 ring = &cpr2->cp_ring_struct;
2777 ring->fw_ring_id = INVALID_HW_RING_ID;
2778 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2779 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2780 }
Sankar Patchineelam22479252017-03-28 19:47:29 -04002781 }
2782}
2783
Michael Chanc0c050c2015-10-22 16:01:17 -04002784static int bnxt_init_rx_rings(struct bnxt *bp)
2785{
2786 int i, rc = 0;
2787
Michael Chanc61fb992017-02-06 16:55:36 -05002788 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002789 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2790 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002791 } else {
2792 bp->rx_offset = BNXT_RX_OFFSET;
2793 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2794 }
Michael Chanb3dba772017-02-06 16:55:35 -05002795
Michael Chanc0c050c2015-10-22 16:01:17 -04002796 for (i = 0; i < bp->rx_nr_rings; i++) {
2797 rc = bnxt_init_one_rx_ring(bp, i);
2798 if (rc)
2799 break;
2800 }
2801
2802 return rc;
2803}
2804
2805static int bnxt_init_tx_rings(struct bnxt *bp)
2806{
2807 u16 i;
2808
2809 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2810 MAX_SKB_FRAGS + 1);
2811
2812 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002813 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002814 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2815
2816 ring->fw_ring_id = INVALID_HW_RING_ID;
2817 }
2818
2819 return 0;
2820}
2821
2822static void bnxt_free_ring_grps(struct bnxt *bp)
2823{
2824 kfree(bp->grp_info);
2825 bp->grp_info = NULL;
2826}
2827
2828static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2829{
2830 int i;
2831
2832 if (irq_re_init) {
2833 bp->grp_info = kcalloc(bp->cp_nr_rings,
2834 sizeof(struct bnxt_ring_grp_info),
2835 GFP_KERNEL);
2836 if (!bp->grp_info)
2837 return -ENOMEM;
2838 }
2839 for (i = 0; i < bp->cp_nr_rings; i++) {
2840 if (irq_re_init)
2841 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2842 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2843 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2844 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2845 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2846 }
2847 return 0;
2848}
2849
2850static void bnxt_free_vnics(struct bnxt *bp)
2851{
2852 kfree(bp->vnic_info);
2853 bp->vnic_info = NULL;
2854 bp->nr_vnics = 0;
2855}
2856
2857static int bnxt_alloc_vnics(struct bnxt *bp)
2858{
2859 int num_vnics = 1;
2860
2861#ifdef CONFIG_RFS_ACCEL
2862 if (bp->flags & BNXT_FLAG_RFS)
2863 num_vnics += bp->rx_nr_rings;
2864#endif
2865
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002866 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2867 num_vnics++;
2868
Michael Chanc0c050c2015-10-22 16:01:17 -04002869 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2870 GFP_KERNEL);
2871 if (!bp->vnic_info)
2872 return -ENOMEM;
2873
2874 bp->nr_vnics = num_vnics;
2875 return 0;
2876}
2877
2878static void bnxt_init_vnics(struct bnxt *bp)
2879{
2880 int i;
2881
2882 for (i = 0; i < bp->nr_vnics; i++) {
2883 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2884
2885 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002886 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2887 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002888 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2889
2890 if (bp->vnic_info[i].rss_hash_key) {
2891 if (i == 0)
2892 prandom_bytes(vnic->rss_hash_key,
2893 HW_HASH_KEY_SIZE);
2894 else
2895 memcpy(vnic->rss_hash_key,
2896 bp->vnic_info[0].rss_hash_key,
2897 HW_HASH_KEY_SIZE);
2898 }
2899 }
2900}
2901
2902static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2903{
2904 int pages;
2905
2906 pages = ring_size / desc_per_pg;
2907
2908 if (!pages)
2909 return 1;
2910
2911 pages++;
2912
2913 while (pages & (pages - 1))
2914 pages++;
2915
2916 return pages;
2917}
2918
Michael Chanc6d30e82017-02-06 16:55:42 -05002919void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002920{
2921 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002922 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2923 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002924 if (bp->dev->features & NETIF_F_LRO)
2925 bp->flags |= BNXT_FLAG_LRO;
Michael Chan1054aee2017-12-16 03:09:42 -05002926 else if (bp->dev->features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04002927 bp->flags |= BNXT_FLAG_GRO;
2928}
2929
2930/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2931 * be set on entry.
2932 */
2933void bnxt_set_ring_params(struct bnxt *bp)
2934{
2935 u32 ring_size, rx_size, rx_space;
2936 u32 agg_factor = 0, agg_ring_size = 0;
2937
2938 /* 8 for CRC and VLAN */
2939 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2940
2941 rx_space = rx_size + NET_SKB_PAD +
2942 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2943
2944 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2945 ring_size = bp->rx_ring_size;
2946 bp->rx_agg_ring_size = 0;
2947 bp->rx_agg_nr_pages = 0;
2948
2949 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002950 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002951
2952 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002953 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002954 u32 jumbo_factor;
2955
2956 bp->flags |= BNXT_FLAG_JUMBO;
2957 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2958 if (jumbo_factor > agg_factor)
2959 agg_factor = jumbo_factor;
2960 }
2961 agg_ring_size = ring_size * agg_factor;
2962
2963 if (agg_ring_size) {
2964 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2965 RX_DESC_CNT);
2966 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2967 u32 tmp = agg_ring_size;
2968
2969 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2970 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2971 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2972 tmp, agg_ring_size);
2973 }
2974 bp->rx_agg_ring_size = agg_ring_size;
2975 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2976 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2977 rx_space = rx_size + NET_SKB_PAD +
2978 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2979 }
2980
2981 bp->rx_buf_use_size = rx_size;
2982 bp->rx_buf_size = rx_space;
2983
2984 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2985 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2986
2987 ring_size = bp->tx_ring_size;
2988 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2989 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2990
2991 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2992 bp->cp_ring_size = ring_size;
2993
2994 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2995 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2996 bp->cp_nr_pages = MAX_CP_PAGES;
2997 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2998 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2999 ring_size, bp->cp_ring_size);
3000 }
3001 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3002 bp->cp_ring_mask = bp->cp_bit - 1;
3003}
3004
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01003005/* Changing allocation mode of RX rings.
3006 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3007 */
Michael Chanc61fb992017-02-06 16:55:36 -05003008int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05003009{
Michael Chanc61fb992017-02-06 16:55:36 -05003010 if (page_mode) {
3011 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3012 return -EOPNOTSUPP;
Michael Chan7eb9bb32017-10-26 11:51:25 -04003013 bp->dev->max_mtu =
3014 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
Michael Chanc61fb992017-02-06 16:55:36 -05003015 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3016 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
Michael Chanc61fb992017-02-06 16:55:36 -05003017 bp->rx_dir = DMA_BIDIRECTIONAL;
3018 bp->rx_skb_func = bnxt_rx_page_skb;
Michael Chan1054aee2017-12-16 03:09:42 -05003019 /* Disable LRO or GRO_HW */
3020 netdev_update_features(bp->dev);
Michael Chanc61fb992017-02-06 16:55:36 -05003021 } else {
Michael Chan7eb9bb32017-10-26 11:51:25 -04003022 bp->dev->max_mtu = bp->max_mtu;
Michael Chanc61fb992017-02-06 16:55:36 -05003023 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3024 bp->rx_dir = DMA_FROM_DEVICE;
3025 bp->rx_skb_func = bnxt_rx_skb;
3026 }
Michael Chan6bb19472017-02-06 16:55:32 -05003027 return 0;
3028}
3029
Michael Chanc0c050c2015-10-22 16:01:17 -04003030static void bnxt_free_vnic_attributes(struct bnxt *bp)
3031{
3032 int i;
3033 struct bnxt_vnic_info *vnic;
3034 struct pci_dev *pdev = bp->pdev;
3035
3036 if (!bp->vnic_info)
3037 return;
3038
3039 for (i = 0; i < bp->nr_vnics; i++) {
3040 vnic = &bp->vnic_info[i];
3041
3042 kfree(vnic->fw_grp_ids);
3043 vnic->fw_grp_ids = NULL;
3044
3045 kfree(vnic->uc_list);
3046 vnic->uc_list = NULL;
3047
3048 if (vnic->mc_list) {
3049 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3050 vnic->mc_list, vnic->mc_list_mapping);
3051 vnic->mc_list = NULL;
3052 }
3053
3054 if (vnic->rss_table) {
3055 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3056 vnic->rss_table,
3057 vnic->rss_table_dma_addr);
3058 vnic->rss_table = NULL;
3059 }
3060
3061 vnic->rss_hash_key = NULL;
3062 vnic->flags = 0;
3063 }
3064}
3065
3066static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3067{
3068 int i, rc = 0, size;
3069 struct bnxt_vnic_info *vnic;
3070 struct pci_dev *pdev = bp->pdev;
3071 int max_rings;
3072
3073 for (i = 0; i < bp->nr_vnics; i++) {
3074 vnic = &bp->vnic_info[i];
3075
3076 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3077 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3078
3079 if (mem_size > 0) {
3080 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3081 if (!vnic->uc_list) {
3082 rc = -ENOMEM;
3083 goto out;
3084 }
3085 }
3086 }
3087
3088 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3089 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3090 vnic->mc_list =
3091 dma_alloc_coherent(&pdev->dev,
3092 vnic->mc_list_size,
3093 &vnic->mc_list_mapping,
3094 GFP_KERNEL);
3095 if (!vnic->mc_list) {
3096 rc = -ENOMEM;
3097 goto out;
3098 }
3099 }
3100
3101 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3102 max_rings = bp->rx_nr_rings;
3103 else
3104 max_rings = 1;
3105
3106 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3107 if (!vnic->fw_grp_ids) {
3108 rc = -ENOMEM;
3109 goto out;
3110 }
3111
Michael Chanae10ae72016-12-29 12:13:38 -05003112 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3113 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3114 continue;
3115
Michael Chanc0c050c2015-10-22 16:01:17 -04003116 /* Allocate rss table and hash key */
3117 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3118 &vnic->rss_table_dma_addr,
3119 GFP_KERNEL);
3120 if (!vnic->rss_table) {
3121 rc = -ENOMEM;
3122 goto out;
3123 }
3124
3125 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3126
3127 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3128 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3129 }
3130 return 0;
3131
3132out:
3133 return rc;
3134}
3135
3136static void bnxt_free_hwrm_resources(struct bnxt *bp)
3137{
3138 struct pci_dev *pdev = bp->pdev;
3139
Venkat Duvvurua2bf74f2018-10-05 00:26:02 -04003140 if (bp->hwrm_cmd_resp_addr) {
3141 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3142 bp->hwrm_cmd_resp_dma_addr);
3143 bp->hwrm_cmd_resp_addr = NULL;
3144 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003145}
3146
3147static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3148{
3149 struct pci_dev *pdev = bp->pdev;
3150
3151 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3152 &bp->hwrm_cmd_resp_dma_addr,
3153 GFP_KERNEL);
3154 if (!bp->hwrm_cmd_resp_addr)
3155 return -ENOMEM;
Michael Chanc0c050c2015-10-22 16:01:17 -04003156
3157 return 0;
3158}
3159
Deepak Khungare605db82017-05-29 19:06:04 -04003160static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3161{
3162 if (bp->hwrm_short_cmd_req_addr) {
3163 struct pci_dev *pdev = bp->pdev;
3164
Michael Chan1dfddc42018-10-14 07:02:39 -04003165 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
Deepak Khungare605db82017-05-29 19:06:04 -04003166 bp->hwrm_short_cmd_req_addr,
3167 bp->hwrm_short_cmd_req_dma_addr);
3168 bp->hwrm_short_cmd_req_addr = NULL;
3169 }
3170}
3171
3172static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3173{
3174 struct pci_dev *pdev = bp->pdev;
3175
3176 bp->hwrm_short_cmd_req_addr =
Michael Chan1dfddc42018-10-14 07:02:39 -04003177 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
Deepak Khungare605db82017-05-29 19:06:04 -04003178 &bp->hwrm_short_cmd_req_dma_addr,
3179 GFP_KERNEL);
3180 if (!bp->hwrm_short_cmd_req_addr)
3181 return -ENOMEM;
3182
3183 return 0;
3184}
3185
Michael Chanc0c050c2015-10-22 16:01:17 -04003186static void bnxt_free_stats(struct bnxt *bp)
3187{
3188 u32 size, i;
3189 struct pci_dev *pdev = bp->pdev;
3190
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003191 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3192 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3193
Michael Chan3bdf56c2016-03-07 15:38:45 -05003194 if (bp->hw_rx_port_stats) {
3195 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3196 bp->hw_rx_port_stats,
3197 bp->hw_rx_port_stats_map);
3198 bp->hw_rx_port_stats = NULL;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003199 }
3200
Michael Chan36e53342018-10-14 07:02:38 -04003201 if (bp->hw_tx_port_stats_ext) {
3202 dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
3203 bp->hw_tx_port_stats_ext,
3204 bp->hw_tx_port_stats_ext_map);
3205 bp->hw_tx_port_stats_ext = NULL;
3206 }
3207
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003208 if (bp->hw_rx_port_stats_ext) {
3209 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3210 bp->hw_rx_port_stats_ext,
3211 bp->hw_rx_port_stats_ext_map);
3212 bp->hw_rx_port_stats_ext = NULL;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003213 }
3214
Michael Chanc0c050c2015-10-22 16:01:17 -04003215 if (!bp->bnapi)
3216 return;
3217
3218 size = sizeof(struct ctx_hw_stats);
3219
3220 for (i = 0; i < bp->cp_nr_rings; i++) {
3221 struct bnxt_napi *bnapi = bp->bnapi[i];
3222 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3223
3224 if (cpr->hw_stats) {
3225 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3226 cpr->hw_stats_map);
3227 cpr->hw_stats = NULL;
3228 }
3229 }
3230}
3231
3232static int bnxt_alloc_stats(struct bnxt *bp)
3233{
3234 u32 size, i;
3235 struct pci_dev *pdev = bp->pdev;
3236
3237 size = sizeof(struct ctx_hw_stats);
3238
3239 for (i = 0; i < bp->cp_nr_rings; i++) {
3240 struct bnxt_napi *bnapi = bp->bnapi[i];
3241 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3242
3243 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3244 &cpr->hw_stats_map,
3245 GFP_KERNEL);
3246 if (!cpr->hw_stats)
3247 return -ENOMEM;
3248
3249 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3250 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003251
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003252 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05003253 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3254 sizeof(struct tx_port_stats) + 1024;
3255
3256 bp->hw_rx_port_stats =
3257 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3258 &bp->hw_rx_port_stats_map,
3259 GFP_KERNEL);
3260 if (!bp->hw_rx_port_stats)
3261 return -ENOMEM;
3262
3263 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3264 512;
3265 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3266 sizeof(struct rx_port_stats) + 512;
3267 bp->flags |= BNXT_FLAG_PORT_STATS;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003268
3269 /* Display extended statistics only if FW supports it */
3270 if (bp->hwrm_spec_code < 0x10804 ||
3271 bp->hwrm_spec_code == 0x10900)
3272 return 0;
3273
3274 bp->hw_rx_port_stats_ext =
3275 dma_zalloc_coherent(&pdev->dev,
3276 sizeof(struct rx_port_stats_ext),
3277 &bp->hw_rx_port_stats_ext_map,
3278 GFP_KERNEL);
3279 if (!bp->hw_rx_port_stats_ext)
3280 return 0;
3281
Michael Chan36e53342018-10-14 07:02:38 -04003282 if (bp->hwrm_spec_code >= 0x10902) {
3283 bp->hw_tx_port_stats_ext =
3284 dma_zalloc_coherent(&pdev->dev,
3285 sizeof(struct tx_port_stats_ext),
3286 &bp->hw_tx_port_stats_ext_map,
3287 GFP_KERNEL);
3288 }
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003289 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003290 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003291 return 0;
3292}
3293
3294static void bnxt_clear_ring_indices(struct bnxt *bp)
3295{
3296 int i;
3297
3298 if (!bp->bnapi)
3299 return;
3300
3301 for (i = 0; i < bp->cp_nr_rings; i++) {
3302 struct bnxt_napi *bnapi = bp->bnapi[i];
3303 struct bnxt_cp_ring_info *cpr;
3304 struct bnxt_rx_ring_info *rxr;
3305 struct bnxt_tx_ring_info *txr;
3306
3307 if (!bnapi)
3308 continue;
3309
3310 cpr = &bnapi->cp_ring;
3311 cpr->cp_raw_cons = 0;
3312
Michael Chanb6ab4b02016-01-02 23:44:59 -05003313 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003314 if (txr) {
3315 txr->tx_prod = 0;
3316 txr->tx_cons = 0;
3317 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003318
Michael Chanb6ab4b02016-01-02 23:44:59 -05003319 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003320 if (rxr) {
3321 rxr->rx_prod = 0;
3322 rxr->rx_agg_prod = 0;
3323 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003324 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003325 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003326 }
3327}
3328
3329static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3330{
3331#ifdef CONFIG_RFS_ACCEL
3332 int i;
3333
3334 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3335 * safe to delete the hash table.
3336 */
3337 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3338 struct hlist_head *head;
3339 struct hlist_node *tmp;
3340 struct bnxt_ntuple_filter *fltr;
3341
3342 head = &bp->ntp_fltr_hash_tbl[i];
3343 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3344 hlist_del(&fltr->hash);
3345 kfree(fltr);
3346 }
3347 }
3348 if (irq_reinit) {
3349 kfree(bp->ntp_fltr_bmap);
3350 bp->ntp_fltr_bmap = NULL;
3351 }
3352 bp->ntp_fltr_count = 0;
3353#endif
3354}
3355
3356static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3357{
3358#ifdef CONFIG_RFS_ACCEL
3359 int i, rc = 0;
3360
3361 if (!(bp->flags & BNXT_FLAG_RFS))
3362 return 0;
3363
3364 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3365 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3366
3367 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003368 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3369 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003370 GFP_KERNEL);
3371
3372 if (!bp->ntp_fltr_bmap)
3373 rc = -ENOMEM;
3374
3375 return rc;
3376#else
3377 return 0;
3378#endif
3379}
3380
3381static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3382{
3383 bnxt_free_vnic_attributes(bp);
3384 bnxt_free_tx_rings(bp);
3385 bnxt_free_rx_rings(bp);
3386 bnxt_free_cp_rings(bp);
3387 bnxt_free_ntp_fltrs(bp, irq_re_init);
3388 if (irq_re_init) {
3389 bnxt_free_stats(bp);
3390 bnxt_free_ring_grps(bp);
3391 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003392 kfree(bp->tx_ring_map);
3393 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003394 kfree(bp->tx_ring);
3395 bp->tx_ring = NULL;
3396 kfree(bp->rx_ring);
3397 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003398 kfree(bp->bnapi);
3399 bp->bnapi = NULL;
3400 } else {
3401 bnxt_clear_ring_indices(bp);
3402 }
3403}
3404
3405static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3406{
Michael Chan01657bc2016-01-02 23:45:03 -05003407 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003408 void *bnapi;
3409
3410 if (irq_re_init) {
3411 /* Allocate bnapi mem pointer array and mem block for
3412 * all queues
3413 */
3414 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3415 bp->cp_nr_rings);
3416 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3417 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3418 if (!bnapi)
3419 return -ENOMEM;
3420
3421 bp->bnapi = bnapi;
3422 bnapi += arr_size;
3423 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3424 bp->bnapi[i] = bnapi;
3425 bp->bnapi[i]->index = i;
3426 bp->bnapi[i]->bp = bp;
Michael Chane38287b2018-10-14 07:02:45 -04003427 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3428 struct bnxt_cp_ring_info *cpr =
3429 &bp->bnapi[i]->cp_ring;
3430
3431 cpr->cp_ring_struct.ring_mem.flags =
3432 BNXT_RMEM_RING_PTE_FLAG;
3433 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003434 }
3435
Michael Chanb6ab4b02016-01-02 23:44:59 -05003436 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3437 sizeof(struct bnxt_rx_ring_info),
3438 GFP_KERNEL);
3439 if (!bp->rx_ring)
3440 return -ENOMEM;
3441
3442 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chane38287b2018-10-14 07:02:45 -04003443 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3444
3445 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3446 rxr->rx_ring_struct.ring_mem.flags =
3447 BNXT_RMEM_RING_PTE_FLAG;
3448 rxr->rx_agg_ring_struct.ring_mem.flags =
3449 BNXT_RMEM_RING_PTE_FLAG;
3450 }
3451 rxr->bnapi = bp->bnapi[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05003452 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3453 }
3454
3455 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3456 sizeof(struct bnxt_tx_ring_info),
3457 GFP_KERNEL);
3458 if (!bp->tx_ring)
3459 return -ENOMEM;
3460
Michael Chana960dec2017-02-06 16:55:39 -05003461 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3462 GFP_KERNEL);
3463
3464 if (!bp->tx_ring_map)
3465 return -ENOMEM;
3466
Michael Chan01657bc2016-01-02 23:45:03 -05003467 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3468 j = 0;
3469 else
3470 j = bp->rx_nr_rings;
3471
3472 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
Michael Chane38287b2018-10-14 07:02:45 -04003473 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3474
3475 if (bp->flags & BNXT_FLAG_CHIP_P5)
3476 txr->tx_ring_struct.ring_mem.flags =
3477 BNXT_RMEM_RING_PTE_FLAG;
3478 txr->bnapi = bp->bnapi[j];
3479 bp->bnapi[j]->tx_ring = txr;
Michael Chan5f449242017-02-06 16:55:40 -05003480 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003481 if (i >= bp->tx_nr_rings_xdp) {
Michael Chane38287b2018-10-14 07:02:45 -04003482 txr->txq_index = i - bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003483 bp->bnapi[j]->tx_int = bnxt_tx_int;
3484 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003485 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003486 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3487 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003488 }
3489
Michael Chanc0c050c2015-10-22 16:01:17 -04003490 rc = bnxt_alloc_stats(bp);
3491 if (rc)
3492 goto alloc_mem_err;
3493
3494 rc = bnxt_alloc_ntp_fltrs(bp);
3495 if (rc)
3496 goto alloc_mem_err;
3497
3498 rc = bnxt_alloc_vnics(bp);
3499 if (rc)
3500 goto alloc_mem_err;
3501 }
3502
3503 bnxt_init_ring_struct(bp);
3504
3505 rc = bnxt_alloc_rx_rings(bp);
3506 if (rc)
3507 goto alloc_mem_err;
3508
3509 rc = bnxt_alloc_tx_rings(bp);
3510 if (rc)
3511 goto alloc_mem_err;
3512
3513 rc = bnxt_alloc_cp_rings(bp);
3514 if (rc)
3515 goto alloc_mem_err;
3516
3517 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3518 BNXT_VNIC_UCAST_FLAG;
3519 rc = bnxt_alloc_vnic_attributes(bp);
3520 if (rc)
3521 goto alloc_mem_err;
3522 return 0;
3523
3524alloc_mem_err:
3525 bnxt_free_mem(bp, true);
3526 return rc;
3527}
3528
Michael Chan9d8bc092016-12-29 12:13:33 -05003529static void bnxt_disable_int(struct bnxt *bp)
3530{
3531 int i;
3532
3533 if (!bp->bnapi)
3534 return;
3535
3536 for (i = 0; i < bp->cp_nr_rings; i++) {
3537 struct bnxt_napi *bnapi = bp->bnapi[i];
3538 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003539 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003540
Michael Chandaf1f1e2017-02-20 19:25:17 -05003541 if (ring->fw_ring_id != INVALID_HW_RING_ID)
Michael Chan697197e2018-10-14 07:02:46 -04003542 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003543 }
3544}
3545
Michael Chane5811b82018-03-31 13:54:18 -04003546static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3547{
3548 struct bnxt_napi *bnapi = bp->bnapi[n];
3549 struct bnxt_cp_ring_info *cpr;
3550
3551 cpr = &bnapi->cp_ring;
3552 return cpr->cp_ring_struct.map_idx;
3553}
3554
Michael Chan9d8bc092016-12-29 12:13:33 -05003555static void bnxt_disable_int_sync(struct bnxt *bp)
3556{
3557 int i;
3558
3559 atomic_inc(&bp->intr_sem);
3560
3561 bnxt_disable_int(bp);
Michael Chane5811b82018-03-31 13:54:18 -04003562 for (i = 0; i < bp->cp_nr_rings; i++) {
3563 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3564
3565 synchronize_irq(bp->irq_tbl[map_idx].vector);
3566 }
Michael Chan9d8bc092016-12-29 12:13:33 -05003567}
3568
3569static void bnxt_enable_int(struct bnxt *bp)
3570{
3571 int i;
3572
3573 atomic_set(&bp->intr_sem, 0);
3574 for (i = 0; i < bp->cp_nr_rings; i++) {
3575 struct bnxt_napi *bnapi = bp->bnapi[i];
3576 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3577
Michael Chan697197e2018-10-14 07:02:46 -04003578 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003579 }
3580}
3581
Michael Chanc0c050c2015-10-22 16:01:17 -04003582void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3583 u16 cmpl_ring, u16 target_id)
3584{
Michael Chana8643e12016-02-26 04:00:05 -05003585 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003586
Michael Chana8643e12016-02-26 04:00:05 -05003587 req->req_type = cpu_to_le16(req_type);
3588 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3589 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003590 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3591}
3592
Michael Chanfbfbc482016-02-26 04:00:07 -05003593static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3594 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003595{
Michael Chana11fa2b2016-05-15 03:04:47 -04003596 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003597 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003598 u32 *data = msg;
Michael Chan845adfe2018-03-31 13:54:15 -04003599 __le32 *resp_len;
3600 u8 *valid;
Michael Chanc0c050c2015-10-22 16:01:17 -04003601 u16 cp_ring_id, len = 0;
3602 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003603 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Vasundhara Volamebd58182017-12-01 03:13:05 -05003604 struct hwrm_short_input short_input = {0};
Michael Chanc0c050c2015-10-22 16:01:17 -04003605
Michael Chana8643e12016-02-26 04:00:05 -05003606 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003607 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003608 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003609 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3610
Michael Chan1dfddc42018-10-14 07:02:39 -04003611 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
3612 if (msg_len > bp->hwrm_max_ext_req_len ||
3613 !bp->hwrm_short_cmd_req_addr)
3614 return -EINVAL;
3615 }
3616
3617 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
3618 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
Deepak Khungare605db82017-05-29 19:06:04 -04003619 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
Michael Chan1dfddc42018-10-14 07:02:39 -04003620 u16 max_msg_len;
3621
3622 /* Set boundary for maximum extended request length for short
3623 * cmd format. If passed up from device use the max supported
3624 * internal req length.
3625 */
3626 max_msg_len = bp->hwrm_max_ext_req_len;
Deepak Khungare605db82017-05-29 19:06:04 -04003627
3628 memcpy(short_cmd_req, req, msg_len);
Michael Chan1dfddc42018-10-14 07:02:39 -04003629 if (msg_len < max_msg_len)
3630 memset(short_cmd_req + msg_len, 0,
3631 max_msg_len - msg_len);
Deepak Khungare605db82017-05-29 19:06:04 -04003632
3633 short_input.req_type = req->req_type;
3634 short_input.signature =
3635 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3636 short_input.size = cpu_to_le16(msg_len);
3637 short_input.req_addr =
3638 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3639
3640 data = (u32 *)&short_input;
3641 msg_len = sizeof(short_input);
3642
3643 /* Sync memory write before updating doorbell */
3644 wmb();
3645
3646 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3647 }
3648
Michael Chanc0c050c2015-10-22 16:01:17 -04003649 /* Write request msg to hwrm channel */
3650 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3651
Deepak Khungare605db82017-05-29 19:06:04 -04003652 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003653 writel(0, bp->bar0 + i);
3654
Michael Chanc0c050c2015-10-22 16:01:17 -04003655 /* currently supports only one outstanding message */
3656 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003657 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003658
3659 /* Ring channel doorbell */
3660 writel(1, bp->bar0 + 0x100);
3661
Michael Chanff4fe812016-02-26 04:00:04 -05003662 if (!timeout)
3663 timeout = DFLT_HWRM_CMD_TIMEOUT;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003664 /* convert timeout to usec */
3665 timeout *= 1000;
Michael Chanff4fe812016-02-26 04:00:04 -05003666
Michael Chanc0c050c2015-10-22 16:01:17 -04003667 i = 0;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003668 /* Short timeout for the first few iterations:
3669 * number of loops = number of loops for short timeout +
3670 * number of loops for standard timeout.
3671 */
3672 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3673 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3674 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
Michael Chan845adfe2018-03-31 13:54:15 -04003675 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chanc0c050c2015-10-22 16:01:17 -04003676 if (intr_process) {
3677 /* Wait until hwrm response cmpl interrupt is processed */
3678 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003679 i++ < tmo_count) {
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003680 /* on first few passes, just barely sleep */
3681 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3682 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3683 HWRM_SHORT_MAX_TIMEOUT);
3684 else
3685 usleep_range(HWRM_MIN_TIMEOUT,
3686 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003687 }
3688
3689 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3690 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003691 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003692 return -1;
3693 }
Michael Chan845adfe2018-03-31 13:54:15 -04003694 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3695 HWRM_RESP_LEN_SFT;
3696 valid = bp->hwrm_cmd_resp_addr + len - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003697 } else {
Michael Chancc559c12018-05-08 03:18:38 -04003698 int j;
3699
Michael Chanc0c050c2015-10-22 16:01:17 -04003700 /* Check if response len is updated */
Michael Chana11fa2b2016-05-15 03:04:47 -04003701 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003702 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3703 HWRM_RESP_LEN_SFT;
3704 if (len)
3705 break;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003706 /* on first few passes, just barely sleep */
3707 if (i < DFLT_HWRM_CMD_TIMEOUT)
3708 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3709 HWRM_SHORT_MAX_TIMEOUT);
3710 else
3711 usleep_range(HWRM_MIN_TIMEOUT,
3712 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003713 }
3714
Michael Chana11fa2b2016-05-15 03:04:47 -04003715 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003716 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003717 HWRM_TOTAL_TIMEOUT(i),
3718 le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003719 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003720 return -1;
3721 }
3722
Michael Chan845adfe2018-03-31 13:54:15 -04003723 /* Last byte of resp contains valid bit */
3724 valid = bp->hwrm_cmd_resp_addr + len - 1;
Michael Chancc559c12018-05-08 03:18:38 -04003725 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
Michael Chan845adfe2018-03-31 13:54:15 -04003726 /* make sure we read from updated DMA memory */
3727 dma_rmb();
3728 if (*valid)
Michael Chanc0c050c2015-10-22 16:01:17 -04003729 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003730 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003731 }
3732
Michael Chancc559c12018-05-08 03:18:38 -04003733 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003734 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003735 HWRM_TOTAL_TIMEOUT(i),
3736 le16_to_cpu(req->req_type),
Michael Chana8643e12016-02-26 04:00:05 -05003737 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003738 return -1;
3739 }
3740 }
3741
Michael Chan845adfe2018-03-31 13:54:15 -04003742 /* Zero valid bit for compatibility. Valid bit in an older spec
3743 * may become a new field in a newer spec. We must make sure that
3744 * a new field not implemented by old spec will read zero.
3745 */
3746 *valid = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003747 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003748 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003749 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3750 le16_to_cpu(resp->req_type),
3751 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003752 return rc;
3753}
3754
3755int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3756{
3757 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003758}
3759
Michael Chancc72f3b2017-10-13 21:09:33 -04003760int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3761 int timeout)
3762{
3763 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3764}
3765
Michael Chanc0c050c2015-10-22 16:01:17 -04003766int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3767{
3768 int rc;
3769
3770 mutex_lock(&bp->hwrm_cmd_lock);
3771 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3772 mutex_unlock(&bp->hwrm_cmd_lock);
3773 return rc;
3774}
3775
Michael Chan90e209212016-02-26 04:00:08 -05003776int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3777 int timeout)
3778{
3779 int rc;
3780
3781 mutex_lock(&bp->hwrm_cmd_lock);
3782 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3783 mutex_unlock(&bp->hwrm_cmd_lock);
3784 return rc;
3785}
3786
Michael Chana1653b12016-12-07 00:26:20 -05003787int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3788 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003789{
3790 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003791 DECLARE_BITMAP(async_events_bmap, 256);
3792 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003793 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003794
3795 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3796
3797 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003798 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003799
Michael Chan25be8622016-04-05 14:09:00 -04003800 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3801 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3802 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3803
Michael Chana1653b12016-12-07 00:26:20 -05003804 if (bmap && bmap_size) {
3805 for (i = 0; i < bmap_size; i++) {
3806 if (test_bit(i, bmap))
3807 __set_bit(i, async_events_bmap);
3808 }
3809 }
3810
Michael Chan25be8622016-04-05 14:09:00 -04003811 for (i = 0; i < 8; i++)
3812 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3813
Michael Chana1653b12016-12-07 00:26:20 -05003814 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3815}
3816
3817static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3818{
Michael Chan25e1acd2018-08-05 16:51:55 -04003819 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chana1653b12016-12-07 00:26:20 -05003820 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25e1acd2018-08-05 16:51:55 -04003821 int rc;
Michael Chana1653b12016-12-07 00:26:20 -05003822
3823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3824
3825 req.enables =
3826 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3827 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3828
Michael Chan11f15ed2016-04-05 14:08:55 -04003829 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chand4f52de02018-03-31 13:54:06 -04003830 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
3831 req.ver_maj_8b = DRV_VER_MAJ;
3832 req.ver_min_8b = DRV_VER_MIN;
3833 req.ver_upd_8b = DRV_VER_UPD;
3834 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
3835 req.ver_min = cpu_to_le16(DRV_VER_MIN);
3836 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003837
3838 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003839 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003840 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003841
Michael Chan9b0436c2017-07-11 13:05:36 -04003842 memset(data, 0, sizeof(data));
3843 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3844 u16 cmd = bnxt_vf_req_snif[i];
3845 unsigned int bit, idx;
3846
3847 idx = cmd / 32;
3848 bit = cmd % 32;
3849 data[idx] |= 1 << bit;
3850 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003851
Michael Chande68f5de2015-12-09 19:35:41 -05003852 for (i = 0; i < 8; i++)
3853 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3854
Michael Chanc0c050c2015-10-22 16:01:17 -04003855 req.enables |=
3856 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3857 }
3858
Michael Chan25e1acd2018-08-05 16:51:55 -04003859 mutex_lock(&bp->hwrm_cmd_lock);
3860 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3861 if (rc)
3862 rc = -EIO;
3863 else if (resp->flags &
3864 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
3865 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
3866 mutex_unlock(&bp->hwrm_cmd_lock);
3867 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04003868}
3869
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003870static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3871{
3872 struct hwrm_func_drv_unrgtr_input req = {0};
3873
3874 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3875 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3876}
3877
Michael Chanc0c050c2015-10-22 16:01:17 -04003878static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3879{
3880 u32 rc = 0;
3881 struct hwrm_tunnel_dst_port_free_input req = {0};
3882
3883 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3884 req.tunnel_type = tunnel_type;
3885
3886 switch (tunnel_type) {
3887 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3888 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3889 break;
3890 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3891 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3892 break;
3893 default:
3894 break;
3895 }
3896
3897 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3898 if (rc)
3899 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3900 rc);
3901 return rc;
3902}
3903
3904static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3905 u8 tunnel_type)
3906{
3907 u32 rc = 0;
3908 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3909 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3910
3911 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3912
3913 req.tunnel_type = tunnel_type;
3914 req.tunnel_dst_port_val = port;
3915
3916 mutex_lock(&bp->hwrm_cmd_lock);
3917 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3918 if (rc) {
3919 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3920 rc);
3921 goto err_out;
3922 }
3923
Christophe Jaillet57aac712016-11-22 06:14:40 +01003924 switch (tunnel_type) {
3925 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003926 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003927 break;
3928 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003929 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003930 break;
3931 default:
3932 break;
3933 }
3934
Michael Chanc0c050c2015-10-22 16:01:17 -04003935err_out:
3936 mutex_unlock(&bp->hwrm_cmd_lock);
3937 return rc;
3938}
3939
3940static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3941{
3942 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3943 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3944
3945 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003946 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003947
3948 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3949 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3950 req.mask = cpu_to_le32(vnic->rx_mask);
3951 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3952}
3953
3954#ifdef CONFIG_RFS_ACCEL
3955static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3956 struct bnxt_ntuple_filter *fltr)
3957{
3958 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3959
3960 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3961 req.ntuple_filter_id = fltr->filter_id;
3962 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3963}
3964
3965#define BNXT_NTP_FLTR_FLAGS \
3966 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3967 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3968 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3969 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3970 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3971 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3972 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3973 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3974 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3975 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3976 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3977 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3978 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003979 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003980
Michael Chan61aad722017-02-12 19:18:14 -05003981#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3982 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3983
Michael Chanc0c050c2015-10-22 16:01:17 -04003984static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3985 struct bnxt_ntuple_filter *fltr)
3986{
3987 int rc = 0;
3988 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3989 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3990 bp->hwrm_cmd_resp_addr;
3991 struct flow_keys *keys = &fltr->fkeys;
3992 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3993
3994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003995 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003996
3997 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3998
3999 req.ethertype = htons(ETH_P_IP);
4000 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05004001 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04004002 req.ip_protocol = keys->basic.ip_proto;
4003
Michael Chandda0e742016-12-29 12:13:40 -05004004 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4005 int i;
4006
4007 req.ethertype = htons(ETH_P_IPV6);
4008 req.ip_addr_type =
4009 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4010 *(struct in6_addr *)&req.src_ipaddr[0] =
4011 keys->addrs.v6addrs.src;
4012 *(struct in6_addr *)&req.dst_ipaddr[0] =
4013 keys->addrs.v6addrs.dst;
4014 for (i = 0; i < 4; i++) {
4015 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4016 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4017 }
4018 } else {
4019 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4020 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4021 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4022 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4023 }
Michael Chan61aad722017-02-12 19:18:14 -05004024 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4025 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4026 req.tunnel_type =
4027 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4028 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004029
4030 req.src_port = keys->ports.src;
4031 req.src_port_mask = cpu_to_be16(0xffff);
4032 req.dst_port = keys->ports.dst;
4033 req.dst_port_mask = cpu_to_be16(0xffff);
4034
Michael Chanc1935542015-12-27 18:19:28 -05004035 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004036 mutex_lock(&bp->hwrm_cmd_lock);
4037 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4038 if (!rc)
4039 fltr->filter_id = resp->ntuple_filter_id;
4040 mutex_unlock(&bp->hwrm_cmd_lock);
4041 return rc;
4042}
4043#endif
4044
4045static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4046 u8 *mac_addr)
4047{
4048 u32 rc = 0;
4049 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4050 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4051
4052 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004053 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4054 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4055 req.flags |=
4056 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05004057 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004058 req.enables =
4059 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05004060 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04004061 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4062 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4063 req.l2_addr_mask[0] = 0xff;
4064 req.l2_addr_mask[1] = 0xff;
4065 req.l2_addr_mask[2] = 0xff;
4066 req.l2_addr_mask[3] = 0xff;
4067 req.l2_addr_mask[4] = 0xff;
4068 req.l2_addr_mask[5] = 0xff;
4069
4070 mutex_lock(&bp->hwrm_cmd_lock);
4071 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4072 if (!rc)
4073 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4074 resp->l2_filter_id;
4075 mutex_unlock(&bp->hwrm_cmd_lock);
4076 return rc;
4077}
4078
4079static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4080{
4081 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4082 int rc = 0;
4083
4084 /* Any associated ntuple filters will also be cleared by firmware. */
4085 mutex_lock(&bp->hwrm_cmd_lock);
4086 for (i = 0; i < num_of_vnics; i++) {
4087 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4088
4089 for (j = 0; j < vnic->uc_filter_count; j++) {
4090 struct hwrm_cfa_l2_filter_free_input req = {0};
4091
4092 bnxt_hwrm_cmd_hdr_init(bp, &req,
4093 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4094
4095 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4096
4097 rc = _hwrm_send_message(bp, &req, sizeof(req),
4098 HWRM_CMD_TIMEOUT);
4099 }
4100 vnic->uc_filter_count = 0;
4101 }
4102 mutex_unlock(&bp->hwrm_cmd_lock);
4103
4104 return rc;
4105}
4106
4107static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4108{
4109 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4110 struct hwrm_vnic_tpa_cfg_input req = {0};
4111
Michael Chan3c4fe802018-03-09 23:46:10 -05004112 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4113 return 0;
4114
Michael Chanc0c050c2015-10-22 16:01:17 -04004115 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4116
4117 if (tpa_flags) {
4118 u16 mss = bp->dev->mtu - 40;
4119 u32 nsegs, n, segs = 0, flags;
4120
4121 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4122 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4123 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4124 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4125 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4126 if (tpa_flags & BNXT_FLAG_GRO)
4127 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4128
4129 req.flags = cpu_to_le32(flags);
4130
4131 req.enables =
4132 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05004133 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4134 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004135
4136 /* Number of segs are log2 units, and first packet is not
4137 * included as part of this units.
4138 */
Michael Chan2839f282016-04-25 02:30:50 -04004139 if (mss <= BNXT_RX_PAGE_SIZE) {
4140 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04004141 nsegs = (MAX_SKB_FRAGS - 1) * n;
4142 } else {
Michael Chan2839f282016-04-25 02:30:50 -04004143 n = mss / BNXT_RX_PAGE_SIZE;
4144 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04004145 n++;
4146 nsegs = (MAX_SKB_FRAGS - n) / n;
4147 }
4148
4149 segs = ilog2(nsegs);
4150 req.max_agg_segs = cpu_to_le16(segs);
4151 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05004152
4153 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04004154 }
4155 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4156
4157 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4158}
4159
Michael Chan2c61d212018-10-14 07:02:50 -04004160static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4161{
4162 struct bnxt_ring_grp_info *grp_info;
4163
4164 grp_info = &bp->grp_info[ring->grp_idx];
4165 return grp_info->cp_fw_ring_id;
4166}
4167
4168static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4169{
4170 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4171 struct bnxt_napi *bnapi = rxr->bnapi;
4172 struct bnxt_cp_ring_info *cpr;
4173
4174 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4175 return cpr->cp_ring_struct.fw_ring_id;
4176 } else {
4177 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4178 }
4179}
4180
4181static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4182{
4183 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4184 struct bnxt_napi *bnapi = txr->bnapi;
4185 struct bnxt_cp_ring_info *cpr;
4186
4187 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4188 return cpr->cp_ring_struct.fw_ring_id;
4189 } else {
4190 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4191 }
4192}
4193
Michael Chanc0c050c2015-10-22 16:01:17 -04004194static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
4195{
4196 u32 i, j, max_rings;
4197 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4198 struct hwrm_vnic_rss_cfg_input req = {0};
4199
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004200 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04004201 return 0;
4202
4203 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4204 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05004205 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Michael Chan50f011b2018-08-05 16:51:51 -04004206 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004207 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
4208 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4209 max_rings = bp->rx_nr_rings - 1;
4210 else
4211 max_rings = bp->rx_nr_rings;
4212 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04004213 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004214 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004215
4216 /* Fill the RSS indirection table with ring group ids */
4217 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4218 if (j == max_rings)
4219 j = 0;
4220 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4221 }
4222
4223 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4224 req.hash_key_tbl_addr =
4225 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4226 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004227 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004228 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4229}
4230
4231static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4232{
4233 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4234 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4235
4236 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4237 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4238 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4239 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4240 req.enables =
4241 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4242 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4243 /* thresholds not implemented in firmware yet */
4244 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4245 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4246 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4247 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4248}
4249
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004250static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4251 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004252{
4253 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4254
4255 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4256 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004257 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004258
4259 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004260 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004261}
4262
4263static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4264{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004265 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04004266
4267 for (i = 0; i < bp->nr_vnics; i++) {
4268 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4269
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004270 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4271 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4272 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4273 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004274 }
4275 bp->rsscos_nr_ctxs = 0;
4276}
4277
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004278static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004279{
4280 int rc;
4281 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4282 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4283 bp->hwrm_cmd_resp_addr;
4284
4285 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4286 -1);
4287
4288 mutex_lock(&bp->hwrm_cmd_lock);
4289 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4290 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004291 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04004292 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4293 mutex_unlock(&bp->hwrm_cmd_lock);
4294
4295 return rc;
4296}
4297
Michael Chanabe93ad2018-03-31 13:54:08 -04004298static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4299{
4300 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4301 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4302 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4303}
4304
Michael Chana588e452016-12-07 00:26:21 -05004305int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04004306{
Michael Chanb81a90d2016-01-02 23:45:01 -05004307 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004308 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4309 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04004310 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004311
4312 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004313
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004314 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4315 /* Only RSS support for now TBD: COS & LB */
4316 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4317 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4318 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4319 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05004320 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4321 req.rss_rule =
4322 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4323 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4324 VNIC_CFG_REQ_ENABLES_MRU);
4325 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004326 } else {
4327 req.rss_rule = cpu_to_le16(0xffff);
4328 }
4329
4330 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4331 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004332 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4333 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4334 } else {
4335 req.cos_rule = cpu_to_le16(0xffff);
4336 }
4337
Michael Chanc0c050c2015-10-22 16:01:17 -04004338 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004339 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004340 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004341 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004342 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4343 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004344
Michael Chanb81a90d2016-01-02 23:45:01 -05004345 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004346 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4347 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4348
4349 req.lb_rule = cpu_to_le16(0xffff);
4350 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4351 VLAN_HLEN);
4352
Michael Chancf6645f2016-06-13 02:25:28 -04004353#ifdef CONFIG_BNXT_SRIOV
4354 if (BNXT_VF(bp))
4355 def_vlan = bp->vf.vlan;
4356#endif
4357 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04004358 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05004359 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
Michael Chanabe93ad2018-03-31 13:54:08 -04004360 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
Michael Chanc0c050c2015-10-22 16:01:17 -04004361
4362 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4363}
4364
4365static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4366{
4367 u32 rc = 0;
4368
4369 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4370 struct hwrm_vnic_free_input req = {0};
4371
4372 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4373 req.vnic_id =
4374 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4375
4376 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4377 if (rc)
4378 return rc;
4379 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4380 }
4381 return rc;
4382}
4383
4384static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4385{
4386 u16 i;
4387
4388 for (i = 0; i < bp->nr_vnics; i++)
4389 bnxt_hwrm_vnic_free_one(bp, i);
4390}
4391
Michael Chanb81a90d2016-01-02 23:45:01 -05004392static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4393 unsigned int start_rx_ring_idx,
4394 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004395{
Michael Chanb81a90d2016-01-02 23:45:01 -05004396 int rc = 0;
4397 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004398 struct hwrm_vnic_alloc_input req = {0};
4399 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4400
4401 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004402 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4403 grp_idx = bp->rx_ring[i].bnapi->index;
4404 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004405 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004406 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004407 break;
4408 }
4409 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05004410 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004411 }
4412
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004413 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4414 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004415 if (vnic_id == 0)
4416 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4417
4418 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4419
4420 mutex_lock(&bp->hwrm_cmd_lock);
4421 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4422 if (!rc)
4423 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4424 mutex_unlock(&bp->hwrm_cmd_lock);
4425 return rc;
4426}
4427
Michael Chan8fdefd62016-12-29 12:13:36 -05004428static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4429{
4430 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4431 struct hwrm_vnic_qcaps_input req = {0};
4432 int rc;
4433
4434 if (bp->hwrm_spec_code < 0x10600)
4435 return 0;
4436
4437 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4438 mutex_lock(&bp->hwrm_cmd_lock);
4439 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4440 if (!rc) {
Michael Chanabe93ad2018-03-31 13:54:08 -04004441 u32 flags = le32_to_cpu(resp->flags);
4442
Michael Chan41e8d792018-10-14 07:02:48 -04004443 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
4444 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
Michael Chan8fdefd62016-12-29 12:13:36 -05004445 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
Michael Chanabe93ad2018-03-31 13:54:08 -04004446 if (flags &
4447 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4448 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
Michael Chan8fdefd62016-12-29 12:13:36 -05004449 }
4450 mutex_unlock(&bp->hwrm_cmd_lock);
4451 return rc;
4452}
4453
Michael Chanc0c050c2015-10-22 16:01:17 -04004454static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4455{
4456 u16 i;
4457 u32 rc = 0;
4458
4459 mutex_lock(&bp->hwrm_cmd_lock);
4460 for (i = 0; i < bp->rx_nr_rings; i++) {
4461 struct hwrm_ring_grp_alloc_input req = {0};
4462 struct hwrm_ring_grp_alloc_output *resp =
4463 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004464 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004465
4466 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4467
Michael Chanb81a90d2016-01-02 23:45:01 -05004468 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4469 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4470 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4471 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004472
4473 rc = _hwrm_send_message(bp, &req, sizeof(req),
4474 HWRM_CMD_TIMEOUT);
4475 if (rc)
4476 break;
4477
Michael Chanb81a90d2016-01-02 23:45:01 -05004478 bp->grp_info[grp_idx].fw_grp_id =
4479 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004480 }
4481 mutex_unlock(&bp->hwrm_cmd_lock);
4482 return rc;
4483}
4484
4485static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4486{
4487 u16 i;
4488 u32 rc = 0;
4489 struct hwrm_ring_grp_free_input req = {0};
4490
4491 if (!bp->grp_info)
4492 return 0;
4493
4494 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4495
4496 mutex_lock(&bp->hwrm_cmd_lock);
4497 for (i = 0; i < bp->cp_nr_rings; i++) {
4498 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4499 continue;
4500 req.ring_group_id =
4501 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4502
4503 rc = _hwrm_send_message(bp, &req, sizeof(req),
4504 HWRM_CMD_TIMEOUT);
4505 if (rc)
4506 break;
4507 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4508 }
4509 mutex_unlock(&bp->hwrm_cmd_lock);
4510 return rc;
4511}
4512
4513static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4514 struct bnxt_ring_struct *ring,
Michael Chan9899bb52018-03-31 13:54:16 -04004515 u32 ring_type, u32 map_index)
Michael Chanc0c050c2015-10-22 16:01:17 -04004516{
4517 int rc = 0, err = 0;
4518 struct hwrm_ring_alloc_input req = {0};
4519 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6fe19882018-10-14 07:02:41 -04004520 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
Michael Chan9899bb52018-03-31 13:54:16 -04004521 struct bnxt_ring_grp_info *grp_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04004522 u16 ring_id;
4523
4524 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4525
4526 req.enables = 0;
Michael Chan6fe19882018-10-14 07:02:41 -04004527 if (rmem->nr_pages > 1) {
4528 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
Michael Chanc0c050c2015-10-22 16:01:17 -04004529 /* Page size is in log2 units */
4530 req.page_size = BNXT_PAGE_SHIFT;
4531 req.page_tbl_depth = 1;
4532 } else {
Michael Chan6fe19882018-10-14 07:02:41 -04004533 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004534 }
4535 req.fbo = 0;
4536 /* Association of ring index with doorbell index and MSIX number */
4537 req.logical_id = cpu_to_le16(map_index);
4538
4539 switch (ring_type) {
Michael Chan2c61d212018-10-14 07:02:50 -04004540 case HWRM_RING_ALLOC_TX: {
4541 struct bnxt_tx_ring_info *txr;
4542
4543 txr = container_of(ring, struct bnxt_tx_ring_info,
4544 tx_ring_struct);
Michael Chanc0c050c2015-10-22 16:01:17 -04004545 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4546 /* Association of transmit ring with completion ring */
Michael Chan9899bb52018-03-31 13:54:16 -04004547 grp_info = &bp->grp_info[ring->grp_idx];
Michael Chan2c61d212018-10-14 07:02:50 -04004548 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
Michael Chanc0c050c2015-10-22 16:01:17 -04004549 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
Michael Chan9899bb52018-03-31 13:54:16 -04004550 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004551 req.queue_id = cpu_to_le16(ring->queue_id);
4552 break;
Michael Chan2c61d212018-10-14 07:02:50 -04004553 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004554 case HWRM_RING_ALLOC_RX:
4555 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4556 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
Michael Chan23aefdd2018-10-14 07:02:51 -04004557 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4558 u16 flags = 0;
4559
4560 /* Association of rx ring with stats context */
4561 grp_info = &bp->grp_info[ring->grp_idx];
4562 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
4563 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4564 req.enables |= cpu_to_le32(
4565 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4566 if (NET_IP_ALIGN == 2)
4567 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
4568 req.flags = cpu_to_le16(flags);
4569 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004570 break;
4571 case HWRM_RING_ALLOC_AGG:
Michael Chan23aefdd2018-10-14 07:02:51 -04004572 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4573 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
4574 /* Association of agg ring with rx ring */
4575 grp_info = &bp->grp_info[ring->grp_idx];
4576 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
4577 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
4578 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4579 req.enables |= cpu_to_le32(
4580 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
4581 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4582 } else {
4583 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4584 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004585 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4586 break;
4587 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004588 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004589 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
Michael Chan23aefdd2018-10-14 07:02:51 -04004590 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4591 /* Association of cp ring with nq */
4592 grp_info = &bp->grp_info[map_index];
4593 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
4594 req.cq_handle = cpu_to_le64(ring->handle);
4595 req.enables |= cpu_to_le32(
4596 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
4597 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
4598 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4599 }
4600 break;
4601 case HWRM_RING_ALLOC_NQ:
4602 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
4603 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004604 if (bp->flags & BNXT_FLAG_USING_MSIX)
4605 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4606 break;
4607 default:
4608 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4609 ring_type);
4610 return -1;
4611 }
4612
4613 mutex_lock(&bp->hwrm_cmd_lock);
4614 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4615 err = le16_to_cpu(resp->error_code);
4616 ring_id = le16_to_cpu(resp->ring_id);
4617 mutex_unlock(&bp->hwrm_cmd_lock);
4618
4619 if (rc || err) {
Michael Chan2727c882018-04-26 17:44:35 -04004620 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4621 ring_type, rc, err);
4622 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04004623 }
4624 ring->fw_ring_id = ring_id;
4625 return rc;
4626}
4627
Michael Chan486b5c22016-12-29 12:13:42 -05004628static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4629{
4630 int rc;
4631
4632 if (BNXT_PF(bp)) {
4633 struct hwrm_func_cfg_input req = {0};
4634
4635 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4636 req.fid = cpu_to_le16(0xffff);
4637 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4638 req.async_event_cr = cpu_to_le16(idx);
4639 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4640 } else {
4641 struct hwrm_func_vf_cfg_input req = {0};
4642
4643 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4644 req.enables =
4645 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4646 req.async_event_cr = cpu_to_le16(idx);
4647 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4648 }
4649 return rc;
4650}
4651
Michael Chan697197e2018-10-14 07:02:46 -04004652static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
4653 u32 map_idx, u32 xid)
4654{
4655 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4656 if (BNXT_PF(bp))
4657 db->doorbell = bp->bar1 + 0x10000;
4658 else
4659 db->doorbell = bp->bar1 + 0x4000;
4660 switch (ring_type) {
4661 case HWRM_RING_ALLOC_TX:
4662 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
4663 break;
4664 case HWRM_RING_ALLOC_RX:
4665 case HWRM_RING_ALLOC_AGG:
4666 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
4667 break;
4668 case HWRM_RING_ALLOC_CMPL:
4669 db->db_key64 = DBR_PATH_L2;
4670 break;
4671 case HWRM_RING_ALLOC_NQ:
4672 db->db_key64 = DBR_PATH_L2;
4673 break;
4674 }
4675 db->db_key64 |= (u64)xid << DBR_XID_SFT;
4676 } else {
4677 db->doorbell = bp->bar1 + map_idx * 0x80;
4678 switch (ring_type) {
4679 case HWRM_RING_ALLOC_TX:
4680 db->db_key32 = DB_KEY_TX;
4681 break;
4682 case HWRM_RING_ALLOC_RX:
4683 case HWRM_RING_ALLOC_AGG:
4684 db->db_key32 = DB_KEY_RX;
4685 break;
4686 case HWRM_RING_ALLOC_CMPL:
4687 db->db_key32 = DB_KEY_CP;
4688 break;
4689 }
4690 }
4691}
4692
Michael Chanc0c050c2015-10-22 16:01:17 -04004693static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4694{
4695 int i, rc = 0;
Michael Chan697197e2018-10-14 07:02:46 -04004696 u32 type;
Michael Chanc0c050c2015-10-22 16:01:17 -04004697
Michael Chan23aefdd2018-10-14 07:02:51 -04004698 if (bp->flags & BNXT_FLAG_CHIP_P5)
4699 type = HWRM_RING_ALLOC_NQ;
4700 else
4701 type = HWRM_RING_ALLOC_CMPL;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004702 for (i = 0; i < bp->cp_nr_rings; i++) {
4703 struct bnxt_napi *bnapi = bp->bnapi[i];
4704 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4705 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004706 u32 map_idx = ring->map_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004707
Michael Chan697197e2018-10-14 07:02:46 -04004708 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004709 if (rc)
4710 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004711 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
4712 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004713 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004714
4715 if (!i) {
4716 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4717 if (rc)
4718 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4719 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004720 }
4721
Michael Chan697197e2018-10-14 07:02:46 -04004722 type = HWRM_RING_ALLOC_TX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004723 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004724 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chan3e08b182018-10-14 07:02:52 -04004725 struct bnxt_ring_struct *ring;
4726 u32 map_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004727
Michael Chan3e08b182018-10-14 07:02:52 -04004728 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4729 struct bnxt_napi *bnapi = txr->bnapi;
4730 struct bnxt_cp_ring_info *cpr, *cpr2;
4731 u32 type2 = HWRM_RING_ALLOC_CMPL;
4732
4733 cpr = &bnapi->cp_ring;
4734 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
4735 ring = &cpr2->cp_ring_struct;
4736 ring->handle = BNXT_TX_HDL;
4737 map_idx = bnapi->index;
4738 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
4739 if (rc)
4740 goto err_out;
4741 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
4742 ring->fw_ring_id);
4743 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
4744 }
4745 ring = &txr->tx_ring_struct;
4746 map_idx = i;
Michael Chan697197e2018-10-14 07:02:46 -04004747 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004748 if (rc)
4749 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004750 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004751 }
4752
Michael Chan697197e2018-10-14 07:02:46 -04004753 type = HWRM_RING_ALLOC_RX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004754 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004755 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004756 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chan3e08b182018-10-14 07:02:52 -04004757 struct bnxt_napi *bnapi = rxr->bnapi;
4758 u32 map_idx = bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004759
Michael Chan697197e2018-10-14 07:02:46 -04004760 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004761 if (rc)
4762 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004763 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
4764 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
Michael Chanb81a90d2016-01-02 23:45:01 -05004765 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chan3e08b182018-10-14 07:02:52 -04004766 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4767 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4768 u32 type2 = HWRM_RING_ALLOC_CMPL;
4769 struct bnxt_cp_ring_info *cpr2;
4770
4771 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
4772 ring = &cpr2->cp_ring_struct;
4773 ring->handle = BNXT_RX_HDL;
4774 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
4775 if (rc)
4776 goto err_out;
4777 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
4778 ring->fw_ring_id);
4779 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
4780 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004781 }
4782
4783 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
Michael Chan697197e2018-10-14 07:02:46 -04004784 type = HWRM_RING_ALLOC_AGG;
Michael Chanc0c050c2015-10-22 16:01:17 -04004785 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004786 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004787 struct bnxt_ring_struct *ring =
4788 &rxr->rx_agg_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004789 u32 grp_idx = ring->grp_idx;
Michael Chanb81a90d2016-01-02 23:45:01 -05004790 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004791
Michael Chan697197e2018-10-14 07:02:46 -04004792 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004793 if (rc)
4794 goto err_out;
4795
Michael Chan697197e2018-10-14 07:02:46 -04004796 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
4797 ring->fw_ring_id);
4798 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
Michael Chanb81a90d2016-01-02 23:45:01 -05004799 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004800 }
4801 }
4802err_out:
4803 return rc;
4804}
4805
4806static int hwrm_ring_free_send_msg(struct bnxt *bp,
4807 struct bnxt_ring_struct *ring,
4808 u32 ring_type, int cmpl_ring_id)
4809{
4810 int rc;
4811 struct hwrm_ring_free_input req = {0};
4812 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4813 u16 error_code;
4814
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004815 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004816 req.ring_type = ring_type;
4817 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4818
4819 mutex_lock(&bp->hwrm_cmd_lock);
4820 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4821 error_code = le16_to_cpu(resp->error_code);
4822 mutex_unlock(&bp->hwrm_cmd_lock);
4823
4824 if (rc || error_code) {
Michael Chan2727c882018-04-26 17:44:35 -04004825 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
4826 ring_type, rc, error_code);
4827 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04004828 }
4829 return 0;
4830}
4831
Michael Chanedd0c2c2015-12-27 18:19:19 -05004832static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004833{
Michael Chan23aefdd2018-10-14 07:02:51 -04004834 u32 type;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004835 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004836
4837 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004838 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004839
Michael Chanedd0c2c2015-12-27 18:19:19 -05004840 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004841 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004842 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chan2c61d212018-10-14 07:02:50 -04004843 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004844
Michael Chan2c61d212018-10-14 07:02:50 -04004845 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004846 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4847 hwrm_ring_free_send_msg(bp, ring,
4848 RING_FREE_REQ_RING_TYPE_TX,
4849 close_path ? cmpl_ring_id :
4850 INVALID_HW_RING_ID);
4851 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004852 }
4853 }
4854
Michael Chanedd0c2c2015-12-27 18:19:19 -05004855 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004856 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004857 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004858 u32 grp_idx = rxr->bnapi->index;
Michael Chan2c61d212018-10-14 07:02:50 -04004859 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004860
Michael Chan2c61d212018-10-14 07:02:50 -04004861 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004862 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4863 hwrm_ring_free_send_msg(bp, ring,
4864 RING_FREE_REQ_RING_TYPE_RX,
4865 close_path ? cmpl_ring_id :
4866 INVALID_HW_RING_ID);
4867 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004868 bp->grp_info[grp_idx].rx_fw_ring_id =
4869 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004870 }
4871 }
4872
Michael Chan23aefdd2018-10-14 07:02:51 -04004873 if (bp->flags & BNXT_FLAG_CHIP_P5)
4874 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
4875 else
4876 type = RING_FREE_REQ_RING_TYPE_RX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004877 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004878 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004879 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004880 u32 grp_idx = rxr->bnapi->index;
Michael Chan2c61d212018-10-14 07:02:50 -04004881 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004882
Michael Chan2c61d212018-10-14 07:02:50 -04004883 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004884 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
Michael Chan23aefdd2018-10-14 07:02:51 -04004885 hwrm_ring_free_send_msg(bp, ring, type,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004886 close_path ? cmpl_ring_id :
4887 INVALID_HW_RING_ID);
4888 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004889 bp->grp_info[grp_idx].agg_fw_ring_id =
4890 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004891 }
4892 }
4893
Michael Chan9d8bc092016-12-29 12:13:33 -05004894 /* The completion rings are about to be freed. After that the
4895 * IRQ doorbell will not work anymore. So we need to disable
4896 * IRQ here.
4897 */
4898 bnxt_disable_int_sync(bp);
4899
Michael Chan23aefdd2018-10-14 07:02:51 -04004900 if (bp->flags & BNXT_FLAG_CHIP_P5)
4901 type = RING_FREE_REQ_RING_TYPE_NQ;
4902 else
4903 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004904 for (i = 0; i < bp->cp_nr_rings; i++) {
4905 struct bnxt_napi *bnapi = bp->bnapi[i];
4906 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chan3e08b182018-10-14 07:02:52 -04004907 struct bnxt_ring_struct *ring;
4908 int j;
Michael Chanc0c050c2015-10-22 16:01:17 -04004909
Michael Chan3e08b182018-10-14 07:02:52 -04004910 for (j = 0; j < 2; j++) {
4911 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
4912
4913 if (cpr2) {
4914 ring = &cpr2->cp_ring_struct;
4915 if (ring->fw_ring_id == INVALID_HW_RING_ID)
4916 continue;
4917 hwrm_ring_free_send_msg(bp, ring,
4918 RING_FREE_REQ_RING_TYPE_L2_CMPL,
4919 INVALID_HW_RING_ID);
4920 ring->fw_ring_id = INVALID_HW_RING_ID;
4921 }
4922 }
4923 ring = &cpr->cp_ring_struct;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004924 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
Michael Chan23aefdd2018-10-14 07:02:51 -04004925 hwrm_ring_free_send_msg(bp, ring, type,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004926 INVALID_HW_RING_ID);
4927 ring->fw_ring_id = INVALID_HW_RING_ID;
4928 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004929 }
4930 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004931}
4932
Michael Chan41e8d792018-10-14 07:02:48 -04004933static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4934 bool shared);
4935
Michael Chan674f50a2018-01-17 03:21:09 -05004936static int bnxt_hwrm_get_rings(struct bnxt *bp)
4937{
4938 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4939 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4940 struct hwrm_func_qcfg_input req = {0};
4941 int rc;
4942
4943 if (bp->hwrm_spec_code < 0x10601)
4944 return 0;
4945
4946 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4947 req.fid = cpu_to_le16(0xffff);
4948 mutex_lock(&bp->hwrm_cmd_lock);
4949 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4950 if (rc) {
4951 mutex_unlock(&bp->hwrm_cmd_lock);
4952 return -EIO;
4953 }
4954
4955 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
Michael Chanf1ca94d2018-08-05 16:51:53 -04004956 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05004957 u16 cp, stats;
4958
4959 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
4960 hw_resc->resv_hw_ring_grps =
4961 le32_to_cpu(resp->alloc_hw_ring_grps);
4962 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
4963 cp = le16_to_cpu(resp->alloc_cmpl_rings);
4964 stats = le16_to_cpu(resp->alloc_stat_ctx);
4965 cp = min_t(u16, cp, stats);
Michael Chan41e8d792018-10-14 07:02:48 -04004966 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4967 int rx = hw_resc->resv_rx_rings;
4968 int tx = hw_resc->resv_tx_rings;
4969
4970 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4971 rx >>= 1;
4972 if (cp < (rx + tx)) {
4973 bnxt_trim_rings(bp, &rx, &tx, cp, false);
4974 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4975 rx <<= 1;
4976 hw_resc->resv_rx_rings = rx;
4977 hw_resc->resv_tx_rings = tx;
4978 }
4979 cp = le16_to_cpu(resp->alloc_msix);
4980 hw_resc->resv_hw_ring_grps = rx;
4981 }
Michael Chan674f50a2018-01-17 03:21:09 -05004982 hw_resc->resv_cp_rings = cp;
4983 }
4984 mutex_unlock(&bp->hwrm_cmd_lock);
4985 return 0;
4986}
4987
Michael Chan391be5c2016-12-29 12:13:41 -05004988/* Caller must hold bp->hwrm_cmd_lock */
4989int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4990{
4991 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4992 struct hwrm_func_qcfg_input req = {0};
4993 int rc;
4994
4995 if (bp->hwrm_spec_code < 0x10601)
4996 return 0;
4997
4998 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4999 req.fid = cpu_to_le16(fid);
5000 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5001 if (!rc)
5002 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5003
5004 return rc;
5005}
5006
Michael Chan41e8d792018-10-14 07:02:48 -04005007static bool bnxt_rfs_supported(struct bnxt *bp);
5008
Michael Chan4ed50ef2018-03-09 23:46:03 -05005009static void
5010__bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5011 int tx_rings, int rx_rings, int ring_grps,
5012 int cp_rings, int vnics)
Michael Chan391be5c2016-12-29 12:13:41 -05005013{
Michael Chan674f50a2018-01-17 03:21:09 -05005014 u32 enables = 0;
Michael Chan391be5c2016-12-29 12:13:41 -05005015
Michael Chan4ed50ef2018-03-09 23:46:03 -05005016 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5017 req->fid = cpu_to_le16(0xffff);
Michael Chan674f50a2018-01-17 03:21:09 -05005018 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
Michael Chan4ed50ef2018-03-09 23:46:03 -05005019 req->num_tx_rings = cpu_to_le16(tx_rings);
Michael Chanf1ca94d2018-08-05 16:51:53 -04005020 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005021 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005022 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5023 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5024 enables |= tx_rings + ring_grps ?
5025 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5026 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5027 enables |= rx_rings ?
5028 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5029 } else {
5030 enables |= cp_rings ?
5031 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5032 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5033 enables |= ring_grps ?
5034 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5035 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5036 }
Michael Chandbe80d42018-10-05 00:26:00 -04005037 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
Michael Chan674f50a2018-01-17 03:21:09 -05005038
Michael Chan4ed50ef2018-03-09 23:46:03 -05005039 req->num_rx_rings = cpu_to_le16(rx_rings);
Michael Chan41e8d792018-10-14 07:02:48 -04005040 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5041 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5042 req->num_msix = cpu_to_le16(cp_rings);
5043 req->num_rsscos_ctxs =
5044 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5045 } else {
5046 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5047 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5048 req->num_rsscos_ctxs = cpu_to_le16(1);
5049 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5050 bnxt_rfs_supported(bp))
5051 req->num_rsscos_ctxs =
5052 cpu_to_le16(ring_grps + 1);
5053 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005054 req->num_stat_ctxs = req->num_cmpl_rings;
5055 req->num_vnics = cpu_to_le16(vnics);
Michael Chan674f50a2018-01-17 03:21:09 -05005056 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005057 req->enables = cpu_to_le32(enables);
5058}
5059
5060static void
5061__bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5062 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5063 int rx_rings, int ring_grps, int cp_rings,
5064 int vnics)
5065{
5066 u32 enables = 0;
5067
5068 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5069 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005070 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5071 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5072 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5073 enables |= tx_rings + ring_grps ?
5074 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5075 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5076 } else {
5077 enables |= cp_rings ?
5078 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5079 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5080 enables |= ring_grps ?
5081 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5082 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005083 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005084 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
Michael Chan4ed50ef2018-03-09 23:46:03 -05005085
Michael Chan41e8d792018-10-14 07:02:48 -04005086 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
Michael Chan4ed50ef2018-03-09 23:46:03 -05005087 req->num_tx_rings = cpu_to_le16(tx_rings);
5088 req->num_rx_rings = cpu_to_le16(rx_rings);
Michael Chan41e8d792018-10-14 07:02:48 -04005089 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5090 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5091 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5092 } else {
5093 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5094 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5095 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
5096 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005097 req->num_stat_ctxs = req->num_cmpl_rings;
5098 req->num_vnics = cpu_to_le16(vnics);
5099
5100 req->enables = cpu_to_le32(enables);
5101}
5102
5103static int
5104bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5105 int ring_grps, int cp_rings, int vnics)
5106{
5107 struct hwrm_func_cfg_input req = {0};
5108 int rc;
5109
5110 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5111 cp_rings, vnics);
5112 if (!req.enables)
Michael Chan674f50a2018-01-17 03:21:09 -05005113 return 0;
5114
Michael Chan674f50a2018-01-17 03:21:09 -05005115 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5116 if (rc)
5117 return -ENOMEM;
5118
5119 if (bp->hwrm_spec_code < 0x10601)
5120 bp->hw_resc.resv_tx_rings = tx_rings;
5121
5122 rc = bnxt_hwrm_get_rings(bp);
5123 return rc;
5124}
5125
5126static int
5127bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5128 int ring_grps, int cp_rings, int vnics)
5129{
5130 struct hwrm_func_vf_cfg_input req = {0};
Michael Chan674f50a2018-01-17 03:21:09 -05005131 int rc;
5132
Michael Chanf1ca94d2018-08-05 16:51:53 -04005133 if (!BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005134 bp->hw_resc.resv_tx_rings = tx_rings;
5135 return 0;
5136 }
5137
Michael Chan4ed50ef2018-03-09 23:46:03 -05005138 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5139 cp_rings, vnics);
Michael Chan674f50a2018-01-17 03:21:09 -05005140 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5141 if (rc)
5142 return -ENOMEM;
5143
5144 rc = bnxt_hwrm_get_rings(bp);
5145 return rc;
5146}
5147
5148static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
5149 int cp, int vnic)
5150{
5151 if (BNXT_PF(bp))
5152 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, vnic);
5153 else
5154 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, vnic);
5155}
5156
Michael Chan08654eb2018-03-31 13:54:17 -04005157static int bnxt_cp_rings_in_use(struct bnxt *bp)
5158{
5159 int cp = bp->cp_nr_rings;
5160 int ulp_msix, ulp_base;
5161
5162 ulp_msix = bnxt_get_ulp_msix_num(bp);
5163 if (ulp_msix) {
5164 ulp_base = bnxt_get_ulp_msix_base(bp);
5165 cp += ulp_msix;
5166 if ((ulp_base + ulp_msix) > cp)
5167 cp = ulp_base + ulp_msix;
5168 }
5169 return cp;
5170}
5171
Michael Chan4e41dc52018-03-31 13:54:19 -04005172static bool bnxt_need_reserve_rings(struct bnxt *bp)
5173{
5174 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005175 int cp = bnxt_cp_rings_in_use(bp);
Michael Chan4e41dc52018-03-31 13:54:19 -04005176 int rx = bp->rx_nr_rings;
5177 int vnic = 1, grp = rx;
5178
5179 if (bp->hwrm_spec_code < 0x10601)
5180 return false;
5181
5182 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
5183 return true;
5184
Michael Chan41e8d792018-10-14 07:02:48 -04005185 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
Michael Chan4e41dc52018-03-31 13:54:19 -04005186 vnic = rx + 1;
5187 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5188 rx <<= 1;
Michael Chanf1ca94d2018-08-05 16:51:53 -04005189 if (BNXT_NEW_RM(bp) &&
Michael Chan4e41dc52018-03-31 13:54:19 -04005190 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
Michael Chan41e8d792018-10-14 07:02:48 -04005191 hw_resc->resv_vnics != vnic ||
5192 (hw_resc->resv_hw_ring_grps != grp &&
5193 !(bp->flags & BNXT_FLAG_CHIP_P5))))
Michael Chan4e41dc52018-03-31 13:54:19 -04005194 return true;
5195 return false;
5196}
5197
Michael Chan674f50a2018-01-17 03:21:09 -05005198static int __bnxt_reserve_rings(struct bnxt *bp)
5199{
5200 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005201 int cp = bnxt_cp_rings_in_use(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05005202 int tx = bp->tx_nr_rings;
5203 int rx = bp->rx_nr_rings;
Michael Chan674f50a2018-01-17 03:21:09 -05005204 int grp, rx_rings, rc;
5205 bool sh = false;
5206 int vnic = 1;
5207
Michael Chan4e41dc52018-03-31 13:54:19 -04005208 if (!bnxt_need_reserve_rings(bp))
Michael Chan391be5c2016-12-29 12:13:41 -05005209 return 0;
5210
Michael Chan674f50a2018-01-17 03:21:09 -05005211 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5212 sh = true;
Michael Chan41e8d792018-10-14 07:02:48 -04005213 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
Michael Chan674f50a2018-01-17 03:21:09 -05005214 vnic = rx + 1;
5215 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5216 rx <<= 1;
Michael Chan674f50a2018-01-17 03:21:09 -05005217 grp = bp->rx_nr_rings;
Michael Chan391be5c2016-12-29 12:13:41 -05005218
Michael Chan674f50a2018-01-17 03:21:09 -05005219 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, vnic);
Michael Chan391be5c2016-12-29 12:13:41 -05005220 if (rc)
5221 return rc;
5222
Michael Chan674f50a2018-01-17 03:21:09 -05005223 tx = hw_resc->resv_tx_rings;
Michael Chanf1ca94d2018-08-05 16:51:53 -04005224 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005225 rx = hw_resc->resv_rx_rings;
5226 cp = hw_resc->resv_cp_rings;
5227 grp = hw_resc->resv_hw_ring_grps;
5228 vnic = hw_resc->resv_vnics;
5229 }
5230
5231 rx_rings = rx;
5232 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5233 if (rx >= 2) {
5234 rx_rings = rx >> 1;
5235 } else {
5236 if (netif_running(bp->dev))
5237 return -ENOMEM;
5238
5239 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
5240 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5241 bp->dev->hw_features &= ~NETIF_F_LRO;
5242 bp->dev->features &= ~NETIF_F_LRO;
5243 bnxt_set_ring_params(bp);
5244 }
5245 }
5246 rx_rings = min_t(int, rx_rings, grp);
5247 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
5248 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5249 rx = rx_rings << 1;
5250 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
5251 bp->tx_nr_rings = tx;
5252 bp->rx_nr_rings = rx_rings;
5253 bp->cp_nr_rings = cp;
5254
5255 if (!tx || !rx || !cp || !grp || !vnic)
5256 return -ENOMEM;
5257
Michael Chan391be5c2016-12-29 12:13:41 -05005258 return rc;
5259}
5260
Michael Chan8f23d632018-01-17 03:21:12 -05005261static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005262 int ring_grps, int cp_rings, int vnics)
Michael Chan98fdbe72017-08-28 13:40:26 -04005263{
Michael Chan8f23d632018-01-17 03:21:12 -05005264 struct hwrm_func_vf_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005265 u32 flags;
Michael Chan98fdbe72017-08-28 13:40:26 -04005266 int rc;
5267
Michael Chanf1ca94d2018-08-05 16:51:53 -04005268 if (!BNXT_NEW_RM(bp))
Michael Chan98fdbe72017-08-28 13:40:26 -04005269 return 0;
5270
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005271 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5272 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005273 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
5274 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5275 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
Michael Chan8f23d632018-01-17 03:21:12 -05005276 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
Michael Chan41e8d792018-10-14 07:02:48 -04005277 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
5278 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5279 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5280 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
Michael Chan98fdbe72017-08-28 13:40:26 -04005281
Michael Chan8f23d632018-01-17 03:21:12 -05005282 req.flags = cpu_to_le32(flags);
Michael Chan98fdbe72017-08-28 13:40:26 -04005283 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5284 if (rc)
5285 return -ENOMEM;
5286 return 0;
5287}
5288
Michael Chan8f23d632018-01-17 03:21:12 -05005289static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005290 int ring_grps, int cp_rings, int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05005291{
5292 struct hwrm_func_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005293 u32 flags;
Michael Chan8f23d632018-01-17 03:21:12 -05005294 int rc;
5295
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005296 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5297 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005298 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
Michael Chan41e8d792018-10-14 07:02:48 -04005299 if (BNXT_NEW_RM(bp)) {
Michael Chan8f23d632018-01-17 03:21:12 -05005300 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5301 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
Michael Chan8f23d632018-01-17 03:21:12 -05005302 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5303 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
Michael Chan41e8d792018-10-14 07:02:48 -04005304 if (bp->flags & BNXT_FLAG_CHIP_P5)
5305 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5306 else
5307 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5308 }
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005309
Michael Chan8f23d632018-01-17 03:21:12 -05005310 req.flags = cpu_to_le32(flags);
Michael Chan8f23d632018-01-17 03:21:12 -05005311 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5312 if (rc)
5313 return -ENOMEM;
5314 return 0;
5315}
5316
5317static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005318 int ring_grps, int cp_rings, int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05005319{
5320 if (bp->hwrm_spec_code < 0x10801)
5321 return 0;
5322
5323 if (BNXT_PF(bp))
5324 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005325 ring_grps, cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005326
5327 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005328 cp_rings, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005329}
5330
Michael Chan74706af2018-10-14 07:02:40 -04005331static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
5332{
5333 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5334 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5335 struct hwrm_ring_aggint_qcaps_input req = {0};
5336 int rc;
5337
5338 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
5339 coal_cap->num_cmpl_dma_aggr_max = 63;
5340 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
5341 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
5342 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
5343 coal_cap->int_lat_tmr_min_max = 65535;
5344 coal_cap->int_lat_tmr_max_max = 65535;
5345 coal_cap->num_cmpl_aggr_int_max = 65535;
5346 coal_cap->timer_units = 80;
5347
5348 if (bp->hwrm_spec_code < 0x10902)
5349 return;
5350
5351 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
5352 mutex_lock(&bp->hwrm_cmd_lock);
5353 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5354 if (!rc) {
5355 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
5356 coal_cap->num_cmpl_dma_aggr_max =
5357 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
5358 coal_cap->num_cmpl_dma_aggr_during_int_max =
5359 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
5360 coal_cap->cmpl_aggr_dma_tmr_max =
5361 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
5362 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
5363 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
5364 coal_cap->int_lat_tmr_min_max =
5365 le16_to_cpu(resp->int_lat_tmr_min_max);
5366 coal_cap->int_lat_tmr_max_max =
5367 le16_to_cpu(resp->int_lat_tmr_max_max);
5368 coal_cap->num_cmpl_aggr_int_max =
5369 le16_to_cpu(resp->num_cmpl_aggr_int_max);
5370 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
5371 }
5372 mutex_unlock(&bp->hwrm_cmd_lock);
5373}
5374
5375static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
5376{
5377 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5378
5379 return usec * 1000 / coal_cap->timer_units;
5380}
5381
5382static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
5383 struct bnxt_coal *hw_coal,
Michael Chanbb053f52016-02-26 04:00:02 -05005384 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
5385{
Michael Chan74706af2018-10-14 07:02:40 -04005386 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5387 u32 cmpl_params = coal_cap->cmpl_params;
5388 u16 val, tmr, max, flags = 0;
Michael Chanf8503962017-10-26 11:51:28 -04005389
5390 max = hw_coal->bufs_per_record * 128;
5391 if (hw_coal->budget)
5392 max = hw_coal->bufs_per_record * hw_coal->budget;
Michael Chan74706af2018-10-14 07:02:40 -04005393 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
Michael Chanf8503962017-10-26 11:51:28 -04005394
5395 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
5396 req->num_cmpl_aggr_int = cpu_to_le16(val);
Michael Chanb153cbc2017-11-03 03:32:39 -04005397
Michael Chan74706af2018-10-14 07:02:40 -04005398 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
Michael Chanf8503962017-10-26 11:51:28 -04005399 req->num_cmpl_dma_aggr = cpu_to_le16(val);
5400
Michael Chan74706af2018-10-14 07:02:40 -04005401 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
5402 coal_cap->num_cmpl_dma_aggr_during_int_max);
Michael Chanf8503962017-10-26 11:51:28 -04005403 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
5404
Michael Chan74706af2018-10-14 07:02:40 -04005405 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
5406 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
Michael Chanf8503962017-10-26 11:51:28 -04005407 req->int_lat_tmr_max = cpu_to_le16(tmr);
5408
5409 /* min timer set to 1/2 of interrupt timer */
Michael Chan74706af2018-10-14 07:02:40 -04005410 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
5411 val = tmr / 2;
5412 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
5413 req->int_lat_tmr_min = cpu_to_le16(val);
5414 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
5415 }
Michael Chanf8503962017-10-26 11:51:28 -04005416
5417 /* buf timer set to 1/4 of interrupt timer */
Michael Chan74706af2018-10-14 07:02:40 -04005418 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
Michael Chanf8503962017-10-26 11:51:28 -04005419 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
5420
Michael Chan74706af2018-10-14 07:02:40 -04005421 if (cmpl_params &
5422 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
5423 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
5424 val = clamp_t(u16, tmr, 1,
5425 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
5426 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
5427 req->enables |=
5428 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
5429 }
Michael Chanf8503962017-10-26 11:51:28 -04005430
Michael Chan74706af2018-10-14 07:02:40 -04005431 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
5432 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
5433 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
5434 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
Michael Chanf8503962017-10-26 11:51:28 -04005435 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
Michael Chanbb053f52016-02-26 04:00:02 -05005436 req->flags = cpu_to_le16(flags);
Michael Chan74706af2018-10-14 07:02:40 -04005437 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
Michael Chanbb053f52016-02-26 04:00:02 -05005438}
5439
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005440int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
5441{
5442 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
5443 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5444 struct bnxt_coal coal;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005445
5446 /* Tick values in micro seconds.
5447 * 1 coal_buf x bufs_per_record = 1 completion record.
5448 */
5449 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
5450
5451 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
5452 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
5453
5454 if (!bnapi->rx_ring)
5455 return -ENODEV;
5456
5457 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5458 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5459
Michael Chan74706af2018-10-14 07:02:40 -04005460 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005461
Michael Chan2c61d212018-10-14 07:02:50 -04005462 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005463
5464 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
5465 HWRM_CMD_TIMEOUT);
5466}
5467
Michael Chanc0c050c2015-10-22 16:01:17 -04005468int bnxt_hwrm_set_coal(struct bnxt *bp)
5469{
5470 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05005471 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
5472 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04005473
Michael Chandfc9c942016-02-26 04:00:03 -05005474 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5475 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5476 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
5477 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005478
Michael Chan74706af2018-10-14 07:02:40 -04005479 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
5480 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04005481
5482 mutex_lock(&bp->hwrm_cmd_lock);
5483 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05005484 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chan2c61d212018-10-14 07:02:50 -04005485 u16 ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005486
Michael Chandfc9c942016-02-26 04:00:03 -05005487 req = &req_rx;
Michael Chan2c61d212018-10-14 07:02:50 -04005488 if (!bnapi->rx_ring) {
5489 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
Michael Chandfc9c942016-02-26 04:00:03 -05005490 req = &req_tx;
Michael Chan2c61d212018-10-14 07:02:50 -04005491 } else {
5492 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
5493 }
5494 req->ring_id = cpu_to_le16(ring_id);
Michael Chandfc9c942016-02-26 04:00:03 -05005495
5496 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04005497 HWRM_CMD_TIMEOUT);
5498 if (rc)
5499 break;
5500 }
5501 mutex_unlock(&bp->hwrm_cmd_lock);
5502 return rc;
5503}
5504
5505static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5506{
5507 int rc = 0, i;
5508 struct hwrm_stat_ctx_free_input req = {0};
5509
5510 if (!bp->bnapi)
5511 return 0;
5512
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005513 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5514 return 0;
5515
Michael Chanc0c050c2015-10-22 16:01:17 -04005516 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5517
5518 mutex_lock(&bp->hwrm_cmd_lock);
5519 for (i = 0; i < bp->cp_nr_rings; i++) {
5520 struct bnxt_napi *bnapi = bp->bnapi[i];
5521 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5522
5523 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5524 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5525
5526 rc = _hwrm_send_message(bp, &req, sizeof(req),
5527 HWRM_CMD_TIMEOUT);
5528 if (rc)
5529 break;
5530
5531 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5532 }
5533 }
5534 mutex_unlock(&bp->hwrm_cmd_lock);
5535 return rc;
5536}
5537
5538static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5539{
5540 int rc = 0, i;
5541 struct hwrm_stat_ctx_alloc_input req = {0};
5542 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5543
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005544 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5545 return 0;
5546
Michael Chanc0c050c2015-10-22 16:01:17 -04005547 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5548
Michael Chan51f30782016-07-01 18:46:29 -04005549 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04005550
5551 mutex_lock(&bp->hwrm_cmd_lock);
5552 for (i = 0; i < bp->cp_nr_rings; i++) {
5553 struct bnxt_napi *bnapi = bp->bnapi[i];
5554 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5555
5556 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5557
5558 rc = _hwrm_send_message(bp, &req, sizeof(req),
5559 HWRM_CMD_TIMEOUT);
5560 if (rc)
5561 break;
5562
5563 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5564
5565 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5566 }
5567 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08005568 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005569}
5570
Michael Chancf6645f2016-06-13 02:25:28 -04005571static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5572{
5573 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005574 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04005575 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04005576 int rc;
5577
5578 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5579 req.fid = cpu_to_le16(0xffff);
5580 mutex_lock(&bp->hwrm_cmd_lock);
5581 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5582 if (rc)
5583 goto func_qcfg_exit;
5584
5585#ifdef CONFIG_BNXT_SRIOV
5586 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04005587 struct bnxt_vf_info *vf = &bp->vf;
5588
5589 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5590 }
5591#endif
Michael Chan9315edc2017-07-24 12:34:25 -04005592 flags = le16_to_cpu(resp->flags);
5593 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5594 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
Michael Chan97381a12018-08-05 16:51:54 -04005595 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
Michael Chan9315edc2017-07-24 12:34:25 -04005596 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
Michael Chan97381a12018-08-05 16:51:54 -04005597 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04005598 }
Michael Chan9315edc2017-07-24 12:34:25 -04005599 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5600 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05005601
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005602 switch (resp->port_partition_type) {
5603 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5604 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5605 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5606 bp->port_partition_type = resp->port_partition_type;
5607 break;
5608 }
Michael Chan32e8239c2017-07-24 12:34:21 -04005609 if (bp->hwrm_spec_code < 0x10707 ||
5610 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5611 bp->br_mode = BRIDGE_MODE_VEB;
5612 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5613 bp->br_mode = BRIDGE_MODE_VEPA;
5614 else
5615 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04005616
Michael Chan7eb9bb32017-10-26 11:51:25 -04005617 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5618 if (!bp->max_mtu)
5619 bp->max_mtu = BNXT_MAX_MTU;
5620
Michael Chancf6645f2016-06-13 02:25:28 -04005621func_qcfg_exit:
5622 mutex_unlock(&bp->hwrm_cmd_lock);
5623 return rc;
5624}
5625
Michael Chan98f04cf2018-10-14 07:02:43 -04005626static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
5627{
5628 struct hwrm_func_backing_store_qcaps_input req = {0};
5629 struct hwrm_func_backing_store_qcaps_output *resp =
5630 bp->hwrm_cmd_resp_addr;
5631 int rc;
5632
5633 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
5634 return 0;
5635
5636 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
5637 mutex_lock(&bp->hwrm_cmd_lock);
5638 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5639 if (!rc) {
5640 struct bnxt_ctx_pg_info *ctx_pg;
5641 struct bnxt_ctx_mem_info *ctx;
5642 int i;
5643
5644 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
5645 if (!ctx) {
5646 rc = -ENOMEM;
5647 goto ctx_err;
5648 }
5649 ctx_pg = kzalloc(sizeof(*ctx_pg) * (bp->max_q + 1), GFP_KERNEL);
5650 if (!ctx_pg) {
5651 kfree(ctx);
5652 rc = -ENOMEM;
5653 goto ctx_err;
5654 }
5655 for (i = 0; i < bp->max_q + 1; i++, ctx_pg++)
5656 ctx->tqm_mem[i] = ctx_pg;
5657
5658 bp->ctx = ctx;
5659 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
5660 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
5661 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
5662 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
5663 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
5664 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
5665 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
5666 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
5667 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
5668 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
5669 ctx->vnic_max_vnic_entries =
5670 le16_to_cpu(resp->vnic_max_vnic_entries);
5671 ctx->vnic_max_ring_table_entries =
5672 le16_to_cpu(resp->vnic_max_ring_table_entries);
5673 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
5674 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
5675 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
5676 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
5677 ctx->tqm_min_entries_per_ring =
5678 le32_to_cpu(resp->tqm_min_entries_per_ring);
5679 ctx->tqm_max_entries_per_ring =
5680 le32_to_cpu(resp->tqm_max_entries_per_ring);
5681 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
5682 if (!ctx->tqm_entries_multiple)
5683 ctx->tqm_entries_multiple = 1;
5684 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
5685 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
5686 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
5687 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
5688 } else {
5689 rc = 0;
5690 }
5691ctx_err:
5692 mutex_unlock(&bp->hwrm_cmd_lock);
5693 return rc;
5694}
5695
Michael Chan1b9394e2018-10-14 07:02:44 -04005696static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
5697 __le64 *pg_dir)
5698{
5699 u8 pg_size = 0;
5700
5701 if (BNXT_PAGE_SHIFT == 13)
5702 pg_size = 1 << 4;
5703 else if (BNXT_PAGE_SIZE == 16)
5704 pg_size = 2 << 4;
5705
5706 *pg_attr = pg_size;
5707 if (rmem->nr_pages > 1) {
5708 *pg_attr |= 1;
5709 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
5710 } else {
5711 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
5712 }
5713}
5714
5715#define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
5716 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
5717 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
5718 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
5719 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
5720 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
5721
5722static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
5723{
5724 struct hwrm_func_backing_store_cfg_input req = {0};
5725 struct bnxt_ctx_mem_info *ctx = bp->ctx;
5726 struct bnxt_ctx_pg_info *ctx_pg;
5727 __le32 *num_entries;
5728 __le64 *pg_dir;
5729 u8 *pg_attr;
5730 int i, rc;
5731 u32 ena;
5732
5733 if (!ctx)
5734 return 0;
5735
5736 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
5737 req.enables = cpu_to_le32(enables);
5738
5739 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
5740 ctx_pg = &ctx->qp_mem;
5741 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
5742 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
5743 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
5744 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
5745 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5746 &req.qpc_pg_size_qpc_lvl,
5747 &req.qpc_page_dir);
5748 }
5749 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
5750 ctx_pg = &ctx->srq_mem;
5751 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
5752 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
5753 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
5754 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5755 &req.srq_pg_size_srq_lvl,
5756 &req.srq_page_dir);
5757 }
5758 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
5759 ctx_pg = &ctx->cq_mem;
5760 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
5761 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
5762 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
5763 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
5764 &req.cq_page_dir);
5765 }
5766 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
5767 ctx_pg = &ctx->vnic_mem;
5768 req.vnic_num_vnic_entries =
5769 cpu_to_le16(ctx->vnic_max_vnic_entries);
5770 req.vnic_num_ring_table_entries =
5771 cpu_to_le16(ctx->vnic_max_ring_table_entries);
5772 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
5773 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5774 &req.vnic_pg_size_vnic_lvl,
5775 &req.vnic_page_dir);
5776 }
5777 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
5778 ctx_pg = &ctx->stat_mem;
5779 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
5780 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
5781 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
5782 &req.stat_pg_size_stat_lvl,
5783 &req.stat_page_dir);
5784 }
5785 for (i = 0, num_entries = &req.tqm_sp_num_entries,
5786 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
5787 pg_dir = &req.tqm_sp_page_dir,
5788 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
5789 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
5790 if (!(enables & ena))
5791 continue;
5792
5793 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
5794 ctx_pg = ctx->tqm_mem[i];
5795 *num_entries = cpu_to_le32(ctx_pg->entries);
5796 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
5797 }
5798 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5799 if (rc)
5800 rc = -EIO;
5801 return rc;
5802}
5803
Michael Chan98f04cf2018-10-14 07:02:43 -04005804static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
5805 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size)
5806{
5807 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
5808
5809 if (!mem_size)
5810 return 0;
5811
5812 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
5813 if (rmem->nr_pages > MAX_CTX_PAGES) {
5814 rmem->nr_pages = 0;
5815 return -EINVAL;
5816 }
5817 rmem->page_size = BNXT_PAGE_SIZE;
5818 rmem->pg_arr = ctx_pg->ctx_pg_arr;
5819 rmem->dma_arr = ctx_pg->ctx_dma_arr;
Michael Chan1b9394e2018-10-14 07:02:44 -04005820 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
Michael Chan98f04cf2018-10-14 07:02:43 -04005821 return bnxt_alloc_ring(bp, rmem);
5822}
5823
5824static void bnxt_free_ctx_mem(struct bnxt *bp)
5825{
5826 struct bnxt_ctx_mem_info *ctx = bp->ctx;
5827 int i;
5828
5829 if (!ctx)
5830 return;
5831
5832 if (ctx->tqm_mem[0]) {
5833 for (i = 0; i < bp->max_q + 1; i++)
5834 bnxt_free_ring(bp, &ctx->tqm_mem[i]->ring_mem);
5835 kfree(ctx->tqm_mem[0]);
5836 ctx->tqm_mem[0] = NULL;
5837 }
5838
5839 bnxt_free_ring(bp, &ctx->stat_mem.ring_mem);
5840 bnxt_free_ring(bp, &ctx->vnic_mem.ring_mem);
5841 bnxt_free_ring(bp, &ctx->cq_mem.ring_mem);
5842 bnxt_free_ring(bp, &ctx->srq_mem.ring_mem);
5843 bnxt_free_ring(bp, &ctx->qp_mem.ring_mem);
5844 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
5845}
5846
5847static int bnxt_alloc_ctx_mem(struct bnxt *bp)
5848{
5849 struct bnxt_ctx_pg_info *ctx_pg;
5850 struct bnxt_ctx_mem_info *ctx;
Michael Chan1b9394e2018-10-14 07:02:44 -04005851 u32 mem_size, ena, entries;
Michael Chan98f04cf2018-10-14 07:02:43 -04005852 int i, rc;
5853
5854 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
5855 if (rc) {
5856 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
5857 rc);
5858 return rc;
5859 }
5860 ctx = bp->ctx;
5861 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
5862 return 0;
5863
5864 ctx_pg = &ctx->qp_mem;
5865 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries;
5866 mem_size = ctx->qp_entry_size * ctx_pg->entries;
5867 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5868 if (rc)
5869 return rc;
5870
5871 ctx_pg = &ctx->srq_mem;
5872 ctx_pg->entries = ctx->srq_max_l2_entries;
5873 mem_size = ctx->srq_entry_size * ctx_pg->entries;
5874 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5875 if (rc)
5876 return rc;
5877
5878 ctx_pg = &ctx->cq_mem;
5879 ctx_pg->entries = ctx->cq_max_l2_entries;
5880 mem_size = ctx->cq_entry_size * ctx_pg->entries;
5881 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5882 if (rc)
5883 return rc;
5884
5885 ctx_pg = &ctx->vnic_mem;
5886 ctx_pg->entries = ctx->vnic_max_vnic_entries +
5887 ctx->vnic_max_ring_table_entries;
5888 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
5889 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5890 if (rc)
5891 return rc;
5892
5893 ctx_pg = &ctx->stat_mem;
5894 ctx_pg->entries = ctx->stat_max_entries;
5895 mem_size = ctx->stat_entry_size * ctx_pg->entries;
5896 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5897 if (rc)
5898 return rc;
5899
5900 entries = ctx->qp_max_l2_entries;
5901 entries = roundup(entries, ctx->tqm_entries_multiple);
5902 entries = clamp_t(u32, entries, ctx->tqm_min_entries_per_ring,
5903 ctx->tqm_max_entries_per_ring);
Michael Chan1b9394e2018-10-14 07:02:44 -04005904 for (i = 0, ena = 0; i < bp->max_q + 1; i++) {
Michael Chan98f04cf2018-10-14 07:02:43 -04005905 ctx_pg = ctx->tqm_mem[i];
5906 ctx_pg->entries = entries;
5907 mem_size = ctx->tqm_entry_size * entries;
5908 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
5909 if (rc)
5910 return rc;
Michael Chan1b9394e2018-10-14 07:02:44 -04005911 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
Michael Chan98f04cf2018-10-14 07:02:43 -04005912 }
Michael Chan1b9394e2018-10-14 07:02:44 -04005913 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
5914 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
5915 if (rc)
5916 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
5917 rc);
5918 else
5919 ctx->flags |= BNXT_CTX_FLAG_INITED;
5920
Michael Chan98f04cf2018-10-14 07:02:43 -04005921 return 0;
5922}
5923
Michael Chandb4723b2018-03-31 13:54:13 -04005924int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005925{
5926 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5927 struct hwrm_func_resource_qcaps_input req = {0};
5928 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5929 int rc;
5930
5931 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
5932 req.fid = cpu_to_le16(0xffff);
5933
5934 mutex_lock(&bp->hwrm_cmd_lock);
5935 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5936 if (rc) {
5937 rc = -EIO;
5938 goto hwrm_func_resc_qcaps_exit;
5939 }
5940
Michael Chandb4723b2018-03-31 13:54:13 -04005941 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
5942 if (!all)
5943 goto hwrm_func_resc_qcaps_exit;
5944
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005945 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
5946 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5947 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
5948 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5949 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
5950 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5951 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
5952 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5953 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
5954 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
5955 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
5956 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5957 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
5958 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5959 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
5960 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5961
Michael Chan9c1fabd2018-10-14 07:02:47 -04005962 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5963 u16 max_msix = le16_to_cpu(resp->max_msix);
5964
5965 hw_resc->max_irqs = min_t(u16, hw_resc->max_irqs, max_msix);
5966 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
5967 }
5968
Michael Chan4673d662018-01-17 03:21:11 -05005969 if (BNXT_PF(bp)) {
5970 struct bnxt_pf_info *pf = &bp->pf;
5971
5972 pf->vf_resv_strategy =
5973 le16_to_cpu(resp->vf_reservation_strategy);
Michael Chanbf827362018-08-05 16:51:50 -04005974 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
Michael Chan4673d662018-01-17 03:21:11 -05005975 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
5976 }
Michael Chanbe0dd9c2018-01-17 03:21:07 -05005977hwrm_func_resc_qcaps_exit:
5978 mutex_unlock(&bp->hwrm_cmd_lock);
5979 return rc;
5980}
5981
5982static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005983{
5984 int rc = 0;
5985 struct hwrm_func_qcaps_input req = {0};
5986 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6a4f2942018-01-17 03:21:06 -05005987 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5988 u32 flags;
Michael Chanc0c050c2015-10-22 16:01:17 -04005989
5990 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
5991 req.fid = cpu_to_le16(0xffff);
5992
5993 mutex_lock(&bp->hwrm_cmd_lock);
5994 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5995 if (rc)
5996 goto hwrm_func_qcaps_exit;
5997
Michael Chan6a4f2942018-01-17 03:21:06 -05005998 flags = le32_to_cpu(resp->flags);
5999 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05006000 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
Michael Chan6a4f2942018-01-17 03:21:06 -05006001 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05006002 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
6003
Michael Chan7cc5a202016-09-19 03:58:05 -04006004 bp->tx_push_thresh = 0;
Michael Chan6a4f2942018-01-17 03:21:06 -05006005 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
Michael Chan7cc5a202016-09-19 03:58:05 -04006006 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
6007
Michael Chan6a4f2942018-01-17 03:21:06 -05006008 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6009 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6010 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6011 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6012 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
6013 if (!hw_resc->max_hw_ring_grps)
6014 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
6015 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6016 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6017 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6018
Michael Chanc0c050c2015-10-22 16:01:17 -04006019 if (BNXT_PF(bp)) {
6020 struct bnxt_pf_info *pf = &bp->pf;
6021
6022 pf->fw_fid = le16_to_cpu(resp->fid);
6023 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04006024 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04006025 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006026 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
6027 pf->max_vfs = le16_to_cpu(resp->max_vfs);
6028 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
6029 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
6030 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
6031 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
6032 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
6033 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chan6a4f2942018-01-17 03:21:06 -05006034 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
Michael Chanc1ef1462017-04-04 18:14:07 -04006035 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006036 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04006037#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04006038 struct bnxt_vf_info *vf = &bp->vf;
6039
6040 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan7cc5a202016-09-19 03:58:05 -04006041 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan379a80a2015-10-23 15:06:19 -04006042#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006043 }
6044
Michael Chanc0c050c2015-10-22 16:01:17 -04006045hwrm_func_qcaps_exit:
6046 mutex_unlock(&bp->hwrm_cmd_lock);
6047 return rc;
6048}
6049
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006050static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
6051{
6052 int rc;
6053
6054 rc = __bnxt_hwrm_func_qcaps(bp);
6055 if (rc)
6056 return rc;
6057 if (bp->hwrm_spec_code >= 0x10803) {
Michael Chan98f04cf2018-10-14 07:02:43 -04006058 rc = bnxt_alloc_ctx_mem(bp);
6059 if (rc)
6060 return rc;
Michael Chandb4723b2018-03-31 13:54:13 -04006061 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006062 if (!rc)
Michael Chan97381a12018-08-05 16:51:54 -04006063 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006064 }
6065 return 0;
6066}
6067
Michael Chanc0c050c2015-10-22 16:01:17 -04006068static int bnxt_hwrm_func_reset(struct bnxt *bp)
6069{
6070 struct hwrm_func_reset_input req = {0};
6071
6072 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
6073 req.enables = 0;
6074
6075 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
6076}
6077
6078static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
6079{
6080 int rc = 0;
6081 struct hwrm_queue_qportcfg_input req = {0};
6082 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chanaabfc012018-08-05 16:51:58 -04006083 u8 i, j, *qptr;
6084 bool no_rdma;
Michael Chanc0c050c2015-10-22 16:01:17 -04006085
6086 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
6087
6088 mutex_lock(&bp->hwrm_cmd_lock);
6089 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6090 if (rc)
6091 goto qportcfg_exit;
6092
6093 if (!resp->max_configurable_queues) {
6094 rc = -EINVAL;
6095 goto qportcfg_exit;
6096 }
6097 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05006098 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04006099 if (bp->max_tc > BNXT_MAX_QUEUE)
6100 bp->max_tc = BNXT_MAX_QUEUE;
6101
Michael Chanaabfc012018-08-05 16:51:58 -04006102 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
6103 qptr = &resp->queue_id0;
6104 for (i = 0, j = 0; i < bp->max_tc; i++) {
Michael Chan98f04cf2018-10-14 07:02:43 -04006105 bp->q_info[j].queue_id = *qptr;
6106 bp->q_ids[i] = *qptr++;
Michael Chanaabfc012018-08-05 16:51:58 -04006107 bp->q_info[j].queue_profile = *qptr++;
6108 bp->tc_to_qidx[j] = j;
6109 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
6110 (no_rdma && BNXT_PF(bp)))
6111 j++;
6112 }
Michael Chan98f04cf2018-10-14 07:02:43 -04006113 bp->max_q = bp->max_tc;
Michael Chanaabfc012018-08-05 16:51:58 -04006114 bp->max_tc = max_t(u8, j, 1);
6115
Michael Chan441cabb2016-09-19 03:58:02 -04006116 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
6117 bp->max_tc = 1;
6118
Michael Chan87c374d2016-12-02 21:17:16 -05006119 if (bp->max_lltc > bp->max_tc)
6120 bp->max_lltc = bp->max_tc;
6121
Michael Chanc0c050c2015-10-22 16:01:17 -04006122qportcfg_exit:
6123 mutex_unlock(&bp->hwrm_cmd_lock);
6124 return rc;
6125}
6126
6127static int bnxt_hwrm_ver_get(struct bnxt *bp)
6128{
6129 int rc;
6130 struct hwrm_ver_get_input req = {0};
6131 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04006132 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04006133
Michael Chane6ef2692016-03-28 19:46:05 -04006134 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04006135 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
6136 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
6137 req.hwrm_intf_min = HWRM_VERSION_MINOR;
6138 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
6139 mutex_lock(&bp->hwrm_cmd_lock);
6140 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6141 if (rc)
6142 goto hwrm_ver_get_exit;
6143
6144 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
6145
Michael Chan894aa692018-01-17 03:21:03 -05006146 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
6147 resp->hwrm_intf_min_8b << 8 |
6148 resp->hwrm_intf_upd_8b;
6149 if (resp->hwrm_intf_maj_8b < 1) {
Michael Chanc1935542015-12-27 18:19:28 -05006150 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chan894aa692018-01-17 03:21:03 -05006151 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
6152 resp->hwrm_intf_upd_8b);
Michael Chanc1935542015-12-27 18:19:28 -05006153 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006154 }
Michael Chan431aa1e2017-10-26 11:51:23 -04006155 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
Michael Chan894aa692018-01-17 03:21:03 -05006156 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
6157 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
Michael Chanc0c050c2015-10-22 16:01:17 -04006158
Michael Chanff4fe812016-02-26 04:00:04 -05006159 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
6160 if (!bp->hwrm_cmd_timeout)
6161 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
6162
Michael Chan1dfddc42018-10-14 07:02:39 -04006163 if (resp->hwrm_intf_maj_8b >= 1) {
Michael Chane6ef2692016-03-28 19:46:05 -04006164 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
Michael Chan1dfddc42018-10-14 07:02:39 -04006165 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
6166 }
6167 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
6168 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
Michael Chane6ef2692016-03-28 19:46:05 -04006169
Michael Chan659c8052016-06-13 02:25:33 -04006170 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006171 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
6172 !resp->chip_metal)
6173 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04006174
Deepak Khungare605db82017-05-29 19:06:04 -04006175 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
6176 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
6177 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
Michael Chan97381a12018-08-05 16:51:54 -04006178 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
Deepak Khungare605db82017-05-29 19:06:04 -04006179
Michael Chanc0c050c2015-10-22 16:01:17 -04006180hwrm_ver_get_exit:
6181 mutex_unlock(&bp->hwrm_cmd_lock);
6182 return rc;
6183}
6184
Rob Swindell5ac67d82016-09-19 03:58:03 -04006185int bnxt_hwrm_fw_set_time(struct bnxt *bp)
6186{
6187 struct hwrm_fw_set_time_input req = {0};
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01006188 struct tm tm;
6189 time64_t now = ktime_get_real_seconds();
Rob Swindell5ac67d82016-09-19 03:58:03 -04006190
Michael Chanca2c39e2018-04-26 17:44:34 -04006191 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
6192 bp->hwrm_spec_code < 0x10400)
Rob Swindell5ac67d82016-09-19 03:58:03 -04006193 return -EOPNOTSUPP;
6194
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01006195 time64_to_tm(now, 0, &tm);
Rob Swindell5ac67d82016-09-19 03:58:03 -04006196 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
6197 req.year = cpu_to_le16(1900 + tm.tm_year);
6198 req.month = 1 + tm.tm_mon;
6199 req.day = tm.tm_mday;
6200 req.hour = tm.tm_hour;
6201 req.minute = tm.tm_min;
6202 req.second = tm.tm_sec;
6203 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6204}
6205
Michael Chan3bdf56c2016-03-07 15:38:45 -05006206static int bnxt_hwrm_port_qstats(struct bnxt *bp)
6207{
6208 int rc;
6209 struct bnxt_pf_info *pf = &bp->pf;
6210 struct hwrm_port_qstats_input req = {0};
6211
6212 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
6213 return 0;
6214
6215 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
6216 req.port_id = cpu_to_le16(pf->port_id);
6217 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
6218 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
6219 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6220 return rc;
6221}
6222
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006223static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
6224{
Michael Chan36e53342018-10-14 07:02:38 -04006225 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006226 struct hwrm_port_qstats_ext_input req = {0};
6227 struct bnxt_pf_info *pf = &bp->pf;
Michael Chan36e53342018-10-14 07:02:38 -04006228 int rc;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006229
6230 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
6231 return 0;
6232
6233 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
6234 req.port_id = cpu_to_le16(pf->port_id);
6235 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
6236 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
Michael Chan36e53342018-10-14 07:02:38 -04006237 req.tx_stat_size = cpu_to_le16(sizeof(struct tx_port_stats_ext));
6238 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
6239 mutex_lock(&bp->hwrm_cmd_lock);
6240 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6241 if (!rc) {
6242 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
6243 bp->fw_tx_stats_ext_size = le16_to_cpu(resp->tx_stat_size) / 8;
6244 } else {
6245 bp->fw_rx_stats_ext_size = 0;
6246 bp->fw_tx_stats_ext_size = 0;
6247 }
6248 mutex_unlock(&bp->hwrm_cmd_lock);
6249 return rc;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006250}
6251
Michael Chanc0c050c2015-10-22 16:01:17 -04006252static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
6253{
6254 if (bp->vxlan_port_cnt) {
6255 bnxt_hwrm_tunnel_dst_port_free(
6256 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6257 }
6258 bp->vxlan_port_cnt = 0;
6259 if (bp->nge_port_cnt) {
6260 bnxt_hwrm_tunnel_dst_port_free(
6261 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6262 }
6263 bp->nge_port_cnt = 0;
6264}
6265
6266static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
6267{
6268 int rc, i;
6269 u32 tpa_flags = 0;
6270
6271 if (set_tpa)
6272 tpa_flags = bp->flags & BNXT_FLAG_TPA;
6273 for (i = 0; i < bp->nr_vnics; i++) {
6274 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
6275 if (rc) {
6276 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04006277 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04006278 return rc;
6279 }
6280 }
6281 return 0;
6282}
6283
6284static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
6285{
6286 int i;
6287
6288 for (i = 0; i < bp->nr_vnics; i++)
6289 bnxt_hwrm_vnic_set_rss(bp, i, false);
6290}
6291
6292static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
6293 bool irq_re_init)
6294{
6295 if (bp->vnic_info) {
6296 bnxt_hwrm_clear_vnic_filter(bp);
6297 /* clear all RSS setting before free vnic ctx */
6298 bnxt_hwrm_clear_vnic_rss(bp);
6299 bnxt_hwrm_vnic_ctx_free(bp);
6300 /* before free the vnic, undo the vnic tpa settings */
6301 if (bp->flags & BNXT_FLAG_TPA)
6302 bnxt_set_tpa(bp, false);
6303 bnxt_hwrm_vnic_free(bp);
6304 }
6305 bnxt_hwrm_ring_free(bp, close_path);
6306 bnxt_hwrm_ring_grp_free(bp);
6307 if (irq_re_init) {
6308 bnxt_hwrm_stat_ctx_free(bp);
6309 bnxt_hwrm_free_tunnel_ports(bp);
6310 }
6311}
6312
Michael Chan39d8ba22017-07-24 12:34:22 -04006313static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
6314{
6315 struct hwrm_func_cfg_input req = {0};
6316 int rc;
6317
6318 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6319 req.fid = cpu_to_le16(0xffff);
6320 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
6321 if (br_mode == BRIDGE_MODE_VEB)
6322 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
6323 else if (br_mode == BRIDGE_MODE_VEPA)
6324 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
6325 else
6326 return -EINVAL;
6327 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6328 if (rc)
6329 rc = -EIO;
6330 return rc;
6331}
6332
Michael Chanc3480a62018-01-17 03:21:15 -05006333static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
6334{
6335 struct hwrm_func_cfg_input req = {0};
6336 int rc;
6337
6338 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
6339 return 0;
6340
6341 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6342 req.fid = cpu_to_le16(0xffff);
6343 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
Michael Chand4f52de02018-03-31 13:54:06 -04006344 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
Michael Chanc3480a62018-01-17 03:21:15 -05006345 if (size == 128)
Michael Chand4f52de02018-03-31 13:54:06 -04006346 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
Michael Chanc3480a62018-01-17 03:21:15 -05006347
6348 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6349 if (rc)
6350 rc = -EIO;
6351 return rc;
6352}
6353
Michael Chanc0c050c2015-10-22 16:01:17 -04006354static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
6355{
Michael Chanae10ae72016-12-29 12:13:38 -05006356 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04006357 int rc;
6358
Michael Chanae10ae72016-12-29 12:13:38 -05006359 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
6360 goto skip_rss_ctx;
6361
Michael Chanc0c050c2015-10-22 16:01:17 -04006362 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04006363 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04006364 if (rc) {
6365 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
6366 vnic_id, rc);
6367 goto vnic_setup_err;
6368 }
6369 bp->rsscos_nr_ctxs++;
6370
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04006371 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6372 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
6373 if (rc) {
6374 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
6375 vnic_id, rc);
6376 goto vnic_setup_err;
6377 }
6378 bp->rsscos_nr_ctxs++;
6379 }
6380
Michael Chanae10ae72016-12-29 12:13:38 -05006381skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04006382 /* configure default vnic, ring grp */
6383 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
6384 if (rc) {
6385 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
6386 vnic_id, rc);
6387 goto vnic_setup_err;
6388 }
6389
6390 /* Enable RSS hashing on vnic */
6391 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
6392 if (rc) {
6393 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
6394 vnic_id, rc);
6395 goto vnic_setup_err;
6396 }
6397
6398 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6399 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
6400 if (rc) {
6401 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
6402 vnic_id, rc);
6403 }
6404 }
6405
6406vnic_setup_err:
6407 return rc;
6408}
6409
6410static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
6411{
6412#ifdef CONFIG_RFS_ACCEL
6413 int i, rc = 0;
6414
6415 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05006416 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04006417 u16 vnic_id = i + 1;
6418 u16 ring_id = i;
6419
6420 if (vnic_id >= bp->nr_vnics)
6421 break;
6422
Michael Chanae10ae72016-12-29 12:13:38 -05006423 vnic = &bp->vnic_info[vnic_id];
6424 vnic->flags |= BNXT_VNIC_RFS_FLAG;
6425 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6426 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05006427 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04006428 if (rc) {
6429 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
6430 vnic_id, rc);
6431 break;
6432 }
6433 rc = bnxt_setup_vnic(bp, vnic_id);
6434 if (rc)
6435 break;
6436 }
6437 return rc;
6438#else
6439 return 0;
6440#endif
6441}
6442
Michael Chan17c71ac2016-07-01 18:46:27 -04006443/* Allow PF and VF with default VLAN to be in promiscuous mode */
6444static bool bnxt_promisc_ok(struct bnxt *bp)
6445{
6446#ifdef CONFIG_BNXT_SRIOV
6447 if (BNXT_VF(bp) && !bp->vf.vlan)
6448 return false;
6449#endif
6450 return true;
6451}
6452
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04006453static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
6454{
6455 unsigned int rc = 0;
6456
6457 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
6458 if (rc) {
6459 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
6460 rc);
6461 return rc;
6462 }
6463
6464 rc = bnxt_hwrm_vnic_cfg(bp, 1);
6465 if (rc) {
6466 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
6467 rc);
6468 return rc;
6469 }
6470 return rc;
6471}
6472
Michael Chanb664f002015-12-02 01:54:08 -05006473static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04006474static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05006475
Michael Chanc0c050c2015-10-22 16:01:17 -04006476static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
6477{
Michael Chan7d2837d2016-05-04 16:56:44 -04006478 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04006479 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006480 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006481
6482 if (irq_re_init) {
6483 rc = bnxt_hwrm_stat_ctx_alloc(bp);
6484 if (rc) {
6485 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
6486 rc);
6487 goto err_out;
6488 }
6489 }
6490
6491 rc = bnxt_hwrm_ring_alloc(bp);
6492 if (rc) {
6493 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
6494 goto err_out;
6495 }
6496
6497 rc = bnxt_hwrm_ring_grp_alloc(bp);
6498 if (rc) {
6499 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
6500 goto err_out;
6501 }
6502
Prashant Sreedharan76595192016-07-18 07:15:22 -04006503 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6504 rx_nr_rings--;
6505
Michael Chanc0c050c2015-10-22 16:01:17 -04006506 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04006507 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04006508 if (rc) {
6509 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
6510 goto err_out;
6511 }
6512
6513 rc = bnxt_setup_vnic(bp, 0);
6514 if (rc)
6515 goto err_out;
6516
6517 if (bp->flags & BNXT_FLAG_RFS) {
6518 rc = bnxt_alloc_rfs_vnics(bp);
6519 if (rc)
6520 goto err_out;
6521 }
6522
6523 if (bp->flags & BNXT_FLAG_TPA) {
6524 rc = bnxt_set_tpa(bp, true);
6525 if (rc)
6526 goto err_out;
6527 }
6528
6529 if (BNXT_VF(bp))
6530 bnxt_update_vf_mac(bp);
6531
6532 /* Filter for default vnic 0 */
6533 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
6534 if (rc) {
6535 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
6536 goto err_out;
6537 }
Michael Chan7d2837d2016-05-04 16:56:44 -04006538 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04006539
Michael Chan30e33842018-07-09 02:24:50 -04006540 vnic->rx_mask = 0;
6541 if (bp->dev->flags & IFF_BROADCAST)
6542 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04006543
Michael Chan17c71ac2016-07-01 18:46:27 -04006544 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04006545 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6546
6547 if (bp->dev->flags & IFF_ALLMULTI) {
6548 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6549 vnic->mc_list_count = 0;
6550 } else {
6551 u32 mask = 0;
6552
6553 bnxt_mc_list_updated(bp, &mask);
6554 vnic->rx_mask |= mask;
6555 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006556
Michael Chanb664f002015-12-02 01:54:08 -05006557 rc = bnxt_cfg_rx_mode(bp);
6558 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006559 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04006560
6561 rc = bnxt_hwrm_set_coal(bp);
6562 if (rc)
6563 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04006564 rc);
6565
6566 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6567 rc = bnxt_setup_nitroa0_vnic(bp);
6568 if (rc)
6569 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
6570 rc);
6571 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006572
Michael Chancf6645f2016-06-13 02:25:28 -04006573 if (BNXT_VF(bp)) {
6574 bnxt_hwrm_func_qcfg(bp);
6575 netdev_update_features(bp->dev);
6576 }
6577
Michael Chanc0c050c2015-10-22 16:01:17 -04006578 return 0;
6579
6580err_out:
6581 bnxt_hwrm_resource_free(bp, 0, true);
6582
6583 return rc;
6584}
6585
6586static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
6587{
6588 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
6589 return 0;
6590}
6591
6592static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
6593{
Sankar Patchineelam22479252017-03-28 19:47:29 -04006594 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006595 bnxt_init_rx_rings(bp);
6596 bnxt_init_tx_rings(bp);
6597 bnxt_init_ring_grps(bp, irq_re_init);
6598 bnxt_init_vnics(bp);
6599
6600 return bnxt_init_chip(bp, irq_re_init);
6601}
6602
Michael Chanc0c050c2015-10-22 16:01:17 -04006603static int bnxt_set_real_num_queues(struct bnxt *bp)
6604{
6605 int rc;
6606 struct net_device *dev = bp->dev;
6607
Michael Chan5f449242017-02-06 16:55:40 -05006608 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
6609 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006610 if (rc)
6611 return rc;
6612
6613 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
6614 if (rc)
6615 return rc;
6616
6617#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05006618 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04006619 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04006620#endif
6621
6622 return rc;
6623}
6624
Michael Chan6e6c5a52016-01-02 23:45:02 -05006625static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6626 bool shared)
6627{
6628 int _rx = *rx, _tx = *tx;
6629
6630 if (shared) {
6631 *rx = min_t(int, _rx, max);
6632 *tx = min_t(int, _tx, max);
6633 } else {
6634 if (max < 2)
6635 return -ENOMEM;
6636
6637 while (_rx + _tx > max) {
6638 if (_rx > _tx && _rx > 1)
6639 _rx--;
6640 else if (_tx > 1)
6641 _tx--;
6642 }
6643 *rx = _rx;
6644 *tx = _tx;
6645 }
6646 return 0;
6647}
6648
Michael Chan78095922016-12-07 00:26:16 -05006649static void bnxt_setup_msix(struct bnxt *bp)
6650{
6651 const int len = sizeof(bp->irq_tbl[0].name);
6652 struct net_device *dev = bp->dev;
6653 int tcs, i;
6654
6655 tcs = netdev_get_num_tc(dev);
6656 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05006657 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05006658
Michael Chand1e79252017-02-06 16:55:38 -05006659 for (i = 0; i < tcs; i++) {
6660 count = bp->tx_nr_rings_per_tc;
6661 off = i * count;
6662 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05006663 }
6664 }
6665
6666 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04006667 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
Michael Chan78095922016-12-07 00:26:16 -05006668 char *attr;
6669
6670 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6671 attr = "TxRx";
6672 else if (i < bp->rx_nr_rings)
6673 attr = "rx";
6674 else
6675 attr = "tx";
6676
Michael Chane5811b82018-03-31 13:54:18 -04006677 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
6678 attr, i);
6679 bp->irq_tbl[map_idx].handler = bnxt_msix;
Michael Chan78095922016-12-07 00:26:16 -05006680 }
6681}
6682
6683static void bnxt_setup_inta(struct bnxt *bp)
6684{
6685 const int len = sizeof(bp->irq_tbl[0].name);
6686
6687 if (netdev_get_num_tc(bp->dev))
6688 netdev_reset_tc(bp->dev);
6689
6690 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
6691 0);
6692 bp->irq_tbl[0].handler = bnxt_inta;
6693}
6694
6695static int bnxt_setup_int_mode(struct bnxt *bp)
6696{
6697 int rc;
6698
6699 if (bp->flags & BNXT_FLAG_USING_MSIX)
6700 bnxt_setup_msix(bp);
6701 else
6702 bnxt_setup_inta(bp);
6703
6704 rc = bnxt_set_real_num_queues(bp);
6705 return rc;
6706}
6707
Michael Chanb7429952017-01-13 01:32:00 -05006708#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006709static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
6710{
Michael Chan6a4f2942018-01-17 03:21:06 -05006711 return bp->hw_resc.max_rsscos_ctxs;
Michael Chan8079e8f2016-12-29 12:13:37 -05006712}
6713
6714static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
6715{
Michael Chan6a4f2942018-01-17 03:21:06 -05006716 return bp->hw_resc.max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006717}
Michael Chanb7429952017-01-13 01:32:00 -05006718#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05006719
Michael Chane4060d32016-12-07 00:26:19 -05006720unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
6721{
Michael Chan6a4f2942018-01-17 03:21:06 -05006722 return bp->hw_resc.max_stat_ctxs;
Michael Chane4060d32016-12-07 00:26:19 -05006723}
6724
Michael Chana588e452016-12-07 00:26:21 -05006725void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
6726{
Michael Chan6a4f2942018-01-17 03:21:06 -05006727 bp->hw_resc.max_stat_ctxs = max;
Michael Chana588e452016-12-07 00:26:21 -05006728}
6729
Michael Chane4060d32016-12-07 00:26:19 -05006730unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
6731{
Michael Chan6a4f2942018-01-17 03:21:06 -05006732 return bp->hw_resc.max_cp_rings;
Michael Chane4060d32016-12-07 00:26:19 -05006733}
6734
Michael Chan00fe9c32018-09-03 04:23:19 -04006735unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
Michael Chana588e452016-12-07 00:26:21 -05006736{
Michael Chan00fe9c32018-09-03 04:23:19 -04006737 return bp->hw_resc.max_cp_rings - bnxt_get_ulp_msix_num(bp);
Michael Chana588e452016-12-07 00:26:21 -05006738}
6739
Michael Chanad95c272018-09-03 04:23:18 -04006740static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
Michael Chan78095922016-12-07 00:26:16 -05006741{
Michael Chan6a4f2942018-01-17 03:21:06 -05006742 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6743
6744 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05006745}
6746
Michael Chan30f52942018-07-09 02:24:51 -04006747static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
Michael Chan33c26572016-12-07 00:26:15 -05006748{
Michael Chan6a4f2942018-01-17 03:21:06 -05006749 bp->hw_resc.max_irqs = max_irqs;
Michael Chan33c26572016-12-07 00:26:15 -05006750}
6751
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006752int bnxt_get_avail_msix(struct bnxt *bp, int num)
6753{
6754 int max_cp = bnxt_get_max_func_cp_rings(bp);
6755 int max_irq = bnxt_get_max_func_irqs(bp);
6756 int total_req = bp->cp_nr_rings + num;
6757 int max_idx, avail_msix;
6758
6759 max_idx = min_t(int, bp->total_irqs, max_cp);
6760 avail_msix = max_idx - bp->cp_nr_rings;
Michael Chanf1ca94d2018-08-05 16:51:53 -04006761 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006762 return avail_msix;
6763
6764 if (max_irq < total_req) {
6765 num = max_irq - bp->cp_nr_rings;
6766 if (num <= 0)
6767 return 0;
6768 }
6769 return num;
6770}
6771
Michael Chan08654eb2018-03-31 13:54:17 -04006772static int bnxt_get_num_msix(struct bnxt *bp)
6773{
Michael Chanf1ca94d2018-08-05 16:51:53 -04006774 if (!BNXT_NEW_RM(bp))
Michael Chan08654eb2018-03-31 13:54:17 -04006775 return bnxt_get_max_func_irqs(bp);
6776
6777 return bnxt_cp_rings_in_use(bp);
6778}
6779
Michael Chan78095922016-12-07 00:26:16 -05006780static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006781{
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006782 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
Michael Chan78095922016-12-07 00:26:16 -05006783 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04006784
Michael Chan08654eb2018-03-31 13:54:17 -04006785 total_vecs = bnxt_get_num_msix(bp);
6786 max = bnxt_get_max_func_irqs(bp);
6787 if (total_vecs > max)
6788 total_vecs = max;
6789
Michael Chan2773dfb2018-04-26 17:44:42 -04006790 if (!total_vecs)
6791 return 0;
6792
Michael Chanc0c050c2015-10-22 16:01:17 -04006793 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
6794 if (!msix_ent)
6795 return -ENOMEM;
6796
6797 for (i = 0; i < total_vecs; i++) {
6798 msix_ent[i].entry = i;
6799 msix_ent[i].vector = 0;
6800 }
6801
Michael Chan01657bc2016-01-02 23:45:03 -05006802 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
6803 min = 2;
6804
6805 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006806 ulp_msix = bnxt_get_ulp_msix_num(bp);
6807 if (total_vecs < 0 || total_vecs < ulp_msix) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006808 rc = -ENODEV;
6809 goto msix_setup_exit;
6810 }
6811
6812 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
6813 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05006814 for (i = 0; i < total_vecs; i++)
6815 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04006816
Michael Chan78095922016-12-07 00:26:16 -05006817 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006818 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05006819 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006820 total_vecs - ulp_msix, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006821 if (rc)
6822 goto msix_setup_exit;
6823
Michael Chan78095922016-12-07 00:26:16 -05006824 bp->cp_nr_rings = (min == 1) ?
6825 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6826 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006827
Michael Chanc0c050c2015-10-22 16:01:17 -04006828 } else {
6829 rc = -ENOMEM;
6830 goto msix_setup_exit;
6831 }
6832 bp->flags |= BNXT_FLAG_USING_MSIX;
6833 kfree(msix_ent);
6834 return 0;
6835
6836msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05006837 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
6838 kfree(bp->irq_tbl);
6839 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04006840 pci_disable_msix(bp->pdev);
6841 kfree(msix_ent);
6842 return rc;
6843}
6844
Michael Chan78095922016-12-07 00:26:16 -05006845static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006846{
Michael Chanc0c050c2015-10-22 16:01:17 -04006847 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05006848 if (!bp->irq_tbl)
6849 return -ENOMEM;
6850
6851 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04006852 bp->rx_nr_rings = 1;
6853 bp->tx_nr_rings = 1;
6854 bp->cp_nr_rings = 1;
Michael Chan01657bc2016-01-02 23:45:03 -05006855 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04006856 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05006857 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006858}
6859
Michael Chan78095922016-12-07 00:26:16 -05006860static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006861{
6862 int rc = 0;
6863
6864 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05006865 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006866
Michael Chan1fa72e22016-04-25 02:30:49 -04006867 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006868 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05006869 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006870 }
6871 return rc;
6872}
6873
Michael Chan78095922016-12-07 00:26:16 -05006874static void bnxt_clear_int_mode(struct bnxt *bp)
6875{
6876 if (bp->flags & BNXT_FLAG_USING_MSIX)
6877 pci_disable_msix(bp->pdev);
6878
6879 kfree(bp->irq_tbl);
6880 bp->irq_tbl = NULL;
6881 bp->flags &= ~BNXT_FLAG_USING_MSIX;
6882}
6883
Michael Chanfbcfc8e2018-03-31 13:54:20 -04006884int bnxt_reserve_rings(struct bnxt *bp)
Michael Chan674f50a2018-01-17 03:21:09 -05006885{
Michael Chan674f50a2018-01-17 03:21:09 -05006886 int tcs = netdev_get_num_tc(bp->dev);
6887 int rc;
6888
6889 if (!bnxt_need_reserve_rings(bp))
6890 return 0;
6891
6892 rc = __bnxt_reserve_rings(bp);
6893 if (rc) {
6894 netdev_err(bp->dev, "ring reservation failure rc: %d\n", rc);
6895 return rc;
6896 }
Michael Chanf1ca94d2018-08-05 16:51:53 -04006897 if (BNXT_NEW_RM(bp) && (bnxt_get_num_msix(bp) != bp->total_irqs)) {
Michael Chanec86f142018-03-31 13:54:21 -04006898 bnxt_ulp_irq_stop(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05006899 bnxt_clear_int_mode(bp);
6900 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -04006901 bnxt_ulp_irq_restart(bp, rc);
Michael Chan674f50a2018-01-17 03:21:09 -05006902 if (rc)
6903 return rc;
6904 }
6905 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
6906 netdev_err(bp->dev, "tx ring reservation failure\n");
6907 netdev_reset_tc(bp->dev);
6908 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
6909 return -ENOMEM;
6910 }
6911 bp->num_stat_ctxs = bp->cp_nr_rings;
6912 return 0;
6913}
6914
Michael Chanc0c050c2015-10-22 16:01:17 -04006915static void bnxt_free_irq(struct bnxt *bp)
6916{
6917 struct bnxt_irq *irq;
6918 int i;
6919
6920#ifdef CONFIG_RFS_ACCEL
6921 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
6922 bp->dev->rx_cpu_rmap = NULL;
6923#endif
Michael Chancb985262018-04-11 11:50:18 -04006924 if (!bp->irq_tbl || !bp->bnapi)
Michael Chanc0c050c2015-10-22 16:01:17 -04006925 return;
6926
6927 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04006928 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6929
6930 irq = &bp->irq_tbl[map_idx];
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006931 if (irq->requested) {
6932 if (irq->have_cpumask) {
6933 irq_set_affinity_hint(irq->vector, NULL);
6934 free_cpumask_var(irq->cpu_mask);
6935 irq->have_cpumask = 0;
6936 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006937 free_irq(irq->vector, bp->bnapi[i]);
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006938 }
6939
Michael Chanc0c050c2015-10-22 16:01:17 -04006940 irq->requested = 0;
6941 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006942}
6943
6944static int bnxt_request_irq(struct bnxt *bp)
6945{
Michael Chanb81a90d2016-01-02 23:45:01 -05006946 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006947 unsigned long flags = 0;
6948#ifdef CONFIG_RFS_ACCEL
Michael Chane5811b82018-03-31 13:54:18 -04006949 struct cpu_rmap *rmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04006950#endif
6951
Michael Chane5811b82018-03-31 13:54:18 -04006952 rc = bnxt_setup_int_mode(bp);
6953 if (rc) {
6954 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6955 rc);
6956 return rc;
6957 }
6958#ifdef CONFIG_RFS_ACCEL
6959 rmap = bp->dev->rx_cpu_rmap;
6960#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006961 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
6962 flags = IRQF_SHARED;
6963
Michael Chanb81a90d2016-01-02 23:45:01 -05006964 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04006965 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6966 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
6967
Michael Chanc0c050c2015-10-22 16:01:17 -04006968#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05006969 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006970 rc = irq_cpu_rmap_add(rmap, irq->vector);
6971 if (rc)
6972 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05006973 j);
6974 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04006975 }
6976#endif
6977 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
6978 bp->bnapi[i]);
6979 if (rc)
6980 break;
6981
6982 irq->requested = 1;
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04006983
6984 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
6985 int numa_node = dev_to_node(&bp->pdev->dev);
6986
6987 irq->have_cpumask = 1;
6988 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
6989 irq->cpu_mask);
6990 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
6991 if (rc) {
6992 netdev_warn(bp->dev,
6993 "Set affinity failed, IRQ = %d\n",
6994 irq->vector);
6995 break;
6996 }
6997 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006998 }
6999 return rc;
7000}
7001
7002static void bnxt_del_napi(struct bnxt *bp)
7003{
7004 int i;
7005
7006 if (!bp->bnapi)
7007 return;
7008
7009 for (i = 0; i < bp->cp_nr_rings; i++) {
7010 struct bnxt_napi *bnapi = bp->bnapi[i];
7011
7012 napi_hash_del(&bnapi->napi);
7013 netif_napi_del(&bnapi->napi);
7014 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08007015 /* We called napi_hash_del() before netif_napi_del(), we need
7016 * to respect an RCU grace period before freeing napi structures.
7017 */
7018 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04007019}
7020
7021static void bnxt_init_napi(struct bnxt *bp)
7022{
7023 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007024 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007025 struct bnxt_napi *bnapi;
7026
7027 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007028 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7029 cp_nr_rings--;
7030 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007031 bnapi = bp->bnapi[i];
7032 netif_napi_add(bp->dev, &bnapi->napi,
7033 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04007034 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007035 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7036 bnapi = bp->bnapi[cp_nr_rings];
7037 netif_napi_add(bp->dev, &bnapi->napi,
7038 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007039 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007040 } else {
7041 bnapi = bp->bnapi[0];
7042 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04007043 }
7044}
7045
7046static void bnxt_disable_napi(struct bnxt *bp)
7047{
7048 int i;
7049
7050 if (!bp->bnapi)
7051 return;
7052
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05007053 for (i = 0; i < bp->cp_nr_rings; i++) {
7054 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
7055
7056 if (bp->bnapi[i]->rx_ring)
7057 cancel_work_sync(&cpr->dim.work);
7058
Michael Chanc0c050c2015-10-22 16:01:17 -04007059 napi_disable(&bp->bnapi[i]->napi);
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05007060 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007061}
7062
7063static void bnxt_enable_napi(struct bnxt *bp)
7064{
7065 int i;
7066
7067 for (i = 0; i < bp->cp_nr_rings; i++) {
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05007068 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
Michael Chanfa7e2812016-05-10 19:18:00 -04007069 bp->bnapi[i]->in_reset = false;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05007070
7071 if (bp->bnapi[i]->rx_ring) {
7072 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
7073 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
7074 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007075 napi_enable(&bp->bnapi[i]->napi);
7076 }
7077}
7078
Michael Chan7df4ae92016-12-02 21:17:17 -05007079void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007080{
7081 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04007082 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04007083
Michael Chanb6ab4b02016-01-02 23:44:59 -05007084 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007085 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05007086 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007087 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04007088 }
7089 }
7090 /* Stop all TX queues */
7091 netif_tx_disable(bp->dev);
7092 netif_carrier_off(bp->dev);
7093}
7094
Michael Chan7df4ae92016-12-02 21:17:17 -05007095void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007096{
7097 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04007098 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04007099
7100 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05007101 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007102 txr->dev_state = 0;
7103 }
7104 netif_tx_wake_all_queues(bp->dev);
7105 if (bp->link_info.link_up)
7106 netif_carrier_on(bp->dev);
7107}
7108
7109static void bnxt_report_link(struct bnxt *bp)
7110{
7111 if (bp->link_info.link_up) {
7112 const char *duplex;
7113 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04007114 u32 speed;
7115 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04007116
7117 netif_carrier_on(bp->dev);
7118 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
7119 duplex = "full";
7120 else
7121 duplex = "half";
7122 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
7123 flow_ctrl = "ON - receive & transmit";
7124 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
7125 flow_ctrl = "ON - transmit";
7126 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
7127 flow_ctrl = "ON - receive";
7128 else
7129 flow_ctrl = "none";
7130 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04007131 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007132 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04007133 if (bp->flags & BNXT_FLAG_EEE_CAP)
7134 netdev_info(bp->dev, "EEE is %s\n",
7135 bp->eee.eee_active ? "active" :
7136 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05007137 fec = bp->link_info.fec_cfg;
7138 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
7139 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
7140 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
7141 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
7142 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04007143 } else {
7144 netif_carrier_off(bp->dev);
7145 netdev_err(bp->dev, "NIC Link is Down\n");
7146 }
7147}
7148
Michael Chan170ce012016-04-05 14:08:57 -04007149static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
7150{
7151 int rc = 0;
7152 struct hwrm_port_phy_qcaps_input req = {0};
7153 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04007154 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04007155
7156 if (bp->hwrm_spec_code < 0x10201)
7157 return 0;
7158
7159 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
7160
7161 mutex_lock(&bp->hwrm_cmd_lock);
7162 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7163 if (rc)
7164 goto hwrm_phy_qcaps_exit;
7165
Michael Chanacb20052017-07-24 12:34:20 -04007166 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04007167 struct ethtool_eee *eee = &bp->eee;
7168 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
7169
7170 bp->flags |= BNXT_FLAG_EEE_CAP;
7171 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7172 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
7173 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
7174 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
7175 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
7176 }
Michael Chan55fd0cf2018-08-05 16:51:48 -04007177 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
7178 if (bp->test_info)
7179 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
7180 }
Michael Chan520ad892017-03-08 18:44:35 -05007181 if (resp->supported_speeds_auto_mode)
7182 link_info->support_auto_speeds =
7183 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04007184
Michael Chand5430d32017-08-28 13:40:31 -04007185 bp->port_count = resp->port_cnt;
7186
Michael Chan170ce012016-04-05 14:08:57 -04007187hwrm_phy_qcaps_exit:
7188 mutex_unlock(&bp->hwrm_cmd_lock);
7189 return rc;
7190}
7191
Michael Chanc0c050c2015-10-22 16:01:17 -04007192static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
7193{
7194 int rc = 0;
7195 struct bnxt_link_info *link_info = &bp->link_info;
7196 struct hwrm_port_phy_qcfg_input req = {0};
7197 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7198 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05007199 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04007200
7201 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
7202
7203 mutex_lock(&bp->hwrm_cmd_lock);
7204 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7205 if (rc) {
7206 mutex_unlock(&bp->hwrm_cmd_lock);
7207 return rc;
7208 }
7209
7210 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
7211 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04007212 link_info->duplex = resp->duplex_cfg;
7213 if (bp->hwrm_spec_code >= 0x10800)
7214 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04007215 link_info->pause = resp->pause;
7216 link_info->auto_mode = resp->auto_mode;
7217 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05007218 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04007219 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04007220 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04007221 if (link_info->phy_link_status == BNXT_LINK_LINK)
7222 link_info->link_speed = le16_to_cpu(resp->link_speed);
7223 else
7224 link_info->link_speed = 0;
7225 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04007226 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
7227 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05007228 link_info->lp_auto_link_speeds =
7229 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04007230 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
7231 link_info->phy_ver[0] = resp->phy_maj;
7232 link_info->phy_ver[1] = resp->phy_min;
7233 link_info->phy_ver[2] = resp->phy_bld;
7234 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04007235 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04007236 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04007237 link_info->phy_addr = resp->eee_config_phy_addr &
7238 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04007239 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04007240
Michael Chan170ce012016-04-05 14:08:57 -04007241 if (bp->flags & BNXT_FLAG_EEE_CAP) {
7242 struct ethtool_eee *eee = &bp->eee;
7243 u16 fw_speeds;
7244
7245 eee->eee_active = 0;
7246 if (resp->eee_config_phy_addr &
7247 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
7248 eee->eee_active = 1;
7249 fw_speeds = le16_to_cpu(
7250 resp->link_partner_adv_eee_link_speed_mask);
7251 eee->lp_advertised =
7252 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7253 }
7254
7255 /* Pull initial EEE config */
7256 if (!chng_link_state) {
7257 if (resp->eee_config_phy_addr &
7258 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
7259 eee->eee_enabled = 1;
7260
7261 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
7262 eee->advertised =
7263 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7264
7265 if (resp->eee_config_phy_addr &
7266 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
7267 __le32 tmr;
7268
7269 eee->tx_lpi_enabled = 1;
7270 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
7271 eee->tx_lpi_timer = le32_to_cpu(tmr) &
7272 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
7273 }
7274 }
7275 }
Michael Chane70c7522017-02-12 19:18:16 -05007276
7277 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
7278 if (bp->hwrm_spec_code >= 0x10504)
7279 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
7280
Michael Chanc0c050c2015-10-22 16:01:17 -04007281 /* TODO: need to add more logic to report VF link */
7282 if (chng_link_state) {
7283 if (link_info->phy_link_status == BNXT_LINK_LINK)
7284 link_info->link_up = 1;
7285 else
7286 link_info->link_up = 0;
7287 if (link_up != link_info->link_up)
7288 bnxt_report_link(bp);
7289 } else {
7290 /* alwasy link down if not require to update link state */
7291 link_info->link_up = 0;
7292 }
7293 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05007294
Michael Chandac049072018-05-08 03:18:39 -04007295 if (!BNXT_SINGLE_PF(bp))
7296 return 0;
7297
Michael Chan286ef9d2016-11-16 21:13:08 -05007298 diff = link_info->support_auto_speeds ^ link_info->advertising;
7299 if ((link_info->support_auto_speeds | diff) !=
7300 link_info->support_auto_speeds) {
7301 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05007302 * update the advertisement settings. Caller holds RTNL
7303 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05007304 */
Michael Chan286ef9d2016-11-16 21:13:08 -05007305 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05007306 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05007307 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05007308 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007309 return 0;
7310}
7311
Michael Chan10289be2016-05-15 03:04:49 -04007312static void bnxt_get_port_module_status(struct bnxt *bp)
7313{
7314 struct bnxt_link_info *link_info = &bp->link_info;
7315 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
7316 u8 module_status;
7317
7318 if (bnxt_update_link(bp, true))
7319 return;
7320
7321 module_status = link_info->module_status;
7322 switch (module_status) {
7323 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
7324 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
7325 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
7326 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
7327 bp->pf.port_id);
7328 if (bp->hwrm_spec_code >= 0x10201) {
7329 netdev_warn(bp->dev, "Module part number %s\n",
7330 resp->phy_vendor_partnumber);
7331 }
7332 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
7333 netdev_warn(bp->dev, "TX is disabled\n");
7334 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
7335 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
7336 }
7337}
7338
Michael Chanc0c050c2015-10-22 16:01:17 -04007339static void
7340bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
7341{
7342 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007343 if (bp->hwrm_spec_code >= 0x10201)
7344 req->auto_pause =
7345 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04007346 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7347 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
7348 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04007349 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04007350 req->enables |=
7351 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7352 } else {
7353 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7354 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
7355 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
7356 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
7357 req->enables |=
7358 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04007359 if (bp->hwrm_spec_code >= 0x10201) {
7360 req->auto_pause = req->force_pause;
7361 req->enables |= cpu_to_le32(
7362 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7363 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007364 }
7365}
7366
7367static void bnxt_hwrm_set_link_common(struct bnxt *bp,
7368 struct hwrm_port_phy_cfg_input *req)
7369{
7370 u8 autoneg = bp->link_info.autoneg;
7371 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05007372 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04007373
7374 if (autoneg & BNXT_AUTONEG_SPEED) {
7375 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04007376 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04007377
7378 req->enables |= cpu_to_le32(
7379 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
7380 req->auto_link_speed_mask = cpu_to_le16(advertising);
7381
7382 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
7383 req->flags |=
7384 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
7385 } else {
7386 req->force_link_speed = cpu_to_le16(fw_link_speed);
7387 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
7388 }
7389
Michael Chanc0c050c2015-10-22 16:01:17 -04007390 /* tell chimp that the setting takes effect immediately */
7391 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
7392}
7393
7394int bnxt_hwrm_set_pause(struct bnxt *bp)
7395{
7396 struct hwrm_port_phy_cfg_input req = {0};
7397 int rc;
7398
7399 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
7400 bnxt_hwrm_set_pause_common(bp, &req);
7401
7402 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
7403 bp->link_info.force_link_chng)
7404 bnxt_hwrm_set_link_common(bp, &req);
7405
7406 mutex_lock(&bp->hwrm_cmd_lock);
7407 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7408 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
7409 /* since changing of pause setting doesn't trigger any link
7410 * change event, the driver needs to update the current pause
7411 * result upon successfully return of the phy_cfg command
7412 */
7413 bp->link_info.pause =
7414 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
7415 bp->link_info.auto_pause_setting = 0;
7416 if (!bp->link_info.force_link_chng)
7417 bnxt_report_link(bp);
7418 }
7419 bp->link_info.force_link_chng = false;
7420 mutex_unlock(&bp->hwrm_cmd_lock);
7421 return rc;
7422}
7423
Michael Chan939f7f02016-04-05 14:08:58 -04007424static void bnxt_hwrm_set_eee(struct bnxt *bp,
7425 struct hwrm_port_phy_cfg_input *req)
7426{
7427 struct ethtool_eee *eee = &bp->eee;
7428
7429 if (eee->eee_enabled) {
7430 u16 eee_speeds;
7431 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
7432
7433 if (eee->tx_lpi_enabled)
7434 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
7435 else
7436 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
7437
7438 req->flags |= cpu_to_le32(flags);
7439 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
7440 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
7441 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
7442 } else {
7443 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
7444 }
7445}
7446
7447int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04007448{
7449 struct hwrm_port_phy_cfg_input req = {0};
7450
7451 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
7452 if (set_pause)
7453 bnxt_hwrm_set_pause_common(bp, &req);
7454
7455 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04007456
7457 if (set_eee)
7458 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04007459 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7460}
7461
Michael Chan33f7d552016-04-11 04:11:12 -04007462static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
7463{
7464 struct hwrm_port_phy_cfg_input req = {0};
7465
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007466 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04007467 return 0;
7468
7469 if (pci_num_vf(bp->pdev))
7470 return 0;
7471
7472 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05007473 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04007474 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7475}
7476
Michael Chan25e1acd2018-08-05 16:51:55 -04007477static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
7478{
7479 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
7480 struct hwrm_func_drv_if_change_input req = {0};
7481 bool resc_reinit = false;
7482 int rc;
7483
7484 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
7485 return 0;
7486
7487 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
7488 if (up)
7489 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
7490 mutex_lock(&bp->hwrm_cmd_lock);
7491 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7492 if (!rc && (resp->flags &
7493 cpu_to_le32(FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)))
7494 resc_reinit = true;
7495 mutex_unlock(&bp->hwrm_cmd_lock);
7496
7497 if (up && resc_reinit && BNXT_NEW_RM(bp)) {
7498 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7499
7500 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7501 hw_resc->resv_cp_rings = 0;
7502 hw_resc->resv_tx_rings = 0;
7503 hw_resc->resv_rx_rings = 0;
7504 hw_resc->resv_hw_ring_grps = 0;
7505 hw_resc->resv_vnics = 0;
Michael Chan6b95c3e2018-09-03 04:23:17 -04007506 bp->tx_nr_rings = 0;
7507 bp->rx_nr_rings = 0;
Michael Chan25e1acd2018-08-05 16:51:55 -04007508 }
7509 return rc;
7510}
7511
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007512static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
7513{
7514 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7515 struct hwrm_port_led_qcaps_input req = {0};
7516 struct bnxt_pf_info *pf = &bp->pf;
7517 int rc;
7518
7519 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
7520 return 0;
7521
7522 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
7523 req.port_id = cpu_to_le16(pf->port_id);
7524 mutex_lock(&bp->hwrm_cmd_lock);
7525 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7526 if (rc) {
7527 mutex_unlock(&bp->hwrm_cmd_lock);
7528 return rc;
7529 }
7530 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
7531 int i;
7532
7533 bp->num_leds = resp->num_leds;
7534 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
7535 bp->num_leds);
7536 for (i = 0; i < bp->num_leds; i++) {
7537 struct bnxt_led_info *led = &bp->leds[i];
7538 __le16 caps = led->led_state_caps;
7539
7540 if (!led->led_group_id ||
7541 !BNXT_LED_ALT_BLINK_CAP(caps)) {
7542 bp->num_leds = 0;
7543 break;
7544 }
7545 }
7546 }
7547 mutex_unlock(&bp->hwrm_cmd_lock);
7548 return 0;
7549}
7550
Michael Chan5282db62017-04-04 18:14:10 -04007551int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
7552{
7553 struct hwrm_wol_filter_alloc_input req = {0};
7554 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
7555 int rc;
7556
7557 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
7558 req.port_id = cpu_to_le16(bp->pf.port_id);
7559 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
7560 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
7561 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
7562 mutex_lock(&bp->hwrm_cmd_lock);
7563 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7564 if (!rc)
7565 bp->wol_filter_id = resp->wol_filter_id;
7566 mutex_unlock(&bp->hwrm_cmd_lock);
7567 return rc;
7568}
7569
7570int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
7571{
7572 struct hwrm_wol_filter_free_input req = {0};
7573 int rc;
7574
7575 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
7576 req.port_id = cpu_to_le16(bp->pf.port_id);
7577 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
7578 req.wol_filter_id = bp->wol_filter_id;
7579 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7580 return rc;
7581}
7582
Michael Chanc1ef1462017-04-04 18:14:07 -04007583static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
7584{
7585 struct hwrm_wol_filter_qcfg_input req = {0};
7586 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7587 u16 next_handle = 0;
7588 int rc;
7589
7590 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
7591 req.port_id = cpu_to_le16(bp->pf.port_id);
7592 req.handle = cpu_to_le16(handle);
7593 mutex_lock(&bp->hwrm_cmd_lock);
7594 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7595 if (!rc) {
7596 next_handle = le16_to_cpu(resp->next_handle);
7597 if (next_handle != 0) {
7598 if (resp->wol_type ==
7599 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
7600 bp->wol = 1;
7601 bp->wol_filter_id = resp->wol_filter_id;
7602 }
7603 }
7604 }
7605 mutex_unlock(&bp->hwrm_cmd_lock);
7606 return next_handle;
7607}
7608
7609static void bnxt_get_wol_settings(struct bnxt *bp)
7610{
7611 u16 handle = 0;
7612
7613 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
7614 return;
7615
7616 do {
7617 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
7618 } while (handle && handle != 0xffff);
7619}
7620
Vasundhara Volamcde49a42018-08-05 16:51:56 -04007621#ifdef CONFIG_BNXT_HWMON
7622static ssize_t bnxt_show_temp(struct device *dev,
7623 struct device_attribute *devattr, char *buf)
7624{
7625 struct hwrm_temp_monitor_query_input req = {0};
7626 struct hwrm_temp_monitor_query_output *resp;
7627 struct bnxt *bp = dev_get_drvdata(dev);
7628 u32 temp = 0;
7629
7630 resp = bp->hwrm_cmd_resp_addr;
7631 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
7632 mutex_lock(&bp->hwrm_cmd_lock);
7633 if (!_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
7634 temp = resp->temp * 1000; /* display millidegree */
7635 mutex_unlock(&bp->hwrm_cmd_lock);
7636
7637 return sprintf(buf, "%u\n", temp);
7638}
7639static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
7640
7641static struct attribute *bnxt_attrs[] = {
7642 &sensor_dev_attr_temp1_input.dev_attr.attr,
7643 NULL
7644};
7645ATTRIBUTE_GROUPS(bnxt);
7646
7647static void bnxt_hwmon_close(struct bnxt *bp)
7648{
7649 if (bp->hwmon_dev) {
7650 hwmon_device_unregister(bp->hwmon_dev);
7651 bp->hwmon_dev = NULL;
7652 }
7653}
7654
7655static void bnxt_hwmon_open(struct bnxt *bp)
7656{
7657 struct pci_dev *pdev = bp->pdev;
7658
7659 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
7660 DRV_MODULE_NAME, bp,
7661 bnxt_groups);
7662 if (IS_ERR(bp->hwmon_dev)) {
7663 bp->hwmon_dev = NULL;
7664 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
7665 }
7666}
7667#else
7668static void bnxt_hwmon_close(struct bnxt *bp)
7669{
7670}
7671
7672static void bnxt_hwmon_open(struct bnxt *bp)
7673{
7674}
7675#endif
7676
Michael Chan939f7f02016-04-05 14:08:58 -04007677static bool bnxt_eee_config_ok(struct bnxt *bp)
7678{
7679 struct ethtool_eee *eee = &bp->eee;
7680 struct bnxt_link_info *link_info = &bp->link_info;
7681
7682 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
7683 return true;
7684
7685 if (eee->eee_enabled) {
7686 u32 advertising =
7687 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
7688
7689 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
7690 eee->eee_enabled = 0;
7691 return false;
7692 }
7693 if (eee->advertised & ~advertising) {
7694 eee->advertised = advertising & eee->supported;
7695 return false;
7696 }
7697 }
7698 return true;
7699}
7700
Michael Chanc0c050c2015-10-22 16:01:17 -04007701static int bnxt_update_phy_setting(struct bnxt *bp)
7702{
7703 int rc;
7704 bool update_link = false;
7705 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04007706 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04007707 struct bnxt_link_info *link_info = &bp->link_info;
7708
7709 rc = bnxt_update_link(bp, true);
7710 if (rc) {
7711 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
7712 rc);
7713 return rc;
7714 }
Michael Chan33dac242017-02-12 19:18:15 -05007715 if (!BNXT_SINGLE_PF(bp))
7716 return 0;
7717
Michael Chanc0c050c2015-10-22 16:01:17 -04007718 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04007719 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
7720 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04007721 update_pause = true;
7722 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
7723 link_info->force_pause_setting != link_info->req_flow_ctrl)
7724 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04007725 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
7726 if (BNXT_AUTO_MODE(link_info->auto_mode))
7727 update_link = true;
7728 if (link_info->req_link_speed != link_info->force_link_speed)
7729 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05007730 if (link_info->req_duplex != link_info->duplex_setting)
7731 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04007732 } else {
7733 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
7734 update_link = true;
7735 if (link_info->advertising != link_info->auto_link_speeds)
7736 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04007737 }
7738
Michael Chan16d663a2016-11-16 21:13:07 -05007739 /* The last close may have shutdown the link, so need to call
7740 * PHY_CFG to bring it back up.
7741 */
7742 if (!netif_carrier_ok(bp->dev))
7743 update_link = true;
7744
Michael Chan939f7f02016-04-05 14:08:58 -04007745 if (!bnxt_eee_config_ok(bp))
7746 update_eee = true;
7747
Michael Chanc0c050c2015-10-22 16:01:17 -04007748 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04007749 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04007750 else if (update_pause)
7751 rc = bnxt_hwrm_set_pause(bp);
7752 if (rc) {
7753 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
7754 rc);
7755 return rc;
7756 }
7757
7758 return rc;
7759}
7760
Jeffrey Huang11809492015-11-05 16:25:49 -05007761/* Common routine to pre-map certain register block to different GRC window.
7762 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
7763 * in PF and 3 windows in VF that can be customized to map in different
7764 * register blocks.
7765 */
7766static void bnxt_preset_reg_win(struct bnxt *bp)
7767{
7768 if (BNXT_PF(bp)) {
7769 /* CAG registers map to GRC window #4 */
7770 writel(BNXT_CAG_REG_BASE,
7771 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
7772 }
7773}
7774
Michael Chan47558ac2018-04-26 17:44:44 -04007775static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
7776
Michael Chanc0c050c2015-10-22 16:01:17 -04007777static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7778{
7779 int rc = 0;
7780
Jeffrey Huang11809492015-11-05 16:25:49 -05007781 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007782 netif_carrier_off(bp->dev);
7783 if (irq_re_init) {
Michael Chan47558ac2018-04-26 17:44:44 -04007784 /* Reserve rings now if none were reserved at driver probe. */
7785 rc = bnxt_init_dflt_ring_mode(bp);
7786 if (rc) {
7787 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
7788 return rc;
7789 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007790 }
Michael Chan41e8d792018-10-14 07:02:48 -04007791 rc = bnxt_reserve_rings(bp);
7792 if (rc)
7793 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007794 if ((bp->flags & BNXT_FLAG_RFS) &&
7795 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
7796 /* disable RFS if falling back to INTA */
7797 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
7798 bp->flags &= ~BNXT_FLAG_RFS;
7799 }
7800
7801 rc = bnxt_alloc_mem(bp, irq_re_init);
7802 if (rc) {
7803 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
7804 goto open_err_free_mem;
7805 }
7806
7807 if (irq_re_init) {
7808 bnxt_init_napi(bp);
7809 rc = bnxt_request_irq(bp);
7810 if (rc) {
7811 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
Vikas Guptac58387a2018-07-09 02:24:52 -04007812 goto open_err_irq;
Michael Chanc0c050c2015-10-22 16:01:17 -04007813 }
7814 }
7815
7816 bnxt_enable_napi(bp);
Andy Gospodarekcabfb092018-04-26 17:44:40 -04007817 bnxt_debug_dev_init(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007818
7819 rc = bnxt_init_nic(bp, irq_re_init);
7820 if (rc) {
7821 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
7822 goto open_err;
7823 }
7824
7825 if (link_re_init) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007826 mutex_lock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04007827 rc = bnxt_update_phy_setting(bp);
Michael Chane2dc9b62017-10-13 21:09:30 -04007828 mutex_unlock(&bp->link_lock);
Michael Chana1ef4a72018-08-05 16:51:49 -04007829 if (rc) {
Michael Chanba41d462016-02-19 19:43:21 -05007830 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chana1ef4a72018-08-05 16:51:49 -04007831 if (BNXT_SINGLE_PF(bp)) {
7832 bp->link_info.phy_retry = true;
7833 bp->link_info.phy_retry_expires =
7834 jiffies + 5 * HZ;
7835 }
7836 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007837 }
7838
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007839 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007840 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007841
Michael Chancaefe522015-12-09 19:35:42 -05007842 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007843 bnxt_enable_int(bp);
7844 /* Enable TX queues */
7845 bnxt_tx_enable(bp);
7846 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04007847 /* Poll link status and check for SFP+ module status */
7848 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007849
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04007850 /* VF-reps may need to be re-opened after the PF is re-opened */
7851 if (BNXT_PF(bp))
7852 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007853 return 0;
7854
7855open_err:
Andy Gospodarekcabfb092018-04-26 17:44:40 -04007856 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007857 bnxt_disable_napi(bp);
Vikas Guptac58387a2018-07-09 02:24:52 -04007858
7859open_err_irq:
Michael Chanc0c050c2015-10-22 16:01:17 -04007860 bnxt_del_napi(bp);
7861
7862open_err_free_mem:
7863 bnxt_free_skbs(bp);
7864 bnxt_free_irq(bp);
7865 bnxt_free_mem(bp, true);
7866 return rc;
7867}
7868
7869/* rtnl_lock held */
7870int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7871{
7872 int rc = 0;
7873
7874 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
7875 if (rc) {
7876 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
7877 dev_close(bp->dev);
7878 }
7879 return rc;
7880}
7881
Michael Chanf7dc1ea2017-04-04 18:14:13 -04007882/* rtnl_lock held, open the NIC half way by allocating all resources, but
7883 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
7884 * self tests.
7885 */
7886int bnxt_half_open_nic(struct bnxt *bp)
7887{
7888 int rc = 0;
7889
7890 rc = bnxt_alloc_mem(bp, false);
7891 if (rc) {
7892 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
7893 goto half_open_err;
7894 }
7895 rc = bnxt_init_nic(bp, false);
7896 if (rc) {
7897 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
7898 goto half_open_err;
7899 }
7900 return 0;
7901
7902half_open_err:
7903 bnxt_free_skbs(bp);
7904 bnxt_free_mem(bp, false);
7905 dev_close(bp->dev);
7906 return rc;
7907}
7908
7909/* rtnl_lock held, this call can only be made after a previous successful
7910 * call to bnxt_half_open_nic().
7911 */
7912void bnxt_half_close_nic(struct bnxt *bp)
7913{
7914 bnxt_hwrm_resource_free(bp, false, false);
7915 bnxt_free_skbs(bp);
7916 bnxt_free_mem(bp, false);
7917}
7918
Michael Chanc0c050c2015-10-22 16:01:17 -04007919static int bnxt_open(struct net_device *dev)
7920{
7921 struct bnxt *bp = netdev_priv(dev);
Michael Chan25e1acd2018-08-05 16:51:55 -04007922 int rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007923
Michael Chan25e1acd2018-08-05 16:51:55 -04007924 bnxt_hwrm_if_change(bp, true);
7925 rc = __bnxt_open_nic(bp, true, true);
7926 if (rc)
7927 bnxt_hwrm_if_change(bp, false);
Vasundhara Volamcde49a42018-08-05 16:51:56 -04007928
7929 bnxt_hwmon_open(bp);
7930
Michael Chan25e1acd2018-08-05 16:51:55 -04007931 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007932}
7933
Michael Chanf9b76eb2017-07-11 13:05:34 -04007934static bool bnxt_drv_busy(struct bnxt *bp)
7935{
7936 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
7937 test_bit(BNXT_STATE_READ_STATS, &bp->state));
7938}
7939
Michael Chan86e953d2018-01-17 03:21:04 -05007940static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
7941 bool link_re_init)
Michael Chanc0c050c2015-10-22 16:01:17 -04007942{
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04007943 /* Close the VF-reps before closing PF */
7944 if (BNXT_PF(bp))
7945 bnxt_vf_reps_close(bp);
Michael Chan86e953d2018-01-17 03:21:04 -05007946
Michael Chanc0c050c2015-10-22 16:01:17 -04007947 /* Change device state to avoid TX queue wake up's */
7948 bnxt_tx_disable(bp);
7949
Michael Chancaefe522015-12-09 19:35:42 -05007950 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05007951 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04007952 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05007953 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04007954
Michael Chan9d8bc092016-12-29 12:13:33 -05007955 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04007956 bnxt_shutdown_nic(bp, irq_re_init);
7957
7958 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
7959
Andy Gospodarekcabfb092018-04-26 17:44:40 -04007960 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007961 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007962 del_timer_sync(&bp->timer);
7963 bnxt_free_skbs(bp);
7964
7965 if (irq_re_init) {
7966 bnxt_free_irq(bp);
7967 bnxt_del_napi(bp);
7968 }
7969 bnxt_free_mem(bp, irq_re_init);
Michael Chan86e953d2018-01-17 03:21:04 -05007970}
7971
7972int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7973{
7974 int rc = 0;
7975
7976#ifdef CONFIG_BNXT_SRIOV
7977 if (bp->sriov_cfg) {
7978 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
7979 !bp->sriov_cfg,
7980 BNXT_SRIOV_CFG_WAIT_TMO);
7981 if (rc)
7982 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
7983 }
7984#endif
7985 __bnxt_close_nic(bp, irq_re_init, link_re_init);
Michael Chanc0c050c2015-10-22 16:01:17 -04007986 return rc;
7987}
7988
7989static int bnxt_close(struct net_device *dev)
7990{
7991 struct bnxt *bp = netdev_priv(dev);
7992
Vasundhara Volamcde49a42018-08-05 16:51:56 -04007993 bnxt_hwmon_close(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007994 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04007995 bnxt_hwrm_shutdown_link(bp);
Michael Chan25e1acd2018-08-05 16:51:55 -04007996 bnxt_hwrm_if_change(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007997 return 0;
7998}
7999
8000/* rtnl_lock held */
8001static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
8002{
8003 switch (cmd) {
8004 case SIOCGMIIPHY:
8005 /* fallthru */
8006 case SIOCGMIIREG: {
8007 if (!netif_running(dev))
8008 return -EAGAIN;
8009
8010 return 0;
8011 }
8012
8013 case SIOCSMIIREG:
8014 if (!netif_running(dev))
8015 return -EAGAIN;
8016
8017 return 0;
8018
8019 default:
8020 /* do nothing */
8021 break;
8022 }
8023 return -EOPNOTSUPP;
8024}
8025
stephen hemmingerbc1f4472017-01-06 19:12:52 -08008026static void
Michael Chanc0c050c2015-10-22 16:01:17 -04008027bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
8028{
8029 u32 i;
8030 struct bnxt *bp = netdev_priv(dev);
8031
Michael Chanf9b76eb2017-07-11 13:05:34 -04008032 set_bit(BNXT_STATE_READ_STATS, &bp->state);
8033 /* Make sure bnxt_close_nic() sees that we are reading stats before
8034 * we check the BNXT_STATE_OPEN flag.
8035 */
8036 smp_mb__after_atomic();
8037 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
8038 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08008039 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04008040 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008041
8042 /* TODO check if we need to synchronize with bnxt_close path */
8043 for (i = 0; i < bp->cp_nr_rings; i++) {
8044 struct bnxt_napi *bnapi = bp->bnapi[i];
8045 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8046 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
8047
8048 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
8049 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
8050 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
8051
8052 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
8053 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
8054 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
8055
8056 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
8057 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
8058 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
8059
8060 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
8061 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
8062 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
8063
8064 stats->rx_missed_errors +=
8065 le64_to_cpu(hw_stats->rx_discard_pkts);
8066
8067 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
8068
Michael Chanc0c050c2015-10-22 16:01:17 -04008069 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
8070 }
8071
Michael Chan9947f832016-03-07 15:38:46 -05008072 if (bp->flags & BNXT_FLAG_PORT_STATS) {
8073 struct rx_port_stats *rx = bp->hw_rx_port_stats;
8074 struct tx_port_stats *tx = bp->hw_tx_port_stats;
8075
8076 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
8077 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
8078 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
8079 le64_to_cpu(rx->rx_ovrsz_frames) +
8080 le64_to_cpu(rx->rx_runt_frames);
8081 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
8082 le64_to_cpu(rx->rx_jbr_frames);
8083 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
8084 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
8085 stats->tx_errors = le64_to_cpu(tx->tx_err);
8086 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04008087 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04008088}
8089
8090static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
8091{
8092 struct net_device *dev = bp->dev;
8093 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8094 struct netdev_hw_addr *ha;
8095 u8 *haddr;
8096 int mc_count = 0;
8097 bool update = false;
8098 int off = 0;
8099
8100 netdev_for_each_mc_addr(ha, dev) {
8101 if (mc_count >= BNXT_MAX_MC_ADDRS) {
8102 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8103 vnic->mc_list_count = 0;
8104 return false;
8105 }
8106 haddr = ha->addr;
8107 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
8108 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
8109 update = true;
8110 }
8111 off += ETH_ALEN;
8112 mc_count++;
8113 }
8114 if (mc_count)
8115 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
8116
8117 if (mc_count != vnic->mc_list_count) {
8118 vnic->mc_list_count = mc_count;
8119 update = true;
8120 }
8121 return update;
8122}
8123
8124static bool bnxt_uc_list_updated(struct bnxt *bp)
8125{
8126 struct net_device *dev = bp->dev;
8127 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8128 struct netdev_hw_addr *ha;
8129 int off = 0;
8130
8131 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
8132 return true;
8133
8134 netdev_for_each_uc_addr(ha, dev) {
8135 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
8136 return true;
8137
8138 off += ETH_ALEN;
8139 }
8140 return false;
8141}
8142
8143static void bnxt_set_rx_mode(struct net_device *dev)
8144{
8145 struct bnxt *bp = netdev_priv(dev);
8146 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8147 u32 mask = vnic->rx_mask;
8148 bool mc_update = false;
8149 bool uc_update;
8150
8151 if (!netif_running(dev))
8152 return;
8153
8154 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
8155 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
Michael Chan30e33842018-07-09 02:24:50 -04008156 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
8157 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
Michael Chanc0c050c2015-10-22 16:01:17 -04008158
Michael Chan17c71ac2016-07-01 18:46:27 -04008159 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04008160 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8161
8162 uc_update = bnxt_uc_list_updated(bp);
8163
Michael Chan30e33842018-07-09 02:24:50 -04008164 if (dev->flags & IFF_BROADCAST)
8165 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04008166 if (dev->flags & IFF_ALLMULTI) {
8167 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8168 vnic->mc_list_count = 0;
8169 } else {
8170 mc_update = bnxt_mc_list_updated(bp, &mask);
8171 }
8172
8173 if (mask != vnic->rx_mask || uc_update || mc_update) {
8174 vnic->rx_mask = mask;
8175
8176 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008177 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008178 }
8179}
8180
Michael Chanb664f002015-12-02 01:54:08 -05008181static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04008182{
8183 struct net_device *dev = bp->dev;
8184 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8185 struct netdev_hw_addr *ha;
8186 int i, off = 0, rc;
8187 bool uc_update;
8188
8189 netif_addr_lock_bh(dev);
8190 uc_update = bnxt_uc_list_updated(bp);
8191 netif_addr_unlock_bh(dev);
8192
8193 if (!uc_update)
8194 goto skip_uc;
8195
8196 mutex_lock(&bp->hwrm_cmd_lock);
8197 for (i = 1; i < vnic->uc_filter_count; i++) {
8198 struct hwrm_cfa_l2_filter_free_input req = {0};
8199
8200 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
8201 -1);
8202
8203 req.l2_filter_id = vnic->fw_l2_filter_id[i];
8204
8205 rc = _hwrm_send_message(bp, &req, sizeof(req),
8206 HWRM_CMD_TIMEOUT);
8207 }
8208 mutex_unlock(&bp->hwrm_cmd_lock);
8209
8210 vnic->uc_filter_count = 1;
8211
8212 netif_addr_lock_bh(dev);
8213 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
8214 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8215 } else {
8216 netdev_for_each_uc_addr(ha, dev) {
8217 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
8218 off += ETH_ALEN;
8219 vnic->uc_filter_count++;
8220 }
8221 }
8222 netif_addr_unlock_bh(dev);
8223
8224 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
8225 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
8226 if (rc) {
8227 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
8228 rc);
8229 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05008230 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008231 }
8232 }
8233
8234skip_uc:
8235 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
8236 if (rc)
8237 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
8238 rc);
Michael Chanb664f002015-12-02 01:54:08 -05008239
8240 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008241}
8242
Michael Chan2773dfb2018-04-26 17:44:42 -04008243static bool bnxt_can_reserve_rings(struct bnxt *bp)
8244{
8245#ifdef CONFIG_BNXT_SRIOV
Michael Chanf1ca94d2018-08-05 16:51:53 -04008246 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
Michael Chan2773dfb2018-04-26 17:44:42 -04008247 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8248
8249 /* No minimum rings were provisioned by the PF. Don't
8250 * reserve rings by default when device is down.
8251 */
8252 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
8253 return true;
8254
8255 if (!netif_running(bp->dev))
8256 return false;
8257 }
8258#endif
8259 return true;
8260}
8261
Michael Chan8079e8f2016-12-29 12:13:37 -05008262/* If the chip and firmware supports RFS */
8263static bool bnxt_rfs_supported(struct bnxt *bp)
8264{
Michael Chan41e8d792018-10-14 07:02:48 -04008265 if (bp->flags & BNXT_FLAG_CHIP_P5)
8266 return false;
Michael Chan8079e8f2016-12-29 12:13:37 -05008267 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
8268 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05008269 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8270 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05008271 return false;
8272}
8273
8274/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008275static bool bnxt_rfs_capable(struct bnxt *bp)
8276{
8277#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05008278 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008279
Michael Chan41e8d792018-10-14 07:02:48 -04008280 if (bp->flags & BNXT_FLAG_CHIP_P5)
8281 return false;
Michael Chan2773dfb2018-04-26 17:44:42 -04008282 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008283 return false;
8284
8285 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05008286 max_vnics = bnxt_get_max_func_vnics(bp);
8287 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05008288
8289 /* RSS contexts not a limiting factor */
8290 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8291 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05008292 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Michael Chan6a1eef52018-01-17 03:21:10 -05008293 if (bp->rx_nr_rings > 1)
8294 netdev_warn(bp->dev,
8295 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
8296 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008297 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04008298 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008299
Michael Chanf1ca94d2018-08-05 16:51:53 -04008300 if (!BNXT_NEW_RM(bp))
Michael Chan6a1eef52018-01-17 03:21:10 -05008301 return true;
8302
8303 if (vnics == bp->hw_resc.resv_vnics)
8304 return true;
8305
8306 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, vnics);
8307 if (vnics <= bp->hw_resc.resv_vnics)
8308 return true;
8309
8310 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
8311 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 1);
8312 return false;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008313#else
8314 return false;
8315#endif
8316}
8317
Michael Chanc0c050c2015-10-22 16:01:17 -04008318static netdev_features_t bnxt_fix_features(struct net_device *dev,
8319 netdev_features_t features)
8320{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008321 struct bnxt *bp = netdev_priv(dev);
8322
Vasundhara Volama2304902016-07-25 12:33:36 -04008323 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008324 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04008325
Michael Chan1054aee2017-12-16 03:09:42 -05008326 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
8327 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8328
8329 if (!(features & NETIF_F_GRO))
8330 features &= ~NETIF_F_GRO_HW;
8331
8332 if (features & NETIF_F_GRO_HW)
8333 features &= ~NETIF_F_LRO;
8334
Michael Chan5a9f6b22016-06-06 02:37:15 -04008335 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
8336 * turned on or off together.
8337 */
8338 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
8339 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
8340 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
8341 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
8342 NETIF_F_HW_VLAN_STAG_RX);
8343 else
8344 features |= NETIF_F_HW_VLAN_CTAG_RX |
8345 NETIF_F_HW_VLAN_STAG_RX;
8346 }
Michael Chancf6645f2016-06-13 02:25:28 -04008347#ifdef CONFIG_BNXT_SRIOV
8348 if (BNXT_VF(bp)) {
8349 if (bp->vf.vlan) {
8350 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
8351 NETIF_F_HW_VLAN_STAG_RX);
8352 }
8353 }
8354#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04008355 return features;
8356}
8357
8358static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
8359{
8360 struct bnxt *bp = netdev_priv(dev);
8361 u32 flags = bp->flags;
8362 u32 changes;
8363 int rc = 0;
8364 bool re_init = false;
8365 bool update_tpa = false;
8366
8367 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Michael Chan1054aee2017-12-16 03:09:42 -05008368 if (features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04008369 flags |= BNXT_FLAG_GRO;
Michael Chan1054aee2017-12-16 03:09:42 -05008370 else if (features & NETIF_F_LRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04008371 flags |= BNXT_FLAG_LRO;
8372
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008373 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
8374 flags &= ~BNXT_FLAG_TPA;
8375
Michael Chanc0c050c2015-10-22 16:01:17 -04008376 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8377 flags |= BNXT_FLAG_STRIP_VLAN;
8378
8379 if (features & NETIF_F_NTUPLE)
8380 flags |= BNXT_FLAG_RFS;
8381
8382 changes = flags ^ bp->flags;
8383 if (changes & BNXT_FLAG_TPA) {
8384 update_tpa = true;
8385 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
8386 (flags & BNXT_FLAG_TPA) == 0)
8387 re_init = true;
8388 }
8389
8390 if (changes & ~BNXT_FLAG_TPA)
8391 re_init = true;
8392
8393 if (flags != bp->flags) {
8394 u32 old_flags = bp->flags;
8395
8396 bp->flags = flags;
8397
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008398 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04008399 if (update_tpa)
8400 bnxt_set_ring_params(bp);
8401 return rc;
8402 }
8403
8404 if (re_init) {
8405 bnxt_close_nic(bp, false, false);
8406 if (update_tpa)
8407 bnxt_set_ring_params(bp);
8408
8409 return bnxt_open_nic(bp, false, false);
8410 }
8411 if (update_tpa) {
8412 rc = bnxt_set_tpa(bp,
8413 (flags & BNXT_FLAG_TPA) ?
8414 true : false);
8415 if (rc)
8416 bp->flags = old_flags;
8417 }
8418 }
8419 return rc;
8420}
8421
Michael Chan9f554592016-01-02 23:44:58 -05008422static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
8423{
Michael Chanb6ab4b02016-01-02 23:44:59 -05008424 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05008425 int i = bnapi->index;
8426
Michael Chan3b2b7d92016-01-02 23:45:00 -05008427 if (!txr)
8428 return;
8429
Michael Chan9f554592016-01-02 23:44:58 -05008430 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
8431 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
8432 txr->tx_cons);
8433}
8434
8435static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
8436{
Michael Chanb6ab4b02016-01-02 23:44:59 -05008437 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05008438 int i = bnapi->index;
8439
Michael Chan3b2b7d92016-01-02 23:45:00 -05008440 if (!rxr)
8441 return;
8442
Michael Chan9f554592016-01-02 23:44:58 -05008443 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",
8444 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
8445 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
8446 rxr->rx_sw_agg_prod);
8447}
8448
8449static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
8450{
8451 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8452 int i = bnapi->index;
8453
8454 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
8455 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
8456}
8457
Michael Chanc0c050c2015-10-22 16:01:17 -04008458static void bnxt_dbg_dump_states(struct bnxt *bp)
8459{
8460 int i;
8461 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04008462
8463 for (i = 0; i < bp->cp_nr_rings; i++) {
8464 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04008465 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05008466 bnxt_dump_tx_sw_state(bnapi);
8467 bnxt_dump_rx_sw_state(bnapi);
8468 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04008469 }
8470 }
8471}
8472
Michael Chan6988bd92016-06-13 02:25:29 -04008473static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04008474{
Michael Chan6988bd92016-06-13 02:25:29 -04008475 if (!silent)
8476 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05008477 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05008478 int rc;
8479
8480 if (!silent)
8481 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05008482 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05008483 rc = bnxt_open_nic(bp, false, false);
8484 if (!silent && !rc)
8485 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05008486 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008487}
8488
8489static void bnxt_tx_timeout(struct net_device *dev)
8490{
8491 struct bnxt *bp = netdev_priv(dev);
8492
8493 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
8494 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008495 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008496}
8497
Kees Cooke99e88a2017-10-16 14:43:17 -07008498static void bnxt_timer(struct timer_list *t)
Michael Chanc0c050c2015-10-22 16:01:17 -04008499{
Kees Cooke99e88a2017-10-16 14:43:17 -07008500 struct bnxt *bp = from_timer(bp, t, timer);
Michael Chanc0c050c2015-10-22 16:01:17 -04008501 struct net_device *dev = bp->dev;
8502
8503 if (!netif_running(dev))
8504 return;
8505
8506 if (atomic_read(&bp->intr_sem) != 0)
8507 goto bnxt_restart_timer;
8508
Michael Chanadcc3312017-07-24 12:34:24 -04008509 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
8510 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05008511 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008512 bnxt_queue_sp_work(bp);
Michael Chan3bdf56c2016-03-07 15:38:45 -05008513 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04008514
8515 if (bnxt_tc_flower_enabled(bp)) {
8516 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
8517 bnxt_queue_sp_work(bp);
8518 }
Michael Chana1ef4a72018-08-05 16:51:49 -04008519
8520 if (bp->link_info.phy_retry) {
8521 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
8522 bp->link_info.phy_retry = 0;
8523 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
8524 } else {
8525 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
8526 bnxt_queue_sp_work(bp);
8527 }
8528 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008529bnxt_restart_timer:
8530 mod_timer(&bp->timer, jiffies + bp->current_interval);
8531}
8532
Michael Chana551ee92017-01-25 02:55:07 -05008533static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04008534{
Michael Chana551ee92017-01-25 02:55:07 -05008535 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
8536 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04008537 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
8538 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
8539 */
8540 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
8541 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05008542}
8543
8544static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
8545{
Michael Chan6988bd92016-06-13 02:25:29 -04008546 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
8547 rtnl_unlock();
8548}
8549
Michael Chana551ee92017-01-25 02:55:07 -05008550/* Only called from bnxt_sp_task() */
8551static void bnxt_reset(struct bnxt *bp, bool silent)
8552{
8553 bnxt_rtnl_lock_sp(bp);
8554 if (test_bit(BNXT_STATE_OPEN, &bp->state))
8555 bnxt_reset_task(bp, silent);
8556 bnxt_rtnl_unlock_sp(bp);
8557}
8558
Michael Chanc0c050c2015-10-22 16:01:17 -04008559static void bnxt_cfg_ntp_filters(struct bnxt *);
8560
8561static void bnxt_sp_task(struct work_struct *work)
8562{
8563 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04008564
Michael Chan4cebdce2015-12-09 19:35:43 -05008565 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
8566 smp_mb__after_atomic();
8567 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
8568 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04008569 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05008570 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008571
8572 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
8573 bnxt_cfg_rx_mode(bp);
8574
8575 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
8576 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008577 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
8578 bnxt_hwrm_exec_fwd_req(bp);
8579 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
8580 bnxt_hwrm_tunnel_dst_port_alloc(
8581 bp, bp->vxlan_port,
8582 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8583 }
8584 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
8585 bnxt_hwrm_tunnel_dst_port_free(
8586 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8587 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07008588 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
8589 bnxt_hwrm_tunnel_dst_port_alloc(
8590 bp, bp->nge_port,
8591 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8592 }
8593 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
8594 bnxt_hwrm_tunnel_dst_port_free(
8595 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8596 }
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04008597 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05008598 bnxt_hwrm_port_qstats(bp);
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04008599 bnxt_hwrm_port_qstats_ext(bp);
8600 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05008601
Michael Chan0eaa24b2017-01-25 02:55:08 -05008602 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04008603 int rc;
Michael Chan0eaa24b2017-01-25 02:55:08 -05008604
Michael Chane2dc9b62017-10-13 21:09:30 -04008605 mutex_lock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05008606 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
8607 &bp->sp_event))
8608 bnxt_hwrm_phy_qcaps(bp);
8609
Michael Chane2dc9b62017-10-13 21:09:30 -04008610 rc = bnxt_update_link(bp, true);
8611 mutex_unlock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05008612 if (rc)
8613 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
8614 rc);
8615 }
Michael Chana1ef4a72018-08-05 16:51:49 -04008616 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
8617 int rc;
8618
8619 mutex_lock(&bp->link_lock);
8620 rc = bnxt_update_phy_setting(bp);
8621 mutex_unlock(&bp->link_lock);
8622 if (rc) {
8623 netdev_warn(bp->dev, "update phy settings retry failed\n");
8624 } else {
8625 bp->link_info.phy_retry = false;
8626 netdev_info(bp->dev, "update phy settings retry succeeded\n");
8627 }
8628 }
Michael Chan90c694b2017-01-25 02:55:09 -05008629 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04008630 mutex_lock(&bp->link_lock);
8631 bnxt_get_port_module_status(bp);
8632 mutex_unlock(&bp->link_lock);
Michael Chan90c694b2017-01-25 02:55:09 -05008633 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04008634
8635 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
8636 bnxt_tc_flow_stats_work(bp);
8637
Michael Chane2dc9b62017-10-13 21:09:30 -04008638 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
8639 * must be the last functions to be called before exiting.
8640 */
Michael Chanc0c050c2015-10-22 16:01:17 -04008641 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
8642 bnxt_reset(bp, false);
8643
8644 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
8645 bnxt_reset(bp, true);
8646
Michael Chanc0c050c2015-10-22 16:01:17 -04008647 smp_mb__before_atomic();
8648 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
8649}
8650
Michael Chand1e79252017-02-06 16:55:38 -05008651/* Under rtnl_lock */
Michael Chan98fdbe72017-08-28 13:40:26 -04008652int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
8653 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05008654{
8655 int max_rx, max_tx, tx_sets = 1;
8656 int tx_rings_needed;
Michael Chan8f23d632018-01-17 03:21:12 -05008657 int rx_rings = rx;
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05008658 int cp, vnics, rc;
Michael Chand1e79252017-02-06 16:55:38 -05008659
Michael Chand1e79252017-02-06 16:55:38 -05008660 if (tcs)
8661 tx_sets = tcs;
8662
8663 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
8664 if (rc)
8665 return rc;
8666
8667 if (max_rx < rx)
8668 return -ENOMEM;
8669
Michael Chan5f449242017-02-06 16:55:40 -05008670 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05008671 if (max_tx < tx_rings_needed)
8672 return -ENOMEM;
8673
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05008674 vnics = 1;
8675 if (bp->flags & BNXT_FLAG_RFS)
8676 vnics += rx_rings;
8677
Michael Chan8f23d632018-01-17 03:21:12 -05008678 if (bp->flags & BNXT_FLAG_AGG_RINGS)
8679 rx_rings <<= 1;
8680 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
Michael Chanf1ca94d2018-08-05 16:51:53 -04008681 if (BNXT_NEW_RM(bp))
Michael Chan11c3ec72018-04-11 11:50:17 -04008682 cp += bnxt_get_ulp_msix_num(bp);
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05008683 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
8684 vnics);
Michael Chand1e79252017-02-06 16:55:38 -05008685}
8686
Sathya Perla17086392017-02-20 19:25:18 -05008687static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
8688{
8689 if (bp->bar2) {
8690 pci_iounmap(pdev, bp->bar2);
8691 bp->bar2 = NULL;
8692 }
8693
8694 if (bp->bar1) {
8695 pci_iounmap(pdev, bp->bar1);
8696 bp->bar1 = NULL;
8697 }
8698
8699 if (bp->bar0) {
8700 pci_iounmap(pdev, bp->bar0);
8701 bp->bar0 = NULL;
8702 }
8703}
8704
8705static void bnxt_cleanup_pci(struct bnxt *bp)
8706{
8707 bnxt_unmap_bars(bp, bp->pdev);
8708 pci_release_regions(bp->pdev);
8709 pci_disable_device(bp->pdev);
8710}
8711
Michael Chan18775aa2017-10-26 11:51:27 -04008712static void bnxt_init_dflt_coal(struct bnxt *bp)
8713{
8714 struct bnxt_coal *coal;
8715
8716 /* Tick values in micro seconds.
8717 * 1 coal_buf x bufs_per_record = 1 completion record.
8718 */
8719 coal = &bp->rx_coal;
8720 coal->coal_ticks = 14;
8721 coal->coal_bufs = 30;
8722 coal->coal_ticks_irq = 1;
8723 coal->coal_bufs_irq = 2;
Andy Gospodarek05abe4dd2018-04-26 17:44:38 -04008724 coal->idle_thresh = 50;
Michael Chan18775aa2017-10-26 11:51:27 -04008725 coal->bufs_per_record = 2;
8726 coal->budget = 64; /* NAPI budget */
8727
8728 coal = &bp->tx_coal;
8729 coal->coal_ticks = 28;
8730 coal->coal_bufs = 30;
8731 coal->coal_ticks_irq = 2;
8732 coal->coal_bufs_irq = 2;
8733 coal->bufs_per_record = 1;
8734
8735 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
8736}
8737
Michael Chanc0c050c2015-10-22 16:01:17 -04008738static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
8739{
8740 int rc;
8741 struct bnxt *bp = netdev_priv(dev);
8742
8743 SET_NETDEV_DEV(dev, &pdev->dev);
8744
8745 /* enable device (incl. PCI PM wakeup), and bus-mastering */
8746 rc = pci_enable_device(pdev);
8747 if (rc) {
8748 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
8749 goto init_err;
8750 }
8751
8752 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
8753 dev_err(&pdev->dev,
8754 "Cannot find PCI device base address, aborting\n");
8755 rc = -ENODEV;
8756 goto init_err_disable;
8757 }
8758
8759 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
8760 if (rc) {
8761 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
8762 goto init_err_disable;
8763 }
8764
8765 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
8766 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
8767 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
8768 goto init_err_disable;
8769 }
8770
8771 pci_set_master(pdev);
8772
8773 bp->dev = dev;
8774 bp->pdev = pdev;
8775
8776 bp->bar0 = pci_ioremap_bar(pdev, 0);
8777 if (!bp->bar0) {
8778 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
8779 rc = -ENOMEM;
8780 goto init_err_release;
8781 }
8782
8783 bp->bar1 = pci_ioremap_bar(pdev, 2);
8784 if (!bp->bar1) {
8785 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
8786 rc = -ENOMEM;
8787 goto init_err_release;
8788 }
8789
8790 bp->bar2 = pci_ioremap_bar(pdev, 4);
8791 if (!bp->bar2) {
8792 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
8793 rc = -ENOMEM;
8794 goto init_err_release;
8795 }
8796
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008797 pci_enable_pcie_error_reporting(pdev);
8798
Michael Chanc0c050c2015-10-22 16:01:17 -04008799 INIT_WORK(&bp->sp_task, bnxt_sp_task);
8800
8801 spin_lock_init(&bp->ntp_fltr_lock);
Michael Chan697197e2018-10-14 07:02:46 -04008802#if BITS_PER_LONG == 32
8803 spin_lock_init(&bp->db_lock);
8804#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04008805
8806 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
8807 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
8808
Michael Chan18775aa2017-10-26 11:51:27 -04008809 bnxt_init_dflt_coal(bp);
Michael Chan51f30782016-07-01 18:46:29 -04008810
Kees Cooke99e88a2017-10-16 14:43:17 -07008811 timer_setup(&bp->timer, bnxt_timer, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04008812 bp->current_interval = BNXT_TIMER_INTERVAL;
8813
Michael Chancaefe522015-12-09 19:35:42 -05008814 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04008815 return 0;
8816
8817init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05008818 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04008819 pci_release_regions(pdev);
8820
8821init_err_disable:
8822 pci_disable_device(pdev);
8823
8824init_err:
8825 return rc;
8826}
8827
8828/* rtnl_lock held */
8829static int bnxt_change_mac_addr(struct net_device *dev, void *p)
8830{
8831 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05008832 struct bnxt *bp = netdev_priv(dev);
8833 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04008834
8835 if (!is_valid_ether_addr(addr->sa_data))
8836 return -EADDRNOTAVAIL;
8837
Michael Chanc1a7bdf2017-10-26 11:51:24 -04008838 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
8839 return 0;
8840
Michael Chan28ea3342018-09-14 15:41:29 -04008841 rc = bnxt_approve_mac(bp, addr->sa_data, true);
Michael Chan84c33dd2016-04-11 04:11:13 -04008842 if (rc)
8843 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008844
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05008845 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8846 if (netif_running(dev)) {
8847 bnxt_close_nic(bp, false, false);
8848 rc = bnxt_open_nic(bp, false, false);
8849 }
8850
8851 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008852}
8853
8854/* rtnl_lock held */
8855static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
8856{
8857 struct bnxt *bp = netdev_priv(dev);
8858
Michael Chanc0c050c2015-10-22 16:01:17 -04008859 if (netif_running(dev))
8860 bnxt_close_nic(bp, false, false);
8861
8862 dev->mtu = new_mtu;
8863 bnxt_set_ring_params(bp);
8864
8865 if (netif_running(dev))
8866 return bnxt_open_nic(bp, false, false);
8867
8868 return 0;
8869}
8870
Michael Chanc5e3deb2016-12-02 21:17:15 -05008871int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04008872{
8873 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05008874 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05008875 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08008876
Michael Chanc0c050c2015-10-22 16:01:17 -04008877 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05008878 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04008879 tc, bp->max_tc);
8880 return -EINVAL;
8881 }
8882
8883 if (netdev_get_num_tc(dev) == tc)
8884 return 0;
8885
Michael Chan3ffb6a32016-11-11 00:11:42 -05008886 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8887 sh = true;
8888
Michael Chan98fdbe72017-08-28 13:40:26 -04008889 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
8890 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05008891 if (rc)
8892 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008893
8894 /* Needs to close the device and do hw resource re-allocations */
8895 if (netif_running(bp->dev))
8896 bnxt_close_nic(bp, true, false);
8897
8898 if (tc) {
8899 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
8900 netdev_set_num_tc(dev, tc);
8901 } else {
8902 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8903 netdev_reset_tc(dev);
8904 }
Michael Chan87e9b372017-08-23 19:34:03 -04008905 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
Michael Chan3ffb6a32016-11-11 00:11:42 -05008906 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8907 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04008908 bp->num_stat_ctxs = bp->cp_nr_rings;
8909
8910 if (netif_running(bp->dev))
8911 return bnxt_open_nic(bp, true, false);
8912
8913 return 0;
8914}
8915
Jiri Pirko9e0fd152017-10-19 15:50:39 +02008916static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8917 void *cb_priv)
Sathya Perla2ae74082017-08-28 13:40:33 -04008918{
Jiri Pirko9e0fd152017-10-19 15:50:39 +02008919 struct bnxt *bp = cb_priv;
Sathya Perla2ae74082017-08-28 13:40:33 -04008920
Jakub Kicinski312324f2018-01-25 14:00:48 -08008921 if (!bnxt_tc_flower_enabled(bp) ||
8922 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
Sathya Perla2ae74082017-08-28 13:40:33 -04008923 return -EOPNOTSUPP;
8924
Jiri Pirko9e0fd152017-10-19 15:50:39 +02008925 switch (type) {
8926 case TC_SETUP_CLSFLOWER:
8927 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
8928 default:
8929 return -EOPNOTSUPP;
8930 }
8931}
8932
8933static int bnxt_setup_tc_block(struct net_device *dev,
8934 struct tc_block_offload *f)
8935{
8936 struct bnxt *bp = netdev_priv(dev);
8937
8938 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8939 return -EOPNOTSUPP;
8940
8941 switch (f->command) {
8942 case TC_BLOCK_BIND:
8943 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
John Hurley60513bd2018-06-25 14:30:04 -07008944 bp, bp, f->extack);
Jiri Pirko9e0fd152017-10-19 15:50:39 +02008945 case TC_BLOCK_UNBIND:
8946 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
8947 return 0;
8948 default:
8949 return -EOPNOTSUPP;
8950 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008951}
8952
Jiri Pirko2572ac52017-08-07 10:15:17 +02008953static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02008954 void *type_data)
Michael Chanc5e3deb2016-12-02 21:17:15 -05008955{
Sathya Perla2ae74082017-08-28 13:40:33 -04008956 switch (type) {
Jiri Pirko9e0fd152017-10-19 15:50:39 +02008957 case TC_SETUP_BLOCK:
8958 return bnxt_setup_tc_block(dev, type_data);
Nogah Frankel575ed7d2017-11-06 07:23:42 +01008959 case TC_SETUP_QDISC_MQPRIO: {
Sathya Perla2ae74082017-08-28 13:40:33 -04008960 struct tc_mqprio_qopt *mqprio = type_data;
Jiri Pirkode4784c2017-08-07 10:15:32 +02008961
Sathya Perla2ae74082017-08-28 13:40:33 -04008962 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
8963
8964 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
8965 }
8966 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02008967 return -EOPNOTSUPP;
Sathya Perla2ae74082017-08-28 13:40:33 -04008968 }
Michael Chanc5e3deb2016-12-02 21:17:15 -05008969}
8970
Michael Chanc0c050c2015-10-22 16:01:17 -04008971#ifdef CONFIG_RFS_ACCEL
8972static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
8973 struct bnxt_ntuple_filter *f2)
8974{
8975 struct flow_keys *keys1 = &f1->fkeys;
8976 struct flow_keys *keys2 = &f2->fkeys;
8977
8978 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
8979 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
8980 keys1->ports.ports == keys2->ports.ports &&
8981 keys1->basic.ip_proto == keys2->basic.ip_proto &&
8982 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05008983 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04008984 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
8985 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04008986 return true;
8987
8988 return false;
8989}
8990
8991static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
8992 u16 rxq_index, u32 flow_id)
8993{
8994 struct bnxt *bp = netdev_priv(dev);
8995 struct bnxt_ntuple_filter *fltr, *new_fltr;
8996 struct flow_keys *fkeys;
8997 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04008998 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04008999 struct hlist_head *head;
9000
Michael Chana54c4d72016-07-25 12:33:35 -04009001 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
9002 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9003 int off = 0, j;
9004
9005 netif_addr_lock_bh(dev);
9006 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
9007 if (ether_addr_equal(eth->h_dest,
9008 vnic->uc_list + off)) {
9009 l2_idx = j + 1;
9010 break;
9011 }
9012 }
9013 netif_addr_unlock_bh(dev);
9014 if (!l2_idx)
9015 return -EINVAL;
9016 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009017 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
9018 if (!new_fltr)
9019 return -ENOMEM;
9020
9021 fkeys = &new_fltr->fkeys;
9022 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
9023 rc = -EPROTONOSUPPORT;
9024 goto err_free;
9025 }
9026
Michael Chandda0e742016-12-29 12:13:40 -05009027 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
9028 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04009029 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
9030 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
9031 rc = -EPROTONOSUPPORT;
9032 goto err_free;
9033 }
Michael Chandda0e742016-12-29 12:13:40 -05009034 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
9035 bp->hwrm_spec_code < 0x10601) {
9036 rc = -EPROTONOSUPPORT;
9037 goto err_free;
9038 }
Michael Chan61aad722017-02-12 19:18:14 -05009039 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
9040 bp->hwrm_spec_code < 0x10601) {
9041 rc = -EPROTONOSUPPORT;
9042 goto err_free;
9043 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009044
Michael Chana54c4d72016-07-25 12:33:35 -04009045 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04009046 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
9047
9048 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
9049 head = &bp->ntp_fltr_hash_tbl[idx];
9050 rcu_read_lock();
9051 hlist_for_each_entry_rcu(fltr, head, hash) {
9052 if (bnxt_fltr_match(fltr, new_fltr)) {
9053 rcu_read_unlock();
9054 rc = 0;
9055 goto err_free;
9056 }
9057 }
9058 rcu_read_unlock();
9059
9060 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05009061 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
9062 BNXT_NTP_FLTR_MAX_FLTR, 0);
9063 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04009064 spin_unlock_bh(&bp->ntp_fltr_lock);
9065 rc = -ENOMEM;
9066 goto err_free;
9067 }
9068
Michael Chan84e86b92015-11-05 16:25:50 -05009069 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04009070 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04009071 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04009072 new_fltr->rxq = rxq_index;
9073 hlist_add_head_rcu(&new_fltr->hash, head);
9074 bp->ntp_fltr_count++;
9075 spin_unlock_bh(&bp->ntp_fltr_lock);
9076
9077 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04009078 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009079
9080 return new_fltr->sw_id;
9081
9082err_free:
9083 kfree(new_fltr);
9084 return rc;
9085}
9086
9087static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9088{
9089 int i;
9090
9091 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
9092 struct hlist_head *head;
9093 struct hlist_node *tmp;
9094 struct bnxt_ntuple_filter *fltr;
9095 int rc;
9096
9097 head = &bp->ntp_fltr_hash_tbl[i];
9098 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
9099 bool del = false;
9100
9101 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
9102 if (rps_may_expire_flow(bp->dev, fltr->rxq,
9103 fltr->flow_id,
9104 fltr->sw_id)) {
9105 bnxt_hwrm_cfa_ntuple_filter_free(bp,
9106 fltr);
9107 del = true;
9108 }
9109 } else {
9110 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
9111 fltr);
9112 if (rc)
9113 del = true;
9114 else
9115 set_bit(BNXT_FLTR_VALID, &fltr->state);
9116 }
9117
9118 if (del) {
9119 spin_lock_bh(&bp->ntp_fltr_lock);
9120 hlist_del_rcu(&fltr->hash);
9121 bp->ntp_fltr_count--;
9122 spin_unlock_bh(&bp->ntp_fltr_lock);
9123 synchronize_rcu();
9124 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
9125 kfree(fltr);
9126 }
9127 }
9128 }
Jeffrey Huang19241362016-02-26 04:00:00 -05009129 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
9130 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04009131}
9132
9133#else
9134
9135static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9136{
9137}
9138
9139#endif /* CONFIG_RFS_ACCEL */
9140
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009141static void bnxt_udp_tunnel_add(struct net_device *dev,
9142 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04009143{
9144 struct bnxt *bp = netdev_priv(dev);
9145
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009146 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9147 return;
9148
Michael Chanc0c050c2015-10-22 16:01:17 -04009149 if (!netif_running(dev))
9150 return;
9151
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009152 switch (ti->type) {
9153 case UDP_TUNNEL_TYPE_VXLAN:
9154 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
9155 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04009156
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009157 bp->vxlan_port_cnt++;
9158 if (bp->vxlan_port_cnt == 1) {
9159 bp->vxlan_port = ti->port;
9160 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04009161 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009162 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009163 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07009164 case UDP_TUNNEL_TYPE_GENEVE:
9165 if (bp->nge_port_cnt && bp->nge_port != ti->port)
9166 return;
9167
9168 bp->nge_port_cnt++;
9169 if (bp->nge_port_cnt == 1) {
9170 bp->nge_port = ti->port;
9171 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
9172 }
9173 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009174 default:
9175 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04009176 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009177
Michael Chanc213eae2017-10-13 21:09:29 -04009178 bnxt_queue_sp_work(bp);
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009179}
9180
9181static void bnxt_udp_tunnel_del(struct net_device *dev,
9182 struct udp_tunnel_info *ti)
9183{
9184 struct bnxt *bp = netdev_priv(dev);
9185
9186 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9187 return;
9188
9189 if (!netif_running(dev))
9190 return;
9191
9192 switch (ti->type) {
9193 case UDP_TUNNEL_TYPE_VXLAN:
9194 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
9195 return;
9196 bp->vxlan_port_cnt--;
9197
9198 if (bp->vxlan_port_cnt != 0)
9199 return;
9200
9201 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
9202 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07009203 case UDP_TUNNEL_TYPE_GENEVE:
9204 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
9205 return;
9206 bp->nge_port_cnt--;
9207
9208 if (bp->nge_port_cnt != 0)
9209 return;
9210
9211 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
9212 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009213 default:
9214 return;
9215 }
9216
Michael Chanc213eae2017-10-13 21:09:29 -04009217 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009218}
9219
Michael Chan39d8ba22017-07-24 12:34:22 -04009220static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9221 struct net_device *dev, u32 filter_mask,
9222 int nlflags)
9223{
9224 struct bnxt *bp = netdev_priv(dev);
9225
9226 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
9227 nlflags, filter_mask, NULL);
9228}
9229
9230static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
9231 u16 flags)
9232{
9233 struct bnxt *bp = netdev_priv(dev);
9234 struct nlattr *attr, *br_spec;
9235 int rem, rc = 0;
9236
9237 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
9238 return -EOPNOTSUPP;
9239
9240 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9241 if (!br_spec)
9242 return -EINVAL;
9243
9244 nla_for_each_nested(attr, br_spec, rem) {
9245 u16 mode;
9246
9247 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9248 continue;
9249
9250 if (nla_len(attr) < sizeof(mode))
9251 return -EINVAL;
9252
9253 mode = nla_get_u16(attr);
9254 if (mode == bp->br_mode)
9255 break;
9256
9257 rc = bnxt_hwrm_set_br_mode(bp, mode);
9258 if (!rc)
9259 bp->br_mode = mode;
9260 break;
9261 }
9262 return rc;
9263}
9264
Sathya Perlac124a622017-07-24 12:34:29 -04009265static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
9266 size_t len)
9267{
9268 struct bnxt *bp = netdev_priv(dev);
9269 int rc;
9270
9271 /* The PF and it's VF-reps only support the switchdev framework */
9272 if (!BNXT_PF(bp))
9273 return -EOPNOTSUPP;
9274
Sathya Perla53f70b82017-07-25 13:28:41 -04009275 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04009276
9277 if (rc >= len)
9278 return -EOPNOTSUPP;
9279 return 0;
9280}
9281
9282int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
9283{
9284 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
9285 return -EOPNOTSUPP;
9286
9287 /* The PF and it's VF-reps only support the switchdev framework */
9288 if (!BNXT_PF(bp))
9289 return -EOPNOTSUPP;
9290
9291 switch (attr->id) {
9292 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Sathya Perladd4ea1d2018-01-17 03:21:16 -05009293 attr->u.ppid.id_len = sizeof(bp->switch_id);
9294 memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
Sathya Perlac124a622017-07-24 12:34:29 -04009295 break;
9296 default:
9297 return -EOPNOTSUPP;
9298 }
9299 return 0;
9300}
9301
9302static int bnxt_swdev_port_attr_get(struct net_device *dev,
9303 struct switchdev_attr *attr)
9304{
9305 return bnxt_port_attr_get(netdev_priv(dev), attr);
9306}
9307
9308static const struct switchdev_ops bnxt_switchdev_ops = {
9309 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
9310};
9311
Michael Chanc0c050c2015-10-22 16:01:17 -04009312static const struct net_device_ops bnxt_netdev_ops = {
9313 .ndo_open = bnxt_open,
9314 .ndo_start_xmit = bnxt_start_xmit,
9315 .ndo_stop = bnxt_close,
9316 .ndo_get_stats64 = bnxt_get_stats64,
9317 .ndo_set_rx_mode = bnxt_set_rx_mode,
9318 .ndo_do_ioctl = bnxt_ioctl,
9319 .ndo_validate_addr = eth_validate_addr,
9320 .ndo_set_mac_address = bnxt_change_mac_addr,
9321 .ndo_change_mtu = bnxt_change_mtu,
9322 .ndo_fix_features = bnxt_fix_features,
9323 .ndo_set_features = bnxt_set_features,
9324 .ndo_tx_timeout = bnxt_tx_timeout,
9325#ifdef CONFIG_BNXT_SRIOV
9326 .ndo_get_vf_config = bnxt_get_vf_config,
9327 .ndo_set_vf_mac = bnxt_set_vf_mac,
9328 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
9329 .ndo_set_vf_rate = bnxt_set_vf_bw,
9330 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
9331 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
Vasundhara Volam746df132018-03-31 13:54:10 -04009332 .ndo_set_vf_trust = bnxt_set_vf_trust,
Michael Chanc0c050c2015-10-22 16:01:17 -04009333#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04009334 .ndo_setup_tc = bnxt_setup_tc,
9335#ifdef CONFIG_RFS_ACCEL
9336 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
9337#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009338 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
9339 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009340 .ndo_bpf = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04009341 .ndo_bridge_getlink = bnxt_bridge_getlink,
9342 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04009343 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04009344};
9345
9346static void bnxt_remove_one(struct pci_dev *pdev)
9347{
9348 struct net_device *dev = pci_get_drvdata(pdev);
9349 struct bnxt *bp = netdev_priv(dev);
9350
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009351 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04009352 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009353 bnxt_dl_unregister(bp);
9354 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009355
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009356 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04009357 unregister_netdev(dev);
Sathya Perla2ae74082017-08-28 13:40:33 -04009358 bnxt_shutdown_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04009359 bnxt_cancel_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009360 bp->sp_event = 0;
9361
Michael Chan78095922016-12-07 00:26:16 -05009362 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05009363 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009364 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04009365 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04009366 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05009367 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05009368 kfree(bp->edev);
9369 bp->edev = NULL;
Michael Chan98f04cf2018-10-14 07:02:43 -04009370 bnxt_free_ctx_mem(bp);
9371 kfree(bp->ctx);
9372 bp->ctx = NULL;
Sathya Perla17086392017-02-20 19:25:18 -05009373 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009374 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04009375}
9376
9377static int bnxt_probe_phy(struct bnxt *bp)
9378{
9379 int rc = 0;
9380 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04009381
Michael Chan170ce012016-04-05 14:08:57 -04009382 rc = bnxt_hwrm_phy_qcaps(bp);
9383 if (rc) {
9384 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
9385 rc);
9386 return rc;
9387 }
Michael Chane2dc9b62017-10-13 21:09:30 -04009388 mutex_init(&bp->link_lock);
Michael Chan170ce012016-04-05 14:08:57 -04009389
Michael Chanc0c050c2015-10-22 16:01:17 -04009390 rc = bnxt_update_link(bp, false);
9391 if (rc) {
9392 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
9393 rc);
9394 return rc;
9395 }
9396
Michael Chan93ed8112016-06-13 02:25:37 -04009397 /* Older firmware does not have supported_auto_speeds, so assume
9398 * that all supported speeds can be autonegotiated.
9399 */
9400 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
9401 link_info->support_auto_speeds = link_info->support_speeds;
9402
Michael Chanc0c050c2015-10-22 16:01:17 -04009403 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05009404 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04009405 link_info->autoneg = BNXT_AUTONEG_SPEED;
9406 if (bp->hwrm_spec_code >= 0x10201) {
9407 if (link_info->auto_pause_setting &
9408 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
9409 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
9410 } else {
9411 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
9412 }
Michael Chan0d8abf02016-02-10 17:33:47 -05009413 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05009414 } else {
9415 link_info->req_link_speed = link_info->force_link_speed;
9416 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04009417 }
Michael Chanc9ee9512016-04-05 14:08:56 -04009418 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
9419 link_info->req_flow_ctrl =
9420 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
9421 else
9422 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04009423 return rc;
9424}
9425
9426static int bnxt_get_max_irq(struct pci_dev *pdev)
9427{
9428 u16 ctrl;
9429
9430 if (!pdev->msix_cap)
9431 return 1;
9432
9433 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
9434 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
9435}
9436
Michael Chan6e6c5a52016-01-02 23:45:02 -05009437static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
9438 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04009439{
Michael Chan6a4f2942018-01-17 03:21:06 -05009440 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05009441 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04009442
Michael Chan6a4f2942018-01-17 03:21:06 -05009443 *max_tx = hw_resc->max_tx_rings;
9444 *max_rx = hw_resc->max_rx_rings;
Michael Chan00fe9c32018-09-03 04:23:19 -04009445 *max_cp = min_t(int, bnxt_get_max_func_cp_rings_for_en(bp),
Vasundhara Volamc78fe052018-10-05 00:26:03 -04009446 hw_resc->max_irqs - bnxt_get_ulp_msix_num(bp));
Michael Chan6a4f2942018-01-17 03:21:06 -05009447 *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
9448 max_ring_grps = hw_resc->max_hw_ring_grps;
Prashant Sreedharan76595192016-07-18 07:15:22 -04009449 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
9450 *max_cp -= 1;
9451 *max_rx -= 2;
9452 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009453 if (bp->flags & BNXT_FLAG_AGG_RINGS)
9454 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05009455 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05009456}
9457
9458int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
9459{
9460 int rx, tx, cp;
9461
9462 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
Michael Chan78f058a2018-07-09 02:24:49 -04009463 *max_rx = rx;
9464 *max_tx = tx;
Michael Chan6e6c5a52016-01-02 23:45:02 -05009465 if (!rx || !tx || !cp)
9466 return -ENOMEM;
9467
Michael Chan6e6c5a52016-01-02 23:45:02 -05009468 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
9469}
9470
Michael Chane4060d32016-12-07 00:26:19 -05009471static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
9472 bool shared)
9473{
9474 int rc;
9475
9476 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009477 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
9478 /* Not enough rings, try disabling agg rings. */
9479 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
9480 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chan07f4fde2018-07-09 02:24:48 -04009481 if (rc) {
9482 /* set BNXT_FLAG_AGG_RINGS back for consistency */
9483 bp->flags |= BNXT_FLAG_AGG_RINGS;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009484 return rc;
Michael Chan07f4fde2018-07-09 02:24:48 -04009485 }
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009486 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
Michael Chan1054aee2017-12-16 03:09:42 -05009487 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
9488 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009489 bnxt_set_ring_params(bp);
9490 }
Michael Chane4060d32016-12-07 00:26:19 -05009491
9492 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
9493 int max_cp, max_stat, max_irq;
9494
9495 /* Reserve minimum resources for RoCE */
9496 max_cp = bnxt_get_max_func_cp_rings(bp);
9497 max_stat = bnxt_get_max_func_stat_ctxs(bp);
9498 max_irq = bnxt_get_max_func_irqs(bp);
9499 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
9500 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
9501 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
9502 return 0;
9503
9504 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
9505 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
9506 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
9507 max_cp = min_t(int, max_cp, max_irq);
9508 max_cp = min_t(int, max_cp, max_stat);
9509 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
9510 if (rc)
9511 rc = 0;
9512 }
9513 return rc;
9514}
9515
Michael Chan58ea8012018-01-17 03:21:08 -05009516/* In initial default shared ring setting, each shared ring must have a
9517 * RX/TX ring pair.
9518 */
9519static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
9520{
9521 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
9522 bp->rx_nr_rings = bp->cp_nr_rings;
9523 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
9524 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
9525}
9526
Michael Chan702c2212017-05-29 19:06:10 -04009527static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05009528{
9529 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05009530
Michael Chan2773dfb2018-04-26 17:44:42 -04009531 if (!bnxt_can_reserve_rings(bp))
9532 return 0;
9533
Michael Chan6e6c5a52016-01-02 23:45:02 -05009534 if (sh)
9535 bp->flags |= BNXT_FLAG_SHARED_RINGS;
9536 dflt_rings = netif_get_num_default_rss_queues();
Michael Chan1d3ef132018-03-31 13:54:07 -04009537 /* Reduce default rings on multi-port cards so that total default
9538 * rings do not exceed CPU count.
9539 */
9540 if (bp->port_count > 1) {
9541 int max_rings =
9542 max_t(int, num_online_cpus() / bp->port_count, 1);
9543
9544 dflt_rings = min_t(int, dflt_rings, max_rings);
9545 }
Michael Chane4060d32016-12-07 00:26:19 -05009546 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05009547 if (rc)
9548 return rc;
9549 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
9550 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan58ea8012018-01-17 03:21:08 -05009551 if (sh)
9552 bnxt_trim_dflt_sh_rings(bp);
9553 else
9554 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
9555 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
Michael Chan391be5c2016-12-29 12:13:41 -05009556
Michael Chan674f50a2018-01-17 03:21:09 -05009557 rc = __bnxt_reserve_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05009558 if (rc)
9559 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
Michael Chan58ea8012018-01-17 03:21:08 -05009560 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9561 if (sh)
9562 bnxt_trim_dflt_sh_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05009563
Michael Chan674f50a2018-01-17 03:21:09 -05009564 /* Rings may have been trimmed, re-reserve the trimmed rings. */
9565 if (bnxt_need_reserve_rings(bp)) {
9566 rc = __bnxt_reserve_rings(bp);
9567 if (rc)
9568 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
9569 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9570 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05009571 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04009572 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9573 bp->rx_nr_rings++;
9574 bp->cp_nr_rings++;
9575 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05009576 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04009577}
9578
Michael Chan47558ac2018-04-26 17:44:44 -04009579static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
9580{
9581 int rc;
9582
9583 if (bp->tx_nr_rings)
9584 return 0;
9585
Michael Chan6b95c3e2018-09-03 04:23:17 -04009586 bnxt_ulp_irq_stop(bp);
9587 bnxt_clear_int_mode(bp);
Michael Chan47558ac2018-04-26 17:44:44 -04009588 rc = bnxt_set_dflt_rings(bp, true);
9589 if (rc) {
9590 netdev_err(bp->dev, "Not enough rings available.\n");
Michael Chan6b95c3e2018-09-03 04:23:17 -04009591 goto init_dflt_ring_err;
Michael Chan47558ac2018-04-26 17:44:44 -04009592 }
9593 rc = bnxt_init_int_mode(bp);
9594 if (rc)
Michael Chan6b95c3e2018-09-03 04:23:17 -04009595 goto init_dflt_ring_err;
9596
Michael Chan47558ac2018-04-26 17:44:44 -04009597 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9598 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
9599 bp->flags |= BNXT_FLAG_RFS;
9600 bp->dev->features |= NETIF_F_NTUPLE;
9601 }
Michael Chan6b95c3e2018-09-03 04:23:17 -04009602init_dflt_ring_err:
9603 bnxt_ulp_irq_restart(bp, rc);
9604 return rc;
Michael Chan47558ac2018-04-26 17:44:44 -04009605}
9606
Michael Chan80fcaf42018-01-17 03:21:05 -05009607int bnxt_restore_pf_fw_resources(struct bnxt *bp)
Michael Chan7b08f662016-12-07 00:26:18 -05009608{
Michael Chan80fcaf42018-01-17 03:21:05 -05009609 int rc;
9610
Michael Chan7b08f662016-12-07 00:26:18 -05009611 ASSERT_RTNL();
9612 bnxt_hwrm_func_qcaps(bp);
Venkat Duvvuru1a037782018-03-09 23:46:09 -05009613
9614 if (netif_running(bp->dev))
9615 __bnxt_close_nic(bp, true, false);
9616
Michael Chanec86f142018-03-31 13:54:21 -04009617 bnxt_ulp_irq_stop(bp);
Michael Chan80fcaf42018-01-17 03:21:05 -05009618 bnxt_clear_int_mode(bp);
9619 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -04009620 bnxt_ulp_irq_restart(bp, rc);
Venkat Duvvuru1a037782018-03-09 23:46:09 -05009621
9622 if (netif_running(bp->dev)) {
9623 if (rc)
9624 dev_close(bp->dev);
9625 else
9626 rc = bnxt_open_nic(bp, true, false);
9627 }
9628
Michael Chan80fcaf42018-01-17 03:21:05 -05009629 return rc;
Michael Chan7b08f662016-12-07 00:26:18 -05009630}
9631
Michael Chana22a6ac2017-08-23 19:34:05 -04009632static int bnxt_init_mac_addr(struct bnxt *bp)
9633{
9634 int rc = 0;
9635
9636 if (BNXT_PF(bp)) {
9637 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
9638 } else {
9639#ifdef CONFIG_BNXT_SRIOV
9640 struct bnxt_vf_info *vf = &bp->vf;
Michael Chan28ea3342018-09-14 15:41:29 -04009641 bool strict_approval = true;
Michael Chana22a6ac2017-08-23 19:34:05 -04009642
9643 if (is_valid_ether_addr(vf->mac_addr)) {
Vasundhara Volam91cdda42018-01-17 03:21:14 -05009644 /* overwrite netdev dev_addr with admin VF MAC */
Michael Chana22a6ac2017-08-23 19:34:05 -04009645 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan28ea3342018-09-14 15:41:29 -04009646 /* Older PF driver or firmware may not approve this
9647 * correctly.
9648 */
9649 strict_approval = false;
Michael Chana22a6ac2017-08-23 19:34:05 -04009650 } else {
9651 eth_hw_addr_random(bp->dev);
Michael Chana22a6ac2017-08-23 19:34:05 -04009652 }
Michael Chan28ea3342018-09-14 15:41:29 -04009653 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
Michael Chana22a6ac2017-08-23 19:34:05 -04009654#endif
9655 }
9656 return rc;
9657}
9658
Michael Chanc0c050c2015-10-22 16:01:17 -04009659static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
9660{
9661 static int version_printed;
9662 struct net_device *dev;
9663 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05009664 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04009665
Ray Jui4e003382017-02-20 19:25:16 -05009666 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04009667 return -ENODEV;
9668
Michael Chanc0c050c2015-10-22 16:01:17 -04009669 if (version_printed++ == 0)
9670 pr_info("%s", version);
9671
9672 max_irqs = bnxt_get_max_irq(pdev);
9673 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
9674 if (!dev)
9675 return -ENOMEM;
9676
9677 bp = netdev_priv(dev);
Michael Chan9c1fabd2018-10-14 07:02:47 -04009678 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanc0c050c2015-10-22 16:01:17 -04009679
9680 if (bnxt_vf_pciid(ent->driver_data))
9681 bp->flags |= BNXT_FLAG_VF;
9682
Michael Chan2bcfa6f2015-12-27 18:19:24 -05009683 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04009684 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04009685
9686 rc = bnxt_init_board(pdev, dev);
9687 if (rc < 0)
9688 goto init_err_free;
9689
9690 dev->netdev_ops = &bnxt_netdev_ops;
9691 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
9692 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -07009693 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -04009694 pci_set_drvdata(pdev, dev);
9695
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04009696 rc = bnxt_alloc_hwrm_resources(bp);
9697 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05009698 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04009699
9700 mutex_init(&bp->hwrm_cmd_lock);
9701 rc = bnxt_hwrm_ver_get(bp);
9702 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05009703 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04009704
Michael Chan1dfddc42018-10-14 07:02:39 -04009705 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
9706 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
Deepak Khungare605db82017-05-29 19:06:04 -04009707 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
9708 if (rc)
9709 goto init_err_pci_clean;
9710 }
9711
Michael Chane38287b2018-10-14 07:02:45 -04009712 if (BNXT_CHIP_P5(bp))
9713 bp->flags |= BNXT_FLAG_CHIP_P5;
9714
Michael Chan3c2217a2017-03-08 18:44:32 -05009715 rc = bnxt_hwrm_func_reset(bp);
9716 if (rc)
9717 goto init_err_pci_clean;
9718
Rob Swindell5ac67d82016-09-19 03:58:03 -04009719 bnxt_hwrm_fw_set_time(bp);
9720
Michael Chanc0c050c2015-10-22 16:01:17 -04009721 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
9722 NETIF_F_TSO | NETIF_F_TSO6 |
9723 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07009724 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07009725 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
9726 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04009727 NETIF_F_RXCSUM | NETIF_F_GRO;
9728
Michael Chane38287b2018-10-14 07:02:45 -04009729 if (BNXT_SUPPORTS_TPA(bp))
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04009730 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04009731
Michael Chanc0c050c2015-10-22 16:01:17 -04009732 dev->hw_enc_features =
9733 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
9734 NETIF_F_TSO | NETIF_F_TSO6 |
9735 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07009736 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009737 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07009738 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
9739 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04009740 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
9741 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
9742 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
Michael Chane38287b2018-10-14 07:02:45 -04009743 if (BNXT_SUPPORTS_TPA(bp))
Michael Chan1054aee2017-12-16 03:09:42 -05009744 dev->hw_features |= NETIF_F_GRO_HW;
Michael Chanc0c050c2015-10-22 16:01:17 -04009745 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Michael Chan1054aee2017-12-16 03:09:42 -05009746 if (dev->features & NETIF_F_GRO_HW)
9747 dev->features &= ~NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04009748 dev->priv_flags |= IFF_UNICAST_FLT;
9749
9750#ifdef CONFIG_BNXT_SRIOV
9751 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009752 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04009753#endif
Michael Chane38287b2018-10-14 07:02:45 -04009754 if (BNXT_SUPPORTS_TPA(bp)) {
9755 bp->gro_func = bnxt_gro_func_5730x;
9756 if (BNXT_CHIP_P4(bp))
9757 bp->gro_func = bnxt_gro_func_5731x;
9758 }
9759 if (!BNXT_CHIP_P4_PLUS(bp))
Michael Chan434c9752017-05-29 19:06:08 -04009760 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04009761
Michael Chanc0c050c2015-10-22 16:01:17 -04009762 rc = bnxt_hwrm_func_drv_rgtr(bp);
9763 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05009764 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04009765
Michael Chana1653b12016-12-07 00:26:20 -05009766 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
9767 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05009768 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05009769
Michael Chana588e452016-12-07 00:26:21 -05009770 bp->ulp_probe = bnxt_ulp_probe;
9771
Michael Chan98f04cf2018-10-14 07:02:43 -04009772 rc = bnxt_hwrm_queue_qportcfg(bp);
9773 if (rc) {
9774 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
9775 rc);
9776 rc = -1;
9777 goto init_err_pci_clean;
9778 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009779 /* Get the MAX capabilities for this function */
9780 rc = bnxt_hwrm_func_qcaps(bp);
9781 if (rc) {
9782 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
9783 rc);
9784 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05009785 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04009786 }
Michael Chana22a6ac2017-08-23 19:34:05 -04009787 rc = bnxt_init_mac_addr(bp);
9788 if (rc) {
9789 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
9790 rc = -EADDRNOTAVAIL;
9791 goto init_err_pci_clean;
9792 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009793
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04009794 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05009795 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04009796 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04009797 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04009798
Michael Chan7eb9bb32017-10-26 11:51:25 -04009799 /* MTU range: 60 - FW defined max */
9800 dev->min_mtu = ETH_ZLEN;
9801 dev->max_mtu = bp->max_mtu;
9802
Michael Chand5430d32017-08-28 13:40:31 -04009803 rc = bnxt_probe_phy(bp);
9804 if (rc)
9805 goto init_err_pci_clean;
9806
Michael Chanc61fb992017-02-06 16:55:36 -05009807 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04009808 bnxt_set_tpa_flags(bp);
9809 bnxt_set_ring_params(bp);
Michael Chan702c2212017-05-29 19:06:10 -04009810 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009811 if (rc) {
9812 netdev_err(bp->dev, "Not enough rings available.\n");
9813 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05009814 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05009815 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009816
Michael Chan87da7f72016-11-16 21:13:09 -05009817 /* Default RSS hash cfg. */
9818 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
9819 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
9820 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
9821 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chane38287b2018-10-14 07:02:45 -04009822 if (BNXT_CHIP_P4(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05009823 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
9824 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
9825 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
9826 }
9827
Michael Chan8fdefd62016-12-29 12:13:36 -05009828 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05009829 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05009830 dev->hw_features |= NETIF_F_NTUPLE;
9831 if (bnxt_rfs_capable(bp)) {
9832 bp->flags |= BNXT_FLAG_RFS;
9833 dev->features |= NETIF_F_NTUPLE;
9834 }
9835 }
9836
Michael Chanc0c050c2015-10-22 16:01:17 -04009837 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
9838 bp->flags |= BNXT_FLAG_STRIP_VLAN;
9839
Michael Chan78095922016-12-07 00:26:16 -05009840 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009841 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05009842 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04009843
Michael Chan832aed12018-03-09 23:46:07 -05009844 /* No TC has been set yet and rings may have been trimmed due to
9845 * limited MSIX, so we re-initialize the TX rings per TC.
9846 */
9847 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9848
Michael Chanc1ef1462017-04-04 18:14:07 -04009849 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04009850 if (bp->flags & BNXT_FLAG_WOL_CAP)
9851 device_set_wakeup_enable(&pdev->dev, bp->wol);
9852 else
9853 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04009854
Michael Chanc3480a62018-01-17 03:21:15 -05009855 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
9856
Michael Chan74706af2018-10-14 07:02:40 -04009857 bnxt_hwrm_coal_params_qcaps(bp);
9858
Michael Chanc213eae2017-10-13 21:09:29 -04009859 if (BNXT_PF(bp)) {
9860 if (!bnxt_pf_wq) {
9861 bnxt_pf_wq =
9862 create_singlethread_workqueue("bnxt_pf_wq");
9863 if (!bnxt_pf_wq) {
9864 dev_err(&pdev->dev, "Unable to create workqueue.\n");
9865 goto init_err_pci_clean;
9866 }
9867 }
Sathya Perla2ae74082017-08-28 13:40:33 -04009868 bnxt_init_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04009869 }
Sathya Perla2ae74082017-08-28 13:40:33 -04009870
Michael Chan78095922016-12-07 00:26:16 -05009871 rc = register_netdev(dev);
9872 if (rc)
Sathya Perla2ae74082017-08-28 13:40:33 -04009873 goto init_err_cleanup_tc;
Michael Chan78095922016-12-07 00:26:16 -05009874
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009875 if (BNXT_PF(bp))
9876 bnxt_dl_register(bp);
9877
Michael Chanc0c050c2015-10-22 16:01:17 -04009878 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
9879 board_info[ent->driver_data].name,
9880 (long)pci_resource_start(pdev, 0), dev->dev_addr);
Bjorn Helgaasaf125b72018-03-30 14:09:54 -05009881 pcie_print_link_status(pdev);
Ajit Khaparde90c4f782016-05-15 03:04:45 -04009882
Michael Chanc0c050c2015-10-22 16:01:17 -04009883 return 0;
9884
Sathya Perla2ae74082017-08-28 13:40:33 -04009885init_err_cleanup_tc:
9886 bnxt_shutdown_tc(bp);
Michael Chan78095922016-12-07 00:26:16 -05009887 bnxt_clear_int_mode(bp);
9888
Sathya Perla17086392017-02-20 19:25:18 -05009889init_err_pci_clean:
Venkat Duvvurua2bf74f2018-10-05 00:26:02 -04009890 bnxt_free_hwrm_resources(bp);
Michael Chan98f04cf2018-10-14 07:02:43 -04009891 bnxt_free_ctx_mem(bp);
9892 kfree(bp->ctx);
9893 bp->ctx = NULL;
Sathya Perla17086392017-02-20 19:25:18 -05009894 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009895
9896init_err_free:
9897 free_netdev(dev);
9898 return rc;
9899}
9900
Michael Chand196ece2017-04-04 18:14:08 -04009901static void bnxt_shutdown(struct pci_dev *pdev)
9902{
9903 struct net_device *dev = pci_get_drvdata(pdev);
9904 struct bnxt *bp;
9905
9906 if (!dev)
9907 return;
9908
9909 rtnl_lock();
9910 bp = netdev_priv(dev);
9911 if (!bp)
9912 goto shutdown_exit;
9913
9914 if (netif_running(dev))
9915 dev_close(dev);
9916
Ray Juia7f3f932017-12-01 03:13:02 -05009917 bnxt_ulp_shutdown(bp);
9918
Michael Chand196ece2017-04-04 18:14:08 -04009919 if (system_state == SYSTEM_POWER_OFF) {
9920 bnxt_clear_int_mode(bp);
9921 pci_wake_from_d3(pdev, bp->wol);
9922 pci_set_power_state(pdev, PCI_D3hot);
9923 }
9924
9925shutdown_exit:
9926 rtnl_unlock();
9927}
9928
Michael Chanf65a2042017-04-04 18:14:11 -04009929#ifdef CONFIG_PM_SLEEP
9930static int bnxt_suspend(struct device *device)
9931{
9932 struct pci_dev *pdev = to_pci_dev(device);
9933 struct net_device *dev = pci_get_drvdata(pdev);
9934 struct bnxt *bp = netdev_priv(dev);
9935 int rc = 0;
9936
9937 rtnl_lock();
9938 if (netif_running(dev)) {
9939 netif_device_detach(dev);
9940 rc = bnxt_close(dev);
9941 }
9942 bnxt_hwrm_func_drv_unrgtr(bp);
9943 rtnl_unlock();
9944 return rc;
9945}
9946
9947static int bnxt_resume(struct device *device)
9948{
9949 struct pci_dev *pdev = to_pci_dev(device);
9950 struct net_device *dev = pci_get_drvdata(pdev);
9951 struct bnxt *bp = netdev_priv(dev);
9952 int rc = 0;
9953
9954 rtnl_lock();
9955 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
9956 rc = -ENODEV;
9957 goto resume_exit;
9958 }
9959 rc = bnxt_hwrm_func_reset(bp);
9960 if (rc) {
9961 rc = -EBUSY;
9962 goto resume_exit;
9963 }
9964 bnxt_get_wol_settings(bp);
9965 if (netif_running(dev)) {
9966 rc = bnxt_open(dev);
9967 if (!rc)
9968 netif_device_attach(dev);
9969 }
9970
9971resume_exit:
9972 rtnl_unlock();
9973 return rc;
9974}
9975
9976static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
9977#define BNXT_PM_OPS (&bnxt_pm_ops)
9978
9979#else
9980
9981#define BNXT_PM_OPS NULL
9982
9983#endif /* CONFIG_PM_SLEEP */
9984
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009985/**
9986 * bnxt_io_error_detected - called when PCI error is detected
9987 * @pdev: Pointer to PCI device
9988 * @state: The current pci connection state
9989 *
9990 * This function is called after a PCI bus error affecting
9991 * this device has been detected.
9992 */
9993static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
9994 pci_channel_state_t state)
9995{
9996 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05009997 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009998
9999 netdev_info(netdev, "PCI I/O error detected\n");
10000
10001 rtnl_lock();
10002 netif_device_detach(netdev);
10003
Michael Chana588e452016-12-07 00:26:21 -050010004 bnxt_ulp_stop(bp);
10005
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010006 if (state == pci_channel_io_perm_failure) {
10007 rtnl_unlock();
10008 return PCI_ERS_RESULT_DISCONNECT;
10009 }
10010
10011 if (netif_running(netdev))
10012 bnxt_close(netdev);
10013
10014 pci_disable_device(pdev);
10015 rtnl_unlock();
10016
10017 /* Request a slot slot reset. */
10018 return PCI_ERS_RESULT_NEED_RESET;
10019}
10020
10021/**
10022 * bnxt_io_slot_reset - called after the pci bus has been reset.
10023 * @pdev: Pointer to PCI device
10024 *
10025 * Restart the card from scratch, as if from a cold-boot.
10026 * At this point, the card has exprienced a hard reset,
10027 * followed by fixups by BIOS, and has its config space
10028 * set up identically to what it was at cold boot.
10029 */
10030static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
10031{
10032 struct net_device *netdev = pci_get_drvdata(pdev);
10033 struct bnxt *bp = netdev_priv(netdev);
10034 int err = 0;
10035 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
10036
10037 netdev_info(bp->dev, "PCI Slot Reset\n");
10038
10039 rtnl_lock();
10040
10041 if (pci_enable_device(pdev)) {
10042 dev_err(&pdev->dev,
10043 "Cannot re-enable PCI device after reset.\n");
10044 } else {
10045 pci_set_master(pdev);
10046
Michael Chanaa8ed022016-12-07 00:26:17 -050010047 err = bnxt_hwrm_func_reset(bp);
10048 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010049 err = bnxt_open(netdev);
10050
Michael Chana588e452016-12-07 00:26:21 -050010051 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010052 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -050010053 bnxt_ulp_start(bp);
10054 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010055 }
10056
10057 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
10058 dev_close(netdev);
10059
10060 rtnl_unlock();
10061
10062 err = pci_cleanup_aer_uncorrect_error_status(pdev);
10063 if (err) {
10064 dev_err(&pdev->dev,
10065 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
10066 err); /* non-fatal, continue */
10067 }
10068
10069 return PCI_ERS_RESULT_RECOVERED;
10070}
10071
10072/**
10073 * bnxt_io_resume - called when traffic can start flowing again.
10074 * @pdev: Pointer to PCI device
10075 *
10076 * This callback is called when the error recovery driver tells
10077 * us that its OK to resume normal operation.
10078 */
10079static void bnxt_io_resume(struct pci_dev *pdev)
10080{
10081 struct net_device *netdev = pci_get_drvdata(pdev);
10082
10083 rtnl_lock();
10084
10085 netif_device_attach(netdev);
10086
10087 rtnl_unlock();
10088}
10089
10090static const struct pci_error_handlers bnxt_err_handler = {
10091 .error_detected = bnxt_io_error_detected,
10092 .slot_reset = bnxt_io_slot_reset,
10093 .resume = bnxt_io_resume
10094};
10095
Michael Chanc0c050c2015-10-22 16:01:17 -040010096static struct pci_driver bnxt_pci_driver = {
10097 .name = DRV_MODULE_NAME,
10098 .id_table = bnxt_pci_tbl,
10099 .probe = bnxt_init_one,
10100 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -040010101 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -040010102 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010103 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -040010104#if defined(CONFIG_BNXT_SRIOV)
10105 .sriov_configure = bnxt_sriov_configure,
10106#endif
10107};
10108
Michael Chanc213eae2017-10-13 21:09:29 -040010109static int __init bnxt_init(void)
10110{
Andy Gospodarekcabfb092018-04-26 17:44:40 -040010111 bnxt_debug_init();
Michael Chanc213eae2017-10-13 21:09:29 -040010112 return pci_register_driver(&bnxt_pci_driver);
10113}
10114
10115static void __exit bnxt_exit(void)
10116{
10117 pci_unregister_driver(&bnxt_pci_driver);
10118 if (bnxt_pf_wq)
10119 destroy_workqueue(bnxt_pf_wq);
Andy Gospodarekcabfb092018-04-26 17:44:40 -040010120 bnxt_debug_exit();
Michael Chanc213eae2017-10-13 21:09:29 -040010121}
10122
10123module_init(bnxt_init);
10124module_exit(bnxt_exit);