blob: d2403f47e102ff5161be315e59b704684c769c4f [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanbac9a7e2017-02-12 19:18:10 -05004 * Copyright (c) 2016-2017 Broadcom Limited
Michael Chanc0c050c2015-10-22 16:01:17 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12
13#include <linux/stringify.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/errno.h>
17#include <linux/ioport.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/interrupt.h>
21#include <linux/pci.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/skbuff.h>
25#include <linux/dma-mapping.h>
26#include <linux/bitops.h>
27#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/delay.h>
30#include <asm/byteorder.h>
31#include <asm/page.h>
32#include <linux/time.h>
33#include <linux/mii.h>
34#include <linux/if.h>
35#include <linux/if_vlan.h>
Michael Chan32e8239c2017-07-24 12:34:21 -040036#include <linux/if_bridge.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040037#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050038#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040039#include <net/ip.h>
40#include <net/tcp.h>
41#include <net/udp.h>
42#include <net/checksum.h>
43#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070044#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040045#include <linux/workqueue.h>
46#include <linux/prefetch.h>
47#include <linux/cache.h>
48#include <linux/log2.h>
49#include <linux/aer.h>
50#include <linux/bitmap.h>
51#include <linux/cpu_rmap.h>
Vasundhara Volam56f0fd82017-08-28 13:40:27 -040052#include <linux/cpumask.h>
Sathya Perla2ae74082017-08-28 13:40:33 -040053#include <net/pkt_cls.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040054
55#include "bnxt_hsi.h"
56#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050057#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040058#include "bnxt_sriov.h"
59#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050060#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050061#include "bnxt_xdp.h"
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040062#include "bnxt_vfr.h"
Sathya Perla2ae74082017-08-28 13:40:33 -040063#include "bnxt_tc.h"
Steve Lin3c467bf2017-10-19 10:45:56 -040064#include "bnxt_devlink.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040065
66#define BNXT_TX_TIMEOUT (5 * HZ)
67
68static const char version[] =
69 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
70
71MODULE_LICENSE("GPL");
72MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
73MODULE_VERSION(DRV_MODULE_VERSION);
74
75#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
76#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
77#define BNXT_RX_COPY_THRESH 256
78
Michael Chan4419dbe2016-02-10 17:33:49 -050079#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040080
81enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050082 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040083 BCM57302,
84 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040085 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040086 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040087 BCM57311,
88 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050089 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040090 BCM57404,
91 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57402_NPAR,
93 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040094 BCM57412,
95 BCM57414,
96 BCM57416,
97 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040098 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040099 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -0400100 BCM57417_SFP,
101 BCM57416_SFP,
102 BCM57404_NPAR,
103 BCM57406_NPAR,
104 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400105 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400106 BCM57414_NPAR,
107 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500108 BCM57452,
109 BCM57454,
Ray Jui4a581392017-08-28 13:40:28 -0400110 BCM58802,
Ray Jui8ed693b2017-10-26 11:51:20 -0400111 BCM58804,
Ray Jui4a581392017-08-28 13:40:28 -0400112 BCM58808,
Michael Chanadbc8302016-09-19 03:58:01 -0400113 NETXTREME_E_VF,
114 NETXTREME_C_VF,
Rob Miller618784e2017-10-26 11:51:21 -0400115 NETXTREME_S_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400116};
117
118/* indexed by enum above */
119static const struct {
120 char *name;
121} board_info[] = {
Scott Branden27573a72017-08-28 13:40:29 -0400122 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
123 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
124 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
126 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
127 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
128 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
129 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
130 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
131 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
132 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
133 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
134 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
135 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
136 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
137 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
138 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
139 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
140 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
141 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
142 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
143 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
144 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
145 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
146 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
147 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
148 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
149 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
150 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
Ray Jui8ed693b2017-10-26 11:51:20 -0400151 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Scott Branden27573a72017-08-28 13:40:29 -0400152 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
153 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
154 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Rob Miller618784e2017-10-26 11:51:21 -0400155 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400156};
157
158static const struct pci_device_id bnxt_pci_tbl[] = {
Ray Jui4a581392017-08-28 13:40:28 -0400159 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanadbc8302016-09-19 03:58:01 -0400160 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500161 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400162 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
163 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400165 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
167 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500168 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400169 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
170 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400173 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
174 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
175 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
176 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400177 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400178 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400179 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
180 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
181 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
182 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
183 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400184 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
185 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400186 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400187 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400188 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400189 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Ray Jui4a581392017-08-28 13:40:28 -0400190 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
Deepak Khungar32b40792017-02-12 19:18:18 -0500191 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
Ray Jui4a581392017-08-28 13:40:28 -0400192 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
Ray Jui8ed693b2017-10-26 11:51:20 -0400193 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400194#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400195 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400197 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
198 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
199 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
200 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
201 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
202 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Rob Miller618784e2017-10-26 11:51:21 -0400203 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400204#endif
205 { 0 }
206};
207
208MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
209
210static const u16 bnxt_vf_req_snif[] = {
211 HWRM_FUNC_CFG,
212 HWRM_PORT_PHY_QCFG,
213 HWRM_CFA_L2_FILTER_ALLOC,
214};
215
Michael Chan25be8622016-04-05 14:09:00 -0400216static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500217 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
218 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
219 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
220 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
221 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400222};
223
Michael Chanc213eae2017-10-13 21:09:29 -0400224static struct workqueue_struct *bnxt_pf_wq;
225
Michael Chanc0c050c2015-10-22 16:01:17 -0400226static bool bnxt_vf_pciid(enum board_idx idx)
227{
Rob Miller618784e2017-10-26 11:51:21 -0400228 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
229 idx == NETXTREME_S_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400230}
231
232#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
233#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
234#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
235
236#define BNXT_CP_DB_REARM(db, raw_cons) \
237 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
238
239#define BNXT_CP_DB(db, raw_cons) \
240 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
241
242#define BNXT_CP_DB_IRQ_DIS(db) \
243 writel(DB_CP_IRQ_DIS_FLAGS, db)
244
Michael Chan38413402017-02-06 16:55:43 -0500245const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400246 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
247 TX_BD_FLAGS_LHINT_512_TO_1023,
248 TX_BD_FLAGS_LHINT_1024_TO_2047,
249 TX_BD_FLAGS_LHINT_1024_TO_2047,
250 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
251 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
252 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
253 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
254 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
255 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
256 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
257 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
258 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
259 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
260 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
261 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
262 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
263 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
264 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
265};
266
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400267static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
268{
269 struct metadata_dst *md_dst = skb_metadata_dst(skb);
270
271 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
272 return 0;
273
274 return md_dst->u.port_info.port_id;
275}
276
Michael Chanc0c050c2015-10-22 16:01:17 -0400277static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
278{
279 struct bnxt *bp = netdev_priv(dev);
280 struct tx_bd *txbd;
281 struct tx_bd_ext *txbd1;
282 struct netdev_queue *txq;
283 int i;
284 dma_addr_t mapping;
285 unsigned int length, pad = 0;
286 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
287 u16 prod, last_frag;
288 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400289 struct bnxt_tx_ring_info *txr;
290 struct bnxt_sw_tx_bd *tx_buf;
291
292 i = skb_get_queue_mapping(skb);
293 if (unlikely(i >= bp->tx_nr_rings)) {
294 dev_kfree_skb_any(skb);
295 return NETDEV_TX_OK;
296 }
297
Michael Chanc0c050c2015-10-22 16:01:17 -0400298 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500299 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400300 prod = txr->tx_prod;
301
302 free_size = bnxt_tx_avail(bp, txr);
303 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
304 netif_tx_stop_queue(txq);
305 return NETDEV_TX_BUSY;
306 }
307
308 length = skb->len;
309 len = skb_headlen(skb);
310 last_frag = skb_shinfo(skb)->nr_frags;
311
312 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
313
314 txbd->tx_bd_opaque = prod;
315
316 tx_buf = &txr->tx_buf_ring[prod];
317 tx_buf->skb = skb;
318 tx_buf->nr_frags = last_frag;
319
320 vlan_tag_flags = 0;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400321 cfa_action = bnxt_xmit_get_cfa_action(skb);
Michael Chanc0c050c2015-10-22 16:01:17 -0400322 if (skb_vlan_tag_present(skb)) {
323 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
324 skb_vlan_tag_get(skb);
325 /* Currently supports 8021Q, 8021AD vlan offloads
326 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
327 */
328 if (skb->vlan_proto == htons(ETH_P_8021Q))
329 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
330 }
331
332 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500333 struct tx_push_buffer *tx_push_buf = txr->tx_push;
334 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
335 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
336 void *pdata = tx_push_buf->data;
337 u64 *end;
338 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400339
340 /* Set COAL_NOW to be ready quickly for the next push */
341 tx_push->tx_bd_len_flags_type =
342 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
343 TX_BD_TYPE_LONG_TX_BD |
344 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
345 TX_BD_FLAGS_COAL_NOW |
346 TX_BD_FLAGS_PACKET_END |
347 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
348
349 if (skb->ip_summed == CHECKSUM_PARTIAL)
350 tx_push1->tx_bd_hsize_lflags =
351 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
352 else
353 tx_push1->tx_bd_hsize_lflags = 0;
354
355 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400356 tx_push1->tx_bd_cfa_action =
357 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400358
Michael Chanfbb0fa82016-02-22 02:10:26 -0500359 end = pdata + length;
360 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500361 *end = 0;
362
Michael Chanc0c050c2015-10-22 16:01:17 -0400363 skb_copy_from_linear_data(skb, pdata, len);
364 pdata += len;
365 for (j = 0; j < last_frag; j++) {
366 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
367 void *fptr;
368
369 fptr = skb_frag_address_safe(frag);
370 if (!fptr)
371 goto normal_tx;
372
373 memcpy(pdata, fptr, skb_frag_size(frag));
374 pdata += skb_frag_size(frag);
375 }
376
Michael Chan4419dbe2016-02-10 17:33:49 -0500377 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
378 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400379 prod = NEXT_TX(prod);
380 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
381 memcpy(txbd, tx_push1, sizeof(*txbd));
382 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500383 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400384 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
385 txr->tx_prod = prod;
386
Michael Chanb9a84602016-06-06 02:37:14 -0400387 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400388 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400389 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400390
Michael Chan4419dbe2016-02-10 17:33:49 -0500391 push_len = (length + sizeof(*tx_push) + 7) / 8;
392 if (push_len > 16) {
393 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400394 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
395 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500396 } else {
397 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
398 push_len);
399 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400400
Michael Chanc0c050c2015-10-22 16:01:17 -0400401 goto tx_done;
402 }
403
404normal_tx:
405 if (length < BNXT_MIN_PKT_SIZE) {
406 pad = BNXT_MIN_PKT_SIZE - length;
407 if (skb_pad(skb, pad)) {
408 /* SKB already freed. */
409 tx_buf->skb = NULL;
410 return NETDEV_TX_OK;
411 }
412 length = BNXT_MIN_PKT_SIZE;
413 }
414
415 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
416
417 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
418 dev_kfree_skb_any(skb);
419 tx_buf->skb = NULL;
420 return NETDEV_TX_OK;
421 }
422
423 dma_unmap_addr_set(tx_buf, mapping, mapping);
424 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
425 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
426
427 txbd->tx_bd_haddr = cpu_to_le64(mapping);
428
429 prod = NEXT_TX(prod);
430 txbd1 = (struct tx_bd_ext *)
431 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
432
433 txbd1->tx_bd_hsize_lflags = 0;
434 if (skb_is_gso(skb)) {
435 u32 hdr_len;
436
437 if (skb->encapsulation)
438 hdr_len = skb_inner_network_offset(skb) +
439 skb_inner_network_header_len(skb) +
440 inner_tcp_hdrlen(skb);
441 else
442 hdr_len = skb_transport_offset(skb) +
443 tcp_hdrlen(skb);
444
445 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
446 TX_BD_FLAGS_T_IPID |
447 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
448 length = skb_shinfo(skb)->gso_size;
449 txbd1->tx_bd_mss = cpu_to_le32(length);
450 length += hdr_len;
451 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
452 txbd1->tx_bd_hsize_lflags =
453 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
454 txbd1->tx_bd_mss = 0;
455 }
456
457 length >>= 9;
458 flags |= bnxt_lhint_arr[length];
459 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
460
461 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
Sathya Perlaee5c7fb2017-07-24 12:34:28 -0400462 txbd1->tx_bd_cfa_action =
463 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
Michael Chanc0c050c2015-10-22 16:01:17 -0400464 for (i = 0; i < last_frag; i++) {
465 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
466
467 prod = NEXT_TX(prod);
468 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
469
470 len = skb_frag_size(frag);
471 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
472 DMA_TO_DEVICE);
473
474 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
475 goto tx_dma_error;
476
477 tx_buf = &txr->tx_buf_ring[prod];
478 dma_unmap_addr_set(tx_buf, mapping, mapping);
479
480 txbd->tx_bd_haddr = cpu_to_le64(mapping);
481
482 flags = len << TX_BD_LEN_SHIFT;
483 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
484 }
485
486 flags &= ~TX_BD_LEN;
487 txbd->tx_bd_len_flags_type =
488 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
489 TX_BD_FLAGS_PACKET_END);
490
491 netdev_tx_sent_queue(txq, skb->len);
492
493 /* Sync BD data before updating doorbell */
494 wmb();
495
496 prod = NEXT_TX(prod);
497 txr->tx_prod = prod;
498
Michael Chanffe40642017-05-30 20:03:00 -0400499 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400500 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400501
502tx_done:
503
504 mmiowb();
505
506 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400507 if (skb->xmit_more && !tx_buf->is_push)
508 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
509
Michael Chanc0c050c2015-10-22 16:01:17 -0400510 netif_tx_stop_queue(txq);
511
512 /* netif_tx_stop_queue() must be done before checking
513 * tx index in bnxt_tx_avail() below, because in
514 * bnxt_tx_int(), we update tx index before checking for
515 * netif_tx_queue_stopped().
516 */
517 smp_mb();
518 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
519 netif_tx_wake_queue(txq);
520 }
521 return NETDEV_TX_OK;
522
523tx_dma_error:
524 last_frag = i;
525
526 /* start back at beginning and unmap skb */
527 prod = txr->tx_prod;
528 tx_buf = &txr->tx_buf_ring[prod];
529 tx_buf->skb = NULL;
530 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
531 skb_headlen(skb), PCI_DMA_TODEVICE);
532 prod = NEXT_TX(prod);
533
534 /* unmap remaining mapped pages */
535 for (i = 0; i < last_frag; i++) {
536 prod = NEXT_TX(prod);
537 tx_buf = &txr->tx_buf_ring[prod];
538 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
539 skb_frag_size(&skb_shinfo(skb)->frags[i]),
540 PCI_DMA_TODEVICE);
541 }
542
543 dev_kfree_skb_any(skb);
544 return NETDEV_TX_OK;
545}
546
547static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
548{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500549 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500550 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400551 u16 cons = txr->tx_cons;
552 struct pci_dev *pdev = bp->pdev;
553 int i;
554 unsigned int tx_bytes = 0;
555
556 for (i = 0; i < nr_pkts; i++) {
557 struct bnxt_sw_tx_bd *tx_buf;
558 struct sk_buff *skb;
559 int j, last;
560
561 tx_buf = &txr->tx_buf_ring[cons];
562 cons = NEXT_TX(cons);
563 skb = tx_buf->skb;
564 tx_buf->skb = NULL;
565
566 if (tx_buf->is_push) {
567 tx_buf->is_push = 0;
568 goto next_tx_int;
569 }
570
571 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
572 skb_headlen(skb), PCI_DMA_TODEVICE);
573 last = tx_buf->nr_frags;
574
575 for (j = 0; j < last; j++) {
576 cons = NEXT_TX(cons);
577 tx_buf = &txr->tx_buf_ring[cons];
578 dma_unmap_page(
579 &pdev->dev,
580 dma_unmap_addr(tx_buf, mapping),
581 skb_frag_size(&skb_shinfo(skb)->frags[j]),
582 PCI_DMA_TODEVICE);
583 }
584
585next_tx_int:
586 cons = NEXT_TX(cons);
587
588 tx_bytes += skb->len;
589 dev_kfree_skb_any(skb);
590 }
591
592 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
593 txr->tx_cons = cons;
594
595 /* Need to make the tx_cons update visible to bnxt_start_xmit()
596 * before checking for netif_tx_queue_stopped(). Without the
597 * memory barrier, there is a small possibility that bnxt_start_xmit()
598 * will miss it and cause the queue to be stopped forever.
599 */
600 smp_mb();
601
602 if (unlikely(netif_tx_queue_stopped(txq)) &&
603 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
604 __netif_tx_lock(txq, smp_processor_id());
605 if (netif_tx_queue_stopped(txq) &&
606 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
607 txr->dev_state != BNXT_DEV_STATE_CLOSING)
608 netif_tx_wake_queue(txq);
609 __netif_tx_unlock(txq);
610 }
611}
612
Michael Chanc61fb992017-02-06 16:55:36 -0500613static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
614 gfp_t gfp)
615{
616 struct device *dev = &bp->pdev->dev;
617 struct page *page;
618
619 page = alloc_page(gfp);
620 if (!page)
621 return NULL;
622
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700623 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
624 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500625 if (dma_mapping_error(dev, *mapping)) {
626 __free_page(page);
627 return NULL;
628 }
629 *mapping += bp->rx_dma_offset;
630 return page;
631}
632
Michael Chanc0c050c2015-10-22 16:01:17 -0400633static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
634 gfp_t gfp)
635{
636 u8 *data;
637 struct pci_dev *pdev = bp->pdev;
638
639 data = kmalloc(bp->rx_buf_size, gfp);
640 if (!data)
641 return NULL;
642
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700643 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
644 bp->rx_buf_use_size, bp->rx_dir,
645 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400646
647 if (dma_mapping_error(&pdev->dev, *mapping)) {
648 kfree(data);
649 data = NULL;
650 }
651 return data;
652}
653
Michael Chan38413402017-02-06 16:55:43 -0500654int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
655 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400656{
657 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
658 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400659 dma_addr_t mapping;
660
Michael Chanc61fb992017-02-06 16:55:36 -0500661 if (BNXT_RX_PAGE_MODE(bp)) {
662 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400663
Michael Chanc61fb992017-02-06 16:55:36 -0500664 if (!page)
665 return -ENOMEM;
666
667 rx_buf->data = page;
668 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
669 } else {
670 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
671
672 if (!data)
673 return -ENOMEM;
674
675 rx_buf->data = data;
676 rx_buf->data_ptr = data + bp->rx_offset;
677 }
Michael Chan11cd1192017-02-06 16:55:33 -0500678 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400679
680 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400681 return 0;
682}
683
Michael Chanc6d30e82017-02-06 16:55:42 -0500684void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400685{
686 u16 prod = rxr->rx_prod;
687 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
688 struct rx_bd *cons_bd, *prod_bd;
689
690 prod_rx_buf = &rxr->rx_buf_ring[prod];
691 cons_rx_buf = &rxr->rx_buf_ring[cons];
692
693 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500694 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400695
Michael Chan11cd1192017-02-06 16:55:33 -0500696 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400697
698 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
699 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
700
701 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
702}
703
704static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
705{
706 u16 next, max = rxr->rx_agg_bmap_size;
707
708 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
709 if (next >= max)
710 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
711 return next;
712}
713
714static inline int bnxt_alloc_rx_page(struct bnxt *bp,
715 struct bnxt_rx_ring_info *rxr,
716 u16 prod, gfp_t gfp)
717{
718 struct rx_bd *rxbd =
719 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
720 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
721 struct pci_dev *pdev = bp->pdev;
722 struct page *page;
723 dma_addr_t mapping;
724 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400725 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400726
Michael Chan89d0a062016-04-25 02:30:51 -0400727 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
728 page = rxr->rx_page;
729 if (!page) {
730 page = alloc_page(gfp);
731 if (!page)
732 return -ENOMEM;
733 rxr->rx_page = page;
734 rxr->rx_page_offset = 0;
735 }
736 offset = rxr->rx_page_offset;
737 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
738 if (rxr->rx_page_offset == PAGE_SIZE)
739 rxr->rx_page = NULL;
740 else
741 get_page(page);
742 } else {
743 page = alloc_page(gfp);
744 if (!page)
745 return -ENOMEM;
746 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400747
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700748 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
749 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
750 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400751 if (dma_mapping_error(&pdev->dev, mapping)) {
752 __free_page(page);
753 return -EIO;
754 }
755
756 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
757 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
758
759 __set_bit(sw_prod, rxr->rx_agg_bmap);
760 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
761 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
762
763 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400764 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400765 rx_agg_buf->mapping = mapping;
766 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
767 rxbd->rx_bd_opaque = sw_prod;
768 return 0;
769}
770
771static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
772 u32 agg_bufs)
773{
774 struct bnxt *bp = bnapi->bp;
775 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500776 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400777 u16 prod = rxr->rx_agg_prod;
778 u16 sw_prod = rxr->rx_sw_agg_prod;
779 u32 i;
780
781 for (i = 0; i < agg_bufs; i++) {
782 u16 cons;
783 struct rx_agg_cmp *agg;
784 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
785 struct rx_bd *prod_bd;
786 struct page *page;
787
788 agg = (struct rx_agg_cmp *)
789 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
790 cons = agg->rx_agg_cmp_opaque;
791 __clear_bit(cons, rxr->rx_agg_bmap);
792
793 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
794 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
795
796 __set_bit(sw_prod, rxr->rx_agg_bmap);
797 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
798 cons_rx_buf = &rxr->rx_agg_ring[cons];
799
800 /* It is possible for sw_prod to be equal to cons, so
801 * set cons_rx_buf->page to NULL first.
802 */
803 page = cons_rx_buf->page;
804 cons_rx_buf->page = NULL;
805 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400806 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400807
808 prod_rx_buf->mapping = cons_rx_buf->mapping;
809
810 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
811
812 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
813 prod_bd->rx_bd_opaque = sw_prod;
814
815 prod = NEXT_RX_AGG(prod);
816 sw_prod = NEXT_RX_AGG(sw_prod);
817 cp_cons = NEXT_CMP(cp_cons);
818 }
819 rxr->rx_agg_prod = prod;
820 rxr->rx_sw_agg_prod = sw_prod;
821}
822
Michael Chanc61fb992017-02-06 16:55:36 -0500823static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
824 struct bnxt_rx_ring_info *rxr,
825 u16 cons, void *data, u8 *data_ptr,
826 dma_addr_t dma_addr,
827 unsigned int offset_and_len)
828{
829 unsigned int payload = offset_and_len >> 16;
830 unsigned int len = offset_and_len & 0xffff;
831 struct skb_frag_struct *frag;
832 struct page *page = data;
833 u16 prod = rxr->rx_prod;
834 struct sk_buff *skb;
835 int off, err;
836
837 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
838 if (unlikely(err)) {
839 bnxt_reuse_rx_data(rxr, cons, data);
840 return NULL;
841 }
842 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700843 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
844 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500845
846 if (unlikely(!payload))
847 payload = eth_get_headlen(data_ptr, len);
848
849 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
850 if (!skb) {
851 __free_page(page);
852 return NULL;
853 }
854
855 off = (void *)data_ptr - page_address(page);
856 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
857 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
858 payload + NET_IP_ALIGN);
859
860 frag = &skb_shinfo(skb)->frags[0];
861 skb_frag_size_sub(frag, payload);
862 frag->page_offset += payload;
863 skb->data_len -= payload;
864 skb->tail += payload;
865
866 return skb;
867}
868
Michael Chanc0c050c2015-10-22 16:01:17 -0400869static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
870 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500871 void *data, u8 *data_ptr,
872 dma_addr_t dma_addr,
873 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400874{
Michael Chan6bb19472017-02-06 16:55:32 -0500875 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400876 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500877 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400878
879 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
880 if (unlikely(err)) {
881 bnxt_reuse_rx_data(rxr, cons, data);
882 return NULL;
883 }
884
885 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700886 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
887 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400888 if (!skb) {
889 kfree(data);
890 return NULL;
891 }
892
Michael Chanb3dba772017-02-06 16:55:35 -0500893 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500894 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400895 return skb;
896}
897
898static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
899 struct sk_buff *skb, u16 cp_cons,
900 u32 agg_bufs)
901{
902 struct pci_dev *pdev = bp->pdev;
903 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500904 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400905 u16 prod = rxr->rx_agg_prod;
906 u32 i;
907
908 for (i = 0; i < agg_bufs; i++) {
909 u16 cons, frag_len;
910 struct rx_agg_cmp *agg;
911 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
912 struct page *page;
913 dma_addr_t mapping;
914
915 agg = (struct rx_agg_cmp *)
916 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
917 cons = agg->rx_agg_cmp_opaque;
918 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
919 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
920
921 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400922 skb_fill_page_desc(skb, i, cons_rx_buf->page,
923 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400924 __clear_bit(cons, rxr->rx_agg_bmap);
925
926 /* It is possible for bnxt_alloc_rx_page() to allocate
927 * a sw_prod index that equals the cons index, so we
928 * need to clear the cons entry now.
929 */
Michael Chan11cd1192017-02-06 16:55:33 -0500930 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400931 page = cons_rx_buf->page;
932 cons_rx_buf->page = NULL;
933
934 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
935 struct skb_shared_info *shinfo;
936 unsigned int nr_frags;
937
938 shinfo = skb_shinfo(skb);
939 nr_frags = --shinfo->nr_frags;
940 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
941
942 dev_kfree_skb(skb);
943
944 cons_rx_buf->page = page;
945
946 /* Update prod since possibly some pages have been
947 * allocated already.
948 */
949 rxr->rx_agg_prod = prod;
950 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
951 return NULL;
952 }
953
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700954 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
955 PCI_DMA_FROMDEVICE,
956 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400957
958 skb->data_len += frag_len;
959 skb->len += frag_len;
960 skb->truesize += PAGE_SIZE;
961
962 prod = NEXT_RX_AGG(prod);
963 cp_cons = NEXT_CMP(cp_cons);
964 }
965 rxr->rx_agg_prod = prod;
966 return skb;
967}
968
969static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
970 u8 agg_bufs, u32 *raw_cons)
971{
972 u16 last;
973 struct rx_agg_cmp *agg;
974
975 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
976 last = RING_CMP(*raw_cons);
977 agg = (struct rx_agg_cmp *)
978 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
979 return RX_AGG_CMP_VALID(agg, *raw_cons);
980}
981
982static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
983 unsigned int len,
984 dma_addr_t mapping)
985{
986 struct bnxt *bp = bnapi->bp;
987 struct pci_dev *pdev = bp->pdev;
988 struct sk_buff *skb;
989
990 skb = napi_alloc_skb(&bnapi->napi, len);
991 if (!skb)
992 return NULL;
993
Michael Chan745fc052017-02-06 16:55:34 -0500994 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
995 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400996
Michael Chan6bb19472017-02-06 16:55:32 -0500997 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
998 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400999
Michael Chan745fc052017-02-06 16:55:34 -05001000 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1001 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001002
1003 skb_put(skb, len);
1004 return skb;
1005}
1006
Michael Chanfa7e2812016-05-10 19:18:00 -04001007static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1008 u32 *raw_cons, void *cmp)
1009{
1010 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1011 struct rx_cmp *rxcmp = cmp;
1012 u32 tmp_raw_cons = *raw_cons;
1013 u8 cmp_type, agg_bufs = 0;
1014
1015 cmp_type = RX_CMP_TYPE(rxcmp);
1016
1017 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1018 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1019 RX_CMP_AGG_BUFS) >>
1020 RX_CMP_AGG_BUFS_SHIFT;
1021 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1022 struct rx_tpa_end_cmp *tpa_end = cmp;
1023
1024 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1025 RX_TPA_END_CMP_AGG_BUFS) >>
1026 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1027 }
1028
1029 if (agg_bufs) {
1030 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1031 return -EBUSY;
1032 }
1033 *raw_cons = tmp_raw_cons;
1034 return 0;
1035}
1036
Michael Chanc213eae2017-10-13 21:09:29 -04001037static void bnxt_queue_sp_work(struct bnxt *bp)
1038{
1039 if (BNXT_PF(bp))
1040 queue_work(bnxt_pf_wq, &bp->sp_task);
1041 else
1042 schedule_work(&bp->sp_task);
1043}
1044
1045static void bnxt_cancel_sp_work(struct bnxt *bp)
1046{
1047 if (BNXT_PF(bp))
1048 flush_workqueue(bnxt_pf_wq);
1049 else
1050 cancel_work_sync(&bp->sp_task);
1051}
1052
Michael Chanfa7e2812016-05-10 19:18:00 -04001053static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1054{
1055 if (!rxr->bnapi->in_reset) {
1056 rxr->bnapi->in_reset = true;
1057 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001058 bnxt_queue_sp_work(bp);
Michael Chanfa7e2812016-05-10 19:18:00 -04001059 }
1060 rxr->rx_next_cons = 0xffff;
1061}
1062
Michael Chanc0c050c2015-10-22 16:01:17 -04001063static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1064 struct rx_tpa_start_cmp *tpa_start,
1065 struct rx_tpa_start_cmp_ext *tpa_start1)
1066{
1067 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1068 u16 cons, prod;
1069 struct bnxt_tpa_info *tpa_info;
1070 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1071 struct rx_bd *prod_bd;
1072 dma_addr_t mapping;
1073
1074 cons = tpa_start->rx_tpa_start_cmp_opaque;
1075 prod = rxr->rx_prod;
1076 cons_rx_buf = &rxr->rx_buf_ring[cons];
1077 prod_rx_buf = &rxr->rx_buf_ring[prod];
1078 tpa_info = &rxr->rx_tpa[agg_id];
1079
Michael Chanfa7e2812016-05-10 19:18:00 -04001080 if (unlikely(cons != rxr->rx_next_cons)) {
1081 bnxt_sched_reset(bp, rxr);
1082 return;
1083 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001084 /* Store cfa_code in tpa_info to use in tpa_end
1085 * completion processing.
1086 */
1087 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
Michael Chanc0c050c2015-10-22 16:01:17 -04001088 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001089 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001090
1091 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001092 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001093
1094 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1095
1096 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1097
1098 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001099 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001100 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001101 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001102
1103 tpa_info->len =
1104 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1105 RX_TPA_START_CMP_LEN_SHIFT;
1106 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1107 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1108
1109 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1110 tpa_info->gso_type = SKB_GSO_TCPV4;
1111 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1112 if (hash_type == 3)
1113 tpa_info->gso_type = SKB_GSO_TCPV6;
1114 tpa_info->rss_hash =
1115 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1116 } else {
1117 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1118 tpa_info->gso_type = 0;
1119 if (netif_msg_rx_err(bp))
1120 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1121 }
1122 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1123 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001124 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001125
1126 rxr->rx_prod = NEXT_RX(prod);
1127 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001128 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001129 cons_rx_buf = &rxr->rx_buf_ring[cons];
1130
1131 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1132 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1133 cons_rx_buf->data = NULL;
1134}
1135
1136static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1137 u16 cp_cons, u32 agg_bufs)
1138{
1139 if (agg_bufs)
1140 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1141}
1142
Michael Chan94758f82016-06-13 02:25:35 -04001143static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1144 int payload_off, int tcp_ts,
1145 struct sk_buff *skb)
1146{
1147#ifdef CONFIG_INET
1148 struct tcphdr *th;
1149 int len, nw_off;
1150 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1151 u32 hdr_info = tpa_info->hdr_info;
1152 bool loopback = false;
1153
1154 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1155 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1156 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1157
1158 /* If the packet is an internal loopback packet, the offsets will
1159 * have an extra 4 bytes.
1160 */
1161 if (inner_mac_off == 4) {
1162 loopback = true;
1163 } else if (inner_mac_off > 4) {
1164 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1165 ETH_HLEN - 2));
1166
1167 /* We only support inner iPv4/ipv6. If we don't see the
1168 * correct protocol ID, it must be a loopback packet where
1169 * the offsets are off by 4.
1170 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001171 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001172 loopback = true;
1173 }
1174 if (loopback) {
1175 /* internal loopback packet, subtract all offsets by 4 */
1176 inner_ip_off -= 4;
1177 inner_mac_off -= 4;
1178 outer_ip_off -= 4;
1179 }
1180
1181 nw_off = inner_ip_off - ETH_HLEN;
1182 skb_set_network_header(skb, nw_off);
1183 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1184 struct ipv6hdr *iph = ipv6_hdr(skb);
1185
1186 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1187 len = skb->len - skb_transport_offset(skb);
1188 th = tcp_hdr(skb);
1189 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1190 } else {
1191 struct iphdr *iph = ip_hdr(skb);
1192
1193 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1194 len = skb->len - skb_transport_offset(skb);
1195 th = tcp_hdr(skb);
1196 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1197 }
1198
1199 if (inner_mac_off) { /* tunnel */
1200 struct udphdr *uh = NULL;
1201 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1202 ETH_HLEN - 2));
1203
1204 if (proto == htons(ETH_P_IP)) {
1205 struct iphdr *iph = (struct iphdr *)skb->data;
1206
1207 if (iph->protocol == IPPROTO_UDP)
1208 uh = (struct udphdr *)(iph + 1);
1209 } else {
1210 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1211
1212 if (iph->nexthdr == IPPROTO_UDP)
1213 uh = (struct udphdr *)(iph + 1);
1214 }
1215 if (uh) {
1216 if (uh->check)
1217 skb_shinfo(skb)->gso_type |=
1218 SKB_GSO_UDP_TUNNEL_CSUM;
1219 else
1220 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1221 }
1222 }
1223#endif
1224 return skb;
1225}
1226
Michael Chanc0c050c2015-10-22 16:01:17 -04001227#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1228#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1229
Michael Chan309369c2016-06-13 02:25:34 -04001230static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1231 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001232 struct sk_buff *skb)
1233{
Michael Chand1611c32015-10-25 22:27:57 -04001234#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001235 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001236 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001237
Michael Chan309369c2016-06-13 02:25:34 -04001238 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001239 tcp_opt_len = 12;
1240
Michael Chanc0c050c2015-10-22 16:01:17 -04001241 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1242 struct iphdr *iph;
1243
1244 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1245 ETH_HLEN;
1246 skb_set_network_header(skb, nw_off);
1247 iph = ip_hdr(skb);
1248 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1249 len = skb->len - skb_transport_offset(skb);
1250 th = tcp_hdr(skb);
1251 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1252 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1253 struct ipv6hdr *iph;
1254
1255 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1256 ETH_HLEN;
1257 skb_set_network_header(skb, nw_off);
1258 iph = ipv6_hdr(skb);
1259 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1260 len = skb->len - skb_transport_offset(skb);
1261 th = tcp_hdr(skb);
1262 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1263 } else {
1264 dev_kfree_skb_any(skb);
1265 return NULL;
1266 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001267
1268 if (nw_off) { /* tunnel */
1269 struct udphdr *uh = NULL;
1270
1271 if (skb->protocol == htons(ETH_P_IP)) {
1272 struct iphdr *iph = (struct iphdr *)skb->data;
1273
1274 if (iph->protocol == IPPROTO_UDP)
1275 uh = (struct udphdr *)(iph + 1);
1276 } else {
1277 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1278
1279 if (iph->nexthdr == IPPROTO_UDP)
1280 uh = (struct udphdr *)(iph + 1);
1281 }
1282 if (uh) {
1283 if (uh->check)
1284 skb_shinfo(skb)->gso_type |=
1285 SKB_GSO_UDP_TUNNEL_CSUM;
1286 else
1287 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1288 }
1289 }
1290#endif
1291 return skb;
1292}
1293
Michael Chan309369c2016-06-13 02:25:34 -04001294static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1295 struct bnxt_tpa_info *tpa_info,
1296 struct rx_tpa_end_cmp *tpa_end,
1297 struct rx_tpa_end_cmp_ext *tpa_end1,
1298 struct sk_buff *skb)
1299{
1300#ifdef CONFIG_INET
1301 int payload_off;
1302 u16 segs;
1303
1304 segs = TPA_END_TPA_SEGS(tpa_end);
1305 if (segs == 1)
1306 return skb;
1307
1308 NAPI_GRO_CB(skb)->count = segs;
1309 skb_shinfo(skb)->gso_size =
1310 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1311 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1312 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1313 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1314 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1315 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001316 if (likely(skb))
1317 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001318#endif
1319 return skb;
1320}
1321
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001322/* Given the cfa_code of a received packet determine which
1323 * netdev (vf-rep or PF) the packet is destined to.
1324 */
1325static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1326{
1327 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1328
1329 /* if vf-rep dev is NULL, the must belongs to the PF */
1330 return dev ? dev : bp->dev;
1331}
1332
Michael Chanc0c050c2015-10-22 16:01:17 -04001333static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1334 struct bnxt_napi *bnapi,
1335 u32 *raw_cons,
1336 struct rx_tpa_end_cmp *tpa_end,
1337 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001338 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001339{
1340 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001341 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001342 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001343 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001344 u16 cp_cons = RING_CMP(*raw_cons);
1345 unsigned int len;
1346 struct bnxt_tpa_info *tpa_info;
1347 dma_addr_t mapping;
1348 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001349 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001350
Michael Chanfa7e2812016-05-10 19:18:00 -04001351 if (unlikely(bnapi->in_reset)) {
1352 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1353
1354 if (rc < 0)
1355 return ERR_PTR(-EBUSY);
1356 return NULL;
1357 }
1358
Michael Chanc0c050c2015-10-22 16:01:17 -04001359 tpa_info = &rxr->rx_tpa[agg_id];
1360 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001361 data_ptr = tpa_info->data_ptr;
1362 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001363 len = tpa_info->len;
1364 mapping = tpa_info->mapping;
1365
1366 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1367 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1368
1369 if (agg_bufs) {
1370 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1371 return ERR_PTR(-EBUSY);
1372
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001373 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001374 cp_cons = NEXT_CMP(cp_cons);
1375 }
1376
Michael Chan69c149e2017-06-23 14:01:00 -04001377 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001378 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001379 if (agg_bufs > MAX_SKB_FRAGS)
1380 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1381 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001382 return NULL;
1383 }
1384
1385 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001386 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001387 if (!skb) {
1388 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1389 return NULL;
1390 }
1391 } else {
1392 u8 *new_data;
1393 dma_addr_t new_mapping;
1394
1395 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1396 if (!new_data) {
1397 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1398 return NULL;
1399 }
1400
1401 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001402 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001403 tpa_info->mapping = new_mapping;
1404
1405 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001406 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1407 bp->rx_buf_use_size, bp->rx_dir,
1408 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001409
1410 if (!skb) {
1411 kfree(data);
1412 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1413 return NULL;
1414 }
Michael Chanb3dba772017-02-06 16:55:35 -05001415 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001416 skb_put(skb, len);
1417 }
1418
1419 if (agg_bufs) {
1420 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1421 if (!skb) {
1422 /* Page reuse already handled by bnxt_rx_pages(). */
1423 return NULL;
1424 }
1425 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001426
1427 skb->protocol =
1428 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001429
1430 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1431 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1432
Michael Chan8852ddb2016-06-06 02:37:16 -04001433 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1434 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001435 u16 vlan_proto = tpa_info->metadata >>
1436 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001437 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001438
Michael Chan8852ddb2016-06-06 02:37:16 -04001439 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001440 }
1441
1442 skb_checksum_none_assert(skb);
1443 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1444 skb->ip_summed = CHECKSUM_UNNECESSARY;
1445 skb->csum_level =
1446 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1447 }
1448
1449 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001450 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001451
1452 return skb;
1453}
1454
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001455static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1456 struct sk_buff *skb)
1457{
1458 if (skb->dev != bp->dev) {
1459 /* this packet belongs to a vf-rep */
1460 bnxt_vf_rep_rx(bp, skb);
1461 return;
1462 }
1463 skb_record_rx_queue(skb, bnapi->index);
1464 napi_gro_receive(&bnapi->napi, skb);
1465}
1466
Michael Chanc0c050c2015-10-22 16:01:17 -04001467/* returns the following:
1468 * 1 - 1 packet successfully received
1469 * 0 - successful TPA_START, packet not completed yet
1470 * -EBUSY - completion ring does not have all the agg buffers yet
1471 * -ENOMEM - packet aborted due to out of memory
1472 * -EIO - packet aborted due to hw error indicated in BD
1473 */
1474static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001475 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001476{
1477 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001478 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001479 struct net_device *dev = bp->dev;
1480 struct rx_cmp *rxcmp;
1481 struct rx_cmp_ext *rxcmp1;
1482 u32 tmp_raw_cons = *raw_cons;
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001483 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001484 struct bnxt_sw_rx_bd *rx_buf;
1485 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001486 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001487 dma_addr_t dma_addr;
1488 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001489 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001490 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001491 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001492
1493 rxcmp = (struct rx_cmp *)
1494 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1495
1496 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1497 cp_cons = RING_CMP(tmp_raw_cons);
1498 rxcmp1 = (struct rx_cmp_ext *)
1499 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1500
1501 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1502 return -EBUSY;
1503
1504 cmp_type = RX_CMP_TYPE(rxcmp);
1505
1506 prod = rxr->rx_prod;
1507
1508 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1509 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1510 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1511
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001512 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001513 goto next_rx_no_prod;
1514
1515 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1516 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1517 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001518 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001519
Tobias Klauser1fac4b22017-09-26 15:12:26 +02001520 if (IS_ERR(skb))
Michael Chanc0c050c2015-10-22 16:01:17 -04001521 return -EBUSY;
1522
1523 rc = -ENOMEM;
1524 if (likely(skb)) {
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001525 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001526 rc = 1;
1527 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001528 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001529 goto next_rx_no_prod;
1530 }
1531
1532 cons = rxcmp->rx_cmp_opaque;
1533 rx_buf = &rxr->rx_buf_ring[cons];
1534 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001535 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001536 if (unlikely(cons != rxr->rx_next_cons)) {
1537 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1538
1539 bnxt_sched_reset(bp, rxr);
1540 return rc1;
1541 }
Michael Chan6bb19472017-02-06 16:55:32 -05001542 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001543
Michael Chanc61fb992017-02-06 16:55:36 -05001544 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1545 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001546
1547 if (agg_bufs) {
1548 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1549 return -EBUSY;
1550
1551 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001552 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001553 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001554 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001555
1556 rx_buf->data = NULL;
1557 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1558 bnxt_reuse_rx_data(rxr, cons, data);
1559 if (agg_bufs)
1560 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1561
1562 rc = -EIO;
1563 goto next_rx;
1564 }
1565
1566 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001567 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001568
Michael Chanc6d30e82017-02-06 16:55:42 -05001569 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1570 rc = 1;
1571 goto next_rx;
1572 }
1573
Michael Chanc0c050c2015-10-22 16:01:17 -04001574 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001575 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001576 bnxt_reuse_rx_data(rxr, cons, data);
1577 if (!skb) {
1578 rc = -ENOMEM;
1579 goto next_rx;
1580 }
1581 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001582 u32 payload;
1583
Michael Chanc6d30e82017-02-06 16:55:42 -05001584 if (rx_buf->data_ptr == data_ptr)
1585 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1586 else
1587 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001588 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001589 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001590 if (!skb) {
1591 rc = -ENOMEM;
1592 goto next_rx;
1593 }
1594 }
1595
1596 if (agg_bufs) {
1597 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1598 if (!skb) {
1599 rc = -ENOMEM;
1600 goto next_rx;
1601 }
1602 }
1603
1604 if (RX_CMP_HASH_VALID(rxcmp)) {
1605 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1606 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1607
1608 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1609 if (hash_type != 1 && hash_type != 3)
1610 type = PKT_HASH_TYPE_L3;
1611 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1612 }
1613
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001614 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1615 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
Michael Chanc0c050c2015-10-22 16:01:17 -04001616
Michael Chan8852ddb2016-06-06 02:37:16 -04001617 if ((rxcmp1->rx_cmp_flags2 &
1618 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1619 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001620 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001621 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001622 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1623
Michael Chan8852ddb2016-06-06 02:37:16 -04001624 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001625 }
1626
1627 skb_checksum_none_assert(skb);
1628 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1629 if (dev->features & NETIF_F_RXCSUM) {
1630 skb->ip_summed = CHECKSUM_UNNECESSARY;
1631 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1632 }
1633 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001634 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1635 if (dev->features & NETIF_F_RXCSUM)
1636 cpr->rx_l4_csum_errors++;
1637 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001638 }
1639
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04001640 bnxt_deliver_skb(bp, bnapi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001641 rc = 1;
1642
1643next_rx:
1644 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001645 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001646
1647next_rx_no_prod:
1648 *raw_cons = tmp_raw_cons;
1649
1650 return rc;
1651}
1652
Michael Chan2270bc52017-06-23 14:01:01 -04001653/* In netpoll mode, if we are using a combined completion ring, we need to
1654 * discard the rx packets and recycle the buffers.
1655 */
1656static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1657 u32 *raw_cons, u8 *event)
1658{
1659 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1660 u32 tmp_raw_cons = *raw_cons;
1661 struct rx_cmp_ext *rxcmp1;
1662 struct rx_cmp *rxcmp;
1663 u16 cp_cons;
1664 u8 cmp_type;
1665
1666 cp_cons = RING_CMP(tmp_raw_cons);
1667 rxcmp = (struct rx_cmp *)
1668 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1669
1670 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1671 cp_cons = RING_CMP(tmp_raw_cons);
1672 rxcmp1 = (struct rx_cmp_ext *)
1673 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1674
1675 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1676 return -EBUSY;
1677
1678 cmp_type = RX_CMP_TYPE(rxcmp);
1679 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1680 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1681 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1682 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1683 struct rx_tpa_end_cmp_ext *tpa_end1;
1684
1685 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1686 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1687 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1688 }
1689 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1690}
1691
Michael Chan4bb13ab2016-04-05 14:09:01 -04001692#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001693 ((data) & \
1694 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001695
Michael Chanc0c050c2015-10-22 16:01:17 -04001696static int bnxt_async_event_process(struct bnxt *bp,
1697 struct hwrm_async_event_cmpl *cmpl)
1698{
1699 u16 event_id = le16_to_cpu(cmpl->event_id);
1700
1701 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1702 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001703 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001704 u32 data1 = le32_to_cpu(cmpl->event_data1);
1705 struct bnxt_link_info *link_info = &bp->link_info;
1706
1707 if (BNXT_VF(bp))
1708 goto async_event_process_exit;
1709 if (data1 & 0x20000) {
1710 u16 fw_speed = link_info->force_link_speed;
1711 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1712
1713 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1714 speed);
1715 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001716 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001717 /* fall thru */
1718 }
Michael Chan87c374d2016-12-02 21:17:16 -05001719 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001720 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001721 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001722 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001723 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001724 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001725 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001726 u32 data1 = le32_to_cpu(cmpl->event_data1);
1727 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1728
1729 if (BNXT_VF(bp))
1730 break;
1731
1732 if (bp->pf.port_id != port_id)
1733 break;
1734
Michael Chan4bb13ab2016-04-05 14:09:01 -04001735 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1736 break;
1737 }
Michael Chan87c374d2016-12-02 21:17:16 -05001738 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001739 if (BNXT_PF(bp))
1740 goto async_event_process_exit;
1741 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1742 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001743 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001744 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001745 }
Michael Chanc213eae2017-10-13 21:09:29 -04001746 bnxt_queue_sp_work(bp);
Jeffrey Huang19241362016-02-26 04:00:00 -05001747async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001748 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001749 return 0;
1750}
1751
1752static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1753{
1754 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1755 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1756 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1757 (struct hwrm_fwd_req_cmpl *)txcmp;
1758
1759 switch (cmpl_type) {
1760 case CMPL_BASE_TYPE_HWRM_DONE:
1761 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1762 if (seq_id == bp->hwrm_intr_seq_id)
1763 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1764 else
1765 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1766 break;
1767
1768 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1769 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1770
1771 if ((vf_id < bp->pf.first_vf_id) ||
1772 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1773 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1774 vf_id);
1775 return -EINVAL;
1776 }
1777
1778 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1779 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04001780 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04001781 break;
1782
1783 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1784 bnxt_async_event_process(bp,
1785 (struct hwrm_async_event_cmpl *)txcmp);
1786
1787 default:
1788 break;
1789 }
1790
1791 return 0;
1792}
1793
1794static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1795{
1796 struct bnxt_napi *bnapi = dev_instance;
1797 struct bnxt *bp = bnapi->bp;
1798 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1799 u32 cons = RING_CMP(cpr->cp_raw_cons);
1800
1801 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1802 napi_schedule(&bnapi->napi);
1803 return IRQ_HANDLED;
1804}
1805
1806static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1807{
1808 u32 raw_cons = cpr->cp_raw_cons;
1809 u16 cons = RING_CMP(raw_cons);
1810 struct tx_cmp *txcmp;
1811
1812 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1813
1814 return TX_CMP_VALID(txcmp, raw_cons);
1815}
1816
Michael Chanc0c050c2015-10-22 16:01:17 -04001817static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1818{
1819 struct bnxt_napi *bnapi = dev_instance;
1820 struct bnxt *bp = bnapi->bp;
1821 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1822 u32 cons = RING_CMP(cpr->cp_raw_cons);
1823 u32 int_status;
1824
1825 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1826
1827 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001828 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001829 /* return if erroneous interrupt */
1830 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1831 return IRQ_NONE;
1832 }
1833
1834 /* disable ring IRQ */
1835 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1836
1837 /* Return here if interrupt is shared and is disabled. */
1838 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1839 return IRQ_HANDLED;
1840
1841 napi_schedule(&bnapi->napi);
1842 return IRQ_HANDLED;
1843}
1844
1845static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1846{
1847 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1848 u32 raw_cons = cpr->cp_raw_cons;
1849 u32 cons;
1850 int tx_pkts = 0;
1851 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001852 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001853 struct tx_cmp *txcmp;
1854
1855 while (1) {
1856 int rc;
1857
1858 cons = RING_CMP(raw_cons);
1859 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1860
1861 if (!TX_CMP_VALID(txcmp, raw_cons))
1862 break;
1863
Michael Chan67a95e22016-05-04 16:56:43 -04001864 /* The valid test of the entry must be done first before
1865 * reading any further.
1866 */
Michael Chanb67daab2016-05-15 03:04:51 -04001867 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001868 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1869 tx_pkts++;
1870 /* return full budget so NAPI will complete. */
1871 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1872 rx_pkts = budget;
1873 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001874 if (likely(budget))
1875 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1876 else
1877 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1878 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001879 if (likely(rc >= 0))
1880 rx_pkts += rc;
Michael Chan903649e2017-08-28 13:40:30 -04001881 /* Increment rx_pkts when rc is -ENOMEM to count towards
1882 * the NAPI budget. Otherwise, we may potentially loop
1883 * here forever if we consistently cannot allocate
1884 * buffers.
1885 */
1886 else if (rc == -ENOMEM)
1887 rx_pkts++;
Michael Chanc0c050c2015-10-22 16:01:17 -04001888 else if (rc == -EBUSY) /* partial completion */
1889 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001890 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1891 CMPL_BASE_TYPE_HWRM_DONE) ||
1892 (TX_CMP_TYPE(txcmp) ==
1893 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1894 (TX_CMP_TYPE(txcmp) ==
1895 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1896 bnxt_hwrm_handler(bp, txcmp);
1897 }
1898 raw_cons = NEXT_RAW_CMP(raw_cons);
1899
1900 if (rx_pkts == budget)
1901 break;
1902 }
1903
Michael Chan38413402017-02-06 16:55:43 -05001904 if (event & BNXT_TX_EVENT) {
1905 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1906 void __iomem *db = txr->tx_doorbell;
1907 u16 prod = txr->tx_prod;
1908
1909 /* Sync BD data before updating doorbell */
1910 wmb();
1911
Michael Chan434c9752017-05-29 19:06:08 -04001912 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001913 }
1914
Michael Chanc0c050c2015-10-22 16:01:17 -04001915 cpr->cp_raw_cons = raw_cons;
1916 /* ACK completion ring before freeing tx ring and producing new
1917 * buffers in rx/agg rings to prevent overflowing the completion
1918 * ring.
1919 */
1920 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1921
1922 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001923 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001924
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001925 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001926 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001927
Michael Chan434c9752017-05-29 19:06:08 -04001928 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1929 if (event & BNXT_AGG_EVENT)
1930 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1931 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001932 }
1933 return rx_pkts;
1934}
1935
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001936static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1937{
1938 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1939 struct bnxt *bp = bnapi->bp;
1940 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1941 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1942 struct tx_cmp *txcmp;
1943 struct rx_cmp_ext *rxcmp1;
1944 u32 cp_cons, tmp_raw_cons;
1945 u32 raw_cons = cpr->cp_raw_cons;
1946 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001947 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001948
1949 while (1) {
1950 int rc;
1951
1952 cp_cons = RING_CMP(raw_cons);
1953 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1954
1955 if (!TX_CMP_VALID(txcmp, raw_cons))
1956 break;
1957
1958 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1959 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1960 cp_cons = RING_CMP(tmp_raw_cons);
1961 rxcmp1 = (struct rx_cmp_ext *)
1962 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1963
1964 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1965 break;
1966
1967 /* force an error to recycle the buffer */
1968 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1969 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1970
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001971 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001972 if (likely(rc == -EIO))
1973 rx_pkts++;
1974 else if (rc == -EBUSY) /* partial completion */
1975 break;
1976 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1977 CMPL_BASE_TYPE_HWRM_DONE)) {
1978 bnxt_hwrm_handler(bp, txcmp);
1979 } else {
1980 netdev_err(bp->dev,
1981 "Invalid completion received on special ring\n");
1982 }
1983 raw_cons = NEXT_RAW_CMP(raw_cons);
1984
1985 if (rx_pkts == budget)
1986 break;
1987 }
1988
1989 cpr->cp_raw_cons = raw_cons;
1990 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001991 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001992
Michael Chan434c9752017-05-29 19:06:08 -04001993 if (event & BNXT_AGG_EVENT)
1994 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1995 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001996
1997 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001998 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001999 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2000 }
2001 return rx_pkts;
2002}
2003
Michael Chanc0c050c2015-10-22 16:01:17 -04002004static int bnxt_poll(struct napi_struct *napi, int budget)
2005{
2006 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2007 struct bnxt *bp = bnapi->bp;
2008 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2009 int work_done = 0;
2010
Michael Chanc0c050c2015-10-22 16:01:17 -04002011 while (1) {
2012 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2013
2014 if (work_done >= budget)
2015 break;
2016
2017 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05002018 if (napi_complete_done(napi, work_done))
2019 BNXT_CP_DB_REARM(cpr->cp_doorbell,
2020 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04002021 break;
2022 }
2023 }
2024 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04002025 return work_done;
2026}
2027
Michael Chanc0c050c2015-10-22 16:01:17 -04002028static void bnxt_free_tx_skbs(struct bnxt *bp)
2029{
2030 int i, max_idx;
2031 struct pci_dev *pdev = bp->pdev;
2032
Michael Chanb6ab4b02016-01-02 23:44:59 -05002033 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002034 return;
2035
2036 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2037 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002038 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002039 int j;
2040
Michael Chanc0c050c2015-10-22 16:01:17 -04002041 for (j = 0; j < max_idx;) {
2042 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2043 struct sk_buff *skb = tx_buf->skb;
2044 int k, last;
2045
2046 if (!skb) {
2047 j++;
2048 continue;
2049 }
2050
2051 tx_buf->skb = NULL;
2052
2053 if (tx_buf->is_push) {
2054 dev_kfree_skb(skb);
2055 j += 2;
2056 continue;
2057 }
2058
2059 dma_unmap_single(&pdev->dev,
2060 dma_unmap_addr(tx_buf, mapping),
2061 skb_headlen(skb),
2062 PCI_DMA_TODEVICE);
2063
2064 last = tx_buf->nr_frags;
2065 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05002066 for (k = 0; k < last; k++, j++) {
2067 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04002068 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2069
Michael Chand612a572016-01-28 03:11:22 -05002070 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04002071 dma_unmap_page(
2072 &pdev->dev,
2073 dma_unmap_addr(tx_buf, mapping),
2074 skb_frag_size(frag), PCI_DMA_TODEVICE);
2075 }
2076 dev_kfree_skb(skb);
2077 }
2078 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2079 }
2080}
2081
2082static void bnxt_free_rx_skbs(struct bnxt *bp)
2083{
2084 int i, max_idx, max_agg_idx;
2085 struct pci_dev *pdev = bp->pdev;
2086
Michael Chanb6ab4b02016-01-02 23:44:59 -05002087 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002088 return;
2089
2090 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2091 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2092 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002093 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002094 int j;
2095
Michael Chanc0c050c2015-10-22 16:01:17 -04002096 if (rxr->rx_tpa) {
2097 for (j = 0; j < MAX_TPA; j++) {
2098 struct bnxt_tpa_info *tpa_info =
2099 &rxr->rx_tpa[j];
2100 u8 *data = tpa_info->data;
2101
2102 if (!data)
2103 continue;
2104
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002105 dma_unmap_single_attrs(&pdev->dev,
2106 tpa_info->mapping,
2107 bp->rx_buf_use_size,
2108 bp->rx_dir,
2109 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002110
2111 tpa_info->data = NULL;
2112
2113 kfree(data);
2114 }
2115 }
2116
2117 for (j = 0; j < max_idx; j++) {
2118 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002119 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002120 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002121
2122 if (!data)
2123 continue;
2124
Michael Chanc0c050c2015-10-22 16:01:17 -04002125 rx_buf->data = NULL;
2126
Michael Chan3ed3a832017-03-28 19:47:31 -04002127 if (BNXT_RX_PAGE_MODE(bp)) {
2128 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002129 dma_unmap_page_attrs(&pdev->dev, mapping,
2130 PAGE_SIZE, bp->rx_dir,
2131 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002132 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002133 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002134 dma_unmap_single_attrs(&pdev->dev, mapping,
2135 bp->rx_buf_use_size,
2136 bp->rx_dir,
2137 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002138 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002139 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002140 }
2141
2142 for (j = 0; j < max_agg_idx; j++) {
2143 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2144 &rxr->rx_agg_ring[j];
2145 struct page *page = rx_agg_buf->page;
2146
2147 if (!page)
2148 continue;
2149
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002150 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2151 BNXT_RX_PAGE_SIZE,
2152 PCI_DMA_FROMDEVICE,
2153 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002154
2155 rx_agg_buf->page = NULL;
2156 __clear_bit(j, rxr->rx_agg_bmap);
2157
2158 __free_page(page);
2159 }
Michael Chan89d0a062016-04-25 02:30:51 -04002160 if (rxr->rx_page) {
2161 __free_page(rxr->rx_page);
2162 rxr->rx_page = NULL;
2163 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002164 }
2165}
2166
2167static void bnxt_free_skbs(struct bnxt *bp)
2168{
2169 bnxt_free_tx_skbs(bp);
2170 bnxt_free_rx_skbs(bp);
2171}
2172
2173static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2174{
2175 struct pci_dev *pdev = bp->pdev;
2176 int i;
2177
2178 for (i = 0; i < ring->nr_pages; i++) {
2179 if (!ring->pg_arr[i])
2180 continue;
2181
2182 dma_free_coherent(&pdev->dev, ring->page_size,
2183 ring->pg_arr[i], ring->dma_arr[i]);
2184
2185 ring->pg_arr[i] = NULL;
2186 }
2187 if (ring->pg_tbl) {
2188 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2189 ring->pg_tbl, ring->pg_tbl_map);
2190 ring->pg_tbl = NULL;
2191 }
2192 if (ring->vmem_size && *ring->vmem) {
2193 vfree(*ring->vmem);
2194 *ring->vmem = NULL;
2195 }
2196}
2197
2198static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2199{
2200 int i;
2201 struct pci_dev *pdev = bp->pdev;
2202
2203 if (ring->nr_pages > 1) {
2204 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2205 ring->nr_pages * 8,
2206 &ring->pg_tbl_map,
2207 GFP_KERNEL);
2208 if (!ring->pg_tbl)
2209 return -ENOMEM;
2210 }
2211
2212 for (i = 0; i < ring->nr_pages; i++) {
2213 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2214 ring->page_size,
2215 &ring->dma_arr[i],
2216 GFP_KERNEL);
2217 if (!ring->pg_arr[i])
2218 return -ENOMEM;
2219
2220 if (ring->nr_pages > 1)
2221 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2222 }
2223
2224 if (ring->vmem_size) {
2225 *ring->vmem = vzalloc(ring->vmem_size);
2226 if (!(*ring->vmem))
2227 return -ENOMEM;
2228 }
2229 return 0;
2230}
2231
2232static void bnxt_free_rx_rings(struct bnxt *bp)
2233{
2234 int i;
2235
Michael Chanb6ab4b02016-01-02 23:44:59 -05002236 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002237 return;
2238
2239 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002240 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002241 struct bnxt_ring_struct *ring;
2242
Michael Chanc6d30e82017-02-06 16:55:42 -05002243 if (rxr->xdp_prog)
2244 bpf_prog_put(rxr->xdp_prog);
2245
Michael Chanc0c050c2015-10-22 16:01:17 -04002246 kfree(rxr->rx_tpa);
2247 rxr->rx_tpa = NULL;
2248
2249 kfree(rxr->rx_agg_bmap);
2250 rxr->rx_agg_bmap = NULL;
2251
2252 ring = &rxr->rx_ring_struct;
2253 bnxt_free_ring(bp, ring);
2254
2255 ring = &rxr->rx_agg_ring_struct;
2256 bnxt_free_ring(bp, ring);
2257 }
2258}
2259
2260static int bnxt_alloc_rx_rings(struct bnxt *bp)
2261{
2262 int i, rc, agg_rings = 0, tpa_rings = 0;
2263
Michael Chanb6ab4b02016-01-02 23:44:59 -05002264 if (!bp->rx_ring)
2265 return -ENOMEM;
2266
Michael Chanc0c050c2015-10-22 16:01:17 -04002267 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2268 agg_rings = 1;
2269
2270 if (bp->flags & BNXT_FLAG_TPA)
2271 tpa_rings = 1;
2272
2273 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002274 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002275 struct bnxt_ring_struct *ring;
2276
Michael Chanc0c050c2015-10-22 16:01:17 -04002277 ring = &rxr->rx_ring_struct;
2278
2279 rc = bnxt_alloc_ring(bp, ring);
2280 if (rc)
2281 return rc;
2282
2283 if (agg_rings) {
2284 u16 mem_size;
2285
2286 ring = &rxr->rx_agg_ring_struct;
2287 rc = bnxt_alloc_ring(bp, ring);
2288 if (rc)
2289 return rc;
2290
2291 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2292 mem_size = rxr->rx_agg_bmap_size / 8;
2293 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2294 if (!rxr->rx_agg_bmap)
2295 return -ENOMEM;
2296
2297 if (tpa_rings) {
2298 rxr->rx_tpa = kcalloc(MAX_TPA,
2299 sizeof(struct bnxt_tpa_info),
2300 GFP_KERNEL);
2301 if (!rxr->rx_tpa)
2302 return -ENOMEM;
2303 }
2304 }
2305 }
2306 return 0;
2307}
2308
2309static void bnxt_free_tx_rings(struct bnxt *bp)
2310{
2311 int i;
2312 struct pci_dev *pdev = bp->pdev;
2313
Michael Chanb6ab4b02016-01-02 23:44:59 -05002314 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002315 return;
2316
2317 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002318 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002319 struct bnxt_ring_struct *ring;
2320
Michael Chanc0c050c2015-10-22 16:01:17 -04002321 if (txr->tx_push) {
2322 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2323 txr->tx_push, txr->tx_push_mapping);
2324 txr->tx_push = NULL;
2325 }
2326
2327 ring = &txr->tx_ring_struct;
2328
2329 bnxt_free_ring(bp, ring);
2330 }
2331}
2332
2333static int bnxt_alloc_tx_rings(struct bnxt *bp)
2334{
2335 int i, j, rc;
2336 struct pci_dev *pdev = bp->pdev;
2337
2338 bp->tx_push_size = 0;
2339 if (bp->tx_push_thresh) {
2340 int push_size;
2341
2342 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2343 bp->tx_push_thresh);
2344
Michael Chan4419dbe2016-02-10 17:33:49 -05002345 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002346 push_size = 0;
2347 bp->tx_push_thresh = 0;
2348 }
2349
2350 bp->tx_push_size = push_size;
2351 }
2352
2353 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002354 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002355 struct bnxt_ring_struct *ring;
2356
Michael Chanc0c050c2015-10-22 16:01:17 -04002357 ring = &txr->tx_ring_struct;
2358
2359 rc = bnxt_alloc_ring(bp, ring);
2360 if (rc)
2361 return rc;
2362
2363 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002364 dma_addr_t mapping;
2365
2366 /* One pre-allocated DMA buffer to backup
2367 * TX push operation
2368 */
2369 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2370 bp->tx_push_size,
2371 &txr->tx_push_mapping,
2372 GFP_KERNEL);
2373
2374 if (!txr->tx_push)
2375 return -ENOMEM;
2376
Michael Chanc0c050c2015-10-22 16:01:17 -04002377 mapping = txr->tx_push_mapping +
2378 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002379 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002380
Michael Chan4419dbe2016-02-10 17:33:49 -05002381 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002382 }
2383 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002384 if (i < bp->tx_nr_rings_xdp)
2385 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002386 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2387 j++;
2388 }
2389 return 0;
2390}
2391
2392static void bnxt_free_cp_rings(struct bnxt *bp)
2393{
2394 int i;
2395
2396 if (!bp->bnapi)
2397 return;
2398
2399 for (i = 0; i < bp->cp_nr_rings; i++) {
2400 struct bnxt_napi *bnapi = bp->bnapi[i];
2401 struct bnxt_cp_ring_info *cpr;
2402 struct bnxt_ring_struct *ring;
2403
2404 if (!bnapi)
2405 continue;
2406
2407 cpr = &bnapi->cp_ring;
2408 ring = &cpr->cp_ring_struct;
2409
2410 bnxt_free_ring(bp, ring);
2411 }
2412}
2413
2414static int bnxt_alloc_cp_rings(struct bnxt *bp)
2415{
2416 int i, rc;
2417
2418 for (i = 0; i < bp->cp_nr_rings; i++) {
2419 struct bnxt_napi *bnapi = bp->bnapi[i];
2420 struct bnxt_cp_ring_info *cpr;
2421 struct bnxt_ring_struct *ring;
2422
2423 if (!bnapi)
2424 continue;
2425
2426 cpr = &bnapi->cp_ring;
2427 ring = &cpr->cp_ring_struct;
2428
2429 rc = bnxt_alloc_ring(bp, ring);
2430 if (rc)
2431 return rc;
2432 }
2433 return 0;
2434}
2435
2436static void bnxt_init_ring_struct(struct bnxt *bp)
2437{
2438 int i;
2439
2440 for (i = 0; i < bp->cp_nr_rings; i++) {
2441 struct bnxt_napi *bnapi = bp->bnapi[i];
2442 struct bnxt_cp_ring_info *cpr;
2443 struct bnxt_rx_ring_info *rxr;
2444 struct bnxt_tx_ring_info *txr;
2445 struct bnxt_ring_struct *ring;
2446
2447 if (!bnapi)
2448 continue;
2449
2450 cpr = &bnapi->cp_ring;
2451 ring = &cpr->cp_ring_struct;
2452 ring->nr_pages = bp->cp_nr_pages;
2453 ring->page_size = HW_CMPD_RING_SIZE;
2454 ring->pg_arr = (void **)cpr->cp_desc_ring;
2455 ring->dma_arr = cpr->cp_desc_mapping;
2456 ring->vmem_size = 0;
2457
Michael Chanb6ab4b02016-01-02 23:44:59 -05002458 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002459 if (!rxr)
2460 goto skip_rx;
2461
Michael Chanc0c050c2015-10-22 16:01:17 -04002462 ring = &rxr->rx_ring_struct;
2463 ring->nr_pages = bp->rx_nr_pages;
2464 ring->page_size = HW_RXBD_RING_SIZE;
2465 ring->pg_arr = (void **)rxr->rx_desc_ring;
2466 ring->dma_arr = rxr->rx_desc_mapping;
2467 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2468 ring->vmem = (void **)&rxr->rx_buf_ring;
2469
2470 ring = &rxr->rx_agg_ring_struct;
2471 ring->nr_pages = bp->rx_agg_nr_pages;
2472 ring->page_size = HW_RXBD_RING_SIZE;
2473 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2474 ring->dma_arr = rxr->rx_agg_desc_mapping;
2475 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2476 ring->vmem = (void **)&rxr->rx_agg_ring;
2477
Michael Chan3b2b7d92016-01-02 23:45:00 -05002478skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002479 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002480 if (!txr)
2481 continue;
2482
Michael Chanc0c050c2015-10-22 16:01:17 -04002483 ring = &txr->tx_ring_struct;
2484 ring->nr_pages = bp->tx_nr_pages;
2485 ring->page_size = HW_RXBD_RING_SIZE;
2486 ring->pg_arr = (void **)txr->tx_desc_ring;
2487 ring->dma_arr = txr->tx_desc_mapping;
2488 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2489 ring->vmem = (void **)&txr->tx_buf_ring;
2490 }
2491}
2492
2493static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2494{
2495 int i;
2496 u32 prod;
2497 struct rx_bd **rx_buf_ring;
2498
2499 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2500 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2501 int j;
2502 struct rx_bd *rxbd;
2503
2504 rxbd = rx_buf_ring[i];
2505 if (!rxbd)
2506 continue;
2507
2508 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2509 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2510 rxbd->rx_bd_opaque = prod;
2511 }
2512 }
2513}
2514
2515static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2516{
2517 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002518 struct bnxt_rx_ring_info *rxr;
2519 struct bnxt_ring_struct *ring;
2520 u32 prod, type;
2521 int i;
2522
Michael Chanc0c050c2015-10-22 16:01:17 -04002523 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2524 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2525
2526 if (NET_IP_ALIGN == 2)
2527 type |= RX_BD_FLAGS_SOP;
2528
Michael Chanb6ab4b02016-01-02 23:44:59 -05002529 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002530 ring = &rxr->rx_ring_struct;
2531 bnxt_init_rxbd_pages(ring, type);
2532
Michael Chanc6d30e82017-02-06 16:55:42 -05002533 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2534 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2535 if (IS_ERR(rxr->xdp_prog)) {
2536 int rc = PTR_ERR(rxr->xdp_prog);
2537
2538 rxr->xdp_prog = NULL;
2539 return rc;
2540 }
2541 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002542 prod = rxr->rx_prod;
2543 for (i = 0; i < bp->rx_ring_size; i++) {
2544 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2545 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2546 ring_nr, i, bp->rx_ring_size);
2547 break;
2548 }
2549 prod = NEXT_RX(prod);
2550 }
2551 rxr->rx_prod = prod;
2552 ring->fw_ring_id = INVALID_HW_RING_ID;
2553
Michael Chanedd0c2c2015-12-27 18:19:19 -05002554 ring = &rxr->rx_agg_ring_struct;
2555 ring->fw_ring_id = INVALID_HW_RING_ID;
2556
Michael Chanc0c050c2015-10-22 16:01:17 -04002557 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2558 return 0;
2559
Michael Chan2839f282016-04-25 02:30:50 -04002560 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002561 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2562
2563 bnxt_init_rxbd_pages(ring, type);
2564
2565 prod = rxr->rx_agg_prod;
2566 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2567 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2568 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2569 ring_nr, i, bp->rx_ring_size);
2570 break;
2571 }
2572 prod = NEXT_RX_AGG(prod);
2573 }
2574 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002575
2576 if (bp->flags & BNXT_FLAG_TPA) {
2577 if (rxr->rx_tpa) {
2578 u8 *data;
2579 dma_addr_t mapping;
2580
2581 for (i = 0; i < MAX_TPA; i++) {
2582 data = __bnxt_alloc_rx_data(bp, &mapping,
2583 GFP_KERNEL);
2584 if (!data)
2585 return -ENOMEM;
2586
2587 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002588 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002589 rxr->rx_tpa[i].mapping = mapping;
2590 }
2591 } else {
2592 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2593 return -ENOMEM;
2594 }
2595 }
2596
2597 return 0;
2598}
2599
Sankar Patchineelam22479252017-03-28 19:47:29 -04002600static void bnxt_init_cp_rings(struct bnxt *bp)
2601{
2602 int i;
2603
2604 for (i = 0; i < bp->cp_nr_rings; i++) {
2605 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2606 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2607
2608 ring->fw_ring_id = INVALID_HW_RING_ID;
2609 }
2610}
2611
Michael Chanc0c050c2015-10-22 16:01:17 -04002612static int bnxt_init_rx_rings(struct bnxt *bp)
2613{
2614 int i, rc = 0;
2615
Michael Chanc61fb992017-02-06 16:55:36 -05002616 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002617 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2618 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002619 } else {
2620 bp->rx_offset = BNXT_RX_OFFSET;
2621 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2622 }
Michael Chanb3dba772017-02-06 16:55:35 -05002623
Michael Chanc0c050c2015-10-22 16:01:17 -04002624 for (i = 0; i < bp->rx_nr_rings; i++) {
2625 rc = bnxt_init_one_rx_ring(bp, i);
2626 if (rc)
2627 break;
2628 }
2629
2630 return rc;
2631}
2632
2633static int bnxt_init_tx_rings(struct bnxt *bp)
2634{
2635 u16 i;
2636
2637 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2638 MAX_SKB_FRAGS + 1);
2639
2640 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002641 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002642 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2643
2644 ring->fw_ring_id = INVALID_HW_RING_ID;
2645 }
2646
2647 return 0;
2648}
2649
2650static void bnxt_free_ring_grps(struct bnxt *bp)
2651{
2652 kfree(bp->grp_info);
2653 bp->grp_info = NULL;
2654}
2655
2656static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2657{
2658 int i;
2659
2660 if (irq_re_init) {
2661 bp->grp_info = kcalloc(bp->cp_nr_rings,
2662 sizeof(struct bnxt_ring_grp_info),
2663 GFP_KERNEL);
2664 if (!bp->grp_info)
2665 return -ENOMEM;
2666 }
2667 for (i = 0; i < bp->cp_nr_rings; i++) {
2668 if (irq_re_init)
2669 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2670 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2671 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2672 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2673 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2674 }
2675 return 0;
2676}
2677
2678static void bnxt_free_vnics(struct bnxt *bp)
2679{
2680 kfree(bp->vnic_info);
2681 bp->vnic_info = NULL;
2682 bp->nr_vnics = 0;
2683}
2684
2685static int bnxt_alloc_vnics(struct bnxt *bp)
2686{
2687 int num_vnics = 1;
2688
2689#ifdef CONFIG_RFS_ACCEL
2690 if (bp->flags & BNXT_FLAG_RFS)
2691 num_vnics += bp->rx_nr_rings;
2692#endif
2693
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002694 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2695 num_vnics++;
2696
Michael Chanc0c050c2015-10-22 16:01:17 -04002697 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2698 GFP_KERNEL);
2699 if (!bp->vnic_info)
2700 return -ENOMEM;
2701
2702 bp->nr_vnics = num_vnics;
2703 return 0;
2704}
2705
2706static void bnxt_init_vnics(struct bnxt *bp)
2707{
2708 int i;
2709
2710 for (i = 0; i < bp->nr_vnics; i++) {
2711 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2712
2713 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002714 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2715 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002716 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2717
2718 if (bp->vnic_info[i].rss_hash_key) {
2719 if (i == 0)
2720 prandom_bytes(vnic->rss_hash_key,
2721 HW_HASH_KEY_SIZE);
2722 else
2723 memcpy(vnic->rss_hash_key,
2724 bp->vnic_info[0].rss_hash_key,
2725 HW_HASH_KEY_SIZE);
2726 }
2727 }
2728}
2729
2730static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2731{
2732 int pages;
2733
2734 pages = ring_size / desc_per_pg;
2735
2736 if (!pages)
2737 return 1;
2738
2739 pages++;
2740
2741 while (pages & (pages - 1))
2742 pages++;
2743
2744 return pages;
2745}
2746
Michael Chanc6d30e82017-02-06 16:55:42 -05002747void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002748{
2749 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002750 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2751 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002752 if (bp->dev->features & NETIF_F_LRO)
2753 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002754 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002755 bp->flags |= BNXT_FLAG_GRO;
2756}
2757
2758/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2759 * be set on entry.
2760 */
2761void bnxt_set_ring_params(struct bnxt *bp)
2762{
2763 u32 ring_size, rx_size, rx_space;
2764 u32 agg_factor = 0, agg_ring_size = 0;
2765
2766 /* 8 for CRC and VLAN */
2767 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2768
2769 rx_space = rx_size + NET_SKB_PAD +
2770 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2771
2772 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2773 ring_size = bp->rx_ring_size;
2774 bp->rx_agg_ring_size = 0;
2775 bp->rx_agg_nr_pages = 0;
2776
2777 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002778 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002779
2780 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002781 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002782 u32 jumbo_factor;
2783
2784 bp->flags |= BNXT_FLAG_JUMBO;
2785 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2786 if (jumbo_factor > agg_factor)
2787 agg_factor = jumbo_factor;
2788 }
2789 agg_ring_size = ring_size * agg_factor;
2790
2791 if (agg_ring_size) {
2792 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2793 RX_DESC_CNT);
2794 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2795 u32 tmp = agg_ring_size;
2796
2797 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2798 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2799 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2800 tmp, agg_ring_size);
2801 }
2802 bp->rx_agg_ring_size = agg_ring_size;
2803 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2804 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2805 rx_space = rx_size + NET_SKB_PAD +
2806 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2807 }
2808
2809 bp->rx_buf_use_size = rx_size;
2810 bp->rx_buf_size = rx_space;
2811
2812 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2813 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2814
2815 ring_size = bp->tx_ring_size;
2816 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2817 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2818
2819 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2820 bp->cp_ring_size = ring_size;
2821
2822 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2823 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2824 bp->cp_nr_pages = MAX_CP_PAGES;
2825 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2826 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2827 ring_size, bp->cp_ring_size);
2828 }
2829 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2830 bp->cp_ring_mask = bp->cp_bit - 1;
2831}
2832
Michael Chanc61fb992017-02-06 16:55:36 -05002833int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002834{
Michael Chanc61fb992017-02-06 16:55:36 -05002835 if (page_mode) {
2836 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2837 return -EOPNOTSUPP;
2838 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2839 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2840 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2841 bp->dev->hw_features &= ~NETIF_F_LRO;
2842 bp->dev->features &= ~NETIF_F_LRO;
2843 bp->rx_dir = DMA_BIDIRECTIONAL;
2844 bp->rx_skb_func = bnxt_rx_page_skb;
2845 } else {
2846 bp->dev->max_mtu = BNXT_MAX_MTU;
2847 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2848 bp->rx_dir = DMA_FROM_DEVICE;
2849 bp->rx_skb_func = bnxt_rx_skb;
2850 }
Michael Chan6bb19472017-02-06 16:55:32 -05002851 return 0;
2852}
2853
Michael Chanc0c050c2015-10-22 16:01:17 -04002854static void bnxt_free_vnic_attributes(struct bnxt *bp)
2855{
2856 int i;
2857 struct bnxt_vnic_info *vnic;
2858 struct pci_dev *pdev = bp->pdev;
2859
2860 if (!bp->vnic_info)
2861 return;
2862
2863 for (i = 0; i < bp->nr_vnics; i++) {
2864 vnic = &bp->vnic_info[i];
2865
2866 kfree(vnic->fw_grp_ids);
2867 vnic->fw_grp_ids = NULL;
2868
2869 kfree(vnic->uc_list);
2870 vnic->uc_list = NULL;
2871
2872 if (vnic->mc_list) {
2873 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2874 vnic->mc_list, vnic->mc_list_mapping);
2875 vnic->mc_list = NULL;
2876 }
2877
2878 if (vnic->rss_table) {
2879 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2880 vnic->rss_table,
2881 vnic->rss_table_dma_addr);
2882 vnic->rss_table = NULL;
2883 }
2884
2885 vnic->rss_hash_key = NULL;
2886 vnic->flags = 0;
2887 }
2888}
2889
2890static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2891{
2892 int i, rc = 0, size;
2893 struct bnxt_vnic_info *vnic;
2894 struct pci_dev *pdev = bp->pdev;
2895 int max_rings;
2896
2897 for (i = 0; i < bp->nr_vnics; i++) {
2898 vnic = &bp->vnic_info[i];
2899
2900 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2901 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2902
2903 if (mem_size > 0) {
2904 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2905 if (!vnic->uc_list) {
2906 rc = -ENOMEM;
2907 goto out;
2908 }
2909 }
2910 }
2911
2912 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2913 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2914 vnic->mc_list =
2915 dma_alloc_coherent(&pdev->dev,
2916 vnic->mc_list_size,
2917 &vnic->mc_list_mapping,
2918 GFP_KERNEL);
2919 if (!vnic->mc_list) {
2920 rc = -ENOMEM;
2921 goto out;
2922 }
2923 }
2924
2925 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2926 max_rings = bp->rx_nr_rings;
2927 else
2928 max_rings = 1;
2929
2930 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2931 if (!vnic->fw_grp_ids) {
2932 rc = -ENOMEM;
2933 goto out;
2934 }
2935
Michael Chanae10ae72016-12-29 12:13:38 -05002936 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2937 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2938 continue;
2939
Michael Chanc0c050c2015-10-22 16:01:17 -04002940 /* Allocate rss table and hash key */
2941 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2942 &vnic->rss_table_dma_addr,
2943 GFP_KERNEL);
2944 if (!vnic->rss_table) {
2945 rc = -ENOMEM;
2946 goto out;
2947 }
2948
2949 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2950
2951 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2952 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2953 }
2954 return 0;
2955
2956out:
2957 return rc;
2958}
2959
2960static void bnxt_free_hwrm_resources(struct bnxt *bp)
2961{
2962 struct pci_dev *pdev = bp->pdev;
2963
2964 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2965 bp->hwrm_cmd_resp_dma_addr);
2966
2967 bp->hwrm_cmd_resp_addr = NULL;
2968 if (bp->hwrm_dbg_resp_addr) {
2969 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2970 bp->hwrm_dbg_resp_addr,
2971 bp->hwrm_dbg_resp_dma_addr);
2972
2973 bp->hwrm_dbg_resp_addr = NULL;
2974 }
2975}
2976
2977static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2978{
2979 struct pci_dev *pdev = bp->pdev;
2980
2981 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2982 &bp->hwrm_cmd_resp_dma_addr,
2983 GFP_KERNEL);
2984 if (!bp->hwrm_cmd_resp_addr)
2985 return -ENOMEM;
2986 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2987 HWRM_DBG_REG_BUF_SIZE,
2988 &bp->hwrm_dbg_resp_dma_addr,
2989 GFP_KERNEL);
2990 if (!bp->hwrm_dbg_resp_addr)
2991 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2992
2993 return 0;
2994}
2995
Deepak Khungare605db82017-05-29 19:06:04 -04002996static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2997{
2998 if (bp->hwrm_short_cmd_req_addr) {
2999 struct pci_dev *pdev = bp->pdev;
3000
3001 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3002 bp->hwrm_short_cmd_req_addr,
3003 bp->hwrm_short_cmd_req_dma_addr);
3004 bp->hwrm_short_cmd_req_addr = NULL;
3005 }
3006}
3007
3008static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3009{
3010 struct pci_dev *pdev = bp->pdev;
3011
3012 bp->hwrm_short_cmd_req_addr =
3013 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3014 &bp->hwrm_short_cmd_req_dma_addr,
3015 GFP_KERNEL);
3016 if (!bp->hwrm_short_cmd_req_addr)
3017 return -ENOMEM;
3018
3019 return 0;
3020}
3021
Michael Chanc0c050c2015-10-22 16:01:17 -04003022static void bnxt_free_stats(struct bnxt *bp)
3023{
3024 u32 size, i;
3025 struct pci_dev *pdev = bp->pdev;
3026
Michael Chan3bdf56c2016-03-07 15:38:45 -05003027 if (bp->hw_rx_port_stats) {
3028 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3029 bp->hw_rx_port_stats,
3030 bp->hw_rx_port_stats_map);
3031 bp->hw_rx_port_stats = NULL;
3032 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3033 }
3034
Michael Chanc0c050c2015-10-22 16:01:17 -04003035 if (!bp->bnapi)
3036 return;
3037
3038 size = sizeof(struct ctx_hw_stats);
3039
3040 for (i = 0; i < bp->cp_nr_rings; i++) {
3041 struct bnxt_napi *bnapi = bp->bnapi[i];
3042 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3043
3044 if (cpr->hw_stats) {
3045 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3046 cpr->hw_stats_map);
3047 cpr->hw_stats = NULL;
3048 }
3049 }
3050}
3051
3052static int bnxt_alloc_stats(struct bnxt *bp)
3053{
3054 u32 size, i;
3055 struct pci_dev *pdev = bp->pdev;
3056
3057 size = sizeof(struct ctx_hw_stats);
3058
3059 for (i = 0; i < bp->cp_nr_rings; i++) {
3060 struct bnxt_napi *bnapi = bp->bnapi[i];
3061 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3062
3063 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3064 &cpr->hw_stats_map,
3065 GFP_KERNEL);
3066 if (!cpr->hw_stats)
3067 return -ENOMEM;
3068
3069 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3070 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05003071
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04003072 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05003073 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3074 sizeof(struct tx_port_stats) + 1024;
3075
3076 bp->hw_rx_port_stats =
3077 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3078 &bp->hw_rx_port_stats_map,
3079 GFP_KERNEL);
3080 if (!bp->hw_rx_port_stats)
3081 return -ENOMEM;
3082
3083 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3084 512;
3085 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3086 sizeof(struct rx_port_stats) + 512;
3087 bp->flags |= BNXT_FLAG_PORT_STATS;
3088 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003089 return 0;
3090}
3091
3092static void bnxt_clear_ring_indices(struct bnxt *bp)
3093{
3094 int i;
3095
3096 if (!bp->bnapi)
3097 return;
3098
3099 for (i = 0; i < bp->cp_nr_rings; i++) {
3100 struct bnxt_napi *bnapi = bp->bnapi[i];
3101 struct bnxt_cp_ring_info *cpr;
3102 struct bnxt_rx_ring_info *rxr;
3103 struct bnxt_tx_ring_info *txr;
3104
3105 if (!bnapi)
3106 continue;
3107
3108 cpr = &bnapi->cp_ring;
3109 cpr->cp_raw_cons = 0;
3110
Michael Chanb6ab4b02016-01-02 23:44:59 -05003111 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003112 if (txr) {
3113 txr->tx_prod = 0;
3114 txr->tx_cons = 0;
3115 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003116
Michael Chanb6ab4b02016-01-02 23:44:59 -05003117 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003118 if (rxr) {
3119 rxr->rx_prod = 0;
3120 rxr->rx_agg_prod = 0;
3121 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003122 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003123 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003124 }
3125}
3126
3127static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3128{
3129#ifdef CONFIG_RFS_ACCEL
3130 int i;
3131
3132 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3133 * safe to delete the hash table.
3134 */
3135 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3136 struct hlist_head *head;
3137 struct hlist_node *tmp;
3138 struct bnxt_ntuple_filter *fltr;
3139
3140 head = &bp->ntp_fltr_hash_tbl[i];
3141 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3142 hlist_del(&fltr->hash);
3143 kfree(fltr);
3144 }
3145 }
3146 if (irq_reinit) {
3147 kfree(bp->ntp_fltr_bmap);
3148 bp->ntp_fltr_bmap = NULL;
3149 }
3150 bp->ntp_fltr_count = 0;
3151#endif
3152}
3153
3154static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3155{
3156#ifdef CONFIG_RFS_ACCEL
3157 int i, rc = 0;
3158
3159 if (!(bp->flags & BNXT_FLAG_RFS))
3160 return 0;
3161
3162 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3163 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3164
3165 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003166 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3167 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003168 GFP_KERNEL);
3169
3170 if (!bp->ntp_fltr_bmap)
3171 rc = -ENOMEM;
3172
3173 return rc;
3174#else
3175 return 0;
3176#endif
3177}
3178
3179static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3180{
3181 bnxt_free_vnic_attributes(bp);
3182 bnxt_free_tx_rings(bp);
3183 bnxt_free_rx_rings(bp);
3184 bnxt_free_cp_rings(bp);
3185 bnxt_free_ntp_fltrs(bp, irq_re_init);
3186 if (irq_re_init) {
3187 bnxt_free_stats(bp);
3188 bnxt_free_ring_grps(bp);
3189 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003190 kfree(bp->tx_ring_map);
3191 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003192 kfree(bp->tx_ring);
3193 bp->tx_ring = NULL;
3194 kfree(bp->rx_ring);
3195 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003196 kfree(bp->bnapi);
3197 bp->bnapi = NULL;
3198 } else {
3199 bnxt_clear_ring_indices(bp);
3200 }
3201}
3202
3203static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3204{
Michael Chan01657bc2016-01-02 23:45:03 -05003205 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003206 void *bnapi;
3207
3208 if (irq_re_init) {
3209 /* Allocate bnapi mem pointer array and mem block for
3210 * all queues
3211 */
3212 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3213 bp->cp_nr_rings);
3214 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3215 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3216 if (!bnapi)
3217 return -ENOMEM;
3218
3219 bp->bnapi = bnapi;
3220 bnapi += arr_size;
3221 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3222 bp->bnapi[i] = bnapi;
3223 bp->bnapi[i]->index = i;
3224 bp->bnapi[i]->bp = bp;
3225 }
3226
Michael Chanb6ab4b02016-01-02 23:44:59 -05003227 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3228 sizeof(struct bnxt_rx_ring_info),
3229 GFP_KERNEL);
3230 if (!bp->rx_ring)
3231 return -ENOMEM;
3232
3233 for (i = 0; i < bp->rx_nr_rings; i++) {
3234 bp->rx_ring[i].bnapi = bp->bnapi[i];
3235 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3236 }
3237
3238 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3239 sizeof(struct bnxt_tx_ring_info),
3240 GFP_KERNEL);
3241 if (!bp->tx_ring)
3242 return -ENOMEM;
3243
Michael Chana960dec2017-02-06 16:55:39 -05003244 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3245 GFP_KERNEL);
3246
3247 if (!bp->tx_ring_map)
3248 return -ENOMEM;
3249
Michael Chan01657bc2016-01-02 23:45:03 -05003250 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3251 j = 0;
3252 else
3253 j = bp->rx_nr_rings;
3254
3255 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3256 bp->tx_ring[i].bnapi = bp->bnapi[j];
3257 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003258 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003259 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003260 bp->tx_ring[i].txq_index = i -
3261 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003262 bp->bnapi[j]->tx_int = bnxt_tx_int;
3263 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003264 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003265 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3266 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003267 }
3268
Michael Chanc0c050c2015-10-22 16:01:17 -04003269 rc = bnxt_alloc_stats(bp);
3270 if (rc)
3271 goto alloc_mem_err;
3272
3273 rc = bnxt_alloc_ntp_fltrs(bp);
3274 if (rc)
3275 goto alloc_mem_err;
3276
3277 rc = bnxt_alloc_vnics(bp);
3278 if (rc)
3279 goto alloc_mem_err;
3280 }
3281
3282 bnxt_init_ring_struct(bp);
3283
3284 rc = bnxt_alloc_rx_rings(bp);
3285 if (rc)
3286 goto alloc_mem_err;
3287
3288 rc = bnxt_alloc_tx_rings(bp);
3289 if (rc)
3290 goto alloc_mem_err;
3291
3292 rc = bnxt_alloc_cp_rings(bp);
3293 if (rc)
3294 goto alloc_mem_err;
3295
3296 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3297 BNXT_VNIC_UCAST_FLAG;
3298 rc = bnxt_alloc_vnic_attributes(bp);
3299 if (rc)
3300 goto alloc_mem_err;
3301 return 0;
3302
3303alloc_mem_err:
3304 bnxt_free_mem(bp, true);
3305 return rc;
3306}
3307
Michael Chan9d8bc092016-12-29 12:13:33 -05003308static void bnxt_disable_int(struct bnxt *bp)
3309{
3310 int i;
3311
3312 if (!bp->bnapi)
3313 return;
3314
3315 for (i = 0; i < bp->cp_nr_rings; i++) {
3316 struct bnxt_napi *bnapi = bp->bnapi[i];
3317 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003318 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003319
Michael Chandaf1f1e2017-02-20 19:25:17 -05003320 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3321 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003322 }
3323}
3324
3325static void bnxt_disable_int_sync(struct bnxt *bp)
3326{
3327 int i;
3328
3329 atomic_inc(&bp->intr_sem);
3330
3331 bnxt_disable_int(bp);
3332 for (i = 0; i < bp->cp_nr_rings; i++)
3333 synchronize_irq(bp->irq_tbl[i].vector);
3334}
3335
3336static void bnxt_enable_int(struct bnxt *bp)
3337{
3338 int i;
3339
3340 atomic_set(&bp->intr_sem, 0);
3341 for (i = 0; i < bp->cp_nr_rings; i++) {
3342 struct bnxt_napi *bnapi = bp->bnapi[i];
3343 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3344
3345 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3346 }
3347}
3348
Michael Chanc0c050c2015-10-22 16:01:17 -04003349void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3350 u16 cmpl_ring, u16 target_id)
3351{
Michael Chana8643e12016-02-26 04:00:05 -05003352 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003353
Michael Chana8643e12016-02-26 04:00:05 -05003354 req->req_type = cpu_to_le16(req_type);
3355 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3356 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003357 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3358}
3359
Michael Chanfbfbc482016-02-26 04:00:07 -05003360static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3361 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003362{
Michael Chana11fa2b2016-05-15 03:04:47 -04003363 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003364 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003365 u32 *data = msg;
3366 __le32 *resp_len, *valid;
3367 u16 cp_ring_id, len = 0;
3368 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003369 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003370
Michael Chana8643e12016-02-26 04:00:05 -05003371 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003372 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003373 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003374 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3375
Deepak Khungare605db82017-05-29 19:06:04 -04003376 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3377 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3378 struct hwrm_short_input short_input = {0};
3379
3380 memcpy(short_cmd_req, req, msg_len);
3381 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3382 msg_len);
3383
3384 short_input.req_type = req->req_type;
3385 short_input.signature =
3386 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3387 short_input.size = cpu_to_le16(msg_len);
3388 short_input.req_addr =
3389 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3390
3391 data = (u32 *)&short_input;
3392 msg_len = sizeof(short_input);
3393
3394 /* Sync memory write before updating doorbell */
3395 wmb();
3396
3397 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3398 }
3399
Michael Chanc0c050c2015-10-22 16:01:17 -04003400 /* Write request msg to hwrm channel */
3401 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3402
Deepak Khungare605db82017-05-29 19:06:04 -04003403 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003404 writel(0, bp->bar0 + i);
3405
Michael Chanc0c050c2015-10-22 16:01:17 -04003406 /* currently supports only one outstanding message */
3407 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003408 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003409
3410 /* Ring channel doorbell */
3411 writel(1, bp->bar0 + 0x100);
3412
Michael Chanff4fe812016-02-26 04:00:04 -05003413 if (!timeout)
3414 timeout = DFLT_HWRM_CMD_TIMEOUT;
3415
Michael Chanc0c050c2015-10-22 16:01:17 -04003416 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003417 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003418 if (intr_process) {
3419 /* Wait until hwrm response cmpl interrupt is processed */
3420 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003421 i++ < tmo_count) {
3422 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003423 }
3424
3425 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3426 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003427 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003428 return -1;
3429 }
3430 } else {
3431 /* Check if response len is updated */
3432 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003433 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003434 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3435 HWRM_RESP_LEN_SFT;
3436 if (len)
3437 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003438 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003439 }
3440
Michael Chana11fa2b2016-05-15 03:04:47 -04003441 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003442 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003443 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003444 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003445 return -1;
3446 }
3447
3448 /* Last word of resp contains valid bit */
3449 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003450 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003451 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3452 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003453 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003454 }
3455
Michael Chana11fa2b2016-05-15 03:04:47 -04003456 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003457 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003458 timeout, le16_to_cpu(req->req_type),
3459 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003460 return -1;
3461 }
3462 }
3463
3464 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003465 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003466 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3467 le16_to_cpu(resp->req_type),
3468 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003469 return rc;
3470}
3471
3472int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3473{
3474 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003475}
3476
Michael Chancc72f3b2017-10-13 21:09:33 -04003477int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3478 int timeout)
3479{
3480 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3481}
3482
Michael Chanc0c050c2015-10-22 16:01:17 -04003483int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3484{
3485 int rc;
3486
3487 mutex_lock(&bp->hwrm_cmd_lock);
3488 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3489 mutex_unlock(&bp->hwrm_cmd_lock);
3490 return rc;
3491}
3492
Michael Chan90e209212016-02-26 04:00:08 -05003493int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3494 int timeout)
3495{
3496 int rc;
3497
3498 mutex_lock(&bp->hwrm_cmd_lock);
3499 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3500 mutex_unlock(&bp->hwrm_cmd_lock);
3501 return rc;
3502}
3503
Michael Chana1653b12016-12-07 00:26:20 -05003504int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3505 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003506{
3507 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003508 DECLARE_BITMAP(async_events_bmap, 256);
3509 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003510 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003511
3512 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3513
3514 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003515 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003516
Michael Chan25be8622016-04-05 14:09:00 -04003517 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3518 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3519 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3520
Michael Chana1653b12016-12-07 00:26:20 -05003521 if (bmap && bmap_size) {
3522 for (i = 0; i < bmap_size; i++) {
3523 if (test_bit(i, bmap))
3524 __set_bit(i, async_events_bmap);
3525 }
3526 }
3527
Michael Chan25be8622016-04-05 14:09:00 -04003528 for (i = 0; i < 8; i++)
3529 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3530
Michael Chana1653b12016-12-07 00:26:20 -05003531 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3532}
3533
3534static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3535{
3536 struct hwrm_func_drv_rgtr_input req = {0};
3537
3538 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3539
3540 req.enables =
3541 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3542 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3543
Michael Chan11f15ed2016-04-05 14:08:55 -04003544 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003545 req.ver_maj = DRV_VER_MAJ;
3546 req.ver_min = DRV_VER_MIN;
3547 req.ver_upd = DRV_VER_UPD;
3548
3549 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003550 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003551 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003552
Michael Chan9b0436c2017-07-11 13:05:36 -04003553 memset(data, 0, sizeof(data));
3554 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3555 u16 cmd = bnxt_vf_req_snif[i];
3556 unsigned int bit, idx;
3557
3558 idx = cmd / 32;
3559 bit = cmd % 32;
3560 data[idx] |= 1 << bit;
3561 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003562
Michael Chande68f5de2015-12-09 19:35:41 -05003563 for (i = 0; i < 8; i++)
3564 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3565
Michael Chanc0c050c2015-10-22 16:01:17 -04003566 req.enables |=
3567 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3568 }
3569
3570 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3571}
3572
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003573static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3574{
3575 struct hwrm_func_drv_unrgtr_input req = {0};
3576
3577 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3578 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3579}
3580
Michael Chanc0c050c2015-10-22 16:01:17 -04003581static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3582{
3583 u32 rc = 0;
3584 struct hwrm_tunnel_dst_port_free_input req = {0};
3585
3586 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3587 req.tunnel_type = tunnel_type;
3588
3589 switch (tunnel_type) {
3590 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3591 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3592 break;
3593 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3594 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3595 break;
3596 default:
3597 break;
3598 }
3599
3600 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3601 if (rc)
3602 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3603 rc);
3604 return rc;
3605}
3606
3607static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3608 u8 tunnel_type)
3609{
3610 u32 rc = 0;
3611 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3612 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3613
3614 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3615
3616 req.tunnel_type = tunnel_type;
3617 req.tunnel_dst_port_val = port;
3618
3619 mutex_lock(&bp->hwrm_cmd_lock);
3620 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3621 if (rc) {
3622 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3623 rc);
3624 goto err_out;
3625 }
3626
Christophe Jaillet57aac712016-11-22 06:14:40 +01003627 switch (tunnel_type) {
3628 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003629 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003630 break;
3631 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003632 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003633 break;
3634 default:
3635 break;
3636 }
3637
Michael Chanc0c050c2015-10-22 16:01:17 -04003638err_out:
3639 mutex_unlock(&bp->hwrm_cmd_lock);
3640 return rc;
3641}
3642
3643static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3644{
3645 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3646 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3647
3648 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003649 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003650
3651 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3652 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3653 req.mask = cpu_to_le32(vnic->rx_mask);
3654 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3655}
3656
3657#ifdef CONFIG_RFS_ACCEL
3658static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3659 struct bnxt_ntuple_filter *fltr)
3660{
3661 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3662
3663 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3664 req.ntuple_filter_id = fltr->filter_id;
3665 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3666}
3667
3668#define BNXT_NTP_FLTR_FLAGS \
3669 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3670 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3671 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3672 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3673 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3674 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3675 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3676 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3677 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3678 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3679 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3680 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3681 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003682 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003683
Michael Chan61aad722017-02-12 19:18:14 -05003684#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3685 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3686
Michael Chanc0c050c2015-10-22 16:01:17 -04003687static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3688 struct bnxt_ntuple_filter *fltr)
3689{
3690 int rc = 0;
3691 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3692 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3693 bp->hwrm_cmd_resp_addr;
3694 struct flow_keys *keys = &fltr->fkeys;
3695 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3696
3697 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003698 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003699
3700 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3701
3702 req.ethertype = htons(ETH_P_IP);
3703 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003704 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003705 req.ip_protocol = keys->basic.ip_proto;
3706
Michael Chandda0e742016-12-29 12:13:40 -05003707 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3708 int i;
3709
3710 req.ethertype = htons(ETH_P_IPV6);
3711 req.ip_addr_type =
3712 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3713 *(struct in6_addr *)&req.src_ipaddr[0] =
3714 keys->addrs.v6addrs.src;
3715 *(struct in6_addr *)&req.dst_ipaddr[0] =
3716 keys->addrs.v6addrs.dst;
3717 for (i = 0; i < 4; i++) {
3718 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3719 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3720 }
3721 } else {
3722 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3723 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3724 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3725 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3726 }
Michael Chan61aad722017-02-12 19:18:14 -05003727 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3728 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3729 req.tunnel_type =
3730 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3731 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003732
3733 req.src_port = keys->ports.src;
3734 req.src_port_mask = cpu_to_be16(0xffff);
3735 req.dst_port = keys->ports.dst;
3736 req.dst_port_mask = cpu_to_be16(0xffff);
3737
Michael Chanc1935542015-12-27 18:19:28 -05003738 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003739 mutex_lock(&bp->hwrm_cmd_lock);
3740 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3741 if (!rc)
3742 fltr->filter_id = resp->ntuple_filter_id;
3743 mutex_unlock(&bp->hwrm_cmd_lock);
3744 return rc;
3745}
3746#endif
3747
3748static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3749 u8 *mac_addr)
3750{
3751 u32 rc = 0;
3752 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3753 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3754
3755 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003756 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3757 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3758 req.flags |=
3759 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003760 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003761 req.enables =
3762 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003763 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003764 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3765 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3766 req.l2_addr_mask[0] = 0xff;
3767 req.l2_addr_mask[1] = 0xff;
3768 req.l2_addr_mask[2] = 0xff;
3769 req.l2_addr_mask[3] = 0xff;
3770 req.l2_addr_mask[4] = 0xff;
3771 req.l2_addr_mask[5] = 0xff;
3772
3773 mutex_lock(&bp->hwrm_cmd_lock);
3774 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3775 if (!rc)
3776 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3777 resp->l2_filter_id;
3778 mutex_unlock(&bp->hwrm_cmd_lock);
3779 return rc;
3780}
3781
3782static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3783{
3784 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3785 int rc = 0;
3786
3787 /* Any associated ntuple filters will also be cleared by firmware. */
3788 mutex_lock(&bp->hwrm_cmd_lock);
3789 for (i = 0; i < num_of_vnics; i++) {
3790 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3791
3792 for (j = 0; j < vnic->uc_filter_count; j++) {
3793 struct hwrm_cfa_l2_filter_free_input req = {0};
3794
3795 bnxt_hwrm_cmd_hdr_init(bp, &req,
3796 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3797
3798 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3799
3800 rc = _hwrm_send_message(bp, &req, sizeof(req),
3801 HWRM_CMD_TIMEOUT);
3802 }
3803 vnic->uc_filter_count = 0;
3804 }
3805 mutex_unlock(&bp->hwrm_cmd_lock);
3806
3807 return rc;
3808}
3809
3810static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3811{
3812 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3813 struct hwrm_vnic_tpa_cfg_input req = {0};
3814
3815 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3816
3817 if (tpa_flags) {
3818 u16 mss = bp->dev->mtu - 40;
3819 u32 nsegs, n, segs = 0, flags;
3820
3821 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3822 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3823 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3824 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3825 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3826 if (tpa_flags & BNXT_FLAG_GRO)
3827 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3828
3829 req.flags = cpu_to_le32(flags);
3830
3831 req.enables =
3832 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003833 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3834 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003835
3836 /* Number of segs are log2 units, and first packet is not
3837 * included as part of this units.
3838 */
Michael Chan2839f282016-04-25 02:30:50 -04003839 if (mss <= BNXT_RX_PAGE_SIZE) {
3840 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003841 nsegs = (MAX_SKB_FRAGS - 1) * n;
3842 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003843 n = mss / BNXT_RX_PAGE_SIZE;
3844 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003845 n++;
3846 nsegs = (MAX_SKB_FRAGS - n) / n;
3847 }
3848
3849 segs = ilog2(nsegs);
3850 req.max_agg_segs = cpu_to_le16(segs);
3851 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003852
3853 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003854 }
3855 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3856
3857 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3858}
3859
3860static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3861{
3862 u32 i, j, max_rings;
3863 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3864 struct hwrm_vnic_rss_cfg_input req = {0};
3865
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003866 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003867 return 0;
3868
3869 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3870 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003871 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003872 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3873 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3874 max_rings = bp->rx_nr_rings - 1;
3875 else
3876 max_rings = bp->rx_nr_rings;
3877 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003878 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003879 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003880
3881 /* Fill the RSS indirection table with ring group ids */
3882 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3883 if (j == max_rings)
3884 j = 0;
3885 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3886 }
3887
3888 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3889 req.hash_key_tbl_addr =
3890 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3891 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003892 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003893 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3894}
3895
3896static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3897{
3898 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3899 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3900
3901 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3902 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3903 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3904 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3905 req.enables =
3906 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3907 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3908 /* thresholds not implemented in firmware yet */
3909 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3910 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3911 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3912 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3913}
3914
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003915static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3916 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003917{
3918 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3919
3920 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3921 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003922 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003923
3924 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003925 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003926}
3927
3928static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3929{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003930 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003931
3932 for (i = 0; i < bp->nr_vnics; i++) {
3933 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3934
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003935 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3936 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3937 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3938 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003939 }
3940 bp->rsscos_nr_ctxs = 0;
3941}
3942
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003943static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003944{
3945 int rc;
3946 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3947 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3948 bp->hwrm_cmd_resp_addr;
3949
3950 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3951 -1);
3952
3953 mutex_lock(&bp->hwrm_cmd_lock);
3954 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3955 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003956 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003957 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3958 mutex_unlock(&bp->hwrm_cmd_lock);
3959
3960 return rc;
3961}
3962
Michael Chana588e452016-12-07 00:26:21 -05003963int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003964{
Michael Chanb81a90d2016-01-02 23:45:01 -05003965 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003966 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3967 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003968 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003969
3970 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003971
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003972 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3973 /* Only RSS support for now TBD: COS & LB */
3974 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3975 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3976 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3977 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003978 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3979 req.rss_rule =
3980 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3981 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3982 VNIC_CFG_REQ_ENABLES_MRU);
3983 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003984 } else {
3985 req.rss_rule = cpu_to_le16(0xffff);
3986 }
3987
3988 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3989 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003990 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3991 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3992 } else {
3993 req.cos_rule = cpu_to_le16(0xffff);
3994 }
3995
Michael Chanc0c050c2015-10-22 16:01:17 -04003996 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003997 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003998 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003999 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004000 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4001 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004002
Michael Chanb81a90d2016-01-02 23:45:01 -05004003 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004004 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4005 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4006
4007 req.lb_rule = cpu_to_le16(0xffff);
4008 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4009 VLAN_HLEN);
4010
Michael Chancf6645f2016-06-13 02:25:28 -04004011#ifdef CONFIG_BNXT_SRIOV
4012 if (BNXT_VF(bp))
4013 def_vlan = bp->vf.vlan;
4014#endif
4015 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04004016 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05004017 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4018 req.flags |=
4019 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04004020
4021 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4022}
4023
4024static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4025{
4026 u32 rc = 0;
4027
4028 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4029 struct hwrm_vnic_free_input req = {0};
4030
4031 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4032 req.vnic_id =
4033 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4034
4035 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4036 if (rc)
4037 return rc;
4038 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4039 }
4040 return rc;
4041}
4042
4043static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4044{
4045 u16 i;
4046
4047 for (i = 0; i < bp->nr_vnics; i++)
4048 bnxt_hwrm_vnic_free_one(bp, i);
4049}
4050
Michael Chanb81a90d2016-01-02 23:45:01 -05004051static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4052 unsigned int start_rx_ring_idx,
4053 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04004054{
Michael Chanb81a90d2016-01-02 23:45:01 -05004055 int rc = 0;
4056 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004057 struct hwrm_vnic_alloc_input req = {0};
4058 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4059
4060 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05004061 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4062 grp_idx = bp->rx_ring[i].bnapi->index;
4063 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004064 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004065 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004066 break;
4067 }
4068 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05004069 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004070 }
4071
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004072 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4073 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004074 if (vnic_id == 0)
4075 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4076
4077 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4078
4079 mutex_lock(&bp->hwrm_cmd_lock);
4080 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4081 if (!rc)
4082 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4083 mutex_unlock(&bp->hwrm_cmd_lock);
4084 return rc;
4085}
4086
Michael Chan8fdefd62016-12-29 12:13:36 -05004087static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4088{
4089 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4090 struct hwrm_vnic_qcaps_input req = {0};
4091 int rc;
4092
4093 if (bp->hwrm_spec_code < 0x10600)
4094 return 0;
4095
4096 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4097 mutex_lock(&bp->hwrm_cmd_lock);
4098 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4099 if (!rc) {
4100 if (resp->flags &
4101 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4102 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4103 }
4104 mutex_unlock(&bp->hwrm_cmd_lock);
4105 return rc;
4106}
4107
Michael Chanc0c050c2015-10-22 16:01:17 -04004108static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4109{
4110 u16 i;
4111 u32 rc = 0;
4112
4113 mutex_lock(&bp->hwrm_cmd_lock);
4114 for (i = 0; i < bp->rx_nr_rings; i++) {
4115 struct hwrm_ring_grp_alloc_input req = {0};
4116 struct hwrm_ring_grp_alloc_output *resp =
4117 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004118 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004119
4120 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4121
Michael Chanb81a90d2016-01-02 23:45:01 -05004122 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4123 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4124 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4125 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004126
4127 rc = _hwrm_send_message(bp, &req, sizeof(req),
4128 HWRM_CMD_TIMEOUT);
4129 if (rc)
4130 break;
4131
Michael Chanb81a90d2016-01-02 23:45:01 -05004132 bp->grp_info[grp_idx].fw_grp_id =
4133 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004134 }
4135 mutex_unlock(&bp->hwrm_cmd_lock);
4136 return rc;
4137}
4138
4139static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4140{
4141 u16 i;
4142 u32 rc = 0;
4143 struct hwrm_ring_grp_free_input req = {0};
4144
4145 if (!bp->grp_info)
4146 return 0;
4147
4148 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4149
4150 mutex_lock(&bp->hwrm_cmd_lock);
4151 for (i = 0; i < bp->cp_nr_rings; i++) {
4152 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4153 continue;
4154 req.ring_group_id =
4155 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4156
4157 rc = _hwrm_send_message(bp, &req, sizeof(req),
4158 HWRM_CMD_TIMEOUT);
4159 if (rc)
4160 break;
4161 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4162 }
4163 mutex_unlock(&bp->hwrm_cmd_lock);
4164 return rc;
4165}
4166
4167static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4168 struct bnxt_ring_struct *ring,
4169 u32 ring_type, u32 map_index,
4170 u32 stats_ctx_id)
4171{
4172 int rc = 0, err = 0;
4173 struct hwrm_ring_alloc_input req = {0};
4174 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4175 u16 ring_id;
4176
4177 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4178
4179 req.enables = 0;
4180 if (ring->nr_pages > 1) {
4181 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4182 /* Page size is in log2 units */
4183 req.page_size = BNXT_PAGE_SHIFT;
4184 req.page_tbl_depth = 1;
4185 } else {
4186 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4187 }
4188 req.fbo = 0;
4189 /* Association of ring index with doorbell index and MSIX number */
4190 req.logical_id = cpu_to_le16(map_index);
4191
4192 switch (ring_type) {
4193 case HWRM_RING_ALLOC_TX:
4194 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4195 /* Association of transmit ring with completion ring */
4196 req.cmpl_ring_id =
4197 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4198 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4199 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4200 req.queue_id = cpu_to_le16(ring->queue_id);
4201 break;
4202 case HWRM_RING_ALLOC_RX:
4203 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4204 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4205 break;
4206 case HWRM_RING_ALLOC_AGG:
4207 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4208 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4209 break;
4210 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004211 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004212 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4213 if (bp->flags & BNXT_FLAG_USING_MSIX)
4214 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4215 break;
4216 default:
4217 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4218 ring_type);
4219 return -1;
4220 }
4221
4222 mutex_lock(&bp->hwrm_cmd_lock);
4223 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4224 err = le16_to_cpu(resp->error_code);
4225 ring_id = le16_to_cpu(resp->ring_id);
4226 mutex_unlock(&bp->hwrm_cmd_lock);
4227
4228 if (rc || err) {
4229 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004230 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004231 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4232 rc, err);
4233 return -1;
4234
4235 case RING_FREE_REQ_RING_TYPE_RX:
4236 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4237 rc, err);
4238 return -1;
4239
4240 case RING_FREE_REQ_RING_TYPE_TX:
4241 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4242 rc, err);
4243 return -1;
4244
4245 default:
4246 netdev_err(bp->dev, "Invalid ring\n");
4247 return -1;
4248 }
4249 }
4250 ring->fw_ring_id = ring_id;
4251 return rc;
4252}
4253
Michael Chan486b5c22016-12-29 12:13:42 -05004254static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4255{
4256 int rc;
4257
4258 if (BNXT_PF(bp)) {
4259 struct hwrm_func_cfg_input req = {0};
4260
4261 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4262 req.fid = cpu_to_le16(0xffff);
4263 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4264 req.async_event_cr = cpu_to_le16(idx);
4265 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4266 } else {
4267 struct hwrm_func_vf_cfg_input req = {0};
4268
4269 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4270 req.enables =
4271 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4272 req.async_event_cr = cpu_to_le16(idx);
4273 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4274 }
4275 return rc;
4276}
4277
Michael Chanc0c050c2015-10-22 16:01:17 -04004278static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4279{
4280 int i, rc = 0;
4281
Michael Chanedd0c2c2015-12-27 18:19:19 -05004282 for (i = 0; i < bp->cp_nr_rings; i++) {
4283 struct bnxt_napi *bnapi = bp->bnapi[i];
4284 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4285 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004286
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004287 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004288 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4289 INVALID_STATS_CTX_ID);
4290 if (rc)
4291 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004292 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4293 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004294
4295 if (!i) {
4296 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4297 if (rc)
4298 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4299 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004300 }
4301
Michael Chanedd0c2c2015-12-27 18:19:19 -05004302 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004303 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004304 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004305 u32 map_idx = txr->bnapi->index;
4306 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004307
Michael Chanb81a90d2016-01-02 23:45:01 -05004308 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4309 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004310 if (rc)
4311 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004312 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004313 }
4314
Michael Chanedd0c2c2015-12-27 18:19:19 -05004315 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004316 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004317 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004318 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004319
Michael Chanb81a90d2016-01-02 23:45:01 -05004320 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4321 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004322 if (rc)
4323 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004324 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004325 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004326 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004327 }
4328
4329 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4330 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004331 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004332 struct bnxt_ring_struct *ring =
4333 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004334 u32 grp_idx = rxr->bnapi->index;
4335 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004336
4337 rc = hwrm_ring_alloc_send_msg(bp, ring,
4338 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004339 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004340 INVALID_STATS_CTX_ID);
4341 if (rc)
4342 goto err_out;
4343
Michael Chanb81a90d2016-01-02 23:45:01 -05004344 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004345 writel(DB_KEY_RX | rxr->rx_agg_prod,
4346 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004347 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004348 }
4349 }
4350err_out:
4351 return rc;
4352}
4353
4354static int hwrm_ring_free_send_msg(struct bnxt *bp,
4355 struct bnxt_ring_struct *ring,
4356 u32 ring_type, int cmpl_ring_id)
4357{
4358 int rc;
4359 struct hwrm_ring_free_input req = {0};
4360 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4361 u16 error_code;
4362
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004363 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004364 req.ring_type = ring_type;
4365 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4366
4367 mutex_lock(&bp->hwrm_cmd_lock);
4368 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4369 error_code = le16_to_cpu(resp->error_code);
4370 mutex_unlock(&bp->hwrm_cmd_lock);
4371
4372 if (rc || error_code) {
4373 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004374 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004375 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4376 rc);
4377 return rc;
4378 case RING_FREE_REQ_RING_TYPE_RX:
4379 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4380 rc);
4381 return rc;
4382 case RING_FREE_REQ_RING_TYPE_TX:
4383 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4384 rc);
4385 return rc;
4386 default:
4387 netdev_err(bp->dev, "Invalid ring\n");
4388 return -1;
4389 }
4390 }
4391 return 0;
4392}
4393
Michael Chanedd0c2c2015-12-27 18:19:19 -05004394static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004395{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004396 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004397
4398 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004399 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004400
Michael Chanedd0c2c2015-12-27 18:19:19 -05004401 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004402 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004403 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004404 u32 grp_idx = txr->bnapi->index;
4405 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004406
Michael Chanedd0c2c2015-12-27 18:19:19 -05004407 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4408 hwrm_ring_free_send_msg(bp, ring,
4409 RING_FREE_REQ_RING_TYPE_TX,
4410 close_path ? cmpl_ring_id :
4411 INVALID_HW_RING_ID);
4412 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004413 }
4414 }
4415
Michael Chanedd0c2c2015-12-27 18:19:19 -05004416 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004417 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004418 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004419 u32 grp_idx = rxr->bnapi->index;
4420 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004421
Michael Chanedd0c2c2015-12-27 18:19:19 -05004422 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4423 hwrm_ring_free_send_msg(bp, ring,
4424 RING_FREE_REQ_RING_TYPE_RX,
4425 close_path ? cmpl_ring_id :
4426 INVALID_HW_RING_ID);
4427 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004428 bp->grp_info[grp_idx].rx_fw_ring_id =
4429 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004430 }
4431 }
4432
Michael Chanedd0c2c2015-12-27 18:19:19 -05004433 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004434 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004435 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004436 u32 grp_idx = rxr->bnapi->index;
4437 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004438
Michael Chanedd0c2c2015-12-27 18:19:19 -05004439 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4440 hwrm_ring_free_send_msg(bp, ring,
4441 RING_FREE_REQ_RING_TYPE_RX,
4442 close_path ? cmpl_ring_id :
4443 INVALID_HW_RING_ID);
4444 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004445 bp->grp_info[grp_idx].agg_fw_ring_id =
4446 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004447 }
4448 }
4449
Michael Chan9d8bc092016-12-29 12:13:33 -05004450 /* The completion rings are about to be freed. After that the
4451 * IRQ doorbell will not work anymore. So we need to disable
4452 * IRQ here.
4453 */
4454 bnxt_disable_int_sync(bp);
4455
Michael Chanedd0c2c2015-12-27 18:19:19 -05004456 for (i = 0; i < bp->cp_nr_rings; i++) {
4457 struct bnxt_napi *bnapi = bp->bnapi[i];
4458 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4459 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004460
Michael Chanedd0c2c2015-12-27 18:19:19 -05004461 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4462 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004463 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004464 INVALID_HW_RING_ID);
4465 ring->fw_ring_id = INVALID_HW_RING_ID;
4466 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004467 }
4468 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004469}
4470
Michael Chan391be5c2016-12-29 12:13:41 -05004471/* Caller must hold bp->hwrm_cmd_lock */
4472int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4473{
4474 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4475 struct hwrm_func_qcfg_input req = {0};
4476 int rc;
4477
4478 if (bp->hwrm_spec_code < 0x10601)
4479 return 0;
4480
4481 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4482 req.fid = cpu_to_le16(fid);
4483 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4484 if (!rc)
4485 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4486
4487 return rc;
4488}
4489
Michael Chand1e79252017-02-06 16:55:38 -05004490static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004491{
4492 struct hwrm_func_cfg_input req = {0};
4493 int rc;
4494
4495 if (bp->hwrm_spec_code < 0x10601)
4496 return 0;
4497
4498 if (BNXT_VF(bp))
4499 return 0;
4500
4501 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4502 req.fid = cpu_to_le16(0xffff);
4503 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4504 req.num_tx_rings = cpu_to_le16(*tx_rings);
4505 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4506 if (rc)
4507 return rc;
4508
4509 mutex_lock(&bp->hwrm_cmd_lock);
4510 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4511 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan98fdbe72017-08-28 13:40:26 -04004512 if (!rc)
4513 bp->tx_reserved_rings = *tx_rings;
Michael Chan391be5c2016-12-29 12:13:41 -05004514 return rc;
4515}
4516
Michael Chan98fdbe72017-08-28 13:40:26 -04004517static int bnxt_hwrm_check_tx_rings(struct bnxt *bp, int tx_rings)
4518{
4519 struct hwrm_func_cfg_input req = {0};
4520 int rc;
4521
4522 if (bp->hwrm_spec_code < 0x10801)
4523 return 0;
4524
4525 if (BNXT_VF(bp))
4526 return 0;
4527
4528 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4529 req.fid = cpu_to_le16(0xffff);
4530 req.flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST);
4531 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4532 req.num_tx_rings = cpu_to_le16(tx_rings);
4533 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4534 if (rc)
4535 return -ENOMEM;
4536 return 0;
4537}
4538
Michael Chanbb053f52016-02-26 04:00:02 -05004539static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4540 u32 buf_tmrs, u16 flags,
4541 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4542{
4543 req->flags = cpu_to_le16(flags);
4544 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4545 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4546 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4547 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4548 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4549 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4550 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4551 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4552}
4553
Michael Chanc0c050c2015-10-22 16:01:17 -04004554int bnxt_hwrm_set_coal(struct bnxt *bp)
4555{
4556 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004557 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4558 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004559 u16 max_buf, max_buf_irq;
4560 u16 buf_tmr, buf_tmr_irq;
4561 u32 flags;
4562
Michael Chandfc9c942016-02-26 04:00:03 -05004563 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4564 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4565 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4566 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004567
Michael Chandfb5b892016-02-26 04:00:01 -05004568 /* Each rx completion (2 records) should be DMAed immediately.
4569 * DMA 1/4 of the completion buffers at a time.
4570 */
4571 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004572 /* max_buf must not be zero */
4573 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004574 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4575 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4576 /* buf timer set to 1/4 of interrupt timer */
4577 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4578 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4579 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004580
4581 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4582
4583 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4584 * if coal_ticks is less than 25 us.
4585 */
Michael Chandfb5b892016-02-26 04:00:01 -05004586 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004587 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4588
Michael Chanbb053f52016-02-26 04:00:02 -05004589 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004590 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4591
4592 /* max_buf must not be zero */
4593 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4594 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4595 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4596 /* buf timer set to 1/4 of interrupt timer */
4597 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4598 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4599 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4600
4601 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4602 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4603 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004604
4605 mutex_lock(&bp->hwrm_cmd_lock);
4606 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004607 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004608
Michael Chandfc9c942016-02-26 04:00:03 -05004609 req = &req_rx;
4610 if (!bnapi->rx_ring)
4611 req = &req_tx;
4612 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4613
4614 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004615 HWRM_CMD_TIMEOUT);
4616 if (rc)
4617 break;
4618 }
4619 mutex_unlock(&bp->hwrm_cmd_lock);
4620 return rc;
4621}
4622
4623static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4624{
4625 int rc = 0, i;
4626 struct hwrm_stat_ctx_free_input req = {0};
4627
4628 if (!bp->bnapi)
4629 return 0;
4630
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004631 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4632 return 0;
4633
Michael Chanc0c050c2015-10-22 16:01:17 -04004634 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4635
4636 mutex_lock(&bp->hwrm_cmd_lock);
4637 for (i = 0; i < bp->cp_nr_rings; i++) {
4638 struct bnxt_napi *bnapi = bp->bnapi[i];
4639 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4640
4641 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4642 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4643
4644 rc = _hwrm_send_message(bp, &req, sizeof(req),
4645 HWRM_CMD_TIMEOUT);
4646 if (rc)
4647 break;
4648
4649 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4650 }
4651 }
4652 mutex_unlock(&bp->hwrm_cmd_lock);
4653 return rc;
4654}
4655
4656static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4657{
4658 int rc = 0, i;
4659 struct hwrm_stat_ctx_alloc_input req = {0};
4660 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4661
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004662 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4663 return 0;
4664
Michael Chanc0c050c2015-10-22 16:01:17 -04004665 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4666
Michael Chan51f30782016-07-01 18:46:29 -04004667 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004668
4669 mutex_lock(&bp->hwrm_cmd_lock);
4670 for (i = 0; i < bp->cp_nr_rings; i++) {
4671 struct bnxt_napi *bnapi = bp->bnapi[i];
4672 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4673
4674 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4675
4676 rc = _hwrm_send_message(bp, &req, sizeof(req),
4677 HWRM_CMD_TIMEOUT);
4678 if (rc)
4679 break;
4680
4681 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4682
4683 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4684 }
4685 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004686 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004687}
4688
Michael Chancf6645f2016-06-13 02:25:28 -04004689static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4690{
4691 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004692 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04004693 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04004694 int rc;
4695
4696 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4697 req.fid = cpu_to_le16(0xffff);
4698 mutex_lock(&bp->hwrm_cmd_lock);
4699 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4700 if (rc)
4701 goto func_qcfg_exit;
4702
4703#ifdef CONFIG_BNXT_SRIOV
4704 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004705 struct bnxt_vf_info *vf = &bp->vf;
4706
4707 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4708 }
4709#endif
Michael Chan9315edc2017-07-24 12:34:25 -04004710 flags = le16_to_cpu(resp->flags);
4711 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4712 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
4713 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4714 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
4715 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04004716 }
Michael Chan9315edc2017-07-24 12:34:25 -04004717 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
4718 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05004719
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004720 switch (resp->port_partition_type) {
4721 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4722 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4723 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4724 bp->port_partition_type = resp->port_partition_type;
4725 break;
4726 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004727 if (bp->hwrm_spec_code < 0x10707 ||
4728 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4729 bp->br_mode = BRIDGE_MODE_VEB;
4730 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4731 bp->br_mode = BRIDGE_MODE_VEPA;
4732 else
4733 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004734
4735func_qcfg_exit:
4736 mutex_unlock(&bp->hwrm_cmd_lock);
4737 return rc;
4738}
4739
Michael Chan7b08f662016-12-07 00:26:18 -05004740static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004741{
4742 int rc = 0;
4743 struct hwrm_func_qcaps_input req = {0};
4744 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4745
4746 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4747 req.fid = cpu_to_le16(0xffff);
4748
4749 mutex_lock(&bp->hwrm_cmd_lock);
4750 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4751 if (rc)
4752 goto hwrm_func_qcaps_exit;
4753
Michael Chane4060d32016-12-07 00:26:19 -05004754 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4755 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4756 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4757 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4758
Michael Chan7cc5a202016-09-19 03:58:05 -04004759 bp->tx_push_thresh = 0;
4760 if (resp->flags &
4761 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4762 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4763
Michael Chanc0c050c2015-10-22 16:01:17 -04004764 if (BNXT_PF(bp)) {
4765 struct bnxt_pf_info *pf = &bp->pf;
4766
4767 pf->fw_fid = le16_to_cpu(resp->fid);
4768 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004769 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004770 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004771 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4772 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4773 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004774 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004775 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4776 if (!pf->max_hw_ring_grps)
4777 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004778 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4779 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4780 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4781 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4782 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4783 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4784 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4785 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4786 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4787 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4788 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004789 if (resp->flags &
4790 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4791 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004792 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004793#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004794 struct bnxt_vf_info *vf = &bp->vf;
4795
4796 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004797
4798 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4799 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4800 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4801 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004802 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4803 if (!vf->max_hw_ring_grps)
4804 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004805 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4806 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4807 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004808
4809 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan379a80a2015-10-23 15:06:19 -04004810#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004811 }
4812
Michael Chanc0c050c2015-10-22 16:01:17 -04004813hwrm_func_qcaps_exit:
4814 mutex_unlock(&bp->hwrm_cmd_lock);
4815 return rc;
4816}
4817
4818static int bnxt_hwrm_func_reset(struct bnxt *bp)
4819{
4820 struct hwrm_func_reset_input req = {0};
4821
4822 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4823 req.enables = 0;
4824
4825 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4826}
4827
4828static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4829{
4830 int rc = 0;
4831 struct hwrm_queue_qportcfg_input req = {0};
4832 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4833 u8 i, *qptr;
4834
4835 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4836
4837 mutex_lock(&bp->hwrm_cmd_lock);
4838 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4839 if (rc)
4840 goto qportcfg_exit;
4841
4842 if (!resp->max_configurable_queues) {
4843 rc = -EINVAL;
4844 goto qportcfg_exit;
4845 }
4846 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004847 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004848 if (bp->max_tc > BNXT_MAX_QUEUE)
4849 bp->max_tc = BNXT_MAX_QUEUE;
4850
Michael Chan441cabb2016-09-19 03:58:02 -04004851 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4852 bp->max_tc = 1;
4853
Michael Chan87c374d2016-12-02 21:17:16 -05004854 if (bp->max_lltc > bp->max_tc)
4855 bp->max_lltc = bp->max_tc;
4856
Michael Chanc0c050c2015-10-22 16:01:17 -04004857 qptr = &resp->queue_id0;
4858 for (i = 0; i < bp->max_tc; i++) {
4859 bp->q_info[i].queue_id = *qptr++;
4860 bp->q_info[i].queue_profile = *qptr++;
4861 }
4862
4863qportcfg_exit:
4864 mutex_unlock(&bp->hwrm_cmd_lock);
4865 return rc;
4866}
4867
4868static int bnxt_hwrm_ver_get(struct bnxt *bp)
4869{
4870 int rc;
4871 struct hwrm_ver_get_input req = {0};
4872 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004873 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004874
Michael Chane6ef2692016-03-28 19:46:05 -04004875 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004876 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4877 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4878 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4879 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4880 mutex_lock(&bp->hwrm_cmd_lock);
4881 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4882 if (rc)
4883 goto hwrm_ver_get_exit;
4884
4885 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4886
Michael Chan11f15ed2016-04-05 14:08:55 -04004887 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4888 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004889 if (resp->hwrm_intf_maj < 1) {
4890 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004891 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004892 resp->hwrm_intf_upd);
4893 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004894 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004895 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004896 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4897 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4898
Michael Chanff4fe812016-02-26 04:00:04 -05004899 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4900 if (!bp->hwrm_cmd_timeout)
4901 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4902
Michael Chane6ef2692016-03-28 19:46:05 -04004903 if (resp->hwrm_intf_maj >= 1)
4904 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4905
Michael Chan659c8052016-06-13 02:25:33 -04004906 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004907 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4908 !resp->chip_metal)
4909 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004910
Deepak Khungare605db82017-05-29 19:06:04 -04004911 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4912 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4913 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4914 bp->flags |= BNXT_FLAG_SHORT_CMD;
4915
Michael Chanc0c050c2015-10-22 16:01:17 -04004916hwrm_ver_get_exit:
4917 mutex_unlock(&bp->hwrm_cmd_lock);
4918 return rc;
4919}
4920
Rob Swindell5ac67d82016-09-19 03:58:03 -04004921int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4922{
Rob Swindell878786d2016-09-20 03:36:33 -04004923#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004924 struct hwrm_fw_set_time_input req = {0};
4925 struct rtc_time tm;
4926 struct timeval tv;
4927
4928 if (bp->hwrm_spec_code < 0x10400)
4929 return -EOPNOTSUPP;
4930
4931 do_gettimeofday(&tv);
4932 rtc_time_to_tm(tv.tv_sec, &tm);
4933 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4934 req.year = cpu_to_le16(1900 + tm.tm_year);
4935 req.month = 1 + tm.tm_mon;
4936 req.day = tm.tm_mday;
4937 req.hour = tm.tm_hour;
4938 req.minute = tm.tm_min;
4939 req.second = tm.tm_sec;
4940 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004941#else
4942 return -EOPNOTSUPP;
4943#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004944}
4945
Michael Chan3bdf56c2016-03-07 15:38:45 -05004946static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4947{
4948 int rc;
4949 struct bnxt_pf_info *pf = &bp->pf;
4950 struct hwrm_port_qstats_input req = {0};
4951
4952 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4953 return 0;
4954
4955 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4956 req.port_id = cpu_to_le16(pf->port_id);
4957 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4958 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4959 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4960 return rc;
4961}
4962
Michael Chanc0c050c2015-10-22 16:01:17 -04004963static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4964{
4965 if (bp->vxlan_port_cnt) {
4966 bnxt_hwrm_tunnel_dst_port_free(
4967 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4968 }
4969 bp->vxlan_port_cnt = 0;
4970 if (bp->nge_port_cnt) {
4971 bnxt_hwrm_tunnel_dst_port_free(
4972 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4973 }
4974 bp->nge_port_cnt = 0;
4975}
4976
4977static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4978{
4979 int rc, i;
4980 u32 tpa_flags = 0;
4981
4982 if (set_tpa)
4983 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4984 for (i = 0; i < bp->nr_vnics; i++) {
4985 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4986 if (rc) {
4987 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004988 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004989 return rc;
4990 }
4991 }
4992 return 0;
4993}
4994
4995static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4996{
4997 int i;
4998
4999 for (i = 0; i < bp->nr_vnics; i++)
5000 bnxt_hwrm_vnic_set_rss(bp, i, false);
5001}
5002
5003static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
5004 bool irq_re_init)
5005{
5006 if (bp->vnic_info) {
5007 bnxt_hwrm_clear_vnic_filter(bp);
5008 /* clear all RSS setting before free vnic ctx */
5009 bnxt_hwrm_clear_vnic_rss(bp);
5010 bnxt_hwrm_vnic_ctx_free(bp);
5011 /* before free the vnic, undo the vnic tpa settings */
5012 if (bp->flags & BNXT_FLAG_TPA)
5013 bnxt_set_tpa(bp, false);
5014 bnxt_hwrm_vnic_free(bp);
5015 }
5016 bnxt_hwrm_ring_free(bp, close_path);
5017 bnxt_hwrm_ring_grp_free(bp);
5018 if (irq_re_init) {
5019 bnxt_hwrm_stat_ctx_free(bp);
5020 bnxt_hwrm_free_tunnel_ports(bp);
5021 }
5022}
5023
Michael Chan39d8ba22017-07-24 12:34:22 -04005024static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
5025{
5026 struct hwrm_func_cfg_input req = {0};
5027 int rc;
5028
5029 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5030 req.fid = cpu_to_le16(0xffff);
5031 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
5032 if (br_mode == BRIDGE_MODE_VEB)
5033 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
5034 else if (br_mode == BRIDGE_MODE_VEPA)
5035 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
5036 else
5037 return -EINVAL;
5038 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5039 if (rc)
5040 rc = -EIO;
5041 return rc;
5042}
5043
Michael Chanc0c050c2015-10-22 16:01:17 -04005044static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
5045{
Michael Chanae10ae72016-12-29 12:13:38 -05005046 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04005047 int rc;
5048
Michael Chanae10ae72016-12-29 12:13:38 -05005049 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
5050 goto skip_rss_ctx;
5051
Michael Chanc0c050c2015-10-22 16:01:17 -04005052 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005053 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04005054 if (rc) {
5055 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5056 vnic_id, rc);
5057 goto vnic_setup_err;
5058 }
5059 bp->rsscos_nr_ctxs++;
5060
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04005061 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5062 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5063 if (rc) {
5064 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5065 vnic_id, rc);
5066 goto vnic_setup_err;
5067 }
5068 bp->rsscos_nr_ctxs++;
5069 }
5070
Michael Chanae10ae72016-12-29 12:13:38 -05005071skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04005072 /* configure default vnic, ring grp */
5073 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5074 if (rc) {
5075 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5076 vnic_id, rc);
5077 goto vnic_setup_err;
5078 }
5079
5080 /* Enable RSS hashing on vnic */
5081 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5082 if (rc) {
5083 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5084 vnic_id, rc);
5085 goto vnic_setup_err;
5086 }
5087
5088 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5089 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5090 if (rc) {
5091 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5092 vnic_id, rc);
5093 }
5094 }
5095
5096vnic_setup_err:
5097 return rc;
5098}
5099
5100static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5101{
5102#ifdef CONFIG_RFS_ACCEL
5103 int i, rc = 0;
5104
5105 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005106 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005107 u16 vnic_id = i + 1;
5108 u16 ring_id = i;
5109
5110 if (vnic_id >= bp->nr_vnics)
5111 break;
5112
Michael Chanae10ae72016-12-29 12:13:38 -05005113 vnic = &bp->vnic_info[vnic_id];
5114 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5115 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5116 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005117 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005118 if (rc) {
5119 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5120 vnic_id, rc);
5121 break;
5122 }
5123 rc = bnxt_setup_vnic(bp, vnic_id);
5124 if (rc)
5125 break;
5126 }
5127 return rc;
5128#else
5129 return 0;
5130#endif
5131}
5132
Michael Chan17c71ac2016-07-01 18:46:27 -04005133/* Allow PF and VF with default VLAN to be in promiscuous mode */
5134static bool bnxt_promisc_ok(struct bnxt *bp)
5135{
5136#ifdef CONFIG_BNXT_SRIOV
5137 if (BNXT_VF(bp) && !bp->vf.vlan)
5138 return false;
5139#endif
5140 return true;
5141}
5142
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005143static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5144{
5145 unsigned int rc = 0;
5146
5147 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5148 if (rc) {
5149 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5150 rc);
5151 return rc;
5152 }
5153
5154 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5155 if (rc) {
5156 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5157 rc);
5158 return rc;
5159 }
5160 return rc;
5161}
5162
Michael Chanb664f002015-12-02 01:54:08 -05005163static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005164static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005165
Michael Chanc0c050c2015-10-22 16:01:17 -04005166static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5167{
Michael Chan7d2837d2016-05-04 16:56:44 -04005168 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005169 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005170 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005171
5172 if (irq_re_init) {
5173 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5174 if (rc) {
5175 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5176 rc);
5177 goto err_out;
5178 }
Michael Chan98fdbe72017-08-28 13:40:26 -04005179 if (bp->tx_reserved_rings != bp->tx_nr_rings) {
5180 int tx = bp->tx_nr_rings;
5181
5182 if (bnxt_hwrm_reserve_tx_rings(bp, &tx) ||
5183 tx < bp->tx_nr_rings) {
5184 rc = -ENOMEM;
5185 goto err_out;
5186 }
5187 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005188 }
5189
5190 rc = bnxt_hwrm_ring_alloc(bp);
5191 if (rc) {
5192 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5193 goto err_out;
5194 }
5195
5196 rc = bnxt_hwrm_ring_grp_alloc(bp);
5197 if (rc) {
5198 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5199 goto err_out;
5200 }
5201
Prashant Sreedharan76595192016-07-18 07:15:22 -04005202 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5203 rx_nr_rings--;
5204
Michael Chanc0c050c2015-10-22 16:01:17 -04005205 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005206 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005207 if (rc) {
5208 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5209 goto err_out;
5210 }
5211
5212 rc = bnxt_setup_vnic(bp, 0);
5213 if (rc)
5214 goto err_out;
5215
5216 if (bp->flags & BNXT_FLAG_RFS) {
5217 rc = bnxt_alloc_rfs_vnics(bp);
5218 if (rc)
5219 goto err_out;
5220 }
5221
5222 if (bp->flags & BNXT_FLAG_TPA) {
5223 rc = bnxt_set_tpa(bp, true);
5224 if (rc)
5225 goto err_out;
5226 }
5227
5228 if (BNXT_VF(bp))
5229 bnxt_update_vf_mac(bp);
5230
5231 /* Filter for default vnic 0 */
5232 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5233 if (rc) {
5234 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5235 goto err_out;
5236 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005237 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005238
Michael Chan7d2837d2016-05-04 16:56:44 -04005239 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005240
Michael Chan17c71ac2016-07-01 18:46:27 -04005241 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005242 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5243
5244 if (bp->dev->flags & IFF_ALLMULTI) {
5245 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5246 vnic->mc_list_count = 0;
5247 } else {
5248 u32 mask = 0;
5249
5250 bnxt_mc_list_updated(bp, &mask);
5251 vnic->rx_mask |= mask;
5252 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005253
Michael Chanb664f002015-12-02 01:54:08 -05005254 rc = bnxt_cfg_rx_mode(bp);
5255 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005256 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005257
5258 rc = bnxt_hwrm_set_coal(bp);
5259 if (rc)
5260 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005261 rc);
5262
5263 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5264 rc = bnxt_setup_nitroa0_vnic(bp);
5265 if (rc)
5266 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5267 rc);
5268 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005269
Michael Chancf6645f2016-06-13 02:25:28 -04005270 if (BNXT_VF(bp)) {
5271 bnxt_hwrm_func_qcfg(bp);
5272 netdev_update_features(bp->dev);
5273 }
5274
Michael Chanc0c050c2015-10-22 16:01:17 -04005275 return 0;
5276
5277err_out:
5278 bnxt_hwrm_resource_free(bp, 0, true);
5279
5280 return rc;
5281}
5282
5283static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5284{
5285 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5286 return 0;
5287}
5288
5289static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5290{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005291 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005292 bnxt_init_rx_rings(bp);
5293 bnxt_init_tx_rings(bp);
5294 bnxt_init_ring_grps(bp, irq_re_init);
5295 bnxt_init_vnics(bp);
5296
5297 return bnxt_init_chip(bp, irq_re_init);
5298}
5299
Michael Chanc0c050c2015-10-22 16:01:17 -04005300static int bnxt_set_real_num_queues(struct bnxt *bp)
5301{
5302 int rc;
5303 struct net_device *dev = bp->dev;
5304
Michael Chan5f449242017-02-06 16:55:40 -05005305 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5306 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005307 if (rc)
5308 return rc;
5309
5310 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5311 if (rc)
5312 return rc;
5313
5314#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005315 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005316 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005317#endif
5318
5319 return rc;
5320}
5321
Michael Chan6e6c5a52016-01-02 23:45:02 -05005322static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5323 bool shared)
5324{
5325 int _rx = *rx, _tx = *tx;
5326
5327 if (shared) {
5328 *rx = min_t(int, _rx, max);
5329 *tx = min_t(int, _tx, max);
5330 } else {
5331 if (max < 2)
5332 return -ENOMEM;
5333
5334 while (_rx + _tx > max) {
5335 if (_rx > _tx && _rx > 1)
5336 _rx--;
5337 else if (_tx > 1)
5338 _tx--;
5339 }
5340 *rx = _rx;
5341 *tx = _tx;
5342 }
5343 return 0;
5344}
5345
Michael Chan78095922016-12-07 00:26:16 -05005346static void bnxt_setup_msix(struct bnxt *bp)
5347{
5348 const int len = sizeof(bp->irq_tbl[0].name);
5349 struct net_device *dev = bp->dev;
5350 int tcs, i;
5351
5352 tcs = netdev_get_num_tc(dev);
5353 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005354 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005355
Michael Chand1e79252017-02-06 16:55:38 -05005356 for (i = 0; i < tcs; i++) {
5357 count = bp->tx_nr_rings_per_tc;
5358 off = i * count;
5359 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005360 }
5361 }
5362
5363 for (i = 0; i < bp->cp_nr_rings; i++) {
5364 char *attr;
5365
5366 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5367 attr = "TxRx";
5368 else if (i < bp->rx_nr_rings)
5369 attr = "rx";
5370 else
5371 attr = "tx";
5372
5373 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5374 i);
5375 bp->irq_tbl[i].handler = bnxt_msix;
5376 }
5377}
5378
5379static void bnxt_setup_inta(struct bnxt *bp)
5380{
5381 const int len = sizeof(bp->irq_tbl[0].name);
5382
5383 if (netdev_get_num_tc(bp->dev))
5384 netdev_reset_tc(bp->dev);
5385
5386 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5387 0);
5388 bp->irq_tbl[0].handler = bnxt_inta;
5389}
5390
5391static int bnxt_setup_int_mode(struct bnxt *bp)
5392{
5393 int rc;
5394
5395 if (bp->flags & BNXT_FLAG_USING_MSIX)
5396 bnxt_setup_msix(bp);
5397 else
5398 bnxt_setup_inta(bp);
5399
5400 rc = bnxt_set_real_num_queues(bp);
5401 return rc;
5402}
5403
Michael Chanb7429952017-01-13 01:32:00 -05005404#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005405static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5406{
5407#if defined(CONFIG_BNXT_SRIOV)
5408 if (BNXT_VF(bp))
5409 return bp->vf.max_rsscos_ctxs;
5410#endif
5411 return bp->pf.max_rsscos_ctxs;
5412}
5413
5414static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5415{
5416#if defined(CONFIG_BNXT_SRIOV)
5417 if (BNXT_VF(bp))
5418 return bp->vf.max_vnics;
5419#endif
5420 return bp->pf.max_vnics;
5421}
Michael Chanb7429952017-01-13 01:32:00 -05005422#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005423
Michael Chane4060d32016-12-07 00:26:19 -05005424unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5425{
5426#if defined(CONFIG_BNXT_SRIOV)
5427 if (BNXT_VF(bp))
5428 return bp->vf.max_stat_ctxs;
5429#endif
5430 return bp->pf.max_stat_ctxs;
5431}
5432
Michael Chana588e452016-12-07 00:26:21 -05005433void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5434{
5435#if defined(CONFIG_BNXT_SRIOV)
5436 if (BNXT_VF(bp))
5437 bp->vf.max_stat_ctxs = max;
5438 else
5439#endif
5440 bp->pf.max_stat_ctxs = max;
5441}
5442
Michael Chane4060d32016-12-07 00:26:19 -05005443unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5444{
5445#if defined(CONFIG_BNXT_SRIOV)
5446 if (BNXT_VF(bp))
5447 return bp->vf.max_cp_rings;
5448#endif
5449 return bp->pf.max_cp_rings;
5450}
5451
Michael Chana588e452016-12-07 00:26:21 -05005452void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5453{
5454#if defined(CONFIG_BNXT_SRIOV)
5455 if (BNXT_VF(bp))
5456 bp->vf.max_cp_rings = max;
5457 else
5458#endif
5459 bp->pf.max_cp_rings = max;
5460}
5461
Michael Chan78095922016-12-07 00:26:16 -05005462static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5463{
5464#if defined(CONFIG_BNXT_SRIOV)
5465 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005466 return min_t(unsigned int, bp->vf.max_irqs,
5467 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005468#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005469 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005470}
5471
Michael Chan33c26572016-12-07 00:26:15 -05005472void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5473{
5474#if defined(CONFIG_BNXT_SRIOV)
5475 if (BNXT_VF(bp))
5476 bp->vf.max_irqs = max_irqs;
5477 else
5478#endif
5479 bp->pf.max_irqs = max_irqs;
5480}
5481
Michael Chan78095922016-12-07 00:26:16 -05005482static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005483{
Michael Chan01657bc2016-01-02 23:45:03 -05005484 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005485 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005486
Michael Chan78095922016-12-07 00:26:16 -05005487 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005488 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5489 if (!msix_ent)
5490 return -ENOMEM;
5491
5492 for (i = 0; i < total_vecs; i++) {
5493 msix_ent[i].entry = i;
5494 msix_ent[i].vector = 0;
5495 }
5496
Michael Chan01657bc2016-01-02 23:45:03 -05005497 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5498 min = 2;
5499
5500 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005501 if (total_vecs < 0) {
5502 rc = -ENODEV;
5503 goto msix_setup_exit;
5504 }
5505
5506 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5507 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005508 for (i = 0; i < total_vecs; i++)
5509 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005510
Michael Chan78095922016-12-07 00:26:16 -05005511 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005512 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005513 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005514 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005515 if (rc)
5516 goto msix_setup_exit;
5517
Michael Chanc0c050c2015-10-22 16:01:17 -04005518 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005519 bp->cp_nr_rings = (min == 1) ?
5520 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5521 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005522
Michael Chanc0c050c2015-10-22 16:01:17 -04005523 } else {
5524 rc = -ENOMEM;
5525 goto msix_setup_exit;
5526 }
5527 bp->flags |= BNXT_FLAG_USING_MSIX;
5528 kfree(msix_ent);
5529 return 0;
5530
5531msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005532 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5533 kfree(bp->irq_tbl);
5534 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005535 pci_disable_msix(bp->pdev);
5536 kfree(msix_ent);
5537 return rc;
5538}
5539
Michael Chan78095922016-12-07 00:26:16 -05005540static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005541{
Michael Chanc0c050c2015-10-22 16:01:17 -04005542 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005543 if (!bp->irq_tbl)
5544 return -ENOMEM;
5545
5546 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005547 bp->rx_nr_rings = 1;
5548 bp->tx_nr_rings = 1;
5549 bp->cp_nr_rings = 1;
5550 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005551 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005552 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005553 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005554}
5555
Michael Chan78095922016-12-07 00:26:16 -05005556static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005557{
5558 int rc = 0;
5559
5560 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005561 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005562
Michael Chan1fa72e22016-04-25 02:30:49 -04005563 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005564 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005565 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005566 }
5567 return rc;
5568}
5569
Michael Chan78095922016-12-07 00:26:16 -05005570static void bnxt_clear_int_mode(struct bnxt *bp)
5571{
5572 if (bp->flags & BNXT_FLAG_USING_MSIX)
5573 pci_disable_msix(bp->pdev);
5574
5575 kfree(bp->irq_tbl);
5576 bp->irq_tbl = NULL;
5577 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5578}
5579
Michael Chanc0c050c2015-10-22 16:01:17 -04005580static void bnxt_free_irq(struct bnxt *bp)
5581{
5582 struct bnxt_irq *irq;
5583 int i;
5584
5585#ifdef CONFIG_RFS_ACCEL
5586 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5587 bp->dev->rx_cpu_rmap = NULL;
5588#endif
5589 if (!bp->irq_tbl)
5590 return;
5591
5592 for (i = 0; i < bp->cp_nr_rings; i++) {
5593 irq = &bp->irq_tbl[i];
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005594 if (irq->requested) {
5595 if (irq->have_cpumask) {
5596 irq_set_affinity_hint(irq->vector, NULL);
5597 free_cpumask_var(irq->cpu_mask);
5598 irq->have_cpumask = 0;
5599 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005600 free_irq(irq->vector, bp->bnapi[i]);
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005601 }
5602
Michael Chanc0c050c2015-10-22 16:01:17 -04005603 irq->requested = 0;
5604 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005605}
5606
5607static int bnxt_request_irq(struct bnxt *bp)
5608{
Michael Chanb81a90d2016-01-02 23:45:01 -05005609 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005610 unsigned long flags = 0;
5611#ifdef CONFIG_RFS_ACCEL
5612 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5613#endif
5614
5615 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5616 flags = IRQF_SHARED;
5617
Michael Chanb81a90d2016-01-02 23:45:01 -05005618 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005619 struct bnxt_irq *irq = &bp->irq_tbl[i];
5620#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005621 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005622 rc = irq_cpu_rmap_add(rmap, irq->vector);
5623 if (rc)
5624 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005625 j);
5626 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005627 }
5628#endif
5629 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5630 bp->bnapi[i]);
5631 if (rc)
5632 break;
5633
5634 irq->requested = 1;
Vasundhara Volam56f0fd82017-08-28 13:40:27 -04005635
5636 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
5637 int numa_node = dev_to_node(&bp->pdev->dev);
5638
5639 irq->have_cpumask = 1;
5640 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
5641 irq->cpu_mask);
5642 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
5643 if (rc) {
5644 netdev_warn(bp->dev,
5645 "Set affinity failed, IRQ = %d\n",
5646 irq->vector);
5647 break;
5648 }
5649 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005650 }
5651 return rc;
5652}
5653
5654static void bnxt_del_napi(struct bnxt *bp)
5655{
5656 int i;
5657
5658 if (!bp->bnapi)
5659 return;
5660
5661 for (i = 0; i < bp->cp_nr_rings; i++) {
5662 struct bnxt_napi *bnapi = bp->bnapi[i];
5663
5664 napi_hash_del(&bnapi->napi);
5665 netif_napi_del(&bnapi->napi);
5666 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005667 /* We called napi_hash_del() before netif_napi_del(), we need
5668 * to respect an RCU grace period before freeing napi structures.
5669 */
5670 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005671}
5672
5673static void bnxt_init_napi(struct bnxt *bp)
5674{
5675 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005676 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005677 struct bnxt_napi *bnapi;
5678
5679 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005680 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5681 cp_nr_rings--;
5682 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005683 bnapi = bp->bnapi[i];
5684 netif_napi_add(bp->dev, &bnapi->napi,
5685 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005686 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005687 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5688 bnapi = bp->bnapi[cp_nr_rings];
5689 netif_napi_add(bp->dev, &bnapi->napi,
5690 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005691 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005692 } else {
5693 bnapi = bp->bnapi[0];
5694 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005695 }
5696}
5697
5698static void bnxt_disable_napi(struct bnxt *bp)
5699{
5700 int i;
5701
5702 if (!bp->bnapi)
5703 return;
5704
Michael Chanb356a2e2016-12-29 12:13:31 -05005705 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005706 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005707}
5708
5709static void bnxt_enable_napi(struct bnxt *bp)
5710{
5711 int i;
5712
5713 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005714 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005715 napi_enable(&bp->bnapi[i]->napi);
5716 }
5717}
5718
Michael Chan7df4ae92016-12-02 21:17:17 -05005719void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005720{
5721 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005722 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04005723
Michael Chanb6ab4b02016-01-02 23:44:59 -05005724 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005725 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005726 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005727 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005728 }
5729 }
5730 /* Stop all TX queues */
5731 netif_tx_disable(bp->dev);
5732 netif_carrier_off(bp->dev);
5733}
5734
Michael Chan7df4ae92016-12-02 21:17:17 -05005735void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005736{
5737 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005738 struct bnxt_tx_ring_info *txr;
Michael Chanc0c050c2015-10-22 16:01:17 -04005739
5740 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005741 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005742 txr->dev_state = 0;
5743 }
5744 netif_tx_wake_all_queues(bp->dev);
5745 if (bp->link_info.link_up)
5746 netif_carrier_on(bp->dev);
5747}
5748
5749static void bnxt_report_link(struct bnxt *bp)
5750{
5751 if (bp->link_info.link_up) {
5752 const char *duplex;
5753 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005754 u32 speed;
5755 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005756
5757 netif_carrier_on(bp->dev);
5758 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5759 duplex = "full";
5760 else
5761 duplex = "half";
5762 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5763 flow_ctrl = "ON - receive & transmit";
5764 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5765 flow_ctrl = "ON - transmit";
5766 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5767 flow_ctrl = "ON - receive";
5768 else
5769 flow_ctrl = "none";
5770 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005771 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005772 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005773 if (bp->flags & BNXT_FLAG_EEE_CAP)
5774 netdev_info(bp->dev, "EEE is %s\n",
5775 bp->eee.eee_active ? "active" :
5776 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005777 fec = bp->link_info.fec_cfg;
5778 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5779 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5780 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5781 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5782 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005783 } else {
5784 netif_carrier_off(bp->dev);
5785 netdev_err(bp->dev, "NIC Link is Down\n");
5786 }
5787}
5788
Michael Chan170ce012016-04-05 14:08:57 -04005789static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5790{
5791 int rc = 0;
5792 struct hwrm_port_phy_qcaps_input req = {0};
5793 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005794 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005795
5796 if (bp->hwrm_spec_code < 0x10201)
5797 return 0;
5798
5799 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5800
5801 mutex_lock(&bp->hwrm_cmd_lock);
5802 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5803 if (rc)
5804 goto hwrm_phy_qcaps_exit;
5805
Michael Chanacb20052017-07-24 12:34:20 -04005806 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005807 struct ethtool_eee *eee = &bp->eee;
5808 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5809
5810 bp->flags |= BNXT_FLAG_EEE_CAP;
5811 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5812 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5813 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5814 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5815 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5816 }
Michael Chan520ad892017-03-08 18:44:35 -05005817 if (resp->supported_speeds_auto_mode)
5818 link_info->support_auto_speeds =
5819 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005820
Michael Chand5430d32017-08-28 13:40:31 -04005821 bp->port_count = resp->port_cnt;
5822
Michael Chan170ce012016-04-05 14:08:57 -04005823hwrm_phy_qcaps_exit:
5824 mutex_unlock(&bp->hwrm_cmd_lock);
5825 return rc;
5826}
5827
Michael Chanc0c050c2015-10-22 16:01:17 -04005828static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5829{
5830 int rc = 0;
5831 struct bnxt_link_info *link_info = &bp->link_info;
5832 struct hwrm_port_phy_qcfg_input req = {0};
5833 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5834 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005835 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005836
5837 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5838
5839 mutex_lock(&bp->hwrm_cmd_lock);
5840 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5841 if (rc) {
5842 mutex_unlock(&bp->hwrm_cmd_lock);
5843 return rc;
5844 }
5845
5846 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5847 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005848 link_info->duplex = resp->duplex_cfg;
5849 if (bp->hwrm_spec_code >= 0x10800)
5850 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005851 link_info->pause = resp->pause;
5852 link_info->auto_mode = resp->auto_mode;
5853 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005854 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005855 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005856 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005857 if (link_info->phy_link_status == BNXT_LINK_LINK)
5858 link_info->link_speed = le16_to_cpu(resp->link_speed);
5859 else
5860 link_info->link_speed = 0;
5861 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005862 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5863 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005864 link_info->lp_auto_link_speeds =
5865 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005866 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5867 link_info->phy_ver[0] = resp->phy_maj;
5868 link_info->phy_ver[1] = resp->phy_min;
5869 link_info->phy_ver[2] = resp->phy_bld;
5870 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005871 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005872 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005873 link_info->phy_addr = resp->eee_config_phy_addr &
5874 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005875 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005876
Michael Chan170ce012016-04-05 14:08:57 -04005877 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5878 struct ethtool_eee *eee = &bp->eee;
5879 u16 fw_speeds;
5880
5881 eee->eee_active = 0;
5882 if (resp->eee_config_phy_addr &
5883 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5884 eee->eee_active = 1;
5885 fw_speeds = le16_to_cpu(
5886 resp->link_partner_adv_eee_link_speed_mask);
5887 eee->lp_advertised =
5888 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5889 }
5890
5891 /* Pull initial EEE config */
5892 if (!chng_link_state) {
5893 if (resp->eee_config_phy_addr &
5894 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5895 eee->eee_enabled = 1;
5896
5897 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5898 eee->advertised =
5899 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5900
5901 if (resp->eee_config_phy_addr &
5902 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5903 __le32 tmr;
5904
5905 eee->tx_lpi_enabled = 1;
5906 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5907 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5908 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5909 }
5910 }
5911 }
Michael Chane70c7522017-02-12 19:18:16 -05005912
5913 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5914 if (bp->hwrm_spec_code >= 0x10504)
5915 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5916
Michael Chanc0c050c2015-10-22 16:01:17 -04005917 /* TODO: need to add more logic to report VF link */
5918 if (chng_link_state) {
5919 if (link_info->phy_link_status == BNXT_LINK_LINK)
5920 link_info->link_up = 1;
5921 else
5922 link_info->link_up = 0;
5923 if (link_up != link_info->link_up)
5924 bnxt_report_link(bp);
5925 } else {
5926 /* alwasy link down if not require to update link state */
5927 link_info->link_up = 0;
5928 }
5929 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005930
5931 diff = link_info->support_auto_speeds ^ link_info->advertising;
5932 if ((link_info->support_auto_speeds | diff) !=
5933 link_info->support_auto_speeds) {
5934 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005935 * update the advertisement settings. Caller holds RTNL
5936 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005937 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005938 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005939 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005940 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005941 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005942 return 0;
5943}
5944
Michael Chan10289be2016-05-15 03:04:49 -04005945static void bnxt_get_port_module_status(struct bnxt *bp)
5946{
5947 struct bnxt_link_info *link_info = &bp->link_info;
5948 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5949 u8 module_status;
5950
5951 if (bnxt_update_link(bp, true))
5952 return;
5953
5954 module_status = link_info->module_status;
5955 switch (module_status) {
5956 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5957 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5958 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5959 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5960 bp->pf.port_id);
5961 if (bp->hwrm_spec_code >= 0x10201) {
5962 netdev_warn(bp->dev, "Module part number %s\n",
5963 resp->phy_vendor_partnumber);
5964 }
5965 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5966 netdev_warn(bp->dev, "TX is disabled\n");
5967 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5968 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5969 }
5970}
5971
Michael Chanc0c050c2015-10-22 16:01:17 -04005972static void
5973bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5974{
5975 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005976 if (bp->hwrm_spec_code >= 0x10201)
5977 req->auto_pause =
5978 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005979 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5980 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5981 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005982 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005983 req->enables |=
5984 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5985 } else {
5986 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5987 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5988 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5989 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5990 req->enables |=
5991 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005992 if (bp->hwrm_spec_code >= 0x10201) {
5993 req->auto_pause = req->force_pause;
5994 req->enables |= cpu_to_le32(
5995 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5996 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005997 }
5998}
5999
6000static void bnxt_hwrm_set_link_common(struct bnxt *bp,
6001 struct hwrm_port_phy_cfg_input *req)
6002{
6003 u8 autoneg = bp->link_info.autoneg;
6004 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05006005 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04006006
6007 if (autoneg & BNXT_AUTONEG_SPEED) {
6008 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04006009 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04006010
6011 req->enables |= cpu_to_le32(
6012 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
6013 req->auto_link_speed_mask = cpu_to_le16(advertising);
6014
6015 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
6016 req->flags |=
6017 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
6018 } else {
6019 req->force_link_speed = cpu_to_le16(fw_link_speed);
6020 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
6021 }
6022
Michael Chanc0c050c2015-10-22 16:01:17 -04006023 /* tell chimp that the setting takes effect immediately */
6024 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
6025}
6026
6027int bnxt_hwrm_set_pause(struct bnxt *bp)
6028{
6029 struct hwrm_port_phy_cfg_input req = {0};
6030 int rc;
6031
6032 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6033 bnxt_hwrm_set_pause_common(bp, &req);
6034
6035 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
6036 bp->link_info.force_link_chng)
6037 bnxt_hwrm_set_link_common(bp, &req);
6038
6039 mutex_lock(&bp->hwrm_cmd_lock);
6040 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6041 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
6042 /* since changing of pause setting doesn't trigger any link
6043 * change event, the driver needs to update the current pause
6044 * result upon successfully return of the phy_cfg command
6045 */
6046 bp->link_info.pause =
6047 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
6048 bp->link_info.auto_pause_setting = 0;
6049 if (!bp->link_info.force_link_chng)
6050 bnxt_report_link(bp);
6051 }
6052 bp->link_info.force_link_chng = false;
6053 mutex_unlock(&bp->hwrm_cmd_lock);
6054 return rc;
6055}
6056
Michael Chan939f7f02016-04-05 14:08:58 -04006057static void bnxt_hwrm_set_eee(struct bnxt *bp,
6058 struct hwrm_port_phy_cfg_input *req)
6059{
6060 struct ethtool_eee *eee = &bp->eee;
6061
6062 if (eee->eee_enabled) {
6063 u16 eee_speeds;
6064 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
6065
6066 if (eee->tx_lpi_enabled)
6067 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
6068 else
6069 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
6070
6071 req->flags |= cpu_to_le32(flags);
6072 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
6073 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
6074 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
6075 } else {
6076 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
6077 }
6078}
6079
6080int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04006081{
6082 struct hwrm_port_phy_cfg_input req = {0};
6083
6084 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6085 if (set_pause)
6086 bnxt_hwrm_set_pause_common(bp, &req);
6087
6088 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04006089
6090 if (set_eee)
6091 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04006092 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6093}
6094
Michael Chan33f7d552016-04-11 04:11:12 -04006095static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6096{
6097 struct hwrm_port_phy_cfg_input req = {0};
6098
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006099 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04006100 return 0;
6101
6102 if (pci_num_vf(bp->pdev))
6103 return 0;
6104
6105 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05006106 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04006107 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6108}
6109
Michael Chan5ad2cbe2017-01-13 01:32:03 -05006110static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6111{
6112 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6113 struct hwrm_port_led_qcaps_input req = {0};
6114 struct bnxt_pf_info *pf = &bp->pf;
6115 int rc;
6116
6117 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6118 return 0;
6119
6120 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6121 req.port_id = cpu_to_le16(pf->port_id);
6122 mutex_lock(&bp->hwrm_cmd_lock);
6123 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6124 if (rc) {
6125 mutex_unlock(&bp->hwrm_cmd_lock);
6126 return rc;
6127 }
6128 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6129 int i;
6130
6131 bp->num_leds = resp->num_leds;
6132 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6133 bp->num_leds);
6134 for (i = 0; i < bp->num_leds; i++) {
6135 struct bnxt_led_info *led = &bp->leds[i];
6136 __le16 caps = led->led_state_caps;
6137
6138 if (!led->led_group_id ||
6139 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6140 bp->num_leds = 0;
6141 break;
6142 }
6143 }
6144 }
6145 mutex_unlock(&bp->hwrm_cmd_lock);
6146 return 0;
6147}
6148
Michael Chan5282db62017-04-04 18:14:10 -04006149int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6150{
6151 struct hwrm_wol_filter_alloc_input req = {0};
6152 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6153 int rc;
6154
6155 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6156 req.port_id = cpu_to_le16(bp->pf.port_id);
6157 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6158 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6159 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6160 mutex_lock(&bp->hwrm_cmd_lock);
6161 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6162 if (!rc)
6163 bp->wol_filter_id = resp->wol_filter_id;
6164 mutex_unlock(&bp->hwrm_cmd_lock);
6165 return rc;
6166}
6167
6168int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6169{
6170 struct hwrm_wol_filter_free_input req = {0};
6171 int rc;
6172
6173 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6174 req.port_id = cpu_to_le16(bp->pf.port_id);
6175 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6176 req.wol_filter_id = bp->wol_filter_id;
6177 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6178 return rc;
6179}
6180
Michael Chanc1ef1462017-04-04 18:14:07 -04006181static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6182{
6183 struct hwrm_wol_filter_qcfg_input req = {0};
6184 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6185 u16 next_handle = 0;
6186 int rc;
6187
6188 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6189 req.port_id = cpu_to_le16(bp->pf.port_id);
6190 req.handle = cpu_to_le16(handle);
6191 mutex_lock(&bp->hwrm_cmd_lock);
6192 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6193 if (!rc) {
6194 next_handle = le16_to_cpu(resp->next_handle);
6195 if (next_handle != 0) {
6196 if (resp->wol_type ==
6197 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6198 bp->wol = 1;
6199 bp->wol_filter_id = resp->wol_filter_id;
6200 }
6201 }
6202 }
6203 mutex_unlock(&bp->hwrm_cmd_lock);
6204 return next_handle;
6205}
6206
6207static void bnxt_get_wol_settings(struct bnxt *bp)
6208{
6209 u16 handle = 0;
6210
6211 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6212 return;
6213
6214 do {
6215 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6216 } while (handle && handle != 0xffff);
6217}
6218
Michael Chan939f7f02016-04-05 14:08:58 -04006219static bool bnxt_eee_config_ok(struct bnxt *bp)
6220{
6221 struct ethtool_eee *eee = &bp->eee;
6222 struct bnxt_link_info *link_info = &bp->link_info;
6223
6224 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6225 return true;
6226
6227 if (eee->eee_enabled) {
6228 u32 advertising =
6229 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6230
6231 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6232 eee->eee_enabled = 0;
6233 return false;
6234 }
6235 if (eee->advertised & ~advertising) {
6236 eee->advertised = advertising & eee->supported;
6237 return false;
6238 }
6239 }
6240 return true;
6241}
6242
Michael Chanc0c050c2015-10-22 16:01:17 -04006243static int bnxt_update_phy_setting(struct bnxt *bp)
6244{
6245 int rc;
6246 bool update_link = false;
6247 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006248 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006249 struct bnxt_link_info *link_info = &bp->link_info;
6250
6251 rc = bnxt_update_link(bp, true);
6252 if (rc) {
6253 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6254 rc);
6255 return rc;
6256 }
Michael Chan33dac242017-02-12 19:18:15 -05006257 if (!BNXT_SINGLE_PF(bp))
6258 return 0;
6259
Michael Chanc0c050c2015-10-22 16:01:17 -04006260 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006261 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6262 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006263 update_pause = true;
6264 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6265 link_info->force_pause_setting != link_info->req_flow_ctrl)
6266 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006267 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6268 if (BNXT_AUTO_MODE(link_info->auto_mode))
6269 update_link = true;
6270 if (link_info->req_link_speed != link_info->force_link_speed)
6271 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006272 if (link_info->req_duplex != link_info->duplex_setting)
6273 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006274 } else {
6275 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6276 update_link = true;
6277 if (link_info->advertising != link_info->auto_link_speeds)
6278 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006279 }
6280
Michael Chan16d663a2016-11-16 21:13:07 -05006281 /* The last close may have shutdown the link, so need to call
6282 * PHY_CFG to bring it back up.
6283 */
6284 if (!netif_carrier_ok(bp->dev))
6285 update_link = true;
6286
Michael Chan939f7f02016-04-05 14:08:58 -04006287 if (!bnxt_eee_config_ok(bp))
6288 update_eee = true;
6289
Michael Chanc0c050c2015-10-22 16:01:17 -04006290 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006291 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006292 else if (update_pause)
6293 rc = bnxt_hwrm_set_pause(bp);
6294 if (rc) {
6295 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6296 rc);
6297 return rc;
6298 }
6299
6300 return rc;
6301}
6302
Jeffrey Huang11809492015-11-05 16:25:49 -05006303/* Common routine to pre-map certain register block to different GRC window.
6304 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6305 * in PF and 3 windows in VF that can be customized to map in different
6306 * register blocks.
6307 */
6308static void bnxt_preset_reg_win(struct bnxt *bp)
6309{
6310 if (BNXT_PF(bp)) {
6311 /* CAG registers map to GRC window #4 */
6312 writel(BNXT_CAG_REG_BASE,
6313 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6314 }
6315}
6316
Michael Chanc0c050c2015-10-22 16:01:17 -04006317static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6318{
6319 int rc = 0;
6320
Jeffrey Huang11809492015-11-05 16:25:49 -05006321 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006322 netif_carrier_off(bp->dev);
6323 if (irq_re_init) {
6324 rc = bnxt_setup_int_mode(bp);
6325 if (rc) {
6326 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6327 rc);
6328 return rc;
6329 }
6330 }
6331 if ((bp->flags & BNXT_FLAG_RFS) &&
6332 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6333 /* disable RFS if falling back to INTA */
6334 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6335 bp->flags &= ~BNXT_FLAG_RFS;
6336 }
6337
6338 rc = bnxt_alloc_mem(bp, irq_re_init);
6339 if (rc) {
6340 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6341 goto open_err_free_mem;
6342 }
6343
6344 if (irq_re_init) {
6345 bnxt_init_napi(bp);
6346 rc = bnxt_request_irq(bp);
6347 if (rc) {
6348 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6349 goto open_err;
6350 }
6351 }
6352
6353 bnxt_enable_napi(bp);
6354
6355 rc = bnxt_init_nic(bp, irq_re_init);
6356 if (rc) {
6357 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6358 goto open_err;
6359 }
6360
6361 if (link_re_init) {
Michael Chane2dc9b62017-10-13 21:09:30 -04006362 mutex_lock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006363 rc = bnxt_update_phy_setting(bp);
Michael Chane2dc9b62017-10-13 21:09:30 -04006364 mutex_unlock(&bp->link_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04006365 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006366 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006367 }
6368
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006369 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006370 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006371
Michael Chancaefe522015-12-09 19:35:42 -05006372 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006373 bnxt_enable_int(bp);
6374 /* Enable TX queues */
6375 bnxt_tx_enable(bp);
6376 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006377 /* Poll link status and check for SFP+ module status */
6378 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006379
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006380 /* VF-reps may need to be re-opened after the PF is re-opened */
6381 if (BNXT_PF(bp))
6382 bnxt_vf_reps_open(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006383 return 0;
6384
6385open_err:
6386 bnxt_disable_napi(bp);
6387 bnxt_del_napi(bp);
6388
6389open_err_free_mem:
6390 bnxt_free_skbs(bp);
6391 bnxt_free_irq(bp);
6392 bnxt_free_mem(bp, true);
6393 return rc;
6394}
6395
6396/* rtnl_lock held */
6397int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6398{
6399 int rc = 0;
6400
6401 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6402 if (rc) {
6403 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6404 dev_close(bp->dev);
6405 }
6406 return rc;
6407}
6408
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006409/* rtnl_lock held, open the NIC half way by allocating all resources, but
6410 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6411 * self tests.
6412 */
6413int bnxt_half_open_nic(struct bnxt *bp)
6414{
6415 int rc = 0;
6416
6417 rc = bnxt_alloc_mem(bp, false);
6418 if (rc) {
6419 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6420 goto half_open_err;
6421 }
6422 rc = bnxt_init_nic(bp, false);
6423 if (rc) {
6424 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6425 goto half_open_err;
6426 }
6427 return 0;
6428
6429half_open_err:
6430 bnxt_free_skbs(bp);
6431 bnxt_free_mem(bp, false);
6432 dev_close(bp->dev);
6433 return rc;
6434}
6435
6436/* rtnl_lock held, this call can only be made after a previous successful
6437 * call to bnxt_half_open_nic().
6438 */
6439void bnxt_half_close_nic(struct bnxt *bp)
6440{
6441 bnxt_hwrm_resource_free(bp, false, false);
6442 bnxt_free_skbs(bp);
6443 bnxt_free_mem(bp, false);
6444}
6445
Michael Chanc0c050c2015-10-22 16:01:17 -04006446static int bnxt_open(struct net_device *dev)
6447{
6448 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006449
Michael Chanc0c050c2015-10-22 16:01:17 -04006450 return __bnxt_open_nic(bp, true, true);
6451}
6452
Michael Chanf9b76eb2017-07-11 13:05:34 -04006453static bool bnxt_drv_busy(struct bnxt *bp)
6454{
6455 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6456 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6457}
6458
Michael Chanc0c050c2015-10-22 16:01:17 -04006459int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6460{
6461 int rc = 0;
6462
6463#ifdef CONFIG_BNXT_SRIOV
6464 if (bp->sriov_cfg) {
6465 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6466 !bp->sriov_cfg,
6467 BNXT_SRIOV_CFG_WAIT_TMO);
6468 if (rc)
6469 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6470 }
Sathya Perlaee5c7fb2017-07-24 12:34:28 -04006471
6472 /* Close the VF-reps before closing PF */
6473 if (BNXT_PF(bp))
6474 bnxt_vf_reps_close(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006475#endif
6476 /* Change device state to avoid TX queue wake up's */
6477 bnxt_tx_disable(bp);
6478
Michael Chancaefe522015-12-09 19:35:42 -05006479 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006480 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006481 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006482 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006483
Michael Chan9d8bc092016-12-29 12:13:33 -05006484 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006485 bnxt_shutdown_nic(bp, irq_re_init);
6486
6487 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6488
6489 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006490 del_timer_sync(&bp->timer);
6491 bnxt_free_skbs(bp);
6492
6493 if (irq_re_init) {
6494 bnxt_free_irq(bp);
6495 bnxt_del_napi(bp);
6496 }
6497 bnxt_free_mem(bp, irq_re_init);
6498 return rc;
6499}
6500
6501static int bnxt_close(struct net_device *dev)
6502{
6503 struct bnxt *bp = netdev_priv(dev);
6504
6505 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006506 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006507 return 0;
6508}
6509
6510/* rtnl_lock held */
6511static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6512{
6513 switch (cmd) {
6514 case SIOCGMIIPHY:
6515 /* fallthru */
6516 case SIOCGMIIREG: {
6517 if (!netif_running(dev))
6518 return -EAGAIN;
6519
6520 return 0;
6521 }
6522
6523 case SIOCSMIIREG:
6524 if (!netif_running(dev))
6525 return -EAGAIN;
6526
6527 return 0;
6528
6529 default:
6530 /* do nothing */
6531 break;
6532 }
6533 return -EOPNOTSUPP;
6534}
6535
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006536static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006537bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6538{
6539 u32 i;
6540 struct bnxt *bp = netdev_priv(dev);
6541
Michael Chanf9b76eb2017-07-11 13:05:34 -04006542 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6543 /* Make sure bnxt_close_nic() sees that we are reading stats before
6544 * we check the BNXT_STATE_OPEN flag.
6545 */
6546 smp_mb__after_atomic();
6547 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6548 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006549 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006550 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006551
6552 /* TODO check if we need to synchronize with bnxt_close path */
6553 for (i = 0; i < bp->cp_nr_rings; i++) {
6554 struct bnxt_napi *bnapi = bp->bnapi[i];
6555 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6556 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6557
6558 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6559 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6560 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6561
6562 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6563 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6564 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6565
6566 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6567 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6568 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6569
6570 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6571 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6572 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6573
6574 stats->rx_missed_errors +=
6575 le64_to_cpu(hw_stats->rx_discard_pkts);
6576
6577 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6578
Michael Chanc0c050c2015-10-22 16:01:17 -04006579 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6580 }
6581
Michael Chan9947f832016-03-07 15:38:46 -05006582 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6583 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6584 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6585
6586 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6587 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6588 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6589 le64_to_cpu(rx->rx_ovrsz_frames) +
6590 le64_to_cpu(rx->rx_runt_frames);
6591 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6592 le64_to_cpu(rx->rx_jbr_frames);
6593 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6594 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6595 stats->tx_errors = le64_to_cpu(tx->tx_err);
6596 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006597 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006598}
6599
6600static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6601{
6602 struct net_device *dev = bp->dev;
6603 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6604 struct netdev_hw_addr *ha;
6605 u8 *haddr;
6606 int mc_count = 0;
6607 bool update = false;
6608 int off = 0;
6609
6610 netdev_for_each_mc_addr(ha, dev) {
6611 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6612 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6613 vnic->mc_list_count = 0;
6614 return false;
6615 }
6616 haddr = ha->addr;
6617 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6618 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6619 update = true;
6620 }
6621 off += ETH_ALEN;
6622 mc_count++;
6623 }
6624 if (mc_count)
6625 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6626
6627 if (mc_count != vnic->mc_list_count) {
6628 vnic->mc_list_count = mc_count;
6629 update = true;
6630 }
6631 return update;
6632}
6633
6634static bool bnxt_uc_list_updated(struct bnxt *bp)
6635{
6636 struct net_device *dev = bp->dev;
6637 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6638 struct netdev_hw_addr *ha;
6639 int off = 0;
6640
6641 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6642 return true;
6643
6644 netdev_for_each_uc_addr(ha, dev) {
6645 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6646 return true;
6647
6648 off += ETH_ALEN;
6649 }
6650 return false;
6651}
6652
6653static void bnxt_set_rx_mode(struct net_device *dev)
6654{
6655 struct bnxt *bp = netdev_priv(dev);
6656 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6657 u32 mask = vnic->rx_mask;
6658 bool mc_update = false;
6659 bool uc_update;
6660
6661 if (!netif_running(dev))
6662 return;
6663
6664 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6665 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6666 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6667
Michael Chan17c71ac2016-07-01 18:46:27 -04006668 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006669 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6670
6671 uc_update = bnxt_uc_list_updated(bp);
6672
6673 if (dev->flags & IFF_ALLMULTI) {
6674 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6675 vnic->mc_list_count = 0;
6676 } else {
6677 mc_update = bnxt_mc_list_updated(bp, &mask);
6678 }
6679
6680 if (mask != vnic->rx_mask || uc_update || mc_update) {
6681 vnic->rx_mask = mask;
6682
6683 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04006684 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006685 }
6686}
6687
Michael Chanb664f002015-12-02 01:54:08 -05006688static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006689{
6690 struct net_device *dev = bp->dev;
6691 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6692 struct netdev_hw_addr *ha;
6693 int i, off = 0, rc;
6694 bool uc_update;
6695
6696 netif_addr_lock_bh(dev);
6697 uc_update = bnxt_uc_list_updated(bp);
6698 netif_addr_unlock_bh(dev);
6699
6700 if (!uc_update)
6701 goto skip_uc;
6702
6703 mutex_lock(&bp->hwrm_cmd_lock);
6704 for (i = 1; i < vnic->uc_filter_count; i++) {
6705 struct hwrm_cfa_l2_filter_free_input req = {0};
6706
6707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6708 -1);
6709
6710 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6711
6712 rc = _hwrm_send_message(bp, &req, sizeof(req),
6713 HWRM_CMD_TIMEOUT);
6714 }
6715 mutex_unlock(&bp->hwrm_cmd_lock);
6716
6717 vnic->uc_filter_count = 1;
6718
6719 netif_addr_lock_bh(dev);
6720 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6721 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6722 } else {
6723 netdev_for_each_uc_addr(ha, dev) {
6724 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6725 off += ETH_ALEN;
6726 vnic->uc_filter_count++;
6727 }
6728 }
6729 netif_addr_unlock_bh(dev);
6730
6731 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6732 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6733 if (rc) {
6734 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6735 rc);
6736 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006737 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006738 }
6739 }
6740
6741skip_uc:
6742 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6743 if (rc)
6744 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6745 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006746
6747 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006748}
6749
Michael Chan8079e8f2016-12-29 12:13:37 -05006750/* If the chip and firmware supports RFS */
6751static bool bnxt_rfs_supported(struct bnxt *bp)
6752{
6753 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6754 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006755 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6756 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006757 return false;
6758}
6759
6760/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006761static bool bnxt_rfs_capable(struct bnxt *bp)
6762{
6763#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006764 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006765
Michael Chan964fd482017-02-12 19:18:13 -05006766 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006767 return false;
6768
6769 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006770 max_vnics = bnxt_get_max_func_vnics(bp);
6771 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006772
6773 /* RSS contexts not a limiting factor */
6774 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6775 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006776 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006777 netdev_warn(bp->dev,
6778 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006779 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006780 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006781 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006782
6783 return true;
6784#else
6785 return false;
6786#endif
6787}
6788
Michael Chanc0c050c2015-10-22 16:01:17 -04006789static netdev_features_t bnxt_fix_features(struct net_device *dev,
6790 netdev_features_t features)
6791{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006792 struct bnxt *bp = netdev_priv(dev);
6793
Vasundhara Volama2304902016-07-25 12:33:36 -04006794 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006795 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006796
6797 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6798 * turned on or off together.
6799 */
6800 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6801 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6802 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6803 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6804 NETIF_F_HW_VLAN_STAG_RX);
6805 else
6806 features |= NETIF_F_HW_VLAN_CTAG_RX |
6807 NETIF_F_HW_VLAN_STAG_RX;
6808 }
Michael Chancf6645f2016-06-13 02:25:28 -04006809#ifdef CONFIG_BNXT_SRIOV
6810 if (BNXT_VF(bp)) {
6811 if (bp->vf.vlan) {
6812 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6813 NETIF_F_HW_VLAN_STAG_RX);
6814 }
6815 }
6816#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006817 return features;
6818}
6819
6820static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6821{
6822 struct bnxt *bp = netdev_priv(dev);
6823 u32 flags = bp->flags;
6824 u32 changes;
6825 int rc = 0;
6826 bool re_init = false;
6827 bool update_tpa = false;
6828
6829 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006830 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006831 flags |= BNXT_FLAG_GRO;
6832 if (features & NETIF_F_LRO)
6833 flags |= BNXT_FLAG_LRO;
6834
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006835 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6836 flags &= ~BNXT_FLAG_TPA;
6837
Michael Chanc0c050c2015-10-22 16:01:17 -04006838 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6839 flags |= BNXT_FLAG_STRIP_VLAN;
6840
6841 if (features & NETIF_F_NTUPLE)
6842 flags |= BNXT_FLAG_RFS;
6843
6844 changes = flags ^ bp->flags;
6845 if (changes & BNXT_FLAG_TPA) {
6846 update_tpa = true;
6847 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6848 (flags & BNXT_FLAG_TPA) == 0)
6849 re_init = true;
6850 }
6851
6852 if (changes & ~BNXT_FLAG_TPA)
6853 re_init = true;
6854
6855 if (flags != bp->flags) {
6856 u32 old_flags = bp->flags;
6857
6858 bp->flags = flags;
6859
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006860 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006861 if (update_tpa)
6862 bnxt_set_ring_params(bp);
6863 return rc;
6864 }
6865
6866 if (re_init) {
6867 bnxt_close_nic(bp, false, false);
6868 if (update_tpa)
6869 bnxt_set_ring_params(bp);
6870
6871 return bnxt_open_nic(bp, false, false);
6872 }
6873 if (update_tpa) {
6874 rc = bnxt_set_tpa(bp,
6875 (flags & BNXT_FLAG_TPA) ?
6876 true : false);
6877 if (rc)
6878 bp->flags = old_flags;
6879 }
6880 }
6881 return rc;
6882}
6883
Michael Chan9f554592016-01-02 23:44:58 -05006884static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6885{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006886 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006887 int i = bnapi->index;
6888
Michael Chan3b2b7d92016-01-02 23:45:00 -05006889 if (!txr)
6890 return;
6891
Michael Chan9f554592016-01-02 23:44:58 -05006892 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6893 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6894 txr->tx_cons);
6895}
6896
6897static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6898{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006899 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006900 int i = bnapi->index;
6901
Michael Chan3b2b7d92016-01-02 23:45:00 -05006902 if (!rxr)
6903 return;
6904
Michael Chan9f554592016-01-02 23:44:58 -05006905 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",
6906 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6907 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6908 rxr->rx_sw_agg_prod);
6909}
6910
6911static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6912{
6913 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6914 int i = bnapi->index;
6915
6916 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6917 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6918}
6919
Michael Chanc0c050c2015-10-22 16:01:17 -04006920static void bnxt_dbg_dump_states(struct bnxt *bp)
6921{
6922 int i;
6923 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006924
6925 for (i = 0; i < bp->cp_nr_rings; i++) {
6926 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006927 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006928 bnxt_dump_tx_sw_state(bnapi);
6929 bnxt_dump_rx_sw_state(bnapi);
6930 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006931 }
6932 }
6933}
6934
Michael Chan6988bd92016-06-13 02:25:29 -04006935static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006936{
Michael Chan6988bd92016-06-13 02:25:29 -04006937 if (!silent)
6938 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006939 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006940 int rc;
6941
6942 if (!silent)
6943 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006944 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006945 rc = bnxt_open_nic(bp, false, false);
6946 if (!silent && !rc)
6947 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006948 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006949}
6950
6951static void bnxt_tx_timeout(struct net_device *dev)
6952{
6953 struct bnxt *bp = netdev_priv(dev);
6954
6955 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6956 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04006957 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006958}
6959
6960#ifdef CONFIG_NET_POLL_CONTROLLER
6961static void bnxt_poll_controller(struct net_device *dev)
6962{
6963 struct bnxt *bp = netdev_priv(dev);
6964 int i;
6965
Michael Chan2270bc52017-06-23 14:01:01 -04006966 /* Only process tx rings/combined rings in netpoll mode. */
6967 for (i = 0; i < bp->tx_nr_rings; i++) {
6968 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006969
Michael Chan2270bc52017-06-23 14:01:01 -04006970 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006971 }
6972}
6973#endif
6974
6975static void bnxt_timer(unsigned long data)
6976{
6977 struct bnxt *bp = (struct bnxt *)data;
6978 struct net_device *dev = bp->dev;
6979
6980 if (!netif_running(dev))
6981 return;
6982
6983 if (atomic_read(&bp->intr_sem) != 0)
6984 goto bnxt_restart_timer;
6985
Michael Chanadcc3312017-07-24 12:34:24 -04006986 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
6987 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05006988 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04006989 bnxt_queue_sp_work(bp);
Michael Chan3bdf56c2016-03-07 15:38:45 -05006990 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006991bnxt_restart_timer:
6992 mod_timer(&bp->timer, jiffies + bp->current_interval);
6993}
6994
Michael Chana551ee92017-01-25 02:55:07 -05006995static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006996{
Michael Chana551ee92017-01-25 02:55:07 -05006997 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6998 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006999 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
7000 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
7001 */
7002 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7003 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05007004}
7005
7006static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
7007{
Michael Chan6988bd92016-06-13 02:25:29 -04007008 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7009 rtnl_unlock();
7010}
7011
Michael Chana551ee92017-01-25 02:55:07 -05007012/* Only called from bnxt_sp_task() */
7013static void bnxt_reset(struct bnxt *bp, bool silent)
7014{
7015 bnxt_rtnl_lock_sp(bp);
7016 if (test_bit(BNXT_STATE_OPEN, &bp->state))
7017 bnxt_reset_task(bp, silent);
7018 bnxt_rtnl_unlock_sp(bp);
7019}
7020
Michael Chanc0c050c2015-10-22 16:01:17 -04007021static void bnxt_cfg_ntp_filters(struct bnxt *);
7022
7023static void bnxt_sp_task(struct work_struct *work)
7024{
7025 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007026
Michael Chan4cebdce2015-12-09 19:35:43 -05007027 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7028 smp_mb__after_atomic();
7029 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7030 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007031 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05007032 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007033
7034 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
7035 bnxt_cfg_rx_mode(bp);
7036
7037 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
7038 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007039 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
7040 bnxt_hwrm_exec_fwd_req(bp);
7041 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7042 bnxt_hwrm_tunnel_dst_port_alloc(
7043 bp, bp->vxlan_port,
7044 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7045 }
7046 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7047 bnxt_hwrm_tunnel_dst_port_free(
7048 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7049 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007050 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7051 bnxt_hwrm_tunnel_dst_port_alloc(
7052 bp, bp->nge_port,
7053 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7054 }
7055 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7056 bnxt_hwrm_tunnel_dst_port_free(
7057 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7058 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05007059 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
7060 bnxt_hwrm_port_qstats(bp);
7061
Michael Chan0eaa24b2017-01-25 02:55:08 -05007062 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007063 int rc;
Michael Chan0eaa24b2017-01-25 02:55:08 -05007064
Michael Chane2dc9b62017-10-13 21:09:30 -04007065 mutex_lock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007066 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
7067 &bp->sp_event))
7068 bnxt_hwrm_phy_qcaps(bp);
7069
Michael Chane2dc9b62017-10-13 21:09:30 -04007070 rc = bnxt_update_link(bp, true);
7071 mutex_unlock(&bp->link_lock);
Michael Chan0eaa24b2017-01-25 02:55:08 -05007072 if (rc)
7073 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
7074 rc);
7075 }
Michael Chan90c694b2017-01-25 02:55:09 -05007076 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
Michael Chane2dc9b62017-10-13 21:09:30 -04007077 mutex_lock(&bp->link_lock);
7078 bnxt_get_port_module_status(bp);
7079 mutex_unlock(&bp->link_lock);
Michael Chan90c694b2017-01-25 02:55:09 -05007080 }
Michael Chane2dc9b62017-10-13 21:09:30 -04007081 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
7082 * must be the last functions to be called before exiting.
7083 */
Michael Chanc0c050c2015-10-22 16:01:17 -04007084 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
7085 bnxt_reset(bp, false);
7086
7087 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
7088 bnxt_reset(bp, true);
7089
Michael Chanc0c050c2015-10-22 16:01:17 -04007090 smp_mb__before_atomic();
7091 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7092}
7093
Michael Chand1e79252017-02-06 16:55:38 -05007094/* Under rtnl_lock */
Michael Chan98fdbe72017-08-28 13:40:26 -04007095int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7096 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05007097{
7098 int max_rx, max_tx, tx_sets = 1;
7099 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05007100 int rc;
7101
Michael Chand1e79252017-02-06 16:55:38 -05007102 if (tcs)
7103 tx_sets = tcs;
7104
7105 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7106 if (rc)
7107 return rc;
7108
7109 if (max_rx < rx)
7110 return -ENOMEM;
7111
Michael Chan5f449242017-02-06 16:55:40 -05007112 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05007113 if (max_tx < tx_rings_needed)
7114 return -ENOMEM;
7115
Michael Chan98fdbe72017-08-28 13:40:26 -04007116 return bnxt_hwrm_check_tx_rings(bp, tx_rings_needed);
Michael Chand1e79252017-02-06 16:55:38 -05007117}
7118
Sathya Perla17086392017-02-20 19:25:18 -05007119static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7120{
7121 if (bp->bar2) {
7122 pci_iounmap(pdev, bp->bar2);
7123 bp->bar2 = NULL;
7124 }
7125
7126 if (bp->bar1) {
7127 pci_iounmap(pdev, bp->bar1);
7128 bp->bar1 = NULL;
7129 }
7130
7131 if (bp->bar0) {
7132 pci_iounmap(pdev, bp->bar0);
7133 bp->bar0 = NULL;
7134 }
7135}
7136
7137static void bnxt_cleanup_pci(struct bnxt *bp)
7138{
7139 bnxt_unmap_bars(bp, bp->pdev);
7140 pci_release_regions(bp->pdev);
7141 pci_disable_device(bp->pdev);
7142}
7143
Michael Chanc0c050c2015-10-22 16:01:17 -04007144static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7145{
7146 int rc;
7147 struct bnxt *bp = netdev_priv(dev);
7148
7149 SET_NETDEV_DEV(dev, &pdev->dev);
7150
7151 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7152 rc = pci_enable_device(pdev);
7153 if (rc) {
7154 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7155 goto init_err;
7156 }
7157
7158 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7159 dev_err(&pdev->dev,
7160 "Cannot find PCI device base address, aborting\n");
7161 rc = -ENODEV;
7162 goto init_err_disable;
7163 }
7164
7165 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7166 if (rc) {
7167 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7168 goto init_err_disable;
7169 }
7170
7171 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7172 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7173 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7174 goto init_err_disable;
7175 }
7176
7177 pci_set_master(pdev);
7178
7179 bp->dev = dev;
7180 bp->pdev = pdev;
7181
7182 bp->bar0 = pci_ioremap_bar(pdev, 0);
7183 if (!bp->bar0) {
7184 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7185 rc = -ENOMEM;
7186 goto init_err_release;
7187 }
7188
7189 bp->bar1 = pci_ioremap_bar(pdev, 2);
7190 if (!bp->bar1) {
7191 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7192 rc = -ENOMEM;
7193 goto init_err_release;
7194 }
7195
7196 bp->bar2 = pci_ioremap_bar(pdev, 4);
7197 if (!bp->bar2) {
7198 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7199 rc = -ENOMEM;
7200 goto init_err_release;
7201 }
7202
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007203 pci_enable_pcie_error_reporting(pdev);
7204
Michael Chanc0c050c2015-10-22 16:01:17 -04007205 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7206
7207 spin_lock_init(&bp->ntp_fltr_lock);
7208
7209 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7210 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7211
Michael Chandfb5b892016-02-26 04:00:01 -05007212 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007213 bp->rx_coal_ticks = 12;
7214 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007215 bp->rx_coal_ticks_irq = 1;
7216 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007217
Michael Chandfc9c942016-02-26 04:00:03 -05007218 bp->tx_coal_ticks = 25;
7219 bp->tx_coal_bufs = 30;
7220 bp->tx_coal_ticks_irq = 2;
7221 bp->tx_coal_bufs_irq = 2;
7222
Michael Chan51f30782016-07-01 18:46:29 -04007223 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7224
Allen Pais6c438242017-09-21 22:35:08 +05307225 setup_timer(&bp->timer, bnxt_timer, (unsigned long)bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007226 bp->current_interval = BNXT_TIMER_INTERVAL;
7227
Michael Chancaefe522015-12-09 19:35:42 -05007228 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007229 return 0;
7230
7231init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007232 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007233 pci_release_regions(pdev);
7234
7235init_err_disable:
7236 pci_disable_device(pdev);
7237
7238init_err:
7239 return rc;
7240}
7241
7242/* rtnl_lock held */
7243static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7244{
7245 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007246 struct bnxt *bp = netdev_priv(dev);
7247 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007248
7249 if (!is_valid_ether_addr(addr->sa_data))
7250 return -EADDRNOTAVAIL;
7251
Michael Chan84c33dd2016-04-11 04:11:13 -04007252 rc = bnxt_approve_mac(bp, addr->sa_data);
7253 if (rc)
7254 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007255
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007256 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7257 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007258
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007259 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7260 if (netif_running(dev)) {
7261 bnxt_close_nic(bp, false, false);
7262 rc = bnxt_open_nic(bp, false, false);
7263 }
7264
7265 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007266}
7267
7268/* rtnl_lock held */
7269static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7270{
7271 struct bnxt *bp = netdev_priv(dev);
7272
Michael Chanc0c050c2015-10-22 16:01:17 -04007273 if (netif_running(dev))
7274 bnxt_close_nic(bp, false, false);
7275
7276 dev->mtu = new_mtu;
7277 bnxt_set_ring_params(bp);
7278
7279 if (netif_running(dev))
7280 return bnxt_open_nic(bp, false, false);
7281
7282 return 0;
7283}
7284
Michael Chanc5e3deb2016-12-02 21:17:15 -05007285int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007286{
7287 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007288 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007289 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007290
Michael Chanc0c050c2015-10-22 16:01:17 -04007291 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007292 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007293 tc, bp->max_tc);
7294 return -EINVAL;
7295 }
7296
7297 if (netdev_get_num_tc(dev) == tc)
7298 return 0;
7299
Michael Chan3ffb6a32016-11-11 00:11:42 -05007300 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7301 sh = true;
7302
Michael Chan98fdbe72017-08-28 13:40:26 -04007303 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7304 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007305 if (rc)
7306 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007307
7308 /* Needs to close the device and do hw resource re-allocations */
7309 if (netif_running(bp->dev))
7310 bnxt_close_nic(bp, true, false);
7311
7312 if (tc) {
7313 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7314 netdev_set_num_tc(dev, tc);
7315 } else {
7316 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7317 netdev_reset_tc(dev);
7318 }
Michael Chan87e9b372017-08-23 19:34:03 -04007319 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
Michael Chan3ffb6a32016-11-11 00:11:42 -05007320 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7321 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007322 bp->num_stat_ctxs = bp->cp_nr_rings;
7323
7324 if (netif_running(bp->dev))
7325 return bnxt_open_nic(bp, true, false);
7326
7327 return 0;
7328}
7329
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007330static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7331 void *cb_priv)
Sathya Perla2ae74082017-08-28 13:40:33 -04007332{
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007333 struct bnxt *bp = cb_priv;
Sathya Perla2ae74082017-08-28 13:40:33 -04007334
7335 if (BNXT_VF(bp))
7336 return -EOPNOTSUPP;
7337
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007338 switch (type) {
7339 case TC_SETUP_CLSFLOWER:
7340 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
7341 default:
7342 return -EOPNOTSUPP;
7343 }
7344}
7345
7346static int bnxt_setup_tc_block(struct net_device *dev,
7347 struct tc_block_offload *f)
7348{
7349 struct bnxt *bp = netdev_priv(dev);
7350
7351 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7352 return -EOPNOTSUPP;
7353
7354 switch (f->command) {
7355 case TC_BLOCK_BIND:
7356 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
7357 bp, bp);
7358 case TC_BLOCK_UNBIND:
7359 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
7360 return 0;
7361 default:
7362 return -EOPNOTSUPP;
7363 }
Sathya Perla2ae74082017-08-28 13:40:33 -04007364}
7365
Jiri Pirko2572ac52017-08-07 10:15:17 +02007366static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02007367 void *type_data)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007368{
Sathya Perla2ae74082017-08-28 13:40:33 -04007369 switch (type) {
Jiri Pirko9e0fd152017-10-19 15:50:39 +02007370 case TC_SETUP_BLOCK:
7371 return bnxt_setup_tc_block(dev, type_data);
Sathya Perla2ae74082017-08-28 13:40:33 -04007372 case TC_SETUP_MQPRIO: {
7373 struct tc_mqprio_qopt *mqprio = type_data;
Jiri Pirkode4784c2017-08-07 10:15:32 +02007374
Sathya Perla2ae74082017-08-28 13:40:33 -04007375 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7376
7377 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
7378 }
7379 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02007380 return -EOPNOTSUPP;
Sathya Perla2ae74082017-08-28 13:40:33 -04007381 }
Michael Chanc5e3deb2016-12-02 21:17:15 -05007382}
7383
Michael Chanc0c050c2015-10-22 16:01:17 -04007384#ifdef CONFIG_RFS_ACCEL
7385static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7386 struct bnxt_ntuple_filter *f2)
7387{
7388 struct flow_keys *keys1 = &f1->fkeys;
7389 struct flow_keys *keys2 = &f2->fkeys;
7390
7391 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7392 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7393 keys1->ports.ports == keys2->ports.ports &&
7394 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7395 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007396 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007397 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7398 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007399 return true;
7400
7401 return false;
7402}
7403
7404static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7405 u16 rxq_index, u32 flow_id)
7406{
7407 struct bnxt *bp = netdev_priv(dev);
7408 struct bnxt_ntuple_filter *fltr, *new_fltr;
7409 struct flow_keys *fkeys;
7410 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007411 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007412 struct hlist_head *head;
7413
Michael Chana54c4d72016-07-25 12:33:35 -04007414 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7415 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7416 int off = 0, j;
7417
7418 netif_addr_lock_bh(dev);
7419 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7420 if (ether_addr_equal(eth->h_dest,
7421 vnic->uc_list + off)) {
7422 l2_idx = j + 1;
7423 break;
7424 }
7425 }
7426 netif_addr_unlock_bh(dev);
7427 if (!l2_idx)
7428 return -EINVAL;
7429 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007430 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7431 if (!new_fltr)
7432 return -ENOMEM;
7433
7434 fkeys = &new_fltr->fkeys;
7435 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7436 rc = -EPROTONOSUPPORT;
7437 goto err_free;
7438 }
7439
Michael Chandda0e742016-12-29 12:13:40 -05007440 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7441 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007442 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7443 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7444 rc = -EPROTONOSUPPORT;
7445 goto err_free;
7446 }
Michael Chandda0e742016-12-29 12:13:40 -05007447 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7448 bp->hwrm_spec_code < 0x10601) {
7449 rc = -EPROTONOSUPPORT;
7450 goto err_free;
7451 }
Michael Chan61aad722017-02-12 19:18:14 -05007452 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7453 bp->hwrm_spec_code < 0x10601) {
7454 rc = -EPROTONOSUPPORT;
7455 goto err_free;
7456 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007457
Michael Chana54c4d72016-07-25 12:33:35 -04007458 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007459 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7460
7461 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7462 head = &bp->ntp_fltr_hash_tbl[idx];
7463 rcu_read_lock();
7464 hlist_for_each_entry_rcu(fltr, head, hash) {
7465 if (bnxt_fltr_match(fltr, new_fltr)) {
7466 rcu_read_unlock();
7467 rc = 0;
7468 goto err_free;
7469 }
7470 }
7471 rcu_read_unlock();
7472
7473 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007474 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7475 BNXT_NTP_FLTR_MAX_FLTR, 0);
7476 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007477 spin_unlock_bh(&bp->ntp_fltr_lock);
7478 rc = -ENOMEM;
7479 goto err_free;
7480 }
7481
Michael Chan84e86b92015-11-05 16:25:50 -05007482 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007483 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007484 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007485 new_fltr->rxq = rxq_index;
7486 hlist_add_head_rcu(&new_fltr->hash, head);
7487 bp->ntp_fltr_count++;
7488 spin_unlock_bh(&bp->ntp_fltr_lock);
7489
7490 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007491 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007492
7493 return new_fltr->sw_id;
7494
7495err_free:
7496 kfree(new_fltr);
7497 return rc;
7498}
7499
7500static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7501{
7502 int i;
7503
7504 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7505 struct hlist_head *head;
7506 struct hlist_node *tmp;
7507 struct bnxt_ntuple_filter *fltr;
7508 int rc;
7509
7510 head = &bp->ntp_fltr_hash_tbl[i];
7511 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7512 bool del = false;
7513
7514 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7515 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7516 fltr->flow_id,
7517 fltr->sw_id)) {
7518 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7519 fltr);
7520 del = true;
7521 }
7522 } else {
7523 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7524 fltr);
7525 if (rc)
7526 del = true;
7527 else
7528 set_bit(BNXT_FLTR_VALID, &fltr->state);
7529 }
7530
7531 if (del) {
7532 spin_lock_bh(&bp->ntp_fltr_lock);
7533 hlist_del_rcu(&fltr->hash);
7534 bp->ntp_fltr_count--;
7535 spin_unlock_bh(&bp->ntp_fltr_lock);
7536 synchronize_rcu();
7537 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7538 kfree(fltr);
7539 }
7540 }
7541 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007542 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7543 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007544}
7545
7546#else
7547
7548static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7549{
7550}
7551
7552#endif /* CONFIG_RFS_ACCEL */
7553
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007554static void bnxt_udp_tunnel_add(struct net_device *dev,
7555 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007556{
7557 struct bnxt *bp = netdev_priv(dev);
7558
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007559 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7560 return;
7561
Michael Chanc0c050c2015-10-22 16:01:17 -04007562 if (!netif_running(dev))
7563 return;
7564
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007565 switch (ti->type) {
7566 case UDP_TUNNEL_TYPE_VXLAN:
7567 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7568 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007569
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007570 bp->vxlan_port_cnt++;
7571 if (bp->vxlan_port_cnt == 1) {
7572 bp->vxlan_port = ti->port;
7573 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc213eae2017-10-13 21:09:29 -04007574 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007575 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007576 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007577 case UDP_TUNNEL_TYPE_GENEVE:
7578 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7579 return;
7580
7581 bp->nge_port_cnt++;
7582 if (bp->nge_port_cnt == 1) {
7583 bp->nge_port = ti->port;
7584 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7585 }
7586 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007587 default:
7588 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007589 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007590
Michael Chanc213eae2017-10-13 21:09:29 -04007591 bnxt_queue_sp_work(bp);
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007592}
7593
7594static void bnxt_udp_tunnel_del(struct net_device *dev,
7595 struct udp_tunnel_info *ti)
7596{
7597 struct bnxt *bp = netdev_priv(dev);
7598
7599 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7600 return;
7601
7602 if (!netif_running(dev))
7603 return;
7604
7605 switch (ti->type) {
7606 case UDP_TUNNEL_TYPE_VXLAN:
7607 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7608 return;
7609 bp->vxlan_port_cnt--;
7610
7611 if (bp->vxlan_port_cnt != 0)
7612 return;
7613
7614 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7615 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007616 case UDP_TUNNEL_TYPE_GENEVE:
7617 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7618 return;
7619 bp->nge_port_cnt--;
7620
7621 if (bp->nge_port_cnt != 0)
7622 return;
7623
7624 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7625 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007626 default:
7627 return;
7628 }
7629
Michael Chanc213eae2017-10-13 21:09:29 -04007630 bnxt_queue_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007631}
7632
Michael Chan39d8ba22017-07-24 12:34:22 -04007633static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7634 struct net_device *dev, u32 filter_mask,
7635 int nlflags)
7636{
7637 struct bnxt *bp = netdev_priv(dev);
7638
7639 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7640 nlflags, filter_mask, NULL);
7641}
7642
7643static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7644 u16 flags)
7645{
7646 struct bnxt *bp = netdev_priv(dev);
7647 struct nlattr *attr, *br_spec;
7648 int rem, rc = 0;
7649
7650 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7651 return -EOPNOTSUPP;
7652
7653 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7654 if (!br_spec)
7655 return -EINVAL;
7656
7657 nla_for_each_nested(attr, br_spec, rem) {
7658 u16 mode;
7659
7660 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7661 continue;
7662
7663 if (nla_len(attr) < sizeof(mode))
7664 return -EINVAL;
7665
7666 mode = nla_get_u16(attr);
7667 if (mode == bp->br_mode)
7668 break;
7669
7670 rc = bnxt_hwrm_set_br_mode(bp, mode);
7671 if (!rc)
7672 bp->br_mode = mode;
7673 break;
7674 }
7675 return rc;
7676}
7677
Sathya Perlac124a622017-07-24 12:34:29 -04007678static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
7679 size_t len)
7680{
7681 struct bnxt *bp = netdev_priv(dev);
7682 int rc;
7683
7684 /* The PF and it's VF-reps only support the switchdev framework */
7685 if (!BNXT_PF(bp))
7686 return -EOPNOTSUPP;
7687
Sathya Perla53f70b82017-07-25 13:28:41 -04007688 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
Sathya Perlac124a622017-07-24 12:34:29 -04007689
7690 if (rc >= len)
7691 return -EOPNOTSUPP;
7692 return 0;
7693}
7694
7695int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
7696{
7697 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
7698 return -EOPNOTSUPP;
7699
7700 /* The PF and it's VF-reps only support the switchdev framework */
7701 if (!BNXT_PF(bp))
7702 return -EOPNOTSUPP;
7703
7704 switch (attr->id) {
7705 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
7706 /* In SRIOV each PF-pool (PF + child VFs) serves as a
7707 * switching domain, the PF's perm mac-addr can be used
7708 * as the unique parent-id
7709 */
7710 attr->u.ppid.id_len = ETH_ALEN;
7711 ether_addr_copy(attr->u.ppid.id, bp->pf.mac_addr);
7712 break;
7713 default:
7714 return -EOPNOTSUPP;
7715 }
7716 return 0;
7717}
7718
7719static int bnxt_swdev_port_attr_get(struct net_device *dev,
7720 struct switchdev_attr *attr)
7721{
7722 return bnxt_port_attr_get(netdev_priv(dev), attr);
7723}
7724
7725static const struct switchdev_ops bnxt_switchdev_ops = {
7726 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
7727};
7728
Michael Chanc0c050c2015-10-22 16:01:17 -04007729static const struct net_device_ops bnxt_netdev_ops = {
7730 .ndo_open = bnxt_open,
7731 .ndo_start_xmit = bnxt_start_xmit,
7732 .ndo_stop = bnxt_close,
7733 .ndo_get_stats64 = bnxt_get_stats64,
7734 .ndo_set_rx_mode = bnxt_set_rx_mode,
7735 .ndo_do_ioctl = bnxt_ioctl,
7736 .ndo_validate_addr = eth_validate_addr,
7737 .ndo_set_mac_address = bnxt_change_mac_addr,
7738 .ndo_change_mtu = bnxt_change_mtu,
7739 .ndo_fix_features = bnxt_fix_features,
7740 .ndo_set_features = bnxt_set_features,
7741 .ndo_tx_timeout = bnxt_tx_timeout,
7742#ifdef CONFIG_BNXT_SRIOV
7743 .ndo_get_vf_config = bnxt_get_vf_config,
7744 .ndo_set_vf_mac = bnxt_set_vf_mac,
7745 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7746 .ndo_set_vf_rate = bnxt_set_vf_bw,
7747 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7748 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7749#endif
7750#ifdef CONFIG_NET_POLL_CONTROLLER
7751 .ndo_poll_controller = bnxt_poll_controller,
7752#endif
7753 .ndo_setup_tc = bnxt_setup_tc,
7754#ifdef CONFIG_RFS_ACCEL
7755 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7756#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007757 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7758 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007759 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007760 .ndo_bridge_getlink = bnxt_bridge_getlink,
7761 .ndo_bridge_setlink = bnxt_bridge_setlink,
Sathya Perlac124a622017-07-24 12:34:29 -04007762 .ndo_get_phys_port_name = bnxt_get_phys_port_name
Michael Chanc0c050c2015-10-22 16:01:17 -04007763};
7764
7765static void bnxt_remove_one(struct pci_dev *pdev)
7766{
7767 struct net_device *dev = pci_get_drvdata(pdev);
7768 struct bnxt *bp = netdev_priv(dev);
7769
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007770 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007771 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007772 bnxt_dl_unregister(bp);
7773 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007774
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007775 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007776 unregister_netdev(dev);
Sathya Perla2ae74082017-08-28 13:40:33 -04007777 bnxt_shutdown_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04007778 bnxt_cancel_sp_work(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007779 bp->sp_event = 0;
7780
Michael Chan78095922016-12-07 00:26:16 -05007781 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007782 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007783 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007784 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007785 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007786 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007787 kfree(bp->edev);
7788 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007789 if (bp->xdp_prog)
7790 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007791 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007792 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007793}
7794
7795static int bnxt_probe_phy(struct bnxt *bp)
7796{
7797 int rc = 0;
7798 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007799
Michael Chan170ce012016-04-05 14:08:57 -04007800 rc = bnxt_hwrm_phy_qcaps(bp);
7801 if (rc) {
7802 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7803 rc);
7804 return rc;
7805 }
Michael Chane2dc9b62017-10-13 21:09:30 -04007806 mutex_init(&bp->link_lock);
Michael Chan170ce012016-04-05 14:08:57 -04007807
Michael Chanc0c050c2015-10-22 16:01:17 -04007808 rc = bnxt_update_link(bp, false);
7809 if (rc) {
7810 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7811 rc);
7812 return rc;
7813 }
7814
Michael Chan93ed8112016-06-13 02:25:37 -04007815 /* Older firmware does not have supported_auto_speeds, so assume
7816 * that all supported speeds can be autonegotiated.
7817 */
7818 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7819 link_info->support_auto_speeds = link_info->support_speeds;
7820
Michael Chanc0c050c2015-10-22 16:01:17 -04007821 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007822 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007823 link_info->autoneg = BNXT_AUTONEG_SPEED;
7824 if (bp->hwrm_spec_code >= 0x10201) {
7825 if (link_info->auto_pause_setting &
7826 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7827 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7828 } else {
7829 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7830 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007831 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007832 } else {
7833 link_info->req_link_speed = link_info->force_link_speed;
7834 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007835 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007836 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7837 link_info->req_flow_ctrl =
7838 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7839 else
7840 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007841 return rc;
7842}
7843
7844static int bnxt_get_max_irq(struct pci_dev *pdev)
7845{
7846 u16 ctrl;
7847
7848 if (!pdev->msix_cap)
7849 return 1;
7850
7851 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7852 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7853}
7854
Michael Chan6e6c5a52016-01-02 23:45:02 -05007855static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7856 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007857{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007858 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007859
Michael Chan379a80a2015-10-23 15:06:19 -04007860#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007861 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007862 *max_tx = bp->vf.max_tx_rings;
7863 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007864 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7865 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007866 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007867 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007868#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007869 {
7870 *max_tx = bp->pf.max_tx_rings;
7871 *max_rx = bp->pf.max_rx_rings;
7872 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7873 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7874 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007875 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007876 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7877 *max_cp -= 1;
7878 *max_rx -= 2;
7879 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007880 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7881 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007882 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007883}
7884
7885int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7886{
7887 int rx, tx, cp;
7888
7889 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7890 if (!rx || !tx || !cp)
7891 return -ENOMEM;
7892
7893 *max_rx = rx;
7894 *max_tx = tx;
7895 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7896}
7897
Michael Chane4060d32016-12-07 00:26:19 -05007898static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7899 bool shared)
7900{
7901 int rc;
7902
7903 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007904 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7905 /* Not enough rings, try disabling agg rings. */
7906 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7907 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7908 if (rc)
7909 return rc;
7910 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7911 bp->dev->hw_features &= ~NETIF_F_LRO;
7912 bp->dev->features &= ~NETIF_F_LRO;
7913 bnxt_set_ring_params(bp);
7914 }
Michael Chane4060d32016-12-07 00:26:19 -05007915
7916 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7917 int max_cp, max_stat, max_irq;
7918
7919 /* Reserve minimum resources for RoCE */
7920 max_cp = bnxt_get_max_func_cp_rings(bp);
7921 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7922 max_irq = bnxt_get_max_func_irqs(bp);
7923 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7924 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7925 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7926 return 0;
7927
7928 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7929 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7930 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7931 max_cp = min_t(int, max_cp, max_irq);
7932 max_cp = min_t(int, max_cp, max_stat);
7933 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7934 if (rc)
7935 rc = 0;
7936 }
7937 return rc;
7938}
7939
Michael Chan702c2212017-05-29 19:06:10 -04007940static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007941{
7942 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007943
7944 if (sh)
7945 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7946 dflt_rings = netif_get_num_default_rss_queues();
Michael Chand5430d32017-08-28 13:40:31 -04007947 /* Reduce default rings to reduce memory usage on multi-port cards */
7948 if (bp->port_count > 1)
7949 dflt_rings = min_t(int, dflt_rings, 4);
Michael Chane4060d32016-12-07 00:26:19 -05007950 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007951 if (rc)
7952 return rc;
7953 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7954 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007955
7956 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7957 if (rc)
7958 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7959
Michael Chan6e6c5a52016-01-02 23:45:02 -05007960 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7961 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7962 bp->tx_nr_rings + bp->rx_nr_rings;
7963 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007964 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7965 bp->rx_nr_rings++;
7966 bp->cp_nr_rings++;
7967 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007968 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007969}
7970
Michael Chan7b08f662016-12-07 00:26:18 -05007971void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7972{
7973 ASSERT_RTNL();
7974 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007975 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007976}
7977
Michael Chana22a6ac2017-08-23 19:34:05 -04007978static int bnxt_init_mac_addr(struct bnxt *bp)
7979{
7980 int rc = 0;
7981
7982 if (BNXT_PF(bp)) {
7983 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
7984 } else {
7985#ifdef CONFIG_BNXT_SRIOV
7986 struct bnxt_vf_info *vf = &bp->vf;
7987
7988 if (is_valid_ether_addr(vf->mac_addr)) {
7989 /* overwrite netdev dev_adr with admin VF MAC */
7990 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
7991 } else {
7992 eth_hw_addr_random(bp->dev);
7993 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
7994 }
7995#endif
7996 }
7997 return rc;
7998}
7999
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008000static void bnxt_parse_log_pcie_link(struct bnxt *bp)
8001{
8002 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
8003 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
8004
Vasundhara Volam7ab07602017-10-13 21:09:31 -04008005 if (pcie_get_minimum_link(pci_physfn(bp->pdev), &speed, &width) ||
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008006 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
8007 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
8008 else
8009 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
8010 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
8011 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
8012 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
8013 "Unknown", width);
8014}
8015
Michael Chanc0c050c2015-10-22 16:01:17 -04008016static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8017{
8018 static int version_printed;
8019 struct net_device *dev;
8020 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05008021 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04008022
Ray Jui4e003382017-02-20 19:25:16 -05008023 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04008024 return -ENODEV;
8025
Michael Chanc0c050c2015-10-22 16:01:17 -04008026 if (version_printed++ == 0)
8027 pr_info("%s", version);
8028
8029 max_irqs = bnxt_get_max_irq(pdev);
8030 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
8031 if (!dev)
8032 return -ENOMEM;
8033
8034 bp = netdev_priv(dev);
8035
8036 if (bnxt_vf_pciid(ent->driver_data))
8037 bp->flags |= BNXT_FLAG_VF;
8038
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008039 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04008040 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04008041
8042 rc = bnxt_init_board(pdev, dev);
8043 if (rc < 0)
8044 goto init_err_free;
8045
8046 dev->netdev_ops = &bnxt_netdev_ops;
8047 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
8048 dev->ethtool_ops = &bnxt_ethtool_ops;
David S. Millerbc880552017-07-24 21:20:16 -07008049 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
Michael Chanc0c050c2015-10-22 16:01:17 -04008050 pci_set_drvdata(pdev, dev);
8051
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008052 rc = bnxt_alloc_hwrm_resources(bp);
8053 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008054 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008055
8056 mutex_init(&bp->hwrm_cmd_lock);
8057 rc = bnxt_hwrm_ver_get(bp);
8058 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008059 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008060
Deepak Khungare605db82017-05-29 19:06:04 -04008061 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
8062 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
8063 if (rc)
8064 goto init_err_pci_clean;
8065 }
8066
Michael Chan3c2217a2017-03-08 18:44:32 -05008067 rc = bnxt_hwrm_func_reset(bp);
8068 if (rc)
8069 goto init_err_pci_clean;
8070
Rob Swindell5ac67d82016-09-19 03:58:03 -04008071 bnxt_hwrm_fw_set_time(bp);
8072
Michael Chanc0c050c2015-10-22 16:01:17 -04008073 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8074 NETIF_F_TSO | NETIF_F_TSO6 |
8075 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07008076 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07008077 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8078 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04008079 NETIF_F_RXCSUM | NETIF_F_GRO;
8080
8081 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8082 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04008083
Michael Chanc0c050c2015-10-22 16:01:17 -04008084 dev->hw_enc_features =
8085 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8086 NETIF_F_TSO | NETIF_F_TSO6 |
8087 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07008088 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07008089 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07008090 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
8091 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04008092 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
8093 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
8094 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
8095 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
8096 dev->priv_flags |= IFF_UNICAST_FLT;
8097
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04008098 /* MTU range: 60 - 9500 */
8099 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05008100 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04008101
Michael Chanc0c050c2015-10-22 16:01:17 -04008102#ifdef CONFIG_BNXT_SRIOV
8103 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008104 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04008105#endif
Michael Chan309369c2016-06-13 02:25:34 -04008106 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04008107 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04008108 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04008109 else
8110 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04008111
Michael Chanc0c050c2015-10-22 16:01:17 -04008112 rc = bnxt_hwrm_func_drv_rgtr(bp);
8113 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008114 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008115
Michael Chana1653b12016-12-07 00:26:20 -05008116 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
8117 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008118 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05008119
Michael Chana588e452016-12-07 00:26:21 -05008120 bp->ulp_probe = bnxt_ulp_probe;
8121
Michael Chanc0c050c2015-10-22 16:01:17 -04008122 /* Get the MAX capabilities for this function */
8123 rc = bnxt_hwrm_func_qcaps(bp);
8124 if (rc) {
8125 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
8126 rc);
8127 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008128 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008129 }
Michael Chana22a6ac2017-08-23 19:34:05 -04008130 rc = bnxt_init_mac_addr(bp);
8131 if (rc) {
8132 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
8133 rc = -EADDRNOTAVAIL;
8134 goto init_err_pci_clean;
8135 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008136 rc = bnxt_hwrm_queue_qportcfg(bp);
8137 if (rc) {
8138 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
8139 rc);
8140 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05008141 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008142 }
8143
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008144 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05008145 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04008146 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04008147 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04008148
Michael Chand5430d32017-08-28 13:40:31 -04008149 rc = bnxt_probe_phy(bp);
8150 if (rc)
8151 goto init_err_pci_clean;
8152
Michael Chanc61fb992017-02-06 16:55:36 -05008153 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04008154 bnxt_set_tpa_flags(bp);
8155 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05008156 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04008157 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008158 if (rc) {
8159 netdev_err(bp->dev, "Not enough rings available.\n");
8160 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05008161 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05008162 }
Michael Chanc0c050c2015-10-22 16:01:17 -04008163
Michael Chan87da7f72016-11-16 21:13:09 -05008164 /* Default RSS hash cfg. */
8165 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
8166 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
8167 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
8168 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04008169 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05008170 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8171 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8172 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8173 }
8174
Michael Chan8fdefd62016-12-29 12:13:36 -05008175 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05008176 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05008177 dev->hw_features |= NETIF_F_NTUPLE;
8178 if (bnxt_rfs_capable(bp)) {
8179 bp->flags |= BNXT_FLAG_RFS;
8180 dev->features |= NETIF_F_NTUPLE;
8181 }
8182 }
8183
Michael Chanc0c050c2015-10-22 16:01:17 -04008184 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8185 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8186
Michael Chan78095922016-12-07 00:26:16 -05008187 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008188 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05008189 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04008190
Michael Chanc1ef1462017-04-04 18:14:07 -04008191 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008192 if (bp->flags & BNXT_FLAG_WOL_CAP)
8193 device_set_wakeup_enable(&pdev->dev, bp->wol);
8194 else
8195 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04008196
Michael Chanc213eae2017-10-13 21:09:29 -04008197 if (BNXT_PF(bp)) {
8198 if (!bnxt_pf_wq) {
8199 bnxt_pf_wq =
8200 create_singlethread_workqueue("bnxt_pf_wq");
8201 if (!bnxt_pf_wq) {
8202 dev_err(&pdev->dev, "Unable to create workqueue.\n");
8203 goto init_err_pci_clean;
8204 }
8205 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008206 bnxt_init_tc(bp);
Michael Chanc213eae2017-10-13 21:09:29 -04008207 }
Sathya Perla2ae74082017-08-28 13:40:33 -04008208
Michael Chan78095922016-12-07 00:26:16 -05008209 rc = register_netdev(dev);
8210 if (rc)
Sathya Perla2ae74082017-08-28 13:40:33 -04008211 goto init_err_cleanup_tc;
Michael Chan78095922016-12-07 00:26:16 -05008212
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04008213 if (BNXT_PF(bp))
8214 bnxt_dl_register(bp);
8215
Michael Chanc0c050c2015-10-22 16:01:17 -04008216 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8217 board_info[ent->driver_data].name,
8218 (long)pci_resource_start(pdev, 0), dev->dev_addr);
8219
Ajit Khaparde90c4f782016-05-15 03:04:45 -04008220 bnxt_parse_log_pcie_link(bp);
8221
Michael Chanc0c050c2015-10-22 16:01:17 -04008222 return 0;
8223
Sathya Perla2ae74082017-08-28 13:40:33 -04008224init_err_cleanup_tc:
8225 bnxt_shutdown_tc(bp);
Michael Chan78095922016-12-07 00:26:16 -05008226 bnxt_clear_int_mode(bp);
8227
Sathya Perla17086392017-02-20 19:25:18 -05008228init_err_pci_clean:
8229 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04008230
8231init_err_free:
8232 free_netdev(dev);
8233 return rc;
8234}
8235
Michael Chand196ece2017-04-04 18:14:08 -04008236static void bnxt_shutdown(struct pci_dev *pdev)
8237{
8238 struct net_device *dev = pci_get_drvdata(pdev);
8239 struct bnxt *bp;
8240
8241 if (!dev)
8242 return;
8243
8244 rtnl_lock();
8245 bp = netdev_priv(dev);
8246 if (!bp)
8247 goto shutdown_exit;
8248
8249 if (netif_running(dev))
8250 dev_close(dev);
8251
8252 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04008253 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04008254 bnxt_clear_int_mode(bp);
8255 pci_wake_from_d3(pdev, bp->wol);
8256 pci_set_power_state(pdev, PCI_D3hot);
8257 }
8258
8259shutdown_exit:
8260 rtnl_unlock();
8261}
8262
Michael Chanf65a2042017-04-04 18:14:11 -04008263#ifdef CONFIG_PM_SLEEP
8264static int bnxt_suspend(struct device *device)
8265{
8266 struct pci_dev *pdev = to_pci_dev(device);
8267 struct net_device *dev = pci_get_drvdata(pdev);
8268 struct bnxt *bp = netdev_priv(dev);
8269 int rc = 0;
8270
8271 rtnl_lock();
8272 if (netif_running(dev)) {
8273 netif_device_detach(dev);
8274 rc = bnxt_close(dev);
8275 }
8276 bnxt_hwrm_func_drv_unrgtr(bp);
8277 rtnl_unlock();
8278 return rc;
8279}
8280
8281static int bnxt_resume(struct device *device)
8282{
8283 struct pci_dev *pdev = to_pci_dev(device);
8284 struct net_device *dev = pci_get_drvdata(pdev);
8285 struct bnxt *bp = netdev_priv(dev);
8286 int rc = 0;
8287
8288 rtnl_lock();
8289 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8290 rc = -ENODEV;
8291 goto resume_exit;
8292 }
8293 rc = bnxt_hwrm_func_reset(bp);
8294 if (rc) {
8295 rc = -EBUSY;
8296 goto resume_exit;
8297 }
8298 bnxt_get_wol_settings(bp);
8299 if (netif_running(dev)) {
8300 rc = bnxt_open(dev);
8301 if (!rc)
8302 netif_device_attach(dev);
8303 }
8304
8305resume_exit:
8306 rtnl_unlock();
8307 return rc;
8308}
8309
8310static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8311#define BNXT_PM_OPS (&bnxt_pm_ops)
8312
8313#else
8314
8315#define BNXT_PM_OPS NULL
8316
8317#endif /* CONFIG_PM_SLEEP */
8318
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008319/**
8320 * bnxt_io_error_detected - called when PCI error is detected
8321 * @pdev: Pointer to PCI device
8322 * @state: The current pci connection state
8323 *
8324 * This function is called after a PCI bus error affecting
8325 * this device has been detected.
8326 */
8327static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8328 pci_channel_state_t state)
8329{
8330 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008331 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008332
8333 netdev_info(netdev, "PCI I/O error detected\n");
8334
8335 rtnl_lock();
8336 netif_device_detach(netdev);
8337
Michael Chana588e452016-12-07 00:26:21 -05008338 bnxt_ulp_stop(bp);
8339
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008340 if (state == pci_channel_io_perm_failure) {
8341 rtnl_unlock();
8342 return PCI_ERS_RESULT_DISCONNECT;
8343 }
8344
8345 if (netif_running(netdev))
8346 bnxt_close(netdev);
8347
8348 pci_disable_device(pdev);
8349 rtnl_unlock();
8350
8351 /* Request a slot slot reset. */
8352 return PCI_ERS_RESULT_NEED_RESET;
8353}
8354
8355/**
8356 * bnxt_io_slot_reset - called after the pci bus has been reset.
8357 * @pdev: Pointer to PCI device
8358 *
8359 * Restart the card from scratch, as if from a cold-boot.
8360 * At this point, the card has exprienced a hard reset,
8361 * followed by fixups by BIOS, and has its config space
8362 * set up identically to what it was at cold boot.
8363 */
8364static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8365{
8366 struct net_device *netdev = pci_get_drvdata(pdev);
8367 struct bnxt *bp = netdev_priv(netdev);
8368 int err = 0;
8369 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8370
8371 netdev_info(bp->dev, "PCI Slot Reset\n");
8372
8373 rtnl_lock();
8374
8375 if (pci_enable_device(pdev)) {
8376 dev_err(&pdev->dev,
8377 "Cannot re-enable PCI device after reset.\n");
8378 } else {
8379 pci_set_master(pdev);
8380
Michael Chanaa8ed022016-12-07 00:26:17 -05008381 err = bnxt_hwrm_func_reset(bp);
8382 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008383 err = bnxt_open(netdev);
8384
Michael Chana588e452016-12-07 00:26:21 -05008385 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008386 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008387 bnxt_ulp_start(bp);
8388 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008389 }
8390
8391 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8392 dev_close(netdev);
8393
8394 rtnl_unlock();
8395
8396 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8397 if (err) {
8398 dev_err(&pdev->dev,
8399 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8400 err); /* non-fatal, continue */
8401 }
8402
8403 return PCI_ERS_RESULT_RECOVERED;
8404}
8405
8406/**
8407 * bnxt_io_resume - called when traffic can start flowing again.
8408 * @pdev: Pointer to PCI device
8409 *
8410 * This callback is called when the error recovery driver tells
8411 * us that its OK to resume normal operation.
8412 */
8413static void bnxt_io_resume(struct pci_dev *pdev)
8414{
8415 struct net_device *netdev = pci_get_drvdata(pdev);
8416
8417 rtnl_lock();
8418
8419 netif_device_attach(netdev);
8420
8421 rtnl_unlock();
8422}
8423
8424static const struct pci_error_handlers bnxt_err_handler = {
8425 .error_detected = bnxt_io_error_detected,
8426 .slot_reset = bnxt_io_slot_reset,
8427 .resume = bnxt_io_resume
8428};
8429
Michael Chanc0c050c2015-10-22 16:01:17 -04008430static struct pci_driver bnxt_pci_driver = {
8431 .name = DRV_MODULE_NAME,
8432 .id_table = bnxt_pci_tbl,
8433 .probe = bnxt_init_one,
8434 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008435 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008436 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008437 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008438#if defined(CONFIG_BNXT_SRIOV)
8439 .sriov_configure = bnxt_sriov_configure,
8440#endif
8441};
8442
Michael Chanc213eae2017-10-13 21:09:29 -04008443static int __init bnxt_init(void)
8444{
8445 return pci_register_driver(&bnxt_pci_driver);
8446}
8447
8448static void __exit bnxt_exit(void)
8449{
8450 pci_unregister_driver(&bnxt_pci_driver);
8451 if (bnxt_pf_wq)
8452 destroy_workqueue(bnxt_pf_wq);
8453}
8454
8455module_init(bnxt_init);
8456module_exit(bnxt_exit);