blob: 4acaeaf87f188464638e13e0aae7991406b41a04 [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 Chancf6645f2016-06-13 02:25:28 -04004581 int rc;
4582
4583 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4584 req.fid = cpu_to_le16(0xffff);
4585 mutex_lock(&bp->hwrm_cmd_lock);
4586 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4587 if (rc)
4588 goto func_qcfg_exit;
4589
4590#ifdef CONFIG_BNXT_SRIOV
4591 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004592 struct bnxt_vf_info *vf = &bp->vf;
4593
4594 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4595 }
4596#endif
Deepak Khungar9e54e322017-04-21 20:11:26 -04004597 if (BNXT_PF(bp)) {
4598 u16 flags = le16_to_cpu(resp->flags);
4599
4600 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4601 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED))
4602 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4603 if (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
4604 bp->flags |= BNXT_FLAG_MULTI_HOST;
4605 }
Michael Chanbc39f882017-03-08 18:44:34 -05004606
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004607 switch (resp->port_partition_type) {
4608 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4609 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4610 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4611 bp->port_partition_type = resp->port_partition_type;
4612 break;
4613 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004614 if (bp->hwrm_spec_code < 0x10707 ||
4615 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4616 bp->br_mode = BRIDGE_MODE_VEB;
4617 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4618 bp->br_mode = BRIDGE_MODE_VEPA;
4619 else
4620 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004621
4622func_qcfg_exit:
4623 mutex_unlock(&bp->hwrm_cmd_lock);
4624 return rc;
4625}
4626
Michael Chan7b08f662016-12-07 00:26:18 -05004627static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004628{
4629 int rc = 0;
4630 struct hwrm_func_qcaps_input req = {0};
4631 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4632
4633 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4634 req.fid = cpu_to_le16(0xffff);
4635
4636 mutex_lock(&bp->hwrm_cmd_lock);
4637 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4638 if (rc)
4639 goto hwrm_func_qcaps_exit;
4640
Michael Chane4060d32016-12-07 00:26:19 -05004641 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4642 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4643 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4644 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4645
Michael Chan7cc5a202016-09-19 03:58:05 -04004646 bp->tx_push_thresh = 0;
4647 if (resp->flags &
4648 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4649 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4650
Michael Chanc0c050c2015-10-22 16:01:17 -04004651 if (BNXT_PF(bp)) {
4652 struct bnxt_pf_info *pf = &bp->pf;
4653
4654 pf->fw_fid = le16_to_cpu(resp->fid);
4655 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004656 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004657 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004658 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004659 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4660 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4661 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004662 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004663 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4664 if (!pf->max_hw_ring_grps)
4665 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004666 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4667 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4668 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4669 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4670 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4671 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4672 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4673 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4674 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4675 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4676 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004677 if (resp->flags &
4678 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4679 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004680 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004681#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004682 struct bnxt_vf_info *vf = &bp->vf;
4683
4684 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004685
4686 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4687 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4688 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4689 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004690 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4691 if (!vf->max_hw_ring_grps)
4692 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004693 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4694 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4695 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004696
4697 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004698 mutex_unlock(&bp->hwrm_cmd_lock);
4699
4700 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004701 /* overwrite netdev dev_adr with admin VF MAC */
4702 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004703 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004704 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004705 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4706 }
4707 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004708#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004709 }
4710
Michael Chanc0c050c2015-10-22 16:01:17 -04004711hwrm_func_qcaps_exit:
4712 mutex_unlock(&bp->hwrm_cmd_lock);
4713 return rc;
4714}
4715
4716static int bnxt_hwrm_func_reset(struct bnxt *bp)
4717{
4718 struct hwrm_func_reset_input req = {0};
4719
4720 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4721 req.enables = 0;
4722
4723 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4724}
4725
4726static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4727{
4728 int rc = 0;
4729 struct hwrm_queue_qportcfg_input req = {0};
4730 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4731 u8 i, *qptr;
4732
4733 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4734
4735 mutex_lock(&bp->hwrm_cmd_lock);
4736 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4737 if (rc)
4738 goto qportcfg_exit;
4739
4740 if (!resp->max_configurable_queues) {
4741 rc = -EINVAL;
4742 goto qportcfg_exit;
4743 }
4744 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004745 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004746 if (bp->max_tc > BNXT_MAX_QUEUE)
4747 bp->max_tc = BNXT_MAX_QUEUE;
4748
Michael Chan441cabb2016-09-19 03:58:02 -04004749 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4750 bp->max_tc = 1;
4751
Michael Chan87c374d2016-12-02 21:17:16 -05004752 if (bp->max_lltc > bp->max_tc)
4753 bp->max_lltc = bp->max_tc;
4754
Michael Chanc0c050c2015-10-22 16:01:17 -04004755 qptr = &resp->queue_id0;
4756 for (i = 0; i < bp->max_tc; i++) {
4757 bp->q_info[i].queue_id = *qptr++;
4758 bp->q_info[i].queue_profile = *qptr++;
4759 }
4760
4761qportcfg_exit:
4762 mutex_unlock(&bp->hwrm_cmd_lock);
4763 return rc;
4764}
4765
4766static int bnxt_hwrm_ver_get(struct bnxt *bp)
4767{
4768 int rc;
4769 struct hwrm_ver_get_input req = {0};
4770 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004771 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004772
Michael Chane6ef2692016-03-28 19:46:05 -04004773 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4775 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4776 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4777 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4778 mutex_lock(&bp->hwrm_cmd_lock);
4779 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4780 if (rc)
4781 goto hwrm_ver_get_exit;
4782
4783 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4784
Michael Chan11f15ed2016-04-05 14:08:55 -04004785 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4786 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004787 if (resp->hwrm_intf_maj < 1) {
4788 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004789 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004790 resp->hwrm_intf_upd);
4791 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004792 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004793 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004794 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4795 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4796
Michael Chanff4fe812016-02-26 04:00:04 -05004797 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4798 if (!bp->hwrm_cmd_timeout)
4799 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4800
Michael Chane6ef2692016-03-28 19:46:05 -04004801 if (resp->hwrm_intf_maj >= 1)
4802 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4803
Michael Chan659c8052016-06-13 02:25:33 -04004804 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004805 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4806 !resp->chip_metal)
4807 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004808
Deepak Khungare605db82017-05-29 19:06:04 -04004809 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4810 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4811 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4812 bp->flags |= BNXT_FLAG_SHORT_CMD;
4813
Michael Chanc0c050c2015-10-22 16:01:17 -04004814hwrm_ver_get_exit:
4815 mutex_unlock(&bp->hwrm_cmd_lock);
4816 return rc;
4817}
4818
Rob Swindell5ac67d82016-09-19 03:58:03 -04004819int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4820{
Rob Swindell878786d2016-09-20 03:36:33 -04004821#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004822 struct hwrm_fw_set_time_input req = {0};
4823 struct rtc_time tm;
4824 struct timeval tv;
4825
4826 if (bp->hwrm_spec_code < 0x10400)
4827 return -EOPNOTSUPP;
4828
4829 do_gettimeofday(&tv);
4830 rtc_time_to_tm(tv.tv_sec, &tm);
4831 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4832 req.year = cpu_to_le16(1900 + tm.tm_year);
4833 req.month = 1 + tm.tm_mon;
4834 req.day = tm.tm_mday;
4835 req.hour = tm.tm_hour;
4836 req.minute = tm.tm_min;
4837 req.second = tm.tm_sec;
4838 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004839#else
4840 return -EOPNOTSUPP;
4841#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004842}
4843
Michael Chan3bdf56c2016-03-07 15:38:45 -05004844static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4845{
4846 int rc;
4847 struct bnxt_pf_info *pf = &bp->pf;
4848 struct hwrm_port_qstats_input req = {0};
4849
4850 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4851 return 0;
4852
4853 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4854 req.port_id = cpu_to_le16(pf->port_id);
4855 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4856 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4857 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4858 return rc;
4859}
4860
Michael Chanc0c050c2015-10-22 16:01:17 -04004861static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4862{
4863 if (bp->vxlan_port_cnt) {
4864 bnxt_hwrm_tunnel_dst_port_free(
4865 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4866 }
4867 bp->vxlan_port_cnt = 0;
4868 if (bp->nge_port_cnt) {
4869 bnxt_hwrm_tunnel_dst_port_free(
4870 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4871 }
4872 bp->nge_port_cnt = 0;
4873}
4874
4875static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4876{
4877 int rc, i;
4878 u32 tpa_flags = 0;
4879
4880 if (set_tpa)
4881 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4882 for (i = 0; i < bp->nr_vnics; i++) {
4883 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4884 if (rc) {
4885 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004886 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004887 return rc;
4888 }
4889 }
4890 return 0;
4891}
4892
4893static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4894{
4895 int i;
4896
4897 for (i = 0; i < bp->nr_vnics; i++)
4898 bnxt_hwrm_vnic_set_rss(bp, i, false);
4899}
4900
4901static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4902 bool irq_re_init)
4903{
4904 if (bp->vnic_info) {
4905 bnxt_hwrm_clear_vnic_filter(bp);
4906 /* clear all RSS setting before free vnic ctx */
4907 bnxt_hwrm_clear_vnic_rss(bp);
4908 bnxt_hwrm_vnic_ctx_free(bp);
4909 /* before free the vnic, undo the vnic tpa settings */
4910 if (bp->flags & BNXT_FLAG_TPA)
4911 bnxt_set_tpa(bp, false);
4912 bnxt_hwrm_vnic_free(bp);
4913 }
4914 bnxt_hwrm_ring_free(bp, close_path);
4915 bnxt_hwrm_ring_grp_free(bp);
4916 if (irq_re_init) {
4917 bnxt_hwrm_stat_ctx_free(bp);
4918 bnxt_hwrm_free_tunnel_ports(bp);
4919 }
4920}
4921
Michael Chan39d8ba22017-07-24 12:34:22 -04004922static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
4923{
4924 struct hwrm_func_cfg_input req = {0};
4925 int rc;
4926
4927 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4928 req.fid = cpu_to_le16(0xffff);
4929 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
4930 if (br_mode == BRIDGE_MODE_VEB)
4931 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
4932 else if (br_mode == BRIDGE_MODE_VEPA)
4933 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
4934 else
4935 return -EINVAL;
4936 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4937 if (rc)
4938 rc = -EIO;
4939 return rc;
4940}
4941
Michael Chanc0c050c2015-10-22 16:01:17 -04004942static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4943{
Michael Chanae10ae72016-12-29 12:13:38 -05004944 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004945 int rc;
4946
Michael Chanae10ae72016-12-29 12:13:38 -05004947 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4948 goto skip_rss_ctx;
4949
Michael Chanc0c050c2015-10-22 16:01:17 -04004950 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004951 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004952 if (rc) {
4953 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4954 vnic_id, rc);
4955 goto vnic_setup_err;
4956 }
4957 bp->rsscos_nr_ctxs++;
4958
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004959 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4960 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4961 if (rc) {
4962 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4963 vnic_id, rc);
4964 goto vnic_setup_err;
4965 }
4966 bp->rsscos_nr_ctxs++;
4967 }
4968
Michael Chanae10ae72016-12-29 12:13:38 -05004969skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004970 /* configure default vnic, ring grp */
4971 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4972 if (rc) {
4973 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4974 vnic_id, rc);
4975 goto vnic_setup_err;
4976 }
4977
4978 /* Enable RSS hashing on vnic */
4979 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4980 if (rc) {
4981 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4982 vnic_id, rc);
4983 goto vnic_setup_err;
4984 }
4985
4986 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4987 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4988 if (rc) {
4989 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4990 vnic_id, rc);
4991 }
4992 }
4993
4994vnic_setup_err:
4995 return rc;
4996}
4997
4998static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4999{
5000#ifdef CONFIG_RFS_ACCEL
5001 int i, rc = 0;
5002
5003 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005004 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005005 u16 vnic_id = i + 1;
5006 u16 ring_id = i;
5007
5008 if (vnic_id >= bp->nr_vnics)
5009 break;
5010
Michael Chanae10ae72016-12-29 12:13:38 -05005011 vnic = &bp->vnic_info[vnic_id];
5012 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5013 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5014 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005015 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005016 if (rc) {
5017 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5018 vnic_id, rc);
5019 break;
5020 }
5021 rc = bnxt_setup_vnic(bp, vnic_id);
5022 if (rc)
5023 break;
5024 }
5025 return rc;
5026#else
5027 return 0;
5028#endif
5029}
5030
Michael Chan17c71ac2016-07-01 18:46:27 -04005031/* Allow PF and VF with default VLAN to be in promiscuous mode */
5032static bool bnxt_promisc_ok(struct bnxt *bp)
5033{
5034#ifdef CONFIG_BNXT_SRIOV
5035 if (BNXT_VF(bp) && !bp->vf.vlan)
5036 return false;
5037#endif
5038 return true;
5039}
5040
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005041static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5042{
5043 unsigned int rc = 0;
5044
5045 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5046 if (rc) {
5047 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5048 rc);
5049 return rc;
5050 }
5051
5052 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5053 if (rc) {
5054 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5055 rc);
5056 return rc;
5057 }
5058 return rc;
5059}
5060
Michael Chanb664f002015-12-02 01:54:08 -05005061static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005062static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005063
Michael Chanc0c050c2015-10-22 16:01:17 -04005064static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5065{
Michael Chan7d2837d2016-05-04 16:56:44 -04005066 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005067 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005068 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005069
5070 if (irq_re_init) {
5071 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5072 if (rc) {
5073 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5074 rc);
5075 goto err_out;
5076 }
5077 }
5078
5079 rc = bnxt_hwrm_ring_alloc(bp);
5080 if (rc) {
5081 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5082 goto err_out;
5083 }
5084
5085 rc = bnxt_hwrm_ring_grp_alloc(bp);
5086 if (rc) {
5087 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5088 goto err_out;
5089 }
5090
Prashant Sreedharan76595192016-07-18 07:15:22 -04005091 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5092 rx_nr_rings--;
5093
Michael Chanc0c050c2015-10-22 16:01:17 -04005094 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005095 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005096 if (rc) {
5097 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5098 goto err_out;
5099 }
5100
5101 rc = bnxt_setup_vnic(bp, 0);
5102 if (rc)
5103 goto err_out;
5104
5105 if (bp->flags & BNXT_FLAG_RFS) {
5106 rc = bnxt_alloc_rfs_vnics(bp);
5107 if (rc)
5108 goto err_out;
5109 }
5110
5111 if (bp->flags & BNXT_FLAG_TPA) {
5112 rc = bnxt_set_tpa(bp, true);
5113 if (rc)
5114 goto err_out;
5115 }
5116
5117 if (BNXT_VF(bp))
5118 bnxt_update_vf_mac(bp);
5119
5120 /* Filter for default vnic 0 */
5121 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5122 if (rc) {
5123 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5124 goto err_out;
5125 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005126 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005127
Michael Chan7d2837d2016-05-04 16:56:44 -04005128 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005129
Michael Chan17c71ac2016-07-01 18:46:27 -04005130 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005131 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5132
5133 if (bp->dev->flags & IFF_ALLMULTI) {
5134 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5135 vnic->mc_list_count = 0;
5136 } else {
5137 u32 mask = 0;
5138
5139 bnxt_mc_list_updated(bp, &mask);
5140 vnic->rx_mask |= mask;
5141 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005142
Michael Chanb664f002015-12-02 01:54:08 -05005143 rc = bnxt_cfg_rx_mode(bp);
5144 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005145 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005146
5147 rc = bnxt_hwrm_set_coal(bp);
5148 if (rc)
5149 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005150 rc);
5151
5152 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5153 rc = bnxt_setup_nitroa0_vnic(bp);
5154 if (rc)
5155 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5156 rc);
5157 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005158
Michael Chancf6645f2016-06-13 02:25:28 -04005159 if (BNXT_VF(bp)) {
5160 bnxt_hwrm_func_qcfg(bp);
5161 netdev_update_features(bp->dev);
5162 }
5163
Michael Chanc0c050c2015-10-22 16:01:17 -04005164 return 0;
5165
5166err_out:
5167 bnxt_hwrm_resource_free(bp, 0, true);
5168
5169 return rc;
5170}
5171
5172static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5173{
5174 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5175 return 0;
5176}
5177
5178static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5179{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005180 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005181 bnxt_init_rx_rings(bp);
5182 bnxt_init_tx_rings(bp);
5183 bnxt_init_ring_grps(bp, irq_re_init);
5184 bnxt_init_vnics(bp);
5185
5186 return bnxt_init_chip(bp, irq_re_init);
5187}
5188
Michael Chanc0c050c2015-10-22 16:01:17 -04005189static int bnxt_set_real_num_queues(struct bnxt *bp)
5190{
5191 int rc;
5192 struct net_device *dev = bp->dev;
5193
Michael Chan5f449242017-02-06 16:55:40 -05005194 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5195 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005196 if (rc)
5197 return rc;
5198
5199 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5200 if (rc)
5201 return rc;
5202
5203#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005204 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005205 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005206#endif
5207
5208 return rc;
5209}
5210
Michael Chan6e6c5a52016-01-02 23:45:02 -05005211static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5212 bool shared)
5213{
5214 int _rx = *rx, _tx = *tx;
5215
5216 if (shared) {
5217 *rx = min_t(int, _rx, max);
5218 *tx = min_t(int, _tx, max);
5219 } else {
5220 if (max < 2)
5221 return -ENOMEM;
5222
5223 while (_rx + _tx > max) {
5224 if (_rx > _tx && _rx > 1)
5225 _rx--;
5226 else if (_tx > 1)
5227 _tx--;
5228 }
5229 *rx = _rx;
5230 *tx = _tx;
5231 }
5232 return 0;
5233}
5234
Michael Chan78095922016-12-07 00:26:16 -05005235static void bnxt_setup_msix(struct bnxt *bp)
5236{
5237 const int len = sizeof(bp->irq_tbl[0].name);
5238 struct net_device *dev = bp->dev;
5239 int tcs, i;
5240
5241 tcs = netdev_get_num_tc(dev);
5242 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005243 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005244
Michael Chand1e79252017-02-06 16:55:38 -05005245 for (i = 0; i < tcs; i++) {
5246 count = bp->tx_nr_rings_per_tc;
5247 off = i * count;
5248 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005249 }
5250 }
5251
5252 for (i = 0; i < bp->cp_nr_rings; i++) {
5253 char *attr;
5254
5255 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5256 attr = "TxRx";
5257 else if (i < bp->rx_nr_rings)
5258 attr = "rx";
5259 else
5260 attr = "tx";
5261
5262 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5263 i);
5264 bp->irq_tbl[i].handler = bnxt_msix;
5265 }
5266}
5267
5268static void bnxt_setup_inta(struct bnxt *bp)
5269{
5270 const int len = sizeof(bp->irq_tbl[0].name);
5271
5272 if (netdev_get_num_tc(bp->dev))
5273 netdev_reset_tc(bp->dev);
5274
5275 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5276 0);
5277 bp->irq_tbl[0].handler = bnxt_inta;
5278}
5279
5280static int bnxt_setup_int_mode(struct bnxt *bp)
5281{
5282 int rc;
5283
5284 if (bp->flags & BNXT_FLAG_USING_MSIX)
5285 bnxt_setup_msix(bp);
5286 else
5287 bnxt_setup_inta(bp);
5288
5289 rc = bnxt_set_real_num_queues(bp);
5290 return rc;
5291}
5292
Michael Chanb7429952017-01-13 01:32:00 -05005293#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005294static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5295{
5296#if defined(CONFIG_BNXT_SRIOV)
5297 if (BNXT_VF(bp))
5298 return bp->vf.max_rsscos_ctxs;
5299#endif
5300 return bp->pf.max_rsscos_ctxs;
5301}
5302
5303static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5304{
5305#if defined(CONFIG_BNXT_SRIOV)
5306 if (BNXT_VF(bp))
5307 return bp->vf.max_vnics;
5308#endif
5309 return bp->pf.max_vnics;
5310}
Michael Chanb7429952017-01-13 01:32:00 -05005311#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005312
Michael Chane4060d32016-12-07 00:26:19 -05005313unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5314{
5315#if defined(CONFIG_BNXT_SRIOV)
5316 if (BNXT_VF(bp))
5317 return bp->vf.max_stat_ctxs;
5318#endif
5319 return bp->pf.max_stat_ctxs;
5320}
5321
Michael Chana588e452016-12-07 00:26:21 -05005322void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5323{
5324#if defined(CONFIG_BNXT_SRIOV)
5325 if (BNXT_VF(bp))
5326 bp->vf.max_stat_ctxs = max;
5327 else
5328#endif
5329 bp->pf.max_stat_ctxs = max;
5330}
5331
Michael Chane4060d32016-12-07 00:26:19 -05005332unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5333{
5334#if defined(CONFIG_BNXT_SRIOV)
5335 if (BNXT_VF(bp))
5336 return bp->vf.max_cp_rings;
5337#endif
5338 return bp->pf.max_cp_rings;
5339}
5340
Michael Chana588e452016-12-07 00:26:21 -05005341void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5342{
5343#if defined(CONFIG_BNXT_SRIOV)
5344 if (BNXT_VF(bp))
5345 bp->vf.max_cp_rings = max;
5346 else
5347#endif
5348 bp->pf.max_cp_rings = max;
5349}
5350
Michael Chan78095922016-12-07 00:26:16 -05005351static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5352{
5353#if defined(CONFIG_BNXT_SRIOV)
5354 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005355 return min_t(unsigned int, bp->vf.max_irqs,
5356 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005357#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005358 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005359}
5360
Michael Chan33c26572016-12-07 00:26:15 -05005361void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5362{
5363#if defined(CONFIG_BNXT_SRIOV)
5364 if (BNXT_VF(bp))
5365 bp->vf.max_irqs = max_irqs;
5366 else
5367#endif
5368 bp->pf.max_irqs = max_irqs;
5369}
5370
Michael Chan78095922016-12-07 00:26:16 -05005371static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005372{
Michael Chan01657bc2016-01-02 23:45:03 -05005373 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005374 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005375
Michael Chan78095922016-12-07 00:26:16 -05005376 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005377 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5378 if (!msix_ent)
5379 return -ENOMEM;
5380
5381 for (i = 0; i < total_vecs; i++) {
5382 msix_ent[i].entry = i;
5383 msix_ent[i].vector = 0;
5384 }
5385
Michael Chan01657bc2016-01-02 23:45:03 -05005386 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5387 min = 2;
5388
5389 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005390 if (total_vecs < 0) {
5391 rc = -ENODEV;
5392 goto msix_setup_exit;
5393 }
5394
5395 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5396 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005397 for (i = 0; i < total_vecs; i++)
5398 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005399
Michael Chan78095922016-12-07 00:26:16 -05005400 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005401 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005402 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005403 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005404 if (rc)
5405 goto msix_setup_exit;
5406
Michael Chanc0c050c2015-10-22 16:01:17 -04005407 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005408 bp->cp_nr_rings = (min == 1) ?
5409 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5410 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005411
Michael Chanc0c050c2015-10-22 16:01:17 -04005412 } else {
5413 rc = -ENOMEM;
5414 goto msix_setup_exit;
5415 }
5416 bp->flags |= BNXT_FLAG_USING_MSIX;
5417 kfree(msix_ent);
5418 return 0;
5419
5420msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005421 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5422 kfree(bp->irq_tbl);
5423 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005424 pci_disable_msix(bp->pdev);
5425 kfree(msix_ent);
5426 return rc;
5427}
5428
Michael Chan78095922016-12-07 00:26:16 -05005429static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005430{
Michael Chanc0c050c2015-10-22 16:01:17 -04005431 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005432 if (!bp->irq_tbl)
5433 return -ENOMEM;
5434
5435 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005436 bp->rx_nr_rings = 1;
5437 bp->tx_nr_rings = 1;
5438 bp->cp_nr_rings = 1;
5439 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005440 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005441 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005442 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005443}
5444
Michael Chan78095922016-12-07 00:26:16 -05005445static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005446{
5447 int rc = 0;
5448
5449 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005450 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005451
Michael Chan1fa72e22016-04-25 02:30:49 -04005452 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005453 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005454 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005455 }
5456 return rc;
5457}
5458
Michael Chan78095922016-12-07 00:26:16 -05005459static void bnxt_clear_int_mode(struct bnxt *bp)
5460{
5461 if (bp->flags & BNXT_FLAG_USING_MSIX)
5462 pci_disable_msix(bp->pdev);
5463
5464 kfree(bp->irq_tbl);
5465 bp->irq_tbl = NULL;
5466 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5467}
5468
Michael Chanc0c050c2015-10-22 16:01:17 -04005469static void bnxt_free_irq(struct bnxt *bp)
5470{
5471 struct bnxt_irq *irq;
5472 int i;
5473
5474#ifdef CONFIG_RFS_ACCEL
5475 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5476 bp->dev->rx_cpu_rmap = NULL;
5477#endif
5478 if (!bp->irq_tbl)
5479 return;
5480
5481 for (i = 0; i < bp->cp_nr_rings; i++) {
5482 irq = &bp->irq_tbl[i];
5483 if (irq->requested)
5484 free_irq(irq->vector, bp->bnapi[i]);
5485 irq->requested = 0;
5486 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005487}
5488
5489static int bnxt_request_irq(struct bnxt *bp)
5490{
Michael Chanb81a90d2016-01-02 23:45:01 -05005491 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005492 unsigned long flags = 0;
5493#ifdef CONFIG_RFS_ACCEL
5494 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5495#endif
5496
5497 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5498 flags = IRQF_SHARED;
5499
Michael Chanb81a90d2016-01-02 23:45:01 -05005500 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005501 struct bnxt_irq *irq = &bp->irq_tbl[i];
5502#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005503 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005504 rc = irq_cpu_rmap_add(rmap, irq->vector);
5505 if (rc)
5506 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005507 j);
5508 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005509 }
5510#endif
5511 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5512 bp->bnapi[i]);
5513 if (rc)
5514 break;
5515
5516 irq->requested = 1;
5517 }
5518 return rc;
5519}
5520
5521static void bnxt_del_napi(struct bnxt *bp)
5522{
5523 int i;
5524
5525 if (!bp->bnapi)
5526 return;
5527
5528 for (i = 0; i < bp->cp_nr_rings; i++) {
5529 struct bnxt_napi *bnapi = bp->bnapi[i];
5530
5531 napi_hash_del(&bnapi->napi);
5532 netif_napi_del(&bnapi->napi);
5533 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005534 /* We called napi_hash_del() before netif_napi_del(), we need
5535 * to respect an RCU grace period before freeing napi structures.
5536 */
5537 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005538}
5539
5540static void bnxt_init_napi(struct bnxt *bp)
5541{
5542 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005543 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005544 struct bnxt_napi *bnapi;
5545
5546 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005547 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5548 cp_nr_rings--;
5549 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005550 bnapi = bp->bnapi[i];
5551 netif_napi_add(bp->dev, &bnapi->napi,
5552 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005553 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005554 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5555 bnapi = bp->bnapi[cp_nr_rings];
5556 netif_napi_add(bp->dev, &bnapi->napi,
5557 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005558 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005559 } else {
5560 bnapi = bp->bnapi[0];
5561 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005562 }
5563}
5564
5565static void bnxt_disable_napi(struct bnxt *bp)
5566{
5567 int i;
5568
5569 if (!bp->bnapi)
5570 return;
5571
Michael Chanb356a2e2016-12-29 12:13:31 -05005572 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005573 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005574}
5575
5576static void bnxt_enable_napi(struct bnxt *bp)
5577{
5578 int i;
5579
5580 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005581 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005582 napi_enable(&bp->bnapi[i]->napi);
5583 }
5584}
5585
Michael Chan7df4ae92016-12-02 21:17:17 -05005586void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005587{
5588 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005589 struct bnxt_tx_ring_info *txr;
5590 struct netdev_queue *txq;
5591
Michael Chanb6ab4b02016-01-02 23:44:59 -05005592 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005593 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005594 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005595 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005596 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005597 }
5598 }
5599 /* Stop all TX queues */
5600 netif_tx_disable(bp->dev);
5601 netif_carrier_off(bp->dev);
5602}
5603
Michael Chan7df4ae92016-12-02 21:17:17 -05005604void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005605{
5606 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005607 struct bnxt_tx_ring_info *txr;
5608 struct netdev_queue *txq;
5609
5610 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005611 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005612 txq = netdev_get_tx_queue(bp->dev, i);
5613 txr->dev_state = 0;
5614 }
5615 netif_tx_wake_all_queues(bp->dev);
5616 if (bp->link_info.link_up)
5617 netif_carrier_on(bp->dev);
5618}
5619
5620static void bnxt_report_link(struct bnxt *bp)
5621{
5622 if (bp->link_info.link_up) {
5623 const char *duplex;
5624 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005625 u32 speed;
5626 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005627
5628 netif_carrier_on(bp->dev);
5629 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5630 duplex = "full";
5631 else
5632 duplex = "half";
5633 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5634 flow_ctrl = "ON - receive & transmit";
5635 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5636 flow_ctrl = "ON - transmit";
5637 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5638 flow_ctrl = "ON - receive";
5639 else
5640 flow_ctrl = "none";
5641 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005642 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005643 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005644 if (bp->flags & BNXT_FLAG_EEE_CAP)
5645 netdev_info(bp->dev, "EEE is %s\n",
5646 bp->eee.eee_active ? "active" :
5647 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005648 fec = bp->link_info.fec_cfg;
5649 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5650 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5651 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5652 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5653 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005654 } else {
5655 netif_carrier_off(bp->dev);
5656 netdev_err(bp->dev, "NIC Link is Down\n");
5657 }
5658}
5659
Michael Chan170ce012016-04-05 14:08:57 -04005660static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5661{
5662 int rc = 0;
5663 struct hwrm_port_phy_qcaps_input req = {0};
5664 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005665 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005666
5667 if (bp->hwrm_spec_code < 0x10201)
5668 return 0;
5669
5670 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5671
5672 mutex_lock(&bp->hwrm_cmd_lock);
5673 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5674 if (rc)
5675 goto hwrm_phy_qcaps_exit;
5676
Michael Chanacb20052017-07-24 12:34:20 -04005677 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005678 struct ethtool_eee *eee = &bp->eee;
5679 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5680
5681 bp->flags |= BNXT_FLAG_EEE_CAP;
5682 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5683 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5684 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5685 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5686 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5687 }
Michael Chan520ad892017-03-08 18:44:35 -05005688 if (resp->supported_speeds_auto_mode)
5689 link_info->support_auto_speeds =
5690 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005691
5692hwrm_phy_qcaps_exit:
5693 mutex_unlock(&bp->hwrm_cmd_lock);
5694 return rc;
5695}
5696
Michael Chanc0c050c2015-10-22 16:01:17 -04005697static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5698{
5699 int rc = 0;
5700 struct bnxt_link_info *link_info = &bp->link_info;
5701 struct hwrm_port_phy_qcfg_input req = {0};
5702 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5703 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005704 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005705
5706 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5707
5708 mutex_lock(&bp->hwrm_cmd_lock);
5709 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5710 if (rc) {
5711 mutex_unlock(&bp->hwrm_cmd_lock);
5712 return rc;
5713 }
5714
5715 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5716 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005717 link_info->duplex = resp->duplex_cfg;
5718 if (bp->hwrm_spec_code >= 0x10800)
5719 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005720 link_info->pause = resp->pause;
5721 link_info->auto_mode = resp->auto_mode;
5722 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005723 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005724 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005725 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005726 if (link_info->phy_link_status == BNXT_LINK_LINK)
5727 link_info->link_speed = le16_to_cpu(resp->link_speed);
5728 else
5729 link_info->link_speed = 0;
5730 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005731 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5732 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005733 link_info->lp_auto_link_speeds =
5734 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005735 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5736 link_info->phy_ver[0] = resp->phy_maj;
5737 link_info->phy_ver[1] = resp->phy_min;
5738 link_info->phy_ver[2] = resp->phy_bld;
5739 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005740 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005741 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005742 link_info->phy_addr = resp->eee_config_phy_addr &
5743 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005744 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005745
Michael Chan170ce012016-04-05 14:08:57 -04005746 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5747 struct ethtool_eee *eee = &bp->eee;
5748 u16 fw_speeds;
5749
5750 eee->eee_active = 0;
5751 if (resp->eee_config_phy_addr &
5752 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5753 eee->eee_active = 1;
5754 fw_speeds = le16_to_cpu(
5755 resp->link_partner_adv_eee_link_speed_mask);
5756 eee->lp_advertised =
5757 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5758 }
5759
5760 /* Pull initial EEE config */
5761 if (!chng_link_state) {
5762 if (resp->eee_config_phy_addr &
5763 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5764 eee->eee_enabled = 1;
5765
5766 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5767 eee->advertised =
5768 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5769
5770 if (resp->eee_config_phy_addr &
5771 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5772 __le32 tmr;
5773
5774 eee->tx_lpi_enabled = 1;
5775 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5776 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5777 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5778 }
5779 }
5780 }
Michael Chane70c7522017-02-12 19:18:16 -05005781
5782 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5783 if (bp->hwrm_spec_code >= 0x10504)
5784 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5785
Michael Chanc0c050c2015-10-22 16:01:17 -04005786 /* TODO: need to add more logic to report VF link */
5787 if (chng_link_state) {
5788 if (link_info->phy_link_status == BNXT_LINK_LINK)
5789 link_info->link_up = 1;
5790 else
5791 link_info->link_up = 0;
5792 if (link_up != link_info->link_up)
5793 bnxt_report_link(bp);
5794 } else {
5795 /* alwasy link down if not require to update link state */
5796 link_info->link_up = 0;
5797 }
5798 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005799
5800 diff = link_info->support_auto_speeds ^ link_info->advertising;
5801 if ((link_info->support_auto_speeds | diff) !=
5802 link_info->support_auto_speeds) {
5803 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005804 * update the advertisement settings. Caller holds RTNL
5805 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005806 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005807 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005808 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005809 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005810 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005811 return 0;
5812}
5813
Michael Chan10289be2016-05-15 03:04:49 -04005814static void bnxt_get_port_module_status(struct bnxt *bp)
5815{
5816 struct bnxt_link_info *link_info = &bp->link_info;
5817 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5818 u8 module_status;
5819
5820 if (bnxt_update_link(bp, true))
5821 return;
5822
5823 module_status = link_info->module_status;
5824 switch (module_status) {
5825 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5826 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5827 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5828 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5829 bp->pf.port_id);
5830 if (bp->hwrm_spec_code >= 0x10201) {
5831 netdev_warn(bp->dev, "Module part number %s\n",
5832 resp->phy_vendor_partnumber);
5833 }
5834 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5835 netdev_warn(bp->dev, "TX is disabled\n");
5836 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5837 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5838 }
5839}
5840
Michael Chanc0c050c2015-10-22 16:01:17 -04005841static void
5842bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5843{
5844 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005845 if (bp->hwrm_spec_code >= 0x10201)
5846 req->auto_pause =
5847 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005848 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5849 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5850 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005851 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005852 req->enables |=
5853 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5854 } else {
5855 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5856 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5857 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5858 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5859 req->enables |=
5860 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005861 if (bp->hwrm_spec_code >= 0x10201) {
5862 req->auto_pause = req->force_pause;
5863 req->enables |= cpu_to_le32(
5864 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5865 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005866 }
5867}
5868
5869static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5870 struct hwrm_port_phy_cfg_input *req)
5871{
5872 u8 autoneg = bp->link_info.autoneg;
5873 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005874 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005875
5876 if (autoneg & BNXT_AUTONEG_SPEED) {
5877 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005878 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005879
5880 req->enables |= cpu_to_le32(
5881 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5882 req->auto_link_speed_mask = cpu_to_le16(advertising);
5883
5884 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5885 req->flags |=
5886 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5887 } else {
5888 req->force_link_speed = cpu_to_le16(fw_link_speed);
5889 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5890 }
5891
Michael Chanc0c050c2015-10-22 16:01:17 -04005892 /* tell chimp that the setting takes effect immediately */
5893 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5894}
5895
5896int bnxt_hwrm_set_pause(struct bnxt *bp)
5897{
5898 struct hwrm_port_phy_cfg_input req = {0};
5899 int rc;
5900
5901 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5902 bnxt_hwrm_set_pause_common(bp, &req);
5903
5904 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5905 bp->link_info.force_link_chng)
5906 bnxt_hwrm_set_link_common(bp, &req);
5907
5908 mutex_lock(&bp->hwrm_cmd_lock);
5909 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5910 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5911 /* since changing of pause setting doesn't trigger any link
5912 * change event, the driver needs to update the current pause
5913 * result upon successfully return of the phy_cfg command
5914 */
5915 bp->link_info.pause =
5916 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5917 bp->link_info.auto_pause_setting = 0;
5918 if (!bp->link_info.force_link_chng)
5919 bnxt_report_link(bp);
5920 }
5921 bp->link_info.force_link_chng = false;
5922 mutex_unlock(&bp->hwrm_cmd_lock);
5923 return rc;
5924}
5925
Michael Chan939f7f02016-04-05 14:08:58 -04005926static void bnxt_hwrm_set_eee(struct bnxt *bp,
5927 struct hwrm_port_phy_cfg_input *req)
5928{
5929 struct ethtool_eee *eee = &bp->eee;
5930
5931 if (eee->eee_enabled) {
5932 u16 eee_speeds;
5933 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5934
5935 if (eee->tx_lpi_enabled)
5936 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5937 else
5938 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5939
5940 req->flags |= cpu_to_le32(flags);
5941 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5942 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5943 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5944 } else {
5945 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5946 }
5947}
5948
5949int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005950{
5951 struct hwrm_port_phy_cfg_input req = {0};
5952
5953 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5954 if (set_pause)
5955 bnxt_hwrm_set_pause_common(bp, &req);
5956
5957 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005958
5959 if (set_eee)
5960 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005961 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5962}
5963
Michael Chan33f7d552016-04-11 04:11:12 -04005964static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5965{
5966 struct hwrm_port_phy_cfg_input req = {0};
5967
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005968 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005969 return 0;
5970
5971 if (pci_num_vf(bp->pdev))
5972 return 0;
5973
5974 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005975 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005976 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5977}
5978
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005979static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5980{
5981 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5982 struct hwrm_port_led_qcaps_input req = {0};
5983 struct bnxt_pf_info *pf = &bp->pf;
5984 int rc;
5985
5986 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5987 return 0;
5988
5989 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5990 req.port_id = cpu_to_le16(pf->port_id);
5991 mutex_lock(&bp->hwrm_cmd_lock);
5992 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5993 if (rc) {
5994 mutex_unlock(&bp->hwrm_cmd_lock);
5995 return rc;
5996 }
5997 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5998 int i;
5999
6000 bp->num_leds = resp->num_leds;
6001 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6002 bp->num_leds);
6003 for (i = 0; i < bp->num_leds; i++) {
6004 struct bnxt_led_info *led = &bp->leds[i];
6005 __le16 caps = led->led_state_caps;
6006
6007 if (!led->led_group_id ||
6008 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6009 bp->num_leds = 0;
6010 break;
6011 }
6012 }
6013 }
6014 mutex_unlock(&bp->hwrm_cmd_lock);
6015 return 0;
6016}
6017
Michael Chan5282db62017-04-04 18:14:10 -04006018int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6019{
6020 struct hwrm_wol_filter_alloc_input req = {0};
6021 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6022 int rc;
6023
6024 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6025 req.port_id = cpu_to_le16(bp->pf.port_id);
6026 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6027 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6028 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6029 mutex_lock(&bp->hwrm_cmd_lock);
6030 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6031 if (!rc)
6032 bp->wol_filter_id = resp->wol_filter_id;
6033 mutex_unlock(&bp->hwrm_cmd_lock);
6034 return rc;
6035}
6036
6037int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6038{
6039 struct hwrm_wol_filter_free_input req = {0};
6040 int rc;
6041
6042 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6043 req.port_id = cpu_to_le16(bp->pf.port_id);
6044 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6045 req.wol_filter_id = bp->wol_filter_id;
6046 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6047 return rc;
6048}
6049
Michael Chanc1ef1462017-04-04 18:14:07 -04006050static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6051{
6052 struct hwrm_wol_filter_qcfg_input req = {0};
6053 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6054 u16 next_handle = 0;
6055 int rc;
6056
6057 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6058 req.port_id = cpu_to_le16(bp->pf.port_id);
6059 req.handle = cpu_to_le16(handle);
6060 mutex_lock(&bp->hwrm_cmd_lock);
6061 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6062 if (!rc) {
6063 next_handle = le16_to_cpu(resp->next_handle);
6064 if (next_handle != 0) {
6065 if (resp->wol_type ==
6066 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6067 bp->wol = 1;
6068 bp->wol_filter_id = resp->wol_filter_id;
6069 }
6070 }
6071 }
6072 mutex_unlock(&bp->hwrm_cmd_lock);
6073 return next_handle;
6074}
6075
6076static void bnxt_get_wol_settings(struct bnxt *bp)
6077{
6078 u16 handle = 0;
6079
6080 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6081 return;
6082
6083 do {
6084 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6085 } while (handle && handle != 0xffff);
6086}
6087
Michael Chan939f7f02016-04-05 14:08:58 -04006088static bool bnxt_eee_config_ok(struct bnxt *bp)
6089{
6090 struct ethtool_eee *eee = &bp->eee;
6091 struct bnxt_link_info *link_info = &bp->link_info;
6092
6093 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6094 return true;
6095
6096 if (eee->eee_enabled) {
6097 u32 advertising =
6098 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6099
6100 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6101 eee->eee_enabled = 0;
6102 return false;
6103 }
6104 if (eee->advertised & ~advertising) {
6105 eee->advertised = advertising & eee->supported;
6106 return false;
6107 }
6108 }
6109 return true;
6110}
6111
Michael Chanc0c050c2015-10-22 16:01:17 -04006112static int bnxt_update_phy_setting(struct bnxt *bp)
6113{
6114 int rc;
6115 bool update_link = false;
6116 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006117 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006118 struct bnxt_link_info *link_info = &bp->link_info;
6119
6120 rc = bnxt_update_link(bp, true);
6121 if (rc) {
6122 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6123 rc);
6124 return rc;
6125 }
Michael Chan33dac242017-02-12 19:18:15 -05006126 if (!BNXT_SINGLE_PF(bp))
6127 return 0;
6128
Michael Chanc0c050c2015-10-22 16:01:17 -04006129 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006130 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6131 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006132 update_pause = true;
6133 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6134 link_info->force_pause_setting != link_info->req_flow_ctrl)
6135 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006136 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6137 if (BNXT_AUTO_MODE(link_info->auto_mode))
6138 update_link = true;
6139 if (link_info->req_link_speed != link_info->force_link_speed)
6140 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006141 if (link_info->req_duplex != link_info->duplex_setting)
6142 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006143 } else {
6144 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6145 update_link = true;
6146 if (link_info->advertising != link_info->auto_link_speeds)
6147 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006148 }
6149
Michael Chan16d663a2016-11-16 21:13:07 -05006150 /* The last close may have shutdown the link, so need to call
6151 * PHY_CFG to bring it back up.
6152 */
6153 if (!netif_carrier_ok(bp->dev))
6154 update_link = true;
6155
Michael Chan939f7f02016-04-05 14:08:58 -04006156 if (!bnxt_eee_config_ok(bp))
6157 update_eee = true;
6158
Michael Chanc0c050c2015-10-22 16:01:17 -04006159 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006160 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006161 else if (update_pause)
6162 rc = bnxt_hwrm_set_pause(bp);
6163 if (rc) {
6164 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6165 rc);
6166 return rc;
6167 }
6168
6169 return rc;
6170}
6171
Jeffrey Huang11809492015-11-05 16:25:49 -05006172/* Common routine to pre-map certain register block to different GRC window.
6173 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6174 * in PF and 3 windows in VF that can be customized to map in different
6175 * register blocks.
6176 */
6177static void bnxt_preset_reg_win(struct bnxt *bp)
6178{
6179 if (BNXT_PF(bp)) {
6180 /* CAG registers map to GRC window #4 */
6181 writel(BNXT_CAG_REG_BASE,
6182 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6183 }
6184}
6185
Michael Chanc0c050c2015-10-22 16:01:17 -04006186static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6187{
6188 int rc = 0;
6189
Jeffrey Huang11809492015-11-05 16:25:49 -05006190 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006191 netif_carrier_off(bp->dev);
6192 if (irq_re_init) {
6193 rc = bnxt_setup_int_mode(bp);
6194 if (rc) {
6195 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6196 rc);
6197 return rc;
6198 }
6199 }
6200 if ((bp->flags & BNXT_FLAG_RFS) &&
6201 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6202 /* disable RFS if falling back to INTA */
6203 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6204 bp->flags &= ~BNXT_FLAG_RFS;
6205 }
6206
6207 rc = bnxt_alloc_mem(bp, irq_re_init);
6208 if (rc) {
6209 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6210 goto open_err_free_mem;
6211 }
6212
6213 if (irq_re_init) {
6214 bnxt_init_napi(bp);
6215 rc = bnxt_request_irq(bp);
6216 if (rc) {
6217 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6218 goto open_err;
6219 }
6220 }
6221
6222 bnxt_enable_napi(bp);
6223
6224 rc = bnxt_init_nic(bp, irq_re_init);
6225 if (rc) {
6226 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6227 goto open_err;
6228 }
6229
6230 if (link_re_init) {
6231 rc = bnxt_update_phy_setting(bp);
6232 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006233 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006234 }
6235
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006236 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006237 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006238
Michael Chancaefe522015-12-09 19:35:42 -05006239 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006240 bnxt_enable_int(bp);
6241 /* Enable TX queues */
6242 bnxt_tx_enable(bp);
6243 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006244 /* Poll link status and check for SFP+ module status */
6245 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006246
6247 return 0;
6248
6249open_err:
6250 bnxt_disable_napi(bp);
6251 bnxt_del_napi(bp);
6252
6253open_err_free_mem:
6254 bnxt_free_skbs(bp);
6255 bnxt_free_irq(bp);
6256 bnxt_free_mem(bp, true);
6257 return rc;
6258}
6259
6260/* rtnl_lock held */
6261int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6262{
6263 int rc = 0;
6264
6265 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6266 if (rc) {
6267 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6268 dev_close(bp->dev);
6269 }
6270 return rc;
6271}
6272
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006273/* rtnl_lock held, open the NIC half way by allocating all resources, but
6274 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6275 * self tests.
6276 */
6277int bnxt_half_open_nic(struct bnxt *bp)
6278{
6279 int rc = 0;
6280
6281 rc = bnxt_alloc_mem(bp, false);
6282 if (rc) {
6283 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6284 goto half_open_err;
6285 }
6286 rc = bnxt_init_nic(bp, false);
6287 if (rc) {
6288 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6289 goto half_open_err;
6290 }
6291 return 0;
6292
6293half_open_err:
6294 bnxt_free_skbs(bp);
6295 bnxt_free_mem(bp, false);
6296 dev_close(bp->dev);
6297 return rc;
6298}
6299
6300/* rtnl_lock held, this call can only be made after a previous successful
6301 * call to bnxt_half_open_nic().
6302 */
6303void bnxt_half_close_nic(struct bnxt *bp)
6304{
6305 bnxt_hwrm_resource_free(bp, false, false);
6306 bnxt_free_skbs(bp);
6307 bnxt_free_mem(bp, false);
6308}
6309
Michael Chanc0c050c2015-10-22 16:01:17 -04006310static int bnxt_open(struct net_device *dev)
6311{
6312 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006313
Michael Chanc0c050c2015-10-22 16:01:17 -04006314 return __bnxt_open_nic(bp, true, true);
6315}
6316
Michael Chanf9b76eb2017-07-11 13:05:34 -04006317static bool bnxt_drv_busy(struct bnxt *bp)
6318{
6319 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6320 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6321}
6322
Michael Chanc0c050c2015-10-22 16:01:17 -04006323int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6324{
6325 int rc = 0;
6326
6327#ifdef CONFIG_BNXT_SRIOV
6328 if (bp->sriov_cfg) {
6329 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6330 !bp->sriov_cfg,
6331 BNXT_SRIOV_CFG_WAIT_TMO);
6332 if (rc)
6333 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6334 }
6335#endif
6336 /* Change device state to avoid TX queue wake up's */
6337 bnxt_tx_disable(bp);
6338
Michael Chancaefe522015-12-09 19:35:42 -05006339 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006340 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006341 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006342 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006343
Michael Chan9d8bc092016-12-29 12:13:33 -05006344 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006345 bnxt_shutdown_nic(bp, irq_re_init);
6346
6347 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6348
6349 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006350 del_timer_sync(&bp->timer);
6351 bnxt_free_skbs(bp);
6352
6353 if (irq_re_init) {
6354 bnxt_free_irq(bp);
6355 bnxt_del_napi(bp);
6356 }
6357 bnxt_free_mem(bp, irq_re_init);
6358 return rc;
6359}
6360
6361static int bnxt_close(struct net_device *dev)
6362{
6363 struct bnxt *bp = netdev_priv(dev);
6364
6365 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006366 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006367 return 0;
6368}
6369
6370/* rtnl_lock held */
6371static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6372{
6373 switch (cmd) {
6374 case SIOCGMIIPHY:
6375 /* fallthru */
6376 case SIOCGMIIREG: {
6377 if (!netif_running(dev))
6378 return -EAGAIN;
6379
6380 return 0;
6381 }
6382
6383 case SIOCSMIIREG:
6384 if (!netif_running(dev))
6385 return -EAGAIN;
6386
6387 return 0;
6388
6389 default:
6390 /* do nothing */
6391 break;
6392 }
6393 return -EOPNOTSUPP;
6394}
6395
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006396static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006397bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6398{
6399 u32 i;
6400 struct bnxt *bp = netdev_priv(dev);
6401
Michael Chanf9b76eb2017-07-11 13:05:34 -04006402 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6403 /* Make sure bnxt_close_nic() sees that we are reading stats before
6404 * we check the BNXT_STATE_OPEN flag.
6405 */
6406 smp_mb__after_atomic();
6407 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6408 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006409 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006410 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006411
6412 /* TODO check if we need to synchronize with bnxt_close path */
6413 for (i = 0; i < bp->cp_nr_rings; i++) {
6414 struct bnxt_napi *bnapi = bp->bnapi[i];
6415 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6416 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6417
6418 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6419 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6420 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6421
6422 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6423 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6424 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6425
6426 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6427 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6428 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6429
6430 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6431 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6432 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6433
6434 stats->rx_missed_errors +=
6435 le64_to_cpu(hw_stats->rx_discard_pkts);
6436
6437 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6438
Michael Chanc0c050c2015-10-22 16:01:17 -04006439 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6440 }
6441
Michael Chan9947f832016-03-07 15:38:46 -05006442 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6443 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6444 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6445
6446 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6447 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6448 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6449 le64_to_cpu(rx->rx_ovrsz_frames) +
6450 le64_to_cpu(rx->rx_runt_frames);
6451 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6452 le64_to_cpu(rx->rx_jbr_frames);
6453 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6454 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6455 stats->tx_errors = le64_to_cpu(tx->tx_err);
6456 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006457 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006458}
6459
6460static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6461{
6462 struct net_device *dev = bp->dev;
6463 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6464 struct netdev_hw_addr *ha;
6465 u8 *haddr;
6466 int mc_count = 0;
6467 bool update = false;
6468 int off = 0;
6469
6470 netdev_for_each_mc_addr(ha, dev) {
6471 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6472 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6473 vnic->mc_list_count = 0;
6474 return false;
6475 }
6476 haddr = ha->addr;
6477 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6478 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6479 update = true;
6480 }
6481 off += ETH_ALEN;
6482 mc_count++;
6483 }
6484 if (mc_count)
6485 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6486
6487 if (mc_count != vnic->mc_list_count) {
6488 vnic->mc_list_count = mc_count;
6489 update = true;
6490 }
6491 return update;
6492}
6493
6494static bool bnxt_uc_list_updated(struct bnxt *bp)
6495{
6496 struct net_device *dev = bp->dev;
6497 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6498 struct netdev_hw_addr *ha;
6499 int off = 0;
6500
6501 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6502 return true;
6503
6504 netdev_for_each_uc_addr(ha, dev) {
6505 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6506 return true;
6507
6508 off += ETH_ALEN;
6509 }
6510 return false;
6511}
6512
6513static void bnxt_set_rx_mode(struct net_device *dev)
6514{
6515 struct bnxt *bp = netdev_priv(dev);
6516 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6517 u32 mask = vnic->rx_mask;
6518 bool mc_update = false;
6519 bool uc_update;
6520
6521 if (!netif_running(dev))
6522 return;
6523
6524 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6525 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6526 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6527
Michael Chan17c71ac2016-07-01 18:46:27 -04006528 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006529 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6530
6531 uc_update = bnxt_uc_list_updated(bp);
6532
6533 if (dev->flags & IFF_ALLMULTI) {
6534 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6535 vnic->mc_list_count = 0;
6536 } else {
6537 mc_update = bnxt_mc_list_updated(bp, &mask);
6538 }
6539
6540 if (mask != vnic->rx_mask || uc_update || mc_update) {
6541 vnic->rx_mask = mask;
6542
6543 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6544 schedule_work(&bp->sp_task);
6545 }
6546}
6547
Michael Chanb664f002015-12-02 01:54:08 -05006548static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006549{
6550 struct net_device *dev = bp->dev;
6551 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6552 struct netdev_hw_addr *ha;
6553 int i, off = 0, rc;
6554 bool uc_update;
6555
6556 netif_addr_lock_bh(dev);
6557 uc_update = bnxt_uc_list_updated(bp);
6558 netif_addr_unlock_bh(dev);
6559
6560 if (!uc_update)
6561 goto skip_uc;
6562
6563 mutex_lock(&bp->hwrm_cmd_lock);
6564 for (i = 1; i < vnic->uc_filter_count; i++) {
6565 struct hwrm_cfa_l2_filter_free_input req = {0};
6566
6567 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6568 -1);
6569
6570 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6571
6572 rc = _hwrm_send_message(bp, &req, sizeof(req),
6573 HWRM_CMD_TIMEOUT);
6574 }
6575 mutex_unlock(&bp->hwrm_cmd_lock);
6576
6577 vnic->uc_filter_count = 1;
6578
6579 netif_addr_lock_bh(dev);
6580 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6581 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6582 } else {
6583 netdev_for_each_uc_addr(ha, dev) {
6584 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6585 off += ETH_ALEN;
6586 vnic->uc_filter_count++;
6587 }
6588 }
6589 netif_addr_unlock_bh(dev);
6590
6591 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6592 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6593 if (rc) {
6594 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6595 rc);
6596 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006597 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006598 }
6599 }
6600
6601skip_uc:
6602 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6603 if (rc)
6604 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6605 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006606
6607 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006608}
6609
Michael Chan8079e8f2016-12-29 12:13:37 -05006610/* If the chip and firmware supports RFS */
6611static bool bnxt_rfs_supported(struct bnxt *bp)
6612{
6613 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6614 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006615 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6616 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006617 return false;
6618}
6619
6620/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006621static bool bnxt_rfs_capable(struct bnxt *bp)
6622{
6623#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006624 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006625
Michael Chan964fd482017-02-12 19:18:13 -05006626 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006627 return false;
6628
6629 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006630 max_vnics = bnxt_get_max_func_vnics(bp);
6631 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006632
6633 /* RSS contexts not a limiting factor */
6634 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6635 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006636 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006637 netdev_warn(bp->dev,
6638 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006639 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006640 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006641 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006642
6643 return true;
6644#else
6645 return false;
6646#endif
6647}
6648
Michael Chanc0c050c2015-10-22 16:01:17 -04006649static netdev_features_t bnxt_fix_features(struct net_device *dev,
6650 netdev_features_t features)
6651{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006652 struct bnxt *bp = netdev_priv(dev);
6653
Vasundhara Volama2304902016-07-25 12:33:36 -04006654 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006655 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006656
6657 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6658 * turned on or off together.
6659 */
6660 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6661 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6662 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6663 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6664 NETIF_F_HW_VLAN_STAG_RX);
6665 else
6666 features |= NETIF_F_HW_VLAN_CTAG_RX |
6667 NETIF_F_HW_VLAN_STAG_RX;
6668 }
Michael Chancf6645f2016-06-13 02:25:28 -04006669#ifdef CONFIG_BNXT_SRIOV
6670 if (BNXT_VF(bp)) {
6671 if (bp->vf.vlan) {
6672 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6673 NETIF_F_HW_VLAN_STAG_RX);
6674 }
6675 }
6676#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006677 return features;
6678}
6679
6680static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6681{
6682 struct bnxt *bp = netdev_priv(dev);
6683 u32 flags = bp->flags;
6684 u32 changes;
6685 int rc = 0;
6686 bool re_init = false;
6687 bool update_tpa = false;
6688
6689 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006690 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006691 flags |= BNXT_FLAG_GRO;
6692 if (features & NETIF_F_LRO)
6693 flags |= BNXT_FLAG_LRO;
6694
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006695 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6696 flags &= ~BNXT_FLAG_TPA;
6697
Michael Chanc0c050c2015-10-22 16:01:17 -04006698 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6699 flags |= BNXT_FLAG_STRIP_VLAN;
6700
6701 if (features & NETIF_F_NTUPLE)
6702 flags |= BNXT_FLAG_RFS;
6703
6704 changes = flags ^ bp->flags;
6705 if (changes & BNXT_FLAG_TPA) {
6706 update_tpa = true;
6707 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6708 (flags & BNXT_FLAG_TPA) == 0)
6709 re_init = true;
6710 }
6711
6712 if (changes & ~BNXT_FLAG_TPA)
6713 re_init = true;
6714
6715 if (flags != bp->flags) {
6716 u32 old_flags = bp->flags;
6717
6718 bp->flags = flags;
6719
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006720 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006721 if (update_tpa)
6722 bnxt_set_ring_params(bp);
6723 return rc;
6724 }
6725
6726 if (re_init) {
6727 bnxt_close_nic(bp, false, false);
6728 if (update_tpa)
6729 bnxt_set_ring_params(bp);
6730
6731 return bnxt_open_nic(bp, false, false);
6732 }
6733 if (update_tpa) {
6734 rc = bnxt_set_tpa(bp,
6735 (flags & BNXT_FLAG_TPA) ?
6736 true : false);
6737 if (rc)
6738 bp->flags = old_flags;
6739 }
6740 }
6741 return rc;
6742}
6743
Michael Chan9f554592016-01-02 23:44:58 -05006744static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6745{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006746 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006747 int i = bnapi->index;
6748
Michael Chan3b2b7d92016-01-02 23:45:00 -05006749 if (!txr)
6750 return;
6751
Michael Chan9f554592016-01-02 23:44:58 -05006752 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6753 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6754 txr->tx_cons);
6755}
6756
6757static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6758{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006759 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006760 int i = bnapi->index;
6761
Michael Chan3b2b7d92016-01-02 23:45:00 -05006762 if (!rxr)
6763 return;
6764
Michael Chan9f554592016-01-02 23:44:58 -05006765 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",
6766 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6767 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6768 rxr->rx_sw_agg_prod);
6769}
6770
6771static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6772{
6773 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6774 int i = bnapi->index;
6775
6776 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6777 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6778}
6779
Michael Chanc0c050c2015-10-22 16:01:17 -04006780static void bnxt_dbg_dump_states(struct bnxt *bp)
6781{
6782 int i;
6783 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006784
6785 for (i = 0; i < bp->cp_nr_rings; i++) {
6786 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006787 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006788 bnxt_dump_tx_sw_state(bnapi);
6789 bnxt_dump_rx_sw_state(bnapi);
6790 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006791 }
6792 }
6793}
6794
Michael Chan6988bd92016-06-13 02:25:29 -04006795static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006796{
Michael Chan6988bd92016-06-13 02:25:29 -04006797 if (!silent)
6798 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006799 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006800 int rc;
6801
6802 if (!silent)
6803 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006804 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006805 rc = bnxt_open_nic(bp, false, false);
6806 if (!silent && !rc)
6807 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006808 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006809}
6810
6811static void bnxt_tx_timeout(struct net_device *dev)
6812{
6813 struct bnxt *bp = netdev_priv(dev);
6814
6815 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6816 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6817 schedule_work(&bp->sp_task);
6818}
6819
6820#ifdef CONFIG_NET_POLL_CONTROLLER
6821static void bnxt_poll_controller(struct net_device *dev)
6822{
6823 struct bnxt *bp = netdev_priv(dev);
6824 int i;
6825
Michael Chan2270bc52017-06-23 14:01:01 -04006826 /* Only process tx rings/combined rings in netpoll mode. */
6827 for (i = 0; i < bp->tx_nr_rings; i++) {
6828 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006829
Michael Chan2270bc52017-06-23 14:01:01 -04006830 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006831 }
6832}
6833#endif
6834
6835static void bnxt_timer(unsigned long data)
6836{
6837 struct bnxt *bp = (struct bnxt *)data;
6838 struct net_device *dev = bp->dev;
6839
6840 if (!netif_running(dev))
6841 return;
6842
6843 if (atomic_read(&bp->intr_sem) != 0)
6844 goto bnxt_restart_timer;
6845
Michael Chan3bdf56c2016-03-07 15:38:45 -05006846 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6847 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6848 schedule_work(&bp->sp_task);
6849 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006850bnxt_restart_timer:
6851 mod_timer(&bp->timer, jiffies + bp->current_interval);
6852}
6853
Michael Chana551ee92017-01-25 02:55:07 -05006854static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006855{
Michael Chana551ee92017-01-25 02:55:07 -05006856 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6857 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006858 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6859 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6860 */
6861 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6862 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006863}
6864
6865static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6866{
Michael Chan6988bd92016-06-13 02:25:29 -04006867 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6868 rtnl_unlock();
6869}
6870
Michael Chana551ee92017-01-25 02:55:07 -05006871/* Only called from bnxt_sp_task() */
6872static void bnxt_reset(struct bnxt *bp, bool silent)
6873{
6874 bnxt_rtnl_lock_sp(bp);
6875 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6876 bnxt_reset_task(bp, silent);
6877 bnxt_rtnl_unlock_sp(bp);
6878}
6879
Michael Chanc0c050c2015-10-22 16:01:17 -04006880static void bnxt_cfg_ntp_filters(struct bnxt *);
6881
6882static void bnxt_sp_task(struct work_struct *work)
6883{
6884 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006885
Michael Chan4cebdce2015-12-09 19:35:43 -05006886 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6887 smp_mb__after_atomic();
6888 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6889 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006890 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006891 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006892
6893 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6894 bnxt_cfg_rx_mode(bp);
6895
6896 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6897 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006898 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6899 bnxt_hwrm_exec_fwd_req(bp);
6900 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6901 bnxt_hwrm_tunnel_dst_port_alloc(
6902 bp, bp->vxlan_port,
6903 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6904 }
6905 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6906 bnxt_hwrm_tunnel_dst_port_free(
6907 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6908 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006909 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6910 bnxt_hwrm_tunnel_dst_port_alloc(
6911 bp, bp->nge_port,
6912 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6913 }
6914 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6915 bnxt_hwrm_tunnel_dst_port_free(
6916 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6917 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006918 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6919 bnxt_hwrm_port_qstats(bp);
6920
Michael Chana551ee92017-01-25 02:55:07 -05006921 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6922 * must be the last functions to be called before exiting.
6923 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006924 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6925 int rc = 0;
6926
6927 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6928 &bp->sp_event))
6929 bnxt_hwrm_phy_qcaps(bp);
6930
6931 bnxt_rtnl_lock_sp(bp);
6932 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6933 rc = bnxt_update_link(bp, true);
6934 bnxt_rtnl_unlock_sp(bp);
6935 if (rc)
6936 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6937 rc);
6938 }
Michael Chan90c694b2017-01-25 02:55:09 -05006939 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6940 bnxt_rtnl_lock_sp(bp);
6941 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6942 bnxt_get_port_module_status(bp);
6943 bnxt_rtnl_unlock_sp(bp);
6944 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006945 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6946 bnxt_reset(bp, false);
6947
6948 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6949 bnxt_reset(bp, true);
6950
Michael Chanc0c050c2015-10-22 16:01:17 -04006951 smp_mb__before_atomic();
6952 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6953}
6954
Michael Chand1e79252017-02-06 16:55:38 -05006955/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04006956int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
6957 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006958{
6959 int max_rx, max_tx, tx_sets = 1;
6960 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05006961 int rc;
6962
Michael Chand1e79252017-02-06 16:55:38 -05006963 if (tcs)
6964 tx_sets = tcs;
6965
6966 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6967 if (rc)
6968 return rc;
6969
6970 if (max_rx < rx)
6971 return -ENOMEM;
6972
Michael Chan5f449242017-02-06 16:55:40 -05006973 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006974 if (max_tx < tx_rings_needed)
6975 return -ENOMEM;
6976
6977 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006978 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006979 return -ENOMEM;
6980 return 0;
6981}
6982
Sathya Perla17086392017-02-20 19:25:18 -05006983static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6984{
6985 if (bp->bar2) {
6986 pci_iounmap(pdev, bp->bar2);
6987 bp->bar2 = NULL;
6988 }
6989
6990 if (bp->bar1) {
6991 pci_iounmap(pdev, bp->bar1);
6992 bp->bar1 = NULL;
6993 }
6994
6995 if (bp->bar0) {
6996 pci_iounmap(pdev, bp->bar0);
6997 bp->bar0 = NULL;
6998 }
6999}
7000
7001static void bnxt_cleanup_pci(struct bnxt *bp)
7002{
7003 bnxt_unmap_bars(bp, bp->pdev);
7004 pci_release_regions(bp->pdev);
7005 pci_disable_device(bp->pdev);
7006}
7007
Michael Chanc0c050c2015-10-22 16:01:17 -04007008static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7009{
7010 int rc;
7011 struct bnxt *bp = netdev_priv(dev);
7012
7013 SET_NETDEV_DEV(dev, &pdev->dev);
7014
7015 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7016 rc = pci_enable_device(pdev);
7017 if (rc) {
7018 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7019 goto init_err;
7020 }
7021
7022 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7023 dev_err(&pdev->dev,
7024 "Cannot find PCI device base address, aborting\n");
7025 rc = -ENODEV;
7026 goto init_err_disable;
7027 }
7028
7029 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7030 if (rc) {
7031 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7032 goto init_err_disable;
7033 }
7034
7035 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7036 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7037 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7038 goto init_err_disable;
7039 }
7040
7041 pci_set_master(pdev);
7042
7043 bp->dev = dev;
7044 bp->pdev = pdev;
7045
7046 bp->bar0 = pci_ioremap_bar(pdev, 0);
7047 if (!bp->bar0) {
7048 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7049 rc = -ENOMEM;
7050 goto init_err_release;
7051 }
7052
7053 bp->bar1 = pci_ioremap_bar(pdev, 2);
7054 if (!bp->bar1) {
7055 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7056 rc = -ENOMEM;
7057 goto init_err_release;
7058 }
7059
7060 bp->bar2 = pci_ioremap_bar(pdev, 4);
7061 if (!bp->bar2) {
7062 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7063 rc = -ENOMEM;
7064 goto init_err_release;
7065 }
7066
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007067 pci_enable_pcie_error_reporting(pdev);
7068
Michael Chanc0c050c2015-10-22 16:01:17 -04007069 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7070
7071 spin_lock_init(&bp->ntp_fltr_lock);
7072
7073 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7074 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7075
Michael Chandfb5b892016-02-26 04:00:01 -05007076 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007077 bp->rx_coal_ticks = 12;
7078 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007079 bp->rx_coal_ticks_irq = 1;
7080 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007081
Michael Chandfc9c942016-02-26 04:00:03 -05007082 bp->tx_coal_ticks = 25;
7083 bp->tx_coal_bufs = 30;
7084 bp->tx_coal_ticks_irq = 2;
7085 bp->tx_coal_bufs_irq = 2;
7086
Michael Chan51f30782016-07-01 18:46:29 -04007087 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7088
Michael Chanc0c050c2015-10-22 16:01:17 -04007089 init_timer(&bp->timer);
7090 bp->timer.data = (unsigned long)bp;
7091 bp->timer.function = bnxt_timer;
7092 bp->current_interval = BNXT_TIMER_INTERVAL;
7093
Michael Chancaefe522015-12-09 19:35:42 -05007094 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007095 return 0;
7096
7097init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007098 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007099 pci_release_regions(pdev);
7100
7101init_err_disable:
7102 pci_disable_device(pdev);
7103
7104init_err:
7105 return rc;
7106}
7107
7108/* rtnl_lock held */
7109static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7110{
7111 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007112 struct bnxt *bp = netdev_priv(dev);
7113 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007114
7115 if (!is_valid_ether_addr(addr->sa_data))
7116 return -EADDRNOTAVAIL;
7117
Michael Chan84c33dd2016-04-11 04:11:13 -04007118 rc = bnxt_approve_mac(bp, addr->sa_data);
7119 if (rc)
7120 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007121
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007122 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7123 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007124
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007125 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7126 if (netif_running(dev)) {
7127 bnxt_close_nic(bp, false, false);
7128 rc = bnxt_open_nic(bp, false, false);
7129 }
7130
7131 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007132}
7133
7134/* rtnl_lock held */
7135static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7136{
7137 struct bnxt *bp = netdev_priv(dev);
7138
Michael Chanc0c050c2015-10-22 16:01:17 -04007139 if (netif_running(dev))
7140 bnxt_close_nic(bp, false, false);
7141
7142 dev->mtu = new_mtu;
7143 bnxt_set_ring_params(bp);
7144
7145 if (netif_running(dev))
7146 return bnxt_open_nic(bp, false, false);
7147
7148 return 0;
7149}
7150
Michael Chanc5e3deb2016-12-02 21:17:15 -05007151int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007152{
7153 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007154 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007155 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007156
Michael Chanc0c050c2015-10-22 16:01:17 -04007157 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007158 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007159 tc, bp->max_tc);
7160 return -EINVAL;
7161 }
7162
7163 if (netdev_get_num_tc(dev) == tc)
7164 return 0;
7165
Michael Chan3ffb6a32016-11-11 00:11:42 -05007166 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7167 sh = true;
7168
Michael Chan5f449242017-02-06 16:55:40 -05007169 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007170 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007171 if (rc)
7172 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007173
7174 /* Needs to close the device and do hw resource re-allocations */
7175 if (netif_running(bp->dev))
7176 bnxt_close_nic(bp, true, false);
7177
7178 if (tc) {
7179 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7180 netdev_set_num_tc(dev, tc);
7181 } else {
7182 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7183 netdev_reset_tc(dev);
7184 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007185 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7186 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007187 bp->num_stat_ctxs = bp->cp_nr_rings;
7188
7189 if (netif_running(bp->dev))
7190 return bnxt_open_nic(bp, true, false);
7191
7192 return 0;
7193}
7194
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007195static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7196 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007197{
7198 if (ntc->type != TC_SETUP_MQPRIO)
7199 return -EINVAL;
7200
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007201 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7202
7203 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007204}
7205
Michael Chanc0c050c2015-10-22 16:01:17 -04007206#ifdef CONFIG_RFS_ACCEL
7207static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7208 struct bnxt_ntuple_filter *f2)
7209{
7210 struct flow_keys *keys1 = &f1->fkeys;
7211 struct flow_keys *keys2 = &f2->fkeys;
7212
7213 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7214 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7215 keys1->ports.ports == keys2->ports.ports &&
7216 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7217 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007218 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007219 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7220 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007221 return true;
7222
7223 return false;
7224}
7225
7226static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7227 u16 rxq_index, u32 flow_id)
7228{
7229 struct bnxt *bp = netdev_priv(dev);
7230 struct bnxt_ntuple_filter *fltr, *new_fltr;
7231 struct flow_keys *fkeys;
7232 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007233 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007234 struct hlist_head *head;
7235
Michael Chana54c4d72016-07-25 12:33:35 -04007236 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7237 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7238 int off = 0, j;
7239
7240 netif_addr_lock_bh(dev);
7241 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7242 if (ether_addr_equal(eth->h_dest,
7243 vnic->uc_list + off)) {
7244 l2_idx = j + 1;
7245 break;
7246 }
7247 }
7248 netif_addr_unlock_bh(dev);
7249 if (!l2_idx)
7250 return -EINVAL;
7251 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007252 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7253 if (!new_fltr)
7254 return -ENOMEM;
7255
7256 fkeys = &new_fltr->fkeys;
7257 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7258 rc = -EPROTONOSUPPORT;
7259 goto err_free;
7260 }
7261
Michael Chandda0e742016-12-29 12:13:40 -05007262 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7263 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007264 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7265 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7266 rc = -EPROTONOSUPPORT;
7267 goto err_free;
7268 }
Michael Chandda0e742016-12-29 12:13:40 -05007269 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7270 bp->hwrm_spec_code < 0x10601) {
7271 rc = -EPROTONOSUPPORT;
7272 goto err_free;
7273 }
Michael Chan61aad722017-02-12 19:18:14 -05007274 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7275 bp->hwrm_spec_code < 0x10601) {
7276 rc = -EPROTONOSUPPORT;
7277 goto err_free;
7278 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007279
Michael Chana54c4d72016-07-25 12:33:35 -04007280 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007281 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7282
7283 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7284 head = &bp->ntp_fltr_hash_tbl[idx];
7285 rcu_read_lock();
7286 hlist_for_each_entry_rcu(fltr, head, hash) {
7287 if (bnxt_fltr_match(fltr, new_fltr)) {
7288 rcu_read_unlock();
7289 rc = 0;
7290 goto err_free;
7291 }
7292 }
7293 rcu_read_unlock();
7294
7295 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007296 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7297 BNXT_NTP_FLTR_MAX_FLTR, 0);
7298 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007299 spin_unlock_bh(&bp->ntp_fltr_lock);
7300 rc = -ENOMEM;
7301 goto err_free;
7302 }
7303
Michael Chan84e86b92015-11-05 16:25:50 -05007304 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007305 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007306 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007307 new_fltr->rxq = rxq_index;
7308 hlist_add_head_rcu(&new_fltr->hash, head);
7309 bp->ntp_fltr_count++;
7310 spin_unlock_bh(&bp->ntp_fltr_lock);
7311
7312 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7313 schedule_work(&bp->sp_task);
7314
7315 return new_fltr->sw_id;
7316
7317err_free:
7318 kfree(new_fltr);
7319 return rc;
7320}
7321
7322static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7323{
7324 int i;
7325
7326 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7327 struct hlist_head *head;
7328 struct hlist_node *tmp;
7329 struct bnxt_ntuple_filter *fltr;
7330 int rc;
7331
7332 head = &bp->ntp_fltr_hash_tbl[i];
7333 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7334 bool del = false;
7335
7336 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7337 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7338 fltr->flow_id,
7339 fltr->sw_id)) {
7340 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7341 fltr);
7342 del = true;
7343 }
7344 } else {
7345 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7346 fltr);
7347 if (rc)
7348 del = true;
7349 else
7350 set_bit(BNXT_FLTR_VALID, &fltr->state);
7351 }
7352
7353 if (del) {
7354 spin_lock_bh(&bp->ntp_fltr_lock);
7355 hlist_del_rcu(&fltr->hash);
7356 bp->ntp_fltr_count--;
7357 spin_unlock_bh(&bp->ntp_fltr_lock);
7358 synchronize_rcu();
7359 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7360 kfree(fltr);
7361 }
7362 }
7363 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007364 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7365 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007366}
7367
7368#else
7369
7370static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7371{
7372}
7373
7374#endif /* CONFIG_RFS_ACCEL */
7375
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007376static void bnxt_udp_tunnel_add(struct net_device *dev,
7377 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007378{
7379 struct bnxt *bp = netdev_priv(dev);
7380
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007381 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7382 return;
7383
Michael Chanc0c050c2015-10-22 16:01:17 -04007384 if (!netif_running(dev))
7385 return;
7386
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007387 switch (ti->type) {
7388 case UDP_TUNNEL_TYPE_VXLAN:
7389 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7390 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007391
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007392 bp->vxlan_port_cnt++;
7393 if (bp->vxlan_port_cnt == 1) {
7394 bp->vxlan_port = ti->port;
7395 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007396 schedule_work(&bp->sp_task);
7397 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007398 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007399 case UDP_TUNNEL_TYPE_GENEVE:
7400 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7401 return;
7402
7403 bp->nge_port_cnt++;
7404 if (bp->nge_port_cnt == 1) {
7405 bp->nge_port = ti->port;
7406 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7407 }
7408 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007409 default:
7410 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007411 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007412
7413 schedule_work(&bp->sp_task);
7414}
7415
7416static void bnxt_udp_tunnel_del(struct net_device *dev,
7417 struct udp_tunnel_info *ti)
7418{
7419 struct bnxt *bp = netdev_priv(dev);
7420
7421 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7422 return;
7423
7424 if (!netif_running(dev))
7425 return;
7426
7427 switch (ti->type) {
7428 case UDP_TUNNEL_TYPE_VXLAN:
7429 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7430 return;
7431 bp->vxlan_port_cnt--;
7432
7433 if (bp->vxlan_port_cnt != 0)
7434 return;
7435
7436 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7437 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007438 case UDP_TUNNEL_TYPE_GENEVE:
7439 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7440 return;
7441 bp->nge_port_cnt--;
7442
7443 if (bp->nge_port_cnt != 0)
7444 return;
7445
7446 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7447 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007448 default:
7449 return;
7450 }
7451
7452 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007453}
7454
Michael Chan39d8ba22017-07-24 12:34:22 -04007455static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7456 struct net_device *dev, u32 filter_mask,
7457 int nlflags)
7458{
7459 struct bnxt *bp = netdev_priv(dev);
7460
7461 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7462 nlflags, filter_mask, NULL);
7463}
7464
7465static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7466 u16 flags)
7467{
7468 struct bnxt *bp = netdev_priv(dev);
7469 struct nlattr *attr, *br_spec;
7470 int rem, rc = 0;
7471
7472 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7473 return -EOPNOTSUPP;
7474
7475 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7476 if (!br_spec)
7477 return -EINVAL;
7478
7479 nla_for_each_nested(attr, br_spec, rem) {
7480 u16 mode;
7481
7482 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7483 continue;
7484
7485 if (nla_len(attr) < sizeof(mode))
7486 return -EINVAL;
7487
7488 mode = nla_get_u16(attr);
7489 if (mode == bp->br_mode)
7490 break;
7491
7492 rc = bnxt_hwrm_set_br_mode(bp, mode);
7493 if (!rc)
7494 bp->br_mode = mode;
7495 break;
7496 }
7497 return rc;
7498}
7499
Michael Chanc0c050c2015-10-22 16:01:17 -04007500static const struct net_device_ops bnxt_netdev_ops = {
7501 .ndo_open = bnxt_open,
7502 .ndo_start_xmit = bnxt_start_xmit,
7503 .ndo_stop = bnxt_close,
7504 .ndo_get_stats64 = bnxt_get_stats64,
7505 .ndo_set_rx_mode = bnxt_set_rx_mode,
7506 .ndo_do_ioctl = bnxt_ioctl,
7507 .ndo_validate_addr = eth_validate_addr,
7508 .ndo_set_mac_address = bnxt_change_mac_addr,
7509 .ndo_change_mtu = bnxt_change_mtu,
7510 .ndo_fix_features = bnxt_fix_features,
7511 .ndo_set_features = bnxt_set_features,
7512 .ndo_tx_timeout = bnxt_tx_timeout,
7513#ifdef CONFIG_BNXT_SRIOV
7514 .ndo_get_vf_config = bnxt_get_vf_config,
7515 .ndo_set_vf_mac = bnxt_set_vf_mac,
7516 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7517 .ndo_set_vf_rate = bnxt_set_vf_bw,
7518 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7519 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7520#endif
7521#ifdef CONFIG_NET_POLL_CONTROLLER
7522 .ndo_poll_controller = bnxt_poll_controller,
7523#endif
7524 .ndo_setup_tc = bnxt_setup_tc,
7525#ifdef CONFIG_RFS_ACCEL
7526 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7527#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007528 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7529 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007530 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007531 .ndo_bridge_getlink = bnxt_bridge_getlink,
7532 .ndo_bridge_setlink = bnxt_bridge_setlink,
Michael Chanc0c050c2015-10-22 16:01:17 -04007533};
7534
7535static void bnxt_remove_one(struct pci_dev *pdev)
7536{
7537 struct net_device *dev = pci_get_drvdata(pdev);
7538 struct bnxt *bp = netdev_priv(dev);
7539
7540 if (BNXT_PF(bp))
7541 bnxt_sriov_disable(bp);
7542
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007543 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007544 unregister_netdev(dev);
7545 cancel_work_sync(&bp->sp_task);
7546 bp->sp_event = 0;
7547
Michael Chan78095922016-12-07 00:26:16 -05007548 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007549 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007550 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007551 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007552 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007553 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007554 kfree(bp->edev);
7555 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007556 if (bp->xdp_prog)
7557 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007558 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007559 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007560}
7561
7562static int bnxt_probe_phy(struct bnxt *bp)
7563{
7564 int rc = 0;
7565 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007566
Michael Chan170ce012016-04-05 14:08:57 -04007567 rc = bnxt_hwrm_phy_qcaps(bp);
7568 if (rc) {
7569 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7570 rc);
7571 return rc;
7572 }
7573
Michael Chanc0c050c2015-10-22 16:01:17 -04007574 rc = bnxt_update_link(bp, false);
7575 if (rc) {
7576 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7577 rc);
7578 return rc;
7579 }
7580
Michael Chan93ed8112016-06-13 02:25:37 -04007581 /* Older firmware does not have supported_auto_speeds, so assume
7582 * that all supported speeds can be autonegotiated.
7583 */
7584 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7585 link_info->support_auto_speeds = link_info->support_speeds;
7586
Michael Chanc0c050c2015-10-22 16:01:17 -04007587 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007588 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007589 link_info->autoneg = BNXT_AUTONEG_SPEED;
7590 if (bp->hwrm_spec_code >= 0x10201) {
7591 if (link_info->auto_pause_setting &
7592 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7593 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7594 } else {
7595 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7596 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007597 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007598 } else {
7599 link_info->req_link_speed = link_info->force_link_speed;
7600 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007601 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007602 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7603 link_info->req_flow_ctrl =
7604 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7605 else
7606 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007607 return rc;
7608}
7609
7610static int bnxt_get_max_irq(struct pci_dev *pdev)
7611{
7612 u16 ctrl;
7613
7614 if (!pdev->msix_cap)
7615 return 1;
7616
7617 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7618 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7619}
7620
Michael Chan6e6c5a52016-01-02 23:45:02 -05007621static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7622 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007623{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007624 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007625
Michael Chan379a80a2015-10-23 15:06:19 -04007626#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007627 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007628 *max_tx = bp->vf.max_tx_rings;
7629 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007630 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7631 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007632 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007633 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007634#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007635 {
7636 *max_tx = bp->pf.max_tx_rings;
7637 *max_rx = bp->pf.max_rx_rings;
7638 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7639 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7640 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007641 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007642 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7643 *max_cp -= 1;
7644 *max_rx -= 2;
7645 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007646 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7647 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007648 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007649}
7650
7651int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7652{
7653 int rx, tx, cp;
7654
7655 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7656 if (!rx || !tx || !cp)
7657 return -ENOMEM;
7658
7659 *max_rx = rx;
7660 *max_tx = tx;
7661 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7662}
7663
Michael Chane4060d32016-12-07 00:26:19 -05007664static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7665 bool shared)
7666{
7667 int rc;
7668
7669 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007670 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7671 /* Not enough rings, try disabling agg rings. */
7672 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7673 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7674 if (rc)
7675 return rc;
7676 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7677 bp->dev->hw_features &= ~NETIF_F_LRO;
7678 bp->dev->features &= ~NETIF_F_LRO;
7679 bnxt_set_ring_params(bp);
7680 }
Michael Chane4060d32016-12-07 00:26:19 -05007681
7682 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7683 int max_cp, max_stat, max_irq;
7684
7685 /* Reserve minimum resources for RoCE */
7686 max_cp = bnxt_get_max_func_cp_rings(bp);
7687 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7688 max_irq = bnxt_get_max_func_irqs(bp);
7689 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7690 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7691 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7692 return 0;
7693
7694 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7695 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7696 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7697 max_cp = min_t(int, max_cp, max_irq);
7698 max_cp = min_t(int, max_cp, max_stat);
7699 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7700 if (rc)
7701 rc = 0;
7702 }
7703 return rc;
7704}
7705
Michael Chan702c2212017-05-29 19:06:10 -04007706static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007707{
7708 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007709
7710 if (sh)
7711 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7712 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007713 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007714 if (rc)
7715 return rc;
7716 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7717 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007718
7719 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7720 if (rc)
7721 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7722
Michael Chan6e6c5a52016-01-02 23:45:02 -05007723 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7724 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7725 bp->tx_nr_rings + bp->rx_nr_rings;
7726 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007727 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7728 bp->rx_nr_rings++;
7729 bp->cp_nr_rings++;
7730 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007731 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007732}
7733
Michael Chan7b08f662016-12-07 00:26:18 -05007734void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7735{
7736 ASSERT_RTNL();
7737 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007738 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007739}
7740
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007741static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7742{
7743 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7744 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7745
7746 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7747 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7748 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7749 else
7750 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7751 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7752 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7753 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7754 "Unknown", width);
7755}
7756
Michael Chanc0c050c2015-10-22 16:01:17 -04007757static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7758{
7759 static int version_printed;
7760 struct net_device *dev;
7761 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007762 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007763
Ray Jui4e003382017-02-20 19:25:16 -05007764 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007765 return -ENODEV;
7766
Michael Chanc0c050c2015-10-22 16:01:17 -04007767 if (version_printed++ == 0)
7768 pr_info("%s", version);
7769
7770 max_irqs = bnxt_get_max_irq(pdev);
7771 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7772 if (!dev)
7773 return -ENOMEM;
7774
7775 bp = netdev_priv(dev);
7776
7777 if (bnxt_vf_pciid(ent->driver_data))
7778 bp->flags |= BNXT_FLAG_VF;
7779
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007780 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007781 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007782
7783 rc = bnxt_init_board(pdev, dev);
7784 if (rc < 0)
7785 goto init_err_free;
7786
7787 dev->netdev_ops = &bnxt_netdev_ops;
7788 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7789 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007790 pci_set_drvdata(pdev, dev);
7791
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007792 rc = bnxt_alloc_hwrm_resources(bp);
7793 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007794 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007795
7796 mutex_init(&bp->hwrm_cmd_lock);
7797 rc = bnxt_hwrm_ver_get(bp);
7798 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007799 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007800
Deepak Khungare605db82017-05-29 19:06:04 -04007801 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7802 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7803 if (rc)
7804 goto init_err_pci_clean;
7805 }
7806
Michael Chan3c2217a2017-03-08 18:44:32 -05007807 rc = bnxt_hwrm_func_reset(bp);
7808 if (rc)
7809 goto init_err_pci_clean;
7810
Rob Swindell5ac67d82016-09-19 03:58:03 -04007811 bnxt_hwrm_fw_set_time(bp);
7812
Michael Chanc0c050c2015-10-22 16:01:17 -04007813 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7814 NETIF_F_TSO | NETIF_F_TSO6 |
7815 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007816 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007817 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7818 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007819 NETIF_F_RXCSUM | NETIF_F_GRO;
7820
7821 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7822 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007823
Michael Chanc0c050c2015-10-22 16:01:17 -04007824 dev->hw_enc_features =
7825 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7826 NETIF_F_TSO | NETIF_F_TSO6 |
7827 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007828 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007829 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007830 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7831 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007832 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7833 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7834 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7835 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7836 dev->priv_flags |= IFF_UNICAST_FLT;
7837
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007838 /* MTU range: 60 - 9500 */
7839 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007840 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007841
Michael Chanc0c050c2015-10-22 16:01:17 -04007842#ifdef CONFIG_BNXT_SRIOV
7843 init_waitqueue_head(&bp->sriov_cfg_wait);
7844#endif
Michael Chan309369c2016-06-13 02:25:34 -04007845 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007846 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007847 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007848 else
7849 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007850
Michael Chanc0c050c2015-10-22 16:01:17 -04007851 rc = bnxt_hwrm_func_drv_rgtr(bp);
7852 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007853 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007854
Michael Chana1653b12016-12-07 00:26:20 -05007855 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7856 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007857 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007858
Michael Chana588e452016-12-07 00:26:21 -05007859 bp->ulp_probe = bnxt_ulp_probe;
7860
Michael Chanc0c050c2015-10-22 16:01:17 -04007861 /* Get the MAX capabilities for this function */
7862 rc = bnxt_hwrm_func_qcaps(bp);
7863 if (rc) {
7864 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7865 rc);
7866 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007867 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007868 }
7869
7870 rc = bnxt_hwrm_queue_qportcfg(bp);
7871 if (rc) {
7872 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7873 rc);
7874 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007875 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007876 }
7877
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007878 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007879 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007880 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007881 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007882
Michael Chanc61fb992017-02-06 16:55:36 -05007883 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007884 bnxt_set_tpa_flags(bp);
7885 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007886 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007887 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007888 if (rc) {
7889 netdev_err(bp->dev, "Not enough rings available.\n");
7890 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007891 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007892 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007893
Michael Chan87da7f72016-11-16 21:13:09 -05007894 /* Default RSS hash cfg. */
7895 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7896 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7897 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7898 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007899 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007900 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7901 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7902 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7903 }
7904
Michael Chan8fdefd62016-12-29 12:13:36 -05007905 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007906 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007907 dev->hw_features |= NETIF_F_NTUPLE;
7908 if (bnxt_rfs_capable(bp)) {
7909 bp->flags |= BNXT_FLAG_RFS;
7910 dev->features |= NETIF_F_NTUPLE;
7911 }
7912 }
7913
Michael Chanc0c050c2015-10-22 16:01:17 -04007914 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7915 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7916
7917 rc = bnxt_probe_phy(bp);
7918 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007919 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007920
Michael Chan78095922016-12-07 00:26:16 -05007921 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007922 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007923 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007924
Michael Chanc1ef1462017-04-04 18:14:07 -04007925 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007926 if (bp->flags & BNXT_FLAG_WOL_CAP)
7927 device_set_wakeup_enable(&pdev->dev, bp->wol);
7928 else
7929 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007930
Michael Chan78095922016-12-07 00:26:16 -05007931 rc = register_netdev(dev);
7932 if (rc)
7933 goto init_err_clr_int;
7934
Michael Chanc0c050c2015-10-22 16:01:17 -04007935 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7936 board_info[ent->driver_data].name,
7937 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7938
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007939 bnxt_parse_log_pcie_link(bp);
7940
Michael Chanc0c050c2015-10-22 16:01:17 -04007941 return 0;
7942
Michael Chan78095922016-12-07 00:26:16 -05007943init_err_clr_int:
7944 bnxt_clear_int_mode(bp);
7945
Sathya Perla17086392017-02-20 19:25:18 -05007946init_err_pci_clean:
7947 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007948
7949init_err_free:
7950 free_netdev(dev);
7951 return rc;
7952}
7953
Michael Chand196ece2017-04-04 18:14:08 -04007954static void bnxt_shutdown(struct pci_dev *pdev)
7955{
7956 struct net_device *dev = pci_get_drvdata(pdev);
7957 struct bnxt *bp;
7958
7959 if (!dev)
7960 return;
7961
7962 rtnl_lock();
7963 bp = netdev_priv(dev);
7964 if (!bp)
7965 goto shutdown_exit;
7966
7967 if (netif_running(dev))
7968 dev_close(dev);
7969
7970 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04007971 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007972 bnxt_clear_int_mode(bp);
7973 pci_wake_from_d3(pdev, bp->wol);
7974 pci_set_power_state(pdev, PCI_D3hot);
7975 }
7976
7977shutdown_exit:
7978 rtnl_unlock();
7979}
7980
Michael Chanf65a2042017-04-04 18:14:11 -04007981#ifdef CONFIG_PM_SLEEP
7982static int bnxt_suspend(struct device *device)
7983{
7984 struct pci_dev *pdev = to_pci_dev(device);
7985 struct net_device *dev = pci_get_drvdata(pdev);
7986 struct bnxt *bp = netdev_priv(dev);
7987 int rc = 0;
7988
7989 rtnl_lock();
7990 if (netif_running(dev)) {
7991 netif_device_detach(dev);
7992 rc = bnxt_close(dev);
7993 }
7994 bnxt_hwrm_func_drv_unrgtr(bp);
7995 rtnl_unlock();
7996 return rc;
7997}
7998
7999static int bnxt_resume(struct device *device)
8000{
8001 struct pci_dev *pdev = to_pci_dev(device);
8002 struct net_device *dev = pci_get_drvdata(pdev);
8003 struct bnxt *bp = netdev_priv(dev);
8004 int rc = 0;
8005
8006 rtnl_lock();
8007 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8008 rc = -ENODEV;
8009 goto resume_exit;
8010 }
8011 rc = bnxt_hwrm_func_reset(bp);
8012 if (rc) {
8013 rc = -EBUSY;
8014 goto resume_exit;
8015 }
8016 bnxt_get_wol_settings(bp);
8017 if (netif_running(dev)) {
8018 rc = bnxt_open(dev);
8019 if (!rc)
8020 netif_device_attach(dev);
8021 }
8022
8023resume_exit:
8024 rtnl_unlock();
8025 return rc;
8026}
8027
8028static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8029#define BNXT_PM_OPS (&bnxt_pm_ops)
8030
8031#else
8032
8033#define BNXT_PM_OPS NULL
8034
8035#endif /* CONFIG_PM_SLEEP */
8036
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008037/**
8038 * bnxt_io_error_detected - called when PCI error is detected
8039 * @pdev: Pointer to PCI device
8040 * @state: The current pci connection state
8041 *
8042 * This function is called after a PCI bus error affecting
8043 * this device has been detected.
8044 */
8045static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8046 pci_channel_state_t state)
8047{
8048 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008049 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008050
8051 netdev_info(netdev, "PCI I/O error detected\n");
8052
8053 rtnl_lock();
8054 netif_device_detach(netdev);
8055
Michael Chana588e452016-12-07 00:26:21 -05008056 bnxt_ulp_stop(bp);
8057
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008058 if (state == pci_channel_io_perm_failure) {
8059 rtnl_unlock();
8060 return PCI_ERS_RESULT_DISCONNECT;
8061 }
8062
8063 if (netif_running(netdev))
8064 bnxt_close(netdev);
8065
8066 pci_disable_device(pdev);
8067 rtnl_unlock();
8068
8069 /* Request a slot slot reset. */
8070 return PCI_ERS_RESULT_NEED_RESET;
8071}
8072
8073/**
8074 * bnxt_io_slot_reset - called after the pci bus has been reset.
8075 * @pdev: Pointer to PCI device
8076 *
8077 * Restart the card from scratch, as if from a cold-boot.
8078 * At this point, the card has exprienced a hard reset,
8079 * followed by fixups by BIOS, and has its config space
8080 * set up identically to what it was at cold boot.
8081 */
8082static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8083{
8084 struct net_device *netdev = pci_get_drvdata(pdev);
8085 struct bnxt *bp = netdev_priv(netdev);
8086 int err = 0;
8087 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8088
8089 netdev_info(bp->dev, "PCI Slot Reset\n");
8090
8091 rtnl_lock();
8092
8093 if (pci_enable_device(pdev)) {
8094 dev_err(&pdev->dev,
8095 "Cannot re-enable PCI device after reset.\n");
8096 } else {
8097 pci_set_master(pdev);
8098
Michael Chanaa8ed022016-12-07 00:26:17 -05008099 err = bnxt_hwrm_func_reset(bp);
8100 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008101 err = bnxt_open(netdev);
8102
Michael Chana588e452016-12-07 00:26:21 -05008103 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008104 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008105 bnxt_ulp_start(bp);
8106 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008107 }
8108
8109 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8110 dev_close(netdev);
8111
8112 rtnl_unlock();
8113
8114 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8115 if (err) {
8116 dev_err(&pdev->dev,
8117 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8118 err); /* non-fatal, continue */
8119 }
8120
8121 return PCI_ERS_RESULT_RECOVERED;
8122}
8123
8124/**
8125 * bnxt_io_resume - called when traffic can start flowing again.
8126 * @pdev: Pointer to PCI device
8127 *
8128 * This callback is called when the error recovery driver tells
8129 * us that its OK to resume normal operation.
8130 */
8131static void bnxt_io_resume(struct pci_dev *pdev)
8132{
8133 struct net_device *netdev = pci_get_drvdata(pdev);
8134
8135 rtnl_lock();
8136
8137 netif_device_attach(netdev);
8138
8139 rtnl_unlock();
8140}
8141
8142static const struct pci_error_handlers bnxt_err_handler = {
8143 .error_detected = bnxt_io_error_detected,
8144 .slot_reset = bnxt_io_slot_reset,
8145 .resume = bnxt_io_resume
8146};
8147
Michael Chanc0c050c2015-10-22 16:01:17 -04008148static struct pci_driver bnxt_pci_driver = {
8149 .name = DRV_MODULE_NAME,
8150 .id_table = bnxt_pci_tbl,
8151 .probe = bnxt_init_one,
8152 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008153 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008154 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008155 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008156#if defined(CONFIG_BNXT_SRIOV)
8157 .sriov_configure = bnxt_sriov_configure,
8158#endif
8159};
8160
8161module_pci_driver(bnxt_pci_driver);