blob: 46bbe42412d3f94f05f231da551be332bcd10fea [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,
Michael Chan1ab968d2018-10-14 07:02:59 -0400114 BCM57508,
Ray Jui4a581392017-08-28 13:40:28 -0400115 BCM58802,
Ray Jui8ed693b2017-10-26 11:51:20 -0400116 BCM58804,
Ray Jui4a581392017-08-28 13:40:28 -0400117 BCM58808,
Michael Chanadbc8302016-09-19 03:58:01 -0400118 NETXTREME_E_VF,
119 NETXTREME_C_VF,
Rob Miller618784e2017-10-26 11:51:21 -0400120 NETXTREME_S_VF,
Michael Chanb16b6892018-12-16 18:46:25 -0500121 NETXTREME_E_P5_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400122};
123
124/* indexed by enum above */
125static const struct {
126 char *name;
127} board_info[] = {
Scott Branden27573a72017-08-28 13:40:29 -0400128 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
129 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
130 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
132 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
133 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
134 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
135 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
136 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
137 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
138 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
139 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
140 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
141 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
142 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
143 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
144 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
145 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
146 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
147 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
148 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
149 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
150 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
151 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
152 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
153 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
154 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
155 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Vasundhara Volam92abef32018-01-17 03:21:13 -0500156 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
Michael Chan1ab968d2018-10-14 07:02:59 -0400157 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400158 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
Ray Jui8ed693b2017-10-26 11:51:20 -0400159 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400160 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
161 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
162 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Rob Miller618784e2017-10-26 11:51:21 -0400163 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
Michael Chanb16b6892018-12-16 18:46:25 -0500164 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400165};
166
167static const struct pci_device_id bnxt_pci_tbl[] = {
Vasundhara Volam92abef32018-01-17 03:21:13 -0500168 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
169 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400170 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanadbc8302016-09-19 03:58:01 -0400171 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500172 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400173 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
174 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400175 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400176 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400177 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
178 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500179 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400180 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
181 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400182 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
183 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400184 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
185 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
186 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
187 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400188 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400189 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400190 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
191 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
192 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
193 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
194 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400195 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
196 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400197 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400198 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400199 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400200 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400201 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
Deepak Khungar32b40792017-02-12 19:18:18 -0500202 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
Michael Chan1ab968d2018-10-14 07:02:59 -0400203 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
Ray Jui4a581392017-08-28 13:40:28 -0400204 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
Ray Jui8ed693b2017-10-26 11:51:20 -0400205 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400206#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400207 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
208 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400209 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
210 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanb16b6892018-12-16 18:46:25 -0500215 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
Rob Miller618784e2017-10-26 11:51:21 -0400216 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400217#endif
218 { 0 }
219};
220
221MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222
223static const u16 bnxt_vf_req_snif[] = {
224 HWRM_FUNC_CFG,
Vasundhara Volam91cdda42018-01-17 03:21:14 -0500225 HWRM_FUNC_VF_CFG,
Michael Chanc0c050c2015-10-22 16:01:17 -0400226 HWRM_PORT_PHY_QCFG,
227 HWRM_CFA_L2_FILTER_ALLOC,
228};
229
Michael Chan25be8622016-04-05 14:09:00 -0400230static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500231 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
233 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
234 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
235 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400236};
237
Michael Chanc213eae2017-10-13 21:09:29 -0400238static struct workqueue_struct *bnxt_pf_wq;
239
Michael Chanc0c050c2015-10-22 16:01:17 -0400240static bool bnxt_vf_pciid(enum board_idx idx)
241{
Rob Miller618784e2017-10-26 11:51:21 -0400242 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
Michael Chanb16b6892018-12-16 18:46:25 -0500243 idx == NETXTREME_S_VF || idx == NETXTREME_E_P5_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400244}
245
246#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
247#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
248#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
249
Michael Chanc0c050c2015-10-22 16:01:17 -0400250#define BNXT_CP_DB_IRQ_DIS(db) \
251 writel(DB_CP_IRQ_DIS_FLAGS, db)
252
Michael Chan697197e2018-10-14 07:02:46 -0400253#define BNXT_DB_CQ(db, idx) \
254 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
255
256#define BNXT_DB_NQ_P5(db, idx) \
257 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
258
259#define BNXT_DB_CQ_ARM(db, idx) \
260 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
261
262#define BNXT_DB_NQ_ARM_P5(db, idx) \
263 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
264
265static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
266{
267 if (bp->flags & BNXT_FLAG_CHIP_P5)
268 BNXT_DB_NQ_P5(db, idx);
269 else
270 BNXT_DB_CQ(db, idx);
271}
272
273static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
274{
275 if (bp->flags & BNXT_FLAG_CHIP_P5)
276 BNXT_DB_NQ_ARM_P5(db, idx);
277 else
278 BNXT_DB_CQ_ARM(db, idx);
279}
280
281static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
282{
283 if (bp->flags & BNXT_FLAG_CHIP_P5)
284 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
285 db->doorbell);
286 else
287 BNXT_DB_CQ(db, idx);
288}
289
Michael Chan38413402017-02-06 16:55:43 -0500290const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400291 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
292 TX_BD_FLAGS_LHINT_512_TO_1023,
293 TX_BD_FLAGS_LHINT_1024_TO_2047,
294 TX_BD_FLAGS_LHINT_1024_TO_2047,
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 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
305 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
306 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
307 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
308 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
309 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
310};
311
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400312static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
313{
314 struct metadata_dst *md_dst = skb_metadata_dst(skb);
315
316 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
317 return 0;
318
319 return md_dst->u.port_info.port_id;
320}
321
Michael Chanc0c050c2015-10-22 16:01:17 -0400322static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
323{
324 struct bnxt *bp = netdev_priv(dev);
325 struct tx_bd *txbd;
326 struct tx_bd_ext *txbd1;
327 struct netdev_queue *txq;
328 int i;
329 dma_addr_t mapping;
330 unsigned int length, pad = 0;
331 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
332 u16 prod, last_frag;
333 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400334 struct bnxt_tx_ring_info *txr;
335 struct bnxt_sw_tx_bd *tx_buf;
336
337 i = skb_get_queue_mapping(skb);
338 if (unlikely(i >= bp->tx_nr_rings)) {
339 dev_kfree_skb_any(skb);
340 return NETDEV_TX_OK;
341 }
342
Michael Chanc0c050c2015-10-22 16:01:17 -0400343 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500344 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = txr->tx_prod;
346
347 free_size = bnxt_tx_avail(bp, txr);
348 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
349 netif_tx_stop_queue(txq);
350 return NETDEV_TX_BUSY;
351 }
352
353 length = skb->len;
354 len = skb_headlen(skb);
355 last_frag = skb_shinfo(skb)->nr_frags;
356
357 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
358
359 txbd->tx_bd_opaque = prod;
360
361 tx_buf = &txr->tx_buf_ring[prod];
362 tx_buf->skb = skb;
363 tx_buf->nr_frags = last_frag;
364
365 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400366 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 if (skb_vlan_tag_present(skb)) {
368 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
369 skb_vlan_tag_get(skb);
370 /* Currently supports 8021Q, 8021AD vlan offloads
371 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
372 */
373 if (skb->vlan_proto == htons(ETH_P_8021Q))
374 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
375 }
376
377 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500378 struct tx_push_buffer *tx_push_buf = txr->tx_push;
379 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
380 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
Michael Chan697197e2018-10-14 07:02:46 -0400381 void __iomem *db = txr->tx_db.doorbell;
Michael Chan4419dbe2016-02-10 17:33:49 -0500382 void *pdata = tx_push_buf->data;
383 u64 *end;
384 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400385
386 /* Set COAL_NOW to be ready quickly for the next push */
387 tx_push->tx_bd_len_flags_type =
388 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
389 TX_BD_TYPE_LONG_TX_BD |
390 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
391 TX_BD_FLAGS_COAL_NOW |
392 TX_BD_FLAGS_PACKET_END |
393 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
394
395 if (skb->ip_summed == CHECKSUM_PARTIAL)
396 tx_push1->tx_bd_hsize_lflags =
397 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
398 else
399 tx_push1->tx_bd_hsize_lflags = 0;
400
401 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400402 tx_push1->tx_bd_cfa_action =
403 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400404
Michael Chanfbb0fa82016-02-22 02:10:26 -0500405 end = pdata + length;
406 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500407 *end = 0;
408
Michael Chanc0c050c2015-10-22 16:01:17 -0400409 skb_copy_from_linear_data(skb, pdata, len);
410 pdata += len;
411 for (j = 0; j < last_frag; j++) {
412 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
413 void *fptr;
414
415 fptr = skb_frag_address_safe(frag);
416 if (!fptr)
417 goto normal_tx;
418
419 memcpy(pdata, fptr, skb_frag_size(frag));
420 pdata += skb_frag_size(frag);
421 }
422
Michael Chan4419dbe2016-02-10 17:33:49 -0500423 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
424 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400425 prod = NEXT_TX(prod);
426 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
427 memcpy(txbd, tx_push1, sizeof(*txbd));
428 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500429 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400430 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
431 txr->tx_prod = prod;
432
Michael Chanb9a84602016-06-06 02:37:14 -0400433 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400434 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400435 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400436
Michael Chan4419dbe2016-02-10 17:33:49 -0500437 push_len = (length + sizeof(*tx_push) + 7) / 8;
438 if (push_len > 16) {
Michael Chan697197e2018-10-14 07:02:46 -0400439 __iowrite64_copy(db, tx_push_buf, 16);
440 __iowrite32_copy(db + 4, tx_push_buf + 1,
Michael Chan9d137442016-09-05 01:57:35 -0400441 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500442 } else {
Michael Chan697197e2018-10-14 07:02:46 -0400443 __iowrite64_copy(db, tx_push_buf, push_len);
Michael Chan4419dbe2016-02-10 17:33:49 -0500444 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400445
Michael Chanc0c050c2015-10-22 16:01:17 -0400446 goto tx_done;
447 }
448
449normal_tx:
450 if (length < BNXT_MIN_PKT_SIZE) {
451 pad = BNXT_MIN_PKT_SIZE - length;
452 if (skb_pad(skb, pad)) {
453 /* SKB already freed. */
454 tx_buf->skb = NULL;
455 return NETDEV_TX_OK;
456 }
457 length = BNXT_MIN_PKT_SIZE;
458 }
459
460 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
461
462 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
463 dev_kfree_skb_any(skb);
464 tx_buf->skb = NULL;
465 return NETDEV_TX_OK;
466 }
467
468 dma_unmap_addr_set(tx_buf, mapping, mapping);
469 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
470 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
471
472 txbd->tx_bd_haddr = cpu_to_le64(mapping);
473
474 prod = NEXT_TX(prod);
475 txbd1 = (struct tx_bd_ext *)
476 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
477
478 txbd1->tx_bd_hsize_lflags = 0;
479 if (skb_is_gso(skb)) {
480 u32 hdr_len;
481
482 if (skb->encapsulation)
483 hdr_len = skb_inner_network_offset(skb) +
484 skb_inner_network_header_len(skb) +
485 inner_tcp_hdrlen(skb);
486 else
487 hdr_len = skb_transport_offset(skb) +
488 tcp_hdrlen(skb);
489
490 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
491 TX_BD_FLAGS_T_IPID |
492 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
493 length = skb_shinfo(skb)->gso_size;
494 txbd1->tx_bd_mss = cpu_to_le32(length);
495 length += hdr_len;
496 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
497 txbd1->tx_bd_hsize_lflags =
498 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
499 txbd1->tx_bd_mss = 0;
500 }
501
502 length >>= 9;
503 flags |= bnxt_lhint_arr[length];
504 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
505
506 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400507 txbd1->tx_bd_cfa_action =
508 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400509 for (i = 0; i < last_frag; i++) {
510 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
511
512 prod = NEXT_TX(prod);
513 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
514
515 len = skb_frag_size(frag);
516 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
517 DMA_TO_DEVICE);
518
519 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
520 goto tx_dma_error;
521
522 tx_buf = &txr->tx_buf_ring[prod];
523 dma_unmap_addr_set(tx_buf, mapping, mapping);
524
525 txbd->tx_bd_haddr = cpu_to_le64(mapping);
526
527 flags = len << TX_BD_LEN_SHIFT;
528 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
529 }
530
531 flags &= ~TX_BD_LEN;
532 txbd->tx_bd_len_flags_type =
533 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
534 TX_BD_FLAGS_PACKET_END);
535
536 netdev_tx_sent_queue(txq, skb->len);
537
538 /* Sync BD data before updating doorbell */
539 wmb();
540
541 prod = NEXT_TX(prod);
542 txr->tx_prod = prod;
543
Michael Chanffe40642017-05-30 20:03:00 -0400544 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan697197e2018-10-14 07:02:46 -0400545 bnxt_db_write(bp, &txr->tx_db, prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400546
547tx_done:
548
549 mmiowb();
550
551 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400552 if (skb->xmit_more && !tx_buf->is_push)
Michael Chan697197e2018-10-14 07:02:46 -0400553 bnxt_db_write(bp, &txr->tx_db, prod);
Michael Chan4d172f22017-05-29 19:06:09 -0400554
Michael Chanc0c050c2015-10-22 16:01:17 -0400555 netif_tx_stop_queue(txq);
556
557 /* netif_tx_stop_queue() must be done before checking
558 * tx index in bnxt_tx_avail() below, because in
559 * bnxt_tx_int(), we update tx index before checking for
560 * netif_tx_queue_stopped().
561 */
562 smp_mb();
563 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
564 netif_tx_wake_queue(txq);
565 }
566 return NETDEV_TX_OK;
567
568tx_dma_error:
569 last_frag = i;
570
571 /* start back at beginning and unmap skb */
572 prod = txr->tx_prod;
573 tx_buf = &txr->tx_buf_ring[prod];
574 tx_buf->skb = NULL;
575 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
576 skb_headlen(skb), PCI_DMA_TODEVICE);
577 prod = NEXT_TX(prod);
578
579 /* unmap remaining mapped pages */
580 for (i = 0; i < last_frag; i++) {
581 prod = NEXT_TX(prod);
582 tx_buf = &txr->tx_buf_ring[prod];
583 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
584 skb_frag_size(&skb_shinfo(skb)->frags[i]),
585 PCI_DMA_TODEVICE);
586 }
587
588 dev_kfree_skb_any(skb);
589 return NETDEV_TX_OK;
590}
591
592static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
593{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500594 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500595 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400596 u16 cons = txr->tx_cons;
597 struct pci_dev *pdev = bp->pdev;
598 int i;
599 unsigned int tx_bytes = 0;
600
601 for (i = 0; i < nr_pkts; i++) {
602 struct bnxt_sw_tx_bd *tx_buf;
603 struct sk_buff *skb;
604 int j, last;
605
606 tx_buf = &txr->tx_buf_ring[cons];
607 cons = NEXT_TX(cons);
608 skb = tx_buf->skb;
609 tx_buf->skb = NULL;
610
611 if (tx_buf->is_push) {
612 tx_buf->is_push = 0;
613 goto next_tx_int;
614 }
615
616 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
617 skb_headlen(skb), PCI_DMA_TODEVICE);
618 last = tx_buf->nr_frags;
619
620 for (j = 0; j < last; j++) {
621 cons = NEXT_TX(cons);
622 tx_buf = &txr->tx_buf_ring[cons];
623 dma_unmap_page(
624 &pdev->dev,
625 dma_unmap_addr(tx_buf, mapping),
626 skb_frag_size(&skb_shinfo(skb)->frags[j]),
627 PCI_DMA_TODEVICE);
628 }
629
630next_tx_int:
631 cons = NEXT_TX(cons);
632
633 tx_bytes += skb->len;
634 dev_kfree_skb_any(skb);
635 }
636
637 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
638 txr->tx_cons = cons;
639
640 /* Need to make the tx_cons update visible to bnxt_start_xmit()
641 * before checking for netif_tx_queue_stopped(). Without the
642 * memory barrier, there is a small possibility that bnxt_start_xmit()
643 * will miss it and cause the queue to be stopped forever.
644 */
645 smp_mb();
646
647 if (unlikely(netif_tx_queue_stopped(txq)) &&
648 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
649 __netif_tx_lock(txq, smp_processor_id());
650 if (netif_tx_queue_stopped(txq) &&
651 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
652 txr->dev_state != BNXT_DEV_STATE_CLOSING)
653 netif_tx_wake_queue(txq);
654 __netif_tx_unlock(txq);
655 }
656}
657
Michael Chanc61fb992017-02-06 16:55:36 -0500658static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
659 gfp_t gfp)
660{
661 struct device *dev = &bp->pdev->dev;
662 struct page *page;
663
664 page = alloc_page(gfp);
665 if (!page)
666 return NULL;
667
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700668 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
669 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500670 if (dma_mapping_error(dev, *mapping)) {
671 __free_page(page);
672 return NULL;
673 }
674 *mapping += bp->rx_dma_offset;
675 return page;
676}
677
Michael Chanc0c050c2015-10-22 16:01:17 -0400678static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
679 gfp_t gfp)
680{
681 u8 *data;
682 struct pci_dev *pdev = bp->pdev;
683
684 data = kmalloc(bp->rx_buf_size, gfp);
685 if (!data)
686 return NULL;
687
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700688 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
689 bp->rx_buf_use_size, bp->rx_dir,
690 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400691
692 if (dma_mapping_error(&pdev->dev, *mapping)) {
693 kfree(data);
694 data = NULL;
695 }
696 return data;
697}
698
Michael Chan38413402017-02-06 16:55:43 -0500699int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
700 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400701{
702 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
703 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400704 dma_addr_t mapping;
705
Michael Chanc61fb992017-02-06 16:55:36 -0500706 if (BNXT_RX_PAGE_MODE(bp)) {
707 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400708
Michael Chanc61fb992017-02-06 16:55:36 -0500709 if (!page)
710 return -ENOMEM;
711
712 rx_buf->data = page;
713 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
714 } else {
715 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
716
717 if (!data)
718 return -ENOMEM;
719
720 rx_buf->data = data;
721 rx_buf->data_ptr = data + bp->rx_offset;
722 }
Michael Chan11cd1192017-02-06 16:55:33 -0500723 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400724
725 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400726 return 0;
727}
728
Michael Chanc6d30e82017-02-06 16:55:42 -0500729void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400730{
731 u16 prod = rxr->rx_prod;
732 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
733 struct rx_bd *cons_bd, *prod_bd;
734
735 prod_rx_buf = &rxr->rx_buf_ring[prod];
736 cons_rx_buf = &rxr->rx_buf_ring[cons];
737
738 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500739 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400740
Michael Chan11cd1192017-02-06 16:55:33 -0500741 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400742
743 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
744 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
745
746 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
747}
748
749static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
750{
751 u16 next, max = rxr->rx_agg_bmap_size;
752
753 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
754 if (next >= max)
755 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
756 return next;
757}
758
759static inline int bnxt_alloc_rx_page(struct bnxt *bp,
760 struct bnxt_rx_ring_info *rxr,
761 u16 prod, gfp_t gfp)
762{
763 struct rx_bd *rxbd =
764 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
765 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
766 struct pci_dev *pdev = bp->pdev;
767 struct page *page;
768 dma_addr_t mapping;
769 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400770 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400771
Michael Chan89d0a062016-04-25 02:30:51 -0400772 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
773 page = rxr->rx_page;
774 if (!page) {
775 page = alloc_page(gfp);
776 if (!page)
777 return -ENOMEM;
778 rxr->rx_page = page;
779 rxr->rx_page_offset = 0;
780 }
781 offset = rxr->rx_page_offset;
782 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
783 if (rxr->rx_page_offset == PAGE_SIZE)
784 rxr->rx_page = NULL;
785 else
786 get_page(page);
787 } else {
788 page = alloc_page(gfp);
789 if (!page)
790 return -ENOMEM;
791 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400792
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700793 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
794 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
795 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400796 if (dma_mapping_error(&pdev->dev, mapping)) {
797 __free_page(page);
798 return -EIO;
799 }
800
801 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
802 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
803
804 __set_bit(sw_prod, rxr->rx_agg_bmap);
805 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
806 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
807
808 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400809 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400810 rx_agg_buf->mapping = mapping;
811 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
812 rxbd->rx_bd_opaque = sw_prod;
813 return 0;
814}
815
Michael Chane44758b2018-10-14 07:02:55 -0400816static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 cp_cons,
Michael Chanc0c050c2015-10-22 16:01:17 -0400817 u32 agg_bufs)
818{
Michael Chane44758b2018-10-14 07:02:55 -0400819 struct bnxt_napi *bnapi = cpr->bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -0400820 struct bnxt *bp = bnapi->bp;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500821 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400822 u16 prod = rxr->rx_agg_prod;
823 u16 sw_prod = rxr->rx_sw_agg_prod;
824 u32 i;
825
826 for (i = 0; i < agg_bufs; i++) {
827 u16 cons;
828 struct rx_agg_cmp *agg;
829 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
830 struct rx_bd *prod_bd;
831 struct page *page;
832
833 agg = (struct rx_agg_cmp *)
834 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
835 cons = agg->rx_agg_cmp_opaque;
836 __clear_bit(cons, rxr->rx_agg_bmap);
837
838 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
839 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
840
841 __set_bit(sw_prod, rxr->rx_agg_bmap);
842 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
843 cons_rx_buf = &rxr->rx_agg_ring[cons];
844
845 /* It is possible for sw_prod to be equal to cons, so
846 * set cons_rx_buf->page to NULL first.
847 */
848 page = cons_rx_buf->page;
849 cons_rx_buf->page = NULL;
850 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400851 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400852
853 prod_rx_buf->mapping = cons_rx_buf->mapping;
854
855 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
856
857 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
858 prod_bd->rx_bd_opaque = sw_prod;
859
860 prod = NEXT_RX_AGG(prod);
861 sw_prod = NEXT_RX_AGG(sw_prod);
862 cp_cons = NEXT_CMP(cp_cons);
863 }
864 rxr->rx_agg_prod = prod;
865 rxr->rx_sw_agg_prod = sw_prod;
866}
867
Michael Chanc61fb992017-02-06 16:55:36 -0500868static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
869 struct bnxt_rx_ring_info *rxr,
870 u16 cons, void *data, u8 *data_ptr,
871 dma_addr_t dma_addr,
872 unsigned int offset_and_len)
873{
874 unsigned int payload = offset_and_len >> 16;
875 unsigned int len = offset_and_len & 0xffff;
876 struct skb_frag_struct *frag;
877 struct page *page = data;
878 u16 prod = rxr->rx_prod;
879 struct sk_buff *skb;
880 int off, err;
881
882 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
883 if (unlikely(err)) {
884 bnxt_reuse_rx_data(rxr, cons, data);
885 return NULL;
886 }
887 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700888 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
889 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500890
891 if (unlikely(!payload))
892 payload = eth_get_headlen(data_ptr, len);
893
894 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
895 if (!skb) {
896 __free_page(page);
897 return NULL;
898 }
899
900 off = (void *)data_ptr - page_address(page);
901 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
902 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
903 payload + NET_IP_ALIGN);
904
905 frag = &skb_shinfo(skb)->frags[0];
906 skb_frag_size_sub(frag, payload);
907 frag->page_offset += payload;
908 skb->data_len -= payload;
909 skb->tail += payload;
910
911 return skb;
912}
913
Michael Chanc0c050c2015-10-22 16:01:17 -0400914static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
915 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500916 void *data, u8 *data_ptr,
917 dma_addr_t dma_addr,
918 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400919{
Michael Chan6bb19472017-02-06 16:55:32 -0500920 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400921 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500922 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400923
924 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
925 if (unlikely(err)) {
926 bnxt_reuse_rx_data(rxr, cons, data);
927 return NULL;
928 }
929
930 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700931 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
932 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400933 if (!skb) {
934 kfree(data);
935 return NULL;
936 }
937
Michael Chanb3dba772017-02-06 16:55:35 -0500938 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500939 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400940 return skb;
941}
942
Michael Chane44758b2018-10-14 07:02:55 -0400943static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
944 struct bnxt_cp_ring_info *cpr,
Michael Chanc0c050c2015-10-22 16:01:17 -0400945 struct sk_buff *skb, u16 cp_cons,
946 u32 agg_bufs)
947{
Michael Chane44758b2018-10-14 07:02:55 -0400948 struct bnxt_napi *bnapi = cpr->bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -0400949 struct pci_dev *pdev = bp->pdev;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500950 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400951 u16 prod = rxr->rx_agg_prod;
952 u32 i;
953
954 for (i = 0; i < agg_bufs; i++) {
955 u16 cons, frag_len;
956 struct rx_agg_cmp *agg;
957 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
958 struct page *page;
959 dma_addr_t mapping;
960
961 agg = (struct rx_agg_cmp *)
962 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
963 cons = agg->rx_agg_cmp_opaque;
964 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
965 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
966
967 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400968 skb_fill_page_desc(skb, i, cons_rx_buf->page,
969 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400970 __clear_bit(cons, rxr->rx_agg_bmap);
971
972 /* It is possible for bnxt_alloc_rx_page() to allocate
973 * a sw_prod index that equals the cons index, so we
974 * need to clear the cons entry now.
975 */
Michael Chan11cd1192017-02-06 16:55:33 -0500976 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400977 page = cons_rx_buf->page;
978 cons_rx_buf->page = NULL;
979
980 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
981 struct skb_shared_info *shinfo;
982 unsigned int nr_frags;
983
984 shinfo = skb_shinfo(skb);
985 nr_frags = --shinfo->nr_frags;
986 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
987
988 dev_kfree_skb(skb);
989
990 cons_rx_buf->page = page;
991
992 /* Update prod since possibly some pages have been
993 * allocated already.
994 */
995 rxr->rx_agg_prod = prod;
Michael Chane44758b2018-10-14 07:02:55 -0400996 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs - i);
Michael Chanc0c050c2015-10-22 16:01:17 -0400997 return NULL;
998 }
999
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001000 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1001 PCI_DMA_FROMDEVICE,
1002 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001003
1004 skb->data_len += frag_len;
1005 skb->len += frag_len;
1006 skb->truesize += PAGE_SIZE;
1007
1008 prod = NEXT_RX_AGG(prod);
1009 cp_cons = NEXT_CMP(cp_cons);
1010 }
1011 rxr->rx_agg_prod = prod;
1012 return skb;
1013}
1014
1015static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1016 u8 agg_bufs, u32 *raw_cons)
1017{
1018 u16 last;
1019 struct rx_agg_cmp *agg;
1020
1021 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1022 last = RING_CMP(*raw_cons);
1023 agg = (struct rx_agg_cmp *)
1024 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1025 return RX_AGG_CMP_VALID(agg, *raw_cons);
1026}
1027
1028static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1029 unsigned int len,
1030 dma_addr_t mapping)
1031{
1032 struct bnxt *bp = bnapi->bp;
1033 struct pci_dev *pdev = bp->pdev;
1034 struct sk_buff *skb;
1035
1036 skb = napi_alloc_skb(&bnapi->napi, len);
1037 if (!skb)
1038 return NULL;
1039
Michael Chan745fc052017-02-06 16:55:34 -05001040 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1041 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001042
Michael Chan6bb19472017-02-06 16:55:32 -05001043 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1044 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -04001045
Michael Chan745fc052017-02-06 16:55:34 -05001046 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1047 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001048
1049 skb_put(skb, len);
1050 return skb;
1051}
1052
Michael Chane44758b2018-10-14 07:02:55 -04001053static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
Michael Chanfa7e2812016-05-10 19:18:00 -04001054 u32 *raw_cons, void *cmp)
1055{
Michael Chanfa7e2812016-05-10 19:18:00 -04001056 struct rx_cmp *rxcmp = cmp;
1057 u32 tmp_raw_cons = *raw_cons;
1058 u8 cmp_type, agg_bufs = 0;
1059
1060 cmp_type = RX_CMP_TYPE(rxcmp);
1061
1062 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1063 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1064 RX_CMP_AGG_BUFS) >>
1065 RX_CMP_AGG_BUFS_SHIFT;
1066 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1067 struct rx_tpa_end_cmp *tpa_end = cmp;
1068
1069 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1070 RX_TPA_END_CMP_AGG_BUFS) >>
1071 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1072 }
1073
1074 if (agg_bufs) {
1075 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1076 return -EBUSY;
1077 }
1078 *raw_cons = tmp_raw_cons;
1079 return 0;
1080}
1081
Michael Chanc213eae2017-10-13 21:09:29 -04001082static void bnxt_queue_sp_work(struct bnxt *bp)
1083{
1084 if (BNXT_PF(bp))
1085 queue_work(bnxt_pf_wq, &bp->sp_task);
1086 else
1087 schedule_work(&bp->sp_task);
1088}
1089
1090static void bnxt_cancel_sp_work(struct bnxt *bp)
1091{
1092 if (BNXT_PF(bp))
1093 flush_workqueue(bnxt_pf_wq);
1094 else
1095 cancel_work_sync(&bp->sp_task);
1096}
1097
Michael Chanfa7e2812016-05-10 19:18:00 -04001098static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1099{
1100 if (!rxr->bnapi->in_reset) {
1101 rxr->bnapi->in_reset = true;
1102 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001103 bnxt_queue_sp_work(bp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001104 }
1105 rxr->rx_next_cons = 0xffff;
1106}
1107
Michael Chanc0c050c2015-10-22 16:01:17 -04001108static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1109 struct rx_tpa_start_cmp *tpa_start,
1110 struct rx_tpa_start_cmp_ext *tpa_start1)
1111{
1112 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1113 u16 cons, prod;
1114 struct bnxt_tpa_info *tpa_info;
1115 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1116 struct rx_bd *prod_bd;
1117 dma_addr_t mapping;
1118
1119 cons = tpa_start->rx_tpa_start_cmp_opaque;
1120 prod = rxr->rx_prod;
1121 cons_rx_buf = &rxr->rx_buf_ring[cons];
1122 prod_rx_buf = &rxr->rx_buf_ring[prod];
1123 tpa_info = &rxr->rx_tpa[agg_id];
1124
Michael Chanfa7e2812016-05-10 19:18:00 -04001125 if (unlikely(cons != rxr->rx_next_cons)) {
1126 bnxt_sched_reset(bp, rxr);
1127 return;
1128 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001129 /* Store cfa_code in tpa_info to use in tpa_end
1130 * completion processing.
1131 */
1132 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001133 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001134 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001135
1136 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001137 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001138
1139 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1140
1141 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1142
1143 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001144 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001145 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001146 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001147
1148 tpa_info->len =
1149 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1150 RX_TPA_START_CMP_LEN_SHIFT;
1151 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1152 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1153
1154 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1155 tpa_info->gso_type = SKB_GSO_TCPV4;
1156 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
Michael Chan50f011b2018-08-05 16:51:51 -04001157 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
Michael Chanc0c050c2015-10-22 16:01:17 -04001158 tpa_info->gso_type = SKB_GSO_TCPV6;
1159 tpa_info->rss_hash =
1160 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1161 } else {
1162 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1163 tpa_info->gso_type = 0;
1164 if (netif_msg_rx_err(bp))
1165 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1166 }
1167 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1168 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001169 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001170
1171 rxr->rx_prod = NEXT_RX(prod);
1172 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001173 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001174 cons_rx_buf = &rxr->rx_buf_ring[cons];
1175
1176 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1177 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1178 cons_rx_buf->data = NULL;
1179}
1180
Michael Chane44758b2018-10-14 07:02:55 -04001181static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 cp_cons,
1182 u32 agg_bufs)
Michael Chanc0c050c2015-10-22 16:01:17 -04001183{
1184 if (agg_bufs)
Michael Chane44758b2018-10-14 07:02:55 -04001185 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001186}
1187
Michael Chan94758f82016-06-13 02:25:35 -04001188static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1189 int payload_off, int tcp_ts,
1190 struct sk_buff *skb)
1191{
1192#ifdef CONFIG_INET
1193 struct tcphdr *th;
1194 int len, nw_off;
1195 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1196 u32 hdr_info = tpa_info->hdr_info;
1197 bool loopback = false;
1198
1199 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1200 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1201 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1202
1203 /* If the packet is an internal loopback packet, the offsets will
1204 * have an extra 4 bytes.
1205 */
1206 if (inner_mac_off == 4) {
1207 loopback = true;
1208 } else if (inner_mac_off > 4) {
1209 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1210 ETH_HLEN - 2));
1211
1212 /* We only support inner iPv4/ipv6. If we don't see the
1213 * correct protocol ID, it must be a loopback packet where
1214 * the offsets are off by 4.
1215 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001216 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001217 loopback = true;
1218 }
1219 if (loopback) {
1220 /* internal loopback packet, subtract all offsets by 4 */
1221 inner_ip_off -= 4;
1222 inner_mac_off -= 4;
1223 outer_ip_off -= 4;
1224 }
1225
1226 nw_off = inner_ip_off - ETH_HLEN;
1227 skb_set_network_header(skb, nw_off);
1228 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1229 struct ipv6hdr *iph = ipv6_hdr(skb);
1230
1231 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1232 len = skb->len - skb_transport_offset(skb);
1233 th = tcp_hdr(skb);
1234 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1235 } else {
1236 struct iphdr *iph = ip_hdr(skb);
1237
1238 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1239 len = skb->len - skb_transport_offset(skb);
1240 th = tcp_hdr(skb);
1241 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1242 }
1243
1244 if (inner_mac_off) { /* tunnel */
1245 struct udphdr *uh = NULL;
1246 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1247 ETH_HLEN - 2));
1248
1249 if (proto == htons(ETH_P_IP)) {
1250 struct iphdr *iph = (struct iphdr *)skb->data;
1251
1252 if (iph->protocol == IPPROTO_UDP)
1253 uh = (struct udphdr *)(iph + 1);
1254 } else {
1255 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1256
1257 if (iph->nexthdr == IPPROTO_UDP)
1258 uh = (struct udphdr *)(iph + 1);
1259 }
1260 if (uh) {
1261 if (uh->check)
1262 skb_shinfo(skb)->gso_type |=
1263 SKB_GSO_UDP_TUNNEL_CSUM;
1264 else
1265 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1266 }
1267 }
1268#endif
1269 return skb;
1270}
1271
Michael Chanc0c050c2015-10-22 16:01:17 -04001272#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1273#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1274
Michael Chan309369c2016-06-13 02:25:34 -04001275static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1276 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001277 struct sk_buff *skb)
1278{
Michael Chand1611c32015-10-25 22:27:57 -04001279#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001280 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001281 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001282
Michael Chan309369c2016-06-13 02:25:34 -04001283 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001284 tcp_opt_len = 12;
1285
Michael Chanc0c050c2015-10-22 16:01:17 -04001286 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1287 struct iphdr *iph;
1288
1289 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1290 ETH_HLEN;
1291 skb_set_network_header(skb, nw_off);
1292 iph = ip_hdr(skb);
1293 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1294 len = skb->len - skb_transport_offset(skb);
1295 th = tcp_hdr(skb);
1296 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1297 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1298 struct ipv6hdr *iph;
1299
1300 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1301 ETH_HLEN;
1302 skb_set_network_header(skb, nw_off);
1303 iph = ipv6_hdr(skb);
1304 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1305 len = skb->len - skb_transport_offset(skb);
1306 th = tcp_hdr(skb);
1307 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1308 } else {
1309 dev_kfree_skb_any(skb);
1310 return NULL;
1311 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001312
1313 if (nw_off) { /* tunnel */
1314 struct udphdr *uh = NULL;
1315
1316 if (skb->protocol == htons(ETH_P_IP)) {
1317 struct iphdr *iph = (struct iphdr *)skb->data;
1318
1319 if (iph->protocol == IPPROTO_UDP)
1320 uh = (struct udphdr *)(iph + 1);
1321 } else {
1322 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1323
1324 if (iph->nexthdr == IPPROTO_UDP)
1325 uh = (struct udphdr *)(iph + 1);
1326 }
1327 if (uh) {
1328 if (uh->check)
1329 skb_shinfo(skb)->gso_type |=
1330 SKB_GSO_UDP_TUNNEL_CSUM;
1331 else
1332 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1333 }
1334 }
1335#endif
1336 return skb;
1337}
1338
Michael Chan309369c2016-06-13 02:25:34 -04001339static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1340 struct bnxt_tpa_info *tpa_info,
1341 struct rx_tpa_end_cmp *tpa_end,
1342 struct rx_tpa_end_cmp_ext *tpa_end1,
1343 struct sk_buff *skb)
1344{
1345#ifdef CONFIG_INET
1346 int payload_off;
1347 u16 segs;
1348
1349 segs = TPA_END_TPA_SEGS(tpa_end);
1350 if (segs == 1)
1351 return skb;
1352
1353 NAPI_GRO_CB(skb)->count = segs;
1354 skb_shinfo(skb)->gso_size =
1355 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1356 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1357 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1358 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1359 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1360 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001361 if (likely(skb))
1362 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001363#endif
1364 return skb;
1365}
1366
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001367/* Given the cfa_code of a received packet determine which
1368 * netdev (vf-rep or PF) the packet is destined to.
1369 */
1370static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1371{
1372 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1373
1374 /* if vf-rep dev is NULL, the must belongs to the PF */
1375 return dev ? dev : bp->dev;
1376}
1377
Michael Chanc0c050c2015-10-22 16:01:17 -04001378static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
Michael Chane44758b2018-10-14 07:02:55 -04001379 struct bnxt_cp_ring_info *cpr,
Michael Chanc0c050c2015-10-22 16:01:17 -04001380 u32 *raw_cons,
1381 struct rx_tpa_end_cmp *tpa_end,
1382 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001383 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001384{
Michael Chane44758b2018-10-14 07:02:55 -04001385 struct bnxt_napi *bnapi = cpr->bnapi;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001386 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001387 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001388 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001389 u16 cp_cons = RING_CMP(*raw_cons);
1390 unsigned int len;
1391 struct bnxt_tpa_info *tpa_info;
1392 dma_addr_t mapping;
1393 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001394 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001395
Michael Chanfa7e2812016-05-10 19:18:00 -04001396 if (unlikely(bnapi->in_reset)) {
Michael Chane44758b2018-10-14 07:02:55 -04001397 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
Michael Chanfa7e2812016-05-10 19:18:00 -04001398
1399 if (rc < 0)
1400 return ERR_PTR(-EBUSY);
1401 return NULL;
1402 }
1403
Michael Chanc0c050c2015-10-22 16:01:17 -04001404 tpa_info = &rxr->rx_tpa[agg_id];
1405 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001406 data_ptr = tpa_info->data_ptr;
1407 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001408 len = tpa_info->len;
1409 mapping = tpa_info->mapping;
1410
1411 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1412 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1413
1414 if (agg_bufs) {
1415 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1416 return ERR_PTR(-EBUSY);
1417
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001418 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001419 cp_cons = NEXT_CMP(cp_cons);
1420 }
1421
Michael Chan69c149e2017-06-23 14:01:00 -04001422 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chane44758b2018-10-14 07:02:55 -04001423 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001424 if (agg_bufs > MAX_SKB_FRAGS)
1425 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1426 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001427 return NULL;
1428 }
1429
1430 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001431 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001432 if (!skb) {
Michael Chane44758b2018-10-14 07:02:55 -04001433 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001434 return NULL;
1435 }
1436 } else {
1437 u8 *new_data;
1438 dma_addr_t new_mapping;
1439
1440 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1441 if (!new_data) {
Michael Chane44758b2018-10-14 07:02:55 -04001442 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001443 return NULL;
1444 }
1445
1446 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001447 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001448 tpa_info->mapping = new_mapping;
1449
1450 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001451 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1452 bp->rx_buf_use_size, bp->rx_dir,
1453 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001454
1455 if (!skb) {
1456 kfree(data);
Michael Chane44758b2018-10-14 07:02:55 -04001457 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001458 return NULL;
1459 }
Michael Chanb3dba772017-02-06 16:55:35 -05001460 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001461 skb_put(skb, len);
1462 }
1463
1464 if (agg_bufs) {
Michael Chane44758b2018-10-14 07:02:55 -04001465 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001466 if (!skb) {
1467 /* Page reuse already handled by bnxt_rx_pages(). */
1468 return NULL;
1469 }
1470 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001471
1472 skb->protocol =
1473 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001474
1475 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1476 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1477
Michael Chan8852ddb2016-06-06 02:37:16 -04001478 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1479 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001480 u16 vlan_proto = tpa_info->metadata >>
1481 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chaned7bc6022018-03-09 23:46:06 -05001482 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001483
Michael Chan8852ddb2016-06-06 02:37:16 -04001484 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001485 }
1486
1487 skb_checksum_none_assert(skb);
1488 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1489 skb->ip_summed = CHECKSUM_UNNECESSARY;
1490 skb->csum_level =
1491 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1492 }
1493
1494 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001495 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001496
1497 return skb;
1498}
1499
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001500static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1501 struct sk_buff *skb)
1502{
1503 if (skb->dev != bp->dev) {
1504 /* this packet belongs to a vf-rep */
1505 bnxt_vf_rep_rx(bp, skb);
1506 return;
1507 }
1508 skb_record_rx_queue(skb, bnapi->index);
1509 napi_gro_receive(&bnapi->napi, skb);
1510}
1511
Michael Chanc0c050c2015-10-22 16:01:17 -04001512/* returns the following:
1513 * 1 - 1 packet successfully received
1514 * 0 - successful TPA_START, packet not completed yet
1515 * -EBUSY - completion ring does not have all the agg buffers yet
1516 * -ENOMEM - packet aborted due to out of memory
1517 * -EIO - packet aborted due to hw error indicated in BD
1518 */
Michael Chane44758b2018-10-14 07:02:55 -04001519static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1520 u32 *raw_cons, u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001521{
Michael Chane44758b2018-10-14 07:02:55 -04001522 struct bnxt_napi *bnapi = cpr->bnapi;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001523 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001524 struct net_device *dev = bp->dev;
1525 struct rx_cmp *rxcmp;
1526 struct rx_cmp_ext *rxcmp1;
1527 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001528 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001529 struct bnxt_sw_rx_bd *rx_buf;
1530 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001531 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001532 dma_addr_t dma_addr;
1533 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001534 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001535 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001536 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001537
1538 rxcmp = (struct rx_cmp *)
1539 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1540
1541 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1542 cp_cons = RING_CMP(tmp_raw_cons);
1543 rxcmp1 = (struct rx_cmp_ext *)
1544 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1545
1546 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1547 return -EBUSY;
1548
1549 cmp_type = RX_CMP_TYPE(rxcmp);
1550
1551 prod = rxr->rx_prod;
1552
1553 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1554 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1555 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1556
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001557 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001558 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001559
1560 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
Michael Chane44758b2018-10-14 07:02:55 -04001561 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
Michael Chanc0c050c2015-10-22 16:01:17 -04001562 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001563 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001564
Tobias Klauser1fac4b22017-09-26 15:12:26 +02001565 if (IS_ERR(skb))
Michael Chanc0c050c2015-10-22 16:01:17 -04001566 return -EBUSY;
1567
1568 rc = -ENOMEM;
1569 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001570 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001571 rc = 1;
1572 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001573 *event |= BNXT_RX_EVENT;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001574 goto next_rx_no_prod_no_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001575 }
1576
1577 cons = rxcmp->rx_cmp_opaque;
1578 rx_buf = &rxr->rx_buf_ring[cons];
1579 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001580 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001581 if (unlikely(cons != rxr->rx_next_cons)) {
Michael Chane44758b2018-10-14 07:02:55 -04001582 int rc1 = bnxt_discard_rx(bp, cpr, raw_cons, rxcmp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001583
1584 bnxt_sched_reset(bp, rxr);
1585 return rc1;
1586 }
Michael Chan6bb19472017-02-06 16:55:32 -05001587 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001588
Michael Chanc61fb992017-02-06 16:55:36 -05001589 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1590 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001591
1592 if (agg_bufs) {
1593 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1594 return -EBUSY;
1595
1596 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001597 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001598 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001599 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001600
1601 rx_buf->data = NULL;
1602 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1603 bnxt_reuse_rx_data(rxr, cons, data);
1604 if (agg_bufs)
Michael Chane44758b2018-10-14 07:02:55 -04001605 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001606
1607 rc = -EIO;
1608 goto next_rx;
1609 }
1610
1611 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001612 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001613
Michael Chanc6d30e82017-02-06 16:55:42 -05001614 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1615 rc = 1;
1616 goto next_rx;
1617 }
1618
Michael Chanc0c050c2015-10-22 16:01:17 -04001619 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001620 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001621 bnxt_reuse_rx_data(rxr, cons, data);
1622 if (!skb) {
1623 rc = -ENOMEM;
1624 goto next_rx;
1625 }
1626 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001627 u32 payload;
1628
Michael Chanc6d30e82017-02-06 16:55:42 -05001629 if (rx_buf->data_ptr == data_ptr)
1630 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1631 else
1632 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001633 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001634 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001635 if (!skb) {
1636 rc = -ENOMEM;
1637 goto next_rx;
1638 }
1639 }
1640
1641 if (agg_bufs) {
Michael Chane44758b2018-10-14 07:02:55 -04001642 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs);
Michael Chanc0c050c2015-10-22 16:01:17 -04001643 if (!skb) {
1644 rc = -ENOMEM;
1645 goto next_rx;
1646 }
1647 }
1648
1649 if (RX_CMP_HASH_VALID(rxcmp)) {
1650 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1651 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1652
1653 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1654 if (hash_type != 1 && hash_type != 3)
1655 type = PKT_HASH_TYPE_L3;
1656 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1657 }
1658
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001659 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1660 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001661
Michael Chan8852ddb2016-06-06 02:37:16 -04001662 if ((rxcmp1->rx_cmp_flags2 &
1663 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1664 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001665 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chaned7bc6022018-03-09 23:46:06 -05001666 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001667 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1668
Michael Chan8852ddb2016-06-06 02:37:16 -04001669 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001670 }
1671
1672 skb_checksum_none_assert(skb);
1673 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1674 if (dev->features & NETIF_F_RXCSUM) {
1675 skb->ip_summed = CHECKSUM_UNNECESSARY;
1676 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1677 }
1678 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001679 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1680 if (dev->features & NETIF_F_RXCSUM)
Michael Chand1981922018-11-15 03:25:38 -05001681 bnapi->cp_ring.rx_l4_csum_errors++;
Satish Baddipadige665e3502015-12-27 18:19:21 -05001682 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001683 }
1684
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001685 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001686 rc = 1;
1687
1688next_rx:
1689 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001690 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001691
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001692 cpr->rx_packets += 1;
1693 cpr->rx_bytes += len;
Colin Ian Kinge7e70fa2018-01-16 10:22:50 +00001694
1695next_rx_no_prod_no_len:
Michael Chanc0c050c2015-10-22 16:01:17 -04001696 *raw_cons = tmp_raw_cons;
1697
1698 return rc;
1699}
1700
Michael Chan2270bc52017-06-23 14:01:01 -04001701/* In netpoll mode, if we are using a combined completion ring, we need to
1702 * discard the rx packets and recycle the buffers.
1703 */
Michael Chane44758b2018-10-14 07:02:55 -04001704static int bnxt_force_rx_discard(struct bnxt *bp,
1705 struct bnxt_cp_ring_info *cpr,
Michael Chan2270bc52017-06-23 14:01:01 -04001706 u32 *raw_cons, u8 *event)
1707{
Michael Chan2270bc52017-06-23 14:01:01 -04001708 u32 tmp_raw_cons = *raw_cons;
1709 struct rx_cmp_ext *rxcmp1;
1710 struct rx_cmp *rxcmp;
1711 u16 cp_cons;
1712 u8 cmp_type;
1713
1714 cp_cons = RING_CMP(tmp_raw_cons);
1715 rxcmp = (struct rx_cmp *)
1716 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1717
1718 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1719 cp_cons = RING_CMP(tmp_raw_cons);
1720 rxcmp1 = (struct rx_cmp_ext *)
1721 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1722
1723 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1724 return -EBUSY;
1725
1726 cmp_type = RX_CMP_TYPE(rxcmp);
1727 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1728 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1729 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1730 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1731 struct rx_tpa_end_cmp_ext *tpa_end1;
1732
1733 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1734 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1735 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1736 }
Michael Chane44758b2018-10-14 07:02:55 -04001737 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
Michael Chan2270bc52017-06-23 14:01:01 -04001738}
1739
Michael Chan4bb13ab2016-04-05 14:09:01 -04001740#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001741 ((data) & \
1742 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001743
Michael Chanc0c050c2015-10-22 16:01:17 -04001744static int bnxt_async_event_process(struct bnxt *bp,
1745 struct hwrm_async_event_cmpl *cmpl)
1746{
1747 u16 event_id = le16_to_cpu(cmpl->event_id);
1748
1749 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1750 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001751 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001752 u32 data1 = le32_to_cpu(cmpl->event_data1);
1753 struct bnxt_link_info *link_info = &bp->link_info;
1754
1755 if (BNXT_VF(bp))
1756 goto async_event_process_exit;
Michael Chana8168b62017-12-06 17:31:22 -05001757
1758 /* print unsupported speed warning in forced speed mode only */
1759 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1760 (data1 & 0x20000)) {
Michael Chan8cbde112016-04-11 04:11:14 -04001761 u16 fw_speed = link_info->force_link_speed;
1762 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1763
Michael Chana8168b62017-12-06 17:31:22 -05001764 if (speed != SPEED_UNKNOWN)
1765 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1766 speed);
Michael Chan8cbde112016-04-11 04:11:14 -04001767 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001768 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001769 }
Gustavo A. R. Silvabc171e82018-08-07 18:11:14 -05001770 /* fall through */
Michael Chan87c374d2016-12-02 21:17:16 -05001771 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001772 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001773 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001774 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001775 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001776 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001777 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001778 u32 data1 = le32_to_cpu(cmpl->event_data1);
1779 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1780
1781 if (BNXT_VF(bp))
1782 break;
1783
1784 if (bp->pf.port_id != port_id)
1785 break;
1786
Michael Chan4bb13ab2016-04-05 14:09:01 -04001787 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1788 break;
1789 }
Michael Chan87c374d2016-12-02 21:17:16 -05001790 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001791 if (BNXT_PF(bp))
1792 goto async_event_process_exit;
1793 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1794 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001795 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001796 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001797 }
Michael Chanc213eae2017-10-13 21:09:29 -04001798 bnxt_queue_sp_work(bp);
Jeffrey Huang19241362016-02-26 04:00:00 -05001799async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001800 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001801 return 0;
1802}
1803
1804static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1805{
1806 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1807 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1808 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1809 (struct hwrm_fwd_req_cmpl *)txcmp;
1810
1811 switch (cmpl_type) {
1812 case CMPL_BASE_TYPE_HWRM_DONE:
1813 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1814 if (seq_id == bp->hwrm_intr_seq_id)
Venkat Duvvurufc718bb2018-12-20 03:38:44 -05001815 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04001816 else
1817 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1818 break;
1819
1820 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1821 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1822
1823 if ((vf_id < bp->pf.first_vf_id) ||
1824 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1825 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1826 vf_id);
1827 return -EINVAL;
1828 }
1829
1830 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1831 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001832 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04001833 break;
1834
1835 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1836 bnxt_async_event_process(bp,
1837 (struct hwrm_async_event_cmpl *)txcmp);
1838
1839 default:
1840 break;
1841 }
1842
1843 return 0;
1844}
1845
1846static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1847{
1848 struct bnxt_napi *bnapi = dev_instance;
1849 struct bnxt *bp = bnapi->bp;
1850 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1851 u32 cons = RING_CMP(cpr->cp_raw_cons);
1852
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05001853 cpr->event_ctr++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001854 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1855 napi_schedule(&bnapi->napi);
1856 return IRQ_HANDLED;
1857}
1858
1859static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1860{
1861 u32 raw_cons = cpr->cp_raw_cons;
1862 u16 cons = RING_CMP(raw_cons);
1863 struct tx_cmp *txcmp;
1864
1865 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1866
1867 return TX_CMP_VALID(txcmp, raw_cons);
1868}
1869
Michael Chanc0c050c2015-10-22 16:01:17 -04001870static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1871{
1872 struct bnxt_napi *bnapi = dev_instance;
1873 struct bnxt *bp = bnapi->bp;
1874 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1875 u32 cons = RING_CMP(cpr->cp_raw_cons);
1876 u32 int_status;
1877
1878 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1879
1880 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001881 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001882 /* return if erroneous interrupt */
1883 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1884 return IRQ_NONE;
1885 }
1886
1887 /* disable ring IRQ */
Michael Chan697197e2018-10-14 07:02:46 -04001888 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
Michael Chanc0c050c2015-10-22 16:01:17 -04001889
1890 /* Return here if interrupt is shared and is disabled. */
1891 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1892 return IRQ_HANDLED;
1893
1894 napi_schedule(&bnapi->napi);
1895 return IRQ_HANDLED;
1896}
1897
Michael Chan3675b922018-10-14 07:02:57 -04001898static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1899 int budget)
Michael Chanc0c050c2015-10-22 16:01:17 -04001900{
Michael Chane44758b2018-10-14 07:02:55 -04001901 struct bnxt_napi *bnapi = cpr->bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04001902 u32 raw_cons = cpr->cp_raw_cons;
1903 u32 cons;
1904 int tx_pkts = 0;
1905 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001906 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001907 struct tx_cmp *txcmp;
1908
Michael Chan0fcec982018-10-14 07:02:58 -04001909 cpr->has_more_work = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001910 while (1) {
1911 int rc;
1912
1913 cons = RING_CMP(raw_cons);
1914 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1915
1916 if (!TX_CMP_VALID(txcmp, raw_cons))
1917 break;
1918
Michael Chan67a95e22016-05-04 16:56:43 -04001919 /* The valid test of the entry must be done first before
1920 * reading any further.
1921 */
Michael Chanb67daab2016-05-15 03:04:51 -04001922 dma_rmb();
Michael Chan3675b922018-10-14 07:02:57 -04001923 cpr->had_work_done = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04001924 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1925 tx_pkts++;
1926 /* return full budget so NAPI will complete. */
Michael Chan73f21c62018-09-26 00:41:04 -04001927 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001928 rx_pkts = budget;
Michael Chan73f21c62018-09-26 00:41:04 -04001929 raw_cons = NEXT_RAW_CMP(raw_cons);
Michael Chan0fcec982018-10-14 07:02:58 -04001930 if (budget)
1931 cpr->has_more_work = 1;
Michael Chan73f21c62018-09-26 00:41:04 -04001932 break;
1933 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001934 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001935 if (likely(budget))
Michael Chane44758b2018-10-14 07:02:55 -04001936 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
Michael Chan2270bc52017-06-23 14:01:01 -04001937 else
Michael Chane44758b2018-10-14 07:02:55 -04001938 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
Michael Chan2270bc52017-06-23 14:01:01 -04001939 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001940 if (likely(rc >= 0))
1941 rx_pkts += rc;
Michael Chan903649e2017-08-28 13:40:30 -04001942 /* Increment rx_pkts when rc is -ENOMEM to count towards
1943 * the NAPI budget. Otherwise, we may potentially loop
1944 * here forever if we consistently cannot allocate
1945 * buffers.
1946 */
Calvin Owens2edbdb32017-12-08 09:05:26 -08001947 else if (rc == -ENOMEM && budget)
Michael Chan903649e2017-08-28 13:40:30 -04001948 rx_pkts++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001949 else if (rc == -EBUSY) /* partial completion */
1950 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001951 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1952 CMPL_BASE_TYPE_HWRM_DONE) ||
1953 (TX_CMP_TYPE(txcmp) ==
1954 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1955 (TX_CMP_TYPE(txcmp) ==
1956 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1957 bnxt_hwrm_handler(bp, txcmp);
1958 }
1959 raw_cons = NEXT_RAW_CMP(raw_cons);
1960
Michael Chan0fcec982018-10-14 07:02:58 -04001961 if (rx_pkts && rx_pkts == budget) {
1962 cpr->has_more_work = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04001963 break;
Michael Chan0fcec982018-10-14 07:02:58 -04001964 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001965 }
1966
Michael Chan38413402017-02-06 16:55:43 -05001967 if (event & BNXT_TX_EVENT) {
1968 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan38413402017-02-06 16:55:43 -05001969 u16 prod = txr->tx_prod;
1970
1971 /* Sync BD data before updating doorbell */
1972 wmb();
1973
Michael Chan697197e2018-10-14 07:02:46 -04001974 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
Michael Chan38413402017-02-06 16:55:43 -05001975 }
1976
Michael Chanc0c050c2015-10-22 16:01:17 -04001977 cpr->cp_raw_cons = raw_cons;
Michael Chan3675b922018-10-14 07:02:57 -04001978 bnapi->tx_pkts += tx_pkts;
1979 bnapi->events |= event;
1980 return rx_pkts;
1981}
1982
1983static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
1984{
1985 if (bnapi->tx_pkts) {
1986 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
1987 bnapi->tx_pkts = 0;
1988 }
1989
1990 if (bnapi->events & BNXT_RX_EVENT) {
1991 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1992
1993 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
1994 if (bnapi->events & BNXT_AGG_EVENT)
1995 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
1996 }
1997 bnapi->events = 0;
1998}
1999
2000static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2001 int budget)
2002{
2003 struct bnxt_napi *bnapi = cpr->bnapi;
2004 int rx_pkts;
2005
2006 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2007
Michael Chanc0c050c2015-10-22 16:01:17 -04002008 /* ACK completion ring before freeing tx ring and producing new
2009 * buffers in rx/agg rings to prevent overflowing the completion
2010 * ring.
2011 */
Michael Chan697197e2018-10-14 07:02:46 -04002012 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002013
Michael Chan3675b922018-10-14 07:02:57 -04002014 __bnxt_poll_work_done(bp, bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04002015 return rx_pkts;
2016}
2017
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002018static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2019{
2020 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2021 struct bnxt *bp = bnapi->bp;
2022 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2023 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2024 struct tx_cmp *txcmp;
2025 struct rx_cmp_ext *rxcmp1;
2026 u32 cp_cons, tmp_raw_cons;
2027 u32 raw_cons = cpr->cp_raw_cons;
2028 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05002029 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002030
2031 while (1) {
2032 int rc;
2033
2034 cp_cons = RING_CMP(raw_cons);
2035 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2036
2037 if (!TX_CMP_VALID(txcmp, raw_cons))
2038 break;
2039
2040 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2041 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2042 cp_cons = RING_CMP(tmp_raw_cons);
2043 rxcmp1 = (struct rx_cmp_ext *)
2044 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2045
2046 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2047 break;
2048
2049 /* force an error to recycle the buffer */
2050 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2051 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2052
Michael Chane44758b2018-10-14 07:02:55 -04002053 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
Calvin Owens2edbdb32017-12-08 09:05:26 -08002054 if (likely(rc == -EIO) && budget)
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002055 rx_pkts++;
2056 else if (rc == -EBUSY) /* partial completion */
2057 break;
2058 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2059 CMPL_BASE_TYPE_HWRM_DONE)) {
2060 bnxt_hwrm_handler(bp, txcmp);
2061 } else {
2062 netdev_err(bp->dev,
2063 "Invalid completion received on special ring\n");
2064 }
2065 raw_cons = NEXT_RAW_CMP(raw_cons);
2066
2067 if (rx_pkts == budget)
2068 break;
2069 }
2070
2071 cpr->cp_raw_cons = raw_cons;
Michael Chan697197e2018-10-14 07:02:46 -04002072 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2073 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002074
Michael Chan434c9752017-05-29 19:06:08 -04002075 if (event & BNXT_AGG_EVENT)
Michael Chan697197e2018-10-14 07:02:46 -04002076 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002077
2078 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08002079 napi_complete_done(napi, rx_pkts);
Michael Chan697197e2018-10-14 07:02:46 -04002080 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04002081 }
2082 return rx_pkts;
2083}
2084
Michael Chanc0c050c2015-10-22 16:01:17 -04002085static int bnxt_poll(struct napi_struct *napi, int budget)
2086{
2087 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2088 struct bnxt *bp = bnapi->bp;
2089 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2090 int work_done = 0;
2091
Michael Chanc0c050c2015-10-22 16:01:17 -04002092 while (1) {
Michael Chane44758b2018-10-14 07:02:55 -04002093 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
Michael Chanc0c050c2015-10-22 16:01:17 -04002094
Michael Chan73f21c62018-09-26 00:41:04 -04002095 if (work_done >= budget) {
2096 if (!budget)
Michael Chan697197e2018-10-14 07:02:46 -04002097 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002098 break;
Michael Chan73f21c62018-09-26 00:41:04 -04002099 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002100
2101 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05002102 if (napi_complete_done(napi, work_done))
Michael Chan697197e2018-10-14 07:02:46 -04002103 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002104 break;
2105 }
2106 }
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002107 if (bp->flags & BNXT_FLAG_DIM) {
2108 struct net_dim_sample dim_sample;
2109
2110 net_dim_sample(cpr->event_ctr,
2111 cpr->rx_packets,
2112 cpr->rx_bytes,
2113 &dim_sample);
2114 net_dim(&cpr->dim, dim_sample);
2115 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002116 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04002117 return work_done;
2118}
2119
Michael Chan0fcec982018-10-14 07:02:58 -04002120static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2121{
2122 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2123 int i, work_done = 0;
2124
2125 for (i = 0; i < 2; i++) {
2126 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2127
2128 if (cpr2) {
2129 work_done += __bnxt_poll_work(bp, cpr2,
2130 budget - work_done);
2131 cpr->has_more_work |= cpr2->has_more_work;
2132 }
2133 }
2134 return work_done;
2135}
2136
2137static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2138 u64 dbr_type, bool all)
2139{
2140 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2141 int i;
2142
2143 for (i = 0; i < 2; i++) {
2144 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2145 struct bnxt_db_info *db;
2146
2147 if (cpr2 && (all || cpr2->had_work_done)) {
2148 db = &cpr2->cp_db;
2149 writeq(db->db_key64 | dbr_type |
2150 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2151 cpr2->had_work_done = 0;
2152 }
2153 }
2154 __bnxt_poll_work_done(bp, bnapi);
2155}
2156
2157static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2158{
2159 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2160 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2161 u32 raw_cons = cpr->cp_raw_cons;
2162 struct bnxt *bp = bnapi->bp;
2163 struct nqe_cn *nqcmp;
2164 int work_done = 0;
2165 u32 cons;
2166
2167 if (cpr->has_more_work) {
2168 cpr->has_more_work = 0;
2169 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2170 if (cpr->has_more_work) {
2171 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, false);
2172 return work_done;
2173 }
2174 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, true);
2175 if (napi_complete_done(napi, work_done))
2176 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, cpr->cp_raw_cons);
2177 return work_done;
2178 }
2179 while (1) {
2180 cons = RING_CMP(raw_cons);
2181 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2182
2183 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2184 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
2185 false);
2186 cpr->cp_raw_cons = raw_cons;
2187 if (napi_complete_done(napi, work_done))
2188 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2189 cpr->cp_raw_cons);
2190 return work_done;
2191 }
2192
2193 /* The valid test of the entry must be done first before
2194 * reading any further.
2195 */
2196 dma_rmb();
2197
2198 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2199 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2200 struct bnxt_cp_ring_info *cpr2;
2201
2202 cpr2 = cpr->cp_ring_arr[idx];
2203 work_done += __bnxt_poll_work(bp, cpr2,
2204 budget - work_done);
2205 cpr->has_more_work = cpr2->has_more_work;
2206 } else {
2207 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2208 }
2209 raw_cons = NEXT_RAW_CMP(raw_cons);
2210 if (cpr->has_more_work)
2211 break;
2212 }
2213 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, true);
2214 cpr->cp_raw_cons = raw_cons;
2215 return work_done;
2216}
2217
Michael Chanc0c050c2015-10-22 16:01:17 -04002218static void bnxt_free_tx_skbs(struct bnxt *bp)
2219{
2220 int i, max_idx;
2221 struct pci_dev *pdev = bp->pdev;
2222
Michael Chanb6ab4b02016-01-02 23:44:59 -05002223 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002224 return;
2225
2226 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2227 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002228 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002229 int j;
2230
Michael Chanc0c050c2015-10-22 16:01:17 -04002231 for (j = 0; j < max_idx;) {
2232 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2233 struct sk_buff *skb = tx_buf->skb;
2234 int k, last;
2235
2236 if (!skb) {
2237 j++;
2238 continue;
2239 }
2240
2241 tx_buf->skb = NULL;
2242
2243 if (tx_buf->is_push) {
2244 dev_kfree_skb(skb);
2245 j += 2;
2246 continue;
2247 }
2248
2249 dma_unmap_single(&pdev->dev,
2250 dma_unmap_addr(tx_buf, mapping),
2251 skb_headlen(skb),
2252 PCI_DMA_TODEVICE);
2253
2254 last = tx_buf->nr_frags;
2255 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002256 for (k = 0; k < last; k++, j++) {
2257 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002258 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2259
Michael Chand612a572016-01-28 03:11:22 -05002260 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002261 dma_unmap_page(
2262 &pdev->dev,
2263 dma_unmap_addr(tx_buf, mapping),
2264 skb_frag_size(frag), PCI_DMA_TODEVICE);
2265 }
2266 dev_kfree_skb(skb);
2267 }
2268 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2269 }
2270}
2271
2272static void bnxt_free_rx_skbs(struct bnxt *bp)
2273{
2274 int i, max_idx, max_agg_idx;
2275 struct pci_dev *pdev = bp->pdev;
2276
Michael Chanb6ab4b02016-01-02 23:44:59 -05002277 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002278 return;
2279
2280 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2281 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2282 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002283 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002284 int j;
2285
Michael Chanc0c050c2015-10-22 16:01:17 -04002286 if (rxr->rx_tpa) {
2287 for (j = 0; j < MAX_TPA; j++) {
2288 struct bnxt_tpa_info *tpa_info =
2289 &rxr->rx_tpa[j];
2290 u8 *data = tpa_info->data;
2291
2292 if (!data)
2293 continue;
2294
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002295 dma_unmap_single_attrs(&pdev->dev,
2296 tpa_info->mapping,
2297 bp->rx_buf_use_size,
2298 bp->rx_dir,
2299 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002300
2301 tpa_info->data = NULL;
2302
2303 kfree(data);
2304 }
2305 }
2306
2307 for (j = 0; j < max_idx; j++) {
2308 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002309 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002310 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002311
2312 if (!data)
2313 continue;
2314
Michael Chanc0c050c2015-10-22 16:01:17 -04002315 rx_buf->data = NULL;
2316
Michael Chan3ed3a832017-03-28 19:47:31 -04002317 if (BNXT_RX_PAGE_MODE(bp)) {
2318 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002319 dma_unmap_page_attrs(&pdev->dev, mapping,
2320 PAGE_SIZE, bp->rx_dir,
2321 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002322 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002323 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002324 dma_unmap_single_attrs(&pdev->dev, mapping,
2325 bp->rx_buf_use_size,
2326 bp->rx_dir,
2327 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002328 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002329 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002330 }
2331
2332 for (j = 0; j < max_agg_idx; j++) {
2333 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2334 &rxr->rx_agg_ring[j];
2335 struct page *page = rx_agg_buf->page;
2336
2337 if (!page)
2338 continue;
2339
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002340 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2341 BNXT_RX_PAGE_SIZE,
2342 PCI_DMA_FROMDEVICE,
2343 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002344
2345 rx_agg_buf->page = NULL;
2346 __clear_bit(j, rxr->rx_agg_bmap);
2347
2348 __free_page(page);
2349 }
Michael Chan89d0a062016-04-25 02:30:51 -04002350 if (rxr->rx_page) {
2351 __free_page(rxr->rx_page);
2352 rxr->rx_page = NULL;
2353 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002354 }
2355}
2356
2357static void bnxt_free_skbs(struct bnxt *bp)
2358{
2359 bnxt_free_tx_skbs(bp);
2360 bnxt_free_rx_skbs(bp);
2361}
2362
Michael Chan6fe19882018-10-14 07:02:41 -04002363static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
Michael Chanc0c050c2015-10-22 16:01:17 -04002364{
2365 struct pci_dev *pdev = bp->pdev;
2366 int i;
2367
Michael Chan6fe19882018-10-14 07:02:41 -04002368 for (i = 0; i < rmem->nr_pages; i++) {
2369 if (!rmem->pg_arr[i])
Michael Chanc0c050c2015-10-22 16:01:17 -04002370 continue;
2371
Michael Chan6fe19882018-10-14 07:02:41 -04002372 dma_free_coherent(&pdev->dev, rmem->page_size,
2373 rmem->pg_arr[i], rmem->dma_arr[i]);
Michael Chanc0c050c2015-10-22 16:01:17 -04002374
Michael Chan6fe19882018-10-14 07:02:41 -04002375 rmem->pg_arr[i] = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002376 }
Michael Chan6fe19882018-10-14 07:02:41 -04002377 if (rmem->pg_tbl) {
2378 dma_free_coherent(&pdev->dev, rmem->nr_pages * 8,
2379 rmem->pg_tbl, rmem->pg_tbl_map);
2380 rmem->pg_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002381 }
Michael Chan6fe19882018-10-14 07:02:41 -04002382 if (rmem->vmem_size && *rmem->vmem) {
2383 vfree(*rmem->vmem);
2384 *rmem->vmem = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002385 }
2386}
2387
Michael Chan6fe19882018-10-14 07:02:41 -04002388static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
Michael Chanc0c050c2015-10-22 16:01:17 -04002389{
Michael Chanc0c050c2015-10-22 16:01:17 -04002390 struct pci_dev *pdev = bp->pdev;
Michael Chan66cca202018-10-14 07:02:42 -04002391 u64 valid_bit = 0;
Michael Chan6fe19882018-10-14 07:02:41 -04002392 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002393
Michael Chan66cca202018-10-14 07:02:42 -04002394 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2395 valid_bit = PTU_PTE_VALID;
Michael Chan6fe19882018-10-14 07:02:41 -04002396 if (rmem->nr_pages > 1) {
2397 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev,
2398 rmem->nr_pages * 8,
2399 &rmem->pg_tbl_map,
Michael Chanc0c050c2015-10-22 16:01:17 -04002400 GFP_KERNEL);
Michael Chan6fe19882018-10-14 07:02:41 -04002401 if (!rmem->pg_tbl)
Michael Chanc0c050c2015-10-22 16:01:17 -04002402 return -ENOMEM;
2403 }
2404
Michael Chan6fe19882018-10-14 07:02:41 -04002405 for (i = 0; i < rmem->nr_pages; i++) {
Michael Chan66cca202018-10-14 07:02:42 -04002406 u64 extra_bits = valid_bit;
2407
Michael Chan6fe19882018-10-14 07:02:41 -04002408 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2409 rmem->page_size,
2410 &rmem->dma_arr[i],
Michael Chanc0c050c2015-10-22 16:01:17 -04002411 GFP_KERNEL);
Michael Chan6fe19882018-10-14 07:02:41 -04002412 if (!rmem->pg_arr[i])
Michael Chanc0c050c2015-10-22 16:01:17 -04002413 return -ENOMEM;
2414
Michael Chan66cca202018-10-14 07:02:42 -04002415 if (rmem->nr_pages > 1) {
2416 if (i == rmem->nr_pages - 2 &&
2417 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2418 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2419 else if (i == rmem->nr_pages - 1 &&
2420 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2421 extra_bits |= PTU_PTE_LAST;
2422 rmem->pg_tbl[i] =
2423 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2424 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002425 }
2426
Michael Chan6fe19882018-10-14 07:02:41 -04002427 if (rmem->vmem_size) {
2428 *rmem->vmem = vzalloc(rmem->vmem_size);
2429 if (!(*rmem->vmem))
Michael Chanc0c050c2015-10-22 16:01:17 -04002430 return -ENOMEM;
2431 }
2432 return 0;
2433}
2434
2435static void bnxt_free_rx_rings(struct bnxt *bp)
2436{
2437 int i;
2438
Michael Chanb6ab4b02016-01-02 23:44:59 -05002439 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002440 return;
2441
2442 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002443 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002444 struct bnxt_ring_struct *ring;
2445
Michael Chanc6d30e82017-02-06 16:55:42 -05002446 if (rxr->xdp_prog)
2447 bpf_prog_put(rxr->xdp_prog);
2448
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002449 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2450 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2451
Michael Chanc0c050c2015-10-22 16:01:17 -04002452 kfree(rxr->rx_tpa);
2453 rxr->rx_tpa = NULL;
2454
2455 kfree(rxr->rx_agg_bmap);
2456 rxr->rx_agg_bmap = NULL;
2457
2458 ring = &rxr->rx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002459 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002460
2461 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002462 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002463 }
2464}
2465
2466static int bnxt_alloc_rx_rings(struct bnxt *bp)
2467{
2468 int i, rc, agg_rings = 0, tpa_rings = 0;
2469
Michael Chanb6ab4b02016-01-02 23:44:59 -05002470 if (!bp->rx_ring)
2471 return -ENOMEM;
2472
Michael Chanc0c050c2015-10-22 16:01:17 -04002473 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2474 agg_rings = 1;
2475
2476 if (bp->flags & BNXT_FLAG_TPA)
2477 tpa_rings = 1;
2478
2479 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002480 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002481 struct bnxt_ring_struct *ring;
2482
Michael Chanc0c050c2015-10-22 16:01:17 -04002483 ring = &rxr->rx_ring_struct;
2484
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01002485 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2486 if (rc < 0)
2487 return rc;
2488
Michael Chan6fe19882018-10-14 07:02:41 -04002489 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002490 if (rc)
2491 return rc;
2492
Michael Chan2c61d212018-10-14 07:02:50 -04002493 ring->grp_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002494 if (agg_rings) {
2495 u16 mem_size;
2496
2497 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002498 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002499 if (rc)
2500 return rc;
2501
Michael Chan9899bb52018-03-31 13:54:16 -04002502 ring->grp_idx = i;
Michael Chanc0c050c2015-10-22 16:01:17 -04002503 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2504 mem_size = rxr->rx_agg_bmap_size / 8;
2505 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2506 if (!rxr->rx_agg_bmap)
2507 return -ENOMEM;
2508
2509 if (tpa_rings) {
2510 rxr->rx_tpa = kcalloc(MAX_TPA,
2511 sizeof(struct bnxt_tpa_info),
2512 GFP_KERNEL);
2513 if (!rxr->rx_tpa)
2514 return -ENOMEM;
2515 }
2516 }
2517 }
2518 return 0;
2519}
2520
2521static void bnxt_free_tx_rings(struct bnxt *bp)
2522{
2523 int i;
2524 struct pci_dev *pdev = bp->pdev;
2525
Michael Chanb6ab4b02016-01-02 23:44:59 -05002526 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002527 return;
2528
2529 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002530 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002531 struct bnxt_ring_struct *ring;
2532
Michael Chanc0c050c2015-10-22 16:01:17 -04002533 if (txr->tx_push) {
2534 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2535 txr->tx_push, txr->tx_push_mapping);
2536 txr->tx_push = NULL;
2537 }
2538
2539 ring = &txr->tx_ring_struct;
2540
Michael Chan6fe19882018-10-14 07:02:41 -04002541 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002542 }
2543}
2544
2545static int bnxt_alloc_tx_rings(struct bnxt *bp)
2546{
2547 int i, j, rc;
2548 struct pci_dev *pdev = bp->pdev;
2549
2550 bp->tx_push_size = 0;
2551 if (bp->tx_push_thresh) {
2552 int push_size;
2553
2554 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2555 bp->tx_push_thresh);
2556
Michael Chan4419dbe2016-02-10 17:33:49 -05002557 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002558 push_size = 0;
2559 bp->tx_push_thresh = 0;
2560 }
2561
2562 bp->tx_push_size = push_size;
2563 }
2564
2565 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002566 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002567 struct bnxt_ring_struct *ring;
Michael Chan2e8ef772018-04-26 17:44:31 -04002568 u8 qidx;
Michael Chanc0c050c2015-10-22 16:01:17 -04002569
Michael Chanc0c050c2015-10-22 16:01:17 -04002570 ring = &txr->tx_ring_struct;
2571
Michael Chan6fe19882018-10-14 07:02:41 -04002572 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002573 if (rc)
2574 return rc;
2575
Michael Chan9899bb52018-03-31 13:54:16 -04002576 ring->grp_idx = txr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04002577 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002578 dma_addr_t mapping;
2579
2580 /* One pre-allocated DMA buffer to backup
2581 * TX push operation
2582 */
2583 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2584 bp->tx_push_size,
2585 &txr->tx_push_mapping,
2586 GFP_KERNEL);
2587
2588 if (!txr->tx_push)
2589 return -ENOMEM;
2590
Michael Chanc0c050c2015-10-22 16:01:17 -04002591 mapping = txr->tx_push_mapping +
2592 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002593 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002594
Michael Chan4419dbe2016-02-10 17:33:49 -05002595 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002596 }
Michael Chan2e8ef772018-04-26 17:44:31 -04002597 qidx = bp->tc_to_qidx[j];
2598 ring->queue_id = bp->q_info[qidx].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002599 if (i < bp->tx_nr_rings_xdp)
2600 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002601 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2602 j++;
2603 }
2604 return 0;
2605}
2606
2607static void bnxt_free_cp_rings(struct bnxt *bp)
2608{
2609 int i;
2610
2611 if (!bp->bnapi)
2612 return;
2613
2614 for (i = 0; i < bp->cp_nr_rings; i++) {
2615 struct bnxt_napi *bnapi = bp->bnapi[i];
2616 struct bnxt_cp_ring_info *cpr;
2617 struct bnxt_ring_struct *ring;
Michael Chan50e3ab72018-10-14 07:02:49 -04002618 int j;
Michael Chanc0c050c2015-10-22 16:01:17 -04002619
2620 if (!bnapi)
2621 continue;
2622
2623 cpr = &bnapi->cp_ring;
2624 ring = &cpr->cp_ring_struct;
2625
Michael Chan6fe19882018-10-14 07:02:41 -04002626 bnxt_free_ring(bp, &ring->ring_mem);
Michael Chan50e3ab72018-10-14 07:02:49 -04002627
2628 for (j = 0; j < 2; j++) {
2629 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2630
2631 if (cpr2) {
2632 ring = &cpr2->cp_ring_struct;
2633 bnxt_free_ring(bp, &ring->ring_mem);
2634 kfree(cpr2);
2635 cpr->cp_ring_arr[j] = NULL;
2636 }
2637 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002638 }
2639}
2640
Michael Chan50e3ab72018-10-14 07:02:49 -04002641static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
2642{
2643 struct bnxt_ring_mem_info *rmem;
2644 struct bnxt_ring_struct *ring;
2645 struct bnxt_cp_ring_info *cpr;
2646 int rc;
2647
2648 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
2649 if (!cpr)
2650 return NULL;
2651
2652 ring = &cpr->cp_ring_struct;
2653 rmem = &ring->ring_mem;
2654 rmem->nr_pages = bp->cp_nr_pages;
2655 rmem->page_size = HW_CMPD_RING_SIZE;
2656 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2657 rmem->dma_arr = cpr->cp_desc_mapping;
2658 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
2659 rc = bnxt_alloc_ring(bp, rmem);
2660 if (rc) {
2661 bnxt_free_ring(bp, rmem);
2662 kfree(cpr);
2663 cpr = NULL;
2664 }
2665 return cpr;
2666}
2667
Michael Chanc0c050c2015-10-22 16:01:17 -04002668static int bnxt_alloc_cp_rings(struct bnxt *bp)
2669{
Michael Chan50e3ab72018-10-14 07:02:49 -04002670 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
Michael Chane5811b82018-03-31 13:54:18 -04002671 int i, rc, ulp_base_vec, ulp_msix;
Michael Chanc0c050c2015-10-22 16:01:17 -04002672
Michael Chane5811b82018-03-31 13:54:18 -04002673 ulp_msix = bnxt_get_ulp_msix_num(bp);
2674 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04002675 for (i = 0; i < bp->cp_nr_rings; i++) {
2676 struct bnxt_napi *bnapi = bp->bnapi[i];
2677 struct bnxt_cp_ring_info *cpr;
2678 struct bnxt_ring_struct *ring;
2679
2680 if (!bnapi)
2681 continue;
2682
2683 cpr = &bnapi->cp_ring;
Michael Chan50e3ab72018-10-14 07:02:49 -04002684 cpr->bnapi = bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04002685 ring = &cpr->cp_ring_struct;
2686
Michael Chan6fe19882018-10-14 07:02:41 -04002687 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
Michael Chanc0c050c2015-10-22 16:01:17 -04002688 if (rc)
2689 return rc;
Michael Chane5811b82018-03-31 13:54:18 -04002690
2691 if (ulp_msix && i >= ulp_base_vec)
2692 ring->map_idx = i + ulp_msix;
2693 else
2694 ring->map_idx = i;
Michael Chan50e3ab72018-10-14 07:02:49 -04002695
2696 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2697 continue;
2698
2699 if (i < bp->rx_nr_rings) {
2700 struct bnxt_cp_ring_info *cpr2 =
2701 bnxt_alloc_cp_sub_ring(bp);
2702
2703 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
2704 if (!cpr2)
2705 return -ENOMEM;
2706 cpr2->bnapi = bnapi;
2707 }
2708 if ((sh && i < bp->tx_nr_rings) ||
2709 (!sh && i >= bp->rx_nr_rings)) {
2710 struct bnxt_cp_ring_info *cpr2 =
2711 bnxt_alloc_cp_sub_ring(bp);
2712
2713 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
2714 if (!cpr2)
2715 return -ENOMEM;
2716 cpr2->bnapi = bnapi;
2717 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002718 }
2719 return 0;
2720}
2721
2722static void bnxt_init_ring_struct(struct bnxt *bp)
2723{
2724 int i;
2725
2726 for (i = 0; i < bp->cp_nr_rings; i++) {
2727 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chan6fe19882018-10-14 07:02:41 -04002728 struct bnxt_ring_mem_info *rmem;
Michael Chanc0c050c2015-10-22 16:01:17 -04002729 struct bnxt_cp_ring_info *cpr;
2730 struct bnxt_rx_ring_info *rxr;
2731 struct bnxt_tx_ring_info *txr;
2732 struct bnxt_ring_struct *ring;
2733
2734 if (!bnapi)
2735 continue;
2736
2737 cpr = &bnapi->cp_ring;
2738 ring = &cpr->cp_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002739 rmem = &ring->ring_mem;
2740 rmem->nr_pages = bp->cp_nr_pages;
2741 rmem->page_size = HW_CMPD_RING_SIZE;
2742 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2743 rmem->dma_arr = cpr->cp_desc_mapping;
2744 rmem->vmem_size = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04002745
Michael Chanb6ab4b02016-01-02 23:44:59 -05002746 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002747 if (!rxr)
2748 goto skip_rx;
2749
Michael Chanc0c050c2015-10-22 16:01:17 -04002750 ring = &rxr->rx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002751 rmem = &ring->ring_mem;
2752 rmem->nr_pages = bp->rx_nr_pages;
2753 rmem->page_size = HW_RXBD_RING_SIZE;
2754 rmem->pg_arr = (void **)rxr->rx_desc_ring;
2755 rmem->dma_arr = rxr->rx_desc_mapping;
2756 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2757 rmem->vmem = (void **)&rxr->rx_buf_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002758
2759 ring = &rxr->rx_agg_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002760 rmem = &ring->ring_mem;
2761 rmem->nr_pages = bp->rx_agg_nr_pages;
2762 rmem->page_size = HW_RXBD_RING_SIZE;
2763 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
2764 rmem->dma_arr = rxr->rx_agg_desc_mapping;
2765 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2766 rmem->vmem = (void **)&rxr->rx_agg_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002767
Michael Chan3b2b7d92016-01-02 23:45:00 -05002768skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002769 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002770 if (!txr)
2771 continue;
2772
Michael Chanc0c050c2015-10-22 16:01:17 -04002773 ring = &txr->tx_ring_struct;
Michael Chan6fe19882018-10-14 07:02:41 -04002774 rmem = &ring->ring_mem;
2775 rmem->nr_pages = bp->tx_nr_pages;
2776 rmem->page_size = HW_RXBD_RING_SIZE;
2777 rmem->pg_arr = (void **)txr->tx_desc_ring;
2778 rmem->dma_arr = txr->tx_desc_mapping;
2779 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2780 rmem->vmem = (void **)&txr->tx_buf_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04002781 }
2782}
2783
2784static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2785{
2786 int i;
2787 u32 prod;
2788 struct rx_bd **rx_buf_ring;
2789
Michael Chan6fe19882018-10-14 07:02:41 -04002790 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
2791 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002792 int j;
2793 struct rx_bd *rxbd;
2794
2795 rxbd = rx_buf_ring[i];
2796 if (!rxbd)
2797 continue;
2798
2799 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2800 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2801 rxbd->rx_bd_opaque = prod;
2802 }
2803 }
2804}
2805
2806static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2807{
2808 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002809 struct bnxt_rx_ring_info *rxr;
2810 struct bnxt_ring_struct *ring;
2811 u32 prod, type;
2812 int i;
2813
Michael Chanc0c050c2015-10-22 16:01:17 -04002814 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2815 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2816
2817 if (NET_IP_ALIGN == 2)
2818 type |= RX_BD_FLAGS_SOP;
2819
Michael Chanb6ab4b02016-01-02 23:44:59 -05002820 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002821 ring = &rxr->rx_ring_struct;
2822 bnxt_init_rxbd_pages(ring, type);
2823
Michael Chanc6d30e82017-02-06 16:55:42 -05002824 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2825 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2826 if (IS_ERR(rxr->xdp_prog)) {
2827 int rc = PTR_ERR(rxr->xdp_prog);
2828
2829 rxr->xdp_prog = NULL;
2830 return rc;
2831 }
2832 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002833 prod = rxr->rx_prod;
2834 for (i = 0; i < bp->rx_ring_size; i++) {
2835 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2836 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2837 ring_nr, i, bp->rx_ring_size);
2838 break;
2839 }
2840 prod = NEXT_RX(prod);
2841 }
2842 rxr->rx_prod = prod;
2843 ring->fw_ring_id = INVALID_HW_RING_ID;
2844
Michael Chanedd0c2c2015-12-27 18:19:19 -05002845 ring = &rxr->rx_agg_ring_struct;
2846 ring->fw_ring_id = INVALID_HW_RING_ID;
2847
Michael Chanc0c050c2015-10-22 16:01:17 -04002848 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2849 return 0;
2850
Michael Chan2839f282016-04-25 02:30:50 -04002851 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002852 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2853
2854 bnxt_init_rxbd_pages(ring, type);
2855
2856 prod = rxr->rx_agg_prod;
2857 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2858 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2859 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2860 ring_nr, i, bp->rx_ring_size);
2861 break;
2862 }
2863 prod = NEXT_RX_AGG(prod);
2864 }
2865 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002866
2867 if (bp->flags & BNXT_FLAG_TPA) {
2868 if (rxr->rx_tpa) {
2869 u8 *data;
2870 dma_addr_t mapping;
2871
2872 for (i = 0; i < MAX_TPA; i++) {
2873 data = __bnxt_alloc_rx_data(bp, &mapping,
2874 GFP_KERNEL);
2875 if (!data)
2876 return -ENOMEM;
2877
2878 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002879 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002880 rxr->rx_tpa[i].mapping = mapping;
2881 }
2882 } else {
2883 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2884 return -ENOMEM;
2885 }
2886 }
2887
2888 return 0;
2889}
2890
Sankar Patchineelam22479252017-03-28 19:47:29 -04002891static void bnxt_init_cp_rings(struct bnxt *bp)
2892{
Michael Chan3e08b182018-10-14 07:02:52 -04002893 int i, j;
Sankar Patchineelam22479252017-03-28 19:47:29 -04002894
2895 for (i = 0; i < bp->cp_nr_rings; i++) {
2896 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2897 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2898
2899 ring->fw_ring_id = INVALID_HW_RING_ID;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05002900 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2901 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
Michael Chan3e08b182018-10-14 07:02:52 -04002902 for (j = 0; j < 2; j++) {
2903 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2904
2905 if (!cpr2)
2906 continue;
2907
2908 ring = &cpr2->cp_ring_struct;
2909 ring->fw_ring_id = INVALID_HW_RING_ID;
2910 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2911 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2912 }
Sankar Patchineelam22479252017-03-28 19:47:29 -04002913 }
2914}
2915
Michael Chanc0c050c2015-10-22 16:01:17 -04002916static int bnxt_init_rx_rings(struct bnxt *bp)
2917{
2918 int i, rc = 0;
2919
Michael Chanc61fb992017-02-06 16:55:36 -05002920 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002921 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2922 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002923 } else {
2924 bp->rx_offset = BNXT_RX_OFFSET;
2925 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2926 }
Michael Chanb3dba772017-02-06 16:55:35 -05002927
Michael Chanc0c050c2015-10-22 16:01:17 -04002928 for (i = 0; i < bp->rx_nr_rings; i++) {
2929 rc = bnxt_init_one_rx_ring(bp, i);
2930 if (rc)
2931 break;
2932 }
2933
2934 return rc;
2935}
2936
2937static int bnxt_init_tx_rings(struct bnxt *bp)
2938{
2939 u16 i;
2940
2941 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2942 MAX_SKB_FRAGS + 1);
2943
2944 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002945 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002946 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2947
2948 ring->fw_ring_id = INVALID_HW_RING_ID;
2949 }
2950
2951 return 0;
2952}
2953
2954static void bnxt_free_ring_grps(struct bnxt *bp)
2955{
2956 kfree(bp->grp_info);
2957 bp->grp_info = NULL;
2958}
2959
2960static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2961{
2962 int i;
2963
2964 if (irq_re_init) {
2965 bp->grp_info = kcalloc(bp->cp_nr_rings,
2966 sizeof(struct bnxt_ring_grp_info),
2967 GFP_KERNEL);
2968 if (!bp->grp_info)
2969 return -ENOMEM;
2970 }
2971 for (i = 0; i < bp->cp_nr_rings; i++) {
2972 if (irq_re_init)
2973 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2974 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2975 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2976 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2977 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2978 }
2979 return 0;
2980}
2981
2982static void bnxt_free_vnics(struct bnxt *bp)
2983{
2984 kfree(bp->vnic_info);
2985 bp->vnic_info = NULL;
2986 bp->nr_vnics = 0;
2987}
2988
2989static int bnxt_alloc_vnics(struct bnxt *bp)
2990{
2991 int num_vnics = 1;
2992
2993#ifdef CONFIG_RFS_ACCEL
2994 if (bp->flags & BNXT_FLAG_RFS)
2995 num_vnics += bp->rx_nr_rings;
2996#endif
2997
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002998 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2999 num_vnics++;
3000
Michael Chanc0c050c2015-10-22 16:01:17 -04003001 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3002 GFP_KERNEL);
3003 if (!bp->vnic_info)
3004 return -ENOMEM;
3005
3006 bp->nr_vnics = num_vnics;
3007 return 0;
3008}
3009
3010static void bnxt_init_vnics(struct bnxt *bp)
3011{
3012 int i;
3013
3014 for (i = 0; i < bp->nr_vnics; i++) {
3015 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
Michael Chan44c6f722018-10-14 07:02:53 -04003016 int j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003017
3018 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Michael Chan44c6f722018-10-14 07:02:53 -04003019 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3020 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3021
Michael Chanc0c050c2015-10-22 16:01:17 -04003022 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3023
3024 if (bp->vnic_info[i].rss_hash_key) {
3025 if (i == 0)
3026 prandom_bytes(vnic->rss_hash_key,
3027 HW_HASH_KEY_SIZE);
3028 else
3029 memcpy(vnic->rss_hash_key,
3030 bp->vnic_info[0].rss_hash_key,
3031 HW_HASH_KEY_SIZE);
3032 }
3033 }
3034}
3035
3036static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3037{
3038 int pages;
3039
3040 pages = ring_size / desc_per_pg;
3041
3042 if (!pages)
3043 return 1;
3044
3045 pages++;
3046
3047 while (pages & (pages - 1))
3048 pages++;
3049
3050 return pages;
3051}
3052
Michael Chanc6d30e82017-02-06 16:55:42 -05003053void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04003054{
3055 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05003056 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3057 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003058 if (bp->dev->features & NETIF_F_LRO)
3059 bp->flags |= BNXT_FLAG_LRO;
Michael Chan1054aee2017-12-16 03:09:42 -05003060 else if (bp->dev->features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04003061 bp->flags |= BNXT_FLAG_GRO;
3062}
3063
3064/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3065 * be set on entry.
3066 */
3067void bnxt_set_ring_params(struct bnxt *bp)
3068{
3069 u32 ring_size, rx_size, rx_space;
3070 u32 agg_factor = 0, agg_ring_size = 0;
3071
3072 /* 8 for CRC and VLAN */
3073 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3074
3075 rx_space = rx_size + NET_SKB_PAD +
3076 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3077
3078 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3079 ring_size = bp->rx_ring_size;
3080 bp->rx_agg_ring_size = 0;
3081 bp->rx_agg_nr_pages = 0;
3082
3083 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04003084 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003085
3086 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05003087 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003088 u32 jumbo_factor;
3089
3090 bp->flags |= BNXT_FLAG_JUMBO;
3091 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3092 if (jumbo_factor > agg_factor)
3093 agg_factor = jumbo_factor;
3094 }
3095 agg_ring_size = ring_size * agg_factor;
3096
3097 if (agg_ring_size) {
3098 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3099 RX_DESC_CNT);
3100 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3101 u32 tmp = agg_ring_size;
3102
3103 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3104 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3105 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3106 tmp, agg_ring_size);
3107 }
3108 bp->rx_agg_ring_size = agg_ring_size;
3109 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3110 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3111 rx_space = rx_size + NET_SKB_PAD +
3112 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3113 }
3114
3115 bp->rx_buf_use_size = rx_size;
3116 bp->rx_buf_size = rx_space;
3117
3118 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3119 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3120
3121 ring_size = bp->tx_ring_size;
3122 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3123 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3124
3125 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
3126 bp->cp_ring_size = ring_size;
3127
3128 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3129 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3130 bp->cp_nr_pages = MAX_CP_PAGES;
3131 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3132 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3133 ring_size, bp->cp_ring_size);
3134 }
3135 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3136 bp->cp_ring_mask = bp->cp_bit - 1;
3137}
3138
Jesper Dangaard Brouer96a86042018-01-03 11:25:44 +01003139/* Changing allocation mode of RX rings.
3140 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3141 */
Michael Chanc61fb992017-02-06 16:55:36 -05003142int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05003143{
Michael Chanc61fb992017-02-06 16:55:36 -05003144 if (page_mode) {
3145 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3146 return -EOPNOTSUPP;
Michael Chan7eb9bb32017-10-26 11:51:25 -04003147 bp->dev->max_mtu =
3148 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
Michael Chanc61fb992017-02-06 16:55:36 -05003149 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3150 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
Michael Chanc61fb992017-02-06 16:55:36 -05003151 bp->rx_dir = DMA_BIDIRECTIONAL;
3152 bp->rx_skb_func = bnxt_rx_page_skb;
Michael Chan1054aee2017-12-16 03:09:42 -05003153 /* Disable LRO or GRO_HW */
3154 netdev_update_features(bp->dev);
Michael Chanc61fb992017-02-06 16:55:36 -05003155 } else {
Michael Chan7eb9bb32017-10-26 11:51:25 -04003156 bp->dev->max_mtu = bp->max_mtu;
Michael Chanc61fb992017-02-06 16:55:36 -05003157 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3158 bp->rx_dir = DMA_FROM_DEVICE;
3159 bp->rx_skb_func = bnxt_rx_skb;
3160 }
Michael Chan6bb19472017-02-06 16:55:32 -05003161 return 0;
3162}
3163
Michael Chanc0c050c2015-10-22 16:01:17 -04003164static void bnxt_free_vnic_attributes(struct bnxt *bp)
3165{
3166 int i;
3167 struct bnxt_vnic_info *vnic;
3168 struct pci_dev *pdev = bp->pdev;
3169
3170 if (!bp->vnic_info)
3171 return;
3172
3173 for (i = 0; i < bp->nr_vnics; i++) {
3174 vnic = &bp->vnic_info[i];
3175
3176 kfree(vnic->fw_grp_ids);
3177 vnic->fw_grp_ids = NULL;
3178
3179 kfree(vnic->uc_list);
3180 vnic->uc_list = NULL;
3181
3182 if (vnic->mc_list) {
3183 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3184 vnic->mc_list, vnic->mc_list_mapping);
3185 vnic->mc_list = NULL;
3186 }
3187
3188 if (vnic->rss_table) {
3189 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3190 vnic->rss_table,
3191 vnic->rss_table_dma_addr);
3192 vnic->rss_table = NULL;
3193 }
3194
3195 vnic->rss_hash_key = NULL;
3196 vnic->flags = 0;
3197 }
3198}
3199
3200static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3201{
3202 int i, rc = 0, size;
3203 struct bnxt_vnic_info *vnic;
3204 struct pci_dev *pdev = bp->pdev;
3205 int max_rings;
3206
3207 for (i = 0; i < bp->nr_vnics; i++) {
3208 vnic = &bp->vnic_info[i];
3209
3210 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3211 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3212
3213 if (mem_size > 0) {
3214 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3215 if (!vnic->uc_list) {
3216 rc = -ENOMEM;
3217 goto out;
3218 }
3219 }
3220 }
3221
3222 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3223 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3224 vnic->mc_list =
3225 dma_alloc_coherent(&pdev->dev,
3226 vnic->mc_list_size,
3227 &vnic->mc_list_mapping,
3228 GFP_KERNEL);
3229 if (!vnic->mc_list) {
3230 rc = -ENOMEM;
3231 goto out;
3232 }
3233 }
3234
Michael Chan44c6f722018-10-14 07:02:53 -04003235 if (bp->flags & BNXT_FLAG_CHIP_P5)
3236 goto vnic_skip_grps;
3237
Michael Chanc0c050c2015-10-22 16:01:17 -04003238 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3239 max_rings = bp->rx_nr_rings;
3240 else
3241 max_rings = 1;
3242
3243 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3244 if (!vnic->fw_grp_ids) {
3245 rc = -ENOMEM;
3246 goto out;
3247 }
Michael Chan44c6f722018-10-14 07:02:53 -04003248vnic_skip_grps:
Michael Chanae10ae72016-12-29 12:13:38 -05003249 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3250 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3251 continue;
3252
Michael Chanc0c050c2015-10-22 16:01:17 -04003253 /* Allocate rss table and hash key */
3254 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3255 &vnic->rss_table_dma_addr,
3256 GFP_KERNEL);
3257 if (!vnic->rss_table) {
3258 rc = -ENOMEM;
3259 goto out;
3260 }
3261
3262 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3263
3264 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3265 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3266 }
3267 return 0;
3268
3269out:
3270 return rc;
3271}
3272
3273static void bnxt_free_hwrm_resources(struct bnxt *bp)
3274{
3275 struct pci_dev *pdev = bp->pdev;
3276
Venkat Duvvurua2bf74f2018-10-05 00:26:02 -04003277 if (bp->hwrm_cmd_resp_addr) {
3278 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3279 bp->hwrm_cmd_resp_dma_addr);
3280 bp->hwrm_cmd_resp_addr = NULL;
3281 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003282}
3283
3284static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3285{
3286 struct pci_dev *pdev = bp->pdev;
3287
3288 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3289 &bp->hwrm_cmd_resp_dma_addr,
3290 GFP_KERNEL);
3291 if (!bp->hwrm_cmd_resp_addr)
3292 return -ENOMEM;
Michael Chanc0c050c2015-10-22 16:01:17 -04003293
3294 return 0;
3295}
3296
Deepak Khungare605db82017-05-29 19:06:04 -04003297static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3298{
3299 if (bp->hwrm_short_cmd_req_addr) {
3300 struct pci_dev *pdev = bp->pdev;
3301
Michael Chan1dfddc42018-10-14 07:02:39 -04003302 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
Deepak Khungare605db82017-05-29 19:06:04 -04003303 bp->hwrm_short_cmd_req_addr,
3304 bp->hwrm_short_cmd_req_dma_addr);
3305 bp->hwrm_short_cmd_req_addr = NULL;
3306 }
3307}
3308
3309static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3310{
3311 struct pci_dev *pdev = bp->pdev;
3312
3313 bp->hwrm_short_cmd_req_addr =
Michael Chan1dfddc42018-10-14 07:02:39 -04003314 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
Deepak Khungare605db82017-05-29 19:06:04 -04003315 &bp->hwrm_short_cmd_req_dma_addr,
3316 GFP_KERNEL);
3317 if (!bp->hwrm_short_cmd_req_addr)
3318 return -ENOMEM;
3319
3320 return 0;
3321}
3322
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003323static void bnxt_free_port_stats(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04003324{
Michael Chanc0c050c2015-10-22 16:01:17 -04003325 struct pci_dev *pdev = bp->pdev;
3326
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003327 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3328 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3329
Michael Chan3bdf56c2016-03-07 15:38:45 -05003330 if (bp->hw_rx_port_stats) {
3331 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3332 bp->hw_rx_port_stats,
3333 bp->hw_rx_port_stats_map);
3334 bp->hw_rx_port_stats = NULL;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003335 }
3336
Michael Chan36e53342018-10-14 07:02:38 -04003337 if (bp->hw_tx_port_stats_ext) {
3338 dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
3339 bp->hw_tx_port_stats_ext,
3340 bp->hw_tx_port_stats_ext_map);
3341 bp->hw_tx_port_stats_ext = NULL;
3342 }
3343
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003344 if (bp->hw_rx_port_stats_ext) {
3345 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3346 bp->hw_rx_port_stats_ext,
3347 bp->hw_rx_port_stats_ext_map);
3348 bp->hw_rx_port_stats_ext = NULL;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003349 }
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003350}
3351
3352static void bnxt_free_ring_stats(struct bnxt *bp)
3353{
3354 struct pci_dev *pdev = bp->pdev;
3355 int size, i;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003356
Michael Chanc0c050c2015-10-22 16:01:17 -04003357 if (!bp->bnapi)
3358 return;
3359
3360 size = sizeof(struct ctx_hw_stats);
3361
3362 for (i = 0; i < bp->cp_nr_rings; i++) {
3363 struct bnxt_napi *bnapi = bp->bnapi[i];
3364 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3365
3366 if (cpr->hw_stats) {
3367 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3368 cpr->hw_stats_map);
3369 cpr->hw_stats = NULL;
3370 }
3371 }
3372}
3373
3374static int bnxt_alloc_stats(struct bnxt *bp)
3375{
3376 u32 size, i;
3377 struct pci_dev *pdev = bp->pdev;
3378
3379 size = sizeof(struct ctx_hw_stats);
3380
3381 for (i = 0; i < bp->cp_nr_rings; i++) {
3382 struct bnxt_napi *bnapi = bp->bnapi[i];
3383 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3384
3385 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3386 &cpr->hw_stats_map,
3387 GFP_KERNEL);
3388 if (!cpr->hw_stats)
3389 return -ENOMEM;
3390
3391 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3392 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003393
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003394 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003395 if (bp->hw_rx_port_stats)
3396 goto alloc_ext_stats;
3397
Michael Chan3bdf56c2016-03-07 15:38:45 -05003398 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3399 sizeof(struct tx_port_stats) + 1024;
3400
3401 bp->hw_rx_port_stats =
3402 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3403 &bp->hw_rx_port_stats_map,
3404 GFP_KERNEL);
3405 if (!bp->hw_rx_port_stats)
3406 return -ENOMEM;
3407
3408 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3409 512;
3410 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3411 sizeof(struct rx_port_stats) + 512;
3412 bp->flags |= BNXT_FLAG_PORT_STATS;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003413
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003414alloc_ext_stats:
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003415 /* Display extended statistics only if FW supports it */
3416 if (bp->hwrm_spec_code < 0x10804 ||
3417 bp->hwrm_spec_code == 0x10900)
3418 return 0;
3419
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003420 if (bp->hw_rx_port_stats_ext)
3421 goto alloc_tx_ext_stats;
3422
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003423 bp->hw_rx_port_stats_ext =
3424 dma_zalloc_coherent(&pdev->dev,
3425 sizeof(struct rx_port_stats_ext),
3426 &bp->hw_rx_port_stats_ext_map,
3427 GFP_KERNEL);
3428 if (!bp->hw_rx_port_stats_ext)
3429 return 0;
3430
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003431alloc_tx_ext_stats:
3432 if (bp->hw_tx_port_stats_ext)
3433 return 0;
3434
Michael Chan36e53342018-10-14 07:02:38 -04003435 if (bp->hwrm_spec_code >= 0x10902) {
3436 bp->hw_tx_port_stats_ext =
3437 dma_zalloc_coherent(&pdev->dev,
3438 sizeof(struct tx_port_stats_ext),
3439 &bp->hw_tx_port_stats_ext_map,
3440 GFP_KERNEL);
3441 }
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04003442 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
Michael Chan3bdf56c2016-03-07 15:38:45 -05003443 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003444 return 0;
3445}
3446
3447static void bnxt_clear_ring_indices(struct bnxt *bp)
3448{
3449 int i;
3450
3451 if (!bp->bnapi)
3452 return;
3453
3454 for (i = 0; i < bp->cp_nr_rings; i++) {
3455 struct bnxt_napi *bnapi = bp->bnapi[i];
3456 struct bnxt_cp_ring_info *cpr;
3457 struct bnxt_rx_ring_info *rxr;
3458 struct bnxt_tx_ring_info *txr;
3459
3460 if (!bnapi)
3461 continue;
3462
3463 cpr = &bnapi->cp_ring;
3464 cpr->cp_raw_cons = 0;
3465
Michael Chanb6ab4b02016-01-02 23:44:59 -05003466 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003467 if (txr) {
3468 txr->tx_prod = 0;
3469 txr->tx_cons = 0;
3470 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003471
Michael Chanb6ab4b02016-01-02 23:44:59 -05003472 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003473 if (rxr) {
3474 rxr->rx_prod = 0;
3475 rxr->rx_agg_prod = 0;
3476 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003477 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003478 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003479 }
3480}
3481
3482static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3483{
3484#ifdef CONFIG_RFS_ACCEL
3485 int i;
3486
3487 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3488 * safe to delete the hash table.
3489 */
3490 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3491 struct hlist_head *head;
3492 struct hlist_node *tmp;
3493 struct bnxt_ntuple_filter *fltr;
3494
3495 head = &bp->ntp_fltr_hash_tbl[i];
3496 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3497 hlist_del(&fltr->hash);
3498 kfree(fltr);
3499 }
3500 }
3501 if (irq_reinit) {
3502 kfree(bp->ntp_fltr_bmap);
3503 bp->ntp_fltr_bmap = NULL;
3504 }
3505 bp->ntp_fltr_count = 0;
3506#endif
3507}
3508
3509static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3510{
3511#ifdef CONFIG_RFS_ACCEL
3512 int i, rc = 0;
3513
3514 if (!(bp->flags & BNXT_FLAG_RFS))
3515 return 0;
3516
3517 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3518 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3519
3520 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003521 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3522 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003523 GFP_KERNEL);
3524
3525 if (!bp->ntp_fltr_bmap)
3526 rc = -ENOMEM;
3527
3528 return rc;
3529#else
3530 return 0;
3531#endif
3532}
3533
3534static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3535{
3536 bnxt_free_vnic_attributes(bp);
3537 bnxt_free_tx_rings(bp);
3538 bnxt_free_rx_rings(bp);
3539 bnxt_free_cp_rings(bp);
3540 bnxt_free_ntp_fltrs(bp, irq_re_init);
3541 if (irq_re_init) {
Michael Chanfd3ab1c2018-12-16 18:46:30 -05003542 bnxt_free_ring_stats(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04003543 bnxt_free_ring_grps(bp);
3544 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003545 kfree(bp->tx_ring_map);
3546 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003547 kfree(bp->tx_ring);
3548 bp->tx_ring = NULL;
3549 kfree(bp->rx_ring);
3550 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003551 kfree(bp->bnapi);
3552 bp->bnapi = NULL;
3553 } else {
3554 bnxt_clear_ring_indices(bp);
3555 }
3556}
3557
3558static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3559{
Michael Chan01657bc2016-01-02 23:45:03 -05003560 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003561 void *bnapi;
3562
3563 if (irq_re_init) {
3564 /* Allocate bnapi mem pointer array and mem block for
3565 * all queues
3566 */
3567 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3568 bp->cp_nr_rings);
3569 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3570 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3571 if (!bnapi)
3572 return -ENOMEM;
3573
3574 bp->bnapi = bnapi;
3575 bnapi += arr_size;
3576 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3577 bp->bnapi[i] = bnapi;
3578 bp->bnapi[i]->index = i;
3579 bp->bnapi[i]->bp = bp;
Michael Chane38287b2018-10-14 07:02:45 -04003580 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3581 struct bnxt_cp_ring_info *cpr =
3582 &bp->bnapi[i]->cp_ring;
3583
3584 cpr->cp_ring_struct.ring_mem.flags =
3585 BNXT_RMEM_RING_PTE_FLAG;
3586 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003587 }
3588
Michael Chanb6ab4b02016-01-02 23:44:59 -05003589 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3590 sizeof(struct bnxt_rx_ring_info),
3591 GFP_KERNEL);
3592 if (!bp->rx_ring)
3593 return -ENOMEM;
3594
3595 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chane38287b2018-10-14 07:02:45 -04003596 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3597
3598 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3599 rxr->rx_ring_struct.ring_mem.flags =
3600 BNXT_RMEM_RING_PTE_FLAG;
3601 rxr->rx_agg_ring_struct.ring_mem.flags =
3602 BNXT_RMEM_RING_PTE_FLAG;
3603 }
3604 rxr->bnapi = bp->bnapi[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05003605 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3606 }
3607
3608 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3609 sizeof(struct bnxt_tx_ring_info),
3610 GFP_KERNEL);
3611 if (!bp->tx_ring)
3612 return -ENOMEM;
3613
Michael Chana960dec2017-02-06 16:55:39 -05003614 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3615 GFP_KERNEL);
3616
3617 if (!bp->tx_ring_map)
3618 return -ENOMEM;
3619
Michael Chan01657bc2016-01-02 23:45:03 -05003620 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3621 j = 0;
3622 else
3623 j = bp->rx_nr_rings;
3624
3625 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
Michael Chane38287b2018-10-14 07:02:45 -04003626 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3627
3628 if (bp->flags & BNXT_FLAG_CHIP_P5)
3629 txr->tx_ring_struct.ring_mem.flags =
3630 BNXT_RMEM_RING_PTE_FLAG;
3631 txr->bnapi = bp->bnapi[j];
3632 bp->bnapi[j]->tx_ring = txr;
Michael Chan5f449242017-02-06 16:55:40 -05003633 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003634 if (i >= bp->tx_nr_rings_xdp) {
Michael Chane38287b2018-10-14 07:02:45 -04003635 txr->txq_index = i - bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003636 bp->bnapi[j]->tx_int = bnxt_tx_int;
3637 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003638 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003639 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3640 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003641 }
3642
Michael Chanc0c050c2015-10-22 16:01:17 -04003643 rc = bnxt_alloc_stats(bp);
3644 if (rc)
3645 goto alloc_mem_err;
3646
3647 rc = bnxt_alloc_ntp_fltrs(bp);
3648 if (rc)
3649 goto alloc_mem_err;
3650
3651 rc = bnxt_alloc_vnics(bp);
3652 if (rc)
3653 goto alloc_mem_err;
3654 }
3655
3656 bnxt_init_ring_struct(bp);
3657
3658 rc = bnxt_alloc_rx_rings(bp);
3659 if (rc)
3660 goto alloc_mem_err;
3661
3662 rc = bnxt_alloc_tx_rings(bp);
3663 if (rc)
3664 goto alloc_mem_err;
3665
3666 rc = bnxt_alloc_cp_rings(bp);
3667 if (rc)
3668 goto alloc_mem_err;
3669
3670 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3671 BNXT_VNIC_UCAST_FLAG;
3672 rc = bnxt_alloc_vnic_attributes(bp);
3673 if (rc)
3674 goto alloc_mem_err;
3675 return 0;
3676
3677alloc_mem_err:
3678 bnxt_free_mem(bp, true);
3679 return rc;
3680}
3681
Michael Chan9d8bc092016-12-29 12:13:33 -05003682static void bnxt_disable_int(struct bnxt *bp)
3683{
3684 int i;
3685
3686 if (!bp->bnapi)
3687 return;
3688
3689 for (i = 0; i < bp->cp_nr_rings; i++) {
3690 struct bnxt_napi *bnapi = bp->bnapi[i];
3691 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003692 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003693
Michael Chandaf1f1e2017-02-20 19:25:17 -05003694 if (ring->fw_ring_id != INVALID_HW_RING_ID)
Michael Chan697197e2018-10-14 07:02:46 -04003695 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003696 }
3697}
3698
Michael Chane5811b82018-03-31 13:54:18 -04003699static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3700{
3701 struct bnxt_napi *bnapi = bp->bnapi[n];
3702 struct bnxt_cp_ring_info *cpr;
3703
3704 cpr = &bnapi->cp_ring;
3705 return cpr->cp_ring_struct.map_idx;
3706}
3707
Michael Chan9d8bc092016-12-29 12:13:33 -05003708static void bnxt_disable_int_sync(struct bnxt *bp)
3709{
3710 int i;
3711
3712 atomic_inc(&bp->intr_sem);
3713
3714 bnxt_disable_int(bp);
Michael Chane5811b82018-03-31 13:54:18 -04003715 for (i = 0; i < bp->cp_nr_rings; i++) {
3716 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3717
3718 synchronize_irq(bp->irq_tbl[map_idx].vector);
3719 }
Michael Chan9d8bc092016-12-29 12:13:33 -05003720}
3721
3722static void bnxt_enable_int(struct bnxt *bp)
3723{
3724 int i;
3725
3726 atomic_set(&bp->intr_sem, 0);
3727 for (i = 0; i < bp->cp_nr_rings; i++) {
3728 struct bnxt_napi *bnapi = bp->bnapi[i];
3729 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3730
Michael Chan697197e2018-10-14 07:02:46 -04003731 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003732 }
3733}
3734
Michael Chanc0c050c2015-10-22 16:01:17 -04003735void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3736 u16 cmpl_ring, u16 target_id)
3737{
Michael Chana8643e12016-02-26 04:00:05 -05003738 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003739
Michael Chana8643e12016-02-26 04:00:05 -05003740 req->req_type = cpu_to_le16(req_type);
3741 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3742 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003743 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3744}
3745
Michael Chanfbfbc482016-02-26 04:00:07 -05003746static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3747 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003748{
Michael Chana11fa2b2016-05-15 03:04:47 -04003749 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003750 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003751 u32 *data = msg;
Michael Chan845adfe2018-03-31 13:54:15 -04003752 __le32 *resp_len;
3753 u8 *valid;
Michael Chanc0c050c2015-10-22 16:01:17 -04003754 u16 cp_ring_id, len = 0;
3755 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003756 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Vasundhara Volamebd58182017-12-01 03:13:05 -05003757 struct hwrm_short_input short_input = {0};
Venkat Duvvuru2e9ee392018-12-20 03:38:45 -05003758 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
Venkat Duvvuru89455012018-12-20 03:38:46 -05003759 u8 *resp_addr = (u8 *)bp->hwrm_cmd_resp_addr;
Venkat Duvvuru2e9ee392018-12-20 03:38:45 -05003760 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
Michael Chanc0c050c2015-10-22 16:01:17 -04003761
Venkat Duvvuru5c209fc2018-12-20 03:38:47 -05003762 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp));
Michael Chanc0c050c2015-10-22 16:01:17 -04003763 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003764 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003765 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3766
Michael Chan1dfddc42018-10-14 07:02:39 -04003767 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
3768 if (msg_len > bp->hwrm_max_ext_req_len ||
3769 !bp->hwrm_short_cmd_req_addr)
3770 return -EINVAL;
3771 }
3772
3773 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
3774 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
Deepak Khungare605db82017-05-29 19:06:04 -04003775 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
Michael Chan1dfddc42018-10-14 07:02:39 -04003776 u16 max_msg_len;
3777
3778 /* Set boundary for maximum extended request length for short
3779 * cmd format. If passed up from device use the max supported
3780 * internal req length.
3781 */
3782 max_msg_len = bp->hwrm_max_ext_req_len;
Deepak Khungare605db82017-05-29 19:06:04 -04003783
3784 memcpy(short_cmd_req, req, msg_len);
Michael Chan1dfddc42018-10-14 07:02:39 -04003785 if (msg_len < max_msg_len)
3786 memset(short_cmd_req + msg_len, 0,
3787 max_msg_len - msg_len);
Deepak Khungare605db82017-05-29 19:06:04 -04003788
3789 short_input.req_type = req->req_type;
3790 short_input.signature =
3791 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3792 short_input.size = cpu_to_le16(msg_len);
3793 short_input.req_addr =
3794 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3795
3796 data = (u32 *)&short_input;
3797 msg_len = sizeof(short_input);
3798
3799 /* Sync memory write before updating doorbell */
3800 wmb();
3801
3802 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3803 }
3804
Michael Chanc0c050c2015-10-22 16:01:17 -04003805 /* Write request msg to hwrm channel */
Venkat Duvvuru2e9ee392018-12-20 03:38:45 -05003806 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
Michael Chanc0c050c2015-10-22 16:01:17 -04003807
Deepak Khungare605db82017-05-29 19:06:04 -04003808 for (i = msg_len; i < max_req_len; i += 4)
Venkat Duvvuru2e9ee392018-12-20 03:38:45 -05003809 writel(0, bp->bar0 + bar_offset + i);
Michael Chand79979a2016-01-07 19:56:57 -05003810
Michael Chanc0c050c2015-10-22 16:01:17 -04003811 /* currently supports only one outstanding message */
3812 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003813 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003814
3815 /* Ring channel doorbell */
Venkat Duvvuru2e9ee392018-12-20 03:38:45 -05003816 writel(1, bp->bar0 + doorbell_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04003817
Michael Chanff4fe812016-02-26 04:00:04 -05003818 if (!timeout)
3819 timeout = DFLT_HWRM_CMD_TIMEOUT;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003820 /* convert timeout to usec */
3821 timeout *= 1000;
Michael Chanff4fe812016-02-26 04:00:04 -05003822
Michael Chanc0c050c2015-10-22 16:01:17 -04003823 i = 0;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003824 /* Short timeout for the first few iterations:
3825 * number of loops = number of loops for short timeout +
3826 * number of loops for standard timeout.
3827 */
3828 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3829 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3830 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
Venkat Duvvuru89455012018-12-20 03:38:46 -05003831 resp_len = (__le32 *)(resp_addr + HWRM_RESP_LEN_OFFSET);
3832
Michael Chanc0c050c2015-10-22 16:01:17 -04003833 if (intr_process) {
Venkat Duvvurufc718bb2018-12-20 03:38:44 -05003834 u16 seq_id = bp->hwrm_intr_seq_id;
3835
Michael Chanc0c050c2015-10-22 16:01:17 -04003836 /* Wait until hwrm response cmpl interrupt is processed */
Venkat Duvvurufc718bb2018-12-20 03:38:44 -05003837 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003838 i++ < tmo_count) {
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003839 /* on first few passes, just barely sleep */
3840 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3841 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3842 HWRM_SHORT_MAX_TIMEOUT);
3843 else
3844 usleep_range(HWRM_MIN_TIMEOUT,
3845 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003846 }
3847
Venkat Duvvurufc718bb2018-12-20 03:38:44 -05003848 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003849 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003850 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003851 return -1;
3852 }
Michael Chan845adfe2018-03-31 13:54:15 -04003853 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3854 HWRM_RESP_LEN_SFT;
Venkat Duvvuru89455012018-12-20 03:38:46 -05003855 valid = resp_addr + len - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003856 } else {
Michael Chancc559c12018-05-08 03:18:38 -04003857 int j;
3858
Michael Chanc0c050c2015-10-22 16:01:17 -04003859 /* Check if response len is updated */
Michael Chana11fa2b2016-05-15 03:04:47 -04003860 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003861 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3862 HWRM_RESP_LEN_SFT;
3863 if (len)
3864 break;
Andy Gospodarek9751e8e2018-04-26 17:44:39 -04003865 /* on first few passes, just barely sleep */
3866 if (i < DFLT_HWRM_CMD_TIMEOUT)
3867 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3868 HWRM_SHORT_MAX_TIMEOUT);
3869 else
3870 usleep_range(HWRM_MIN_TIMEOUT,
3871 HWRM_MAX_TIMEOUT);
Michael Chanc0c050c2015-10-22 16:01:17 -04003872 }
3873
Michael Chana11fa2b2016-05-15 03:04:47 -04003874 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003875 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003876 HWRM_TOTAL_TIMEOUT(i),
3877 le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003878 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003879 return -1;
3880 }
3881
Michael Chan845adfe2018-03-31 13:54:15 -04003882 /* Last byte of resp contains valid bit */
Venkat Duvvuru89455012018-12-20 03:38:46 -05003883 valid = resp_addr + len - 1;
Michael Chancc559c12018-05-08 03:18:38 -04003884 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
Michael Chan845adfe2018-03-31 13:54:15 -04003885 /* make sure we read from updated DMA memory */
3886 dma_rmb();
3887 if (*valid)
Michael Chanc0c050c2015-10-22 16:01:17 -04003888 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003889 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003890 }
3891
Michael Chancc559c12018-05-08 03:18:38 -04003892 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003893 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chancc559c12018-05-08 03:18:38 -04003894 HWRM_TOTAL_TIMEOUT(i),
3895 le16_to_cpu(req->req_type),
Michael Chana8643e12016-02-26 04:00:05 -05003896 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003897 return -1;
3898 }
3899 }
3900
Michael Chan845adfe2018-03-31 13:54:15 -04003901 /* Zero valid bit for compatibility. Valid bit in an older spec
3902 * may become a new field in a newer spec. We must make sure that
3903 * a new field not implemented by old spec will read zero.
3904 */
3905 *valid = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003906 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003907 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003908 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3909 le16_to_cpu(resp->req_type),
3910 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003911 return rc;
3912}
3913
3914int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3915{
3916 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003917}
3918
Michael Chancc72f3b2017-10-13 21:09:33 -04003919int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3920 int timeout)
3921{
3922 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3923}
3924
Michael Chanc0c050c2015-10-22 16:01:17 -04003925int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3926{
3927 int rc;
3928
3929 mutex_lock(&bp->hwrm_cmd_lock);
3930 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3931 mutex_unlock(&bp->hwrm_cmd_lock);
3932 return rc;
3933}
3934
Michael Chan90e209212016-02-26 04:00:08 -05003935int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3936 int timeout)
3937{
3938 int rc;
3939
3940 mutex_lock(&bp->hwrm_cmd_lock);
3941 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3942 mutex_unlock(&bp->hwrm_cmd_lock);
3943 return rc;
3944}
3945
Michael Chana1653b12016-12-07 00:26:20 -05003946int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3947 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003948{
3949 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003950 DECLARE_BITMAP(async_events_bmap, 256);
3951 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003952 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003953
3954 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3955
3956 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003957 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003958
Michael Chan25be8622016-04-05 14:09:00 -04003959 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3960 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3961 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3962
Michael Chana1653b12016-12-07 00:26:20 -05003963 if (bmap && bmap_size) {
3964 for (i = 0; i < bmap_size; i++) {
3965 if (test_bit(i, bmap))
3966 __set_bit(i, async_events_bmap);
3967 }
3968 }
3969
Michael Chan25be8622016-04-05 14:09:00 -04003970 for (i = 0; i < 8; i++)
3971 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3972
Michael Chana1653b12016-12-07 00:26:20 -05003973 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3974}
3975
3976static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3977{
Michael Chan25e1acd2018-08-05 16:51:55 -04003978 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chana1653b12016-12-07 00:26:20 -05003979 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25e1acd2018-08-05 16:51:55 -04003980 int rc;
Michael Chana1653b12016-12-07 00:26:20 -05003981
3982 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3983
3984 req.enables =
3985 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3986 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3987
Michael Chan11f15ed2016-04-05 14:08:55 -04003988 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chand4f52de02018-03-31 13:54:06 -04003989 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
3990 req.ver_maj_8b = DRV_VER_MAJ;
3991 req.ver_min_8b = DRV_VER_MIN;
3992 req.ver_upd_8b = DRV_VER_UPD;
3993 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
3994 req.ver_min = cpu_to_le16(DRV_VER_MIN);
3995 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003996
3997 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003998 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003999 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004000
Michael Chan9b0436c2017-07-11 13:05:36 -04004001 memset(data, 0, sizeof(data));
4002 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4003 u16 cmd = bnxt_vf_req_snif[i];
4004 unsigned int bit, idx;
4005
4006 idx = cmd / 32;
4007 bit = cmd % 32;
4008 data[idx] |= 1 << bit;
4009 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004010
Michael Chande68f5de2015-12-09 19:35:41 -05004011 for (i = 0; i < 8; i++)
4012 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4013
Michael Chanc0c050c2015-10-22 16:01:17 -04004014 req.enables |=
4015 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4016 }
4017
Michael Chan25e1acd2018-08-05 16:51:55 -04004018 mutex_lock(&bp->hwrm_cmd_lock);
4019 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4020 if (rc)
4021 rc = -EIO;
4022 else if (resp->flags &
4023 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4024 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4025 mutex_unlock(&bp->hwrm_cmd_lock);
4026 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004027}
4028
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05004029static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4030{
4031 struct hwrm_func_drv_unrgtr_input req = {0};
4032
4033 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4034 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4035}
4036
Michael Chanc0c050c2015-10-22 16:01:17 -04004037static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4038{
4039 u32 rc = 0;
4040 struct hwrm_tunnel_dst_port_free_input req = {0};
4041
4042 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4043 req.tunnel_type = tunnel_type;
4044
4045 switch (tunnel_type) {
4046 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4047 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
4048 break;
4049 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4050 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
4051 break;
4052 default:
4053 break;
4054 }
4055
4056 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4057 if (rc)
4058 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4059 rc);
4060 return rc;
4061}
4062
4063static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4064 u8 tunnel_type)
4065{
4066 u32 rc = 0;
4067 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4068 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4069
4070 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4071
4072 req.tunnel_type = tunnel_type;
4073 req.tunnel_dst_port_val = port;
4074
4075 mutex_lock(&bp->hwrm_cmd_lock);
4076 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4077 if (rc) {
4078 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4079 rc);
4080 goto err_out;
4081 }
4082
Christophe Jaillet57aac712016-11-22 06:14:40 +01004083 switch (tunnel_type) {
4084 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04004085 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01004086 break;
4087 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04004088 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01004089 break;
4090 default:
4091 break;
4092 }
4093
Michael Chanc0c050c2015-10-22 16:01:17 -04004094err_out:
4095 mutex_unlock(&bp->hwrm_cmd_lock);
4096 return rc;
4097}
4098
4099static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4100{
4101 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4102 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4103
4104 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05004105 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004106
4107 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4108 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4109 req.mask = cpu_to_le32(vnic->rx_mask);
4110 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4111}
4112
4113#ifdef CONFIG_RFS_ACCEL
4114static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4115 struct bnxt_ntuple_filter *fltr)
4116{
4117 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4118
4119 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4120 req.ntuple_filter_id = fltr->filter_id;
4121 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4122}
4123
4124#define BNXT_NTP_FLTR_FLAGS \
4125 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4126 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4127 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4128 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4129 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4130 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4131 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4132 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4133 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4134 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4135 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4136 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4137 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05004138 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04004139
Michael Chan61aad722017-02-12 19:18:14 -05004140#define BNXT_NTP_TUNNEL_FLTR_FLAG \
4141 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4142
Michael Chanc0c050c2015-10-22 16:01:17 -04004143static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4144 struct bnxt_ntuple_filter *fltr)
4145{
Michael Chanc0c050c2015-10-22 16:01:17 -04004146 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
Venkat Duvvuru5c209fc2018-12-20 03:38:47 -05004147 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4148 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4149 struct flow_keys *keys = &fltr->fkeys;
4150 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004151
4152 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04004153 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04004154
4155 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4156
4157 req.ethertype = htons(ETH_P_IP);
4158 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05004159 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04004160 req.ip_protocol = keys->basic.ip_proto;
4161
Michael Chandda0e742016-12-29 12:13:40 -05004162 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4163 int i;
4164
4165 req.ethertype = htons(ETH_P_IPV6);
4166 req.ip_addr_type =
4167 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4168 *(struct in6_addr *)&req.src_ipaddr[0] =
4169 keys->addrs.v6addrs.src;
4170 *(struct in6_addr *)&req.dst_ipaddr[0] =
4171 keys->addrs.v6addrs.dst;
4172 for (i = 0; i < 4; i++) {
4173 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4174 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4175 }
4176 } else {
4177 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4178 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4179 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4180 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4181 }
Michael Chan61aad722017-02-12 19:18:14 -05004182 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4183 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4184 req.tunnel_type =
4185 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4186 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004187
4188 req.src_port = keys->ports.src;
4189 req.src_port_mask = cpu_to_be16(0xffff);
4190 req.dst_port = keys->ports.dst;
4191 req.dst_port_mask = cpu_to_be16(0xffff);
4192
Michael Chanc1935542015-12-27 18:19:28 -05004193 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004194 mutex_lock(&bp->hwrm_cmd_lock);
4195 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Venkat Duvvuru5c209fc2018-12-20 03:38:47 -05004196 if (!rc) {
4197 resp = bnxt_get_hwrm_resp_addr(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04004198 fltr->filter_id = resp->ntuple_filter_id;
Venkat Duvvuru5c209fc2018-12-20 03:38:47 -05004199 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004200 mutex_unlock(&bp->hwrm_cmd_lock);
4201 return rc;
4202}
4203#endif
4204
4205static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4206 u8 *mac_addr)
4207{
4208 u32 rc = 0;
4209 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4210 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4211
4212 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004213 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4214 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4215 req.flags |=
4216 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05004217 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004218 req.enables =
4219 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05004220 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04004221 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4222 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4223 req.l2_addr_mask[0] = 0xff;
4224 req.l2_addr_mask[1] = 0xff;
4225 req.l2_addr_mask[2] = 0xff;
4226 req.l2_addr_mask[3] = 0xff;
4227 req.l2_addr_mask[4] = 0xff;
4228 req.l2_addr_mask[5] = 0xff;
4229
4230 mutex_lock(&bp->hwrm_cmd_lock);
4231 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4232 if (!rc)
4233 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4234 resp->l2_filter_id;
4235 mutex_unlock(&bp->hwrm_cmd_lock);
4236 return rc;
4237}
4238
4239static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4240{
4241 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4242 int rc = 0;
4243
4244 /* Any associated ntuple filters will also be cleared by firmware. */
4245 mutex_lock(&bp->hwrm_cmd_lock);
4246 for (i = 0; i < num_of_vnics; i++) {
4247 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4248
4249 for (j = 0; j < vnic->uc_filter_count; j++) {
4250 struct hwrm_cfa_l2_filter_free_input req = {0};
4251
4252 bnxt_hwrm_cmd_hdr_init(bp, &req,
4253 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4254
4255 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4256
4257 rc = _hwrm_send_message(bp, &req, sizeof(req),
4258 HWRM_CMD_TIMEOUT);
4259 }
4260 vnic->uc_filter_count = 0;
4261 }
4262 mutex_unlock(&bp->hwrm_cmd_lock);
4263
4264 return rc;
4265}
4266
4267static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4268{
4269 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4270 struct hwrm_vnic_tpa_cfg_input req = {0};
4271
Michael Chan3c4fe802018-03-09 23:46:10 -05004272 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4273 return 0;
4274
Michael Chanc0c050c2015-10-22 16:01:17 -04004275 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4276
4277 if (tpa_flags) {
4278 u16 mss = bp->dev->mtu - 40;
4279 u32 nsegs, n, segs = 0, flags;
4280
4281 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4282 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4283 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4284 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4285 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4286 if (tpa_flags & BNXT_FLAG_GRO)
4287 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4288
4289 req.flags = cpu_to_le32(flags);
4290
4291 req.enables =
4292 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05004293 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4294 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004295
4296 /* Number of segs are log2 units, and first packet is not
4297 * included as part of this units.
4298 */
Michael Chan2839f282016-04-25 02:30:50 -04004299 if (mss <= BNXT_RX_PAGE_SIZE) {
4300 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04004301 nsegs = (MAX_SKB_FRAGS - 1) * n;
4302 } else {
Michael Chan2839f282016-04-25 02:30:50 -04004303 n = mss / BNXT_RX_PAGE_SIZE;
4304 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04004305 n++;
4306 nsegs = (MAX_SKB_FRAGS - n) / n;
4307 }
4308
4309 segs = ilog2(nsegs);
4310 req.max_agg_segs = cpu_to_le16(segs);
4311 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05004312
4313 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04004314 }
4315 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4316
4317 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4318}
4319
Michael Chan2c61d212018-10-14 07:02:50 -04004320static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4321{
4322 struct bnxt_ring_grp_info *grp_info;
4323
4324 grp_info = &bp->grp_info[ring->grp_idx];
4325 return grp_info->cp_fw_ring_id;
4326}
4327
4328static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4329{
4330 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4331 struct bnxt_napi *bnapi = rxr->bnapi;
4332 struct bnxt_cp_ring_info *cpr;
4333
4334 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4335 return cpr->cp_ring_struct.fw_ring_id;
4336 } else {
4337 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4338 }
4339}
4340
4341static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4342{
4343 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4344 struct bnxt_napi *bnapi = txr->bnapi;
4345 struct bnxt_cp_ring_info *cpr;
4346
4347 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4348 return cpr->cp_ring_struct.fw_ring_id;
4349 } else {
4350 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4351 }
4352}
4353
Michael Chanc0c050c2015-10-22 16:01:17 -04004354static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
4355{
4356 u32 i, j, max_rings;
4357 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4358 struct hwrm_vnic_rss_cfg_input req = {0};
4359
Michael Chan7b3af4f2018-10-14 07:02:54 -04004360 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
4361 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04004362 return 0;
4363
4364 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4365 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05004366 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Michael Chan50f011b2018-08-05 16:51:51 -04004367 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004368 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
4369 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4370 max_rings = bp->rx_nr_rings - 1;
4371 else
4372 max_rings = bp->rx_nr_rings;
4373 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04004374 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004375 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004376
4377 /* Fill the RSS indirection table with ring group ids */
4378 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4379 if (j == max_rings)
4380 j = 0;
4381 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4382 }
4383
4384 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4385 req.hash_key_tbl_addr =
4386 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4387 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004388 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004389 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4390}
4391
Michael Chan7b3af4f2018-10-14 07:02:54 -04004392static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
4393{
4394 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4395 u32 i, j, k, nr_ctxs, max_rings = bp->rx_nr_rings;
4396 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4397 struct hwrm_vnic_rss_cfg_input req = {0};
4398
4399 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4400 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4401 if (!set_rss) {
4402 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4403 return 0;
4404 }
4405 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4406 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4407 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4408 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
4409 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
4410 for (i = 0, k = 0; i < nr_ctxs; i++) {
4411 __le16 *ring_tbl = vnic->rss_table;
4412 int rc;
4413
4414 req.ring_table_pair_index = i;
4415 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
4416 for (j = 0; j < 64; j++) {
4417 u16 ring_id;
4418
4419 ring_id = rxr->rx_ring_struct.fw_ring_id;
4420 *ring_tbl++ = cpu_to_le16(ring_id);
4421 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
4422 *ring_tbl++ = cpu_to_le16(ring_id);
4423 rxr++;
4424 k++;
4425 if (k == max_rings) {
4426 k = 0;
4427 rxr = &bp->rx_ring[0];
4428 }
4429 }
4430 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4431 if (rc)
4432 return -EIO;
4433 }
4434 return 0;
4435}
4436
Michael Chanc0c050c2015-10-22 16:01:17 -04004437static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4438{
4439 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4440 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4441
4442 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4443 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4444 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4445 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4446 req.enables =
4447 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4448 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4449 /* thresholds not implemented in firmware yet */
4450 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4451 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4452 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4453 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4454}
4455
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004456static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4457 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004458{
4459 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4460
4461 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4462 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004463 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004464
4465 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004466 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004467}
4468
4469static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4470{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004471 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04004472
4473 for (i = 0; i < bp->nr_vnics; i++) {
4474 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4475
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004476 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4477 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4478 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4479 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004480 }
4481 bp->rsscos_nr_ctxs = 0;
4482}
4483
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004484static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04004485{
4486 int rc;
4487 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4488 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4489 bp->hwrm_cmd_resp_addr;
4490
4491 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4492 -1);
4493
4494 mutex_lock(&bp->hwrm_cmd_lock);
4495 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4496 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004497 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04004498 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4499 mutex_unlock(&bp->hwrm_cmd_lock);
4500
4501 return rc;
4502}
4503
Michael Chanabe93ad2018-03-31 13:54:08 -04004504static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4505{
4506 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4507 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4508 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4509}
4510
Michael Chana588e452016-12-07 00:26:21 -05004511int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04004512{
Michael Chanb81a90d2016-01-02 23:45:01 -05004513 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004514 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4515 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04004516 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004517
4518 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004519
Michael Chan7b3af4f2018-10-14 07:02:54 -04004520 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4521 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4522
4523 req.default_rx_ring_id =
4524 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
4525 req.default_cmpl_ring_id =
4526 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
4527 req.enables =
4528 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
4529 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
4530 goto vnic_mru;
4531 }
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004532 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4533 /* Only RSS support for now TBD: COS & LB */
4534 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4535 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4536 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4537 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05004538 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4539 req.rss_rule =
4540 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4541 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4542 VNIC_CFG_REQ_ENABLES_MRU);
4543 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004544 } else {
4545 req.rss_rule = cpu_to_le16(0xffff);
4546 }
4547
4548 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4549 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004550 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4551 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4552 } else {
4553 req.cos_rule = cpu_to_le16(0xffff);
4554 }
4555
Michael Chanc0c050c2015-10-22 16:01:17 -04004556 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004557 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004558 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05004559 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004560 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4561 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004562
Michael Chanb81a90d2016-01-02 23:45:01 -05004563 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004564 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004565 req.lb_rule = cpu_to_le16(0xffff);
Michael Chan7b3af4f2018-10-14 07:02:54 -04004566vnic_mru:
Michael Chanc0c050c2015-10-22 16:01:17 -04004567 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4568 VLAN_HLEN);
4569
Michael Chan7b3af4f2018-10-14 07:02:54 -04004570 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chancf6645f2016-06-13 02:25:28 -04004571#ifdef CONFIG_BNXT_SRIOV
4572 if (BNXT_VF(bp))
4573 def_vlan = bp->vf.vlan;
4574#endif
4575 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04004576 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05004577 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
Michael Chanabe93ad2018-03-31 13:54:08 -04004578 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
Michael Chanc0c050c2015-10-22 16:01:17 -04004579
4580 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4581}
4582
4583static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4584{
4585 u32 rc = 0;
4586
4587 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4588 struct hwrm_vnic_free_input req = {0};
4589
4590 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4591 req.vnic_id =
4592 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4593
4594 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4595 if (rc)
4596 return rc;
4597 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4598 }
4599 return rc;
4600}
4601
4602static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4603{
4604 u16 i;
4605
4606 for (i = 0; i < bp->nr_vnics; i++)
4607 bnxt_hwrm_vnic_free_one(bp, i);
4608}
4609
Michael Chanb81a90d2016-01-02 23:45:01 -05004610static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4611 unsigned int start_rx_ring_idx,
4612 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004613{
Michael Chanb81a90d2016-01-02 23:45:01 -05004614 int rc = 0;
4615 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004616 struct hwrm_vnic_alloc_input req = {0};
4617 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan44c6f722018-10-14 07:02:53 -04004618 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4619
4620 if (bp->flags & BNXT_FLAG_CHIP_P5)
4621 goto vnic_no_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04004622
4623 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004624 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4625 grp_idx = bp->rx_ring[i].bnapi->index;
4626 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004627 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004628 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004629 break;
4630 }
Michael Chan44c6f722018-10-14 07:02:53 -04004631 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004632 }
4633
Michael Chan44c6f722018-10-14 07:02:53 -04004634vnic_no_ring_grps:
4635 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
4636 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004637 if (vnic_id == 0)
4638 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4639
4640 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4641
4642 mutex_lock(&bp->hwrm_cmd_lock);
4643 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4644 if (!rc)
Michael Chan44c6f722018-10-14 07:02:53 -04004645 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004646 mutex_unlock(&bp->hwrm_cmd_lock);
4647 return rc;
4648}
4649
Michael Chan8fdefd62016-12-29 12:13:36 -05004650static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4651{
4652 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4653 struct hwrm_vnic_qcaps_input req = {0};
4654 int rc;
4655
4656 if (bp->hwrm_spec_code < 0x10600)
4657 return 0;
4658
4659 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4660 mutex_lock(&bp->hwrm_cmd_lock);
4661 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4662 if (!rc) {
Michael Chanabe93ad2018-03-31 13:54:08 -04004663 u32 flags = le32_to_cpu(resp->flags);
4664
Michael Chan41e8d792018-10-14 07:02:48 -04004665 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
4666 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
Michael Chan8fdefd62016-12-29 12:13:36 -05004667 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
Michael Chanabe93ad2018-03-31 13:54:08 -04004668 if (flags &
4669 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4670 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
Michael Chan8fdefd62016-12-29 12:13:36 -05004671 }
4672 mutex_unlock(&bp->hwrm_cmd_lock);
4673 return rc;
4674}
4675
Michael Chanc0c050c2015-10-22 16:01:17 -04004676static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4677{
4678 u16 i;
4679 u32 rc = 0;
4680
Michael Chan44c6f722018-10-14 07:02:53 -04004681 if (bp->flags & BNXT_FLAG_CHIP_P5)
4682 return 0;
4683
Michael Chanc0c050c2015-10-22 16:01:17 -04004684 mutex_lock(&bp->hwrm_cmd_lock);
4685 for (i = 0; i < bp->rx_nr_rings; i++) {
4686 struct hwrm_ring_grp_alloc_input req = {0};
4687 struct hwrm_ring_grp_alloc_output *resp =
4688 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004689 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004690
4691 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4692
Michael Chanb81a90d2016-01-02 23:45:01 -05004693 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4694 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4695 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4696 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004697
4698 rc = _hwrm_send_message(bp, &req, sizeof(req),
4699 HWRM_CMD_TIMEOUT);
4700 if (rc)
4701 break;
4702
Michael Chanb81a90d2016-01-02 23:45:01 -05004703 bp->grp_info[grp_idx].fw_grp_id =
4704 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004705 }
4706 mutex_unlock(&bp->hwrm_cmd_lock);
4707 return rc;
4708}
4709
4710static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4711{
4712 u16 i;
4713 u32 rc = 0;
4714 struct hwrm_ring_grp_free_input req = {0};
4715
Michael Chan44c6f722018-10-14 07:02:53 -04004716 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
Michael Chanc0c050c2015-10-22 16:01:17 -04004717 return 0;
4718
4719 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4720
4721 mutex_lock(&bp->hwrm_cmd_lock);
4722 for (i = 0; i < bp->cp_nr_rings; i++) {
4723 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4724 continue;
4725 req.ring_group_id =
4726 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4727
4728 rc = _hwrm_send_message(bp, &req, sizeof(req),
4729 HWRM_CMD_TIMEOUT);
4730 if (rc)
4731 break;
4732 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4733 }
4734 mutex_unlock(&bp->hwrm_cmd_lock);
4735 return rc;
4736}
4737
4738static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4739 struct bnxt_ring_struct *ring,
Michael Chan9899bb52018-03-31 13:54:16 -04004740 u32 ring_type, u32 map_index)
Michael Chanc0c050c2015-10-22 16:01:17 -04004741{
4742 int rc = 0, err = 0;
4743 struct hwrm_ring_alloc_input req = {0};
4744 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6fe19882018-10-14 07:02:41 -04004745 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
Michael Chan9899bb52018-03-31 13:54:16 -04004746 struct bnxt_ring_grp_info *grp_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04004747 u16 ring_id;
4748
4749 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4750
4751 req.enables = 0;
Michael Chan6fe19882018-10-14 07:02:41 -04004752 if (rmem->nr_pages > 1) {
4753 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
Michael Chanc0c050c2015-10-22 16:01:17 -04004754 /* Page size is in log2 units */
4755 req.page_size = BNXT_PAGE_SHIFT;
4756 req.page_tbl_depth = 1;
4757 } else {
Michael Chan6fe19882018-10-14 07:02:41 -04004758 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04004759 }
4760 req.fbo = 0;
4761 /* Association of ring index with doorbell index and MSIX number */
4762 req.logical_id = cpu_to_le16(map_index);
4763
4764 switch (ring_type) {
Michael Chan2c61d212018-10-14 07:02:50 -04004765 case HWRM_RING_ALLOC_TX: {
4766 struct bnxt_tx_ring_info *txr;
4767
4768 txr = container_of(ring, struct bnxt_tx_ring_info,
4769 tx_ring_struct);
Michael Chanc0c050c2015-10-22 16:01:17 -04004770 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4771 /* Association of transmit ring with completion ring */
Michael Chan9899bb52018-03-31 13:54:16 -04004772 grp_info = &bp->grp_info[ring->grp_idx];
Michael Chan2c61d212018-10-14 07:02:50 -04004773 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
Michael Chanc0c050c2015-10-22 16:01:17 -04004774 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
Michael Chan9899bb52018-03-31 13:54:16 -04004775 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004776 req.queue_id = cpu_to_le16(ring->queue_id);
4777 break;
Michael Chan2c61d212018-10-14 07:02:50 -04004778 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004779 case HWRM_RING_ALLOC_RX:
4780 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4781 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
Michael Chan23aefdd2018-10-14 07:02:51 -04004782 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4783 u16 flags = 0;
4784
4785 /* Association of rx ring with stats context */
4786 grp_info = &bp->grp_info[ring->grp_idx];
4787 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
4788 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4789 req.enables |= cpu_to_le32(
4790 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4791 if (NET_IP_ALIGN == 2)
4792 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
4793 req.flags = cpu_to_le16(flags);
4794 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004795 break;
4796 case HWRM_RING_ALLOC_AGG:
Michael Chan23aefdd2018-10-14 07:02:51 -04004797 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4798 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
4799 /* Association of agg ring with rx ring */
4800 grp_info = &bp->grp_info[ring->grp_idx];
4801 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
4802 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
4803 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4804 req.enables |= cpu_to_le32(
4805 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
4806 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4807 } else {
4808 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4809 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004810 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4811 break;
4812 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004813 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004814 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
Michael Chan23aefdd2018-10-14 07:02:51 -04004815 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4816 /* Association of cp ring with nq */
4817 grp_info = &bp->grp_info[map_index];
4818 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
4819 req.cq_handle = cpu_to_le64(ring->handle);
4820 req.enables |= cpu_to_le32(
4821 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
4822 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
4823 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4824 }
4825 break;
4826 case HWRM_RING_ALLOC_NQ:
4827 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
4828 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004829 if (bp->flags & BNXT_FLAG_USING_MSIX)
4830 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4831 break;
4832 default:
4833 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4834 ring_type);
4835 return -1;
4836 }
4837
4838 mutex_lock(&bp->hwrm_cmd_lock);
4839 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4840 err = le16_to_cpu(resp->error_code);
4841 ring_id = le16_to_cpu(resp->ring_id);
4842 mutex_unlock(&bp->hwrm_cmd_lock);
4843
4844 if (rc || err) {
Michael Chan2727c882018-04-26 17:44:35 -04004845 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4846 ring_type, rc, err);
4847 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04004848 }
4849 ring->fw_ring_id = ring_id;
4850 return rc;
4851}
4852
Michael Chan486b5c22016-12-29 12:13:42 -05004853static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4854{
4855 int rc;
4856
4857 if (BNXT_PF(bp)) {
4858 struct hwrm_func_cfg_input req = {0};
4859
4860 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4861 req.fid = cpu_to_le16(0xffff);
4862 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4863 req.async_event_cr = cpu_to_le16(idx);
4864 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4865 } else {
4866 struct hwrm_func_vf_cfg_input req = {0};
4867
4868 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4869 req.enables =
4870 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4871 req.async_event_cr = cpu_to_le16(idx);
4872 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4873 }
4874 return rc;
4875}
4876
Michael Chan697197e2018-10-14 07:02:46 -04004877static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
4878 u32 map_idx, u32 xid)
4879{
4880 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4881 if (BNXT_PF(bp))
4882 db->doorbell = bp->bar1 + 0x10000;
4883 else
4884 db->doorbell = bp->bar1 + 0x4000;
4885 switch (ring_type) {
4886 case HWRM_RING_ALLOC_TX:
4887 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
4888 break;
4889 case HWRM_RING_ALLOC_RX:
4890 case HWRM_RING_ALLOC_AGG:
4891 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
4892 break;
4893 case HWRM_RING_ALLOC_CMPL:
4894 db->db_key64 = DBR_PATH_L2;
4895 break;
4896 case HWRM_RING_ALLOC_NQ:
4897 db->db_key64 = DBR_PATH_L2;
4898 break;
4899 }
4900 db->db_key64 |= (u64)xid << DBR_XID_SFT;
4901 } else {
4902 db->doorbell = bp->bar1 + map_idx * 0x80;
4903 switch (ring_type) {
4904 case HWRM_RING_ALLOC_TX:
4905 db->db_key32 = DB_KEY_TX;
4906 break;
4907 case HWRM_RING_ALLOC_RX:
4908 case HWRM_RING_ALLOC_AGG:
4909 db->db_key32 = DB_KEY_RX;
4910 break;
4911 case HWRM_RING_ALLOC_CMPL:
4912 db->db_key32 = DB_KEY_CP;
4913 break;
4914 }
4915 }
4916}
4917
Michael Chanc0c050c2015-10-22 16:01:17 -04004918static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4919{
4920 int i, rc = 0;
Michael Chan697197e2018-10-14 07:02:46 -04004921 u32 type;
Michael Chanc0c050c2015-10-22 16:01:17 -04004922
Michael Chan23aefdd2018-10-14 07:02:51 -04004923 if (bp->flags & BNXT_FLAG_CHIP_P5)
4924 type = HWRM_RING_ALLOC_NQ;
4925 else
4926 type = HWRM_RING_ALLOC_CMPL;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004927 for (i = 0; i < bp->cp_nr_rings; i++) {
4928 struct bnxt_napi *bnapi = bp->bnapi[i];
4929 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4930 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04004931 u32 map_idx = ring->map_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004932
Michael Chan697197e2018-10-14 07:02:46 -04004933 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004934 if (rc)
4935 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004936 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
4937 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004938 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004939
4940 if (!i) {
4941 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4942 if (rc)
4943 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4944 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004945 }
4946
Michael Chan697197e2018-10-14 07:02:46 -04004947 type = HWRM_RING_ALLOC_TX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004948 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004949 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chan3e08b182018-10-14 07:02:52 -04004950 struct bnxt_ring_struct *ring;
4951 u32 map_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004952
Michael Chan3e08b182018-10-14 07:02:52 -04004953 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4954 struct bnxt_napi *bnapi = txr->bnapi;
4955 struct bnxt_cp_ring_info *cpr, *cpr2;
4956 u32 type2 = HWRM_RING_ALLOC_CMPL;
4957
4958 cpr = &bnapi->cp_ring;
4959 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
4960 ring = &cpr2->cp_ring_struct;
4961 ring->handle = BNXT_TX_HDL;
4962 map_idx = bnapi->index;
4963 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
4964 if (rc)
4965 goto err_out;
4966 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
4967 ring->fw_ring_id);
4968 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
4969 }
4970 ring = &txr->tx_ring_struct;
4971 map_idx = i;
Michael Chan697197e2018-10-14 07:02:46 -04004972 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004973 if (rc)
4974 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004975 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004976 }
4977
Michael Chan697197e2018-10-14 07:02:46 -04004978 type = HWRM_RING_ALLOC_RX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004979 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004980 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004981 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chan3e08b182018-10-14 07:02:52 -04004982 struct bnxt_napi *bnapi = rxr->bnapi;
4983 u32 map_idx = bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004984
Michael Chan697197e2018-10-14 07:02:46 -04004985 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004986 if (rc)
4987 goto err_out;
Michael Chan697197e2018-10-14 07:02:46 -04004988 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
4989 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
Michael Chanb81a90d2016-01-02 23:45:01 -05004990 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chan3e08b182018-10-14 07:02:52 -04004991 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4992 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4993 u32 type2 = HWRM_RING_ALLOC_CMPL;
4994 struct bnxt_cp_ring_info *cpr2;
4995
4996 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
4997 ring = &cpr2->cp_ring_struct;
4998 ring->handle = BNXT_RX_HDL;
4999 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5000 if (rc)
5001 goto err_out;
5002 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5003 ring->fw_ring_id);
5004 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5005 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005006 }
5007
5008 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
Michael Chan697197e2018-10-14 07:02:46 -04005009 type = HWRM_RING_ALLOC_AGG;
Michael Chanc0c050c2015-10-22 16:01:17 -04005010 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005011 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005012 struct bnxt_ring_struct *ring =
5013 &rxr->rx_agg_ring_struct;
Michael Chan9899bb52018-03-31 13:54:16 -04005014 u32 grp_idx = ring->grp_idx;
Michael Chanb81a90d2016-01-02 23:45:01 -05005015 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005016
Michael Chan697197e2018-10-14 07:02:46 -04005017 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
Michael Chanc0c050c2015-10-22 16:01:17 -04005018 if (rc)
5019 goto err_out;
5020
Michael Chan697197e2018-10-14 07:02:46 -04005021 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5022 ring->fw_ring_id);
5023 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
Michael Chanb81a90d2016-01-02 23:45:01 -05005024 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005025 }
5026 }
5027err_out:
5028 return rc;
5029}
5030
5031static int hwrm_ring_free_send_msg(struct bnxt *bp,
5032 struct bnxt_ring_struct *ring,
5033 u32 ring_type, int cmpl_ring_id)
5034{
5035 int rc;
5036 struct hwrm_ring_free_input req = {0};
5037 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5038 u16 error_code;
5039
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05005040 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005041 req.ring_type = ring_type;
5042 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5043
5044 mutex_lock(&bp->hwrm_cmd_lock);
5045 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5046 error_code = le16_to_cpu(resp->error_code);
5047 mutex_unlock(&bp->hwrm_cmd_lock);
5048
5049 if (rc || error_code) {
Michael Chan2727c882018-04-26 17:44:35 -04005050 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5051 ring_type, rc, error_code);
5052 return -EIO;
Michael Chanc0c050c2015-10-22 16:01:17 -04005053 }
5054 return 0;
5055}
5056
Michael Chanedd0c2c2015-12-27 18:19:19 -05005057static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04005058{
Michael Chan23aefdd2018-10-14 07:02:51 -04005059 u32 type;
Michael Chanedd0c2c2015-12-27 18:19:19 -05005060 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005061
5062 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05005063 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04005064
Michael Chanedd0c2c2015-12-27 18:19:19 -05005065 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005066 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05005067 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chan2c61d212018-10-14 07:02:50 -04005068 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005069
Michael Chan2c61d212018-10-14 07:02:50 -04005070 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05005071 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5072 hwrm_ring_free_send_msg(bp, ring,
5073 RING_FREE_REQ_RING_TYPE_TX,
5074 close_path ? cmpl_ring_id :
5075 INVALID_HW_RING_ID);
5076 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04005077 }
5078 }
5079
Michael Chanedd0c2c2015-12-27 18:19:19 -05005080 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005081 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05005082 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05005083 u32 grp_idx = rxr->bnapi->index;
Michael Chan2c61d212018-10-14 07:02:50 -04005084 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005085
Michael Chan2c61d212018-10-14 07:02:50 -04005086 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05005087 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5088 hwrm_ring_free_send_msg(bp, ring,
5089 RING_FREE_REQ_RING_TYPE_RX,
5090 close_path ? cmpl_ring_id :
5091 INVALID_HW_RING_ID);
5092 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05005093 bp->grp_info[grp_idx].rx_fw_ring_id =
5094 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04005095 }
5096 }
5097
Michael Chan23aefdd2018-10-14 07:02:51 -04005098 if (bp->flags & BNXT_FLAG_CHIP_P5)
5099 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5100 else
5101 type = RING_FREE_REQ_RING_TYPE_RX;
Michael Chanedd0c2c2015-12-27 18:19:19 -05005102 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005103 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05005104 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05005105 u32 grp_idx = rxr->bnapi->index;
Michael Chan2c61d212018-10-14 07:02:50 -04005106 u32 cmpl_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005107
Michael Chan2c61d212018-10-14 07:02:50 -04005108 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
Michael Chanedd0c2c2015-12-27 18:19:19 -05005109 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
Michael Chan23aefdd2018-10-14 07:02:51 -04005110 hwrm_ring_free_send_msg(bp, ring, type,
Michael Chanedd0c2c2015-12-27 18:19:19 -05005111 close_path ? cmpl_ring_id :
5112 INVALID_HW_RING_ID);
5113 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05005114 bp->grp_info[grp_idx].agg_fw_ring_id =
5115 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04005116 }
5117 }
5118
Michael Chan9d8bc092016-12-29 12:13:33 -05005119 /* The completion rings are about to be freed. After that the
5120 * IRQ doorbell will not work anymore. So we need to disable
5121 * IRQ here.
5122 */
5123 bnxt_disable_int_sync(bp);
5124
Michael Chan23aefdd2018-10-14 07:02:51 -04005125 if (bp->flags & BNXT_FLAG_CHIP_P5)
5126 type = RING_FREE_REQ_RING_TYPE_NQ;
5127 else
5128 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
Michael Chanedd0c2c2015-12-27 18:19:19 -05005129 for (i = 0; i < bp->cp_nr_rings; i++) {
5130 struct bnxt_napi *bnapi = bp->bnapi[i];
5131 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chan3e08b182018-10-14 07:02:52 -04005132 struct bnxt_ring_struct *ring;
5133 int j;
Michael Chanc0c050c2015-10-22 16:01:17 -04005134
Michael Chan3e08b182018-10-14 07:02:52 -04005135 for (j = 0; j < 2; j++) {
5136 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5137
5138 if (cpr2) {
5139 ring = &cpr2->cp_ring_struct;
5140 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5141 continue;
5142 hwrm_ring_free_send_msg(bp, ring,
5143 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5144 INVALID_HW_RING_ID);
5145 ring->fw_ring_id = INVALID_HW_RING_ID;
5146 }
5147 }
5148 ring = &cpr->cp_ring_struct;
Michael Chanedd0c2c2015-12-27 18:19:19 -05005149 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
Michael Chan23aefdd2018-10-14 07:02:51 -04005150 hwrm_ring_free_send_msg(bp, ring, type,
Michael Chanedd0c2c2015-12-27 18:19:19 -05005151 INVALID_HW_RING_ID);
5152 ring->fw_ring_id = INVALID_HW_RING_ID;
5153 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04005154 }
5155 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005156}
5157
Michael Chan41e8d792018-10-14 07:02:48 -04005158static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5159 bool shared);
5160
Michael Chan674f50a2018-01-17 03:21:09 -05005161static int bnxt_hwrm_get_rings(struct bnxt *bp)
5162{
5163 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5164 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5165 struct hwrm_func_qcfg_input req = {0};
5166 int rc;
5167
5168 if (bp->hwrm_spec_code < 0x10601)
5169 return 0;
5170
5171 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5172 req.fid = cpu_to_le16(0xffff);
5173 mutex_lock(&bp->hwrm_cmd_lock);
5174 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5175 if (rc) {
5176 mutex_unlock(&bp->hwrm_cmd_lock);
5177 return -EIO;
5178 }
5179
5180 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
Michael Chanf1ca94d2018-08-05 16:51:53 -04005181 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005182 u16 cp, stats;
5183
5184 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5185 hw_resc->resv_hw_ring_grps =
5186 le32_to_cpu(resp->alloc_hw_ring_grps);
5187 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5188 cp = le16_to_cpu(resp->alloc_cmpl_rings);
5189 stats = le16_to_cpu(resp->alloc_stat_ctx);
Michael Chan75720e62018-12-09 07:01:00 -05005190 hw_resc->resv_irqs = cp;
Michael Chan41e8d792018-10-14 07:02:48 -04005191 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5192 int rx = hw_resc->resv_rx_rings;
5193 int tx = hw_resc->resv_tx_rings;
5194
5195 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5196 rx >>= 1;
5197 if (cp < (rx + tx)) {
5198 bnxt_trim_rings(bp, &rx, &tx, cp, false);
5199 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5200 rx <<= 1;
5201 hw_resc->resv_rx_rings = rx;
5202 hw_resc->resv_tx_rings = tx;
5203 }
Michael Chan75720e62018-12-09 07:01:00 -05005204 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
Michael Chan41e8d792018-10-14 07:02:48 -04005205 hw_resc->resv_hw_ring_grps = rx;
5206 }
Michael Chan674f50a2018-01-17 03:21:09 -05005207 hw_resc->resv_cp_rings = cp;
Vasundhara Volam780baad2018-12-16 18:46:23 -05005208 hw_resc->resv_stat_ctxs = stats;
Michael Chan674f50a2018-01-17 03:21:09 -05005209 }
5210 mutex_unlock(&bp->hwrm_cmd_lock);
5211 return 0;
5212}
5213
Michael Chan391be5c2016-12-29 12:13:41 -05005214/* Caller must hold bp->hwrm_cmd_lock */
5215int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5216{
5217 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5218 struct hwrm_func_qcfg_input req = {0};
5219 int rc;
5220
5221 if (bp->hwrm_spec_code < 0x10601)
5222 return 0;
5223
5224 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5225 req.fid = cpu_to_le16(fid);
5226 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5227 if (!rc)
5228 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5229
5230 return rc;
5231}
5232
Michael Chan41e8d792018-10-14 07:02:48 -04005233static bool bnxt_rfs_supported(struct bnxt *bp);
5234
Michael Chan4ed50ef2018-03-09 23:46:03 -05005235static void
5236__bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5237 int tx_rings, int rx_rings, int ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005238 int cp_rings, int stats, int vnics)
Michael Chan391be5c2016-12-29 12:13:41 -05005239{
Michael Chan674f50a2018-01-17 03:21:09 -05005240 u32 enables = 0;
Michael Chan391be5c2016-12-29 12:13:41 -05005241
Michael Chan4ed50ef2018-03-09 23:46:03 -05005242 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5243 req->fid = cpu_to_le16(0xffff);
Michael Chan674f50a2018-01-17 03:21:09 -05005244 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
Michael Chan4ed50ef2018-03-09 23:46:03 -05005245 req->num_tx_rings = cpu_to_le16(tx_rings);
Michael Chanf1ca94d2018-08-05 16:51:53 -04005246 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005247 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005248 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5249 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5250 enables |= tx_rings + ring_grps ?
5251 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5252 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5253 enables |= rx_rings ?
5254 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5255 } else {
5256 enables |= cp_rings ?
5257 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5258 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5259 enables |= ring_grps ?
5260 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5261 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5262 }
Michael Chandbe80d42018-10-05 00:26:00 -04005263 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
Michael Chan674f50a2018-01-17 03:21:09 -05005264
Michael Chan4ed50ef2018-03-09 23:46:03 -05005265 req->num_rx_rings = cpu_to_le16(rx_rings);
Michael Chan41e8d792018-10-14 07:02:48 -04005266 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5267 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5268 req->num_msix = cpu_to_le16(cp_rings);
5269 req->num_rsscos_ctxs =
5270 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5271 } else {
5272 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5273 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5274 req->num_rsscos_ctxs = cpu_to_le16(1);
5275 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5276 bnxt_rfs_supported(bp))
5277 req->num_rsscos_ctxs =
5278 cpu_to_le16(ring_grps + 1);
5279 }
Vasundhara Volam780baad2018-12-16 18:46:23 -05005280 req->num_stat_ctxs = cpu_to_le16(stats);
Michael Chan4ed50ef2018-03-09 23:46:03 -05005281 req->num_vnics = cpu_to_le16(vnics);
Michael Chan674f50a2018-01-17 03:21:09 -05005282 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005283 req->enables = cpu_to_le32(enables);
5284}
5285
5286static void
5287__bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5288 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5289 int rx_rings, int ring_grps, int cp_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005290 int stats, int vnics)
Michael Chan4ed50ef2018-03-09 23:46:03 -05005291{
5292 u32 enables = 0;
5293
5294 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5295 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005296 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5297 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5298 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5299 enables |= tx_rings + ring_grps ?
5300 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5301 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5302 } else {
5303 enables |= cp_rings ?
5304 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5305 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5306 enables |= ring_grps ?
5307 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5308 }
Michael Chan4ed50ef2018-03-09 23:46:03 -05005309 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
Michael Chan41e8d792018-10-14 07:02:48 -04005310 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
Michael Chan4ed50ef2018-03-09 23:46:03 -05005311
Michael Chan41e8d792018-10-14 07:02:48 -04005312 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
Michael Chan4ed50ef2018-03-09 23:46:03 -05005313 req->num_tx_rings = cpu_to_le16(tx_rings);
5314 req->num_rx_rings = cpu_to_le16(rx_rings);
Michael Chan41e8d792018-10-14 07:02:48 -04005315 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5316 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5317 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5318 } else {
5319 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5320 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5321 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
5322 }
Vasundhara Volam780baad2018-12-16 18:46:23 -05005323 req->num_stat_ctxs = cpu_to_le16(stats);
Michael Chan4ed50ef2018-03-09 23:46:03 -05005324 req->num_vnics = cpu_to_le16(vnics);
5325
5326 req->enables = cpu_to_le32(enables);
5327}
5328
5329static int
5330bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005331 int ring_grps, int cp_rings, int stats, int vnics)
Michael Chan4ed50ef2018-03-09 23:46:03 -05005332{
5333 struct hwrm_func_cfg_input req = {0};
5334 int rc;
5335
5336 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005337 cp_rings, stats, vnics);
Michael Chan4ed50ef2018-03-09 23:46:03 -05005338 if (!req.enables)
Michael Chan674f50a2018-01-17 03:21:09 -05005339 return 0;
5340
Michael Chan674f50a2018-01-17 03:21:09 -05005341 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5342 if (rc)
5343 return -ENOMEM;
5344
5345 if (bp->hwrm_spec_code < 0x10601)
5346 bp->hw_resc.resv_tx_rings = tx_rings;
5347
5348 rc = bnxt_hwrm_get_rings(bp);
5349 return rc;
5350}
5351
5352static int
5353bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005354 int ring_grps, int cp_rings, int stats, int vnics)
Michael Chan674f50a2018-01-17 03:21:09 -05005355{
5356 struct hwrm_func_vf_cfg_input req = {0};
Michael Chan674f50a2018-01-17 03:21:09 -05005357 int rc;
5358
Michael Chanf1ca94d2018-08-05 16:51:53 -04005359 if (!BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005360 bp->hw_resc.resv_tx_rings = tx_rings;
5361 return 0;
5362 }
5363
Michael Chan4ed50ef2018-03-09 23:46:03 -05005364 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005365 cp_rings, stats, vnics);
Michael Chan674f50a2018-01-17 03:21:09 -05005366 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5367 if (rc)
5368 return -ENOMEM;
5369
5370 rc = bnxt_hwrm_get_rings(bp);
5371 return rc;
5372}
5373
5374static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005375 int cp, int stat, int vnic)
Michael Chan674f50a2018-01-17 03:21:09 -05005376{
5377 if (BNXT_PF(bp))
Vasundhara Volam780baad2018-12-16 18:46:23 -05005378 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
5379 vnic);
Michael Chan674f50a2018-01-17 03:21:09 -05005380 else
Vasundhara Volam780baad2018-12-16 18:46:23 -05005381 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
5382 vnic);
Michael Chan674f50a2018-01-17 03:21:09 -05005383}
5384
Michael Chanb16b6892018-12-16 18:46:25 -05005385int bnxt_nq_rings_in_use(struct bnxt *bp)
Michael Chan08654eb2018-03-31 13:54:17 -04005386{
5387 int cp = bp->cp_nr_rings;
5388 int ulp_msix, ulp_base;
5389
5390 ulp_msix = bnxt_get_ulp_msix_num(bp);
5391 if (ulp_msix) {
5392 ulp_base = bnxt_get_ulp_msix_base(bp);
5393 cp += ulp_msix;
5394 if ((ulp_base + ulp_msix) > cp)
5395 cp = ulp_base + ulp_msix;
5396 }
5397 return cp;
5398}
5399
Michael Chanc0b8cda2018-12-09 07:01:01 -05005400static int bnxt_cp_rings_in_use(struct bnxt *bp)
5401{
5402 int cp;
5403
5404 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5405 return bnxt_nq_rings_in_use(bp);
5406
5407 cp = bp->tx_nr_rings + bp->rx_nr_rings;
5408 return cp;
5409}
5410
Vasundhara Volam780baad2018-12-16 18:46:23 -05005411static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
5412{
5413 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
5414}
5415
Michael Chan4e41dc52018-03-31 13:54:19 -04005416static bool bnxt_need_reserve_rings(struct bnxt *bp)
5417{
5418 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanfbcfc8e2018-03-31 13:54:20 -04005419 int cp = bnxt_cp_rings_in_use(bp);
Michael Chanc0b8cda2018-12-09 07:01:01 -05005420 int nq = bnxt_nq_rings_in_use(bp);
Vasundhara Volam780baad2018-12-16 18:46:23 -05005421 int rx = bp->rx_nr_rings, stat;
Michael Chan4e41dc52018-03-31 13:54:19 -04005422 int vnic = 1, grp = rx;
5423
5424 if (bp->hwrm_spec_code < 0x10601)
5425 return false;
5426
5427 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
5428 return true;
5429
Michael Chan41e8d792018-10-14 07:02:48 -04005430 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
Michael Chan4e41dc52018-03-31 13:54:19 -04005431 vnic = rx + 1;
5432 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5433 rx <<= 1;
Vasundhara Volam780baad2018-12-16 18:46:23 -05005434 stat = bnxt_get_func_stat_ctxs(bp);
Michael Chanf1ca94d2018-08-05 16:51:53 -04005435 if (BNXT_NEW_RM(bp) &&
Michael Chan4e41dc52018-03-31 13:54:19 -04005436 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
Michael Chanc0b8cda2018-12-09 07:01:01 -05005437 hw_resc->resv_irqs < nq || hw_resc->resv_vnics != vnic ||
Vasundhara Volam780baad2018-12-16 18:46:23 -05005438 hw_resc->resv_stat_ctxs != stat ||
Michael Chan41e8d792018-10-14 07:02:48 -04005439 (hw_resc->resv_hw_ring_grps != grp &&
5440 !(bp->flags & BNXT_FLAG_CHIP_P5))))
Michael Chan4e41dc52018-03-31 13:54:19 -04005441 return true;
5442 return false;
5443}
5444
Michael Chan674f50a2018-01-17 03:21:09 -05005445static int __bnxt_reserve_rings(struct bnxt *bp)
5446{
5447 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chanc0b8cda2018-12-09 07:01:01 -05005448 int cp = bnxt_nq_rings_in_use(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05005449 int tx = bp->tx_nr_rings;
5450 int rx = bp->rx_nr_rings;
Michael Chan674f50a2018-01-17 03:21:09 -05005451 int grp, rx_rings, rc;
Vasundhara Volam780baad2018-12-16 18:46:23 -05005452 int vnic = 1, stat;
Michael Chan674f50a2018-01-17 03:21:09 -05005453 bool sh = false;
Michael Chan674f50a2018-01-17 03:21:09 -05005454
Michael Chan4e41dc52018-03-31 13:54:19 -04005455 if (!bnxt_need_reserve_rings(bp))
Michael Chan391be5c2016-12-29 12:13:41 -05005456 return 0;
5457
Michael Chan674f50a2018-01-17 03:21:09 -05005458 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5459 sh = true;
Michael Chan41e8d792018-10-14 07:02:48 -04005460 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
Michael Chan674f50a2018-01-17 03:21:09 -05005461 vnic = rx + 1;
5462 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5463 rx <<= 1;
Michael Chan674f50a2018-01-17 03:21:09 -05005464 grp = bp->rx_nr_rings;
Vasundhara Volam780baad2018-12-16 18:46:23 -05005465 stat = bnxt_get_func_stat_ctxs(bp);
Michael Chan391be5c2016-12-29 12:13:41 -05005466
Vasundhara Volam780baad2018-12-16 18:46:23 -05005467 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
Michael Chan391be5c2016-12-29 12:13:41 -05005468 if (rc)
5469 return rc;
5470
Michael Chan674f50a2018-01-17 03:21:09 -05005471 tx = hw_resc->resv_tx_rings;
Michael Chanf1ca94d2018-08-05 16:51:53 -04005472 if (BNXT_NEW_RM(bp)) {
Michael Chan674f50a2018-01-17 03:21:09 -05005473 rx = hw_resc->resv_rx_rings;
Michael Chanc0b8cda2018-12-09 07:01:01 -05005474 cp = hw_resc->resv_irqs;
Michael Chan674f50a2018-01-17 03:21:09 -05005475 grp = hw_resc->resv_hw_ring_grps;
5476 vnic = hw_resc->resv_vnics;
Vasundhara Volam780baad2018-12-16 18:46:23 -05005477 stat = hw_resc->resv_stat_ctxs;
Michael Chan674f50a2018-01-17 03:21:09 -05005478 }
5479
5480 rx_rings = rx;
5481 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5482 if (rx >= 2) {
5483 rx_rings = rx >> 1;
5484 } else {
5485 if (netif_running(bp->dev))
5486 return -ENOMEM;
5487
5488 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
5489 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5490 bp->dev->hw_features &= ~NETIF_F_LRO;
5491 bp->dev->features &= ~NETIF_F_LRO;
5492 bnxt_set_ring_params(bp);
5493 }
5494 }
5495 rx_rings = min_t(int, rx_rings, grp);
Vasundhara Volam780baad2018-12-16 18:46:23 -05005496 cp = min_t(int, cp, bp->cp_nr_rings);
5497 if (stat > bnxt_get_ulp_stat_ctxs(bp))
5498 stat -= bnxt_get_ulp_stat_ctxs(bp);
5499 cp = min_t(int, cp, stat);
Michael Chan674f50a2018-01-17 03:21:09 -05005500 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
5501 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5502 rx = rx_rings << 1;
5503 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
5504 bp->tx_nr_rings = tx;
5505 bp->rx_nr_rings = rx_rings;
5506 bp->cp_nr_rings = cp;
5507
Vasundhara Volam780baad2018-12-16 18:46:23 -05005508 if (!tx || !rx || !cp || !grp || !vnic || !stat)
Michael Chan674f50a2018-01-17 03:21:09 -05005509 return -ENOMEM;
5510
Michael Chan391be5c2016-12-29 12:13:41 -05005511 return rc;
5512}
5513
Michael Chan8f23d632018-01-17 03:21:12 -05005514static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005515 int ring_grps, int cp_rings, int stats,
5516 int vnics)
Michael Chan98fdbe72017-08-28 13:40:26 -04005517{
Michael Chan8f23d632018-01-17 03:21:12 -05005518 struct hwrm_func_vf_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005519 u32 flags;
Michael Chan98fdbe72017-08-28 13:40:26 -04005520 int rc;
5521
Michael Chanf1ca94d2018-08-05 16:51:53 -04005522 if (!BNXT_NEW_RM(bp))
Michael Chan98fdbe72017-08-28 13:40:26 -04005523 return 0;
5524
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005525 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005526 cp_rings, stats, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005527 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
5528 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5529 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
Michael Chan8f23d632018-01-17 03:21:12 -05005530 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
Michael Chan41e8d792018-10-14 07:02:48 -04005531 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
5532 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5533 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5534 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
Michael Chan98fdbe72017-08-28 13:40:26 -04005535
Michael Chan8f23d632018-01-17 03:21:12 -05005536 req.flags = cpu_to_le32(flags);
Michael Chan98fdbe72017-08-28 13:40:26 -04005537 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5538 if (rc)
5539 return -ENOMEM;
5540 return 0;
5541}
5542
Michael Chan8f23d632018-01-17 03:21:12 -05005543static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005544 int ring_grps, int cp_rings, int stats,
5545 int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05005546{
5547 struct hwrm_func_cfg_input req = {0};
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005548 u32 flags;
Michael Chan8f23d632018-01-17 03:21:12 -05005549 int rc;
5550
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005551 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005552 cp_rings, stats, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005553 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
Michael Chan41e8d792018-10-14 07:02:48 -04005554 if (BNXT_NEW_RM(bp)) {
Michael Chan8f23d632018-01-17 03:21:12 -05005555 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5556 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
Michael Chan8f23d632018-01-17 03:21:12 -05005557 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5558 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
Michael Chan41e8d792018-10-14 07:02:48 -04005559 if (bp->flags & BNXT_FLAG_CHIP_P5)
5560 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5561 else
5562 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5563 }
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05005564
Michael Chan8f23d632018-01-17 03:21:12 -05005565 req.flags = cpu_to_le32(flags);
Michael Chan8f23d632018-01-17 03:21:12 -05005566 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5567 if (rc)
5568 return -ENOMEM;
5569 return 0;
5570}
5571
5572static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005573 int ring_grps, int cp_rings, int stats,
5574 int vnics)
Michael Chan8f23d632018-01-17 03:21:12 -05005575{
5576 if (bp->hwrm_spec_code < 0x10801)
5577 return 0;
5578
5579 if (BNXT_PF(bp))
5580 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005581 ring_grps, cp_rings, stats,
5582 vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005583
5584 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
Vasundhara Volam780baad2018-12-16 18:46:23 -05005585 cp_rings, stats, vnics);
Michael Chan8f23d632018-01-17 03:21:12 -05005586}
5587
Michael Chan74706af2018-10-14 07:02:40 -04005588static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
5589{
5590 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5591 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5592 struct hwrm_ring_aggint_qcaps_input req = {0};
5593 int rc;
5594
5595 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
5596 coal_cap->num_cmpl_dma_aggr_max = 63;
5597 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
5598 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
5599 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
5600 coal_cap->int_lat_tmr_min_max = 65535;
5601 coal_cap->int_lat_tmr_max_max = 65535;
5602 coal_cap->num_cmpl_aggr_int_max = 65535;
5603 coal_cap->timer_units = 80;
5604
5605 if (bp->hwrm_spec_code < 0x10902)
5606 return;
5607
5608 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
5609 mutex_lock(&bp->hwrm_cmd_lock);
5610 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5611 if (!rc) {
5612 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
Michael Chan58590c82018-10-14 07:02:56 -04005613 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
Michael Chan74706af2018-10-14 07:02:40 -04005614 coal_cap->num_cmpl_dma_aggr_max =
5615 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
5616 coal_cap->num_cmpl_dma_aggr_during_int_max =
5617 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
5618 coal_cap->cmpl_aggr_dma_tmr_max =
5619 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
5620 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
5621 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
5622 coal_cap->int_lat_tmr_min_max =
5623 le16_to_cpu(resp->int_lat_tmr_min_max);
5624 coal_cap->int_lat_tmr_max_max =
5625 le16_to_cpu(resp->int_lat_tmr_max_max);
5626 coal_cap->num_cmpl_aggr_int_max =
5627 le16_to_cpu(resp->num_cmpl_aggr_int_max);
5628 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
5629 }
5630 mutex_unlock(&bp->hwrm_cmd_lock);
5631}
5632
5633static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
5634{
5635 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5636
5637 return usec * 1000 / coal_cap->timer_units;
5638}
5639
5640static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
5641 struct bnxt_coal *hw_coal,
Michael Chanbb053f52016-02-26 04:00:02 -05005642 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
5643{
Michael Chan74706af2018-10-14 07:02:40 -04005644 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5645 u32 cmpl_params = coal_cap->cmpl_params;
5646 u16 val, tmr, max, flags = 0;
Michael Chanf8503962017-10-26 11:51:28 -04005647
5648 max = hw_coal->bufs_per_record * 128;
5649 if (hw_coal->budget)
5650 max = hw_coal->bufs_per_record * hw_coal->budget;
Michael Chan74706af2018-10-14 07:02:40 -04005651 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
Michael Chanf8503962017-10-26 11:51:28 -04005652
5653 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
5654 req->num_cmpl_aggr_int = cpu_to_le16(val);
Michael Chanb153cbc2017-11-03 03:32:39 -04005655
Michael Chan74706af2018-10-14 07:02:40 -04005656 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
Michael Chanf8503962017-10-26 11:51:28 -04005657 req->num_cmpl_dma_aggr = cpu_to_le16(val);
5658
Michael Chan74706af2018-10-14 07:02:40 -04005659 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
5660 coal_cap->num_cmpl_dma_aggr_during_int_max);
Michael Chanf8503962017-10-26 11:51:28 -04005661 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
5662
Michael Chan74706af2018-10-14 07:02:40 -04005663 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
5664 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
Michael Chanf8503962017-10-26 11:51:28 -04005665 req->int_lat_tmr_max = cpu_to_le16(tmr);
5666
5667 /* min timer set to 1/2 of interrupt timer */
Michael Chan74706af2018-10-14 07:02:40 -04005668 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
5669 val = tmr / 2;
5670 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
5671 req->int_lat_tmr_min = cpu_to_le16(val);
5672 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
5673 }
Michael Chanf8503962017-10-26 11:51:28 -04005674
5675 /* buf timer set to 1/4 of interrupt timer */
Michael Chan74706af2018-10-14 07:02:40 -04005676 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
Michael Chanf8503962017-10-26 11:51:28 -04005677 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
5678
Michael Chan74706af2018-10-14 07:02:40 -04005679 if (cmpl_params &
5680 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
5681 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
5682 val = clamp_t(u16, tmr, 1,
5683 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
5684 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
5685 req->enables |=
5686 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
5687 }
Michael Chanf8503962017-10-26 11:51:28 -04005688
Michael Chan74706af2018-10-14 07:02:40 -04005689 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
5690 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
5691 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
5692 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
Michael Chanf8503962017-10-26 11:51:28 -04005693 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
Michael Chanbb053f52016-02-26 04:00:02 -05005694 req->flags = cpu_to_le16(flags);
Michael Chan74706af2018-10-14 07:02:40 -04005695 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
Michael Chanbb053f52016-02-26 04:00:02 -05005696}
5697
Michael Chan58590c82018-10-14 07:02:56 -04005698/* Caller holds bp->hwrm_cmd_lock */
5699static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
5700 struct bnxt_coal *hw_coal)
5701{
5702 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
5703 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5704 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5705 u32 nq_params = coal_cap->nq_params;
5706 u16 tmr;
5707
5708 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
5709 return 0;
5710
5711 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
5712 -1, -1);
5713 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
5714 req.flags =
5715 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
5716
5717 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
5718 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
5719 req.int_lat_tmr_min = cpu_to_le16(tmr);
5720 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
5721 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5722}
5723
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005724int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
5725{
5726 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
5727 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5728 struct bnxt_coal coal;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005729
5730 /* Tick values in micro seconds.
5731 * 1 coal_buf x bufs_per_record = 1 completion record.
5732 */
5733 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
5734
5735 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
5736 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
5737
5738 if (!bnapi->rx_ring)
5739 return -ENODEV;
5740
5741 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5742 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5743
Michael Chan74706af2018-10-14 07:02:40 -04005744 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005745
Michael Chan2c61d212018-10-14 07:02:50 -04005746 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05005747
5748 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
5749 HWRM_CMD_TIMEOUT);
5750}
5751
Michael Chanc0c050c2015-10-22 16:01:17 -04005752int bnxt_hwrm_set_coal(struct bnxt *bp)
5753{
5754 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05005755 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
5756 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04005757
Michael Chandfc9c942016-02-26 04:00:03 -05005758 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5759 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5760 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
5761 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005762
Michael Chan74706af2018-10-14 07:02:40 -04005763 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
5764 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04005765
5766 mutex_lock(&bp->hwrm_cmd_lock);
5767 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05005768 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chan58590c82018-10-14 07:02:56 -04005769 struct bnxt_coal *hw_coal;
Michael Chan2c61d212018-10-14 07:02:50 -04005770 u16 ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005771
Michael Chandfc9c942016-02-26 04:00:03 -05005772 req = &req_rx;
Michael Chan2c61d212018-10-14 07:02:50 -04005773 if (!bnapi->rx_ring) {
5774 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
Michael Chandfc9c942016-02-26 04:00:03 -05005775 req = &req_tx;
Michael Chan2c61d212018-10-14 07:02:50 -04005776 } else {
5777 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
5778 }
5779 req->ring_id = cpu_to_le16(ring_id);
Michael Chandfc9c942016-02-26 04:00:03 -05005780
5781 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04005782 HWRM_CMD_TIMEOUT);
5783 if (rc)
5784 break;
Michael Chan58590c82018-10-14 07:02:56 -04005785
5786 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5787 continue;
5788
5789 if (bnapi->rx_ring && bnapi->tx_ring) {
5790 req = &req_tx;
5791 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
5792 req->ring_id = cpu_to_le16(ring_id);
5793 rc = _hwrm_send_message(bp, req, sizeof(*req),
5794 HWRM_CMD_TIMEOUT);
5795 if (rc)
5796 break;
5797 }
5798 if (bnapi->rx_ring)
5799 hw_coal = &bp->rx_coal;
5800 else
5801 hw_coal = &bp->tx_coal;
5802 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
Michael Chanc0c050c2015-10-22 16:01:17 -04005803 }
5804 mutex_unlock(&bp->hwrm_cmd_lock);
5805 return rc;
5806}
5807
5808static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5809{
5810 int rc = 0, i;
5811 struct hwrm_stat_ctx_free_input req = {0};
5812
5813 if (!bp->bnapi)
5814 return 0;
5815
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005816 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5817 return 0;
5818
Michael Chanc0c050c2015-10-22 16:01:17 -04005819 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5820
5821 mutex_lock(&bp->hwrm_cmd_lock);
5822 for (i = 0; i < bp->cp_nr_rings; i++) {
5823 struct bnxt_napi *bnapi = bp->bnapi[i];
5824 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5825
5826 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5827 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5828
5829 rc = _hwrm_send_message(bp, &req, sizeof(req),
5830 HWRM_CMD_TIMEOUT);
5831 if (rc)
5832 break;
5833
5834 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5835 }
5836 }
5837 mutex_unlock(&bp->hwrm_cmd_lock);
5838 return rc;
5839}
5840
5841static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5842{
5843 int rc = 0, i;
5844 struct hwrm_stat_ctx_alloc_input req = {0};
5845 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5846
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005847 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5848 return 0;
5849
Michael Chanc0c050c2015-10-22 16:01:17 -04005850 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5851
Michael Chan51f30782016-07-01 18:46:29 -04005852 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04005853
5854 mutex_lock(&bp->hwrm_cmd_lock);
5855 for (i = 0; i < bp->cp_nr_rings; i++) {
5856 struct bnxt_napi *bnapi = bp->bnapi[i];
5857 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5858
5859 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5860
5861 rc = _hwrm_send_message(bp, &req, sizeof(req),
5862 HWRM_CMD_TIMEOUT);
5863 if (rc)
5864 break;
5865
5866 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5867
5868 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5869 }
5870 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08005871 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005872}
5873
Michael Chancf6645f2016-06-13 02:25:28 -04005874static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5875{
5876 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005877 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04005878 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04005879 int rc;
5880
5881 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5882 req.fid = cpu_to_le16(0xffff);
5883 mutex_lock(&bp->hwrm_cmd_lock);
5884 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5885 if (rc)
5886 goto func_qcfg_exit;
5887
5888#ifdef CONFIG_BNXT_SRIOV
5889 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04005890 struct bnxt_vf_info *vf = &bp->vf;
5891
5892 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5893 }
5894#endif
Michael Chan9315edc2017-07-24 12:34:25 -04005895 flags = le16_to_cpu(resp->flags);
5896 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5897 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
Michael Chan97381a12018-08-05 16:51:54 -04005898 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
Michael Chan9315edc2017-07-24 12:34:25 -04005899 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
Michael Chan97381a12018-08-05 16:51:54 -04005900 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04005901 }
Michael Chan9315edc2017-07-24 12:34:25 -04005902 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5903 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05005904
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005905 switch (resp->port_partition_type) {
5906 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5907 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5908 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5909 bp->port_partition_type = resp->port_partition_type;
5910 break;
5911 }
Michael Chan32e8239c2017-07-24 12:34:21 -04005912 if (bp->hwrm_spec_code < 0x10707 ||
5913 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5914 bp->br_mode = BRIDGE_MODE_VEB;
5915 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5916 bp->br_mode = BRIDGE_MODE_VEPA;
5917 else
5918 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04005919
Michael Chan7eb9bb32017-10-26 11:51:25 -04005920 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5921 if (!bp->max_mtu)
5922 bp->max_mtu = BNXT_MAX_MTU;
5923
Michael Chancf6645f2016-06-13 02:25:28 -04005924func_qcfg_exit:
5925 mutex_unlock(&bp->hwrm_cmd_lock);
5926 return rc;
5927}
5928
Michael Chan98f04cf2018-10-14 07:02:43 -04005929static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
5930{
5931 struct hwrm_func_backing_store_qcaps_input req = {0};
5932 struct hwrm_func_backing_store_qcaps_output *resp =
5933 bp->hwrm_cmd_resp_addr;
5934 int rc;
5935
5936 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
5937 return 0;
5938
5939 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
5940 mutex_lock(&bp->hwrm_cmd_lock);
5941 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5942 if (!rc) {
5943 struct bnxt_ctx_pg_info *ctx_pg;
5944 struct bnxt_ctx_mem_info *ctx;
5945 int i;
5946
5947 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
5948 if (!ctx) {
5949 rc = -ENOMEM;
5950 goto ctx_err;
5951 }
5952 ctx_pg = kzalloc(sizeof(*ctx_pg) * (bp->max_q + 1), GFP_KERNEL);
5953 if (!ctx_pg) {
5954 kfree(ctx);
5955 rc = -ENOMEM;
5956 goto ctx_err;
5957 }
5958 for (i = 0; i < bp->max_q + 1; i++, ctx_pg++)
5959 ctx->tqm_mem[i] = ctx_pg;
5960
5961 bp->ctx = ctx;
5962 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
5963 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
5964 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
5965 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
5966 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
5967 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
5968 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
5969 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
5970 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
5971 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
5972 ctx->vnic_max_vnic_entries =
5973 le16_to_cpu(resp->vnic_max_vnic_entries);
5974 ctx->vnic_max_ring_table_entries =
5975 le16_to_cpu(resp->vnic_max_ring_table_entries);
5976 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
5977 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
5978 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
5979 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
5980 ctx->tqm_min_entries_per_ring =
5981 le32_to_cpu(resp->tqm_min_entries_per_ring);
5982 ctx->tqm_max_entries_per_ring =
5983 le32_to_cpu(resp->tqm_max_entries_per_ring);
5984 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
5985 if (!ctx->tqm_entries_multiple)
5986 ctx->tqm_entries_multiple = 1;
5987 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
5988 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
5989 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
5990 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
5991 } else {
5992 rc = 0;
5993 }
5994ctx_err:
5995 mutex_unlock(&bp->hwrm_cmd_lock);
5996 return rc;
5997}
5998
Michael Chan1b9394e2018-10-14 07:02:44 -04005999static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6000 __le64 *pg_dir)
6001{
6002 u8 pg_size = 0;
6003
6004 if (BNXT_PAGE_SHIFT == 13)
6005 pg_size = 1 << 4;
6006 else if (BNXT_PAGE_SIZE == 16)
6007 pg_size = 2 << 4;
6008
6009 *pg_attr = pg_size;
6010 if (rmem->nr_pages > 1) {
6011 *pg_attr |= 1;
6012 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6013 } else {
6014 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6015 }
6016}
6017
6018#define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6019 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6020 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6021 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6022 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6023 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6024
6025static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6026{
6027 struct hwrm_func_backing_store_cfg_input req = {0};
6028 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6029 struct bnxt_ctx_pg_info *ctx_pg;
6030 __le32 *num_entries;
6031 __le64 *pg_dir;
6032 u8 *pg_attr;
6033 int i, rc;
6034 u32 ena;
6035
6036 if (!ctx)
6037 return 0;
6038
6039 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6040 req.enables = cpu_to_le32(enables);
6041
6042 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6043 ctx_pg = &ctx->qp_mem;
6044 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6045 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6046 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6047 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6048 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6049 &req.qpc_pg_size_qpc_lvl,
6050 &req.qpc_page_dir);
6051 }
6052 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6053 ctx_pg = &ctx->srq_mem;
6054 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6055 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6056 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6057 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6058 &req.srq_pg_size_srq_lvl,
6059 &req.srq_page_dir);
6060 }
6061 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6062 ctx_pg = &ctx->cq_mem;
6063 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6064 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6065 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6066 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6067 &req.cq_page_dir);
6068 }
6069 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6070 ctx_pg = &ctx->vnic_mem;
6071 req.vnic_num_vnic_entries =
6072 cpu_to_le16(ctx->vnic_max_vnic_entries);
6073 req.vnic_num_ring_table_entries =
6074 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6075 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6076 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6077 &req.vnic_pg_size_vnic_lvl,
6078 &req.vnic_page_dir);
6079 }
6080 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6081 ctx_pg = &ctx->stat_mem;
6082 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6083 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6084 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6085 &req.stat_pg_size_stat_lvl,
6086 &req.stat_page_dir);
6087 }
6088 for (i = 0, num_entries = &req.tqm_sp_num_entries,
6089 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
6090 pg_dir = &req.tqm_sp_page_dir,
6091 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
6092 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
6093 if (!(enables & ena))
6094 continue;
6095
6096 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
6097 ctx_pg = ctx->tqm_mem[i];
6098 *num_entries = cpu_to_le32(ctx_pg->entries);
6099 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
6100 }
6101 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6102 if (rc)
6103 rc = -EIO;
6104 return rc;
6105}
6106
Michael Chan98f04cf2018-10-14 07:02:43 -04006107static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
6108 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size)
6109{
6110 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6111
6112 if (!mem_size)
6113 return 0;
6114
6115 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6116 if (rmem->nr_pages > MAX_CTX_PAGES) {
6117 rmem->nr_pages = 0;
6118 return -EINVAL;
6119 }
6120 rmem->page_size = BNXT_PAGE_SIZE;
6121 rmem->pg_arr = ctx_pg->ctx_pg_arr;
6122 rmem->dma_arr = ctx_pg->ctx_dma_arr;
Michael Chan1b9394e2018-10-14 07:02:44 -04006123 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
Michael Chan98f04cf2018-10-14 07:02:43 -04006124 return bnxt_alloc_ring(bp, rmem);
6125}
6126
6127static void bnxt_free_ctx_mem(struct bnxt *bp)
6128{
6129 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6130 int i;
6131
6132 if (!ctx)
6133 return;
6134
6135 if (ctx->tqm_mem[0]) {
6136 for (i = 0; i < bp->max_q + 1; i++)
6137 bnxt_free_ring(bp, &ctx->tqm_mem[i]->ring_mem);
6138 kfree(ctx->tqm_mem[0]);
6139 ctx->tqm_mem[0] = NULL;
6140 }
6141
6142 bnxt_free_ring(bp, &ctx->stat_mem.ring_mem);
6143 bnxt_free_ring(bp, &ctx->vnic_mem.ring_mem);
6144 bnxt_free_ring(bp, &ctx->cq_mem.ring_mem);
6145 bnxt_free_ring(bp, &ctx->srq_mem.ring_mem);
6146 bnxt_free_ring(bp, &ctx->qp_mem.ring_mem);
6147 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
6148}
6149
6150static int bnxt_alloc_ctx_mem(struct bnxt *bp)
6151{
6152 struct bnxt_ctx_pg_info *ctx_pg;
6153 struct bnxt_ctx_mem_info *ctx;
Michael Chan1b9394e2018-10-14 07:02:44 -04006154 u32 mem_size, ena, entries;
Michael Chan98f04cf2018-10-14 07:02:43 -04006155 int i, rc;
6156
6157 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
6158 if (rc) {
6159 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
6160 rc);
6161 return rc;
6162 }
6163 ctx = bp->ctx;
6164 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
6165 return 0;
6166
6167 ctx_pg = &ctx->qp_mem;
6168 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries;
6169 mem_size = ctx->qp_entry_size * ctx_pg->entries;
6170 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6171 if (rc)
6172 return rc;
6173
6174 ctx_pg = &ctx->srq_mem;
6175 ctx_pg->entries = ctx->srq_max_l2_entries;
6176 mem_size = ctx->srq_entry_size * ctx_pg->entries;
6177 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6178 if (rc)
6179 return rc;
6180
6181 ctx_pg = &ctx->cq_mem;
6182 ctx_pg->entries = ctx->cq_max_l2_entries;
6183 mem_size = ctx->cq_entry_size * ctx_pg->entries;
6184 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6185 if (rc)
6186 return rc;
6187
6188 ctx_pg = &ctx->vnic_mem;
6189 ctx_pg->entries = ctx->vnic_max_vnic_entries +
6190 ctx->vnic_max_ring_table_entries;
6191 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
6192 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6193 if (rc)
6194 return rc;
6195
6196 ctx_pg = &ctx->stat_mem;
6197 ctx_pg->entries = ctx->stat_max_entries;
6198 mem_size = ctx->stat_entry_size * ctx_pg->entries;
6199 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6200 if (rc)
6201 return rc;
6202
6203 entries = ctx->qp_max_l2_entries;
6204 entries = roundup(entries, ctx->tqm_entries_multiple);
6205 entries = clamp_t(u32, entries, ctx->tqm_min_entries_per_ring,
6206 ctx->tqm_max_entries_per_ring);
Michael Chan1b9394e2018-10-14 07:02:44 -04006207 for (i = 0, ena = 0; i < bp->max_q + 1; i++) {
Michael Chan98f04cf2018-10-14 07:02:43 -04006208 ctx_pg = ctx->tqm_mem[i];
6209 ctx_pg->entries = entries;
6210 mem_size = ctx->tqm_entry_size * entries;
6211 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size);
6212 if (rc)
6213 return rc;
Michael Chan1b9394e2018-10-14 07:02:44 -04006214 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
Michael Chan98f04cf2018-10-14 07:02:43 -04006215 }
Michael Chan1b9394e2018-10-14 07:02:44 -04006216 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
6217 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
6218 if (rc)
6219 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
6220 rc);
6221 else
6222 ctx->flags |= BNXT_CTX_FLAG_INITED;
6223
Michael Chan98f04cf2018-10-14 07:02:43 -04006224 return 0;
6225}
6226
Michael Chandb4723b2018-03-31 13:54:13 -04006227int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006228{
6229 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6230 struct hwrm_func_resource_qcaps_input req = {0};
6231 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6232 int rc;
6233
6234 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
6235 req.fid = cpu_to_le16(0xffff);
6236
6237 mutex_lock(&bp->hwrm_cmd_lock);
Jonathan Toppins351cbde2018-12-12 11:58:51 -05006238 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
6239 HWRM_CMD_TIMEOUT);
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006240 if (rc) {
6241 rc = -EIO;
6242 goto hwrm_func_resc_qcaps_exit;
6243 }
6244
Michael Chandb4723b2018-03-31 13:54:13 -04006245 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
6246 if (!all)
6247 goto hwrm_func_resc_qcaps_exit;
6248
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006249 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
6250 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6251 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
6252 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6253 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
6254 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6255 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
6256 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6257 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
6258 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
6259 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
6260 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6261 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
6262 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6263 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
6264 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6265
Michael Chan9c1fabd2018-10-14 07:02:47 -04006266 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6267 u16 max_msix = le16_to_cpu(resp->max_msix);
6268
Michael Chanf7588cd2018-12-16 18:46:19 -05006269 hw_resc->max_nqs = max_msix;
Michael Chan9c1fabd2018-10-14 07:02:47 -04006270 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
6271 }
6272
Michael Chan4673d662018-01-17 03:21:11 -05006273 if (BNXT_PF(bp)) {
6274 struct bnxt_pf_info *pf = &bp->pf;
6275
6276 pf->vf_resv_strategy =
6277 le16_to_cpu(resp->vf_reservation_strategy);
Michael Chanbf827362018-08-05 16:51:50 -04006278 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
Michael Chan4673d662018-01-17 03:21:11 -05006279 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
6280 }
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006281hwrm_func_resc_qcaps_exit:
6282 mutex_unlock(&bp->hwrm_cmd_lock);
6283 return rc;
6284}
6285
6286static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006287{
6288 int rc = 0;
6289 struct hwrm_func_qcaps_input req = {0};
6290 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan6a4f2942018-01-17 03:21:06 -05006291 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6292 u32 flags;
Michael Chanc0c050c2015-10-22 16:01:17 -04006293
6294 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
6295 req.fid = cpu_to_le16(0xffff);
6296
6297 mutex_lock(&bp->hwrm_cmd_lock);
6298 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6299 if (rc)
6300 goto hwrm_func_qcaps_exit;
6301
Michael Chan6a4f2942018-01-17 03:21:06 -05006302 flags = le32_to_cpu(resp->flags);
6303 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05006304 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
Michael Chan6a4f2942018-01-17 03:21:06 -05006305 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
Michael Chane4060d32016-12-07 00:26:19 -05006306 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
6307
Michael Chan7cc5a202016-09-19 03:58:05 -04006308 bp->tx_push_thresh = 0;
Michael Chan6a4f2942018-01-17 03:21:06 -05006309 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
Michael Chan7cc5a202016-09-19 03:58:05 -04006310 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
6311
Michael Chan6a4f2942018-01-17 03:21:06 -05006312 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6313 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6314 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6315 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6316 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
6317 if (!hw_resc->max_hw_ring_grps)
6318 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
6319 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6320 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6321 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6322
Michael Chanc0c050c2015-10-22 16:01:17 -04006323 if (BNXT_PF(bp)) {
6324 struct bnxt_pf_info *pf = &bp->pf;
6325
6326 pf->fw_fid = le16_to_cpu(resp->fid);
6327 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04006328 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04006329 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006330 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
6331 pf->max_vfs = le16_to_cpu(resp->max_vfs);
6332 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
6333 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
6334 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
6335 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
6336 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
6337 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chan6a4f2942018-01-17 03:21:06 -05006338 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
Michael Chanc1ef1462017-04-04 18:14:07 -04006339 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006340 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04006341#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04006342 struct bnxt_vf_info *vf = &bp->vf;
6343
6344 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan7cc5a202016-09-19 03:58:05 -04006345 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan379a80a2015-10-23 15:06:19 -04006346#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006347 }
6348
Michael Chanc0c050c2015-10-22 16:01:17 -04006349hwrm_func_qcaps_exit:
6350 mutex_unlock(&bp->hwrm_cmd_lock);
6351 return rc;
6352}
6353
Michael Chan804fba42018-12-09 07:00:59 -05006354static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
6355
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006356static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
6357{
6358 int rc;
6359
6360 rc = __bnxt_hwrm_func_qcaps(bp);
6361 if (rc)
6362 return rc;
Michael Chan804fba42018-12-09 07:00:59 -05006363 rc = bnxt_hwrm_queue_qportcfg(bp);
6364 if (rc) {
6365 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
6366 return rc;
6367 }
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006368 if (bp->hwrm_spec_code >= 0x10803) {
Michael Chan98f04cf2018-10-14 07:02:43 -04006369 rc = bnxt_alloc_ctx_mem(bp);
6370 if (rc)
6371 return rc;
Michael Chandb4723b2018-03-31 13:54:13 -04006372 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006373 if (!rc)
Michael Chan97381a12018-08-05 16:51:54 -04006374 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
Michael Chanbe0dd9c2018-01-17 03:21:07 -05006375 }
6376 return 0;
6377}
6378
Michael Chanc0c050c2015-10-22 16:01:17 -04006379static int bnxt_hwrm_func_reset(struct bnxt *bp)
6380{
6381 struct hwrm_func_reset_input req = {0};
6382
6383 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
6384 req.enables = 0;
6385
6386 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
6387}
6388
6389static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
6390{
6391 int rc = 0;
6392 struct hwrm_queue_qportcfg_input req = {0};
6393 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chanaabfc012018-08-05 16:51:58 -04006394 u8 i, j, *qptr;
6395 bool no_rdma;
Michael Chanc0c050c2015-10-22 16:01:17 -04006396
6397 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
6398
6399 mutex_lock(&bp->hwrm_cmd_lock);
6400 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6401 if (rc)
6402 goto qportcfg_exit;
6403
6404 if (!resp->max_configurable_queues) {
6405 rc = -EINVAL;
6406 goto qportcfg_exit;
6407 }
6408 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05006409 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04006410 if (bp->max_tc > BNXT_MAX_QUEUE)
6411 bp->max_tc = BNXT_MAX_QUEUE;
6412
Michael Chanaabfc012018-08-05 16:51:58 -04006413 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
6414 qptr = &resp->queue_id0;
6415 for (i = 0, j = 0; i < bp->max_tc; i++) {
Michael Chan98f04cf2018-10-14 07:02:43 -04006416 bp->q_info[j].queue_id = *qptr;
6417 bp->q_ids[i] = *qptr++;
Michael Chanaabfc012018-08-05 16:51:58 -04006418 bp->q_info[j].queue_profile = *qptr++;
6419 bp->tc_to_qidx[j] = j;
6420 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
6421 (no_rdma && BNXT_PF(bp)))
6422 j++;
6423 }
Michael Chan98f04cf2018-10-14 07:02:43 -04006424 bp->max_q = bp->max_tc;
Michael Chanaabfc012018-08-05 16:51:58 -04006425 bp->max_tc = max_t(u8, j, 1);
6426
Michael Chan441cabb2016-09-19 03:58:02 -04006427 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
6428 bp->max_tc = 1;
6429
Michael Chan87c374d2016-12-02 21:17:16 -05006430 if (bp->max_lltc > bp->max_tc)
6431 bp->max_lltc = bp->max_tc;
6432
Michael Chanc0c050c2015-10-22 16:01:17 -04006433qportcfg_exit:
6434 mutex_unlock(&bp->hwrm_cmd_lock);
6435 return rc;
6436}
6437
6438static int bnxt_hwrm_ver_get(struct bnxt *bp)
6439{
6440 int rc;
6441 struct hwrm_ver_get_input req = {0};
6442 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04006443 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04006444
Michael Chane6ef2692016-03-28 19:46:05 -04006445 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04006446 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
6447 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
6448 req.hwrm_intf_min = HWRM_VERSION_MINOR;
6449 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
6450 mutex_lock(&bp->hwrm_cmd_lock);
6451 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6452 if (rc)
6453 goto hwrm_ver_get_exit;
6454
6455 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
6456
Michael Chan894aa692018-01-17 03:21:03 -05006457 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
6458 resp->hwrm_intf_min_8b << 8 |
6459 resp->hwrm_intf_upd_8b;
6460 if (resp->hwrm_intf_maj_8b < 1) {
Michael Chanc1935542015-12-27 18:19:28 -05006461 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chan894aa692018-01-17 03:21:03 -05006462 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
6463 resp->hwrm_intf_upd_8b);
Michael Chanc1935542015-12-27 18:19:28 -05006464 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006465 }
Michael Chan431aa1e2017-10-26 11:51:23 -04006466 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
Michael Chan894aa692018-01-17 03:21:03 -05006467 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
6468 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
Michael Chanc0c050c2015-10-22 16:01:17 -04006469
Michael Chanff4fe812016-02-26 04:00:04 -05006470 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
6471 if (!bp->hwrm_cmd_timeout)
6472 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
6473
Michael Chan1dfddc42018-10-14 07:02:39 -04006474 if (resp->hwrm_intf_maj_8b >= 1) {
Michael Chane6ef2692016-03-28 19:46:05 -04006475 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
Michael Chan1dfddc42018-10-14 07:02:39 -04006476 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
6477 }
6478 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
6479 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
Michael Chane6ef2692016-03-28 19:46:05 -04006480
Michael Chan659c8052016-06-13 02:25:33 -04006481 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006482 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
6483 !resp->chip_metal)
6484 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04006485
Deepak Khungare605db82017-05-29 19:06:04 -04006486 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
6487 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
6488 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
Michael Chan97381a12018-08-05 16:51:54 -04006489 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
Deepak Khungare605db82017-05-29 19:06:04 -04006490
Michael Chanc0c050c2015-10-22 16:01:17 -04006491hwrm_ver_get_exit:
6492 mutex_unlock(&bp->hwrm_cmd_lock);
6493 return rc;
6494}
6495
Rob Swindell5ac67d82016-09-19 03:58:03 -04006496int bnxt_hwrm_fw_set_time(struct bnxt *bp)
6497{
6498 struct hwrm_fw_set_time_input req = {0};
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01006499 struct tm tm;
6500 time64_t now = ktime_get_real_seconds();
Rob Swindell5ac67d82016-09-19 03:58:03 -04006501
Michael Chanca2c39e2018-04-26 17:44:34 -04006502 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
6503 bp->hwrm_spec_code < 0x10400)
Rob Swindell5ac67d82016-09-19 03:58:03 -04006504 return -EOPNOTSUPP;
6505
Arnd Bergmann7dfaa7b2017-11-06 15:04:39 +01006506 time64_to_tm(now, 0, &tm);
Rob Swindell5ac67d82016-09-19 03:58:03 -04006507 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
6508 req.year = cpu_to_le16(1900 + tm.tm_year);
6509 req.month = 1 + tm.tm_mon;
6510 req.day = tm.tm_mday;
6511 req.hour = tm.tm_hour;
6512 req.minute = tm.tm_min;
6513 req.second = tm.tm_sec;
6514 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6515}
6516
Michael Chan3bdf56c2016-03-07 15:38:45 -05006517static int bnxt_hwrm_port_qstats(struct bnxt *bp)
6518{
6519 int rc;
6520 struct bnxt_pf_info *pf = &bp->pf;
6521 struct hwrm_port_qstats_input req = {0};
6522
6523 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
6524 return 0;
6525
6526 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
6527 req.port_id = cpu_to_le16(pf->port_id);
6528 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
6529 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
6530 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6531 return rc;
6532}
6533
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006534static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
6535{
Michael Chan36e53342018-10-14 07:02:38 -04006536 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chane37fed72018-12-16 18:46:26 -05006537 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006538 struct hwrm_port_qstats_ext_input req = {0};
6539 struct bnxt_pf_info *pf = &bp->pf;
Michael Chan36e53342018-10-14 07:02:38 -04006540 int rc;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006541
6542 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
6543 return 0;
6544
6545 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
6546 req.port_id = cpu_to_le16(pf->port_id);
6547 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
6548 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
Michael Chan36e53342018-10-14 07:02:38 -04006549 req.tx_stat_size = cpu_to_le16(sizeof(struct tx_port_stats_ext));
6550 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
6551 mutex_lock(&bp->hwrm_cmd_lock);
6552 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6553 if (!rc) {
6554 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
6555 bp->fw_tx_stats_ext_size = le16_to_cpu(resp->tx_stat_size) / 8;
6556 } else {
6557 bp->fw_rx_stats_ext_size = 0;
6558 bp->fw_tx_stats_ext_size = 0;
6559 }
Michael Chane37fed72018-12-16 18:46:26 -05006560 if (bp->fw_tx_stats_ext_size <=
6561 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
6562 mutex_unlock(&bp->hwrm_cmd_lock);
6563 bp->pri2cos_valid = 0;
6564 return rc;
6565 }
6566
6567 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
6568 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
6569
6570 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
6571 if (!rc) {
6572 struct hwrm_queue_pri2cos_qcfg_output *resp2;
6573 u8 *pri2cos;
6574 int i, j;
6575
6576 resp2 = bp->hwrm_cmd_resp_addr;
6577 pri2cos = &resp2->pri0_cos_queue_id;
6578 for (i = 0; i < 8; i++) {
6579 u8 queue_id = pri2cos[i];
6580
6581 for (j = 0; j < bp->max_q; j++) {
6582 if (bp->q_ids[j] == queue_id)
6583 bp->pri2cos[i] = j;
6584 }
6585 }
6586 bp->pri2cos_valid = 1;
6587 }
Michael Chan36e53342018-10-14 07:02:38 -04006588 mutex_unlock(&bp->hwrm_cmd_lock);
6589 return rc;
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04006590}
6591
Michael Chanc0c050c2015-10-22 16:01:17 -04006592static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
6593{
6594 if (bp->vxlan_port_cnt) {
6595 bnxt_hwrm_tunnel_dst_port_free(
6596 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6597 }
6598 bp->vxlan_port_cnt = 0;
6599 if (bp->nge_port_cnt) {
6600 bnxt_hwrm_tunnel_dst_port_free(
6601 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6602 }
6603 bp->nge_port_cnt = 0;
6604}
6605
6606static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
6607{
6608 int rc, i;
6609 u32 tpa_flags = 0;
6610
6611 if (set_tpa)
6612 tpa_flags = bp->flags & BNXT_FLAG_TPA;
6613 for (i = 0; i < bp->nr_vnics; i++) {
6614 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
6615 if (rc) {
6616 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04006617 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04006618 return rc;
6619 }
6620 }
6621 return 0;
6622}
6623
6624static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
6625{
6626 int i;
6627
6628 for (i = 0; i < bp->nr_vnics; i++)
6629 bnxt_hwrm_vnic_set_rss(bp, i, false);
6630}
6631
6632static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
6633 bool irq_re_init)
6634{
6635 if (bp->vnic_info) {
6636 bnxt_hwrm_clear_vnic_filter(bp);
6637 /* clear all RSS setting before free vnic ctx */
6638 bnxt_hwrm_clear_vnic_rss(bp);
6639 bnxt_hwrm_vnic_ctx_free(bp);
6640 /* before free the vnic, undo the vnic tpa settings */
6641 if (bp->flags & BNXT_FLAG_TPA)
6642 bnxt_set_tpa(bp, false);
6643 bnxt_hwrm_vnic_free(bp);
6644 }
6645 bnxt_hwrm_ring_free(bp, close_path);
6646 bnxt_hwrm_ring_grp_free(bp);
6647 if (irq_re_init) {
6648 bnxt_hwrm_stat_ctx_free(bp);
6649 bnxt_hwrm_free_tunnel_ports(bp);
6650 }
6651}
6652
Michael Chan39d8ba22017-07-24 12:34:22 -04006653static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
6654{
6655 struct hwrm_func_cfg_input req = {0};
6656 int rc;
6657
6658 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6659 req.fid = cpu_to_le16(0xffff);
6660 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
6661 if (br_mode == BRIDGE_MODE_VEB)
6662 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
6663 else if (br_mode == BRIDGE_MODE_VEPA)
6664 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
6665 else
6666 return -EINVAL;
6667 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6668 if (rc)
6669 rc = -EIO;
6670 return rc;
6671}
6672
Michael Chanc3480a62018-01-17 03:21:15 -05006673static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
6674{
6675 struct hwrm_func_cfg_input req = {0};
6676 int rc;
6677
6678 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
6679 return 0;
6680
6681 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6682 req.fid = cpu_to_le16(0xffff);
6683 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
Michael Chand4f52de02018-03-31 13:54:06 -04006684 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
Michael Chanc3480a62018-01-17 03:21:15 -05006685 if (size == 128)
Michael Chand4f52de02018-03-31 13:54:06 -04006686 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
Michael Chanc3480a62018-01-17 03:21:15 -05006687
6688 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6689 if (rc)
6690 rc = -EIO;
6691 return rc;
6692}
6693
Michael Chan7b3af4f2018-10-14 07:02:54 -04006694static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04006695{
Michael Chanae10ae72016-12-29 12:13:38 -05006696 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04006697 int rc;
6698
Michael Chanae10ae72016-12-29 12:13:38 -05006699 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
6700 goto skip_rss_ctx;
6701
Michael Chanc0c050c2015-10-22 16:01:17 -04006702 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04006703 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04006704 if (rc) {
6705 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
6706 vnic_id, rc);
6707 goto vnic_setup_err;
6708 }
6709 bp->rsscos_nr_ctxs++;
6710
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04006711 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6712 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
6713 if (rc) {
6714 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
6715 vnic_id, rc);
6716 goto vnic_setup_err;
6717 }
6718 bp->rsscos_nr_ctxs++;
6719 }
6720
Michael Chanae10ae72016-12-29 12:13:38 -05006721skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04006722 /* configure default vnic, ring grp */
6723 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
6724 if (rc) {
6725 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
6726 vnic_id, rc);
6727 goto vnic_setup_err;
6728 }
6729
6730 /* Enable RSS hashing on vnic */
6731 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
6732 if (rc) {
6733 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
6734 vnic_id, rc);
6735 goto vnic_setup_err;
6736 }
6737
6738 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6739 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
6740 if (rc) {
6741 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
6742 vnic_id, rc);
6743 }
6744 }
6745
6746vnic_setup_err:
6747 return rc;
6748}
6749
Michael Chan7b3af4f2018-10-14 07:02:54 -04006750static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
6751{
6752 int rc, i, nr_ctxs;
6753
6754 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
6755 for (i = 0; i < nr_ctxs; i++) {
6756 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
6757 if (rc) {
6758 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
6759 vnic_id, i, rc);
6760 break;
6761 }
6762 bp->rsscos_nr_ctxs++;
6763 }
6764 if (i < nr_ctxs)
6765 return -ENOMEM;
6766
6767 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
6768 if (rc) {
6769 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
6770 vnic_id, rc);
6771 return rc;
6772 }
6773 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
6774 if (rc) {
6775 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
6776 vnic_id, rc);
6777 return rc;
6778 }
6779 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6780 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
6781 if (rc) {
6782 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
6783 vnic_id, rc);
6784 }
6785 }
6786 return rc;
6787}
6788
6789static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
6790{
6791 if (bp->flags & BNXT_FLAG_CHIP_P5)
6792 return __bnxt_setup_vnic_p5(bp, vnic_id);
6793 else
6794 return __bnxt_setup_vnic(bp, vnic_id);
6795}
6796
Michael Chanc0c050c2015-10-22 16:01:17 -04006797static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
6798{
6799#ifdef CONFIG_RFS_ACCEL
6800 int i, rc = 0;
6801
6802 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05006803 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04006804 u16 vnic_id = i + 1;
6805 u16 ring_id = i;
6806
6807 if (vnic_id >= bp->nr_vnics)
6808 break;
6809
Michael Chanae10ae72016-12-29 12:13:38 -05006810 vnic = &bp->vnic_info[vnic_id];
6811 vnic->flags |= BNXT_VNIC_RFS_FLAG;
6812 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6813 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05006814 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04006815 if (rc) {
6816 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
6817 vnic_id, rc);
6818 break;
6819 }
6820 rc = bnxt_setup_vnic(bp, vnic_id);
6821 if (rc)
6822 break;
6823 }
6824 return rc;
6825#else
6826 return 0;
6827#endif
6828}
6829
Michael Chan17c71ac2016-07-01 18:46:27 -04006830/* Allow PF and VF with default VLAN to be in promiscuous mode */
6831static bool bnxt_promisc_ok(struct bnxt *bp)
6832{
6833#ifdef CONFIG_BNXT_SRIOV
6834 if (BNXT_VF(bp) && !bp->vf.vlan)
6835 return false;
6836#endif
6837 return true;
6838}
6839
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04006840static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
6841{
6842 unsigned int rc = 0;
6843
6844 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
6845 if (rc) {
6846 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
6847 rc);
6848 return rc;
6849 }
6850
6851 rc = bnxt_hwrm_vnic_cfg(bp, 1);
6852 if (rc) {
6853 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
6854 rc);
6855 return rc;
6856 }
6857 return rc;
6858}
6859
Michael Chanb664f002015-12-02 01:54:08 -05006860static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04006861static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05006862
Michael Chanc0c050c2015-10-22 16:01:17 -04006863static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
6864{
Michael Chan7d2837d2016-05-04 16:56:44 -04006865 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04006866 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006867 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006868
6869 if (irq_re_init) {
6870 rc = bnxt_hwrm_stat_ctx_alloc(bp);
6871 if (rc) {
6872 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
6873 rc);
6874 goto err_out;
6875 }
6876 }
6877
6878 rc = bnxt_hwrm_ring_alloc(bp);
6879 if (rc) {
6880 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
6881 goto err_out;
6882 }
6883
6884 rc = bnxt_hwrm_ring_grp_alloc(bp);
6885 if (rc) {
6886 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
6887 goto err_out;
6888 }
6889
Prashant Sreedharan76595192016-07-18 07:15:22 -04006890 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6891 rx_nr_rings--;
6892
Michael Chanc0c050c2015-10-22 16:01:17 -04006893 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04006894 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04006895 if (rc) {
6896 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
6897 goto err_out;
6898 }
6899
6900 rc = bnxt_setup_vnic(bp, 0);
6901 if (rc)
6902 goto err_out;
6903
6904 if (bp->flags & BNXT_FLAG_RFS) {
6905 rc = bnxt_alloc_rfs_vnics(bp);
6906 if (rc)
6907 goto err_out;
6908 }
6909
6910 if (bp->flags & BNXT_FLAG_TPA) {
6911 rc = bnxt_set_tpa(bp, true);
6912 if (rc)
6913 goto err_out;
6914 }
6915
6916 if (BNXT_VF(bp))
6917 bnxt_update_vf_mac(bp);
6918
6919 /* Filter for default vnic 0 */
6920 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
6921 if (rc) {
6922 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
6923 goto err_out;
6924 }
Michael Chan7d2837d2016-05-04 16:56:44 -04006925 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04006926
Michael Chan30e33842018-07-09 02:24:50 -04006927 vnic->rx_mask = 0;
6928 if (bp->dev->flags & IFF_BROADCAST)
6929 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04006930
Michael Chan17c71ac2016-07-01 18:46:27 -04006931 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04006932 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6933
6934 if (bp->dev->flags & IFF_ALLMULTI) {
6935 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6936 vnic->mc_list_count = 0;
6937 } else {
6938 u32 mask = 0;
6939
6940 bnxt_mc_list_updated(bp, &mask);
6941 vnic->rx_mask |= mask;
6942 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006943
Michael Chanb664f002015-12-02 01:54:08 -05006944 rc = bnxt_cfg_rx_mode(bp);
6945 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006946 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04006947
6948 rc = bnxt_hwrm_set_coal(bp);
6949 if (rc)
6950 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04006951 rc);
6952
6953 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6954 rc = bnxt_setup_nitroa0_vnic(bp);
6955 if (rc)
6956 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
6957 rc);
6958 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006959
Michael Chancf6645f2016-06-13 02:25:28 -04006960 if (BNXT_VF(bp)) {
6961 bnxt_hwrm_func_qcfg(bp);
6962 netdev_update_features(bp->dev);
6963 }
6964
Michael Chanc0c050c2015-10-22 16:01:17 -04006965 return 0;
6966
6967err_out:
6968 bnxt_hwrm_resource_free(bp, 0, true);
6969
6970 return rc;
6971}
6972
6973static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
6974{
6975 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
6976 return 0;
6977}
6978
6979static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
6980{
Sankar Patchineelam22479252017-03-28 19:47:29 -04006981 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006982 bnxt_init_rx_rings(bp);
6983 bnxt_init_tx_rings(bp);
6984 bnxt_init_ring_grps(bp, irq_re_init);
6985 bnxt_init_vnics(bp);
6986
6987 return bnxt_init_chip(bp, irq_re_init);
6988}
6989
Michael Chanc0c050c2015-10-22 16:01:17 -04006990static int bnxt_set_real_num_queues(struct bnxt *bp)
6991{
6992 int rc;
6993 struct net_device *dev = bp->dev;
6994
Michael Chan5f449242017-02-06 16:55:40 -05006995 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
6996 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006997 if (rc)
6998 return rc;
6999
7000 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
7001 if (rc)
7002 return rc;
7003
7004#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05007005 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04007006 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04007007#endif
7008
7009 return rc;
7010}
7011
Michael Chan6e6c5a52016-01-02 23:45:02 -05007012static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7013 bool shared)
7014{
7015 int _rx = *rx, _tx = *tx;
7016
7017 if (shared) {
7018 *rx = min_t(int, _rx, max);
7019 *tx = min_t(int, _tx, max);
7020 } else {
7021 if (max < 2)
7022 return -ENOMEM;
7023
7024 while (_rx + _tx > max) {
7025 if (_rx > _tx && _rx > 1)
7026 _rx--;
7027 else if (_tx > 1)
7028 _tx--;
7029 }
7030 *rx = _rx;
7031 *tx = _tx;
7032 }
7033 return 0;
7034}
7035
Michael Chan78095922016-12-07 00:26:16 -05007036static void bnxt_setup_msix(struct bnxt *bp)
7037{
7038 const int len = sizeof(bp->irq_tbl[0].name);
7039 struct net_device *dev = bp->dev;
7040 int tcs, i;
7041
7042 tcs = netdev_get_num_tc(dev);
7043 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05007044 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05007045
Michael Chand1e79252017-02-06 16:55:38 -05007046 for (i = 0; i < tcs; i++) {
7047 count = bp->tx_nr_rings_per_tc;
7048 off = i * count;
7049 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05007050 }
7051 }
7052
7053 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04007054 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
Michael Chan78095922016-12-07 00:26:16 -05007055 char *attr;
7056
7057 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7058 attr = "TxRx";
7059 else if (i < bp->rx_nr_rings)
7060 attr = "rx";
7061 else
7062 attr = "tx";
7063
Michael Chane5811b82018-03-31 13:54:18 -04007064 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
7065 attr, i);
7066 bp->irq_tbl[map_idx].handler = bnxt_msix;
Michael Chan78095922016-12-07 00:26:16 -05007067 }
7068}
7069
7070static void bnxt_setup_inta(struct bnxt *bp)
7071{
7072 const int len = sizeof(bp->irq_tbl[0].name);
7073
7074 if (netdev_get_num_tc(bp->dev))
7075 netdev_reset_tc(bp->dev);
7076
7077 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
7078 0);
7079 bp->irq_tbl[0].handler = bnxt_inta;
7080}
7081
7082static int bnxt_setup_int_mode(struct bnxt *bp)
7083{
7084 int rc;
7085
7086 if (bp->flags & BNXT_FLAG_USING_MSIX)
7087 bnxt_setup_msix(bp);
7088 else
7089 bnxt_setup_inta(bp);
7090
7091 rc = bnxt_set_real_num_queues(bp);
7092 return rc;
7093}
7094
Michael Chanb7429952017-01-13 01:32:00 -05007095#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05007096static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
7097{
Michael Chan6a4f2942018-01-17 03:21:06 -05007098 return bp->hw_resc.max_rsscos_ctxs;
Michael Chan8079e8f2016-12-29 12:13:37 -05007099}
7100
7101static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
7102{
Michael Chan6a4f2942018-01-17 03:21:06 -05007103 return bp->hw_resc.max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05007104}
Michael Chanb7429952017-01-13 01:32:00 -05007105#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05007106
Michael Chane4060d32016-12-07 00:26:19 -05007107unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
7108{
Michael Chan6a4f2942018-01-17 03:21:06 -05007109 return bp->hw_resc.max_stat_ctxs;
Michael Chane4060d32016-12-07 00:26:19 -05007110}
7111
7112unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
7113{
Michael Chan6a4f2942018-01-17 03:21:06 -05007114 return bp->hw_resc.max_cp_rings;
Michael Chane4060d32016-12-07 00:26:19 -05007115}
7116
Michael Chane916b082018-12-16 18:46:20 -05007117static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
Michael Chana588e452016-12-07 00:26:21 -05007118{
Michael Chanc0b8cda2018-12-09 07:01:01 -05007119 unsigned int cp = bp->hw_resc.max_cp_rings;
7120
7121 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7122 cp -= bnxt_get_ulp_msix_num(bp);
7123
7124 return cp;
Michael Chana588e452016-12-07 00:26:21 -05007125}
7126
Michael Chanad95c272018-09-03 04:23:18 -04007127static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
Michael Chan78095922016-12-07 00:26:16 -05007128{
Michael Chan6a4f2942018-01-17 03:21:06 -05007129 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7130
Michael Chanf7588cd2018-12-16 18:46:19 -05007131 if (bp->flags & BNXT_FLAG_CHIP_P5)
7132 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
7133
Michael Chan6a4f2942018-01-17 03:21:06 -05007134 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05007135}
7136
Michael Chan30f52942018-07-09 02:24:51 -04007137static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
Michael Chan33c26572016-12-07 00:26:15 -05007138{
Michael Chan6a4f2942018-01-17 03:21:06 -05007139 bp->hw_resc.max_irqs = max_irqs;
Michael Chan33c26572016-12-07 00:26:15 -05007140}
7141
Michael Chane916b082018-12-16 18:46:20 -05007142unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
7143{
7144 unsigned int cp;
7145
7146 cp = bnxt_get_max_func_cp_rings_for_en(bp);
7147 if (bp->flags & BNXT_FLAG_CHIP_P5)
7148 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
7149 else
7150 return cp - bp->cp_nr_rings;
7151}
7152
Vasundhara Volamc027c6b2018-12-16 18:46:21 -05007153unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
7154{
7155 unsigned int stat;
7156
7157 stat = bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_ulp_stat_ctxs(bp);
7158 stat -= bp->cp_nr_rings;
7159 return stat;
7160}
7161
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007162int bnxt_get_avail_msix(struct bnxt *bp, int num)
7163{
7164 int max_cp = bnxt_get_max_func_cp_rings(bp);
7165 int max_irq = bnxt_get_max_func_irqs(bp);
7166 int total_req = bp->cp_nr_rings + num;
7167 int max_idx, avail_msix;
7168
Michael Chan75720e62018-12-09 07:01:00 -05007169 max_idx = bp->total_irqs;
7170 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7171 max_idx = min_t(int, bp->total_irqs, max_cp);
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007172 avail_msix = max_idx - bp->cp_nr_rings;
Michael Chanf1ca94d2018-08-05 16:51:53 -04007173 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007174 return avail_msix;
7175
7176 if (max_irq < total_req) {
7177 num = max_irq - bp->cp_nr_rings;
7178 if (num <= 0)
7179 return 0;
7180 }
7181 return num;
7182}
7183
Michael Chan08654eb2018-03-31 13:54:17 -04007184static int bnxt_get_num_msix(struct bnxt *bp)
7185{
Michael Chanf1ca94d2018-08-05 16:51:53 -04007186 if (!BNXT_NEW_RM(bp))
Michael Chan08654eb2018-03-31 13:54:17 -04007187 return bnxt_get_max_func_irqs(bp);
7188
Michael Chanc0b8cda2018-12-09 07:01:01 -05007189 return bnxt_nq_rings_in_use(bp);
Michael Chan08654eb2018-03-31 13:54:17 -04007190}
7191
Michael Chan78095922016-12-07 00:26:16 -05007192static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007193{
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007194 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
Michael Chan78095922016-12-07 00:26:16 -05007195 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04007196
Michael Chan08654eb2018-03-31 13:54:17 -04007197 total_vecs = bnxt_get_num_msix(bp);
7198 max = bnxt_get_max_func_irqs(bp);
7199 if (total_vecs > max)
7200 total_vecs = max;
7201
Michael Chan2773dfb2018-04-26 17:44:42 -04007202 if (!total_vecs)
7203 return 0;
7204
Michael Chanc0c050c2015-10-22 16:01:17 -04007205 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
7206 if (!msix_ent)
7207 return -ENOMEM;
7208
7209 for (i = 0; i < total_vecs; i++) {
7210 msix_ent[i].entry = i;
7211 msix_ent[i].vector = 0;
7212 }
7213
Michael Chan01657bc2016-01-02 23:45:03 -05007214 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
7215 min = 2;
7216
7217 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007218 ulp_msix = bnxt_get_ulp_msix_num(bp);
7219 if (total_vecs < 0 || total_vecs < ulp_msix) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007220 rc = -ENODEV;
7221 goto msix_setup_exit;
7222 }
7223
7224 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
7225 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05007226 for (i = 0; i < total_vecs; i++)
7227 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04007228
Michael Chan78095922016-12-07 00:26:16 -05007229 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007230 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05007231 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007232 total_vecs - ulp_msix, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007233 if (rc)
7234 goto msix_setup_exit;
7235
Michael Chan78095922016-12-07 00:26:16 -05007236 bp->cp_nr_rings = (min == 1) ?
7237 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7238 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007239
Michael Chanc0c050c2015-10-22 16:01:17 -04007240 } else {
7241 rc = -ENOMEM;
7242 goto msix_setup_exit;
7243 }
7244 bp->flags |= BNXT_FLAG_USING_MSIX;
7245 kfree(msix_ent);
7246 return 0;
7247
7248msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05007249 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
7250 kfree(bp->irq_tbl);
7251 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04007252 pci_disable_msix(bp->pdev);
7253 kfree(msix_ent);
7254 return rc;
7255}
7256
Michael Chan78095922016-12-07 00:26:16 -05007257static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007258{
Michael Chanc0c050c2015-10-22 16:01:17 -04007259 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05007260 if (!bp->irq_tbl)
7261 return -ENOMEM;
7262
7263 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04007264 bp->rx_nr_rings = 1;
7265 bp->tx_nr_rings = 1;
7266 bp->cp_nr_rings = 1;
Michael Chan01657bc2016-01-02 23:45:03 -05007267 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04007268 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05007269 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007270}
7271
Michael Chan78095922016-12-07 00:26:16 -05007272static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007273{
7274 int rc = 0;
7275
7276 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05007277 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007278
Michael Chan1fa72e22016-04-25 02:30:49 -04007279 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007280 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05007281 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007282 }
7283 return rc;
7284}
7285
Michael Chan78095922016-12-07 00:26:16 -05007286static void bnxt_clear_int_mode(struct bnxt *bp)
7287{
7288 if (bp->flags & BNXT_FLAG_USING_MSIX)
7289 pci_disable_msix(bp->pdev);
7290
7291 kfree(bp->irq_tbl);
7292 bp->irq_tbl = NULL;
7293 bp->flags &= ~BNXT_FLAG_USING_MSIX;
7294}
7295
Michael Chanfbcfc8e2018-03-31 13:54:20 -04007296int bnxt_reserve_rings(struct bnxt *bp)
Michael Chan674f50a2018-01-17 03:21:09 -05007297{
Michael Chan674f50a2018-01-17 03:21:09 -05007298 int tcs = netdev_get_num_tc(bp->dev);
Michael Chan36d65be2018-12-16 18:46:24 -05007299 bool reinit_irq = false;
Michael Chan674f50a2018-01-17 03:21:09 -05007300 int rc;
7301
7302 if (!bnxt_need_reserve_rings(bp))
7303 return 0;
7304
Michael Chanf1ca94d2018-08-05 16:51:53 -04007305 if (BNXT_NEW_RM(bp) && (bnxt_get_num_msix(bp) != bp->total_irqs)) {
Michael Chanec86f142018-03-31 13:54:21 -04007306 bnxt_ulp_irq_stop(bp);
Michael Chan674f50a2018-01-17 03:21:09 -05007307 bnxt_clear_int_mode(bp);
Michael Chan36d65be2018-12-16 18:46:24 -05007308 reinit_irq = true;
7309 }
7310 rc = __bnxt_reserve_rings(bp);
7311 if (reinit_irq) {
7312 if (!rc)
7313 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -04007314 bnxt_ulp_irq_restart(bp, rc);
Michael Chan36d65be2018-12-16 18:46:24 -05007315 }
7316 if (rc) {
7317 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
7318 return rc;
Michael Chan674f50a2018-01-17 03:21:09 -05007319 }
7320 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
7321 netdev_err(bp->dev, "tx ring reservation failure\n");
7322 netdev_reset_tc(bp->dev);
7323 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
7324 return -ENOMEM;
7325 }
Michael Chan674f50a2018-01-17 03:21:09 -05007326 return 0;
7327}
7328
Michael Chanc0c050c2015-10-22 16:01:17 -04007329static void bnxt_free_irq(struct bnxt *bp)
7330{
7331 struct bnxt_irq *irq;
7332 int i;
7333
7334#ifdef CONFIG_RFS_ACCEL
7335 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
7336 bp->dev->rx_cpu_rmap = NULL;
7337#endif
Michael Chancb985262018-04-11 11:50:18 -04007338 if (!bp->irq_tbl || !bp->bnapi)
Michael Chanc0c050c2015-10-22 16:01:17 -04007339 return;
7340
7341 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04007342 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7343
7344 irq = &bp->irq_tbl[map_idx];
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04007345 if (irq->requested) {
7346 if (irq->have_cpumask) {
7347 irq_set_affinity_hint(irq->vector, NULL);
7348 free_cpumask_var(irq->cpu_mask);
7349 irq->have_cpumask = 0;
7350 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007351 free_irq(irq->vector, bp->bnapi[i]);
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04007352 }
7353
Michael Chanc0c050c2015-10-22 16:01:17 -04007354 irq->requested = 0;
7355 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007356}
7357
7358static int bnxt_request_irq(struct bnxt *bp)
7359{
Michael Chanb81a90d2016-01-02 23:45:01 -05007360 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007361 unsigned long flags = 0;
7362#ifdef CONFIG_RFS_ACCEL
Michael Chane5811b82018-03-31 13:54:18 -04007363 struct cpu_rmap *rmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04007364#endif
7365
Michael Chane5811b82018-03-31 13:54:18 -04007366 rc = bnxt_setup_int_mode(bp);
7367 if (rc) {
7368 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
7369 rc);
7370 return rc;
7371 }
7372#ifdef CONFIG_RFS_ACCEL
7373 rmap = bp->dev->rx_cpu_rmap;
7374#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04007375 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
7376 flags = IRQF_SHARED;
7377
Michael Chanb81a90d2016-01-02 23:45:01 -05007378 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chane5811b82018-03-31 13:54:18 -04007379 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7380 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
7381
Michael Chanc0c050c2015-10-22 16:01:17 -04007382#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05007383 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007384 rc = irq_cpu_rmap_add(rmap, irq->vector);
7385 if (rc)
7386 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05007387 j);
7388 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04007389 }
7390#endif
7391 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
7392 bp->bnapi[i]);
7393 if (rc)
7394 break;
7395
7396 irq->requested = 1;
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04007397
7398 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
7399 int numa_node = dev_to_node(&bp->pdev->dev);
7400
7401 irq->have_cpumask = 1;
7402 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
7403 irq->cpu_mask);
7404 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
7405 if (rc) {
7406 netdev_warn(bp->dev,
7407 "Set affinity failed, IRQ = %d\n",
7408 irq->vector);
7409 break;
7410 }
7411 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007412 }
7413 return rc;
7414}
7415
7416static void bnxt_del_napi(struct bnxt *bp)
7417{
7418 int i;
7419
7420 if (!bp->bnapi)
7421 return;
7422
7423 for (i = 0; i < bp->cp_nr_rings; i++) {
7424 struct bnxt_napi *bnapi = bp->bnapi[i];
7425
7426 napi_hash_del(&bnapi->napi);
7427 netif_napi_del(&bnapi->napi);
7428 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08007429 /* We called napi_hash_del() before netif_napi_del(), we need
7430 * to respect an RCU grace period before freeing napi structures.
7431 */
7432 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04007433}
7434
7435static void bnxt_init_napi(struct bnxt *bp)
7436{
7437 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007438 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007439 struct bnxt_napi *bnapi;
7440
7441 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Michael Chan0fcec982018-10-14 07:02:58 -04007442 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
7443
7444 if (bp->flags & BNXT_FLAG_CHIP_P5)
7445 poll_fn = bnxt_poll_p5;
7446 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007447 cp_nr_rings--;
7448 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007449 bnapi = bp->bnapi[i];
Michael Chan0fcec982018-10-14 07:02:58 -04007450 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04007451 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007452 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7453 bnapi = bp->bnapi[cp_nr_rings];
7454 netif_napi_add(bp->dev, &bnapi->napi,
7455 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04007456 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007457 } else {
7458 bnapi = bp->bnapi[0];
7459 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04007460 }
7461}
7462
7463static void bnxt_disable_napi(struct bnxt *bp)
7464{
7465 int i;
7466
7467 if (!bp->bnapi)
7468 return;
7469
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05007470 for (i = 0; i < bp->cp_nr_rings; i++) {
7471 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
7472
7473 if (bp->bnapi[i]->rx_ring)
7474 cancel_work_sync(&cpr->dim.work);
7475
Michael Chanc0c050c2015-10-22 16:01:17 -04007476 napi_disable(&bp->bnapi[i]->napi);
Andy Gospodarek0bc0b972018-01-26 10:27:47 -05007477 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007478}
7479
7480static void bnxt_enable_napi(struct bnxt *bp)
7481{
7482 int i;
7483
7484 for (i = 0; i < bp->cp_nr_rings; i++) {
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05007485 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
Michael Chanfa7e2812016-05-10 19:18:00 -04007486 bp->bnapi[i]->in_reset = false;
Andy Gospodarek6a8788f2018-01-09 16:06:20 -05007487
7488 if (bp->bnapi[i]->rx_ring) {
7489 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
7490 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
7491 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007492 napi_enable(&bp->bnapi[i]->napi);
7493 }
7494}
7495
Michael Chan7df4ae92016-12-02 21:17:17 -05007496void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007497{
7498 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04007499 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04007500
Michael Chanb6ab4b02016-01-02 23:44:59 -05007501 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007502 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05007503 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007504 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04007505 }
7506 }
7507 /* Stop all TX queues */
7508 netif_tx_disable(bp->dev);
7509 netif_carrier_off(bp->dev);
7510}
7511
Michael Chan7df4ae92016-12-02 21:17:17 -05007512void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007513{
7514 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04007515 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04007516
7517 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05007518 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04007519 txr->dev_state = 0;
7520 }
7521 netif_tx_wake_all_queues(bp->dev);
7522 if (bp->link_info.link_up)
7523 netif_carrier_on(bp->dev);
7524}
7525
7526static void bnxt_report_link(struct bnxt *bp)
7527{
7528 if (bp->link_info.link_up) {
7529 const char *duplex;
7530 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04007531 u32 speed;
7532 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04007533
7534 netif_carrier_on(bp->dev);
7535 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
7536 duplex = "full";
7537 else
7538 duplex = "half";
7539 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
7540 flow_ctrl = "ON - receive & transmit";
7541 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
7542 flow_ctrl = "ON - transmit";
7543 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
7544 flow_ctrl = "ON - receive";
7545 else
7546 flow_ctrl = "none";
7547 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04007548 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007549 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04007550 if (bp->flags & BNXT_FLAG_EEE_CAP)
7551 netdev_info(bp->dev, "EEE is %s\n",
7552 bp->eee.eee_active ? "active" :
7553 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05007554 fec = bp->link_info.fec_cfg;
7555 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
7556 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
7557 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
7558 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
7559 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04007560 } else {
7561 netif_carrier_off(bp->dev);
7562 netdev_err(bp->dev, "NIC Link is Down\n");
7563 }
7564}
7565
Michael Chan170ce012016-04-05 14:08:57 -04007566static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
7567{
7568 int rc = 0;
7569 struct hwrm_port_phy_qcaps_input req = {0};
7570 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04007571 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04007572
7573 if (bp->hwrm_spec_code < 0x10201)
7574 return 0;
7575
7576 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
7577
7578 mutex_lock(&bp->hwrm_cmd_lock);
7579 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7580 if (rc)
7581 goto hwrm_phy_qcaps_exit;
7582
Michael Chanacb20052017-07-24 12:34:20 -04007583 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04007584 struct ethtool_eee *eee = &bp->eee;
7585 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
7586
7587 bp->flags |= BNXT_FLAG_EEE_CAP;
7588 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7589 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
7590 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
7591 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
7592 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
7593 }
Michael Chan55fd0cf2018-08-05 16:51:48 -04007594 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
7595 if (bp->test_info)
7596 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
7597 }
Michael Chan520ad892017-03-08 18:44:35 -05007598 if (resp->supported_speeds_auto_mode)
7599 link_info->support_auto_speeds =
7600 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04007601
Michael Chand5430d32017-08-28 13:40:31 -04007602 bp->port_count = resp->port_cnt;
7603
Michael Chan170ce012016-04-05 14:08:57 -04007604hwrm_phy_qcaps_exit:
7605 mutex_unlock(&bp->hwrm_cmd_lock);
7606 return rc;
7607}
7608
Michael Chanc0c050c2015-10-22 16:01:17 -04007609static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
7610{
7611 int rc = 0;
7612 struct bnxt_link_info *link_info = &bp->link_info;
7613 struct hwrm_port_phy_qcfg_input req = {0};
7614 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7615 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05007616 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04007617
7618 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
7619
7620 mutex_lock(&bp->hwrm_cmd_lock);
7621 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7622 if (rc) {
7623 mutex_unlock(&bp->hwrm_cmd_lock);
7624 return rc;
7625 }
7626
7627 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
7628 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04007629 link_info->duplex = resp->duplex_cfg;
7630 if (bp->hwrm_spec_code >= 0x10800)
7631 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04007632 link_info->pause = resp->pause;
7633 link_info->auto_mode = resp->auto_mode;
7634 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05007635 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04007636 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04007637 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04007638 if (link_info->phy_link_status == BNXT_LINK_LINK)
7639 link_info->link_speed = le16_to_cpu(resp->link_speed);
7640 else
7641 link_info->link_speed = 0;
7642 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04007643 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
7644 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05007645 link_info->lp_auto_link_speeds =
7646 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04007647 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
7648 link_info->phy_ver[0] = resp->phy_maj;
7649 link_info->phy_ver[1] = resp->phy_min;
7650 link_info->phy_ver[2] = resp->phy_bld;
7651 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04007652 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04007653 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04007654 link_info->phy_addr = resp->eee_config_phy_addr &
7655 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04007656 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04007657
Michael Chan170ce012016-04-05 14:08:57 -04007658 if (bp->flags & BNXT_FLAG_EEE_CAP) {
7659 struct ethtool_eee *eee = &bp->eee;
7660 u16 fw_speeds;
7661
7662 eee->eee_active = 0;
7663 if (resp->eee_config_phy_addr &
7664 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
7665 eee->eee_active = 1;
7666 fw_speeds = le16_to_cpu(
7667 resp->link_partner_adv_eee_link_speed_mask);
7668 eee->lp_advertised =
7669 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7670 }
7671
7672 /* Pull initial EEE config */
7673 if (!chng_link_state) {
7674 if (resp->eee_config_phy_addr &
7675 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
7676 eee->eee_enabled = 1;
7677
7678 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
7679 eee->advertised =
7680 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7681
7682 if (resp->eee_config_phy_addr &
7683 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
7684 __le32 tmr;
7685
7686 eee->tx_lpi_enabled = 1;
7687 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
7688 eee->tx_lpi_timer = le32_to_cpu(tmr) &
7689 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
7690 }
7691 }
7692 }
Michael Chane70c7522017-02-12 19:18:16 -05007693
7694 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
7695 if (bp->hwrm_spec_code >= 0x10504)
7696 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
7697
Michael Chanc0c050c2015-10-22 16:01:17 -04007698 /* TODO: need to add more logic to report VF link */
7699 if (chng_link_state) {
7700 if (link_info->phy_link_status == BNXT_LINK_LINK)
7701 link_info->link_up = 1;
7702 else
7703 link_info->link_up = 0;
7704 if (link_up != link_info->link_up)
7705 bnxt_report_link(bp);
7706 } else {
7707 /* alwasy link down if not require to update link state */
7708 link_info->link_up = 0;
7709 }
7710 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05007711
Michael Chandac049072018-05-08 03:18:39 -04007712 if (!BNXT_SINGLE_PF(bp))
7713 return 0;
7714
Michael Chan286ef9d2016-11-16 21:13:08 -05007715 diff = link_info->support_auto_speeds ^ link_info->advertising;
7716 if ((link_info->support_auto_speeds | diff) !=
7717 link_info->support_auto_speeds) {
7718 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05007719 * update the advertisement settings. Caller holds RTNL
7720 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05007721 */
Michael Chan286ef9d2016-11-16 21:13:08 -05007722 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05007723 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05007724 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05007725 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007726 return 0;
7727}
7728
Michael Chan10289be2016-05-15 03:04:49 -04007729static void bnxt_get_port_module_status(struct bnxt *bp)
7730{
7731 struct bnxt_link_info *link_info = &bp->link_info;
7732 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
7733 u8 module_status;
7734
7735 if (bnxt_update_link(bp, true))
7736 return;
7737
7738 module_status = link_info->module_status;
7739 switch (module_status) {
7740 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
7741 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
7742 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
7743 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
7744 bp->pf.port_id);
7745 if (bp->hwrm_spec_code >= 0x10201) {
7746 netdev_warn(bp->dev, "Module part number %s\n",
7747 resp->phy_vendor_partnumber);
7748 }
7749 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
7750 netdev_warn(bp->dev, "TX is disabled\n");
7751 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
7752 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
7753 }
7754}
7755
Michael Chanc0c050c2015-10-22 16:01:17 -04007756static void
7757bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
7758{
7759 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007760 if (bp->hwrm_spec_code >= 0x10201)
7761 req->auto_pause =
7762 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04007763 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7764 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
7765 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04007766 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04007767 req->enables |=
7768 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7769 } else {
7770 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7771 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
7772 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
7773 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
7774 req->enables |=
7775 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04007776 if (bp->hwrm_spec_code >= 0x10201) {
7777 req->auto_pause = req->force_pause;
7778 req->enables |= cpu_to_le32(
7779 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7780 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007781 }
7782}
7783
7784static void bnxt_hwrm_set_link_common(struct bnxt *bp,
7785 struct hwrm_port_phy_cfg_input *req)
7786{
7787 u8 autoneg = bp->link_info.autoneg;
7788 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05007789 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04007790
7791 if (autoneg & BNXT_AUTONEG_SPEED) {
7792 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04007793 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04007794
7795 req->enables |= cpu_to_le32(
7796 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
7797 req->auto_link_speed_mask = cpu_to_le16(advertising);
7798
7799 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
7800 req->flags |=
7801 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
7802 } else {
7803 req->force_link_speed = cpu_to_le16(fw_link_speed);
7804 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
7805 }
7806
Michael Chanc0c050c2015-10-22 16:01:17 -04007807 /* tell chimp that the setting takes effect immediately */
7808 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
7809}
7810
7811int bnxt_hwrm_set_pause(struct bnxt *bp)
7812{
7813 struct hwrm_port_phy_cfg_input req = {0};
7814 int rc;
7815
7816 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
7817 bnxt_hwrm_set_pause_common(bp, &req);
7818
7819 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
7820 bp->link_info.force_link_chng)
7821 bnxt_hwrm_set_link_common(bp, &req);
7822
7823 mutex_lock(&bp->hwrm_cmd_lock);
7824 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7825 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
7826 /* since changing of pause setting doesn't trigger any link
7827 * change event, the driver needs to update the current pause
7828 * result upon successfully return of the phy_cfg command
7829 */
7830 bp->link_info.pause =
7831 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
7832 bp->link_info.auto_pause_setting = 0;
7833 if (!bp->link_info.force_link_chng)
7834 bnxt_report_link(bp);
7835 }
7836 bp->link_info.force_link_chng = false;
7837 mutex_unlock(&bp->hwrm_cmd_lock);
7838 return rc;
7839}
7840
Michael Chan939f7f02016-04-05 14:08:58 -04007841static void bnxt_hwrm_set_eee(struct bnxt *bp,
7842 struct hwrm_port_phy_cfg_input *req)
7843{
7844 struct ethtool_eee *eee = &bp->eee;
7845
7846 if (eee->eee_enabled) {
7847 u16 eee_speeds;
7848 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
7849
7850 if (eee->tx_lpi_enabled)
7851 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
7852 else
7853 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
7854
7855 req->flags |= cpu_to_le32(flags);
7856 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
7857 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
7858 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
7859 } else {
7860 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
7861 }
7862}
7863
7864int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04007865{
7866 struct hwrm_port_phy_cfg_input req = {0};
7867
7868 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
7869 if (set_pause)
7870 bnxt_hwrm_set_pause_common(bp, &req);
7871
7872 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04007873
7874 if (set_eee)
7875 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04007876 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7877}
7878
Michael Chan33f7d552016-04-11 04:11:12 -04007879static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
7880{
7881 struct hwrm_port_phy_cfg_input req = {0};
7882
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007883 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04007884 return 0;
7885
7886 if (pci_num_vf(bp->pdev))
7887 return 0;
7888
7889 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05007890 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04007891 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7892}
7893
Michael Chan25e1acd2018-08-05 16:51:55 -04007894static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
7895{
7896 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
7897 struct hwrm_func_drv_if_change_input req = {0};
7898 bool resc_reinit = false;
7899 int rc;
7900
7901 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
7902 return 0;
7903
7904 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
7905 if (up)
7906 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
7907 mutex_lock(&bp->hwrm_cmd_lock);
7908 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7909 if (!rc && (resp->flags &
7910 cpu_to_le32(FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)))
7911 resc_reinit = true;
7912 mutex_unlock(&bp->hwrm_cmd_lock);
7913
7914 if (up && resc_reinit && BNXT_NEW_RM(bp)) {
7915 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7916
7917 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7918 hw_resc->resv_cp_rings = 0;
Vasundhara Volam780baad2018-12-16 18:46:23 -05007919 hw_resc->resv_stat_ctxs = 0;
Michael Chan75720e62018-12-09 07:01:00 -05007920 hw_resc->resv_irqs = 0;
Michael Chan25e1acd2018-08-05 16:51:55 -04007921 hw_resc->resv_tx_rings = 0;
7922 hw_resc->resv_rx_rings = 0;
7923 hw_resc->resv_hw_ring_grps = 0;
7924 hw_resc->resv_vnics = 0;
Michael Chan6b95c3e2018-09-03 04:23:17 -04007925 bp->tx_nr_rings = 0;
7926 bp->rx_nr_rings = 0;
Michael Chan25e1acd2018-08-05 16:51:55 -04007927 }
7928 return rc;
7929}
7930
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007931static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
7932{
7933 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7934 struct hwrm_port_led_qcaps_input req = {0};
7935 struct bnxt_pf_info *pf = &bp->pf;
7936 int rc;
7937
7938 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
7939 return 0;
7940
7941 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
7942 req.port_id = cpu_to_le16(pf->port_id);
7943 mutex_lock(&bp->hwrm_cmd_lock);
7944 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7945 if (rc) {
7946 mutex_unlock(&bp->hwrm_cmd_lock);
7947 return rc;
7948 }
7949 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
7950 int i;
7951
7952 bp->num_leds = resp->num_leds;
7953 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
7954 bp->num_leds);
7955 for (i = 0; i < bp->num_leds; i++) {
7956 struct bnxt_led_info *led = &bp->leds[i];
7957 __le16 caps = led->led_state_caps;
7958
7959 if (!led->led_group_id ||
7960 !BNXT_LED_ALT_BLINK_CAP(caps)) {
7961 bp->num_leds = 0;
7962 break;
7963 }
7964 }
7965 }
7966 mutex_unlock(&bp->hwrm_cmd_lock);
7967 return 0;
7968}
7969
Michael Chan5282db62017-04-04 18:14:10 -04007970int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
7971{
7972 struct hwrm_wol_filter_alloc_input req = {0};
7973 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
7974 int rc;
7975
7976 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
7977 req.port_id = cpu_to_le16(bp->pf.port_id);
7978 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
7979 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
7980 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
7981 mutex_lock(&bp->hwrm_cmd_lock);
7982 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7983 if (!rc)
7984 bp->wol_filter_id = resp->wol_filter_id;
7985 mutex_unlock(&bp->hwrm_cmd_lock);
7986 return rc;
7987}
7988
7989int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
7990{
7991 struct hwrm_wol_filter_free_input req = {0};
7992 int rc;
7993
7994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
7995 req.port_id = cpu_to_le16(bp->pf.port_id);
7996 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
7997 req.wol_filter_id = bp->wol_filter_id;
7998 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7999 return rc;
8000}
8001
Michael Chanc1ef1462017-04-04 18:14:07 -04008002static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
8003{
8004 struct hwrm_wol_filter_qcfg_input req = {0};
8005 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8006 u16 next_handle = 0;
8007 int rc;
8008
8009 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
8010 req.port_id = cpu_to_le16(bp->pf.port_id);
8011 req.handle = cpu_to_le16(handle);
8012 mutex_lock(&bp->hwrm_cmd_lock);
8013 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8014 if (!rc) {
8015 next_handle = le16_to_cpu(resp->next_handle);
8016 if (next_handle != 0) {
8017 if (resp->wol_type ==
8018 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
8019 bp->wol = 1;
8020 bp->wol_filter_id = resp->wol_filter_id;
8021 }
8022 }
8023 }
8024 mutex_unlock(&bp->hwrm_cmd_lock);
8025 return next_handle;
8026}
8027
8028static void bnxt_get_wol_settings(struct bnxt *bp)
8029{
8030 u16 handle = 0;
8031
8032 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
8033 return;
8034
8035 do {
8036 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
8037 } while (handle && handle != 0xffff);
8038}
8039
Vasundhara Volamcde49a42018-08-05 16:51:56 -04008040#ifdef CONFIG_BNXT_HWMON
8041static ssize_t bnxt_show_temp(struct device *dev,
8042 struct device_attribute *devattr, char *buf)
8043{
8044 struct hwrm_temp_monitor_query_input req = {0};
8045 struct hwrm_temp_monitor_query_output *resp;
8046 struct bnxt *bp = dev_get_drvdata(dev);
8047 u32 temp = 0;
8048
8049 resp = bp->hwrm_cmd_resp_addr;
8050 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
8051 mutex_lock(&bp->hwrm_cmd_lock);
8052 if (!_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
8053 temp = resp->temp * 1000; /* display millidegree */
8054 mutex_unlock(&bp->hwrm_cmd_lock);
8055
8056 return sprintf(buf, "%u\n", temp);
8057}
8058static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
8059
8060static struct attribute *bnxt_attrs[] = {
8061 &sensor_dev_attr_temp1_input.dev_attr.attr,
8062 NULL
8063};
8064ATTRIBUTE_GROUPS(bnxt);
8065
8066static void bnxt_hwmon_close(struct bnxt *bp)
8067{
8068 if (bp->hwmon_dev) {
8069 hwmon_device_unregister(bp->hwmon_dev);
8070 bp->hwmon_dev = NULL;
8071 }
8072}
8073
8074static void bnxt_hwmon_open(struct bnxt *bp)
8075{
8076 struct pci_dev *pdev = bp->pdev;
8077
8078 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
8079 DRV_MODULE_NAME, bp,
8080 bnxt_groups);
8081 if (IS_ERR(bp->hwmon_dev)) {
8082 bp->hwmon_dev = NULL;
8083 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
8084 }
8085}
8086#else
8087static void bnxt_hwmon_close(struct bnxt *bp)
8088{
8089}
8090
8091static void bnxt_hwmon_open(struct bnxt *bp)
8092{
8093}
8094#endif
8095
Michael Chan939f7f02016-04-05 14:08:58 -04008096static bool bnxt_eee_config_ok(struct bnxt *bp)
8097{
8098 struct ethtool_eee *eee = &bp->eee;
8099 struct bnxt_link_info *link_info = &bp->link_info;
8100
8101 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
8102 return true;
8103
8104 if (eee->eee_enabled) {
8105 u32 advertising =
8106 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
8107
8108 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8109 eee->eee_enabled = 0;
8110 return false;
8111 }
8112 if (eee->advertised & ~advertising) {
8113 eee->advertised = advertising & eee->supported;
8114 return false;
8115 }
8116 }
8117 return true;
8118}
8119
Michael Chanc0c050c2015-10-22 16:01:17 -04008120static int bnxt_update_phy_setting(struct bnxt *bp)
8121{
8122 int rc;
8123 bool update_link = false;
8124 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04008125 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04008126 struct bnxt_link_info *link_info = &bp->link_info;
8127
8128 rc = bnxt_update_link(bp, true);
8129 if (rc) {
8130 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
8131 rc);
8132 return rc;
8133 }
Michael Chan33dac242017-02-12 19:18:15 -05008134 if (!BNXT_SINGLE_PF(bp))
8135 return 0;
8136
Michael Chanc0c050c2015-10-22 16:01:17 -04008137 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04008138 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
8139 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04008140 update_pause = true;
8141 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
8142 link_info->force_pause_setting != link_info->req_flow_ctrl)
8143 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04008144 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8145 if (BNXT_AUTO_MODE(link_info->auto_mode))
8146 update_link = true;
8147 if (link_info->req_link_speed != link_info->force_link_speed)
8148 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05008149 if (link_info->req_duplex != link_info->duplex_setting)
8150 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04008151 } else {
8152 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
8153 update_link = true;
8154 if (link_info->advertising != link_info->auto_link_speeds)
8155 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04008156 }
8157
Michael Chan16d663a2016-11-16 21:13:07 -05008158 /* The last close may have shutdown the link, so need to call
8159 * PHY_CFG to bring it back up.
8160 */
8161 if (!netif_carrier_ok(bp->dev))
8162 update_link = true;
8163
Michael Chan939f7f02016-04-05 14:08:58 -04008164 if (!bnxt_eee_config_ok(bp))
8165 update_eee = true;
8166
Michael Chanc0c050c2015-10-22 16:01:17 -04008167 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04008168 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04008169 else if (update_pause)
8170 rc = bnxt_hwrm_set_pause(bp);
8171 if (rc) {
8172 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
8173 rc);
8174 return rc;
8175 }
8176
8177 return rc;
8178}
8179
Jeffrey Huang11809492015-11-05 16:25:49 -05008180/* Common routine to pre-map certain register block to different GRC window.
8181 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
8182 * in PF and 3 windows in VF that can be customized to map in different
8183 * register blocks.
8184 */
8185static void bnxt_preset_reg_win(struct bnxt *bp)
8186{
8187 if (BNXT_PF(bp)) {
8188 /* CAG registers map to GRC window #4 */
8189 writel(BNXT_CAG_REG_BASE,
8190 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
8191 }
8192}
8193
Michael Chan47558ac2018-04-26 17:44:44 -04008194static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
8195
Michael Chanc0c050c2015-10-22 16:01:17 -04008196static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8197{
8198 int rc = 0;
8199
Jeffrey Huang11809492015-11-05 16:25:49 -05008200 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008201 netif_carrier_off(bp->dev);
8202 if (irq_re_init) {
Michael Chan47558ac2018-04-26 17:44:44 -04008203 /* Reserve rings now if none were reserved at driver probe. */
8204 rc = bnxt_init_dflt_ring_mode(bp);
8205 if (rc) {
8206 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
8207 return rc;
8208 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008209 }
Michael Chan41e8d792018-10-14 07:02:48 -04008210 rc = bnxt_reserve_rings(bp);
8211 if (rc)
8212 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008213 if ((bp->flags & BNXT_FLAG_RFS) &&
8214 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
8215 /* disable RFS if falling back to INTA */
8216 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
8217 bp->flags &= ~BNXT_FLAG_RFS;
8218 }
8219
8220 rc = bnxt_alloc_mem(bp, irq_re_init);
8221 if (rc) {
8222 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8223 goto open_err_free_mem;
8224 }
8225
8226 if (irq_re_init) {
8227 bnxt_init_napi(bp);
8228 rc = bnxt_request_irq(bp);
8229 if (rc) {
8230 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
Vikas Guptac58387a2018-07-09 02:24:52 -04008231 goto open_err_irq;
Michael Chanc0c050c2015-10-22 16:01:17 -04008232 }
8233 }
8234
8235 bnxt_enable_napi(bp);
Andy Gospodarekcabfb092018-04-26 17:44:40 -04008236 bnxt_debug_dev_init(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008237
8238 rc = bnxt_init_nic(bp, irq_re_init);
8239 if (rc) {
8240 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8241 goto open_err;
8242 }
8243
8244 if (link_re_init) {
Michael Chane2dc9b62017-10-13 21:09:30 -04008245 mutex_lock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04008246 rc = bnxt_update_phy_setting(bp);
Michael Chane2dc9b62017-10-13 21:09:30 -04008247 mutex_unlock(&bp->link_lock);
Michael Chana1ef4a72018-08-05 16:51:49 -04008248 if (rc) {
Michael Chanba41d462016-02-19 19:43:21 -05008249 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chana1ef4a72018-08-05 16:51:49 -04008250 if (BNXT_SINGLE_PF(bp)) {
8251 bp->link_info.phy_retry = true;
8252 bp->link_info.phy_retry_expires =
8253 jiffies + 5 * HZ;
8254 }
8255 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008256 }
8257
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07008258 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07008259 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04008260
Michael Chancaefe522015-12-09 19:35:42 -05008261 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04008262 bnxt_enable_int(bp);
8263 /* Enable TX queues */
8264 bnxt_tx_enable(bp);
8265 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04008266 /* Poll link status and check for SFP+ module status */
8267 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008268
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04008269 /* VF-reps may need to be re-opened after the PF is re-opened */
8270 if (BNXT_PF(bp))
8271 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008272 return 0;
8273
8274open_err:
Andy Gospodarekcabfb092018-04-26 17:44:40 -04008275 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008276 bnxt_disable_napi(bp);
Vikas Guptac58387a2018-07-09 02:24:52 -04008277
8278open_err_irq:
Michael Chanc0c050c2015-10-22 16:01:17 -04008279 bnxt_del_napi(bp);
8280
8281open_err_free_mem:
8282 bnxt_free_skbs(bp);
8283 bnxt_free_irq(bp);
8284 bnxt_free_mem(bp, true);
8285 return rc;
8286}
8287
8288/* rtnl_lock held */
8289int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8290{
8291 int rc = 0;
8292
8293 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
8294 if (rc) {
8295 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
8296 dev_close(bp->dev);
8297 }
8298 return rc;
8299}
8300
Michael Chanf7dc1ea2017-04-04 18:14:13 -04008301/* rtnl_lock held, open the NIC half way by allocating all resources, but
8302 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
8303 * self tests.
8304 */
8305int bnxt_half_open_nic(struct bnxt *bp)
8306{
8307 int rc = 0;
8308
8309 rc = bnxt_alloc_mem(bp, false);
8310 if (rc) {
8311 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8312 goto half_open_err;
8313 }
8314 rc = bnxt_init_nic(bp, false);
8315 if (rc) {
8316 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8317 goto half_open_err;
8318 }
8319 return 0;
8320
8321half_open_err:
8322 bnxt_free_skbs(bp);
8323 bnxt_free_mem(bp, false);
8324 dev_close(bp->dev);
8325 return rc;
8326}
8327
8328/* rtnl_lock held, this call can only be made after a previous successful
8329 * call to bnxt_half_open_nic().
8330 */
8331void bnxt_half_close_nic(struct bnxt *bp)
8332{
8333 bnxt_hwrm_resource_free(bp, false, false);
8334 bnxt_free_skbs(bp);
8335 bnxt_free_mem(bp, false);
8336}
8337
Michael Chanc0c050c2015-10-22 16:01:17 -04008338static int bnxt_open(struct net_device *dev)
8339{
8340 struct bnxt *bp = netdev_priv(dev);
Michael Chan25e1acd2018-08-05 16:51:55 -04008341 int rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008342
Michael Chan25e1acd2018-08-05 16:51:55 -04008343 bnxt_hwrm_if_change(bp, true);
8344 rc = __bnxt_open_nic(bp, true, true);
8345 if (rc)
8346 bnxt_hwrm_if_change(bp, false);
Vasundhara Volamcde49a42018-08-05 16:51:56 -04008347
8348 bnxt_hwmon_open(bp);
8349
Michael Chan25e1acd2018-08-05 16:51:55 -04008350 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008351}
8352
Michael Chanf9b76eb2017-07-11 13:05:34 -04008353static bool bnxt_drv_busy(struct bnxt *bp)
8354{
8355 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
8356 test_bit(BNXT_STATE_READ_STATS, &bp->state));
8357}
8358
Michael Chanb8875ca2018-12-16 18:46:29 -05008359static void bnxt_get_ring_stats(struct bnxt *bp,
8360 struct rtnl_link_stats64 *stats);
8361
Michael Chan86e953d2018-01-17 03:21:04 -05008362static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
8363 bool link_re_init)
Michael Chanc0c050c2015-10-22 16:01:17 -04008364{
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04008365 /* Close the VF-reps before closing PF */
8366 if (BNXT_PF(bp))
8367 bnxt_vf_reps_close(bp);
Michael Chan86e953d2018-01-17 03:21:04 -05008368
Michael Chanc0c050c2015-10-22 16:01:17 -04008369 /* Change device state to avoid TX queue wake up's */
8370 bnxt_tx_disable(bp);
8371
Michael Chancaefe522015-12-09 19:35:42 -05008372 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05008373 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04008374 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05008375 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04008376
Michael Chan9d8bc092016-12-29 12:13:33 -05008377 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04008378 bnxt_shutdown_nic(bp, irq_re_init);
8379
8380 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
8381
Andy Gospodarekcabfb092018-04-26 17:44:40 -04008382 bnxt_debug_dev_exit(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008383 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008384 del_timer_sync(&bp->timer);
8385 bnxt_free_skbs(bp);
8386
Michael Chanb8875ca2018-12-16 18:46:29 -05008387 /* Save ring stats before shutdown */
8388 if (bp->bnapi)
8389 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
Michael Chanc0c050c2015-10-22 16:01:17 -04008390 if (irq_re_init) {
8391 bnxt_free_irq(bp);
8392 bnxt_del_napi(bp);
8393 }
8394 bnxt_free_mem(bp, irq_re_init);
Michael Chan86e953d2018-01-17 03:21:04 -05008395}
8396
8397int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8398{
8399 int rc = 0;
8400
8401#ifdef CONFIG_BNXT_SRIOV
8402 if (bp->sriov_cfg) {
8403 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
8404 !bp->sriov_cfg,
8405 BNXT_SRIOV_CFG_WAIT_TMO);
8406 if (rc)
8407 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
8408 }
8409#endif
8410 __bnxt_close_nic(bp, irq_re_init, link_re_init);
Michael Chanc0c050c2015-10-22 16:01:17 -04008411 return rc;
8412}
8413
8414static int bnxt_close(struct net_device *dev)
8415{
8416 struct bnxt *bp = netdev_priv(dev);
8417
Vasundhara Volamcde49a42018-08-05 16:51:56 -04008418 bnxt_hwmon_close(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008419 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04008420 bnxt_hwrm_shutdown_link(bp);
Michael Chan25e1acd2018-08-05 16:51:55 -04008421 bnxt_hwrm_if_change(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04008422 return 0;
8423}
8424
8425/* rtnl_lock held */
8426static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
8427{
8428 switch (cmd) {
8429 case SIOCGMIIPHY:
8430 /* fallthru */
8431 case SIOCGMIIREG: {
8432 if (!netif_running(dev))
8433 return -EAGAIN;
8434
8435 return 0;
8436 }
8437
8438 case SIOCSMIIREG:
8439 if (!netif_running(dev))
8440 return -EAGAIN;
8441
8442 return 0;
8443
8444 default:
8445 /* do nothing */
8446 break;
8447 }
8448 return -EOPNOTSUPP;
8449}
8450
Michael Chanb8875ca2018-12-16 18:46:29 -05008451static void bnxt_get_ring_stats(struct bnxt *bp,
8452 struct rtnl_link_stats64 *stats)
Michael Chanc0c050c2015-10-22 16:01:17 -04008453{
Michael Chanb8875ca2018-12-16 18:46:29 -05008454 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04008455
Michael Chanc0c050c2015-10-22 16:01:17 -04008456
Michael Chanc0c050c2015-10-22 16:01:17 -04008457 for (i = 0; i < bp->cp_nr_rings; i++) {
8458 struct bnxt_napi *bnapi = bp->bnapi[i];
8459 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8460 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
8461
8462 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
8463 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
8464 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
8465
8466 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
8467 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
8468 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
8469
8470 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
8471 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
8472 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
8473
8474 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
8475 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
8476 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
8477
8478 stats->rx_missed_errors +=
8479 le64_to_cpu(hw_stats->rx_discard_pkts);
8480
8481 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
8482
Michael Chanc0c050c2015-10-22 16:01:17 -04008483 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
8484 }
Michael Chanb8875ca2018-12-16 18:46:29 -05008485}
8486
8487static void bnxt_add_prev_stats(struct bnxt *bp,
8488 struct rtnl_link_stats64 *stats)
8489{
8490 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
8491
8492 stats->rx_packets += prev_stats->rx_packets;
8493 stats->tx_packets += prev_stats->tx_packets;
8494 stats->rx_bytes += prev_stats->rx_bytes;
8495 stats->tx_bytes += prev_stats->tx_bytes;
8496 stats->rx_missed_errors += prev_stats->rx_missed_errors;
8497 stats->multicast += prev_stats->multicast;
8498 stats->tx_dropped += prev_stats->tx_dropped;
8499}
8500
8501static void
8502bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
8503{
8504 struct bnxt *bp = netdev_priv(dev);
8505
8506 set_bit(BNXT_STATE_READ_STATS, &bp->state);
8507 /* Make sure bnxt_close_nic() sees that we are reading stats before
8508 * we check the BNXT_STATE_OPEN flag.
8509 */
8510 smp_mb__after_atomic();
8511 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
8512 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
8513 *stats = bp->net_stats_prev;
8514 return;
8515 }
8516
8517 bnxt_get_ring_stats(bp, stats);
8518 bnxt_add_prev_stats(bp, stats);
Michael Chanc0c050c2015-10-22 16:01:17 -04008519
Michael Chan9947f832016-03-07 15:38:46 -05008520 if (bp->flags & BNXT_FLAG_PORT_STATS) {
8521 struct rx_port_stats *rx = bp->hw_rx_port_stats;
8522 struct tx_port_stats *tx = bp->hw_tx_port_stats;
8523
8524 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
8525 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
8526 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
8527 le64_to_cpu(rx->rx_ovrsz_frames) +
8528 le64_to_cpu(rx->rx_runt_frames);
8529 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
8530 le64_to_cpu(rx->rx_jbr_frames);
8531 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
8532 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
8533 stats->tx_errors = le64_to_cpu(tx->tx_err);
8534 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04008535 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04008536}
8537
8538static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
8539{
8540 struct net_device *dev = bp->dev;
8541 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8542 struct netdev_hw_addr *ha;
8543 u8 *haddr;
8544 int mc_count = 0;
8545 bool update = false;
8546 int off = 0;
8547
8548 netdev_for_each_mc_addr(ha, dev) {
8549 if (mc_count >= BNXT_MAX_MC_ADDRS) {
8550 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8551 vnic->mc_list_count = 0;
8552 return false;
8553 }
8554 haddr = ha->addr;
8555 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
8556 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
8557 update = true;
8558 }
8559 off += ETH_ALEN;
8560 mc_count++;
8561 }
8562 if (mc_count)
8563 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
8564
8565 if (mc_count != vnic->mc_list_count) {
8566 vnic->mc_list_count = mc_count;
8567 update = true;
8568 }
8569 return update;
8570}
8571
8572static bool bnxt_uc_list_updated(struct bnxt *bp)
8573{
8574 struct net_device *dev = bp->dev;
8575 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8576 struct netdev_hw_addr *ha;
8577 int off = 0;
8578
8579 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
8580 return true;
8581
8582 netdev_for_each_uc_addr(ha, dev) {
8583 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
8584 return true;
8585
8586 off += ETH_ALEN;
8587 }
8588 return false;
8589}
8590
8591static void bnxt_set_rx_mode(struct net_device *dev)
8592{
8593 struct bnxt *bp = netdev_priv(dev);
8594 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8595 u32 mask = vnic->rx_mask;
8596 bool mc_update = false;
8597 bool uc_update;
8598
8599 if (!netif_running(dev))
8600 return;
8601
8602 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
8603 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
Michael Chan30e33842018-07-09 02:24:50 -04008604 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
8605 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
Michael Chanc0c050c2015-10-22 16:01:17 -04008606
Michael Chan17c71ac2016-07-01 18:46:27 -04008607 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04008608 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8609
8610 uc_update = bnxt_uc_list_updated(bp);
8611
Michael Chan30e33842018-07-09 02:24:50 -04008612 if (dev->flags & IFF_BROADCAST)
8613 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04008614 if (dev->flags & IFF_ALLMULTI) {
8615 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8616 vnic->mc_list_count = 0;
8617 } else {
8618 mc_update = bnxt_mc_list_updated(bp, &mask);
8619 }
8620
8621 if (mask != vnic->rx_mask || uc_update || mc_update) {
8622 vnic->rx_mask = mask;
8623
8624 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008625 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008626 }
8627}
8628
Michael Chanb664f002015-12-02 01:54:08 -05008629static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04008630{
8631 struct net_device *dev = bp->dev;
8632 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8633 struct netdev_hw_addr *ha;
8634 int i, off = 0, rc;
8635 bool uc_update;
8636
8637 netif_addr_lock_bh(dev);
8638 uc_update = bnxt_uc_list_updated(bp);
8639 netif_addr_unlock_bh(dev);
8640
8641 if (!uc_update)
8642 goto skip_uc;
8643
8644 mutex_lock(&bp->hwrm_cmd_lock);
8645 for (i = 1; i < vnic->uc_filter_count; i++) {
8646 struct hwrm_cfa_l2_filter_free_input req = {0};
8647
8648 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
8649 -1);
8650
8651 req.l2_filter_id = vnic->fw_l2_filter_id[i];
8652
8653 rc = _hwrm_send_message(bp, &req, sizeof(req),
8654 HWRM_CMD_TIMEOUT);
8655 }
8656 mutex_unlock(&bp->hwrm_cmd_lock);
8657
8658 vnic->uc_filter_count = 1;
8659
8660 netif_addr_lock_bh(dev);
8661 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
8662 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8663 } else {
8664 netdev_for_each_uc_addr(ha, dev) {
8665 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
8666 off += ETH_ALEN;
8667 vnic->uc_filter_count++;
8668 }
8669 }
8670 netif_addr_unlock_bh(dev);
8671
8672 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
8673 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
8674 if (rc) {
8675 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
8676 rc);
8677 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05008678 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008679 }
8680 }
8681
8682skip_uc:
8683 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
8684 if (rc)
8685 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
8686 rc);
Michael Chanb664f002015-12-02 01:54:08 -05008687
8688 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04008689}
8690
Michael Chan2773dfb2018-04-26 17:44:42 -04008691static bool bnxt_can_reserve_rings(struct bnxt *bp)
8692{
8693#ifdef CONFIG_BNXT_SRIOV
Michael Chanf1ca94d2018-08-05 16:51:53 -04008694 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
Michael Chan2773dfb2018-04-26 17:44:42 -04008695 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8696
8697 /* No minimum rings were provisioned by the PF. Don't
8698 * reserve rings by default when device is down.
8699 */
8700 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
8701 return true;
8702
8703 if (!netif_running(bp->dev))
8704 return false;
8705 }
8706#endif
8707 return true;
8708}
8709
Michael Chan8079e8f2016-12-29 12:13:37 -05008710/* If the chip and firmware supports RFS */
8711static bool bnxt_rfs_supported(struct bnxt *bp)
8712{
Michael Chan41e8d792018-10-14 07:02:48 -04008713 if (bp->flags & BNXT_FLAG_CHIP_P5)
8714 return false;
Michael Chan8079e8f2016-12-29 12:13:37 -05008715 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
8716 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05008717 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8718 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05008719 return false;
8720}
8721
8722/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008723static bool bnxt_rfs_capable(struct bnxt *bp)
8724{
8725#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05008726 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008727
Michael Chan41e8d792018-10-14 07:02:48 -04008728 if (bp->flags & BNXT_FLAG_CHIP_P5)
8729 return false;
Michael Chan2773dfb2018-04-26 17:44:42 -04008730 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008731 return false;
8732
8733 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05008734 max_vnics = bnxt_get_max_func_vnics(bp);
8735 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05008736
8737 /* RSS contexts not a limiting factor */
8738 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8739 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05008740 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Michael Chan6a1eef52018-01-17 03:21:10 -05008741 if (bp->rx_nr_rings > 1)
8742 netdev_warn(bp->dev,
8743 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
8744 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008745 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04008746 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008747
Michael Chanf1ca94d2018-08-05 16:51:53 -04008748 if (!BNXT_NEW_RM(bp))
Michael Chan6a1eef52018-01-17 03:21:10 -05008749 return true;
8750
8751 if (vnics == bp->hw_resc.resv_vnics)
8752 return true;
8753
Vasundhara Volam780baad2018-12-16 18:46:23 -05008754 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
Michael Chan6a1eef52018-01-17 03:21:10 -05008755 if (vnics <= bp->hw_resc.resv_vnics)
8756 return true;
8757
8758 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
Vasundhara Volam780baad2018-12-16 18:46:23 -05008759 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
Michael Chan6a1eef52018-01-17 03:21:10 -05008760 return false;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008761#else
8762 return false;
8763#endif
8764}
8765
Michael Chanc0c050c2015-10-22 16:01:17 -04008766static netdev_features_t bnxt_fix_features(struct net_device *dev,
8767 netdev_features_t features)
8768{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008769 struct bnxt *bp = netdev_priv(dev);
8770
Vasundhara Volama2304902016-07-25 12:33:36 -04008771 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008772 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04008773
Michael Chan1054aee2017-12-16 03:09:42 -05008774 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
8775 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8776
8777 if (!(features & NETIF_F_GRO))
8778 features &= ~NETIF_F_GRO_HW;
8779
8780 if (features & NETIF_F_GRO_HW)
8781 features &= ~NETIF_F_LRO;
8782
Michael Chan5a9f6b22016-06-06 02:37:15 -04008783 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
8784 * turned on or off together.
8785 */
8786 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
8787 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
8788 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
8789 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
8790 NETIF_F_HW_VLAN_STAG_RX);
8791 else
8792 features |= NETIF_F_HW_VLAN_CTAG_RX |
8793 NETIF_F_HW_VLAN_STAG_RX;
8794 }
Michael Chancf6645f2016-06-13 02:25:28 -04008795#ifdef CONFIG_BNXT_SRIOV
8796 if (BNXT_VF(bp)) {
8797 if (bp->vf.vlan) {
8798 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
8799 NETIF_F_HW_VLAN_STAG_RX);
8800 }
8801 }
8802#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04008803 return features;
8804}
8805
8806static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
8807{
8808 struct bnxt *bp = netdev_priv(dev);
8809 u32 flags = bp->flags;
8810 u32 changes;
8811 int rc = 0;
8812 bool re_init = false;
8813 bool update_tpa = false;
8814
8815 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Michael Chan1054aee2017-12-16 03:09:42 -05008816 if (features & NETIF_F_GRO_HW)
Michael Chanc0c050c2015-10-22 16:01:17 -04008817 flags |= BNXT_FLAG_GRO;
Michael Chan1054aee2017-12-16 03:09:42 -05008818 else if (features & NETIF_F_LRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04008819 flags |= BNXT_FLAG_LRO;
8820
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008821 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
8822 flags &= ~BNXT_FLAG_TPA;
8823
Michael Chanc0c050c2015-10-22 16:01:17 -04008824 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8825 flags |= BNXT_FLAG_STRIP_VLAN;
8826
8827 if (features & NETIF_F_NTUPLE)
8828 flags |= BNXT_FLAG_RFS;
8829
8830 changes = flags ^ bp->flags;
8831 if (changes & BNXT_FLAG_TPA) {
8832 update_tpa = true;
8833 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
8834 (flags & BNXT_FLAG_TPA) == 0)
8835 re_init = true;
8836 }
8837
8838 if (changes & ~BNXT_FLAG_TPA)
8839 re_init = true;
8840
8841 if (flags != bp->flags) {
8842 u32 old_flags = bp->flags;
8843
8844 bp->flags = flags;
8845
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008846 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04008847 if (update_tpa)
8848 bnxt_set_ring_params(bp);
8849 return rc;
8850 }
8851
8852 if (re_init) {
8853 bnxt_close_nic(bp, false, false);
8854 if (update_tpa)
8855 bnxt_set_ring_params(bp);
8856
8857 return bnxt_open_nic(bp, false, false);
8858 }
8859 if (update_tpa) {
8860 rc = bnxt_set_tpa(bp,
8861 (flags & BNXT_FLAG_TPA) ?
8862 true : false);
8863 if (rc)
8864 bp->flags = old_flags;
8865 }
8866 }
8867 return rc;
8868}
8869
Michael Chanffd77622018-11-15 03:25:40 -05008870static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
8871 u32 ring_id, u32 *prod, u32 *cons)
8872{
8873 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
8874 struct hwrm_dbg_ring_info_get_input req = {0};
8875 int rc;
8876
8877 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
8878 req.ring_type = ring_type;
8879 req.fw_ring_id = cpu_to_le32(ring_id);
8880 mutex_lock(&bp->hwrm_cmd_lock);
8881 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8882 if (!rc) {
8883 *prod = le32_to_cpu(resp->producer_index);
8884 *cons = le32_to_cpu(resp->consumer_index);
8885 }
8886 mutex_unlock(&bp->hwrm_cmd_lock);
8887 return rc;
8888}
8889
Michael Chan9f554592016-01-02 23:44:58 -05008890static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
8891{
Michael Chanb6ab4b02016-01-02 23:44:59 -05008892 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05008893 int i = bnapi->index;
8894
Michael Chan3b2b7d92016-01-02 23:45:00 -05008895 if (!txr)
8896 return;
8897
Michael Chan9f554592016-01-02 23:44:58 -05008898 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
8899 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
8900 txr->tx_cons);
8901}
8902
8903static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
8904{
Michael Chanb6ab4b02016-01-02 23:44:59 -05008905 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05008906 int i = bnapi->index;
8907
Michael Chan3b2b7d92016-01-02 23:45:00 -05008908 if (!rxr)
8909 return;
8910
Michael Chan9f554592016-01-02 23:44:58 -05008911 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",
8912 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
8913 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
8914 rxr->rx_sw_agg_prod);
8915}
8916
8917static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
8918{
8919 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8920 int i = bnapi->index;
8921
8922 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
8923 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
8924}
8925
Michael Chanc0c050c2015-10-22 16:01:17 -04008926static void bnxt_dbg_dump_states(struct bnxt *bp)
8927{
8928 int i;
8929 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04008930
8931 for (i = 0; i < bp->cp_nr_rings; i++) {
8932 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04008933 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05008934 bnxt_dump_tx_sw_state(bnapi);
8935 bnxt_dump_rx_sw_state(bnapi);
8936 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04008937 }
8938 }
8939}
8940
Michael Chan6988bd92016-06-13 02:25:29 -04008941static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04008942{
Michael Chan6988bd92016-06-13 02:25:29 -04008943 if (!silent)
8944 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05008945 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05008946 int rc;
8947
8948 if (!silent)
8949 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05008950 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05008951 rc = bnxt_open_nic(bp, false, false);
8952 if (!silent && !rc)
8953 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05008954 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008955}
8956
8957static void bnxt_tx_timeout(struct net_device *dev)
8958{
8959 struct bnxt *bp = netdev_priv(dev);
8960
8961 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
8962 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008963 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008964}
8965
Kees Cooke99e88a2017-10-16 14:43:17 -07008966static void bnxt_timer(struct timer_list *t)
Michael Chanc0c050c2015-10-22 16:01:17 -04008967{
Kees Cooke99e88a2017-10-16 14:43:17 -07008968 struct bnxt *bp = from_timer(bp, t, timer);
Michael Chanc0c050c2015-10-22 16:01:17 -04008969 struct net_device *dev = bp->dev;
8970
8971 if (!netif_running(dev))
8972 return;
8973
8974 if (atomic_read(&bp->intr_sem) != 0)
8975 goto bnxt_restart_timer;
8976
Michael Chanadcc3312017-07-24 12:34:24 -04008977 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
8978 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05008979 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04008980 bnxt_queue_sp_work(bp);
Michael Chan3bdf56c2016-03-07 15:38:45 -05008981 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04008982
8983 if (bnxt_tc_flower_enabled(bp)) {
8984 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
8985 bnxt_queue_sp_work(bp);
8986 }
Michael Chana1ef4a72018-08-05 16:51:49 -04008987
8988 if (bp->link_info.phy_retry) {
8989 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
8990 bp->link_info.phy_retry = 0;
8991 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
8992 } else {
8993 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
8994 bnxt_queue_sp_work(bp);
8995 }
8996 }
Michael Chanffd77622018-11-15 03:25:40 -05008997
8998 if ((bp->flags & BNXT_FLAG_CHIP_P5) && netif_carrier_ok(dev)) {
8999 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
9000 bnxt_queue_sp_work(bp);
9001 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009002bnxt_restart_timer:
9003 mod_timer(&bp->timer, jiffies + bp->current_interval);
9004}
9005
Michael Chana551ee92017-01-25 02:55:07 -05009006static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04009007{
Michael Chana551ee92017-01-25 02:55:07 -05009008 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
9009 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04009010 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
9011 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
9012 */
9013 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9014 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05009015}
9016
9017static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
9018{
Michael Chan6988bd92016-06-13 02:25:29 -04009019 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9020 rtnl_unlock();
9021}
9022
Michael Chana551ee92017-01-25 02:55:07 -05009023/* Only called from bnxt_sp_task() */
9024static void bnxt_reset(struct bnxt *bp, bool silent)
9025{
9026 bnxt_rtnl_lock_sp(bp);
9027 if (test_bit(BNXT_STATE_OPEN, &bp->state))
9028 bnxt_reset_task(bp, silent);
9029 bnxt_rtnl_unlock_sp(bp);
9030}
9031
Michael Chanffd77622018-11-15 03:25:40 -05009032static void bnxt_chk_missed_irq(struct bnxt *bp)
9033{
9034 int i;
9035
9036 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9037 return;
9038
9039 for (i = 0; i < bp->cp_nr_rings; i++) {
9040 struct bnxt_napi *bnapi = bp->bnapi[i];
9041 struct bnxt_cp_ring_info *cpr;
9042 u32 fw_ring_id;
9043 int j;
9044
9045 if (!bnapi)
9046 continue;
9047
9048 cpr = &bnapi->cp_ring;
9049 for (j = 0; j < 2; j++) {
9050 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
9051 u32 val[2];
9052
9053 if (!cpr2 || cpr2->has_more_work ||
9054 !bnxt_has_work(bp, cpr2))
9055 continue;
9056
9057 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
9058 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
9059 continue;
9060 }
9061 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
9062 bnxt_dbg_hwrm_ring_info_get(bp,
9063 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
9064 fw_ring_id, &val[0], &val[1]);
Michael Chan83eb5c52018-11-15 03:25:41 -05009065 cpr->missed_irqs++;
Michael Chanffd77622018-11-15 03:25:40 -05009066 }
9067 }
9068}
9069
Michael Chanc0c050c2015-10-22 16:01:17 -04009070static void bnxt_cfg_ntp_filters(struct bnxt *);
9071
9072static void bnxt_sp_task(struct work_struct *work)
9073{
9074 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04009075
Michael Chan4cebdce2015-12-09 19:35:43 -05009076 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9077 smp_mb__after_atomic();
9078 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9079 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04009080 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05009081 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009082
9083 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
9084 bnxt_cfg_rx_mode(bp);
9085
9086 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
9087 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009088 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
9089 bnxt_hwrm_exec_fwd_req(bp);
9090 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9091 bnxt_hwrm_tunnel_dst_port_alloc(
9092 bp, bp->vxlan_port,
9093 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9094 }
9095 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9096 bnxt_hwrm_tunnel_dst_port_free(
9097 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9098 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07009099 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9100 bnxt_hwrm_tunnel_dst_port_alloc(
9101 bp, bp->nge_port,
9102 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9103 }
9104 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9105 bnxt_hwrm_tunnel_dst_port_free(
9106 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9107 }
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04009108 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05009109 bnxt_hwrm_port_qstats(bp);
Vasundhara Volam00db3cb2018-03-31 13:54:12 -04009110 bnxt_hwrm_port_qstats_ext(bp);
9111 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05009112
Michael Chan0eaa24b2017-01-25 02:55:08 -05009113 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04009114 int rc;
Michael Chan0eaa24b2017-01-25 02:55:08 -05009115
Michael Chane2dc9b62017-10-13 21:09:30 -04009116 mutex_lock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05009117 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
9118 &bp->sp_event))
9119 bnxt_hwrm_phy_qcaps(bp);
9120
Michael Chane2dc9b62017-10-13 21:09:30 -04009121 rc = bnxt_update_link(bp, true);
9122 mutex_unlock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05009123 if (rc)
9124 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
9125 rc);
9126 }
Michael Chana1ef4a72018-08-05 16:51:49 -04009127 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
9128 int rc;
9129
9130 mutex_lock(&bp->link_lock);
9131 rc = bnxt_update_phy_setting(bp);
9132 mutex_unlock(&bp->link_lock);
9133 if (rc) {
9134 netdev_warn(bp->dev, "update phy settings retry failed\n");
9135 } else {
9136 bp->link_info.phy_retry = false;
9137 netdev_info(bp->dev, "update phy settings retry succeeded\n");
9138 }
9139 }
Michael Chan90c694b2017-01-25 02:55:09 -05009140 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04009141 mutex_lock(&bp->link_lock);
9142 bnxt_get_port_module_status(bp);
9143 mutex_unlock(&bp->link_lock);
Michael Chan90c694b2017-01-25 02:55:09 -05009144 }
Sathya Perla5a84acb2017-10-26 11:51:31 -04009145
9146 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
9147 bnxt_tc_flow_stats_work(bp);
9148
Michael Chanffd77622018-11-15 03:25:40 -05009149 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
9150 bnxt_chk_missed_irq(bp);
9151
Michael Chane2dc9b62017-10-13 21:09:30 -04009152 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
9153 * must be the last functions to be called before exiting.
9154 */
Michael Chanc0c050c2015-10-22 16:01:17 -04009155 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
9156 bnxt_reset(bp, false);
9157
9158 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
9159 bnxt_reset(bp, true);
9160
Michael Chanc0c050c2015-10-22 16:01:17 -04009161 smp_mb__before_atomic();
9162 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9163}
9164
Michael Chand1e79252017-02-06 16:55:38 -05009165/* Under rtnl_lock */
Michael Chan98fdbe72017-08-28 13:40:26 -04009166int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
9167 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05009168{
9169 int max_rx, max_tx, tx_sets = 1;
Vasundhara Volam780baad2018-12-16 18:46:23 -05009170 int tx_rings_needed, stats;
Michael Chan8f23d632018-01-17 03:21:12 -05009171 int rx_rings = rx;
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05009172 int cp, vnics, rc;
Michael Chand1e79252017-02-06 16:55:38 -05009173
Michael Chand1e79252017-02-06 16:55:38 -05009174 if (tcs)
9175 tx_sets = tcs;
9176
9177 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
9178 if (rc)
9179 return rc;
9180
9181 if (max_rx < rx)
9182 return -ENOMEM;
9183
Michael Chan5f449242017-02-06 16:55:40 -05009184 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05009185 if (max_tx < tx_rings_needed)
9186 return -ENOMEM;
9187
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05009188 vnics = 1;
9189 if (bp->flags & BNXT_FLAG_RFS)
9190 vnics += rx_rings;
9191
Michael Chan8f23d632018-01-17 03:21:12 -05009192 if (bp->flags & BNXT_FLAG_AGG_RINGS)
9193 rx_rings <<= 1;
9194 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
Vasundhara Volam780baad2018-12-16 18:46:23 -05009195 stats = cp;
9196 if (BNXT_NEW_RM(bp)) {
Michael Chan11c3ec72018-04-11 11:50:17 -04009197 cp += bnxt_get_ulp_msix_num(bp);
Vasundhara Volam780baad2018-12-16 18:46:23 -05009198 stats += bnxt_get_ulp_stat_ctxs(bp);
9199 }
Eddie Wai6fc2ffd2018-03-09 23:46:04 -05009200 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
Vasundhara Volam780baad2018-12-16 18:46:23 -05009201 stats, vnics);
Michael Chand1e79252017-02-06 16:55:38 -05009202}
9203
Sathya Perla17086392017-02-20 19:25:18 -05009204static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
9205{
9206 if (bp->bar2) {
9207 pci_iounmap(pdev, bp->bar2);
9208 bp->bar2 = NULL;
9209 }
9210
9211 if (bp->bar1) {
9212 pci_iounmap(pdev, bp->bar1);
9213 bp->bar1 = NULL;
9214 }
9215
9216 if (bp->bar0) {
9217 pci_iounmap(pdev, bp->bar0);
9218 bp->bar0 = NULL;
9219 }
9220}
9221
9222static void bnxt_cleanup_pci(struct bnxt *bp)
9223{
9224 bnxt_unmap_bars(bp, bp->pdev);
9225 pci_release_regions(bp->pdev);
9226 pci_disable_device(bp->pdev);
9227}
9228
Michael Chan18775aa2017-10-26 11:51:27 -04009229static void bnxt_init_dflt_coal(struct bnxt *bp)
9230{
9231 struct bnxt_coal *coal;
9232
9233 /* Tick values in micro seconds.
9234 * 1 coal_buf x bufs_per_record = 1 completion record.
9235 */
9236 coal = &bp->rx_coal;
9237 coal->coal_ticks = 14;
9238 coal->coal_bufs = 30;
9239 coal->coal_ticks_irq = 1;
9240 coal->coal_bufs_irq = 2;
Andy Gospodarek05abe4dd2018-04-26 17:44:38 -04009241 coal->idle_thresh = 50;
Michael Chan18775aa2017-10-26 11:51:27 -04009242 coal->bufs_per_record = 2;
9243 coal->budget = 64; /* NAPI budget */
9244
9245 coal = &bp->tx_coal;
9246 coal->coal_ticks = 28;
9247 coal->coal_bufs = 30;
9248 coal->coal_ticks_irq = 2;
9249 coal->coal_bufs_irq = 2;
9250 coal->bufs_per_record = 1;
9251
9252 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
9253}
9254
Michael Chanc0c050c2015-10-22 16:01:17 -04009255static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
9256{
9257 int rc;
9258 struct bnxt *bp = netdev_priv(dev);
9259
9260 SET_NETDEV_DEV(dev, &pdev->dev);
9261
9262 /* enable device (incl. PCI PM wakeup), and bus-mastering */
9263 rc = pci_enable_device(pdev);
9264 if (rc) {
9265 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
9266 goto init_err;
9267 }
9268
9269 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
9270 dev_err(&pdev->dev,
9271 "Cannot find PCI device base address, aborting\n");
9272 rc = -ENODEV;
9273 goto init_err_disable;
9274 }
9275
9276 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
9277 if (rc) {
9278 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
9279 goto init_err_disable;
9280 }
9281
9282 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
9283 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
9284 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
9285 goto init_err_disable;
9286 }
9287
9288 pci_set_master(pdev);
9289
9290 bp->dev = dev;
9291 bp->pdev = pdev;
9292
9293 bp->bar0 = pci_ioremap_bar(pdev, 0);
9294 if (!bp->bar0) {
9295 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
9296 rc = -ENOMEM;
9297 goto init_err_release;
9298 }
9299
9300 bp->bar1 = pci_ioremap_bar(pdev, 2);
9301 if (!bp->bar1) {
9302 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
9303 rc = -ENOMEM;
9304 goto init_err_release;
9305 }
9306
9307 bp->bar2 = pci_ioremap_bar(pdev, 4);
9308 if (!bp->bar2) {
9309 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
9310 rc = -ENOMEM;
9311 goto init_err_release;
9312 }
9313
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009314 pci_enable_pcie_error_reporting(pdev);
9315
Michael Chanc0c050c2015-10-22 16:01:17 -04009316 INIT_WORK(&bp->sp_task, bnxt_sp_task);
9317
9318 spin_lock_init(&bp->ntp_fltr_lock);
Michael Chan697197e2018-10-14 07:02:46 -04009319#if BITS_PER_LONG == 32
9320 spin_lock_init(&bp->db_lock);
9321#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04009322
9323 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
9324 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
9325
Michael Chan18775aa2017-10-26 11:51:27 -04009326 bnxt_init_dflt_coal(bp);
Michael Chan51f30782016-07-01 18:46:29 -04009327
Kees Cooke99e88a2017-10-16 14:43:17 -07009328 timer_setup(&bp->timer, bnxt_timer, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04009329 bp->current_interval = BNXT_TIMER_INTERVAL;
9330
Michael Chancaefe522015-12-09 19:35:42 -05009331 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04009332 return 0;
9333
9334init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05009335 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04009336 pci_release_regions(pdev);
9337
9338init_err_disable:
9339 pci_disable_device(pdev);
9340
9341init_err:
9342 return rc;
9343}
9344
9345/* rtnl_lock held */
9346static int bnxt_change_mac_addr(struct net_device *dev, void *p)
9347{
9348 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05009349 struct bnxt *bp = netdev_priv(dev);
9350 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04009351
9352 if (!is_valid_ether_addr(addr->sa_data))
9353 return -EADDRNOTAVAIL;
9354
Michael Chanc1a7bdf2017-10-26 11:51:24 -04009355 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
9356 return 0;
9357
Michael Chan28ea3342018-09-14 15:41:29 -04009358 rc = bnxt_approve_mac(bp, addr->sa_data, true);
Michael Chan84c33dd2016-04-11 04:11:13 -04009359 if (rc)
9360 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04009361
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05009362 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9363 if (netif_running(dev)) {
9364 bnxt_close_nic(bp, false, false);
9365 rc = bnxt_open_nic(bp, false, false);
9366 }
9367
9368 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04009369}
9370
9371/* rtnl_lock held */
9372static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
9373{
9374 struct bnxt *bp = netdev_priv(dev);
9375
Michael Chanc0c050c2015-10-22 16:01:17 -04009376 if (netif_running(dev))
9377 bnxt_close_nic(bp, false, false);
9378
9379 dev->mtu = new_mtu;
9380 bnxt_set_ring_params(bp);
9381
9382 if (netif_running(dev))
9383 return bnxt_open_nic(bp, false, false);
9384
9385 return 0;
9386}
9387
Michael Chanc5e3deb2016-12-02 21:17:15 -05009388int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04009389{
9390 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05009391 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05009392 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08009393
Michael Chanc0c050c2015-10-22 16:01:17 -04009394 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05009395 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04009396 tc, bp->max_tc);
9397 return -EINVAL;
9398 }
9399
9400 if (netdev_get_num_tc(dev) == tc)
9401 return 0;
9402
Michael Chan3ffb6a32016-11-11 00:11:42 -05009403 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
9404 sh = true;
9405
Michael Chan98fdbe72017-08-28 13:40:26 -04009406 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
9407 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05009408 if (rc)
9409 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04009410
9411 /* Needs to close the device and do hw resource re-allocations */
9412 if (netif_running(bp->dev))
9413 bnxt_close_nic(bp, true, false);
9414
9415 if (tc) {
9416 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
9417 netdev_set_num_tc(dev, tc);
9418 } else {
9419 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
9420 netdev_reset_tc(dev);
9421 }
Michael Chan87e9b372017-08-23 19:34:03 -04009422 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
Michael Chan3ffb6a32016-11-11 00:11:42 -05009423 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
9424 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04009425
9426 if (netif_running(bp->dev))
9427 return bnxt_open_nic(bp, true, false);
9428
9429 return 0;
9430}
9431
Jiri Pirko9e0fd152017-10-19 15:50:39 +02009432static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
9433 void *cb_priv)
Sathya Perla2ae74082017-08-28 13:40:33 -04009434{
Jiri Pirko9e0fd152017-10-19 15:50:39 +02009435 struct bnxt *bp = cb_priv;
Sathya Perla2ae74082017-08-28 13:40:33 -04009436
Jakub Kicinski312324f2018-01-25 14:00:48 -08009437 if (!bnxt_tc_flower_enabled(bp) ||
9438 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
Sathya Perla2ae74082017-08-28 13:40:33 -04009439 return -EOPNOTSUPP;
9440
Jiri Pirko9e0fd152017-10-19 15:50:39 +02009441 switch (type) {
9442 case TC_SETUP_CLSFLOWER:
9443 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
9444 default:
9445 return -EOPNOTSUPP;
9446 }
9447}
9448
9449static int bnxt_setup_tc_block(struct net_device *dev,
9450 struct tc_block_offload *f)
9451{
9452 struct bnxt *bp = netdev_priv(dev);
9453
9454 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9455 return -EOPNOTSUPP;
9456
9457 switch (f->command) {
9458 case TC_BLOCK_BIND:
9459 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
John Hurley60513bd2018-06-25 14:30:04 -07009460 bp, bp, f->extack);
Jiri Pirko9e0fd152017-10-19 15:50:39 +02009461 case TC_BLOCK_UNBIND:
9462 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
9463 return 0;
9464 default:
9465 return -EOPNOTSUPP;
9466 }
Sathya Perla2ae74082017-08-28 13:40:33 -04009467}
9468
Jiri Pirko2572ac52017-08-07 10:15:17 +02009469static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02009470 void *type_data)
Michael Chanc5e3deb2016-12-02 21:17:15 -05009471{
Sathya Perla2ae74082017-08-28 13:40:33 -04009472 switch (type) {
Jiri Pirko9e0fd152017-10-19 15:50:39 +02009473 case TC_SETUP_BLOCK:
9474 return bnxt_setup_tc_block(dev, type_data);
Nogah Frankel575ed7d2017-11-06 07:23:42 +01009475 case TC_SETUP_QDISC_MQPRIO: {
Sathya Perla2ae74082017-08-28 13:40:33 -04009476 struct tc_mqprio_qopt *mqprio = type_data;
Jiri Pirkode4784c2017-08-07 10:15:32 +02009477
Sathya Perla2ae74082017-08-28 13:40:33 -04009478 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
9479
9480 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
9481 }
9482 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02009483 return -EOPNOTSUPP;
Sathya Perla2ae74082017-08-28 13:40:33 -04009484 }
Michael Chanc5e3deb2016-12-02 21:17:15 -05009485}
9486
Michael Chanc0c050c2015-10-22 16:01:17 -04009487#ifdef CONFIG_RFS_ACCEL
9488static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
9489 struct bnxt_ntuple_filter *f2)
9490{
9491 struct flow_keys *keys1 = &f1->fkeys;
9492 struct flow_keys *keys2 = &f2->fkeys;
9493
9494 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
9495 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
9496 keys1->ports.ports == keys2->ports.ports &&
9497 keys1->basic.ip_proto == keys2->basic.ip_proto &&
9498 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05009499 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04009500 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
9501 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04009502 return true;
9503
9504 return false;
9505}
9506
9507static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
9508 u16 rxq_index, u32 flow_id)
9509{
9510 struct bnxt *bp = netdev_priv(dev);
9511 struct bnxt_ntuple_filter *fltr, *new_fltr;
9512 struct flow_keys *fkeys;
9513 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04009514 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04009515 struct hlist_head *head;
9516
Michael Chana54c4d72016-07-25 12:33:35 -04009517 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
9518 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9519 int off = 0, j;
9520
9521 netif_addr_lock_bh(dev);
9522 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
9523 if (ether_addr_equal(eth->h_dest,
9524 vnic->uc_list + off)) {
9525 l2_idx = j + 1;
9526 break;
9527 }
9528 }
9529 netif_addr_unlock_bh(dev);
9530 if (!l2_idx)
9531 return -EINVAL;
9532 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009533 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
9534 if (!new_fltr)
9535 return -ENOMEM;
9536
9537 fkeys = &new_fltr->fkeys;
9538 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
9539 rc = -EPROTONOSUPPORT;
9540 goto err_free;
9541 }
9542
Michael Chandda0e742016-12-29 12:13:40 -05009543 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
9544 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04009545 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
9546 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
9547 rc = -EPROTONOSUPPORT;
9548 goto err_free;
9549 }
Michael Chandda0e742016-12-29 12:13:40 -05009550 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
9551 bp->hwrm_spec_code < 0x10601) {
9552 rc = -EPROTONOSUPPORT;
9553 goto err_free;
9554 }
Michael Chan61aad722017-02-12 19:18:14 -05009555 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
9556 bp->hwrm_spec_code < 0x10601) {
9557 rc = -EPROTONOSUPPORT;
9558 goto err_free;
9559 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009560
Michael Chana54c4d72016-07-25 12:33:35 -04009561 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04009562 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
9563
9564 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
9565 head = &bp->ntp_fltr_hash_tbl[idx];
9566 rcu_read_lock();
9567 hlist_for_each_entry_rcu(fltr, head, hash) {
9568 if (bnxt_fltr_match(fltr, new_fltr)) {
9569 rcu_read_unlock();
9570 rc = 0;
9571 goto err_free;
9572 }
9573 }
9574 rcu_read_unlock();
9575
9576 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05009577 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
9578 BNXT_NTP_FLTR_MAX_FLTR, 0);
9579 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04009580 spin_unlock_bh(&bp->ntp_fltr_lock);
9581 rc = -ENOMEM;
9582 goto err_free;
9583 }
9584
Michael Chan84e86b92015-11-05 16:25:50 -05009585 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04009586 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04009587 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04009588 new_fltr->rxq = rxq_index;
9589 hlist_add_head_rcu(&new_fltr->hash, head);
9590 bp->ntp_fltr_count++;
9591 spin_unlock_bh(&bp->ntp_fltr_lock);
9592
9593 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04009594 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009595
9596 return new_fltr->sw_id;
9597
9598err_free:
9599 kfree(new_fltr);
9600 return rc;
9601}
9602
9603static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9604{
9605 int i;
9606
9607 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
9608 struct hlist_head *head;
9609 struct hlist_node *tmp;
9610 struct bnxt_ntuple_filter *fltr;
9611 int rc;
9612
9613 head = &bp->ntp_fltr_hash_tbl[i];
9614 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
9615 bool del = false;
9616
9617 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
9618 if (rps_may_expire_flow(bp->dev, fltr->rxq,
9619 fltr->flow_id,
9620 fltr->sw_id)) {
9621 bnxt_hwrm_cfa_ntuple_filter_free(bp,
9622 fltr);
9623 del = true;
9624 }
9625 } else {
9626 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
9627 fltr);
9628 if (rc)
9629 del = true;
9630 else
9631 set_bit(BNXT_FLTR_VALID, &fltr->state);
9632 }
9633
9634 if (del) {
9635 spin_lock_bh(&bp->ntp_fltr_lock);
9636 hlist_del_rcu(&fltr->hash);
9637 bp->ntp_fltr_count--;
9638 spin_unlock_bh(&bp->ntp_fltr_lock);
9639 synchronize_rcu();
9640 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
9641 kfree(fltr);
9642 }
9643 }
9644 }
Jeffrey Huang19241362016-02-26 04:00:00 -05009645 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
9646 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04009647}
9648
9649#else
9650
9651static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9652{
9653}
9654
9655#endif /* CONFIG_RFS_ACCEL */
9656
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009657static void bnxt_udp_tunnel_add(struct net_device *dev,
9658 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04009659{
9660 struct bnxt *bp = netdev_priv(dev);
9661
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009662 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9663 return;
9664
Michael Chanc0c050c2015-10-22 16:01:17 -04009665 if (!netif_running(dev))
9666 return;
9667
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009668 switch (ti->type) {
9669 case UDP_TUNNEL_TYPE_VXLAN:
9670 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
9671 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04009672
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009673 bp->vxlan_port_cnt++;
9674 if (bp->vxlan_port_cnt == 1) {
9675 bp->vxlan_port = ti->port;
9676 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04009677 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009678 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009679 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07009680 case UDP_TUNNEL_TYPE_GENEVE:
9681 if (bp->nge_port_cnt && bp->nge_port != ti->port)
9682 return;
9683
9684 bp->nge_port_cnt++;
9685 if (bp->nge_port_cnt == 1) {
9686 bp->nge_port = ti->port;
9687 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
9688 }
9689 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009690 default:
9691 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04009692 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009693
Michael Chanc213eae2017-10-13 21:09:29 -04009694 bnxt_queue_sp_work(bp);
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009695}
9696
9697static void bnxt_udp_tunnel_del(struct net_device *dev,
9698 struct udp_tunnel_info *ti)
9699{
9700 struct bnxt *bp = netdev_priv(dev);
9701
9702 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9703 return;
9704
9705 if (!netif_running(dev))
9706 return;
9707
9708 switch (ti->type) {
9709 case UDP_TUNNEL_TYPE_VXLAN:
9710 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
9711 return;
9712 bp->vxlan_port_cnt--;
9713
9714 if (bp->vxlan_port_cnt != 0)
9715 return;
9716
9717 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
9718 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07009719 case UDP_TUNNEL_TYPE_GENEVE:
9720 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
9721 return;
9722 bp->nge_port_cnt--;
9723
9724 if (bp->nge_port_cnt != 0)
9725 return;
9726
9727 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
9728 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009729 default:
9730 return;
9731 }
9732
Michael Chanc213eae2017-10-13 21:09:29 -04009733 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009734}
9735
Michael Chan39d8ba22017-07-24 12:34:22 -04009736static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9737 struct net_device *dev, u32 filter_mask,
9738 int nlflags)
9739{
9740 struct bnxt *bp = netdev_priv(dev);
9741
9742 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
9743 nlflags, filter_mask, NULL);
9744}
9745
9746static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00009747 u16 flags, struct netlink_ext_ack *extack)
Michael Chan39d8ba22017-07-24 12:34:22 -04009748{
9749 struct bnxt *bp = netdev_priv(dev);
9750 struct nlattr *attr, *br_spec;
9751 int rem, rc = 0;
9752
9753 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
9754 return -EOPNOTSUPP;
9755
9756 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9757 if (!br_spec)
9758 return -EINVAL;
9759
9760 nla_for_each_nested(attr, br_spec, rem) {
9761 u16 mode;
9762
9763 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9764 continue;
9765
9766 if (nla_len(attr) < sizeof(mode))
9767 return -EINVAL;
9768
9769 mode = nla_get_u16(attr);
9770 if (mode == bp->br_mode)
9771 break;
9772
9773 rc = bnxt_hwrm_set_br_mode(bp, mode);
9774 if (!rc)
9775 bp->br_mode = mode;
9776 break;
9777 }
9778 return rc;
9779}
9780
Sathya Perlac124a622017-07-24 12:34:29 -04009781static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
9782 size_t len)
9783{
9784 struct bnxt *bp = netdev_priv(dev);
9785 int rc;
9786
9787 /* The PF and it's VF-reps only support the switchdev framework */
9788 if (!BNXT_PF(bp))
9789 return -EOPNOTSUPP;
9790
Sathya Perla53f70b82017-07-25 13:28:41 -04009791 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04009792
9793 if (rc >= len)
9794 return -EOPNOTSUPP;
9795 return 0;
9796}
9797
9798int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
9799{
9800 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
9801 return -EOPNOTSUPP;
9802
9803 /* The PF and it's VF-reps only support the switchdev framework */
9804 if (!BNXT_PF(bp))
9805 return -EOPNOTSUPP;
9806
9807 switch (attr->id) {
9808 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Sathya Perladd4ea1d2018-01-17 03:21:16 -05009809 attr->u.ppid.id_len = sizeof(bp->switch_id);
9810 memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
Sathya Perlac124a622017-07-24 12:34:29 -04009811 break;
9812 default:
9813 return -EOPNOTSUPP;
9814 }
9815 return 0;
9816}
9817
9818static int bnxt_swdev_port_attr_get(struct net_device *dev,
9819 struct switchdev_attr *attr)
9820{
9821 return bnxt_port_attr_get(netdev_priv(dev), attr);
9822}
9823
9824static const struct switchdev_ops bnxt_switchdev_ops = {
9825 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
9826};
9827
Michael Chanc0c050c2015-10-22 16:01:17 -04009828static const struct net_device_ops bnxt_netdev_ops = {
9829 .ndo_open = bnxt_open,
9830 .ndo_start_xmit = bnxt_start_xmit,
9831 .ndo_stop = bnxt_close,
9832 .ndo_get_stats64 = bnxt_get_stats64,
9833 .ndo_set_rx_mode = bnxt_set_rx_mode,
9834 .ndo_do_ioctl = bnxt_ioctl,
9835 .ndo_validate_addr = eth_validate_addr,
9836 .ndo_set_mac_address = bnxt_change_mac_addr,
9837 .ndo_change_mtu = bnxt_change_mtu,
9838 .ndo_fix_features = bnxt_fix_features,
9839 .ndo_set_features = bnxt_set_features,
9840 .ndo_tx_timeout = bnxt_tx_timeout,
9841#ifdef CONFIG_BNXT_SRIOV
9842 .ndo_get_vf_config = bnxt_get_vf_config,
9843 .ndo_set_vf_mac = bnxt_set_vf_mac,
9844 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
9845 .ndo_set_vf_rate = bnxt_set_vf_bw,
9846 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
9847 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
Vasundhara Volam746df132018-03-31 13:54:10 -04009848 .ndo_set_vf_trust = bnxt_set_vf_trust,
Michael Chanc0c050c2015-10-22 16:01:17 -04009849#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04009850 .ndo_setup_tc = bnxt_setup_tc,
9851#ifdef CONFIG_RFS_ACCEL
9852 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
9853#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07009854 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
9855 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07009856 .ndo_bpf = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04009857 .ndo_bridge_getlink = bnxt_bridge_getlink,
9858 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04009859 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04009860};
9861
9862static void bnxt_remove_one(struct pci_dev *pdev)
9863{
9864 struct net_device *dev = pci_get_drvdata(pdev);
9865 struct bnxt *bp = netdev_priv(dev);
9866
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009867 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04009868 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04009869 bnxt_dl_unregister(bp);
9870 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009871
Satish Baddipadige6316ea62016-03-07 15:38:48 -05009872 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04009873 unregister_netdev(dev);
Sathya Perla2ae74082017-08-28 13:40:33 -04009874 bnxt_shutdown_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04009875 bnxt_cancel_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009876 bp->sp_event = 0;
9877
Michael Chan78095922016-12-07 00:26:16 -05009878 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05009879 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009880 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04009881 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04009882 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05009883 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05009884 kfree(bp->edev);
9885 bp->edev = NULL;
Michael Chan98f04cf2018-10-14 07:02:43 -04009886 bnxt_free_ctx_mem(bp);
9887 kfree(bp->ctx);
9888 bp->ctx = NULL;
Sathya Perla17086392017-02-20 19:25:18 -05009889 bnxt_cleanup_pci(bp);
Michael Chanfd3ab1c2018-12-16 18:46:30 -05009890 bnxt_free_port_stats(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04009891 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04009892}
9893
9894static int bnxt_probe_phy(struct bnxt *bp)
9895{
9896 int rc = 0;
9897 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04009898
Michael Chan170ce012016-04-05 14:08:57 -04009899 rc = bnxt_hwrm_phy_qcaps(bp);
9900 if (rc) {
9901 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
9902 rc);
9903 return rc;
9904 }
Michael Chane2dc9b62017-10-13 21:09:30 -04009905 mutex_init(&bp->link_lock);
Michael Chan170ce012016-04-05 14:08:57 -04009906
Michael Chanc0c050c2015-10-22 16:01:17 -04009907 rc = bnxt_update_link(bp, false);
9908 if (rc) {
9909 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
9910 rc);
9911 return rc;
9912 }
9913
Michael Chan93ed8112016-06-13 02:25:37 -04009914 /* Older firmware does not have supported_auto_speeds, so assume
9915 * that all supported speeds can be autonegotiated.
9916 */
9917 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
9918 link_info->support_auto_speeds = link_info->support_speeds;
9919
Michael Chanc0c050c2015-10-22 16:01:17 -04009920 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05009921 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04009922 link_info->autoneg = BNXT_AUTONEG_SPEED;
9923 if (bp->hwrm_spec_code >= 0x10201) {
9924 if (link_info->auto_pause_setting &
9925 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
9926 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
9927 } else {
9928 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
9929 }
Michael Chan0d8abf02016-02-10 17:33:47 -05009930 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05009931 } else {
9932 link_info->req_link_speed = link_info->force_link_speed;
9933 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04009934 }
Michael Chanc9ee9512016-04-05 14:08:56 -04009935 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
9936 link_info->req_flow_ctrl =
9937 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
9938 else
9939 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04009940 return rc;
9941}
9942
9943static int bnxt_get_max_irq(struct pci_dev *pdev)
9944{
9945 u16 ctrl;
9946
9947 if (!pdev->msix_cap)
9948 return 1;
9949
9950 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
9951 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
9952}
9953
Michael Chan6e6c5a52016-01-02 23:45:02 -05009954static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
9955 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04009956{
Michael Chan6a4f2942018-01-17 03:21:06 -05009957 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
Michael Chane30fbc32018-12-09 07:01:02 -05009958 int max_ring_grps = 0, max_irq;
Michael Chanc0c050c2015-10-22 16:01:17 -04009959
Michael Chan6a4f2942018-01-17 03:21:06 -05009960 *max_tx = hw_resc->max_tx_rings;
9961 *max_rx = hw_resc->max_rx_rings;
Michael Chane30fbc32018-12-09 07:01:02 -05009962 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
9963 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
9964 bnxt_get_ulp_msix_num(bp),
Vasundhara Volamc027c6b2018-12-16 18:46:21 -05009965 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
Michael Chane30fbc32018-12-09 07:01:02 -05009966 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9967 *max_cp = min_t(int, *max_cp, max_irq);
Michael Chan6a4f2942018-01-17 03:21:06 -05009968 max_ring_grps = hw_resc->max_hw_ring_grps;
Prashant Sreedharan76595192016-07-18 07:15:22 -04009969 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
9970 *max_cp -= 1;
9971 *max_rx -= 2;
9972 }
Michael Chanc0c050c2015-10-22 16:01:17 -04009973 if (bp->flags & BNXT_FLAG_AGG_RINGS)
9974 *max_rx >>= 1;
Michael Chane30fbc32018-12-09 07:01:02 -05009975 if (bp->flags & BNXT_FLAG_CHIP_P5) {
9976 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
9977 /* On P5 chips, max_cp output param should be available NQs */
9978 *max_cp = max_irq;
9979 }
Michael Chanb72d4a62015-12-27 18:19:27 -05009980 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05009981}
9982
9983int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
9984{
9985 int rx, tx, cp;
9986
9987 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
Michael Chan78f058a2018-07-09 02:24:49 -04009988 *max_rx = rx;
9989 *max_tx = tx;
Michael Chan6e6c5a52016-01-02 23:45:02 -05009990 if (!rx || !tx || !cp)
9991 return -ENOMEM;
9992
Michael Chan6e6c5a52016-01-02 23:45:02 -05009993 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
9994}
9995
Michael Chane4060d32016-12-07 00:26:19 -05009996static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
9997 bool shared)
9998{
9999 int rc;
10000
10001 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010002 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
10003 /* Not enough rings, try disabling agg rings. */
10004 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
10005 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chan07f4fde2018-07-09 02:24:48 -040010006 if (rc) {
10007 /* set BNXT_FLAG_AGG_RINGS back for consistency */
10008 bp->flags |= BNXT_FLAG_AGG_RINGS;
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010009 return rc;
Michael Chan07f4fde2018-07-09 02:24:48 -040010010 }
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010011 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
Michael Chan1054aee2017-12-16 03:09:42 -050010012 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10013 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010014 bnxt_set_ring_params(bp);
10015 }
Michael Chane4060d32016-12-07 00:26:19 -050010016
10017 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
10018 int max_cp, max_stat, max_irq;
10019
10020 /* Reserve minimum resources for RoCE */
10021 max_cp = bnxt_get_max_func_cp_rings(bp);
10022 max_stat = bnxt_get_max_func_stat_ctxs(bp);
10023 max_irq = bnxt_get_max_func_irqs(bp);
10024 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
10025 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
10026 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
10027 return 0;
10028
10029 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
10030 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
10031 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
10032 max_cp = min_t(int, max_cp, max_irq);
10033 max_cp = min_t(int, max_cp, max_stat);
10034 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
10035 if (rc)
10036 rc = 0;
10037 }
10038 return rc;
10039}
10040
Michael Chan58ea8012018-01-17 03:21:08 -050010041/* In initial default shared ring setting, each shared ring must have a
10042 * RX/TX ring pair.
10043 */
10044static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
10045{
10046 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
10047 bp->rx_nr_rings = bp->cp_nr_rings;
10048 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
10049 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10050}
10051
Michael Chan702c2212017-05-29 19:06:10 -040010052static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -050010053{
10054 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -050010055
Michael Chan2773dfb2018-04-26 17:44:42 -040010056 if (!bnxt_can_reserve_rings(bp))
10057 return 0;
10058
Michael Chan6e6c5a52016-01-02 23:45:02 -050010059 if (sh)
10060 bp->flags |= BNXT_FLAG_SHARED_RINGS;
10061 dflt_rings = netif_get_num_default_rss_queues();
Michael Chan1d3ef132018-03-31 13:54:07 -040010062 /* Reduce default rings on multi-port cards so that total default
10063 * rings do not exceed CPU count.
10064 */
10065 if (bp->port_count > 1) {
10066 int max_rings =
10067 max_t(int, num_online_cpus() / bp->port_count, 1);
10068
10069 dflt_rings = min_t(int, dflt_rings, max_rings);
10070 }
Michael Chane4060d32016-12-07 00:26:19 -050010071 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -050010072 if (rc)
10073 return rc;
10074 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
10075 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan58ea8012018-01-17 03:21:08 -050010076 if (sh)
10077 bnxt_trim_dflt_sh_rings(bp);
10078 else
10079 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
10080 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
Michael Chan391be5c2016-12-29 12:13:41 -050010081
Michael Chan674f50a2018-01-17 03:21:09 -050010082 rc = __bnxt_reserve_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -050010083 if (rc)
10084 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
Michael Chan58ea8012018-01-17 03:21:08 -050010085 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10086 if (sh)
10087 bnxt_trim_dflt_sh_rings(bp);
Michael Chan391be5c2016-12-29 12:13:41 -050010088
Michael Chan674f50a2018-01-17 03:21:09 -050010089 /* Rings may have been trimmed, re-reserve the trimmed rings. */
10090 if (bnxt_need_reserve_rings(bp)) {
10091 rc = __bnxt_reserve_rings(bp);
10092 if (rc)
10093 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
10094 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10095 }
Prashant Sreedharan76595192016-07-18 07:15:22 -040010096 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10097 bp->rx_nr_rings++;
10098 bp->cp_nr_rings++;
10099 }
Michael Chan6e6c5a52016-01-02 23:45:02 -050010100 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -040010101}
10102
Michael Chan47558ac2018-04-26 17:44:44 -040010103static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
10104{
10105 int rc;
10106
10107 if (bp->tx_nr_rings)
10108 return 0;
10109
Michael Chan6b95c3e2018-09-03 04:23:17 -040010110 bnxt_ulp_irq_stop(bp);
10111 bnxt_clear_int_mode(bp);
Michael Chan47558ac2018-04-26 17:44:44 -040010112 rc = bnxt_set_dflt_rings(bp, true);
10113 if (rc) {
10114 netdev_err(bp->dev, "Not enough rings available.\n");
Michael Chan6b95c3e2018-09-03 04:23:17 -040010115 goto init_dflt_ring_err;
Michael Chan47558ac2018-04-26 17:44:44 -040010116 }
10117 rc = bnxt_init_int_mode(bp);
10118 if (rc)
Michael Chan6b95c3e2018-09-03 04:23:17 -040010119 goto init_dflt_ring_err;
10120
Michael Chan47558ac2018-04-26 17:44:44 -040010121 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10122 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
10123 bp->flags |= BNXT_FLAG_RFS;
10124 bp->dev->features |= NETIF_F_NTUPLE;
10125 }
Michael Chan6b95c3e2018-09-03 04:23:17 -040010126init_dflt_ring_err:
10127 bnxt_ulp_irq_restart(bp, rc);
10128 return rc;
Michael Chan47558ac2018-04-26 17:44:44 -040010129}
10130
Michael Chan80fcaf42018-01-17 03:21:05 -050010131int bnxt_restore_pf_fw_resources(struct bnxt *bp)
Michael Chan7b08f662016-12-07 00:26:18 -050010132{
Michael Chan80fcaf42018-01-17 03:21:05 -050010133 int rc;
10134
Michael Chan7b08f662016-12-07 00:26:18 -050010135 ASSERT_RTNL();
10136 bnxt_hwrm_func_qcaps(bp);
Venkat Duvvuru1a037782018-03-09 23:46:09 -050010137
10138 if (netif_running(bp->dev))
10139 __bnxt_close_nic(bp, true, false);
10140
Michael Chanec86f142018-03-31 13:54:21 -040010141 bnxt_ulp_irq_stop(bp);
Michael Chan80fcaf42018-01-17 03:21:05 -050010142 bnxt_clear_int_mode(bp);
10143 rc = bnxt_init_int_mode(bp);
Michael Chanec86f142018-03-31 13:54:21 -040010144 bnxt_ulp_irq_restart(bp, rc);
Venkat Duvvuru1a037782018-03-09 23:46:09 -050010145
10146 if (netif_running(bp->dev)) {
10147 if (rc)
10148 dev_close(bp->dev);
10149 else
10150 rc = bnxt_open_nic(bp, true, false);
10151 }
10152
Michael Chan80fcaf42018-01-17 03:21:05 -050010153 return rc;
Michael Chan7b08f662016-12-07 00:26:18 -050010154}
10155
Michael Chana22a6ac2017-08-23 19:34:05 -040010156static int bnxt_init_mac_addr(struct bnxt *bp)
10157{
10158 int rc = 0;
10159
10160 if (BNXT_PF(bp)) {
10161 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
10162 } else {
10163#ifdef CONFIG_BNXT_SRIOV
10164 struct bnxt_vf_info *vf = &bp->vf;
Michael Chan28ea3342018-09-14 15:41:29 -040010165 bool strict_approval = true;
Michael Chana22a6ac2017-08-23 19:34:05 -040010166
10167 if (is_valid_ether_addr(vf->mac_addr)) {
Vasundhara Volam91cdda42018-01-17 03:21:14 -050010168 /* overwrite netdev dev_addr with admin VF MAC */
Michael Chana22a6ac2017-08-23 19:34:05 -040010169 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan28ea3342018-09-14 15:41:29 -040010170 /* Older PF driver or firmware may not approve this
10171 * correctly.
10172 */
10173 strict_approval = false;
Michael Chana22a6ac2017-08-23 19:34:05 -040010174 } else {
10175 eth_hw_addr_random(bp->dev);
Michael Chana22a6ac2017-08-23 19:34:05 -040010176 }
Michael Chan28ea3342018-09-14 15:41:29 -040010177 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
Michael Chana22a6ac2017-08-23 19:34:05 -040010178#endif
10179 }
10180 return rc;
10181}
10182
Michael Chanc0c050c2015-10-22 16:01:17 -040010183static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
10184{
10185 static int version_printed;
10186 struct net_device *dev;
10187 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -050010188 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -040010189
Ray Jui4e003382017-02-20 19:25:16 -050010190 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040010191 return -ENODEV;
10192
Michael Chanc0c050c2015-10-22 16:01:17 -040010193 if (version_printed++ == 0)
10194 pr_info("%s", version);
10195
10196 max_irqs = bnxt_get_max_irq(pdev);
10197 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
10198 if (!dev)
10199 return -ENOMEM;
10200
10201 bp = netdev_priv(dev);
Michael Chan9c1fabd2018-10-14 07:02:47 -040010202 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanc0c050c2015-10-22 16:01:17 -040010203
10204 if (bnxt_vf_pciid(ent->driver_data))
10205 bp->flags |= BNXT_FLAG_VF;
10206
Michael Chan2bcfa6f2015-12-27 18:19:24 -050010207 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -040010208 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -040010209
10210 rc = bnxt_init_board(pdev, dev);
10211 if (rc < 0)
10212 goto init_err_free;
10213
10214 dev->netdev_ops = &bnxt_netdev_ops;
10215 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
10216 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -070010217 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -040010218 pci_set_drvdata(pdev, dev);
10219
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -040010220 rc = bnxt_alloc_hwrm_resources(bp);
10221 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -050010222 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -040010223
10224 mutex_init(&bp->hwrm_cmd_lock);
10225 rc = bnxt_hwrm_ver_get(bp);
10226 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -050010227 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -040010228
Michael Chan1dfddc42018-10-14 07:02:39 -040010229 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
10230 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
Deepak Khungare605db82017-05-29 19:06:04 -040010231 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
10232 if (rc)
10233 goto init_err_pci_clean;
10234 }
10235
Michael Chane38287b2018-10-14 07:02:45 -040010236 if (BNXT_CHIP_P5(bp))
10237 bp->flags |= BNXT_FLAG_CHIP_P5;
10238
Michael Chan3c2217a2017-03-08 18:44:32 -050010239 rc = bnxt_hwrm_func_reset(bp);
10240 if (rc)
10241 goto init_err_pci_clean;
10242
Rob Swindell5ac67d82016-09-19 03:58:03 -040010243 bnxt_hwrm_fw_set_time(bp);
10244
Michael Chanc0c050c2015-10-22 16:01:17 -040010245 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10246 NETIF_F_TSO | NETIF_F_TSO6 |
10247 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -070010248 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -070010249 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
10250 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -040010251 NETIF_F_RXCSUM | NETIF_F_GRO;
10252
Michael Chane38287b2018-10-14 07:02:45 -040010253 if (BNXT_SUPPORTS_TPA(bp))
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -040010254 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -040010255
Michael Chanc0c050c2015-10-22 16:01:17 -040010256 dev->hw_enc_features =
10257 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10258 NETIF_F_TSO | NETIF_F_TSO6 |
10259 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -070010260 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -070010261 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -070010262 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
10263 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -040010264 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
10265 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
10266 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
Michael Chane38287b2018-10-14 07:02:45 -040010267 if (BNXT_SUPPORTS_TPA(bp))
Michael Chan1054aee2017-12-16 03:09:42 -050010268 dev->hw_features |= NETIF_F_GRO_HW;
Michael Chanc0c050c2015-10-22 16:01:17 -040010269 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Michael Chan1054aee2017-12-16 03:09:42 -050010270 if (dev->features & NETIF_F_GRO_HW)
10271 dev->features &= ~NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -040010272 dev->priv_flags |= IFF_UNICAST_FLT;
10273
10274#ifdef CONFIG_BNXT_SRIOV
10275 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040010276 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -040010277#endif
Michael Chane38287b2018-10-14 07:02:45 -040010278 if (BNXT_SUPPORTS_TPA(bp)) {
10279 bp->gro_func = bnxt_gro_func_5730x;
10280 if (BNXT_CHIP_P4(bp))
10281 bp->gro_func = bnxt_gro_func_5731x;
10282 }
10283 if (!BNXT_CHIP_P4_PLUS(bp))
Michael Chan434c9752017-05-29 19:06:08 -040010284 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -040010285
Michael Chanc0c050c2015-10-22 16:01:17 -040010286 rc = bnxt_hwrm_func_drv_rgtr(bp);
10287 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -050010288 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -040010289
Michael Chana1653b12016-12-07 00:26:20 -050010290 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
10291 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -050010292 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -050010293
Michael Chana588e452016-12-07 00:26:21 -050010294 bp->ulp_probe = bnxt_ulp_probe;
10295
Michael Chan98f04cf2018-10-14 07:02:43 -040010296 rc = bnxt_hwrm_queue_qportcfg(bp);
10297 if (rc) {
10298 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
10299 rc);
10300 rc = -1;
10301 goto init_err_pci_clean;
10302 }
Michael Chanc0c050c2015-10-22 16:01:17 -040010303 /* Get the MAX capabilities for this function */
10304 rc = bnxt_hwrm_func_qcaps(bp);
10305 if (rc) {
10306 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
10307 rc);
10308 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -050010309 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -040010310 }
Michael Chana22a6ac2017-08-23 19:34:05 -040010311 rc = bnxt_init_mac_addr(bp);
10312 if (rc) {
10313 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
10314 rc = -EADDRNOTAVAIL;
10315 goto init_err_pci_clean;
10316 }
Michael Chanc0c050c2015-10-22 16:01:17 -040010317
Satish Baddipadige567b2ab2016-06-13 02:25:31 -040010318 bnxt_hwrm_func_qcfg(bp);
Michael Chan6ba99032018-11-15 03:25:37 -050010319 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -050010320 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -040010321 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -040010322 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -040010323
Michael Chan7eb9bb32017-10-26 11:51:25 -040010324 /* MTU range: 60 - FW defined max */
10325 dev->min_mtu = ETH_ZLEN;
10326 dev->max_mtu = bp->max_mtu;
10327
Michael Chand5430d32017-08-28 13:40:31 -040010328 rc = bnxt_probe_phy(bp);
10329 if (rc)
10330 goto init_err_pci_clean;
10331
Michael Chanc61fb992017-02-06 16:55:36 -050010332 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -040010333 bnxt_set_tpa_flags(bp);
10334 bnxt_set_ring_params(bp);
Michael Chan702c2212017-05-29 19:06:10 -040010335 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010336 if (rc) {
10337 netdev_err(bp->dev, "Not enough rings available.\n");
10338 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -050010339 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -050010340 }
Michael Chanc0c050c2015-10-22 16:01:17 -040010341
Michael Chan87da7f72016-11-16 21:13:09 -050010342 /* Default RSS hash cfg. */
10343 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
10344 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
10345 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
10346 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chane38287b2018-10-14 07:02:45 -040010347 if (BNXT_CHIP_P4(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -050010348 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
10349 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
10350 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
10351 }
10352
Michael Chan8079e8f2016-12-29 12:13:37 -050010353 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -050010354 dev->hw_features |= NETIF_F_NTUPLE;
10355 if (bnxt_rfs_capable(bp)) {
10356 bp->flags |= BNXT_FLAG_RFS;
10357 dev->features |= NETIF_F_NTUPLE;
10358 }
10359 }
10360
Michael Chanc0c050c2015-10-22 16:01:17 -040010361 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
10362 bp->flags |= BNXT_FLAG_STRIP_VLAN;
10363
Michael Chan78095922016-12-07 00:26:16 -050010364 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -040010365 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -050010366 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -040010367
Michael Chan832aed12018-03-09 23:46:07 -050010368 /* No TC has been set yet and rings may have been trimmed due to
10369 * limited MSIX, so we re-initialize the TX rings per TC.
10370 */
10371 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10372
Michael Chanc1ef1462017-04-04 18:14:07 -040010373 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -040010374 if (bp->flags & BNXT_FLAG_WOL_CAP)
10375 device_set_wakeup_enable(&pdev->dev, bp->wol);
10376 else
10377 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -040010378
Michael Chanc3480a62018-01-17 03:21:15 -050010379 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
10380
Michael Chan74706af2018-10-14 07:02:40 -040010381 bnxt_hwrm_coal_params_qcaps(bp);
10382
Michael Chanc213eae2017-10-13 21:09:29 -040010383 if (BNXT_PF(bp)) {
10384 if (!bnxt_pf_wq) {
10385 bnxt_pf_wq =
10386 create_singlethread_workqueue("bnxt_pf_wq");
10387 if (!bnxt_pf_wq) {
10388 dev_err(&pdev->dev, "Unable to create workqueue.\n");
10389 goto init_err_pci_clean;
10390 }
10391 }
Sathya Perla2ae74082017-08-28 13:40:33 -040010392 bnxt_init_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -040010393 }
Sathya Perla2ae74082017-08-28 13:40:33 -040010394
Michael Chan78095922016-12-07 00:26:16 -050010395 rc = register_netdev(dev);
10396 if (rc)
Sathya Perla2ae74082017-08-28 13:40:33 -040010397 goto init_err_cleanup_tc;
Michael Chan78095922016-12-07 00:26:16 -050010398
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040010399 if (BNXT_PF(bp))
10400 bnxt_dl_register(bp);
10401
Michael Chanc0c050c2015-10-22 16:01:17 -040010402 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
10403 board_info[ent->driver_data].name,
10404 (long)pci_resource_start(pdev, 0), dev->dev_addr);
Bjorn Helgaasaf125b72018-03-30 14:09:54 -050010405 pcie_print_link_status(pdev);
Ajit Khaparde90c4f782016-05-15 03:04:45 -040010406
Michael Chanc0c050c2015-10-22 16:01:17 -040010407 return 0;
10408
Sathya Perla2ae74082017-08-28 13:40:33 -040010409init_err_cleanup_tc:
10410 bnxt_shutdown_tc(bp);
Michael Chan78095922016-12-07 00:26:16 -050010411 bnxt_clear_int_mode(bp);
10412
Sathya Perla17086392017-02-20 19:25:18 -050010413init_err_pci_clean:
Venkat Duvvurua2bf74f2018-10-05 00:26:02 -040010414 bnxt_free_hwrm_resources(bp);
Michael Chan98f04cf2018-10-14 07:02:43 -040010415 bnxt_free_ctx_mem(bp);
10416 kfree(bp->ctx);
10417 bp->ctx = NULL;
Sathya Perla17086392017-02-20 19:25:18 -050010418 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -040010419
10420init_err_free:
10421 free_netdev(dev);
10422 return rc;
10423}
10424
Michael Chand196ece2017-04-04 18:14:08 -040010425static void bnxt_shutdown(struct pci_dev *pdev)
10426{
10427 struct net_device *dev = pci_get_drvdata(pdev);
10428 struct bnxt *bp;
10429
10430 if (!dev)
10431 return;
10432
10433 rtnl_lock();
10434 bp = netdev_priv(dev);
10435 if (!bp)
10436 goto shutdown_exit;
10437
10438 if (netif_running(dev))
10439 dev_close(dev);
10440
Ray Juia7f3f932017-12-01 03:13:02 -050010441 bnxt_ulp_shutdown(bp);
10442
Michael Chand196ece2017-04-04 18:14:08 -040010443 if (system_state == SYSTEM_POWER_OFF) {
10444 bnxt_clear_int_mode(bp);
10445 pci_wake_from_d3(pdev, bp->wol);
10446 pci_set_power_state(pdev, PCI_D3hot);
10447 }
10448
10449shutdown_exit:
10450 rtnl_unlock();
10451}
10452
Michael Chanf65a2042017-04-04 18:14:11 -040010453#ifdef CONFIG_PM_SLEEP
10454static int bnxt_suspend(struct device *device)
10455{
10456 struct pci_dev *pdev = to_pci_dev(device);
10457 struct net_device *dev = pci_get_drvdata(pdev);
10458 struct bnxt *bp = netdev_priv(dev);
10459 int rc = 0;
10460
10461 rtnl_lock();
10462 if (netif_running(dev)) {
10463 netif_device_detach(dev);
10464 rc = bnxt_close(dev);
10465 }
10466 bnxt_hwrm_func_drv_unrgtr(bp);
10467 rtnl_unlock();
10468 return rc;
10469}
10470
10471static int bnxt_resume(struct device *device)
10472{
10473 struct pci_dev *pdev = to_pci_dev(device);
10474 struct net_device *dev = pci_get_drvdata(pdev);
10475 struct bnxt *bp = netdev_priv(dev);
10476 int rc = 0;
10477
10478 rtnl_lock();
10479 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
10480 rc = -ENODEV;
10481 goto resume_exit;
10482 }
10483 rc = bnxt_hwrm_func_reset(bp);
10484 if (rc) {
10485 rc = -EBUSY;
10486 goto resume_exit;
10487 }
10488 bnxt_get_wol_settings(bp);
10489 if (netif_running(dev)) {
10490 rc = bnxt_open(dev);
10491 if (!rc)
10492 netif_device_attach(dev);
10493 }
10494
10495resume_exit:
10496 rtnl_unlock();
10497 return rc;
10498}
10499
10500static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
10501#define BNXT_PM_OPS (&bnxt_pm_ops)
10502
10503#else
10504
10505#define BNXT_PM_OPS NULL
10506
10507#endif /* CONFIG_PM_SLEEP */
10508
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010509/**
10510 * bnxt_io_error_detected - called when PCI error is detected
10511 * @pdev: Pointer to PCI device
10512 * @state: The current pci connection state
10513 *
10514 * This function is called after a PCI bus error affecting
10515 * this device has been detected.
10516 */
10517static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
10518 pci_channel_state_t state)
10519{
10520 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -050010521 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010522
10523 netdev_info(netdev, "PCI I/O error detected\n");
10524
10525 rtnl_lock();
10526 netif_device_detach(netdev);
10527
Michael Chana588e452016-12-07 00:26:21 -050010528 bnxt_ulp_stop(bp);
10529
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010530 if (state == pci_channel_io_perm_failure) {
10531 rtnl_unlock();
10532 return PCI_ERS_RESULT_DISCONNECT;
10533 }
10534
10535 if (netif_running(netdev))
10536 bnxt_close(netdev);
10537
10538 pci_disable_device(pdev);
10539 rtnl_unlock();
10540
10541 /* Request a slot slot reset. */
10542 return PCI_ERS_RESULT_NEED_RESET;
10543}
10544
10545/**
10546 * bnxt_io_slot_reset - called after the pci bus has been reset.
10547 * @pdev: Pointer to PCI device
10548 *
10549 * Restart the card from scratch, as if from a cold-boot.
10550 * At this point, the card has exprienced a hard reset,
10551 * followed by fixups by BIOS, and has its config space
10552 * set up identically to what it was at cold boot.
10553 */
10554static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
10555{
10556 struct net_device *netdev = pci_get_drvdata(pdev);
10557 struct bnxt *bp = netdev_priv(netdev);
10558 int err = 0;
10559 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
10560
10561 netdev_info(bp->dev, "PCI Slot Reset\n");
10562
10563 rtnl_lock();
10564
10565 if (pci_enable_device(pdev)) {
10566 dev_err(&pdev->dev,
10567 "Cannot re-enable PCI device after reset.\n");
10568 } else {
10569 pci_set_master(pdev);
10570
Michael Chanaa8ed022016-12-07 00:26:17 -050010571 err = bnxt_hwrm_func_reset(bp);
10572 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010573 err = bnxt_open(netdev);
10574
Michael Chana588e452016-12-07 00:26:21 -050010575 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010576 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -050010577 bnxt_ulp_start(bp);
10578 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010579 }
10580
10581 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
10582 dev_close(netdev);
10583
10584 rtnl_unlock();
10585
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010586 return PCI_ERS_RESULT_RECOVERED;
10587}
10588
10589/**
10590 * bnxt_io_resume - called when traffic can start flowing again.
10591 * @pdev: Pointer to PCI device
10592 *
10593 * This callback is called when the error recovery driver tells
10594 * us that its OK to resume normal operation.
10595 */
10596static void bnxt_io_resume(struct pci_dev *pdev)
10597{
10598 struct net_device *netdev = pci_get_drvdata(pdev);
10599
10600 rtnl_lock();
10601
10602 netif_device_attach(netdev);
10603
10604 rtnl_unlock();
10605}
10606
10607static const struct pci_error_handlers bnxt_err_handler = {
10608 .error_detected = bnxt_io_error_detected,
10609 .slot_reset = bnxt_io_slot_reset,
10610 .resume = bnxt_io_resume
10611};
10612
Michael Chanc0c050c2015-10-22 16:01:17 -040010613static struct pci_driver bnxt_pci_driver = {
10614 .name = DRV_MODULE_NAME,
10615 .id_table = bnxt_pci_tbl,
10616 .probe = bnxt_init_one,
10617 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -040010618 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -040010619 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -050010620 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -040010621#if defined(CONFIG_BNXT_SRIOV)
10622 .sriov_configure = bnxt_sriov_configure,
10623#endif
10624};
10625
Michael Chanc213eae2017-10-13 21:09:29 -040010626static int __init bnxt_init(void)
10627{
Andy Gospodarekcabfb092018-04-26 17:44:40 -040010628 bnxt_debug_init();
Michael Chanc213eae2017-10-13 21:09:29 -040010629 return pci_register_driver(&bnxt_pci_driver);
10630}
10631
10632static void __exit bnxt_exit(void)
10633{
10634 pci_unregister_driver(&bnxt_pci_driver);
10635 if (bnxt_pf_wq)
10636 destroy_workqueue(bnxt_pf_wq);
Andy Gospodarekcabfb092018-04-26 17:44:40 -040010637 bnxt_debug_exit();
Michael Chanc213eae2017-10-13 21:09:29 -040010638}
10639
10640module_init(bnxt_init);
10641module_exit(bnxt_exit);