blob: 95fea2622d640e9a366cfb98201ecea6844d963f [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>
52
53#include "bnxt_hsi.h"
54#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050055#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056#include "bnxt_sriov.h"
57#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050058#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050059#include "bnxt_xdp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040060
61#define BNXT_TX_TIMEOUT (5 * HZ)
62
63static const char version[] =
64 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
65
66MODULE_LICENSE("GPL");
67MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
68MODULE_VERSION(DRV_MODULE_VERSION);
69
70#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
71#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
72#define BNXT_RX_COPY_THRESH 256
73
Michael Chan4419dbe2016-02-10 17:33:49 -050074#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040075
76enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050077 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040078 BCM57302,
79 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040080 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040081 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040082 BCM57311,
83 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050084 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040085 BCM57404,
86 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040087 BCM57402_NPAR,
88 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040089 BCM57412,
90 BCM57414,
91 BCM57416,
92 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040093 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040094 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040095 BCM57417_SFP,
96 BCM57416_SFP,
97 BCM57404_NPAR,
98 BCM57406_NPAR,
99 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400100 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400101 BCM57414_NPAR,
102 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500103 BCM57452,
104 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400105 NETXTREME_E_VF,
106 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400107};
108
109/* indexed by enum above */
110static const struct {
111 char *name;
112} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400113 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
114 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
115 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400116 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400117 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
118 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
119 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
120 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
121 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
122 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
125 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
126 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
127 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
128 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400129 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400130 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
132 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400133 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
134 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400135 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
136 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400137 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
138 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500139 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
140 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400141 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
142 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400143};
144
145static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400146 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500147 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400148 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
149 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400150 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400151 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400152 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
153 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500154 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400155 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
156 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
158 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400159 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
160 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
161 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
162 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400163 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400164 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400165 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
166 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
167 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
169 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400170 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
171 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400172 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400175 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500176 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
177 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400178#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400179 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
180 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400181 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
182 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
186 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400187#endif
188 { 0 }
189};
190
191MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
192
193static const u16 bnxt_vf_req_snif[] = {
194 HWRM_FUNC_CFG,
195 HWRM_PORT_PHY_QCFG,
196 HWRM_CFA_L2_FILTER_ALLOC,
197};
198
Michael Chan25be8622016-04-05 14:09:00 -0400199static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500200 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
201 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
202 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
203 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
204 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400205};
206
Michael Chanc0c050c2015-10-22 16:01:17 -0400207static bool bnxt_vf_pciid(enum board_idx idx)
208{
Michael Chanadbc8302016-09-19 03:58:01 -0400209 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400210}
211
212#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
213#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
214#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
215
216#define BNXT_CP_DB_REARM(db, raw_cons) \
217 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
218
219#define BNXT_CP_DB(db, raw_cons) \
220 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
221
222#define BNXT_CP_DB_IRQ_DIS(db) \
223 writel(DB_CP_IRQ_DIS_FLAGS, db)
224
Michael Chan38413402017-02-06 16:55:43 -0500225const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400226 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
227 TX_BD_FLAGS_LHINT_512_TO_1023,
228 TX_BD_FLAGS_LHINT_1024_TO_2047,
229 TX_BD_FLAGS_LHINT_1024_TO_2047,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
244 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
245};
246
247static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
248{
249 struct bnxt *bp = netdev_priv(dev);
250 struct tx_bd *txbd;
251 struct tx_bd_ext *txbd1;
252 struct netdev_queue *txq;
253 int i;
254 dma_addr_t mapping;
255 unsigned int length, pad = 0;
256 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
257 u16 prod, last_frag;
258 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400259 struct bnxt_tx_ring_info *txr;
260 struct bnxt_sw_tx_bd *tx_buf;
261
262 i = skb_get_queue_mapping(skb);
263 if (unlikely(i >= bp->tx_nr_rings)) {
264 dev_kfree_skb_any(skb);
265 return NETDEV_TX_OK;
266 }
267
Michael Chanc0c050c2015-10-22 16:01:17 -0400268 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500269 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400270 prod = txr->tx_prod;
271
272 free_size = bnxt_tx_avail(bp, txr);
273 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
274 netif_tx_stop_queue(txq);
275 return NETDEV_TX_BUSY;
276 }
277
278 length = skb->len;
279 len = skb_headlen(skb);
280 last_frag = skb_shinfo(skb)->nr_frags;
281
282 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
283
284 txbd->tx_bd_opaque = prod;
285
286 tx_buf = &txr->tx_buf_ring[prod];
287 tx_buf->skb = skb;
288 tx_buf->nr_frags = last_frag;
289
290 vlan_tag_flags = 0;
291 cfa_action = 0;
292 if (skb_vlan_tag_present(skb)) {
293 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
294 skb_vlan_tag_get(skb);
295 /* Currently supports 8021Q, 8021AD vlan offloads
296 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
297 */
298 if (skb->vlan_proto == htons(ETH_P_8021Q))
299 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
300 }
301
302 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500303 struct tx_push_buffer *tx_push_buf = txr->tx_push;
304 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
305 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
306 void *pdata = tx_push_buf->data;
307 u64 *end;
308 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400309
310 /* Set COAL_NOW to be ready quickly for the next push */
311 tx_push->tx_bd_len_flags_type =
312 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
313 TX_BD_TYPE_LONG_TX_BD |
314 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
315 TX_BD_FLAGS_COAL_NOW |
316 TX_BD_FLAGS_PACKET_END |
317 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
318
319 if (skb->ip_summed == CHECKSUM_PARTIAL)
320 tx_push1->tx_bd_hsize_lflags =
321 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
322 else
323 tx_push1->tx_bd_hsize_lflags = 0;
324
325 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
326 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
327
Michael Chanfbb0fa82016-02-22 02:10:26 -0500328 end = pdata + length;
329 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500330 *end = 0;
331
Michael Chanc0c050c2015-10-22 16:01:17 -0400332 skb_copy_from_linear_data(skb, pdata, len);
333 pdata += len;
334 for (j = 0; j < last_frag; j++) {
335 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
336 void *fptr;
337
338 fptr = skb_frag_address_safe(frag);
339 if (!fptr)
340 goto normal_tx;
341
342 memcpy(pdata, fptr, skb_frag_size(frag));
343 pdata += skb_frag_size(frag);
344 }
345
Michael Chan4419dbe2016-02-10 17:33:49 -0500346 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
347 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400348 prod = NEXT_TX(prod);
349 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
350 memcpy(txbd, tx_push1, sizeof(*txbd));
351 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500352 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400353 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
354 txr->tx_prod = prod;
355
Michael Chanb9a84602016-06-06 02:37:14 -0400356 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400357 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400358 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400359
Michael Chan4419dbe2016-02-10 17:33:49 -0500360 push_len = (length + sizeof(*tx_push) + 7) / 8;
361 if (push_len > 16) {
362 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400363 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
364 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500365 } else {
366 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
367 push_len);
368 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400369
Michael Chanc0c050c2015-10-22 16:01:17 -0400370 goto tx_done;
371 }
372
373normal_tx:
374 if (length < BNXT_MIN_PKT_SIZE) {
375 pad = BNXT_MIN_PKT_SIZE - length;
376 if (skb_pad(skb, pad)) {
377 /* SKB already freed. */
378 tx_buf->skb = NULL;
379 return NETDEV_TX_OK;
380 }
381 length = BNXT_MIN_PKT_SIZE;
382 }
383
384 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
385
386 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
387 dev_kfree_skb_any(skb);
388 tx_buf->skb = NULL;
389 return NETDEV_TX_OK;
390 }
391
392 dma_unmap_addr_set(tx_buf, mapping, mapping);
393 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
394 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
395
396 txbd->tx_bd_haddr = cpu_to_le64(mapping);
397
398 prod = NEXT_TX(prod);
399 txbd1 = (struct tx_bd_ext *)
400 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
401
402 txbd1->tx_bd_hsize_lflags = 0;
403 if (skb_is_gso(skb)) {
404 u32 hdr_len;
405
406 if (skb->encapsulation)
407 hdr_len = skb_inner_network_offset(skb) +
408 skb_inner_network_header_len(skb) +
409 inner_tcp_hdrlen(skb);
410 else
411 hdr_len = skb_transport_offset(skb) +
412 tcp_hdrlen(skb);
413
414 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
415 TX_BD_FLAGS_T_IPID |
416 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
417 length = skb_shinfo(skb)->gso_size;
418 txbd1->tx_bd_mss = cpu_to_le32(length);
419 length += hdr_len;
420 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
421 txbd1->tx_bd_hsize_lflags =
422 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
423 txbd1->tx_bd_mss = 0;
424 }
425
426 length >>= 9;
427 flags |= bnxt_lhint_arr[length];
428 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
429
430 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
431 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
432 for (i = 0; i < last_frag; i++) {
433 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
434
435 prod = NEXT_TX(prod);
436 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
437
438 len = skb_frag_size(frag);
439 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
440 DMA_TO_DEVICE);
441
442 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
443 goto tx_dma_error;
444
445 tx_buf = &txr->tx_buf_ring[prod];
446 dma_unmap_addr_set(tx_buf, mapping, mapping);
447
448 txbd->tx_bd_haddr = cpu_to_le64(mapping);
449
450 flags = len << TX_BD_LEN_SHIFT;
451 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
452 }
453
454 flags &= ~TX_BD_LEN;
455 txbd->tx_bd_len_flags_type =
456 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
457 TX_BD_FLAGS_PACKET_END);
458
459 netdev_tx_sent_queue(txq, skb->len);
460
461 /* Sync BD data before updating doorbell */
462 wmb();
463
464 prod = NEXT_TX(prod);
465 txr->tx_prod = prod;
466
Michael Chanffe40642017-05-30 20:03:00 -0400467 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400468 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400469
470tx_done:
471
472 mmiowb();
473
474 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400475 if (skb->xmit_more && !tx_buf->is_push)
476 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
477
Michael Chanc0c050c2015-10-22 16:01:17 -0400478 netif_tx_stop_queue(txq);
479
480 /* netif_tx_stop_queue() must be done before checking
481 * tx index in bnxt_tx_avail() below, because in
482 * bnxt_tx_int(), we update tx index before checking for
483 * netif_tx_queue_stopped().
484 */
485 smp_mb();
486 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
487 netif_tx_wake_queue(txq);
488 }
489 return NETDEV_TX_OK;
490
491tx_dma_error:
492 last_frag = i;
493
494 /* start back at beginning and unmap skb */
495 prod = txr->tx_prod;
496 tx_buf = &txr->tx_buf_ring[prod];
497 tx_buf->skb = NULL;
498 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
499 skb_headlen(skb), PCI_DMA_TODEVICE);
500 prod = NEXT_TX(prod);
501
502 /* unmap remaining mapped pages */
503 for (i = 0; i < last_frag; i++) {
504 prod = NEXT_TX(prod);
505 tx_buf = &txr->tx_buf_ring[prod];
506 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
507 skb_frag_size(&skb_shinfo(skb)->frags[i]),
508 PCI_DMA_TODEVICE);
509 }
510
511 dev_kfree_skb_any(skb);
512 return NETDEV_TX_OK;
513}
514
515static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
516{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500517 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500518 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400519 u16 cons = txr->tx_cons;
520 struct pci_dev *pdev = bp->pdev;
521 int i;
522 unsigned int tx_bytes = 0;
523
524 for (i = 0; i < nr_pkts; i++) {
525 struct bnxt_sw_tx_bd *tx_buf;
526 struct sk_buff *skb;
527 int j, last;
528
529 tx_buf = &txr->tx_buf_ring[cons];
530 cons = NEXT_TX(cons);
531 skb = tx_buf->skb;
532 tx_buf->skb = NULL;
533
534 if (tx_buf->is_push) {
535 tx_buf->is_push = 0;
536 goto next_tx_int;
537 }
538
539 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
540 skb_headlen(skb), PCI_DMA_TODEVICE);
541 last = tx_buf->nr_frags;
542
543 for (j = 0; j < last; j++) {
544 cons = NEXT_TX(cons);
545 tx_buf = &txr->tx_buf_ring[cons];
546 dma_unmap_page(
547 &pdev->dev,
548 dma_unmap_addr(tx_buf, mapping),
549 skb_frag_size(&skb_shinfo(skb)->frags[j]),
550 PCI_DMA_TODEVICE);
551 }
552
553next_tx_int:
554 cons = NEXT_TX(cons);
555
556 tx_bytes += skb->len;
557 dev_kfree_skb_any(skb);
558 }
559
560 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
561 txr->tx_cons = cons;
562
563 /* Need to make the tx_cons update visible to bnxt_start_xmit()
564 * before checking for netif_tx_queue_stopped(). Without the
565 * memory barrier, there is a small possibility that bnxt_start_xmit()
566 * will miss it and cause the queue to be stopped forever.
567 */
568 smp_mb();
569
570 if (unlikely(netif_tx_queue_stopped(txq)) &&
571 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
572 __netif_tx_lock(txq, smp_processor_id());
573 if (netif_tx_queue_stopped(txq) &&
574 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
575 txr->dev_state != BNXT_DEV_STATE_CLOSING)
576 netif_tx_wake_queue(txq);
577 __netif_tx_unlock(txq);
578 }
579}
580
Michael Chanc61fb992017-02-06 16:55:36 -0500581static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
582 gfp_t gfp)
583{
584 struct device *dev = &bp->pdev->dev;
585 struct page *page;
586
587 page = alloc_page(gfp);
588 if (!page)
589 return NULL;
590
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700591 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
592 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500593 if (dma_mapping_error(dev, *mapping)) {
594 __free_page(page);
595 return NULL;
596 }
597 *mapping += bp->rx_dma_offset;
598 return page;
599}
600
Michael Chanc0c050c2015-10-22 16:01:17 -0400601static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
602 gfp_t gfp)
603{
604 u8 *data;
605 struct pci_dev *pdev = bp->pdev;
606
607 data = kmalloc(bp->rx_buf_size, gfp);
608 if (!data)
609 return NULL;
610
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700611 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
612 bp->rx_buf_use_size, bp->rx_dir,
613 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400614
615 if (dma_mapping_error(&pdev->dev, *mapping)) {
616 kfree(data);
617 data = NULL;
618 }
619 return data;
620}
621
Michael Chan38413402017-02-06 16:55:43 -0500622int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
623 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400624{
625 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
626 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400627 dma_addr_t mapping;
628
Michael Chanc61fb992017-02-06 16:55:36 -0500629 if (BNXT_RX_PAGE_MODE(bp)) {
630 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400631
Michael Chanc61fb992017-02-06 16:55:36 -0500632 if (!page)
633 return -ENOMEM;
634
635 rx_buf->data = page;
636 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
637 } else {
638 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
639
640 if (!data)
641 return -ENOMEM;
642
643 rx_buf->data = data;
644 rx_buf->data_ptr = data + bp->rx_offset;
645 }
Michael Chan11cd1192017-02-06 16:55:33 -0500646 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400647
648 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400649 return 0;
650}
651
Michael Chanc6d30e82017-02-06 16:55:42 -0500652void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400653{
654 u16 prod = rxr->rx_prod;
655 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
656 struct rx_bd *cons_bd, *prod_bd;
657
658 prod_rx_buf = &rxr->rx_buf_ring[prod];
659 cons_rx_buf = &rxr->rx_buf_ring[cons];
660
661 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500662 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400663
Michael Chan11cd1192017-02-06 16:55:33 -0500664 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400665
666 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
667 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
668
669 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
670}
671
672static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
673{
674 u16 next, max = rxr->rx_agg_bmap_size;
675
676 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
677 if (next >= max)
678 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
679 return next;
680}
681
682static inline int bnxt_alloc_rx_page(struct bnxt *bp,
683 struct bnxt_rx_ring_info *rxr,
684 u16 prod, gfp_t gfp)
685{
686 struct rx_bd *rxbd =
687 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
688 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
689 struct pci_dev *pdev = bp->pdev;
690 struct page *page;
691 dma_addr_t mapping;
692 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400693 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400694
Michael Chan89d0a062016-04-25 02:30:51 -0400695 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
696 page = rxr->rx_page;
697 if (!page) {
698 page = alloc_page(gfp);
699 if (!page)
700 return -ENOMEM;
701 rxr->rx_page = page;
702 rxr->rx_page_offset = 0;
703 }
704 offset = rxr->rx_page_offset;
705 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
706 if (rxr->rx_page_offset == PAGE_SIZE)
707 rxr->rx_page = NULL;
708 else
709 get_page(page);
710 } else {
711 page = alloc_page(gfp);
712 if (!page)
713 return -ENOMEM;
714 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400715
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700716 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
717 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
718 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400719 if (dma_mapping_error(&pdev->dev, mapping)) {
720 __free_page(page);
721 return -EIO;
722 }
723
724 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
725 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
726
727 __set_bit(sw_prod, rxr->rx_agg_bmap);
728 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
729 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
730
731 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400732 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400733 rx_agg_buf->mapping = mapping;
734 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
735 rxbd->rx_bd_opaque = sw_prod;
736 return 0;
737}
738
739static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
740 u32 agg_bufs)
741{
742 struct bnxt *bp = bnapi->bp;
743 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500744 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400745 u16 prod = rxr->rx_agg_prod;
746 u16 sw_prod = rxr->rx_sw_agg_prod;
747 u32 i;
748
749 for (i = 0; i < agg_bufs; i++) {
750 u16 cons;
751 struct rx_agg_cmp *agg;
752 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
753 struct rx_bd *prod_bd;
754 struct page *page;
755
756 agg = (struct rx_agg_cmp *)
757 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
758 cons = agg->rx_agg_cmp_opaque;
759 __clear_bit(cons, rxr->rx_agg_bmap);
760
761 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
762 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
763
764 __set_bit(sw_prod, rxr->rx_agg_bmap);
765 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
766 cons_rx_buf = &rxr->rx_agg_ring[cons];
767
768 /* It is possible for sw_prod to be equal to cons, so
769 * set cons_rx_buf->page to NULL first.
770 */
771 page = cons_rx_buf->page;
772 cons_rx_buf->page = NULL;
773 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400774 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400775
776 prod_rx_buf->mapping = cons_rx_buf->mapping;
777
778 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
779
780 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
781 prod_bd->rx_bd_opaque = sw_prod;
782
783 prod = NEXT_RX_AGG(prod);
784 sw_prod = NEXT_RX_AGG(sw_prod);
785 cp_cons = NEXT_CMP(cp_cons);
786 }
787 rxr->rx_agg_prod = prod;
788 rxr->rx_sw_agg_prod = sw_prod;
789}
790
Michael Chanc61fb992017-02-06 16:55:36 -0500791static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
792 struct bnxt_rx_ring_info *rxr,
793 u16 cons, void *data, u8 *data_ptr,
794 dma_addr_t dma_addr,
795 unsigned int offset_and_len)
796{
797 unsigned int payload = offset_and_len >> 16;
798 unsigned int len = offset_and_len & 0xffff;
799 struct skb_frag_struct *frag;
800 struct page *page = data;
801 u16 prod = rxr->rx_prod;
802 struct sk_buff *skb;
803 int off, err;
804
805 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
806 if (unlikely(err)) {
807 bnxt_reuse_rx_data(rxr, cons, data);
808 return NULL;
809 }
810 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700811 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
812 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500813
814 if (unlikely(!payload))
815 payload = eth_get_headlen(data_ptr, len);
816
817 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
818 if (!skb) {
819 __free_page(page);
820 return NULL;
821 }
822
823 off = (void *)data_ptr - page_address(page);
824 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
825 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
826 payload + NET_IP_ALIGN);
827
828 frag = &skb_shinfo(skb)->frags[0];
829 skb_frag_size_sub(frag, payload);
830 frag->page_offset += payload;
831 skb->data_len -= payload;
832 skb->tail += payload;
833
834 return skb;
835}
836
Michael Chanc0c050c2015-10-22 16:01:17 -0400837static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
838 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500839 void *data, u8 *data_ptr,
840 dma_addr_t dma_addr,
841 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400842{
Michael Chan6bb19472017-02-06 16:55:32 -0500843 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400844 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500845 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400846
847 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
848 if (unlikely(err)) {
849 bnxt_reuse_rx_data(rxr, cons, data);
850 return NULL;
851 }
852
853 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700854 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
855 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400856 if (!skb) {
857 kfree(data);
858 return NULL;
859 }
860
Michael Chanb3dba772017-02-06 16:55:35 -0500861 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500862 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400863 return skb;
864}
865
866static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
867 struct sk_buff *skb, u16 cp_cons,
868 u32 agg_bufs)
869{
870 struct pci_dev *pdev = bp->pdev;
871 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500872 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400873 u16 prod = rxr->rx_agg_prod;
874 u32 i;
875
876 for (i = 0; i < agg_bufs; i++) {
877 u16 cons, frag_len;
878 struct rx_agg_cmp *agg;
879 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
880 struct page *page;
881 dma_addr_t mapping;
882
883 agg = (struct rx_agg_cmp *)
884 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
885 cons = agg->rx_agg_cmp_opaque;
886 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
887 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
888
889 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400890 skb_fill_page_desc(skb, i, cons_rx_buf->page,
891 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400892 __clear_bit(cons, rxr->rx_agg_bmap);
893
894 /* It is possible for bnxt_alloc_rx_page() to allocate
895 * a sw_prod index that equals the cons index, so we
896 * need to clear the cons entry now.
897 */
Michael Chan11cd1192017-02-06 16:55:33 -0500898 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400899 page = cons_rx_buf->page;
900 cons_rx_buf->page = NULL;
901
902 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
903 struct skb_shared_info *shinfo;
904 unsigned int nr_frags;
905
906 shinfo = skb_shinfo(skb);
907 nr_frags = --shinfo->nr_frags;
908 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
909
910 dev_kfree_skb(skb);
911
912 cons_rx_buf->page = page;
913
914 /* Update prod since possibly some pages have been
915 * allocated already.
916 */
917 rxr->rx_agg_prod = prod;
918 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
919 return NULL;
920 }
921
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700922 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
923 PCI_DMA_FROMDEVICE,
924 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400925
926 skb->data_len += frag_len;
927 skb->len += frag_len;
928 skb->truesize += PAGE_SIZE;
929
930 prod = NEXT_RX_AGG(prod);
931 cp_cons = NEXT_CMP(cp_cons);
932 }
933 rxr->rx_agg_prod = prod;
934 return skb;
935}
936
937static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
938 u8 agg_bufs, u32 *raw_cons)
939{
940 u16 last;
941 struct rx_agg_cmp *agg;
942
943 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
944 last = RING_CMP(*raw_cons);
945 agg = (struct rx_agg_cmp *)
946 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
947 return RX_AGG_CMP_VALID(agg, *raw_cons);
948}
949
950static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
951 unsigned int len,
952 dma_addr_t mapping)
953{
954 struct bnxt *bp = bnapi->bp;
955 struct pci_dev *pdev = bp->pdev;
956 struct sk_buff *skb;
957
958 skb = napi_alloc_skb(&bnapi->napi, len);
959 if (!skb)
960 return NULL;
961
Michael Chan745fc052017-02-06 16:55:34 -0500962 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
963 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400964
Michael Chan6bb19472017-02-06 16:55:32 -0500965 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
966 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400967
Michael Chan745fc052017-02-06 16:55:34 -0500968 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
969 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400970
971 skb_put(skb, len);
972 return skb;
973}
974
Michael Chanfa7e2812016-05-10 19:18:00 -0400975static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
976 u32 *raw_cons, void *cmp)
977{
978 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
979 struct rx_cmp *rxcmp = cmp;
980 u32 tmp_raw_cons = *raw_cons;
981 u8 cmp_type, agg_bufs = 0;
982
983 cmp_type = RX_CMP_TYPE(rxcmp);
984
985 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
986 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
987 RX_CMP_AGG_BUFS) >>
988 RX_CMP_AGG_BUFS_SHIFT;
989 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
990 struct rx_tpa_end_cmp *tpa_end = cmp;
991
992 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
993 RX_TPA_END_CMP_AGG_BUFS) >>
994 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
995 }
996
997 if (agg_bufs) {
998 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
999 return -EBUSY;
1000 }
1001 *raw_cons = tmp_raw_cons;
1002 return 0;
1003}
1004
1005static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1006{
1007 if (!rxr->bnapi->in_reset) {
1008 rxr->bnapi->in_reset = true;
1009 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1010 schedule_work(&bp->sp_task);
1011 }
1012 rxr->rx_next_cons = 0xffff;
1013}
1014
Michael Chanc0c050c2015-10-22 16:01:17 -04001015static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1016 struct rx_tpa_start_cmp *tpa_start,
1017 struct rx_tpa_start_cmp_ext *tpa_start1)
1018{
1019 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1020 u16 cons, prod;
1021 struct bnxt_tpa_info *tpa_info;
1022 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1023 struct rx_bd *prod_bd;
1024 dma_addr_t mapping;
1025
1026 cons = tpa_start->rx_tpa_start_cmp_opaque;
1027 prod = rxr->rx_prod;
1028 cons_rx_buf = &rxr->rx_buf_ring[cons];
1029 prod_rx_buf = &rxr->rx_buf_ring[prod];
1030 tpa_info = &rxr->rx_tpa[agg_id];
1031
Michael Chanfa7e2812016-05-10 19:18:00 -04001032 if (unlikely(cons != rxr->rx_next_cons)) {
1033 bnxt_sched_reset(bp, rxr);
1034 return;
1035 }
1036
Michael Chanc0c050c2015-10-22 16:01:17 -04001037 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001038 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001039
1040 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001041 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001042
1043 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1044
1045 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1046
1047 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001048 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001049 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001050 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001051
1052 tpa_info->len =
1053 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1054 RX_TPA_START_CMP_LEN_SHIFT;
1055 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1056 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1057
1058 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1059 tpa_info->gso_type = SKB_GSO_TCPV4;
1060 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1061 if (hash_type == 3)
1062 tpa_info->gso_type = SKB_GSO_TCPV6;
1063 tpa_info->rss_hash =
1064 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1065 } else {
1066 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1067 tpa_info->gso_type = 0;
1068 if (netif_msg_rx_err(bp))
1069 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1070 }
1071 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1072 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001073 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001074
1075 rxr->rx_prod = NEXT_RX(prod);
1076 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001077 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001078 cons_rx_buf = &rxr->rx_buf_ring[cons];
1079
1080 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1081 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1082 cons_rx_buf->data = NULL;
1083}
1084
1085static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1086 u16 cp_cons, u32 agg_bufs)
1087{
1088 if (agg_bufs)
1089 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1090}
1091
Michael Chan94758f82016-06-13 02:25:35 -04001092static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1093 int payload_off, int tcp_ts,
1094 struct sk_buff *skb)
1095{
1096#ifdef CONFIG_INET
1097 struct tcphdr *th;
1098 int len, nw_off;
1099 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1100 u32 hdr_info = tpa_info->hdr_info;
1101 bool loopback = false;
1102
1103 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1104 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1105 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1106
1107 /* If the packet is an internal loopback packet, the offsets will
1108 * have an extra 4 bytes.
1109 */
1110 if (inner_mac_off == 4) {
1111 loopback = true;
1112 } else if (inner_mac_off > 4) {
1113 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1114 ETH_HLEN - 2));
1115
1116 /* We only support inner iPv4/ipv6. If we don't see the
1117 * correct protocol ID, it must be a loopback packet where
1118 * the offsets are off by 4.
1119 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001120 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001121 loopback = true;
1122 }
1123 if (loopback) {
1124 /* internal loopback packet, subtract all offsets by 4 */
1125 inner_ip_off -= 4;
1126 inner_mac_off -= 4;
1127 outer_ip_off -= 4;
1128 }
1129
1130 nw_off = inner_ip_off - ETH_HLEN;
1131 skb_set_network_header(skb, nw_off);
1132 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1133 struct ipv6hdr *iph = ipv6_hdr(skb);
1134
1135 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1136 len = skb->len - skb_transport_offset(skb);
1137 th = tcp_hdr(skb);
1138 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1139 } else {
1140 struct iphdr *iph = ip_hdr(skb);
1141
1142 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1143 len = skb->len - skb_transport_offset(skb);
1144 th = tcp_hdr(skb);
1145 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1146 }
1147
1148 if (inner_mac_off) { /* tunnel */
1149 struct udphdr *uh = NULL;
1150 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1151 ETH_HLEN - 2));
1152
1153 if (proto == htons(ETH_P_IP)) {
1154 struct iphdr *iph = (struct iphdr *)skb->data;
1155
1156 if (iph->protocol == IPPROTO_UDP)
1157 uh = (struct udphdr *)(iph + 1);
1158 } else {
1159 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1160
1161 if (iph->nexthdr == IPPROTO_UDP)
1162 uh = (struct udphdr *)(iph + 1);
1163 }
1164 if (uh) {
1165 if (uh->check)
1166 skb_shinfo(skb)->gso_type |=
1167 SKB_GSO_UDP_TUNNEL_CSUM;
1168 else
1169 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1170 }
1171 }
1172#endif
1173 return skb;
1174}
1175
Michael Chanc0c050c2015-10-22 16:01:17 -04001176#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1177#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1178
Michael Chan309369c2016-06-13 02:25:34 -04001179static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1180 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001181 struct sk_buff *skb)
1182{
Michael Chand1611c32015-10-25 22:27:57 -04001183#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001184 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001185 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001186
Michael Chan309369c2016-06-13 02:25:34 -04001187 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001188 tcp_opt_len = 12;
1189
Michael Chanc0c050c2015-10-22 16:01:17 -04001190 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1191 struct iphdr *iph;
1192
1193 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1194 ETH_HLEN;
1195 skb_set_network_header(skb, nw_off);
1196 iph = ip_hdr(skb);
1197 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1198 len = skb->len - skb_transport_offset(skb);
1199 th = tcp_hdr(skb);
1200 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1201 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1202 struct ipv6hdr *iph;
1203
1204 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1205 ETH_HLEN;
1206 skb_set_network_header(skb, nw_off);
1207 iph = ipv6_hdr(skb);
1208 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1209 len = skb->len - skb_transport_offset(skb);
1210 th = tcp_hdr(skb);
1211 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1212 } else {
1213 dev_kfree_skb_any(skb);
1214 return NULL;
1215 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001216
1217 if (nw_off) { /* tunnel */
1218 struct udphdr *uh = NULL;
1219
1220 if (skb->protocol == htons(ETH_P_IP)) {
1221 struct iphdr *iph = (struct iphdr *)skb->data;
1222
1223 if (iph->protocol == IPPROTO_UDP)
1224 uh = (struct udphdr *)(iph + 1);
1225 } else {
1226 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1227
1228 if (iph->nexthdr == IPPROTO_UDP)
1229 uh = (struct udphdr *)(iph + 1);
1230 }
1231 if (uh) {
1232 if (uh->check)
1233 skb_shinfo(skb)->gso_type |=
1234 SKB_GSO_UDP_TUNNEL_CSUM;
1235 else
1236 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1237 }
1238 }
1239#endif
1240 return skb;
1241}
1242
Michael Chan309369c2016-06-13 02:25:34 -04001243static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1244 struct bnxt_tpa_info *tpa_info,
1245 struct rx_tpa_end_cmp *tpa_end,
1246 struct rx_tpa_end_cmp_ext *tpa_end1,
1247 struct sk_buff *skb)
1248{
1249#ifdef CONFIG_INET
1250 int payload_off;
1251 u16 segs;
1252
1253 segs = TPA_END_TPA_SEGS(tpa_end);
1254 if (segs == 1)
1255 return skb;
1256
1257 NAPI_GRO_CB(skb)->count = segs;
1258 skb_shinfo(skb)->gso_size =
1259 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1260 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1261 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1262 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1263 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1264 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001265 if (likely(skb))
1266 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001267#endif
1268 return skb;
1269}
1270
Michael Chanc0c050c2015-10-22 16:01:17 -04001271static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1272 struct bnxt_napi *bnapi,
1273 u32 *raw_cons,
1274 struct rx_tpa_end_cmp *tpa_end,
1275 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001276 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001277{
1278 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001279 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001280 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001281 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001282 u16 cp_cons = RING_CMP(*raw_cons);
1283 unsigned int len;
1284 struct bnxt_tpa_info *tpa_info;
1285 dma_addr_t mapping;
1286 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001287 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001288
Michael Chanfa7e2812016-05-10 19:18:00 -04001289 if (unlikely(bnapi->in_reset)) {
1290 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1291
1292 if (rc < 0)
1293 return ERR_PTR(-EBUSY);
1294 return NULL;
1295 }
1296
Michael Chanc0c050c2015-10-22 16:01:17 -04001297 tpa_info = &rxr->rx_tpa[agg_id];
1298 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001299 data_ptr = tpa_info->data_ptr;
1300 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001301 len = tpa_info->len;
1302 mapping = tpa_info->mapping;
1303
1304 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1305 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1306
1307 if (agg_bufs) {
1308 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1309 return ERR_PTR(-EBUSY);
1310
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001311 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001312 cp_cons = NEXT_CMP(cp_cons);
1313 }
1314
Michael Chan69c149e2017-06-23 14:01:00 -04001315 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001316 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001317 if (agg_bufs > MAX_SKB_FRAGS)
1318 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1319 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001320 return NULL;
1321 }
1322
1323 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001324 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001325 if (!skb) {
1326 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1327 return NULL;
1328 }
1329 } else {
1330 u8 *new_data;
1331 dma_addr_t new_mapping;
1332
1333 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1334 if (!new_data) {
1335 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1336 return NULL;
1337 }
1338
1339 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001340 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001341 tpa_info->mapping = new_mapping;
1342
1343 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001344 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1345 bp->rx_buf_use_size, bp->rx_dir,
1346 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001347
1348 if (!skb) {
1349 kfree(data);
1350 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1351 return NULL;
1352 }
Michael Chanb3dba772017-02-06 16:55:35 -05001353 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001354 skb_put(skb, len);
1355 }
1356
1357 if (agg_bufs) {
1358 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1359 if (!skb) {
1360 /* Page reuse already handled by bnxt_rx_pages(). */
1361 return NULL;
1362 }
1363 }
1364 skb->protocol = eth_type_trans(skb, bp->dev);
1365
1366 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1367 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1368
Michael Chan8852ddb2016-06-06 02:37:16 -04001369 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1370 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001371 u16 vlan_proto = tpa_info->metadata >>
1372 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001373 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001374
Michael Chan8852ddb2016-06-06 02:37:16 -04001375 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001376 }
1377
1378 skb_checksum_none_assert(skb);
1379 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1380 skb->ip_summed = CHECKSUM_UNNECESSARY;
1381 skb->csum_level =
1382 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1383 }
1384
1385 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001386 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001387
1388 return skb;
1389}
1390
1391/* returns the following:
1392 * 1 - 1 packet successfully received
1393 * 0 - successful TPA_START, packet not completed yet
1394 * -EBUSY - completion ring does not have all the agg buffers yet
1395 * -ENOMEM - packet aborted due to out of memory
1396 * -EIO - packet aborted due to hw error indicated in BD
1397 */
1398static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001399 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001400{
1401 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001402 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001403 struct net_device *dev = bp->dev;
1404 struct rx_cmp *rxcmp;
1405 struct rx_cmp_ext *rxcmp1;
1406 u32 tmp_raw_cons = *raw_cons;
1407 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1408 struct bnxt_sw_rx_bd *rx_buf;
1409 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001410 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001411 dma_addr_t dma_addr;
1412 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001413 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001414 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001415 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001416
1417 rxcmp = (struct rx_cmp *)
1418 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1419
1420 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1421 cp_cons = RING_CMP(tmp_raw_cons);
1422 rxcmp1 = (struct rx_cmp_ext *)
1423 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1424
1425 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1426 return -EBUSY;
1427
1428 cmp_type = RX_CMP_TYPE(rxcmp);
1429
1430 prod = rxr->rx_prod;
1431
1432 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1433 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1434 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1435
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001436 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001437 goto next_rx_no_prod;
1438
1439 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1440 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1441 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001442 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001443
1444 if (unlikely(IS_ERR(skb)))
1445 return -EBUSY;
1446
1447 rc = -ENOMEM;
1448 if (likely(skb)) {
1449 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001450 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001451 rc = 1;
1452 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001453 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001454 goto next_rx_no_prod;
1455 }
1456
1457 cons = rxcmp->rx_cmp_opaque;
1458 rx_buf = &rxr->rx_buf_ring[cons];
1459 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001460 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001461 if (unlikely(cons != rxr->rx_next_cons)) {
1462 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1463
1464 bnxt_sched_reset(bp, rxr);
1465 return rc1;
1466 }
Michael Chan6bb19472017-02-06 16:55:32 -05001467 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001468
Michael Chanc61fb992017-02-06 16:55:36 -05001469 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1470 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001471
1472 if (agg_bufs) {
1473 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1474 return -EBUSY;
1475
1476 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001477 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001478 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001479 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001480
1481 rx_buf->data = NULL;
1482 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1483 bnxt_reuse_rx_data(rxr, cons, data);
1484 if (agg_bufs)
1485 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1486
1487 rc = -EIO;
1488 goto next_rx;
1489 }
1490
1491 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001492 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001493
Michael Chanc6d30e82017-02-06 16:55:42 -05001494 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1495 rc = 1;
1496 goto next_rx;
1497 }
1498
Michael Chanc0c050c2015-10-22 16:01:17 -04001499 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001500 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001501 bnxt_reuse_rx_data(rxr, cons, data);
1502 if (!skb) {
1503 rc = -ENOMEM;
1504 goto next_rx;
1505 }
1506 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001507 u32 payload;
1508
Michael Chanc6d30e82017-02-06 16:55:42 -05001509 if (rx_buf->data_ptr == data_ptr)
1510 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1511 else
1512 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001513 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001514 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001515 if (!skb) {
1516 rc = -ENOMEM;
1517 goto next_rx;
1518 }
1519 }
1520
1521 if (agg_bufs) {
1522 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1523 if (!skb) {
1524 rc = -ENOMEM;
1525 goto next_rx;
1526 }
1527 }
1528
1529 if (RX_CMP_HASH_VALID(rxcmp)) {
1530 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1531 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1532
1533 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1534 if (hash_type != 1 && hash_type != 3)
1535 type = PKT_HASH_TYPE_L3;
1536 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1537 }
1538
1539 skb->protocol = eth_type_trans(skb, dev);
1540
Michael Chan8852ddb2016-06-06 02:37:16 -04001541 if ((rxcmp1->rx_cmp_flags2 &
1542 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1543 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001544 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001545 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001546 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1547
Michael Chan8852ddb2016-06-06 02:37:16 -04001548 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001549 }
1550
1551 skb_checksum_none_assert(skb);
1552 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1553 if (dev->features & NETIF_F_RXCSUM) {
1554 skb->ip_summed = CHECKSUM_UNNECESSARY;
1555 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1556 }
1557 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001558 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1559 if (dev->features & NETIF_F_RXCSUM)
1560 cpr->rx_l4_csum_errors++;
1561 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001562 }
1563
1564 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001565 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001566 rc = 1;
1567
1568next_rx:
1569 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001570 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001571
1572next_rx_no_prod:
1573 *raw_cons = tmp_raw_cons;
1574
1575 return rc;
1576}
1577
Michael Chan2270bc52017-06-23 14:01:01 -04001578/* In netpoll mode, if we are using a combined completion ring, we need to
1579 * discard the rx packets and recycle the buffers.
1580 */
1581static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1582 u32 *raw_cons, u8 *event)
1583{
1584 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1585 u32 tmp_raw_cons = *raw_cons;
1586 struct rx_cmp_ext *rxcmp1;
1587 struct rx_cmp *rxcmp;
1588 u16 cp_cons;
1589 u8 cmp_type;
1590
1591 cp_cons = RING_CMP(tmp_raw_cons);
1592 rxcmp = (struct rx_cmp *)
1593 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1594
1595 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1596 cp_cons = RING_CMP(tmp_raw_cons);
1597 rxcmp1 = (struct rx_cmp_ext *)
1598 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1599
1600 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1601 return -EBUSY;
1602
1603 cmp_type = RX_CMP_TYPE(rxcmp);
1604 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1605 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1606 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1607 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1608 struct rx_tpa_end_cmp_ext *tpa_end1;
1609
1610 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1611 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1612 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1613 }
1614 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1615}
1616
Michael Chan4bb13ab2016-04-05 14:09:01 -04001617#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001618 ((data) & \
1619 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001620
Michael Chanc0c050c2015-10-22 16:01:17 -04001621static int bnxt_async_event_process(struct bnxt *bp,
1622 struct hwrm_async_event_cmpl *cmpl)
1623{
1624 u16 event_id = le16_to_cpu(cmpl->event_id);
1625
1626 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1627 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001628 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001629 u32 data1 = le32_to_cpu(cmpl->event_data1);
1630 struct bnxt_link_info *link_info = &bp->link_info;
1631
1632 if (BNXT_VF(bp))
1633 goto async_event_process_exit;
1634 if (data1 & 0x20000) {
1635 u16 fw_speed = link_info->force_link_speed;
1636 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1637
1638 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1639 speed);
1640 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001641 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001642 /* fall thru */
1643 }
Michael Chan87c374d2016-12-02 21:17:16 -05001644 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001645 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001646 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001647 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001648 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001649 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001650 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001651 u32 data1 = le32_to_cpu(cmpl->event_data1);
1652 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1653
1654 if (BNXT_VF(bp))
1655 break;
1656
1657 if (bp->pf.port_id != port_id)
1658 break;
1659
Michael Chan4bb13ab2016-04-05 14:09:01 -04001660 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1661 break;
1662 }
Michael Chan87c374d2016-12-02 21:17:16 -05001663 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001664 if (BNXT_PF(bp))
1665 goto async_event_process_exit;
1666 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1667 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001668 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001669 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001670 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001671 schedule_work(&bp->sp_task);
1672async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001673 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001674 return 0;
1675}
1676
1677static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1678{
1679 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1680 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1681 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1682 (struct hwrm_fwd_req_cmpl *)txcmp;
1683
1684 switch (cmpl_type) {
1685 case CMPL_BASE_TYPE_HWRM_DONE:
1686 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1687 if (seq_id == bp->hwrm_intr_seq_id)
1688 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1689 else
1690 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1691 break;
1692
1693 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1694 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1695
1696 if ((vf_id < bp->pf.first_vf_id) ||
1697 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1698 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1699 vf_id);
1700 return -EINVAL;
1701 }
1702
1703 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1704 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1705 schedule_work(&bp->sp_task);
1706 break;
1707
1708 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1709 bnxt_async_event_process(bp,
1710 (struct hwrm_async_event_cmpl *)txcmp);
1711
1712 default:
1713 break;
1714 }
1715
1716 return 0;
1717}
1718
1719static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1720{
1721 struct bnxt_napi *bnapi = dev_instance;
1722 struct bnxt *bp = bnapi->bp;
1723 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1724 u32 cons = RING_CMP(cpr->cp_raw_cons);
1725
1726 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1727 napi_schedule(&bnapi->napi);
1728 return IRQ_HANDLED;
1729}
1730
1731static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1732{
1733 u32 raw_cons = cpr->cp_raw_cons;
1734 u16 cons = RING_CMP(raw_cons);
1735 struct tx_cmp *txcmp;
1736
1737 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1738
1739 return TX_CMP_VALID(txcmp, raw_cons);
1740}
1741
Michael Chanc0c050c2015-10-22 16:01:17 -04001742static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1743{
1744 struct bnxt_napi *bnapi = dev_instance;
1745 struct bnxt *bp = bnapi->bp;
1746 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1747 u32 cons = RING_CMP(cpr->cp_raw_cons);
1748 u32 int_status;
1749
1750 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1751
1752 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001753 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001754 /* return if erroneous interrupt */
1755 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1756 return IRQ_NONE;
1757 }
1758
1759 /* disable ring IRQ */
1760 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1761
1762 /* Return here if interrupt is shared and is disabled. */
1763 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1764 return IRQ_HANDLED;
1765
1766 napi_schedule(&bnapi->napi);
1767 return IRQ_HANDLED;
1768}
1769
1770static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1771{
1772 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1773 u32 raw_cons = cpr->cp_raw_cons;
1774 u32 cons;
1775 int tx_pkts = 0;
1776 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001777 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001778 struct tx_cmp *txcmp;
1779
1780 while (1) {
1781 int rc;
1782
1783 cons = RING_CMP(raw_cons);
1784 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1785
1786 if (!TX_CMP_VALID(txcmp, raw_cons))
1787 break;
1788
Michael Chan67a95e22016-05-04 16:56:43 -04001789 /* The valid test of the entry must be done first before
1790 * reading any further.
1791 */
Michael Chanb67daab2016-05-15 03:04:51 -04001792 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001793 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1794 tx_pkts++;
1795 /* return full budget so NAPI will complete. */
1796 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1797 rx_pkts = budget;
1798 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001799 if (likely(budget))
1800 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1801 else
1802 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1803 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001804 if (likely(rc >= 0))
1805 rx_pkts += rc;
1806 else if (rc == -EBUSY) /* partial completion */
1807 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001808 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1809 CMPL_BASE_TYPE_HWRM_DONE) ||
1810 (TX_CMP_TYPE(txcmp) ==
1811 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1812 (TX_CMP_TYPE(txcmp) ==
1813 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1814 bnxt_hwrm_handler(bp, txcmp);
1815 }
1816 raw_cons = NEXT_RAW_CMP(raw_cons);
1817
1818 if (rx_pkts == budget)
1819 break;
1820 }
1821
Michael Chan38413402017-02-06 16:55:43 -05001822 if (event & BNXT_TX_EVENT) {
1823 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1824 void __iomem *db = txr->tx_doorbell;
1825 u16 prod = txr->tx_prod;
1826
1827 /* Sync BD data before updating doorbell */
1828 wmb();
1829
Michael Chan434c9752017-05-29 19:06:08 -04001830 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001831 }
1832
Michael Chanc0c050c2015-10-22 16:01:17 -04001833 cpr->cp_raw_cons = raw_cons;
1834 /* ACK completion ring before freeing tx ring and producing new
1835 * buffers in rx/agg rings to prevent overflowing the completion
1836 * ring.
1837 */
1838 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1839
1840 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001841 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001842
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001843 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001844 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001845
Michael Chan434c9752017-05-29 19:06:08 -04001846 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1847 if (event & BNXT_AGG_EVENT)
1848 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1849 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001850 }
1851 return rx_pkts;
1852}
1853
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001854static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1855{
1856 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1857 struct bnxt *bp = bnapi->bp;
1858 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1859 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1860 struct tx_cmp *txcmp;
1861 struct rx_cmp_ext *rxcmp1;
1862 u32 cp_cons, tmp_raw_cons;
1863 u32 raw_cons = cpr->cp_raw_cons;
1864 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001865 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001866
1867 while (1) {
1868 int rc;
1869
1870 cp_cons = RING_CMP(raw_cons);
1871 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1872
1873 if (!TX_CMP_VALID(txcmp, raw_cons))
1874 break;
1875
1876 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1877 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1878 cp_cons = RING_CMP(tmp_raw_cons);
1879 rxcmp1 = (struct rx_cmp_ext *)
1880 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1881
1882 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1883 break;
1884
1885 /* force an error to recycle the buffer */
1886 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1887 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1888
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001889 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001890 if (likely(rc == -EIO))
1891 rx_pkts++;
1892 else if (rc == -EBUSY) /* partial completion */
1893 break;
1894 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1895 CMPL_BASE_TYPE_HWRM_DONE)) {
1896 bnxt_hwrm_handler(bp, txcmp);
1897 } else {
1898 netdev_err(bp->dev,
1899 "Invalid completion received on special ring\n");
1900 }
1901 raw_cons = NEXT_RAW_CMP(raw_cons);
1902
1903 if (rx_pkts == budget)
1904 break;
1905 }
1906
1907 cpr->cp_raw_cons = raw_cons;
1908 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001909 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001910
Michael Chan434c9752017-05-29 19:06:08 -04001911 if (event & BNXT_AGG_EVENT)
1912 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1913 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001914
1915 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001916 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001917 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1918 }
1919 return rx_pkts;
1920}
1921
Michael Chanc0c050c2015-10-22 16:01:17 -04001922static int bnxt_poll(struct napi_struct *napi, int budget)
1923{
1924 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1925 struct bnxt *bp = bnapi->bp;
1926 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1927 int work_done = 0;
1928
Michael Chanc0c050c2015-10-22 16:01:17 -04001929 while (1) {
1930 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1931
1932 if (work_done >= budget)
1933 break;
1934
1935 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001936 if (napi_complete_done(napi, work_done))
1937 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1938 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001939 break;
1940 }
1941 }
1942 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001943 return work_done;
1944}
1945
Michael Chanc0c050c2015-10-22 16:01:17 -04001946static void bnxt_free_tx_skbs(struct bnxt *bp)
1947{
1948 int i, max_idx;
1949 struct pci_dev *pdev = bp->pdev;
1950
Michael Chanb6ab4b02016-01-02 23:44:59 -05001951 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001952 return;
1953
1954 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1955 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001956 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001957 int j;
1958
Michael Chanc0c050c2015-10-22 16:01:17 -04001959 for (j = 0; j < max_idx;) {
1960 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1961 struct sk_buff *skb = tx_buf->skb;
1962 int k, last;
1963
1964 if (!skb) {
1965 j++;
1966 continue;
1967 }
1968
1969 tx_buf->skb = NULL;
1970
1971 if (tx_buf->is_push) {
1972 dev_kfree_skb(skb);
1973 j += 2;
1974 continue;
1975 }
1976
1977 dma_unmap_single(&pdev->dev,
1978 dma_unmap_addr(tx_buf, mapping),
1979 skb_headlen(skb),
1980 PCI_DMA_TODEVICE);
1981
1982 last = tx_buf->nr_frags;
1983 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001984 for (k = 0; k < last; k++, j++) {
1985 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001986 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1987
Michael Chand612a572016-01-28 03:11:22 -05001988 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001989 dma_unmap_page(
1990 &pdev->dev,
1991 dma_unmap_addr(tx_buf, mapping),
1992 skb_frag_size(frag), PCI_DMA_TODEVICE);
1993 }
1994 dev_kfree_skb(skb);
1995 }
1996 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1997 }
1998}
1999
2000static void bnxt_free_rx_skbs(struct bnxt *bp)
2001{
2002 int i, max_idx, max_agg_idx;
2003 struct pci_dev *pdev = bp->pdev;
2004
Michael Chanb6ab4b02016-01-02 23:44:59 -05002005 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002006 return;
2007
2008 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2009 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2010 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002011 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002012 int j;
2013
Michael Chanc0c050c2015-10-22 16:01:17 -04002014 if (rxr->rx_tpa) {
2015 for (j = 0; j < MAX_TPA; j++) {
2016 struct bnxt_tpa_info *tpa_info =
2017 &rxr->rx_tpa[j];
2018 u8 *data = tpa_info->data;
2019
2020 if (!data)
2021 continue;
2022
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002023 dma_unmap_single_attrs(&pdev->dev,
2024 tpa_info->mapping,
2025 bp->rx_buf_use_size,
2026 bp->rx_dir,
2027 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002028
2029 tpa_info->data = NULL;
2030
2031 kfree(data);
2032 }
2033 }
2034
2035 for (j = 0; j < max_idx; j++) {
2036 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002037 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002038 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002039
2040 if (!data)
2041 continue;
2042
Michael Chanc0c050c2015-10-22 16:01:17 -04002043 rx_buf->data = NULL;
2044
Michael Chan3ed3a832017-03-28 19:47:31 -04002045 if (BNXT_RX_PAGE_MODE(bp)) {
2046 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002047 dma_unmap_page_attrs(&pdev->dev, mapping,
2048 PAGE_SIZE, bp->rx_dir,
2049 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002050 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002051 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002052 dma_unmap_single_attrs(&pdev->dev, mapping,
2053 bp->rx_buf_use_size,
2054 bp->rx_dir,
2055 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002056 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002057 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002058 }
2059
2060 for (j = 0; j < max_agg_idx; j++) {
2061 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2062 &rxr->rx_agg_ring[j];
2063 struct page *page = rx_agg_buf->page;
2064
2065 if (!page)
2066 continue;
2067
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002068 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2069 BNXT_RX_PAGE_SIZE,
2070 PCI_DMA_FROMDEVICE,
2071 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002072
2073 rx_agg_buf->page = NULL;
2074 __clear_bit(j, rxr->rx_agg_bmap);
2075
2076 __free_page(page);
2077 }
Michael Chan89d0a062016-04-25 02:30:51 -04002078 if (rxr->rx_page) {
2079 __free_page(rxr->rx_page);
2080 rxr->rx_page = NULL;
2081 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002082 }
2083}
2084
2085static void bnxt_free_skbs(struct bnxt *bp)
2086{
2087 bnxt_free_tx_skbs(bp);
2088 bnxt_free_rx_skbs(bp);
2089}
2090
2091static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2092{
2093 struct pci_dev *pdev = bp->pdev;
2094 int i;
2095
2096 for (i = 0; i < ring->nr_pages; i++) {
2097 if (!ring->pg_arr[i])
2098 continue;
2099
2100 dma_free_coherent(&pdev->dev, ring->page_size,
2101 ring->pg_arr[i], ring->dma_arr[i]);
2102
2103 ring->pg_arr[i] = NULL;
2104 }
2105 if (ring->pg_tbl) {
2106 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2107 ring->pg_tbl, ring->pg_tbl_map);
2108 ring->pg_tbl = NULL;
2109 }
2110 if (ring->vmem_size && *ring->vmem) {
2111 vfree(*ring->vmem);
2112 *ring->vmem = NULL;
2113 }
2114}
2115
2116static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2117{
2118 int i;
2119 struct pci_dev *pdev = bp->pdev;
2120
2121 if (ring->nr_pages > 1) {
2122 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2123 ring->nr_pages * 8,
2124 &ring->pg_tbl_map,
2125 GFP_KERNEL);
2126 if (!ring->pg_tbl)
2127 return -ENOMEM;
2128 }
2129
2130 for (i = 0; i < ring->nr_pages; i++) {
2131 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2132 ring->page_size,
2133 &ring->dma_arr[i],
2134 GFP_KERNEL);
2135 if (!ring->pg_arr[i])
2136 return -ENOMEM;
2137
2138 if (ring->nr_pages > 1)
2139 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2140 }
2141
2142 if (ring->vmem_size) {
2143 *ring->vmem = vzalloc(ring->vmem_size);
2144 if (!(*ring->vmem))
2145 return -ENOMEM;
2146 }
2147 return 0;
2148}
2149
2150static void bnxt_free_rx_rings(struct bnxt *bp)
2151{
2152 int i;
2153
Michael Chanb6ab4b02016-01-02 23:44:59 -05002154 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002155 return;
2156
2157 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002158 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002159 struct bnxt_ring_struct *ring;
2160
Michael Chanc6d30e82017-02-06 16:55:42 -05002161 if (rxr->xdp_prog)
2162 bpf_prog_put(rxr->xdp_prog);
2163
Michael Chanc0c050c2015-10-22 16:01:17 -04002164 kfree(rxr->rx_tpa);
2165 rxr->rx_tpa = NULL;
2166
2167 kfree(rxr->rx_agg_bmap);
2168 rxr->rx_agg_bmap = NULL;
2169
2170 ring = &rxr->rx_ring_struct;
2171 bnxt_free_ring(bp, ring);
2172
2173 ring = &rxr->rx_agg_ring_struct;
2174 bnxt_free_ring(bp, ring);
2175 }
2176}
2177
2178static int bnxt_alloc_rx_rings(struct bnxt *bp)
2179{
2180 int i, rc, agg_rings = 0, tpa_rings = 0;
2181
Michael Chanb6ab4b02016-01-02 23:44:59 -05002182 if (!bp->rx_ring)
2183 return -ENOMEM;
2184
Michael Chanc0c050c2015-10-22 16:01:17 -04002185 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2186 agg_rings = 1;
2187
2188 if (bp->flags & BNXT_FLAG_TPA)
2189 tpa_rings = 1;
2190
2191 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002192 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002193 struct bnxt_ring_struct *ring;
2194
Michael Chanc0c050c2015-10-22 16:01:17 -04002195 ring = &rxr->rx_ring_struct;
2196
2197 rc = bnxt_alloc_ring(bp, ring);
2198 if (rc)
2199 return rc;
2200
2201 if (agg_rings) {
2202 u16 mem_size;
2203
2204 ring = &rxr->rx_agg_ring_struct;
2205 rc = bnxt_alloc_ring(bp, ring);
2206 if (rc)
2207 return rc;
2208
2209 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2210 mem_size = rxr->rx_agg_bmap_size / 8;
2211 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2212 if (!rxr->rx_agg_bmap)
2213 return -ENOMEM;
2214
2215 if (tpa_rings) {
2216 rxr->rx_tpa = kcalloc(MAX_TPA,
2217 sizeof(struct bnxt_tpa_info),
2218 GFP_KERNEL);
2219 if (!rxr->rx_tpa)
2220 return -ENOMEM;
2221 }
2222 }
2223 }
2224 return 0;
2225}
2226
2227static void bnxt_free_tx_rings(struct bnxt *bp)
2228{
2229 int i;
2230 struct pci_dev *pdev = bp->pdev;
2231
Michael Chanb6ab4b02016-01-02 23:44:59 -05002232 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002233 return;
2234
2235 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002236 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002237 struct bnxt_ring_struct *ring;
2238
Michael Chanc0c050c2015-10-22 16:01:17 -04002239 if (txr->tx_push) {
2240 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2241 txr->tx_push, txr->tx_push_mapping);
2242 txr->tx_push = NULL;
2243 }
2244
2245 ring = &txr->tx_ring_struct;
2246
2247 bnxt_free_ring(bp, ring);
2248 }
2249}
2250
2251static int bnxt_alloc_tx_rings(struct bnxt *bp)
2252{
2253 int i, j, rc;
2254 struct pci_dev *pdev = bp->pdev;
2255
2256 bp->tx_push_size = 0;
2257 if (bp->tx_push_thresh) {
2258 int push_size;
2259
2260 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2261 bp->tx_push_thresh);
2262
Michael Chan4419dbe2016-02-10 17:33:49 -05002263 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002264 push_size = 0;
2265 bp->tx_push_thresh = 0;
2266 }
2267
2268 bp->tx_push_size = push_size;
2269 }
2270
2271 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002272 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002273 struct bnxt_ring_struct *ring;
2274
Michael Chanc0c050c2015-10-22 16:01:17 -04002275 ring = &txr->tx_ring_struct;
2276
2277 rc = bnxt_alloc_ring(bp, ring);
2278 if (rc)
2279 return rc;
2280
2281 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002282 dma_addr_t mapping;
2283
2284 /* One pre-allocated DMA buffer to backup
2285 * TX push operation
2286 */
2287 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2288 bp->tx_push_size,
2289 &txr->tx_push_mapping,
2290 GFP_KERNEL);
2291
2292 if (!txr->tx_push)
2293 return -ENOMEM;
2294
Michael Chanc0c050c2015-10-22 16:01:17 -04002295 mapping = txr->tx_push_mapping +
2296 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002297 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002298
Michael Chan4419dbe2016-02-10 17:33:49 -05002299 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002300 }
2301 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002302 if (i < bp->tx_nr_rings_xdp)
2303 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002304 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2305 j++;
2306 }
2307 return 0;
2308}
2309
2310static void bnxt_free_cp_rings(struct bnxt *bp)
2311{
2312 int i;
2313
2314 if (!bp->bnapi)
2315 return;
2316
2317 for (i = 0; i < bp->cp_nr_rings; i++) {
2318 struct bnxt_napi *bnapi = bp->bnapi[i];
2319 struct bnxt_cp_ring_info *cpr;
2320 struct bnxt_ring_struct *ring;
2321
2322 if (!bnapi)
2323 continue;
2324
2325 cpr = &bnapi->cp_ring;
2326 ring = &cpr->cp_ring_struct;
2327
2328 bnxt_free_ring(bp, ring);
2329 }
2330}
2331
2332static int bnxt_alloc_cp_rings(struct bnxt *bp)
2333{
2334 int i, rc;
2335
2336 for (i = 0; i < bp->cp_nr_rings; i++) {
2337 struct bnxt_napi *bnapi = bp->bnapi[i];
2338 struct bnxt_cp_ring_info *cpr;
2339 struct bnxt_ring_struct *ring;
2340
2341 if (!bnapi)
2342 continue;
2343
2344 cpr = &bnapi->cp_ring;
2345 ring = &cpr->cp_ring_struct;
2346
2347 rc = bnxt_alloc_ring(bp, ring);
2348 if (rc)
2349 return rc;
2350 }
2351 return 0;
2352}
2353
2354static void bnxt_init_ring_struct(struct bnxt *bp)
2355{
2356 int i;
2357
2358 for (i = 0; i < bp->cp_nr_rings; i++) {
2359 struct bnxt_napi *bnapi = bp->bnapi[i];
2360 struct bnxt_cp_ring_info *cpr;
2361 struct bnxt_rx_ring_info *rxr;
2362 struct bnxt_tx_ring_info *txr;
2363 struct bnxt_ring_struct *ring;
2364
2365 if (!bnapi)
2366 continue;
2367
2368 cpr = &bnapi->cp_ring;
2369 ring = &cpr->cp_ring_struct;
2370 ring->nr_pages = bp->cp_nr_pages;
2371 ring->page_size = HW_CMPD_RING_SIZE;
2372 ring->pg_arr = (void **)cpr->cp_desc_ring;
2373 ring->dma_arr = cpr->cp_desc_mapping;
2374 ring->vmem_size = 0;
2375
Michael Chanb6ab4b02016-01-02 23:44:59 -05002376 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002377 if (!rxr)
2378 goto skip_rx;
2379
Michael Chanc0c050c2015-10-22 16:01:17 -04002380 ring = &rxr->rx_ring_struct;
2381 ring->nr_pages = bp->rx_nr_pages;
2382 ring->page_size = HW_RXBD_RING_SIZE;
2383 ring->pg_arr = (void **)rxr->rx_desc_ring;
2384 ring->dma_arr = rxr->rx_desc_mapping;
2385 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2386 ring->vmem = (void **)&rxr->rx_buf_ring;
2387
2388 ring = &rxr->rx_agg_ring_struct;
2389 ring->nr_pages = bp->rx_agg_nr_pages;
2390 ring->page_size = HW_RXBD_RING_SIZE;
2391 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2392 ring->dma_arr = rxr->rx_agg_desc_mapping;
2393 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2394 ring->vmem = (void **)&rxr->rx_agg_ring;
2395
Michael Chan3b2b7d92016-01-02 23:45:00 -05002396skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002397 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002398 if (!txr)
2399 continue;
2400
Michael Chanc0c050c2015-10-22 16:01:17 -04002401 ring = &txr->tx_ring_struct;
2402 ring->nr_pages = bp->tx_nr_pages;
2403 ring->page_size = HW_RXBD_RING_SIZE;
2404 ring->pg_arr = (void **)txr->tx_desc_ring;
2405 ring->dma_arr = txr->tx_desc_mapping;
2406 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2407 ring->vmem = (void **)&txr->tx_buf_ring;
2408 }
2409}
2410
2411static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2412{
2413 int i;
2414 u32 prod;
2415 struct rx_bd **rx_buf_ring;
2416
2417 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2418 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2419 int j;
2420 struct rx_bd *rxbd;
2421
2422 rxbd = rx_buf_ring[i];
2423 if (!rxbd)
2424 continue;
2425
2426 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2427 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2428 rxbd->rx_bd_opaque = prod;
2429 }
2430 }
2431}
2432
2433static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2434{
2435 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002436 struct bnxt_rx_ring_info *rxr;
2437 struct bnxt_ring_struct *ring;
2438 u32 prod, type;
2439 int i;
2440
Michael Chanc0c050c2015-10-22 16:01:17 -04002441 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2442 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2443
2444 if (NET_IP_ALIGN == 2)
2445 type |= RX_BD_FLAGS_SOP;
2446
Michael Chanb6ab4b02016-01-02 23:44:59 -05002447 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002448 ring = &rxr->rx_ring_struct;
2449 bnxt_init_rxbd_pages(ring, type);
2450
Michael Chanc6d30e82017-02-06 16:55:42 -05002451 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2452 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2453 if (IS_ERR(rxr->xdp_prog)) {
2454 int rc = PTR_ERR(rxr->xdp_prog);
2455
2456 rxr->xdp_prog = NULL;
2457 return rc;
2458 }
2459 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002460 prod = rxr->rx_prod;
2461 for (i = 0; i < bp->rx_ring_size; i++) {
2462 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2463 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2464 ring_nr, i, bp->rx_ring_size);
2465 break;
2466 }
2467 prod = NEXT_RX(prod);
2468 }
2469 rxr->rx_prod = prod;
2470 ring->fw_ring_id = INVALID_HW_RING_ID;
2471
Michael Chanedd0c2c2015-12-27 18:19:19 -05002472 ring = &rxr->rx_agg_ring_struct;
2473 ring->fw_ring_id = INVALID_HW_RING_ID;
2474
Michael Chanc0c050c2015-10-22 16:01:17 -04002475 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2476 return 0;
2477
Michael Chan2839f282016-04-25 02:30:50 -04002478 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002479 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2480
2481 bnxt_init_rxbd_pages(ring, type);
2482
2483 prod = rxr->rx_agg_prod;
2484 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2485 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2486 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2487 ring_nr, i, bp->rx_ring_size);
2488 break;
2489 }
2490 prod = NEXT_RX_AGG(prod);
2491 }
2492 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002493
2494 if (bp->flags & BNXT_FLAG_TPA) {
2495 if (rxr->rx_tpa) {
2496 u8 *data;
2497 dma_addr_t mapping;
2498
2499 for (i = 0; i < MAX_TPA; i++) {
2500 data = __bnxt_alloc_rx_data(bp, &mapping,
2501 GFP_KERNEL);
2502 if (!data)
2503 return -ENOMEM;
2504
2505 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002506 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002507 rxr->rx_tpa[i].mapping = mapping;
2508 }
2509 } else {
2510 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2511 return -ENOMEM;
2512 }
2513 }
2514
2515 return 0;
2516}
2517
Sankar Patchineelam22479252017-03-28 19:47:29 -04002518static void bnxt_init_cp_rings(struct bnxt *bp)
2519{
2520 int i;
2521
2522 for (i = 0; i < bp->cp_nr_rings; i++) {
2523 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2524 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2525
2526 ring->fw_ring_id = INVALID_HW_RING_ID;
2527 }
2528}
2529
Michael Chanc0c050c2015-10-22 16:01:17 -04002530static int bnxt_init_rx_rings(struct bnxt *bp)
2531{
2532 int i, rc = 0;
2533
Michael Chanc61fb992017-02-06 16:55:36 -05002534 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002535 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2536 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002537 } else {
2538 bp->rx_offset = BNXT_RX_OFFSET;
2539 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2540 }
Michael Chanb3dba772017-02-06 16:55:35 -05002541
Michael Chanc0c050c2015-10-22 16:01:17 -04002542 for (i = 0; i < bp->rx_nr_rings; i++) {
2543 rc = bnxt_init_one_rx_ring(bp, i);
2544 if (rc)
2545 break;
2546 }
2547
2548 return rc;
2549}
2550
2551static int bnxt_init_tx_rings(struct bnxt *bp)
2552{
2553 u16 i;
2554
2555 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2556 MAX_SKB_FRAGS + 1);
2557
2558 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002559 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002560 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2561
2562 ring->fw_ring_id = INVALID_HW_RING_ID;
2563 }
2564
2565 return 0;
2566}
2567
2568static void bnxt_free_ring_grps(struct bnxt *bp)
2569{
2570 kfree(bp->grp_info);
2571 bp->grp_info = NULL;
2572}
2573
2574static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2575{
2576 int i;
2577
2578 if (irq_re_init) {
2579 bp->grp_info = kcalloc(bp->cp_nr_rings,
2580 sizeof(struct bnxt_ring_grp_info),
2581 GFP_KERNEL);
2582 if (!bp->grp_info)
2583 return -ENOMEM;
2584 }
2585 for (i = 0; i < bp->cp_nr_rings; i++) {
2586 if (irq_re_init)
2587 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2588 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2589 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2590 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2591 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2592 }
2593 return 0;
2594}
2595
2596static void bnxt_free_vnics(struct bnxt *bp)
2597{
2598 kfree(bp->vnic_info);
2599 bp->vnic_info = NULL;
2600 bp->nr_vnics = 0;
2601}
2602
2603static int bnxt_alloc_vnics(struct bnxt *bp)
2604{
2605 int num_vnics = 1;
2606
2607#ifdef CONFIG_RFS_ACCEL
2608 if (bp->flags & BNXT_FLAG_RFS)
2609 num_vnics += bp->rx_nr_rings;
2610#endif
2611
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002612 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2613 num_vnics++;
2614
Michael Chanc0c050c2015-10-22 16:01:17 -04002615 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2616 GFP_KERNEL);
2617 if (!bp->vnic_info)
2618 return -ENOMEM;
2619
2620 bp->nr_vnics = num_vnics;
2621 return 0;
2622}
2623
2624static void bnxt_init_vnics(struct bnxt *bp)
2625{
2626 int i;
2627
2628 for (i = 0; i < bp->nr_vnics; i++) {
2629 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2630
2631 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002632 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2633 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002634 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2635
2636 if (bp->vnic_info[i].rss_hash_key) {
2637 if (i == 0)
2638 prandom_bytes(vnic->rss_hash_key,
2639 HW_HASH_KEY_SIZE);
2640 else
2641 memcpy(vnic->rss_hash_key,
2642 bp->vnic_info[0].rss_hash_key,
2643 HW_HASH_KEY_SIZE);
2644 }
2645 }
2646}
2647
2648static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2649{
2650 int pages;
2651
2652 pages = ring_size / desc_per_pg;
2653
2654 if (!pages)
2655 return 1;
2656
2657 pages++;
2658
2659 while (pages & (pages - 1))
2660 pages++;
2661
2662 return pages;
2663}
2664
Michael Chanc6d30e82017-02-06 16:55:42 -05002665void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002666{
2667 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002668 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2669 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002670 if (bp->dev->features & NETIF_F_LRO)
2671 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002672 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002673 bp->flags |= BNXT_FLAG_GRO;
2674}
2675
2676/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2677 * be set on entry.
2678 */
2679void bnxt_set_ring_params(struct bnxt *bp)
2680{
2681 u32 ring_size, rx_size, rx_space;
2682 u32 agg_factor = 0, agg_ring_size = 0;
2683
2684 /* 8 for CRC and VLAN */
2685 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2686
2687 rx_space = rx_size + NET_SKB_PAD +
2688 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2689
2690 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2691 ring_size = bp->rx_ring_size;
2692 bp->rx_agg_ring_size = 0;
2693 bp->rx_agg_nr_pages = 0;
2694
2695 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002696 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002697
2698 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002699 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002700 u32 jumbo_factor;
2701
2702 bp->flags |= BNXT_FLAG_JUMBO;
2703 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2704 if (jumbo_factor > agg_factor)
2705 agg_factor = jumbo_factor;
2706 }
2707 agg_ring_size = ring_size * agg_factor;
2708
2709 if (agg_ring_size) {
2710 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2711 RX_DESC_CNT);
2712 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2713 u32 tmp = agg_ring_size;
2714
2715 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2716 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2717 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2718 tmp, agg_ring_size);
2719 }
2720 bp->rx_agg_ring_size = agg_ring_size;
2721 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2722 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2723 rx_space = rx_size + NET_SKB_PAD +
2724 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2725 }
2726
2727 bp->rx_buf_use_size = rx_size;
2728 bp->rx_buf_size = rx_space;
2729
2730 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2731 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2732
2733 ring_size = bp->tx_ring_size;
2734 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2735 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2736
2737 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2738 bp->cp_ring_size = ring_size;
2739
2740 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2741 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2742 bp->cp_nr_pages = MAX_CP_PAGES;
2743 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2744 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2745 ring_size, bp->cp_ring_size);
2746 }
2747 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2748 bp->cp_ring_mask = bp->cp_bit - 1;
2749}
2750
Michael Chanc61fb992017-02-06 16:55:36 -05002751int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002752{
Michael Chanc61fb992017-02-06 16:55:36 -05002753 if (page_mode) {
2754 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2755 return -EOPNOTSUPP;
2756 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2757 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2758 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2759 bp->dev->hw_features &= ~NETIF_F_LRO;
2760 bp->dev->features &= ~NETIF_F_LRO;
2761 bp->rx_dir = DMA_BIDIRECTIONAL;
2762 bp->rx_skb_func = bnxt_rx_page_skb;
2763 } else {
2764 bp->dev->max_mtu = BNXT_MAX_MTU;
2765 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2766 bp->rx_dir = DMA_FROM_DEVICE;
2767 bp->rx_skb_func = bnxt_rx_skb;
2768 }
Michael Chan6bb19472017-02-06 16:55:32 -05002769 return 0;
2770}
2771
Michael Chanc0c050c2015-10-22 16:01:17 -04002772static void bnxt_free_vnic_attributes(struct bnxt *bp)
2773{
2774 int i;
2775 struct bnxt_vnic_info *vnic;
2776 struct pci_dev *pdev = bp->pdev;
2777
2778 if (!bp->vnic_info)
2779 return;
2780
2781 for (i = 0; i < bp->nr_vnics; i++) {
2782 vnic = &bp->vnic_info[i];
2783
2784 kfree(vnic->fw_grp_ids);
2785 vnic->fw_grp_ids = NULL;
2786
2787 kfree(vnic->uc_list);
2788 vnic->uc_list = NULL;
2789
2790 if (vnic->mc_list) {
2791 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2792 vnic->mc_list, vnic->mc_list_mapping);
2793 vnic->mc_list = NULL;
2794 }
2795
2796 if (vnic->rss_table) {
2797 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2798 vnic->rss_table,
2799 vnic->rss_table_dma_addr);
2800 vnic->rss_table = NULL;
2801 }
2802
2803 vnic->rss_hash_key = NULL;
2804 vnic->flags = 0;
2805 }
2806}
2807
2808static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2809{
2810 int i, rc = 0, size;
2811 struct bnxt_vnic_info *vnic;
2812 struct pci_dev *pdev = bp->pdev;
2813 int max_rings;
2814
2815 for (i = 0; i < bp->nr_vnics; i++) {
2816 vnic = &bp->vnic_info[i];
2817
2818 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2819 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2820
2821 if (mem_size > 0) {
2822 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2823 if (!vnic->uc_list) {
2824 rc = -ENOMEM;
2825 goto out;
2826 }
2827 }
2828 }
2829
2830 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2831 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2832 vnic->mc_list =
2833 dma_alloc_coherent(&pdev->dev,
2834 vnic->mc_list_size,
2835 &vnic->mc_list_mapping,
2836 GFP_KERNEL);
2837 if (!vnic->mc_list) {
2838 rc = -ENOMEM;
2839 goto out;
2840 }
2841 }
2842
2843 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2844 max_rings = bp->rx_nr_rings;
2845 else
2846 max_rings = 1;
2847
2848 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2849 if (!vnic->fw_grp_ids) {
2850 rc = -ENOMEM;
2851 goto out;
2852 }
2853
Michael Chanae10ae72016-12-29 12:13:38 -05002854 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2855 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2856 continue;
2857
Michael Chanc0c050c2015-10-22 16:01:17 -04002858 /* Allocate rss table and hash key */
2859 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2860 &vnic->rss_table_dma_addr,
2861 GFP_KERNEL);
2862 if (!vnic->rss_table) {
2863 rc = -ENOMEM;
2864 goto out;
2865 }
2866
2867 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2868
2869 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2870 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2871 }
2872 return 0;
2873
2874out:
2875 return rc;
2876}
2877
2878static void bnxt_free_hwrm_resources(struct bnxt *bp)
2879{
2880 struct pci_dev *pdev = bp->pdev;
2881
2882 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2883 bp->hwrm_cmd_resp_dma_addr);
2884
2885 bp->hwrm_cmd_resp_addr = NULL;
2886 if (bp->hwrm_dbg_resp_addr) {
2887 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2888 bp->hwrm_dbg_resp_addr,
2889 bp->hwrm_dbg_resp_dma_addr);
2890
2891 bp->hwrm_dbg_resp_addr = NULL;
2892 }
2893}
2894
2895static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2896{
2897 struct pci_dev *pdev = bp->pdev;
2898
2899 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2900 &bp->hwrm_cmd_resp_dma_addr,
2901 GFP_KERNEL);
2902 if (!bp->hwrm_cmd_resp_addr)
2903 return -ENOMEM;
2904 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2905 HWRM_DBG_REG_BUF_SIZE,
2906 &bp->hwrm_dbg_resp_dma_addr,
2907 GFP_KERNEL);
2908 if (!bp->hwrm_dbg_resp_addr)
2909 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2910
2911 return 0;
2912}
2913
Deepak Khungare605db82017-05-29 19:06:04 -04002914static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2915{
2916 if (bp->hwrm_short_cmd_req_addr) {
2917 struct pci_dev *pdev = bp->pdev;
2918
2919 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2920 bp->hwrm_short_cmd_req_addr,
2921 bp->hwrm_short_cmd_req_dma_addr);
2922 bp->hwrm_short_cmd_req_addr = NULL;
2923 }
2924}
2925
2926static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2927{
2928 struct pci_dev *pdev = bp->pdev;
2929
2930 bp->hwrm_short_cmd_req_addr =
2931 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2932 &bp->hwrm_short_cmd_req_dma_addr,
2933 GFP_KERNEL);
2934 if (!bp->hwrm_short_cmd_req_addr)
2935 return -ENOMEM;
2936
2937 return 0;
2938}
2939
Michael Chanc0c050c2015-10-22 16:01:17 -04002940static void bnxt_free_stats(struct bnxt *bp)
2941{
2942 u32 size, i;
2943 struct pci_dev *pdev = bp->pdev;
2944
Michael Chan3bdf56c2016-03-07 15:38:45 -05002945 if (bp->hw_rx_port_stats) {
2946 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2947 bp->hw_rx_port_stats,
2948 bp->hw_rx_port_stats_map);
2949 bp->hw_rx_port_stats = NULL;
2950 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2951 }
2952
Michael Chanc0c050c2015-10-22 16:01:17 -04002953 if (!bp->bnapi)
2954 return;
2955
2956 size = sizeof(struct ctx_hw_stats);
2957
2958 for (i = 0; i < bp->cp_nr_rings; i++) {
2959 struct bnxt_napi *bnapi = bp->bnapi[i];
2960 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2961
2962 if (cpr->hw_stats) {
2963 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2964 cpr->hw_stats_map);
2965 cpr->hw_stats = NULL;
2966 }
2967 }
2968}
2969
2970static int bnxt_alloc_stats(struct bnxt *bp)
2971{
2972 u32 size, i;
2973 struct pci_dev *pdev = bp->pdev;
2974
2975 size = sizeof(struct ctx_hw_stats);
2976
2977 for (i = 0; i < bp->cp_nr_rings; i++) {
2978 struct bnxt_napi *bnapi = bp->bnapi[i];
2979 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2980
2981 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2982 &cpr->hw_stats_map,
2983 GFP_KERNEL);
2984 if (!cpr->hw_stats)
2985 return -ENOMEM;
2986
2987 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2988 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002989
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002990 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002991 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2992 sizeof(struct tx_port_stats) + 1024;
2993
2994 bp->hw_rx_port_stats =
2995 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2996 &bp->hw_rx_port_stats_map,
2997 GFP_KERNEL);
2998 if (!bp->hw_rx_port_stats)
2999 return -ENOMEM;
3000
3001 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3002 512;
3003 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3004 sizeof(struct rx_port_stats) + 512;
3005 bp->flags |= BNXT_FLAG_PORT_STATS;
3006 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003007 return 0;
3008}
3009
3010static void bnxt_clear_ring_indices(struct bnxt *bp)
3011{
3012 int i;
3013
3014 if (!bp->bnapi)
3015 return;
3016
3017 for (i = 0; i < bp->cp_nr_rings; i++) {
3018 struct bnxt_napi *bnapi = bp->bnapi[i];
3019 struct bnxt_cp_ring_info *cpr;
3020 struct bnxt_rx_ring_info *rxr;
3021 struct bnxt_tx_ring_info *txr;
3022
3023 if (!bnapi)
3024 continue;
3025
3026 cpr = &bnapi->cp_ring;
3027 cpr->cp_raw_cons = 0;
3028
Michael Chanb6ab4b02016-01-02 23:44:59 -05003029 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003030 if (txr) {
3031 txr->tx_prod = 0;
3032 txr->tx_cons = 0;
3033 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003034
Michael Chanb6ab4b02016-01-02 23:44:59 -05003035 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003036 if (rxr) {
3037 rxr->rx_prod = 0;
3038 rxr->rx_agg_prod = 0;
3039 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003040 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003041 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003042 }
3043}
3044
3045static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3046{
3047#ifdef CONFIG_RFS_ACCEL
3048 int i;
3049
3050 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3051 * safe to delete the hash table.
3052 */
3053 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3054 struct hlist_head *head;
3055 struct hlist_node *tmp;
3056 struct bnxt_ntuple_filter *fltr;
3057
3058 head = &bp->ntp_fltr_hash_tbl[i];
3059 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3060 hlist_del(&fltr->hash);
3061 kfree(fltr);
3062 }
3063 }
3064 if (irq_reinit) {
3065 kfree(bp->ntp_fltr_bmap);
3066 bp->ntp_fltr_bmap = NULL;
3067 }
3068 bp->ntp_fltr_count = 0;
3069#endif
3070}
3071
3072static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3073{
3074#ifdef CONFIG_RFS_ACCEL
3075 int i, rc = 0;
3076
3077 if (!(bp->flags & BNXT_FLAG_RFS))
3078 return 0;
3079
3080 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3081 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3082
3083 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003084 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3085 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003086 GFP_KERNEL);
3087
3088 if (!bp->ntp_fltr_bmap)
3089 rc = -ENOMEM;
3090
3091 return rc;
3092#else
3093 return 0;
3094#endif
3095}
3096
3097static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3098{
3099 bnxt_free_vnic_attributes(bp);
3100 bnxt_free_tx_rings(bp);
3101 bnxt_free_rx_rings(bp);
3102 bnxt_free_cp_rings(bp);
3103 bnxt_free_ntp_fltrs(bp, irq_re_init);
3104 if (irq_re_init) {
3105 bnxt_free_stats(bp);
3106 bnxt_free_ring_grps(bp);
3107 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003108 kfree(bp->tx_ring_map);
3109 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003110 kfree(bp->tx_ring);
3111 bp->tx_ring = NULL;
3112 kfree(bp->rx_ring);
3113 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003114 kfree(bp->bnapi);
3115 bp->bnapi = NULL;
3116 } else {
3117 bnxt_clear_ring_indices(bp);
3118 }
3119}
3120
3121static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3122{
Michael Chan01657bc2016-01-02 23:45:03 -05003123 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003124 void *bnapi;
3125
3126 if (irq_re_init) {
3127 /* Allocate bnapi mem pointer array and mem block for
3128 * all queues
3129 */
3130 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3131 bp->cp_nr_rings);
3132 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3133 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3134 if (!bnapi)
3135 return -ENOMEM;
3136
3137 bp->bnapi = bnapi;
3138 bnapi += arr_size;
3139 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3140 bp->bnapi[i] = bnapi;
3141 bp->bnapi[i]->index = i;
3142 bp->bnapi[i]->bp = bp;
3143 }
3144
Michael Chanb6ab4b02016-01-02 23:44:59 -05003145 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3146 sizeof(struct bnxt_rx_ring_info),
3147 GFP_KERNEL);
3148 if (!bp->rx_ring)
3149 return -ENOMEM;
3150
3151 for (i = 0; i < bp->rx_nr_rings; i++) {
3152 bp->rx_ring[i].bnapi = bp->bnapi[i];
3153 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3154 }
3155
3156 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3157 sizeof(struct bnxt_tx_ring_info),
3158 GFP_KERNEL);
3159 if (!bp->tx_ring)
3160 return -ENOMEM;
3161
Michael Chana960dec2017-02-06 16:55:39 -05003162 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3163 GFP_KERNEL);
3164
3165 if (!bp->tx_ring_map)
3166 return -ENOMEM;
3167
Michael Chan01657bc2016-01-02 23:45:03 -05003168 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3169 j = 0;
3170 else
3171 j = bp->rx_nr_rings;
3172
3173 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3174 bp->tx_ring[i].bnapi = bp->bnapi[j];
3175 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003176 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003177 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003178 bp->tx_ring[i].txq_index = i -
3179 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003180 bp->bnapi[j]->tx_int = bnxt_tx_int;
3181 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003182 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003183 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3184 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003185 }
3186
Michael Chanc0c050c2015-10-22 16:01:17 -04003187 rc = bnxt_alloc_stats(bp);
3188 if (rc)
3189 goto alloc_mem_err;
3190
3191 rc = bnxt_alloc_ntp_fltrs(bp);
3192 if (rc)
3193 goto alloc_mem_err;
3194
3195 rc = bnxt_alloc_vnics(bp);
3196 if (rc)
3197 goto alloc_mem_err;
3198 }
3199
3200 bnxt_init_ring_struct(bp);
3201
3202 rc = bnxt_alloc_rx_rings(bp);
3203 if (rc)
3204 goto alloc_mem_err;
3205
3206 rc = bnxt_alloc_tx_rings(bp);
3207 if (rc)
3208 goto alloc_mem_err;
3209
3210 rc = bnxt_alloc_cp_rings(bp);
3211 if (rc)
3212 goto alloc_mem_err;
3213
3214 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3215 BNXT_VNIC_UCAST_FLAG;
3216 rc = bnxt_alloc_vnic_attributes(bp);
3217 if (rc)
3218 goto alloc_mem_err;
3219 return 0;
3220
3221alloc_mem_err:
3222 bnxt_free_mem(bp, true);
3223 return rc;
3224}
3225
Michael Chan9d8bc092016-12-29 12:13:33 -05003226static void bnxt_disable_int(struct bnxt *bp)
3227{
3228 int i;
3229
3230 if (!bp->bnapi)
3231 return;
3232
3233 for (i = 0; i < bp->cp_nr_rings; i++) {
3234 struct bnxt_napi *bnapi = bp->bnapi[i];
3235 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003236 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003237
Michael Chandaf1f1e2017-02-20 19:25:17 -05003238 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3239 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003240 }
3241}
3242
3243static void bnxt_disable_int_sync(struct bnxt *bp)
3244{
3245 int i;
3246
3247 atomic_inc(&bp->intr_sem);
3248
3249 bnxt_disable_int(bp);
3250 for (i = 0; i < bp->cp_nr_rings; i++)
3251 synchronize_irq(bp->irq_tbl[i].vector);
3252}
3253
3254static void bnxt_enable_int(struct bnxt *bp)
3255{
3256 int i;
3257
3258 atomic_set(&bp->intr_sem, 0);
3259 for (i = 0; i < bp->cp_nr_rings; i++) {
3260 struct bnxt_napi *bnapi = bp->bnapi[i];
3261 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3262
3263 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3264 }
3265}
3266
Michael Chanc0c050c2015-10-22 16:01:17 -04003267void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3268 u16 cmpl_ring, u16 target_id)
3269{
Michael Chana8643e12016-02-26 04:00:05 -05003270 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003271
Michael Chana8643e12016-02-26 04:00:05 -05003272 req->req_type = cpu_to_le16(req_type);
3273 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3274 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003275 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3276}
3277
Michael Chanfbfbc482016-02-26 04:00:07 -05003278static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3279 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003280{
Michael Chana11fa2b2016-05-15 03:04:47 -04003281 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003282 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003283 u32 *data = msg;
3284 __le32 *resp_len, *valid;
3285 u16 cp_ring_id, len = 0;
3286 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003287 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003288
Michael Chana8643e12016-02-26 04:00:05 -05003289 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003290 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003291 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003292 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3293
Deepak Khungare605db82017-05-29 19:06:04 -04003294 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3295 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3296 struct hwrm_short_input short_input = {0};
3297
3298 memcpy(short_cmd_req, req, msg_len);
3299 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3300 msg_len);
3301
3302 short_input.req_type = req->req_type;
3303 short_input.signature =
3304 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3305 short_input.size = cpu_to_le16(msg_len);
3306 short_input.req_addr =
3307 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3308
3309 data = (u32 *)&short_input;
3310 msg_len = sizeof(short_input);
3311
3312 /* Sync memory write before updating doorbell */
3313 wmb();
3314
3315 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3316 }
3317
Michael Chanc0c050c2015-10-22 16:01:17 -04003318 /* Write request msg to hwrm channel */
3319 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3320
Deepak Khungare605db82017-05-29 19:06:04 -04003321 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003322 writel(0, bp->bar0 + i);
3323
Michael Chanc0c050c2015-10-22 16:01:17 -04003324 /* currently supports only one outstanding message */
3325 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003326 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003327
3328 /* Ring channel doorbell */
3329 writel(1, bp->bar0 + 0x100);
3330
Michael Chanff4fe812016-02-26 04:00:04 -05003331 if (!timeout)
3332 timeout = DFLT_HWRM_CMD_TIMEOUT;
3333
Michael Chanc0c050c2015-10-22 16:01:17 -04003334 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003335 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003336 if (intr_process) {
3337 /* Wait until hwrm response cmpl interrupt is processed */
3338 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003339 i++ < tmo_count) {
3340 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003341 }
3342
3343 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3344 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003345 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003346 return -1;
3347 }
3348 } else {
3349 /* Check if response len is updated */
3350 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003351 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003352 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3353 HWRM_RESP_LEN_SFT;
3354 if (len)
3355 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003356 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003357 }
3358
Michael Chana11fa2b2016-05-15 03:04:47 -04003359 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003360 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003361 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003362 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003363 return -1;
3364 }
3365
3366 /* Last word of resp contains valid bit */
3367 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003368 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003369 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3370 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003371 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003372 }
3373
Michael Chana11fa2b2016-05-15 03:04:47 -04003374 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003375 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003376 timeout, le16_to_cpu(req->req_type),
3377 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003378 return -1;
3379 }
3380 }
3381
3382 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003383 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003384 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3385 le16_to_cpu(resp->req_type),
3386 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003387 return rc;
3388}
3389
3390int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3391{
3392 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003393}
3394
3395int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3396{
3397 int rc;
3398
3399 mutex_lock(&bp->hwrm_cmd_lock);
3400 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3401 mutex_unlock(&bp->hwrm_cmd_lock);
3402 return rc;
3403}
3404
Michael Chan90e209212016-02-26 04:00:08 -05003405int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3406 int timeout)
3407{
3408 int rc;
3409
3410 mutex_lock(&bp->hwrm_cmd_lock);
3411 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3412 mutex_unlock(&bp->hwrm_cmd_lock);
3413 return rc;
3414}
3415
Michael Chana1653b12016-12-07 00:26:20 -05003416int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3417 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003418{
3419 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003420 DECLARE_BITMAP(async_events_bmap, 256);
3421 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003422 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003423
3424 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3425
3426 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003427 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003428
Michael Chan25be8622016-04-05 14:09:00 -04003429 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3430 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3431 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3432
Michael Chana1653b12016-12-07 00:26:20 -05003433 if (bmap && bmap_size) {
3434 for (i = 0; i < bmap_size; i++) {
3435 if (test_bit(i, bmap))
3436 __set_bit(i, async_events_bmap);
3437 }
3438 }
3439
Michael Chan25be8622016-04-05 14:09:00 -04003440 for (i = 0; i < 8; i++)
3441 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3442
Michael Chana1653b12016-12-07 00:26:20 -05003443 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3444}
3445
3446static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3447{
3448 struct hwrm_func_drv_rgtr_input req = {0};
3449
3450 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3451
3452 req.enables =
3453 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3454 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3455
Michael Chan11f15ed2016-04-05 14:08:55 -04003456 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003457 req.ver_maj = DRV_VER_MAJ;
3458 req.ver_min = DRV_VER_MIN;
3459 req.ver_upd = DRV_VER_UPD;
3460
3461 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003462 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003463 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003464
Michael Chan9b0436c2017-07-11 13:05:36 -04003465 memset(data, 0, sizeof(data));
3466 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3467 u16 cmd = bnxt_vf_req_snif[i];
3468 unsigned int bit, idx;
3469
3470 idx = cmd / 32;
3471 bit = cmd % 32;
3472 data[idx] |= 1 << bit;
3473 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003474
Michael Chande68f5de2015-12-09 19:35:41 -05003475 for (i = 0; i < 8; i++)
3476 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3477
Michael Chanc0c050c2015-10-22 16:01:17 -04003478 req.enables |=
3479 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3480 }
3481
3482 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3483}
3484
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003485static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3486{
3487 struct hwrm_func_drv_unrgtr_input req = {0};
3488
3489 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3490 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3491}
3492
Michael Chanc0c050c2015-10-22 16:01:17 -04003493static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3494{
3495 u32 rc = 0;
3496 struct hwrm_tunnel_dst_port_free_input req = {0};
3497
3498 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3499 req.tunnel_type = tunnel_type;
3500
3501 switch (tunnel_type) {
3502 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3503 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3504 break;
3505 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3506 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3507 break;
3508 default:
3509 break;
3510 }
3511
3512 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3513 if (rc)
3514 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3515 rc);
3516 return rc;
3517}
3518
3519static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3520 u8 tunnel_type)
3521{
3522 u32 rc = 0;
3523 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3524 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3525
3526 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3527
3528 req.tunnel_type = tunnel_type;
3529 req.tunnel_dst_port_val = port;
3530
3531 mutex_lock(&bp->hwrm_cmd_lock);
3532 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3533 if (rc) {
3534 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3535 rc);
3536 goto err_out;
3537 }
3538
Christophe Jaillet57aac712016-11-22 06:14:40 +01003539 switch (tunnel_type) {
3540 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003541 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003542 break;
3543 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003544 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003545 break;
3546 default:
3547 break;
3548 }
3549
Michael Chanc0c050c2015-10-22 16:01:17 -04003550err_out:
3551 mutex_unlock(&bp->hwrm_cmd_lock);
3552 return rc;
3553}
3554
3555static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3556{
3557 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3558 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3559
3560 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003561 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003562
3563 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3564 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3565 req.mask = cpu_to_le32(vnic->rx_mask);
3566 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3567}
3568
3569#ifdef CONFIG_RFS_ACCEL
3570static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3571 struct bnxt_ntuple_filter *fltr)
3572{
3573 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3574
3575 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3576 req.ntuple_filter_id = fltr->filter_id;
3577 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3578}
3579
3580#define BNXT_NTP_FLTR_FLAGS \
3581 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3582 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3583 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3584 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3585 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3586 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3587 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3588 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3589 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3590 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3591 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3592 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3593 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003594 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003595
Michael Chan61aad722017-02-12 19:18:14 -05003596#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3597 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3598
Michael Chanc0c050c2015-10-22 16:01:17 -04003599static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3600 struct bnxt_ntuple_filter *fltr)
3601{
3602 int rc = 0;
3603 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3604 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3605 bp->hwrm_cmd_resp_addr;
3606 struct flow_keys *keys = &fltr->fkeys;
3607 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3608
3609 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003610 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003611
3612 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3613
3614 req.ethertype = htons(ETH_P_IP);
3615 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003616 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003617 req.ip_protocol = keys->basic.ip_proto;
3618
Michael Chandda0e742016-12-29 12:13:40 -05003619 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3620 int i;
3621
3622 req.ethertype = htons(ETH_P_IPV6);
3623 req.ip_addr_type =
3624 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3625 *(struct in6_addr *)&req.src_ipaddr[0] =
3626 keys->addrs.v6addrs.src;
3627 *(struct in6_addr *)&req.dst_ipaddr[0] =
3628 keys->addrs.v6addrs.dst;
3629 for (i = 0; i < 4; i++) {
3630 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3631 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3632 }
3633 } else {
3634 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3635 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3636 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3637 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3638 }
Michael Chan61aad722017-02-12 19:18:14 -05003639 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3640 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3641 req.tunnel_type =
3642 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3643 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003644
3645 req.src_port = keys->ports.src;
3646 req.src_port_mask = cpu_to_be16(0xffff);
3647 req.dst_port = keys->ports.dst;
3648 req.dst_port_mask = cpu_to_be16(0xffff);
3649
Michael Chanc1935542015-12-27 18:19:28 -05003650 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003651 mutex_lock(&bp->hwrm_cmd_lock);
3652 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3653 if (!rc)
3654 fltr->filter_id = resp->ntuple_filter_id;
3655 mutex_unlock(&bp->hwrm_cmd_lock);
3656 return rc;
3657}
3658#endif
3659
3660static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3661 u8 *mac_addr)
3662{
3663 u32 rc = 0;
3664 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3665 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3666
3667 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003668 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3669 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3670 req.flags |=
3671 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003672 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003673 req.enables =
3674 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003675 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003676 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3677 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3678 req.l2_addr_mask[0] = 0xff;
3679 req.l2_addr_mask[1] = 0xff;
3680 req.l2_addr_mask[2] = 0xff;
3681 req.l2_addr_mask[3] = 0xff;
3682 req.l2_addr_mask[4] = 0xff;
3683 req.l2_addr_mask[5] = 0xff;
3684
3685 mutex_lock(&bp->hwrm_cmd_lock);
3686 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3687 if (!rc)
3688 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3689 resp->l2_filter_id;
3690 mutex_unlock(&bp->hwrm_cmd_lock);
3691 return rc;
3692}
3693
3694static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3695{
3696 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3697 int rc = 0;
3698
3699 /* Any associated ntuple filters will also be cleared by firmware. */
3700 mutex_lock(&bp->hwrm_cmd_lock);
3701 for (i = 0; i < num_of_vnics; i++) {
3702 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3703
3704 for (j = 0; j < vnic->uc_filter_count; j++) {
3705 struct hwrm_cfa_l2_filter_free_input req = {0};
3706
3707 bnxt_hwrm_cmd_hdr_init(bp, &req,
3708 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3709
3710 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3711
3712 rc = _hwrm_send_message(bp, &req, sizeof(req),
3713 HWRM_CMD_TIMEOUT);
3714 }
3715 vnic->uc_filter_count = 0;
3716 }
3717 mutex_unlock(&bp->hwrm_cmd_lock);
3718
3719 return rc;
3720}
3721
3722static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3723{
3724 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3725 struct hwrm_vnic_tpa_cfg_input req = {0};
3726
3727 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3728
3729 if (tpa_flags) {
3730 u16 mss = bp->dev->mtu - 40;
3731 u32 nsegs, n, segs = 0, flags;
3732
3733 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3734 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3735 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3736 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3737 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3738 if (tpa_flags & BNXT_FLAG_GRO)
3739 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3740
3741 req.flags = cpu_to_le32(flags);
3742
3743 req.enables =
3744 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003745 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3746 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003747
3748 /* Number of segs are log2 units, and first packet is not
3749 * included as part of this units.
3750 */
Michael Chan2839f282016-04-25 02:30:50 -04003751 if (mss <= BNXT_RX_PAGE_SIZE) {
3752 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003753 nsegs = (MAX_SKB_FRAGS - 1) * n;
3754 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003755 n = mss / BNXT_RX_PAGE_SIZE;
3756 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003757 n++;
3758 nsegs = (MAX_SKB_FRAGS - n) / n;
3759 }
3760
3761 segs = ilog2(nsegs);
3762 req.max_agg_segs = cpu_to_le16(segs);
3763 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003764
3765 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003766 }
3767 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3768
3769 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3770}
3771
3772static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3773{
3774 u32 i, j, max_rings;
3775 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3776 struct hwrm_vnic_rss_cfg_input req = {0};
3777
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003778 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003779 return 0;
3780
3781 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3782 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003783 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003784 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3785 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3786 max_rings = bp->rx_nr_rings - 1;
3787 else
3788 max_rings = bp->rx_nr_rings;
3789 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003790 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003791 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003792
3793 /* Fill the RSS indirection table with ring group ids */
3794 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3795 if (j == max_rings)
3796 j = 0;
3797 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3798 }
3799
3800 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3801 req.hash_key_tbl_addr =
3802 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3803 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003804 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003805 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3806}
3807
3808static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3809{
3810 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3811 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3812
3813 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3814 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3815 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3816 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3817 req.enables =
3818 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3819 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3820 /* thresholds not implemented in firmware yet */
3821 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3822 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3823 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3824 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3825}
3826
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003827static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3828 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003829{
3830 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3831
3832 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3833 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003834 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003835
3836 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003837 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003838}
3839
3840static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3841{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003842 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003843
3844 for (i = 0; i < bp->nr_vnics; i++) {
3845 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3846
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003847 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3848 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3849 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3850 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003851 }
3852 bp->rsscos_nr_ctxs = 0;
3853}
3854
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003855static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003856{
3857 int rc;
3858 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3859 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3860 bp->hwrm_cmd_resp_addr;
3861
3862 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3863 -1);
3864
3865 mutex_lock(&bp->hwrm_cmd_lock);
3866 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3867 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003868 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003869 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3870 mutex_unlock(&bp->hwrm_cmd_lock);
3871
3872 return rc;
3873}
3874
Michael Chana588e452016-12-07 00:26:21 -05003875int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003876{
Michael Chanb81a90d2016-01-02 23:45:01 -05003877 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003878 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3879 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003880 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003881
3882 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003883
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003884 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3885 /* Only RSS support for now TBD: COS & LB */
3886 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3887 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3888 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3889 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003890 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3891 req.rss_rule =
3892 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3893 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3894 VNIC_CFG_REQ_ENABLES_MRU);
3895 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003896 } else {
3897 req.rss_rule = cpu_to_le16(0xffff);
3898 }
3899
3900 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3901 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003902 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3903 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3904 } else {
3905 req.cos_rule = cpu_to_le16(0xffff);
3906 }
3907
Michael Chanc0c050c2015-10-22 16:01:17 -04003908 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003909 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003910 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003911 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003912 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3913 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003914
Michael Chanb81a90d2016-01-02 23:45:01 -05003915 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003916 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3917 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3918
3919 req.lb_rule = cpu_to_le16(0xffff);
3920 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3921 VLAN_HLEN);
3922
Michael Chancf6645f2016-06-13 02:25:28 -04003923#ifdef CONFIG_BNXT_SRIOV
3924 if (BNXT_VF(bp))
3925 def_vlan = bp->vf.vlan;
3926#endif
3927 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003928 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003929 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3930 req.flags |=
3931 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003932
3933 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3934}
3935
3936static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3937{
3938 u32 rc = 0;
3939
3940 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3941 struct hwrm_vnic_free_input req = {0};
3942
3943 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3944 req.vnic_id =
3945 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3946
3947 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3948 if (rc)
3949 return rc;
3950 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3951 }
3952 return rc;
3953}
3954
3955static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3956{
3957 u16 i;
3958
3959 for (i = 0; i < bp->nr_vnics; i++)
3960 bnxt_hwrm_vnic_free_one(bp, i);
3961}
3962
Michael Chanb81a90d2016-01-02 23:45:01 -05003963static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3964 unsigned int start_rx_ring_idx,
3965 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003966{
Michael Chanb81a90d2016-01-02 23:45:01 -05003967 int rc = 0;
3968 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003969 struct hwrm_vnic_alloc_input req = {0};
3970 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3971
3972 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003973 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3974 grp_idx = bp->rx_ring[i].bnapi->index;
3975 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003976 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003977 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003978 break;
3979 }
3980 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003981 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003982 }
3983
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003984 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3985 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003986 if (vnic_id == 0)
3987 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3988
3989 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3990
3991 mutex_lock(&bp->hwrm_cmd_lock);
3992 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3993 if (!rc)
3994 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3995 mutex_unlock(&bp->hwrm_cmd_lock);
3996 return rc;
3997}
3998
Michael Chan8fdefd62016-12-29 12:13:36 -05003999static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4000{
4001 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4002 struct hwrm_vnic_qcaps_input req = {0};
4003 int rc;
4004
4005 if (bp->hwrm_spec_code < 0x10600)
4006 return 0;
4007
4008 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4009 mutex_lock(&bp->hwrm_cmd_lock);
4010 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4011 if (!rc) {
4012 if (resp->flags &
4013 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4014 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4015 }
4016 mutex_unlock(&bp->hwrm_cmd_lock);
4017 return rc;
4018}
4019
Michael Chanc0c050c2015-10-22 16:01:17 -04004020static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4021{
4022 u16 i;
4023 u32 rc = 0;
4024
4025 mutex_lock(&bp->hwrm_cmd_lock);
4026 for (i = 0; i < bp->rx_nr_rings; i++) {
4027 struct hwrm_ring_grp_alloc_input req = {0};
4028 struct hwrm_ring_grp_alloc_output *resp =
4029 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004030 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004031
4032 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4033
Michael Chanb81a90d2016-01-02 23:45:01 -05004034 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4035 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4036 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4037 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004038
4039 rc = _hwrm_send_message(bp, &req, sizeof(req),
4040 HWRM_CMD_TIMEOUT);
4041 if (rc)
4042 break;
4043
Michael Chanb81a90d2016-01-02 23:45:01 -05004044 bp->grp_info[grp_idx].fw_grp_id =
4045 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004046 }
4047 mutex_unlock(&bp->hwrm_cmd_lock);
4048 return rc;
4049}
4050
4051static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4052{
4053 u16 i;
4054 u32 rc = 0;
4055 struct hwrm_ring_grp_free_input req = {0};
4056
4057 if (!bp->grp_info)
4058 return 0;
4059
4060 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4061
4062 mutex_lock(&bp->hwrm_cmd_lock);
4063 for (i = 0; i < bp->cp_nr_rings; i++) {
4064 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4065 continue;
4066 req.ring_group_id =
4067 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4068
4069 rc = _hwrm_send_message(bp, &req, sizeof(req),
4070 HWRM_CMD_TIMEOUT);
4071 if (rc)
4072 break;
4073 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4074 }
4075 mutex_unlock(&bp->hwrm_cmd_lock);
4076 return rc;
4077}
4078
4079static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4080 struct bnxt_ring_struct *ring,
4081 u32 ring_type, u32 map_index,
4082 u32 stats_ctx_id)
4083{
4084 int rc = 0, err = 0;
4085 struct hwrm_ring_alloc_input req = {0};
4086 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4087 u16 ring_id;
4088
4089 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4090
4091 req.enables = 0;
4092 if (ring->nr_pages > 1) {
4093 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4094 /* Page size is in log2 units */
4095 req.page_size = BNXT_PAGE_SHIFT;
4096 req.page_tbl_depth = 1;
4097 } else {
4098 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4099 }
4100 req.fbo = 0;
4101 /* Association of ring index with doorbell index and MSIX number */
4102 req.logical_id = cpu_to_le16(map_index);
4103
4104 switch (ring_type) {
4105 case HWRM_RING_ALLOC_TX:
4106 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4107 /* Association of transmit ring with completion ring */
4108 req.cmpl_ring_id =
4109 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4110 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4111 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4112 req.queue_id = cpu_to_le16(ring->queue_id);
4113 break;
4114 case HWRM_RING_ALLOC_RX:
4115 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4116 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4117 break;
4118 case HWRM_RING_ALLOC_AGG:
4119 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4120 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4121 break;
4122 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004123 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004124 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4125 if (bp->flags & BNXT_FLAG_USING_MSIX)
4126 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4127 break;
4128 default:
4129 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4130 ring_type);
4131 return -1;
4132 }
4133
4134 mutex_lock(&bp->hwrm_cmd_lock);
4135 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4136 err = le16_to_cpu(resp->error_code);
4137 ring_id = le16_to_cpu(resp->ring_id);
4138 mutex_unlock(&bp->hwrm_cmd_lock);
4139
4140 if (rc || err) {
4141 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004142 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004143 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4144 rc, err);
4145 return -1;
4146
4147 case RING_FREE_REQ_RING_TYPE_RX:
4148 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4149 rc, err);
4150 return -1;
4151
4152 case RING_FREE_REQ_RING_TYPE_TX:
4153 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4154 rc, err);
4155 return -1;
4156
4157 default:
4158 netdev_err(bp->dev, "Invalid ring\n");
4159 return -1;
4160 }
4161 }
4162 ring->fw_ring_id = ring_id;
4163 return rc;
4164}
4165
Michael Chan486b5c22016-12-29 12:13:42 -05004166static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4167{
4168 int rc;
4169
4170 if (BNXT_PF(bp)) {
4171 struct hwrm_func_cfg_input req = {0};
4172
4173 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4174 req.fid = cpu_to_le16(0xffff);
4175 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4176 req.async_event_cr = cpu_to_le16(idx);
4177 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4178 } else {
4179 struct hwrm_func_vf_cfg_input req = {0};
4180
4181 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4182 req.enables =
4183 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4184 req.async_event_cr = cpu_to_le16(idx);
4185 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4186 }
4187 return rc;
4188}
4189
Michael Chanc0c050c2015-10-22 16:01:17 -04004190static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4191{
4192 int i, rc = 0;
4193
Michael Chanedd0c2c2015-12-27 18:19:19 -05004194 for (i = 0; i < bp->cp_nr_rings; i++) {
4195 struct bnxt_napi *bnapi = bp->bnapi[i];
4196 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4197 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004198
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004199 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004200 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4201 INVALID_STATS_CTX_ID);
4202 if (rc)
4203 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004204 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4205 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004206
4207 if (!i) {
4208 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4209 if (rc)
4210 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4211 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004212 }
4213
Michael Chanedd0c2c2015-12-27 18:19:19 -05004214 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004215 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004216 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004217 u32 map_idx = txr->bnapi->index;
4218 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004219
Michael Chanb81a90d2016-01-02 23:45:01 -05004220 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4221 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004222 if (rc)
4223 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004224 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004225 }
4226
Michael Chanedd0c2c2015-12-27 18:19:19 -05004227 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004228 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004229 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004230 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004231
Michael Chanb81a90d2016-01-02 23:45:01 -05004232 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4233 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004234 if (rc)
4235 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004236 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004237 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004238 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004239 }
4240
4241 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4242 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004243 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004244 struct bnxt_ring_struct *ring =
4245 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004246 u32 grp_idx = rxr->bnapi->index;
4247 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004248
4249 rc = hwrm_ring_alloc_send_msg(bp, ring,
4250 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004251 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004252 INVALID_STATS_CTX_ID);
4253 if (rc)
4254 goto err_out;
4255
Michael Chanb81a90d2016-01-02 23:45:01 -05004256 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004257 writel(DB_KEY_RX | rxr->rx_agg_prod,
4258 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004259 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004260 }
4261 }
4262err_out:
4263 return rc;
4264}
4265
4266static int hwrm_ring_free_send_msg(struct bnxt *bp,
4267 struct bnxt_ring_struct *ring,
4268 u32 ring_type, int cmpl_ring_id)
4269{
4270 int rc;
4271 struct hwrm_ring_free_input req = {0};
4272 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4273 u16 error_code;
4274
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004275 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004276 req.ring_type = ring_type;
4277 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4278
4279 mutex_lock(&bp->hwrm_cmd_lock);
4280 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4281 error_code = le16_to_cpu(resp->error_code);
4282 mutex_unlock(&bp->hwrm_cmd_lock);
4283
4284 if (rc || error_code) {
4285 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004286 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004287 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4288 rc);
4289 return rc;
4290 case RING_FREE_REQ_RING_TYPE_RX:
4291 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4292 rc);
4293 return rc;
4294 case RING_FREE_REQ_RING_TYPE_TX:
4295 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4296 rc);
4297 return rc;
4298 default:
4299 netdev_err(bp->dev, "Invalid ring\n");
4300 return -1;
4301 }
4302 }
4303 return 0;
4304}
4305
Michael Chanedd0c2c2015-12-27 18:19:19 -05004306static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004307{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004308 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004309
4310 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004311 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004312
Michael Chanedd0c2c2015-12-27 18:19:19 -05004313 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004314 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004315 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004316 u32 grp_idx = txr->bnapi->index;
4317 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004318
Michael Chanedd0c2c2015-12-27 18:19:19 -05004319 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4320 hwrm_ring_free_send_msg(bp, ring,
4321 RING_FREE_REQ_RING_TYPE_TX,
4322 close_path ? cmpl_ring_id :
4323 INVALID_HW_RING_ID);
4324 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004325 }
4326 }
4327
Michael Chanedd0c2c2015-12-27 18:19:19 -05004328 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004329 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004330 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004331 u32 grp_idx = rxr->bnapi->index;
4332 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004333
Michael Chanedd0c2c2015-12-27 18:19:19 -05004334 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4335 hwrm_ring_free_send_msg(bp, ring,
4336 RING_FREE_REQ_RING_TYPE_RX,
4337 close_path ? cmpl_ring_id :
4338 INVALID_HW_RING_ID);
4339 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004340 bp->grp_info[grp_idx].rx_fw_ring_id =
4341 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004342 }
4343 }
4344
Michael Chanedd0c2c2015-12-27 18:19:19 -05004345 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004346 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004347 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004348 u32 grp_idx = rxr->bnapi->index;
4349 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004350
Michael Chanedd0c2c2015-12-27 18:19:19 -05004351 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4352 hwrm_ring_free_send_msg(bp, ring,
4353 RING_FREE_REQ_RING_TYPE_RX,
4354 close_path ? cmpl_ring_id :
4355 INVALID_HW_RING_ID);
4356 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004357 bp->grp_info[grp_idx].agg_fw_ring_id =
4358 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004359 }
4360 }
4361
Michael Chan9d8bc092016-12-29 12:13:33 -05004362 /* The completion rings are about to be freed. After that the
4363 * IRQ doorbell will not work anymore. So we need to disable
4364 * IRQ here.
4365 */
4366 bnxt_disable_int_sync(bp);
4367
Michael Chanedd0c2c2015-12-27 18:19:19 -05004368 for (i = 0; i < bp->cp_nr_rings; i++) {
4369 struct bnxt_napi *bnapi = bp->bnapi[i];
4370 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4371 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004372
Michael Chanedd0c2c2015-12-27 18:19:19 -05004373 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4374 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004375 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004376 INVALID_HW_RING_ID);
4377 ring->fw_ring_id = INVALID_HW_RING_ID;
4378 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004379 }
4380 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004381}
4382
Michael Chan391be5c2016-12-29 12:13:41 -05004383/* Caller must hold bp->hwrm_cmd_lock */
4384int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4385{
4386 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4387 struct hwrm_func_qcfg_input req = {0};
4388 int rc;
4389
4390 if (bp->hwrm_spec_code < 0x10601)
4391 return 0;
4392
4393 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4394 req.fid = cpu_to_le16(fid);
4395 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4396 if (!rc)
4397 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4398
4399 return rc;
4400}
4401
Michael Chand1e79252017-02-06 16:55:38 -05004402static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004403{
4404 struct hwrm_func_cfg_input req = {0};
4405 int rc;
4406
4407 if (bp->hwrm_spec_code < 0x10601)
4408 return 0;
4409
4410 if (BNXT_VF(bp))
4411 return 0;
4412
4413 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4414 req.fid = cpu_to_le16(0xffff);
4415 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4416 req.num_tx_rings = cpu_to_le16(*tx_rings);
4417 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4418 if (rc)
4419 return rc;
4420
4421 mutex_lock(&bp->hwrm_cmd_lock);
4422 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4423 mutex_unlock(&bp->hwrm_cmd_lock);
4424 return rc;
4425}
4426
Michael Chanbb053f52016-02-26 04:00:02 -05004427static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4428 u32 buf_tmrs, u16 flags,
4429 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4430{
4431 req->flags = cpu_to_le16(flags);
4432 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4433 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4434 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4435 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4436 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4437 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4438 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4439 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4440}
4441
Michael Chanc0c050c2015-10-22 16:01:17 -04004442int bnxt_hwrm_set_coal(struct bnxt *bp)
4443{
4444 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004445 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4446 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004447 u16 max_buf, max_buf_irq;
4448 u16 buf_tmr, buf_tmr_irq;
4449 u32 flags;
4450
Michael Chandfc9c942016-02-26 04:00:03 -05004451 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4452 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4453 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4454 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004455
Michael Chandfb5b892016-02-26 04:00:01 -05004456 /* Each rx completion (2 records) should be DMAed immediately.
4457 * DMA 1/4 of the completion buffers at a time.
4458 */
4459 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004460 /* max_buf must not be zero */
4461 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004462 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4463 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4464 /* buf timer set to 1/4 of interrupt timer */
4465 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4466 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4467 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004468
4469 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4470
4471 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4472 * if coal_ticks is less than 25 us.
4473 */
Michael Chandfb5b892016-02-26 04:00:01 -05004474 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004475 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4476
Michael Chanbb053f52016-02-26 04:00:02 -05004477 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004478 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4479
4480 /* max_buf must not be zero */
4481 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4482 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4483 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4484 /* buf timer set to 1/4 of interrupt timer */
4485 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4486 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4487 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4488
4489 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4490 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4491 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004492
4493 mutex_lock(&bp->hwrm_cmd_lock);
4494 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004495 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004496
Michael Chandfc9c942016-02-26 04:00:03 -05004497 req = &req_rx;
4498 if (!bnapi->rx_ring)
4499 req = &req_tx;
4500 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4501
4502 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004503 HWRM_CMD_TIMEOUT);
4504 if (rc)
4505 break;
4506 }
4507 mutex_unlock(&bp->hwrm_cmd_lock);
4508 return rc;
4509}
4510
4511static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4512{
4513 int rc = 0, i;
4514 struct hwrm_stat_ctx_free_input req = {0};
4515
4516 if (!bp->bnapi)
4517 return 0;
4518
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004519 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4520 return 0;
4521
Michael Chanc0c050c2015-10-22 16:01:17 -04004522 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4523
4524 mutex_lock(&bp->hwrm_cmd_lock);
4525 for (i = 0; i < bp->cp_nr_rings; i++) {
4526 struct bnxt_napi *bnapi = bp->bnapi[i];
4527 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4528
4529 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4530 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4531
4532 rc = _hwrm_send_message(bp, &req, sizeof(req),
4533 HWRM_CMD_TIMEOUT);
4534 if (rc)
4535 break;
4536
4537 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4538 }
4539 }
4540 mutex_unlock(&bp->hwrm_cmd_lock);
4541 return rc;
4542}
4543
4544static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4545{
4546 int rc = 0, i;
4547 struct hwrm_stat_ctx_alloc_input req = {0};
4548 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4549
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004550 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4551 return 0;
4552
Michael Chanc0c050c2015-10-22 16:01:17 -04004553 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4554
Michael Chan51f30782016-07-01 18:46:29 -04004555 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004556
4557 mutex_lock(&bp->hwrm_cmd_lock);
4558 for (i = 0; i < bp->cp_nr_rings; i++) {
4559 struct bnxt_napi *bnapi = bp->bnapi[i];
4560 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4561
4562 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4563
4564 rc = _hwrm_send_message(bp, &req, sizeof(req),
4565 HWRM_CMD_TIMEOUT);
4566 if (rc)
4567 break;
4568
4569 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4570
4571 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4572 }
4573 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004574 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004575}
4576
Michael Chancf6645f2016-06-13 02:25:28 -04004577static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4578{
4579 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004580 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04004581 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04004582 int rc;
4583
4584 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4585 req.fid = cpu_to_le16(0xffff);
4586 mutex_lock(&bp->hwrm_cmd_lock);
4587 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4588 if (rc)
4589 goto func_qcfg_exit;
4590
4591#ifdef CONFIG_BNXT_SRIOV
4592 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004593 struct bnxt_vf_info *vf = &bp->vf;
4594
4595 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4596 }
4597#endif
Michael Chan9315edc2017-07-24 12:34:25 -04004598 flags = le16_to_cpu(resp->flags);
4599 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4600 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
4601 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4602 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
4603 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04004604 }
Michael Chan9315edc2017-07-24 12:34:25 -04004605 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
4606 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05004607
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004608 switch (resp->port_partition_type) {
4609 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4610 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4611 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4612 bp->port_partition_type = resp->port_partition_type;
4613 break;
4614 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004615 if (bp->hwrm_spec_code < 0x10707 ||
4616 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4617 bp->br_mode = BRIDGE_MODE_VEB;
4618 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4619 bp->br_mode = BRIDGE_MODE_VEPA;
4620 else
4621 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004622
4623func_qcfg_exit:
4624 mutex_unlock(&bp->hwrm_cmd_lock);
4625 return rc;
4626}
4627
Michael Chan7b08f662016-12-07 00:26:18 -05004628static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004629{
4630 int rc = 0;
4631 struct hwrm_func_qcaps_input req = {0};
4632 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4633
4634 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4635 req.fid = cpu_to_le16(0xffff);
4636
4637 mutex_lock(&bp->hwrm_cmd_lock);
4638 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4639 if (rc)
4640 goto hwrm_func_qcaps_exit;
4641
Michael Chane4060d32016-12-07 00:26:19 -05004642 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4643 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4644 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4645 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4646
Michael Chan7cc5a202016-09-19 03:58:05 -04004647 bp->tx_push_thresh = 0;
4648 if (resp->flags &
4649 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4650 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4651
Michael Chanc0c050c2015-10-22 16:01:17 -04004652 if (BNXT_PF(bp)) {
4653 struct bnxt_pf_info *pf = &bp->pf;
4654
4655 pf->fw_fid = le16_to_cpu(resp->fid);
4656 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004657 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004658 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004659 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004660 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4661 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4662 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004663 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004664 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4665 if (!pf->max_hw_ring_grps)
4666 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004667 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4668 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4669 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4670 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4671 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4672 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4673 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4674 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4675 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4676 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4677 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004678 if (resp->flags &
4679 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4680 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004681 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004682#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004683 struct bnxt_vf_info *vf = &bp->vf;
4684
4685 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004686
4687 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4688 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4689 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4690 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004691 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4692 if (!vf->max_hw_ring_grps)
4693 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004694 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4695 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4696 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004697
4698 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004699 mutex_unlock(&bp->hwrm_cmd_lock);
4700
4701 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004702 /* overwrite netdev dev_adr with admin VF MAC */
4703 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004704 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004705 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004706 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4707 }
4708 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004709#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004710 }
4711
Michael Chanc0c050c2015-10-22 16:01:17 -04004712hwrm_func_qcaps_exit:
4713 mutex_unlock(&bp->hwrm_cmd_lock);
4714 return rc;
4715}
4716
4717static int bnxt_hwrm_func_reset(struct bnxt *bp)
4718{
4719 struct hwrm_func_reset_input req = {0};
4720
4721 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4722 req.enables = 0;
4723
4724 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4725}
4726
4727static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4728{
4729 int rc = 0;
4730 struct hwrm_queue_qportcfg_input req = {0};
4731 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4732 u8 i, *qptr;
4733
4734 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4735
4736 mutex_lock(&bp->hwrm_cmd_lock);
4737 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4738 if (rc)
4739 goto qportcfg_exit;
4740
4741 if (!resp->max_configurable_queues) {
4742 rc = -EINVAL;
4743 goto qportcfg_exit;
4744 }
4745 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004746 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004747 if (bp->max_tc > BNXT_MAX_QUEUE)
4748 bp->max_tc = BNXT_MAX_QUEUE;
4749
Michael Chan441cabb2016-09-19 03:58:02 -04004750 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4751 bp->max_tc = 1;
4752
Michael Chan87c374d2016-12-02 21:17:16 -05004753 if (bp->max_lltc > bp->max_tc)
4754 bp->max_lltc = bp->max_tc;
4755
Michael Chanc0c050c2015-10-22 16:01:17 -04004756 qptr = &resp->queue_id0;
4757 for (i = 0; i < bp->max_tc; i++) {
4758 bp->q_info[i].queue_id = *qptr++;
4759 bp->q_info[i].queue_profile = *qptr++;
4760 }
4761
4762qportcfg_exit:
4763 mutex_unlock(&bp->hwrm_cmd_lock);
4764 return rc;
4765}
4766
4767static int bnxt_hwrm_ver_get(struct bnxt *bp)
4768{
4769 int rc;
4770 struct hwrm_ver_get_input req = {0};
4771 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004772 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004773
Michael Chane6ef2692016-03-28 19:46:05 -04004774 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004775 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4776 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4777 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4778 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4779 mutex_lock(&bp->hwrm_cmd_lock);
4780 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4781 if (rc)
4782 goto hwrm_ver_get_exit;
4783
4784 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4785
Michael Chan11f15ed2016-04-05 14:08:55 -04004786 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4787 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004788 if (resp->hwrm_intf_maj < 1) {
4789 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004790 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004791 resp->hwrm_intf_upd);
4792 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004793 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004794 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004795 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4796 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4797
Michael Chanff4fe812016-02-26 04:00:04 -05004798 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4799 if (!bp->hwrm_cmd_timeout)
4800 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4801
Michael Chane6ef2692016-03-28 19:46:05 -04004802 if (resp->hwrm_intf_maj >= 1)
4803 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4804
Michael Chan659c8052016-06-13 02:25:33 -04004805 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004806 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4807 !resp->chip_metal)
4808 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004809
Deepak Khungare605db82017-05-29 19:06:04 -04004810 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4811 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4812 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4813 bp->flags |= BNXT_FLAG_SHORT_CMD;
4814
Michael Chanc0c050c2015-10-22 16:01:17 -04004815hwrm_ver_get_exit:
4816 mutex_unlock(&bp->hwrm_cmd_lock);
4817 return rc;
4818}
4819
Rob Swindell5ac67d82016-09-19 03:58:03 -04004820int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4821{
Rob Swindell878786d2016-09-20 03:36:33 -04004822#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004823 struct hwrm_fw_set_time_input req = {0};
4824 struct rtc_time tm;
4825 struct timeval tv;
4826
4827 if (bp->hwrm_spec_code < 0x10400)
4828 return -EOPNOTSUPP;
4829
4830 do_gettimeofday(&tv);
4831 rtc_time_to_tm(tv.tv_sec, &tm);
4832 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4833 req.year = cpu_to_le16(1900 + tm.tm_year);
4834 req.month = 1 + tm.tm_mon;
4835 req.day = tm.tm_mday;
4836 req.hour = tm.tm_hour;
4837 req.minute = tm.tm_min;
4838 req.second = tm.tm_sec;
4839 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004840#else
4841 return -EOPNOTSUPP;
4842#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004843}
4844
Michael Chan3bdf56c2016-03-07 15:38:45 -05004845static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4846{
4847 int rc;
4848 struct bnxt_pf_info *pf = &bp->pf;
4849 struct hwrm_port_qstats_input req = {0};
4850
4851 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4852 return 0;
4853
4854 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4855 req.port_id = cpu_to_le16(pf->port_id);
4856 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4857 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4858 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4859 return rc;
4860}
4861
Michael Chanc0c050c2015-10-22 16:01:17 -04004862static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4863{
4864 if (bp->vxlan_port_cnt) {
4865 bnxt_hwrm_tunnel_dst_port_free(
4866 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4867 }
4868 bp->vxlan_port_cnt = 0;
4869 if (bp->nge_port_cnt) {
4870 bnxt_hwrm_tunnel_dst_port_free(
4871 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4872 }
4873 bp->nge_port_cnt = 0;
4874}
4875
4876static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4877{
4878 int rc, i;
4879 u32 tpa_flags = 0;
4880
4881 if (set_tpa)
4882 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4883 for (i = 0; i < bp->nr_vnics; i++) {
4884 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4885 if (rc) {
4886 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004887 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004888 return rc;
4889 }
4890 }
4891 return 0;
4892}
4893
4894static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4895{
4896 int i;
4897
4898 for (i = 0; i < bp->nr_vnics; i++)
4899 bnxt_hwrm_vnic_set_rss(bp, i, false);
4900}
4901
4902static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4903 bool irq_re_init)
4904{
4905 if (bp->vnic_info) {
4906 bnxt_hwrm_clear_vnic_filter(bp);
4907 /* clear all RSS setting before free vnic ctx */
4908 bnxt_hwrm_clear_vnic_rss(bp);
4909 bnxt_hwrm_vnic_ctx_free(bp);
4910 /* before free the vnic, undo the vnic tpa settings */
4911 if (bp->flags & BNXT_FLAG_TPA)
4912 bnxt_set_tpa(bp, false);
4913 bnxt_hwrm_vnic_free(bp);
4914 }
4915 bnxt_hwrm_ring_free(bp, close_path);
4916 bnxt_hwrm_ring_grp_free(bp);
4917 if (irq_re_init) {
4918 bnxt_hwrm_stat_ctx_free(bp);
4919 bnxt_hwrm_free_tunnel_ports(bp);
4920 }
4921}
4922
Michael Chan39d8ba22017-07-24 12:34:22 -04004923static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
4924{
4925 struct hwrm_func_cfg_input req = {0};
4926 int rc;
4927
4928 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4929 req.fid = cpu_to_le16(0xffff);
4930 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
4931 if (br_mode == BRIDGE_MODE_VEB)
4932 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
4933 else if (br_mode == BRIDGE_MODE_VEPA)
4934 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
4935 else
4936 return -EINVAL;
4937 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4938 if (rc)
4939 rc = -EIO;
4940 return rc;
4941}
4942
Michael Chanc0c050c2015-10-22 16:01:17 -04004943static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4944{
Michael Chanae10ae72016-12-29 12:13:38 -05004945 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004946 int rc;
4947
Michael Chanae10ae72016-12-29 12:13:38 -05004948 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4949 goto skip_rss_ctx;
4950
Michael Chanc0c050c2015-10-22 16:01:17 -04004951 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004952 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004953 if (rc) {
4954 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4955 vnic_id, rc);
4956 goto vnic_setup_err;
4957 }
4958 bp->rsscos_nr_ctxs++;
4959
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004960 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4961 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4962 if (rc) {
4963 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4964 vnic_id, rc);
4965 goto vnic_setup_err;
4966 }
4967 bp->rsscos_nr_ctxs++;
4968 }
4969
Michael Chanae10ae72016-12-29 12:13:38 -05004970skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004971 /* configure default vnic, ring grp */
4972 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4973 if (rc) {
4974 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4975 vnic_id, rc);
4976 goto vnic_setup_err;
4977 }
4978
4979 /* Enable RSS hashing on vnic */
4980 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4981 if (rc) {
4982 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4983 vnic_id, rc);
4984 goto vnic_setup_err;
4985 }
4986
4987 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4988 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4989 if (rc) {
4990 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4991 vnic_id, rc);
4992 }
4993 }
4994
4995vnic_setup_err:
4996 return rc;
4997}
4998
4999static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5000{
5001#ifdef CONFIG_RFS_ACCEL
5002 int i, rc = 0;
5003
5004 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005005 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005006 u16 vnic_id = i + 1;
5007 u16 ring_id = i;
5008
5009 if (vnic_id >= bp->nr_vnics)
5010 break;
5011
Michael Chanae10ae72016-12-29 12:13:38 -05005012 vnic = &bp->vnic_info[vnic_id];
5013 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5014 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5015 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005016 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005017 if (rc) {
5018 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5019 vnic_id, rc);
5020 break;
5021 }
5022 rc = bnxt_setup_vnic(bp, vnic_id);
5023 if (rc)
5024 break;
5025 }
5026 return rc;
5027#else
5028 return 0;
5029#endif
5030}
5031
Michael Chan17c71ac2016-07-01 18:46:27 -04005032/* Allow PF and VF with default VLAN to be in promiscuous mode */
5033static bool bnxt_promisc_ok(struct bnxt *bp)
5034{
5035#ifdef CONFIG_BNXT_SRIOV
5036 if (BNXT_VF(bp) && !bp->vf.vlan)
5037 return false;
5038#endif
5039 return true;
5040}
5041
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005042static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5043{
5044 unsigned int rc = 0;
5045
5046 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5047 if (rc) {
5048 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5049 rc);
5050 return rc;
5051 }
5052
5053 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5054 if (rc) {
5055 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5056 rc);
5057 return rc;
5058 }
5059 return rc;
5060}
5061
Michael Chanb664f002015-12-02 01:54:08 -05005062static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005063static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005064
Michael Chanc0c050c2015-10-22 16:01:17 -04005065static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5066{
Michael Chan7d2837d2016-05-04 16:56:44 -04005067 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005068 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005069 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005070
5071 if (irq_re_init) {
5072 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5073 if (rc) {
5074 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5075 rc);
5076 goto err_out;
5077 }
5078 }
5079
5080 rc = bnxt_hwrm_ring_alloc(bp);
5081 if (rc) {
5082 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5083 goto err_out;
5084 }
5085
5086 rc = bnxt_hwrm_ring_grp_alloc(bp);
5087 if (rc) {
5088 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5089 goto err_out;
5090 }
5091
Prashant Sreedharan76595192016-07-18 07:15:22 -04005092 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5093 rx_nr_rings--;
5094
Michael Chanc0c050c2015-10-22 16:01:17 -04005095 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005096 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005097 if (rc) {
5098 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5099 goto err_out;
5100 }
5101
5102 rc = bnxt_setup_vnic(bp, 0);
5103 if (rc)
5104 goto err_out;
5105
5106 if (bp->flags & BNXT_FLAG_RFS) {
5107 rc = bnxt_alloc_rfs_vnics(bp);
5108 if (rc)
5109 goto err_out;
5110 }
5111
5112 if (bp->flags & BNXT_FLAG_TPA) {
5113 rc = bnxt_set_tpa(bp, true);
5114 if (rc)
5115 goto err_out;
5116 }
5117
5118 if (BNXT_VF(bp))
5119 bnxt_update_vf_mac(bp);
5120
5121 /* Filter for default vnic 0 */
5122 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5123 if (rc) {
5124 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5125 goto err_out;
5126 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005127 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005128
Michael Chan7d2837d2016-05-04 16:56:44 -04005129 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005130
Michael Chan17c71ac2016-07-01 18:46:27 -04005131 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005132 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5133
5134 if (bp->dev->flags & IFF_ALLMULTI) {
5135 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5136 vnic->mc_list_count = 0;
5137 } else {
5138 u32 mask = 0;
5139
5140 bnxt_mc_list_updated(bp, &mask);
5141 vnic->rx_mask |= mask;
5142 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005143
Michael Chanb664f002015-12-02 01:54:08 -05005144 rc = bnxt_cfg_rx_mode(bp);
5145 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005146 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005147
5148 rc = bnxt_hwrm_set_coal(bp);
5149 if (rc)
5150 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005151 rc);
5152
5153 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5154 rc = bnxt_setup_nitroa0_vnic(bp);
5155 if (rc)
5156 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5157 rc);
5158 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005159
Michael Chancf6645f2016-06-13 02:25:28 -04005160 if (BNXT_VF(bp)) {
5161 bnxt_hwrm_func_qcfg(bp);
5162 netdev_update_features(bp->dev);
5163 }
5164
Michael Chanc0c050c2015-10-22 16:01:17 -04005165 return 0;
5166
5167err_out:
5168 bnxt_hwrm_resource_free(bp, 0, true);
5169
5170 return rc;
5171}
5172
5173static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5174{
5175 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5176 return 0;
5177}
5178
5179static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5180{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005181 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005182 bnxt_init_rx_rings(bp);
5183 bnxt_init_tx_rings(bp);
5184 bnxt_init_ring_grps(bp, irq_re_init);
5185 bnxt_init_vnics(bp);
5186
5187 return bnxt_init_chip(bp, irq_re_init);
5188}
5189
Michael Chanc0c050c2015-10-22 16:01:17 -04005190static int bnxt_set_real_num_queues(struct bnxt *bp)
5191{
5192 int rc;
5193 struct net_device *dev = bp->dev;
5194
Michael Chan5f449242017-02-06 16:55:40 -05005195 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5196 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005197 if (rc)
5198 return rc;
5199
5200 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5201 if (rc)
5202 return rc;
5203
5204#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005205 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005206 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005207#endif
5208
5209 return rc;
5210}
5211
Michael Chan6e6c5a52016-01-02 23:45:02 -05005212static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5213 bool shared)
5214{
5215 int _rx = *rx, _tx = *tx;
5216
5217 if (shared) {
5218 *rx = min_t(int, _rx, max);
5219 *tx = min_t(int, _tx, max);
5220 } else {
5221 if (max < 2)
5222 return -ENOMEM;
5223
5224 while (_rx + _tx > max) {
5225 if (_rx > _tx && _rx > 1)
5226 _rx--;
5227 else if (_tx > 1)
5228 _tx--;
5229 }
5230 *rx = _rx;
5231 *tx = _tx;
5232 }
5233 return 0;
5234}
5235
Michael Chan78095922016-12-07 00:26:16 -05005236static void bnxt_setup_msix(struct bnxt *bp)
5237{
5238 const int len = sizeof(bp->irq_tbl[0].name);
5239 struct net_device *dev = bp->dev;
5240 int tcs, i;
5241
5242 tcs = netdev_get_num_tc(dev);
5243 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005244 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005245
Michael Chand1e79252017-02-06 16:55:38 -05005246 for (i = 0; i < tcs; i++) {
5247 count = bp->tx_nr_rings_per_tc;
5248 off = i * count;
5249 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005250 }
5251 }
5252
5253 for (i = 0; i < bp->cp_nr_rings; i++) {
5254 char *attr;
5255
5256 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5257 attr = "TxRx";
5258 else if (i < bp->rx_nr_rings)
5259 attr = "rx";
5260 else
5261 attr = "tx";
5262
5263 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5264 i);
5265 bp->irq_tbl[i].handler = bnxt_msix;
5266 }
5267}
5268
5269static void bnxt_setup_inta(struct bnxt *bp)
5270{
5271 const int len = sizeof(bp->irq_tbl[0].name);
5272
5273 if (netdev_get_num_tc(bp->dev))
5274 netdev_reset_tc(bp->dev);
5275
5276 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5277 0);
5278 bp->irq_tbl[0].handler = bnxt_inta;
5279}
5280
5281static int bnxt_setup_int_mode(struct bnxt *bp)
5282{
5283 int rc;
5284
5285 if (bp->flags & BNXT_FLAG_USING_MSIX)
5286 bnxt_setup_msix(bp);
5287 else
5288 bnxt_setup_inta(bp);
5289
5290 rc = bnxt_set_real_num_queues(bp);
5291 return rc;
5292}
5293
Michael Chanb7429952017-01-13 01:32:00 -05005294#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005295static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5296{
5297#if defined(CONFIG_BNXT_SRIOV)
5298 if (BNXT_VF(bp))
5299 return bp->vf.max_rsscos_ctxs;
5300#endif
5301 return bp->pf.max_rsscos_ctxs;
5302}
5303
5304static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5305{
5306#if defined(CONFIG_BNXT_SRIOV)
5307 if (BNXT_VF(bp))
5308 return bp->vf.max_vnics;
5309#endif
5310 return bp->pf.max_vnics;
5311}
Michael Chanb7429952017-01-13 01:32:00 -05005312#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005313
Michael Chane4060d32016-12-07 00:26:19 -05005314unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5315{
5316#if defined(CONFIG_BNXT_SRIOV)
5317 if (BNXT_VF(bp))
5318 return bp->vf.max_stat_ctxs;
5319#endif
5320 return bp->pf.max_stat_ctxs;
5321}
5322
Michael Chana588e452016-12-07 00:26:21 -05005323void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5324{
5325#if defined(CONFIG_BNXT_SRIOV)
5326 if (BNXT_VF(bp))
5327 bp->vf.max_stat_ctxs = max;
5328 else
5329#endif
5330 bp->pf.max_stat_ctxs = max;
5331}
5332
Michael Chane4060d32016-12-07 00:26:19 -05005333unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5334{
5335#if defined(CONFIG_BNXT_SRIOV)
5336 if (BNXT_VF(bp))
5337 return bp->vf.max_cp_rings;
5338#endif
5339 return bp->pf.max_cp_rings;
5340}
5341
Michael Chana588e452016-12-07 00:26:21 -05005342void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5343{
5344#if defined(CONFIG_BNXT_SRIOV)
5345 if (BNXT_VF(bp))
5346 bp->vf.max_cp_rings = max;
5347 else
5348#endif
5349 bp->pf.max_cp_rings = max;
5350}
5351
Michael Chan78095922016-12-07 00:26:16 -05005352static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5353{
5354#if defined(CONFIG_BNXT_SRIOV)
5355 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005356 return min_t(unsigned int, bp->vf.max_irqs,
5357 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005358#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005359 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005360}
5361
Michael Chan33c26572016-12-07 00:26:15 -05005362void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5363{
5364#if defined(CONFIG_BNXT_SRIOV)
5365 if (BNXT_VF(bp))
5366 bp->vf.max_irqs = max_irqs;
5367 else
5368#endif
5369 bp->pf.max_irqs = max_irqs;
5370}
5371
Michael Chan78095922016-12-07 00:26:16 -05005372static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005373{
Michael Chan01657bc2016-01-02 23:45:03 -05005374 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005375 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005376
Michael Chan78095922016-12-07 00:26:16 -05005377 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005378 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5379 if (!msix_ent)
5380 return -ENOMEM;
5381
5382 for (i = 0; i < total_vecs; i++) {
5383 msix_ent[i].entry = i;
5384 msix_ent[i].vector = 0;
5385 }
5386
Michael Chan01657bc2016-01-02 23:45:03 -05005387 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5388 min = 2;
5389
5390 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005391 if (total_vecs < 0) {
5392 rc = -ENODEV;
5393 goto msix_setup_exit;
5394 }
5395
5396 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5397 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005398 for (i = 0; i < total_vecs; i++)
5399 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005400
Michael Chan78095922016-12-07 00:26:16 -05005401 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005402 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005403 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005404 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005405 if (rc)
5406 goto msix_setup_exit;
5407
Michael Chanc0c050c2015-10-22 16:01:17 -04005408 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005409 bp->cp_nr_rings = (min == 1) ?
5410 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5411 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005412
Michael Chanc0c050c2015-10-22 16:01:17 -04005413 } else {
5414 rc = -ENOMEM;
5415 goto msix_setup_exit;
5416 }
5417 bp->flags |= BNXT_FLAG_USING_MSIX;
5418 kfree(msix_ent);
5419 return 0;
5420
5421msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005422 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5423 kfree(bp->irq_tbl);
5424 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005425 pci_disable_msix(bp->pdev);
5426 kfree(msix_ent);
5427 return rc;
5428}
5429
Michael Chan78095922016-12-07 00:26:16 -05005430static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005431{
Michael Chanc0c050c2015-10-22 16:01:17 -04005432 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005433 if (!bp->irq_tbl)
5434 return -ENOMEM;
5435
5436 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005437 bp->rx_nr_rings = 1;
5438 bp->tx_nr_rings = 1;
5439 bp->cp_nr_rings = 1;
5440 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005441 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005442 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005443 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005444}
5445
Michael Chan78095922016-12-07 00:26:16 -05005446static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005447{
5448 int rc = 0;
5449
5450 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005451 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005452
Michael Chan1fa72e22016-04-25 02:30:49 -04005453 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005454 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005455 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005456 }
5457 return rc;
5458}
5459
Michael Chan78095922016-12-07 00:26:16 -05005460static void bnxt_clear_int_mode(struct bnxt *bp)
5461{
5462 if (bp->flags & BNXT_FLAG_USING_MSIX)
5463 pci_disable_msix(bp->pdev);
5464
5465 kfree(bp->irq_tbl);
5466 bp->irq_tbl = NULL;
5467 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5468}
5469
Michael Chanc0c050c2015-10-22 16:01:17 -04005470static void bnxt_free_irq(struct bnxt *bp)
5471{
5472 struct bnxt_irq *irq;
5473 int i;
5474
5475#ifdef CONFIG_RFS_ACCEL
5476 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5477 bp->dev->rx_cpu_rmap = NULL;
5478#endif
5479 if (!bp->irq_tbl)
5480 return;
5481
5482 for (i = 0; i < bp->cp_nr_rings; i++) {
5483 irq = &bp->irq_tbl[i];
5484 if (irq->requested)
5485 free_irq(irq->vector, bp->bnapi[i]);
5486 irq->requested = 0;
5487 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005488}
5489
5490static int bnxt_request_irq(struct bnxt *bp)
5491{
Michael Chanb81a90d2016-01-02 23:45:01 -05005492 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005493 unsigned long flags = 0;
5494#ifdef CONFIG_RFS_ACCEL
5495 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5496#endif
5497
5498 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5499 flags = IRQF_SHARED;
5500
Michael Chanb81a90d2016-01-02 23:45:01 -05005501 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005502 struct bnxt_irq *irq = &bp->irq_tbl[i];
5503#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005504 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005505 rc = irq_cpu_rmap_add(rmap, irq->vector);
5506 if (rc)
5507 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005508 j);
5509 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005510 }
5511#endif
5512 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5513 bp->bnapi[i]);
5514 if (rc)
5515 break;
5516
5517 irq->requested = 1;
5518 }
5519 return rc;
5520}
5521
5522static void bnxt_del_napi(struct bnxt *bp)
5523{
5524 int i;
5525
5526 if (!bp->bnapi)
5527 return;
5528
5529 for (i = 0; i < bp->cp_nr_rings; i++) {
5530 struct bnxt_napi *bnapi = bp->bnapi[i];
5531
5532 napi_hash_del(&bnapi->napi);
5533 netif_napi_del(&bnapi->napi);
5534 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005535 /* We called napi_hash_del() before netif_napi_del(), we need
5536 * to respect an RCU grace period before freeing napi structures.
5537 */
5538 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005539}
5540
5541static void bnxt_init_napi(struct bnxt *bp)
5542{
5543 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005544 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005545 struct bnxt_napi *bnapi;
5546
5547 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005548 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5549 cp_nr_rings--;
5550 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005551 bnapi = bp->bnapi[i];
5552 netif_napi_add(bp->dev, &bnapi->napi,
5553 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005554 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005555 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5556 bnapi = bp->bnapi[cp_nr_rings];
5557 netif_napi_add(bp->dev, &bnapi->napi,
5558 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005559 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005560 } else {
5561 bnapi = bp->bnapi[0];
5562 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005563 }
5564}
5565
5566static void bnxt_disable_napi(struct bnxt *bp)
5567{
5568 int i;
5569
5570 if (!bp->bnapi)
5571 return;
5572
Michael Chanb356a2e2016-12-29 12:13:31 -05005573 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005574 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005575}
5576
5577static void bnxt_enable_napi(struct bnxt *bp)
5578{
5579 int i;
5580
5581 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005582 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005583 napi_enable(&bp->bnapi[i]->napi);
5584 }
5585}
5586
Michael Chan7df4ae92016-12-02 21:17:17 -05005587void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005588{
5589 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005590 struct bnxt_tx_ring_info *txr;
5591 struct netdev_queue *txq;
5592
Michael Chanb6ab4b02016-01-02 23:44:59 -05005593 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005594 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005595 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005596 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005597 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005598 }
5599 }
5600 /* Stop all TX queues */
5601 netif_tx_disable(bp->dev);
5602 netif_carrier_off(bp->dev);
5603}
5604
Michael Chan7df4ae92016-12-02 21:17:17 -05005605void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005606{
5607 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005608 struct bnxt_tx_ring_info *txr;
5609 struct netdev_queue *txq;
5610
5611 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005612 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005613 txq = netdev_get_tx_queue(bp->dev, i);
5614 txr->dev_state = 0;
5615 }
5616 netif_tx_wake_all_queues(bp->dev);
5617 if (bp->link_info.link_up)
5618 netif_carrier_on(bp->dev);
5619}
5620
5621static void bnxt_report_link(struct bnxt *bp)
5622{
5623 if (bp->link_info.link_up) {
5624 const char *duplex;
5625 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005626 u32 speed;
5627 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005628
5629 netif_carrier_on(bp->dev);
5630 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5631 duplex = "full";
5632 else
5633 duplex = "half";
5634 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5635 flow_ctrl = "ON - receive & transmit";
5636 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5637 flow_ctrl = "ON - transmit";
5638 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5639 flow_ctrl = "ON - receive";
5640 else
5641 flow_ctrl = "none";
5642 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005643 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005644 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005645 if (bp->flags & BNXT_FLAG_EEE_CAP)
5646 netdev_info(bp->dev, "EEE is %s\n",
5647 bp->eee.eee_active ? "active" :
5648 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005649 fec = bp->link_info.fec_cfg;
5650 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5651 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5652 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5653 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5654 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005655 } else {
5656 netif_carrier_off(bp->dev);
5657 netdev_err(bp->dev, "NIC Link is Down\n");
5658 }
5659}
5660
Michael Chan170ce012016-04-05 14:08:57 -04005661static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5662{
5663 int rc = 0;
5664 struct hwrm_port_phy_qcaps_input req = {0};
5665 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005666 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005667
5668 if (bp->hwrm_spec_code < 0x10201)
5669 return 0;
5670
5671 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5672
5673 mutex_lock(&bp->hwrm_cmd_lock);
5674 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5675 if (rc)
5676 goto hwrm_phy_qcaps_exit;
5677
Michael Chanacb20052017-07-24 12:34:20 -04005678 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005679 struct ethtool_eee *eee = &bp->eee;
5680 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5681
5682 bp->flags |= BNXT_FLAG_EEE_CAP;
5683 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5684 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5685 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5686 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5687 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5688 }
Michael Chan520ad892017-03-08 18:44:35 -05005689 if (resp->supported_speeds_auto_mode)
5690 link_info->support_auto_speeds =
5691 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005692
5693hwrm_phy_qcaps_exit:
5694 mutex_unlock(&bp->hwrm_cmd_lock);
5695 return rc;
5696}
5697
Michael Chanc0c050c2015-10-22 16:01:17 -04005698static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5699{
5700 int rc = 0;
5701 struct bnxt_link_info *link_info = &bp->link_info;
5702 struct hwrm_port_phy_qcfg_input req = {0};
5703 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5704 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005705 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005706
5707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5708
5709 mutex_lock(&bp->hwrm_cmd_lock);
5710 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5711 if (rc) {
5712 mutex_unlock(&bp->hwrm_cmd_lock);
5713 return rc;
5714 }
5715
5716 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5717 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005718 link_info->duplex = resp->duplex_cfg;
5719 if (bp->hwrm_spec_code >= 0x10800)
5720 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005721 link_info->pause = resp->pause;
5722 link_info->auto_mode = resp->auto_mode;
5723 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005724 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005725 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005726 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005727 if (link_info->phy_link_status == BNXT_LINK_LINK)
5728 link_info->link_speed = le16_to_cpu(resp->link_speed);
5729 else
5730 link_info->link_speed = 0;
5731 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005732 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5733 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005734 link_info->lp_auto_link_speeds =
5735 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005736 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5737 link_info->phy_ver[0] = resp->phy_maj;
5738 link_info->phy_ver[1] = resp->phy_min;
5739 link_info->phy_ver[2] = resp->phy_bld;
5740 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005741 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005742 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005743 link_info->phy_addr = resp->eee_config_phy_addr &
5744 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005745 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005746
Michael Chan170ce012016-04-05 14:08:57 -04005747 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5748 struct ethtool_eee *eee = &bp->eee;
5749 u16 fw_speeds;
5750
5751 eee->eee_active = 0;
5752 if (resp->eee_config_phy_addr &
5753 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5754 eee->eee_active = 1;
5755 fw_speeds = le16_to_cpu(
5756 resp->link_partner_adv_eee_link_speed_mask);
5757 eee->lp_advertised =
5758 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5759 }
5760
5761 /* Pull initial EEE config */
5762 if (!chng_link_state) {
5763 if (resp->eee_config_phy_addr &
5764 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5765 eee->eee_enabled = 1;
5766
5767 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5768 eee->advertised =
5769 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5770
5771 if (resp->eee_config_phy_addr &
5772 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5773 __le32 tmr;
5774
5775 eee->tx_lpi_enabled = 1;
5776 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5777 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5778 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5779 }
5780 }
5781 }
Michael Chane70c7522017-02-12 19:18:16 -05005782
5783 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5784 if (bp->hwrm_spec_code >= 0x10504)
5785 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5786
Michael Chanc0c050c2015-10-22 16:01:17 -04005787 /* TODO: need to add more logic to report VF link */
5788 if (chng_link_state) {
5789 if (link_info->phy_link_status == BNXT_LINK_LINK)
5790 link_info->link_up = 1;
5791 else
5792 link_info->link_up = 0;
5793 if (link_up != link_info->link_up)
5794 bnxt_report_link(bp);
5795 } else {
5796 /* alwasy link down if not require to update link state */
5797 link_info->link_up = 0;
5798 }
5799 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005800
5801 diff = link_info->support_auto_speeds ^ link_info->advertising;
5802 if ((link_info->support_auto_speeds | diff) !=
5803 link_info->support_auto_speeds) {
5804 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005805 * update the advertisement settings. Caller holds RTNL
5806 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005807 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005808 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005809 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005810 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005811 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005812 return 0;
5813}
5814
Michael Chan10289be2016-05-15 03:04:49 -04005815static void bnxt_get_port_module_status(struct bnxt *bp)
5816{
5817 struct bnxt_link_info *link_info = &bp->link_info;
5818 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5819 u8 module_status;
5820
5821 if (bnxt_update_link(bp, true))
5822 return;
5823
5824 module_status = link_info->module_status;
5825 switch (module_status) {
5826 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5827 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5828 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5829 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5830 bp->pf.port_id);
5831 if (bp->hwrm_spec_code >= 0x10201) {
5832 netdev_warn(bp->dev, "Module part number %s\n",
5833 resp->phy_vendor_partnumber);
5834 }
5835 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5836 netdev_warn(bp->dev, "TX is disabled\n");
5837 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5838 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5839 }
5840}
5841
Michael Chanc0c050c2015-10-22 16:01:17 -04005842static void
5843bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5844{
5845 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005846 if (bp->hwrm_spec_code >= 0x10201)
5847 req->auto_pause =
5848 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005849 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5850 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5851 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005852 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005853 req->enables |=
5854 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5855 } else {
5856 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5857 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5858 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5859 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5860 req->enables |=
5861 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005862 if (bp->hwrm_spec_code >= 0x10201) {
5863 req->auto_pause = req->force_pause;
5864 req->enables |= cpu_to_le32(
5865 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5866 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005867 }
5868}
5869
5870static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5871 struct hwrm_port_phy_cfg_input *req)
5872{
5873 u8 autoneg = bp->link_info.autoneg;
5874 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005875 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005876
5877 if (autoneg & BNXT_AUTONEG_SPEED) {
5878 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005879 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005880
5881 req->enables |= cpu_to_le32(
5882 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5883 req->auto_link_speed_mask = cpu_to_le16(advertising);
5884
5885 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5886 req->flags |=
5887 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5888 } else {
5889 req->force_link_speed = cpu_to_le16(fw_link_speed);
5890 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5891 }
5892
Michael Chanc0c050c2015-10-22 16:01:17 -04005893 /* tell chimp that the setting takes effect immediately */
5894 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5895}
5896
5897int bnxt_hwrm_set_pause(struct bnxt *bp)
5898{
5899 struct hwrm_port_phy_cfg_input req = {0};
5900 int rc;
5901
5902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5903 bnxt_hwrm_set_pause_common(bp, &req);
5904
5905 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5906 bp->link_info.force_link_chng)
5907 bnxt_hwrm_set_link_common(bp, &req);
5908
5909 mutex_lock(&bp->hwrm_cmd_lock);
5910 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5911 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5912 /* since changing of pause setting doesn't trigger any link
5913 * change event, the driver needs to update the current pause
5914 * result upon successfully return of the phy_cfg command
5915 */
5916 bp->link_info.pause =
5917 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5918 bp->link_info.auto_pause_setting = 0;
5919 if (!bp->link_info.force_link_chng)
5920 bnxt_report_link(bp);
5921 }
5922 bp->link_info.force_link_chng = false;
5923 mutex_unlock(&bp->hwrm_cmd_lock);
5924 return rc;
5925}
5926
Michael Chan939f7f02016-04-05 14:08:58 -04005927static void bnxt_hwrm_set_eee(struct bnxt *bp,
5928 struct hwrm_port_phy_cfg_input *req)
5929{
5930 struct ethtool_eee *eee = &bp->eee;
5931
5932 if (eee->eee_enabled) {
5933 u16 eee_speeds;
5934 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5935
5936 if (eee->tx_lpi_enabled)
5937 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5938 else
5939 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5940
5941 req->flags |= cpu_to_le32(flags);
5942 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5943 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5944 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5945 } else {
5946 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5947 }
5948}
5949
5950int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005951{
5952 struct hwrm_port_phy_cfg_input req = {0};
5953
5954 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5955 if (set_pause)
5956 bnxt_hwrm_set_pause_common(bp, &req);
5957
5958 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005959
5960 if (set_eee)
5961 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005962 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5963}
5964
Michael Chan33f7d552016-04-11 04:11:12 -04005965static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5966{
5967 struct hwrm_port_phy_cfg_input req = {0};
5968
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005969 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005970 return 0;
5971
5972 if (pci_num_vf(bp->pdev))
5973 return 0;
5974
5975 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005976 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005977 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5978}
5979
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005980static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5981{
5982 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5983 struct hwrm_port_led_qcaps_input req = {0};
5984 struct bnxt_pf_info *pf = &bp->pf;
5985 int rc;
5986
5987 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5988 return 0;
5989
5990 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5991 req.port_id = cpu_to_le16(pf->port_id);
5992 mutex_lock(&bp->hwrm_cmd_lock);
5993 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5994 if (rc) {
5995 mutex_unlock(&bp->hwrm_cmd_lock);
5996 return rc;
5997 }
5998 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5999 int i;
6000
6001 bp->num_leds = resp->num_leds;
6002 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6003 bp->num_leds);
6004 for (i = 0; i < bp->num_leds; i++) {
6005 struct bnxt_led_info *led = &bp->leds[i];
6006 __le16 caps = led->led_state_caps;
6007
6008 if (!led->led_group_id ||
6009 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6010 bp->num_leds = 0;
6011 break;
6012 }
6013 }
6014 }
6015 mutex_unlock(&bp->hwrm_cmd_lock);
6016 return 0;
6017}
6018
Michael Chan5282db62017-04-04 18:14:10 -04006019int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6020{
6021 struct hwrm_wol_filter_alloc_input req = {0};
6022 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6023 int rc;
6024
6025 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6026 req.port_id = cpu_to_le16(bp->pf.port_id);
6027 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6028 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6029 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6030 mutex_lock(&bp->hwrm_cmd_lock);
6031 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6032 if (!rc)
6033 bp->wol_filter_id = resp->wol_filter_id;
6034 mutex_unlock(&bp->hwrm_cmd_lock);
6035 return rc;
6036}
6037
6038int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6039{
6040 struct hwrm_wol_filter_free_input req = {0};
6041 int rc;
6042
6043 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6044 req.port_id = cpu_to_le16(bp->pf.port_id);
6045 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6046 req.wol_filter_id = bp->wol_filter_id;
6047 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6048 return rc;
6049}
6050
Michael Chanc1ef1462017-04-04 18:14:07 -04006051static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6052{
6053 struct hwrm_wol_filter_qcfg_input req = {0};
6054 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6055 u16 next_handle = 0;
6056 int rc;
6057
6058 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6059 req.port_id = cpu_to_le16(bp->pf.port_id);
6060 req.handle = cpu_to_le16(handle);
6061 mutex_lock(&bp->hwrm_cmd_lock);
6062 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6063 if (!rc) {
6064 next_handle = le16_to_cpu(resp->next_handle);
6065 if (next_handle != 0) {
6066 if (resp->wol_type ==
6067 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6068 bp->wol = 1;
6069 bp->wol_filter_id = resp->wol_filter_id;
6070 }
6071 }
6072 }
6073 mutex_unlock(&bp->hwrm_cmd_lock);
6074 return next_handle;
6075}
6076
6077static void bnxt_get_wol_settings(struct bnxt *bp)
6078{
6079 u16 handle = 0;
6080
6081 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6082 return;
6083
6084 do {
6085 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6086 } while (handle && handle != 0xffff);
6087}
6088
Michael Chan939f7f02016-04-05 14:08:58 -04006089static bool bnxt_eee_config_ok(struct bnxt *bp)
6090{
6091 struct ethtool_eee *eee = &bp->eee;
6092 struct bnxt_link_info *link_info = &bp->link_info;
6093
6094 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6095 return true;
6096
6097 if (eee->eee_enabled) {
6098 u32 advertising =
6099 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6100
6101 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6102 eee->eee_enabled = 0;
6103 return false;
6104 }
6105 if (eee->advertised & ~advertising) {
6106 eee->advertised = advertising & eee->supported;
6107 return false;
6108 }
6109 }
6110 return true;
6111}
6112
Michael Chanc0c050c2015-10-22 16:01:17 -04006113static int bnxt_update_phy_setting(struct bnxt *bp)
6114{
6115 int rc;
6116 bool update_link = false;
6117 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006118 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006119 struct bnxt_link_info *link_info = &bp->link_info;
6120
6121 rc = bnxt_update_link(bp, true);
6122 if (rc) {
6123 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6124 rc);
6125 return rc;
6126 }
Michael Chan33dac242017-02-12 19:18:15 -05006127 if (!BNXT_SINGLE_PF(bp))
6128 return 0;
6129
Michael Chanc0c050c2015-10-22 16:01:17 -04006130 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006131 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6132 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006133 update_pause = true;
6134 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6135 link_info->force_pause_setting != link_info->req_flow_ctrl)
6136 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006137 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6138 if (BNXT_AUTO_MODE(link_info->auto_mode))
6139 update_link = true;
6140 if (link_info->req_link_speed != link_info->force_link_speed)
6141 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006142 if (link_info->req_duplex != link_info->duplex_setting)
6143 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006144 } else {
6145 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6146 update_link = true;
6147 if (link_info->advertising != link_info->auto_link_speeds)
6148 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006149 }
6150
Michael Chan16d663a2016-11-16 21:13:07 -05006151 /* The last close may have shutdown the link, so need to call
6152 * PHY_CFG to bring it back up.
6153 */
6154 if (!netif_carrier_ok(bp->dev))
6155 update_link = true;
6156
Michael Chan939f7f02016-04-05 14:08:58 -04006157 if (!bnxt_eee_config_ok(bp))
6158 update_eee = true;
6159
Michael Chanc0c050c2015-10-22 16:01:17 -04006160 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006161 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006162 else if (update_pause)
6163 rc = bnxt_hwrm_set_pause(bp);
6164 if (rc) {
6165 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6166 rc);
6167 return rc;
6168 }
6169
6170 return rc;
6171}
6172
Jeffrey Huang11809492015-11-05 16:25:49 -05006173/* Common routine to pre-map certain register block to different GRC window.
6174 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6175 * in PF and 3 windows in VF that can be customized to map in different
6176 * register blocks.
6177 */
6178static void bnxt_preset_reg_win(struct bnxt *bp)
6179{
6180 if (BNXT_PF(bp)) {
6181 /* CAG registers map to GRC window #4 */
6182 writel(BNXT_CAG_REG_BASE,
6183 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6184 }
6185}
6186
Michael Chanc0c050c2015-10-22 16:01:17 -04006187static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6188{
6189 int rc = 0;
6190
Jeffrey Huang11809492015-11-05 16:25:49 -05006191 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006192 netif_carrier_off(bp->dev);
6193 if (irq_re_init) {
6194 rc = bnxt_setup_int_mode(bp);
6195 if (rc) {
6196 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6197 rc);
6198 return rc;
6199 }
6200 }
6201 if ((bp->flags & BNXT_FLAG_RFS) &&
6202 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6203 /* disable RFS if falling back to INTA */
6204 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6205 bp->flags &= ~BNXT_FLAG_RFS;
6206 }
6207
6208 rc = bnxt_alloc_mem(bp, irq_re_init);
6209 if (rc) {
6210 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6211 goto open_err_free_mem;
6212 }
6213
6214 if (irq_re_init) {
6215 bnxt_init_napi(bp);
6216 rc = bnxt_request_irq(bp);
6217 if (rc) {
6218 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6219 goto open_err;
6220 }
6221 }
6222
6223 bnxt_enable_napi(bp);
6224
6225 rc = bnxt_init_nic(bp, irq_re_init);
6226 if (rc) {
6227 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6228 goto open_err;
6229 }
6230
6231 if (link_re_init) {
6232 rc = bnxt_update_phy_setting(bp);
6233 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006234 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006235 }
6236
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006237 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006238 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006239
Michael Chancaefe522015-12-09 19:35:42 -05006240 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006241 bnxt_enable_int(bp);
6242 /* Enable TX queues */
6243 bnxt_tx_enable(bp);
6244 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006245 /* Poll link status and check for SFP+ module status */
6246 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006247
6248 return 0;
6249
6250open_err:
6251 bnxt_disable_napi(bp);
6252 bnxt_del_napi(bp);
6253
6254open_err_free_mem:
6255 bnxt_free_skbs(bp);
6256 bnxt_free_irq(bp);
6257 bnxt_free_mem(bp, true);
6258 return rc;
6259}
6260
6261/* rtnl_lock held */
6262int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6263{
6264 int rc = 0;
6265
6266 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6267 if (rc) {
6268 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6269 dev_close(bp->dev);
6270 }
6271 return rc;
6272}
6273
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006274/* rtnl_lock held, open the NIC half way by allocating all resources, but
6275 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6276 * self tests.
6277 */
6278int bnxt_half_open_nic(struct bnxt *bp)
6279{
6280 int rc = 0;
6281
6282 rc = bnxt_alloc_mem(bp, false);
6283 if (rc) {
6284 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6285 goto half_open_err;
6286 }
6287 rc = bnxt_init_nic(bp, false);
6288 if (rc) {
6289 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6290 goto half_open_err;
6291 }
6292 return 0;
6293
6294half_open_err:
6295 bnxt_free_skbs(bp);
6296 bnxt_free_mem(bp, false);
6297 dev_close(bp->dev);
6298 return rc;
6299}
6300
6301/* rtnl_lock held, this call can only be made after a previous successful
6302 * call to bnxt_half_open_nic().
6303 */
6304void bnxt_half_close_nic(struct bnxt *bp)
6305{
6306 bnxt_hwrm_resource_free(bp, false, false);
6307 bnxt_free_skbs(bp);
6308 bnxt_free_mem(bp, false);
6309}
6310
Michael Chanc0c050c2015-10-22 16:01:17 -04006311static int bnxt_open(struct net_device *dev)
6312{
6313 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006314
Michael Chanc0c050c2015-10-22 16:01:17 -04006315 return __bnxt_open_nic(bp, true, true);
6316}
6317
Michael Chanf9b76eb2017-07-11 13:05:34 -04006318static bool bnxt_drv_busy(struct bnxt *bp)
6319{
6320 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6321 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6322}
6323
Michael Chanc0c050c2015-10-22 16:01:17 -04006324int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6325{
6326 int rc = 0;
6327
6328#ifdef CONFIG_BNXT_SRIOV
6329 if (bp->sriov_cfg) {
6330 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6331 !bp->sriov_cfg,
6332 BNXT_SRIOV_CFG_WAIT_TMO);
6333 if (rc)
6334 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6335 }
6336#endif
6337 /* Change device state to avoid TX queue wake up's */
6338 bnxt_tx_disable(bp);
6339
Michael Chancaefe522015-12-09 19:35:42 -05006340 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006341 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006342 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006343 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006344
Michael Chan9d8bc092016-12-29 12:13:33 -05006345 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006346 bnxt_shutdown_nic(bp, irq_re_init);
6347
6348 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6349
6350 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006351 del_timer_sync(&bp->timer);
6352 bnxt_free_skbs(bp);
6353
6354 if (irq_re_init) {
6355 bnxt_free_irq(bp);
6356 bnxt_del_napi(bp);
6357 }
6358 bnxt_free_mem(bp, irq_re_init);
6359 return rc;
6360}
6361
6362static int bnxt_close(struct net_device *dev)
6363{
6364 struct bnxt *bp = netdev_priv(dev);
6365
6366 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006367 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006368 return 0;
6369}
6370
6371/* rtnl_lock held */
6372static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6373{
6374 switch (cmd) {
6375 case SIOCGMIIPHY:
6376 /* fallthru */
6377 case SIOCGMIIREG: {
6378 if (!netif_running(dev))
6379 return -EAGAIN;
6380
6381 return 0;
6382 }
6383
6384 case SIOCSMIIREG:
6385 if (!netif_running(dev))
6386 return -EAGAIN;
6387
6388 return 0;
6389
6390 default:
6391 /* do nothing */
6392 break;
6393 }
6394 return -EOPNOTSUPP;
6395}
6396
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006397static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006398bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6399{
6400 u32 i;
6401 struct bnxt *bp = netdev_priv(dev);
6402
Michael Chanf9b76eb2017-07-11 13:05:34 -04006403 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6404 /* Make sure bnxt_close_nic() sees that we are reading stats before
6405 * we check the BNXT_STATE_OPEN flag.
6406 */
6407 smp_mb__after_atomic();
6408 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6409 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006410 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006411 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006412
6413 /* TODO check if we need to synchronize with bnxt_close path */
6414 for (i = 0; i < bp->cp_nr_rings; i++) {
6415 struct bnxt_napi *bnapi = bp->bnapi[i];
6416 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6417 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6418
6419 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6420 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6421 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6422
6423 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6424 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6425 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6426
6427 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6428 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6429 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6430
6431 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6432 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6433 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6434
6435 stats->rx_missed_errors +=
6436 le64_to_cpu(hw_stats->rx_discard_pkts);
6437
6438 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6439
Michael Chanc0c050c2015-10-22 16:01:17 -04006440 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6441 }
6442
Michael Chan9947f832016-03-07 15:38:46 -05006443 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6444 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6445 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6446
6447 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6448 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6449 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6450 le64_to_cpu(rx->rx_ovrsz_frames) +
6451 le64_to_cpu(rx->rx_runt_frames);
6452 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6453 le64_to_cpu(rx->rx_jbr_frames);
6454 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6455 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6456 stats->tx_errors = le64_to_cpu(tx->tx_err);
6457 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006458 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006459}
6460
6461static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6462{
6463 struct net_device *dev = bp->dev;
6464 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6465 struct netdev_hw_addr *ha;
6466 u8 *haddr;
6467 int mc_count = 0;
6468 bool update = false;
6469 int off = 0;
6470
6471 netdev_for_each_mc_addr(ha, dev) {
6472 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6473 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6474 vnic->mc_list_count = 0;
6475 return false;
6476 }
6477 haddr = ha->addr;
6478 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6479 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6480 update = true;
6481 }
6482 off += ETH_ALEN;
6483 mc_count++;
6484 }
6485 if (mc_count)
6486 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6487
6488 if (mc_count != vnic->mc_list_count) {
6489 vnic->mc_list_count = mc_count;
6490 update = true;
6491 }
6492 return update;
6493}
6494
6495static bool bnxt_uc_list_updated(struct bnxt *bp)
6496{
6497 struct net_device *dev = bp->dev;
6498 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6499 struct netdev_hw_addr *ha;
6500 int off = 0;
6501
6502 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6503 return true;
6504
6505 netdev_for_each_uc_addr(ha, dev) {
6506 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6507 return true;
6508
6509 off += ETH_ALEN;
6510 }
6511 return false;
6512}
6513
6514static void bnxt_set_rx_mode(struct net_device *dev)
6515{
6516 struct bnxt *bp = netdev_priv(dev);
6517 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6518 u32 mask = vnic->rx_mask;
6519 bool mc_update = false;
6520 bool uc_update;
6521
6522 if (!netif_running(dev))
6523 return;
6524
6525 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6526 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6527 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6528
Michael Chan17c71ac2016-07-01 18:46:27 -04006529 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006530 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6531
6532 uc_update = bnxt_uc_list_updated(bp);
6533
6534 if (dev->flags & IFF_ALLMULTI) {
6535 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6536 vnic->mc_list_count = 0;
6537 } else {
6538 mc_update = bnxt_mc_list_updated(bp, &mask);
6539 }
6540
6541 if (mask != vnic->rx_mask || uc_update || mc_update) {
6542 vnic->rx_mask = mask;
6543
6544 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6545 schedule_work(&bp->sp_task);
6546 }
6547}
6548
Michael Chanb664f002015-12-02 01:54:08 -05006549static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006550{
6551 struct net_device *dev = bp->dev;
6552 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6553 struct netdev_hw_addr *ha;
6554 int i, off = 0, rc;
6555 bool uc_update;
6556
6557 netif_addr_lock_bh(dev);
6558 uc_update = bnxt_uc_list_updated(bp);
6559 netif_addr_unlock_bh(dev);
6560
6561 if (!uc_update)
6562 goto skip_uc;
6563
6564 mutex_lock(&bp->hwrm_cmd_lock);
6565 for (i = 1; i < vnic->uc_filter_count; i++) {
6566 struct hwrm_cfa_l2_filter_free_input req = {0};
6567
6568 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6569 -1);
6570
6571 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6572
6573 rc = _hwrm_send_message(bp, &req, sizeof(req),
6574 HWRM_CMD_TIMEOUT);
6575 }
6576 mutex_unlock(&bp->hwrm_cmd_lock);
6577
6578 vnic->uc_filter_count = 1;
6579
6580 netif_addr_lock_bh(dev);
6581 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6582 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6583 } else {
6584 netdev_for_each_uc_addr(ha, dev) {
6585 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6586 off += ETH_ALEN;
6587 vnic->uc_filter_count++;
6588 }
6589 }
6590 netif_addr_unlock_bh(dev);
6591
6592 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6593 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6594 if (rc) {
6595 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6596 rc);
6597 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006598 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006599 }
6600 }
6601
6602skip_uc:
6603 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6604 if (rc)
6605 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6606 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006607
6608 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006609}
6610
Michael Chan8079e8f2016-12-29 12:13:37 -05006611/* If the chip and firmware supports RFS */
6612static bool bnxt_rfs_supported(struct bnxt *bp)
6613{
6614 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6615 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006616 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6617 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006618 return false;
6619}
6620
6621/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006622static bool bnxt_rfs_capable(struct bnxt *bp)
6623{
6624#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006625 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006626
Michael Chan964fd482017-02-12 19:18:13 -05006627 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006628 return false;
6629
6630 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006631 max_vnics = bnxt_get_max_func_vnics(bp);
6632 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006633
6634 /* RSS contexts not a limiting factor */
6635 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6636 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006637 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006638 netdev_warn(bp->dev,
6639 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006640 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006641 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006642 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006643
6644 return true;
6645#else
6646 return false;
6647#endif
6648}
6649
Michael Chanc0c050c2015-10-22 16:01:17 -04006650static netdev_features_t bnxt_fix_features(struct net_device *dev,
6651 netdev_features_t features)
6652{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006653 struct bnxt *bp = netdev_priv(dev);
6654
Vasundhara Volama2304902016-07-25 12:33:36 -04006655 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006656 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006657
6658 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6659 * turned on or off together.
6660 */
6661 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6662 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6663 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6664 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6665 NETIF_F_HW_VLAN_STAG_RX);
6666 else
6667 features |= NETIF_F_HW_VLAN_CTAG_RX |
6668 NETIF_F_HW_VLAN_STAG_RX;
6669 }
Michael Chancf6645f2016-06-13 02:25:28 -04006670#ifdef CONFIG_BNXT_SRIOV
6671 if (BNXT_VF(bp)) {
6672 if (bp->vf.vlan) {
6673 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6674 NETIF_F_HW_VLAN_STAG_RX);
6675 }
6676 }
6677#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006678 return features;
6679}
6680
6681static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6682{
6683 struct bnxt *bp = netdev_priv(dev);
6684 u32 flags = bp->flags;
6685 u32 changes;
6686 int rc = 0;
6687 bool re_init = false;
6688 bool update_tpa = false;
6689
6690 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006691 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006692 flags |= BNXT_FLAG_GRO;
6693 if (features & NETIF_F_LRO)
6694 flags |= BNXT_FLAG_LRO;
6695
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006696 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6697 flags &= ~BNXT_FLAG_TPA;
6698
Michael Chanc0c050c2015-10-22 16:01:17 -04006699 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6700 flags |= BNXT_FLAG_STRIP_VLAN;
6701
6702 if (features & NETIF_F_NTUPLE)
6703 flags |= BNXT_FLAG_RFS;
6704
6705 changes = flags ^ bp->flags;
6706 if (changes & BNXT_FLAG_TPA) {
6707 update_tpa = true;
6708 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6709 (flags & BNXT_FLAG_TPA) == 0)
6710 re_init = true;
6711 }
6712
6713 if (changes & ~BNXT_FLAG_TPA)
6714 re_init = true;
6715
6716 if (flags != bp->flags) {
6717 u32 old_flags = bp->flags;
6718
6719 bp->flags = flags;
6720
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006721 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006722 if (update_tpa)
6723 bnxt_set_ring_params(bp);
6724 return rc;
6725 }
6726
6727 if (re_init) {
6728 bnxt_close_nic(bp, false, false);
6729 if (update_tpa)
6730 bnxt_set_ring_params(bp);
6731
6732 return bnxt_open_nic(bp, false, false);
6733 }
6734 if (update_tpa) {
6735 rc = bnxt_set_tpa(bp,
6736 (flags & BNXT_FLAG_TPA) ?
6737 true : false);
6738 if (rc)
6739 bp->flags = old_flags;
6740 }
6741 }
6742 return rc;
6743}
6744
Michael Chan9f554592016-01-02 23:44:58 -05006745static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6746{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006747 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006748 int i = bnapi->index;
6749
Michael Chan3b2b7d92016-01-02 23:45:00 -05006750 if (!txr)
6751 return;
6752
Michael Chan9f554592016-01-02 23:44:58 -05006753 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6754 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6755 txr->tx_cons);
6756}
6757
6758static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6759{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006760 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006761 int i = bnapi->index;
6762
Michael Chan3b2b7d92016-01-02 23:45:00 -05006763 if (!rxr)
6764 return;
6765
Michael Chan9f554592016-01-02 23:44:58 -05006766 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",
6767 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6768 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6769 rxr->rx_sw_agg_prod);
6770}
6771
6772static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6773{
6774 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6775 int i = bnapi->index;
6776
6777 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6778 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6779}
6780
Michael Chanc0c050c2015-10-22 16:01:17 -04006781static void bnxt_dbg_dump_states(struct bnxt *bp)
6782{
6783 int i;
6784 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006785
6786 for (i = 0; i < bp->cp_nr_rings; i++) {
6787 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006788 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006789 bnxt_dump_tx_sw_state(bnapi);
6790 bnxt_dump_rx_sw_state(bnapi);
6791 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006792 }
6793 }
6794}
6795
Michael Chan6988bd92016-06-13 02:25:29 -04006796static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006797{
Michael Chan6988bd92016-06-13 02:25:29 -04006798 if (!silent)
6799 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006800 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006801 int rc;
6802
6803 if (!silent)
6804 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006805 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006806 rc = bnxt_open_nic(bp, false, false);
6807 if (!silent && !rc)
6808 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006809 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006810}
6811
6812static void bnxt_tx_timeout(struct net_device *dev)
6813{
6814 struct bnxt *bp = netdev_priv(dev);
6815
6816 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6817 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6818 schedule_work(&bp->sp_task);
6819}
6820
6821#ifdef CONFIG_NET_POLL_CONTROLLER
6822static void bnxt_poll_controller(struct net_device *dev)
6823{
6824 struct bnxt *bp = netdev_priv(dev);
6825 int i;
6826
Michael Chan2270bc52017-06-23 14:01:01 -04006827 /* Only process tx rings/combined rings in netpoll mode. */
6828 for (i = 0; i < bp->tx_nr_rings; i++) {
6829 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006830
Michael Chan2270bc52017-06-23 14:01:01 -04006831 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006832 }
6833}
6834#endif
6835
6836static void bnxt_timer(unsigned long data)
6837{
6838 struct bnxt *bp = (struct bnxt *)data;
6839 struct net_device *dev = bp->dev;
6840
6841 if (!netif_running(dev))
6842 return;
6843
6844 if (atomic_read(&bp->intr_sem) != 0)
6845 goto bnxt_restart_timer;
6846
Michael Chanadcc3312017-07-24 12:34:24 -04006847 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
6848 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05006849 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6850 schedule_work(&bp->sp_task);
6851 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006852bnxt_restart_timer:
6853 mod_timer(&bp->timer, jiffies + bp->current_interval);
6854}
6855
Michael Chana551ee92017-01-25 02:55:07 -05006856static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006857{
Michael Chana551ee92017-01-25 02:55:07 -05006858 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6859 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006860 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6861 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6862 */
6863 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6864 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006865}
6866
6867static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6868{
Michael Chan6988bd92016-06-13 02:25:29 -04006869 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6870 rtnl_unlock();
6871}
6872
Michael Chana551ee92017-01-25 02:55:07 -05006873/* Only called from bnxt_sp_task() */
6874static void bnxt_reset(struct bnxt *bp, bool silent)
6875{
6876 bnxt_rtnl_lock_sp(bp);
6877 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6878 bnxt_reset_task(bp, silent);
6879 bnxt_rtnl_unlock_sp(bp);
6880}
6881
Michael Chanc0c050c2015-10-22 16:01:17 -04006882static void bnxt_cfg_ntp_filters(struct bnxt *);
6883
6884static void bnxt_sp_task(struct work_struct *work)
6885{
6886 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006887
Michael Chan4cebdce2015-12-09 19:35:43 -05006888 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6889 smp_mb__after_atomic();
6890 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6891 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006892 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006893 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006894
6895 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6896 bnxt_cfg_rx_mode(bp);
6897
6898 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6899 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006900 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6901 bnxt_hwrm_exec_fwd_req(bp);
6902 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6903 bnxt_hwrm_tunnel_dst_port_alloc(
6904 bp, bp->vxlan_port,
6905 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6906 }
6907 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6908 bnxt_hwrm_tunnel_dst_port_free(
6909 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6910 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006911 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6912 bnxt_hwrm_tunnel_dst_port_alloc(
6913 bp, bp->nge_port,
6914 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6915 }
6916 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6917 bnxt_hwrm_tunnel_dst_port_free(
6918 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6919 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006920 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6921 bnxt_hwrm_port_qstats(bp);
6922
Michael Chana551ee92017-01-25 02:55:07 -05006923 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6924 * must be the last functions to be called before exiting.
6925 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006926 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6927 int rc = 0;
6928
6929 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6930 &bp->sp_event))
6931 bnxt_hwrm_phy_qcaps(bp);
6932
6933 bnxt_rtnl_lock_sp(bp);
6934 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6935 rc = bnxt_update_link(bp, true);
6936 bnxt_rtnl_unlock_sp(bp);
6937 if (rc)
6938 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6939 rc);
6940 }
Michael Chan90c694b2017-01-25 02:55:09 -05006941 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6942 bnxt_rtnl_lock_sp(bp);
6943 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6944 bnxt_get_port_module_status(bp);
6945 bnxt_rtnl_unlock_sp(bp);
6946 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006947 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6948 bnxt_reset(bp, false);
6949
6950 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6951 bnxt_reset(bp, true);
6952
Michael Chanc0c050c2015-10-22 16:01:17 -04006953 smp_mb__before_atomic();
6954 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6955}
6956
Michael Chand1e79252017-02-06 16:55:38 -05006957/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04006958int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
6959 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006960{
6961 int max_rx, max_tx, tx_sets = 1;
6962 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05006963 int rc;
6964
Michael Chand1e79252017-02-06 16:55:38 -05006965 if (tcs)
6966 tx_sets = tcs;
6967
6968 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6969 if (rc)
6970 return rc;
6971
6972 if (max_rx < rx)
6973 return -ENOMEM;
6974
Michael Chan5f449242017-02-06 16:55:40 -05006975 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006976 if (max_tx < tx_rings_needed)
6977 return -ENOMEM;
6978
6979 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006980 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006981 return -ENOMEM;
6982 return 0;
6983}
6984
Sathya Perla17086392017-02-20 19:25:18 -05006985static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6986{
6987 if (bp->bar2) {
6988 pci_iounmap(pdev, bp->bar2);
6989 bp->bar2 = NULL;
6990 }
6991
6992 if (bp->bar1) {
6993 pci_iounmap(pdev, bp->bar1);
6994 bp->bar1 = NULL;
6995 }
6996
6997 if (bp->bar0) {
6998 pci_iounmap(pdev, bp->bar0);
6999 bp->bar0 = NULL;
7000 }
7001}
7002
7003static void bnxt_cleanup_pci(struct bnxt *bp)
7004{
7005 bnxt_unmap_bars(bp, bp->pdev);
7006 pci_release_regions(bp->pdev);
7007 pci_disable_device(bp->pdev);
7008}
7009
Michael Chanc0c050c2015-10-22 16:01:17 -04007010static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7011{
7012 int rc;
7013 struct bnxt *bp = netdev_priv(dev);
7014
7015 SET_NETDEV_DEV(dev, &pdev->dev);
7016
7017 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7018 rc = pci_enable_device(pdev);
7019 if (rc) {
7020 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7021 goto init_err;
7022 }
7023
7024 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7025 dev_err(&pdev->dev,
7026 "Cannot find PCI device base address, aborting\n");
7027 rc = -ENODEV;
7028 goto init_err_disable;
7029 }
7030
7031 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7032 if (rc) {
7033 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7034 goto init_err_disable;
7035 }
7036
7037 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7038 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7039 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7040 goto init_err_disable;
7041 }
7042
7043 pci_set_master(pdev);
7044
7045 bp->dev = dev;
7046 bp->pdev = pdev;
7047
7048 bp->bar0 = pci_ioremap_bar(pdev, 0);
7049 if (!bp->bar0) {
7050 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7051 rc = -ENOMEM;
7052 goto init_err_release;
7053 }
7054
7055 bp->bar1 = pci_ioremap_bar(pdev, 2);
7056 if (!bp->bar1) {
7057 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7058 rc = -ENOMEM;
7059 goto init_err_release;
7060 }
7061
7062 bp->bar2 = pci_ioremap_bar(pdev, 4);
7063 if (!bp->bar2) {
7064 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7065 rc = -ENOMEM;
7066 goto init_err_release;
7067 }
7068
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007069 pci_enable_pcie_error_reporting(pdev);
7070
Michael Chanc0c050c2015-10-22 16:01:17 -04007071 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7072
7073 spin_lock_init(&bp->ntp_fltr_lock);
7074
7075 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7076 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7077
Michael Chandfb5b892016-02-26 04:00:01 -05007078 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007079 bp->rx_coal_ticks = 12;
7080 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007081 bp->rx_coal_ticks_irq = 1;
7082 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007083
Michael Chandfc9c942016-02-26 04:00:03 -05007084 bp->tx_coal_ticks = 25;
7085 bp->tx_coal_bufs = 30;
7086 bp->tx_coal_ticks_irq = 2;
7087 bp->tx_coal_bufs_irq = 2;
7088
Michael Chan51f30782016-07-01 18:46:29 -04007089 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7090
Michael Chanc0c050c2015-10-22 16:01:17 -04007091 init_timer(&bp->timer);
7092 bp->timer.data = (unsigned long)bp;
7093 bp->timer.function = bnxt_timer;
7094 bp->current_interval = BNXT_TIMER_INTERVAL;
7095
Michael Chancaefe522015-12-09 19:35:42 -05007096 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007097 return 0;
7098
7099init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007100 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007101 pci_release_regions(pdev);
7102
7103init_err_disable:
7104 pci_disable_device(pdev);
7105
7106init_err:
7107 return rc;
7108}
7109
7110/* rtnl_lock held */
7111static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7112{
7113 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007114 struct bnxt *bp = netdev_priv(dev);
7115 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007116
7117 if (!is_valid_ether_addr(addr->sa_data))
7118 return -EADDRNOTAVAIL;
7119
Michael Chan84c33dd2016-04-11 04:11:13 -04007120 rc = bnxt_approve_mac(bp, addr->sa_data);
7121 if (rc)
7122 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007123
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007124 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7125 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007126
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007127 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7128 if (netif_running(dev)) {
7129 bnxt_close_nic(bp, false, false);
7130 rc = bnxt_open_nic(bp, false, false);
7131 }
7132
7133 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007134}
7135
7136/* rtnl_lock held */
7137static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7138{
7139 struct bnxt *bp = netdev_priv(dev);
7140
Michael Chanc0c050c2015-10-22 16:01:17 -04007141 if (netif_running(dev))
7142 bnxt_close_nic(bp, false, false);
7143
7144 dev->mtu = new_mtu;
7145 bnxt_set_ring_params(bp);
7146
7147 if (netif_running(dev))
7148 return bnxt_open_nic(bp, false, false);
7149
7150 return 0;
7151}
7152
Michael Chanc5e3deb2016-12-02 21:17:15 -05007153int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007154{
7155 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007156 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007157 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007158
Michael Chanc0c050c2015-10-22 16:01:17 -04007159 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007160 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007161 tc, bp->max_tc);
7162 return -EINVAL;
7163 }
7164
7165 if (netdev_get_num_tc(dev) == tc)
7166 return 0;
7167
Michael Chan3ffb6a32016-11-11 00:11:42 -05007168 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7169 sh = true;
7170
Michael Chan5f449242017-02-06 16:55:40 -05007171 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007172 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007173 if (rc)
7174 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007175
7176 /* Needs to close the device and do hw resource re-allocations */
7177 if (netif_running(bp->dev))
7178 bnxt_close_nic(bp, true, false);
7179
7180 if (tc) {
7181 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7182 netdev_set_num_tc(dev, tc);
7183 } else {
7184 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7185 netdev_reset_tc(dev);
7186 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007187 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7188 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007189 bp->num_stat_ctxs = bp->cp_nr_rings;
7190
7191 if (netif_running(bp->dev))
7192 return bnxt_open_nic(bp, true, false);
7193
7194 return 0;
7195}
7196
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007197static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7198 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007199{
7200 if (ntc->type != TC_SETUP_MQPRIO)
7201 return -EINVAL;
7202
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007203 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7204
7205 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007206}
7207
Michael Chanc0c050c2015-10-22 16:01:17 -04007208#ifdef CONFIG_RFS_ACCEL
7209static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7210 struct bnxt_ntuple_filter *f2)
7211{
7212 struct flow_keys *keys1 = &f1->fkeys;
7213 struct flow_keys *keys2 = &f2->fkeys;
7214
7215 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7216 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7217 keys1->ports.ports == keys2->ports.ports &&
7218 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7219 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007220 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007221 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7222 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007223 return true;
7224
7225 return false;
7226}
7227
7228static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7229 u16 rxq_index, u32 flow_id)
7230{
7231 struct bnxt *bp = netdev_priv(dev);
7232 struct bnxt_ntuple_filter *fltr, *new_fltr;
7233 struct flow_keys *fkeys;
7234 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007235 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007236 struct hlist_head *head;
7237
Michael Chana54c4d72016-07-25 12:33:35 -04007238 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7239 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7240 int off = 0, j;
7241
7242 netif_addr_lock_bh(dev);
7243 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7244 if (ether_addr_equal(eth->h_dest,
7245 vnic->uc_list + off)) {
7246 l2_idx = j + 1;
7247 break;
7248 }
7249 }
7250 netif_addr_unlock_bh(dev);
7251 if (!l2_idx)
7252 return -EINVAL;
7253 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007254 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7255 if (!new_fltr)
7256 return -ENOMEM;
7257
7258 fkeys = &new_fltr->fkeys;
7259 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7260 rc = -EPROTONOSUPPORT;
7261 goto err_free;
7262 }
7263
Michael Chandda0e742016-12-29 12:13:40 -05007264 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7265 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007266 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7267 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7268 rc = -EPROTONOSUPPORT;
7269 goto err_free;
7270 }
Michael Chandda0e742016-12-29 12:13:40 -05007271 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7272 bp->hwrm_spec_code < 0x10601) {
7273 rc = -EPROTONOSUPPORT;
7274 goto err_free;
7275 }
Michael Chan61aad722017-02-12 19:18:14 -05007276 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7277 bp->hwrm_spec_code < 0x10601) {
7278 rc = -EPROTONOSUPPORT;
7279 goto err_free;
7280 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007281
Michael Chana54c4d72016-07-25 12:33:35 -04007282 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007283 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7284
7285 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7286 head = &bp->ntp_fltr_hash_tbl[idx];
7287 rcu_read_lock();
7288 hlist_for_each_entry_rcu(fltr, head, hash) {
7289 if (bnxt_fltr_match(fltr, new_fltr)) {
7290 rcu_read_unlock();
7291 rc = 0;
7292 goto err_free;
7293 }
7294 }
7295 rcu_read_unlock();
7296
7297 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007298 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7299 BNXT_NTP_FLTR_MAX_FLTR, 0);
7300 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007301 spin_unlock_bh(&bp->ntp_fltr_lock);
7302 rc = -ENOMEM;
7303 goto err_free;
7304 }
7305
Michael Chan84e86b92015-11-05 16:25:50 -05007306 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007307 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007308 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007309 new_fltr->rxq = rxq_index;
7310 hlist_add_head_rcu(&new_fltr->hash, head);
7311 bp->ntp_fltr_count++;
7312 spin_unlock_bh(&bp->ntp_fltr_lock);
7313
7314 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7315 schedule_work(&bp->sp_task);
7316
7317 return new_fltr->sw_id;
7318
7319err_free:
7320 kfree(new_fltr);
7321 return rc;
7322}
7323
7324static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7325{
7326 int i;
7327
7328 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7329 struct hlist_head *head;
7330 struct hlist_node *tmp;
7331 struct bnxt_ntuple_filter *fltr;
7332 int rc;
7333
7334 head = &bp->ntp_fltr_hash_tbl[i];
7335 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7336 bool del = false;
7337
7338 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7339 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7340 fltr->flow_id,
7341 fltr->sw_id)) {
7342 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7343 fltr);
7344 del = true;
7345 }
7346 } else {
7347 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7348 fltr);
7349 if (rc)
7350 del = true;
7351 else
7352 set_bit(BNXT_FLTR_VALID, &fltr->state);
7353 }
7354
7355 if (del) {
7356 spin_lock_bh(&bp->ntp_fltr_lock);
7357 hlist_del_rcu(&fltr->hash);
7358 bp->ntp_fltr_count--;
7359 spin_unlock_bh(&bp->ntp_fltr_lock);
7360 synchronize_rcu();
7361 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7362 kfree(fltr);
7363 }
7364 }
7365 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007366 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7367 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007368}
7369
7370#else
7371
7372static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7373{
7374}
7375
7376#endif /* CONFIG_RFS_ACCEL */
7377
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007378static void bnxt_udp_tunnel_add(struct net_device *dev,
7379 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007380{
7381 struct bnxt *bp = netdev_priv(dev);
7382
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007383 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7384 return;
7385
Michael Chanc0c050c2015-10-22 16:01:17 -04007386 if (!netif_running(dev))
7387 return;
7388
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007389 switch (ti->type) {
7390 case UDP_TUNNEL_TYPE_VXLAN:
7391 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7392 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007393
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007394 bp->vxlan_port_cnt++;
7395 if (bp->vxlan_port_cnt == 1) {
7396 bp->vxlan_port = ti->port;
7397 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007398 schedule_work(&bp->sp_task);
7399 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007400 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007401 case UDP_TUNNEL_TYPE_GENEVE:
7402 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7403 return;
7404
7405 bp->nge_port_cnt++;
7406 if (bp->nge_port_cnt == 1) {
7407 bp->nge_port = ti->port;
7408 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7409 }
7410 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007411 default:
7412 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007413 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007414
7415 schedule_work(&bp->sp_task);
7416}
7417
7418static void bnxt_udp_tunnel_del(struct net_device *dev,
7419 struct udp_tunnel_info *ti)
7420{
7421 struct bnxt *bp = netdev_priv(dev);
7422
7423 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7424 return;
7425
7426 if (!netif_running(dev))
7427 return;
7428
7429 switch (ti->type) {
7430 case UDP_TUNNEL_TYPE_VXLAN:
7431 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7432 return;
7433 bp->vxlan_port_cnt--;
7434
7435 if (bp->vxlan_port_cnt != 0)
7436 return;
7437
7438 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7439 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007440 case UDP_TUNNEL_TYPE_GENEVE:
7441 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7442 return;
7443 bp->nge_port_cnt--;
7444
7445 if (bp->nge_port_cnt != 0)
7446 return;
7447
7448 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7449 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007450 default:
7451 return;
7452 }
7453
7454 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007455}
7456
Michael Chan39d8ba22017-07-24 12:34:22 -04007457static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7458 struct net_device *dev, u32 filter_mask,
7459 int nlflags)
7460{
7461 struct bnxt *bp = netdev_priv(dev);
7462
7463 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7464 nlflags, filter_mask, NULL);
7465}
7466
7467static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7468 u16 flags)
7469{
7470 struct bnxt *bp = netdev_priv(dev);
7471 struct nlattr *attr, *br_spec;
7472 int rem, rc = 0;
7473
7474 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7475 return -EOPNOTSUPP;
7476
7477 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7478 if (!br_spec)
7479 return -EINVAL;
7480
7481 nla_for_each_nested(attr, br_spec, rem) {
7482 u16 mode;
7483
7484 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7485 continue;
7486
7487 if (nla_len(attr) < sizeof(mode))
7488 return -EINVAL;
7489
7490 mode = nla_get_u16(attr);
7491 if (mode == bp->br_mode)
7492 break;
7493
7494 rc = bnxt_hwrm_set_br_mode(bp, mode);
7495 if (!rc)
7496 bp->br_mode = mode;
7497 break;
7498 }
7499 return rc;
7500}
7501
Michael Chanc0c050c2015-10-22 16:01:17 -04007502static const struct net_device_ops bnxt_netdev_ops = {
7503 .ndo_open = bnxt_open,
7504 .ndo_start_xmit = bnxt_start_xmit,
7505 .ndo_stop = bnxt_close,
7506 .ndo_get_stats64 = bnxt_get_stats64,
7507 .ndo_set_rx_mode = bnxt_set_rx_mode,
7508 .ndo_do_ioctl = bnxt_ioctl,
7509 .ndo_validate_addr = eth_validate_addr,
7510 .ndo_set_mac_address = bnxt_change_mac_addr,
7511 .ndo_change_mtu = bnxt_change_mtu,
7512 .ndo_fix_features = bnxt_fix_features,
7513 .ndo_set_features = bnxt_set_features,
7514 .ndo_tx_timeout = bnxt_tx_timeout,
7515#ifdef CONFIG_BNXT_SRIOV
7516 .ndo_get_vf_config = bnxt_get_vf_config,
7517 .ndo_set_vf_mac = bnxt_set_vf_mac,
7518 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7519 .ndo_set_vf_rate = bnxt_set_vf_bw,
7520 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7521 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7522#endif
7523#ifdef CONFIG_NET_POLL_CONTROLLER
7524 .ndo_poll_controller = bnxt_poll_controller,
7525#endif
7526 .ndo_setup_tc = bnxt_setup_tc,
7527#ifdef CONFIG_RFS_ACCEL
7528 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7529#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007530 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7531 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007532 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007533 .ndo_bridge_getlink = bnxt_bridge_getlink,
7534 .ndo_bridge_setlink = bnxt_bridge_setlink,
Michael Chanc0c050c2015-10-22 16:01:17 -04007535};
7536
7537static void bnxt_remove_one(struct pci_dev *pdev)
7538{
7539 struct net_device *dev = pci_get_drvdata(pdev);
7540 struct bnxt *bp = netdev_priv(dev);
7541
7542 if (BNXT_PF(bp))
7543 bnxt_sriov_disable(bp);
7544
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007545 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007546 unregister_netdev(dev);
7547 cancel_work_sync(&bp->sp_task);
7548 bp->sp_event = 0;
7549
Michael Chan78095922016-12-07 00:26:16 -05007550 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007551 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007552 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007553 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007554 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007555 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007556 kfree(bp->edev);
7557 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007558 if (bp->xdp_prog)
7559 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007560 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007561 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007562}
7563
7564static int bnxt_probe_phy(struct bnxt *bp)
7565{
7566 int rc = 0;
7567 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007568
Michael Chan170ce012016-04-05 14:08:57 -04007569 rc = bnxt_hwrm_phy_qcaps(bp);
7570 if (rc) {
7571 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7572 rc);
7573 return rc;
7574 }
7575
Michael Chanc0c050c2015-10-22 16:01:17 -04007576 rc = bnxt_update_link(bp, false);
7577 if (rc) {
7578 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7579 rc);
7580 return rc;
7581 }
7582
Michael Chan93ed8112016-06-13 02:25:37 -04007583 /* Older firmware does not have supported_auto_speeds, so assume
7584 * that all supported speeds can be autonegotiated.
7585 */
7586 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7587 link_info->support_auto_speeds = link_info->support_speeds;
7588
Michael Chanc0c050c2015-10-22 16:01:17 -04007589 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007590 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007591 link_info->autoneg = BNXT_AUTONEG_SPEED;
7592 if (bp->hwrm_spec_code >= 0x10201) {
7593 if (link_info->auto_pause_setting &
7594 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7595 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7596 } else {
7597 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7598 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007599 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007600 } else {
7601 link_info->req_link_speed = link_info->force_link_speed;
7602 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007603 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007604 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7605 link_info->req_flow_ctrl =
7606 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7607 else
7608 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007609 return rc;
7610}
7611
7612static int bnxt_get_max_irq(struct pci_dev *pdev)
7613{
7614 u16 ctrl;
7615
7616 if (!pdev->msix_cap)
7617 return 1;
7618
7619 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7620 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7621}
7622
Michael Chan6e6c5a52016-01-02 23:45:02 -05007623static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7624 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007625{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007626 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007627
Michael Chan379a80a2015-10-23 15:06:19 -04007628#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007629 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007630 *max_tx = bp->vf.max_tx_rings;
7631 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007632 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7633 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007634 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007635 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007636#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007637 {
7638 *max_tx = bp->pf.max_tx_rings;
7639 *max_rx = bp->pf.max_rx_rings;
7640 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7641 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7642 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007643 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007644 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7645 *max_cp -= 1;
7646 *max_rx -= 2;
7647 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007648 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7649 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007650 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007651}
7652
7653int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7654{
7655 int rx, tx, cp;
7656
7657 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7658 if (!rx || !tx || !cp)
7659 return -ENOMEM;
7660
7661 *max_rx = rx;
7662 *max_tx = tx;
7663 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7664}
7665
Michael Chane4060d32016-12-07 00:26:19 -05007666static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7667 bool shared)
7668{
7669 int rc;
7670
7671 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007672 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7673 /* Not enough rings, try disabling agg rings. */
7674 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7675 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7676 if (rc)
7677 return rc;
7678 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7679 bp->dev->hw_features &= ~NETIF_F_LRO;
7680 bp->dev->features &= ~NETIF_F_LRO;
7681 bnxt_set_ring_params(bp);
7682 }
Michael Chane4060d32016-12-07 00:26:19 -05007683
7684 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7685 int max_cp, max_stat, max_irq;
7686
7687 /* Reserve minimum resources for RoCE */
7688 max_cp = bnxt_get_max_func_cp_rings(bp);
7689 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7690 max_irq = bnxt_get_max_func_irqs(bp);
7691 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7692 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7693 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7694 return 0;
7695
7696 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7697 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7698 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7699 max_cp = min_t(int, max_cp, max_irq);
7700 max_cp = min_t(int, max_cp, max_stat);
7701 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7702 if (rc)
7703 rc = 0;
7704 }
7705 return rc;
7706}
7707
Michael Chan702c2212017-05-29 19:06:10 -04007708static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007709{
7710 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007711
7712 if (sh)
7713 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7714 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007715 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007716 if (rc)
7717 return rc;
7718 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7719 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007720
7721 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7722 if (rc)
7723 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7724
Michael Chan6e6c5a52016-01-02 23:45:02 -05007725 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7726 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7727 bp->tx_nr_rings + bp->rx_nr_rings;
7728 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007729 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7730 bp->rx_nr_rings++;
7731 bp->cp_nr_rings++;
7732 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007733 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007734}
7735
Michael Chan7b08f662016-12-07 00:26:18 -05007736void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7737{
7738 ASSERT_RTNL();
7739 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007740 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007741}
7742
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007743static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7744{
7745 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7746 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7747
7748 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7749 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7750 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7751 else
7752 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7753 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7754 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7755 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7756 "Unknown", width);
7757}
7758
Michael Chanc0c050c2015-10-22 16:01:17 -04007759static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7760{
7761 static int version_printed;
7762 struct net_device *dev;
7763 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007764 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007765
Ray Jui4e003382017-02-20 19:25:16 -05007766 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007767 return -ENODEV;
7768
Michael Chanc0c050c2015-10-22 16:01:17 -04007769 if (version_printed++ == 0)
7770 pr_info("%s", version);
7771
7772 max_irqs = bnxt_get_max_irq(pdev);
7773 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7774 if (!dev)
7775 return -ENOMEM;
7776
7777 bp = netdev_priv(dev);
7778
7779 if (bnxt_vf_pciid(ent->driver_data))
7780 bp->flags |= BNXT_FLAG_VF;
7781
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007782 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007783 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007784
7785 rc = bnxt_init_board(pdev, dev);
7786 if (rc < 0)
7787 goto init_err_free;
7788
7789 dev->netdev_ops = &bnxt_netdev_ops;
7790 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7791 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007792 pci_set_drvdata(pdev, dev);
7793
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007794 rc = bnxt_alloc_hwrm_resources(bp);
7795 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007796 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007797
7798 mutex_init(&bp->hwrm_cmd_lock);
7799 rc = bnxt_hwrm_ver_get(bp);
7800 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007801 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007802
Deepak Khungare605db82017-05-29 19:06:04 -04007803 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7804 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7805 if (rc)
7806 goto init_err_pci_clean;
7807 }
7808
Michael Chan3c2217a2017-03-08 18:44:32 -05007809 rc = bnxt_hwrm_func_reset(bp);
7810 if (rc)
7811 goto init_err_pci_clean;
7812
Rob Swindell5ac67d82016-09-19 03:58:03 -04007813 bnxt_hwrm_fw_set_time(bp);
7814
Michael Chanc0c050c2015-10-22 16:01:17 -04007815 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7816 NETIF_F_TSO | NETIF_F_TSO6 |
7817 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007818 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007819 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7820 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007821 NETIF_F_RXCSUM | NETIF_F_GRO;
7822
7823 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7824 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007825
Michael Chanc0c050c2015-10-22 16:01:17 -04007826 dev->hw_enc_features =
7827 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7828 NETIF_F_TSO | NETIF_F_TSO6 |
7829 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007830 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007831 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007832 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7833 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007834 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7835 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7836 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7837 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7838 dev->priv_flags |= IFF_UNICAST_FLT;
7839
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007840 /* MTU range: 60 - 9500 */
7841 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007842 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007843
Michael Chanc0c050c2015-10-22 16:01:17 -04007844#ifdef CONFIG_BNXT_SRIOV
7845 init_waitqueue_head(&bp->sriov_cfg_wait);
7846#endif
Michael Chan309369c2016-06-13 02:25:34 -04007847 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007848 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007849 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007850 else
7851 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007852
Michael Chanc0c050c2015-10-22 16:01:17 -04007853 rc = bnxt_hwrm_func_drv_rgtr(bp);
7854 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007855 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007856
Michael Chana1653b12016-12-07 00:26:20 -05007857 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7858 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007859 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007860
Michael Chana588e452016-12-07 00:26:21 -05007861 bp->ulp_probe = bnxt_ulp_probe;
7862
Michael Chanc0c050c2015-10-22 16:01:17 -04007863 /* Get the MAX capabilities for this function */
7864 rc = bnxt_hwrm_func_qcaps(bp);
7865 if (rc) {
7866 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7867 rc);
7868 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007869 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007870 }
7871
7872 rc = bnxt_hwrm_queue_qportcfg(bp);
7873 if (rc) {
7874 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7875 rc);
7876 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007877 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007878 }
7879
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007880 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007881 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007882 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007883 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007884
Michael Chanc61fb992017-02-06 16:55:36 -05007885 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007886 bnxt_set_tpa_flags(bp);
7887 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007888 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007889 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007890 if (rc) {
7891 netdev_err(bp->dev, "Not enough rings available.\n");
7892 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007893 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007894 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007895
Michael Chan87da7f72016-11-16 21:13:09 -05007896 /* Default RSS hash cfg. */
7897 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7898 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7899 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7900 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007901 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007902 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7903 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7904 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7905 }
7906
Michael Chan8fdefd62016-12-29 12:13:36 -05007907 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007908 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007909 dev->hw_features |= NETIF_F_NTUPLE;
7910 if (bnxt_rfs_capable(bp)) {
7911 bp->flags |= BNXT_FLAG_RFS;
7912 dev->features |= NETIF_F_NTUPLE;
7913 }
7914 }
7915
Michael Chanc0c050c2015-10-22 16:01:17 -04007916 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7917 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7918
7919 rc = bnxt_probe_phy(bp);
7920 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007921 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007922
Michael Chan78095922016-12-07 00:26:16 -05007923 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007924 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007925 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007926
Michael Chanc1ef1462017-04-04 18:14:07 -04007927 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007928 if (bp->flags & BNXT_FLAG_WOL_CAP)
7929 device_set_wakeup_enable(&pdev->dev, bp->wol);
7930 else
7931 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007932
Michael Chan78095922016-12-07 00:26:16 -05007933 rc = register_netdev(dev);
7934 if (rc)
7935 goto init_err_clr_int;
7936
Michael Chanc0c050c2015-10-22 16:01:17 -04007937 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7938 board_info[ent->driver_data].name,
7939 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7940
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007941 bnxt_parse_log_pcie_link(bp);
7942
Michael Chanc0c050c2015-10-22 16:01:17 -04007943 return 0;
7944
Michael Chan78095922016-12-07 00:26:16 -05007945init_err_clr_int:
7946 bnxt_clear_int_mode(bp);
7947
Sathya Perla17086392017-02-20 19:25:18 -05007948init_err_pci_clean:
7949 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007950
7951init_err_free:
7952 free_netdev(dev);
7953 return rc;
7954}
7955
Michael Chand196ece2017-04-04 18:14:08 -04007956static void bnxt_shutdown(struct pci_dev *pdev)
7957{
7958 struct net_device *dev = pci_get_drvdata(pdev);
7959 struct bnxt *bp;
7960
7961 if (!dev)
7962 return;
7963
7964 rtnl_lock();
7965 bp = netdev_priv(dev);
7966 if (!bp)
7967 goto shutdown_exit;
7968
7969 if (netif_running(dev))
7970 dev_close(dev);
7971
7972 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04007973 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007974 bnxt_clear_int_mode(bp);
7975 pci_wake_from_d3(pdev, bp->wol);
7976 pci_set_power_state(pdev, PCI_D3hot);
7977 }
7978
7979shutdown_exit:
7980 rtnl_unlock();
7981}
7982
Michael Chanf65a2042017-04-04 18:14:11 -04007983#ifdef CONFIG_PM_SLEEP
7984static int bnxt_suspend(struct device *device)
7985{
7986 struct pci_dev *pdev = to_pci_dev(device);
7987 struct net_device *dev = pci_get_drvdata(pdev);
7988 struct bnxt *bp = netdev_priv(dev);
7989 int rc = 0;
7990
7991 rtnl_lock();
7992 if (netif_running(dev)) {
7993 netif_device_detach(dev);
7994 rc = bnxt_close(dev);
7995 }
7996 bnxt_hwrm_func_drv_unrgtr(bp);
7997 rtnl_unlock();
7998 return rc;
7999}
8000
8001static int bnxt_resume(struct device *device)
8002{
8003 struct pci_dev *pdev = to_pci_dev(device);
8004 struct net_device *dev = pci_get_drvdata(pdev);
8005 struct bnxt *bp = netdev_priv(dev);
8006 int rc = 0;
8007
8008 rtnl_lock();
8009 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8010 rc = -ENODEV;
8011 goto resume_exit;
8012 }
8013 rc = bnxt_hwrm_func_reset(bp);
8014 if (rc) {
8015 rc = -EBUSY;
8016 goto resume_exit;
8017 }
8018 bnxt_get_wol_settings(bp);
8019 if (netif_running(dev)) {
8020 rc = bnxt_open(dev);
8021 if (!rc)
8022 netif_device_attach(dev);
8023 }
8024
8025resume_exit:
8026 rtnl_unlock();
8027 return rc;
8028}
8029
8030static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8031#define BNXT_PM_OPS (&bnxt_pm_ops)
8032
8033#else
8034
8035#define BNXT_PM_OPS NULL
8036
8037#endif /* CONFIG_PM_SLEEP */
8038
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008039/**
8040 * bnxt_io_error_detected - called when PCI error is detected
8041 * @pdev: Pointer to PCI device
8042 * @state: The current pci connection state
8043 *
8044 * This function is called after a PCI bus error affecting
8045 * this device has been detected.
8046 */
8047static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8048 pci_channel_state_t state)
8049{
8050 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008051 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008052
8053 netdev_info(netdev, "PCI I/O error detected\n");
8054
8055 rtnl_lock();
8056 netif_device_detach(netdev);
8057
Michael Chana588e452016-12-07 00:26:21 -05008058 bnxt_ulp_stop(bp);
8059
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008060 if (state == pci_channel_io_perm_failure) {
8061 rtnl_unlock();
8062 return PCI_ERS_RESULT_DISCONNECT;
8063 }
8064
8065 if (netif_running(netdev))
8066 bnxt_close(netdev);
8067
8068 pci_disable_device(pdev);
8069 rtnl_unlock();
8070
8071 /* Request a slot slot reset. */
8072 return PCI_ERS_RESULT_NEED_RESET;
8073}
8074
8075/**
8076 * bnxt_io_slot_reset - called after the pci bus has been reset.
8077 * @pdev: Pointer to PCI device
8078 *
8079 * Restart the card from scratch, as if from a cold-boot.
8080 * At this point, the card has exprienced a hard reset,
8081 * followed by fixups by BIOS, and has its config space
8082 * set up identically to what it was at cold boot.
8083 */
8084static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8085{
8086 struct net_device *netdev = pci_get_drvdata(pdev);
8087 struct bnxt *bp = netdev_priv(netdev);
8088 int err = 0;
8089 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8090
8091 netdev_info(bp->dev, "PCI Slot Reset\n");
8092
8093 rtnl_lock();
8094
8095 if (pci_enable_device(pdev)) {
8096 dev_err(&pdev->dev,
8097 "Cannot re-enable PCI device after reset.\n");
8098 } else {
8099 pci_set_master(pdev);
8100
Michael Chanaa8ed022016-12-07 00:26:17 -05008101 err = bnxt_hwrm_func_reset(bp);
8102 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008103 err = bnxt_open(netdev);
8104
Michael Chana588e452016-12-07 00:26:21 -05008105 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008106 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008107 bnxt_ulp_start(bp);
8108 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008109 }
8110
8111 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8112 dev_close(netdev);
8113
8114 rtnl_unlock();
8115
8116 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8117 if (err) {
8118 dev_err(&pdev->dev,
8119 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8120 err); /* non-fatal, continue */
8121 }
8122
8123 return PCI_ERS_RESULT_RECOVERED;
8124}
8125
8126/**
8127 * bnxt_io_resume - called when traffic can start flowing again.
8128 * @pdev: Pointer to PCI device
8129 *
8130 * This callback is called when the error recovery driver tells
8131 * us that its OK to resume normal operation.
8132 */
8133static void bnxt_io_resume(struct pci_dev *pdev)
8134{
8135 struct net_device *netdev = pci_get_drvdata(pdev);
8136
8137 rtnl_lock();
8138
8139 netif_device_attach(netdev);
8140
8141 rtnl_unlock();
8142}
8143
8144static const struct pci_error_handlers bnxt_err_handler = {
8145 .error_detected = bnxt_io_error_detected,
8146 .slot_reset = bnxt_io_slot_reset,
8147 .resume = bnxt_io_resume
8148};
8149
Michael Chanc0c050c2015-10-22 16:01:17 -04008150static struct pci_driver bnxt_pci_driver = {
8151 .name = DRV_MODULE_NAME,
8152 .id_table = bnxt_pci_tbl,
8153 .probe = bnxt_init_one,
8154 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008155 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008156 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008157 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008158#if defined(CONFIG_BNXT_SRIOV)
8159 .sriov_configure = bnxt_sriov_configure,
8160#endif
8161};
8162
8163module_pci_driver(bnxt_pci_driver);